github.com/dominant-strategies/go-quai@v0.28.2/core/vm/opcodes.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package vm 18 19 import ( 20 "fmt" 21 ) 22 23 // OpCode is an EVM opcode 24 type OpCode byte 25 26 // IsPush specifies if an opcode is a PUSH opcode. 27 func (op OpCode) IsPush() bool { 28 switch op { 29 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: 30 return true 31 } 32 return false 33 } 34 35 // IsStaticJump specifies if an opcode is JUMP. 36 func (op OpCode) IsStaticJump() bool { 37 return op == JUMP 38 } 39 40 // 0x0 range - arithmetic ops. 41 const ( 42 STOP OpCode = iota 43 ADD 44 MUL 45 SUB 46 DIV 47 SDIV 48 MOD 49 SMOD 50 ADDMOD 51 MULMOD 52 EXP 53 SIGNEXTEND 54 ) 55 56 // 0x10 range - comparison ops. 57 const ( 58 LT OpCode = iota + 0x10 59 GT 60 SLT 61 SGT 62 EQ 63 ISZERO 64 AND 65 OR 66 XOR 67 NOT 68 BYTE 69 SHL 70 SHR 71 SAR 72 73 SHA3 OpCode = 0x20 74 ) 75 76 // 0x30 range - closure state. 77 const ( 78 ADDRESS OpCode = 0x30 + iota 79 BALANCE 80 ORIGIN 81 CALLER 82 CALLVALUE 83 CALLDATALOAD 84 CALLDATASIZE 85 CALLDATACOPY 86 CODESIZE 87 CODECOPY 88 GASPRICE 89 EXTCODESIZE 90 EXTCODECOPY 91 RETURNDATASIZE 92 RETURNDATACOPY 93 EXTCODEHASH 94 ) 95 96 // 0x40 range - block operations. 97 const ( 98 BLOCKHASH OpCode = 0x40 + iota 99 COINBASE 100 TIMESTAMP 101 NUMBER 102 DIFFICULTY 103 GASLIMIT 104 CHAINID OpCode = 0x46 105 SELFBALANCE OpCode = 0x47 106 BASEFEE OpCode = 0x48 107 ) 108 109 // 0x50 range - 'storage' and execution. 110 const ( 111 POP OpCode = 0x50 112 MLOAD OpCode = 0x51 113 MSTORE OpCode = 0x52 114 MSTORE8 OpCode = 0x53 115 SLOAD OpCode = 0x54 116 SSTORE OpCode = 0x55 117 JUMP OpCode = 0x56 118 JUMPI OpCode = 0x57 119 PC OpCode = 0x58 120 MSIZE OpCode = 0x59 121 GAS OpCode = 0x5a 122 JUMPDEST OpCode = 0x5b 123 ) 124 125 // 0x60 range. 126 const ( 127 PUSH1 OpCode = 0x60 + iota 128 PUSH2 129 PUSH3 130 PUSH4 131 PUSH5 132 PUSH6 133 PUSH7 134 PUSH8 135 PUSH9 136 PUSH10 137 PUSH11 138 PUSH12 139 PUSH13 140 PUSH14 141 PUSH15 142 PUSH16 143 PUSH17 144 PUSH18 145 PUSH19 146 PUSH20 147 PUSH21 148 PUSH22 149 PUSH23 150 PUSH24 151 PUSH25 152 PUSH26 153 PUSH27 154 PUSH28 155 PUSH29 156 PUSH30 157 PUSH31 158 PUSH32 159 DUP1 160 DUP2 161 DUP3 162 DUP4 163 DUP5 164 DUP6 165 DUP7 166 DUP8 167 DUP9 168 DUP10 169 DUP11 170 DUP12 171 DUP13 172 DUP14 173 DUP15 174 DUP16 175 SWAP1 176 SWAP2 177 SWAP3 178 SWAP4 179 SWAP5 180 SWAP6 181 SWAP7 182 SWAP8 183 SWAP9 184 SWAP10 185 SWAP11 186 SWAP12 187 SWAP13 188 SWAP14 189 SWAP15 190 SWAP16 191 ) 192 193 // 0xa0 range - logging ops. 194 const ( 195 LOG0 OpCode = 0xa0 + iota 196 LOG1 197 LOG2 198 LOG3 199 LOG4 200 ) 201 202 // unofficial opcodes used for parsing. 203 const ( 204 PUSH OpCode = 0xb0 + iota 205 DUP 206 SWAP 207 ) 208 209 // 0xf0 range - closures. 210 const ( 211 CREATE OpCode = 0xf0 + iota 212 CALL 213 CALLCODE 214 RETURN 215 DELEGATECALL 216 CREATE2 217 ETX 218 ISADDRINTERNAL 219 STATICCALL OpCode = 0xfa 220 REVERT OpCode = 0xfd 221 SELFDESTRUCT OpCode = 0xff 222 ) 223 224 // Since the opcodes aren't all in order we can't use a regular slice. 225 var opCodeToString = map[OpCode]string{ 226 // 0x0 range - arithmetic ops. 227 STOP: "STOP", 228 ADD: "ADD", 229 MUL: "MUL", 230 SUB: "SUB", 231 DIV: "DIV", 232 SDIV: "SDIV", 233 MOD: "MOD", 234 SMOD: "SMOD", 235 EXP: "EXP", 236 NOT: "NOT", 237 LT: "LT", 238 GT: "GT", 239 SLT: "SLT", 240 SGT: "SGT", 241 EQ: "EQ", 242 ISZERO: "ISZERO", 243 SIGNEXTEND: "SIGNEXTEND", 244 245 // 0x10 range - bit ops. 246 AND: "AND", 247 OR: "OR", 248 XOR: "XOR", 249 BYTE: "BYTE", 250 SHL: "SHL", 251 SHR: "SHR", 252 SAR: "SAR", 253 ADDMOD: "ADDMOD", 254 MULMOD: "MULMOD", 255 256 // 0x20 range - crypto. 257 SHA3: "SHA3", 258 259 // 0x30 range - closure state. 260 ADDRESS: "ADDRESS", 261 BALANCE: "BALANCE", 262 ORIGIN: "ORIGIN", 263 CALLER: "CALLER", 264 CALLVALUE: "CALLVALUE", 265 CALLDATALOAD: "CALLDATALOAD", 266 CALLDATASIZE: "CALLDATASIZE", 267 CALLDATACOPY: "CALLDATACOPY", 268 CODESIZE: "CODESIZE", 269 CODECOPY: "CODECOPY", 270 GASPRICE: "GASPRICE", 271 EXTCODESIZE: "EXTCODESIZE", 272 EXTCODECOPY: "EXTCODECOPY", 273 RETURNDATASIZE: "RETURNDATASIZE", 274 RETURNDATACOPY: "RETURNDATACOPY", 275 EXTCODEHASH: "EXTCODEHASH", 276 277 // 0x40 range - block operations. 278 BLOCKHASH: "BLOCKHASH", 279 COINBASE: "COINBASE", 280 TIMESTAMP: "TIMESTAMP", 281 NUMBER: "NUMBER", 282 DIFFICULTY: "DIFFICULTY", 283 GASLIMIT: "GASLIMIT", 284 CHAINID: "CHAINID", 285 SELFBALANCE: "SELFBALANCE", 286 BASEFEE: "BASEFEE", 287 288 // 0x50 range - 'storage' and execution. 289 POP: "POP", 290 //DUP: "DUP", 291 //SWAP: "SWAP", 292 MLOAD: "MLOAD", 293 MSTORE: "MSTORE", 294 MSTORE8: "MSTORE8", 295 SLOAD: "SLOAD", 296 SSTORE: "SSTORE", 297 JUMP: "JUMP", 298 JUMPI: "JUMPI", 299 PC: "PC", 300 MSIZE: "MSIZE", 301 GAS: "GAS", 302 JUMPDEST: "JUMPDEST", 303 304 // 0x60 range - push. 305 PUSH1: "PUSH1", 306 PUSH2: "PUSH2", 307 PUSH3: "PUSH3", 308 PUSH4: "PUSH4", 309 PUSH5: "PUSH5", 310 PUSH6: "PUSH6", 311 PUSH7: "PUSH7", 312 PUSH8: "PUSH8", 313 PUSH9: "PUSH9", 314 PUSH10: "PUSH10", 315 PUSH11: "PUSH11", 316 PUSH12: "PUSH12", 317 PUSH13: "PUSH13", 318 PUSH14: "PUSH14", 319 PUSH15: "PUSH15", 320 PUSH16: "PUSH16", 321 PUSH17: "PUSH17", 322 PUSH18: "PUSH18", 323 PUSH19: "PUSH19", 324 PUSH20: "PUSH20", 325 PUSH21: "PUSH21", 326 PUSH22: "PUSH22", 327 PUSH23: "PUSH23", 328 PUSH24: "PUSH24", 329 PUSH25: "PUSH25", 330 PUSH26: "PUSH26", 331 PUSH27: "PUSH27", 332 PUSH28: "PUSH28", 333 PUSH29: "PUSH29", 334 PUSH30: "PUSH30", 335 PUSH31: "PUSH31", 336 PUSH32: "PUSH32", 337 338 DUP1: "DUP1", 339 DUP2: "DUP2", 340 DUP3: "DUP3", 341 DUP4: "DUP4", 342 DUP5: "DUP5", 343 DUP6: "DUP6", 344 DUP7: "DUP7", 345 DUP8: "DUP8", 346 DUP9: "DUP9", 347 DUP10: "DUP10", 348 DUP11: "DUP11", 349 DUP12: "DUP12", 350 DUP13: "DUP13", 351 DUP14: "DUP14", 352 DUP15: "DUP15", 353 DUP16: "DUP16", 354 355 SWAP1: "SWAP1", 356 SWAP2: "SWAP2", 357 SWAP3: "SWAP3", 358 SWAP4: "SWAP4", 359 SWAP5: "SWAP5", 360 SWAP6: "SWAP6", 361 SWAP7: "SWAP7", 362 SWAP8: "SWAP8", 363 SWAP9: "SWAP9", 364 SWAP10: "SWAP10", 365 SWAP11: "SWAP11", 366 SWAP12: "SWAP12", 367 SWAP13: "SWAP13", 368 SWAP14: "SWAP14", 369 SWAP15: "SWAP15", 370 SWAP16: "SWAP16", 371 LOG0: "LOG0", 372 LOG1: "LOG1", 373 LOG2: "LOG2", 374 LOG3: "LOG3", 375 LOG4: "LOG4", 376 377 // 0xf0 range. 378 CREATE: "CREATE", 379 CALL: "CALL", 380 RETURN: "RETURN", 381 CALLCODE: "CALLCODE", 382 DELEGATECALL: "DELEGATECALL", 383 CREATE2: "CREATE2", 384 ETX: "ETX", 385 ISADDRINTERNAL: "ISADDRINTERNAL", 386 STATICCALL: "STATICCALL", 387 REVERT: "REVERT", 388 SELFDESTRUCT: "SELFDESTRUCT", 389 390 PUSH: "PUSH", 391 DUP: "DUP", 392 SWAP: "SWAP", 393 } 394 395 func (op OpCode) String() string { 396 str := opCodeToString[op] 397 if len(str) == 0 { 398 return fmt.Sprintf("opcode 0x%x not defined", int(op)) 399 } 400 401 return str 402 } 403 404 var stringToOp = map[string]OpCode{ 405 "STOP": STOP, 406 "ADD": ADD, 407 "MUL": MUL, 408 "SUB": SUB, 409 "DIV": DIV, 410 "SDIV": SDIV, 411 "MOD": MOD, 412 "SMOD": SMOD, 413 "EXP": EXP, 414 "NOT": NOT, 415 "LT": LT, 416 "GT": GT, 417 "SLT": SLT, 418 "SGT": SGT, 419 "EQ": EQ, 420 "ISZERO": ISZERO, 421 "SIGNEXTEND": SIGNEXTEND, 422 "AND": AND, 423 "OR": OR, 424 "XOR": XOR, 425 "BYTE": BYTE, 426 "SHL": SHL, 427 "SHR": SHR, 428 "SAR": SAR, 429 "ADDMOD": ADDMOD, 430 "MULMOD": MULMOD, 431 "SHA3": SHA3, 432 "ADDRESS": ADDRESS, 433 "BALANCE": BALANCE, 434 "ORIGIN": ORIGIN, 435 "CALLER": CALLER, 436 "CALLVALUE": CALLVALUE, 437 "CALLDATALOAD": CALLDATALOAD, 438 "CALLDATASIZE": CALLDATASIZE, 439 "CALLDATACOPY": CALLDATACOPY, 440 "CHAINID": CHAINID, 441 "BASEFEE": BASEFEE, 442 "DELEGATECALL": DELEGATECALL, 443 "STATICCALL": STATICCALL, 444 "CODESIZE": CODESIZE, 445 "CODECOPY": CODECOPY, 446 "GASPRICE": GASPRICE, 447 "EXTCODESIZE": EXTCODESIZE, 448 "EXTCODECOPY": EXTCODECOPY, 449 "RETURNDATASIZE": RETURNDATASIZE, 450 "RETURNDATACOPY": RETURNDATACOPY, 451 "EXTCODEHASH": EXTCODEHASH, 452 "BLOCKHASH": BLOCKHASH, 453 "COINBASE": COINBASE, 454 "TIMESTAMP": TIMESTAMP, 455 "NUMBER": NUMBER, 456 "DIFFICULTY": DIFFICULTY, 457 "GASLIMIT": GASLIMIT, 458 "SELFBALANCE": SELFBALANCE, 459 "POP": POP, 460 "MLOAD": MLOAD, 461 "MSTORE": MSTORE, 462 "MSTORE8": MSTORE8, 463 "SLOAD": SLOAD, 464 "SSTORE": SSTORE, 465 "JUMP": JUMP, 466 "JUMPI": JUMPI, 467 "PC": PC, 468 "MSIZE": MSIZE, 469 "GAS": GAS, 470 "JUMPDEST": JUMPDEST, 471 "PUSH1": PUSH1, 472 "PUSH2": PUSH2, 473 "PUSH3": PUSH3, 474 "PUSH4": PUSH4, 475 "PUSH5": PUSH5, 476 "PUSH6": PUSH6, 477 "PUSH7": PUSH7, 478 "PUSH8": PUSH8, 479 "PUSH9": PUSH9, 480 "PUSH10": PUSH10, 481 "PUSH11": PUSH11, 482 "PUSH12": PUSH12, 483 "PUSH13": PUSH13, 484 "PUSH14": PUSH14, 485 "PUSH15": PUSH15, 486 "PUSH16": PUSH16, 487 "PUSH17": PUSH17, 488 "PUSH18": PUSH18, 489 "PUSH19": PUSH19, 490 "PUSH20": PUSH20, 491 "PUSH21": PUSH21, 492 "PUSH22": PUSH22, 493 "PUSH23": PUSH23, 494 "PUSH24": PUSH24, 495 "PUSH25": PUSH25, 496 "PUSH26": PUSH26, 497 "PUSH27": PUSH27, 498 "PUSH28": PUSH28, 499 "PUSH29": PUSH29, 500 "PUSH30": PUSH30, 501 "PUSH31": PUSH31, 502 "PUSH32": PUSH32, 503 "DUP1": DUP1, 504 "DUP2": DUP2, 505 "DUP3": DUP3, 506 "DUP4": DUP4, 507 "DUP5": DUP5, 508 "DUP6": DUP6, 509 "DUP7": DUP7, 510 "DUP8": DUP8, 511 "DUP9": DUP9, 512 "DUP10": DUP10, 513 "DUP11": DUP11, 514 "DUP12": DUP12, 515 "DUP13": DUP13, 516 "DUP14": DUP14, 517 "DUP15": DUP15, 518 "DUP16": DUP16, 519 "SWAP1": SWAP1, 520 "SWAP2": SWAP2, 521 "SWAP3": SWAP3, 522 "SWAP4": SWAP4, 523 "SWAP5": SWAP5, 524 "SWAP6": SWAP6, 525 "SWAP7": SWAP7, 526 "SWAP8": SWAP8, 527 "SWAP9": SWAP9, 528 "SWAP10": SWAP10, 529 "SWAP11": SWAP11, 530 "SWAP12": SWAP12, 531 "SWAP13": SWAP13, 532 "SWAP14": SWAP14, 533 "SWAP15": SWAP15, 534 "SWAP16": SWAP16, 535 "LOG0": LOG0, 536 "LOG1": LOG1, 537 "LOG2": LOG2, 538 "LOG3": LOG3, 539 "LOG4": LOG4, 540 "CREATE": CREATE, 541 "CREATE2": CREATE2, 542 "CALL": CALL, 543 "RETURN": RETURN, 544 "CALLCODE": CALLCODE, 545 "REVERT": REVERT, 546 "SELFDESTRUCT": SELFDESTRUCT, 547 "ETX": ETX, 548 "ISADDRINTERNAL": ISADDRINTERNAL, 549 } 550 551 // StringToOp finds the opcode whose name is stored in `str`. 552 func StringToOp(str string) OpCode { 553 return stringToOp[str] 554 }