aot_emit_variable.c 8.6 KB

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