aot_compiler.c 109 KB

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