aot_compiler.c 162 KB

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