wasm_interp_fast.c 299 KB

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