summaryrefslogtreecommitdiff
path: root/weerun_inst.cpp
blob: f05997b7777c8c6700a6ea4c9a454fa30b7a1bbf (plain)
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
#include <cmath>
#include <iostream>

#include "weerun.hpp"
#include "weerun_jit.hpp"

#define INVALID_INST \
  { 0, 0, "illegal", nullptr, nullptr }

static void run_unreachable(wasm_instance_t* ins, byte opcode);
static void run_nop(wasm_instance_t* ins, byte opcode);
static void run_drop(wasm_instance_t* ins, byte opcode);
static void run_select(wasm_instance_t* ins, byte opcode);
static void run_local_get(wasm_instance_t* ins, byte opcode);
static void run_local_set(wasm_instance_t* ins, byte opcode);
static void run_local_tee(wasm_instance_t* ins, byte opcode);
static void run_global_get(wasm_instance_t* ins, byte opcode);
static void run_global_set(wasm_instance_t* ins, byte opcode);

#if 0
static void run_tabel_get(wasm_instance_t* ins, byte opcode);
static void run_tabel_set(wasm_instance_t* ins, byte opcode);
#endif

static void run_i32_load(wasm_instance_t* ins, byte opcode);
static void run_f64_load(wasm_instance_t* ins, byte opcode);
static void run_i32_load8_u(wasm_instance_t* ins, byte opcode);
static void run_i32_load8_s(wasm_instance_t* ins, byte opcode);
static void run_i32_load16_u(wasm_instance_t* ins, byte opcode);
static void run_i32_load16_s(wasm_instance_t* ins, byte opcode);
static void run_i32_store(wasm_instance_t* ins, byte opcode);
static void run_f64_store(wasm_instance_t* ins, byte opcode);
static void run_i32_store8(wasm_instance_t* ins, byte opcode);
static void run_i32_store16(wasm_instance_t* ins, byte opcode);
static void run_i32_const(wasm_instance_t* ins, byte opcode);
static void run_f64_const(wasm_instance_t* ins, byte opcode);
static void run_i32_numeric(wasm_instance_t* ins, byte opcode);
static void run_f64_compare(wasm_instance_t* ins, byte opcode);
static void run_f64_compute(wasm_instance_t* ins, byte opcode);
static void run_f64_convert(wasm_instance_t* ins, byte opcode);
static void run_call(wasm_instance_t* ins, byte opcode);
static void run_call_indirect(wasm_instance_t* ins, byte opcode);
static void run_return(wasm_instance_t* ins, byte opcode);
static void run_block(wasm_instance_t* ins, byte opcode);
static void run_end(wasm_instance_t* ins, byte opcode);
static void run_br(wasm_instance_t* ins, byte opcode);
static void run_br_table(wasm_instance_t* ins, byte opcode);

