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