aot_compiler.c 162 KB

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