-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtccwasm.c
More file actions
2243 lines (2037 loc) · 78.8 KB
/
Copy pathtccwasm.c
File metadata and controls
2243 lines (2037 loc) · 78.8 KB
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
/*
* wasm module output for TCC (restricted backend)
*/
#define USING_GLOBALS
#include "tcc.h"
#include "wasm-backend.h"
typedef struct WasmBuf {
unsigned char *data;
int len;
int cap;
} WasmBuf;
typedef struct WasmMemLayout {
int rodata_base;
int data_base;
int bss_base;
int stack_top;
} WasmMemLayout;
static WasmMemLayout wasm_layout;
static Section *wasm_sec_text;
static Section *wasm_sec_data;
static Section *wasm_sec_rodata;
static Section *wasm_sec_bss;
typedef struct WasmSig {
unsigned char ret_type;
unsigned char nb_params;
unsigned char param_types[WASM_MAX_CALL_ARGS];
} WasmSig;
typedef struct WasmImportFunc {
char *name;
WasmSig sig;
int type_index;
} WasmImportFunc;
static WasmImportFunc *wasm_import_funcs;
static int wasm_nb_import_funcs;
static int wasm_cap_import_funcs;
static int wasm_find_import_index_by_name(const char *name);
static int wasm_align_up(int v, int a)
{
return (v + a - 1) & -a;
}
static void wb_reserve(WasmBuf *b, int add)
{
int need = b->len + add;
int n;
if (need <= b->cap)
return;
n = b->cap ? b->cap : 256;
while (n < need)
n = n * 2;
b->data = tcc_realloc(b->data, n);
b->cap = n;
}
static void wb_u8(WasmBuf *b, int v)
{
wb_reserve(b, 1);
b->data[b->len++] = (unsigned char)v;
}
static void wb_mem(WasmBuf *b, const void *p, int n)
{
wb_reserve(b, n);
memcpy(b->data + b->len, p, n);
b->len += n;
}
static void wb_uleb(WasmBuf *b, unsigned v)
{
do {
unsigned c = v & 0x7f;
v >>= 7;
if (v)
c |= 0x80;
wb_u8(b, c);
} while (v);
}
static void wb_sleb(WasmBuf *b, int v)
{
int more = 1;
while (more) {
int c = v & 0x7f;
int sign = c & 0x40;
v >>= 7;
more = !((v == 0 && !sign) || (v == -1 && sign));
if (more)
c |= 0x80;
wb_u8(b, c);
}
}
static void wb_sleb64(WasmBuf *b, int64_t v)
{
int more = 1;
while (more) {
int c = (int)(v & 0x7f);
int sign = c & 0x40;
v >>= 7;
more = !((v == 0 && !sign) || (v == -1 && sign));
if (more)
c |= 0x80;
wb_u8(b, c);
}
}
static void wb_f64(WasmBuf *b, double x)
{
union {
double d;
unsigned char c[8];
} u;
u.d = x;
wb_mem(b, u.c, 8);
}
static int wasm_valtype_byte(int t)
{
switch (t) {
case WASM_VAL_I32: return 0x7f;
case WASM_VAL_I64: return 0x7e;
case WASM_VAL_F32: return 0x7d;
case WASM_VAL_F64: return 0x7c;
default:
tcc_error("wasm32 backend: invalid wasm value type %d", t);
return 0x40;
}
}
static void wb_local_get(WasmBuf *b, int idx) { wb_u8(b, 0x20), wb_uleb(b, idx); }
static void wb_local_set(WasmBuf *b, int idx) { wb_u8(b, 0x21), wb_uleb(b, idx); }
static void wb_local_tee(WasmBuf *b, int idx) { wb_u8(b, 0x22), wb_uleb(b, idx); }
static void wb_global_get(WasmBuf *b, int idx) { wb_u8(b, 0x23), wb_uleb(b, idx); }
static void wb_global_set(WasmBuf *b, int idx) { wb_u8(b, 0x24), wb_uleb(b, idx); }
static void wb_i32_const(WasmBuf *b, int v) { wb_u8(b, 0x41), wb_sleb(b, v); }
static void wb_i64_const(WasmBuf *b, int64_t v) { wb_u8(b, 0x42), wb_sleb64(b, v); }
static void wb_f64_const(WasmBuf *b, double v) { wb_u8(b, 0x44), wb_f64(b, v); }
static void wb_memarg(WasmBuf *b, int align_log2)
{
wb_uleb(b, align_log2);
wb_uleb(b, 0);
}
static int wasm_i32_reg_local(int reg, int local_i0)
{
if (reg < 0 || reg > 3)
tcc_error("wasm32 backend: invalid integer register %d", reg);
return local_i0 + reg;
}
static int wasm_f64_reg_local(int reg, int local_f0)
{
if (reg < 4 || reg > 7)
tcc_error("wasm32 backend: invalid floating register %d", reg);
return local_f0 + (reg - 4);
}
static int wasm_i32_bin_opcode(int op)
{
switch (op) {
case '+': return 0x6a;
case '-': return 0x6b;
case '*': return 0x6c;
case '/': return 0x6d;
case TOK_UDIV:
case TOK_PDIV: return 0x6e;
case '%': return 0x6f;
case TOK_UMOD: return 0x70;
case '&': return 0x71;
case '|': return 0x72;
case '^': return 0x73;
case TOK_SHL: return 0x74;
case TOK_SAR: return 0x75;
case TOK_SHR: return 0x76;
}
tcc_error("wasm32 backend: unsupported i32 binop token %d", op);
return 0x6a;
}
static int wasm_i32_cmp_opcode(int op)
{
switch (op) {
case TOK_EQ: return 0x46;
case TOK_NE: return 0x47;
case TOK_LT: return 0x48;
case TOK_ULT: return 0x49;
case TOK_GT: return 0x4a;
case TOK_UGT: return 0x4b;
case TOK_LE: return 0x4c;
case TOK_ULE: return 0x4d;
case TOK_GE: return 0x4e;
case TOK_UGE: return 0x4f;
}
tcc_error("wasm32 backend: unsupported i32 cmp token %d", op);
return 0x46;
}
static int wasm_f32_cmp_opcode(int op)
{
switch (op) {
case TOK_EQ: return 0x5b;
case TOK_NE: return 0x5c;
case TOK_LT:
case TOK_ULT: return 0x5d;
case TOK_GT:
case TOK_UGT: return 0x5e;
case TOK_LE:
case TOK_ULE: return 0x5f;
case TOK_GE:
case TOK_UGE: return 0x60;
}
tcc_error("wasm32 backend: unsupported f32 cmp token %d", op);
return 0x5b;
}
static int wasm_f64_cmp_opcode(int op)
{
switch (op) {
case TOK_EQ: return 0x61;
case TOK_NE: return 0x62;
case TOK_LT:
case TOK_ULT: return 0x63;
case TOK_GT:
case TOK_UGT: return 0x64;
case TOK_LE:
case TOK_ULE: return 0x65;
case TOK_GE:
case TOK_UGE: return 0x66;
}
tcc_error("wasm32 backend: unsupported f64 cmp token %d", op);
return 0x61;
}
static int wasm_f_bin_opcode(int op, int is_f32)
{
switch (op) {
case '+': return is_f32 ? 0x92 : 0xa0;
case '-': return is_f32 ? 0x93 : 0xa1;
case '*': return is_f32 ? 0x94 : 0xa2;
case '/': return is_f32 ? 0x95 : 0xa3;
}
tcc_error("wasm32 backend: unsupported floating binop token %d", op);
return is_f32 ? 0x92 : 0xa0;
}
static int wasm_find_func_index_by_tok(int tok)
{
int i;
for (i = 0; i < tcc_wasm_nb_funcs; ++i) {
if (tcc_wasm_funcs[i].sym_tok == tok)
return i;
}
return -1;
}
static int wasm_find_func_index_by_sym_index(int sym_index)
{
int i;
for (i = 0; i < tcc_wasm_nb_funcs; ++i) {
if (tcc_wasm_funcs[i].sym_index == sym_index)
return i;
}
return -1;
}
static int wasm_find_func_index_by_name(const char *name)
{
int i;
if (!name || !*name)
return -1;
for (i = 0; i < tcc_wasm_nb_funcs; ++i) {
const char *fname = tcc_wasm_funcs[i].name;
if (fname && !strcmp(fname, name))
return i;
}
return -1;
}
static int wasm_find_defined_sym_index_by_name(const char *name)
{
ElfSym *symtab, *es;
Section *strsec;
int i, n, best = -1;
if (!name || !*name || !symtab_section || !symtab_section->data)
return -1;
strsec = symtab_section->link;
if (!strsec || !strsec->data)
return -1;
symtab = (ElfSym *)symtab_section->data;
n = symtab_section->data_offset / sizeof(ElfSym);
for (i = 1; i < n; ++i) {
const char *sname;
es = &symtab[i];
if (es->st_shndx == SHN_UNDEF || es->st_name >= strsec->data_offset)
continue;
sname = (const char *)strsec->data + es->st_name;
if (strcmp(sname, name))
continue;
if (ELFW(ST_TYPE)(es->st_info) != STT_SECTION)
return i;
if (best < 0)
best = i;
}
return best;
}
static int wasm_func_ptr_value_from_sym_index(int sym_index, const char *name, int addend)
{
int fi;
int ii;
if (addend)
tcc_error("wasm32 backend: function pointer arithmetic is not supported");
fi = wasm_find_func_index_by_sym_index(sym_index);
if (fi < 0)
fi = wasm_find_func_index_by_name(name);
if (fi >= 0)
return fi + 1; /* keep table index 0 as null */
ii = wasm_find_import_index_by_name(name);
if (ii >= 0)
return tcc_wasm_nb_funcs + ii + 1; /* imported funcs follow defined funcs in table */
if (fi < 0)
tcc_error("wasm32 backend: unresolved function symbol '%s'",
name ? name : "?");
return 1;
}
static void wasm_push_i64_from_i32_pair(WasmBuf *b, int lo_local, int hi_local)
{
wb_local_get(b, lo_local);
wb_u8(b, 0xad); /* i64.extend_i32_u */
wb_local_get(b, hi_local);
wb_u8(b, 0xad); /* i64.extend_i32_u */
wb_i64_const(b, 32);
wb_u8(b, 0x86); /* i64.shl */
wb_u8(b, 0x84); /* i64.or */
}
static void wasm_store_i64_to_i32_pair(WasmBuf *b, int local_tmp64, int lo_local, int hi_local)
{
wb_local_tee(b, local_tmp64);
wb_u8(b, 0xa7); /* i32.wrap_i64 */
wb_local_set(b, lo_local);
wb_local_get(b, local_tmp64);
wb_i64_const(b, 32);
wb_u8(b, 0x88); /* i64.shr_u */
wb_u8(b, 0xa7); /* i32.wrap_i64 */
wb_local_set(b, hi_local);
}
static int wasm_sym_addr_from_elfsym(int sym_index, int addend)
{
ElfSym *es;
ElfSym *symtab;
Section *strsec;
const char *name = NULL;
int sh;
int base = 0;
if (sym_index <= 0)
return addend;
if (!symtab_section || !symtab_section->data)
tcc_error("wasm32 backend: missing symbol table for relocation");
if ((unsigned)sym_index >= (unsigned)(symtab_section->data_offset / sizeof(ElfSym)))
tcc_error("wasm32 backend: bad relocation symbol index %d", sym_index);
symtab = (ElfSym *)symtab_section->data;
es = &symtab[sym_index];
sh = es->st_shndx;
strsec = symtab_section->link;
if (strsec && strsec->data && es->st_name < strsec->data_offset)
name = (const char *)strsec->data + es->st_name;
if (sh == SHN_UNDEF || (wasm_sec_text && sh == wasm_sec_text->sh_num))
return wasm_func_ptr_value_from_sym_index(sym_index, name, addend);
if (wasm_sec_rodata && sh == wasm_sec_rodata->sh_num)
base = wasm_layout.rodata_base;
else if (wasm_sec_data && sh == wasm_sec_data->sh_num)
base = wasm_layout.data_base;
else if (wasm_sec_bss && sh == wasm_sec_bss->sh_num)
base = wasm_layout.bss_base;
else if (sh == SHN_ABS)
return (int)es->st_value + addend;
else
tcc_error("wasm32 backend: unsupported relocation section for symbol '%s'",
name ? name : "?");
return base + (int)es->st_value + addend;
}
static int wasm_sym_addr_from_tok(int tok, const char *name, int addend)
{
int si;
if (tok < TOK_IDENT)
return addend;
if (!name || !*name)
tcc_error("wasm32 backend: unresolved symbol token %d", tok);
si = wasm_find_defined_sym_index_by_name(name);
if (si > 0)
return wasm_sym_addr_from_elfsym(si, addend);
return wasm_func_ptr_value_from_sym_index(0, name, addend);
}
static int wasm_sym_addr_from_op(WasmOp *op)
{
if (op->sym_index > 0)
return wasm_sym_addr_from_elfsym(op->sym_index, op->imm);
if (op->sym_tok >= TOK_IDENT)
return wasm_sym_addr_from_tok(op->sym_tok, op->sym_name, op->imm);
tcc_error("wasm32 backend: unresolved symbol reference in IR");
return 0;
}
static void wasm_apply_data_relocs(Section *s)
{
Section *sr;
ElfW_Rel *rel;
int i, n;
if (!s || !s->data)
return;
sr = s->reloc;
if (!sr || !sr->data)
return;
n = sr->data_offset / sizeof(ElfW_Rel);
for (i = 0; i < n; ++i) {
unsigned long off;
unsigned char *ptr;
int sym_index;
int addend;
int value;
rel = (ElfW_Rel *)sr->data + i;
off = rel->r_offset;
if (off + 4 > (unsigned long)s->data_offset)
tcc_error("wasm32 backend: relocation offset out of range in section '%s'", s->name);
ptr = s->data + off;
sym_index = ELFW(R_SYM)(rel->r_info);
#if SHT_RELX == SHT_RELA
addend = rel->r_addend;
#else
addend = (int)(int32_t)read32le(ptr);
#endif
value = wasm_sym_addr_from_elfsym(sym_index, addend);
write32le(ptr, value);
}
}
static int wasm_sig_matches_func(const WasmSig *sig, const WasmFuncIR *f)
{
if (sig->ret_type != f->ret_type || sig->nb_params != f->nb_params)
return 0;
return !memcmp(sig->param_types, f->param_types, f->nb_params);
}
static int wasm_sig_matches_sig(const WasmSig *a, const WasmSig *b)
{
if (a->ret_type != b->ret_type || a->nb_params != b->nb_params)
return 0;
return !memcmp(a->param_types, b->param_types, a->nb_params);
}
static void wasm_sig_from_call(WasmSig *sig, WasmOp *op)
{
sig->ret_type = op->type;
sig->nb_params = op->call_nb_args;
memcpy(sig->param_types, op->call_arg_type, op->call_nb_args);
}
static int wasm_find_or_add_type_index(const WasmSig *wanted,
WasmSig **extra_sigs,
int *cap_extra_sigs,
int *nb_extra_sigs)
{
int j;
for (j = 0; j < tcc_wasm_nb_funcs; ++j) {
WasmFuncIR *g = &tcc_wasm_funcs[j];
if (g->nb_params <= WASM_MAX_CALL_ARGS && wasm_sig_matches_func(wanted, g))
return j;
}
for (j = 0; j < *nb_extra_sigs; ++j) {
if (wasm_sig_matches_sig(wanted, &(*extra_sigs)[j]))
return tcc_wasm_nb_funcs + j;
}
if (*nb_extra_sigs >= *cap_extra_sigs) {
int n = *cap_extra_sigs ? (*cap_extra_sigs * 2) : 8;
WasmSig *next = tcc_realloc(*extra_sigs, n * sizeof(*next));
if (!next) {
tcc_error_noabort("wasm32 backend: out of memory while growing type signatures");
return -1;
}
*extra_sigs = next;
*cap_extra_sigs = n;
}
(*extra_sigs)[*nb_extra_sigs] = *wanted;
j = *nb_extra_sigs;
(*nb_extra_sigs)++;
return tcc_wasm_nb_funcs + j;
}
static int wasm_find_import_index_by_name(const char *name)
{
int i;
if (!name || !*name)
return -1;
for (i = 0; i < wasm_nb_import_funcs; ++i) {
if (!strcmp(wasm_import_funcs[i].name, name))
return i;
}
return -1;
}
static void wasm_free_imports(void)
{
int i;
for (i = 0; i < wasm_nb_import_funcs; ++i)
tcc_free(wasm_import_funcs[i].name);
tcc_free(wasm_import_funcs);
wasm_import_funcs = NULL;
wasm_nb_import_funcs = 0;
wasm_cap_import_funcs = 0;
}
static int wasm_find_or_add_import(const char *name, const WasmSig *sig, int type_index)
{
int i = wasm_find_import_index_by_name(name);
if (i >= 0) {
if (!wasm_sig_matches_sig(&wasm_import_funcs[i].sig, sig)) {
tcc_error_noabort("wasm32 backend: conflicting signatures for import '%s'", name);
return -1;
}
return i;
}
if (wasm_nb_import_funcs >= wasm_cap_import_funcs) {
int n = wasm_cap_import_funcs ? (wasm_cap_import_funcs * 2) : 8;
WasmImportFunc *next = tcc_realloc(wasm_import_funcs, n * sizeof(*next));
if (!next) {
tcc_error_noabort("wasm32 backend: out of memory while growing import table");
return -1;
}
wasm_import_funcs = next;
wasm_cap_import_funcs = n;
}
i = wasm_nb_import_funcs++;
wasm_import_funcs[i].name = tcc_strdup(name);
if (!wasm_import_funcs[i].name) {
tcc_error_noabort("wasm32 backend: out of memory while copying import name");
return -1;
}
wasm_import_funcs[i].sig = *sig;
wasm_import_funcs[i].type_index = type_index;
return i;
}
static int wasm_is_libcall_token(int tok)
{
switch (tok) {
case TOK___divdi3:
case TOK___udivdi3:
case TOK___moddi3:
case TOK___umoddi3:
case TOK___ashldi3:
case TOK___lshrdi3:
case TOK___ashrdi3:
case TOK___floatundisf:
case TOK___floatundidf:
case TOK___floatundixf:
case TOK___fixunssfdi:
case TOK___fixunsdfdi:
case TOK___fixunsxfdi:
return 1;
default:
return 0;
}
}
static int wasm_collect_call_metadata(WasmSig **extra_sigs,
int *cap_extra_sigs,
int *nb_extra_sigs,
int *has_indirect_calls)
{
int i, k;
for (i = 0; i < tcc_wasm_nb_funcs; ++i) {
WasmFuncIR *f = &tcc_wasm_funcs[i];
for (k = 0; k < f->nb_ops; ++k) {
WasmOp *op = &f->ops[k];
WasmSig wanted;
int ti;
if (op->kind == WASM_OP_CALL_INDIRECT) {
*has_indirect_calls = 1;
wasm_sig_from_call(&wanted, op);
ti = wasm_find_or_add_type_index(&wanted, extra_sigs, cap_extra_sigs, nb_extra_sigs);
if (ti < 0)
return -1;
op->imm = ti;
continue;
}
if (op->kind != WASM_OP_CALL)
continue;
if (wasm_find_func_index_by_tok(op->call_tok) >= 0)
continue;
if (wasm_is_libcall_token(op->call_tok))
continue;
if (!op->call_name || !*op->call_name) {
tcc_error_noabort("wasm32 backend: unresolved direct call token %d", op->call_tok);
return -1;
}
wasm_sig_from_call(&wanted, op);
ti = wasm_find_or_add_type_index(&wanted, extra_sigs, cap_extra_sigs, nb_extra_sigs);
if (ti < 0)
return -1;
if (wasm_find_or_add_import(op->call_name, &wanted, ti) < 0)
return -1;
}
}
return 0;
}
static int wasm_pc_to_index(WasmFuncIR *f, int pc)
{
int idx = pc - f->start_pc;
if (pc == f->end_pc)
return f->nb_ops;
if (idx < 0 || idx >= f->nb_ops)
tcc_error("wasm32 backend: bad jump target pc=%d (range %d..%d)",
pc, f->start_pc, f->end_pc);
return idx;
}
static void wasm_emit_addr(WasmBuf *b, WasmOp *op, int local_fp, int local_i0)
{
switch (op->flags & 0x00ff) {
case WASM_ADDR_REG:
wb_local_get(b, wasm_i32_reg_local(op->r1, local_i0));
if (op->imm)
wb_i32_const(b, op->imm), wb_u8(b, 0x6a);
break;
case WASM_ADDR_FP:
wb_local_get(b, local_fp);
if (op->imm)
wb_i32_const(b, op->imm), wb_u8(b, 0x6a);
break;
case WASM_ADDR_ABS:
wb_i32_const(b, op->imm);
break;
case WASM_ADDR_SYM:
wb_i32_const(b, wasm_sym_addr_from_op(op));
break;
default:
tcc_error("wasm32 backend: invalid address mode %u", op->flags & 0x00ff);
}
}
static void wasm_emit_call_arg(WasmBuf *b, WasmOp *op, int i, int local_fp, int local_i0, int local_f0)
{
int at = op->call_arg_type[i];
int ar = op->call_arg_reg[i];
if (ar == WASM_ARG_STACK) {
wb_local_get(b, local_fp);
if (op->call_arg_off[i])
wb_i32_const(b, op->call_arg_off[i]), wb_u8(b, 0x6a);
if (at == WASM_VAL_I32)
wb_u8(b, 0x28), wb_memarg(b, 2);
else if (at == WASM_VAL_I64)
wb_u8(b, 0x29), wb_memarg(b, 3);
else if (at == WASM_VAL_F32)
wb_u8(b, 0x2a), wb_memarg(b, 2);
else if (at == WASM_VAL_F64)
wb_u8(b, 0x2b), wb_memarg(b, 3);
else
tcc_error("wasm32 backend: invalid call argument type %d", at);
return;
}
if (at == WASM_VAL_I32)
wb_local_get(b, wasm_i32_reg_local(ar, local_i0));
else if (at == WASM_VAL_I64) {
int ah = op->call_arg_hi[i];
if (ah >= VT_CONST)
tcc_error("wasm32 backend: malformed i64 call argument");
wasm_push_i64_from_i32_pair(b,
wasm_i32_reg_local(ar, local_i0),
wasm_i32_reg_local(ah, local_i0));
} else if (at == WASM_VAL_F32)
wb_local_get(b, wasm_f64_reg_local(ar, local_f0)), wb_u8(b, 0xb6);
else if (at == WASM_VAL_F64)
wb_local_get(b, wasm_f64_reg_local(ar, local_f0));
else
tcc_error("wasm32 backend: invalid call argument type %d", at);
}
static int wasm_emit_libcall(WasmBuf *b, WasmOp *op,
int local_fp, int local_i0, int local_f0, int local_tmp64)
{
int tok = op->call_tok;
switch (tok) {
case TOK___divdi3:
case TOK___udivdi3:
case TOK___moddi3:
case TOK___umoddi3:
if (op->call_nb_args != 2)
tcc_error("wasm32 backend: invalid helper arity for token %d", tok);
wasm_emit_call_arg(b, op, 0, local_fp, local_i0, local_f0);
wasm_emit_call_arg(b, op, 1, local_fp, local_i0, local_f0);
if (tok == TOK___divdi3) wb_u8(b, 0x7f); /* i64.div_s */
else if (tok == TOK___udivdi3) wb_u8(b, 0x80);/* i64.div_u */
else if (tok == TOK___moddi3) wb_u8(b, 0x81); /* i64.rem_s */
else wb_u8(b, 0x82); /* i64.rem_u */
wasm_store_i64_to_i32_pair(b, local_tmp64,
wasm_i32_reg_local(REG_IRET, local_i0),
wasm_i32_reg_local(REG_IRE2, local_i0));
return 1;
case TOK___ashldi3:
case TOK___lshrdi3:
case TOK___ashrdi3:
if (op->call_nb_args != 2)
tcc_error("wasm32 backend: invalid helper arity for token %d", tok);
wasm_emit_call_arg(b, op, 0, local_fp, local_i0, local_f0); /* i64 */
wasm_emit_call_arg(b, op, 1, local_fp, local_i0, local_f0); /* i32 */
if (op->call_arg_type[1] != WASM_VAL_I64)
wb_u8(b, 0xad); /* i64.extend_i32_u */
if (tok == TOK___ashldi3) wb_u8(b, 0x86); /* i64.shl */
else if (tok == TOK___lshrdi3) wb_u8(b, 0x88); /* i64.shr_u */
else wb_u8(b, 0x87); /* i64.shr_s */
wasm_store_i64_to_i32_pair(b, local_tmp64,
wasm_i32_reg_local(REG_IRET, local_i0),
wasm_i32_reg_local(REG_IRE2, local_i0));
return 1;
case TOK___floatundisf:
if (op->call_nb_args != 1)
tcc_error("wasm32 backend: invalid helper arity for token %d", tok);
wasm_emit_call_arg(b, op, 0, local_fp, local_i0, local_f0);
wb_u8(b, 0xb5); /* f32.convert_i64_u */
wb_u8(b, 0xbb); /* f64.promote_f32 */
wb_local_set(b, wasm_f64_reg_local(REG_FRET, local_f0));
return 1;
case TOK___floatundidf:
case TOK___floatundixf:
if (op->call_nb_args != 1)
tcc_error("wasm32 backend: invalid helper arity for token %d", tok);
wasm_emit_call_arg(b, op, 0, local_fp, local_i0, local_f0);
wb_u8(b, 0xba); /* f64.convert_i64_u */
wb_local_set(b, wasm_f64_reg_local(REG_FRET, local_f0));
return 1;
case TOK___fixunssfdi:
case TOK___fixunsdfdi:
case TOK___fixunsxfdi:
if (op->call_nb_args != 1)
tcc_error("wasm32 backend: invalid helper arity for token %d", tok);
if (tok == TOK___fixunssfdi && op->call_arg_type[0] == WASM_VAL_F32) {
wasm_emit_call_arg(b, op, 0, local_fp, local_i0, local_f0);
wb_u8(b, 0xaf); /* i64.trunc_f32_u */
} else {
wasm_emit_call_arg(b, op, 0, local_fp, local_i0, local_f0);
wb_u8(b, 0xb1); /* i64.trunc_f64_u */
}
wasm_store_i64_to_i32_pair(b, local_tmp64,
wasm_i32_reg_local(REG_IRET, local_i0),
wasm_i32_reg_local(REG_IRE2, local_i0));
return 1;
}
return 0;
}
/* Returns the wasm local index that op will read first, or -1 if unknown.
* Used by peephole optimization to chain values on the wasm stack. */
static int wasm_op_first_input(WasmOp *op, int local_i0, int local_f0)
{
switch (op->kind) {
/* Ops that read r0 (dst) as first operand — integer */
case WASM_OP_I32_BIN:
case WASM_OP_I32_ADDC:
case WASM_OP_I32_SUBC:
case WASM_OP_UMULL_U32:
case WASM_OP_SET_CMP_I32:
if (op->r0 < 0 || op->r0 > 3) return -1;
return wasm_i32_reg_local(op->r0, local_i0);
/* Ops that read r0 (dst) as first operand — float */
case WASM_OP_F64_BIN:
case WASM_OP_F64_NEG:
case WASM_OP_SET_CMP_F64:
case WASM_OP_F32_BIN:
case WASM_OP_F32_NEG:
case WASM_OP_SET_CMP_F32:
case WASM_OP_FTOF_TO_F32:
if (op->r0 < 4 || op->r0 > 7) return -1;
return wasm_f64_reg_local(op->r0, local_f0);
/* Ops that read r1 as first operand (conversions: output to r0, input from r1) */
case WASM_OP_ITOF_F32:
case WASM_OP_ITOF_F64:
if (op->r1 < 0 || op->r1 > 3) return -1;
return wasm_i32_reg_local(op->r1, local_i0);
case WASM_OP_FTOI_I32:
case WASM_OP_FTOI_I64:
if (op->r1 < 4 || op->r1 > 7) return -1;
return wasm_f64_reg_local(op->r1, local_f0);
/* MOV reads r1 */
case WASM_OP_MOV_I32:
if (op->r1 < 0 || op->r1 > 3) return -1;
return wasm_i32_reg_local(op->r1, local_i0);
case WASM_OP_MOV_F64:
if (op->r1 < 4 || op->r1 > 7) return -1;
return wasm_f64_reg_local(op->r1, local_f0);
default:
return -1;
}
}
static void wasm_emit_case(WasmBuf *b, WasmFuncIR *f, WasmOp *op,
int case_index, int loop_depth, int cur_block,
int local_pc, int local_fp, int local_cmp, int local_carry,
int local_i0, int local_f0, int local_tmp64,
int *op_to_block, int nb_blocks, int emit_dispatch,
int stack_reg, int next_first_input, int *p_stack_out)
{
int next_index = case_index + 1;
int dst, src, target_index;
*p_stack_out = -1;
/* Peephole: emit local.tee (keep on stack) instead of local.set when
* the next op in the same block will read this local first. */
#define WB_SET_OR_TEE(buf, local) do { \
if ((local) == next_first_input && next_first_input >= 0) { \
wb_local_tee(buf, local); \
*p_stack_out = local; \
} else { \
wb_local_set(buf, local); \
} \
} while (0)
/* Peephole: skip local.get if the value is already on the wasm stack
* from the previous op's local.tee. */
#define WB_GET_OR_SKIP(buf, local) do { \
if ((local) == stack_reg && stack_reg >= 0) \
stack_reg = -2; /* consumed */ \
else \
wb_local_get(buf, local); \
} while (0)
if (next_index > f->nb_ops)
next_index = f->nb_ops;
switch (op->kind) {
case WASM_OP_I32_CONST:
dst = wasm_i32_reg_local(op->r0, local_i0);
wb_i32_const(b, op->imm);
WB_SET_OR_TEE(b, dst);
break;
case WASM_OP_F64_CONST:
dst = wasm_f64_reg_local(op->r0, local_f0);
wb_f64_const(b, op->f64);
WB_SET_OR_TEE(b, dst);
break;
case WASM_OP_MOV_I32:
dst = wasm_i32_reg_local(op->r0, local_i0);
src = wasm_i32_reg_local(op->r1, local_i0);
WB_GET_OR_SKIP(b, src);
WB_SET_OR_TEE(b, dst);
break;
case WASM_OP_MOV_F64:
dst = wasm_f64_reg_local(op->r0, local_f0);
src = wasm_f64_reg_local(op->r1, local_f0);
WB_GET_OR_SKIP(b, src);
WB_SET_OR_TEE(b, dst);
break;
case WASM_OP_ADDR_LOCAL:
dst = wasm_i32_reg_local(op->r0, local_i0);
wb_local_get(b, local_fp);
if (op->imm)
wb_i32_const(b, op->imm), wb_u8(b, 0x6a);
WB_SET_OR_TEE(b, dst);
break;
case WASM_OP_ADDR_SYM:
dst = wasm_i32_reg_local(op->r0, local_i0);
wb_i32_const(b, wasm_sym_addr_from_op(op));
WB_SET_OR_TEE(b, dst);
break;
case WASM_OP_LOAD_I32:
case WASM_OP_LOAD_S8:
case WASM_OP_LOAD_U8:
case WASM_OP_LOAD_S16:
case WASM_OP_LOAD_U16:
dst = wasm_i32_reg_local(op->r0, local_i0);
wasm_emit_addr(b, op, local_fp, local_i0);
switch (op->kind) {
case WASM_OP_LOAD_I32: wb_u8(b, 0x28), wb_memarg(b, 2); break;
case WASM_OP_LOAD_S8: wb_u8(b, 0x2c), wb_memarg(b, 0); break;
case WASM_OP_LOAD_U8: wb_u8(b, 0x2d), wb_memarg(b, 0); break;
case WASM_OP_LOAD_S16: wb_u8(b, 0x2e), wb_memarg(b, 1); break;
default: wb_u8(b, 0x2f), wb_memarg(b, 1); break;
}
WB_SET_OR_TEE(b, dst);
break;
case WASM_OP_LOAD_F32:
dst = wasm_f64_reg_local(op->r0, local_f0);
wasm_emit_addr(b, op, local_fp, local_i0);
wb_u8(b, 0x2a), wb_memarg(b, 2);
wb_u8(b, 0xbb); /* f64.promote_f32 */
WB_SET_OR_TEE(b, dst);
break;
case WASM_OP_LOAD_F64:
dst = wasm_f64_reg_local(op->r0, local_f0);
wasm_emit_addr(b, op, local_fp, local_i0);
wb_u8(b, 0x2b), wb_memarg(b, 3);
WB_SET_OR_TEE(b, dst);
break;
case WASM_OP_STORE_I32:
case WASM_OP_STORE_I64:
case WASM_OP_STORE_I8:
case WASM_OP_STORE_I16:
src = wasm_i32_reg_local(op->r0, local_i0);
wasm_emit_addr(b, op, local_fp, local_i0);
if (op->kind == WASM_OP_STORE_I64) {
wasm_push_i64_from_i32_pair(b, src, wasm_i32_reg_local(op->r2, local_i0));
wb_u8(b, 0x37), wb_memarg(b, 3);
} else {
wb_local_get(b, src);
if (op->kind == WASM_OP_STORE_I32)
wb_u8(b, 0x36), wb_memarg(b, 2);
else if (op->kind == WASM_OP_STORE_I8)
wb_u8(b, 0x3a), wb_memarg(b, 0);
else
wb_u8(b, 0x3b), wb_memarg(b, 1);
}
break;
case WASM_OP_STORE_F32:
src = wasm_f64_reg_local(op->r0, local_f0);
wasm_emit_addr(b, op, local_fp, local_i0);
wb_local_get(b, src);
wb_u8(b, 0xb6); /* f32.demote_f64 */
wb_u8(b, 0x38), wb_memarg(b, 2);
break;
case WASM_OP_STORE_F64:
src = wasm_f64_reg_local(op->r0, local_f0);
wasm_emit_addr(b, op, local_fp, local_i0);
wb_local_get(b, src);
wb_u8(b, 0x39), wb_memarg(b, 3);
break;
case WASM_OP_I32_BIN:
dst = wasm_i32_reg_local(op->r0, local_i0);
WB_GET_OR_SKIP(b, dst);
if (op->flags & WASM_OP_FLAG_IMM)
wb_i32_const(b, op->imm);
else
wb_local_get(b, wasm_i32_reg_local(op->r1, local_i0));
wb_u8(b, wasm_i32_bin_opcode(op->op));
WB_SET_OR_TEE(b, dst);
break;
case WASM_OP_I32_ADDC:
case WASM_OP_I32_SUBC:
dst = wasm_i32_reg_local(op->r0, local_i0);
WB_GET_OR_SKIP(b, dst);
wb_u8(b, 0xad); /* i64.extend_i32_u */
if (op->flags & WASM_OP_FLAG_IMM)
wb_i32_const(b, op->imm);
else
wb_local_get(b, wasm_i32_reg_local(op->r1, local_i0));
wb_u8(b, 0xad); /* i64.extend_i32_u */