aot_compiler.c 163 KB

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