aot_compiler.c 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "aot_compiler.h"
  6. #include "aot_emit_compare.h"
  7. #include "aot_emit_conversion.h"
  8. #include "aot_emit_memory.h"
  9. #include "aot_emit_variable.h"
  10. #include "aot_emit_const.h"
  11. #include "aot_emit_exception.h"
  12. #include "aot_emit_numberic.h"
  13. #include "aot_emit_control.h"
  14. #include "aot_emit_function.h"
  15. #include "aot_emit_parametric.h"
  16. #include "aot_emit_table.h"
  17. #include "simd/simd_access_lanes.h"
  18. #include "simd/simd_bitmask_extracts.h"
  19. #include "simd/simd_bit_shifts.h"
  20. #include "simd/simd_bitwise_ops.h"
  21. #include "simd/simd_bool_reductions.h"
  22. #include "simd/simd_comparisons.h"
  23. #include "simd/simd_conversions.h"
  24. #include "simd/simd_construct_values.h"
  25. #include "simd/simd_conversions.h"
  26. #include "simd/simd_floating_point.h"
  27. #include "simd/simd_int_arith.h"
  28. #include "simd/simd_load_store.h"
  29. #include "simd/simd_sat_int_arith.h"
  30. #include "../aot/aot_runtime.h"
  31. #include "../interpreter/wasm_opcode.h"
  32. #include <errno.h>
  33. #if WASM_ENABLE_DEBUG_AOT != 0
  34. #include "debug/dwarf_extractor.h"
  35. #endif
  36. #define CHECK_BUF(buf, buf_end, length) \
  37. do { \
  38. if (buf + length > buf_end) { \
  39. aot_set_last_error("read leb failed: unexpected end."); \
  40. return false; \
  41. } \
  42. } while (0)
  43. static bool
  44. read_leb(const uint8 *buf, const uint8 *buf_end, uint32 *p_offset,
  45. uint32 maxbits, bool sign, uint64 *p_result)
  46. {
  47. uint64 result = 0;
  48. uint32 shift = 0;
  49. uint32 bcnt = 0;
  50. uint64 byte;
  51. while (true) {
  52. CHECK_BUF(buf, buf_end, 1);
  53. byte = buf[*p_offset];
  54. *p_offset += 1;
  55. result |= ((byte & 0x7f) << shift);
  56. shift += 7;
  57. if ((byte & 0x80) == 0) {
  58. break;
  59. }
  60. bcnt += 1;
  61. }
  62. if (bcnt > (maxbits + 6) / 7) {
  63. aot_set_last_error("read leb failed: "
  64. "integer representation too long");
  65. return false;
  66. }
  67. if (sign && (shift < maxbits) && (byte & 0x40)) {
  68. /* Sign extend */
  69. result |= (~((uint64)0)) << shift;
  70. }
  71. *p_result = result;
  72. return true;
  73. }
  74. #define read_leb_uint32(p, p_end, res) \
  75. do { \
  76. uint32 off = 0; \
  77. uint64 res64; \
  78. if (!read_leb(p, p_end, &off, 32, false, &res64)) \
  79. return false; \
  80. p += off; \
  81. res = (uint32)res64; \
  82. } while (0)
  83. #define read_leb_int32(p, p_end, res) \
  84. do { \
  85. uint32 off = 0; \
  86. uint64 res64; \
  87. if (!read_leb(p, p_end, &off, 32, true, &res64)) \
  88. return false; \
  89. p += off; \
  90. res = (int32)res64; \
  91. } while (0)
  92. #define read_leb_int64(p, p_end, res) \
  93. do { \
  94. uint32 off = 0; \
  95. uint64 res64; \
  96. if (!read_leb(p, p_end, &off, 64, true, &res64)) \
  97. return false; \
  98. p += off; \
  99. res = (int64)res64; \
  100. } while (0)
  101. /**
  102. * Since Wamrc uses a full feature Wasm loader,
  103. * add a post-validator here to run checks according
  104. * to options, like enable_tail_call, enable_ref_types,
  105. * and so on.
  106. */
  107. static bool
  108. aot_validate_wasm(AOTCompContext *comp_ctx)
  109. {
  110. if (!comp_ctx->enable_ref_types) {
  111. /* Doesn't support multiple tables unless enabling reference type */
  112. if (comp_ctx->comp_data->import_table_count
  113. + comp_ctx->comp_data->table_count
  114. > 1) {
  115. aot_set_last_error("multiple tables");
  116. return false;
  117. }
  118. }
  119. return true;
  120. }
  121. #define COMPILE_ATOMIC_RMW(OP, NAME) \
  122. case WASM_OP_ATOMIC_RMW_I32_##NAME: \
  123. bytes = 4; \
  124. op_type = VALUE_TYPE_I32; \
  125. goto OP_ATOMIC_##OP; \
  126. case WASM_OP_ATOMIC_RMW_I64_##NAME: \
  127. bytes = 8; \
  128. op_type = VALUE_TYPE_I64; \
  129. goto OP_ATOMIC_##OP; \
  130. case WASM_OP_ATOMIC_RMW_I32_##NAME##8_U: \
  131. bytes = 1; \
  132. op_type = VALUE_TYPE_I32; \
  133. goto OP_ATOMIC_##OP; \
  134. case WASM_OP_ATOMIC_RMW_I32_##NAME##16_U: \
  135. bytes = 2; \
  136. op_type = VALUE_TYPE_I32; \
  137. goto OP_ATOMIC_##OP; \
  138. case WASM_OP_ATOMIC_RMW_I64_##NAME##8_U: \
  139. bytes = 1; \
  140. op_type = VALUE_TYPE_I64; \
  141. goto OP_ATOMIC_##OP; \
  142. case WASM_OP_ATOMIC_RMW_I64_##NAME##16_U: \
  143. bytes = 2; \
  144. op_type = VALUE_TYPE_I64; \
  145. goto OP_ATOMIC_##OP; \
  146. case WASM_OP_ATOMIC_RMW_I64_##NAME##32_U: \
  147. bytes = 4; \
  148. op_type = VALUE_TYPE_I64; \
  149. OP_ATOMIC_##OP : bin_op = LLVMAtomicRMWBinOp##OP; \
  150. goto build_atomic_rmw;
  151. static bool
  152. aot_compile_func(AOTCompContext *comp_ctx, uint32 func_index)
  153. {
  154. AOTFuncContext *func_ctx = comp_ctx->func_ctxes[func_index];
  155. uint8 *frame_ip = func_ctx->aot_func->code, opcode, *p_f32, *p_f64;
  156. uint8 *frame_ip_end = frame_ip + func_ctx->aot_func->code_size;
  157. uint8 *param_types = NULL;
  158. uint8 *result_types = NULL;
  159. uint8 value_type;
  160. uint16 param_count;
  161. uint16 result_count;
  162. uint32 br_depth, *br_depths, br_count;
  163. uint32 func_idx, type_idx, mem_idx, local_idx, global_idx, i;
  164. uint32 bytes = 4, align, offset;
  165. uint32 type_index;
  166. bool sign = true;
  167. int32 i32_const;
  168. int64 i64_const;
  169. float32 f32_const;
  170. float64 f64_const;
  171. AOTFuncType *func_type = NULL;
  172. #if WASM_ENABLE_DEBUG_AOT != 0
  173. LLVMMetadataRef location;
  174. #endif
  175. /* Start to translate the opcodes */
  176. LLVMPositionBuilderAtEnd(
  177. comp_ctx->builder,
  178. func_ctx->block_stack.block_list_head->llvm_entry_block);
  179. while (frame_ip < frame_ip_end) {
  180. opcode = *frame_ip++;
  181. #if WASM_ENABLE_DEBUG_AOT != 0
  182. location = dwarf_gen_location(
  183. comp_ctx, func_ctx,
  184. (frame_ip - 1) - comp_ctx->comp_data->wasm_module->buf_code);
  185. LLVMSetCurrentDebugLocation2(comp_ctx->builder, location);
  186. #endif
  187. switch (opcode) {
  188. case WASM_OP_UNREACHABLE:
  189. if (!aot_compile_op_unreachable(comp_ctx, func_ctx, &frame_ip))
  190. return false;
  191. break;
  192. case WASM_OP_NOP:
  193. break;
  194. case WASM_OP_BLOCK:
  195. case WASM_OP_LOOP:
  196. case WASM_OP_IF:
  197. {
  198. value_type = *frame_ip++;
  199. if (value_type == VALUE_TYPE_I32 || value_type == VALUE_TYPE_I64
  200. || value_type == VALUE_TYPE_F32
  201. || value_type == VALUE_TYPE_F64
  202. || value_type == VALUE_TYPE_V128
  203. || value_type == VALUE_TYPE_VOID
  204. || value_type == VALUE_TYPE_FUNCREF
  205. || value_type == VALUE_TYPE_EXTERNREF) {
  206. param_count = 0;
  207. param_types = NULL;
  208. if (value_type == VALUE_TYPE_VOID) {
  209. result_count = 0;
  210. result_types = NULL;
  211. }
  212. else {
  213. result_count = 1;
  214. result_types = &value_type;
  215. }
  216. }
  217. else {
  218. frame_ip--;
  219. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  220. func_type = comp_ctx->comp_data->func_types[type_index];
  221. param_count = func_type->param_count;
  222. param_types = func_type->types;
  223. result_count = func_type->result_count;
  224. result_types = func_type->types + param_count;
  225. }
  226. if (!aot_compile_op_block(
  227. comp_ctx, func_ctx, &frame_ip, frame_ip_end,
  228. (uint32)(LABEL_TYPE_BLOCK + opcode - WASM_OP_BLOCK),
  229. param_count, param_types, result_count, result_types))
  230. return false;
  231. break;
  232. }
  233. case EXT_OP_BLOCK:
  234. case EXT_OP_LOOP:
  235. case EXT_OP_IF:
  236. {
  237. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  238. func_type = comp_ctx->comp_data->func_types[type_index];
  239. param_count = func_type->param_count;
  240. param_types = func_type->types;
  241. result_count = func_type->result_count;
  242. result_types = func_type->types + param_count;
  243. if (!aot_compile_op_block(
  244. comp_ctx, func_ctx, &frame_ip, frame_ip_end,
  245. (uint32)(LABEL_TYPE_BLOCK + opcode - EXT_OP_BLOCK),
  246. param_count, param_types, result_count, result_types))
  247. return false;
  248. break;
  249. }
  250. case WASM_OP_ELSE:
  251. if (!aot_compile_op_else(comp_ctx, func_ctx, &frame_ip))
  252. return false;
  253. break;
  254. case WASM_OP_END:
  255. if (!aot_compile_op_end(comp_ctx, func_ctx, &frame_ip))
  256. return false;
  257. break;
  258. case WASM_OP_BR:
  259. read_leb_uint32(frame_ip, frame_ip_end, br_depth);
  260. if (!aot_compile_op_br(comp_ctx, func_ctx, br_depth, &frame_ip))
  261. return false;
  262. break;
  263. case WASM_OP_BR_IF:
  264. read_leb_uint32(frame_ip, frame_ip_end, br_depth);
  265. if (!aot_compile_op_br_if(comp_ctx, func_ctx, br_depth,
  266. &frame_ip))
  267. return false;
  268. break;
  269. case WASM_OP_BR_TABLE:
  270. read_leb_uint32(frame_ip, frame_ip_end, br_count);
  271. if (!(br_depths = wasm_runtime_malloc((uint32)sizeof(uint32)
  272. * (br_count + 1)))) {
  273. aot_set_last_error("allocate memory failed.");
  274. goto fail;
  275. }
  276. #if WASM_ENABLE_FAST_INTERP != 0
  277. for (i = 0; i <= br_count; i++)
  278. read_leb_uint32(frame_ip, frame_ip_end, br_depths[i]);
  279. #else
  280. for (i = 0; i <= br_count; i++)
  281. br_depths[i] = *frame_ip++;
  282. #endif
  283. if (!aot_compile_op_br_table(comp_ctx, func_ctx, br_depths,
  284. br_count, &frame_ip)) {
  285. wasm_runtime_free(br_depths);
  286. return false;
  287. }
  288. wasm_runtime_free(br_depths);
  289. break;
  290. #if WASM_ENABLE_FAST_INTERP == 0
  291. case EXT_OP_BR_TABLE_CACHE:
  292. {
  293. BrTableCache *node = bh_list_first_elem(
  294. comp_ctx->comp_data->wasm_module->br_table_cache_list);
  295. BrTableCache *node_next;
  296. uint8 *p_opcode = frame_ip - 1;
  297. read_leb_uint32(frame_ip, frame_ip_end, br_count);
  298. while (node) {
  299. node_next = bh_list_elem_next(node);
  300. if (node->br_table_op_addr == p_opcode) {
  301. br_depths = node->br_depths;
  302. if (!aot_compile_op_br_table(comp_ctx, func_ctx,
  303. br_depths, br_count,
  304. &frame_ip)) {
  305. return false;
  306. }
  307. break;
  308. }
  309. node = node_next;
  310. }
  311. bh_assert(node);
  312. break;
  313. }
  314. #endif
  315. case WASM_OP_RETURN:
  316. if (!aot_compile_op_return(comp_ctx, func_ctx, &frame_ip))
  317. return false;
  318. break;
  319. case WASM_OP_CALL:
  320. read_leb_uint32(frame_ip, frame_ip_end, func_idx);
  321. if (!aot_compile_op_call(comp_ctx, func_ctx, func_idx, false))
  322. return false;
  323. break;
  324. case WASM_OP_CALL_INDIRECT:
  325. {
  326. uint32 tbl_idx;
  327. read_leb_uint32(frame_ip, frame_ip_end, type_idx);
  328. #if WASM_ENABLE_REF_TYPES != 0
  329. if (comp_ctx->enable_ref_types) {
  330. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  331. }
  332. else
  333. #endif
  334. {
  335. frame_ip++;
  336. tbl_idx = 0;
  337. }
  338. if (!aot_compile_op_call_indirect(comp_ctx, func_ctx, type_idx,
  339. tbl_idx))
  340. return false;
  341. break;
  342. }
  343. #if WASM_ENABLE_TAIL_CALL != 0
  344. case WASM_OP_RETURN_CALL:
  345. if (!comp_ctx->enable_tail_call) {
  346. aot_set_last_error("unsupported opcode");
  347. return false;
  348. }
  349. read_leb_uint32(frame_ip, frame_ip_end, func_idx);
  350. if (!aot_compile_op_call(comp_ctx, func_ctx, func_idx, true))
  351. return false;
  352. if (!aot_compile_op_return(comp_ctx, func_ctx, &frame_ip))
  353. return false;
  354. break;
  355. case WASM_OP_RETURN_CALL_INDIRECT:
  356. {
  357. uint32 tbl_idx;
  358. if (!comp_ctx->enable_tail_call) {
  359. aot_set_last_error("unsupported opcode");
  360. return false;
  361. }
  362. read_leb_uint32(frame_ip, frame_ip_end, type_idx);
  363. #if WASM_ENABLE_REF_TYPES != 0
  364. if (comp_ctx->enable_ref_types) {
  365. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  366. }
  367. else
  368. #endif
  369. {
  370. frame_ip++;
  371. tbl_idx = 0;
  372. }
  373. if (!aot_compile_op_call_indirect(comp_ctx, func_ctx, type_idx,
  374. tbl_idx))
  375. return false;
  376. if (!aot_compile_op_return(comp_ctx, func_ctx, &frame_ip))
  377. return false;
  378. break;
  379. }
  380. #endif /* end of WASM_ENABLE_TAIL_CALL */
  381. case WASM_OP_DROP:
  382. if (!aot_compile_op_drop(comp_ctx, func_ctx, true))
  383. return false;
  384. break;
  385. case WASM_OP_DROP_64:
  386. if (!aot_compile_op_drop(comp_ctx, func_ctx, false))
  387. return false;
  388. break;
  389. case WASM_OP_SELECT:
  390. if (!aot_compile_op_select(comp_ctx, func_ctx, true))
  391. return false;
  392. break;
  393. case WASM_OP_SELECT_64:
  394. if (!aot_compile_op_select(comp_ctx, func_ctx, false))
  395. return false;
  396. break;
  397. #if WASM_ENABLE_REF_TYPES != 0
  398. case WASM_OP_SELECT_T:
  399. {
  400. uint32 vec_len;
  401. if (!comp_ctx->enable_ref_types) {
  402. goto unsupport_ref_types;
  403. }
  404. read_leb_uint32(frame_ip, frame_ip_end, vec_len);
  405. bh_assert(vec_len == 1);
  406. (void)vec_len;
  407. type_idx = *frame_ip++;
  408. if (!aot_compile_op_select(comp_ctx, func_ctx,
  409. (type_idx != VALUE_TYPE_I64)
  410. && (type_idx != VALUE_TYPE_F64)))
  411. return false;
  412. break;
  413. }
  414. case WASM_OP_TABLE_GET:
  415. {
  416. uint32 tbl_idx;
  417. if (!comp_ctx->enable_ref_types) {
  418. goto unsupport_ref_types;
  419. }
  420. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  421. if (!aot_compile_op_table_get(comp_ctx, func_ctx, tbl_idx))
  422. return false;
  423. break;
  424. }
  425. case WASM_OP_TABLE_SET:
  426. {
  427. uint32 tbl_idx;
  428. if (!comp_ctx->enable_ref_types) {
  429. goto unsupport_ref_types;
  430. }
  431. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  432. if (!aot_compile_op_table_set(comp_ctx, func_ctx, tbl_idx))
  433. return false;
  434. break;
  435. }
  436. case WASM_OP_REF_NULL:
  437. {
  438. uint32 type;
  439. if (!comp_ctx->enable_ref_types) {
  440. goto unsupport_ref_types;
  441. }
  442. read_leb_uint32(frame_ip, frame_ip_end, type);
  443. if (!aot_compile_op_ref_null(comp_ctx, func_ctx))
  444. return false;
  445. (void)type;
  446. break;
  447. }
  448. case WASM_OP_REF_IS_NULL:
  449. {
  450. if (!comp_ctx->enable_ref_types) {
  451. goto unsupport_ref_types;
  452. }
  453. if (!aot_compile_op_ref_is_null(comp_ctx, func_ctx))
  454. return false;
  455. break;
  456. }
  457. case WASM_OP_REF_FUNC:
  458. {
  459. if (!comp_ctx->enable_ref_types) {
  460. goto unsupport_ref_types;
  461. }
  462. read_leb_uint32(frame_ip, frame_ip_end, func_idx);
  463. if (!aot_compile_op_ref_func(comp_ctx, func_ctx, func_idx))
  464. return false;
  465. break;
  466. }
  467. #endif
  468. case WASM_OP_GET_LOCAL:
  469. read_leb_uint32(frame_ip, frame_ip_end, local_idx);
  470. if (!aot_compile_op_get_local(comp_ctx, func_ctx, local_idx))
  471. return false;
  472. break;
  473. case WASM_OP_SET_LOCAL:
  474. read_leb_uint32(frame_ip, frame_ip_end, local_idx);
  475. if (!aot_compile_op_set_local(comp_ctx, func_ctx, local_idx))
  476. return false;
  477. break;
  478. case WASM_OP_TEE_LOCAL:
  479. read_leb_uint32(frame_ip, frame_ip_end, local_idx);
  480. if (!aot_compile_op_tee_local(comp_ctx, func_ctx, local_idx))
  481. return false;
  482. break;
  483. case WASM_OP_GET_GLOBAL:
  484. case WASM_OP_GET_GLOBAL_64:
  485. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  486. if (!aot_compile_op_get_global(comp_ctx, func_ctx, global_idx))
  487. return false;
  488. break;
  489. case WASM_OP_SET_GLOBAL:
  490. case WASM_OP_SET_GLOBAL_64:
  491. case WASM_OP_SET_GLOBAL_AUX_STACK:
  492. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  493. if (!aot_compile_op_set_global(
  494. comp_ctx, func_ctx, global_idx,
  495. opcode == WASM_OP_SET_GLOBAL_AUX_STACK ? true : false))
  496. return false;
  497. break;
  498. case WASM_OP_I32_LOAD:
  499. bytes = 4;
  500. sign = true;
  501. goto op_i32_load;
  502. case WASM_OP_I32_LOAD8_S:
  503. case WASM_OP_I32_LOAD8_U:
  504. bytes = 1;
  505. sign = (opcode == WASM_OP_I32_LOAD8_S) ? true : false;
  506. goto op_i32_load;
  507. case WASM_OP_I32_LOAD16_S:
  508. case WASM_OP_I32_LOAD16_U:
  509. bytes = 2;
  510. sign = (opcode == WASM_OP_I32_LOAD16_S) ? true : false;
  511. op_i32_load:
  512. read_leb_uint32(frame_ip, frame_ip_end, align);
  513. read_leb_uint32(frame_ip, frame_ip_end, offset);
  514. if (!aot_compile_op_i32_load(comp_ctx, func_ctx, align, offset,
  515. bytes, sign, false))
  516. return false;
  517. break;
  518. case WASM_OP_I64_LOAD:
  519. bytes = 8;
  520. sign = true;
  521. goto op_i64_load;
  522. case WASM_OP_I64_LOAD8_S:
  523. case WASM_OP_I64_LOAD8_U:
  524. bytes = 1;
  525. sign = (opcode == WASM_OP_I64_LOAD8_S) ? true : false;
  526. goto op_i64_load;
  527. case WASM_OP_I64_LOAD16_S:
  528. case WASM_OP_I64_LOAD16_U:
  529. bytes = 2;
  530. sign = (opcode == WASM_OP_I64_LOAD16_S) ? true : false;
  531. goto op_i64_load;
  532. case WASM_OP_I64_LOAD32_S:
  533. case WASM_OP_I64_LOAD32_U:
  534. bytes = 4;
  535. sign = (opcode == WASM_OP_I64_LOAD32_S) ? true : false;
  536. op_i64_load:
  537. read_leb_uint32(frame_ip, frame_ip_end, align);
  538. read_leb_uint32(frame_ip, frame_ip_end, offset);
  539. if (!aot_compile_op_i64_load(comp_ctx, func_ctx, align, offset,
  540. bytes, sign, false))
  541. return false;
  542. break;
  543. case WASM_OP_F32_LOAD:
  544. read_leb_uint32(frame_ip, frame_ip_end, align);
  545. read_leb_uint32(frame_ip, frame_ip_end, offset);
  546. if (!aot_compile_op_f32_load(comp_ctx, func_ctx, align, offset))
  547. return false;
  548. break;
  549. case WASM_OP_F64_LOAD:
  550. read_leb_uint32(frame_ip, frame_ip_end, align);
  551. read_leb_uint32(frame_ip, frame_ip_end, offset);
  552. if (!aot_compile_op_f64_load(comp_ctx, func_ctx, align, offset))
  553. return false;
  554. break;
  555. case WASM_OP_I32_STORE:
  556. bytes = 4;
  557. goto op_i32_store;
  558. case WASM_OP_I32_STORE8:
  559. bytes = 1;
  560. goto op_i32_store;
  561. case WASM_OP_I32_STORE16:
  562. bytes = 2;
  563. op_i32_store:
  564. read_leb_uint32(frame_ip, frame_ip_end, align);
  565. read_leb_uint32(frame_ip, frame_ip_end, offset);
  566. if (!aot_compile_op_i32_store(comp_ctx, func_ctx, align, offset,
  567. bytes, false))
  568. return false;
  569. break;
  570. case WASM_OP_I64_STORE:
  571. bytes = 8;
  572. goto op_i64_store;
  573. case WASM_OP_I64_STORE8:
  574. bytes = 1;
  575. goto op_i64_store;
  576. case WASM_OP_I64_STORE16:
  577. bytes = 2;
  578. goto op_i64_store;
  579. case WASM_OP_I64_STORE32:
  580. bytes = 4;
  581. op_i64_store:
  582. read_leb_uint32(frame_ip, frame_ip_end, align);
  583. read_leb_uint32(frame_ip, frame_ip_end, offset);
  584. if (!aot_compile_op_i64_store(comp_ctx, func_ctx, align, offset,
  585. bytes, false))
  586. return false;
  587. break;
  588. case WASM_OP_F32_STORE:
  589. read_leb_uint32(frame_ip, frame_ip_end, align);
  590. read_leb_uint32(frame_ip, frame_ip_end, offset);
  591. if (!aot_compile_op_f32_store(comp_ctx, func_ctx, align,
  592. offset))
  593. return false;
  594. break;
  595. case WASM_OP_F64_STORE:
  596. read_leb_uint32(frame_ip, frame_ip_end, align);
  597. read_leb_uint32(frame_ip, frame_ip_end, offset);
  598. if (!aot_compile_op_f64_store(comp_ctx, func_ctx, align,
  599. offset))
  600. return false;
  601. break;
  602. case WASM_OP_MEMORY_SIZE:
  603. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  604. if (!aot_compile_op_memory_size(comp_ctx, func_ctx))
  605. return false;
  606. (void)mem_idx;
  607. break;
  608. case WASM_OP_MEMORY_GROW:
  609. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  610. if (!aot_compile_op_memory_grow(comp_ctx, func_ctx))
  611. return false;
  612. break;
  613. case WASM_OP_I32_CONST:
  614. read_leb_int32(frame_ip, frame_ip_end, i32_const);
  615. if (!aot_compile_op_i32_const(comp_ctx, func_ctx, i32_const))
  616. return false;
  617. break;
  618. case WASM_OP_I64_CONST:
  619. read_leb_int64(frame_ip, frame_ip_end, i64_const);
  620. if (!aot_compile_op_i64_const(comp_ctx, func_ctx, i64_const))
  621. return false;
  622. break;
  623. case WASM_OP_F32_CONST:
  624. p_f32 = (uint8 *)&f32_const;
  625. for (i = 0; i < sizeof(float32); i++)
  626. *p_f32++ = *frame_ip++;
  627. if (!aot_compile_op_f32_const(comp_ctx, func_ctx, f32_const))
  628. return false;
  629. break;
  630. case WASM_OP_F64_CONST:
  631. p_f64 = (uint8 *)&f64_const;
  632. for (i = 0; i < sizeof(float64); i++)
  633. *p_f64++ = *frame_ip++;
  634. if (!aot_compile_op_f64_const(comp_ctx, func_ctx, f64_const))
  635. return false;
  636. break;
  637. case WASM_OP_I32_EQZ:
  638. case WASM_OP_I32_EQ:
  639. case WASM_OP_I32_NE:
  640. case WASM_OP_I32_LT_S:
  641. case WASM_OP_I32_LT_U:
  642. case WASM_OP_I32_GT_S:
  643. case WASM_OP_I32_GT_U:
  644. case WASM_OP_I32_LE_S:
  645. case WASM_OP_I32_LE_U:
  646. case WASM_OP_I32_GE_S:
  647. case WASM_OP_I32_GE_U:
  648. if (!aot_compile_op_i32_compare(
  649. comp_ctx, func_ctx, INT_EQZ + opcode - WASM_OP_I32_EQZ))
  650. return false;
  651. break;
  652. case WASM_OP_I64_EQZ:
  653. case WASM_OP_I64_EQ:
  654. case WASM_OP_I64_NE:
  655. case WASM_OP_I64_LT_S:
  656. case WASM_OP_I64_LT_U:
  657. case WASM_OP_I64_GT_S:
  658. case WASM_OP_I64_GT_U:
  659. case WASM_OP_I64_LE_S:
  660. case WASM_OP_I64_LE_U:
  661. case WASM_OP_I64_GE_S:
  662. case WASM_OP_I64_GE_U:
  663. if (!aot_compile_op_i64_compare(
  664. comp_ctx, func_ctx, INT_EQZ + opcode - WASM_OP_I64_EQZ))
  665. return false;
  666. break;
  667. case WASM_OP_F32_EQ:
  668. case WASM_OP_F32_NE:
  669. case WASM_OP_F32_LT:
  670. case WASM_OP_F32_GT:
  671. case WASM_OP_F32_LE:
  672. case WASM_OP_F32_GE:
  673. if (!aot_compile_op_f32_compare(
  674. comp_ctx, func_ctx, FLOAT_EQ + opcode - WASM_OP_F32_EQ))
  675. return false;
  676. break;
  677. case WASM_OP_F64_EQ:
  678. case WASM_OP_F64_NE:
  679. case WASM_OP_F64_LT:
  680. case WASM_OP_F64_GT:
  681. case WASM_OP_F64_LE:
  682. case WASM_OP_F64_GE:
  683. if (!aot_compile_op_f64_compare(
  684. comp_ctx, func_ctx, FLOAT_EQ + opcode - WASM_OP_F64_EQ))
  685. return false;
  686. break;
  687. case WASM_OP_I32_CLZ:
  688. if (!aot_compile_op_i32_clz(comp_ctx, func_ctx))
  689. return false;
  690. break;
  691. case WASM_OP_I32_CTZ:
  692. if (!aot_compile_op_i32_ctz(comp_ctx, func_ctx))
  693. return false;
  694. break;
  695. case WASM_OP_I32_POPCNT:
  696. if (!aot_compile_op_i32_popcnt(comp_ctx, func_ctx))
  697. return false;
  698. break;
  699. case WASM_OP_I32_ADD:
  700. case WASM_OP_I32_SUB:
  701. case WASM_OP_I32_MUL:
  702. case WASM_OP_I32_DIV_S:
  703. case WASM_OP_I32_DIV_U:
  704. case WASM_OP_I32_REM_S:
  705. case WASM_OP_I32_REM_U:
  706. if (!aot_compile_op_i32_arithmetic(
  707. comp_ctx, func_ctx, INT_ADD + opcode - WASM_OP_I32_ADD,
  708. &frame_ip))
  709. return false;
  710. break;
  711. case WASM_OP_I32_AND:
  712. case WASM_OP_I32_OR:
  713. case WASM_OP_I32_XOR:
  714. if (!aot_compile_op_i32_bitwise(
  715. comp_ctx, func_ctx, INT_SHL + opcode - WASM_OP_I32_AND))
  716. return false;
  717. break;
  718. case WASM_OP_I32_SHL:
  719. case WASM_OP_I32_SHR_S:
  720. case WASM_OP_I32_SHR_U:
  721. case WASM_OP_I32_ROTL:
  722. case WASM_OP_I32_ROTR:
  723. if (!aot_compile_op_i32_shift(
  724. comp_ctx, func_ctx, INT_SHL + opcode - WASM_OP_I32_SHL))
  725. return false;
  726. break;
  727. case WASM_OP_I64_CLZ:
  728. if (!aot_compile_op_i64_clz(comp_ctx, func_ctx))
  729. return false;
  730. break;
  731. case WASM_OP_I64_CTZ:
  732. if (!aot_compile_op_i64_ctz(comp_ctx, func_ctx))
  733. return false;
  734. break;
  735. case WASM_OP_I64_POPCNT:
  736. if (!aot_compile_op_i64_popcnt(comp_ctx, func_ctx))
  737. return false;
  738. break;
  739. case WASM_OP_I64_ADD:
  740. case WASM_OP_I64_SUB:
  741. case WASM_OP_I64_MUL:
  742. case WASM_OP_I64_DIV_S:
  743. case WASM_OP_I64_DIV_U:
  744. case WASM_OP_I64_REM_S:
  745. case WASM_OP_I64_REM_U:
  746. if (!aot_compile_op_i64_arithmetic(
  747. comp_ctx, func_ctx, INT_ADD + opcode - WASM_OP_I64_ADD,
  748. &frame_ip))
  749. return false;
  750. break;
  751. case WASM_OP_I64_AND:
  752. case WASM_OP_I64_OR:
  753. case WASM_OP_I64_XOR:
  754. if (!aot_compile_op_i64_bitwise(
  755. comp_ctx, func_ctx, INT_SHL + opcode - WASM_OP_I64_AND))
  756. return false;
  757. break;
  758. case WASM_OP_I64_SHL:
  759. case WASM_OP_I64_SHR_S:
  760. case WASM_OP_I64_SHR_U:
  761. case WASM_OP_I64_ROTL:
  762. case WASM_OP_I64_ROTR:
  763. if (!aot_compile_op_i64_shift(
  764. comp_ctx, func_ctx, INT_SHL + opcode - WASM_OP_I64_SHL))
  765. return false;
  766. break;
  767. case WASM_OP_F32_ABS:
  768. case WASM_OP_F32_NEG:
  769. case WASM_OP_F32_CEIL:
  770. case WASM_OP_F32_FLOOR:
  771. case WASM_OP_F32_TRUNC:
  772. case WASM_OP_F32_NEAREST:
  773. case WASM_OP_F32_SQRT:
  774. if (!aot_compile_op_f32_math(comp_ctx, func_ctx,
  775. FLOAT_ABS + opcode
  776. - WASM_OP_F32_ABS))
  777. return false;
  778. break;
  779. case WASM_OP_F32_ADD:
  780. case WASM_OP_F32_SUB:
  781. case WASM_OP_F32_MUL:
  782. case WASM_OP_F32_DIV:
  783. case WASM_OP_F32_MIN:
  784. case WASM_OP_F32_MAX:
  785. if (!aot_compile_op_f32_arithmetic(comp_ctx, func_ctx,
  786. FLOAT_ADD + opcode
  787. - WASM_OP_F32_ADD))
  788. return false;
  789. break;
  790. case WASM_OP_F32_COPYSIGN:
  791. if (!aot_compile_op_f32_copysign(comp_ctx, func_ctx))
  792. return false;
  793. break;
  794. case WASM_OP_F64_ABS:
  795. case WASM_OP_F64_NEG:
  796. case WASM_OP_F64_CEIL:
  797. case WASM_OP_F64_FLOOR:
  798. case WASM_OP_F64_TRUNC:
  799. case WASM_OP_F64_NEAREST:
  800. case WASM_OP_F64_SQRT:
  801. if (!aot_compile_op_f64_math(comp_ctx, func_ctx,
  802. FLOAT_ABS + opcode
  803. - WASM_OP_F64_ABS))
  804. return false;
  805. break;
  806. case WASM_OP_F64_ADD:
  807. case WASM_OP_F64_SUB:
  808. case WASM_OP_F64_MUL:
  809. case WASM_OP_F64_DIV:
  810. case WASM_OP_F64_MIN:
  811. case WASM_OP_F64_MAX:
  812. if (!aot_compile_op_f64_arithmetic(comp_ctx, func_ctx,
  813. FLOAT_ADD + opcode
  814. - WASM_OP_F64_ADD))
  815. return false;
  816. break;
  817. case WASM_OP_F64_COPYSIGN:
  818. if (!aot_compile_op_f64_copysign(comp_ctx, func_ctx))
  819. return false;
  820. break;
  821. case WASM_OP_I32_WRAP_I64:
  822. if (!aot_compile_op_i32_wrap_i64(comp_ctx, func_ctx))
  823. return false;
  824. break;
  825. case WASM_OP_I32_TRUNC_S_F32:
  826. case WASM_OP_I32_TRUNC_U_F32:
  827. sign = (opcode == WASM_OP_I32_TRUNC_S_F32) ? true : false;
  828. if (!aot_compile_op_i32_trunc_f32(comp_ctx, func_ctx, sign,
  829. false))
  830. return false;
  831. break;
  832. case WASM_OP_I32_TRUNC_S_F64:
  833. case WASM_OP_I32_TRUNC_U_F64:
  834. sign = (opcode == WASM_OP_I32_TRUNC_S_F64) ? true : false;
  835. if (!aot_compile_op_i32_trunc_f64(comp_ctx, func_ctx, sign,
  836. false))
  837. return false;
  838. break;
  839. case WASM_OP_I64_EXTEND_S_I32:
  840. case WASM_OP_I64_EXTEND_U_I32:
  841. sign = (opcode == WASM_OP_I64_EXTEND_S_I32) ? true : false;
  842. if (!aot_compile_op_i64_extend_i32(comp_ctx, func_ctx, sign))
  843. return false;
  844. break;
  845. case WASM_OP_I64_TRUNC_S_F32:
  846. case WASM_OP_I64_TRUNC_U_F32:
  847. sign = (opcode == WASM_OP_I64_TRUNC_S_F32) ? true : false;
  848. if (!aot_compile_op_i64_trunc_f32(comp_ctx, func_ctx, sign,
  849. false))
  850. return false;
  851. break;
  852. case WASM_OP_I64_TRUNC_S_F64:
  853. case WASM_OP_I64_TRUNC_U_F64:
  854. sign = (opcode == WASM_OP_I64_TRUNC_S_F64) ? true : false;
  855. if (!aot_compile_op_i64_trunc_f64(comp_ctx, func_ctx, sign,
  856. false))
  857. return false;
  858. break;
  859. case WASM_OP_F32_CONVERT_S_I32:
  860. case WASM_OP_F32_CONVERT_U_I32:
  861. sign = (opcode == WASM_OP_F32_CONVERT_S_I32) ? true : false;
  862. if (!aot_compile_op_f32_convert_i32(comp_ctx, func_ctx, sign))
  863. return false;
  864. break;
  865. case WASM_OP_F32_CONVERT_S_I64:
  866. case WASM_OP_F32_CONVERT_U_I64:
  867. sign = (opcode == WASM_OP_F32_CONVERT_S_I64) ? true : false;
  868. if (!aot_compile_op_f32_convert_i64(comp_ctx, func_ctx, sign))
  869. return false;
  870. break;
  871. case WASM_OP_F32_DEMOTE_F64:
  872. if (!aot_compile_op_f32_demote_f64(comp_ctx, func_ctx))
  873. return false;
  874. break;
  875. case WASM_OP_F64_CONVERT_S_I32:
  876. case WASM_OP_F64_CONVERT_U_I32:
  877. sign = (opcode == WASM_OP_F64_CONVERT_S_I32) ? true : false;
  878. if (!aot_compile_op_f64_convert_i32(comp_ctx, func_ctx, sign))
  879. return false;
  880. break;
  881. case WASM_OP_F64_CONVERT_S_I64:
  882. case WASM_OP_F64_CONVERT_U_I64:
  883. sign = (opcode == WASM_OP_F64_CONVERT_S_I64) ? true : false;
  884. if (!aot_compile_op_f64_convert_i64(comp_ctx, func_ctx, sign))
  885. return false;
  886. break;
  887. case WASM_OP_F64_PROMOTE_F32:
  888. if (!aot_compile_op_f64_promote_f32(comp_ctx, func_ctx))
  889. return false;
  890. break;
  891. case WASM_OP_I32_REINTERPRET_F32:
  892. if (!aot_compile_op_i32_reinterpret_f32(comp_ctx, func_ctx))
  893. return false;
  894. break;
  895. case WASM_OP_I64_REINTERPRET_F64:
  896. if (!aot_compile_op_i64_reinterpret_f64(comp_ctx, func_ctx))
  897. return false;
  898. break;
  899. case WASM_OP_F32_REINTERPRET_I32:
  900. if (!aot_compile_op_f32_reinterpret_i32(comp_ctx, func_ctx))
  901. return false;
  902. break;
  903. case WASM_OP_F64_REINTERPRET_I64:
  904. if (!aot_compile_op_f64_reinterpret_i64(comp_ctx, func_ctx))
  905. return false;
  906. break;
  907. case WASM_OP_I32_EXTEND8_S:
  908. if (!aot_compile_op_i32_extend_i32(comp_ctx, func_ctx, 8))
  909. return false;
  910. break;
  911. case WASM_OP_I32_EXTEND16_S:
  912. if (!aot_compile_op_i32_extend_i32(comp_ctx, func_ctx, 16))
  913. return false;
  914. break;
  915. case WASM_OP_I64_EXTEND8_S:
  916. if (!aot_compile_op_i64_extend_i64(comp_ctx, func_ctx, 8))
  917. return false;
  918. break;
  919. case WASM_OP_I64_EXTEND16_S:
  920. if (!aot_compile_op_i64_extend_i64(comp_ctx, func_ctx, 16))
  921. return false;
  922. break;
  923. case WASM_OP_I64_EXTEND32_S:
  924. if (!aot_compile_op_i64_extend_i64(comp_ctx, func_ctx, 32))
  925. return false;
  926. break;
  927. case WASM_OP_MISC_PREFIX:
  928. {
  929. uint32 opcode1;
  930. read_leb_uint32(frame_ip, frame_ip_end, opcode1);
  931. opcode = (uint32)opcode1;
  932. #if WASM_ENABLE_BULK_MEMORY != 0
  933. if (WASM_OP_MEMORY_INIT <= opcode
  934. && opcode <= WASM_OP_MEMORY_FILL
  935. && !comp_ctx->enable_bulk_memory) {
  936. goto unsupport_bulk_memory;
  937. }
  938. #endif
  939. #if WASM_ENABLE_REF_TYPES != 0
  940. if (WASM_OP_TABLE_INIT <= opcode && opcode <= WASM_OP_TABLE_FILL
  941. && !comp_ctx->enable_ref_types) {
  942. goto unsupport_ref_types;
  943. }
  944. #endif
  945. switch (opcode) {
  946. case WASM_OP_I32_TRUNC_SAT_S_F32:
  947. case WASM_OP_I32_TRUNC_SAT_U_F32:
  948. sign = (opcode == WASM_OP_I32_TRUNC_SAT_S_F32) ? true
  949. : false;
  950. if (!aot_compile_op_i32_trunc_f32(comp_ctx, func_ctx,
  951. sign, true))
  952. return false;
  953. break;
  954. case WASM_OP_I32_TRUNC_SAT_S_F64:
  955. case WASM_OP_I32_TRUNC_SAT_U_F64:
  956. sign = (opcode == WASM_OP_I32_TRUNC_SAT_S_F64) ? true
  957. : false;
  958. if (!aot_compile_op_i32_trunc_f64(comp_ctx, func_ctx,
  959. sign, true))
  960. return false;
  961. break;
  962. case WASM_OP_I64_TRUNC_SAT_S_F32:
  963. case WASM_OP_I64_TRUNC_SAT_U_F32:
  964. sign = (opcode == WASM_OP_I64_TRUNC_SAT_S_F32) ? true
  965. : false;
  966. if (!aot_compile_op_i64_trunc_f32(comp_ctx, func_ctx,
  967. sign, true))
  968. return false;
  969. break;
  970. case WASM_OP_I64_TRUNC_SAT_S_F64:
  971. case WASM_OP_I64_TRUNC_SAT_U_F64:
  972. sign = (opcode == WASM_OP_I64_TRUNC_SAT_S_F64) ? true
  973. : false;
  974. if (!aot_compile_op_i64_trunc_f64(comp_ctx, func_ctx,
  975. sign, true))
  976. return false;
  977. break;
  978. #if WASM_ENABLE_BULK_MEMORY != 0
  979. case WASM_OP_MEMORY_INIT:
  980. {
  981. uint32 seg_index;
  982. read_leb_uint32(frame_ip, frame_ip_end, seg_index);
  983. frame_ip++;
  984. if (!aot_compile_op_memory_init(comp_ctx, func_ctx,
  985. seg_index))
  986. return false;
  987. break;
  988. }
  989. case WASM_OP_DATA_DROP:
  990. {
  991. uint32 seg_index;
  992. read_leb_uint32(frame_ip, frame_ip_end, seg_index);
  993. if (!aot_compile_op_data_drop(comp_ctx, func_ctx,
  994. seg_index))
  995. return false;
  996. break;
  997. }
  998. case WASM_OP_MEMORY_COPY:
  999. {
  1000. frame_ip += 2;
  1001. if (!aot_compile_op_memory_copy(comp_ctx, func_ctx))
  1002. return false;
  1003. break;
  1004. }
  1005. case WASM_OP_MEMORY_FILL:
  1006. {
  1007. frame_ip++;
  1008. if (!aot_compile_op_memory_fill(comp_ctx, func_ctx))
  1009. return false;
  1010. break;
  1011. }
  1012. #endif /* WASM_ENABLE_BULK_MEMORY */
  1013. #if WASM_ENABLE_REF_TYPES != 0
  1014. case WASM_OP_TABLE_INIT:
  1015. {
  1016. uint32 tbl_idx, tbl_seg_idx;
  1017. read_leb_uint32(frame_ip, frame_ip_end, tbl_seg_idx);
  1018. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  1019. if (!aot_compile_op_table_init(comp_ctx, func_ctx,
  1020. tbl_idx, tbl_seg_idx))
  1021. return false;
  1022. break;
  1023. }
  1024. case WASM_OP_ELEM_DROP:
  1025. {
  1026. uint32 tbl_seg_idx;
  1027. read_leb_uint32(frame_ip, frame_ip_end, tbl_seg_idx);
  1028. if (!aot_compile_op_elem_drop(comp_ctx, func_ctx,
  1029. tbl_seg_idx))
  1030. return false;
  1031. break;
  1032. }
  1033. case WASM_OP_TABLE_COPY:
  1034. {
  1035. uint32 src_tbl_idx, dst_tbl_idx;
  1036. read_leb_uint32(frame_ip, frame_ip_end, dst_tbl_idx);
  1037. read_leb_uint32(frame_ip, frame_ip_end, src_tbl_idx);
  1038. if (!aot_compile_op_table_copy(
  1039. comp_ctx, func_ctx, src_tbl_idx, dst_tbl_idx))
  1040. return false;
  1041. break;
  1042. }
  1043. case WASM_OP_TABLE_GROW:
  1044. {
  1045. uint32 tbl_idx;
  1046. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  1047. if (!aot_compile_op_table_grow(comp_ctx, func_ctx,
  1048. tbl_idx))
  1049. return false;
  1050. break;
  1051. }
  1052. case WASM_OP_TABLE_SIZE:
  1053. {
  1054. uint32 tbl_idx;
  1055. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  1056. if (!aot_compile_op_table_size(comp_ctx, func_ctx,
  1057. tbl_idx))
  1058. return false;
  1059. break;
  1060. }
  1061. case WASM_OP_TABLE_FILL:
  1062. {
  1063. uint32 tbl_idx;
  1064. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  1065. if (!aot_compile_op_table_fill(comp_ctx, func_ctx,
  1066. tbl_idx))
  1067. return false;
  1068. break;
  1069. }
  1070. #endif /* WASM_ENABLE_REF_TYPES */
  1071. default:
  1072. aot_set_last_error("unsupported opcode");
  1073. return false;
  1074. }
  1075. break;
  1076. }
  1077. #if WASM_ENABLE_SHARED_MEMORY != 0
  1078. case WASM_OP_ATOMIC_PREFIX:
  1079. {
  1080. uint8 bin_op, op_type;
  1081. if (frame_ip < frame_ip_end) {
  1082. opcode = *frame_ip++;
  1083. }
  1084. if (opcode != WASM_OP_ATOMIC_FENCE) {
  1085. read_leb_uint32(frame_ip, frame_ip_end, align);
  1086. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1087. }
  1088. switch (opcode) {
  1089. case WASM_OP_ATOMIC_WAIT32:
  1090. if (!aot_compile_op_atomic_wait(comp_ctx, func_ctx,
  1091. VALUE_TYPE_I32, align,
  1092. offset, 4))
  1093. return false;
  1094. break;
  1095. case WASM_OP_ATOMIC_WAIT64:
  1096. if (!aot_compile_op_atomic_wait(comp_ctx, func_ctx,
  1097. VALUE_TYPE_I64, align,
  1098. offset, 8))
  1099. return false;
  1100. break;
  1101. case WASM_OP_ATOMIC_NOTIFY:
  1102. if (!aot_compiler_op_atomic_notify(
  1103. comp_ctx, func_ctx, align, offset, bytes))
  1104. return false;
  1105. break;
  1106. case WASM_OP_ATOMIC_FENCE:
  1107. /* Skip memory index */
  1108. frame_ip++;
  1109. break;
  1110. case WASM_OP_ATOMIC_I32_LOAD:
  1111. bytes = 4;
  1112. goto op_atomic_i32_load;
  1113. case WASM_OP_ATOMIC_I32_LOAD8_U:
  1114. bytes = 1;
  1115. goto op_atomic_i32_load;
  1116. case WASM_OP_ATOMIC_I32_LOAD16_U:
  1117. bytes = 2;
  1118. op_atomic_i32_load:
  1119. if (!aot_compile_op_i32_load(comp_ctx, func_ctx, align,
  1120. offset, bytes, sign, true))
  1121. return false;
  1122. break;
  1123. case WASM_OP_ATOMIC_I64_LOAD:
  1124. bytes = 8;
  1125. goto op_atomic_i64_load;
  1126. case WASM_OP_ATOMIC_I64_LOAD8_U:
  1127. bytes = 1;
  1128. goto op_atomic_i64_load;
  1129. case WASM_OP_ATOMIC_I64_LOAD16_U:
  1130. bytes = 2;
  1131. goto op_atomic_i64_load;
  1132. case WASM_OP_ATOMIC_I64_LOAD32_U:
  1133. bytes = 4;
  1134. op_atomic_i64_load:
  1135. if (!aot_compile_op_i64_load(comp_ctx, func_ctx, align,
  1136. offset, bytes, sign, true))
  1137. return false;
  1138. break;
  1139. case WASM_OP_ATOMIC_I32_STORE:
  1140. bytes = 4;
  1141. goto op_atomic_i32_store;
  1142. case WASM_OP_ATOMIC_I32_STORE8:
  1143. bytes = 1;
  1144. goto op_atomic_i32_store;
  1145. case WASM_OP_ATOMIC_I32_STORE16:
  1146. bytes = 2;
  1147. op_atomic_i32_store:
  1148. if (!aot_compile_op_i32_store(comp_ctx, func_ctx, align,
  1149. offset, bytes, true))
  1150. return false;
  1151. break;
  1152. case WASM_OP_ATOMIC_I64_STORE:
  1153. bytes = 8;
  1154. goto op_atomic_i64_store;
  1155. case WASM_OP_ATOMIC_I64_STORE8:
  1156. bytes = 1;
  1157. goto op_atomic_i64_store;
  1158. case WASM_OP_ATOMIC_I64_STORE16:
  1159. bytes = 2;
  1160. goto op_atomic_i64_store;
  1161. case WASM_OP_ATOMIC_I64_STORE32:
  1162. bytes = 4;
  1163. op_atomic_i64_store:
  1164. if (!aot_compile_op_i64_store(comp_ctx, func_ctx, align,
  1165. offset, bytes, true))
  1166. return false;
  1167. break;
  1168. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  1169. bytes = 4;
  1170. op_type = VALUE_TYPE_I32;
  1171. goto op_atomic_cmpxchg;
  1172. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  1173. bytes = 8;
  1174. op_type = VALUE_TYPE_I64;
  1175. goto op_atomic_cmpxchg;
  1176. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  1177. bytes = 1;
  1178. op_type = VALUE_TYPE_I32;
  1179. goto op_atomic_cmpxchg;
  1180. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  1181. bytes = 2;
  1182. op_type = VALUE_TYPE_I32;
  1183. goto op_atomic_cmpxchg;
  1184. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  1185. bytes = 1;
  1186. op_type = VALUE_TYPE_I64;
  1187. goto op_atomic_cmpxchg;
  1188. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  1189. bytes = 2;
  1190. op_type = VALUE_TYPE_I64;
  1191. goto op_atomic_cmpxchg;
  1192. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  1193. bytes = 4;
  1194. op_type = VALUE_TYPE_I64;
  1195. op_atomic_cmpxchg:
  1196. if (!aot_compile_op_atomic_cmpxchg(comp_ctx, func_ctx,
  1197. op_type, align,
  1198. offset, bytes))
  1199. return false;
  1200. break;
  1201. COMPILE_ATOMIC_RMW(Add, ADD);
  1202. COMPILE_ATOMIC_RMW(Sub, SUB);
  1203. COMPILE_ATOMIC_RMW(And, AND);
  1204. COMPILE_ATOMIC_RMW(Or, OR);
  1205. COMPILE_ATOMIC_RMW(Xor, XOR);
  1206. COMPILE_ATOMIC_RMW(Xchg, XCHG);
  1207. build_atomic_rmw:
  1208. if (!aot_compile_op_atomic_rmw(comp_ctx, func_ctx,
  1209. bin_op, op_type, align,
  1210. offset, bytes))
  1211. return false;
  1212. break;
  1213. default:
  1214. aot_set_last_error("unsupported opcode");
  1215. return false;
  1216. }
  1217. break;
  1218. }
  1219. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  1220. #if WASM_ENABLE_SIMD != 0
  1221. case WASM_OP_SIMD_PREFIX:
  1222. {
  1223. if (!comp_ctx->enable_simd) {
  1224. goto unsupport_simd;
  1225. }
  1226. opcode = *frame_ip++;
  1227. /* follow the order of enum WASMSimdEXTOpcode in
  1228. wasm_opcode.h */
  1229. switch (opcode) {
  1230. /* Memory instruction */
  1231. case SIMD_v128_load:
  1232. {
  1233. read_leb_uint32(frame_ip, frame_ip_end, align);
  1234. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1235. if (!aot_compile_simd_v128_load(comp_ctx, func_ctx,
  1236. align, offset))
  1237. return false;
  1238. break;
  1239. }
  1240. case SIMD_v128_load8x8_s:
  1241. case SIMD_v128_load8x8_u:
  1242. case SIMD_v128_load16x4_s:
  1243. case SIMD_v128_load16x4_u:
  1244. case SIMD_v128_load32x2_s:
  1245. case SIMD_v128_load32x2_u:
  1246. {
  1247. read_leb_uint32(frame_ip, frame_ip_end, align);
  1248. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1249. if (!aot_compile_simd_load_extend(
  1250. comp_ctx, func_ctx, opcode, align, offset))
  1251. return false;
  1252. break;
  1253. }
  1254. case SIMD_v128_load8_splat:
  1255. case SIMD_v128_load16_splat:
  1256. case SIMD_v128_load32_splat:
  1257. case SIMD_v128_load64_splat:
  1258. {
  1259. read_leb_uint32(frame_ip, frame_ip_end, align);
  1260. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1261. if (!aot_compile_simd_load_splat(comp_ctx, func_ctx,
  1262. opcode, align, offset))
  1263. return false;
  1264. break;
  1265. }
  1266. case SIMD_v128_store:
  1267. {
  1268. read_leb_uint32(frame_ip, frame_ip_end, align);
  1269. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1270. if (!aot_compile_simd_v128_store(comp_ctx, func_ctx,
  1271. align, offset))
  1272. return false;
  1273. break;
  1274. }
  1275. /* Basic operation */
  1276. case SIMD_v128_const:
  1277. {
  1278. if (!aot_compile_simd_v128_const(comp_ctx, func_ctx,
  1279. frame_ip))
  1280. return false;
  1281. frame_ip += 16;
  1282. break;
  1283. }
  1284. case SIMD_v8x16_shuffle:
  1285. {
  1286. if (!aot_compile_simd_shuffle(comp_ctx, func_ctx,
  1287. frame_ip))
  1288. return false;
  1289. frame_ip += 16;
  1290. break;
  1291. }
  1292. case SIMD_v8x16_swizzle:
  1293. {
  1294. if (!aot_compile_simd_swizzle(comp_ctx, func_ctx))
  1295. return false;
  1296. break;
  1297. }
  1298. /* Splat operation */
  1299. case SIMD_i8x16_splat:
  1300. case SIMD_i16x8_splat:
  1301. case SIMD_i32x4_splat:
  1302. case SIMD_i64x2_splat:
  1303. case SIMD_f32x4_splat:
  1304. case SIMD_f64x2_splat:
  1305. {
  1306. if (!aot_compile_simd_splat(comp_ctx, func_ctx, opcode))
  1307. return false;
  1308. break;
  1309. }
  1310. /* Lane operation */
  1311. case SIMD_i8x16_extract_lane_s:
  1312. case SIMD_i8x16_extract_lane_u:
  1313. {
  1314. if (!aot_compile_simd_extract_i8x16(
  1315. comp_ctx, func_ctx, *frame_ip++,
  1316. SIMD_i8x16_extract_lane_s == opcode))
  1317. return false;
  1318. break;
  1319. }
  1320. case SIMD_i8x16_replace_lane:
  1321. {
  1322. if (!aot_compile_simd_replace_i8x16(comp_ctx, func_ctx,
  1323. *frame_ip++))
  1324. return false;
  1325. break;
  1326. }
  1327. case SIMD_i16x8_extract_lane_s:
  1328. case SIMD_i16x8_extract_lane_u:
  1329. {
  1330. if (!aot_compile_simd_extract_i16x8(
  1331. comp_ctx, func_ctx, *frame_ip++,
  1332. SIMD_i16x8_extract_lane_s == opcode))
  1333. return false;
  1334. break;
  1335. }
  1336. case SIMD_i16x8_replace_lane:
  1337. {
  1338. if (!aot_compile_simd_replace_i16x8(comp_ctx, func_ctx,
  1339. *frame_ip++))
  1340. return false;
  1341. break;
  1342. }
  1343. case SIMD_i32x4_extract_lane:
  1344. {
  1345. if (!aot_compile_simd_extract_i32x4(comp_ctx, func_ctx,
  1346. *frame_ip++))
  1347. return false;
  1348. break;
  1349. }
  1350. case SIMD_i32x4_replace_lane:
  1351. {
  1352. if (!aot_compile_simd_replace_i32x4(comp_ctx, func_ctx,
  1353. *frame_ip++))
  1354. return false;
  1355. break;
  1356. }
  1357. case SIMD_i64x2_extract_lane:
  1358. {
  1359. if (!aot_compile_simd_extract_i64x2(comp_ctx, func_ctx,
  1360. *frame_ip++))
  1361. return false;
  1362. break;
  1363. }
  1364. case SIMD_i64x2_replace_lane:
  1365. {
  1366. if (!aot_compile_simd_replace_i64x2(comp_ctx, func_ctx,
  1367. *frame_ip++))
  1368. return false;
  1369. break;
  1370. }
  1371. case SIMD_f32x4_extract_lane:
  1372. {
  1373. if (!aot_compile_simd_extract_f32x4(comp_ctx, func_ctx,
  1374. *frame_ip++))
  1375. return false;
  1376. break;
  1377. }
  1378. case SIMD_f32x4_replace_lane:
  1379. {
  1380. if (!aot_compile_simd_replace_f32x4(comp_ctx, func_ctx,
  1381. *frame_ip++))
  1382. return false;
  1383. break;
  1384. }
  1385. case SIMD_f64x2_extract_lane:
  1386. {
  1387. if (!aot_compile_simd_extract_f64x2(comp_ctx, func_ctx,
  1388. *frame_ip++))
  1389. return false;
  1390. break;
  1391. }
  1392. case SIMD_f64x2_replace_lane:
  1393. {
  1394. if (!aot_compile_simd_replace_f64x2(comp_ctx, func_ctx,
  1395. *frame_ip++))
  1396. return false;
  1397. break;
  1398. }
  1399. /* i8x16 Cmp */
  1400. case SIMD_i8x16_eq:
  1401. case SIMD_i8x16_ne:
  1402. case SIMD_i8x16_lt_s:
  1403. case SIMD_i8x16_lt_u:
  1404. case SIMD_i8x16_gt_s:
  1405. case SIMD_i8x16_gt_u:
  1406. case SIMD_i8x16_le_s:
  1407. case SIMD_i8x16_le_u:
  1408. case SIMD_i8x16_ge_s:
  1409. case SIMD_i8x16_ge_u:
  1410. {
  1411. if (!aot_compile_simd_i8x16_compare(
  1412. comp_ctx, func_ctx,
  1413. INT_EQ + opcode - SIMD_i8x16_eq))
  1414. return false;
  1415. break;
  1416. }
  1417. /* i16x8 Cmp */
  1418. case SIMD_i16x8_eq:
  1419. case SIMD_i16x8_ne:
  1420. case SIMD_i16x8_lt_s:
  1421. case SIMD_i16x8_lt_u:
  1422. case SIMD_i16x8_gt_s:
  1423. case SIMD_i16x8_gt_u:
  1424. case SIMD_i16x8_le_s:
  1425. case SIMD_i16x8_le_u:
  1426. case SIMD_i16x8_ge_s:
  1427. case SIMD_i16x8_ge_u:
  1428. {
  1429. if (!aot_compile_simd_i16x8_compare(
  1430. comp_ctx, func_ctx,
  1431. INT_EQ + opcode - SIMD_i16x8_eq))
  1432. return false;
  1433. break;
  1434. }
  1435. /* i32x4 Cmp */
  1436. case SIMD_i32x4_eq:
  1437. case SIMD_i32x4_ne:
  1438. case SIMD_i32x4_lt_s:
  1439. case SIMD_i32x4_lt_u:
  1440. case SIMD_i32x4_gt_s:
  1441. case SIMD_i32x4_gt_u:
  1442. case SIMD_i32x4_le_s:
  1443. case SIMD_i32x4_le_u:
  1444. case SIMD_i32x4_ge_s:
  1445. case SIMD_i32x4_ge_u:
  1446. {
  1447. if (!aot_compile_simd_i32x4_compare(
  1448. comp_ctx, func_ctx,
  1449. INT_EQ + opcode - SIMD_i32x4_eq))
  1450. return false;
  1451. break;
  1452. }
  1453. /* f32x4 Cmp */
  1454. case SIMD_f32x4_eq:
  1455. case SIMD_f32x4_ne:
  1456. case SIMD_f32x4_lt:
  1457. case SIMD_f32x4_gt:
  1458. case SIMD_f32x4_le:
  1459. case SIMD_f32x4_ge:
  1460. {
  1461. if (!aot_compile_simd_f32x4_compare(
  1462. comp_ctx, func_ctx,
  1463. FLOAT_EQ + opcode - SIMD_f32x4_eq))
  1464. return false;
  1465. break;
  1466. }
  1467. /* f64x2 Cmp */
  1468. case SIMD_f64x2_eq:
  1469. case SIMD_f64x2_ne:
  1470. case SIMD_f64x2_lt:
  1471. case SIMD_f64x2_gt:
  1472. case SIMD_f64x2_le:
  1473. case SIMD_f64x2_ge:
  1474. {
  1475. if (!aot_compile_simd_f64x2_compare(
  1476. comp_ctx, func_ctx,
  1477. FLOAT_EQ + opcode - SIMD_f64x2_eq))
  1478. return false;
  1479. break;
  1480. }
  1481. /* v128 Op */
  1482. case SIMD_v128_not:
  1483. case SIMD_v128_and:
  1484. case SIMD_v128_andnot:
  1485. case SIMD_v128_or:
  1486. case SIMD_v128_xor:
  1487. case SIMD_v128_bitselect:
  1488. {
  1489. if (!aot_compile_simd_v128_bitwise(comp_ctx, func_ctx,
  1490. V128_NOT + opcode
  1491. - SIMD_v128_not))
  1492. return false;
  1493. break;
  1494. }
  1495. case SIMD_v128_any_true:
  1496. {
  1497. if (!aot_compile_simd_v128_any_true(comp_ctx, func_ctx))
  1498. return false;
  1499. break;
  1500. }
  1501. /* Load Lane Op */
  1502. case SIMD_v128_load8_lane:
  1503. case SIMD_v128_load16_lane:
  1504. case SIMD_v128_load32_lane:
  1505. case SIMD_v128_load64_lane:
  1506. {
  1507. read_leb_uint32(frame_ip, frame_ip_end, align);
  1508. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1509. if (!aot_compile_simd_load_lane(comp_ctx, func_ctx,
  1510. opcode, align, offset,
  1511. *frame_ip++))
  1512. return false;
  1513. break;
  1514. }
  1515. case SIMD_v128_store8_lane:
  1516. case SIMD_v128_store16_lane:
  1517. case SIMD_v128_store32_lane:
  1518. case SIMD_v128_store64_lane:
  1519. {
  1520. read_leb_uint32(frame_ip, frame_ip_end, align);
  1521. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1522. if (!aot_compile_simd_store_lane(comp_ctx, func_ctx,
  1523. opcode, align, offset,
  1524. *frame_ip++))
  1525. return false;
  1526. break;
  1527. }
  1528. case SIMD_v128_load32_zero:
  1529. case SIMD_v128_load64_zero:
  1530. {
  1531. read_leb_uint32(frame_ip, frame_ip_end, align);
  1532. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1533. if (!aot_compile_simd_load_zero(comp_ctx, func_ctx,
  1534. opcode, align, offset))
  1535. return false;
  1536. break;
  1537. }
  1538. /* Float conversion */
  1539. case SIMD_f32x4_demote_f64x2_zero:
  1540. {
  1541. if (!aot_compile_simd_f64x2_demote(comp_ctx, func_ctx))
  1542. return false;
  1543. break;
  1544. }
  1545. case SIMD_f64x2_promote_low_f32x4_zero:
  1546. {
  1547. if (!aot_compile_simd_f32x4_promote(comp_ctx, func_ctx))
  1548. return false;
  1549. break;
  1550. }
  1551. /* i8x16 Op */
  1552. case SIMD_i8x16_abs:
  1553. {
  1554. if (!aot_compile_simd_i8x16_abs(comp_ctx, func_ctx))
  1555. return false;
  1556. break;
  1557. }
  1558. case SIMD_i8x16_neg:
  1559. {
  1560. if (!aot_compile_simd_i8x16_neg(comp_ctx, func_ctx))
  1561. return false;
  1562. break;
  1563. }
  1564. case SIMD_i8x16_popcnt:
  1565. {
  1566. if (!aot_compile_simd_i8x16_popcnt(comp_ctx, func_ctx))
  1567. return false;
  1568. break;
  1569. }
  1570. case SIMD_i8x16_all_true:
  1571. {
  1572. if (!aot_compile_simd_i8x16_all_true(comp_ctx,
  1573. func_ctx))
  1574. return false;
  1575. break;
  1576. }
  1577. case SIMD_i8x16_bitmask:
  1578. {
  1579. if (!aot_compile_simd_i8x16_bitmask(comp_ctx, func_ctx))
  1580. return false;
  1581. break;
  1582. }
  1583. case SIMD_i8x16_narrow_i16x8_s:
  1584. case SIMD_i8x16_narrow_i16x8_u:
  1585. {
  1586. if (!aot_compile_simd_i8x16_narrow_i16x8(
  1587. comp_ctx, func_ctx,
  1588. (opcode == SIMD_i8x16_narrow_i16x8_s)))
  1589. return false;
  1590. break;
  1591. }
  1592. case SIMD_f32x4_ceil:
  1593. {
  1594. if (!aot_compile_simd_f32x4_ceil(comp_ctx, func_ctx))
  1595. return false;
  1596. break;
  1597. }
  1598. case SIMD_f32x4_floor:
  1599. {
  1600. if (!aot_compile_simd_f32x4_floor(comp_ctx, func_ctx))
  1601. return false;
  1602. break;
  1603. }
  1604. case SIMD_f32x4_trunc:
  1605. {
  1606. if (!aot_compile_simd_f32x4_trunc(comp_ctx, func_ctx))
  1607. return false;
  1608. break;
  1609. }
  1610. case SIMD_f32x4_nearest:
  1611. {
  1612. if (!aot_compile_simd_f32x4_nearest(comp_ctx, func_ctx))
  1613. return false;
  1614. break;
  1615. }
  1616. case SIMD_i8x16_shl:
  1617. case SIMD_i8x16_shr_s:
  1618. case SIMD_i8x16_shr_u:
  1619. {
  1620. if (!aot_compile_simd_i8x16_shift(comp_ctx, func_ctx,
  1621. INT_SHL + opcode
  1622. - SIMD_i8x16_shl))
  1623. return false;
  1624. break;
  1625. }
  1626. case SIMD_i8x16_add:
  1627. {
  1628. if (!aot_compile_simd_i8x16_arith(comp_ctx, func_ctx,
  1629. V128_ADD))
  1630. return false;
  1631. break;
  1632. }
  1633. case SIMD_i8x16_add_sat_s:
  1634. case SIMD_i8x16_add_sat_u:
  1635. {
  1636. if (!aot_compile_simd_i8x16_saturate(
  1637. comp_ctx, func_ctx, V128_ADD,
  1638. opcode == SIMD_i8x16_add_sat_s))
  1639. return false;
  1640. break;
  1641. }
  1642. case SIMD_i8x16_sub:
  1643. {
  1644. if (!aot_compile_simd_i8x16_arith(comp_ctx, func_ctx,
  1645. V128_SUB))
  1646. return false;
  1647. break;
  1648. }
  1649. case SIMD_i8x16_sub_sat_s:
  1650. case SIMD_i8x16_sub_sat_u:
  1651. {
  1652. if (!aot_compile_simd_i8x16_saturate(
  1653. comp_ctx, func_ctx, V128_SUB,
  1654. opcode == SIMD_i8x16_sub_sat_s))
  1655. return false;
  1656. break;
  1657. }
  1658. case SIMD_f64x2_ceil:
  1659. {
  1660. if (!aot_compile_simd_f64x2_ceil(comp_ctx, func_ctx))
  1661. return false;
  1662. break;
  1663. }
  1664. case SIMD_f64x2_floor:
  1665. {
  1666. if (!aot_compile_simd_f64x2_floor(comp_ctx, func_ctx))
  1667. return false;
  1668. break;
  1669. }
  1670. case SIMD_i8x16_min_s:
  1671. case SIMD_i8x16_min_u:
  1672. {
  1673. if (!aot_compile_simd_i8x16_cmp(
  1674. comp_ctx, func_ctx, V128_MIN,
  1675. opcode == SIMD_i8x16_min_s))
  1676. return false;
  1677. break;
  1678. }
  1679. case SIMD_i8x16_max_s:
  1680. case SIMD_i8x16_max_u:
  1681. {
  1682. if (!aot_compile_simd_i8x16_cmp(
  1683. comp_ctx, func_ctx, V128_MAX,
  1684. opcode == SIMD_i8x16_max_s))
  1685. return false;
  1686. break;
  1687. }
  1688. case SIMD_f64x2_trunc:
  1689. {
  1690. if (!aot_compile_simd_f64x2_trunc(comp_ctx, func_ctx))
  1691. return false;
  1692. break;
  1693. }
  1694. case SIMD_i8x16_avgr_u:
  1695. {
  1696. if (!aot_compile_simd_i8x16_avgr_u(comp_ctx, func_ctx))
  1697. return false;
  1698. break;
  1699. }
  1700. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  1701. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  1702. {
  1703. if (!aot_compile_simd_i16x8_extadd_pairwise_i8x16(
  1704. comp_ctx, func_ctx,
  1705. SIMD_i16x8_extadd_pairwise_i8x16_s == opcode))
  1706. return false;
  1707. break;
  1708. }
  1709. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  1710. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  1711. {
  1712. if (!aot_compile_simd_i32x4_extadd_pairwise_i16x8(
  1713. comp_ctx, func_ctx,
  1714. SIMD_i32x4_extadd_pairwise_i16x8_s == opcode))
  1715. return false;
  1716. break;
  1717. }
  1718. /* i16x8 Op */
  1719. case SIMD_i16x8_abs:
  1720. {
  1721. if (!aot_compile_simd_i16x8_abs(comp_ctx, func_ctx))
  1722. return false;
  1723. break;
  1724. }
  1725. case SIMD_i16x8_neg:
  1726. {
  1727. if (!aot_compile_simd_i16x8_neg(comp_ctx, func_ctx))
  1728. return false;
  1729. break;
  1730. }
  1731. case SIMD_i16x8_q15mulr_sat_s:
  1732. {
  1733. if (!aot_compile_simd_i16x8_q15mulr_sat(comp_ctx,
  1734. func_ctx))
  1735. return false;
  1736. break;
  1737. }
  1738. case SIMD_i16x8_all_true:
  1739. {
  1740. if (!aot_compile_simd_i16x8_all_true(comp_ctx,
  1741. func_ctx))
  1742. return false;
  1743. break;
  1744. }
  1745. case SIMD_i16x8_bitmask:
  1746. {
  1747. if (!aot_compile_simd_i16x8_bitmask(comp_ctx, func_ctx))
  1748. return false;
  1749. break;
  1750. }
  1751. case SIMD_i16x8_narrow_i32x4_s:
  1752. case SIMD_i16x8_narrow_i32x4_u:
  1753. {
  1754. if (!aot_compile_simd_i16x8_narrow_i32x4(
  1755. comp_ctx, func_ctx,
  1756. SIMD_i16x8_narrow_i32x4_s == opcode))
  1757. return false;
  1758. break;
  1759. }
  1760. case SIMD_i16x8_extend_low_i8x16_s:
  1761. case SIMD_i16x8_extend_high_i8x16_s:
  1762. {
  1763. if (!aot_compile_simd_i16x8_extend_i8x16(
  1764. comp_ctx, func_ctx,
  1765. SIMD_i16x8_extend_low_i8x16_s == opcode, true))
  1766. return false;
  1767. break;
  1768. }
  1769. case SIMD_i16x8_extend_low_i8x16_u:
  1770. case SIMD_i16x8_extend_high_i8x16_u:
  1771. {
  1772. if (!aot_compile_simd_i16x8_extend_i8x16(
  1773. comp_ctx, func_ctx,
  1774. SIMD_i16x8_extend_low_i8x16_u == opcode, false))
  1775. return false;
  1776. break;
  1777. }
  1778. case SIMD_i16x8_shl:
  1779. case SIMD_i16x8_shr_s:
  1780. case SIMD_i16x8_shr_u:
  1781. {
  1782. if (!aot_compile_simd_i16x8_shift(comp_ctx, func_ctx,
  1783. INT_SHL + opcode
  1784. - SIMD_i16x8_shl))
  1785. return false;
  1786. break;
  1787. }
  1788. case SIMD_i16x8_add:
  1789. {
  1790. if (!aot_compile_simd_i16x8_arith(comp_ctx, func_ctx,
  1791. V128_ADD))
  1792. return false;
  1793. break;
  1794. }
  1795. case SIMD_i16x8_add_sat_s:
  1796. case SIMD_i16x8_add_sat_u:
  1797. {
  1798. if (!aot_compile_simd_i16x8_saturate(
  1799. comp_ctx, func_ctx, V128_ADD,
  1800. opcode == SIMD_i16x8_add_sat_s ? true : false))
  1801. return false;
  1802. break;
  1803. }
  1804. case SIMD_i16x8_sub:
  1805. {
  1806. if (!aot_compile_simd_i16x8_arith(comp_ctx, func_ctx,
  1807. V128_SUB))
  1808. return false;
  1809. break;
  1810. }
  1811. case SIMD_i16x8_sub_sat_s:
  1812. case SIMD_i16x8_sub_sat_u:
  1813. {
  1814. if (!aot_compile_simd_i16x8_saturate(
  1815. comp_ctx, func_ctx, V128_SUB,
  1816. opcode == SIMD_i16x8_sub_sat_s ? true : false))
  1817. return false;
  1818. break;
  1819. }
  1820. case SIMD_f64x2_nearest:
  1821. {
  1822. if (!aot_compile_simd_f64x2_nearest(comp_ctx, func_ctx))
  1823. return false;
  1824. break;
  1825. }
  1826. case SIMD_i16x8_mul:
  1827. {
  1828. if (!aot_compile_simd_i16x8_arith(comp_ctx, func_ctx,
  1829. V128_MUL))
  1830. return false;
  1831. break;
  1832. }
  1833. case SIMD_i16x8_min_s:
  1834. case SIMD_i16x8_min_u:
  1835. {
  1836. if (!aot_compile_simd_i16x8_cmp(
  1837. comp_ctx, func_ctx, V128_MIN,
  1838. opcode == SIMD_i16x8_min_s))
  1839. return false;
  1840. break;
  1841. }
  1842. case SIMD_i16x8_max_s:
  1843. case SIMD_i16x8_max_u:
  1844. {
  1845. if (!aot_compile_simd_i16x8_cmp(
  1846. comp_ctx, func_ctx, V128_MAX,
  1847. opcode == SIMD_i16x8_max_s))
  1848. return false;
  1849. break;
  1850. }
  1851. case SIMD_i16x8_avgr_u:
  1852. {
  1853. if (!aot_compile_simd_i16x8_avgr_u(comp_ctx, func_ctx))
  1854. return false;
  1855. break;
  1856. }
  1857. case SIMD_i16x8_extmul_low_i8x16_s:
  1858. case SIMD_i16x8_extmul_high_i8x16_s:
  1859. {
  1860. if (!(aot_compile_simd_i16x8_extmul_i8x16(
  1861. comp_ctx, func_ctx,
  1862. SIMD_i16x8_extmul_low_i8x16_s == opcode, true)))
  1863. return false;
  1864. break;
  1865. }
  1866. case SIMD_i16x8_extmul_low_i8x16_u:
  1867. case SIMD_i16x8_extmul_high_i8x16_u:
  1868. {
  1869. if (!(aot_compile_simd_i16x8_extmul_i8x16(
  1870. comp_ctx, func_ctx,
  1871. SIMD_i16x8_extmul_low_i8x16_u == opcode,
  1872. false)))
  1873. return false;
  1874. break;
  1875. }
  1876. /* i32x4 Op */
  1877. case SIMD_i32x4_abs:
  1878. {
  1879. if (!aot_compile_simd_i32x4_abs(comp_ctx, func_ctx))
  1880. return false;
  1881. break;
  1882. }
  1883. case SIMD_i32x4_neg:
  1884. {
  1885. if (!aot_compile_simd_i32x4_neg(comp_ctx, func_ctx))
  1886. return false;
  1887. break;
  1888. }
  1889. case SIMD_i32x4_all_true:
  1890. {
  1891. if (!aot_compile_simd_i32x4_all_true(comp_ctx,
  1892. func_ctx))
  1893. return false;
  1894. break;
  1895. }
  1896. case SIMD_i32x4_bitmask:
  1897. {
  1898. if (!aot_compile_simd_i32x4_bitmask(comp_ctx, func_ctx))
  1899. return false;
  1900. break;
  1901. }
  1902. case SIMD_i32x4_narrow_i64x2_s:
  1903. case SIMD_i32x4_narrow_i64x2_u:
  1904. {
  1905. if (!aot_compile_simd_i32x4_narrow_i64x2(
  1906. comp_ctx, func_ctx,
  1907. SIMD_i32x4_narrow_i64x2_s == opcode))
  1908. return false;
  1909. break;
  1910. }
  1911. case SIMD_i32x4_extend_low_i16x8_s:
  1912. case SIMD_i32x4_extend_high_i16x8_s:
  1913. {
  1914. if (!aot_compile_simd_i32x4_extend_i16x8(
  1915. comp_ctx, func_ctx,
  1916. SIMD_i32x4_extend_low_i16x8_s == opcode, true))
  1917. return false;
  1918. break;
  1919. }
  1920. case SIMD_i32x4_extend_low_i16x8_u:
  1921. case SIMD_i32x4_extend_high_i16x8_u:
  1922. {
  1923. if (!aot_compile_simd_i32x4_extend_i16x8(
  1924. comp_ctx, func_ctx,
  1925. SIMD_i32x4_extend_low_i16x8_u == opcode, false))
  1926. return false;
  1927. break;
  1928. }
  1929. case SIMD_i32x4_shl:
  1930. case SIMD_i32x4_shr_s:
  1931. case SIMD_i32x4_shr_u:
  1932. {
  1933. if (!aot_compile_simd_i32x4_shift(comp_ctx, func_ctx,
  1934. INT_SHL + opcode
  1935. - SIMD_i32x4_shl))
  1936. return false;
  1937. break;
  1938. }
  1939. case SIMD_i32x4_add:
  1940. {
  1941. if (!aot_compile_simd_i32x4_arith(comp_ctx, func_ctx,
  1942. V128_ADD))
  1943. return false;
  1944. break;
  1945. }
  1946. case SIMD_i32x4_add_sat_s:
  1947. case SIMD_i32x4_add_sat_u:
  1948. {
  1949. if (!aot_compile_simd_i32x4_saturate(
  1950. comp_ctx, func_ctx, V128_ADD,
  1951. opcode == SIMD_i32x4_add_sat_s))
  1952. return false;
  1953. break;
  1954. }
  1955. case SIMD_i32x4_sub:
  1956. {
  1957. if (!aot_compile_simd_i32x4_arith(comp_ctx, func_ctx,
  1958. V128_SUB))
  1959. return false;
  1960. break;
  1961. }
  1962. case SIMD_i32x4_sub_sat_s:
  1963. case SIMD_i32x4_sub_sat_u:
  1964. {
  1965. if (!aot_compile_simd_i32x4_saturate(
  1966. comp_ctx, func_ctx, V128_SUB,
  1967. opcode == SIMD_i32x4_add_sat_s))
  1968. return false;
  1969. break;
  1970. }
  1971. case SIMD_i32x4_mul:
  1972. {
  1973. if (!aot_compile_simd_i32x4_arith(comp_ctx, func_ctx,
  1974. V128_MUL))
  1975. return false;
  1976. break;
  1977. }
  1978. case SIMD_i32x4_min_s:
  1979. case SIMD_i32x4_min_u:
  1980. {
  1981. if (!aot_compile_simd_i32x4_cmp(
  1982. comp_ctx, func_ctx, V128_MIN,
  1983. SIMD_i32x4_min_s == opcode))
  1984. return false;
  1985. break;
  1986. }
  1987. case SIMD_i32x4_max_s:
  1988. case SIMD_i32x4_max_u:
  1989. {
  1990. if (!aot_compile_simd_i32x4_cmp(
  1991. comp_ctx, func_ctx, V128_MAX,
  1992. SIMD_i32x4_max_s == opcode))
  1993. return false;
  1994. break;
  1995. }
  1996. case SIMD_i32x4_dot_i16x8_s:
  1997. {
  1998. if (!aot_compile_simd_i32x4_dot_i16x8(comp_ctx,
  1999. func_ctx))
  2000. return false;
  2001. break;
  2002. }
  2003. case SIMD_i32x4_avgr_u:
  2004. {
  2005. if (!aot_compile_simd_i32x4_avgr_u(comp_ctx, func_ctx))
  2006. return false;
  2007. break;
  2008. }
  2009. case SIMD_i32x4_extmul_low_i16x8_s:
  2010. case SIMD_i32x4_extmul_high_i16x8_s:
  2011. {
  2012. if (!aot_compile_simd_i32x4_extmul_i16x8(
  2013. comp_ctx, func_ctx,
  2014. SIMD_i32x4_extmul_low_i16x8_s == opcode, true))
  2015. return false;
  2016. break;
  2017. }
  2018. case SIMD_i32x4_extmul_low_i16x8_u:
  2019. case SIMD_i32x4_extmul_high_i16x8_u:
  2020. {
  2021. if (!aot_compile_simd_i32x4_extmul_i16x8(
  2022. comp_ctx, func_ctx,
  2023. SIMD_i32x4_extmul_low_i16x8_u == opcode, false))
  2024. return false;
  2025. break;
  2026. }
  2027. /* i64x2 Op */
  2028. case SIMD_i64x2_abs:
  2029. {
  2030. if (!aot_compile_simd_i64x2_abs(comp_ctx, func_ctx))
  2031. return false;
  2032. break;
  2033. }
  2034. case SIMD_i64x2_neg:
  2035. {
  2036. if (!aot_compile_simd_i64x2_neg(comp_ctx, func_ctx))
  2037. return false;
  2038. break;
  2039. }
  2040. case SIMD_i64x2_all_true:
  2041. {
  2042. if (!aot_compile_simd_i64x2_all_true(comp_ctx,
  2043. func_ctx))
  2044. return false;
  2045. break;
  2046. }
  2047. case SIMD_i64x2_bitmask:
  2048. {
  2049. if (!aot_compile_simd_i64x2_bitmask(comp_ctx, func_ctx))
  2050. return false;
  2051. break;
  2052. }
  2053. case SIMD_i64x2_extend_low_i32x4_s:
  2054. case SIMD_i64x2_extend_high_i32x4_s:
  2055. {
  2056. if (!aot_compile_simd_i64x2_extend_i32x4(
  2057. comp_ctx, func_ctx,
  2058. SIMD_i64x2_extend_low_i32x4_s == opcode, true))
  2059. return false;
  2060. break;
  2061. }
  2062. case SIMD_i64x2_extend_low_i32x4_u:
  2063. case SIMD_i64x2_extend_high_i32x4_u:
  2064. {
  2065. if (!aot_compile_simd_i64x2_extend_i32x4(
  2066. comp_ctx, func_ctx,
  2067. SIMD_i64x2_extend_low_i32x4_u == opcode, false))
  2068. return false;
  2069. break;
  2070. }
  2071. case SIMD_i64x2_shl:
  2072. case SIMD_i64x2_shr_s:
  2073. case SIMD_i64x2_shr_u:
  2074. {
  2075. if (!aot_compile_simd_i64x2_shift(comp_ctx, func_ctx,
  2076. INT_SHL + opcode
  2077. - SIMD_i64x2_shl))
  2078. return false;
  2079. break;
  2080. }
  2081. case SIMD_i64x2_add:
  2082. {
  2083. if (!aot_compile_simd_i64x2_arith(comp_ctx, func_ctx,
  2084. V128_ADD))
  2085. return false;
  2086. break;
  2087. }
  2088. case SIMD_i64x2_sub:
  2089. {
  2090. if (!aot_compile_simd_i64x2_arith(comp_ctx, func_ctx,
  2091. V128_SUB))
  2092. return false;
  2093. break;
  2094. }
  2095. case SIMD_i64x2_mul:
  2096. {
  2097. if (!aot_compile_simd_i64x2_arith(comp_ctx, func_ctx,
  2098. V128_MUL))
  2099. return false;
  2100. break;
  2101. }
  2102. case SIMD_i64x2_eq:
  2103. case SIMD_i64x2_ne:
  2104. case SIMD_i64x2_lt_s:
  2105. case SIMD_i64x2_gt_s:
  2106. case SIMD_i64x2_le_s:
  2107. case SIMD_i64x2_ge_s:
  2108. {
  2109. IntCond icond[] = { INT_EQ, INT_NE, INT_LT_S,
  2110. INT_GT_S, INT_LE_S, INT_GE_S };
  2111. if (!aot_compile_simd_i64x2_compare(
  2112. comp_ctx, func_ctx,
  2113. icond[opcode - SIMD_i64x2_eq]))
  2114. return false;
  2115. break;
  2116. }
  2117. case SIMD_i64x2_extmul_low_i32x4_s:
  2118. case SIMD_i64x2_extmul_high_i32x4_s:
  2119. {
  2120. if (!aot_compile_simd_i64x2_extmul_i32x4(
  2121. comp_ctx, func_ctx,
  2122. SIMD_i64x2_extmul_low_i32x4_s == opcode, true))
  2123. return false;
  2124. break;
  2125. }
  2126. case SIMD_i64x2_extmul_low_i32x4_u:
  2127. case SIMD_i64x2_extmul_high_i32x4_u:
  2128. {
  2129. if (!aot_compile_simd_i64x2_extmul_i32x4(
  2130. comp_ctx, func_ctx,
  2131. SIMD_i64x2_extmul_low_i32x4_u == opcode, false))
  2132. return false;
  2133. break;
  2134. }
  2135. /* f32x4 Op */
  2136. case SIMD_f32x4_abs:
  2137. {
  2138. if (!aot_compile_simd_f32x4_abs(comp_ctx, func_ctx))
  2139. return false;
  2140. break;
  2141. }
  2142. case SIMD_f32x4_neg:
  2143. {
  2144. if (!aot_compile_simd_f32x4_neg(comp_ctx, func_ctx))
  2145. return false;
  2146. break;
  2147. }
  2148. case SIMD_f32x4_round:
  2149. {
  2150. if (!aot_compile_simd_f32x4_round(comp_ctx, func_ctx))
  2151. return false;
  2152. break;
  2153. }
  2154. case SIMD_f32x4_sqrt:
  2155. {
  2156. if (!aot_compile_simd_f32x4_sqrt(comp_ctx, func_ctx))
  2157. return false;
  2158. break;
  2159. }
  2160. case SIMD_f32x4_add:
  2161. case SIMD_f32x4_sub:
  2162. case SIMD_f32x4_mul:
  2163. case SIMD_f32x4_div:
  2164. {
  2165. if (!aot_compile_simd_f32x4_arith(comp_ctx, func_ctx,
  2166. FLOAT_ADD + opcode
  2167. - SIMD_f32x4_add))
  2168. return false;
  2169. break;
  2170. }
  2171. case SIMD_f32x4_min:
  2172. case SIMD_f32x4_max:
  2173. {
  2174. if (!aot_compile_simd_f32x4_min_max(
  2175. comp_ctx, func_ctx, SIMD_f32x4_min == opcode))
  2176. return false;
  2177. break;
  2178. }
  2179. case SIMD_f32x4_pmin:
  2180. case SIMD_f32x4_pmax:
  2181. {
  2182. if (!aot_compile_simd_f32x4_pmin_pmax(
  2183. comp_ctx, func_ctx, SIMD_f32x4_pmin == opcode))
  2184. return false;
  2185. break;
  2186. }
  2187. /* f64x2 Op */
  2188. case SIMD_f64x2_abs:
  2189. {
  2190. if (!aot_compile_simd_f64x2_abs(comp_ctx, func_ctx))
  2191. return false;
  2192. break;
  2193. }
  2194. case SIMD_f64x2_neg:
  2195. {
  2196. if (!aot_compile_simd_f64x2_neg(comp_ctx, func_ctx))
  2197. return false;
  2198. break;
  2199. }
  2200. case SIMD_f64x2_round:
  2201. {
  2202. if (!aot_compile_simd_f64x2_round(comp_ctx, func_ctx))
  2203. return false;
  2204. break;
  2205. }
  2206. case SIMD_f64x2_sqrt:
  2207. {
  2208. if (!aot_compile_simd_f64x2_sqrt(comp_ctx, func_ctx))
  2209. return false;
  2210. break;
  2211. }
  2212. case SIMD_f64x2_add:
  2213. case SIMD_f64x2_sub:
  2214. case SIMD_f64x2_mul:
  2215. case SIMD_f64x2_div:
  2216. {
  2217. if (!aot_compile_simd_f64x2_arith(comp_ctx, func_ctx,
  2218. FLOAT_ADD + opcode
  2219. - SIMD_f64x2_add))
  2220. return false;
  2221. break;
  2222. }
  2223. case SIMD_f64x2_min:
  2224. case SIMD_f64x2_max:
  2225. {
  2226. if (!aot_compile_simd_f64x2_min_max(
  2227. comp_ctx, func_ctx, SIMD_f64x2_min == opcode))
  2228. return false;
  2229. break;
  2230. }
  2231. case SIMD_f64x2_pmin:
  2232. case SIMD_f64x2_pmax:
  2233. {
  2234. if (!aot_compile_simd_f64x2_pmin_pmax(
  2235. comp_ctx, func_ctx, SIMD_f64x2_pmin == opcode))
  2236. return false;
  2237. break;
  2238. }
  2239. /* Conversion Op */
  2240. case SIMD_i32x4_trunc_sat_f32x4_s:
  2241. case SIMD_i32x4_trunc_sat_f32x4_u:
  2242. {
  2243. if (!aot_compile_simd_i32x4_trunc_sat_f32x4(
  2244. comp_ctx, func_ctx,
  2245. SIMD_i32x4_trunc_sat_f32x4_s == opcode))
  2246. return false;
  2247. break;
  2248. }
  2249. case SIMD_f32x4_convert_i32x4_s:
  2250. case SIMD_f32x4_convert_i32x4_u:
  2251. {
  2252. if (!aot_compile_simd_f32x4_convert_i32x4(
  2253. comp_ctx, func_ctx,
  2254. SIMD_f32x4_convert_i32x4_s == opcode))
  2255. return false;
  2256. break;
  2257. }
  2258. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  2259. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  2260. {
  2261. if (!aot_compile_simd_i32x4_trunc_sat_f64x2(
  2262. comp_ctx, func_ctx,
  2263. SIMD_i32x4_trunc_sat_f64x2_s_zero == opcode))
  2264. return false;
  2265. break;
  2266. }
  2267. case SIMD_f64x2_convert_low_i32x4_s:
  2268. case SIMD_f64x2_convert_low_i32x4_u:
  2269. {
  2270. if (!aot_compile_simd_f64x2_convert_i32x4(
  2271. comp_ctx, func_ctx,
  2272. SIMD_f64x2_convert_low_i32x4_s == opcode))
  2273. return false;
  2274. break;
  2275. }
  2276. default:
  2277. aot_set_last_error("unsupported SIMD opcode");
  2278. return false;
  2279. }
  2280. break;
  2281. }
  2282. #endif /* end of WASM_ENABLE_SIMD */
  2283. default:
  2284. aot_set_last_error("unsupported opcode");
  2285. return false;
  2286. }
  2287. }
  2288. /* Move func_return block to the bottom */
  2289. if (func_ctx->func_return_block) {
  2290. LLVMBasicBlockRef last_block = LLVMGetLastBasicBlock(func_ctx->func);
  2291. if (last_block != func_ctx->func_return_block)
  2292. LLVMMoveBasicBlockAfter(func_ctx->func_return_block, last_block);
  2293. }
  2294. /* Move got_exception block to the bottom */
  2295. if (func_ctx->got_exception_block) {
  2296. LLVMBasicBlockRef last_block = LLVMGetLastBasicBlock(func_ctx->func);
  2297. if (last_block != func_ctx->got_exception_block)
  2298. LLVMMoveBasicBlockAfter(func_ctx->got_exception_block, last_block);
  2299. }
  2300. return true;
  2301. #if WASM_ENABLE_SIMD != 0
  2302. unsupport_simd:
  2303. aot_set_last_error("SIMD instruction was found, "
  2304. "try removing --disable-simd option");
  2305. return false;
  2306. #endif
  2307. #if WASM_ENABLE_REF_TYPES != 0
  2308. unsupport_ref_types:
  2309. aot_set_last_error("reference type instruction was found, "
  2310. "try removing --disable-ref-types option");
  2311. return false;
  2312. #endif
  2313. #if WASM_ENABLE_BULK_MEMORY != 0
  2314. unsupport_bulk_memory:
  2315. aot_set_last_error("bulk memory instruction was found, "
  2316. "try removing --disable-bulk-memory option");
  2317. return false;
  2318. #endif
  2319. fail:
  2320. return false;
  2321. }
  2322. static bool
  2323. veriy_module(AOTCompContext *comp_ctx)
  2324. {
  2325. char *msg = NULL;
  2326. bool ret;
  2327. ret = LLVMVerifyModule(comp_ctx->module, LLVMPrintMessageAction, &msg);
  2328. if (!ret && msg) {
  2329. if (msg[0] != '\0') {
  2330. aot_set_last_error(msg);
  2331. LLVMDisposeMessage(msg);
  2332. return false;
  2333. }
  2334. LLVMDisposeMessage(msg);
  2335. }
  2336. return true;
  2337. }
  2338. /* Check whether the target supports hardware atomic instructions */
  2339. static bool
  2340. aot_require_lower_atomic_pass(AOTCompContext *comp_ctx)
  2341. {
  2342. bool ret = false;
  2343. if (!strncmp(comp_ctx->target_arch, "riscv", 5)) {
  2344. char *feature =
  2345. LLVMGetTargetMachineFeatureString(comp_ctx->target_machine);
  2346. if (feature) {
  2347. if (!strstr(feature, "+a")) {
  2348. ret = true;
  2349. }
  2350. LLVMDisposeMessage(feature);
  2351. }
  2352. }
  2353. return ret;
  2354. }
  2355. /* Check whether the target needs to expand switch to if/else */
  2356. static bool
  2357. aot_require_lower_switch_pass(AOTCompContext *comp_ctx)
  2358. {
  2359. bool ret = false;
  2360. /* IR switch/case will cause .rodata relocation on riscv/xtensa */
  2361. if (!strncmp(comp_ctx->target_arch, "riscv", 5)
  2362. || !strncmp(comp_ctx->target_arch, "xtensa", 6)) {
  2363. ret = true;
  2364. }
  2365. return ret;
  2366. }
  2367. static bool
  2368. apply_passes_for_indirect_mode(AOTCompContext *comp_ctx)
  2369. {
  2370. LLVMPassManagerRef common_pass_mgr;
  2371. if (!(common_pass_mgr = LLVMCreatePassManager())) {
  2372. aot_set_last_error("create pass manager failed");
  2373. return false;
  2374. }
  2375. aot_add_expand_memory_op_pass(common_pass_mgr);
  2376. if (aot_require_lower_atomic_pass(comp_ctx))
  2377. LLVMAddLowerAtomicPass(common_pass_mgr);
  2378. if (aot_require_lower_switch_pass(comp_ctx))
  2379. LLVMAddLowerSwitchPass(common_pass_mgr);
  2380. LLVMRunPassManager(common_pass_mgr, comp_ctx->module);
  2381. LLVMDisposePassManager(common_pass_mgr);
  2382. return true;
  2383. }
  2384. bool
  2385. aot_compile_wasm(AOTCompContext *comp_ctx)
  2386. {
  2387. uint32 i;
  2388. if (!aot_validate_wasm(comp_ctx)) {
  2389. return false;
  2390. }
  2391. bh_print_time("Begin to compile WASM bytecode to LLVM IR");
  2392. for (i = 0; i < comp_ctx->func_ctx_count; i++) {
  2393. if (!aot_compile_func(comp_ctx, i)) {
  2394. return false;
  2395. }
  2396. }
  2397. #if WASM_ENABLE_DEBUG_AOT != 0
  2398. LLVMDIBuilderFinalize(comp_ctx->debug_builder);
  2399. #endif
  2400. /* Disable LLVM module verification for jit mode to speedup
  2401. the compilation process */
  2402. if (!comp_ctx->is_jit_mode) {
  2403. bh_print_time("Begin to verify LLVM module");
  2404. if (!veriy_module(comp_ctx)) {
  2405. return false;
  2406. }
  2407. }
  2408. /* Run IR optimization before feeding in ORCJIT and AOT codegen */
  2409. if (comp_ctx->optimize) {
  2410. /* Run specific passes for AOT indirect mode */
  2411. if (!comp_ctx->is_jit_mode && comp_ctx->is_indirect_mode) {
  2412. bh_print_time("Begin to run optimization passes "
  2413. "for indirect mode");
  2414. if (!apply_passes_for_indirect_mode(comp_ctx)) {
  2415. return false;
  2416. }
  2417. }
  2418. /* Run passes for AOT/JIT mode.
  2419. TODO: Apply these passes in the do_ir_transform callback of
  2420. TransformLayer when compiling each jit function, so as to
  2421. speedup the launch process. Now there are two issues in the
  2422. JIT: one is memory leak in do_ir_transform, the other is
  2423. possible core dump. */
  2424. bh_print_time("Begin to run llvm optimization passes");
  2425. aot_apply_llvm_new_pass_manager(comp_ctx, comp_ctx->module);
  2426. bh_print_time("Finish llvm optimization passes");
  2427. }
  2428. #ifdef DUMP_MODULE
  2429. LLVMDumpModule(comp_ctx->module);
  2430. os_printf("\n");
  2431. #endif
  2432. if (comp_ctx->is_jit_mode) {
  2433. LLVMErrorRef err;
  2434. LLVMOrcJITDylibRef orc_main_dylib;
  2435. LLVMOrcThreadSafeModuleRef orc_thread_safe_module;
  2436. orc_main_dylib = LLVMOrcLLLazyJITGetMainJITDylib(comp_ctx->orc_jit);
  2437. if (!orc_main_dylib) {
  2438. aot_set_last_error(
  2439. "failed to get orc orc_jit main dynmaic library");
  2440. return false;
  2441. }
  2442. orc_thread_safe_module = LLVMOrcCreateNewThreadSafeModule(
  2443. comp_ctx->module, comp_ctx->orc_thread_safe_context);
  2444. if (!orc_thread_safe_module) {
  2445. aot_set_last_error("failed to create thread safe module");
  2446. return false;
  2447. }
  2448. if ((err = LLVMOrcLLLazyJITAddLLVMIRModule(
  2449. comp_ctx->orc_jit, orc_main_dylib, orc_thread_safe_module))) {
  2450. /* If adding the ThreadSafeModule fails then we need to clean it up
  2451. by ourselves, otherwise the orc orc_jit will manage the memory.
  2452. */
  2453. LLVMOrcDisposeThreadSafeModule(orc_thread_safe_module);
  2454. aot_handle_llvm_errmsg("failed to addIRModule", err);
  2455. return false;
  2456. }
  2457. }
  2458. return true;
  2459. }
  2460. #if !(defined(_WIN32) || defined(_WIN32_))
  2461. char *
  2462. aot_generate_tempfile_name(const char *prefix, const char *extension,
  2463. char *buffer, uint32 len)
  2464. {
  2465. int fd, name_len;
  2466. name_len = snprintf(buffer, len, "%s-XXXXXX", prefix);
  2467. if ((fd = mkstemp(buffer)) <= 0) {
  2468. aot_set_last_error("make temp file failed.");
  2469. return NULL;
  2470. }
  2471. /* close and remove temp file */
  2472. close(fd);
  2473. unlink(buffer);
  2474. /* Check if buffer length is enough */
  2475. /* name_len + '.' + extension + '\0' */
  2476. if (name_len + 1 + strlen(extension) + 1 > len) {
  2477. aot_set_last_error("temp file name too long.");
  2478. return NULL;
  2479. }
  2480. snprintf(buffer + name_len, len - name_len, ".%s", extension);
  2481. return buffer;
  2482. }
  2483. #endif /* end of !(defined(_WIN32) || defined(_WIN32_)) */
  2484. bool
  2485. aot_emit_llvm_file(AOTCompContext *comp_ctx, const char *file_name)
  2486. {
  2487. char *err = NULL;
  2488. bh_print_time("Begin to emit LLVM IR file");
  2489. if (LLVMPrintModuleToFile(comp_ctx->module, file_name, &err) != 0) {
  2490. if (err) {
  2491. LLVMDisposeMessage(err);
  2492. err = NULL;
  2493. }
  2494. aot_set_last_error("emit llvm ir to file failed.");
  2495. return false;
  2496. }
  2497. return true;
  2498. }
  2499. bool
  2500. aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name)
  2501. {
  2502. char *err = NULL;
  2503. LLVMCodeGenFileType file_type = LLVMObjectFile;
  2504. LLVMTargetRef target = LLVMGetTargetMachineTarget(comp_ctx->target_machine);
  2505. bh_print_time("Begin to emit object file");
  2506. #if !(defined(_WIN32) || defined(_WIN32_))
  2507. if (comp_ctx->external_llc_compiler || comp_ctx->external_asm_compiler) {
  2508. char cmd[1024];
  2509. int ret;
  2510. if (comp_ctx->external_llc_compiler) {
  2511. char bc_file_name[64];
  2512. if (!aot_generate_tempfile_name("wamrc-bc", "bc", bc_file_name,
  2513. sizeof(bc_file_name))) {
  2514. return false;
  2515. }
  2516. if (LLVMWriteBitcodeToFile(comp_ctx->module, bc_file_name) != 0) {
  2517. aot_set_last_error("emit llvm bitcode file failed.");
  2518. return false;
  2519. }
  2520. snprintf(cmd, sizeof(cmd), "%s %s -o %s %s",
  2521. comp_ctx->external_llc_compiler,
  2522. comp_ctx->llc_compiler_flags ? comp_ctx->llc_compiler_flags
  2523. : "-O3 -c",
  2524. file_name, bc_file_name);
  2525. LOG_VERBOSE("invoking external LLC compiler:\n\t%s", cmd);
  2526. ret = system(cmd);
  2527. /* remove temp bitcode file */
  2528. unlink(bc_file_name);
  2529. if (ret != 0) {
  2530. aot_set_last_error("failed to compile LLVM bitcode to obj file "
  2531. "with external LLC compiler.");
  2532. return false;
  2533. }
  2534. }
  2535. else if (comp_ctx->external_asm_compiler) {
  2536. char asm_file_name[64];
  2537. if (!aot_generate_tempfile_name("wamrc-asm", "s", asm_file_name,
  2538. sizeof(asm_file_name))) {
  2539. return false;
  2540. }
  2541. if (LLVMTargetMachineEmitToFile(comp_ctx->target_machine,
  2542. comp_ctx->module, asm_file_name,
  2543. LLVMAssemblyFile, &err)
  2544. != 0) {
  2545. if (err) {
  2546. LLVMDisposeMessage(err);
  2547. err = NULL;
  2548. }
  2549. aot_set_last_error("emit elf to assembly file failed.");
  2550. return false;
  2551. }
  2552. snprintf(cmd, sizeof(cmd), "%s %s -o %s %s",
  2553. comp_ctx->external_asm_compiler,
  2554. comp_ctx->asm_compiler_flags ? comp_ctx->asm_compiler_flags
  2555. : "-O3 -c",
  2556. file_name, asm_file_name);
  2557. LOG_VERBOSE("invoking external ASM compiler:\n\t%s", cmd);
  2558. ret = system(cmd);
  2559. /* remove temp assembly file */
  2560. unlink(asm_file_name);
  2561. if (ret != 0) {
  2562. aot_set_last_error("failed to compile Assembly file to obj "
  2563. "file with external ASM compiler.");
  2564. return false;
  2565. }
  2566. }
  2567. return true;
  2568. }
  2569. #endif /* end of !(defined(_WIN32) || defined(_WIN32_)) */
  2570. if (!strncmp(LLVMGetTargetName(target), "arc", 3))
  2571. /* Emit to assmelby file instead for arc target
  2572. as it cannot emit to object file */
  2573. file_type = LLVMAssemblyFile;
  2574. if (LLVMTargetMachineEmitToFile(comp_ctx->target_machine, comp_ctx->module,
  2575. file_name, file_type, &err)
  2576. != 0) {
  2577. if (err) {
  2578. LLVMDisposeMessage(err);
  2579. err = NULL;
  2580. }
  2581. aot_set_last_error("emit elf to object file failed.");
  2582. return false;
  2583. }
  2584. return true;
  2585. }