jit_frontend.c 92 KB

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