wasm_interp_fast.c 300 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_interp.h"
  6. #include "bh_log.h"
  7. #include "wasm_runtime.h"
  8. #include "wasm_opcode.h"
  9. #include "wasm_loader.h"
  10. #include "wasm_memory.h"
  11. #include "../common/wasm_exec_env.h"
  12. #if WASM_ENABLE_GC != 0
  13. #include "../common/gc/gc_object.h"
  14. #include "mem_alloc.h"
  15. #if WASM_ENABLE_STRINGREF != 0
  16. #include "string_object.h"
  17. #endif
  18. #endif
  19. #if WASM_ENABLE_SHARED_MEMORY != 0
  20. #include "../common/wasm_shared_memory.h"
  21. #endif
  22. #if WASM_ENABLE_SIMDE != 0
  23. #include "simde/wasm/simd128.h"
  24. #endif
  25. typedef int32 CellType_I32;
  26. typedef int64 CellType_I64;
  27. typedef float32 CellType_F32;
  28. typedef float64 CellType_F64;
  29. #if WASM_ENABLE_THREAD_MGR == 0
  30. #define get_linear_mem_size() linear_mem_size
  31. #else
  32. /**
  33. * Load memory data size in each time boundary check in
  34. * multi-threading mode since it may be changed by other
  35. * threads in memory.grow
  36. */
  37. #define get_linear_mem_size() GET_LINEAR_MEMORY_SIZE(memory)
  38. #endif
  39. #if WASM_ENABLE_SHARED_HEAP != 0
  40. #define app_addr_in_shared_heap(app_addr, bytes) \
  41. (shared_heap && (app_addr) >= shared_heap_start_off \
  42. && (app_addr) <= shared_heap_end_off - bytes + 1)
  43. #define shared_heap_addr_app_to_native(app_addr, native_addr) \
  44. native_addr = shared_heap_base_addr + ((app_addr)-shared_heap_start_off)
  45. #define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr) \
  46. if (app_addr_in_shared_heap(app_addr, bytes)) \
  47. shared_heap_addr_app_to_native(app_addr, native_addr); \
  48. else
  49. #else
  50. #define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr)
  51. #endif
  52. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  53. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  54. #define CHECK_MEMORY_OVERFLOW(bytes) \
  55. do { \
  56. uint64 offset1 = (uint64)offset + (uint64)addr; \
  57. CHECK_SHARED_HEAP_OVERFLOW(offset1, bytes, maddr) \
  58. if (disable_bounds_checks || offset1 + bytes <= get_linear_mem_size()) \
  59. /* If offset1 is in valid range, maddr must also \
  60. be in valid range, no need to check it again. */ \
  61. maddr = memory->memory_data + offset1; \
  62. else \
  63. goto out_of_bounds; \
  64. } while (0)
  65. #define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
  66. do { \
  67. uint64 offset1 = (uint32)(start); \
  68. CHECK_SHARED_HEAP_OVERFLOW(offset1, bytes, maddr) \
  69. if (disable_bounds_checks || offset1 + bytes <= get_linear_mem_size()) \
  70. /* App heap space is not valid space for \
  71. bulk memory operation */ \
  72. maddr = memory->memory_data + offset1; \
  73. else \
  74. goto out_of_bounds; \
  75. } while (0)
  76. #else
  77. #define CHECK_MEMORY_OVERFLOW(bytes) \
  78. do { \
  79. uint64 offset1 = (uint64)offset + (uint64)addr; \
  80. CHECK_SHARED_HEAP_OVERFLOW(offset1, bytes, maddr) \
  81. maddr = memory->memory_data + offset1; \
  82. } while (0)
  83. #define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
  84. do { \
  85. uint64 offset1 = (uint32)(start); \
  86. CHECK_SHARED_HEAP_OVERFLOW(offset1, bytes, maddr) \
  87. maddr = memory->memory_data + offset1; \
  88. } while (0)
  89. #endif /* !defined(OS_ENABLE_HW_BOUND_CHECK) \
  90. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 */
  91. #define CHECK_ATOMIC_MEMORY_ACCESS(align) \
  92. do { \
  93. if (((uintptr_t)maddr & (align - 1)) != 0) \
  94. goto unaligned_atomic; \
  95. } while (0)
  96. #if WASM_ENABLE_INSTRUCTION_METERING != 0
  97. #define CHECK_INSTRUCTION_LIMIT() \
  98. if (instructions_left == 0) { \
  99. wasm_set_exception(module, "instruction limit exceeded"); \
  100. goto got_exception; \
  101. } \
  102. else if (instructions_left > 0) \
  103. instructions_left--;
  104. #else
  105. #define CHECK_INSTRUCTION_LIMIT() (void)0
  106. #endif
  107. static inline uint32
  108. rotl32(uint32 n, uint32 c)
  109. {
  110. const uint32 mask = (31);
  111. c = c % 32;
  112. c &= mask;
  113. return (n << c) | (n >> ((0 - c) & mask));
  114. }
  115. static inline uint32
  116. rotr32(uint32 n, uint32 c)
  117. {
  118. const uint32 mask = (31);
  119. c = c % 32;
  120. c &= mask;
  121. return (n >> c) | (n << ((0 - c) & mask));
  122. }
  123. static inline uint64
  124. rotl64(uint64 n, uint64 c)
  125. {
  126. const uint64 mask = (63);
  127. c = c % 64;
  128. c &= mask;
  129. return (n << c) | (n >> ((0 - c) & mask));
  130. }
  131. static inline uint64
  132. rotr64(uint64 n, uint64 c)
  133. {
  134. const uint64 mask = (63);
  135. c = c % 64;
  136. c &= mask;
  137. return (n >> c) | (n << ((0 - c) & mask));
  138. }
  139. static inline float32
  140. f32_min(float32 a, float32 b)
  141. {
  142. if (isnan(a) || isnan(b))
  143. return NAN;
  144. else if (a == 0 && a == b)
  145. return signbit(a) ? a : b;
  146. else
  147. return a > b ? b : a;
  148. }
  149. static inline float32
  150. f32_max(float32 a, float32 b)
  151. {
  152. if (isnan(a) || isnan(b))
  153. return NAN;
  154. else if (a == 0 && a == b)
  155. return signbit(a) ? b : a;
  156. else
  157. return a > b ? a : b;
  158. }
  159. static inline float64
  160. f64_min(float64 a, float64 b)
  161. {
  162. if (isnan(a) || isnan(b))
  163. return NAN;
  164. else if (a == 0 && a == b)
  165. return signbit(a) ? a : b;
  166. else
  167. return a > b ? b : a;
  168. }
  169. static inline float64
  170. f64_max(float64 a, float64 b)
  171. {
  172. if (isnan(a) || isnan(b))
  173. return NAN;
  174. else if (a == 0 && a == b)
  175. return signbit(a) ? b : a;
  176. else
  177. return a > b ? a : b;
  178. }
  179. static inline uint32
  180. clz32(uint32 type)
  181. {
  182. uint32 num = 0;
  183. if (type == 0)
  184. return 32;
  185. while (!(type & 0x80000000)) {
  186. num++;
  187. type <<= 1;
  188. }
  189. return num;
  190. }
  191. static inline uint32
  192. clz64(uint64 type)
  193. {
  194. uint32 num = 0;
  195. if (type == 0)
  196. return 64;
  197. while (!(type & 0x8000000000000000LL)) {
  198. num++;
  199. type <<= 1;
  200. }
  201. return num;
  202. }
  203. static inline uint32
  204. ctz32(uint32 type)
  205. {
  206. uint32 num = 0;
  207. if (type == 0)
  208. return 32;
  209. while (!(type & 1)) {
  210. num++;
  211. type >>= 1;
  212. }
  213. return num;
  214. }
  215. static inline uint32
  216. ctz64(uint64 type)
  217. {
  218. uint32 num = 0;
  219. if (type == 0)
  220. return 64;
  221. while (!(type & 1)) {
  222. num++;
  223. type >>= 1;
  224. }
  225. return num;
  226. }
  227. static inline uint32
  228. popcount32(uint32 u)
  229. {
  230. uint32 ret = 0;
  231. while (u) {
  232. u = (u & (u - 1));
  233. ret++;
  234. }
  235. return ret;
  236. }
  237. static inline uint32
  238. popcount64(uint64 u)
  239. {
  240. uint32 ret = 0;
  241. while (u) {
  242. u = (u & (u - 1));
  243. ret++;
  244. }
  245. return ret;
  246. }
  247. static float
  248. local_copysignf(float x, float y)
  249. {
  250. union {
  251. float f;
  252. uint32 i;
  253. } ux = { x }, uy = { y };
  254. ux.i &= 0x7fffffff;
  255. ux.i |= uy.i & 0x80000000;
  256. return ux.f;
  257. }
  258. static double
  259. local_copysign(double x, double y)
  260. {
  261. union {
  262. double f;
  263. uint64 i;
  264. } ux = { x }, uy = { y };
  265. ux.i &= UINT64_MAX / 2;
  266. ux.i |= uy.i & 1ULL << 63;
  267. return ux.f;
  268. }
  269. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  270. #define LOAD_U32_WITH_2U16S(addr) (*(uint32 *)(addr))
  271. #define LOAD_PTR(addr) (*(void **)(addr))
  272. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  273. static inline uint32
  274. LOAD_U32_WITH_2U16S(void *addr)
  275. {
  276. union {
  277. uint32 val;
  278. uint16 u16[2];
  279. } u;
  280. bh_assert(((uintptr_t)addr & 1) == 0);
  281. u.u16[0] = ((uint16 *)addr)[0];
  282. u.u16[1] = ((uint16 *)addr)[1];
  283. return u.val;
  284. }
  285. #if UINTPTR_MAX == UINT32_MAX
  286. #define LOAD_PTR(addr) ((void *)LOAD_U32_WITH_2U16S(addr))
  287. #elif UINTPTR_MAX == UINT64_MAX
  288. static inline void *
  289. LOAD_PTR(void *addr)
  290. {
  291. uintptr_t addr1 = (uintptr_t)addr;
  292. union {
  293. void *val;
  294. uint32 u32[2];
  295. uint16 u16[4];
  296. } u;
  297. bh_assert(((uintptr_t)addr & 1) == 0);
  298. if ((addr1 & (uintptr_t)7) == 0)
  299. return *(void **)addr;
  300. if ((addr1 & (uintptr_t)3) == 0) {
  301. u.u32[0] = ((uint32 *)addr)[0];
  302. u.u32[1] = ((uint32 *)addr)[1];
  303. }
  304. else {
  305. u.u16[0] = ((uint16 *)addr)[0];
  306. u.u16[1] = ((uint16 *)addr)[1];
  307. u.u16[2] = ((uint16 *)addr)[2];
  308. u.u16[3] = ((uint16 *)addr)[3];
  309. }
  310. return u.val;
  311. }
  312. #endif /* end of UINTPTR_MAX */
  313. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  314. #if WASM_ENABLE_GC != 0
  315. static void
  316. init_frame_refs(uint8 *frame_ref, uint32 cell_num, WASMFunctionInstance *func)
  317. {
  318. uint32 i, j;
  319. memset(frame_ref, 0, cell_num);
  320. for (i = 0, j = 0; i < func->param_count; i++) {
  321. if (wasm_is_type_reftype(func->param_types[i])
  322. && !wasm_is_reftype_i31ref(func->param_types[i])) {
  323. frame_ref[j++] = 1;
  324. #if UINTPTR_MAX == UINT64_MAX
  325. frame_ref[j++] = 1;
  326. #endif
  327. }
  328. else {
  329. j += wasm_value_type_cell_num(func->param_types[i]);
  330. }
  331. }
  332. for (i = 0; i < func->local_count; i++) {
  333. if (wasm_is_type_reftype(func->local_types[i])
  334. && !wasm_is_reftype_i31ref(func->local_types[i])) {
  335. frame_ref[j++] = 1;
  336. #if UINTPTR_MAX == UINT64_MAX
  337. frame_ref[j++] = 1;
  338. #endif
  339. }
  340. else {
  341. j += wasm_value_type_cell_num(func->local_types[i]);
  342. }
  343. }
  344. }
  345. uint8 *
  346. wasm_interp_get_frame_ref(WASMInterpFrame *frame)
  347. {
  348. return frame->frame_ref;
  349. }
  350. /* Return the corresponding ref slot of the given slot of local
  351. variable or stack pointer. */
  352. #define COMPUTE_FRAME_REF(ref, off) (ref + (unsigned)(off))
  353. #define FRAME_REF(off) COMPUTE_FRAME_REF(frame_ref, off)
  354. #if UINTPTR_MAX == UINT64_MAX
  355. #define SET_FRAME_REF(off) *FRAME_REF(off) = *FRAME_REF(off + 1) = 1
  356. #define CLEAR_FRAME_REF(off) \
  357. (unsigned)off >= local_cell_num \
  358. ? (*FRAME_REF(off) = *FRAME_REF(off + 1) = 0) \
  359. : (void)0
  360. #else
  361. #define SET_FRAME_REF(off) *FRAME_REF(off) = 1
  362. #define CLEAR_FRAME_REF(off) \
  363. (unsigned)off >= local_cell_num ? (*FRAME_REF(off) = 0) : (void)0
  364. #endif
  365. #define FRAME_REF_FOR(frame, p) \
  366. COMPUTE_FRAME_REF(frame->frame_ref, p - frame->lp)
  367. #define CLEAR_FRAME_REF_FOR(p, n) \
  368. do { \
  369. int32 ref_i, ref_n = (int32)(n); \
  370. uint8 *ref = FRAME_REF(p - frame_lp); \
  371. for (ref_i = 0; ref_i < ref_n; ref_i++) \
  372. ref[ref_i] = 0; \
  373. } while (0)
  374. #endif /* end of WASM_ENABLE_GC != 0 */
  375. #define read_uint32(p) \
  376. (p += sizeof(uint32), LOAD_U32_WITH_2U16S(p - sizeof(uint32)))
  377. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  378. do { \
  379. uint32 param_count = cur_func->param_count; \
  380. local_idx = read_uint32(frame_ip); \
  381. bh_assert(local_idx < param_count + cur_func->local_count); \
  382. local_offset = cur_func->local_offsets[local_idx]; \
  383. if (local_idx < param_count) \
  384. local_type = cur_func->param_types[local_idx]; \
  385. else \
  386. local_type = cur_func->local_types[local_idx - param_count]; \
  387. } while (0)
  388. #define GET_OFFSET() (frame_ip += 2, *(int16 *)(frame_ip - 2))
  389. #define SET_OPERAND_I32(off, value) \
  390. do { \
  391. *(uint32 *)(frame_lp + *(int16 *)(frame_ip + off)) = value; \
  392. } while (0)
  393. #define SET_OPERAND_F32(off, value) \
  394. do { \
  395. *(float32 *)(frame_lp + *(int16 *)(frame_ip + off)) = value; \
  396. } while (0)
  397. #define SET_OPERAND_I64(off, value) \
  398. do { \
  399. uint32 *addr_tmp = frame_lp + *(int16 *)(frame_ip + off); \
  400. PUT_I64_TO_ADDR(addr_tmp, value); \
  401. } while (0)
  402. #define SET_OPERAND_F64(off, value) \
  403. do { \
  404. uint32 *addr_tmp = frame_lp + *(int16 *)(frame_ip + off); \
  405. PUT_F64_TO_ADDR(addr_tmp, value); \
  406. } while (0)
  407. #define SET_OPERAND_REF(off, value) \
  408. do { \
  409. uint32 *addr_tmp; \
  410. opnd_off = *(int16 *)(frame_ip + off); \
  411. addr_tmp = frame_lp + opnd_off; \
  412. PUT_REF_TO_ADDR(addr_tmp, value); \
  413. SET_FRAME_REF(ond_off); \
  414. } while (0)
  415. #define SET_OPERAND(op_type, off, value) SET_OPERAND_##op_type(off, value)
  416. #define GET_OPERAND_I32(type, off) \
  417. *(type *)(frame_lp + *(int16 *)(frame_ip + off))
  418. #define GET_OPERAND_F32(type, off) \
  419. *(type *)(frame_lp + *(int16 *)(frame_ip + off))
  420. #define GET_OPERAND_I64(type, off) \
  421. (type) GET_I64_FROM_ADDR(frame_lp + *(int16 *)(frame_ip + off))
  422. #define GET_OPERAND_F64(type, off) \
  423. (type) GET_F64_FROM_ADDR(frame_lp + *(int16 *)(frame_ip + off))
  424. #define GET_OPERAND_V128(off) \
  425. GET_V128_FROM_ADDR(frame_lp + *(int16 *)(frame_ip + off))
  426. #define GET_OPERAND_REF(type, off) \
  427. (type) GET_REF_FROM_ADDR(frame_lp + *(int16 *)(frame_ip + off))
  428. #define GET_OPERAND(type, op_type, off) GET_OPERAND_##op_type(type, off)
  429. #define PUSH_I32(value) \
  430. do { \
  431. *(int32 *)(frame_lp + GET_OFFSET()) = value; \
  432. } while (0)
  433. #define PUSH_F32(value) \
  434. do { \
  435. *(float32 *)(frame_lp + GET_OFFSET()) = value; \
  436. } while (0)
  437. #define PUSH_I64(value) \
  438. do { \
  439. uint32 *addr_tmp = frame_lp + GET_OFFSET(); \
  440. PUT_I64_TO_ADDR(addr_tmp, value); \
  441. } while (0)
  442. #define PUSH_F64(value) \
  443. do { \
  444. uint32 *addr_tmp = frame_lp + GET_OFFSET(); \
  445. PUT_F64_TO_ADDR(addr_tmp, value); \
  446. } while (0)
  447. #define PUSH_REF(value) \
  448. do { \
  449. uint32 *addr_tmp; \
  450. opnd_off = GET_OFFSET(); \
  451. addr_tmp = frame_lp + opnd_off; \
  452. PUT_REF_TO_ADDR(addr_tmp, value); \
  453. SET_FRAME_REF(opnd_off); \
  454. } while (0)
  455. #define PUSH_I31REF(value) \
  456. do { \
  457. uint32 *addr_tmp; \
  458. opnd_off = GET_OFFSET(); \
  459. addr_tmp = frame_lp + opnd_off; \
  460. PUT_REF_TO_ADDR(addr_tmp, value); \
  461. } while (0)
  462. #define POP_I32() (*(int32 *)(frame_lp + GET_OFFSET()))
  463. #define POP_F32() (*(float32 *)(frame_lp + GET_OFFSET()))
  464. #define POP_I64() (GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()))
  465. #define POP_V128() (GET_V128_FROM_ADDR(frame_lp + GET_OFFSET()))
  466. #define POP_F64() (GET_F64_FROM_ADDR(frame_lp + GET_OFFSET()))
  467. #define POP_REF() \
  468. (opnd_off = GET_OFFSET(), CLEAR_FRAME_REF((unsigned)(opnd_off)), \
  469. GET_REF_FROM_ADDR(frame_lp + opnd_off))
  470. #if WASM_ENABLE_GC != 0
  471. #define SYNC_FRAME_REF() frame->frame_ref = frame_ref
  472. #define UPDATE_FRAME_REF() frame_ref = frame->frame_ref
  473. #else
  474. #define SYNC_FRAME_REF() (void)0
  475. #define UPDATE_FRAME_REF() (void)0
  476. #endif
  477. #define SYNC_ALL_TO_FRAME() \
  478. do { \
  479. frame->ip = frame_ip; \
  480. SYNC_FRAME_REF(); \
  481. } while (0)
  482. #define UPDATE_ALL_FROM_FRAME() \
  483. do { \
  484. frame_ip = frame->ip; \
  485. UPDATE_FRAME_REF(); \
  486. } while (0)
  487. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  488. #define UPDATE_FRAME_IP_END() (void)0
  489. #else
  490. #define UPDATE_FRAME_IP_END() frame_ip_end = wasm_get_func_code_end(cur_func)
  491. #endif
  492. #if WASM_ENABLE_GC != 0
  493. #define RECOVER_FRAME_REF() frame_ref = frame->frame_ref
  494. #else
  495. #define RECOVER_FRAME_REF() (void)0
  496. #endif
  497. #define RECOVER_CONTEXT(new_frame) \
  498. do { \
  499. frame = (new_frame); \
  500. cur_func = frame->function; \
  501. prev_frame = frame->prev_frame; \
  502. frame_ip = frame->ip; \
  503. UPDATE_FRAME_IP_END(); \
  504. frame_lp = frame->lp; \
  505. RECOVER_FRAME_REF(); \
  506. } while (0)
  507. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  508. #define GET_OPCODE() opcode = *frame_ip++;
  509. #else
  510. #define GET_OPCODE() \
  511. opcode = *frame_ip; \
  512. frame_ip += 2;
  513. #endif
  514. #define DEF_OP_EQZ(ctype, src_op_type) \
  515. do { \
  516. SET_OPERAND(I32, 2, (GET_OPERAND(ctype, src_op_type, 0) == 0)); \
  517. frame_ip += 4; \
  518. } while (0)
  519. #define DEF_OP_CMP(src_type, src_op_type, cond) \
  520. do { \
  521. SET_OPERAND(I32, 4, \
  522. GET_OPERAND(src_type, src_op_type, 2) \
  523. cond GET_OPERAND(src_type, src_op_type, 0)); \
  524. frame_ip += 6; \
  525. } while (0)
  526. #define DEF_OP_BIT_COUNT(src_type, src_op_type, operation) \
  527. do { \
  528. SET_OPERAND( \
  529. src_op_type, 2, \
  530. (src_type)operation(GET_OPERAND(src_type, src_op_type, 0))); \
  531. frame_ip += 4; \
  532. } while (0)
  533. #define DEF_OP_NUMERIC(src_type1, src_type2, src_op_type, operation) \
  534. do { \
  535. SET_OPERAND(src_op_type, 4, \
  536. GET_OPERAND(src_type1, src_op_type, 2) \
  537. operation GET_OPERAND(src_type2, src_op_type, 0)); \
  538. frame_ip += 6; \
  539. } while (0)
  540. #define DEF_OP_REINTERPRET(src_type, src_op_type) \
  541. do { \
  542. SET_OPERAND(src_op_type, 2, GET_OPERAND(src_type, src_op_type, 0)); \
  543. frame_ip += 4; \
  544. } while (0)
  545. #define DEF_OP_NUMERIC_64 DEF_OP_NUMERIC
  546. #define DEF_OP_NUMERIC2(src_type1, src_type2, src_op_type, operation) \
  547. do { \
  548. SET_OPERAND(src_op_type, 4, \
  549. GET_OPERAND(src_type1, src_op_type, 2) operation( \
  550. GET_OPERAND(src_type2, src_op_type, 0) % 32)); \
  551. frame_ip += 6; \
  552. } while (0)
  553. #define DEF_OP_NUMERIC2_64(src_type1, src_type2, src_op_type, operation) \
  554. do { \
  555. SET_OPERAND(src_op_type, 4, \
  556. GET_OPERAND(src_type1, src_op_type, 2) operation( \
  557. GET_OPERAND(src_type2, src_op_type, 0) % 64)); \
  558. frame_ip += 6; \
  559. } while (0)
  560. #define DEF_ATOMIC_RMW_OPCODE(OP_NAME, op) \
  561. case WASM_OP_ATOMIC_RMW_I32_##OP_NAME: \
  562. case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U: \
  563. case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U: \
  564. { \
  565. uint32 readv, sval; \
  566. \
  567. sval = POP_I32(); \
  568. addr = POP_I32(); \
  569. \
  570. if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U) { \
  571. CHECK_MEMORY_OVERFLOW(1); \
  572. CHECK_ATOMIC_MEMORY_ACCESS(1); \
  573. \
  574. shared_memory_lock(memory); \
  575. readv = (uint32)(*(uint8 *)maddr); \
  576. *(uint8 *)maddr = (uint8)(readv op sval); \
  577. shared_memory_unlock(memory); \
  578. } \
  579. else if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U) { \
  580. CHECK_MEMORY_OVERFLOW(2); \
  581. CHECK_ATOMIC_MEMORY_ACCESS(2); \
  582. \
  583. shared_memory_lock(memory); \
  584. readv = (uint32)LOAD_U16(maddr); \
  585. STORE_U16(maddr, (uint16)(readv op sval)); \
  586. shared_memory_unlock(memory); \
  587. } \
  588. else { \
  589. CHECK_MEMORY_OVERFLOW(4); \
  590. CHECK_ATOMIC_MEMORY_ACCESS(4); \
  591. \
  592. shared_memory_lock(memory); \
  593. readv = LOAD_I32(maddr); \
  594. STORE_U32(maddr, readv op sval); \
  595. shared_memory_unlock(memory); \
  596. } \
  597. PUSH_I32(readv); \
  598. break; \
  599. } \
  600. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME: \
  601. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U: \
  602. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U: \
  603. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U: \
  604. { \
  605. uint64 readv, sval; \
  606. \
  607. sval = (uint64)POP_I64(); \
  608. addr = POP_I32(); \
  609. \
  610. if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U) { \
  611. CHECK_MEMORY_OVERFLOW(1); \
  612. CHECK_ATOMIC_MEMORY_ACCESS(1); \
  613. \
  614. shared_memory_lock(memory); \
  615. readv = (uint64)(*(uint8 *)maddr); \
  616. *(uint8 *)maddr = (uint8)(readv op sval); \
  617. shared_memory_unlock(memory); \
  618. } \
  619. else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U) { \
  620. CHECK_MEMORY_OVERFLOW(2); \
  621. CHECK_ATOMIC_MEMORY_ACCESS(2); \
  622. \
  623. shared_memory_lock(memory); \
  624. readv = (uint64)LOAD_U16(maddr); \
  625. STORE_U16(maddr, (uint16)(readv op sval)); \
  626. shared_memory_unlock(memory); \
  627. } \
  628. else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U) { \
  629. CHECK_MEMORY_OVERFLOW(4); \
  630. CHECK_ATOMIC_MEMORY_ACCESS(4); \
  631. \
  632. shared_memory_lock(memory); \
  633. readv = (uint64)LOAD_U32(maddr); \
  634. STORE_U32(maddr, (uint32)(readv op sval)); \
  635. shared_memory_unlock(memory); \
  636. } \
  637. else { \
  638. uint64 op_result; \
  639. CHECK_MEMORY_OVERFLOW(8); \
  640. CHECK_ATOMIC_MEMORY_ACCESS(8); \
  641. \
  642. shared_memory_lock(memory); \
  643. readv = (uint64)LOAD_I64(maddr); \
  644. op_result = readv op sval; \
  645. STORE_I64(maddr, op_result); \
  646. shared_memory_unlock(memory); \
  647. } \
  648. PUSH_I64(readv); \
  649. break; \
  650. }
  651. #define DEF_OP_MATH(src_type, src_op_type, method) \
  652. do { \
  653. SET_OPERAND(src_op_type, 2, \
  654. (src_type)method(GET_OPERAND(src_type, src_op_type, 0))); \
  655. frame_ip += 4; \
  656. } while (0)
  657. #define TRUNC_FUNCTION(func_name, src_type, dst_type, signed_type) \
  658. static dst_type func_name(src_type src_value, src_type src_min, \
  659. src_type src_max, dst_type dst_min, \
  660. dst_type dst_max, bool is_sign) \
  661. { \
  662. dst_type dst_value = 0; \
  663. if (!isnan(src_value)) { \
  664. if (src_value <= src_min) \
  665. dst_value = dst_min; \
  666. else if (src_value >= src_max) \
  667. dst_value = dst_max; \
  668. else { \
  669. if (is_sign) \
  670. dst_value = (dst_type)(signed_type)src_value; \
  671. else \
  672. dst_value = (dst_type)src_value; \
  673. } \
  674. } \
  675. return dst_value; \
  676. }
  677. TRUNC_FUNCTION(trunc_f32_to_i32, float32, uint32, int32)
  678. TRUNC_FUNCTION(trunc_f32_to_i64, float32, uint64, int64)
  679. TRUNC_FUNCTION(trunc_f64_to_i32, float64, uint32, int32)
  680. TRUNC_FUNCTION(trunc_f64_to_i64, float64, uint64, int64)
  681. static bool
  682. trunc_f32_to_int(WASMModuleInstance *module, uint8 *frame_ip, uint32 *frame_lp,
  683. float32 src_min, float32 src_max, bool saturating, bool is_i32,
  684. bool is_sign)
  685. {
  686. float32 src_value = GET_OPERAND(float32, F32, 0);
  687. uint64 dst_value_i64;
  688. uint32 dst_value_i32;
  689. if (!saturating) {
  690. if (isnan(src_value)) {
  691. wasm_set_exception(module, "invalid conversion to integer");
  692. return false;
  693. }
  694. else if (src_value <= src_min || src_value >= src_max) {
  695. wasm_set_exception(module, "integer overflow");
  696. return false;
  697. }
  698. }
  699. if (is_i32) {
  700. uint32 dst_min = is_sign ? INT32_MIN : 0;
  701. uint32 dst_max = is_sign ? INT32_MAX : UINT32_MAX;
  702. dst_value_i32 = trunc_f32_to_i32(src_value, src_min, src_max, dst_min,
  703. dst_max, is_sign);
  704. SET_OPERAND(I32, 2, dst_value_i32);
  705. }
  706. else {
  707. uint64 dst_min = is_sign ? INT64_MIN : 0;
  708. uint64 dst_max = is_sign ? INT64_MAX : UINT64_MAX;
  709. dst_value_i64 = trunc_f32_to_i64(src_value, src_min, src_max, dst_min,
  710. dst_max, is_sign);
  711. SET_OPERAND(I64, 2, dst_value_i64);
  712. }
  713. return true;
  714. }
  715. static bool
  716. trunc_f64_to_int(WASMModuleInstance *module, uint8 *frame_ip, uint32 *frame_lp,
  717. float64 src_min, float64 src_max, bool saturating, bool is_i32,
  718. bool is_sign)
  719. {
  720. float64 src_value = GET_OPERAND(float64, F64, 0);
  721. uint64 dst_value_i64;
  722. uint32 dst_value_i32;
  723. if (!saturating) {
  724. if (isnan(src_value)) {
  725. wasm_set_exception(module, "invalid conversion to integer");
  726. return false;
  727. }
  728. else if (src_value <= src_min || src_value >= src_max) {
  729. wasm_set_exception(module, "integer overflow");
  730. return false;
  731. }
  732. }
  733. if (is_i32) {
  734. uint32 dst_min = is_sign ? INT32_MIN : 0;
  735. uint32 dst_max = is_sign ? INT32_MAX : UINT32_MAX;
  736. dst_value_i32 = trunc_f64_to_i32(src_value, src_min, src_max, dst_min,
  737. dst_max, is_sign);
  738. SET_OPERAND(I32, 2, dst_value_i32);
  739. }
  740. else {
  741. uint64 dst_min = is_sign ? INT64_MIN : 0;
  742. uint64 dst_max = is_sign ? INT64_MAX : UINT64_MAX;
  743. dst_value_i64 = trunc_f64_to_i64(src_value, src_min, src_max, dst_min,
  744. dst_max, is_sign);
  745. SET_OPERAND(I64, 2, dst_value_i64);
  746. }
  747. return true;
  748. }
  749. #define DEF_OP_TRUNC_F32(min, max, is_i32, is_sign) \
  750. do { \
  751. if (!trunc_f32_to_int(module, frame_ip, frame_lp, min, max, false, \
  752. is_i32, is_sign)) \
  753. goto got_exception; \
  754. frame_ip += 4; \
  755. } while (0)
  756. #define DEF_OP_TRUNC_F64(min, max, is_i32, is_sign) \
  757. do { \
  758. if (!trunc_f64_to_int(module, frame_ip, frame_lp, min, max, false, \
  759. is_i32, is_sign)) \
  760. goto got_exception; \
  761. frame_ip += 4; \
  762. } while (0)
  763. #define DEF_OP_TRUNC_SAT_F32(min, max, is_i32, is_sign) \
  764. do { \
  765. (void)trunc_f32_to_int(module, frame_ip, frame_lp, min, max, true, \
  766. is_i32, is_sign); \
  767. frame_ip += 4; \
  768. } while (0)
  769. #define DEF_OP_TRUNC_SAT_F64(min, max, is_i32, is_sign) \
  770. do { \
  771. (void)trunc_f64_to_int(module, frame_ip, frame_lp, min, max, true, \
  772. is_i32, is_sign); \
  773. frame_ip += 4; \
  774. } while (0)
  775. #define DEF_OP_CONVERT(dst_type, dst_op_type, src_type, src_op_type) \
  776. do { \
  777. dst_type value = (dst_type)(src_type)POP_##src_op_type(); \
  778. PUSH_##dst_op_type(value); \
  779. } while (0)
  780. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  781. #define CELL_SIZE sizeof(uint8)
  782. #else
  783. #define CELL_SIZE (sizeof(uint8) * 2)
  784. #endif
  785. static bool
  786. copy_stack_values(WASMModuleInstance *module, uint32 *frame_lp, uint32 arity,
  787. #if WASM_ENABLE_GC != 0
  788. uint8 *frame_ref,
  789. #endif
  790. uint32 total_cell_num, const uint8 *cells,
  791. const int16 *src_offsets, const uint16 *dst_offsets)
  792. {
  793. /* To avoid the overlap issue between src offsets and dst offset,
  794. * we use 2 steps to do the copy. First step, copy the src values
  795. * to a tmp buf. Second step, copy the values from tmp buf to dst.
  796. */
  797. bool ret = false;
  798. uint32 buf[16] = { 0 }, i;
  799. uint32 *tmp_buf = buf;
  800. uint8 cell;
  801. int16 src, buf_index = 0;
  802. uint16 dst;
  803. #if WASM_ENABLE_GC != 0
  804. uint8 ref_buf[4];
  805. uint8 *tmp_ref_buf = ref_buf;
  806. #endif
  807. /* Allocate memory if the buf is not large enough */
  808. if (total_cell_num > sizeof(buf) / sizeof(uint32)) {
  809. uint64 total_size = sizeof(uint32) * (uint64)total_cell_num;
  810. if (total_size >= UINT32_MAX
  811. || !(tmp_buf = wasm_runtime_malloc((uint32)total_size))) {
  812. wasm_set_exception(module, "allocate memory failed");
  813. goto fail;
  814. }
  815. }
  816. #if WASM_ENABLE_GC != 0
  817. if (total_cell_num > sizeof(ref_buf) / sizeof(uint8)) {
  818. uint64 total_size = sizeof(uint8) * (uint64)total_cell_num;
  819. if (total_size >= UINT32_MAX
  820. || !(tmp_ref_buf = wasm_runtime_malloc((uint32)total_size))) {
  821. wasm_set_exception(module, "allocate memory failed");
  822. goto fail;
  823. }
  824. }
  825. #endif
  826. /* 1) Copy values from src to tmp buf */
  827. for (i = 0; i < arity; i++) {
  828. cell = cells[i * CELL_SIZE];
  829. src = src_offsets[i];
  830. if (cell == 1) {
  831. tmp_buf[buf_index] = frame_lp[src];
  832. #if WASM_ENABLE_GC != 0
  833. tmp_ref_buf[buf_index] = frame_ref[src];
  834. frame_ref[src] = 0;
  835. #endif
  836. }
  837. else {
  838. tmp_buf[buf_index] = frame_lp[src];
  839. tmp_buf[buf_index + 1] = frame_lp[src + 1];
  840. #if WASM_ENABLE_GC != 0
  841. tmp_ref_buf[buf_index] = frame_ref[src];
  842. tmp_ref_buf[buf_index + 1] = frame_ref[src + 1];
  843. frame_ref[src] = 0;
  844. frame_ref[src + 1] = 0;
  845. #endif
  846. }
  847. buf_index += cell;
  848. }
  849. /* 2) Copy values from tmp buf to dest */
  850. buf_index = 0;
  851. for (i = 0; i < arity; i++) {
  852. cell = cells[i * CELL_SIZE];
  853. dst = dst_offsets[i];
  854. if (cell == 1) {
  855. frame_lp[dst] = tmp_buf[buf_index];
  856. #if WASM_ENABLE_GC != 0
  857. frame_ref[dst] = tmp_ref_buf[buf_index];
  858. #endif
  859. }
  860. else {
  861. frame_lp[dst] = tmp_buf[buf_index];
  862. frame_lp[dst + 1] = tmp_buf[buf_index + 1];
  863. #if WASM_ENABLE_GC != 0
  864. frame_ref[dst] = tmp_ref_buf[buf_index];
  865. frame_ref[dst + 1] = tmp_ref_buf[buf_index + 1];
  866. #endif
  867. }
  868. buf_index += cell;
  869. }
  870. ret = true;
  871. fail:
  872. if (tmp_buf != buf) {
  873. wasm_runtime_free(tmp_buf);
  874. }
  875. #if WASM_ENABLE_GC != 0
  876. if (tmp_ref_buf != ref_buf) {
  877. wasm_runtime_free(tmp_ref_buf);
  878. }
  879. #endif
  880. return ret;
  881. }
  882. #if WASM_ENABLE_GC != 0
  883. #define RECOVER_BR_INFO() \
  884. do { \
  885. uint32 arity; \
  886. /* read arity */ \
  887. arity = read_uint32(frame_ip); \
  888. if (arity) { \
  889. uint32 total_cell; \
  890. uint16 *dst_offsets = NULL; \
  891. uint8 *cells; \
  892. int16 *src_offsets = NULL; \
  893. /* read total cell num */ \
  894. total_cell = read_uint32(frame_ip); \
  895. /* cells */ \
  896. cells = (uint8 *)frame_ip; \
  897. frame_ip += arity * CELL_SIZE; \
  898. /* src offsets */ \
  899. src_offsets = (int16 *)frame_ip; \
  900. frame_ip += arity * sizeof(int16); \
  901. /* dst offsets */ \
  902. dst_offsets = (uint16 *)frame_ip; \
  903. frame_ip += arity * sizeof(uint16); \
  904. if (arity == 1) { \
  905. if (cells[0] == 1) { \
  906. frame_lp[dst_offsets[0]] = frame_lp[src_offsets[0]]; \
  907. /* Ignore constants because they are not reference */ \
  908. if (src_offsets[0] >= 0) { \
  909. CLEAR_FRAME_REF((unsigned)(src_offsets[0])); \
  910. SET_FRAME_REF(dst_offsets[0]); \
  911. } \
  912. } \
  913. else if (cells[0] == 2) { \
  914. PUT_I64_TO_ADDR( \
  915. frame_lp + dst_offsets[0], \
  916. GET_I64_FROM_ADDR(frame_lp + src_offsets[0])); \
  917. /* Ignore constants because they are not reference */ \
  918. if (src_offsets[0] >= 0) { \
  919. CLEAR_FRAME_REF((unsigned)src_offsets[0]); \
  920. CLEAR_FRAME_REF((unsigned)(src_offsets[0] + 1)); \
  921. SET_FRAME_REF((unsigned)dst_offsets[0]); \
  922. SET_FRAME_REF((unsigned)(dst_offsets[0] + 1)); \
  923. } \
  924. } \
  925. else if (cells[0] == 4) { \
  926. PUT_V128_TO_ADDR( \
  927. frame_lp + dst_offsets[0], \
  928. GET_V128_FROM_ADDR(frame_lp + src_offsets[0])); \
  929. /* Ignore constants because they are not reference */ \
  930. if (src_offsets[0] >= 0) { \
  931. CLEAR_FRAME_REF((unsigned)src_offsets[0]); \
  932. CLEAR_FRAME_REF((unsigned)(src_offsets[0] + 1)); \
  933. CLEAR_FRAME_REF((unsigned)(src_offsets[0] + 2)); \
  934. CLEAR_FRAME_REF((unsigned)(src_offsets[0] + 3)); \
  935. SET_FRAME_REF((unsigned)dst_offsets[0]); \
  936. SET_FRAME_REF((unsigned)(dst_offsets[0] + 1)); \
  937. SET_FRAME_REF((unsigned)(dst_offsets[0] + 2)); \
  938. SET_FRAME_REF((unsigned)(dst_offsets[0] + 3)); \
  939. } \
  940. } \
  941. } \
  942. else { \
  943. if (!copy_stack_values(module, frame_lp, arity, frame_ref, \
  944. total_cell, cells, src_offsets, \
  945. dst_offsets)) \
  946. goto got_exception; \
  947. } \
  948. } \
  949. frame_ip = (uint8 *)LOAD_PTR(frame_ip); \
  950. } while (0)
  951. #else
  952. #define RECOVER_BR_INFO() \
  953. do { \
  954. uint32 arity; \
  955. /* read arity */ \
  956. arity = read_uint32(frame_ip); \
  957. if (arity) { \
  958. uint32 total_cell; \
  959. uint16 *dst_offsets = NULL; \
  960. uint8 *cells; \
  961. int16 *src_offsets = NULL; \
  962. /* read total cell num */ \
  963. total_cell = read_uint32(frame_ip); \
  964. /* cells */ \
  965. cells = (uint8 *)frame_ip; \
  966. frame_ip += arity * CELL_SIZE; \
  967. /* src offsets */ \
  968. src_offsets = (int16 *)frame_ip; \
  969. frame_ip += arity * sizeof(int16); \
  970. /* dst offsets */ \
  971. dst_offsets = (uint16 *)frame_ip; \
  972. frame_ip += arity * sizeof(uint16); \
  973. if (arity == 1) { \
  974. if (cells[0] == 1) \
  975. frame_lp[dst_offsets[0]] = frame_lp[src_offsets[0]]; \
  976. else if (cells[0] == 2) { \
  977. PUT_I64_TO_ADDR( \
  978. frame_lp + dst_offsets[0], \
  979. GET_I64_FROM_ADDR(frame_lp + src_offsets[0])); \
  980. } \
  981. else if (cells[0] == 4) { \
  982. PUT_V128_TO_ADDR( \
  983. frame_lp + dst_offsets[0], \
  984. GET_V128_FROM_ADDR(frame_lp + src_offsets[0])); \
  985. } \
  986. } \
  987. else { \
  988. if (!copy_stack_values(module, frame_lp, arity, total_cell, \
  989. cells, src_offsets, dst_offsets)) \
  990. goto got_exception; \
  991. } \
  992. } \
  993. frame_ip = (uint8 *)LOAD_PTR(frame_ip); \
  994. } while (0)
  995. #endif
  996. #define SKIP_BR_INFO() \
  997. do { \
  998. uint32 arity; \
  999. /* read and skip arity */ \
  1000. arity = read_uint32(frame_ip); \
  1001. if (arity) { \
  1002. /* skip total cell num */ \
  1003. frame_ip += sizeof(uint32); \
  1004. /* skip cells, src offsets and dst offsets */ \
  1005. frame_ip += (CELL_SIZE + sizeof(int16) + sizeof(uint16)) * arity; \
  1006. } \
  1007. /* skip target address */ \
  1008. frame_ip += sizeof(uint8 *); \
  1009. } while (0)
  1010. static inline int32
  1011. sign_ext_8_32(int8 val)
  1012. {
  1013. if (val & 0x80)
  1014. return (int32)val | (int32)0xffffff00;
  1015. return val;
  1016. }
  1017. static inline int32
  1018. sign_ext_16_32(int16 val)
  1019. {
  1020. if (val & 0x8000)
  1021. return (int32)val | (int32)0xffff0000;
  1022. return val;
  1023. }
  1024. static inline int64
  1025. sign_ext_8_64(int8 val)
  1026. {
  1027. if (val & 0x80)
  1028. return (int64)val | (int64)0xffffffffffffff00LL;
  1029. return val;
  1030. }
  1031. static inline int64
  1032. sign_ext_16_64(int16 val)
  1033. {
  1034. if (val & 0x8000)
  1035. return (int64)val | (int64)0xffffffffffff0000LL;
  1036. return val;
  1037. }
  1038. static inline int64
  1039. sign_ext_32_64(int32 val)
  1040. {
  1041. if (val & (int32)0x80000000)
  1042. return (int64)val | (int64)0xffffffff00000000LL;
  1043. return val;
  1044. }
  1045. static inline void
  1046. word_copy(uint32 *dest, uint32 *src, unsigned num)
  1047. {
  1048. bh_assert(dest != NULL);
  1049. bh_assert(src != NULL);
  1050. bh_assert(num > 0);
  1051. if (dest != src) {
  1052. /* No overlap buffer */
  1053. bh_assert(!((src < dest) && (dest < src + num)));
  1054. for (; num > 0; num--)
  1055. *dest++ = *src++;
  1056. }
  1057. }
  1058. static inline WASMInterpFrame *
  1059. ALLOC_FRAME(WASMExecEnv *exec_env, uint32 size, WASMInterpFrame *prev_frame)
  1060. {
  1061. WASMInterpFrame *frame = wasm_exec_env_alloc_wasm_frame(exec_env, size);
  1062. if (frame) {
  1063. frame->prev_frame = prev_frame;
  1064. #if WASM_ENABLE_PERF_PROFILING != 0
  1065. frame->time_started = os_time_thread_cputime_us();
  1066. #endif
  1067. }
  1068. else {
  1069. wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
  1070. "wasm operand stack overflow");
  1071. }
  1072. return frame;
  1073. }
  1074. static inline void
  1075. FREE_FRAME(WASMExecEnv *exec_env, WASMInterpFrame *frame)
  1076. {
  1077. #if WASM_ENABLE_PERF_PROFILING != 0
  1078. if (frame->function) {
  1079. WASMInterpFrame *prev_frame = frame->prev_frame;
  1080. uint64 time_elapsed = os_time_thread_cputime_us() - frame->time_started;
  1081. frame->function->total_exec_time += time_elapsed;
  1082. frame->function->total_exec_cnt++;
  1083. /* parent function */
  1084. if (prev_frame && prev_frame->function)
  1085. prev_frame->function->children_exec_time += time_elapsed;
  1086. }
  1087. #endif
  1088. wasm_exec_env_free_wasm_frame(exec_env, frame);
  1089. }
  1090. static void
  1091. wasm_interp_call_func_native(WASMModuleInstance *module_inst,
  1092. WASMExecEnv *exec_env,
  1093. WASMFunctionInstance *cur_func,
  1094. WASMInterpFrame *prev_frame)
  1095. {
  1096. WASMFunctionImport *func_import = cur_func->u.func_import;
  1097. CApiFuncImport *c_api_func_import = NULL;
  1098. unsigned local_cell_num =
  1099. cur_func->param_cell_num > 2 ? cur_func->param_cell_num : 2;
  1100. unsigned all_cell_num;
  1101. WASMInterpFrame *frame;
  1102. uint32 argv_ret[2], cur_func_index;
  1103. void *native_func_pointer = NULL;
  1104. bool ret;
  1105. #if WASM_ENABLE_GC != 0
  1106. WASMFuncType *func_type;
  1107. uint8 *frame_ref;
  1108. #endif
  1109. all_cell_num = local_cell_num;
  1110. #if WASM_ENABLE_GC != 0
  1111. all_cell_num += (local_cell_num + 3) / 4;
  1112. #endif
  1113. if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
  1114. return;
  1115. }
  1116. if (!(frame =
  1117. ALLOC_FRAME(exec_env, wasm_interp_interp_frame_size(all_cell_num),
  1118. prev_frame)))
  1119. return;
  1120. frame->function = cur_func;
  1121. frame->ip = NULL;
  1122. frame->lp = frame->operand;
  1123. #if WASM_ENABLE_GC != 0
  1124. frame->frame_ref = (uint8 *)(frame->lp + local_cell_num);
  1125. init_frame_refs(frame->frame_ref, local_cell_num, cur_func);
  1126. #endif
  1127. wasm_exec_env_set_cur_frame(exec_env, frame);
  1128. cur_func_index = (uint32)(cur_func - module_inst->e->functions);
  1129. bh_assert(cur_func_index < module_inst->module->import_function_count);
  1130. if (!func_import->call_conv_wasm_c_api) {
  1131. native_func_pointer = module_inst->import_func_ptrs[cur_func_index];
  1132. }
  1133. else if (module_inst->c_api_func_imports) {
  1134. c_api_func_import = module_inst->c_api_func_imports + cur_func_index;
  1135. native_func_pointer = c_api_func_import->func_ptr_linked;
  1136. }
  1137. if (!native_func_pointer) {
  1138. char buf[128];
  1139. snprintf(buf, sizeof(buf),
  1140. "failed to call unlinked import function (%s, %s)",
  1141. func_import->module_name, func_import->field_name);
  1142. wasm_set_exception((WASMModuleInstance *)module_inst, buf);
  1143. return;
  1144. }
  1145. if (func_import->call_conv_wasm_c_api) {
  1146. ret = wasm_runtime_invoke_c_api_native(
  1147. (WASMModuleInstanceCommon *)module_inst, native_func_pointer,
  1148. func_import->func_type, cur_func->param_cell_num, frame->lp,
  1149. c_api_func_import->with_env_arg, c_api_func_import->env_arg);
  1150. if (ret) {
  1151. argv_ret[0] = frame->lp[0];
  1152. argv_ret[1] = frame->lp[1];
  1153. }
  1154. }
  1155. else if (!func_import->call_conv_raw) {
  1156. ret = wasm_runtime_invoke_native(
  1157. exec_env, native_func_pointer, func_import->func_type,
  1158. func_import->signature, func_import->attachment, frame->lp,
  1159. cur_func->param_cell_num, argv_ret);
  1160. }
  1161. else {
  1162. ret = wasm_runtime_invoke_native_raw(
  1163. exec_env, native_func_pointer, func_import->func_type,
  1164. func_import->signature, func_import->attachment, frame->lp,
  1165. cur_func->param_cell_num, argv_ret);
  1166. }
  1167. if (!ret)
  1168. return;
  1169. #if WASM_ENABLE_GC != 0
  1170. func_type = cur_func->u.func_import->func_type;
  1171. if (func_type->result_count
  1172. && wasm_is_type_reftype(func_type->types[cur_func->param_count])
  1173. && !wasm_is_reftype_i31ref(func_type->types[cur_func->param_count])) {
  1174. frame_ref = prev_frame->frame_ref + prev_frame->ret_offset;
  1175. #if UINTPTR_MAX == UINT64_MAX
  1176. *frame_ref = *(frame_ref + 1) = 1;
  1177. #else
  1178. *frame_ref = 1;
  1179. #endif
  1180. }
  1181. #endif
  1182. if (cur_func->ret_cell_num == 1) {
  1183. prev_frame->lp[prev_frame->ret_offset] = argv_ret[0];
  1184. }
  1185. else if (cur_func->ret_cell_num == 2) {
  1186. prev_frame->lp[prev_frame->ret_offset] = argv_ret[0];
  1187. prev_frame->lp[prev_frame->ret_offset + 1] = argv_ret[1];
  1188. }
  1189. FREE_FRAME(exec_env, frame);
  1190. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  1191. }
  1192. #if WASM_ENABLE_MULTI_MODULE != 0
  1193. static void
  1194. wasm_interp_call_func_bytecode(WASMModuleInstance *module,
  1195. WASMExecEnv *exec_env,
  1196. WASMFunctionInstance *cur_func,
  1197. WASMInterpFrame *prev_frame);
  1198. static void
  1199. wasm_interp_call_func_import(WASMModuleInstance *module_inst,
  1200. WASMExecEnv *exec_env,
  1201. WASMFunctionInstance *cur_func,
  1202. WASMInterpFrame *prev_frame)
  1203. {
  1204. WASMModuleInstance *sub_module_inst = cur_func->import_module_inst;
  1205. WASMFunctionInstance *sub_func_inst = cur_func->import_func_inst;
  1206. WASMFunctionImport *func_import = cur_func->u.func_import;
  1207. uint8 *ip = prev_frame->ip;
  1208. char buf[128];
  1209. WASMExecEnv *sub_module_exec_env = NULL;
  1210. uintptr_t aux_stack_origin_boundary = 0;
  1211. uintptr_t aux_stack_origin_bottom = 0;
  1212. /*
  1213. * perform stack overflow check before calling
  1214. * wasm_interp_call_func_bytecode recursively.
  1215. */
  1216. if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
  1217. return;
  1218. }
  1219. if (!sub_func_inst) {
  1220. snprintf(buf, sizeof(buf),
  1221. "failed to call unlinked import function (%s, %s)",
  1222. func_import->module_name, func_import->field_name);
  1223. wasm_set_exception(module_inst, buf);
  1224. return;
  1225. }
  1226. /* Switch exec_env but keep using the same one by replacing necessary
  1227. * variables */
  1228. sub_module_exec_env = wasm_runtime_get_exec_env_singleton(
  1229. (WASMModuleInstanceCommon *)sub_module_inst);
  1230. if (!sub_module_exec_env) {
  1231. wasm_set_exception(module_inst, "create singleton exec_env failed");
  1232. return;
  1233. }
  1234. /* - module_inst */
  1235. wasm_exec_env_set_module_inst(exec_env,
  1236. (WASMModuleInstanceCommon *)sub_module_inst);
  1237. /* - aux_stack_boundary */
  1238. aux_stack_origin_boundary = exec_env->aux_stack_boundary;
  1239. exec_env->aux_stack_boundary = sub_module_exec_env->aux_stack_boundary;
  1240. /* - aux_stack_bottom */
  1241. aux_stack_origin_bottom = exec_env->aux_stack_bottom;
  1242. exec_env->aux_stack_bottom = sub_module_exec_env->aux_stack_bottom;
  1243. /* set ip NULL to make call_func_bytecode return after executing
  1244. this function */
  1245. prev_frame->ip = NULL;
  1246. /* call function of sub-module*/
  1247. wasm_interp_call_func_bytecode(sub_module_inst, exec_env, sub_func_inst,
  1248. prev_frame);
  1249. /* restore ip and other replaced */
  1250. prev_frame->ip = ip;
  1251. exec_env->aux_stack_boundary = aux_stack_origin_boundary;
  1252. exec_env->aux_stack_bottom = aux_stack_origin_bottom;
  1253. wasm_exec_env_restore_module_inst(exec_env,
  1254. (WASMModuleInstanceCommon *)module_inst);
  1255. }
  1256. #endif
  1257. #if WASM_ENABLE_THREAD_MGR != 0
  1258. #define CHECK_SUSPEND_FLAGS() \
  1259. do { \
  1260. WASM_SUSPEND_FLAGS_LOCK(exec_env->wait_lock); \
  1261. if (WASM_SUSPEND_FLAGS_GET(exec_env->suspend_flags) \
  1262. & WASM_SUSPEND_FLAG_TERMINATE) { \
  1263. /* terminate current thread */ \
  1264. WASM_SUSPEND_FLAGS_UNLOCK(exec_env->wait_lock); \
  1265. return; \
  1266. } \
  1267. /* TODO: support suspend and breakpoint */ \
  1268. WASM_SUSPEND_FLAGS_UNLOCK(exec_env->wait_lock); \
  1269. } while (0)
  1270. #endif
  1271. #if WASM_ENABLE_OPCODE_COUNTER != 0
  1272. typedef struct OpcodeInfo {
  1273. char *name;
  1274. uint64 count;
  1275. } OpcodeInfo;
  1276. /* clang-format off */
  1277. #define HANDLE_OPCODE(op) \
  1278. { \
  1279. #op, 0 \
  1280. }
  1281. DEFINE_GOTO_TABLE(OpcodeInfo, opcode_table);
  1282. #undef HANDLE_OPCODE
  1283. /* clang-format on */
  1284. static void
  1285. wasm_interp_dump_op_count()
  1286. {
  1287. uint32 i;
  1288. uint64 total_count = 0;
  1289. for (i = 0; i < WASM_OP_IMPDEP; i++)
  1290. total_count += opcode_table[i].count;
  1291. os_printf("total opcode count: %ld\n", total_count);
  1292. for (i = 0; i < WASM_OP_IMPDEP; i++)
  1293. if (opcode_table[i].count > 0)
  1294. os_printf("\t\t%s count:\t\t%ld,\t\t%.2f%%\n", opcode_table[i].name,
  1295. opcode_table[i].count,
  1296. opcode_table[i].count * 100.0f / total_count);
  1297. }
  1298. #endif
  1299. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1300. /* #define HANDLE_OP(opcode) HANDLE_##opcode:printf(#opcode"\n"); */
  1301. #if WASM_ENABLE_OPCODE_COUNTER != 0
  1302. #define HANDLE_OP(opcode) HANDLE_##opcode : opcode_table[opcode].count++;
  1303. #else
  1304. #define HANDLE_OP(opcode) HANDLE_##opcode:
  1305. #endif
  1306. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  1307. #define FETCH_OPCODE_AND_DISPATCH() \
  1308. do { \
  1309. const void *p_label_addr = *(void **)frame_ip; \
  1310. frame_ip += sizeof(void *); \
  1311. CHECK_INSTRUCTION_LIMIT(); \
  1312. goto *p_label_addr; \
  1313. } while (0)
  1314. #else
  1315. #if UINTPTR_MAX == UINT64_MAX
  1316. #define FETCH_OPCODE_AND_DISPATCH() \
  1317. do { \
  1318. const void *p_label_addr; \
  1319. bh_assert(((uintptr_t)frame_ip & 1) == 0); \
  1320. /* int32 relative offset was emitted in 64-bit target */ \
  1321. p_label_addr = label_base + (int32)LOAD_U32_WITH_2U16S(frame_ip); \
  1322. frame_ip += sizeof(int32); \
  1323. CHECK_INSTRUCTION_LIMIT(); \
  1324. goto *p_label_addr; \
  1325. } while (0)
  1326. #else
  1327. #define FETCH_OPCODE_AND_DISPATCH() \
  1328. do { \
  1329. const void *p_label_addr; \
  1330. bh_assert(((uintptr_t)frame_ip & 1) == 0); \
  1331. /* uint32 label address was emitted in 32-bit target */ \
  1332. p_label_addr = (void *)(uintptr_t)LOAD_U32_WITH_2U16S(frame_ip); \
  1333. frame_ip += sizeof(int32); \
  1334. CHECK_INSTRUCTION_LIMIT(); \
  1335. goto *p_label_addr; \
  1336. } while (0)
  1337. #endif
  1338. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  1339. #define HANDLE_OP_END() FETCH_OPCODE_AND_DISPATCH()
  1340. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  1341. #define HANDLE_OP(opcode) case opcode:
  1342. #define HANDLE_OP_END() continue
  1343. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  1344. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1345. static void **global_handle_table;
  1346. #endif
  1347. static inline uint8 *
  1348. get_global_addr(uint8 *global_data, WASMGlobalInstance *global)
  1349. {
  1350. #if WASM_ENABLE_MULTI_MODULE == 0
  1351. return global_data + global->data_offset;
  1352. #else
  1353. return global->import_global_inst
  1354. ? global->import_module_inst->global_data
  1355. + global->import_global_inst->data_offset
  1356. : global_data + global->data_offset;
  1357. #endif
  1358. }
  1359. static void
  1360. wasm_interp_call_func_bytecode(WASMModuleInstance *module,
  1361. WASMExecEnv *exec_env,
  1362. WASMFunctionInstance *cur_func,
  1363. WASMInterpFrame *prev_frame)
  1364. {
  1365. WASMMemoryInstance *memory = wasm_get_default_memory(module);
  1366. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  1367. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  1368. || WASM_ENABLE_BULK_MEMORY != 0
  1369. uint64 linear_mem_size = 0;
  1370. if (memory)
  1371. #if WASM_ENABLE_THREAD_MGR == 0
  1372. linear_mem_size = memory->memory_data_size;
  1373. #else
  1374. linear_mem_size = GET_LINEAR_MEMORY_SIZE(memory);
  1375. #endif
  1376. #endif
  1377. WASMGlobalInstance *globals = module->e ? module->e->globals : NULL;
  1378. WASMGlobalInstance *global;
  1379. uint8 *global_data = module->global_data;
  1380. uint8 opcode_IMPDEP = WASM_OP_IMPDEP;
  1381. WASMInterpFrame *frame = NULL;
  1382. /* Points to this special opcode so as to jump to the
  1383. * call_method_from_entry. */
  1384. register uint8 *frame_ip = &opcode_IMPDEP; /* cache of frame->ip */
  1385. register uint32 *frame_lp = NULL; /* cache of frame->lp */
  1386. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1387. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 && UINTPTR_MAX == UINT64_MAX
  1388. /* cache of label base addr */
  1389. register uint8 *label_base = &&HANDLE_WASM_OP_UNREACHABLE;
  1390. #endif
  1391. #endif
  1392. #if WASM_ENABLE_GC != 0
  1393. register uint8 *frame_ref = NULL; /* cache of frame->ref */
  1394. uint32 local_cell_num = 0;
  1395. int16 opnd_off;
  1396. #endif
  1397. uint8 *frame_ip_end = frame_ip + 1;
  1398. uint32 cond, count, fidx, tidx, frame_size = 0;
  1399. uint32 all_cell_num = 0;
  1400. int16 addr1, addr2, addr_ret = 0;
  1401. int32 didx, val;
  1402. uint8 *maddr = NULL;
  1403. uint32 local_idx, local_offset, global_idx;
  1404. uint8 opcode = 0, local_type, *global_addr;
  1405. #if WASM_ENABLE_INSTRUCTION_METERING != 0
  1406. int instructions_left = -1;
  1407. if (exec_env) {
  1408. instructions_left = exec_env->instructions_to_execute;
  1409. }
  1410. #endif
  1411. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  1412. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  1413. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  1414. bool disable_bounds_checks = !wasm_runtime_is_bounds_checks_enabled(
  1415. (WASMModuleInstanceCommon *)module);
  1416. #else
  1417. bool disable_bounds_checks = false;
  1418. #endif
  1419. #endif
  1420. #if WASM_ENABLE_GC != 0
  1421. WASMObjectRef gc_obj;
  1422. WASMStructObjectRef struct_obj;
  1423. WASMArrayObjectRef array_obj;
  1424. WASMFuncObjectRef func_obj;
  1425. WASMI31ObjectRef i31_obj;
  1426. WASMExternrefObjectRef externref_obj;
  1427. uint32 type_idx;
  1428. #if WASM_ENABLE_STRINGREF != 0
  1429. WASMString str_obj;
  1430. WASMStringrefObjectRef stringref_obj;
  1431. WASMStringviewWTF8ObjectRef stringview_wtf8_obj;
  1432. WASMStringviewWTF16ObjectRef stringview_wtf16_obj;
  1433. WASMStringviewIterObjectRef stringview_iter_obj;
  1434. #endif
  1435. #endif
  1436. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  1437. bool is_return_call = false;
  1438. #endif
  1439. #if WASM_ENABLE_SHARED_HEAP != 0
  1440. WASMSharedHeap *shared_heap = module->e ? module->e->shared_heap : NULL;
  1441. uint8 *shared_heap_base_addr = shared_heap ? shared_heap->base_addr : NULL;
  1442. /*
  1443. #if WASM_ENABLE_MEMORY64 != 0
  1444. uint64 shared_heap_start_off =
  1445. shared_heap ? (is_memory64 ? shared_heap->start_off_mem64
  1446. : shared_heap->start_off_mem32)
  1447. : 0;
  1448. uint64 shared_heap_end_off =
  1449. shared_heap ? (is_memory64 ? UINT64_MAX : UINT32_MAX) : 0;
  1450. #else
  1451. */ /* TODO: uncomment the code when memory64 is enabled for fast-interp */
  1452. uint64 shared_heap_start_off =
  1453. shared_heap ? shared_heap->start_off_mem32 : 0;
  1454. uint64 shared_heap_end_off = shared_heap ? UINT32_MAX : 0;
  1455. /* #endif */
  1456. #endif /* end of WASM_ENABLE_SHARED_HEAP != 0 */
  1457. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1458. #define HANDLE_OPCODE(op) &&HANDLE_##op
  1459. DEFINE_GOTO_TABLE(const void *, handle_table);
  1460. #undef HANDLE_OPCODE
  1461. if (exec_env == NULL) {
  1462. global_handle_table = (void **)handle_table;
  1463. return;
  1464. }
  1465. #endif
  1466. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  1467. while (frame_ip < frame_ip_end) {
  1468. opcode = *frame_ip++;
  1469. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  1470. frame_ip++;
  1471. #endif
  1472. switch (opcode) {
  1473. #else
  1474. goto *handle_table[WASM_OP_IMPDEP];
  1475. #endif
  1476. /* control instructions */
  1477. HANDLE_OP(WASM_OP_UNREACHABLE)
  1478. {
  1479. wasm_set_exception(module, "unreachable");
  1480. goto got_exception;
  1481. }
  1482. HANDLE_OP(WASM_OP_IF)
  1483. {
  1484. cond = (uint32)POP_I32();
  1485. if (cond == 0) {
  1486. uint8 *else_addr = (uint8 *)LOAD_PTR(frame_ip);
  1487. if (else_addr == NULL) {
  1488. frame_ip =
  1489. (uint8 *)LOAD_PTR(frame_ip + sizeof(uint8 *));
  1490. }
  1491. else {
  1492. frame_ip = else_addr;
  1493. }
  1494. }
  1495. else {
  1496. frame_ip += sizeof(uint8 *) * 2;
  1497. }
  1498. HANDLE_OP_END();
  1499. }
  1500. HANDLE_OP(WASM_OP_ELSE)
  1501. {
  1502. frame_ip = (uint8 *)LOAD_PTR(frame_ip);
  1503. HANDLE_OP_END();
  1504. }
  1505. HANDLE_OP(WASM_OP_BR)
  1506. {
  1507. #if WASM_ENABLE_THREAD_MGR != 0
  1508. CHECK_SUSPEND_FLAGS();
  1509. #endif
  1510. recover_br_info:
  1511. RECOVER_BR_INFO();
  1512. HANDLE_OP_END();
  1513. }
  1514. HANDLE_OP(WASM_OP_BR_IF)
  1515. {
  1516. #if WASM_ENABLE_THREAD_MGR != 0
  1517. CHECK_SUSPEND_FLAGS();
  1518. #endif
  1519. cond = frame_lp[GET_OFFSET()];
  1520. if (cond)
  1521. goto recover_br_info;
  1522. else
  1523. SKIP_BR_INFO();
  1524. HANDLE_OP_END();
  1525. }
  1526. HANDLE_OP(WASM_OP_BR_TABLE)
  1527. {
  1528. uint32 arity, br_item_size;
  1529. #if WASM_ENABLE_THREAD_MGR != 0
  1530. CHECK_SUSPEND_FLAGS();
  1531. #endif
  1532. count = read_uint32(frame_ip);
  1533. didx = GET_OPERAND(uint32, I32, 0);
  1534. frame_ip += 2;
  1535. if (!(didx >= 0 && (uint32)didx < count))
  1536. didx = count;
  1537. /* all br items must have the same arity and item size,
  1538. so we only calculate the first item size */
  1539. arity = LOAD_U32_WITH_2U16S(frame_ip);
  1540. br_item_size = sizeof(uint32); /* arity */
  1541. if (arity) {
  1542. /* total cell num */
  1543. br_item_size += sizeof(uint32);
  1544. /* cells, src offsets and dst offsets */
  1545. br_item_size +=
  1546. (CELL_SIZE + sizeof(int16) + sizeof(uint16)) * arity;
  1547. }
  1548. /* target address */
  1549. br_item_size += sizeof(uint8 *);
  1550. frame_ip += br_item_size * didx;
  1551. goto recover_br_info;
  1552. }
  1553. HANDLE_OP(WASM_OP_RETURN)
  1554. {
  1555. uint32 ret_idx;
  1556. WASMFuncType *func_type;
  1557. int32 off;
  1558. uint32 ret_offset;
  1559. uint8 *ret_types;
  1560. if (cur_func->is_import_func)
  1561. func_type = cur_func->u.func_import->func_type;
  1562. else
  1563. func_type = cur_func->u.func->func_type;
  1564. /* types of each return value */
  1565. ret_types = func_type->types + func_type->param_count;
  1566. ret_offset = prev_frame->ret_offset;
  1567. for (ret_idx = 0,
  1568. off = (int32)sizeof(int16) * (func_type->result_count - 1);
  1569. ret_idx < func_type->result_count;
  1570. ret_idx++, off -= (int32)sizeof(int16)) {
  1571. if (ret_types[ret_idx] == VALUE_TYPE_I64
  1572. || ret_types[ret_idx] == VALUE_TYPE_F64) {
  1573. PUT_I64_TO_ADDR(prev_frame->lp + ret_offset,
  1574. GET_OPERAND(uint64, I64, off));
  1575. ret_offset += 2;
  1576. }
  1577. else if (ret_types[ret_idx] == VALUE_TYPE_V128) {
  1578. PUT_V128_TO_ADDR(prev_frame->lp + ret_offset,
  1579. GET_OPERAND_V128(off));
  1580. ret_offset += 4;
  1581. }
  1582. #if WASM_ENABLE_GC != 0
  1583. else if (wasm_is_type_reftype(ret_types[ret_idx])) {
  1584. PUT_REF_TO_ADDR(prev_frame->lp + ret_offset,
  1585. GET_OPERAND(void *, REF, off));
  1586. if (!wasm_is_reftype_i31ref(ret_types[ret_idx])) {
  1587. *(prev_frame->frame_ref + ret_offset) = 1;
  1588. #if UINTPTR_MAX == UINT64_MAX
  1589. *(prev_frame->frame_ref + ret_offset + 1) = 1;
  1590. #endif
  1591. }
  1592. ret_offset += REF_CELL_NUM;
  1593. }
  1594. #endif
  1595. else {
  1596. prev_frame->lp[ret_offset] =
  1597. GET_OPERAND(uint32, I32, off);
  1598. ret_offset++;
  1599. }
  1600. }
  1601. goto return_func;
  1602. }
  1603. HANDLE_OP(WASM_OP_CALL_INDIRECT)
  1604. #if WASM_ENABLE_TAIL_CALL != 0
  1605. HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT)
  1606. #endif
  1607. {
  1608. WASMFuncType *cur_type, *cur_func_type;
  1609. WASMTableInstance *tbl_inst;
  1610. uint32 tbl_idx;
  1611. #if WASM_ENABLE_TAIL_CALL != 0
  1612. GET_OPCODE();
  1613. #endif
  1614. #if WASM_ENABLE_THREAD_MGR != 0
  1615. CHECK_SUSPEND_FLAGS();
  1616. #endif
  1617. tidx = read_uint32(frame_ip);
  1618. cur_type = (WASMFuncType *)module->module->types[tidx];
  1619. tbl_idx = read_uint32(frame_ip);
  1620. bh_assert(tbl_idx < module->table_count);
  1621. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  1622. val = GET_OPERAND(uint32, I32, 0);
  1623. frame_ip += 2;
  1624. if ((uint32)val >= tbl_inst->cur_size) {
  1625. wasm_set_exception(module, "undefined element");
  1626. goto got_exception;
  1627. }
  1628. /* clang-format off */
  1629. #if WASM_ENABLE_GC == 0
  1630. fidx = (uint32)tbl_inst->elems[val];
  1631. if (fidx == (uint32)-1) {
  1632. wasm_set_exception(module, "uninitialized element");
  1633. goto got_exception;
  1634. }
  1635. #else
  1636. func_obj = (WASMFuncObjectRef)tbl_inst->elems[val];
  1637. if (!func_obj) {
  1638. wasm_set_exception(module, "uninitialized element");
  1639. goto got_exception;
  1640. }
  1641. fidx = wasm_func_obj_get_func_idx_bound(func_obj);
  1642. #endif
  1643. /* clang-format on */
  1644. /*
  1645. * we might be using a table injected by host or
  1646. * another module. in that case, we don't validate
  1647. * the elem value while loading
  1648. */
  1649. if (fidx >= module->e->function_count) {
  1650. wasm_set_exception(module, "unknown function");
  1651. goto got_exception;
  1652. }
  1653. /* always call module own functions */
  1654. cur_func = module->e->functions + fidx;
  1655. if (cur_func->is_import_func)
  1656. cur_func_type = cur_func->u.func_import->func_type;
  1657. else
  1658. cur_func_type = cur_func->u.func->func_type;
  1659. /* clang-format off */
  1660. #if WASM_ENABLE_GC == 0
  1661. if (cur_type != cur_func_type) {
  1662. wasm_set_exception(module, "indirect call type mismatch");
  1663. goto got_exception;
  1664. }
  1665. #else
  1666. if (!wasm_func_type_is_super_of(cur_type, cur_func_type)) {
  1667. wasm_set_exception(module, "indirect call type mismatch");
  1668. goto got_exception;
  1669. }
  1670. #endif
  1671. /* clang-format on */
  1672. #if WASM_ENABLE_TAIL_CALL != 0
  1673. if (opcode == WASM_OP_RETURN_CALL_INDIRECT)
  1674. goto call_func_from_return_call;
  1675. #endif
  1676. goto call_func_from_interp;
  1677. }
  1678. #if WASM_ENABLE_EXCE_HANDLING != 0
  1679. HANDLE_OP(WASM_OP_TRY)
  1680. HANDLE_OP(WASM_OP_CATCH)
  1681. HANDLE_OP(WASM_OP_THROW)
  1682. HANDLE_OP(WASM_OP_RETHROW)
  1683. HANDLE_OP(WASM_OP_DELEGATE)
  1684. HANDLE_OP(WASM_OP_CATCH_ALL)
  1685. HANDLE_OP(EXT_OP_TRY)
  1686. {
  1687. wasm_set_exception(module, "unsupported opcode");
  1688. goto got_exception;
  1689. }
  1690. #endif
  1691. /* parametric instructions */
  1692. HANDLE_OP(WASM_OP_SELECT)
  1693. {
  1694. cond = frame_lp[GET_OFFSET()];
  1695. addr1 = GET_OFFSET();
  1696. addr2 = GET_OFFSET();
  1697. addr_ret = GET_OFFSET();
  1698. if (!cond) {
  1699. if (addr_ret != addr1)
  1700. frame_lp[addr_ret] = frame_lp[addr1];
  1701. }
  1702. else {
  1703. if (addr_ret != addr2)
  1704. frame_lp[addr_ret] = frame_lp[addr2];
  1705. }
  1706. HANDLE_OP_END();
  1707. }
  1708. HANDLE_OP(WASM_OP_SELECT_64)
  1709. {
  1710. cond = frame_lp[GET_OFFSET()];
  1711. addr1 = GET_OFFSET();
  1712. addr2 = GET_OFFSET();
  1713. addr_ret = GET_OFFSET();
  1714. if (!cond) {
  1715. if (addr_ret != addr1)
  1716. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1717. GET_I64_FROM_ADDR(frame_lp + addr1));
  1718. }
  1719. else {
  1720. if (addr_ret != addr2)
  1721. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1722. GET_I64_FROM_ADDR(frame_lp + addr2));
  1723. }
  1724. HANDLE_OP_END();
  1725. }
  1726. #if WASM_ENABLE_SIMD != 0
  1727. HANDLE_OP(WASM_OP_SELECT_128)
  1728. {
  1729. cond = frame_lp[GET_OFFSET()];
  1730. addr1 = GET_OFFSET();
  1731. addr2 = GET_OFFSET();
  1732. addr_ret = GET_OFFSET();
  1733. if (!cond) {
  1734. if (addr_ret != addr1)
  1735. PUT_V128_TO_ADDR(frame_lp + addr_ret,
  1736. GET_V128_FROM_ADDR(frame_lp + addr1));
  1737. }
  1738. else {
  1739. if (addr_ret != addr2)
  1740. PUT_V128_TO_ADDR(frame_lp + addr_ret,
  1741. GET_V128_FROM_ADDR(frame_lp + addr2));
  1742. }
  1743. HANDLE_OP_END();
  1744. }
  1745. #endif
  1746. #if WASM_ENABLE_GC != 0
  1747. HANDLE_OP(WASM_OP_SELECT_T)
  1748. {
  1749. cond = frame_lp[GET_OFFSET()];
  1750. addr1 = GET_OFFSET();
  1751. addr2 = GET_OFFSET();
  1752. addr_ret = GET_OFFSET();
  1753. if (!cond) {
  1754. if (addr_ret != addr1)
  1755. PUT_REF_TO_ADDR(frame_lp + addr_ret,
  1756. GET_REF_FROM_ADDR(frame_lp + addr1));
  1757. }
  1758. else {
  1759. if (addr_ret != addr2)
  1760. PUT_REF_TO_ADDR(frame_lp + addr_ret,
  1761. GET_REF_FROM_ADDR(frame_lp + addr2));
  1762. }
  1763. {
  1764. uint8 orig_ref = 0;
  1765. /* Ignore constants because they are not reference */
  1766. if (addr1 >= 0) {
  1767. orig_ref = *FRAME_REF(addr1);
  1768. CLEAR_FRAME_REF(addr1);
  1769. }
  1770. if (addr2 >= 0) {
  1771. CLEAR_FRAME_REF(addr2);
  1772. }
  1773. if (orig_ref) {
  1774. SET_FRAME_REF(addr_ret);
  1775. }
  1776. }
  1777. HANDLE_OP_END();
  1778. }
  1779. #endif
  1780. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  1781. HANDLE_OP(WASM_OP_TABLE_GET)
  1782. {
  1783. uint32 tbl_idx, elem_idx;
  1784. WASMTableInstance *tbl_inst;
  1785. tbl_idx = read_uint32(frame_ip);
  1786. bh_assert(tbl_idx < module->table_count);
  1787. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  1788. elem_idx = POP_I32();
  1789. if (elem_idx >= tbl_inst->cur_size) {
  1790. wasm_set_exception(module, "out of bounds table access");
  1791. goto got_exception;
  1792. }
  1793. #if WASM_ENABLE_GC == 0
  1794. PUSH_I32(tbl_inst->elems[elem_idx]);
  1795. #else
  1796. PUSH_REF(tbl_inst->elems[elem_idx]);
  1797. #endif
  1798. HANDLE_OP_END();
  1799. }
  1800. HANDLE_OP(WASM_OP_TABLE_SET)
  1801. {
  1802. uint32 tbl_idx, elem_idx;
  1803. WASMTableInstance *tbl_inst;
  1804. table_elem_type_t elem_val;
  1805. tbl_idx = read_uint32(frame_ip);
  1806. bh_assert(tbl_idx < module->table_count);
  1807. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  1808. #if WASM_ENABLE_GC == 0
  1809. elem_val = POP_I32();
  1810. #else
  1811. elem_val = POP_REF();
  1812. #endif
  1813. elem_idx = POP_I32();
  1814. if (elem_idx >= tbl_inst->cur_size) {
  1815. wasm_set_exception(module, "out of bounds table access");
  1816. goto got_exception;
  1817. }
  1818. tbl_inst->elems[elem_idx] = elem_val;
  1819. HANDLE_OP_END();
  1820. }
  1821. HANDLE_OP(WASM_OP_REF_NULL)
  1822. {
  1823. #if WASM_ENABLE_GC == 0
  1824. PUSH_I32(NULL_REF);
  1825. #else
  1826. PUSH_REF(NULL_REF);
  1827. #endif
  1828. HANDLE_OP_END();
  1829. }
  1830. HANDLE_OP(WASM_OP_REF_IS_NULL)
  1831. {
  1832. #if WASM_ENABLE_GC == 0
  1833. uint32 ref_val;
  1834. ref_val = POP_I32();
  1835. #else
  1836. void *ref_val;
  1837. ref_val = POP_REF();
  1838. #endif
  1839. PUSH_I32(ref_val == NULL_REF ? 1 : 0);
  1840. HANDLE_OP_END();
  1841. }
  1842. HANDLE_OP(WASM_OP_REF_FUNC)
  1843. {
  1844. uint32 func_idx = read_uint32(frame_ip);
  1845. #if WASM_ENABLE_GC == 0
  1846. PUSH_I32(func_idx);
  1847. #else
  1848. SYNC_ALL_TO_FRAME();
  1849. if (!(gc_obj = wasm_create_func_obj(module, func_idx, true,
  1850. NULL, 0))) {
  1851. goto got_exception;
  1852. }
  1853. PUSH_REF(gc_obj);
  1854. #endif
  1855. HANDLE_OP_END();
  1856. }
  1857. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  1858. #if WASM_ENABLE_GC != 0
  1859. HANDLE_OP(WASM_OP_CALL_REF)
  1860. {
  1861. #if WASM_ENABLE_THREAD_MGR != 0
  1862. CHECK_SUSPEND_FLAGS();
  1863. #endif
  1864. func_obj = POP_REF();
  1865. if (!func_obj) {
  1866. wasm_set_exception(module, "null function reference");
  1867. goto got_exception;
  1868. }
  1869. fidx = wasm_func_obj_get_func_idx_bound(func_obj);
  1870. cur_func = module->e->functions + fidx;
  1871. goto call_func_from_interp;
  1872. }
  1873. HANDLE_OP(WASM_OP_RETURN_CALL_REF)
  1874. {
  1875. #if WASM_ENABLE_THREAD_MGR != 0
  1876. CHECK_SUSPEND_FLAGS();
  1877. #endif
  1878. func_obj = POP_REF();
  1879. if (!func_obj) {
  1880. wasm_set_exception(module, "null function reference");
  1881. goto got_exception;
  1882. }
  1883. fidx = wasm_func_obj_get_func_idx_bound(func_obj);
  1884. cur_func = module->e->functions + fidx;
  1885. goto call_func_from_return_call;
  1886. }
  1887. HANDLE_OP(WASM_OP_REF_AS_NON_NULL)
  1888. {
  1889. gc_obj = POP_REF();
  1890. if (gc_obj == NULL_REF) {
  1891. wasm_set_exception(module, "null reference");
  1892. goto got_exception;
  1893. }
  1894. PUSH_REF(gc_obj);
  1895. HANDLE_OP_END();
  1896. }
  1897. HANDLE_OP(WASM_OP_REF_EQ)
  1898. {
  1899. WASMObjectRef gc_obj1, gc_obj2;
  1900. gc_obj2 = POP_REF();
  1901. gc_obj1 = POP_REF();
  1902. val = wasm_obj_equal(gc_obj1, gc_obj2);
  1903. PUSH_I32(val);
  1904. HANDLE_OP_END();
  1905. }
  1906. HANDLE_OP(WASM_OP_BR_ON_NULL)
  1907. {
  1908. #if WASM_ENABLE_THREAD_MGR != 0
  1909. CHECK_SUSPEND_FLAGS();
  1910. #endif
  1911. opnd_off = GET_OFFSET();
  1912. gc_obj = GET_REF_FROM_ADDR(frame_lp + opnd_off);
  1913. if (gc_obj == NULL_REF) {
  1914. CLEAR_FRAME_REF(opnd_off);
  1915. goto recover_br_info;
  1916. }
  1917. else {
  1918. SKIP_BR_INFO();
  1919. }
  1920. HANDLE_OP_END();
  1921. }
  1922. HANDLE_OP(WASM_OP_BR_ON_NON_NULL)
  1923. {
  1924. #if WASM_ENABLE_THREAD_MGR != 0
  1925. CHECK_SUSPEND_FLAGS();
  1926. #endif
  1927. opnd_off = GET_OFFSET();
  1928. gc_obj = GET_REF_FROM_ADDR(frame_lp + opnd_off);
  1929. if (gc_obj != NULL_REF) {
  1930. goto recover_br_info;
  1931. }
  1932. else {
  1933. CLEAR_FRAME_REF(opnd_off);
  1934. SKIP_BR_INFO();
  1935. }
  1936. HANDLE_OP_END();
  1937. }
  1938. HANDLE_OP(WASM_OP_GC_PREFIX)
  1939. {
  1940. GET_OPCODE();
  1941. switch (opcode) {
  1942. case WASM_OP_STRUCT_NEW:
  1943. case WASM_OP_STRUCT_NEW_DEFAULT:
  1944. {
  1945. WASMModule *wasm_module = module->module;
  1946. WASMStructType *struct_type;
  1947. WASMRttType *rtt_type;
  1948. WASMValue field_value = { 0 };
  1949. type_idx = read_uint32(frame_ip);
  1950. struct_type =
  1951. (WASMStructType *)module->module->types[type_idx];
  1952. if (!(rtt_type = wasm_rtt_type_new(
  1953. (WASMType *)struct_type, type_idx,
  1954. wasm_module->rtt_types,
  1955. wasm_module->type_count,
  1956. &wasm_module->rtt_type_lock))) {
  1957. wasm_set_exception(module,
  1958. "create rtt type failed");
  1959. goto got_exception;
  1960. }
  1961. SYNC_ALL_TO_FRAME();
  1962. struct_obj = wasm_struct_obj_new(exec_env, rtt_type);
  1963. if (!struct_obj) {
  1964. wasm_set_exception(module,
  1965. "create struct object failed");
  1966. goto got_exception;
  1967. }
  1968. if (opcode == WASM_OP_STRUCT_NEW) {
  1969. WASMStructFieldType *fields = struct_type->fields;
  1970. int32 field_count = (int32)struct_type->field_count;
  1971. int32 field_idx;
  1972. uint8 field_type;
  1973. for (field_idx = field_count - 1; field_idx >= 0;
  1974. field_idx--) {
  1975. field_type = fields[field_idx].field_type;
  1976. if (wasm_is_type_reftype(field_type)) {
  1977. field_value.gc_obj = POP_REF();
  1978. }
  1979. else if (field_type == VALUE_TYPE_I32
  1980. || field_type == VALUE_TYPE_F32
  1981. || field_type == PACKED_TYPE_I8
  1982. || field_type == PACKED_TYPE_I16) {
  1983. field_value.i32 = POP_I32();
  1984. }
  1985. else {
  1986. field_value.i64 = POP_I64();
  1987. }
  1988. wasm_struct_obj_set_field(struct_obj, field_idx,
  1989. &field_value);
  1990. }
  1991. }
  1992. PUSH_REF(struct_obj);
  1993. HANDLE_OP_END();
  1994. }
  1995. case WASM_OP_STRUCT_GET:
  1996. case WASM_OP_STRUCT_GET_S:
  1997. case WASM_OP_STRUCT_GET_U:
  1998. {
  1999. WASMStructType *struct_type;
  2000. WASMValue field_value = { 0 };
  2001. uint32 field_idx;
  2002. uint8 field_type;
  2003. type_idx = read_uint32(frame_ip);
  2004. field_idx = read_uint32(frame_ip);
  2005. struct_type =
  2006. (WASMStructType *)module->module->types[type_idx];
  2007. struct_obj = POP_REF();
  2008. if (!struct_obj) {
  2009. wasm_set_exception(module,
  2010. "null structure reference");
  2011. goto got_exception;
  2012. }
  2013. wasm_struct_obj_get_field(
  2014. struct_obj, field_idx,
  2015. opcode == WASM_OP_STRUCT_GET_S ? true : false,
  2016. &field_value);
  2017. field_type = struct_type->fields[field_idx].field_type;
  2018. if (wasm_is_reftype_i31ref(field_type)) {
  2019. PUSH_I31REF(field_value.gc_obj);
  2020. }
  2021. else if (wasm_is_type_reftype(field_type)) {
  2022. PUSH_REF(field_value.gc_obj);
  2023. }
  2024. else if (field_type == VALUE_TYPE_I32
  2025. || field_type == VALUE_TYPE_F32
  2026. || field_type == PACKED_TYPE_I8
  2027. || field_type == PACKED_TYPE_I16) {
  2028. PUSH_I32(field_value.i32);
  2029. }
  2030. else {
  2031. PUSH_I64(field_value.i64);
  2032. }
  2033. HANDLE_OP_END();
  2034. }
  2035. case WASM_OP_STRUCT_SET:
  2036. {
  2037. WASMStructType *struct_type;
  2038. WASMValue field_value = { 0 };
  2039. uint32 field_idx;
  2040. uint8 field_type;
  2041. type_idx = read_uint32(frame_ip);
  2042. field_idx = read_uint32(frame_ip);
  2043. struct_type =
  2044. (WASMStructType *)module->module->types[type_idx];
  2045. field_type = struct_type->fields[field_idx].field_type;
  2046. if (wasm_is_type_reftype(field_type)) {
  2047. field_value.gc_obj = POP_REF();
  2048. }
  2049. else if (field_type == VALUE_TYPE_I32
  2050. || field_type == VALUE_TYPE_F32
  2051. || field_type == PACKED_TYPE_I8
  2052. || field_type == PACKED_TYPE_I16) {
  2053. field_value.i32 = POP_I32();
  2054. }
  2055. else {
  2056. field_value.i64 = POP_I64();
  2057. }
  2058. struct_obj = POP_REF();
  2059. if (!struct_obj) {
  2060. wasm_set_exception(module,
  2061. "null structure reference");
  2062. goto got_exception;
  2063. }
  2064. wasm_struct_obj_set_field(struct_obj, field_idx,
  2065. &field_value);
  2066. HANDLE_OP_END();
  2067. }
  2068. case WASM_OP_ARRAY_NEW:
  2069. case WASM_OP_ARRAY_NEW_DEFAULT:
  2070. case WASM_OP_ARRAY_NEW_FIXED:
  2071. {
  2072. WASMModule *wasm_module = module->module;
  2073. WASMArrayType *array_type;
  2074. WASMRttType *rtt_type;
  2075. WASMValue array_elem = { 0 };
  2076. uint32 array_len, i;
  2077. type_idx = read_uint32(frame_ip);
  2078. array_type =
  2079. (WASMArrayType *)wasm_module->types[type_idx];
  2080. if (!(rtt_type = wasm_rtt_type_new(
  2081. (WASMType *)array_type, type_idx,
  2082. wasm_module->rtt_types,
  2083. wasm_module->type_count,
  2084. &wasm_module->rtt_type_lock))) {
  2085. wasm_set_exception(module,
  2086. "create rtt type failed");
  2087. goto got_exception;
  2088. }
  2089. if (opcode != WASM_OP_ARRAY_NEW_FIXED)
  2090. array_len = POP_I32();
  2091. else
  2092. array_len = read_uint32(frame_ip);
  2093. if (opcode == WASM_OP_ARRAY_NEW) {
  2094. if (wasm_is_type_reftype(array_type->elem_type)) {
  2095. array_elem.gc_obj = POP_REF();
  2096. }
  2097. else if (array_type->elem_type == VALUE_TYPE_I32
  2098. || array_type->elem_type == VALUE_TYPE_F32
  2099. || array_type->elem_type == PACKED_TYPE_I8
  2100. || array_type->elem_type
  2101. == PACKED_TYPE_I16) {
  2102. array_elem.i32 = POP_I32();
  2103. }
  2104. else {
  2105. array_elem.i64 = POP_I64();
  2106. }
  2107. }
  2108. SYNC_ALL_TO_FRAME();
  2109. array_obj = wasm_array_obj_new(exec_env, rtt_type,
  2110. array_len, &array_elem);
  2111. if (!array_obj) {
  2112. wasm_set_exception(module,
  2113. "create array object failed");
  2114. goto got_exception;
  2115. }
  2116. if (opcode == WASM_OP_ARRAY_NEW_FIXED) {
  2117. for (i = 0; i < array_len; i++) {
  2118. if (wasm_is_type_reftype(
  2119. array_type->elem_type)) {
  2120. array_elem.gc_obj = POP_REF();
  2121. }
  2122. else if (array_type->elem_type == VALUE_TYPE_I32
  2123. || array_type->elem_type
  2124. == VALUE_TYPE_F32
  2125. || array_type->elem_type
  2126. == PACKED_TYPE_I8
  2127. || array_type->elem_type
  2128. == PACKED_TYPE_I16) {
  2129. array_elem.i32 = POP_I32();
  2130. }
  2131. else {
  2132. array_elem.i64 = POP_I64();
  2133. }
  2134. wasm_array_obj_set_elem(
  2135. array_obj, array_len - 1 - i, &array_elem);
  2136. }
  2137. }
  2138. PUSH_REF(array_obj);
  2139. HANDLE_OP_END();
  2140. }
  2141. case WASM_OP_ARRAY_NEW_DATA:
  2142. {
  2143. WASMModule *wasm_module = module->module;
  2144. WASMArrayType *array_type;
  2145. WASMRttType *rtt_type;
  2146. WASMValue array_elem = { 0 };
  2147. WASMDataSeg *data_seg;
  2148. uint8 *array_elem_base;
  2149. uint32 array_len, data_seg_idx, data_seg_offset;
  2150. uint32 elem_size = 0;
  2151. uint64 total_size;
  2152. type_idx = read_uint32(frame_ip);
  2153. data_seg_idx = read_uint32(frame_ip);
  2154. data_seg = wasm_module->data_segments[data_seg_idx];
  2155. array_type =
  2156. (WASMArrayType *)wasm_module->types[type_idx];
  2157. if (!(rtt_type = wasm_rtt_type_new(
  2158. (WASMType *)array_type, type_idx,
  2159. wasm_module->rtt_types,
  2160. wasm_module->type_count,
  2161. &wasm_module->rtt_type_lock))) {
  2162. wasm_set_exception(module,
  2163. "create rtt type failed");
  2164. goto got_exception;
  2165. }
  2166. array_len = POP_I32();
  2167. data_seg_offset = POP_I32();
  2168. switch (array_type->elem_type) {
  2169. case PACKED_TYPE_I8:
  2170. elem_size = 1;
  2171. break;
  2172. case PACKED_TYPE_I16:
  2173. elem_size = 2;
  2174. break;
  2175. case VALUE_TYPE_I32:
  2176. case VALUE_TYPE_F32:
  2177. elem_size = 4;
  2178. break;
  2179. case VALUE_TYPE_I64:
  2180. case VALUE_TYPE_F64:
  2181. elem_size = 8;
  2182. break;
  2183. default:
  2184. bh_assert(0);
  2185. }
  2186. total_size = (uint64)elem_size * array_len;
  2187. if (data_seg_offset >= data_seg->data_length
  2188. || total_size
  2189. > data_seg->data_length - data_seg_offset) {
  2190. wasm_set_exception(module,
  2191. "data segment out of bounds");
  2192. goto got_exception;
  2193. }
  2194. SYNC_ALL_TO_FRAME();
  2195. array_obj = wasm_array_obj_new(exec_env, rtt_type,
  2196. array_len, &array_elem);
  2197. if (!array_obj) {
  2198. wasm_set_exception(module,
  2199. "create array object failed");
  2200. goto got_exception;
  2201. }
  2202. array_elem_base =
  2203. (uint8 *)wasm_array_obj_first_elem_addr(array_obj);
  2204. bh_memcpy_s(array_elem_base, (uint32)total_size,
  2205. data_seg->data + data_seg_offset,
  2206. (uint32)total_size);
  2207. PUSH_REF(array_obj);
  2208. HANDLE_OP_END();
  2209. }
  2210. case WASM_OP_ARRAY_NEW_ELEM:
  2211. {
  2212. /* TODO */
  2213. wasm_set_exception(module, "unsupported opcode");
  2214. goto got_exception;
  2215. }
  2216. case WASM_OP_ARRAY_GET:
  2217. case WASM_OP_ARRAY_GET_S:
  2218. case WASM_OP_ARRAY_GET_U:
  2219. {
  2220. WASMArrayType *array_type;
  2221. WASMValue array_elem = { 0 };
  2222. uint32 elem_idx, elem_size_log;
  2223. type_idx = read_uint32(frame_ip);
  2224. array_type =
  2225. (WASMArrayType *)module->module->types[type_idx];
  2226. elem_idx = POP_I32();
  2227. array_obj = POP_REF();
  2228. if (!array_obj) {
  2229. wasm_set_exception(module, "null array reference");
  2230. goto got_exception;
  2231. }
  2232. if (elem_idx >= wasm_array_obj_length(array_obj)) {
  2233. wasm_set_exception(module,
  2234. "out of bounds array access");
  2235. goto got_exception;
  2236. }
  2237. wasm_array_obj_get_elem(
  2238. array_obj, elem_idx,
  2239. opcode == WASM_OP_ARRAY_GET_S ? true : false,
  2240. &array_elem);
  2241. elem_size_log = wasm_array_obj_elem_size_log(array_obj);
  2242. if (wasm_is_reftype_i31ref(array_type->elem_type)) {
  2243. PUSH_I31REF(array_elem.gc_obj);
  2244. }
  2245. else if (wasm_is_type_reftype(array_type->elem_type)) {
  2246. PUSH_REF(array_elem.gc_obj);
  2247. }
  2248. else if (elem_size_log < 3) {
  2249. PUSH_I32(array_elem.i32);
  2250. }
  2251. else {
  2252. PUSH_I64(array_elem.i64);
  2253. }
  2254. HANDLE_OP_END();
  2255. }
  2256. case WASM_OP_ARRAY_SET:
  2257. {
  2258. WASMArrayType *array_type;
  2259. WASMValue array_elem = { 0 };
  2260. uint32 elem_idx;
  2261. type_idx = read_uint32(frame_ip);
  2262. array_type =
  2263. (WASMArrayType *)module->module->types[type_idx];
  2264. if (wasm_is_type_reftype(array_type->elem_type)) {
  2265. array_elem.gc_obj = POP_REF();
  2266. }
  2267. else if (array_type->elem_type == VALUE_TYPE_I32
  2268. || array_type->elem_type == VALUE_TYPE_F32
  2269. || array_type->elem_type == PACKED_TYPE_I8
  2270. || array_type->elem_type == PACKED_TYPE_I16) {
  2271. array_elem.i32 = POP_I32();
  2272. }
  2273. else {
  2274. array_elem.i64 = POP_I64();
  2275. }
  2276. elem_idx = POP_I32();
  2277. array_obj = POP_REF();
  2278. if (!array_obj) {
  2279. wasm_set_exception(module, "null array reference");
  2280. goto got_exception;
  2281. }
  2282. if (elem_idx >= wasm_array_obj_length(array_obj)) {
  2283. wasm_set_exception(module,
  2284. "out of bounds array access");
  2285. goto got_exception;
  2286. }
  2287. wasm_array_obj_set_elem(array_obj, elem_idx,
  2288. &array_elem);
  2289. HANDLE_OP_END();
  2290. }
  2291. case WASM_OP_ARRAY_LEN:
  2292. {
  2293. uint32 array_len;
  2294. array_obj = POP_REF();
  2295. if (!array_obj) {
  2296. wasm_set_exception(module, "null array reference");
  2297. goto got_exception;
  2298. }
  2299. array_len = wasm_array_obj_length(array_obj);
  2300. PUSH_I32(array_len);
  2301. HANDLE_OP_END();
  2302. }
  2303. case WASM_OP_ARRAY_FILL:
  2304. {
  2305. WASMArrayType *array_type;
  2306. WASMValue fill_value = { 0 };
  2307. uint32 start_offset, len;
  2308. type_idx = read_uint32(frame_ip);
  2309. array_type =
  2310. (WASMArrayType *)module->module->types[type_idx];
  2311. len = POP_I32();
  2312. if (wasm_is_type_reftype(array_type->elem_type)) {
  2313. fill_value.gc_obj = POP_REF();
  2314. }
  2315. else if (array_type->elem_type == VALUE_TYPE_I32
  2316. || array_type->elem_type == VALUE_TYPE_F32
  2317. || array_type->elem_type == PACKED_TYPE_I8
  2318. || array_type->elem_type == PACKED_TYPE_I16) {
  2319. fill_value.i32 = POP_I32();
  2320. }
  2321. else {
  2322. fill_value.i64 = POP_I64();
  2323. }
  2324. start_offset = POP_I32();
  2325. array_obj = POP_REF();
  2326. if (!array_obj) {
  2327. wasm_set_exception(module, "null array reference");
  2328. goto got_exception;
  2329. }
  2330. if (len > 0) {
  2331. if ((uint64)start_offset + len
  2332. >= wasm_array_obj_length(array_obj)) {
  2333. wasm_set_exception(
  2334. module, "out of bounds array access");
  2335. goto got_exception;
  2336. }
  2337. wasm_array_obj_fill(array_obj, start_offset, len,
  2338. &fill_value);
  2339. }
  2340. HANDLE_OP_END();
  2341. }
  2342. case WASM_OP_ARRAY_COPY:
  2343. {
  2344. uint32 dst_offset, src_offset, len, src_type_index;
  2345. WASMArrayObjectRef src_obj, dst_obj;
  2346. type_idx = read_uint32(frame_ip);
  2347. src_type_index = read_uint32(frame_ip);
  2348. len = POP_I32();
  2349. src_offset = POP_I32();
  2350. src_obj = POP_REF();
  2351. dst_offset = POP_I32();
  2352. dst_obj = POP_REF();
  2353. if (!src_obj || !dst_obj) {
  2354. wasm_set_exception(module, "null array reference");
  2355. goto got_exception;
  2356. }
  2357. if (len > 0) {
  2358. if ((dst_offset > UINT32_MAX - len)
  2359. || (dst_offset + len
  2360. > wasm_array_obj_length(dst_obj))
  2361. || (src_offset > UINT32_MAX - len)
  2362. || (src_offset + len
  2363. > wasm_array_obj_length(src_obj))) {
  2364. wasm_set_exception(
  2365. module, "out of bounds array access");
  2366. goto got_exception;
  2367. }
  2368. wasm_array_obj_copy(dst_obj, dst_offset, src_obj,
  2369. src_offset, len);
  2370. }
  2371. (void)src_type_index;
  2372. HANDLE_OP_END();
  2373. }
  2374. case WASM_OP_REF_I31:
  2375. {
  2376. uint32 i31_val;
  2377. i31_val = POP_I32();
  2378. i31_obj = wasm_i31_obj_new(i31_val);
  2379. PUSH_I31REF(i31_obj);
  2380. HANDLE_OP_END();
  2381. }
  2382. case WASM_OP_I31_GET_S:
  2383. case WASM_OP_I31_GET_U:
  2384. {
  2385. uint32 i31_val;
  2386. i31_obj = (WASMI31ObjectRef)POP_REF();
  2387. if (!i31_obj) {
  2388. wasm_set_exception(module, "null i31 reference");
  2389. goto got_exception;
  2390. }
  2391. i31_val = (uint32)(((uintptr_t)i31_obj) >> 1);
  2392. if (opcode == WASM_OP_I31_GET_S
  2393. && (i31_val & 0x40000000) /* bit 30 is 1 */)
  2394. /* set bit 31 to 1 */
  2395. i31_val |= 0x80000000;
  2396. PUSH_I32(i31_val);
  2397. HANDLE_OP_END();
  2398. }
  2399. case WASM_OP_REF_TEST:
  2400. case WASM_OP_REF_CAST:
  2401. case WASM_OP_REF_TEST_NULLABLE:
  2402. case WASM_OP_REF_CAST_NULLABLE:
  2403. {
  2404. int32 heap_type;
  2405. heap_type = (int32)read_uint32(frame_ip);
  2406. gc_obj = POP_REF();
  2407. if (!gc_obj) {
  2408. if (opcode == WASM_OP_REF_TEST
  2409. || opcode == WASM_OP_REF_TEST_NULLABLE) {
  2410. if (opcode == WASM_OP_REF_TEST)
  2411. PUSH_I32(0);
  2412. else
  2413. PUSH_I32(1);
  2414. }
  2415. else if (opcode == WASM_OP_REF_CAST) {
  2416. wasm_set_exception(module, "cast failure");
  2417. goto got_exception;
  2418. }
  2419. else {
  2420. PUSH_REF(gc_obj);
  2421. }
  2422. }
  2423. else {
  2424. bool castable = false;
  2425. if (heap_type >= 0) {
  2426. WASMModule *wasm_module = module->module;
  2427. castable = wasm_obj_is_instance_of(
  2428. gc_obj, (uint32)heap_type,
  2429. wasm_module->types,
  2430. wasm_module->type_count);
  2431. }
  2432. else {
  2433. castable =
  2434. wasm_obj_is_type_of(gc_obj, heap_type);
  2435. }
  2436. if (opcode == WASM_OP_REF_TEST
  2437. || opcode == WASM_OP_REF_TEST_NULLABLE) {
  2438. if (castable)
  2439. PUSH_I32(1);
  2440. else
  2441. PUSH_I32(0);
  2442. }
  2443. else if (!castable) {
  2444. wasm_set_exception(module, "cast failure");
  2445. goto got_exception;
  2446. }
  2447. else {
  2448. PUSH_REF(gc_obj);
  2449. }
  2450. }
  2451. HANDLE_OP_END();
  2452. }
  2453. case WASM_OP_BR_ON_CAST:
  2454. case WASM_OP_BR_ON_CAST_FAIL:
  2455. {
  2456. int32 heap_type, heap_type_dst;
  2457. uint8 castflags;
  2458. uint16 opnd_off_br;
  2459. #if WASM_ENABLE_THREAD_MGR != 0
  2460. CHECK_SUSPEND_FLAGS();
  2461. #endif
  2462. castflags = *frame_ip++;
  2463. heap_type = (int32)read_uint32(frame_ip);
  2464. heap_type_dst = (int32)read_uint32(frame_ip);
  2465. opnd_off = GET_OFFSET();
  2466. opnd_off_br = GET_OFFSET();
  2467. gc_obj = GET_REF_FROM_ADDR(frame_lp + opnd_off);
  2468. PUT_REF_TO_ADDR(frame_lp + opnd_off_br, gc_obj);
  2469. if (!gc_obj) {
  2470. /*
  2471. * castflags should be 0~3:
  2472. * 0: (non-null, non-null)
  2473. * 1: (null, non-null)
  2474. * 2: (non-null, null)
  2475. * 3: (null, null)
  2476. */
  2477. if (
  2478. /* op is BR_ON_CAST and dst reftype is nullable
  2479. */
  2480. ((opcode == WASM_OP_BR_ON_CAST)
  2481. && ((castflags == 2) || (castflags == 3)))
  2482. /* op is BR_ON_CAST_FAIL and dst reftype is
  2483. non-nullable */
  2484. || ((opcode == WASM_OP_BR_ON_CAST_FAIL)
  2485. && ((castflags == 0)
  2486. || (castflags == 1)))) {
  2487. CLEAR_FRAME_REF(opnd_off);
  2488. if (!wasm_is_reftype_i31ref(heap_type)) {
  2489. SET_FRAME_REF(opnd_off_br);
  2490. }
  2491. goto recover_br_info;
  2492. }
  2493. }
  2494. else {
  2495. bool castable = false;
  2496. if (heap_type_dst >= 0) {
  2497. WASMModule *wasm_module = module->module;
  2498. castable = wasm_obj_is_instance_of(
  2499. gc_obj, (uint32)heap_type_dst,
  2500. wasm_module->types,
  2501. wasm_module->type_count);
  2502. }
  2503. else {
  2504. castable =
  2505. wasm_obj_is_type_of(gc_obj, heap_type_dst);
  2506. }
  2507. if ((castable && (opcode == WASM_OP_BR_ON_CAST))
  2508. || (!castable
  2509. && (opcode == WASM_OP_BR_ON_CAST_FAIL))) {
  2510. CLEAR_FRAME_REF(opnd_off);
  2511. if (!wasm_is_reftype_i31ref(heap_type)) {
  2512. SET_FRAME_REF(opnd_off_br);
  2513. }
  2514. goto recover_br_info;
  2515. }
  2516. }
  2517. SKIP_BR_INFO();
  2518. (void)heap_type_dst;
  2519. HANDLE_OP_END();
  2520. }
  2521. case WASM_OP_ANY_CONVERT_EXTERN:
  2522. {
  2523. externref_obj = POP_REF();
  2524. if (externref_obj == NULL_REF)
  2525. PUSH_REF(NULL_REF);
  2526. else {
  2527. gc_obj = wasm_externref_obj_to_internal_obj(
  2528. externref_obj);
  2529. PUSH_REF(gc_obj);
  2530. }
  2531. HANDLE_OP_END();
  2532. }
  2533. case WASM_OP_EXTERN_CONVERT_ANY:
  2534. {
  2535. gc_obj = POP_REF();
  2536. if (gc_obj == NULL_REF)
  2537. PUSH_REF(NULL_REF);
  2538. else {
  2539. if (!(externref_obj =
  2540. wasm_internal_obj_to_externref_obj(
  2541. exec_env, gc_obj))) {
  2542. wasm_set_exception(
  2543. module, "create externref object failed");
  2544. goto got_exception;
  2545. }
  2546. PUSH_REF(externref_obj);
  2547. }
  2548. HANDLE_OP_END();
  2549. }
  2550. #if WASM_ENABLE_STRINGREF != 0
  2551. case WASM_OP_STRING_NEW_UTF8:
  2552. case WASM_OP_STRING_NEW_WTF16:
  2553. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  2554. case WASM_OP_STRING_NEW_WTF8:
  2555. {
  2556. uint32 mem_idx, addr, bytes_length, offset = 0;
  2557. EncodingFlag flag = WTF8;
  2558. mem_idx = (uint32)read_uint32(frame_ip);
  2559. bytes_length = POP_I32();
  2560. addr = POP_I32();
  2561. CHECK_MEMORY_OVERFLOW(bytes_length);
  2562. if (opcode == WASM_OP_STRING_NEW_WTF16) {
  2563. flag = WTF16;
  2564. }
  2565. else if (opcode == WASM_OP_STRING_NEW_UTF8) {
  2566. flag = UTF8;
  2567. }
  2568. else if (opcode == WASM_OP_STRING_NEW_LOSSY_UTF8) {
  2569. flag = LOSSY_UTF8;
  2570. }
  2571. else if (opcode == WASM_OP_STRING_NEW_WTF8) {
  2572. flag = WTF8;
  2573. }
  2574. str_obj = wasm_string_new_with_encoding(
  2575. maddr, bytes_length, flag);
  2576. if (!str_obj) {
  2577. wasm_set_exception(module,
  2578. "create string object failed");
  2579. goto got_exception;
  2580. }
  2581. SYNC_ALL_TO_FRAME();
  2582. stringref_obj =
  2583. wasm_stringref_obj_new(exec_env, str_obj);
  2584. if (!stringref_obj) {
  2585. wasm_set_exception(module,
  2586. "create stringref failed");
  2587. goto got_exception;
  2588. }
  2589. PUSH_REF(stringref_obj);
  2590. (void)mem_idx;
  2591. HANDLE_OP_END();
  2592. }
  2593. case WASM_OP_STRING_CONST:
  2594. {
  2595. WASMModule *wasm_module = module->module;
  2596. uint32 contents;
  2597. contents = (uint32)read_uint32(frame_ip);
  2598. str_obj = wasm_string_new_const(
  2599. (const char *)
  2600. wasm_module->string_literal_ptrs[contents],
  2601. wasm_module->string_literal_lengths[contents]);
  2602. if (!str_obj) {
  2603. wasm_set_exception(module,
  2604. "create string object failed");
  2605. goto got_exception;
  2606. }
  2607. SYNC_ALL_TO_FRAME();
  2608. stringref_obj =
  2609. wasm_stringref_obj_new(exec_env, str_obj);
  2610. if (!str_obj) {
  2611. wasm_set_exception(module,
  2612. "create stringref failed");
  2613. goto got_exception;
  2614. }
  2615. PUSH_REF(stringref_obj);
  2616. HANDLE_OP_END();
  2617. }
  2618. case WASM_OP_STRING_MEASURE_UTF8:
  2619. case WASM_OP_STRING_MEASURE_WTF8:
  2620. case WASM_OP_STRING_MEASURE_WTF16:
  2621. {
  2622. int32 target_bytes_length;
  2623. EncodingFlag flag = WTF8;
  2624. stringref_obj = POP_REF();
  2625. if (opcode == WASM_OP_STRING_MEASURE_WTF16) {
  2626. flag = WTF16;
  2627. }
  2628. else if (opcode == WASM_OP_STRING_MEASURE_UTF8) {
  2629. flag = UTF8;
  2630. }
  2631. else if (opcode == WASM_OP_STRING_MEASURE_WTF8) {
  2632. flag = LOSSY_UTF8;
  2633. }
  2634. target_bytes_length = wasm_string_measure(
  2635. (WASMString)wasm_stringref_obj_get_value(
  2636. stringref_obj),
  2637. flag);
  2638. PUSH_I32(target_bytes_length);
  2639. HANDLE_OP_END();
  2640. }
  2641. case WASM_OP_STRING_ENCODE_UTF8:
  2642. case WASM_OP_STRING_ENCODE_WTF16:
  2643. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  2644. case WASM_OP_STRING_ENCODE_WTF8:
  2645. {
  2646. uint32 mem_idx, addr;
  2647. int32 target_bytes_length;
  2648. WASMMemoryInstance *memory_inst;
  2649. EncodingFlag flag = WTF8;
  2650. mem_idx = (uint32)read_uint32(frame_ip);
  2651. addr = POP_I32();
  2652. stringref_obj = POP_REF();
  2653. str_obj = (WASMString)wasm_stringref_obj_get_value(
  2654. stringref_obj);
  2655. #if WASM_ENABLE_SHARED_HEAP != 0
  2656. if (app_addr_in_shared_heap((uint64)addr, 1))
  2657. shared_heap_addr_app_to_native((uint64)addr, maddr);
  2658. else
  2659. #endif
  2660. {
  2661. memory_inst = module->memories[mem_idx];
  2662. maddr = memory_inst->memory_data + addr;
  2663. }
  2664. if (opcode == WASM_OP_STRING_ENCODE_WTF16) {
  2665. flag = WTF16;
  2666. count = wasm_string_measure(str_obj, flag);
  2667. target_bytes_length = wasm_string_encode(
  2668. str_obj, 0, count, maddr, NULL, flag);
  2669. }
  2670. else {
  2671. if (opcode == WASM_OP_STRING_ENCODE_UTF8) {
  2672. flag = UTF8;
  2673. }
  2674. else if (opcode
  2675. == WASM_OP_STRING_ENCODE_LOSSY_UTF8) {
  2676. flag = LOSSY_UTF8;
  2677. }
  2678. else if (opcode == WASM_OP_STRING_ENCODE_WTF8) {
  2679. flag = WTF8;
  2680. }
  2681. count = wasm_string_measure(str_obj, flag);
  2682. target_bytes_length = wasm_string_encode(
  2683. str_obj, 0, count, maddr, NULL, flag);
  2684. if (target_bytes_length == -1) {
  2685. wasm_set_exception(
  2686. module, "isolated surrogate is seen");
  2687. goto got_exception;
  2688. }
  2689. }
  2690. if (target_bytes_length < 0) {
  2691. wasm_set_exception(module,
  2692. "stringref encode failed");
  2693. goto got_exception;
  2694. }
  2695. PUSH_I32(target_bytes_length);
  2696. HANDLE_OP_END();
  2697. }
  2698. case WASM_OP_STRING_CONCAT:
  2699. {
  2700. WASMStringrefObjectRef stringref_obj1, stringref_obj2;
  2701. stringref_obj2 = POP_REF();
  2702. stringref_obj1 = POP_REF();
  2703. str_obj = wasm_string_concat(
  2704. (WASMString)wasm_stringref_obj_get_value(
  2705. stringref_obj1),
  2706. (WASMString)wasm_stringref_obj_get_value(
  2707. stringref_obj2));
  2708. if (!str_obj) {
  2709. wasm_set_exception(module,
  2710. "create string object failed");
  2711. goto got_exception;
  2712. }
  2713. SYNC_ALL_TO_FRAME();
  2714. stringref_obj =
  2715. wasm_stringref_obj_new(exec_env, str_obj);
  2716. if (!stringref_obj) {
  2717. wasm_set_exception(module,
  2718. "create stringref failed");
  2719. goto got_exception;
  2720. }
  2721. PUSH_REF(stringref_obj);
  2722. HANDLE_OP_END();
  2723. }
  2724. case WASM_OP_STRING_EQ:
  2725. {
  2726. WASMStringrefObjectRef stringref_obj1, stringref_obj2;
  2727. int32 is_eq;
  2728. stringref_obj2 = POP_REF();
  2729. stringref_obj1 = POP_REF();
  2730. is_eq = wasm_string_eq(
  2731. (WASMString)wasm_stringref_obj_get_value(
  2732. stringref_obj1),
  2733. (WASMString)wasm_stringref_obj_get_value(
  2734. stringref_obj2));
  2735. PUSH_I32(is_eq);
  2736. HANDLE_OP_END();
  2737. }
  2738. case WASM_OP_STRING_IS_USV_SEQUENCE:
  2739. {
  2740. int32 is_usv_sequence;
  2741. stringref_obj = POP_REF();
  2742. is_usv_sequence = wasm_string_is_usv_sequence(
  2743. (WASMString)wasm_stringref_obj_get_value(
  2744. stringref_obj));
  2745. PUSH_I32(is_usv_sequence);
  2746. HANDLE_OP_END();
  2747. }
  2748. case WASM_OP_STRING_AS_WTF8:
  2749. {
  2750. stringref_obj = POP_REF();
  2751. str_obj = wasm_string_create_view(
  2752. (WASMString)wasm_stringref_obj_get_value(
  2753. stringref_obj),
  2754. STRING_VIEW_WTF8);
  2755. if (!str_obj) {
  2756. wasm_set_exception(module,
  2757. "create string object failed");
  2758. goto got_exception;
  2759. }
  2760. SYNC_ALL_TO_FRAME();
  2761. stringview_wtf8_obj =
  2762. wasm_stringview_wtf8_obj_new(exec_env, str_obj);
  2763. if (!stringview_wtf8_obj) {
  2764. wasm_set_exception(module,
  2765. "create stringview wtf8 failed");
  2766. goto got_exception;
  2767. }
  2768. PUSH_REF(stringview_wtf8_obj);
  2769. HANDLE_OP_END();
  2770. }
  2771. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  2772. {
  2773. uint32 next_pos, bytes, pos;
  2774. bytes = POP_I32();
  2775. pos = POP_I32();
  2776. stringview_wtf8_obj = POP_REF();
  2777. next_pos = wasm_string_advance(
  2778. (WASMString)wasm_stringview_wtf8_obj_get_value(
  2779. stringview_wtf8_obj),
  2780. pos, bytes, NULL);
  2781. PUSH_I32(next_pos);
  2782. HANDLE_OP_END();
  2783. }
  2784. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  2785. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  2786. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  2787. {
  2788. uint32 mem_idx, addr, pos, bytes, next_pos;
  2789. int32 bytes_written;
  2790. WASMMemoryInstance *memory_inst;
  2791. EncodingFlag flag = WTF8;
  2792. if (opcode == WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8) {
  2793. flag = UTF8;
  2794. }
  2795. else if (opcode
  2796. == WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8) {
  2797. flag = LOSSY_UTF8;
  2798. }
  2799. else if (opcode
  2800. == WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8) {
  2801. flag = WTF8;
  2802. }
  2803. mem_idx = (uint32)read_uint32(frame_ip);
  2804. bytes = POP_I32();
  2805. pos = POP_I32();
  2806. addr = POP_I32();
  2807. stringview_wtf8_obj = POP_REF();
  2808. #if WASM_ENABLE_SHARED_HEAP != 0
  2809. if (app_addr_in_shared_heap((uint64)addr, 1))
  2810. shared_heap_addr_app_to_native((uint64)addr, maddr);
  2811. else
  2812. #endif
  2813. {
  2814. memory_inst = module->memories[mem_idx];
  2815. maddr = memory_inst->memory_data + addr;
  2816. }
  2817. bytes_written = wasm_string_encode(
  2818. (WASMString)wasm_stringview_wtf8_obj_get_value(
  2819. stringview_wtf8_obj),
  2820. pos, bytes, maddr, &next_pos, flag);
  2821. if (bytes_written < 0) {
  2822. if (bytes_written == Isolated_Surrogate) {
  2823. wasm_set_exception(
  2824. module, "isolated surrogate is seen");
  2825. }
  2826. else {
  2827. wasm_set_exception(module, "encode failed");
  2828. }
  2829. goto got_exception;
  2830. }
  2831. PUSH_I32(next_pos);
  2832. PUSH_I32(bytes_written);
  2833. HANDLE_OP_END();
  2834. }
  2835. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  2836. {
  2837. uint32 start, end;
  2838. end = POP_I32();
  2839. start = POP_I32();
  2840. stringview_wtf8_obj = POP_REF();
  2841. str_obj = wasm_string_slice(
  2842. (WASMString)wasm_stringview_wtf8_obj_get_value(
  2843. stringview_wtf8_obj),
  2844. start, end, STRING_VIEW_WTF8);
  2845. if (!str_obj) {
  2846. wasm_set_exception(module,
  2847. "create string object failed");
  2848. goto got_exception;
  2849. }
  2850. SYNC_ALL_TO_FRAME();
  2851. stringref_obj =
  2852. wasm_stringref_obj_new(exec_env, str_obj);
  2853. if (!stringref_obj) {
  2854. wasm_set_exception(module,
  2855. "create stringref failed");
  2856. goto got_exception;
  2857. }
  2858. PUSH_REF(stringref_obj);
  2859. HANDLE_OP_END();
  2860. }
  2861. case WASM_OP_STRING_AS_WTF16:
  2862. {
  2863. stringref_obj = POP_REF();
  2864. str_obj = wasm_string_create_view(
  2865. (WASMString)wasm_stringref_obj_get_value(
  2866. stringref_obj),
  2867. STRING_VIEW_WTF16);
  2868. if (!str_obj) {
  2869. wasm_set_exception(module,
  2870. "create string object failed");
  2871. goto got_exception;
  2872. }
  2873. SYNC_ALL_TO_FRAME();
  2874. stringview_wtf16_obj =
  2875. wasm_stringview_wtf16_obj_new(exec_env, str_obj);
  2876. if (!stringview_wtf16_obj) {
  2877. wasm_set_exception(
  2878. module, "create stringview wtf16 failed");
  2879. goto got_exception;
  2880. }
  2881. PUSH_REF(stringview_wtf16_obj);
  2882. HANDLE_OP_END();
  2883. }
  2884. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  2885. {
  2886. int32 code_units_length;
  2887. stringview_wtf16_obj = POP_REF();
  2888. code_units_length = wasm_string_wtf16_get_length(
  2889. (WASMString)wasm_stringview_wtf16_obj_get_value(
  2890. stringview_wtf16_obj));
  2891. PUSH_I32(code_units_length);
  2892. HANDLE_OP_END();
  2893. }
  2894. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  2895. {
  2896. int32 pos;
  2897. uint32 code_unit;
  2898. pos = POP_I32();
  2899. stringview_wtf16_obj = POP_REF();
  2900. code_unit = (uint32)wasm_string_get_wtf16_codeunit(
  2901. (WASMString)wasm_stringview_wtf16_obj_get_value(
  2902. stringview_wtf16_obj),
  2903. pos);
  2904. PUSH_I32(code_unit);
  2905. HANDLE_OP_END();
  2906. }
  2907. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  2908. {
  2909. uint32 mem_idx, addr, pos, len, offset = 0;
  2910. int32 written_code_units = 0;
  2911. mem_idx = (uint32)read_uint32(frame_ip);
  2912. len = POP_I32();
  2913. pos = POP_I32();
  2914. addr = POP_I32();
  2915. stringview_wtf16_obj = POP_REF();
  2916. CHECK_MEMORY_OVERFLOW(len * sizeof(uint16));
  2917. /* check 2-byte alignment */
  2918. if (((uintptr_t)maddr & (((uintptr_t)1 << 2) - 1))
  2919. != 0) {
  2920. wasm_set_exception(module,
  2921. "unaligned memory access");
  2922. goto got_exception;
  2923. }
  2924. written_code_units = wasm_string_encode(
  2925. (WASMString)wasm_stringview_wtf16_obj_get_value(
  2926. stringview_wtf16_obj),
  2927. pos, len, maddr, NULL, WTF16);
  2928. PUSH_I32(written_code_units);
  2929. (void)mem_idx;
  2930. HANDLE_OP_END();
  2931. }
  2932. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  2933. {
  2934. uint32 start, end;
  2935. end = POP_I32();
  2936. start = POP_I32();
  2937. stringview_wtf16_obj = POP_REF();
  2938. str_obj = wasm_string_slice(
  2939. (WASMString)wasm_stringview_wtf16_obj_get_value(
  2940. stringview_wtf16_obj),
  2941. start, end, STRING_VIEW_WTF16);
  2942. if (!str_obj) {
  2943. wasm_set_exception(module,
  2944. "create string object failed");
  2945. goto got_exception;
  2946. }
  2947. SYNC_ALL_TO_FRAME();
  2948. stringref_obj =
  2949. wasm_stringref_obj_new(exec_env, str_obj);
  2950. if (!stringref_obj) {
  2951. wasm_set_exception(module,
  2952. "create stringref failed");
  2953. goto got_exception;
  2954. }
  2955. PUSH_REF(stringref_obj);
  2956. HANDLE_OP_END();
  2957. }
  2958. case WASM_OP_STRING_AS_ITER:
  2959. {
  2960. stringref_obj = POP_REF();
  2961. str_obj = wasm_string_create_view(
  2962. (WASMString)wasm_stringref_obj_get_value(
  2963. stringref_obj),
  2964. STRING_VIEW_ITER);
  2965. if (!str_obj) {
  2966. wasm_set_exception(module,
  2967. "create string object failed");
  2968. goto got_exception;
  2969. }
  2970. SYNC_ALL_TO_FRAME();
  2971. stringview_iter_obj =
  2972. wasm_stringview_iter_obj_new(exec_env, str_obj, 0);
  2973. if (!stringview_iter_obj) {
  2974. wasm_set_exception(module,
  2975. "create stringview iter failed");
  2976. goto got_exception;
  2977. }
  2978. PUSH_REF(stringview_iter_obj);
  2979. HANDLE_OP_END();
  2980. }
  2981. case WASM_OP_STRINGVIEW_ITER_NEXT:
  2982. {
  2983. uint32 code_point;
  2984. stringview_iter_obj = POP_REF();
  2985. code_point = wasm_string_next_codepoint(
  2986. (WASMString)wasm_stringview_iter_obj_get_value(
  2987. stringview_iter_obj),
  2988. wasm_stringview_iter_obj_get_pos(
  2989. stringview_iter_obj));
  2990. PUSH_I32(code_point);
  2991. HANDLE_OP_END();
  2992. }
  2993. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  2994. case WASM_OP_STRINGVIEW_ITER_REWIND:
  2995. {
  2996. uint32 code_points_count, code_points_consumed = 0,
  2997. cur_pos, next_pos = 0;
  2998. code_points_count = POP_I32();
  2999. stringview_iter_obj = POP_REF();
  3000. str_obj =
  3001. (WASMString)wasm_stringview_iter_obj_get_value(
  3002. stringview_iter_obj);
  3003. cur_pos = wasm_stringview_iter_obj_get_pos(
  3004. stringview_iter_obj);
  3005. if (opcode == WASM_OP_STRINGVIEW_ITER_ADVANCE) {
  3006. next_pos = wasm_string_advance(
  3007. str_obj, cur_pos, code_points_count,
  3008. &code_points_consumed);
  3009. }
  3010. else if (opcode == WASM_OP_STRINGVIEW_ITER_REWIND) {
  3011. next_pos = wasm_string_rewind(
  3012. str_obj, cur_pos, code_points_count,
  3013. &code_points_consumed);
  3014. }
  3015. wasm_stringview_iter_obj_update_pos(stringview_iter_obj,
  3016. next_pos);
  3017. PUSH_I32(code_points_consumed);
  3018. HANDLE_OP_END();
  3019. }
  3020. case WASM_OP_STRINGVIEW_ITER_SLICE:
  3021. {
  3022. uint32 code_points_count, cur_pos;
  3023. code_points_count = POP_I32();
  3024. stringview_iter_obj = POP_REF();
  3025. cur_pos = wasm_stringview_iter_obj_get_pos(
  3026. stringview_iter_obj);
  3027. str_obj = wasm_string_slice(
  3028. (WASMString)wasm_stringview_iter_obj_get_value(
  3029. stringview_iter_obj),
  3030. cur_pos, cur_pos + code_points_count,
  3031. STRING_VIEW_ITER);
  3032. if (!str_obj) {
  3033. wasm_set_exception(module,
  3034. "create string object failed");
  3035. goto got_exception;
  3036. }
  3037. SYNC_ALL_TO_FRAME();
  3038. stringref_obj =
  3039. wasm_stringref_obj_new(exec_env, str_obj);
  3040. if (!stringref_obj) {
  3041. wasm_set_exception(module,
  3042. "create stringref failed");
  3043. goto got_exception;
  3044. }
  3045. PUSH_REF(stringref_obj);
  3046. HANDLE_OP_END();
  3047. }
  3048. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  3049. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  3050. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  3051. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  3052. {
  3053. uint32 start, end, array_len;
  3054. EncodingFlag flag = WTF8;
  3055. WASMArrayType *array_type;
  3056. void *arr_start_addr;
  3057. end = POP_I32();
  3058. start = POP_I32();
  3059. array_obj = POP_REF();
  3060. array_type = (WASMArrayType *)wasm_obj_get_defined_type(
  3061. (WASMObjectRef)array_obj);
  3062. arr_start_addr =
  3063. wasm_array_obj_elem_addr(array_obj, start);
  3064. array_len = wasm_array_obj_length(array_obj);
  3065. if (start > end || end > array_len) {
  3066. wasm_set_exception(module,
  3067. "out of bounds array access");
  3068. goto got_exception;
  3069. }
  3070. if (opcode == WASM_OP_STRING_NEW_WTF16_ARRAY) {
  3071. if (array_type->elem_type != VALUE_TYPE_I16) {
  3072. wasm_set_exception(module,
  3073. "array type mismatch");
  3074. goto got_exception;
  3075. }
  3076. flag = WTF16;
  3077. }
  3078. else {
  3079. if (array_type->elem_type != VALUE_TYPE_I8) {
  3080. wasm_set_exception(module,
  3081. "array type mismatch");
  3082. goto got_exception;
  3083. }
  3084. if (opcode == WASM_OP_STRING_NEW_UTF8_ARRAY) {
  3085. flag = UTF8;
  3086. }
  3087. else if (opcode == WASM_OP_STRING_NEW_WTF8_ARRAY) {
  3088. flag = WTF8;
  3089. }
  3090. else if (opcode
  3091. == WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY) {
  3092. flag = LOSSY_UTF8;
  3093. }
  3094. }
  3095. str_obj = wasm_string_new_with_encoding(
  3096. arr_start_addr, (end - start), flag);
  3097. if (!str_obj) {
  3098. wasm_set_exception(module,
  3099. "create string object failed");
  3100. goto got_exception;
  3101. }
  3102. SYNC_ALL_TO_FRAME();
  3103. stringref_obj =
  3104. wasm_stringref_obj_new(exec_env, str_obj);
  3105. if (!stringref_obj) {
  3106. wasm_set_exception(module,
  3107. "create stringref failed");
  3108. goto got_exception;
  3109. }
  3110. PUSH_REF(stringref_obj);
  3111. HANDLE_OP_END();
  3112. }
  3113. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  3114. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  3115. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  3116. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  3117. {
  3118. uint32 start, array_len, count;
  3119. int32 bytes_written;
  3120. EncodingFlag flag = WTF8;
  3121. WASMArrayType *array_type;
  3122. void *arr_start_addr;
  3123. start = POP_I32();
  3124. array_obj = POP_REF();
  3125. stringref_obj = POP_REF();
  3126. str_obj = (WASMString)wasm_stringref_obj_get_value(
  3127. stringref_obj);
  3128. array_type = (WASMArrayType *)wasm_obj_get_defined_type(
  3129. (WASMObjectRef)array_obj);
  3130. arr_start_addr =
  3131. wasm_array_obj_elem_addr(array_obj, start);
  3132. array_len = wasm_array_obj_length(array_obj);
  3133. if (start > array_len) {
  3134. wasm_set_exception(module,
  3135. "out of bounds array access");
  3136. goto got_exception;
  3137. }
  3138. if (opcode == WASM_OP_STRING_ENCODE_WTF16_ARRAY) {
  3139. if (array_type->elem_type != VALUE_TYPE_I16) {
  3140. wasm_set_exception(module,
  3141. "array type mismatch");
  3142. goto got_exception;
  3143. }
  3144. flag = WTF16;
  3145. }
  3146. else {
  3147. if (array_type->elem_type != VALUE_TYPE_I8) {
  3148. wasm_set_exception(module,
  3149. "array type mismatch");
  3150. goto got_exception;
  3151. }
  3152. if (opcode == WASM_OP_STRING_ENCODE_UTF8_ARRAY) {
  3153. flag = UTF8;
  3154. }
  3155. else if (opcode
  3156. == WASM_OP_STRING_ENCODE_WTF8_ARRAY) {
  3157. flag = WTF8;
  3158. }
  3159. else if (
  3160. opcode
  3161. == WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY) {
  3162. flag = LOSSY_UTF8;
  3163. }
  3164. }
  3165. count = wasm_string_measure(str_obj, flag);
  3166. bytes_written = wasm_string_encode(
  3167. str_obj, 0, count, arr_start_addr, NULL, flag);
  3168. if (bytes_written < 0) {
  3169. if (bytes_written == Isolated_Surrogate) {
  3170. wasm_set_exception(
  3171. module, "isolated surrogate is seen");
  3172. }
  3173. else if (bytes_written == Insufficient_Space) {
  3174. wasm_set_exception(
  3175. module, "array space is insufficient");
  3176. }
  3177. else {
  3178. wasm_set_exception(module, "encode failed");
  3179. }
  3180. goto got_exception;
  3181. }
  3182. PUSH_I32(bytes_written);
  3183. HANDLE_OP_END();
  3184. }
  3185. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  3186. default:
  3187. {
  3188. wasm_set_exception(module, "unsupported opcode");
  3189. goto got_exception;
  3190. }
  3191. }
  3192. }
  3193. #endif /* end of WASM_ENABLE_GC != 0 */
  3194. /* variable instructions */
  3195. HANDLE_OP(EXT_OP_SET_LOCAL_FAST)
  3196. HANDLE_OP(EXT_OP_TEE_LOCAL_FAST)
  3197. {
  3198. /* clang-format off */
  3199. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  3200. local_offset = *frame_ip++;
  3201. #else
  3202. local_offset = *frame_ip;
  3203. frame_ip += 2;
  3204. #endif
  3205. /* clang-format on */
  3206. *(uint32 *)(frame_lp + local_offset) =
  3207. GET_OPERAND(uint32, I32, 0);
  3208. frame_ip += 2;
  3209. HANDLE_OP_END();
  3210. }
  3211. HANDLE_OP(EXT_OP_SET_LOCAL_FAST_I64)
  3212. HANDLE_OP(EXT_OP_TEE_LOCAL_FAST_I64)
  3213. {
  3214. /* clang-format off */
  3215. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  3216. local_offset = *frame_ip++;
  3217. #else
  3218. local_offset = *frame_ip;
  3219. frame_ip += 2;
  3220. #endif
  3221. /* clang-format on */
  3222. PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),
  3223. GET_OPERAND(uint64, I64, 0));
  3224. frame_ip += 2;
  3225. HANDLE_OP_END();
  3226. }
  3227. #if WASM_ENABLE_SIMDE != 0
  3228. HANDLE_OP(EXT_OP_SET_LOCAL_FAST_V128)
  3229. HANDLE_OP(EXT_OP_TEE_LOCAL_FAST_V128)
  3230. {
  3231. /* clang-format off */
  3232. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  3233. local_offset = *frame_ip++;
  3234. #else
  3235. local_offset = *frame_ip;
  3236. frame_ip += 2;
  3237. #endif
  3238. /* clang-format on */
  3239. PUT_V128_TO_ADDR((uint32 *)(frame_lp + local_offset),
  3240. GET_OPERAND_V128(0));
  3241. frame_ip += 2;
  3242. HANDLE_OP_END();
  3243. }
  3244. #endif
  3245. HANDLE_OP(WASM_OP_GET_GLOBAL)
  3246. {
  3247. global_idx = read_uint32(frame_ip);
  3248. bh_assert(global_idx < module->e->global_count);
  3249. global = globals + global_idx;
  3250. global_addr = get_global_addr(global_data, global);
  3251. addr_ret = GET_OFFSET();
  3252. /* clang-format off */
  3253. #if WASM_ENABLE_GC == 0
  3254. frame_lp[addr_ret] = *(uint32 *)global_addr;
  3255. #else
  3256. if (!wasm_is_type_reftype(global->type))
  3257. frame_lp[addr_ret] = *(uint32 *)global_addr;
  3258. else {
  3259. PUT_REF_TO_ADDR(frame_lp + addr_ret,
  3260. GET_REF_FROM_ADDR((uint32 *)global_addr));
  3261. if (!wasm_is_reftype_i31ref(global->type)) {
  3262. SET_FRAME_REF(addr_ret);
  3263. }
  3264. }
  3265. #endif
  3266. /* clang-format on */
  3267. HANDLE_OP_END();
  3268. }
  3269. HANDLE_OP(WASM_OP_GET_GLOBAL_64)
  3270. {
  3271. global_idx = read_uint32(frame_ip);
  3272. bh_assert(global_idx < module->e->global_count);
  3273. global = globals + global_idx;
  3274. global_addr = get_global_addr(global_data, global);
  3275. addr_ret = GET_OFFSET();
  3276. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  3277. GET_I64_FROM_ADDR((uint32 *)global_addr));
  3278. HANDLE_OP_END();
  3279. }
  3280. #if WASM_ENABLE_SIMDE != 0
  3281. HANDLE_OP(WASM_OP_GET_GLOBAL_V128)
  3282. {
  3283. global_idx = read_uint32(frame_ip);
  3284. bh_assert(global_idx < module->e->global_count);
  3285. global = globals + global_idx;
  3286. global_addr = get_global_addr(global_data, global);
  3287. addr_ret = GET_OFFSET();
  3288. PUT_V128_TO_ADDR(frame_lp + addr_ret,
  3289. GET_V128_FROM_ADDR((uint32 *)global_addr));
  3290. HANDLE_OP_END();
  3291. }
  3292. #endif
  3293. HANDLE_OP(WASM_OP_SET_GLOBAL)
  3294. {
  3295. global_idx = read_uint32(frame_ip);
  3296. bh_assert(global_idx < module->e->global_count);
  3297. global = globals + global_idx;
  3298. global_addr = get_global_addr(global_data, global);
  3299. addr1 = GET_OFFSET();
  3300. /* clang-format off */
  3301. #if WASM_ENABLE_GC == 0
  3302. *(int32 *)global_addr = frame_lp[addr1];
  3303. #else
  3304. if (!wasm_is_type_reftype(global->type))
  3305. *(int32 *)global_addr = frame_lp[addr1];
  3306. else {
  3307. PUT_REF_TO_ADDR((uint32 *)global_addr,
  3308. GET_REF_FROM_ADDR(frame_lp + addr1));
  3309. CLEAR_FRAME_REF(addr1);
  3310. }
  3311. #endif
  3312. /* clang-format on */
  3313. HANDLE_OP_END();
  3314. }
  3315. HANDLE_OP(WASM_OP_SET_GLOBAL_AUX_STACK)
  3316. {
  3317. uint64 aux_stack_top;
  3318. global_idx = read_uint32(frame_ip);
  3319. bh_assert(global_idx < module->e->global_count);
  3320. global = globals + global_idx;
  3321. global_addr = get_global_addr(global_data, global);
  3322. /* TODO: Memory64 the data type depends on mem idx type */
  3323. aux_stack_top = (uint64)frame_lp[GET_OFFSET()];
  3324. if (aux_stack_top <= (uint64)exec_env->aux_stack_boundary) {
  3325. wasm_set_exception(module, "wasm auxiliary stack overflow");
  3326. goto got_exception;
  3327. }
  3328. if (aux_stack_top > (uint64)exec_env->aux_stack_bottom) {
  3329. wasm_set_exception(module,
  3330. "wasm auxiliary stack underflow");
  3331. goto got_exception;
  3332. }
  3333. *(int32 *)global_addr = (uint32)aux_stack_top;
  3334. #if WASM_ENABLE_MEMORY_PROFILING != 0
  3335. if (module->module->aux_stack_top_global_index != (uint32)-1) {
  3336. uint32 aux_stack_used =
  3337. (uint32)(module->module->aux_stack_bottom
  3338. - *(uint32 *)global_addr);
  3339. if (aux_stack_used > module->e->max_aux_stack_used)
  3340. module->e->max_aux_stack_used = aux_stack_used;
  3341. }
  3342. #endif
  3343. HANDLE_OP_END();
  3344. }
  3345. HANDLE_OP(WASM_OP_SET_GLOBAL_64)
  3346. {
  3347. global_idx = read_uint32(frame_ip);
  3348. bh_assert(global_idx < module->e->global_count);
  3349. global = globals + global_idx;
  3350. global_addr = get_global_addr(global_data, global);
  3351. addr1 = GET_OFFSET();
  3352. PUT_I64_TO_ADDR((uint32 *)global_addr,
  3353. GET_I64_FROM_ADDR(frame_lp + addr1));
  3354. HANDLE_OP_END();
  3355. }
  3356. #if WASM_ENABLE_SIMDE != 0
  3357. HANDLE_OP(WASM_OP_SET_GLOBAL_V128)
  3358. {
  3359. global_idx = read_uint32(frame_ip);
  3360. bh_assert(global_idx < module->e->global_count);
  3361. global = globals + global_idx;
  3362. global_addr = get_global_addr(global_data, global);
  3363. addr1 = GET_OFFSET();
  3364. PUT_V128_TO_ADDR((uint32 *)global_addr,
  3365. GET_V128_FROM_ADDR(frame_lp + addr1));
  3366. HANDLE_OP_END();
  3367. }
  3368. #endif
  3369. /* memory load instructions */
  3370. HANDLE_OP(WASM_OP_I32_LOAD)
  3371. {
  3372. uint32 offset, addr;
  3373. offset = read_uint32(frame_ip);
  3374. addr = GET_OPERAND(uint32, I32, 0);
  3375. frame_ip += 2;
  3376. addr_ret = GET_OFFSET();
  3377. CHECK_MEMORY_OVERFLOW(4);
  3378. frame_lp[addr_ret] = LOAD_I32(maddr);
  3379. HANDLE_OP_END();
  3380. }
  3381. HANDLE_OP(WASM_OP_I64_LOAD)
  3382. {
  3383. uint32 offset, addr;
  3384. offset = read_uint32(frame_ip);
  3385. addr = GET_OPERAND(uint32, I32, 0);
  3386. frame_ip += 2;
  3387. addr_ret = GET_OFFSET();
  3388. CHECK_MEMORY_OVERFLOW(8);
  3389. PUT_I64_TO_ADDR(frame_lp + addr_ret, LOAD_I64(maddr));
  3390. HANDLE_OP_END();
  3391. }
  3392. HANDLE_OP(WASM_OP_I32_LOAD8_S)
  3393. {
  3394. uint32 offset, addr;
  3395. offset = read_uint32(frame_ip);
  3396. addr = GET_OPERAND(uint32, I32, 0);
  3397. frame_ip += 2;
  3398. addr_ret = GET_OFFSET();
  3399. CHECK_MEMORY_OVERFLOW(1);
  3400. frame_lp[addr_ret] = sign_ext_8_32(*(int8 *)maddr);
  3401. HANDLE_OP_END();
  3402. }
  3403. HANDLE_OP(WASM_OP_I32_LOAD8_U)
  3404. {
  3405. uint32 offset, addr;
  3406. offset = read_uint32(frame_ip);
  3407. addr = GET_OPERAND(uint32, I32, 0);
  3408. frame_ip += 2;
  3409. addr_ret = GET_OFFSET();
  3410. CHECK_MEMORY_OVERFLOW(1);
  3411. frame_lp[addr_ret] = (uint32)(*(uint8 *)(maddr));
  3412. HANDLE_OP_END();
  3413. }
  3414. HANDLE_OP(WASM_OP_I32_LOAD16_S)
  3415. {
  3416. uint32 offset, addr;
  3417. offset = read_uint32(frame_ip);
  3418. addr = GET_OPERAND(uint32, I32, 0);
  3419. frame_ip += 2;
  3420. addr_ret = GET_OFFSET();
  3421. CHECK_MEMORY_OVERFLOW(2);
  3422. frame_lp[addr_ret] = sign_ext_16_32(LOAD_I16(maddr));
  3423. HANDLE_OP_END();
  3424. }
  3425. HANDLE_OP(WASM_OP_I32_LOAD16_U)
  3426. {
  3427. uint32 offset, addr;
  3428. offset = read_uint32(frame_ip);
  3429. addr = GET_OPERAND(uint32, I32, 0);
  3430. frame_ip += 2;
  3431. addr_ret = GET_OFFSET();
  3432. CHECK_MEMORY_OVERFLOW(2);
  3433. frame_lp[addr_ret] = (uint32)(LOAD_U16(maddr));
  3434. HANDLE_OP_END();
  3435. }
  3436. HANDLE_OP(WASM_OP_I64_LOAD8_S)
  3437. {
  3438. uint32 offset, addr;
  3439. offset = read_uint32(frame_ip);
  3440. addr = GET_OPERAND(uint32, I32, 0);
  3441. frame_ip += 2;
  3442. addr_ret = GET_OFFSET();
  3443. CHECK_MEMORY_OVERFLOW(1);
  3444. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  3445. sign_ext_8_64(*(int8 *)maddr));
  3446. HANDLE_OP_END();
  3447. }
  3448. HANDLE_OP(WASM_OP_I64_LOAD8_U)
  3449. {
  3450. uint32 offset, addr;
  3451. offset = read_uint32(frame_ip);
  3452. addr = GET_OPERAND(uint32, I32, 0);
  3453. frame_ip += 2;
  3454. addr_ret = GET_OFFSET();
  3455. CHECK_MEMORY_OVERFLOW(1);
  3456. PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(*(uint8 *)maddr));
  3457. HANDLE_OP_END();
  3458. }
  3459. HANDLE_OP(WASM_OP_I64_LOAD16_S)
  3460. {
  3461. uint32 offset, addr;
  3462. offset = read_uint32(frame_ip);
  3463. addr = GET_OPERAND(uint32, I32, 0);
  3464. frame_ip += 2;
  3465. addr_ret = GET_OFFSET();
  3466. CHECK_MEMORY_OVERFLOW(2);
  3467. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  3468. sign_ext_16_64(LOAD_I16(maddr)));
  3469. HANDLE_OP_END();
  3470. }
  3471. HANDLE_OP(WASM_OP_I64_LOAD16_U)
  3472. {
  3473. uint32 offset, addr;
  3474. offset = read_uint32(frame_ip);
  3475. addr = GET_OPERAND(uint32, I32, 0);
  3476. frame_ip += 2;
  3477. addr_ret = GET_OFFSET();
  3478. CHECK_MEMORY_OVERFLOW(2);
  3479. PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(LOAD_U16(maddr)));
  3480. HANDLE_OP_END();
  3481. }
  3482. HANDLE_OP(WASM_OP_I64_LOAD32_S)
  3483. {
  3484. uint32 offset, addr;
  3485. offset = read_uint32(frame_ip);
  3486. addr = GET_OPERAND(uint32, I32, 0);
  3487. frame_ip += 2;
  3488. addr_ret = GET_OFFSET();
  3489. CHECK_MEMORY_OVERFLOW(4);
  3490. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  3491. sign_ext_32_64(LOAD_I32(maddr)));
  3492. HANDLE_OP_END();
  3493. }
  3494. HANDLE_OP(WASM_OP_I64_LOAD32_U)
  3495. {
  3496. uint32 offset, addr;
  3497. offset = read_uint32(frame_ip);
  3498. addr = GET_OPERAND(uint32, I32, 0);
  3499. frame_ip += 2;
  3500. addr_ret = GET_OFFSET();
  3501. CHECK_MEMORY_OVERFLOW(4);
  3502. PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(LOAD_U32(maddr)));
  3503. HANDLE_OP_END();
  3504. }
  3505. HANDLE_OP(WASM_OP_I32_STORE)
  3506. {
  3507. uint32 offset, addr;
  3508. uint32 sval;
  3509. offset = read_uint32(frame_ip);
  3510. sval = GET_OPERAND(uint32, I32, 0);
  3511. addr = GET_OPERAND(uint32, I32, 2);
  3512. frame_ip += 4;
  3513. CHECK_MEMORY_OVERFLOW(4);
  3514. STORE_U32(maddr, sval);
  3515. HANDLE_OP_END();
  3516. }
  3517. HANDLE_OP(WASM_OP_I32_STORE8)
  3518. {
  3519. uint32 offset, addr;
  3520. uint32 sval;
  3521. offset = read_uint32(frame_ip);
  3522. sval = GET_OPERAND(uint32, I32, 0);
  3523. addr = GET_OPERAND(uint32, I32, 2);
  3524. frame_ip += 4;
  3525. CHECK_MEMORY_OVERFLOW(1);
  3526. STORE_U8(maddr, (uint8_t)sval);
  3527. HANDLE_OP_END();
  3528. }
  3529. HANDLE_OP(WASM_OP_I32_STORE16)
  3530. {
  3531. uint32 offset, addr;
  3532. uint32 sval;
  3533. offset = read_uint32(frame_ip);
  3534. sval = GET_OPERAND(uint32, I32, 0);
  3535. addr = GET_OPERAND(uint32, I32, 2);
  3536. frame_ip += 4;
  3537. CHECK_MEMORY_OVERFLOW(2);
  3538. STORE_U16(maddr, (uint16)sval);
  3539. HANDLE_OP_END();
  3540. }
  3541. HANDLE_OP(WASM_OP_I64_STORE)
  3542. {
  3543. uint32 offset, addr;
  3544. uint64 sval;
  3545. offset = read_uint32(frame_ip);
  3546. sval = GET_OPERAND(uint64, I64, 0);
  3547. addr = GET_OPERAND(uint32, I32, 2);
  3548. frame_ip += 4;
  3549. CHECK_MEMORY_OVERFLOW(8);
  3550. STORE_I64(maddr, sval);
  3551. HANDLE_OP_END();
  3552. }
  3553. HANDLE_OP(WASM_OP_I64_STORE8)
  3554. {
  3555. uint32 offset, addr;
  3556. uint64 sval;
  3557. offset = read_uint32(frame_ip);
  3558. sval = GET_OPERAND(uint64, I64, 0);
  3559. addr = GET_OPERAND(uint32, I32, 2);
  3560. frame_ip += 4;
  3561. CHECK_MEMORY_OVERFLOW(1);
  3562. *(uint8 *)maddr = (uint8)sval;
  3563. HANDLE_OP_END();
  3564. }
  3565. HANDLE_OP(WASM_OP_I64_STORE16)
  3566. {
  3567. uint32 offset, addr;
  3568. uint64 sval;
  3569. offset = read_uint32(frame_ip);
  3570. sval = GET_OPERAND(uint64, I64, 0);
  3571. addr = GET_OPERAND(uint32, I32, 2);
  3572. frame_ip += 4;
  3573. CHECK_MEMORY_OVERFLOW(2);
  3574. STORE_U16(maddr, (uint16)sval);
  3575. HANDLE_OP_END();
  3576. }
  3577. HANDLE_OP(WASM_OP_I64_STORE32)
  3578. {
  3579. uint32 offset, addr;
  3580. uint64 sval;
  3581. offset = read_uint32(frame_ip);
  3582. sval = GET_OPERAND(uint64, I64, 0);
  3583. addr = GET_OPERAND(uint32, I32, 2);
  3584. frame_ip += 4;
  3585. CHECK_MEMORY_OVERFLOW(4);
  3586. STORE_U32(maddr, (uint32)sval);
  3587. HANDLE_OP_END();
  3588. }
  3589. /* memory size and memory grow instructions */
  3590. HANDLE_OP(WASM_OP_MEMORY_SIZE)
  3591. {
  3592. uint32 reserved;
  3593. addr_ret = GET_OFFSET();
  3594. frame_lp[addr_ret] = memory->cur_page_count;
  3595. (void)reserved;
  3596. HANDLE_OP_END();
  3597. }
  3598. HANDLE_OP(WASM_OP_MEMORY_GROW)
  3599. {
  3600. uint32 reserved, delta,
  3601. prev_page_count = memory->cur_page_count;
  3602. addr1 = GET_OFFSET();
  3603. addr_ret = GET_OFFSET();
  3604. delta = (uint32)frame_lp[addr1];
  3605. /* TODO: multi-memory wasm_enlarge_memory_with_idx() */
  3606. if (!wasm_enlarge_memory(module, delta)) {
  3607. /* failed to memory.grow, return -1 */
  3608. frame_lp[addr_ret] = -1;
  3609. }
  3610. else {
  3611. /* success, return previous page count */
  3612. frame_lp[addr_ret] = prev_page_count;
  3613. /* update memory size, no need to update memory ptr as
  3614. it isn't changed in wasm_enlarge_memory */
  3615. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  3616. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  3617. || WASM_ENABLE_BULK_MEMORY != 0
  3618. linear_mem_size = GET_LINEAR_MEMORY_SIZE(memory);
  3619. #endif
  3620. }
  3621. (void)reserved;
  3622. HANDLE_OP_END();
  3623. }
  3624. /* constant instructions */
  3625. HANDLE_OP(WASM_OP_F64_CONST)
  3626. HANDLE_OP(WASM_OP_I64_CONST)
  3627. {
  3628. uint8 *orig_ip = frame_ip;
  3629. frame_ip += sizeof(uint64);
  3630. addr_ret = GET_OFFSET();
  3631. bh_memcpy_s(frame_lp + addr_ret, sizeof(uint64), orig_ip,
  3632. sizeof(uint64));
  3633. HANDLE_OP_END();
  3634. }
  3635. HANDLE_OP(WASM_OP_F32_CONST)
  3636. HANDLE_OP(WASM_OP_I32_CONST)
  3637. {
  3638. uint8 *orig_ip = frame_ip;
  3639. frame_ip += sizeof(uint32);
  3640. addr_ret = GET_OFFSET();
  3641. bh_memcpy_s(frame_lp + addr_ret, sizeof(uint32), orig_ip,
  3642. sizeof(uint32));
  3643. HANDLE_OP_END();
  3644. }
  3645. /* comparison instructions of i32 */
  3646. HANDLE_OP(WASM_OP_I32_EQZ)
  3647. {
  3648. DEF_OP_EQZ(int32, I32);
  3649. HANDLE_OP_END();
  3650. }
  3651. HANDLE_OP(WASM_OP_I32_EQ)
  3652. {
  3653. DEF_OP_CMP(uint32, I32, ==);
  3654. HANDLE_OP_END();
  3655. }
  3656. HANDLE_OP(WASM_OP_I32_NE)
  3657. {
  3658. DEF_OP_CMP(uint32, I32, !=);
  3659. HANDLE_OP_END();
  3660. }
  3661. HANDLE_OP(WASM_OP_I32_LT_S)
  3662. {
  3663. DEF_OP_CMP(int32, I32, <);
  3664. HANDLE_OP_END();
  3665. }
  3666. HANDLE_OP(WASM_OP_I32_LT_U)
  3667. {
  3668. DEF_OP_CMP(uint32, I32, <);
  3669. HANDLE_OP_END();
  3670. }
  3671. HANDLE_OP(WASM_OP_I32_GT_S)
  3672. {
  3673. DEF_OP_CMP(int32, I32, >);
  3674. HANDLE_OP_END();
  3675. }
  3676. HANDLE_OP(WASM_OP_I32_GT_U)
  3677. {
  3678. DEF_OP_CMP(uint32, I32, >);
  3679. HANDLE_OP_END();
  3680. }
  3681. HANDLE_OP(WASM_OP_I32_LE_S)
  3682. {
  3683. DEF_OP_CMP(int32, I32, <=);
  3684. HANDLE_OP_END();
  3685. }
  3686. HANDLE_OP(WASM_OP_I32_LE_U)
  3687. {
  3688. DEF_OP_CMP(uint32, I32, <=);
  3689. HANDLE_OP_END();
  3690. }
  3691. HANDLE_OP(WASM_OP_I32_GE_S)
  3692. {
  3693. DEF_OP_CMP(int32, I32, >=);
  3694. HANDLE_OP_END();
  3695. }
  3696. HANDLE_OP(WASM_OP_I32_GE_U)
  3697. {
  3698. DEF_OP_CMP(uint32, I32, >=);
  3699. HANDLE_OP_END();
  3700. }
  3701. /* comparison instructions of i64 */
  3702. HANDLE_OP(WASM_OP_I64_EQZ)
  3703. {
  3704. DEF_OP_EQZ(int64, I64);
  3705. HANDLE_OP_END();
  3706. }
  3707. HANDLE_OP(WASM_OP_I64_EQ)
  3708. {
  3709. DEF_OP_CMP(uint64, I64, ==);
  3710. HANDLE_OP_END();
  3711. }
  3712. HANDLE_OP(WASM_OP_I64_NE)
  3713. {
  3714. DEF_OP_CMP(uint64, I64, !=);
  3715. HANDLE_OP_END();
  3716. }
  3717. HANDLE_OP(WASM_OP_I64_LT_S)
  3718. {
  3719. DEF_OP_CMP(int64, I64, <);
  3720. HANDLE_OP_END();
  3721. }
  3722. HANDLE_OP(WASM_OP_I64_LT_U)
  3723. {
  3724. DEF_OP_CMP(uint64, I64, <);
  3725. HANDLE_OP_END();
  3726. }
  3727. HANDLE_OP(WASM_OP_I64_GT_S)
  3728. {
  3729. DEF_OP_CMP(int64, I64, >);
  3730. HANDLE_OP_END();
  3731. }
  3732. HANDLE_OP(WASM_OP_I64_GT_U)
  3733. {
  3734. DEF_OP_CMP(uint64, I64, >);
  3735. HANDLE_OP_END();
  3736. }
  3737. HANDLE_OP(WASM_OP_I64_LE_S)
  3738. {
  3739. DEF_OP_CMP(int64, I64, <=);
  3740. HANDLE_OP_END();
  3741. }
  3742. HANDLE_OP(WASM_OP_I64_LE_U)
  3743. {
  3744. DEF_OP_CMP(uint64, I64, <=);
  3745. HANDLE_OP_END();
  3746. }
  3747. HANDLE_OP(WASM_OP_I64_GE_S)
  3748. {
  3749. DEF_OP_CMP(int64, I64, >=);
  3750. HANDLE_OP_END();
  3751. }
  3752. HANDLE_OP(WASM_OP_I64_GE_U)
  3753. {
  3754. DEF_OP_CMP(uint64, I64, >=);
  3755. HANDLE_OP_END();
  3756. }
  3757. /* comparison instructions of f32 */
  3758. HANDLE_OP(WASM_OP_F32_EQ)
  3759. {
  3760. DEF_OP_CMP(float32, F32, ==);
  3761. HANDLE_OP_END();
  3762. }
  3763. HANDLE_OP(WASM_OP_F32_NE)
  3764. {
  3765. DEF_OP_CMP(float32, F32, !=);
  3766. HANDLE_OP_END();
  3767. }
  3768. HANDLE_OP(WASM_OP_F32_LT)
  3769. {
  3770. DEF_OP_CMP(float32, F32, <);
  3771. HANDLE_OP_END();
  3772. }
  3773. HANDLE_OP(WASM_OP_F32_GT)
  3774. {
  3775. DEF_OP_CMP(float32, F32, >);
  3776. HANDLE_OP_END();
  3777. }
  3778. HANDLE_OP(WASM_OP_F32_LE)
  3779. {
  3780. DEF_OP_CMP(float32, F32, <=);
  3781. HANDLE_OP_END();
  3782. }
  3783. HANDLE_OP(WASM_OP_F32_GE)
  3784. {
  3785. DEF_OP_CMP(float32, F32, >=);
  3786. HANDLE_OP_END();
  3787. }
  3788. /* comparison instructions of f64 */
  3789. HANDLE_OP(WASM_OP_F64_EQ)
  3790. {
  3791. DEF_OP_CMP(float64, F64, ==);
  3792. HANDLE_OP_END();
  3793. }
  3794. HANDLE_OP(WASM_OP_F64_NE)
  3795. {
  3796. DEF_OP_CMP(float64, F64, !=);
  3797. HANDLE_OP_END();
  3798. }
  3799. HANDLE_OP(WASM_OP_F64_LT)
  3800. {
  3801. DEF_OP_CMP(float64, F64, <);
  3802. HANDLE_OP_END();
  3803. }
  3804. HANDLE_OP(WASM_OP_F64_GT)
  3805. {
  3806. DEF_OP_CMP(float64, F64, >);
  3807. HANDLE_OP_END();
  3808. }
  3809. HANDLE_OP(WASM_OP_F64_LE)
  3810. {
  3811. DEF_OP_CMP(float64, F64, <=);
  3812. HANDLE_OP_END();
  3813. }
  3814. HANDLE_OP(WASM_OP_F64_GE)
  3815. {
  3816. DEF_OP_CMP(float64, F64, >=);
  3817. HANDLE_OP_END();
  3818. }
  3819. /* numeric instructions of i32 */
  3820. HANDLE_OP(WASM_OP_I32_CLZ)
  3821. {
  3822. DEF_OP_BIT_COUNT(uint32, I32, clz32);
  3823. HANDLE_OP_END();
  3824. }
  3825. HANDLE_OP(WASM_OP_I32_CTZ)
  3826. {
  3827. DEF_OP_BIT_COUNT(uint32, I32, ctz32);
  3828. HANDLE_OP_END();
  3829. }
  3830. HANDLE_OP(WASM_OP_I32_POPCNT)
  3831. {
  3832. DEF_OP_BIT_COUNT(uint32, I32, popcount32);
  3833. HANDLE_OP_END();
  3834. }
  3835. HANDLE_OP(WASM_OP_I32_ADD)
  3836. {
  3837. DEF_OP_NUMERIC(uint32, uint32, I32, +);
  3838. HANDLE_OP_END();
  3839. }
  3840. HANDLE_OP(WASM_OP_I32_SUB)
  3841. {
  3842. DEF_OP_NUMERIC(uint32, uint32, I32, -);
  3843. HANDLE_OP_END();
  3844. }
  3845. HANDLE_OP(WASM_OP_I32_MUL)
  3846. {
  3847. DEF_OP_NUMERIC(uint32, uint32, I32, *);
  3848. HANDLE_OP_END();
  3849. }
  3850. HANDLE_OP(WASM_OP_I32_DIV_S)
  3851. {
  3852. int32 a, b;
  3853. b = frame_lp[GET_OFFSET()];
  3854. a = frame_lp[GET_OFFSET()];
  3855. addr_ret = GET_OFFSET();
  3856. if (a == (int32)0x80000000 && b == -1) {
  3857. wasm_set_exception(module, "integer overflow");
  3858. goto got_exception;
  3859. }
  3860. if (b == 0) {
  3861. wasm_set_exception(module, "integer divide by zero");
  3862. goto got_exception;
  3863. }
  3864. frame_lp[addr_ret] = (a / b);
  3865. HANDLE_OP_END();
  3866. }
  3867. HANDLE_OP(WASM_OP_I32_DIV_U)
  3868. {
  3869. uint32 a, b;
  3870. addr1 = GET_OFFSET();
  3871. addr2 = GET_OFFSET();
  3872. addr_ret = GET_OFFSET();
  3873. b = (uint32)frame_lp[addr1];
  3874. a = (uint32)frame_lp[addr2];
  3875. if (b == 0) {
  3876. wasm_set_exception(module, "integer divide by zero");
  3877. goto got_exception;
  3878. }
  3879. frame_lp[addr_ret] = (a / b);
  3880. HANDLE_OP_END();
  3881. }
  3882. HANDLE_OP(WASM_OP_I32_REM_S)
  3883. {
  3884. int32 a, b;
  3885. addr1 = GET_OFFSET();
  3886. addr2 = GET_OFFSET();
  3887. addr_ret = GET_OFFSET();
  3888. b = frame_lp[addr1];
  3889. a = frame_lp[addr2];
  3890. if (a == (int32)0x80000000 && b == -1) {
  3891. frame_lp[addr_ret] = 0;
  3892. HANDLE_OP_END();
  3893. }
  3894. if (b == 0) {
  3895. wasm_set_exception(module, "integer divide by zero");
  3896. goto got_exception;
  3897. }
  3898. frame_lp[addr_ret] = (a % b);
  3899. HANDLE_OP_END();
  3900. }
  3901. HANDLE_OP(WASM_OP_I32_REM_U)
  3902. {
  3903. uint32 a, b;
  3904. addr1 = GET_OFFSET();
  3905. addr2 = GET_OFFSET();
  3906. addr_ret = GET_OFFSET();
  3907. b = (uint32)frame_lp[addr1];
  3908. a = (uint32)frame_lp[addr2];
  3909. if (b == 0) {
  3910. wasm_set_exception(module, "integer divide by zero");
  3911. goto got_exception;
  3912. }
  3913. frame_lp[addr_ret] = (a % b);
  3914. HANDLE_OP_END();
  3915. }
  3916. HANDLE_OP(WASM_OP_I32_AND)
  3917. {
  3918. DEF_OP_NUMERIC(uint32, uint32, I32, &);
  3919. HANDLE_OP_END();
  3920. }
  3921. HANDLE_OP(WASM_OP_I32_OR)
  3922. {
  3923. DEF_OP_NUMERIC(uint32, uint32, I32, |);
  3924. HANDLE_OP_END();
  3925. }
  3926. HANDLE_OP(WASM_OP_I32_XOR)
  3927. {
  3928. DEF_OP_NUMERIC(uint32, uint32, I32, ^);
  3929. HANDLE_OP_END();
  3930. }
  3931. HANDLE_OP(WASM_OP_I32_SHL)
  3932. {
  3933. DEF_OP_NUMERIC2(uint32, uint32, I32, <<);
  3934. HANDLE_OP_END();
  3935. }
  3936. HANDLE_OP(WASM_OP_I32_SHR_S)
  3937. {
  3938. DEF_OP_NUMERIC2(int32, uint32, I32, >>);
  3939. HANDLE_OP_END();
  3940. }
  3941. HANDLE_OP(WASM_OP_I32_SHR_U)
  3942. {
  3943. DEF_OP_NUMERIC2(uint32, uint32, I32, >>);
  3944. HANDLE_OP_END();
  3945. }
  3946. HANDLE_OP(WASM_OP_I32_ROTL)
  3947. {
  3948. uint32 a, b;
  3949. b = (uint32)frame_lp[GET_OFFSET()];
  3950. a = (uint32)frame_lp[GET_OFFSET()];
  3951. frame_lp[GET_OFFSET()] = rotl32(a, b);
  3952. HANDLE_OP_END();
  3953. }
  3954. HANDLE_OP(WASM_OP_I32_ROTR)
  3955. {
  3956. uint32 a, b;
  3957. b = (uint32)frame_lp[GET_OFFSET()];
  3958. a = (uint32)frame_lp[GET_OFFSET()];
  3959. frame_lp[GET_OFFSET()] = rotr32(a, b);
  3960. HANDLE_OP_END();
  3961. }
  3962. /* numeric instructions of i64 */
  3963. HANDLE_OP(WASM_OP_I64_CLZ)
  3964. {
  3965. DEF_OP_BIT_COUNT(uint64, I64, clz64);
  3966. HANDLE_OP_END();
  3967. }
  3968. HANDLE_OP(WASM_OP_I64_CTZ)
  3969. {
  3970. DEF_OP_BIT_COUNT(uint64, I64, ctz64);
  3971. HANDLE_OP_END();
  3972. }
  3973. HANDLE_OP(WASM_OP_I64_POPCNT)
  3974. {
  3975. DEF_OP_BIT_COUNT(uint64, I64, popcount64);
  3976. HANDLE_OP_END();
  3977. }
  3978. HANDLE_OP(WASM_OP_I64_ADD)
  3979. {
  3980. DEF_OP_NUMERIC_64(uint64, uint64, I64, +);
  3981. HANDLE_OP_END();
  3982. }
  3983. HANDLE_OP(WASM_OP_I64_SUB)
  3984. {
  3985. DEF_OP_NUMERIC_64(uint64, uint64, I64, -);
  3986. HANDLE_OP_END();
  3987. }
  3988. HANDLE_OP(WASM_OP_I64_MUL)
  3989. {
  3990. DEF_OP_NUMERIC_64(uint64, uint64, I64, *);
  3991. HANDLE_OP_END();
  3992. }
  3993. HANDLE_OP(WASM_OP_I64_DIV_S)
  3994. {
  3995. int64 a, b;
  3996. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  3997. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  3998. if (a == (int64)0x8000000000000000LL && b == -1) {
  3999. wasm_set_exception(module, "integer overflow");
  4000. goto got_exception;
  4001. }
  4002. if (b == 0) {
  4003. wasm_set_exception(module, "integer divide by zero");
  4004. goto got_exception;
  4005. }
  4006. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a / b);
  4007. HANDLE_OP_END();
  4008. }
  4009. HANDLE_OP(WASM_OP_I64_DIV_U)
  4010. {
  4011. uint64 a, b;
  4012. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  4013. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  4014. if (b == 0) {
  4015. wasm_set_exception(module, "integer divide by zero");
  4016. goto got_exception;
  4017. }
  4018. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a / b);
  4019. HANDLE_OP_END();
  4020. }
  4021. HANDLE_OP(WASM_OP_I64_REM_S)
  4022. {
  4023. int64 a, b;
  4024. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  4025. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  4026. if (a == (int64)0x8000000000000000LL && b == -1) {
  4027. *(int64 *)(frame_lp + GET_OFFSET()) = 0;
  4028. HANDLE_OP_END();
  4029. }
  4030. if (b == 0) {
  4031. wasm_set_exception(module, "integer divide by zero");
  4032. goto got_exception;
  4033. }
  4034. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a % b);
  4035. HANDLE_OP_END();
  4036. }
  4037. HANDLE_OP(WASM_OP_I64_REM_U)
  4038. {
  4039. uint64 a, b;
  4040. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  4041. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  4042. if (b == 0) {
  4043. wasm_set_exception(module, "integer divide by zero");
  4044. goto got_exception;
  4045. }
  4046. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a % b);
  4047. HANDLE_OP_END();
  4048. }
  4049. HANDLE_OP(WASM_OP_I64_AND)
  4050. {
  4051. DEF_OP_NUMERIC_64(uint64, uint64, I64, &);
  4052. HANDLE_OP_END();
  4053. }
  4054. HANDLE_OP(WASM_OP_I64_OR)
  4055. {
  4056. DEF_OP_NUMERIC_64(uint64, uint64, I64, |);
  4057. HANDLE_OP_END();
  4058. }
  4059. HANDLE_OP(WASM_OP_I64_XOR)
  4060. {
  4061. DEF_OP_NUMERIC_64(uint64, uint64, I64, ^);
  4062. HANDLE_OP_END();
  4063. }
  4064. HANDLE_OP(WASM_OP_I64_SHL)
  4065. {
  4066. DEF_OP_NUMERIC2_64(uint64, uint64, I64, <<);
  4067. HANDLE_OP_END();
  4068. }
  4069. HANDLE_OP(WASM_OP_I64_SHR_S)
  4070. {
  4071. DEF_OP_NUMERIC2_64(int64, uint64, I64, >>);
  4072. HANDLE_OP_END();
  4073. }
  4074. HANDLE_OP(WASM_OP_I64_SHR_U)
  4075. {
  4076. DEF_OP_NUMERIC2_64(uint64, uint64, I64, >>);
  4077. HANDLE_OP_END();
  4078. }
  4079. HANDLE_OP(WASM_OP_I64_ROTL)
  4080. {
  4081. uint64 a, b;
  4082. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  4083. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  4084. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), rotl64(a, b));
  4085. HANDLE_OP_END();
  4086. }
  4087. HANDLE_OP(WASM_OP_I64_ROTR)
  4088. {
  4089. uint64 a, b;
  4090. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  4091. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  4092. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), rotr64(a, b));
  4093. HANDLE_OP_END();
  4094. }
  4095. /* numeric instructions of f32 */
  4096. HANDLE_OP(WASM_OP_F32_ABS)
  4097. {
  4098. DEF_OP_MATH(float32, F32, fabsf);
  4099. HANDLE_OP_END();
  4100. }
  4101. HANDLE_OP(WASM_OP_F32_NEG)
  4102. {
  4103. uint32 u32 = frame_lp[GET_OFFSET()];
  4104. uint32 sign_bit = u32 & ((uint32)1 << 31);
  4105. addr_ret = GET_OFFSET();
  4106. if (sign_bit)
  4107. frame_lp[addr_ret] = u32 & ~((uint32)1 << 31);
  4108. else
  4109. frame_lp[addr_ret] = u32 | ((uint32)1 << 31);
  4110. HANDLE_OP_END();
  4111. }
  4112. HANDLE_OP(WASM_OP_F32_CEIL)
  4113. {
  4114. DEF_OP_MATH(float32, F32, ceilf);
  4115. HANDLE_OP_END();
  4116. }
  4117. HANDLE_OP(WASM_OP_F32_FLOOR)
  4118. {
  4119. DEF_OP_MATH(float32, F32, floorf);
  4120. HANDLE_OP_END();
  4121. }
  4122. HANDLE_OP(WASM_OP_F32_TRUNC)
  4123. {
  4124. DEF_OP_MATH(float32, F32, truncf);
  4125. HANDLE_OP_END();
  4126. }
  4127. HANDLE_OP(WASM_OP_F32_NEAREST)
  4128. {
  4129. DEF_OP_MATH(float32, F32, rintf);
  4130. HANDLE_OP_END();
  4131. }
  4132. HANDLE_OP(WASM_OP_F32_SQRT)
  4133. {
  4134. DEF_OP_MATH(float32, F32, sqrtf);
  4135. HANDLE_OP_END();
  4136. }
  4137. HANDLE_OP(WASM_OP_F32_ADD)
  4138. {
  4139. DEF_OP_NUMERIC(float32, float32, F32, +);
  4140. HANDLE_OP_END();
  4141. }
  4142. HANDLE_OP(WASM_OP_F32_SUB)
  4143. {
  4144. DEF_OP_NUMERIC(float32, float32, F32, -);
  4145. HANDLE_OP_END();
  4146. }
  4147. HANDLE_OP(WASM_OP_F32_MUL)
  4148. {
  4149. DEF_OP_NUMERIC(float32, float32, F32, *);
  4150. HANDLE_OP_END();
  4151. }
  4152. HANDLE_OP(WASM_OP_F32_DIV)
  4153. {
  4154. DEF_OP_NUMERIC(float32, float32, F32, /);
  4155. HANDLE_OP_END();
  4156. }
  4157. HANDLE_OP(WASM_OP_F32_MIN)
  4158. {
  4159. float32 a, b;
  4160. b = *(float32 *)(frame_lp + GET_OFFSET());
  4161. a = *(float32 *)(frame_lp + GET_OFFSET());
  4162. *(float32 *)(frame_lp + GET_OFFSET()) = f32_min(a, b);
  4163. HANDLE_OP_END();
  4164. }
  4165. HANDLE_OP(WASM_OP_F32_MAX)
  4166. {
  4167. float32 a, b;
  4168. b = *(float32 *)(frame_lp + GET_OFFSET());
  4169. a = *(float32 *)(frame_lp + GET_OFFSET());
  4170. *(float32 *)(frame_lp + GET_OFFSET()) = f32_max(a, b);
  4171. HANDLE_OP_END();
  4172. }
  4173. HANDLE_OP(WASM_OP_F32_COPYSIGN)
  4174. {
  4175. float32 a, b;
  4176. b = *(float32 *)(frame_lp + GET_OFFSET());
  4177. a = *(float32 *)(frame_lp + GET_OFFSET());
  4178. *(float32 *)(frame_lp + GET_OFFSET()) = local_copysignf(a, b);
  4179. HANDLE_OP_END();
  4180. }
  4181. /* numeric instructions of f64 */
  4182. HANDLE_OP(WASM_OP_F64_ABS)
  4183. {
  4184. DEF_OP_MATH(float64, F64, fabs);
  4185. HANDLE_OP_END();
  4186. }
  4187. HANDLE_OP(WASM_OP_F64_NEG)
  4188. {
  4189. uint64 u64 = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  4190. uint64 sign_bit = u64 & (((uint64)1) << 63);
  4191. if (sign_bit)
  4192. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(),
  4193. (u64 & ~(((uint64)1) << 63)));
  4194. else
  4195. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(),
  4196. (u64 | (((uint64)1) << 63)));
  4197. HANDLE_OP_END();
  4198. }
  4199. HANDLE_OP(WASM_OP_F64_CEIL)
  4200. {
  4201. DEF_OP_MATH(float64, F64, ceil);
  4202. HANDLE_OP_END();
  4203. }
  4204. HANDLE_OP(WASM_OP_F64_FLOOR)
  4205. {
  4206. DEF_OP_MATH(float64, F64, floor);
  4207. HANDLE_OP_END();
  4208. }
  4209. HANDLE_OP(WASM_OP_F64_TRUNC)
  4210. {
  4211. DEF_OP_MATH(float64, F64, trunc);
  4212. HANDLE_OP_END();
  4213. }
  4214. HANDLE_OP(WASM_OP_F64_NEAREST)
  4215. {
  4216. DEF_OP_MATH(float64, F64, rint);
  4217. HANDLE_OP_END();
  4218. }
  4219. HANDLE_OP(WASM_OP_F64_SQRT)
  4220. {
  4221. DEF_OP_MATH(float64, F64, sqrt);
  4222. HANDLE_OP_END();
  4223. }
  4224. HANDLE_OP(WASM_OP_F64_ADD)
  4225. {
  4226. DEF_OP_NUMERIC_64(float64, float64, F64, +);
  4227. HANDLE_OP_END();
  4228. }
  4229. HANDLE_OP(WASM_OP_F64_SUB)
  4230. {
  4231. DEF_OP_NUMERIC_64(float64, float64, F64, -);
  4232. HANDLE_OP_END();
  4233. }
  4234. HANDLE_OP(WASM_OP_F64_MUL)
  4235. {
  4236. DEF_OP_NUMERIC_64(float64, float64, F64, *);
  4237. HANDLE_OP_END();
  4238. }
  4239. HANDLE_OP(WASM_OP_F64_DIV)
  4240. {
  4241. DEF_OP_NUMERIC_64(float64, float64, F64, /);
  4242. HANDLE_OP_END();
  4243. }
  4244. HANDLE_OP(WASM_OP_F64_MIN)
  4245. {
  4246. float64 a, b;
  4247. b = POP_F64();
  4248. a = POP_F64();
  4249. PUSH_F64(f64_min(a, b));
  4250. HANDLE_OP_END();
  4251. }
  4252. HANDLE_OP(WASM_OP_F64_MAX)
  4253. {
  4254. float64 a, b;
  4255. b = POP_F64();
  4256. a = POP_F64();
  4257. PUSH_F64(f64_max(a, b));
  4258. HANDLE_OP_END();
  4259. }
  4260. HANDLE_OP(WASM_OP_F64_COPYSIGN)
  4261. {
  4262. float64 a, b;
  4263. b = POP_F64();
  4264. a = POP_F64();
  4265. PUSH_F64(local_copysign(a, b));
  4266. HANDLE_OP_END();
  4267. }
  4268. /* conversions of i32 */
  4269. HANDLE_OP(WASM_OP_I32_WRAP_I64)
  4270. {
  4271. int32 value = (int32)(POP_I64() & 0xFFFFFFFFLL);
  4272. PUSH_I32(value);
  4273. HANDLE_OP_END();
  4274. }
  4275. HANDLE_OP(WASM_OP_I32_TRUNC_S_F32)
  4276. {
  4277. /* We don't use INT32_MIN/INT32_MAX/UINT32_MIN/UINT32_MAX,
  4278. since float/double values of ieee754 cannot precisely
  4279. represent all int32/uint32/int64/uint64 values, e.g.:
  4280. UINT32_MAX is 4294967295, but (float32)4294967295 is
  4281. 4294967296.0f, but not 4294967295.0f. */
  4282. DEF_OP_TRUNC_F32(-2147483904.0f, 2147483648.0f, true, true);
  4283. HANDLE_OP_END();
  4284. }
  4285. HANDLE_OP(WASM_OP_I32_TRUNC_U_F32)
  4286. {
  4287. DEF_OP_TRUNC_F32(-1.0f, 4294967296.0f, true, false);
  4288. HANDLE_OP_END();
  4289. }
  4290. HANDLE_OP(WASM_OP_I32_TRUNC_S_F64)
  4291. {
  4292. DEF_OP_TRUNC_F64(-2147483649.0, 2147483648.0, true, true);
  4293. HANDLE_OP_END();
  4294. }
  4295. HANDLE_OP(WASM_OP_I32_TRUNC_U_F64)
  4296. {
  4297. DEF_OP_TRUNC_F64(-1.0, 4294967296.0, true, false);
  4298. HANDLE_OP_END();
  4299. }
  4300. /* conversions of i64 */
  4301. HANDLE_OP(WASM_OP_I64_EXTEND_S_I32)
  4302. {
  4303. DEF_OP_CONVERT(int64, I64, int32, I32);
  4304. HANDLE_OP_END();
  4305. }
  4306. HANDLE_OP(WASM_OP_I64_EXTEND_U_I32)
  4307. {
  4308. DEF_OP_CONVERT(int64, I64, uint32, I32);
  4309. HANDLE_OP_END();
  4310. }
  4311. HANDLE_OP(WASM_OP_I64_TRUNC_S_F32)
  4312. {
  4313. DEF_OP_TRUNC_F32(-9223373136366403584.0f,
  4314. 9223372036854775808.0f, false, true);
  4315. HANDLE_OP_END();
  4316. }
  4317. HANDLE_OP(WASM_OP_I64_TRUNC_U_F32)
  4318. {
  4319. DEF_OP_TRUNC_F32(-1.0f, 18446744073709551616.0f, false, false);
  4320. HANDLE_OP_END();
  4321. }
  4322. HANDLE_OP(WASM_OP_I64_TRUNC_S_F64)
  4323. {
  4324. DEF_OP_TRUNC_F64(-9223372036854777856.0, 9223372036854775808.0,
  4325. false, true);
  4326. HANDLE_OP_END();
  4327. }
  4328. HANDLE_OP(WASM_OP_I64_TRUNC_U_F64)
  4329. {
  4330. DEF_OP_TRUNC_F64(-1.0, 18446744073709551616.0, false, false);
  4331. HANDLE_OP_END();
  4332. }
  4333. /* conversions of f32 */
  4334. HANDLE_OP(WASM_OP_F32_CONVERT_S_I32)
  4335. {
  4336. DEF_OP_CONVERT(float32, F32, int32, I32);
  4337. HANDLE_OP_END();
  4338. }
  4339. HANDLE_OP(WASM_OP_F32_CONVERT_U_I32)
  4340. {
  4341. DEF_OP_CONVERT(float32, F32, uint32, I32);
  4342. HANDLE_OP_END();
  4343. }
  4344. HANDLE_OP(WASM_OP_F32_CONVERT_S_I64)
  4345. {
  4346. DEF_OP_CONVERT(float32, F32, int64, I64);
  4347. HANDLE_OP_END();
  4348. }
  4349. HANDLE_OP(WASM_OP_F32_CONVERT_U_I64)
  4350. {
  4351. DEF_OP_CONVERT(float32, F32, uint64, I64);
  4352. HANDLE_OP_END();
  4353. }
  4354. HANDLE_OP(WASM_OP_F32_DEMOTE_F64)
  4355. {
  4356. DEF_OP_CONVERT(float32, F32, float64, F64);
  4357. HANDLE_OP_END();
  4358. }
  4359. /* conversions of f64 */
  4360. HANDLE_OP(WASM_OP_F64_CONVERT_S_I32)
  4361. {
  4362. DEF_OP_CONVERT(float64, F64, int32, I32);
  4363. HANDLE_OP_END();
  4364. }
  4365. HANDLE_OP(WASM_OP_F64_CONVERT_U_I32)
  4366. {
  4367. DEF_OP_CONVERT(float64, F64, uint32, I32);
  4368. HANDLE_OP_END();
  4369. }
  4370. HANDLE_OP(WASM_OP_F64_CONVERT_S_I64)
  4371. {
  4372. DEF_OP_CONVERT(float64, F64, int64, I64);
  4373. HANDLE_OP_END();
  4374. }
  4375. HANDLE_OP(WASM_OP_F64_CONVERT_U_I64)
  4376. {
  4377. DEF_OP_CONVERT(float64, F64, uint64, I64);
  4378. HANDLE_OP_END();
  4379. }
  4380. HANDLE_OP(WASM_OP_F64_PROMOTE_F32)
  4381. {
  4382. DEF_OP_CONVERT(float64, F64, float32, F32);
  4383. HANDLE_OP_END();
  4384. }
  4385. /* reinterpretations */
  4386. HANDLE_OP(WASM_OP_I32_REINTERPRET_F32)
  4387. HANDLE_OP(WASM_OP_F32_REINTERPRET_I32)
  4388. {
  4389. DEF_OP_REINTERPRET(uint32, I32);
  4390. HANDLE_OP_END();
  4391. }
  4392. HANDLE_OP(WASM_OP_I64_REINTERPRET_F64)
  4393. HANDLE_OP(WASM_OP_F64_REINTERPRET_I64)
  4394. {
  4395. DEF_OP_REINTERPRET(int64, I64);
  4396. HANDLE_OP_END();
  4397. }
  4398. HANDLE_OP(EXT_OP_COPY_STACK_TOP)
  4399. {
  4400. addr1 = GET_OFFSET();
  4401. addr2 = GET_OFFSET();
  4402. frame_lp[addr2] = frame_lp[addr1];
  4403. #if WASM_ENABLE_GC != 0
  4404. /* Ignore constants because they are not reference */
  4405. if (addr1 >= 0) {
  4406. if (*FRAME_REF(addr1)) {
  4407. CLEAR_FRAME_REF(addr1);
  4408. SET_FRAME_REF(addr2);
  4409. }
  4410. }
  4411. #endif
  4412. HANDLE_OP_END();
  4413. }
  4414. HANDLE_OP(EXT_OP_COPY_STACK_TOP_I64)
  4415. {
  4416. addr1 = GET_OFFSET();
  4417. addr2 = GET_OFFSET();
  4418. PUT_I64_TO_ADDR(frame_lp + addr2,
  4419. GET_I64_FROM_ADDR(frame_lp + addr1));
  4420. #if WASM_ENABLE_GC != 0
  4421. /* Ignore constants because they are not reference */
  4422. if (addr1 >= 0) {
  4423. if (*FRAME_REF(addr1)) {
  4424. CLEAR_FRAME_REF(addr1);
  4425. SET_FRAME_REF(addr2);
  4426. }
  4427. }
  4428. #endif
  4429. HANDLE_OP_END();
  4430. }
  4431. #if WASM_ENABLE_SIMDE != 0
  4432. HANDLE_OP(EXT_OP_COPY_STACK_TOP_V128)
  4433. {
  4434. addr1 = GET_OFFSET();
  4435. addr2 = GET_OFFSET();
  4436. PUT_V128_TO_ADDR(frame_lp + addr2,
  4437. GET_V128_FROM_ADDR(frame_lp + addr1));
  4438. #if WASM_ENABLE_GC != 0
  4439. /* Ignore constants because they are not reference */
  4440. if (addr1 >= 0) {
  4441. if (*FRAME_REF(addr1)) {
  4442. CLEAR_FRAME_REF(addr1);
  4443. SET_FRAME_REF(addr2);
  4444. }
  4445. }
  4446. #endif
  4447. HANDLE_OP_END();
  4448. }
  4449. #endif
  4450. HANDLE_OP(EXT_OP_COPY_STACK_VALUES)
  4451. {
  4452. uint32 values_count, total_cell;
  4453. uint8 *cells;
  4454. int16 *src_offsets = NULL;
  4455. uint16 *dst_offsets = NULL;
  4456. /* read values_count */
  4457. values_count = read_uint32(frame_ip);
  4458. /* read total cell num */
  4459. total_cell = read_uint32(frame_ip);
  4460. /* cells */
  4461. cells = (uint8 *)frame_ip;
  4462. frame_ip += values_count * CELL_SIZE;
  4463. /* src offsets */
  4464. src_offsets = (int16 *)frame_ip;
  4465. frame_ip += values_count * sizeof(int16);
  4466. /* dst offsets */
  4467. dst_offsets = (uint16 *)frame_ip;
  4468. frame_ip += values_count * sizeof(uint16);
  4469. if (!copy_stack_values(module, frame_lp, values_count,
  4470. #if WASM_ENABLE_GC != 0
  4471. frame_ref,
  4472. #endif
  4473. total_cell, cells, src_offsets,
  4474. dst_offsets))
  4475. goto got_exception;
  4476. HANDLE_OP_END();
  4477. }
  4478. HANDLE_OP(WASM_OP_SET_LOCAL)
  4479. {
  4480. opcode = WASM_OP_SET_LOCAL;
  4481. goto handle_op_set_tee_local;
  4482. }
  4483. HANDLE_OP(WASM_OP_TEE_LOCAL)
  4484. {
  4485. opcode = WASM_OP_TEE_LOCAL;
  4486. handle_op_set_tee_local:
  4487. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  4488. addr1 = GET_OFFSET();
  4489. if (local_type == VALUE_TYPE_I32 || local_type == VALUE_TYPE_F32
  4490. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  4491. || local_type == VALUE_TYPE_FUNCREF
  4492. || local_type == VALUE_TYPE_EXTERNREF
  4493. #endif
  4494. ) {
  4495. *(int32 *)(frame_lp + local_offset) = frame_lp[addr1];
  4496. }
  4497. else if (local_type == VALUE_TYPE_I64
  4498. || local_type == VALUE_TYPE_F64) {
  4499. PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),
  4500. GET_I64_FROM_ADDR(frame_lp + addr1));
  4501. }
  4502. #if WASM_ENABLE_GC != 0
  4503. else if (wasm_is_type_reftype(local_type)) {
  4504. PUT_REF_TO_ADDR((uint32 *)(frame_lp + local_offset),
  4505. GET_REF_FROM_ADDR(frame_lp + addr1));
  4506. if (opcode == WASM_OP_SET_LOCAL) {
  4507. CLEAR_FRAME_REF(addr1);
  4508. }
  4509. }
  4510. #endif
  4511. else {
  4512. wasm_set_exception(module, "invalid local type");
  4513. goto got_exception;
  4514. }
  4515. HANDLE_OP_END();
  4516. }
  4517. HANDLE_OP(WASM_OP_I32_EXTEND8_S)
  4518. {
  4519. DEF_OP_CONVERT(int32, I32, int8, I32);
  4520. HANDLE_OP_END();
  4521. }
  4522. HANDLE_OP(WASM_OP_I32_EXTEND16_S)
  4523. {
  4524. DEF_OP_CONVERT(int32, I32, int16, I32);
  4525. HANDLE_OP_END();
  4526. }
  4527. HANDLE_OP(WASM_OP_I64_EXTEND8_S)
  4528. {
  4529. DEF_OP_CONVERT(int64, I64, int8, I64);
  4530. HANDLE_OP_END();
  4531. }
  4532. HANDLE_OP(WASM_OP_I64_EXTEND16_S)
  4533. {
  4534. DEF_OP_CONVERT(int64, I64, int16, I64);
  4535. HANDLE_OP_END();
  4536. }
  4537. HANDLE_OP(WASM_OP_I64_EXTEND32_S)
  4538. {
  4539. DEF_OP_CONVERT(int64, I64, int32, I64);
  4540. HANDLE_OP_END();
  4541. }
  4542. HANDLE_OP(WASM_OP_MISC_PREFIX)
  4543. {
  4544. GET_OPCODE();
  4545. switch (opcode) {
  4546. case WASM_OP_I32_TRUNC_SAT_S_F32:
  4547. DEF_OP_TRUNC_SAT_F32(-2147483904.0f, 2147483648.0f,
  4548. true, true);
  4549. break;
  4550. case WASM_OP_I32_TRUNC_SAT_U_F32:
  4551. DEF_OP_TRUNC_SAT_F32(-1.0f, 4294967296.0f, true, false);
  4552. break;
  4553. case WASM_OP_I32_TRUNC_SAT_S_F64:
  4554. DEF_OP_TRUNC_SAT_F64(-2147483649.0, 2147483648.0, true,
  4555. true);
  4556. break;
  4557. case WASM_OP_I32_TRUNC_SAT_U_F64:
  4558. DEF_OP_TRUNC_SAT_F64(-1.0, 4294967296.0, true, false);
  4559. break;
  4560. case WASM_OP_I64_TRUNC_SAT_S_F32:
  4561. DEF_OP_TRUNC_SAT_F32(-9223373136366403584.0f,
  4562. 9223372036854775808.0f, false,
  4563. true);
  4564. break;
  4565. case WASM_OP_I64_TRUNC_SAT_U_F32:
  4566. DEF_OP_TRUNC_SAT_F32(-1.0f, 18446744073709551616.0f,
  4567. false, false);
  4568. break;
  4569. case WASM_OP_I64_TRUNC_SAT_S_F64:
  4570. DEF_OP_TRUNC_SAT_F64(-9223372036854777856.0,
  4571. 9223372036854775808.0, false,
  4572. true);
  4573. break;
  4574. case WASM_OP_I64_TRUNC_SAT_U_F64:
  4575. DEF_OP_TRUNC_SAT_F64(-1.0, 18446744073709551616.0,
  4576. false, false);
  4577. break;
  4578. #if WASM_ENABLE_BULK_MEMORY != 0
  4579. case WASM_OP_MEMORY_INIT:
  4580. {
  4581. uint32 addr, segment;
  4582. uint64 bytes, offset, seg_len;
  4583. uint8 *data;
  4584. segment = read_uint32(frame_ip);
  4585. bytes = (uint64)(uint32)POP_I32();
  4586. offset = (uint64)(uint32)POP_I32();
  4587. addr = POP_I32();
  4588. #if WASM_ENABLE_THREAD_MGR != 0
  4589. linear_mem_size = get_linear_mem_size();
  4590. #endif
  4591. #ifndef OS_ENABLE_HW_BOUND_CHECK
  4592. CHECK_BULK_MEMORY_OVERFLOW(addr, bytes, maddr);
  4593. #else
  4594. #if WASM_ENABLE_SHARED_HEAP != 0
  4595. if (app_addr_in_shared_heap((uint64)(uint32)addr,
  4596. bytes))
  4597. shared_heap_addr_app_to_native((uint64)(uint32)addr,
  4598. maddr);
  4599. else
  4600. #endif
  4601. {
  4602. if ((uint64)(uint32)addr + bytes > linear_mem_size)
  4603. goto out_of_bounds;
  4604. maddr = memory->memory_data + (uint32)addr;
  4605. }
  4606. #endif
  4607. if (bh_bitmap_get_bit(module->e->common.data_dropped,
  4608. segment)) {
  4609. seg_len = 0;
  4610. data = NULL;
  4611. }
  4612. else {
  4613. seg_len =
  4614. (uint64)module->module->data_segments[segment]
  4615. ->data_length;
  4616. data = module->module->data_segments[segment]->data;
  4617. }
  4618. if (offset + bytes > seg_len)
  4619. goto out_of_bounds;
  4620. bh_memcpy_s(maddr, (uint32)(linear_mem_size - addr),
  4621. data + offset, (uint32)bytes);
  4622. break;
  4623. }
  4624. case WASM_OP_DATA_DROP:
  4625. {
  4626. uint32 segment;
  4627. segment = read_uint32(frame_ip);
  4628. bh_bitmap_set_bit(module->e->common.data_dropped,
  4629. segment);
  4630. break;
  4631. }
  4632. case WASM_OP_MEMORY_COPY:
  4633. {
  4634. uint32 dst, src, len;
  4635. uint8 *mdst, *msrc;
  4636. len = POP_I32();
  4637. src = POP_I32();
  4638. dst = POP_I32();
  4639. #if WASM_ENABLE_THREAD_MGR != 0
  4640. linear_mem_size = get_linear_mem_size();
  4641. #endif
  4642. #ifndef OS_ENABLE_HW_BOUND_CHECK
  4643. CHECK_BULK_MEMORY_OVERFLOW(src, len, msrc);
  4644. CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
  4645. #else /* else of OS_ENABLE_HW_BOUND_CHECK */
  4646. #if WASM_ENABLE_SHARED_HEAP != 0
  4647. if (app_addr_in_shared_heap((uint64)src, len))
  4648. shared_heap_addr_app_to_native((uint64)src, msrc);
  4649. else
  4650. #endif
  4651. {
  4652. if ((uint64)(uint32)src + len > linear_mem_size)
  4653. goto out_of_bounds;
  4654. msrc = memory->memory_data + (uint32)src;
  4655. }
  4656. #if WASM_ENABLE_SHARED_HEAP != 0
  4657. if (app_addr_in_shared_heap((uint64)dst, len)) {
  4658. shared_heap_addr_app_to_native((uint64)dst, mdst);
  4659. }
  4660. else
  4661. #endif
  4662. {
  4663. if ((uint64)(uint32)dst + len > linear_mem_size)
  4664. goto out_of_bounds;
  4665. mdst = memory->memory_data + (uint32)dst;
  4666. }
  4667. #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
  4668. /*
  4669. * avoid unnecessary operations
  4670. *
  4671. * since dst and src both are valid indexes in the
  4672. * linear memory, mdst and msrc can't be NULL
  4673. *
  4674. * The spec. converts memory.copy into i32.load8 and
  4675. * i32.store8; the following are runtime-specific
  4676. * optimizations.
  4677. *
  4678. */
  4679. if (len && mdst != msrc) {
  4680. /* allowing the destination and source to overlap */
  4681. memmove(mdst, msrc, len);
  4682. }
  4683. break;
  4684. }
  4685. case WASM_OP_MEMORY_FILL:
  4686. {
  4687. uint32 dst, len;
  4688. uint8 fill_val, *mdst;
  4689. len = POP_I32();
  4690. fill_val = POP_I32();
  4691. dst = POP_I32();
  4692. #if WASM_ENABLE_THREAD_MGR != 0
  4693. linear_mem_size = get_linear_mem_size();
  4694. #endif
  4695. #ifndef OS_ENABLE_HW_BOUND_CHECK
  4696. CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
  4697. #else
  4698. #if WASM_ENABLE_SHARED_HEAP != 0
  4699. if (app_addr_in_shared_heap((uint64)(uint32)dst, len))
  4700. shared_heap_addr_app_to_native((uint64)(uint32)dst,
  4701. mdst);
  4702. else
  4703. #endif
  4704. {
  4705. if ((uint64)(uint32)dst + len > linear_mem_size)
  4706. goto out_of_bounds;
  4707. mdst = memory->memory_data + (uint32)dst;
  4708. }
  4709. #endif
  4710. memset(mdst, fill_val, len);
  4711. break;
  4712. }
  4713. #endif /* WASM_ENABLE_BULK_MEMORY */
  4714. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  4715. case WASM_OP_TABLE_INIT:
  4716. {
  4717. uint32 tbl_idx, elem_idx;
  4718. uint32 n, s, d;
  4719. WASMTableInstance *tbl_inst;
  4720. table_elem_type_t *table_elems;
  4721. InitializerExpression *tbl_seg_init_values = NULL,
  4722. *init_values;
  4723. uint64 i;
  4724. uint32 tbl_seg_len = 0;
  4725. elem_idx = read_uint32(frame_ip);
  4726. bh_assert(elem_idx < module->module->table_seg_count);
  4727. tbl_idx = read_uint32(frame_ip);
  4728. bh_assert(tbl_idx < module->module->table_count);
  4729. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  4730. n = (uint32)POP_I32();
  4731. s = (uint32)POP_I32();
  4732. d = (uint32)POP_I32();
  4733. if (!bh_bitmap_get_bit(module->e->common.elem_dropped,
  4734. elem_idx)) {
  4735. /* table segment isn't dropped */
  4736. tbl_seg_init_values =
  4737. module->module->table_segments[elem_idx]
  4738. .init_values;
  4739. tbl_seg_len =
  4740. module->module->table_segments[elem_idx]
  4741. .value_count;
  4742. }
  4743. if (offset_len_out_of_bounds(s, n, tbl_seg_len)
  4744. || offset_len_out_of_bounds(d, n,
  4745. tbl_inst->cur_size)) {
  4746. wasm_set_exception(module,
  4747. "out of bounds table access");
  4748. goto got_exception;
  4749. }
  4750. if (!n) {
  4751. break;
  4752. }
  4753. table_elems = tbl_inst->elems + d;
  4754. init_values = tbl_seg_init_values + s;
  4755. #if WASM_ENABLE_GC != 0
  4756. SYNC_ALL_TO_FRAME();
  4757. #endif
  4758. for (i = 0; i < n; i++) {
  4759. /* UINT32_MAX indicates that it is a null ref */
  4760. bh_assert(init_values[i].init_expr_type
  4761. == INIT_EXPR_TYPE_REFNULL_CONST
  4762. || init_values[i].init_expr_type
  4763. == INIT_EXPR_TYPE_FUNCREF_CONST);
  4764. #if WASM_ENABLE_GC == 0
  4765. table_elems[i] =
  4766. (table_elem_type_t)init_values[i].u.ref_index;
  4767. #else
  4768. if (init_values[i].u.ref_index != UINT32_MAX) {
  4769. if (!(func_obj = wasm_create_func_obj(
  4770. module, init_values[i].u.ref_index,
  4771. true, NULL, 0))) {
  4772. goto got_exception;
  4773. }
  4774. table_elems[i] = func_obj;
  4775. }
  4776. else {
  4777. table_elems[i] = NULL_REF;
  4778. }
  4779. #endif
  4780. }
  4781. break;
  4782. }
  4783. case WASM_OP_ELEM_DROP:
  4784. {
  4785. uint32 elem_idx = read_uint32(frame_ip);
  4786. bh_assert(elem_idx < module->module->table_seg_count);
  4787. bh_bitmap_set_bit(module->e->common.elem_dropped,
  4788. elem_idx);
  4789. break;
  4790. }
  4791. case WASM_OP_TABLE_COPY:
  4792. {
  4793. uint32 src_tbl_idx, dst_tbl_idx;
  4794. uint32 n, s, d;
  4795. WASMTableInstance *src_tbl_inst, *dst_tbl_inst;
  4796. dst_tbl_idx = read_uint32(frame_ip);
  4797. bh_assert(dst_tbl_idx < module->table_count);
  4798. dst_tbl_inst = wasm_get_table_inst(module, dst_tbl_idx);
  4799. src_tbl_idx = read_uint32(frame_ip);
  4800. bh_assert(src_tbl_idx < module->table_count);
  4801. src_tbl_inst = wasm_get_table_inst(module, src_tbl_idx);
  4802. n = (uint32)POP_I32();
  4803. s = (uint32)POP_I32();
  4804. d = (uint32)POP_I32();
  4805. if (offset_len_out_of_bounds(d, n,
  4806. dst_tbl_inst->cur_size)
  4807. || offset_len_out_of_bounds(
  4808. s, n, src_tbl_inst->cur_size)) {
  4809. wasm_set_exception(module,
  4810. "out of bounds table access");
  4811. goto got_exception;
  4812. }
  4813. /* if s >= d, copy from front to back */
  4814. /* if s < d, copy from back to front */
  4815. /* merge all together */
  4816. bh_memmove_s((uint8 *)dst_tbl_inst
  4817. + offsetof(WASMTableInstance, elems)
  4818. + d * sizeof(table_elem_type_t),
  4819. (uint32)((dst_tbl_inst->cur_size - d)
  4820. * sizeof(table_elem_type_t)),
  4821. (uint8 *)src_tbl_inst
  4822. + offsetof(WASMTableInstance, elems)
  4823. + s * sizeof(table_elem_type_t),
  4824. (uint32)(n * sizeof(table_elem_type_t)));
  4825. break;
  4826. }
  4827. case WASM_OP_TABLE_GROW:
  4828. {
  4829. uint32 tbl_idx, n, orig_tbl_sz;
  4830. WASMTableInstance *tbl_inst;
  4831. table_elem_type_t init_val;
  4832. tbl_idx = read_uint32(frame_ip);
  4833. bh_assert(tbl_idx < module->table_count);
  4834. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  4835. orig_tbl_sz = tbl_inst->cur_size;
  4836. n = POP_I32();
  4837. #if WASM_ENABLE_GC == 0
  4838. init_val = POP_I32();
  4839. #else
  4840. init_val = POP_REF();
  4841. #endif
  4842. if (!wasm_enlarge_table(module, tbl_idx, n, init_val)) {
  4843. PUSH_I32(-1);
  4844. }
  4845. else {
  4846. PUSH_I32(orig_tbl_sz);
  4847. }
  4848. break;
  4849. }
  4850. case WASM_OP_TABLE_SIZE:
  4851. {
  4852. uint32 tbl_idx;
  4853. WASMTableInstance *tbl_inst;
  4854. tbl_idx = read_uint32(frame_ip);
  4855. bh_assert(tbl_idx < module->table_count);
  4856. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  4857. PUSH_I32(tbl_inst->cur_size);
  4858. break;
  4859. }
  4860. case WASM_OP_TABLE_FILL:
  4861. {
  4862. uint32 tbl_idx, n, i;
  4863. WASMTableInstance *tbl_inst;
  4864. table_elem_type_t fill_val;
  4865. tbl_idx = read_uint32(frame_ip);
  4866. bh_assert(tbl_idx < module->table_count);
  4867. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  4868. n = POP_I32();
  4869. #if WASM_ENABLE_GC == 0
  4870. fill_val = POP_I32();
  4871. #else
  4872. fill_val = POP_REF();
  4873. #endif
  4874. i = POP_I32();
  4875. if (offset_len_out_of_bounds(i, n,
  4876. tbl_inst->cur_size)) {
  4877. wasm_set_exception(module,
  4878. "out of bounds table access");
  4879. goto got_exception;
  4880. }
  4881. for (; n != 0; i++, n--) {
  4882. tbl_inst->elems[i] = fill_val;
  4883. }
  4884. break;
  4885. }
  4886. #endif /* WASM_ENABLE_REF_TYPES */
  4887. default:
  4888. wasm_set_exception(module, "unsupported opcode");
  4889. goto got_exception;
  4890. }
  4891. HANDLE_OP_END();
  4892. }
  4893. #if WASM_ENABLE_SHARED_MEMORY != 0
  4894. HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
  4895. {
  4896. uint32 offset = 0, addr;
  4897. GET_OPCODE();
  4898. if (opcode != WASM_OP_ATOMIC_FENCE) {
  4899. offset = read_uint32(frame_ip);
  4900. }
  4901. switch (opcode) {
  4902. case WASM_OP_ATOMIC_NOTIFY:
  4903. {
  4904. uint32 notify_count, ret;
  4905. notify_count = POP_I32();
  4906. addr = POP_I32();
  4907. CHECK_MEMORY_OVERFLOW(4);
  4908. CHECK_ATOMIC_MEMORY_ACCESS(4);
  4909. ret = wasm_runtime_atomic_notify(
  4910. (WASMModuleInstanceCommon *)module, maddr,
  4911. notify_count);
  4912. if (ret == (uint32)-1)
  4913. goto got_exception;
  4914. PUSH_I32(ret);
  4915. break;
  4916. }
  4917. case WASM_OP_ATOMIC_WAIT32:
  4918. {
  4919. uint64 timeout;
  4920. uint32 expect, ret;
  4921. timeout = POP_I64();
  4922. expect = POP_I32();
  4923. addr = POP_I32();
  4924. CHECK_MEMORY_OVERFLOW(4);
  4925. CHECK_ATOMIC_MEMORY_ACCESS(4);
  4926. ret = wasm_runtime_atomic_wait(
  4927. (WASMModuleInstanceCommon *)module, maddr,
  4928. (uint64)expect, timeout, false);
  4929. if (ret == (uint32)-1)
  4930. goto got_exception;
  4931. #if WASM_ENABLE_THREAD_MGR != 0
  4932. CHECK_SUSPEND_FLAGS();
  4933. #endif
  4934. PUSH_I32(ret);
  4935. break;
  4936. }
  4937. case WASM_OP_ATOMIC_WAIT64:
  4938. {
  4939. uint64 timeout, expect;
  4940. uint32 ret;
  4941. timeout = POP_I64();
  4942. expect = POP_I64();
  4943. addr = POP_I32();
  4944. CHECK_MEMORY_OVERFLOW(8);
  4945. CHECK_ATOMIC_MEMORY_ACCESS(8);
  4946. ret = wasm_runtime_atomic_wait(
  4947. (WASMModuleInstanceCommon *)module, maddr, expect,
  4948. timeout, true);
  4949. if (ret == (uint32)-1)
  4950. goto got_exception;
  4951. #if WASM_ENABLE_THREAD_MGR != 0
  4952. CHECK_SUSPEND_FLAGS();
  4953. #endif
  4954. PUSH_I32(ret);
  4955. break;
  4956. }
  4957. case WASM_OP_ATOMIC_FENCE:
  4958. {
  4959. os_atomic_thread_fence(os_memory_order_seq_cst);
  4960. break;
  4961. }
  4962. case WASM_OP_ATOMIC_I32_LOAD:
  4963. case WASM_OP_ATOMIC_I32_LOAD8_U:
  4964. case WASM_OP_ATOMIC_I32_LOAD16_U:
  4965. {
  4966. uint32 readv;
  4967. addr = POP_I32();
  4968. if (opcode == WASM_OP_ATOMIC_I32_LOAD8_U) {
  4969. CHECK_MEMORY_OVERFLOW(1);
  4970. CHECK_ATOMIC_MEMORY_ACCESS(1);
  4971. shared_memory_lock(memory);
  4972. readv = (uint32)(*(uint8 *)maddr);
  4973. shared_memory_unlock(memory);
  4974. }
  4975. else if (opcode == WASM_OP_ATOMIC_I32_LOAD16_U) {
  4976. CHECK_MEMORY_OVERFLOW(2);
  4977. CHECK_ATOMIC_MEMORY_ACCESS(2);
  4978. shared_memory_lock(memory);
  4979. readv = (uint32)LOAD_U16(maddr);
  4980. shared_memory_unlock(memory);
  4981. }
  4982. else {
  4983. CHECK_MEMORY_OVERFLOW(4);
  4984. CHECK_ATOMIC_MEMORY_ACCESS(4);
  4985. shared_memory_lock(memory);
  4986. readv = LOAD_I32(maddr);
  4987. shared_memory_unlock(memory);
  4988. }
  4989. PUSH_I32(readv);
  4990. break;
  4991. }
  4992. case WASM_OP_ATOMIC_I64_LOAD:
  4993. case WASM_OP_ATOMIC_I64_LOAD8_U:
  4994. case WASM_OP_ATOMIC_I64_LOAD16_U:
  4995. case WASM_OP_ATOMIC_I64_LOAD32_U:
  4996. {
  4997. uint64 readv;
  4998. addr = POP_I32();
  4999. if (opcode == WASM_OP_ATOMIC_I64_LOAD8_U) {
  5000. CHECK_MEMORY_OVERFLOW(1);
  5001. CHECK_ATOMIC_MEMORY_ACCESS(1);
  5002. shared_memory_lock(memory);
  5003. readv = (uint64)(*(uint8 *)maddr);
  5004. shared_memory_unlock(memory);
  5005. }
  5006. else if (opcode == WASM_OP_ATOMIC_I64_LOAD16_U) {
  5007. CHECK_MEMORY_OVERFLOW(2);
  5008. CHECK_ATOMIC_MEMORY_ACCESS(2);
  5009. shared_memory_lock(memory);
  5010. readv = (uint64)LOAD_U16(maddr);
  5011. shared_memory_unlock(memory);
  5012. }
  5013. else if (opcode == WASM_OP_ATOMIC_I64_LOAD32_U) {
  5014. CHECK_MEMORY_OVERFLOW(4);
  5015. CHECK_ATOMIC_MEMORY_ACCESS(4);
  5016. shared_memory_lock(memory);
  5017. readv = (uint64)LOAD_U32(maddr);
  5018. shared_memory_unlock(memory);
  5019. }
  5020. else {
  5021. CHECK_MEMORY_OVERFLOW(8);
  5022. CHECK_ATOMIC_MEMORY_ACCESS(8);
  5023. shared_memory_lock(memory);
  5024. readv = LOAD_I64(maddr);
  5025. shared_memory_unlock(memory);
  5026. }
  5027. PUSH_I64(readv);
  5028. break;
  5029. }
  5030. case WASM_OP_ATOMIC_I32_STORE:
  5031. case WASM_OP_ATOMIC_I32_STORE8:
  5032. case WASM_OP_ATOMIC_I32_STORE16:
  5033. {
  5034. uint32 sval;
  5035. sval = (uint32)POP_I32();
  5036. addr = POP_I32();
  5037. if (opcode == WASM_OP_ATOMIC_I32_STORE8) {
  5038. CHECK_MEMORY_OVERFLOW(1);
  5039. CHECK_ATOMIC_MEMORY_ACCESS(1);
  5040. shared_memory_lock(memory);
  5041. *(uint8 *)maddr = (uint8)sval;
  5042. shared_memory_unlock(memory);
  5043. }
  5044. else if (opcode == WASM_OP_ATOMIC_I32_STORE16) {
  5045. CHECK_MEMORY_OVERFLOW(2);
  5046. CHECK_ATOMIC_MEMORY_ACCESS(2);
  5047. shared_memory_lock(memory);
  5048. STORE_U16(maddr, (uint16)sval);
  5049. shared_memory_unlock(memory);
  5050. }
  5051. else {
  5052. CHECK_MEMORY_OVERFLOW(4);
  5053. CHECK_ATOMIC_MEMORY_ACCESS(4);
  5054. shared_memory_lock(memory);
  5055. STORE_U32(maddr, sval);
  5056. shared_memory_unlock(memory);
  5057. }
  5058. break;
  5059. }
  5060. case WASM_OP_ATOMIC_I64_STORE:
  5061. case WASM_OP_ATOMIC_I64_STORE8:
  5062. case WASM_OP_ATOMIC_I64_STORE16:
  5063. case WASM_OP_ATOMIC_I64_STORE32:
  5064. {
  5065. uint64 sval;
  5066. sval = (uint64)POP_I64();
  5067. addr = POP_I32();
  5068. if (opcode == WASM_OP_ATOMIC_I64_STORE8) {
  5069. CHECK_MEMORY_OVERFLOW(1);
  5070. CHECK_ATOMIC_MEMORY_ACCESS(1);
  5071. shared_memory_lock(memory);
  5072. *(uint8 *)maddr = (uint8)sval;
  5073. shared_memory_unlock(memory);
  5074. }
  5075. else if (opcode == WASM_OP_ATOMIC_I64_STORE16) {
  5076. CHECK_MEMORY_OVERFLOW(2);
  5077. CHECK_ATOMIC_MEMORY_ACCESS(2);
  5078. shared_memory_lock(memory);
  5079. STORE_U16(maddr, (uint16)sval);
  5080. shared_memory_unlock(memory);
  5081. }
  5082. else if (opcode == WASM_OP_ATOMIC_I64_STORE32) {
  5083. CHECK_MEMORY_OVERFLOW(4);
  5084. CHECK_ATOMIC_MEMORY_ACCESS(4);
  5085. shared_memory_lock(memory);
  5086. STORE_U32(maddr, (uint32)sval);
  5087. shared_memory_unlock(memory);
  5088. }
  5089. else {
  5090. CHECK_MEMORY_OVERFLOW(8);
  5091. CHECK_ATOMIC_MEMORY_ACCESS(8);
  5092. shared_memory_lock(memory);
  5093. STORE_I64(maddr, sval);
  5094. shared_memory_unlock(memory);
  5095. }
  5096. break;
  5097. }
  5098. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  5099. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  5100. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  5101. {
  5102. uint32 readv, sval, expect;
  5103. sval = POP_I32();
  5104. expect = POP_I32();
  5105. addr = POP_I32();
  5106. if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U) {
  5107. CHECK_MEMORY_OVERFLOW(1);
  5108. CHECK_ATOMIC_MEMORY_ACCESS(1);
  5109. expect = (uint8)expect;
  5110. shared_memory_lock(memory);
  5111. readv = (uint32)(*(uint8 *)maddr);
  5112. if (readv == expect)
  5113. *(uint8 *)maddr = (uint8)(sval);
  5114. shared_memory_unlock(memory);
  5115. }
  5116. else if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U) {
  5117. CHECK_MEMORY_OVERFLOW(2);
  5118. CHECK_ATOMIC_MEMORY_ACCESS(2);
  5119. expect = (uint16)expect;
  5120. shared_memory_lock(memory);
  5121. readv = (uint32)LOAD_U16(maddr);
  5122. if (readv == expect)
  5123. STORE_U16(maddr, (uint16)(sval));
  5124. shared_memory_unlock(memory);
  5125. }
  5126. else {
  5127. CHECK_MEMORY_OVERFLOW(4);
  5128. CHECK_ATOMIC_MEMORY_ACCESS(4);
  5129. shared_memory_lock(memory);
  5130. readv = LOAD_I32(maddr);
  5131. if (readv == expect)
  5132. STORE_U32(maddr, sval);
  5133. shared_memory_unlock(memory);
  5134. }
  5135. PUSH_I32(readv);
  5136. break;
  5137. }
  5138. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  5139. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  5140. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  5141. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  5142. {
  5143. uint64 readv, sval, expect;
  5144. sval = (uint64)POP_I64();
  5145. expect = (uint64)POP_I64();
  5146. addr = POP_I32();
  5147. if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U) {
  5148. CHECK_MEMORY_OVERFLOW(1);
  5149. CHECK_ATOMIC_MEMORY_ACCESS(1);
  5150. expect = (uint8)expect;
  5151. shared_memory_lock(memory);
  5152. readv = (uint64)(*(uint8 *)maddr);
  5153. if (readv == expect)
  5154. *(uint8 *)maddr = (uint8)(sval);
  5155. shared_memory_unlock(memory);
  5156. }
  5157. else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U) {
  5158. CHECK_MEMORY_OVERFLOW(2);
  5159. CHECK_ATOMIC_MEMORY_ACCESS(2);
  5160. expect = (uint16)expect;
  5161. shared_memory_lock(memory);
  5162. readv = (uint64)LOAD_U16(maddr);
  5163. if (readv == expect)
  5164. STORE_U16(maddr, (uint16)(sval));
  5165. shared_memory_unlock(memory);
  5166. }
  5167. else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U) {
  5168. CHECK_MEMORY_OVERFLOW(4);
  5169. CHECK_ATOMIC_MEMORY_ACCESS(4);
  5170. expect = (uint32)expect;
  5171. shared_memory_lock(memory);
  5172. readv = (uint64)LOAD_U32(maddr);
  5173. if (readv == expect)
  5174. STORE_U32(maddr, (uint32)(sval));
  5175. shared_memory_unlock(memory);
  5176. }
  5177. else {
  5178. CHECK_MEMORY_OVERFLOW(8);
  5179. CHECK_ATOMIC_MEMORY_ACCESS(8);
  5180. shared_memory_lock(memory);
  5181. readv = (uint64)LOAD_I64(maddr);
  5182. if (readv == expect)
  5183. STORE_I64(maddr, sval);
  5184. shared_memory_unlock(memory);
  5185. }
  5186. PUSH_I64(readv);
  5187. break;
  5188. }
  5189. DEF_ATOMIC_RMW_OPCODE(ADD, +);
  5190. DEF_ATOMIC_RMW_OPCODE(SUB, -);
  5191. DEF_ATOMIC_RMW_OPCODE(AND, &);
  5192. DEF_ATOMIC_RMW_OPCODE(OR, |);
  5193. DEF_ATOMIC_RMW_OPCODE(XOR, ^);
  5194. /* xchg, ignore the read value, and store the given
  5195. value: readv * 0 + sval */
  5196. DEF_ATOMIC_RMW_OPCODE(XCHG, *0 +);
  5197. }
  5198. HANDLE_OP_END();
  5199. }
  5200. #endif
  5201. HANDLE_OP(WASM_OP_IMPDEP)
  5202. {
  5203. frame = prev_frame;
  5204. frame_ip = frame->ip;
  5205. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  5206. is_return_call = false;
  5207. #endif
  5208. goto call_func_from_entry;
  5209. }
  5210. #if WASM_ENABLE_SIMDE != 0
  5211. #define SIMD_V128_TO_SIMDE_V128(s_v) \
  5212. ({ \
  5213. bh_assert(sizeof(V128) == sizeof(simde_v128_t)); \
  5214. simde_v128_t se_v; \
  5215. bh_memcpy_s(&se_v, sizeof(simde_v128_t), &(s_v), sizeof(V128)); \
  5216. se_v; \
  5217. })
  5218. #define SIMDE_V128_TO_SIMD_V128(sv, v) \
  5219. do { \
  5220. bh_assert(sizeof(V128) == sizeof(simde_v128_t)); \
  5221. bh_memcpy_s(&(v), sizeof(V128), &(sv), sizeof(simde_v128_t)); \
  5222. } while (0)
  5223. HANDLE_OP(WASM_OP_SIMD_PREFIX)
  5224. {
  5225. GET_OPCODE();
  5226. switch (opcode) {
  5227. /* Memory */
  5228. case SIMD_v128_load:
  5229. {
  5230. uint32 offset, addr;
  5231. offset = read_uint32(frame_ip);
  5232. addr = POP_I32();
  5233. addr_ret = GET_OFFSET();
  5234. CHECK_MEMORY_OVERFLOW(16);
  5235. PUT_V128_TO_ADDR(frame_lp + addr_ret, LOAD_V128(maddr));
  5236. break;
  5237. }
  5238. #define SIMD_LOAD_OP(simde_func) \
  5239. do { \
  5240. uint32 offset, addr; \
  5241. offset = read_uint32(frame_ip); \
  5242. addr = POP_I32(); \
  5243. addr_ret = GET_OFFSET(); \
  5244. CHECK_MEMORY_OVERFLOW(8); \
  5245. \
  5246. simde_v128_t simde_result = simde_func(maddr); \
  5247. \
  5248. V128 result; \
  5249. SIMDE_V128_TO_SIMD_V128(simde_result, result); \
  5250. PUT_V128_TO_ADDR(frame_lp + addr_ret, result); \
  5251. \
  5252. } while (0)
  5253. case SIMD_v128_load8x8_s:
  5254. {
  5255. SIMD_LOAD_OP(simde_wasm_i16x8_load8x8);
  5256. break;
  5257. }
  5258. case SIMD_v128_load8x8_u:
  5259. {
  5260. SIMD_LOAD_OP(simde_wasm_u16x8_load8x8);
  5261. break;
  5262. }
  5263. case SIMD_v128_load16x4_s:
  5264. {
  5265. SIMD_LOAD_OP(simde_wasm_i32x4_load16x4);
  5266. break;
  5267. }
  5268. case SIMD_v128_load16x4_u:
  5269. {
  5270. SIMD_LOAD_OP(simde_wasm_u32x4_load16x4);
  5271. break;
  5272. }
  5273. case SIMD_v128_load32x2_s:
  5274. {
  5275. SIMD_LOAD_OP(simde_wasm_i64x2_load32x2);
  5276. break;
  5277. }
  5278. case SIMD_v128_load32x2_u:
  5279. {
  5280. SIMD_LOAD_OP(simde_wasm_u64x2_load32x2);
  5281. break;
  5282. }
  5283. #define SIMD_LOAD_SPLAT_OP(simde_func, width) \
  5284. do { \
  5285. uint32 offset, addr; \
  5286. offset = read_uint32(frame_ip); \
  5287. addr = POP_I32(); \
  5288. addr_ret = GET_OFFSET(); \
  5289. CHECK_MEMORY_OVERFLOW(width / 8); \
  5290. \
  5291. simde_v128_t simde_result = simde_func(maddr); \
  5292. \
  5293. V128 result; \
  5294. SIMDE_V128_TO_SIMD_V128(simde_result, result); \
  5295. \
  5296. PUT_V128_TO_ADDR(frame_lp + addr_ret, result); \
  5297. } while (0)
  5298. case SIMD_v128_load8_splat:
  5299. {
  5300. SIMD_LOAD_SPLAT_OP(simde_wasm_v128_load8_splat, 8);
  5301. break;
  5302. }
  5303. case SIMD_v128_load16_splat:
  5304. {
  5305. SIMD_LOAD_SPLAT_OP(simde_wasm_v128_load16_splat, 16);
  5306. break;
  5307. }
  5308. case SIMD_v128_load32_splat:
  5309. {
  5310. SIMD_LOAD_SPLAT_OP(simde_wasm_v128_load32_splat, 32);
  5311. break;
  5312. }
  5313. case SIMD_v128_load64_splat:
  5314. {
  5315. SIMD_LOAD_SPLAT_OP(simde_wasm_v128_load64_splat, 64);
  5316. break;
  5317. }
  5318. case SIMD_v128_store:
  5319. {
  5320. uint32 offset, addr;
  5321. offset = read_uint32(frame_ip);
  5322. V128 data = POP_V128();
  5323. addr = POP_I32();
  5324. CHECK_MEMORY_OVERFLOW(16);
  5325. STORE_V128(maddr, data);
  5326. break;
  5327. }
  5328. /* Basic */
  5329. case SIMD_v128_const:
  5330. {
  5331. uint8 *orig_ip = frame_ip;
  5332. frame_ip += sizeof(V128);
  5333. addr_ret = GET_OFFSET();
  5334. PUT_V128_TO_ADDR(frame_lp + addr_ret, *(V128 *)orig_ip);
  5335. break;
  5336. }
  5337. /* TODO: Add a faster SIMD implementation */
  5338. case SIMD_v8x16_shuffle:
  5339. {
  5340. V128 indices;
  5341. bh_memcpy_s(&indices, sizeof(V128), frame_ip,
  5342. sizeof(V128));
  5343. frame_ip += sizeof(V128);
  5344. V128 v2 = POP_V128();
  5345. V128 v1 = POP_V128();
  5346. addr_ret = GET_OFFSET();
  5347. V128 result;
  5348. for (int i = 0; i < 16; i++) {
  5349. uint8_t index = indices.i8x16[i];
  5350. if (index < 16) {
  5351. result.i8x16[i] = v1.i8x16[index];
  5352. }
  5353. else {
  5354. result.i8x16[i] = v2.i8x16[index - 16];
  5355. }
  5356. }
  5357. PUT_V128_TO_ADDR(frame_lp + addr_ret, result);
  5358. break;
  5359. }
  5360. case SIMD_v8x16_swizzle:
  5361. {
  5362. V128 v2 = POP_V128();
  5363. V128 v1 = POP_V128();
  5364. addr_ret = GET_OFFSET();
  5365. simde_v128_t simde_result = simde_wasm_i8x16_swizzle(
  5366. SIMD_V128_TO_SIMDE_V128(v1),
  5367. SIMD_V128_TO_SIMDE_V128(v2));
  5368. V128 result;
  5369. SIMDE_V128_TO_SIMD_V128(simde_result, result);
  5370. PUT_V128_TO_ADDR(frame_lp + addr_ret, result);
  5371. break;
  5372. }
  5373. /* Splat */
  5374. #define SIMD_SPLAT_OP(simde_func, pop_func, val_type) \
  5375. do { \
  5376. val_type v = pop_func(); \
  5377. addr_ret = GET_OFFSET(); \
  5378. \
  5379. simde_v128_t simde_result = simde_func(v); \
  5380. \
  5381. V128 result; \
  5382. SIMDE_V128_TO_SIMD_V128(simde_result, result); \
  5383. \
  5384. PUT_V128_TO_ADDR(frame_lp + addr_ret, result); \
  5385. } while (0)
  5386. #define SIMD_SPLAT_OP_I32(simde_func) SIMD_SPLAT_OP(simde_func, POP_I32, uint32)
  5387. #define SIMD_SPLAT_OP_I64(simde_func) SIMD_SPLAT_OP(simde_func, POP_I64, uint64)
  5388. #define SIMD_SPLAT_OP_F32(simde_func) \
  5389. SIMD_SPLAT_OP(simde_func, POP_F32, float32)
  5390. #define SIMD_SPLAT_OP_F64(simde_func) \
  5391. SIMD_SPLAT_OP(simde_func, POP_F64, float64)
  5392. case SIMD_i8x16_splat:
  5393. {
  5394. val = POP_I32();
  5395. addr_ret = GET_OFFSET();
  5396. simde_v128_t simde_result = simde_wasm_i8x16_splat(val);
  5397. V128 result;
  5398. SIMDE_V128_TO_SIMD_V128(simde_result, result);
  5399. PUT_V128_TO_ADDR(frame_lp + addr_ret, result);
  5400. break;
  5401. }
  5402. case SIMD_i16x8_splat:
  5403. {
  5404. SIMD_SPLAT_OP_I32(simde_wasm_i16x8_splat);
  5405. break;
  5406. }
  5407. case SIMD_i32x4_splat:
  5408. {
  5409. SIMD_SPLAT_OP_I32(simde_wasm_i32x4_splat);
  5410. break;
  5411. }
  5412. case SIMD_i64x2_splat:
  5413. {
  5414. SIMD_SPLAT_OP_I64(simde_wasm_i64x2_splat);
  5415. break;
  5416. }
  5417. case SIMD_f32x4_splat:
  5418. {
  5419. SIMD_SPLAT_OP_F32(simde_wasm_f32x4_splat);
  5420. break;
  5421. }
  5422. case SIMD_f64x2_splat:
  5423. {
  5424. SIMD_SPLAT_OP_F64(simde_wasm_f64x2_splat);
  5425. break;
  5426. }
  5427. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  5428. #define SIMD_LANE_HANDLE_UNALIGNED_ACCESS()
  5429. #else
  5430. #define SIMD_LANE_HANDLE_UNALIGNED_ACCESS() (void)*frame_ip++
  5431. #endif /* WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 */
  5432. #define SIMD_EXTRACT_LANE_OP(register, return_type, push_elem) \
  5433. do { \
  5434. uint8 lane = *frame_ip++; \
  5435. SIMD_LANE_HANDLE_UNALIGNED_ACCESS(); \
  5436. V128 v = POP_V128(); \
  5437. push_elem((return_type)(v.register[lane])); \
  5438. } while (0)
  5439. #define SIMD_REPLACE_LANE_OP(register, return_type, pop_elem) \
  5440. do { \
  5441. uint8 lane = *frame_ip++; \
  5442. SIMD_LANE_HANDLE_UNALIGNED_ACCESS(); \
  5443. return_type replacement = pop_elem(); \
  5444. V128 v = POP_V128(); \
  5445. v.register[lane] = replacement; \
  5446. addr_ret = GET_OFFSET(); \
  5447. PUT_V128_TO_ADDR(frame_lp + addr_ret, v); \
  5448. } while (0)
  5449. case SIMD_i8x16_extract_lane_s:
  5450. {
  5451. SIMD_EXTRACT_LANE_OP(i8x16, int8, PUSH_I32);
  5452. break;
  5453. }
  5454. case SIMD_i8x16_extract_lane_u:
  5455. {
  5456. SIMD_EXTRACT_LANE_OP(i8x16, uint8, PUSH_I32);
  5457. break;
  5458. }
  5459. case SIMD_i8x16_replace_lane:
  5460. {
  5461. SIMD_REPLACE_LANE_OP(i8x16, int8, POP_I32);
  5462. break;
  5463. }
  5464. case SIMD_i16x8_extract_lane_s:
  5465. {
  5466. SIMD_EXTRACT_LANE_OP(i16x8, int16, PUSH_I32);
  5467. break;
  5468. }
  5469. case SIMD_i16x8_extract_lane_u:
  5470. {
  5471. SIMD_EXTRACT_LANE_OP(i16x8, uint16, PUSH_I32);
  5472. break;
  5473. }
  5474. case SIMD_i16x8_replace_lane:
  5475. {
  5476. SIMD_REPLACE_LANE_OP(i16x8, int16, POP_I32);
  5477. break;
  5478. }
  5479. case SIMD_i32x4_extract_lane:
  5480. {
  5481. SIMD_EXTRACT_LANE_OP(i32x4, int32, PUSH_I32);
  5482. break;
  5483. }
  5484. case SIMD_i32x4_replace_lane:
  5485. {
  5486. SIMD_REPLACE_LANE_OP(i32x4, int32, POP_I32);
  5487. break;
  5488. }
  5489. case SIMD_i64x2_extract_lane:
  5490. {
  5491. SIMD_EXTRACT_LANE_OP(i64x2, int64, PUSH_I64);
  5492. break;
  5493. }
  5494. case SIMD_i64x2_replace_lane:
  5495. {
  5496. SIMD_REPLACE_LANE_OP(i64x2, int64, POP_I64);
  5497. break;
  5498. }
  5499. case SIMD_f32x4_extract_lane:
  5500. {
  5501. SIMD_EXTRACT_LANE_OP(f32x4, float32, PUSH_F32);
  5502. break;
  5503. }
  5504. case SIMD_f32x4_replace_lane:
  5505. {
  5506. SIMD_REPLACE_LANE_OP(f32x4, float32, POP_F32);
  5507. break;
  5508. }
  5509. case SIMD_f64x2_extract_lane:
  5510. {
  5511. SIMD_EXTRACT_LANE_OP(f64x2, float64, PUSH_F64);
  5512. break;
  5513. }
  5514. case SIMD_f64x2_replace_lane:
  5515. {
  5516. SIMD_REPLACE_LANE_OP(f64x2, float64, POP_F64);
  5517. break;
  5518. }
  5519. #define SIMD_DOUBLE_OP(simde_func) \
  5520. do { \
  5521. V128 v2 = POP_V128(); \
  5522. V128 v1 = POP_V128(); \
  5523. addr_ret = GET_OFFSET(); \
  5524. \
  5525. simde_v128_t simde_result = simde_func(SIMD_V128_TO_SIMDE_V128(v1), \
  5526. SIMD_V128_TO_SIMDE_V128(v2)); \
  5527. \
  5528. V128 result; \
  5529. SIMDE_V128_TO_SIMD_V128(simde_result, result); \
  5530. \
  5531. PUT_V128_TO_ADDR(frame_lp + addr_ret, result); \
  5532. } while (0)
  5533. /* i8x16 comparison operations */
  5534. case SIMD_i8x16_eq:
  5535. {
  5536. V128 v2 = POP_V128();
  5537. V128 v1 = POP_V128();
  5538. addr_ret = GET_OFFSET();
  5539. simde_v128_t simde_result =
  5540. simde_wasm_i8x16_eq(SIMD_V128_TO_SIMDE_V128(v1),
  5541. SIMD_V128_TO_SIMDE_V128(v2));
  5542. V128 result;
  5543. SIMDE_V128_TO_SIMD_V128(simde_result, result);
  5544. PUT_V128_TO_ADDR(frame_lp + addr_ret, result);
  5545. break;
  5546. }
  5547. case SIMD_i8x16_ne:
  5548. {
  5549. SIMD_DOUBLE_OP(simde_wasm_i8x16_ne);
  5550. break;
  5551. }
  5552. case SIMD_i8x16_lt_s:
  5553. {
  5554. SIMD_DOUBLE_OP(simde_wasm_i8x16_lt);
  5555. break;
  5556. }
  5557. case SIMD_i8x16_lt_u:
  5558. {
  5559. SIMD_DOUBLE_OP(simde_wasm_u8x16_lt);
  5560. break;
  5561. }
  5562. case SIMD_i8x16_gt_s:
  5563. {
  5564. SIMD_DOUBLE_OP(simde_wasm_i8x16_gt);
  5565. break;
  5566. }
  5567. case SIMD_i8x16_gt_u:
  5568. {
  5569. SIMD_DOUBLE_OP(simde_wasm_u8x16_gt);
  5570. break;
  5571. }
  5572. case SIMD_i8x16_le_s:
  5573. {
  5574. SIMD_DOUBLE_OP(simde_wasm_i8x16_le);
  5575. break;
  5576. }
  5577. case SIMD_i8x16_le_u:
  5578. {
  5579. SIMD_DOUBLE_OP(simde_wasm_u8x16_le);
  5580. break;
  5581. }
  5582. case SIMD_i8x16_ge_s:
  5583. {
  5584. SIMD_DOUBLE_OP(simde_wasm_i8x16_ge);
  5585. break;
  5586. }
  5587. case SIMD_i8x16_ge_u:
  5588. {
  5589. SIMD_DOUBLE_OP(simde_wasm_u8x16_ge);
  5590. break;
  5591. }
  5592. /* i16x8 comparison operations */
  5593. case SIMD_i16x8_eq:
  5594. {
  5595. SIMD_DOUBLE_OP(simde_wasm_i16x8_eq);
  5596. break;
  5597. }
  5598. case SIMD_i16x8_ne:
  5599. {
  5600. SIMD_DOUBLE_OP(simde_wasm_i16x8_ne);
  5601. break;
  5602. }
  5603. case SIMD_i16x8_lt_s:
  5604. {
  5605. SIMD_DOUBLE_OP(simde_wasm_i16x8_lt);
  5606. break;
  5607. }
  5608. case SIMD_i16x8_lt_u:
  5609. {
  5610. SIMD_DOUBLE_OP(simde_wasm_u16x8_lt);
  5611. break;
  5612. }
  5613. case SIMD_i16x8_gt_s:
  5614. {
  5615. SIMD_DOUBLE_OP(simde_wasm_i16x8_gt);
  5616. break;
  5617. }
  5618. case SIMD_i16x8_gt_u:
  5619. {
  5620. SIMD_DOUBLE_OP(simde_wasm_u16x8_gt);
  5621. break;
  5622. }
  5623. case SIMD_i16x8_le_s:
  5624. {
  5625. SIMD_DOUBLE_OP(simde_wasm_i16x8_le);
  5626. break;
  5627. }
  5628. case SIMD_i16x8_le_u:
  5629. {
  5630. SIMD_DOUBLE_OP(simde_wasm_u16x8_le);
  5631. break;
  5632. }
  5633. case SIMD_i16x8_ge_s:
  5634. {
  5635. SIMD_DOUBLE_OP(simde_wasm_i16x8_ge);
  5636. break;
  5637. }
  5638. case SIMD_i16x8_ge_u:
  5639. {
  5640. SIMD_DOUBLE_OP(simde_wasm_u16x8_ge);
  5641. break;
  5642. }
  5643. /* i32x4 comparison operations */
  5644. case SIMD_i32x4_eq:
  5645. {
  5646. SIMD_DOUBLE_OP(simde_wasm_i32x4_eq);
  5647. break;
  5648. }
  5649. case SIMD_i32x4_ne:
  5650. {
  5651. SIMD_DOUBLE_OP(simde_wasm_i32x4_ne);
  5652. break;
  5653. }
  5654. case SIMD_i32x4_lt_s:
  5655. {
  5656. SIMD_DOUBLE_OP(simde_wasm_i32x4_lt);
  5657. break;
  5658. }
  5659. case SIMD_i32x4_lt_u:
  5660. {
  5661. SIMD_DOUBLE_OP(simde_wasm_u32x4_lt);
  5662. break;
  5663. }
  5664. case SIMD_i32x4_gt_s:
  5665. {
  5666. SIMD_DOUBLE_OP(simde_wasm_i32x4_gt);
  5667. break;
  5668. }
  5669. case SIMD_i32x4_gt_u:
  5670. {
  5671. SIMD_DOUBLE_OP(simde_wasm_u32x4_gt);
  5672. break;
  5673. }
  5674. case SIMD_i32x4_le_s:
  5675. {
  5676. SIMD_DOUBLE_OP(simde_wasm_i32x4_le);
  5677. break;
  5678. }
  5679. case SIMD_i32x4_le_u:
  5680. {
  5681. SIMD_DOUBLE_OP(simde_wasm_u32x4_le);
  5682. break;
  5683. }
  5684. case SIMD_i32x4_ge_s:
  5685. {
  5686. SIMD_DOUBLE_OP(simde_wasm_i32x4_ge);
  5687. break;
  5688. }
  5689. case SIMD_i32x4_ge_u:
  5690. {
  5691. SIMD_DOUBLE_OP(simde_wasm_u32x4_ge);
  5692. break;
  5693. }
  5694. /* f32x4 comparison operations */
  5695. case SIMD_f32x4_eq:
  5696. {
  5697. SIMD_DOUBLE_OP(simde_wasm_f32x4_eq);
  5698. break;
  5699. }
  5700. case SIMD_f32x4_ne:
  5701. {
  5702. SIMD_DOUBLE_OP(simde_wasm_f32x4_ne);
  5703. break;
  5704. }
  5705. case SIMD_f32x4_lt:
  5706. {
  5707. SIMD_DOUBLE_OP(simde_wasm_f32x4_lt);
  5708. break;
  5709. }
  5710. case SIMD_f32x4_gt:
  5711. {
  5712. SIMD_DOUBLE_OP(simde_wasm_f32x4_gt);
  5713. break;
  5714. }
  5715. case SIMD_f32x4_le:
  5716. {
  5717. SIMD_DOUBLE_OP(simde_wasm_f32x4_le);
  5718. break;
  5719. }
  5720. case SIMD_f32x4_ge:
  5721. {
  5722. SIMD_DOUBLE_OP(simde_wasm_f32x4_ge);
  5723. break;
  5724. }
  5725. /* f64x2 comparison operations */
  5726. case SIMD_f64x2_eq:
  5727. {
  5728. SIMD_DOUBLE_OP(simde_wasm_f64x2_eq);
  5729. break;
  5730. }
  5731. case SIMD_f64x2_ne:
  5732. {
  5733. SIMD_DOUBLE_OP(simde_wasm_f64x2_ne);
  5734. break;
  5735. }
  5736. case SIMD_f64x2_lt:
  5737. {
  5738. SIMD_DOUBLE_OP(simde_wasm_f64x2_lt);
  5739. break;
  5740. }
  5741. case SIMD_f64x2_gt:
  5742. {
  5743. SIMD_DOUBLE_OP(simde_wasm_f64x2_gt);
  5744. break;
  5745. }
  5746. case SIMD_f64x2_le:
  5747. {
  5748. SIMD_DOUBLE_OP(simde_wasm_f64x2_le);
  5749. break;
  5750. }
  5751. case SIMD_f64x2_ge:
  5752. {
  5753. SIMD_DOUBLE_OP(simde_wasm_f64x2_ge);
  5754. break;
  5755. }
  5756. /* v128 bitwise operations */
  5757. #define SIMD_V128_BITWISE_OP_COMMON(result_expr_0, result_expr_1) \
  5758. do { \
  5759. V128 result; \
  5760. result.i64x2[0] = (result_expr_0); \
  5761. result.i64x2[1] = (result_expr_1); \
  5762. addr_ret = GET_OFFSET(); \
  5763. PUT_V128_TO_ADDR(frame_lp + addr_ret, result); \
  5764. } while (0)
  5765. case SIMD_v128_not:
  5766. {
  5767. V128 value = POP_V128();
  5768. SIMD_V128_BITWISE_OP_COMMON(~value.i64x2[0],
  5769. ~value.i64x2[1]);
  5770. break;
  5771. }
  5772. case SIMD_v128_and:
  5773. {
  5774. V128 v2 = POP_V128();
  5775. V128 v1 = POP_V128();
  5776. SIMD_V128_BITWISE_OP_COMMON(v1.i64x2[0] & v2.i64x2[0],
  5777. v1.i64x2[1] & v2.i64x2[1]);
  5778. break;
  5779. }
  5780. case SIMD_v128_andnot:
  5781. {
  5782. V128 v2 = POP_V128();
  5783. V128 v1 = POP_V128();
  5784. SIMD_V128_BITWISE_OP_COMMON(
  5785. v1.i64x2[0] & (~v2.i64x2[0]),
  5786. v1.i64x2[1] & (~v2.i64x2[1]));
  5787. break;
  5788. }
  5789. case SIMD_v128_or:
  5790. {
  5791. V128 v2 = POP_V128();
  5792. V128 v1 = POP_V128();
  5793. SIMD_V128_BITWISE_OP_COMMON(v1.i64x2[0] | v2.i64x2[0],
  5794. v1.i64x2[1] | v2.i64x2[1]);
  5795. break;
  5796. }
  5797. case SIMD_v128_xor:
  5798. {
  5799. V128 v2 = POP_V128();
  5800. V128 v1 = POP_V128();
  5801. SIMD_V128_BITWISE_OP_COMMON(v1.i64x2[0] ^ v2.i64x2[0],
  5802. v1.i64x2[1] ^ v2.i64x2[1]);
  5803. break;
  5804. }
  5805. case SIMD_v128_bitselect:
  5806. {
  5807. V128 v1 = POP_V128();
  5808. V128 v2 = POP_V128();
  5809. V128 v3 = POP_V128();
  5810. addr_ret = GET_OFFSET();
  5811. simde_v128_t simde_result = simde_wasm_v128_bitselect(
  5812. SIMD_V128_TO_SIMDE_V128(v3),
  5813. SIMD_V128_TO_SIMDE_V128(v2),
  5814. SIMD_V128_TO_SIMDE_V128(v1));
  5815. V128 result;
  5816. SIMDE_V128_TO_SIMD_V128(simde_result, result);
  5817. PUT_V128_TO_ADDR(frame_lp + addr_ret, result);
  5818. break;
  5819. }
  5820. case SIMD_v128_any_true:
  5821. {
  5822. V128 value = POP_V128();
  5823. addr_ret = GET_OFFSET();
  5824. frame_lp[addr_ret] =
  5825. value.i64x2[0] != 0 || value.i64x2[1] != 0;
  5826. break;
  5827. }
  5828. #define SIMD_LOAD_LANE_COMMON(vec, register, lane, width) \
  5829. do { \
  5830. addr_ret = GET_OFFSET(); \
  5831. CHECK_MEMORY_OVERFLOW(width / 8); \
  5832. if (width == 64) { \
  5833. vec.register[lane] = GET_I64_FROM_ADDR((uint32 *)maddr); \
  5834. } \
  5835. else { \
  5836. vec.register[lane] = *(uint##width *)(maddr); \
  5837. } \
  5838. PUT_V128_TO_ADDR(frame_lp + addr_ret, vec); \
  5839. } while (0)
  5840. #define SIMD_LOAD_LANE_OP(register, width) \
  5841. do { \
  5842. uint32 offset, addr; \
  5843. offset = read_uint32(frame_ip); \
  5844. V128 vec = POP_V128(); \
  5845. addr = POP_I32(); \
  5846. int lane = *frame_ip++; \
  5847. SIMD_LANE_HANDLE_UNALIGNED_ACCESS(); \
  5848. SIMD_LOAD_LANE_COMMON(vec, register, lane, width); \
  5849. } while (0)
  5850. case SIMD_v128_load8_lane:
  5851. {
  5852. SIMD_LOAD_LANE_OP(i8x16, 8);
  5853. break;
  5854. }
  5855. case SIMD_v128_load16_lane:
  5856. {
  5857. SIMD_LOAD_LANE_OP(i16x8, 16);
  5858. break;
  5859. }
  5860. case SIMD_v128_load32_lane:
  5861. {
  5862. SIMD_LOAD_LANE_OP(i32x4, 32);
  5863. break;
  5864. }
  5865. case SIMD_v128_load64_lane:
  5866. {
  5867. SIMD_LOAD_LANE_OP(i64x2, 64);
  5868. break;
  5869. }
  5870. #define SIMD_STORE_LANE_OP(register, width) \
  5871. do { \
  5872. uint32 offset, addr; \
  5873. offset = read_uint32(frame_ip); \
  5874. V128 vec = POP_V128(); \
  5875. addr = POP_I32(); \
  5876. int lane = *frame_ip++; \
  5877. SIMD_LANE_HANDLE_UNALIGNED_ACCESS(); \
  5878. CHECK_MEMORY_OVERFLOW(width / 8); \
  5879. if (width == 64) { \
  5880. STORE_I64(maddr, vec.register[lane]); \
  5881. } \
  5882. else { \
  5883. *(uint##width *)(maddr) = vec.register[lane]; \
  5884. } \
  5885. } while (0)
  5886. case SIMD_v128_store8_lane:
  5887. {
  5888. SIMD_STORE_LANE_OP(i8x16, 8);
  5889. break;
  5890. }
  5891. case SIMD_v128_store16_lane:
  5892. {
  5893. SIMD_STORE_LANE_OP(i16x8, 16);
  5894. break;
  5895. }
  5896. case SIMD_v128_store32_lane:
  5897. {
  5898. SIMD_STORE_LANE_OP(i32x4, 32);
  5899. break;
  5900. }
  5901. case SIMD_v128_store64_lane:
  5902. {
  5903. SIMD_STORE_LANE_OP(i64x2, 64);
  5904. break;
  5905. }
  5906. #define SIMD_LOAD_ZERO_OP(register, width) \
  5907. do { \
  5908. uint32 offset, addr; \
  5909. offset = read_uint32(frame_ip); \
  5910. addr = POP_I32(); \
  5911. int32 lane = 0; \
  5912. V128 vec = { 0 }; \
  5913. SIMD_LOAD_LANE_COMMON(vec, register, lane, width); \
  5914. } while (0)
  5915. case SIMD_v128_load32_zero:
  5916. {
  5917. SIMD_LOAD_ZERO_OP(i32x4, 32);
  5918. break;
  5919. }
  5920. case SIMD_v128_load64_zero:
  5921. {
  5922. SIMD_LOAD_ZERO_OP(i64x2, 64);
  5923. break;
  5924. }
  5925. #define SIMD_SINGLE_OP(simde_func) \
  5926. do { \
  5927. V128 v1 = POP_V128(); \
  5928. addr_ret = GET_OFFSET(); \
  5929. \
  5930. simde_v128_t simde_result = simde_func(SIMD_V128_TO_SIMDE_V128(v1)); \
  5931. \
  5932. V128 result; \
  5933. SIMDE_V128_TO_SIMD_V128(simde_result, result); \
  5934. \
  5935. PUT_V128_TO_ADDR(frame_lp + addr_ret, result); \
  5936. } while (0)
  5937. /* Float conversion */
  5938. case SIMD_f32x4_demote_f64x2_zero:
  5939. {
  5940. SIMD_SINGLE_OP(simde_wasm_f32x4_demote_f64x2_zero);
  5941. break;
  5942. }
  5943. case SIMD_f64x2_promote_low_f32x4_zero:
  5944. {
  5945. SIMD_SINGLE_OP(simde_wasm_f64x2_promote_low_f32x4);
  5946. break;
  5947. }
  5948. /* i8x16 operations */
  5949. case SIMD_i8x16_abs:
  5950. {
  5951. SIMD_SINGLE_OP(simde_wasm_i8x16_abs);
  5952. break;
  5953. }
  5954. case SIMD_i8x16_neg:
  5955. {
  5956. SIMD_SINGLE_OP(simde_wasm_i8x16_neg);
  5957. break;
  5958. }
  5959. case SIMD_i8x16_popcnt:
  5960. {
  5961. SIMD_SINGLE_OP(simde_wasm_i8x16_popcnt);
  5962. break;
  5963. }
  5964. case SIMD_i8x16_all_true:
  5965. {
  5966. V128 v1 = POP_V128();
  5967. bool result = simde_wasm_i8x16_all_true(
  5968. SIMD_V128_TO_SIMDE_V128(v1));
  5969. addr_ret = GET_OFFSET();
  5970. frame_lp[addr_ret] = result;
  5971. break;
  5972. }
  5973. case SIMD_i8x16_bitmask:
  5974. {
  5975. V128 v1 = POP_V128();
  5976. uint32_t result = simde_wasm_i8x16_bitmask(
  5977. SIMD_V128_TO_SIMDE_V128(v1));
  5978. addr_ret = GET_OFFSET();
  5979. frame_lp[addr_ret] = result;
  5980. break;
  5981. }
  5982. case SIMD_i8x16_narrow_i16x8_s:
  5983. {
  5984. SIMD_DOUBLE_OP(simde_wasm_i8x16_narrow_i16x8);
  5985. break;
  5986. }
  5987. case SIMD_i8x16_narrow_i16x8_u:
  5988. {
  5989. SIMD_DOUBLE_OP(simde_wasm_u8x16_narrow_i16x8);
  5990. break;
  5991. }
  5992. case SIMD_f32x4_ceil:
  5993. {
  5994. SIMD_SINGLE_OP(simde_wasm_f32x4_ceil);
  5995. break;
  5996. }
  5997. case SIMD_f32x4_floor:
  5998. {
  5999. SIMD_SINGLE_OP(simde_wasm_f32x4_floor);
  6000. break;
  6001. }
  6002. case SIMD_f32x4_trunc:
  6003. {
  6004. SIMD_SINGLE_OP(simde_wasm_f32x4_trunc);
  6005. break;
  6006. }
  6007. case SIMD_f32x4_nearest:
  6008. {
  6009. SIMD_SINGLE_OP(simde_wasm_f32x4_nearest);
  6010. break;
  6011. }
  6012. #define SIMD_LANE_SHIFT(simde_func) \
  6013. do { \
  6014. int32 c = POP_I32(); \
  6015. V128 v1 = POP_V128(); \
  6016. addr_ret = GET_OFFSET(); \
  6017. \
  6018. simde_v128_t simde_result = \
  6019. simde_func(SIMD_V128_TO_SIMDE_V128(v1), c); \
  6020. \
  6021. V128 result; \
  6022. SIMDE_V128_TO_SIMD_V128(simde_result, result); \
  6023. \
  6024. PUT_V128_TO_ADDR(frame_lp + addr_ret, result); \
  6025. } while (0)
  6026. case SIMD_i8x16_shl:
  6027. {
  6028. SIMD_LANE_SHIFT(simde_wasm_i8x16_shl);
  6029. break;
  6030. }
  6031. case SIMD_i8x16_shr_s:
  6032. {
  6033. SIMD_LANE_SHIFT(simde_wasm_i8x16_shr);
  6034. break;
  6035. }
  6036. case SIMD_i8x16_shr_u:
  6037. {
  6038. SIMD_LANE_SHIFT(simde_wasm_u8x16_shr);
  6039. break;
  6040. }
  6041. case SIMD_i8x16_add:
  6042. {
  6043. SIMD_DOUBLE_OP(simde_wasm_i8x16_add);
  6044. break;
  6045. }
  6046. case SIMD_i8x16_add_sat_s:
  6047. {
  6048. SIMD_DOUBLE_OP(simde_wasm_i8x16_add_sat);
  6049. break;
  6050. }
  6051. case SIMD_i8x16_add_sat_u:
  6052. {
  6053. SIMD_DOUBLE_OP(simde_wasm_u8x16_add_sat);
  6054. break;
  6055. }
  6056. case SIMD_i8x16_sub:
  6057. {
  6058. SIMD_DOUBLE_OP(simde_wasm_i8x16_sub);
  6059. break;
  6060. }
  6061. case SIMD_i8x16_sub_sat_s:
  6062. {
  6063. SIMD_DOUBLE_OP(simde_wasm_i8x16_sub_sat);
  6064. break;
  6065. }
  6066. case SIMD_i8x16_sub_sat_u:
  6067. {
  6068. SIMD_DOUBLE_OP(simde_wasm_u8x16_sub_sat);
  6069. break;
  6070. }
  6071. case SIMD_f64x2_ceil:
  6072. {
  6073. SIMD_SINGLE_OP(simde_wasm_f64x2_ceil);
  6074. break;
  6075. }
  6076. case SIMD_f64x2_floor:
  6077. {
  6078. SIMD_SINGLE_OP(simde_wasm_f64x2_floor);
  6079. break;
  6080. }
  6081. case SIMD_i8x16_min_s:
  6082. {
  6083. SIMD_DOUBLE_OP(simde_wasm_i8x16_min);
  6084. break;
  6085. }
  6086. case SIMD_i8x16_min_u:
  6087. {
  6088. SIMD_DOUBLE_OP(simde_wasm_u8x16_min);
  6089. break;
  6090. }
  6091. case SIMD_i8x16_max_s:
  6092. {
  6093. SIMD_DOUBLE_OP(simde_wasm_i8x16_max);
  6094. break;
  6095. }
  6096. case SIMD_i8x16_max_u:
  6097. {
  6098. SIMD_DOUBLE_OP(simde_wasm_u8x16_max);
  6099. break;
  6100. }
  6101. case SIMD_f64x2_trunc:
  6102. {
  6103. SIMD_SINGLE_OP(simde_wasm_f64x2_trunc);
  6104. break;
  6105. }
  6106. case SIMD_i8x16_avgr_u:
  6107. {
  6108. SIMD_DOUBLE_OP(simde_wasm_u8x16_avgr);
  6109. break;
  6110. }
  6111. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  6112. {
  6113. SIMD_SINGLE_OP(simde_wasm_i16x8_extadd_pairwise_i8x16);
  6114. break;
  6115. }
  6116. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  6117. {
  6118. SIMD_SINGLE_OP(simde_wasm_u16x8_extadd_pairwise_u8x16);
  6119. break;
  6120. }
  6121. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  6122. {
  6123. SIMD_SINGLE_OP(simde_wasm_i32x4_extadd_pairwise_i16x8);
  6124. break;
  6125. }
  6126. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  6127. {
  6128. SIMD_SINGLE_OP(simde_wasm_u32x4_extadd_pairwise_u16x8);
  6129. break;
  6130. }
  6131. /* i16x8 operations */
  6132. case SIMD_i16x8_abs:
  6133. {
  6134. SIMD_SINGLE_OP(simde_wasm_i16x8_abs);
  6135. break;
  6136. }
  6137. case SIMD_i16x8_neg:
  6138. {
  6139. SIMD_SINGLE_OP(simde_wasm_i16x8_neg);
  6140. break;
  6141. }
  6142. case SIMD_i16x8_q15mulr_sat_s:
  6143. {
  6144. SIMD_DOUBLE_OP(simde_wasm_i16x8_q15mulr_sat);
  6145. break;
  6146. }
  6147. case SIMD_i16x8_all_true:
  6148. {
  6149. V128 v1 = POP_V128();
  6150. bool result = simde_wasm_i16x8_all_true(
  6151. SIMD_V128_TO_SIMDE_V128(v1));
  6152. addr_ret = GET_OFFSET();
  6153. frame_lp[addr_ret] = result;
  6154. break;
  6155. }
  6156. case SIMD_i16x8_bitmask:
  6157. {
  6158. V128 v1 = POP_V128();
  6159. uint32_t result = simde_wasm_i16x8_bitmask(
  6160. SIMD_V128_TO_SIMDE_V128(v1));
  6161. addr_ret = GET_OFFSET();
  6162. frame_lp[addr_ret] = result;
  6163. break;
  6164. }
  6165. case SIMD_i16x8_narrow_i32x4_s:
  6166. {
  6167. SIMD_DOUBLE_OP(simde_wasm_i16x8_narrow_i32x4);
  6168. break;
  6169. }
  6170. case SIMD_i16x8_narrow_i32x4_u:
  6171. {
  6172. SIMD_DOUBLE_OP(simde_wasm_u16x8_narrow_i32x4);
  6173. break;
  6174. }
  6175. case SIMD_i16x8_extend_low_i8x16_s:
  6176. {
  6177. SIMD_SINGLE_OP(simde_wasm_i16x8_extend_low_i8x16);
  6178. break;
  6179. }
  6180. case SIMD_i16x8_extend_high_i8x16_s:
  6181. {
  6182. SIMD_SINGLE_OP(simde_wasm_i16x8_extend_high_i8x16);
  6183. break;
  6184. }
  6185. case SIMD_i16x8_extend_low_i8x16_u:
  6186. {
  6187. SIMD_SINGLE_OP(simde_wasm_u16x8_extend_low_u8x16);
  6188. break;
  6189. }
  6190. case SIMD_i16x8_extend_high_i8x16_u:
  6191. {
  6192. SIMD_SINGLE_OP(simde_wasm_u16x8_extend_high_u8x16);
  6193. break;
  6194. }
  6195. case SIMD_i16x8_shl:
  6196. {
  6197. SIMD_LANE_SHIFT(simde_wasm_i16x8_shl);
  6198. break;
  6199. }
  6200. case SIMD_i16x8_shr_s:
  6201. {
  6202. SIMD_LANE_SHIFT(simde_wasm_i16x8_shr);
  6203. break;
  6204. }
  6205. case SIMD_i16x8_shr_u:
  6206. {
  6207. SIMD_LANE_SHIFT(simde_wasm_u16x8_shr);
  6208. break;
  6209. }
  6210. case SIMD_i16x8_add:
  6211. {
  6212. SIMD_DOUBLE_OP(simde_wasm_i16x8_add);
  6213. break;
  6214. }
  6215. case SIMD_i16x8_add_sat_s:
  6216. {
  6217. SIMD_DOUBLE_OP(simde_wasm_i16x8_add_sat);
  6218. break;
  6219. }
  6220. case SIMD_i16x8_add_sat_u:
  6221. {
  6222. SIMD_DOUBLE_OP(simde_wasm_u16x8_add_sat);
  6223. break;
  6224. }
  6225. case SIMD_i16x8_sub:
  6226. {
  6227. SIMD_DOUBLE_OP(simde_wasm_i16x8_sub);
  6228. break;
  6229. }
  6230. case SIMD_i16x8_sub_sat_s:
  6231. {
  6232. SIMD_DOUBLE_OP(simde_wasm_i16x8_sub_sat);
  6233. break;
  6234. }
  6235. case SIMD_i16x8_sub_sat_u:
  6236. {
  6237. SIMD_DOUBLE_OP(simde_wasm_u16x8_sub_sat);
  6238. break;
  6239. }
  6240. case SIMD_f64x2_nearest:
  6241. {
  6242. SIMD_SINGLE_OP(simde_wasm_f64x2_nearest);
  6243. break;
  6244. }
  6245. case SIMD_i16x8_mul:
  6246. {
  6247. SIMD_DOUBLE_OP(simde_wasm_i16x8_mul);
  6248. break;
  6249. }
  6250. case SIMD_i16x8_min_s:
  6251. {
  6252. SIMD_DOUBLE_OP(simde_wasm_i16x8_min);
  6253. break;
  6254. }
  6255. case SIMD_i16x8_min_u:
  6256. {
  6257. SIMD_DOUBLE_OP(simde_wasm_u16x8_min);
  6258. break;
  6259. }
  6260. case SIMD_i16x8_max_s:
  6261. {
  6262. SIMD_DOUBLE_OP(simde_wasm_i16x8_max);
  6263. break;
  6264. }
  6265. case SIMD_i16x8_max_u:
  6266. {
  6267. SIMD_DOUBLE_OP(simde_wasm_u16x8_max);
  6268. break;
  6269. }
  6270. case SIMD_i16x8_avgr_u:
  6271. {
  6272. SIMD_DOUBLE_OP(simde_wasm_u16x8_avgr);
  6273. break;
  6274. }
  6275. case SIMD_i16x8_extmul_low_i8x16_s:
  6276. {
  6277. SIMD_DOUBLE_OP(simde_wasm_i16x8_extmul_low_i8x16);
  6278. break;
  6279. }
  6280. case SIMD_i16x8_extmul_high_i8x16_s:
  6281. {
  6282. SIMD_DOUBLE_OP(simde_wasm_i16x8_extmul_high_i8x16);
  6283. break;
  6284. }
  6285. case SIMD_i16x8_extmul_low_i8x16_u:
  6286. {
  6287. SIMD_DOUBLE_OP(simde_wasm_u16x8_extmul_low_u8x16);
  6288. break;
  6289. }
  6290. case SIMD_i16x8_extmul_high_i8x16_u:
  6291. {
  6292. SIMD_DOUBLE_OP(simde_wasm_u16x8_extmul_high_u8x16);
  6293. break;
  6294. }
  6295. /* i32x4 operations */
  6296. case SIMD_i32x4_abs:
  6297. {
  6298. SIMD_SINGLE_OP(simde_wasm_i32x4_abs);
  6299. break;
  6300. }
  6301. case SIMD_i32x4_neg:
  6302. {
  6303. SIMD_SINGLE_OP(simde_wasm_i32x4_neg);
  6304. break;
  6305. }
  6306. case SIMD_i32x4_all_true:
  6307. {
  6308. V128 v1 = POP_V128();
  6309. bool result = simde_wasm_i32x4_all_true(
  6310. SIMD_V128_TO_SIMDE_V128(v1));
  6311. addr_ret = GET_OFFSET();
  6312. frame_lp[addr_ret] = result;
  6313. break;
  6314. }
  6315. case SIMD_i32x4_bitmask:
  6316. {
  6317. V128 v1 = POP_V128();
  6318. uint32_t result = simde_wasm_i32x4_bitmask(
  6319. SIMD_V128_TO_SIMDE_V128(v1));
  6320. addr_ret = GET_OFFSET();
  6321. frame_lp[addr_ret] = result;
  6322. break;
  6323. }
  6324. case SIMD_i32x4_extend_low_i16x8_s:
  6325. {
  6326. SIMD_SINGLE_OP(simde_wasm_i32x4_extend_low_i16x8);
  6327. break;
  6328. }
  6329. case SIMD_i32x4_extend_high_i16x8_s:
  6330. {
  6331. SIMD_SINGLE_OP(simde_wasm_i32x4_extend_high_i16x8);
  6332. break;
  6333. }
  6334. case SIMD_i32x4_extend_low_i16x8_u:
  6335. {
  6336. SIMD_SINGLE_OP(simde_wasm_u32x4_extend_low_u16x8);
  6337. break;
  6338. }
  6339. case SIMD_i32x4_extend_high_i16x8_u:
  6340. {
  6341. SIMD_SINGLE_OP(simde_wasm_u32x4_extend_high_u16x8);
  6342. break;
  6343. }
  6344. case SIMD_i32x4_shl:
  6345. {
  6346. SIMD_LANE_SHIFT(simde_wasm_i32x4_shl);
  6347. break;
  6348. }
  6349. case SIMD_i32x4_shr_s:
  6350. {
  6351. SIMD_LANE_SHIFT(simde_wasm_i32x4_shr);
  6352. break;
  6353. }
  6354. case SIMD_i32x4_shr_u:
  6355. {
  6356. SIMD_LANE_SHIFT(simde_wasm_u32x4_shr);
  6357. break;
  6358. }
  6359. case SIMD_i32x4_add:
  6360. {
  6361. SIMD_DOUBLE_OP(simde_wasm_i32x4_add);
  6362. break;
  6363. }
  6364. case SIMD_i32x4_sub:
  6365. {
  6366. SIMD_DOUBLE_OP(simde_wasm_i32x4_sub);
  6367. break;
  6368. }
  6369. case SIMD_i32x4_mul:
  6370. {
  6371. SIMD_DOUBLE_OP(simde_wasm_i32x4_mul);
  6372. break;
  6373. }
  6374. case SIMD_i32x4_min_s:
  6375. {
  6376. SIMD_DOUBLE_OP(simde_wasm_i32x4_min);
  6377. break;
  6378. }
  6379. case SIMD_i32x4_min_u:
  6380. {
  6381. SIMD_DOUBLE_OP(simde_wasm_u32x4_min);
  6382. break;
  6383. }
  6384. case SIMD_i32x4_max_s:
  6385. {
  6386. SIMD_DOUBLE_OP(simde_wasm_i32x4_max);
  6387. break;
  6388. }
  6389. case SIMD_i32x4_max_u:
  6390. {
  6391. SIMD_DOUBLE_OP(simde_wasm_u32x4_max);
  6392. break;
  6393. }
  6394. case SIMD_i32x4_dot_i16x8_s:
  6395. {
  6396. SIMD_DOUBLE_OP(simde_wasm_i32x4_dot_i16x8);
  6397. break;
  6398. }
  6399. case SIMD_i32x4_extmul_low_i16x8_s:
  6400. {
  6401. SIMD_DOUBLE_OP(simde_wasm_i32x4_extmul_low_i16x8);
  6402. break;
  6403. }
  6404. case SIMD_i32x4_extmul_high_i16x8_s:
  6405. {
  6406. SIMD_DOUBLE_OP(simde_wasm_i32x4_extmul_high_i16x8);
  6407. break;
  6408. }
  6409. case SIMD_i32x4_extmul_low_i16x8_u:
  6410. {
  6411. SIMD_DOUBLE_OP(simde_wasm_u32x4_extmul_low_u16x8);
  6412. break;
  6413. }
  6414. case SIMD_i32x4_extmul_high_i16x8_u:
  6415. {
  6416. SIMD_DOUBLE_OP(simde_wasm_u32x4_extmul_high_u16x8);
  6417. break;
  6418. }
  6419. /* i64x2 operations */
  6420. case SIMD_i64x2_abs:
  6421. {
  6422. SIMD_SINGLE_OP(simde_wasm_i64x2_abs);
  6423. break;
  6424. }
  6425. case SIMD_i64x2_neg:
  6426. {
  6427. SIMD_SINGLE_OP(simde_wasm_i64x2_neg);
  6428. break;
  6429. }
  6430. case SIMD_i64x2_all_true:
  6431. {
  6432. V128 v1 = POP_V128();
  6433. bool result = simde_wasm_i64x2_all_true(
  6434. SIMD_V128_TO_SIMDE_V128(v1));
  6435. addr_ret = GET_OFFSET();
  6436. frame_lp[addr_ret] = result;
  6437. break;
  6438. }
  6439. case SIMD_i64x2_bitmask:
  6440. {
  6441. V128 v1 = POP_V128();
  6442. uint32_t result = simde_wasm_i64x2_bitmask(
  6443. SIMD_V128_TO_SIMDE_V128(v1));
  6444. addr_ret = GET_OFFSET();
  6445. frame_lp[addr_ret] = result;
  6446. break;
  6447. }
  6448. case SIMD_i64x2_extend_low_i32x4_s:
  6449. {
  6450. SIMD_SINGLE_OP(simde_wasm_i64x2_extend_low_i32x4);
  6451. break;
  6452. }
  6453. case SIMD_i64x2_extend_high_i32x4_s:
  6454. {
  6455. SIMD_SINGLE_OP(simde_wasm_i64x2_extend_high_i32x4);
  6456. break;
  6457. }
  6458. case SIMD_i64x2_extend_low_i32x4_u:
  6459. {
  6460. SIMD_SINGLE_OP(simde_wasm_u64x2_extend_low_u32x4);
  6461. break;
  6462. }
  6463. case SIMD_i64x2_extend_high_i32x4_u:
  6464. {
  6465. SIMD_SINGLE_OP(simde_wasm_u64x2_extend_high_u32x4);
  6466. break;
  6467. }
  6468. case SIMD_i64x2_shl:
  6469. {
  6470. SIMD_LANE_SHIFT(simde_wasm_i64x2_shl);
  6471. break;
  6472. }
  6473. case SIMD_i64x2_shr_s:
  6474. {
  6475. SIMD_LANE_SHIFT(simde_wasm_i64x2_shr);
  6476. break;
  6477. }
  6478. case SIMD_i64x2_shr_u:
  6479. {
  6480. SIMD_LANE_SHIFT(simde_wasm_u64x2_shr);
  6481. break;
  6482. }
  6483. case SIMD_i64x2_add:
  6484. {
  6485. SIMD_DOUBLE_OP(simde_wasm_i64x2_add);
  6486. break;
  6487. }
  6488. case SIMD_i64x2_sub:
  6489. {
  6490. SIMD_DOUBLE_OP(simde_wasm_i64x2_sub);
  6491. break;
  6492. }
  6493. case SIMD_i64x2_mul:
  6494. {
  6495. SIMD_DOUBLE_OP(simde_wasm_i64x2_mul);
  6496. break;
  6497. }
  6498. case SIMD_i64x2_eq:
  6499. {
  6500. SIMD_DOUBLE_OP(simde_wasm_i64x2_eq);
  6501. break;
  6502. }
  6503. case SIMD_i64x2_ne:
  6504. {
  6505. SIMD_DOUBLE_OP(simde_wasm_i64x2_ne);
  6506. break;
  6507. }
  6508. case SIMD_i64x2_lt_s:
  6509. {
  6510. SIMD_DOUBLE_OP(simde_wasm_i64x2_lt);
  6511. break;
  6512. }
  6513. case SIMD_i64x2_gt_s:
  6514. {
  6515. SIMD_DOUBLE_OP(simde_wasm_i64x2_gt);
  6516. break;
  6517. }
  6518. case SIMD_i64x2_le_s:
  6519. {
  6520. SIMD_DOUBLE_OP(simde_wasm_i64x2_le);
  6521. break;
  6522. }
  6523. case SIMD_i64x2_ge_s:
  6524. {
  6525. SIMD_DOUBLE_OP(simde_wasm_i64x2_ge);
  6526. break;
  6527. }
  6528. case SIMD_i64x2_extmul_low_i32x4_s:
  6529. {
  6530. SIMD_DOUBLE_OP(simde_wasm_i64x2_extmul_low_i32x4);
  6531. break;
  6532. }
  6533. case SIMD_i64x2_extmul_high_i32x4_s:
  6534. {
  6535. SIMD_DOUBLE_OP(simde_wasm_i64x2_extmul_high_i32x4);
  6536. break;
  6537. }
  6538. case SIMD_i64x2_extmul_low_i32x4_u:
  6539. {
  6540. SIMD_DOUBLE_OP(simde_wasm_u64x2_extmul_low_u32x4);
  6541. break;
  6542. }
  6543. case SIMD_i64x2_extmul_high_i32x4_u:
  6544. {
  6545. SIMD_DOUBLE_OP(simde_wasm_u64x2_extmul_high_u32x4);
  6546. break;
  6547. }
  6548. /* f32x4 opertions */
  6549. case SIMD_f32x4_abs:
  6550. {
  6551. SIMD_SINGLE_OP(simde_wasm_f32x4_abs);
  6552. break;
  6553. }
  6554. case SIMD_f32x4_neg:
  6555. {
  6556. SIMD_SINGLE_OP(simde_wasm_f32x4_neg);
  6557. break;
  6558. }
  6559. case SIMD_f32x4_sqrt:
  6560. {
  6561. SIMD_SINGLE_OP(simde_wasm_f32x4_sqrt);
  6562. break;
  6563. }
  6564. case SIMD_f32x4_add:
  6565. {
  6566. SIMD_DOUBLE_OP(simde_wasm_f32x4_add);
  6567. break;
  6568. }
  6569. case SIMD_f32x4_sub:
  6570. {
  6571. SIMD_DOUBLE_OP(simde_wasm_f32x4_sub);
  6572. break;
  6573. }
  6574. case SIMD_f32x4_mul:
  6575. {
  6576. SIMD_DOUBLE_OP(simde_wasm_f32x4_mul);
  6577. break;
  6578. }
  6579. case SIMD_f32x4_div:
  6580. {
  6581. SIMD_DOUBLE_OP(simde_wasm_f32x4_div);
  6582. break;
  6583. }
  6584. case SIMD_f32x4_min:
  6585. {
  6586. SIMD_DOUBLE_OP(simde_wasm_f32x4_min);
  6587. break;
  6588. }
  6589. case SIMD_f32x4_max:
  6590. {
  6591. SIMD_DOUBLE_OP(simde_wasm_f32x4_max);
  6592. break;
  6593. }
  6594. case SIMD_f32x4_pmin:
  6595. {
  6596. SIMD_DOUBLE_OP(simde_wasm_f32x4_pmin);
  6597. break;
  6598. }
  6599. case SIMD_f32x4_pmax:
  6600. {
  6601. SIMD_DOUBLE_OP(simde_wasm_f32x4_pmax);
  6602. break;
  6603. }
  6604. /* f64x2 operations */
  6605. case SIMD_f64x2_abs:
  6606. {
  6607. SIMD_SINGLE_OP(simde_wasm_f64x2_abs);
  6608. break;
  6609. }
  6610. case SIMD_f64x2_neg:
  6611. {
  6612. SIMD_SINGLE_OP(simde_wasm_f64x2_neg);
  6613. break;
  6614. }
  6615. case SIMD_f64x2_sqrt:
  6616. {
  6617. SIMD_SINGLE_OP(simde_wasm_f64x2_sqrt);
  6618. break;
  6619. }
  6620. case SIMD_f64x2_add:
  6621. {
  6622. SIMD_DOUBLE_OP(simde_wasm_f64x2_add);
  6623. break;
  6624. }
  6625. case SIMD_f64x2_sub:
  6626. {
  6627. SIMD_DOUBLE_OP(simde_wasm_f64x2_sub);
  6628. break;
  6629. }
  6630. case SIMD_f64x2_mul:
  6631. {
  6632. SIMD_DOUBLE_OP(simde_wasm_f64x2_mul);
  6633. break;
  6634. }
  6635. case SIMD_f64x2_div:
  6636. {
  6637. SIMD_DOUBLE_OP(simde_wasm_f64x2_div);
  6638. break;
  6639. }
  6640. case SIMD_f64x2_min:
  6641. {
  6642. SIMD_DOUBLE_OP(simde_wasm_f64x2_min);
  6643. break;
  6644. }
  6645. case SIMD_f64x2_max:
  6646. {
  6647. SIMD_DOUBLE_OP(simde_wasm_f64x2_max);
  6648. break;
  6649. }
  6650. case SIMD_f64x2_pmin:
  6651. {
  6652. SIMD_DOUBLE_OP(simde_wasm_f64x2_pmin);
  6653. break;
  6654. }
  6655. case SIMD_f64x2_pmax:
  6656. {
  6657. SIMD_DOUBLE_OP(simde_wasm_f64x2_pmax);
  6658. break;
  6659. }
  6660. /* Conversion operations */
  6661. case SIMD_i32x4_trunc_sat_f32x4_s:
  6662. {
  6663. SIMD_SINGLE_OP(simde_wasm_i32x4_trunc_sat_f32x4);
  6664. break;
  6665. }
  6666. case SIMD_i32x4_trunc_sat_f32x4_u:
  6667. {
  6668. SIMD_SINGLE_OP(simde_wasm_u32x4_trunc_sat_f32x4);
  6669. break;
  6670. }
  6671. case SIMD_f32x4_convert_i32x4_s:
  6672. {
  6673. SIMD_SINGLE_OP(simde_wasm_f32x4_convert_i32x4);
  6674. break;
  6675. }
  6676. case SIMD_f32x4_convert_i32x4_u:
  6677. {
  6678. SIMD_SINGLE_OP(simde_wasm_f32x4_convert_u32x4);
  6679. break;
  6680. }
  6681. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  6682. {
  6683. SIMD_SINGLE_OP(simde_wasm_i32x4_trunc_sat_f64x2_zero);
  6684. break;
  6685. }
  6686. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  6687. {
  6688. SIMD_SINGLE_OP(simde_wasm_u32x4_trunc_sat_f64x2_zero);
  6689. break;
  6690. }
  6691. case SIMD_f64x2_convert_low_i32x4_s:
  6692. {
  6693. SIMD_SINGLE_OP(simde_wasm_f64x2_convert_low_i32x4);
  6694. break;
  6695. }
  6696. case SIMD_f64x2_convert_low_i32x4_u:
  6697. {
  6698. SIMD_SINGLE_OP(simde_wasm_f64x2_convert_low_u32x4);
  6699. break;
  6700. }
  6701. default:
  6702. wasm_set_exception(module, "unsupported SIMD opcode");
  6703. }
  6704. HANDLE_OP_END();
  6705. }
  6706. #endif
  6707. HANDLE_OP(WASM_OP_CALL)
  6708. {
  6709. #if WASM_ENABLE_THREAD_MGR != 0
  6710. CHECK_SUSPEND_FLAGS();
  6711. #endif
  6712. fidx = read_uint32(frame_ip);
  6713. #if WASM_ENABLE_MULTI_MODULE != 0
  6714. if (fidx >= module->e->function_count) {
  6715. wasm_set_exception(module, "unknown function");
  6716. goto got_exception;
  6717. }
  6718. #endif
  6719. cur_func = module->e->functions + fidx;
  6720. goto call_func_from_interp;
  6721. }
  6722. #if WASM_ENABLE_TAIL_CALL != 0
  6723. HANDLE_OP(WASM_OP_RETURN_CALL)
  6724. {
  6725. #if WASM_ENABLE_THREAD_MGR != 0
  6726. CHECK_SUSPEND_FLAGS();
  6727. #endif
  6728. fidx = read_uint32(frame_ip);
  6729. #if WASM_ENABLE_MULTI_MODULE != 0
  6730. if (fidx >= module->e->function_count) {
  6731. wasm_set_exception(module, "unknown function");
  6732. goto got_exception;
  6733. }
  6734. #endif
  6735. cur_func = module->e->functions + fidx;
  6736. goto call_func_from_return_call;
  6737. }
  6738. #endif /* WASM_ENABLE_TAIL_CALL */
  6739. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  6740. default:
  6741. wasm_set_exception(module, "unsupported opcode");
  6742. goto got_exception;
  6743. }
  6744. #endif
  6745. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  6746. HANDLE_OP(WASM_OP_UNUSED_0x0a)
  6747. #if WASM_ENABLE_TAIL_CALL == 0
  6748. HANDLE_OP(WASM_OP_RETURN_CALL)
  6749. HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT)
  6750. #endif
  6751. #if WASM_ENABLE_SHARED_MEMORY == 0
  6752. HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
  6753. #endif
  6754. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  6755. HANDLE_OP(WASM_OP_TABLE_GET)
  6756. HANDLE_OP(WASM_OP_TABLE_SET)
  6757. HANDLE_OP(WASM_OP_REF_NULL)
  6758. HANDLE_OP(WASM_OP_REF_IS_NULL)
  6759. HANDLE_OP(WASM_OP_REF_FUNC)
  6760. #endif
  6761. #if WASM_ENABLE_GC == 0
  6762. /* SELECT_T is converted to SELECT or SELECT_64 */
  6763. HANDLE_OP(WASM_OP_SELECT_T)
  6764. HANDLE_OP(WASM_OP_CALL_REF)
  6765. HANDLE_OP(WASM_OP_RETURN_CALL_REF)
  6766. HANDLE_OP(WASM_OP_REF_EQ)
  6767. HANDLE_OP(WASM_OP_REF_AS_NON_NULL)
  6768. HANDLE_OP(WASM_OP_BR_ON_NULL)
  6769. HANDLE_OP(WASM_OP_BR_ON_NON_NULL)
  6770. HANDLE_OP(WASM_OP_GC_PREFIX)
  6771. #endif
  6772. #if WASM_ENABLE_EXCE_HANDLING == 0
  6773. /* if exception handling is disabled, these opcodes issue a trap */
  6774. HANDLE_OP(WASM_OP_TRY)
  6775. HANDLE_OP(WASM_OP_CATCH)
  6776. HANDLE_OP(WASM_OP_THROW)
  6777. HANDLE_OP(WASM_OP_RETHROW)
  6778. HANDLE_OP(WASM_OP_DELEGATE)
  6779. HANDLE_OP(WASM_OP_CATCH_ALL)
  6780. HANDLE_OP(EXT_OP_TRY)
  6781. #endif
  6782. HANDLE_OP(WASM_OP_UNUSED_0x16)
  6783. HANDLE_OP(WASM_OP_UNUSED_0x17)
  6784. HANDLE_OP(WASM_OP_UNUSED_0x27)
  6785. /* optimized op code */
  6786. HANDLE_OP(WASM_OP_F32_STORE)
  6787. HANDLE_OP(WASM_OP_F64_STORE)
  6788. HANDLE_OP(WASM_OP_F32_LOAD)
  6789. HANDLE_OP(WASM_OP_F64_LOAD)
  6790. HANDLE_OP(EXT_OP_GET_LOCAL_FAST)
  6791. HANDLE_OP(WASM_OP_GET_LOCAL)
  6792. HANDLE_OP(WASM_OP_DROP)
  6793. HANDLE_OP(WASM_OP_DROP_64)
  6794. HANDLE_OP(WASM_OP_BLOCK)
  6795. HANDLE_OP(WASM_OP_LOOP)
  6796. HANDLE_OP(WASM_OP_END)
  6797. HANDLE_OP(WASM_OP_NOP)
  6798. HANDLE_OP(EXT_OP_BLOCK)
  6799. HANDLE_OP(EXT_OP_LOOP)
  6800. HANDLE_OP(EXT_OP_IF)
  6801. HANDLE_OP(EXT_OP_BR_TABLE_CACHE)
  6802. {
  6803. wasm_set_exception(module, "unsupported opcode");
  6804. goto got_exception;
  6805. }
  6806. #endif
  6807. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  6808. continue;
  6809. #else
  6810. FETCH_OPCODE_AND_DISPATCH();
  6811. #endif
  6812. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  6813. call_func_from_return_call:
  6814. {
  6815. uint32 *lp_base = NULL, *lp = NULL;
  6816. int i;
  6817. if (cur_func->param_cell_num > 0
  6818. && !(lp_base = lp = wasm_runtime_malloc(cur_func->param_cell_num
  6819. * sizeof(uint32)))) {
  6820. wasm_set_exception(module, "allocate memory failed");
  6821. goto got_exception;
  6822. }
  6823. for (i = 0; i < cur_func->param_count; i++) {
  6824. if (cur_func->param_types[i] == VALUE_TYPE_I64
  6825. || cur_func->param_types[i] == VALUE_TYPE_F64) {
  6826. PUT_I64_TO_ADDR(
  6827. lp, GET_OPERAND(uint64, I64,
  6828. 2 * (cur_func->param_count - i - 1)));
  6829. lp += 2;
  6830. }
  6831. else {
  6832. *lp = GET_OPERAND(uint32, I32,
  6833. (2 * (cur_func->param_count - i - 1)));
  6834. lp++;
  6835. }
  6836. }
  6837. frame->lp = frame->operand + cur_func->const_cell_num;
  6838. if (lp - lp_base > 0) {
  6839. word_copy(frame->lp, lp_base, lp - lp_base);
  6840. }
  6841. if (lp_base)
  6842. wasm_runtime_free(lp_base);
  6843. FREE_FRAME(exec_env, frame);
  6844. frame_ip += cur_func->param_count * sizeof(int16);
  6845. wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)prev_frame);
  6846. is_return_call = true;
  6847. goto call_func_from_entry;
  6848. }
  6849. #endif /* WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0 */
  6850. call_func_from_interp:
  6851. {
  6852. /* Only do the copy when it's called from interpreter. */
  6853. WASMInterpFrame *outs_area = wasm_exec_env_wasm_stack_top(exec_env);
  6854. int i;
  6855. #if WASM_ENABLE_MULTI_MODULE != 0
  6856. if (cur_func->is_import_func) {
  6857. outs_area->lp = outs_area->operand
  6858. + (cur_func->import_func_inst
  6859. ? cur_func->import_func_inst->const_cell_num
  6860. : 0);
  6861. }
  6862. else
  6863. #endif
  6864. {
  6865. outs_area->lp = outs_area->operand + cur_func->const_cell_num;
  6866. }
  6867. if ((uint8 *)(outs_area->lp + cur_func->param_cell_num)
  6868. > exec_env->wasm_stack.top_boundary) {
  6869. wasm_set_exception(module, "wasm operand stack overflow");
  6870. goto got_exception;
  6871. }
  6872. for (i = 0; i < cur_func->param_count; i++) {
  6873. if (cur_func->param_types[i] == VALUE_TYPE_V128) {
  6874. PUT_V128_TO_ADDR(
  6875. outs_area->lp,
  6876. GET_OPERAND_V128(2 * (cur_func->param_count - i - 1)));
  6877. outs_area->lp += 4;
  6878. }
  6879. else if (cur_func->param_types[i] == VALUE_TYPE_I64
  6880. || cur_func->param_types[i] == VALUE_TYPE_F64) {
  6881. PUT_I64_TO_ADDR(
  6882. outs_area->lp,
  6883. GET_OPERAND(uint64, I64,
  6884. 2 * (cur_func->param_count - i - 1)));
  6885. outs_area->lp += 2;
  6886. }
  6887. #if WASM_ENABLE_GC != 0
  6888. else if (wasm_is_type_reftype(cur_func->param_types[i])) {
  6889. PUT_REF_TO_ADDR(
  6890. outs_area->lp,
  6891. GET_OPERAND(void *, REF,
  6892. 2 * (cur_func->param_count - i - 1)));
  6893. CLEAR_FRAME_REF(
  6894. *(uint16 *)(frame_ip
  6895. + (2 * (cur_func->param_count - i - 1))));
  6896. outs_area->lp += REF_CELL_NUM;
  6897. }
  6898. #endif
  6899. else {
  6900. *outs_area->lp = GET_OPERAND(
  6901. uint32, I32, (2 * (cur_func->param_count - i - 1)));
  6902. outs_area->lp++;
  6903. }
  6904. }
  6905. frame_ip += cur_func->param_count * sizeof(int16);
  6906. if (cur_func->ret_cell_num != 0) {
  6907. /* Get the first return value's offset. Since loader emit
  6908. * all return values' offset so we must skip remain return
  6909. * values' offsets.
  6910. */
  6911. WASMFuncType *func_type;
  6912. if (cur_func->is_import_func)
  6913. func_type = cur_func->u.func_import->func_type;
  6914. else
  6915. func_type = cur_func->u.func->func_type;
  6916. frame->ret_offset = GET_OFFSET();
  6917. frame_ip += 2 * (func_type->result_count - 1);
  6918. }
  6919. SYNC_ALL_TO_FRAME();
  6920. prev_frame = frame;
  6921. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  6922. is_return_call = false;
  6923. #endif
  6924. }
  6925. call_func_from_entry:
  6926. {
  6927. if (cur_func->is_import_func) {
  6928. #if WASM_ENABLE_MULTI_MODULE != 0
  6929. if (cur_func->import_func_inst) {
  6930. wasm_interp_call_func_import(module, exec_env, cur_func,
  6931. prev_frame);
  6932. }
  6933. else
  6934. #endif
  6935. {
  6936. wasm_interp_call_func_native(module, exec_env, cur_func,
  6937. prev_frame);
  6938. }
  6939. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  6940. if (is_return_call) {
  6941. /* the frame was freed before tail calling and
  6942. the prev_frame was set as exec_env's cur_frame,
  6943. so here we recover context from prev_frame */
  6944. RECOVER_CONTEXT(prev_frame);
  6945. }
  6946. else
  6947. #endif
  6948. {
  6949. prev_frame = frame->prev_frame;
  6950. cur_func = frame->function;
  6951. UPDATE_ALL_FROM_FRAME();
  6952. }
  6953. /* update memory size, no need to update memory ptr as
  6954. it isn't changed in wasm_enlarge_memory */
  6955. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  6956. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  6957. || WASM_ENABLE_BULK_MEMORY != 0
  6958. if (memory)
  6959. linear_mem_size = GET_LINEAR_MEMORY_SIZE(memory);
  6960. #endif
  6961. if (wasm_copy_exception(module, NULL))
  6962. goto got_exception;
  6963. }
  6964. else {
  6965. WASMFunction *cur_wasm_func = cur_func->u.func;
  6966. uint32 cell_num_of_local_stack;
  6967. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  6968. uint32 i, local_cell_idx;
  6969. #endif
  6970. cell_num_of_local_stack = cur_func->param_cell_num
  6971. + cur_func->local_cell_num
  6972. + cur_wasm_func->max_stack_cell_num;
  6973. all_cell_num = cur_func->const_cell_num + cell_num_of_local_stack;
  6974. #if WASM_ENABLE_GC != 0
  6975. /* area of frame_ref */
  6976. all_cell_num += (cell_num_of_local_stack + 3) / 4;
  6977. /* cells occupied by locals, POP_REF should not clear frame_ref for
  6978. * these cells */
  6979. local_cell_num =
  6980. cur_func->param_cell_num + cur_func->local_cell_num;
  6981. #endif
  6982. /* param_cell_num, local_cell_num, const_cell_num and
  6983. max_stack_cell_num are all no larger than UINT16_MAX (checked
  6984. in loader), all_cell_num must be smaller than 1MB */
  6985. bh_assert(all_cell_num < 1 * BH_MB);
  6986. frame_size = wasm_interp_interp_frame_size(all_cell_num);
  6987. if (!(frame = ALLOC_FRAME(exec_env, frame_size, prev_frame))) {
  6988. frame = prev_frame;
  6989. goto got_exception;
  6990. }
  6991. /* Initialize the interpreter context. */
  6992. frame->function = cur_func;
  6993. frame_ip = wasm_get_func_code(cur_func);
  6994. frame_ip_end = wasm_get_func_code_end(cur_func);
  6995. frame_lp = frame->lp =
  6996. frame->operand + cur_wasm_func->const_cell_num;
  6997. /* Initialize the consts */
  6998. if (cur_wasm_func->const_cell_num > 0) {
  6999. word_copy(frame->operand, (uint32 *)cur_wasm_func->consts,
  7000. cur_wasm_func->const_cell_num);
  7001. }
  7002. /* Initialize the local variables */
  7003. memset(frame_lp + cur_func->param_cell_num, 0,
  7004. (uint32)(cur_func->local_cell_num * 4));
  7005. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  7006. /* externref/funcref should be NULL_REF rather than 0 */
  7007. local_cell_idx = cur_func->param_cell_num;
  7008. for (i = 0; i < cur_wasm_func->local_count; i++) {
  7009. if (cur_wasm_func->local_types[i] == VALUE_TYPE_EXTERNREF
  7010. || cur_wasm_func->local_types[i] == VALUE_TYPE_FUNCREF) {
  7011. *(frame_lp + local_cell_idx) = NULL_REF;
  7012. }
  7013. local_cell_idx +=
  7014. wasm_value_type_cell_num(cur_wasm_func->local_types[i]);
  7015. }
  7016. #endif
  7017. #if WASM_ENABLE_GC != 0
  7018. /* frame->ip is used during GC root set enumeration, so we must
  7019. * initialized this field here */
  7020. frame->ip = frame_ip;
  7021. frame_ref = frame->frame_ref =
  7022. (uint8 *)(frame->lp + (uint32)cell_num_of_local_stack);
  7023. init_frame_refs(frame_ref, (uint32)cell_num_of_local_stack,
  7024. cur_func);
  7025. #endif
  7026. wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)frame);
  7027. }
  7028. #if WASM_ENABLE_THREAD_MGR != 0
  7029. CHECK_SUSPEND_FLAGS();
  7030. #endif
  7031. HANDLE_OP_END();
  7032. }
  7033. return_func:
  7034. {
  7035. FREE_FRAME(exec_env, frame);
  7036. wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)prev_frame);
  7037. if (!prev_frame->ip)
  7038. /* Called from native. */
  7039. return;
  7040. RECOVER_CONTEXT(prev_frame);
  7041. #if WASM_ENABLE_GC != 0
  7042. local_cell_num = cur_func->param_cell_num + cur_func->local_cell_num;
  7043. #endif
  7044. HANDLE_OP_END();
  7045. }
  7046. (void)frame_ip_end;
  7047. #if WASM_ENABLE_SHARED_MEMORY != 0
  7048. unaligned_atomic:
  7049. wasm_set_exception(module, "unaligned atomic");
  7050. goto got_exception;
  7051. #endif
  7052. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  7053. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  7054. || WASM_ENABLE_BULK_MEMORY != 0
  7055. out_of_bounds:
  7056. wasm_set_exception(module, "out of bounds memory access");
  7057. #endif
  7058. got_exception:
  7059. SYNC_ALL_TO_FRAME();
  7060. return;
  7061. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  7062. }
  7063. #else
  7064. FETCH_OPCODE_AND_DISPATCH();
  7065. #endif
  7066. }
  7067. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7068. void **
  7069. wasm_interp_get_handle_table(void)
  7070. {
  7071. WASMModuleInstance module;
  7072. memset(&module, 0, sizeof(WASMModuleInstance));
  7073. wasm_interp_call_func_bytecode(&module, NULL, NULL, NULL);
  7074. return global_handle_table;
  7075. }
  7076. #endif
  7077. #if WASM_ENABLE_GC != 0
  7078. bool
  7079. wasm_interp_traverse_gc_rootset(WASMExecEnv *exec_env, void *heap)
  7080. {
  7081. WASMInterpFrame *frame;
  7082. WASMObjectRef gc_obj;
  7083. WASMFunctionInstance *cur_func;
  7084. uint8 *frame_ref;
  7085. uint32 local_cell_num, i;
  7086. frame = wasm_exec_env_get_cur_frame(exec_env);
  7087. for (; frame; frame = frame->prev_frame) {
  7088. frame_ref = frame->frame_ref;
  7089. cur_func = frame->function;
  7090. if (!cur_func)
  7091. continue;
  7092. local_cell_num = cur_func->param_cell_num;
  7093. if (frame->ip)
  7094. local_cell_num +=
  7095. cur_func->local_cell_num + cur_func->u.func->max_stack_cell_num;
  7096. for (i = 0; i < local_cell_num; i++) {
  7097. if (frame_ref[i]) {
  7098. gc_obj = GET_REF_FROM_ADDR(frame->lp + i);
  7099. if (wasm_obj_is_created_from_heap(gc_obj)) {
  7100. if (mem_allocator_add_root((mem_allocator_t)heap, gc_obj)) {
  7101. return false;
  7102. }
  7103. }
  7104. #if UINTPTR_MAX == UINT64_MAX
  7105. bh_assert(frame_ref[i + 1]);
  7106. i++;
  7107. #endif
  7108. }
  7109. }
  7110. }
  7111. return true;
  7112. }
  7113. #endif
  7114. void
  7115. wasm_interp_call_wasm(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
  7116. WASMFunctionInstance *function, uint32 argc,
  7117. uint32 argv[])
  7118. {
  7119. WASMRuntimeFrame *prev_frame = wasm_exec_env_get_cur_frame(exec_env);
  7120. WASMInterpFrame *frame, *outs_area;
  7121. /* Allocate sufficient cells for all kinds of return values. */
  7122. unsigned all_cell_num =
  7123. function->ret_cell_num > 2 ? function->ret_cell_num : 2,
  7124. i;
  7125. /* This frame won't be used by JITed code, so only allocate interp
  7126. frame here. */
  7127. unsigned frame_size;
  7128. #if WASM_ENABLE_GC != 0
  7129. all_cell_num += (all_cell_num + 3) / 4;
  7130. #endif
  7131. frame_size = wasm_interp_interp_frame_size(all_cell_num);
  7132. if (argc < function->param_cell_num) {
  7133. char buf[128];
  7134. snprintf(buf, sizeof(buf),
  7135. "invalid argument count %" PRIu32
  7136. ", must be no smaller than %" PRIu32,
  7137. argc, (uint32)function->param_cell_num);
  7138. wasm_set_exception(module_inst, buf);
  7139. return;
  7140. }
  7141. argc = function->param_cell_num;
  7142. #if defined(OS_ENABLE_HW_BOUND_CHECK) && WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
  7143. /*
  7144. * wasm_runtime_detect_native_stack_overflow is done by
  7145. * call_wasm_with_hw_bound_check.
  7146. */
  7147. #else
  7148. if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
  7149. return;
  7150. }
  7151. #endif
  7152. if (!(frame =
  7153. ALLOC_FRAME(exec_env, frame_size, (WASMInterpFrame *)prev_frame)))
  7154. return;
  7155. outs_area = wasm_exec_env_wasm_stack_top(exec_env);
  7156. frame->function = NULL;
  7157. frame->ip = NULL;
  7158. /* There is no local variable. */
  7159. frame->lp = frame->operand + 0;
  7160. #if WASM_ENABLE_GC != 0
  7161. frame->frame_ref =
  7162. (uint8 *)(frame->lp
  7163. + (function->ret_cell_num > 2 ? function->ret_cell_num : 2));
  7164. #endif
  7165. frame->ret_offset = 0;
  7166. if ((uint8 *)(outs_area->operand + function->const_cell_num + argc)
  7167. > exec_env->wasm_stack.top_boundary) {
  7168. wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
  7169. "wasm operand stack overflow");
  7170. return;
  7171. }
  7172. if (argc > 0)
  7173. word_copy(outs_area->operand + function->const_cell_num, argv, argc);
  7174. wasm_exec_env_set_cur_frame(exec_env, frame);
  7175. #if defined(os_writegsbase)
  7176. {
  7177. WASMMemoryInstance *memory_inst = wasm_get_default_memory(module_inst);
  7178. if (memory_inst)
  7179. /* write base addr of linear memory to GS segment register */
  7180. os_writegsbase(memory_inst->memory_data);
  7181. }
  7182. #endif
  7183. if (function->is_import_func) {
  7184. #if WASM_ENABLE_MULTI_MODULE != 0
  7185. if (function->import_module_inst) {
  7186. LOG_DEBUG("it is a function of a sub module");
  7187. wasm_interp_call_func_import(module_inst, exec_env, function,
  7188. frame);
  7189. }
  7190. else
  7191. #endif
  7192. {
  7193. LOG_DEBUG("it is an native function");
  7194. wasm_interp_call_func_native(module_inst, exec_env, function,
  7195. frame);
  7196. }
  7197. }
  7198. else {
  7199. wasm_interp_call_func_bytecode(module_inst, exec_env, function, frame);
  7200. }
  7201. /* Output the return value to the caller */
  7202. if (!wasm_copy_exception(module_inst, NULL)) {
  7203. for (i = 0; i < function->ret_cell_num; i++)
  7204. argv[i] = *(frame->lp + i);
  7205. }
  7206. else {
  7207. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  7208. if (wasm_interp_create_call_stack(exec_env)) {
  7209. wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
  7210. }
  7211. #endif
  7212. }
  7213. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  7214. FREE_FRAME(exec_env, frame);
  7215. #if WASM_ENABLE_OPCODE_COUNTER != 0
  7216. wasm_interp_dump_op_count();
  7217. #endif
  7218. }