jit_frontend.c 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588
  1. /*
  2. * Copyright (C) 2021 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "jit_compiler.h"
  6. #include "jit_frontend.h"
  7. #include "fe/jit_emit_compare.h"
  8. #include "fe/jit_emit_const.h"
  9. #include "fe/jit_emit_control.h"
  10. #include "fe/jit_emit_conversion.h"
  11. #include "fe/jit_emit_exception.h"
  12. #include "fe/jit_emit_function.h"
  13. #include "fe/jit_emit_memory.h"
  14. #include "fe/jit_emit_numberic.h"
  15. #include "fe/jit_emit_parametric.h"
  16. #include "fe/jit_emit_table.h"
  17. #include "fe/jit_emit_variable.h"
  18. #include "../interpreter/wasm_interp.h"
  19. #include "../interpreter/wasm_opcode.h"
  20. #include "../interpreter/wasm_runtime.h"
  21. #include "../common/wasm_exec_env.h"
  22. static uint32
  23. get_global_base_offset(const WASMModule *module)
  24. {
  25. uint32 module_inst_struct_size =
  26. (uint32)offsetof(WASMModuleInstance, global_table_data.bytes);
  27. uint32 mem_inst_size =
  28. (uint32)sizeof(WASMMemoryInstance)
  29. * (module->import_memory_count + module->memory_count);
  30. #if WASM_ENABLE_JIT != 0
  31. /* If the module dosen't have memory, reserve one mem_info space
  32. with empty content to align with llvm jit compiler */
  33. if (mem_inst_size == 0)
  34. mem_inst_size = (uint32)sizeof(WASMMemoryInstance);
  35. #endif
  36. /* Size of module inst and memory instances */
  37. return module_inst_struct_size + mem_inst_size;
  38. }
  39. static uint32
  40. get_first_table_inst_offset(const WASMModule *module)
  41. {
  42. return get_global_base_offset(module) + module->global_data_size;
  43. }
  44. uint32
  45. jit_frontend_get_global_data_offset(const WASMModule *module, uint32 global_idx)
  46. {
  47. uint32 global_base_offset = get_global_base_offset(module);
  48. if (global_idx < module->import_global_count) {
  49. const WASMGlobalImport *import_global =
  50. &((module->import_globals + global_idx)->u.global);
  51. return global_base_offset + import_global->data_offset;
  52. }
  53. else {
  54. const WASMGlobal *global =
  55. module->globals + (global_idx - module->import_global_count);
  56. return global_base_offset + global->data_offset;
  57. }
  58. }
  59. uint32
  60. jit_frontend_get_table_inst_offset(const WASMModule *module, uint32 tbl_idx)
  61. {
  62. uint32 offset, i = 0;
  63. offset = get_first_table_inst_offset(module);
  64. while (i < tbl_idx && i < module->import_table_count) {
  65. WASMTableImport *import_table = &module->import_tables[i].u.table;
  66. offset += (uint32)offsetof(WASMTableInstance, elems);
  67. #if WASM_ENABLE_MULTI_MODULE != 0
  68. offset += (uint32)sizeof(uint32) * import_table->max_size;
  69. #else
  70. offset += (uint32)sizeof(uint32)
  71. * (import_table->possible_grow ? import_table->max_size
  72. : import_table->init_size);
  73. #endif
  74. i++;
  75. }
  76. if (i == tbl_idx) {
  77. return offset;
  78. }
  79. tbl_idx -= module->import_table_count;
  80. i -= module->import_table_count;
  81. while (i < tbl_idx && i < module->table_count) {
  82. WASMTable *table = module->tables + i;
  83. offset += (uint32)offsetof(WASMTableInstance, elems);
  84. #if WASM_ENABLE_MULTI_MODULE != 0
  85. offset += (uint32)sizeof(uint32) * table->max_size;
  86. #else
  87. offset += (uint32)sizeof(uint32)
  88. * (table->possible_grow ? table->max_size : table->init_size);
  89. #endif
  90. i++;
  91. }
  92. return offset;
  93. }
  94. uint32
  95. jit_frontend_get_module_inst_extra_offset(const WASMModule *module)
  96. {
  97. uint32 offset = jit_frontend_get_table_inst_offset(
  98. module, module->import_table_count + module->table_count);
  99. return align_uint(offset, 8);
  100. }
  101. JitReg
  102. get_module_inst_reg(JitFrame *frame)
  103. {
  104. JitCompContext *cc = frame->cc;
  105. if (!frame->module_inst_reg) {
  106. frame->module_inst_reg = cc->module_inst_reg;
  107. GEN_INSN(LDPTR, frame->module_inst_reg, cc->exec_env_reg,
  108. NEW_CONST(I32, offsetof(WASMExecEnv, module_inst)));
  109. }
  110. return frame->module_inst_reg;
  111. }
  112. JitReg
  113. get_module_reg(JitFrame *frame)
  114. {
  115. JitCompContext *cc = frame->cc;
  116. JitReg module_inst_reg = get_module_inst_reg(frame);
  117. if (!frame->module_reg) {
  118. frame->module_reg = cc->module_reg;
  119. GEN_INSN(LDPTR, frame->module_reg, module_inst_reg,
  120. NEW_CONST(I32, offsetof(WASMModuleInstance, module)));
  121. }
  122. return frame->module_reg;
  123. }
  124. JitReg
  125. get_import_func_ptrs_reg(JitFrame *frame)
  126. {
  127. JitCompContext *cc = frame->cc;
  128. JitReg module_inst_reg = get_module_inst_reg(frame);
  129. if (!frame->import_func_ptrs_reg) {
  130. frame->import_func_ptrs_reg = cc->import_func_ptrs_reg;
  131. GEN_INSN(
  132. LDPTR, frame->import_func_ptrs_reg, module_inst_reg,
  133. NEW_CONST(I32, offsetof(WASMModuleInstance, import_func_ptrs)));
  134. }
  135. return frame->import_func_ptrs_reg;
  136. }
  137. JitReg
  138. get_fast_jit_func_ptrs_reg(JitFrame *frame)
  139. {
  140. JitCompContext *cc = frame->cc;
  141. JitReg module_inst_reg = get_module_inst_reg(frame);
  142. if (!frame->fast_jit_func_ptrs_reg) {
  143. frame->fast_jit_func_ptrs_reg = cc->fast_jit_func_ptrs_reg;
  144. GEN_INSN(
  145. LDPTR, frame->fast_jit_func_ptrs_reg, module_inst_reg,
  146. NEW_CONST(I32, offsetof(WASMModuleInstance, fast_jit_func_ptrs)));
  147. }
  148. return frame->fast_jit_func_ptrs_reg;
  149. }
  150. JitReg
  151. get_func_type_indexes_reg(JitFrame *frame)
  152. {
  153. JitCompContext *cc = frame->cc;
  154. JitReg module_inst_reg = get_module_inst_reg(frame);
  155. if (!frame->func_type_indexes_reg) {
  156. frame->func_type_indexes_reg = cc->func_type_indexes_reg;
  157. GEN_INSN(
  158. LDPTR, frame->func_type_indexes_reg, module_inst_reg,
  159. NEW_CONST(I32, offsetof(WASMModuleInstance, func_type_indexes)));
  160. }
  161. return frame->func_type_indexes_reg;
  162. }
  163. JitReg
  164. get_aux_stack_bound_reg(JitFrame *frame)
  165. {
  166. JitCompContext *cc = frame->cc;
  167. if (!frame->aux_stack_bound_reg) {
  168. frame->aux_stack_bound_reg = cc->aux_stack_bound_reg;
  169. GEN_INSN(
  170. LDI32, frame->aux_stack_bound_reg, cc->exec_env_reg,
  171. NEW_CONST(I32, offsetof(WASMExecEnv, aux_stack_boundary.boundary)));
  172. }
  173. return frame->aux_stack_bound_reg;
  174. }
  175. JitReg
  176. get_aux_stack_bottom_reg(JitFrame *frame)
  177. {
  178. JitCompContext *cc = frame->cc;
  179. if (!frame->aux_stack_bottom_reg) {
  180. frame->aux_stack_bottom_reg = cc->aux_stack_bottom_reg;
  181. GEN_INSN(
  182. LDI32, frame->aux_stack_bottom_reg, cc->exec_env_reg,
  183. NEW_CONST(I32, offsetof(WASMExecEnv, aux_stack_bottom.bottom)));
  184. }
  185. return frame->aux_stack_bottom_reg;
  186. }
  187. #if WASM_ENABLE_SHARED_MEMORY != 0
  188. static bool
  189. is_shared_memory(WASMModule *module, uint32 mem_idx)
  190. {
  191. WASMMemory *memory;
  192. WASMMemoryImport *memory_import;
  193. bool is_shared;
  194. if (mem_idx < module->import_memory_count) {
  195. memory_import = &(module->import_memories[mem_idx].u.memory);
  196. is_shared = memory_import->flags & 0x02 ? true : false;
  197. }
  198. else {
  199. memory = &module->memories[mem_idx - module->import_memory_count];
  200. is_shared = memory->flags & 0x02 ? true : false;
  201. }
  202. return is_shared;
  203. }
  204. #endif
  205. JitReg
  206. get_memory_inst_reg(JitFrame *frame, uint32 mem_idx)
  207. {
  208. JitCompContext *cc = frame->cc;
  209. JitReg module_inst_reg = get_module_inst_reg(frame);
  210. uint32 memory_inst_offset;
  211. #if WASM_ENABLE_SHARED_MEMORY != 0
  212. JitReg memories_addr;
  213. uint32 memories_offset;
  214. bool is_shared;
  215. #endif
  216. if (frame->memory_regs[mem_idx].memory_inst)
  217. return frame->memory_regs[mem_idx].memory_inst;
  218. frame->memory_regs[mem_idx].memory_inst =
  219. cc->memory_regs[mem_idx].memory_inst;
  220. bh_assert(mem_idx == 0);
  221. #if WASM_ENABLE_SHARED_MEMORY != 0
  222. is_shared = is_shared_memory(cc->cur_wasm_module, mem_idx);
  223. if (is_shared) {
  224. memories_addr = jit_cc_new_reg_ptr(cc);
  225. memories_offset = (uint32)offsetof(WASMModuleInstance, memories);
  226. /* module_inst->memories */
  227. GEN_INSN(LDPTR, memories_addr, module_inst_reg,
  228. NEW_CONST(I32, memories_offset));
  229. /* module_inst->memories[mem_idx], mem_idx can only be 0 now */
  230. GEN_INSN(LDPTR, frame->memory_regs[mem_idx].memory_inst, memories_addr,
  231. NEW_CONST(I32, mem_idx));
  232. }
  233. else
  234. #endif
  235. {
  236. memory_inst_offset =
  237. (uint32)offsetof(WASMModuleInstance, global_table_data.bytes);
  238. GEN_INSN(LDPTR, frame->memory_regs[mem_idx].memory_inst,
  239. module_inst_reg, NEW_CONST(I32, memory_inst_offset));
  240. }
  241. return frame->memory_regs[mem_idx].memory_inst;
  242. }
  243. JitReg
  244. get_cur_page_count_reg(JitFrame *frame, uint32 mem_idx)
  245. {
  246. JitCompContext *cc = frame->cc;
  247. JitReg module_inst_reg;
  248. uint32 cur_page_count_offset;
  249. #if WASM_ENABLE_SHARED_MEMORY != 0
  250. JitReg memory_inst_reg;
  251. bool is_shared;
  252. #endif
  253. if (frame->memory_regs[mem_idx].cur_page_count)
  254. return frame->memory_regs[mem_idx].cur_page_count;
  255. frame->memory_regs[mem_idx].cur_page_count =
  256. cc->memory_regs[mem_idx].cur_page_count;
  257. /* Get current page count */
  258. bh_assert(mem_idx == 0);
  259. #if WASM_ENABLE_SHARED_MEMORY != 0
  260. is_shared = is_shared_memory(cc->cur_wasm_module, mem_idx);
  261. if (is_shared) {
  262. memory_inst_reg = get_memory_inst_reg(frame, mem_idx);
  263. cur_page_count_offset =
  264. (uint32)offsetof(WASMMemoryInstance, cur_page_count);
  265. /* memories[mem_idx]->cur_page_count_offset */
  266. GEN_INSN(LDI32, frame->memory_regs[mem_idx].cur_page_count,
  267. memory_inst_reg, NEW_CONST(I32, cur_page_count_offset));
  268. }
  269. else
  270. #endif
  271. {
  272. module_inst_reg = get_module_inst_reg(frame);
  273. cur_page_count_offset =
  274. (uint32)offsetof(WASMModuleInstance, global_table_data.bytes)
  275. + (uint32)offsetof(WASMMemoryInstance, cur_page_count);
  276. GEN_INSN(LDI32, frame->memory_regs[mem_idx].cur_page_count,
  277. module_inst_reg, NEW_CONST(I32, cur_page_count_offset));
  278. }
  279. return frame->memory_regs[mem_idx].cur_page_count;
  280. }
  281. JitReg
  282. get_memory_data_reg(JitFrame *frame, uint32 mem_idx)
  283. {
  284. JitCompContext *cc = frame->cc;
  285. JitReg module_inst_reg;
  286. uint32 memory_data_offset;
  287. #if WASM_ENABLE_SHARED_MEMORY != 0
  288. JitReg memory_inst_reg;
  289. bool is_shared;
  290. #endif
  291. if (frame->memory_regs[mem_idx].memory_data)
  292. return frame->memory_regs[mem_idx].memory_data;
  293. frame->memory_regs[mem_idx].memory_data =
  294. cc->memory_regs[mem_idx].memory_data;
  295. bh_assert(mem_idx == 0);
  296. #if WASM_ENABLE_SHARED_MEMORY != 0
  297. is_shared = is_shared_memory(cc->cur_wasm_module, mem_idx);
  298. if (is_shared) {
  299. memory_inst_reg = get_memory_inst_reg(frame, mem_idx);
  300. memory_data_offset = (uint32)offsetof(WASMMemoryInstance, memory_data);
  301. /* memories[mem_idx]->memory_data */
  302. GEN_INSN(LDPTR, frame->memory_regs[mem_idx].memory_data,
  303. memory_inst_reg, NEW_CONST(I32, memory_data_offset));
  304. }
  305. else
  306. #endif
  307. {
  308. module_inst_reg = get_module_inst_reg(frame);
  309. memory_data_offset =
  310. (uint32)offsetof(WASMModuleInstance, global_table_data.bytes)
  311. + (uint32)offsetof(WASMMemoryInstance, memory_data);
  312. GEN_INSN(LDPTR, frame->memory_regs[mem_idx].memory_data,
  313. module_inst_reg, NEW_CONST(I32, memory_data_offset));
  314. }
  315. return frame->memory_regs[mem_idx].memory_data;
  316. }
  317. JitReg
  318. get_memory_data_end_reg(JitFrame *frame, uint32 mem_idx)
  319. {
  320. JitCompContext *cc = frame->cc;
  321. JitReg module_inst_reg;
  322. uint32 memory_data_end_offset;
  323. #if WASM_ENABLE_SHARED_MEMORY != 0
  324. JitReg memory_inst_reg;
  325. bool is_shared;
  326. #endif
  327. if (frame->memory_regs[mem_idx].memory_data_end)
  328. return frame->memory_regs[mem_idx].memory_data_end;
  329. frame->memory_regs[mem_idx].memory_data_end =
  330. cc->memory_regs[mem_idx].memory_data_end;
  331. bh_assert(mem_idx == 0);
  332. #if WASM_ENABLE_SHARED_MEMORY != 0
  333. is_shared = is_shared_memory(cc->cur_wasm_module, mem_idx);
  334. if (is_shared) {
  335. memory_inst_reg = get_memory_inst_reg(frame, mem_idx);
  336. memory_data_end_offset =
  337. (uint32)offsetof(WASMMemoryInstance, memory_data_end);
  338. /* memories[mem_idx]->memory_data_end */
  339. GEN_INSN(LDPTR, frame->memory_regs[mem_idx].memory_data_end,
  340. memory_inst_reg, NEW_CONST(I32, memory_data_end_offset));
  341. }
  342. else
  343. #endif
  344. {
  345. module_inst_reg = get_module_inst_reg(frame);
  346. memory_data_end_offset =
  347. (uint32)offsetof(WASMModuleInstance, global_table_data.bytes)
  348. + (uint32)offsetof(WASMMemoryInstance, memory_data_end);
  349. GEN_INSN(LDPTR, frame->memory_regs[mem_idx].memory_data_end,
  350. module_inst_reg, NEW_CONST(I32, memory_data_end_offset));
  351. }
  352. return frame->memory_regs[mem_idx].memory_data_end;
  353. }
  354. JitReg
  355. get_mem_bound_check_1byte_reg(JitFrame *frame, uint32 mem_idx)
  356. {
  357. JitCompContext *cc = frame->cc;
  358. JitReg module_inst_reg;
  359. uint32 mem_bound_check_1byte_offset;
  360. #if WASM_ENABLE_SHARED_MEMORY != 0
  361. JitReg memory_inst_reg;
  362. bool is_shared;
  363. #endif
  364. if (frame->memory_regs[mem_idx].mem_bound_check_1byte)
  365. return frame->memory_regs[mem_idx].mem_bound_check_1byte;
  366. frame->memory_regs[mem_idx].mem_bound_check_1byte =
  367. cc->memory_regs[mem_idx].mem_bound_check_1byte;
  368. bh_assert(mem_idx == 0);
  369. #if WASM_ENABLE_SHARED_MEMORY != 0
  370. is_shared = is_shared_memory(cc->cur_wasm_module, mem_idx);
  371. if (is_shared) {
  372. memory_inst_reg = get_memory_inst_reg(frame, mem_idx);
  373. mem_bound_check_1byte_offset =
  374. (uint32)offsetof(WASMMemoryInstance, mem_bound_check_1byte);
  375. /* memories[mem_idx]->mem_bound_check_1byte */
  376. #if UINTPTR_MAX == UINT64_MAX
  377. GEN_INSN(LDI64, frame->memory_regs[mem_idx].mem_bound_check_1byte,
  378. memory_inst_reg, NEW_CONST(I32, mem_bound_check_1byte_offset));
  379. #else
  380. GEN_INSN(LDI32, frame->memory_regs[mem_idx].mem_bound_check_1byte,
  381. memory_inst_reg, NEW_CONST(I32, mem_bound_check_1byte_offset));
  382. #endif
  383. }
  384. else
  385. #endif
  386. {
  387. module_inst_reg = get_module_inst_reg(frame);
  388. mem_bound_check_1byte_offset =
  389. (uint32)offsetof(WASMModuleInstance, global_table_data.bytes)
  390. + (uint32)offsetof(WASMMemoryInstance, mem_bound_check_1byte);
  391. #if UINTPTR_MAX == UINT64_MAX
  392. GEN_INSN(LDI64, frame->memory_regs[mem_idx].mem_bound_check_1byte,
  393. module_inst_reg, NEW_CONST(I32, mem_bound_check_1byte_offset));
  394. #else
  395. GEN_INSN(LDI32, frame->memory_regs[mem_idx].mem_bound_check_1byte,
  396. module_inst_reg, NEW_CONST(I32, mem_bound_check_1byte_offset));
  397. #endif
  398. }
  399. return frame->memory_regs[mem_idx].mem_bound_check_1byte;
  400. }
  401. JitReg
  402. get_mem_bound_check_2bytes_reg(JitFrame *frame, uint32 mem_idx)
  403. {
  404. JitCompContext *cc = frame->cc;
  405. JitReg module_inst_reg;
  406. uint32 mem_bound_check_2bytes_offset;
  407. #if WASM_ENABLE_SHARED_MEMORY != 0
  408. JitReg memory_inst_reg;
  409. bool is_shared;
  410. #endif
  411. if (frame->memory_regs[mem_idx].mem_bound_check_2bytes)
  412. return frame->memory_regs[mem_idx].mem_bound_check_2bytes;
  413. frame->memory_regs[mem_idx].mem_bound_check_2bytes =
  414. cc->memory_regs[mem_idx].mem_bound_check_2bytes;
  415. bh_assert(mem_idx == 0);
  416. #if WASM_ENABLE_SHARED_MEMORY != 0
  417. is_shared = is_shared_memory(cc->cur_wasm_module, mem_idx);
  418. if (is_shared) {
  419. memory_inst_reg = get_memory_inst_reg(frame, mem_idx);
  420. mem_bound_check_2bytes_offset =
  421. (uint32)offsetof(WASMMemoryInstance, mem_bound_check_2bytes);
  422. /* memories[mem_idx]->mem_bound_check_2bytes */
  423. #if UINTPTR_MAX == UINT64_MAX
  424. GEN_INSN(LDI64, frame->memory_regs[mem_idx].mem_bound_check_2bytes,
  425. memory_inst_reg,
  426. NEW_CONST(I32, mem_bound_check_2bytes_offset));
  427. #else
  428. GEN_INSN(LDI32, frame->memory_regs[mem_idx].mem_bound_check_2bytes,
  429. memory_inst_reg,
  430. NEW_CONST(I32, mem_bound_check_2bytes_offset));
  431. #endif
  432. }
  433. else
  434. #endif
  435. {
  436. module_inst_reg = get_module_inst_reg(frame);
  437. mem_bound_check_2bytes_offset =
  438. (uint32)offsetof(WASMModuleInstance, global_table_data.bytes)
  439. + (uint32)offsetof(WASMMemoryInstance, mem_bound_check_2bytes);
  440. #if UINTPTR_MAX == UINT64_MAX
  441. GEN_INSN(LDI64, frame->memory_regs[mem_idx].mem_bound_check_2bytes,
  442. module_inst_reg,
  443. NEW_CONST(I32, mem_bound_check_2bytes_offset));
  444. #else
  445. GEN_INSN(LDI32, frame->memory_regs[mem_idx].mem_bound_check_2bytes,
  446. module_inst_reg,
  447. NEW_CONST(I32, mem_bound_check_2bytes_offset));
  448. #endif
  449. }
  450. return frame->memory_regs[mem_idx].mem_bound_check_2bytes;
  451. }
  452. JitReg
  453. get_mem_bound_check_4bytes_reg(JitFrame *frame, uint32 mem_idx)
  454. {
  455. JitCompContext *cc = frame->cc;
  456. JitReg module_inst_reg;
  457. uint32 mem_bound_check_4bytes_offset;
  458. #if WASM_ENABLE_SHARED_MEMORY != 0
  459. JitReg memory_inst_reg;
  460. bool is_shared;
  461. #endif
  462. if (frame->memory_regs[mem_idx].mem_bound_check_4bytes)
  463. return frame->memory_regs[mem_idx].mem_bound_check_4bytes;
  464. frame->memory_regs[mem_idx].mem_bound_check_4bytes =
  465. cc->memory_regs[mem_idx].mem_bound_check_4bytes;
  466. bh_assert(mem_idx == 0);
  467. #if WASM_ENABLE_SHARED_MEMORY != 0
  468. is_shared = is_shared_memory(cc->cur_wasm_module, mem_idx);
  469. if (is_shared) {
  470. memory_inst_reg = get_memory_inst_reg(frame, mem_idx);
  471. mem_bound_check_4bytes_offset =
  472. (uint32)offsetof(WASMMemoryInstance, mem_bound_check_4bytes);
  473. /* memories[mem_idx]->mem_bound_check_4bytes */
  474. #if UINTPTR_MAX == UINT64_MAX
  475. GEN_INSN(LDI64, frame->memory_regs[mem_idx].mem_bound_check_4bytes,
  476. memory_inst_reg,
  477. NEW_CONST(I32, mem_bound_check_4bytes_offset));
  478. #else
  479. GEN_INSN(LDI32, frame->memory_regs[mem_idx].mem_bound_check_4bytes,
  480. memory_inst_reg,
  481. NEW_CONST(I32, mem_bound_check_4bytes_offset));
  482. #endif
  483. }
  484. else
  485. #endif
  486. {
  487. module_inst_reg = get_module_inst_reg(frame);
  488. mem_bound_check_4bytes_offset =
  489. (uint32)offsetof(WASMModuleInstance, global_table_data.bytes)
  490. + (uint32)offsetof(WASMMemoryInstance, mem_bound_check_4bytes);
  491. #if UINTPTR_MAX == UINT64_MAX
  492. GEN_INSN(LDI64, frame->memory_regs[mem_idx].mem_bound_check_4bytes,
  493. module_inst_reg,
  494. NEW_CONST(I32, mem_bound_check_4bytes_offset));
  495. #else
  496. GEN_INSN(LDI32, frame->memory_regs[mem_idx].mem_bound_check_4bytes,
  497. module_inst_reg,
  498. NEW_CONST(I32, mem_bound_check_4bytes_offset));
  499. #endif
  500. }
  501. return frame->memory_regs[mem_idx].mem_bound_check_4bytes;
  502. }
  503. JitReg
  504. get_mem_bound_check_8bytes_reg(JitFrame *frame, uint32 mem_idx)
  505. {
  506. JitCompContext *cc = frame->cc;
  507. JitReg module_inst_reg;
  508. uint32 mem_bound_check_8bytes_offset;
  509. #if WASM_ENABLE_SHARED_MEMORY != 0
  510. JitReg memory_inst_reg;
  511. bool is_shared;
  512. #endif
  513. if (frame->memory_regs[mem_idx].mem_bound_check_8bytes)
  514. return frame->memory_regs[mem_idx].mem_bound_check_8bytes;
  515. frame->memory_regs[mem_idx].mem_bound_check_8bytes =
  516. cc->memory_regs[mem_idx].mem_bound_check_8bytes;
  517. bh_assert(mem_idx == 0);
  518. #if WASM_ENABLE_SHARED_MEMORY != 0
  519. is_shared = is_shared_memory(cc->cur_wasm_module, mem_idx);
  520. if (is_shared) {
  521. memory_inst_reg = get_memory_inst_reg(frame, mem_idx);
  522. mem_bound_check_8bytes_offset =
  523. (uint32)offsetof(WASMMemoryInstance, mem_bound_check_8bytes);
  524. /* memories[mem_idx]->mem_bound_check_8bytes */
  525. #if UINTPTR_MAX == UINT64_MAX
  526. GEN_INSN(LDI64, frame->memory_regs[mem_idx].mem_bound_check_8bytes,
  527. memory_inst_reg,
  528. NEW_CONST(I32, mem_bound_check_8bytes_offset));
  529. #else
  530. GEN_INSN(LDI32, frame->memory_regs[mem_idx].mem_bound_check_8bytes,
  531. memory_inst_reg,
  532. NEW_CONST(I32, mem_bound_check_8bytes_offset));
  533. #endif
  534. }
  535. else
  536. #endif
  537. {
  538. module_inst_reg = get_module_inst_reg(frame);
  539. mem_bound_check_8bytes_offset =
  540. (uint32)offsetof(WASMModuleInstance, global_table_data.bytes)
  541. + (uint32)offsetof(WASMMemoryInstance, mem_bound_check_8bytes);
  542. #if UINTPTR_MAX == UINT64_MAX
  543. GEN_INSN(LDI64, frame->memory_regs[mem_idx].mem_bound_check_8bytes,
  544. module_inst_reg,
  545. NEW_CONST(I32, mem_bound_check_8bytes_offset));
  546. #else
  547. GEN_INSN(LDI32, frame->memory_regs[mem_idx].mem_bound_check_8bytes,
  548. module_inst_reg,
  549. NEW_CONST(I32, mem_bound_check_8bytes_offset));
  550. #endif
  551. }
  552. return frame->memory_regs[mem_idx].mem_bound_check_8bytes;
  553. }
  554. JitReg
  555. get_mem_bound_check_16bytes_reg(JitFrame *frame, uint32 mem_idx)
  556. {
  557. JitCompContext *cc = frame->cc;
  558. JitReg module_inst_reg;
  559. uint32 mem_bound_check_16bytes_offset;
  560. #if WASM_ENABLE_SHARED_MEMORY != 0
  561. JitReg memory_inst_reg;
  562. bool is_shared;
  563. #endif
  564. if (frame->memory_regs[mem_idx].mem_bound_check_16bytes)
  565. return frame->memory_regs[mem_idx].mem_bound_check_16bytes;
  566. frame->memory_regs[mem_idx].mem_bound_check_16bytes =
  567. cc->memory_regs[mem_idx].mem_bound_check_16bytes;
  568. bh_assert(mem_idx == 0);
  569. #if WASM_ENABLE_SHARED_MEMORY != 0
  570. is_shared = is_shared_memory(cc->cur_wasm_module, mem_idx);
  571. if (is_shared) {
  572. memory_inst_reg = get_memory_inst_reg(frame, mem_idx);
  573. mem_bound_check_16bytes_offset =
  574. (uint32)offsetof(WASMMemoryInstance, mem_bound_check_16bytes);
  575. /* memories[mem_idx]->mem_bound_check_16bytes */
  576. #if UINTPTR_MAX == UINT64_MAX
  577. GEN_INSN(LDI64, frame->memory_regs[mem_idx].mem_bound_check_16bytes,
  578. memory_inst_reg,
  579. NEW_CONST(I32, mem_bound_check_16bytes_offset));
  580. #else
  581. GEN_INSN(LDI32, frame->memory_regs[mem_idx].mem_bound_check_16bytes,
  582. memory_inst_reg,
  583. NEW_CONST(I32, mem_bound_check_16bytes_offset));
  584. #endif
  585. }
  586. else
  587. #endif
  588. {
  589. module_inst_reg = get_module_inst_reg(frame);
  590. mem_bound_check_16bytes_offset =
  591. (uint32)offsetof(WASMModuleInstance, global_table_data.bytes)
  592. + (uint32)offsetof(WASMMemoryInstance, mem_bound_check_16bytes);
  593. #if UINTPTR_MAX == UINT64_MAX
  594. GEN_INSN(LDI64, frame->memory_regs[mem_idx].mem_bound_check_16bytes,
  595. module_inst_reg,
  596. NEW_CONST(I32, mem_bound_check_16bytes_offset));
  597. #else
  598. GEN_INSN(LDI32, frame->memory_regs[mem_idx].mem_bound_check_16bytes,
  599. module_inst_reg,
  600. NEW_CONST(I32, mem_bound_check_16bytes_offset));
  601. #endif
  602. }
  603. return frame->memory_regs[mem_idx].mem_bound_check_16bytes;
  604. }
  605. JitReg
  606. get_table_elems_reg(JitFrame *frame, uint32 tbl_idx)
  607. {
  608. JitCompContext *cc = frame->cc;
  609. JitReg module_inst = get_module_inst_reg(frame);
  610. uint32 offset =
  611. jit_frontend_get_table_inst_offset(cc->cur_wasm_module, tbl_idx)
  612. + (uint32)offsetof(WASMTableInstance, elems);
  613. if (!frame->table_regs[tbl_idx].table_elems) {
  614. frame->table_regs[tbl_idx].table_elems =
  615. cc->table_regs[tbl_idx].table_elems;
  616. GEN_INSN(ADD, frame->table_regs[tbl_idx].table_elems, module_inst,
  617. NEW_CONST(PTR, offset));
  618. }
  619. return frame->table_regs[tbl_idx].table_elems;
  620. }
  621. JitReg
  622. get_table_cur_size_reg(JitFrame *frame, uint32 tbl_idx)
  623. {
  624. JitCompContext *cc = frame->cc;
  625. JitReg module_inst = get_module_inst_reg(frame);
  626. uint32 offset =
  627. jit_frontend_get_table_inst_offset(cc->cur_wasm_module, tbl_idx)
  628. + (uint32)offsetof(WASMTableInstance, cur_size);
  629. if (!frame->table_regs[tbl_idx].table_cur_size) {
  630. frame->table_regs[tbl_idx].table_cur_size =
  631. cc->table_regs[tbl_idx].table_cur_size;
  632. GEN_INSN(LDI32, frame->table_regs[tbl_idx].table_cur_size, module_inst,
  633. NEW_CONST(I32, offset));
  634. }
  635. return frame->table_regs[tbl_idx].table_cur_size;
  636. }
  637. void
  638. clear_fixed_virtual_regs(JitFrame *frame)
  639. {
  640. WASMModule *module = frame->cc->cur_wasm_module;
  641. uint32 count, i;
  642. frame->module_inst_reg = 0;
  643. frame->module_reg = 0;
  644. frame->import_func_ptrs_reg = 0;
  645. frame->fast_jit_func_ptrs_reg = 0;
  646. frame->func_type_indexes_reg = 0;
  647. frame->aux_stack_bound_reg = 0;
  648. frame->aux_stack_bottom_reg = 0;
  649. count = module->import_memory_count + module->memory_count;
  650. for (i = 0; i < count; i++) {
  651. frame->memory_regs[i].memory_inst = 0;
  652. frame->memory_regs[i].cur_page_count = 0;
  653. frame->memory_regs[i].memory_data = 0;
  654. frame->memory_regs[i].memory_data_end = 0;
  655. frame->memory_regs[i].mem_bound_check_1byte = 0;
  656. frame->memory_regs[i].mem_bound_check_2bytes = 0;
  657. frame->memory_regs[i].mem_bound_check_4bytes = 0;
  658. frame->memory_regs[i].mem_bound_check_8bytes = 0;
  659. frame->memory_regs[i].mem_bound_check_16bytes = 0;
  660. }
  661. count = module->import_table_count + module->table_count;
  662. for (i = 0; i < count; i++) {
  663. frame->table_regs[i].table_elems = 0;
  664. frame->table_regs[i].table_cur_size = 0;
  665. }
  666. }
  667. void
  668. clear_memory_regs(JitFrame *frame)
  669. {
  670. WASMModule *module = frame->cc->cur_wasm_module;
  671. uint32 count, i;
  672. count = module->import_memory_count + module->memory_count;
  673. for (i = 0; i < count; i++) {
  674. frame->memory_regs[i].cur_page_count = 0;
  675. frame->memory_regs[i].memory_data = 0;
  676. frame->memory_regs[i].memory_data_end = 0;
  677. frame->memory_regs[i].mem_bound_check_1byte = 0;
  678. frame->memory_regs[i].mem_bound_check_2bytes = 0;
  679. frame->memory_regs[i].mem_bound_check_4bytes = 0;
  680. frame->memory_regs[i].mem_bound_check_8bytes = 0;
  681. frame->memory_regs[i].mem_bound_check_16bytes = 0;
  682. }
  683. }
  684. void
  685. clear_table_regs(JitFrame *frame)
  686. {
  687. WASMModule *module = frame->cc->cur_wasm_module;
  688. uint32 count, i;
  689. count = module->import_table_count + module->table_count;
  690. for (i = 0; i < count; i++) {
  691. frame->table_regs[i].table_cur_size = 0;
  692. }
  693. }
  694. JitReg
  695. gen_load_i32(JitFrame *frame, unsigned n)
  696. {
  697. if (!frame->lp[n].reg) {
  698. JitCompContext *cc = frame->cc;
  699. frame->lp[n].reg = jit_cc_new_reg_I32(cc);
  700. GEN_INSN(LDI32, frame->lp[n].reg, cc->fp_reg,
  701. NEW_CONST(I32, offset_of_local(n)));
  702. }
  703. return frame->lp[n].reg;
  704. }
  705. JitReg
  706. gen_load_i64(JitFrame *frame, unsigned n)
  707. {
  708. if (!frame->lp[n].reg) {
  709. JitCompContext *cc = frame->cc;
  710. frame->lp[n].reg = frame->lp[n + 1].reg = jit_cc_new_reg_I64(cc);
  711. GEN_INSN(LDI64, frame->lp[n].reg, cc->fp_reg,
  712. NEW_CONST(I32, offset_of_local(n)));
  713. }
  714. return frame->lp[n].reg;
  715. }
  716. JitReg
  717. gen_load_f32(JitFrame *frame, unsigned n)
  718. {
  719. if (!frame->lp[n].reg) {
  720. JitCompContext *cc = frame->cc;
  721. frame->lp[n].reg = jit_cc_new_reg_F32(cc);
  722. GEN_INSN(LDF32, frame->lp[n].reg, cc->fp_reg,
  723. NEW_CONST(I32, offset_of_local(n)));
  724. }
  725. return frame->lp[n].reg;
  726. }
  727. JitReg
  728. gen_load_f64(JitFrame *frame, unsigned n)
  729. {
  730. if (!frame->lp[n].reg) {
  731. JitCompContext *cc = frame->cc;
  732. frame->lp[n].reg = frame->lp[n + 1].reg = jit_cc_new_reg_F64(cc);
  733. GEN_INSN(LDF64, frame->lp[n].reg, cc->fp_reg,
  734. NEW_CONST(I32, offset_of_local(n)));
  735. }
  736. return frame->lp[n].reg;
  737. }
  738. void
  739. gen_commit_values(JitFrame *frame, JitValueSlot *begin, JitValueSlot *end)
  740. {
  741. JitCompContext *cc = frame->cc;
  742. JitValueSlot *p;
  743. int n;
  744. for (p = begin; p < end; p++) {
  745. if (!p->dirty)
  746. continue;
  747. p->dirty = 0;
  748. n = p - frame->lp;
  749. switch (jit_reg_kind(p->reg)) {
  750. case JIT_REG_KIND_I32:
  751. GEN_INSN(STI32, p->reg, cc->fp_reg,
  752. NEW_CONST(I32, offset_of_local(n)));
  753. break;
  754. case JIT_REG_KIND_I64:
  755. GEN_INSN(STI64, p->reg, cc->fp_reg,
  756. NEW_CONST(I32, offset_of_local(n)));
  757. (++p)->dirty = 0;
  758. break;
  759. case JIT_REG_KIND_F32:
  760. GEN_INSN(STF32, p->reg, cc->fp_reg,
  761. NEW_CONST(I32, offset_of_local(n)));
  762. break;
  763. case JIT_REG_KIND_F64:
  764. GEN_INSN(STF64, p->reg, cc->fp_reg,
  765. NEW_CONST(I32, offset_of_local(n)));
  766. (++p)->dirty = 0;
  767. break;
  768. }
  769. }
  770. }
  771. /**
  772. * Generate instructions to commit SP and IP pointers to the frame.
  773. *
  774. * @param frame the frame information
  775. */
  776. void
  777. gen_commit_sp_ip(JitFrame *frame)
  778. {
  779. JitCompContext *cc = frame->cc;
  780. JitReg sp;
  781. if (frame->sp != frame->committed_sp) {
  782. sp = jit_cc_new_reg_ptr(cc);
  783. GEN_INSN(ADD, sp, cc->fp_reg,
  784. NEW_CONST(PTR, offset_of_local(frame->sp - frame->lp)));
  785. GEN_INSN(STPTR, sp, cc->fp_reg,
  786. NEW_CONST(I32, offsetof(WASMInterpFrame, sp)));
  787. frame->committed_sp = frame->sp;
  788. }
  789. #if 0 /* Disable committing ip currently */
  790. if (frame->ip != frame->committed_ip) {
  791. GEN_INSN(STPTR, NEW_CONST(PTR, (uintptr_t)frame->ip), cc->fp_reg,
  792. NEW_CONST(I32, offsetof(WASMInterpFrame, ip)));
  793. frame->committed_ip = frame->ip;
  794. }
  795. #endif
  796. }
  797. static bool
  798. create_fixed_virtual_regs(JitCompContext *cc)
  799. {
  800. WASMModule *module = cc->cur_wasm_module;
  801. uint64 total_size;
  802. uint32 i, count;
  803. cc->module_inst_reg = jit_cc_new_reg_ptr(cc);
  804. cc->module_reg = jit_cc_new_reg_ptr(cc);
  805. cc->import_func_ptrs_reg = jit_cc_new_reg_ptr(cc);
  806. cc->fast_jit_func_ptrs_reg = jit_cc_new_reg_ptr(cc);
  807. cc->func_type_indexes_reg = jit_cc_new_reg_ptr(cc);
  808. cc->aux_stack_bound_reg = jit_cc_new_reg_I32(cc);
  809. cc->aux_stack_bottom_reg = jit_cc_new_reg_I32(cc);
  810. count = module->import_memory_count + module->memory_count;
  811. if (count > 0) {
  812. total_size = (uint64)sizeof(JitMemRegs) * count;
  813. if (total_size > UINT32_MAX
  814. || !(cc->memory_regs = jit_calloc((uint32)total_size))) {
  815. jit_set_last_error(cc, "allocate memory failed");
  816. return false;
  817. }
  818. for (i = 0; i < count; i++) {
  819. cc->memory_regs[i].memory_inst = jit_cc_new_reg_ptr(cc);
  820. cc->memory_regs[i].cur_page_count = jit_cc_new_reg_I32(cc);
  821. cc->memory_regs[i].memory_data = jit_cc_new_reg_ptr(cc);
  822. cc->memory_regs[i].memory_data_end = jit_cc_new_reg_ptr(cc);
  823. cc->memory_regs[i].mem_bound_check_1byte = jit_cc_new_reg_ptr(cc);
  824. cc->memory_regs[i].mem_bound_check_2bytes = jit_cc_new_reg_ptr(cc);
  825. cc->memory_regs[i].mem_bound_check_4bytes = jit_cc_new_reg_ptr(cc);
  826. cc->memory_regs[i].mem_bound_check_8bytes = jit_cc_new_reg_ptr(cc);
  827. cc->memory_regs[i].mem_bound_check_16bytes = jit_cc_new_reg_ptr(cc);
  828. }
  829. }
  830. count = module->import_table_count + module->table_count;
  831. if (count > 0) {
  832. total_size = (uint64)sizeof(JitTableRegs) * count;
  833. if (total_size > UINT32_MAX
  834. || !(cc->table_regs = jit_calloc((uint32)total_size))) {
  835. jit_set_last_error(cc, "allocate memory failed");
  836. return false;
  837. }
  838. for (i = 0; i < count; i++) {
  839. cc->table_regs[i].table_elems = jit_cc_new_reg_ptr(cc);
  840. cc->table_regs[i].table_cur_size = jit_cc_new_reg_I32(cc);
  841. }
  842. }
  843. return true;
  844. }
  845. static bool
  846. form_and_translate_func(JitCompContext *cc)
  847. {
  848. JitBasicBlock *func_entry_basic_block;
  849. JitReg func_entry_label;
  850. JitInsn *insn;
  851. JitIncomingInsn *incoming_insn, *incoming_insn_next;
  852. uint32 i;
  853. if (!create_fixed_virtual_regs(cc))
  854. return false;
  855. if (!(func_entry_basic_block = jit_frontend_translate_func(cc)))
  856. return false;
  857. jit_cc_reset_insn_hash(cc);
  858. /* The label of the func entry basic block. */
  859. func_entry_label = jit_basic_block_label(func_entry_basic_block);
  860. /* Create a JMP instruction jumping to the func entry. */
  861. if (!(insn = jit_cc_new_insn(cc, JMP, func_entry_label)))
  862. return false;
  863. /* Insert the instruction into the cc entry block. */
  864. jit_basic_block_append_insn(jit_cc_entry_basic_block(cc), insn);
  865. /* Patch INSNs jumping to exception basic blocks. */
  866. for (i = 0; i < EXCE_NUM; i++) {
  867. incoming_insn = cc->incoming_insns_for_exec_bbs[i];
  868. if (incoming_insn) {
  869. if (!(cc->exce_basic_blocks[i] = jit_cc_new_basic_block(cc, 0))) {
  870. jit_set_last_error(cc, "create basic block failed");
  871. return false;
  872. }
  873. while (incoming_insn) {
  874. incoming_insn_next = incoming_insn->next;
  875. insn = incoming_insn->insn;
  876. if (insn->opcode == JIT_OP_JMP) {
  877. *(jit_insn_opnd(insn, 0)) =
  878. jit_basic_block_label(cc->exce_basic_blocks[i]);
  879. }
  880. else if (insn->opcode >= JIT_OP_BEQ
  881. && insn->opcode <= JIT_OP_BLEU) {
  882. *(jit_insn_opnd(insn, 1)) =
  883. jit_basic_block_label(cc->exce_basic_blocks[i]);
  884. }
  885. incoming_insn = incoming_insn_next;
  886. }
  887. cc->cur_basic_block = cc->exce_basic_blocks[i];
  888. if (i != EXCE_ALREADY_THROWN) {
  889. JitReg module_inst_reg = jit_cc_new_reg_ptr(cc);
  890. GEN_INSN(LDPTR, module_inst_reg, cc->exec_env_reg,
  891. NEW_CONST(I32, offsetof(WASMExecEnv, module_inst)));
  892. insn = GEN_INSN(
  893. CALLNATIVE, 0,
  894. NEW_CONST(PTR, (uintptr_t)jit_set_exception_with_id), 2);
  895. if (insn) {
  896. *(jit_insn_opndv(insn, 2)) = module_inst_reg;
  897. *(jit_insn_opndv(insn, 3)) = NEW_CONST(I32, i);
  898. }
  899. }
  900. GEN_INSN(RETURN, NEW_CONST(I32, JIT_INTERP_ACTION_THROWN));
  901. *(jit_annl_begin_bcip(cc,
  902. jit_basic_block_label(cc->cur_basic_block))) =
  903. *(jit_annl_end_bcip(
  904. cc, jit_basic_block_label(cc->cur_basic_block))) =
  905. cc->cur_wasm_module->load_addr;
  906. }
  907. }
  908. *(jit_annl_begin_bcip(cc, cc->entry_label)) =
  909. *(jit_annl_end_bcip(cc, cc->entry_label)) =
  910. *(jit_annl_begin_bcip(cc, cc->exit_label)) =
  911. *(jit_annl_end_bcip(cc, cc->exit_label)) =
  912. cc->cur_wasm_module->load_addr;
  913. if (jit_get_last_error(cc)) {
  914. return false;
  915. }
  916. return true;
  917. }
  918. bool
  919. jit_pass_frontend(JitCompContext *cc)
  920. {
  921. /* Enable necessary annotations required at the current stage. */
  922. if (!jit_annl_enable_begin_bcip(cc) || !jit_annl_enable_end_bcip(cc)
  923. || !jit_annl_enable_end_sp(cc) || !jit_annr_enable_def_insn(cc)
  924. || !jit_cc_enable_insn_hash(cc, 127))
  925. return false;
  926. if (!(form_and_translate_func(cc)))
  927. return false;
  928. /* Release the annotations after local CSE and translation. */
  929. jit_cc_disable_insn_hash(cc);
  930. jit_annl_disable_end_sp(cc);
  931. return true;
  932. }
  933. static JitFrame *
  934. init_func_translation(JitCompContext *cc)
  935. {
  936. JitFrame *jit_frame;
  937. JitReg top, top_boundary, new_top, frame_boundary, frame_sp;
  938. WASMModule *cur_wasm_module = cc->cur_wasm_module;
  939. WASMFunction *cur_wasm_func = cc->cur_wasm_func;
  940. uint32 cur_wasm_func_idx = cc->cur_wasm_func_idx;
  941. uint32 max_locals =
  942. cur_wasm_func->param_cell_num + cur_wasm_func->local_cell_num;
  943. uint32 max_stacks = cur_wasm_func->max_stack_cell_num;
  944. uint64 total_cell_num =
  945. (uint64)cur_wasm_func->param_cell_num
  946. + (uint64)cur_wasm_func->local_cell_num
  947. + (uint64)cur_wasm_func->max_stack_cell_num
  948. + ((uint64)cur_wasm_func->max_block_num) * sizeof(WASMBranchBlock) / 4;
  949. uint32 frame_size, outs_size, local_size, count;
  950. uint32 i, local_off;
  951. uint64 total_size;
  952. #if WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_PERF_PROFILING != 0
  953. JitReg module_inst, func_inst;
  954. uint32 func_insts_offset;
  955. #if WASM_ENABLE_PERF_PROFILING != 0
  956. JitReg time_started;
  957. #endif
  958. #endif
  959. if ((uint64)max_locals + (uint64)max_stacks >= UINT32_MAX
  960. || total_cell_num >= UINT32_MAX
  961. || !(jit_frame = jit_calloc(offsetof(JitFrame, lp)
  962. + sizeof(*jit_frame->lp)
  963. * (max_locals + max_stacks)))) {
  964. os_printf("allocate jit frame failed\n");
  965. return NULL;
  966. }
  967. count =
  968. cur_wasm_module->import_memory_count + cur_wasm_module->memory_count;
  969. if (count > 0) {
  970. total_size = (uint64)sizeof(JitMemRegs) * count;
  971. if (total_size > UINT32_MAX
  972. || !(jit_frame->memory_regs = jit_calloc((uint32)total_size))) {
  973. jit_set_last_error(cc, "allocate memory failed");
  974. jit_free(jit_frame);
  975. return NULL;
  976. }
  977. }
  978. count = cur_wasm_module->import_table_count + cur_wasm_module->table_count;
  979. if (count > 0) {
  980. total_size = (uint64)sizeof(JitTableRegs) * count;
  981. if (total_size > UINT32_MAX
  982. || !(jit_frame->table_regs = jit_calloc((uint32)total_size))) {
  983. jit_set_last_error(cc, "allocate memory failed");
  984. if (jit_frame->memory_regs)
  985. jit_free(jit_frame->memory_regs);
  986. jit_free(jit_frame);
  987. return NULL;
  988. }
  989. }
  990. jit_frame->cur_wasm_module = cur_wasm_module;
  991. jit_frame->cur_wasm_func = cur_wasm_func;
  992. jit_frame->cur_wasm_func_idx = cur_wasm_func_idx;
  993. jit_frame->cc = cc;
  994. jit_frame->max_locals = max_locals;
  995. jit_frame->max_stacks = max_stacks;
  996. jit_frame->sp = jit_frame->lp + max_locals;
  997. jit_frame->ip = cur_wasm_func->code;
  998. cc->jit_frame = jit_frame;
  999. cc->cur_basic_block = jit_cc_entry_basic_block(cc);
  1000. cc->spill_cache_offset = wasm_interp_interp_frame_size(total_cell_num);
  1001. /* Set spill cache size according to max local cell num, max stack cell
  1002. num and virtual fixed register num */
  1003. cc->spill_cache_size = (max_locals + max_stacks) * 4 + sizeof(void *) * 16;
  1004. cc->total_frame_size = cc->spill_cache_offset + cc->spill_cache_size;
  1005. cc->jitted_return_address_offset =
  1006. offsetof(WASMInterpFrame, jitted_return_addr);
  1007. cc->cur_basic_block = jit_cc_entry_basic_block(cc);
  1008. frame_size = outs_size = cc->total_frame_size;
  1009. local_size =
  1010. (cur_wasm_func->param_cell_num + cur_wasm_func->local_cell_num) * 4;
  1011. top = jit_cc_new_reg_ptr(cc);
  1012. top_boundary = jit_cc_new_reg_ptr(cc);
  1013. new_top = jit_cc_new_reg_ptr(cc);
  1014. frame_boundary = jit_cc_new_reg_ptr(cc);
  1015. frame_sp = jit_cc_new_reg_ptr(cc);
  1016. #if WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_PERF_PROFILING != 0
  1017. module_inst = jit_cc_new_reg_ptr(cc);
  1018. func_inst = jit_cc_new_reg_ptr(cc);
  1019. #if WASM_ENABLE_PERF_PROFILING != 0
  1020. time_started = jit_cc_new_reg_I64(cc);
  1021. /* Call os_time_get_boot_us() to get time_started firstly
  1022. as there is stack frame switching below, calling native in them
  1023. may cause register spilling work inproperly */
  1024. if (!jit_emit_callnative(cc, os_time_get_boot_us, time_started, NULL, 0)) {
  1025. return NULL;
  1026. }
  1027. #endif
  1028. #endif
  1029. /* top = exec_env->wasm_stack.s.top */
  1030. GEN_INSN(LDPTR, top, cc->exec_env_reg,
  1031. NEW_CONST(I32, offsetof(WASMExecEnv, wasm_stack.s.top)));
  1032. /* top_boundary = exec_env->wasm_stack.s.top_boundary */
  1033. GEN_INSN(LDPTR, top_boundary, cc->exec_env_reg,
  1034. NEW_CONST(I32, offsetof(WASMExecEnv, wasm_stack.s.top_boundary)));
  1035. /* frame_boundary = top + frame_size + outs_size */
  1036. GEN_INSN(ADD, frame_boundary, top, NEW_CONST(PTR, frame_size + outs_size));
  1037. /* if frame_boundary > top_boundary, throw stack overflow exception */
  1038. GEN_INSN(CMP, cc->cmp_reg, frame_boundary, top_boundary);
  1039. if (!jit_emit_exception(cc, EXCE_OPERAND_STACK_OVERFLOW, JIT_OP_BGTU,
  1040. cc->cmp_reg, NULL)) {
  1041. return NULL;
  1042. }
  1043. /* Add first and then sub to reduce one used register */
  1044. /* new_top = frame_boundary - outs_size = top + frame_size */
  1045. GEN_INSN(SUB, new_top, frame_boundary, NEW_CONST(PTR, outs_size));
  1046. /* exec_env->wasm_stack.s.top = new_top */
  1047. GEN_INSN(STPTR, new_top, cc->exec_env_reg,
  1048. NEW_CONST(I32, offsetof(WASMExecEnv, wasm_stack.s.top)));
  1049. /* frame_sp = frame->lp + local_size */
  1050. GEN_INSN(ADD, frame_sp, top,
  1051. NEW_CONST(PTR, offsetof(WASMInterpFrame, lp) + local_size));
  1052. /* frame->sp = frame_sp */
  1053. GEN_INSN(STPTR, frame_sp, top,
  1054. NEW_CONST(I32, offsetof(WASMInterpFrame, sp)));
  1055. /* frame->prev_frame = fp_reg */
  1056. GEN_INSN(STPTR, cc->fp_reg, top,
  1057. NEW_CONST(I32, offsetof(WASMInterpFrame, prev_frame)));
  1058. #if WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_PERF_PROFILING != 0
  1059. /* module_inst = exec_env->module_inst */
  1060. GEN_INSN(LDPTR, module_inst, cc->exec_env_reg,
  1061. NEW_CONST(I32, offsetof(WASMExecEnv, module_inst)));
  1062. func_insts_offset =
  1063. jit_frontend_get_module_inst_extra_offset(cur_wasm_module)
  1064. + (uint32)offsetof(WASMModuleInstanceExtra, functions);
  1065. /* func_inst = module_inst->e->functions */
  1066. GEN_INSN(LDPTR, func_inst, module_inst, NEW_CONST(I32, func_insts_offset));
  1067. /* func_inst = func_inst + cur_wasm_func_idx */
  1068. GEN_INSN(ADD, func_inst, func_inst,
  1069. NEW_CONST(PTR, (uint32)sizeof(WASMFunctionInstance)
  1070. * cur_wasm_func_idx));
  1071. /* frame->function = func_inst */
  1072. GEN_INSN(STPTR, func_inst, top,
  1073. NEW_CONST(I32, offsetof(WASMInterpFrame, function)));
  1074. #if WASM_ENABLE_PERF_PROFILING != 0
  1075. /* frame->time_started = time_started */
  1076. GEN_INSN(STI64, time_started, top,
  1077. NEW_CONST(I32, offsetof(WASMInterpFrame, time_started)));
  1078. #endif
  1079. #endif
  1080. /* exec_env->cur_frame = top */
  1081. GEN_INSN(STPTR, top, cc->exec_env_reg,
  1082. NEW_CONST(I32, offsetof(WASMExecEnv, cur_frame)));
  1083. /* fp_reg = top */
  1084. GEN_INSN(MOV, cc->fp_reg, top);
  1085. /* Initialize local variables, set them to 0 */
  1086. local_off = (uint32)offsetof(WASMInterpFrame, lp)
  1087. + cur_wasm_func->param_cell_num * 4;
  1088. for (i = 0; i < cur_wasm_func->local_cell_num / 2; i++, local_off += 8) {
  1089. GEN_INSN(STI64, NEW_CONST(I64, 0), cc->fp_reg,
  1090. NEW_CONST(I32, local_off));
  1091. }
  1092. if (cur_wasm_func->local_cell_num & 1) {
  1093. GEN_INSN(STI32, NEW_CONST(I32, 0), cc->fp_reg,
  1094. NEW_CONST(I32, local_off));
  1095. }
  1096. return jit_frame;
  1097. }
  1098. static void
  1099. free_block_memory(JitBlock *block)
  1100. {
  1101. if (block->param_types)
  1102. jit_free(block->param_types);
  1103. if (block->result_types)
  1104. jit_free(block->result_types);
  1105. jit_free(block);
  1106. }
  1107. static JitBasicBlock *
  1108. create_func_block(JitCompContext *cc)
  1109. {
  1110. JitBlock *jit_block;
  1111. WASMFunction *cur_func = cc->cur_wasm_func;
  1112. WASMType *func_type = cur_func->func_type;
  1113. uint32 param_count = func_type->param_count;
  1114. uint32 result_count = func_type->result_count;
  1115. if (!(jit_block = jit_calloc(sizeof(JitBlock)))) {
  1116. return NULL;
  1117. }
  1118. if (param_count && !(jit_block->param_types = jit_calloc(param_count))) {
  1119. goto fail;
  1120. }
  1121. if (result_count && !(jit_block->result_types = jit_calloc(result_count))) {
  1122. goto fail;
  1123. }
  1124. /* Set block data */
  1125. jit_block->label_type = LABEL_TYPE_FUNCTION;
  1126. jit_block->param_count = param_count;
  1127. if (param_count) {
  1128. bh_memcpy_s(jit_block->param_types, param_count, func_type->types,
  1129. param_count);
  1130. }
  1131. jit_block->result_count = result_count;
  1132. if (result_count) {
  1133. bh_memcpy_s(jit_block->result_types, result_count,
  1134. func_type->types + param_count, result_count);
  1135. }
  1136. jit_block->wasm_code_end = cur_func->code + cur_func->code_size;
  1137. jit_block->frame_sp_begin = cc->jit_frame->sp;
  1138. /* Add function entry block */
  1139. if (!(jit_block->basic_block_entry = jit_cc_new_basic_block(cc, 0))) {
  1140. goto fail;
  1141. }
  1142. *(jit_annl_begin_bcip(
  1143. cc, jit_basic_block_label(jit_block->basic_block_entry))) =
  1144. cur_func->code;
  1145. jit_block_stack_push(&cc->block_stack, jit_block);
  1146. cc->cur_basic_block = jit_block->basic_block_entry;
  1147. return jit_block->basic_block_entry;
  1148. fail:
  1149. free_block_memory(jit_block);
  1150. return NULL;
  1151. }
  1152. #define CHECK_BUF(buf, buf_end, length) \
  1153. do { \
  1154. if (buf + length > buf_end) { \
  1155. jit_set_last_error(cc, "read leb failed: unexpected end."); \
  1156. return false; \
  1157. } \
  1158. } while (0)
  1159. static bool
  1160. read_leb(JitCompContext *cc, const uint8 *buf, const uint8 *buf_end,
  1161. uint32 *p_offset, uint32 maxbits, bool sign, uint64 *p_result)
  1162. {
  1163. uint64 result = 0;
  1164. uint32 shift = 0;
  1165. uint32 bcnt = 0;
  1166. uint64 byte;
  1167. while (true) {
  1168. CHECK_BUF(buf, buf_end, 1);
  1169. byte = buf[*p_offset];
  1170. *p_offset += 1;
  1171. result |= ((byte & 0x7f) << shift);
  1172. shift += 7;
  1173. if ((byte & 0x80) == 0) {
  1174. break;
  1175. }
  1176. bcnt += 1;
  1177. }
  1178. if (bcnt > (maxbits + 6) / 7) {
  1179. jit_set_last_error(cc, "read leb failed: "
  1180. "integer representation too long");
  1181. return false;
  1182. }
  1183. if (sign && (shift < maxbits) && (byte & 0x40)) {
  1184. /* Sign extend */
  1185. result |= (~((uint64)0)) << shift;
  1186. }
  1187. *p_result = result;
  1188. return true;
  1189. }
  1190. #define read_leb_uint32(p, p_end, res) \
  1191. do { \
  1192. uint32 off = 0; \
  1193. uint64 res64; \
  1194. if (!read_leb(cc, p, p_end, &off, 32, false, &res64)) \
  1195. return false; \
  1196. p += off; \
  1197. res = (uint32)res64; \
  1198. } while (0)
  1199. #define read_leb_int32(p, p_end, res) \
  1200. do { \
  1201. uint32 off = 0; \
  1202. uint64 res64; \
  1203. if (!read_leb(cc, p, p_end, &off, 32, true, &res64)) \
  1204. return false; \
  1205. p += off; \
  1206. res = (int32)res64; \
  1207. } while (0)
  1208. #define read_leb_int64(p, p_end, res) \
  1209. do { \
  1210. uint32 off = 0; \
  1211. uint64 res64; \
  1212. if (!read_leb(cc, p, p_end, &off, 64, true, &res64)) \
  1213. return false; \
  1214. p += off; \
  1215. res = (int64)res64; \
  1216. } while (0)
  1217. #if WASM_ENABLE_SHARED_MEMORY != 0
  1218. #define COMPILE_ATOMIC_RMW(OP, NAME) \
  1219. case WASM_OP_ATOMIC_RMW_I32_##NAME: \
  1220. bytes = 4; \
  1221. op_type = VALUE_TYPE_I32; \
  1222. goto OP_ATOMIC_##OP; \
  1223. case WASM_OP_ATOMIC_RMW_I64_##NAME: \
  1224. bytes = 8; \
  1225. op_type = VALUE_TYPE_I64; \
  1226. goto OP_ATOMIC_##OP; \
  1227. case WASM_OP_ATOMIC_RMW_I32_##NAME##8_U: \
  1228. bytes = 1; \
  1229. op_type = VALUE_TYPE_I32; \
  1230. goto OP_ATOMIC_##OP; \
  1231. case WASM_OP_ATOMIC_RMW_I32_##NAME##16_U: \
  1232. bytes = 2; \
  1233. op_type = VALUE_TYPE_I32; \
  1234. goto OP_ATOMIC_##OP; \
  1235. case WASM_OP_ATOMIC_RMW_I64_##NAME##8_U: \
  1236. bytes = 1; \
  1237. op_type = VALUE_TYPE_I64; \
  1238. goto OP_ATOMIC_##OP; \
  1239. case WASM_OP_ATOMIC_RMW_I64_##NAME##16_U: \
  1240. bytes = 2; \
  1241. op_type = VALUE_TYPE_I64; \
  1242. goto OP_ATOMIC_##OP; \
  1243. case WASM_OP_ATOMIC_RMW_I64_##NAME##32_U: \
  1244. bytes = 4; \
  1245. op_type = VALUE_TYPE_I64; \
  1246. OP_ATOMIC_##OP : bin_op = AtomicRMWBinOp##OP; \
  1247. goto build_atomic_rmw;
  1248. #endif
  1249. static bool
  1250. jit_compile_func(JitCompContext *cc)
  1251. {
  1252. WASMFunction *cur_func = cc->cur_wasm_func;
  1253. WASMType *func_type = NULL;
  1254. uint8 *frame_ip = cur_func->code, opcode, *p_f32, *p_f64;
  1255. uint8 *frame_ip_end = frame_ip + cur_func->code_size;
  1256. uint8 *param_types = NULL, *result_types = NULL, value_type;
  1257. uint16 param_count, result_count;
  1258. uint32 br_depth, *br_depths, br_count;
  1259. uint32 func_idx, type_idx, mem_idx, local_idx, global_idx, i;
  1260. uint32 bytes = 4, align, offset;
  1261. bool merge_cmp_and_if = false, merge_cmp_and_br_if = false;
  1262. bool sign = true;
  1263. int32 i32_const;
  1264. int64 i64_const;
  1265. float32 f32_const;
  1266. float64 f64_const;
  1267. while (frame_ip < frame_ip_end) {
  1268. cc->jit_frame->ip = frame_ip;
  1269. opcode = *frame_ip++;
  1270. #if 0 /* TODO */
  1271. #if WASM_ENABLE_THREAD_MGR != 0
  1272. /* Insert suspend check point */
  1273. if (cc->enable_thread_mgr) {
  1274. if (!check_suspend_flags(cc, func_ctx))
  1275. return false;
  1276. }
  1277. #endif
  1278. #endif
  1279. switch (opcode) {
  1280. case WASM_OP_UNREACHABLE:
  1281. if (!jit_compile_op_unreachable(cc, &frame_ip))
  1282. return false;
  1283. break;
  1284. case WASM_OP_NOP:
  1285. break;
  1286. case WASM_OP_BLOCK:
  1287. case WASM_OP_LOOP:
  1288. case WASM_OP_IF:
  1289. {
  1290. value_type = *frame_ip++;
  1291. if (value_type == VALUE_TYPE_I32 || value_type == VALUE_TYPE_I64
  1292. || value_type == VALUE_TYPE_F32
  1293. || value_type == VALUE_TYPE_F64
  1294. || value_type == VALUE_TYPE_V128
  1295. || value_type == VALUE_TYPE_VOID
  1296. || value_type == VALUE_TYPE_FUNCREF
  1297. || value_type == VALUE_TYPE_EXTERNREF) {
  1298. param_count = 0;
  1299. param_types = NULL;
  1300. if (value_type == VALUE_TYPE_VOID) {
  1301. result_count = 0;
  1302. result_types = NULL;
  1303. }
  1304. else {
  1305. result_count = 1;
  1306. result_types = &value_type;
  1307. }
  1308. }
  1309. else {
  1310. jit_set_last_error(cc, "unsupported value type");
  1311. return false;
  1312. }
  1313. if (!jit_compile_op_block(
  1314. cc, &frame_ip, frame_ip_end,
  1315. (uint32)(LABEL_TYPE_BLOCK + opcode - WASM_OP_BLOCK),
  1316. param_count, param_types, result_count, result_types,
  1317. merge_cmp_and_if))
  1318. return false;
  1319. /* Clear flag */
  1320. merge_cmp_and_if = false;
  1321. break;
  1322. }
  1323. case EXT_OP_BLOCK:
  1324. case EXT_OP_LOOP:
  1325. case EXT_OP_IF:
  1326. {
  1327. read_leb_uint32(frame_ip, frame_ip_end, type_idx);
  1328. func_type = cc->cur_wasm_module->types[type_idx];
  1329. param_count = func_type->param_count;
  1330. param_types = func_type->types;
  1331. result_count = func_type->result_count;
  1332. result_types = func_type->types + param_count;
  1333. if (!jit_compile_op_block(
  1334. cc, &frame_ip, frame_ip_end,
  1335. (uint32)(LABEL_TYPE_BLOCK + opcode - EXT_OP_BLOCK),
  1336. param_count, param_types, result_count, result_types,
  1337. merge_cmp_and_if))
  1338. return false;
  1339. /* Clear flag */
  1340. merge_cmp_and_if = false;
  1341. break;
  1342. }
  1343. case WASM_OP_ELSE:
  1344. if (!jit_compile_op_else(cc, &frame_ip))
  1345. return false;
  1346. break;
  1347. case WASM_OP_END:
  1348. if (!jit_compile_op_end(cc, &frame_ip))
  1349. return false;
  1350. break;
  1351. case WASM_OP_BR:
  1352. read_leb_uint32(frame_ip, frame_ip_end, br_depth);
  1353. if (!jit_compile_op_br(cc, br_depth, &frame_ip))
  1354. return false;
  1355. break;
  1356. case WASM_OP_BR_IF:
  1357. read_leb_uint32(frame_ip, frame_ip_end, br_depth);
  1358. if (!jit_compile_op_br_if(cc, br_depth, merge_cmp_and_br_if,
  1359. &frame_ip))
  1360. return false;
  1361. /* Clear flag */
  1362. merge_cmp_and_br_if = false;
  1363. break;
  1364. case WASM_OP_BR_TABLE:
  1365. read_leb_uint32(frame_ip, frame_ip_end, br_count);
  1366. if (!(br_depths = jit_calloc((uint32)sizeof(uint32)
  1367. * (br_count + 1)))) {
  1368. jit_set_last_error(cc, "allocate memory failed.");
  1369. goto fail;
  1370. }
  1371. #if WASM_ENABLE_FAST_INTERP != 0
  1372. for (i = 0; i <= br_count; i++)
  1373. read_leb_uint32(frame_ip, frame_ip_end, br_depths[i]);
  1374. #else
  1375. for (i = 0; i <= br_count; i++)
  1376. br_depths[i] = *frame_ip++;
  1377. #endif
  1378. if (!jit_compile_op_br_table(cc, br_depths, br_count,
  1379. &frame_ip)) {
  1380. jit_free(br_depths);
  1381. return false;
  1382. }
  1383. jit_free(br_depths);
  1384. break;
  1385. #if WASM_ENABLE_FAST_INTERP == 0
  1386. case EXT_OP_BR_TABLE_CACHE:
  1387. {
  1388. BrTableCache *node = bh_list_first_elem(
  1389. cc->cur_wasm_module->br_table_cache_list);
  1390. BrTableCache *node_next;
  1391. uint8 *p_opcode = frame_ip - 1;
  1392. read_leb_uint32(frame_ip, frame_ip_end, br_count);
  1393. while (node) {
  1394. node_next = bh_list_elem_next(node);
  1395. if (node->br_table_op_addr == p_opcode) {
  1396. br_depths = node->br_depths;
  1397. if (!jit_compile_op_br_table(cc, br_depths, br_count,
  1398. &frame_ip)) {
  1399. return false;
  1400. }
  1401. break;
  1402. }
  1403. node = node_next;
  1404. }
  1405. bh_assert(node);
  1406. break;
  1407. }
  1408. #endif
  1409. case WASM_OP_RETURN:
  1410. if (!jit_compile_op_return(cc, &frame_ip))
  1411. return false;
  1412. break;
  1413. case WASM_OP_CALL:
  1414. read_leb_uint32(frame_ip, frame_ip_end, func_idx);
  1415. if (!jit_compile_op_call(cc, func_idx, false))
  1416. return false;
  1417. break;
  1418. case WASM_OP_CALL_INDIRECT:
  1419. {
  1420. uint32 tbl_idx;
  1421. read_leb_uint32(frame_ip, frame_ip_end, type_idx);
  1422. #if WASM_ENABLE_REF_TYPES != 0
  1423. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  1424. #else
  1425. frame_ip++;
  1426. tbl_idx = 0;
  1427. #endif
  1428. if (!jit_compile_op_call_indirect(cc, type_idx, tbl_idx))
  1429. return false;
  1430. break;
  1431. }
  1432. #if WASM_ENABLE_TAIL_CALL != 0
  1433. case WASM_OP_RETURN_CALL:
  1434. read_leb_uint32(frame_ip, frame_ip_end, func_idx);
  1435. if (!jit_compile_op_call(cc, func_idx, true))
  1436. return false;
  1437. if (!jit_compile_op_return(cc, &frame_ip))
  1438. return false;
  1439. break;
  1440. case WASM_OP_RETURN_CALL_INDIRECT:
  1441. {
  1442. uint32 tbl_idx;
  1443. read_leb_uint32(frame_ip, frame_ip_end, type_idx);
  1444. #if WASM_ENABLE_REF_TYPES != 0
  1445. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  1446. #else
  1447. frame_ip++;
  1448. tbl_idx = 0;
  1449. #endif
  1450. if (!jit_compile_op_call_indirect(cc, type_idx, tbl_idx))
  1451. return false;
  1452. if (!jit_compile_op_return(cc, &frame_ip))
  1453. return false;
  1454. break;
  1455. }
  1456. #endif /* end of WASM_ENABLE_TAIL_CALL */
  1457. case WASM_OP_DROP:
  1458. if (!jit_compile_op_drop(cc, true))
  1459. return false;
  1460. break;
  1461. case WASM_OP_DROP_64:
  1462. if (!jit_compile_op_drop(cc, false))
  1463. return false;
  1464. break;
  1465. case WASM_OP_SELECT:
  1466. if (!jit_compile_op_select(cc, true))
  1467. return false;
  1468. break;
  1469. case WASM_OP_SELECT_64:
  1470. if (!jit_compile_op_select(cc, false))
  1471. return false;
  1472. break;
  1473. #if WASM_ENABLE_REF_TYPES != 0
  1474. case WASM_OP_SELECT_T:
  1475. {
  1476. uint32 vec_len;
  1477. read_leb_uint32(frame_ip, frame_ip_end, vec_len);
  1478. bh_assert(vec_len == 1);
  1479. (void)vec_len;
  1480. type_idx = *frame_ip++;
  1481. if (!jit_compile_op_select(cc,
  1482. (type_idx != VALUE_TYPE_I64)
  1483. && (type_idx != VALUE_TYPE_F64)))
  1484. return false;
  1485. break;
  1486. }
  1487. case WASM_OP_TABLE_GET:
  1488. {
  1489. uint32 tbl_idx;
  1490. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  1491. if (!jit_compile_op_table_get(cc, tbl_idx))
  1492. return false;
  1493. break;
  1494. }
  1495. case WASM_OP_TABLE_SET:
  1496. {
  1497. uint32 tbl_idx;
  1498. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  1499. if (!jit_compile_op_table_set(cc, tbl_idx))
  1500. return false;
  1501. break;
  1502. }
  1503. case WASM_OP_REF_NULL:
  1504. {
  1505. uint32 ref_type;
  1506. read_leb_uint32(frame_ip, frame_ip_end, ref_type);
  1507. if (!jit_compile_op_ref_null(cc, ref_type))
  1508. return false;
  1509. break;
  1510. }
  1511. case WASM_OP_REF_IS_NULL:
  1512. {
  1513. if (!jit_compile_op_ref_is_null(cc))
  1514. return false;
  1515. break;
  1516. }
  1517. case WASM_OP_REF_FUNC:
  1518. {
  1519. read_leb_uint32(frame_ip, frame_ip_end, func_idx);
  1520. if (!jit_compile_op_ref_func(cc, func_idx))
  1521. return false;
  1522. break;
  1523. }
  1524. #endif
  1525. case WASM_OP_GET_LOCAL:
  1526. read_leb_uint32(frame_ip, frame_ip_end, local_idx);
  1527. if (!jit_compile_op_get_local(cc, local_idx))
  1528. return false;
  1529. break;
  1530. case WASM_OP_SET_LOCAL:
  1531. read_leb_uint32(frame_ip, frame_ip_end, local_idx);
  1532. if (!jit_compile_op_set_local(cc, local_idx))
  1533. return false;
  1534. break;
  1535. case WASM_OP_TEE_LOCAL:
  1536. read_leb_uint32(frame_ip, frame_ip_end, local_idx);
  1537. if (!jit_compile_op_tee_local(cc, local_idx))
  1538. return false;
  1539. break;
  1540. case WASM_OP_GET_GLOBAL:
  1541. case WASM_OP_GET_GLOBAL_64:
  1542. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  1543. if (!jit_compile_op_get_global(cc, global_idx))
  1544. return false;
  1545. break;
  1546. case WASM_OP_SET_GLOBAL:
  1547. case WASM_OP_SET_GLOBAL_64:
  1548. case WASM_OP_SET_GLOBAL_AUX_STACK:
  1549. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  1550. if (!jit_compile_op_set_global(
  1551. cc, global_idx,
  1552. opcode == WASM_OP_SET_GLOBAL_AUX_STACK ? true : false))
  1553. return false;
  1554. break;
  1555. case WASM_OP_I32_LOAD:
  1556. bytes = 4;
  1557. sign = true;
  1558. goto op_i32_load;
  1559. case WASM_OP_I32_LOAD8_S:
  1560. case WASM_OP_I32_LOAD8_U:
  1561. bytes = 1;
  1562. sign = (opcode == WASM_OP_I32_LOAD8_S) ? true : false;
  1563. goto op_i32_load;
  1564. case WASM_OP_I32_LOAD16_S:
  1565. case WASM_OP_I32_LOAD16_U:
  1566. bytes = 2;
  1567. sign = (opcode == WASM_OP_I32_LOAD16_S) ? true : false;
  1568. op_i32_load:
  1569. read_leb_uint32(frame_ip, frame_ip_end, align);
  1570. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1571. if (!jit_compile_op_i32_load(cc, align, offset, bytes, sign,
  1572. false))
  1573. return false;
  1574. break;
  1575. case WASM_OP_I64_LOAD:
  1576. bytes = 8;
  1577. sign = true;
  1578. goto op_i64_load;
  1579. case WASM_OP_I64_LOAD8_S:
  1580. case WASM_OP_I64_LOAD8_U:
  1581. bytes = 1;
  1582. sign = (opcode == WASM_OP_I64_LOAD8_S) ? true : false;
  1583. goto op_i64_load;
  1584. case WASM_OP_I64_LOAD16_S:
  1585. case WASM_OP_I64_LOAD16_U:
  1586. bytes = 2;
  1587. sign = (opcode == WASM_OP_I64_LOAD16_S) ? true : false;
  1588. goto op_i64_load;
  1589. case WASM_OP_I64_LOAD32_S:
  1590. case WASM_OP_I64_LOAD32_U:
  1591. bytes = 4;
  1592. sign = (opcode == WASM_OP_I64_LOAD32_S) ? true : false;
  1593. op_i64_load:
  1594. read_leb_uint32(frame_ip, frame_ip_end, align);
  1595. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1596. if (!jit_compile_op_i64_load(cc, align, offset, bytes, sign,
  1597. false))
  1598. return false;
  1599. break;
  1600. case WASM_OP_F32_LOAD:
  1601. read_leb_uint32(frame_ip, frame_ip_end, align);
  1602. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1603. if (!jit_compile_op_f32_load(cc, align, offset))
  1604. return false;
  1605. break;
  1606. case WASM_OP_F64_LOAD:
  1607. read_leb_uint32(frame_ip, frame_ip_end, align);
  1608. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1609. if (!jit_compile_op_f64_load(cc, align, offset))
  1610. return false;
  1611. break;
  1612. case WASM_OP_I32_STORE:
  1613. bytes = 4;
  1614. goto op_i32_store;
  1615. case WASM_OP_I32_STORE8:
  1616. bytes = 1;
  1617. goto op_i32_store;
  1618. case WASM_OP_I32_STORE16:
  1619. bytes = 2;
  1620. op_i32_store:
  1621. read_leb_uint32(frame_ip, frame_ip_end, align);
  1622. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1623. if (!jit_compile_op_i32_store(cc, align, offset, bytes, false))
  1624. return false;
  1625. break;
  1626. case WASM_OP_I64_STORE:
  1627. bytes = 8;
  1628. goto op_i64_store;
  1629. case WASM_OP_I64_STORE8:
  1630. bytes = 1;
  1631. goto op_i64_store;
  1632. case WASM_OP_I64_STORE16:
  1633. bytes = 2;
  1634. goto op_i64_store;
  1635. case WASM_OP_I64_STORE32:
  1636. bytes = 4;
  1637. op_i64_store:
  1638. read_leb_uint32(frame_ip, frame_ip_end, align);
  1639. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1640. if (!jit_compile_op_i64_store(cc, align, offset, bytes, false))
  1641. return false;
  1642. break;
  1643. case WASM_OP_F32_STORE:
  1644. read_leb_uint32(frame_ip, frame_ip_end, align);
  1645. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1646. if (!jit_compile_op_f32_store(cc, align, offset))
  1647. return false;
  1648. break;
  1649. case WASM_OP_F64_STORE:
  1650. read_leb_uint32(frame_ip, frame_ip_end, align);
  1651. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1652. if (!jit_compile_op_f64_store(cc, align, offset))
  1653. return false;
  1654. break;
  1655. case WASM_OP_MEMORY_SIZE:
  1656. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  1657. if (!jit_compile_op_memory_size(cc, mem_idx))
  1658. return false;
  1659. break;
  1660. case WASM_OP_MEMORY_GROW:
  1661. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  1662. if (!jit_compile_op_memory_grow(cc, mem_idx))
  1663. return false;
  1664. break;
  1665. case WASM_OP_I32_CONST:
  1666. read_leb_int32(frame_ip, frame_ip_end, i32_const);
  1667. if (!jit_compile_op_i32_const(cc, i32_const))
  1668. return false;
  1669. break;
  1670. case WASM_OP_I64_CONST:
  1671. read_leb_int64(frame_ip, frame_ip_end, i64_const);
  1672. if (!jit_compile_op_i64_const(cc, i64_const))
  1673. return false;
  1674. break;
  1675. case WASM_OP_F32_CONST:
  1676. p_f32 = (uint8 *)&f32_const;
  1677. for (i = 0; i < sizeof(float32); i++)
  1678. *p_f32++ = *frame_ip++;
  1679. if (!jit_compile_op_f32_const(cc, f32_const))
  1680. return false;
  1681. break;
  1682. case WASM_OP_F64_CONST:
  1683. p_f64 = (uint8 *)&f64_const;
  1684. for (i = 0; i < sizeof(float64); i++)
  1685. *p_f64++ = *frame_ip++;
  1686. if (!jit_compile_op_f64_const(cc, f64_const))
  1687. return false;
  1688. break;
  1689. case WASM_OP_I32_EQZ:
  1690. case WASM_OP_I32_EQ:
  1691. case WASM_OP_I32_NE:
  1692. case WASM_OP_I32_LT_S:
  1693. case WASM_OP_I32_LT_U:
  1694. case WASM_OP_I32_GT_S:
  1695. case WASM_OP_I32_GT_U:
  1696. case WASM_OP_I32_LE_S:
  1697. case WASM_OP_I32_LE_U:
  1698. case WASM_OP_I32_GE_S:
  1699. case WASM_OP_I32_GE_U:
  1700. if (!jit_compile_op_i32_compare(cc, INT_EQZ + opcode
  1701. - WASM_OP_I32_EQZ))
  1702. return false;
  1703. if (frame_ip < frame_ip_end) {
  1704. /* Merge `CMP, SELECTcc, CMP, BNE` insns into `CMP, Bcc` */
  1705. if (*frame_ip == WASM_OP_IF || *frame_ip == EXT_OP_IF)
  1706. merge_cmp_and_if = true;
  1707. if (*frame_ip == WASM_OP_BR_IF)
  1708. merge_cmp_and_br_if = true;
  1709. }
  1710. break;
  1711. case WASM_OP_I64_EQZ:
  1712. case WASM_OP_I64_EQ:
  1713. case WASM_OP_I64_NE:
  1714. case WASM_OP_I64_LT_S:
  1715. case WASM_OP_I64_LT_U:
  1716. case WASM_OP_I64_GT_S:
  1717. case WASM_OP_I64_GT_U:
  1718. case WASM_OP_I64_LE_S:
  1719. case WASM_OP_I64_LE_U:
  1720. case WASM_OP_I64_GE_S:
  1721. case WASM_OP_I64_GE_U:
  1722. if (!jit_compile_op_i64_compare(cc, INT_EQZ + opcode
  1723. - WASM_OP_I64_EQZ))
  1724. return false;
  1725. if (frame_ip < frame_ip_end) {
  1726. /* Merge `CMP, SELECTcc, CMP, BNE` insns into `CMP, Bcc` */
  1727. if (*frame_ip == WASM_OP_IF || *frame_ip == EXT_OP_IF)
  1728. merge_cmp_and_if = true;
  1729. if (*frame_ip == WASM_OP_BR_IF)
  1730. merge_cmp_and_br_if = true;
  1731. }
  1732. break;
  1733. case WASM_OP_F32_EQ:
  1734. case WASM_OP_F32_NE:
  1735. case WASM_OP_F32_LT:
  1736. case WASM_OP_F32_GT:
  1737. case WASM_OP_F32_LE:
  1738. case WASM_OP_F32_GE:
  1739. if (!jit_compile_op_f32_compare(cc, FLOAT_EQ + opcode
  1740. - WASM_OP_F32_EQ))
  1741. return false;
  1742. if (frame_ip < frame_ip_end) {
  1743. /* Merge `CMP, SELECTcc, CMP, BNE` insns into `CMP, Bcc` */
  1744. if (*frame_ip == WASM_OP_IF || *frame_ip == EXT_OP_IF)
  1745. merge_cmp_and_if = true;
  1746. if (*frame_ip == WASM_OP_BR_IF)
  1747. merge_cmp_and_br_if = true;
  1748. }
  1749. break;
  1750. case WASM_OP_F64_EQ:
  1751. case WASM_OP_F64_NE:
  1752. case WASM_OP_F64_LT:
  1753. case WASM_OP_F64_GT:
  1754. case WASM_OP_F64_LE:
  1755. case WASM_OP_F64_GE:
  1756. if (!jit_compile_op_f64_compare(cc, FLOAT_EQ + opcode
  1757. - WASM_OP_F64_EQ))
  1758. return false;
  1759. if (frame_ip < frame_ip_end) {
  1760. /* Merge `CMP, SELECTcc, CMP, BNE` insns into `CMP, Bcc` */
  1761. if (*frame_ip == WASM_OP_IF || *frame_ip == EXT_OP_IF)
  1762. merge_cmp_and_if = true;
  1763. if (*frame_ip == WASM_OP_BR_IF)
  1764. merge_cmp_and_br_if = true;
  1765. }
  1766. break;
  1767. case WASM_OP_I32_CLZ:
  1768. if (!jit_compile_op_i32_clz(cc))
  1769. return false;
  1770. break;
  1771. case WASM_OP_I32_CTZ:
  1772. if (!jit_compile_op_i32_ctz(cc))
  1773. return false;
  1774. break;
  1775. case WASM_OP_I32_POPCNT:
  1776. if (!jit_compile_op_i32_popcnt(cc))
  1777. return false;
  1778. break;
  1779. case WASM_OP_I32_ADD:
  1780. case WASM_OP_I32_SUB:
  1781. case WASM_OP_I32_MUL:
  1782. case WASM_OP_I32_DIV_S:
  1783. case WASM_OP_I32_DIV_U:
  1784. case WASM_OP_I32_REM_S:
  1785. case WASM_OP_I32_REM_U:
  1786. if (!jit_compile_op_i32_arithmetic(
  1787. cc, INT_ADD + opcode - WASM_OP_I32_ADD, &frame_ip))
  1788. return false;
  1789. break;
  1790. case WASM_OP_I32_AND:
  1791. case WASM_OP_I32_OR:
  1792. case WASM_OP_I32_XOR:
  1793. if (!jit_compile_op_i32_bitwise(cc, INT_SHL + opcode
  1794. - WASM_OP_I32_AND))
  1795. return false;
  1796. break;
  1797. case WASM_OP_I32_SHL:
  1798. case WASM_OP_I32_SHR_S:
  1799. case WASM_OP_I32_SHR_U:
  1800. case WASM_OP_I32_ROTL:
  1801. case WASM_OP_I32_ROTR:
  1802. if (!jit_compile_op_i32_shift(cc, INT_SHL + opcode
  1803. - WASM_OP_I32_SHL))
  1804. return false;
  1805. break;
  1806. case WASM_OP_I64_CLZ:
  1807. if (!jit_compile_op_i64_clz(cc))
  1808. return false;
  1809. break;
  1810. case WASM_OP_I64_CTZ:
  1811. if (!jit_compile_op_i64_ctz(cc))
  1812. return false;
  1813. break;
  1814. case WASM_OP_I64_POPCNT:
  1815. if (!jit_compile_op_i64_popcnt(cc))
  1816. return false;
  1817. break;
  1818. case WASM_OP_I64_ADD:
  1819. case WASM_OP_I64_SUB:
  1820. case WASM_OP_I64_MUL:
  1821. case WASM_OP_I64_DIV_S:
  1822. case WASM_OP_I64_DIV_U:
  1823. case WASM_OP_I64_REM_S:
  1824. case WASM_OP_I64_REM_U:
  1825. if (!jit_compile_op_i64_arithmetic(
  1826. cc, INT_ADD + opcode - WASM_OP_I64_ADD, &frame_ip))
  1827. return false;
  1828. break;
  1829. case WASM_OP_I64_AND:
  1830. case WASM_OP_I64_OR:
  1831. case WASM_OP_I64_XOR:
  1832. if (!jit_compile_op_i64_bitwise(cc, INT_SHL + opcode
  1833. - WASM_OP_I64_AND))
  1834. return false;
  1835. break;
  1836. case WASM_OP_I64_SHL:
  1837. case WASM_OP_I64_SHR_S:
  1838. case WASM_OP_I64_SHR_U:
  1839. case WASM_OP_I64_ROTL:
  1840. case WASM_OP_I64_ROTR:
  1841. if (!jit_compile_op_i64_shift(cc, INT_SHL + opcode
  1842. - WASM_OP_I64_SHL))
  1843. return false;
  1844. break;
  1845. case WASM_OP_F32_ABS:
  1846. case WASM_OP_F32_NEG:
  1847. case WASM_OP_F32_CEIL:
  1848. case WASM_OP_F32_FLOOR:
  1849. case WASM_OP_F32_TRUNC:
  1850. case WASM_OP_F32_NEAREST:
  1851. case WASM_OP_F32_SQRT:
  1852. if (!jit_compile_op_f32_math(cc, FLOAT_ABS + opcode
  1853. - WASM_OP_F32_ABS))
  1854. return false;
  1855. break;
  1856. case WASM_OP_F32_ADD:
  1857. case WASM_OP_F32_SUB:
  1858. case WASM_OP_F32_MUL:
  1859. case WASM_OP_F32_DIV:
  1860. case WASM_OP_F32_MIN:
  1861. case WASM_OP_F32_MAX:
  1862. if (!jit_compile_op_f32_arithmetic(cc, FLOAT_ADD + opcode
  1863. - WASM_OP_F32_ADD))
  1864. return false;
  1865. break;
  1866. case WASM_OP_F32_COPYSIGN:
  1867. if (!jit_compile_op_f32_copysign(cc))
  1868. return false;
  1869. break;
  1870. case WASM_OP_F64_ABS:
  1871. case WASM_OP_F64_NEG:
  1872. case WASM_OP_F64_CEIL:
  1873. case WASM_OP_F64_FLOOR:
  1874. case WASM_OP_F64_TRUNC:
  1875. case WASM_OP_F64_NEAREST:
  1876. case WASM_OP_F64_SQRT:
  1877. if (!jit_compile_op_f64_math(cc, FLOAT_ABS + opcode
  1878. - WASM_OP_F64_ABS))
  1879. return false;
  1880. break;
  1881. case WASM_OP_F64_ADD:
  1882. case WASM_OP_F64_SUB:
  1883. case WASM_OP_F64_MUL:
  1884. case WASM_OP_F64_DIV:
  1885. case WASM_OP_F64_MIN:
  1886. case WASM_OP_F64_MAX:
  1887. if (!jit_compile_op_f64_arithmetic(cc, FLOAT_ADD + opcode
  1888. - WASM_OP_F64_ADD))
  1889. return false;
  1890. break;
  1891. case WASM_OP_F64_COPYSIGN:
  1892. if (!jit_compile_op_f64_copysign(cc))
  1893. return false;
  1894. break;
  1895. case WASM_OP_I32_WRAP_I64:
  1896. if (!jit_compile_op_i32_wrap_i64(cc))
  1897. return false;
  1898. break;
  1899. case WASM_OP_I32_TRUNC_S_F32:
  1900. case WASM_OP_I32_TRUNC_U_F32:
  1901. sign = (opcode == WASM_OP_I32_TRUNC_S_F32) ? true : false;
  1902. if (!jit_compile_op_i32_trunc_f32(cc, sign, false))
  1903. return false;
  1904. break;
  1905. case WASM_OP_I32_TRUNC_S_F64:
  1906. case WASM_OP_I32_TRUNC_U_F64:
  1907. sign = (opcode == WASM_OP_I32_TRUNC_S_F64) ? true : false;
  1908. if (!jit_compile_op_i32_trunc_f64(cc, sign, false))
  1909. return false;
  1910. break;
  1911. case WASM_OP_I64_EXTEND_S_I32:
  1912. case WASM_OP_I64_EXTEND_U_I32:
  1913. sign = (opcode == WASM_OP_I64_EXTEND_S_I32) ? true : false;
  1914. if (!jit_compile_op_i64_extend_i32(cc, sign))
  1915. return false;
  1916. break;
  1917. case WASM_OP_I64_TRUNC_S_F32:
  1918. case WASM_OP_I64_TRUNC_U_F32:
  1919. sign = (opcode == WASM_OP_I64_TRUNC_S_F32) ? true : false;
  1920. if (!jit_compile_op_i64_trunc_f32(cc, sign, false))
  1921. return false;
  1922. break;
  1923. case WASM_OP_I64_TRUNC_S_F64:
  1924. case WASM_OP_I64_TRUNC_U_F64:
  1925. sign = (opcode == WASM_OP_I64_TRUNC_S_F64) ? true : false;
  1926. if (!jit_compile_op_i64_trunc_f64(cc, sign, false))
  1927. return false;
  1928. break;
  1929. case WASM_OP_F32_CONVERT_S_I32:
  1930. case WASM_OP_F32_CONVERT_U_I32:
  1931. sign = (opcode == WASM_OP_F32_CONVERT_S_I32) ? true : false;
  1932. if (!jit_compile_op_f32_convert_i32(cc, sign))
  1933. return false;
  1934. break;
  1935. case WASM_OP_F32_CONVERT_S_I64:
  1936. case WASM_OP_F32_CONVERT_U_I64:
  1937. sign = (opcode == WASM_OP_F32_CONVERT_S_I64) ? true : false;
  1938. if (!jit_compile_op_f32_convert_i64(cc, sign))
  1939. return false;
  1940. break;
  1941. case WASM_OP_F32_DEMOTE_F64:
  1942. if (!jit_compile_op_f32_demote_f64(cc))
  1943. return false;
  1944. break;
  1945. case WASM_OP_F64_CONVERT_S_I32:
  1946. case WASM_OP_F64_CONVERT_U_I32:
  1947. sign = (opcode == WASM_OP_F64_CONVERT_S_I32) ? true : false;
  1948. if (!jit_compile_op_f64_convert_i32(cc, sign))
  1949. return false;
  1950. break;
  1951. case WASM_OP_F64_CONVERT_S_I64:
  1952. case WASM_OP_F64_CONVERT_U_I64:
  1953. sign = (opcode == WASM_OP_F64_CONVERT_S_I64) ? true : false;
  1954. if (!jit_compile_op_f64_convert_i64(cc, sign))
  1955. return false;
  1956. break;
  1957. case WASM_OP_F64_PROMOTE_F32:
  1958. if (!jit_compile_op_f64_promote_f32(cc))
  1959. return false;
  1960. break;
  1961. case WASM_OP_I32_REINTERPRET_F32:
  1962. if (!jit_compile_op_i32_reinterpret_f32(cc))
  1963. return false;
  1964. break;
  1965. case WASM_OP_I64_REINTERPRET_F64:
  1966. if (!jit_compile_op_i64_reinterpret_f64(cc))
  1967. return false;
  1968. break;
  1969. case WASM_OP_F32_REINTERPRET_I32:
  1970. if (!jit_compile_op_f32_reinterpret_i32(cc))
  1971. return false;
  1972. break;
  1973. case WASM_OP_F64_REINTERPRET_I64:
  1974. if (!jit_compile_op_f64_reinterpret_i64(cc))
  1975. return false;
  1976. break;
  1977. case WASM_OP_I32_EXTEND8_S:
  1978. if (!jit_compile_op_i32_extend_i32(cc, 8))
  1979. return false;
  1980. break;
  1981. case WASM_OP_I32_EXTEND16_S:
  1982. if (!jit_compile_op_i32_extend_i32(cc, 16))
  1983. return false;
  1984. break;
  1985. case WASM_OP_I64_EXTEND8_S:
  1986. if (!jit_compile_op_i64_extend_i64(cc, 8))
  1987. return false;
  1988. break;
  1989. case WASM_OP_I64_EXTEND16_S:
  1990. if (!jit_compile_op_i64_extend_i64(cc, 16))
  1991. return false;
  1992. break;
  1993. case WASM_OP_I64_EXTEND32_S:
  1994. if (!jit_compile_op_i64_extend_i64(cc, 32))
  1995. return false;
  1996. break;
  1997. case WASM_OP_MISC_PREFIX:
  1998. {
  1999. uint32 opcode1;
  2000. read_leb_uint32(frame_ip, frame_ip_end, opcode1);
  2001. opcode = (uint32)opcode1;
  2002. switch (opcode) {
  2003. case WASM_OP_I32_TRUNC_SAT_S_F32:
  2004. case WASM_OP_I32_TRUNC_SAT_U_F32:
  2005. sign = (opcode == WASM_OP_I32_TRUNC_SAT_S_F32) ? true
  2006. : false;
  2007. if (!jit_compile_op_i32_trunc_f32(cc, sign, true))
  2008. return false;
  2009. break;
  2010. case WASM_OP_I32_TRUNC_SAT_S_F64:
  2011. case WASM_OP_I32_TRUNC_SAT_U_F64:
  2012. sign = (opcode == WASM_OP_I32_TRUNC_SAT_S_F64) ? true
  2013. : false;
  2014. if (!jit_compile_op_i32_trunc_f64(cc, sign, true))
  2015. return false;
  2016. break;
  2017. case WASM_OP_I64_TRUNC_SAT_S_F32:
  2018. case WASM_OP_I64_TRUNC_SAT_U_F32:
  2019. sign = (opcode == WASM_OP_I64_TRUNC_SAT_S_F32) ? true
  2020. : false;
  2021. if (!jit_compile_op_i64_trunc_f32(cc, sign, true))
  2022. return false;
  2023. break;
  2024. case WASM_OP_I64_TRUNC_SAT_S_F64:
  2025. case WASM_OP_I64_TRUNC_SAT_U_F64:
  2026. sign = (opcode == WASM_OP_I64_TRUNC_SAT_S_F64) ? true
  2027. : false;
  2028. if (!jit_compile_op_i64_trunc_f64(cc, sign, true))
  2029. return false;
  2030. break;
  2031. #if WASM_ENABLE_BULK_MEMORY != 0
  2032. case WASM_OP_MEMORY_INIT:
  2033. {
  2034. uint32 seg_idx = 0;
  2035. read_leb_uint32(frame_ip, frame_ip_end, seg_idx);
  2036. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  2037. if (!jit_compile_op_memory_init(cc, mem_idx, seg_idx))
  2038. return false;
  2039. break;
  2040. }
  2041. case WASM_OP_DATA_DROP:
  2042. {
  2043. uint32 seg_idx;
  2044. read_leb_uint32(frame_ip, frame_ip_end, seg_idx);
  2045. if (!jit_compile_op_data_drop(cc, seg_idx))
  2046. return false;
  2047. break;
  2048. }
  2049. case WASM_OP_MEMORY_COPY:
  2050. {
  2051. uint32 src_mem_idx, dst_mem_idx;
  2052. read_leb_uint32(frame_ip, frame_ip_end, src_mem_idx);
  2053. read_leb_uint32(frame_ip, frame_ip_end, dst_mem_idx);
  2054. if (!jit_compile_op_memory_copy(cc, src_mem_idx,
  2055. dst_mem_idx))
  2056. return false;
  2057. break;
  2058. }
  2059. case WASM_OP_MEMORY_FILL:
  2060. {
  2061. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  2062. if (!jit_compile_op_memory_fill(cc, mem_idx))
  2063. return false;
  2064. break;
  2065. }
  2066. #endif /* WASM_ENABLE_BULK_MEMORY */
  2067. #if WASM_ENABLE_REF_TYPES != 0
  2068. case WASM_OP_TABLE_INIT:
  2069. {
  2070. uint32 tbl_idx, tbl_seg_idx;
  2071. read_leb_uint32(frame_ip, frame_ip_end, tbl_seg_idx);
  2072. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  2073. if (!jit_compile_op_table_init(cc, tbl_idx,
  2074. tbl_seg_idx))
  2075. return false;
  2076. break;
  2077. }
  2078. case WASM_OP_ELEM_DROP:
  2079. {
  2080. uint32 tbl_seg_idx;
  2081. read_leb_uint32(frame_ip, frame_ip_end, tbl_seg_idx);
  2082. if (!jit_compile_op_elem_drop(cc, tbl_seg_idx))
  2083. return false;
  2084. break;
  2085. }
  2086. case WASM_OP_TABLE_COPY:
  2087. {
  2088. uint32 src_tbl_idx, dst_tbl_idx;
  2089. read_leb_uint32(frame_ip, frame_ip_end, dst_tbl_idx);
  2090. read_leb_uint32(frame_ip, frame_ip_end, src_tbl_idx);
  2091. if (!jit_compile_op_table_copy(cc, src_tbl_idx,
  2092. dst_tbl_idx))
  2093. return false;
  2094. break;
  2095. }
  2096. case WASM_OP_TABLE_GROW:
  2097. {
  2098. uint32 tbl_idx;
  2099. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  2100. if (!jit_compile_op_table_grow(cc, tbl_idx))
  2101. return false;
  2102. break;
  2103. }
  2104. case WASM_OP_TABLE_SIZE:
  2105. {
  2106. uint32 tbl_idx;
  2107. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  2108. if (!jit_compile_op_table_size(cc, tbl_idx))
  2109. return false;
  2110. break;
  2111. }
  2112. case WASM_OP_TABLE_FILL:
  2113. {
  2114. uint32 tbl_idx;
  2115. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  2116. if (!jit_compile_op_table_fill(cc, tbl_idx))
  2117. return false;
  2118. break;
  2119. }
  2120. #endif /* WASM_ENABLE_REF_TYPES */
  2121. default:
  2122. jit_set_last_error(cc, "unsupported opcode");
  2123. return false;
  2124. }
  2125. break;
  2126. }
  2127. #if WASM_ENABLE_SHARED_MEMORY != 0
  2128. case WASM_OP_ATOMIC_PREFIX:
  2129. {
  2130. uint8 bin_op, op_type;
  2131. if (frame_ip < frame_ip_end) {
  2132. opcode = *frame_ip++;
  2133. }
  2134. if (opcode != WASM_OP_ATOMIC_FENCE) {
  2135. read_leb_uint32(frame_ip, frame_ip_end, align);
  2136. read_leb_uint32(frame_ip, frame_ip_end, offset);
  2137. }
  2138. switch (opcode) {
  2139. case WASM_OP_ATOMIC_WAIT32:
  2140. if (!jit_compile_op_atomic_wait(cc, VALUE_TYPE_I32,
  2141. align, offset, 4))
  2142. return false;
  2143. break;
  2144. case WASM_OP_ATOMIC_WAIT64:
  2145. if (!jit_compile_op_atomic_wait(cc, VALUE_TYPE_I64,
  2146. align, offset, 8))
  2147. return false;
  2148. break;
  2149. case WASM_OP_ATOMIC_NOTIFY:
  2150. if (!jit_compiler_op_atomic_notify(cc, align, offset,
  2151. bytes))
  2152. return false;
  2153. break;
  2154. case WASM_OP_ATOMIC_FENCE:
  2155. /* Skip memory index */
  2156. frame_ip++;
  2157. if (!jit_compiler_op_atomic_fence(cc))
  2158. return false;
  2159. break;
  2160. case WASM_OP_ATOMIC_I32_LOAD:
  2161. bytes = 4;
  2162. goto op_atomic_i32_load;
  2163. case WASM_OP_ATOMIC_I32_LOAD8_U:
  2164. bytes = 1;
  2165. goto op_atomic_i32_load;
  2166. case WASM_OP_ATOMIC_I32_LOAD16_U:
  2167. bytes = 2;
  2168. op_atomic_i32_load:
  2169. if (!jit_compile_op_i32_load(cc, align, offset, bytes,
  2170. sign, true))
  2171. return false;
  2172. break;
  2173. case WASM_OP_ATOMIC_I64_LOAD:
  2174. bytes = 8;
  2175. goto op_atomic_i64_load;
  2176. case WASM_OP_ATOMIC_I64_LOAD8_U:
  2177. bytes = 1;
  2178. goto op_atomic_i64_load;
  2179. case WASM_OP_ATOMIC_I64_LOAD16_U:
  2180. bytes = 2;
  2181. goto op_atomic_i64_load;
  2182. case WASM_OP_ATOMIC_I64_LOAD32_U:
  2183. bytes = 4;
  2184. op_atomic_i64_load:
  2185. if (!jit_compile_op_i64_load(cc, align, offset, bytes,
  2186. sign, true))
  2187. return false;
  2188. break;
  2189. case WASM_OP_ATOMIC_I32_STORE:
  2190. bytes = 4;
  2191. goto op_atomic_i32_store;
  2192. case WASM_OP_ATOMIC_I32_STORE8:
  2193. bytes = 1;
  2194. goto op_atomic_i32_store;
  2195. case WASM_OP_ATOMIC_I32_STORE16:
  2196. bytes = 2;
  2197. op_atomic_i32_store:
  2198. if (!jit_compile_op_i32_store(cc, align, offset, bytes,
  2199. true))
  2200. return false;
  2201. break;
  2202. case WASM_OP_ATOMIC_I64_STORE:
  2203. bytes = 8;
  2204. goto op_atomic_i64_store;
  2205. case WASM_OP_ATOMIC_I64_STORE8:
  2206. bytes = 1;
  2207. goto op_atomic_i64_store;
  2208. case WASM_OP_ATOMIC_I64_STORE16:
  2209. bytes = 2;
  2210. goto op_atomic_i64_store;
  2211. case WASM_OP_ATOMIC_I64_STORE32:
  2212. bytes = 4;
  2213. op_atomic_i64_store:
  2214. if (!jit_compile_op_i64_store(cc, align, offset, bytes,
  2215. true))
  2216. return false;
  2217. break;
  2218. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  2219. bytes = 4;
  2220. op_type = VALUE_TYPE_I32;
  2221. goto op_atomic_cmpxchg;
  2222. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  2223. bytes = 8;
  2224. op_type = VALUE_TYPE_I64;
  2225. goto op_atomic_cmpxchg;
  2226. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  2227. bytes = 1;
  2228. op_type = VALUE_TYPE_I32;
  2229. goto op_atomic_cmpxchg;
  2230. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  2231. bytes = 2;
  2232. op_type = VALUE_TYPE_I32;
  2233. goto op_atomic_cmpxchg;
  2234. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  2235. bytes = 1;
  2236. op_type = VALUE_TYPE_I64;
  2237. goto op_atomic_cmpxchg;
  2238. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  2239. bytes = 2;
  2240. op_type = VALUE_TYPE_I64;
  2241. goto op_atomic_cmpxchg;
  2242. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  2243. bytes = 4;
  2244. op_type = VALUE_TYPE_I64;
  2245. op_atomic_cmpxchg:
  2246. if (!jit_compile_op_atomic_cmpxchg(cc, op_type, align,
  2247. offset, bytes))
  2248. return false;
  2249. break;
  2250. COMPILE_ATOMIC_RMW(Add, ADD);
  2251. COMPILE_ATOMIC_RMW(Sub, SUB);
  2252. COMPILE_ATOMIC_RMW(And, AND);
  2253. COMPILE_ATOMIC_RMW(Or, OR);
  2254. COMPILE_ATOMIC_RMW(Xor, XOR);
  2255. COMPILE_ATOMIC_RMW(Xchg, XCHG);
  2256. build_atomic_rmw:
  2257. if (!jit_compile_op_atomic_rmw(cc, bin_op, op_type,
  2258. align, offset, bytes))
  2259. return false;
  2260. break;
  2261. default:
  2262. jit_set_last_error(cc, "unsupported opcode");
  2263. return false;
  2264. }
  2265. break;
  2266. }
  2267. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  2268. default:
  2269. jit_set_last_error(cc, "unsupported opcode");
  2270. return false;
  2271. }
  2272. /* Error may occur when creating registers, basic blocks, insns,
  2273. consts and labels, in which the return value may be unchecked,
  2274. here we check again */
  2275. if (jit_get_last_error(cc)) {
  2276. return false;
  2277. }
  2278. }
  2279. (void)func_idx;
  2280. return true;
  2281. fail:
  2282. return false;
  2283. }
  2284. JitBasicBlock *
  2285. jit_frontend_translate_func(JitCompContext *cc)
  2286. {
  2287. JitFrame *jit_frame;
  2288. JitBasicBlock *basic_block_entry;
  2289. if (!(jit_frame = init_func_translation(cc))) {
  2290. return NULL;
  2291. }
  2292. if (!(basic_block_entry = create_func_block(cc))) {
  2293. return NULL;
  2294. }
  2295. if (!jit_compile_func(cc)) {
  2296. return NULL;
  2297. }
  2298. return basic_block_entry;
  2299. }
  2300. uint32
  2301. jit_frontend_get_jitted_return_addr_offset()
  2302. {
  2303. return (uint32)offsetof(WASMInterpFrame, jitted_return_addr);
  2304. }