-
Notifications
You must be signed in to change notification settings - Fork 786
Expand file tree
/
Copy pathaot_emit_function.c
More file actions
3318 lines (2918 loc) · 124 KB
/
aot_emit_function.c
File metadata and controls
3318 lines (2918 loc) · 124 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_emit_function.h"
#include "aot_emit_exception.h"
#include "aot_emit_control.h"
#include "aot_emit_table.h"
#include "aot_stack_frame_comp.h"
#include "../aot/aot_runtime.h"
#if WASM_ENABLE_GC != 0
#include "aot_emit_gc.h"
#endif
#define ADD_BASIC_BLOCK(block, name) \
do { \
if (!(block = LLVMAppendBasicBlockInContext(comp_ctx->context, \
func_ctx->func, name))) { \
aot_set_last_error("llvm add basic block failed."); \
goto fail; \
} \
} while (0)
static bool
is_win_platform(AOTCompContext *comp_ctx)
{
char *triple = LLVMGetTargetMachineTriple(comp_ctx->target_machine);
bool ret;
bh_assert(triple);
ret = (strstr(triple, "win32") || strstr(triple, "win")) ? true : false;
LLVMDisposeMessage(triple);
return ret;
}
static bool
create_func_return_block(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
{
LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
/* Create function return block if it isn't created */
if (!func_ctx->func_return_block) {
if (!(func_ctx->func_return_block = LLVMAppendBasicBlockInContext(
comp_ctx->context, func_ctx->func, "func_ret"))) {
aot_set_last_error("llvm add basic block failed.");
return false;
}
/* Create return IR */
LLVMPositionBuilderAtEnd(comp_ctx->builder,
func_ctx->func_return_block);
if (!comp_ctx->enable_bound_check) {
if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_ALREADY_THROWN,
false, NULL, NULL)) {
return false;
}
}
else if (!aot_build_zero_function_ret(comp_ctx, func_ctx,
aot_func_type)) {
return false;
}
}
LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
return true;
}
/* Check whether there was exception thrown, if yes, return directly */
static bool
check_exception_thrown(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
{
LLVMBasicBlockRef block_curr, check_exce_succ;
LLVMValueRef value, cmp;
/* Create function return block if it isn't created */
if (!create_func_return_block(comp_ctx, func_ctx))
return false;
/* Load the first byte of aot_module_inst->cur_exception, and check
whether it is '\0'. If yes, no exception was thrown. */
if (!(value = LLVMBuildLoad2(comp_ctx->builder, INT8_TYPE,
func_ctx->cur_exception, "exce_value"))
|| !(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ, value, I8_ZERO,
"cmp"))) {
aot_set_last_error("llvm build icmp failed.");
return false;
}
/* Add check exception success block */
if (!(check_exce_succ = LLVMAppendBasicBlockInContext(
comp_ctx->context, func_ctx->func, "check_exce_succ"))) {
aot_set_last_error("llvm add basic block failed.");
return false;
}
block_curr = LLVMGetInsertBlock(comp_ctx->builder);
LLVMMoveBasicBlockAfter(check_exce_succ, block_curr);
LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
/* Create condition br */
if (!LLVMBuildCondBr(comp_ctx->builder, cmp, check_exce_succ,
func_ctx->func_return_block)) {
aot_set_last_error("llvm build cond br failed.");
return false;
}
LLVMPositionBuilderAtEnd(comp_ctx->builder, check_exce_succ);
return true;
}
#if WASM_ENABLE_COMPILATION_HINTS != 0
static void
aot_emit_call_target_hint(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
uint32 offset, LLVMValueRef call_instr)
{
struct WASMCompilationHint *hint = func_ctx->function_hints;
while (hint != NULL) {
if (hint->type == WASM_COMPILATION_HINT_CALL_TARGETS
&& hint->offset == offset) {
break;
}
hint = hint->next;
}
if (hint != NULL) {
hint->used = true;
struct WASMCompilationHintCallTargets *ct_hint =
(struct WASMCompilationHintCallTargets *)hint;
const unsigned md_node_cnt = ct_hint->target_count * 2 + 3;
LLVMMetadataRef *md_nodes =
wasm_runtime_malloc(md_node_cnt * sizeof(LLVMMetadataRef));
if (!md_nodes) {
aot_set_last_error("allocate memory failed.");
return;
}
md_nodes[0] =
LLVMMDStringInContext2(comp_ctx->context, "VP", strlen("VP"));
md_nodes[1] = LLVMValueAsMetadata(I32_CONST(0));
// since wasm encodes a call frequency in full percent, we forward this
// to llvm as 100 calls to match the percentages
// (could be enhanced with the instruction frequency hints)
md_nodes[2] = LLVMValueAsMetadata(I64_CONST(100));
for (size_t i = 0; i < ct_hint->target_count; ++i) {
struct WASMCompilationHintCallTargetsHint *target =
&ct_hint->hints[i];
char target_func_name[48];
snprintf(target_func_name, sizeof(target_func_name), "%s%d",
AOT_FUNC_PREFIX,
target->func_idx - comp_ctx->comp_data->import_func_count);
const uint64_t func_name_hash =
aot_func_name_hash(target_func_name);
md_nodes[i * 2 + 3] =
LLVMValueAsMetadata(I64_CONST(func_name_hash));
md_nodes[i * 2 + 3 + 1] =
LLVMValueAsMetadata(I64_CONST(target->call_frequency));
}
LLVMMetadataRef meta_data =
LLVMMDNodeInContext2(comp_ctx->context, md_nodes, md_node_cnt);
LLVMValueRef meta_data_as_value =
LLVMMetadataAsValue(comp_ctx->context, meta_data);
LLVMSetMetadata(call_instr, 2, meta_data_as_value);
wasm_runtime_free(md_nodes);
}
}
#endif
/* Check whether there was exception thrown, if yes, return directly */
static bool
check_call_return(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
LLVMValueRef res)
{
LLVMBasicBlockRef block_curr, check_call_succ;
LLVMValueRef cmp;
/* Create function return block if it isn't created */
if (!create_func_return_block(comp_ctx, func_ctx))
return false;
if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntNE, res, I8_ZERO,
"cmp"))) {
aot_set_last_error("llvm build icmp failed.");
return false;
}
/* Add check exception success block */
if (!(check_call_succ = LLVMAppendBasicBlockInContext(
comp_ctx->context, func_ctx->func, "check_call_succ"))) {
aot_set_last_error("llvm add basic block failed.");
return false;
}
block_curr = LLVMGetInsertBlock(comp_ctx->builder);
LLVMMoveBasicBlockAfter(check_call_succ, block_curr);
LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr);
/* Create condition br */
if (!LLVMBuildCondBr(comp_ctx->builder, cmp, check_call_succ,
func_ctx->func_return_block)) {
aot_set_last_error("llvm build cond br failed.");
return false;
}
LLVMPositionBuilderAtEnd(comp_ctx->builder, check_call_succ);
return true;
}
static bool
call_aot_invoke_native_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
LLVMValueRef func_idx, AOTFuncType *aot_func_type,
LLVMTypeRef *param_types,
LLVMValueRef *param_values, uint32 param_count,
uint32 param_cell_num, LLVMTypeRef ret_type,
uint8 wasm_ret_type, LLVMValueRef *p_value_ret,
LLVMValueRef *p_res)
{
LLVMTypeRef func_type, func_ptr_type, func_param_types[4];
LLVMTypeRef ret_ptr_type, elem_ptr_type;
LLVMValueRef func, elem_idx, elem_ptr;
LLVMValueRef func_param_values[4], value_ret = NULL, res;
char buf[32], *func_name = "aot_invoke_native";
uint32 i, cell_num = 0;
/* prepare function type of aot_invoke_native */
func_param_types[0] = comp_ctx->exec_env_type; /* exec_env */
func_param_types[1] = I32_TYPE; /* func_idx */
func_param_types[2] = I32_TYPE; /* argc */
func_param_types[3] = INT32_PTR_TYPE; /* argv */
if (!(func_type =
LLVMFunctionType(INT8_TYPE, func_param_types, 4, false))) {
aot_set_last_error("llvm add function type failed.");
return false;
}
/* prepare function pointer */
if (comp_ctx->is_jit_mode) {
if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
aot_set_last_error("create LLVM function type failed.");
return false;
}
/* JIT mode, call the function directly */
if (!(func = I64_CONST((uint64)(uintptr_t)llvm_jit_invoke_native))
|| !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
aot_set_last_error("create LLVM value failed.");
return false;
}
}
else if (comp_ctx->is_indirect_mode) {
int32 func_index;
if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
aot_set_last_error("create LLVM function type failed.");
return false;
}
func_index = aot_get_native_symbol_index(comp_ctx, func_name);
if (func_index < 0) {
return false;
}
if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
func_ptr_type, func_index))) {
return false;
}
}
else {
if (!(func = LLVMGetNamedFunction(func_ctx->module, func_name))
&& !(func =
LLVMAddFunction(func_ctx->module, func_name, func_type))) {
aot_set_last_error("add LLVM function failed.");
return false;
}
}
if (param_cell_num > 64) {
aot_set_last_error("prepare native arguments failed: "
"maximum 64 parameter cell number supported.");
return false;
}
/* prepare frame_lp */
for (i = 0; i < param_count; i++) {
if (!(elem_idx = I32_CONST(cell_num))
|| !(elem_ptr_type = LLVMPointerType(param_types[i], 0))) {
aot_set_last_error("llvm add const or pointer type failed.");
return false;
}
snprintf(buf, sizeof(buf), "%s%d", "elem", i);
if (!(elem_ptr =
LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE,
func_ctx->argv_buf, &elem_idx, 1, buf))
|| !(elem_ptr = LLVMBuildBitCast(comp_ctx->builder, elem_ptr,
elem_ptr_type, buf))) {
aot_set_last_error("llvm build bit cast failed.");
return false;
}
if (!(res = LLVMBuildStore(comp_ctx->builder, param_values[i],
elem_ptr))) {
aot_set_last_error("llvm build store failed.");
return false;
}
LLVMSetAlignment(res, 1);
cell_num += wasm_value_type_cell_num_internal(aot_func_type->types[i],
comp_ctx->pointer_size);
}
func_param_values[0] = func_ctx->exec_env;
func_param_values[1] = func_idx;
func_param_values[2] = I32_CONST(param_cell_num);
func_param_values[3] = func_ctx->argv_buf;
if (!func_param_values[2]) {
aot_set_last_error("llvm create const failed.");
return false;
}
/* call aot_invoke_native() function */
if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func,
func_param_values, 4, "res"))) {
aot_set_last_error("llvm build call failed.");
return false;
}
/* get function return value */
if (wasm_ret_type != VALUE_TYPE_VOID) {
if (!(ret_ptr_type = LLVMPointerType(ret_type, 0))) {
aot_set_last_error("llvm add pointer type failed.");
return false;
}
if (!(value_ret =
LLVMBuildBitCast(comp_ctx->builder, func_ctx->argv_buf,
ret_ptr_type, "argv_ret"))) {
aot_set_last_error("llvm build bit cast failed.");
return false;
}
if (!(*p_value_ret = LLVMBuildLoad2(comp_ctx->builder, ret_type,
value_ret, "value_ret"))) {
aot_set_last_error("llvm build load failed.");
return false;
}
}
*p_res = res;
return true;
}
static bool
call_aot_invoke_c_api_native(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
uint32 import_func_idx, AOTFuncType *aot_func_type,
LLVMValueRef *params)
{
LLVMTypeRef int8_ptr_type, param_types[6], ret_type;
LLVMTypeRef value_ptr_type = NULL, value_type = NULL;
LLVMTypeRef func_type, func_ptr_type;
LLVMValueRef param_values[6], res, func, value = NULL, offset;
LLVMValueRef c_api_func_imports, c_api_func_import;
LLVMValueRef c_api_params, c_api_results, value_ret;
LLVMValueRef c_api_param_kind, c_api_param_value;
LLVMValueRef c_api_result_value;
uint32 offset_c_api_func_imports, i;
uint32 offset_param_kind, offset_param_value;
char buf[16];
/* `int8 **` type */
int8_ptr_type = LLVMPointerType(INT8_PTR_TYPE, 0);
if (!int8_ptr_type) {
aot_set_last_error("create llvm pointer type failed");
return false;
}
param_types[0] = INT8_PTR_TYPE; /* module_inst */
param_types[1] = INT8_PTR_TYPE; /* CApiFuncImport *c_api_import */
param_types[2] = INT8_PTR_TYPE; /* wasm_val_t *params */
param_types[3] = I32_TYPE; /* uint32 param_count */
param_types[4] = INT8_PTR_TYPE; /* wasm_val_t *results */
param_types[5] = I32_TYPE; /* uint32 result_count */
ret_type = INT8_TYPE;
GET_AOT_FUNCTION(wasm_runtime_quick_invoke_c_api_native, 6);
param_values[0] = func_ctx->aot_inst;
/* Get module_inst->c_api_func_imports, jit mode WASMModuleInstance is the
* same layout with AOTModuleInstance */
offset_c_api_func_imports = offsetof(AOTModuleInstance, c_api_func_imports);
offset = I32_CONST(offset_c_api_func_imports);
CHECK_LLVM_CONST(offset);
c_api_func_imports =
LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, func_ctx->aot_inst,
&offset, 1, "c_api_func_imports_addr");
c_api_func_imports =
LLVMBuildBitCast(comp_ctx->builder, c_api_func_imports, int8_ptr_type,
"c_api_func_imports_ptr");
c_api_func_imports =
LLVMBuildLoad2(comp_ctx->builder, INT8_PTR_TYPE, c_api_func_imports,
"c_api_func_imports");
/* Get &c_api_func_imports[func_idx], note size of CApiFuncImport
is pointer_size * 3 */
offset = I32_CONST((unsigned long long)comp_ctx->pointer_size * 3
* import_func_idx);
CHECK_LLVM_CONST(offset);
c_api_func_import =
LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, c_api_func_imports,
&offset, 1, "c_api_func_import");
param_values[1] = c_api_func_import;
param_values[2] = c_api_params = func_ctx->argv_buf;
param_values[3] = I32_CONST(aot_func_type->param_count);
CHECK_LLVM_CONST(param_values[3]);
/* Ensure sizeof(wasm_val_t) is 16 bytes */
offset = I32_CONST(sizeof(wasm_val_t) * aot_func_type->param_count);
c_api_results =
LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, func_ctx->argv_buf,
&offset, 1, "results");
param_values[4] = c_api_results;
param_values[5] = I32_CONST(aot_func_type->result_count);
CHECK_LLVM_CONST(param_values[5]);
/* Set each c api param */
for (i = 0; i < aot_func_type->param_count; i++) {
/* Ensure sizeof(wasm_val_t) is 16 bytes */
offset_param_kind = sizeof(wasm_val_t) * i;
offset = I32_CONST(offset_param_kind);
CHECK_LLVM_CONST(offset);
c_api_param_kind =
LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, c_api_params,
&offset, 1, "c_api_param_kind_addr");
c_api_param_kind =
LLVMBuildBitCast(comp_ctx->builder, c_api_param_kind, INT8_PTR_TYPE,
"c_api_param_kind_ptr");
switch (aot_func_type->types[i]) {
case VALUE_TYPE_I32:
value = I8_CONST(WASM_I32);
break;
case VALUE_TYPE_F32:
value = I8_CONST(WASM_F32);
break;
case VALUE_TYPE_I64:
value = I8_CONST(WASM_I64);
break;
case VALUE_TYPE_F64:
value = I8_CONST(WASM_F64);
break;
default:
bh_assert(0);
break;
}
CHECK_LLVM_CONST(value);
LLVMBuildStore(comp_ctx->builder, value, c_api_param_kind);
/* Ensure offsetof(wasm_val_t, of) is 8 bytes */
offset_param_value = offset_param_kind + offsetof(wasm_val_t, of);
offset = I32_CONST(offset_param_value);
CHECK_LLVM_CONST(offset);
c_api_param_value =
LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, c_api_params,
&offset, 1, "c_api_param_value_addr");
switch (aot_func_type->types[i]) {
case VALUE_TYPE_I32:
value_ptr_type = INT32_PTR_TYPE;
break;
case VALUE_TYPE_F32:
value_ptr_type = F32_PTR_TYPE;
break;
case VALUE_TYPE_I64:
value_ptr_type = INT64_PTR_TYPE;
break;
case VALUE_TYPE_F64:
value_ptr_type = F64_PTR_TYPE;
break;
default:
bh_assert(0);
break;
}
c_api_param_value =
LLVMBuildBitCast(comp_ctx->builder, c_api_param_value,
value_ptr_type, "c_api_param_value_ptr");
LLVMBuildStore(comp_ctx->builder, params[i], c_api_param_value);
}
/* Call the function */
if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func, param_values,
6, "call"))) {
aot_set_last_error("LLVM build call failed.");
goto fail;
}
/* Check whether exception was thrown when executing the function */
if (comp_ctx->enable_bound_check
&& !check_call_return(comp_ctx, func_ctx, res)) {
goto fail;
}
for (i = 0; i < aot_func_type->result_count; i++) {
/* Ensure sizeof(wasm_val_t) is 16 bytes and
offsetof(wasm_val_t, of) is 8 bytes */
uint32 offset_result_value =
sizeof(wasm_val_t) * i + offsetof(wasm_val_t, of);
offset = I32_CONST(offset_result_value);
CHECK_LLVM_CONST(offset);
c_api_result_value =
LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, c_api_results,
&offset, 1, "c_api_result_value_addr");
switch (aot_func_type->types[aot_func_type->param_count + i]) {
case VALUE_TYPE_I32:
value_type = I32_TYPE;
value_ptr_type = INT32_PTR_TYPE;
break;
case VALUE_TYPE_F32:
value_type = F32_TYPE;
value_ptr_type = F32_PTR_TYPE;
break;
case VALUE_TYPE_I64:
value_type = I64_TYPE;
value_ptr_type = INT64_PTR_TYPE;
break;
case VALUE_TYPE_F64:
value_type = F64_TYPE;
value_ptr_type = F64_PTR_TYPE;
break;
default:
bh_assert(0);
break;
}
c_api_result_value =
LLVMBuildBitCast(comp_ctx->builder, c_api_result_value,
value_ptr_type, "c_api_result_value_ptr");
snprintf(buf, sizeof(buf), "%s%u", "ret", i);
value_ret = LLVMBuildLoad2(comp_ctx->builder, value_type,
c_api_result_value, buf);
PUSH(value_ret, aot_func_type->types[aot_func_type->param_count + i]);
}
return true;
fail:
return false;
}
#if WASM_ENABLE_AOT_STACK_FRAME != 0
static bool
call_aot_alloc_frame_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
LLVMValueRef func_idx)
{
LLVMValueRef param_values[2], ret_value, value, func;
LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
LLVMBasicBlockRef frame_alloc_fail, frame_alloc_success;
AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
param_types[0] = comp_ctx->exec_env_type;
param_types[1] = I32_TYPE;
ret_type = INT8_TYPE;
#if WASM_ENABLE_JIT != 0
if (comp_ctx->is_jit_mode)
GET_AOT_FUNCTION(llvm_jit_alloc_frame, 2);
else
#endif
GET_AOT_FUNCTION(aot_alloc_frame, 2);
param_values[0] = func_ctx->exec_env;
param_values[1] = func_idx;
if (!(ret_value =
LLVMBuildCall2(comp_ctx->builder, func_type, func, param_values,
2, "call_aot_alloc_frame"))) {
aot_set_last_error("llvm build call failed.");
return false;
}
if (!(ret_value = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGT, ret_value,
I8_ZERO, "frame_alloc_ret"))) {
aot_set_last_error("llvm build icmp failed.");
return false;
}
ADD_BASIC_BLOCK(frame_alloc_fail, "frame_alloc_fail");
ADD_BASIC_BLOCK(frame_alloc_success, "frame_alloc_success");
LLVMMoveBasicBlockAfter(frame_alloc_fail, block_curr);
LLVMMoveBasicBlockAfter(frame_alloc_success, block_curr);
if (!LLVMBuildCondBr(comp_ctx->builder, ret_value, frame_alloc_success,
frame_alloc_fail)) {
aot_set_last_error("llvm build cond br failed.");
return false;
}
/* If frame alloc failed, return this function
so the runtime can catch the exception */
LLVMPositionBuilderAtEnd(comp_ctx->builder, frame_alloc_fail);
if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
return false;
}
LLVMPositionBuilderAtEnd(comp_ctx->builder, frame_alloc_success);
return true;
fail:
return false;
}
static bool
alloc_frame_for_aot_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
uint32 func_idx)
{
LLVMValueRef wasm_stack_top_bound = func_ctx->wasm_stack_top_bound;
LLVMValueRef wasm_stack_top_ptr = func_ctx->wasm_stack_top_ptr,
wasm_stack_top;
LLVMValueRef wasm_stack_top_max, offset, cmp;
LLVMValueRef cur_frame, new_frame, prev_frame_ptr;
LLVMValueRef cur_frame_ptr = func_ctx->cur_frame_ptr;
LLVMValueRef func_idx_ptr, func_idx_val, func_inst_ptr, func_inst;
LLVMTypeRef int8_ptr_type;
LLVMBasicBlockRef check_wasm_stack_succ;
uint32 import_func_count = comp_ctx->comp_data->import_func_count;
uint32 param_cell_num = 0, local_cell_num = 0, i;
uint32 max_local_cell_num, max_stack_cell_num;
uint32 all_cell_num, frame_size, frame_size_with_outs_area;
uint32 aot_frame_ptr_num = offsetof(AOTFrame, lp) / sizeof(uintptr_t);
AOTImportFunc *import_funcs = comp_ctx->comp_data->import_funcs;
AOTFuncType *aot_func_type;
AOTFunc *aot_func = NULL;
/* `int8 **` type */
int8_ptr_type = LLVMPointerType(INT8_PTR_TYPE, 0);
if (!int8_ptr_type) {
aot_set_last_error("create llvm pointer type failed");
return false;
}
/* Get param_cell_num, local_cell_num and max_stack_cell_num */
if (func_idx < import_func_count) {
aot_func_type = import_funcs[func_idx].func_type;
for (i = 0; i < aot_func_type->param_count; i++)
param_cell_num += wasm_value_type_cell_num_internal(
aot_func_type->types[i], comp_ctx->pointer_size);
max_local_cell_num = param_cell_num > 2 ? param_cell_num : 2;
max_stack_cell_num = 0;
}
else {
aot_func = comp_ctx->comp_data->funcs[func_idx - import_func_count];
param_cell_num = aot_func->param_cell_num;
local_cell_num = aot_func->local_cell_num;
max_local_cell_num = param_cell_num + local_cell_num;
max_stack_cell_num = aot_func->max_stack_cell_num;
}
all_cell_num = max_local_cell_num + max_stack_cell_num;
/* Get size of the frame to allocate and get size with outs_area to
check whether wasm operand stack is overflow */
if (!comp_ctx->is_jit_mode) {
/* Refer to aot_alloc_frame */
if (!comp_ctx->enable_gc) {
frame_size = frame_size_with_outs_area =
comp_ctx->pointer_size * aot_frame_ptr_num;
}
else {
frame_size = comp_ctx->pointer_size * aot_frame_ptr_num
+ align_uint(all_cell_num * 5, 4);
frame_size_with_outs_area =
frame_size + comp_ctx->pointer_size * aot_frame_ptr_num
+ max_stack_cell_num * 4;
}
}
else {
/* Refer to wasm_interp_interp_frame_size */
if (!comp_ctx->enable_gc) {
frame_size = frame_size_with_outs_area =
offsetof(WASMInterpFrame, lp);
}
else {
frame_size =
offsetof(WASMInterpFrame, lp) + align_uint(all_cell_num * 5, 4);
frame_size_with_outs_area = frame_size
+ offsetof(WASMInterpFrame, lp)
+ max_stack_cell_num * 4;
}
}
cur_frame = func_ctx->cur_frame;
if (!comp_ctx->enable_gc) {
offset = I32_CONST(frame_size);
CHECK_LLVM_CONST(offset);
if (!(wasm_stack_top =
LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, cur_frame,
&offset, 1, "wasm_stack_top"))) {
aot_set_last_error("llvm build in bounds gep failed");
return false;
}
offset = I32_CONST(frame_size * 2);
CHECK_LLVM_CONST(offset);
if (!(wasm_stack_top_max =
LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, cur_frame,
&offset, 1, "wasm_stack_top_max"))) {
aot_set_last_error("llvm build in bounds gep failed");
return false;
}
}
else {
/* Get exec_env->wasm_stack.top */
if (!(wasm_stack_top =
LLVMBuildLoad2(comp_ctx->builder, INT8_PTR_TYPE,
wasm_stack_top_ptr, "wasm_stack_top"))) {
aot_set_last_error("load wasm_stack.top failed");
return false;
}
/* Check whether wasm operand stack is overflow */
offset = I32_CONST(frame_size_with_outs_area);
CHECK_LLVM_CONST(offset);
if (!(wasm_stack_top_max = LLVMBuildInBoundsGEP2(
comp_ctx->builder, INT8_TYPE, wasm_stack_top, &offset, 1,
"wasm_stack_top_max"))) {
aot_set_last_error("llvm build in bounds gep failed");
return false;
}
}
new_frame = wasm_stack_top;
if (comp_ctx->call_stack_features.bounds_checks) {
if (!(check_wasm_stack_succ = LLVMAppendBasicBlockInContext(
comp_ctx->context, func_ctx->func,
"check_wasm_stack_succ"))) {
aot_set_last_error("llvm add basic block failed.");
return false;
}
LLVMMoveBasicBlockAfter(check_wasm_stack_succ,
LLVMGetInsertBlock(comp_ctx->builder));
if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGT,
wasm_stack_top_max, wasm_stack_top_bound,
"cmp"))) {
aot_set_last_error("llvm build icmp failed");
return false;
}
if (!(aot_emit_exception(comp_ctx, func_ctx,
EXCE_OPERAND_STACK_OVERFLOW, true, cmp,
check_wasm_stack_succ))) {
return false;
}
}
#if WASM_ENABLE_GC != 0
if (comp_ctx->enable_gc) {
LLVMValueRef wasm_stack_top_new, frame_ref, frame_ref_ptr;
uint32 j, k;
/* exec_env->wasm_stack.top += frame_size */
offset = I32_CONST(frame_size);
CHECK_LLVM_CONST(offset);
if (!(wasm_stack_top_new = LLVMBuildInBoundsGEP2(
comp_ctx->builder, INT8_TYPE, wasm_stack_top, &offset, 1,
"wasm_stack_top_new"))) {
aot_set_last_error("llvm build in bounds gep failed");
return false;
}
if (!LLVMBuildStore(comp_ctx->builder, wasm_stack_top_new,
wasm_stack_top_ptr)) {
aot_set_last_error("llvm build store failed");
return false;
}
if (func_idx < import_func_count) {
LLVMValueRef frame_sp, frame_sp_ptr;
/* Only need to initialize new_frame->sp when it's import function
otherwise they will be committed in AOT code if needed */
/* new_frame->sp = new_frame->lp + max_local_cell_num */
if (!comp_ctx->is_jit_mode)
offset = I32_CONST(comp_ctx->pointer_size * 5);
else
offset = I32_CONST(offsetof(WASMInterpFrame, sp));
CHECK_LLVM_CONST(offset);
if (!(frame_sp_ptr = LLVMBuildInBoundsGEP2(
comp_ctx->builder, INT8_TYPE, new_frame, &offset, 1,
"frame_sp_addr"))
|| !(frame_sp_ptr =
LLVMBuildBitCast(comp_ctx->builder, frame_sp_ptr,
int8_ptr_type, "frame_sp_ptr"))) {
aot_set_last_error("llvm get frame_sp_ptr failed");
return false;
}
if (!comp_ctx->is_jit_mode)
offset = I32_CONST(comp_ctx->pointer_size * aot_frame_ptr_num
+ max_local_cell_num * sizeof(uint32));
else
offset = I32_CONST(offsetof(WASMInterpFrame, lp)
+ max_local_cell_num * sizeof(uint32));
CHECK_LLVM_CONST(offset);
if (!(frame_sp = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
new_frame, &offset, 1,
"frame_sp"))) {
aot_set_last_error("llvm build in bounds gep failed");
return false;
}
if (!LLVMBuildStore(comp_ctx->builder, frame_sp, frame_sp_ptr)) {
aot_set_last_error("llvm build store failed");
return false;
}
}
if (!comp_ctx->is_jit_mode) {
/* new_frame->frame_ref = new_frame->lp + max_local_cell_num
+ max_stack_cell_num */
offset = I32_CONST(comp_ctx->pointer_size * 6);
CHECK_LLVM_CONST(offset);
if (!(frame_ref_ptr = LLVMBuildInBoundsGEP2(
comp_ctx->builder, INT8_TYPE, new_frame, &offset, 1,
"frame_ref_addr"))
|| !(frame_ref_ptr =
LLVMBuildBitCast(comp_ctx->builder, frame_ref_ptr,
int8_ptr_type, "frame_ref_ptr"))) {
aot_set_last_error("llvm get frame_ref_ptr failed");
return false;
}
offset = I32_CONST(comp_ctx->pointer_size * aot_frame_ptr_num
+ (max_local_cell_num + max_stack_cell_num)
* sizeof(uint32));
CHECK_LLVM_CONST(offset);
if (!(frame_ref = LLVMBuildInBoundsGEP2(comp_ctx->builder,
INT8_TYPE, new_frame,
&offset, 1, "frame_ref"))) {
aot_set_last_error("llvm build in bounds gep failed");
return false;
}
if (!LLVMBuildStore(comp_ctx->builder, frame_ref, frame_ref_ptr)) {
aot_set_last_error("llvm build store failed");
return false;
}
}
else {
/* Get frame_ref in WASMInterpFrame */
offset = I32_CONST(offsetof(WASMInterpFrame, lp)
+ (max_local_cell_num + max_stack_cell_num)
* sizeof(uint32));
CHECK_LLVM_CONST(offset);
if (!(frame_ref = LLVMBuildInBoundsGEP2(comp_ctx->builder,
INT8_TYPE, new_frame,
&offset, 1, "frame_ref"))) {
aot_set_last_error("llvm build in bounds gep failed");
return false;
}
}
/* Initialize frame ref flags for import function only in JIT mode */
if (func_idx < import_func_count && comp_ctx->is_jit_mode) {
aot_func_type = import_funcs[func_idx].func_type;
for (i = 0, j = 0; i < aot_func_type->param_count; i++) {
if (aot_is_type_gc_reftype(aot_func_type->types[i])
&& !wasm_is_reftype_i31ref(aot_func_type->types[i])) {
for (k = 0; k < comp_ctx->pointer_size / sizeof(uint32);
k++) {
/* frame_ref[j++] = 1 */
offset = I32_CONST(j);
CHECK_LLVM_CONST(offset);
frame_ref_ptr = LLVMBuildInBoundsGEP2(
comp_ctx->builder, INT8_TYPE, frame_ref, &offset, 1,
"frame_ref_ptr");
if (!LLVMBuildStore(comp_ctx->builder, I8_ONE,
frame_ref_ptr)) {
aot_set_last_error("llvm build store failed");
return false;
}
j++;
}
}
else {
uint32 value_type_cell_num =
wasm_value_type_cell_num_internal(
aot_func_type->types[i], comp_ctx->pointer_size);
for (k = 0; k < value_type_cell_num; k++) {
/* frame_ref[j++] = 0 */
offset = I32_CONST(j);
CHECK_LLVM_CONST(offset);
frame_ref_ptr = LLVMBuildInBoundsGEP2(
comp_ctx->builder, INT8_TYPE, frame_ref, &offset, 1,
"frame_ref_ptr");
if (!LLVMBuildStore(comp_ctx->builder, I8_ZERO,
frame_ref_ptr)) {
aot_set_last_error("llvm build store failed");
return false;
}
j++;
}
}
}
for (; j < 2; j++) {
/* frame_ref[j++] = 0 */
offset = I32_CONST(j);
CHECK_LLVM_CONST(offset);
frame_ref_ptr = LLVMBuildInBoundsGEP2(
comp_ctx->builder, INT8_TYPE, frame_ref, &offset, 1,
"frame_ref_ptr");
if (!LLVMBuildStore(comp_ctx->builder, I8_ZERO,
frame_ref_ptr)) {
aot_set_last_error("llvm build store failed");
return false;
}
}
}
}
#endif /* end of WASM_ENABLE_GC != 0 */
/* new_frame->prev_frame = cur_frame */
if (!(prev_frame_ptr = LLVMBuildBitCast(comp_ctx->builder, new_frame,
int8_ptr_type, "prev_frame_ptr"))) {
aot_set_last_error("llvm build bitcast failed");
return false;
}
if (!LLVMBuildStore(comp_ctx->builder, cur_frame, prev_frame_ptr)) {
aot_set_last_error("llvm build store failed");
return false;
}
if (!comp_ctx->is_jit_mode) {
if (comp_ctx->call_stack_features.func_idx) {
/* aot mode: new_frame->func_idx = func_idx */
func_idx_val = comp_ctx->pointer_size == sizeof(uint64)
? I64_CONST(func_idx)
: I32_CONST(func_idx);
offset = I32_CONST(comp_ctx->pointer_size);
CHECK_LLVM_CONST(func_idx_val);
CHECK_LLVM_CONST(offset);
if (!(func_idx_ptr = LLVMBuildInBoundsGEP2(
comp_ctx->builder, INT8_TYPE, new_frame, &offset, 1,
"func_idx_addr"))
|| !(func_idx_ptr =
LLVMBuildBitCast(comp_ctx->builder, func_idx_ptr,
INTPTR_T_PTR_TYPE, "func_idx_ptr"))) {
aot_set_last_error("llvm get func_idx_ptr failed");
return false;
}
if (!LLVMBuildStore(comp_ctx->builder, func_idx_val,
func_idx_ptr)) {
aot_set_last_error("llvm build store failed");
return false;
}
}
}
else {
/* jit mode: frame->function = module_inst->e->functions + func_index */
LLVMValueRef functions;
uint32 offset_functions =
get_module_inst_extra_offset(comp_ctx)
+ offsetof(WASMModuleInstanceExtra, functions);
offset = I32_CONST(offset_functions);
CHECK_LLVM_CONST(offset);
if (!(functions = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
func_ctx->aot_inst, &offset, 1,
"functions_addr"))) {
aot_set_last_error("llvm build inbounds gep failed");
return false;
}
if (!(functions = LLVMBuildBitCast(comp_ctx->builder, functions,
int8_ptr_type, "functions_ptr"))) {
aot_set_last_error("llvm build bitcast failed");
return false;
}
if (!(functions = LLVMBuildLoad2(comp_ctx->builder, INT8_PTR_TYPE,
functions, "functions"))) {
aot_set_last_error("llvm build load failed");
return false;
}
offset = I32_CONST(sizeof(WASMFunctionInstance) * func_idx);
CHECK_LLVM_CONST(offset);