aot_emit_gc.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "aot_emit_gc.h"
  6. #include "aot_compiler.h"
  7. #include "aot_emit_exception.h"
  8. #if WASM_ENABLE_GC != 0
  9. #define BUILD_ISNULL(ptr, res, name) \
  10. do { \
  11. if (!(res = LLVMBuildIsNull(comp_ctx->builder, ptr, name))) { \
  12. aot_set_last_error("llvm build isnull failed."); \
  13. goto fail; \
  14. } \
  15. } while (0)
  16. #define BUILD_ISNOTNULL(ptr, res, name) \
  17. do { \
  18. if (!(res = LLVMBuildIsNotNull(comp_ctx->builder, ptr, name))) { \
  19. aot_set_last_error("llvm build isnotnull failed."); \
  20. goto fail; \
  21. } \
  22. } while (0)
  23. #define ADD_BASIC_BLOCK(block, name) \
  24. do { \
  25. if (!(block = LLVMAppendBasicBlockInContext(comp_ctx->context, \
  26. func_ctx->func, name))) { \
  27. aot_set_last_error("llvm add basic block failed."); \
  28. goto fail; \
  29. } \
  30. } while (0)
  31. #define CURR_BLOCK() LLVMGetInsertBlock(comp_ctx->builder)
  32. #define MOVE_BLOCK_AFTER(llvm_block, llvm_block_after) \
  33. LLVMMoveBasicBlockAfter(llvm_block, llvm_block_after)
  34. #define MOVE_BLOCK_AFTER_CURR(llvm_block) \
  35. LLVMMoveBasicBlockAfter(llvm_block, CURR_BLOCK())
  36. #define MOVE_BLOCK_BEFORE(llvm_block, llvm_block_before) \
  37. LLVMMoveBasicBlockBefore(llvm_block, llvm_block_before)
  38. #define BUILD_COND_BR(value_if, block_then, block_else) \
  39. do { \
  40. if (!LLVMBuildCondBr(comp_ctx->builder, value_if, block_then, \
  41. block_else)) { \
  42. aot_set_last_error("llvm build cond br failed."); \
  43. goto fail; \
  44. } \
  45. } while (0)
  46. #define SET_BUILDER_POS(llvm_block) \
  47. LLVMPositionBuilderAtEnd(comp_ctx->builder, llvm_block)
  48. #define BUILD_BR(llvm_block) \
  49. do { \
  50. if (!LLVMBuildBr(comp_ctx->builder, llvm_block)) { \
  51. aot_set_last_error("llvm build br failed."); \
  52. goto fail; \
  53. } \
  54. } while (0)
  55. #define BUILD_ICMP(op, left, right, res, name) \
  56. do { \
  57. if (!(res = \
  58. LLVMBuildICmp(comp_ctx->builder, op, left, right, name))) { \
  59. aot_set_last_error("llvm build icmp failed."); \
  60. goto fail; \
  61. } \
  62. } while (0)
  63. static bool
  64. is_target_x86(AOTCompContext *comp_ctx)
  65. {
  66. return !strncmp(comp_ctx->target_arch, "x86_64", 6)
  67. || !strncmp(comp_ctx->target_arch, "i386", 4);
  68. }
  69. bool
  70. aot_call_aot_create_func_obj(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  71. LLVMValueRef func_idx, LLVMValueRef *p_gc_obj)
  72. {
  73. LLVMValueRef gc_obj, cmp_gc_obj, param_values[5], func, value;
  74. LLVMTypeRef param_types[5], ret_type, func_type, func_ptr_type;
  75. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  76. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  77. LLVMBasicBlockRef init_gc_obj_fail, init_gc_obj_succ;
  78. param_types[0] = INT8_PTR_TYPE;
  79. param_types[1] = I32_TYPE;
  80. param_types[2] = INT8_TYPE;
  81. param_types[3] = INT8_PTR_TYPE;
  82. param_types[4] = I32_TYPE;
  83. ret_type = GC_REF_TYPE;
  84. if (comp_ctx->is_jit_mode)
  85. GET_AOT_FUNCTION(llvm_jit_create_func_obj, 5);
  86. else
  87. GET_AOT_FUNCTION(aot_create_func_obj, 5);
  88. /* Call function llvm_jit/aot_create_func_obj() */
  89. param_values[0] = func_ctx->aot_inst;
  90. param_values[1] = func_idx;
  91. param_values[2] = I8_CONST(1);
  92. param_values[3] = I8_PTR_NULL;
  93. param_values[4] = I32_ZERO;
  94. if (!(gc_obj = LLVMBuildCall2(comp_ctx->builder, func_type, func,
  95. param_values, 5, "call"))) {
  96. aot_set_last_error("llvm build call failed.");
  97. return false;
  98. }
  99. BUILD_ISNOTNULL(gc_obj, cmp_gc_obj, "gc_obj_not_null");
  100. ADD_BASIC_BLOCK(init_gc_obj_fail, "init_gc_obj_fail");
  101. ADD_BASIC_BLOCK(init_gc_obj_succ, "init_gc_obj_success");
  102. LLVMMoveBasicBlockAfter(init_gc_obj_fail, block_curr);
  103. LLVMMoveBasicBlockAfter(init_gc_obj_succ, block_curr);
  104. if (!LLVMBuildCondBr(comp_ctx->builder, cmp_gc_obj, init_gc_obj_succ,
  105. init_gc_obj_fail)) {
  106. aot_set_last_error("llvm build cond br failed.");
  107. goto fail;
  108. }
  109. /* If init gc_obj failed, return this function
  110. so the runtime can catch the exception */
  111. LLVMPositionBuilderAtEnd(comp_ctx->builder, init_gc_obj_fail);
  112. if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
  113. goto fail;
  114. }
  115. LLVMPositionBuilderAtEnd(comp_ctx->builder, init_gc_obj_succ);
  116. *p_gc_obj = gc_obj;
  117. return true;
  118. fail:
  119. return false;
  120. }
  121. bool
  122. aot_call_aot_obj_is_instance_of(AOTCompContext *comp_ctx,
  123. AOTFuncContext *func_ctx, LLVMValueRef gc_obj,
  124. LLVMValueRef heap_type, LLVMValueRef *castable)
  125. {
  126. LLVMValueRef param_values[3], func, value, res;
  127. LLVMTypeRef param_types[3], ret_type, func_type, func_ptr_type;
  128. param_types[0] = INT8_PTR_TYPE;
  129. param_types[1] = GC_REF_TYPE;
  130. param_types[2] = I32_TYPE;
  131. ret_type = INT8_TYPE;
  132. if (comp_ctx->is_jit_mode)
  133. GET_AOT_FUNCTION(llvm_jit_obj_is_instance_of, 3);
  134. else
  135. GET_AOT_FUNCTION(aot_obj_is_instance_of, 3);
  136. /* Call function aot_obj_is_instance_of() or llvm_jit_obj_is_instance_of()
  137. */
  138. param_values[0] = func_ctx->aot_inst;
  139. param_values[1] = gc_obj;
  140. param_values[2] = heap_type;
  141. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func, param_values,
  142. 3, "call"))) {
  143. aot_set_last_error("llvm build call failed.");
  144. goto fail;
  145. }
  146. *castable = res;
  147. return true;
  148. fail:
  149. return false;
  150. }
  151. bool
  152. aot_call_wasm_obj_is_type_of(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  153. LLVMValueRef gc_obj, LLVMValueRef heap_type,
  154. LLVMValueRef *castable)
  155. {
  156. LLVMValueRef param_values[2], func, value, res;
  157. LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
  158. param_types[0] = GC_REF_TYPE;
  159. param_types[1] = I32_TYPE;
  160. ret_type = INT8_TYPE;
  161. GET_AOT_FUNCTION(wasm_obj_is_type_of, 2);
  162. /* Call function wasm_obj_is_type_of() */
  163. param_values[0] = gc_obj;
  164. param_values[1] = heap_type;
  165. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func, param_values,
  166. 2, "call"))) {
  167. aot_set_last_error("llvm build call failed.");
  168. goto fail;
  169. }
  170. *castable = res;
  171. return true;
  172. fail:
  173. return false;
  174. }
  175. bool
  176. aot_call_aot_rtt_type_new(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  177. LLVMValueRef type_index, LLVMValueRef *rtt_type)
  178. {
  179. LLVMValueRef param_values[2], func, value, res;
  180. LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
  181. param_types[0] = INT8_PTR_TYPE;
  182. param_types[1] = I32_TYPE;
  183. ret_type = GC_REF_TYPE;
  184. if (comp_ctx->is_jit_mode)
  185. GET_AOT_FUNCTION(llvm_jit_rtt_type_new, 2);
  186. else
  187. GET_AOT_FUNCTION(aot_rtt_type_new, 2);
  188. /* Call function llvm_jit/aot_rtt_type_new() */
  189. param_values[0] = func_ctx->aot_inst;
  190. param_values[1] = type_index;
  191. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func, param_values,
  192. 2, "call"))) {
  193. aot_set_last_error("llvm build call failed.");
  194. goto fail;
  195. }
  196. *rtt_type = res;
  197. return true;
  198. fail:
  199. return false;
  200. }
  201. bool
  202. aot_compile_op_ref_as_non_null(AOTCompContext *comp_ctx,
  203. AOTFuncContext *func_ctx)
  204. {
  205. LLVMValueRef gc_obj, cmp_gc_obj;
  206. LLVMBasicBlockRef check_gc_obj_succ;
  207. GET_GC_REF_FROM_STACK(gc_obj);
  208. /* Check if gc object is NULL */
  209. BUILD_ISNULL(gc_obj, cmp_gc_obj, "cmp_gc_obj");
  210. ADD_BASIC_BLOCK(check_gc_obj_succ, "check_gc_obj_succ");
  211. MOVE_BLOCK_AFTER_CURR(check_gc_obj_succ);
  212. /* Throw exception if it is NULL */
  213. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_NULL_REFERENCE, true,
  214. cmp_gc_obj, check_gc_obj_succ))
  215. goto fail;
  216. return true;
  217. fail:
  218. return false;
  219. }
  220. static bool
  221. aot_call_wasm_struct_obj_new(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  222. LLVMValueRef rtt_type, LLVMValueRef *struct_obj)
  223. {
  224. LLVMValueRef param_values[2], func, value, res;
  225. LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
  226. param_types[0] = INT8_PTR_TYPE;
  227. param_types[1] = INT8_PTR_TYPE;
  228. ret_type = GC_REF_TYPE;
  229. GET_AOT_FUNCTION(wasm_struct_obj_new, 2);
  230. /* Call function wasm_struct_obj_new() */
  231. param_values[0] = func_ctx->exec_env;
  232. param_values[1] = rtt_type;
  233. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func, param_values,
  234. 2, "call"))) {
  235. aot_set_last_error("llvm build call failed.");
  236. goto fail;
  237. }
  238. *struct_obj = res;
  239. return true;
  240. fail:
  241. return false;
  242. }
  243. static void
  244. get_struct_field_data_types(const AOTCompContext *comp_ctx, uint8 field_type,
  245. LLVMTypeRef *p_field_data_type,
  246. LLVMTypeRef *p_field_data_ptr_type,
  247. bool *p_trunc_or_extend)
  248. {
  249. LLVMTypeRef field_data_type = NULL, field_data_ptr_type = NULL;
  250. bool trunc_or_extend = false;
  251. if (wasm_is_type_reftype(field_type)) {
  252. field_data_type = GC_REF_TYPE;
  253. field_data_ptr_type = GC_REF_PTR_TYPE;
  254. }
  255. else {
  256. switch (field_type) {
  257. case VALUE_TYPE_I32:
  258. field_data_type = I32_TYPE;
  259. field_data_ptr_type = INT32_PTR_TYPE;
  260. break;
  261. case VALUE_TYPE_I64:
  262. field_data_type = I64_TYPE;
  263. field_data_ptr_type = INT64_PTR_TYPE;
  264. break;
  265. case VALUE_TYPE_F32:
  266. field_data_type = F32_TYPE;
  267. field_data_ptr_type = F32_PTR_TYPE;
  268. break;
  269. case VALUE_TYPE_F64:
  270. field_data_type = F64_TYPE;
  271. field_data_ptr_type = F64_PTR_TYPE;
  272. break;
  273. case PACKED_TYPE_I8:
  274. field_data_type = INT8_TYPE;
  275. field_data_ptr_type = INT8_PTR_TYPE;
  276. trunc_or_extend = true;
  277. break;
  278. case PACKED_TYPE_I16:
  279. field_data_type = INT16_TYPE;
  280. field_data_ptr_type = INT16_PTR_TYPE;
  281. trunc_or_extend = true;
  282. break;
  283. default:
  284. bh_assert(0);
  285. break;
  286. }
  287. }
  288. *p_field_data_type = field_data_type;
  289. *p_field_data_ptr_type = field_data_ptr_type;
  290. *p_trunc_or_extend = trunc_or_extend;
  291. }
  292. static bool
  293. aot_struct_obj_set_field(AOTCompContext *comp_ctx, LLVMValueRef struct_obj,
  294. LLVMValueRef field_offset, LLVMValueRef field_value,
  295. uint8 field_type)
  296. {
  297. bool trunc = false;
  298. LLVMValueRef field_data_ptr, res;
  299. LLVMTypeRef field_data_type = NULL, field_data_ptr_type = NULL;
  300. get_struct_field_data_types(comp_ctx, field_type, &field_data_type,
  301. &field_data_ptr_type, &trunc);
  302. /* Truncate field_value if necessary */
  303. if (trunc) {
  304. if (!(field_value =
  305. LLVMBuildTrunc(comp_ctx->builder, field_value,
  306. field_data_type, "field_value_trunc"))) {
  307. aot_set_last_error("llvm build trunc failed.");
  308. goto fail;
  309. }
  310. }
  311. if (!(struct_obj = LLVMBuildBitCast(comp_ctx->builder, struct_obj,
  312. INT8_PTR_TYPE, "struct_obj_i8p"))) {
  313. aot_set_last_error("llvm build bitcast failed.");
  314. goto fail;
  315. }
  316. /* Build field data ptr and store the value */
  317. if (!(field_data_ptr =
  318. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, struct_obj,
  319. &field_offset, 1, "field_data_i8p"))) {
  320. aot_set_last_error("llvm build gep failed.");
  321. goto fail;
  322. }
  323. /* Cast to the field data type ptr */
  324. if (!(field_data_ptr =
  325. LLVMBuildBitCast(comp_ctx->builder, field_data_ptr,
  326. field_data_ptr_type, "field_value_ptr"))) {
  327. aot_set_last_error("llvm build bitcast failed.");
  328. goto fail;
  329. }
  330. if (!(res =
  331. LLVMBuildStore(comp_ctx->builder, field_value, field_data_ptr))) {
  332. aot_set_last_error("llvm build store failed.");
  333. goto fail;
  334. }
  335. if (!is_target_x86(comp_ctx)
  336. && (field_data_type == I64_TYPE || field_data_type == F64_TYPE
  337. || field_data_type == GC_REF_TYPE)) {
  338. LLVMSetAlignment(res, 4);
  339. }
  340. return true;
  341. fail:
  342. return false;
  343. }
  344. static bool
  345. aot_struct_obj_get_field(AOTCompContext *comp_ctx, LLVMValueRef struct_obj,
  346. LLVMValueRef field_offset, LLVMValueRef *p_field_value,
  347. uint8 field_type, bool sign_extend)
  348. {
  349. bool extend = false;
  350. LLVMValueRef field_value, field_data_ptr;
  351. LLVMTypeRef field_data_type = NULL, field_data_ptr_type = NULL;
  352. get_struct_field_data_types(comp_ctx, field_type, &field_data_type,
  353. &field_data_ptr_type, &extend);
  354. if (!(struct_obj = LLVMBuildBitCast(comp_ctx->builder, struct_obj,
  355. INT8_PTR_TYPE, "struct_obj_i8p"))) {
  356. aot_set_last_error("llvm build bitcast failed.");
  357. goto fail;
  358. }
  359. if (!(field_data_ptr =
  360. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, struct_obj,
  361. &field_offset, 1, "field_data_i8p"))) {
  362. aot_set_last_error("llvm build gep failed.");
  363. goto fail;
  364. }
  365. if (!(field_data_ptr =
  366. LLVMBuildBitCast(comp_ctx->builder, field_data_ptr,
  367. field_data_ptr_type, "field_value_ptr"))) {
  368. aot_set_last_error("llvm build bitcast failed.");
  369. goto fail;
  370. }
  371. if (!(field_value = LLVMBuildLoad2(comp_ctx->builder, field_data_type,
  372. field_data_ptr, "field_value"))) {
  373. aot_set_last_error("llvm build load failed.");
  374. goto fail;
  375. }
  376. if (!is_target_x86(comp_ctx)
  377. && (field_data_type == I64_TYPE || field_data_type == F64_TYPE
  378. || field_data_type == GC_REF_TYPE)) {
  379. LLVMSetAlignment(field_value, 4);
  380. }
  381. if (extend) {
  382. if (sign_extend) {
  383. if (!(field_value = LLVMBuildSExt(comp_ctx->builder, field_value,
  384. I32_TYPE, "field_value_sext"))) {
  385. aot_set_last_error("llvm build signed ext failed.");
  386. goto fail;
  387. }
  388. }
  389. else {
  390. if (!(field_value = LLVMBuildZExt(comp_ctx->builder, field_value,
  391. I32_TYPE, "field_value_zext"))) {
  392. aot_set_last_error("llvm build unsigned ext failed.");
  393. goto fail;
  394. }
  395. }
  396. }
  397. *p_field_value = field_value;
  398. return true;
  399. fail:
  400. return false;
  401. }
  402. static bool
  403. struct_new_canon_init_fields(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  404. uint32 type_index, LLVMValueRef struct_obj)
  405. {
  406. LLVMValueRef field_value = NULL;
  407. /* Used in compile time, to distinguish what type of AOTValue POP,
  408. * field_data offset, size */
  409. WASMStructType *compile_time_struct_type =
  410. (WASMStructType *)comp_ctx->comp_data->types[type_index];
  411. WASMStructFieldType *fields = compile_time_struct_type->fields;
  412. int32 field_count = (int32)compile_time_struct_type->field_count;
  413. int32 field_idx;
  414. uint32 field_offset;
  415. uint8 field_type;
  416. for (field_idx = field_count - 1; field_idx >= 0; field_idx--) {
  417. field_type = fields[field_idx].field_type;
  418. field_offset = comp_ctx->pointer_size == sizeof(uint64)
  419. ? fields[field_idx].field_offset_64bit
  420. : fields[field_idx].field_offset_32bit;
  421. if (wasm_is_type_reftype(field_type)) {
  422. POP_GC_REF(field_value);
  423. }
  424. else if (field_type == VALUE_TYPE_I32 || field_type == PACKED_TYPE_I8
  425. || field_type == PACKED_TYPE_I16) {
  426. POP_I32(field_value);
  427. }
  428. else if (field_type == VALUE_TYPE_I64) {
  429. POP_I64(field_value);
  430. }
  431. else if (field_type == VALUE_TYPE_F32) {
  432. POP_F32(field_value);
  433. }
  434. else if (field_type == VALUE_TYPE_F64) {
  435. POP_F64(field_value);
  436. }
  437. else {
  438. bh_assert(0);
  439. }
  440. if (!aot_struct_obj_set_field(comp_ctx, struct_obj,
  441. I32_CONST(field_offset), field_value,
  442. field_type))
  443. goto fail;
  444. }
  445. return true;
  446. fail:
  447. return false;
  448. }
  449. bool
  450. aot_compile_op_struct_new(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  451. uint32 type_index, bool init_with_default)
  452. {
  453. LLVMValueRef rtt_type, struct_obj, cmp;
  454. LLVMBasicBlockRef check_rtt_type_succ, check_struct_obj_succ;
  455. if (!aot_gen_commit_values(comp_ctx->aot_frame))
  456. return false;
  457. if (!aot_gen_commit_sp_ip(comp_ctx->aot_frame, true, true))
  458. return false;
  459. /* Generate call wasm_rtt_type_new and check for exception */
  460. if (!aot_call_aot_rtt_type_new(comp_ctx, func_ctx, I32_CONST(type_index),
  461. &rtt_type))
  462. goto fail;
  463. ADD_BASIC_BLOCK(check_rtt_type_succ, "check rtt type succ");
  464. MOVE_BLOCK_AFTER_CURR(check_rtt_type_succ);
  465. BUILD_ISNULL(rtt_type, cmp, "cmp_rtt_type");
  466. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_FAILED_TO_CREATE_RTT_TYPE,
  467. true, cmp, check_rtt_type_succ))
  468. goto fail;
  469. /* Generate call wasm_struct_obj_new and check for exception */
  470. if (!aot_call_wasm_struct_obj_new(comp_ctx, func_ctx, rtt_type,
  471. &struct_obj))
  472. goto fail;
  473. ADD_BASIC_BLOCK(check_struct_obj_succ, "check struct obj succ");
  474. MOVE_BLOCK_AFTER(check_struct_obj_succ, check_rtt_type_succ);
  475. BUILD_ISNULL(struct_obj, cmp, "cmp_struct_obj");
  476. if (!aot_emit_exception(comp_ctx, func_ctx,
  477. EXCE_FAILED_TO_CREATE_STRUCT_OBJ, true, cmp,
  478. check_struct_obj_succ))
  479. goto fail;
  480. SET_BUILDER_POS(check_struct_obj_succ);
  481. /* For WASM_OP_STRUCT_NEW, init field with poped value */
  482. if (!init_with_default
  483. && !struct_new_canon_init_fields(comp_ctx, func_ctx, type_index,
  484. struct_obj)) {
  485. goto fail;
  486. }
  487. PUSH_GC_REF(struct_obj);
  488. return true;
  489. fail:
  490. return false;
  491. }
  492. bool
  493. aot_compile_op_struct_get(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  494. uint32 type_index, uint32 field_idx, bool sign)
  495. {
  496. LLVMValueRef struct_obj, cmp, field_value;
  497. LLVMBasicBlockRef check_struct_obj_succ;
  498. /* Used in compile time, to distinguish what type of AOTValue PUSH,
  499. * field_data offset, size */
  500. WASMStructType *compile_time_struct_type =
  501. (WASMStructType *)comp_ctx->comp_data->types[type_index];
  502. WASMStructFieldType *field;
  503. uint32 field_offset;
  504. uint8 field_type;
  505. field = compile_time_struct_type->fields + field_idx;
  506. field_type = field->field_type;
  507. field_offset = comp_ctx->pointer_size == sizeof(uint64)
  508. ? field->field_offset_64bit
  509. : field->field_offset_32bit;
  510. if (field_idx >= compile_time_struct_type->field_count) {
  511. aot_set_last_error("struct field index out of bounds");
  512. goto fail;
  513. }
  514. POP_GC_REF(struct_obj);
  515. ADD_BASIC_BLOCK(check_struct_obj_succ, "check struct obj succ");
  516. MOVE_BLOCK_AFTER_CURR(check_struct_obj_succ);
  517. BUILD_ISNULL(struct_obj, cmp, "cmp_struct_obj");
  518. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_NULL_STRUCT_OBJ, true, cmp,
  519. check_struct_obj_succ))
  520. goto fail;
  521. if (!aot_struct_obj_get_field(comp_ctx, struct_obj, I32_CONST(field_offset),
  522. &field_value, field_type, sign))
  523. goto fail;
  524. if (wasm_is_type_reftype(field_type)) {
  525. PUSH_GC_REF(field_value);
  526. }
  527. else if (field_type == VALUE_TYPE_I32 || field_type == PACKED_TYPE_I8
  528. || field_type == PACKED_TYPE_I16) {
  529. PUSH_I32(field_value);
  530. }
  531. else if (field_type == VALUE_TYPE_I64) {
  532. PUSH_I64(field_value);
  533. }
  534. else if (field_type == VALUE_TYPE_F32) {
  535. PUSH_F32(field_value);
  536. }
  537. else if (field_type == VALUE_TYPE_F64) {
  538. PUSH_F64(field_value);
  539. }
  540. else {
  541. bh_assert(0);
  542. }
  543. return true;
  544. fail:
  545. return false;
  546. }
  547. bool
  548. aot_compile_op_struct_set(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  549. uint32 type_index, uint32 field_idx)
  550. {
  551. LLVMValueRef struct_obj, cmp, field_value = NULL;
  552. LLVMBasicBlockRef check_struct_obj_succ;
  553. /* Used in compile time, to distinguish what type of AOTValue POP,
  554. * field_data offset, size */
  555. WASMStructType *compile_time_struct_type =
  556. (WASMStructType *)comp_ctx->comp_data->types[type_index];
  557. WASMStructFieldType *field;
  558. uint32 field_offset;
  559. uint8 field_type;
  560. field = compile_time_struct_type->fields + field_idx;
  561. field_type = field->field_type;
  562. field_offset = comp_ctx->pointer_size == sizeof(uint64)
  563. ? field->field_offset_64bit
  564. : field->field_offset_32bit;
  565. if (field_idx >= compile_time_struct_type->field_count) {
  566. aot_set_last_error("struct field index out of bounds");
  567. goto fail;
  568. }
  569. if (wasm_is_type_reftype(field_type)) {
  570. POP_GC_REF(field_value);
  571. }
  572. else if (field_type == VALUE_TYPE_I32 || field_type == PACKED_TYPE_I8
  573. || field_type == PACKED_TYPE_I16) {
  574. POP_I32(field_value);
  575. }
  576. else if (field_type == VALUE_TYPE_I64) {
  577. POP_I64(field_value);
  578. }
  579. else if (field_type == VALUE_TYPE_F32) {
  580. POP_F32(field_value);
  581. }
  582. else if (field_type == VALUE_TYPE_F64) {
  583. POP_F64(field_value);
  584. }
  585. else {
  586. bh_assert(0);
  587. }
  588. POP_GC_REF(struct_obj);
  589. ADD_BASIC_BLOCK(check_struct_obj_succ, "check struct obj succ");
  590. MOVE_BLOCK_AFTER_CURR(check_struct_obj_succ);
  591. BUILD_ISNULL(struct_obj, cmp, "cmp_struct_obj");
  592. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_NULL_STRUCT_OBJ, true, cmp,
  593. check_struct_obj_succ))
  594. goto fail;
  595. if (!aot_struct_obj_set_field(comp_ctx, struct_obj, I32_CONST(field_offset),
  596. field_value, field_type))
  597. goto fail;
  598. return true;
  599. fail:
  600. return false;
  601. }
  602. static bool
  603. aot_call_wasm_array_obj_new(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  604. LLVMValueRef rtt_type, LLVMValueRef array_len,
  605. LLVMValueRef array_elem, LLVMValueRef *array_obj)
  606. {
  607. LLVMValueRef param_values[4], func, value, res, array_elem_ptr;
  608. LLVMTypeRef param_types[4], ret_type, func_type, func_ptr_type;
  609. if (!(array_elem_ptr = LLVMBuildAlloca(
  610. comp_ctx->builder, LLVMTypeOf(array_elem), "array_elem_ptr"))) {
  611. aot_set_last_error("llvm build alloca failed.");
  612. goto fail;
  613. }
  614. if (!LLVMBuildStore(comp_ctx->builder, array_elem, array_elem_ptr)) {
  615. aot_set_last_error("llvm build store failed.");
  616. goto fail;
  617. }
  618. if (!(array_elem_ptr = LLVMBuildBitCast(comp_ctx->builder, array_elem_ptr,
  619. INT8_PTR_TYPE, "array_elem_ptr"))) {
  620. aot_set_last_error("llvm build bitcast failed.");
  621. goto fail;
  622. }
  623. param_types[0] = INT8_PTR_TYPE;
  624. param_types[1] = INT8_PTR_TYPE;
  625. param_types[2] = I32_TYPE;
  626. param_types[3] = INT8_PTR_TYPE;
  627. ret_type = GC_REF_TYPE;
  628. GET_AOT_FUNCTION(wasm_array_obj_new, 4);
  629. /* Call function wasm_array_obj_new() */
  630. param_values[0] = func_ctx->exec_env;
  631. param_values[1] = rtt_type;
  632. param_values[2] = array_len;
  633. param_values[3] = array_elem_ptr;
  634. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func, param_values,
  635. 4, "call"))) {
  636. aot_set_last_error("llvm build call failed.");
  637. goto fail;
  638. }
  639. *array_obj = res;
  640. return true;
  641. fail:
  642. return false;
  643. }
  644. static uint32
  645. aot_array_obj_elem_size_log(AOTCompContext *comp_ctx, uint8 array_elem_type)
  646. {
  647. uint32 elem_size_log = 0;
  648. if (wasm_is_type_reftype(array_elem_type)) {
  649. elem_size_log = comp_ctx->pointer_size == sizeof(uint32) ? 2 : 3;
  650. }
  651. else if (array_elem_type == PACKED_TYPE_I8) {
  652. elem_size_log = 0;
  653. }
  654. else if (array_elem_type == PACKED_TYPE_I16) {
  655. elem_size_log = 1;
  656. }
  657. else if (array_elem_type == VALUE_TYPE_I32
  658. || array_elem_type == VALUE_TYPE_F32) {
  659. elem_size_log = 2;
  660. }
  661. else if (array_elem_type == VALUE_TYPE_I64
  662. || array_elem_type == VALUE_TYPE_F64) {
  663. elem_size_log = 3;
  664. }
  665. else {
  666. bh_assert(0);
  667. }
  668. return elem_size_log;
  669. }
  670. /* array_obj->elem_data + (elem_idx << elem_size_log) */
  671. bool
  672. aot_array_obj_elem_addr(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  673. LLVMValueRef array_obj, LLVMValueRef elem_idx,
  674. LLVMValueRef *p_elem_data, uint8 array_elem_type)
  675. {
  676. uint32 elem_size_log = 0;
  677. LLVMValueRef start_offset, elem_offset, elem_data;
  678. elem_size_log = aot_array_obj_elem_size_log(comp_ctx, array_elem_type);
  679. /* Get the elem data start offset of the WASMArrayObject, the offset may be
  680. * different in 32-bit runtime and 64-bit runtime since WASMObjectHeader
  681. * is uintptr_t. Use comp_ctx->pointer_size + 4(uint32 for length) as the
  682. * offsetof(WASMArrayObject, length)*/
  683. if (!(start_offset = I32_CONST(comp_ctx->pointer_size + sizeof(uint32)))) {
  684. aot_set_last_error("llvm build const failed.");
  685. goto fail;
  686. }
  687. if (!(elem_offset =
  688. LLVMBuildShl(comp_ctx->builder, elem_idx,
  689. I32_CONST(elem_size_log), "elem_offset"))) {
  690. aot_set_last_error("llvm build shl failed.");
  691. goto fail;
  692. }
  693. if (!(elem_offset = LLVMBuildAdd(comp_ctx->builder, start_offset,
  694. elem_offset, "total_offset"))) {
  695. aot_set_last_error("llvm build add failed.");
  696. goto fail;
  697. }
  698. if (!(elem_data = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  699. array_obj, &elem_offset, 1,
  700. "array_obj_elem_data_i8p"))) {
  701. aot_set_last_error("llvm build gep failed.");
  702. goto fail;
  703. }
  704. *p_elem_data = elem_data;
  705. return true;
  706. fail:
  707. return false;
  708. }
  709. static bool
  710. aot_array_obj_set_elem(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  711. LLVMValueRef array_obj, LLVMValueRef elem_idx,
  712. LLVMValueRef array_elem, uint8 array_elem_type)
  713. {
  714. bool trunc = false;
  715. LLVMValueRef elem_data_ptr, res;
  716. LLVMTypeRef elem_data_type = NULL, elem_data_ptr_type = NULL;
  717. if (!aot_array_obj_elem_addr(comp_ctx, func_ctx, array_obj, elem_idx,
  718. &elem_data_ptr, array_elem_type))
  719. goto fail;
  720. if (wasm_is_type_reftype(array_elem_type)) {
  721. elem_data_type = GC_REF_TYPE;
  722. elem_data_ptr_type = GC_REF_PTR_TYPE;
  723. }
  724. else
  725. switch (array_elem_type) {
  726. case PACKED_TYPE_I8:
  727. elem_data_type = INT8_TYPE;
  728. elem_data_ptr_type = INT8_PTR_TYPE;
  729. trunc = true;
  730. break;
  731. case PACKED_TYPE_I16:
  732. elem_data_type = INT16_TYPE;
  733. elem_data_ptr_type = INT16_PTR_TYPE;
  734. trunc = true;
  735. break;
  736. case VALUE_TYPE_I32:
  737. elem_data_type = I32_TYPE;
  738. elem_data_ptr_type = INT32_PTR_TYPE;
  739. break;
  740. case VALUE_TYPE_I64:
  741. elem_data_type = I64_TYPE;
  742. elem_data_ptr_type = INT64_PTR_TYPE;
  743. break;
  744. case VALUE_TYPE_F32:
  745. elem_data_type = F32_TYPE;
  746. elem_data_ptr_type = F32_PTR_TYPE;
  747. break;
  748. case VALUE_TYPE_F64:
  749. elem_data_type = F64_TYPE;
  750. elem_data_ptr_type = F64_PTR_TYPE;
  751. break;
  752. default:
  753. bh_assert(0);
  754. break;
  755. }
  756. /* Based on elem_size, trunc array_elem if necessary */
  757. if (trunc) {
  758. if (!(array_elem =
  759. LLVMBuildTrunc(comp_ctx->builder, array_elem, elem_data_type,
  760. "array_elem_trunc"))) {
  761. aot_set_last_error("llvm build trunc failed.");
  762. goto fail;
  763. }
  764. }
  765. /* Cast to the field data type ptr */
  766. if (!(elem_data_ptr =
  767. LLVMBuildBitCast(comp_ctx->builder, elem_data_ptr,
  768. elem_data_ptr_type, "elem_data_ptr"))) {
  769. aot_set_last_error("llvm build bitcast failed.");
  770. goto fail;
  771. }
  772. if (!(res = LLVMBuildStore(comp_ctx->builder, array_elem, elem_data_ptr))) {
  773. aot_set_last_error("llvm build store failed.");
  774. goto fail;
  775. }
  776. if (!is_target_x86(comp_ctx)
  777. && (elem_data_type == I64_TYPE || elem_data_type == F64_TYPE
  778. || elem_data_type == GC_REF_TYPE)) {
  779. LLVMSetAlignment(res, 4);
  780. }
  781. return true;
  782. fail:
  783. return false;
  784. }
  785. static bool
  786. aot_call_aot_array_init_with_data(
  787. AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, LLVMValueRef seg_index,
  788. LLVMValueRef data_seg_offset, LLVMValueRef array_obj,
  789. LLVMValueRef elem_size, LLVMValueRef array_len)
  790. {
  791. LLVMValueRef param_values[6], func, value, res, cmp;
  792. LLVMTypeRef param_types[6], ret_type, func_type, func_ptr_type;
  793. LLVMBasicBlockRef init_success;
  794. ADD_BASIC_BLOCK(init_success, "init success");
  795. MOVE_BLOCK_AFTER_CURR(init_success);
  796. param_types[0] = INT8_PTR_TYPE;
  797. param_types[1] = I32_TYPE;
  798. param_types[2] = I32_TYPE;
  799. param_types[3] = INT8_PTR_TYPE;
  800. param_types[4] = I32_TYPE;
  801. param_types[5] = I32_TYPE;
  802. ret_type = INT8_TYPE;
  803. if (comp_ctx->is_jit_mode)
  804. GET_AOT_FUNCTION(llvm_array_init_with_data, 6);
  805. else
  806. GET_AOT_FUNCTION(aot_array_init_with_data, 6);
  807. /* Call function aot_array_init_with_data() */
  808. param_values[0] = func_ctx->aot_inst;
  809. param_values[1] = seg_index;
  810. param_values[2] = data_seg_offset;
  811. param_values[3] = array_obj;
  812. param_values[4] = elem_size;
  813. param_values[5] = array_len;
  814. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func, param_values,
  815. 6, "call"))) {
  816. aot_set_last_error("llvm build call failed.");
  817. goto fail;
  818. }
  819. BUILD_ICMP(LLVMIntEQ, res, I8_ZERO, cmp, "array_init_ret");
  820. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_ARRAY_IDX_OOB, true, cmp,
  821. init_success))
  822. goto fail;
  823. return true;
  824. fail:
  825. return false;
  826. }
  827. static bool
  828. aot_call_wasm_array_get_elem(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  829. LLVMValueRef array_obj, LLVMValueRef elem_idx,
  830. LLVMValueRef *p_array_elem, uint8 array_elem_type,
  831. bool sign)
  832. {
  833. bool extend = false;
  834. LLVMValueRef elem_data_ptr, array_elem;
  835. LLVMTypeRef elem_data_type = NULL, elem_data_ptr_type = NULL;
  836. if (!aot_array_obj_elem_addr(comp_ctx, func_ctx, array_obj, elem_idx,
  837. &elem_data_ptr, array_elem_type))
  838. goto fail;
  839. if (wasm_is_type_reftype(array_elem_type)) {
  840. elem_data_type = GC_REF_TYPE;
  841. elem_data_ptr_type = GC_REF_PTR_TYPE;
  842. }
  843. else
  844. switch (array_elem_type) {
  845. case PACKED_TYPE_I8:
  846. elem_data_type = INT8_TYPE;
  847. elem_data_ptr_type = INT8_PTR_TYPE;
  848. extend = true;
  849. break;
  850. case PACKED_TYPE_I16:
  851. elem_data_type = INT16_TYPE;
  852. elem_data_ptr_type = INT16_PTR_TYPE;
  853. extend = true;
  854. break;
  855. case VALUE_TYPE_I32:
  856. elem_data_type = I32_TYPE;
  857. elem_data_ptr_type = INT32_PTR_TYPE;
  858. break;
  859. case VALUE_TYPE_I64:
  860. elem_data_type = I64_TYPE;
  861. elem_data_ptr_type = INT64_PTR_TYPE;
  862. break;
  863. case VALUE_TYPE_F32:
  864. elem_data_type = F32_TYPE;
  865. elem_data_ptr_type = F32_PTR_TYPE;
  866. break;
  867. case VALUE_TYPE_F64:
  868. elem_data_type = F64_TYPE;
  869. elem_data_ptr_type = F64_PTR_TYPE;
  870. break;
  871. default:
  872. bh_assert(0);
  873. break;
  874. }
  875. /* Based on elem_size, trunc array_elem if necessary */
  876. if (!(elem_data_ptr =
  877. LLVMBuildBitCast(comp_ctx->builder, elem_data_ptr,
  878. elem_data_ptr_type, "elem_data_ptr"))) {
  879. aot_set_last_error("llvm build bitcast failed.");
  880. goto fail;
  881. }
  882. if (!(array_elem = LLVMBuildLoad2(comp_ctx->builder, elem_data_type,
  883. elem_data_ptr, "array_elem"))) {
  884. aot_set_last_error("llvm build load failed.");
  885. goto fail;
  886. }
  887. if (!is_target_x86(comp_ctx)
  888. && (elem_data_type == I64_TYPE || elem_data_type == F64_TYPE
  889. || elem_data_type == GC_REF_TYPE)) {
  890. LLVMSetAlignment(array_elem, 4);
  891. }
  892. if (extend) {
  893. if (sign) {
  894. if (!(array_elem = LLVMBuildSExt(comp_ctx->builder, array_elem,
  895. I32_TYPE, "array_elem_sext"))) {
  896. aot_set_last_error("llvm build signed ext failed.");
  897. goto fail;
  898. }
  899. }
  900. else {
  901. if (!(array_elem = LLVMBuildZExt(comp_ctx->builder, array_elem,
  902. I32_TYPE, "array_elem_zext"))) {
  903. aot_set_last_error("llvm build unsigned ext failed.");
  904. goto fail;
  905. }
  906. }
  907. }
  908. *p_array_elem = array_elem;
  909. return true;
  910. fail:
  911. return false;
  912. }
  913. /* array_obj->length >> WASM_ARRAY_LENGTH_SHIFT */
  914. bool
  915. aot_array_obj_length(AOTCompContext *comp_ctx, LLVMValueRef array_obj,
  916. LLVMValueRef *p_array_len)
  917. {
  918. LLVMValueRef offset, array_len;
  919. /* Get the length of the WASMArrayObject, the offset may be
  920. * different in 32-bit runtime and 64-bit runtime since WASMObjectHeader
  921. * is uintptr_t. Use comp_ctx->pointer_size as the
  922. * offsetof(WASMArrayObject, length)*/
  923. if (!(offset = I32_CONST(comp_ctx->pointer_size))) {
  924. aot_set_last_error("llvm build const failed.");
  925. goto fail;
  926. }
  927. if (!(array_len =
  928. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, array_obj,
  929. &offset, 1, "array_obj_length_i8p"))) {
  930. aot_set_last_error("llvm build gep failed.");
  931. goto fail;
  932. }
  933. if (!(array_len =
  934. LLVMBuildBitCast(comp_ctx->builder, array_len, INT32_PTR_TYPE,
  935. "array_obj_length_i32ptr"))) {
  936. aot_set_last_error("llvm build bitcast failed.");
  937. goto fail;
  938. }
  939. if (!(array_len = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, array_len,
  940. "array_obj_length"))) {
  941. aot_set_last_error("llvm build load failed.");
  942. goto fail;
  943. }
  944. if (!(array_len = LLVMBuildLShr(comp_ctx->builder, array_len,
  945. I32_CONST(WASM_ARRAY_LENGTH_SHIFT),
  946. "array_obj_length_shr"))) {
  947. aot_set_last_error("llvm build lshr failed.");
  948. goto fail;
  949. }
  950. *p_array_len = array_len;
  951. return true;
  952. fail:
  953. return false;
  954. }
  955. bool
  956. aot_compile_op_array_new(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  957. uint32 type_index, bool init_with_default,
  958. bool fixed_size, uint32 array_len)
  959. {
  960. LLVMValueRef array_length, array_elem = NULL, array_obj;
  961. LLVMValueRef rtt_type, cmp, elem_idx;
  962. LLVMBasicBlockRef check_rtt_type_succ, check_array_obj_succ;
  963. /* Use for distinguish what type of AOTValue POP */
  964. WASMArrayType *compile_time_array_type =
  965. (WASMArrayType *)comp_ctx->comp_data->types[type_index];
  966. uint8 array_elem_type = compile_time_array_type->elem_type;
  967. uint32 i;
  968. if (!aot_gen_commit_values(comp_ctx->aot_frame))
  969. return false;
  970. if (!aot_gen_commit_sp_ip(comp_ctx->aot_frame, true, true))
  971. return false;
  972. /* Generate call aot_rtt_type_new and check for exception */
  973. if (!aot_call_aot_rtt_type_new(comp_ctx, func_ctx, I32_CONST(type_index),
  974. &rtt_type))
  975. goto fail;
  976. ADD_BASIC_BLOCK(check_rtt_type_succ, "check rtt type succ");
  977. MOVE_BLOCK_AFTER_CURR(check_rtt_type_succ);
  978. BUILD_ISNULL(rtt_type, cmp, "cmp_rtt_type");
  979. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_FAILED_TO_CREATE_RTT_TYPE,
  980. true, cmp, check_rtt_type_succ))
  981. goto fail;
  982. if (!fixed_size)
  983. POP_I32(array_length);
  984. else
  985. array_length = I32_CONST(array_len);
  986. /* For WASM_OP_ARRAY_NEW */
  987. if (!fixed_size && !init_with_default) {
  988. if (wasm_is_type_reftype(array_elem_type)) {
  989. POP_GC_REF(array_elem);
  990. }
  991. else if (array_elem_type == VALUE_TYPE_I32
  992. || array_elem_type == PACKED_TYPE_I8
  993. || array_elem_type == PACKED_TYPE_I16) {
  994. POP_I32(array_elem);
  995. }
  996. else if (array_elem_type == VALUE_TYPE_I64) {
  997. POP_I64(array_elem);
  998. }
  999. else if (array_elem_type == VALUE_TYPE_F32) {
  1000. POP_F32(array_elem);
  1001. }
  1002. else if (array_elem_type == VALUE_TYPE_F64) {
  1003. POP_F64(array_elem);
  1004. }
  1005. else {
  1006. bh_assert(0);
  1007. }
  1008. }
  1009. else {
  1010. /* I64 will alloca large enough space for all union access includes
  1011. * array_elem.gc_ob, i32, i64 to be interpreted as 0*/
  1012. array_elem = I64_ZERO;
  1013. }
  1014. /* Generate call wasm_array_obj_new and check for exception */
  1015. if (!aot_call_wasm_array_obj_new(comp_ctx, func_ctx, rtt_type, array_length,
  1016. array_elem, &array_obj))
  1017. goto fail;
  1018. ADD_BASIC_BLOCK(check_array_obj_succ, "check array obj succ");
  1019. MOVE_BLOCK_AFTER(check_array_obj_succ, check_rtt_type_succ);
  1020. BUILD_ISNULL(array_obj, cmp, "cmp_array_obj");
  1021. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_FAILED_TO_CREATE_ARRAY_OBJ,
  1022. true, cmp, check_array_obj_succ))
  1023. goto fail;
  1024. if (fixed_size) {
  1025. for (i = 0; i < array_len; i++) {
  1026. if (wasm_is_type_reftype(array_elem_type)) {
  1027. POP_GC_REF(array_elem);
  1028. }
  1029. else if (array_elem_type == VALUE_TYPE_I32
  1030. || array_elem_type == PACKED_TYPE_I8
  1031. || array_elem_type == PACKED_TYPE_I16) {
  1032. POP_I32(array_elem);
  1033. }
  1034. else if (array_elem_type == VALUE_TYPE_I64) {
  1035. POP_I64(array_elem);
  1036. }
  1037. else if (array_elem_type == VALUE_TYPE_F32) {
  1038. POP_F32(array_elem);
  1039. }
  1040. else if (array_elem_type == VALUE_TYPE_F64) {
  1041. POP_F64(array_elem);
  1042. }
  1043. else {
  1044. bh_assert(0);
  1045. }
  1046. /* array_len - 1 - i */
  1047. if (!(elem_idx = LLVMBuildSub(comp_ctx->builder, array_length,
  1048. I32_CONST(i + 1), "elem_idx"))) {
  1049. aot_set_last_error("llvm build sub failed.");
  1050. goto fail;
  1051. }
  1052. if (!aot_array_obj_set_elem(comp_ctx, func_ctx, array_obj, elem_idx,
  1053. array_elem, array_elem_type))
  1054. goto fail;
  1055. }
  1056. }
  1057. PUSH_GC_REF(array_obj);
  1058. return true;
  1059. fail:
  1060. return false;
  1061. }
  1062. bool
  1063. aot_compile_op_array_new_data(AOTCompContext *comp_ctx,
  1064. AOTFuncContext *func_ctx, uint32 type_index,
  1065. uint32 data_seg_index)
  1066. {
  1067. LLVMValueRef array_length, data_seg_offset, rtt_type,
  1068. elem_size = NULL, array_elem, array_obj, cmp;
  1069. LLVMBasicBlockRef check_rtt_type_succ, check_array_obj_succ;
  1070. /* Use for distinguish what type of element in array */
  1071. WASMArrayType *compile_time_array_type =
  1072. (WASMArrayType *)comp_ctx->comp_data->types[type_index];
  1073. uint8 array_elem_type = compile_time_array_type->elem_type;
  1074. if (!aot_gen_commit_values(comp_ctx->aot_frame))
  1075. return false;
  1076. if (!aot_gen_commit_sp_ip(comp_ctx->aot_frame, true, true))
  1077. return false;
  1078. /* Generate call aot_rtt_type_new and check for exception */
  1079. if (!aot_call_aot_rtt_type_new(comp_ctx, func_ctx, I32_CONST(type_index),
  1080. &rtt_type))
  1081. goto fail;
  1082. ADD_BASIC_BLOCK(check_rtt_type_succ, "check rtt type succ");
  1083. MOVE_BLOCK_AFTER_CURR(check_rtt_type_succ);
  1084. BUILD_ISNULL(rtt_type, cmp, "cmp_rtt_type");
  1085. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_FAILED_TO_CREATE_RTT_TYPE,
  1086. true, cmp, check_rtt_type_succ))
  1087. goto fail;
  1088. POP_I32(array_length);
  1089. POP_I32(data_seg_offset);
  1090. switch (array_elem_type) {
  1091. case PACKED_TYPE_I8:
  1092. elem_size = I32_ONE;
  1093. break;
  1094. case PACKED_TYPE_I16:
  1095. elem_size = I32_TWO;
  1096. break;
  1097. case VALUE_TYPE_I32:
  1098. case VALUE_TYPE_F32:
  1099. elem_size = I32_FOUR;
  1100. break;
  1101. case VALUE_TYPE_I64:
  1102. case VALUE_TYPE_F64:
  1103. elem_size = I32_EIGHT;
  1104. break;
  1105. default:
  1106. bh_assert(0);
  1107. }
  1108. if (elem_size == I32_EIGHT)
  1109. array_elem = I64_ZERO;
  1110. else
  1111. array_elem = I32_ZERO;
  1112. /* Generate call wasm_array_obj_new and check for exception */
  1113. if (!aot_call_wasm_array_obj_new(comp_ctx, func_ctx, rtt_type, array_length,
  1114. array_elem, &array_obj))
  1115. goto fail;
  1116. ADD_BASIC_BLOCK(check_array_obj_succ, "check array obj succ");
  1117. MOVE_BLOCK_AFTER(check_array_obj_succ, check_rtt_type_succ);
  1118. BUILD_ISNULL(array_obj, cmp, "cmp_array_obj");
  1119. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_FAILED_TO_CREATE_ARRAY_OBJ,
  1120. true, cmp, check_array_obj_succ))
  1121. goto fail;
  1122. if (!aot_call_aot_array_init_with_data(
  1123. comp_ctx, func_ctx, I32_CONST(data_seg_index), data_seg_offset,
  1124. array_obj, elem_size, array_length))
  1125. goto fail;
  1126. PUSH_GC_REF(array_obj);
  1127. return true;
  1128. fail:
  1129. return false;
  1130. }
  1131. bool
  1132. aot_compile_op_array_get(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1133. uint32 type_index, bool sign)
  1134. {
  1135. LLVMValueRef elem_idx, array_obj, cmp, array_len, array_elem;
  1136. LLVMBasicBlockRef check_array_obj_succ, check_boundary_succ;
  1137. /* Use for distinguish what type of AOTValue PUSH */
  1138. WASMArrayType *compile_time_array_type =
  1139. (WASMArrayType *)comp_ctx->comp_data->types[type_index];
  1140. uint8 array_elem_type = compile_time_array_type->elem_type;
  1141. POP_I32(elem_idx);
  1142. POP_GC_REF(array_obj);
  1143. ADD_BASIC_BLOCK(check_array_obj_succ, "check array obj succ");
  1144. MOVE_BLOCK_AFTER_CURR(check_array_obj_succ);
  1145. BUILD_ISNULL(array_obj, cmp, "cmp_array_obj");
  1146. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_NULL_ARRAY_OBJ, true, cmp,
  1147. check_array_obj_succ))
  1148. goto fail;
  1149. SET_BUILDER_POS(check_array_obj_succ);
  1150. if (!aot_array_obj_length(comp_ctx, array_obj, &array_len))
  1151. goto fail;
  1152. ADD_BASIC_BLOCK(check_boundary_succ, "check boundary succ");
  1153. MOVE_BLOCK_AFTER(check_boundary_succ, check_array_obj_succ);
  1154. BUILD_ICMP(LLVMIntUGE, elem_idx, array_len, cmp, "cmp_array_obj");
  1155. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_ARRAY_IDX_OOB, true, cmp,
  1156. check_boundary_succ))
  1157. goto fail;
  1158. SET_BUILDER_POS(check_boundary_succ);
  1159. if (!aot_call_wasm_array_get_elem(comp_ctx, func_ctx, array_obj, elem_idx,
  1160. &array_elem, array_elem_type, sign))
  1161. goto fail;
  1162. if (wasm_is_type_reftype(array_elem_type)) {
  1163. PUSH_GC_REF(array_elem);
  1164. }
  1165. else if (array_elem_type == VALUE_TYPE_I32
  1166. || array_elem_type == PACKED_TYPE_I8
  1167. || array_elem_type == PACKED_TYPE_I16) {
  1168. PUSH_I32(array_elem);
  1169. }
  1170. else if (array_elem_type == VALUE_TYPE_I64) {
  1171. PUSH_I64(array_elem);
  1172. }
  1173. else if (array_elem_type == VALUE_TYPE_F32) {
  1174. PUSH_F32(array_elem);
  1175. }
  1176. else if (array_elem_type == VALUE_TYPE_F64) {
  1177. PUSH_F64(array_elem);
  1178. }
  1179. else {
  1180. bh_assert(0);
  1181. }
  1182. return true;
  1183. fail:
  1184. return false;
  1185. }
  1186. bool
  1187. aot_compile_op_array_set(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1188. uint32 type_index)
  1189. {
  1190. LLVMValueRef elem_idx, array_obj, cmp, array_len, array_elem = NULL;
  1191. LLVMBasicBlockRef check_array_obj_succ, check_boundary_succ;
  1192. /* Use for distinguish what type of AOTValue POP */
  1193. WASMArrayType *compile_time_array_type =
  1194. (WASMArrayType *)comp_ctx->comp_data->types[type_index];
  1195. uint8 array_elem_type = compile_time_array_type->elem_type;
  1196. /* Get LLVM type based on array_elem_type */
  1197. if (wasm_is_type_reftype(array_elem_type)) {
  1198. POP_GC_REF(array_elem);
  1199. }
  1200. else if (array_elem_type == VALUE_TYPE_I32
  1201. || array_elem_type == PACKED_TYPE_I8
  1202. || array_elem_type == PACKED_TYPE_I16) {
  1203. POP_I32(array_elem);
  1204. }
  1205. else if (array_elem_type == VALUE_TYPE_I64) {
  1206. POP_I64(array_elem);
  1207. }
  1208. else if (array_elem_type == VALUE_TYPE_F32) {
  1209. POP_F32(array_elem);
  1210. }
  1211. else if (array_elem_type == VALUE_TYPE_F64) {
  1212. POP_F64(array_elem);
  1213. }
  1214. else {
  1215. bh_assert(0);
  1216. }
  1217. POP_I32(elem_idx);
  1218. POP_GC_REF(array_obj);
  1219. ADD_BASIC_BLOCK(check_array_obj_succ, "check array obj succ");
  1220. MOVE_BLOCK_AFTER_CURR(check_array_obj_succ);
  1221. BUILD_ISNULL(array_obj, cmp, "cmp_array_obj");
  1222. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_NULL_ARRAY_OBJ, true, cmp,
  1223. check_array_obj_succ))
  1224. goto fail;
  1225. SET_BUILDER_POS(check_array_obj_succ);
  1226. if (!aot_array_obj_length(comp_ctx, array_obj, &array_len))
  1227. goto fail;
  1228. ADD_BASIC_BLOCK(check_boundary_succ, "check boundary succ");
  1229. MOVE_BLOCK_AFTER(check_boundary_succ, check_array_obj_succ);
  1230. BUILD_ICMP(LLVMIntUGE, elem_idx, array_len, cmp, "cmp_array_obj");
  1231. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_ARRAY_IDX_OOB, true, cmp,
  1232. check_boundary_succ))
  1233. goto fail;
  1234. SET_BUILDER_POS(check_boundary_succ);
  1235. if (!aot_array_obj_set_elem(comp_ctx, func_ctx, array_obj, elem_idx,
  1236. array_elem, array_elem_type)) {
  1237. aot_set_last_error("llvm build alloca failed.");
  1238. goto fail;
  1239. }
  1240. return true;
  1241. fail:
  1242. return false;
  1243. }
  1244. bool
  1245. aot_compile_op_array_fill(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1246. uint32 type_index)
  1247. {
  1248. LLVMValueRef len, array_obj, fill_value = NULL, offset, array_len, cmp[2],
  1249. boundary, loop_counter_addr, loop_counter_val;
  1250. LLVMBasicBlockRef check_obj_succ, len_gt_zero, len_le_zero, inner_else;
  1251. LLVMBasicBlockRef fill_loop_header, fill_loop_body;
  1252. WASMArrayType *compile_time_array_type =
  1253. (WASMArrayType *)comp_ctx->comp_data->types[type_index];
  1254. uint8 array_elem_type = compile_time_array_type->elem_type;
  1255. POP_I32(len);
  1256. /* Get LLVM type based on array_elem_type */
  1257. if (wasm_is_type_reftype(array_elem_type)) {
  1258. POP_GC_REF(fill_value);
  1259. }
  1260. else if (array_elem_type == VALUE_TYPE_I32
  1261. || array_elem_type == PACKED_TYPE_I8
  1262. || array_elem_type == PACKED_TYPE_I16) {
  1263. POP_I32(fill_value);
  1264. }
  1265. else if (array_elem_type == VALUE_TYPE_I64) {
  1266. POP_I64(fill_value);
  1267. }
  1268. else if (array_elem_type == VALUE_TYPE_F32) {
  1269. POP_F32(fill_value);
  1270. }
  1271. else if (array_elem_type == VALUE_TYPE_F64) {
  1272. POP_F64(fill_value);
  1273. }
  1274. else {
  1275. bh_assert(0);
  1276. }
  1277. POP_I32(offset);
  1278. POP_GC_REF(array_obj);
  1279. ADD_BASIC_BLOCK(check_obj_succ, "check array objs succ");
  1280. MOVE_BLOCK_AFTER_CURR(check_obj_succ);
  1281. BUILD_ISNULL(array_obj, cmp[0], "cmp_obj");
  1282. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_NULL_ARRAY_OBJ, true,
  1283. cmp[0], check_obj_succ))
  1284. goto fail;
  1285. /* Create if block */
  1286. ADD_BASIC_BLOCK(len_gt_zero, "len_gt_zero");
  1287. MOVE_BLOCK_AFTER_CURR(len_gt_zero);
  1288. /* Create inner else block */
  1289. ADD_BASIC_BLOCK(inner_else, "inner_else");
  1290. MOVE_BLOCK_AFTER(inner_else, len_gt_zero);
  1291. /* Create fill_loop_header block */
  1292. ADD_BASIC_BLOCK(fill_loop_header, "fill_loop_header");
  1293. MOVE_BLOCK_AFTER(fill_loop_header, len_gt_zero);
  1294. /* Create fill_loop_body block */
  1295. ADD_BASIC_BLOCK(fill_loop_body, "fill_loop_body");
  1296. MOVE_BLOCK_AFTER(fill_loop_body, len_gt_zero);
  1297. /* Create else(end) block */
  1298. ADD_BASIC_BLOCK(len_le_zero, "len_le_zero");
  1299. MOVE_BLOCK_AFTER(len_le_zero, len_gt_zero);
  1300. BUILD_ICMP(LLVMIntSGT, len, I32_ZERO, cmp[0], "cmp_len");
  1301. BUILD_COND_BR(cmp[0], len_gt_zero, len_le_zero);
  1302. /* Move builder to len > 0 block */
  1303. SET_BUILDER_POS(len_gt_zero);
  1304. /* dst_offset > UINT32_MAX - len */
  1305. if (!(boundary = LLVMBuildAdd(comp_ctx->builder, offset, len, ""))) {
  1306. aot_set_last_error("llvm build failed.");
  1307. goto fail;
  1308. }
  1309. BUILD_ICMP(LLVMIntUGT, boundary, I32_CONST(UINT32_MAX), cmp[0],
  1310. "boundary_check1");
  1311. /* dst_offset + len > wasm_array_obj_length(dst_obj) */
  1312. if (!aot_array_obj_length(comp_ctx, array_obj, &array_len))
  1313. goto fail;
  1314. BUILD_ICMP(LLVMIntUGT, boundary, array_len, cmp[1], "boundary_check2");
  1315. if (!(cmp[0] = LLVMBuildOr(comp_ctx->builder, cmp[0], cmp[1], ""))) {
  1316. aot_set_last_error("llvm build failed.");
  1317. goto fail;
  1318. }
  1319. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_ARRAY_IDX_OOB, true,
  1320. cmp[0], inner_else))
  1321. goto fail;
  1322. if (!(loop_counter_addr = LLVMBuildAlloca(comp_ctx->builder, I32_TYPE,
  1323. "fill_loop_counter"))) {
  1324. aot_set_last_error("llvm build alloc failed.");
  1325. goto fail;
  1326. }
  1327. if (!is_target_x86(comp_ctx)) {
  1328. LLVMSetAlignment(loop_counter_addr, 4);
  1329. }
  1330. if (!LLVMBuildStore(comp_ctx->builder, offset, loop_counter_addr)) {
  1331. aot_set_last_error("llvm build store failed.");
  1332. goto fail;
  1333. }
  1334. BUILD_BR(fill_loop_header);
  1335. SET_BUILDER_POS(fill_loop_header);
  1336. if (!(loop_counter_val =
  1337. LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, loop_counter_addr,
  1338. "fill_loop_counter"))) {
  1339. aot_set_last_error("llvm build load failed.");
  1340. goto fail;
  1341. }
  1342. BUILD_ICMP(LLVMIntULT, loop_counter_val, boundary, cmp[0],
  1343. "cmp_loop_counter");
  1344. BUILD_COND_BR(cmp[0], fill_loop_body, len_le_zero);
  1345. SET_BUILDER_POS(fill_loop_body);
  1346. if (!aot_array_obj_set_elem(comp_ctx, func_ctx, array_obj, loop_counter_val,
  1347. fill_value, array_elem_type))
  1348. goto fail;
  1349. if (!(loop_counter_val = LLVMBuildAdd(comp_ctx->builder, loop_counter_val,
  1350. I32_ONE, "fill_loop_counter"))) {
  1351. aot_set_last_error("llvm build add failed.");
  1352. goto fail;
  1353. }
  1354. if (!LLVMBuildStore(comp_ctx->builder, loop_counter_val,
  1355. loop_counter_addr)) {
  1356. aot_set_last_error("llvm build store failed.");
  1357. goto fail;
  1358. }
  1359. BUILD_BR(fill_loop_header);
  1360. SET_BUILDER_POS(len_le_zero);
  1361. return true;
  1362. fail:
  1363. return false;
  1364. }
  1365. static bool
  1366. aot_call_wasm_array_obj_copy(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1367. LLVMValueRef dst_obj, LLVMValueRef dst_offset,
  1368. LLVMValueRef src_obj, LLVMValueRef src_offset,
  1369. LLVMValueRef len)
  1370. {
  1371. LLVMValueRef param_values[5], func, value;
  1372. LLVMTypeRef param_types[5], ret_type, func_type, func_ptr_type;
  1373. param_types[0] = GC_REF_TYPE;
  1374. param_types[1] = I32_TYPE;
  1375. param_types[2] = GC_REF_TYPE;
  1376. param_types[3] = I32_TYPE;
  1377. param_types[4] = I32_TYPE;
  1378. ret_type = VOID_TYPE;
  1379. GET_AOT_FUNCTION(wasm_array_obj_copy, 5);
  1380. /* Call function wasm_array_obj_copy() */
  1381. param_values[0] = dst_obj;
  1382. param_values[1] = dst_offset;
  1383. param_values[2] = src_obj;
  1384. param_values[3] = src_offset;
  1385. param_values[4] = len;
  1386. if (!LLVMBuildCall2(comp_ctx->builder, func_type, func, param_values, 5,
  1387. "")) {
  1388. aot_set_last_error("llvm build call failed.");
  1389. goto fail;
  1390. }
  1391. return true;
  1392. fail:
  1393. return false;
  1394. }
  1395. bool
  1396. aot_compile_op_array_copy(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1397. uint32 type_index, uint32 src_type_index)
  1398. {
  1399. LLVMValueRef len, src_offset, src_obj, dst_offset, dst_obj, array_len,
  1400. cmp[4], boundary;
  1401. LLVMBasicBlockRef check_objs_succ, len_gt_zero, len_le_zero, inner_else;
  1402. int i;
  1403. POP_I32(len);
  1404. POP_I32(src_offset);
  1405. POP_GC_REF(src_obj);
  1406. POP_I32(dst_offset);
  1407. POP_GC_REF(dst_obj);
  1408. ADD_BASIC_BLOCK(check_objs_succ, "check array objs succ");
  1409. MOVE_BLOCK_AFTER_CURR(check_objs_succ);
  1410. BUILD_ISNULL(src_obj, cmp[0], "cmp_src_obj");
  1411. BUILD_ISNULL(dst_obj, cmp[1], "cmp_dst_obj");
  1412. /* src_obj is null or dst_obj is null, throw exception */
  1413. if (!(cmp[0] = LLVMBuildOr(comp_ctx->builder, cmp[0], cmp[1], ""))) {
  1414. aot_set_last_error("llvm build or failed.");
  1415. goto fail;
  1416. }
  1417. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_NULL_ARRAY_OBJ, true,
  1418. cmp[0], check_objs_succ))
  1419. goto fail;
  1420. /* Create if block */
  1421. ADD_BASIC_BLOCK(len_gt_zero, "len_gt_zero");
  1422. MOVE_BLOCK_AFTER_CURR(len_gt_zero);
  1423. /* Create else(end) block */
  1424. ADD_BASIC_BLOCK(len_le_zero, "len_le_zero");
  1425. MOVE_BLOCK_AFTER(len_le_zero, len_gt_zero);
  1426. /* Create inner else block */
  1427. ADD_BASIC_BLOCK(inner_else, "inner_else");
  1428. MOVE_BLOCK_AFTER(inner_else, len_gt_zero);
  1429. BUILD_ICMP(LLVMIntSGT, len, I32_ZERO, cmp[0], "cmp_len");
  1430. BUILD_COND_BR(cmp[0], len_gt_zero, len_le_zero);
  1431. /* Move builder to len > 0 block */
  1432. SET_BUILDER_POS(len_gt_zero);
  1433. /* dst_offset > UINT32_MAX - len */
  1434. if (!(boundary = LLVMBuildAdd(comp_ctx->builder, dst_offset, len, ""))) {
  1435. aot_set_last_error("llvm build failed.");
  1436. goto fail;
  1437. }
  1438. BUILD_ICMP(LLVMIntUGT, boundary, I32_CONST(UINT32_MAX), cmp[0],
  1439. "boundary_check1");
  1440. /* dst_offset + len > wasm_array_obj_length(dst_obj) */
  1441. if (!aot_array_obj_length(comp_ctx, dst_obj, &array_len))
  1442. goto fail;
  1443. BUILD_ICMP(LLVMIntUGT, boundary, array_len, cmp[1], "boundary_check2");
  1444. /* src_offset > UINT32_MAX - len */
  1445. if (!(boundary = LLVMBuildAdd(comp_ctx->builder, src_offset, len, ""))) {
  1446. aot_set_last_error("llvm build failed.");
  1447. goto fail;
  1448. }
  1449. BUILD_ICMP(LLVMIntUGT, boundary, I32_CONST(UINT32_MAX), cmp[2],
  1450. "boundary_check3");
  1451. /* src_offset + len > wasm_array_obj_length(src_obj) */
  1452. if (!aot_array_obj_length(comp_ctx, src_obj, &array_len))
  1453. goto fail;
  1454. BUILD_ICMP(LLVMIntUGT, boundary, array_len, cmp[3], "boundary_check4");
  1455. /* logical or above 4 boundary checks */
  1456. for (i = 1; i < 4; ++i) {
  1457. if (!(cmp[0] = LLVMBuildOr(comp_ctx->builder, cmp[0], cmp[i], ""))) {
  1458. aot_set_last_error("llvm build failed.");
  1459. goto fail;
  1460. }
  1461. }
  1462. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_ARRAY_IDX_OOB, true,
  1463. cmp[0], inner_else))
  1464. goto fail;
  1465. if (!aot_call_wasm_array_obj_copy(comp_ctx, func_ctx, dst_obj, dst_offset,
  1466. src_obj, src_offset, len))
  1467. goto fail;
  1468. BUILD_BR(len_le_zero);
  1469. SET_BUILDER_POS(len_le_zero);
  1470. return true;
  1471. fail:
  1472. return false;
  1473. }
  1474. bool
  1475. aot_compile_op_array_len(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  1476. {
  1477. LLVMValueRef array_obj, cmp, array_len;
  1478. LLVMBasicBlockRef check_array_obj_succ;
  1479. POP_GC_REF(array_obj);
  1480. ADD_BASIC_BLOCK(check_array_obj_succ, "check array obj succ");
  1481. MOVE_BLOCK_AFTER_CURR(check_array_obj_succ);
  1482. BUILD_ISNULL(array_obj, cmp, "cmp_array_obj");
  1483. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_NULL_ARRAY_OBJ, true, cmp,
  1484. check_array_obj_succ))
  1485. goto fail;
  1486. if (!aot_array_obj_length(comp_ctx, array_obj, &array_len))
  1487. goto fail;
  1488. PUSH_I32(array_len);
  1489. return true;
  1490. fail:
  1491. return false;
  1492. }
  1493. bool
  1494. aot_compile_op_i31_new(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  1495. {
  1496. LLVMValueRef i31_val, i31_obj;
  1497. POP_I32(i31_val);
  1498. /* i31_val <<= 1 */
  1499. if (!(i31_val = LLVMBuildShl(comp_ctx->builder, i31_val, I32_ONE,
  1500. "i31_val_shl"))) {
  1501. aot_set_last_error("llvm build shl failed.");
  1502. goto fail;
  1503. }
  1504. /* i31_val |= 1 */
  1505. if (!(i31_val =
  1506. LLVMBuildOr(comp_ctx->builder, i31_val, I32_ONE, "i31_val_or"))) {
  1507. aot_set_last_error("llvm build or failed.");
  1508. goto fail;
  1509. }
  1510. if (!(i31_obj = LLVMBuildIntToPtr(comp_ctx->builder, i31_val, GC_REF_TYPE,
  1511. "i31_obj"))) {
  1512. aot_set_last_error("llvm build bit cast failed.");
  1513. goto fail;
  1514. }
  1515. PUSH_GC_REF(i31_obj);
  1516. return true;
  1517. fail:
  1518. return false;
  1519. }
  1520. bool
  1521. aot_compile_op_i31_get(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1522. bool sign)
  1523. {
  1524. LLVMValueRef i31_obj, i31_val, cmp_i31_obj;
  1525. LLVMBasicBlockRef check_i31_obj_succ;
  1526. POP_GC_REF(i31_obj);
  1527. ADD_BASIC_BLOCK(check_i31_obj_succ, "check_i31_obj_succ");
  1528. MOVE_BLOCK_AFTER_CURR(check_i31_obj_succ);
  1529. /* Check if i31 object is NULL, throw exception if it is */
  1530. BUILD_ISNULL(i31_obj, cmp_i31_obj, "cmp_i31_obj");
  1531. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_NULL_I31_OBJ, true,
  1532. cmp_i31_obj, check_i31_obj_succ)) {
  1533. goto fail;
  1534. }
  1535. if (!(i31_val = LLVMBuildPtrToInt(comp_ctx->builder, i31_obj, I32_TYPE,
  1536. "i31_val"))) {
  1537. aot_set_last_error("llvm build ptr to init failed.");
  1538. goto fail;
  1539. }
  1540. if (!sign) {
  1541. if (!(i31_val = LLVMBuildLShr(comp_ctx->builder, i31_val, I32_ONE,
  1542. "i31_value"))) {
  1543. aot_set_last_error("llvm build lshr failed.");
  1544. goto fail;
  1545. }
  1546. }
  1547. else {
  1548. if (!(i31_val = LLVMBuildAShr(comp_ctx->builder, i31_val, I32_ONE,
  1549. "i31_value"))) {
  1550. aot_set_last_error("llvm build ashr failed.");
  1551. goto fail;
  1552. }
  1553. }
  1554. PUSH_I32(i31_val);
  1555. return true;
  1556. fail:
  1557. return false;
  1558. }
  1559. bool
  1560. aot_compile_op_ref_test(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1561. int32 heap_type, bool nullable)
  1562. {
  1563. LLVMValueRef gc_obj, ref_test_phi, cmp, castable;
  1564. LLVMBasicBlockRef block_curr, block_obj_non_null, block_end;
  1565. POP_GC_REF(gc_obj);
  1566. block_curr = CURR_BLOCK();
  1567. /* Create non-null object block */
  1568. ADD_BASIC_BLOCK(block_obj_non_null, "non_null_obj");
  1569. MOVE_BLOCK_AFTER_CURR(block_obj_non_null);
  1570. /* Create end block */
  1571. ADD_BASIC_BLOCK(block_end, "ref_test_end");
  1572. MOVE_BLOCK_AFTER(block_end, block_obj_non_null);
  1573. /* Create ref test result phi */
  1574. SET_BUILDER_POS(block_end);
  1575. if (!(ref_test_phi =
  1576. LLVMBuildPhi(comp_ctx->builder, INT1_TYPE, "ref_test_res"))) {
  1577. aot_set_last_error("llvm build phi failed");
  1578. return false;
  1579. }
  1580. /* Check if gc object is NULL */
  1581. SET_BUILDER_POS(block_curr);
  1582. BUILD_ISNULL(gc_obj, cmp, "cmp_gc_obj");
  1583. BUILD_COND_BR(cmp, block_end, block_obj_non_null);
  1584. if (nullable)
  1585. LLVMAddIncoming(ref_test_phi, &I1_ONE, &block_curr, 1);
  1586. else
  1587. LLVMAddIncoming(ref_test_phi, &I1_ZERO, &block_curr, 1);
  1588. /* Move builder to non-null object block */
  1589. SET_BUILDER_POS(block_obj_non_null);
  1590. if (heap_type >= 0) {
  1591. if (!aot_call_aot_obj_is_instance_of(comp_ctx, func_ctx, gc_obj,
  1592. I32_CONST(heap_type), &castable))
  1593. return false;
  1594. }
  1595. else {
  1596. if (!aot_call_wasm_obj_is_type_of(comp_ctx, func_ctx, gc_obj,
  1597. I32_CONST(heap_type), &castable))
  1598. return false;
  1599. }
  1600. if (!(castable = LLVMBuildICmp(comp_ctx->builder, LLVMIntNE, castable,
  1601. I8_ZERO, "castable"))) {
  1602. aot_set_last_error("llvm build icmp failed.");
  1603. return false;
  1604. }
  1605. BUILD_BR(block_end);
  1606. LLVMAddIncoming(ref_test_phi, &castable, &block_obj_non_null, 1);
  1607. SET_BUILDER_POS(block_end);
  1608. PUSH_COND(ref_test_phi);
  1609. return true;
  1610. fail:
  1611. return false;
  1612. }
  1613. bool
  1614. aot_compile_op_ref_cast(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1615. int32 heap_type, bool nullable)
  1616. {
  1617. LLVMValueRef gc_obj, cmp, castable;
  1618. LLVMBasicBlockRef block_obj_non_null, block_end;
  1619. GET_GC_REF_FROM_STACK(gc_obj);
  1620. /* Create non null block */
  1621. ADD_BASIC_BLOCK(block_obj_non_null, "non_null_obj");
  1622. MOVE_BLOCK_AFTER_CURR(block_obj_non_null);
  1623. /* Create end block */
  1624. ADD_BASIC_BLOCK(block_end, "ref_cast_end");
  1625. MOVE_BLOCK_AFTER(block_end, block_obj_non_null);
  1626. BUILD_ISNULL(gc_obj, cmp, "obj_is_null");
  1627. if (nullable) {
  1628. BUILD_COND_BR(cmp, block_end, block_obj_non_null);
  1629. }
  1630. else {
  1631. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_CAST_FAILURE, true,
  1632. cmp, block_obj_non_null)) {
  1633. return false;
  1634. }
  1635. }
  1636. SET_BUILDER_POS(block_obj_non_null);
  1637. if (heap_type >= 0) {
  1638. if (!aot_call_aot_obj_is_instance_of(comp_ctx, func_ctx, gc_obj,
  1639. I32_CONST(heap_type), &castable))
  1640. return false;
  1641. }
  1642. else {
  1643. if (!aot_call_wasm_obj_is_type_of(comp_ctx, func_ctx, gc_obj,
  1644. I32_CONST(heap_type), &castable))
  1645. return false;
  1646. }
  1647. if (!(cmp = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ, castable, I8_ZERO,
  1648. "is_uncastable"))) {
  1649. aot_set_last_error("llvm build not failed");
  1650. return false;
  1651. }
  1652. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_CAST_FAILURE, true, cmp,
  1653. block_end)) {
  1654. return false;
  1655. }
  1656. SET_BUILDER_POS(block_end);
  1657. return true;
  1658. fail:
  1659. return false;
  1660. }
  1661. static bool
  1662. aot_call_wasm_externref_obj_to_internal_obj(AOTCompContext *comp_ctx,
  1663. AOTFuncContext *func_ctx,
  1664. LLVMValueRef externref_obj,
  1665. LLVMValueRef *gc_obj)
  1666. {
  1667. LLVMValueRef param_values[1], func, value, res;
  1668. LLVMTypeRef param_types[1], ret_type, func_type, func_ptr_type;
  1669. param_types[0] = GC_REF_TYPE;
  1670. ret_type = GC_REF_TYPE;
  1671. GET_AOT_FUNCTION(wasm_externref_obj_to_internal_obj, 1);
  1672. /* Call function wasm_externref_obj_to_internal_obj */
  1673. param_values[0] = externref_obj;
  1674. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func, param_values,
  1675. 1, "call"))) {
  1676. aot_set_last_error("llvm build call failed.");
  1677. goto fail;
  1678. }
  1679. *gc_obj = res;
  1680. return true;
  1681. fail:
  1682. return false;
  1683. }
  1684. bool
  1685. aot_compile_op_extern_internalize(AOTCompContext *comp_ctx,
  1686. AOTFuncContext *func_ctx)
  1687. {
  1688. LLVMValueRef externref_obj, gc_obj, cmp, internal_obj_phi;
  1689. LLVMBasicBlockRef block_curr, block_obj_non_null, block_end;
  1690. POP_GC_REF(externref_obj);
  1691. block_curr = CURR_BLOCK();
  1692. /* Create non-null object block */
  1693. ADD_BASIC_BLOCK(block_obj_non_null, "non_null_obj");
  1694. MOVE_BLOCK_AFTER_CURR(block_obj_non_null);
  1695. /* Create end block */
  1696. ADD_BASIC_BLOCK(block_end, "internalize_end");
  1697. MOVE_BLOCK_AFTER(block_end, block_obj_non_null);
  1698. /* Create internalized object phi */
  1699. SET_BUILDER_POS(block_end);
  1700. if (!(internal_obj_phi =
  1701. LLVMBuildPhi(comp_ctx->builder, GC_REF_TYPE, "internal_obj"))) {
  1702. aot_set_last_error("llvm build phi failed");
  1703. return false;
  1704. }
  1705. /* Check if externref object is NULL */
  1706. SET_BUILDER_POS(block_curr);
  1707. BUILD_ISNULL(externref_obj, cmp, "cmp_externref_obj");
  1708. BUILD_COND_BR(cmp, block_end, block_obj_non_null);
  1709. LLVMAddIncoming(internal_obj_phi, &GC_REF_NULL, &block_curr, 1);
  1710. /* Move builder to non-null object block */
  1711. SET_BUILDER_POS(block_obj_non_null);
  1712. if (!aot_call_wasm_externref_obj_to_internal_obj(comp_ctx, func_ctx,
  1713. externref_obj, &gc_obj)) {
  1714. return false;
  1715. }
  1716. BUILD_BR(block_end);
  1717. LLVMAddIncoming(internal_obj_phi, &gc_obj, &block_obj_non_null, 1);
  1718. /* Move builder to end block */
  1719. SET_BUILDER_POS(block_end);
  1720. PUSH_GC_REF(internal_obj_phi);
  1721. return true;
  1722. fail:
  1723. return false;
  1724. }
  1725. static bool
  1726. aot_call_wasm_internal_obj_to_external_obj(AOTCompContext *comp_ctx,
  1727. AOTFuncContext *func_ctx,
  1728. LLVMValueRef gc_obj,
  1729. LLVMValueRef *externref_obj)
  1730. {
  1731. LLVMValueRef param_values[2], func, value, res;
  1732. LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
  1733. param_types[0] = INT8_PTR_TYPE;
  1734. param_types[1] = GC_REF_TYPE;
  1735. ret_type = GC_REF_TYPE;
  1736. GET_AOT_FUNCTION(wasm_internal_obj_to_externref_obj, 2);
  1737. /* Call function wasm_internal_obj_to_externref_obj() */
  1738. param_values[0] = func_ctx->exec_env;
  1739. param_values[1] = gc_obj;
  1740. if (!(res = LLVMBuildCall2(comp_ctx->builder, func_type, func, param_values,
  1741. 2, "call"))) {
  1742. aot_set_last_error("llvm build call failed.");
  1743. goto fail;
  1744. }
  1745. *externref_obj = res;
  1746. return true;
  1747. fail:
  1748. return false;
  1749. }
  1750. bool
  1751. aot_compile_op_extern_externalize(AOTCompContext *comp_ctx,
  1752. AOTFuncContext *func_ctx)
  1753. {
  1754. LLVMValueRef gc_obj, cmp, external_obj_phi, externref_obj;
  1755. LLVMBasicBlockRef block_curr, block_obj_non_null, block_end;
  1756. if (!aot_gen_commit_values(comp_ctx->aot_frame))
  1757. return false;
  1758. if (!aot_gen_commit_sp_ip(comp_ctx->aot_frame, true, true))
  1759. return false;
  1760. POP_GC_REF(gc_obj);
  1761. block_curr = CURR_BLOCK();
  1762. /* Create non-null object block */
  1763. ADD_BASIC_BLOCK(block_obj_non_null, "non_null_obj");
  1764. MOVE_BLOCK_AFTER_CURR(block_obj_non_null);
  1765. /* Create end block */
  1766. ADD_BASIC_BLOCK(block_end, "externalize_end");
  1767. MOVE_BLOCK_AFTER(block_end, block_obj_non_null);
  1768. /* Create externalized object phi */
  1769. SET_BUILDER_POS(block_end);
  1770. if (!(external_obj_phi =
  1771. LLVMBuildPhi(comp_ctx->builder, GC_REF_TYPE, "external_obj"))) {
  1772. aot_set_last_error("llvm build phi failed");
  1773. return false;
  1774. }
  1775. /* Check if gc object is NULL */
  1776. SET_BUILDER_POS(block_curr);
  1777. BUILD_ISNULL(gc_obj, cmp, "cmp_gc_obj");
  1778. BUILD_COND_BR(cmp, block_end, block_obj_non_null);
  1779. LLVMAddIncoming(external_obj_phi, &GC_REF_NULL, &block_curr, 1);
  1780. /* Move builder to non-null object block */
  1781. SET_BUILDER_POS(block_obj_non_null);
  1782. if (!aot_call_wasm_internal_obj_to_external_obj(comp_ctx, func_ctx, gc_obj,
  1783. &externref_obj)) {
  1784. return false;
  1785. }
  1786. /* Check whether failed to externalize */
  1787. BUILD_ISNULL(externref_obj, cmp, "cmp_externref_obj");
  1788. if (!aot_emit_exception(comp_ctx, func_ctx,
  1789. EXCE_FAILED_TO_CREATE_EXTERNREF_OBJ, true, cmp,
  1790. block_end)) {
  1791. return false;
  1792. }
  1793. LLVMAddIncoming(external_obj_phi, &externref_obj, &block_obj_non_null, 1);
  1794. /* Move builder to end block */
  1795. SET_BUILDER_POS(block_end);
  1796. PUSH_GC_REF(external_obj_phi);
  1797. return true;
  1798. fail:
  1799. return false;
  1800. }
  1801. #endif /* end of WASM_ENABLE_GC != 0 */