Skip to content

Commit

Permalink
debug pring
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny2022da committed Feb 19, 2024
1 parent ff1aed2 commit 0f69a50
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/vm/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
}()
}

log.Warn("Interpreter Run (gasps)", "ContractAddr", contract.Address(), "CodeAddress", contract.CodeAddr, "Code", common.Bytes2Hex(contract.Code))
// The Interpreter main run loop (contextual). This loop runs until either an
// explicit STOP, RETURN or SELFDESTRUCT is executed, an error occurred during
// the execution of one of the operations or until the done flag is set by the
Expand Down
43 changes: 43 additions & 0 deletions core/vm/opcodeProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func GenOrLoadOptimizedCode(address common.Address, code []byte, codeHash common
//TODO - dav: - make following in one func.
//todo - dav: if cache number > 5. clear. maybe instead reinstall at setcallcode().
err = codeCache.UpdateCodeCache(address, processedCode, codeHash)

log.Warn("GenerateOptimizedCode (gasps)", "address", address.Hex())
log.Warn("GenerateOptimizedCode (gasps)", "orgStr", codeToString(code))
log.Warn("GenerateOptimizedCode (gasps)", "optStr", codeToString(processedCode))

if err != nil {
log.Error("Not update code cache", "err", err)
}
Expand Down Expand Up @@ -104,6 +109,9 @@ func doCodeFusion(code []byte) ([]byte, error) {
// ShlAndSub is actually worked like pushed an uint256,
// todo-dav: replace with push32.
op := ShlAndSub

log.Warn("Optimize Code (gasps)", "code", op.String())

fusedCode[cur] = byte(op)
codeCache := compiler.GetOpCodeCacheInstance()
codeCache.CacheShlAndSubMap(x, y, z, val)
Expand All @@ -127,6 +135,7 @@ func doCodeFusion(code []byte) ([]byte, error) {
code4 := OpCode(fusedCode[cur+4])
if code0 == AND && code1 == SWAP1 && code2 == POP && code3 == SWAP2 && code4 == SWAP1 {
op := AndSwap1PopSwap2Swap1
log.Warn("Optimize Code (gasps)", "code", op.String())
fusedCode[cur] = byte(op)
fusedCode[cur+1] = byte(Nop)
fusedCode[cur+2] = byte(Nop)
Expand All @@ -138,6 +147,7 @@ func doCodeFusion(code []byte) ([]byte, error) {
// Test zero and Jump. target offset at code[2-3]
if code0 == ISZERO && code1 == PUSH2 && code4 == JUMPI {
op := JumpIfZero
log.Warn("Optimize Code (gasps)", "code", op.String())
fusedCode[cur] = byte(op)
fusedCode[cur+1] = byte(Nop)
fusedCode[cur+4] = byte(Nop)
Expand All @@ -164,6 +174,7 @@ func doCodeFusion(code []byte) ([]byte, error) {
code3 := OpCode(fusedCode[cur+3])
if code0 == SWAP2 && code1 == SWAP1 && code2 == POP && code3 == JUMP {
op := Swap2Swap1PopJump
log.Warn("Optimize Code (gasps)", "code", op.String())
fusedCode[cur] = byte(op)
fusedCode[cur+1] = byte(Nop)
fusedCode[cur+2] = byte(Nop)
Expand All @@ -173,6 +184,7 @@ func doCodeFusion(code []byte) ([]byte, error) {

if code0 == SWAP1 && code1 == POP && code2 == SWAP2 && code3 == SWAP1 {
op := Swap1PopSwap2Swap1
log.Warn("Optimize Code (gasps)", "code", op.String())
fusedCode[cur] = byte(op)
fusedCode[cur+1] = byte(Nop)
fusedCode[cur+2] = byte(Nop)
Expand All @@ -182,6 +194,7 @@ func doCodeFusion(code []byte) ([]byte, error) {

if code0 == POP && code1 == SWAP2 && code2 == SWAP1 && code3 == POP {
op := PopSwap2Swap1Pop
log.Warn("Optimize Code (gasps)", "code", op.String())
fusedCode[cur] = byte(op)
fusedCode[cur+1] = byte(Nop)
fusedCode[cur+2] = byte(Nop)
Expand All @@ -191,20 +204,23 @@ func doCodeFusion(code []byte) ([]byte, error) {
// push and jump
if code0 == PUSH2 && code3 == JUMP {
op := Push2Jump
log.Warn("Optimize Code (gasps)", "code", op.String())
fusedCode[cur] = byte(op)
fusedCode[cur+3] = byte(Nop)
skipToNext = true
}

if code0 == PUSH2 && code3 == JUMPI {
op := Push2JumpI
log.Warn("Optimize Code (gasps)", "code", op.String())
fusedCode[cur] = byte(op)
fusedCode[cur+3] = byte(Nop)
skipToNext = true
}

if code0 == PUSH1 && code2 == PUSH1 {
op := Push1Push1
log.Warn("Optimize Code (gasps)", "code", op.String())
fusedCode[cur] = byte(op)
fusedCode[cur+2] = byte(Nop)
skipToNext = true
Expand All @@ -223,19 +239,22 @@ func doCodeFusion(code []byte) ([]byte, error) {
if code0 == PUSH1 {
if code2 == ADD {
op := Push1Add
log.Warn("Optimize Code (gasps)", "code", op.String())
fusedCode[cur] = byte(op)
fusedCode[cur+2] = byte(Nop)
skipToNext = true
}
if code2 == SHL {
op := Push1Shl
log.Warn("Optimize Code (gasps)", "code", op.String())
fusedCode[cur] = byte(op)
fusedCode[cur+2] = byte(Nop)
skipToNext = true
}

if code2 == DUP1 {
op := Push1Dup1
log.Warn("Optimize Code (gasps)", "code", op.String())
fusedCode[cur] = byte(op)
fusedCode[cur+2] = byte(Nop)
skipToNext = true
Expand All @@ -254,40 +273,46 @@ func doCodeFusion(code []byte) ([]byte, error) {

if code0 == SWAP1 && code1 == POP {
op := Swap1Pop
log.Warn("Optimize Code (gasps)", "code", op.String())
fusedCode[cur] = byte(op)
fusedCode[cur+1] = byte(Nop)
skipToNext = true
}
if code0 == POP && code1 == JUMP {
op := PopJump
log.Warn("Optimize Code (gasps)", "code", op.String())
fusedCode[cur] = byte(op)
fusedCode[cur+1] = byte(Nop)
skipToNext = true
}

if code0 == POP && code1 == POP {
op := Pop2
log.Warn("Optimize Code (gasps)", "code", op.String())
fusedCode[cur] = byte(op)
fusedCode[cur+1] = byte(Nop)
skipToNext = true
}

if code0 == SWAP2 && code1 == SWAP1 {
op := Swap2Swap1
log.Warn("Optimize Code (gasps)", "code", op.String())
fusedCode[cur] = byte(op)
fusedCode[cur+1] = byte(Nop)
skipToNext = true
}

if code0 == SWAP2 && code1 == POP {
op := Swap2Pop
log.Warn("Optimize Code (gasps)", "code", op.String())
fusedCode[cur] = byte(op)
fusedCode[cur+1] = byte(Nop)
skipToNext = true
}

if code0 == DUP2 && code1 == LT {
op := Dup2LT
log.Warn("Optimize Code (gasps)", "code", op.String())
fusedCode[cur] = byte(op)
fusedCode[cur+1] = byte(Nop)
skipToNext = true
Expand Down Expand Up @@ -339,3 +364,21 @@ func calculateSkipSteps(code []byte, cur int) (skip bool, steps int) {
}
return skip, steps
}

func codeToString(code []byte) string {
length := len(code)
codeStr := ""
for i := 0; i < length; i++ {
cur := i
opcode := OpCode(code[i])
codeStr += opcode.String() + " "

skip, steps := calculateSkipSteps(code, cur)
if skip {
i += steps
continue
}

}
return codeStr
}

0 comments on commit 0f69a50

Please sign in to comment.