jit_frontend.c 85 KB

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