wasm_interp_fast.c 299 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004
  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 (float64)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 (float64)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_OPT != 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. else if (local_type == VALUE_TYPE_V128) {
  4482. PUT_V128_TO_ADDR((frame_lp + local_offset),
  4483. GET_V128_FROM_ADDR(frame_lp + addr1));
  4484. }
  4485. #if WASM_ENABLE_GC != 0
  4486. else if (wasm_is_type_reftype(local_type)) {
  4487. PUT_REF_TO_ADDR((uint32 *)(frame_lp + local_offset),
  4488. GET_REF_FROM_ADDR(frame_lp + addr1));
  4489. if (opcode == WASM_OP_SET_LOCAL) {
  4490. CLEAR_FRAME_REF(addr1);
  4491. }
  4492. }
  4493. #endif
  4494. else {
  4495. wasm_set_exception(module, "invalid local type");
  4496. goto got_exception;
  4497. }
  4498. HANDLE_OP_END();
  4499. }
  4500. HANDLE_OP(WASM_OP_I32_EXTEND8_S)
  4501. {
  4502. DEF_OP_CONVERT(int32, I32, int8, I32);
  4503. HANDLE_OP_END();
  4504. }
  4505. HANDLE_OP(WASM_OP_I32_EXTEND16_S)
  4506. {
  4507. DEF_OP_CONVERT(int32, I32, int16, I32);
  4508. HANDLE_OP_END();
  4509. }
  4510. HANDLE_OP(WASM_OP_I64_EXTEND8_S)
  4511. {
  4512. DEF_OP_CONVERT(int64, I64, int8, I64);
  4513. HANDLE_OP_END();
  4514. }
  4515. HANDLE_OP(WASM_OP_I64_EXTEND16_S)
  4516. {
  4517. DEF_OP_CONVERT(int64, I64, int16, I64);
  4518. HANDLE_OP_END();
  4519. }
  4520. HANDLE_OP(WASM_OP_I64_EXTEND32_S)
  4521. {
  4522. DEF_OP_CONVERT(int64, I64, int32, I64);
  4523. HANDLE_OP_END();
  4524. }
  4525. HANDLE_OP(WASM_OP_MISC_PREFIX)
  4526. {
  4527. GET_OPCODE();
  4528. switch (opcode) {
  4529. case WASM_OP_I32_TRUNC_SAT_S_F32:
  4530. DEF_OP_TRUNC_SAT_F32(-2147483904.0f, 2147483648.0f,
  4531. true, true);
  4532. break;
  4533. case WASM_OP_I32_TRUNC_SAT_U_F32:
  4534. DEF_OP_TRUNC_SAT_F32(-1.0f, 4294967296.0f, true, false);
  4535. break;
  4536. case WASM_OP_I32_TRUNC_SAT_S_F64:
  4537. DEF_OP_TRUNC_SAT_F64(-2147483649.0, 2147483648.0, true,
  4538. true);
  4539. break;
  4540. case WASM_OP_I32_TRUNC_SAT_U_F64:
  4541. DEF_OP_TRUNC_SAT_F64(-1.0, 4294967296.0, true, false);
  4542. break;
  4543. case WASM_OP_I64_TRUNC_SAT_S_F32:
  4544. DEF_OP_TRUNC_SAT_F32(-9223373136366403584.0f,
  4545. 9223372036854775808.0f, false,
  4546. true);
  4547. break;
  4548. case WASM_OP_I64_TRUNC_SAT_U_F32:
  4549. DEF_OP_TRUNC_SAT_F32(-1.0f, 18446744073709551616.0f,
  4550. false, false);
  4551. break;
  4552. case WASM_OP_I64_TRUNC_SAT_S_F64:
  4553. DEF_OP_TRUNC_SAT_F64(-9223372036854777856.0,
  4554. 9223372036854775808.0, false,
  4555. true);
  4556. break;
  4557. case WASM_OP_I64_TRUNC_SAT_U_F64:
  4558. DEF_OP_TRUNC_SAT_F64(-1.0, 18446744073709551616.0,
  4559. false, false);
  4560. break;
  4561. #if WASM_ENABLE_BULK_MEMORY != 0
  4562. case WASM_OP_MEMORY_INIT:
  4563. {
  4564. uint32 addr, segment;
  4565. uint64 bytes, offset, seg_len;
  4566. uint8 *data;
  4567. segment = read_uint32(frame_ip);
  4568. bytes = (uint64)(uint32)POP_I32();
  4569. offset = (uint64)(uint32)POP_I32();
  4570. addr = POP_I32();
  4571. #if WASM_ENABLE_THREAD_MGR != 0
  4572. linear_mem_size = get_linear_mem_size();
  4573. #endif
  4574. #ifndef OS_ENABLE_HW_BOUND_CHECK
  4575. CHECK_BULK_MEMORY_OVERFLOW(addr, bytes, maddr);
  4576. #else
  4577. #if WASM_ENABLE_SHARED_HEAP != 0
  4578. if (app_addr_in_shared_heap((uint64)(uint32)addr,
  4579. bytes))
  4580. shared_heap_addr_app_to_native((uint64)(uint32)addr,
  4581. maddr);
  4582. else
  4583. #endif
  4584. {
  4585. if ((uint64)(uint32)addr + bytes > linear_mem_size)
  4586. goto out_of_bounds;
  4587. maddr = memory->memory_data + (uint32)addr;
  4588. }
  4589. #endif
  4590. if (bh_bitmap_get_bit(module->e->common.data_dropped,
  4591. segment)) {
  4592. seg_len = 0;
  4593. data = NULL;
  4594. }
  4595. else {
  4596. seg_len =
  4597. (uint64)module->module->data_segments[segment]
  4598. ->data_length;
  4599. data = module->module->data_segments[segment]->data;
  4600. }
  4601. if (offset + bytes > seg_len)
  4602. goto out_of_bounds;
  4603. bh_memcpy_s(maddr, (uint32)(linear_mem_size - addr),
  4604. data + offset, (uint32)bytes);
  4605. break;
  4606. }
  4607. case WASM_OP_DATA_DROP:
  4608. {
  4609. uint32 segment;
  4610. segment = read_uint32(frame_ip);
  4611. bh_bitmap_set_bit(module->e->common.data_dropped,
  4612. segment);
  4613. break;
  4614. }
  4615. #endif /* WASM_ENABLE_BULK_MEMORY */
  4616. #if WASM_ENABLE_BULK_MEMORY_OPT != 0
  4617. case WASM_OP_MEMORY_COPY:
  4618. {
  4619. uint32 dst, src, len;
  4620. uint8 *mdst, *msrc;
  4621. len = POP_I32();
  4622. src = POP_I32();
  4623. dst = POP_I32();
  4624. #if WASM_ENABLE_THREAD_MGR != 0
  4625. linear_mem_size = get_linear_mem_size();
  4626. #endif
  4627. #ifndef OS_ENABLE_HW_BOUND_CHECK
  4628. CHECK_BULK_MEMORY_OVERFLOW(src, len, msrc);
  4629. CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
  4630. #else /* else of OS_ENABLE_HW_BOUND_CHECK */
  4631. #if WASM_ENABLE_SHARED_HEAP != 0
  4632. if (app_addr_in_shared_heap((uint64)src, len))
  4633. shared_heap_addr_app_to_native((uint64)src, msrc);
  4634. else
  4635. #endif
  4636. {
  4637. if ((uint64)(uint32)src + len > linear_mem_size)
  4638. goto out_of_bounds;
  4639. msrc = memory->memory_data + (uint32)src;
  4640. }
  4641. #if WASM_ENABLE_SHARED_HEAP != 0
  4642. if (app_addr_in_shared_heap((uint64)dst, len)) {
  4643. shared_heap_addr_app_to_native((uint64)dst, mdst);
  4644. }
  4645. else
  4646. #endif
  4647. {
  4648. if ((uint64)(uint32)dst + len > linear_mem_size)
  4649. goto out_of_bounds;
  4650. mdst = memory->memory_data + (uint32)dst;
  4651. }
  4652. #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
  4653. /*
  4654. * avoid unnecessary operations
  4655. *
  4656. * since dst and src both are valid indexes in the
  4657. * linear memory, mdst and msrc can't be NULL
  4658. *
  4659. * The spec. converts memory.copy into i32.load8 and
  4660. * i32.store8; the following are runtime-specific
  4661. * optimizations.
  4662. *
  4663. */
  4664. if (len && mdst != msrc) {
  4665. /* allowing the destination and source to overlap */
  4666. memmove(mdst, msrc, len);
  4667. }
  4668. break;
  4669. }
  4670. case WASM_OP_MEMORY_FILL:
  4671. {
  4672. uint32 dst, len;
  4673. uint8 fill_val, *mdst;
  4674. len = POP_I32();
  4675. fill_val = POP_I32();
  4676. dst = POP_I32();
  4677. #if WASM_ENABLE_THREAD_MGR != 0
  4678. linear_mem_size = get_linear_mem_size();
  4679. #endif
  4680. #ifndef OS_ENABLE_HW_BOUND_CHECK
  4681. CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
  4682. #else
  4683. #if WASM_ENABLE_SHARED_HEAP != 0
  4684. if (app_addr_in_shared_heap((uint64)(uint32)dst, len))
  4685. shared_heap_addr_app_to_native((uint64)(uint32)dst,
  4686. mdst);
  4687. else
  4688. #endif
  4689. {
  4690. if ((uint64)(uint32)dst + len > linear_mem_size)
  4691. goto out_of_bounds;
  4692. mdst = memory->memory_data + (uint32)dst;
  4693. }
  4694. #endif
  4695. memset(mdst, fill_val, len);
  4696. break;
  4697. }
  4698. #endif /* WASM_ENABLE_BULK_MEMORY_OPT */
  4699. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  4700. case WASM_OP_TABLE_INIT:
  4701. {
  4702. uint32 tbl_idx, elem_idx;
  4703. uint32 n, s, d;
  4704. WASMTableInstance *tbl_inst;
  4705. table_elem_type_t *table_elems;
  4706. InitializerExpression *tbl_seg_init_values = NULL,
  4707. *init_values;
  4708. uint64 i;
  4709. uint32 tbl_seg_len = 0;
  4710. elem_idx = read_uint32(frame_ip);
  4711. bh_assert(elem_idx < module->module->table_seg_count);
  4712. tbl_idx = read_uint32(frame_ip);
  4713. bh_assert(tbl_idx < module->module->table_count);
  4714. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  4715. n = (uint32)POP_I32();
  4716. s = (uint32)POP_I32();
  4717. d = (uint32)POP_I32();
  4718. if (!bh_bitmap_get_bit(module->e->common.elem_dropped,
  4719. elem_idx)) {
  4720. /* table segment isn't dropped */
  4721. tbl_seg_init_values =
  4722. module->module->table_segments[elem_idx]
  4723. .init_values;
  4724. tbl_seg_len =
  4725. module->module->table_segments[elem_idx]
  4726. .value_count;
  4727. }
  4728. if (offset_len_out_of_bounds(s, n, tbl_seg_len)
  4729. || offset_len_out_of_bounds(d, n,
  4730. tbl_inst->cur_size)) {
  4731. wasm_set_exception(module,
  4732. "out of bounds table access");
  4733. goto got_exception;
  4734. }
  4735. if (!n) {
  4736. break;
  4737. }
  4738. table_elems = tbl_inst->elems + d;
  4739. init_values = tbl_seg_init_values + s;
  4740. #if WASM_ENABLE_GC != 0
  4741. SYNC_ALL_TO_FRAME();
  4742. #endif
  4743. for (i = 0; i < n; i++) {
  4744. /* UINT32_MAX indicates that it is a null ref */
  4745. bh_assert(init_values[i].init_expr_type
  4746. == INIT_EXPR_TYPE_REFNULL_CONST
  4747. || init_values[i].init_expr_type
  4748. == INIT_EXPR_TYPE_FUNCREF_CONST);
  4749. #if WASM_ENABLE_GC == 0
  4750. table_elems[i] = (table_elem_type_t)init_values[i]
  4751. .u.unary.v.ref_index;
  4752. #else
  4753. if (init_values[i].u.unary.v.ref_index
  4754. != UINT32_MAX) {
  4755. if (!(func_obj = wasm_create_func_obj(
  4756. module,
  4757. init_values[i].u.unary.v.ref_index,
  4758. true, NULL, 0))) {
  4759. goto got_exception;
  4760. }
  4761. table_elems[i] = func_obj;
  4762. }
  4763. else {
  4764. table_elems[i] = NULL_REF;
  4765. }
  4766. #endif
  4767. }
  4768. break;
  4769. }
  4770. case WASM_OP_ELEM_DROP:
  4771. {
  4772. uint32 elem_idx = read_uint32(frame_ip);
  4773. bh_assert(elem_idx < module->module->table_seg_count);
  4774. bh_bitmap_set_bit(module->e->common.elem_dropped,
  4775. elem_idx);
  4776. break;
  4777. }
  4778. case WASM_OP_TABLE_COPY:
  4779. {
  4780. uint32 src_tbl_idx, dst_tbl_idx;
  4781. uint32 n, s, d;
  4782. WASMTableInstance *src_tbl_inst, *dst_tbl_inst;
  4783. dst_tbl_idx = read_uint32(frame_ip);
  4784. bh_assert(dst_tbl_idx < module->table_count);
  4785. dst_tbl_inst = wasm_get_table_inst(module, dst_tbl_idx);
  4786. src_tbl_idx = read_uint32(frame_ip);
  4787. bh_assert(src_tbl_idx < module->table_count);
  4788. src_tbl_inst = wasm_get_table_inst(module, src_tbl_idx);
  4789. n = (uint32)POP_I32();
  4790. s = (uint32)POP_I32();
  4791. d = (uint32)POP_I32();
  4792. if (offset_len_out_of_bounds(d, n,
  4793. dst_tbl_inst->cur_size)
  4794. || offset_len_out_of_bounds(
  4795. s, n, src_tbl_inst->cur_size)) {
  4796. wasm_set_exception(module,
  4797. "out of bounds table access");
  4798. goto got_exception;
  4799. }
  4800. /* if s >= d, copy from front to back */
  4801. /* if s < d, copy from back to front */
  4802. /* merge all together */
  4803. bh_memmove_s((uint8 *)dst_tbl_inst
  4804. + offsetof(WASMTableInstance, elems)
  4805. + d * sizeof(table_elem_type_t),
  4806. (uint32)((dst_tbl_inst->cur_size - d)
  4807. * sizeof(table_elem_type_t)),
  4808. (uint8 *)src_tbl_inst
  4809. + offsetof(WASMTableInstance, elems)
  4810. + s * sizeof(table_elem_type_t),
  4811. (uint32)(n * sizeof(table_elem_type_t)));
  4812. break;
  4813. }
  4814. case WASM_OP_TABLE_GROW:
  4815. {
  4816. uint32 tbl_idx, n, orig_tbl_sz;
  4817. WASMTableInstance *tbl_inst;
  4818. table_elem_type_t init_val;
  4819. tbl_idx = read_uint32(frame_ip);
  4820. bh_assert(tbl_idx < module->table_count);
  4821. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  4822. orig_tbl_sz = tbl_inst->cur_size;
  4823. n = POP_I32();
  4824. #if WASM_ENABLE_GC == 0
  4825. init_val = POP_I32();
  4826. #else
  4827. init_val = POP_REF();
  4828. #endif
  4829. if (!wasm_enlarge_table(module, tbl_idx, n, init_val)) {
  4830. PUSH_I32(-1);
  4831. }
  4832. else {
  4833. PUSH_I32(orig_tbl_sz);
  4834. }
  4835. break;
  4836. }
  4837. case WASM_OP_TABLE_SIZE:
  4838. {
  4839. uint32 tbl_idx;
  4840. WASMTableInstance *tbl_inst;
  4841. tbl_idx = read_uint32(frame_ip);
  4842. bh_assert(tbl_idx < module->table_count);
  4843. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  4844. PUSH_I32(tbl_inst->cur_size);
  4845. break;
  4846. }
  4847. case WASM_OP_TABLE_FILL:
  4848. {
  4849. uint32 tbl_idx, n, i;
  4850. WASMTableInstance *tbl_inst;
  4851. table_elem_type_t fill_val;
  4852. tbl_idx = read_uint32(frame_ip);
  4853. bh_assert(tbl_idx < module->table_count);
  4854. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  4855. n = POP_I32();
  4856. #if WASM_ENABLE_GC == 0
  4857. fill_val = POP_I32();
  4858. #else
  4859. fill_val = POP_REF();
  4860. #endif
  4861. i = POP_I32();
  4862. if (offset_len_out_of_bounds(i, n,
  4863. tbl_inst->cur_size)) {
  4864. wasm_set_exception(module,
  4865. "out of bounds table access");
  4866. goto got_exception;
  4867. }
  4868. for (; n != 0; i++, n--) {
  4869. tbl_inst->elems[i] = fill_val;
  4870. }
  4871. break;
  4872. }
  4873. #endif /* WASM_ENABLE_REF_TYPES */
  4874. default:
  4875. wasm_set_exception(module, "unsupported opcode");
  4876. goto got_exception;
  4877. }
  4878. HANDLE_OP_END();
  4879. }
  4880. #if WASM_ENABLE_SHARED_MEMORY != 0
  4881. HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
  4882. {
  4883. uint32 offset = 0, addr;
  4884. GET_OPCODE();
  4885. if (opcode != WASM_OP_ATOMIC_FENCE) {
  4886. offset = read_uint32(frame_ip);
  4887. }
  4888. switch (opcode) {
  4889. case WASM_OP_ATOMIC_NOTIFY:
  4890. {
  4891. uint32 notify_count, ret;
  4892. notify_count = POP_I32();
  4893. addr = POP_I32();
  4894. CHECK_MEMORY_OVERFLOW(4);
  4895. CHECK_ATOMIC_MEMORY_ACCESS(4);
  4896. ret = wasm_runtime_atomic_notify(
  4897. (WASMModuleInstanceCommon *)module, maddr,
  4898. notify_count);
  4899. if (ret == (uint32)-1)
  4900. goto got_exception;
  4901. PUSH_I32(ret);
  4902. break;
  4903. }
  4904. case WASM_OP_ATOMIC_WAIT32:
  4905. {
  4906. uint64 timeout;
  4907. uint32 expect, ret;
  4908. timeout = POP_I64();
  4909. expect = POP_I32();
  4910. addr = POP_I32();
  4911. CHECK_MEMORY_OVERFLOW(4);
  4912. CHECK_ATOMIC_MEMORY_ACCESS(4);
  4913. ret = wasm_runtime_atomic_wait(
  4914. (WASMModuleInstanceCommon *)module, maddr,
  4915. (uint64)expect, timeout, false);
  4916. if (ret == (uint32)-1)
  4917. goto got_exception;
  4918. #if WASM_ENABLE_THREAD_MGR != 0
  4919. CHECK_SUSPEND_FLAGS();
  4920. #endif
  4921. PUSH_I32(ret);
  4922. break;
  4923. }
  4924. case WASM_OP_ATOMIC_WAIT64:
  4925. {
  4926. uint64 timeout, expect;
  4927. uint32 ret;
  4928. timeout = POP_I64();
  4929. expect = POP_I64();
  4930. addr = POP_I32();
  4931. CHECK_MEMORY_OVERFLOW(8);
  4932. CHECK_ATOMIC_MEMORY_ACCESS(8);
  4933. ret = wasm_runtime_atomic_wait(
  4934. (WASMModuleInstanceCommon *)module, maddr, expect,
  4935. timeout, true);
  4936. if (ret == (uint32)-1)
  4937. goto got_exception;
  4938. #if WASM_ENABLE_THREAD_MGR != 0
  4939. CHECK_SUSPEND_FLAGS();
  4940. #endif
  4941. PUSH_I32(ret);
  4942. break;
  4943. }
  4944. case WASM_OP_ATOMIC_FENCE:
  4945. {
  4946. os_atomic_thread_fence(os_memory_order_seq_cst);
  4947. break;
  4948. }
  4949. case WASM_OP_ATOMIC_I32_LOAD:
  4950. case WASM_OP_ATOMIC_I32_LOAD8_U:
  4951. case WASM_OP_ATOMIC_I32_LOAD16_U:
  4952. {
  4953. uint32 readv;
  4954. addr = POP_I32();
  4955. if (opcode == WASM_OP_ATOMIC_I32_LOAD8_U) {
  4956. CHECK_MEMORY_OVERFLOW(1);
  4957. CHECK_ATOMIC_MEMORY_ACCESS(1);
  4958. shared_memory_lock(memory);
  4959. readv = (uint32)(*(uint8 *)maddr);
  4960. shared_memory_unlock(memory);
  4961. }
  4962. else if (opcode == WASM_OP_ATOMIC_I32_LOAD16_U) {
  4963. CHECK_MEMORY_OVERFLOW(2);
  4964. CHECK_ATOMIC_MEMORY_ACCESS(2);
  4965. shared_memory_lock(memory);
  4966. readv = (uint32)LOAD_U16(maddr);
  4967. shared_memory_unlock(memory);
  4968. }
  4969. else {
  4970. CHECK_MEMORY_OVERFLOW(4);
  4971. CHECK_ATOMIC_MEMORY_ACCESS(4);
  4972. shared_memory_lock(memory);
  4973. readv = LOAD_I32(maddr);
  4974. shared_memory_unlock(memory);
  4975. }
  4976. PUSH_I32(readv);
  4977. break;
  4978. }
  4979. case WASM_OP_ATOMIC_I64_LOAD:
  4980. case WASM_OP_ATOMIC_I64_LOAD8_U:
  4981. case WASM_OP_ATOMIC_I64_LOAD16_U:
  4982. case WASM_OP_ATOMIC_I64_LOAD32_U:
  4983. {
  4984. uint64 readv;
  4985. addr = POP_I32();
  4986. if (opcode == WASM_OP_ATOMIC_I64_LOAD8_U) {
  4987. CHECK_MEMORY_OVERFLOW(1);
  4988. CHECK_ATOMIC_MEMORY_ACCESS(1);
  4989. shared_memory_lock(memory);
  4990. readv = (uint64)(*(uint8 *)maddr);
  4991. shared_memory_unlock(memory);
  4992. }
  4993. else if (opcode == WASM_OP_ATOMIC_I64_LOAD16_U) {
  4994. CHECK_MEMORY_OVERFLOW(2);
  4995. CHECK_ATOMIC_MEMORY_ACCESS(2);
  4996. shared_memory_lock(memory);
  4997. readv = (uint64)LOAD_U16(maddr);
  4998. shared_memory_unlock(memory);
  4999. }
  5000. else if (opcode == WASM_OP_ATOMIC_I64_LOAD32_U) {
  5001. CHECK_MEMORY_OVERFLOW(4);
  5002. CHECK_ATOMIC_MEMORY_ACCESS(4);
  5003. shared_memory_lock(memory);
  5004. readv = (uint64)LOAD_U32(maddr);
  5005. shared_memory_unlock(memory);
  5006. }
  5007. else {
  5008. CHECK_MEMORY_OVERFLOW(8);
  5009. CHECK_ATOMIC_MEMORY_ACCESS(8);
  5010. shared_memory_lock(memory);
  5011. readv = LOAD_I64(maddr);
  5012. shared_memory_unlock(memory);
  5013. }
  5014. PUSH_I64(readv);
  5015. break;
  5016. }
  5017. case WASM_OP_ATOMIC_I32_STORE:
  5018. case WASM_OP_ATOMIC_I32_STORE8:
  5019. case WASM_OP_ATOMIC_I32_STORE16:
  5020. {
  5021. uint32 sval;
  5022. sval = (uint32)POP_I32();
  5023. addr = POP_I32();
  5024. if (opcode == WASM_OP_ATOMIC_I32_STORE8) {
  5025. CHECK_MEMORY_OVERFLOW(1);
  5026. CHECK_ATOMIC_MEMORY_ACCESS(1);
  5027. shared_memory_lock(memory);
  5028. *(uint8 *)maddr = (uint8)sval;
  5029. shared_memory_unlock(memory);
  5030. }
  5031. else if (opcode == WASM_OP_ATOMIC_I32_STORE16) {
  5032. CHECK_MEMORY_OVERFLOW(2);
  5033. CHECK_ATOMIC_MEMORY_ACCESS(2);
  5034. shared_memory_lock(memory);
  5035. STORE_U16(maddr, (uint16)sval);
  5036. shared_memory_unlock(memory);
  5037. }
  5038. else {
  5039. CHECK_MEMORY_OVERFLOW(4);
  5040. CHECK_ATOMIC_MEMORY_ACCESS(4);
  5041. shared_memory_lock(memory);
  5042. STORE_U32(maddr, sval);
  5043. shared_memory_unlock(memory);
  5044. }
  5045. break;
  5046. }
  5047. case WASM_OP_ATOMIC_I64_STORE:
  5048. case WASM_OP_ATOMIC_I64_STORE8:
  5049. case WASM_OP_ATOMIC_I64_STORE16:
  5050. case WASM_OP_ATOMIC_I64_STORE32:
  5051. {
  5052. uint64 sval;
  5053. sval = (uint64)POP_I64();
  5054. addr = POP_I32();
  5055. if (opcode == WASM_OP_ATOMIC_I64_STORE8) {
  5056. CHECK_MEMORY_OVERFLOW(1);
  5057. CHECK_ATOMIC_MEMORY_ACCESS(1);
  5058. shared_memory_lock(memory);
  5059. *(uint8 *)maddr = (uint8)sval;
  5060. shared_memory_unlock(memory);
  5061. }
  5062. else if (opcode == WASM_OP_ATOMIC_I64_STORE16) {
  5063. CHECK_MEMORY_OVERFLOW(2);
  5064. CHECK_ATOMIC_MEMORY_ACCESS(2);
  5065. shared_memory_lock(memory);
  5066. STORE_U16(maddr, (uint16)sval);
  5067. shared_memory_unlock(memory);
  5068. }
  5069. else if (opcode == WASM_OP_ATOMIC_I64_STORE32) {
  5070. CHECK_MEMORY_OVERFLOW(4);
  5071. CHECK_ATOMIC_MEMORY_ACCESS(4);
  5072. shared_memory_lock(memory);
  5073. STORE_U32(maddr, (uint32)sval);
  5074. shared_memory_unlock(memory);
  5075. }
  5076. else {
  5077. CHECK_MEMORY_OVERFLOW(8);
  5078. CHECK_ATOMIC_MEMORY_ACCESS(8);
  5079. shared_memory_lock(memory);
  5080. STORE_I64(maddr, sval);
  5081. shared_memory_unlock(memory);
  5082. }
  5083. break;
  5084. }
  5085. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  5086. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  5087. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  5088. {
  5089. uint32 readv, sval, expect;
  5090. sval = POP_I32();
  5091. expect = POP_I32();
  5092. addr = POP_I32();
  5093. if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U) {
  5094. CHECK_MEMORY_OVERFLOW(1);
  5095. CHECK_ATOMIC_MEMORY_ACCESS(1);
  5096. expect = (uint8)expect;
  5097. shared_memory_lock(memory);
  5098. readv = (uint32)(*(uint8 *)maddr);
  5099. if (readv == expect)
  5100. *(uint8 *)maddr = (uint8)(sval);
  5101. shared_memory_unlock(memory);
  5102. }
  5103. else if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U) {
  5104. CHECK_MEMORY_OVERFLOW(2);
  5105. CHECK_ATOMIC_MEMORY_ACCESS(2);
  5106. expect = (uint16)expect;
  5107. shared_memory_lock(memory);
  5108. readv = (uint32)LOAD_U16(maddr);
  5109. if (readv == expect)
  5110. STORE_U16(maddr, (uint16)(sval));
  5111. shared_memory_unlock(memory);
  5112. }
  5113. else {
  5114. CHECK_MEMORY_OVERFLOW(4);
  5115. CHECK_ATOMIC_MEMORY_ACCESS(4);
  5116. shared_memory_lock(memory);
  5117. readv = LOAD_I32(maddr);
  5118. if (readv == expect)
  5119. STORE_U32(maddr, sval);
  5120. shared_memory_unlock(memory);
  5121. }
  5122. PUSH_I32(readv);
  5123. break;
  5124. }
  5125. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  5126. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  5127. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  5128. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  5129. {
  5130. uint64 readv, sval, expect;
  5131. sval = (uint64)POP_I64();
  5132. expect = (uint64)POP_I64();
  5133. addr = POP_I32();
  5134. if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U) {
  5135. CHECK_MEMORY_OVERFLOW(1);
  5136. CHECK_ATOMIC_MEMORY_ACCESS(1);
  5137. expect = (uint8)expect;
  5138. shared_memory_lock(memory);
  5139. readv = (uint64)(*(uint8 *)maddr);
  5140. if (readv == expect)
  5141. *(uint8 *)maddr = (uint8)(sval);
  5142. shared_memory_unlock(memory);
  5143. }
  5144. else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U) {
  5145. CHECK_MEMORY_OVERFLOW(2);
  5146. CHECK_ATOMIC_MEMORY_ACCESS(2);
  5147. expect = (uint16)expect;
  5148. shared_memory_lock(memory);
  5149. readv = (uint64)LOAD_U16(maddr);
  5150. if (readv == expect)
  5151. STORE_U16(maddr, (uint16)(sval));
  5152. shared_memory_unlock(memory);
  5153. }
  5154. else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U) {
  5155. CHECK_MEMORY_OVERFLOW(4);
  5156. CHECK_ATOMIC_MEMORY_ACCESS(4);
  5157. expect = (uint32)expect;
  5158. shared_memory_lock(memory);
  5159. readv = (uint64)LOAD_U32(maddr);
  5160. if (readv == expect)
  5161. STORE_U32(maddr, (uint32)(sval));
  5162. shared_memory_unlock(memory);
  5163. }
  5164. else {
  5165. CHECK_MEMORY_OVERFLOW(8);
  5166. CHECK_ATOMIC_MEMORY_ACCESS(8);
  5167. shared_memory_lock(memory);
  5168. readv = (uint64)LOAD_I64(maddr);
  5169. if (readv == expect)
  5170. STORE_I64(maddr, sval);
  5171. shared_memory_unlock(memory);
  5172. }
  5173. PUSH_I64(readv);
  5174. break;
  5175. }
  5176. DEF_ATOMIC_RMW_OPCODE(ADD, +);
  5177. DEF_ATOMIC_RMW_OPCODE(SUB, -);
  5178. DEF_ATOMIC_RMW_OPCODE(AND, &);
  5179. DEF_ATOMIC_RMW_OPCODE(OR, |);
  5180. DEF_ATOMIC_RMW_OPCODE(XOR, ^);
  5181. /* xchg, ignore the read value, and store the given
  5182. value: readv * 0 + sval */
  5183. DEF_ATOMIC_RMW_OPCODE(XCHG, *0 +);
  5184. }
  5185. HANDLE_OP_END();
  5186. }
  5187. #endif
  5188. HANDLE_OP(WASM_OP_IMPDEP)
  5189. {
  5190. frame = prev_frame;
  5191. frame_ip = frame->ip;
  5192. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  5193. is_return_call = false;
  5194. #endif
  5195. goto call_func_from_entry;
  5196. }
  5197. #if WASM_ENABLE_SIMDE != 0
  5198. #define SIMD_V128_TO_SIMDE_V128(s_v) \
  5199. ({ \
  5200. bh_assert(sizeof(V128) == sizeof(simde_v128_t)); \
  5201. simde_v128_t se_v; \
  5202. bh_memcpy_s(&se_v, sizeof(simde_v128_t), &(s_v), sizeof(V128)); \
  5203. se_v; \
  5204. })
  5205. #define SIMDE_V128_TO_SIMD_V128(sv, v) \
  5206. do { \
  5207. bh_assert(sizeof(V128) == sizeof(simde_v128_t)); \
  5208. bh_memcpy_s(&(v), sizeof(V128), &(sv), sizeof(simde_v128_t)); \
  5209. } while (0)
  5210. HANDLE_OP(WASM_OP_SIMD_PREFIX)
  5211. {
  5212. GET_OPCODE();
  5213. switch (opcode) {
  5214. /* Memory */
  5215. case SIMD_v128_load:
  5216. {
  5217. uint32 offset, addr;
  5218. offset = read_uint32(frame_ip);
  5219. addr = POP_I32();
  5220. addr_ret = GET_OFFSET();
  5221. CHECK_MEMORY_OVERFLOW(16);
  5222. PUT_V128_TO_ADDR(frame_lp + addr_ret, LOAD_V128(maddr));
  5223. break;
  5224. }
  5225. #define SIMD_LOAD_OP(simde_func) \
  5226. do { \
  5227. uint32 offset, addr; \
  5228. offset = read_uint32(frame_ip); \
  5229. addr = POP_I32(); \
  5230. addr_ret = GET_OFFSET(); \
  5231. CHECK_MEMORY_OVERFLOW(8); \
  5232. \
  5233. simde_v128_t simde_result = simde_func(maddr); \
  5234. \
  5235. V128 result; \
  5236. SIMDE_V128_TO_SIMD_V128(simde_result, result); \
  5237. PUT_V128_TO_ADDR(frame_lp + addr_ret, result); \
  5238. \
  5239. } while (0)
  5240. case SIMD_v128_load8x8_s:
  5241. {
  5242. SIMD_LOAD_OP(simde_wasm_i16x8_load8x8);
  5243. break;
  5244. }
  5245. case SIMD_v128_load8x8_u:
  5246. {
  5247. SIMD_LOAD_OP(simde_wasm_u16x8_load8x8);
  5248. break;
  5249. }
  5250. case SIMD_v128_load16x4_s:
  5251. {
  5252. SIMD_LOAD_OP(simde_wasm_i32x4_load16x4);
  5253. break;
  5254. }
  5255. case SIMD_v128_load16x4_u:
  5256. {
  5257. SIMD_LOAD_OP(simde_wasm_u32x4_load16x4);
  5258. break;
  5259. }
  5260. case SIMD_v128_load32x2_s:
  5261. {
  5262. SIMD_LOAD_OP(simde_wasm_i64x2_load32x2);
  5263. break;
  5264. }
  5265. case SIMD_v128_load32x2_u:
  5266. {
  5267. SIMD_LOAD_OP(simde_wasm_u64x2_load32x2);
  5268. break;
  5269. }
  5270. #define SIMD_LOAD_SPLAT_OP(simde_func, width) \
  5271. do { \
  5272. uint32 offset, addr; \
  5273. offset = read_uint32(frame_ip); \
  5274. addr = POP_I32(); \
  5275. addr_ret = GET_OFFSET(); \
  5276. CHECK_MEMORY_OVERFLOW(width / 8); \
  5277. \
  5278. simde_v128_t simde_result = simde_func(maddr); \
  5279. \
  5280. V128 result; \
  5281. SIMDE_V128_TO_SIMD_V128(simde_result, result); \
  5282. \
  5283. PUT_V128_TO_ADDR(frame_lp + addr_ret, result); \
  5284. } while (0)
  5285. case SIMD_v128_load8_splat:
  5286. {
  5287. SIMD_LOAD_SPLAT_OP(simde_wasm_v128_load8_splat, 8);
  5288. break;
  5289. }
  5290. case SIMD_v128_load16_splat:
  5291. {
  5292. SIMD_LOAD_SPLAT_OP(simde_wasm_v128_load16_splat, 16);
  5293. break;
  5294. }
  5295. case SIMD_v128_load32_splat:
  5296. {
  5297. SIMD_LOAD_SPLAT_OP(simde_wasm_v128_load32_splat, 32);
  5298. break;
  5299. }
  5300. case SIMD_v128_load64_splat:
  5301. {
  5302. SIMD_LOAD_SPLAT_OP(simde_wasm_v128_load64_splat, 64);
  5303. break;
  5304. }
  5305. case SIMD_v128_store:
  5306. {
  5307. uint32 offset, addr;
  5308. offset = read_uint32(frame_ip);
  5309. V128 data = POP_V128();
  5310. addr = POP_I32();
  5311. CHECK_MEMORY_OVERFLOW(16);
  5312. STORE_V128(maddr, data);
  5313. break;
  5314. }
  5315. /* Basic */
  5316. case SIMD_v128_const:
  5317. {
  5318. uint8 *orig_ip = frame_ip;
  5319. frame_ip += sizeof(V128);
  5320. addr_ret = GET_OFFSET();
  5321. PUT_V128_TO_ADDR(frame_lp + addr_ret, *(V128 *)orig_ip);
  5322. break;
  5323. }
  5324. /* TODO: Add a faster SIMD implementation */
  5325. case SIMD_v8x16_shuffle:
  5326. {
  5327. V128 indices;
  5328. bh_memcpy_s(&indices, sizeof(V128), frame_ip,
  5329. sizeof(V128));
  5330. frame_ip += sizeof(V128);
  5331. V128 v2 = POP_V128();
  5332. V128 v1 = POP_V128();
  5333. addr_ret = GET_OFFSET();
  5334. V128 result;
  5335. for (int i = 0; i < 16; i++) {
  5336. uint8_t index = indices.i8x16[i];
  5337. if (index < 16) {
  5338. result.i8x16[i] = v1.i8x16[index];
  5339. }
  5340. else {
  5341. result.i8x16[i] = v2.i8x16[index - 16];
  5342. }
  5343. }
  5344. PUT_V128_TO_ADDR(frame_lp + addr_ret, result);
  5345. break;
  5346. }
  5347. case SIMD_v8x16_swizzle:
  5348. {
  5349. V128 v2 = POP_V128();
  5350. V128 v1 = POP_V128();
  5351. addr_ret = GET_OFFSET();
  5352. simde_v128_t simde_result = simde_wasm_i8x16_swizzle(
  5353. SIMD_V128_TO_SIMDE_V128(v1),
  5354. SIMD_V128_TO_SIMDE_V128(v2));
  5355. V128 result;
  5356. SIMDE_V128_TO_SIMD_V128(simde_result, result);
  5357. PUT_V128_TO_ADDR(frame_lp + addr_ret, result);
  5358. break;
  5359. }
  5360. /* Splat */
  5361. #define SIMD_SPLAT_OP(simde_func, pop_func, val_type) \
  5362. do { \
  5363. val_type v = pop_func(); \
  5364. addr_ret = GET_OFFSET(); \
  5365. \
  5366. simde_v128_t simde_result = simde_func(v); \
  5367. \
  5368. V128 result; \
  5369. SIMDE_V128_TO_SIMD_V128(simde_result, result); \
  5370. \
  5371. PUT_V128_TO_ADDR(frame_lp + addr_ret, result); \
  5372. } while (0)
  5373. #define SIMD_SPLAT_OP_I32(simde_func) SIMD_SPLAT_OP(simde_func, POP_I32, uint32)
  5374. #define SIMD_SPLAT_OP_I64(simde_func) SIMD_SPLAT_OP(simde_func, POP_I64, uint64)
  5375. #define SIMD_SPLAT_OP_F32(simde_func) \
  5376. SIMD_SPLAT_OP(simde_func, POP_F32, float32)
  5377. #define SIMD_SPLAT_OP_F64(simde_func) \
  5378. SIMD_SPLAT_OP(simde_func, POP_F64, float64)
  5379. case SIMD_i8x16_splat:
  5380. {
  5381. val = POP_I32();
  5382. addr_ret = GET_OFFSET();
  5383. simde_v128_t simde_result = simde_wasm_i8x16_splat(val);
  5384. V128 result;
  5385. SIMDE_V128_TO_SIMD_V128(simde_result, result);
  5386. PUT_V128_TO_ADDR(frame_lp + addr_ret, result);
  5387. break;
  5388. }
  5389. case SIMD_i16x8_splat:
  5390. {
  5391. SIMD_SPLAT_OP_I32(simde_wasm_i16x8_splat);
  5392. break;
  5393. }
  5394. case SIMD_i32x4_splat:
  5395. {
  5396. SIMD_SPLAT_OP_I32(simde_wasm_i32x4_splat);
  5397. break;
  5398. }
  5399. case SIMD_i64x2_splat:
  5400. {
  5401. SIMD_SPLAT_OP_I64(simde_wasm_i64x2_splat);
  5402. break;
  5403. }
  5404. case SIMD_f32x4_splat:
  5405. {
  5406. SIMD_SPLAT_OP_F32(simde_wasm_f32x4_splat);
  5407. break;
  5408. }
  5409. case SIMD_f64x2_splat:
  5410. {
  5411. SIMD_SPLAT_OP_F64(simde_wasm_f64x2_splat);
  5412. break;
  5413. }
  5414. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  5415. #define SIMD_LANE_HANDLE_UNALIGNED_ACCESS()
  5416. #else
  5417. #define SIMD_LANE_HANDLE_UNALIGNED_ACCESS() (void)*frame_ip++
  5418. #endif /* WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0 */
  5419. #define SIMD_EXTRACT_LANE_OP(register, return_type, push_elem) \
  5420. do { \
  5421. uint8 lane = *frame_ip++; \
  5422. SIMD_LANE_HANDLE_UNALIGNED_ACCESS(); \
  5423. V128 v = POP_V128(); \
  5424. push_elem((return_type)(v.register[lane])); \
  5425. } while (0)
  5426. #define SIMD_REPLACE_LANE_OP(register, return_type, pop_elem) \
  5427. do { \
  5428. uint8 lane = *frame_ip++; \
  5429. SIMD_LANE_HANDLE_UNALIGNED_ACCESS(); \
  5430. return_type replacement = pop_elem(); \
  5431. V128 v = POP_V128(); \
  5432. v.register[lane] = replacement; \
  5433. addr_ret = GET_OFFSET(); \
  5434. PUT_V128_TO_ADDR(frame_lp + addr_ret, v); \
  5435. } while (0)
  5436. case SIMD_i8x16_extract_lane_s:
  5437. {
  5438. SIMD_EXTRACT_LANE_OP(i8x16, int8, PUSH_I32);
  5439. break;
  5440. }
  5441. case SIMD_i8x16_extract_lane_u:
  5442. {
  5443. SIMD_EXTRACT_LANE_OP(i8x16, uint8, PUSH_I32);
  5444. break;
  5445. }
  5446. case SIMD_i8x16_replace_lane:
  5447. {
  5448. SIMD_REPLACE_LANE_OP(i8x16, int8, POP_I32);
  5449. break;
  5450. }
  5451. case SIMD_i16x8_extract_lane_s:
  5452. {
  5453. SIMD_EXTRACT_LANE_OP(i16x8, int16, PUSH_I32);
  5454. break;
  5455. }
  5456. case SIMD_i16x8_extract_lane_u:
  5457. {
  5458. SIMD_EXTRACT_LANE_OP(i16x8, uint16, PUSH_I32);
  5459. break;
  5460. }
  5461. case SIMD_i16x8_replace_lane:
  5462. {
  5463. SIMD_REPLACE_LANE_OP(i16x8, int16, POP_I32);
  5464. break;
  5465. }
  5466. case SIMD_i32x4_extract_lane:
  5467. {
  5468. SIMD_EXTRACT_LANE_OP(i32x4, int32, PUSH_I32);
  5469. break;
  5470. }
  5471. case SIMD_i32x4_replace_lane:
  5472. {
  5473. SIMD_REPLACE_LANE_OP(i32x4, int32, POP_I32);
  5474. break;
  5475. }
  5476. case SIMD_i64x2_extract_lane:
  5477. {
  5478. SIMD_EXTRACT_LANE_OP(i64x2, int64, PUSH_I64);
  5479. break;
  5480. }
  5481. case SIMD_i64x2_replace_lane:
  5482. {
  5483. SIMD_REPLACE_LANE_OP(i64x2, int64, POP_I64);
  5484. break;
  5485. }
  5486. case SIMD_f32x4_extract_lane:
  5487. {
  5488. SIMD_EXTRACT_LANE_OP(f32x4, float32, PUSH_F32);
  5489. break;
  5490. }
  5491. case SIMD_f32x4_replace_lane:
  5492. {
  5493. SIMD_REPLACE_LANE_OP(f32x4, float32, POP_F32);
  5494. break;
  5495. }
  5496. case SIMD_f64x2_extract_lane:
  5497. {
  5498. SIMD_EXTRACT_LANE_OP(f64x2, float64, PUSH_F64);
  5499. break;
  5500. }
  5501. case SIMD_f64x2_replace_lane:
  5502. {
  5503. SIMD_REPLACE_LANE_OP(f64x2, float64, POP_F64);
  5504. break;
  5505. }
  5506. #define SIMD_DOUBLE_OP(simde_func) \
  5507. do { \
  5508. V128 v2 = POP_V128(); \
  5509. V128 v1 = POP_V128(); \
  5510. addr_ret = GET_OFFSET(); \
  5511. \
  5512. simde_v128_t simde_result = simde_func(SIMD_V128_TO_SIMDE_V128(v1), \
  5513. SIMD_V128_TO_SIMDE_V128(v2)); \
  5514. \
  5515. V128 result; \
  5516. SIMDE_V128_TO_SIMD_V128(simde_result, result); \
  5517. \
  5518. PUT_V128_TO_ADDR(frame_lp + addr_ret, result); \
  5519. } while (0)
  5520. /* i8x16 comparison operations */
  5521. case SIMD_i8x16_eq:
  5522. {
  5523. V128 v2 = POP_V128();
  5524. V128 v1 = POP_V128();
  5525. addr_ret = GET_OFFSET();
  5526. simde_v128_t simde_result =
  5527. simde_wasm_i8x16_eq(SIMD_V128_TO_SIMDE_V128(v1),
  5528. SIMD_V128_TO_SIMDE_V128(v2));
  5529. V128 result;
  5530. SIMDE_V128_TO_SIMD_V128(simde_result, result);
  5531. PUT_V128_TO_ADDR(frame_lp + addr_ret, result);
  5532. break;
  5533. }
  5534. case SIMD_i8x16_ne:
  5535. {
  5536. SIMD_DOUBLE_OP(simde_wasm_i8x16_ne);
  5537. break;
  5538. }
  5539. case SIMD_i8x16_lt_s:
  5540. {
  5541. SIMD_DOUBLE_OP(simde_wasm_i8x16_lt);
  5542. break;
  5543. }
  5544. case SIMD_i8x16_lt_u:
  5545. {
  5546. SIMD_DOUBLE_OP(simde_wasm_u8x16_lt);
  5547. break;
  5548. }
  5549. case SIMD_i8x16_gt_s:
  5550. {
  5551. SIMD_DOUBLE_OP(simde_wasm_i8x16_gt);
  5552. break;
  5553. }
  5554. case SIMD_i8x16_gt_u:
  5555. {
  5556. SIMD_DOUBLE_OP(simde_wasm_u8x16_gt);
  5557. break;
  5558. }
  5559. case SIMD_i8x16_le_s:
  5560. {
  5561. SIMD_DOUBLE_OP(simde_wasm_i8x16_le);
  5562. break;
  5563. }
  5564. case SIMD_i8x16_le_u:
  5565. {
  5566. SIMD_DOUBLE_OP(simde_wasm_u8x16_le);
  5567. break;
  5568. }
  5569. case SIMD_i8x16_ge_s:
  5570. {
  5571. SIMD_DOUBLE_OP(simde_wasm_i8x16_ge);
  5572. break;
  5573. }
  5574. case SIMD_i8x16_ge_u:
  5575. {
  5576. SIMD_DOUBLE_OP(simde_wasm_u8x16_ge);
  5577. break;
  5578. }
  5579. /* i16x8 comparison operations */
  5580. case SIMD_i16x8_eq:
  5581. {
  5582. SIMD_DOUBLE_OP(simde_wasm_i16x8_eq);
  5583. break;
  5584. }
  5585. case SIMD_i16x8_ne:
  5586. {
  5587. SIMD_DOUBLE_OP(simde_wasm_i16x8_ne);
  5588. break;
  5589. }
  5590. case SIMD_i16x8_lt_s:
  5591. {
  5592. SIMD_DOUBLE_OP(simde_wasm_i16x8_lt);
  5593. break;
  5594. }
  5595. case SIMD_i16x8_lt_u:
  5596. {
  5597. SIMD_DOUBLE_OP(simde_wasm_u16x8_lt);
  5598. break;
  5599. }
  5600. case SIMD_i16x8_gt_s:
  5601. {
  5602. SIMD_DOUBLE_OP(simde_wasm_i16x8_gt);
  5603. break;
  5604. }
  5605. case SIMD_i16x8_gt_u:
  5606. {
  5607. SIMD_DOUBLE_OP(simde_wasm_u16x8_gt);
  5608. break;
  5609. }
  5610. case SIMD_i16x8_le_s:
  5611. {
  5612. SIMD_DOUBLE_OP(simde_wasm_i16x8_le);
  5613. break;
  5614. }
  5615. case SIMD_i16x8_le_u:
  5616. {
  5617. SIMD_DOUBLE_OP(simde_wasm_u16x8_le);
  5618. break;
  5619. }
  5620. case SIMD_i16x8_ge_s:
  5621. {
  5622. SIMD_DOUBLE_OP(simde_wasm_i16x8_ge);
  5623. break;
  5624. }
  5625. case SIMD_i16x8_ge_u:
  5626. {
  5627. SIMD_DOUBLE_OP(simde_wasm_u16x8_ge);
  5628. break;
  5629. }
  5630. /* i32x4 comparison operations */
  5631. case SIMD_i32x4_eq:
  5632. {
  5633. SIMD_DOUBLE_OP(simde_wasm_i32x4_eq);
  5634. break;
  5635. }
  5636. case SIMD_i32x4_ne:
  5637. {
  5638. SIMD_DOUBLE_OP(simde_wasm_i32x4_ne);
  5639. break;
  5640. }
  5641. case SIMD_i32x4_lt_s:
  5642. {
  5643. SIMD_DOUBLE_OP(simde_wasm_i32x4_lt);
  5644. break;
  5645. }
  5646. case SIMD_i32x4_lt_u:
  5647. {
  5648. SIMD_DOUBLE_OP(simde_wasm_u32x4_lt);
  5649. break;
  5650. }
  5651. case SIMD_i32x4_gt_s:
  5652. {
  5653. SIMD_DOUBLE_OP(simde_wasm_i32x4_gt);
  5654. break;
  5655. }
  5656. case SIMD_i32x4_gt_u:
  5657. {
  5658. SIMD_DOUBLE_OP(simde_wasm_u32x4_gt);
  5659. break;
  5660. }
  5661. case SIMD_i32x4_le_s:
  5662. {
  5663. SIMD_DOUBLE_OP(simde_wasm_i32x4_le);
  5664. break;
  5665. }
  5666. case SIMD_i32x4_le_u:
  5667. {
  5668. SIMD_DOUBLE_OP(simde_wasm_u32x4_le);
  5669. break;
  5670. }
  5671. case SIMD_i32x4_ge_s:
  5672. {
  5673. SIMD_DOUBLE_OP(simde_wasm_i32x4_ge);
  5674. break;
  5675. }
  5676. case SIMD_i32x4_ge_u:
  5677. {
  5678. SIMD_DOUBLE_OP(simde_wasm_u32x4_ge);
  5679. break;
  5680. }
  5681. /* f32x4 comparison operations */
  5682. case SIMD_f32x4_eq:
  5683. {
  5684. SIMD_DOUBLE_OP(simde_wasm_f32x4_eq);
  5685. break;
  5686. }
  5687. case SIMD_f32x4_ne:
  5688. {
  5689. SIMD_DOUBLE_OP(simde_wasm_f32x4_ne);
  5690. break;
  5691. }
  5692. case SIMD_f32x4_lt:
  5693. {
  5694. SIMD_DOUBLE_OP(simde_wasm_f32x4_lt);
  5695. break;
  5696. }
  5697. case SIMD_f32x4_gt:
  5698. {
  5699. SIMD_DOUBLE_OP(simde_wasm_f32x4_gt);
  5700. break;
  5701. }
  5702. case SIMD_f32x4_le:
  5703. {
  5704. SIMD_DOUBLE_OP(simde_wasm_f32x4_le);
  5705. break;
  5706. }
  5707. case SIMD_f32x4_ge:
  5708. {
  5709. SIMD_DOUBLE_OP(simde_wasm_f32x4_ge);
  5710. break;
  5711. }
  5712. /* f64x2 comparison operations */
  5713. case SIMD_f64x2_eq:
  5714. {
  5715. SIMD_DOUBLE_OP(simde_wasm_f64x2_eq);
  5716. break;
  5717. }
  5718. case SIMD_f64x2_ne:
  5719. {
  5720. SIMD_DOUBLE_OP(simde_wasm_f64x2_ne);
  5721. break;
  5722. }
  5723. case SIMD_f64x2_lt:
  5724. {
  5725. SIMD_DOUBLE_OP(simde_wasm_f64x2_lt);
  5726. break;
  5727. }
  5728. case SIMD_f64x2_gt:
  5729. {
  5730. SIMD_DOUBLE_OP(simde_wasm_f64x2_gt);
  5731. break;
  5732. }
  5733. case SIMD_f64x2_le:
  5734. {
  5735. SIMD_DOUBLE_OP(simde_wasm_f64x2_le);
  5736. break;
  5737. }
  5738. case SIMD_f64x2_ge:
  5739. {
  5740. SIMD_DOUBLE_OP(simde_wasm_f64x2_ge);
  5741. break;
  5742. }
  5743. /* v128 bitwise operations */
  5744. #define SIMD_V128_BITWISE_OP_COMMON(result_expr_0, result_expr_1) \
  5745. do { \
  5746. V128 result; \
  5747. result.i64x2[0] = (result_expr_0); \
  5748. result.i64x2[1] = (result_expr_1); \
  5749. addr_ret = GET_OFFSET(); \
  5750. PUT_V128_TO_ADDR(frame_lp + addr_ret, result); \
  5751. } while (0)
  5752. case SIMD_v128_not:
  5753. {
  5754. V128 value = POP_V128();
  5755. SIMD_V128_BITWISE_OP_COMMON(~value.i64x2[0],
  5756. ~value.i64x2[1]);
  5757. break;
  5758. }
  5759. case SIMD_v128_and:
  5760. {
  5761. V128 v2 = POP_V128();
  5762. V128 v1 = POP_V128();
  5763. SIMD_V128_BITWISE_OP_COMMON(v1.i64x2[0] & v2.i64x2[0],
  5764. v1.i64x2[1] & v2.i64x2[1]);
  5765. break;
  5766. }
  5767. case SIMD_v128_andnot:
  5768. {
  5769. V128 v2 = POP_V128();
  5770. V128 v1 = POP_V128();
  5771. SIMD_V128_BITWISE_OP_COMMON(
  5772. v1.i64x2[0] & (~v2.i64x2[0]),
  5773. v1.i64x2[1] & (~v2.i64x2[1]));
  5774. break;
  5775. }
  5776. case SIMD_v128_or:
  5777. {
  5778. V128 v2 = POP_V128();
  5779. V128 v1 = POP_V128();
  5780. SIMD_V128_BITWISE_OP_COMMON(v1.i64x2[0] | v2.i64x2[0],
  5781. v1.i64x2[1] | v2.i64x2[1]);
  5782. break;
  5783. }
  5784. case SIMD_v128_xor:
  5785. {
  5786. V128 v2 = POP_V128();
  5787. V128 v1 = POP_V128();
  5788. SIMD_V128_BITWISE_OP_COMMON(v1.i64x2[0] ^ v2.i64x2[0],
  5789. v1.i64x2[1] ^ v2.i64x2[1]);
  5790. break;
  5791. }
  5792. case SIMD_v128_bitselect:
  5793. {
  5794. V128 v1 = POP_V128();
  5795. V128 v2 = POP_V128();
  5796. V128 v3 = POP_V128();
  5797. addr_ret = GET_OFFSET();
  5798. simde_v128_t simde_result = simde_wasm_v128_bitselect(
  5799. SIMD_V128_TO_SIMDE_V128(v3),
  5800. SIMD_V128_TO_SIMDE_V128(v2),
  5801. SIMD_V128_TO_SIMDE_V128(v1));
  5802. V128 result;
  5803. SIMDE_V128_TO_SIMD_V128(simde_result, result);
  5804. PUT_V128_TO_ADDR(frame_lp + addr_ret, result);
  5805. break;
  5806. }
  5807. case SIMD_v128_any_true:
  5808. {
  5809. V128 value = POP_V128();
  5810. addr_ret = GET_OFFSET();
  5811. frame_lp[addr_ret] =
  5812. value.i64x2[0] != 0 || value.i64x2[1] != 0;
  5813. break;
  5814. }
  5815. #define SIMD_LOAD_LANE_COMMON(vec, register, lane, width) \
  5816. do { \
  5817. addr_ret = GET_OFFSET(); \
  5818. CHECK_MEMORY_OVERFLOW(width / 8); \
  5819. if (width == 64) { \
  5820. vec.register[lane] = GET_I64_FROM_ADDR((uint32 *)maddr); \
  5821. } \
  5822. else { \
  5823. vec.register[lane] = *(uint##width *)(maddr); \
  5824. } \
  5825. PUT_V128_TO_ADDR(frame_lp + addr_ret, vec); \
  5826. } while (0)
  5827. #define SIMD_LOAD_LANE_OP(register, width) \
  5828. do { \
  5829. uint32 offset, addr; \
  5830. offset = read_uint32(frame_ip); \
  5831. V128 vec = POP_V128(); \
  5832. addr = POP_I32(); \
  5833. int lane = *frame_ip++; \
  5834. SIMD_LANE_HANDLE_UNALIGNED_ACCESS(); \
  5835. SIMD_LOAD_LANE_COMMON(vec, register, lane, width); \
  5836. } while (0)
  5837. case SIMD_v128_load8_lane:
  5838. {
  5839. SIMD_LOAD_LANE_OP(i8x16, 8);
  5840. break;
  5841. }
  5842. case SIMD_v128_load16_lane:
  5843. {
  5844. SIMD_LOAD_LANE_OP(i16x8, 16);
  5845. break;
  5846. }
  5847. case SIMD_v128_load32_lane:
  5848. {
  5849. SIMD_LOAD_LANE_OP(i32x4, 32);
  5850. break;
  5851. }
  5852. case SIMD_v128_load64_lane:
  5853. {
  5854. SIMD_LOAD_LANE_OP(i64x2, 64);
  5855. break;
  5856. }
  5857. #define SIMD_STORE_LANE_OP(register, width) \
  5858. do { \
  5859. uint32 offset, addr; \
  5860. offset = read_uint32(frame_ip); \
  5861. V128 vec = POP_V128(); \
  5862. addr = POP_I32(); \
  5863. int lane = *frame_ip++; \
  5864. SIMD_LANE_HANDLE_UNALIGNED_ACCESS(); \
  5865. CHECK_MEMORY_OVERFLOW(width / 8); \
  5866. if (width == 64) { \
  5867. STORE_I64(maddr, vec.register[lane]); \
  5868. } \
  5869. else { \
  5870. *(uint##width *)(maddr) = vec.register[lane]; \
  5871. } \
  5872. } while (0)
  5873. case SIMD_v128_store8_lane:
  5874. {
  5875. SIMD_STORE_LANE_OP(i8x16, 8);
  5876. break;
  5877. }
  5878. case SIMD_v128_store16_lane:
  5879. {
  5880. SIMD_STORE_LANE_OP(i16x8, 16);
  5881. break;
  5882. }
  5883. case SIMD_v128_store32_lane:
  5884. {
  5885. SIMD_STORE_LANE_OP(i32x4, 32);
  5886. break;
  5887. }
  5888. case SIMD_v128_store64_lane:
  5889. {
  5890. SIMD_STORE_LANE_OP(i64x2, 64);
  5891. break;
  5892. }
  5893. #define SIMD_LOAD_ZERO_OP(register, width) \
  5894. do { \
  5895. uint32 offset, addr; \
  5896. offset = read_uint32(frame_ip); \
  5897. addr = POP_I32(); \
  5898. int32 lane = 0; \
  5899. V128 vec = { 0 }; \
  5900. SIMD_LOAD_LANE_COMMON(vec, register, lane, width); \
  5901. } while (0)
  5902. case SIMD_v128_load32_zero:
  5903. {
  5904. SIMD_LOAD_ZERO_OP(i32x4, 32);
  5905. break;
  5906. }
  5907. case SIMD_v128_load64_zero:
  5908. {
  5909. SIMD_LOAD_ZERO_OP(i64x2, 64);
  5910. break;
  5911. }
  5912. #define SIMD_SINGLE_OP(simde_func) \
  5913. do { \
  5914. V128 v1 = POP_V128(); \
  5915. addr_ret = GET_OFFSET(); \
  5916. \
  5917. simde_v128_t simde_result = simde_func(SIMD_V128_TO_SIMDE_V128(v1)); \
  5918. \
  5919. V128 result; \
  5920. SIMDE_V128_TO_SIMD_V128(simde_result, result); \
  5921. \
  5922. PUT_V128_TO_ADDR(frame_lp + addr_ret, result); \
  5923. } while (0)
  5924. /* Float conversion */
  5925. case SIMD_f32x4_demote_f64x2_zero:
  5926. {
  5927. SIMD_SINGLE_OP(simde_wasm_f32x4_demote_f64x2_zero);
  5928. break;
  5929. }
  5930. case SIMD_f64x2_promote_low_f32x4_zero:
  5931. {
  5932. SIMD_SINGLE_OP(simde_wasm_f64x2_promote_low_f32x4);
  5933. break;
  5934. }
  5935. /* i8x16 operations */
  5936. case SIMD_i8x16_abs:
  5937. {
  5938. SIMD_SINGLE_OP(simde_wasm_i8x16_abs);
  5939. break;
  5940. }
  5941. case SIMD_i8x16_neg:
  5942. {
  5943. SIMD_SINGLE_OP(simde_wasm_i8x16_neg);
  5944. break;
  5945. }
  5946. case SIMD_i8x16_popcnt:
  5947. {
  5948. SIMD_SINGLE_OP(simde_wasm_i8x16_popcnt);
  5949. break;
  5950. }
  5951. case SIMD_i8x16_all_true:
  5952. {
  5953. V128 v1 = POP_V128();
  5954. bool result = simde_wasm_i8x16_all_true(
  5955. SIMD_V128_TO_SIMDE_V128(v1));
  5956. addr_ret = GET_OFFSET();
  5957. frame_lp[addr_ret] = result;
  5958. break;
  5959. }
  5960. case SIMD_i8x16_bitmask:
  5961. {
  5962. V128 v1 = POP_V128();
  5963. uint32_t result = simde_wasm_i8x16_bitmask(
  5964. SIMD_V128_TO_SIMDE_V128(v1));
  5965. addr_ret = GET_OFFSET();
  5966. frame_lp[addr_ret] = result;
  5967. break;
  5968. }
  5969. case SIMD_i8x16_narrow_i16x8_s:
  5970. {
  5971. SIMD_DOUBLE_OP(simde_wasm_i8x16_narrow_i16x8);
  5972. break;
  5973. }
  5974. case SIMD_i8x16_narrow_i16x8_u:
  5975. {
  5976. SIMD_DOUBLE_OP(simde_wasm_u8x16_narrow_i16x8);
  5977. break;
  5978. }
  5979. case SIMD_f32x4_ceil:
  5980. {
  5981. SIMD_SINGLE_OP(simde_wasm_f32x4_ceil);
  5982. break;
  5983. }
  5984. case SIMD_f32x4_floor:
  5985. {
  5986. SIMD_SINGLE_OP(simde_wasm_f32x4_floor);
  5987. break;
  5988. }
  5989. case SIMD_f32x4_trunc:
  5990. {
  5991. SIMD_SINGLE_OP(simde_wasm_f32x4_trunc);
  5992. break;
  5993. }
  5994. case SIMD_f32x4_nearest:
  5995. {
  5996. SIMD_SINGLE_OP(simde_wasm_f32x4_nearest);
  5997. break;
  5998. }
  5999. #define SIMD_LANE_SHIFT(simde_func) \
  6000. do { \
  6001. int32 c = POP_I32(); \
  6002. V128 v1 = POP_V128(); \
  6003. addr_ret = GET_OFFSET(); \
  6004. \
  6005. simde_v128_t simde_result = \
  6006. simde_func(SIMD_V128_TO_SIMDE_V128(v1), c); \
  6007. \
  6008. V128 result; \
  6009. SIMDE_V128_TO_SIMD_V128(simde_result, result); \
  6010. \
  6011. PUT_V128_TO_ADDR(frame_lp + addr_ret, result); \
  6012. } while (0)
  6013. case SIMD_i8x16_shl:
  6014. {
  6015. SIMD_LANE_SHIFT(simde_wasm_i8x16_shl);
  6016. break;
  6017. }
  6018. case SIMD_i8x16_shr_s:
  6019. {
  6020. SIMD_LANE_SHIFT(simde_wasm_i8x16_shr);
  6021. break;
  6022. }
  6023. case SIMD_i8x16_shr_u:
  6024. {
  6025. SIMD_LANE_SHIFT(simde_wasm_u8x16_shr);
  6026. break;
  6027. }
  6028. case SIMD_i8x16_add:
  6029. {
  6030. SIMD_DOUBLE_OP(simde_wasm_i8x16_add);
  6031. break;
  6032. }
  6033. case SIMD_i8x16_add_sat_s:
  6034. {
  6035. SIMD_DOUBLE_OP(simde_wasm_i8x16_add_sat);
  6036. break;
  6037. }
  6038. case SIMD_i8x16_add_sat_u:
  6039. {
  6040. SIMD_DOUBLE_OP(simde_wasm_u8x16_add_sat);
  6041. break;
  6042. }
  6043. case SIMD_i8x16_sub:
  6044. {
  6045. SIMD_DOUBLE_OP(simde_wasm_i8x16_sub);
  6046. break;
  6047. }
  6048. case SIMD_i8x16_sub_sat_s:
  6049. {
  6050. SIMD_DOUBLE_OP(simde_wasm_i8x16_sub_sat);
  6051. break;
  6052. }
  6053. case SIMD_i8x16_sub_sat_u:
  6054. {
  6055. SIMD_DOUBLE_OP(simde_wasm_u8x16_sub_sat);
  6056. break;
  6057. }
  6058. case SIMD_f64x2_ceil:
  6059. {
  6060. SIMD_SINGLE_OP(simde_wasm_f64x2_ceil);
  6061. break;
  6062. }
  6063. case SIMD_f64x2_floor:
  6064. {
  6065. SIMD_SINGLE_OP(simde_wasm_f64x2_floor);
  6066. break;
  6067. }
  6068. case SIMD_i8x16_min_s:
  6069. {
  6070. SIMD_DOUBLE_OP(simde_wasm_i8x16_min);
  6071. break;
  6072. }
  6073. case SIMD_i8x16_min_u:
  6074. {
  6075. SIMD_DOUBLE_OP(simde_wasm_u8x16_min);
  6076. break;
  6077. }
  6078. case SIMD_i8x16_max_s:
  6079. {
  6080. SIMD_DOUBLE_OP(simde_wasm_i8x16_max);
  6081. break;
  6082. }
  6083. case SIMD_i8x16_max_u:
  6084. {
  6085. SIMD_DOUBLE_OP(simde_wasm_u8x16_max);
  6086. break;
  6087. }
  6088. case SIMD_f64x2_trunc:
  6089. {
  6090. SIMD_SINGLE_OP(simde_wasm_f64x2_trunc);
  6091. break;
  6092. }
  6093. case SIMD_i8x16_avgr_u:
  6094. {
  6095. SIMD_DOUBLE_OP(simde_wasm_u8x16_avgr);
  6096. break;
  6097. }
  6098. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  6099. {
  6100. SIMD_SINGLE_OP(simde_wasm_i16x8_extadd_pairwise_i8x16);
  6101. break;
  6102. }
  6103. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  6104. {
  6105. SIMD_SINGLE_OP(simde_wasm_u16x8_extadd_pairwise_u8x16);
  6106. break;
  6107. }
  6108. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  6109. {
  6110. SIMD_SINGLE_OP(simde_wasm_i32x4_extadd_pairwise_i16x8);
  6111. break;
  6112. }
  6113. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  6114. {
  6115. SIMD_SINGLE_OP(simde_wasm_u32x4_extadd_pairwise_u16x8);
  6116. break;
  6117. }
  6118. /* i16x8 operations */
  6119. case SIMD_i16x8_abs:
  6120. {
  6121. SIMD_SINGLE_OP(simde_wasm_i16x8_abs);
  6122. break;
  6123. }
  6124. case SIMD_i16x8_neg:
  6125. {
  6126. SIMD_SINGLE_OP(simde_wasm_i16x8_neg);
  6127. break;
  6128. }
  6129. case SIMD_i16x8_q15mulr_sat_s:
  6130. {
  6131. SIMD_DOUBLE_OP(simde_wasm_i16x8_q15mulr_sat);
  6132. break;
  6133. }
  6134. case SIMD_i16x8_all_true:
  6135. {
  6136. V128 v1 = POP_V128();
  6137. bool result = simde_wasm_i16x8_all_true(
  6138. SIMD_V128_TO_SIMDE_V128(v1));
  6139. addr_ret = GET_OFFSET();
  6140. frame_lp[addr_ret] = result;
  6141. break;
  6142. }
  6143. case SIMD_i16x8_bitmask:
  6144. {
  6145. V128 v1 = POP_V128();
  6146. uint32_t result = simde_wasm_i16x8_bitmask(
  6147. SIMD_V128_TO_SIMDE_V128(v1));
  6148. addr_ret = GET_OFFSET();
  6149. frame_lp[addr_ret] = result;
  6150. break;
  6151. }
  6152. case SIMD_i16x8_narrow_i32x4_s:
  6153. {
  6154. SIMD_DOUBLE_OP(simde_wasm_i16x8_narrow_i32x4);
  6155. break;
  6156. }
  6157. case SIMD_i16x8_narrow_i32x4_u:
  6158. {
  6159. SIMD_DOUBLE_OP(simde_wasm_u16x8_narrow_i32x4);
  6160. break;
  6161. }
  6162. case SIMD_i16x8_extend_low_i8x16_s:
  6163. {
  6164. SIMD_SINGLE_OP(simde_wasm_i16x8_extend_low_i8x16);
  6165. break;
  6166. }
  6167. case SIMD_i16x8_extend_high_i8x16_s:
  6168. {
  6169. SIMD_SINGLE_OP(simde_wasm_i16x8_extend_high_i8x16);
  6170. break;
  6171. }
  6172. case SIMD_i16x8_extend_low_i8x16_u:
  6173. {
  6174. SIMD_SINGLE_OP(simde_wasm_u16x8_extend_low_u8x16);
  6175. break;
  6176. }
  6177. case SIMD_i16x8_extend_high_i8x16_u:
  6178. {
  6179. SIMD_SINGLE_OP(simde_wasm_u16x8_extend_high_u8x16);
  6180. break;
  6181. }
  6182. case SIMD_i16x8_shl:
  6183. {
  6184. SIMD_LANE_SHIFT(simde_wasm_i16x8_shl);
  6185. break;
  6186. }
  6187. case SIMD_i16x8_shr_s:
  6188. {
  6189. SIMD_LANE_SHIFT(simde_wasm_i16x8_shr);
  6190. break;
  6191. }
  6192. case SIMD_i16x8_shr_u:
  6193. {
  6194. SIMD_LANE_SHIFT(simde_wasm_u16x8_shr);
  6195. break;
  6196. }
  6197. case SIMD_i16x8_add:
  6198. {
  6199. SIMD_DOUBLE_OP(simde_wasm_i16x8_add);
  6200. break;
  6201. }
  6202. case SIMD_i16x8_add_sat_s:
  6203. {
  6204. SIMD_DOUBLE_OP(simde_wasm_i16x8_add_sat);
  6205. break;
  6206. }
  6207. case SIMD_i16x8_add_sat_u:
  6208. {
  6209. SIMD_DOUBLE_OP(simde_wasm_u16x8_add_sat);
  6210. break;
  6211. }
  6212. case SIMD_i16x8_sub:
  6213. {
  6214. SIMD_DOUBLE_OP(simde_wasm_i16x8_sub);
  6215. break;
  6216. }
  6217. case SIMD_i16x8_sub_sat_s:
  6218. {
  6219. SIMD_DOUBLE_OP(simde_wasm_i16x8_sub_sat);
  6220. break;
  6221. }
  6222. case SIMD_i16x8_sub_sat_u:
  6223. {
  6224. SIMD_DOUBLE_OP(simde_wasm_u16x8_sub_sat);
  6225. break;
  6226. }
  6227. case SIMD_f64x2_nearest:
  6228. {
  6229. SIMD_SINGLE_OP(simde_wasm_f64x2_nearest);
  6230. break;
  6231. }
  6232. case SIMD_i16x8_mul:
  6233. {
  6234. SIMD_DOUBLE_OP(simde_wasm_i16x8_mul);
  6235. break;
  6236. }
  6237. case SIMD_i16x8_min_s:
  6238. {
  6239. SIMD_DOUBLE_OP(simde_wasm_i16x8_min);
  6240. break;
  6241. }
  6242. case SIMD_i16x8_min_u:
  6243. {
  6244. SIMD_DOUBLE_OP(simde_wasm_u16x8_min);
  6245. break;
  6246. }
  6247. case SIMD_i16x8_max_s:
  6248. {
  6249. SIMD_DOUBLE_OP(simde_wasm_i16x8_max);
  6250. break;
  6251. }
  6252. case SIMD_i16x8_max_u:
  6253. {
  6254. SIMD_DOUBLE_OP(simde_wasm_u16x8_max);
  6255. break;
  6256. }
  6257. case SIMD_i16x8_avgr_u:
  6258. {
  6259. SIMD_DOUBLE_OP(simde_wasm_u16x8_avgr);
  6260. break;
  6261. }
  6262. case SIMD_i16x8_extmul_low_i8x16_s:
  6263. {
  6264. SIMD_DOUBLE_OP(simde_wasm_i16x8_extmul_low_i8x16);
  6265. break;
  6266. }
  6267. case SIMD_i16x8_extmul_high_i8x16_s:
  6268. {
  6269. SIMD_DOUBLE_OP(simde_wasm_i16x8_extmul_high_i8x16);
  6270. break;
  6271. }
  6272. case SIMD_i16x8_extmul_low_i8x16_u:
  6273. {
  6274. SIMD_DOUBLE_OP(simde_wasm_u16x8_extmul_low_u8x16);
  6275. break;
  6276. }
  6277. case SIMD_i16x8_extmul_high_i8x16_u:
  6278. {
  6279. SIMD_DOUBLE_OP(simde_wasm_u16x8_extmul_high_u8x16);
  6280. break;
  6281. }
  6282. /* i32x4 operations */
  6283. case SIMD_i32x4_abs:
  6284. {
  6285. SIMD_SINGLE_OP(simde_wasm_i32x4_abs);
  6286. break;
  6287. }
  6288. case SIMD_i32x4_neg:
  6289. {
  6290. SIMD_SINGLE_OP(simde_wasm_i32x4_neg);
  6291. break;
  6292. }
  6293. case SIMD_i32x4_all_true:
  6294. {
  6295. V128 v1 = POP_V128();
  6296. bool result = simde_wasm_i32x4_all_true(
  6297. SIMD_V128_TO_SIMDE_V128(v1));
  6298. addr_ret = GET_OFFSET();
  6299. frame_lp[addr_ret] = result;
  6300. break;
  6301. }
  6302. case SIMD_i32x4_bitmask:
  6303. {
  6304. V128 v1 = POP_V128();
  6305. uint32_t result = simde_wasm_i32x4_bitmask(
  6306. SIMD_V128_TO_SIMDE_V128(v1));
  6307. addr_ret = GET_OFFSET();
  6308. frame_lp[addr_ret] = result;
  6309. break;
  6310. }
  6311. case SIMD_i32x4_extend_low_i16x8_s:
  6312. {
  6313. SIMD_SINGLE_OP(simde_wasm_i32x4_extend_low_i16x8);
  6314. break;
  6315. }
  6316. case SIMD_i32x4_extend_high_i16x8_s:
  6317. {
  6318. SIMD_SINGLE_OP(simde_wasm_i32x4_extend_high_i16x8);
  6319. break;
  6320. }
  6321. case SIMD_i32x4_extend_low_i16x8_u:
  6322. {
  6323. SIMD_SINGLE_OP(simde_wasm_u32x4_extend_low_u16x8);
  6324. break;
  6325. }
  6326. case SIMD_i32x4_extend_high_i16x8_u:
  6327. {
  6328. SIMD_SINGLE_OP(simde_wasm_u32x4_extend_high_u16x8);
  6329. break;
  6330. }
  6331. case SIMD_i32x4_shl:
  6332. {
  6333. SIMD_LANE_SHIFT(simde_wasm_i32x4_shl);
  6334. break;
  6335. }
  6336. case SIMD_i32x4_shr_s:
  6337. {
  6338. SIMD_LANE_SHIFT(simde_wasm_i32x4_shr);
  6339. break;
  6340. }
  6341. case SIMD_i32x4_shr_u:
  6342. {
  6343. SIMD_LANE_SHIFT(simde_wasm_u32x4_shr);
  6344. break;
  6345. }
  6346. case SIMD_i32x4_add:
  6347. {
  6348. SIMD_DOUBLE_OP(simde_wasm_i32x4_add);
  6349. break;
  6350. }
  6351. case SIMD_i32x4_sub:
  6352. {
  6353. SIMD_DOUBLE_OP(simde_wasm_i32x4_sub);
  6354. break;
  6355. }
  6356. case SIMD_i32x4_mul:
  6357. {
  6358. SIMD_DOUBLE_OP(simde_wasm_i32x4_mul);
  6359. break;
  6360. }
  6361. case SIMD_i32x4_min_s:
  6362. {
  6363. SIMD_DOUBLE_OP(simde_wasm_i32x4_min);
  6364. break;
  6365. }
  6366. case SIMD_i32x4_min_u:
  6367. {
  6368. SIMD_DOUBLE_OP(simde_wasm_u32x4_min);
  6369. break;
  6370. }
  6371. case SIMD_i32x4_max_s:
  6372. {
  6373. SIMD_DOUBLE_OP(simde_wasm_i32x4_max);
  6374. break;
  6375. }
  6376. case SIMD_i32x4_max_u:
  6377. {
  6378. SIMD_DOUBLE_OP(simde_wasm_u32x4_max);
  6379. break;
  6380. }
  6381. case SIMD_i32x4_dot_i16x8_s:
  6382. {
  6383. SIMD_DOUBLE_OP(simde_wasm_i32x4_dot_i16x8);
  6384. break;
  6385. }
  6386. case SIMD_i32x4_extmul_low_i16x8_s:
  6387. {
  6388. SIMD_DOUBLE_OP(simde_wasm_i32x4_extmul_low_i16x8);
  6389. break;
  6390. }
  6391. case SIMD_i32x4_extmul_high_i16x8_s:
  6392. {
  6393. SIMD_DOUBLE_OP(simde_wasm_i32x4_extmul_high_i16x8);
  6394. break;
  6395. }
  6396. case SIMD_i32x4_extmul_low_i16x8_u:
  6397. {
  6398. SIMD_DOUBLE_OP(simde_wasm_u32x4_extmul_low_u16x8);
  6399. break;
  6400. }
  6401. case SIMD_i32x4_extmul_high_i16x8_u:
  6402. {
  6403. SIMD_DOUBLE_OP(simde_wasm_u32x4_extmul_high_u16x8);
  6404. break;
  6405. }
  6406. /* i64x2 operations */
  6407. case SIMD_i64x2_abs:
  6408. {
  6409. SIMD_SINGLE_OP(simde_wasm_i64x2_abs);
  6410. break;
  6411. }
  6412. case SIMD_i64x2_neg:
  6413. {
  6414. SIMD_SINGLE_OP(simde_wasm_i64x2_neg);
  6415. break;
  6416. }
  6417. case SIMD_i64x2_all_true:
  6418. {
  6419. V128 v1 = POP_V128();
  6420. bool result = simde_wasm_i64x2_all_true(
  6421. SIMD_V128_TO_SIMDE_V128(v1));
  6422. addr_ret = GET_OFFSET();
  6423. frame_lp[addr_ret] = result;
  6424. break;
  6425. }
  6426. case SIMD_i64x2_bitmask:
  6427. {
  6428. V128 v1 = POP_V128();
  6429. uint32_t result = simde_wasm_i64x2_bitmask(
  6430. SIMD_V128_TO_SIMDE_V128(v1));
  6431. addr_ret = GET_OFFSET();
  6432. frame_lp[addr_ret] = result;
  6433. break;
  6434. }
  6435. case SIMD_i64x2_extend_low_i32x4_s:
  6436. {
  6437. SIMD_SINGLE_OP(simde_wasm_i64x2_extend_low_i32x4);
  6438. break;
  6439. }
  6440. case SIMD_i64x2_extend_high_i32x4_s:
  6441. {
  6442. SIMD_SINGLE_OP(simde_wasm_i64x2_extend_high_i32x4);
  6443. break;
  6444. }
  6445. case SIMD_i64x2_extend_low_i32x4_u:
  6446. {
  6447. SIMD_SINGLE_OP(simde_wasm_u64x2_extend_low_u32x4);
  6448. break;
  6449. }
  6450. case SIMD_i64x2_extend_high_i32x4_u:
  6451. {
  6452. SIMD_SINGLE_OP(simde_wasm_u64x2_extend_high_u32x4);
  6453. break;
  6454. }
  6455. case SIMD_i64x2_shl:
  6456. {
  6457. SIMD_LANE_SHIFT(simde_wasm_i64x2_shl);
  6458. break;
  6459. }
  6460. case SIMD_i64x2_shr_s:
  6461. {
  6462. SIMD_LANE_SHIFT(simde_wasm_i64x2_shr);
  6463. break;
  6464. }
  6465. case SIMD_i64x2_shr_u:
  6466. {
  6467. SIMD_LANE_SHIFT(simde_wasm_u64x2_shr);
  6468. break;
  6469. }
  6470. case SIMD_i64x2_add:
  6471. {
  6472. SIMD_DOUBLE_OP(simde_wasm_i64x2_add);
  6473. break;
  6474. }
  6475. case SIMD_i64x2_sub:
  6476. {
  6477. SIMD_DOUBLE_OP(simde_wasm_i64x2_sub);
  6478. break;
  6479. }
  6480. case SIMD_i64x2_mul:
  6481. {
  6482. SIMD_DOUBLE_OP(simde_wasm_i64x2_mul);
  6483. break;
  6484. }
  6485. case SIMD_i64x2_eq:
  6486. {
  6487. SIMD_DOUBLE_OP(simde_wasm_i64x2_eq);
  6488. break;
  6489. }
  6490. case SIMD_i64x2_ne:
  6491. {
  6492. SIMD_DOUBLE_OP(simde_wasm_i64x2_ne);
  6493. break;
  6494. }
  6495. case SIMD_i64x2_lt_s:
  6496. {
  6497. SIMD_DOUBLE_OP(simde_wasm_i64x2_lt);
  6498. break;
  6499. }
  6500. case SIMD_i64x2_gt_s:
  6501. {
  6502. SIMD_DOUBLE_OP(simde_wasm_i64x2_gt);
  6503. break;
  6504. }
  6505. case SIMD_i64x2_le_s:
  6506. {
  6507. SIMD_DOUBLE_OP(simde_wasm_i64x2_le);
  6508. break;
  6509. }
  6510. case SIMD_i64x2_ge_s:
  6511. {
  6512. SIMD_DOUBLE_OP(simde_wasm_i64x2_ge);
  6513. break;
  6514. }
  6515. case SIMD_i64x2_extmul_low_i32x4_s:
  6516. {
  6517. SIMD_DOUBLE_OP(simde_wasm_i64x2_extmul_low_i32x4);
  6518. break;
  6519. }
  6520. case SIMD_i64x2_extmul_high_i32x4_s:
  6521. {
  6522. SIMD_DOUBLE_OP(simde_wasm_i64x2_extmul_high_i32x4);
  6523. break;
  6524. }
  6525. case SIMD_i64x2_extmul_low_i32x4_u:
  6526. {
  6527. SIMD_DOUBLE_OP(simde_wasm_u64x2_extmul_low_u32x4);
  6528. break;
  6529. }
  6530. case SIMD_i64x2_extmul_high_i32x4_u:
  6531. {
  6532. SIMD_DOUBLE_OP(simde_wasm_u64x2_extmul_high_u32x4);
  6533. break;
  6534. }
  6535. /* f32x4 opertions */
  6536. case SIMD_f32x4_abs:
  6537. {
  6538. SIMD_SINGLE_OP(simde_wasm_f32x4_abs);
  6539. break;
  6540. }
  6541. case SIMD_f32x4_neg:
  6542. {
  6543. SIMD_SINGLE_OP(simde_wasm_f32x4_neg);
  6544. break;
  6545. }
  6546. case SIMD_f32x4_sqrt:
  6547. {
  6548. SIMD_SINGLE_OP(simde_wasm_f32x4_sqrt);
  6549. break;
  6550. }
  6551. case SIMD_f32x4_add:
  6552. {
  6553. SIMD_DOUBLE_OP(simde_wasm_f32x4_add);
  6554. break;
  6555. }
  6556. case SIMD_f32x4_sub:
  6557. {
  6558. SIMD_DOUBLE_OP(simde_wasm_f32x4_sub);
  6559. break;
  6560. }
  6561. case SIMD_f32x4_mul:
  6562. {
  6563. SIMD_DOUBLE_OP(simde_wasm_f32x4_mul);
  6564. break;
  6565. }
  6566. case SIMD_f32x4_div:
  6567. {
  6568. SIMD_DOUBLE_OP(simde_wasm_f32x4_div);
  6569. break;
  6570. }
  6571. case SIMD_f32x4_min:
  6572. {
  6573. SIMD_DOUBLE_OP(simde_wasm_f32x4_min);
  6574. break;
  6575. }
  6576. case SIMD_f32x4_max:
  6577. {
  6578. SIMD_DOUBLE_OP(simde_wasm_f32x4_max);
  6579. break;
  6580. }
  6581. case SIMD_f32x4_pmin:
  6582. {
  6583. SIMD_DOUBLE_OP(simde_wasm_f32x4_pmin);
  6584. break;
  6585. }
  6586. case SIMD_f32x4_pmax:
  6587. {
  6588. SIMD_DOUBLE_OP(simde_wasm_f32x4_pmax);
  6589. break;
  6590. }
  6591. /* f64x2 operations */
  6592. case SIMD_f64x2_abs:
  6593. {
  6594. SIMD_SINGLE_OP(simde_wasm_f64x2_abs);
  6595. break;
  6596. }
  6597. case SIMD_f64x2_neg:
  6598. {
  6599. SIMD_SINGLE_OP(simde_wasm_f64x2_neg);
  6600. break;
  6601. }
  6602. case SIMD_f64x2_sqrt:
  6603. {
  6604. SIMD_SINGLE_OP(simde_wasm_f64x2_sqrt);
  6605. break;
  6606. }
  6607. case SIMD_f64x2_add:
  6608. {
  6609. SIMD_DOUBLE_OP(simde_wasm_f64x2_add);
  6610. break;
  6611. }
  6612. case SIMD_f64x2_sub:
  6613. {
  6614. SIMD_DOUBLE_OP(simde_wasm_f64x2_sub);
  6615. break;
  6616. }
  6617. case SIMD_f64x2_mul:
  6618. {
  6619. SIMD_DOUBLE_OP(simde_wasm_f64x2_mul);
  6620. break;
  6621. }
  6622. case SIMD_f64x2_div:
  6623. {
  6624. SIMD_DOUBLE_OP(simde_wasm_f64x2_div);
  6625. break;
  6626. }
  6627. case SIMD_f64x2_min:
  6628. {
  6629. SIMD_DOUBLE_OP(simde_wasm_f64x2_min);
  6630. break;
  6631. }
  6632. case SIMD_f64x2_max:
  6633. {
  6634. SIMD_DOUBLE_OP(simde_wasm_f64x2_max);
  6635. break;
  6636. }
  6637. case SIMD_f64x2_pmin:
  6638. {
  6639. SIMD_DOUBLE_OP(simde_wasm_f64x2_pmin);
  6640. break;
  6641. }
  6642. case SIMD_f64x2_pmax:
  6643. {
  6644. SIMD_DOUBLE_OP(simde_wasm_f64x2_pmax);
  6645. break;
  6646. }
  6647. /* Conversion operations */
  6648. case SIMD_i32x4_trunc_sat_f32x4_s:
  6649. {
  6650. SIMD_SINGLE_OP(simde_wasm_i32x4_trunc_sat_f32x4);
  6651. break;
  6652. }
  6653. case SIMD_i32x4_trunc_sat_f32x4_u:
  6654. {
  6655. SIMD_SINGLE_OP(simde_wasm_u32x4_trunc_sat_f32x4);
  6656. break;
  6657. }
  6658. case SIMD_f32x4_convert_i32x4_s:
  6659. {
  6660. SIMD_SINGLE_OP(simde_wasm_f32x4_convert_i32x4);
  6661. break;
  6662. }
  6663. case SIMD_f32x4_convert_i32x4_u:
  6664. {
  6665. SIMD_SINGLE_OP(simde_wasm_f32x4_convert_u32x4);
  6666. break;
  6667. }
  6668. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  6669. {
  6670. SIMD_SINGLE_OP(simde_wasm_i32x4_trunc_sat_f64x2_zero);
  6671. break;
  6672. }
  6673. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  6674. {
  6675. SIMD_SINGLE_OP(simde_wasm_u32x4_trunc_sat_f64x2_zero);
  6676. break;
  6677. }
  6678. case SIMD_f64x2_convert_low_i32x4_s:
  6679. {
  6680. SIMD_SINGLE_OP(simde_wasm_f64x2_convert_low_i32x4);
  6681. break;
  6682. }
  6683. case SIMD_f64x2_convert_low_i32x4_u:
  6684. {
  6685. SIMD_SINGLE_OP(simde_wasm_f64x2_convert_low_u32x4);
  6686. break;
  6687. }
  6688. default:
  6689. wasm_set_exception(module, "unsupported SIMD opcode");
  6690. }
  6691. HANDLE_OP_END();
  6692. }
  6693. #endif
  6694. HANDLE_OP(WASM_OP_CALL)
  6695. {
  6696. #if WASM_ENABLE_THREAD_MGR != 0
  6697. CHECK_SUSPEND_FLAGS();
  6698. #endif
  6699. fidx = read_uint32(frame_ip);
  6700. #if WASM_ENABLE_MULTI_MODULE != 0
  6701. if (fidx >= module->e->function_count) {
  6702. wasm_set_exception(module, "unknown function");
  6703. goto got_exception;
  6704. }
  6705. #endif
  6706. cur_func = module->e->functions + fidx;
  6707. goto call_func_from_interp;
  6708. }
  6709. #if WASM_ENABLE_TAIL_CALL != 0
  6710. HANDLE_OP(WASM_OP_RETURN_CALL)
  6711. {
  6712. #if WASM_ENABLE_THREAD_MGR != 0
  6713. CHECK_SUSPEND_FLAGS();
  6714. #endif
  6715. fidx = read_uint32(frame_ip);
  6716. #if WASM_ENABLE_MULTI_MODULE != 0
  6717. if (fidx >= module->e->function_count) {
  6718. wasm_set_exception(module, "unknown function");
  6719. goto got_exception;
  6720. }
  6721. #endif
  6722. cur_func = module->e->functions + fidx;
  6723. goto call_func_from_return_call;
  6724. }
  6725. #endif /* WASM_ENABLE_TAIL_CALL */
  6726. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  6727. default:
  6728. wasm_set_exception(module, "unsupported opcode");
  6729. goto got_exception;
  6730. }
  6731. #endif
  6732. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  6733. HANDLE_OP(WASM_OP_UNUSED_0x0a)
  6734. #if WASM_ENABLE_TAIL_CALL == 0
  6735. HANDLE_OP(WASM_OP_RETURN_CALL)
  6736. HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT)
  6737. #endif
  6738. #if WASM_ENABLE_SHARED_MEMORY == 0
  6739. HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
  6740. #endif
  6741. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  6742. HANDLE_OP(WASM_OP_TABLE_GET)
  6743. HANDLE_OP(WASM_OP_TABLE_SET)
  6744. HANDLE_OP(WASM_OP_REF_NULL)
  6745. HANDLE_OP(WASM_OP_REF_IS_NULL)
  6746. HANDLE_OP(WASM_OP_REF_FUNC)
  6747. #endif
  6748. #if WASM_ENABLE_GC == 0
  6749. /* SELECT_T is converted to SELECT or SELECT_64 */
  6750. HANDLE_OP(WASM_OP_SELECT_T)
  6751. HANDLE_OP(WASM_OP_CALL_REF)
  6752. HANDLE_OP(WASM_OP_RETURN_CALL_REF)
  6753. HANDLE_OP(WASM_OP_REF_EQ)
  6754. HANDLE_OP(WASM_OP_REF_AS_NON_NULL)
  6755. HANDLE_OP(WASM_OP_BR_ON_NULL)
  6756. HANDLE_OP(WASM_OP_BR_ON_NON_NULL)
  6757. HANDLE_OP(WASM_OP_GC_PREFIX)
  6758. #endif
  6759. #if WASM_ENABLE_EXCE_HANDLING == 0
  6760. /* if exception handling is disabled, these opcodes issue a trap */
  6761. HANDLE_OP(WASM_OP_TRY)
  6762. HANDLE_OP(WASM_OP_CATCH)
  6763. HANDLE_OP(WASM_OP_THROW)
  6764. HANDLE_OP(WASM_OP_RETHROW)
  6765. HANDLE_OP(WASM_OP_DELEGATE)
  6766. HANDLE_OP(WASM_OP_CATCH_ALL)
  6767. HANDLE_OP(EXT_OP_TRY)
  6768. #endif
  6769. HANDLE_OP(WASM_OP_UNUSED_0x16)
  6770. HANDLE_OP(WASM_OP_UNUSED_0x17)
  6771. HANDLE_OP(WASM_OP_UNUSED_0x27)
  6772. /* optimized op code */
  6773. HANDLE_OP(WASM_OP_F32_STORE)
  6774. HANDLE_OP(WASM_OP_F64_STORE)
  6775. HANDLE_OP(WASM_OP_F32_LOAD)
  6776. HANDLE_OP(WASM_OP_F64_LOAD)
  6777. HANDLE_OP(EXT_OP_GET_LOCAL_FAST)
  6778. HANDLE_OP(WASM_OP_GET_LOCAL)
  6779. HANDLE_OP(WASM_OP_DROP)
  6780. HANDLE_OP(WASM_OP_DROP_64)
  6781. HANDLE_OP(WASM_OP_BLOCK)
  6782. HANDLE_OP(WASM_OP_LOOP)
  6783. HANDLE_OP(WASM_OP_END)
  6784. HANDLE_OP(WASM_OP_NOP)
  6785. HANDLE_OP(EXT_OP_BLOCK)
  6786. HANDLE_OP(EXT_OP_LOOP)
  6787. HANDLE_OP(EXT_OP_IF)
  6788. HANDLE_OP(EXT_OP_BR_TABLE_CACHE)
  6789. #if WASM_ENABLE_SIMDE == 0
  6790. HANDLE_OP(WASM_OP_SIMD_PREFIX)
  6791. #endif
  6792. {
  6793. wasm_set_exception(module, "unsupported opcode");
  6794. goto got_exception;
  6795. }
  6796. #endif
  6797. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  6798. continue;
  6799. #else
  6800. FETCH_OPCODE_AND_DISPATCH();
  6801. #endif
  6802. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  6803. call_func_from_return_call:
  6804. {
  6805. uint32 *lp_base = NULL, *lp = NULL;
  6806. int i;
  6807. if (cur_func->param_cell_num > 0
  6808. && !(lp_base = lp = wasm_runtime_malloc(cur_func->param_cell_num
  6809. * sizeof(uint32)))) {
  6810. wasm_set_exception(module, "allocate memory failed");
  6811. goto got_exception;
  6812. }
  6813. for (i = 0; i < cur_func->param_count; i++) {
  6814. if (cur_func->param_types[i] == VALUE_TYPE_I64
  6815. || cur_func->param_types[i] == VALUE_TYPE_F64) {
  6816. PUT_I64_TO_ADDR(
  6817. lp, GET_OPERAND(uint64, I64,
  6818. 2 * (cur_func->param_count - i - 1)));
  6819. lp += 2;
  6820. }
  6821. else {
  6822. *lp = GET_OPERAND(uint32, I32,
  6823. (2 * (cur_func->param_count - i - 1)));
  6824. lp++;
  6825. }
  6826. }
  6827. frame->lp = frame->operand + cur_func->const_cell_num;
  6828. if (lp - lp_base > 0) {
  6829. word_copy(frame->lp, lp_base, lp - lp_base);
  6830. }
  6831. if (lp_base)
  6832. wasm_runtime_free(lp_base);
  6833. FREE_FRAME(exec_env, frame);
  6834. frame_ip += cur_func->param_count * sizeof(int16);
  6835. wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)prev_frame);
  6836. is_return_call = true;
  6837. goto call_func_from_entry;
  6838. }
  6839. #endif /* WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0 */
  6840. call_func_from_interp:
  6841. {
  6842. /* Only do the copy when it's called from interpreter. */
  6843. WASMInterpFrame *outs_area = wasm_exec_env_wasm_stack_top(exec_env);
  6844. int i;
  6845. #if WASM_ENABLE_MULTI_MODULE != 0
  6846. if (cur_func->is_import_func) {
  6847. outs_area->lp = outs_area->operand
  6848. + (cur_func->import_func_inst
  6849. ? cur_func->import_func_inst->const_cell_num
  6850. : 0);
  6851. }
  6852. else
  6853. #endif
  6854. {
  6855. outs_area->lp = outs_area->operand + cur_func->const_cell_num;
  6856. }
  6857. if ((uint8 *)(outs_area->lp + cur_func->param_cell_num)
  6858. > exec_env->wasm_stack.top_boundary) {
  6859. wasm_set_exception(module, "wasm operand stack overflow");
  6860. goto got_exception;
  6861. }
  6862. for (i = 0; i < cur_func->param_count; i++) {
  6863. if (cur_func->param_types[i] == VALUE_TYPE_V128) {
  6864. PUT_V128_TO_ADDR(
  6865. outs_area->lp,
  6866. GET_OPERAND_V128(2 * (cur_func->param_count - i - 1)));
  6867. outs_area->lp += 4;
  6868. }
  6869. else if (cur_func->param_types[i] == VALUE_TYPE_I64
  6870. || cur_func->param_types[i] == VALUE_TYPE_F64) {
  6871. PUT_I64_TO_ADDR(
  6872. outs_area->lp,
  6873. GET_OPERAND(uint64, I64,
  6874. 2 * (cur_func->param_count - i - 1)));
  6875. outs_area->lp += 2;
  6876. }
  6877. #if WASM_ENABLE_GC != 0
  6878. else if (wasm_is_type_reftype(cur_func->param_types[i])) {
  6879. PUT_REF_TO_ADDR(
  6880. outs_area->lp,
  6881. GET_OPERAND(void *, REF,
  6882. 2 * (cur_func->param_count - i - 1)));
  6883. CLEAR_FRAME_REF(
  6884. *(uint16 *)(frame_ip
  6885. + (2 * (cur_func->param_count - i - 1))));
  6886. outs_area->lp += REF_CELL_NUM;
  6887. }
  6888. #endif
  6889. else {
  6890. *outs_area->lp = GET_OPERAND(
  6891. uint32, I32, (2 * (cur_func->param_count - i - 1)));
  6892. outs_area->lp++;
  6893. }
  6894. }
  6895. frame_ip += cur_func->param_count * sizeof(int16);
  6896. if (cur_func->ret_cell_num != 0) {
  6897. /* Get the first return value's offset. Since loader emit
  6898. * all return values' offset so we must skip remain return
  6899. * values' offsets.
  6900. */
  6901. WASMFuncType *func_type;
  6902. if (cur_func->is_import_func)
  6903. func_type = cur_func->u.func_import->func_type;
  6904. else
  6905. func_type = cur_func->u.func->func_type;
  6906. frame->ret_offset = GET_OFFSET();
  6907. frame_ip += 2 * (func_type->result_count - 1);
  6908. }
  6909. SYNC_ALL_TO_FRAME();
  6910. prev_frame = frame;
  6911. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  6912. is_return_call = false;
  6913. #endif
  6914. }
  6915. call_func_from_entry:
  6916. {
  6917. if (cur_func->is_import_func) {
  6918. #if WASM_ENABLE_MULTI_MODULE != 0
  6919. if (cur_func->import_func_inst) {
  6920. wasm_interp_call_func_import(module, exec_env, cur_func,
  6921. prev_frame);
  6922. }
  6923. else
  6924. #endif
  6925. {
  6926. wasm_interp_call_func_native(module, exec_env, cur_func,
  6927. prev_frame);
  6928. }
  6929. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  6930. if (is_return_call) {
  6931. /* the frame was freed before tail calling and
  6932. the prev_frame was set as exec_env's cur_frame,
  6933. so here we recover context from prev_frame */
  6934. RECOVER_CONTEXT(prev_frame);
  6935. }
  6936. else
  6937. #endif
  6938. {
  6939. prev_frame = frame->prev_frame;
  6940. cur_func = frame->function;
  6941. UPDATE_ALL_FROM_FRAME();
  6942. }
  6943. /* update memory size, no need to update memory ptr as
  6944. it isn't changed in wasm_enlarge_memory */
  6945. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  6946. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  6947. || WASM_ENABLE_BULK_MEMORY != 0
  6948. if (memory)
  6949. linear_mem_size = GET_LINEAR_MEMORY_SIZE(memory);
  6950. #endif
  6951. if (wasm_copy_exception(module, NULL))
  6952. goto got_exception;
  6953. }
  6954. else {
  6955. WASMFunction *cur_wasm_func = cur_func->u.func;
  6956. uint32 cell_num_of_local_stack;
  6957. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  6958. uint32 i, local_cell_idx;
  6959. #endif
  6960. cell_num_of_local_stack = cur_func->param_cell_num
  6961. + cur_func->local_cell_num
  6962. + cur_wasm_func->max_stack_cell_num;
  6963. all_cell_num = cur_func->const_cell_num + cell_num_of_local_stack;
  6964. #if WASM_ENABLE_GC != 0
  6965. /* area of frame_ref */
  6966. all_cell_num += (cell_num_of_local_stack + 3) / 4;
  6967. /* cells occupied by locals, POP_REF should not clear frame_ref for
  6968. * these cells */
  6969. local_cell_num =
  6970. cur_func->param_cell_num + cur_func->local_cell_num;
  6971. #endif
  6972. /* param_cell_num, local_cell_num, const_cell_num and
  6973. max_stack_cell_num are all no larger than UINT16_MAX (checked
  6974. in loader), all_cell_num must be smaller than 1MB */
  6975. bh_assert(all_cell_num < 1 * BH_MB);
  6976. frame_size = wasm_interp_interp_frame_size(all_cell_num);
  6977. if (!(frame = ALLOC_FRAME(exec_env, frame_size, prev_frame))) {
  6978. frame = prev_frame;
  6979. goto got_exception;
  6980. }
  6981. /* Initialize the interpreter context. */
  6982. frame->function = cur_func;
  6983. frame_ip = wasm_get_func_code(cur_func);
  6984. frame_ip_end = wasm_get_func_code_end(cur_func);
  6985. frame_lp = frame->lp =
  6986. frame->operand + cur_wasm_func->const_cell_num;
  6987. /* Initialize the consts */
  6988. if (cur_wasm_func->const_cell_num > 0) {
  6989. word_copy(frame->operand, (uint32 *)cur_wasm_func->consts,
  6990. cur_wasm_func->const_cell_num);
  6991. }
  6992. /* Initialize the local variables */
  6993. memset(frame_lp + cur_func->param_cell_num, 0,
  6994. (uint32)(cur_func->local_cell_num * 4));
  6995. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  6996. /* externref/funcref should be NULL_REF rather than 0 */
  6997. local_cell_idx = cur_func->param_cell_num;
  6998. for (i = 0; i < cur_wasm_func->local_count; i++) {
  6999. if (cur_wasm_func->local_types[i] == VALUE_TYPE_EXTERNREF
  7000. || cur_wasm_func->local_types[i] == VALUE_TYPE_FUNCREF) {
  7001. *(frame_lp + local_cell_idx) = NULL_REF;
  7002. }
  7003. local_cell_idx +=
  7004. wasm_value_type_cell_num(cur_wasm_func->local_types[i]);
  7005. }
  7006. #endif
  7007. #if WASM_ENABLE_GC != 0
  7008. /* frame->ip is used during GC root set enumeration, so we must
  7009. * initialized this field here */
  7010. frame->ip = frame_ip;
  7011. frame_ref = frame->frame_ref =
  7012. (uint8 *)(frame->lp + (uint32)cell_num_of_local_stack);
  7013. init_frame_refs(frame_ref, (uint32)cell_num_of_local_stack,
  7014. cur_func);
  7015. #endif
  7016. wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)frame);
  7017. }
  7018. #if WASM_ENABLE_THREAD_MGR != 0
  7019. CHECK_SUSPEND_FLAGS();
  7020. #endif
  7021. HANDLE_OP_END();
  7022. }
  7023. return_func:
  7024. {
  7025. FREE_FRAME(exec_env, frame);
  7026. wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)prev_frame);
  7027. if (!prev_frame->ip)
  7028. /* Called from native. */
  7029. return;
  7030. RECOVER_CONTEXT(prev_frame);
  7031. #if WASM_ENABLE_GC != 0
  7032. local_cell_num = cur_func->param_cell_num + cur_func->local_cell_num;
  7033. #endif
  7034. HANDLE_OP_END();
  7035. }
  7036. (void)frame_ip_end;
  7037. #if WASM_ENABLE_SHARED_MEMORY != 0
  7038. unaligned_atomic:
  7039. wasm_set_exception(module, "unaligned atomic");
  7040. goto got_exception;
  7041. #endif
  7042. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  7043. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  7044. || WASM_ENABLE_BULK_MEMORY_OPT != 0
  7045. out_of_bounds:
  7046. wasm_set_exception(module, "out of bounds memory access");
  7047. #endif
  7048. got_exception:
  7049. SYNC_ALL_TO_FRAME();
  7050. return;
  7051. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  7052. }
  7053. #else
  7054. FETCH_OPCODE_AND_DISPATCH();
  7055. #endif
  7056. }
  7057. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7058. void **
  7059. wasm_interp_get_handle_table(void)
  7060. {
  7061. WASMModuleInstance module;
  7062. memset(&module, 0, sizeof(WASMModuleInstance));
  7063. wasm_interp_call_func_bytecode(&module, NULL, NULL, NULL);
  7064. return global_handle_table;
  7065. }
  7066. #endif
  7067. #if WASM_ENABLE_GC != 0
  7068. bool
  7069. wasm_interp_traverse_gc_rootset(WASMExecEnv *exec_env, void *heap)
  7070. {
  7071. WASMInterpFrame *frame;
  7072. WASMObjectRef gc_obj;
  7073. WASMFunctionInstance *cur_func;
  7074. uint8 *frame_ref;
  7075. uint32 local_cell_num, i;
  7076. frame = wasm_exec_env_get_cur_frame(exec_env);
  7077. for (; frame; frame = frame->prev_frame) {
  7078. frame_ref = frame->frame_ref;
  7079. cur_func = frame->function;
  7080. if (!cur_func)
  7081. continue;
  7082. local_cell_num = cur_func->param_cell_num;
  7083. if (frame->ip)
  7084. local_cell_num +=
  7085. cur_func->local_cell_num + cur_func->u.func->max_stack_cell_num;
  7086. for (i = 0; i < local_cell_num; i++) {
  7087. if (frame_ref[i]) {
  7088. gc_obj = GET_REF_FROM_ADDR(frame->lp + i);
  7089. if (wasm_obj_is_created_from_heap(gc_obj)) {
  7090. if (mem_allocator_add_root((mem_allocator_t)heap, gc_obj)) {
  7091. return false;
  7092. }
  7093. }
  7094. #if UINTPTR_MAX == UINT64_MAX
  7095. bh_assert(frame_ref[i + 1]);
  7096. i++;
  7097. #endif
  7098. }
  7099. }
  7100. }
  7101. return true;
  7102. }
  7103. #endif
  7104. void
  7105. wasm_interp_call_wasm(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
  7106. WASMFunctionInstance *function, uint32 argc,
  7107. uint32 argv[])
  7108. {
  7109. WASMRuntimeFrame *prev_frame = wasm_exec_env_get_cur_frame(exec_env);
  7110. WASMInterpFrame *frame, *outs_area;
  7111. /* Allocate sufficient cells for all kinds of return values. */
  7112. unsigned all_cell_num =
  7113. function->ret_cell_num > 2 ? function->ret_cell_num : 2,
  7114. i;
  7115. /* This frame won't be used by JITed code, so only allocate interp
  7116. frame here. */
  7117. unsigned frame_size;
  7118. #if WASM_ENABLE_GC != 0
  7119. all_cell_num += (all_cell_num + 3) / 4;
  7120. #endif
  7121. frame_size = wasm_interp_interp_frame_size(all_cell_num);
  7122. if (argc < function->param_cell_num) {
  7123. char buf[128];
  7124. snprintf(buf, sizeof(buf),
  7125. "invalid argument count %" PRIu32
  7126. ", must be no smaller than %" PRIu32,
  7127. argc, (uint32)function->param_cell_num);
  7128. wasm_set_exception(module_inst, buf);
  7129. return;
  7130. }
  7131. argc = function->param_cell_num;
  7132. #if defined(OS_ENABLE_HW_BOUND_CHECK) && WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
  7133. /*
  7134. * wasm_runtime_detect_native_stack_overflow is done by
  7135. * call_wasm_with_hw_bound_check.
  7136. */
  7137. #else
  7138. if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
  7139. return;
  7140. }
  7141. #endif
  7142. if (!(frame =
  7143. ALLOC_FRAME(exec_env, frame_size, (WASMInterpFrame *)prev_frame)))
  7144. return;
  7145. outs_area = wasm_exec_env_wasm_stack_top(exec_env);
  7146. frame->function = NULL;
  7147. frame->ip = NULL;
  7148. /* There is no local variable. */
  7149. frame->lp = frame->operand + 0;
  7150. #if WASM_ENABLE_GC != 0
  7151. frame->frame_ref =
  7152. (uint8 *)(frame->lp
  7153. + (function->ret_cell_num > 2 ? function->ret_cell_num : 2));
  7154. #endif
  7155. frame->ret_offset = 0;
  7156. if ((uint8 *)(outs_area->operand + function->const_cell_num + argc)
  7157. > exec_env->wasm_stack.top_boundary) {
  7158. wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
  7159. "wasm operand stack overflow");
  7160. return;
  7161. }
  7162. if (argc > 0)
  7163. word_copy(outs_area->operand + function->const_cell_num, argv, argc);
  7164. wasm_exec_env_set_cur_frame(exec_env, frame);
  7165. #if defined(os_writegsbase)
  7166. {
  7167. WASMMemoryInstance *memory_inst = wasm_get_default_memory(module_inst);
  7168. if (memory_inst)
  7169. /* write base addr of linear memory to GS segment register */
  7170. os_writegsbase(memory_inst->memory_data);
  7171. }
  7172. #endif
  7173. if (function->is_import_func) {
  7174. #if WASM_ENABLE_MULTI_MODULE != 0
  7175. if (function->import_module_inst) {
  7176. LOG_DEBUG("it is a function of a sub module");
  7177. wasm_interp_call_func_import(module_inst, exec_env, function,
  7178. frame);
  7179. }
  7180. else
  7181. #endif
  7182. {
  7183. LOG_DEBUG("it is an native function");
  7184. wasm_interp_call_func_native(module_inst, exec_env, function,
  7185. frame);
  7186. }
  7187. }
  7188. else {
  7189. wasm_interp_call_func_bytecode(module_inst, exec_env, function, frame);
  7190. }
  7191. /* Output the return value to the caller */
  7192. if (!wasm_copy_exception(module_inst, NULL)) {
  7193. for (i = 0; i < function->ret_cell_num; i++)
  7194. argv[i] = *(frame->lp + i);
  7195. }
  7196. else {
  7197. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  7198. if (wasm_interp_create_call_stack(exec_env)) {
  7199. wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
  7200. }
  7201. #endif
  7202. }
  7203. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  7204. FREE_FRAME(exec_env, frame);
  7205. #if WASM_ENABLE_OPCODE_COUNTER != 0
  7206. wasm_interp_dump_op_count();
  7207. #endif
  7208. }