aot_compiler.c 110 KB

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