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