aot_llvm.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961
  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. case VALUE_TYPE_FUNCREF:
  15. case VALUE_TYPE_EXTERNREF:
  16. return llvm_types->int32_type;
  17. case VALUE_TYPE_I64:
  18. return llvm_types->int64_type;
  19. case VALUE_TYPE_F32:
  20. return llvm_types->float32_type;
  21. case VALUE_TYPE_F64:
  22. return llvm_types->float64_type;
  23. case VALUE_TYPE_V128:
  24. return llvm_types->i64x2_vec_type;
  25. case VALUE_TYPE_VOID:
  26. return llvm_types->void_type;
  27. default:
  28. break;
  29. }
  30. return NULL;
  31. }
  32. /**
  33. * Add LLVM function
  34. */
  35. static LLVMValueRef
  36. aot_add_llvm_func(AOTCompContext *comp_ctx, AOTFuncType *aot_func_type,
  37. uint32 func_index)
  38. {
  39. LLVMValueRef func = NULL;
  40. LLVMTypeRef *param_types, ret_type, func_type;
  41. LLVMValueRef local_value;
  42. char func_name[32];
  43. uint64 size;
  44. uint32 i, j = 0, param_count = (uint64)aot_func_type->param_count;
  45. /* exec env as first parameter */
  46. param_count++;
  47. /* Extra wasm function results(except the first one)'s address are
  48. * appended to aot function parameters. */
  49. if (aot_func_type->result_count > 1)
  50. param_count += aot_func_type->result_count - 1;
  51. /* Initialize parameter types of the LLVM function */
  52. size = sizeof(LLVMTypeRef) * ((uint64)param_count);
  53. if (size >= UINT32_MAX
  54. || !(param_types = wasm_runtime_malloc((uint32)size))) {
  55. aot_set_last_error("allocate memory failed.");
  56. return NULL;
  57. }
  58. /* exec env as first parameter */
  59. param_types[j++] = comp_ctx->exec_env_type;
  60. for (i = 0; i < aot_func_type->param_count; i++)
  61. param_types[j++] = TO_LLVM_TYPE(aot_func_type->types[i]);
  62. /* Extra results' address */
  63. for (i = 1; i < aot_func_type->result_count; i++, j++) {
  64. param_types[j] =
  65. TO_LLVM_TYPE(aot_func_type->types[aot_func_type->param_count + i]);
  66. if (!(param_types[j] = LLVMPointerType(param_types[j], 0))) {
  67. aot_set_last_error("llvm get pointer type failed.");
  68. goto fail;
  69. }
  70. }
  71. /* Resolve return type of the LLVM function */
  72. if (aot_func_type->result_count)
  73. ret_type = TO_LLVM_TYPE(aot_func_type->types[aot_func_type->param_count]);
  74. else
  75. ret_type = VOID_TYPE;
  76. /* Resolve function prototype */
  77. if (!(func_type = LLVMFunctionType(ret_type, param_types,
  78. param_count, false))) {
  79. aot_set_last_error("create LLVM function type failed.");
  80. goto fail;
  81. }
  82. /* Add LLVM function */
  83. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, func_index);
  84. if (!(func = LLVMAddFunction(comp_ctx->module, func_name, func_type))) {
  85. aot_set_last_error("add LLVM function failed.");
  86. goto fail;
  87. }
  88. j = 0;
  89. local_value = LLVMGetParam(func, j++);
  90. LLVMSetValueName(local_value, "exec_env");
  91. /* Set parameter names */
  92. for (i = 0; i < aot_func_type->param_count; i++) {
  93. local_value = LLVMGetParam(func, j++);
  94. LLVMSetValueName(local_value, "");
  95. }
  96. fail:
  97. wasm_runtime_free(param_types);
  98. return func;
  99. }
  100. static void
  101. free_block_memory(AOTBlock *block)
  102. {
  103. if (block->param_types)
  104. wasm_runtime_free(block->param_types);
  105. if (block->result_types)
  106. wasm_runtime_free(block->result_types);
  107. wasm_runtime_free(block);
  108. }
  109. /**
  110. * Create first AOTBlock, or function block for the function
  111. */
  112. static AOTBlock *
  113. aot_create_func_block(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  114. AOTFunc *func, AOTFuncType *aot_func_type)
  115. {
  116. AOTBlock *aot_block;
  117. uint32 param_count = aot_func_type->param_count,
  118. result_count = aot_func_type->result_count;
  119. /* Allocate memory */
  120. if (!(aot_block = wasm_runtime_malloc(sizeof(AOTBlock)))) {
  121. aot_set_last_error("allocate memory failed.");
  122. return NULL;
  123. }
  124. memset(aot_block, 0, sizeof(AOTBlock));
  125. if (param_count
  126. && !(aot_block->param_types = wasm_runtime_malloc(param_count))) {
  127. aot_set_last_error("allocate memory failed.");
  128. goto fail;
  129. }
  130. if (result_count) {
  131. if (!(aot_block->result_types = wasm_runtime_malloc(result_count))) {
  132. aot_set_last_error("allocate memory failed.");
  133. goto fail;
  134. }
  135. }
  136. /* Set block data */
  137. aot_block->label_type = LABEL_TYPE_FUNCTION;
  138. aot_block->param_count = param_count;
  139. memcpy(aot_block->param_types, aot_func_type->types, param_count);
  140. aot_block->result_count = result_count;
  141. memcpy(aot_block->result_types, aot_func_type->types + param_count, result_count);
  142. aot_block->wasm_code_end = func->code + func->code_size;
  143. /* Add function entry block */
  144. if (!(aot_block->llvm_entry_block =
  145. LLVMAppendBasicBlockInContext(comp_ctx->context, func_ctx->func,
  146. "func_begin"))) {
  147. aot_set_last_error("add LLVM basic block failed.");
  148. goto fail;
  149. }
  150. return aot_block;
  151. fail:
  152. free_block_memory(aot_block);
  153. return NULL;
  154. }
  155. static bool
  156. create_memory_info(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  157. LLVMTypeRef int8_ptr_type, uint32 func_index)
  158. {
  159. LLVMValueRef offset, mem_info_base;
  160. uint32 memory_count;
  161. WASMModule *module = comp_ctx->comp_data->wasm_module;
  162. WASMFunction *func = module->functions[func_index];
  163. LLVMTypeRef bound_check_type;
  164. bool mem_space_unchanged = (!func->has_op_memory_grow && !func->has_op_func_call)
  165. || (!module->possible_memory_grow);
  166. #if WASM_ENABLE_SHARED_MEMORY != 0
  167. bool is_shared_memory;
  168. #endif
  169. func_ctx->mem_space_unchanged = mem_space_unchanged;
  170. memory_count = module->memory_count + module->import_memory_count;
  171. /* If the module dosen't have memory, reserve
  172. one mem_info space with empty content */
  173. if (memory_count == 0)
  174. memory_count = 1;
  175. if (!(func_ctx->mem_info =
  176. wasm_runtime_malloc(sizeof(AOTMemInfo) * memory_count))) {
  177. return false;
  178. }
  179. memset(func_ctx->mem_info, 0, sizeof(AOTMemInfo));
  180. /* Currently we only create memory info for memory 0 */
  181. /* Load memory base address */
  182. #if WASM_ENABLE_SHARED_MEMORY != 0
  183. is_shared_memory = comp_ctx->comp_data->memories[0].memory_flags & 0x02
  184. ? true : false;
  185. if (is_shared_memory) {
  186. LLVMValueRef shared_mem_addr;
  187. offset = I32_CONST(offsetof(AOTModuleInstance, memories));
  188. if (!offset) {
  189. aot_set_last_error("create llvm const failed.");
  190. return false;
  191. }
  192. /* aot_inst->memories */
  193. if (!(shared_mem_addr =
  194. LLVMBuildInBoundsGEP(comp_ctx->builder, func_ctx->aot_inst,
  195. &offset, 1, "shared_mem_addr_offset"))) {
  196. aot_set_last_error("llvm build in bounds gep failed");
  197. return false;
  198. }
  199. if (!(shared_mem_addr =
  200. LLVMBuildBitCast(comp_ctx->builder,
  201. shared_mem_addr, int8_ptr_type,
  202. "shared_mem_addr_ptr"))) {
  203. aot_set_last_error("llvm build bit cast failed");
  204. return false;
  205. }
  206. /* aot_inst->memories[0] */
  207. if (!(shared_mem_addr =
  208. LLVMBuildLoad(comp_ctx->builder,
  209. shared_mem_addr, "shared_mem_addr"))) {
  210. aot_set_last_error("llvm build load failed");
  211. return false;
  212. }
  213. if (!(shared_mem_addr =
  214. LLVMBuildBitCast(comp_ctx->builder,
  215. shared_mem_addr, int8_ptr_type,
  216. "shared_mem_addr_ptr"))) {
  217. aot_set_last_error("llvm build bit cast failed");
  218. return false;
  219. }
  220. if (!(shared_mem_addr =
  221. LLVMBuildLoad(comp_ctx->builder,
  222. shared_mem_addr, "shared_mem_addr"))) {
  223. aot_set_last_error("llvm build load failed");
  224. return false;
  225. }
  226. offset = I32_CONST(offsetof(AOTMemoryInstance, memory_data.ptr));
  227. if (!(func_ctx->mem_info[0].mem_base_addr =
  228. LLVMBuildInBoundsGEP(comp_ctx->builder, shared_mem_addr,
  229. &offset, 1, "mem_base_addr_offset"))) {
  230. aot_set_last_error("llvm build in bounds gep failed");
  231. return false;
  232. }
  233. offset = I32_CONST(offsetof(AOTMemoryInstance, cur_page_count));
  234. if (!(func_ctx->mem_info[0].mem_cur_page_count_addr =
  235. LLVMBuildInBoundsGEP(comp_ctx->builder, shared_mem_addr,
  236. &offset, 1, "mem_cur_page_offset"))) {
  237. aot_set_last_error("llvm build in bounds gep failed");
  238. return false;
  239. }
  240. offset = I32_CONST(offsetof(AOTMemoryInstance, memory_data_size));
  241. if (!(func_ctx->mem_info[0].mem_data_size_addr =
  242. LLVMBuildInBoundsGEP(comp_ctx->builder, shared_mem_addr,
  243. &offset, 1, "mem_data_size_offset"))) {
  244. aot_set_last_error("llvm build in bounds gep failed");
  245. return false;
  246. }
  247. }
  248. else
  249. #endif
  250. {
  251. offset = I32_CONST(offsetof(AOTModuleInstance, global_table_data)
  252. + offsetof(AOTMemoryInstance, memory_data.ptr));
  253. if (!(func_ctx->mem_info[0].mem_base_addr =
  254. LLVMBuildInBoundsGEP(comp_ctx->builder, func_ctx->aot_inst,
  255. &offset, 1, "mem_base_addr_offset"))) {
  256. aot_set_last_error("llvm build in bounds gep failed");
  257. return false;
  258. }
  259. offset = I32_CONST(offsetof(AOTModuleInstance, global_table_data)
  260. + offsetof(AOTMemoryInstance, cur_page_count));
  261. if (!(func_ctx->mem_info[0].mem_cur_page_count_addr =
  262. LLVMBuildInBoundsGEP(comp_ctx->builder, func_ctx->aot_inst,
  263. &offset, 1, "mem_cur_page_offset"))) {
  264. aot_set_last_error("llvm build in bounds gep failed");
  265. return false;
  266. }
  267. offset = I32_CONST(offsetof(AOTModuleInstance, global_table_data)
  268. + offsetof(AOTMemoryInstance, memory_data_size));
  269. if (!(func_ctx->mem_info[0].mem_data_size_addr =
  270. LLVMBuildInBoundsGEP(comp_ctx->builder, func_ctx->aot_inst,
  271. &offset, 1, "mem_data_size_offset"))) {
  272. aot_set_last_error("llvm build in bounds gep failed");
  273. return false;
  274. }
  275. }
  276. /* Store mem info base address before cast */
  277. mem_info_base = func_ctx->mem_info[0].mem_base_addr;
  278. if (!(func_ctx->mem_info[0].mem_base_addr =
  279. LLVMBuildBitCast(comp_ctx->builder,
  280. func_ctx->mem_info[0].mem_base_addr,
  281. int8_ptr_type, "mem_base_addr_ptr"))) {
  282. aot_set_last_error("llvm build bit cast failed");
  283. return false;
  284. }
  285. if (!(func_ctx->mem_info[0].mem_cur_page_count_addr =
  286. LLVMBuildBitCast(comp_ctx->builder,
  287. func_ctx->mem_info[0].mem_cur_page_count_addr,
  288. INT32_PTR_TYPE, "mem_cur_page_ptr"))) {
  289. aot_set_last_error("llvm build bit cast failed");
  290. return false;
  291. }
  292. if (!(func_ctx->mem_info[0].mem_data_size_addr =
  293. LLVMBuildBitCast(comp_ctx->builder,
  294. func_ctx->mem_info[0].mem_data_size_addr,
  295. INT32_PTR_TYPE, "mem_data_size_ptr"))) {
  296. aot_set_last_error("llvm build bit cast failed");
  297. return false;
  298. }
  299. if (mem_space_unchanged) {
  300. if (!(func_ctx->mem_info[0].mem_base_addr =
  301. LLVMBuildLoad(comp_ctx->builder,
  302. func_ctx->mem_info[0].mem_base_addr,
  303. "mem_base_addr"))) {
  304. aot_set_last_error("llvm build load failed");
  305. return false;
  306. }
  307. if (!(func_ctx->mem_info[0].mem_cur_page_count_addr =
  308. LLVMBuildLoad(comp_ctx->builder,
  309. func_ctx->mem_info[0].mem_cur_page_count_addr,
  310. "mem_cur_page_count"))) {
  311. aot_set_last_error("llvm build load failed");
  312. return false;
  313. }
  314. if (!(func_ctx->mem_info[0].mem_data_size_addr =
  315. LLVMBuildLoad(comp_ctx->builder,
  316. func_ctx->mem_info[0].mem_data_size_addr,
  317. "mem_data_size"))) {
  318. aot_set_last_error("llvm build load failed");
  319. return false;
  320. }
  321. }
  322. #if WASM_ENABLE_SHARED_MEMORY != 0
  323. else if (is_shared_memory) {
  324. /* The base address for shared memory will never changed,
  325. we can load the value here */
  326. if (!(func_ctx->mem_info[0].mem_base_addr =
  327. LLVMBuildLoad(comp_ctx->builder,
  328. func_ctx->mem_info[0].mem_base_addr,
  329. "mem_base_addr"))) {
  330. aot_set_last_error("llvm build load failed");
  331. return false;
  332. }
  333. }
  334. #endif
  335. bound_check_type = (comp_ctx->pointer_size == sizeof(uint64))
  336. ? INT64_PTR_TYPE : INT32_PTR_TYPE;
  337. /* Load memory bound check constants */
  338. offset = I32_CONST(offsetof(AOTMemoryInstance, mem_bound_check_1byte)
  339. - offsetof(AOTMemoryInstance, memory_data.ptr));
  340. if (!(func_ctx->mem_info[0].mem_bound_check_1byte =
  341. LLVMBuildInBoundsGEP(comp_ctx->builder, mem_info_base,
  342. &offset, 1, "bound_check_1byte_offset"))) {
  343. aot_set_last_error("llvm build in bounds gep failed");
  344. return false;
  345. }
  346. if (!(func_ctx->mem_info[0].mem_bound_check_1byte =
  347. LLVMBuildBitCast(comp_ctx->builder,
  348. func_ctx->mem_info[0].mem_bound_check_1byte,
  349. bound_check_type, "bound_check_1byte_ptr"))) {
  350. aot_set_last_error("llvm build bit cast failed");
  351. return false;
  352. }
  353. if (mem_space_unchanged) {
  354. if (!(func_ctx->mem_info[0].mem_bound_check_1byte =
  355. LLVMBuildLoad(comp_ctx->builder,
  356. func_ctx->mem_info[0].mem_bound_check_1byte,
  357. "bound_check_1byte"))) {
  358. aot_set_last_error("llvm build load failed");
  359. return false;
  360. }
  361. }
  362. offset = I32_CONST(offsetof(AOTMemoryInstance, mem_bound_check_2bytes)
  363. - offsetof(AOTMemoryInstance, memory_data.ptr));
  364. if (!(func_ctx->mem_info[0].mem_bound_check_2bytes =
  365. LLVMBuildInBoundsGEP(comp_ctx->builder, mem_info_base,
  366. &offset, 1, "bound_check_2bytes_offset"))) {
  367. aot_set_last_error("llvm build in bounds gep failed");
  368. return false;
  369. }
  370. if (!(func_ctx->mem_info[0].mem_bound_check_2bytes =
  371. LLVMBuildBitCast(comp_ctx->builder,
  372. func_ctx->mem_info[0].mem_bound_check_2bytes,
  373. bound_check_type, "bound_check_2bytes_ptr"))) {
  374. aot_set_last_error("llvm build bit cast failed");
  375. return false;
  376. }
  377. if (mem_space_unchanged) {
  378. if (!(func_ctx->mem_info[0].mem_bound_check_2bytes =
  379. LLVMBuildLoad(comp_ctx->builder,
  380. func_ctx->mem_info[0].mem_bound_check_2bytes,
  381. "bound_check_2bytes"))) {
  382. aot_set_last_error("llvm build load failed");
  383. return false;
  384. }
  385. }
  386. offset = I32_CONST(offsetof(AOTMemoryInstance, mem_bound_check_4bytes)
  387. - offsetof(AOTMemoryInstance, memory_data.ptr));
  388. if (!(func_ctx->mem_info[0].mem_bound_check_4bytes =
  389. LLVMBuildInBoundsGEP(comp_ctx->builder, mem_info_base,
  390. &offset, 1, "bound_check_4bytes_offset"))) {
  391. aot_set_last_error("llvm build in bounds gep failed");
  392. return false;
  393. }
  394. if (!(func_ctx->mem_info[0].mem_bound_check_4bytes =
  395. LLVMBuildBitCast(comp_ctx->builder,
  396. func_ctx->mem_info[0].mem_bound_check_4bytes,
  397. bound_check_type, "bound_check_4bytes_ptr"))) {
  398. aot_set_last_error("llvm build bit cast failed");
  399. return false;
  400. }
  401. if (mem_space_unchanged) {
  402. if (!(func_ctx->mem_info[0].mem_bound_check_4bytes =
  403. LLVMBuildLoad(comp_ctx->builder,
  404. func_ctx->mem_info[0].mem_bound_check_4bytes,
  405. "bound_check_4bytes"))) {
  406. aot_set_last_error("llvm build load failed");
  407. return false;
  408. }
  409. }
  410. offset = I32_CONST(offsetof(AOTMemoryInstance, mem_bound_check_8bytes)
  411. - offsetof(AOTMemoryInstance, memory_data.ptr));
  412. if (!(func_ctx->mem_info[0].mem_bound_check_8bytes =
  413. LLVMBuildInBoundsGEP(comp_ctx->builder, mem_info_base,
  414. &offset, 1, "bound_check_8bytes_offset"))) {
  415. aot_set_last_error("llvm build in bounds gep failed");
  416. return false;
  417. }
  418. if (!(func_ctx->mem_info[0].mem_bound_check_8bytes =
  419. LLVMBuildBitCast(comp_ctx->builder,
  420. func_ctx->mem_info[0].mem_bound_check_8bytes,
  421. bound_check_type, "bound_check_8bytes_ptr"))) {
  422. aot_set_last_error("llvm build bit cast failed");
  423. return false;
  424. }
  425. if (mem_space_unchanged) {
  426. if (!(func_ctx->mem_info[0].mem_bound_check_8bytes =
  427. LLVMBuildLoad(comp_ctx->builder,
  428. func_ctx->mem_info[0].mem_bound_check_8bytes,
  429. "bound_check_8bytes"))) {
  430. aot_set_last_error("llvm build load failed");
  431. return false;
  432. }
  433. }
  434. offset = I32_CONST(offsetof(AOTMemoryInstance, mem_bound_check_16bytes)
  435. - offsetof(AOTMemoryInstance, memory_data.ptr));
  436. if (!(func_ctx->mem_info[0].mem_bound_check_16bytes =
  437. LLVMBuildInBoundsGEP(comp_ctx->builder, mem_info_base,
  438. &offset, 1, "bound_check_16bytes_offset"))) {
  439. aot_set_last_error("llvm build in bounds gep failed");
  440. return false;
  441. }
  442. if (!(func_ctx->mem_info[0].mem_bound_check_16bytes =
  443. LLVMBuildBitCast(comp_ctx->builder,
  444. func_ctx->mem_info[0].mem_bound_check_16bytes,
  445. bound_check_type, "bound_check_16bytes_ptr"))) {
  446. aot_set_last_error("llvm build bit cast failed");
  447. return false;
  448. }
  449. if (mem_space_unchanged) {
  450. if (!(func_ctx->mem_info[0].mem_bound_check_16bytes =
  451. LLVMBuildLoad(comp_ctx->builder,
  452. func_ctx->mem_info[0].mem_bound_check_16bytes,
  453. "bound_check_16bytes"))) {
  454. aot_set_last_error("llvm build load failed");
  455. return false;
  456. }
  457. }
  458. return true;
  459. }
  460. static bool
  461. create_cur_exception(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  462. {
  463. LLVMValueRef offset;
  464. offset = I32_CONST(offsetof(AOTModuleInstance, cur_exception));
  465. func_ctx->cur_exception = LLVMBuildInBoundsGEP(comp_ctx->builder,
  466. func_ctx->aot_inst,
  467. &offset, 1,
  468. "cur_execption");
  469. if (!func_ctx->cur_exception) {
  470. aot_set_last_error("llvm build in bounds gep failed.");
  471. return false;
  472. }
  473. return true;
  474. }
  475. static bool
  476. create_func_type_indexes(AOTCompContext *comp_ctx,
  477. AOTFuncContext *func_ctx)
  478. {
  479. LLVMValueRef offset, func_type_indexes_ptr;
  480. LLVMTypeRef int32_ptr_type;
  481. offset = I32_CONST(offsetof(AOTModuleInstance, func_type_indexes.ptr));
  482. func_type_indexes_ptr = LLVMBuildInBoundsGEP(comp_ctx->builder,
  483. func_ctx->aot_inst,
  484. &offset, 1,
  485. "func_type_indexes_ptr");
  486. if (!func_type_indexes_ptr) {
  487. aot_set_last_error("llvm build add failed.");
  488. return false;
  489. }
  490. if (!(int32_ptr_type = LLVMPointerType(INT32_PTR_TYPE, 0))) {
  491. aot_set_last_error("llvm get pointer type failed.");
  492. return false;
  493. }
  494. func_ctx->func_type_indexes = LLVMBuildBitCast(comp_ctx->builder,
  495. func_type_indexes_ptr,
  496. int32_ptr_type,
  497. "func_type_indexes_tmp");
  498. if (!func_ctx->func_type_indexes) {
  499. aot_set_last_error("llvm build bit cast failed.");
  500. return false;
  501. }
  502. func_ctx->func_type_indexes = LLVMBuildLoad(comp_ctx->builder,
  503. func_ctx->func_type_indexes,
  504. "func_type_indexes");
  505. if (!func_ctx->func_type_indexes) {
  506. aot_set_last_error("llvm build load failed.");
  507. return false;
  508. }
  509. return true;
  510. }
  511. static bool
  512. create_func_ptrs(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  513. {
  514. LLVMValueRef offset;
  515. offset = I32_CONST(offsetof(AOTModuleInstance, func_ptrs));
  516. func_ctx->func_ptrs = LLVMBuildInBoundsGEP(comp_ctx->builder,
  517. func_ctx->aot_inst,
  518. &offset, 1, "func_ptrs_offset");
  519. if (!func_ctx->func_ptrs) {
  520. aot_set_last_error("llvm build in bounds gep failed.");
  521. return false;
  522. }
  523. func_ctx->func_ptrs = LLVMBuildBitCast(comp_ctx->builder, func_ctx->func_ptrs,
  524. comp_ctx->exec_env_type, "func_ptrs_tmp");
  525. if (!func_ctx->func_ptrs) {
  526. aot_set_last_error("llvm build bit cast failed.");
  527. return false;
  528. }
  529. func_ctx->func_ptrs = LLVMBuildLoad(comp_ctx->builder, func_ctx->func_ptrs,
  530. "func_ptrs_ptr");
  531. if (!func_ctx->func_ptrs) {
  532. aot_set_last_error("llvm build load failed.");
  533. return false;
  534. }
  535. func_ctx->func_ptrs = LLVMBuildBitCast(comp_ctx->builder, func_ctx->func_ptrs,
  536. comp_ctx->exec_env_type, "func_ptrs");
  537. if (!func_ctx->func_ptrs) {
  538. aot_set_last_error("llvm build bit cast failed.");
  539. return false;
  540. }
  541. return true;
  542. }
  543. /**
  544. * Create function compiler context
  545. */
  546. static AOTFuncContext *
  547. aot_create_func_context(AOTCompData *comp_data, AOTCompContext *comp_ctx,
  548. AOTFunc *func, uint32 func_index)
  549. {
  550. AOTFuncContext *func_ctx;
  551. AOTFuncType *aot_func_type = comp_data->func_types[func->func_type_index];
  552. AOTBlock *aot_block;
  553. LLVMTypeRef int8_ptr_type, int32_ptr_type;
  554. LLVMValueRef aot_inst_offset = I32_TWO, aot_inst_addr;
  555. LLVMValueRef argv_buf_offset = I32_THREE, argv_buf_addr;
  556. LLVMValueRef stack_bound_offset = I32_FOUR, stack_bound_addr;
  557. LLVMValueRef aux_stack_bound_offset = I32_SIX, aux_stack_bound_addr;
  558. LLVMValueRef aux_stack_bottom_offset = I32_SEVEN, aux_stack_bottom_addr;
  559. char local_name[32];
  560. uint64 size;
  561. uint32 i, j = 0;
  562. /* Allocate memory for the function context */
  563. size = offsetof(AOTFuncContext, locals) + sizeof(LLVMValueRef) *
  564. ((uint64)aot_func_type->param_count + func->local_count);
  565. if (size >= UINT32_MAX
  566. || !(func_ctx = wasm_runtime_malloc((uint32)size))) {
  567. aot_set_last_error("allocate memory failed.");
  568. return NULL;
  569. }
  570. memset(func_ctx, 0, (uint32)size);
  571. func_ctx->aot_func = func;
  572. /* Add LLVM function */
  573. if (!(func_ctx->func = aot_add_llvm_func(comp_ctx, aot_func_type, func_index)))
  574. goto fail;
  575. /* Create function's first AOTBlock */
  576. if (!(aot_block = aot_create_func_block(comp_ctx, func_ctx,
  577. func, aot_func_type)))
  578. goto fail;
  579. aot_block_stack_push(&func_ctx->block_stack, aot_block);
  580. /* Add local variables */
  581. LLVMPositionBuilderAtEnd(comp_ctx->builder, aot_block->llvm_entry_block);
  582. /* Save the pameters for fast access */
  583. func_ctx->exec_env = LLVMGetParam(func_ctx->func, j++);
  584. /* Get aot inst address, the layout of exec_env is:
  585. exec_env->next, exec_env->prev, exec_env->module_inst, and argv_buf */
  586. if (!(aot_inst_addr =
  587. LLVMBuildInBoundsGEP(comp_ctx->builder, func_ctx->exec_env,
  588. &aot_inst_offset, 1, "aot_inst_addr"))) {
  589. aot_set_last_error("llvm build in bounds gep failed");
  590. goto fail;
  591. }
  592. /* Load aot inst */
  593. if (!(func_ctx->aot_inst = LLVMBuildLoad(comp_ctx->builder,
  594. aot_inst_addr, "aot_inst"))) {
  595. aot_set_last_error("llvm build load failed");
  596. goto fail;
  597. }
  598. /* Get argv buffer address */
  599. if (!(argv_buf_addr =
  600. LLVMBuildInBoundsGEP(comp_ctx->builder, func_ctx->exec_env,
  601. &argv_buf_offset, 1, "argv_buf_addr"))) {
  602. aot_set_last_error("llvm build in bounds gep failed");
  603. goto fail;
  604. }
  605. if (!(int32_ptr_type = LLVMPointerType(INT32_PTR_TYPE, 0))) {
  606. aot_set_last_error("llvm add pointer type failed");
  607. goto fail;
  608. }
  609. /* Convert to int32 pointer type */
  610. if (!(argv_buf_addr = LLVMBuildBitCast(comp_ctx->builder, argv_buf_addr,
  611. int32_ptr_type, "argv_buf_ptr"))) {
  612. aot_set_last_error("llvm build load failed");
  613. goto fail;
  614. }
  615. if (!(func_ctx->argv_buf = LLVMBuildLoad(comp_ctx->builder,
  616. argv_buf_addr, "argv_buf"))) {
  617. aot_set_last_error("llvm build load failed");
  618. goto fail;
  619. }
  620. /* Get native stack boundary address */
  621. if (!(stack_bound_addr =
  622. LLVMBuildInBoundsGEP(comp_ctx->builder, func_ctx->exec_env,
  623. &stack_bound_offset, 1, "stack_bound_addr"))) {
  624. aot_set_last_error("llvm build in bounds gep failed");
  625. goto fail;
  626. }
  627. if (!(func_ctx->native_stack_bound =
  628. LLVMBuildLoad(comp_ctx->builder,
  629. stack_bound_addr, "native_stack_bound"))) {
  630. aot_set_last_error("llvm build load failed");
  631. goto fail;
  632. }
  633. /* Get aux stack boundary address */
  634. if (!(aux_stack_bound_addr =
  635. LLVMBuildInBoundsGEP(comp_ctx->builder, func_ctx->exec_env,
  636. &aux_stack_bound_offset, 1,
  637. "aux_stack_bound_addr"))) {
  638. aot_set_last_error("llvm build in bounds gep failed");
  639. goto fail;
  640. }
  641. if (!(aux_stack_bound_addr =
  642. LLVMBuildBitCast(comp_ctx->builder,
  643. aux_stack_bound_addr,
  644. INT32_PTR_TYPE, "aux_stack_bound_ptr"))) {
  645. aot_set_last_error("llvm build bit cast failed");
  646. goto fail;
  647. }
  648. if (!(func_ctx->aux_stack_bound =
  649. LLVMBuildLoad(comp_ctx->builder,
  650. aux_stack_bound_addr, "aux_stack_bound"))) {
  651. aot_set_last_error("llvm build load failed");
  652. goto fail;
  653. }
  654. /* Get aux stack bottom address */
  655. if (!(aux_stack_bottom_addr =
  656. LLVMBuildInBoundsGEP(comp_ctx->builder, func_ctx->exec_env,
  657. &aux_stack_bottom_offset, 1,
  658. "aux_stack_bottom_addr"))) {
  659. aot_set_last_error("llvm build in bounds gep failed");
  660. goto fail;
  661. }
  662. if (!(aux_stack_bottom_addr =
  663. LLVMBuildBitCast(comp_ctx->builder,
  664. aux_stack_bottom_addr,
  665. INT32_PTR_TYPE, "aux_stack_bottom_ptr"))) {
  666. aot_set_last_error("llvm build bit cast failed");
  667. goto fail;
  668. }
  669. if (!(func_ctx->aux_stack_bottom =
  670. LLVMBuildLoad(comp_ctx->builder,
  671. aux_stack_bottom_addr, "aux_stack_bottom"))) {
  672. aot_set_last_error("llvm build load failed");
  673. goto fail;
  674. }
  675. for (i = 0; i < aot_func_type->param_count; i++, j++) {
  676. snprintf(local_name, sizeof(local_name), "l%d", i);
  677. func_ctx->locals[i] =
  678. LLVMBuildAlloca(comp_ctx->builder,
  679. TO_LLVM_TYPE(aot_func_type->types[i]),
  680. local_name);
  681. if (!func_ctx->locals[i]) {
  682. aot_set_last_error("llvm build alloca failed.");
  683. goto fail;
  684. }
  685. if (!LLVMBuildStore(comp_ctx->builder,
  686. LLVMGetParam(func_ctx->func, j),
  687. func_ctx->locals[i])) {
  688. aot_set_last_error("llvm build store failed.");
  689. goto fail;
  690. }
  691. }
  692. for (i = 0; i < func->local_count; i++) {
  693. LLVMTypeRef local_type;
  694. LLVMValueRef local_value = NULL;
  695. snprintf(local_name, sizeof(local_name), "l%d",
  696. aot_func_type->param_count + i);
  697. local_type = TO_LLVM_TYPE(func->local_types[i]);
  698. func_ctx->locals[aot_func_type->param_count + i] =
  699. LLVMBuildAlloca(comp_ctx->builder, local_type, local_name);
  700. if (!func_ctx->locals[aot_func_type->param_count + i]) {
  701. aot_set_last_error("llvm build alloca failed.");
  702. goto fail;
  703. }
  704. switch (func->local_types[i]) {
  705. case VALUE_TYPE_I32:
  706. local_value = I32_ZERO;
  707. break;
  708. case VALUE_TYPE_I64:
  709. local_value = I64_ZERO;
  710. break;
  711. case VALUE_TYPE_F32:
  712. local_value = F32_ZERO;
  713. break;
  714. case VALUE_TYPE_F64:
  715. local_value = F64_ZERO;
  716. break;
  717. case VALUE_TYPE_V128:
  718. local_value = V128_ZERO;
  719. break;
  720. case VALUE_TYPE_FUNCREF:
  721. case VALUE_TYPE_EXTERNREF:
  722. local_value = REF_NULL;
  723. break;
  724. default:
  725. bh_assert(0);
  726. break;
  727. }
  728. if (!LLVMBuildStore(comp_ctx->builder, local_value,
  729. func_ctx->locals[aot_func_type->param_count + i])) {
  730. aot_set_last_error("llvm build store failed.");
  731. goto fail;
  732. }
  733. }
  734. if (aot_func_type->param_count + func->local_count > 0) {
  735. func_ctx->last_alloca = func_ctx->locals[aot_func_type->param_count
  736. + func->local_count - 1];
  737. if (!(func_ctx->last_alloca =
  738. LLVMBuildBitCast(comp_ctx->builder, func_ctx->last_alloca,
  739. INT8_PTR_TYPE, "stack_ptr"))) {
  740. aot_set_last_error("llvm build bit cast failed.");
  741. goto fail;
  742. }
  743. }
  744. else {
  745. if (!(func_ctx->last_alloca = LLVMBuildAlloca(comp_ctx->builder, INT8_TYPE,
  746. "stack_ptr"))) {
  747. aot_set_last_error("llvm build alloca failed.");
  748. goto fail;
  749. }
  750. }
  751. if (!(int8_ptr_type = LLVMPointerType(INT8_PTR_TYPE, 0))) {
  752. aot_set_last_error("llvm add pointer type failed.");
  753. goto fail;
  754. }
  755. /* Create base addr, end addr, data size of mem, heap */
  756. if (!create_memory_info(comp_ctx, func_ctx, int8_ptr_type, func_index))
  757. goto fail;
  758. /* Load current exception */
  759. if (!create_cur_exception(comp_ctx, func_ctx))
  760. goto fail;
  761. /* Load function type indexes */
  762. if (!create_func_type_indexes(comp_ctx, func_ctx))
  763. goto fail;
  764. /* Load function pointers */
  765. if (!create_func_ptrs(comp_ctx, func_ctx))
  766. goto fail;
  767. return func_ctx;
  768. fail:
  769. if (func_ctx->mem_info)
  770. wasm_runtime_free(func_ctx->mem_info);
  771. aot_block_stack_destroy(&func_ctx->block_stack);
  772. wasm_runtime_free(func_ctx);
  773. return NULL;
  774. }
  775. static void
  776. aot_destroy_func_contexts(AOTFuncContext **func_ctxes, uint32 count)
  777. {
  778. uint32 i;
  779. for (i = 0; i < count; i++)
  780. if (func_ctxes[i]) {
  781. if (func_ctxes[i]->mem_info)
  782. wasm_runtime_free(func_ctxes[i]->mem_info);
  783. aot_block_stack_destroy(&func_ctxes[i]->block_stack);
  784. aot_checked_addr_list_destroy(func_ctxes[i]);
  785. wasm_runtime_free(func_ctxes[i]);
  786. }
  787. wasm_runtime_free(func_ctxes);
  788. }
  789. /**
  790. * Create function compiler contexts
  791. */
  792. static AOTFuncContext **
  793. aot_create_func_contexts(AOTCompData *comp_data, AOTCompContext *comp_ctx)
  794. {
  795. AOTFuncContext **func_ctxes;
  796. uint64 size;
  797. uint32 i;
  798. /* Allocate memory */
  799. size = sizeof(AOTFuncContext*) * (uint64)comp_data->func_count;
  800. if (size >= UINT32_MAX
  801. || !(func_ctxes = wasm_runtime_malloc((uint32)size))) {
  802. aot_set_last_error("allocate memory failed.");
  803. return NULL;
  804. }
  805. memset(func_ctxes, 0, size);
  806. /* Create each function context */
  807. for (i = 0; i < comp_data->func_count; i++) {
  808. AOTFunc *func = comp_data->funcs[i];
  809. if (!(func_ctxes[i] = aot_create_func_context(comp_data, comp_ctx,
  810. func, i))) {
  811. aot_destroy_func_contexts(func_ctxes, comp_data->func_count);
  812. return NULL;
  813. }
  814. }
  815. return func_ctxes;
  816. }
  817. static bool
  818. aot_set_llvm_basic_types(AOTLLVMTypes *basic_types, LLVMContextRef context)
  819. {
  820. basic_types->int1_type = LLVMInt1TypeInContext(context);
  821. basic_types->int8_type = LLVMInt8TypeInContext(context);
  822. basic_types->int16_type = LLVMInt16TypeInContext(context);
  823. basic_types->int32_type = LLVMInt32TypeInContext(context);
  824. basic_types->int64_type = LLVMInt64TypeInContext(context);
  825. basic_types->float32_type = LLVMFloatTypeInContext(context);
  826. basic_types->float64_type = LLVMDoubleTypeInContext(context);
  827. basic_types->void_type = LLVMVoidTypeInContext(context);
  828. basic_types->meta_data_type = LLVMMetadataTypeInContext(context);
  829. basic_types->int8_ptr_type = LLVMPointerType(basic_types->int8_type, 0);
  830. if (basic_types->int8_ptr_type) {
  831. basic_types->int8_pptr_type =
  832. LLVMPointerType(basic_types->int8_ptr_type, 0);
  833. }
  834. basic_types->int16_ptr_type = LLVMPointerType(basic_types->int16_type, 0);
  835. basic_types->int32_ptr_type = LLVMPointerType(basic_types->int32_type, 0);
  836. basic_types->int64_ptr_type = LLVMPointerType(basic_types->int64_type, 0);
  837. basic_types->float32_ptr_type = LLVMPointerType(basic_types->float32_type, 0);
  838. basic_types->float64_ptr_type = LLVMPointerType(basic_types->float64_type, 0);
  839. basic_types->i8x16_vec_type = LLVMVectorType(basic_types->int8_type, 16);
  840. basic_types->i16x8_vec_type = LLVMVectorType(basic_types->int16_type, 8);
  841. basic_types->i32x4_vec_type = LLVMVectorType(basic_types->int32_type, 4);
  842. basic_types->i64x2_vec_type = LLVMVectorType(basic_types->int64_type, 2);
  843. basic_types->f32x4_vec_type = LLVMVectorType(basic_types->float32_type, 4);
  844. basic_types->f64x2_vec_type = LLVMVectorType(basic_types->float64_type, 2);
  845. basic_types->v128_type = basic_types->i64x2_vec_type;
  846. basic_types->v128_ptr_type = LLVMPointerType(basic_types->v128_type, 0);
  847. basic_types->funcref_type = LLVMInt32TypeInContext(context);
  848. basic_types->externref_type = LLVMInt32TypeInContext(context);
  849. return (basic_types->int8_ptr_type
  850. && basic_types->int8_pptr_type
  851. && basic_types->int16_ptr_type
  852. && basic_types->int32_ptr_type
  853. && basic_types->int64_ptr_type
  854. && basic_types->float32_ptr_type
  855. && basic_types->float64_ptr_type
  856. && basic_types->i8x16_vec_type
  857. && basic_types->i16x8_vec_type
  858. && basic_types->i32x4_vec_type
  859. && basic_types->i64x2_vec_type
  860. && basic_types->f32x4_vec_type
  861. && basic_types->f64x2_vec_type
  862. && basic_types->meta_data_type
  863. && basic_types->funcref_type
  864. && basic_types->externref_type) ? true : false;
  865. }
  866. static bool
  867. aot_create_llvm_consts(AOTLLVMConsts *consts, AOTCompContext *comp_ctx)
  868. {
  869. LLVMValueRef i64_consts[2];
  870. consts->i8_zero = I8_CONST(0);
  871. consts->i32_zero = I32_CONST(0);
  872. consts->i64_zero = I64_CONST(0);
  873. consts->f32_zero = F32_CONST(0);
  874. consts->f64_zero = F64_CONST(0);
  875. if (consts->i64_zero) {
  876. i64_consts[0] = i64_consts[1] = consts->i64_zero;
  877. consts->v128_zero = consts->i64x2_vec_zero =
  878. LLVMConstVector(i64_consts, 2);
  879. if (consts->i64x2_vec_zero) {
  880. consts->i8x16_vec_zero = TO_V128_i8x16(consts->i64x2_vec_zero);
  881. consts->i16x8_vec_zero = TO_V128_i16x8(consts->i64x2_vec_zero);
  882. consts->i32x4_vec_zero = TO_V128_i32x4(consts->i64x2_vec_zero);
  883. consts->f32x4_vec_zero = TO_V128_f32x4(consts->i64x2_vec_zero);
  884. consts->f64x2_vec_zero = TO_V128_f64x2(consts->i64x2_vec_zero);
  885. }
  886. }
  887. consts->i32_one = I32_CONST(1);
  888. consts->i32_two = I32_CONST(2);
  889. consts->i32_three = I32_CONST(3);
  890. consts->i32_four = I32_CONST(4);
  891. consts->i32_five = I32_CONST(5);
  892. consts->i32_six = I32_CONST(6);
  893. consts->i32_seven = I32_CONST(7);
  894. consts->i32_eight = I32_CONST(8);
  895. consts->i32_neg_one = I32_CONST((uint32)-1);
  896. consts->i64_neg_one = I64_CONST((uint64)-1);
  897. consts->i32_min = I32_CONST((uint32)INT32_MIN);
  898. consts->i64_min = I64_CONST((uint64)INT64_MIN);
  899. consts->i32_31 = I32_CONST(31);
  900. consts->i32_32 = I32_CONST(32);
  901. consts->i64_63 = I64_CONST(63);
  902. consts->i64_64 = I64_CONST(64);
  903. consts->ref_null = I32_CONST(NULL_REF);
  904. return (consts->i8_zero
  905. && consts->i32_zero
  906. && consts->i64_zero
  907. && consts->f32_zero
  908. && consts->f64_zero
  909. && consts->i8x16_vec_zero
  910. && consts->i16x8_vec_zero
  911. && consts->i32x4_vec_zero
  912. && consts->i64x2_vec_zero
  913. && consts->f32x4_vec_zero
  914. && consts->f64x2_vec_zero
  915. && consts->i32_one
  916. && consts->i32_two
  917. && consts->i32_three
  918. && consts->i32_four
  919. && consts->i32_five
  920. && consts->i32_six
  921. && consts->i32_seven
  922. && consts->i32_eight
  923. && consts->i32_neg_one
  924. && consts->i64_neg_one
  925. && consts->i32_min
  926. && consts->i64_min
  927. && consts->i32_31
  928. && consts->i32_32
  929. && consts->i64_63
  930. && consts->i64_64
  931. && consts->ref_null) ? true : false;
  932. }
  933. typedef struct ArchItem {
  934. char *arch;
  935. bool support_eb;
  936. } ArchItem;
  937. static ArchItem valid_archs[] = {
  938. { "x86_64", false },
  939. { "i386", false },
  940. { "xtensa", false},
  941. { "mips", true },
  942. { "aarch64v8", false },
  943. { "aarch64v8.1", false },
  944. { "aarch64v8.2", false },
  945. { "aarch64v8.3", false },
  946. { "aarch64v8.4", false },
  947. { "aarch64v8.5", false },
  948. { "aarch64_bev8", false }, /* big endian */
  949. { "aarch64_bev8.1", false },
  950. { "aarch64_bev8.2", false },
  951. { "aarch64_bev8.3", false },
  952. { "aarch64_bev8.4", false },
  953. { "aarch64_bev8.5", false },
  954. { "armv4", true },
  955. { "armv4t", true },
  956. { "armv5t", true },
  957. { "armv5te", true },
  958. { "armv5tej", true },
  959. { "armv6", true },
  960. { "armv6kz", true },
  961. { "armv6t2", true },
  962. { "armv6k", true },
  963. { "armv7", true },
  964. { "armv6m", true },
  965. { "armv6sm", true },
  966. { "armv7em", true },
  967. { "armv8a", true },
  968. { "armv8r", true },
  969. { "armv8m.base", true },
  970. { "armv8m.main", true },
  971. { "armv8.1m.main", true },
  972. { "thumbv4", true },
  973. { "thumbv4t", true },
  974. { "thumbv5t", true },
  975. { "thumbv5te", true },
  976. { "thumbv5tej", true },
  977. { "thumbv6", true },
  978. { "thumbv6kz", true },
  979. { "thumbv6t2", true },
  980. { "thumbv6k", true },
  981. { "thumbv7", true },
  982. { "thumbv6m", true },
  983. { "thumbv6sm", true },
  984. { "thumbv7em", true },
  985. { "thumbv8a", true },
  986. { "thumbv8r", true },
  987. { "thumbv8m.base", true },
  988. { "thumbv8m.main", true },
  989. { "thumbv8.1m.main", true }
  990. };
  991. static const char *valid_abis[] = {
  992. "gnu",
  993. "eabi",
  994. "gnueabihf",
  995. "msvc"
  996. };
  997. static void
  998. print_supported_targets()
  999. {
  1000. uint32 i;
  1001. os_printf("Supported targets:\n");
  1002. for (i = 0; i < sizeof(valid_archs) / sizeof(ArchItem); i++) {
  1003. os_printf("%s ", valid_archs[i].arch);
  1004. if (valid_archs[i].support_eb)
  1005. os_printf("%seb ", valid_archs[i].arch);
  1006. }
  1007. os_printf("\n");
  1008. }
  1009. static void
  1010. print_supported_abis()
  1011. {
  1012. uint32 i;
  1013. os_printf("Supported ABI: ");
  1014. for (i = 0; i < sizeof(valid_abis) / sizeof(const char *); i++)
  1015. os_printf("%s ", valid_abis[i]);
  1016. os_printf("\n");
  1017. }
  1018. static bool
  1019. check_target_arch(const char *target_arch)
  1020. {
  1021. uint32 i;
  1022. char *arch;
  1023. bool support_eb;
  1024. for (i = 0; i < sizeof(valid_archs) / sizeof(ArchItem); i++) {
  1025. arch = valid_archs[i].arch;
  1026. support_eb = valid_archs[i].support_eb;
  1027. if (!strncmp(target_arch, arch, strlen(arch))
  1028. && ((support_eb && (!strcmp(target_arch + strlen(arch), "eb")
  1029. || !strcmp(target_arch + strlen(arch), "")))
  1030. || (!support_eb && !strcmp(target_arch + strlen(arch), "")))) {
  1031. return true;
  1032. }
  1033. }
  1034. return false;
  1035. }
  1036. static bool
  1037. check_target_abi(const char *target_abi)
  1038. {
  1039. uint32 i;
  1040. for (i = 0; i < sizeof(valid_abis) / sizeof(char *); i++) {
  1041. if (!strcmp(target_abi, valid_abis[i]))
  1042. return true;
  1043. }
  1044. return false;
  1045. }
  1046. static void
  1047. get_target_arch_from_triple(const char *triple, char *arch_buf, uint32 buf_size)
  1048. {
  1049. uint32 i = 0;
  1050. while (*triple != '-' && *triple != '\0' && i < buf_size - 1)
  1051. arch_buf[i++] = *triple++;
  1052. /* Make sure buffer is long enough */
  1053. bh_assert(*triple == '-' || *triple == '\0');
  1054. }
  1055. LLVMBool
  1056. WAMRCreateMCJITCompilerForModule(LLVMExecutionEngineRef *OutJIT,
  1057. LLVMModuleRef M,
  1058. struct LLVMMCJITCompilerOptions *Options,
  1059. size_t SizeOfOptions,
  1060. char **OutError);
  1061. void LLVMAddPromoteMemoryToRegisterPass(LLVMPassManagerRef PM);
  1062. AOTCompContext *
  1063. aot_create_comp_context(AOTCompData *comp_data,
  1064. aot_comp_option_t option)
  1065. {
  1066. AOTCompContext *comp_ctx, *ret = NULL;
  1067. /*LLVMTypeRef elem_types[8];*/
  1068. struct LLVMMCJITCompilerOptions jit_options;
  1069. LLVMTargetRef target;
  1070. char *triple = NULL, *triple_norm, *arch, *abi;
  1071. char *cpu = NULL, *features, buf[128];
  1072. char *triple_norm_new = NULL, *cpu_new = NULL;
  1073. char *err = NULL, *fp_round= "round.tonearest", *fp_exce = "fpexcept.strict";
  1074. char triple_buf[32] = {0};
  1075. uint32 opt_level, size_level;
  1076. LLVMCodeModel code_model;
  1077. LLVMTargetDataRef target_data_ref;
  1078. /* Initialize LLVM environment */
  1079. LLVMInitializeAllTargetInfos();
  1080. LLVMInitializeAllTargets();
  1081. LLVMInitializeAllTargetMCs();
  1082. LLVMInitializeAllAsmPrinters();
  1083. LLVMLinkInMCJIT();
  1084. /* Allocate memory */
  1085. if (!(comp_ctx = wasm_runtime_malloc(sizeof(AOTCompContext)))) {
  1086. aot_set_last_error("allocate memory failed.");
  1087. return NULL;
  1088. }
  1089. memset(comp_ctx, 0, sizeof(AOTCompContext));
  1090. comp_ctx->comp_data = comp_data;
  1091. /* Create LLVM context, module and builder */
  1092. if (!(comp_ctx->context = LLVMContextCreate())) {
  1093. aot_set_last_error("create LLVM context failed.");
  1094. goto fail;
  1095. }
  1096. if (!(comp_ctx->builder = LLVMCreateBuilderInContext(comp_ctx->context))) {
  1097. aot_set_last_error("create LLVM builder failed.");
  1098. goto fail;
  1099. }
  1100. if (!(comp_ctx->module =
  1101. LLVMModuleCreateWithNameInContext("WASM Module", comp_ctx->context))) {
  1102. aot_set_last_error("create LLVM module failed.");
  1103. goto fail;
  1104. }
  1105. if (option->enable_bulk_memory)
  1106. comp_ctx->enable_bulk_memory = true;
  1107. if (option->enable_thread_mgr)
  1108. comp_ctx->enable_thread_mgr = true;
  1109. if (option->enable_tail_call)
  1110. comp_ctx->enable_tail_call = true;
  1111. if (option->enable_ref_types)
  1112. comp_ctx->enable_ref_types = true;
  1113. if (option->enable_aux_stack_frame)
  1114. comp_ctx->enable_aux_stack_frame = true;
  1115. if (option->enable_aux_stack_check)
  1116. comp_ctx->enable_aux_stack_check = true;
  1117. if (option->is_jit_mode) {
  1118. char *triple_jit = NULL;
  1119. /* Create LLVM execution engine */
  1120. LLVMInitializeMCJITCompilerOptions(&jit_options, sizeof(jit_options));
  1121. jit_options.OptLevel = LLVMCodeGenLevelAggressive;
  1122. jit_options.EnableFastISel = true;
  1123. /*jit_options.CodeModel = LLVMCodeModelSmall;*/
  1124. if (WAMRCreateMCJITCompilerForModule
  1125. (&comp_ctx->exec_engine, comp_ctx->module,
  1126. &jit_options, sizeof(jit_options), &err) != 0) {
  1127. if (err) {
  1128. LLVMDisposeMessage(err);
  1129. err = NULL;
  1130. }
  1131. aot_set_last_error("create LLVM JIT compiler failed.");
  1132. goto fail;
  1133. }
  1134. comp_ctx->is_jit_mode = true;
  1135. comp_ctx->target_machine =
  1136. LLVMGetExecutionEngineTargetMachine(comp_ctx->exec_engine);
  1137. #ifndef OS_ENABLE_HW_BOUND_CHECK
  1138. comp_ctx->enable_bound_check = true;
  1139. #else
  1140. comp_ctx->enable_bound_check = false;
  1141. #endif
  1142. if (!(triple_jit =
  1143. LLVMGetTargetMachineTriple(comp_ctx->target_machine))) {
  1144. aot_set_last_error("can not get triple from the target machine");
  1145. goto fail;
  1146. }
  1147. /* Save target arch */
  1148. get_target_arch_from_triple(triple_jit, comp_ctx->target_arch,
  1149. sizeof(comp_ctx->target_arch));
  1150. LLVMDisposeMessage(triple_jit);
  1151. }
  1152. else {
  1153. /* Create LLVM target machine */
  1154. arch = option->target_arch;
  1155. abi = option->target_abi;
  1156. cpu = option->target_cpu;
  1157. features = option->cpu_features;
  1158. opt_level = option->opt_level;
  1159. size_level = option->size_level;
  1160. if (arch) {
  1161. /* Add default sub-arch if not specified */
  1162. if (!strcmp(arch, "arm"))
  1163. arch = "armv4";
  1164. else if (!strcmp(arch, "armeb"))
  1165. arch = "armv4eb";
  1166. else if (!strcmp(arch, "thumb"))
  1167. arch = "thumbv4t";
  1168. else if (!strcmp(arch, "thumbeb"))
  1169. arch = "thumbv4teb";
  1170. else if (!strcmp(arch, "aarch64"))
  1171. arch = "aarch64v8";
  1172. else if (!strcmp(arch, "aarch64_be"))
  1173. arch = "aarch64_bev8";
  1174. }
  1175. /* Check target arch */
  1176. if (arch && !check_target_arch(arch)) {
  1177. if (!strcmp(arch, "help"))
  1178. print_supported_targets();
  1179. else
  1180. aot_set_last_error("Invalid target. "
  1181. "Use --target=help to list all supported targets");
  1182. goto fail;
  1183. }
  1184. /* Check target ABI */
  1185. if (abi && !check_target_abi(abi)) {
  1186. if (!strcmp(abi, "help"))
  1187. print_supported_abis();
  1188. else
  1189. aot_set_last_error("Invalid target ABI. "
  1190. "Use --target-abi=help to list all supported ABI");
  1191. goto fail;
  1192. }
  1193. if (arch) {
  1194. /* Construct target triple: <arch>-<vendor>-<sys>-<abi> */
  1195. const char *vendor_sys;
  1196. char *default_triple = LLVMGetDefaultTargetTriple();
  1197. if (!default_triple) {
  1198. aot_set_last_error("llvm get default target triple failed.");
  1199. goto fail;
  1200. }
  1201. if (strstr(default_triple, "windows")) {
  1202. vendor_sys = "-pc-windows-";
  1203. if (!abi)
  1204. abi = "msvc";
  1205. }
  1206. else if (strstr(default_triple, "win32")) {
  1207. vendor_sys = "-pc-win32-";
  1208. if (!abi)
  1209. abi = "msvc";
  1210. }
  1211. else {
  1212. vendor_sys = "-pc-linux-";
  1213. if (!abi)
  1214. abi = "gnu";
  1215. }
  1216. LLVMDisposeMessage(default_triple);
  1217. bh_assert(strlen(arch) + strlen(vendor_sys) + strlen(abi) < sizeof(triple_buf));
  1218. memcpy(triple_buf, arch, strlen(arch));
  1219. memcpy(triple_buf + strlen(arch), vendor_sys, strlen(vendor_sys));
  1220. memcpy(triple_buf + strlen(arch) + strlen(vendor_sys), abi, strlen(abi));
  1221. triple = triple_buf;
  1222. }
  1223. if (!cpu && features) {
  1224. aot_set_last_error("cpu isn't specified for cpu features.");
  1225. goto fail;
  1226. }
  1227. if (!triple && !cpu) {
  1228. /* Get a triple for the host machine */
  1229. if (!(triple_norm = triple_norm_new = LLVMGetDefaultTargetTriple())) {
  1230. aot_set_last_error("llvm get default target triple failed.");
  1231. goto fail;
  1232. }
  1233. /* Get CPU name of the host machine */
  1234. if (!(cpu = cpu_new = LLVMGetHostCPUName())) {
  1235. aot_set_last_error("llvm get host cpu name failed.");
  1236. goto fail;
  1237. }
  1238. }
  1239. else if (triple) {
  1240. /* Normalize a target triple */
  1241. if (!(triple_norm = triple_norm_new = LLVMNormalizeTargetTriple(triple))) {
  1242. snprintf(buf, sizeof(buf),
  1243. "llvm normlalize target triple (%s) failed.", triple);
  1244. aot_set_last_error(buf);
  1245. goto fail;
  1246. }
  1247. if (!cpu)
  1248. cpu = "";
  1249. }
  1250. else {
  1251. /* triple is NULL, cpu isn't NULL */
  1252. snprintf(buf, sizeof(buf),
  1253. "target isn't specified for cpu %s.", cpu);
  1254. aot_set_last_error(buf);
  1255. goto fail;
  1256. }
  1257. if (!features)
  1258. features = "";
  1259. /* Get target with triple, note that LLVMGetTargetFromTriple()
  1260. return 0 when success, but not true. */
  1261. if (LLVMGetTargetFromTriple(triple_norm, &target, &err) != 0) {
  1262. if (err) {
  1263. LLVMDisposeMessage(err);
  1264. err = NULL;
  1265. }
  1266. snprintf(buf, sizeof(buf),
  1267. "llvm get target from triple (%s) failed", triple_norm);
  1268. aot_set_last_error(buf);
  1269. goto fail;
  1270. }
  1271. /* Save target arch */
  1272. get_target_arch_from_triple(triple_norm, comp_ctx->target_arch,
  1273. sizeof(comp_ctx->target_arch));
  1274. if (option->bounds_checks == 1 || option->bounds_checks == 0) {
  1275. /* Set by user */
  1276. comp_ctx->enable_bound_check =
  1277. (option->bounds_checks == 1) ? true : false;
  1278. }
  1279. else {
  1280. /* Unset by user, use default value */
  1281. if (strstr(comp_ctx->target_arch, "64") && !option->is_sgx_platform) {
  1282. comp_ctx->enable_bound_check = false;
  1283. }
  1284. else {
  1285. comp_ctx->enable_bound_check = true;
  1286. }
  1287. }
  1288. os_printf("Create AoT compiler with:\n");
  1289. os_printf(" target: %s\n", comp_ctx->target_arch);
  1290. os_printf(" target cpu: %s\n", cpu);
  1291. os_printf(" cpu features: %s\n", features);
  1292. os_printf(" opt level: %d\n", opt_level);
  1293. os_printf(" size level: %d\n", size_level);
  1294. switch (option->output_format) {
  1295. case AOT_LLVMIR_UNOPT_FILE:
  1296. os_printf(" output format: unoptimized LLVM IR\n");
  1297. break;
  1298. case AOT_LLVMIR_OPT_FILE:
  1299. os_printf(" output format: optimized LLVM IR\n");
  1300. break;
  1301. case AOT_FORMAT_FILE:
  1302. os_printf(" output format: AoT file\n");
  1303. break;
  1304. case AOT_OBJECT_FILE:
  1305. os_printf(" output format: native object file\n");
  1306. break;
  1307. }
  1308. if (!LLVMTargetHasTargetMachine(target)) {
  1309. snprintf(buf, sizeof(buf),
  1310. "no target machine for this target (%s).", triple_norm);
  1311. aot_set_last_error(buf);
  1312. goto fail;
  1313. }
  1314. if (!LLVMTargetHasAsmBackend(target)) {
  1315. snprintf(buf, sizeof(buf),
  1316. "no asm backend for this target (%s).", LLVMGetTargetName(target));
  1317. aot_set_last_error(buf);
  1318. goto fail;
  1319. }
  1320. /* Set code model */
  1321. if (size_level == 0)
  1322. code_model = LLVMCodeModelLarge;
  1323. else if (size_level == 1)
  1324. code_model = LLVMCodeModelMedium;
  1325. else if (size_level == 2)
  1326. code_model = LLVMCodeModelKernel;
  1327. else
  1328. code_model = LLVMCodeModelSmall;
  1329. /* Create the target machine */
  1330. if (!(comp_ctx->target_machine =
  1331. LLVMCreateTargetMachine(target, triple_norm, cpu, features,
  1332. opt_level, LLVMRelocStatic,
  1333. code_model))) {
  1334. aot_set_last_error("create LLVM target machine failed.");
  1335. goto fail;
  1336. }
  1337. }
  1338. if (option->enable_simd
  1339. && strcmp(comp_ctx->target_arch, "x86_64") != 0
  1340. && strncmp(comp_ctx->target_arch, "aarch64", 7) != 0) {
  1341. /* Disable simd if it isn't supported by target arch */
  1342. option->enable_simd = false;
  1343. }
  1344. if (option->enable_simd) {
  1345. char *tmp;
  1346. bool ret;
  1347. comp_ctx->enable_simd = true;
  1348. if (!(tmp = LLVMGetTargetMachineCPU(comp_ctx->target_machine))) {
  1349. aot_set_last_error("get CPU from Target Machine fail");
  1350. goto fail;
  1351. }
  1352. ret = aot_check_simd_compatibility(comp_ctx->target_arch, tmp);
  1353. LLVMDisposeMessage(tmp);
  1354. if (!ret) {
  1355. aot_set_last_error("SIMD compatibility check failed, "
  1356. "try adding --cpu=<cpu> to specify a cpu "
  1357. "or adding --disable-simd to disable SIMD");
  1358. goto fail;
  1359. }
  1360. }
  1361. if (!(target_data_ref =
  1362. LLVMCreateTargetDataLayout(comp_ctx->target_machine))) {
  1363. aot_set_last_error("create LLVM target data layout failed.");
  1364. goto fail;
  1365. }
  1366. comp_ctx->pointer_size = LLVMPointerSize(target_data_ref);
  1367. LLVMDisposeTargetData(target_data_ref);
  1368. comp_ctx->optimize = true;
  1369. if (option->output_format == AOT_LLVMIR_UNOPT_FILE)
  1370. comp_ctx->optimize = false;
  1371. if (!(comp_ctx->pass_mgr = LLVMCreateFunctionPassManagerForModule
  1372. (comp_ctx->module))) {
  1373. aot_set_last_error("create LLVM pass manager failed.");
  1374. goto fail;
  1375. }
  1376. LLVMAddPromoteMemoryToRegisterPass(comp_ctx->pass_mgr);
  1377. LLVMAddInstructionCombiningPass(comp_ctx->pass_mgr);
  1378. LLVMAddCFGSimplificationPass(comp_ctx->pass_mgr);
  1379. LLVMAddJumpThreadingPass(comp_ctx->pass_mgr);
  1380. LLVMAddConstantPropagationPass(comp_ctx->pass_mgr);
  1381. LLVMAddIndVarSimplifyPass(comp_ctx->pass_mgr);
  1382. if (!option->is_jit_mode) {
  1383. LLVMAddLoopRotatePass(comp_ctx->pass_mgr);
  1384. LLVMAddLoopUnswitchPass(comp_ctx->pass_mgr);
  1385. LLVMAddInstructionCombiningPass(comp_ctx->pass_mgr);
  1386. LLVMAddCFGSimplificationPass(comp_ctx->pass_mgr);
  1387. if (!option->enable_thread_mgr) {
  1388. /* These two passes may destroy the volatile semantics,
  1389. disable them when building as multi-thread mode */
  1390. LLVMAddGVNPass(comp_ctx->pass_mgr);
  1391. LLVMAddLICMPass(comp_ctx->pass_mgr);
  1392. }
  1393. LLVMAddLoopVectorizePass(comp_ctx->pass_mgr);
  1394. LLVMAddSLPVectorizePass(comp_ctx->pass_mgr);
  1395. LLVMAddInstructionCombiningPass(comp_ctx->pass_mgr);
  1396. LLVMAddCFGSimplificationPass(comp_ctx->pass_mgr);
  1397. }
  1398. /* Create metadata for llvm float experimental constrained intrinsics */
  1399. if (!(comp_ctx->fp_rounding_mode =
  1400. LLVMMDStringInContext(comp_ctx->context,
  1401. fp_round,
  1402. (uint32)strlen(fp_round)))
  1403. || !(comp_ctx->fp_exception_behavior =
  1404. LLVMMDStringInContext(comp_ctx->context,
  1405. fp_exce,
  1406. (uint32)strlen(fp_exce)))) {
  1407. aot_set_last_error("create float llvm metadata failed.");
  1408. goto fail;
  1409. }
  1410. if (!aot_set_llvm_basic_types(&comp_ctx->basic_types, comp_ctx->context)) {
  1411. aot_set_last_error("create LLVM basic types failed.");
  1412. goto fail;
  1413. }
  1414. if (!aot_create_llvm_consts(&comp_ctx->llvm_consts, comp_ctx)) {
  1415. aot_set_last_error("create LLVM const values failed.");
  1416. goto fail;
  1417. }
  1418. /* set exec_env data type to int8** */
  1419. comp_ctx->exec_env_type = comp_ctx->basic_types.int8_pptr_type;
  1420. /* set aot_inst data type to int8* */
  1421. comp_ctx->aot_inst_type = INT8_PTR_TYPE;
  1422. /* Create function context for each function */
  1423. comp_ctx->func_ctx_count = comp_data->func_count;
  1424. if (comp_data->func_count > 0
  1425. && !(comp_ctx->func_ctxes =
  1426. aot_create_func_contexts(comp_data, comp_ctx)))
  1427. goto fail;
  1428. ret = comp_ctx;
  1429. fail:
  1430. if (triple_norm_new)
  1431. LLVMDisposeMessage(triple_norm_new);
  1432. if (cpu_new)
  1433. LLVMDisposeMessage(cpu_new);
  1434. if (!ret)
  1435. aot_destroy_comp_context(comp_ctx);
  1436. return ret;
  1437. }
  1438. void
  1439. aot_destroy_comp_context(AOTCompContext *comp_ctx)
  1440. {
  1441. if (!comp_ctx)
  1442. return;
  1443. if (comp_ctx->pass_mgr)
  1444. LLVMDisposePassManager(comp_ctx->pass_mgr);
  1445. if (comp_ctx->target_machine && !comp_ctx->is_jit_mode)
  1446. LLVMDisposeTargetMachine(comp_ctx->target_machine);
  1447. if (comp_ctx->builder)
  1448. LLVMDisposeBuilder(comp_ctx->builder);
  1449. if (comp_ctx->exec_engine) {
  1450. LLVMDisposeExecutionEngine(comp_ctx->exec_engine);
  1451. /* The LLVM module is freed when disposing execution engine,
  1452. no need to dispose it again. */
  1453. }
  1454. else if (comp_ctx->module)
  1455. LLVMDisposeModule(comp_ctx->module);
  1456. if (comp_ctx->context)
  1457. LLVMContextDispose(comp_ctx->context);
  1458. if (comp_ctx->func_ctxes)
  1459. aot_destroy_func_contexts(comp_ctx->func_ctxes,
  1460. comp_ctx->func_ctx_count);
  1461. wasm_runtime_free(comp_ctx);
  1462. }
  1463. void
  1464. aot_value_stack_push(AOTValueStack *stack, AOTValue *value)
  1465. {
  1466. if (!stack->value_list_head)
  1467. stack->value_list_head = stack->value_list_end = value;
  1468. else {
  1469. stack->value_list_end->next = value;
  1470. value->prev = stack->value_list_end;
  1471. stack->value_list_end = value;
  1472. }
  1473. }
  1474. AOTValue *
  1475. aot_value_stack_pop(AOTValueStack *stack)
  1476. {
  1477. AOTValue *value = stack->value_list_end;
  1478. bh_assert(stack->value_list_end);
  1479. if (stack->value_list_head == stack->value_list_end)
  1480. stack->value_list_head = stack->value_list_end = NULL;
  1481. else {
  1482. stack->value_list_end = stack->value_list_end->prev;
  1483. stack->value_list_end->next = NULL;
  1484. value->prev = NULL;
  1485. }
  1486. return value;
  1487. }
  1488. void
  1489. aot_value_stack_destroy(AOTValueStack *stack)
  1490. {
  1491. AOTValue *value = stack->value_list_head, *p;
  1492. while (value) {
  1493. p = value->next;
  1494. wasm_runtime_free(value);
  1495. value = p;
  1496. }
  1497. }
  1498. void
  1499. aot_block_stack_push(AOTBlockStack *stack, AOTBlock *block)
  1500. {
  1501. if (!stack->block_list_head)
  1502. stack->block_list_head = stack->block_list_end = block;
  1503. else {
  1504. stack->block_list_end->next = block;
  1505. block->prev = stack->block_list_end;
  1506. stack->block_list_end = block;
  1507. }
  1508. }
  1509. AOTBlock *
  1510. aot_block_stack_pop(AOTBlockStack *stack)
  1511. {
  1512. AOTBlock *block = stack->block_list_end;
  1513. bh_assert(stack->block_list_end);
  1514. if (stack->block_list_head == stack->block_list_end)
  1515. stack->block_list_head = stack->block_list_end = NULL;
  1516. else {
  1517. stack->block_list_end = stack->block_list_end->prev;
  1518. stack->block_list_end->next = NULL;
  1519. block->prev = NULL;
  1520. }
  1521. return block;
  1522. }
  1523. void
  1524. aot_block_stack_destroy(AOTBlockStack *stack)
  1525. {
  1526. AOTBlock *block = stack->block_list_head, *p;
  1527. while (block) {
  1528. p = block->next;
  1529. aot_value_stack_destroy(&block->value_stack);
  1530. wasm_runtime_free(block);
  1531. block = p;
  1532. }
  1533. }
  1534. void
  1535. aot_block_destroy(AOTBlock *block)
  1536. {
  1537. aot_value_stack_destroy(&block->value_stack);
  1538. if (block->param_types)
  1539. wasm_runtime_free(block->param_types);
  1540. if (block->param_phis)
  1541. wasm_runtime_free(block->param_phis);
  1542. if (block->else_param_phis)
  1543. wasm_runtime_free(block->else_param_phis);
  1544. if (block->result_types)
  1545. wasm_runtime_free(block->result_types);
  1546. if (block->result_phis)
  1547. wasm_runtime_free(block->result_phis);
  1548. wasm_runtime_free(block);
  1549. }
  1550. bool
  1551. aot_checked_addr_list_add(AOTFuncContext *func_ctx,
  1552. uint32 local_idx, uint32 offset, uint32 bytes)
  1553. {
  1554. AOTCheckedAddr *node = func_ctx->checked_addr_list;
  1555. if (!(node = wasm_runtime_malloc(sizeof(AOTCheckedAddr)))) {
  1556. aot_set_last_error("allocate memory failed.");
  1557. return false;
  1558. }
  1559. node->local_idx = local_idx;
  1560. node->offset = offset;
  1561. node->bytes = bytes;
  1562. node->next = func_ctx->checked_addr_list;
  1563. func_ctx->checked_addr_list = node;
  1564. return true;
  1565. }
  1566. void
  1567. aot_checked_addr_list_del(AOTFuncContext *func_ctx, uint32 local_idx)
  1568. {
  1569. AOTCheckedAddr *node = func_ctx->checked_addr_list;
  1570. AOTCheckedAddr *node_prev = NULL, *node_next;
  1571. while (node) {
  1572. node_next = node->next;
  1573. if (node->local_idx == local_idx) {
  1574. if (!node_prev)
  1575. func_ctx->checked_addr_list = node_next;
  1576. else
  1577. node_prev->next = node_next;
  1578. wasm_runtime_free(node);
  1579. }
  1580. else {
  1581. node_prev = node;
  1582. }
  1583. node = node_next;
  1584. }
  1585. }
  1586. bool
  1587. aot_checked_addr_list_find(AOTFuncContext *func_ctx,
  1588. uint32 local_idx, uint32 offset, uint32 bytes)
  1589. {
  1590. AOTCheckedAddr *node = func_ctx->checked_addr_list;
  1591. while (node) {
  1592. if (node->local_idx == local_idx
  1593. && node->offset == offset
  1594. && node->bytes >= bytes) {
  1595. return true;
  1596. }
  1597. node = node->next;
  1598. }
  1599. return false;
  1600. }
  1601. void
  1602. aot_checked_addr_list_destroy(AOTFuncContext *func_ctx)
  1603. {
  1604. AOTCheckedAddr *node = func_ctx->checked_addr_list, *node_next;
  1605. while (node) {
  1606. node_next = node->next;
  1607. wasm_runtime_free(node);
  1608. node = node_next;
  1609. }
  1610. func_ctx->checked_addr_list = NULL;
  1611. }
  1612. bool
  1613. aot_build_zero_function_ret(AOTCompContext *comp_ctx,
  1614. AOTFuncType *func_type)
  1615. {
  1616. LLVMValueRef ret = NULL;
  1617. if (func_type->result_count) {
  1618. switch (func_type->types[func_type->param_count]) {
  1619. case VALUE_TYPE_I32:
  1620. ret = LLVMBuildRet(comp_ctx->builder, I32_ZERO);
  1621. break;
  1622. case VALUE_TYPE_I64:
  1623. ret = LLVMBuildRet(comp_ctx->builder, I64_ZERO);
  1624. break;
  1625. case VALUE_TYPE_F32:
  1626. ret = LLVMBuildRet(comp_ctx->builder, F32_ZERO);
  1627. break;
  1628. case VALUE_TYPE_F64:
  1629. ret = LLVMBuildRet(comp_ctx->builder, F64_ZERO);
  1630. break;
  1631. case VALUE_TYPE_V128:
  1632. ret = LLVMBuildRet(comp_ctx->builder, V128_ZERO);
  1633. break;
  1634. case VALUE_TYPE_FUNCREF:
  1635. case VALUE_TYPE_EXTERNREF:
  1636. ret = LLVMBuildRet(comp_ctx->builder, REF_NULL);
  1637. break;
  1638. default:
  1639. bh_assert(0);
  1640. }
  1641. }
  1642. else {
  1643. ret = LLVMBuildRetVoid(comp_ctx->builder);
  1644. }
  1645. if (!ret) {
  1646. aot_set_last_error("llvm build ret failed.");
  1647. return false;
  1648. }
  1649. return true;
  1650. }
  1651. static LLVMValueRef
  1652. __call_llvm_intrinsic(const AOTCompContext *comp_ctx,
  1653. const char *name,
  1654. LLVMTypeRef ret_type,
  1655. LLVMTypeRef *param_types,
  1656. int param_count,
  1657. LLVMValueRef *param_values)
  1658. {
  1659. LLVMValueRef func, ret;
  1660. LLVMTypeRef func_type;
  1661. /* Declare llvm intrinsic function if necessary */
  1662. if (!(func = LLVMGetNamedFunction(comp_ctx->module, name))) {
  1663. if (!(func_type = LLVMFunctionType(ret_type, param_types,
  1664. (uint32)param_count, false))) {
  1665. aot_set_last_error("create LLVM function type failed.");
  1666. return NULL;
  1667. }
  1668. if (!(func = LLVMAddFunction(comp_ctx->module, name, func_type))) {
  1669. aot_set_last_error("add LLVM function failed.");
  1670. return NULL;
  1671. }
  1672. }
  1673. /* Call the LLVM intrinsic function */
  1674. if (!(ret = LLVMBuildCall(comp_ctx->builder, func, param_values,
  1675. (uint32)param_count, "call"))) {
  1676. aot_set_last_error("llvm build call failed.");
  1677. return NULL;
  1678. }
  1679. return ret;
  1680. }
  1681. LLVMValueRef
  1682. aot_call_llvm_intrinsic(const AOTCompContext *comp_ctx,
  1683. const char *name,
  1684. LLVMTypeRef ret_type,
  1685. LLVMTypeRef *param_types,
  1686. int param_count,
  1687. ...)
  1688. {
  1689. LLVMValueRef *param_values, ret;
  1690. va_list argptr;
  1691. uint64 total_size;
  1692. int i = 0;
  1693. /* Create param values */
  1694. total_size = sizeof(LLVMValueRef) * (uint64)param_count;
  1695. if (total_size >= UINT32_MAX
  1696. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  1697. aot_set_last_error("allocate memory for param values failed.");
  1698. return false;
  1699. }
  1700. /* Load each param value */
  1701. va_start(argptr, param_count);
  1702. while (i < param_count)
  1703. param_values[i++] = va_arg(argptr, LLVMValueRef);
  1704. va_end(argptr);
  1705. ret = __call_llvm_intrinsic(comp_ctx, name, ret_type, param_types,
  1706. param_count, param_values);
  1707. wasm_runtime_free(param_values);
  1708. return ret;
  1709. }
  1710. LLVMValueRef
  1711. aot_call_llvm_intrinsic_v(const AOTCompContext *comp_ctx,
  1712. const char *name,
  1713. LLVMTypeRef ret_type,
  1714. LLVMTypeRef *param_types,
  1715. int param_count,
  1716. va_list param_value_list)
  1717. {
  1718. LLVMValueRef *param_values, ret;
  1719. uint64 total_size;
  1720. int i = 0;
  1721. /* Create param values */
  1722. total_size = sizeof(LLVMValueRef) * (uint64)param_count;
  1723. if (total_size >= UINT32_MAX
  1724. || !(param_values = wasm_runtime_malloc((uint32)total_size))) {
  1725. aot_set_last_error("allocate memory for param values failed.");
  1726. return false;
  1727. }
  1728. /* Load each param value */
  1729. while (i < param_count)
  1730. param_values[i++] = va_arg(param_value_list, LLVMValueRef);
  1731. ret = __call_llvm_intrinsic(comp_ctx, name, ret_type, param_types,
  1732. param_count, param_values);
  1733. wasm_runtime_free(param_values);
  1734. return ret;
  1735. }