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