diff options
author | stefan <stefan@s00.xyz> | 2023-05-24 08:32:08 -0400 |
---|---|---|
committer | stefan <stefan@s00.xyz> | 2023-05-24 08:32:08 -0400 |
commit | ba2b9c8a1bb1876b6eb4c9783fde798b19de4418 (patch) | |
tree | 9b49307112b0f489189cf65c1b50dd281f8e430f /as | |
parent | c30e7f5f99f9ad4d9552b645d7b89d7385972f99 (diff) | |
download | evm-ba2b9c8a1bb1876b6eb4c9783fde798b19de4418.tar.gz |
init
Diffstat (limited to 'as')
-rwxr-xr-x | as/as | bin | 0 -> 2088979 bytes | |||
-rw-r--r-- | as/as.go | 32 | ||||
-rw-r--r-- | as/go.mod | 5 | ||||
-rw-r--r-- | as/go.sum | 2 | ||||
-rw-r--r-- | as/main.go | 59 | ||||
-rw-r--r-- | as/opcodes.go | 101 |
6 files changed, 199 insertions, 0 deletions
Binary files differ diff --git a/as/as.go b/as/as.go new file mode 100644 index 0000000..bcc7cf8 --- /dev/null +++ b/as/as.go @@ -0,0 +1,32 @@ +package main + +import ( + "fmt" + "strings" + "github.com/holiman/uint256" + +) + +func Assemble(code string) []byte { + bytecode := make([]byte, 0) + + t := strings.Fields(code) + + for i := range t { + x, found := OpcodeStrings[t[i]] + + if found { + bytecode = append(bytecode, x) + } else { + var n *uint256.Int = nil + if len(t[i]) > 1 && t[i][1] == 'x' { + n, _ = uint256.FromHex(t[i]) + } else { + n, _ = uint256.FromDecimal(t[i]) + } + b := n.Bytes32() + bytecode = append(bytecode, b[:]...) + } + } + return bytecode +} diff --git a/as/go.mod b/as/go.mod new file mode 100644 index 0000000..6f40dd8 --- /dev/null +++ b/as/go.mod @@ -0,0 +1,5 @@ +module s00.xyz/as + +go 1.20 + +require github.com/holiman/uint256 v1.2.2 // indirect diff --git a/as/go.sum b/as/go.sum new file mode 100644 index 0000000..e0f03a0 --- /dev/null +++ b/as/go.sum @@ -0,0 +1,2 @@ +github.com/holiman/uint256 v1.2.2 h1:TXKcSGc2WaxPD2+bmzAsVthL4+pEN0YwXcL5qED83vk= +github.com/holiman/uint256 v1.2.2/go.mod h1:SC8Ryt4n+UBbPbIBKaG9zbbDlp4jOru9xFZmPzLUTxw= diff --git a/as/main.go b/as/main.go new file mode 100644 index 0000000..95aff46 --- /dev/null +++ b/as/main.go @@ -0,0 +1,59 @@ +package main + +import ( + "os" + "fmt" +) + +func usage(arg0 string) { + fmt.Fprintf(os.Stderr, "usage: %s [-o file] file.easm\n", os.Args[0]) + os.Exit(1) +} + +func main() { + if (len(os.Args) <= 2) { + usage(os.Args[0]) + } + + + var aout int = 0 + var ain int = 0 + + for i := 0; i < len(os.Args); i++ { + fmt.Println(os.Args[i]) + switch os.Args[i] { + case "-o": + i = i + 1 + aout = i + default: + ain = i + } + } + + if (ain == 0) { + usage(os.Args[0]) + } + + var outf string = "out.bin" + if (aout != 0) { + outf = os.Args[aout] + } + + b, errin := os.ReadFile(os.Args[ain]) + out, errout := os.Create(outf) + + if errin != nil { + fmt.Fprintf(os.Stderr, "failed to read file: %s\n", os.Args[ain]) + os.Exit(1) + } + + if errout != nil { + fmt.Fprintf(os.Stderr, "failed to create file for output: %s\n", outf) + os.Exit(1) + } + + s := string(b) + c := Assemble(s) + + out.Write(c) +} diff --git a/as/opcodes.go b/as/opcodes.go new file mode 100644 index 0000000..94c2e2b --- /dev/null +++ b/as/opcodes.go @@ -0,0 +1,101 @@ +package main + +type Opcode byte + +const ( + STOP = iota + ADD + MUL + SUB + DIV + SDIV + MOD + SMOD + ADDMOD + MULMOD + POP = 0x50 + MLOAD = 0x51 + MSTORE = 0x52 + MSTORE8 = 0x53 + PUSH1 = 0x60 + PUSH2 = 0x61 + PUSH3 = 0x62 + PUSH4 = 0x63 + PUSH5 = 0x64 + PUSH6 = 0x65 + PUSH7 = 0x66 + PUSH8 = 0x67 + PUSH9 = 0x68 + PUSH10 = 0x69 + PUSH11 = 0x6a + PUSH12 = 0x6b + PUSH13 = 0x6c + PUSH14 = 0x6d + PUSH15 = 0x6e + PUSH16 = 0x6f + PUSH17 = 0x70 + PUSH18 = 0x71 + PUSH19 = 0x72 + PUSH20 = 0x73 + PUSH21 = 0x74 + PUSH22 = 0x75 + PUSH23 = 0x76 + PUSH24 = 0x77 + PUSH25 = 0x78 + PUSH26 = 0x79 + PUSH27 = 0x7a + PUSH28 = 0x7b + PUSH29 = 0x7c + PUSH30 = 0x7d + PUSH31 = 0x7e + PUSH32 = 0x7f +) + +var OpcodeStrings = map[string]byte { + "STOP": STOP, + "ADD": ADD, + "MUL": MUL, + "SUB": SUB, + "DIV": DIV, + "SDIV": SDIV, + "MOD": MOD, + "SMOD": SMOD, + "ADDMOD": ADDMOD, + "MULMOD": MULMOD, + "POP": POP, + "MLOAD": MLOAD, + "MSTORE": MSTORE, + "MSTORE8": MSTORE8, + "PUSH1": PUSH1, + "PUSH2": PUSH2, + "PUSH3": PUSH3, + "PUSH4": PUSH4, + "PUSH5": PUSH5, + "PUSH6": PUSH6, + "PUSH7": PUSH7, + "PUSH8": PUSH8, + "PUSH9": PUSH9, + "PUSH10": PUSH10, + "PUSH11": PUSH11, + "PUSH12": PUSH12, + "PUSH13": PUSH13, + "PUSH14": PUSH14, + "PUSH15": PUSH15, + "PUSH16": PUSH16, + "PUSH17": PUSH17, + "PUSH18": PUSH18, + "PUSH19": PUSH19, + "PUSH20": PUSH20, + "PUSH21": PUSH21, + "PUSH22": PUSH22, + "PUSH23": PUSH23, + "PUSH24": PUSH24, + "PUSH25": PUSH25, + "PUSH26": PUSH26, + "PUSH27": PUSH27, + "PUSH28": PUSH28, + "PUSH29": PUSH29, + "PUSH30": PUSH30, + "PUSH31": PUSH31, + "PUSH32": PUSH32, +} |