const struct wasm_inst_desc_t g_inst_desc[] = {
    {WASM_OP_UNREACHABLE, WASM_INST_TYPE_NOARG, "unreachable", run_unreachable,
     &wasm_jit_t::emit_unreachable},
    {WASM_OP_NOP, WASM_INST_TYPE_NOARG, "nop", run_nop, &wasm_jit_t::emit_nop},
    {WASM_OP_BLOCK, WASM_INST_TYPE_BLOCK, "block", run_block,
     &wasm_jit_t::emit_block},
    {WASM_OP_LOOP, WASM_INST_TYPE_BLOCK, "loop", run_block,
     &wasm_jit_t::emit_block},
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    {WASM_OP_END, WASM_INST_TYPE_NOARG, "end", run_end, &wasm_jit_t::emit_end},
    {WASM_OP_BR, WASM_INST_TYPE_I32, "br", run_br, &wasm_jit_t::emit_br},
    {WASM_OP_BR_IF, WASM_INST_TYPE_I32, "br_if", run_br,
     &wasm_jit_t::emit_br_if},
    {WASM_OP_BR_TABLE, WASM_INST_TYPE_BRTABLE, "br_tale", run_br_table,
     &wasm_jit_t::emit_br_table},
    {WASM_OP_RETURN, WASM_INST_TYPE_NOARG, "return", run_return,
     &wasm_jit_t::emit_return},
    {WASM_OP_CALL, WASM_INST_TYPE_I32, "call", run_call,
     &wasm_jit_t::emit_call},
    {WASM_OP_CALL_INDIRECT, WASM_INST_TYPE_I32_I32, "call_indirect",
     run_call_indirect, &wasm_jit_t::emit_call_indirect},
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    {WASM_OP_DROP, WASM_INST_TYPE_NOARG, "drop", run_drop,
     &wasm_jit_t::emit_drop},
    {WASM_OP_SELECT, WASM_INST_TYPE_NOARG, "select", run_select,
     &wasm_jit_t::emit_select},
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    {WASM_OP_LOCAL_GET, WASM_INST_TYPE_I32, "local.get", run_local_get,
     &wasm_jit_t::emit_local_get},
    {WASM_OP_LOCAL_SET, WASM_INST_TYPE_I32, "local.set", run_local_set,
     &wasm_jit_t::emit_local_set},
    {WASM_OP_LOCAL_TEE, WASM_INST_TYPE_I32, "local.tee", run_local_tee,
     &wasm_jit_t::emit_local_tee},
    {WASM_OP_GLOBAL_GET, WASM_INST_TYPE_I32, "global.get", run_global_get,
     &wasm_jit_t::emit_global_get},
    {WASM_OP_GLOBAL_SET, WASM_INST_TYPE_I32, "global.set", run_global_set,
     &wasm_jit_t::emit_global_set},
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    {WASM_OP_I32_LOAD, WASM_INST_TYPE_MEMARG, "i32.load", run_i32_load,
     &wasm_jit_t::emit_i32_load},
    INVALID_INST,
    INVALID_INST,
    {WASM_OP_F64_LOAD, WASM_INST_TYPE_MEMARG, "f64.load", run_f64_load,
     &wasm_jit_t::emit_f64_load},
    {WASM_OP_I32_LOAD8_S, WASM_INST_TYPE_MEMARG, "i32.load8_s", run_i32_load8_s,
     &wasm_jit_t::emit_i32_load},
    {WASM_OP_I32_LOAD8_U, WASM_INST_TYPE_MEMARG, "i32.load8_u", run_i32_load8_u,
     &wasm_jit_t::emit_i32_load},
    {WASM_OP_I32_LOAD16_S, WASM_INST_TYPE_MEMARG, "i32.load16_s",
     run_i32_load16_s, &wasm_jit_t::emit_i32_load},
    {WASM_OP_I32_LOAD16_U, WASM_INST_TYPE_MEMARG, "i32.load16_u",
     run_i32_load16_u, &wasm_jit_t::emit_i32_load},
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    {WASM_OP_I32_STORE, WASM_INST_TYPE_MEMARG, "i32.store", run_i32_store,
     &wasm_jit_t::emit_i32_store},
    INVALID_INST,
    INVALID_INST,
    {WASM_OP_F64_STORE, WASM_INST_TYPE_MEMARG, "f64.store", run_f64_store,
     &wasm_jit_t::emit_f64_store},
    {WASM_OP_I32_STORE8, WASM_INST_TYPE_MEMARG, "i32.store8", run_i32_store8,
     &wasm_jit_t::emit_i32_store},
    {WASM_OP_I32_STORE16, WASM_INST_TYPE_MEMARG, "i32.store16", run_i32_store16,
     &wasm_jit_t::emit_i32_store},
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    {WASM_OP_I32_CONST, WASM_INST_TYPE_S32, "i32.const", run_i32_const,
     &wasm_jit_t::emit_i32_const},
    INVALID_INST,
    INVALID_INST,
    {WASM_OP_F64_CONST, WASM_INST_TYPE_F64, "f64.const", run_f64_const,
     &wasm_jit_t::emit_f64_const},
    {WASM_OP_I32_EQZ, WASM_INST_TYPE_NOARG, "i32.eqz", run_i32_numeric,
     &wasm_jit_t::emit_i32_numeric},
    {WASM_OP_I32_EQ, WASM_INST_TYPE_NOARG, "i32.eq", run_i32_numeric,
     &wasm_jit_t::emit_i32_numeric},
    {WASM_OP_I32_NE, WASM_INST_TYPE_NOARG, "i32.ne", run_i32_numeric,
     &wasm_jit_t::emit_i32_numeric},
    {WASM_OP_I32_LT_S, WASM_INST_TYPE_NOARG, "i32.lt_s", run_i32_numeric,
     &wasm_jit_t::emit_i32_numeric},
    {WASM_OP_I32_LT_U, WASM_INST_TYPE_NOARG, "i32.lt_u", run_i32_numeric,
     &wasm_jit_t::emit_i32_numeric},
    {WASM_OP_I32_GT_S, WASM_INST_TYPE_NOARG, "i32.gt_s", run_i32_numeric,
     &wasm_jit_t::emit_i32_numeric},
    {WASM_OP_I32_GT_U, WASM_INST_TYPE_NOARG, "i32.gt_u", run_i32_numeric,
     &wasm_jit_t::emit_i32_numeric},
    {WASM_OP_I32_LE_S, WASM_INST_TYPE_NOARG, "i32.le_s", run_i32_numeric,
     &wasm_jit_t::emit_i32_numeric},
    {WASM_OP_I32_LE_U, WASM_INST_TYPE_NOARG, "i32.le_u", run_i32_numeric,
     &wasm_jit_t::emit_i32_numeric},
    {WASM_OP_I32_GE_S, WASM_INST_TYPE_NOARG, "i32.ge_s", run_i32_numeric,
     &wasm_jit_t::emit_i32_numeric},
    {WASM_OP_I32_GE_U, WASM_INST_TYPE_NOARG, "i32.ge_u", run_i32_numeric,
     &wasm_jit_t::emit_i32_numeric},
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    {WASM_OP_F64_EQ, WASM_INST_TYPE_NOARG, "f64.eq", run_f64_compare,
     &wasm_jit_t::emit_f64_compute},
    {WASM_OP_F64_NE, WASM_INST_TYPE_NOARG, "f64.ne", run_f64_compare,
     &wasm_jit_t::emit_f64_compute},
    {WASM_OP_F64_LT, WASM_INST_TYPE_NOARG, "f64.lt", run_f64_compare,
     &wasm_jit_t::emit_f64_compute},
    {WASM_OP_F64_GT, WASM_INST_TYPE_NOARG, "f64.gt", run_f64_compare,
     &wasm_jit_t::emit_f64_compute},
    {WASM_OP_F64_LE, WASM_INST_TYPE_NOARG, "f64.le", run_f64_compare,
     &wasm_jit_t::emit_f64_compute},
    {WASM_OP_F64_GE, WASM_INST_TYPE_NOARG, "f64.ge", run_f64_compare,
     &wasm_jit_t::emit_f64_compute},
    {WASM_OP_I32_CLZ, WASM_INST_TYPE_NOARG, "i32.clz", run_i32_numeric,
     &wasm_jit_t::emit_i32_numeric},
    {WASM_OP_I32_CTZ, WASM_INST_TYPE_NOARG, "i32.ctz", run_i32_numeric,
     &wasm_jit_t::emit_i32_numeric},
    {WASM_OP_I32_POPCNT, WASM_INST_TYPE_NOARG, "i32.popcnt", run_i32_numeric,
     &wasm_jit_t::emit_i32_numeric},
    {WASM_OP_I32_ADD, WASM_INST_TYPE_NOARG, "i32.add", run_i32_numeric,
     &wasm_jit_t::emit_i32_numeric},
    {WASM_OP_I32_SUB, WASM_INST_TYPE_NOARG, "i32.sub", run_i32_numeric,
     &wasm_jit_t::emit_i32_numeric},
    {WASM_OP_I32_MUL, WASM_INST_TYPE_NOARG, "i32.mul", run_i32_numeric,
     &wasm_jit_t::emit_i32_numeric},
    {WASM_OP_I32_DIV_S, WASM_INST_TYPE_NOARG, "i32.div_s", run_i32_numeric,
     &wasm_jit_t::emit_i32_numeric},
    {WASM_OP_I32_DIV_U, WASM_INST_TYPE_NOARG, "i32.div_u", run_i32_numeric,
     &wasm_jit_t::emit_i32_numeric},
    {WASM_OP_I32_REM_S, WASM_INST_TYPE_NOARG, "i32.rem_s", run_i32_numeric,
     &wasm_jit_t::emit_i32_numeric},
    {WASM_OP_I32_REM_U, WASM_INST_TYPE_NOARG, "i32.rem_u", run_i32_numeric,
     &wasm_jit_t::emit_i32_numeric},
    {WASM_OP_I32_AND, WASM_INST_TYPE_NOARG, "i32.and", run_i32_numeric,
     &wasm_jit_t::emit_i32_numeric},
    {WASM_OP_I32_OR, WASM_INST_TYPE_NOARG, "i32.or", run_i32_numeric,
     &wasm_jit_t::emit_i32_numeric},
    {WASM_OP_I32_XOR, WASM_INST_TYPE_NOARG, "i32.xor", run_i32_numeric,
     &wasm_jit_t::emit_i32_numeric},
    {WASM_OP_I32_SHL, WASM_INST_TYPE_NOARG, "i32.shl", run_i32_numeric,
     &wasm_jit_t::emit_i32_shift},
    {WASM_OP_I32_SHR_S, WASM_INST_TYPE_NOARG, "i32.shr_s", run_i32_numeric,
     &wasm_jit_t::emit_i32_shift},
    {WASM_OP_I32_SHR_U, WASM_INST_TYPE_NOARG, "i32.shr_u", run_i32_numeric,
     &wasm_jit_t::emit_i32_shift},
    {WASM_OP_I32_ROTL, WASM_INST_TYPE_NOARG, "i32.rotl", run_i32_numeric,
     &wasm_jit_t::emit_i32_shift},
    {WASM_OP_I32_ROTR, WASM_INST_TYPE_NOARG, "i32.rotr", run_i32_numeric,
     &wasm_jit_t::emit_i32_shift},
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    {WASM_OP_F64_ADD, WASM_INST_TYPE_NOARG, "f64.add", run_f64_compute,
     &wasm_jit_t::emit_f64_compute},
    {WASM_OP_F64_SUB, WASM_INST_TYPE_NOARG, "f64.sub", run_f64_compute,
     &wasm_jit_t::emit_f64_compute},
    {WASM_OP_F64_MUL, WASM_INST_TYPE_NOARG, "f64.mul", run_f64_compute,
     &wasm_jit_t::emit_f64_compute},
    {WASM_OP_F64_DIV, WASM_INST_TYPE_NOARG, "f64.div", run_f64_compute,
     &wasm_jit_t::emit_f64_compute},
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    {WASM_OP_I32_TRUNC_F64_S, WASM_INST_TYPE_NOARG, "i32.trunc_f64_s",
     run_i32_numeric, &wasm_jit_t::emit_i32_convert},
    {WASM_OP_I32_TRUNC_F64_U, WASM_INST_TYPE_NOARG, "i32.trunc_f64_u",
     run_i32_numeric, &wasm_jit_t::emit_i32_convert},
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    {WASM_OP_F64_CONVERT_I32_S, WASM_INST_TYPE_NOARG, "f64.convert_i32_s",
     run_f64_convert, &wasm_jit_t::emit_f64_compute},
    {WASM_OP_F64_CONVERT_I32_U, WASM_INST_TYPE_NOARG, "f64.convert_i32_u",
     run_f64_convert, &wasm_jit_t::emit_f64_compute},
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    INVALID_INST,
    {WASM_OP_I32_EXTEND8_S, WASM_INST_TYPE_NOARG, "i32.extend8_s",
     run_i32_numeric, &wasm_jit_t::emit_i32_numeric},
    {WASM_OP_I32_EXTEND16_S, WASM_INST_TYPE_NOARG, "i32.extend16_s",
     run_i32_numeric, &wasm_jit_t::emit_i32_numeric},
};

