-
Notifications
You must be signed in to change notification settings - Fork 791
Expand file tree
/
Copy pathaot_compiler.c
More file actions
4350 lines (3879 loc) · 163 KB
/
aot_compiler.c
File metadata and controls
4350 lines (3879 loc) · 163 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
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "aot_compiler.h"
#include "aot_emit_compare.h"
#include "aot_emit_conversion.h"
#include "aot_emit_memory.h"
#include "aot_emit_variable.h"
#include "aot_emit_const.h"
#include "aot_emit_exception.h"
#include "aot_emit_numberic.h"
#include "aot_emit_control.h"
#include "aot_emit_function.h"
#include "aot_emit_parametric.h"
#include "aot_emit_table.h"
#include "aot_emit_gc.h"
#include "aot_stack_frame_comp.h"
#include "simd/simd_access_lanes.h"
#include "simd/simd_bitmask_extracts.h"
#include "simd/simd_bit_shifts.h"
#include "simd/simd_bitwise_ops.h"
#include "simd/simd_bool_reductions.h"
#include "simd/simd_comparisons.h"
#include "simd/simd_conversions.h"
#include "simd/simd_construct_values.h"
#include "simd/simd_conversions.h"
#include "simd/simd_floating_point.h"
#include "simd/simd_int_arith.h"
#include "simd/simd_load_store.h"
#include "simd/simd_sat_int_arith.h"
#include "../aot/aot_runtime.h"
#include "../interpreter/wasm_opcode.h"
#include <errno.h>
#if WASM_ENABLE_DEBUG_AOT != 0
#include "debug/dwarf_extractor.h"
#endif
#if WASM_ENABLE_STRINGREF != 0
#include "string_object.h"
#include "aot_emit_stringref.h"
#endif
#define CHECK_BUF(buf, buf_end, length) \
do { \
if (buf + length > buf_end) { \
aot_set_last_error("read leb failed: unexpected end."); \
return false; \
} \
} while (0)
static bool
read_leb(const uint8 *buf, const uint8 *buf_end, uint32 *p_offset,
uint32 maxbits, bool sign, uint64 *p_result)
{
uint64 result = 0;
uint32 shift = 0;
uint32 bcnt = 0;
uint64 byte;
while (true) {
CHECK_BUF(buf, buf_end, 1);
byte = buf[*p_offset];
*p_offset += 1;
result |= ((byte & 0x7f) << shift);
shift += 7;
if ((byte & 0x80) == 0) {
break;
}
bcnt += 1;
}
if (bcnt > (maxbits + 6) / 7) {
aot_set_last_error("read leb failed: "
"integer representation too long");
return false;
}
if (sign && (shift < maxbits) && (byte & 0x40)) {
/* Sign extend */
result |= (~((uint64)0)) << shift;
}
*p_result = result;
return true;
}
/* NOLINTNEXTLINE */
#define read_leb_generic(p, p_end, res, res_type, sign) \
do { \
uint32 off = 0; \
uint64 res64; \
if (!read_leb(p, p_end, &off, sizeof(res_type) << 3, sign, &res64)) \
return false; \
p += off; \
res = (res_type)res64; \
} while (0)
/* NOLINTNEXTLINE */
#define read_leb_int32(p, p_end, res) \
read_leb_generic(p, p_end, res, int32, true)
/* NOLINTNEXTLINE */
#define read_leb_int64(p, p_end, res) \
read_leb_generic(p, p_end, res, int64, true)
/* NOLINTNEXTLINE */
#define read_leb_uint32(p, p_end, res) \
read_leb_generic(p, p_end, res, uint32, false)
/* NOLINTNEXTLINE */
#define read_leb_uint64(p, p_end, res) \
read_leb_generic(p, p_end, res, uint64, false)
/* NOLINTNEXTLINE */
#if WASM_ENABLE_MEMORY64 != 0
#define read_leb_mem_offset(p, p_end, res) \
do { \
if (IS_MEMORY64) { \
read_leb_uint64(p, p_end, res); \
} \
else { \
read_leb_uint32(p, p_end, res); \
} \
} while (0)
#else
#define read_leb_mem_offset read_leb_uint32
#endif
/**
* Since wamrc uses a full feature Wasm loader,
* add a post-validator here to run checks according
* to options, like enable_tail_call, enable_ref_types,
* and so on.
*/
static bool
aot_validate_wasm(AOTCompContext *comp_ctx)
{
if (!comp_ctx->enable_ref_types && !comp_ctx->enable_gc) {
/* Doesn't support multiple tables unless enabling reference type */
if (comp_ctx->comp_data->import_table_count
+ comp_ctx->comp_data->table_count
> 1) {
aot_set_last_error("multiple tables");
return false;
}
}
#if WASM_ENABLE_MEMORY64 != 0
if (comp_ctx->pointer_size < sizeof(uint64)) {
if (IS_MEMORY64) {
aot_set_last_error("Compiling wasm64(contains i64 memory section) "
"to 32bit platform is not allowed");
return false;
}
for (uint32 i = 0; i < comp_ctx->comp_data->table_count; ++i) {
if (IS_TABLE64(i)) {
aot_set_last_error("Compiling wasm64(contains i64 table "
"section) to 32bit platform is not allowed");
return false;
}
}
}
#endif
return true;
}
#define COMPILE_ATOMIC_RMW(OP, NAME) \
case WASM_OP_ATOMIC_RMW_I32_##NAME: \
bytes = 4; \
op_type = VALUE_TYPE_I32; \
goto OP_ATOMIC_##OP; \
case WASM_OP_ATOMIC_RMW_I64_##NAME: \
bytes = 8; \
op_type = VALUE_TYPE_I64; \
goto OP_ATOMIC_##OP; \
case WASM_OP_ATOMIC_RMW_I32_##NAME##8_U: \
bytes = 1; \
op_type = VALUE_TYPE_I32; \
goto OP_ATOMIC_##OP; \
case WASM_OP_ATOMIC_RMW_I32_##NAME##16_U: \
bytes = 2; \
op_type = VALUE_TYPE_I32; \
goto OP_ATOMIC_##OP; \
case WASM_OP_ATOMIC_RMW_I64_##NAME##8_U: \
bytes = 1; \
op_type = VALUE_TYPE_I64; \
goto OP_ATOMIC_##OP; \
case WASM_OP_ATOMIC_RMW_I64_##NAME##16_U: \
bytes = 2; \
op_type = VALUE_TYPE_I64; \
goto OP_ATOMIC_##OP; \
case WASM_OP_ATOMIC_RMW_I64_##NAME##32_U: \
bytes = 4; \
op_type = VALUE_TYPE_I64; \
OP_ATOMIC_##OP : bin_op = LLVMAtomicRMWBinOp##OP; \
goto build_atomic_rmw;
uint32
offset_of_local_in_outs_area(AOTCompContext *comp_ctx, unsigned n)
{
AOTCompFrame *frame = comp_ctx->aot_frame;
return frame->cur_frame_size + offset_of_local(comp_ctx, n);
}
static bool
store_value(AOTCompContext *comp_ctx, LLVMValueRef value, uint8 value_type,
LLVMValueRef cur_frame, uint32 offset)
{
LLVMValueRef value_offset, value_addr, value_ptr = NULL, res;
LLVMTypeRef value_ptr_type = NULL;
if (!(value_offset = I32_CONST(offset))) {
aot_set_last_error("llvm build const failed");
return false;
}
if (!(value_addr =
LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, cur_frame,
&value_offset, 1, "value_addr"))) {
aot_set_last_error("llvm build in bounds gep failed");
return false;
}
switch (value_type) {
case VALUE_TYPE_I32:
value_ptr_type = INT32_PTR_TYPE;
break;
case VALUE_TYPE_I64:
value_ptr_type = INT64_PTR_TYPE;
break;
case VALUE_TYPE_F32:
value_ptr_type = F32_PTR_TYPE;
break;
case VALUE_TYPE_F64:
value_ptr_type = F64_PTR_TYPE;
break;
case VALUE_TYPE_V128:
value_ptr_type = V128_PTR_TYPE;
break;
#if WASM_ENABLE_GC != 0
case VALUE_TYPE_GC_REF:
value_ptr_type = GC_REF_PTR_TYPE;
break;
#endif
default:
bh_assert(0);
break;
}
if (!(value_ptr = LLVMBuildBitCast(comp_ctx->builder, value_addr,
value_ptr_type, "value_ptr"))) {
aot_set_last_error("llvm build bit cast failed");
return false;
}
if (!(res = LLVMBuildStore(comp_ctx->builder, value, value_ptr))) {
aot_set_last_error("llvm build store failed");
return false;
}
LLVMSetAlignment(res, 4);
return true;
}
void
aot_call_stack_features_init_default(AOTCallStackFeatures *features)
{
memset(features, 1, sizeof(AOTCallStackFeatures));
features->frame_per_function = false;
}
bool
aot_frame_store_value(AOTCompContext *comp_ctx, LLVMValueRef value,
uint8 value_type, LLVMValueRef cur_frame, uint32 offset)
{
return store_value(comp_ctx, value, value_type, cur_frame, offset);
}
static bool
store_ref(AOTCompContext *comp_ctx, uint32 ref, LLVMValueRef cur_frame,
uint32 offset, uint32 nbytes)
{
LLVMValueRef value_ref = NULL, value_offset, value_addr, res;
if (!(value_offset = I32_CONST(offset))) {
aot_set_last_error("llvm build const failed");
return false;
}
if (!(value_addr =
LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, cur_frame,
&value_offset, 1, "value_addr"))) {
aot_set_last_error("llvm build in bounds gep failed");
return false;
}
switch (nbytes) {
case 1:
if (!(value_ref = I8_CONST((uint8)ref))) {
aot_set_last_error("llvm build const failed");
}
break;
case 2:
ref = (ref << 8) | ref;
if (!(value_ref = LLVMConstInt(INT16_TYPE, (uint16)ref, false))) {
aot_set_last_error("llvm build const failed");
return false;
}
if (!(value_addr =
LLVMBuildBitCast(comp_ctx->builder, value_addr,
INT16_PTR_TYPE, "value_addr"))) {
aot_set_last_error("llvm build bit cast failed");
return false;
}
break;
case 4:
ref = (ref << 24) | (ref << 16) | (ref << 8) | ref;
if (!(value_ref = I32_CONST(ref))) {
aot_set_last_error("llvm build const failed");
return false;
}
if (!(value_addr =
LLVMBuildBitCast(comp_ctx->builder, value_addr,
INT32_PTR_TYPE, "value_addr"))) {
aot_set_last_error("llvm build bit cast failed");
return false;
}
break;
default:
bh_assert(0);
break;
}
if (!(res = LLVMBuildStore(comp_ctx->builder, value_ref, value_addr))) {
aot_set_last_error("llvm build store failed");
return false;
}
LLVMSetAlignment(res, 1);
return true;
}
bool
aot_gen_commit_values(AOTCompFrame *frame)
{
AOTCompContext *comp_ctx = frame->comp_ctx;
AOTFuncContext *func_ctx = frame->func_ctx;
AOTValueSlot *p, *end;
LLVMValueRef value;
uint32 n;
if (!frame->comp_ctx->call_stack_features.values) {
return true;
}
/* First, commit reference flags
* For LLVM JIT, iterate all local and stack ref flags
* For AOT, ignore local(params + locals) ref flags */
for (p = comp_ctx->is_jit_mode ? frame->lp
: frame->lp + frame->max_local_cell_num;
p < frame->sp; p++) {
if (!p->dirty)
continue;
n = (uint32)(p - frame->lp);
/* Commit reference flag */
if (comp_ctx->enable_gc) {
switch (p->type) {
case VALUE_TYPE_I32:
case VALUE_TYPE_F32:
case VALUE_TYPE_I1:
if (p->ref != p->committed_ref - 1) {
if (!store_ref(comp_ctx, p->ref, func_ctx->cur_frame,
offset_of_ref(comp_ctx, n), 1))
return false;
p->committed_ref = p->ref + 1;
}
break;
case VALUE_TYPE_I64:
case VALUE_TYPE_F64:
bh_assert(p->ref == (p + 1)->ref);
if (p->ref != p->committed_ref - 1
|| p->ref != (p + 1)->committed_ref - 1) {
if (!store_ref(comp_ctx, p->ref, func_ctx->cur_frame,
offset_of_ref(comp_ctx, n), 2))
return false;
p->committed_ref = (p + 1)->committed_ref = p->ref + 1;
}
p++;
break;
case VALUE_TYPE_V128:
bh_assert(p->ref == (p + 1)->ref && p->ref == (p + 2)->ref
&& p->ref == (p + 3)->ref);
if (p->ref != p->committed_ref - 1
|| p->ref != (p + 1)->committed_ref - 1
|| p->ref != (p + 2)->committed_ref - 1
|| p->ref != (p + 3)->committed_ref - 1) {
if (!store_ref(comp_ctx, p->ref, func_ctx->cur_frame,
offset_of_ref(comp_ctx, n), 4))
return false;
p->committed_ref = (p + 1)->committed_ref =
(p + 2)->committed_ref = (p + 3)->committed_ref =
p->ref + 1;
}
p += 3;
break;
case REF_TYPE_NULLFUNCREF:
case REF_TYPE_NULLEXTERNREF:
case REF_TYPE_NULLREF:
case REF_TYPE_FUNCREF:
case REF_TYPE_EXTERNREF:
case REF_TYPE_ANYREF:
case REF_TYPE_EQREF:
case REF_TYPE_HT_NULLABLE:
case REF_TYPE_HT_NON_NULLABLE:
case REF_TYPE_I31REF:
case REF_TYPE_STRUCTREF:
case REF_TYPE_ARRAYREF:
#if WASM_ENABLE_STRINGREF != 0
case REF_TYPE_STRINGREF:
case REF_TYPE_STRINGVIEWWTF8:
case REF_TYPE_STRINGVIEWWTF16:
case REF_TYPE_STRINGVIEWITER:
#endif
case VALUE_TYPE_GC_REF:
if (comp_ctx->pointer_size == sizeof(uint64)) {
bh_assert(p->ref == (p + 1)->ref);
if (p->ref != p->committed_ref - 1
|| p->ref != (p + 1)->committed_ref - 1) {
if (!store_ref(comp_ctx, p->ref,
func_ctx->cur_frame,
offset_of_ref(comp_ctx, n), 2))
return false;
p->committed_ref = (p + 1)->committed_ref =
p->ref + 1;
}
p++;
}
else {
if (p->ref != p->committed_ref - 1) {
if (!store_ref(comp_ctx, p->ref,
func_ctx->cur_frame,
offset_of_ref(comp_ctx, n), 1))
return false;
p->committed_ref = p->ref + 1;
}
}
break;
default:
bh_assert(0);
break;
}
}
}
/* Second, commit all values */
for (p = frame->lp; p < frame->sp; p++) {
if (!p->dirty)
continue;
p->dirty = 0;
n = (uint32)(p - frame->lp);
/* Commit values */
switch (p->type) {
case VALUE_TYPE_I32:
if (!store_value(comp_ctx, p->value, VALUE_TYPE_I32,
func_ctx->cur_frame,
offset_of_local(comp_ctx, n)))
return false;
break;
case VALUE_TYPE_I64:
(++p)->dirty = 0;
if (!store_value(comp_ctx, p->value, VALUE_TYPE_I64,
func_ctx->cur_frame,
offset_of_local(comp_ctx, n)))
return false;
break;
case VALUE_TYPE_F32:
if (!store_value(comp_ctx, p->value, VALUE_TYPE_F32,
func_ctx->cur_frame,
offset_of_local(comp_ctx, n)))
return false;
break;
case VALUE_TYPE_F64:
(++p)->dirty = 0;
if (!store_value(comp_ctx, p->value, VALUE_TYPE_F64,
func_ctx->cur_frame,
offset_of_local(comp_ctx, n)))
return false;
break;
case VALUE_TYPE_V128:
(++p)->dirty = 0;
(++p)->dirty = 0;
(++p)->dirty = 0;
if (!store_value(comp_ctx, p->value, VALUE_TYPE_V128,
func_ctx->cur_frame,
offset_of_local(comp_ctx, n)))
return false;
break;
case VALUE_TYPE_I1:
if (!(value = LLVMBuildZExt(comp_ctx->builder, p->value,
I32_TYPE, "i32_val"))) {
aot_set_last_error("llvm build bit cast failed");
return false;
}
if (!store_value(comp_ctx, value, VALUE_TYPE_I32,
func_ctx->cur_frame,
offset_of_local(comp_ctx, n)))
return false;
break;
case VALUE_TYPE_FUNCREF:
case VALUE_TYPE_EXTERNREF:
if (comp_ctx->enable_ref_types) {
if (!store_value(comp_ctx, p->value, VALUE_TYPE_I32,
func_ctx->cur_frame,
offset_of_local(comp_ctx, n)))
return false;
}
#if WASM_ENABLE_GC != 0
else if (comp_ctx->enable_gc) {
if (comp_ctx->pointer_size == sizeof(uint64))
(++p)->dirty = 0;
if (!store_value(comp_ctx, p->value, VALUE_TYPE_GC_REF,
func_ctx->cur_frame,
offset_of_local(comp_ctx, n)))
return false;
}
#endif
else {
bh_assert(0);
}
break;
#if WASM_ENABLE_GC != 0
case REF_TYPE_NULLFUNCREF:
case REF_TYPE_NULLEXTERNREF:
case REF_TYPE_NULLREF:
/* case REF_TYPE_FUNCREF: */
/* case REF_TYPE_EXTERNREF: */
case REF_TYPE_ANYREF:
case REF_TYPE_EQREF:
case REF_TYPE_HT_NULLABLE:
case REF_TYPE_HT_NON_NULLABLE:
case REF_TYPE_I31REF:
case REF_TYPE_STRUCTREF:
case REF_TYPE_ARRAYREF:
case VALUE_TYPE_GC_REF:
if (comp_ctx->pointer_size == sizeof(uint64))
(++p)->dirty = 0;
if (!store_value(comp_ctx, p->value, VALUE_TYPE_GC_REF,
func_ctx->cur_frame,
offset_of_local(comp_ctx, n)))
return false;
break;
#endif
default:
bh_assert(0);
break;
}
}
if (comp_ctx->enable_gc) {
end = frame->lp + frame->max_local_cell_num + frame->max_stack_cell_num;
/* Clear reference flags for unused stack slots. */
for (p = frame->sp; p < end; p++) {
bh_assert(!p->ref);
n = (uint32)(p - frame->lp);
/* Commit reference flag. */
if (p->ref != p->committed_ref - 1) {
if (!store_ref(comp_ctx, p->ref, func_ctx->cur_frame,
offset_of_ref(comp_ctx, n), 1))
return false;
p->committed_ref = 1 + p->ref;
}
}
}
return true;
}
static bool
aot_standard_frame_gen_commit_ip(AOTCompContext *comp_ctx,
AOTFuncContext *func_ctx,
LLVMValueRef ip_value, bool is_64bit)
{
LLVMValueRef cur_frame = func_ctx->cur_frame;
LLVMValueRef value_offset, value_addr, value_ptr;
uint32 offset_ip;
if (!comp_ctx->is_jit_mode)
offset_ip = comp_ctx->pointer_size * 4;
else
offset_ip = offsetof(WASMInterpFrame, ip);
if (!(value_offset = I32_CONST(offset_ip))) {
aot_set_last_error("llvm build const failed");
return false;
}
if (!(value_addr =
LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, cur_frame,
&value_offset, 1, "ip_addr"))) {
aot_set_last_error("llvm build in bounds gep failed");
return false;
}
if (!(value_ptr = LLVMBuildBitCast(
comp_ctx->builder, value_addr,
is_64bit ? INT64_PTR_TYPE : INT32_PTR_TYPE, "ip_ptr"))) {
aot_set_last_error("llvm build bit cast failed");
return false;
}
if (!LLVMBuildStore(comp_ctx->builder, ip_value, value_ptr)) {
aot_set_last_error("llvm build store failed");
return false;
}
return true;
}
bool
aot_gen_commit_ip(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
LLVMValueRef ip_value, bool is_64bit)
{
switch (comp_ctx->aux_stack_frame_type) {
case AOT_STACK_FRAME_TYPE_STANDARD:
return aot_standard_frame_gen_commit_ip(comp_ctx, func_ctx,
ip_value, is_64bit);
case AOT_STACK_FRAME_TYPE_TINY:
return aot_tiny_frame_gen_commit_ip(comp_ctx, func_ctx, ip_value);
default:
aot_set_last_error(
"unsupported mode when generating commit_ip code");
return false;
}
}
bool
aot_gen_commit_sp_ip(AOTCompFrame *frame, bool commit_sp, bool commit_ip)
{
AOTCompContext *comp_ctx = frame->comp_ctx;
AOTFuncContext *func_ctx = frame->func_ctx;
LLVMValueRef cur_frame = func_ctx->cur_frame;
LLVMValueRef value_offset, value_addr, value_ptr, value;
LLVMTypeRef int8_ptr_ptr_type;
uint32 offset_sp, n;
bool is_64bit = (comp_ctx->pointer_size == sizeof(uint64)) ? true : false;
const AOTValueSlot *sp = frame->sp;
const uint8 *ip = frame->frame_ip;
if (!comp_ctx->is_jit_mode) {
offset_sp = frame->comp_ctx->pointer_size * 5;
}
else {
offset_sp = offsetof(WASMInterpFrame, sp);
}
if (commit_ip && comp_ctx->call_stack_features.ip) {
if (!comp_ctx->is_jit_mode) {
WASMModule *module = comp_ctx->comp_data->wasm_module;
if (is_64bit)
value = I64_CONST((uint64)(uintptr_t)(ip - module->load_addr));
else
value = I32_CONST((uint32)(uintptr_t)(ip - module->load_addr));
}
else {
if (is_64bit)
value = I64_CONST((uint64)(uintptr_t)ip);
else
value = I32_CONST((uint32)(uintptr_t)ip);
}
if (!value) {
aot_set_last_error("llvm build const failed");
return false;
}
if (!aot_gen_commit_ip(comp_ctx, func_ctx, value, is_64bit)) {
return false;
}
}
if (commit_sp && comp_ctx->call_stack_features.values) {
n = (uint32)(sp - frame->lp);
value = I32_CONST(offset_of_local(comp_ctx, n));
if (!value) {
aot_set_last_error("llvm build const failed");
return false;
}
if (!(value = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
cur_frame, &value, 1, "sp"))) {
aot_set_last_error("llvm build in bounds gep failed");
return false;
}
if (!(value_offset = I32_CONST(offset_sp))) {
aot_set_last_error("llvm build const failed");
return false;
}
if (!(value_addr =
LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, cur_frame,
&value_offset, 1, "sp_addr"))) {
aot_set_last_error("llvm build in bounds gep failed");
return false;
}
if (!(int8_ptr_ptr_type = LLVMPointerType(INT8_PTR_TYPE, 0))) {
aot_set_last_error("llvm build pointer type failed");
return false;
}
if (!(value_ptr = LLVMBuildBitCast(comp_ctx->builder, value_addr,
int8_ptr_ptr_type, "sp_ptr"))) {
aot_set_last_error("llvm build bit cast failed");
return false;
}
if (!LLVMBuildStore(comp_ctx->builder, value, value_ptr)) {
aot_set_last_error("llvm build store failed");
return false;
}
}
return true;
}
static uint32
get_cur_frame_size(const AOTCompContext *comp_ctx, uint32 max_local_cell_num,
uint32 max_stack_cell_num)
{
uint32 all_cell_num = max_local_cell_num + max_stack_cell_num;
uint32 frame_size;
if (!comp_ctx->is_jit_mode) {
/* Refer to aot_alloc_frame */
if (!comp_ctx->enable_gc)
frame_size = comp_ctx->pointer_size
* (offsetof(AOTFrame, lp) / sizeof(uintptr_t))
+ all_cell_num * 4;
else
frame_size = comp_ctx->pointer_size
* (offsetof(AOTFrame, lp) / sizeof(uintptr_t))
+ align_uint(all_cell_num * 5, 4);
}
else {
/* Refer to wasm_interp_interp_frame_size */
if (!comp_ctx->enable_gc)
frame_size = offsetof(WASMInterpFrame, lp) + all_cell_num * 4;
else
frame_size =
offsetof(WASMInterpFrame, lp) + align_uint(all_cell_num * 5, 4);
}
return frame_size;
}
static bool
init_comp_frame(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
uint32 func_idx)
{
AOTCompFrame *aot_frame;
AOTFunc *aot_func = func_ctx->aot_func;
AOTFuncType *func_type = aot_func->func_type;
AOTBlock *block = func_ctx->block_stack.block_list_end;
LLVMValueRef local_value;
uint32 max_local_cell_num =
aot_func->param_cell_num + aot_func->local_cell_num;
uint32 max_stack_cell_num = aot_func->max_stack_cell_num;
uint32 all_cell_num = max_local_cell_num + max_stack_cell_num;
uint32 i, n;
uint64 total_size;
uint8 local_type;
/* Free aot_frame if it was allocated previously for
compiling other functions */
if (comp_ctx->aot_frame) {
wasm_runtime_free(comp_ctx->aot_frame);
comp_ctx->aot_frame = NULL;
}
/* Allocate extra 2 cells since some operations may push more
operands than the number calculated in wasm loader, such as
PUSH_F64(F64_CONST(1.0)) in aot_compile_op_f64_promote_f32 */
all_cell_num += 2;
total_size = offsetof(AOTCompFrame, lp)
+ (uint64)sizeof(AOTValueSlot) * all_cell_num;
if (total_size > UINT32_MAX
|| !(comp_ctx->aot_frame = aot_frame =
wasm_runtime_malloc((uint32)total_size))) {
aot_set_last_error("allocate memory failed.");
return false;
}
memset(aot_frame, 0, (uint32)total_size);
aot_frame->comp_ctx = comp_ctx;
aot_frame->func_ctx = func_ctx;
aot_frame->max_local_cell_num = max_local_cell_num;
aot_frame->max_stack_cell_num = max_stack_cell_num;
aot_frame->cur_frame_size =
get_cur_frame_size(comp_ctx, max_local_cell_num, max_stack_cell_num);
aot_frame->sp = aot_frame->lp + max_local_cell_num;
/* Init the frame_sp_begin and frame_sp_max_reached
of the function block */
block->frame_sp_begin = block->frame_sp_max_reached = aot_frame->sp;
n = 0;
/* Set all params dirty since they were set to llvm value but
haven't been committed to the AOT/JIT stack frame */
for (i = 0; i < func_type->param_count; i++) {
local_type = func_type->types[i];
local_value = LLVMGetParam(func_ctx->func, i + 1);
switch (local_type) {
case VALUE_TYPE_I32:
set_local_i32(comp_ctx->aot_frame, n, local_value);
n++;
break;
case VALUE_TYPE_I64:
set_local_i64(comp_ctx->aot_frame, n, local_value);
n += 2;
break;
case VALUE_TYPE_F32:
set_local_f32(comp_ctx->aot_frame, n, local_value);
n++;
break;
case VALUE_TYPE_F64:
set_local_f64(comp_ctx->aot_frame, n, local_value);
n += 2;
break;
case VALUE_TYPE_V128:
set_local_v128(comp_ctx->aot_frame, n, local_value);
n += 4;
break;
case VALUE_TYPE_FUNCREF:
case VALUE_TYPE_EXTERNREF:
{
if (comp_ctx->enable_ref_types) {
set_local_ref(comp_ctx->aot_frame, n, local_value,
local_type);
n++;
}
#if WASM_ENABLE_GC != 0
else if (comp_ctx->enable_gc) {
set_local_gc_ref(comp_ctx->aot_frame, n, local_value,
VALUE_TYPE_GC_REF);
n += comp_ctx->pointer_size / sizeof(uint32);
}
#endif
else {
bh_assert(0);
}
break;
}
#if WASM_ENABLE_GC != 0
case REF_TYPE_NULLFUNCREF:
case REF_TYPE_NULLEXTERNREF:
case REF_TYPE_NULLREF:
/* case REF_TYPE_FUNCREF: */
/* case REF_TYPE_EXTERNREF: */
case REF_TYPE_ANYREF:
case REF_TYPE_EQREF:
case REF_TYPE_HT_NULLABLE:
case REF_TYPE_HT_NON_NULLABLE:
case REF_TYPE_I31REF:
case REF_TYPE_STRUCTREF:
case REF_TYPE_ARRAYREF:
#if WASM_ENABLE_STRINGREF != 0
case REF_TYPE_STRINGREF:
case REF_TYPE_STRINGVIEWWTF8:
case REF_TYPE_STRINGVIEWWTF16:
case REF_TYPE_STRINGVIEWITER:
#endif
bh_assert(comp_ctx->enable_gc);
set_local_gc_ref(comp_ctx->aot_frame, n, local_value,
VALUE_TYPE_GC_REF);
n += comp_ctx->pointer_size / sizeof(uint32);
break;
#endif
default:
bh_assert(0);
break;
}
}
/* TODO: re-calculate param_cell_num according to the build target
after creating comp_ctx */
/* bh_assert(n == aot_func->param_cell_num); */
/* Set all locals dirty since they were set to llvm value but
haven't been committed to the AOT/JIT stack frame */
for (i = 0; i < aot_func->local_count; i++) {
local_type = aot_func->local_types_wp[i];
switch (local_type) {
case VALUE_TYPE_I32:
set_local_i32(comp_ctx->aot_frame, n, I32_ZERO);
n++;
break;
case VALUE_TYPE_I64:
set_local_i64(comp_ctx->aot_frame, n, I64_ZERO);
n += 2;
break;
case VALUE_TYPE_F32:
set_local_f32(comp_ctx->aot_frame, n, F32_ZERO);
n++;
break;
case VALUE_TYPE_F64:
set_local_f64(comp_ctx->aot_frame, n, F64_ZERO);
n += 2;
break;
case VALUE_TYPE_V128:
set_local_v128(comp_ctx->aot_frame, n, V128_f64x2_ZERO);
n += 4;
break;
case VALUE_TYPE_FUNCREF:
case VALUE_TYPE_EXTERNREF:
{
if (comp_ctx->enable_ref_types) {
set_local_ref(comp_ctx->aot_frame, n, I32_ZERO, local_type);
n++;
}
#if WASM_ENABLE_GC != 0
else if (comp_ctx->enable_gc) {
set_local_gc_ref(comp_ctx->aot_frame, n, GC_REF_NULL,
VALUE_TYPE_GC_REF);
n += comp_ctx->pointer_size / sizeof(uint32);
}
#endif
else {
bh_assert(0);
}
break;
}
#if WASM_ENABLE_GC != 0
case REF_TYPE_NULLFUNCREF:
case REF_TYPE_NULLEXTERNREF:
case REF_TYPE_NULLREF:
/* case REF_TYPE_FUNCREF: */
/* case REF_TYPE_EXTERNREF: */
case REF_TYPE_ANYREF:
case REF_TYPE_EQREF:
case REF_TYPE_HT_NULLABLE:
case REF_TYPE_HT_NON_NULLABLE:
case REF_TYPE_I31REF:
case REF_TYPE_STRUCTREF:
case REF_TYPE_ARRAYREF:
#if WASM_ENABLE_STRINGREF != 0
case REF_TYPE_STRINGREF:
case REF_TYPE_STRINGVIEWWTF8:
case REF_TYPE_STRINGVIEWWTF16:
case REF_TYPE_STRINGVIEWITER:
#endif
bh_assert(comp_ctx->enable_gc);
set_local_gc_ref(comp_ctx->aot_frame, n, GC_REF_NULL,
VALUE_TYPE_GC_REF);
n += comp_ctx->pointer_size / sizeof(uint32);
break;
#endif
default:
bh_assert(0);
break;
}
}
/* TODO: re-calculate local_cell_num according to the build target
after creating comp_ctx */
/* bh_assert(n == aot_func->param_cell_num + aot_func->local_cell_num); */
/* No need to initialize aot_frame all cells' committed_ref flags
and all stack cells' ref flags since they have been initialized
as 0 (uncommitted and not-reference) by the memset above */
return true;
}
static bool
aot_compile_func(AOTCompContext *comp_ctx, uint32 func_index)
{