aot_compiler.c 109 KB

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