static bool is_zero(wasm_value_t v, uint8_t type) {
  switch (type) {
    case WASM_TYPE_I32:
      return v.val.i32 == 0;
    case WASM_TYPE_F64:
      return v.val.f64 == 0;
    default:
      break;
  }
  return v.val.ref == nullptr;
}

// fancy ops
static inline uint32_t i32_clz(uint32_t x) {
  uint32_t zeros = 0;
  for (int i = 31; i >= 0; i--) {
    if ((x & (1 << i)) == 0) {
      zeros++;
    } else {
      break;
    }
  }
  return zeros;
}

static inline uint32_t i32_ctz(uint32_t x) {
  uint32_t zeros = 0;
  for (int i = 0; i < 32; i++) {
    if ((x & (1 << i)) == 0) {
      zeros++;
    } else {
      break;
    }
  }
  return zeros;
}

static inline uint32_t i32_popcnt(uint32_t x) {
  uint32_t ones = 0;
  for (int i = 31; i >= 0; i--) {
    if ((x & (1 << i)) != 0) {
      ones++;
    }
  }
  return ones;
}

static inline uint32_t i32_rotl(uint32_t x, uint32_t n) {
  return (x << n) | (x >> (32 - n));
}

static inline uint32_t i32_rotr(uint32_t x, uint32_t n) {
  return (x >> n) | (x << (32 - n));
}

