github.com/core-coin/go-core/v2@v2.1.9/core/vm/opcodes.go (about) 1 // Copyright 2014 by the Authors 2 // This file is part of the go-core library. 3 // 4 // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>. 16 17 package vm 18 19 import ( 20 "fmt" 21 ) 22 23 // OpCode is an CVM 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 ENERGYPRICE 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 ENERGYLIMIT 104 NETWORKID OpCode = 0x46 105 SELFBALANCE OpCode = 0x47 106 ) 107 108 // 0x50 range - 'storage' and execution. 109 const ( 110 POP OpCode = 0x50 111 MLOAD OpCode = 0x51 112 MSTORE OpCode = 0x52 113 MSTORE8 OpCode = 0x53 114 SLOAD OpCode = 0x54 115 SSTORE OpCode = 0x55 116 JUMP OpCode = 0x56 117 JUMPI OpCode = 0x57 118 PC OpCode = 0x58 119 MSIZE OpCode = 0x59 120 ENERGY OpCode = 0x5a 121 JUMPDEST OpCode = 0x5b 122 BEGINSUB OpCode = 0x5c 123 RETURNSUB OpCode = 0x5d 124 JUMPSUB OpCode = 0x5e 125 ) 126 127 // 0x60 range. 128 const ( 129 PUSH1 OpCode = 0x60 + iota 130 PUSH2 131 PUSH3 132 PUSH4 133 PUSH5 134 PUSH6 135 PUSH7 136 PUSH8 137 PUSH9 138 PUSH10 139 PUSH11 140 PUSH12 141 PUSH13 142 PUSH14 143 PUSH15 144 PUSH16 145 PUSH17 146 PUSH18 147 PUSH19 148 PUSH20 149 PUSH21 150 PUSH22 151 PUSH23 152 PUSH24 153 PUSH25 154 PUSH26 155 PUSH27 156 PUSH28 157 PUSH29 158 PUSH30 159 PUSH31 160 PUSH32 161 DUP1 162 DUP2 163 DUP3 164 DUP4 165 DUP5 166 DUP6 167 DUP7 168 DUP8 169 DUP9 170 DUP10 171 DUP11 172 DUP12 173 DUP13 174 DUP14 175 DUP15 176 DUP16 177 SWAP1 178 SWAP2 179 SWAP3 180 SWAP4 181 SWAP5 182 SWAP6 183 SWAP7 184 SWAP8 185 SWAP9 186 SWAP10 187 SWAP11 188 SWAP12 189 SWAP13 190 SWAP14 191 SWAP15 192 SWAP16 193 ) 194 195 // 0xa0 range - logging ops. 196 const ( 197 LOG0 OpCode = 0xa0 + iota 198 LOG1 199 LOG2 200 LOG3 201 LOG4 202 ) 203 204 // unofficial opcodes used for parsing. 205 const ( 206 PUSH OpCode = 0xb0 + iota 207 DUP 208 SWAP 209 ) 210 211 // 0xf0 range - closures. 212 const ( 213 CREATE OpCode = 0xf0 + iota 214 CALL 215 CALLCODE 216 RETURN 217 DELEGATECALL 218 CREATE2 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 ENERGYPRICE: "ENERGYPRICE", 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 ENERGYLIMIT: "ENERGYLIMIT", 284 NETWORKID: "NETWORKID", 285 SELFBALANCE: "SELFBALANCE", 286 287 // 0x50 range - 'storage' and execution. 288 POP: "POP", 289 //DUP: "DUP", 290 //SWAP: "SWAP", 291 MLOAD: "MLOAD", 292 MSTORE: "MSTORE", 293 MSTORE8: "MSTORE8", 294 SLOAD: "SLOAD", 295 SSTORE: "SSTORE", 296 JUMP: "JUMP", 297 JUMPI: "JUMPI", 298 PC: "PC", 299 MSIZE: "MSIZE", 300 ENERGY: "ENERGY", 301 JUMPDEST: "JUMPDEST", 302 303 BEGINSUB: "BEGINSUB", 304 JUMPSUB: "JUMPSUB", 305 RETURNSUB: "RETURNSUB", 306 307 // 0x60 range - push. 308 PUSH1: "PUSH1", 309 PUSH2: "PUSH2", 310 PUSH3: "PUSH3", 311 PUSH4: "PUSH4", 312 PUSH5: "PUSH5", 313 PUSH6: "PUSH6", 314 PUSH7: "PUSH7", 315 PUSH8: "PUSH8", 316 PUSH9: "PUSH9", 317 PUSH10: "PUSH10", 318 PUSH11: "PUSH11", 319 PUSH12: "PUSH12", 320 PUSH13: "PUSH13", 321 PUSH14: "PUSH14", 322 PUSH15: "PUSH15", 323 PUSH16: "PUSH16", 324 PUSH17: "PUSH17", 325 PUSH18: "PUSH18", 326 PUSH19: "PUSH19", 327 PUSH20: "PUSH20", 328 PUSH21: "PUSH21", 329 PUSH22: "PUSH22", 330 PUSH23: "PUSH23", 331 PUSH24: "PUSH24", 332 PUSH25: "PUSH25", 333 PUSH26: "PUSH26", 334 PUSH27: "PUSH27", 335 PUSH28: "PUSH28", 336 PUSH29: "PUSH29", 337 PUSH30: "PUSH30", 338 PUSH31: "PUSH31", 339 PUSH32: "PUSH32", 340 341 DUP1: "DUP1", 342 DUP2: "DUP2", 343 DUP3: "DUP3", 344 DUP4: "DUP4", 345 DUP5: "DUP5", 346 DUP6: "DUP6", 347 DUP7: "DUP7", 348 DUP8: "DUP8", 349 DUP9: "DUP9", 350 DUP10: "DUP10", 351 DUP11: "DUP11", 352 DUP12: "DUP12", 353 DUP13: "DUP13", 354 DUP14: "DUP14", 355 DUP15: "DUP15", 356 DUP16: "DUP16", 357 358 SWAP1: "SWAP1", 359 SWAP2: "SWAP2", 360 SWAP3: "SWAP3", 361 SWAP4: "SWAP4", 362 SWAP5: "SWAP5", 363 SWAP6: "SWAP6", 364 SWAP7: "SWAP7", 365 SWAP8: "SWAP8", 366 SWAP9: "SWAP9", 367 SWAP10: "SWAP10", 368 SWAP11: "SWAP11", 369 SWAP12: "SWAP12", 370 SWAP13: "SWAP13", 371 SWAP14: "SWAP14", 372 SWAP15: "SWAP15", 373 SWAP16: "SWAP16", 374 LOG0: "LOG0", 375 LOG1: "LOG1", 376 LOG2: "LOG2", 377 LOG3: "LOG3", 378 LOG4: "LOG4", 379 380 // 0xf0 range. 381 CREATE: "CREATE", 382 CALL: "CALL", 383 RETURN: "RETURN", 384 CALLCODE: "CALLCODE", 385 DELEGATECALL: "DELEGATECALL", 386 CREATE2: "CREATE2", 387 STATICCALL: "STATICCALL", 388 REVERT: "REVERT", 389 SELFDESTRUCT: "SELFDESTRUCT", 390 391 PUSH: "PUSH", 392 DUP: "DUP", 393 SWAP: "SWAP", 394 } 395 396 func (op OpCode) String() string { 397 str := opCodeToString[op] 398 if len(str) == 0 { 399 return fmt.Sprintf("opcode 0x%x not defined", int(op)) 400 } 401 402 return str 403 } 404 405 var stringToOp = map[string]OpCode{ 406 "STOP": STOP, 407 "ADD": ADD, 408 "MUL": MUL, 409 "SUB": SUB, 410 "DIV": DIV, 411 "SDIV": SDIV, 412 "MOD": MOD, 413 "SMOD": SMOD, 414 "EXP": EXP, 415 "NOT": NOT, 416 "LT": LT, 417 "GT": GT, 418 "SLT": SLT, 419 "SGT": SGT, 420 "EQ": EQ, 421 "ISZERO": ISZERO, 422 "SIGNEXTEND": SIGNEXTEND, 423 "AND": AND, 424 "OR": OR, 425 "XOR": XOR, 426 "BYTE": BYTE, 427 "SHL": SHL, 428 "SHR": SHR, 429 "SAR": SAR, 430 "ADDMOD": ADDMOD, 431 "MULMOD": MULMOD, 432 "SHA3": SHA3, 433 "ADDRESS": ADDRESS, 434 "BALANCE": BALANCE, 435 "ORIGIN": ORIGIN, 436 "CALLER": CALLER, 437 "CALLVALUE": CALLVALUE, 438 "CALLDATALOAD": CALLDATALOAD, 439 "CALLDATASIZE": CALLDATASIZE, 440 "CALLDATACOPY": CALLDATACOPY, 441 "NETWORKID": NETWORKID, 442 "DELEGATECALL": DELEGATECALL, 443 "STATICCALL": STATICCALL, 444 "CODESIZE": CODESIZE, 445 "CODECOPY": CODECOPY, 446 "ENERGYPRICE": ENERGYPRICE, 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 "ENERGYLIMIT": ENERGYLIMIT, 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 "ENERGY": ENERGY, 470 "JUMPDEST": JUMPDEST, 471 "BEGINSUB": BEGINSUB, 472 "RETURNSUB": RETURNSUB, 473 "JUMPSUB": JUMPSUB, 474 "PUSH1": PUSH1, 475 "PUSH2": PUSH2, 476 "PUSH3": PUSH3, 477 "PUSH4": PUSH4, 478 "PUSH5": PUSH5, 479 "PUSH6": PUSH6, 480 "PUSH7": PUSH7, 481 "PUSH8": PUSH8, 482 "PUSH9": PUSH9, 483 "PUSH10": PUSH10, 484 "PUSH11": PUSH11, 485 "PUSH12": PUSH12, 486 "PUSH13": PUSH13, 487 "PUSH14": PUSH14, 488 "PUSH15": PUSH15, 489 "PUSH16": PUSH16, 490 "PUSH17": PUSH17, 491 "PUSH18": PUSH18, 492 "PUSH19": PUSH19, 493 "PUSH20": PUSH20, 494 "PUSH21": PUSH21, 495 "PUSH22": PUSH22, 496 "PUSH23": PUSH23, 497 "PUSH24": PUSH24, 498 "PUSH25": PUSH25, 499 "PUSH26": PUSH26, 500 "PUSH27": PUSH27, 501 "PUSH28": PUSH28, 502 "PUSH29": PUSH29, 503 "PUSH30": PUSH30, 504 "PUSH31": PUSH31, 505 "PUSH32": PUSH32, 506 "DUP1": DUP1, 507 "DUP2": DUP2, 508 "DUP3": DUP3, 509 "DUP4": DUP4, 510 "DUP5": DUP5, 511 "DUP6": DUP6, 512 "DUP7": DUP7, 513 "DUP8": DUP8, 514 "DUP9": DUP9, 515 "DUP10": DUP10, 516 "DUP11": DUP11, 517 "DUP12": DUP12, 518 "DUP13": DUP13, 519 "DUP14": DUP14, 520 "DUP15": DUP15, 521 "DUP16": DUP16, 522 "SWAP1": SWAP1, 523 "SWAP2": SWAP2, 524 "SWAP3": SWAP3, 525 "SWAP4": SWAP4, 526 "SWAP5": SWAP5, 527 "SWAP6": SWAP6, 528 "SWAP7": SWAP7, 529 "SWAP8": SWAP8, 530 "SWAP9": SWAP9, 531 "SWAP10": SWAP10, 532 "SWAP11": SWAP11, 533 "SWAP12": SWAP12, 534 "SWAP13": SWAP13, 535 "SWAP14": SWAP14, 536 "SWAP15": SWAP15, 537 "SWAP16": SWAP16, 538 "LOG0": LOG0, 539 "LOG1": LOG1, 540 "LOG2": LOG2, 541 "LOG3": LOG3, 542 "LOG4": LOG4, 543 "CREATE": CREATE, 544 "CREATE2": CREATE2, 545 "CALL": CALL, 546 "RETURN": RETURN, 547 "CALLCODE": CALLCODE, 548 "REVERT": REVERT, 549 "SELFDESTRUCT": SELFDESTRUCT, 550 } 551 552 // StringToOp finds the opcode whose name is stored in `str`. 553 func StringToOp(str string) OpCode { 554 return stringToOp[str] 555 }