aot_emit_variable.c 8.7 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) do { \
  9. if (idx >= func_ctx->aot_func->func_type->param_count \
  10. + func_ctx->aot_func->local_count) { \
  11. aot_set_last_error("local index out of range"); \
  12. return false; \
  13. } \
  14. } while (0)
  15. static uint8
  16. get_local_type(AOTFuncContext *func_ctx, uint32 local_idx)
  17. {
  18. AOTFunc *aot_func = func_ctx->aot_func;
  19. uint32 param_count = aot_func->func_type->param_count;
  20. return local_idx < param_count
  21. ? aot_func->func_type->types[local_idx]
  22. : aot_func->local_types[local_idx - param_count];
  23. }
  24. bool
  25. aot_compile_op_get_local(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  26. uint32 local_idx)
  27. {
  28. char name[32];
  29. LLVMValueRef value;
  30. AOTValue *aot_value;
  31. CHECK_LOCAL(local_idx);
  32. snprintf(name, sizeof(name), "%s%d%s", "local", local_idx, "#");
  33. if (!(value = LLVMBuildLoad(comp_ctx->builder,
  34. 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 = func_ctx->block_stack.block_list_end->value_stack.value_list_end;
  41. aot_value->is_local = true;
  42. aot_value->local_idx = local_idx;
  43. return true;
  44. fail:
  45. return false;
  46. }
  47. bool
  48. aot_compile_op_set_local(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  49. uint32 local_idx)
  50. {
  51. LLVMValueRef value;
  52. CHECK_LOCAL(local_idx);
  53. POP(value, get_local_type(func_ctx, local_idx));
  54. if (!LLVMBuildStore(comp_ctx->builder,
  55. 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,
  75. value,
  76. func_ctx->locals[local_idx])) {
  77. aot_set_last_error("llvm build store fail");
  78. return false;
  79. }
  80. PUSH(value, type);
  81. aot_checked_addr_list_del(func_ctx, local_idx);
  82. return true;
  83. fail:
  84. return false;
  85. }
  86. static bool
  87. compile_global(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  88. uint32 global_idx, bool is_set, bool is_aux_stack)
  89. {
  90. AOTCompData *comp_data = comp_ctx->comp_data;
  91. uint32 import_global_count = comp_data->import_global_count;
  92. uint32 global_base_offset =
  93. offsetof(AOTModuleInstance, global_table_data.bytes)
  94. + sizeof(AOTMemoryInstance) * comp_ctx->comp_data->memory_count;
  95. uint32 global_offset;
  96. uint8 global_type;
  97. LLVMValueRef offset, global_ptr, global, res;
  98. LLVMTypeRef ptr_type = NULL;
  99. bh_assert(global_idx < import_global_count + comp_data->global_count);
  100. if (global_idx < import_global_count) {
  101. global_offset = global_base_offset
  102. + comp_data->import_globals[global_idx].data_offset;
  103. global_type = comp_data->import_globals[global_idx].type;
  104. }
  105. else {
  106. global_offset = global_base_offset
  107. + comp_data->globals[global_idx - import_global_count].data_offset;
  108. global_type =
  109. comp_data->globals[global_idx - import_global_count].type;
  110. }
  111. offset = I32_CONST(global_offset);
  112. if (!(global_ptr = 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,
  140. ptr_type, "global_ptr"))) {
  141. aot_set_last_error("llvm build bit cast failed.");
  142. return false;
  143. }
  144. if (!is_set) {
  145. if (!(global = LLVMBuildLoad(comp_ctx->builder,
  146. 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 = LLVMGetInsertBlock(comp_ctx->builder);
  158. LLVMBasicBlockRef check_overflow_succ, check_underflow_succ;
  159. LLVMValueRef cmp;
  160. /* Add basic blocks */
  161. if (!(check_overflow_succ =
  162. LLVMAppendBasicBlockInContext(comp_ctx->context,
  163. 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 =
  170. LLVMAppendBasicBlockInContext(comp_ctx->context,
  171. func_ctx->func,
  172. "check_underflow_succ"))) {
  173. aot_set_last_error("llvm add basic block failed.");
  174. return false;
  175. }
  176. LLVMMoveBasicBlockAfter(check_underflow_succ, check_overflow_succ);
  177. /* Check aux stack overflow */
  178. if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntULE,
  179. global, func_ctx->aux_stack_bound,
  180. "cmp"))) {
  181. aot_set_last_error("llvm build icmp failed.");
  182. return false;
  183. }
  184. if (!aot_emit_exception(comp_ctx, func_ctx,
  185. EXCE_AUX_STACK_OVERFLOW,
  186. true, cmp, check_overflow_succ)) {
  187. return false;
  188. }
  189. /* Check aux stack underflow */
  190. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_overflow_succ);
  191. if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntUGT,
  192. global, func_ctx->aux_stack_bottom,
  193. "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,
  199. true, cmp, check_underflow_succ)) {
  200. return false;
  201. }
  202. LLVMPositionBuilderAtEnd(comp_ctx->builder, check_underflow_succ);
  203. }
  204. if (!(res = LLVMBuildStore(comp_ctx->builder,
  205. global, global_ptr))) {
  206. aot_set_last_error("llvm build store failed.");
  207. return false;
  208. }
  209. /* All globals' data is 4-byte aligned */
  210. LLVMSetAlignment(res, 4);
  211. }
  212. return true;
  213. fail:
  214. return false;
  215. }
  216. bool
  217. aot_compile_op_get_global(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  218. uint32 global_idx)
  219. {
  220. return compile_global(comp_ctx, func_ctx, global_idx, false, false);
  221. }
  222. bool
  223. aot_compile_op_set_global(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  224. uint32 global_idx, bool is_aux_stack)
  225. {
  226. return compile_global(comp_ctx, func_ctx, global_idx, true, is_aux_stack);
  227. }