aot_compiler.c 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "aot_compiler.h"
  6. #include "aot_emit_compare.h"
  7. #include "aot_emit_conversion.h"
  8. #include "aot_emit_memory.h"
  9. #include "aot_emit_variable.h"
  10. #include "aot_emit_const.h"
  11. #include "aot_emit_exception.h"
  12. #include "aot_emit_numberic.h"
  13. #include "aot_emit_control.h"
  14. #include "aot_emit_function.h"
  15. #include "aot_emit_parametric.h"
  16. #include "aot_emit_table.h"
  17. #include "simd/simd_access_lanes.h"
  18. #include "simd/simd_bitmask_extracts.h"
  19. #include "simd/simd_bit_shifts.h"
  20. #include "simd/simd_bitwise_ops.h"
  21. #include "simd/simd_bool_reductions.h"
  22. #include "simd/simd_comparisons.h"
  23. #include "simd/simd_conversions.h"
  24. #include "simd/simd_construct_values.h"
  25. #include "simd/simd_conversions.h"
  26. #include "simd/simd_floating_point.h"
  27. #include "simd/simd_int_arith.h"
  28. #include "simd/simd_load_store.h"
  29. #include "simd/simd_sat_int_arith.h"
  30. #include "../aot/aot_runtime.h"
  31. #include "../interpreter/wasm_opcode.h"
  32. #include <errno.h>
  33. #if WASM_ENABLE_DEBUG_AOT != 0
  34. #include "debug/dwarf_extractor.h"
  35. #endif
  36. #define CHECK_BUF(buf, buf_end, length) \
  37. do { \
  38. if (buf + length > buf_end) { \
  39. aot_set_last_error("read leb failed: unexpected end."); \
  40. return false; \
  41. } \
  42. } while (0)
  43. static bool
  44. read_leb(const uint8 *buf, const uint8 *buf_end, uint32 *p_offset,
  45. uint32 maxbits, bool sign, uint64 *p_result)
  46. {
  47. uint64 result = 0;
  48. uint32 shift = 0;
  49. uint32 bcnt = 0;
  50. uint64 byte;
  51. while (true) {
  52. CHECK_BUF(buf, buf_end, 1);
  53. byte = buf[*p_offset];
  54. *p_offset += 1;
  55. result |= ((byte & 0x7f) << shift);
  56. shift += 7;
  57. if ((byte & 0x80) == 0) {
  58. break;
  59. }
  60. bcnt += 1;
  61. }
  62. if (bcnt > (maxbits + 6) / 7) {
  63. aot_set_last_error("read leb failed: "
  64. "integer representation too long");
  65. return false;
  66. }
  67. if (sign && (shift < maxbits) && (byte & 0x40)) {
  68. /* Sign extend */
  69. result |= (~((uint64)0)) << shift;
  70. }
  71. *p_result = result;
  72. return true;
  73. }
  74. #define read_leb_uint32(p, p_end, res) \
  75. do { \
  76. uint32 off = 0; \
  77. uint64 res64; \
  78. if (!read_leb(p, p_end, &off, 32, false, &res64)) \
  79. return false; \
  80. p += off; \
  81. res = (uint32)res64; \
  82. } while (0)
  83. #define read_leb_int32(p, p_end, res) \
  84. do { \
  85. uint32 off = 0; \
  86. uint64 res64; \
  87. if (!read_leb(p, p_end, &off, 32, true, &res64)) \
  88. return false; \
  89. p += off; \
  90. res = (int32)res64; \
  91. } while (0)
  92. #define read_leb_int64(p, p_end, res) \
  93. do { \
  94. uint32 off = 0; \
  95. uint64 res64; \
  96. if (!read_leb(p, p_end, &off, 64, true, &res64)) \
  97. return false; \
  98. p += off; \
  99. res = (int64)res64; \
  100. } while (0)
  101. /**
  102. * Since Wamrc uses a full feature Wasm loader,
  103. * add a post-validator here to run checks according
  104. * to options, like enable_tail_call, enable_ref_types,
  105. * and so on.
  106. */
  107. static bool
  108. aot_validate_wasm(AOTCompContext *comp_ctx)
  109. {
  110. if (!comp_ctx->enable_ref_types) {
  111. /* Doesn't support multiple tables unless enabling reference type */
  112. if (comp_ctx->comp_data->import_table_count
  113. + comp_ctx->comp_data->table_count
  114. > 1) {
  115. aot_set_last_error("multiple tables");
  116. return false;
  117. }
  118. }
  119. return true;
  120. }
  121. #define COMPILE_ATOMIC_RMW(OP, NAME) \
  122. case WASM_OP_ATOMIC_RMW_I32_##NAME: \
  123. bytes = 4; \
  124. op_type = VALUE_TYPE_I32; \
  125. goto OP_ATOMIC_##OP; \
  126. case WASM_OP_ATOMIC_RMW_I64_##NAME: \
  127. bytes = 8; \
  128. op_type = VALUE_TYPE_I64; \
  129. goto OP_ATOMIC_##OP; \
  130. case WASM_OP_ATOMIC_RMW_I32_##NAME##8_U: \
  131. bytes = 1; \
  132. op_type = VALUE_TYPE_I32; \
  133. goto OP_ATOMIC_##OP; \
  134. case WASM_OP_ATOMIC_RMW_I32_##NAME##16_U: \
  135. bytes = 2; \
  136. op_type = VALUE_TYPE_I32; \
  137. goto OP_ATOMIC_##OP; \
  138. case WASM_OP_ATOMIC_RMW_I64_##NAME##8_U: \
  139. bytes = 1; \
  140. op_type = VALUE_TYPE_I64; \
  141. goto OP_ATOMIC_##OP; \
  142. case WASM_OP_ATOMIC_RMW_I64_##NAME##16_U: \
  143. bytes = 2; \
  144. op_type = VALUE_TYPE_I64; \
  145. goto OP_ATOMIC_##OP; \
  146. case WASM_OP_ATOMIC_RMW_I64_##NAME##32_U: \
  147. bytes = 4; \
  148. op_type = VALUE_TYPE_I64; \
  149. OP_ATOMIC_##OP : bin_op = LLVMAtomicRMWBinOp##OP; \
  150. goto build_atomic_rmw;
  151. static bool
  152. aot_compile_func(AOTCompContext *comp_ctx, uint32 func_index)
  153. {
  154. AOTFuncContext *func_ctx = comp_ctx->func_ctxes[func_index];
  155. uint8 *frame_ip = func_ctx->aot_func->code, opcode, *p_f32, *p_f64;
  156. uint8 *frame_ip_end = frame_ip + func_ctx->aot_func->code_size;
  157. uint8 *param_types = NULL;
  158. uint8 *result_types = NULL;
  159. uint8 value_type;
  160. uint16 param_count;
  161. uint16 result_count;
  162. uint32 br_depth, *br_depths, br_count;
  163. uint32 func_idx, type_idx, mem_idx, local_idx, global_idx, i;
  164. uint32 bytes = 4, align, offset;
  165. uint32 type_index;
  166. bool sign = true;
  167. int32 i32_const;
  168. int64 i64_const;
  169. float32 f32_const;
  170. float64 f64_const;
  171. AOTFuncType *func_type = NULL;
  172. #if WASM_ENABLE_DEBUG_AOT != 0
  173. LLVMMetadataRef location;
  174. #endif
  175. /* Start to translate the opcodes */
  176. LLVMPositionBuilderAtEnd(
  177. comp_ctx->builder,
  178. func_ctx->block_stack.block_list_head->llvm_entry_block);
  179. while (frame_ip < frame_ip_end) {
  180. opcode = *frame_ip++;
  181. #if WASM_ENABLE_DEBUG_AOT != 0
  182. location = dwarf_gen_location(
  183. comp_ctx, func_ctx,
  184. (frame_ip - 1) - comp_ctx->comp_data->wasm_module->buf_code);
  185. LLVMSetCurrentDebugLocation2(comp_ctx->builder, location);
  186. #endif
  187. switch (opcode) {
  188. case WASM_OP_UNREACHABLE:
  189. if (!aot_compile_op_unreachable(comp_ctx, func_ctx, &frame_ip))
  190. return false;
  191. break;
  192. case WASM_OP_NOP:
  193. break;
  194. case WASM_OP_BLOCK:
  195. case WASM_OP_LOOP:
  196. case WASM_OP_IF:
  197. value_type = *frame_ip++;
  198. if (value_type == VALUE_TYPE_I32 || value_type == VALUE_TYPE_I64
  199. || value_type == VALUE_TYPE_F32
  200. || value_type == VALUE_TYPE_F64
  201. || value_type == VALUE_TYPE_V128
  202. || value_type == VALUE_TYPE_VOID
  203. || value_type == VALUE_TYPE_FUNCREF
  204. || value_type == VALUE_TYPE_EXTERNREF) {
  205. param_count = 0;
  206. param_types = NULL;
  207. if (value_type == VALUE_TYPE_VOID) {
  208. result_count = 0;
  209. result_types = NULL;
  210. }
  211. else {
  212. result_count = 1;
  213. result_types = &value_type;
  214. }
  215. }
  216. else {
  217. frame_ip--;
  218. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  219. func_type = comp_ctx->comp_data->func_types[type_index];
  220. param_count = func_type->param_count;
  221. param_types = func_type->types;
  222. result_count = func_type->result_count;
  223. result_types = func_type->types + param_count;
  224. }
  225. if (!aot_compile_op_block(
  226. comp_ctx, func_ctx, &frame_ip, frame_ip_end,
  227. (uint32)(LABEL_TYPE_BLOCK + opcode - WASM_OP_BLOCK),
  228. param_count, param_types, result_count, result_types))
  229. return false;
  230. break;
  231. case WASM_OP_ELSE:
  232. if (!aot_compile_op_else(comp_ctx, func_ctx, &frame_ip))
  233. return false;
  234. break;
  235. case WASM_OP_END:
  236. if (!aot_compile_op_end(comp_ctx, func_ctx, &frame_ip))
  237. return false;
  238. break;
  239. case WASM_OP_BR:
  240. read_leb_uint32(frame_ip, frame_ip_end, br_depth);
  241. if (!aot_compile_op_br(comp_ctx, func_ctx, br_depth, &frame_ip))
  242. return false;
  243. break;
  244. case WASM_OP_BR_IF:
  245. read_leb_uint32(frame_ip, frame_ip_end, br_depth);
  246. if (!aot_compile_op_br_if(comp_ctx, func_ctx, br_depth,
  247. &frame_ip))
  248. return false;
  249. break;
  250. case WASM_OP_BR_TABLE:
  251. read_leb_uint32(frame_ip, frame_ip_end, br_count);
  252. if (!(br_depths = wasm_runtime_malloc((uint32)sizeof(uint32)
  253. * (br_count + 1)))) {
  254. aot_set_last_error("allocate memory failed.");
  255. goto fail;
  256. }
  257. for (i = 0; i <= br_count; i++)
  258. read_leb_uint32(frame_ip, frame_ip_end, br_depths[i]);
  259. if (!aot_compile_op_br_table(comp_ctx, func_ctx, br_depths,
  260. br_count, &frame_ip)) {
  261. wasm_runtime_free(br_depths);
  262. return false;
  263. }
  264. wasm_runtime_free(br_depths);
  265. break;
  266. case WASM_OP_RETURN:
  267. if (!aot_compile_op_return(comp_ctx, func_ctx, &frame_ip))
  268. return false;
  269. break;
  270. case WASM_OP_CALL:
  271. read_leb_uint32(frame_ip, frame_ip_end, func_idx);
  272. if (!aot_compile_op_call(comp_ctx, func_ctx, func_idx, false))
  273. return false;
  274. break;
  275. case WASM_OP_CALL_INDIRECT:
  276. {
  277. uint32 tbl_idx;
  278. read_leb_uint32(frame_ip, frame_ip_end, type_idx);
  279. #if WASM_ENABLE_REF_TYPES != 0
  280. if (comp_ctx->enable_ref_types) {
  281. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  282. }
  283. else
  284. #endif
  285. {
  286. frame_ip++;
  287. tbl_idx = 0;
  288. }
  289. if (!aot_compile_op_call_indirect(comp_ctx, func_ctx, type_idx,
  290. tbl_idx))
  291. return false;
  292. break;
  293. }
  294. #if WASM_ENABLE_TAIL_CALL != 0
  295. case WASM_OP_RETURN_CALL:
  296. if (!comp_ctx->enable_tail_call) {
  297. aot_set_last_error("unsupported opcode");
  298. return false;
  299. }
  300. read_leb_uint32(frame_ip, frame_ip_end, func_idx);
  301. if (!aot_compile_op_call(comp_ctx, func_ctx, func_idx, true))
  302. return false;
  303. if (!aot_compile_op_return(comp_ctx, func_ctx, &frame_ip))
  304. return false;
  305. break;
  306. case WASM_OP_RETURN_CALL_INDIRECT:
  307. {
  308. uint32 tbl_idx;
  309. if (!comp_ctx->enable_tail_call) {
  310. aot_set_last_error("unsupported opcode");
  311. return false;
  312. }
  313. read_leb_uint32(frame_ip, frame_ip_end, type_idx);
  314. #if WASM_ENABLE_REF_TYPES != 0
  315. if (comp_ctx->enable_ref_types) {
  316. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  317. }
  318. else
  319. #endif
  320. {
  321. frame_ip++;
  322. tbl_idx = 0;
  323. }
  324. if (!aot_compile_op_call_indirect(comp_ctx, func_ctx, type_idx,
  325. tbl_idx))
  326. return false;
  327. if (!aot_compile_op_return(comp_ctx, func_ctx, &frame_ip))
  328. return false;
  329. break;
  330. }
  331. #endif /* end of WASM_ENABLE_TAIL_CALL */
  332. case WASM_OP_DROP:
  333. if (!aot_compile_op_drop(comp_ctx, func_ctx, true))
  334. return false;
  335. break;
  336. case WASM_OP_DROP_64:
  337. if (!aot_compile_op_drop(comp_ctx, func_ctx, false))
  338. return false;
  339. break;
  340. case WASM_OP_SELECT:
  341. if (!aot_compile_op_select(comp_ctx, func_ctx, true))
  342. return false;
  343. break;
  344. case WASM_OP_SELECT_64:
  345. if (!aot_compile_op_select(comp_ctx, func_ctx, false))
  346. return false;
  347. break;
  348. #if WASM_ENABLE_REF_TYPES != 0
  349. case WASM_OP_SELECT_T:
  350. {
  351. uint32 vec_len;
  352. if (!comp_ctx->enable_ref_types) {
  353. goto unsupport_ref_types;
  354. }
  355. read_leb_uint32(frame_ip, frame_ip_end, vec_len);
  356. bh_assert(vec_len == 1);
  357. (void)vec_len;
  358. type_idx = *frame_ip++;
  359. if (!aot_compile_op_select(comp_ctx, func_ctx,
  360. (type_idx != VALUE_TYPE_I64)
  361. && (type_idx != VALUE_TYPE_F64)))
  362. return false;
  363. break;
  364. }
  365. case WASM_OP_TABLE_GET:
  366. {
  367. uint32 tbl_idx;
  368. if (!comp_ctx->enable_ref_types) {
  369. goto unsupport_ref_types;
  370. }
  371. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  372. if (!aot_compile_op_table_get(comp_ctx, func_ctx, tbl_idx))
  373. return false;
  374. break;
  375. }
  376. case WASM_OP_TABLE_SET:
  377. {
  378. uint32 tbl_idx;
  379. if (!comp_ctx->enable_ref_types) {
  380. goto unsupport_ref_types;
  381. }
  382. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  383. if (!aot_compile_op_table_set(comp_ctx, func_ctx, tbl_idx))
  384. return false;
  385. break;
  386. }
  387. case WASM_OP_REF_NULL:
  388. {
  389. uint32 type;
  390. if (!comp_ctx->enable_ref_types) {
  391. goto unsupport_ref_types;
  392. }
  393. read_leb_uint32(frame_ip, frame_ip_end, type);
  394. if (!aot_compile_op_ref_null(comp_ctx, func_ctx))
  395. return false;
  396. (void)type;
  397. break;
  398. }
  399. case WASM_OP_REF_IS_NULL:
  400. {
  401. if (!comp_ctx->enable_ref_types) {
  402. goto unsupport_ref_types;
  403. }
  404. if (!aot_compile_op_ref_is_null(comp_ctx, func_ctx))
  405. return false;
  406. break;
  407. }
  408. case WASM_OP_REF_FUNC:
  409. {
  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. }