jit_frontend.c 93 KB

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