aot_compiler.c 162 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323
  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. read_leb_uint32(frame_ip, frame_ip_end, br_depth);
  1053. if (!aot_compile_op_br_if(comp_ctx, func_ctx, br_depth,
  1054. &frame_ip))
  1055. return false;
  1056. break;
  1057. }
  1058. case WASM_OP_BR_TABLE:
  1059. {
  1060. read_leb_uint32(frame_ip, frame_ip_end, br_count);
  1061. if (!(br_depths = wasm_runtime_malloc((uint32)sizeof(uint32)
  1062. * (br_count + 1)))) {
  1063. aot_set_last_error("allocate memory failed.");
  1064. goto fail;
  1065. }
  1066. #if WASM_ENABLE_FAST_INTERP != 0
  1067. for (i = 0; i <= br_count; i++)
  1068. read_leb_uint32(frame_ip, frame_ip_end, br_depths[i]);
  1069. #else
  1070. for (i = 0; i <= br_count; i++)
  1071. br_depths[i] = *frame_ip++;
  1072. #endif
  1073. if (!aot_compile_op_br_table(comp_ctx, func_ctx, br_depths,
  1074. br_count, &frame_ip)) {
  1075. wasm_runtime_free(br_depths);
  1076. return false;
  1077. }
  1078. wasm_runtime_free(br_depths);
  1079. break;
  1080. }
  1081. #if WASM_ENABLE_FAST_INTERP == 0
  1082. case EXT_OP_BR_TABLE_CACHE:
  1083. {
  1084. BrTableCache *node = bh_list_first_elem(
  1085. comp_ctx->comp_data->wasm_module->br_table_cache_list);
  1086. BrTableCache *node_next;
  1087. const uint8 *frame_ip_org = frame_ip - 1;
  1088. read_leb_uint32(frame_ip, frame_ip_end, br_count);
  1089. while (node) {
  1090. node_next = bh_list_elem_next(node);
  1091. if (node->br_table_op_addr == frame_ip_org) {
  1092. br_depths = node->br_depths;
  1093. if (!aot_compile_op_br_table(comp_ctx, func_ctx,
  1094. br_depths, br_count,
  1095. &frame_ip)) {
  1096. return false;
  1097. }
  1098. break;
  1099. }
  1100. node = node_next;
  1101. }
  1102. bh_assert(node);
  1103. break;
  1104. }
  1105. #endif
  1106. case WASM_OP_RETURN:
  1107. if (!aot_compile_op_return(comp_ctx, func_ctx, &frame_ip))
  1108. return false;
  1109. break;
  1110. case WASM_OP_CALL:
  1111. {
  1112. read_leb_uint32(frame_ip, frame_ip_end, func_idx);
  1113. if (!aot_compile_op_call(comp_ctx, func_ctx, func_idx, false))
  1114. return false;
  1115. break;
  1116. }
  1117. case WASM_OP_CALL_INDIRECT:
  1118. {
  1119. uint32 tbl_idx;
  1120. read_leb_uint32(frame_ip, frame_ip_end, type_idx);
  1121. if (comp_ctx->enable_gc || comp_ctx->enable_ref_types) {
  1122. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  1123. }
  1124. else {
  1125. frame_ip++;
  1126. tbl_idx = 0;
  1127. }
  1128. if (!aot_compile_op_call_indirect(comp_ctx, func_ctx, type_idx,
  1129. tbl_idx))
  1130. return false;
  1131. break;
  1132. }
  1133. #if WASM_ENABLE_TAIL_CALL != 0
  1134. case WASM_OP_RETURN_CALL:
  1135. {
  1136. if (!comp_ctx->enable_tail_call) {
  1137. aot_set_last_error("unsupported opcode");
  1138. return false;
  1139. }
  1140. read_leb_uint32(frame_ip, frame_ip_end, func_idx);
  1141. if (!aot_compile_op_call(comp_ctx, func_ctx, func_idx, true))
  1142. return false;
  1143. if (!aot_compile_op_return(comp_ctx, func_ctx, &frame_ip))
  1144. return false;
  1145. break;
  1146. }
  1147. case WASM_OP_RETURN_CALL_INDIRECT:
  1148. {
  1149. uint32 tbl_idx;
  1150. if (!comp_ctx->enable_tail_call) {
  1151. aot_set_last_error("unsupported opcode");
  1152. return false;
  1153. }
  1154. read_leb_uint32(frame_ip, frame_ip_end, type_idx);
  1155. if (comp_ctx->enable_gc || comp_ctx->enable_ref_types) {
  1156. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  1157. }
  1158. else {
  1159. frame_ip++;
  1160. tbl_idx = 0;
  1161. }
  1162. if (!aot_compile_op_call_indirect(comp_ctx, func_ctx, type_idx,
  1163. tbl_idx))
  1164. return false;
  1165. if (!aot_compile_op_return(comp_ctx, func_ctx, &frame_ip))
  1166. return false;
  1167. break;
  1168. }
  1169. #endif /* end of WASM_ENABLE_TAIL_CALL */
  1170. case WASM_OP_DROP:
  1171. if (!aot_compile_op_drop(comp_ctx, func_ctx, true))
  1172. return false;
  1173. break;
  1174. case WASM_OP_DROP_64:
  1175. if (!aot_compile_op_drop(comp_ctx, func_ctx, false))
  1176. return false;
  1177. break;
  1178. case WASM_OP_SELECT:
  1179. if (!aot_compile_op_select(comp_ctx, func_ctx, true))
  1180. return false;
  1181. break;
  1182. case WASM_OP_SELECT_64:
  1183. if (!aot_compile_op_select(comp_ctx, func_ctx, false))
  1184. return false;
  1185. break;
  1186. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  1187. case WASM_OP_SELECT_T:
  1188. {
  1189. uint32 vec_len;
  1190. if (!comp_ctx->enable_ref_types && !comp_ctx->enable_gc) {
  1191. goto unsupport_gc_and_ref_types;
  1192. }
  1193. read_leb_uint32(frame_ip, frame_ip_end, vec_len);
  1194. bh_assert(vec_len == 1);
  1195. (void)vec_len;
  1196. type_idx = *frame_ip++;
  1197. if (!aot_compile_op_select(
  1198. comp_ctx, func_ctx,
  1199. (type_idx != VALUE_TYPE_I64)
  1200. && (type_idx != VALUE_TYPE_F64)
  1201. #if WASM_ENABLE_GC != 0
  1202. && !(comp_ctx->enable_gc
  1203. && comp_ctx->pointer_size == sizeof(uint64)
  1204. && wasm_is_type_reftype(type_idx))
  1205. #endif
  1206. ))
  1207. return false;
  1208. break;
  1209. }
  1210. case WASM_OP_TABLE_GET:
  1211. {
  1212. uint32 tbl_idx;
  1213. if (!comp_ctx->enable_ref_types && !comp_ctx->enable_gc) {
  1214. goto unsupport_gc_and_ref_types;
  1215. }
  1216. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  1217. if (!aot_compile_op_table_get(comp_ctx, func_ctx, tbl_idx))
  1218. return false;
  1219. break;
  1220. }
  1221. case WASM_OP_TABLE_SET:
  1222. {
  1223. uint32 tbl_idx;
  1224. if (!comp_ctx->enable_ref_types && !comp_ctx->enable_gc) {
  1225. goto unsupport_gc_and_ref_types;
  1226. }
  1227. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  1228. if (!aot_compile_op_table_set(comp_ctx, func_ctx, tbl_idx))
  1229. return false;
  1230. break;
  1231. }
  1232. case WASM_OP_REF_NULL:
  1233. {
  1234. uint32 type;
  1235. if (!comp_ctx->enable_ref_types && !comp_ctx->enable_gc) {
  1236. goto unsupport_gc_and_ref_types;
  1237. }
  1238. read_leb_uint32(frame_ip, frame_ip_end, type);
  1239. if (!aot_compile_op_ref_null(comp_ctx, func_ctx))
  1240. return false;
  1241. (void)type;
  1242. break;
  1243. }
  1244. case WASM_OP_REF_IS_NULL:
  1245. {
  1246. if (!comp_ctx->enable_ref_types && !comp_ctx->enable_gc) {
  1247. goto unsupport_gc_and_ref_types;
  1248. }
  1249. if (!aot_compile_op_ref_is_null(comp_ctx, func_ctx))
  1250. return false;
  1251. break;
  1252. }
  1253. case WASM_OP_REF_FUNC:
  1254. {
  1255. if (!comp_ctx->enable_ref_types && !comp_ctx->enable_gc) {
  1256. goto unsupport_gc_and_ref_types;
  1257. }
  1258. read_leb_uint32(frame_ip, frame_ip_end, func_idx);
  1259. if (!aot_compile_op_ref_func(comp_ctx, func_ctx, func_idx))
  1260. return false;
  1261. break;
  1262. }
  1263. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  1264. #if WASM_ENABLE_GC != 0
  1265. case WASM_OP_CALL_REF:
  1266. {
  1267. if (!comp_ctx->enable_gc) {
  1268. goto unsupport_gc;
  1269. }
  1270. read_leb_uint32(frame_ip, frame_ip_end, type_idx);
  1271. if (!aot_compile_op_call_ref(comp_ctx, func_ctx, type_idx,
  1272. false))
  1273. return false;
  1274. break;
  1275. }
  1276. case WASM_OP_RETURN_CALL_REF:
  1277. {
  1278. if (!comp_ctx->enable_gc) {
  1279. goto unsupport_gc;
  1280. }
  1281. read_leb_uint32(frame_ip, frame_ip_end, type_idx);
  1282. if (!aot_compile_op_call_ref(comp_ctx, func_ctx, type_idx,
  1283. true))
  1284. return false;
  1285. if (!aot_compile_op_return(comp_ctx, func_ctx, &frame_ip))
  1286. return false;
  1287. break;
  1288. }
  1289. case WASM_OP_REF_EQ:
  1290. if (!comp_ctx->enable_gc) {
  1291. goto unsupport_gc;
  1292. }
  1293. if (!aot_compile_op_ref_eq(comp_ctx, func_ctx))
  1294. return false;
  1295. break;
  1296. case WASM_OP_REF_AS_NON_NULL:
  1297. if (!comp_ctx->enable_gc) {
  1298. goto unsupport_gc;
  1299. }
  1300. if (!aot_compile_op_ref_as_non_null(comp_ctx, func_ctx))
  1301. return false;
  1302. break;
  1303. case WASM_OP_BR_ON_NULL:
  1304. {
  1305. if (!comp_ctx->enable_gc) {
  1306. goto unsupport_gc;
  1307. }
  1308. read_leb_uint32(frame_ip, frame_ip_end, br_depth);
  1309. if (!aot_compile_op_br_on_null(comp_ctx, func_ctx, br_depth,
  1310. &frame_ip))
  1311. return false;
  1312. break;
  1313. }
  1314. case WASM_OP_BR_ON_NON_NULL:
  1315. {
  1316. if (!comp_ctx->enable_gc) {
  1317. goto unsupport_gc;
  1318. }
  1319. read_leb_uint32(frame_ip, frame_ip_end, br_depth);
  1320. if (!aot_compile_op_br_on_non_null(comp_ctx, func_ctx, br_depth,
  1321. &frame_ip))
  1322. return false;
  1323. break;
  1324. }
  1325. case WASM_OP_GC_PREFIX:
  1326. {
  1327. uint32 opcode1, field_idx, data_seg_idx, array_len;
  1328. if (!comp_ctx->enable_gc) {
  1329. goto unsupport_gc;
  1330. }
  1331. read_leb_uint32(frame_ip, frame_ip_end, opcode1);
  1332. /* opcode1 was checked in loader and is no larger than
  1333. UINT8_MAX */
  1334. opcode = (uint8)opcode1;
  1335. switch (opcode) {
  1336. case WASM_OP_STRUCT_NEW:
  1337. case WASM_OP_STRUCT_NEW_DEFAULT:
  1338. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1339. if (!aot_compile_op_struct_new(
  1340. comp_ctx, func_ctx, type_index,
  1341. opcode == WASM_OP_STRUCT_NEW_DEFAULT))
  1342. return false;
  1343. break;
  1344. case WASM_OP_STRUCT_GET:
  1345. case WASM_OP_STRUCT_GET_S:
  1346. case WASM_OP_STRUCT_GET_U:
  1347. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1348. read_leb_uint32(frame_ip, frame_ip_end, field_idx);
  1349. if (!aot_compile_op_struct_get(
  1350. comp_ctx, func_ctx, type_index, field_idx,
  1351. opcode == WASM_OP_STRUCT_GET_S))
  1352. return false;
  1353. break;
  1354. case WASM_OP_STRUCT_SET:
  1355. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1356. read_leb_uint32(frame_ip, frame_ip_end, field_idx);
  1357. if (!aot_compile_op_struct_set(comp_ctx, func_ctx,
  1358. type_index, field_idx))
  1359. return false;
  1360. break;
  1361. case WASM_OP_ARRAY_NEW:
  1362. case WASM_OP_ARRAY_NEW_DEFAULT:
  1363. case WASM_OP_ARRAY_NEW_FIXED:
  1364. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1365. if (opcode == WASM_OP_ARRAY_NEW_FIXED)
  1366. read_leb_uint32(frame_ip, frame_ip_end, array_len);
  1367. else
  1368. array_len = 0;
  1369. if (!aot_compile_op_array_new(
  1370. comp_ctx, func_ctx, type_index,
  1371. opcode == WASM_OP_ARRAY_NEW_DEFAULT,
  1372. opcode == WASM_OP_ARRAY_NEW_FIXED, array_len))
  1373. return false;
  1374. break;
  1375. case WASM_OP_ARRAY_NEW_DATA:
  1376. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1377. read_leb_uint32(frame_ip, frame_ip_end, data_seg_idx);
  1378. if (!aot_compile_op_array_new_data(
  1379. comp_ctx, func_ctx, type_index, data_seg_idx))
  1380. return false;
  1381. break;
  1382. case WASM_OP_ARRAY_NEW_ELEM:
  1383. /* TODO */
  1384. aot_set_last_error("unsupported opcode");
  1385. return false;
  1386. case WASM_OP_ARRAY_GET:
  1387. case WASM_OP_ARRAY_GET_S:
  1388. case WASM_OP_ARRAY_GET_U:
  1389. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1390. if (!aot_compile_op_array_get(
  1391. comp_ctx, func_ctx, type_index,
  1392. opcode == WASM_OP_ARRAY_GET_S))
  1393. return false;
  1394. break;
  1395. case WASM_OP_ARRAY_SET:
  1396. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1397. if (!aot_compile_op_array_set(comp_ctx, func_ctx,
  1398. type_index))
  1399. return false;
  1400. break;
  1401. case WASM_OP_ARRAY_FILL:
  1402. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1403. if (!aot_compile_op_array_fill(comp_ctx, func_ctx,
  1404. type_index))
  1405. return false;
  1406. break;
  1407. case WASM_OP_ARRAY_COPY:
  1408. {
  1409. uint32 src_type_index;
  1410. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1411. read_leb_uint32(frame_ip, frame_ip_end, src_type_index);
  1412. if (!aot_compile_op_array_copy(
  1413. comp_ctx, func_ctx, type_index, src_type_index))
  1414. return false;
  1415. break;
  1416. }
  1417. case WASM_OP_ARRAY_LEN:
  1418. if (!aot_compile_op_array_len(comp_ctx, func_ctx))
  1419. return false;
  1420. break;
  1421. case WASM_OP_REF_I31:
  1422. if (!aot_compile_op_i31_new(comp_ctx, func_ctx))
  1423. return false;
  1424. break;
  1425. case WASM_OP_I31_GET_S:
  1426. case WASM_OP_I31_GET_U:
  1427. if (!aot_compile_op_i31_get(
  1428. comp_ctx, func_ctx,
  1429. opcode == WASM_OP_I31_GET_S ? true : false))
  1430. return false;
  1431. break;
  1432. case WASM_OP_REF_TEST:
  1433. case WASM_OP_REF_TEST_NULLABLE:
  1434. {
  1435. int32 heap_type;
  1436. read_leb_int32(frame_ip, frame_ip_end, heap_type);
  1437. if (!aot_compile_op_ref_test(
  1438. comp_ctx, func_ctx, heap_type,
  1439. opcode == WASM_OP_REF_TEST_NULLABLE ? true
  1440. : false))
  1441. return false;
  1442. break;
  1443. }
  1444. case WASM_OP_REF_CAST:
  1445. case WASM_OP_REF_CAST_NULLABLE:
  1446. {
  1447. int32 heap_type;
  1448. read_leb_int32(frame_ip, frame_ip_end, heap_type);
  1449. if (!aot_compile_op_ref_cast(
  1450. comp_ctx, func_ctx, heap_type,
  1451. opcode == WASM_OP_REF_CAST_NULLABLE ? true
  1452. : false))
  1453. return false;
  1454. break;
  1455. }
  1456. case WASM_OP_BR_ON_CAST:
  1457. case WASM_OP_BR_ON_CAST_FAIL:
  1458. {
  1459. uint8 castflags;
  1460. int32 heap_type, dst_heap_type;
  1461. CHECK_BUF(frame_ip, frame_ip_end, 1);
  1462. castflags = *frame_ip++;
  1463. read_leb_uint32(frame_ip, frame_ip_end, br_depth);
  1464. read_leb_int32(frame_ip, frame_ip_end, heap_type);
  1465. read_leb_int32(frame_ip, frame_ip_end, dst_heap_type);
  1466. /*
  1467. * castflags should be 0~3:
  1468. * 0: (non-null, non-null)
  1469. * 1: (null, non-null)
  1470. * 2: (non-null, null)
  1471. * 3: (null, null)
  1472. * The nullability of source type has been checked in
  1473. * wasm loader, here we just need the dst nullability
  1474. */
  1475. if (!aot_compile_op_br_on_cast(
  1476. comp_ctx, func_ctx, dst_heap_type,
  1477. castflags & 0x02,
  1478. opcode == WASM_OP_BR_ON_CAST_FAIL, br_depth,
  1479. &frame_ip))
  1480. return false;
  1481. (void)heap_type;
  1482. break;
  1483. }
  1484. case WASM_OP_ANY_CONVERT_EXTERN:
  1485. if (!aot_compile_op_extern_internalize(comp_ctx,
  1486. func_ctx))
  1487. return false;
  1488. break;
  1489. case WASM_OP_EXTERN_CONVERT_ANY:
  1490. if (!aot_compile_op_extern_externalize(comp_ctx,
  1491. func_ctx))
  1492. return false;
  1493. break;
  1494. #if WASM_ENABLE_STRINGREF != 0
  1495. case WASM_OP_STRING_NEW_UTF8:
  1496. case WASM_OP_STRING_NEW_WTF16:
  1497. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  1498. case WASM_OP_STRING_NEW_WTF8:
  1499. {
  1500. EncodingFlag flag = WTF8;
  1501. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  1502. bh_assert(mem_idx == 0);
  1503. if (opcode == WASM_OP_STRING_NEW_WTF16) {
  1504. flag = WTF16;
  1505. }
  1506. else if (opcode == WASM_OP_STRING_NEW_UTF8) {
  1507. flag = UTF8;
  1508. }
  1509. else if (opcode == WASM_OP_STRING_NEW_LOSSY_UTF8) {
  1510. flag = LOSSY_UTF8;
  1511. }
  1512. else if (opcode == WASM_OP_STRING_NEW_WTF8) {
  1513. flag = WTF8;
  1514. }
  1515. if (!aot_compile_op_string_new(comp_ctx, func_ctx,
  1516. flag))
  1517. return false;
  1518. break;
  1519. }
  1520. case WASM_OP_STRING_CONST:
  1521. {
  1522. uint32 contents;
  1523. read_leb_uint32(frame_ip, frame_ip_end, contents);
  1524. if (!aot_compile_op_string_const(comp_ctx, func_ctx,
  1525. contents))
  1526. return false;
  1527. break;
  1528. }
  1529. case WASM_OP_STRING_MEASURE_UTF8:
  1530. case WASM_OP_STRING_MEASURE_WTF8:
  1531. case WASM_OP_STRING_MEASURE_WTF16:
  1532. {
  1533. EncodingFlag flag = WTF8;
  1534. if (opcode == WASM_OP_STRING_MEASURE_WTF16) {
  1535. flag = WTF16;
  1536. }
  1537. else if (opcode == WASM_OP_STRING_MEASURE_UTF8) {
  1538. flag = UTF8;
  1539. }
  1540. else if (opcode == WASM_OP_STRING_MEASURE_WTF8) {
  1541. flag = LOSSY_UTF8;
  1542. }
  1543. if (!aot_compile_op_string_measure(comp_ctx, func_ctx,
  1544. flag))
  1545. return false;
  1546. break;
  1547. }
  1548. case WASM_OP_STRING_ENCODE_UTF8:
  1549. case WASM_OP_STRING_ENCODE_WTF16:
  1550. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  1551. case WASM_OP_STRING_ENCODE_WTF8:
  1552. {
  1553. EncodingFlag flag = WTF8;
  1554. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  1555. bh_assert(mem_idx == 0);
  1556. if (opcode == WASM_OP_STRING_ENCODE_WTF16) {
  1557. flag = WTF16;
  1558. }
  1559. else if (opcode == WASM_OP_STRING_ENCODE_UTF8) {
  1560. flag = UTF8;
  1561. }
  1562. else if (opcode == WASM_OP_STRING_ENCODE_LOSSY_UTF8) {
  1563. flag = LOSSY_UTF8;
  1564. }
  1565. else if (opcode == WASM_OP_STRING_ENCODE_WTF8) {
  1566. flag = WTF8;
  1567. }
  1568. if (!aot_compile_op_string_encode(comp_ctx, func_ctx,
  1569. mem_idx, flag))
  1570. return false;
  1571. break;
  1572. }
  1573. case WASM_OP_STRING_CONCAT:
  1574. if (!aot_compile_op_string_concat(comp_ctx, func_ctx))
  1575. return false;
  1576. break;
  1577. case WASM_OP_STRING_EQ:
  1578. if (!aot_compile_op_string_eq(comp_ctx, func_ctx))
  1579. return false;
  1580. break;
  1581. case WASM_OP_STRING_IS_USV_SEQUENCE:
  1582. if (!aot_compile_op_string_is_usv_sequence(comp_ctx,
  1583. func_ctx))
  1584. return false;
  1585. break;
  1586. case WASM_OP_STRING_AS_WTF8:
  1587. if (!aot_compile_op_string_as_wtf8(comp_ctx, func_ctx))
  1588. return false;
  1589. break;
  1590. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  1591. if (!aot_compile_op_stringview_wtf8_advance(comp_ctx,
  1592. func_ctx))
  1593. return false;
  1594. break;
  1595. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  1596. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  1597. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  1598. {
  1599. EncodingFlag flag = WTF8;
  1600. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  1601. bh_assert(mem_idx == 0);
  1602. if (opcode == WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8) {
  1603. flag = UTF8;
  1604. }
  1605. else if (opcode
  1606. == WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8) {
  1607. flag = LOSSY_UTF8;
  1608. }
  1609. else if (opcode
  1610. == WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8) {
  1611. flag = WTF8;
  1612. }
  1613. if (!aot_compile_op_stringview_wtf8_encode(
  1614. comp_ctx, func_ctx, mem_idx, flag))
  1615. return false;
  1616. break;
  1617. }
  1618. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  1619. if (!aot_compile_op_stringview_wtf8_slice(comp_ctx,
  1620. func_ctx))
  1621. return false;
  1622. break;
  1623. case WASM_OP_STRING_AS_WTF16:
  1624. if (!aot_compile_op_string_as_wtf16(comp_ctx, func_ctx))
  1625. return false;
  1626. break;
  1627. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  1628. if (!aot_compile_op_stringview_wtf16_length(comp_ctx,
  1629. func_ctx))
  1630. return false;
  1631. break;
  1632. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  1633. if (!aot_compile_op_stringview_wtf16_get_codeunit(
  1634. comp_ctx, func_ctx))
  1635. return false;
  1636. break;
  1637. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  1638. {
  1639. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  1640. bh_assert(mem_idx == 0);
  1641. if (!aot_compile_op_stringview_wtf16_encode(
  1642. comp_ctx, func_ctx, mem_idx))
  1643. return false;
  1644. break;
  1645. }
  1646. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  1647. if (!aot_compile_op_stringview_wtf16_slice(comp_ctx,
  1648. func_ctx))
  1649. return false;
  1650. break;
  1651. case WASM_OP_STRING_AS_ITER:
  1652. if (!aot_compile_op_string_as_iter(comp_ctx, func_ctx))
  1653. return false;
  1654. break;
  1655. case WASM_OP_STRINGVIEW_ITER_NEXT:
  1656. if (!aot_compile_op_stringview_iter_next(comp_ctx,
  1657. func_ctx))
  1658. return false;
  1659. break;
  1660. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  1661. if (!aot_compile_op_stringview_iter_advance(comp_ctx,
  1662. func_ctx))
  1663. return false;
  1664. break;
  1665. case WASM_OP_STRINGVIEW_ITER_REWIND:
  1666. if (!aot_compile_op_stringview_iter_rewind(comp_ctx,
  1667. func_ctx))
  1668. return false;
  1669. break;
  1670. case WASM_OP_STRINGVIEW_ITER_SLICE:
  1671. if (!aot_compile_op_stringview_iter_slice(comp_ctx,
  1672. func_ctx))
  1673. return false;
  1674. break;
  1675. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  1676. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  1677. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  1678. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  1679. {
  1680. EncodingFlag flag = WTF8;
  1681. if (opcode == WASM_OP_STRING_NEW_WTF16) {
  1682. flag = WTF16;
  1683. }
  1684. else if (opcode == WASM_OP_STRING_NEW_UTF8) {
  1685. flag = UTF8;
  1686. }
  1687. else if (opcode == WASM_OP_STRING_NEW_LOSSY_UTF8) {
  1688. flag = LOSSY_UTF8;
  1689. }
  1690. else if (opcode == WASM_OP_STRING_NEW_WTF8) {
  1691. flag = WTF8;
  1692. }
  1693. if (!aot_compile_op_string_new_array(comp_ctx, func_ctx,
  1694. flag))
  1695. return false;
  1696. break;
  1697. }
  1698. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  1699. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  1700. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  1701. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  1702. {
  1703. EncodingFlag flag = WTF8;
  1704. if (opcode == WASM_OP_STRING_ENCODE_WTF16) {
  1705. flag = WTF16;
  1706. }
  1707. else if (opcode == WASM_OP_STRING_ENCODE_UTF8) {
  1708. flag = UTF8;
  1709. }
  1710. else if (opcode == WASM_OP_STRING_ENCODE_LOSSY_UTF8) {
  1711. flag = LOSSY_UTF8;
  1712. }
  1713. else if (opcode == WASM_OP_STRING_ENCODE_WTF8) {
  1714. flag = WTF8;
  1715. }
  1716. if (!aot_compile_op_string_encode_array(comp_ctx,
  1717. func_ctx, flag))
  1718. return false;
  1719. break;
  1720. }
  1721. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  1722. default:
  1723. aot_set_last_error("unsupported opcode");
  1724. return false;
  1725. }
  1726. break;
  1727. }
  1728. #endif /* end of WASM_ENABLE_GC != 0 */
  1729. case WASM_OP_GET_LOCAL:
  1730. read_leb_uint32(frame_ip, frame_ip_end, local_idx);
  1731. if (!aot_compile_op_get_local(comp_ctx, func_ctx, local_idx))
  1732. return false;
  1733. break;
  1734. case WASM_OP_SET_LOCAL:
  1735. read_leb_uint32(frame_ip, frame_ip_end, local_idx);
  1736. if (!aot_compile_op_set_local(comp_ctx, func_ctx, local_idx))
  1737. return false;
  1738. break;
  1739. case WASM_OP_TEE_LOCAL:
  1740. read_leb_uint32(frame_ip, frame_ip_end, local_idx);
  1741. if (!aot_compile_op_tee_local(comp_ctx, func_ctx, local_idx))
  1742. return false;
  1743. break;
  1744. case WASM_OP_GET_GLOBAL:
  1745. case WASM_OP_GET_GLOBAL_64:
  1746. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  1747. if (!aot_compile_op_get_global(comp_ctx, func_ctx, global_idx))
  1748. return false;
  1749. break;
  1750. case WASM_OP_SET_GLOBAL:
  1751. case WASM_OP_SET_GLOBAL_64:
  1752. case WASM_OP_SET_GLOBAL_AUX_STACK:
  1753. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  1754. if (!aot_compile_op_set_global(
  1755. comp_ctx, func_ctx, global_idx,
  1756. opcode == WASM_OP_SET_GLOBAL_AUX_STACK ? true : false))
  1757. return false;
  1758. break;
  1759. case WASM_OP_I32_LOAD:
  1760. bytes = 4;
  1761. sign = true;
  1762. goto op_i32_load;
  1763. case WASM_OP_I32_LOAD8_S:
  1764. case WASM_OP_I32_LOAD8_U:
  1765. bytes = 1;
  1766. sign = (opcode == WASM_OP_I32_LOAD8_S) ? true : false;
  1767. goto op_i32_load;
  1768. case WASM_OP_I32_LOAD16_S:
  1769. case WASM_OP_I32_LOAD16_U:
  1770. bytes = 2;
  1771. sign = (opcode == WASM_OP_I32_LOAD16_S) ? true : false;
  1772. op_i32_load:
  1773. read_leb_uint32(frame_ip, frame_ip_end, align);
  1774. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  1775. if (!aot_compile_op_i32_load(comp_ctx, func_ctx, align, offset,
  1776. bytes, sign, false))
  1777. return false;
  1778. break;
  1779. case WASM_OP_I64_LOAD:
  1780. bytes = 8;
  1781. sign = true;
  1782. goto op_i64_load;
  1783. case WASM_OP_I64_LOAD8_S:
  1784. case WASM_OP_I64_LOAD8_U:
  1785. bytes = 1;
  1786. sign = (opcode == WASM_OP_I64_LOAD8_S) ? true : false;
  1787. goto op_i64_load;
  1788. case WASM_OP_I64_LOAD16_S:
  1789. case WASM_OP_I64_LOAD16_U:
  1790. bytes = 2;
  1791. sign = (opcode == WASM_OP_I64_LOAD16_S) ? true : false;
  1792. goto op_i64_load;
  1793. case WASM_OP_I64_LOAD32_S:
  1794. case WASM_OP_I64_LOAD32_U:
  1795. bytes = 4;
  1796. sign = (opcode == WASM_OP_I64_LOAD32_S) ? true : false;
  1797. op_i64_load:
  1798. read_leb_uint32(frame_ip, frame_ip_end, align);
  1799. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  1800. if (!aot_compile_op_i64_load(comp_ctx, func_ctx, align, offset,
  1801. bytes, sign, false))
  1802. return false;
  1803. break;
  1804. case WASM_OP_F32_LOAD:
  1805. read_leb_uint32(frame_ip, frame_ip_end, align);
  1806. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  1807. if (!aot_compile_op_f32_load(comp_ctx, func_ctx, align, offset))
  1808. return false;
  1809. break;
  1810. case WASM_OP_F64_LOAD:
  1811. read_leb_uint32(frame_ip, frame_ip_end, align);
  1812. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  1813. if (!aot_compile_op_f64_load(comp_ctx, func_ctx, align, offset))
  1814. return false;
  1815. break;
  1816. case WASM_OP_I32_STORE:
  1817. bytes = 4;
  1818. goto op_i32_store;
  1819. case WASM_OP_I32_STORE8:
  1820. bytes = 1;
  1821. goto op_i32_store;
  1822. case WASM_OP_I32_STORE16:
  1823. bytes = 2;
  1824. op_i32_store:
  1825. read_leb_uint32(frame_ip, frame_ip_end, align);
  1826. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  1827. if (!aot_compile_op_i32_store(comp_ctx, func_ctx, align, offset,
  1828. bytes, false))
  1829. return false;
  1830. break;
  1831. case WASM_OP_I64_STORE:
  1832. bytes = 8;
  1833. goto op_i64_store;
  1834. case WASM_OP_I64_STORE8:
  1835. bytes = 1;
  1836. goto op_i64_store;
  1837. case WASM_OP_I64_STORE16:
  1838. bytes = 2;
  1839. goto op_i64_store;
  1840. case WASM_OP_I64_STORE32:
  1841. bytes = 4;
  1842. op_i64_store:
  1843. read_leb_uint32(frame_ip, frame_ip_end, align);
  1844. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  1845. if (!aot_compile_op_i64_store(comp_ctx, func_ctx, align, offset,
  1846. bytes, false))
  1847. return false;
  1848. break;
  1849. case WASM_OP_F32_STORE:
  1850. read_leb_uint32(frame_ip, frame_ip_end, align);
  1851. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  1852. if (!aot_compile_op_f32_store(comp_ctx, func_ctx, align,
  1853. offset))
  1854. return false;
  1855. break;
  1856. case WASM_OP_F64_STORE:
  1857. read_leb_uint32(frame_ip, frame_ip_end, align);
  1858. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  1859. if (!aot_compile_op_f64_store(comp_ctx, func_ctx, align,
  1860. offset))
  1861. return false;
  1862. break;
  1863. case WASM_OP_MEMORY_SIZE:
  1864. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  1865. if (!aot_compile_op_memory_size(comp_ctx, func_ctx))
  1866. return false;
  1867. (void)mem_idx;
  1868. break;
  1869. case WASM_OP_MEMORY_GROW:
  1870. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  1871. if (!aot_compile_op_memory_grow(comp_ctx, func_ctx))
  1872. return false;
  1873. break;
  1874. case WASM_OP_I32_CONST:
  1875. read_leb_int32(frame_ip, frame_ip_end, i32_const);
  1876. if (!aot_compile_op_i32_const(comp_ctx, func_ctx, i32_const))
  1877. return false;
  1878. break;
  1879. case WASM_OP_I64_CONST:
  1880. read_leb_int64(frame_ip, frame_ip_end, i64_const);
  1881. if (!aot_compile_op_i64_const(comp_ctx, func_ctx, i64_const))
  1882. return false;
  1883. break;
  1884. case WASM_OP_F32_CONST:
  1885. p_f32 = (uint8 *)&f32_const;
  1886. for (i = 0; i < sizeof(float32); i++)
  1887. *p_f32++ = *frame_ip++;
  1888. if (!aot_compile_op_f32_const(comp_ctx, func_ctx, f32_const))
  1889. return false;
  1890. break;
  1891. case WASM_OP_F64_CONST:
  1892. p_f64 = (uint8 *)&f64_const;
  1893. for (i = 0; i < sizeof(float64); i++)
  1894. *p_f64++ = *frame_ip++;
  1895. if (!aot_compile_op_f64_const(comp_ctx, func_ctx, f64_const))
  1896. return false;
  1897. break;
  1898. case WASM_OP_I32_EQZ:
  1899. case WASM_OP_I32_EQ:
  1900. case WASM_OP_I32_NE:
  1901. case WASM_OP_I32_LT_S:
  1902. case WASM_OP_I32_LT_U:
  1903. case WASM_OP_I32_GT_S:
  1904. case WASM_OP_I32_GT_U:
  1905. case WASM_OP_I32_LE_S:
  1906. case WASM_OP_I32_LE_U:
  1907. case WASM_OP_I32_GE_S:
  1908. case WASM_OP_I32_GE_U:
  1909. if (!aot_compile_op_i32_compare(
  1910. comp_ctx, func_ctx, INT_EQZ + opcode - WASM_OP_I32_EQZ))
  1911. return false;
  1912. break;
  1913. case WASM_OP_I64_EQZ:
  1914. case WASM_OP_I64_EQ:
  1915. case WASM_OP_I64_NE:
  1916. case WASM_OP_I64_LT_S:
  1917. case WASM_OP_I64_LT_U:
  1918. case WASM_OP_I64_GT_S:
  1919. case WASM_OP_I64_GT_U:
  1920. case WASM_OP_I64_LE_S:
  1921. case WASM_OP_I64_LE_U:
  1922. case WASM_OP_I64_GE_S:
  1923. case WASM_OP_I64_GE_U:
  1924. if (!aot_compile_op_i64_compare(
  1925. comp_ctx, func_ctx, INT_EQZ + opcode - WASM_OP_I64_EQZ))
  1926. return false;
  1927. break;
  1928. case WASM_OP_F32_EQ:
  1929. case WASM_OP_F32_NE:
  1930. case WASM_OP_F32_LT:
  1931. case WASM_OP_F32_GT:
  1932. case WASM_OP_F32_LE:
  1933. case WASM_OP_F32_GE:
  1934. if (!aot_compile_op_f32_compare(
  1935. comp_ctx, func_ctx, FLOAT_EQ + opcode - WASM_OP_F32_EQ))
  1936. return false;
  1937. break;
  1938. case WASM_OP_F64_EQ:
  1939. case WASM_OP_F64_NE:
  1940. case WASM_OP_F64_LT:
  1941. case WASM_OP_F64_GT:
  1942. case WASM_OP_F64_LE:
  1943. case WASM_OP_F64_GE:
  1944. if (!aot_compile_op_f64_compare(
  1945. comp_ctx, func_ctx, FLOAT_EQ + opcode - WASM_OP_F64_EQ))
  1946. return false;
  1947. break;
  1948. case WASM_OP_I32_CLZ:
  1949. if (!aot_compile_op_i32_clz(comp_ctx, func_ctx))
  1950. return false;
  1951. break;
  1952. case WASM_OP_I32_CTZ:
  1953. if (!aot_compile_op_i32_ctz(comp_ctx, func_ctx))
  1954. return false;
  1955. break;
  1956. case WASM_OP_I32_POPCNT:
  1957. if (!aot_compile_op_i32_popcnt(comp_ctx, func_ctx))
  1958. return false;
  1959. break;
  1960. case WASM_OP_I32_ADD:
  1961. case WASM_OP_I32_SUB:
  1962. case WASM_OP_I32_MUL:
  1963. case WASM_OP_I32_DIV_S:
  1964. case WASM_OP_I32_DIV_U:
  1965. case WASM_OP_I32_REM_S:
  1966. case WASM_OP_I32_REM_U:
  1967. if (!aot_compile_op_i32_arithmetic(
  1968. comp_ctx, func_ctx, INT_ADD + opcode - WASM_OP_I32_ADD,
  1969. &frame_ip))
  1970. return false;
  1971. break;
  1972. case WASM_OP_I32_AND:
  1973. case WASM_OP_I32_OR:
  1974. case WASM_OP_I32_XOR:
  1975. if (!aot_compile_op_i32_bitwise(
  1976. comp_ctx, func_ctx, INT_SHL + opcode - WASM_OP_I32_AND))
  1977. return false;
  1978. break;
  1979. case WASM_OP_I32_SHL:
  1980. case WASM_OP_I32_SHR_S:
  1981. case WASM_OP_I32_SHR_U:
  1982. case WASM_OP_I32_ROTL:
  1983. case WASM_OP_I32_ROTR:
  1984. if (!aot_compile_op_i32_shift(
  1985. comp_ctx, func_ctx, INT_SHL + opcode - WASM_OP_I32_SHL))
  1986. return false;
  1987. break;
  1988. case WASM_OP_I64_CLZ:
  1989. if (!aot_compile_op_i64_clz(comp_ctx, func_ctx))
  1990. return false;
  1991. break;
  1992. case WASM_OP_I64_CTZ:
  1993. if (!aot_compile_op_i64_ctz(comp_ctx, func_ctx))
  1994. return false;
  1995. break;
  1996. case WASM_OP_I64_POPCNT:
  1997. if (!aot_compile_op_i64_popcnt(comp_ctx, func_ctx))
  1998. return false;
  1999. break;
  2000. case WASM_OP_I64_ADD:
  2001. case WASM_OP_I64_SUB:
  2002. case WASM_OP_I64_MUL:
  2003. case WASM_OP_I64_DIV_S:
  2004. case WASM_OP_I64_DIV_U:
  2005. case WASM_OP_I64_REM_S:
  2006. case WASM_OP_I64_REM_U:
  2007. if (!aot_compile_op_i64_arithmetic(
  2008. comp_ctx, func_ctx, INT_ADD + opcode - WASM_OP_I64_ADD,
  2009. &frame_ip))
  2010. return false;
  2011. break;
  2012. case WASM_OP_I64_AND:
  2013. case WASM_OP_I64_OR:
  2014. case WASM_OP_I64_XOR:
  2015. if (!aot_compile_op_i64_bitwise(
  2016. comp_ctx, func_ctx, INT_SHL + opcode - WASM_OP_I64_AND))
  2017. return false;
  2018. break;
  2019. case WASM_OP_I64_SHL:
  2020. case WASM_OP_I64_SHR_S:
  2021. case WASM_OP_I64_SHR_U:
  2022. case WASM_OP_I64_ROTL:
  2023. case WASM_OP_I64_ROTR:
  2024. if (!aot_compile_op_i64_shift(
  2025. comp_ctx, func_ctx, INT_SHL + opcode - WASM_OP_I64_SHL))
  2026. return false;
  2027. break;
  2028. case WASM_OP_F32_ABS:
  2029. case WASM_OP_F32_NEG:
  2030. case WASM_OP_F32_CEIL:
  2031. case WASM_OP_F32_FLOOR:
  2032. case WASM_OP_F32_TRUNC:
  2033. case WASM_OP_F32_NEAREST:
  2034. case WASM_OP_F32_SQRT:
  2035. if (!aot_compile_op_f32_math(comp_ctx, func_ctx,
  2036. FLOAT_ABS + opcode
  2037. - WASM_OP_F32_ABS))
  2038. return false;
  2039. break;
  2040. case WASM_OP_F32_ADD:
  2041. case WASM_OP_F32_SUB:
  2042. case WASM_OP_F32_MUL:
  2043. case WASM_OP_F32_DIV:
  2044. case WASM_OP_F32_MIN:
  2045. case WASM_OP_F32_MAX:
  2046. if (!aot_compile_op_f32_arithmetic(comp_ctx, func_ctx,
  2047. FLOAT_ADD + opcode
  2048. - WASM_OP_F32_ADD))
  2049. return false;
  2050. break;
  2051. case WASM_OP_F32_COPYSIGN:
  2052. if (!aot_compile_op_f32_copysign(comp_ctx, func_ctx))
  2053. return false;
  2054. break;
  2055. case WASM_OP_F64_ABS:
  2056. case WASM_OP_F64_NEG:
  2057. case WASM_OP_F64_CEIL:
  2058. case WASM_OP_F64_FLOOR:
  2059. case WASM_OP_F64_TRUNC:
  2060. case WASM_OP_F64_NEAREST:
  2061. case WASM_OP_F64_SQRT:
  2062. if (!aot_compile_op_f64_math(comp_ctx, func_ctx,
  2063. FLOAT_ABS + opcode
  2064. - WASM_OP_F64_ABS))
  2065. return false;
  2066. break;
  2067. case WASM_OP_F64_ADD:
  2068. case WASM_OP_F64_SUB:
  2069. case WASM_OP_F64_MUL:
  2070. case WASM_OP_F64_DIV:
  2071. case WASM_OP_F64_MIN:
  2072. case WASM_OP_F64_MAX:
  2073. if (!aot_compile_op_f64_arithmetic(comp_ctx, func_ctx,
  2074. FLOAT_ADD + opcode
  2075. - WASM_OP_F64_ADD))
  2076. return false;
  2077. break;
  2078. case WASM_OP_F64_COPYSIGN:
  2079. if (!aot_compile_op_f64_copysign(comp_ctx, func_ctx))
  2080. return false;
  2081. break;
  2082. case WASM_OP_I32_WRAP_I64:
  2083. if (!aot_compile_op_i32_wrap_i64(comp_ctx, func_ctx))
  2084. return false;
  2085. break;
  2086. case WASM_OP_I32_TRUNC_S_F32:
  2087. case WASM_OP_I32_TRUNC_U_F32:
  2088. sign = (opcode == WASM_OP_I32_TRUNC_S_F32) ? true : false;
  2089. if (!aot_compile_op_i32_trunc_f32(comp_ctx, func_ctx, sign,
  2090. false))
  2091. return false;
  2092. break;
  2093. case WASM_OP_I32_TRUNC_S_F64:
  2094. case WASM_OP_I32_TRUNC_U_F64:
  2095. sign = (opcode == WASM_OP_I32_TRUNC_S_F64) ? true : false;
  2096. if (!aot_compile_op_i32_trunc_f64(comp_ctx, func_ctx, sign,
  2097. false))
  2098. return false;
  2099. break;
  2100. case WASM_OP_I64_EXTEND_S_I32:
  2101. case WASM_OP_I64_EXTEND_U_I32:
  2102. sign = (opcode == WASM_OP_I64_EXTEND_S_I32) ? true : false;
  2103. if (!aot_compile_op_i64_extend_i32(comp_ctx, func_ctx, sign))
  2104. return false;
  2105. break;
  2106. case WASM_OP_I64_TRUNC_S_F32:
  2107. case WASM_OP_I64_TRUNC_U_F32:
  2108. sign = (opcode == WASM_OP_I64_TRUNC_S_F32) ? true : false;
  2109. if (!aot_compile_op_i64_trunc_f32(comp_ctx, func_ctx, sign,
  2110. false))
  2111. return false;
  2112. break;
  2113. case WASM_OP_I64_TRUNC_S_F64:
  2114. case WASM_OP_I64_TRUNC_U_F64:
  2115. sign = (opcode == WASM_OP_I64_TRUNC_S_F64) ? true : false;
  2116. if (!aot_compile_op_i64_trunc_f64(comp_ctx, func_ctx, sign,
  2117. false))
  2118. return false;
  2119. break;
  2120. case WASM_OP_F32_CONVERT_S_I32:
  2121. case WASM_OP_F32_CONVERT_U_I32:
  2122. sign = (opcode == WASM_OP_F32_CONVERT_S_I32) ? true : false;
  2123. if (!aot_compile_op_f32_convert_i32(comp_ctx, func_ctx, sign))
  2124. return false;
  2125. break;
  2126. case WASM_OP_F32_CONVERT_S_I64:
  2127. case WASM_OP_F32_CONVERT_U_I64:
  2128. sign = (opcode == WASM_OP_F32_CONVERT_S_I64) ? true : false;
  2129. if (!aot_compile_op_f32_convert_i64(comp_ctx, func_ctx, sign))
  2130. return false;
  2131. break;
  2132. case WASM_OP_F32_DEMOTE_F64:
  2133. if (!aot_compile_op_f32_demote_f64(comp_ctx, func_ctx))
  2134. return false;
  2135. break;
  2136. case WASM_OP_F64_CONVERT_S_I32:
  2137. case WASM_OP_F64_CONVERT_U_I32:
  2138. sign = (opcode == WASM_OP_F64_CONVERT_S_I32) ? true : false;
  2139. if (!aot_compile_op_f64_convert_i32(comp_ctx, func_ctx, sign))
  2140. return false;
  2141. break;
  2142. case WASM_OP_F64_CONVERT_S_I64:
  2143. case WASM_OP_F64_CONVERT_U_I64:
  2144. sign = (opcode == WASM_OP_F64_CONVERT_S_I64) ? true : false;
  2145. if (!aot_compile_op_f64_convert_i64(comp_ctx, func_ctx, sign))
  2146. return false;
  2147. break;
  2148. case WASM_OP_F64_PROMOTE_F32:
  2149. if (!aot_compile_op_f64_promote_f32(comp_ctx, func_ctx))
  2150. return false;
  2151. break;
  2152. case WASM_OP_I32_REINTERPRET_F32:
  2153. if (!aot_compile_op_i32_reinterpret_f32(comp_ctx, func_ctx))
  2154. return false;
  2155. break;
  2156. case WASM_OP_I64_REINTERPRET_F64:
  2157. if (!aot_compile_op_i64_reinterpret_f64(comp_ctx, func_ctx))
  2158. return false;
  2159. break;
  2160. case WASM_OP_F32_REINTERPRET_I32:
  2161. if (!aot_compile_op_f32_reinterpret_i32(comp_ctx, func_ctx))
  2162. return false;
  2163. break;
  2164. case WASM_OP_F64_REINTERPRET_I64:
  2165. if (!aot_compile_op_f64_reinterpret_i64(comp_ctx, func_ctx))
  2166. return false;
  2167. break;
  2168. case WASM_OP_I32_EXTEND8_S:
  2169. if (!aot_compile_op_i32_extend_i32(comp_ctx, func_ctx, 8))
  2170. return false;
  2171. break;
  2172. case WASM_OP_I32_EXTEND16_S:
  2173. if (!aot_compile_op_i32_extend_i32(comp_ctx, func_ctx, 16))
  2174. return false;
  2175. break;
  2176. case WASM_OP_I64_EXTEND8_S:
  2177. if (!aot_compile_op_i64_extend_i64(comp_ctx, func_ctx, 8))
  2178. return false;
  2179. break;
  2180. case WASM_OP_I64_EXTEND16_S:
  2181. if (!aot_compile_op_i64_extend_i64(comp_ctx, func_ctx, 16))
  2182. return false;
  2183. break;
  2184. case WASM_OP_I64_EXTEND32_S:
  2185. if (!aot_compile_op_i64_extend_i64(comp_ctx, func_ctx, 32))
  2186. return false;
  2187. break;
  2188. case WASM_OP_MISC_PREFIX:
  2189. {
  2190. uint32 opcode1;
  2191. read_leb_uint32(frame_ip, frame_ip_end, opcode1);
  2192. /* opcode1 was checked in loader and is no larger than
  2193. UINT8_MAX */
  2194. opcode = (uint8)opcode1;
  2195. #if WASM_ENABLE_BULK_MEMORY != 0
  2196. if (WASM_OP_MEMORY_INIT <= opcode
  2197. && opcode <= WASM_OP_MEMORY_FILL
  2198. && !comp_ctx->enable_bulk_memory) {
  2199. goto unsupport_bulk_memory;
  2200. }
  2201. #endif
  2202. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2203. if (WASM_OP_TABLE_INIT <= opcode && opcode <= WASM_OP_TABLE_FILL
  2204. && (!comp_ctx->enable_ref_types && !comp_ctx->enable_gc)) {
  2205. goto unsupport_ref_types;
  2206. }
  2207. #endif
  2208. switch (opcode) {
  2209. case WASM_OP_I32_TRUNC_SAT_S_F32:
  2210. case WASM_OP_I32_TRUNC_SAT_U_F32:
  2211. sign = (opcode == WASM_OP_I32_TRUNC_SAT_S_F32) ? true
  2212. : false;
  2213. if (!aot_compile_op_i32_trunc_f32(comp_ctx, func_ctx,
  2214. sign, true))
  2215. return false;
  2216. break;
  2217. case WASM_OP_I32_TRUNC_SAT_S_F64:
  2218. case WASM_OP_I32_TRUNC_SAT_U_F64:
  2219. sign = (opcode == WASM_OP_I32_TRUNC_SAT_S_F64) ? true
  2220. : false;
  2221. if (!aot_compile_op_i32_trunc_f64(comp_ctx, func_ctx,
  2222. sign, true))
  2223. return false;
  2224. break;
  2225. case WASM_OP_I64_TRUNC_SAT_S_F32:
  2226. case WASM_OP_I64_TRUNC_SAT_U_F32:
  2227. sign = (opcode == WASM_OP_I64_TRUNC_SAT_S_F32) ? true
  2228. : false;
  2229. if (!aot_compile_op_i64_trunc_f32(comp_ctx, func_ctx,
  2230. sign, true))
  2231. return false;
  2232. break;
  2233. case WASM_OP_I64_TRUNC_SAT_S_F64:
  2234. case WASM_OP_I64_TRUNC_SAT_U_F64:
  2235. sign = (opcode == WASM_OP_I64_TRUNC_SAT_S_F64) ? true
  2236. : false;
  2237. if (!aot_compile_op_i64_trunc_f64(comp_ctx, func_ctx,
  2238. sign, true))
  2239. return false;
  2240. break;
  2241. #if WASM_ENABLE_BULK_MEMORY != 0
  2242. case WASM_OP_MEMORY_INIT:
  2243. {
  2244. uint32 seg_index;
  2245. read_leb_uint32(frame_ip, frame_ip_end, seg_index);
  2246. frame_ip++;
  2247. if (!aot_compile_op_memory_init(comp_ctx, func_ctx,
  2248. seg_index))
  2249. return false;
  2250. break;
  2251. }
  2252. case WASM_OP_DATA_DROP:
  2253. {
  2254. uint32 seg_index;
  2255. read_leb_uint32(frame_ip, frame_ip_end, seg_index);
  2256. if (!aot_compile_op_data_drop(comp_ctx, func_ctx,
  2257. seg_index))
  2258. return false;
  2259. break;
  2260. }
  2261. case WASM_OP_MEMORY_COPY:
  2262. {
  2263. frame_ip += 2;
  2264. if (!aot_compile_op_memory_copy(comp_ctx, func_ctx))
  2265. return false;
  2266. break;
  2267. }
  2268. case WASM_OP_MEMORY_FILL:
  2269. {
  2270. frame_ip++;
  2271. if (!aot_compile_op_memory_fill(comp_ctx, func_ctx))
  2272. return false;
  2273. break;
  2274. }
  2275. #endif /* WASM_ENABLE_BULK_MEMORY */
  2276. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2277. case WASM_OP_TABLE_INIT:
  2278. {
  2279. uint32 tbl_idx, tbl_seg_idx;
  2280. read_leb_uint32(frame_ip, frame_ip_end, tbl_seg_idx);
  2281. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  2282. if (!aot_compile_op_table_init(comp_ctx, func_ctx,
  2283. tbl_idx, tbl_seg_idx))
  2284. return false;
  2285. break;
  2286. }
  2287. case WASM_OP_ELEM_DROP:
  2288. {
  2289. uint32 tbl_seg_idx;
  2290. read_leb_uint32(frame_ip, frame_ip_end, tbl_seg_idx);
  2291. if (!aot_compile_op_elem_drop(comp_ctx, func_ctx,
  2292. tbl_seg_idx))
  2293. return false;
  2294. break;
  2295. }
  2296. case WASM_OP_TABLE_COPY:
  2297. {
  2298. uint32 src_tbl_idx, dst_tbl_idx;
  2299. read_leb_uint32(frame_ip, frame_ip_end, dst_tbl_idx);
  2300. read_leb_uint32(frame_ip, frame_ip_end, src_tbl_idx);
  2301. if (!aot_compile_op_table_copy(
  2302. comp_ctx, func_ctx, src_tbl_idx, dst_tbl_idx))
  2303. return false;
  2304. break;
  2305. }
  2306. case WASM_OP_TABLE_GROW:
  2307. {
  2308. uint32 tbl_idx;
  2309. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  2310. if (!aot_compile_op_table_grow(comp_ctx, func_ctx,
  2311. tbl_idx))
  2312. return false;
  2313. break;
  2314. }
  2315. case WASM_OP_TABLE_SIZE:
  2316. {
  2317. uint32 tbl_idx;
  2318. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  2319. if (!aot_compile_op_table_size(comp_ctx, func_ctx,
  2320. tbl_idx))
  2321. return false;
  2322. break;
  2323. }
  2324. case WASM_OP_TABLE_FILL:
  2325. {
  2326. uint32 tbl_idx;
  2327. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  2328. if (!aot_compile_op_table_fill(comp_ctx, func_ctx,
  2329. tbl_idx))
  2330. return false;
  2331. break;
  2332. }
  2333. #endif /* WASM_ENABLE_REF_TYPES || WASM_ENABLE_GC */
  2334. default:
  2335. aot_set_last_error("unsupported opcode");
  2336. return false;
  2337. }
  2338. break;
  2339. }
  2340. #if WASM_ENABLE_SHARED_MEMORY != 0
  2341. case WASM_OP_ATOMIC_PREFIX:
  2342. {
  2343. uint8 bin_op, op_type;
  2344. uint32 opcode1;
  2345. read_leb_uint32(frame_ip, frame_ip_end, opcode1);
  2346. /* opcode1 was checked in loader and is no larger than
  2347. UINT8_MAX */
  2348. opcode = (uint8)opcode1;
  2349. if (opcode != WASM_OP_ATOMIC_FENCE) {
  2350. read_leb_uint32(frame_ip, frame_ip_end, align);
  2351. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  2352. }
  2353. switch (opcode) {
  2354. case WASM_OP_ATOMIC_WAIT32:
  2355. if (!aot_compile_op_atomic_wait(comp_ctx, func_ctx,
  2356. VALUE_TYPE_I32, align,
  2357. offset, 4))
  2358. return false;
  2359. break;
  2360. case WASM_OP_ATOMIC_WAIT64:
  2361. if (!aot_compile_op_atomic_wait(comp_ctx, func_ctx,
  2362. VALUE_TYPE_I64, align,
  2363. offset, 8))
  2364. return false;
  2365. break;
  2366. case WASM_OP_ATOMIC_NOTIFY:
  2367. if (!aot_compiler_op_atomic_notify(
  2368. comp_ctx, func_ctx, align, offset, bytes))
  2369. return false;
  2370. break;
  2371. case WASM_OP_ATOMIC_FENCE:
  2372. /* Skip memory index */
  2373. frame_ip++;
  2374. if (!aot_compiler_op_atomic_fence(comp_ctx, func_ctx))
  2375. return false;
  2376. break;
  2377. case WASM_OP_ATOMIC_I32_LOAD:
  2378. bytes = 4;
  2379. goto op_atomic_i32_load;
  2380. case WASM_OP_ATOMIC_I32_LOAD8_U:
  2381. bytes = 1;
  2382. goto op_atomic_i32_load;
  2383. case WASM_OP_ATOMIC_I32_LOAD16_U:
  2384. bytes = 2;
  2385. op_atomic_i32_load:
  2386. if (!aot_compile_op_i32_load(comp_ctx, func_ctx, align,
  2387. offset, bytes, sign, true))
  2388. return false;
  2389. break;
  2390. case WASM_OP_ATOMIC_I64_LOAD:
  2391. bytes = 8;
  2392. goto op_atomic_i64_load;
  2393. case WASM_OP_ATOMIC_I64_LOAD8_U:
  2394. bytes = 1;
  2395. goto op_atomic_i64_load;
  2396. case WASM_OP_ATOMIC_I64_LOAD16_U:
  2397. bytes = 2;
  2398. goto op_atomic_i64_load;
  2399. case WASM_OP_ATOMIC_I64_LOAD32_U:
  2400. bytes = 4;
  2401. op_atomic_i64_load:
  2402. if (!aot_compile_op_i64_load(comp_ctx, func_ctx, align,
  2403. offset, bytes, sign, true))
  2404. return false;
  2405. break;
  2406. case WASM_OP_ATOMIC_I32_STORE:
  2407. bytes = 4;
  2408. goto op_atomic_i32_store;
  2409. case WASM_OP_ATOMIC_I32_STORE8:
  2410. bytes = 1;
  2411. goto op_atomic_i32_store;
  2412. case WASM_OP_ATOMIC_I32_STORE16:
  2413. bytes = 2;
  2414. op_atomic_i32_store:
  2415. if (!aot_compile_op_i32_store(comp_ctx, func_ctx, align,
  2416. offset, bytes, true))
  2417. return false;
  2418. break;
  2419. case WASM_OP_ATOMIC_I64_STORE:
  2420. bytes = 8;
  2421. goto op_atomic_i64_store;
  2422. case WASM_OP_ATOMIC_I64_STORE8:
  2423. bytes = 1;
  2424. goto op_atomic_i64_store;
  2425. case WASM_OP_ATOMIC_I64_STORE16:
  2426. bytes = 2;
  2427. goto op_atomic_i64_store;
  2428. case WASM_OP_ATOMIC_I64_STORE32:
  2429. bytes = 4;
  2430. op_atomic_i64_store:
  2431. if (!aot_compile_op_i64_store(comp_ctx, func_ctx, align,
  2432. offset, bytes, true))
  2433. return false;
  2434. break;
  2435. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  2436. bytes = 4;
  2437. op_type = VALUE_TYPE_I32;
  2438. goto op_atomic_cmpxchg;
  2439. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  2440. bytes = 8;
  2441. op_type = VALUE_TYPE_I64;
  2442. goto op_atomic_cmpxchg;
  2443. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  2444. bytes = 1;
  2445. op_type = VALUE_TYPE_I32;
  2446. goto op_atomic_cmpxchg;
  2447. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  2448. bytes = 2;
  2449. op_type = VALUE_TYPE_I32;
  2450. goto op_atomic_cmpxchg;
  2451. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  2452. bytes = 1;
  2453. op_type = VALUE_TYPE_I64;
  2454. goto op_atomic_cmpxchg;
  2455. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  2456. bytes = 2;
  2457. op_type = VALUE_TYPE_I64;
  2458. goto op_atomic_cmpxchg;
  2459. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  2460. bytes = 4;
  2461. op_type = VALUE_TYPE_I64;
  2462. op_atomic_cmpxchg:
  2463. if (!aot_compile_op_atomic_cmpxchg(comp_ctx, func_ctx,
  2464. op_type, align,
  2465. offset, bytes))
  2466. return false;
  2467. break;
  2468. COMPILE_ATOMIC_RMW(Add, ADD);
  2469. COMPILE_ATOMIC_RMW(Sub, SUB);
  2470. COMPILE_ATOMIC_RMW(And, AND);
  2471. COMPILE_ATOMIC_RMW(Or, OR);
  2472. COMPILE_ATOMIC_RMW(Xor, XOR);
  2473. COMPILE_ATOMIC_RMW(Xchg, XCHG);
  2474. build_atomic_rmw:
  2475. if (!aot_compile_op_atomic_rmw(comp_ctx, func_ctx,
  2476. bin_op, op_type, align,
  2477. offset, bytes))
  2478. return false;
  2479. break;
  2480. default:
  2481. aot_set_last_error("unsupported opcode");
  2482. return false;
  2483. }
  2484. break;
  2485. }
  2486. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  2487. #if WASM_ENABLE_SIMD != 0
  2488. case WASM_OP_SIMD_PREFIX:
  2489. {
  2490. uint32 opcode1;
  2491. if (!comp_ctx->enable_simd) {
  2492. goto unsupport_simd;
  2493. }
  2494. read_leb_uint32(frame_ip, frame_ip_end, opcode1);
  2495. /* opcode1 was checked in loader and is no larger than
  2496. UINT8_MAX */
  2497. opcode = (uint8)opcode1;
  2498. /* follow the order of enum WASMSimdEXTOpcode in
  2499. wasm_opcode.h */
  2500. switch (opcode) {
  2501. /* Memory instruction */
  2502. case SIMD_v128_load:
  2503. {
  2504. read_leb_uint32(frame_ip, frame_ip_end, align);
  2505. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  2506. if (!aot_compile_simd_v128_load(comp_ctx, func_ctx,
  2507. align, offset))
  2508. return false;
  2509. break;
  2510. }
  2511. case SIMD_v128_load8x8_s:
  2512. case SIMD_v128_load8x8_u:
  2513. case SIMD_v128_load16x4_s:
  2514. case SIMD_v128_load16x4_u:
  2515. case SIMD_v128_load32x2_s:
  2516. case SIMD_v128_load32x2_u:
  2517. {
  2518. read_leb_uint32(frame_ip, frame_ip_end, align);
  2519. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  2520. if (!aot_compile_simd_load_extend(
  2521. comp_ctx, func_ctx, opcode, align, offset))
  2522. return false;
  2523. break;
  2524. }
  2525. case SIMD_v128_load8_splat:
  2526. case SIMD_v128_load16_splat:
  2527. case SIMD_v128_load32_splat:
  2528. case SIMD_v128_load64_splat:
  2529. {
  2530. read_leb_uint32(frame_ip, frame_ip_end, align);
  2531. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  2532. if (!aot_compile_simd_load_splat(comp_ctx, func_ctx,
  2533. opcode, align, offset))
  2534. return false;
  2535. break;
  2536. }
  2537. case SIMD_v128_store:
  2538. {
  2539. read_leb_uint32(frame_ip, frame_ip_end, align);
  2540. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  2541. if (!aot_compile_simd_v128_store(comp_ctx, func_ctx,
  2542. align, offset))
  2543. return false;
  2544. break;
  2545. }
  2546. /* Basic operation */
  2547. case SIMD_v128_const:
  2548. {
  2549. if (!aot_compile_simd_v128_const(comp_ctx, func_ctx,
  2550. frame_ip))
  2551. return false;
  2552. frame_ip += 16;
  2553. break;
  2554. }
  2555. case SIMD_v8x16_shuffle:
  2556. {
  2557. if (!aot_compile_simd_shuffle(comp_ctx, func_ctx,
  2558. frame_ip))
  2559. return false;
  2560. frame_ip += 16;
  2561. break;
  2562. }
  2563. case SIMD_v8x16_swizzle:
  2564. {
  2565. if (!aot_compile_simd_swizzle(comp_ctx, func_ctx))
  2566. return false;
  2567. break;
  2568. }
  2569. /* Splat operation */
  2570. case SIMD_i8x16_splat:
  2571. case SIMD_i16x8_splat:
  2572. case SIMD_i32x4_splat:
  2573. case SIMD_i64x2_splat:
  2574. case SIMD_f32x4_splat:
  2575. case SIMD_f64x2_splat:
  2576. {
  2577. if (!aot_compile_simd_splat(comp_ctx, func_ctx, opcode))
  2578. return false;
  2579. break;
  2580. }
  2581. /* Lane operation */
  2582. case SIMD_i8x16_extract_lane_s:
  2583. case SIMD_i8x16_extract_lane_u:
  2584. {
  2585. if (!aot_compile_simd_extract_i8x16(
  2586. comp_ctx, func_ctx, *frame_ip++,
  2587. SIMD_i8x16_extract_lane_s == opcode))
  2588. return false;
  2589. break;
  2590. }
  2591. case SIMD_i8x16_replace_lane:
  2592. {
  2593. if (!aot_compile_simd_replace_i8x16(comp_ctx, func_ctx,
  2594. *frame_ip++))
  2595. return false;
  2596. break;
  2597. }
  2598. case SIMD_i16x8_extract_lane_s:
  2599. case SIMD_i16x8_extract_lane_u:
  2600. {
  2601. if (!aot_compile_simd_extract_i16x8(
  2602. comp_ctx, func_ctx, *frame_ip++,
  2603. SIMD_i16x8_extract_lane_s == opcode))
  2604. return false;
  2605. break;
  2606. }
  2607. case SIMD_i16x8_replace_lane:
  2608. {
  2609. if (!aot_compile_simd_replace_i16x8(comp_ctx, func_ctx,
  2610. *frame_ip++))
  2611. return false;
  2612. break;
  2613. }
  2614. case SIMD_i32x4_extract_lane:
  2615. {
  2616. if (!aot_compile_simd_extract_i32x4(comp_ctx, func_ctx,
  2617. *frame_ip++))
  2618. return false;
  2619. break;
  2620. }
  2621. case SIMD_i32x4_replace_lane:
  2622. {
  2623. if (!aot_compile_simd_replace_i32x4(comp_ctx, func_ctx,
  2624. *frame_ip++))
  2625. return false;
  2626. break;
  2627. }
  2628. case SIMD_i64x2_extract_lane:
  2629. {
  2630. if (!aot_compile_simd_extract_i64x2(comp_ctx, func_ctx,
  2631. *frame_ip++))
  2632. return false;
  2633. break;
  2634. }
  2635. case SIMD_i64x2_replace_lane:
  2636. {
  2637. if (!aot_compile_simd_replace_i64x2(comp_ctx, func_ctx,
  2638. *frame_ip++))
  2639. return false;
  2640. break;
  2641. }
  2642. case SIMD_f32x4_extract_lane:
  2643. {
  2644. if (!aot_compile_simd_extract_f32x4(comp_ctx, func_ctx,
  2645. *frame_ip++))
  2646. return false;
  2647. break;
  2648. }
  2649. case SIMD_f32x4_replace_lane:
  2650. {
  2651. if (!aot_compile_simd_replace_f32x4(comp_ctx, func_ctx,
  2652. *frame_ip++))
  2653. return false;
  2654. break;
  2655. }
  2656. case SIMD_f64x2_extract_lane:
  2657. {
  2658. if (!aot_compile_simd_extract_f64x2(comp_ctx, func_ctx,
  2659. *frame_ip++))
  2660. return false;
  2661. break;
  2662. }
  2663. case SIMD_f64x2_replace_lane:
  2664. {
  2665. if (!aot_compile_simd_replace_f64x2(comp_ctx, func_ctx,
  2666. *frame_ip++))
  2667. return false;
  2668. break;
  2669. }
  2670. /* i8x16 Cmp */
  2671. case SIMD_i8x16_eq:
  2672. case SIMD_i8x16_ne:
  2673. case SIMD_i8x16_lt_s:
  2674. case SIMD_i8x16_lt_u:
  2675. case SIMD_i8x16_gt_s:
  2676. case SIMD_i8x16_gt_u:
  2677. case SIMD_i8x16_le_s:
  2678. case SIMD_i8x16_le_u:
  2679. case SIMD_i8x16_ge_s:
  2680. case SIMD_i8x16_ge_u:
  2681. {
  2682. if (!aot_compile_simd_i8x16_compare(
  2683. comp_ctx, func_ctx,
  2684. INT_EQ + opcode - SIMD_i8x16_eq))
  2685. return false;
  2686. break;
  2687. }
  2688. /* i16x8 Cmp */
  2689. case SIMD_i16x8_eq:
  2690. case SIMD_i16x8_ne:
  2691. case SIMD_i16x8_lt_s:
  2692. case SIMD_i16x8_lt_u:
  2693. case SIMD_i16x8_gt_s:
  2694. case SIMD_i16x8_gt_u:
  2695. case SIMD_i16x8_le_s:
  2696. case SIMD_i16x8_le_u:
  2697. case SIMD_i16x8_ge_s:
  2698. case SIMD_i16x8_ge_u:
  2699. {
  2700. if (!aot_compile_simd_i16x8_compare(
  2701. comp_ctx, func_ctx,
  2702. INT_EQ + opcode - SIMD_i16x8_eq))
  2703. return false;
  2704. break;
  2705. }
  2706. /* i32x4 Cmp */
  2707. case SIMD_i32x4_eq:
  2708. case SIMD_i32x4_ne:
  2709. case SIMD_i32x4_lt_s:
  2710. case SIMD_i32x4_lt_u:
  2711. case SIMD_i32x4_gt_s:
  2712. case SIMD_i32x4_gt_u:
  2713. case SIMD_i32x4_le_s:
  2714. case SIMD_i32x4_le_u:
  2715. case SIMD_i32x4_ge_s:
  2716. case SIMD_i32x4_ge_u:
  2717. {
  2718. if (!aot_compile_simd_i32x4_compare(
  2719. comp_ctx, func_ctx,
  2720. INT_EQ + opcode - SIMD_i32x4_eq))
  2721. return false;
  2722. break;
  2723. }
  2724. /* f32x4 Cmp */
  2725. case SIMD_f32x4_eq:
  2726. case SIMD_f32x4_ne:
  2727. case SIMD_f32x4_lt:
  2728. case SIMD_f32x4_gt:
  2729. case SIMD_f32x4_le:
  2730. case SIMD_f32x4_ge:
  2731. {
  2732. if (!aot_compile_simd_f32x4_compare(
  2733. comp_ctx, func_ctx,
  2734. FLOAT_EQ + opcode - SIMD_f32x4_eq))
  2735. return false;
  2736. break;
  2737. }
  2738. /* f64x2 Cmp */
  2739. case SIMD_f64x2_eq:
  2740. case SIMD_f64x2_ne:
  2741. case SIMD_f64x2_lt:
  2742. case SIMD_f64x2_gt:
  2743. case SIMD_f64x2_le:
  2744. case SIMD_f64x2_ge:
  2745. {
  2746. if (!aot_compile_simd_f64x2_compare(
  2747. comp_ctx, func_ctx,
  2748. FLOAT_EQ + opcode - SIMD_f64x2_eq))
  2749. return false;
  2750. break;
  2751. }
  2752. /* v128 Op */
  2753. case SIMD_v128_not:
  2754. case SIMD_v128_and:
  2755. case SIMD_v128_andnot:
  2756. case SIMD_v128_or:
  2757. case SIMD_v128_xor:
  2758. case SIMD_v128_bitselect:
  2759. {
  2760. if (!aot_compile_simd_v128_bitwise(comp_ctx, func_ctx,
  2761. V128_NOT + opcode
  2762. - SIMD_v128_not))
  2763. return false;
  2764. break;
  2765. }
  2766. case SIMD_v128_any_true:
  2767. {
  2768. if (!aot_compile_simd_v128_any_true(comp_ctx, func_ctx))
  2769. return false;
  2770. break;
  2771. }
  2772. /* Load Lane Op */
  2773. case SIMD_v128_load8_lane:
  2774. case SIMD_v128_load16_lane:
  2775. case SIMD_v128_load32_lane:
  2776. case SIMD_v128_load64_lane:
  2777. {
  2778. read_leb_uint32(frame_ip, frame_ip_end, align);
  2779. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  2780. if (!aot_compile_simd_load_lane(comp_ctx, func_ctx,
  2781. opcode, align, offset,
  2782. *frame_ip++))
  2783. return false;
  2784. break;
  2785. }
  2786. case SIMD_v128_store8_lane:
  2787. case SIMD_v128_store16_lane:
  2788. case SIMD_v128_store32_lane:
  2789. case SIMD_v128_store64_lane:
  2790. {
  2791. read_leb_uint32(frame_ip, frame_ip_end, align);
  2792. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  2793. if (!aot_compile_simd_store_lane(comp_ctx, func_ctx,
  2794. opcode, align, offset,
  2795. *frame_ip++))
  2796. return false;
  2797. break;
  2798. }
  2799. case SIMD_v128_load32_zero:
  2800. case SIMD_v128_load64_zero:
  2801. {
  2802. read_leb_uint32(frame_ip, frame_ip_end, align);
  2803. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  2804. if (!aot_compile_simd_load_zero(comp_ctx, func_ctx,
  2805. opcode, align, offset))
  2806. return false;
  2807. break;
  2808. }
  2809. /* Float conversion */
  2810. case SIMD_f32x4_demote_f64x2_zero:
  2811. {
  2812. if (!aot_compile_simd_f64x2_demote(comp_ctx, func_ctx))
  2813. return false;
  2814. break;
  2815. }
  2816. case SIMD_f64x2_promote_low_f32x4_zero:
  2817. {
  2818. if (!aot_compile_simd_f32x4_promote(comp_ctx, func_ctx))
  2819. return false;
  2820. break;
  2821. }
  2822. /* i8x16 Op */
  2823. case SIMD_i8x16_abs:
  2824. {
  2825. if (!aot_compile_simd_i8x16_abs(comp_ctx, func_ctx))
  2826. return false;
  2827. break;
  2828. }
  2829. case SIMD_i8x16_neg:
  2830. {
  2831. if (!aot_compile_simd_i8x16_neg(comp_ctx, func_ctx))
  2832. return false;
  2833. break;
  2834. }
  2835. case SIMD_i8x16_popcnt:
  2836. {
  2837. if (!aot_compile_simd_i8x16_popcnt(comp_ctx, func_ctx))
  2838. return false;
  2839. break;
  2840. }
  2841. case SIMD_i8x16_all_true:
  2842. {
  2843. if (!aot_compile_simd_i8x16_all_true(comp_ctx,
  2844. func_ctx))
  2845. return false;
  2846. break;
  2847. }
  2848. case SIMD_i8x16_bitmask:
  2849. {
  2850. if (!aot_compile_simd_i8x16_bitmask(comp_ctx, func_ctx))
  2851. return false;
  2852. break;
  2853. }
  2854. case SIMD_i8x16_narrow_i16x8_s:
  2855. case SIMD_i8x16_narrow_i16x8_u:
  2856. {
  2857. if (!aot_compile_simd_i8x16_narrow_i16x8(
  2858. comp_ctx, func_ctx,
  2859. (opcode == SIMD_i8x16_narrow_i16x8_s)))
  2860. return false;
  2861. break;
  2862. }
  2863. case SIMD_f32x4_ceil:
  2864. {
  2865. if (!aot_compile_simd_f32x4_ceil(comp_ctx, func_ctx))
  2866. return false;
  2867. break;
  2868. }
  2869. case SIMD_f32x4_floor:
  2870. {
  2871. if (!aot_compile_simd_f32x4_floor(comp_ctx, func_ctx))
  2872. return false;
  2873. break;
  2874. }
  2875. case SIMD_f32x4_trunc:
  2876. {
  2877. if (!aot_compile_simd_f32x4_trunc(comp_ctx, func_ctx))
  2878. return false;
  2879. break;
  2880. }
  2881. case SIMD_f32x4_nearest:
  2882. {
  2883. if (!aot_compile_simd_f32x4_nearest(comp_ctx, func_ctx))
  2884. return false;
  2885. break;
  2886. }
  2887. case SIMD_i8x16_shl:
  2888. case SIMD_i8x16_shr_s:
  2889. case SIMD_i8x16_shr_u:
  2890. {
  2891. if (!aot_compile_simd_i8x16_shift(comp_ctx, func_ctx,
  2892. INT_SHL + opcode
  2893. - SIMD_i8x16_shl))
  2894. return false;
  2895. break;
  2896. }
  2897. case SIMD_i8x16_add:
  2898. {
  2899. if (!aot_compile_simd_i8x16_arith(comp_ctx, func_ctx,
  2900. V128_ADD))
  2901. return false;
  2902. break;
  2903. }
  2904. case SIMD_i8x16_add_sat_s:
  2905. case SIMD_i8x16_add_sat_u:
  2906. {
  2907. if (!aot_compile_simd_i8x16_saturate(
  2908. comp_ctx, func_ctx, V128_ADD,
  2909. opcode == SIMD_i8x16_add_sat_s))
  2910. return false;
  2911. break;
  2912. }
  2913. case SIMD_i8x16_sub:
  2914. {
  2915. if (!aot_compile_simd_i8x16_arith(comp_ctx, func_ctx,
  2916. V128_SUB))
  2917. return false;
  2918. break;
  2919. }
  2920. case SIMD_i8x16_sub_sat_s:
  2921. case SIMD_i8x16_sub_sat_u:
  2922. {
  2923. if (!aot_compile_simd_i8x16_saturate(
  2924. comp_ctx, func_ctx, V128_SUB,
  2925. opcode == SIMD_i8x16_sub_sat_s))
  2926. return false;
  2927. break;
  2928. }
  2929. case SIMD_f64x2_ceil:
  2930. {
  2931. if (!aot_compile_simd_f64x2_ceil(comp_ctx, func_ctx))
  2932. return false;
  2933. break;
  2934. }
  2935. case SIMD_f64x2_floor:
  2936. {
  2937. if (!aot_compile_simd_f64x2_floor(comp_ctx, func_ctx))
  2938. return false;
  2939. break;
  2940. }
  2941. case SIMD_i8x16_min_s:
  2942. case SIMD_i8x16_min_u:
  2943. {
  2944. if (!aot_compile_simd_i8x16_cmp(
  2945. comp_ctx, func_ctx, V128_MIN,
  2946. opcode == SIMD_i8x16_min_s))
  2947. return false;
  2948. break;
  2949. }
  2950. case SIMD_i8x16_max_s:
  2951. case SIMD_i8x16_max_u:
  2952. {
  2953. if (!aot_compile_simd_i8x16_cmp(
  2954. comp_ctx, func_ctx, V128_MAX,
  2955. opcode == SIMD_i8x16_max_s))
  2956. return false;
  2957. break;
  2958. }
  2959. case SIMD_f64x2_trunc:
  2960. {
  2961. if (!aot_compile_simd_f64x2_trunc(comp_ctx, func_ctx))
  2962. return false;
  2963. break;
  2964. }
  2965. case SIMD_i8x16_avgr_u:
  2966. {
  2967. if (!aot_compile_simd_i8x16_avgr_u(comp_ctx, func_ctx))
  2968. return false;
  2969. break;
  2970. }
  2971. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  2972. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  2973. {
  2974. if (!aot_compile_simd_i16x8_extadd_pairwise_i8x16(
  2975. comp_ctx, func_ctx,
  2976. SIMD_i16x8_extadd_pairwise_i8x16_s == opcode))
  2977. return false;
  2978. break;
  2979. }
  2980. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  2981. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  2982. {
  2983. if (!aot_compile_simd_i32x4_extadd_pairwise_i16x8(
  2984. comp_ctx, func_ctx,
  2985. SIMD_i32x4_extadd_pairwise_i16x8_s == opcode))
  2986. return false;
  2987. break;
  2988. }
  2989. /* i16x8 Op */
  2990. case SIMD_i16x8_abs:
  2991. {
  2992. if (!aot_compile_simd_i16x8_abs(comp_ctx, func_ctx))
  2993. return false;
  2994. break;
  2995. }
  2996. case SIMD_i16x8_neg:
  2997. {
  2998. if (!aot_compile_simd_i16x8_neg(comp_ctx, func_ctx))
  2999. return false;
  3000. break;
  3001. }
  3002. case SIMD_i16x8_q15mulr_sat_s:
  3003. {
  3004. if (!aot_compile_simd_i16x8_q15mulr_sat(comp_ctx,
  3005. func_ctx))
  3006. return false;
  3007. break;
  3008. }
  3009. case SIMD_i16x8_all_true:
  3010. {
  3011. if (!aot_compile_simd_i16x8_all_true(comp_ctx,
  3012. func_ctx))
  3013. return false;
  3014. break;
  3015. }
  3016. case SIMD_i16x8_bitmask:
  3017. {
  3018. if (!aot_compile_simd_i16x8_bitmask(comp_ctx, func_ctx))
  3019. return false;
  3020. break;
  3021. }
  3022. case SIMD_i16x8_narrow_i32x4_s:
  3023. case SIMD_i16x8_narrow_i32x4_u:
  3024. {
  3025. if (!aot_compile_simd_i16x8_narrow_i32x4(
  3026. comp_ctx, func_ctx,
  3027. SIMD_i16x8_narrow_i32x4_s == opcode))
  3028. return false;
  3029. break;
  3030. }
  3031. case SIMD_i16x8_extend_low_i8x16_s:
  3032. case SIMD_i16x8_extend_high_i8x16_s:
  3033. {
  3034. if (!aot_compile_simd_i16x8_extend_i8x16(
  3035. comp_ctx, func_ctx,
  3036. SIMD_i16x8_extend_low_i8x16_s == opcode, true))
  3037. return false;
  3038. break;
  3039. }
  3040. case SIMD_i16x8_extend_low_i8x16_u:
  3041. case SIMD_i16x8_extend_high_i8x16_u:
  3042. {
  3043. if (!aot_compile_simd_i16x8_extend_i8x16(
  3044. comp_ctx, func_ctx,
  3045. SIMD_i16x8_extend_low_i8x16_u == opcode, false))
  3046. return false;
  3047. break;
  3048. }
  3049. case SIMD_i16x8_shl:
  3050. case SIMD_i16x8_shr_s:
  3051. case SIMD_i16x8_shr_u:
  3052. {
  3053. if (!aot_compile_simd_i16x8_shift(comp_ctx, func_ctx,
  3054. INT_SHL + opcode
  3055. - SIMD_i16x8_shl))
  3056. return false;
  3057. break;
  3058. }
  3059. case SIMD_i16x8_add:
  3060. {
  3061. if (!aot_compile_simd_i16x8_arith(comp_ctx, func_ctx,
  3062. V128_ADD))
  3063. return false;
  3064. break;
  3065. }
  3066. case SIMD_i16x8_add_sat_s:
  3067. case SIMD_i16x8_add_sat_u:
  3068. {
  3069. if (!aot_compile_simd_i16x8_saturate(
  3070. comp_ctx, func_ctx, V128_ADD,
  3071. opcode == SIMD_i16x8_add_sat_s ? true : false))
  3072. return false;
  3073. break;
  3074. }
  3075. case SIMD_i16x8_sub:
  3076. {
  3077. if (!aot_compile_simd_i16x8_arith(comp_ctx, func_ctx,
  3078. V128_SUB))
  3079. return false;
  3080. break;
  3081. }
  3082. case SIMD_i16x8_sub_sat_s:
  3083. case SIMD_i16x8_sub_sat_u:
  3084. {
  3085. if (!aot_compile_simd_i16x8_saturate(
  3086. comp_ctx, func_ctx, V128_SUB,
  3087. opcode == SIMD_i16x8_sub_sat_s ? true : false))
  3088. return false;
  3089. break;
  3090. }
  3091. case SIMD_f64x2_nearest:
  3092. {
  3093. if (!aot_compile_simd_f64x2_nearest(comp_ctx, func_ctx))
  3094. return false;
  3095. break;
  3096. }
  3097. case SIMD_i16x8_mul:
  3098. {
  3099. if (!aot_compile_simd_i16x8_arith(comp_ctx, func_ctx,
  3100. V128_MUL))
  3101. return false;
  3102. break;
  3103. }
  3104. case SIMD_i16x8_min_s:
  3105. case SIMD_i16x8_min_u:
  3106. {
  3107. if (!aot_compile_simd_i16x8_cmp(
  3108. comp_ctx, func_ctx, V128_MIN,
  3109. opcode == SIMD_i16x8_min_s))
  3110. return false;
  3111. break;
  3112. }
  3113. case SIMD_i16x8_max_s:
  3114. case SIMD_i16x8_max_u:
  3115. {
  3116. if (!aot_compile_simd_i16x8_cmp(
  3117. comp_ctx, func_ctx, V128_MAX,
  3118. opcode == SIMD_i16x8_max_s))
  3119. return false;
  3120. break;
  3121. }
  3122. case SIMD_i16x8_avgr_u:
  3123. {
  3124. if (!aot_compile_simd_i16x8_avgr_u(comp_ctx, func_ctx))
  3125. return false;
  3126. break;
  3127. }
  3128. case SIMD_i16x8_extmul_low_i8x16_s:
  3129. case SIMD_i16x8_extmul_high_i8x16_s:
  3130. {
  3131. if (!(aot_compile_simd_i16x8_extmul_i8x16(
  3132. comp_ctx, func_ctx,
  3133. SIMD_i16x8_extmul_low_i8x16_s == opcode, true)))
  3134. return false;
  3135. break;
  3136. }
  3137. case SIMD_i16x8_extmul_low_i8x16_u:
  3138. case SIMD_i16x8_extmul_high_i8x16_u:
  3139. {
  3140. if (!(aot_compile_simd_i16x8_extmul_i8x16(
  3141. comp_ctx, func_ctx,
  3142. SIMD_i16x8_extmul_low_i8x16_u == opcode,
  3143. false)))
  3144. return false;
  3145. break;
  3146. }
  3147. /* i32x4 Op */
  3148. case SIMD_i32x4_abs:
  3149. {
  3150. if (!aot_compile_simd_i32x4_abs(comp_ctx, func_ctx))
  3151. return false;
  3152. break;
  3153. }
  3154. case SIMD_i32x4_neg:
  3155. {
  3156. if (!aot_compile_simd_i32x4_neg(comp_ctx, func_ctx))
  3157. return false;
  3158. break;
  3159. }
  3160. case SIMD_i32x4_all_true:
  3161. {
  3162. if (!aot_compile_simd_i32x4_all_true(comp_ctx,
  3163. func_ctx))
  3164. return false;
  3165. break;
  3166. }
  3167. case SIMD_i32x4_bitmask:
  3168. {
  3169. if (!aot_compile_simd_i32x4_bitmask(comp_ctx, func_ctx))
  3170. return false;
  3171. break;
  3172. }
  3173. case SIMD_i32x4_extend_low_i16x8_s:
  3174. case SIMD_i32x4_extend_high_i16x8_s:
  3175. {
  3176. if (!aot_compile_simd_i32x4_extend_i16x8(
  3177. comp_ctx, func_ctx,
  3178. SIMD_i32x4_extend_low_i16x8_s == opcode, true))
  3179. return false;
  3180. break;
  3181. }
  3182. case SIMD_i32x4_extend_low_i16x8_u:
  3183. case SIMD_i32x4_extend_high_i16x8_u:
  3184. {
  3185. if (!aot_compile_simd_i32x4_extend_i16x8(
  3186. comp_ctx, func_ctx,
  3187. SIMD_i32x4_extend_low_i16x8_u == opcode, false))
  3188. return false;
  3189. break;
  3190. }
  3191. case SIMD_i32x4_shl:
  3192. case SIMD_i32x4_shr_s:
  3193. case SIMD_i32x4_shr_u:
  3194. {
  3195. if (!aot_compile_simd_i32x4_shift(comp_ctx, func_ctx,
  3196. INT_SHL + opcode
  3197. - SIMD_i32x4_shl))
  3198. return false;
  3199. break;
  3200. }
  3201. case SIMD_i32x4_add:
  3202. {
  3203. if (!aot_compile_simd_i32x4_arith(comp_ctx, func_ctx,
  3204. V128_ADD))
  3205. return false;
  3206. break;
  3207. }
  3208. case SIMD_i32x4_sub:
  3209. {
  3210. if (!aot_compile_simd_i32x4_arith(comp_ctx, func_ctx,
  3211. V128_SUB))
  3212. return false;
  3213. break;
  3214. }
  3215. case SIMD_i32x4_mul:
  3216. {
  3217. if (!aot_compile_simd_i32x4_arith(comp_ctx, func_ctx,
  3218. V128_MUL))
  3219. return false;
  3220. break;
  3221. }
  3222. case SIMD_i32x4_min_s:
  3223. case SIMD_i32x4_min_u:
  3224. {
  3225. if (!aot_compile_simd_i32x4_cmp(
  3226. comp_ctx, func_ctx, V128_MIN,
  3227. SIMD_i32x4_min_s == opcode))
  3228. return false;
  3229. break;
  3230. }
  3231. case SIMD_i32x4_max_s:
  3232. case SIMD_i32x4_max_u:
  3233. {
  3234. if (!aot_compile_simd_i32x4_cmp(
  3235. comp_ctx, func_ctx, V128_MAX,
  3236. SIMD_i32x4_max_s == opcode))
  3237. return false;
  3238. break;
  3239. }
  3240. case SIMD_i32x4_dot_i16x8_s:
  3241. {
  3242. if (!aot_compile_simd_i32x4_dot_i16x8(comp_ctx,
  3243. func_ctx))
  3244. return false;
  3245. break;
  3246. }
  3247. case SIMD_i32x4_extmul_low_i16x8_s:
  3248. case SIMD_i32x4_extmul_high_i16x8_s:
  3249. {
  3250. if (!aot_compile_simd_i32x4_extmul_i16x8(
  3251. comp_ctx, func_ctx,
  3252. SIMD_i32x4_extmul_low_i16x8_s == opcode, true))
  3253. return false;
  3254. break;
  3255. }
  3256. case SIMD_i32x4_extmul_low_i16x8_u:
  3257. case SIMD_i32x4_extmul_high_i16x8_u:
  3258. {
  3259. if (!aot_compile_simd_i32x4_extmul_i16x8(
  3260. comp_ctx, func_ctx,
  3261. SIMD_i32x4_extmul_low_i16x8_u == opcode, false))
  3262. return false;
  3263. break;
  3264. }
  3265. /* i64x2 Op */
  3266. case SIMD_i64x2_abs:
  3267. {
  3268. if (!aot_compile_simd_i64x2_abs(comp_ctx, func_ctx))
  3269. return false;
  3270. break;
  3271. }
  3272. case SIMD_i64x2_neg:
  3273. {
  3274. if (!aot_compile_simd_i64x2_neg(comp_ctx, func_ctx))
  3275. return false;
  3276. break;
  3277. }
  3278. case SIMD_i64x2_all_true:
  3279. {
  3280. if (!aot_compile_simd_i64x2_all_true(comp_ctx,
  3281. func_ctx))
  3282. return false;
  3283. break;
  3284. }
  3285. case SIMD_i64x2_bitmask:
  3286. {
  3287. if (!aot_compile_simd_i64x2_bitmask(comp_ctx, func_ctx))
  3288. return false;
  3289. break;
  3290. }
  3291. case SIMD_i64x2_extend_low_i32x4_s:
  3292. case SIMD_i64x2_extend_high_i32x4_s:
  3293. {
  3294. if (!aot_compile_simd_i64x2_extend_i32x4(
  3295. comp_ctx, func_ctx,
  3296. SIMD_i64x2_extend_low_i32x4_s == opcode, true))
  3297. return false;
  3298. break;
  3299. }
  3300. case SIMD_i64x2_extend_low_i32x4_u:
  3301. case SIMD_i64x2_extend_high_i32x4_u:
  3302. {
  3303. if (!aot_compile_simd_i64x2_extend_i32x4(
  3304. comp_ctx, func_ctx,
  3305. SIMD_i64x2_extend_low_i32x4_u == opcode, false))
  3306. return false;
  3307. break;
  3308. }
  3309. case SIMD_i64x2_shl:
  3310. case SIMD_i64x2_shr_s:
  3311. case SIMD_i64x2_shr_u:
  3312. {
  3313. if (!aot_compile_simd_i64x2_shift(comp_ctx, func_ctx,
  3314. INT_SHL + opcode
  3315. - SIMD_i64x2_shl))
  3316. return false;
  3317. break;
  3318. }
  3319. case SIMD_i64x2_add:
  3320. {
  3321. if (!aot_compile_simd_i64x2_arith(comp_ctx, func_ctx,
  3322. V128_ADD))
  3323. return false;
  3324. break;
  3325. }
  3326. case SIMD_i64x2_sub:
  3327. {
  3328. if (!aot_compile_simd_i64x2_arith(comp_ctx, func_ctx,
  3329. V128_SUB))
  3330. return false;
  3331. break;
  3332. }
  3333. case SIMD_i64x2_mul:
  3334. {
  3335. if (!aot_compile_simd_i64x2_arith(comp_ctx, func_ctx,
  3336. V128_MUL))
  3337. return false;
  3338. break;
  3339. }
  3340. case SIMD_i64x2_eq:
  3341. case SIMD_i64x2_ne:
  3342. case SIMD_i64x2_lt_s:
  3343. case SIMD_i64x2_gt_s:
  3344. case SIMD_i64x2_le_s:
  3345. case SIMD_i64x2_ge_s:
  3346. {
  3347. IntCond icond[] = { INT_EQ, INT_NE, INT_LT_S,
  3348. INT_GT_S, INT_LE_S, INT_GE_S };
  3349. if (!aot_compile_simd_i64x2_compare(
  3350. comp_ctx, func_ctx,
  3351. icond[opcode - SIMD_i64x2_eq]))
  3352. return false;
  3353. break;
  3354. }
  3355. case SIMD_i64x2_extmul_low_i32x4_s:
  3356. case SIMD_i64x2_extmul_high_i32x4_s:
  3357. {
  3358. if (!aot_compile_simd_i64x2_extmul_i32x4(
  3359. comp_ctx, func_ctx,
  3360. SIMD_i64x2_extmul_low_i32x4_s == opcode, true))
  3361. return false;
  3362. break;
  3363. }
  3364. case SIMD_i64x2_extmul_low_i32x4_u:
  3365. case SIMD_i64x2_extmul_high_i32x4_u:
  3366. {
  3367. if (!aot_compile_simd_i64x2_extmul_i32x4(
  3368. comp_ctx, func_ctx,
  3369. SIMD_i64x2_extmul_low_i32x4_u == opcode, false))
  3370. return false;
  3371. break;
  3372. }
  3373. /* f32x4 Op */
  3374. case SIMD_f32x4_abs:
  3375. {
  3376. if (!aot_compile_simd_f32x4_abs(comp_ctx, func_ctx))
  3377. return false;
  3378. break;
  3379. }
  3380. case SIMD_f32x4_neg:
  3381. {
  3382. if (!aot_compile_simd_f32x4_neg(comp_ctx, func_ctx))
  3383. return false;
  3384. break;
  3385. }
  3386. case SIMD_f32x4_sqrt:
  3387. {
  3388. if (!aot_compile_simd_f32x4_sqrt(comp_ctx, func_ctx))
  3389. return false;
  3390. break;
  3391. }
  3392. case SIMD_f32x4_add:
  3393. case SIMD_f32x4_sub:
  3394. case SIMD_f32x4_mul:
  3395. case SIMD_f32x4_div:
  3396. {
  3397. if (!aot_compile_simd_f32x4_arith(comp_ctx, func_ctx,
  3398. FLOAT_ADD + opcode
  3399. - SIMD_f32x4_add))
  3400. return false;
  3401. break;
  3402. }
  3403. case SIMD_f32x4_min:
  3404. case SIMD_f32x4_max:
  3405. {
  3406. if (!aot_compile_simd_f32x4_min_max(
  3407. comp_ctx, func_ctx, SIMD_f32x4_min == opcode))
  3408. return false;
  3409. break;
  3410. }
  3411. case SIMD_f32x4_pmin:
  3412. case SIMD_f32x4_pmax:
  3413. {
  3414. if (!aot_compile_simd_f32x4_pmin_pmax(
  3415. comp_ctx, func_ctx, SIMD_f32x4_pmin == opcode))
  3416. return false;
  3417. break;
  3418. }
  3419. /* f64x2 Op */
  3420. case SIMD_f64x2_abs:
  3421. {
  3422. if (!aot_compile_simd_f64x2_abs(comp_ctx, func_ctx))
  3423. return false;
  3424. break;
  3425. }
  3426. case SIMD_f64x2_neg:
  3427. {
  3428. if (!aot_compile_simd_f64x2_neg(comp_ctx, func_ctx))
  3429. return false;
  3430. break;
  3431. }
  3432. case SIMD_f64x2_sqrt:
  3433. {
  3434. if (!aot_compile_simd_f64x2_sqrt(comp_ctx, func_ctx))
  3435. return false;
  3436. break;
  3437. }
  3438. case SIMD_f64x2_add:
  3439. case SIMD_f64x2_sub:
  3440. case SIMD_f64x2_mul:
  3441. case SIMD_f64x2_div:
  3442. {
  3443. if (!aot_compile_simd_f64x2_arith(comp_ctx, func_ctx,
  3444. FLOAT_ADD + opcode
  3445. - SIMD_f64x2_add))
  3446. return false;
  3447. break;
  3448. }
  3449. case SIMD_f64x2_min:
  3450. case SIMD_f64x2_max:
  3451. {
  3452. if (!aot_compile_simd_f64x2_min_max(
  3453. comp_ctx, func_ctx, SIMD_f64x2_min == opcode))
  3454. return false;
  3455. break;
  3456. }
  3457. case SIMD_f64x2_pmin:
  3458. case SIMD_f64x2_pmax:
  3459. {
  3460. if (!aot_compile_simd_f64x2_pmin_pmax(
  3461. comp_ctx, func_ctx, SIMD_f64x2_pmin == opcode))
  3462. return false;
  3463. break;
  3464. }
  3465. /* Conversion Op */
  3466. case SIMD_i32x4_trunc_sat_f32x4_s:
  3467. case SIMD_i32x4_trunc_sat_f32x4_u:
  3468. {
  3469. if (!aot_compile_simd_i32x4_trunc_sat_f32x4(
  3470. comp_ctx, func_ctx,
  3471. SIMD_i32x4_trunc_sat_f32x4_s == opcode))
  3472. return false;
  3473. break;
  3474. }
  3475. case SIMD_f32x4_convert_i32x4_s:
  3476. case SIMD_f32x4_convert_i32x4_u:
  3477. {
  3478. if (!aot_compile_simd_f32x4_convert_i32x4(
  3479. comp_ctx, func_ctx,
  3480. SIMD_f32x4_convert_i32x4_s == opcode))
  3481. return false;
  3482. break;
  3483. }
  3484. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  3485. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  3486. {
  3487. if (!aot_compile_simd_i32x4_trunc_sat_f64x2(
  3488. comp_ctx, func_ctx,
  3489. SIMD_i32x4_trunc_sat_f64x2_s_zero == opcode))
  3490. return false;
  3491. break;
  3492. }
  3493. case SIMD_f64x2_convert_low_i32x4_s:
  3494. case SIMD_f64x2_convert_low_i32x4_u:
  3495. {
  3496. if (!aot_compile_simd_f64x2_convert_i32x4(
  3497. comp_ctx, func_ctx,
  3498. SIMD_f64x2_convert_low_i32x4_s == opcode))
  3499. return false;
  3500. break;
  3501. }
  3502. default:
  3503. aot_set_last_error("unsupported SIMD opcode");
  3504. return false;
  3505. }
  3506. break;
  3507. }
  3508. #endif /* end of WASM_ENABLE_SIMD */
  3509. default:
  3510. aot_set_last_error("unsupported opcode");
  3511. return false;
  3512. }
  3513. }
  3514. /* Move func_return block to the bottom */
  3515. if (func_ctx->func_return_block) {
  3516. LLVMBasicBlockRef last_block = LLVMGetLastBasicBlock(func_ctx->func);
  3517. if (last_block != func_ctx->func_return_block)
  3518. LLVMMoveBasicBlockAfter(func_ctx->func_return_block, last_block);
  3519. }
  3520. /* Move got_exception block to the bottom */
  3521. if (func_ctx->got_exception_block) {
  3522. LLVMBasicBlockRef last_block = LLVMGetLastBasicBlock(func_ctx->func);
  3523. if (last_block != func_ctx->got_exception_block)
  3524. LLVMMoveBasicBlockAfter(func_ctx->got_exception_block, last_block);
  3525. }
  3526. return true;
  3527. #if WASM_ENABLE_SIMD != 0
  3528. unsupport_simd:
  3529. aot_set_last_error("SIMD instruction was found, "
  3530. "try removing --disable-simd option");
  3531. return false;
  3532. #endif
  3533. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3534. unsupport_ref_types:
  3535. aot_set_last_error("reference type instruction was found, "
  3536. "try removing --disable-ref-types option "
  3537. "or adding --enable-gc option");
  3538. return false;
  3539. #endif
  3540. #if WASM_ENABLE_GC != 0
  3541. unsupport_gc:
  3542. aot_set_last_error("GC instruction was found, "
  3543. "try adding --enable-gc option");
  3544. return false;
  3545. #endif
  3546. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3547. unsupport_gc_and_ref_types:
  3548. aot_set_last_error(
  3549. "reference type or gc instruction was found, try removing "
  3550. "--disable-ref-types option or adding --enable-gc option");
  3551. return false;
  3552. #endif
  3553. #if WASM_ENABLE_BULK_MEMORY != 0
  3554. unsupport_bulk_memory:
  3555. aot_set_last_error("bulk memory instruction was found, "
  3556. "try removing --disable-bulk-memory option");
  3557. return false;
  3558. #endif
  3559. fail:
  3560. return false;
  3561. }
  3562. static bool
  3563. verify_module(AOTCompContext *comp_ctx)
  3564. {
  3565. char *msg = NULL;
  3566. bool ret;
  3567. ret = LLVMVerifyModule(comp_ctx->module, LLVMPrintMessageAction, &msg);
  3568. if (!ret && msg) {
  3569. if (msg[0] != '\0') {
  3570. aot_set_last_error(msg);
  3571. LLVMDisposeMessage(msg);
  3572. return false;
  3573. }
  3574. LLVMDisposeMessage(msg);
  3575. }
  3576. return true;
  3577. }
  3578. bool
  3579. aot_compile_wasm(AOTCompContext *comp_ctx)
  3580. {
  3581. uint32 i;
  3582. if (!aot_validate_wasm(comp_ctx)) {
  3583. return false;
  3584. }
  3585. bh_print_time("Begin to compile WASM bytecode to LLVM IR");
  3586. for (i = 0; i < comp_ctx->func_ctx_count; i++) {
  3587. if (!aot_compile_func(comp_ctx, i)) {
  3588. return false;
  3589. }
  3590. }
  3591. #if WASM_ENABLE_DEBUG_AOT != 0
  3592. LLVMDIBuilderFinalize(comp_ctx->debug_builder);
  3593. #endif
  3594. /* Disable LLVM module verification for jit mode to speedup
  3595. the compilation process */
  3596. if (!comp_ctx->is_jit_mode) {
  3597. bh_print_time("Begin to verify LLVM module");
  3598. if (!verify_module(comp_ctx)) {
  3599. return false;
  3600. }
  3601. }
  3602. /* Run IR optimization before feeding in ORCJIT and AOT codegen */
  3603. if (comp_ctx->optimize) {
  3604. /* Run passes for AOT/JIT mode.
  3605. TODO: Apply these passes in the do_ir_transform callback of
  3606. TransformLayer when compiling each jit function, so as to
  3607. speedup the launch process. Now there are two issues in the
  3608. JIT: one is memory leak in do_ir_transform, the other is
  3609. possible core dump. */
  3610. bh_print_time("Begin to run llvm optimization passes");
  3611. aot_apply_llvm_new_pass_manager(comp_ctx, comp_ctx->module);
  3612. bh_print_time("Finish llvm optimization passes");
  3613. }
  3614. #ifdef DUMP_MODULE
  3615. LLVMDumpModule(comp_ctx->module);
  3616. os_printf("\n");
  3617. #endif
  3618. if (comp_ctx->is_jit_mode) {
  3619. LLVMErrorRef err;
  3620. LLVMOrcJITDylibRef orc_main_dylib;
  3621. LLVMOrcThreadSafeModuleRef orc_thread_safe_module;
  3622. orc_main_dylib = LLVMOrcLLLazyJITGetMainJITDylib(comp_ctx->orc_jit);
  3623. if (!orc_main_dylib) {
  3624. aot_set_last_error(
  3625. "failed to get orc orc_jit main dynamic library");
  3626. return false;
  3627. }
  3628. orc_thread_safe_module = LLVMOrcCreateNewThreadSafeModule(
  3629. comp_ctx->module, comp_ctx->orc_thread_safe_context);
  3630. if (!orc_thread_safe_module) {
  3631. aot_set_last_error("failed to create thread safe module");
  3632. return false;
  3633. }
  3634. if ((err = LLVMOrcLLLazyJITAddLLVMIRModule(
  3635. comp_ctx->orc_jit, orc_main_dylib, orc_thread_safe_module))) {
  3636. /* If adding the ThreadSafeModule fails then we need to clean it up
  3637. by ourselves, otherwise the orc orc_jit will manage the memory.
  3638. */
  3639. LLVMOrcDisposeThreadSafeModule(orc_thread_safe_module);
  3640. aot_handle_llvm_errmsg("failed to addIRModule", err);
  3641. return false;
  3642. }
  3643. if (comp_ctx->stack_sizes != NULL) {
  3644. LLVMOrcJITTargetAddress addr;
  3645. if ((err = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &addr,
  3646. aot_stack_sizes_alias_name))) {
  3647. aot_handle_llvm_errmsg("failed to look up stack_sizes", err);
  3648. return false;
  3649. }
  3650. comp_ctx->jit_stack_sizes = (uint32 *)addr;
  3651. }
  3652. }
  3653. return true;
  3654. }
  3655. char *
  3656. aot_generate_tempfile_name(const char *prefix, const char *extension,
  3657. char *buffer, uint32 len)
  3658. {
  3659. int name_len;
  3660. name_len = snprintf(buffer, len, "%s-XXXXXX", prefix);
  3661. if (!bh_mkstemp(buffer, name_len + 1)) {
  3662. aot_set_last_error("make temp file failed.");
  3663. return NULL;
  3664. }
  3665. /* Check if buffer length is enough */
  3666. /* name_len + '.' + extension + '\0' */
  3667. if (name_len + 1 + strlen(extension) + 1 > len) {
  3668. aot_set_last_error("temp file name too long.");
  3669. return NULL;
  3670. }
  3671. snprintf(buffer + name_len, len - name_len, ".%s", extension);
  3672. return buffer;
  3673. }
  3674. bool
  3675. aot_emit_llvm_file(AOTCompContext *comp_ctx, const char *file_name)
  3676. {
  3677. char *err = NULL;
  3678. bh_print_time("Begin to emit LLVM IR file");
  3679. if (LLVMPrintModuleToFile(comp_ctx->module, file_name, &err) != 0) {
  3680. if (err) {
  3681. LLVMDisposeMessage(err);
  3682. err = NULL;
  3683. }
  3684. aot_set_last_error("emit llvm ir to file failed.");
  3685. return false;
  3686. }
  3687. return true;
  3688. }
  3689. static bool
  3690. aot_move_file(const char *dest, const char *src)
  3691. {
  3692. FILE *dfp = fopen(dest, "w");
  3693. FILE *sfp = fopen(src, "r");
  3694. size_t rsz;
  3695. char buf[128];
  3696. bool success = false;
  3697. if (dfp == NULL || sfp == NULL) {
  3698. LOG_DEBUG("open error %s %s", dest, src);
  3699. goto fail;
  3700. }
  3701. do {
  3702. rsz = fread(buf, 1, sizeof(buf), sfp);
  3703. if (rsz > 0) {
  3704. size_t wsz = fwrite(buf, 1, rsz, dfp);
  3705. if (wsz < rsz) {
  3706. LOG_DEBUG("write error");
  3707. goto fail;
  3708. }
  3709. }
  3710. if (rsz < sizeof(buf)) {
  3711. if (ferror(sfp)) {
  3712. LOG_DEBUG("read error");
  3713. goto fail;
  3714. }
  3715. }
  3716. } while (rsz > 0);
  3717. success = true;
  3718. fail:
  3719. if (dfp != NULL) {
  3720. if (fclose(dfp)) {
  3721. LOG_DEBUG("close error");
  3722. success = false;
  3723. }
  3724. if (!success) {
  3725. (void)unlink(dest);
  3726. }
  3727. }
  3728. if (sfp != NULL) {
  3729. (void)fclose(sfp);
  3730. }
  3731. if (success) {
  3732. (void)unlink(src);
  3733. }
  3734. return success;
  3735. }
  3736. bool
  3737. aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name)
  3738. {
  3739. char *err = NULL;
  3740. LLVMCodeGenFileType file_type = LLVMObjectFile;
  3741. LLVMTargetRef target = LLVMGetTargetMachineTarget(comp_ctx->target_machine);
  3742. bh_print_time("Begin to emit object file");
  3743. if (comp_ctx->external_llc_compiler || comp_ctx->external_asm_compiler) {
  3744. char cmd[1024];
  3745. int ret;
  3746. if (comp_ctx->external_llc_compiler) {
  3747. const char *stack_usage_flag = "";
  3748. char bc_file_name[64];
  3749. char su_file_name[65]; /* See the comment below */
  3750. if (comp_ctx->stack_usage_file != NULL) {
  3751. /*
  3752. * Note: we know the caller uses 64 byte buffer for
  3753. * file_name. It will get 1 byte longer because we
  3754. * replace ".o" with ".su".
  3755. */
  3756. size_t len = strlen(file_name);
  3757. bh_assert(len + 1 <= sizeof(su_file_name));
  3758. bh_assert(len > 3);
  3759. bh_assert(file_name[len - 2] == '.');
  3760. bh_assert(file_name[len - 1] == 'o');
  3761. snprintf(su_file_name, sizeof(su_file_name), "%.*s.su",
  3762. (int)(len - 2), file_name);
  3763. stack_usage_flag = " -fstack-usage";
  3764. }
  3765. if (!aot_generate_tempfile_name("wamrc-bc", "bc", bc_file_name,
  3766. sizeof(bc_file_name))) {
  3767. return false;
  3768. }
  3769. if (LLVMWriteBitcodeToFile(comp_ctx->module, bc_file_name) != 0) {
  3770. aot_set_last_error("emit llvm bitcode file failed.");
  3771. return false;
  3772. }
  3773. snprintf(cmd, sizeof(cmd), "%s%s %s -o %s %s",
  3774. comp_ctx->external_llc_compiler, stack_usage_flag,
  3775. comp_ctx->llc_compiler_flags ? comp_ctx->llc_compiler_flags
  3776. : "-O3 -c",
  3777. file_name, bc_file_name);
  3778. LOG_VERBOSE("invoking external LLC compiler:\n\t%s", cmd);
  3779. ret = bh_system(cmd);
  3780. /* remove temp bitcode file */
  3781. unlink(bc_file_name);
  3782. if (ret != 0) {
  3783. aot_set_last_error("failed to compile LLVM bitcode to obj file "
  3784. "with external LLC compiler.");
  3785. return false;
  3786. }
  3787. if (comp_ctx->stack_usage_file != NULL) {
  3788. /*
  3789. * move the temporary .su file to the specified location.
  3790. *
  3791. * Note: the former is automatically inferred from the output
  3792. * filename (file_name here) by clang.
  3793. *
  3794. * Note: the latter might be user-specified.
  3795. * (wamrc --stack-usage=<file>)
  3796. */
  3797. if (!aot_move_file(comp_ctx->stack_usage_file, su_file_name)) {
  3798. aot_set_last_error("failed to move su file.");
  3799. (void)unlink(su_file_name);
  3800. return false;
  3801. }
  3802. }
  3803. }
  3804. else if (comp_ctx->external_asm_compiler) {
  3805. char asm_file_name[64];
  3806. if (!aot_generate_tempfile_name("wamrc-asm", "s", asm_file_name,
  3807. sizeof(asm_file_name))) {
  3808. return false;
  3809. }
  3810. if (LLVMTargetMachineEmitToFile(comp_ctx->target_machine,
  3811. comp_ctx->module, asm_file_name,
  3812. LLVMAssemblyFile, &err)
  3813. != 0) {
  3814. if (err) {
  3815. LLVMDisposeMessage(err);
  3816. err = NULL;
  3817. }
  3818. aot_set_last_error("emit elf to assembly file failed.");
  3819. return false;
  3820. }
  3821. snprintf(cmd, sizeof(cmd), "%s %s -o %s %s",
  3822. comp_ctx->external_asm_compiler,
  3823. comp_ctx->asm_compiler_flags ? comp_ctx->asm_compiler_flags
  3824. : "-O3 -c",
  3825. file_name, asm_file_name);
  3826. LOG_VERBOSE("invoking external ASM compiler:\n\t%s", cmd);
  3827. ret = bh_system(cmd);
  3828. /* remove temp assembly file */
  3829. unlink(asm_file_name);
  3830. if (ret != 0) {
  3831. aot_set_last_error("failed to compile Assembly file to obj "
  3832. "file with external ASM compiler.");
  3833. return false;
  3834. }
  3835. }
  3836. return true;
  3837. }
  3838. if (!strncmp(LLVMGetTargetName(target), "arc", 3))
  3839. /* Emit to assembly file instead for arc target
  3840. as it cannot emit to object file */
  3841. file_type = LLVMAssemblyFile;
  3842. if (LLVMTargetMachineEmitToFile(comp_ctx->target_machine, comp_ctx->module,
  3843. file_name, file_type, &err)
  3844. != 0) {
  3845. if (err) {
  3846. LLVMDisposeMessage(err);
  3847. err = NULL;
  3848. }
  3849. aot_set_last_error("emit elf to object file failed.");
  3850. return false;
  3851. }
  3852. return true;
  3853. }