aot_emit_memory.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  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_memory.h"
  6. #include "aot_emit_exception.h"
  7. #include "../aot/aot_runtime.h"
  8. #include "aot_intrinsic.h"
  9. #define BUILD_ICMP(op, left, right, res, name) \
  10. do { \
  11. if (!(res = \
  12. LLVMBuildICmp(comp_ctx->builder, op, left, right, name))) { \
  13. aot_set_last_error("llvm build icmp failed."); \
  14. goto fail; \
  15. } \
  16. } while (0)
  17. #define BUILD_OP(Op, left, right, res, name) \
  18. do { \
  19. if (!(res = LLVMBuild##Op(comp_ctx->builder, left, right, name))) { \
  20. aot_set_last_error("llvm build " #Op " fail."); \
  21. goto fail; \
  22. } \
  23. } while (0)
  24. #define ADD_BASIC_BLOCK(block, name) \
  25. do { \
  26. if (!(block = LLVMAppendBasicBlockInContext(comp_ctx->context, \
  27. func_ctx->func, name))) { \
  28. aot_set_last_error("llvm add basic block failed."); \
  29. goto fail; \
  30. } \
  31. } while (0)
  32. #define SET_BUILD_POS(block) LLVMPositionBuilderAtEnd(comp_ctx->builder, block)
  33. static LLVMValueRef
  34. get_memory_check_bound(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  35. uint32 bytes)
  36. {
  37. LLVMValueRef mem_check_bound = NULL;
  38. switch (bytes) {
  39. case 1:
  40. mem_check_bound = func_ctx->mem_info[0].mem_bound_check_1byte;
  41. break;
  42. case 2:
  43. mem_check_bound = func_ctx->mem_info[0].mem_bound_check_2bytes;
  44. break;
  45. case 4:
  46. mem_check_bound = func_ctx->mem_info[0].mem_bound_check_4bytes;
  47. break;
  48. case 8:
  49. mem_check_bound = func_ctx->mem_info[0].mem_bound_check_8bytes;
  50. break;
  51. case 16:
  52. mem_check_bound = func_ctx->mem_info[0].mem_bound_check_16bytes;
  53. break;
  54. default:
  55. bh_assert(0);
  56. return NULL;
  57. }
  58. if (func_ctx->mem_space_unchanged)
  59. return mem_check_bound;
  60. if (!(mem_check_bound = LLVMBuildLoad(comp_ctx->builder, mem_check_bound,
  61. "mem_check_bound"))) {
  62. aot_set_last_error("llvm build load failed.");
  63. return NULL;
  64. }
  65. return mem_check_bound;
  66. }
  67. static LLVMValueRef
  68. get_memory_curr_page_count(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx);
  69. LLVMValueRef
  70. aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  71. uint32 offset, uint32 bytes)
  72. {
  73. LLVMValueRef offset_const = I32_CONST(offset);
  74. LLVMValueRef addr, maddr, offset1, cmp1, cmp2, cmp;
  75. LLVMValueRef mem_base_addr, mem_check_bound;
  76. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  77. LLVMBasicBlockRef check_succ;
  78. AOTValue *aot_value_top;
  79. uint32 local_idx_of_aot_value = 0;
  80. bool is_target_64bit, is_local_of_aot_value = false;
  81. #if WASM_ENABLE_SHARED_MEMORY != 0
  82. bool is_shared_memory =
  83. comp_ctx->comp_data->memories[0].memory_flags & 0x02;
  84. #endif
  85. is_target_64bit = (comp_ctx->pointer_size == sizeof(uint64)) ? true : false;
  86. CHECK_LLVM_CONST(offset_const);
  87. /* Get memory base address and memory data size */
  88. if (func_ctx->mem_space_unchanged
  89. #if WASM_ENABLE_SHARED_MEMORY != 0
  90. || is_shared_memory
  91. #endif
  92. ) {
  93. mem_base_addr = func_ctx->mem_info[0].mem_base_addr;
  94. }
  95. else {
  96. if (!(mem_base_addr = LLVMBuildLoad(comp_ctx->builder,
  97. func_ctx->mem_info[0].mem_base_addr,
  98. "mem_base"))) {
  99. aot_set_last_error("llvm build load failed.");
  100. goto fail;
  101. }
  102. }
  103. aot_value_top =
  104. func_ctx->block_stack.block_list_end->value_stack.value_list_end;
  105. if (aot_value_top) {
  106. /* aot_value_top is freed in the following POP_I32(addr),
  107. so save its fields here for further use */
  108. is_local_of_aot_value = aot_value_top->is_local;
  109. local_idx_of_aot_value = aot_value_top->local_idx;
  110. }
  111. POP_I32(addr);
  112. /*
  113. * Note: not throw the integer-overflow-exception here since it must
  114. * have been thrown when converting float to integer before
  115. */
  116. /* return addres directly if constant offset and inside memory space */
  117. if (LLVMIsConstant(addr) && !LLVMIsUndef(addr)
  118. #if LLVM_VERSION_NUMBER >= 12
  119. && !LLVMIsPoison(addr)
  120. #endif
  121. ) {
  122. uint64 mem_offset =
  123. (uint64)LLVMConstIntGetZExtValue(addr) + (uint64)offset;
  124. uint32 num_bytes_per_page =
  125. comp_ctx->comp_data->memories[0].num_bytes_per_page;
  126. uint32 init_page_count =
  127. comp_ctx->comp_data->memories[0].mem_init_page_count;
  128. uint64 mem_data_size = num_bytes_per_page * init_page_count;
  129. if (mem_offset + bytes <= mem_data_size) {
  130. /* inside memory space */
  131. offset1 = I32_CONST((uint32)mem_offset);
  132. CHECK_LLVM_CONST(offset1);
  133. if (!(maddr = LLVMBuildInBoundsGEP(comp_ctx->builder, mem_base_addr,
  134. &offset1, 1, "maddr"))) {
  135. aot_set_last_error("llvm build add failed.");
  136. goto fail;
  137. }
  138. return maddr;
  139. }
  140. }
  141. if (is_target_64bit) {
  142. if (!(offset_const = LLVMBuildZExt(comp_ctx->builder, offset_const,
  143. I64_TYPE, "offset_i64"))
  144. || !(addr = LLVMBuildZExt(comp_ctx->builder, addr, I64_TYPE,
  145. "addr_i64"))) {
  146. aot_set_last_error("llvm build zero extend failed.");
  147. goto fail;
  148. }
  149. }
  150. /* offset1 = offset + addr; */
  151. BUILD_OP(Add, offset_const, addr, offset1, "offset1");
  152. if (comp_ctx->enable_bound_check
  153. && !(is_local_of_aot_value
  154. && aot_checked_addr_list_find(func_ctx, local_idx_of_aot_value,
  155. offset, bytes))) {
  156. uint32 init_page_count =
  157. comp_ctx->comp_data->memories[0].mem_init_page_count;
  158. if (init_page_count == 0) {
  159. LLVMValueRef mem_size;
  160. if (!(mem_size = get_memory_curr_page_count(comp_ctx, func_ctx))) {
  161. goto fail;
  162. }
  163. BUILD_ICMP(LLVMIntEQ, mem_size, I32_ZERO, cmp, "is_zero");
  164. ADD_BASIC_BLOCK(check_succ, "check_mem_size_succ");
  165. LLVMMoveBasicBlockAfter(check_succ, block_curr);
  166. if (!aot_emit_exception(comp_ctx, func_ctx,
  167. EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp,
  168. check_succ)) {
  169. goto fail;
  170. }
  171. SET_BUILD_POS(check_succ);
  172. block_curr = check_succ;
  173. }
  174. if (!(mem_check_bound =
  175. get_memory_check_bound(comp_ctx, func_ctx, bytes))) {
  176. goto fail;
  177. }
  178. if (is_target_64bit) {
  179. BUILD_ICMP(LLVMIntUGT, offset1, mem_check_bound, cmp, "cmp");
  180. }
  181. else {
  182. /* Check integer overflow */
  183. BUILD_ICMP(LLVMIntULT, offset1, addr, cmp1, "cmp1");
  184. BUILD_ICMP(LLVMIntUGT, offset1, mem_check_bound, cmp2, "cmp2");
  185. BUILD_OP(Or, cmp1, cmp2, cmp, "cmp");
  186. }
  187. /* Add basic blocks */
  188. ADD_BASIC_BLOCK(check_succ, "check_succ");
  189. LLVMMoveBasicBlockAfter(check_succ, block_curr);
  190. if (!aot_emit_exception(comp_ctx, func_ctx,
  191. EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp,
  192. check_succ)) {
  193. goto fail;
  194. }
  195. SET_BUILD_POS(check_succ);
  196. if (is_local_of_aot_value) {
  197. if (!aot_checked_addr_list_add(func_ctx, local_idx_of_aot_value,
  198. offset, bytes))
  199. goto fail;
  200. }
  201. }
  202. /* maddr = mem_base_addr + offset1 */
  203. if (!(maddr = LLVMBuildInBoundsGEP(comp_ctx->builder, mem_base_addr,
  204. &offset1, 1, "maddr"))) {
  205. aot_set_last_error("llvm build add failed.");
  206. goto fail;
  207. }
  208. return maddr;
  209. fail:
  210. return NULL;
  211. }
  212. #define BUILD_PTR_CAST(ptr_type) \
  213. do { \
  214. if (!(maddr = LLVMBuildBitCast(comp_ctx->builder, maddr, ptr_type, \
  215. "data_ptr"))) { \
  216. aot_set_last_error("llvm build bit cast failed."); \
  217. goto fail; \
  218. } \
  219. } while (0)
  220. #define BUILD_LOAD() \
  221. do { \
  222. if (!(value = LLVMBuildLoad(comp_ctx->builder, maddr, "data"))) { \
  223. aot_set_last_error("llvm build load failed."); \
  224. goto fail; \
  225. } \
  226. LLVMSetAlignment(value, 1); \
  227. } while (0)
  228. #define BUILD_TRUNC(value, data_type) \
  229. do { \
  230. if (!(value = LLVMBuildTrunc(comp_ctx->builder, value, data_type, \
  231. "val_trunc"))) { \
  232. aot_set_last_error("llvm build trunc failed."); \
  233. goto fail; \
  234. } \
  235. } while (0)
  236. #define BUILD_STORE() \
  237. do { \
  238. LLVMValueRef res; \
  239. if (!(res = LLVMBuildStore(comp_ctx->builder, value, maddr))) { \
  240. aot_set_last_error("llvm build store failed."); \
  241. goto fail; \
  242. } \
  243. LLVMSetAlignment(res, 1); \
  244. } while (0)
  245. #define BUILD_SIGN_EXT(dst_type) \
  246. do { \
  247. if (!(value = LLVMBuildSExt(comp_ctx->builder, value, dst_type, \
  248. "data_s_ext"))) { \
  249. aot_set_last_error("llvm build sign ext failed."); \
  250. goto fail; \
  251. } \
  252. } while (0)
  253. #define BUILD_ZERO_EXT(dst_type) \
  254. do { \
  255. if (!(value = LLVMBuildZExt(comp_ctx->builder, value, dst_type, \
  256. "data_z_ext"))) { \
  257. aot_set_last_error("llvm build zero ext failed."); \
  258. goto fail; \
  259. } \
  260. } while (0)
  261. #if WASM_ENABLE_SHARED_MEMORY != 0
  262. bool
  263. check_memory_alignment(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  264. LLVMValueRef addr, uint32 align)
  265. {
  266. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  267. LLVMBasicBlockRef check_align_succ;
  268. LLVMValueRef align_mask = I32_CONST(((uint32)1 << align) - 1);
  269. LLVMValueRef res;
  270. CHECK_LLVM_CONST(align_mask);
  271. /* Convert pointer to int */
  272. if (!(addr = LLVMBuildPtrToInt(comp_ctx->builder, addr, I32_TYPE,
  273. "address"))) {
  274. aot_set_last_error("llvm build ptr to int failed.");
  275. goto fail;
  276. }
  277. /* The memory address should be aligned */
  278. BUILD_OP(And, addr, align_mask, res, "and");
  279. BUILD_ICMP(LLVMIntNE, res, I32_ZERO, res, "cmp");
  280. /* Add basic blocks */
  281. ADD_BASIC_BLOCK(check_align_succ, "check_align_succ");
  282. LLVMMoveBasicBlockAfter(check_align_succ, block_curr);
  283. if (!aot_emit_exception(comp_ctx, func_ctx, EXCE_UNALIGNED_ATOMIC, true,
  284. res, check_align_succ)) {
  285. goto fail;
  286. }
  287. SET_BUILD_POS(check_align_succ);
  288. return true;
  289. fail:
  290. return false;
  291. }
  292. #define BUILD_ATOMIC_LOAD(align) \
  293. do { \
  294. if (!(check_memory_alignment(comp_ctx, func_ctx, maddr, align))) { \
  295. goto fail; \
  296. } \
  297. if (!(value = LLVMBuildLoad(comp_ctx->builder, maddr, "data"))) { \
  298. aot_set_last_error("llvm build load failed."); \
  299. goto fail; \
  300. } \
  301. LLVMSetAlignment(value, 1 << align); \
  302. LLVMSetVolatile(value, true); \
  303. LLVMSetOrdering(value, LLVMAtomicOrderingSequentiallyConsistent); \
  304. } while (0)
  305. #define BUILD_ATOMIC_STORE(align) \
  306. do { \
  307. LLVMValueRef res; \
  308. if (!(check_memory_alignment(comp_ctx, func_ctx, maddr, align))) { \
  309. goto fail; \
  310. } \
  311. if (!(res = LLVMBuildStore(comp_ctx->builder, value, maddr))) { \
  312. aot_set_last_error("llvm build store failed."); \
  313. goto fail; \
  314. } \
  315. LLVMSetAlignment(res, 1 << align); \
  316. LLVMSetVolatile(res, true); \
  317. LLVMSetOrdering(res, LLVMAtomicOrderingSequentiallyConsistent); \
  318. } while (0)
  319. #endif
  320. bool
  321. aot_compile_op_i32_load(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  322. uint32 align, uint32 offset, uint32 bytes, bool sign,
  323. bool atomic)
  324. {
  325. LLVMValueRef maddr, value = NULL;
  326. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes)))
  327. return false;
  328. switch (bytes) {
  329. case 4:
  330. BUILD_PTR_CAST(INT32_PTR_TYPE);
  331. #if WASM_ENABLE_SHARED_MEMORY != 0
  332. if (atomic)
  333. BUILD_ATOMIC_LOAD(align);
  334. else
  335. #endif
  336. BUILD_LOAD();
  337. break;
  338. case 2:
  339. case 1:
  340. if (bytes == 2)
  341. BUILD_PTR_CAST(INT16_PTR_TYPE);
  342. else
  343. BUILD_PTR_CAST(INT8_PTR_TYPE);
  344. #if WASM_ENABLE_SHARED_MEMORY != 0
  345. if (atomic) {
  346. BUILD_ATOMIC_LOAD(align);
  347. BUILD_ZERO_EXT(I32_TYPE);
  348. }
  349. else
  350. #endif
  351. {
  352. BUILD_LOAD();
  353. if (sign)
  354. BUILD_SIGN_EXT(I32_TYPE);
  355. else
  356. BUILD_ZERO_EXT(I32_TYPE);
  357. }
  358. break;
  359. default:
  360. bh_assert(0);
  361. break;
  362. }
  363. PUSH_I32(value);
  364. return true;
  365. fail:
  366. return false;
  367. }
  368. bool
  369. aot_compile_op_i64_load(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  370. uint32 align, uint32 offset, uint32 bytes, bool sign,
  371. bool atomic)
  372. {
  373. LLVMValueRef maddr, value = NULL;
  374. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes)))
  375. return false;
  376. switch (bytes) {
  377. case 8:
  378. BUILD_PTR_CAST(INT64_PTR_TYPE);
  379. #if WASM_ENABLE_SHARED_MEMORY != 0
  380. if (atomic)
  381. BUILD_ATOMIC_LOAD(align);
  382. else
  383. #endif
  384. BUILD_LOAD();
  385. break;
  386. case 4:
  387. case 2:
  388. case 1:
  389. if (bytes == 4)
  390. BUILD_PTR_CAST(INT32_PTR_TYPE);
  391. else if (bytes == 2)
  392. BUILD_PTR_CAST(INT16_PTR_TYPE);
  393. else
  394. BUILD_PTR_CAST(INT8_PTR_TYPE);
  395. #if WASM_ENABLE_SHARED_MEMORY != 0
  396. if (atomic) {
  397. BUILD_ATOMIC_LOAD(align);
  398. BUILD_ZERO_EXT(I64_TYPE);
  399. }
  400. else
  401. #endif
  402. {
  403. BUILD_LOAD();
  404. if (sign)
  405. BUILD_SIGN_EXT(I64_TYPE);
  406. else
  407. BUILD_ZERO_EXT(I64_TYPE);
  408. }
  409. break;
  410. default:
  411. bh_assert(0);
  412. break;
  413. }
  414. PUSH_I64(value);
  415. return true;
  416. fail:
  417. return false;
  418. }
  419. bool
  420. aot_compile_op_f32_load(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  421. uint32 align, uint32 offset)
  422. {
  423. LLVMValueRef maddr, value;
  424. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, 4)))
  425. return false;
  426. BUILD_PTR_CAST(F32_PTR_TYPE);
  427. BUILD_LOAD();
  428. PUSH_F32(value);
  429. return true;
  430. fail:
  431. return false;
  432. }
  433. bool
  434. aot_compile_op_f64_load(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  435. uint32 align, uint32 offset)
  436. {
  437. LLVMValueRef maddr, value;
  438. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, 8)))
  439. return false;
  440. BUILD_PTR_CAST(F64_PTR_TYPE);
  441. BUILD_LOAD();
  442. PUSH_F64(value);
  443. return true;
  444. fail:
  445. return false;
  446. }
  447. bool
  448. aot_compile_op_i32_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  449. uint32 align, uint32 offset, uint32 bytes, bool atomic)
  450. {
  451. LLVMValueRef maddr, value;
  452. POP_I32(value);
  453. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes)))
  454. return false;
  455. switch (bytes) {
  456. case 4:
  457. BUILD_PTR_CAST(INT32_PTR_TYPE);
  458. break;
  459. case 2:
  460. BUILD_PTR_CAST(INT16_PTR_TYPE);
  461. BUILD_TRUNC(value, INT16_TYPE);
  462. break;
  463. case 1:
  464. BUILD_PTR_CAST(INT8_PTR_TYPE);
  465. BUILD_TRUNC(value, INT8_TYPE);
  466. break;
  467. default:
  468. bh_assert(0);
  469. break;
  470. }
  471. #if WASM_ENABLE_SHARED_MEMORY != 0
  472. if (atomic)
  473. BUILD_ATOMIC_STORE(align);
  474. else
  475. #endif
  476. BUILD_STORE();
  477. return true;
  478. fail:
  479. return false;
  480. }
  481. bool
  482. aot_compile_op_i64_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  483. uint32 align, uint32 offset, uint32 bytes, bool atomic)
  484. {
  485. LLVMValueRef maddr, value;
  486. POP_I64(value);
  487. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes)))
  488. return false;
  489. switch (bytes) {
  490. case 8:
  491. BUILD_PTR_CAST(INT64_PTR_TYPE);
  492. break;
  493. case 4:
  494. BUILD_PTR_CAST(INT32_PTR_TYPE);
  495. BUILD_TRUNC(value, I32_TYPE);
  496. break;
  497. case 2:
  498. BUILD_PTR_CAST(INT16_PTR_TYPE);
  499. BUILD_TRUNC(value, INT16_TYPE);
  500. break;
  501. case 1:
  502. BUILD_PTR_CAST(INT8_PTR_TYPE);
  503. BUILD_TRUNC(value, INT8_TYPE);
  504. break;
  505. default:
  506. bh_assert(0);
  507. break;
  508. }
  509. #if WASM_ENABLE_SHARED_MEMORY != 0
  510. if (atomic)
  511. BUILD_ATOMIC_STORE(align);
  512. else
  513. #endif
  514. BUILD_STORE();
  515. return true;
  516. fail:
  517. return false;
  518. }
  519. bool
  520. aot_compile_op_f32_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  521. uint32 align, uint32 offset)
  522. {
  523. LLVMValueRef maddr, value;
  524. POP_F32(value);
  525. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, 4)))
  526. return false;
  527. BUILD_PTR_CAST(F32_PTR_TYPE);
  528. BUILD_STORE();
  529. return true;
  530. fail:
  531. return false;
  532. }
  533. bool
  534. aot_compile_op_f64_store(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  535. uint32 align, uint32 offset)
  536. {
  537. LLVMValueRef maddr, value;
  538. POP_F64(value);
  539. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, 8)))
  540. return false;
  541. BUILD_PTR_CAST(F64_PTR_TYPE);
  542. BUILD_STORE();
  543. return true;
  544. fail:
  545. return false;
  546. }
  547. static LLVMValueRef
  548. get_memory_curr_page_count(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  549. {
  550. LLVMValueRef mem_size;
  551. if (func_ctx->mem_space_unchanged) {
  552. mem_size = func_ctx->mem_info[0].mem_cur_page_count_addr;
  553. }
  554. else {
  555. if (!(mem_size = LLVMBuildLoad(
  556. comp_ctx->builder,
  557. func_ctx->mem_info[0].mem_cur_page_count_addr, "mem_size"))) {
  558. aot_set_last_error("llvm build load failed.");
  559. goto fail;
  560. }
  561. }
  562. return mem_size;
  563. fail:
  564. return NULL;
  565. }
  566. bool
  567. aot_compile_op_memory_size(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  568. {
  569. LLVMValueRef mem_size = get_memory_curr_page_count(comp_ctx, func_ctx);
  570. if (mem_size)
  571. PUSH_I32(mem_size);
  572. return mem_size ? true : false;
  573. fail:
  574. return false;
  575. }
  576. bool
  577. aot_compile_op_memory_grow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  578. {
  579. LLVMValueRef mem_size = get_memory_curr_page_count(comp_ctx, func_ctx);
  580. LLVMValueRef delta, param_values[2], ret_value, func, value;
  581. LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
  582. int32 func_index;
  583. if (!mem_size)
  584. return false;
  585. POP_I32(delta);
  586. /* Function type of aot_enlarge_memory() */
  587. param_types[0] = INT8_PTR_TYPE;
  588. param_types[1] = I32_TYPE;
  589. ret_type = INT8_TYPE;
  590. if (!(func_type = LLVMFunctionType(ret_type, param_types, 2, false))) {
  591. aot_set_last_error("llvm add function type failed.");
  592. return false;
  593. }
  594. if (comp_ctx->is_jit_mode) {
  595. /* JIT mode, call the function directly */
  596. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  597. aot_set_last_error("llvm add pointer type failed.");
  598. return false;
  599. }
  600. if (!(value = I64_CONST((uint64)(uintptr_t)aot_enlarge_memory))
  601. || !(func = LLVMConstIntToPtr(value, func_ptr_type))) {
  602. aot_set_last_error("create LLVM value failed.");
  603. return false;
  604. }
  605. }
  606. else if (comp_ctx->is_indirect_mode) {
  607. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  608. aot_set_last_error("create LLVM function type failed.");
  609. return false;
  610. }
  611. func_index =
  612. aot_get_native_symbol_index(comp_ctx, "aot_enlarge_memory");
  613. if (func_index < 0) {
  614. return false;
  615. }
  616. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  617. func_ptr_type, func_index))) {
  618. return false;
  619. }
  620. }
  621. else {
  622. char *func_name = "aot_enlarge_memory";
  623. /* AOT mode, delcare the function */
  624. if (!(func = LLVMGetNamedFunction(func_ctx->module, func_name))
  625. && !(func =
  626. LLVMAddFunction(func_ctx->module, func_name, func_type))) {
  627. aot_set_last_error("llvm add function failed.");
  628. return false;
  629. }
  630. }
  631. /* Call function aot_enlarge_memory() */
  632. param_values[0] = func_ctx->aot_inst;
  633. param_values[1] = delta;
  634. if (!(ret_value = LLVMBuildCall(comp_ctx->builder, func, param_values, 2,
  635. "call"))) {
  636. aot_set_last_error("llvm build call failed.");
  637. return false;
  638. }
  639. BUILD_ICMP(LLVMIntUGT, ret_value, I8_ZERO, ret_value, "mem_grow_ret");
  640. /* ret_value = ret_value == true ? delta : pre_page_count */
  641. if (!(ret_value = LLVMBuildSelect(comp_ctx->builder, ret_value, mem_size,
  642. I32_NEG_ONE, "mem_grow_ret"))) {
  643. aot_set_last_error("llvm build select failed.");
  644. return false;
  645. }
  646. PUSH_I32(ret_value);
  647. return true;
  648. fail:
  649. return false;
  650. }
  651. #if WASM_ENABLE_BULK_MEMORY != 0
  652. static LLVMValueRef
  653. check_bulk_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  654. LLVMValueRef offset, LLVMValueRef bytes)
  655. {
  656. LLVMValueRef maddr, max_addr, cmp;
  657. LLVMValueRef mem_base_addr;
  658. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  659. LLVMBasicBlockRef check_succ;
  660. LLVMValueRef mem_size;
  661. /* Get memory base address and memory data size */
  662. #if WASM_ENABLE_SHARED_MEMORY != 0
  663. bool is_shared_memory =
  664. comp_ctx->comp_data->memories[0].memory_flags & 0x02;
  665. if (func_ctx->mem_space_unchanged || is_shared_memory) {
  666. #else
  667. if (func_ctx->mem_space_unchanged) {
  668. #endif
  669. mem_base_addr = func_ctx->mem_info[0].mem_base_addr;
  670. }
  671. else {
  672. if (!(mem_base_addr = LLVMBuildLoad(comp_ctx->builder,
  673. func_ctx->mem_info[0].mem_base_addr,
  674. "mem_base"))) {
  675. aot_set_last_error("llvm build load failed.");
  676. goto fail;
  677. }
  678. }
  679. /*
  680. * Note: not throw the integer-overflow-exception here since it must
  681. * have been thrown when converting float to integer before
  682. */
  683. /* return addres directly if constant offset and inside memory space */
  684. if (!LLVMIsUndef(offset) && !LLVMIsUndef(bytes)
  685. #if LLVM_VERSION_NUMBER >= 12
  686. && !LLVMIsPoison(offset) && !LLVMIsPoison(bytes)
  687. #endif
  688. && LLVMIsConstant(offset) && LLVMIsConstant(bytes)) {
  689. uint64 mem_offset = (uint64)LLVMConstIntGetZExtValue(offset);
  690. uint64 mem_len = (uint64)LLVMConstIntGetZExtValue(bytes);
  691. uint32 num_bytes_per_page =
  692. comp_ctx->comp_data->memories[0].num_bytes_per_page;
  693. uint32 init_page_count =
  694. comp_ctx->comp_data->memories[0].mem_init_page_count;
  695. uint32 mem_data_size = num_bytes_per_page * init_page_count;
  696. if (mem_data_size > 0 && mem_offset + mem_len <= mem_data_size) {
  697. /* inside memory space */
  698. /* maddr = mem_base_addr + moffset */
  699. if (!(maddr = LLVMBuildInBoundsGEP(comp_ctx->builder, mem_base_addr,
  700. &offset, 1, "maddr"))) {
  701. aot_set_last_error("llvm build add failed.");
  702. goto fail;
  703. }
  704. return maddr;
  705. }
  706. }
  707. if (func_ctx->mem_space_unchanged) {
  708. mem_size = func_ctx->mem_info[0].mem_data_size_addr;
  709. }
  710. else {
  711. if (!(mem_size = LLVMBuildLoad(comp_ctx->builder,
  712. func_ctx->mem_info[0].mem_data_size_addr,
  713. "mem_size"))) {
  714. aot_set_last_error("llvm build load failed.");
  715. goto fail;
  716. }
  717. }
  718. ADD_BASIC_BLOCK(check_succ, "check_succ");
  719. LLVMMoveBasicBlockAfter(check_succ, block_curr);
  720. offset =
  721. LLVMBuildZExt(comp_ctx->builder, offset, I64_TYPE, "extend_offset");
  722. bytes = LLVMBuildZExt(comp_ctx->builder, bytes, I64_TYPE, "extend_len");
  723. mem_size =
  724. LLVMBuildZExt(comp_ctx->builder, mem_size, I64_TYPE, "extend_size");
  725. BUILD_OP(Add, offset, bytes, max_addr, "max_addr");
  726. BUILD_ICMP(LLVMIntUGT, max_addr, mem_size, cmp, "cmp_max_mem_addr");
  727. if (!aot_emit_exception(comp_ctx, func_ctx,
  728. EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp,
  729. check_succ)) {
  730. goto fail;
  731. }
  732. /* maddr = mem_base_addr + offset */
  733. if (!(maddr = LLVMBuildInBoundsGEP(comp_ctx->builder, mem_base_addr,
  734. &offset, 1, "maddr"))) {
  735. aot_set_last_error("llvm build add failed.");
  736. goto fail;
  737. }
  738. return maddr;
  739. fail:
  740. return NULL;
  741. }
  742. bool
  743. aot_compile_op_memory_init(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  744. uint32 seg_index)
  745. {
  746. LLVMValueRef seg, offset, dst, len, param_values[5], ret_value, func, value;
  747. LLVMTypeRef param_types[5], ret_type, func_type, func_ptr_type;
  748. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  749. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  750. LLVMBasicBlockRef mem_init_fail, init_success;
  751. seg = I32_CONST(seg_index);
  752. POP_I32(len);
  753. POP_I32(offset);
  754. POP_I32(dst);
  755. param_types[0] = INT8_PTR_TYPE;
  756. param_types[1] = I32_TYPE;
  757. param_types[2] = I32_TYPE;
  758. param_types[3] = I32_TYPE;
  759. param_types[4] = I32_TYPE;
  760. ret_type = INT8_TYPE;
  761. GET_AOT_FUNCTION(aot_memory_init, 5);
  762. /* Call function aot_memory_init() */
  763. param_values[0] = func_ctx->aot_inst;
  764. param_values[1] = seg;
  765. param_values[2] = offset;
  766. param_values[3] = len;
  767. param_values[4] = dst;
  768. if (!(ret_value = LLVMBuildCall(comp_ctx->builder, func, param_values, 5,
  769. "call"))) {
  770. aot_set_last_error("llvm build call failed.");
  771. return false;
  772. }
  773. BUILD_ICMP(LLVMIntUGT, ret_value, I8_ZERO, ret_value, "mem_init_ret");
  774. ADD_BASIC_BLOCK(mem_init_fail, "mem_init_fail");
  775. ADD_BASIC_BLOCK(init_success, "init_success");
  776. LLVMMoveBasicBlockAfter(mem_init_fail, block_curr);
  777. LLVMMoveBasicBlockAfter(init_success, block_curr);
  778. if (!LLVMBuildCondBr(comp_ctx->builder, ret_value, init_success,
  779. mem_init_fail)) {
  780. aot_set_last_error("llvm build cond br failed.");
  781. goto fail;
  782. }
  783. /* If memory.init failed, return this function
  784. so the runtime can catch the exception */
  785. LLVMPositionBuilderAtEnd(comp_ctx->builder, mem_init_fail);
  786. if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
  787. goto fail;
  788. }
  789. LLVMPositionBuilderAtEnd(comp_ctx->builder, init_success);
  790. return true;
  791. fail:
  792. return false;
  793. }
  794. bool
  795. aot_compile_op_data_drop(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  796. uint32 seg_index)
  797. {
  798. LLVMValueRef seg, param_values[2], ret_value, func, value;
  799. LLVMTypeRef param_types[2], ret_type, func_type, func_ptr_type;
  800. seg = I32_CONST(seg_index);
  801. CHECK_LLVM_CONST(seg);
  802. param_types[0] = INT8_PTR_TYPE;
  803. param_types[1] = I32_TYPE;
  804. ret_type = INT8_TYPE;
  805. GET_AOT_FUNCTION(aot_data_drop, 2);
  806. /* Call function aot_data_drop() */
  807. param_values[0] = func_ctx->aot_inst;
  808. param_values[1] = seg;
  809. if (!(ret_value = LLVMBuildCall(comp_ctx->builder, func, param_values, 2,
  810. "call"))) {
  811. aot_set_last_error("llvm build call failed.");
  812. return false;
  813. }
  814. return true;
  815. fail:
  816. return false;
  817. }
  818. bool
  819. aot_compile_op_memory_copy(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  820. {
  821. LLVMValueRef src, dst, src_addr, dst_addr, len, res;
  822. bool call_aot_memmove = false;
  823. POP_I32(len);
  824. POP_I32(src);
  825. POP_I32(dst);
  826. if (!(src_addr = check_bulk_memory_overflow(comp_ctx, func_ctx, src, len)))
  827. return false;
  828. if (!(dst_addr = check_bulk_memory_overflow(comp_ctx, func_ctx, dst, len)))
  829. return false;
  830. #if WASM_ENABLE_LAZY_JIT != 0
  831. call_aot_memmove = true;
  832. #endif
  833. if (comp_ctx->is_indirect_mode)
  834. call_aot_memmove = true;
  835. if (call_aot_memmove) {
  836. LLVMTypeRef param_types[3], ret_type, func_type, func_ptr_type;
  837. LLVMValueRef func, params[3];
  838. #if WASM_ENABLE_LAZY_JIT == 0
  839. int32 func_idx;
  840. #endif
  841. param_types[0] = INT8_PTR_TYPE;
  842. param_types[1] = INT8_PTR_TYPE;
  843. param_types[2] = I32_TYPE;
  844. ret_type = INT8_PTR_TYPE;
  845. if (!(func_type = LLVMFunctionType(ret_type, param_types, 3, false))) {
  846. aot_set_last_error("create LLVM function type failed.");
  847. return false;
  848. }
  849. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  850. aot_set_last_error("create LLVM function pointer type failed.");
  851. return false;
  852. }
  853. #if WASM_ENABLE_LAZY_JIT == 0
  854. func_idx = aot_get_native_symbol_index(comp_ctx, "memmove");
  855. if (func_idx < 0) {
  856. return false;
  857. }
  858. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  859. func_ptr_type, func_idx))) {
  860. return false;
  861. }
  862. #else
  863. if (!(func = I64_CONST((uint64)(uintptr_t)aot_memmove))
  864. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  865. aot_set_last_error("create LLVM value failed.");
  866. return false;
  867. }
  868. #endif
  869. params[0] = dst_addr;
  870. params[1] = src_addr;
  871. params[2] = len;
  872. if (!(res = LLVMBuildCall(comp_ctx->builder, func, params, 3,
  873. "call_memmove"))) {
  874. aot_set_last_error("llvm build memmove failed.");
  875. return false;
  876. }
  877. }
  878. else {
  879. if (!(res = LLVMBuildMemMove(comp_ctx->builder, dst_addr, 1, src_addr,
  880. 1, len))) {
  881. aot_set_last_error("llvm build memmove failed.");
  882. return false;
  883. }
  884. }
  885. return true;
  886. fail:
  887. return false;
  888. }
  889. bool
  890. aot_compile_op_memory_fill(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  891. {
  892. LLVMValueRef val, dst, dst_addr, len, res;
  893. LLVMTypeRef param_types[3], ret_type, func_type, func_ptr_type;
  894. LLVMValueRef func, params[3];
  895. POP_I32(len);
  896. POP_I32(val);
  897. POP_I32(dst);
  898. if (!(dst_addr = check_bulk_memory_overflow(comp_ctx, func_ctx, dst, len)))
  899. return false;
  900. param_types[0] = INT8_PTR_TYPE;
  901. param_types[1] = I32_TYPE;
  902. param_types[2] = I32_TYPE;
  903. ret_type = INT8_PTR_TYPE;
  904. if (!(func_type = LLVMFunctionType(ret_type, param_types, 3, false))) {
  905. aot_set_last_error("create LLVM function type failed.");
  906. return false;
  907. }
  908. if (!(func_ptr_type = LLVMPointerType(func_type, 0))) {
  909. aot_set_last_error("create LLVM function pointer type failed.");
  910. return false;
  911. }
  912. if (comp_ctx->is_jit_mode) {
  913. if (!(func = I64_CONST((uint64)(uintptr_t)aot_memset))
  914. || !(func = LLVMConstIntToPtr(func, func_ptr_type))) {
  915. aot_set_last_error("create LLVM value failed.");
  916. return false;
  917. }
  918. }
  919. else if (comp_ctx->is_indirect_mode) {
  920. int32 func_index;
  921. func_index = aot_get_native_symbol_index(comp_ctx, "memset");
  922. if (func_index < 0) {
  923. return false;
  924. }
  925. if (!(func = aot_get_func_from_table(comp_ctx, func_ctx->native_symbol,
  926. func_ptr_type, func_index))) {
  927. return false;
  928. }
  929. }
  930. else {
  931. if (!(func = LLVMGetNamedFunction(func_ctx->module, "memset"))
  932. && !(func =
  933. LLVMAddFunction(func_ctx->module, "memset", func_type))) {
  934. aot_set_last_error("llvm add function failed.");
  935. return false;
  936. }
  937. }
  938. params[0] = dst_addr;
  939. params[1] = val;
  940. params[2] = len;
  941. if (!(res = LLVMBuildCall(comp_ctx->builder, func, params, 3,
  942. "call_memset"))) {
  943. aot_set_last_error("llvm build memset failed.");
  944. return false;
  945. }
  946. return true;
  947. fail:
  948. return false;
  949. }
  950. #endif /* end of WASM_ENABLE_BULK_MEMORY */
  951. #if WASM_ENABLE_SHARED_MEMORY != 0
  952. bool
  953. aot_compile_op_atomic_rmw(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  954. uint8 atomic_op, uint8 op_type, uint32 align,
  955. uint32 offset, uint32 bytes)
  956. {
  957. LLVMValueRef maddr, value, result;
  958. if (op_type == VALUE_TYPE_I32)
  959. POP_I32(value);
  960. else
  961. POP_I64(value);
  962. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes)))
  963. return false;
  964. if (!check_memory_alignment(comp_ctx, func_ctx, maddr, align))
  965. return false;
  966. switch (bytes) {
  967. case 8:
  968. BUILD_PTR_CAST(INT64_PTR_TYPE);
  969. break;
  970. case 4:
  971. BUILD_PTR_CAST(INT32_PTR_TYPE);
  972. if (op_type == VALUE_TYPE_I64)
  973. BUILD_TRUNC(value, I32_TYPE);
  974. break;
  975. case 2:
  976. BUILD_PTR_CAST(INT16_PTR_TYPE);
  977. BUILD_TRUNC(value, INT16_TYPE);
  978. break;
  979. case 1:
  980. BUILD_PTR_CAST(INT8_PTR_TYPE);
  981. BUILD_TRUNC(value, INT8_TYPE);
  982. break;
  983. default:
  984. bh_assert(0);
  985. break;
  986. }
  987. if (!(result = LLVMBuildAtomicRMW(
  988. comp_ctx->builder, atomic_op, maddr, value,
  989. LLVMAtomicOrderingSequentiallyConsistent, false))) {
  990. goto fail;
  991. }
  992. LLVMSetVolatile(result, true);
  993. if (op_type == VALUE_TYPE_I32) {
  994. if (!(result = LLVMBuildZExt(comp_ctx->builder, result, I32_TYPE,
  995. "result_i32"))) {
  996. goto fail;
  997. }
  998. PUSH_I32(result);
  999. }
  1000. else {
  1001. if (!(result = LLVMBuildZExt(comp_ctx->builder, result, I64_TYPE,
  1002. "result_i64"))) {
  1003. goto fail;
  1004. }
  1005. PUSH_I64(result);
  1006. }
  1007. return true;
  1008. fail:
  1009. return false;
  1010. }
  1011. bool
  1012. aot_compile_op_atomic_cmpxchg(AOTCompContext *comp_ctx,
  1013. AOTFuncContext *func_ctx, uint8 op_type,
  1014. uint32 align, uint32 offset, uint32 bytes)
  1015. {
  1016. LLVMValueRef maddr, value, expect, result;
  1017. if (op_type == VALUE_TYPE_I32) {
  1018. POP_I32(value);
  1019. POP_I32(expect);
  1020. }
  1021. else {
  1022. POP_I64(value);
  1023. POP_I64(expect);
  1024. }
  1025. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes)))
  1026. return false;
  1027. if (!check_memory_alignment(comp_ctx, func_ctx, maddr, align))
  1028. return false;
  1029. switch (bytes) {
  1030. case 8:
  1031. BUILD_PTR_CAST(INT64_PTR_TYPE);
  1032. break;
  1033. case 4:
  1034. BUILD_PTR_CAST(INT32_PTR_TYPE);
  1035. if (op_type == VALUE_TYPE_I64) {
  1036. BUILD_TRUNC(value, I32_TYPE);
  1037. BUILD_TRUNC(expect, I32_TYPE);
  1038. }
  1039. break;
  1040. case 2:
  1041. BUILD_PTR_CAST(INT16_PTR_TYPE);
  1042. BUILD_TRUNC(value, INT16_TYPE);
  1043. BUILD_TRUNC(expect, INT16_TYPE);
  1044. break;
  1045. case 1:
  1046. BUILD_PTR_CAST(INT8_PTR_TYPE);
  1047. BUILD_TRUNC(value, INT8_TYPE);
  1048. BUILD_TRUNC(expect, INT8_TYPE);
  1049. break;
  1050. default:
  1051. bh_assert(0);
  1052. break;
  1053. }
  1054. if (!(result = LLVMBuildAtomicCmpXchg(
  1055. comp_ctx->builder, maddr, expect, value,
  1056. LLVMAtomicOrderingSequentiallyConsistent,
  1057. LLVMAtomicOrderingSequentiallyConsistent, false))) {
  1058. goto fail;
  1059. }
  1060. LLVMSetVolatile(result, true);
  1061. /* CmpXchg return {i32, i1} structure,
  1062. we need to extrack the previous_value from the structure */
  1063. if (!(result = LLVMBuildExtractValue(comp_ctx->builder, result, 0,
  1064. "previous_value"))) {
  1065. goto fail;
  1066. }
  1067. if (op_type == VALUE_TYPE_I32) {
  1068. if (LLVMTypeOf(result) != I32_TYPE) {
  1069. if (!(result = LLVMBuildZExt(comp_ctx->builder, result, I32_TYPE,
  1070. "result_i32"))) {
  1071. goto fail;
  1072. }
  1073. }
  1074. PUSH_I32(result);
  1075. }
  1076. else {
  1077. if (LLVMTypeOf(result) != I64_TYPE) {
  1078. if (!(result = LLVMBuildZExt(comp_ctx->builder, result, I64_TYPE,
  1079. "result_i64"))) {
  1080. goto fail;
  1081. }
  1082. }
  1083. PUSH_I64(result);
  1084. }
  1085. return true;
  1086. fail:
  1087. return false;
  1088. }
  1089. bool
  1090. aot_compile_op_atomic_wait(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  1091. uint8 op_type, uint32 align, uint32 offset,
  1092. uint32 bytes)
  1093. {
  1094. LLVMValueRef maddr, value, timeout, expect, cmp;
  1095. LLVMValueRef param_values[5], ret_value, func, is_wait64;
  1096. LLVMTypeRef param_types[5], ret_type, func_type, func_ptr_type;
  1097. LLVMBasicBlockRef wait_fail, wait_success;
  1098. LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder);
  1099. AOTFuncType *aot_func_type = func_ctx->aot_func->func_type;
  1100. POP_I64(timeout);
  1101. if (op_type == VALUE_TYPE_I32) {
  1102. POP_I32(expect);
  1103. is_wait64 = I8_CONST(false);
  1104. if (!(expect = LLVMBuildZExt(comp_ctx->builder, expect, I64_TYPE,
  1105. "expect_i64"))) {
  1106. goto fail;
  1107. }
  1108. }
  1109. else {
  1110. POP_I64(expect);
  1111. is_wait64 = I8_CONST(true);
  1112. }
  1113. CHECK_LLVM_CONST(is_wait64);
  1114. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes)))
  1115. return false;
  1116. if (!check_memory_alignment(comp_ctx, func_ctx, maddr, align))
  1117. return false;
  1118. param_types[0] = INT8_PTR_TYPE;
  1119. param_types[1] = INT8_PTR_TYPE;
  1120. param_types[2] = I64_TYPE;
  1121. param_types[3] = I64_TYPE;
  1122. param_types[4] = INT8_TYPE;
  1123. ret_type = I32_TYPE;
  1124. GET_AOT_FUNCTION(wasm_runtime_atomic_wait, 5);
  1125. /* Call function wasm_runtime_atomic_wait() */
  1126. param_values[0] = func_ctx->aot_inst;
  1127. param_values[1] = maddr;
  1128. param_values[2] = expect;
  1129. param_values[3] = timeout;
  1130. param_values[4] = is_wait64;
  1131. if (!(ret_value = LLVMBuildCall(comp_ctx->builder, func, param_values, 5,
  1132. "call"))) {
  1133. aot_set_last_error("llvm build call failed.");
  1134. return false;
  1135. }
  1136. BUILD_ICMP(LLVMIntSGT, ret_value, I32_ZERO, cmp, "atomic_wait_ret");
  1137. ADD_BASIC_BLOCK(wait_fail, "atomic_wait_fail");
  1138. ADD_BASIC_BLOCK(wait_success, "wait_success");
  1139. LLVMMoveBasicBlockAfter(wait_fail, block_curr);
  1140. LLVMMoveBasicBlockAfter(wait_success, block_curr);
  1141. if (!LLVMBuildCondBr(comp_ctx->builder, cmp, wait_success, wait_fail)) {
  1142. aot_set_last_error("llvm build cond br failed.");
  1143. goto fail;
  1144. }
  1145. /* If atomic wait failed, return this function
  1146. so the runtime can catch the exception */
  1147. LLVMPositionBuilderAtEnd(comp_ctx->builder, wait_fail);
  1148. if (!aot_build_zero_function_ret(comp_ctx, func_ctx, aot_func_type)) {
  1149. goto fail;
  1150. }
  1151. LLVMPositionBuilderAtEnd(comp_ctx->builder, wait_success);
  1152. PUSH_I32(ret_value);
  1153. return true;
  1154. fail:
  1155. return false;
  1156. }
  1157. bool
  1158. aot_compiler_op_atomic_notify(AOTCompContext *comp_ctx,
  1159. AOTFuncContext *func_ctx, uint32 align,
  1160. uint32 offset, uint32 bytes)
  1161. {
  1162. LLVMValueRef maddr, value, count;
  1163. LLVMValueRef param_values[3], ret_value, func;
  1164. LLVMTypeRef param_types[3], ret_type, func_type, func_ptr_type;
  1165. POP_I32(count);
  1166. if (!(maddr = aot_check_memory_overflow(comp_ctx, func_ctx, offset, bytes)))
  1167. return false;
  1168. if (!check_memory_alignment(comp_ctx, func_ctx, maddr, align))
  1169. return false;
  1170. param_types[0] = INT8_PTR_TYPE;
  1171. param_types[1] = INT8_PTR_TYPE;
  1172. param_types[2] = I32_TYPE;
  1173. ret_type = I32_TYPE;
  1174. GET_AOT_FUNCTION(wasm_runtime_atomic_notify, 3);
  1175. /* Call function wasm_runtime_atomic_notify() */
  1176. param_values[0] = func_ctx->aot_inst;
  1177. param_values[1] = maddr;
  1178. param_values[2] = count;
  1179. if (!(ret_value = LLVMBuildCall(comp_ctx->builder, func, param_values, 3,
  1180. "call"))) {
  1181. aot_set_last_error("llvm build call failed.");
  1182. return false;
  1183. }
  1184. PUSH_I32(ret_value);
  1185. return true;
  1186. fail:
  1187. return false;
  1188. }
  1189. #endif /* end of WASM_ENABLE_SHARED_MEMORY */