Skip to content

Commit

Permalink
asm: refactor push output
Browse files Browse the repository at this point in the history
  • Loading branch information
fjl committed Nov 23, 2024
1 parent 8f5b81e commit 2016511
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 1 addition & 4 deletions asm/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"fmt"
"io/fs"
"path"
"strconv"
"strings"

"github.com/fjl/geas/internal/ast"
Expand Down Expand Up @@ -285,7 +284,6 @@ func (c *Compiler) generateOutput(prog *compilerProg) []byte {
return nil
}

pushNameBuf := []byte{'P', 'U', 'S', 'H', 0, 0}
var output []byte
for _, inst := range prog.iterInstructions() {
if len(output) != inst.pc {
Expand All @@ -308,8 +306,7 @@ func (c *Compiler) generateOutput(prog *compilerProg) []byte {
if size == 0 && !prog.evm.SupportsPush0() {
size = 1
}
pushName := strconv.AppendInt(pushNameBuf[:4], int64(size), 10)
op = prog.evm.OpByName(string(pushName))
op = prog.evm.PushBySize(size)
} else {
op = prog.evm.OpByName(inst.op)
}
Expand Down
8 changes: 8 additions & 0 deletions internal/evm/instruction_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"maps"
"slices"
"strconv"
"strings"
)

Expand Down Expand Up @@ -79,6 +80,13 @@ func (is *InstructionSet) OpByName(opname string) *Op {
return is.byName[opname]
}

// PushBySize resolves a push op by its size.
func (is *InstructionSet) PushBySize(size int) *Op {
buf := []byte{'P', 'U', 'S', 'H', 0, 0}
name := strconv.AppendInt(buf[:4], int64(size), 10)
return is.byName[string(name)]
}

// OpByCode resolves an opcode by its code.
func (is *InstructionSet) OpByCode(code byte) *Op {
return is.byCode[code]
Expand Down

0 comments on commit 2016511

Please sign in to comment.