static inline std::pair<bool, int32_t> i32_div_s(int32_t a, int32_t b) {
  if (b == 0) {
    return {false, 0};
  }
  if (a == (int32_t)0x80000000 && b == (int32_t)0xffffffff) {
    return {false, 0};
  }
  return {true, a / b};
}

static inline std::pair<bool, int32_t> i32_rem_s(int32_t a, int32_t b) {
  if (b == 0) {
    return {false, 0};
  }
  if (a == (int32_t)0x80000000 && b == (int32_t)0xffffffff) {
    return {true, 0};
  }
  return {true, a % b};
}

static inline std::pair<bool, uint32_t> i32_div_u(uint32_t a, uint32_t b) {
  if (b == 0) {
    return {false, 0};
  }
  return {true, a / b};
}

static inline std::pair<bool, uint32_t> i32_rem_u(uint32_t a, uint32_t b) {
  if (b == 0) {
    return {false, 0};
  }
  return {true, a % b};
}

static inline std::pair<bool, int32_t> i32_trunc_f64_s(double x) {
  int type = std::fpclassify(x);
  if (type == FP_NAN || type == FP_INFINITE) {
    return {false, 0};
  }
  if (x <= -2147483649.0 || x >= 2147483648.0) {
    return {false, 0};
  }
  return {true, x};
}

static inline std::pair<bool, uint32_t> i32_trunc_f64_u(double x) {
  int type = std::fpclassify(x);
  if (type == FP_NAN || type == FP_INFINITE) {
    return {false, 0};
  }
  if (x <= -1.0 || x >= 4294967296.0) {
    return {false, 0};
  }
  return {true, x};
}

template <typename T>
static inline bool check_access(wasm_instance_t* ins,
                                uint32_t offset,
                                uint32_t addend) {
  uint32_t size = ins->memory_.size();
  return offset <= size && addend <= size &&
         offset + addend + sizeof(T) <= size;
}

static void run_unreachable(wasm_instance_t* ins, byte opcode) {
  ins->trap(WASM_OP_UNREACHABLE);
}

