aot_compiler.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342
  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_construct_values.h"
  24. #include "simd/simd_conversions.h"
  25. #include "simd/simd_floating_point.h"
  26. #include "simd/simd_int_arith.h"
  27. #include "simd/simd_load_store.h"
  28. #include "simd/simd_sat_int_arith.h"
  29. #include "../aot/aot_runtime.h"
  30. #include "../interpreter/wasm_opcode.h"
  31. #include <errno.h>
  32. #define CHECK_BUF(buf, buf_end, length) do { \
  33. if (buf + length > buf_end) { \
  34. aot_set_last_error("read leb failed: unexpected end."); \
  35. return false; \
  36. } \
  37. } while (0)
  38. static bool
  39. read_leb(const uint8 *buf, const uint8 *buf_end,
  40. uint32 *p_offset, uint32 maxbits,
  41. bool sign, uint64 *p_result)
  42. {
  43. uint64 result = 0;
  44. uint32 shift = 0;
  45. uint32 bcnt = 0;
  46. uint64 byte;
  47. while (true) {
  48. CHECK_BUF(buf, buf_end, 1);
  49. byte = buf[*p_offset];
  50. *p_offset += 1;
  51. result |= ((byte & 0x7f) << shift);
  52. shift += 7;
  53. if ((byte & 0x80) == 0) {
  54. break;
  55. }
  56. bcnt += 1;
  57. }
  58. if (bcnt > (((maxbits + 8) >> 3) - (maxbits + 8))) {
  59. aot_set_last_error("read leb failed: unsigned leb overflow.");
  60. return false;
  61. }
  62. if (sign && (shift < maxbits) && (byte & 0x40)) {
  63. /* Sign extend */
  64. result |= (uint64)(- (((uint64)1) << shift));
  65. }
  66. *p_result = result;
  67. return true;
  68. }
  69. #define read_leb_uint32(p, p_end, res) do { \
  70. uint32 off = 0; \
  71. uint64 res64; \
  72. if (!read_leb(p, p_end, &off, 32, false, &res64)) \
  73. return false; \
  74. p += off; \
  75. res = (uint32)res64; \
  76. } while (0)
  77. #define read_leb_int32(p, p_end, res) do { \
  78. uint32 off = 0; \
  79. uint64 res64; \
  80. if (!read_leb(p, p_end, &off, 32, true, &res64)) \
  81. return false; \
  82. p += off; \
  83. res = (int32)res64; \
  84. } while (0)
  85. #define read_leb_int64(p, p_end, res) do { \
  86. uint32 off = 0; \
  87. uint64 res64; \
  88. if (!read_leb(p, p_end, &off, 64, true, &res64)) \
  89. return false; \
  90. p += off; \
  91. res = (int64)res64; \
  92. } while (0)
  93. #define COMPILE_ATOMIC_RMW(OP, NAME) \
  94. case WASM_OP_ATOMIC_RMW_I32_##NAME: \
  95. bytes = 4; \
  96. op_type = VALUE_TYPE_I32; \
  97. goto OP_ATOMIC_##OP; \
  98. case WASM_OP_ATOMIC_RMW_I64_##NAME: \
  99. bytes = 8; \
  100. op_type = VALUE_TYPE_I64; \
  101. goto OP_ATOMIC_##OP; \
  102. case WASM_OP_ATOMIC_RMW_I32_##NAME##8_U: \
  103. bytes = 1; \
  104. op_type = VALUE_TYPE_I32; \
  105. goto OP_ATOMIC_##OP; \
  106. case WASM_OP_ATOMIC_RMW_I32_##NAME##16_U: \
  107. bytes = 2; \
  108. op_type = VALUE_TYPE_I32; \
  109. goto OP_ATOMIC_##OP; \
  110. case WASM_OP_ATOMIC_RMW_I64_##NAME##8_U: \
  111. bytes = 1; \
  112. op_type = VALUE_TYPE_I64; \
  113. goto OP_ATOMIC_##OP; \
  114. case WASM_OP_ATOMIC_RMW_I64_##NAME##16_U: \
  115. bytes = 2; \
  116. op_type = VALUE_TYPE_I64; \
  117. goto OP_ATOMIC_##OP; \
  118. case WASM_OP_ATOMIC_RMW_I64_##NAME##32_U: \
  119. bytes = 4; \
  120. op_type = VALUE_TYPE_I64; \
  121. OP_ATOMIC_##OP: \
  122. bin_op = LLVMAtomicRMWBinOp##OP; \
  123. goto build_atomic_rmw;
  124. static bool
  125. aot_compile_func(AOTCompContext *comp_ctx, uint32 func_index)
  126. {
  127. AOTFuncContext *func_ctx = comp_ctx->func_ctxes[func_index];
  128. uint8 *frame_ip = func_ctx->aot_func->code, opcode, *p_f32, *p_f64;
  129. uint8 *frame_ip_end = frame_ip + func_ctx->aot_func->code_size;
  130. uint8 *param_types = NULL;
  131. uint8 *result_types = NULL;
  132. uint8 value_type;
  133. uint16 param_count;
  134. uint16 result_count;
  135. uint32 br_depth, *br_depths, br_count;
  136. uint32 func_idx, type_idx, mem_idx, local_idx, global_idx, i;
  137. uint32 bytes = 4, align, offset;
  138. uint32 type_index;
  139. bool sign = true;
  140. int32 i32_const;
  141. int64 i64_const;
  142. float32 f32_const;
  143. float64 f64_const;
  144. AOTFuncType *func_type = NULL;
  145. /* Start to translate the opcodes */
  146. LLVMPositionBuilderAtEnd(comp_ctx->builder,
  147. func_ctx->block_stack.block_list_head
  148. ->llvm_entry_block);
  149. while (frame_ip < frame_ip_end) {
  150. opcode = *frame_ip++;
  151. switch (opcode) {
  152. case WASM_OP_UNREACHABLE:
  153. if (!aot_compile_op_unreachable(comp_ctx, func_ctx, &frame_ip))
  154. return false;
  155. break;
  156. case WASM_OP_NOP:
  157. break;
  158. case WASM_OP_BLOCK:
  159. case WASM_OP_LOOP:
  160. case WASM_OP_IF:
  161. value_type = *frame_ip++;
  162. if (value_type == VALUE_TYPE_I32
  163. || value_type == VALUE_TYPE_I64
  164. || value_type == VALUE_TYPE_F32
  165. || value_type == VALUE_TYPE_F64
  166. || value_type == VALUE_TYPE_V128
  167. || value_type == VALUE_TYPE_VOID
  168. || value_type == VALUE_TYPE_FUNCREF
  169. || value_type == VALUE_TYPE_EXTERNREF) {
  170. param_count = 0;
  171. param_types = NULL;
  172. if (value_type == VALUE_TYPE_VOID) {
  173. result_count = 0;
  174. result_types = NULL;
  175. }
  176. else {
  177. result_count = 1;
  178. result_types = &value_type;
  179. }
  180. }
  181. else {
  182. frame_ip--;
  183. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  184. func_type = comp_ctx->comp_data->func_types[type_index];
  185. param_count = func_type->param_count;
  186. param_types = func_type->types;
  187. result_count = func_type->result_count;
  188. result_types = func_type->types + param_count;
  189. }
  190. if (!aot_compile_op_block(comp_ctx, func_ctx,
  191. &frame_ip, frame_ip_end,
  192. (uint32)(LABEL_TYPE_BLOCK + opcode - WASM_OP_BLOCK),
  193. param_count, param_types,
  194. result_count, result_types))
  195. return false;
  196. break;
  197. case WASM_OP_ELSE:
  198. if (!aot_compile_op_else(comp_ctx, func_ctx, &frame_ip))
  199. return false;
  200. break;
  201. case WASM_OP_END:
  202. if (!aot_compile_op_end(comp_ctx, func_ctx, &frame_ip))
  203. return false;
  204. break;
  205. case WASM_OP_BR:
  206. read_leb_uint32(frame_ip, frame_ip_end, br_depth);
  207. if (!aot_compile_op_br(comp_ctx, func_ctx, br_depth, &frame_ip))
  208. return false;
  209. break;
  210. case WASM_OP_BR_IF:
  211. read_leb_uint32(frame_ip, frame_ip_end, br_depth);
  212. if (!aot_compile_op_br_if(comp_ctx, func_ctx, br_depth, &frame_ip))
  213. return false;
  214. break;
  215. case WASM_OP_BR_TABLE:
  216. read_leb_uint32(frame_ip, frame_ip_end, br_count);
  217. if (!(br_depths =
  218. wasm_runtime_malloc((uint32)sizeof(uint32) * (br_count + 1)))) {
  219. aot_set_last_error("allocate memory failed.");
  220. goto fail;
  221. }
  222. for (i = 0; i <= br_count; i++)
  223. read_leb_uint32(frame_ip, frame_ip_end, br_depths[i]);
  224. if (!aot_compile_op_br_table(comp_ctx, func_ctx,
  225. br_depths, br_count, &frame_ip)) {
  226. wasm_runtime_free(br_depths);
  227. return false;
  228. }
  229. wasm_runtime_free(br_depths);
  230. break;
  231. case WASM_OP_RETURN:
  232. if (!aot_compile_op_return(comp_ctx, func_ctx, &frame_ip))
  233. return false;
  234. break;
  235. case WASM_OP_CALL:
  236. read_leb_uint32(frame_ip, frame_ip_end, func_idx);
  237. if (!aot_compile_op_call(comp_ctx, func_ctx, func_idx, false))
  238. return false;
  239. break;
  240. case WASM_OP_CALL_INDIRECT:
  241. {
  242. uint32 tbl_idx;
  243. read_leb_uint32(frame_ip, frame_ip_end, type_idx);
  244. #if WASM_ENABLE_REF_TYPES != 0
  245. if (comp_ctx->enable_ref_types) {
  246. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  247. }
  248. else
  249. #endif
  250. {
  251. frame_ip++;
  252. tbl_idx = 0;
  253. }
  254. if (!aot_compile_op_call_indirect(comp_ctx, func_ctx, type_idx,
  255. tbl_idx))
  256. return false;
  257. break;
  258. }
  259. #if WASM_ENABLE_TAIL_CALL != 0
  260. case WASM_OP_RETURN_CALL:
  261. if (!comp_ctx->enable_tail_call) {
  262. aot_set_last_error("unsupported opcode");
  263. return false;
  264. }
  265. read_leb_uint32(frame_ip, frame_ip_end, func_idx);
  266. if (!aot_compile_op_call(comp_ctx, func_ctx, func_idx, true))
  267. return false;
  268. if (!aot_compile_op_return(comp_ctx, func_ctx, &frame_ip))
  269. return false;
  270. break;
  271. case WASM_OP_RETURN_CALL_INDIRECT:
  272. {
  273. uint32 tbl_idx;
  274. if (!comp_ctx->enable_tail_call) {
  275. aot_set_last_error("unsupported opcode");
  276. return false;
  277. }
  278. read_leb_uint32(frame_ip, frame_ip_end, type_idx);
  279. #if WASM_ENABLE_REF_TYPES != 0
  280. if (comp_ctx->enable_ref_types) {
  281. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  282. }
  283. else
  284. #endif
  285. {
  286. frame_ip++;
  287. tbl_idx = 0;
  288. }
  289. if (!aot_compile_op_call_indirect(comp_ctx, func_ctx, type_idx,
  290. tbl_idx))
  291. return false;
  292. if (!aot_compile_op_return(comp_ctx, func_ctx, &frame_ip))
  293. return false;
  294. break;
  295. }
  296. #endif /* end of WASM_ENABLE_TAIL_CALL */
  297. case WASM_OP_DROP:
  298. if (!aot_compile_op_drop(comp_ctx, func_ctx, true))
  299. return false;
  300. break;
  301. case WASM_OP_DROP_64:
  302. if (!aot_compile_op_drop(comp_ctx, func_ctx, false))
  303. return false;
  304. break;
  305. case WASM_OP_SELECT:
  306. if (!aot_compile_op_select(comp_ctx, func_ctx, true))
  307. return false;
  308. break;
  309. case WASM_OP_SELECT_64:
  310. if (!aot_compile_op_select(comp_ctx, func_ctx, false))
  311. return false;
  312. break;
  313. #if WASM_ENABLE_REF_TYPES != 0
  314. case WASM_OP_SELECT_T:
  315. {
  316. uint32 vec_len;
  317. if (!comp_ctx->enable_ref_types) {
  318. goto unsupport_ref_types;
  319. }
  320. read_leb_uint32(frame_ip, frame_ip_end, vec_len);
  321. bh_assert(vec_len == 1);
  322. vec_len = vec_len;
  323. type_idx = *frame_ip++;
  324. if (!aot_compile_op_select(comp_ctx, func_ctx,
  325. (type_idx != VALUE_TYPE_I64)
  326. && (type_idx != VALUE_TYPE_F64)))
  327. return false;
  328. break;
  329. }
  330. case WASM_OP_TABLE_GET:
  331. {
  332. uint32 tbl_idx;
  333. if (!comp_ctx->enable_ref_types) {
  334. goto unsupport_ref_types;
  335. }
  336. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  337. if (!aot_compile_op_table_get(comp_ctx, func_ctx, tbl_idx))
  338. return false;
  339. break;
  340. }
  341. case WASM_OP_TABLE_SET:
  342. {
  343. uint32 tbl_idx;
  344. if (!comp_ctx->enable_ref_types) {
  345. goto unsupport_ref_types;
  346. }
  347. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  348. if (!aot_compile_op_table_set(comp_ctx, func_ctx, tbl_idx))
  349. return false;
  350. break;
  351. }
  352. case WASM_OP_REF_NULL:
  353. {
  354. uint32 type;
  355. if (!comp_ctx->enable_ref_types) {
  356. goto unsupport_ref_types;
  357. }
  358. read_leb_uint32(frame_ip, frame_ip_end, type);
  359. if (!aot_compile_op_ref_null(comp_ctx, func_ctx))
  360. return false;
  361. (void)type;
  362. break;
  363. }
  364. case WASM_OP_REF_IS_NULL:
  365. {
  366. if (!comp_ctx->enable_ref_types) {
  367. goto unsupport_ref_types;
  368. }
  369. if (!aot_compile_op_ref_is_null(comp_ctx, func_ctx))
  370. return false;
  371. break;
  372. }
  373. case WASM_OP_REF_FUNC:
  374. {
  375. uint32 func_idx;
  376. if (!comp_ctx->enable_ref_types) {
  377. goto unsupport_ref_types;
  378. }
  379. read_leb_uint32(frame_ip, frame_ip_end, func_idx);
  380. if (!aot_compile_op_ref_func(comp_ctx, func_ctx, func_idx))
  381. return false;
  382. break;
  383. }
  384. #endif
  385. case WASM_OP_GET_LOCAL:
  386. read_leb_uint32(frame_ip, frame_ip_end, local_idx);
  387. if (!aot_compile_op_get_local(comp_ctx, func_ctx, local_idx))
  388. return false;
  389. break;
  390. case WASM_OP_SET_LOCAL:
  391. read_leb_uint32(frame_ip, frame_ip_end, local_idx);
  392. if (!aot_compile_op_set_local(comp_ctx, func_ctx, local_idx))
  393. return false;
  394. break;
  395. case WASM_OP_TEE_LOCAL:
  396. read_leb_uint32(frame_ip, frame_ip_end, local_idx);
  397. if (!aot_compile_op_tee_local(comp_ctx, func_ctx, local_idx))
  398. return false;
  399. break;
  400. case WASM_OP_GET_GLOBAL:
  401. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  402. if (!aot_compile_op_get_global(comp_ctx, func_ctx, global_idx))
  403. return false;
  404. break;
  405. case WASM_OP_SET_GLOBAL:
  406. case WASM_OP_SET_GLOBAL_64:
  407. case WASM_OP_SET_GLOBAL_AUX_STACK:
  408. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  409. if (!aot_compile_op_set_global(comp_ctx, func_ctx, global_idx,
  410. opcode == WASM_OP_SET_GLOBAL_AUX_STACK
  411. ? true : false))
  412. return false;
  413. break;
  414. case WASM_OP_I32_LOAD:
  415. bytes = 4;
  416. sign = true;
  417. goto op_i32_load;
  418. case WASM_OP_I32_LOAD8_S:
  419. case WASM_OP_I32_LOAD8_U:
  420. bytes = 1;
  421. sign = (opcode == WASM_OP_I32_LOAD8_S) ? true : false;
  422. goto op_i32_load;
  423. case WASM_OP_I32_LOAD16_S:
  424. case WASM_OP_I32_LOAD16_U:
  425. bytes = 2;
  426. sign = (opcode == WASM_OP_I32_LOAD16_S) ? true : false;
  427. op_i32_load:
  428. read_leb_uint32(frame_ip, frame_ip_end, align);
  429. read_leb_uint32(frame_ip, frame_ip_end, offset);
  430. if (!aot_compile_op_i32_load(comp_ctx, func_ctx, align, offset,
  431. bytes, sign, false))
  432. return false;
  433. break;
  434. case WASM_OP_I64_LOAD:
  435. bytes = 8;
  436. sign = true;
  437. goto op_i64_load;
  438. case WASM_OP_I64_LOAD8_S:
  439. case WASM_OP_I64_LOAD8_U:
  440. bytes = 1;
  441. sign = (opcode == WASM_OP_I64_LOAD8_S) ? true : false;
  442. goto op_i64_load;
  443. case WASM_OP_I64_LOAD16_S:
  444. case WASM_OP_I64_LOAD16_U:
  445. bytes = 2;
  446. sign = (opcode == WASM_OP_I64_LOAD16_S) ? true : false;
  447. goto op_i64_load;
  448. case WASM_OP_I64_LOAD32_S:
  449. case WASM_OP_I64_LOAD32_U:
  450. bytes = 4;
  451. sign = (opcode == WASM_OP_I64_LOAD32_S) ? true : false;
  452. op_i64_load:
  453. read_leb_uint32(frame_ip, frame_ip_end, align);
  454. read_leb_uint32(frame_ip, frame_ip_end, offset);
  455. if (!aot_compile_op_i64_load(comp_ctx, func_ctx, align, offset,
  456. bytes, sign, false))
  457. return false;
  458. break;
  459. case WASM_OP_F32_LOAD:
  460. read_leb_uint32(frame_ip, frame_ip_end, align);
  461. read_leb_uint32(frame_ip, frame_ip_end, offset);
  462. if (!aot_compile_op_f32_load(comp_ctx, func_ctx, align, offset))
  463. return false;
  464. break;
  465. case WASM_OP_F64_LOAD:
  466. read_leb_uint32(frame_ip, frame_ip_end, align);
  467. read_leb_uint32(frame_ip, frame_ip_end, offset);
  468. if (!aot_compile_op_f64_load(comp_ctx, func_ctx, align, offset))
  469. return false;
  470. break;
  471. case WASM_OP_I32_STORE:
  472. bytes = 4;
  473. goto op_i32_store;
  474. case WASM_OP_I32_STORE8:
  475. bytes = 1;
  476. goto op_i32_store;
  477. case WASM_OP_I32_STORE16:
  478. bytes = 2;
  479. op_i32_store:
  480. read_leb_uint32(frame_ip, frame_ip_end, align);
  481. read_leb_uint32(frame_ip, frame_ip_end, offset);
  482. if (!aot_compile_op_i32_store(comp_ctx, func_ctx, align,
  483. offset, bytes, false))
  484. return false;
  485. break;
  486. case WASM_OP_I64_STORE:
  487. bytes = 8;
  488. goto op_i64_store;
  489. case WASM_OP_I64_STORE8:
  490. bytes = 1;
  491. goto op_i64_store;
  492. case WASM_OP_I64_STORE16:
  493. bytes = 2;
  494. goto op_i64_store;
  495. case WASM_OP_I64_STORE32:
  496. bytes = 4;
  497. op_i64_store:
  498. read_leb_uint32(frame_ip, frame_ip_end, align);
  499. read_leb_uint32(frame_ip, frame_ip_end, offset);
  500. if (!aot_compile_op_i64_store(comp_ctx, func_ctx, align,
  501. offset, bytes, false))
  502. return false;
  503. break;
  504. case WASM_OP_F32_STORE:
  505. read_leb_uint32(frame_ip, frame_ip_end, align);
  506. read_leb_uint32(frame_ip, frame_ip_end, offset);
  507. if (!aot_compile_op_f32_store(comp_ctx, func_ctx, align, offset))
  508. return false;
  509. break;
  510. case WASM_OP_F64_STORE:
  511. read_leb_uint32(frame_ip, frame_ip_end, align);
  512. read_leb_uint32(frame_ip, frame_ip_end, offset);
  513. if (!aot_compile_op_f64_store(comp_ctx, func_ctx, align, offset))
  514. return false;
  515. break;
  516. case WASM_OP_MEMORY_SIZE:
  517. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  518. if (!aot_compile_op_memory_size(comp_ctx, func_ctx))
  519. return false;
  520. (void)mem_idx;
  521. break;
  522. case WASM_OP_MEMORY_GROW:
  523. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  524. if (!aot_compile_op_memory_grow(comp_ctx, func_ctx))
  525. return false;
  526. break;
  527. case WASM_OP_I32_CONST:
  528. read_leb_int32(frame_ip, frame_ip_end, i32_const);
  529. if (!aot_compile_op_i32_const(comp_ctx, func_ctx, i32_const))
  530. return false;
  531. break;
  532. case WASM_OP_I64_CONST:
  533. read_leb_int64(frame_ip, frame_ip_end, i64_const);
  534. if (!aot_compile_op_i64_const(comp_ctx, func_ctx, i64_const))
  535. return false;
  536. break;
  537. case WASM_OP_F32_CONST:
  538. p_f32 = (uint8*)&f32_const;
  539. for (i = 0; i < sizeof(float32); i++)
  540. *p_f32++ = *frame_ip++;
  541. if (!aot_compile_op_f32_const(comp_ctx, func_ctx, f32_const))
  542. return false;
  543. break;
  544. case WASM_OP_F64_CONST:
  545. p_f64 = (uint8*)&f64_const;
  546. for (i = 0; i < sizeof(float64); i++)
  547. *p_f64++ = *frame_ip++;
  548. if (!aot_compile_op_f64_const(comp_ctx, func_ctx, f64_const))
  549. return false;
  550. break;
  551. case WASM_OP_I32_EQZ:
  552. case WASM_OP_I32_EQ:
  553. case WASM_OP_I32_NE:
  554. case WASM_OP_I32_LT_S:
  555. case WASM_OP_I32_LT_U:
  556. case WASM_OP_I32_GT_S:
  557. case WASM_OP_I32_GT_U:
  558. case WASM_OP_I32_LE_S:
  559. case WASM_OP_I32_LE_U:
  560. case WASM_OP_I32_GE_S:
  561. case WASM_OP_I32_GE_U:
  562. if (!aot_compile_op_i32_compare(comp_ctx, func_ctx,
  563. INT_EQZ + opcode - WASM_OP_I32_EQZ))
  564. return false;
  565. break;
  566. case WASM_OP_I64_EQZ:
  567. case WASM_OP_I64_EQ:
  568. case WASM_OP_I64_NE:
  569. case WASM_OP_I64_LT_S:
  570. case WASM_OP_I64_LT_U:
  571. case WASM_OP_I64_GT_S:
  572. case WASM_OP_I64_GT_U:
  573. case WASM_OP_I64_LE_S:
  574. case WASM_OP_I64_LE_U:
  575. case WASM_OP_I64_GE_S:
  576. case WASM_OP_I64_GE_U:
  577. if (!aot_compile_op_i64_compare(comp_ctx, func_ctx,
  578. INT_EQZ + opcode - WASM_OP_I64_EQZ))
  579. return false;
  580. break;
  581. case WASM_OP_F32_EQ:
  582. case WASM_OP_F32_NE:
  583. case WASM_OP_F32_LT:
  584. case WASM_OP_F32_GT:
  585. case WASM_OP_F32_LE:
  586. case WASM_OP_F32_GE:
  587. if (!aot_compile_op_f32_compare(comp_ctx, func_ctx,
  588. FLOAT_EQ + opcode - WASM_OP_F32_EQ))
  589. return false;
  590. break;
  591. case WASM_OP_F64_EQ:
  592. case WASM_OP_F64_NE:
  593. case WASM_OP_F64_LT:
  594. case WASM_OP_F64_GT:
  595. case WASM_OP_F64_LE:
  596. case WASM_OP_F64_GE:
  597. if (!aot_compile_op_f64_compare(comp_ctx, func_ctx,
  598. FLOAT_EQ + opcode - WASM_OP_F64_EQ))
  599. return false;
  600. break;
  601. case WASM_OP_I32_CLZ:
  602. if (!aot_compile_op_i32_clz(comp_ctx, func_ctx))
  603. return false;
  604. break;
  605. case WASM_OP_I32_CTZ:
  606. if (!aot_compile_op_i32_ctz(comp_ctx, func_ctx))
  607. return false;
  608. break;
  609. case WASM_OP_I32_POPCNT:
  610. if (!aot_compile_op_i32_popcnt(comp_ctx, func_ctx))
  611. return false;
  612. break;
  613. case WASM_OP_I32_ADD:
  614. case WASM_OP_I32_SUB:
  615. case WASM_OP_I32_MUL:
  616. case WASM_OP_I32_DIV_S:
  617. case WASM_OP_I32_DIV_U:
  618. case WASM_OP_I32_REM_S:
  619. case WASM_OP_I32_REM_U:
  620. if (!aot_compile_op_i32_arithmetic(comp_ctx, func_ctx,
  621. INT_ADD + opcode - WASM_OP_I32_ADD,
  622. &frame_ip))
  623. return false;
  624. break;
  625. case WASM_OP_I32_AND:
  626. case WASM_OP_I32_OR:
  627. case WASM_OP_I32_XOR:
  628. if (!aot_compile_op_i32_bitwise(comp_ctx, func_ctx,
  629. INT_SHL + opcode - WASM_OP_I32_AND))
  630. return false;
  631. break;
  632. case WASM_OP_I32_SHL:
  633. case WASM_OP_I32_SHR_S:
  634. case WASM_OP_I32_SHR_U:
  635. case WASM_OP_I32_ROTL:
  636. case WASM_OP_I32_ROTR:
  637. if (!aot_compile_op_i32_shift(comp_ctx, func_ctx,
  638. INT_SHL + opcode - WASM_OP_I32_SHL))
  639. return false;
  640. break;
  641. case WASM_OP_I64_CLZ:
  642. if (!aot_compile_op_i64_clz(comp_ctx, func_ctx))
  643. return false;
  644. break;
  645. case WASM_OP_I64_CTZ:
  646. if (!aot_compile_op_i64_ctz(comp_ctx, func_ctx))
  647. return false;
  648. break;
  649. case WASM_OP_I64_POPCNT:
  650. if (!aot_compile_op_i64_popcnt(comp_ctx, func_ctx))
  651. return false;
  652. break;
  653. case WASM_OP_I64_ADD:
  654. case WASM_OP_I64_SUB:
  655. case WASM_OP_I64_MUL:
  656. case WASM_OP_I64_DIV_S:
  657. case WASM_OP_I64_DIV_U:
  658. case WASM_OP_I64_REM_S:
  659. case WASM_OP_I64_REM_U:
  660. if (!aot_compile_op_i64_arithmetic(comp_ctx, func_ctx,
  661. INT_ADD + opcode - WASM_OP_I64_ADD,
  662. &frame_ip))
  663. return false;
  664. break;
  665. case WASM_OP_I64_AND:
  666. case WASM_OP_I64_OR:
  667. case WASM_OP_I64_XOR:
  668. if (!aot_compile_op_i64_bitwise(comp_ctx, func_ctx,
  669. INT_SHL + opcode - WASM_OP_I64_AND))
  670. return false;
  671. break;
  672. case WASM_OP_I64_SHL:
  673. case WASM_OP_I64_SHR_S:
  674. case WASM_OP_I64_SHR_U:
  675. case WASM_OP_I64_ROTL:
  676. case WASM_OP_I64_ROTR:
  677. if (!aot_compile_op_i64_shift(comp_ctx, func_ctx,
  678. INT_SHL + opcode - WASM_OP_I64_SHL))
  679. return false;
  680. break;
  681. case WASM_OP_F32_ABS:
  682. case WASM_OP_F32_NEG:
  683. case WASM_OP_F32_CEIL:
  684. case WASM_OP_F32_FLOOR:
  685. case WASM_OP_F32_TRUNC:
  686. case WASM_OP_F32_NEAREST:
  687. case WASM_OP_F32_SQRT:
  688. if (!aot_compile_op_f32_math(comp_ctx, func_ctx,
  689. FLOAT_ABS + opcode - WASM_OP_F32_ABS))
  690. return false;
  691. break;
  692. case WASM_OP_F32_ADD:
  693. case WASM_OP_F32_SUB:
  694. case WASM_OP_F32_MUL:
  695. case WASM_OP_F32_DIV:
  696. case WASM_OP_F32_MIN:
  697. case WASM_OP_F32_MAX:
  698. if (!aot_compile_op_f32_arithmetic(comp_ctx, func_ctx,
  699. FLOAT_ADD + opcode - WASM_OP_F32_ADD))
  700. return false;
  701. break;
  702. case WASM_OP_F32_COPYSIGN:
  703. if (!aot_compile_op_f32_copysign(comp_ctx, func_ctx))
  704. return false;
  705. break;
  706. case WASM_OP_F64_ABS:
  707. case WASM_OP_F64_NEG:
  708. case WASM_OP_F64_CEIL:
  709. case WASM_OP_F64_FLOOR:
  710. case WASM_OP_F64_TRUNC:
  711. case WASM_OP_F64_NEAREST:
  712. case WASM_OP_F64_SQRT:
  713. if (!aot_compile_op_f64_math(comp_ctx, func_ctx,
  714. FLOAT_ABS + opcode - WASM_OP_F64_ABS))
  715. return false;
  716. break;
  717. case WASM_OP_F64_ADD:
  718. case WASM_OP_F64_SUB:
  719. case WASM_OP_F64_MUL:
  720. case WASM_OP_F64_DIV:
  721. case WASM_OP_F64_MIN:
  722. case WASM_OP_F64_MAX:
  723. if (!aot_compile_op_f64_arithmetic(comp_ctx, func_ctx,
  724. FLOAT_ADD + opcode - WASM_OP_F64_ADD))
  725. return false;
  726. break;
  727. case WASM_OP_F64_COPYSIGN:
  728. if (!aot_compile_op_f64_copysign(comp_ctx, func_ctx))
  729. return false;
  730. break;
  731. case WASM_OP_I32_WRAP_I64:
  732. if (!aot_compile_op_i32_wrap_i64(comp_ctx, func_ctx))
  733. return false;
  734. break;
  735. case WASM_OP_I32_TRUNC_S_F32:
  736. case WASM_OP_I32_TRUNC_U_F32:
  737. sign = (opcode == WASM_OP_I32_TRUNC_S_F32) ? true : false;
  738. if (!aot_compile_op_i32_trunc_f32(comp_ctx, func_ctx, sign, false))
  739. return false;
  740. break;
  741. case WASM_OP_I32_TRUNC_S_F64:
  742. case WASM_OP_I32_TRUNC_U_F64:
  743. sign = (opcode == WASM_OP_I32_TRUNC_S_F64) ? true : false;
  744. if (!aot_compile_op_i32_trunc_f64(comp_ctx, func_ctx, sign, false))
  745. return false;
  746. break;
  747. case WASM_OP_I64_EXTEND_S_I32:
  748. case WASM_OP_I64_EXTEND_U_I32:
  749. sign = (opcode == WASM_OP_I64_EXTEND_S_I32) ? true : false;
  750. if (!aot_compile_op_i64_extend_i32(comp_ctx, func_ctx, sign))
  751. return false;
  752. break;
  753. case WASM_OP_I64_TRUNC_S_F32:
  754. case WASM_OP_I64_TRUNC_U_F32:
  755. sign = (opcode == WASM_OP_I64_TRUNC_S_F32) ? true : false;
  756. if (!aot_compile_op_i64_trunc_f32(comp_ctx, func_ctx, sign, false))
  757. return false;
  758. break;
  759. case WASM_OP_I64_TRUNC_S_F64:
  760. case WASM_OP_I64_TRUNC_U_F64:
  761. sign = (opcode == WASM_OP_I64_TRUNC_S_F64) ? true : false;
  762. if (!aot_compile_op_i64_trunc_f64(comp_ctx, func_ctx, sign, false))
  763. return false;
  764. break;
  765. case WASM_OP_F32_CONVERT_S_I32:
  766. case WASM_OP_F32_CONVERT_U_I32:
  767. sign = (opcode == WASM_OP_F32_CONVERT_S_I32) ? true : false;
  768. if (!aot_compile_op_f32_convert_i32(comp_ctx, func_ctx, sign))
  769. return false;
  770. break;
  771. case WASM_OP_F32_CONVERT_S_I64:
  772. case WASM_OP_F32_CONVERT_U_I64:
  773. sign = (opcode == WASM_OP_F32_CONVERT_S_I64) ? true : false;
  774. if (!aot_compile_op_f32_convert_i64(comp_ctx, func_ctx, sign))
  775. return false;
  776. break;
  777. case WASM_OP_F32_DEMOTE_F64:
  778. if (!aot_compile_op_f32_demote_f64(comp_ctx, func_ctx))
  779. return false;
  780. break;
  781. case WASM_OP_F64_CONVERT_S_I32:
  782. case WASM_OP_F64_CONVERT_U_I32:
  783. sign = (opcode == WASM_OP_F64_CONVERT_S_I32) ? true : false;
  784. if (!aot_compile_op_f64_convert_i32(comp_ctx, func_ctx, sign))
  785. return false;
  786. break;
  787. case WASM_OP_F64_CONVERT_S_I64:
  788. case WASM_OP_F64_CONVERT_U_I64:
  789. sign = (opcode == WASM_OP_F64_CONVERT_S_I64) ? true : false;
  790. if (!aot_compile_op_f64_convert_i64(comp_ctx, func_ctx, sign))
  791. return false;
  792. break;
  793. case WASM_OP_F64_PROMOTE_F32:
  794. if (!aot_compile_op_f64_promote_f32(comp_ctx, func_ctx))
  795. return false;
  796. break;
  797. case WASM_OP_I32_REINTERPRET_F32:
  798. if (!aot_compile_op_i32_reinterpret_f32(comp_ctx, func_ctx))
  799. return false;
  800. break;
  801. case WASM_OP_I64_REINTERPRET_F64:
  802. if (!aot_compile_op_i64_reinterpret_f64(comp_ctx, func_ctx))
  803. return false;
  804. break;
  805. case WASM_OP_F32_REINTERPRET_I32:
  806. if (!aot_compile_op_f32_reinterpret_i32(comp_ctx, func_ctx))
  807. return false;
  808. break;
  809. case WASM_OP_F64_REINTERPRET_I64:
  810. if (!aot_compile_op_f64_reinterpret_i64(comp_ctx, func_ctx))
  811. return false;
  812. break;
  813. case WASM_OP_I32_EXTEND8_S:
  814. if (!aot_compile_op_i32_extend_i32(comp_ctx, func_ctx, 8))
  815. return false;
  816. break;
  817. case WASM_OP_I32_EXTEND16_S:
  818. if (!aot_compile_op_i32_extend_i32(comp_ctx, func_ctx, 16))
  819. return false;
  820. break;
  821. case WASM_OP_I64_EXTEND8_S:
  822. if (!aot_compile_op_i64_extend_i64(comp_ctx, func_ctx, 8))
  823. return false;
  824. break;
  825. case WASM_OP_I64_EXTEND16_S:
  826. if (!aot_compile_op_i64_extend_i64(comp_ctx, func_ctx, 16))
  827. return false;
  828. break;
  829. case WASM_OP_I64_EXTEND32_S:
  830. if (!aot_compile_op_i64_extend_i64(comp_ctx, func_ctx, 32))
  831. return false;
  832. break;
  833. case WASM_OP_MISC_PREFIX:
  834. {
  835. uint32 opcode1;
  836. read_leb_uint32(frame_ip, frame_ip_end, opcode1);
  837. opcode = (uint32)opcode1;
  838. //TODO: --enable-bulk-memory ?
  839. #if WASM_ENABLE_REF_TYPES != 0
  840. if (WASM_OP_TABLE_INIT <= opcode && opcode <= WASM_OP_TABLE_FILL
  841. && !comp_ctx->enable_ref_types) {
  842. goto unsupport_ref_types;
  843. }
  844. #endif
  845. switch (opcode) {
  846. case WASM_OP_I32_TRUNC_SAT_S_F32:
  847. case WASM_OP_I32_TRUNC_SAT_U_F32:
  848. sign = (opcode == WASM_OP_I32_TRUNC_SAT_S_F32) ? true : false;
  849. if (!aot_compile_op_i32_trunc_f32(comp_ctx, func_ctx, sign, true))
  850. return false;
  851. break;
  852. case WASM_OP_I32_TRUNC_SAT_S_F64:
  853. case WASM_OP_I32_TRUNC_SAT_U_F64:
  854. sign = (opcode == WASM_OP_I32_TRUNC_SAT_S_F64) ? true : false;
  855. if (!aot_compile_op_i32_trunc_f64(comp_ctx, func_ctx, sign, true))
  856. return false;
  857. break;
  858. case WASM_OP_I64_TRUNC_SAT_S_F32:
  859. case WASM_OP_I64_TRUNC_SAT_U_F32:
  860. sign = (opcode == WASM_OP_I64_TRUNC_SAT_S_F32) ? true : false;
  861. if (!aot_compile_op_i64_trunc_f32(comp_ctx, func_ctx, sign, true))
  862. return false;
  863. break;
  864. case WASM_OP_I64_TRUNC_SAT_S_F64:
  865. case WASM_OP_I64_TRUNC_SAT_U_F64:
  866. sign = (opcode == WASM_OP_I64_TRUNC_SAT_S_F64) ? true : false;
  867. if (!aot_compile_op_i64_trunc_f64(comp_ctx, func_ctx, sign, true))
  868. return false;
  869. break;
  870. #if WASM_ENABLE_BULK_MEMORY != 0
  871. case WASM_OP_MEMORY_INIT:
  872. {
  873. uint32 seg_index;
  874. read_leb_uint32(frame_ip, frame_ip_end, seg_index);
  875. frame_ip ++;
  876. if (!aot_compile_op_memory_init(comp_ctx, func_ctx, seg_index))
  877. return false;
  878. break;
  879. }
  880. case WASM_OP_DATA_DROP:
  881. {
  882. uint32 seg_index;
  883. read_leb_uint32(frame_ip, frame_ip_end, seg_index);
  884. if (!aot_compile_op_data_drop(comp_ctx, func_ctx, seg_index))
  885. return false;
  886. break;
  887. }
  888. case WASM_OP_MEMORY_COPY:
  889. {
  890. frame_ip += 2;
  891. if (!aot_compile_op_memory_copy(comp_ctx, func_ctx))
  892. return false;
  893. break;
  894. }
  895. case WASM_OP_MEMORY_FILL:
  896. {
  897. frame_ip ++;
  898. if (!aot_compile_op_memory_fill(comp_ctx, func_ctx))
  899. return false;
  900. break;
  901. }
  902. #endif /* WASM_ENABLE_BULK_MEMORY */
  903. #if WASM_ENABLE_REF_TYPES != 0
  904. case WASM_OP_TABLE_INIT:
  905. {
  906. uint32 tbl_idx, tbl_seg_idx;
  907. read_leb_uint32(frame_ip, frame_ip_end, tbl_seg_idx);
  908. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  909. if (!aot_compile_op_table_init(comp_ctx, func_ctx, tbl_idx,
  910. tbl_seg_idx))
  911. return false;
  912. break;
  913. }
  914. case WASM_OP_ELEM_DROP:
  915. {
  916. uint32 tbl_seg_idx;
  917. read_leb_uint32(frame_ip, frame_ip_end, tbl_seg_idx);
  918. if (!aot_compile_op_elem_drop(comp_ctx, func_ctx, tbl_seg_idx))
  919. return false;
  920. break;
  921. }
  922. case WASM_OP_TABLE_COPY:
  923. {
  924. uint32 src_tbl_idx, dst_tbl_idx;
  925. read_leb_uint32(frame_ip, frame_ip_end, dst_tbl_idx);
  926. read_leb_uint32(frame_ip, frame_ip_end, src_tbl_idx);
  927. if (!aot_compile_op_table_copy(comp_ctx, func_ctx, src_tbl_idx,
  928. dst_tbl_idx))
  929. return false;
  930. break;
  931. }
  932. case WASM_OP_TABLE_GROW:
  933. {
  934. uint32 tbl_idx;
  935. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  936. if (!aot_compile_op_table_grow(comp_ctx, func_ctx, tbl_idx))
  937. return false;
  938. break;
  939. }
  940. case WASM_OP_TABLE_SIZE:
  941. {
  942. uint32 tbl_idx;
  943. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  944. if (!aot_compile_op_table_size(comp_ctx, func_ctx, tbl_idx))
  945. return false;
  946. break;
  947. }
  948. case WASM_OP_TABLE_FILL:
  949. {
  950. uint32 tbl_idx;
  951. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  952. if (!aot_compile_op_table_fill(comp_ctx, func_ctx, tbl_idx))
  953. return false;
  954. break;
  955. }
  956. #endif /* WASM_ENABLE_REF_TYPES */
  957. default:
  958. aot_set_last_error("unsupported opcode");
  959. return false;
  960. }
  961. break;
  962. }
  963. #if WASM_ENABLE_SHARED_MEMORY != 0
  964. case WASM_OP_ATOMIC_PREFIX:
  965. {
  966. uint8 bin_op, op_type;
  967. if (frame_ip < frame_ip_end) {
  968. opcode = *frame_ip++;
  969. }
  970. if (opcode != WASM_OP_ATOMIC_FENCE) {
  971. read_leb_uint32(frame_ip, frame_ip_end, align);
  972. read_leb_uint32(frame_ip, frame_ip_end, offset);
  973. }
  974. switch (opcode) {
  975. case WASM_OP_ATOMIC_WAIT32:
  976. if (!aot_compile_op_atomic_wait(comp_ctx, func_ctx, VALUE_TYPE_I32,
  977. align, offset, 4))
  978. return false;
  979. break;
  980. case WASM_OP_ATOMIC_WAIT64:
  981. if (!aot_compile_op_atomic_wait(comp_ctx, func_ctx, VALUE_TYPE_I64,
  982. align, offset, 8))
  983. return false;
  984. break;
  985. case WASM_OP_ATOMIC_NOTIFY:
  986. if (!aot_compiler_op_atomic_notify(comp_ctx, func_ctx, align,
  987. offset, bytes))
  988. return false;
  989. break;
  990. case WASM_OP_ATOMIC_I32_LOAD:
  991. bytes = 4;
  992. goto op_atomic_i32_load;
  993. case WASM_OP_ATOMIC_I32_LOAD8_U:
  994. bytes = 1;
  995. goto op_atomic_i32_load;
  996. case WASM_OP_ATOMIC_I32_LOAD16_U:
  997. bytes = 2;
  998. op_atomic_i32_load:
  999. if (!aot_compile_op_i32_load(comp_ctx, func_ctx, align,
  1000. offset, bytes, sign, true))
  1001. return false;
  1002. break;
  1003. case WASM_OP_ATOMIC_I64_LOAD:
  1004. bytes = 8;
  1005. goto op_atomic_i64_load;
  1006. case WASM_OP_ATOMIC_I64_LOAD8_U:
  1007. bytes = 1;
  1008. goto op_atomic_i64_load;
  1009. case WASM_OP_ATOMIC_I64_LOAD16_U:
  1010. bytes = 2;
  1011. goto op_atomic_i64_load;
  1012. case WASM_OP_ATOMIC_I64_LOAD32_U:
  1013. bytes = 4;
  1014. op_atomic_i64_load:
  1015. if (!aot_compile_op_i64_load(comp_ctx, func_ctx, align,
  1016. offset, bytes, sign, true))
  1017. return false;
  1018. break;
  1019. case WASM_OP_ATOMIC_I32_STORE:
  1020. bytes = 4;
  1021. goto op_atomic_i32_store;
  1022. case WASM_OP_ATOMIC_I32_STORE8:
  1023. bytes = 1;
  1024. goto op_atomic_i32_store;
  1025. case WASM_OP_ATOMIC_I32_STORE16:
  1026. bytes = 2;
  1027. op_atomic_i32_store:
  1028. if (!aot_compile_op_i32_store(comp_ctx, func_ctx, align,
  1029. offset, bytes, true))
  1030. return false;
  1031. break;
  1032. case WASM_OP_ATOMIC_I64_STORE:
  1033. bytes = 8;
  1034. goto op_atomic_i64_store;
  1035. case WASM_OP_ATOMIC_I64_STORE8:
  1036. bytes = 1;
  1037. goto op_atomic_i64_store;
  1038. case WASM_OP_ATOMIC_I64_STORE16:
  1039. bytes = 2;
  1040. goto op_atomic_i64_store;
  1041. case WASM_OP_ATOMIC_I64_STORE32:
  1042. bytes = 4;
  1043. op_atomic_i64_store:
  1044. if (!aot_compile_op_i64_store(comp_ctx, func_ctx, align,
  1045. offset, bytes, true))
  1046. return false;
  1047. break;
  1048. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  1049. bytes = 4;
  1050. op_type = VALUE_TYPE_I32;
  1051. goto op_atomic_cmpxchg;
  1052. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  1053. bytes = 8;
  1054. op_type = VALUE_TYPE_I64;
  1055. goto op_atomic_cmpxchg;
  1056. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  1057. bytes = 1;
  1058. op_type = VALUE_TYPE_I32;
  1059. goto op_atomic_cmpxchg;
  1060. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  1061. bytes = 2;
  1062. op_type = VALUE_TYPE_I32;
  1063. goto op_atomic_cmpxchg;
  1064. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  1065. bytes = 1;
  1066. op_type = VALUE_TYPE_I64;
  1067. goto op_atomic_cmpxchg;
  1068. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  1069. bytes = 2;
  1070. op_type = VALUE_TYPE_I64;
  1071. goto op_atomic_cmpxchg;
  1072. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  1073. bytes = 4;
  1074. op_type = VALUE_TYPE_I64;
  1075. op_atomic_cmpxchg:
  1076. if (!aot_compile_op_atomic_cmpxchg(comp_ctx, func_ctx,
  1077. op_type, align,
  1078. offset, bytes))
  1079. return false;
  1080. break;
  1081. COMPILE_ATOMIC_RMW(Add, ADD);
  1082. COMPILE_ATOMIC_RMW(Sub, SUB);
  1083. COMPILE_ATOMIC_RMW(And, AND);
  1084. COMPILE_ATOMIC_RMW(Or, OR);
  1085. COMPILE_ATOMIC_RMW(Xor, XOR);
  1086. COMPILE_ATOMIC_RMW(Xchg, XCHG);
  1087. build_atomic_rmw:
  1088. if (!aot_compile_op_atomic_rmw(comp_ctx, func_ctx,
  1089. bin_op, op_type,
  1090. align, offset, bytes))
  1091. return false;
  1092. break;
  1093. default:
  1094. aot_set_last_error("unsupported opcode");
  1095. return false;
  1096. }
  1097. break;
  1098. }
  1099. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  1100. #if WASM_ENABLE_SIMD != 0
  1101. case WASM_OP_SIMD_PREFIX:
  1102. {
  1103. if (!comp_ctx->enable_simd) {
  1104. goto unsupport_simd;
  1105. }
  1106. opcode = *frame_ip++;
  1107. switch (opcode) {
  1108. case SIMD_v128_load:
  1109. {
  1110. read_leb_uint32(frame_ip, frame_ip_end, align);
  1111. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1112. if (!aot_compile_simd_v128_load(comp_ctx, func_ctx, align, offset))
  1113. return false;
  1114. break;
  1115. }
  1116. case SIMD_i16x8_load8x8_s:
  1117. case SIMD_i16x8_load8x8_u:
  1118. case SIMD_i32x4_load16x4_s:
  1119. case SIMD_i32x4_load16x4_u:
  1120. case SIMD_i64x2_load32x2_s:
  1121. case SIMD_i64x2_load32x2_u:
  1122. {
  1123. read_leb_uint32(frame_ip, frame_ip_end, align);
  1124. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1125. if (!aot_compile_simd_load_extend(comp_ctx, func_ctx,
  1126. opcode, align, offset))
  1127. return false;
  1128. break;
  1129. }
  1130. case SIMD_v8x16_load_splat:
  1131. case SIMD_v16x8_load_splat:
  1132. case SIMD_v32x4_load_splat:
  1133. case SIMD_v64x2_load_splat:
  1134. {
  1135. read_leb_uint32(frame_ip, frame_ip_end, align);
  1136. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1137. if (!aot_compile_simd_load_splat(comp_ctx, func_ctx,
  1138. opcode, align, offset))
  1139. return false;
  1140. break;
  1141. }
  1142. case SIMD_v128_store:
  1143. {
  1144. read_leb_uint32(frame_ip, frame_ip_end, align);
  1145. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1146. if (!aot_compile_simd_v128_store(comp_ctx, func_ctx, align, offset))
  1147. return false;
  1148. break;
  1149. }
  1150. case SIMD_v128_const:
  1151. {
  1152. if (!aot_compile_simd_v128_const(comp_ctx, func_ctx, frame_ip))
  1153. return false;
  1154. frame_ip += 16;
  1155. break;
  1156. }
  1157. case SIMD_v8x16_shuffle:
  1158. {
  1159. if (!aot_compile_simd_shuffle(comp_ctx, func_ctx, frame_ip))
  1160. return false;
  1161. frame_ip += 16;
  1162. break;
  1163. }
  1164. case SIMD_v8x16_swizzle:
  1165. {
  1166. if (!aot_compile_simd_swizzle(comp_ctx, func_ctx))
  1167. return false;
  1168. break;
  1169. }
  1170. case SIMD_i8x16_splat:
  1171. case SIMD_i16x8_splat:
  1172. case SIMD_i32x4_splat:
  1173. case SIMD_i64x2_splat:
  1174. case SIMD_f32x4_splat:
  1175. case SIMD_f64x2_splat:
  1176. {
  1177. if (!aot_compile_simd_splat(comp_ctx, func_ctx, opcode))
  1178. return false;
  1179. break;
  1180. }
  1181. case SIMD_i8x16_extract_lane_s:
  1182. {
  1183. if (!aot_compile_simd_extract_i8x16(comp_ctx, func_ctx, *frame_ip++,
  1184. true))
  1185. return false;
  1186. break;
  1187. }
  1188. case SIMD_i8x16_extract_lane_u:
  1189. {
  1190. if (!aot_compile_simd_extract_i8x16(comp_ctx, func_ctx, *frame_ip++,
  1191. false))
  1192. return false;
  1193. break;
  1194. }
  1195. case SIMD_i16x8_extract_lane_s:
  1196. {
  1197. if (!aot_compile_simd_extract_i16x8(comp_ctx, func_ctx, *frame_ip++,
  1198. true))
  1199. return false;
  1200. break;
  1201. }
  1202. case SIMD_i16x8_extract_lane_u:
  1203. {
  1204. if (!aot_compile_simd_extract_i16x8(comp_ctx, func_ctx, *frame_ip++,
  1205. false))
  1206. return false;
  1207. break;
  1208. }
  1209. case SIMD_i32x4_extract_lane:
  1210. {
  1211. if (!aot_compile_simd_extract_i32x4(comp_ctx, func_ctx, *frame_ip++))
  1212. return false;
  1213. break;
  1214. }
  1215. case SIMD_i64x2_extract_lane:
  1216. {
  1217. if (!aot_compile_simd_extract_i64x2(comp_ctx, func_ctx, *frame_ip++))
  1218. return false;
  1219. break;
  1220. }
  1221. case SIMD_f32x4_extract_lane:
  1222. {
  1223. if (!aot_compile_simd_extract_f32x4(comp_ctx, func_ctx, *frame_ip++))
  1224. return false;
  1225. break;
  1226. }
  1227. case SIMD_f64x2_extract_lane:
  1228. {
  1229. if (!aot_compile_simd_extract_f64x2(comp_ctx, func_ctx, *frame_ip++))
  1230. return false;
  1231. break;
  1232. }
  1233. case SIMD_i8x16_replace_lane:
  1234. {
  1235. if (!aot_compile_simd_replace_i8x16(comp_ctx, func_ctx, *frame_ip++))
  1236. return false;
  1237. break;
  1238. }
  1239. case SIMD_i16x8_replace_lane:
  1240. {
  1241. if (!aot_compile_simd_replace_i16x8(comp_ctx, func_ctx, *frame_ip++))
  1242. return false;
  1243. break;
  1244. }
  1245. case SIMD_i32x4_replace_lane:
  1246. {
  1247. if (!aot_compile_simd_replace_i32x4(comp_ctx, func_ctx, *frame_ip++))
  1248. return false;
  1249. break;
  1250. }
  1251. case SIMD_i64x2_replace_lane:
  1252. {
  1253. if (!aot_compile_simd_replace_i64x2(comp_ctx, func_ctx, *frame_ip++))
  1254. return false;
  1255. break;
  1256. }
  1257. case SIMD_f32x4_replace_lane:
  1258. {
  1259. if (!aot_compile_simd_replace_f32x4(comp_ctx, func_ctx, *frame_ip++))
  1260. return false;
  1261. break;
  1262. }
  1263. case SIMD_f64x2_replace_lane:
  1264. {
  1265. if (!aot_compile_simd_replace_f64x2(comp_ctx, func_ctx, *frame_ip++))
  1266. return false;
  1267. break;
  1268. }
  1269. case SIMD_i8x16_eq:
  1270. case SIMD_i8x16_ne:
  1271. case SIMD_i8x16_lt_s:
  1272. case SIMD_i8x16_lt_u:
  1273. case SIMD_i8x16_gt_s:
  1274. case SIMD_i8x16_gt_u:
  1275. case SIMD_i8x16_le_s:
  1276. case SIMD_i8x16_le_u:
  1277. case SIMD_i8x16_ge_s:
  1278. case SIMD_i8x16_ge_u:
  1279. {
  1280. if (!aot_compile_simd_i8x16_compare(comp_ctx, func_ctx,
  1281. INT_EQ + opcode - SIMD_i8x16_eq))
  1282. return false;
  1283. break;
  1284. }
  1285. case SIMD_i16x8_eq:
  1286. case SIMD_i16x8_ne:
  1287. case SIMD_i16x8_lt_s:
  1288. case SIMD_i16x8_lt_u:
  1289. case SIMD_i16x8_gt_s:
  1290. case SIMD_i16x8_gt_u:
  1291. case SIMD_i16x8_le_s:
  1292. case SIMD_i16x8_le_u:
  1293. case SIMD_i16x8_ge_s:
  1294. case SIMD_i16x8_ge_u:
  1295. {
  1296. if (!aot_compile_simd_i16x8_compare(comp_ctx, func_ctx,
  1297. INT_EQ + opcode - SIMD_i16x8_eq))
  1298. return false;
  1299. break;
  1300. }
  1301. case SIMD_i32x4_eq:
  1302. case SIMD_i32x4_ne:
  1303. case SIMD_i32x4_lt_s:
  1304. case SIMD_i32x4_lt_u:
  1305. case SIMD_i32x4_gt_s:
  1306. case SIMD_i32x4_gt_u:
  1307. case SIMD_i32x4_le_s:
  1308. case SIMD_i32x4_le_u:
  1309. case SIMD_i32x4_ge_s:
  1310. case SIMD_i32x4_ge_u:
  1311. {
  1312. if (!aot_compile_simd_i32x4_compare(comp_ctx, func_ctx,
  1313. INT_EQ + opcode - SIMD_i32x4_eq))
  1314. return false;
  1315. break;
  1316. }
  1317. case SIMD_f32x4_eq:
  1318. case SIMD_f32x4_ne:
  1319. case SIMD_f32x4_lt:
  1320. case SIMD_f32x4_gt:
  1321. case SIMD_f32x4_le:
  1322. case SIMD_f32x4_ge:
  1323. {
  1324. if (!aot_compile_simd_f32x4_compare(comp_ctx, func_ctx,
  1325. FLOAT_EQ + opcode - SIMD_f32x4_eq))
  1326. return false;
  1327. break;
  1328. }
  1329. case SIMD_f64x2_eq:
  1330. case SIMD_f64x2_ne:
  1331. case SIMD_f64x2_lt:
  1332. case SIMD_f64x2_gt:
  1333. case SIMD_f64x2_le:
  1334. case SIMD_f64x2_ge:
  1335. {
  1336. if (!aot_compile_simd_f64x2_compare(comp_ctx, func_ctx,
  1337. FLOAT_EQ + opcode - SIMD_f64x2_eq))
  1338. return false;
  1339. break;
  1340. }
  1341. case SIMD_v128_not:
  1342. case SIMD_v128_and:
  1343. case SIMD_v128_andnot:
  1344. case SIMD_v128_or:
  1345. case SIMD_v128_xor:
  1346. case SIMD_v128_bitselect:
  1347. {
  1348. if (!aot_compile_simd_v128_bitwise(comp_ctx, func_ctx,
  1349. V128_NOT + opcode - SIMD_v128_not))
  1350. return false;
  1351. break;
  1352. }
  1353. case SIMD_i8x16_add:
  1354. case SIMD_i8x16_sub:
  1355. {
  1356. V128Arithmetic arith_op = (opcode == SIMD_i8x16_add)
  1357. ? V128_ADD : V128_SUB;
  1358. if (!aot_compile_simd_i8x16_arith(comp_ctx, func_ctx, arith_op))
  1359. return false;
  1360. break;
  1361. }
  1362. case SIMD_i16x8_add:
  1363. case SIMD_i16x8_sub:
  1364. case SIMD_i16x8_mul:
  1365. {
  1366. V128Arithmetic arith_op = V128_ADD;
  1367. if (opcode == SIMD_i16x8_sub)
  1368. arith_op = V128_SUB;
  1369. else if (opcode == SIMD_i16x8_mul)
  1370. arith_op = V128_MUL;
  1371. if (!aot_compile_simd_i16x8_arith(comp_ctx, func_ctx, arith_op))
  1372. return false;
  1373. break;
  1374. }
  1375. case SIMD_i32x4_add:
  1376. case SIMD_i32x4_sub:
  1377. case SIMD_i32x4_mul:
  1378. {
  1379. V128Arithmetic arith_op = V128_ADD;
  1380. if (opcode == SIMD_i32x4_sub)
  1381. arith_op = V128_SUB;
  1382. else if (opcode == SIMD_i32x4_mul)
  1383. arith_op = V128_MUL;
  1384. if (!aot_compile_simd_i32x4_arith(comp_ctx, func_ctx, arith_op))
  1385. return false;
  1386. break;
  1387. }
  1388. case SIMD_i64x2_add:
  1389. case SIMD_i64x2_sub:
  1390. case SIMD_i64x2_mul:
  1391. {
  1392. V128Arithmetic arith_op = V128_ADD;
  1393. if (opcode == SIMD_i64x2_sub)
  1394. arith_op = V128_SUB;
  1395. else if (opcode == SIMD_i64x2_mul)
  1396. arith_op = V128_MUL;
  1397. if (!aot_compile_simd_i64x2_arith(comp_ctx, func_ctx, arith_op))
  1398. return false;
  1399. break;
  1400. }
  1401. case SIMD_i8x16_neg:
  1402. {
  1403. if (!aot_compile_simd_i8x16_neg(comp_ctx, func_ctx))
  1404. return false;
  1405. break;
  1406. }
  1407. case SIMD_i16x8_neg:
  1408. {
  1409. if (!aot_compile_simd_i16x8_neg(comp_ctx, func_ctx))
  1410. return false;
  1411. break;
  1412. }
  1413. case SIMD_i32x4_neg:
  1414. {
  1415. if (!aot_compile_simd_i32x4_neg(comp_ctx, func_ctx))
  1416. return false;
  1417. break;
  1418. }
  1419. case SIMD_i64x2_neg:
  1420. {
  1421. if (!aot_compile_simd_i64x2_neg(comp_ctx, func_ctx))
  1422. return false;
  1423. break;
  1424. }
  1425. case SIMD_i8x16_add_saturate_s:
  1426. case SIMD_i8x16_add_saturate_u:
  1427. {
  1428. if (!aot_compile_simd_i8x16_saturate(comp_ctx, func_ctx, V128_ADD,
  1429. opcode == SIMD_i8x16_add_saturate_s
  1430. ? true : false))
  1431. return false;
  1432. break;
  1433. }
  1434. case SIMD_i8x16_sub_saturate_s:
  1435. case SIMD_i8x16_sub_saturate_u:
  1436. {
  1437. if (!aot_compile_simd_i8x16_saturate(comp_ctx, func_ctx, V128_SUB,
  1438. opcode == SIMD_i8x16_sub_saturate_s
  1439. ? true : false))
  1440. return false;
  1441. break;
  1442. }
  1443. case SIMD_i16x8_add_saturate_s:
  1444. case SIMD_i16x8_add_saturate_u:
  1445. {
  1446. if (!aot_compile_simd_i16x8_saturate(comp_ctx, func_ctx, V128_ADD,
  1447. opcode == SIMD_i16x8_add_saturate_s
  1448. ? true : false))
  1449. return false;
  1450. break;
  1451. }
  1452. case SIMD_i16x8_sub_saturate_s:
  1453. case SIMD_i16x8_sub_saturate_u:
  1454. {
  1455. if (!aot_compile_simd_i16x8_saturate(comp_ctx, func_ctx, V128_SUB,
  1456. opcode == SIMD_i16x8_sub_saturate_s
  1457. ? true : false))
  1458. return false;
  1459. break;
  1460. }
  1461. case SIMD_i8x16_min_s:
  1462. case SIMD_i8x16_min_u:
  1463. {
  1464. if (!aot_compile_simd_i8x16_cmp(comp_ctx, func_ctx, V128_MIN,
  1465. opcode == SIMD_i8x16_min_s
  1466. ? true : false))
  1467. return false;
  1468. break;
  1469. }
  1470. case SIMD_i8x16_max_s:
  1471. case SIMD_i8x16_max_u:
  1472. {
  1473. if (!aot_compile_simd_i8x16_cmp(comp_ctx, func_ctx, V128_MAX,
  1474. opcode == SIMD_i8x16_max_s
  1475. ? true : false))
  1476. return false;
  1477. break;
  1478. }
  1479. case SIMD_i16x8_min_s:
  1480. case SIMD_i16x8_min_u:
  1481. {
  1482. if (!aot_compile_simd_i16x8_cmp(comp_ctx, func_ctx, V128_MIN,
  1483. opcode == SIMD_i16x8_min_s
  1484. ? true : false))
  1485. return false;
  1486. break;
  1487. }
  1488. case SIMD_i16x8_max_s:
  1489. case SIMD_i16x8_max_u:
  1490. {
  1491. if (!aot_compile_simd_i16x8_cmp(comp_ctx, func_ctx, V128_MAX,
  1492. opcode == SIMD_i16x8_max_s
  1493. ? true : false))
  1494. return false;
  1495. break;
  1496. }
  1497. case SIMD_i32x4_min_s:
  1498. case SIMD_i32x4_min_u:
  1499. {
  1500. if (!aot_compile_simd_i32x4_cmp(comp_ctx, func_ctx, V128_MIN,
  1501. opcode == SIMD_i32x4_min_s
  1502. ? true : false))
  1503. return false;
  1504. break;
  1505. }
  1506. case SIMD_i32x4_max_s:
  1507. case SIMD_i32x4_max_u:
  1508. {
  1509. if (!aot_compile_simd_i32x4_cmp(comp_ctx, func_ctx, V128_MAX,
  1510. opcode == SIMD_i32x4_max_s
  1511. ? true : false))
  1512. return false;
  1513. break;
  1514. }
  1515. case SIMD_i8x16_abs:
  1516. {
  1517. if (!aot_compile_simd_i8x16_abs(comp_ctx, func_ctx))
  1518. return false;
  1519. break;
  1520. }
  1521. case SIMD_i16x8_abs:
  1522. {
  1523. if (!aot_compile_simd_i16x8_abs(comp_ctx, func_ctx))
  1524. return false;
  1525. break;
  1526. }
  1527. case SIMD_i32x4_abs:
  1528. {
  1529. if (!aot_compile_simd_i32x4_abs(comp_ctx, func_ctx))
  1530. return false;
  1531. break;
  1532. }
  1533. case SIMD_i8x16_avgr_u:
  1534. {
  1535. if (!aot_compile_simd_i8x16_avgr_u(comp_ctx, func_ctx))
  1536. return false;
  1537. break;
  1538. }
  1539. case SIMD_i16x8_avgr_u:
  1540. {
  1541. if (!aot_compile_simd_i16x8_avgr_u(comp_ctx, func_ctx))
  1542. return false;
  1543. break;
  1544. }
  1545. case SIMD_i8x16_any_true:
  1546. {
  1547. if (!aot_compile_simd_i8x16_any_true(comp_ctx, func_ctx))
  1548. return false;
  1549. break;
  1550. }
  1551. case SIMD_i16x8_any_true:
  1552. {
  1553. if (!aot_compile_simd_i16x8_any_true(comp_ctx, func_ctx))
  1554. return false;
  1555. break;
  1556. }
  1557. case SIMD_i32x4_any_true:
  1558. {
  1559. if (!aot_compile_simd_i32x4_any_true(comp_ctx, func_ctx))
  1560. return false;
  1561. break;
  1562. }
  1563. case SIMD_i8x16_all_true:
  1564. {
  1565. if (!aot_compile_simd_i8x16_all_true(comp_ctx, func_ctx))
  1566. return false;
  1567. break;
  1568. }
  1569. case SIMD_i16x8_all_true:
  1570. {
  1571. if (!aot_compile_simd_i16x8_all_true(comp_ctx, func_ctx))
  1572. return false;
  1573. break;
  1574. }
  1575. case SIMD_i32x4_all_true:
  1576. {
  1577. if (!aot_compile_simd_i32x4_all_true(comp_ctx, func_ctx))
  1578. return false;
  1579. break;
  1580. }
  1581. case SIMD_i8x16_bitmask:
  1582. {
  1583. if (!aot_compile_simd_i8x16_bitmask(comp_ctx, func_ctx))
  1584. return false;
  1585. break;
  1586. }
  1587. case SIMD_i16x8_bitmask:
  1588. {
  1589. if (!aot_compile_simd_i16x8_bitmask(comp_ctx, func_ctx))
  1590. return false;
  1591. break;
  1592. }
  1593. case SIMD_i32x4_bitmask:
  1594. {
  1595. if (!aot_compile_simd_i32x4_bitmask(comp_ctx, func_ctx))
  1596. return false;
  1597. break;
  1598. }
  1599. case SIMD_i8x16_shl:
  1600. case SIMD_i8x16_shr_s:
  1601. case SIMD_i8x16_shr_u:
  1602. {
  1603. if (!aot_compile_simd_i8x16_shift(comp_ctx, func_ctx,
  1604. INT_SHL + opcode - SIMD_i8x16_shl))
  1605. return false;
  1606. break;
  1607. }
  1608. case SIMD_i16x8_shl:
  1609. case SIMD_i16x8_shr_s:
  1610. case SIMD_i16x8_shr_u:
  1611. {
  1612. if (!aot_compile_simd_i16x8_shift(comp_ctx, func_ctx,
  1613. INT_SHL + opcode - SIMD_i16x8_shl))
  1614. return false;
  1615. break;
  1616. }
  1617. case SIMD_i32x4_shl:
  1618. case SIMD_i32x4_shr_s:
  1619. case SIMD_i32x4_shr_u:
  1620. {
  1621. if (!aot_compile_simd_i32x4_shift(comp_ctx, func_ctx,
  1622. INT_SHL + opcode - SIMD_i32x4_shl))
  1623. return false;
  1624. break;
  1625. }
  1626. case SIMD_i64x2_shl:
  1627. case SIMD_i64x2_shr_s:
  1628. case SIMD_i64x2_shr_u:
  1629. {
  1630. if (!aot_compile_simd_i64x2_shift(comp_ctx, func_ctx,
  1631. INT_SHL + opcode - SIMD_i64x2_shl))
  1632. return false;
  1633. break;
  1634. }
  1635. case SIMD_i8x16_narrow_i16x8_s:
  1636. case SIMD_i8x16_narrow_i16x8_u:
  1637. {
  1638. bool is_signed = (opcode == SIMD_i8x16_narrow_i16x8_s)
  1639. ? true : false;
  1640. if (!aot_compile_simd_i8x16_narrow_i16x8(comp_ctx, func_ctx,
  1641. is_signed))
  1642. return false;
  1643. break;
  1644. }
  1645. case SIMD_i16x8_narrow_i32x4_s:
  1646. case SIMD_i16x8_narrow_i32x4_u:
  1647. {
  1648. bool is_signed = (opcode == SIMD_i16x8_narrow_i32x4_s)
  1649. ? true : false;
  1650. if (!aot_compile_simd_i16x8_narrow_i32x4(comp_ctx, func_ctx,
  1651. is_signed))
  1652. return false;
  1653. break;
  1654. }
  1655. case SIMD_i16x8_widen_low_i8x16_s:
  1656. case SIMD_i16x8_widen_high_i8x16_s:
  1657. {
  1658. bool is_low = (opcode == SIMD_i16x8_widen_low_i8x16_s)
  1659. ? true : false;
  1660. if (!aot_compile_simd_i16x8_widen_i8x16(comp_ctx, func_ctx,
  1661. is_low, true))
  1662. return false;
  1663. break;
  1664. }
  1665. case SIMD_i16x8_widen_low_i8x16_u:
  1666. case SIMD_i16x8_widen_high_i8x16_u:
  1667. {
  1668. bool is_low = (opcode == SIMD_i16x8_widen_low_i8x16_u)
  1669. ? true : false;
  1670. if (!aot_compile_simd_i16x8_widen_i8x16(comp_ctx, func_ctx,
  1671. is_low, false))
  1672. return false;
  1673. break;
  1674. }
  1675. case SIMD_i32x4_widen_low_i16x8_s:
  1676. case SIMD_i32x4_widen_high_i16x8_s:
  1677. {
  1678. bool is_low = (opcode == SIMD_i32x4_widen_low_i16x8_s)
  1679. ? true : false;
  1680. if (!aot_compile_simd_i32x4_widen_i16x8(comp_ctx, func_ctx,
  1681. is_low, true))
  1682. return false;
  1683. break;
  1684. }
  1685. case SIMD_i32x4_widen_low_i16x8_u:
  1686. case SIMD_i32x4_widen_high_i16x8_u:
  1687. {
  1688. bool is_low = (opcode == SIMD_i32x4_widen_low_i16x8_u)
  1689. ? true : false;
  1690. if (!aot_compile_simd_i32x4_widen_i16x8(comp_ctx, func_ctx,
  1691. is_low, false))
  1692. return false;
  1693. break;
  1694. }
  1695. case SIMD_i32x4_trunc_sat_f32x4_s:
  1696. case SIMD_i32x4_trunc_sat_f32x4_u:
  1697. {
  1698. bool is_signed = (opcode == SIMD_i32x4_trunc_sat_f32x4_s)
  1699. ? true : false;
  1700. if (!aot_compile_simd_i32x4_trunc_sat_f32x4(comp_ctx, func_ctx,
  1701. is_signed))
  1702. return false;
  1703. break;
  1704. }
  1705. case SIMD_f32x4_convert_i32x4_s:
  1706. case SIMD_f32x4_convert_i32x4_u:
  1707. {
  1708. bool is_signed = (opcode == SIMD_f32x4_convert_i32x4_s)
  1709. ? true : false;
  1710. if (!aot_compile_simd_f32x4_convert_i32x4(comp_ctx, func_ctx,
  1711. is_signed))
  1712. return false;
  1713. break;
  1714. }
  1715. case SIMD_f32x4_add:
  1716. case SIMD_f32x4_sub:
  1717. case SIMD_f32x4_mul:
  1718. case SIMD_f32x4_div:
  1719. case SIMD_f32x4_min:
  1720. case SIMD_f32x4_max:
  1721. {
  1722. if (!aot_compile_simd_f32x4_arith(comp_ctx, func_ctx,
  1723. FLOAT_ADD + opcode - SIMD_f32x4_add))
  1724. return false;
  1725. break;
  1726. }
  1727. case SIMD_f64x2_add:
  1728. case SIMD_f64x2_sub:
  1729. case SIMD_f64x2_mul:
  1730. case SIMD_f64x2_div:
  1731. case SIMD_f64x2_min:
  1732. case SIMD_f64x2_max:
  1733. {
  1734. if (!aot_compile_simd_f64x2_arith(comp_ctx, func_ctx,
  1735. FLOAT_ADD + opcode - SIMD_f64x2_add))
  1736. return false;
  1737. break;
  1738. }
  1739. case SIMD_f32x4_neg:
  1740. {
  1741. if (!aot_compile_simd_f32x4_neg(comp_ctx, func_ctx))
  1742. return false;
  1743. break;
  1744. }
  1745. case SIMD_f64x2_neg:
  1746. {
  1747. if (!aot_compile_simd_f64x2_neg(comp_ctx, func_ctx))
  1748. return false;
  1749. break;
  1750. }
  1751. case SIMD_f32x4_abs:
  1752. {
  1753. if (!aot_compile_simd_f32x4_abs(comp_ctx, func_ctx))
  1754. return false;
  1755. break;
  1756. }
  1757. case SIMD_f64x2_abs:
  1758. {
  1759. if (!aot_compile_simd_f64x2_abs(comp_ctx, func_ctx))
  1760. return false;
  1761. break;
  1762. }
  1763. case SIMD_f32x4_sqrt:
  1764. {
  1765. if (!aot_compile_simd_f32x4_sqrt(comp_ctx, func_ctx))
  1766. return false;
  1767. break;
  1768. }
  1769. case SIMD_f64x2_sqrt:
  1770. {
  1771. if (!aot_compile_simd_f64x2_sqrt(comp_ctx, func_ctx))
  1772. return false;
  1773. break;
  1774. }
  1775. case SIMD_f32x4_ceil:
  1776. {
  1777. if (!aot_compile_simd_f32x4_ceil(comp_ctx, func_ctx))
  1778. return false;
  1779. break;
  1780. }
  1781. case SIMD_f64x2_ceil:
  1782. {
  1783. if (!aot_compile_simd_f64x2_ceil(comp_ctx, func_ctx))
  1784. return false;
  1785. break;
  1786. }
  1787. case SIMD_f32x4_floor:
  1788. {
  1789. if (!aot_compile_simd_f32x4_floor(comp_ctx, func_ctx))
  1790. return false;
  1791. break;
  1792. }
  1793. case SIMD_f64x2_floor:
  1794. {
  1795. if (!aot_compile_simd_f64x2_floor(comp_ctx, func_ctx))
  1796. return false;
  1797. break;
  1798. }
  1799. case SIMD_f32x4_trunc:
  1800. {
  1801. if (!aot_compile_simd_f32x4_trunc(comp_ctx, func_ctx))
  1802. return false;
  1803. break;
  1804. }
  1805. case SIMD_f64x2_trunc:
  1806. {
  1807. if (!aot_compile_simd_f64x2_trunc(comp_ctx, func_ctx))
  1808. return false;
  1809. break;
  1810. }
  1811. case SIMD_f32x4_nearest:
  1812. {
  1813. if (!aot_compile_simd_f32x4_nearest(comp_ctx, func_ctx))
  1814. return false;
  1815. break;
  1816. }
  1817. case SIMD_f64x2_nearest:
  1818. {
  1819. if (!aot_compile_simd_f64x2_nearest(comp_ctx, func_ctx))
  1820. return false;
  1821. break;
  1822. }
  1823. default:
  1824. aot_set_last_error("unsupported opcode");
  1825. return false;
  1826. }
  1827. break;
  1828. }
  1829. #endif /* end of WASM_ENABLE_SIMD */
  1830. default:
  1831. aot_set_last_error("unsupported opcode");
  1832. return false;
  1833. }
  1834. }
  1835. /* Move func_return block to the bottom */
  1836. if (func_ctx->func_return_block) {
  1837. LLVMBasicBlockRef last_block =
  1838. LLVMGetLastBasicBlock(func_ctx->func);
  1839. if (last_block != func_ctx->func_return_block)
  1840. LLVMMoveBasicBlockAfter(func_ctx->func_return_block,
  1841. last_block);
  1842. }
  1843. /* Move got_exception block to the bottom */
  1844. if (func_ctx->got_exception_block) {
  1845. LLVMBasicBlockRef last_block =
  1846. LLVMGetLastBasicBlock(func_ctx->func);
  1847. if (last_block != func_ctx->got_exception_block)
  1848. LLVMMoveBasicBlockAfter(func_ctx->got_exception_block,
  1849. last_block);
  1850. }
  1851. return true;
  1852. #if WASM_ENABLE_SIMD != 0
  1853. unsupport_simd:
  1854. aot_set_last_error("SIMD instruction was found, "
  1855. "try adding --enable-simd option?");
  1856. return false;
  1857. #endif
  1858. #if WASM_ENABLE_REF_TYPES != 0
  1859. unsupport_ref_types:
  1860. aot_set_last_error("reference type instruction was found, "
  1861. "try adding --enable-ref-types option?");
  1862. return false;
  1863. #endif
  1864. fail:
  1865. return false;
  1866. }
  1867. bool
  1868. aot_compile_wasm(AOTCompContext *comp_ctx)
  1869. {
  1870. char *msg = NULL;
  1871. bool ret;
  1872. uint32 i;
  1873. bh_print_time("Begin to compile WASM bytecode to LLVM IR");
  1874. for (i = 0; i < comp_ctx->func_ctx_count; i++)
  1875. if (!aot_compile_func(comp_ctx, i)) {
  1876. #if 0
  1877. LLVMDumpModule(comp_ctx->module);
  1878. char *err;
  1879. LLVMTargetMachineEmitToFile(comp_ctx->target_machine, comp_ctx->module,
  1880. "./test.o", LLVMObjectFile, &err);
  1881. #endif
  1882. return false;
  1883. }
  1884. #if 0
  1885. LLVMDumpModule(comp_ctx->module);
  1886. /* Clear error no, LLVMDumpModule may set errno */
  1887. errno = 0;
  1888. #endif
  1889. bh_print_time("Begin to verify LLVM module");
  1890. ret = LLVMVerifyModule(comp_ctx->module, LLVMPrintMessageAction, &msg);
  1891. if (!ret && msg) {
  1892. if (msg[0] != '\0') {
  1893. aot_set_last_error(msg);
  1894. LLVMDisposeMessage(msg);
  1895. return false;
  1896. }
  1897. LLVMDisposeMessage(msg);
  1898. }
  1899. bh_print_time("Begin to run function optimization passes");
  1900. if (comp_ctx->optimize) {
  1901. LLVMInitializeFunctionPassManager(comp_ctx->pass_mgr);
  1902. for (i = 0; i < comp_ctx->func_ctx_count; i++)
  1903. LLVMRunFunctionPassManager(comp_ctx->pass_mgr,
  1904. comp_ctx->func_ctxes[i]->func);
  1905. }
  1906. return true;
  1907. }
  1908. bool
  1909. aot_emit_llvm_file(AOTCompContext *comp_ctx, const char *file_name)
  1910. {
  1911. char *err = NULL;
  1912. bh_print_time("Begin to emit LLVM IR file");
  1913. if (LLVMPrintModuleToFile(comp_ctx->module, file_name, &err) != 0) {
  1914. if (err) {
  1915. LLVMDisposeMessage(err);
  1916. err = NULL;
  1917. }
  1918. aot_set_last_error("emit llvm ir to file failed.");
  1919. return false;
  1920. }
  1921. return true;
  1922. }
  1923. bool
  1924. aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name)
  1925. {
  1926. char *err = NULL;
  1927. bh_print_time("Begin to emit object file");
  1928. if (LLVMTargetMachineEmitToFile(comp_ctx->target_machine,
  1929. comp_ctx->module,
  1930. file_name,
  1931. LLVMObjectFile,
  1932. &err) != 0) {
  1933. if (err) {
  1934. LLVMDisposeMessage(err);
  1935. err = NULL;
  1936. }
  1937. aot_set_last_error("emit elf to memory buffer failed.");
  1938. return false;
  1939. }
  1940. return true;
  1941. }
  1942. #if WASM_ENABLE_REF_TYPES != 0
  1943. extern void
  1944. wasm_set_ref_types_flag(bool enable);
  1945. #endif
  1946. typedef struct AOTFileMap {
  1947. uint8 *wasm_file_buf;
  1948. uint32 wasm_file_size;
  1949. uint8 *aot_file_buf;
  1950. uint32 aot_file_size;
  1951. struct AOTFileMap *next;
  1952. } AOTFileMap;
  1953. static bool aot_compile_wasm_file_inited = false;
  1954. static AOTFileMap *aot_file_maps = NULL;
  1955. static korp_mutex aot_file_map_lock;
  1956. bool
  1957. aot_compile_wasm_file_init()
  1958. {
  1959. if (aot_compile_wasm_file_inited) {
  1960. return true;
  1961. }
  1962. if (BHT_OK != os_mutex_init(&aot_file_map_lock)) {
  1963. return false;
  1964. }
  1965. aot_file_maps = NULL;
  1966. aot_compile_wasm_file_inited = true;
  1967. return true;
  1968. }
  1969. void
  1970. aot_compile_wasm_file_destroy()
  1971. {
  1972. AOTFileMap *file_map = aot_file_maps, *file_map_next;
  1973. if (!aot_compile_wasm_file_inited) {
  1974. return;
  1975. }
  1976. while (file_map) {
  1977. file_map_next = file_map->next;
  1978. wasm_runtime_free(file_map->wasm_file_buf);
  1979. wasm_runtime_free(file_map->aot_file_buf);
  1980. wasm_runtime_free(file_map);
  1981. file_map = file_map_next;
  1982. }
  1983. aot_file_maps = NULL;
  1984. os_mutex_destroy(&aot_file_map_lock);
  1985. aot_compile_wasm_file_inited = false;
  1986. }
  1987. static void
  1988. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  1989. {
  1990. if (error_buf != NULL) {
  1991. snprintf(error_buf, error_buf_size,
  1992. "WASM module load failed: %s", string);
  1993. }
  1994. }
  1995. uint8*
  1996. aot_compile_wasm_file(const uint8 *wasm_file_buf, uint32 wasm_file_size,
  1997. uint32 opt_level, uint32 size_level,
  1998. char *error_buf, uint32 error_buf_size,
  1999. uint32 *p_aot_file_size)
  2000. {
  2001. WASMModule *wasm_module = NULL;
  2002. AOTCompData *comp_data = NULL;
  2003. AOTCompContext *comp_ctx = NULL;
  2004. RuntimeInitArgs init_args;
  2005. AOTCompOption option = { 0 };
  2006. AOTFileMap *file_map = NULL, *file_map_next;
  2007. uint8 *wasm_file_buf_cloned = NULL;
  2008. uint8 *aot_file_buf = NULL;
  2009. uint32 aot_file_size;
  2010. option.is_jit_mode = false;
  2011. option.opt_level = opt_level;
  2012. option.size_level = size_level;
  2013. option.output_format = AOT_FORMAT_FILE;
  2014. /* default value, enable or disable depends on the platform */
  2015. option.bounds_checks = 2;
  2016. option.enable_aux_stack_check = true;
  2017. #if WASM_ENABLE_BULK_MEMORY != 0
  2018. option.enable_bulk_memory = true;
  2019. #endif
  2020. #if WASM_ENABLE_THREAD_MGR != 0
  2021. option.enable_thread_mgr = true;
  2022. #endif
  2023. #if WASM_ENABLE_TAIL_CALL != 0
  2024. option.enable_tail_call = true;
  2025. #endif
  2026. #if WASM_ENABLE_SIMD != 0
  2027. option.enable_simd = true;
  2028. #endif
  2029. #if WASM_ENABLE_REF_TYPES != 0
  2030. option.enable_ref_types = true;
  2031. #endif
  2032. #if (WASM_ENABLE_PERF_PROFILING != 0) || (WASM_ENABLE_DUMP_CALL_STACK != 0)
  2033. option.enable_aux_stack_frame = true;
  2034. #endif
  2035. #if WASM_ENABLE_REF_TYPES != 0
  2036. wasm_set_ref_types_flag(option.enable_ref_types);
  2037. #endif
  2038. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  2039. init_args.mem_alloc_type = Alloc_With_Allocator;
  2040. init_args.mem_alloc_option.allocator.malloc_func = malloc;
  2041. init_args.mem_alloc_option.allocator.realloc_func = realloc;
  2042. init_args.mem_alloc_option.allocator.free_func = free;
  2043. os_mutex_lock(&aot_file_map_lock);
  2044. /* lookup the file maps */
  2045. file_map = aot_file_maps;
  2046. while (file_map) {
  2047. file_map_next = file_map->next;
  2048. if (wasm_file_size == file_map->wasm_file_size
  2049. && memcmp(wasm_file_buf, file_map->wasm_file_buf,
  2050. wasm_file_size) == 0) {
  2051. os_mutex_unlock(&aot_file_map_lock);
  2052. /* found */
  2053. *p_aot_file_size = file_map->aot_file_size;
  2054. return file_map->aot_file_buf;
  2055. }
  2056. file_map = file_map_next;
  2057. }
  2058. /* not found, initialize file map and clone wasm file */
  2059. if (!(file_map = wasm_runtime_malloc(sizeof(AOTFileMap)))
  2060. || !(wasm_file_buf_cloned = wasm_runtime_malloc(wasm_file_size))) {
  2061. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  2062. goto fail1;
  2063. }
  2064. bh_memcpy_s(wasm_file_buf_cloned, wasm_file_size,
  2065. wasm_file_buf, wasm_file_size);
  2066. memset(file_map, 0, sizeof(AOTFileMap));
  2067. file_map->wasm_file_buf = wasm_file_buf_cloned;
  2068. file_map->wasm_file_size = wasm_file_size;
  2069. /* load WASM module */
  2070. if (!(wasm_module = wasm_load(wasm_file_buf, wasm_file_size,
  2071. error_buf, sizeof(error_buf)))) {
  2072. goto fail1;
  2073. }
  2074. if (!(comp_data = aot_create_comp_data(wasm_module))) {
  2075. set_error_buf(error_buf, error_buf_size, aot_get_last_error());
  2076. goto fail2;
  2077. }
  2078. if (!(comp_ctx = aot_create_comp_context(comp_data, &option))) {
  2079. set_error_buf(error_buf, error_buf_size, aot_get_last_error());
  2080. goto fail3;
  2081. }
  2082. if (!aot_compile_wasm(comp_ctx)) {
  2083. set_error_buf(error_buf, error_buf_size, aot_get_last_error());
  2084. goto fail4;
  2085. }
  2086. if (!(aot_file_buf = aot_emit_aot_file_buf(comp_ctx, comp_data,
  2087. &aot_file_size))) {
  2088. set_error_buf(error_buf, error_buf_size, aot_get_last_error());
  2089. goto fail4;
  2090. }
  2091. file_map->aot_file_buf = aot_file_buf;
  2092. file_map->aot_file_size = aot_file_size;
  2093. if (!aot_file_maps)
  2094. aot_file_maps = file_map;
  2095. else {
  2096. file_map->next = aot_file_maps;
  2097. aot_file_maps = file_map;
  2098. }
  2099. *p_aot_file_size = aot_file_size;
  2100. fail4:
  2101. /* Destroy compiler context */
  2102. aot_destroy_comp_context(comp_ctx);
  2103. fail3:
  2104. /* Destroy compile data */
  2105. aot_destroy_comp_data(comp_data);
  2106. fail2:
  2107. wasm_unload(wasm_module);
  2108. fail1:
  2109. if (!aot_file_buf) {
  2110. if (wasm_file_buf_cloned)
  2111. wasm_runtime_free(wasm_file_buf_cloned);
  2112. if (file_map)
  2113. wasm_runtime_free(file_map);
  2114. }
  2115. os_mutex_unlock(&aot_file_map_lock);
  2116. return aot_file_buf;
  2117. }