From 2016511b21a8d2606123f1d76a20412bcfbfeccf Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Sat, 23 Nov 2024 09:14:31 +0100 Subject: [PATCH] asm: refactor push output --- asm/compiler.go | 5 +---- internal/evm/instruction_set.go | 8 ++++++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/asm/compiler.go b/asm/compiler.go index 8293fd0..76a1cca 100644 --- a/asm/compiler.go +++ b/asm/compiler.go @@ -24,7 +24,6 @@ import ( "fmt" "io/fs" "path" - "strconv" "strings" "github.com/fjl/geas/internal/ast" @@ -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 { @@ -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) } diff --git a/internal/evm/instruction_set.go b/internal/evm/instruction_set.go index a8ab7e7..b3b6aca 100644 --- a/internal/evm/instruction_set.go +++ b/internal/evm/instruction_set.go @@ -20,6 +20,7 @@ import ( "fmt" "maps" "slices" + "strconv" "strings" ) @@ -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]