static void run_nop(wasm_instance_t* ins, byte opcode) {}

static void run_drop(wasm_instance_t* ins, byte opcode) {
  ins->pop_value();
}

static void run_select(wasm_instance_t* ins, byte opcode) {
  wasm_value_t c = ins->pop_value();
  wasm_value_t b = ins->pop_value();
  wasm_value_t a = ins->pop_value();
  if (is_zero(c, WASM_TYPE_I32) == false) {
    ins->push_value(a);
  } else {
    ins->push_value(b);
  }
}

static void run_local_get(wasm_instance_t* ins, byte opcode) {
  uint32_t idx = ins->read_u32leb();
  ins->push_value(ins->cur_frame_->locals_[idx]);
}

static void run_local_set(wasm_instance_t* ins, byte opcode) {
  uint32_t idx = ins->read_u32leb();
  ins->cur_frame_->locals_[idx] = ins->pop_value();
}

static void run_local_tee(wasm_instance_t* ins, byte opcode) {
  uint32_t idx = ins->read_u32leb();
  wasm_value_t v = ins->value_stack_.back();
  ins->cur_frame_->locals_[idx] = v;
}

static void run_global_get(wasm_instance_t* ins, byte opcode) {
  uint32_t idx = ins->read_u32leb();
  ins->push_value(ins->globals_[idx]);
}

static void run_global_set(wasm_instance_t* ins, byte opcode) {
  uint32_t idx = ins->read_u32leb();
  ins->globals_[idx] = ins->pop_value();
}

#if 0
static void run_tabel_get(wasm_instance_t* ins, byte opcode) {
  uint32_t idx = ins->read_u32leb();
  wasm_value_t v;
  v.val.ref = (void*)ins->table_[idx];
  ins->push_value(v);
}

static void run_tabel_set(wasm_instance_t* ins, byte opcode) {
  uint32_t idx = ins->read_u32leb();
  ins->table_[idx] = (wasm_func_t*)ins->pop_value().val.ref;
}
#endif

static void run_i32_load(wasm_instance_t* ins, byte opcode) {
  uint32_t addend = ins->pop_value().val.i32;
  ins->read_u32leb();
  uint32_t offset = ins->read_u32leb();
  if (check_access<uint32_t>(ins, offset, addend) == false) {
    ins->trap(WASM_TRAP_REASON_OOB);
    return;
  }
  byte* base = ins->memory_.data();
  wasm_value_t v;
  v.val.i32 = *(uint32_t*)(base + (offset + addend));
  ins->push_value(v);
}

static void run_f64_load(wasm_instance_t* ins, byte opcode) {
  uint32_t addend = ins->pop_value().val.i32;
  ins->read_u32leb();
  uint32_t offset = ins->read_u32leb();
  if (check_access<double>(ins, offset, addend) == false) {
    ins->trap(WASM_TRAP_REASON_OOB);
    return;
  }
  byte* base = ins->memory_.data();
  wasm_value_t v;
  v.val.f64 = *(double*)(base + (offset + addend));
  ins->push_value(v);
}

static void run_i32_load8_u(wasm_instance_t* ins, byte opcode) {
  uint32_t addend = ins->pop_value().val.i32;
  ins->read_u32leb();
  uint32_t offset = ins->read_u32leb();
  if (check_access<uint8_t>(ins, offset, addend) == false) {
    ins->trap(WASM_TRAP_REASON_OOB);
    return;
  }
  byte* base = ins->memory_.data();
  uint8_t u8 = *(uint8_t*)(base + (offset + addend));
  wasm_value_t v;
  v.val.i32 = (uint32_t)u8;
  ins->push_value(v);
}

static void run_i32_load8_s(wasm_instance_t* ins, byte opcode) {
  uint32_t addend = ins->pop_value().val.i32;
  ins->read_u32leb();
  uint32_t offset = ins->read_u32leb();
  if (check_access<int8_t>(ins, offset, addend) == false) {
    ins->trap(WASM_TRAP_REASON_OOB);
    return;
  }
  byte* base = ins->memory_.data();
  int8_t i8 = *(int8_t*)(base + (offset + addend));
  wasm_value_t v;
  v.val.i32 = (uint32_t)(int32_t)i8;
  ins->push_value(v);
}

static void run_i32_load16_u(wasm_instance_t* ins, byte opcode) {
  uint32_t addend = ins->pop_value().val.i32;
  ins->read_u32leb();
  uint32_t offset = ins->read_u32leb();
  if (check_access<uint16_t>(ins, offset, addend) == false) {
    ins->trap(WASM_TRAP_REASON_OOB);
    return;
  }
  byte* base = ins->memory_.data();
  uint16_t u16 = *(uint16_t*)(base + (offset + addend));
  wasm_value_t v;
  v.val.i32 = (uint32_t)u16;
  ins->push_value(v);
}

