wasm_interp_fast.c 299 KB

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