aot_compiler.c 111 KB

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