-
Notifications
You must be signed in to change notification settings - Fork 187
/
evm.fe
340 lines (254 loc) · 8.27 KB
/
evm.fe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
use ingot::buf::{MemoryBuffer, RawCallBuffer}
// Basic context accessor functions.
pub unsafe fn chain_id() -> u256 {
return __chainid()
}
pub unsafe fn base_fee() -> u256 {
return __basefee()
}
pub unsafe fn origin() -> address {
return address(__origin())
}
pub unsafe fn gas_price() -> u256 {
return __gasprice()
}
pub unsafe fn gas_limit() -> u256 {
return __gaslimit()
}
pub unsafe fn gas_remaining() -> u256 {
return __gas()
}
pub unsafe fn block_hash(_ b: u256) -> u256 {
return __blockhash(b)
}
pub unsafe fn coinbase() -> address {
return address(__coinbase())
}
pub unsafe fn timestamp() -> u256 {
return __timestamp()
}
pub unsafe fn block_number() -> u256 {
return __number()
}
pub unsafe fn prevrandao() -> u256 {
return __prevrandao()
}
pub unsafe fn self_address() -> address {
return address(__address())
}
pub unsafe fn balance_of(_ addr: address) -> u256 {
return __balance(u256(addr))
}
pub unsafe fn balance() -> u256 {
return __selfbalance()
}
pub unsafe fn caller() -> address {
return address(__caller())
}
pub unsafe fn call_value() -> u256 {
return __callvalue()
}
// Overflowing math ops. Should these be unsafe or named
// `overflowing_add`, etc?
pub fn add(_ x: u256, _ y: u256) -> u256 {
unsafe { return __add(x, y) }
}
pub fn sub(_ x: u256, _ y: u256) -> u256 {
unsafe { return __sub(x, y) }
}
pub fn mul(_ x: u256, _ y: u256) -> u256 {
unsafe { return __mul(x, y) }
}
pub fn div(_ x: u256, _ y: u256) -> u256 {
unsafe { return __div(x, y) }
}
pub fn sdiv(_ x: u256, _ y: u256) -> u256 {
unsafe { return __sdiv(x, y) }
}
pub fn mod(_ x: u256, _ y: u256) -> u256 {
unsafe { return __mod(x, y) }
}
pub fn smod(_ x: u256, _ y: u256) -> u256 {
unsafe { return __smod(x, y) }
}
pub fn exp(_ x: u256, _ y: u256) -> u256 {
unsafe { return __exp(x, y) }
}
pub fn addmod(_ x: u256, _ y: u256, _ m: u256) -> u256 {
unsafe { return __addmod(x, y, m) }
}
pub fn mulmod(_ x: u256, _ y: u256, _ m: u256) -> u256 {
unsafe { return __mulmod(x, y, m) }
}
pub fn sign_extend(_ i: u256, _ x: u256) -> u256 {
unsafe { return __signextend(i, x) }
}
// Comparison ops
// TODO: return bool (see issue //653)
pub fn lt(_ x: u256, _ y: u256) -> u256 {
unsafe { return __lt(x, y) }
}
pub fn gt(_ x: u256, _ y: u256) -> u256 {
unsafe { return __gt(x, y) }
}
pub fn slt(_ x: u256, _ y: u256) -> u256 {
unsafe { return __slt(x, y) }
}
pub fn sgt(_ x: u256, _ y: u256) -> u256 {
unsafe { return __sgt(x, y) }
}
pub fn eq(_ x: u256, _ y: u256) -> u256 {
unsafe { return __eq(x, y) }
}
pub fn is_zero(_ x: u256) -> u256 {
unsafe { return __iszero(x) }
}
// Bitwise ops
pub fn bitwise_and(_ x: u256, _ y: u256) -> u256 {
unsafe { return __and(x, y) }
}
pub fn bitwise_or(_ x: u256, _ y: u256) -> u256 {
unsafe { return __or(x, y) }
}
pub fn bitwise_not(_ x: u256) -> u256 {
unsafe { return __not(x) }
}
pub fn xor(_ x: u256, _ y: u256) -> u256 {
unsafe { return __xor(x, y) }
}
pub fn byte(offset: u256, value: u256) -> u256 {
unsafe { return __byte(offset, value) }
}
pub fn shl(bits: u256, value: u256) -> u256 {
unsafe { return __shl(bits, value) }
}
pub fn shr(bits: u256, value: u256) -> u256 {
unsafe { return __shr(bits, value) }
}
pub fn sar(bits: u256, value: u256) -> u256 {
unsafe { return __sar(bits, value) }
}
// Evm state access and control
pub fn return_mem(buf: MemoryBuffer) {
unsafe{ __return(buf.offset(), buf.len()) }
}
pub fn revert_mem(buf: MemoryBuffer) {
unsafe { __revert(buf.offset(), buf.len()) }
}
pub unsafe fn selfdestruct(_ addr: address) {
__selfdestruct(u256(addr))
}
// Invalid opcode. Equivalent to revert(0, 0),
// except that all remaining gas in the current context
// is consumed.
pub unsafe fn invalid() {
__invalid()
}
pub unsafe fn stop() {
__stop()
}
pub unsafe fn pc() -> u256 {
return __pc()
}
// TODO: dunno if we should enable this
// pub unsafe fn pop(_ x: u256) {
// return __pop(x)
// }
pub unsafe fn mload(offset p: u256) -> u256 {
return __mload(p)
}
pub unsafe fn mstore(offset p: u256, value v: u256) {
__mstore(p, v)
}
pub unsafe fn mstore8(offset p: u256, value v: u256) {
__mstore8(p, v)
}
pub unsafe fn sload(offset p: u256) -> u256 {
return __sload(p)
}
pub unsafe fn sstore(offset p: u256, value v: u256) {
__sstore(p, v)
}
pub unsafe fn msize() -> u256 {
return __msize()
}
pub unsafe fn call_data_load(offset p: u256) -> u256 {
return __calldataload(p)
}
pub unsafe fn call_data_size() -> u256 {
return __calldatasize()
}
pub fn call_data_copy(mut buf: MemoryBuffer, from_offset f: u256) {
unsafe { __calldatacopy(buf.offset(), f, buf.len()) }
}
pub unsafe fn code_size() -> u256 {
return __codesize()
}
pub fn code_copy(mut buf: MemoryBuffer, from_offset f: u256) {
unsafe { __codecopy(buf.offset(), f, buf.len()) }
}
pub unsafe fn return_data_size() -> u256 {
return __returndatasize()
}
pub fn return_data_copy(mut buf: MemoryBuffer, from_offset f: u256) {
unsafe { __returndatacopy(buf.offset(), f, buf.len()) }
}
pub unsafe fn extcodesize(_ addr: address) -> u256 {
return __extcodesize(u256(addr))
}
pub unsafe fn ext_code_copy(addr: address, mut buf: MemoryBuffer, from_offset f: u256) {
__extcodecopy(u256(addr), buf.offset(), f, buf.len())
}
pub unsafe fn ext_code_hash(_ addr: address) -> u256 {
return __extcodehash(u256(addr))
}
pub fn keccak256_mem(buf: MemoryBuffer) -> u256 {
unsafe { return __keccak256(buf.offset(), buf.len()) }
}
// Contract creation and calling
pub fn create(value v: u256, buf: MemoryBuffer) -> address {
unsafe { return address(__create(v, buf.offset(), buf.len())) }
}
pub fn create2(value v: u256, buf: MemoryBuffer, salt s: u256) -> address {
unsafe { return address(__create2(v, buf.offset(), buf.len(), s)) }
}
pub fn call(gas: u256, addr: address, value: u256, mut buf: RawCallBuffer) -> bool {
unsafe { return __call(gas, u256(addr), value, buf.offset(), buf.input_len(), buf.offset(), buf.output_len()) == 1 }
}
pub fn call_code(gas: u256, addr: address, value: u256, mut buf: RawCallBuffer) -> bool {
unsafe { return __callcode(gas, u256(addr), value, buf.offset(), buf.input_len(), buf.offset(), buf.output_len()) == 1 }
}
pub fn delegate_call(gas: u256, addr: address, mut buf: RawCallBuffer) -> bool {
unsafe { return __delegatecall(gas, u256(addr), buf.offset(), buf.input_len(), buf.offset(), buf.output_len()) == 1 }
}
pub fn static_call(gas: u256, addr: address, mut buf: RawCallBuffer) -> bool {
unsafe { return __staticcall(gas, u256(addr), buf.offset(), buf.input_len(), buf.offset(), buf.output_len()) == 1 }
}
pub unsafe fn call_2(gas: u256, addr: address, value: u256, input_offset: u256, input_len: u256, output_offset: u256, output_len: u256) -> bool {
return __call(gas, u256(addr), value, input_offset, input_len, output_offset, output_len) == 1
}
pub unsafe fn call_code_2(gas: u256, addr: address, value: u256, input_offset: u256, input_len: u256, output_offset: u256, output_len: u256) -> bool {
return __callcode(gas, u256(addr), value, input_offset, input_len, output_offset, output_len) == 1
}
pub unsafe fn delegate_call_2(gas: u256, addr: address, input_offset: u256, input_len: u256, output_offset: u256, output_len: u256) -> bool {
return __delegatecall(gas, u256(addr), input_offset, input_len, output_offset, output_len) == 1
}
pub unsafe fn static_call_2(gas: u256, addr: address, input_offset: u256, input_len: u256, output_offset: u256, output_len: u256) -> bool {
return __staticcall(gas, u256(addr), input_offset, input_len, output_offset, output_len) == 1
}
// Logging functions
pub fn log0(buf: MemoryBuffer) {
unsafe { return __log0(buf.offset(), buf.len()) }
}
pub fn log1(buf: MemoryBuffer, topic1 t1: u256) {
unsafe { return __log1(buf.offset(), buf.len(), t1) }
}
pub fn log2(buf: MemoryBuffer, topic1 t1: u256, topic2 t2: u256) {
unsafe { return __log2(buf.offset(), buf.len(), t1, t2) }
}
pub fn log3(buf: MemoryBuffer, topic1 t1: u256, topic2 t2: u256, topic3 t3: u256) {
unsafe { return __log3(buf.offset(), buf.len(), t1, t2, t3) }
}
pub fn log4(buf: MemoryBuffer, topic1 t1: u256, topic2 t2: u256, topic3 t3: u256, topic4 t4: u256) {
unsafe { return __log4(buf.offset(), buf.len(), t1, t2, t3, t4) }
}