forked from ziglang/zig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwasm2c.c
2195 lines (2131 loc) · 114 KB
/
wasm2c.c
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 EXTRA_BRACES 0
#include "FuncGen.h"
#include "InputStream.h"
#include "panic.h"
#include "wasm.h"
#include <inttypes.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct FuncType {
const struct ResultType *param;
const struct ResultType *result;
};
static const struct FuncType *FuncType_blockType(const struct FuncType *types, int64_t block_type) {
if (block_type >= 0) return &types[block_type];
static const struct ResultType none = { 0, { 0 }};
static const struct ResultType i32 = { 1, { WasmValType_i32 } };
static const struct ResultType i64 = { 1, { WasmValType_i64 } };
static const struct ResultType f32 = { 1, { WasmValType_f32 } };
static const struct ResultType f64 = { 1, { WasmValType_f64 } };
static const struct FuncType none_i32 = { &none, &i32 };
static const struct FuncType none_i64 = { &none, &i64 };
static const struct FuncType none_f32 = { &none, &f32 };
static const struct FuncType none_f64 = { &none, &f64 };
static const struct FuncType none_none = { &none, &none };
switch (block_type) {
case WasmValType_i32: return &none_i32;
case WasmValType_i64: return &none_i64;
case WasmValType_f32: return &none_f32;
case WasmValType_f64: return &none_f64;
case WasmValType_empty: return &none_none;
default: panic("unsupported block type");
}
return NULL;
}
static uint32_t evalExpr(struct InputStream *in) {
uint32_t value;
while (true) {
switch (InputStream_readByte(in)) {
case WasmOpcode_end: return value;
case WasmOpcode_i32_const:
value = (uint32_t)InputStream_readLeb128_i32(in);
break;
default: panic("unsupported expr opcode");
}
}
}
static void renderExpr(FILE *out, struct InputStream *in) {
while (true) {
switch (InputStream_readByte(in)) {
case WasmOpcode_end: return;
case WasmOpcode_i32_const: {
uint32_t value = (uint32_t)InputStream_readLeb128_i32(in);
fprintf(out, "UINT32_C(0x%" PRIX32 ")", value);
break;
}
default: panic("unsupported expr opcode");
}
}
}
int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "usage: %s in.wasm.zst out.c\n", argv[0]);
return 1;
}
const char *mod = "wasm";
bool is_big_endian = false; // TODO
struct InputStream in;
InputStream_open(&in, argv[1]);
if (InputStream_readByte(&in) != '\0' ||
InputStream_readByte(&in) != 'a' ||
InputStream_readByte(&in) != 's' ||
InputStream_readByte(&in) != 'm') panic("input is not a zstd-compressed wasm file");
if (InputStream_readLittle_u32(&in) != 1) panic("unsupported wasm version");
FILE *out = fopen(argv[2], "wb");
if (out == NULL) panic("unable to open output file");
fputs("#include <math.h>\n"
"#include <stdint.h>\n"
"#include <stdlib.h>\n"
"#include <string.h>\n"
"\n"
"static uint16_t i16_byteswap(uint16_t src) {\n"
" return (uint16_t)(uint8_t)(src >> 0) << 8 |\n"
" (uint16_t)(uint8_t)(src >> 8) << 0;\n"
"}\n"
"static uint32_t i32_byteswap(uint32_t src) {\n"
" return (uint32_t)i16_byteswap(src >> 0) << 16 |\n"
" (uint32_t)i16_byteswap(src >> 16) << 0;\n"
"}\n"
"static uint64_t i64_byteswap(uint64_t src) {\n"
" return (uint64_t)i32_byteswap(src >> 0) << 32 |\n"
" (uint64_t)i32_byteswap(src >> 32) << 0;\n"
"}\n"
"\n", out);
fputs("static uint16_t load16_align0(const uint8_t *ptr) {\n"
" uint16_t val;\n"
" memcpy(&val, ptr, sizeof(val));\n", out);
if (is_big_endian) fputs(" val = i16_byteswap(val);", out);
fputs(" return val;\n"
"}\n"
"static uint16_t load16_align1(const uint16_t *ptr) {\n"
" uint16_t val;\n"
" memcpy(&val, ptr, sizeof(val));\n", out);
if (is_big_endian) fputs(" val = i16_byteswap(val);", out);
fputs(" return val;\n"
"}\n"
"static uint32_t load32_align0(const uint8_t *ptr) {\n"
" uint32_t val;\n"
" memcpy(&val, ptr, sizeof(val));\n", out);
if (is_big_endian) fputs(" val = i32_byteswap(val);", out);
fputs(" return val;\n"
"}\n"
"static uint32_t load32_align1(const uint16_t *ptr) {\n"
" uint32_t val;\n"
" memcpy(&val, ptr, sizeof(val));\n", out);
if (is_big_endian) fputs(" val = i32_byteswap(val);", out);
fputs(" return val;\n"
"}\n"
"static uint32_t load32_align2(const uint32_t *ptr) {\n"
" uint32_t val;\n"
" memcpy(&val, ptr, sizeof(val));\n", out);
if (is_big_endian) fputs(" val = i32_byteswap(val);", out);
fputs(" return val;\n"
"}\n"
"static uint64_t load64_align0(const uint8_t *ptr) {\n"
" uint64_t val;\n"
" memcpy(&val, ptr, sizeof(val));\n", out);
if (is_big_endian) fputs(" val = i64_byteswap(val);", out);
fputs(" return val;\n"
"}\n"
"static uint64_t load64_align1(const uint16_t *ptr) {\n"
" uint64_t val;\n"
" memcpy(&val, ptr, sizeof(val));\n", out);
if (is_big_endian) fputs(" val = i64_byteswap(val);", out);
fputs(" return val;\n"
"}\n"
"static uint64_t load64_align2(const uint32_t *ptr) {\n"
" uint64_t val;\n"
" memcpy(&val, ptr, sizeof(val));\n", out);
if (is_big_endian) fputs(" val = i64_byteswap(val);", out);
fputs(" return val;\n"
"}\n"
"static uint64_t load64_align3(const uint64_t *ptr) {\n"
" uint64_t val;\n"
" memcpy(&val, ptr, sizeof(val));\n", out);
if (is_big_endian) fputs(" val = i64_byteswap(val);", out);
fputs(" return val;\n"
"}\n"
"\n"
"static uint32_t i32_popcnt(uint32_t lhs) {\n"
" lhs = lhs - ((lhs >> 1) & UINT32_C(0x55555555));\n"
" lhs = (lhs & UINT32_C(0x33333333)) + ((lhs >> 2) & UINT32_C(0x33333333));\n"
" lhs = (lhs + (lhs >> 4)) & UINT32_C(0x0F0F0F0F);\n"
" return (lhs * UINT32_C(0x01010101)) >> 24;\n"
"}\n"
"static uint32_t i32_ctz(uint32_t lhs) {\n"
" return i32_popcnt(~lhs & (lhs - 1));\n"
"}\n"
"static uint32_t i32_clz(uint32_t lhs) {\n"
" lhs = i32_byteswap(lhs);\n"
" lhs = (lhs & UINT32_C(0x0F0F0F0F)) << 4 | (lhs & UINT32_C(0xF0F0F0F0)) >> 4;\n"
" lhs = (lhs & UINT32_C(0x33333333)) << 2 | (lhs & UINT32_C(0xCCCCCCCC)) >> 2;\n"
" lhs = (lhs & UINT32_C(0x55555555)) << 1 | (lhs & UINT32_C(0xAAAAAAAA)) >> 1;\n"
" return i32_ctz(lhs);\n"
"}\n"
"static uint64_t i64_popcnt(uint64_t lhs) {\n"
" lhs = lhs - ((lhs >> 1) & UINT64_C(0x5555555555555555));\n"
" lhs = (lhs & UINT64_C(0x3333333333333333)) + ((lhs >> 2) & UINT64_C(0x3333333333333333));\n"
" lhs = (lhs + (lhs >> 4)) & UINT64_C(0x0F0F0F0F0F0F0F0F);\n"
" return (lhs * UINT64_C(0x0101010101010101)) >> 56;\n"
"}\n"
"static uint64_t i64_ctz(uint64_t lhs) {\n"
" return i64_popcnt(~lhs & (lhs - 1));\n"
"}\n"
"static uint64_t i64_clz(uint64_t lhs) {\n"
" lhs = i64_byteswap(lhs);\n"
" lhs = (lhs & UINT64_C(0x0F0F0F0F0F0F0F0F)) << 4 | (lhs & UINT32_C(0xF0F0F0F0F0F0F0F0)) >> 4;\n"
" lhs = (lhs & UINT64_C(0x3333333333333333)) << 2 | (lhs & UINT32_C(0xCCCCCCCCCCCCCCCC)) >> 2;\n"
" lhs = (lhs & UINT64_C(0x5555555555555555)) << 1 | (lhs & UINT32_C(0xAAAAAAAAAAAAAAAA)) >> 1;\n"
" return i64_ctz(lhs);\n"
"}\n"
"\n"
"static void store16_align0(uint8_t *ptr, uint16_t val) {\n", out);
if (is_big_endian) fputs(" val = i16_byteswap(val);", out);
fputs(" memcpy(ptr, &val, sizeof(val));\n"
"}\n"
"static void store16_align1(uint16_t *ptr, uint16_t val) {\n", out);
if (is_big_endian) fputs(" val = i16_byteswap(val);", out);
fputs(" memcpy(ptr, &val, sizeof(val));\n"
"}\n"
"static void store32_align0(uint8_t *ptr, uint32_t val) {\n", out);
if (is_big_endian) fputs(" val = i32_byteswap(val);", out);
fputs(" memcpy(ptr, &val, sizeof(val));\n"
"}\n"
"static void store32_align1(uint16_t *ptr, uint32_t val) {\n", out);
if (is_big_endian) fputs(" val = i32_byteswap(val);", out);
fputs(" memcpy(ptr, &val, sizeof(val));\n"
"}\n"
"static void store32_align2(uint32_t *ptr, uint32_t val) {\n", out);
if (is_big_endian) fputs(" val = i32_byteswap(val);", out);
fputs(" memcpy(ptr, &val, sizeof(val));\n"
"}\n"
"static void store64_align0(uint8_t *ptr, uint64_t val) {\n", out);
if (is_big_endian) fputs(" val = i64_byteswap(val);", out);
fputs(" memcpy(ptr, &val, sizeof(val));\n"
"}\n"
"static void store64_align1(uint16_t *ptr, uint64_t val) {\n", out);
if (is_big_endian) fputs(" val = i64_byteswap(val);", out);
fputs(" memcpy(ptr, &val, sizeof(val));\n"
"}\n"
"static void store64_align2(uint32_t *ptr, uint64_t val) {\n", out);
if (is_big_endian) fputs(" val = i64_byteswap(val);", out);
fputs(" memcpy(ptr, &val, sizeof(val));\n"
"}\n"
"static void store64_align3(uint64_t *ptr, uint64_t val) {\n", out);
if (is_big_endian) fputs(" val = i64_byteswap(val);", out);
fputs(" memcpy(ptr, &val, sizeof(val));\n"
"}\n"
"\n"
"static uint32_t i32_reinterpret_f32(const float src) {\n"
" uint32_t dst;\n"
" memcpy(&dst, &src, sizeof(dst));\n"
" return dst;\n"
"}\n"
"static uint64_t i64_reinterpret_f64(const double src) {\n"
" uint64_t dst;\n"
" memcpy(&dst, &src, sizeof(dst));\n"
" return dst;\n"
"}\n"
"static float f32_reinterpret_i32(const uint32_t src) {\n"
" float dst;\n"
" memcpy(&dst, &src, sizeof(dst));\n"
" return dst;\n"
"}\n"
"static double f64_reinterpret_i64(const uint64_t src) {\n"
" double dst;\n"
" memcpy(&dst, &src, sizeof(dst));\n"
" return dst;\n"
"}\n"
"\n"
"static uint32_t memory_grow(uint8_t **m, uint32_t *p, uint32_t *c, uint32_t n) {\n"
" uint8_t *new_m = *m;\n"
" uint32_t r = *p;\n"
" uint32_t new_p = r + n;\n"
" if (new_p > UINT32_C(0x10000)) return UINT32_C(0xFFFFFFF);\n"
" uint32_t new_c = *c;\n"
" if (new_c < new_p) {\n"
" do new_c += new_c / 2 + 8; while (new_c < new_p);\n"
" if (new_c > UINT32_C(0x10000)) new_c = UINT32_C(0x10000);\n"
" new_m = realloc(new_m, new_c << 16);\n"
" if (new_m == NULL) return UINT32_C(0xFFFFFFF);\n"
" *m = new_m;\n"
" *c = new_c;\n"
" }\n"
" *p = new_p;\n"
" memset(&new_m[r << 16], 0, n << 16);\n"
" return r;\n"
"}\n"
"\n"
"static int inited;\n"
"static void init_elem(void);\n"
"static void init_data(void);\n"
"static void init(void) {\n"
" if (inited != 0) return;\n"
" init_elem();\n"
" init_data();\n"
" inited = 1;\n"
"}\n"
"\n", out);
struct FuncType *types;
uint32_t max_param_len = 0;
(void)InputStream_skipToSection(&in, WasmSectionId_type);
{
uint32_t len = InputStream_readLeb128_u32(&in);
types = malloc(sizeof(struct FuncType) * len);
if (types == NULL) panic("out of memory");
for (uint32_t i = 0; i < len; i += 1) {
if (InputStream_readByte(&in) != 0x60) panic("expected functype");
types[i].param = InputStream_readResultType(&in);
if (types[i].param->len > max_param_len) max_param_len = types[i].param->len;
types[i].result = InputStream_readResultType(&in);
}
}
struct Import {
const char *mod;
const char *name;
uint32_t type_idx;
} *imports;
(void)InputStream_skipToSection(&in, WasmSectionId_import);
uint32_t imports_len = InputStream_readLeb128_u32(&in);
{
imports = malloc(sizeof(struct Import) * imports_len);
if (imports == NULL) panic("out of memory");
for (uint32_t i = 0; i < imports_len; i += 1) {
imports[i].mod = InputStream_readName(&in);
imports[i].name = InputStream_readName(&in);
switch (InputStream_readByte(&in)) {
case 0x00: { // func
imports[i].type_idx = InputStream_readLeb128_u32(&in);
const struct FuncType *func_type = &types[imports[i].type_idx];
switch (func_type->result->len) {
case 0: fputs("void", out); break;
case 1: fputs(WasmValType_toC(func_type->result->types[0]), out); break;
default: panic("multiple function returns not supported");
}
fprintf(out, " %s_%s(", imports[i].mod, imports[i].name);
if (func_type->param->len == 0) fputs("void", out);
for (uint32_t param_i = 0; param_i < func_type->param->len; param_i += 1) {
if (param_i > 0) fputs(", ", out);
fputs(WasmValType_toC(func_type->param->types[param_i]), out);
}
fputs(");\n", out);
break;
}
case 0x01: // table
case 0x02: // mem
case 0x03: // global
default:
panic("unsupported import type");
}
}
fputc('\n', out);
}
struct Func {
uint32_t type_idx;
} *funcs;
(void)InputStream_skipToSection(&in, WasmSectionId_func);
{
uint32_t len = InputStream_readLeb128_u32(&in);
funcs = malloc(sizeof(struct Func) * len);
if (funcs == NULL) panic("out of memory");
for (uint32_t i = 0; i < len; i += 1) {
funcs[i].type_idx = InputStream_readLeb128_u32(&in);
const struct FuncType *func_type = &types[funcs[i].type_idx];
fputs("static ", out);
switch (func_type->result->len) {
case 0: fputs("void", out); break;
case 1: fputs(WasmValType_toC(func_type->result->types[0]), out); break;
default: panic("multiple function returns not supported");
}
fprintf(out, " f%" PRIu32 "(", i);
if (func_type->param->len == 0) fputs("void", out);
for (uint32_t param_i = 0; param_i < func_type->param->len; param_i += 1) {
if (param_i > 0) fputs(", ", out);
fprintf(out, "%s", WasmValType_toC(func_type->param->types[param_i]));
}
fputs(");\n", out);
}
fputc('\n', out);
}
struct Table {
int8_t type;
struct Limits limits;
} *tables;
(void)InputStream_skipToSection(&in, WasmSectionId_table);
{
uint32_t len = InputStream_readLeb128_u32(&in);
tables = malloc(sizeof(struct Table) * len);
if (tables == NULL) panic("out of memory");
for (uint32_t i = 0; i < len; i += 1) {
int64_t ref_type = InputStream_readLeb128_i64(&in);
switch (ref_type) {
case WasmValType_funcref:
break;
default: panic("unsupported reftype");
}
tables[i].type = ref_type;
tables[i].limits = InputStream_readLimits(&in);
if (tables[i].limits.min != tables[i].limits.max) panic("growable table not supported");
fprintf(out, "static void (*t%" PRIu32 "[UINT32_C(%" PRIu32 ")])(void);\n",
i, tables[i].limits.min);
}
fputc('\n', out);
}
struct Mem {
struct Limits limits;
} *mems;
(void)InputStream_skipToSection(&in, WasmSectionId_mem);
uint32_t mems_len = InputStream_readLeb128_u32(&in);
{
mems = malloc(sizeof(struct Mem) * mems_len);
if (mems == NULL) panic("out of memory");
for (uint32_t i = 0; i < mems_len; i += 1) {
mems[i].limits = InputStream_readLimits(&in);
fprintf(out, "static uint8_t *m%" PRIu32 ";\n"
"static uint32_t p%" PRIu32 ";\n"
"static uint32_t c%" PRIu32 ";\n", i, i, i);
}
fputc('\n', out);
}
struct Global {
bool mut;
int8_t val_type;
} *globals;
(void)InputStream_skipToSection(&in, WasmSectionId_global);
{
uint32_t len = InputStream_readLeb128_u32(&in);
globals = malloc(sizeof(struct Global) * len);
if (globals == NULL) panic("out of memory");
for (uint32_t i = 0; i < len; i += 1) {
int64_t val_type = InputStream_readLeb128_i64(&in);
enum WasmMut mut = InputStream_readByte(&in);
fprintf(out, "%s%s g%" PRIu32 " = ", WasmMut_toC(mut), WasmValType_toC(val_type), i);
renderExpr(out, &in);
fputs(";\n", out);
globals[i].mut = mut;
globals[i].val_type = val_type;
}
fputc('\n', out);
}
(void)InputStream_skipToSection(&in, WasmSectionId_export);
{
uint32_t len = InputStream_readLeb128_u32(&in);
for (uint32_t i = 0; i < len; i += 1) {
char *name = InputStream_readName(&in);
uint8_t kind = InputStream_readByte(&in);
uint32_t idx = InputStream_readLeb128_u32(&in);
switch (kind) {
case 0x00: {
if (idx < imports_len) panic("can't export an import");
const struct FuncType *func_type = &types[funcs[idx - imports_len].type_idx];
switch (func_type->result->len) {
case 0: fputs("void", out); break;
case 1: fputs(WasmValType_toC(func_type->result->types[0]), out); break;
default: panic("multiple function returns not supported");
}
fprintf(out, " %s_%s(", mod, name);
if (func_type->param->len == 0) fputs("void", out);
for (uint32_t param_i = 0; param_i < func_type->param->len; param_i += 1) {
if (param_i > 0) fputs(", ", out);
fprintf(out, "%s l%" PRIu32, WasmValType_toC(func_type->param->types[param_i]), param_i);
}
fprintf(out,
") {\n"
" init();\n"
" %sf%" PRIu32 "(",
func_type->result->len > 0 ? "return " : "", idx - imports_len);
for (uint32_t param_i = 0; param_i < func_type->param->len; param_i += 1) {
if (param_i > 0) fputs(", ", out);
fprintf(out, "l%" PRIu32, param_i);
}
fputs(");\n}\n", out);
break;
}
case 0x02:
fprintf(out, "uint8_t **const %s_%s = &m%" PRIu32 ";\n", mod, name, idx);
break;
default: panic("unsupported export kind");
}
free(name);
}
fputc('\n', out);
}
(void)InputStream_skipToSection(&in, WasmSectionId_elem);
{
uint32_t table_i = 0;
uint32_t len = InputStream_readLeb128_u32(&in);
fputs("static void init_elem(void) {\n", out);
for (uint32_t segment_i = 0; segment_i < len; segment_i += 1) {
uint32_t table_idx = 0;
uint32_t elem_type = InputStream_readLeb128_u32(&in);
if (elem_type != 0x00) panic("unsupported elem type");
uint32_t offset = evalExpr(&in);
uint32_t segment_len = InputStream_readLeb128_u32(&in);
for (uint32_t i = 0; i < segment_len; i += 1) {
uint32_t func_id = InputStream_readLeb128_u32(&in);
fprintf(out, " t%" PRIu32 "[UINT32_C(%" PRIu32 ")] = (void (*)(void))&",
table_idx, offset + i);
if (func_id < imports_len)
fprintf(out, "%s_%s", imports[func_id].mod, imports[func_id].name);
else
fprintf(out, "f%" PRIu32, func_id - imports_len);
fputs(";\n", out);
}
}
fputs("}\n\n", out);
}
(void)InputStream_skipToSection(&in, WasmSectionId_code);
{
struct FuncGen fg;
FuncGen_init(&fg);
bool *param_used = malloc(sizeof(bool) * max_param_len);
uint32_t *param_stash = malloc(sizeof(uint32_t) * max_param_len);
uint32_t len = InputStream_readLeb128_u32(&in);
for (uint32_t func_i = 0; func_i < len; func_i += 1) {
FuncGen_reset(&fg);
uint32_t code_len = InputStream_readLeb128_u32(&in);
const struct FuncType *func_type = &types[funcs[func_i].type_idx];
fputs("static ", out);
switch (func_type->result->len) {
case 0: fputs("void", out); break;
case 1: fputs(WasmValType_toC(func_type->result->types[0]), out); break;
default: panic("multiple function returns not supported");
}
fprintf(out, " f%" PRIu32 "(", func_i);
if (func_type->param->len == 0) fputs("void", out);
for (uint32_t param_i = 0; param_i < func_type->param->len; param_i += 1) {
param_used[param_i] = false;
int8_t param_type = func_type->param->types[param_i];
if (param_i > 0) fputs(", ", out);
FuncGen_localDeclare(&fg, out, param_type);
}
fputs(") {\n", out);
for (uint32_t local_sets_remaining = InputStream_readLeb128_u32(&in);
local_sets_remaining > 0; local_sets_remaining -= 1) {
uint32_t local_set_len = InputStream_readLeb128_u32(&in);
int64_t val_type = InputStream_readLeb128_i64(&in);
for (; local_set_len > 0; local_set_len -= 1) {
FuncGen_indent(&fg, out);
FuncGen_localDeclare(&fg, out, val_type);
fputs(" = 0;\n", out);
}
}
uint32_t unreachable_depth = 0;
for (uint32_t result_i = func_type->result->len; result_i > 0; ) {
result_i -= 1;
FuncGen_indent(&fg, out);
(void)FuncGen_localDeclare(&fg, out,
func_type->result->types[result_i]);
fputs(";\n", out);
}
FuncGen_blockBegin(&fg, out, WasmOpcode_block, funcs[func_i].type_idx);
while (!FuncGen_done(&fg)) {
//static const char *mnemonics[] = {
// [WasmOpcode_unreachable] = "unreachable",
// [WasmOpcode_nop] = "nop",
// [WasmOpcode_block] = "block",
// [WasmOpcode_loop] = "loop",
// [WasmOpcode_if] = "if",
// [WasmOpcode_else] = "else",
// [WasmOpcode_end] = "end",
// [WasmOpcode_br] = "br",
// [WasmOpcode_br_if] = "br_if",
// [WasmOpcode_br_table] = "br_table",
// [WasmOpcode_return] = "return",
// [WasmOpcode_call] = "call",
// [WasmOpcode_call_indirect] = "call_indirect",
//
// [WasmOpcode_drop] = "drop",
// [WasmOpcode_select] = "select",
// [WasmOpcode_select_t] = "select t",
//
// [WasmOpcode_local_get] = "local.get",
// [WasmOpcode_local_set] = "local.set",
// [WasmOpcode_local_tee] = "local.tee",
// [WasmOpcode_global_get] = "global.get",
// [WasmOpcode_global_set] = "global.set",
// [WasmOpcode_table_get] = "table.get",
// [WasmOpcode_table_set] = "table.set",
//
// [WasmOpcode_i32_load] = "i32.load",
// [WasmOpcode_i64_load] = "i64.load",
// [WasmOpcode_f32_load] = "f32.load",
// [WasmOpcode_f64_load] = "f64.load",
// [WasmOpcode_i32_load8_s] = "i32.load8_s",
// [WasmOpcode_i32_load8_u] = "i32.load8_u",
// [WasmOpcode_i32_load16_s] = "i32.load16_s",
// [WasmOpcode_i32_load16_u] = "i32.load16_u",
// [WasmOpcode_i64_load8_s] = "i64.load8_s",
// [WasmOpcode_i64_load8_u] = "i64.load8_u",
// [WasmOpcode_i64_load16_s] = "i64.load16_s",
// [WasmOpcode_i64_load16_u] = "i64.load16_u",
// [WasmOpcode_i64_load32_s] = "i64.load32_s",
// [WasmOpcode_i64_load32_u] = "i64.load32_u",
// [WasmOpcode_i32_store] = "i32.store",
// [WasmOpcode_i64_store] = "i64.store",
// [WasmOpcode_f32_store] = "f32.store",
// [WasmOpcode_f64_store] = "f64.store",
// [WasmOpcode_i32_store8] = "i32.store8",
// [WasmOpcode_i32_store16] = "i32.store16",
// [WasmOpcode_i64_store8] = "i64.store8",
// [WasmOpcode_i64_store16] = "i64.store16",
// [WasmOpcode_i64_store32] = "i64.store32",
// [WasmOpcode_memory_size] = "memory.size",
// [WasmOpcode_memory_grow] = "memory.grow",
//
// [WasmOpcode_i32_const] = "i32.const",
// [WasmOpcode_i64_const] = "i64.const",
// [WasmOpcode_f32_const] = "f32.const",
// [WasmOpcode_f64_const] = "f64.const",
//
// [WasmOpcode_i32_eqz] = "i32.eqz",
// [WasmOpcode_i32_eq] = "i32.eq",
// [WasmOpcode_i32_ne] = "i32.ne",
// [WasmOpcode_i32_lt_s] = "i32.lt_s",
// [WasmOpcode_i32_lt_u] = "i32.lt_u",
// [WasmOpcode_i32_gt_s] = "i32.gt_s",
// [WasmOpcode_i32_gt_u] = "i32.gt_u",
// [WasmOpcode_i32_le_s] = "i32.le_s",
// [WasmOpcode_i32_le_u] = "i32.le_u",
// [WasmOpcode_i32_ge_s] = "i32.ge_s",
// [WasmOpcode_i32_ge_u] = "i32.ge_u",
//
// [WasmOpcode_i64_eqz] = "i64.eqz",
// [WasmOpcode_i64_eq] = "i64.eq",
// [WasmOpcode_i64_ne] = "i64.ne",
// [WasmOpcode_i64_lt_s] = "i64.lt_s",
// [WasmOpcode_i64_lt_u] = "i64.lt_u",
// [WasmOpcode_i64_gt_s] = "i64.gt_s",
// [WasmOpcode_i64_gt_u] = "i64.gt_u",
// [WasmOpcode_i64_le_s] = "i64.le_s",
// [WasmOpcode_i64_le_u] = "i64.le_u",
// [WasmOpcode_i64_ge_s] = "i64.ge_s",
// [WasmOpcode_i64_ge_u] = "i64.ge_u",
//
// [WasmOpcode_f32_eq] = "f32.eq",
// [WasmOpcode_f32_ne] = "f32.ne",
// [WasmOpcode_f32_lt] = "f32.lt",
// [WasmOpcode_f32_gt] = "f32.gt",
// [WasmOpcode_f32_le] = "f32.le",
// [WasmOpcode_f32_ge] = "f32.ge",
//
// [WasmOpcode_f64_eq] = "f64.eq",
// [WasmOpcode_f64_ne] = "f64.ne",
// [WasmOpcode_f64_lt] = "f64.lt",
// [WasmOpcode_f64_gt] = "f64.gt",
// [WasmOpcode_f64_le] = "f64.le",
// [WasmOpcode_f64_ge] = "f64.ge",
//
// [WasmOpcode_i32_clz] = "i32.clz",
// [WasmOpcode_i32_ctz] = "i32.ctz",
// [WasmOpcode_i32_popcnt] = "i32.popcnt",
// [WasmOpcode_i32_add] = "i32.add",
// [WasmOpcode_i32_sub] = "i32.sub",
// [WasmOpcode_i32_mul] = "i32.mul",
// [WasmOpcode_i32_div_s] = "i32.div_s",
// [WasmOpcode_i32_div_u] = "i32.div_u",
// [WasmOpcode_i32_rem_s] = "i32.rem_s",
// [WasmOpcode_i32_rem_u] = "i32.rem_u",
// [WasmOpcode_i32_and] = "i32.and",
// [WasmOpcode_i32_or] = "i32.or",
// [WasmOpcode_i32_xor] = "i32.xor",
// [WasmOpcode_i32_shl] = "i32.shl",
// [WasmOpcode_i32_shr_s] = "i32.shr_s",
// [WasmOpcode_i32_shr_u] = "i32.shr_u",
// [WasmOpcode_i32_rotl] = "i32.rotl",
// [WasmOpcode_i32_rotr] = "i32.rotr",
//
// [WasmOpcode_i64_clz] = "i64.clz",
// [WasmOpcode_i64_ctz] = "i64.ctz",
// [WasmOpcode_i64_popcnt] = "i64.popcnt",
// [WasmOpcode_i64_add] = "i64.add",
// [WasmOpcode_i64_sub] = "i64.sub",
// [WasmOpcode_i64_mul] = "i64.mul",
// [WasmOpcode_i64_div_s] = "i64.div_s",
// [WasmOpcode_i64_div_u] = "i64.div_u",
// [WasmOpcode_i64_rem_s] = "i64.rem_s",
// [WasmOpcode_i64_rem_u] = "i64.rem_u",
// [WasmOpcode_i64_and] = "i64.and",
// [WasmOpcode_i64_or] = "i64.or",
// [WasmOpcode_i64_xor] = "i64.xor",
// [WasmOpcode_i64_shl] = "i64.shl",
// [WasmOpcode_i64_shr_s] = "i64.shr_s",
// [WasmOpcode_i64_shr_u] = "i64.shr_u",
// [WasmOpcode_i64_rotl] = "i64.rotl",
// [WasmOpcode_i64_rotr] = "i64.rotr",
//
// [WasmOpcode_f32_abs] = "f32.abs",
// [WasmOpcode_f32_neg] = "f32.neg",
// [WasmOpcode_f32_ceil] = "f32.ceil",
// [WasmOpcode_f32_floor] = "f32.floor",
// [WasmOpcode_f32_trunc] = "f32.trunc",
// [WasmOpcode_f32_nearest] = "f32.nearest",
// [WasmOpcode_f32_sqrt] = "f32.sqrt",
// [WasmOpcode_f32_add] = "f32.add",
// [WasmOpcode_f32_sub] = "f32.sub",
// [WasmOpcode_f32_mul] = "f32.mul",
// [WasmOpcode_f32_div] = "f32.div",
// [WasmOpcode_f32_min] = "f32.min",
// [WasmOpcode_f32_max] = "f32.max",
// [WasmOpcode_f32_copysign] = "f32.copysign",
//
// [WasmOpcode_f64_abs] = "f64.abs",
// [WasmOpcode_f64_neg] = "f64.neg",
// [WasmOpcode_f64_ceil] = "f64.ceil",
// [WasmOpcode_f64_floor] = "f64.floor",
// [WasmOpcode_f64_trunc] = "f64.trunc",
// [WasmOpcode_f64_nearest] = "f64.nearest",
// [WasmOpcode_f64_sqrt] = "f64.sqrt",
// [WasmOpcode_f64_add] = "f64.add",
// [WasmOpcode_f64_sub] = "f64.sub",
// [WasmOpcode_f64_mul] = "f64.mul",
// [WasmOpcode_f64_div] = "f64.div",
// [WasmOpcode_f64_min] = "f64.min",
// [WasmOpcode_f64_max] = "f64.max",
// [WasmOpcode_f64_copysign] = "f64.copysign",
//
// [WasmOpcode_i32_wrap_i64] = "i32.wrap_i64",
// [WasmOpcode_i32_trunc_f32_s] = "i32.trunc_f32_s",
// [WasmOpcode_i32_trunc_f32_u] = "i32.trunc_f32_u",
// [WasmOpcode_i32_trunc_f64_s] = "i32.trunc_f64_s",
// [WasmOpcode_i32_trunc_f64_u] = "i32.trunc_f64_u",
// [WasmOpcode_i64_extend_i32_s] = "i64.extend_i32_s",
// [WasmOpcode_i64_extend_i32_u] = "i64.extend_i32_u",
// [WasmOpcode_i64_trunc_f32_s] = "i64.trunc_f32_s",
// [WasmOpcode_i64_trunc_f32_u] = "i64.trunc_f32_u",
// [WasmOpcode_i64_trunc_f64_s] = "i64.trunc_f64_s",
// [WasmOpcode_i64_trunc_f64_u] = "i64.trunc_f64_u",
// [WasmOpcode_f32_convert_i32_s] = "f32.convert_i32_s",
// [WasmOpcode_f32_convert_i32_u] = "f32.convert_i32_u",
// [WasmOpcode_f32_convert_i64_s] = "f32.convert_i64_s",
// [WasmOpcode_f32_convert_i64_u] = "f32.convert_i64_u",
// [WasmOpcode_f32_demote_f64] = "f32.demote_f64",
// [WasmOpcode_f64_convert_i32_s] = "f64.convert_i32_s",
// [WasmOpcode_f64_convert_i32_u] = "f64.convert_i32_u",
// [WasmOpcode_f64_convert_i64_s] = "f64.convert_i64_s",
// [WasmOpcode_f64_convert_i64_u] = "f64.convert_i64_u",
// [WasmOpcode_f64_promote_f32] = "f64.promote_f32",
// [WasmOpcode_i32_reinterpret_f32] = "i32.reinterpret_f32",
// [WasmOpcode_i64_reinterpret_f64] = "i64.reinterpret_f64",
// [WasmOpcode_f32_reinterpret_i32] = "f32.reinterpret_i32",
// [WasmOpcode_f64_reinterpret_i64] = "f64.reinterpret_i64",
//
// [WasmOpcode_i32_extend8_s] = "i32.extend8_s",
// [WasmOpcode_i32_extend16_s] = "i32.extend16_s",
// [WasmOpcode_i64_extend8_s] = "i64.extend8_s",
// [WasmOpcode_i64_extend16_s] = "i64.extend16_s",
// [WasmOpcode_i64_extend32_s] = "i64.extend32_s",
//
// [WasmOpcode_prefixed] = "prefixed",
//};
uint8_t opcode = InputStream_readByte(&in);
//FuncGen_indent(&fg, out);
//fprintf(out, "// %2u: ", fg.stack_i);
//if (mnemonics[opcode])
// fprintf(out, "%s\n", mnemonics[opcode]);
//else
// fprintf(out, "%02hhX\n", opcode);
//fflush(out); // DEBUG
switch (opcode) {
case WasmOpcode_unreachable:
if (unreachable_depth == 0) {
FuncGen_indent(&fg, out);
fprintf(out, "abort();\n");
unreachable_depth += 1;
}
break;
case WasmOpcode_nop:
break;
case WasmOpcode_block:
case WasmOpcode_loop:
case WasmOpcode_if: {
int64_t block_type = InputStream_readLeb128_i64(&in);
if (unreachable_depth == 0) {
const struct FuncType *func_type = FuncType_blockType(types, block_type);
for (uint32_t param_i = func_type->param->len; param_i > 0; ) {
param_i -= 1;
FuncGen_indent(&fg, out);
param_stash[param_i] =
FuncGen_localDeclare(&fg, out, func_type->param->types[param_i]);
fprintf(out, " = l%" PRIu32 ";\n", FuncGen_stackPop(&fg));
}
for (uint32_t result_i = func_type->result->len; result_i > 0; ) {
result_i -= 1;
FuncGen_indent(&fg, out);
(void)FuncGen_localDeclare(&fg, out,
func_type->result->types[result_i]);
fputs(";\n", out);
}
FuncGen_blockBegin(&fg, out, opcode, block_type);
for (uint32_t param_i = 0; param_i < func_type->param->len; param_i += 1) {
FuncGen_stackPush(&fg, out, func_type->param->types[param_i]);
fprintf(out, " = l%" PRIu32 ";\n", param_stash[param_i]);
}
} else unreachable_depth += 1;
break;
}
case WasmOpcode_else:
case WasmOpcode_end:
if (unreachable_depth <= 1) {
const struct ResultType *result_type =
FuncType_blockType(types, FuncGen_blockType(&fg, 0))->result;
uint32_t label = FuncGen_blockLabel(&fg, 0);
if (unreachable_depth == 0) {
const struct ResultType *result_type =
FuncType_blockType(types, FuncGen_blockType(&fg, 0))->result;
for (uint32_t result_i = result_type->len; result_i > 0; ) {
result_i -= 1;
FuncGen_indent(&fg, out);
fprintf(out, "l%" PRIu32 " = l%" PRIu32 ";\n",
label - result_type->len + result_i, FuncGen_stackPop(&fg));
}
} else unreachable_depth -= 1;
switch (opcode) {
case WasmOpcode_else:
FuncGen_reuseReset(&fg);
FuncGen_outdent(&fg, out);
fputs("} else {\n", out);
break;
case WasmOpcode_end:
FuncGen_blockEnd(&fg, out);
for (uint32_t result_i = 0; result_i < result_type->len;
result_i += 1) {
FuncGen_stackPush(&fg, out, result_type->types[result_i]);
fprintf(out, "l%" PRIu32 ";\n",
label - result_type->len + result_i);
}
break;
}
} else if (opcode == WasmOpcode_end) unreachable_depth -= 1;
break;
case WasmOpcode_br:
case WasmOpcode_br_if: {
uint32_t label_idx = InputStream_readLeb128_u32(&in);
if (unreachable_depth == 0) {
enum WasmOpcode kind = FuncGen_blockKind(&fg, label_idx);
const struct FuncType *func_type =
FuncType_blockType(types, FuncGen_blockType(&fg, label_idx));
uint32_t label = FuncGen_blockLabel(&fg, label_idx);
if (opcode == WasmOpcode_br_if) {
FuncGen_indent(&fg, out);
fprintf(out, "if (l%" PRIu32 ") {\n", FuncGen_stackPop(&fg));
} else if (EXTRA_BRACES) {
FuncGen_indent(&fg, out);
fputs("{\n", out);
}
const struct ResultType *label_type;
uint32_t lhs;
switch (kind) {
case WasmOpcode_loop:
label_type = func_type->param;
lhs = label - func_type->result->len - func_type->param->len;
break;
default:
label_type = func_type->result;
lhs = label - func_type->result->len;
break;
}
for (uint32_t stack_i = 0; stack_i < label_type->len; stack_i += 1) {
uint32_t rhs;
switch (opcode) {
case WasmOpcode_br:
rhs = FuncGen_stackPop(&fg);
break;
case WasmOpcode_br_if:
rhs = FuncGen_stackAt(&fg, stack_i);
break;
default: panic("unexpected opcode");
}
FuncGen_cont(&fg, out);
fprintf(out, "l%" PRIu32 " = l%" PRIu32 ";\n", lhs, rhs);
lhs += 1;
}
FuncGen_cont(&fg, out);
fprintf(out, "goto l%" PRIu32 ";\n", label);
if (EXTRA_BRACES || opcode == WasmOpcode_br_if) {
FuncGen_indent(&fg, out);
fputs("}\n", out);
}
if (opcode == WasmOpcode_br) unreachable_depth += 1;
}
break;
}
case WasmOpcode_br_table: {
if (unreachable_depth == 0) {
FuncGen_indent(&fg, out);
fprintf(out, "switch (l%" PRIu32 ") {\n", FuncGen_stackPop(&fg));
}
uint32_t label_len = InputStream_readLeb128_u32(&in);
for (uint32_t i = 0; i < label_len; i += 1) {
uint32_t label = InputStream_readLeb128_u32(&in);
if (unreachable_depth == 0) {
FuncGen_indent(&fg, out);
fprintf(out, "case %u: goto l%" PRIu32 ";\n",
i, FuncGen_blockLabel(&fg, label));
}
}
uint32_t label = InputStream_readLeb128_u32(&in);
if (unreachable_depth == 0) {
FuncGen_indent(&fg, out);
fprintf(out, "default: goto l%" PRIu32 ";\n",
FuncGen_blockLabel(&fg, label));
FuncGen_indent(&fg, out);
fputs("}\n", out);
unreachable_depth += 1;
}
break;
}
case WasmOpcode_return:
if (unreachable_depth == 0) {
FuncGen_indent(&fg, out);
fputs("return", out);
switch (func_type->result->len) {
case 0: break;
case 1: fprintf(out, " l%" PRIu32, FuncGen_stackPop(&fg)); break;
default: panic("multiple function returns not supported");
}
fputs(";\n", out);
unreachable_depth += 1;
}
break;
case WasmOpcode_call:
case WasmOpcode_call_indirect: {
uint32_t func_id;
uint32_t type_idx;
uint32_t table_idx;
switch (opcode) {
case WasmOpcode_call:
func_id = InputStream_readLeb128_u32(&in);
if (func_id < imports_len)
type_idx = imports[func_id].type_idx;
else
type_idx = funcs[func_id - imports_len].type_idx;
break;
case WasmOpcode_call_indirect:
type_idx = InputStream_readLeb128_u32(&in);
table_idx = InputStream_readLeb128_u32(&in);
func_id = FuncGen_stackPop(&fg);
break;
}
if (unreachable_depth == 0) {
const struct FuncType *callee_func_type = &types[type_idx];
for (uint32_t param_i = callee_func_type->param->len; param_i > 0; ) {
param_i -= 1;
param_stash[param_i] = FuncGen_stackPop(&fg);
}
switch (callee_func_type->result->len) {
case 0: FuncGen_indent(&fg, out); break;
case 1: FuncGen_stackPush(&fg, out, callee_func_type->result->types[0]); break;
default: panic("multiple function returns not supported");
}
switch (opcode) {
case WasmOpcode_call:
if (func_id < imports_len)
fprintf(out, "%s_%s", imports[func_id].mod, imports[func_id].name);
else
fprintf(out, "f%" PRIu32, func_id - imports_len);
break;
case WasmOpcode_call_indirect:
fputs("(*(", out);
switch (callee_func_type->result->len) {
case 0: fputs("void", out); break;
case 1: fputs(WasmValType_toC(callee_func_type->result->types[0]), out); break;
default: panic("multiple function returns not supported");
}
fputs(" (*)(", out);
if (callee_func_type->param->len == 0) fputs("void", out);
for (uint32_t param_i = 0; param_i < callee_func_type->param->len; param_i += 1) {
if (param_i > 0) fputs(", ", out);
fputs(WasmValType_toC(callee_func_type->param->types[param_i]), out);
}
fprintf(out, "))t%" PRIu32 "[l%" PRIu32 "])", table_idx, func_id);
break;
}
fputc('(', out);
for (uint32_t param_i = 0; param_i < callee_func_type->param->len;
param_i += 1) {
if (param_i > 0) fputs(", ", out);
fprintf(out, "l%" PRIu32, param_stash[param_i]);
}
fputs(");\n", out);
}
break;
}
case WasmOpcode_drop:
if (unreachable_depth == 0) {
FuncGen_indent(&fg, out);
fprintf(out, "(void)l%" PRIu32 ";\n", FuncGen_stackPop(&fg));
}
break;
case WasmOpcode_select: