aot_compiler.c 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983
  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. /* opcode1 was checked in loader and is no larger than
  932. UINT8_MAX */
  933. opcode = (uint8)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. uint32 opcode1;
  1084. read_leb_uint32(frame_ip, frame_ip_end, opcode1);
  1085. /* opcode1 was checked in loader and is no larger than
  1086. UINT8_MAX */
  1087. opcode = (uint8)opcode1;
  1088. if (opcode != WASM_OP_ATOMIC_FENCE) {
  1089. read_leb_uint32(frame_ip, frame_ip_end, align);
  1090. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1091. }
  1092. switch (opcode) {
  1093. case WASM_OP_ATOMIC_WAIT32:
  1094. if (!aot_compile_op_atomic_wait(comp_ctx, func_ctx,
  1095. VALUE_TYPE_I32, align,
  1096. offset, 4))
  1097. return false;
  1098. break;
  1099. case WASM_OP_ATOMIC_WAIT64:
  1100. if (!aot_compile_op_atomic_wait(comp_ctx, func_ctx,
  1101. VALUE_TYPE_I64, align,
  1102. offset, 8))
  1103. return false;
  1104. break;
  1105. case WASM_OP_ATOMIC_NOTIFY:
  1106. if (!aot_compiler_op_atomic_notify(
  1107. comp_ctx, func_ctx, align, offset, bytes))
  1108. return false;
  1109. break;
  1110. case WASM_OP_ATOMIC_FENCE:
  1111. /* Skip memory index */
  1112. frame_ip++;
  1113. if (!aot_compiler_op_atomic_fence(comp_ctx, func_ctx))
  1114. return false;
  1115. break;
  1116. case WASM_OP_ATOMIC_I32_LOAD:
  1117. bytes = 4;
  1118. goto op_atomic_i32_load;
  1119. case WASM_OP_ATOMIC_I32_LOAD8_U:
  1120. bytes = 1;
  1121. goto op_atomic_i32_load;
  1122. case WASM_OP_ATOMIC_I32_LOAD16_U:
  1123. bytes = 2;
  1124. op_atomic_i32_load:
  1125. if (!aot_compile_op_i32_load(comp_ctx, func_ctx, align,
  1126. offset, bytes, sign, true))
  1127. return false;
  1128. break;
  1129. case WASM_OP_ATOMIC_I64_LOAD:
  1130. bytes = 8;
  1131. goto op_atomic_i64_load;
  1132. case WASM_OP_ATOMIC_I64_LOAD8_U:
  1133. bytes = 1;
  1134. goto op_atomic_i64_load;
  1135. case WASM_OP_ATOMIC_I64_LOAD16_U:
  1136. bytes = 2;
  1137. goto op_atomic_i64_load;
  1138. case WASM_OP_ATOMIC_I64_LOAD32_U:
  1139. bytes = 4;
  1140. op_atomic_i64_load:
  1141. if (!aot_compile_op_i64_load(comp_ctx, func_ctx, align,
  1142. offset, bytes, sign, true))
  1143. return false;
  1144. break;
  1145. case WASM_OP_ATOMIC_I32_STORE:
  1146. bytes = 4;
  1147. goto op_atomic_i32_store;
  1148. case WASM_OP_ATOMIC_I32_STORE8:
  1149. bytes = 1;
  1150. goto op_atomic_i32_store;
  1151. case WASM_OP_ATOMIC_I32_STORE16:
  1152. bytes = 2;
  1153. op_atomic_i32_store:
  1154. if (!aot_compile_op_i32_store(comp_ctx, func_ctx, align,
  1155. offset, bytes, true))
  1156. return false;
  1157. break;
  1158. case WASM_OP_ATOMIC_I64_STORE:
  1159. bytes = 8;
  1160. goto op_atomic_i64_store;
  1161. case WASM_OP_ATOMIC_I64_STORE8:
  1162. bytes = 1;
  1163. goto op_atomic_i64_store;
  1164. case WASM_OP_ATOMIC_I64_STORE16:
  1165. bytes = 2;
  1166. goto op_atomic_i64_store;
  1167. case WASM_OP_ATOMIC_I64_STORE32:
  1168. bytes = 4;
  1169. op_atomic_i64_store:
  1170. if (!aot_compile_op_i64_store(comp_ctx, func_ctx, align,
  1171. offset, bytes, true))
  1172. return false;
  1173. break;
  1174. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  1175. bytes = 4;
  1176. op_type = VALUE_TYPE_I32;
  1177. goto op_atomic_cmpxchg;
  1178. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  1179. bytes = 8;
  1180. op_type = VALUE_TYPE_I64;
  1181. goto op_atomic_cmpxchg;
  1182. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  1183. bytes = 1;
  1184. op_type = VALUE_TYPE_I32;
  1185. goto op_atomic_cmpxchg;
  1186. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  1187. bytes = 2;
  1188. op_type = VALUE_TYPE_I32;
  1189. goto op_atomic_cmpxchg;
  1190. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  1191. bytes = 1;
  1192. op_type = VALUE_TYPE_I64;
  1193. goto op_atomic_cmpxchg;
  1194. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  1195. bytes = 2;
  1196. op_type = VALUE_TYPE_I64;
  1197. goto op_atomic_cmpxchg;
  1198. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  1199. bytes = 4;
  1200. op_type = VALUE_TYPE_I64;
  1201. op_atomic_cmpxchg:
  1202. if (!aot_compile_op_atomic_cmpxchg(comp_ctx, func_ctx,
  1203. op_type, align,
  1204. offset, bytes))
  1205. return false;
  1206. break;
  1207. COMPILE_ATOMIC_RMW(Add, ADD);
  1208. COMPILE_ATOMIC_RMW(Sub, SUB);
  1209. COMPILE_ATOMIC_RMW(And, AND);
  1210. COMPILE_ATOMIC_RMW(Or, OR);
  1211. COMPILE_ATOMIC_RMW(Xor, XOR);
  1212. COMPILE_ATOMIC_RMW(Xchg, XCHG);
  1213. build_atomic_rmw:
  1214. if (!aot_compile_op_atomic_rmw(comp_ctx, func_ctx,
  1215. bin_op, op_type, align,
  1216. offset, bytes))
  1217. return false;
  1218. break;
  1219. default:
  1220. aot_set_last_error("unsupported opcode");
  1221. return false;
  1222. }
  1223. break;
  1224. }
  1225. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  1226. #if WASM_ENABLE_SIMD != 0
  1227. case WASM_OP_SIMD_PREFIX:
  1228. {
  1229. uint32 opcode1;
  1230. if (!comp_ctx->enable_simd) {
  1231. goto unsupport_simd;
  1232. }
  1233. read_leb_uint32(frame_ip, frame_ip_end, opcode1);
  1234. /* opcode1 was checked in loader and is no larger than
  1235. UINT8_MAX */
  1236. opcode = (uint8)opcode1;
  1237. /* follow the order of enum WASMSimdEXTOpcode in
  1238. wasm_opcode.h */
  1239. switch (opcode) {
  1240. /* Memory instruction */
  1241. case SIMD_v128_load:
  1242. {
  1243. read_leb_uint32(frame_ip, frame_ip_end, align);
  1244. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1245. if (!aot_compile_simd_v128_load(comp_ctx, func_ctx,
  1246. align, offset))
  1247. return false;
  1248. break;
  1249. }
  1250. case SIMD_v128_load8x8_s:
  1251. case SIMD_v128_load8x8_u:
  1252. case SIMD_v128_load16x4_s:
  1253. case SIMD_v128_load16x4_u:
  1254. case SIMD_v128_load32x2_s:
  1255. case SIMD_v128_load32x2_u:
  1256. {
  1257. read_leb_uint32(frame_ip, frame_ip_end, align);
  1258. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1259. if (!aot_compile_simd_load_extend(
  1260. comp_ctx, func_ctx, opcode, align, offset))
  1261. return false;
  1262. break;
  1263. }
  1264. case SIMD_v128_load8_splat:
  1265. case SIMD_v128_load16_splat:
  1266. case SIMD_v128_load32_splat:
  1267. case SIMD_v128_load64_splat:
  1268. {
  1269. read_leb_uint32(frame_ip, frame_ip_end, align);
  1270. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1271. if (!aot_compile_simd_load_splat(comp_ctx, func_ctx,
  1272. opcode, align, offset))
  1273. return false;
  1274. break;
  1275. }
  1276. case SIMD_v128_store:
  1277. {
  1278. read_leb_uint32(frame_ip, frame_ip_end, align);
  1279. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1280. if (!aot_compile_simd_v128_store(comp_ctx, func_ctx,
  1281. align, offset))
  1282. return false;
  1283. break;
  1284. }
  1285. /* Basic operation */
  1286. case SIMD_v128_const:
  1287. {
  1288. if (!aot_compile_simd_v128_const(comp_ctx, func_ctx,
  1289. frame_ip))
  1290. return false;
  1291. frame_ip += 16;
  1292. break;
  1293. }
  1294. case SIMD_v8x16_shuffle:
  1295. {
  1296. if (!aot_compile_simd_shuffle(comp_ctx, func_ctx,
  1297. frame_ip))
  1298. return false;
  1299. frame_ip += 16;
  1300. break;
  1301. }
  1302. case SIMD_v8x16_swizzle:
  1303. {
  1304. if (!aot_compile_simd_swizzle(comp_ctx, func_ctx))
  1305. return false;
  1306. break;
  1307. }
  1308. /* Splat operation */
  1309. case SIMD_i8x16_splat:
  1310. case SIMD_i16x8_splat:
  1311. case SIMD_i32x4_splat:
  1312. case SIMD_i64x2_splat:
  1313. case SIMD_f32x4_splat:
  1314. case SIMD_f64x2_splat:
  1315. {
  1316. if (!aot_compile_simd_splat(comp_ctx, func_ctx, opcode))
  1317. return false;
  1318. break;
  1319. }
  1320. /* Lane operation */
  1321. case SIMD_i8x16_extract_lane_s:
  1322. case SIMD_i8x16_extract_lane_u:
  1323. {
  1324. if (!aot_compile_simd_extract_i8x16(
  1325. comp_ctx, func_ctx, *frame_ip++,
  1326. SIMD_i8x16_extract_lane_s == opcode))
  1327. return false;
  1328. break;
  1329. }
  1330. case SIMD_i8x16_replace_lane:
  1331. {
  1332. if (!aot_compile_simd_replace_i8x16(comp_ctx, func_ctx,
  1333. *frame_ip++))
  1334. return false;
  1335. break;
  1336. }
  1337. case SIMD_i16x8_extract_lane_s:
  1338. case SIMD_i16x8_extract_lane_u:
  1339. {
  1340. if (!aot_compile_simd_extract_i16x8(
  1341. comp_ctx, func_ctx, *frame_ip++,
  1342. SIMD_i16x8_extract_lane_s == opcode))
  1343. return false;
  1344. break;
  1345. }
  1346. case SIMD_i16x8_replace_lane:
  1347. {
  1348. if (!aot_compile_simd_replace_i16x8(comp_ctx, func_ctx,
  1349. *frame_ip++))
  1350. return false;
  1351. break;
  1352. }
  1353. case SIMD_i32x4_extract_lane:
  1354. {
  1355. if (!aot_compile_simd_extract_i32x4(comp_ctx, func_ctx,
  1356. *frame_ip++))
  1357. return false;
  1358. break;
  1359. }
  1360. case SIMD_i32x4_replace_lane:
  1361. {
  1362. if (!aot_compile_simd_replace_i32x4(comp_ctx, func_ctx,
  1363. *frame_ip++))
  1364. return false;
  1365. break;
  1366. }
  1367. case SIMD_i64x2_extract_lane:
  1368. {
  1369. if (!aot_compile_simd_extract_i64x2(comp_ctx, func_ctx,
  1370. *frame_ip++))
  1371. return false;
  1372. break;
  1373. }
  1374. case SIMD_i64x2_replace_lane:
  1375. {
  1376. if (!aot_compile_simd_replace_i64x2(comp_ctx, func_ctx,
  1377. *frame_ip++))
  1378. return false;
  1379. break;
  1380. }
  1381. case SIMD_f32x4_extract_lane:
  1382. {
  1383. if (!aot_compile_simd_extract_f32x4(comp_ctx, func_ctx,
  1384. *frame_ip++))
  1385. return false;
  1386. break;
  1387. }
  1388. case SIMD_f32x4_replace_lane:
  1389. {
  1390. if (!aot_compile_simd_replace_f32x4(comp_ctx, func_ctx,
  1391. *frame_ip++))
  1392. return false;
  1393. break;
  1394. }
  1395. case SIMD_f64x2_extract_lane:
  1396. {
  1397. if (!aot_compile_simd_extract_f64x2(comp_ctx, func_ctx,
  1398. *frame_ip++))
  1399. return false;
  1400. break;
  1401. }
  1402. case SIMD_f64x2_replace_lane:
  1403. {
  1404. if (!aot_compile_simd_replace_f64x2(comp_ctx, func_ctx,
  1405. *frame_ip++))
  1406. return false;
  1407. break;
  1408. }
  1409. /* i8x16 Cmp */
  1410. case SIMD_i8x16_eq:
  1411. case SIMD_i8x16_ne:
  1412. case SIMD_i8x16_lt_s:
  1413. case SIMD_i8x16_lt_u:
  1414. case SIMD_i8x16_gt_s:
  1415. case SIMD_i8x16_gt_u:
  1416. case SIMD_i8x16_le_s:
  1417. case SIMD_i8x16_le_u:
  1418. case SIMD_i8x16_ge_s:
  1419. case SIMD_i8x16_ge_u:
  1420. {
  1421. if (!aot_compile_simd_i8x16_compare(
  1422. comp_ctx, func_ctx,
  1423. INT_EQ + opcode - SIMD_i8x16_eq))
  1424. return false;
  1425. break;
  1426. }
  1427. /* i16x8 Cmp */
  1428. case SIMD_i16x8_eq:
  1429. case SIMD_i16x8_ne:
  1430. case SIMD_i16x8_lt_s:
  1431. case SIMD_i16x8_lt_u:
  1432. case SIMD_i16x8_gt_s:
  1433. case SIMD_i16x8_gt_u:
  1434. case SIMD_i16x8_le_s:
  1435. case SIMD_i16x8_le_u:
  1436. case SIMD_i16x8_ge_s:
  1437. case SIMD_i16x8_ge_u:
  1438. {
  1439. if (!aot_compile_simd_i16x8_compare(
  1440. comp_ctx, func_ctx,
  1441. INT_EQ + opcode - SIMD_i16x8_eq))
  1442. return false;
  1443. break;
  1444. }
  1445. /* i32x4 Cmp */
  1446. case SIMD_i32x4_eq:
  1447. case SIMD_i32x4_ne:
  1448. case SIMD_i32x4_lt_s:
  1449. case SIMD_i32x4_lt_u:
  1450. case SIMD_i32x4_gt_s:
  1451. case SIMD_i32x4_gt_u:
  1452. case SIMD_i32x4_le_s:
  1453. case SIMD_i32x4_le_u:
  1454. case SIMD_i32x4_ge_s:
  1455. case SIMD_i32x4_ge_u:
  1456. {
  1457. if (!aot_compile_simd_i32x4_compare(
  1458. comp_ctx, func_ctx,
  1459. INT_EQ + opcode - SIMD_i32x4_eq))
  1460. return false;
  1461. break;
  1462. }
  1463. /* f32x4 Cmp */
  1464. case SIMD_f32x4_eq:
  1465. case SIMD_f32x4_ne:
  1466. case SIMD_f32x4_lt:
  1467. case SIMD_f32x4_gt:
  1468. case SIMD_f32x4_le:
  1469. case SIMD_f32x4_ge:
  1470. {
  1471. if (!aot_compile_simd_f32x4_compare(
  1472. comp_ctx, func_ctx,
  1473. FLOAT_EQ + opcode - SIMD_f32x4_eq))
  1474. return false;
  1475. break;
  1476. }
  1477. /* f64x2 Cmp */
  1478. case SIMD_f64x2_eq:
  1479. case SIMD_f64x2_ne:
  1480. case SIMD_f64x2_lt:
  1481. case SIMD_f64x2_gt:
  1482. case SIMD_f64x2_le:
  1483. case SIMD_f64x2_ge:
  1484. {
  1485. if (!aot_compile_simd_f64x2_compare(
  1486. comp_ctx, func_ctx,
  1487. FLOAT_EQ + opcode - SIMD_f64x2_eq))
  1488. return false;
  1489. break;
  1490. }
  1491. /* v128 Op */
  1492. case SIMD_v128_not:
  1493. case SIMD_v128_and:
  1494. case SIMD_v128_andnot:
  1495. case SIMD_v128_or:
  1496. case SIMD_v128_xor:
  1497. case SIMD_v128_bitselect:
  1498. {
  1499. if (!aot_compile_simd_v128_bitwise(comp_ctx, func_ctx,
  1500. V128_NOT + opcode
  1501. - SIMD_v128_not))
  1502. return false;
  1503. break;
  1504. }
  1505. case SIMD_v128_any_true:
  1506. {
  1507. if (!aot_compile_simd_v128_any_true(comp_ctx, func_ctx))
  1508. return false;
  1509. break;
  1510. }
  1511. /* Load Lane Op */
  1512. case SIMD_v128_load8_lane:
  1513. case SIMD_v128_load16_lane:
  1514. case SIMD_v128_load32_lane:
  1515. case SIMD_v128_load64_lane:
  1516. {
  1517. read_leb_uint32(frame_ip, frame_ip_end, align);
  1518. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1519. if (!aot_compile_simd_load_lane(comp_ctx, func_ctx,
  1520. opcode, align, offset,
  1521. *frame_ip++))
  1522. return false;
  1523. break;
  1524. }
  1525. case SIMD_v128_store8_lane:
  1526. case SIMD_v128_store16_lane:
  1527. case SIMD_v128_store32_lane:
  1528. case SIMD_v128_store64_lane:
  1529. {
  1530. read_leb_uint32(frame_ip, frame_ip_end, align);
  1531. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1532. if (!aot_compile_simd_store_lane(comp_ctx, func_ctx,
  1533. opcode, align, offset,
  1534. *frame_ip++))
  1535. return false;
  1536. break;
  1537. }
  1538. case SIMD_v128_load32_zero:
  1539. case SIMD_v128_load64_zero:
  1540. {
  1541. read_leb_uint32(frame_ip, frame_ip_end, align);
  1542. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1543. if (!aot_compile_simd_load_zero(comp_ctx, func_ctx,
  1544. opcode, align, offset))
  1545. return false;
  1546. break;
  1547. }
  1548. /* Float conversion */
  1549. case SIMD_f32x4_demote_f64x2_zero:
  1550. {
  1551. if (!aot_compile_simd_f64x2_demote(comp_ctx, func_ctx))
  1552. return false;
  1553. break;
  1554. }
  1555. case SIMD_f64x2_promote_low_f32x4_zero:
  1556. {
  1557. if (!aot_compile_simd_f32x4_promote(comp_ctx, func_ctx))
  1558. return false;
  1559. break;
  1560. }
  1561. /* i8x16 Op */
  1562. case SIMD_i8x16_abs:
  1563. {
  1564. if (!aot_compile_simd_i8x16_abs(comp_ctx, func_ctx))
  1565. return false;
  1566. break;
  1567. }
  1568. case SIMD_i8x16_neg:
  1569. {
  1570. if (!aot_compile_simd_i8x16_neg(comp_ctx, func_ctx))
  1571. return false;
  1572. break;
  1573. }
  1574. case SIMD_i8x16_popcnt:
  1575. {
  1576. if (!aot_compile_simd_i8x16_popcnt(comp_ctx, func_ctx))
  1577. return false;
  1578. break;
  1579. }
  1580. case SIMD_i8x16_all_true:
  1581. {
  1582. if (!aot_compile_simd_i8x16_all_true(comp_ctx,
  1583. func_ctx))
  1584. return false;
  1585. break;
  1586. }
  1587. case SIMD_i8x16_bitmask:
  1588. {
  1589. if (!aot_compile_simd_i8x16_bitmask(comp_ctx, func_ctx))
  1590. return false;
  1591. break;
  1592. }
  1593. case SIMD_i8x16_narrow_i16x8_s:
  1594. case SIMD_i8x16_narrow_i16x8_u:
  1595. {
  1596. if (!aot_compile_simd_i8x16_narrow_i16x8(
  1597. comp_ctx, func_ctx,
  1598. (opcode == SIMD_i8x16_narrow_i16x8_s)))
  1599. return false;
  1600. break;
  1601. }
  1602. case SIMD_f32x4_ceil:
  1603. {
  1604. if (!aot_compile_simd_f32x4_ceil(comp_ctx, func_ctx))
  1605. return false;
  1606. break;
  1607. }
  1608. case SIMD_f32x4_floor:
  1609. {
  1610. if (!aot_compile_simd_f32x4_floor(comp_ctx, func_ctx))
  1611. return false;
  1612. break;
  1613. }
  1614. case SIMD_f32x4_trunc:
  1615. {
  1616. if (!aot_compile_simd_f32x4_trunc(comp_ctx, func_ctx))
  1617. return false;
  1618. break;
  1619. }
  1620. case SIMD_f32x4_nearest:
  1621. {
  1622. if (!aot_compile_simd_f32x4_nearest(comp_ctx, func_ctx))
  1623. return false;
  1624. break;
  1625. }
  1626. case SIMD_i8x16_shl:
  1627. case SIMD_i8x16_shr_s:
  1628. case SIMD_i8x16_shr_u:
  1629. {
  1630. if (!aot_compile_simd_i8x16_shift(comp_ctx, func_ctx,
  1631. INT_SHL + opcode
  1632. - SIMD_i8x16_shl))
  1633. return false;
  1634. break;
  1635. }
  1636. case SIMD_i8x16_add:
  1637. {
  1638. if (!aot_compile_simd_i8x16_arith(comp_ctx, func_ctx,
  1639. V128_ADD))
  1640. return false;
  1641. break;
  1642. }
  1643. case SIMD_i8x16_add_sat_s:
  1644. case SIMD_i8x16_add_sat_u:
  1645. {
  1646. if (!aot_compile_simd_i8x16_saturate(
  1647. comp_ctx, func_ctx, V128_ADD,
  1648. opcode == SIMD_i8x16_add_sat_s))
  1649. return false;
  1650. break;
  1651. }
  1652. case SIMD_i8x16_sub:
  1653. {
  1654. if (!aot_compile_simd_i8x16_arith(comp_ctx, func_ctx,
  1655. V128_SUB))
  1656. return false;
  1657. break;
  1658. }
  1659. case SIMD_i8x16_sub_sat_s:
  1660. case SIMD_i8x16_sub_sat_u:
  1661. {
  1662. if (!aot_compile_simd_i8x16_saturate(
  1663. comp_ctx, func_ctx, V128_SUB,
  1664. opcode == SIMD_i8x16_sub_sat_s))
  1665. return false;
  1666. break;
  1667. }
  1668. case SIMD_f64x2_ceil:
  1669. {
  1670. if (!aot_compile_simd_f64x2_ceil(comp_ctx, func_ctx))
  1671. return false;
  1672. break;
  1673. }
  1674. case SIMD_f64x2_floor:
  1675. {
  1676. if (!aot_compile_simd_f64x2_floor(comp_ctx, func_ctx))
  1677. return false;
  1678. break;
  1679. }
  1680. case SIMD_i8x16_min_s:
  1681. case SIMD_i8x16_min_u:
  1682. {
  1683. if (!aot_compile_simd_i8x16_cmp(
  1684. comp_ctx, func_ctx, V128_MIN,
  1685. opcode == SIMD_i8x16_min_s))
  1686. return false;
  1687. break;
  1688. }
  1689. case SIMD_i8x16_max_s:
  1690. case SIMD_i8x16_max_u:
  1691. {
  1692. if (!aot_compile_simd_i8x16_cmp(
  1693. comp_ctx, func_ctx, V128_MAX,
  1694. opcode == SIMD_i8x16_max_s))
  1695. return false;
  1696. break;
  1697. }
  1698. case SIMD_f64x2_trunc:
  1699. {
  1700. if (!aot_compile_simd_f64x2_trunc(comp_ctx, func_ctx))
  1701. return false;
  1702. break;
  1703. }
  1704. case SIMD_i8x16_avgr_u:
  1705. {
  1706. if (!aot_compile_simd_i8x16_avgr_u(comp_ctx, func_ctx))
  1707. return false;
  1708. break;
  1709. }
  1710. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  1711. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  1712. {
  1713. if (!aot_compile_simd_i16x8_extadd_pairwise_i8x16(
  1714. comp_ctx, func_ctx,
  1715. SIMD_i16x8_extadd_pairwise_i8x16_s == opcode))
  1716. return false;
  1717. break;
  1718. }
  1719. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  1720. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  1721. {
  1722. if (!aot_compile_simd_i32x4_extadd_pairwise_i16x8(
  1723. comp_ctx, func_ctx,
  1724. SIMD_i32x4_extadd_pairwise_i16x8_s == opcode))
  1725. return false;
  1726. break;
  1727. }
  1728. /* i16x8 Op */
  1729. case SIMD_i16x8_abs:
  1730. {
  1731. if (!aot_compile_simd_i16x8_abs(comp_ctx, func_ctx))
  1732. return false;
  1733. break;
  1734. }
  1735. case SIMD_i16x8_neg:
  1736. {
  1737. if (!aot_compile_simd_i16x8_neg(comp_ctx, func_ctx))
  1738. return false;
  1739. break;
  1740. }
  1741. case SIMD_i16x8_q15mulr_sat_s:
  1742. {
  1743. if (!aot_compile_simd_i16x8_q15mulr_sat(comp_ctx,
  1744. func_ctx))
  1745. return false;
  1746. break;
  1747. }
  1748. case SIMD_i16x8_all_true:
  1749. {
  1750. if (!aot_compile_simd_i16x8_all_true(comp_ctx,
  1751. func_ctx))
  1752. return false;
  1753. break;
  1754. }
  1755. case SIMD_i16x8_bitmask:
  1756. {
  1757. if (!aot_compile_simd_i16x8_bitmask(comp_ctx, func_ctx))
  1758. return false;
  1759. break;
  1760. }
  1761. case SIMD_i16x8_narrow_i32x4_s:
  1762. case SIMD_i16x8_narrow_i32x4_u:
  1763. {
  1764. if (!aot_compile_simd_i16x8_narrow_i32x4(
  1765. comp_ctx, func_ctx,
  1766. SIMD_i16x8_narrow_i32x4_s == opcode))
  1767. return false;
  1768. break;
  1769. }
  1770. case SIMD_i16x8_extend_low_i8x16_s:
  1771. case SIMD_i16x8_extend_high_i8x16_s:
  1772. {
  1773. if (!aot_compile_simd_i16x8_extend_i8x16(
  1774. comp_ctx, func_ctx,
  1775. SIMD_i16x8_extend_low_i8x16_s == opcode, true))
  1776. return false;
  1777. break;
  1778. }
  1779. case SIMD_i16x8_extend_low_i8x16_u:
  1780. case SIMD_i16x8_extend_high_i8x16_u:
  1781. {
  1782. if (!aot_compile_simd_i16x8_extend_i8x16(
  1783. comp_ctx, func_ctx,
  1784. SIMD_i16x8_extend_low_i8x16_u == opcode, false))
  1785. return false;
  1786. break;
  1787. }
  1788. case SIMD_i16x8_shl:
  1789. case SIMD_i16x8_shr_s:
  1790. case SIMD_i16x8_shr_u:
  1791. {
  1792. if (!aot_compile_simd_i16x8_shift(comp_ctx, func_ctx,
  1793. INT_SHL + opcode
  1794. - SIMD_i16x8_shl))
  1795. return false;
  1796. break;
  1797. }
  1798. case SIMD_i16x8_add:
  1799. {
  1800. if (!aot_compile_simd_i16x8_arith(comp_ctx, func_ctx,
  1801. V128_ADD))
  1802. return false;
  1803. break;
  1804. }
  1805. case SIMD_i16x8_add_sat_s:
  1806. case SIMD_i16x8_add_sat_u:
  1807. {
  1808. if (!aot_compile_simd_i16x8_saturate(
  1809. comp_ctx, func_ctx, V128_ADD,
  1810. opcode == SIMD_i16x8_add_sat_s ? true : false))
  1811. return false;
  1812. break;
  1813. }
  1814. case SIMD_i16x8_sub:
  1815. {
  1816. if (!aot_compile_simd_i16x8_arith(comp_ctx, func_ctx,
  1817. V128_SUB))
  1818. return false;
  1819. break;
  1820. }
  1821. case SIMD_i16x8_sub_sat_s:
  1822. case SIMD_i16x8_sub_sat_u:
  1823. {
  1824. if (!aot_compile_simd_i16x8_saturate(
  1825. comp_ctx, func_ctx, V128_SUB,
  1826. opcode == SIMD_i16x8_sub_sat_s ? true : false))
  1827. return false;
  1828. break;
  1829. }
  1830. case SIMD_f64x2_nearest:
  1831. {
  1832. if (!aot_compile_simd_f64x2_nearest(comp_ctx, func_ctx))
  1833. return false;
  1834. break;
  1835. }
  1836. case SIMD_i16x8_mul:
  1837. {
  1838. if (!aot_compile_simd_i16x8_arith(comp_ctx, func_ctx,
  1839. V128_MUL))
  1840. return false;
  1841. break;
  1842. }
  1843. case SIMD_i16x8_min_s:
  1844. case SIMD_i16x8_min_u:
  1845. {
  1846. if (!aot_compile_simd_i16x8_cmp(
  1847. comp_ctx, func_ctx, V128_MIN,
  1848. opcode == SIMD_i16x8_min_s))
  1849. return false;
  1850. break;
  1851. }
  1852. case SIMD_i16x8_max_s:
  1853. case SIMD_i16x8_max_u:
  1854. {
  1855. if (!aot_compile_simd_i16x8_cmp(
  1856. comp_ctx, func_ctx, V128_MAX,
  1857. opcode == SIMD_i16x8_max_s))
  1858. return false;
  1859. break;
  1860. }
  1861. case SIMD_i16x8_avgr_u:
  1862. {
  1863. if (!aot_compile_simd_i16x8_avgr_u(comp_ctx, func_ctx))
  1864. return false;
  1865. break;
  1866. }
  1867. case SIMD_i16x8_extmul_low_i8x16_s:
  1868. case SIMD_i16x8_extmul_high_i8x16_s:
  1869. {
  1870. if (!(aot_compile_simd_i16x8_extmul_i8x16(
  1871. comp_ctx, func_ctx,
  1872. SIMD_i16x8_extmul_low_i8x16_s == opcode, true)))
  1873. return false;
  1874. break;
  1875. }
  1876. case SIMD_i16x8_extmul_low_i8x16_u:
  1877. case SIMD_i16x8_extmul_high_i8x16_u:
  1878. {
  1879. if (!(aot_compile_simd_i16x8_extmul_i8x16(
  1880. comp_ctx, func_ctx,
  1881. SIMD_i16x8_extmul_low_i8x16_u == opcode,
  1882. false)))
  1883. return false;
  1884. break;
  1885. }
  1886. /* i32x4 Op */
  1887. case SIMD_i32x4_abs:
  1888. {
  1889. if (!aot_compile_simd_i32x4_abs(comp_ctx, func_ctx))
  1890. return false;
  1891. break;
  1892. }
  1893. case SIMD_i32x4_neg:
  1894. {
  1895. if (!aot_compile_simd_i32x4_neg(comp_ctx, func_ctx))
  1896. return false;
  1897. break;
  1898. }
  1899. case SIMD_i32x4_all_true:
  1900. {
  1901. if (!aot_compile_simd_i32x4_all_true(comp_ctx,
  1902. func_ctx))
  1903. return false;
  1904. break;
  1905. }
  1906. case SIMD_i32x4_bitmask:
  1907. {
  1908. if (!aot_compile_simd_i32x4_bitmask(comp_ctx, func_ctx))
  1909. return false;
  1910. break;
  1911. }
  1912. case SIMD_i32x4_narrow_i64x2_s:
  1913. case SIMD_i32x4_narrow_i64x2_u:
  1914. {
  1915. if (!aot_compile_simd_i32x4_narrow_i64x2(
  1916. comp_ctx, func_ctx,
  1917. SIMD_i32x4_narrow_i64x2_s == opcode))
  1918. return false;
  1919. break;
  1920. }
  1921. case SIMD_i32x4_extend_low_i16x8_s:
  1922. case SIMD_i32x4_extend_high_i16x8_s:
  1923. {
  1924. if (!aot_compile_simd_i32x4_extend_i16x8(
  1925. comp_ctx, func_ctx,
  1926. SIMD_i32x4_extend_low_i16x8_s == opcode, true))
  1927. return false;
  1928. break;
  1929. }
  1930. case SIMD_i32x4_extend_low_i16x8_u:
  1931. case SIMD_i32x4_extend_high_i16x8_u:
  1932. {
  1933. if (!aot_compile_simd_i32x4_extend_i16x8(
  1934. comp_ctx, func_ctx,
  1935. SIMD_i32x4_extend_low_i16x8_u == opcode, false))
  1936. return false;
  1937. break;
  1938. }
  1939. case SIMD_i32x4_shl:
  1940. case SIMD_i32x4_shr_s:
  1941. case SIMD_i32x4_shr_u:
  1942. {
  1943. if (!aot_compile_simd_i32x4_shift(comp_ctx, func_ctx,
  1944. INT_SHL + opcode
  1945. - SIMD_i32x4_shl))
  1946. return false;
  1947. break;
  1948. }
  1949. case SIMD_i32x4_add:
  1950. {
  1951. if (!aot_compile_simd_i32x4_arith(comp_ctx, func_ctx,
  1952. V128_ADD))
  1953. return false;
  1954. break;
  1955. }
  1956. case SIMD_i32x4_add_sat_s:
  1957. case SIMD_i32x4_add_sat_u:
  1958. {
  1959. if (!aot_compile_simd_i32x4_saturate(
  1960. comp_ctx, func_ctx, V128_ADD,
  1961. opcode == SIMD_i32x4_add_sat_s))
  1962. return false;
  1963. break;
  1964. }
  1965. case SIMD_i32x4_sub:
  1966. {
  1967. if (!aot_compile_simd_i32x4_arith(comp_ctx, func_ctx,
  1968. V128_SUB))
  1969. return false;
  1970. break;
  1971. }
  1972. case SIMD_i32x4_sub_sat_s:
  1973. case SIMD_i32x4_sub_sat_u:
  1974. {
  1975. if (!aot_compile_simd_i32x4_saturate(
  1976. comp_ctx, func_ctx, V128_SUB,
  1977. opcode == SIMD_i32x4_add_sat_s))
  1978. return false;
  1979. break;
  1980. }
  1981. case SIMD_i32x4_mul:
  1982. {
  1983. if (!aot_compile_simd_i32x4_arith(comp_ctx, func_ctx,
  1984. V128_MUL))
  1985. return false;
  1986. break;
  1987. }
  1988. case SIMD_i32x4_min_s:
  1989. case SIMD_i32x4_min_u:
  1990. {
  1991. if (!aot_compile_simd_i32x4_cmp(
  1992. comp_ctx, func_ctx, V128_MIN,
  1993. SIMD_i32x4_min_s == opcode))
  1994. return false;
  1995. break;
  1996. }
  1997. case SIMD_i32x4_max_s:
  1998. case SIMD_i32x4_max_u:
  1999. {
  2000. if (!aot_compile_simd_i32x4_cmp(
  2001. comp_ctx, func_ctx, V128_MAX,
  2002. SIMD_i32x4_max_s == opcode))
  2003. return false;
  2004. break;
  2005. }
  2006. case SIMD_i32x4_dot_i16x8_s:
  2007. {
  2008. if (!aot_compile_simd_i32x4_dot_i16x8(comp_ctx,
  2009. func_ctx))
  2010. return false;
  2011. break;
  2012. }
  2013. case SIMD_i32x4_avgr_u:
  2014. {
  2015. if (!aot_compile_simd_i32x4_avgr_u(comp_ctx, func_ctx))
  2016. return false;
  2017. break;
  2018. }
  2019. case SIMD_i32x4_extmul_low_i16x8_s:
  2020. case SIMD_i32x4_extmul_high_i16x8_s:
  2021. {
  2022. if (!aot_compile_simd_i32x4_extmul_i16x8(
  2023. comp_ctx, func_ctx,
  2024. SIMD_i32x4_extmul_low_i16x8_s == opcode, true))
  2025. return false;
  2026. break;
  2027. }
  2028. case SIMD_i32x4_extmul_low_i16x8_u:
  2029. case SIMD_i32x4_extmul_high_i16x8_u:
  2030. {
  2031. if (!aot_compile_simd_i32x4_extmul_i16x8(
  2032. comp_ctx, func_ctx,
  2033. SIMD_i32x4_extmul_low_i16x8_u == opcode, false))
  2034. return false;
  2035. break;
  2036. }
  2037. /* i64x2 Op */
  2038. case SIMD_i64x2_abs:
  2039. {
  2040. if (!aot_compile_simd_i64x2_abs(comp_ctx, func_ctx))
  2041. return false;
  2042. break;
  2043. }
  2044. case SIMD_i64x2_neg:
  2045. {
  2046. if (!aot_compile_simd_i64x2_neg(comp_ctx, func_ctx))
  2047. return false;
  2048. break;
  2049. }
  2050. case SIMD_i64x2_all_true:
  2051. {
  2052. if (!aot_compile_simd_i64x2_all_true(comp_ctx,
  2053. func_ctx))
  2054. return false;
  2055. break;
  2056. }
  2057. case SIMD_i64x2_bitmask:
  2058. {
  2059. if (!aot_compile_simd_i64x2_bitmask(comp_ctx, func_ctx))
  2060. return false;
  2061. break;
  2062. }
  2063. case SIMD_i64x2_extend_low_i32x4_s:
  2064. case SIMD_i64x2_extend_high_i32x4_s:
  2065. {
  2066. if (!aot_compile_simd_i64x2_extend_i32x4(
  2067. comp_ctx, func_ctx,
  2068. SIMD_i64x2_extend_low_i32x4_s == opcode, true))
  2069. return false;
  2070. break;
  2071. }
  2072. case SIMD_i64x2_extend_low_i32x4_u:
  2073. case SIMD_i64x2_extend_high_i32x4_u:
  2074. {
  2075. if (!aot_compile_simd_i64x2_extend_i32x4(
  2076. comp_ctx, func_ctx,
  2077. SIMD_i64x2_extend_low_i32x4_u == opcode, false))
  2078. return false;
  2079. break;
  2080. }
  2081. case SIMD_i64x2_shl:
  2082. case SIMD_i64x2_shr_s:
  2083. case SIMD_i64x2_shr_u:
  2084. {
  2085. if (!aot_compile_simd_i64x2_shift(comp_ctx, func_ctx,
  2086. INT_SHL + opcode
  2087. - SIMD_i64x2_shl))
  2088. return false;
  2089. break;
  2090. }
  2091. case SIMD_i64x2_add:
  2092. {
  2093. if (!aot_compile_simd_i64x2_arith(comp_ctx, func_ctx,
  2094. V128_ADD))
  2095. return false;
  2096. break;
  2097. }
  2098. case SIMD_i64x2_sub:
  2099. {
  2100. if (!aot_compile_simd_i64x2_arith(comp_ctx, func_ctx,
  2101. V128_SUB))
  2102. return false;
  2103. break;
  2104. }
  2105. case SIMD_i64x2_mul:
  2106. {
  2107. if (!aot_compile_simd_i64x2_arith(comp_ctx, func_ctx,
  2108. V128_MUL))
  2109. return false;
  2110. break;
  2111. }
  2112. case SIMD_i64x2_eq:
  2113. case SIMD_i64x2_ne:
  2114. case SIMD_i64x2_lt_s:
  2115. case SIMD_i64x2_gt_s:
  2116. case SIMD_i64x2_le_s:
  2117. case SIMD_i64x2_ge_s:
  2118. {
  2119. IntCond icond[] = { INT_EQ, INT_NE, INT_LT_S,
  2120. INT_GT_S, INT_LE_S, INT_GE_S };
  2121. if (!aot_compile_simd_i64x2_compare(
  2122. comp_ctx, func_ctx,
  2123. icond[opcode - SIMD_i64x2_eq]))
  2124. return false;
  2125. break;
  2126. }
  2127. case SIMD_i64x2_extmul_low_i32x4_s:
  2128. case SIMD_i64x2_extmul_high_i32x4_s:
  2129. {
  2130. if (!aot_compile_simd_i64x2_extmul_i32x4(
  2131. comp_ctx, func_ctx,
  2132. SIMD_i64x2_extmul_low_i32x4_s == opcode, true))
  2133. return false;
  2134. break;
  2135. }
  2136. case SIMD_i64x2_extmul_low_i32x4_u:
  2137. case SIMD_i64x2_extmul_high_i32x4_u:
  2138. {
  2139. if (!aot_compile_simd_i64x2_extmul_i32x4(
  2140. comp_ctx, func_ctx,
  2141. SIMD_i64x2_extmul_low_i32x4_u == opcode, false))
  2142. return false;
  2143. break;
  2144. }
  2145. /* f32x4 Op */
  2146. case SIMD_f32x4_abs:
  2147. {
  2148. if (!aot_compile_simd_f32x4_abs(comp_ctx, func_ctx))
  2149. return false;
  2150. break;
  2151. }
  2152. case SIMD_f32x4_neg:
  2153. {
  2154. if (!aot_compile_simd_f32x4_neg(comp_ctx, func_ctx))
  2155. return false;
  2156. break;
  2157. }
  2158. case SIMD_f32x4_round:
  2159. {
  2160. if (!aot_compile_simd_f32x4_round(comp_ctx, func_ctx))
  2161. return false;
  2162. break;
  2163. }
  2164. case SIMD_f32x4_sqrt:
  2165. {
  2166. if (!aot_compile_simd_f32x4_sqrt(comp_ctx, func_ctx))
  2167. return false;
  2168. break;
  2169. }
  2170. case SIMD_f32x4_add:
  2171. case SIMD_f32x4_sub:
  2172. case SIMD_f32x4_mul:
  2173. case SIMD_f32x4_div:
  2174. {
  2175. if (!aot_compile_simd_f32x4_arith(comp_ctx, func_ctx,
  2176. FLOAT_ADD + opcode
  2177. - SIMD_f32x4_add))
  2178. return false;
  2179. break;
  2180. }
  2181. case SIMD_f32x4_min:
  2182. case SIMD_f32x4_max:
  2183. {
  2184. if (!aot_compile_simd_f32x4_min_max(
  2185. comp_ctx, func_ctx, SIMD_f32x4_min == opcode))
  2186. return false;
  2187. break;
  2188. }
  2189. case SIMD_f32x4_pmin:
  2190. case SIMD_f32x4_pmax:
  2191. {
  2192. if (!aot_compile_simd_f32x4_pmin_pmax(
  2193. comp_ctx, func_ctx, SIMD_f32x4_pmin == opcode))
  2194. return false;
  2195. break;
  2196. }
  2197. /* f64x2 Op */
  2198. case SIMD_f64x2_abs:
  2199. {
  2200. if (!aot_compile_simd_f64x2_abs(comp_ctx, func_ctx))
  2201. return false;
  2202. break;
  2203. }
  2204. case SIMD_f64x2_neg:
  2205. {
  2206. if (!aot_compile_simd_f64x2_neg(comp_ctx, func_ctx))
  2207. return false;
  2208. break;
  2209. }
  2210. case SIMD_f64x2_round:
  2211. {
  2212. if (!aot_compile_simd_f64x2_round(comp_ctx, func_ctx))
  2213. return false;
  2214. break;
  2215. }
  2216. case SIMD_f64x2_sqrt:
  2217. {
  2218. if (!aot_compile_simd_f64x2_sqrt(comp_ctx, func_ctx))
  2219. return false;
  2220. break;
  2221. }
  2222. case SIMD_f64x2_add:
  2223. case SIMD_f64x2_sub:
  2224. case SIMD_f64x2_mul:
  2225. case SIMD_f64x2_div:
  2226. {
  2227. if (!aot_compile_simd_f64x2_arith(comp_ctx, func_ctx,
  2228. FLOAT_ADD + opcode
  2229. - SIMD_f64x2_add))
  2230. return false;
  2231. break;
  2232. }
  2233. case SIMD_f64x2_min:
  2234. case SIMD_f64x2_max:
  2235. {
  2236. if (!aot_compile_simd_f64x2_min_max(
  2237. comp_ctx, func_ctx, SIMD_f64x2_min == opcode))
  2238. return false;
  2239. break;
  2240. }
  2241. case SIMD_f64x2_pmin:
  2242. case SIMD_f64x2_pmax:
  2243. {
  2244. if (!aot_compile_simd_f64x2_pmin_pmax(
  2245. comp_ctx, func_ctx, SIMD_f64x2_pmin == opcode))
  2246. return false;
  2247. break;
  2248. }
  2249. /* Conversion Op */
  2250. case SIMD_i32x4_trunc_sat_f32x4_s:
  2251. case SIMD_i32x4_trunc_sat_f32x4_u:
  2252. {
  2253. if (!aot_compile_simd_i32x4_trunc_sat_f32x4(
  2254. comp_ctx, func_ctx,
  2255. SIMD_i32x4_trunc_sat_f32x4_s == opcode))
  2256. return false;
  2257. break;
  2258. }
  2259. case SIMD_f32x4_convert_i32x4_s:
  2260. case SIMD_f32x4_convert_i32x4_u:
  2261. {
  2262. if (!aot_compile_simd_f32x4_convert_i32x4(
  2263. comp_ctx, func_ctx,
  2264. SIMD_f32x4_convert_i32x4_s == opcode))
  2265. return false;
  2266. break;
  2267. }
  2268. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  2269. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  2270. {
  2271. if (!aot_compile_simd_i32x4_trunc_sat_f64x2(
  2272. comp_ctx, func_ctx,
  2273. SIMD_i32x4_trunc_sat_f64x2_s_zero == opcode))
  2274. return false;
  2275. break;
  2276. }
  2277. case SIMD_f64x2_convert_low_i32x4_s:
  2278. case SIMD_f64x2_convert_low_i32x4_u:
  2279. {
  2280. if (!aot_compile_simd_f64x2_convert_i32x4(
  2281. comp_ctx, func_ctx,
  2282. SIMD_f64x2_convert_low_i32x4_s == opcode))
  2283. return false;
  2284. break;
  2285. }
  2286. default:
  2287. aot_set_last_error("unsupported SIMD opcode");
  2288. return false;
  2289. }
  2290. break;
  2291. }
  2292. #endif /* end of WASM_ENABLE_SIMD */
  2293. default:
  2294. aot_set_last_error("unsupported opcode");
  2295. return false;
  2296. }
  2297. }
  2298. /* Move func_return block to the bottom */
  2299. if (func_ctx->func_return_block) {
  2300. LLVMBasicBlockRef last_block = LLVMGetLastBasicBlock(func_ctx->func);
  2301. if (last_block != func_ctx->func_return_block)
  2302. LLVMMoveBasicBlockAfter(func_ctx->func_return_block, last_block);
  2303. }
  2304. /* Move got_exception block to the bottom */
  2305. if (func_ctx->got_exception_block) {
  2306. LLVMBasicBlockRef last_block = LLVMGetLastBasicBlock(func_ctx->func);
  2307. if (last_block != func_ctx->got_exception_block)
  2308. LLVMMoveBasicBlockAfter(func_ctx->got_exception_block, last_block);
  2309. }
  2310. return true;
  2311. #if WASM_ENABLE_SIMD != 0
  2312. unsupport_simd:
  2313. aot_set_last_error("SIMD instruction was found, "
  2314. "try removing --disable-simd option");
  2315. return false;
  2316. #endif
  2317. #if WASM_ENABLE_REF_TYPES != 0
  2318. unsupport_ref_types:
  2319. aot_set_last_error("reference type instruction was found, "
  2320. "try removing --disable-ref-types option");
  2321. return false;
  2322. #endif
  2323. #if WASM_ENABLE_BULK_MEMORY != 0
  2324. unsupport_bulk_memory:
  2325. aot_set_last_error("bulk memory instruction was found, "
  2326. "try removing --disable-bulk-memory option");
  2327. return false;
  2328. #endif
  2329. fail:
  2330. return false;
  2331. }
  2332. static bool
  2333. verify_module(AOTCompContext *comp_ctx)
  2334. {
  2335. char *msg = NULL;
  2336. bool ret;
  2337. ret = LLVMVerifyModule(comp_ctx->module, LLVMPrintMessageAction, &msg);
  2338. if (!ret && msg) {
  2339. if (msg[0] != '\0') {
  2340. aot_set_last_error(msg);
  2341. LLVMDisposeMessage(msg);
  2342. return false;
  2343. }
  2344. LLVMDisposeMessage(msg);
  2345. }
  2346. return true;
  2347. }
  2348. bool
  2349. aot_compile_wasm(AOTCompContext *comp_ctx)
  2350. {
  2351. uint32 i;
  2352. if (!aot_validate_wasm(comp_ctx)) {
  2353. return false;
  2354. }
  2355. bh_print_time("Begin to compile WASM bytecode to LLVM IR");
  2356. for (i = 0; i < comp_ctx->func_ctx_count; i++) {
  2357. if (!aot_compile_func(comp_ctx, i)) {
  2358. return false;
  2359. }
  2360. }
  2361. #if WASM_ENABLE_DEBUG_AOT != 0
  2362. LLVMDIBuilderFinalize(comp_ctx->debug_builder);
  2363. #endif
  2364. /* Disable LLVM module verification for jit mode to speedup
  2365. the compilation process */
  2366. if (!comp_ctx->is_jit_mode) {
  2367. bh_print_time("Begin to verify LLVM module");
  2368. if (!verify_module(comp_ctx)) {
  2369. return false;
  2370. }
  2371. }
  2372. /* Run IR optimization before feeding in ORCJIT and AOT codegen */
  2373. if (comp_ctx->optimize) {
  2374. /* Run passes for AOT/JIT mode.
  2375. TODO: Apply these passes in the do_ir_transform callback of
  2376. TransformLayer when compiling each jit function, so as to
  2377. speedup the launch process. Now there are two issues in the
  2378. JIT: one is memory leak in do_ir_transform, the other is
  2379. possible core dump. */
  2380. bh_print_time("Begin to run llvm optimization passes");
  2381. aot_apply_llvm_new_pass_manager(comp_ctx, comp_ctx->module);
  2382. bh_print_time("Finish llvm optimization passes");
  2383. }
  2384. #ifdef DUMP_MODULE
  2385. LLVMDumpModule(comp_ctx->module);
  2386. os_printf("\n");
  2387. #endif
  2388. if (comp_ctx->is_jit_mode) {
  2389. LLVMErrorRef err;
  2390. LLVMOrcJITDylibRef orc_main_dylib;
  2391. LLVMOrcThreadSafeModuleRef orc_thread_safe_module;
  2392. orc_main_dylib = LLVMOrcLLLazyJITGetMainJITDylib(comp_ctx->orc_jit);
  2393. if (!orc_main_dylib) {
  2394. aot_set_last_error(
  2395. "failed to get orc orc_jit main dynmaic library");
  2396. return false;
  2397. }
  2398. orc_thread_safe_module = LLVMOrcCreateNewThreadSafeModule(
  2399. comp_ctx->module, comp_ctx->orc_thread_safe_context);
  2400. if (!orc_thread_safe_module) {
  2401. aot_set_last_error("failed to create thread safe module");
  2402. return false;
  2403. }
  2404. if ((err = LLVMOrcLLLazyJITAddLLVMIRModule(
  2405. comp_ctx->orc_jit, orc_main_dylib, orc_thread_safe_module))) {
  2406. /* If adding the ThreadSafeModule fails then we need to clean it up
  2407. by ourselves, otherwise the orc orc_jit will manage the memory.
  2408. */
  2409. LLVMOrcDisposeThreadSafeModule(orc_thread_safe_module);
  2410. aot_handle_llvm_errmsg("failed to addIRModule", err);
  2411. return false;
  2412. }
  2413. if (comp_ctx->stack_sizes != NULL) {
  2414. LLVMOrcJITTargetAddress addr;
  2415. if ((err = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &addr,
  2416. aot_stack_sizes_alias_name))) {
  2417. aot_handle_llvm_errmsg("failed to look up stack_sizes", err);
  2418. return false;
  2419. }
  2420. comp_ctx->jit_stack_sizes = (uint32 *)addr;
  2421. }
  2422. }
  2423. return true;
  2424. }
  2425. #if !(defined(_WIN32) || defined(_WIN32_))
  2426. char *
  2427. aot_generate_tempfile_name(const char *prefix, const char *extension,
  2428. char *buffer, uint32 len)
  2429. {
  2430. int fd, name_len;
  2431. name_len = snprintf(buffer, len, "%s-XXXXXX", prefix);
  2432. if ((fd = mkstemp(buffer)) <= 0) {
  2433. aot_set_last_error("make temp file failed.");
  2434. return NULL;
  2435. }
  2436. /* close and remove temp file */
  2437. close(fd);
  2438. unlink(buffer);
  2439. /* Check if buffer length is enough */
  2440. /* name_len + '.' + extension + '\0' */
  2441. if (name_len + 1 + strlen(extension) + 1 > len) {
  2442. aot_set_last_error("temp file name too long.");
  2443. return NULL;
  2444. }
  2445. snprintf(buffer + name_len, len - name_len, ".%s", extension);
  2446. return buffer;
  2447. }
  2448. #else
  2449. errno_t
  2450. _mktemp_s(char *nameTemplate, size_t sizeInChars);
  2451. char *
  2452. aot_generate_tempfile_name(const char *prefix, const char *extension,
  2453. char *buffer, uint32 len)
  2454. {
  2455. int name_len;
  2456. name_len = snprintf(buffer, len, "%s-XXXXXX", prefix);
  2457. if (_mktemp_s(buffer, name_len + 1) != 0) {
  2458. return NULL;
  2459. }
  2460. /* Check if buffer length is enough */
  2461. /* name_len + '.' + extension + '\0' */
  2462. if (name_len + 1 + strlen(extension) + 1 > len) {
  2463. aot_set_last_error("temp file name too long.");
  2464. return NULL;
  2465. }
  2466. snprintf(buffer + name_len, len - name_len, ".%s", extension);
  2467. return buffer;
  2468. }
  2469. #endif /* end of !(defined(_WIN32) || defined(_WIN32_)) */
  2470. bool
  2471. aot_emit_llvm_file(AOTCompContext *comp_ctx, const char *file_name)
  2472. {
  2473. char *err = NULL;
  2474. bh_print_time("Begin to emit LLVM IR file");
  2475. if (LLVMPrintModuleToFile(comp_ctx->module, file_name, &err) != 0) {
  2476. if (err) {
  2477. LLVMDisposeMessage(err);
  2478. err = NULL;
  2479. }
  2480. aot_set_last_error("emit llvm ir to file failed.");
  2481. return false;
  2482. }
  2483. return true;
  2484. }
  2485. static bool
  2486. aot_move_file(const char *dest, const char *src)
  2487. {
  2488. FILE *dfp = fopen(dest, "w");
  2489. FILE *sfp = fopen(src, "r");
  2490. size_t rsz;
  2491. char buf[128];
  2492. bool success = false;
  2493. if (dfp == NULL || sfp == NULL) {
  2494. LOG_DEBUG("open error %s %s", dest, src);
  2495. goto fail;
  2496. }
  2497. do {
  2498. rsz = fread(buf, 1, sizeof(buf), sfp);
  2499. if (rsz > 0) {
  2500. size_t wsz = fwrite(buf, 1, rsz, dfp);
  2501. if (wsz < rsz) {
  2502. LOG_DEBUG("write error");
  2503. goto fail;
  2504. }
  2505. }
  2506. if (rsz < sizeof(buf)) {
  2507. if (ferror(sfp)) {
  2508. LOG_DEBUG("read error");
  2509. goto fail;
  2510. }
  2511. }
  2512. } while (rsz > 0);
  2513. success = true;
  2514. fail:
  2515. if (dfp != NULL) {
  2516. if (fclose(dfp)) {
  2517. LOG_DEBUG("close error");
  2518. success = false;
  2519. }
  2520. if (!success) {
  2521. (void)unlink(dest);
  2522. }
  2523. }
  2524. if (sfp != NULL) {
  2525. (void)fclose(sfp);
  2526. }
  2527. if (success) {
  2528. (void)unlink(src);
  2529. }
  2530. return success;
  2531. }
  2532. bool
  2533. aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name)
  2534. {
  2535. char *err = NULL;
  2536. LLVMCodeGenFileType file_type = LLVMObjectFile;
  2537. LLVMTargetRef target = LLVMGetTargetMachineTarget(comp_ctx->target_machine);
  2538. bh_print_time("Begin to emit object file");
  2539. #if !(defined(_WIN32) || defined(_WIN32_))
  2540. if (comp_ctx->external_llc_compiler || comp_ctx->external_asm_compiler) {
  2541. char cmd[1024];
  2542. int ret;
  2543. if (comp_ctx->external_llc_compiler) {
  2544. const char *stack_usage_flag = "";
  2545. char bc_file_name[64];
  2546. char su_file_name[65]; /* See the comment below */
  2547. if (comp_ctx->stack_usage_file != NULL) {
  2548. /*
  2549. * Note: we know the caller uses 64 byte buffer for
  2550. * file_name. It will get 1 byte longer because we
  2551. * replace ".o" with ".su".
  2552. */
  2553. size_t len = strlen(file_name);
  2554. bh_assert(len + 1 <= sizeof(su_file_name));
  2555. bh_assert(len > 3);
  2556. bh_assert(file_name[len - 2] == '.');
  2557. bh_assert(file_name[len - 1] == 'o');
  2558. snprintf(su_file_name, sizeof(su_file_name), "%.*s.su",
  2559. (int)(len - 2), file_name);
  2560. stack_usage_flag = " -fstack-usage";
  2561. }
  2562. if (!aot_generate_tempfile_name("wamrc-bc", "bc", bc_file_name,
  2563. sizeof(bc_file_name))) {
  2564. return false;
  2565. }
  2566. if (LLVMWriteBitcodeToFile(comp_ctx->module, bc_file_name) != 0) {
  2567. aot_set_last_error("emit llvm bitcode file failed.");
  2568. return false;
  2569. }
  2570. snprintf(cmd, sizeof(cmd), "%s%s %s -o %s %s",
  2571. comp_ctx->external_llc_compiler, stack_usage_flag,
  2572. comp_ctx->llc_compiler_flags ? comp_ctx->llc_compiler_flags
  2573. : "-O3 -c",
  2574. file_name, bc_file_name);
  2575. LOG_VERBOSE("invoking external LLC compiler:\n\t%s", cmd);
  2576. ret = system(cmd);
  2577. /* remove temp bitcode file */
  2578. unlink(bc_file_name);
  2579. if (ret != 0) {
  2580. aot_set_last_error("failed to compile LLVM bitcode to obj file "
  2581. "with external LLC compiler.");
  2582. return false;
  2583. }
  2584. if (comp_ctx->stack_usage_file != NULL) {
  2585. /*
  2586. * move the temporary .su file to the specified location.
  2587. *
  2588. * Note: the former is automatimally inferred from the output
  2589. * filename (file_name here) by clang.
  2590. *
  2591. * Note: the latter might be user-specified.
  2592. * (wamrc --stack-usage=<file>)
  2593. */
  2594. if (!aot_move_file(comp_ctx->stack_usage_file, su_file_name)) {
  2595. aot_set_last_error("failed to move su file.");
  2596. (void)unlink(su_file_name);
  2597. return false;
  2598. }
  2599. }
  2600. }
  2601. else if (comp_ctx->external_asm_compiler) {
  2602. char asm_file_name[64];
  2603. if (!aot_generate_tempfile_name("wamrc-asm", "s", asm_file_name,
  2604. sizeof(asm_file_name))) {
  2605. return false;
  2606. }
  2607. if (LLVMTargetMachineEmitToFile(comp_ctx->target_machine,
  2608. comp_ctx->module, asm_file_name,
  2609. LLVMAssemblyFile, &err)
  2610. != 0) {
  2611. if (err) {
  2612. LLVMDisposeMessage(err);
  2613. err = NULL;
  2614. }
  2615. aot_set_last_error("emit elf to assembly file failed.");
  2616. return false;
  2617. }
  2618. snprintf(cmd, sizeof(cmd), "%s %s -o %s %s",
  2619. comp_ctx->external_asm_compiler,
  2620. comp_ctx->asm_compiler_flags ? comp_ctx->asm_compiler_flags
  2621. : "-O3 -c",
  2622. file_name, asm_file_name);
  2623. LOG_VERBOSE("invoking external ASM compiler:\n\t%s", cmd);
  2624. ret = system(cmd);
  2625. /* remove temp assembly file */
  2626. unlink(asm_file_name);
  2627. if (ret != 0) {
  2628. aot_set_last_error("failed to compile Assembly file to obj "
  2629. "file with external ASM compiler.");
  2630. return false;
  2631. }
  2632. }
  2633. return true;
  2634. }
  2635. #endif /* end of !(defined(_WIN32) || defined(_WIN32_)) */
  2636. if (!strncmp(LLVMGetTargetName(target), "arc", 3))
  2637. /* Emit to assmelby file instead for arc target
  2638. as it cannot emit to object file */
  2639. file_type = LLVMAssemblyFile;
  2640. if (LLVMTargetMachineEmitToFile(comp_ctx->target_machine, comp_ctx->module,
  2641. file_name, file_type, &err)
  2642. != 0) {
  2643. if (err) {
  2644. LLVMDisposeMessage(err);
  2645. err = NULL;
  2646. }
  2647. aot_set_last_error("emit elf to object file failed.");
  2648. return false;
  2649. }
  2650. return true;
  2651. }