static void run_i32_load16_s(wasm_instance_t* ins, byte opcode) {
  uint32_t addend = ins->pop_value().val.i32;
  ins->read_u32leb();
  uint32_t offset = ins->read_u32leb();
  if (check_access<int16_t>(ins, offset, addend) == false) {
    ins->trap(WASM_TRAP_REASON_OOB);
    return;
  }
  byte* base = ins->memory_.data();
  int16_t i16 = *(int16_t*)(base + (offset + addend));
  wasm_value_t v;
  v.val.i32 = (uint32_t)(int32_t)i16;
  ins->push_value(v);
}

static void run_i32_store(wasm_instance_t* ins, byte opcode) {
  wasm_value_t v = ins->pop_value();
  uint32_t addend = ins->pop_value().val.i32;
  ins->read_u32leb();
  uint32_t offset = ins->read_u32leb();
  if (check_access<uint32_t>(ins, offset, addend) == false) {
    ins->trap(WASM_TRAP_REASON_OOB);
    return;
  }
  byte* base = ins->memory_.data();
  *(uint32_t*)(base + (offset + addend)) = v.val.i32;
}

static void run_f64_store(wasm_instance_t* ins, byte opcode) {
  wasm_value_t v = ins->pop_value();
  uint32_t addend = ins->pop_value().val.i32;
  ins->read_u32leb();
  uint32_t offset = ins->read_u32leb();
  if (check_access<double>(ins, offset, addend) == false) {
    ins->trap(WASM_TRAP_REASON_OOB);
    return;
  }
  byte* base = ins->memory_.data();
  *(double*)(base + (offset + addend)) = v.val.f64;
}

static void run_i32_store8(wasm_instance_t* ins, byte opcode) {
  wasm_value_t v = ins->pop_value();
  uint32_t addend = ins->pop_value().val.i32;
  ins->read_u32leb();
  uint32_t offset = ins->read_u32leb();
  if (check_access<uint8_t>(ins, offset, addend) == false) {
    ins->trap(WASM_TRAP_REASON_OOB);
    return;
  }
  byte* base = ins->memory_.data();
  *(uint8_t*)(base + (offset + addend)) = (uint8_t)v.val.i32;
}

static void run_i32_store16(wasm_instance_t* ins, byte opcode) {
  wasm_value_t v = ins->pop_value();
  uint32_t addend = ins->pop_value().val.i32;
  ins->read_u32leb();
  uint32_t offset = ins->read_u32leb();
  if (check_access<uint16_t>(ins, offset, addend) == false) {
    ins->trap(WASM_TRAP_REASON_OOB);
    return;
  }
  byte* base = ins->memory_.data();
  *(uint16_t*)(base + (offset + addend)) = (uint16_t)v.val.i32;
}

static void run_i32_const(wasm_instance_t* ins, byte opcode) {
  uint32_t x = ins->read_i32leb();
  wasm_value_t v;
  v.val.i32 = x;
  ins->push_value(v);
}

static void run_f64_const(wasm_instance_t* ins, byte opcode) {
  double x = ins->read_double();
  wasm_value_t v;
  v.val.f64 = x;
  ins->push_value(v);
}

