-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathevm.huff
1072 lines (745 loc) · 13.6 KB
/
evm.huff
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define macro MAIN() = takes (0) returns (0) {
0x00 msize mstore
start:
// load pc2 + 1 (stored in msg.data[0:32])
0x00 mload
0x01 add
// load msg.data.length
calldatasize
// goto label end if (pc2 + 1 > msg.data.length)
lt end jumpi
// pc2 < msg.data.length, continue loop
// load pc2
0x00 mload
// load bytecode[pc2] by (msg.data[pc2:pc2+32] >> 248)
calldataload 0xf8 shr
// stub is at (pc * 6) + stub_offset
0x06 mul
// add jump-table start offset aka stub_offset
op_00 add
// jump to opcode implementation
jump
op_00:
stop
invalid invalid invalid invalid
op_01:
add next jump
op_02:
mul next jump
op_03:
sub next jump
op_04:
div next jump
op_05:
sdiv next jump
op_06:
mod next jump
op_07:
smod next jump
op_08:
addmod next jump
op_09:
mulmod next jump
op_0a:
exp next jump
op_0b:
signextend next jump
op_0c:
invalid invalid invalid invalid invalid
op_0d:
invalid invalid invalid invalid invalid
op_0e:
invalid invalid invalid invalid invalid
op_0f:
invalid invalid invalid invalid invalid
op_10:
lt next jump
op_11:
gt next jump
op_12:
slt next jump
op_13:
sgt next jump
op_14:
eq next jump
op_15:
iszero next jump
op_16:
and next jump
op_17:
or next jump
op_18:
xor next jump
op_19:
not next jump
op_1a:
byte next jump
op_1b:
shl next jump
op_1c:
shr next jump
op_1d:
sar next jump
op_1e:
invalid invalid invalid invalid invalid
op_1f:
invalid invalid invalid invalid invalid
op_20:
impl_sha3 jump
invalid
op_21:
invalid invalid invalid invalid invalid
op_22:
invalid invalid invalid invalid invalid
op_23:
invalid invalid invalid invalid invalid
op_24:
invalid invalid invalid invalid invalid
op_25:
invalid invalid invalid invalid invalid
op_26:
invalid invalid invalid invalid invalid
op_27:
invalid invalid invalid invalid invalid
op_28:
invalid invalid invalid invalid invalid
op_29:
invalid invalid invalid invalid invalid
op_2a:
invalid invalid invalid invalid invalid
op_2b:
invalid invalid invalid invalid invalid
op_2c:
invalid invalid invalid invalid invalid
op_2d:
invalid invalid invalid invalid invalid
op_2e:
invalid invalid invalid invalid invalid
op_2f:
invalid invalid invalid invalid invalid
op_30:
address next jump
op_31:
balance next jump
op_32:
origin next jump
op_33:
caller next jump
op_34:
callvalue next jump
op_35:
// (requires an additional param for calldata which is annoying but essential)
calldataload next jump
op_36:
calldatasize next jump
op_37:
// TODO: restore implementation.. :( will need to init with different code, or use sload or something :\
calldatacopy next jump
op_38:
calldatasize next jump
op_39:
calldatacopy next jump
op_3a:
gasprice next jump
op_3b:
extcodesize next jump
op_3c:
extcodecopy next jump
op_3d:
returndatasize next jump
op_3e:
returndatacopy next jump
op_3f:
extcodehash next jump
op_40:
blockhash next jump
op_41:
coinbase next jump
op_42:
timestamp next jump
op_43:
number next jump
op_44:
difficulty next jump
op_45:
gaslimit next jump
op_46:
chainid next jump
op_47:
selfbalance next jump
op_48:
basefee next jump
op_49:
invalid invalid invalid invalid invalid
op_4a:
invalid invalid invalid invalid invalid
op_4b:
invalid invalid invalid invalid invalid
op_4c:
invalid invalid invalid invalid invalid
op_4d:
invalid invalid invalid invalid invalid
op_4e:
invalid invalid invalid invalid invalid
op_4f:
invalid invalid invalid invalid invalid
op_50:
pop next jump
op_51:
impl_mload jump
invalid
op_52:
impl_mstore jump
invalid
op_53:
impl_mstore8 jump
invalid
op_54:
sload next jump
op_55:
sstore next jump
op_56:
impl_jump jump
invalid
op_57:
impl_jumpi jump
invalid
op_58:
impl_pc jump
invalid
op_59:
impl_msize jump
invalid
op_5a:
gas next jump
op_5b:
next jump
invalid // save gas instead of executing jumpdest twice
op_5c:
invalid invalid invalid invalid invalid
op_5d:
invalid invalid invalid invalid invalid
op_5e:
invalid invalid invalid invalid invalid
op_5f:
invalid invalid invalid invalid invalid
op_60:
impl_push jump
invalid
op_61:
impl_push jump
invalid
op_62:
impl_push jump
invalid
op_63:
impl_push jump
invalid
op_64:
impl_push jump
invalid
op_65:
impl_push jump
invalid
op_66:
impl_push jump
invalid
op_67:
impl_push jump
invalid
op_68:
impl_push jump
invalid
op_69:
impl_push jump
invalid
op_6a:
impl_push jump
invalid
op_6b:
impl_push jump
invalid
op_6c:
impl_push jump
invalid
op_6d:
impl_push jump
invalid
op_6e:
impl_push jump
invalid
op_6f:
impl_push jump
invalid
op_70:
impl_push jump
invalid
op_71:
impl_push jump
invalid
op_72:
impl_push jump
invalid
op_73:
impl_push jump
invalid
op_74:
impl_push jump
invalid
op_75:
impl_push jump
invalid
op_76:
impl_push jump
invalid
op_77:
impl_push jump
invalid
op_78:
impl_push jump
invalid
op_79:
impl_push jump
invalid
op_7a:
impl_push jump
invalid
op_7b:
impl_push jump
invalid
op_7c:
impl_push jump
invalid
op_7d:
impl_push jump
invalid
op_7e:
impl_push jump
invalid
op_7f:
impl_push jump
invalid
op_80:
dup1 next jump
op_81:
dup2 next jump
op_82:
dup3 next jump
op_83:
dup4 next jump
op_84:
dup5 next jump
op_85:
dup6 next jump
op_86:
dup7 next jump
op_87:
dup8 next jump
op_88:
dup9 next jump
op_89:
dup10 next jump
op_8a:
dup11 next jump
op_8b:
dup12 next jump
op_8c:
dup13 next jump
op_8d:
dup14 next jump
op_8e:
dup15 next jump
op_8f:
dup16 next jump
op_90:
swap1 next jump
op_91:
swap2 next jump
op_92:
swap3 next jump
op_93:
swap4 next jump
op_94:
swap5 next jump
op_95:
swap6 next jump
op_96:
swap7 next jump
op_97:
swap8 next jump
op_98:
swap9 next jump
op_99:
swap10 next jump
op_9a:
swap11 next jump
op_9b:
swap12 next jump
op_9c:
swap13 next jump
op_9d:
swap14 next jump
op_9e:
swap15 next jump
op_9f:
swap16 next jump
op_a0:
impl_log0 jump
invalid
op_a1:
impl_log1 jump
invalid
op_a2:
impl_log2 jump
invalid
op_a3:
impl_log3 jump
invalid
op_a4:
impl_log4 jump
invalid
op_a5:
invalid invalid invalid invalid invalid
op_a6:
invalid invalid invalid invalid invalid
op_a7:
invalid invalid invalid invalid invalid
op_a8:
invalid invalid invalid invalid invalid
op_a9:
invalid invalid invalid invalid invalid
op_aa:
invalid invalid invalid invalid invalid
op_ab:
invalid invalid invalid invalid invalid
op_ac:
invalid invalid invalid invalid invalid
op_ad:
invalid invalid invalid invalid invalid
op_ae:
invalid invalid invalid invalid invalid
op_af:
invalid invalid invalid invalid invalid
op_b0:
invalid invalid invalid invalid invalid
op_b1:
invalid invalid invalid invalid invalid
op_b2:
invalid invalid invalid invalid invalid
op_b3:
invalid invalid invalid invalid invalid
op_b4:
invalid invalid invalid invalid invalid
op_b5:
invalid invalid invalid invalid invalid
op_b6:
invalid invalid invalid invalid invalid
op_b7:
invalid invalid invalid invalid invalid
op_b8:
invalid invalid invalid invalid invalid
op_b9:
invalid invalid invalid invalid invalid
op_ba:
invalid invalid invalid invalid invalid
op_bb:
invalid invalid invalid invalid invalid
op_bc:
invalid invalid invalid invalid invalid
op_bd:
invalid invalid invalid invalid invalid
op_be:
invalid invalid invalid invalid invalid
op_bf:
invalid invalid invalid invalid invalid
op_c0:
invalid invalid invalid invalid invalid
op_c1:
invalid invalid invalid invalid invalid
op_c2:
invalid invalid invalid invalid invalid
op_c3:
invalid invalid invalid invalid invalid
op_c4:
invalid invalid invalid invalid invalid
op_c5:
invalid invalid invalid invalid invalid
op_c6:
invalid invalid invalid invalid invalid
op_c7:
invalid invalid invalid invalid invalid
op_c8:
invalid invalid invalid invalid invalid
op_c9:
invalid invalid invalid invalid invalid
op_ca:
invalid invalid invalid invalid invalid
op_cb:
invalid invalid invalid invalid invalid
op_cc:
invalid invalid invalid invalid invalid
op_cd:
invalid invalid invalid invalid invalid
op_ce:
invalid invalid invalid invalid invalid
op_cf:
invalid invalid invalid invalid invalid
op_d0:
invalid invalid invalid invalid invalid
op_d1:
invalid invalid invalid invalid invalid
op_d2:
invalid invalid invalid invalid invalid
op_d3:
invalid invalid invalid invalid invalid
op_d4:
invalid invalid invalid invalid invalid
op_d5:
invalid invalid invalid invalid invalid
op_d6:
invalid invalid invalid invalid invalid
op_d7:
invalid invalid invalid invalid invalid
op_d8:
invalid invalid invalid invalid invalid
op_d9:
invalid invalid invalid invalid invalid
op_da:
invalid invalid invalid invalid invalid
op_db:
invalid invalid invalid invalid invalid
op_dc:
invalid invalid invalid invalid invalid
op_dd:
invalid invalid invalid invalid invalid
op_de:
invalid invalid invalid invalid invalid
op_df:
invalid invalid invalid invalid invalid
op_e0:
invalid invalid invalid invalid invalid
op_e1:
invalid invalid invalid invalid invalid
op_e2:
invalid invalid invalid invalid invalid
op_e3:
invalid invalid invalid invalid invalid
op_e4:
invalid invalid invalid invalid invalid
op_e5:
invalid invalid invalid invalid invalid
op_e6:
invalid invalid invalid invalid invalid
op_e7:
invalid invalid invalid invalid invalid
op_e8:
invalid invalid invalid invalid invalid
op_e9:
invalid invalid invalid invalid invalid
op_ea:
invalid invalid invalid invalid invalid
op_eb:
invalid invalid invalid invalid invalid
op_ec:
invalid invalid invalid invalid invalid
op_ed:
invalid invalid invalid invalid invalid
op_ee:
invalid invalid invalid invalid invalid
op_ef:
invalid invalid invalid invalid invalid
op_f0:
impl_create jump
invalid
op_f1:
impl_call jump
invalid
op_f2:
impl_callcode jump
invalid
op_f3:
impl_return jump
invalid
op_f4:
impl_delegatecall jump
invalid
op_f5:
impl_create2 jump
invalid
op_f6:
invalid invalid invalid invalid invalid
op_f7:
invalid invalid invalid invalid invalid
op_f8:
invalid invalid invalid invalid invalid
op_f9:
invalid invalid invalid invalid invalid
op_fa:
impl_staticcall jump
invalid
op_fb:
invalid invalid invalid invalid invalid
op_fc:
invalid invalid invalid invalid invalid
op_fd:
impl_revert jump
invalid
op_fe:
invalid invalid invalid invalid invalid
op_ff:
// selfdestruct, implemented as stop
stop
invalid invalid invalid invalid
impl_sha3:
// +0x20 for offset (top of stack)
0x20 add sha3
next jump
impl_mload:
// top stack has offset; skip first word (reserved)
0x20 add mload
next jump
impl_mstore:
0x20 add mstore
next jump
impl_mstore8:
0x20 add mstore8
next jump
impl_jump:
// check if target is jumpdest (0x5b)
dup1
calldataload 0xf8 shr
0x5b sub
impl_bad_jump jumpi
0x00 mstore
start jump // skip advancing pc again
impl_jumpi:
// top of stack is the condition
// so push impl_jump and conditionally go there
impl_jump jumpi
// otherwise advance normally
next jump
impl_bad_jump:
// trick to jump into an invalid jumpdest for nicer output
// (although might want to revert here or something)
pc jump
invalid
impl_pc:
0x00 mload
next jump
impl_msize:
// top stack has offset; skip first word (reserved)
0x20 msize sub
next jump
impl_push:
// PUSHx implementation:
// load pc2
0x00 mload
// [pc2]
// for the sub 0x5f later
0x5f
// [pc2 | 0x5f]
// dup pc2 to advance it later
dup2
// [pc2 | 0x5f | pc2]
// (msg.data[pc2] - 0x5f) << 8 to get number of bytes
calldataload 0xf8 shr
sub
// [pc2 | size bytes]
// dup size to advance pc2 later
dup1
// [pc2 | size bytes | size bytes]
// swap1 can be optimized out but kept for sake of code clarity (LOL)
0x04
swap1
shl
// [pc2 | size bytes | size bits]
// 256 - num_bytes
0x100 sub
// [pc2 | size bytes | 256 - size bits]
// load pc2 + 1
dup3
0x01 add
// [pc2 | size bytes | 256 - size bits | pc2 + 1]
// load push data
calldataload
// [pc2 | size bytes | 256 - size bits | push data bytes32]
// msg.data[pc2 + 1:pc2 + 1 + 32] >> size_bits
swap1
shr
// [pc2 | size bytes | push data bytesX]
// advance pc2 by amount of bytes pushed
swap2
// [push data bytesX | size bytes | pc2]
add
// [push data bytesX | size bytes + pc2]
0x00 mstore
// [push data bits]
next jump
impl_log0:
0x20 add log0
next jump
impl_log1:
0x20 add log1
next jump
impl_log2:
0x20 add log2
next jump
impl_log3:
0x20 add log3
next jump
impl_log4:
0x20 add log4
next jump
impl_call: