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