aot_compiler.c 161 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "aot_compiler.h"
  6. #include "aot_emit_compare.h"
  7. #include "aot_emit_conversion.h"
  8. #include "aot_emit_memory.h"
  9. #include "aot_emit_variable.h"
  10. #include "aot_emit_const.h"
  11. #include "aot_emit_exception.h"
  12. #include "aot_emit_numberic.h"
  13. #include "aot_emit_control.h"
  14. #include "aot_emit_function.h"
  15. #include "aot_emit_parametric.h"
  16. #include "aot_emit_table.h"
  17. #include "aot_emit_gc.h"
  18. #include "simd/simd_access_lanes.h"
  19. #include "simd/simd_bitmask_extracts.h"
  20. #include "simd/simd_bit_shifts.h"
  21. #include "simd/simd_bitwise_ops.h"
  22. #include "simd/simd_bool_reductions.h"
  23. #include "simd/simd_comparisons.h"
  24. #include "simd/simd_conversions.h"
  25. #include "simd/simd_construct_values.h"
  26. #include "simd/simd_conversions.h"
  27. #include "simd/simd_floating_point.h"
  28. #include "simd/simd_int_arith.h"
  29. #include "simd/simd_load_store.h"
  30. #include "simd/simd_sat_int_arith.h"
  31. #include "../aot/aot_runtime.h"
  32. #include "../interpreter/wasm_opcode.h"
  33. #include <errno.h>
  34. #if WASM_ENABLE_DEBUG_AOT != 0
  35. #include "debug/dwarf_extractor.h"
  36. #endif
  37. #if WASM_ENABLE_STRINGREF != 0
  38. #include "string_object.h"
  39. #include "aot_emit_stringref.h"
  40. #endif
  41. #define CHECK_BUF(buf, buf_end, length) \
  42. do { \
  43. if (buf + length > buf_end) { \
  44. aot_set_last_error("read leb failed: unexpected end."); \
  45. return false; \
  46. } \
  47. } while (0)
  48. static bool
  49. read_leb(const uint8 *buf, const uint8 *buf_end, uint32 *p_offset,
  50. uint32 maxbits, bool sign, uint64 *p_result)
  51. {
  52. uint64 result = 0;
  53. uint32 shift = 0;
  54. uint32 bcnt = 0;
  55. uint64 byte;
  56. while (true) {
  57. CHECK_BUF(buf, buf_end, 1);
  58. byte = buf[*p_offset];
  59. *p_offset += 1;
  60. result |= ((byte & 0x7f) << shift);
  61. shift += 7;
  62. if ((byte & 0x80) == 0) {
  63. break;
  64. }
  65. bcnt += 1;
  66. }
  67. if (bcnt > (maxbits + 6) / 7) {
  68. aot_set_last_error("read leb failed: "
  69. "integer representation too long");
  70. return false;
  71. }
  72. if (sign && (shift < maxbits) && (byte & 0x40)) {
  73. /* Sign extend */
  74. result |= (~((uint64)0)) << shift;
  75. }
  76. *p_result = result;
  77. return true;
  78. }
  79. /* NOLINTNEXTLINE */
  80. #define read_leb_generic(p, p_end, res, res_type, sign) \
  81. do { \
  82. uint32 off = 0; \
  83. uint64 res64; \
  84. if (!read_leb(p, p_end, &off, sizeof(res_type) << 3, sign, &res64)) \
  85. return false; \
  86. p += off; \
  87. res = (res_type)res64; \
  88. } while (0)
  89. /* NOLINTNEXTLINE */
  90. #define read_leb_int32(p, p_end, res) \
  91. read_leb_generic(p, p_end, res, int32, true)
  92. /* NOLINTNEXTLINE */
  93. #define read_leb_int64(p, p_end, res) \
  94. read_leb_generic(p, p_end, res, int64, true)
  95. /* NOLINTNEXTLINE */
  96. #define read_leb_uint32(p, p_end, res) \
  97. read_leb_generic(p, p_end, res, uint32, false)
  98. /* NOLINTNEXTLINE */
  99. #define read_leb_uint64(p, p_end, res) \
  100. read_leb_generic(p, p_end, res, uint64, false)
  101. /* NOLINTNEXTLINE */
  102. #if WASM_ENABLE_MEMORY64 != 0
  103. #define read_leb_mem_offset(p, p_end, res) \
  104. do { \
  105. if (IS_MEMORY64) { \
  106. read_leb_uint64(p, p_end, res); \
  107. } \
  108. else { \
  109. read_leb_uint32(p, p_end, res); \
  110. } \
  111. } while (0)
  112. #else
  113. #define read_leb_mem_offset read_leb_uint32
  114. #endif
  115. /**
  116. * Since wamrc uses a full feature Wasm loader,
  117. * add a post-validator here to run checks according
  118. * to options, like enable_tail_call, enable_ref_types,
  119. * and so on.
  120. */
  121. static bool
  122. aot_validate_wasm(AOTCompContext *comp_ctx)
  123. {
  124. if (!comp_ctx->enable_ref_types && !comp_ctx->enable_gc) {
  125. /* Doesn't support multiple tables unless enabling reference type */
  126. if (comp_ctx->comp_data->import_table_count
  127. + comp_ctx->comp_data->table_count
  128. > 1) {
  129. aot_set_last_error("multiple tables");
  130. return false;
  131. }
  132. }
  133. #if WASM_ENABLE_MEMORY64 != 0
  134. if (comp_ctx->pointer_size < sizeof(uint64) && IS_MEMORY64) {
  135. aot_set_last_error("Compiling wasm64 to 32bit platform is not allowed");
  136. return false;
  137. }
  138. #endif
  139. return true;
  140. }
  141. #define COMPILE_ATOMIC_RMW(OP, NAME) \
  142. case WASM_OP_ATOMIC_RMW_I32_##NAME: \
  143. bytes = 4; \
  144. op_type = VALUE_TYPE_I32; \
  145. goto OP_ATOMIC_##OP; \
  146. case WASM_OP_ATOMIC_RMW_I64_##NAME: \
  147. bytes = 8; \
  148. op_type = VALUE_TYPE_I64; \
  149. goto OP_ATOMIC_##OP; \
  150. case WASM_OP_ATOMIC_RMW_I32_##NAME##8_U: \
  151. bytes = 1; \
  152. op_type = VALUE_TYPE_I32; \
  153. goto OP_ATOMIC_##OP; \
  154. case WASM_OP_ATOMIC_RMW_I32_##NAME##16_U: \
  155. bytes = 2; \
  156. op_type = VALUE_TYPE_I32; \
  157. goto OP_ATOMIC_##OP; \
  158. case WASM_OP_ATOMIC_RMW_I64_##NAME##8_U: \
  159. bytes = 1; \
  160. op_type = VALUE_TYPE_I64; \
  161. goto OP_ATOMIC_##OP; \
  162. case WASM_OP_ATOMIC_RMW_I64_##NAME##16_U: \
  163. bytes = 2; \
  164. op_type = VALUE_TYPE_I64; \
  165. goto OP_ATOMIC_##OP; \
  166. case WASM_OP_ATOMIC_RMW_I64_##NAME##32_U: \
  167. bytes = 4; \
  168. op_type = VALUE_TYPE_I64; \
  169. OP_ATOMIC_##OP : bin_op = LLVMAtomicRMWBinOp##OP; \
  170. goto build_atomic_rmw;
  171. uint32
  172. offset_of_local_in_outs_area(AOTCompContext *comp_ctx, unsigned n)
  173. {
  174. AOTCompFrame *frame = comp_ctx->aot_frame;
  175. return frame->cur_frame_size + offset_of_local(comp_ctx, n);
  176. }
  177. static bool
  178. store_value(AOTCompContext *comp_ctx, LLVMValueRef value, uint8 value_type,
  179. LLVMValueRef cur_frame, uint32 offset)
  180. {
  181. LLVMValueRef value_offset, value_addr, value_ptr = NULL, res;
  182. LLVMTypeRef value_ptr_type = NULL;
  183. if (!(value_offset = I32_CONST(offset))) {
  184. aot_set_last_error("llvm build const failed");
  185. return false;
  186. }
  187. if (!(value_addr =
  188. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, cur_frame,
  189. &value_offset, 1, "value_addr"))) {
  190. aot_set_last_error("llvm build in bounds gep failed");
  191. return false;
  192. }
  193. switch (value_type) {
  194. case VALUE_TYPE_I32:
  195. value_ptr_type = INT32_PTR_TYPE;
  196. break;
  197. case VALUE_TYPE_I64:
  198. value_ptr_type = INT64_PTR_TYPE;
  199. break;
  200. case VALUE_TYPE_F32:
  201. value_ptr_type = F32_PTR_TYPE;
  202. break;
  203. case VALUE_TYPE_F64:
  204. value_ptr_type = F64_PTR_TYPE;
  205. break;
  206. case VALUE_TYPE_V128:
  207. value_ptr_type = V128_PTR_TYPE;
  208. break;
  209. #if WASM_ENABLE_GC != 0
  210. case VALUE_TYPE_GC_REF:
  211. value_ptr_type = GC_REF_PTR_TYPE;
  212. break;
  213. #endif
  214. default:
  215. bh_assert(0);
  216. break;
  217. }
  218. if (!(value_ptr = LLVMBuildBitCast(comp_ctx->builder, value_addr,
  219. value_ptr_type, "value_ptr"))) {
  220. aot_set_last_error("llvm build bit cast failed");
  221. return false;
  222. }
  223. if (!(res = LLVMBuildStore(comp_ctx->builder, value, value_ptr))) {
  224. aot_set_last_error("llvm build store failed");
  225. return false;
  226. }
  227. LLVMSetAlignment(res, 4);
  228. return true;
  229. }
  230. bool
  231. aot_frame_store_value(AOTCompContext *comp_ctx, LLVMValueRef value,
  232. uint8 value_type, LLVMValueRef cur_frame, uint32 offset)
  233. {
  234. return store_value(comp_ctx, value, value_type, cur_frame, offset);
  235. }
  236. static bool
  237. store_ref(AOTCompContext *comp_ctx, uint32 ref, LLVMValueRef cur_frame,
  238. uint32 offset, uint32 nbytes)
  239. {
  240. LLVMValueRef value_ref = NULL, value_offset, value_addr, res;
  241. if (!(value_offset = I32_CONST(offset))) {
  242. aot_set_last_error("llvm build const failed");
  243. return false;
  244. }
  245. if (!(value_addr =
  246. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, cur_frame,
  247. &value_offset, 1, "value_addr"))) {
  248. aot_set_last_error("llvm build in bounds gep failed");
  249. return false;
  250. }
  251. switch (nbytes) {
  252. case 1:
  253. if (!(value_ref = I8_CONST((uint8)ref))) {
  254. aot_set_last_error("llvm build const failed");
  255. }
  256. break;
  257. case 2:
  258. ref = (ref << 8) | ref;
  259. if (!(value_ref = LLVMConstInt(INT16_TYPE, (uint16)ref, false))) {
  260. aot_set_last_error("llvm build const failed");
  261. return false;
  262. }
  263. if (!(value_addr =
  264. LLVMBuildBitCast(comp_ctx->builder, value_addr,
  265. INT16_PTR_TYPE, "value_addr"))) {
  266. aot_set_last_error("llvm build bit cast failed");
  267. return false;
  268. }
  269. break;
  270. case 4:
  271. ref = (ref << 24) | (ref << 16) | (ref << 8) | ref;
  272. if (!(value_ref = I32_CONST(ref))) {
  273. aot_set_last_error("llvm build const failed");
  274. return false;
  275. }
  276. if (!(value_addr =
  277. LLVMBuildBitCast(comp_ctx->builder, value_addr,
  278. INT32_PTR_TYPE, "value_addr"))) {
  279. aot_set_last_error("llvm build bit cast failed");
  280. return false;
  281. }
  282. break;
  283. default:
  284. bh_assert(0);
  285. break;
  286. }
  287. if (!(res = LLVMBuildStore(comp_ctx->builder, value_ref, value_addr))) {
  288. aot_set_last_error("llvm build store failed");
  289. return false;
  290. }
  291. LLVMSetAlignment(res, 1);
  292. return true;
  293. }
  294. bool
  295. aot_gen_commit_values(AOTCompFrame *frame)
  296. {
  297. AOTCompContext *comp_ctx = frame->comp_ctx;
  298. AOTFuncContext *func_ctx = frame->func_ctx;
  299. AOTValueSlot *p, *end;
  300. LLVMValueRef value;
  301. uint32 n;
  302. /* First, commit reference flags
  303. * For LLVM JIT, iterate all local and stack ref flags
  304. * For AOT, ignore local(params + locals) ref flags */
  305. for (p = comp_ctx->is_jit_mode ? frame->lp
  306. : frame->lp + frame->max_local_cell_num;
  307. p < frame->sp; p++) {
  308. if (!p->dirty)
  309. continue;
  310. n = (uint32)(p - frame->lp);
  311. /* Commit reference flag */
  312. if (comp_ctx->enable_gc) {
  313. switch (p->type) {
  314. case VALUE_TYPE_I32:
  315. case VALUE_TYPE_F32:
  316. case VALUE_TYPE_I1:
  317. if (p->ref != p->committed_ref - 1) {
  318. if (!store_ref(comp_ctx, p->ref, func_ctx->cur_frame,
  319. offset_of_ref(comp_ctx, n), 1))
  320. return false;
  321. p->committed_ref = p->ref + 1;
  322. }
  323. break;
  324. case VALUE_TYPE_I64:
  325. case VALUE_TYPE_F64:
  326. bh_assert(p->ref == (p + 1)->ref);
  327. if (p->ref != p->committed_ref - 1
  328. || p->ref != (p + 1)->committed_ref - 1) {
  329. if (!store_ref(comp_ctx, p->ref, func_ctx->cur_frame,
  330. offset_of_ref(comp_ctx, n), 2))
  331. return false;
  332. p->committed_ref = (p + 1)->committed_ref = p->ref + 1;
  333. }
  334. p++;
  335. break;
  336. case VALUE_TYPE_V128:
  337. bh_assert(p->ref == (p + 1)->ref && p->ref == (p + 2)->ref
  338. && p->ref == (p + 3)->ref);
  339. if (p->ref != p->committed_ref - 1
  340. || p->ref != (p + 1)->committed_ref - 1
  341. || p->ref != (p + 2)->committed_ref - 1
  342. || p->ref != (p + 3)->committed_ref - 1) {
  343. if (!store_ref(comp_ctx, p->ref, func_ctx->cur_frame,
  344. offset_of_ref(comp_ctx, n), 4))
  345. return false;
  346. p->committed_ref = (p + 1)->committed_ref =
  347. (p + 2)->committed_ref = (p + 3)->committed_ref =
  348. p->ref + 1;
  349. }
  350. p += 3;
  351. break;
  352. case REF_TYPE_NULLFUNCREF:
  353. case REF_TYPE_NULLEXTERNREF:
  354. case REF_TYPE_NULLREF:
  355. case REF_TYPE_FUNCREF:
  356. case REF_TYPE_EXTERNREF:
  357. case REF_TYPE_ANYREF:
  358. case REF_TYPE_EQREF:
  359. case REF_TYPE_HT_NULLABLE:
  360. case REF_TYPE_HT_NON_NULLABLE:
  361. case REF_TYPE_I31REF:
  362. case REF_TYPE_STRUCTREF:
  363. case REF_TYPE_ARRAYREF:
  364. #if WASM_ENABLE_STRINGREF != 0
  365. case REF_TYPE_STRINGREF:
  366. case REF_TYPE_STRINGVIEWWTF8:
  367. case REF_TYPE_STRINGVIEWWTF16:
  368. case REF_TYPE_STRINGVIEWITER:
  369. #endif
  370. case VALUE_TYPE_GC_REF:
  371. if (comp_ctx->pointer_size == sizeof(uint64)) {
  372. bh_assert(p->ref == (p + 1)->ref);
  373. if (p->ref != p->committed_ref - 1
  374. || p->ref != (p + 1)->committed_ref - 1) {
  375. if (!store_ref(comp_ctx, p->ref,
  376. func_ctx->cur_frame,
  377. offset_of_ref(comp_ctx, n), 2))
  378. return false;
  379. p->committed_ref = (p + 1)->committed_ref =
  380. p->ref + 1;
  381. }
  382. p++;
  383. }
  384. else {
  385. if (p->ref != p->committed_ref - 1) {
  386. if (!store_ref(comp_ctx, p->ref,
  387. func_ctx->cur_frame,
  388. offset_of_ref(comp_ctx, n), 1))
  389. return false;
  390. p->committed_ref = p->ref + 1;
  391. }
  392. }
  393. break;
  394. default:
  395. bh_assert(0);
  396. break;
  397. }
  398. }
  399. }
  400. /* Second, commit all values */
  401. for (p = frame->lp; p < frame->sp; p++) {
  402. if (!p->dirty)
  403. continue;
  404. p->dirty = 0;
  405. n = (uint32)(p - frame->lp);
  406. /* Commit values */
  407. switch (p->type) {
  408. case VALUE_TYPE_I32:
  409. if (!store_value(comp_ctx, p->value, VALUE_TYPE_I32,
  410. func_ctx->cur_frame,
  411. offset_of_local(comp_ctx, n)))
  412. return false;
  413. break;
  414. case VALUE_TYPE_I64:
  415. (++p)->dirty = 0;
  416. if (!store_value(comp_ctx, p->value, VALUE_TYPE_I64,
  417. func_ctx->cur_frame,
  418. offset_of_local(comp_ctx, n)))
  419. return false;
  420. break;
  421. case VALUE_TYPE_F32:
  422. if (!store_value(comp_ctx, p->value, VALUE_TYPE_F32,
  423. func_ctx->cur_frame,
  424. offset_of_local(comp_ctx, n)))
  425. return false;
  426. break;
  427. case VALUE_TYPE_F64:
  428. (++p)->dirty = 0;
  429. if (!store_value(comp_ctx, p->value, VALUE_TYPE_F64,
  430. func_ctx->cur_frame,
  431. offset_of_local(comp_ctx, n)))
  432. return false;
  433. break;
  434. case VALUE_TYPE_V128:
  435. (++p)->dirty = 0;
  436. (++p)->dirty = 0;
  437. (++p)->dirty = 0;
  438. if (!store_value(comp_ctx, p->value, VALUE_TYPE_V128,
  439. func_ctx->cur_frame,
  440. offset_of_local(comp_ctx, n)))
  441. return false;
  442. break;
  443. case VALUE_TYPE_I1:
  444. if (!(value = LLVMBuildZExt(comp_ctx->builder, p->value,
  445. I32_TYPE, "i32_val"))) {
  446. aot_set_last_error("llvm build bit cast failed");
  447. return false;
  448. }
  449. if (!store_value(comp_ctx, value, VALUE_TYPE_I32,
  450. func_ctx->cur_frame,
  451. offset_of_local(comp_ctx, n)))
  452. return false;
  453. break;
  454. case VALUE_TYPE_FUNCREF:
  455. case VALUE_TYPE_EXTERNREF:
  456. if (comp_ctx->enable_ref_types) {
  457. if (!store_value(comp_ctx, p->value, VALUE_TYPE_I32,
  458. func_ctx->cur_frame,
  459. offset_of_local(comp_ctx, n)))
  460. return false;
  461. }
  462. #if WASM_ENABLE_GC != 0
  463. else if (comp_ctx->enable_gc) {
  464. if (comp_ctx->pointer_size == sizeof(uint64))
  465. (++p)->dirty = 0;
  466. if (!store_value(comp_ctx, p->value, VALUE_TYPE_GC_REF,
  467. func_ctx->cur_frame,
  468. offset_of_local(comp_ctx, n)))
  469. return false;
  470. }
  471. #endif
  472. else {
  473. bh_assert(0);
  474. }
  475. break;
  476. #if WASM_ENABLE_GC != 0
  477. case REF_TYPE_NULLFUNCREF:
  478. case REF_TYPE_NULLEXTERNREF:
  479. case REF_TYPE_NULLREF:
  480. /* case REF_TYPE_FUNCREF: */
  481. /* case REF_TYPE_EXTERNREF: */
  482. case REF_TYPE_ANYREF:
  483. case REF_TYPE_EQREF:
  484. case REF_TYPE_HT_NULLABLE:
  485. case REF_TYPE_HT_NON_NULLABLE:
  486. case REF_TYPE_I31REF:
  487. case REF_TYPE_STRUCTREF:
  488. case REF_TYPE_ARRAYREF:
  489. case VALUE_TYPE_GC_REF:
  490. if (comp_ctx->pointer_size == sizeof(uint64))
  491. (++p)->dirty = 0;
  492. if (!store_value(comp_ctx, p->value, VALUE_TYPE_GC_REF,
  493. func_ctx->cur_frame,
  494. offset_of_local(comp_ctx, n)))
  495. return false;
  496. break;
  497. #endif
  498. default:
  499. bh_assert(0);
  500. break;
  501. }
  502. }
  503. if (comp_ctx->enable_gc) {
  504. end = frame->lp + frame->max_local_cell_num + frame->max_stack_cell_num;
  505. /* Clear reference flags for unused stack slots. */
  506. for (p = frame->sp; p < end; p++) {
  507. bh_assert(!p->ref);
  508. n = (uint32)(p - frame->lp);
  509. /* Commit reference flag. */
  510. if (p->ref != p->committed_ref - 1) {
  511. if (!store_ref(comp_ctx, p->ref, func_ctx->cur_frame,
  512. offset_of_ref(comp_ctx, n), 1))
  513. return false;
  514. p->committed_ref = 1 + p->ref;
  515. }
  516. }
  517. }
  518. return true;
  519. }
  520. bool
  521. aot_gen_commit_sp_ip(AOTCompFrame *frame, bool commit_sp, bool commit_ip)
  522. {
  523. AOTCompContext *comp_ctx = frame->comp_ctx;
  524. AOTFuncContext *func_ctx = frame->func_ctx;
  525. LLVMValueRef cur_frame = func_ctx->cur_frame;
  526. LLVMValueRef value_offset, value_addr, value_ptr, value;
  527. LLVMTypeRef int8_ptr_ptr_type;
  528. uint32 offset_ip, offset_sp, n;
  529. bool is_64bit = (comp_ctx->pointer_size == sizeof(uint64)) ? true : false;
  530. const AOTValueSlot *sp = frame->sp;
  531. const uint8 *ip = frame->frame_ip;
  532. if (!comp_ctx->is_jit_mode) {
  533. offset_ip = frame->comp_ctx->pointer_size * 4;
  534. offset_sp = frame->comp_ctx->pointer_size * 5;
  535. }
  536. else {
  537. offset_ip = offsetof(WASMInterpFrame, ip);
  538. offset_sp = offsetof(WASMInterpFrame, sp);
  539. }
  540. if (commit_ip) {
  541. if (!(value_offset = I32_CONST(offset_ip))) {
  542. aot_set_last_error("llvm build const failed");
  543. return false;
  544. }
  545. if (!(value_addr =
  546. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, cur_frame,
  547. &value_offset, 1, "ip_addr"))) {
  548. aot_set_last_error("llvm build in bounds gep failed");
  549. return false;
  550. }
  551. if (!(value_ptr = LLVMBuildBitCast(
  552. comp_ctx->builder, value_addr,
  553. is_64bit ? INT64_PTR_TYPE : INT32_PTR_TYPE, "ip_ptr"))) {
  554. aot_set_last_error("llvm build bit cast failed");
  555. return false;
  556. }
  557. if (!comp_ctx->is_jit_mode) {
  558. WASMModule *module = comp_ctx->comp_data->wasm_module;
  559. if (is_64bit)
  560. value = I64_CONST((uint64)(uintptr_t)(ip - module->load_addr));
  561. else
  562. value = I32_CONST((uint32)(uintptr_t)(ip - module->load_addr));
  563. }
  564. else {
  565. if (is_64bit)
  566. value = I64_CONST((uint64)(uintptr_t)ip);
  567. else
  568. value = I32_CONST((uint32)(uintptr_t)ip);
  569. }
  570. if (!value) {
  571. aot_set_last_error("llvm build const failed");
  572. return false;
  573. }
  574. if (!LLVMBuildStore(comp_ctx->builder, value, value_ptr)) {
  575. aot_set_last_error("llvm build store failed");
  576. return false;
  577. }
  578. }
  579. if (commit_sp) {
  580. n = (uint32)(sp - frame->lp);
  581. value = I32_CONST(offset_of_local(comp_ctx, n));
  582. if (!value) {
  583. aot_set_last_error("llvm build const failed");
  584. return false;
  585. }
  586. if (!(value = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
  587. cur_frame, &value, 1, "sp"))) {
  588. aot_set_last_error("llvm build in bounds gep failed");
  589. return false;
  590. }
  591. if (!(value_offset = I32_CONST(offset_sp))) {
  592. aot_set_last_error("llvm build const failed");
  593. return false;
  594. }
  595. if (!(value_addr =
  596. LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE, cur_frame,
  597. &value_offset, 1, "sp_addr"))) {
  598. aot_set_last_error("llvm build in bounds gep failed");
  599. return false;
  600. }
  601. if (!(int8_ptr_ptr_type = LLVMPointerType(INT8_PTR_TYPE, 0))) {
  602. aot_set_last_error("llvm build pointer type failed");
  603. return false;
  604. }
  605. if (!(value_ptr = LLVMBuildBitCast(comp_ctx->builder, value_addr,
  606. int8_ptr_ptr_type, "sp_ptr"))) {
  607. aot_set_last_error("llvm build bit cast failed");
  608. return false;
  609. }
  610. if (!LLVMBuildStore(comp_ctx->builder, value, value_ptr)) {
  611. aot_set_last_error("llvm build store failed");
  612. return false;
  613. }
  614. }
  615. return true;
  616. }
  617. static uint32
  618. get_cur_frame_size(const AOTCompContext *comp_ctx, uint32 max_local_cell_num,
  619. uint32 max_stack_cell_num)
  620. {
  621. uint32 all_cell_num = max_local_cell_num + max_stack_cell_num;
  622. uint32 frame_size;
  623. if (!comp_ctx->is_jit_mode) {
  624. /* Refer to aot_alloc_frame */
  625. if (!comp_ctx->enable_gc)
  626. frame_size = comp_ctx->pointer_size
  627. * (offsetof(AOTFrame, lp) / sizeof(uintptr_t))
  628. + all_cell_num * 4;
  629. else
  630. frame_size = comp_ctx->pointer_size
  631. * (offsetof(AOTFrame, lp) / sizeof(uintptr_t))
  632. + align_uint(all_cell_num * 5, 4);
  633. }
  634. else {
  635. /* Refer to wasm_interp_interp_frame_size */
  636. if (!comp_ctx->enable_gc)
  637. frame_size = offsetof(WASMInterpFrame, lp) + all_cell_num * 4;
  638. else
  639. frame_size =
  640. offsetof(WASMInterpFrame, lp) + align_uint(all_cell_num * 5, 4);
  641. }
  642. return frame_size;
  643. }
  644. static bool
  645. init_comp_frame(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
  646. uint32 func_idx)
  647. {
  648. AOTCompFrame *aot_frame;
  649. AOTFunc *aot_func = func_ctx->aot_func;
  650. AOTFuncType *func_type = aot_func->func_type;
  651. AOTBlock *block = func_ctx->block_stack.block_list_end;
  652. LLVMValueRef local_value;
  653. uint32 max_local_cell_num =
  654. aot_func->param_cell_num + aot_func->local_cell_num;
  655. uint32 max_stack_cell_num = aot_func->max_stack_cell_num;
  656. uint32 all_cell_num = max_local_cell_num + max_stack_cell_num;
  657. uint32 i, n;
  658. uint64 total_size;
  659. uint8 local_type;
  660. /* Free aot_frame if it was allocated previously for
  661. compiling other functions */
  662. if (comp_ctx->aot_frame) {
  663. wasm_runtime_free(comp_ctx->aot_frame);
  664. comp_ctx->aot_frame = NULL;
  665. }
  666. /* Allocate extra 2 cells since some operations may push more
  667. operands than the number calculated in wasm loader, such as
  668. PUSH_F64(F64_CONST(1.0)) in aot_compile_op_f64_promote_f32 */
  669. all_cell_num += 2;
  670. total_size = offsetof(AOTCompFrame, lp)
  671. + (uint64)sizeof(AOTValueSlot) * all_cell_num;
  672. if (total_size > UINT32_MAX
  673. || !(comp_ctx->aot_frame = aot_frame =
  674. wasm_runtime_malloc((uint32)total_size))) {
  675. aot_set_last_error("allocate memory failed.");
  676. return false;
  677. }
  678. memset(aot_frame, 0, (uint32)total_size);
  679. aot_frame->comp_ctx = comp_ctx;
  680. aot_frame->func_ctx = func_ctx;
  681. aot_frame->max_local_cell_num = max_local_cell_num;
  682. aot_frame->max_stack_cell_num = max_stack_cell_num;
  683. aot_frame->cur_frame_size =
  684. get_cur_frame_size(comp_ctx, max_local_cell_num, max_stack_cell_num);
  685. aot_frame->sp = aot_frame->lp + max_local_cell_num;
  686. /* Init the frame_sp_begin and frame_sp_max_reached
  687. of the function block */
  688. block->frame_sp_begin = block->frame_sp_max_reached = aot_frame->sp;
  689. n = 0;
  690. /* Set all params dirty since they were set to llvm value but
  691. haven't been committed to the AOT/JIT stack frame */
  692. for (i = 0; i < func_type->param_count; i++) {
  693. local_type = func_type->types[i];
  694. local_value = LLVMGetParam(func_ctx->func, i + 1);
  695. switch (local_type) {
  696. case VALUE_TYPE_I32:
  697. set_local_i32(comp_ctx->aot_frame, n, local_value);
  698. n++;
  699. break;
  700. case VALUE_TYPE_I64:
  701. set_local_i64(comp_ctx->aot_frame, n, local_value);
  702. n += 2;
  703. break;
  704. case VALUE_TYPE_F32:
  705. set_local_f32(comp_ctx->aot_frame, n, local_value);
  706. n++;
  707. break;
  708. case VALUE_TYPE_F64:
  709. set_local_f64(comp_ctx->aot_frame, n, local_value);
  710. n += 2;
  711. break;
  712. case VALUE_TYPE_V128:
  713. set_local_v128(comp_ctx->aot_frame, n, local_value);
  714. n += 4;
  715. break;
  716. case VALUE_TYPE_FUNCREF:
  717. case VALUE_TYPE_EXTERNREF:
  718. {
  719. if (comp_ctx->enable_ref_types) {
  720. set_local_ref(comp_ctx->aot_frame, n, local_value,
  721. local_type);
  722. n++;
  723. }
  724. #if WASM_ENABLE_GC != 0
  725. else if (comp_ctx->enable_gc) {
  726. set_local_gc_ref(comp_ctx->aot_frame, n, local_value,
  727. VALUE_TYPE_GC_REF);
  728. n += comp_ctx->pointer_size / sizeof(uint32);
  729. }
  730. #endif
  731. else {
  732. bh_assert(0);
  733. }
  734. break;
  735. }
  736. #if WASM_ENABLE_GC != 0
  737. case REF_TYPE_NULLFUNCREF:
  738. case REF_TYPE_NULLEXTERNREF:
  739. case REF_TYPE_NULLREF:
  740. /* case REF_TYPE_FUNCREF: */
  741. /* case REF_TYPE_EXTERNREF: */
  742. case REF_TYPE_ANYREF:
  743. case REF_TYPE_EQREF:
  744. case REF_TYPE_HT_NULLABLE:
  745. case REF_TYPE_HT_NON_NULLABLE:
  746. case REF_TYPE_I31REF:
  747. case REF_TYPE_STRUCTREF:
  748. case REF_TYPE_ARRAYREF:
  749. #if WASM_ENABLE_STRINGREF != 0
  750. case REF_TYPE_STRINGREF:
  751. case REF_TYPE_STRINGVIEWWTF8:
  752. case REF_TYPE_STRINGVIEWWTF16:
  753. case REF_TYPE_STRINGVIEWITER:
  754. #endif
  755. bh_assert(comp_ctx->enable_gc);
  756. set_local_gc_ref(comp_ctx->aot_frame, n, local_value,
  757. VALUE_TYPE_GC_REF);
  758. n += comp_ctx->pointer_size / sizeof(uint32);
  759. break;
  760. #endif
  761. default:
  762. bh_assert(0);
  763. break;
  764. }
  765. }
  766. /* TODO: re-calculate param_cell_num according to the build target
  767. after creating comp_ctx */
  768. /* bh_assert(n == aot_func->param_cell_num); */
  769. /* Set all locals dirty since they were set to llvm value but
  770. haven't been committed to the AOT/JIT stack frame */
  771. for (i = 0; i < aot_func->local_count; i++) {
  772. local_type = aot_func->local_types_wp[i];
  773. switch (local_type) {
  774. case VALUE_TYPE_I32:
  775. set_local_i32(comp_ctx->aot_frame, n, I32_ZERO);
  776. n++;
  777. break;
  778. case VALUE_TYPE_I64:
  779. set_local_i64(comp_ctx->aot_frame, n, I64_ZERO);
  780. n += 2;
  781. break;
  782. case VALUE_TYPE_F32:
  783. set_local_f32(comp_ctx->aot_frame, n, F32_ZERO);
  784. n++;
  785. break;
  786. case VALUE_TYPE_F64:
  787. set_local_f64(comp_ctx->aot_frame, n, F64_ZERO);
  788. n += 2;
  789. break;
  790. case VALUE_TYPE_V128:
  791. set_local_v128(comp_ctx->aot_frame, n, V128_f64x2_ZERO);
  792. n += 4;
  793. break;
  794. case VALUE_TYPE_FUNCREF:
  795. case VALUE_TYPE_EXTERNREF:
  796. {
  797. if (comp_ctx->enable_ref_types) {
  798. set_local_ref(comp_ctx->aot_frame, n, I32_ZERO, local_type);
  799. n++;
  800. }
  801. #if WASM_ENABLE_GC != 0
  802. else if (comp_ctx->enable_gc) {
  803. set_local_gc_ref(comp_ctx->aot_frame, n, GC_REF_NULL,
  804. VALUE_TYPE_GC_REF);
  805. n += comp_ctx->pointer_size / sizeof(uint32);
  806. }
  807. #endif
  808. else {
  809. bh_assert(0);
  810. }
  811. break;
  812. }
  813. #if WASM_ENABLE_GC != 0
  814. case REF_TYPE_NULLFUNCREF:
  815. case REF_TYPE_NULLEXTERNREF:
  816. case REF_TYPE_NULLREF:
  817. /* case REF_TYPE_FUNCREF: */
  818. /* case REF_TYPE_EXTERNREF: */
  819. case REF_TYPE_ANYREF:
  820. case REF_TYPE_EQREF:
  821. case REF_TYPE_HT_NULLABLE:
  822. case REF_TYPE_HT_NON_NULLABLE:
  823. case REF_TYPE_I31REF:
  824. case REF_TYPE_STRUCTREF:
  825. case REF_TYPE_ARRAYREF:
  826. #if WASM_ENABLE_STRINGREF != 0
  827. case REF_TYPE_STRINGREF:
  828. case REF_TYPE_STRINGVIEWWTF8:
  829. case REF_TYPE_STRINGVIEWWTF16:
  830. case REF_TYPE_STRINGVIEWITER:
  831. #endif
  832. bh_assert(comp_ctx->enable_gc);
  833. set_local_gc_ref(comp_ctx->aot_frame, n, GC_REF_NULL,
  834. VALUE_TYPE_GC_REF);
  835. n += comp_ctx->pointer_size / sizeof(uint32);
  836. break;
  837. #endif
  838. default:
  839. bh_assert(0);
  840. break;
  841. }
  842. }
  843. /* TODO: re-calculate local_cell_num according to the build target
  844. after creating comp_ctx */
  845. /* bh_assert(n == aot_func->param_cell_num + aot_func->local_cell_num); */
  846. /* No need to initialize aot_frame all cells' committed_ref flags
  847. and all stack cells' ref flags since they have been initialized
  848. as 0 (uncommitted and not-reference) by the memset above */
  849. return true;
  850. }
  851. static bool
  852. aot_compile_func(AOTCompContext *comp_ctx, uint32 func_index)
  853. {
  854. AOTFuncContext *func_ctx = comp_ctx->func_ctxes[func_index];
  855. uint8 *frame_ip = func_ctx->aot_func->code, opcode, *p_f32, *p_f64;
  856. uint8 *frame_ip_end = frame_ip + func_ctx->aot_func->code_size;
  857. uint8 *param_types = NULL;
  858. uint8 *result_types = NULL;
  859. uint8 value_type;
  860. uint16 param_count;
  861. uint16 result_count;
  862. uint32 br_depth, *br_depths, br_count;
  863. uint32 func_idx, type_idx, mem_idx, local_idx, global_idx, i;
  864. uint32 bytes = 4, align;
  865. mem_offset_t offset;
  866. uint32 type_index;
  867. bool sign = true;
  868. int32 i32_const;
  869. int64 i64_const;
  870. float32 f32_const;
  871. float64 f64_const;
  872. AOTFuncType *func_type = NULL;
  873. #if WASM_ENABLE_DEBUG_AOT != 0
  874. LLVMMetadataRef location;
  875. #endif
  876. if (comp_ctx->enable_aux_stack_frame) {
  877. if (!init_comp_frame(comp_ctx, func_ctx, func_index)) {
  878. return false;
  879. }
  880. }
  881. /* Start to translate the opcodes */
  882. LLVMPositionBuilderAtEnd(
  883. comp_ctx->builder,
  884. func_ctx->block_stack.block_list_head->llvm_entry_block);
  885. while (frame_ip < frame_ip_end) {
  886. opcode = *frame_ip++;
  887. if (comp_ctx->aot_frame) {
  888. comp_ctx->aot_frame->frame_ip = frame_ip - 1;
  889. }
  890. #if WASM_ENABLE_DEBUG_AOT != 0
  891. location = dwarf_gen_location(
  892. comp_ctx, func_ctx,
  893. (frame_ip - 1) - comp_ctx->comp_data->wasm_module->buf_code);
  894. if (location != NULL) {
  895. LLVMSetCurrentDebugLocation2(comp_ctx->builder, location);
  896. }
  897. #endif
  898. switch (opcode) {
  899. case WASM_OP_UNREACHABLE:
  900. if (!aot_compile_op_unreachable(comp_ctx, func_ctx, &frame_ip))
  901. return false;
  902. break;
  903. case WASM_OP_NOP:
  904. break;
  905. case WASM_OP_BLOCK:
  906. case WASM_OP_LOOP:
  907. case WASM_OP_IF:
  908. {
  909. value_type = *frame_ip++;
  910. if (value_type == VALUE_TYPE_I32 || value_type == VALUE_TYPE_I64
  911. || value_type == VALUE_TYPE_F32
  912. || value_type == VALUE_TYPE_F64
  913. || value_type == VALUE_TYPE_V128
  914. || value_type == VALUE_TYPE_VOID
  915. || (comp_ctx->enable_ref_types
  916. && (value_type == VALUE_TYPE_FUNCREF
  917. || value_type == VALUE_TYPE_EXTERNREF))
  918. || (comp_ctx->enable_gc /* single byte type */
  919. && aot_is_type_gc_reftype(value_type))) {
  920. param_count = 0;
  921. param_types = NULL;
  922. if (value_type == VALUE_TYPE_VOID) {
  923. result_count = 0;
  924. result_types = NULL;
  925. }
  926. else {
  927. if (comp_ctx->enable_gc
  928. && aot_is_type_gc_reftype(value_type))
  929. value_type = VALUE_TYPE_GC_REF;
  930. result_count = 1;
  931. result_types = &value_type;
  932. }
  933. }
  934. else {
  935. frame_ip--;
  936. read_leb_int32(frame_ip, frame_ip_end, type_index);
  937. /* type index was checked in wasm loader */
  938. bh_assert(type_index < comp_ctx->comp_data->type_count);
  939. func_type =
  940. (AOTFuncType *)comp_ctx->comp_data->types[type_index];
  941. param_count = func_type->param_count;
  942. param_types = func_type->types;
  943. result_count = func_type->result_count;
  944. result_types = func_type->types + param_count;
  945. }
  946. if (!aot_compile_op_block(
  947. comp_ctx, func_ctx, &frame_ip, frame_ip_end,
  948. (uint32)(LABEL_TYPE_BLOCK + opcode - WASM_OP_BLOCK),
  949. param_count, param_types, result_count, result_types))
  950. return false;
  951. break;
  952. }
  953. case EXT_OP_BLOCK:
  954. case EXT_OP_LOOP:
  955. case EXT_OP_IF:
  956. {
  957. read_leb_int32(frame_ip, frame_ip_end, type_index);
  958. /* type index was checked in wasm loader */
  959. bh_assert(type_index < comp_ctx->comp_data->type_count);
  960. func_type =
  961. (AOTFuncType *)comp_ctx->comp_data->types[type_index];
  962. param_count = func_type->param_count;
  963. param_types = func_type->types;
  964. result_count = func_type->result_count;
  965. result_types = func_type->types + param_count;
  966. if (!aot_compile_op_block(
  967. comp_ctx, func_ctx, &frame_ip, frame_ip_end,
  968. (uint32)(LABEL_TYPE_BLOCK + opcode - EXT_OP_BLOCK),
  969. param_count, param_types, result_count, result_types))
  970. return false;
  971. break;
  972. }
  973. case WASM_OP_ELSE:
  974. if (!aot_compile_op_else(comp_ctx, func_ctx, &frame_ip))
  975. return false;
  976. break;
  977. case WASM_OP_END:
  978. if (!aot_compile_op_end(comp_ctx, func_ctx, &frame_ip))
  979. return false;
  980. break;
  981. case WASM_OP_BR:
  982. {
  983. read_leb_uint32(frame_ip, frame_ip_end, br_depth);
  984. if (!aot_compile_op_br(comp_ctx, func_ctx, br_depth, &frame_ip))
  985. return false;
  986. break;
  987. }
  988. case WASM_OP_BR_IF:
  989. {
  990. read_leb_uint32(frame_ip, frame_ip_end, br_depth);
  991. if (!aot_compile_op_br_if(comp_ctx, func_ctx, br_depth,
  992. &frame_ip))
  993. return false;
  994. break;
  995. }
  996. case WASM_OP_BR_TABLE:
  997. {
  998. read_leb_uint32(frame_ip, frame_ip_end, br_count);
  999. if (!(br_depths = wasm_runtime_malloc((uint32)sizeof(uint32)
  1000. * (br_count + 1)))) {
  1001. aot_set_last_error("allocate memory failed.");
  1002. goto fail;
  1003. }
  1004. #if WASM_ENABLE_FAST_INTERP != 0
  1005. for (i = 0; i <= br_count; i++)
  1006. read_leb_uint32(frame_ip, frame_ip_end, br_depths[i]);
  1007. #else
  1008. for (i = 0; i <= br_count; i++)
  1009. br_depths[i] = *frame_ip++;
  1010. #endif
  1011. if (!aot_compile_op_br_table(comp_ctx, func_ctx, br_depths,
  1012. br_count, &frame_ip)) {
  1013. wasm_runtime_free(br_depths);
  1014. return false;
  1015. }
  1016. wasm_runtime_free(br_depths);
  1017. break;
  1018. }
  1019. #if WASM_ENABLE_FAST_INTERP == 0
  1020. case EXT_OP_BR_TABLE_CACHE:
  1021. {
  1022. BrTableCache *node = bh_list_first_elem(
  1023. comp_ctx->comp_data->wasm_module->br_table_cache_list);
  1024. BrTableCache *node_next;
  1025. const uint8 *frame_ip_org = frame_ip - 1;
  1026. read_leb_uint32(frame_ip, frame_ip_end, br_count);
  1027. while (node) {
  1028. node_next = bh_list_elem_next(node);
  1029. if (node->br_table_op_addr == frame_ip_org) {
  1030. br_depths = node->br_depths;
  1031. if (!aot_compile_op_br_table(comp_ctx, func_ctx,
  1032. br_depths, br_count,
  1033. &frame_ip)) {
  1034. return false;
  1035. }
  1036. break;
  1037. }
  1038. node = node_next;
  1039. }
  1040. bh_assert(node);
  1041. break;
  1042. }
  1043. #endif
  1044. case WASM_OP_RETURN:
  1045. if (!aot_compile_op_return(comp_ctx, func_ctx, &frame_ip))
  1046. return false;
  1047. break;
  1048. case WASM_OP_CALL:
  1049. {
  1050. read_leb_uint32(frame_ip, frame_ip_end, func_idx);
  1051. if (!aot_compile_op_call(comp_ctx, func_ctx, func_idx, false))
  1052. return false;
  1053. break;
  1054. }
  1055. case WASM_OP_CALL_INDIRECT:
  1056. {
  1057. uint32 tbl_idx;
  1058. read_leb_uint32(frame_ip, frame_ip_end, type_idx);
  1059. if (comp_ctx->enable_gc || comp_ctx->enable_ref_types) {
  1060. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  1061. }
  1062. else {
  1063. frame_ip++;
  1064. tbl_idx = 0;
  1065. }
  1066. if (!aot_compile_op_call_indirect(comp_ctx, func_ctx, type_idx,
  1067. tbl_idx))
  1068. return false;
  1069. break;
  1070. }
  1071. #if WASM_ENABLE_TAIL_CALL != 0
  1072. case WASM_OP_RETURN_CALL:
  1073. {
  1074. if (!comp_ctx->enable_tail_call) {
  1075. aot_set_last_error("unsupported opcode");
  1076. return false;
  1077. }
  1078. read_leb_uint32(frame_ip, frame_ip_end, func_idx);
  1079. if (!aot_compile_op_call(comp_ctx, func_ctx, func_idx, true))
  1080. return false;
  1081. if (!aot_compile_op_return(comp_ctx, func_ctx, &frame_ip))
  1082. return false;
  1083. break;
  1084. }
  1085. case WASM_OP_RETURN_CALL_INDIRECT:
  1086. {
  1087. uint32 tbl_idx;
  1088. if (!comp_ctx->enable_tail_call) {
  1089. aot_set_last_error("unsupported opcode");
  1090. return false;
  1091. }
  1092. read_leb_uint32(frame_ip, frame_ip_end, type_idx);
  1093. if (comp_ctx->enable_gc || comp_ctx->enable_ref_types) {
  1094. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  1095. }
  1096. else {
  1097. frame_ip++;
  1098. tbl_idx = 0;
  1099. }
  1100. if (!aot_compile_op_call_indirect(comp_ctx, func_ctx, type_idx,
  1101. tbl_idx))
  1102. return false;
  1103. if (!aot_compile_op_return(comp_ctx, func_ctx, &frame_ip))
  1104. return false;
  1105. break;
  1106. }
  1107. #endif /* end of WASM_ENABLE_TAIL_CALL */
  1108. case WASM_OP_DROP:
  1109. if (!aot_compile_op_drop(comp_ctx, func_ctx, true))
  1110. return false;
  1111. break;
  1112. case WASM_OP_DROP_64:
  1113. if (!aot_compile_op_drop(comp_ctx, func_ctx, false))
  1114. return false;
  1115. break;
  1116. case WASM_OP_SELECT:
  1117. if (!aot_compile_op_select(comp_ctx, func_ctx, true))
  1118. return false;
  1119. break;
  1120. case WASM_OP_SELECT_64:
  1121. if (!aot_compile_op_select(comp_ctx, func_ctx, false))
  1122. return false;
  1123. break;
  1124. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  1125. case WASM_OP_SELECT_T:
  1126. {
  1127. uint32 vec_len;
  1128. if (!comp_ctx->enable_ref_types && !comp_ctx->enable_gc) {
  1129. goto unsupport_gc_and_ref_types;
  1130. }
  1131. read_leb_uint32(frame_ip, frame_ip_end, vec_len);
  1132. bh_assert(vec_len == 1);
  1133. (void)vec_len;
  1134. type_idx = *frame_ip++;
  1135. if (!aot_compile_op_select(
  1136. comp_ctx, func_ctx,
  1137. (type_idx != VALUE_TYPE_I64)
  1138. && (type_idx != VALUE_TYPE_F64)
  1139. #if WASM_ENABLE_GC != 0
  1140. && !(comp_ctx->enable_gc
  1141. && comp_ctx->pointer_size == sizeof(uint64)
  1142. && wasm_is_type_reftype(type_idx))
  1143. #endif
  1144. ))
  1145. return false;
  1146. break;
  1147. }
  1148. case WASM_OP_TABLE_GET:
  1149. {
  1150. uint32 tbl_idx;
  1151. if (!comp_ctx->enable_ref_types && !comp_ctx->enable_gc) {
  1152. goto unsupport_gc_and_ref_types;
  1153. }
  1154. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  1155. if (!aot_compile_op_table_get(comp_ctx, func_ctx, tbl_idx))
  1156. return false;
  1157. break;
  1158. }
  1159. case WASM_OP_TABLE_SET:
  1160. {
  1161. uint32 tbl_idx;
  1162. if (!comp_ctx->enable_ref_types && !comp_ctx->enable_gc) {
  1163. goto unsupport_gc_and_ref_types;
  1164. }
  1165. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  1166. if (!aot_compile_op_table_set(comp_ctx, func_ctx, tbl_idx))
  1167. return false;
  1168. break;
  1169. }
  1170. case WASM_OP_REF_NULL:
  1171. {
  1172. uint32 type;
  1173. if (!comp_ctx->enable_ref_types && !comp_ctx->enable_gc) {
  1174. goto unsupport_gc_and_ref_types;
  1175. }
  1176. read_leb_uint32(frame_ip, frame_ip_end, type);
  1177. if (!aot_compile_op_ref_null(comp_ctx, func_ctx))
  1178. return false;
  1179. (void)type;
  1180. break;
  1181. }
  1182. case WASM_OP_REF_IS_NULL:
  1183. {
  1184. if (!comp_ctx->enable_ref_types && !comp_ctx->enable_gc) {
  1185. goto unsupport_gc_and_ref_types;
  1186. }
  1187. if (!aot_compile_op_ref_is_null(comp_ctx, func_ctx))
  1188. return false;
  1189. break;
  1190. }
  1191. case WASM_OP_REF_FUNC:
  1192. {
  1193. if (!comp_ctx->enable_ref_types && !comp_ctx->enable_gc) {
  1194. goto unsupport_gc_and_ref_types;
  1195. }
  1196. read_leb_uint32(frame_ip, frame_ip_end, func_idx);
  1197. if (!aot_compile_op_ref_func(comp_ctx, func_ctx, func_idx))
  1198. return false;
  1199. break;
  1200. }
  1201. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  1202. #if WASM_ENABLE_GC != 0
  1203. case WASM_OP_CALL_REF:
  1204. {
  1205. if (!comp_ctx->enable_gc) {
  1206. goto unsupport_gc;
  1207. }
  1208. read_leb_uint32(frame_ip, frame_ip_end, type_idx);
  1209. if (!aot_compile_op_call_ref(comp_ctx, func_ctx, type_idx,
  1210. false))
  1211. return false;
  1212. break;
  1213. }
  1214. case WASM_OP_RETURN_CALL_REF:
  1215. {
  1216. if (!comp_ctx->enable_gc) {
  1217. goto unsupport_gc;
  1218. }
  1219. read_leb_uint32(frame_ip, frame_ip_end, type_idx);
  1220. if (!aot_compile_op_call_ref(comp_ctx, func_ctx, type_idx,
  1221. true))
  1222. return false;
  1223. if (!aot_compile_op_return(comp_ctx, func_ctx, &frame_ip))
  1224. return false;
  1225. break;
  1226. }
  1227. case WASM_OP_REF_EQ:
  1228. if (!comp_ctx->enable_gc) {
  1229. goto unsupport_gc;
  1230. }
  1231. if (!aot_compile_op_ref_eq(comp_ctx, func_ctx))
  1232. return false;
  1233. break;
  1234. case WASM_OP_REF_AS_NON_NULL:
  1235. if (!comp_ctx->enable_gc) {
  1236. goto unsupport_gc;
  1237. }
  1238. if (!aot_compile_op_ref_as_non_null(comp_ctx, func_ctx))
  1239. return false;
  1240. break;
  1241. case WASM_OP_BR_ON_NULL:
  1242. {
  1243. if (!comp_ctx->enable_gc) {
  1244. goto unsupport_gc;
  1245. }
  1246. read_leb_uint32(frame_ip, frame_ip_end, br_depth);
  1247. if (!aot_compile_op_br_on_null(comp_ctx, func_ctx, br_depth,
  1248. &frame_ip))
  1249. return false;
  1250. break;
  1251. }
  1252. case WASM_OP_BR_ON_NON_NULL:
  1253. {
  1254. if (!comp_ctx->enable_gc) {
  1255. goto unsupport_gc;
  1256. }
  1257. read_leb_uint32(frame_ip, frame_ip_end, br_depth);
  1258. if (!aot_compile_op_br_on_non_null(comp_ctx, func_ctx, br_depth,
  1259. &frame_ip))
  1260. return false;
  1261. break;
  1262. }
  1263. case WASM_OP_GC_PREFIX:
  1264. {
  1265. uint32 opcode1, field_idx, data_seg_idx, array_len;
  1266. if (!comp_ctx->enable_gc) {
  1267. goto unsupport_gc;
  1268. }
  1269. read_leb_uint32(frame_ip, frame_ip_end, opcode1);
  1270. /* opcode1 was checked in loader and is no larger than
  1271. UINT8_MAX */
  1272. opcode = (uint8)opcode1;
  1273. switch (opcode) {
  1274. case WASM_OP_STRUCT_NEW:
  1275. case WASM_OP_STRUCT_NEW_DEFAULT:
  1276. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1277. if (!aot_compile_op_struct_new(
  1278. comp_ctx, func_ctx, type_index,
  1279. opcode == WASM_OP_STRUCT_NEW_DEFAULT))
  1280. return false;
  1281. break;
  1282. case WASM_OP_STRUCT_GET:
  1283. case WASM_OP_STRUCT_GET_S:
  1284. case WASM_OP_STRUCT_GET_U:
  1285. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1286. read_leb_uint32(frame_ip, frame_ip_end, field_idx);
  1287. if (!aot_compile_op_struct_get(
  1288. comp_ctx, func_ctx, type_index, field_idx,
  1289. opcode == WASM_OP_STRUCT_GET_S))
  1290. return false;
  1291. break;
  1292. case WASM_OP_STRUCT_SET:
  1293. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1294. read_leb_uint32(frame_ip, frame_ip_end, field_idx);
  1295. if (!aot_compile_op_struct_set(comp_ctx, func_ctx,
  1296. type_index, field_idx))
  1297. return false;
  1298. break;
  1299. case WASM_OP_ARRAY_NEW:
  1300. case WASM_OP_ARRAY_NEW_DEFAULT:
  1301. case WASM_OP_ARRAY_NEW_FIXED:
  1302. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1303. if (opcode == WASM_OP_ARRAY_NEW_FIXED)
  1304. read_leb_uint32(frame_ip, frame_ip_end, array_len);
  1305. else
  1306. array_len = 0;
  1307. if (!aot_compile_op_array_new(
  1308. comp_ctx, func_ctx, type_index,
  1309. opcode == WASM_OP_ARRAY_NEW_DEFAULT,
  1310. opcode == WASM_OP_ARRAY_NEW_FIXED, array_len))
  1311. return false;
  1312. break;
  1313. case WASM_OP_ARRAY_NEW_DATA:
  1314. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1315. read_leb_uint32(frame_ip, frame_ip_end, data_seg_idx);
  1316. if (!aot_compile_op_array_new_data(
  1317. comp_ctx, func_ctx, type_index, data_seg_idx))
  1318. return false;
  1319. break;
  1320. case WASM_OP_ARRAY_NEW_ELEM:
  1321. /* TODO */
  1322. aot_set_last_error("unsupported opcode");
  1323. return false;
  1324. case WASM_OP_ARRAY_GET:
  1325. case WASM_OP_ARRAY_GET_S:
  1326. case WASM_OP_ARRAY_GET_U:
  1327. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1328. if (!aot_compile_op_array_get(
  1329. comp_ctx, func_ctx, type_index,
  1330. opcode == WASM_OP_ARRAY_GET_S))
  1331. return false;
  1332. break;
  1333. case WASM_OP_ARRAY_SET:
  1334. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1335. if (!aot_compile_op_array_set(comp_ctx, func_ctx,
  1336. type_index))
  1337. return false;
  1338. break;
  1339. case WASM_OP_ARRAY_FILL:
  1340. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1341. if (!aot_compile_op_array_fill(comp_ctx, func_ctx,
  1342. type_index))
  1343. return false;
  1344. break;
  1345. case WASM_OP_ARRAY_COPY:
  1346. {
  1347. uint32 src_type_index;
  1348. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1349. read_leb_uint32(frame_ip, frame_ip_end, src_type_index);
  1350. if (!aot_compile_op_array_copy(
  1351. comp_ctx, func_ctx, type_index, src_type_index))
  1352. return false;
  1353. break;
  1354. }
  1355. case WASM_OP_ARRAY_LEN:
  1356. if (!aot_compile_op_array_len(comp_ctx, func_ctx))
  1357. return false;
  1358. break;
  1359. case WASM_OP_REF_I31:
  1360. if (!aot_compile_op_i31_new(comp_ctx, func_ctx))
  1361. return false;
  1362. break;
  1363. case WASM_OP_I31_GET_S:
  1364. case WASM_OP_I31_GET_U:
  1365. if (!aot_compile_op_i31_get(
  1366. comp_ctx, func_ctx,
  1367. opcode == WASM_OP_I31_GET_S ? true : false))
  1368. return false;
  1369. break;
  1370. case WASM_OP_REF_TEST:
  1371. case WASM_OP_REF_TEST_NULLABLE:
  1372. {
  1373. int32 heap_type;
  1374. read_leb_int32(frame_ip, frame_ip_end, heap_type);
  1375. if (!aot_compile_op_ref_test(
  1376. comp_ctx, func_ctx, heap_type,
  1377. opcode == WASM_OP_REF_TEST_NULLABLE ? true
  1378. : false))
  1379. return false;
  1380. break;
  1381. }
  1382. case WASM_OP_REF_CAST:
  1383. case WASM_OP_REF_CAST_NULLABLE:
  1384. {
  1385. int32 heap_type;
  1386. read_leb_int32(frame_ip, frame_ip_end, heap_type);
  1387. if (!aot_compile_op_ref_cast(
  1388. comp_ctx, func_ctx, heap_type,
  1389. opcode == WASM_OP_REF_CAST_NULLABLE ? true
  1390. : false))
  1391. return false;
  1392. break;
  1393. }
  1394. case WASM_OP_BR_ON_CAST:
  1395. case WASM_OP_BR_ON_CAST_FAIL:
  1396. {
  1397. uint8 castflags;
  1398. int32 heap_type, dst_heap_type;
  1399. CHECK_BUF(frame_ip, frame_ip_end, 1);
  1400. castflags = *frame_ip++;
  1401. read_leb_uint32(frame_ip, frame_ip_end, br_depth);
  1402. read_leb_int32(frame_ip, frame_ip_end, heap_type);
  1403. read_leb_int32(frame_ip, frame_ip_end, dst_heap_type);
  1404. /*
  1405. * castflags should be 0~3:
  1406. * 0: (non-null, non-null)
  1407. * 1: (null, non-null)
  1408. * 2: (non-null, null)
  1409. * 3: (null, null)
  1410. * The nullability of source type has been checked in
  1411. * wasm loader, here we just need the dst nullability
  1412. */
  1413. if (!aot_compile_op_br_on_cast(
  1414. comp_ctx, func_ctx, dst_heap_type,
  1415. castflags & 0x02,
  1416. opcode == WASM_OP_BR_ON_CAST_FAIL, br_depth,
  1417. &frame_ip))
  1418. return false;
  1419. (void)heap_type;
  1420. break;
  1421. }
  1422. case WASM_OP_ANY_CONVERT_EXTERN:
  1423. if (!aot_compile_op_extern_internalize(comp_ctx,
  1424. func_ctx))
  1425. return false;
  1426. break;
  1427. case WASM_OP_EXTERN_CONVERT_ANY:
  1428. if (!aot_compile_op_extern_externalize(comp_ctx,
  1429. func_ctx))
  1430. return false;
  1431. break;
  1432. #if WASM_ENABLE_STRINGREF != 0
  1433. case WASM_OP_STRING_NEW_UTF8:
  1434. case WASM_OP_STRING_NEW_WTF16:
  1435. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  1436. case WASM_OP_STRING_NEW_WTF8:
  1437. {
  1438. EncodingFlag flag = WTF8;
  1439. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  1440. bh_assert(mem_idx == 0);
  1441. if (opcode == WASM_OP_STRING_NEW_WTF16) {
  1442. flag = WTF16;
  1443. }
  1444. else if (opcode == WASM_OP_STRING_NEW_UTF8) {
  1445. flag = UTF8;
  1446. }
  1447. else if (opcode == WASM_OP_STRING_NEW_LOSSY_UTF8) {
  1448. flag = LOSSY_UTF8;
  1449. }
  1450. else if (opcode == WASM_OP_STRING_NEW_WTF8) {
  1451. flag = WTF8;
  1452. }
  1453. if (!aot_compile_op_string_new(comp_ctx, func_ctx,
  1454. flag))
  1455. return false;
  1456. break;
  1457. }
  1458. case WASM_OP_STRING_CONST:
  1459. {
  1460. uint32 contents;
  1461. read_leb_uint32(frame_ip, frame_ip_end, contents);
  1462. if (!aot_compile_op_string_const(comp_ctx, func_ctx,
  1463. contents))
  1464. return false;
  1465. break;
  1466. }
  1467. case WASM_OP_STRING_MEASURE_UTF8:
  1468. case WASM_OP_STRING_MEASURE_WTF8:
  1469. case WASM_OP_STRING_MEASURE_WTF16:
  1470. {
  1471. EncodingFlag flag = WTF8;
  1472. if (opcode == WASM_OP_STRING_MEASURE_WTF16) {
  1473. flag = WTF16;
  1474. }
  1475. else if (opcode == WASM_OP_STRING_MEASURE_UTF8) {
  1476. flag = UTF8;
  1477. }
  1478. else if (opcode == WASM_OP_STRING_MEASURE_WTF8) {
  1479. flag = LOSSY_UTF8;
  1480. }
  1481. if (!aot_compile_op_string_measure(comp_ctx, func_ctx,
  1482. flag))
  1483. return false;
  1484. break;
  1485. }
  1486. case WASM_OP_STRING_ENCODE_UTF8:
  1487. case WASM_OP_STRING_ENCODE_WTF16:
  1488. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  1489. case WASM_OP_STRING_ENCODE_WTF8:
  1490. {
  1491. EncodingFlag flag = WTF8;
  1492. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  1493. bh_assert(mem_idx == 0);
  1494. if (opcode == WASM_OP_STRING_ENCODE_WTF16) {
  1495. flag = WTF16;
  1496. }
  1497. else if (opcode == WASM_OP_STRING_ENCODE_UTF8) {
  1498. flag = UTF8;
  1499. }
  1500. else if (opcode == WASM_OP_STRING_ENCODE_LOSSY_UTF8) {
  1501. flag = LOSSY_UTF8;
  1502. }
  1503. else if (opcode == WASM_OP_STRING_ENCODE_WTF8) {
  1504. flag = WTF8;
  1505. }
  1506. if (!aot_compile_op_string_encode(comp_ctx, func_ctx,
  1507. mem_idx, flag))
  1508. return false;
  1509. break;
  1510. }
  1511. case WASM_OP_STRING_CONCAT:
  1512. if (!aot_compile_op_string_concat(comp_ctx, func_ctx))
  1513. return false;
  1514. break;
  1515. case WASM_OP_STRING_EQ:
  1516. if (!aot_compile_op_string_eq(comp_ctx, func_ctx))
  1517. return false;
  1518. break;
  1519. case WASM_OP_STRING_IS_USV_SEQUENCE:
  1520. if (!aot_compile_op_string_is_usv_sequence(comp_ctx,
  1521. func_ctx))
  1522. return false;
  1523. break;
  1524. case WASM_OP_STRING_AS_WTF8:
  1525. if (!aot_compile_op_string_as_wtf8(comp_ctx, func_ctx))
  1526. return false;
  1527. break;
  1528. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  1529. if (!aot_compile_op_stringview_wtf8_advance(comp_ctx,
  1530. func_ctx))
  1531. return false;
  1532. break;
  1533. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  1534. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  1535. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  1536. {
  1537. EncodingFlag flag = WTF8;
  1538. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  1539. bh_assert(mem_idx == 0);
  1540. if (opcode == WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8) {
  1541. flag = UTF8;
  1542. }
  1543. else if (opcode
  1544. == WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8) {
  1545. flag = LOSSY_UTF8;
  1546. }
  1547. else if (opcode
  1548. == WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8) {
  1549. flag = WTF8;
  1550. }
  1551. if (!aot_compile_op_stringview_wtf8_encode(
  1552. comp_ctx, func_ctx, mem_idx, flag))
  1553. return false;
  1554. break;
  1555. }
  1556. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  1557. if (!aot_compile_op_stringview_wtf8_slice(comp_ctx,
  1558. func_ctx))
  1559. return false;
  1560. break;
  1561. case WASM_OP_STRING_AS_WTF16:
  1562. if (!aot_compile_op_string_as_wtf16(comp_ctx, func_ctx))
  1563. return false;
  1564. break;
  1565. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  1566. if (!aot_compile_op_stringview_wtf16_length(comp_ctx,
  1567. func_ctx))
  1568. return false;
  1569. break;
  1570. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  1571. if (!aot_compile_op_stringview_wtf16_get_codeunit(
  1572. comp_ctx, func_ctx))
  1573. return false;
  1574. break;
  1575. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  1576. {
  1577. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  1578. bh_assert(mem_idx == 0);
  1579. if (!aot_compile_op_stringview_wtf16_encode(
  1580. comp_ctx, func_ctx, mem_idx))
  1581. return false;
  1582. break;
  1583. }
  1584. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  1585. if (!aot_compile_op_stringview_wtf16_slice(comp_ctx,
  1586. func_ctx))
  1587. return false;
  1588. break;
  1589. case WASM_OP_STRING_AS_ITER:
  1590. if (!aot_compile_op_string_as_iter(comp_ctx, func_ctx))
  1591. return false;
  1592. break;
  1593. case WASM_OP_STRINGVIEW_ITER_NEXT:
  1594. if (!aot_compile_op_stringview_iter_next(comp_ctx,
  1595. func_ctx))
  1596. return false;
  1597. break;
  1598. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  1599. if (!aot_compile_op_stringview_iter_advance(comp_ctx,
  1600. func_ctx))
  1601. return false;
  1602. break;
  1603. case WASM_OP_STRINGVIEW_ITER_REWIND:
  1604. if (!aot_compile_op_stringview_iter_rewind(comp_ctx,
  1605. func_ctx))
  1606. return false;
  1607. break;
  1608. case WASM_OP_STRINGVIEW_ITER_SLICE:
  1609. if (!aot_compile_op_stringview_iter_slice(comp_ctx,
  1610. func_ctx))
  1611. return false;
  1612. break;
  1613. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  1614. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  1615. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  1616. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  1617. {
  1618. EncodingFlag flag = WTF8;
  1619. if (opcode == WASM_OP_STRING_NEW_WTF16) {
  1620. flag = WTF16;
  1621. }
  1622. else if (opcode == WASM_OP_STRING_NEW_UTF8) {
  1623. flag = UTF8;
  1624. }
  1625. else if (opcode == WASM_OP_STRING_NEW_LOSSY_UTF8) {
  1626. flag = LOSSY_UTF8;
  1627. }
  1628. else if (opcode == WASM_OP_STRING_NEW_WTF8) {
  1629. flag = WTF8;
  1630. }
  1631. if (!aot_compile_op_string_new_array(comp_ctx, func_ctx,
  1632. flag))
  1633. return false;
  1634. break;
  1635. }
  1636. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  1637. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  1638. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  1639. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  1640. {
  1641. EncodingFlag flag = WTF8;
  1642. if (opcode == WASM_OP_STRING_ENCODE_WTF16) {
  1643. flag = WTF16;
  1644. }
  1645. else if (opcode == WASM_OP_STRING_ENCODE_UTF8) {
  1646. flag = UTF8;
  1647. }
  1648. else if (opcode == WASM_OP_STRING_ENCODE_LOSSY_UTF8) {
  1649. flag = LOSSY_UTF8;
  1650. }
  1651. else if (opcode == WASM_OP_STRING_ENCODE_WTF8) {
  1652. flag = WTF8;
  1653. }
  1654. if (!aot_compile_op_string_encode_array(comp_ctx,
  1655. func_ctx, flag))
  1656. return false;
  1657. break;
  1658. }
  1659. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  1660. default:
  1661. aot_set_last_error("unsupported opcode");
  1662. return false;
  1663. }
  1664. break;
  1665. }
  1666. #endif /* end of WASM_ENABLE_GC != 0 */
  1667. case WASM_OP_GET_LOCAL:
  1668. read_leb_uint32(frame_ip, frame_ip_end, local_idx);
  1669. if (!aot_compile_op_get_local(comp_ctx, func_ctx, local_idx))
  1670. return false;
  1671. break;
  1672. case WASM_OP_SET_LOCAL:
  1673. read_leb_uint32(frame_ip, frame_ip_end, local_idx);
  1674. if (!aot_compile_op_set_local(comp_ctx, func_ctx, local_idx))
  1675. return false;
  1676. break;
  1677. case WASM_OP_TEE_LOCAL:
  1678. read_leb_uint32(frame_ip, frame_ip_end, local_idx);
  1679. if (!aot_compile_op_tee_local(comp_ctx, func_ctx, local_idx))
  1680. return false;
  1681. break;
  1682. case WASM_OP_GET_GLOBAL:
  1683. case WASM_OP_GET_GLOBAL_64:
  1684. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  1685. if (!aot_compile_op_get_global(comp_ctx, func_ctx, global_idx))
  1686. return false;
  1687. break;
  1688. case WASM_OP_SET_GLOBAL:
  1689. case WASM_OP_SET_GLOBAL_64:
  1690. case WASM_OP_SET_GLOBAL_AUX_STACK:
  1691. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  1692. if (!aot_compile_op_set_global(
  1693. comp_ctx, func_ctx, global_idx,
  1694. opcode == WASM_OP_SET_GLOBAL_AUX_STACK ? true : false))
  1695. return false;
  1696. break;
  1697. case WASM_OP_I32_LOAD:
  1698. bytes = 4;
  1699. sign = true;
  1700. goto op_i32_load;
  1701. case WASM_OP_I32_LOAD8_S:
  1702. case WASM_OP_I32_LOAD8_U:
  1703. bytes = 1;
  1704. sign = (opcode == WASM_OP_I32_LOAD8_S) ? true : false;
  1705. goto op_i32_load;
  1706. case WASM_OP_I32_LOAD16_S:
  1707. case WASM_OP_I32_LOAD16_U:
  1708. bytes = 2;
  1709. sign = (opcode == WASM_OP_I32_LOAD16_S) ? true : false;
  1710. op_i32_load:
  1711. read_leb_uint32(frame_ip, frame_ip_end, align);
  1712. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  1713. if (!aot_compile_op_i32_load(comp_ctx, func_ctx, align, offset,
  1714. bytes, sign, false))
  1715. return false;
  1716. break;
  1717. case WASM_OP_I64_LOAD:
  1718. bytes = 8;
  1719. sign = true;
  1720. goto op_i64_load;
  1721. case WASM_OP_I64_LOAD8_S:
  1722. case WASM_OP_I64_LOAD8_U:
  1723. bytes = 1;
  1724. sign = (opcode == WASM_OP_I64_LOAD8_S) ? true : false;
  1725. goto op_i64_load;
  1726. case WASM_OP_I64_LOAD16_S:
  1727. case WASM_OP_I64_LOAD16_U:
  1728. bytes = 2;
  1729. sign = (opcode == WASM_OP_I64_LOAD16_S) ? true : false;
  1730. goto op_i64_load;
  1731. case WASM_OP_I64_LOAD32_S:
  1732. case WASM_OP_I64_LOAD32_U:
  1733. bytes = 4;
  1734. sign = (opcode == WASM_OP_I64_LOAD32_S) ? true : false;
  1735. op_i64_load:
  1736. read_leb_uint32(frame_ip, frame_ip_end, align);
  1737. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  1738. if (!aot_compile_op_i64_load(comp_ctx, func_ctx, align, offset,
  1739. bytes, sign, false))
  1740. return false;
  1741. break;
  1742. case WASM_OP_F32_LOAD:
  1743. read_leb_uint32(frame_ip, frame_ip_end, align);
  1744. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  1745. if (!aot_compile_op_f32_load(comp_ctx, func_ctx, align, offset))
  1746. return false;
  1747. break;
  1748. case WASM_OP_F64_LOAD:
  1749. read_leb_uint32(frame_ip, frame_ip_end, align);
  1750. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  1751. if (!aot_compile_op_f64_load(comp_ctx, func_ctx, align, offset))
  1752. return false;
  1753. break;
  1754. case WASM_OP_I32_STORE:
  1755. bytes = 4;
  1756. goto op_i32_store;
  1757. case WASM_OP_I32_STORE8:
  1758. bytes = 1;
  1759. goto op_i32_store;
  1760. case WASM_OP_I32_STORE16:
  1761. bytes = 2;
  1762. op_i32_store:
  1763. read_leb_uint32(frame_ip, frame_ip_end, align);
  1764. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  1765. if (!aot_compile_op_i32_store(comp_ctx, func_ctx, align, offset,
  1766. bytes, false))
  1767. return false;
  1768. break;
  1769. case WASM_OP_I64_STORE:
  1770. bytes = 8;
  1771. goto op_i64_store;
  1772. case WASM_OP_I64_STORE8:
  1773. bytes = 1;
  1774. goto op_i64_store;
  1775. case WASM_OP_I64_STORE16:
  1776. bytes = 2;
  1777. goto op_i64_store;
  1778. case WASM_OP_I64_STORE32:
  1779. bytes = 4;
  1780. op_i64_store:
  1781. read_leb_uint32(frame_ip, frame_ip_end, align);
  1782. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  1783. if (!aot_compile_op_i64_store(comp_ctx, func_ctx, align, offset,
  1784. bytes, false))
  1785. return false;
  1786. break;
  1787. case WASM_OP_F32_STORE:
  1788. read_leb_uint32(frame_ip, frame_ip_end, align);
  1789. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  1790. if (!aot_compile_op_f32_store(comp_ctx, func_ctx, align,
  1791. offset))
  1792. return false;
  1793. break;
  1794. case WASM_OP_F64_STORE:
  1795. read_leb_uint32(frame_ip, frame_ip_end, align);
  1796. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  1797. if (!aot_compile_op_f64_store(comp_ctx, func_ctx, align,
  1798. offset))
  1799. return false;
  1800. break;
  1801. case WASM_OP_MEMORY_SIZE:
  1802. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  1803. if (!aot_compile_op_memory_size(comp_ctx, func_ctx))
  1804. return false;
  1805. (void)mem_idx;
  1806. break;
  1807. case WASM_OP_MEMORY_GROW:
  1808. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  1809. if (!aot_compile_op_memory_grow(comp_ctx, func_ctx))
  1810. return false;
  1811. break;
  1812. case WASM_OP_I32_CONST:
  1813. read_leb_int32(frame_ip, frame_ip_end, i32_const);
  1814. if (!aot_compile_op_i32_const(comp_ctx, func_ctx, i32_const))
  1815. return false;
  1816. break;
  1817. case WASM_OP_I64_CONST:
  1818. read_leb_int64(frame_ip, frame_ip_end, i64_const);
  1819. if (!aot_compile_op_i64_const(comp_ctx, func_ctx, i64_const))
  1820. return false;
  1821. break;
  1822. case WASM_OP_F32_CONST:
  1823. p_f32 = (uint8 *)&f32_const;
  1824. for (i = 0; i < sizeof(float32); i++)
  1825. *p_f32++ = *frame_ip++;
  1826. if (!aot_compile_op_f32_const(comp_ctx, func_ctx, f32_const))
  1827. return false;
  1828. break;
  1829. case WASM_OP_F64_CONST:
  1830. p_f64 = (uint8 *)&f64_const;
  1831. for (i = 0; i < sizeof(float64); i++)
  1832. *p_f64++ = *frame_ip++;
  1833. if (!aot_compile_op_f64_const(comp_ctx, func_ctx, f64_const))
  1834. return false;
  1835. break;
  1836. case WASM_OP_I32_EQZ:
  1837. case WASM_OP_I32_EQ:
  1838. case WASM_OP_I32_NE:
  1839. case WASM_OP_I32_LT_S:
  1840. case WASM_OP_I32_LT_U:
  1841. case WASM_OP_I32_GT_S:
  1842. case WASM_OP_I32_GT_U:
  1843. case WASM_OP_I32_LE_S:
  1844. case WASM_OP_I32_LE_U:
  1845. case WASM_OP_I32_GE_S:
  1846. case WASM_OP_I32_GE_U:
  1847. if (!aot_compile_op_i32_compare(
  1848. comp_ctx, func_ctx, INT_EQZ + opcode - WASM_OP_I32_EQZ))
  1849. return false;
  1850. break;
  1851. case WASM_OP_I64_EQZ:
  1852. case WASM_OP_I64_EQ:
  1853. case WASM_OP_I64_NE:
  1854. case WASM_OP_I64_LT_S:
  1855. case WASM_OP_I64_LT_U:
  1856. case WASM_OP_I64_GT_S:
  1857. case WASM_OP_I64_GT_U:
  1858. case WASM_OP_I64_LE_S:
  1859. case WASM_OP_I64_LE_U:
  1860. case WASM_OP_I64_GE_S:
  1861. case WASM_OP_I64_GE_U:
  1862. if (!aot_compile_op_i64_compare(
  1863. comp_ctx, func_ctx, INT_EQZ + opcode - WASM_OP_I64_EQZ))
  1864. return false;
  1865. break;
  1866. case WASM_OP_F32_EQ:
  1867. case WASM_OP_F32_NE:
  1868. case WASM_OP_F32_LT:
  1869. case WASM_OP_F32_GT:
  1870. case WASM_OP_F32_LE:
  1871. case WASM_OP_F32_GE:
  1872. if (!aot_compile_op_f32_compare(
  1873. comp_ctx, func_ctx, FLOAT_EQ + opcode - WASM_OP_F32_EQ))
  1874. return false;
  1875. break;
  1876. case WASM_OP_F64_EQ:
  1877. case WASM_OP_F64_NE:
  1878. case WASM_OP_F64_LT:
  1879. case WASM_OP_F64_GT:
  1880. case WASM_OP_F64_LE:
  1881. case WASM_OP_F64_GE:
  1882. if (!aot_compile_op_f64_compare(
  1883. comp_ctx, func_ctx, FLOAT_EQ + opcode - WASM_OP_F64_EQ))
  1884. return false;
  1885. break;
  1886. case WASM_OP_I32_CLZ:
  1887. if (!aot_compile_op_i32_clz(comp_ctx, func_ctx))
  1888. return false;
  1889. break;
  1890. case WASM_OP_I32_CTZ:
  1891. if (!aot_compile_op_i32_ctz(comp_ctx, func_ctx))
  1892. return false;
  1893. break;
  1894. case WASM_OP_I32_POPCNT:
  1895. if (!aot_compile_op_i32_popcnt(comp_ctx, func_ctx))
  1896. return false;
  1897. break;
  1898. case WASM_OP_I32_ADD:
  1899. case WASM_OP_I32_SUB:
  1900. case WASM_OP_I32_MUL:
  1901. case WASM_OP_I32_DIV_S:
  1902. case WASM_OP_I32_DIV_U:
  1903. case WASM_OP_I32_REM_S:
  1904. case WASM_OP_I32_REM_U:
  1905. if (!aot_compile_op_i32_arithmetic(
  1906. comp_ctx, func_ctx, INT_ADD + opcode - WASM_OP_I32_ADD,
  1907. &frame_ip))
  1908. return false;
  1909. break;
  1910. case WASM_OP_I32_AND:
  1911. case WASM_OP_I32_OR:
  1912. case WASM_OP_I32_XOR:
  1913. if (!aot_compile_op_i32_bitwise(
  1914. comp_ctx, func_ctx, INT_SHL + opcode - WASM_OP_I32_AND))
  1915. return false;
  1916. break;
  1917. case WASM_OP_I32_SHL:
  1918. case WASM_OP_I32_SHR_S:
  1919. case WASM_OP_I32_SHR_U:
  1920. case WASM_OP_I32_ROTL:
  1921. case WASM_OP_I32_ROTR:
  1922. if (!aot_compile_op_i32_shift(
  1923. comp_ctx, func_ctx, INT_SHL + opcode - WASM_OP_I32_SHL))
  1924. return false;
  1925. break;
  1926. case WASM_OP_I64_CLZ:
  1927. if (!aot_compile_op_i64_clz(comp_ctx, func_ctx))
  1928. return false;
  1929. break;
  1930. case WASM_OP_I64_CTZ:
  1931. if (!aot_compile_op_i64_ctz(comp_ctx, func_ctx))
  1932. return false;
  1933. break;
  1934. case WASM_OP_I64_POPCNT:
  1935. if (!aot_compile_op_i64_popcnt(comp_ctx, func_ctx))
  1936. return false;
  1937. break;
  1938. case WASM_OP_I64_ADD:
  1939. case WASM_OP_I64_SUB:
  1940. case WASM_OP_I64_MUL:
  1941. case WASM_OP_I64_DIV_S:
  1942. case WASM_OP_I64_DIV_U:
  1943. case WASM_OP_I64_REM_S:
  1944. case WASM_OP_I64_REM_U:
  1945. if (!aot_compile_op_i64_arithmetic(
  1946. comp_ctx, func_ctx, INT_ADD + opcode - WASM_OP_I64_ADD,
  1947. &frame_ip))
  1948. return false;
  1949. break;
  1950. case WASM_OP_I64_AND:
  1951. case WASM_OP_I64_OR:
  1952. case WASM_OP_I64_XOR:
  1953. if (!aot_compile_op_i64_bitwise(
  1954. comp_ctx, func_ctx, INT_SHL + opcode - WASM_OP_I64_AND))
  1955. return false;
  1956. break;
  1957. case WASM_OP_I64_SHL:
  1958. case WASM_OP_I64_SHR_S:
  1959. case WASM_OP_I64_SHR_U:
  1960. case WASM_OP_I64_ROTL:
  1961. case WASM_OP_I64_ROTR:
  1962. if (!aot_compile_op_i64_shift(
  1963. comp_ctx, func_ctx, INT_SHL + opcode - WASM_OP_I64_SHL))
  1964. return false;
  1965. break;
  1966. case WASM_OP_F32_ABS:
  1967. case WASM_OP_F32_NEG:
  1968. case WASM_OP_F32_CEIL:
  1969. case WASM_OP_F32_FLOOR:
  1970. case WASM_OP_F32_TRUNC:
  1971. case WASM_OP_F32_NEAREST:
  1972. case WASM_OP_F32_SQRT:
  1973. if (!aot_compile_op_f32_math(comp_ctx, func_ctx,
  1974. FLOAT_ABS + opcode
  1975. - WASM_OP_F32_ABS))
  1976. return false;
  1977. break;
  1978. case WASM_OP_F32_ADD:
  1979. case WASM_OP_F32_SUB:
  1980. case WASM_OP_F32_MUL:
  1981. case WASM_OP_F32_DIV:
  1982. case WASM_OP_F32_MIN:
  1983. case WASM_OP_F32_MAX:
  1984. if (!aot_compile_op_f32_arithmetic(comp_ctx, func_ctx,
  1985. FLOAT_ADD + opcode
  1986. - WASM_OP_F32_ADD))
  1987. return false;
  1988. break;
  1989. case WASM_OP_F32_COPYSIGN:
  1990. if (!aot_compile_op_f32_copysign(comp_ctx, func_ctx))
  1991. return false;
  1992. break;
  1993. case WASM_OP_F64_ABS:
  1994. case WASM_OP_F64_NEG:
  1995. case WASM_OP_F64_CEIL:
  1996. case WASM_OP_F64_FLOOR:
  1997. case WASM_OP_F64_TRUNC:
  1998. case WASM_OP_F64_NEAREST:
  1999. case WASM_OP_F64_SQRT:
  2000. if (!aot_compile_op_f64_math(comp_ctx, func_ctx,
  2001. FLOAT_ABS + opcode
  2002. - WASM_OP_F64_ABS))
  2003. return false;
  2004. break;
  2005. case WASM_OP_F64_ADD:
  2006. case WASM_OP_F64_SUB:
  2007. case WASM_OP_F64_MUL:
  2008. case WASM_OP_F64_DIV:
  2009. case WASM_OP_F64_MIN:
  2010. case WASM_OP_F64_MAX:
  2011. if (!aot_compile_op_f64_arithmetic(comp_ctx, func_ctx,
  2012. FLOAT_ADD + opcode
  2013. - WASM_OP_F64_ADD))
  2014. return false;
  2015. break;
  2016. case WASM_OP_F64_COPYSIGN:
  2017. if (!aot_compile_op_f64_copysign(comp_ctx, func_ctx))
  2018. return false;
  2019. break;
  2020. case WASM_OP_I32_WRAP_I64:
  2021. if (!aot_compile_op_i32_wrap_i64(comp_ctx, func_ctx))
  2022. return false;
  2023. break;
  2024. case WASM_OP_I32_TRUNC_S_F32:
  2025. case WASM_OP_I32_TRUNC_U_F32:
  2026. sign = (opcode == WASM_OP_I32_TRUNC_S_F32) ? true : false;
  2027. if (!aot_compile_op_i32_trunc_f32(comp_ctx, func_ctx, sign,
  2028. false))
  2029. return false;
  2030. break;
  2031. case WASM_OP_I32_TRUNC_S_F64:
  2032. case WASM_OP_I32_TRUNC_U_F64:
  2033. sign = (opcode == WASM_OP_I32_TRUNC_S_F64) ? true : false;
  2034. if (!aot_compile_op_i32_trunc_f64(comp_ctx, func_ctx, sign,
  2035. false))
  2036. return false;
  2037. break;
  2038. case WASM_OP_I64_EXTEND_S_I32:
  2039. case WASM_OP_I64_EXTEND_U_I32:
  2040. sign = (opcode == WASM_OP_I64_EXTEND_S_I32) ? true : false;
  2041. if (!aot_compile_op_i64_extend_i32(comp_ctx, func_ctx, sign))
  2042. return false;
  2043. break;
  2044. case WASM_OP_I64_TRUNC_S_F32:
  2045. case WASM_OP_I64_TRUNC_U_F32:
  2046. sign = (opcode == WASM_OP_I64_TRUNC_S_F32) ? true : false;
  2047. if (!aot_compile_op_i64_trunc_f32(comp_ctx, func_ctx, sign,
  2048. false))
  2049. return false;
  2050. break;
  2051. case WASM_OP_I64_TRUNC_S_F64:
  2052. case WASM_OP_I64_TRUNC_U_F64:
  2053. sign = (opcode == WASM_OP_I64_TRUNC_S_F64) ? true : false;
  2054. if (!aot_compile_op_i64_trunc_f64(comp_ctx, func_ctx, sign,
  2055. false))
  2056. return false;
  2057. break;
  2058. case WASM_OP_F32_CONVERT_S_I32:
  2059. case WASM_OP_F32_CONVERT_U_I32:
  2060. sign = (opcode == WASM_OP_F32_CONVERT_S_I32) ? true : false;
  2061. if (!aot_compile_op_f32_convert_i32(comp_ctx, func_ctx, sign))
  2062. return false;
  2063. break;
  2064. case WASM_OP_F32_CONVERT_S_I64:
  2065. case WASM_OP_F32_CONVERT_U_I64:
  2066. sign = (opcode == WASM_OP_F32_CONVERT_S_I64) ? true : false;
  2067. if (!aot_compile_op_f32_convert_i64(comp_ctx, func_ctx, sign))
  2068. return false;
  2069. break;
  2070. case WASM_OP_F32_DEMOTE_F64:
  2071. if (!aot_compile_op_f32_demote_f64(comp_ctx, func_ctx))
  2072. return false;
  2073. break;
  2074. case WASM_OP_F64_CONVERT_S_I32:
  2075. case WASM_OP_F64_CONVERT_U_I32:
  2076. sign = (opcode == WASM_OP_F64_CONVERT_S_I32) ? true : false;
  2077. if (!aot_compile_op_f64_convert_i32(comp_ctx, func_ctx, sign))
  2078. return false;
  2079. break;
  2080. case WASM_OP_F64_CONVERT_S_I64:
  2081. case WASM_OP_F64_CONVERT_U_I64:
  2082. sign = (opcode == WASM_OP_F64_CONVERT_S_I64) ? true : false;
  2083. if (!aot_compile_op_f64_convert_i64(comp_ctx, func_ctx, sign))
  2084. return false;
  2085. break;
  2086. case WASM_OP_F64_PROMOTE_F32:
  2087. if (!aot_compile_op_f64_promote_f32(comp_ctx, func_ctx))
  2088. return false;
  2089. break;
  2090. case WASM_OP_I32_REINTERPRET_F32:
  2091. if (!aot_compile_op_i32_reinterpret_f32(comp_ctx, func_ctx))
  2092. return false;
  2093. break;
  2094. case WASM_OP_I64_REINTERPRET_F64:
  2095. if (!aot_compile_op_i64_reinterpret_f64(comp_ctx, func_ctx))
  2096. return false;
  2097. break;
  2098. case WASM_OP_F32_REINTERPRET_I32:
  2099. if (!aot_compile_op_f32_reinterpret_i32(comp_ctx, func_ctx))
  2100. return false;
  2101. break;
  2102. case WASM_OP_F64_REINTERPRET_I64:
  2103. if (!aot_compile_op_f64_reinterpret_i64(comp_ctx, func_ctx))
  2104. return false;
  2105. break;
  2106. case WASM_OP_I32_EXTEND8_S:
  2107. if (!aot_compile_op_i32_extend_i32(comp_ctx, func_ctx, 8))
  2108. return false;
  2109. break;
  2110. case WASM_OP_I32_EXTEND16_S:
  2111. if (!aot_compile_op_i32_extend_i32(comp_ctx, func_ctx, 16))
  2112. return false;
  2113. break;
  2114. case WASM_OP_I64_EXTEND8_S:
  2115. if (!aot_compile_op_i64_extend_i64(comp_ctx, func_ctx, 8))
  2116. return false;
  2117. break;
  2118. case WASM_OP_I64_EXTEND16_S:
  2119. if (!aot_compile_op_i64_extend_i64(comp_ctx, func_ctx, 16))
  2120. return false;
  2121. break;
  2122. case WASM_OP_I64_EXTEND32_S:
  2123. if (!aot_compile_op_i64_extend_i64(comp_ctx, func_ctx, 32))
  2124. return false;
  2125. break;
  2126. case WASM_OP_MISC_PREFIX:
  2127. {
  2128. uint32 opcode1;
  2129. read_leb_uint32(frame_ip, frame_ip_end, opcode1);
  2130. /* opcode1 was checked in loader and is no larger than
  2131. UINT8_MAX */
  2132. opcode = (uint8)opcode1;
  2133. #if WASM_ENABLE_BULK_MEMORY != 0
  2134. if (WASM_OP_MEMORY_INIT <= opcode
  2135. && opcode <= WASM_OP_MEMORY_FILL
  2136. && !comp_ctx->enable_bulk_memory) {
  2137. goto unsupport_bulk_memory;
  2138. }
  2139. #endif
  2140. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2141. if (WASM_OP_TABLE_INIT <= opcode && opcode <= WASM_OP_TABLE_FILL
  2142. && (!comp_ctx->enable_ref_types && !comp_ctx->enable_gc)) {
  2143. goto unsupport_ref_types;
  2144. }
  2145. #endif
  2146. switch (opcode) {
  2147. case WASM_OP_I32_TRUNC_SAT_S_F32:
  2148. case WASM_OP_I32_TRUNC_SAT_U_F32:
  2149. sign = (opcode == WASM_OP_I32_TRUNC_SAT_S_F32) ? true
  2150. : false;
  2151. if (!aot_compile_op_i32_trunc_f32(comp_ctx, func_ctx,
  2152. sign, true))
  2153. return false;
  2154. break;
  2155. case WASM_OP_I32_TRUNC_SAT_S_F64:
  2156. case WASM_OP_I32_TRUNC_SAT_U_F64:
  2157. sign = (opcode == WASM_OP_I32_TRUNC_SAT_S_F64) ? true
  2158. : false;
  2159. if (!aot_compile_op_i32_trunc_f64(comp_ctx, func_ctx,
  2160. sign, true))
  2161. return false;
  2162. break;
  2163. case WASM_OP_I64_TRUNC_SAT_S_F32:
  2164. case WASM_OP_I64_TRUNC_SAT_U_F32:
  2165. sign = (opcode == WASM_OP_I64_TRUNC_SAT_S_F32) ? true
  2166. : false;
  2167. if (!aot_compile_op_i64_trunc_f32(comp_ctx, func_ctx,
  2168. sign, true))
  2169. return false;
  2170. break;
  2171. case WASM_OP_I64_TRUNC_SAT_S_F64:
  2172. case WASM_OP_I64_TRUNC_SAT_U_F64:
  2173. sign = (opcode == WASM_OP_I64_TRUNC_SAT_S_F64) ? true
  2174. : false;
  2175. if (!aot_compile_op_i64_trunc_f64(comp_ctx, func_ctx,
  2176. sign, true))
  2177. return false;
  2178. break;
  2179. #if WASM_ENABLE_BULK_MEMORY != 0
  2180. case WASM_OP_MEMORY_INIT:
  2181. {
  2182. uint32 seg_index;
  2183. read_leb_uint32(frame_ip, frame_ip_end, seg_index);
  2184. frame_ip++;
  2185. if (!aot_compile_op_memory_init(comp_ctx, func_ctx,
  2186. seg_index))
  2187. return false;
  2188. break;
  2189. }
  2190. case WASM_OP_DATA_DROP:
  2191. {
  2192. uint32 seg_index;
  2193. read_leb_uint32(frame_ip, frame_ip_end, seg_index);
  2194. if (!aot_compile_op_data_drop(comp_ctx, func_ctx,
  2195. seg_index))
  2196. return false;
  2197. break;
  2198. }
  2199. case WASM_OP_MEMORY_COPY:
  2200. {
  2201. frame_ip += 2;
  2202. if (!aot_compile_op_memory_copy(comp_ctx, func_ctx))
  2203. return false;
  2204. break;
  2205. }
  2206. case WASM_OP_MEMORY_FILL:
  2207. {
  2208. frame_ip++;
  2209. if (!aot_compile_op_memory_fill(comp_ctx, func_ctx))
  2210. return false;
  2211. break;
  2212. }
  2213. #endif /* WASM_ENABLE_BULK_MEMORY */
  2214. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2215. case WASM_OP_TABLE_INIT:
  2216. {
  2217. uint32 tbl_idx, tbl_seg_idx;
  2218. read_leb_uint32(frame_ip, frame_ip_end, tbl_seg_idx);
  2219. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  2220. if (!aot_compile_op_table_init(comp_ctx, func_ctx,
  2221. tbl_idx, tbl_seg_idx))
  2222. return false;
  2223. break;
  2224. }
  2225. case WASM_OP_ELEM_DROP:
  2226. {
  2227. uint32 tbl_seg_idx;
  2228. read_leb_uint32(frame_ip, frame_ip_end, tbl_seg_idx);
  2229. if (!aot_compile_op_elem_drop(comp_ctx, func_ctx,
  2230. tbl_seg_idx))
  2231. return false;
  2232. break;
  2233. }
  2234. case WASM_OP_TABLE_COPY:
  2235. {
  2236. uint32 src_tbl_idx, dst_tbl_idx;
  2237. read_leb_uint32(frame_ip, frame_ip_end, dst_tbl_idx);
  2238. read_leb_uint32(frame_ip, frame_ip_end, src_tbl_idx);
  2239. if (!aot_compile_op_table_copy(
  2240. comp_ctx, func_ctx, src_tbl_idx, dst_tbl_idx))
  2241. return false;
  2242. break;
  2243. }
  2244. case WASM_OP_TABLE_GROW:
  2245. {
  2246. uint32 tbl_idx;
  2247. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  2248. if (!aot_compile_op_table_grow(comp_ctx, func_ctx,
  2249. tbl_idx))
  2250. return false;
  2251. break;
  2252. }
  2253. case WASM_OP_TABLE_SIZE:
  2254. {
  2255. uint32 tbl_idx;
  2256. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  2257. if (!aot_compile_op_table_size(comp_ctx, func_ctx,
  2258. tbl_idx))
  2259. return false;
  2260. break;
  2261. }
  2262. case WASM_OP_TABLE_FILL:
  2263. {
  2264. uint32 tbl_idx;
  2265. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  2266. if (!aot_compile_op_table_fill(comp_ctx, func_ctx,
  2267. tbl_idx))
  2268. return false;
  2269. break;
  2270. }
  2271. #endif /* WASM_ENABLE_REF_TYPES || WASM_ENABLE_GC */
  2272. default:
  2273. aot_set_last_error("unsupported opcode");
  2274. return false;
  2275. }
  2276. break;
  2277. }
  2278. #if WASM_ENABLE_SHARED_MEMORY != 0
  2279. case WASM_OP_ATOMIC_PREFIX:
  2280. {
  2281. uint8 bin_op, op_type;
  2282. uint32 opcode1;
  2283. read_leb_uint32(frame_ip, frame_ip_end, opcode1);
  2284. /* opcode1 was checked in loader and is no larger than
  2285. UINT8_MAX */
  2286. opcode = (uint8)opcode1;
  2287. if (opcode != WASM_OP_ATOMIC_FENCE) {
  2288. read_leb_uint32(frame_ip, frame_ip_end, align);
  2289. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  2290. }
  2291. switch (opcode) {
  2292. case WASM_OP_ATOMIC_WAIT32:
  2293. if (!aot_compile_op_atomic_wait(comp_ctx, func_ctx,
  2294. VALUE_TYPE_I32, align,
  2295. offset, 4))
  2296. return false;
  2297. break;
  2298. case WASM_OP_ATOMIC_WAIT64:
  2299. if (!aot_compile_op_atomic_wait(comp_ctx, func_ctx,
  2300. VALUE_TYPE_I64, align,
  2301. offset, 8))
  2302. return false;
  2303. break;
  2304. case WASM_OP_ATOMIC_NOTIFY:
  2305. if (!aot_compiler_op_atomic_notify(
  2306. comp_ctx, func_ctx, align, offset, bytes))
  2307. return false;
  2308. break;
  2309. case WASM_OP_ATOMIC_FENCE:
  2310. /* Skip memory index */
  2311. frame_ip++;
  2312. if (!aot_compiler_op_atomic_fence(comp_ctx, func_ctx))
  2313. return false;
  2314. break;
  2315. case WASM_OP_ATOMIC_I32_LOAD:
  2316. bytes = 4;
  2317. goto op_atomic_i32_load;
  2318. case WASM_OP_ATOMIC_I32_LOAD8_U:
  2319. bytes = 1;
  2320. goto op_atomic_i32_load;
  2321. case WASM_OP_ATOMIC_I32_LOAD16_U:
  2322. bytes = 2;
  2323. op_atomic_i32_load:
  2324. if (!aot_compile_op_i32_load(comp_ctx, func_ctx, align,
  2325. offset, bytes, sign, true))
  2326. return false;
  2327. break;
  2328. case WASM_OP_ATOMIC_I64_LOAD:
  2329. bytes = 8;
  2330. goto op_atomic_i64_load;
  2331. case WASM_OP_ATOMIC_I64_LOAD8_U:
  2332. bytes = 1;
  2333. goto op_atomic_i64_load;
  2334. case WASM_OP_ATOMIC_I64_LOAD16_U:
  2335. bytes = 2;
  2336. goto op_atomic_i64_load;
  2337. case WASM_OP_ATOMIC_I64_LOAD32_U:
  2338. bytes = 4;
  2339. op_atomic_i64_load:
  2340. if (!aot_compile_op_i64_load(comp_ctx, func_ctx, align,
  2341. offset, bytes, sign, true))
  2342. return false;
  2343. break;
  2344. case WASM_OP_ATOMIC_I32_STORE:
  2345. bytes = 4;
  2346. goto op_atomic_i32_store;
  2347. case WASM_OP_ATOMIC_I32_STORE8:
  2348. bytes = 1;
  2349. goto op_atomic_i32_store;
  2350. case WASM_OP_ATOMIC_I32_STORE16:
  2351. bytes = 2;
  2352. op_atomic_i32_store:
  2353. if (!aot_compile_op_i32_store(comp_ctx, func_ctx, align,
  2354. offset, bytes, true))
  2355. return false;
  2356. break;
  2357. case WASM_OP_ATOMIC_I64_STORE:
  2358. bytes = 8;
  2359. goto op_atomic_i64_store;
  2360. case WASM_OP_ATOMIC_I64_STORE8:
  2361. bytes = 1;
  2362. goto op_atomic_i64_store;
  2363. case WASM_OP_ATOMIC_I64_STORE16:
  2364. bytes = 2;
  2365. goto op_atomic_i64_store;
  2366. case WASM_OP_ATOMIC_I64_STORE32:
  2367. bytes = 4;
  2368. op_atomic_i64_store:
  2369. if (!aot_compile_op_i64_store(comp_ctx, func_ctx, align,
  2370. offset, bytes, true))
  2371. return false;
  2372. break;
  2373. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  2374. bytes = 4;
  2375. op_type = VALUE_TYPE_I32;
  2376. goto op_atomic_cmpxchg;
  2377. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  2378. bytes = 8;
  2379. op_type = VALUE_TYPE_I64;
  2380. goto op_atomic_cmpxchg;
  2381. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  2382. bytes = 1;
  2383. op_type = VALUE_TYPE_I32;
  2384. goto op_atomic_cmpxchg;
  2385. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  2386. bytes = 2;
  2387. op_type = VALUE_TYPE_I32;
  2388. goto op_atomic_cmpxchg;
  2389. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  2390. bytes = 1;
  2391. op_type = VALUE_TYPE_I64;
  2392. goto op_atomic_cmpxchg;
  2393. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  2394. bytes = 2;
  2395. op_type = VALUE_TYPE_I64;
  2396. goto op_atomic_cmpxchg;
  2397. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  2398. bytes = 4;
  2399. op_type = VALUE_TYPE_I64;
  2400. op_atomic_cmpxchg:
  2401. if (!aot_compile_op_atomic_cmpxchg(comp_ctx, func_ctx,
  2402. op_type, align,
  2403. offset, bytes))
  2404. return false;
  2405. break;
  2406. COMPILE_ATOMIC_RMW(Add, ADD);
  2407. COMPILE_ATOMIC_RMW(Sub, SUB);
  2408. COMPILE_ATOMIC_RMW(And, AND);
  2409. COMPILE_ATOMIC_RMW(Or, OR);
  2410. COMPILE_ATOMIC_RMW(Xor, XOR);
  2411. COMPILE_ATOMIC_RMW(Xchg, XCHG);
  2412. build_atomic_rmw:
  2413. if (!aot_compile_op_atomic_rmw(comp_ctx, func_ctx,
  2414. bin_op, op_type, align,
  2415. offset, bytes))
  2416. return false;
  2417. break;
  2418. default:
  2419. aot_set_last_error("unsupported opcode");
  2420. return false;
  2421. }
  2422. break;
  2423. }
  2424. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  2425. #if WASM_ENABLE_SIMD != 0
  2426. case WASM_OP_SIMD_PREFIX:
  2427. {
  2428. uint32 opcode1;
  2429. if (!comp_ctx->enable_simd) {
  2430. goto unsupport_simd;
  2431. }
  2432. read_leb_uint32(frame_ip, frame_ip_end, opcode1);
  2433. /* opcode1 was checked in loader and is no larger than
  2434. UINT8_MAX */
  2435. opcode = (uint8)opcode1;
  2436. /* follow the order of enum WASMSimdEXTOpcode in
  2437. wasm_opcode.h */
  2438. switch (opcode) {
  2439. /* Memory instruction */
  2440. case SIMD_v128_load:
  2441. {
  2442. read_leb_uint32(frame_ip, frame_ip_end, align);
  2443. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  2444. if (!aot_compile_simd_v128_load(comp_ctx, func_ctx,
  2445. align, offset))
  2446. return false;
  2447. break;
  2448. }
  2449. case SIMD_v128_load8x8_s:
  2450. case SIMD_v128_load8x8_u:
  2451. case SIMD_v128_load16x4_s:
  2452. case SIMD_v128_load16x4_u:
  2453. case SIMD_v128_load32x2_s:
  2454. case SIMD_v128_load32x2_u:
  2455. {
  2456. read_leb_uint32(frame_ip, frame_ip_end, align);
  2457. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  2458. if (!aot_compile_simd_load_extend(
  2459. comp_ctx, func_ctx, opcode, align, offset))
  2460. return false;
  2461. break;
  2462. }
  2463. case SIMD_v128_load8_splat:
  2464. case SIMD_v128_load16_splat:
  2465. case SIMD_v128_load32_splat:
  2466. case SIMD_v128_load64_splat:
  2467. {
  2468. read_leb_uint32(frame_ip, frame_ip_end, align);
  2469. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  2470. if (!aot_compile_simd_load_splat(comp_ctx, func_ctx,
  2471. opcode, align, offset))
  2472. return false;
  2473. break;
  2474. }
  2475. case SIMD_v128_store:
  2476. {
  2477. read_leb_uint32(frame_ip, frame_ip_end, align);
  2478. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  2479. if (!aot_compile_simd_v128_store(comp_ctx, func_ctx,
  2480. align, offset))
  2481. return false;
  2482. break;
  2483. }
  2484. /* Basic operation */
  2485. case SIMD_v128_const:
  2486. {
  2487. if (!aot_compile_simd_v128_const(comp_ctx, func_ctx,
  2488. frame_ip))
  2489. return false;
  2490. frame_ip += 16;
  2491. break;
  2492. }
  2493. case SIMD_v8x16_shuffle:
  2494. {
  2495. if (!aot_compile_simd_shuffle(comp_ctx, func_ctx,
  2496. frame_ip))
  2497. return false;
  2498. frame_ip += 16;
  2499. break;
  2500. }
  2501. case SIMD_v8x16_swizzle:
  2502. {
  2503. if (!aot_compile_simd_swizzle(comp_ctx, func_ctx))
  2504. return false;
  2505. break;
  2506. }
  2507. /* Splat operation */
  2508. case SIMD_i8x16_splat:
  2509. case SIMD_i16x8_splat:
  2510. case SIMD_i32x4_splat:
  2511. case SIMD_i64x2_splat:
  2512. case SIMD_f32x4_splat:
  2513. case SIMD_f64x2_splat:
  2514. {
  2515. if (!aot_compile_simd_splat(comp_ctx, func_ctx, opcode))
  2516. return false;
  2517. break;
  2518. }
  2519. /* Lane operation */
  2520. case SIMD_i8x16_extract_lane_s:
  2521. case SIMD_i8x16_extract_lane_u:
  2522. {
  2523. if (!aot_compile_simd_extract_i8x16(
  2524. comp_ctx, func_ctx, *frame_ip++,
  2525. SIMD_i8x16_extract_lane_s == opcode))
  2526. return false;
  2527. break;
  2528. }
  2529. case SIMD_i8x16_replace_lane:
  2530. {
  2531. if (!aot_compile_simd_replace_i8x16(comp_ctx, func_ctx,
  2532. *frame_ip++))
  2533. return false;
  2534. break;
  2535. }
  2536. case SIMD_i16x8_extract_lane_s:
  2537. case SIMD_i16x8_extract_lane_u:
  2538. {
  2539. if (!aot_compile_simd_extract_i16x8(
  2540. comp_ctx, func_ctx, *frame_ip++,
  2541. SIMD_i16x8_extract_lane_s == opcode))
  2542. return false;
  2543. break;
  2544. }
  2545. case SIMD_i16x8_replace_lane:
  2546. {
  2547. if (!aot_compile_simd_replace_i16x8(comp_ctx, func_ctx,
  2548. *frame_ip++))
  2549. return false;
  2550. break;
  2551. }
  2552. case SIMD_i32x4_extract_lane:
  2553. {
  2554. if (!aot_compile_simd_extract_i32x4(comp_ctx, func_ctx,
  2555. *frame_ip++))
  2556. return false;
  2557. break;
  2558. }
  2559. case SIMD_i32x4_replace_lane:
  2560. {
  2561. if (!aot_compile_simd_replace_i32x4(comp_ctx, func_ctx,
  2562. *frame_ip++))
  2563. return false;
  2564. break;
  2565. }
  2566. case SIMD_i64x2_extract_lane:
  2567. {
  2568. if (!aot_compile_simd_extract_i64x2(comp_ctx, func_ctx,
  2569. *frame_ip++))
  2570. return false;
  2571. break;
  2572. }
  2573. case SIMD_i64x2_replace_lane:
  2574. {
  2575. if (!aot_compile_simd_replace_i64x2(comp_ctx, func_ctx,
  2576. *frame_ip++))
  2577. return false;
  2578. break;
  2579. }
  2580. case SIMD_f32x4_extract_lane:
  2581. {
  2582. if (!aot_compile_simd_extract_f32x4(comp_ctx, func_ctx,
  2583. *frame_ip++))
  2584. return false;
  2585. break;
  2586. }
  2587. case SIMD_f32x4_replace_lane:
  2588. {
  2589. if (!aot_compile_simd_replace_f32x4(comp_ctx, func_ctx,
  2590. *frame_ip++))
  2591. return false;
  2592. break;
  2593. }
  2594. case SIMD_f64x2_extract_lane:
  2595. {
  2596. if (!aot_compile_simd_extract_f64x2(comp_ctx, func_ctx,
  2597. *frame_ip++))
  2598. return false;
  2599. break;
  2600. }
  2601. case SIMD_f64x2_replace_lane:
  2602. {
  2603. if (!aot_compile_simd_replace_f64x2(comp_ctx, func_ctx,
  2604. *frame_ip++))
  2605. return false;
  2606. break;
  2607. }
  2608. /* i8x16 Cmp */
  2609. case SIMD_i8x16_eq:
  2610. case SIMD_i8x16_ne:
  2611. case SIMD_i8x16_lt_s:
  2612. case SIMD_i8x16_lt_u:
  2613. case SIMD_i8x16_gt_s:
  2614. case SIMD_i8x16_gt_u:
  2615. case SIMD_i8x16_le_s:
  2616. case SIMD_i8x16_le_u:
  2617. case SIMD_i8x16_ge_s:
  2618. case SIMD_i8x16_ge_u:
  2619. {
  2620. if (!aot_compile_simd_i8x16_compare(
  2621. comp_ctx, func_ctx,
  2622. INT_EQ + opcode - SIMD_i8x16_eq))
  2623. return false;
  2624. break;
  2625. }
  2626. /* i16x8 Cmp */
  2627. case SIMD_i16x8_eq:
  2628. case SIMD_i16x8_ne:
  2629. case SIMD_i16x8_lt_s:
  2630. case SIMD_i16x8_lt_u:
  2631. case SIMD_i16x8_gt_s:
  2632. case SIMD_i16x8_gt_u:
  2633. case SIMD_i16x8_le_s:
  2634. case SIMD_i16x8_le_u:
  2635. case SIMD_i16x8_ge_s:
  2636. case SIMD_i16x8_ge_u:
  2637. {
  2638. if (!aot_compile_simd_i16x8_compare(
  2639. comp_ctx, func_ctx,
  2640. INT_EQ + opcode - SIMD_i16x8_eq))
  2641. return false;
  2642. break;
  2643. }
  2644. /* i32x4 Cmp */
  2645. case SIMD_i32x4_eq:
  2646. case SIMD_i32x4_ne:
  2647. case SIMD_i32x4_lt_s:
  2648. case SIMD_i32x4_lt_u:
  2649. case SIMD_i32x4_gt_s:
  2650. case SIMD_i32x4_gt_u:
  2651. case SIMD_i32x4_le_s:
  2652. case SIMD_i32x4_le_u:
  2653. case SIMD_i32x4_ge_s:
  2654. case SIMD_i32x4_ge_u:
  2655. {
  2656. if (!aot_compile_simd_i32x4_compare(
  2657. comp_ctx, func_ctx,
  2658. INT_EQ + opcode - SIMD_i32x4_eq))
  2659. return false;
  2660. break;
  2661. }
  2662. /* f32x4 Cmp */
  2663. case SIMD_f32x4_eq:
  2664. case SIMD_f32x4_ne:
  2665. case SIMD_f32x4_lt:
  2666. case SIMD_f32x4_gt:
  2667. case SIMD_f32x4_le:
  2668. case SIMD_f32x4_ge:
  2669. {
  2670. if (!aot_compile_simd_f32x4_compare(
  2671. comp_ctx, func_ctx,
  2672. FLOAT_EQ + opcode - SIMD_f32x4_eq))
  2673. return false;
  2674. break;
  2675. }
  2676. /* f64x2 Cmp */
  2677. case SIMD_f64x2_eq:
  2678. case SIMD_f64x2_ne:
  2679. case SIMD_f64x2_lt:
  2680. case SIMD_f64x2_gt:
  2681. case SIMD_f64x2_le:
  2682. case SIMD_f64x2_ge:
  2683. {
  2684. if (!aot_compile_simd_f64x2_compare(
  2685. comp_ctx, func_ctx,
  2686. FLOAT_EQ + opcode - SIMD_f64x2_eq))
  2687. return false;
  2688. break;
  2689. }
  2690. /* v128 Op */
  2691. case SIMD_v128_not:
  2692. case SIMD_v128_and:
  2693. case SIMD_v128_andnot:
  2694. case SIMD_v128_or:
  2695. case SIMD_v128_xor:
  2696. case SIMD_v128_bitselect:
  2697. {
  2698. if (!aot_compile_simd_v128_bitwise(comp_ctx, func_ctx,
  2699. V128_NOT + opcode
  2700. - SIMD_v128_not))
  2701. return false;
  2702. break;
  2703. }
  2704. case SIMD_v128_any_true:
  2705. {
  2706. if (!aot_compile_simd_v128_any_true(comp_ctx, func_ctx))
  2707. return false;
  2708. break;
  2709. }
  2710. /* Load Lane Op */
  2711. case SIMD_v128_load8_lane:
  2712. case SIMD_v128_load16_lane:
  2713. case SIMD_v128_load32_lane:
  2714. case SIMD_v128_load64_lane:
  2715. {
  2716. read_leb_uint32(frame_ip, frame_ip_end, align);
  2717. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  2718. if (!aot_compile_simd_load_lane(comp_ctx, func_ctx,
  2719. opcode, align, offset,
  2720. *frame_ip++))
  2721. return false;
  2722. break;
  2723. }
  2724. case SIMD_v128_store8_lane:
  2725. case SIMD_v128_store16_lane:
  2726. case SIMD_v128_store32_lane:
  2727. case SIMD_v128_store64_lane:
  2728. {
  2729. read_leb_uint32(frame_ip, frame_ip_end, align);
  2730. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  2731. if (!aot_compile_simd_store_lane(comp_ctx, func_ctx,
  2732. opcode, align, offset,
  2733. *frame_ip++))
  2734. return false;
  2735. break;
  2736. }
  2737. case SIMD_v128_load32_zero:
  2738. case SIMD_v128_load64_zero:
  2739. {
  2740. read_leb_uint32(frame_ip, frame_ip_end, align);
  2741. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  2742. if (!aot_compile_simd_load_zero(comp_ctx, func_ctx,
  2743. opcode, align, offset))
  2744. return false;
  2745. break;
  2746. }
  2747. /* Float conversion */
  2748. case SIMD_f32x4_demote_f64x2_zero:
  2749. {
  2750. if (!aot_compile_simd_f64x2_demote(comp_ctx, func_ctx))
  2751. return false;
  2752. break;
  2753. }
  2754. case SIMD_f64x2_promote_low_f32x4_zero:
  2755. {
  2756. if (!aot_compile_simd_f32x4_promote(comp_ctx, func_ctx))
  2757. return false;
  2758. break;
  2759. }
  2760. /* i8x16 Op */
  2761. case SIMD_i8x16_abs:
  2762. {
  2763. if (!aot_compile_simd_i8x16_abs(comp_ctx, func_ctx))
  2764. return false;
  2765. break;
  2766. }
  2767. case SIMD_i8x16_neg:
  2768. {
  2769. if (!aot_compile_simd_i8x16_neg(comp_ctx, func_ctx))
  2770. return false;
  2771. break;
  2772. }
  2773. case SIMD_i8x16_popcnt:
  2774. {
  2775. if (!aot_compile_simd_i8x16_popcnt(comp_ctx, func_ctx))
  2776. return false;
  2777. break;
  2778. }
  2779. case SIMD_i8x16_all_true:
  2780. {
  2781. if (!aot_compile_simd_i8x16_all_true(comp_ctx,
  2782. func_ctx))
  2783. return false;
  2784. break;
  2785. }
  2786. case SIMD_i8x16_bitmask:
  2787. {
  2788. if (!aot_compile_simd_i8x16_bitmask(comp_ctx, func_ctx))
  2789. return false;
  2790. break;
  2791. }
  2792. case SIMD_i8x16_narrow_i16x8_s:
  2793. case SIMD_i8x16_narrow_i16x8_u:
  2794. {
  2795. if (!aot_compile_simd_i8x16_narrow_i16x8(
  2796. comp_ctx, func_ctx,
  2797. (opcode == SIMD_i8x16_narrow_i16x8_s)))
  2798. return false;
  2799. break;
  2800. }
  2801. case SIMD_f32x4_ceil:
  2802. {
  2803. if (!aot_compile_simd_f32x4_ceil(comp_ctx, func_ctx))
  2804. return false;
  2805. break;
  2806. }
  2807. case SIMD_f32x4_floor:
  2808. {
  2809. if (!aot_compile_simd_f32x4_floor(comp_ctx, func_ctx))
  2810. return false;
  2811. break;
  2812. }
  2813. case SIMD_f32x4_trunc:
  2814. {
  2815. if (!aot_compile_simd_f32x4_trunc(comp_ctx, func_ctx))
  2816. return false;
  2817. break;
  2818. }
  2819. case SIMD_f32x4_nearest:
  2820. {
  2821. if (!aot_compile_simd_f32x4_nearest(comp_ctx, func_ctx))
  2822. return false;
  2823. break;
  2824. }
  2825. case SIMD_i8x16_shl:
  2826. case SIMD_i8x16_shr_s:
  2827. case SIMD_i8x16_shr_u:
  2828. {
  2829. if (!aot_compile_simd_i8x16_shift(comp_ctx, func_ctx,
  2830. INT_SHL + opcode
  2831. - SIMD_i8x16_shl))
  2832. return false;
  2833. break;
  2834. }
  2835. case SIMD_i8x16_add:
  2836. {
  2837. if (!aot_compile_simd_i8x16_arith(comp_ctx, func_ctx,
  2838. V128_ADD))
  2839. return false;
  2840. break;
  2841. }
  2842. case SIMD_i8x16_add_sat_s:
  2843. case SIMD_i8x16_add_sat_u:
  2844. {
  2845. if (!aot_compile_simd_i8x16_saturate(
  2846. comp_ctx, func_ctx, V128_ADD,
  2847. opcode == SIMD_i8x16_add_sat_s))
  2848. return false;
  2849. break;
  2850. }
  2851. case SIMD_i8x16_sub:
  2852. {
  2853. if (!aot_compile_simd_i8x16_arith(comp_ctx, func_ctx,
  2854. V128_SUB))
  2855. return false;
  2856. break;
  2857. }
  2858. case SIMD_i8x16_sub_sat_s:
  2859. case SIMD_i8x16_sub_sat_u:
  2860. {
  2861. if (!aot_compile_simd_i8x16_saturate(
  2862. comp_ctx, func_ctx, V128_SUB,
  2863. opcode == SIMD_i8x16_sub_sat_s))
  2864. return false;
  2865. break;
  2866. }
  2867. case SIMD_f64x2_ceil:
  2868. {
  2869. if (!aot_compile_simd_f64x2_ceil(comp_ctx, func_ctx))
  2870. return false;
  2871. break;
  2872. }
  2873. case SIMD_f64x2_floor:
  2874. {
  2875. if (!aot_compile_simd_f64x2_floor(comp_ctx, func_ctx))
  2876. return false;
  2877. break;
  2878. }
  2879. case SIMD_i8x16_min_s:
  2880. case SIMD_i8x16_min_u:
  2881. {
  2882. if (!aot_compile_simd_i8x16_cmp(
  2883. comp_ctx, func_ctx, V128_MIN,
  2884. opcode == SIMD_i8x16_min_s))
  2885. return false;
  2886. break;
  2887. }
  2888. case SIMD_i8x16_max_s:
  2889. case SIMD_i8x16_max_u:
  2890. {
  2891. if (!aot_compile_simd_i8x16_cmp(
  2892. comp_ctx, func_ctx, V128_MAX,
  2893. opcode == SIMD_i8x16_max_s))
  2894. return false;
  2895. break;
  2896. }
  2897. case SIMD_f64x2_trunc:
  2898. {
  2899. if (!aot_compile_simd_f64x2_trunc(comp_ctx, func_ctx))
  2900. return false;
  2901. break;
  2902. }
  2903. case SIMD_i8x16_avgr_u:
  2904. {
  2905. if (!aot_compile_simd_i8x16_avgr_u(comp_ctx, func_ctx))
  2906. return false;
  2907. break;
  2908. }
  2909. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  2910. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  2911. {
  2912. if (!aot_compile_simd_i16x8_extadd_pairwise_i8x16(
  2913. comp_ctx, func_ctx,
  2914. SIMD_i16x8_extadd_pairwise_i8x16_s == opcode))
  2915. return false;
  2916. break;
  2917. }
  2918. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  2919. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  2920. {
  2921. if (!aot_compile_simd_i32x4_extadd_pairwise_i16x8(
  2922. comp_ctx, func_ctx,
  2923. SIMD_i32x4_extadd_pairwise_i16x8_s == opcode))
  2924. return false;
  2925. break;
  2926. }
  2927. /* i16x8 Op */
  2928. case SIMD_i16x8_abs:
  2929. {
  2930. if (!aot_compile_simd_i16x8_abs(comp_ctx, func_ctx))
  2931. return false;
  2932. break;
  2933. }
  2934. case SIMD_i16x8_neg:
  2935. {
  2936. if (!aot_compile_simd_i16x8_neg(comp_ctx, func_ctx))
  2937. return false;
  2938. break;
  2939. }
  2940. case SIMD_i16x8_q15mulr_sat_s:
  2941. {
  2942. if (!aot_compile_simd_i16x8_q15mulr_sat(comp_ctx,
  2943. func_ctx))
  2944. return false;
  2945. break;
  2946. }
  2947. case SIMD_i16x8_all_true:
  2948. {
  2949. if (!aot_compile_simd_i16x8_all_true(comp_ctx,
  2950. func_ctx))
  2951. return false;
  2952. break;
  2953. }
  2954. case SIMD_i16x8_bitmask:
  2955. {
  2956. if (!aot_compile_simd_i16x8_bitmask(comp_ctx, func_ctx))
  2957. return false;
  2958. break;
  2959. }
  2960. case SIMD_i16x8_narrow_i32x4_s:
  2961. case SIMD_i16x8_narrow_i32x4_u:
  2962. {
  2963. if (!aot_compile_simd_i16x8_narrow_i32x4(
  2964. comp_ctx, func_ctx,
  2965. SIMD_i16x8_narrow_i32x4_s == opcode))
  2966. return false;
  2967. break;
  2968. }
  2969. case SIMD_i16x8_extend_low_i8x16_s:
  2970. case SIMD_i16x8_extend_high_i8x16_s:
  2971. {
  2972. if (!aot_compile_simd_i16x8_extend_i8x16(
  2973. comp_ctx, func_ctx,
  2974. SIMD_i16x8_extend_low_i8x16_s == opcode, true))
  2975. return false;
  2976. break;
  2977. }
  2978. case SIMD_i16x8_extend_low_i8x16_u:
  2979. case SIMD_i16x8_extend_high_i8x16_u:
  2980. {
  2981. if (!aot_compile_simd_i16x8_extend_i8x16(
  2982. comp_ctx, func_ctx,
  2983. SIMD_i16x8_extend_low_i8x16_u == opcode, false))
  2984. return false;
  2985. break;
  2986. }
  2987. case SIMD_i16x8_shl:
  2988. case SIMD_i16x8_shr_s:
  2989. case SIMD_i16x8_shr_u:
  2990. {
  2991. if (!aot_compile_simd_i16x8_shift(comp_ctx, func_ctx,
  2992. INT_SHL + opcode
  2993. - SIMD_i16x8_shl))
  2994. return false;
  2995. break;
  2996. }
  2997. case SIMD_i16x8_add:
  2998. {
  2999. if (!aot_compile_simd_i16x8_arith(comp_ctx, func_ctx,
  3000. V128_ADD))
  3001. return false;
  3002. break;
  3003. }
  3004. case SIMD_i16x8_add_sat_s:
  3005. case SIMD_i16x8_add_sat_u:
  3006. {
  3007. if (!aot_compile_simd_i16x8_saturate(
  3008. comp_ctx, func_ctx, V128_ADD,
  3009. opcode == SIMD_i16x8_add_sat_s ? true : false))
  3010. return false;
  3011. break;
  3012. }
  3013. case SIMD_i16x8_sub:
  3014. {
  3015. if (!aot_compile_simd_i16x8_arith(comp_ctx, func_ctx,
  3016. V128_SUB))
  3017. return false;
  3018. break;
  3019. }
  3020. case SIMD_i16x8_sub_sat_s:
  3021. case SIMD_i16x8_sub_sat_u:
  3022. {
  3023. if (!aot_compile_simd_i16x8_saturate(
  3024. comp_ctx, func_ctx, V128_SUB,
  3025. opcode == SIMD_i16x8_sub_sat_s ? true : false))
  3026. return false;
  3027. break;
  3028. }
  3029. case SIMD_f64x2_nearest:
  3030. {
  3031. if (!aot_compile_simd_f64x2_nearest(comp_ctx, func_ctx))
  3032. return false;
  3033. break;
  3034. }
  3035. case SIMD_i16x8_mul:
  3036. {
  3037. if (!aot_compile_simd_i16x8_arith(comp_ctx, func_ctx,
  3038. V128_MUL))
  3039. return false;
  3040. break;
  3041. }
  3042. case SIMD_i16x8_min_s:
  3043. case SIMD_i16x8_min_u:
  3044. {
  3045. if (!aot_compile_simd_i16x8_cmp(
  3046. comp_ctx, func_ctx, V128_MIN,
  3047. opcode == SIMD_i16x8_min_s))
  3048. return false;
  3049. break;
  3050. }
  3051. case SIMD_i16x8_max_s:
  3052. case SIMD_i16x8_max_u:
  3053. {
  3054. if (!aot_compile_simd_i16x8_cmp(
  3055. comp_ctx, func_ctx, V128_MAX,
  3056. opcode == SIMD_i16x8_max_s))
  3057. return false;
  3058. break;
  3059. }
  3060. case SIMD_i16x8_avgr_u:
  3061. {
  3062. if (!aot_compile_simd_i16x8_avgr_u(comp_ctx, func_ctx))
  3063. return false;
  3064. break;
  3065. }
  3066. case SIMD_i16x8_extmul_low_i8x16_s:
  3067. case SIMD_i16x8_extmul_high_i8x16_s:
  3068. {
  3069. if (!(aot_compile_simd_i16x8_extmul_i8x16(
  3070. comp_ctx, func_ctx,
  3071. SIMD_i16x8_extmul_low_i8x16_s == opcode, true)))
  3072. return false;
  3073. break;
  3074. }
  3075. case SIMD_i16x8_extmul_low_i8x16_u:
  3076. case SIMD_i16x8_extmul_high_i8x16_u:
  3077. {
  3078. if (!(aot_compile_simd_i16x8_extmul_i8x16(
  3079. comp_ctx, func_ctx,
  3080. SIMD_i16x8_extmul_low_i8x16_u == opcode,
  3081. false)))
  3082. return false;
  3083. break;
  3084. }
  3085. /* i32x4 Op */
  3086. case SIMD_i32x4_abs:
  3087. {
  3088. if (!aot_compile_simd_i32x4_abs(comp_ctx, func_ctx))
  3089. return false;
  3090. break;
  3091. }
  3092. case SIMD_i32x4_neg:
  3093. {
  3094. if (!aot_compile_simd_i32x4_neg(comp_ctx, func_ctx))
  3095. return false;
  3096. break;
  3097. }
  3098. case SIMD_i32x4_all_true:
  3099. {
  3100. if (!aot_compile_simd_i32x4_all_true(comp_ctx,
  3101. func_ctx))
  3102. return false;
  3103. break;
  3104. }
  3105. case SIMD_i32x4_bitmask:
  3106. {
  3107. if (!aot_compile_simd_i32x4_bitmask(comp_ctx, func_ctx))
  3108. return false;
  3109. break;
  3110. }
  3111. case SIMD_i32x4_extend_low_i16x8_s:
  3112. case SIMD_i32x4_extend_high_i16x8_s:
  3113. {
  3114. if (!aot_compile_simd_i32x4_extend_i16x8(
  3115. comp_ctx, func_ctx,
  3116. SIMD_i32x4_extend_low_i16x8_s == opcode, true))
  3117. return false;
  3118. break;
  3119. }
  3120. case SIMD_i32x4_extend_low_i16x8_u:
  3121. case SIMD_i32x4_extend_high_i16x8_u:
  3122. {
  3123. if (!aot_compile_simd_i32x4_extend_i16x8(
  3124. comp_ctx, func_ctx,
  3125. SIMD_i32x4_extend_low_i16x8_u == opcode, false))
  3126. return false;
  3127. break;
  3128. }
  3129. case SIMD_i32x4_shl:
  3130. case SIMD_i32x4_shr_s:
  3131. case SIMD_i32x4_shr_u:
  3132. {
  3133. if (!aot_compile_simd_i32x4_shift(comp_ctx, func_ctx,
  3134. INT_SHL + opcode
  3135. - SIMD_i32x4_shl))
  3136. return false;
  3137. break;
  3138. }
  3139. case SIMD_i32x4_add:
  3140. {
  3141. if (!aot_compile_simd_i32x4_arith(comp_ctx, func_ctx,
  3142. V128_ADD))
  3143. return false;
  3144. break;
  3145. }
  3146. case SIMD_i32x4_sub:
  3147. {
  3148. if (!aot_compile_simd_i32x4_arith(comp_ctx, func_ctx,
  3149. V128_SUB))
  3150. return false;
  3151. break;
  3152. }
  3153. case SIMD_i32x4_mul:
  3154. {
  3155. if (!aot_compile_simd_i32x4_arith(comp_ctx, func_ctx,
  3156. V128_MUL))
  3157. return false;
  3158. break;
  3159. }
  3160. case SIMD_i32x4_min_s:
  3161. case SIMD_i32x4_min_u:
  3162. {
  3163. if (!aot_compile_simd_i32x4_cmp(
  3164. comp_ctx, func_ctx, V128_MIN,
  3165. SIMD_i32x4_min_s == opcode))
  3166. return false;
  3167. break;
  3168. }
  3169. case SIMD_i32x4_max_s:
  3170. case SIMD_i32x4_max_u:
  3171. {
  3172. if (!aot_compile_simd_i32x4_cmp(
  3173. comp_ctx, func_ctx, V128_MAX,
  3174. SIMD_i32x4_max_s == opcode))
  3175. return false;
  3176. break;
  3177. }
  3178. case SIMD_i32x4_dot_i16x8_s:
  3179. {
  3180. if (!aot_compile_simd_i32x4_dot_i16x8(comp_ctx,
  3181. func_ctx))
  3182. return false;
  3183. break;
  3184. }
  3185. case SIMD_i32x4_extmul_low_i16x8_s:
  3186. case SIMD_i32x4_extmul_high_i16x8_s:
  3187. {
  3188. if (!aot_compile_simd_i32x4_extmul_i16x8(
  3189. comp_ctx, func_ctx,
  3190. SIMD_i32x4_extmul_low_i16x8_s == opcode, true))
  3191. return false;
  3192. break;
  3193. }
  3194. case SIMD_i32x4_extmul_low_i16x8_u:
  3195. case SIMD_i32x4_extmul_high_i16x8_u:
  3196. {
  3197. if (!aot_compile_simd_i32x4_extmul_i16x8(
  3198. comp_ctx, func_ctx,
  3199. SIMD_i32x4_extmul_low_i16x8_u == opcode, false))
  3200. return false;
  3201. break;
  3202. }
  3203. /* i64x2 Op */
  3204. case SIMD_i64x2_abs:
  3205. {
  3206. if (!aot_compile_simd_i64x2_abs(comp_ctx, func_ctx))
  3207. return false;
  3208. break;
  3209. }
  3210. case SIMD_i64x2_neg:
  3211. {
  3212. if (!aot_compile_simd_i64x2_neg(comp_ctx, func_ctx))
  3213. return false;
  3214. break;
  3215. }
  3216. case SIMD_i64x2_all_true:
  3217. {
  3218. if (!aot_compile_simd_i64x2_all_true(comp_ctx,
  3219. func_ctx))
  3220. return false;
  3221. break;
  3222. }
  3223. case SIMD_i64x2_bitmask:
  3224. {
  3225. if (!aot_compile_simd_i64x2_bitmask(comp_ctx, func_ctx))
  3226. return false;
  3227. break;
  3228. }
  3229. case SIMD_i64x2_extend_low_i32x4_s:
  3230. case SIMD_i64x2_extend_high_i32x4_s:
  3231. {
  3232. if (!aot_compile_simd_i64x2_extend_i32x4(
  3233. comp_ctx, func_ctx,
  3234. SIMD_i64x2_extend_low_i32x4_s == opcode, true))
  3235. return false;
  3236. break;
  3237. }
  3238. case SIMD_i64x2_extend_low_i32x4_u:
  3239. case SIMD_i64x2_extend_high_i32x4_u:
  3240. {
  3241. if (!aot_compile_simd_i64x2_extend_i32x4(
  3242. comp_ctx, func_ctx,
  3243. SIMD_i64x2_extend_low_i32x4_u == opcode, false))
  3244. return false;
  3245. break;
  3246. }
  3247. case SIMD_i64x2_shl:
  3248. case SIMD_i64x2_shr_s:
  3249. case SIMD_i64x2_shr_u:
  3250. {
  3251. if (!aot_compile_simd_i64x2_shift(comp_ctx, func_ctx,
  3252. INT_SHL + opcode
  3253. - SIMD_i64x2_shl))
  3254. return false;
  3255. break;
  3256. }
  3257. case SIMD_i64x2_add:
  3258. {
  3259. if (!aot_compile_simd_i64x2_arith(comp_ctx, func_ctx,
  3260. V128_ADD))
  3261. return false;
  3262. break;
  3263. }
  3264. case SIMD_i64x2_sub:
  3265. {
  3266. if (!aot_compile_simd_i64x2_arith(comp_ctx, func_ctx,
  3267. V128_SUB))
  3268. return false;
  3269. break;
  3270. }
  3271. case SIMD_i64x2_mul:
  3272. {
  3273. if (!aot_compile_simd_i64x2_arith(comp_ctx, func_ctx,
  3274. V128_MUL))
  3275. return false;
  3276. break;
  3277. }
  3278. case SIMD_i64x2_eq:
  3279. case SIMD_i64x2_ne:
  3280. case SIMD_i64x2_lt_s:
  3281. case SIMD_i64x2_gt_s:
  3282. case SIMD_i64x2_le_s:
  3283. case SIMD_i64x2_ge_s:
  3284. {
  3285. IntCond icond[] = { INT_EQ, INT_NE, INT_LT_S,
  3286. INT_GT_S, INT_LE_S, INT_GE_S };
  3287. if (!aot_compile_simd_i64x2_compare(
  3288. comp_ctx, func_ctx,
  3289. icond[opcode - SIMD_i64x2_eq]))
  3290. return false;
  3291. break;
  3292. }
  3293. case SIMD_i64x2_extmul_low_i32x4_s:
  3294. case SIMD_i64x2_extmul_high_i32x4_s:
  3295. {
  3296. if (!aot_compile_simd_i64x2_extmul_i32x4(
  3297. comp_ctx, func_ctx,
  3298. SIMD_i64x2_extmul_low_i32x4_s == opcode, true))
  3299. return false;
  3300. break;
  3301. }
  3302. case SIMD_i64x2_extmul_low_i32x4_u:
  3303. case SIMD_i64x2_extmul_high_i32x4_u:
  3304. {
  3305. if (!aot_compile_simd_i64x2_extmul_i32x4(
  3306. comp_ctx, func_ctx,
  3307. SIMD_i64x2_extmul_low_i32x4_u == opcode, false))
  3308. return false;
  3309. break;
  3310. }
  3311. /* f32x4 Op */
  3312. case SIMD_f32x4_abs:
  3313. {
  3314. if (!aot_compile_simd_f32x4_abs(comp_ctx, func_ctx))
  3315. return false;
  3316. break;
  3317. }
  3318. case SIMD_f32x4_neg:
  3319. {
  3320. if (!aot_compile_simd_f32x4_neg(comp_ctx, func_ctx))
  3321. return false;
  3322. break;
  3323. }
  3324. case SIMD_f32x4_sqrt:
  3325. {
  3326. if (!aot_compile_simd_f32x4_sqrt(comp_ctx, func_ctx))
  3327. return false;
  3328. break;
  3329. }
  3330. case SIMD_f32x4_add:
  3331. case SIMD_f32x4_sub:
  3332. case SIMD_f32x4_mul:
  3333. case SIMD_f32x4_div:
  3334. {
  3335. if (!aot_compile_simd_f32x4_arith(comp_ctx, func_ctx,
  3336. FLOAT_ADD + opcode
  3337. - SIMD_f32x4_add))
  3338. return false;
  3339. break;
  3340. }
  3341. case SIMD_f32x4_min:
  3342. case SIMD_f32x4_max:
  3343. {
  3344. if (!aot_compile_simd_f32x4_min_max(
  3345. comp_ctx, func_ctx, SIMD_f32x4_min == opcode))
  3346. return false;
  3347. break;
  3348. }
  3349. case SIMD_f32x4_pmin:
  3350. case SIMD_f32x4_pmax:
  3351. {
  3352. if (!aot_compile_simd_f32x4_pmin_pmax(
  3353. comp_ctx, func_ctx, SIMD_f32x4_pmin == opcode))
  3354. return false;
  3355. break;
  3356. }
  3357. /* f64x2 Op */
  3358. case SIMD_f64x2_abs:
  3359. {
  3360. if (!aot_compile_simd_f64x2_abs(comp_ctx, func_ctx))
  3361. return false;
  3362. break;
  3363. }
  3364. case SIMD_f64x2_neg:
  3365. {
  3366. if (!aot_compile_simd_f64x2_neg(comp_ctx, func_ctx))
  3367. return false;
  3368. break;
  3369. }
  3370. case SIMD_f64x2_sqrt:
  3371. {
  3372. if (!aot_compile_simd_f64x2_sqrt(comp_ctx, func_ctx))
  3373. return false;
  3374. break;
  3375. }
  3376. case SIMD_f64x2_add:
  3377. case SIMD_f64x2_sub:
  3378. case SIMD_f64x2_mul:
  3379. case SIMD_f64x2_div:
  3380. {
  3381. if (!aot_compile_simd_f64x2_arith(comp_ctx, func_ctx,
  3382. FLOAT_ADD + opcode
  3383. - SIMD_f64x2_add))
  3384. return false;
  3385. break;
  3386. }
  3387. case SIMD_f64x2_min:
  3388. case SIMD_f64x2_max:
  3389. {
  3390. if (!aot_compile_simd_f64x2_min_max(
  3391. comp_ctx, func_ctx, SIMD_f64x2_min == opcode))
  3392. return false;
  3393. break;
  3394. }
  3395. case SIMD_f64x2_pmin:
  3396. case SIMD_f64x2_pmax:
  3397. {
  3398. if (!aot_compile_simd_f64x2_pmin_pmax(
  3399. comp_ctx, func_ctx, SIMD_f64x2_pmin == opcode))
  3400. return false;
  3401. break;
  3402. }
  3403. /* Conversion Op */
  3404. case SIMD_i32x4_trunc_sat_f32x4_s:
  3405. case SIMD_i32x4_trunc_sat_f32x4_u:
  3406. {
  3407. if (!aot_compile_simd_i32x4_trunc_sat_f32x4(
  3408. comp_ctx, func_ctx,
  3409. SIMD_i32x4_trunc_sat_f32x4_s == opcode))
  3410. return false;
  3411. break;
  3412. }
  3413. case SIMD_f32x4_convert_i32x4_s:
  3414. case SIMD_f32x4_convert_i32x4_u:
  3415. {
  3416. if (!aot_compile_simd_f32x4_convert_i32x4(
  3417. comp_ctx, func_ctx,
  3418. SIMD_f32x4_convert_i32x4_s == opcode))
  3419. return false;
  3420. break;
  3421. }
  3422. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  3423. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  3424. {
  3425. if (!aot_compile_simd_i32x4_trunc_sat_f64x2(
  3426. comp_ctx, func_ctx,
  3427. SIMD_i32x4_trunc_sat_f64x2_s_zero == opcode))
  3428. return false;
  3429. break;
  3430. }
  3431. case SIMD_f64x2_convert_low_i32x4_s:
  3432. case SIMD_f64x2_convert_low_i32x4_u:
  3433. {
  3434. if (!aot_compile_simd_f64x2_convert_i32x4(
  3435. comp_ctx, func_ctx,
  3436. SIMD_f64x2_convert_low_i32x4_s == opcode))
  3437. return false;
  3438. break;
  3439. }
  3440. default:
  3441. aot_set_last_error("unsupported SIMD opcode");
  3442. return false;
  3443. }
  3444. break;
  3445. }
  3446. #endif /* end of WASM_ENABLE_SIMD */
  3447. default:
  3448. aot_set_last_error("unsupported opcode");
  3449. return false;
  3450. }
  3451. }
  3452. /* Move func_return block to the bottom */
  3453. if (func_ctx->func_return_block) {
  3454. LLVMBasicBlockRef last_block = LLVMGetLastBasicBlock(func_ctx->func);
  3455. if (last_block != func_ctx->func_return_block)
  3456. LLVMMoveBasicBlockAfter(func_ctx->func_return_block, last_block);
  3457. }
  3458. /* Move got_exception block to the bottom */
  3459. if (func_ctx->got_exception_block) {
  3460. LLVMBasicBlockRef last_block = LLVMGetLastBasicBlock(func_ctx->func);
  3461. if (last_block != func_ctx->got_exception_block)
  3462. LLVMMoveBasicBlockAfter(func_ctx->got_exception_block, last_block);
  3463. }
  3464. return true;
  3465. #if WASM_ENABLE_SIMD != 0
  3466. unsupport_simd:
  3467. aot_set_last_error("SIMD instruction was found, "
  3468. "try removing --disable-simd option");
  3469. return false;
  3470. #endif
  3471. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3472. unsupport_ref_types:
  3473. aot_set_last_error("reference type instruction was found, "
  3474. "try removing --disable-ref-types option "
  3475. "or adding --enable-gc option");
  3476. return false;
  3477. #endif
  3478. #if WASM_ENABLE_GC != 0
  3479. unsupport_gc:
  3480. aot_set_last_error("GC instruction was found, "
  3481. "try adding --enable-gc option");
  3482. return false;
  3483. #endif
  3484. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3485. unsupport_gc_and_ref_types:
  3486. aot_set_last_error(
  3487. "reference type or gc instruction was found, try removing "
  3488. "--disable-ref-types option or adding --enable-gc option");
  3489. return false;
  3490. #endif
  3491. #if WASM_ENABLE_BULK_MEMORY != 0
  3492. unsupport_bulk_memory:
  3493. aot_set_last_error("bulk memory instruction was found, "
  3494. "try removing --disable-bulk-memory option");
  3495. return false;
  3496. #endif
  3497. fail:
  3498. return false;
  3499. }
  3500. static bool
  3501. verify_module(AOTCompContext *comp_ctx)
  3502. {
  3503. char *msg = NULL;
  3504. bool ret;
  3505. ret = LLVMVerifyModule(comp_ctx->module, LLVMPrintMessageAction, &msg);
  3506. if (!ret && msg) {
  3507. if (msg[0] != '\0') {
  3508. aot_set_last_error(msg);
  3509. LLVMDisposeMessage(msg);
  3510. return false;
  3511. }
  3512. LLVMDisposeMessage(msg);
  3513. }
  3514. return true;
  3515. }
  3516. bool
  3517. aot_compile_wasm(AOTCompContext *comp_ctx)
  3518. {
  3519. uint32 i;
  3520. if (!aot_validate_wasm(comp_ctx)) {
  3521. return false;
  3522. }
  3523. bh_print_time("Begin to compile WASM bytecode to LLVM IR");
  3524. for (i = 0; i < comp_ctx->func_ctx_count; i++) {
  3525. if (!aot_compile_func(comp_ctx, i)) {
  3526. return false;
  3527. }
  3528. }
  3529. #if WASM_ENABLE_DEBUG_AOT != 0
  3530. LLVMDIBuilderFinalize(comp_ctx->debug_builder);
  3531. #endif
  3532. /* Disable LLVM module verification for jit mode to speedup
  3533. the compilation process */
  3534. if (!comp_ctx->is_jit_mode) {
  3535. bh_print_time("Begin to verify LLVM module");
  3536. if (!verify_module(comp_ctx)) {
  3537. return false;
  3538. }
  3539. }
  3540. /* Run IR optimization before feeding in ORCJIT and AOT codegen */
  3541. if (comp_ctx->optimize) {
  3542. /* Run passes for AOT/JIT mode.
  3543. TODO: Apply these passes in the do_ir_transform callback of
  3544. TransformLayer when compiling each jit function, so as to
  3545. speedup the launch process. Now there are two issues in the
  3546. JIT: one is memory leak in do_ir_transform, the other is
  3547. possible core dump. */
  3548. bh_print_time("Begin to run llvm optimization passes");
  3549. aot_apply_llvm_new_pass_manager(comp_ctx, comp_ctx->module);
  3550. bh_print_time("Finish llvm optimization passes");
  3551. }
  3552. #ifdef DUMP_MODULE
  3553. LLVMDumpModule(comp_ctx->module);
  3554. os_printf("\n");
  3555. #endif
  3556. if (comp_ctx->is_jit_mode) {
  3557. LLVMErrorRef err;
  3558. LLVMOrcJITDylibRef orc_main_dylib;
  3559. LLVMOrcThreadSafeModuleRef orc_thread_safe_module;
  3560. orc_main_dylib = LLVMOrcLLLazyJITGetMainJITDylib(comp_ctx->orc_jit);
  3561. if (!orc_main_dylib) {
  3562. aot_set_last_error(
  3563. "failed to get orc orc_jit main dynamic library");
  3564. return false;
  3565. }
  3566. orc_thread_safe_module = LLVMOrcCreateNewThreadSafeModule(
  3567. comp_ctx->module, comp_ctx->orc_thread_safe_context);
  3568. if (!orc_thread_safe_module) {
  3569. aot_set_last_error("failed to create thread safe module");
  3570. return false;
  3571. }
  3572. if ((err = LLVMOrcLLLazyJITAddLLVMIRModule(
  3573. comp_ctx->orc_jit, orc_main_dylib, orc_thread_safe_module))) {
  3574. /* If adding the ThreadSafeModule fails then we need to clean it up
  3575. by ourselves, otherwise the orc orc_jit will manage the memory.
  3576. */
  3577. LLVMOrcDisposeThreadSafeModule(orc_thread_safe_module);
  3578. aot_handle_llvm_errmsg("failed to addIRModule", err);
  3579. return false;
  3580. }
  3581. if (comp_ctx->stack_sizes != NULL) {
  3582. LLVMOrcJITTargetAddress addr;
  3583. if ((err = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &addr,
  3584. aot_stack_sizes_alias_name))) {
  3585. aot_handle_llvm_errmsg("failed to look up stack_sizes", err);
  3586. return false;
  3587. }
  3588. comp_ctx->jit_stack_sizes = (uint32 *)addr;
  3589. }
  3590. }
  3591. return true;
  3592. }
  3593. #if !(defined(_WIN32) || defined(_WIN32_))
  3594. char *
  3595. aot_generate_tempfile_name(const char *prefix, const char *extension,
  3596. char *buffer, uint32 len)
  3597. {
  3598. int fd, name_len;
  3599. name_len = snprintf(buffer, len, "%s-XXXXXX", prefix);
  3600. if ((fd = mkstemp(buffer)) <= 0) {
  3601. aot_set_last_error("make temp file failed.");
  3602. return NULL;
  3603. }
  3604. /* close and remove temp file */
  3605. close(fd);
  3606. unlink(buffer);
  3607. /* Check if buffer length is enough */
  3608. /* name_len + '.' + extension + '\0' */
  3609. if (name_len + 1 + strlen(extension) + 1 > len) {
  3610. aot_set_last_error("temp file name too long.");
  3611. return NULL;
  3612. }
  3613. snprintf(buffer + name_len, len - name_len, ".%s", extension);
  3614. return buffer;
  3615. }
  3616. #else
  3617. errno_t
  3618. _mktemp_s(char *nameTemplate, size_t sizeInChars);
  3619. char *
  3620. aot_generate_tempfile_name(const char *prefix, const char *extension,
  3621. char *buffer, uint32 len)
  3622. {
  3623. int name_len;
  3624. name_len = snprintf(buffer, len, "%s-XXXXXX", prefix);
  3625. if (_mktemp_s(buffer, name_len + 1) != 0) {
  3626. return NULL;
  3627. }
  3628. /* Check if buffer length is enough */
  3629. /* name_len + '.' + extension + '\0' */
  3630. if (name_len + 1 + strlen(extension) + 1 > len) {
  3631. aot_set_last_error("temp file name too long.");
  3632. return NULL;
  3633. }
  3634. snprintf(buffer + name_len, len - name_len, ".%s", extension);
  3635. return buffer;
  3636. }
  3637. #endif /* end of !(defined(_WIN32) || defined(_WIN32_)) */
  3638. bool
  3639. aot_emit_llvm_file(AOTCompContext *comp_ctx, const char *file_name)
  3640. {
  3641. char *err = NULL;
  3642. bh_print_time("Begin to emit LLVM IR file");
  3643. if (LLVMPrintModuleToFile(comp_ctx->module, file_name, &err) != 0) {
  3644. if (err) {
  3645. LLVMDisposeMessage(err);
  3646. err = NULL;
  3647. }
  3648. aot_set_last_error("emit llvm ir to file failed.");
  3649. return false;
  3650. }
  3651. return true;
  3652. }
  3653. static bool
  3654. aot_move_file(const char *dest, const char *src)
  3655. {
  3656. FILE *dfp = fopen(dest, "w");
  3657. FILE *sfp = fopen(src, "r");
  3658. size_t rsz;
  3659. char buf[128];
  3660. bool success = false;
  3661. if (dfp == NULL || sfp == NULL) {
  3662. LOG_DEBUG("open error %s %s", dest, src);
  3663. goto fail;
  3664. }
  3665. do {
  3666. rsz = fread(buf, 1, sizeof(buf), sfp);
  3667. if (rsz > 0) {
  3668. size_t wsz = fwrite(buf, 1, rsz, dfp);
  3669. if (wsz < rsz) {
  3670. LOG_DEBUG("write error");
  3671. goto fail;
  3672. }
  3673. }
  3674. if (rsz < sizeof(buf)) {
  3675. if (ferror(sfp)) {
  3676. LOG_DEBUG("read error");
  3677. goto fail;
  3678. }
  3679. }
  3680. } while (rsz > 0);
  3681. success = true;
  3682. fail:
  3683. if (dfp != NULL) {
  3684. if (fclose(dfp)) {
  3685. LOG_DEBUG("close error");
  3686. success = false;
  3687. }
  3688. if (!success) {
  3689. (void)unlink(dest);
  3690. }
  3691. }
  3692. if (sfp != NULL) {
  3693. (void)fclose(sfp);
  3694. }
  3695. if (success) {
  3696. (void)unlink(src);
  3697. }
  3698. return success;
  3699. }
  3700. bool
  3701. aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name)
  3702. {
  3703. char *err = NULL;
  3704. LLVMCodeGenFileType file_type = LLVMObjectFile;
  3705. LLVMTargetRef target = LLVMGetTargetMachineTarget(comp_ctx->target_machine);
  3706. bh_print_time("Begin to emit object file");
  3707. #if !(defined(_WIN32) || defined(_WIN32_))
  3708. if (comp_ctx->external_llc_compiler || comp_ctx->external_asm_compiler) {
  3709. char cmd[1024];
  3710. int ret;
  3711. if (comp_ctx->external_llc_compiler) {
  3712. const char *stack_usage_flag = "";
  3713. char bc_file_name[64];
  3714. char su_file_name[65]; /* See the comment below */
  3715. if (comp_ctx->stack_usage_file != NULL) {
  3716. /*
  3717. * Note: we know the caller uses 64 byte buffer for
  3718. * file_name. It will get 1 byte longer because we
  3719. * replace ".o" with ".su".
  3720. */
  3721. size_t len = strlen(file_name);
  3722. bh_assert(len + 1 <= sizeof(su_file_name));
  3723. bh_assert(len > 3);
  3724. bh_assert(file_name[len - 2] == '.');
  3725. bh_assert(file_name[len - 1] == 'o');
  3726. snprintf(su_file_name, sizeof(su_file_name), "%.*s.su",
  3727. (int)(len - 2), file_name);
  3728. stack_usage_flag = " -fstack-usage";
  3729. }
  3730. if (!aot_generate_tempfile_name("wamrc-bc", "bc", bc_file_name,
  3731. sizeof(bc_file_name))) {
  3732. return false;
  3733. }
  3734. if (LLVMWriteBitcodeToFile(comp_ctx->module, bc_file_name) != 0) {
  3735. aot_set_last_error("emit llvm bitcode file failed.");
  3736. return false;
  3737. }
  3738. snprintf(cmd, sizeof(cmd), "%s%s %s -o %s %s",
  3739. comp_ctx->external_llc_compiler, stack_usage_flag,
  3740. comp_ctx->llc_compiler_flags ? comp_ctx->llc_compiler_flags
  3741. : "-O3 -c",
  3742. file_name, bc_file_name);
  3743. LOG_VERBOSE("invoking external LLC compiler:\n\t%s", cmd);
  3744. ret = system(cmd);
  3745. /* remove temp bitcode file */
  3746. unlink(bc_file_name);
  3747. if (ret != 0) {
  3748. aot_set_last_error("failed to compile LLVM bitcode to obj file "
  3749. "with external LLC compiler.");
  3750. return false;
  3751. }
  3752. if (comp_ctx->stack_usage_file != NULL) {
  3753. /*
  3754. * move the temporary .su file to the specified location.
  3755. *
  3756. * Note: the former is automatically inferred from the output
  3757. * filename (file_name here) by clang.
  3758. *
  3759. * Note: the latter might be user-specified.
  3760. * (wamrc --stack-usage=<file>)
  3761. */
  3762. if (!aot_move_file(comp_ctx->stack_usage_file, su_file_name)) {
  3763. aot_set_last_error("failed to move su file.");
  3764. (void)unlink(su_file_name);
  3765. return false;
  3766. }
  3767. }
  3768. }
  3769. else if (comp_ctx->external_asm_compiler) {
  3770. char asm_file_name[64];
  3771. if (!aot_generate_tempfile_name("wamrc-asm", "s", asm_file_name,
  3772. sizeof(asm_file_name))) {
  3773. return false;
  3774. }
  3775. if (LLVMTargetMachineEmitToFile(comp_ctx->target_machine,
  3776. comp_ctx->module, asm_file_name,
  3777. LLVMAssemblyFile, &err)
  3778. != 0) {
  3779. if (err) {
  3780. LLVMDisposeMessage(err);
  3781. err = NULL;
  3782. }
  3783. aot_set_last_error("emit elf to assembly file failed.");
  3784. return false;
  3785. }
  3786. snprintf(cmd, sizeof(cmd), "%s %s -o %s %s",
  3787. comp_ctx->external_asm_compiler,
  3788. comp_ctx->asm_compiler_flags ? comp_ctx->asm_compiler_flags
  3789. : "-O3 -c",
  3790. file_name, asm_file_name);
  3791. LOG_VERBOSE("invoking external ASM compiler:\n\t%s", cmd);
  3792. ret = system(cmd);
  3793. /* remove temp assembly file */
  3794. unlink(asm_file_name);
  3795. if (ret != 0) {
  3796. aot_set_last_error("failed to compile Assembly file to obj "
  3797. "file with external ASM compiler.");
  3798. return false;
  3799. }
  3800. }
  3801. return true;
  3802. }
  3803. #endif /* end of !(defined(_WIN32) || defined(_WIN32_)) */
  3804. if (!strncmp(LLVMGetTargetName(target), "arc", 3))
  3805. /* Emit to assembly file instead for arc target
  3806. as it cannot emit to object file */
  3807. file_type = LLVMAssemblyFile;
  3808. if (LLVMTargetMachineEmitToFile(comp_ctx->target_machine, comp_ctx->module,
  3809. file_name, file_type, &err)
  3810. != 0) {
  3811. if (err) {
  3812. LLVMDisposeMessage(err);
  3813. err = NULL;
  3814. }
  3815. aot_set_last_error("emit elf to object file failed.");
  3816. return false;
  3817. }
  3818. return true;
  3819. }