aot_emit_variable.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "aot_emit_variable.h"
  6. #include "aot_emit_exception.h"
  7. #include "../aot/aot_runtime.h"
  8. #define CHECK_LOCAL(idx) \
  9. do { \
  10. if (idx >= func_ctx->aot_func->func_type->param_count \
  11. + func_ctx->aot_func->local_count) { \
  12. aot_set_last_error("local index out of range"); \
  13. return false; \
  14. } \
  15. } while (0)
  16. static uint8
  17. get_local_type(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  18. uint32 local_idx)
  19. {
  20. AOTFunc *aot_func = func_ctx->aot_func;
  21. uint32 param_count = aot_func->func_type->param_count;
  22. uint8 local_type;
  23. local_type = local_idx < param_count
  24. ? aot_func->func_type->types[local_idx]
  25. : aot_func->local_types_wp[local_idx - param_count];
  26. if (comp_ctx->enable_gc && aot_is_type_gc_reftype(local_type))
  27. local_type = VALUE_TYPE_GC_REF;
  28. return local_type;
  29. }
  30. bool
  31. aot_compile_op_get_local(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  32. uint32 local_idx)
  33. {
  34. char name[32];
  35. LLVMValueRef value;
  36. AOTValue *aot_value_top;
  37. uint8 local_type;
  38. CHECK_LOCAL(local_idx);
  39. local_type = get_local_type(comp_ctx, func_ctx, local_idx);
  40. snprintf(name, sizeof(name), "%s%d%s", "local", local_idx, "#");
  41. if (!(value = LLVMBuildLoad2(comp_ctx->builder, TO_LLVM_TYPE(local_type),
  42. func_ctx->locals[local_idx], name))) {
  43. aot_set_last_error("llvm build load fail");
  44. return false;
  45. }
  46. PUSH(value, local_type);
  47. aot_value_top =
  48. func_ctx->block_stack.block_list_end->value_stack.value_list_end;
  49. aot_value_top->is_local = true;
  50. aot_value_top->local_idx = local_idx;
  51. return true;
  52. fail:
  53. return false;
  54. }
  55. static bool
  56. aot_compile_op_set_or_tee_local(AOTCompContext *comp_ctx,
  57. AOTFuncContext *func_ctx, uint32 local_idx,
  58. bool is_tee_local)
  59. {
  60. LLVMValueRef value;
  61. uint8 local_type;
  62. uint32 n;
  63. CHECK_LOCAL(local_idx);
  64. local_type = get_local_type(comp_ctx, func_ctx, local_idx);
  65. POP(value, local_type);
  66. if (comp_ctx->aot_frame) {
  67. /* Get the slot index */
  68. n = func_ctx->aot_func->local_offsets[local_idx];
  69. bh_assert(comp_ctx->aot_frame->lp[n].type == local_type);
  70. switch (local_type) {
  71. case VALUE_TYPE_I32:
  72. set_local_i32(comp_ctx->aot_frame, n, value);
  73. break;
  74. case VALUE_TYPE_I64:
  75. set_local_i64(comp_ctx->aot_frame, n, value);
  76. break;
  77. case VALUE_TYPE_F32:
  78. set_local_f32(comp_ctx->aot_frame, n, value);
  79. break;
  80. case VALUE_TYPE_F64:
  81. set_local_f64(comp_ctx->aot_frame, n, value);
  82. break;
  83. case VALUE_TYPE_V128:
  84. set_local_v128(comp_ctx->aot_frame, n, value);
  85. break;
  86. case VALUE_TYPE_FUNCREF:
  87. case VALUE_TYPE_EXTERNREF:
  88. set_local_ref(comp_ctx->aot_frame, n, value, local_type);
  89. break;
  90. #if WASM_ENABLE_GC != 0
  91. case VALUE_TYPE_GC_REF:
  92. set_local_gc_ref(comp_ctx->aot_frame, n, value, local_type);
  93. break;
  94. #endif
  95. default:
  96. bh_assert(0);
  97. break;
  98. }
  99. }
  100. if (!LLVMBuildStore(comp_ctx->builder, value,
  101. func_ctx->locals[local_idx])) {
  102. aot_set_last_error("llvm build store fail");
  103. return false;
  104. }
  105. if (is_tee_local) {
  106. PUSH(value, local_type);
  107. }
  108. aot_checked_addr_list_del(func_ctx, local_idx);
  109. return true;
  110. fail:
  111. return false;
  112. }
  113. bool
  114. aot_compile_op_set_local(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  115. uint32 local_idx)
  116. {
  117. return aot_compile_op_set_or_tee_local(comp_ctx, func_ctx, local_idx,
  118. false);
  119. }
  120. bool
  121. aot_compile_op_tee_local(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  122. uint32 local_idx)
  123. {
  124. return aot_compile_op_set_or_tee_local(comp_ctx, func_ctx, local_idx, true);
  125. }
  126. static bool
  127. compile_global(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  128. uint32 global_idx, bool is_set, bool is_aux_stack)
  129. {
  130. const AOTCompData *comp_data = comp_ctx->comp_data;
  131. uint32 import_global_count = comp_data->import_global_count;
  132. uint32 global_base_offset;
  133. uint32 global_offset;
  134. uint8 global_type;
  135. LLVMValueRef offset, global_ptr, global, res;
  136. LLVMTypeRef ptr_type = NULL;
  137. global_base_offset =
  138. offsetof(AOTModuleInstance, global_table_data.bytes)
  139. + sizeof(AOTMemoryInstance) * comp_ctx->comp_data->memory_count;
  140. bh_assert(global_idx < import_global_count + comp_data->global_count);
  141. if (global_idx < import_global_count) {
  142. global_offset =
  143. global_base_offset
  144. /* Get global data offset according to target info */
  145. + (comp_ctx->pointer_size == sizeof(uint64)
  146. ? comp_data->import_globals[global_idx].data_offset_64bit
  147. : comp_data->import_globals[global_idx].data_offset_32bit);
  148. global_type = comp_data->import_globals[global_idx].type.val_type;
  149. }
  150. else {
  151. global_offset =
  152. global_base_offset
  153. /* Get global data offset according to target info */
  154. + (comp_ctx->pointer_size == sizeof(uint64)
  155. ? comp_data->globals[global_idx - import_global_count]
  156. .data_offset_64bit
  157. : comp_data->globals[global_idx - import_global_count]
  158. .data_offset_32bit);
  159. global_type =
  160. comp_data->globals[global_idx - import_global_count].type.val_type;
  161. }
  162. if (comp_ctx->enable_gc && aot_is_type_gc_reftype(global_type))
  163. global_type = VALUE_TYPE_GC_REF;
  164. offset = I32_CONST(global_offset);
  165. if (!(global_ptr = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  166. func_ctx->aot_inst, &offset, 1,
  167. "global_ptr_tmp"))) {
  168. aot_set_last_error("llvm build in bounds gep failed.");
  169. return false;
  170. }
  171. switch (global_type) {
  172. case VALUE_TYPE_I32:
  173. case VALUE_TYPE_EXTERNREF:
  174. case VALUE_TYPE_FUNCREF:
  175. ptr_type = INT32_PTR_TYPE;
  176. break;
  177. case VALUE_TYPE_I64:
  178. ptr_type = INT64_PTR_TYPE;
  179. break;
  180. case VALUE_TYPE_F32:
  181. ptr_type = F32_PTR_TYPE;
  182. break;
  183. case VALUE_TYPE_F64:
  184. ptr_type = F64_PTR_TYPE;
  185. break;
  186. case VALUE_TYPE_V128:
  187. ptr_type = V128_PTR_TYPE;
  188. break;
  189. #if WASM_ENABLE_GC != 0
  190. case VALUE_TYPE_GC_REF:
  191. ptr_type = GC_REF_PTR_TYPE;
  192. break;
  193. #endif
  194. default:
  195. bh_assert("unknown type");
  196. break;
  197. }
  198. if (!(global_ptr = LLVMBuildBitCast(comp_ctx->builder, global_ptr, ptr_type,
  199. "global_ptr"))) {
  200. aot_set_last_error("llvm build bit cast failed.");
  201. return false;
  202. }
  203. if (!is_set) {
  204. if (!(global =
  205. LLVMBuildLoad2(comp_ctx->builder, TO_LLVM_TYPE(global_type),
  206. global_ptr, "global"))) {
  207. aot_set_last_error("llvm build load failed.");
  208. return false;
  209. }
  210. /* All globals' data is 4-byte aligned */
  211. LLVMSetAlignment(global, 4);
  212. PUSH(global, global_type);
  213. }
  214. else {
  215. POP(global, global_type);
  216. if (is_aux_stack && comp_ctx->enable_aux_stack_check) {
  217. LLVMBasicBlockRef block_curr =
  218. LLVMGetInsertBlock(comp_ctx->builder);
  219. LLVMBasicBlockRef check_overflow_succ, check_underflow_succ;
  220. LLVMValueRef cmp, global_i64;
  221. /* Add basic blocks */
  222. if (!(check_overflow_succ = LLVMAppendBasicBlockInContext(
  223. comp_ctx->context, func_ctx->func,
  224. "check_overflow_succ"))) {
  225. aot_set_last_error("llvm add basic block failed.");
  226. return false;
  227. }
  228. LLVMMoveBasicBlockAfter(check_overflow_succ, block_curr);
  229. if (!(check_underflow_succ = LLVMAppendBasicBlockInContext(
  230. comp_ctx->context, func_ctx->func,
  231. "check_underflow_succ"))) {
  232. aot_set_last_error("llvm add basic block failed.");
  233. return false;
  234. }
  235. LLVMMoveBasicBlockAfter(check_underflow_succ, check_overflow_succ);
  236. if (!(global_i64 = LLVMBuildZExt(comp_ctx->builder, global,
  237. I64_TYPE, "global_i64"))) {
  238. aot_set_last_error("llvm build zext failed.");
  239. return false;
  240. }
  241. /* Check aux stack overflow */
  242. if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntULE, global_i64,
  243. func_ctx->aux_stack_bound, "cmp"))) {
  244. aot_set_last_error("llvm build icmp failed.");
  245. return false;
  246. }
  247. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_AUX_STACK_OVERFLOW,
  248. true, cmp, check_overflow_succ)) {
  249. return false;
  250. }
  251. /* Check aux stack underflow */
  252. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_overflow_succ);
  253. if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGT, global_i64,
  254. func_ctx->aux_stack_bottom, "cmp"))) {
  255. aot_set_last_error("llvm build icmp failed.");
  256. return false;
  257. }
  258. if (!aot_emit_exception(comp_ctx, func_ctx,
  259. EXCE_AUX_STACK_UNDERFLOW, true, cmp,
  260. check_underflow_succ)) {
  261. return false;
  262. }
  263. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_underflow_succ);
  264. }
  265. if (!(res = LLVMBuildStore(comp_ctx->builder, global, global_ptr))) {
  266. aot_set_last_error("llvm build store failed.");
  267. return false;
  268. }
  269. /* All globals' data is 4-byte aligned */
  270. LLVMSetAlignment(res, 4);
  271. }
  272. return true;
  273. fail:
  274. return false;
  275. }
  276. bool
  277. aot_compile_op_get_global(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  278. uint32 global_idx)
  279. {
  280. return compile_global(comp_ctx, func_ctx, global_idx, false, false);
  281. }
  282. bool
  283. aot_compile_op_set_global(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  284. uint32 global_idx, bool is_aux_stack)
  285. {
  286. return compile_global(comp_ctx, func_ctx, global_idx, true, is_aux_stack);
  287. }