github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/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 = 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     = 0x46
   105  	SELFBALANCE = 0x47
   106  )
   107  
   108  // 0x50 range - 'storage' and execution.
   109  const (
   110  	POP OpCode = 0x50 + iota
   111  	MLOAD
   112  	MSTORE
   113  	MSTORE8
   114  	SLOAD
   115  	SSTORE
   116  	JUMP
   117  	JUMPI
   118  	PC
   119  	MSIZE
   120  	GAS
   121  	JUMPDEST
   122  )
   123  
   124  // 0x60 range.
   125  const (
   126  	PUSH1 OpCode = 0x60 + iota
   127  	PUSH2
   128  	PUSH3
   129  	PUSH4
   130  	PUSH5
   131  	PUSH6
   132  	PUSH7
   133  	PUSH8
   134  	PUSH9
   135  	PUSH10
   136  	PUSH11
   137  	PUSH12
   138  	PUSH13
   139  	PUSH14
   140  	PUSH15
   141  	PUSH16
   142  	PUSH17
   143  	PUSH18
   144  	PUSH19
   145  	PUSH20
   146  	PUSH21
   147  	PUSH22
   148  	PUSH23
   149  	PUSH24
   150  	PUSH25
   151  	PUSH26
   152  	PUSH27
   153  	PUSH28
   154  	PUSH29
   155  	PUSH30
   156  	PUSH31
   157  	PUSH32
   158  	DUP1
   159  	DUP2
   160  	DUP3
   161  	DUP4
   162  	DUP5
   163  	DUP6
   164  	DUP7
   165  	DUP8
   166  	DUP9
   167  	DUP10
   168  	DUP11
   169  	DUP12
   170  	DUP13
   171  	DUP14
   172  	DUP15
   173  	DUP16
   174  	SWAP1
   175  	SWAP2
   176  	SWAP3
   177  	SWAP4
   178  	SWAP5
   179  	SWAP6
   180  	SWAP7
   181  	SWAP8
   182  	SWAP9
   183  	SWAP10
   184  	SWAP11
   185  	SWAP12
   186  	SWAP13
   187  	SWAP14
   188  	SWAP15
   189  	SWAP16
   190  )
   191  
   192  // 0xa0 range - logging ops.
   193  const (
   194  	LOG0 OpCode = 0xa0 + iota
   195  	LOG1
   196  	LOG2
   197  	LOG3
   198  	LOG4
   199  )
   200  
   201  // unofficial opcodes used for parsing.
   202  const (
   203  	PUSH OpCode = 0xb0 + iota
   204  	DUP
   205  	SWAP
   206  )
   207  
   208  // 0xf0 range - closures.
   209  const (
   210  	CREATE OpCode = 0xf0 + iota
   211  	CALL
   212  	CALLCODE
   213  	RETURN
   214  	DELEGATECALL
   215  	CREATE2
   216  	STATICCALL = 0xfa
   217  
   218  	REVERT       = 0xfd
   219  	SELFDESTRUCT = 0xff
   220  )
   221  
   222  // Since the opcodes aren't all in order we can't use a regular slice.
   223  var opCodeToString = map[OpCode]string{
   224  	// 0x0 range - arithmetic ops.
   225  	STOP:       "STOP",
   226  	ADD:        "ADD",
   227  	MUL:        "MUL",
   228  	SUB:        "SUB",
   229  	DIV:        "DIV",
   230  	SDIV:       "SDIV",
   231  	MOD:        "MOD",
   232  	SMOD:       "SMOD",
   233  	EXP:        "EXP",
   234  	NOT:        "NOT",
   235  	LT:         "LT",
   236  	GT:         "GT",
   237  	SLT:        "SLT",
   238  	SGT:        "SGT",
   239  	EQ:         "EQ",
   240  	ISZERO:     "ISZERO",
   241  	SIGNEXTEND: "SIGNEXTEND",
   242  
   243  	// 0x10 range - bit ops.
   244  	AND:    "AND",
   245  	OR:     "OR",
   246  	XOR:    "XOR",
   247  	BYTE:   "BYTE",
   248  	SHL:    "SHL",
   249  	SHR:    "SHR",
   250  	SAR:    "SAR",
   251  	ADDMOD: "ADDMOD",
   252  	MULMOD: "MULMOD",
   253  
   254  	// 0x20 range - crypto.
   255  	SHA3: "SHA3",
   256  
   257  	// 0x30 range - closure state.
   258  	ADDRESS:        "ADDRESS",
   259  	BALANCE:        "BALANCE",
   260  	ORIGIN:         "ORIGIN",
   261  	CALLER:         "CALLER",
   262  	CALLVALUE:      "CALLVALUE",
   263  	CALLDATALOAD:   "CALLDATALOAD",
   264  	CALLDATASIZE:   "CALLDATASIZE",
   265  	CALLDATACOPY:   "CALLDATACOPY",
   266  	CODESIZE:       "CODESIZE",
   267  	CODECOPY:       "CODECOPY",
   268  	GASPRICE:       "GASPRICE",
   269  	EXTCODESIZE:    "EXTCODESIZE",
   270  	EXTCODECOPY:    "EXTCODECOPY",
   271  	RETURNDATASIZE: "RETURNDATASIZE",
   272  	RETURNDATACOPY: "RETURNDATACOPY",
   273  	EXTCODEHASH:    "EXTCODEHASH",
   274  
   275  	// 0x40 range - block operations.
   276  	BLOCKHASH:   "BLOCKHASH",
   277  	COINBASE:    "COINBASE",
   278  	TIMESTAMP:   "TIMESTAMP",
   279  	NUMBER:      "NUMBER",
   280  	DIFFICULTY:  "DIFFICULTY",
   281  	GASLIMIT:    "GASLIMIT",
   282  	CHAINID:     "CHAINID",
   283  	SELFBALANCE: "SELFBALANCE",
   284  
   285  	// 0x50 range - 'storage' and execution.
   286  	POP: "POP",
   287  	//DUP:     "DUP",
   288  	//SWAP:    "SWAP",
   289  	MLOAD:    "MLOAD",
   290  	MSTORE:   "MSTORE",
   291  	MSTORE8:  "MSTORE8",
   292  	SLOAD:    "SLOAD",
   293  	SSTORE:   "SSTORE",
   294  	JUMP:     "JUMP",
   295  	JUMPI:    "JUMPI",
   296  	PC:       "PC",
   297  	MSIZE:    "MSIZE",
   298  	GAS:      "GAS",
   299  	JUMPDEST: "JUMPDEST",
   300  
   301  	// 0x60 range - push.
   302  	PUSH1:  "PUSH1",
   303  	PUSH2:  "PUSH2",
   304  	PUSH3:  "PUSH3",
   305  	PUSH4:  "PUSH4",
   306  	PUSH5:  "PUSH5",
   307  	PUSH6:  "PUSH6",
   308  	PUSH7:  "PUSH7",
   309  	PUSH8:  "PUSH8",
   310  	PUSH9:  "PUSH9",
   311  	PUSH10: "PUSH10",
   312  	PUSH11: "PUSH11",
   313  	PUSH12: "PUSH12",
   314  	PUSH13: "PUSH13",
   315  	PUSH14: "PUSH14",
   316  	PUSH15: "PUSH15",
   317  	PUSH16: "PUSH16",
   318  	PUSH17: "PUSH17",
   319  	PUSH18: "PUSH18",
   320  	PUSH19: "PUSH19",
   321  	PUSH20: "PUSH20",
   322  	PUSH21: "PUSH21",
   323  	PUSH22: "PUSH22",
   324  	PUSH23: "PUSH23",
   325  	PUSH24: "PUSH24",
   326  	PUSH25: "PUSH25",
   327  	PUSH26: "PUSH26",
   328  	PUSH27: "PUSH27",
   329  	PUSH28: "PUSH28",
   330  	PUSH29: "PUSH29",
   331  	PUSH30: "PUSH30",
   332  	PUSH31: "PUSH31",
   333  	PUSH32: "PUSH32",
   334  
   335  	DUP1:  "DUP1",
   336  	DUP2:  "DUP2",
   337  	DUP3:  "DUP3",
   338  	DUP4:  "DUP4",
   339  	DUP5:  "DUP5",
   340  	DUP6:  "DUP6",
   341  	DUP7:  "DUP7",
   342  	DUP8:  "DUP8",
   343  	DUP9:  "DUP9",
   344  	DUP10: "DUP10",
   345  	DUP11: "DUP11",
   346  	DUP12: "DUP12",
   347  	DUP13: "DUP13",
   348  	DUP14: "DUP14",
   349  	DUP15: "DUP15",
   350  	DUP16: "DUP16",
   351  
   352  	SWAP1:  "SWAP1",
   353  	SWAP2:  "SWAP2",
   354  	SWAP3:  "SWAP3",
   355  	SWAP4:  "SWAP4",
   356  	SWAP5:  "SWAP5",
   357  	SWAP6:  "SWAP6",
   358  	SWAP7:  "SWAP7",
   359  	SWAP8:  "SWAP8",
   360  	SWAP9:  "SWAP9",
   361  	SWAP10: "SWAP10",
   362  	SWAP11: "SWAP11",
   363  	SWAP12: "SWAP12",
   364  	SWAP13: "SWAP13",
   365  	SWAP14: "SWAP14",
   366  	SWAP15: "SWAP15",
   367  	SWAP16: "SWAP16",
   368  	LOG0:   "LOG0",
   369  	LOG1:   "LOG1",
   370  	LOG2:   "LOG2",
   371  	LOG3:   "LOG3",
   372  	LOG4:   "LOG4",
   373  
   374  	// 0xf0 range.
   375  	CREATE:       "CREATE",
   376  	CALL:         "CALL",
   377  	RETURN:       "RETURN",
   378  	CALLCODE:     "CALLCODE",
   379  	DELEGATECALL: "DELEGATECALL",
   380  	CREATE2:      "CREATE2",
   381  	STATICCALL:   "STATICCALL",
   382  	REVERT:       "REVERT",
   383  	SELFDESTRUCT: "SELFDESTRUCT",
   384  
   385  	PUSH: "PUSH",
   386  	DUP:  "DUP",
   387  	SWAP: "SWAP",
   388  }
   389  
   390  func (op OpCode) String() string {
   391  	str := opCodeToString[op]
   392  	if len(str) == 0 {
   393  		return fmt.Sprintf("Missing opcode 0x%x", int(op))
   394  	}
   395  
   396  	return str
   397  }
   398  
   399  var stringToOp = map[string]OpCode{
   400  	"STOP":           STOP,
   401  	"ADD":            ADD,
   402  	"MUL":            MUL,
   403  	"SUB":            SUB,
   404  	"DIV":            DIV,
   405  	"SDIV":           SDIV,
   406  	"MOD":            MOD,
   407  	"SMOD":           SMOD,
   408  	"EXP":            EXP,
   409  	"NOT":            NOT,
   410  	"LT":             LT,
   411  	"GT":             GT,
   412  	"SLT":            SLT,
   413  	"SGT":            SGT,
   414  	"EQ":             EQ,
   415  	"ISZERO":         ISZERO,
   416  	"SIGNEXTEND":     SIGNEXTEND,
   417  	"AND":            AND,
   418  	"OR":             OR,
   419  	"XOR":            XOR,
   420  	"BYTE":           BYTE,
   421  	"SHL":            SHL,
   422  	"SHR":            SHR,
   423  	"SAR":            SAR,
   424  	"ADDMOD":         ADDMOD,
   425  	"MULMOD":         MULMOD,
   426  	"SHA3":           SHA3,
   427  	"ADDRESS":        ADDRESS,
   428  	"BALANCE":        BALANCE,
   429  	"ORIGIN":         ORIGIN,
   430  	"CALLER":         CALLER,
   431  	"CALLVALUE":      CALLVALUE,
   432  	"CALLDATALOAD":   CALLDATALOAD,
   433  	"CALLDATASIZE":   CALLDATASIZE,
   434  	"CALLDATACOPY":   CALLDATACOPY,
   435  	"CHAINID":        CHAINID,
   436  	"DELEGATECALL":   DELEGATECALL,
   437  	"STATICCALL":     STATICCALL,
   438  	"CODESIZE":       CODESIZE,
   439  	"CODECOPY":       CODECOPY,
   440  	"GASPRICE":       GASPRICE,
   441  	"EXTCODESIZE":    EXTCODESIZE,
   442  	"EXTCODECOPY":    EXTCODECOPY,
   443  	"RETURNDATASIZE": RETURNDATASIZE,
   444  	"RETURNDATACOPY": RETURNDATACOPY,
   445  	"EXTCODEHASH":    EXTCODEHASH,
   446  	"BLOCKHASH":      BLOCKHASH,
   447  	"COINBASE":       COINBASE,
   448  	"TIMESTAMP":      TIMESTAMP,
   449  	"NUMBER":         NUMBER,
   450  	"DIFFICULTY":     DIFFICULTY,
   451  	"GASLIMIT":       GASLIMIT,
   452  	"SELFBALANCE":    SELFBALANCE,
   453  	"POP":            POP,
   454  	"MLOAD":          MLOAD,
   455  	"MSTORE":         MSTORE,
   456  	"MSTORE8":        MSTORE8,
   457  	"SLOAD":          SLOAD,
   458  	"SSTORE":         SSTORE,
   459  	"JUMP":           JUMP,
   460  	"JUMPI":          JUMPI,
   461  	"PC":             PC,
   462  	"MSIZE":          MSIZE,
   463  	"GAS":            GAS,
   464  	"JUMPDEST":       JUMPDEST,
   465  	"PUSH1":          PUSH1,
   466  	"PUSH2":          PUSH2,
   467  	"PUSH3":          PUSH3,
   468  	"PUSH4":          PUSH4,
   469  	"PUSH5":          PUSH5,
   470  	"PUSH6":          PUSH6,
   471  	"PUSH7":          PUSH7,
   472  	"PUSH8":          PUSH8,
   473  	"PUSH9":          PUSH9,
   474  	"PUSH10":         PUSH10,
   475  	"PUSH11":         PUSH11,
   476  	"PUSH12":         PUSH12,
   477  	"PUSH13":         PUSH13,
   478  	"PUSH14":         PUSH14,
   479  	"PUSH15":         PUSH15,
   480  	"PUSH16":         PUSH16,
   481  	"PUSH17":         PUSH17,
   482  	"PUSH18":         PUSH18,
   483  	"PUSH19":         PUSH19,
   484  	"PUSH20":         PUSH20,
   485  	"PUSH21":         PUSH21,
   486  	"PUSH22":         PUSH22,
   487  	"PUSH23":         PUSH23,
   488  	"PUSH24":         PUSH24,
   489  	"PUSH25":         PUSH25,
   490  	"PUSH26":         PUSH26,
   491  	"PUSH27":         PUSH27,
   492  	"PUSH28":         PUSH28,
   493  	"PUSH29":         PUSH29,
   494  	"PUSH30":         PUSH30,
   495  	"PUSH31":         PUSH31,
   496  	"PUSH32":         PUSH32,
   497  	"DUP1":           DUP1,
   498  	"DUP2":           DUP2,
   499  	"DUP3":           DUP3,
   500  	"DUP4":           DUP4,
   501  	"DUP5":           DUP5,
   502  	"DUP6":           DUP6,
   503  	"DUP7":           DUP7,
   504  	"DUP8":           DUP8,
   505  	"DUP9":           DUP9,
   506  	"DUP10":          DUP10,
   507  	"DUP11":          DUP11,
   508  	"DUP12":          DUP12,
   509  	"DUP13":          DUP13,
   510  	"DUP14":          DUP14,
   511  	"DUP15":          DUP15,
   512  	"DUP16":          DUP16,
   513  	"SWAP1":          SWAP1,
   514  	"SWAP2":          SWAP2,
   515  	"SWAP3":          SWAP3,
   516  	"SWAP4":          SWAP4,
   517  	"SWAP5":          SWAP5,
   518  	"SWAP6":          SWAP6,
   519  	"SWAP7":          SWAP7,
   520  	"SWAP8":          SWAP8,
   521  	"SWAP9":          SWAP9,
   522  	"SWAP10":         SWAP10,
   523  	"SWAP11":         SWAP11,
   524  	"SWAP12":         SWAP12,
   525  	"SWAP13":         SWAP13,
   526  	"SWAP14":         SWAP14,
   527  	"SWAP15":         SWAP15,
   528  	"SWAP16":         SWAP16,
   529  	"LOG0":           LOG0,
   530  	"LOG1":           LOG1,
   531  	"LOG2":           LOG2,
   532  	"LOG3":           LOG3,
   533  	"LOG4":           LOG4,
   534  	"CREATE":         CREATE,
   535  	"CREATE2":        CREATE2,
   536  	"CALL":           CALL,
   537  	"RETURN":         RETURN,
   538  	"CALLCODE":       CALLCODE,
   539  	"REVERT":         REVERT,
   540  	"SELFDESTRUCT":   SELFDESTRUCT,
   541  }
   542  
   543  // StringToOp finds the opcode whose name is stored in `str`.
   544  func StringToOp(str string) OpCode {
   545  	return stringToOp[str]
   546  }