github.com/klaytn/klaytn@v1.12.1/blockchain/vm/opcodes.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2015 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from core/vm/opcodes.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package vm
    22  
    23  import "fmt"
    24  
    25  // OpCode is an EVM opcode
    26  type OpCode byte
    27  
    28  // IsPush specifies if an opcode is a PUSH opcode.
    29  func (op OpCode) IsPush() bool {
    30  	switch op {
    31  	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:
    32  		return true
    33  	}
    34  	return false
    35  }
    36  
    37  // IsStaticJump specifies if an opcode is JUMP.
    38  func (op OpCode) IsStaticJump() bool {
    39  	return op == JUMP
    40  }
    41  
    42  // 0x0 range - arithmetic ops.
    43  const (
    44  	STOP OpCode = iota
    45  	ADD
    46  	MUL
    47  	SUB
    48  	DIV
    49  	SDIV
    50  	MOD
    51  	SMOD
    52  	ADDMOD
    53  	MULMOD
    54  	EXP
    55  	SIGNEXTEND
    56  )
    57  
    58  // 0x10 range - comparison ops.
    59  const (
    60  	LT OpCode = iota + 0x10
    61  	GT
    62  	SLT
    63  	SGT
    64  	EQ
    65  	ISZERO
    66  	AND
    67  	OR
    68  	XOR
    69  	NOT
    70  	BYTE
    71  	SHL
    72  	SHR
    73  	SAR
    74  
    75  	SHA3 = 0x20
    76  )
    77  
    78  // 0x30 range - closure state.
    79  const (
    80  	ADDRESS OpCode = 0x30 + iota
    81  	BALANCE
    82  	ORIGIN
    83  	CALLER
    84  	CALLVALUE
    85  	CALLDATALOAD
    86  	CALLDATASIZE
    87  	CALLDATACOPY
    88  	CODESIZE
    89  	CODECOPY
    90  	GASPRICE
    91  	EXTCODESIZE
    92  	EXTCODECOPY
    93  	RETURNDATASIZE
    94  	RETURNDATACOPY
    95  	EXTCODEHASH
    96  )
    97  
    98  // 0x40 range - block operations.
    99  const (
   100  	BLOCKHASH OpCode = 0x40 + iota
   101  	COINBASE
   102  	TIMESTAMP
   103  	NUMBER
   104  	DIFFICULTY  OpCode = 0x44
   105  	PREVRANDAO  OpCode = 0x44
   106  	GASLIMIT    OpCode = 0x45
   107  	CHAINID     OpCode = 0x46
   108  	SELFBALANCE OpCode = 0x47
   109  	BASEFEE     OpCode = 0x48
   110  	BLOBHASH    OpCode = 0x49
   111  	BLOBBASEFEE OpCode = 0x4a
   112  )
   113  
   114  // 0x50 range - 'storage' and execution.
   115  const (
   116  	POP OpCode = 0x50 + iota
   117  	MLOAD
   118  	MSTORE
   119  	MSTORE8
   120  	SLOAD
   121  	SSTORE
   122  	JUMP
   123  	JUMPI
   124  	PC
   125  	MSIZE
   126  	GAS
   127  	JUMPDEST
   128  	TLOAD  OpCode = 0x5c
   129  	TSTORE OpCode = 0x5d
   130  	MCOPY  OpCode = 0x5e
   131  	PUSH0  OpCode = 0x5f
   132  )
   133  
   134  // 0x60 range.
   135  const (
   136  	PUSH1 OpCode = 0x60 + iota
   137  	PUSH2
   138  	PUSH3
   139  	PUSH4
   140  	PUSH5
   141  	PUSH6
   142  	PUSH7
   143  	PUSH8
   144  	PUSH9
   145  	PUSH10
   146  	PUSH11
   147  	PUSH12
   148  	PUSH13
   149  	PUSH14
   150  	PUSH15
   151  	PUSH16
   152  	PUSH17
   153  	PUSH18
   154  	PUSH19
   155  	PUSH20
   156  	PUSH21
   157  	PUSH22
   158  	PUSH23
   159  	PUSH24
   160  	PUSH25
   161  	PUSH26
   162  	PUSH27
   163  	PUSH28
   164  	PUSH29
   165  	PUSH30
   166  	PUSH31
   167  	PUSH32
   168  	DUP1
   169  	DUP2
   170  	DUP3
   171  	DUP4
   172  	DUP5
   173  	DUP6
   174  	DUP7
   175  	DUP8
   176  	DUP9
   177  	DUP10
   178  	DUP11
   179  	DUP12
   180  	DUP13
   181  	DUP14
   182  	DUP15
   183  	DUP16
   184  	SWAP1
   185  	SWAP2
   186  	SWAP3
   187  	SWAP4
   188  	SWAP5
   189  	SWAP6
   190  	SWAP7
   191  	SWAP8
   192  	SWAP9
   193  	SWAP10
   194  	SWAP11
   195  	SWAP12
   196  	SWAP13
   197  	SWAP14
   198  	SWAP15
   199  	SWAP16
   200  )
   201  
   202  // 0xa0 range - logging ops.
   203  const (
   204  	LOG0 OpCode = 0xa0 + iota
   205  	LOG1
   206  	LOG2
   207  	LOG3
   208  	LOG4
   209  )
   210  
   211  // unofficial opcodes used for parsing.
   212  const (
   213  	PUSH OpCode = 0xb0 + iota
   214  	DUP
   215  	SWAP
   216  )
   217  
   218  // 0xf0 range - closures.
   219  const (
   220  	CREATE OpCode = 0xf0 + iota
   221  	CALL
   222  	CALLCODE
   223  	RETURN
   224  	DELEGATECALL
   225  	CREATE2
   226  	STATICCALL = 0xfa
   227  
   228  	REVERT       = 0xfd
   229  	SELFDESTRUCT = 0xff
   230  )
   231  
   232  // Since the opcodes aren't all in order we can't use a regular slice.
   233  var opCodeToString = map[OpCode]string{
   234  	// 0x0 range - arithmetic ops.
   235  	STOP:       "STOP",
   236  	ADD:        "ADD",
   237  	MUL:        "MUL",
   238  	SUB:        "SUB",
   239  	DIV:        "DIV",
   240  	SDIV:       "SDIV",
   241  	MOD:        "MOD",
   242  	SMOD:       "SMOD",
   243  	EXP:        "EXP",
   244  	NOT:        "NOT",
   245  	LT:         "LT",
   246  	GT:         "GT",
   247  	SLT:        "SLT",
   248  	SGT:        "SGT",
   249  	EQ:         "EQ",
   250  	ISZERO:     "ISZERO",
   251  	SIGNEXTEND: "SIGNEXTEND",
   252  
   253  	// 0x10 range - bit ops.
   254  	AND:    "AND",
   255  	OR:     "OR",
   256  	XOR:    "XOR",
   257  	BYTE:   "BYTE",
   258  	SHL:    "SHL",
   259  	SHR:    "SHR",
   260  	SAR:    "SAR",
   261  	ADDMOD: "ADDMOD",
   262  	MULMOD: "MULMOD",
   263  
   264  	// 0x20 range - crypto.
   265  	SHA3: "SHA3",
   266  
   267  	// 0x30 range - closure state.
   268  	ADDRESS:        "ADDRESS",
   269  	BALANCE:        "BALANCE",
   270  	ORIGIN:         "ORIGIN",
   271  	CALLER:         "CALLER",
   272  	CALLVALUE:      "CALLVALUE",
   273  	CALLDATALOAD:   "CALLDATALOAD",
   274  	CALLDATASIZE:   "CALLDATASIZE",
   275  	CALLDATACOPY:   "CALLDATACOPY",
   276  	CODESIZE:       "CODESIZE",
   277  	CODECOPY:       "CODECOPY",
   278  	GASPRICE:       "GASPRICE",
   279  	EXTCODESIZE:    "EXTCODESIZE",
   280  	EXTCODECOPY:    "EXTCODECOPY",
   281  	RETURNDATASIZE: "RETURNDATASIZE",
   282  	RETURNDATACOPY: "RETURNDATACOPY",
   283  	EXTCODEHASH:    "EXTCODEHASH",
   284  
   285  	// 0x40 range - block operations.
   286  	BLOCKHASH:   "BLOCKHASH",
   287  	COINBASE:    "COINBASE",
   288  	TIMESTAMP:   "TIMESTAMP",
   289  	NUMBER:      "NUMBER",
   290  	DIFFICULTY:  "DIFFICULTY",
   291  	GASLIMIT:    "GASLIMIT",
   292  	CHAINID:     "CHAINID",
   293  	SELFBALANCE: "SELFBALANCE",
   294  	BASEFEE:     "BASEFEE",
   295  	BLOBHASH:    "BLOBHASH",
   296  	BLOBBASEFEE: "BLOBBASEFEE",
   297  
   298  	// 0x50 range - 'storage' and execution.
   299  	POP: "POP",
   300  	// DUP:     "DUP",
   301  	// SWAP:    "SWAP",
   302  	MLOAD:    "MLOAD",
   303  	MSTORE:   "MSTORE",
   304  	MSTORE8:  "MSTORE8",
   305  	SLOAD:    "SLOAD",
   306  	SSTORE:   "SSTORE",
   307  	JUMP:     "JUMP",
   308  	JUMPI:    "JUMPI",
   309  	PC:       "PC",
   310  	MSIZE:    "MSIZE",
   311  	GAS:      "GAS",
   312  	JUMPDEST: "JUMPDEST",
   313  	MCOPY:    "MCOPY",
   314  	PUSH0:    "PUSH0",
   315  
   316  	// 0x60 range - push.
   317  	PUSH1:  "PUSH1",
   318  	PUSH2:  "PUSH2",
   319  	PUSH3:  "PUSH3",
   320  	PUSH4:  "PUSH4",
   321  	PUSH5:  "PUSH5",
   322  	PUSH6:  "PUSH6",
   323  	PUSH7:  "PUSH7",
   324  	PUSH8:  "PUSH8",
   325  	PUSH9:  "PUSH9",
   326  	PUSH10: "PUSH10",
   327  	PUSH11: "PUSH11",
   328  	PUSH12: "PUSH12",
   329  	PUSH13: "PUSH13",
   330  	PUSH14: "PUSH14",
   331  	PUSH15: "PUSH15",
   332  	PUSH16: "PUSH16",
   333  	PUSH17: "PUSH17",
   334  	PUSH18: "PUSH18",
   335  	PUSH19: "PUSH19",
   336  	PUSH20: "PUSH20",
   337  	PUSH21: "PUSH21",
   338  	PUSH22: "PUSH22",
   339  	PUSH23: "PUSH23",
   340  	PUSH24: "PUSH24",
   341  	PUSH25: "PUSH25",
   342  	PUSH26: "PUSH26",
   343  	PUSH27: "PUSH27",
   344  	PUSH28: "PUSH28",
   345  	PUSH29: "PUSH29",
   346  	PUSH30: "PUSH30",
   347  	PUSH31: "PUSH31",
   348  	PUSH32: "PUSH32",
   349  
   350  	DUP1:  "DUP1",
   351  	DUP2:  "DUP2",
   352  	DUP3:  "DUP3",
   353  	DUP4:  "DUP4",
   354  	DUP5:  "DUP5",
   355  	DUP6:  "DUP6",
   356  	DUP7:  "DUP7",
   357  	DUP8:  "DUP8",
   358  	DUP9:  "DUP9",
   359  	DUP10: "DUP10",
   360  	DUP11: "DUP11",
   361  	DUP12: "DUP12",
   362  	DUP13: "DUP13",
   363  	DUP14: "DUP14",
   364  	DUP15: "DUP15",
   365  	DUP16: "DUP16",
   366  
   367  	SWAP1:  "SWAP1",
   368  	SWAP2:  "SWAP2",
   369  	SWAP3:  "SWAP3",
   370  	SWAP4:  "SWAP4",
   371  	SWAP5:  "SWAP5",
   372  	SWAP6:  "SWAP6",
   373  	SWAP7:  "SWAP7",
   374  	SWAP8:  "SWAP8",
   375  	SWAP9:  "SWAP9",
   376  	SWAP10: "SWAP10",
   377  	SWAP11: "SWAP11",
   378  	SWAP12: "SWAP12",
   379  	SWAP13: "SWAP13",
   380  	SWAP14: "SWAP14",
   381  	SWAP15: "SWAP15",
   382  	SWAP16: "SWAP16",
   383  	LOG0:   "LOG0",
   384  	LOG1:   "LOG1",
   385  	LOG2:   "LOG2",
   386  	LOG3:   "LOG3",
   387  	LOG4:   "LOG4",
   388  
   389  	// 0xf0 range.
   390  	CREATE:       "CREATE",
   391  	CALL:         "CALL",
   392  	RETURN:       "RETURN",
   393  	CALLCODE:     "CALLCODE",
   394  	DELEGATECALL: "DELEGATECALL",
   395  	CREATE2:      "CREATE2",
   396  	STATICCALL:   "STATICCALL",
   397  	REVERT:       "REVERT",
   398  	SELFDESTRUCT: "SELFDESTRUCT",
   399  
   400  	PUSH: "PUSH",
   401  	DUP:  "DUP",
   402  	SWAP: "SWAP",
   403  }
   404  
   405  func (op OpCode) String() string {
   406  	str := opCodeToString[op]
   407  	if len(str) == 0 {
   408  		return fmt.Sprintf("Missing opcode 0x%x", int(op))
   409  	}
   410  
   411  	return str
   412  }
   413  
   414  var stringToOp = map[string]OpCode{
   415  	"STOP":           STOP,
   416  	"ADD":            ADD,
   417  	"MUL":            MUL,
   418  	"SUB":            SUB,
   419  	"DIV":            DIV,
   420  	"SDIV":           SDIV,
   421  	"MOD":            MOD,
   422  	"SMOD":           SMOD,
   423  	"EXP":            EXP,
   424  	"NOT":            NOT,
   425  	"LT":             LT,
   426  	"GT":             GT,
   427  	"SLT":            SLT,
   428  	"SGT":            SGT,
   429  	"EQ":             EQ,
   430  	"ISZERO":         ISZERO,
   431  	"SIGNEXTEND":     SIGNEXTEND,
   432  	"AND":            AND,
   433  	"OR":             OR,
   434  	"XOR":            XOR,
   435  	"BYTE":           BYTE,
   436  	"SHL":            SHL,
   437  	"SHR":            SHR,
   438  	"SAR":            SAR,
   439  	"ADDMOD":         ADDMOD,
   440  	"MULMOD":         MULMOD,
   441  	"SHA3":           SHA3,
   442  	"ADDRESS":        ADDRESS,
   443  	"BALANCE":        BALANCE,
   444  	"ORIGIN":         ORIGIN,
   445  	"CALLER":         CALLER,
   446  	"CALLVALUE":      CALLVALUE,
   447  	"CALLDATALOAD":   CALLDATALOAD,
   448  	"CALLDATASIZE":   CALLDATASIZE,
   449  	"CALLDATACOPY":   CALLDATACOPY,
   450  	"CHAINID":        CHAINID,
   451  	"BASEFEE":        BASEFEE,
   452  	"BLOBHASH":       BLOBHASH,
   453  	"BLOBBASEFEE":    BLOBBASEFEE,
   454  	"DELEGATECALL":   DELEGATECALL,
   455  	"STATICCALL":     STATICCALL,
   456  	"CODESIZE":       CODESIZE,
   457  	"CODECOPY":       CODECOPY,
   458  	"GASPRICE":       GASPRICE,
   459  	"EXTCODESIZE":    EXTCODESIZE,
   460  	"EXTCODECOPY":    EXTCODECOPY,
   461  	"RETURNDATASIZE": RETURNDATASIZE,
   462  	"RETURNDATACOPY": RETURNDATACOPY,
   463  	"EXTCODEHASH":    EXTCODEHASH,
   464  	"BLOCKHASH":      BLOCKHASH,
   465  	"COINBASE":       COINBASE,
   466  	"TIMESTAMP":      TIMESTAMP,
   467  	"NUMBER":         NUMBER,
   468  	"DIFFICULTY":     DIFFICULTY,
   469  	"GASLIMIT":       GASLIMIT,
   470  	"SELFBALANCE":    SELFBALANCE,
   471  	"POP":            POP,
   472  	"MLOAD":          MLOAD,
   473  	"MSTORE":         MSTORE,
   474  	"MSTORE8":        MSTORE8,
   475  	"SLOAD":          SLOAD,
   476  	"SSTORE":         SSTORE,
   477  	"JUMP":           JUMP,
   478  	"JUMPI":          JUMPI,
   479  	"PC":             PC,
   480  	"MSIZE":          MSIZE,
   481  	"GAS":            GAS,
   482  	"JUMPDEST":       JUMPDEST,
   483  	"MCOPY":          MCOPY,
   484  	"PUSH0":          PUSH0,
   485  	"PUSH1":          PUSH1,
   486  	"PUSH2":          PUSH2,
   487  	"PUSH3":          PUSH3,
   488  	"PUSH4":          PUSH4,
   489  	"PUSH5":          PUSH5,
   490  	"PUSH6":          PUSH6,
   491  	"PUSH7":          PUSH7,
   492  	"PUSH8":          PUSH8,
   493  	"PUSH9":          PUSH9,
   494  	"PUSH10":         PUSH10,
   495  	"PUSH11":         PUSH11,
   496  	"PUSH12":         PUSH12,
   497  	"PUSH13":         PUSH13,
   498  	"PUSH14":         PUSH14,
   499  	"PUSH15":         PUSH15,
   500  	"PUSH16":         PUSH16,
   501  	"PUSH17":         PUSH17,
   502  	"PUSH18":         PUSH18,
   503  	"PUSH19":         PUSH19,
   504  	"PUSH20":         PUSH20,
   505  	"PUSH21":         PUSH21,
   506  	"PUSH22":         PUSH22,
   507  	"PUSH23":         PUSH23,
   508  	"PUSH24":         PUSH24,
   509  	"PUSH25":         PUSH25,
   510  	"PUSH26":         PUSH26,
   511  	"PUSH27":         PUSH27,
   512  	"PUSH28":         PUSH28,
   513  	"PUSH29":         PUSH29,
   514  	"PUSH30":         PUSH30,
   515  	"PUSH31":         PUSH31,
   516  	"PUSH32":         PUSH32,
   517  	"DUP1":           DUP1,
   518  	"DUP2":           DUP2,
   519  	"DUP3":           DUP3,
   520  	"DUP4":           DUP4,
   521  	"DUP5":           DUP5,
   522  	"DUP6":           DUP6,
   523  	"DUP7":           DUP7,
   524  	"DUP8":           DUP8,
   525  	"DUP9":           DUP9,
   526  	"DUP10":          DUP10,
   527  	"DUP11":          DUP11,
   528  	"DUP12":          DUP12,
   529  	"DUP13":          DUP13,
   530  	"DUP14":          DUP14,
   531  	"DUP15":          DUP15,
   532  	"DUP16":          DUP16,
   533  	"SWAP1":          SWAP1,
   534  	"SWAP2":          SWAP2,
   535  	"SWAP3":          SWAP3,
   536  	"SWAP4":          SWAP4,
   537  	"SWAP5":          SWAP5,
   538  	"SWAP6":          SWAP6,
   539  	"SWAP7":          SWAP7,
   540  	"SWAP8":          SWAP8,
   541  	"SWAP9":          SWAP9,
   542  	"SWAP10":         SWAP10,
   543  	"SWAP11":         SWAP11,
   544  	"SWAP12":         SWAP12,
   545  	"SWAP13":         SWAP13,
   546  	"SWAP14":         SWAP14,
   547  	"SWAP15":         SWAP15,
   548  	"SWAP16":         SWAP16,
   549  	"LOG0":           LOG0,
   550  	"LOG1":           LOG1,
   551  	"LOG2":           LOG2,
   552  	"LOG3":           LOG3,
   553  	"LOG4":           LOG4,
   554  	"CREATE":         CREATE,
   555  	"CREATE2":        CREATE2,
   556  	"CALL":           CALL,
   557  	"RETURN":         RETURN,
   558  	"CALLCODE":       CALLCODE,
   559  	"REVERT":         REVERT,
   560  	"SELFDESTRUCT":   SELFDESTRUCT,
   561  	"TLOAD":          TLOAD,
   562  	"TSTORE":         TSTORE,
   563  }
   564  
   565  // StringToOp finds the opcode whose name is stored in `str`.
   566  func StringToOp(str string) OpCode {
   567  	return stringToOp[str]
   568  }