From ca13e3b1058f0d680b79dc1d9319d427a09493f8 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 15 Apr 2014 16:16:38 -0400 Subject: Moved assembler stage processing to it's own file --- ethchain/asm.go | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 ethchain/asm.go (limited to 'ethchain/asm.go') diff --git a/ethchain/asm.go b/ethchain/asm.go new file mode 100644 index 000000000..5f901f8a2 --- /dev/null +++ b/ethchain/asm.go @@ -0,0 +1,126 @@ +package ethchain + +import ( + "fmt" + "github.com/ethereum/eth-go/ethutil" + "math/big" + "regexp" +) + +func CompileInstr(s interface{}) ([]byte, error) { + switch s.(type) { + case string: + str := s.(string) + isOp := IsOpCode(str) + if isOp { + return []byte{OpCodes[str]}, nil + } + + num := new(big.Int) + _, success := num.SetString(str, 0) + // Assume regular bytes during compilation + if !success { + num.SetBytes([]byte(str)) + } else { + // tmp fix for 32 bytes + n := ethutil.BigToBytes(num, 256) + return n, nil + } + + return num.Bytes(), nil + case int: + num := ethutil.BigToBytes(big.NewInt(int64(s.(int))), 256) + return num, nil + case []byte: + return ethutil.BigD(s.([]byte)).Bytes(), nil + } + + return nil, nil +} + +// Script compilation functions +// Compiles strings to machine code +func Assemble(instructions ...interface{}) (script []byte) { + //script = make([]string, len(instructions)) + + for _, val := range instructions { + instr, _ := CompileInstr(val) + + //script[i] = string(instr) + script = append(script, instr...) + } + + return +} + +func Disassemble(script []byte) (asm []string) { + pc := new(big.Int) + for { + if pc.Cmp(big.NewInt(int64(len(script)))) >= 0 { + return + } + + // Get the memory location of pc + val := script[pc.Int64()] + // Get the opcode (it must be an opcode!) + op := OpCode(val) + + asm = append(asm, fmt.Sprintf("%v", op)) + + switch op { + case oPUSH: // Push PC+1 on to the stack + pc.Add(pc, ethutil.Big1) + data := script[pc.Int64() : pc.Int64()+32] + val := ethutil.BigD(data) + + var b []byte + if val.Int64() == 0 { + b = []byte{0} + } else { + b = val.Bytes() + } + + asm = append(asm, fmt.Sprintf("0x%x", b)) + + pc.Add(pc, big.NewInt(31)) + case oPUSH20: + pc.Add(pc, ethutil.Big1) + data := script[pc.Int64() : pc.Int64()+20] + val := ethutil.BigD(data) + var b []byte + if val.Int64() == 0 { + b = []byte{0} + } else { + b = val.Bytes() + } + + asm = append(asm, fmt.Sprintf("0x%x", b)) + + pc.Add(pc, big.NewInt(19)) + } + + pc.Add(pc, ethutil.Big1) + } + + return +} + +func PreProcess(data string) (mainInput, initInput string) { + reg := "\\(\\)\\s*{([\\d\\w\\W\\n\\s]+?)}" + mainReg := regexp.MustCompile("main" + reg) + initReg := regexp.MustCompile("init" + reg) + + main := mainReg.FindStringSubmatch(data) + if len(main) > 0 { + mainInput = main[1] + } else { + mainInput = data + } + + init := initReg.FindStringSubmatch(data) + if len(init) > 0 { + initInput = init[1] + } + + return +} -- cgit v1.2.3 From c5729d7ecc564f8eff6df565173a4f5cc6c43cb0 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 16 Apr 2014 04:07:52 +0200 Subject: comments --- ethchain/asm.go | 1 + 1 file changed, 1 insertion(+) (limited to 'ethchain/asm.go') diff --git a/ethchain/asm.go b/ethchain/asm.go index 5f901f8a2..a6c85cb60 100644 --- a/ethchain/asm.go +++ b/ethchain/asm.go @@ -106,6 +106,7 @@ func Disassemble(script []byte) (asm []string) { } func PreProcess(data string) (mainInput, initInput string) { + // Regexp for parsing anything between brackets reg := "\\(\\)\\s*{([\\d\\w\\W\\n\\s]+?)}" mainReg := regexp.MustCompile("main" + reg) initReg := regexp.MustCompile("init" + reg) -- cgit v1.2.3 From 5516efdfa0494e028fc3649e4a38da81c56ed598 Mon Sep 17 00:00:00 2001 From: obscuren Date: Sun, 27 Apr 2014 18:05:30 +0200 Subject: Removed old code --- ethchain/asm.go | 68 --------------------------------------------------------- 1 file changed, 68 deletions(-) (limited to 'ethchain/asm.go') diff --git a/ethchain/asm.go b/ethchain/asm.go index a6c85cb60..3194549ba 100644 --- a/ethchain/asm.go +++ b/ethchain/asm.go @@ -4,55 +4,8 @@ import ( "fmt" "github.com/ethereum/eth-go/ethutil" "math/big" - "regexp" ) -func CompileInstr(s interface{}) ([]byte, error) { - switch s.(type) { - case string: - str := s.(string) - isOp := IsOpCode(str) - if isOp { - return []byte{OpCodes[str]}, nil - } - - num := new(big.Int) - _, success := num.SetString(str, 0) - // Assume regular bytes during compilation - if !success { - num.SetBytes([]byte(str)) - } else { - // tmp fix for 32 bytes - n := ethutil.BigToBytes(num, 256) - return n, nil - } - - return num.Bytes(), nil - case int: - num := ethutil.BigToBytes(big.NewInt(int64(s.(int))), 256) - return num, nil - case []byte: - return ethutil.BigD(s.([]byte)).Bytes(), nil - } - - return nil, nil -} - -// Script compilation functions -// Compiles strings to machine code -func Assemble(instructions ...interface{}) (script []byte) { - //script = make([]string, len(instructions)) - - for _, val := range instructions { - instr, _ := CompileInstr(val) - - //script[i] = string(instr) - script = append(script, instr...) - } - - return -} - func Disassemble(script []byte) (asm []string) { pc := new(big.Int) for { @@ -104,24 +57,3 @@ func Disassemble(script []byte) (asm []string) { return } - -func PreProcess(data string) (mainInput, initInput string) { - // Regexp for parsing anything between brackets - reg := "\\(\\)\\s*{([\\d\\w\\W\\n\\s]+?)}" - mainReg := regexp.MustCompile("main" + reg) - initReg := regexp.MustCompile("init" + reg) - - main := mainReg.FindStringSubmatch(data) - if len(main) > 0 { - mainInput = main[1] - } else { - mainInput = data - } - - init := initReg.FindStringSubmatch(data) - if len(init) > 0 { - initInput = init[1] - } - - return -} -- cgit v1.2.3 From a0af7de58eeba598c8e967ae9deefb4ee287a1df Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 6 May 2014 17:43:27 +0200 Subject: Optimizations --- ethchain/asm.go | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) (limited to 'ethchain/asm.go') diff --git a/ethchain/asm.go b/ethchain/asm.go index 3194549ba..d46e46af7 100644 --- a/ethchain/asm.go +++ b/ethchain/asm.go @@ -21,7 +21,7 @@ func Disassemble(script []byte) (asm []string) { asm = append(asm, fmt.Sprintf("%v", op)) switch op { - case oPUSH: // Push PC+1 on to the stack + case oPUSH32: // Push PC+1 on to the stack pc.Add(pc, ethutil.Big1) data := script[pc.Int64() : pc.Int64()+32] val := ethutil.BigD(data) @@ -36,20 +36,6 @@ func Disassemble(script []byte) (asm []string) { asm = append(asm, fmt.Sprintf("0x%x", b)) pc.Add(pc, big.NewInt(31)) - case oPUSH20: - pc.Add(pc, ethutil.Big1) - data := script[pc.Int64() : pc.Int64()+20] - val := ethutil.BigD(data) - var b []byte - if val.Int64() == 0 { - b = []byte{0} - } else { - b = val.Bytes() - } - - asm = append(asm, fmt.Sprintf("0x%x", b)) - - pc.Add(pc, big.NewInt(19)) } pc.Add(pc, ethutil.Big1) -- cgit v1.2.3 From 554f4f6f7d8b8bc5332d42631ec9de0d7015eccf Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 8 May 2014 14:20:06 +0200 Subject: Fixed disasamble for all pushes --- ethchain/asm.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'ethchain/asm.go') diff --git a/ethchain/asm.go b/ethchain/asm.go index d46e46af7..492be0999 100644 --- a/ethchain/asm.go +++ b/ethchain/asm.go @@ -21,9 +21,10 @@ func Disassemble(script []byte) (asm []string) { asm = append(asm, fmt.Sprintf("%v", op)) switch op { - case oPUSH32: // Push PC+1 on to the stack + case oPUSH1, oPUSH2, oPUSH3, oPUSH4, oPUSH5, oPUSH6, oPUSH7, oPUSH8, oPUSH9, oPUSH10, oPUSH11, oPUSH12, oPUSH13, oPUSH14, oPUSH15, oPUSH16, oPUSH17, oPUSH18, oPUSH19, oPUSH20, oPUSH21, oPUSH22, oPUSH23, oPUSH24, oPUSH25, oPUSH26, oPUSH27, oPUSH28, oPUSH29, oPUSH30, oPUSH31, oPUSH32: pc.Add(pc, ethutil.Big1) - data := script[pc.Int64() : pc.Int64()+32] + a := int64(op) - int64(oPUSH1) + 1 + data := script[pc.Int64() : pc.Int64()+a] val := ethutil.BigD(data) var b []byte @@ -35,7 +36,7 @@ func Disassemble(script []byte) (asm []string) { asm = append(asm, fmt.Sprintf("0x%x", b)) - pc.Add(pc, big.NewInt(31)) + pc.Add(pc, big.NewInt(a-1)) } pc.Add(pc, ethutil.Big1) -- cgit v1.2.3 From 98d4b511207404a133ceca37467f9a1c32c20bc5 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 28 May 2014 12:03:12 +0200 Subject: Changed opcode names --- ethchain/asm.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ethchain/asm.go') diff --git a/ethchain/asm.go b/ethchain/asm.go index 492be0999..430a89450 100644 --- a/ethchain/asm.go +++ b/ethchain/asm.go @@ -21,9 +21,9 @@ func Disassemble(script []byte) (asm []string) { asm = append(asm, fmt.Sprintf("%v", op)) switch op { - case oPUSH1, oPUSH2, oPUSH3, oPUSH4, oPUSH5, oPUSH6, oPUSH7, oPUSH8, oPUSH9, oPUSH10, oPUSH11, oPUSH12, oPUSH13, oPUSH14, oPUSH15, oPUSH16, oPUSH17, oPUSH18, oPUSH19, oPUSH20, oPUSH21, oPUSH22, oPUSH23, oPUSH24, oPUSH25, oPUSH26, oPUSH27, oPUSH28, oPUSH29, oPUSH30, oPUSH31, oPUSH32: + case PUSH1, PUSH2, PUSH3, PUSH4, PUSH5, PUSH6, PUSH7, PUSH8, PUSH9, PUSH10, PUSH11, PUSH12, PUSH13, PUSH14, PUSH15, PUSH16, PUSH17, PUSH18, PUSH19, PUSH20, PUSH21, PUSH22, PUSH23, PUSH24, PUSH25, PUSH26, PUSH27, PUSH28, PUSH29, PUSH30, PUSH31, PUSH32: pc.Add(pc, ethutil.Big1) - a := int64(op) - int64(oPUSH1) + 1 + a := int64(op) - int64(PUSH1) + 1 data := script[pc.Int64() : pc.Int64()+a] val := ethutil.BigD(data) -- cgit v1.2.3 From d078e9b8c92fb3bd5789a8e39c169b19864e0a04 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 13 Jun 2014 12:45:11 +0200 Subject: Refactoring state transitioning --- ethchain/asm.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'ethchain/asm.go') diff --git a/ethchain/asm.go b/ethchain/asm.go index 430a89450..277326ff9 100644 --- a/ethchain/asm.go +++ b/ethchain/asm.go @@ -25,16 +25,10 @@ func Disassemble(script []byte) (asm []string) { pc.Add(pc, ethutil.Big1) a := int64(op) - int64(PUSH1) + 1 data := script[pc.Int64() : pc.Int64()+a] - val := ethutil.BigD(data) - - var b []byte - if val.Int64() == 0 { - b = []byte{0} - } else { - b = val.Bytes() + if len(data) == 0 { + data = []byte{0} } - - asm = append(asm, fmt.Sprintf("0x%x", b)) + asm = append(asm, fmt.Sprintf("0x%x", data)) pc.Add(pc, big.NewInt(a-1)) } -- cgit v1.2.3 From 63883bf27d8b87f601e1603e9024a279b91bffb7 Mon Sep 17 00:00:00 2001 From: obscuren Date: Sat, 14 Jun 2014 11:46:09 +0200 Subject: Moving closer to interop --- ethchain/asm.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ethchain/asm.go') diff --git a/ethchain/asm.go b/ethchain/asm.go index 277326ff9..c267f9b55 100644 --- a/ethchain/asm.go +++ b/ethchain/asm.go @@ -28,7 +28,7 @@ func Disassemble(script []byte) (asm []string) { if len(data) == 0 { data = []byte{0} } - asm = append(asm, fmt.Sprintf("0x%x", data)) + asm = append(asm, fmt.Sprintf("%#x", data)) pc.Add(pc, big.NewInt(a-1)) } -- cgit v1.2.3 From 8a885c2606fbf675770cf40b31f9ceb5ef8acae9 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 18 Jun 2014 00:25:58 +0200 Subject: Fixed GT and LT --- ethchain/asm.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ethchain/asm.go') diff --git a/ethchain/asm.go b/ethchain/asm.go index c267f9b55..277326ff9 100644 --- a/ethchain/asm.go +++ b/ethchain/asm.go @@ -28,7 +28,7 @@ func Disassemble(script []byte) (asm []string) { if len(data) == 0 { data = []byte{0} } - asm = append(asm, fmt.Sprintf("%#x", data)) + asm = append(asm, fmt.Sprintf("0x%x", data)) pc.Add(pc, big.NewInt(a-1)) } -- cgit v1.2.3 From 39cb34850a573ea9b2ea73eb624139684502bc79 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 26 Jun 2014 11:25:43 +0200 Subject: Added instruction numbers --- ethchain/asm.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ethchain/asm.go') diff --git a/ethchain/asm.go b/ethchain/asm.go index 277326ff9..09d6af56f 100644 --- a/ethchain/asm.go +++ b/ethchain/asm.go @@ -18,7 +18,7 @@ func Disassemble(script []byte) (asm []string) { // Get the opcode (it must be an opcode!) op := OpCode(val) - asm = append(asm, fmt.Sprintf("%v", op)) + asm = append(asm, fmt.Sprintf("%04v: %v", pc, op)) switch op { case PUSH1, PUSH2, PUSH3, PUSH4, PUSH5, PUSH6, PUSH7, PUSH8, PUSH9, PUSH10, PUSH11, PUSH12, PUSH13, PUSH14, PUSH15, PUSH16, PUSH17, PUSH18, PUSH19, PUSH20, PUSH21, PUSH22, PUSH23, PUSH24, PUSH25, PUSH26, PUSH27, PUSH28, PUSH29, PUSH30, PUSH31, PUSH32: @@ -28,7 +28,7 @@ func Disassemble(script []byte) (asm []string) { if len(data) == 0 { data = []byte{0} } - asm = append(asm, fmt.Sprintf("0x%x", data)) + asm = append(asm, fmt.Sprintf("%04v: 0x%x", pc, data)) pc.Add(pc, big.NewInt(a-1)) } -- cgit v1.2.3 From 9010857677ac374e09bab62a89f2fb52c11ed6d3 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 11 Jul 2014 16:04:09 +0200 Subject: Special diff output for execution --- ethchain/asm.go | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'ethchain/asm.go') diff --git a/ethchain/asm.go b/ethchain/asm.go index 09d6af56f..2697953fd 100644 --- a/ethchain/asm.go +++ b/ethchain/asm.go @@ -24,6 +24,10 @@ func Disassemble(script []byte) (asm []string) { case PUSH1, PUSH2, PUSH3, PUSH4, PUSH5, PUSH6, PUSH7, PUSH8, PUSH9, PUSH10, PUSH11, PUSH12, PUSH13, PUSH14, PUSH15, PUSH16, PUSH17, PUSH18, PUSH19, PUSH20, PUSH21, PUSH22, PUSH23, PUSH24, PUSH25, PUSH26, PUSH27, PUSH28, PUSH29, PUSH30, PUSH31, PUSH32: pc.Add(pc, ethutil.Big1) a := int64(op) - int64(PUSH1) + 1 + if int(pc.Int64()+a) > len(script) { + return nil + } + data := script[pc.Int64() : pc.Int64()+a] if len(data) == 0 { data = []byte{0} -- cgit v1.2.3 From 490ca410c01a1b8076214d00c21d2edf09c24f86 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 22 Jul 2014 15:57:54 +0200 Subject: Minor improvements and fixes to the new vm structure --- ethchain/asm.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ethchain/asm.go') diff --git a/ethchain/asm.go b/ethchain/asm.go index 2697953fd..9f99b0c48 100644 --- a/ethchain/asm.go +++ b/ethchain/asm.go @@ -25,7 +25,7 @@ func Disassemble(script []byte) (asm []string) { pc.Add(pc, ethutil.Big1) a := int64(op) - int64(PUSH1) + 1 if int(pc.Int64()+a) > len(script) { - return nil + return } data := script[pc.Int64() : pc.Int64()+a] @@ -40,5 +40,5 @@ func Disassemble(script []byte) (asm []string) { pc.Add(pc, ethutil.Big1) } - return + return asm } -- cgit v1.2.3