jit_frontend.c 94 KB

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