aot_compiler.c 162 KB

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