aot_llvm.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "aot_llvm.h"
  6. #include "aot_compiler.h"
  7. #include "aot_emit_exception.h"
  8. #include "../aot/aot_runtime.h"
  9. LLVMTypeRef
  10. wasm_type_to_llvm_type(AOTLLVMTypes *llvm_types, uint8 wasm_type)
  11. {
  12. switch (wasm_type) {
  13. case VALUE_TYPE_I32:
  14. return llvm_types->int32_type;
  15. case VALUE_TYPE_I64:
  16. return llvm_types->int64_type;
  17. case VALUE_TYPE_F32:
  18. return llvm_types->float32_type;
  19. case VALUE_TYPE_F64:
  20. return llvm_types->float64_type;
  21. case VALUE_TYPE_VOID:
  22. return llvm_types->void_type;
  23. }
  24. return NULL;
  25. }
  26. /**
  27. * Add LLVM function
  28. */
  29. static LLVMValueRef
  30. aot_add_llvm_func(AOTCompContext *comp_ctx, AOTFuncType *aot_func_type,
  31. uint32 func_index)
  32. {
  33. LLVMValueRef func = NULL;
  34. LLVMTypeRef *param_types, ret_type, func_type;
  35. LLVMValueRef local_value;
  36. char func_name[32];
  37. uint64 size;
  38. uint32 i, j = 0, param_count = (uint64)aot_func_type->param_count;
  39. /* exec env as first parameter */
  40. param_count++;
  41. /* Extra wasm function results(except the first one)'s address are
  42. * appended to aot function parameters. */
  43. if (aot_func_type->result_count > 1)
  44. param_count += aot_func_type->result_count - 1;
  45. /* Initialize parameter types of the LLVM function */
  46. size = sizeof(LLVMTypeRef) * ((uint64)param_count);
  47. if (size >= UINT32_MAX
  48. || !(param_types = wasm_runtime_malloc((uint32)size))) {
  49. aot_set_last_error("allocate memory failed.");
  50. return NULL;
  51. }
  52. /* exec env as first parameter */
  53. param_types[j++] = comp_ctx->exec_env_type;
  54. for (i = 0; i < aot_func_type->param_count; i++)
  55. param_types[j++] = TO_LLVM_TYPE(aot_func_type->types[i]);
  56. /* Extra results' address */
  57. for (i = 1; i < aot_func_type->result_count; i++, j++) {
  58. param_types[j] =
  59. TO_LLVM_TYPE(aot_func_type->types[aot_func_type->param_count + i]);
  60. if (!(param_types[j] = LLVMPointerType(param_types[j], 0))) {
  61. aot_set_last_error("llvm get pointer type failed.");
  62. goto fail;
  63. }
  64. }
  65. /* Resolve return type of the LLVM function */
  66. if (aot_func_type->result_count)
  67. ret_type = TO_LLVM_TYPE(aot_func_type->types[aot_func_type->param_count]);
  68. else
  69. ret_type = VOID_TYPE;
  70. /* Resolve function prototype */
  71. if (!(func_type = LLVMFunctionType(ret_type, param_types,
  72. param_count, false))) {
  73. aot_set_last_error("create LLVM function type failed.");
  74. goto fail;
  75. }
  76. /* Add LLVM function */
  77. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, func_index);
  78. if (!(func = LLVMAddFunction(comp_ctx->module, func_name, func_type))) {
  79. aot_set_last_error("add LLVM function failed.");
  80. goto fail;
  81. }
  82. j = 0;
  83. local_value = LLVMGetParam(func, j++);
  84. LLVMSetValueName(local_value, "exec_env");
  85. /* Set parameter names */
  86. for (i = 0; i < aot_func_type->param_count; i++) {
  87. local_value = LLVMGetParam(func, j++);
  88. LLVMSetValueName(local_value, "");
  89. }
  90. fail:
  91. wasm_runtime_free(param_types);
  92. return func;
  93. }
  94. static void
  95. free_block_memory(AOTBlock *block)
  96. {
  97. if (block->param_types)
  98. wasm_runtime_free(block->param_types);
  99. if (block->result_types)
  100. wasm_runtime_free(block->result_types);
  101. wasm_runtime_free(block);
  102. }
  103. /**
  104. * Create first AOTBlock, or function block for the function
  105. */
  106. static AOTBlock *
  107. aot_create_func_block(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  108. AOTFunc *func, AOTFuncType *aot_func_type)
  109. {
  110. AOTBlock *aot_block;
  111. uint32 param_count = aot_func_type->param_count,
  112. result_count = aot_func_type->result_count;
  113. /* Allocate memory */
  114. if (!(aot_block = wasm_runtime_malloc(sizeof(AOTBlock)))) {
  115. aot_set_last_error("allocate memory failed.");
  116. return NULL;
  117. }
  118. memset(aot_block, 0, sizeof(AOTBlock));
  119. if (param_count
  120. && !(aot_block->param_types = wasm_runtime_malloc(param_count))) {
  121. aot_set_last_error("allocate memory failed.");
  122. goto fail;
  123. }
  124. if (result_count) {
  125. if (!(aot_block->result_types = wasm_runtime_malloc(result_count))) {
  126. aot_set_last_error("allocate memory failed.");
  127. goto fail;
  128. }
  129. }
  130. /* Set block data */
  131. aot_block->label_type = LABEL_TYPE_FUNCTION;
  132. aot_block->param_count = param_count;
  133. memcpy(aot_block->param_types, aot_func_type->types, param_count);
  134. aot_block->result_count = result_count;
  135. memcpy(aot_block->result_types, aot_func_type->types + param_count, result_count);
  136. aot_block->wasm_code_end = func->code + func->code_size;
  137. /* Add function entry block */
  138. if (!(aot_block->llvm_entry_block =
  139. LLVMAppendBasicBlockInContext(comp_ctx->context, func_ctx->func,
  140. "func_begin"))) {
  141. aot_set_last_error("add LLVM basic block failed.");
  142. goto fail;
  143. }
  144. return aot_block;
  145. fail:
  146. free_block_memory(aot_block);
  147. return NULL;
  148. }
  149. static bool
  150. create_exception_blocks(AOTFuncContext *func_ctx)
  151. {
  152. if (!(func_ctx->exception_blocks =
  153. wasm_runtime_malloc(sizeof(LLVMBasicBlockRef) * EXCE_NUM))) {
  154. aot_set_last_error("allocate memory failed.");
  155. return false;;
  156. }
  157. memset(func_ctx->exception_blocks, 0,
  158. sizeof(LLVMBasicBlockRef) * EXCE_NUM);
  159. return true;
  160. }
  161. static bool
  162. create_memory_info(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  163. LLVMTypeRef int8_ptr_type, uint32 func_index)
  164. {
  165. LLVMValueRef offset, mem_info_base;
  166. uint32 memory_count;
  167. WASMModule *module = comp_ctx->comp_data->wasm_module;
  168. WASMFunction *func = module->functions[func_index];
  169. bool mem_space_unchanged = (!func->has_op_memory_grow && !func->has_op_func_call)
  170. || (!module->possible_memory_grow);
  171. #if WASM_ENABLE_SHARED_MEMORY != 0
  172. bool is_shared_memory;
  173. #endif
  174. func_ctx->mem_space_unchanged = mem_space_unchanged;
  175. memory_count = module->memory_count + module->import_memory_count;
  176. /* If the module dosen't have memory, reserve
  177. one mem_info space with empty content */
  178. if (memory_count == 0)
  179. memory_count = 1;
  180. if (!(func_ctx->mem_info =
  181. wasm_runtime_malloc(sizeof(AOTMemInfo) * memory_count))) {
  182. return false;
  183. }
  184. memset(func_ctx->mem_info, 0, sizeof(AOTMemInfo));
  185. /* Currently we only create memory info for memory 0 */
  186. /* Load memory base address */
  187. #if WASM_ENABLE_SHARED_MEMORY != 0
  188. is_shared_memory = comp_ctx->comp_data->memories[0].memory_flags & 0x02
  189. ? true : false;
  190. if (is_shared_memory) {
  191. LLVMValueRef shared_mem_addr;
  192. offset = I32_CONST(offsetof(AOTModuleInstance, memories));
  193. if (!offset) {
  194. aot_set_last_error("create llvm const failed.");
  195. return false;
  196. }
  197. /* aot_inst->memories */
  198. if (!(shared_mem_addr =
  199. LLVMBuildInBoundsGEP(comp_ctx->builder, func_ctx->aot_inst,
  200. &offset, 1, "shared_mem_addr_offset"))) {
  201. aot_set_last_error("llvm build in bounds gep failed");
  202. return false;
  203. }
  204. if (!(shared_mem_addr =
  205. LLVMBuildBitCast(comp_ctx->builder,
  206. shared_mem_addr, int8_ptr_type,
  207. "shared_mem_addr_ptr"))) {
  208. aot_set_last_error("llvm build bit cast failed");
  209. return false;
  210. }
  211. /* aot_inst->memories[0] */
  212. if (!(shared_mem_addr =
  213. LLVMBuildLoad(comp_ctx->builder,
  214. shared_mem_addr, "shared_mem_addr"))) {
  215. aot_set_last_error("llvm build load failed");
  216. return false;
  217. }
  218. if (!(shared_mem_addr =
  219. LLVMBuildBitCast(comp_ctx->builder,
  220. shared_mem_addr, int8_ptr_type,
  221. "shared_mem_addr_ptr"))) {
  222. aot_set_last_error("llvm build bit cast failed");
  223. return false;
  224. }
  225. if (!(shared_mem_addr =
  226. LLVMBuildLoad(comp_ctx->builder,
  227. shared_mem_addr, "shared_mem_addr"))) {
  228. aot_set_last_error("llvm build load failed");
  229. return false;
  230. }
  231. offset = I32_CONST(offsetof(AOTMemoryInstance, memory_data.ptr));
  232. if (!(func_ctx->mem_info[0].mem_base_addr =
  233. LLVMBuildInBoundsGEP(comp_ctx->builder, shared_mem_addr,
  234. &offset, 1, "mem_base_addr_offset"))) {
  235. aot_set_last_error("llvm build in bounds gep failed");
  236. return false;
  237. }
  238. offset = I32_CONST(offsetof(AOTMemoryInstance, mem_cur_page_count));
  239. if (!(func_ctx->mem_info[0].mem_cur_page_count_addr =
  240. LLVMBuildInBoundsGEP(comp_ctx->builder, shared_mem_addr,
  241. &offset, 1, "mem_cur_page_offset"))) {
  242. aot_set_last_error("llvm build in bounds gep failed");
  243. return false;
  244. }
  245. }
  246. else
  247. #endif
  248. {
  249. offset = I32_CONST(offsetof(AOTModuleInstance, global_table_data)
  250. + offsetof(AOTMemoryInstance, memory_data.ptr));
  251. if (!(func_ctx->mem_info[0].mem_base_addr =
  252. LLVMBuildInBoundsGEP(comp_ctx->builder, func_ctx->aot_inst,
  253. &offset, 1, "mem_base_addr_offset"))) {
  254. aot_set_last_error("llvm build in bounds gep failed");
  255. return false;
  256. }
  257. offset = I32_CONST(offsetof(AOTModuleInstance, global_table_data)
  258. + offsetof(AOTMemoryInstance, mem_cur_page_count));
  259. if (!(func_ctx->mem_info[0].mem_cur_page_count_addr =
  260. LLVMBuildInBoundsGEP(comp_ctx->builder, func_ctx->aot_inst,
  261. &offset, 1, "mem_cur_page_offset"))) {
  262. aot_set_last_error("llvm build in bounds gep failed");
  263. return false;
  264. }
  265. }
  266. /* Store mem info base address before cast */
  267. mem_info_base = func_ctx->mem_info[0].mem_base_addr;
  268. if (!(func_ctx->mem_info[0].mem_base_addr =
  269. LLVMBuildBitCast(comp_ctx->builder,
  270. func_ctx->mem_info[0].mem_base_addr,
  271. int8_ptr_type, "mem_base_addr_ptr"))) {
  272. aot_set_last_error("llvm build bit cast failed");
  273. return false;
  274. }
  275. if (!(func_ctx->mem_info[0].mem_cur_page_count_addr =
  276. LLVMBuildBitCast(comp_ctx->builder,
  277. func_ctx->mem_info[0].mem_cur_page_count_addr,
  278. INT32_PTR_TYPE, "mem_cur_page_ptr"))) {
  279. aot_set_last_error("llvm build bit cast failed");
  280. return false;
  281. }
  282. if (mem_space_unchanged) {
  283. if (!(func_ctx->mem_info[0].mem_base_addr =
  284. LLVMBuildLoad(comp_ctx->builder,
  285. func_ctx->mem_info[0].mem_base_addr,
  286. "mem_base_addr"))) {
  287. aot_set_last_error("llvm build load failed");
  288. return false;
  289. }
  290. if (!(func_ctx->mem_info[0].mem_cur_page_count_addr =
  291. LLVMBuildLoad(comp_ctx->builder,
  292. func_ctx->mem_info[0].mem_cur_page_count_addr,
  293. "mem_cur_page_count_addr"))) {
  294. aot_set_last_error("llvm build load failed");
  295. return false;
  296. }
  297. }
  298. #if WASM_ENABLE_SHARED_MEMORY != 0
  299. else if (is_shared_memory) {
  300. /* The base address for shared memory will never changed,
  301. we can load the value here */
  302. if (!(func_ctx->mem_info[0].mem_base_addr =
  303. LLVMBuildLoad(comp_ctx->builder,
  304. func_ctx->mem_info[0].mem_base_addr,
  305. "mem_base_addr"))) {
  306. aot_set_last_error("llvm build load failed");
  307. return false;
  308. }
  309. }
  310. #endif
  311. /* Load memory bound check constants */
  312. offset = I32_CONST(offsetof(AOTMemoryInstance, mem_bound_check_1byte)
  313. - offsetof(AOTMemoryInstance, memory_data.ptr));
  314. if (!(func_ctx->mem_info[0].mem_bound_check_1byte =
  315. LLVMBuildInBoundsGEP(comp_ctx->builder, mem_info_base,
  316. &offset, 1, "bound_check_1byte_offset"))) {
  317. aot_set_last_error("llvm build in bounds gep failed");
  318. return false;
  319. }
  320. if (!(func_ctx->mem_info[0].mem_bound_check_1byte =
  321. LLVMBuildBitCast(comp_ctx->builder,
  322. func_ctx->mem_info[0].mem_bound_check_1byte,
  323. INT64_PTR_TYPE, "bound_check_1byte_ptr"))) {
  324. aot_set_last_error("llvm build bit cast failed");
  325. return false;
  326. }
  327. if (mem_space_unchanged) {
  328. if (!(func_ctx->mem_info[0].mem_bound_check_1byte =
  329. LLVMBuildLoad(comp_ctx->builder,
  330. func_ctx->mem_info[0].mem_bound_check_1byte,
  331. "bound_check_1byte"))) {
  332. aot_set_last_error("llvm build load failed");
  333. return false;
  334. }
  335. }
  336. offset = I32_CONST(offsetof(AOTMemoryInstance, mem_bound_check_2bytes)
  337. - offsetof(AOTMemoryInstance, memory_data.ptr));
  338. if (!(func_ctx->mem_info[0].mem_bound_check_2bytes =
  339. LLVMBuildInBoundsGEP(comp_ctx->builder, mem_info_base,
  340. &offset, 1, "bound_check_2bytes_offset"))) {
  341. aot_set_last_error("llvm build in bounds gep failed");
  342. return false;
  343. }
  344. if (!(func_ctx->mem_info[0].mem_bound_check_2bytes =
  345. LLVMBuildBitCast(comp_ctx->builder,
  346. func_ctx->mem_info[0].mem_bound_check_2bytes,
  347. INT64_PTR_TYPE, "bound_check_2bytes_ptr"))) {
  348. aot_set_last_error("llvm build bit cast failed");
  349. return false;
  350. }
  351. if (mem_space_unchanged) {
  352. if (!(func_ctx->mem_info[0].mem_bound_check_2bytes =
  353. LLVMBuildLoad(comp_ctx->builder,
  354. func_ctx->mem_info[0].mem_bound_check_2bytes,
  355. "bound_check_2bytes"))) {
  356. aot_set_last_error("llvm build load failed");
  357. return false;
  358. }
  359. }
  360. offset = I32_CONST(offsetof(AOTMemoryInstance, mem_bound_check_4bytes)
  361. - offsetof(AOTMemoryInstance, memory_data.ptr));
  362. if (!(func_ctx->mem_info[0].mem_bound_check_4bytes =
  363. LLVMBuildInBoundsGEP(comp_ctx->builder, mem_info_base,
  364. &offset, 1, "bound_check_4bytes_offset"))) {
  365. aot_set_last_error("llvm build in bounds gep failed");
  366. return false;
  367. }
  368. if (!(func_ctx->mem_info[0].mem_bound_check_4bytes =
  369. LLVMBuildBitCast(comp_ctx->builder,
  370. func_ctx->mem_info[0].mem_bound_check_4bytes,
  371. INT64_PTR_TYPE, "bound_check_4bytes_ptr"))) {
  372. aot_set_last_error("llvm build bit cast failed");
  373. return false;
  374. }
  375. if (mem_space_unchanged) {
  376. if (!(func_ctx->mem_info[0].mem_bound_check_4bytes =
  377. LLVMBuildLoad(comp_ctx->builder,
  378. func_ctx->mem_info[0].mem_bound_check_4bytes,
  379. "bound_check_4bytes"))) {
  380. aot_set_last_error("llvm build load failed");
  381. return false;
  382. }
  383. }
  384. offset = I32_CONST(offsetof(AOTMemoryInstance, mem_bound_check_8bytes)
  385. - offsetof(AOTMemoryInstance, memory_data.ptr));
  386. if (!(func_ctx->mem_info[0].mem_bound_check_8bytes =
  387. LLVMBuildInBoundsGEP(comp_ctx->builder, mem_info_base,
  388. &offset, 1, "bound_check_8bytes_offset"))) {
  389. aot_set_last_error("llvm build in bounds gep failed");
  390. return false;
  391. }
  392. if (!(func_ctx->mem_info[0].mem_bound_check_8bytes =
  393. LLVMBuildBitCast(comp_ctx->builder,
  394. func_ctx->mem_info[0].mem_bound_check_8bytes,
  395. INT64_PTR_TYPE, "bound_check_8bytes_ptr"))) {
  396. aot_set_last_error("llvm build bit cast failed");
  397. return false;
  398. }
  399. if (mem_space_unchanged) {
  400. if (!(func_ctx->mem_info[0].mem_bound_check_8bytes =
  401. LLVMBuildLoad(comp_ctx->builder,
  402. func_ctx->mem_info[0].mem_bound_check_8bytes,
  403. "bound_check_8bytes"))) {
  404. aot_set_last_error("llvm build load failed");
  405. return false;
  406. }
  407. }
  408. /* Load bound_check_heap_base */
  409. offset = I32_CONST(offsetof(AOTMemoryInstance, mem_bound_check_heap_base)
  410. - offsetof(AOTMemoryInstance, memory_data.ptr));
  411. if (!(func_ctx->mem_info[0].mem_bound_check_heap_base =
  412. LLVMBuildInBoundsGEP(comp_ctx->builder, mem_info_base,
  413. &offset, 1, "bound_check_heap_base_offset"))) {
  414. aot_set_last_error("llvm build in bounds gep failed");
  415. return false;
  416. }
  417. if (!(func_ctx->mem_info[0].mem_bound_check_heap_base =
  418. LLVMBuildBitCast(comp_ctx->builder,
  419. func_ctx->mem_info[0].mem_bound_check_heap_base,
  420. INT64_PTR_TYPE, "bound_check_heap_base_tmp"))) {
  421. aot_set_last_error("llvm build bit cast failed");
  422. return false;
  423. }
  424. if (!(func_ctx->mem_info[0].mem_bound_check_heap_base =
  425. LLVMBuildLoad(comp_ctx->builder,
  426. func_ctx->mem_info[0].mem_bound_check_heap_base,
  427. "bound_check_heap_base"))) {
  428. aot_set_last_error("llvm build load failed");
  429. return false;
  430. }
  431. return true;
  432. }
  433. static bool
  434. create_table_base(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  435. {
  436. LLVMValueRef offset;
  437. offset = I32_CONST(offsetof(AOTModuleInstance, global_table_data.bytes)
  438. + comp_ctx->comp_data->global_data_size);
  439. func_ctx->table_base = LLVMBuildInBoundsGEP(comp_ctx->builder,
  440. func_ctx->aot_inst,
  441. &offset, 1,
  442. "table_base_tmp");
  443. if (!func_ctx->table_base) {
  444. aot_set_last_error("llvm build in bounds gep failed.");
  445. return false;
  446. }
  447. func_ctx->table_base = LLVMBuildBitCast(comp_ctx->builder, func_ctx->table_base,
  448. INT32_PTR_TYPE, "table_base");
  449. if (!func_ctx->table_base) {
  450. aot_set_last_error("llvm build bit cast failed.");
  451. return false;
  452. }
  453. return true;
  454. }
  455. static bool
  456. create_cur_exception(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  457. {
  458. LLVMValueRef offset;
  459. offset = I32_CONST(offsetof(AOTModuleInstance, cur_exception));
  460. func_ctx->cur_exception = LLVMBuildInBoundsGEP(comp_ctx->builder,
  461. func_ctx->aot_inst,
  462. &offset, 1,
  463. "cur_execption");
  464. if (!func_ctx->cur_exception) {
  465. aot_set_last_error("llvm build in bounds gep failed.");
  466. return false;
  467. }
  468. return true;
  469. }
  470. static bool
  471. create_func_type_indexes(AOTCompContext *comp_ctx,
  472. AOTFuncContext *func_ctx)
  473. {
  474. LLVMValueRef offset, func_type_indexes_ptr;
  475. LLVMTypeRef int32_ptr_type;
  476. offset = I32_CONST(offsetof(AOTModuleInstance, func_type_indexes.ptr));
  477. func_type_indexes_ptr = LLVMBuildInBoundsGEP(comp_ctx->builder,
  478. func_ctx->aot_inst,
  479. &offset, 1,
  480. "func_type_indexes_ptr");
  481. if (!func_type_indexes_ptr) {
  482. aot_set_last_error("llvm build add failed.");
  483. return false;
  484. }
  485. if (!(int32_ptr_type = LLVMPointerType(INT32_PTR_TYPE, 0))) {
  486. aot_set_last_error("llvm get pointer type failed.");
  487. return false;
  488. }
  489. func_ctx->func_type_indexes = LLVMBuildBitCast(comp_ctx->builder,
  490. func_type_indexes_ptr,
  491. int32_ptr_type,
  492. "func_type_indexes_tmp");
  493. if (!func_ctx->func_type_indexes) {
  494. aot_set_last_error("llvm build bit cast failed.");
  495. return false;
  496. }
  497. func_ctx->func_type_indexes = LLVMBuildLoad(comp_ctx->builder,
  498. func_ctx->func_type_indexes,
  499. "func_type_indexes");
  500. if (!func_ctx->func_type_indexes) {
  501. aot_set_last_error("llvm build load failed.");
  502. return false;
  503. }
  504. return true;
  505. }
  506. /**
  507. * Create function compiler context
  508. */
  509. static AOTFuncContext *
  510. aot_create_func_context(AOTCompData *comp_data, AOTCompContext *comp_ctx,
  511. AOTFunc *func, uint32 func_index)
  512. {
  513. AOTFuncContext *func_ctx;
  514. AOTFuncType *aot_func_type = comp_data->func_types[func->func_type_index];
  515. AOTBlock *aot_block;
  516. LLVMTypeRef int8_ptr_type, int32_ptr_type;
  517. LLVMValueRef aot_inst_offset = I32_TWO, aot_inst_addr;
  518. LLVMValueRef argv_buf_offset = I32_THREE, argv_buf_addr;
  519. LLVMValueRef stack_bound_offset = I32_FOUR, stack_bound_addr;
  520. char local_name[32];
  521. uint64 size;
  522. uint32 i, j = 0;
  523. /* Allocate memory for the function context */
  524. size = offsetof(AOTFuncContext, locals) + sizeof(LLVMValueRef) *
  525. ((uint64)aot_func_type->param_count + func->local_count);
  526. if (size >= UINT32_MAX
  527. || !(func_ctx = wasm_runtime_malloc((uint32)size))) {
  528. aot_set_last_error("allocate memory failed.");
  529. return NULL;
  530. }
  531. memset(func_ctx, 0, (uint32)size);
  532. func_ctx->aot_func = func;
  533. /* Add LLVM function */
  534. if (!(func_ctx->func = aot_add_llvm_func(comp_ctx, aot_func_type, func_index)))
  535. goto fail;
  536. /* Create function's first AOTBlock */
  537. if (!(aot_block = aot_create_func_block(comp_ctx, func_ctx,
  538. func, aot_func_type)))
  539. goto fail;
  540. aot_block_stack_push(&func_ctx->block_stack, aot_block);
  541. /* Add local variables */
  542. LLVMPositionBuilderAtEnd(comp_ctx->builder, aot_block->llvm_entry_block);
  543. /* Save the pameters for fast access */
  544. func_ctx->exec_env = LLVMGetParam(func_ctx->func, j++);
  545. /* Get aot inst address, the layout of exec_env is:
  546. exec_env->next, exec_env->prev, exec_env->module_inst, and argv_buf */
  547. if (!(aot_inst_addr =
  548. LLVMBuildInBoundsGEP(comp_ctx->builder, func_ctx->exec_env,
  549. &aot_inst_offset, 1, "aot_inst_addr"))) {
  550. aot_set_last_error("llvm build in bounds gep failed");
  551. goto fail;
  552. }
  553. /* Load aot inst */
  554. if (!(func_ctx->aot_inst = LLVMBuildLoad(comp_ctx->builder,
  555. aot_inst_addr, "aot_inst"))) {
  556. aot_set_last_error("llvm build load failed");
  557. goto fail;
  558. }
  559. /* Get argv buffer address */
  560. if (!(argv_buf_addr =
  561. LLVMBuildInBoundsGEP(comp_ctx->builder, func_ctx->exec_env,
  562. &argv_buf_offset, 1, "argv_buf_addr"))) {
  563. aot_set_last_error("llvm build in bounds gep failed");
  564. goto fail;
  565. }
  566. if (!(int32_ptr_type = LLVMPointerType(INT32_PTR_TYPE, 0))) {
  567. aot_set_last_error("llvm add pointer type failed");
  568. goto fail;
  569. }
  570. /* Convert to int32 pointer type */
  571. if (!(argv_buf_addr = LLVMBuildBitCast(comp_ctx->builder, argv_buf_addr,
  572. int32_ptr_type, "argv_buf_ptr"))) {
  573. aot_set_last_error("llvm build load failed");
  574. goto fail;
  575. }
  576. if (!(func_ctx->argv_buf = LLVMBuildLoad(comp_ctx->builder,
  577. argv_buf_addr, "argv_buf"))) {
  578. aot_set_last_error("llvm build load failed");
  579. goto fail;
  580. }
  581. /* Get native stack boundary address */
  582. if (!(stack_bound_addr =
  583. LLVMBuildInBoundsGEP(comp_ctx->builder, func_ctx->exec_env,
  584. &stack_bound_offset, 1, "stack_bound_addr"))) {
  585. aot_set_last_error("llvm build in bounds gep failed");
  586. goto fail;
  587. }
  588. if (!(func_ctx->native_stack_bound =
  589. LLVMBuildLoad(comp_ctx->builder,
  590. stack_bound_addr, "native_stack_bound"))) {
  591. aot_set_last_error("llvm build load failed");
  592. goto fail;
  593. }
  594. for (i = 0; i < aot_func_type->param_count; i++, j++) {
  595. snprintf(local_name, sizeof(local_name), "l%d", i);
  596. func_ctx->locals[i] =
  597. LLVMBuildAlloca(comp_ctx->builder,
  598. TO_LLVM_TYPE(aot_func_type->types[i]),
  599. local_name);
  600. if (!func_ctx->locals[i]) {
  601. aot_set_last_error("llvm build alloca failed.");
  602. goto fail;
  603. }
  604. if (!LLVMBuildStore(comp_ctx->builder,
  605. LLVMGetParam(func_ctx->func, j),
  606. func_ctx->locals[i])) {
  607. aot_set_last_error("llvm build store failed.");
  608. goto fail;
  609. }
  610. }
  611. for (i = 0; i < func->local_count; i++) {
  612. LLVMTypeRef local_type;
  613. LLVMValueRef local_value = NULL;
  614. snprintf(local_name, sizeof(local_name), "l%d",
  615. aot_func_type->param_count + i);
  616. local_type = TO_LLVM_TYPE(func->local_types[i]);
  617. func_ctx->locals[aot_func_type->param_count + i] =
  618. LLVMBuildAlloca(comp_ctx->builder, local_type, local_name);
  619. if (!func_ctx->locals[aot_func_type->param_count + i]) {
  620. aot_set_last_error("llvm build alloca failed.");
  621. goto fail;
  622. }
  623. switch (func->local_types[i]) {
  624. case VALUE_TYPE_I32:
  625. local_value = I32_ZERO;
  626. break;
  627. case VALUE_TYPE_I64:
  628. local_value = I64_ZERO;
  629. break;
  630. case VALUE_TYPE_F32:
  631. local_value = F32_ZERO;
  632. break;
  633. case VALUE_TYPE_F64:
  634. local_value = F64_ZERO;
  635. break;
  636. default:
  637. bh_assert(0);
  638. break;
  639. }
  640. if (!LLVMBuildStore(comp_ctx->builder, local_value,
  641. func_ctx->locals[aot_func_type->param_count + i])) {
  642. aot_set_last_error("llvm build store failed.");
  643. goto fail;
  644. }
  645. }
  646. if (aot_func_type->param_count + func->local_count > 0) {
  647. func_ctx->last_alloca = func_ctx->locals[aot_func_type->param_count
  648. + func->local_count - 1];
  649. if (!(func_ctx->last_alloca =
  650. LLVMBuildBitCast(comp_ctx->builder, func_ctx->last_alloca,
  651. INT8_PTR_TYPE, "stack_ptr"))) {
  652. aot_set_last_error("llvm build bit cast failed.");
  653. goto fail;
  654. }
  655. }
  656. else {
  657. if (!(func_ctx->last_alloca = LLVMBuildAlloca(comp_ctx->builder, INT8_TYPE,
  658. "stack_ptr"))) {
  659. aot_set_last_error("llvm build alloca failed.");
  660. goto fail;
  661. }
  662. }
  663. if (!(int8_ptr_type = LLVMPointerType(INT8_PTR_TYPE, 0))) {
  664. aot_set_last_error("llvm add pointer type failed.");
  665. goto fail;
  666. }
  667. /* Create exception blocks */
  668. if (!create_exception_blocks(func_ctx))
  669. goto fail;
  670. /* Create base addr, end addr, data size of mem, heap */
  671. if (!create_memory_info(comp_ctx, func_ctx, int8_ptr_type, func_index))
  672. goto fail;
  673. /* Load table base */
  674. if (!create_table_base(comp_ctx, func_ctx))
  675. goto fail;
  676. /* Load current exception */
  677. if (!create_cur_exception(comp_ctx, func_ctx))
  678. goto fail;
  679. /* Load function type indexes */
  680. if (!create_func_type_indexes(comp_ctx, func_ctx))
  681. goto fail;
  682. return func_ctx;
  683. fail:
  684. if (func_ctx->mem_info)
  685. wasm_runtime_free(func_ctx->mem_info);
  686. if (func_ctx->exception_blocks)
  687. wasm_runtime_free(func_ctx->exception_blocks);
  688. aot_block_stack_destroy(&func_ctx->block_stack);
  689. wasm_runtime_free(func_ctx);
  690. return NULL;
  691. }
  692. static void
  693. aot_destroy_func_contexts(AOTFuncContext **func_ctxes, uint32 count)
  694. {
  695. uint32 i;
  696. for (i = 0; i < count; i++)
  697. if (func_ctxes[i]) {
  698. if (func_ctxes[i]->mem_info)
  699. wasm_runtime_free(func_ctxes[i]->mem_info);
  700. if (func_ctxes[i]->exception_blocks)
  701. wasm_runtime_free(func_ctxes[i]->exception_blocks);
  702. aot_block_stack_destroy(&func_ctxes[i]->block_stack);
  703. aot_checked_addr_list_destroy(func_ctxes[i]);
  704. wasm_runtime_free(func_ctxes[i]);
  705. }
  706. wasm_runtime_free(func_ctxes);
  707. }
  708. /**
  709. * Create function compiler contexts
  710. */
  711. static AOTFuncContext **
  712. aot_create_func_contexts(AOTCompData *comp_data, AOTCompContext *comp_ctx)
  713. {
  714. AOTFuncContext **func_ctxes;
  715. uint64 size;
  716. uint32 i;
  717. /* Allocate memory */
  718. size = sizeof(AOTFuncContext*) * (uint64)comp_data->func_count;
  719. if (size >= UINT32_MAX
  720. || !(func_ctxes = wasm_runtime_malloc((uint32)size))) {
  721. aot_set_last_error("allocate memory failed.");
  722. return NULL;
  723. }
  724. memset(func_ctxes, 0, size);
  725. /* Create each function context */
  726. for (i = 0; i < comp_data->func_count; i++) {
  727. AOTFunc *func = comp_data->funcs[i];
  728. if (!(func_ctxes[i] = aot_create_func_context(comp_data, comp_ctx,
  729. func, i))) {
  730. aot_destroy_func_contexts(func_ctxes, comp_data->func_count);
  731. return NULL;
  732. }
  733. }
  734. return func_ctxes;
  735. }
  736. static bool
  737. aot_set_llvm_basic_types(AOTLLVMTypes *basic_types, LLVMContextRef context)
  738. {
  739. basic_types->int1_type = LLVMInt1TypeInContext(context);
  740. basic_types->int8_type = LLVMInt8TypeInContext(context);
  741. basic_types->int16_type = LLVMInt16TypeInContext(context);
  742. basic_types->int32_type = LLVMInt32TypeInContext(context);
  743. basic_types->int64_type = LLVMInt64TypeInContext(context);
  744. basic_types->float32_type = LLVMFloatTypeInContext(context);
  745. basic_types->float64_type = LLVMDoubleTypeInContext(context);
  746. basic_types->void_type = LLVMVoidTypeInContext(context);
  747. basic_types->meta_data_type = LLVMMetadataTypeInContext(context);
  748. basic_types->int8_ptr_type = LLVMPointerType(basic_types->int8_type, 0);
  749. basic_types->int16_ptr_type = LLVMPointerType(basic_types->int16_type, 0);
  750. basic_types->int32_ptr_type = LLVMPointerType(basic_types->int32_type, 0);
  751. basic_types->int64_ptr_type = LLVMPointerType(basic_types->int64_type, 0);
  752. basic_types->float32_ptr_type = LLVMPointerType(basic_types->float32_type, 0);
  753. basic_types->float64_ptr_type = LLVMPointerType(basic_types->float64_type, 0);
  754. return (basic_types->int8_ptr_type
  755. && basic_types->int16_ptr_type
  756. && basic_types->int32_ptr_type
  757. && basic_types->int64_ptr_type
  758. && basic_types->float32_ptr_type
  759. && basic_types->float64_ptr_type
  760. && basic_types->meta_data_type) ? true : false;
  761. }
  762. static bool
  763. aot_create_llvm_consts(AOTLLVMConsts *consts, AOTCompContext *comp_ctx)
  764. {
  765. consts->i8_zero = I8_CONST(0);
  766. consts->i32_zero = I32_CONST(0);
  767. consts->i64_zero = I64_CONST(0);
  768. consts->f32_zero = F32_CONST(0);
  769. consts->f64_zero = F64_CONST(0);
  770. consts->i32_one = I32_CONST(1);
  771. consts->i32_two = I32_CONST(2);
  772. consts->i32_three = I32_CONST(3);
  773. consts->i32_four = I32_CONST(4);
  774. consts->i32_eight = I32_CONST(8);
  775. consts->i32_neg_one = I32_CONST((uint32)-1);
  776. consts->i64_neg_one = I64_CONST((uint64)-1);
  777. consts->i32_min = I32_CONST((uint32)INT32_MIN);
  778. consts->i64_min = I64_CONST((uint64)INT64_MIN);
  779. consts->i32_31 = I32_CONST(31);
  780. consts->i32_32 = I32_CONST(32);
  781. consts->i64_63 = I64_CONST(63);
  782. consts->i64_64 = I64_CONST(64);
  783. return (consts->i8_zero
  784. && consts->i32_zero
  785. && consts->i64_zero
  786. && consts->f32_zero
  787. && consts->f64_zero
  788. && consts->i32_one
  789. && consts->i32_two
  790. && consts->i32_three
  791. && consts->i32_four
  792. && consts->i32_eight
  793. && consts->i32_neg_one
  794. && consts->i64_neg_one
  795. && consts->i32_min
  796. && consts->i64_min
  797. && consts->i32_31
  798. && consts->i32_32
  799. && consts->i64_63
  800. && consts->i64_64) ? true : false;
  801. }
  802. typedef struct ArchItem {
  803. char *arch;
  804. bool support_eb;
  805. } ArchItem;
  806. static ArchItem valid_archs[] = {
  807. { "x86_64", false },
  808. { "i386", false },
  809. { "xtensa", false},
  810. { "mips", true },
  811. { "aarch64v8", false },
  812. { "aarch64v8.1", false },
  813. { "aarch64v8.2", false },
  814. { "aarch64v8.3", false },
  815. { "aarch64v8.4", false },
  816. { "aarch64v8.5", false },
  817. { "aarch64_bev8", false }, /* big endian */
  818. { "aarch64_bev8.1", false },
  819. { "aarch64_bev8.2", false },
  820. { "aarch64_bev8.3", false },
  821. { "aarch64_bev8.4", false },
  822. { "aarch64_bev8.5", false },
  823. { "armv4", true },
  824. { "armv4t", true },
  825. { "armv5t", true },
  826. { "armv5te", true },
  827. { "armv5tej", true },
  828. { "armv6", true },
  829. { "armv6kz", true },
  830. { "armv6t2", true },
  831. { "armv6k", true },
  832. { "armv7", true },
  833. { "armv6m", true },
  834. { "armv6sm", true },
  835. { "armv7em", true },
  836. { "armv8a", true },
  837. { "armv8r", true },
  838. { "armv8m.base", true },
  839. { "armv8m.main", true },
  840. { "armv8.1m.main", true },
  841. { "thumbv4", true },
  842. { "thumbv4t", true },
  843. { "thumbv5t", true },
  844. { "thumbv5te", true },
  845. { "thumbv5tej", true },
  846. { "thumbv6", true },
  847. { "thumbv6kz", true },
  848. { "thumbv6t2", true },
  849. { "thumbv6k", true },
  850. { "thumbv7", true },
  851. { "thumbv6m", true },
  852. { "thumbv6sm", true },
  853. { "thumbv7em", true },
  854. { "thumbv8a", true },
  855. { "thumbv8r", true },
  856. { "thumbv8m.base", true },
  857. { "thumbv8m.main", true },
  858. { "thumbv8.1m.main", true }
  859. };
  860. static const char *valid_abis[] = {
  861. "gnu",
  862. "eabi",
  863. "gnueabihf"
  864. };
  865. static void
  866. print_supported_targets()
  867. {
  868. uint32 i;
  869. os_printf("Supported targets:\n");
  870. for (i = 0; i < sizeof(valid_archs) / sizeof(ArchItem); i++) {
  871. os_printf("%s ", valid_archs[i].arch);
  872. if (valid_archs[i].support_eb)
  873. os_printf("%seb ", valid_archs[i].arch);
  874. }
  875. os_printf("\n");
  876. }
  877. static void
  878. print_supported_abis()
  879. {
  880. uint32 i;
  881. os_printf("Supported ABI: ");
  882. for (i = 0; i < sizeof(valid_abis) / sizeof(const char *); i++)
  883. os_printf("%s ", valid_abis[i]);
  884. os_printf("\n");
  885. }
  886. static bool
  887. check_target_arch(const char *target_arch)
  888. {
  889. uint32 i;
  890. char *arch;
  891. bool support_eb;
  892. for (i = 0; i < sizeof(valid_archs) / sizeof(ArchItem); i++) {
  893. arch = valid_archs[i].arch;
  894. support_eb = valid_archs[i].support_eb;
  895. if (!strncmp(target_arch, arch, strlen(arch))
  896. && ((support_eb && (!strcmp(target_arch + strlen(arch), "eb")
  897. || !strcmp(target_arch + strlen(arch), "")))
  898. || (!support_eb && !strcmp(target_arch + strlen(arch), "")))) {
  899. return true;
  900. }
  901. }
  902. return false;
  903. }
  904. static bool
  905. check_target_abi(const char *target_abi)
  906. {
  907. uint32 i;
  908. for (i = 0; i < sizeof(valid_abis) / sizeof(char *); i++) {
  909. if (!strcmp(target_abi, valid_abis[i]))
  910. return true;
  911. }
  912. return false;
  913. }
  914. static void
  915. get_target_arch_from_triple(const char *triple, char *arch_buf, uint32 buf_size)
  916. {
  917. uint32 i = 0;
  918. while (*triple != '-' && *triple != '\0' && i < buf_size - 1)
  919. arch_buf[i++] = *triple++;
  920. /* Make sure buffer is long enough */
  921. bh_assert(*triple == '-' || *triple == '\0');
  922. }
  923. void LLVMAddPromoteMemoryToRegisterPass(LLVMPassManagerRef PM);
  924. AOTCompContext *
  925. aot_create_comp_context(AOTCompData *comp_data,
  926. aot_comp_option_t option)
  927. {
  928. AOTCompContext *comp_ctx, *ret = NULL;
  929. /*LLVMTypeRef elem_types[8];*/
  930. struct LLVMMCJITCompilerOptions jit_options;
  931. LLVMTargetRef target;
  932. char *triple = NULL, *triple_norm, *arch, *abi, *cpu, *features, buf[128];
  933. char *triple_norm_new = NULL, *cpu_new = NULL;
  934. char *err = NULL, *fp_round= "round.tonearest", *fp_exce = "fpexcept.strict";
  935. char triple_buf[32] = {0};
  936. uint32 opt_level, size_level;
  937. LLVMCodeModel code_model;
  938. /* Initialize LLVM environment */
  939. LLVMInitializeAllTargetInfos();
  940. LLVMInitializeAllTargets();
  941. LLVMInitializeAllTargetMCs();
  942. LLVMInitializeAllAsmPrinters();
  943. LLVMLinkInMCJIT();
  944. /* Allocate memory */
  945. if (!(comp_ctx = wasm_runtime_malloc(sizeof(AOTCompContext)))) {
  946. aot_set_last_error("allocate memory failed.");
  947. return NULL;
  948. }
  949. memset(comp_ctx, 0, sizeof(AOTCompContext));
  950. comp_ctx->comp_data = comp_data;
  951. /* Create LLVM context, module and builder */
  952. if (!(comp_ctx->context = LLVMContextCreate())) {
  953. aot_set_last_error("create LLVM context failed.");
  954. goto fail;
  955. }
  956. if (!(comp_ctx->builder = LLVMCreateBuilderInContext(comp_ctx->context))) {
  957. aot_set_last_error("create LLVM builder failed.");
  958. goto fail;
  959. }
  960. if (!(comp_ctx->module =
  961. LLVMModuleCreateWithNameInContext("WASM Module", comp_ctx->context))) {
  962. aot_set_last_error("create LLVM module failed.");
  963. goto fail;
  964. }
  965. if (option->enable_bulk_memory)
  966. comp_ctx->enable_bulk_memory = true;
  967. if (option->enable_thread_mgr)
  968. comp_ctx->enable_thread_mgr = true;
  969. if (option->is_jit_mode) {
  970. /* Create LLVM execution engine */
  971. LLVMInitializeMCJITCompilerOptions(&jit_options, sizeof(jit_options));
  972. jit_options.OptLevel = LLVMCodeGenLevelAggressive;
  973. jit_options.EnableFastISel = true;
  974. /*jit_options.CodeModel = LLVMCodeModelSmall;*/
  975. if (LLVMCreateMCJITCompilerForModule
  976. (&comp_ctx->exec_engine, comp_ctx->module,
  977. &jit_options, sizeof(jit_options), &err) != 0) {
  978. if (err) {
  979. LLVMDisposeMessage(err);
  980. err = NULL;
  981. }
  982. aot_set_last_error("create LLVM JIT compiler failed.");
  983. goto fail;
  984. }
  985. comp_ctx->is_jit_mode = true;
  986. comp_ctx->target_machine =
  987. LLVMGetExecutionEngineTargetMachine(comp_ctx->exec_engine);
  988. #ifndef OS_ENABLE_HW_BOUND_CHECK
  989. comp_ctx->enable_bound_check = true;
  990. #else
  991. comp_ctx->enable_bound_check = false;
  992. #endif
  993. }
  994. else {
  995. /* Create LLVM target machine */
  996. arch = option->target_arch;
  997. abi = option->target_abi;
  998. cpu = option->target_cpu;
  999. features = option->cpu_features;
  1000. opt_level = option->opt_level;
  1001. size_level = option->size_level;
  1002. if (arch) {
  1003. /* Add default sub-arch if not specified */
  1004. if (!strcmp(arch, "arm"))
  1005. arch = "armv4";
  1006. else if (!strcmp(arch, "armeb"))
  1007. arch = "armv4eb";
  1008. else if (!strcmp(arch, "thumb"))
  1009. arch = "thumbv4t";
  1010. else if (!strcmp(arch, "thumbeb"))
  1011. arch = "thumbv4teb";
  1012. else if (!strcmp(arch, "aarch64"))
  1013. arch = "aarch64v8";
  1014. else if (!strcmp(arch, "aarch64_be"))
  1015. arch = "aarch64_bev8";
  1016. }
  1017. /* Check target arch */
  1018. if (arch && !check_target_arch(arch)) {
  1019. if (!strcmp(arch, "help"))
  1020. print_supported_targets();
  1021. else
  1022. aot_set_last_error("Invalid target. "
  1023. "Use --target=help to list all supported targets");
  1024. goto fail;
  1025. }
  1026. /* Check target ABI */
  1027. if (abi && !check_target_abi(abi)) {
  1028. if (!strcmp(abi, "help"))
  1029. print_supported_abis();
  1030. else
  1031. aot_set_last_error("Invalid target ABI. "
  1032. "Use --target-abi=help to list all supported ABI");
  1033. goto fail;
  1034. }
  1035. if (arch) {
  1036. /* Construct target triple: <arch>-<vendor>-<sys>-<abi> */
  1037. const char *vendor_sys = "-pc-linux-";
  1038. if (!abi)
  1039. abi = "gnu";
  1040. bh_assert(strlen(arch) + strlen(vendor_sys) + strlen(abi) < sizeof(triple_buf));
  1041. memcpy(triple_buf, arch, strlen(arch));
  1042. memcpy(triple_buf + strlen(arch), vendor_sys, strlen(vendor_sys));
  1043. memcpy(triple_buf + strlen(arch) + strlen(vendor_sys), abi, strlen(abi));
  1044. triple = triple_buf;
  1045. }
  1046. if (!cpu && features) {
  1047. aot_set_last_error("cpu isn't specified for cpu features.");
  1048. goto fail;
  1049. }
  1050. if (!triple && !cpu) {
  1051. /* Get a triple for the host machine */
  1052. if (!(triple_norm = triple_norm_new = LLVMGetDefaultTargetTriple())) {
  1053. aot_set_last_error("llvm get default target triple failed.");
  1054. goto fail;
  1055. }
  1056. /* Get CPU name of the host machine */
  1057. if (!(cpu = cpu_new = LLVMGetHostCPUName())) {
  1058. aot_set_last_error("llvm get host cpu name failed.");
  1059. goto fail;
  1060. }
  1061. }
  1062. else if (triple) {
  1063. /* Normalize a target triple */
  1064. if (!(triple_norm = triple_norm_new = LLVMNormalizeTargetTriple(triple))) {
  1065. snprintf(buf, sizeof(buf),
  1066. "llvm normlalize target triple (%s) failed.", triple);
  1067. aot_set_last_error(buf);
  1068. goto fail;
  1069. }
  1070. if (!cpu)
  1071. cpu = "";
  1072. }
  1073. else { /* triple is NULL, cpu isn't NULL */
  1074. snprintf(buf, sizeof(buf),
  1075. "target isn't specified for cpu %s.", cpu);
  1076. aot_set_last_error(buf);
  1077. goto fail;
  1078. }
  1079. if (!features)
  1080. features = "";
  1081. /* Get target with triple, note that LLVMGetTargetFromTriple()
  1082. return 0 when success, but not true. */
  1083. if (LLVMGetTargetFromTriple(triple_norm, &target, &err) != 0) {
  1084. if (err) {
  1085. LLVMDisposeMessage(err);
  1086. err = NULL;
  1087. }
  1088. snprintf(buf, sizeof(buf),
  1089. "llvm get target from triple (%s) failed", triple_norm);
  1090. aot_set_last_error(buf);
  1091. goto fail;
  1092. }
  1093. /* Save target arch */
  1094. get_target_arch_from_triple(triple_norm, comp_ctx->target_arch,
  1095. sizeof(comp_ctx->target_arch));
  1096. if (option->bounds_checks == 1 || option->bounds_checks == 0) {
  1097. /* Set by user */
  1098. comp_ctx->enable_bound_check =
  1099. (option->bounds_checks == 1) ? true : false;
  1100. }
  1101. else {
  1102. /* Unset by user, use default value */
  1103. if (strstr(comp_ctx->target_arch, "64") && !option->is_sgx_platform) {
  1104. comp_ctx->enable_bound_check = false;
  1105. }
  1106. else {
  1107. comp_ctx->enable_bound_check = true;
  1108. }
  1109. }
  1110. os_printf("Create AoT compiler with:\n");
  1111. os_printf(" target: %s\n", comp_ctx->target_arch);
  1112. os_printf(" target cpu: %s\n", cpu);
  1113. os_printf(" cpu features: %s\n", features);
  1114. os_printf(" opt level: %d\n", opt_level);
  1115. os_printf(" size level: %d\n", size_level);
  1116. switch (option->output_format) {
  1117. case AOT_LLVMIR_UNOPT_FILE:
  1118. os_printf(" output format: unoptimized LLVM IR\n");
  1119. break;
  1120. case AOT_LLVMIR_OPT_FILE:
  1121. os_printf(" output format: optimized LLVM IR\n");
  1122. break;
  1123. case AOT_FORMAT_FILE:
  1124. os_printf(" output format: AoT file\n");
  1125. break;
  1126. case AOT_OBJECT_FILE:
  1127. os_printf(" output format: native object file\n");
  1128. break;
  1129. }
  1130. if (!LLVMTargetHasTargetMachine(target)) {
  1131. snprintf(buf, sizeof(buf),
  1132. "no target machine for this target (%s).", triple_norm);
  1133. aot_set_last_error(buf);
  1134. goto fail;
  1135. }
  1136. if (!LLVMTargetHasAsmBackend(target)) {
  1137. snprintf(buf, sizeof(buf),
  1138. "no asm backend for this target (%s).", LLVMGetTargetName(target));
  1139. aot_set_last_error(buf);
  1140. goto fail;
  1141. }
  1142. /* Set code model */
  1143. if (size_level == 0)
  1144. code_model = LLVMCodeModelLarge;
  1145. else if (size_level == 1)
  1146. code_model = LLVMCodeModelMedium;
  1147. else if (size_level == 2)
  1148. code_model = LLVMCodeModelKernel;
  1149. else
  1150. code_model = LLVMCodeModelSmall;
  1151. /* Create the target machine */
  1152. if (!(comp_ctx->target_machine =
  1153. LLVMCreateTargetMachine(target, triple_norm, cpu, features,
  1154. opt_level, LLVMRelocStatic,
  1155. code_model))) {
  1156. aot_set_last_error("create LLVM target machine failed.");
  1157. goto fail;
  1158. }
  1159. }
  1160. comp_ctx->optimize = true;
  1161. if (option->output_format == AOT_LLVMIR_UNOPT_FILE)
  1162. comp_ctx->optimize = false;
  1163. if (!(comp_ctx->pass_mgr = LLVMCreateFunctionPassManagerForModule
  1164. (comp_ctx->module))) {
  1165. aot_set_last_error("create LLVM pass manager failed.");
  1166. goto fail;
  1167. }
  1168. LLVMAddPromoteMemoryToRegisterPass(comp_ctx->pass_mgr);
  1169. LLVMAddInstructionCombiningPass(comp_ctx->pass_mgr);
  1170. LLVMAddCFGSimplificationPass(comp_ctx->pass_mgr);
  1171. LLVMAddJumpThreadingPass(comp_ctx->pass_mgr);
  1172. LLVMAddConstantPropagationPass(comp_ctx->pass_mgr);
  1173. /* Create metadata for llvm float experimental constrained intrinsics */
  1174. if (!(comp_ctx->fp_rounding_mode =
  1175. LLVMMDStringInContext(comp_ctx->context,
  1176. fp_round,
  1177. (uint32)strlen(fp_round)))
  1178. || !(comp_ctx->fp_exception_behavior =
  1179. LLVMMDStringInContext(comp_ctx->context,
  1180. fp_exce,
  1181. (uint32)strlen(fp_exce)))) {
  1182. aot_set_last_error("create float llvm metadata failed.");
  1183. goto fail;
  1184. }
  1185. if (!aot_set_llvm_basic_types(&comp_ctx->basic_types, comp_ctx->context)) {
  1186. aot_set_last_error("create LLVM basic types failed.");
  1187. goto fail;
  1188. }
  1189. if (!aot_create_llvm_consts(&comp_ctx->llvm_consts, comp_ctx)) {
  1190. aot_set_last_error("create LLVM const values failed.");
  1191. goto fail;
  1192. }
  1193. /* set exec_env data type to int8** */
  1194. if (!(comp_ctx->exec_env_type = LLVMPointerType(INT8_PTR_TYPE, 0))) {
  1195. aot_set_last_error("llvm get pointer type failed.");
  1196. goto fail;
  1197. }
  1198. /* set aot_inst data type to int8* */
  1199. comp_ctx->aot_inst_type = INT8_PTR_TYPE;
  1200. /* Create function context for each function */
  1201. comp_ctx->func_ctx_count = comp_data->func_count;
  1202. if (!(comp_ctx->func_ctxes = aot_create_func_contexts(comp_data, comp_ctx)))
  1203. goto fail;
  1204. ret = comp_ctx;
  1205. fail:
  1206. if (triple_norm_new)
  1207. LLVMDisposeMessage(triple_norm_new);
  1208. if (cpu_new)
  1209. LLVMDisposeMessage(cpu_new);
  1210. if (!ret)
  1211. aot_destroy_comp_context(comp_ctx);
  1212. return ret;
  1213. }
  1214. void
  1215. aot_destroy_comp_context(AOTCompContext *comp_ctx)
  1216. {
  1217. if (!comp_ctx)
  1218. return;
  1219. if (comp_ctx->pass_mgr)
  1220. LLVMDisposePassManager(comp_ctx->pass_mgr);
  1221. if (comp_ctx->target_machine && !comp_ctx->is_jit_mode)
  1222. LLVMDisposeTargetMachine(comp_ctx->target_machine);
  1223. if (comp_ctx->builder)
  1224. LLVMDisposeBuilder(comp_ctx->builder);
  1225. if (comp_ctx->exec_engine) {
  1226. LLVMDisposeExecutionEngine(comp_ctx->exec_engine);
  1227. /* The LLVM module is freed when disposing execution engine,
  1228. no need to dispose it again. */
  1229. }
  1230. else if (comp_ctx->module)
  1231. LLVMDisposeModule(comp_ctx->module);
  1232. if (comp_ctx->context)
  1233. LLVMContextDispose(comp_ctx->context);
  1234. if (comp_ctx->func_ctxes)
  1235. aot_destroy_func_contexts(comp_ctx->func_ctxes, comp_ctx->func_ctx_count);
  1236. wasm_runtime_free(comp_ctx);
  1237. }
  1238. void
  1239. aot_value_stack_push(AOTValueStack *stack, AOTValue *value)
  1240. {
  1241. if (!stack->value_list_head)
  1242. stack->value_list_head = stack->value_list_end = value;
  1243. else {
  1244. stack->value_list_end->next = value;
  1245. value->prev = stack->value_list_end;
  1246. stack->value_list_end = value;
  1247. }
  1248. }
  1249. AOTValue *
  1250. aot_value_stack_pop(AOTValueStack *stack)
  1251. {
  1252. AOTValue *value = stack->value_list_end;
  1253. bh_assert(stack->value_list_end);
  1254. if (stack->value_list_head == stack->value_list_end)
  1255. stack->value_list_head = stack->value_list_end = NULL;
  1256. else {
  1257. stack->value_list_end = stack->value_list_end->prev;
  1258. stack->value_list_end->next = NULL;
  1259. value->prev = NULL;
  1260. }
  1261. return value;
  1262. }
  1263. void
  1264. aot_value_stack_destroy(AOTValueStack *stack)
  1265. {
  1266. AOTValue *value = stack->value_list_head, *p;
  1267. while (value) {
  1268. p = value->next;
  1269. wasm_runtime_free(value);
  1270. value = p;
  1271. }
  1272. }
  1273. void
  1274. aot_block_stack_push(AOTBlockStack *stack, AOTBlock *block)
  1275. {
  1276. if (!stack->block_list_head)
  1277. stack->block_list_head = stack->block_list_end = block;
  1278. else {
  1279. stack->block_list_end->next = block;
  1280. block->prev = stack->block_list_end;
  1281. stack->block_list_end = block;
  1282. }
  1283. }
  1284. AOTBlock *
  1285. aot_block_stack_pop(AOTBlockStack *stack)
  1286. {
  1287. AOTBlock *block = stack->block_list_end;
  1288. bh_assert(stack->block_list_end);
  1289. if (stack->block_list_head == stack->block_list_end)
  1290. stack->block_list_head = stack->block_list_end = NULL;
  1291. else {
  1292. stack->block_list_end = stack->block_list_end->prev;
  1293. stack->block_list_end->next = NULL;
  1294. block->prev = NULL;
  1295. }
  1296. return block;
  1297. }
  1298. void
  1299. aot_block_stack_destroy(AOTBlockStack *stack)
  1300. {
  1301. AOTBlock *block = stack->block_list_head, *p;
  1302. while (block) {
  1303. p = block->next;
  1304. aot_value_stack_destroy(&block->value_stack);
  1305. wasm_runtime_free(block);
  1306. block = p;
  1307. }
  1308. }
  1309. void
  1310. aot_block_destroy(AOTBlock *block)
  1311. {
  1312. aot_value_stack_destroy(&block->value_stack);
  1313. if (block->param_types)
  1314. wasm_runtime_free(block->param_types);
  1315. if (block->param_phis)
  1316. wasm_runtime_free(block->param_phis);
  1317. if (block->else_param_phis)
  1318. wasm_runtime_free(block->else_param_phis);
  1319. if (block->result_types)
  1320. wasm_runtime_free(block->result_types);
  1321. if (block->result_phis)
  1322. wasm_runtime_free(block->result_phis);
  1323. wasm_runtime_free(block);
  1324. }
  1325. bool
  1326. aot_checked_addr_list_add(AOTFuncContext *func_ctx,
  1327. uint32 local_idx, uint32 offset, uint32 bytes)
  1328. {
  1329. AOTCheckedAddr *node = func_ctx->checked_addr_list;
  1330. if (!(node = wasm_runtime_malloc(sizeof(AOTCheckedAddr)))) {
  1331. aot_set_last_error("allocate memory failed.");
  1332. return false;
  1333. }
  1334. node->local_idx = local_idx;
  1335. node->offset = offset;
  1336. node->bytes = bytes;
  1337. node->next = func_ctx->checked_addr_list;
  1338. func_ctx->checked_addr_list = node;
  1339. return true;
  1340. }
  1341. void
  1342. aot_checked_addr_list_del(AOTFuncContext *func_ctx, uint32 local_idx)
  1343. {
  1344. AOTCheckedAddr *node = func_ctx->checked_addr_list;
  1345. AOTCheckedAddr *node_prev = NULL, *node_next;
  1346. while (node) {
  1347. node_next = node->next;
  1348. if (node->local_idx == local_idx) {
  1349. if (!node_prev)
  1350. func_ctx->checked_addr_list = node_next;
  1351. else
  1352. node_prev->next = node_next;
  1353. wasm_runtime_free(node);
  1354. }
  1355. else {
  1356. node_prev = node;
  1357. }
  1358. node = node_next;
  1359. }
  1360. }
  1361. bool
  1362. aot_checked_addr_list_find(AOTFuncContext *func_ctx,
  1363. uint32 local_idx, uint32 offset, uint32 bytes)
  1364. {
  1365. AOTCheckedAddr *node = func_ctx->checked_addr_list;
  1366. while (node) {
  1367. if (node->local_idx == local_idx
  1368. && node->offset == offset
  1369. && node->bytes >= bytes) {
  1370. return true;
  1371. }
  1372. node = node->next;
  1373. }
  1374. return false;
  1375. }
  1376. void
  1377. aot_checked_addr_list_destroy(AOTFuncContext *func_ctx)
  1378. {
  1379. AOTCheckedAddr *node = func_ctx->checked_addr_list, *node_next;
  1380. while (node) {
  1381. node_next = node->next;
  1382. wasm_runtime_free(node);
  1383. node = node_next;
  1384. }
  1385. func_ctx->checked_addr_list = NULL;
  1386. }