github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/core/vm/opcodes.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2014 Go Ethereum作者
    10  //此文件是Go以太坊库的一部分。
    11  //
    12  //Go-Ethereum库是免费软件:您可以重新分发它和/或修改
    13  //根据GNU发布的较低通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊图书馆的发行目的是希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU较低的通用公共许可证,了解更多详细信息。
    21  //
    22  //你应该收到一份GNU较低级别的公共许可证副本
    23  //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  package vm
    26  
    27  import (
    28  	"fmt"
    29  )
    30  
    31  //操作码是EVM操作码
    32  type OpCode byte
    33  
    34  //is push指定操作码是否为推送操作码。
    35  func (op OpCode) IsPush() bool {
    36  	switch op {
    37  	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:
    38  		return true
    39  	}
    40  	return false
    41  }
    42  
    43  //IsStaticJump指定操作码是否为跳转。
    44  func (op OpCode) IsStaticJump() bool {
    45  	return op == JUMP
    46  }
    47  
    48  //0x0范围-算术运算。
    49  const (
    50  	STOP OpCode = iota
    51  	ADD
    52  	MUL
    53  	SUB
    54  	DIV
    55  	SDIV
    56  	MOD
    57  	SMOD
    58  	ADDMOD
    59  	MULMOD
    60  	EXP
    61  	SIGNEXTEND
    62  )
    63  
    64  //0x10范围-比较操作。
    65  const (
    66  	LT OpCode = iota + 0x10
    67  	GT
    68  	SLT
    69  	SGT
    70  	EQ
    71  	ISZERO
    72  	AND
    73  	OR
    74  	XOR
    75  	NOT
    76  	BYTE
    77  	SHL
    78  	SHR
    79  	SAR
    80  
    81  	SHA3 = 0x20
    82  )
    83  
    84  //0x30范围-关闭状态。
    85  const (
    86  	ADDRESS OpCode = 0x30 + iota
    87  	BALANCE
    88  	ORIGIN
    89  	CALLER
    90  	CALLVALUE
    91  	CALLDATALOAD
    92  	CALLDATASIZE
    93  	CALLDATACOPY
    94  	CODESIZE
    95  	CODECOPY
    96  	GASPRICE
    97  	EXTCODESIZE
    98  	EXTCODECOPY
    99  	RETURNDATASIZE
   100  	RETURNDATACOPY
   101  	EXTCODEHASH
   102  )
   103  
   104  //0x40范围-块操作。
   105  const (
   106  	BLOCKHASH OpCode = 0x40 + iota
   107  	COINBASE
   108  	TIMESTAMP
   109  	NUMBER
   110  	DIFFICULTY
   111  	GASLIMIT
   112  )
   113  
   114  //0x50范围-“存储”和执行。
   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  )
   129  
   130  //0x60范围。
   131  const (
   132  	PUSH1 OpCode = 0x60 + iota
   133  	PUSH2
   134  	PUSH3
   135  	PUSH4
   136  	PUSH5
   137  	PUSH6
   138  	PUSH7
   139  	PUSH8
   140  	PUSH9
   141  	PUSH10
   142  	PUSH11
   143  	PUSH12
   144  	PUSH13
   145  	PUSH14
   146  	PUSH15
   147  	PUSH16
   148  	PUSH17
   149  	PUSH18
   150  	PUSH19
   151  	PUSH20
   152  	PUSH21
   153  	PUSH22
   154  	PUSH23
   155  	PUSH24
   156  	PUSH25
   157  	PUSH26
   158  	PUSH27
   159  	PUSH28
   160  	PUSH29
   161  	PUSH30
   162  	PUSH31
   163  	PUSH32
   164  	DUP1
   165  	DUP2
   166  	DUP3
   167  	DUP4
   168  	DUP5
   169  	DUP6
   170  	DUP7
   171  	DUP8
   172  	DUP9
   173  	DUP10
   174  	DUP11
   175  	DUP12
   176  	DUP13
   177  	DUP14
   178  	DUP15
   179  	DUP16
   180  	SWAP1
   181  	SWAP2
   182  	SWAP3
   183  	SWAP4
   184  	SWAP5
   185  	SWAP6
   186  	SWAP7
   187  	SWAP8
   188  	SWAP9
   189  	SWAP10
   190  	SWAP11
   191  	SWAP12
   192  	SWAP13
   193  	SWAP14
   194  	SWAP15
   195  	SWAP16
   196  )
   197  
   198  //0XA0范围-记录操作。
   199  const (
   200  	LOG0 OpCode = 0xa0 + iota
   201  	LOG1
   202  	LOG2
   203  	LOG3
   204  	LOG4
   205  )
   206  
   207  //用于解析的非官方操作码。
   208  const (
   209  	PUSH OpCode = 0xb0 + iota
   210  	DUP
   211  	SWAP
   212  )
   213  
   214  //0xF0范围-关闭。
   215  const (
   216  	CREATE OpCode = 0xf0 + iota
   217  	CALL
   218  	CALLCODE
   219  	RETURN
   220  	DELEGATECALL
   221  	CREATE2
   222  	STATICCALL = 0xfa
   223  
   224  	REVERT       = 0xfd
   225  	SELFDESTRUCT = 0xff
   226  )
   227  
   228  //由于操作码不是全部正常,我们不能使用常规切片。
   229  var opCodeToString = map[OpCode]string{
   230  //0x0范围-算术运算。
   231  	STOP:       "STOP",
   232  	ADD:        "ADD",
   233  	MUL:        "MUL",
   234  	SUB:        "SUB",
   235  	DIV:        "DIV",
   236  	SDIV:       "SDIV",
   237  	MOD:        "MOD",
   238  	SMOD:       "SMOD",
   239  	EXP:        "EXP",
   240  	NOT:        "NOT",
   241  	LT:         "LT",
   242  	GT:         "GT",
   243  	SLT:        "SLT",
   244  	SGT:        "SGT",
   245  	EQ:         "EQ",
   246  	ISZERO:     "ISZERO",
   247  	SIGNEXTEND: "SIGNEXTEND",
   248  
   249  //0x10范围-位操作。
   250  	AND:    "AND",
   251  	OR:     "OR",
   252  	XOR:    "XOR",
   253  	BYTE:   "BYTE",
   254  	SHL:    "SHL",
   255  	SHR:    "SHR",
   256  	SAR:    "SAR",
   257  	ADDMOD: "ADDMOD",
   258  	MULMOD: "MULMOD",
   259  
   260  //0x20范围-加密。
   261  	SHA3: "SHA3",
   262  
   263  //0x30范围-关闭状态。
   264  	ADDRESS:        "ADDRESS",
   265  	BALANCE:        "BALANCE",
   266  	ORIGIN:         "ORIGIN",
   267  	CALLER:         "CALLER",
   268  	CALLVALUE:      "CALLVALUE",
   269  	CALLDATALOAD:   "CALLDATALOAD",
   270  	CALLDATASIZE:   "CALLDATASIZE",
   271  	CALLDATACOPY:   "CALLDATACOPY",
   272  	CODESIZE:       "CODESIZE",
   273  	CODECOPY:       "CODECOPY",
   274  	GASPRICE:       "GASPRICE",
   275  	EXTCODESIZE:    "EXTCODESIZE",
   276  	EXTCODECOPY:    "EXTCODECOPY",
   277  	RETURNDATASIZE: "RETURNDATASIZE",
   278  	RETURNDATACOPY: "RETURNDATACOPY",
   279  	EXTCODEHASH:    "EXTCODEHASH",
   280  
   281  //0x40范围-块操作。
   282  	BLOCKHASH:  "BLOCKHASH",
   283  	COINBASE:   "COINBASE",
   284  	TIMESTAMP:  "TIMESTAMP",
   285  	NUMBER:     "NUMBER",
   286  	DIFFICULTY: "DIFFICULTY",
   287  	GASLIMIT:   "GASLIMIT",
   288  
   289  //0x50范围-“存储”和执行。
   290  	POP: "POP",
   291  //DUP:“DUP”,
   292  //swap:“交换”,
   293  	MLOAD:    "MLOAD",
   294  	MSTORE:   "MSTORE",
   295  	MSTORE8:  "MSTORE8",
   296  	SLOAD:    "SLOAD",
   297  	SSTORE:   "SSTORE",
   298  	JUMP:     "JUMP",
   299  	JUMPI:    "JUMPI",
   300  	PC:       "PC",
   301  	MSIZE:    "MSIZE",
   302  	GAS:      "GAS",
   303  	JUMPDEST: "JUMPDEST",
   304  
   305  //0x60范围-推。
   306  	PUSH1:  "PUSH1",
   307  	PUSH2:  "PUSH2",
   308  	PUSH3:  "PUSH3",
   309  	PUSH4:  "PUSH4",
   310  	PUSH5:  "PUSH5",
   311  	PUSH6:  "PUSH6",
   312  	PUSH7:  "PUSH7",
   313  	PUSH8:  "PUSH8",
   314  	PUSH9:  "PUSH9",
   315  	PUSH10: "PUSH10",
   316  	PUSH11: "PUSH11",
   317  	PUSH12: "PUSH12",
   318  	PUSH13: "PUSH13",
   319  	PUSH14: "PUSH14",
   320  	PUSH15: "PUSH15",
   321  	PUSH16: "PUSH16",
   322  	PUSH17: "PUSH17",
   323  	PUSH18: "PUSH18",
   324  	PUSH19: "PUSH19",
   325  	PUSH20: "PUSH20",
   326  	PUSH21: "PUSH21",
   327  	PUSH22: "PUSH22",
   328  	PUSH23: "PUSH23",
   329  	PUSH24: "PUSH24",
   330  	PUSH25: "PUSH25",
   331  	PUSH26: "PUSH26",
   332  	PUSH27: "PUSH27",
   333  	PUSH28: "PUSH28",
   334  	PUSH29: "PUSH29",
   335  	PUSH30: "PUSH30",
   336  	PUSH31: "PUSH31",
   337  	PUSH32: "PUSH32",
   338  
   339  	DUP1:  "DUP1",
   340  	DUP2:  "DUP2",
   341  	DUP3:  "DUP3",
   342  	DUP4:  "DUP4",
   343  	DUP5:  "DUP5",
   344  	DUP6:  "DUP6",
   345  	DUP7:  "DUP7",
   346  	DUP8:  "DUP8",
   347  	DUP9:  "DUP9",
   348  	DUP10: "DUP10",
   349  	DUP11: "DUP11",
   350  	DUP12: "DUP12",
   351  	DUP13: "DUP13",
   352  	DUP14: "DUP14",
   353  	DUP15: "DUP15",
   354  	DUP16: "DUP16",
   355  
   356  	SWAP1:  "SWAP1",
   357  	SWAP2:  "SWAP2",
   358  	SWAP3:  "SWAP3",
   359  	SWAP4:  "SWAP4",
   360  	SWAP5:  "SWAP5",
   361  	SWAP6:  "SWAP6",
   362  	SWAP7:  "SWAP7",
   363  	SWAP8:  "SWAP8",
   364  	SWAP9:  "SWAP9",
   365  	SWAP10: "SWAP10",
   366  	SWAP11: "SWAP11",
   367  	SWAP12: "SWAP12",
   368  	SWAP13: "SWAP13",
   369  	SWAP14: "SWAP14",
   370  	SWAP15: "SWAP15",
   371  	SWAP16: "SWAP16",
   372  	LOG0:   "LOG0",
   373  	LOG1:   "LOG1",
   374  	LOG2:   "LOG2",
   375  	LOG3:   "LOG3",
   376  	LOG4:   "LOG4",
   377  
   378  //0xF0范围。
   379  	CREATE:       "CREATE",
   380  	CALL:         "CALL",
   381  	RETURN:       "RETURN",
   382  	CALLCODE:     "CALLCODE",
   383  	DELEGATECALL: "DELEGATECALL",
   384  	CREATE2:      "CREATE2",
   385  	STATICCALL:   "STATICCALL",
   386  	REVERT:       "REVERT",
   387  	SELFDESTRUCT: "SELFDESTRUCT",
   388  
   389  	PUSH: "PUSH",
   390  	DUP:  "DUP",
   391  	SWAP: "SWAP",
   392  }
   393  
   394  func (op OpCode) String() string {
   395  	str := opCodeToString[op]
   396  	if len(str) == 0 {
   397  		return fmt.Sprintf("Missing opcode 0x%x", int(op))
   398  	}
   399  
   400  	return str
   401  }
   402  
   403  var stringToOp = map[string]OpCode{
   404  	"STOP":           STOP,
   405  	"ADD":            ADD,
   406  	"MUL":            MUL,
   407  	"SUB":            SUB,
   408  	"DIV":            DIV,
   409  	"SDIV":           SDIV,
   410  	"MOD":            MOD,
   411  	"SMOD":           SMOD,
   412  	"EXP":            EXP,
   413  	"NOT":            NOT,
   414  	"LT":             LT,
   415  	"GT":             GT,
   416  	"SLT":            SLT,
   417  	"SGT":            SGT,
   418  	"EQ":             EQ,
   419  	"ISZERO":         ISZERO,
   420  	"SIGNEXTEND":     SIGNEXTEND,
   421  	"AND":            AND,
   422  	"OR":             OR,
   423  	"XOR":            XOR,
   424  	"BYTE":           BYTE,
   425  	"SHL":            SHL,
   426  	"SHR":            SHR,
   427  	"SAR":            SAR,
   428  	"ADDMOD":         ADDMOD,
   429  	"MULMOD":         MULMOD,
   430  	"SHA3":           SHA3,
   431  	"ADDRESS":        ADDRESS,
   432  	"BALANCE":        BALANCE,
   433  	"ORIGIN":         ORIGIN,
   434  	"CALLER":         CALLER,
   435  	"CALLVALUE":      CALLVALUE,
   436  	"CALLDATALOAD":   CALLDATALOAD,
   437  	"CALLDATASIZE":   CALLDATASIZE,
   438  	"CALLDATACOPY":   CALLDATACOPY,
   439  	"DELEGATECALL":   DELEGATECALL,
   440  	"STATICCALL":     STATICCALL,
   441  	"CODESIZE":       CODESIZE,
   442  	"CODECOPY":       CODECOPY,
   443  	"GASPRICE":       GASPRICE,
   444  	"EXTCODESIZE":    EXTCODESIZE,
   445  	"EXTCODECOPY":    EXTCODECOPY,
   446  	"RETURNDATASIZE": RETURNDATASIZE,
   447  	"RETURNDATACOPY": RETURNDATACOPY,
   448  	"EXTCODEHASH":    EXTCODEHASH,
   449  	"BLOCKHASH":      BLOCKHASH,
   450  	"COINBASE":       COINBASE,
   451  	"TIMESTAMP":      TIMESTAMP,
   452  	"NUMBER":         NUMBER,
   453  	"DIFFICULTY":     DIFFICULTY,
   454  	"GASLIMIT":       GASLIMIT,
   455  	"POP":            POP,
   456  	"MLOAD":          MLOAD,
   457  	"MSTORE":         MSTORE,
   458  	"MSTORE8":        MSTORE8,
   459  	"SLOAD":          SLOAD,
   460  	"SSTORE":         SSTORE,
   461  	"JUMP":           JUMP,
   462  	"JUMPI":          JUMPI,
   463  	"PC":             PC,
   464  	"MSIZE":          MSIZE,
   465  	"GAS":            GAS,
   466  	"JUMPDEST":       JUMPDEST,
   467  	"PUSH1":          PUSH1,
   468  	"PUSH2":          PUSH2,
   469  	"PUSH3":          PUSH3,
   470  	"PUSH4":          PUSH4,
   471  	"PUSH5":          PUSH5,
   472  	"PUSH6":          PUSH6,
   473  	"PUSH7":          PUSH7,
   474  	"PUSH8":          PUSH8,
   475  	"PUSH9":          PUSH9,
   476  	"PUSH10":         PUSH10,
   477  	"PUSH11":         PUSH11,
   478  	"PUSH12":         PUSH12,
   479  	"PUSH13":         PUSH13,
   480  	"PUSH14":         PUSH14,
   481  	"PUSH15":         PUSH15,
   482  	"PUSH16":         PUSH16,
   483  	"PUSH17":         PUSH17,
   484  	"PUSH18":         PUSH18,
   485  	"PUSH19":         PUSH19,
   486  	"PUSH20":         PUSH20,
   487  	"PUSH21":         PUSH21,
   488  	"PUSH22":         PUSH22,
   489  	"PUSH23":         PUSH23,
   490  	"PUSH24":         PUSH24,
   491  	"PUSH25":         PUSH25,
   492  	"PUSH26":         PUSH26,
   493  	"PUSH27":         PUSH27,
   494  	"PUSH28":         PUSH28,
   495  	"PUSH29":         PUSH29,
   496  	"PUSH30":         PUSH30,
   497  	"PUSH31":         PUSH31,
   498  	"PUSH32":         PUSH32,
   499  	"DUP1":           DUP1,
   500  	"DUP2":           DUP2,
   501  	"DUP3":           DUP3,
   502  	"DUP4":           DUP4,
   503  	"DUP5":           DUP5,
   504  	"DUP6":           DUP6,
   505  	"DUP7":           DUP7,
   506  	"DUP8":           DUP8,
   507  	"DUP9":           DUP9,
   508  	"DUP10":          DUP10,
   509  	"DUP11":          DUP11,
   510  	"DUP12":          DUP12,
   511  	"DUP13":          DUP13,
   512  	"DUP14":          DUP14,
   513  	"DUP15":          DUP15,
   514  	"DUP16":          DUP16,
   515  	"SWAP1":          SWAP1,
   516  	"SWAP2":          SWAP2,
   517  	"SWAP3":          SWAP3,
   518  	"SWAP4":          SWAP4,
   519  	"SWAP5":          SWAP5,
   520  	"SWAP6":          SWAP6,
   521  	"SWAP7":          SWAP7,
   522  	"SWAP8":          SWAP8,
   523  	"SWAP9":          SWAP9,
   524  	"SWAP10":         SWAP10,
   525  	"SWAP11":         SWAP11,
   526  	"SWAP12":         SWAP12,
   527  	"SWAP13":         SWAP13,
   528  	"SWAP14":         SWAP14,
   529  	"SWAP15":         SWAP15,
   530  	"SWAP16":         SWAP16,
   531  	"LOG0":           LOG0,
   532  	"LOG1":           LOG1,
   533  	"LOG2":           LOG2,
   534  	"LOG3":           LOG3,
   535  	"LOG4":           LOG4,
   536  	"CREATE":         CREATE,
   537  	"CREATE2":        CREATE2,
   538  	"CALL":           CALL,
   539  	"RETURN":         RETURN,
   540  	"CALLCODE":       CALLCODE,
   541  	"REVERT":         REVERT,
   542  	"SELFDESTRUCT":   SELFDESTRUCT,
   543  }
   544  
   545  //stringtoop查找名称存储在“str”中的操作码。
   546  func StringToOp(str string) OpCode {
   547  	return stringToOp[str]
   548  }