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