jit_frontend.c 83 KB

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