aot_compiler.c 108 KB

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