aot_emit_variable.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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;
  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 = comp_data->globals[global_idx - import_global_count].type;
  160. }
  161. if (comp_ctx->enable_gc && aot_is_type_gc_reftype(global_type))
  162. global_type = VALUE_TYPE_GC_REF;
  163. offset = I32_CONST(global_offset);
  164. if (!(global_ptr = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  165. func_ctx->aot_inst, &offset, 1,
  166. "global_ptr_tmp"))) {
  167. aot_set_last_error("llvm build in bounds gep failed.");
  168. return false;
  169. }
  170. switch (global_type) {
  171. case VALUE_TYPE_I32:
  172. case VALUE_TYPE_EXTERNREF:
  173. case VALUE_TYPE_FUNCREF:
  174. ptr_type = INT32_PTR_TYPE;
  175. break;
  176. case VALUE_TYPE_I64:
  177. ptr_type = INT64_PTR_TYPE;
  178. break;
  179. case VALUE_TYPE_F32:
  180. ptr_type = F32_PTR_TYPE;
  181. break;
  182. case VALUE_TYPE_F64:
  183. ptr_type = F64_PTR_TYPE;
  184. break;
  185. case VALUE_TYPE_V128:
  186. ptr_type = V128_PTR_TYPE;
  187. break;
  188. #if WASM_ENABLE_GC != 0
  189. case VALUE_TYPE_GC_REF:
  190. ptr_type = GC_REF_PTR_TYPE;
  191. break;
  192. #endif
  193. default:
  194. bh_assert("unknown type");
  195. break;
  196. }
  197. if (!(global_ptr = LLVMBuildBitCast(comp_ctx->builder, global_ptr, ptr_type,
  198. "global_ptr"))) {
  199. aot_set_last_error("llvm build bit cast failed.");
  200. return false;
  201. }
  202. if (!is_set) {
  203. if (!(global =
  204. LLVMBuildLoad2(comp_ctx->builder, TO_LLVM_TYPE(global_type),
  205. global_ptr, "global"))) {
  206. aot_set_last_error("llvm build load failed.");
  207. return false;
  208. }
  209. /* All globals' data is 4-byte aligned */
  210. LLVMSetAlignment(global, 4);
  211. PUSH(global, global_type);
  212. }
  213. else {
  214. POP(global, global_type);
  215. if (is_aux_stack && comp_ctx->enable_aux_stack_check) {
  216. LLVMBasicBlockRef block_curr =
  217. LLVMGetInsertBlock(comp_ctx->builder);
  218. LLVMBasicBlockRef check_overflow_succ, check_underflow_succ;
  219. LLVMValueRef cmp, global_i64;
  220. /* Add basic blocks */
  221. if (!(check_overflow_succ = LLVMAppendBasicBlockInContext(
  222. comp_ctx->context, func_ctx->func,
  223. "check_overflow_succ"))) {
  224. aot_set_last_error("llvm add basic block failed.");
  225. return false;
  226. }
  227. LLVMMoveBasicBlockAfter(check_overflow_succ, block_curr);
  228. if (!(check_underflow_succ = LLVMAppendBasicBlockInContext(
  229. comp_ctx->context, func_ctx->func,
  230. "check_underflow_succ"))) {
  231. aot_set_last_error("llvm add basic block failed.");
  232. return false;
  233. }
  234. LLVMMoveBasicBlockAfter(check_underflow_succ, check_overflow_succ);
  235. if (!(global_i64 = LLVMBuildZExt(comp_ctx->builder, global,
  236. I64_TYPE, "global_i64"))) {
  237. aot_set_last_error("llvm build zext failed.");
  238. return false;
  239. }
  240. /* Check aux stack overflow */
  241. if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntULE, global_i64,
  242. func_ctx->aux_stack_bound, "cmp"))) {
  243. aot_set_last_error("llvm build icmp failed.");
  244. return false;
  245. }
  246. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_AUX_STACK_OVERFLOW,
  247. true, cmp, check_overflow_succ)) {
  248. return false;
  249. }
  250. /* Check aux stack underflow */
  251. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_overflow_succ);
  252. if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGT, global_i64,
  253. func_ctx->aux_stack_bottom, "cmp"))) {
  254. aot_set_last_error("llvm build icmp failed.");
  255. return false;
  256. }
  257. if (!aot_emit_exception(comp_ctx, func_ctx,
  258. EXCE_AUX_STACK_UNDERFLOW, true, cmp,
  259. check_underflow_succ)) {
  260. return false;
  261. }
  262. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_underflow_succ);
  263. }
  264. if (!(res = LLVMBuildStore(comp_ctx->builder, global, global_ptr))) {
  265. aot_set_last_error("llvm build store failed.");
  266. return false;
  267. }
  268. /* All globals' data is 4-byte aligned */
  269. LLVMSetAlignment(res, 4);
  270. }
  271. return true;
  272. fail:
  273. return false;
  274. }
  275. bool
  276. aot_compile_op_get_global(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  277. uint32 global_idx)
  278. {
  279. return compile_global(comp_ctx, func_ctx, global_idx, false, false);
  280. }
  281. bool
  282. aot_compile_op_set_global(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  283. uint32 global_idx, bool is_aux_stack)
  284. {
  285. return compile_global(comp_ctx, func_ctx, global_idx, true, is_aux_stack);
  286. }