github.com/theQRL/go-zond@v0.1.1/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 return PUSH1 <= op && op <= PUSH32 29 } 30 31 // 0x0 range - arithmetic ops. 32 const ( 33 STOP OpCode = 0x0 34 ADD OpCode = 0x1 35 MUL OpCode = 0x2 36 SUB OpCode = 0x3 37 DIV OpCode = 0x4 38 SDIV OpCode = 0x5 39 MOD OpCode = 0x6 40 SMOD OpCode = 0x7 41 ADDMOD OpCode = 0x8 42 MULMOD OpCode = 0x9 43 EXP OpCode = 0xa 44 SIGNEXTEND OpCode = 0xb 45 ) 46 47 // 0x10 range - comparison ops. 48 const ( 49 LT OpCode = 0x10 50 GT OpCode = 0x11 51 SLT OpCode = 0x12 52 SGT OpCode = 0x13 53 EQ OpCode = 0x14 54 ISZERO OpCode = 0x15 55 AND OpCode = 0x16 56 OR OpCode = 0x17 57 XOR OpCode = 0x18 58 NOT OpCode = 0x19 59 BYTE OpCode = 0x1a 60 SHL OpCode = 0x1b 61 SHR OpCode = 0x1c 62 SAR OpCode = 0x1d 63 ) 64 65 // 0x20 range - crypto. 66 const ( 67 KECCAK256 OpCode = 0x20 68 ) 69 70 // 0x30 range - closure state. 71 const ( 72 ADDRESS OpCode = 0x30 73 BALANCE OpCode = 0x31 74 ORIGIN OpCode = 0x32 75 CALLER OpCode = 0x33 76 CALLVALUE OpCode = 0x34 77 CALLDATALOAD OpCode = 0x35 78 CALLDATASIZE OpCode = 0x36 79 CALLDATACOPY OpCode = 0x37 80 CODESIZE OpCode = 0x38 81 CODECOPY OpCode = 0x39 82 GASPRICE OpCode = 0x3a 83 EXTCODESIZE OpCode = 0x3b 84 EXTCODECOPY OpCode = 0x3c 85 RETURNDATASIZE OpCode = 0x3d 86 RETURNDATACOPY OpCode = 0x3e 87 EXTCODEHASH OpCode = 0x3f 88 ) 89 90 // 0x40 range - block operations. 91 const ( 92 BLOCKHASH OpCode = 0x40 93 COINBASE OpCode = 0x41 94 TIMESTAMP OpCode = 0x42 95 NUMBER OpCode = 0x43 96 DIFFICULTY OpCode = 0x44 97 RANDOM OpCode = 0x44 // Same as DIFFICULTY 98 PREVRANDAO OpCode = 0x44 // Same as DIFFICULTY 99 GASLIMIT OpCode = 0x45 100 CHAINID OpCode = 0x46 101 SELFBALANCE OpCode = 0x47 102 BASEFEE OpCode = 0x48 103 BLOBHASH OpCode = 0x49 104 ) 105 106 // 0x50 range - 'storage' and execution. 107 const ( 108 POP OpCode = 0x50 109 MLOAD OpCode = 0x51 110 MSTORE OpCode = 0x52 111 MSTORE8 OpCode = 0x53 112 SLOAD OpCode = 0x54 113 SSTORE OpCode = 0x55 114 JUMP OpCode = 0x56 115 JUMPI OpCode = 0x57 116 PC OpCode = 0x58 117 MSIZE OpCode = 0x59 118 GAS OpCode = 0x5a 119 JUMPDEST OpCode = 0x5b 120 TLOAD OpCode = 0x5c 121 TSTORE OpCode = 0x5d 122 MCOPY OpCode = 0x5e 123 PUSH0 OpCode = 0x5f 124 ) 125 126 // 0x60 range - pushes. 127 const ( 128 PUSH1 OpCode = 0x60 + iota 129 PUSH2 130 PUSH3 131 PUSH4 132 PUSH5 133 PUSH6 134 PUSH7 135 PUSH8 136 PUSH9 137 PUSH10 138 PUSH11 139 PUSH12 140 PUSH13 141 PUSH14 142 PUSH15 143 PUSH16 144 PUSH17 145 PUSH18 146 PUSH19 147 PUSH20 148 PUSH21 149 PUSH22 150 PUSH23 151 PUSH24 152 PUSH25 153 PUSH26 154 PUSH27 155 PUSH28 156 PUSH29 157 PUSH30 158 PUSH31 159 PUSH32 160 ) 161 162 // 0x80 range - dups. 163 const ( 164 DUP1 = 0x80 + iota 165 DUP2 166 DUP3 167 DUP4 168 DUP5 169 DUP6 170 DUP7 171 DUP8 172 DUP9 173 DUP10 174 DUP11 175 DUP12 176 DUP13 177 DUP14 178 DUP15 179 DUP16 180 ) 181 182 // 0x90 range - swaps. 183 const ( 184 SWAP1 = 0x90 + iota 185 SWAP2 186 SWAP3 187 SWAP4 188 SWAP5 189 SWAP6 190 SWAP7 191 SWAP8 192 SWAP9 193 SWAP10 194 SWAP11 195 SWAP12 196 SWAP13 197 SWAP14 198 SWAP15 199 SWAP16 200 ) 201 202 // 0xa0 range - logging ops. 203 const ( 204 LOG0 OpCode = 0xa0 + iota 205 LOG1 206 LOG2 207 LOG3 208 LOG4 209 ) 210 211 // 0xf0 range - closures. 212 const ( 213 CREATE OpCode = 0xf0 214 CALL OpCode = 0xf1 215 CALLCODE OpCode = 0xf2 216 RETURN OpCode = 0xf3 217 DELEGATECALL OpCode = 0xf4 218 CREATE2 OpCode = 0xf5 219 220 STATICCALL OpCode = 0xfa 221 REVERT OpCode = 0xfd 222 INVALID OpCode = 0xfe 223 SELFDESTRUCT OpCode = 0xff 224 ) 225 226 // Since the opcodes aren't all in order we can't use a regular slice. 227 var opCodeToString = map[OpCode]string{ 228 // 0x0 range - arithmetic ops. 229 STOP: "STOP", 230 ADD: "ADD", 231 MUL: "MUL", 232 SUB: "SUB", 233 DIV: "DIV", 234 SDIV: "SDIV", 235 MOD: "MOD", 236 SMOD: "SMOD", 237 EXP: "EXP", 238 NOT: "NOT", 239 LT: "LT", 240 GT: "GT", 241 SLT: "SLT", 242 SGT: "SGT", 243 EQ: "EQ", 244 ISZERO: "ISZERO", 245 SIGNEXTEND: "SIGNEXTEND", 246 247 // 0x10 range - bit ops. 248 AND: "AND", 249 OR: "OR", 250 XOR: "XOR", 251 BYTE: "BYTE", 252 SHL: "SHL", 253 SHR: "SHR", 254 SAR: "SAR", 255 ADDMOD: "ADDMOD", 256 MULMOD: "MULMOD", 257 258 // 0x20 range - crypto. 259 KECCAK256: "KECCAK256", 260 261 // 0x30 range - closure state. 262 ADDRESS: "ADDRESS", 263 BALANCE: "BALANCE", 264 ORIGIN: "ORIGIN", 265 CALLER: "CALLER", 266 CALLVALUE: "CALLVALUE", 267 CALLDATALOAD: "CALLDATALOAD", 268 CALLDATASIZE: "CALLDATASIZE", 269 CALLDATACOPY: "CALLDATACOPY", 270 CODESIZE: "CODESIZE", 271 CODECOPY: "CODECOPY", 272 GASPRICE: "GASPRICE", 273 EXTCODESIZE: "EXTCODESIZE", 274 EXTCODECOPY: "EXTCODECOPY", 275 RETURNDATASIZE: "RETURNDATASIZE", 276 RETURNDATACOPY: "RETURNDATACOPY", 277 EXTCODEHASH: "EXTCODEHASH", 278 279 // 0x40 range - block operations. 280 BLOCKHASH: "BLOCKHASH", 281 COINBASE: "COINBASE", 282 TIMESTAMP: "TIMESTAMP", 283 NUMBER: "NUMBER", 284 DIFFICULTY: "DIFFICULTY", // TODO (MariusVanDerWijden) rename to PREVRANDAO post merge 285 GASLIMIT: "GASLIMIT", 286 CHAINID: "CHAINID", 287 SELFBALANCE: "SELFBALANCE", 288 BASEFEE: "BASEFEE", 289 BLOBHASH: "BLOBHASH", 290 291 // 0x50 range - 'storage' and execution. 292 POP: "POP", 293 MLOAD: "MLOAD", 294 MSTORE: "MSTORE", 295 MSTORE8: "MSTORE8", 296 SLOAD: "SLOAD", 297 SSTORE: "SSTORE", 298 JUMP: "JUMP", 299 JUMPI: "JUMPI", 300 PC: "PC", 301 MSIZE: "MSIZE", 302 GAS: "GAS", 303 JUMPDEST: "JUMPDEST", 304 TLOAD: "TLOAD", 305 TSTORE: "TSTORE", 306 MCOPY: "MCOPY", 307 PUSH0: "PUSH0", 308 309 // 0x60 range - pushes. 310 PUSH1: "PUSH1", 311 PUSH2: "PUSH2", 312 PUSH3: "PUSH3", 313 PUSH4: "PUSH4", 314 PUSH5: "PUSH5", 315 PUSH6: "PUSH6", 316 PUSH7: "PUSH7", 317 PUSH8: "PUSH8", 318 PUSH9: "PUSH9", 319 PUSH10: "PUSH10", 320 PUSH11: "PUSH11", 321 PUSH12: "PUSH12", 322 PUSH13: "PUSH13", 323 PUSH14: "PUSH14", 324 PUSH15: "PUSH15", 325 PUSH16: "PUSH16", 326 PUSH17: "PUSH17", 327 PUSH18: "PUSH18", 328 PUSH19: "PUSH19", 329 PUSH20: "PUSH20", 330 PUSH21: "PUSH21", 331 PUSH22: "PUSH22", 332 PUSH23: "PUSH23", 333 PUSH24: "PUSH24", 334 PUSH25: "PUSH25", 335 PUSH26: "PUSH26", 336 PUSH27: "PUSH27", 337 PUSH28: "PUSH28", 338 PUSH29: "PUSH29", 339 PUSH30: "PUSH30", 340 PUSH31: "PUSH31", 341 PUSH32: "PUSH32", 342 343 // 0x80 - dups. 344 DUP1: "DUP1", 345 DUP2: "DUP2", 346 DUP3: "DUP3", 347 DUP4: "DUP4", 348 DUP5: "DUP5", 349 DUP6: "DUP6", 350 DUP7: "DUP7", 351 DUP8: "DUP8", 352 DUP9: "DUP9", 353 DUP10: "DUP10", 354 DUP11: "DUP11", 355 DUP12: "DUP12", 356 DUP13: "DUP13", 357 DUP14: "DUP14", 358 DUP15: "DUP15", 359 DUP16: "DUP16", 360 361 // 0x90 - swaps. 362 SWAP1: "SWAP1", 363 SWAP2: "SWAP2", 364 SWAP3: "SWAP3", 365 SWAP4: "SWAP4", 366 SWAP5: "SWAP5", 367 SWAP6: "SWAP6", 368 SWAP7: "SWAP7", 369 SWAP8: "SWAP8", 370 SWAP9: "SWAP9", 371 SWAP10: "SWAP10", 372 SWAP11: "SWAP11", 373 SWAP12: "SWAP12", 374 SWAP13: "SWAP13", 375 SWAP14: "SWAP14", 376 SWAP15: "SWAP15", 377 SWAP16: "SWAP16", 378 379 // 0xa0 range - logging ops. 380 LOG0: "LOG0", 381 LOG1: "LOG1", 382 LOG2: "LOG2", 383 LOG3: "LOG3", 384 LOG4: "LOG4", 385 386 // 0xf0 range - closures. 387 CREATE: "CREATE", 388 CALL: "CALL", 389 RETURN: "RETURN", 390 CALLCODE: "CALLCODE", 391 DELEGATECALL: "DELEGATECALL", 392 CREATE2: "CREATE2", 393 STATICCALL: "STATICCALL", 394 REVERT: "REVERT", 395 INVALID: "INVALID", 396 SELFDESTRUCT: "SELFDESTRUCT", 397 } 398 399 func (op OpCode) String() string { 400 str := opCodeToString[op] 401 if len(str) == 0 { 402 return fmt.Sprintf("opcode %#x not defined", int(op)) 403 } 404 405 return str 406 } 407 408 var stringToOp = map[string]OpCode{ 409 "STOP": STOP, 410 "ADD": ADD, 411 "MUL": MUL, 412 "SUB": SUB, 413 "DIV": DIV, 414 "SDIV": SDIV, 415 "MOD": MOD, 416 "SMOD": SMOD, 417 "EXP": EXP, 418 "NOT": NOT, 419 "LT": LT, 420 "GT": GT, 421 "SLT": SLT, 422 "SGT": SGT, 423 "EQ": EQ, 424 "ISZERO": ISZERO, 425 "SIGNEXTEND": SIGNEXTEND, 426 "AND": AND, 427 "OR": OR, 428 "XOR": XOR, 429 "BYTE": BYTE, 430 "SHL": SHL, 431 "SHR": SHR, 432 "SAR": SAR, 433 "ADDMOD": ADDMOD, 434 "MULMOD": MULMOD, 435 "KECCAK256": KECCAK256, 436 "ADDRESS": ADDRESS, 437 "BALANCE": BALANCE, 438 "ORIGIN": ORIGIN, 439 "CALLER": CALLER, 440 "CALLVALUE": CALLVALUE, 441 "CALLDATALOAD": CALLDATALOAD, 442 "CALLDATASIZE": CALLDATASIZE, 443 "CALLDATACOPY": CALLDATACOPY, 444 "CHAINID": CHAINID, 445 "BASEFEE": BASEFEE, 446 "BLOBHASH": BLOBHASH, 447 "DELEGATECALL": DELEGATECALL, 448 "STATICCALL": STATICCALL, 449 "CODESIZE": CODESIZE, 450 "CODECOPY": CODECOPY, 451 "GASPRICE": GASPRICE, 452 "EXTCODESIZE": EXTCODESIZE, 453 "EXTCODECOPY": EXTCODECOPY, 454 "RETURNDATASIZE": RETURNDATASIZE, 455 "RETURNDATACOPY": RETURNDATACOPY, 456 "EXTCODEHASH": EXTCODEHASH, 457 "BLOCKHASH": BLOCKHASH, 458 "COINBASE": COINBASE, 459 "TIMESTAMP": TIMESTAMP, 460 "NUMBER": NUMBER, 461 "DIFFICULTY": DIFFICULTY, 462 "GASLIMIT": GASLIMIT, 463 "SELFBALANCE": SELFBALANCE, 464 "POP": POP, 465 "MLOAD": MLOAD, 466 "MSTORE": MSTORE, 467 "MSTORE8": MSTORE8, 468 "SLOAD": SLOAD, 469 "SSTORE": SSTORE, 470 "JUMP": JUMP, 471 "JUMPI": JUMPI, 472 "PC": PC, 473 "MSIZE": MSIZE, 474 "GAS": GAS, 475 "JUMPDEST": JUMPDEST, 476 "TLOAD": TLOAD, 477 "TSTORE": TSTORE, 478 "MCOPY": MCOPY, 479 "PUSH0": PUSH0, 480 "PUSH1": PUSH1, 481 "PUSH2": PUSH2, 482 "PUSH3": PUSH3, 483 "PUSH4": PUSH4, 484 "PUSH5": PUSH5, 485 "PUSH6": PUSH6, 486 "PUSH7": PUSH7, 487 "PUSH8": PUSH8, 488 "PUSH9": PUSH9, 489 "PUSH10": PUSH10, 490 "PUSH11": PUSH11, 491 "PUSH12": PUSH12, 492 "PUSH13": PUSH13, 493 "PUSH14": PUSH14, 494 "PUSH15": PUSH15, 495 "PUSH16": PUSH16, 496 "PUSH17": PUSH17, 497 "PUSH18": PUSH18, 498 "PUSH19": PUSH19, 499 "PUSH20": PUSH20, 500 "PUSH21": PUSH21, 501 "PUSH22": PUSH22, 502 "PUSH23": PUSH23, 503 "PUSH24": PUSH24, 504 "PUSH25": PUSH25, 505 "PUSH26": PUSH26, 506 "PUSH27": PUSH27, 507 "PUSH28": PUSH28, 508 "PUSH29": PUSH29, 509 "PUSH30": PUSH30, 510 "PUSH31": PUSH31, 511 "PUSH32": PUSH32, 512 "DUP1": DUP1, 513 "DUP2": DUP2, 514 "DUP3": DUP3, 515 "DUP4": DUP4, 516 "DUP5": DUP5, 517 "DUP6": DUP6, 518 "DUP7": DUP7, 519 "DUP8": DUP8, 520 "DUP9": DUP9, 521 "DUP10": DUP10, 522 "DUP11": DUP11, 523 "DUP12": DUP12, 524 "DUP13": DUP13, 525 "DUP14": DUP14, 526 "DUP15": DUP15, 527 "DUP16": DUP16, 528 "SWAP1": SWAP1, 529 "SWAP2": SWAP2, 530 "SWAP3": SWAP3, 531 "SWAP4": SWAP4, 532 "SWAP5": SWAP5, 533 "SWAP6": SWAP6, 534 "SWAP7": SWAP7, 535 "SWAP8": SWAP8, 536 "SWAP9": SWAP9, 537 "SWAP10": SWAP10, 538 "SWAP11": SWAP11, 539 "SWAP12": SWAP12, 540 "SWAP13": SWAP13, 541 "SWAP14": SWAP14, 542 "SWAP15": SWAP15, 543 "SWAP16": SWAP16, 544 "LOG0": LOG0, 545 "LOG1": LOG1, 546 "LOG2": LOG2, 547 "LOG3": LOG3, 548 "LOG4": LOG4, 549 "CREATE": CREATE, 550 "CREATE2": CREATE2, 551 "CALL": CALL, 552 "RETURN": RETURN, 553 "CALLCODE": CALLCODE, 554 "REVERT": REVERT, 555 "INVALID": INVALID, 556 "SELFDESTRUCT": SELFDESTRUCT, 557 } 558 559 // StringToOp finds the opcode whose name is stored in `str`. 560 func StringToOp(str string) OpCode { 561 return stringToOp[str] 562 }