static void run_i32_numeric(wasm_instance_t* ins, byte opcode) {
  wasm_value_t a;
  wasm_value_t b;
  wasm_value_t result;

  b = ins->pop_value();
  switch (opcode) {
    case WASM_OP_I32_EQZ:
      result.val.i32 = (b.val.i32 == 0);
      break;
    case WASM_OP_I32_EQ:
      a = ins->pop_value();
      result.val.i32 = (a.val.i32 == b.val.i32);
      break;
    case WASM_OP_I32_NE:
      a = ins->pop_value();
      result.val.i32 = (a.val.i32 != b.val.i32);
      break;
    case WASM_OP_I32_LT_S:
      a = ins->pop_value();
      result.val.i32 = ((int32_t)a.val.i32 < (int32_t)b.val.i32);
      break;
    case WASM_OP_I32_LT_U:
      a = ins->pop_value();
      result.val.i32 = (a.val.i32 < b.val.i32);
      break;
    case WASM_OP_I32_GT_S:
      a = ins->pop_value();
      result.val.i32 = ((int32_t)a.val.i32 > (int32_t)b.val.i32);
      break;
    case WASM_OP_I32_GT_U:
      a = ins->pop_value();
      result.val.i32 = (a.val.i32 > b.val.i32);
      break;
    case WASM_OP_I32_LE_S:
      a = ins->pop_value();
      result.val.i32 = ((int32_t)a.val.i32 <= (int32_t)b.val.i32);
      break;
    case WASM_OP_I32_LE_U:
      a = ins->pop_value();
      result.val.i32 = (a.val.i32 <= b.val.i32);
      break;
    case WASM_OP_I32_GE_S:
      a = ins->pop_value();
      result.val.i32 = ((int32_t)a.val.i32 >= (int32_t)b.val.i32);
      break;
    case WASM_OP_I32_GE_U:
      a = ins->pop_value();
      result.val.i32 = (a.val.i32 >= b.val.i32);
      break;
    case WASM_OP_I32_CLZ:
      result.val.i32 = i32_clz(b.val.i32);
      break;
    case WASM_OP_I32_CTZ:
      result.val.i32 = i32_ctz(b.val.i32);
      break;
    case WASM_OP_I32_POPCNT:
      result.val.i32 = i32_popcnt(b.val.i32);
      break;
    case WASM_OP_I32_ADD:
      a = ins->pop_value();
      result.val.i32 = (a.val.i32 + b.val.i32);
      break;
    case WASM_OP_I32_SUB:
      a = ins->pop_value();
      result.val.i32 = (a.val.i32 - b.val.i32);
      break;
    case WASM_OP_I32_MUL:
      a = ins->pop_value();
      result.val.i32 = (a.val.i32 * b.val.i32);
      break;
    case WASM_OP_I32_DIV_S: {
      a = ins->pop_value();
      auto r = i32_div_s((int32_t)a.val.i32, (int32_t)b.val.i32);
      if (r.first == false) {
        ins->trap(WASM_TRAP_REASON_SIGFPE);
        return;
      }
      result.val.i32 = r.second;
      break;
    }
    case WASM_OP_I32_DIV_U: {
      a = ins->pop_value();
      auto r = i32_div_u((int32_t)a.val.i32, (int32_t)b.val.i32);
      if (r.first == false) {
        ins->trap(WASM_TRAP_REASON_SIGFPE);
        return;
      }
      result.val.i32 = r.second;
      break;
    }
    case WASM_OP_I32_REM_S: {
      a = ins->pop_value();
      auto r = i32_rem_s((int32_t)a.val.i32, (int32_t)b.val.i32);
      if (r.first == false) {
        ins->trap(WASM_TRAP_REASON_SIGFPE);
        return;
      }
      result.val.i32 = r.second;
      break;
    }
    case WASM_OP_I32_REM_U: {
      a = ins->pop_value();
      auto r = i32_rem_u((int32_t)a.val.i32, (int32_t)b.val.i32);
      if (r.first == false) {
        ins->trap(WASM_TRAP_REASON_SIGFPE);
        return;
      }
      result.val.i32 = r.second;
      break;
    }
    case WASM_OP_I32_AND:
      a = ins->pop_value();
      result.val.i32 = (a.val.i32 & b.val.i32);
      break;
    case WASM_OP_I32_OR:
      a = ins->pop_value();
      result.val.i32 = (a.val.i32 | b.val.i32);
      break;
    case WASM_OP_I32_XOR:
      a = ins->pop_value();
      result.val.i32 = (a.val.i32 ^ b.val.i32);
      break;
    case WASM_OP_I32_SHL:
      a = ins->pop_value();
      result.val.i32 = (a.val.i32 << b.val.i32);
      break;
    case WASM_OP_I32_SHR_S:
      a = ins->pop_value();
      result.val.i32 = ((int32_t)a.val.i32 >> b.val.i32);
      break;
    case WASM_OP_I32_SHR_U:
      a = ins->pop_value();
      result.val.i32 = (a.val.i32 >> b.val.i32);
      break;
    case WASM_OP_I32_ROTL:
      a = ins->pop_value();
      result.val.i32 = i32_rotl(a.val.i32, b.val.i32);
      break;
    case WASM_OP_I32_ROTR:
      a = ins->pop_value();
      result.val.i32 = i32_rotr(a.val.i32, b.val.i32);
      break;
    case WASM_OP_I32_TRUNC_F64_S: {
      auto r = i32_trunc_f64_s(b.val.f64);
      if (r.first == false) {
        ins->trap(WASM_TRAP_REASON_SIGFPE);
        return;
      }
      result.val.i32 = r.second;
      break;
    }
    case WASM_OP_I32_TRUNC_F64_U: {
      auto r = i32_trunc_f64_u(b.val.f64);
      if (r.first == false) {
        ins->trap(WASM_TRAP_REASON_SIGFPE);
        return;
      }
      result.val.i32 = r.second;
      break;
    }
    case WASM_OP_I32_EXTEND8_S:
      result.val.i32 = (int32_t)(int8_t)b.val.i32;
      break;
    case WASM_OP_I32_EXTEND16_S:
      result.val.i32 = (int32_t)(int16_t)b.val.i32;
      break;
  }
  ins->push_value(result);
}

