aot_emit_variable.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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(AOTFuncContext *func_ctx, uint32 local_idx)
  18. {
  19. AOTFunc *aot_func = func_ctx->aot_func;
  20. uint32 param_count = aot_func->func_type->param_count;
  21. return local_idx < param_count
  22. ? aot_func->func_type->types[local_idx]
  23. : aot_func->local_types[local_idx - param_count];
  24. }
  25. bool
  26. aot_compile_op_get_local(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  27. uint32 local_idx)
  28. {
  29. char name[32];
  30. LLVMValueRef value;
  31. AOTValue *aot_value_top;
  32. CHECK_LOCAL(local_idx);
  33. snprintf(name, sizeof(name), "%s%d%s", "local", local_idx, "#");
  34. if (!(value = LLVMBuildLoad(comp_ctx->builder, func_ctx->locals[local_idx],
  35. name))) {
  36. aot_set_last_error("llvm build load fail");
  37. return false;
  38. }
  39. PUSH(value, get_local_type(func_ctx, local_idx));
  40. aot_value_top =
  41. func_ctx->block_stack.block_list_end->value_stack.value_list_end;
  42. aot_value_top->is_local = true;
  43. aot_value_top->local_idx = local_idx;
  44. return true;
  45. fail:
  46. return false;
  47. }
  48. bool
  49. aot_compile_op_set_local(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  50. uint32 local_idx)
  51. {
  52. LLVMValueRef value;
  53. CHECK_LOCAL(local_idx);
  54. POP(value, get_local_type(func_ctx, local_idx));
  55. if (!LLVMBuildStore(comp_ctx->builder, value,
  56. func_ctx->locals[local_idx])) {
  57. aot_set_last_error("llvm build store fail");
  58. return false;
  59. }
  60. aot_checked_addr_list_del(func_ctx, local_idx);
  61. return true;
  62. fail:
  63. return false;
  64. }
  65. bool
  66. aot_compile_op_tee_local(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  67. uint32 local_idx)
  68. {
  69. LLVMValueRef value;
  70. uint8 type;
  71. CHECK_LOCAL(local_idx);
  72. type = get_local_type(func_ctx, local_idx);
  73. POP(value, type);
  74. if (!LLVMBuildStore(comp_ctx->builder, value,
  75. func_ctx->locals[local_idx])) {
  76. aot_set_last_error("llvm build store fail");
  77. return false;
  78. }
  79. PUSH(value, type);
  80. aot_checked_addr_list_del(func_ctx, local_idx);
  81. return true;
  82. fail:
  83. return false;
  84. }
  85. static bool
  86. compile_global(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  87. uint32 global_idx, bool is_set, bool is_aux_stack)
  88. {
  89. AOTCompData *comp_data = comp_ctx->comp_data;
  90. uint32 import_global_count = comp_data->import_global_count;
  91. uint32 global_base_offset =
  92. offsetof(AOTModuleInstance, global_table_data.bytes)
  93. + sizeof(AOTMemoryInstance) * comp_ctx->comp_data->memory_count;
  94. uint32 global_offset;
  95. uint8 global_type;
  96. LLVMValueRef offset, global_ptr, global, res;
  97. LLVMTypeRef ptr_type = NULL;
  98. bh_assert(global_idx < import_global_count + comp_data->global_count);
  99. if (global_idx < import_global_count) {
  100. global_offset = global_base_offset
  101. + comp_data->import_globals[global_idx].data_offset;
  102. global_type = comp_data->import_globals[global_idx].type;
  103. }
  104. else {
  105. global_offset =
  106. global_base_offset
  107. + comp_data->globals[global_idx - import_global_count].data_offset;
  108. global_type = comp_data->globals[global_idx - import_global_count].type;
  109. }
  110. offset = I32_CONST(global_offset);
  111. if (!(global_ptr =
  112. LLVMBuildInBoundsGEP(comp_ctx->builder, func_ctx->aot_inst,
  113. &offset, 1, "global_ptr_tmp"))) {
  114. aot_set_last_error("llvm build in bounds gep failed.");
  115. return false;
  116. }
  117. switch (global_type) {
  118. case VALUE_TYPE_I32:
  119. case VALUE_TYPE_EXTERNREF:
  120. case VALUE_TYPE_FUNCREF:
  121. ptr_type = comp_ctx->basic_types.int32_ptr_type;
  122. break;
  123. case VALUE_TYPE_I64:
  124. ptr_type = comp_ctx->basic_types.int64_ptr_type;
  125. break;
  126. case VALUE_TYPE_F32:
  127. ptr_type = comp_ctx->basic_types.float32_ptr_type;
  128. break;
  129. case VALUE_TYPE_F64:
  130. ptr_type = comp_ctx->basic_types.float64_ptr_type;
  131. break;
  132. case VALUE_TYPE_V128:
  133. ptr_type = comp_ctx->basic_types.v128_ptr_type;
  134. break;
  135. default:
  136. bh_assert("unknown type");
  137. break;
  138. }
  139. if (!(global_ptr = LLVMBuildBitCast(comp_ctx->builder, global_ptr, ptr_type,
  140. "global_ptr"))) {
  141. aot_set_last_error("llvm build bit cast failed.");
  142. return false;
  143. }
  144. if (!is_set) {
  145. if (!(global =
  146. LLVMBuildLoad(comp_ctx->builder, global_ptr, "global"))) {
  147. aot_set_last_error("llvm build load failed.");
  148. return false;
  149. }
  150. /* All globals' data is 4-byte aligned */
  151. LLVMSetAlignment(global, 4);
  152. PUSH(global, global_type);
  153. }
  154. else {
  155. POP(global, global_type);
  156. if (is_aux_stack && comp_ctx->enable_aux_stack_check) {
  157. LLVMBasicBlockRef block_curr =
  158. LLVMGetInsertBlock(comp_ctx->builder);
  159. LLVMBasicBlockRef check_overflow_succ, check_underflow_succ;
  160. LLVMValueRef cmp;
  161. /* Add basic blocks */
  162. if (!(check_overflow_succ = LLVMAppendBasicBlockInContext(
  163. comp_ctx->context, func_ctx->func,
  164. "check_overflow_succ"))) {
  165. aot_set_last_error("llvm add basic block failed.");
  166. return false;
  167. }
  168. LLVMMoveBasicBlockAfter(check_overflow_succ, block_curr);
  169. if (!(check_underflow_succ = LLVMAppendBasicBlockInContext(
  170. comp_ctx->context, func_ctx->func,
  171. "check_underflow_succ"))) {
  172. aot_set_last_error("llvm add basic block failed.");
  173. return false;
  174. }
  175. LLVMMoveBasicBlockAfter(check_underflow_succ, check_overflow_succ);
  176. /* Check aux stack overflow */
  177. if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntULE, global,
  178. func_ctx->aux_stack_bound, "cmp"))) {
  179. aot_set_last_error("llvm build icmp failed.");
  180. return false;
  181. }
  182. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_AUX_STACK_OVERFLOW,
  183. true, cmp, check_overflow_succ)) {
  184. return false;
  185. }
  186. /* Check aux stack underflow */
  187. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_overflow_succ);
  188. if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGT, global,
  189. func_ctx->aux_stack_bottom, "cmp"))) {
  190. aot_set_last_error("llvm build icmp failed.");
  191. return false;
  192. }
  193. if (!aot_emit_exception(comp_ctx, func_ctx,
  194. EXCE_AUX_STACK_UNDERFLOW, true, cmp,
  195. check_underflow_succ)) {
  196. return false;
  197. }
  198. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_underflow_succ);
  199. }
  200. if (!(res = LLVMBuildStore(comp_ctx->builder, global, global_ptr))) {
  201. aot_set_last_error("llvm build store failed.");
  202. return false;
  203. }
  204. /* All globals' data is 4-byte aligned */
  205. LLVMSetAlignment(res, 4);
  206. }
  207. return true;
  208. fail:
  209. return false;
  210. }
  211. bool
  212. aot_compile_op_get_global(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  213. uint32 global_idx)
  214. {
  215. return compile_global(comp_ctx, func_ctx, global_idx, false, false);
  216. }
  217. bool
  218. aot_compile_op_set_global(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  219. uint32 global_idx, bool is_aux_stack)
  220. {
  221. return compile_global(comp_ctx, func_ctx, global_idx, true, is_aux_stack);
  222. }