Fabcoin Core  0.16.2
P2P Digital Currency
Instruction.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU 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  cpp-ethereum 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 General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #pragma once
23 
24 #include <functional>
25 #include <libdevcore/Common.h>
26 #include <libdevcore/Assertions.h>
27 #include "Exceptions.h"
28 
29 #ifdef MSIZE
30 #undef MSIZE
31 #endif
32 
33 namespace dev
34 {
35 namespace eth
36 {
37 
39 enum class Instruction: uint8_t
40 {
41  STOP = 0x00,
42  ADD,
43  MUL,
44  SUB,
45  DIV,
46  SDIV,
47  MOD,
48  SMOD,
49  ADDMOD,
50  MULMOD,
51  EXP,
52  SIGNEXTEND,
53 
54  LT = 0x10,
55  GT,
56  SLT,
57  SGT,
58  EQ,
59  ISZERO,
60  AND,
61  OR,
62  XOR,
63  NOT,
64  BYTE,
65 
66  SHA3 = 0x20,
67 
68  ADDRESS = 0x30,
69  BALANCE,
70  ORIGIN,
71  CALLER,
72  CALLVALUE,
73  CALLDATALOAD,
74  CALLDATASIZE,
75  CALLDATACOPY,
76  CODESIZE,
77  CODECOPY,
78  GASPRICE,
79  EXTCODESIZE,
80  EXTCODECOPY,
81 
82  BLOCKHASH = 0x40,
83  COINBASE,
84  TIMESTAMP,
85  NUMBER,
86  DIFFICULTY,
87  GASLIMIT,
88 
89  JUMPTO = 0x4a,
90  JUMPIF,
91  JUMPV,
92  JUMPSUB,
93  JUMPSUBV,
94  RETURNSUB,
95 
96  POP = 0x50,
97  MLOAD,
98  MSTORE,
99  MSTORE8,
100  SLOAD,
101  SSTORE,
102  JUMP,
103  JUMPI,
104  PC,
105  MSIZE,
106  GAS,
107  JUMPDEST,
108  BEGINSUB,
109  BEGINDATA,
110 
111  PUSH1 = 0x60,
112  PUSH2,
113  PUSH3,
114  PUSH4,
115  PUSH5,
116  PUSH6,
117  PUSH7,
118  PUSH8,
119  PUSH9,
120  PUSH10,
121  PUSH11,
122  PUSH12,
123  PUSH13,
124  PUSH14,
125  PUSH15,
126  PUSH16,
127  PUSH17,
128  PUSH18,
129  PUSH19,
130  PUSH20,
131  PUSH21,
132  PUSH22,
133  PUSH23,
134  PUSH24,
135  PUSH25,
136  PUSH26,
137  PUSH27,
138  PUSH28,
139  PUSH29,
140  PUSH30,
141  PUSH31,
142  PUSH32,
143 
144  DUP1 = 0x80,
145  DUP2,
146  DUP3,
147  DUP4,
148  DUP5,
149  DUP6,
150  DUP7,
151  DUP8,
152  DUP9,
153  DUP10,
154  DUP11,
155  DUP12,
156  DUP13,
157  DUP14,
158  DUP15,
159  DUP16,
160 
161  SWAP1 = 0x90,
162  SWAP2,
163  SWAP3,
164  SWAP4,
165  SWAP5,
166  SWAP6,
167  SWAP7,
168  SWAP8,
169  SWAP9,
170  SWAP10,
171  SWAP11,
172  SWAP12,
173  SWAP13,
174  SWAP14,
175  SWAP15,
176  SWAP16,
177 
178  LOG0 = 0xa0,
179  LOG1,
180  LOG2,
181  LOG3,
182  LOG4,
183 
184  // these are generated by the interpreter - should never be in user code
185  PUSHC = 0xac,
186  JUMPC,
187  JUMPCI,
188  BAD,
189 
190  CREATE = 0xf0,
191  CALL,
192  CALLCODE,
193  RETURN,
194  DELEGATECALL,
195  SUICIDE = 0xff
196 };
197 
199 inline unsigned getPushNumber(Instruction _inst)
200 {
201  return (byte)_inst - unsigned(Instruction::PUSH1) + 1;
202 }
203 
205 inline unsigned getDupNumber(Instruction _inst)
206 {
207  return (byte)_inst - unsigned(Instruction::DUP1) + 1;
208 }
209 
211 inline unsigned getSwapNumber(Instruction _inst)
212 {
213  return (byte)_inst - unsigned(Instruction::SWAP1) + 1;
214 }
215 
217 inline Instruction pushInstruction(unsigned _number)
218 {
219  assertThrow(1 <= _number && _number <= 32, InvalidOpcode, "Invalid PUSH instruction requested.");
220  return Instruction(unsigned(Instruction::PUSH1) + _number - 1);
221 }
222 
224 inline Instruction dupInstruction(unsigned _number)
225 {
226  assertThrow(1 <= _number && _number <= 16, InvalidOpcode, "Invalid DUP instruction requested.");
227  return Instruction(unsigned(Instruction::DUP1) + _number - 1);
228 }
229 
231 inline Instruction swapInstruction(unsigned _number)
232 {
233  assertThrow(1 <= _number && _number <= 16, InvalidOpcode, "Invalid SWAP instruction requested.");
234  return Instruction(unsigned(Instruction::SWAP1) + _number - 1);
235 }
236 
238 inline Instruction logInstruction(unsigned _number)
239 {
240  assertThrow(_number <= 4, InvalidOpcode, "Invalid LOG instruction requested.");
241  return Instruction(unsigned(Instruction::LOG0) + _number);
242 }
243 
244 enum class Tier : unsigned
245 {
246  Zero = 0, // 0, Zero
247  Base, // 2, Quick
248  VeryLow, // 3, Fastest
249  Low, // 5, Fast
250  Mid, // 8, Mid
251  High, // 10, Slow
252  Ext, // 20, Ext
253  Special, // multiparam or otherwise special
254  Invalid // Invalid.
255 };
256 
259 {
260  std::string name;
262  int args;
263  int ret;
264  bool sideEffects;
266 };
267 
270 
272 bool isValidInstruction(Instruction _inst);
273 
275 extern const std::map<std::string, Instruction> c_instructions;
276 
278 void eachInstruction(bytes const& _mem, std::function<void(Instruction,u256 const&)> const& _onInstruction);
279 
281 std::string disassemble(bytes const& _mem);
282 
283 }
284 }
285 
signed greater-than comparision
set a potential jumpsub destination
place 17 byte item on stack
bitwise OR operation
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
exponential operation
unsigned modular addition
void eachInstruction(bytes const &_mem, std::function< void(Instruction, u256 const &)> const &_onInstruction)
Iterate through EVM code and call a function on each instruction.
place 10 byte item on stack
copies the 14th highest item in the stack to the top of the stack
get external code size (from another contract)
#define function(a, b, c, d, k, s)
place 28 byte item on stack
return to subroutine jumped from
uint8_t byte
Definition: Common.h:57
int args
Number of items required on the stack for this instruction (and, for the purposes of ret...
Definition: Instruction.h:262
place 19 byte item on stack
unsigned modular multiplication
get the block&#39;s timestamp
place 1 byte item on stack
place 27 byte item on stack
place 31 byte item on stack
conditionally alter the program counter
SHA3 message digest base class.
Definition: sha3.h:28
place 13 byte item on stack
unsigned getSwapNumber(Instruction _inst)
Definition: Instruction.h:211
alter the program counter to a beginsub
copy input data in current environment to memory
conditionally alter the program counter
place 2 byte item on stack
addition operation
retrieve single byte from word
place 6 byte item on stack
place 20 byte item on stack
swaps the highest and 6th highest value on the stack
swaps the highest and 9th highest value on the stack
get execution origination address
Makes a log entry; 4 topics.
get the block&#39;s number
swaps the highest and 15th highest value on the stack
unsigned getDupNumber(Instruction _inst)
Definition: Instruction.h:205
swaps the highest and 11th highest value on the stack
swaps the highest and 13th highest value on the stack
integer division operation
copies the 9th highest item in the stack to the top of the stack
swaps the highest and 17th highest value on the stack
get the block&#39;s coinbase address
set a potential jump destination
swaps the highest and 7th highest value on the stack
get input data of current environment
place 30 byte item on stack
place 12 byte item on stack
save byte to memory
copies the 5th highest item in the stack to the top of the stack
halt execution and register account for later deletion
place 8 byte item on stack
swaps the highest and third highest value on the stack
signed integer division operation
load word from storage
swaps the highest and 4th highest value on the stack
copies the 16th highest item in the stack to the top of the stack
place 29 byte item on stack
signed less-than comparision
InstructionInfo instructionInfo(Instruction _inst)
Information on all the instructions.
halt execution returning output data
conditionally alter the program counter - pre-verified
copies the 10th highest item in the stack to the top of the stack
get address of currently executing account
place 25 byte item on stack
place 26 byte item on stack
remove item from stack
get hash of most recent complete block
alter the program counter - pre-verified
#define assertThrow(_condition, _ExceptionType, _description)
Assertion that throws an exception containing the given description if it is not met.
Definition: Assertions.h:75
bool isValidInstruction(Instruction _inst)
check whether instructions exists
Instruction logInstruction(unsigned _number)
Definition: Instruction.h:238
get the program counter
copies the 6th highest item in the stack to the top of the stack
copies the third highest item in the stack to the top of the stack
swaps the highest and 14th highest value on the stack
greater-than comparision
std::string name
The name of the instruction.
Definition: Instruction.h:260
get size of code running in current environment
unsigned getPushNumber(Instruction _inst)
Definition: Instruction.h:199
place 16 byte item on stack
bool sideEffects
false if the only effect on the execution environment (apart from gas usage) is a change to a topmost...
Definition: Instruction.h:264
int ret
Number of items placed (back) on the stack by this instruction, assuming args items were removed...
Definition: Instruction.h:263
copies the highest item in the stack to the top of the stack
Makes a log entry; no topics.
get the size of active memory
copies the 8th highest item in the stack to the top of the stack
copies the second highest item in the stack to the top of the stack
get the block&#39;s gas limit
place 3 byte item on stack
place 11 byte item on stack
get balance of the given account
save word to storage
message-call with another account&#39;s code only
less-than comparision
equality comparision
std::vector< byte > bytes
Definition: Common.h:75
get size of input data in current environment
extend length of signed integer
place 18 byte item on stack
const std::map< std::string, Instruction > c_instructions
Convert from string mnemonic to Instruction type.
Definition: Instruction.cpp:32
copy code running in current environment to memory
place 15 byte item on stack
bitwise AND operation
copies the 11th highest item in the stack to the top of the stack
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
signed modulo remainder operation
subtraction operation
mulitplication operation
swaps the highest and 10th highest value on the stack
swaps the highest and 5th highest value on the stack
like CALLCODE but keeps caller&#39;s value and sender
swaps the highest and 16th highest value on the stack
place 23 byte item on stack
Instruction pushInstruction(unsigned _number)
Definition: Instruction.h:217
Instruction
Virtual machine bytecode instruction.
Definition: Instruction.h:39
get the amount of available gas
swaps the highest and 12th highest value on the stack
copies the 4th highest item in the stack to the top of the stack
copy external code (from another contract)
alter the program counter to a beginsub
Information structure for a particular instruction.
Definition: Instruction.h:258
get deposited value by the instruction/transaction responsible for this execution ...
swaps the highest and 8th highest value on the stack
place 32 byte item on stack
place 22 byte item on stack
place 9 byte item on stack
int additional
Additional items required in memory for this instructions (only for PUSH).
Definition: Instruction.h:261
get price of gas in current environment
alter the program counter to a jumpdest
swaps the highest and second highest value on the stack
Makes a log entry; 1 topic.
bitwise XOR operation
copies the 15th highest item in the stack to the top of the stack
place 14 byte item on stack
message-call into an account
get the block&#39;s difficulty
Instruction dupInstruction(unsigned _number)
Definition: Instruction.h:224
save word to memory
alter the program counter to a jumpdest
placed to force invalid instruction exception
place 24 byte item on stack
place 7 byte item on stack
Instruction swapInstruction(unsigned _number)
Definition: Instruction.h:231
Tier gasPriceTier
Tier for gas pricing.
Definition: Instruction.h:265
Makes a log entry; 3 topics.
alter the program counter to a jumpdest
push value from constant pool
place 5 byte item on stack
copies the 12th highest item in the stack to the top of the stack
copies the 7th highest item in the stack to the top of the stack
begine the data section
load word from memory
simple not operator
std::string disassemble(bytes const &_mem)
Convert from EVM code to simple EVM assembly language.
place 4 byte item on stack
bitwise NOT opertation
modulo remainder operation
copies the 13th highest item in the stack to the top of the stack
place 21 byte item on stack
create a new account with associated code
Makes a log entry; 2 topics.