jit_frontend.c 92 KB

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