jit_frontend.c 94 KB

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