gitee.com/liu-zhao234568/cntest@v1.0.0/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  	STATICCALL   OpCode = 0xfa
   218  	REVERT       OpCode = 0xfd
   219  	SELFDESTRUCT OpCode = 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  	BASEFEE:     "BASEFEE",
   285  
   286  	// 0x50 range - 'storage' and execution.
   287  	POP: "POP",
   288  	//DUP:     "DUP",
   289  	//SWAP:    "SWAP",
   290  	MLOAD:    "MLOAD",
   291  	MSTORE:   "MSTORE",
   292  	MSTORE8:  "MSTORE8",
   293  	SLOAD:    "SLOAD",
   294  	SSTORE:   "SSTORE",
   295  	JUMP:     "JUMP",
   296  	JUMPI:    "JUMPI",
   297  	PC:       "PC",
   298  	MSIZE:    "MSIZE",
   299  	GAS:      "GAS",
   300  	JUMPDEST: "JUMPDEST",
   301  
   302  	// 0x60 range - push.
   303  	PUSH1:  "PUSH1",
   304  	PUSH2:  "PUSH2",
   305  	PUSH3:  "PUSH3",
   306  	PUSH4:  "PUSH4",
   307  	PUSH5:  "PUSH5",
   308  	PUSH6:  "PUSH6",
   309  	PUSH7:  "PUSH7",
   310  	PUSH8:  "PUSH8",
   311  	PUSH9:  "PUSH9",
   312  	PUSH10: "PUSH10",
   313  	PUSH11: "PUSH11",
   314  	PUSH12: "PUSH12",
   315  	PUSH13: "PUSH13",
   316  	PUSH14: "PUSH14",
   317  	PUSH15: "PUSH15",
   318  	PUSH16: "PUSH16",
   319  	PUSH17: "PUSH17",
   320  	PUSH18: "PUSH18",
   321  	PUSH19: "PUSH19",
   322  	PUSH20: "PUSH20",
   323  	PUSH21: "PUSH21",
   324  	PUSH22: "PUSH22",
   325  	PUSH23: "PUSH23",
   326  	PUSH24: "PUSH24",
   327  	PUSH25: "PUSH25",
   328  	PUSH26: "PUSH26",
   329  	PUSH27: "PUSH27",
   330  	PUSH28: "PUSH28",
   331  	PUSH29: "PUSH29",
   332  	PUSH30: "PUSH30",
   333  	PUSH31: "PUSH31",
   334  	PUSH32: "PUSH32",
   335  
   336  	DUP1:  "DUP1",
   337  	DUP2:  "DUP2",
   338  	DUP3:  "DUP3",
   339  	DUP4:  "DUP4",
   340  	DUP5:  "DUP5",
   341  	DUP6:  "DUP6",
   342  	DUP7:  "DUP7",
   343  	DUP8:  "DUP8",
   344  	DUP9:  "DUP9",
   345  	DUP10: "DUP10",
   346  	DUP11: "DUP11",
   347  	DUP12: "DUP12",
   348  	DUP13: "DUP13",
   349  	DUP14: "DUP14",
   350  	DUP15: "DUP15",
   351  	DUP16: "DUP16",
   352  
   353  	SWAP1:  "SWAP1",
   354  	SWAP2:  "SWAP2",
   355  	SWAP3:  "SWAP3",
   356  	SWAP4:  "SWAP4",
   357  	SWAP5:  "SWAP5",
   358  	SWAP6:  "SWAP6",
   359  	SWAP7:  "SWAP7",
   360  	SWAP8:  "SWAP8",
   361  	SWAP9:  "SWAP9",
   362  	SWAP10: "SWAP10",
   363  	SWAP11: "SWAP11",
   364  	SWAP12: "SWAP12",
   365  	SWAP13: "SWAP13",
   366  	SWAP14: "SWAP14",
   367  	SWAP15: "SWAP15",
   368  	SWAP16: "SWAP16",
   369  	LOG0:   "LOG0",
   370  	LOG1:   "LOG1",
   371  	LOG2:   "LOG2",
   372  	LOG3:   "LOG3",
   373  	LOG4:   "LOG4",
   374  
   375  	// 0xf0 range.
   376  	CREATE:       "CREATE",
   377  	CALL:         "CALL",
   378  	RETURN:       "RETURN",
   379  	CALLCODE:     "CALLCODE",
   380  	DELEGATECALL: "DELEGATECALL",
   381  	CREATE2:      "CREATE2",
   382  	STATICCALL:   "STATICCALL",
   383  	REVERT:       "REVERT",
   384  	SELFDESTRUCT: "SELFDESTRUCT",
   385  
   386  	PUSH: "PUSH",
   387  	DUP:  "DUP",
   388  	SWAP: "SWAP",
   389  }
   390  
   391  func (op OpCode) String() string {
   392  	str := opCodeToString[op]
   393  	if len(str) == 0 {
   394  		return fmt.Sprintf("opcode 0x%x not defined", int(op))
   395  	}
   396  
   397  	return str
   398  }
   399  
   400  var stringToOp = map[string]OpCode{
   401  	"STOP":           STOP,
   402  	"ADD":            ADD,
   403  	"MUL":            MUL,
   404  	"SUB":            SUB,
   405  	"DIV":            DIV,
   406  	"SDIV":           SDIV,
   407  	"MOD":            MOD,
   408  	"SMOD":           SMOD,
   409  	"EXP":            EXP,
   410  	"NOT":            NOT,
   411  	"LT":             LT,
   412  	"GT":             GT,
   413  	"SLT":            SLT,
   414  	"SGT":            SGT,
   415  	"EQ":             EQ,
   416  	"ISZERO":         ISZERO,
   417  	"SIGNEXTEND":     SIGNEXTEND,
   418  	"AND":            AND,
   419  	"OR":             OR,
   420  	"XOR":            XOR,
   421  	"BYTE":           BYTE,
   422  	"SHL":            SHL,
   423  	"SHR":            SHR,
   424  	"SAR":            SAR,
   425  	"ADDMOD":         ADDMOD,
   426  	"MULMOD":         MULMOD,
   427  	"SHA3":           SHA3,
   428  	"ADDRESS":        ADDRESS,
   429  	"BALANCE":        BALANCE,
   430  	"ORIGIN":         ORIGIN,
   431  	"CALLER":         CALLER,
   432  	"CALLVALUE":      CALLVALUE,
   433  	"CALLDATALOAD":   CALLDATALOAD,
   434  	"CALLDATASIZE":   CALLDATASIZE,
   435  	"CALLDATACOPY":   CALLDATACOPY,
   436  	"CHAINID":        CHAINID,
   437  	"BASEFEE":        BASEFEE,
   438  	"DELEGATECALL":   DELEGATECALL,
   439  	"STATICCALL":     STATICCALL,
   440  	"CODESIZE":       CODESIZE,
   441  	"CODECOPY":       CODECOPY,
   442  	"GASPRICE":       GASPRICE,
   443  	"EXTCODESIZE":    EXTCODESIZE,
   444  	"EXTCODECOPY":    EXTCODECOPY,
   445  	"RETURNDATASIZE": RETURNDATASIZE,
   446  	"RETURNDATACOPY": RETURNDATACOPY,
   447  	"EXTCODEHASH":    EXTCODEHASH,
   448  	"BLOCKHASH":      BLOCKHASH,
   449  	"COINBASE":       COINBASE,
   450  	"TIMESTAMP":      TIMESTAMP,
   451  	"NUMBER":         NUMBER,
   452  	"DIFFICULTY":     DIFFICULTY,
   453  	"GASLIMIT":       GASLIMIT,
   454  	"SELFBALANCE":    SELFBALANCE,
   455  	"POP":            POP,
   456  	"MLOAD":          MLOAD,
   457  	"MSTORE":         MSTORE,
   458  	"MSTORE8":        MSTORE8,
   459  	"SLOAD":          SLOAD,
   460  	"SSTORE":         SSTORE,
   461  	"JUMP":           JUMP,
   462  	"JUMPI":          JUMPI,
   463  	"PC":             PC,
   464  	"MSIZE":          MSIZE,
   465  	"GAS":            GAS,
   466  	"JUMPDEST":       JUMPDEST,
   467  	"PUSH1":          PUSH1,
   468  	"PUSH2":          PUSH2,
   469  	"PUSH3":          PUSH3,
   470  	"PUSH4":          PUSH4,
   471  	"PUSH5":          PUSH5,
   472  	"PUSH6":          PUSH6,
   473  	"PUSH7":          PUSH7,
   474  	"PUSH8":          PUSH8,
   475  	"PUSH9":          PUSH9,
   476  	"PUSH10":         PUSH10,
   477  	"PUSH11":         PUSH11,
   478  	"PUSH12":         PUSH12,
   479  	"PUSH13":         PUSH13,
   480  	"PUSH14":         PUSH14,
   481  	"PUSH15":         PUSH15,
   482  	"PUSH16":         PUSH16,
   483  	"PUSH17":         PUSH17,
   484  	"PUSH18":         PUSH18,
   485  	"PUSH19":         PUSH19,
   486  	"PUSH20":         PUSH20,
   487  	"PUSH21":         PUSH21,
   488  	"PUSH22":         PUSH22,
   489  	"PUSH23":         PUSH23,
   490  	"PUSH24":         PUSH24,
   491  	"PUSH25":         PUSH25,
   492  	"PUSH26":         PUSH26,
   493  	"PUSH27":         PUSH27,
   494  	"PUSH28":         PUSH28,
   495  	"PUSH29":         PUSH29,
   496  	"PUSH30":         PUSH30,
   497  	"PUSH31":         PUSH31,
   498  	"PUSH32":         PUSH32,
   499  	"DUP1":           DUP1,
   500  	"DUP2":           DUP2,
   501  	"DUP3":           DUP3,
   502  	"DUP4":           DUP4,
   503  	"DUP5":           DUP5,
   504  	"DUP6":           DUP6,
   505  	"DUP7":           DUP7,
   506  	"DUP8":           DUP8,
   507  	"DUP9":           DUP9,
   508  	"DUP10":          DUP10,
   509  	"DUP11":          DUP11,
   510  	"DUP12":          DUP12,
   511  	"DUP13":          DUP13,
   512  	"DUP14":          DUP14,
   513  	"DUP15":          DUP15,
   514  	"DUP16":          DUP16,
   515  	"SWAP1":          SWAP1,
   516  	"SWAP2":          SWAP2,
   517  	"SWAP3":          SWAP3,
   518  	"SWAP4":          SWAP4,
   519  	"SWAP5":          SWAP5,
   520  	"SWAP6":          SWAP6,
   521  	"SWAP7":          SWAP7,
   522  	"SWAP8":          SWAP8,
   523  	"SWAP9":          SWAP9,
   524  	"SWAP10":         SWAP10,
   525  	"SWAP11":         SWAP11,
   526  	"SWAP12":         SWAP12,
   527  	"SWAP13":         SWAP13,
   528  	"SWAP14":         SWAP14,
   529  	"SWAP15":         SWAP15,
   530  	"SWAP16":         SWAP16,
   531  	"LOG0":           LOG0,
   532  	"LOG1":           LOG1,
   533  	"LOG2":           LOG2,
   534  	"LOG3":           LOG3,
   535  	"LOG4":           LOG4,
   536  	"CREATE":         CREATE,
   537  	"CREATE2":        CREATE2,
   538  	"CALL":           CALL,
   539  	"RETURN":         RETURN,
   540  	"CALLCODE":       CALLCODE,
   541  	"REVERT":         REVERT,
   542  	"SELFDESTRUCT":   SELFDESTRUCT,
   543  }
   544  
   545  // StringToOp finds the opcode whose name is stored in `str`.
   546  func StringToOp(str string) OpCode {
   547  	return stringToOp[str]
   548  }