aot_emit_memory.c 51 KB

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