aot_emit_variable.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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[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. bool
  56. aot_compile_op_set_local(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  57. uint32 local_idx)
  58. {
  59. LLVMValueRef value;
  60. uint8 local_type;
  61. uint32 n;
  62. CHECK_LOCAL(local_idx);
  63. local_type = get_local_type(comp_ctx, func_ctx, local_idx);
  64. POP(value, local_type);
  65. if (comp_ctx->aot_frame) {
  66. /* Get the slot index */
  67. n = comp_ctx->aot_frame->cur_wasm_func->local_offsets[local_idx];
  68. bh_assert(comp_ctx->aot_frame->lp[n].type == local_type);
  69. switch (local_type) {
  70. case VALUE_TYPE_I32:
  71. set_local_i32(comp_ctx->aot_frame, n, value);
  72. break;
  73. case VALUE_TYPE_I64:
  74. set_local_i64(comp_ctx->aot_frame, n, value);
  75. break;
  76. case VALUE_TYPE_F32:
  77. set_local_f32(comp_ctx->aot_frame, n, value);
  78. break;
  79. case VALUE_TYPE_F64:
  80. set_local_f64(comp_ctx->aot_frame, n, value);
  81. break;
  82. case VALUE_TYPE_V128:
  83. set_local_v128(comp_ctx->aot_frame, n, value);
  84. break;
  85. case VALUE_TYPE_FUNCREF:
  86. case VALUE_TYPE_EXTERNREF:
  87. set_local_ref(comp_ctx->aot_frame, n, value, local_type);
  88. break;
  89. #if WASM_ENABLE_GC != 0
  90. case VALUE_TYPE_GC_REF:
  91. set_local_gc_ref(comp_ctx->aot_frame, n, value, local_type);
  92. break;
  93. #endif
  94. default:
  95. bh_assert(0);
  96. break;
  97. }
  98. }
  99. if (!LLVMBuildStore(comp_ctx->builder, value,
  100. func_ctx->locals[local_idx])) {
  101. aot_set_last_error("llvm build store fail");
  102. return false;
  103. }
  104. aot_checked_addr_list_del(func_ctx, local_idx);
  105. return true;
  106. fail:
  107. return false;
  108. }
  109. bool
  110. aot_compile_op_tee_local(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  111. uint32 local_idx)
  112. {
  113. LLVMValueRef value;
  114. uint8 type;
  115. CHECK_LOCAL(local_idx);
  116. type = get_local_type(comp_ctx, func_ctx, local_idx);
  117. POP(value, type);
  118. if (!LLVMBuildStore(comp_ctx->builder, value,
  119. func_ctx->locals[local_idx])) {
  120. aot_set_last_error("llvm build store fail");
  121. return false;
  122. }
  123. PUSH(value, type);
  124. aot_checked_addr_list_del(func_ctx, local_idx);
  125. return true;
  126. fail:
  127. return false;
  128. }
  129. static bool
  130. compile_global(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  131. uint32 global_idx, bool is_set, bool is_aux_stack)
  132. {
  133. const AOTCompData *comp_data = comp_ctx->comp_data;
  134. uint32 import_global_count = comp_data->import_global_count;
  135. uint32 global_base_offset;
  136. uint32 global_offset;
  137. uint8 global_type;
  138. LLVMValueRef offset, global_ptr, global, res;
  139. LLVMTypeRef ptr_type = NULL;
  140. global_base_offset =
  141. offsetof(AOTModuleInstance, global_table_data.bytes)
  142. + sizeof(AOTMemoryInstance) * comp_ctx->comp_data->memory_count;
  143. bh_assert(global_idx < import_global_count + comp_data->global_count);
  144. if (global_idx < import_global_count) {
  145. global_offset = global_base_offset
  146. + comp_data->import_globals[global_idx].data_offset;
  147. global_type = comp_data->import_globals[global_idx].type;
  148. }
  149. else {
  150. global_offset =
  151. global_base_offset
  152. + comp_data->globals[global_idx - import_global_count].data_offset;
  153. global_type = comp_data->globals[global_idx - import_global_count].type;
  154. }
  155. if (comp_ctx->enable_gc && aot_is_type_gc_reftype(global_type))
  156. global_type = VALUE_TYPE_GC_REF;
  157. offset = I32_CONST(global_offset);
  158. if (!(global_ptr = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  159. func_ctx->aot_inst, &offset, 1,
  160. "global_ptr_tmp"))) {
  161. aot_set_last_error("llvm build in bounds gep failed.");
  162. return false;
  163. }
  164. switch (global_type) {
  165. case VALUE_TYPE_I32:
  166. case VALUE_TYPE_EXTERNREF:
  167. case VALUE_TYPE_FUNCREF:
  168. ptr_type = INT32_PTR_TYPE;
  169. break;
  170. case VALUE_TYPE_I64:
  171. ptr_type = INT64_PTR_TYPE;
  172. break;
  173. case VALUE_TYPE_F32:
  174. ptr_type = F32_PTR_TYPE;
  175. break;
  176. case VALUE_TYPE_F64:
  177. ptr_type = F64_PTR_TYPE;
  178. break;
  179. case VALUE_TYPE_V128:
  180. ptr_type = V128_PTR_TYPE;
  181. break;
  182. #if WASM_ENABLE_GC != 0
  183. case VALUE_TYPE_GC_REF:
  184. ptr_type = GC_REF_PTR_TYPE;
  185. break;
  186. #endif
  187. default:
  188. bh_assert("unknown type");
  189. break;
  190. }
  191. if (!(global_ptr = LLVMBuildBitCast(comp_ctx->builder, global_ptr, ptr_type,
  192. "global_ptr"))) {
  193. aot_set_last_error("llvm build bit cast failed.");
  194. return false;
  195. }
  196. if (!is_set) {
  197. if (!(global =
  198. LLVMBuildLoad2(comp_ctx->builder, TO_LLVM_TYPE(global_type),
  199. global_ptr, "global"))) {
  200. aot_set_last_error("llvm build load failed.");
  201. return false;
  202. }
  203. /* All globals' data is 4-byte aligned */
  204. LLVMSetAlignment(global, 4);
  205. PUSH(global, global_type);
  206. }
  207. else {
  208. POP(global, global_type);
  209. if (is_aux_stack && comp_ctx->enable_aux_stack_check) {
  210. LLVMBasicBlockRef block_curr =
  211. LLVMGetInsertBlock(comp_ctx->builder);
  212. LLVMBasicBlockRef check_overflow_succ, check_underflow_succ;
  213. LLVMValueRef cmp;
  214. /* Add basic blocks */
  215. if (!(check_overflow_succ = LLVMAppendBasicBlockInContext(
  216. comp_ctx->context, func_ctx->func,
  217. "check_overflow_succ"))) {
  218. aot_set_last_error("llvm add basic block failed.");
  219. return false;
  220. }
  221. LLVMMoveBasicBlockAfter(check_overflow_succ, block_curr);
  222. if (!(check_underflow_succ = LLVMAppendBasicBlockInContext(
  223. comp_ctx->context, func_ctx->func,
  224. "check_underflow_succ"))) {
  225. aot_set_last_error("llvm add basic block failed.");
  226. return false;
  227. }
  228. LLVMMoveBasicBlockAfter(check_underflow_succ, check_overflow_succ);
  229. /* Check aux stack overflow */
  230. if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntULE, global,
  231. func_ctx->aux_stack_bound, "cmp"))) {
  232. aot_set_last_error("llvm build icmp failed.");
  233. return false;
  234. }
  235. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_AUX_STACK_OVERFLOW,
  236. true, cmp, check_overflow_succ)) {
  237. return false;
  238. }
  239. /* Check aux stack underflow */
  240. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_overflow_succ);
  241. if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGT, global,
  242. func_ctx->aux_stack_bottom, "cmp"))) {
  243. aot_set_last_error("llvm build icmp failed.");
  244. return false;
  245. }
  246. if (!aot_emit_exception(comp_ctx, func_ctx,
  247. EXCE_AUX_STACK_UNDERFLOW, true, cmp,
  248. check_underflow_succ)) {
  249. return false;
  250. }
  251. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_underflow_succ);
  252. }
  253. if (!(res = LLVMBuildStore(comp_ctx->builder, global, global_ptr))) {
  254. aot_set_last_error("llvm build store failed.");
  255. return false;
  256. }
  257. /* All globals' data is 4-byte aligned */
  258. LLVMSetAlignment(res, 4);
  259. }
  260. return true;
  261. fail:
  262. return false;
  263. }
  264. bool
  265. aot_compile_op_get_global(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  266. uint32 global_idx)
  267. {
  268. return compile_global(comp_ctx, func_ctx, global_idx, false, false);
  269. }
  270. bool
  271. aot_compile_op_set_global(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  272. uint32 global_idx, bool is_aux_stack)
  273. {
  274. return compile_global(comp_ctx, func_ctx, global_idx, true, is_aux_stack);
  275. }