static void run_f64_compare(wasm_instance_t* ins, byte opcode) {
  wasm_value_t a;
  wasm_value_t b;
  wasm_value_t result;

  b = ins->pop_value();
  a = ins->pop_value();
  switch (opcode) {
    case WASM_OP_F64_LT:
      result.val.i32 = (a.val.f64 < b.val.f64);
      break;
    case WASM_OP_F64_GT:
      result.val.i32 = (a.val.f64 > b.val.f64);
      break;
    case WASM_OP_F64_LE:
      result.val.i32 = (a.val.f64 <= b.val.f64);
      break;
    case WASM_OP_F64_GE:
      result.val.i32 = (a.val.f64 >= b.val.f64);
      break;
    case WASM_OP_F64_EQ:
      result.val.i32 = (a.val.f64 == b.val.f64);
      break;
    case WASM_OP_F64_NE:
      result.val.i32 = (a.val.f64 != b.val.f64);
      break;
  }
  ins->push_value(result);
}

static void run_f64_compute(wasm_instance_t* ins, byte opcode) {
  wasm_value_t a;
  wasm_value_t b;
  wasm_value_t result;

  b = ins->pop_value();
  a = ins->pop_value();
  switch (opcode) {
    case WASM_OP_F64_ADD:
      result.val.f64 = (a.val.f64 + b.val.f64);
      break;
    case WASM_OP_F64_SUB:
      result.val.f64 = (a.val.f64 - b.val.f64);
      break;
    case WASM_OP_F64_MUL:
      result.val.f64 = (a.val.f64 * b.val.f64);
      break;
    case WASM_OP_F64_DIV:
      result.val.f64 = (a.val.f64 / b.val.f64);
      break;
  }
  ins->push_value(result);
}

static void run_f64_convert(wasm_instance_t* ins, byte opcode) {
  wasm_value_t b;
  wasm_value_t result;

  b = ins->pop_value();
  switch (opcode) {
    case WASM_OP_F64_CONVERT_I32_S:
      result.val.f64 = (int32_t)b.val.i32;
      break;
    case WASM_OP_F64_CONVERT_I32_U:
      result.val.f64 = b.val.i32;
      break;
  }
  ins->push_value(result);
}

static void run_call(wasm_instance_t* ins, byte opcode) {
  uint32_t funcidx = ins->read_u32leb();
  ins->push_frame(&ins->module_->funcs_[funcidx]);
}

bool is_same_type(wasm_func_decl_t* s, wasm_func_decl_t* t) {
  if (s->args_.size() != t->args_.size()) {
    return false;
  }
  if (memcmp(s->args_.data(), t->args_.data(), s->args_.size()) != 0) {
    return false;
  }
  if (s->ret_.size() != t->ret_.size()) {
    return false;
  }
  if (memcmp(s->ret_.data(), t->ret_.data(), s->ret_.size()) != 0) {
    return false;
  }
  return true;
}

static void run_call_indirect(wasm_instance_t* ins, byte opcode) {
  ins->read_u32leb();
  uint32_t typeidx = ins->read_u32leb();
  wasm_value_t v = ins->pop_value();
  if (v.val.i32 >= ins->table_.size() ||
      typeidx >= ins->module_->sigs_.size()) {
    ins->trap(WASM_TRAP_REASON_OOB);
    return;
  }
  wasm_func_t* f = ins->table_[v.val.i32];
  if (f == nullptr) {
    ins->trap(WASM_TRAP_REASON_NULLFUNCREF);
    return;
  }
  if (is_same_type(&ins->module_->sigs_[typeidx], f->sig_) == false) {
    ins->trap(WASM_TRAP_REASON_TYPEERR);
    return;
  }
  ins->push_frame(ins->table_[v.val.i32]);
}

static void run_return(wasm_instance_t* ins, byte opcode) {
  ins->pop_frame();
  if (ins->frames_.empty()) {
    ins->trap(WASM_TRAP_REASON_FUNCEND);
  }
}

static void run_block(wasm_instance_t* ins, byte opcode) {
  // read the blocktype byte and go
  ins->read_byte();
}

static void run_end(wasm_instance_t* ins, byte opcode) {
  // only the OP_END for the outermost function matters
  if (ins->cur_frame_->pc_.ptr == ins->cur_frame_->pc_.end) {
    ins->pop_frame();
    if (ins->frames_.empty()) {
      ins->trap(WASM_TRAP_REASON_FUNCEND);
    }
  }
}

static void run_br(wasm_instance_t* ins, byte opcode) {
  if (opcode == WASM_OP_BR_IF) {
    wasm_value_t v = ins->pop_value();
    if (is_zero(v, WASM_TYPE_I32) == true) {
      ins->cur_frame_->pc_.ptr += sizeof(uint32_t);
      return;
    }
  }
  uint32_t to_off = *(uint32_t*)(ins->cur_frame_->pc_.ptr);
  ins->branch(to_off);
}

static void run_br_table(wasm_instance_t* ins, byte opcode) {
  uint32_t num_cases = ins->read_u32leb();
  uint32_t* cases = (uint32_t*)(ins->cur_frame_->pc_.ptr);
  wasm_value_t v = ins->pop_value();
  uint32_t to_off;
  uint32_t index = std::min(v.val.i32, num_cases);
  to_off = cases[index];
  ins->branch(to_off);
}