InterpreterStackFrame.cpp 374 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft Corporation and contributors. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "RuntimeLanguagePch.h"
  6. #include "RuntimeMathPch.h"
  7. #include "EHBailoutData.h"
  8. #include "Library/JavascriptRegularExpression.h"
  9. #if DBG_DUMP
  10. #include "ByteCode/OpCodeUtilAsmJs.h"
  11. #endif
  12. #include "Language/InterpreterStackFrame.h"
  13. #include "Library/JavascriptGeneratorFunction.h"
  14. #include "Library/ForInObjectEnumerator.h"
  15. #include "Library/AtomicsOperations.h"
  16. #include "../../WasmReader/WasmParseTree.h"
  17. ///----------------------------------------------------------------------------
  18. ///
  19. /// macros PROCESS_INtoOUT
  20. ///
  21. /// This set of macros defines standard patterns for processing OpCodes in
  22. /// RcInterpreter::Run(). Each macro is named for "in" - "out":
  23. /// - A: Var
  24. /// - I: Integer
  25. /// - R: Double
  26. /// - X: Nothing
  27. ///
  28. /// Examples:
  29. /// - "A2toA1" reads two registers, each storing an Var, and writes a single
  30. /// register with a new Var.
  31. /// - "A1I1toA2" reads two registers, first an Var and second an Int32, then
  32. /// writes two Var registers.
  33. ///
  34. /// Although these could use lookup tables to standard OpLayout types, this
  35. /// additional indirection would slow the main interpreter loop further by
  36. /// preventing the main 'switch' statement from using the OpCode to become a
  37. /// direct local-function jump.
  38. ///----------------------------------------------------------------------------
  39. #define PROCESS_FALLTHROUGH(name, func) \
  40. case OpCode::name:
  41. #define PROCESS_FALLTHROUGH_COMMON(name, func, suffix) \
  42. case OpCode::name:
  43. #define PROCESS_READ_LAYOUT(name, layout, suffix) \
  44. CompileAssert(OpCodeInfo<OpCode::name>::Layout == OpLayoutType::layout); \
  45. const unaligned OpLayout##layout##suffix * playout = m_reader.layout##suffix(ip); \
  46. Assert((playout != nullptr) == (Js::OpLayoutType::##layout != Js::OpLayoutType::Empty)); // Make sure playout is used
  47. #define PROCESS_NOP_COMMON(name, layout, suffix) \
  48. case OpCode::name: \
  49. { \
  50. PROCESS_READ_LAYOUT(name, layout, suffix); \
  51. break; \
  52. }
  53. #define PROCESS_NOP(name, layout) PROCESS_NOP_COMMON(name, layout,)
  54. #define PROCESS_CUSTOM_COMMON(name, func, layout, suffix) \
  55. case OpCode::name: \
  56. { \
  57. PROCESS_READ_LAYOUT(name, layout, suffix); \
  58. func(playout); \
  59. break; \
  60. }
  61. #define PROCESS_CUSTOM(name, func, layout) PROCESS_CUSTOM_COMMON(name, func, layout,)
  62. #define PROCESS_CUSTOM_L_COMMON(name, func, layout, regslot, suffix) \
  63. case OpCode::name: \
  64. { \
  65. PROCESS_READ_LAYOUT(name, layout, suffix); \
  66. func(playout); \
  67. break; \
  68. }
  69. #define PROCESS_CUSTOM_L(name, func, layout, regslot) PROCESS_CUSTOM_L_COMMON(name, func, layout, regslot,)
  70. #define PROCESS_CUSTOM_L_Arg_COMMON(name, func, suffix) PROCESS_CUSTOM_L_COMMON(name, func, Arg, Arg, suffix)
  71. #define PROCESS_CUSTOM_L_Arg2_COMMON(name, func, layout, suffix) PROCESS_CUSTOM_L_COMMON(name, func, layout, Arg, suffix)
  72. #define PROCESS_CUSTOM_L_Arg(name, func) PROCESS_CUSTOM_L_COMMON(name, func, Arg, Arg,)
  73. #define PROCESS_CUSTOM_ArgNoSrc_COMMON(name, func, suffix) PROCESS_CUSTOM_COMMON(name, func, ArgNoSrc, suffix)
  74. #define PROCESS_CUSTOM_ArgNoSrc(name, func) PROCESS_CUSTOM_COMMON(name, func, ArgNoSrc,)
  75. #define PROCESS_CUSTOM_L_R0_COMMON(name, func, layout, suffix) PROCESS_CUSTOM_L_COMMON(name, func, layout, R0, suffix)
  76. #define PROCESS_CUSTOM_L_R0(name, func, layout) PROCESS_CUSTOM_L_COMMON(name, func, layout, R0,)
  77. #define PROCESS_CUSTOM_L_Value_COMMON(name, func, layout, suffix) PROCESS_CUSTOM_L_COMMON(name, func, layout, Value, suffix)
  78. #define PROCESS_CUSTOM_L_Value(name, func, layout) PROCESS_CUSTOM_L_COMMON(name, func, layout, Value,)
  79. #define PROCESS_TRY(name, func) \
  80. case OpCode::name: \
  81. { \
  82. PROCESS_READ_LAYOUT(name, Br,); \
  83. func(playout); \
  84. ip = m_reader.GetIP(); \
  85. break; \
  86. }
  87. #define PROCESS_EMPTY(name, func) \
  88. case OpCode::name: \
  89. { \
  90. PROCESS_READ_LAYOUT(name, Empty, ); \
  91. func(); \
  92. ip = m_reader.GetIP(); \
  93. break; \
  94. }
  95. #define PROCESS_TRYBR2_COMMON(name, func, suffix) \
  96. case OpCode::name: \
  97. { \
  98. PROCESS_READ_LAYOUT(name, BrReg2, suffix); \
  99. func((const byte*)(playout + 1), playout->RelativeJumpOffset, playout->R1, playout->R2); \
  100. ip = m_reader.GetIP(); \
  101. break; \
  102. }
  103. #define PROCESS_CALL_COMMON(name, func, layout, suffix) \
  104. case OpCode::name: \
  105. { \
  106. PROCESS_READ_LAYOUT(name, layout, suffix); \
  107. func(playout); \
  108. break; \
  109. }
  110. #define PROCESS_CALL(name, func, layout) PROCESS_CALL_COMMON(name, func, layout,)
  111. #define PROCESS_CALL_FLAGS_None_COMMON(name, func, layout, suffix) PROCESS_CALL_COMMON(name, func, layout, suffix)
  112. #define PROCESS_CALL_FLAGS_Value_COMMON(name, func, layout, suffix) PROCESS_CALL_COMMON(name, func, layout, suffix)
  113. #define PROCESS_A1toXX_ALLOW_STACK_COMMON(name, func, suffix) \
  114. case OpCode::name: \
  115. { \
  116. PROCESS_READ_LAYOUT(name, Reg1, suffix); \
  117. func(GetRegAllowStackVar(playout->R0)); \
  118. break; \
  119. }
  120. #define PROCESS_A1toXX_COMMON(name, func, suffix) \
  121. case OpCode::name: \
  122. { \
  123. PROCESS_READ_LAYOUT(name, Reg1, suffix); \
  124. func(GetReg(playout->R0)); \
  125. break; \
  126. }
  127. #define PROCESS_A1toXX(name, func) PROCESS_A1toXX_COMMON(name, func,)
  128. #define PROCESS_A1toXXMem_COMMON(name, func, suffix) \
  129. case OpCode::name: \
  130. { \
  131. PROCESS_READ_LAYOUT(name, Reg1, suffix); \
  132. func(GetReg(playout->R0), GetScriptContext()); \
  133. break; \
  134. }
  135. #define PROCESS_A1toXXMem(name, func) PROCESS_A1toXXMem_COMMON(name, func,)
  136. #define PROCESS_A1toXXMemNonVar_COMMON(name, func, type, suffix) \
  137. case OpCode::name: \
  138. { \
  139. PROCESS_READ_LAYOUT(name, Reg1, suffix); \
  140. func((type)GetNonVarReg(playout->R0), GetScriptContext()); \
  141. break; \
  142. }
  143. #define PROCESS_A1toXXMemNonVar(name, func, type) PROCESS_A1toXXMemNonVar_COMMON(name, func, type,)
  144. #define PROCESS_XXtoA1_COMMON(name, func, suffix) \
  145. case OpCode::name: \
  146. { \
  147. PROCESS_READ_LAYOUT(name, Reg1, suffix); \
  148. SetReg(playout->R0, \
  149. func()); \
  150. break; \
  151. }
  152. #define PROCESS_XXtoA1(name, func) PROCESS_XXtoA1_COMMON(name, func,)
  153. #define PROCESS_XXtoA1NonVar_COMMON(name, func, suffix) \
  154. case OpCode::name: \
  155. { \
  156. PROCESS_READ_LAYOUT(name, Reg1, suffix); \
  157. SetNonVarReg(playout->R0, \
  158. func()); \
  159. break; \
  160. }
  161. #define PROCESS_XXtoA1NonVar(name, func) PROCESS_XXtoA1NonVar_COMMON(name, func,)
  162. #define PROCESS_XXtoA1Mem_COMMON(name, func, suffix) \
  163. case OpCode::name: \
  164. { \
  165. PROCESS_READ_LAYOUT(name, Reg1, suffix); \
  166. SetReg(playout->R0, \
  167. func(GetScriptContext())); \
  168. break; \
  169. }
  170. #define PROCESS_XXtoA1Mem(name, func) PROCESS_XXtoA1Mem_COMMON(name, func,)
  171. #define PROCESS_A1toA1_ALLOW_STACK_COMMON(name, func, suffix) \
  172. case OpCode::name: \
  173. { \
  174. PROCESS_READ_LAYOUT(name, Reg2, suffix); \
  175. SetRegAllowStackVar(playout->R0, \
  176. func(GetRegAllowStackVar(playout->R1))); \
  177. break; \
  178. }
  179. #define PROCESS_A1toA1_ALLOW_STACK(name, func) PROCESS_A1toA1_ALLOW_STACK_COMMON(name, func,)
  180. #define PROCESS_A1toA1_COMMON(name, func, suffix) \
  181. case OpCode::name: \
  182. { \
  183. PROCESS_READ_LAYOUT(name, Reg2, suffix); \
  184. SetReg(playout->R0, \
  185. func(GetReg(playout->R1))); \
  186. break; \
  187. }
  188. #define PROCESS_A1toA1(name, func) PROCESS_A1toA1_COMMON(name, func,)
  189. #define PROCESS_A1toA1Profiled_COMMON(name, func, suffix) \
  190. case OpCode::name: \
  191. { \
  192. PROCESS_READ_LAYOUT(name, ProfiledReg2, suffix); \
  193. SetReg(playout->R0, \
  194. func(GetReg(playout->R1), playout->profileId)); \
  195. break; \
  196. }
  197. #define PROCESS_A1toA1Profiled(name, func) PROCESS_A1toA1Profiled_COMMON(name, func,)
  198. #define PROCESS_A1toA1CallNoArg_COMMON(name, func, layout, suffix) \
  199. case OpCode::name: \
  200. { \
  201. PROCESS_READ_LAYOUT(name, layout, suffix); \
  202. SetReg(playout->R0, \
  203. func(playout)); \
  204. break; \
  205. }
  206. #define PROCESS_A1toA1CallNoArg(name, func, layout) PROCESS_A1toA1CallNoArg_COMMON(name, func, layout,)
  207. #define PROCESS_A1toA1Mem_COMMON(name, func, suffix) \
  208. case OpCode::name: \
  209. { \
  210. PROCESS_READ_LAYOUT(name, Reg2, suffix); \
  211. SetReg(playout->R0, \
  212. func(GetReg(playout->R1),GetScriptContext())); \
  213. break; \
  214. }
  215. #define PROCESS_A1toA1Mem(name, func) PROCESS_A1toA1Mem_COMMON(name, func,)
  216. #define PROCESS_A1toA1NonVar_COMMON(name, func, suffix) \
  217. case OpCode::name: \
  218. { \
  219. PROCESS_READ_LAYOUT(name, Reg2, suffix); \
  220. SetNonVarReg(playout->R0, \
  221. func(GetNonVarReg(playout->R1))); \
  222. break; \
  223. }
  224. #define PROCESS_A1toA1NonVar(name, func) PROCESS_A1toA1NonVar_COMMON(name, func,)
  225. #define PROCESS_A1toA1MemNonVar_COMMON(name, func, suffix) \
  226. case OpCode::name: \
  227. { \
  228. PROCESS_READ_LAYOUT(name, Reg2, suffix); \
  229. SetNonVarReg(playout->R0, \
  230. func(GetNonVarReg(playout->R1),GetScriptContext())); \
  231. break; \
  232. }
  233. #define PROCESS_A1toA1MemNonVar(name, func) PROCESS_A1toA1MemNonVar_COMMON(name, func,)
  234. #define PROCESS_SIZEtoA1MemNonVar_COMMON(name, func, suffix) \
  235. case OpCode::name: \
  236. { \
  237. PROCESS_READ_LAYOUT(name, Reg1Unsigned1, suffix); \
  238. SetNonVarReg(playout->R0, \
  239. func(playout->C1, GetScriptContext())); \
  240. break; \
  241. }
  242. #define PROCESS_SIZEtoA1MemNonVar(name, func) PROCESS_SIZEtoA1MemNonVar_COMMON(name, func,)
  243. #define PROCESS_INNERtoA1_COMMON(name, func, suffix) \
  244. case OpCode::name: \
  245. { \
  246. PROCESS_READ_LAYOUT(name, Reg1Unsigned1, suffix); \
  247. SetReg(playout->R0, InnerScopeFromIndex(playout->C1)); \
  248. break; \
  249. }
  250. #define PROCESS_INNERtoA1(name, fun) PROCESS_INNERtoA1_COMMON(name, func,)
  251. #define PROCESS_U1toINNERMemNonVar_COMMON(name, func, suffix) \
  252. case OpCode::name: \
  253. { \
  254. PROCESS_READ_LAYOUT(name, Unsigned1, suffix); \
  255. SetInnerScopeFromIndex(playout->C1, func(GetScriptContext())); \
  256. break; \
  257. }
  258. #define PROCESS_U1toINNERMemNonVar(name, func) PROCESS_U1toINNERMemNonVar_COMMON(name, func,)
  259. #define PROCESS_XXtoA2_FB_COMMON(name, func, suffix) \
  260. case OpCode::name: \
  261. { \
  262. PROCESS_READ_LAYOUT(name, Reg2U, suffix); \
  263. SetReg(playout->R0, \
  264. func(this->GetFrameDisplayForNestedFunc(), this->m_functionBody->GetNestedFuncReference(playout->SlotIndex), playout->R1)); \
  265. break; \
  266. }
  267. #define PROCESS_XXtoA2_FB(name, func) PROCESS_XXtoA2_FB_COMMON(name, func,)
  268. #define PROCESS_A1toA2_FB_COMMON(name, func, suffix) \
  269. case OpCode::name: \
  270. { \
  271. PROCESS_READ_LAYOUT(name, Reg3U, suffix); \
  272. SetReg(playout->R0, \
  273. func((FrameDisplay*)GetNonVarReg(playout->R2), this->m_functionBody->GetNestedFuncReference(playout->SlotIndex), playout->R1)); \
  274. break; \
  275. }
  276. #define PROCESS_A1toA2_FB(name, func) PROCESS_A1toA2_FB_COMMON(name, func,)
  277. #define PROCESS_A2toA2_FB_COMMON(name, func, suffix) \
  278. case OpCode::name: \
  279. { \
  280. PROCESS_READ_LAYOUT(name, Reg4U, suffix); \
  281. SetReg(playout->R0, \
  282. func(this->GetFrameDisplayForNestedFunc(), this->m_functionBody->GetNestedFuncReference(playout->SlotIndex), \
  283. GetReg(playout->R2), GetReg(playout->R3), playout->R1)); \
  284. break; \
  285. }
  286. #define PROCESS_A2toA2_FB(name, func) PROCESS_A2toA2_FB_COMMON(name, func,)
  287. #define PROCESS_A3toA2_FB_COMMON(name, func, suffix) \
  288. case OpCode::name: \
  289. { \
  290. PROCESS_READ_LAYOUT(name, Reg5U, suffix); \
  291. SetReg(playout->R0, \
  292. func((FrameDisplay*)GetNonVarReg(playout->R4), this->m_functionBody->GetNestedFuncReference(playout->SlotIndex), \
  293. GetReg(playout->R2), GetReg(playout->R3), playout->R1)); \
  294. break; \
  295. }
  296. #define PROCESS_A3toA2_FB(name, func) PROCESS_A3toA2_FB_COMMON(name, func,)
  297. #define PROCESS_XXINNERtoA1MemNonVar_COMMON(name, func, suffix) \
  298. case OpCode::name: \
  299. { \
  300. PROCESS_READ_LAYOUT(name, Reg1Unsigned1, suffix); \
  301. SetNonVarReg(playout->R0, \
  302. func(InnerScopeFromIndex(playout->C1), GetScriptContext())); \
  303. break; \
  304. }
  305. #define PROCESS_XXINNERtoA1MemNonVar(name, func) PROCESS_XXINNERtoA1MemNonVar_COMMON(name, func,)
  306. #define PROCESS_A1INNERtoA1MemNonVar_COMMON(name, func, suffix) \
  307. case OpCode::name: \
  308. { \
  309. PROCESS_READ_LAYOUT(name, Reg2Int1, suffix); \
  310. SetNonVarReg(playout->R0, \
  311. func(InnerScopeFromIndex(playout->C1), GetNonVarReg(playout->R1), GetScriptContext())); \
  312. break; \
  313. }
  314. #define PROCESS_A1LOCALtoA1MemNonVar(name, func) PROCESS_A1LOCALtoA1MemNonVar_COMMON(name, func,)
  315. #define PROCESS_LOCALI1toA1_COMMON(name, func, suffix) \
  316. case OpCode::name: \
  317. { \
  318. PROCESS_READ_LAYOUT(name, Reg1Unsigned1, suffix); \
  319. SetReg(playout->R0, \
  320. func(this->localClosure, playout->C1)); \
  321. break; \
  322. }
  323. #define PROCESS_LOCALI1toA1(name, func) PROCESS_LOCALI1toA1_COMMON(name, func,)
  324. #define PROCESS_A1I1toA1_COMMON(name, func, suffix) \
  325. case OpCode::name: \
  326. { \
  327. PROCESS_READ_LAYOUT(name, Reg2Int1, suffix); \
  328. SetReg(playout->R0, \
  329. func(GetReg(playout->R1), playout->C1)); \
  330. break; \
  331. }
  332. #define PROCESS_A1I1toA1(name, func) PROCESS_A1I1toA1_COMMON(name, func,)
  333. #define PROCESS_A1I1toA1Mem_COMMON(name, func, suffix) \
  334. case OpCode::name: \
  335. { \
  336. PROCESS_READ_LAYOUT(name, Reg2Int1, suffix); \
  337. SetReg(playout->R0, \
  338. func(GetReg(playout->R1), playout->C1, GetScriptContext())); \
  339. break; \
  340. }
  341. #define PROCESS_A1I1toA1Mem(name, func) PROCESS_A1I1toA1Mem_COMMON(name, func,)
  342. #define PROCESS_RegextoA1_COMMON(name, func, suffix) \
  343. case OpCode::name: \
  344. { \
  345. PROCESS_READ_LAYOUT(name, Reg1Unsigned1, suffix); \
  346. SetReg(playout->R0, \
  347. func(this->m_functionBody->GetLiteralRegex(playout->C1), GetScriptContext())); \
  348. break; \
  349. }
  350. #define PROCESS_RegextoA1(name, func) PROCESS_RegextoA1_COMMON(name, func,)
  351. #define PROCESS_A2toXX_COMMON(name, func, suffix) \
  352. case OpCode::name: \
  353. { \
  354. PROCESS_READ_LAYOUT(name, Reg2, suffix); \
  355. func(GetReg(playout->R0), GetReg(playout->R1)); \
  356. break; \
  357. }
  358. #define PROCESS_A2toXX(name, func) PROCESS_A2toXX_COMMON(name, func,)
  359. #define PROCESS_A2toXXMemNonVar_COMMON(name, func, suffix) \
  360. case OpCode::name: \
  361. { \
  362. PROCESS_READ_LAYOUT(name, Reg2, suffix); \
  363. func(GetNonVarReg(playout->R0), GetNonVarReg(playout->R1), GetScriptContext()); \
  364. break; \
  365. }
  366. #define PROCESS_A2toXXMemNonVar(name, func) PROCESS_A2toXXMemNonVar_COMMON(name, func,)
  367. #define PROCESS_A2toXXMem_COMMON(name, func, suffix) \
  368. case OpCode::name: \
  369. { \
  370. PROCESS_READ_LAYOUT(name, Reg2, suffix); \
  371. func(GetReg(playout->R0), GetReg(playout->R1), GetScriptContext()); \
  372. break; \
  373. }
  374. #define PROCESS_A2A2NonVartoXXMem(name, func) PROCESS_A2A2NonVartoXXMem_COMMON(name, func,)
  375. #define PROCESS_A2A2NonVartoXXMem_COMMON(name, func, suffix) \
  376. case OpCode::name: \
  377. { \
  378. PROCESS_READ_LAYOUT(name, Reg4, suffix); \
  379. func(GetReg(playout->R0), GetReg(playout->R1), GetNonVarReg(playout->R2), GetNonVarReg(playout->R3), GetScriptContext()); \
  380. break; \
  381. }
  382. #define PROCESS_A2toXXMem(name, func) PROCESS_A2toXXMem_COMMON(name, func,)
  383. #define PROCESS_A1NonVarToA1_COMMON(name, func, suffix) \
  384. case OpCode::name: \
  385. { \
  386. PROCESS_READ_LAYOUT(name, Reg2, suffix); \
  387. SetReg(playout->R0, \
  388. func(GetNonVarReg(playout->R1))); \
  389. break; \
  390. }
  391. #define PROCESS_A2NonVarToA1Reg_COMMON(name, func, suffix) \
  392. case OpCode::name: \
  393. { \
  394. PROCESS_READ_LAYOUT(name, Reg3, suffix); \
  395. SetReg(playout->R0, \
  396. func(GetNonVarReg(playout->R1), playout->R2)); \
  397. break; \
  398. }
  399. #define PROCESS_A2toA1Mem_COMMON(name, func, suffix) \
  400. case OpCode::name: \
  401. { \
  402. PROCESS_READ_LAYOUT(name, Reg3, suffix); \
  403. SetReg(playout->R0, \
  404. func(GetReg(playout->R1), GetReg(playout->R2),GetScriptContext())); \
  405. break; \
  406. }
  407. #define PROCESS_A2toA1Mem(name, func) PROCESS_A2toA1Mem_COMMON(name, func,)
  408. #define PROCESS_A2toA1MemProfiled_COMMON(name, func, suffix) \
  409. case OpCode::name: \
  410. { \
  411. PROCESS_READ_LAYOUT(name, ProfiledReg3, suffix); \
  412. SetReg(playout->R0, \
  413. func(GetReg(playout->R1), GetReg(playout->R2),GetScriptContext(), playout->profileId)); \
  414. break; \
  415. }
  416. #define PROCESS_A2toA1MemProfiled(name, func) PROCESS_A2toA1MemProfiled_COMMON(name, func,)
  417. #define PROCESS_A2toA1NonVar_COMMON(name, func, suffix) \
  418. case OpCode::name: \
  419. { \
  420. PROCESS_READ_LAYOUT(name, Reg3, suffix); \
  421. SetNonVarReg(playout->R0, \
  422. func(GetNonVarReg(playout->R1), GetNonVarReg(playout->R2))); \
  423. break; \
  424. }
  425. #define PROCESS_A2toA1NonVar(name, func) PROCESS_A2toA1NonVar_COMMON(name, func,)
  426. #define PROCESS_A2toA1MemNonVar_COMMON(name, func, suffix) \
  427. case OpCode::name: \
  428. { \
  429. PROCESS_READ_LAYOUT(name, Reg3, suffix); \
  430. SetNonVarReg(playout->R0, \
  431. func(GetNonVarReg(playout->R1), GetNonVarReg(playout->R2),GetScriptContext())); \
  432. break; \
  433. }
  434. #define PROCESS_A2toA1MemNonVar(name, func) PROCESS_A2toA1MemNonVar_COMMON(name, func,)
  435. #define PROCESS_CMMem_COMMON(name, func, suffix) \
  436. case OpCode::name: \
  437. { \
  438. PROCESS_READ_LAYOUT(name, Reg3, suffix); \
  439. SetReg(playout->R0, \
  440. func(GetReg(playout->R1), GetReg(playout->R2), GetScriptContext()) ? JavascriptBoolean::OP_LdTrue(GetScriptContext()) : \
  441. JavascriptBoolean::OP_LdFalse(GetScriptContext())); \
  442. break; \
  443. }
  444. #define PROCESS_CMMem(name, func) PROCESS_CMMem_COMMON(name, func,)
  445. #define PROCESS_ELEM_RtU_to_XX_COMMON(name, func, suffix) \
  446. case OpCode::name: \
  447. { \
  448. PROCESS_READ_LAYOUT(name, ElementRootU, suffix); \
  449. func(playout->PropertyIdIndex); \
  450. break; \
  451. }
  452. #define PROCESS_ELEM_RtU_to_XX(name, func) PROCESS_ELEM_RtU_to_XX_COMMON(name, func,)
  453. #define PROCESS_ELEM_C2_to_XX_COMMON(name, func, suffix) \
  454. case OpCode::name: \
  455. { \
  456. PROCESS_READ_LAYOUT(name, ElementScopedC, suffix); \
  457. func(GetEnvForEvalCode(), playout->PropertyIdIndex, GetReg(playout->Value)); \
  458. break; \
  459. }
  460. #define PROCESS_ELEM_C2_to_XX(name, func) PROCESS_ELEM_C2_to_XX_COMMON(name, func,)
  461. #define PROCESS_GET_ELEM_SLOT_FB_COMMON(name, func, suffix) \
  462. case OpCode::name: \
  463. { \
  464. PROCESS_READ_LAYOUT(name, ElementSlot, suffix); \
  465. SetReg(playout->Value, \
  466. func((FrameDisplay*)GetNonVarReg(playout->Instance), this->m_functionBody->GetNestedFuncReference(playout->SlotIndex))); \
  467. break; \
  468. }
  469. #define PROCESS_GET_ELEM_SLOT_FB(name, func) PROCESS_GET_ELEM_SLOT_FB_COMMON(name, func,)
  470. #define PROCESS_GET_ELEM_SLOT_FB_HMO_COMMON(name, func, suffix) \
  471. case OpCode::name: \
  472. { \
  473. PROCESS_READ_LAYOUT(name, ElementSlotI3, suffix); \
  474. SetReg(playout->Value, \
  475. func((FrameDisplay*)GetNonVarReg(playout->Instance), this->m_functionBody->GetNestedFuncReference(playout->SlotIndex), GetReg(playout->HomeObj))); \
  476. break; \
  477. }
  478. #define PROCESS_GET_ELEM_SLOT_FB_HMO(name, func) PROCESS_GET_ELEM_SLOT_FB_HMO_COMMON(name, func,)
  479. #define PROCESS_GET_SLOT_FB_COMMON(name, func, suffix) \
  480. case OpCode::name: \
  481. { \
  482. PROCESS_READ_LAYOUT(name, ElementSlotI1, suffix); \
  483. SetReg(playout->Value, \
  484. func(this->GetFrameDisplayForNestedFunc(), this->m_functionBody->GetNestedFuncReference(playout->SlotIndex))); \
  485. break; \
  486. }
  487. #define PROCESS_GET_SLOT_FB(name, func) PROCESS_GET_SLOT_FB_COMMON(name, func,)
  488. #define PROCESS_GET_SLOT_FB_HMO_COMMON(name, func, suffix) \
  489. case OpCode::name: \
  490. { \
  491. PROCESS_READ_LAYOUT(name, ElementSlot, suffix); \
  492. SetReg(playout->Value, \
  493. func(this->GetFrameDisplayForNestedFunc(), this->m_functionBody->GetNestedFuncReference(playout->SlotIndex), GetReg(playout->Instance))); \
  494. break; \
  495. }
  496. #define PROCESS_GET_SLOT_FB_HMO(name, func) PROCESS_GET_SLOT_FB_HMO_COMMON(name, func,)
  497. #define PROCESS_GET_ELEM_IMem_COMMON(name, func, suffix) \
  498. case OpCode::name: \
  499. { \
  500. PROCESS_READ_LAYOUT(name, ElementI, suffix); \
  501. SetReg(playout->Value, \
  502. func(GetReg(playout->Instance), GetReg(playout->Element), GetScriptContext())); \
  503. break; \
  504. }
  505. #define PROCESS_GET_ELEM_IMem(name, func) PROCESS_GET_ELEM_IMem_COMMON(name, func,)
  506. #define PROCESS_GET_ELEM_IMem_Strict_COMMON(name, func, suffix) \
  507. case OpCode::name: \
  508. { \
  509. PROCESS_READ_LAYOUT(name, ElementI, suffix); \
  510. SetReg(playout->Value, \
  511. func(GetReg(playout->Instance), GetReg(playout->Element), GetScriptContext(), PropertyOperation_StrictMode)); \
  512. break; \
  513. }
  514. #define PROCESS_GET_ELEM_IMem_Strict(name, func) PROCESS_GET_ELEM_IMem_Strict_COMMON(name, func,)
  515. #define PROCESS_BR(name, func) \
  516. case OpCode::name: \
  517. { \
  518. PROCESS_READ_LAYOUT(name, Br,); \
  519. ip = func(playout); \
  520. break; \
  521. }
  522. #ifdef BYTECODE_BRANCH_ISLAND
  523. #define PROCESS_BRLONG(name, func) \
  524. case OpCode::name: \
  525. { \
  526. PROCESS_READ_LAYOUT(name, BrLong,); \
  527. ip = func(playout); \
  528. break; \
  529. }
  530. #endif
  531. #define PROCESS_BRS(name,func) \
  532. case OpCode::name: \
  533. { \
  534. PROCESS_READ_LAYOUT(name, BrS,); \
  535. if (func(playout->val,GetScriptContext())) \
  536. { \
  537. ip = m_reader.SetCurrentRelativeOffset(ip, playout->RelativeJumpOffset); \
  538. } \
  539. break; \
  540. }
  541. #define PROCESS_BRB_COMMON(name, func, suffix) \
  542. case OpCode::name: \
  543. { \
  544. PROCESS_READ_LAYOUT(name, BrReg1, suffix); \
  545. if (func(GetReg(playout->R1))) \
  546. { \
  547. ip = m_reader.SetCurrentRelativeOffset(ip, playout->RelativeJumpOffset); \
  548. } \
  549. break; \
  550. }
  551. #define PROCESS_BRB(name, func) PROCESS_BRB_COMMON(name, func,)
  552. #define PROCESS_BRB_ALLOW_STACK_COMMON(name, func, suffix) \
  553. case OpCode::name: \
  554. { \
  555. PROCESS_READ_LAYOUT(name, BrReg1, suffix); \
  556. if (func(GetRegAllowStackVar(playout->R1))) \
  557. { \
  558. ip = m_reader.SetCurrentRelativeOffset(ip, playout->RelativeJumpOffset); \
  559. } \
  560. break; \
  561. }
  562. #define PROCESS_BRB_ALLOW_STACK(name, func) PROCESS_BRB_ALLOW_STACK_COMMON(name, func,)
  563. #define PROCESS_BRBS_COMMON(name, func, suffix) \
  564. case OpCode::name: \
  565. { \
  566. PROCESS_READ_LAYOUT(name, BrReg1, suffix); \
  567. if (func(GetReg(playout->R1), GetScriptContext())) \
  568. { \
  569. ip = m_reader.SetCurrentRelativeOffset(ip, playout->RelativeJumpOffset); \
  570. } \
  571. break; \
  572. }
  573. #define PROCESS_BRBS(name, func) PROCESS_BRBS_COMMON(name, func,)
  574. #define PROCESS_BRBReturnP1toA1_COMMON(name, func, suffix) \
  575. case OpCode::name: \
  576. { \
  577. PROCESS_READ_LAYOUT(name, BrReg1Unsigned1, suffix); \
  578. SetReg(playout->R1, func(GetForInEnumerator(playout->C2))); \
  579. if (!GetReg(playout->R1)) \
  580. { \
  581. ip = m_reader.SetCurrentRelativeOffset(ip, playout->RelativeJumpOffset); \
  582. } \
  583. break; \
  584. }
  585. #define PROCESS_BRBReturnP1toA1(name, func) PROCESS_BRBReturnP1toA1_COMMON(name, func,)
  586. #define PROCESS_BRBMem_ALLOW_STACK_COMMON(name, func, suffix) \
  587. case OpCode::name: \
  588. { \
  589. PROCESS_READ_LAYOUT(name, BrReg1, suffix); \
  590. if (func(GetRegAllowStackVar(playout->R1),GetScriptContext())) \
  591. { \
  592. ip = m_reader.SetCurrentRelativeOffset(ip, playout->RelativeJumpOffset); \
  593. } \
  594. break; \
  595. }
  596. #define PROCESS_BRBMem_ALLOW_STACK(name, func) PROCESS_BRBMem_ALLOW_STACK_COMMON(name, func,)
  597. #define PROCESS_BRCMem_COMMON(name, func,suffix) \
  598. case OpCode::name: \
  599. { \
  600. PROCESS_READ_LAYOUT(name, BrReg2, suffix); \
  601. if (func(GetReg(playout->R1), GetReg(playout->R2),GetScriptContext())) \
  602. { \
  603. ip = m_reader.SetCurrentRelativeOffset(ip, playout->RelativeJumpOffset); \
  604. } \
  605. break; \
  606. }
  607. #define PROCESS_BRCMem(name, func) PROCESS_BRCMem_COMMON(name, func,)
  608. #define PROCESS_BR_AtoA2_COMMON(name, func, suffix) \
  609. case OpCode::name: \
  610. { \
  611. PROCESS_READ_LAYOUT(name, BrReg3, suffix); \
  612. if (func(playout->R0, playout->R1, playout->R2)) \
  613. { \
  614. ip = m_reader.SetCurrentRelativeOffset(ip, playout->RelativeJumpOffset); \
  615. } \
  616. break; \
  617. }
  618. #define PROCESS_BR_AtoA2(name, func) PROCESS_BR_AtoA2_COMMON(name, func,)
  619. #define PROCESS_BRPROP(name, func) \
  620. case OpCode::name: \
  621. { \
  622. PROCESS_READ_LAYOUT(name, BrProperty,); \
  623. if (func(GetReg(playout->Instance), playout->PropertyIdIndex, GetScriptContext())) \
  624. { \
  625. ip = m_reader.SetCurrentRelativeOffset(ip, playout->RelativeJumpOffset); \
  626. } \
  627. break; \
  628. }
  629. #define PROCESS_BRLOCALPROP(name, func) \
  630. case OpCode::name: \
  631. { \
  632. PROCESS_READ_LAYOUT(name, BrLocalProperty,); \
  633. if (func(this->localClosure, playout->PropertyIdIndex, GetScriptContext())) \
  634. { \
  635. ip = m_reader.SetCurrentRelativeOffset(ip, playout->RelativeJumpOffset); \
  636. } \
  637. break; \
  638. }
  639. #define PROCESS_BRENVPROP(name, func) \
  640. case OpCode::name: \
  641. { \
  642. PROCESS_READ_LAYOUT(name, BrEnvProperty,); \
  643. if (func(LdEnv(), playout->SlotIndex, playout->PropertyIdIndex, GetScriptContext())) \
  644. { \
  645. ip = m_reader.SetCurrentRelativeOffset(ip, playout->RelativeJumpOffset); \
  646. } \
  647. break; \
  648. }
  649. #define PROCESS_W1(name, func) \
  650. case OpCode::name: \
  651. { \
  652. PROCESS_READ_LAYOUT(name, W1,); \
  653. func(playout->C1, GetScriptContext()); \
  654. break; \
  655. }
  656. #define PROCESS_U1toA1_COMMON(name, func, suffix) \
  657. case OpCode::name: \
  658. { \
  659. PROCESS_READ_LAYOUT(name, Reg1Unsigned1, suffix); \
  660. SetReg(playout->R0, \
  661. func(playout->C1,GetScriptContext())); \
  662. break; \
  663. }
  664. #define PROCESS_U1toA1(name, func) PROCESS_U1toA1_COMMON(name, func,)
  665. #define PROCESS_U1toA1NonVar_COMMON(name, func, suffix) \
  666. case OpCode::name: \
  667. { \
  668. PROCESS_READ_LAYOUT(name, Reg1Unsigned1, suffix); \
  669. SetNonVarReg(playout->R0, \
  670. func(playout->C1)); \
  671. break; \
  672. }
  673. #define PROCESS_U1toA1NonVar(name, func) PROCESS_U1toA1NonVar_COMMON(name, func,)
  674. #define PROCESS_U1toA1NonVar_FuncBody_COMMON(name, func, suffix) \
  675. case OpCode::name: \
  676. { \
  677. PROCESS_READ_LAYOUT(name, Reg1Unsigned1, suffix); \
  678. SetNonVarReg(playout->R0, \
  679. func(playout->C1,GetScriptContext(), this->m_functionBody)); \
  680. break; \
  681. }
  682. #define PROCESS_U1toA1NonVar_FuncBody(name, func) PROCESS_U1toA1NonVar_FuncBody_COMMON(name, func,)
  683. #define PROCESS_A1I2toXXNonVar_FuncBody(name, func) PROCESS_A1I2toXXNonVar_FuncBody_COMMON(name, func,)
  684. #define PROCESS_A1I2toXXNonVar_FuncBody_COMMON(name, func, suffix) \
  685. case OpCode::name: \
  686. { \
  687. PROCESS_READ_LAYOUT(name, Reg3, suffix); \
  688. func(playout->R0, playout->R1, playout->R2, GetScriptContext(), this->m_functionBody); \
  689. break; \
  690. }
  691. #define PROCESS_A1U1toXX_COMMON(name, func, suffix) \
  692. case OpCode::name: \
  693. { \
  694. PROCESS_READ_LAYOUT(name, Reg1Unsigned1, suffix); \
  695. func(GetReg(playout->R0), playout->C1); \
  696. break; \
  697. }
  698. #define PROCESS_A1U1toXX(name, func) PROCESS_A1U1toXX_COMMON(name, func,)
  699. #define PROCESS_A1U1toXXWithCache_COMMON(name, func, suffix) \
  700. case OpCode::name: \
  701. { \
  702. PROCESS_READ_LAYOUT(name, ProfiledReg1Unsigned1, suffix); \
  703. func(GetReg(playout->R0), playout->C1, playout->profileId); \
  704. break; \
  705. }
  706. #define PROCESS_A1U1toXXWithCache(name, func) PROCESS_A1U1toXXWithCache_COMMON(name, func,)
  707. #define PROCESS_EnvU1toXX_COMMON(name, func, suffix) \
  708. case OpCode::name: \
  709. { \
  710. PROCESS_READ_LAYOUT(name, Unsigned1, suffix); \
  711. func(LdEnv(), playout->C1); \
  712. break; \
  713. }
  714. #define PROCESS_EnvU1toXX(name, func) PROCESS_EnvU1toXX_COMMON(name, func,)
  715. #define PROCESS_GET_ELEM_SLOTNonVar_COMMON(name, func, layout, suffix) \
  716. case OpCode::name: \
  717. { \
  718. PROCESS_READ_LAYOUT(name, layout, suffix); \
  719. SetNonVarReg(playout->Value, func(GetNonVarReg(playout->Instance), playout)); \
  720. break; \
  721. }
  722. #define PROCESS_GET_ELEM_SLOTNonVar(name, func, layout) PROCESS_GET_ELEM_SLOTNonVar_COMMON(name, func, layout,)
  723. #define PROCESS_GET_ELEM_LOCALSLOTNonVar_COMMON(name, func, layout, suffix) \
  724. case OpCode::name: \
  725. { \
  726. PROCESS_READ_LAYOUT(name, layout, suffix); \
  727. SetNonVarReg(playout->Value, func((Var*)GetLocalClosure(), playout)); \
  728. break; \
  729. }
  730. #define PROCESS_GET_ELEM_LOCALSLOTNonVar(name, func, layout) PROCESS_GET_ELEM_LOCALSLOTNonVar_COMMON(name, func, layout,)
  731. #define PROCESS_GET_ELEM_PARAMSLOTNonVar_COMMON(name, func, layout, suffix) \
  732. case OpCode::name: \
  733. { \
  734. PROCESS_READ_LAYOUT(name, layout, suffix); \
  735. SetNonVarReg(playout->Value, func((Var*)GetParamClosure(), playout)); \
  736. break; \
  737. }
  738. #define PROCESS_GET_ELEM_PARAMSLOTNonVar(name, func, layout) PROCESS_GET_ELEM_PARAMSLOTNonVar_COMMON(name, func, layout,)
  739. #define PROCESS_GET_ELEM_INNERSLOTNonVar_COMMON(name, func, layout, suffix) \
  740. case OpCode::name: \
  741. { \
  742. PROCESS_READ_LAYOUT(name, layout, suffix); \
  743. SetNonVarReg(playout->Value, func(InnerScopeFromIndex(playout->SlotIndex1), playout)); \
  744. break; \
  745. }
  746. #define PROCESS_GET_ELEM_INNERSLOTNonVar(name, func, layout) PROCESS_GET_ELEM_INNERSLOTNonVar_COMMON(name, func, layout,)
  747. #define PROCESS_GET_ELEM_ENVSLOTNonVar_COMMON(name, func, layout, suffix) \
  748. case OpCode::name: \
  749. { \
  750. PROCESS_READ_LAYOUT(name, layout, suffix); \
  751. SetNonVarReg(playout->Value, func(LdEnv(), playout)); \
  752. break; \
  753. }
  754. #define PROCESS_GET_ELEM_ENVSLOTNonVar(name, func, layout) PROCESS_GET_ELEM_ENVSLOTNonVar_COMMON(name, func, layout,)
  755. #define PROCESS_SET_ELEM_SLOTNonVar_COMMON(name, func, suffix) \
  756. case OpCode::name: \
  757. { \
  758. PROCESS_READ_LAYOUT(name, ElementSlot, suffix); \
  759. func(GetNonVarReg(playout->Instance), playout->SlotIndex, GetRegAllowStackVarEnableOnly(playout->Value)); \
  760. break; \
  761. }
  762. #define PROCESS_SET_ELEM_SLOTNonVar(name, func) PROCESS_SET_ELEM_SLOTNonVar_COMMON(name, func,)
  763. #define PROCESS_SET_ELEM_SLOTMem_COMMON(name, func, suffix) \
  764. case OpCode::name: \
  765. { \
  766. PROCESS_READ_LAYOUT(name, ElementSlot, suffix); \
  767. func(GetNonVarReg(playout->Instance), playout->SlotIndex, GetReg(playout->Value), GetScriptContext()); \
  768. break; \
  769. }
  770. #define PROCESS_SET_ELEM_SLOTMem(name, func) PROCESS_SET_ELEM_SLOTMem_COMMON(name, func,)
  771. #define PROCESS_SET_ELEM_LOCALSLOTNonVar_COMMON(name, func, suffix) \
  772. case OpCode::name: \
  773. { \
  774. PROCESS_READ_LAYOUT(name, ElementSlotI1, suffix); \
  775. func((Var*)GetLocalClosure(), playout->SlotIndex, GetRegAllowStackVarEnableOnly(playout->Value)); \
  776. break; \
  777. }
  778. #define PROCESS_SET_ELEM_LOCALSLOTNonVar(name, func) PROCESS_SET_ELEM_LOCALSLOTNonVar_COMMON(name, func,)
  779. #define PROCESS_SET_ELEM_PARAMSLOTNonVar_COMMON(name, func, suffix) \
  780. case OpCode::name: \
  781. { \
  782. PROCESS_READ_LAYOUT(name, ElementSlotI1, suffix); \
  783. func((Var*)GetParamClosure(), playout->SlotIndex, GetRegAllowStackVarEnableOnly(playout->Value)); \
  784. break; \
  785. }
  786. #define PROCESS_SET_ELEM_PARAMSLOTNonVar(name, func) PROCESS_SET_ELEM_PARAMSLOTNonVar_COMMON(name, func,); \
  787. #define PROCESS_SET_ELEM_INNERSLOTNonVar_COMMON(name, func, suffix) \
  788. case OpCode::name: \
  789. { \
  790. PROCESS_READ_LAYOUT(name, ElementSlotI2, suffix); \
  791. func(InnerScopeFromIndex(playout->SlotIndex1), playout->SlotIndex2, GetRegAllowStackVarEnableOnly(playout->Value)); \
  792. break; \
  793. }
  794. #define PROCESS_SET_ELEM_INNERSLOTNonVar(name, func) PROCESS_SET_ELEM_INNERSLOTNonVar_COMMON(name, func,)
  795. #define PROCESS_SET_ELEM_ENVSLOTNonVar_COMMON(name, func, suffix) \
  796. case OpCode::name: \
  797. { \
  798. PROCESS_READ_LAYOUT(name, ElementSlotI2, suffix); \
  799. func(LdEnv(), playout->SlotIndex1, playout->SlotIndex2, GetRegAllowStackVarEnableOnly(playout->Value)); \
  800. break; \
  801. }
  802. #define PROCESS_SET_ELEM_ENVSLOTNonVar(name, func) PROCESS_SET_ELEM_ENVSLOTNonVar_COMMON(name, func,)
  803. /*---------------------------------------------------------------------------------------------- */
  804. #define PROCESS_A3toA1Mem_COMMON(name, func, suffix) \
  805. case OpCode::name: \
  806. { \
  807. PROCESS_READ_LAYOUT(name, Reg4, suffix); \
  808. SetReg(playout->R0, \
  809. func(GetReg(playout->R1), GetReg(playout->R2), GetReg(playout->R3), GetScriptContext())); \
  810. break; \
  811. }
  812. #define PROCESS_A3toA1Mem(name, func) PROCESS_A3toA1Mem_COMMON(name, func,)
  813. /*---------------------------------------------------------------------------------------------- */
  814. #define PROCESS_A2I1toA1Mem_COMMON(name, func, suffix) \
  815. case OpCode::name: \
  816. { \
  817. PROCESS_READ_LAYOUT(name, Reg3B1, suffix); \
  818. SetReg(playout->R0, \
  819. func(GetReg(playout->R1), GetReg(playout->R2), playout->B3, GetScriptContext())); \
  820. break; \
  821. }
  822. #define PROCESS_A2I1toA1Mem(name, func) PROCESS_A2I1toA1Mem_COMMON(name, func,)
  823. /*---------------------------------------------------------------------------------------------- */
  824. #define PROCESS_A2I1toXXMem_COMMON(name, func, suffix) \
  825. case OpCode::name: \
  826. { \
  827. PROCESS_READ_LAYOUT(name, Reg2B1, suffix); \
  828. func(GetReg(playout->R0), GetReg(playout->R1), playout->B2, scriptContext); \
  829. break; \
  830. }
  831. #define PROCESS_A2I1toXXMem(name, func) PROCESS_A2I1toXXMem_COMMON(name, func,)
  832. /*---------------------------------------------------------------------------------------------- */
  833. #define PROCESS_A3I1toXXMem_COMMON(name, func, suffix) \
  834. case OpCode::name: \
  835. { \
  836. PROCESS_READ_LAYOUT(name, Reg3B1, suffix); \
  837. func(GetReg(playout->R0), GetReg(playout->R1), GetReg(playout->R2), playout->B3, scriptContext); \
  838. break; \
  839. }
  840. #define PROCESS_A3I1toXXMem(name, func) PROCESS_A3I1toXXMem_COMMON(name, func,)
  841. #if ENABLE_PROFILE_INFO
  842. #define PROCESS_IP_TARG_IMPL(name, func, layoutSize) \
  843. case OpCode::name: \
  844. { \
  845. Assert(!switchProfileMode); \
  846. ip = func<layoutSize, INTERPRETERPROFILE>(ip); \
  847. if(switchProfileMode) \
  848. { \
  849. m_reader.SetIP(ip); \
  850. return nullptr; \
  851. } \
  852. break; \
  853. }
  854. #else
  855. #define PROCESS_IP_TARG_IMPL(name, func, layoutSize) \
  856. case OpCode::name: \
  857. { \
  858. ip = func<layoutSize, INTERPRETERPROFILE>(ip); \
  859. break; \
  860. }
  861. #endif
  862. #define PROCESS_IP_TARG_COMMON(name, func, suffix) PROCESS_IP_TARG##suffix(name, func)
  863. #define PROCESS_IP_TARG_Large(name, func) PROCESS_IP_TARG_IMPL(name, func, Js::LargeLayout)
  864. #define PROCESS_IP_TARG_Medium(name, func) PROCESS_IP_TARG_IMPL(name, func, Js::MediumLayout)
  865. #define PROCESS_IP_TARG_Small(name, func) PROCESS_IP_TARG_IMPL(name, func, Js::SmallLayout)
  866. #if ENABLE_TTD
  867. #if ENABLE_TTD_DIAGNOSTICS_TRACING
  868. #define SHOULD_DO_TTD_STACK_STMT_OP(CTX) ((CTX)->ShouldPerformRecordOrReplayAction())
  869. #else
  870. #define SHOULD_DO_TTD_STACK_STMT_OP(CTX) ((CTX)->ShouldPerformRecordOrReplayDebuggerAction())
  871. #endif
  872. #endif
  873. namespace Js
  874. {
  875. Var InterpreterStackFrame::InnerScopeFromRegSlot(RegSlot reg) const
  876. {
  877. return InnerScopeFromIndex(reg - m_functionBody->GetFirstInnerScopeRegister());
  878. }
  879. Var InterpreterStackFrame::InnerScopeFromIndex(uint32 index) const
  880. {
  881. if (index >= m_functionBody->GetInnerScopeCount())
  882. {
  883. AssertMsg(false, "Illegal byte code: bad inner scope index");
  884. Js::Throw::FatalInternalError();
  885. }
  886. Assert(this->innerScopeArray != nullptr);
  887. return this->innerScopeArray[index];
  888. }
  889. void InterpreterStackFrame::SetInnerScopeFromIndex(uint32 index, Var scope)
  890. {
  891. if (index >= m_functionBody->GetInnerScopeCount())
  892. {
  893. AssertMsg(false, "Illegal byte code: bad inner scope index");
  894. Js::Throw::FatalInternalError();
  895. }
  896. Assert(this->innerScopeArray != nullptr);
  897. this->innerScopeArray[index] = scope;
  898. }
  899. const int k_stackFrameVarCount = (sizeof(InterpreterStackFrame) + sizeof(Var) - 1) / sizeof(Var);
  900. InterpreterStackFrame::Setup::Setup(Js::ScriptFunction * function, Js::Arguments& args, bool bailedOut, bool inlinee, bool isGeneratorFrame)
  901. : function(function),
  902. inParams(args.Values),
  903. inSlotsCount(args.Info.Count),
  904. executeFunction(function->GetFunctionBody()),
  905. callFlags(args.Info.Flags),
  906. bailedOutOfInlinee(inlinee),
  907. bailedOut(bailedOut),
  908. isGeneratorFrame(isGeneratorFrame)
  909. {
  910. SetupInternal();
  911. }
  912. InterpreterStackFrame::Setup::Setup(Js::ScriptFunction * function, Var * inParams, int inSlotsCount)
  913. : function(function),
  914. inParams(inParams),
  915. inSlotsCount(inSlotsCount),
  916. executeFunction(function->GetFunctionBody()),
  917. callFlags(CallFlags_None),
  918. bailedOutOfInlinee(false),
  919. bailedOut(false),
  920. isGeneratorFrame(false)
  921. {
  922. SetupInternal();
  923. }
  924. void InterpreterStackFrame::Setup::SetupInternal()
  925. {
  926. if (this->function->GetHasInlineCaches() && Js::VarIs<Js::ScriptFunctionWithInlineCache>(this->function))
  927. {
  928. this->inlineCaches = (void**)Js::VarTo<Js::ScriptFunctionWithInlineCache>(this->function)->GetInlineCaches();
  929. Assert(this->inlineCaches != nullptr);
  930. }
  931. else
  932. {
  933. this->inlineCaches = this->executeFunction->GetInlineCaches();
  934. }
  935. this->inlineCacheCount = this->executeFunction->GetInlineCacheCount();
  936. //
  937. // Compute the amount of memory needed on the stack:
  938. // - We compute this in 'Atoms' instead of 'bytes' to keep everything natural word aligned.
  939. //
  940. this->localCount = this->executeFunction->GetLocalsCount();
  941. uint extraVarCount = 0;
  942. #if ENABLE_PROFILE_INFO
  943. if (Js::DynamicProfileInfo::EnableImplicitCallFlags(this->executeFunction))
  944. {
  945. extraVarCount += (sizeof(ImplicitCallFlags) * this->executeFunction->GetLoopCount() + sizeof(Var) - 1) / sizeof(Var);
  946. }
  947. #endif
  948. // If we bailed out, we will use the JIT frame's for..in enumerators.
  949. // But for generators, we will allocate space for them instead.
  950. uint forInVarCount = (bailedOut && !isGeneratorFrame) ? 0 : (this->executeFunction->GetForInLoopDepth() * (sizeof(Js::ForInObjectEnumerator) / sizeof(Var)));
  951. this->varAllocCount = k_stackFrameVarCount + localCount + this->executeFunction->GetOutParamMaxDepth() + forInVarCount +
  952. extraVarCount + this->executeFunction->GetInnerScopeCount();
  953. this->stackVarAllocCount = 0;
  954. if (this->executeFunction->DoStackNestedFunc() && this->executeFunction->GetNestedCount() != 0)
  955. {
  956. // Track stack funcs...
  957. this->stackVarAllocCount += (sizeof(StackScriptFunction) * this->executeFunction->GetNestedCount()) / sizeof(Var);
  958. if (!this->bailedOutOfInlinee)
  959. {
  960. // Frame display (if environment depth is statically known)...
  961. if (this->executeFunction->DoStackFrameDisplay())
  962. {
  963. uint16 envDepth = this->executeFunction->GetEnvDepth();
  964. Assert(envDepth != (uint16)-1);
  965. this->stackVarAllocCount += sizeof(FrameDisplay) / sizeof(Var) + (envDepth + 1);
  966. }
  967. // ...and scope slots (if any)
  968. if (this->executeFunction->DoStackScopeSlots())
  969. {
  970. uint32 scopeSlots = this->executeFunction->scopeSlotArraySize;
  971. Assert(scopeSlots != 0);
  972. this->stackVarAllocCount += scopeSlots + Js::ScopeSlots::FirstSlotIndex;
  973. }
  974. }
  975. }
  976. }
  977. InterpreterStackFrame *
  978. InterpreterStackFrame::Setup::InitializeAllocation(__in_ecount(varAllocCount) Var * allocation, __in_ecount(stackVarAllocCount) Var * stackAllocation
  979. , bool initParams, bool profileParams, LoopHeader* loopHeaderArray, DWORD_PTR stackAddr
  980. #if DBG
  981. , Var invalidStackVar
  982. #endif
  983. )
  984. {
  985. //
  986. // Initialize the new InterpreterStackFrame instance on the program stack.
  987. //
  988. //This will fail if InterpreterStackFrame ever gets a non-empty ctor (you'll need to use
  989. //placement_new(allocation, InterpreterStackFrame) instead, though that will cause problems
  990. //if the placement_new is surrounded by a try/finally since this would mix C++/SEH exception
  991. //handling.
  992. __analysis_assume(varAllocCount >= k_stackFrameVarCount + localCount);
  993. InterpreterStackFrame* newInstance = (InterpreterStackFrame*)allocation;
  994. newInstance->scriptContext = this->executeFunction->GetScriptContext();
  995. newInstance->m_inSlotsCount = this->inSlotsCount;
  996. newInstance->m_inParams = this->inParams;
  997. newInstance->m_callFlags = this->callFlags;
  998. newInstance->m_outParams = newInstance->m_localSlots + localCount;
  999. newInstance->m_outSp = newInstance->m_outParams;
  1000. newInstance->m_outSpCached = nullptr;
  1001. newInstance->m_arguments = NULL;
  1002. newInstance->function = this->function;
  1003. newInstance->m_functionBody = this->executeFunction;
  1004. newInstance->inlineCaches = this->inlineCaches;
  1005. newInstance->inlineCacheCount = this->inlineCacheCount;
  1006. newInstance->currentLoopNum = LoopHeader::NoLoop;
  1007. newInstance->currentLoopCounter = 0;
  1008. newInstance->m_flags = InterpreterStackFrameFlags_None;
  1009. newInstance->closureInitDone = false;
  1010. newInstance->isParamScopeDone = false;
  1011. newInstance->shouldCacheSP = true;
  1012. #if ENABLE_PROFILE_INFO
  1013. newInstance->switchProfileMode = false;
  1014. newInstance->isAutoProfiling = false;
  1015. newInstance->switchProfileModeOnLoopEndNumber = 0u - 1;
  1016. #endif
  1017. newInstance->ehBailoutData = nullptr;
  1018. newInstance->nestedTryDepth = -1;
  1019. newInstance->nestedCatchDepth = -1;
  1020. newInstance->nestedFinallyDepth = -1;
  1021. newInstance->retOffset = 0;
  1022. newInstance->localFrameDisplay = nullptr;
  1023. newInstance->localClosure = nullptr;
  1024. newInstance->paramClosure = nullptr;
  1025. newInstance->innerScopeArray = nullptr;
  1026. newInstance->m_asmJsBuffer = nullptr;
  1027. #ifdef ENABLE_WASM
  1028. newInstance->m_wasmMemory = nullptr;
  1029. #endif
  1030. Var* outparamsEnd = newInstance->m_outParams + this->executeFunction->GetOutParamMaxDepth();
  1031. #if DBG
  1032. newInstance->m_outParamsEnd = outparamsEnd;
  1033. #endif
  1034. bool doInterruptProbe = newInstance->scriptContext->GetThreadContext()->DoInterruptProbe(this->executeFunction);
  1035. #if ENABLE_NATIVE_CODEGEN
  1036. bool doJITLoopBody =
  1037. !this->executeFunction->GetScriptContext()->GetConfig()->IsNoNative() &&
  1038. !(this->executeFunction->GetHasTry() && (PHASE_OFF((Js::JITLoopBodyInTryCatchPhase), this->executeFunction))) &&
  1039. !(this->executeFunction->GetHasFinally() && (PHASE_OFF((Js::JITLoopBodyInTryFinallyPhase), this->executeFunction))) &&
  1040. (this->executeFunction->ForceJITLoopBody() || this->executeFunction->IsJitLoopBodyPhaseEnabled()) &&
  1041. !this->executeFunction->IsInDebugMode();
  1042. #endif
  1043. // Pick a version of the LoopBodyStart OpCode handlers that is hardcoded to do loop body JIT and
  1044. // interrupt probes as needed.
  1045. if (doInterruptProbe)
  1046. {
  1047. #if ENABLE_NATIVE_CODEGEN
  1048. if (doJITLoopBody)
  1049. {
  1050. newInstance->opProfiledLoopBodyStart = &InterpreterStackFrame::ProfiledLoopBodyStart<true, true>;
  1051. newInstance->opLoopBodyStart = &InterpreterStackFrame::LoopBodyStart<true, true>;
  1052. }
  1053. else
  1054. #endif
  1055. {
  1056. #if ENABLE_PROFILE_INFO
  1057. newInstance->opProfiledLoopBodyStart = &InterpreterStackFrame::ProfiledLoopBodyStart<true, false>;
  1058. #endif
  1059. newInstance->opLoopBodyStart = &InterpreterStackFrame::LoopBodyStart<true, false>;
  1060. }
  1061. }
  1062. else
  1063. {
  1064. #if ENABLE_NATIVE_CODEGEN
  1065. if (doJITLoopBody)
  1066. {
  1067. newInstance->opProfiledLoopBodyStart = &InterpreterStackFrame::ProfiledLoopBodyStart<false, true>;
  1068. newInstance->opLoopBodyStart = &InterpreterStackFrame::LoopBodyStart<false, true>;
  1069. }
  1070. else
  1071. #endif
  1072. {
  1073. #if ENABLE_PROFILE_INFO
  1074. newInstance->opProfiledLoopBodyStart = &InterpreterStackFrame::ProfiledLoopBodyStart<false, false>;
  1075. #endif
  1076. newInstance->opLoopBodyStart = &InterpreterStackFrame::LoopBodyStart<false, false>;
  1077. }
  1078. }
  1079. newInstance->loopHeaderArray = loopHeaderArray;
  1080. newInstance->m_stackAddress = stackAddr;
  1081. #if ENABLE_PROFILE_INFO
  1082. // the savedLoopImplicitCallFlags is allocated at the end of the out param array
  1083. newInstance->savedLoopImplicitCallFlags = nullptr;
  1084. #endif
  1085. char * nextAllocBytes = (char *)(outparamsEnd);
  1086. // If we bailed out, we will use the JIT frame's for..in enumerators
  1087. if (this->executeFunction->GetForInLoopDepth() == 0 || (!isGeneratorFrame && bailedOut))
  1088. {
  1089. newInstance->forInObjectEnumerators = nullptr;
  1090. }
  1091. else
  1092. {
  1093. newInstance->forInObjectEnumerators = (ForInObjectEnumerator *)nextAllocBytes;
  1094. nextAllocBytes += sizeof(ForInObjectEnumerator) * this->executeFunction->GetForInLoopDepth();
  1095. }
  1096. if (this->executeFunction->GetInnerScopeCount())
  1097. {
  1098. newInstance->innerScopeArray = (Var*)nextAllocBytes;
  1099. nextAllocBytes += this->executeFunction->GetInnerScopeCount() * sizeof(Var);
  1100. }
  1101. if (this->executeFunction->DoStackNestedFunc() && this->executeFunction->GetNestedCount() != 0)
  1102. {
  1103. char * stackAllocBytes = (stackAllocation != nullptr) ? (char*)stackAllocation : nextAllocBytes;
  1104. newInstance->InitializeStackFunctions((StackScriptFunction *)stackAllocBytes);
  1105. stackAllocBytes += sizeof(StackScriptFunction) * this->executeFunction->GetNestedCount();
  1106. if (!this->bailedOutOfInlinee)
  1107. {
  1108. if (this->executeFunction->DoStackFrameDisplay())
  1109. {
  1110. uint16 envDepth = this->executeFunction->GetEnvDepth();
  1111. Assert(envDepth != (uint16)-1);
  1112. newInstance->localFrameDisplay = (FrameDisplay*)stackAllocBytes;
  1113. newInstance->localFrameDisplay->SetLength(0); // Start with no scopes. It will get set in NewFrameDisplay
  1114. stackAllocBytes += sizeof(FrameDisplay) + (envDepth + 1) * sizeof(Var);
  1115. }
  1116. if (this->executeFunction->DoStackScopeSlots())
  1117. {
  1118. uint32 scopeSlots = this->executeFunction->scopeSlotArraySize;
  1119. Assert(scopeSlots != 0);
  1120. ScopeSlots((Field(Var)*)stackAllocBytes).SetCount(0); // Start with count as 0. It will get set in NewScopeSlots
  1121. newInstance->localClosure = stackAllocBytes;
  1122. stackAllocBytes += (scopeSlots + ScopeSlots::FirstSlotIndex) * sizeof(Var);
  1123. }
  1124. }
  1125. if (stackAllocation == nullptr)
  1126. {
  1127. nextAllocBytes = stackAllocBytes;
  1128. }
  1129. }
  1130. #if ENABLE_PROFILE_INFO
  1131. if (Js::DynamicProfileInfo::EnableImplicitCallFlags(this->executeFunction))
  1132. {
  1133. /*
  1134. __analysis_assume(varAllocCount == (k_stackFrameVarCount + localCount + executeFunction->GetOutParamMaxDepth()
  1135. + ((sizeof(ImplicitCallFlags) * executeFunction->GetLoopCount() + sizeof(Var) - 1) / sizeof(Var))));
  1136. */
  1137. newInstance->savedLoopImplicitCallFlags = (ImplicitCallFlags *)nextAllocBytes;
  1138. for (uint i = 0; i < this->executeFunction->GetLoopCount(); i++)
  1139. {
  1140. #pragma prefast(suppress:26015, "Above analysis assume doesn't work")
  1141. newInstance->savedLoopImplicitCallFlags[i] = ImplicitCall_None;
  1142. }
  1143. }
  1144. #endif
  1145. #if DBG
  1146. if (CONFIG_ISENABLED(InitializeInterpreterSlotsWithInvalidStackVarFlag))
  1147. {
  1148. // Fill the local slots with the invalid stack var so that we will crash deterministically if something goes wrong
  1149. for (uint i = 0; i < localCount; ++i)
  1150. {
  1151. newInstance->m_localSlots[i] = invalidStackVar;
  1152. }
  1153. }
  1154. else
  1155. {
  1156. memset(newInstance->m_localSlots, 0, sizeof(Js::Var) * localCount);
  1157. }
  1158. #else
  1159. if (newInstance->m_functionBody->IsInDebugMode())
  1160. {
  1161. // In the debug mode zero out the local slot, so this could prevent locals being uninitialized in the case of setNextStatement.
  1162. memset(newInstance->m_localSlots, 0, sizeof(Js::Var) * localCount);
  1163. }
  1164. else
  1165. {
  1166. Js::RegSlot varCount = function->GetFunctionBody()->GetVarCount();
  1167. if (varCount)
  1168. {
  1169. // Zero out the non-constant var slots.
  1170. Js::RegSlot constantCount = function->GetFunctionBody()->GetConstantCount();
  1171. memset(newInstance->m_localSlots + constantCount, 0, varCount * sizeof(Js::Var));
  1172. }
  1173. // Zero out the return slot. This is not a user local, so the byte code will not initialize
  1174. // it to "undefined". And it's not an expression temp, so, for instance, a jitted loop body may expect
  1175. // it to be valid on entry to the loop, where "valid" means either a var or null.
  1176. newInstance->SetNonVarReg(0, NULL);
  1177. }
  1178. #endif
  1179. // Wasm doesn't use const table
  1180. if (!executeFunction->IsWasmFunction())
  1181. {
  1182. // Initialize the low end of the local slots from the constant table.
  1183. // Skip the slot for the return value register.
  1184. this->executeFunction->InitConstantSlots(&newInstance->m_localSlots[FunctionBody::FirstRegSlot]);
  1185. }
  1186. // Set local FD/SS pointers to null until after we've successfully probed the stack in the process loop.
  1187. // That way we avoid trying to box these structures before they've been initialized in the byte code.
  1188. if (this->executeFunction->DoStackFrameDisplay())
  1189. {
  1190. newInstance->SetNonVarReg(executeFunction->GetLocalFrameDisplayRegister(), nullptr);
  1191. }
  1192. if (this->executeFunction->DoStackScopeSlots())
  1193. {
  1194. Assert(!executeFunction->HasScopeObject());
  1195. newInstance->SetNonVarReg(executeFunction->GetLocalClosureRegister(), nullptr);
  1196. }
  1197. Var *prestDest = &newInstance->m_localSlots[this->executeFunction->GetConstantCount()];
  1198. if (initParams)
  1199. {
  1200. #if ENABLE_PROFILE_INFO
  1201. Assert(!this->executeFunction->NeedEnsureDynamicProfileInfo());
  1202. #endif
  1203. if (profileParams)
  1204. {
  1205. #if ENABLE_PROFILE_INFO
  1206. Assert(this->executeFunction->HasExecutionDynamicProfileInfo());
  1207. #endif
  1208. FunctionBody* functionBody = this->executeFunction;
  1209. InitializeParams(newInstance, [functionBody](Var param, ArgSlot index)
  1210. {
  1211. #if ENABLE_PROFILE_INFO
  1212. functionBody->GetDynamicProfileInfo()->RecordParameterInfo(functionBody, index - 1, param);
  1213. #endif
  1214. }, &prestDest);
  1215. }
  1216. else
  1217. {
  1218. InitializeParams(newInstance, [](Var param, ArgSlot index) {}, &prestDest);
  1219. }
  1220. }
  1221. if (this->executeFunction->GetHasRestParameter())
  1222. {
  1223. InitializeRestParam(newInstance, prestDest);
  1224. }
  1225. Js::RegSlot envReg = executeFunction->GetEnvRegister();
  1226. if (envReg != Js::Constants::NoRegister && envReg < executeFunction->GetConstantCount())
  1227. {
  1228. Assert(this->executeFunction->GetThisRegisterForEventHandler() == Constants::NoRegister);
  1229. // The correct FD (possibly distinct from the one on the function) is passed in the constant table.
  1230. this->function->SetEnvironment((Js::FrameDisplay*)newInstance->GetNonVarReg(envReg));
  1231. }
  1232. return newInstance;
  1233. }
  1234. template <class Fn>
  1235. void InterpreterStackFrame::Setup::InitializeParams(InterpreterStackFrame * newInstance, Fn callback, Var **pprestDest)
  1236. {
  1237. ArgSlot requiredInParamCount = executeFunction->GetInParamsCount();
  1238. Assert(requiredInParamCount > 1);
  1239. if (this->inSlotsCount >= requiredInParamCount)
  1240. {
  1241. Var * pArg = &newInstance->m_localSlots[executeFunction->GetConstantCount()];
  1242. Var * paGivenSrc = this->inParams + 1;
  1243. ArgSlot paramIndex = 1;
  1244. do
  1245. {
  1246. Var src = *paGivenSrc++;
  1247. callback(src, paramIndex);
  1248. *pArg++ = src;
  1249. paramIndex++;
  1250. } while (paramIndex < requiredInParamCount);
  1251. *pprestDest = pArg;
  1252. }
  1253. else
  1254. {
  1255. InitializeParamsAndUndef(newInstance, callback, pprestDest);
  1256. }
  1257. }
  1258. template <class Fn>
  1259. void InterpreterStackFrame::Setup::InitializeParamsAndUndef(InterpreterStackFrame * newInstance, Fn callback, Var **pprestDest)
  1260. {
  1261. Var * pArg = &newInstance->m_localSlots[executeFunction->GetConstantCount()];
  1262. Var * paGivenSrc = this->inParams + 1;
  1263. ArgSlot requiredInParamCount = executeFunction->GetInParamsCount();
  1264. ArgSlot paramIndex = 1;
  1265. while (paramIndex < this->inSlotsCount)
  1266. {
  1267. Var src = *paGivenSrc++;
  1268. callback(src, paramIndex);
  1269. *pArg++ = src;
  1270. paramIndex++;
  1271. }
  1272. Var varUndef = executeFunction->GetScriptContext()->GetLibrary()->GetUndefined();
  1273. do
  1274. {
  1275. callback(varUndef, paramIndex);
  1276. *pArg++ = varUndef;
  1277. paramIndex++;
  1278. } while (paramIndex < requiredInParamCount);
  1279. *pprestDest = pArg;
  1280. }
  1281. void InterpreterStackFrame::Setup::InitializeRestParam(InterpreterStackFrame * newInstance, Var *dest)
  1282. {
  1283. Var *src = this->inParams + executeFunction->GetInParamsCount();
  1284. if (this->inSlotsCount > executeFunction->GetInParamsCount())
  1285. {
  1286. // Create the rest array and copy the args directly into the contiguous head segment.
  1287. int excess = this->inSlotsCount - executeFunction->GetInParamsCount();
  1288. *dest = JavascriptArray::OP_NewScArray(excess, executeFunction->GetScriptContext());
  1289. JavascriptArray *array = static_cast<JavascriptArray *>(*dest);
  1290. Field(Var)* elements = SparseArraySegment<Var>::From(array->GetHead())->elements;
  1291. CopyArray(elements, excess, src, excess);
  1292. }
  1293. else
  1294. {
  1295. // Rest is an empty array when there are no excess parameters.
  1296. *dest = JavascriptArray::OP_NewScArray(0, executeFunction->GetScriptContext());
  1297. }
  1298. }
  1299. FrameDisplay * InterpreterStackFrame::GetEnvForEvalCode()
  1300. {
  1301. FrameDisplay *pScope;
  1302. if (m_functionBody->GetIsStrictMode() && m_functionBody->GetIsGlobalFunc())
  1303. {
  1304. pScope = this->GetLocalFrameDisplay();
  1305. }
  1306. else
  1307. {
  1308. pScope = (FrameDisplay*)this->LdEnv();
  1309. }
  1310. return pScope;
  1311. }
  1312. void InterpreterStackFrame::InitializeClosures()
  1313. {
  1314. FunctionBody *executeFunction = this->function->GetFunctionBody();
  1315. Var environment;
  1316. if (executeFunction->IsParamAndBodyScopeMerged())
  1317. {
  1318. this->SetIsParamScopeDone(true);
  1319. }
  1320. RegSlot thisRegForEventHandler = executeFunction->GetThisRegisterForEventHandler();
  1321. if (thisRegForEventHandler != Constants::NoRegister)
  1322. {
  1323. Var varThis = OP_ArgIn0();
  1324. SetReg(thisRegForEventHandler, varThis);
  1325. environment = JavascriptOperators::OP_LdHandlerScope(varThis, GetScriptContext());
  1326. this->SetEnv((FrameDisplay*)environment);
  1327. }
  1328. else if (this->paramClosure != nullptr)
  1329. {
  1330. // When paramClosure is non-null we are calling this method to initialize the closure for body scope.
  1331. // In this case we have to use the param scope's closure as the parent for the body scope's frame display.
  1332. Assert(!executeFunction->IsParamAndBodyScopeMerged());
  1333. environment = this->GetLocalFrameDisplay();
  1334. }
  1335. else
  1336. {
  1337. environment = this->LdEnv();
  1338. }
  1339. Var funcExprScope = nullptr;
  1340. Js::RegSlot funcExprScopeReg = executeFunction->GetFuncExprScopeRegister();
  1341. if (funcExprScopeReg != Constants::NoRegister && this->paramClosure == nullptr)
  1342. {
  1343. // t0 = NewPseudoScope
  1344. // t1 = LdFrameDisplay t0 env
  1345. funcExprScope = JavascriptOperators::OP_NewPseudoScope(GetScriptContext());
  1346. SetReg(funcExprScopeReg, funcExprScope);
  1347. }
  1348. RegSlot closureReg = executeFunction->GetLocalClosureRegister();
  1349. if (closureReg != Js::Constants::NoRegister)
  1350. {
  1351. Assert(closureReg >= executeFunction->GetConstantCount());
  1352. if (executeFunction->HasScopeObject())
  1353. {
  1354. this->NewScopeObject();
  1355. }
  1356. else
  1357. {
  1358. this->NewScopeSlots();
  1359. }
  1360. this->SetNonVarReg(closureReg, nullptr);
  1361. }
  1362. Js::RegSlot frameDisplayReg = executeFunction->GetLocalFrameDisplayRegister();
  1363. if (frameDisplayReg != Js::Constants::NoRegister)
  1364. {
  1365. Assert(frameDisplayReg >= executeFunction->GetConstantCount());
  1366. if (funcExprScope != nullptr)
  1367. {
  1368. environment = OP_LdFrameDisplay(funcExprScope, environment, GetScriptContext());
  1369. }
  1370. if (closureReg != Js::Constants::NoRegister)
  1371. {
  1372. void *argHead = this->GetLocalClosure();
  1373. environment = this->NewFrameDisplay(argHead, environment);
  1374. }
  1375. this->SetLocalFrameDisplay((Js::FrameDisplay*)environment);
  1376. this->SetNonVarReg(frameDisplayReg, nullptr);
  1377. }
  1378. this->closureInitDone = true;
  1379. }
  1380. #ifdef _M_IX86
  1381. #ifdef ASMJS_PLAT
  1382. int InterpreterStackFrame::GetAsmJsArgSize(AsmJsCallStackLayout* stack)
  1383. {
  1384. JavascriptFunction * func = stack->functionObject;
  1385. AsmJsFunctionInfo* asmInfo = func->GetFunctionBody()->GetAsmJsFunctionInfo();
  1386. uint argSize = (uint)(asmInfo->GetArgByteSize());
  1387. argSize = ::Math::Align<int32>(argSize, 8);
  1388. // 2 * sizeof(Var) is for functionObject, and another push that DynamicInterpreterThunk does
  1389. return argSize + 2 * sizeof(Var);
  1390. }
  1391. int InterpreterStackFrame::GetDynamicRetType(AsmJsCallStackLayout* stack)
  1392. {
  1393. return GetRetType(stack->functionObject);
  1394. }
  1395. int InterpreterStackFrame::GetRetType(JavascriptFunction* func)
  1396. {
  1397. AsmJsFunctionInfo* asmInfo = func->GetFunctionBody()->GetAsmJsFunctionInfo();
  1398. return asmInfo->GetReturnType().which();
  1399. }
  1400. #ifdef ASMJS_PLAT
  1401. /*
  1402. AsmInterpreterThunk
  1403. -------------------
  1404. This is the entrypoint for all Asm Interpreter calls (external and internal)
  1405. TODO - Make this a dynamic Interpreter thunk to support ETW
  1406. Functionality:
  1407. 1) Prolog
  1408. 2) call AsmInterpreter passing the function object
  1409. 3) Get The return type
  1410. 4) Check for Double or Float return type
  1411. 5) If true then retrieve the value stored at a constant offset from the ScriptContext
  1412. 6) Get Argument Size for callee cleanup
  1413. 7) EpiLog
  1414. a) Retrieve the frame pointer
  1415. b) Store the return address in register (edx)
  1416. c) Clean the arguments based on the arguments size
  1417. d) push the return address back into the stack
  1418. */
  1419. __declspec(naked)
  1420. void InterpreterStackFrame::InterpreterAsmThunk(AsmJsCallStackLayout* layout)
  1421. {
  1422. __asm
  1423. {
  1424. //Prologue
  1425. push ebp;
  1426. mov ebp, esp;
  1427. // Allocate space to put the return value
  1428. sub esp, 16;
  1429. and esp, -16;
  1430. push esp;
  1431. push layout;
  1432. call InterpreterStackFrame::AsmJsInterpreter;
  1433. // we need to move stack around in order to do callee cleanup
  1434. // unfortunately, we don't really have enough registers to do this cleanly
  1435. //
  1436. // we are rearranging the stack from this:
  1437. // 0x14 caller push scriptArg1
  1438. // 0x10 caller push functionObject
  1439. // 0x0C DynamicInterpreterThunk return address
  1440. // 0x08 DynamicInterpreterThunk push ebp
  1441. // 0x04 DynamicInterpreterThunk push functionObject
  1442. // 0x00 InterpreterAsmThunk return address <- stack pointer
  1443. // to this:
  1444. // 0x14 DynamicInterpreterThunk return address
  1445. // 0x10 DynamicInterpreterThunk push ebp
  1446. // 0x0C InterpreterAsmThunk return address <- stack pointer
  1447. // Read return value into possible registers
  1448. movups xmm0, [esp];
  1449. mov edx, [esp + 4];
  1450. mov eax, [esp];
  1451. mov ecx, layout;
  1452. // Epilog, callee cleanup
  1453. mov esp, ebp;
  1454. pop ebp;
  1455. push eax; // save eax
  1456. push edx; // save edx
  1457. // we have to do +0x8 on all stack addresses because we saved 2 registers
  1458. push ecx; // ebp has been restored, we can't use parameters anymore
  1459. call InterpreterStackFrame::GetAsmJsArgSize;
  1460. mov ecx, eax;
  1461. lea eax, [esp + ecx * 1 + 0x8 + 0x8]; // eax will be our stack destination
  1462. mov edx, [esp + 0xC + 0x8];
  1463. mov[eax], edx; // move the dynamic interpreter thunk return location
  1464. mov edx, [esp + 0x8 + 0x8];
  1465. mov[eax - 0x4], edx; // move the dynamic interpreter thunk "push ebp" location
  1466. // skip "push functionObject"
  1467. mov edx, [esp + 0x0 + 0x8];
  1468. mov[eax - 0x8], edx; // move the return location
  1469. pop edx; // restore possible int64 return value
  1470. pop eax; // restore return value
  1471. add esp, ecx; // cleanup arguments
  1472. ret;
  1473. }
  1474. }
  1475. #endif
  1476. #endif
  1477. #endif
  1478. #if DYNAMIC_INTERPRETER_THUNK
  1479. #ifdef _M_IX86
  1480. __declspec(naked)
  1481. Var InterpreterStackFrame::AsmJsDelayDynamicInterpreterThunk(RecyclableObject* function, CallInfo callInfo, ...)
  1482. {
  1483. __asm
  1484. {
  1485. push ebp;
  1486. mov ebp, esp;
  1487. push[esp + 8]; // push function object
  1488. call WasmLibrary::EnsureWasmEntrypoint;
  1489. test eax, eax;
  1490. jne skipThunk;
  1491. push[esp + 8]; // push function object
  1492. call InterpreterStackFrame::EnsureDynamicInterpreterThunk;
  1493. skipThunk:
  1494. #ifdef _CONTROL_FLOW_GUARD
  1495. // verify that the call target is valid
  1496. mov ecx, eax;
  1497. call[__guard_check_icall_fptr];
  1498. mov eax, ecx;
  1499. #endif
  1500. pop ebp;
  1501. jmp eax;
  1502. }
  1503. }
  1504. __declspec(naked)
  1505. Var InterpreterStackFrame::DelayDynamicInterpreterThunk(RecyclableObject* function, CallInfo callInfo, ...)
  1506. {
  1507. __asm
  1508. {
  1509. push ebp;
  1510. mov ebp, esp;
  1511. push[esp + 8]; // push function object
  1512. call InterpreterStackFrame::EnsureDynamicInterpreterThunk;
  1513. #ifdef _CONTROL_FLOW_GUARD
  1514. // verify that the call target is valid
  1515. mov ecx, eax;
  1516. call[__guard_check_icall_fptr];
  1517. mov eax, ecx;
  1518. #endif
  1519. pop ebp;
  1520. jmp eax;
  1521. }
  1522. }
  1523. #elif !defined(_M_AMD64)
  1524. Var InterpreterStackFrame::AsmJsDelayDynamicInterpreterThunk(RecyclableObject* function, CallInfo callInfo, ...)
  1525. {
  1526. // Asm.js only supported on x64 and x86
  1527. AssertOrFailFast(UNREACHED);
  1528. return nullptr;
  1529. }
  1530. #endif
  1531. #endif
  1532. #if ENABLE_PROFILE_INFO
  1533. JavascriptMethod InterpreterStackFrame::EnsureDynamicInterpreterThunk(Js::ScriptFunction * function)
  1534. {
  1535. #if DYNAMIC_INTERPRETER_THUNK
  1536. Assert(function);
  1537. Js::FunctionBody *functionBody = function->GetFunctionBody();
  1538. JavascriptMethod entrypoint = functionBody->EnsureDynamicInterpreterThunk(function->GetFunctionEntryPointInfo());
  1539. Assert(!IsDelayDynamicInterpreterThunk(functionBody->GetDirectEntryPoint(function->GetEntryPointInfo())));
  1540. if (function->GetEntryPoint() == InterpreterStackFrame::DelayDynamicInterpreterThunk)
  1541. {
  1542. // If we are not profiling, or the function object is not cross site, this is the direct entry point.
  1543. // Change the entry point on the object
  1544. Assert(functionBody->GetDirectEntryPoint(function->GetEntryPointInfo()) == entrypoint);
  1545. function->ChangeEntryPoint(function->GetEntryPointInfo(), entrypoint);
  1546. }
  1547. // Return the original entry point to be called
  1548. return entrypoint;
  1549. #else
  1550. return function->GetEntryPoint();
  1551. #endif
  1552. }
  1553. #endif
  1554. bool InterpreterStackFrame::IsDelayDynamicInterpreterThunk(JavascriptMethod entryPoint)
  1555. {
  1556. return false
  1557. #if DYNAMIC_INTERPRETER_THUNK
  1558. || entryPoint == InterpreterStackFrame::DelayDynamicInterpreterThunk
  1559. #ifdef ASMJS_PLAT
  1560. || entryPoint == InterpreterStackFrame::AsmJsDelayDynamicInterpreterThunk
  1561. #endif
  1562. #endif
  1563. ;
  1564. }
  1565. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  1566. THREAD_LOCAL int InterpreterThunkStackCountTracker::s_count = 0;
  1567. #endif
  1568. #if DYNAMIC_INTERPRETER_THUNK
  1569. #pragma optimize("", off)
  1570. #ifdef ASMJS_PLAT
  1571. void InterpreterStackFrame::StaticInterpreterAsmThunk(RecyclableObject* function, ...)
  1572. {
  1573. InterpreterAsmThunk((AsmJsCallStackLayout*)&function);
  1574. }
  1575. #endif
  1576. #if !defined(_M_ARM64)
  1577. Var InterpreterStackFrame::StaticInterpreterThunk(RecyclableObject* function, CallInfo callInfo, ...)
  1578. {
  1579. return InterpreterThunk((JavascriptCallStackLayout*)&function);
  1580. }
  1581. #else
  1582. // Language\arm64\arm64_Thunks.asm
  1583. #endif
  1584. #pragma optimize("", on)
  1585. Var InterpreterStackFrame::InterpreterThunk(JavascriptCallStackLayout* layout)
  1586. {
  1587. Js::ScriptFunction * function = Js::UnsafeVarTo<Js::ScriptFunction>(layout->functionObject);
  1588. Js::ArgumentReader args(&layout->callInfo, layout->args);
  1589. void* localReturnAddress = _ReturnAddress();
  1590. void* localAddressOfReturnAddress = _AddressOfReturnAddress();
  1591. return InterpreterHelper(function, args, localReturnAddress, localAddressOfReturnAddress);
  1592. }
  1593. #else
  1594. #pragma optimize("", off)
  1595. Var InterpreterStackFrame::InterpreterThunk(RecyclableObject* function, CallInfo callInfo, ...)
  1596. {
  1597. ARGUMENTS(args, callInfo);
  1598. void* localReturnAddress = _ReturnAddress();
  1599. void* localAddressOfReturnAddress = _AddressOfReturnAddress();
  1600. Assert(VarIs<ScriptFunction>(function));
  1601. return InterpreterHelper(VarTo<ScriptFunction>(function), args, localReturnAddress, localAddressOfReturnAddress);
  1602. }
  1603. #pragma optimize("", on)
  1604. #endif
  1605. bool InterpreterStackFrame::ShouldDoProfile(FunctionBody* executeFunction)
  1606. {
  1607. #if ENABLE_PROFILE_INFO
  1608. const bool doProfile = executeFunction->GetInterpreterExecutionMode(false) == ExecutionMode::ProfilingInterpreter ||
  1609. (executeFunction->IsInDebugMode() && DynamicProfileInfo::IsEnabled(executeFunction));
  1610. return doProfile;
  1611. #else
  1612. return false;
  1613. #endif
  1614. }
  1615. InterpreterStackFrame* InterpreterStackFrame::CreateInterpreterStackFrameForGenerator(ScriptFunction* function, FunctionBody* executeFunction, JavascriptGenerator* generator, bool doProfile)
  1616. {
  1617. //
  1618. // Allocate a new InterpreterStackFrame instance on the recycler heap.
  1619. // It will live with the JavascriptGenerator object.
  1620. //
  1621. ScriptContext* functionScriptContext = function->GetScriptContext();
  1622. Arguments generatorArgs = generator->GetArguments();
  1623. InterpreterStackFrame::Setup setup(function, generatorArgs, false /* bailedOut */, false /* inlinee */, true /* isGeneratorFrame */);
  1624. Assert(setup.GetStackAllocationVarCount() == 0);
  1625. size_t varAllocCount = setup.GetAllocationVarCount();
  1626. size_t varSizeInBytes = varAllocCount * sizeof(Var);
  1627. DWORD_PTR stackAddr = reinterpret_cast<DWORD_PTR>(&generator); // use any stack address from this frame to ensure correct debugging functionality
  1628. LoopHeader* loopHeaderArray = executeFunction->GetHasAllocatedLoopHeaders() ? executeFunction->GetLoopHeaderArrayPtr() : nullptr;
  1629. Var* allocation = RecyclerNewPlus(functionScriptContext->GetRecycler(), varSizeInBytes, Var);
  1630. AnalysisAssert(allocation);
  1631. InterpreterStackFrame* newInstance;
  1632. #if DBG
  1633. // Allocate invalidVar on GC instead of stack since this InterpreterStackFrame will out live the current real frame
  1634. Js::RecyclableObject* invalidVar = (Js::RecyclableObject*)RecyclerNewPlusLeaf(functionScriptContext->GetRecycler(), sizeof(Js::RecyclableObject), Var);
  1635. AnalysisAssert(invalidVar);
  1636. memset(reinterpret_cast<void*>(invalidVar), 0xFE, sizeof(Js::RecyclableObject));
  1637. #endif
  1638. newInstance = setup.InitializeAllocation(allocation, nullptr, executeFunction->GetHasImplicitArgIns(), doProfile, loopHeaderArray, stackAddr
  1639. #if DBG
  1640. , invalidVar
  1641. #endif
  1642. );
  1643. newInstance->m_reader.Create(executeFunction);
  1644. generator->SetFrame(newInstance, varSizeInBytes);
  1645. // Moving this to when we create the generator instance in the first place would be nice.
  1646. // But at that point the function might not have been parsed yet, so we don't have the locals count.
  1647. // We are also allocating more space than we actually need because we shouldn't need to
  1648. // reload all the symbols when bailing in.
  1649. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  1650. if (PHASE_TRACE(Js::Phase::BailInPhase, function->GetFunctionBody()))
  1651. {
  1652. generator->bailInSymbolsTraceArray = (Js::JavascriptGenerator::BailInSymbol*) RecyclerNewArrayLeafZ(
  1653. functionScriptContext->GetRecycler(), Js::JavascriptGenerator::BailInSymbol, executeFunction->GetFunctionBody()->GetLocalsCount()
  1654. );
  1655. }
  1656. #endif
  1657. return newInstance;
  1658. }
  1659. Var InterpreterStackFrame::InterpreterHelper(ScriptFunction* function, ArgumentReader args, void* returnAddress, void* addressOfReturnAddress, AsmJsReturnStruct* asmJsReturn)
  1660. {
  1661. const bool isAsmJs = asmJsReturn != nullptr;
  1662. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  1663. // Support for simulating partially initialized interpreter stack frame.
  1664. InterpreterThunkStackCountTracker tracker;
  1665. if (CONFIG_ISENABLED(InjectPartiallyInitializedInterpreterFrameErrorFlag) &&
  1666. CONFIG_FLAG(InjectPartiallyInitializedInterpreterFrameError) == InterpreterThunkStackCountTracker::GetCount())
  1667. {
  1668. switch (CONFIG_FLAG(InjectPartiallyInitializedInterpreterFrameErrorType))
  1669. {
  1670. case 0:
  1671. DebugBreak();
  1672. break;
  1673. case 1:
  1674. Js::JavascriptError::MapAndThrowError(function->GetScriptContext(), VBSERR_InternalError);
  1675. break;
  1676. default:
  1677. DebugBreak();
  1678. }
  1679. }
  1680. #endif
  1681. ScriptContext* functionScriptContext = function->GetScriptContext();
  1682. ThreadContext * threadContext = functionScriptContext->GetThreadContext();
  1683. Assert(!threadContext->IsDisableImplicitException());
  1684. functionScriptContext->VerifyAlive(!function->IsExternal());
  1685. Assert(threadContext->IsScriptActive());
  1686. Assert(threadContext->IsInScript());
  1687. FunctionBody* executeFunction = function->GetFunctionBody();
  1688. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  1689. if (!isAsmJs && executeFunction->IsInDebugMode() != functionScriptContext->IsScriptContextInDebugMode()) // debug mode mismatch
  1690. {
  1691. if (executeFunction->GetUtf8SourceInfo()->GetIsLibraryCode())
  1692. {
  1693. Assert(!executeFunction->IsInDebugMode()); // Library script byteCode is never in debug mode
  1694. }
  1695. else
  1696. {
  1697. Throw::FatalInternalError();
  1698. }
  1699. }
  1700. #endif
  1701. if (executeFunction->GetInterpretedCount() == 0)
  1702. {
  1703. executeFunction->TraceInterpreterExecutionMode();
  1704. }
  1705. class AutoRestore
  1706. {
  1707. private:
  1708. ThreadContext * const threadContext;
  1709. const uint8 savedLoopDepth;
  1710. public:
  1711. AutoRestore(ThreadContext *const threadContext, FunctionBody *const executeFunction)
  1712. : threadContext(threadContext),
  1713. savedLoopDepth(threadContext->LoopDepth())
  1714. {
  1715. if (savedLoopDepth != 0 && !executeFunction->GetIsAsmJsFunction())
  1716. {
  1717. executeFunction->SetWasCalledFromLoop();
  1718. }
  1719. }
  1720. ~AutoRestore()
  1721. {
  1722. threadContext->SetLoopDepth(savedLoopDepth);
  1723. }
  1724. } autoRestore(threadContext, executeFunction);
  1725. #if ENABLE_PROFILE_INFO
  1726. DynamicProfileInfo * dynamicProfileInfo = nullptr;
  1727. const bool doProfile = ShouldDoProfile(executeFunction);
  1728. if (doProfile)
  1729. {
  1730. #if !DYNAMIC_INTERPRETER_THUNK
  1731. executeFunction->EnsureDynamicProfileInfo();
  1732. #endif
  1733. dynamicProfileInfo = executeFunction->GetDynamicProfileInfo();
  1734. threadContext->ClearImplicitCallFlags();
  1735. }
  1736. #else
  1737. const bool doProfile = false;
  1738. #endif
  1739. executeFunction->IncreaseInterpretedCount();
  1740. #ifdef BGJIT_STATS
  1741. functionScriptContext->interpretedCount++;
  1742. functionScriptContext->maxFuncInterpret = max(functionScriptContext->maxFuncInterpret, executeFunction->GetInterpretedCount());
  1743. #endif
  1744. AssertMsg(!executeFunction->IsDeferredParseFunction(),
  1745. "Non-intrinsic functions must provide byte-code to execute");
  1746. executeFunction->BeginExecution();
  1747. bool fReleaseAlloc = false;
  1748. InterpreterStackFrame* newInstance = nullptr;
  1749. if (!isAsmJs && executeFunction->IsCoroutine())
  1750. {
  1751. // If the FunctionBody is a generator then this call is being made by one of the three
  1752. // generator resuming methods: next(), throw(), or return(). They all pass the generator
  1753. // object as the first of two arguments. The real user arguments are obtained from the
  1754. // generator object. The second argument is the ResumeYieldData which is only needed
  1755. // when resuming a generator and so it is only used here if a frame already exists on the
  1756. // generator object.
  1757. AssertOrFailFastMsg(args.Info.Count == 2 && ((args.Info.Flags & CallFlags_ExtraArg) == CallFlags_None), "Generator ScriptFunctions should only be invoked by generator APIs with the pair of arguments they pass in -- the generator object and a ResumeYieldData pointer");
  1758. JavascriptGenerator* generator = VarTo<JavascriptGenerator>(args[0]);
  1759. newInstance = generator->GetFrame();
  1760. if (newInstance != nullptr)
  1761. {
  1762. ResumeYieldData* resumeYieldData = static_cast<ResumeYieldData*>(args[1]);
  1763. newInstance->SetNonVarReg(executeFunction->GetYieldRegister(), resumeYieldData);
  1764. // The debugger relies on comparing stack addresses of frames to decide when a step_out is complete so
  1765. // give the InterpreterStackFrame a legit enough stack address to make this comparison work.
  1766. newInstance->m_stackAddress = reinterpret_cast<DWORD_PTR>(&generator);
  1767. newInstance->retOffset = 0;
  1768. }
  1769. else
  1770. {
  1771. newInstance = CreateInterpreterStackFrameForGenerator(function, executeFunction, generator, doProfile);
  1772. }
  1773. }
  1774. else
  1775. {
  1776. InterpreterStackFrame::Setup setup(function, args);
  1777. size_t varAllocCount = setup.GetAllocationVarCount();
  1778. size_t stackVarAllocCount = setup.GetStackAllocationVarCount();
  1779. size_t varSizeInBytes;
  1780. //
  1781. // Allocate a new InterpreterStackFrame instance on the interpreter's virtual stack.
  1782. //
  1783. DWORD_PTR stackAddr;
  1784. Var* allocation;
  1785. Var* stackAllocation = nullptr;
  1786. // If the locals area exceeds a certain limit, allocate it from a private arena rather than
  1787. // this frame. The current limit is based on an old assert on the number of locals we would allow here.
  1788. if ((varAllocCount + stackVarAllocCount) > InterpreterStackFrame::LocalsThreshold)
  1789. {
  1790. ArenaAllocator *tmpAlloc = nullptr;
  1791. fReleaseAlloc = functionScriptContext->EnsureInterpreterArena(&tmpAlloc);
  1792. varSizeInBytes = varAllocCount * sizeof(Var);
  1793. allocation = (Var*)tmpAlloc->Alloc(varSizeInBytes);
  1794. stackAddr = reinterpret_cast<DWORD_PTR>(&allocation); // use a stack address so the debugger stepping logic works (step-out, for example, compares stack depths to determine when to complete the step)
  1795. if (stackVarAllocCount != 0)
  1796. {
  1797. size_t stackVarSizeInBytes = stackVarAllocCount * sizeof(Var);
  1798. PROBE_STACK_PARTIAL_INITIALIZED_INTERPRETER_FRAME(functionScriptContext, Js::Constants::MinStackInterpreter + stackVarSizeInBytes);
  1799. stackAllocation = (Var*)_alloca(stackVarSizeInBytes);
  1800. }
  1801. }
  1802. else
  1803. {
  1804. varSizeInBytes = (varAllocCount + stackVarAllocCount) * sizeof(Var);
  1805. PROBE_STACK_PARTIAL_INITIALIZED_INTERPRETER_FRAME(functionScriptContext, Js::Constants::MinStackInterpreter + varSizeInBytes);
  1806. allocation = (Var*)_alloca(varSizeInBytes);
  1807. #if DBG
  1808. memset(allocation, 0xFE, varSizeInBytes);
  1809. #endif
  1810. stackAddr = reinterpret_cast<DWORD_PTR>(allocation);
  1811. }
  1812. /*
  1813. * If the function has any loop headers, we allocate an array for the loop headers wrappers, and
  1814. * reference the wrappers in the array. We then push the pointer to the array onto the stack itself.
  1815. * We do this so that while the function is being interpreted, we don't want the jitted loop
  1816. * bodies to be collected, even if the loop body isn't being executed. The loop body will
  1817. * get collected when the function has been JITted, and when the function exits the interpreter.
  1818. * The array contains nulls if the loop body isn't jitted (or hasn't been jitted yet) but
  1819. * it's cheaper to just copy them all into the recycler array rather than just the ones that
  1820. * have been jitted.
  1821. */
  1822. LoopHeader* loopHeaderArray = nullptr;
  1823. if (executeFunction->GetHasAllocatedLoopHeaders())
  1824. {
  1825. // Loop header array is recycler allocated, so we push it on the stack
  1826. // When we scan the stack, we'll recognize it as a recycler allocated
  1827. // object, and mark it's contents and keep the individual loop header
  1828. // wrappers alive
  1829. loopHeaderArray = executeFunction->GetLoopHeaderArrayPtr();
  1830. }
  1831. #if DBG
  1832. Js::RecyclableObject * invalidStackVar = (Js::RecyclableObject*)_alloca(sizeof(Js::RecyclableObject));
  1833. memset(reinterpret_cast<void*>(invalidStackVar), 0xFE, sizeof(Js::RecyclableObject));
  1834. #endif
  1835. newInstance = setup.InitializeAllocation(allocation, stackAllocation, executeFunction->GetHasImplicitArgIns() && !isAsmJs, doProfile, loopHeaderArray, stackAddr
  1836. #if DBG
  1837. , invalidStackVar
  1838. #endif
  1839. );
  1840. newInstance->m_reader.Create(executeFunction);
  1841. }
  1842. //
  1843. // Execute the function's byte-code, returning the return-value:
  1844. // - Mark that the function is current executing and may not be modified.
  1845. //
  1846. #if ENABLE_TTD
  1847. TTD::TTDExceptionFramePopper exceptionFramePopper;
  1848. if (SHOULD_DO_TTD_STACK_STMT_OP(functionScriptContext))
  1849. {
  1850. bool isInFinally = newInstance->TestFlags(Js::InterpreterStackFrameFlags_WithinFinallyBlock);
  1851. threadContext->TTDExecutionInfo->PushCallEvent(function, args.Info.Count, args.Values, isInFinally);
  1852. exceptionFramePopper.PushInfo(threadContext->TTDExecutionInfo, function);
  1853. }
  1854. #endif
  1855. Var aReturn = nullptr;
  1856. {
  1857. #ifdef ENABLE_SCRIPT_DEBUGGING
  1858. if (!isAsmJs && executeFunction->IsInDebugMode())
  1859. {
  1860. #if DYNAMIC_INTERPRETER_THUNK
  1861. PushPopFrameHelper pushPopFrameHelper(newInstance, returnAddress, addressOfReturnAddress);
  1862. aReturn = newInstance->DebugProcess();
  1863. #else
  1864. aReturn = newInstance->DebugProcessThunk(_ReturnAddress(), _AddressOfReturnAddress());
  1865. #endif
  1866. }
  1867. else
  1868. #endif
  1869. {
  1870. #if DYNAMIC_INTERPRETER_THUNK
  1871. PushPopFrameHelper pushPopFrameHelper(newInstance, returnAddress, addressOfReturnAddress);
  1872. aReturn = newInstance->Process();
  1873. #else
  1874. aReturn = newInstance->ProcessThunk(_ReturnAddress(), _AddressOfReturnAddress());
  1875. #endif
  1876. }
  1877. }
  1878. executeFunction->EndExecution();
  1879. #if ENABLE_TTD
  1880. if (SHOULD_DO_TTD_STACK_STMT_OP(functionScriptContext))
  1881. {
  1882. exceptionFramePopper.PopInfo();
  1883. threadContext->TTDExecutionInfo->PopCallEvent(function, aReturn);
  1884. }
  1885. #endif
  1886. #ifdef ASMJS_PLAT
  1887. if (isAsmJs)
  1888. {
  1889. asmJsReturn->i = newInstance->GetRegRawInt(0);
  1890. asmJsReturn->l = newInstance->GetRegRawInt64(0);
  1891. asmJsReturn->d = newInstance->GetRegRawDouble(0);
  1892. asmJsReturn->f = newInstance->GetRegRawFloat(0);
  1893. asmJsReturn->simd = newInstance->GetRegRawSimd(0);
  1894. }
  1895. #endif
  1896. if (fReleaseAlloc)
  1897. {
  1898. functionScriptContext->ReleaseInterpreterArena();
  1899. }
  1900. #if ENABLE_PROFILE_INFO
  1901. if (doProfile)
  1902. {
  1903. dynamicProfileInfo->RecordImplicitCallFlags(threadContext->GetImplicitCallFlags());
  1904. }
  1905. #endif
  1906. return aReturn;
  1907. }
  1908. #ifdef ASMJS_PLAT
  1909. #if _M_IX86
  1910. void InterpreterStackFrame::AsmJsInterpreter(AsmJsCallStackLayout* stack, byte* retDst)
  1911. {
  1912. ScriptFunction * function = (ScriptFunction*)stack->functionObject;
  1913. Var* paramsAddr = stack->args;
  1914. int flags = CallFlags_Value;
  1915. ArgSlot nbArgs = ArgSlotMath::Add(function->GetFunctionBody()->GetAsmJsFunctionInfo()->GetArgCount(), 1);
  1916. CallInfo callInfo((CallFlags)flags, nbArgs);
  1917. ArgumentReader args(&callInfo, paramsAddr);
  1918. void* returnAddress = _ReturnAddress();
  1919. void* addressOfReturnAddress = _AddressOfReturnAddress();
  1920. #if ENABLE_PROFILE_INFO
  1921. function->GetFunctionBody()->EnsureDynamicProfileInfo();
  1922. #endif
  1923. AsmJsReturnStruct asmJsReturn = { 0 };
  1924. InterpreterHelper(function, args, returnAddress, addressOfReturnAddress, &asmJsReturn);
  1925. //Handle return value
  1926. AsmJsRetType::Which retType = (AsmJsRetType::Which) GetRetType(function);
  1927. switch (retType)
  1928. {
  1929. #ifdef ENABLE_WASM_SIMD
  1930. case AsmJsRetType::Int32x4:
  1931. case AsmJsRetType::Bool32x4:
  1932. case AsmJsRetType::Bool16x8:
  1933. case AsmJsRetType::Bool8x16:
  1934. case AsmJsRetType::Float32x4:
  1935. case AsmJsRetType::Float64x2:
  1936. case AsmJsRetType::Int16x8:
  1937. case AsmJsRetType::Int8x16:
  1938. case AsmJsRetType::Uint32x4:
  1939. case AsmJsRetType::Uint16x8:
  1940. case AsmJsRetType::Uint8x16:
  1941. Assert(Wasm::Simd::IsEnabled());
  1942. *(AsmJsSIMDValue*)retDst = asmJsReturn.simd;
  1943. break;
  1944. #endif
  1945. // double return
  1946. case AsmJsRetType::Double:
  1947. *(double*)retDst = asmJsReturn.d;
  1948. break;
  1949. // float return
  1950. case AsmJsRetType::Float:
  1951. *(float*)retDst = asmJsReturn.f;
  1952. break;
  1953. // signed or void return
  1954. case AsmJsRetType::Signed:
  1955. case AsmJsRetType::Void:
  1956. *(int*)retDst = asmJsReturn.i;
  1957. break;
  1958. case AsmJsRetType::Int64:
  1959. *(int64*)retDst = asmJsReturn.l;
  1960. break;
  1961. default:
  1962. Assume(false);
  1963. }
  1964. }
  1965. #elif _M_X64
  1966. typedef double(*AsmJsInterpreterDoubleEP)(AsmJsCallStackLayout*, void *);
  1967. typedef float(*AsmJsInterpreterFloatEP)(AsmJsCallStackLayout*, void *);
  1968. typedef int(*AsmJsInterpreterIntEP)(AsmJsCallStackLayout*, void *);
  1969. typedef int64(*AsmJsInterpreterInt64EP)(AsmJsCallStackLayout*, void *);
  1970. void * InterpreterStackFrame::GetAsmJsInterpreterEntryPoint(AsmJsCallStackLayout* stack)
  1971. {
  1972. JavascriptFunction * function = stack->functionObject;
  1973. void * entryPoint = nullptr;
  1974. switch (function->GetFunctionBody()->GetAsmJsFunctionInfo()->GetReturnType().which())
  1975. {
  1976. case Js::AsmJsRetType::Double:
  1977. {
  1978. entryPoint = (void*)(AsmJsInterpreterDoubleEP)Js::InterpreterStackFrame::AsmJsInterpreter < double >;
  1979. break;
  1980. }
  1981. case Js::AsmJsRetType::Float:
  1982. {
  1983. entryPoint = (void*)(AsmJsInterpreterFloatEP)Js::InterpreterStackFrame::AsmJsInterpreter < float >;
  1984. break;
  1985. }
  1986. case Js::AsmJsRetType::Signed:
  1987. case Js::AsmJsRetType::Void:
  1988. {
  1989. entryPoint = (void*)(AsmJsInterpreterIntEP)Js::InterpreterStackFrame::AsmJsInterpreter < int >;
  1990. break;
  1991. }
  1992. case Js::AsmJsRetType::Int64:
  1993. {
  1994. entryPoint = (void*)(AsmJsInterpreterInt64EP)Js::InterpreterStackFrame::AsmJsInterpreter < int64 >;
  1995. break;
  1996. }
  1997. #ifdef ENABLE_WASM_SIMD
  1998. case Js::AsmJsRetType::Int32x4:
  1999. case Js::AsmJsRetType::Bool32x4:
  2000. case Js::AsmJsRetType::Bool16x8:
  2001. case Js::AsmJsRetType::Bool8x16:
  2002. case Js::AsmJsRetType::Float32x4:
  2003. case Js::AsmJsRetType::Float64x2:
  2004. case Js::AsmJsRetType::Int16x8:
  2005. case Js::AsmJsRetType::Int8x16:
  2006. case Js::AsmJsRetType::Uint32x4:
  2007. case Js::AsmJsRetType::Uint16x8:
  2008. case Js::AsmJsRetType::Uint8x16:
  2009. {
  2010. entryPoint = (void*)Js::InterpreterStackFrame::AsmJsInterpreterSimdJs;
  2011. break;
  2012. }
  2013. #endif
  2014. default:
  2015. Assume(UNREACHED);
  2016. }
  2017. return entryPoint;
  2018. }
  2019. template<typename T>
  2020. T InterpreterStackFrame::AsmJsInterpreter(AsmJsCallStackLayout* layout)
  2021. {
  2022. Js::ScriptFunction * function = Js::VarTo<Js::ScriptFunction>(layout->functionObject);
  2023. int flags = CallFlags_Value;
  2024. ArgSlot nbArgs = ArgSlotMath::Add(function->GetFunctionBody()->GetAsmJsFunctionInfo()->GetArgCount(), 1);
  2025. CallInfo callInfo((CallFlags)flags, nbArgs);
  2026. ArgumentReader args(&callInfo, (Var*)layout->args);
  2027. void* returnAddress = _ReturnAddress();
  2028. void* addressOfReturnAddress = _AddressOfReturnAddress();
  2029. function->GetFunctionBody()->EnsureDynamicProfileInfo();
  2030. AsmJsReturnStruct asmJsReturn = { 0 };
  2031. InterpreterHelper(function, args, returnAddress, addressOfReturnAddress, &asmJsReturn);
  2032. return asmJsReturn.GetRetVal<T>();
  2033. }
  2034. __m128 InterpreterStackFrame::AsmJsInterpreterSimdJs(AsmJsCallStackLayout* layout)
  2035. {
  2036. return AsmJsInterpreter<X86SIMDValue>(layout).m128_value;
  2037. }
  2038. #endif
  2039. #endif
  2040. ///----------------------------------------------------------------------------
  2041. ///
  2042. /// InterpreterStackFrame::SetOut()
  2043. ///
  2044. /// SetOut() change the Var value stored in the specified "out parameter"
  2045. /// register.
  2046. ///
  2047. ///----------------------------------------------------------------------------
  2048. inline void InterpreterStackFrame::SetOut(ArgSlot outRegisterID, Var aValue)
  2049. {
  2050. //
  2051. // The "out" parameter slots are located at the end of the local register range, counting
  2052. // forwards. This results in the "in" parameter slots being disjoint from the rest of the
  2053. // InterpreterStackFrame.
  2054. // ..., InterpreterStackFrame A, Locals A[], ..., Out A:0, Out A:1, Out A:2, ...
  2055. // | In B:0, In B:1, ..., InterpreterStackFrame B, Locals B[], ...
  2056. // (current 'this') |
  2057. // (new 'this' after call)
  2058. //
  2059. Assert(m_outParams + outRegisterID < m_outSp);
  2060. m_outParams[outRegisterID] = aValue;
  2061. }
  2062. inline void InterpreterStackFrame::SetOut(ArgSlot_OneByte outRegisterID, Var aValue)
  2063. {
  2064. Assert(m_outParams + outRegisterID < m_outSp);
  2065. m_outParams[outRegisterID] = aValue;
  2066. }
  2067. inline void InterpreterStackFrame::OP_SetOutAsmDb(RegSlot outRegisterID, double val)
  2068. {
  2069. Assert(m_outParams + outRegisterID < m_outSp);
  2070. m_outParams[outRegisterID] = JavascriptNumber::NewWithCheck(val, scriptContext);
  2071. }
  2072. inline void InterpreterStackFrame::OP_SetOutAsmInt(RegSlot outRegisterID, int val)
  2073. {
  2074. Assert(m_outParams + outRegisterID < m_outSp);
  2075. m_outParams[outRegisterID] = JavascriptNumber::ToVar(val, scriptContext);
  2076. }
  2077. void InterpreterStackFrame::OP_SetOutAsmFlt(RegSlot outRegisterID, float val)
  2078. {
  2079. OP_SetOutAsmDb(outRegisterID, (double)val);
  2080. }
  2081. inline void InterpreterStackFrame::OP_I_SetOutAsmFlt(RegSlot outRegisterID, float val)
  2082. {
  2083. Assert(m_outParams + outRegisterID < m_outSp);
  2084. *(float*)(&(m_outParams[outRegisterID])) = val;
  2085. }
  2086. inline void InterpreterStackFrame::OP_I_SetOutAsmLong(RegSlot outRegisterID, int64 val)
  2087. {
  2088. Assert(m_outParams + outRegisterID < m_outSp);
  2089. *(int64*)(&(m_outParams[outRegisterID])) = val;
  2090. }
  2091. inline void InterpreterStackFrame::OP_I_SetOutAsmInt(RegSlot outRegisterID, int val)
  2092. {
  2093. Assert(m_outParams + outRegisterID < m_outSp);
  2094. *(int*)(&(m_outParams[outRegisterID])) = val;
  2095. }
  2096. inline void InterpreterStackFrame::OP_I_SetOutAsmDb(RegSlot outRegisterID, double val)
  2097. {
  2098. Assert(m_outParams + outRegisterID < m_outSp);
  2099. *(double*)(&(m_outParams[outRegisterID])) = val;
  2100. }
  2101. inline void InterpreterStackFrame::OP_I_SetOutAsmSimd(RegSlot outRegisterID, AsmJsSIMDValue val)
  2102. {
  2103. Assert(m_outParams + outRegisterID < m_outSp);
  2104. *(AsmJsSIMDValue*)(&(m_outParams[outRegisterID])) = val;
  2105. }
  2106. template<int type, bool toJs>
  2107. void InterpreterStackFrame::OP_InvalidWasmTypeConversion(...)
  2108. {
  2109. #ifdef ENABLE_WASM
  2110. CompileAssert(type < Wasm::WasmTypes::Limit);
  2111. const char16* fromType = toJs ? Wasm::WasmTypes::GetTypeName(static_cast<Wasm::WasmTypes::WasmType>(type)) : _u("Javascript Variable");
  2112. const char16* toType = toJs ? _u("Javascript Variable") : Wasm::WasmTypes::GetTypeName(static_cast<Wasm::WasmTypes::WasmType>(type));
  2113. JavascriptError::ThrowTypeErrorVar(scriptContext, WASMERR_InvalidTypeConversion, fromType, toType);
  2114. #else
  2115. Assert(UNREACHED); //shouldn't get there
  2116. JavascriptError::ThrowTypeErrorVar(scriptContext, WASMERR_InvalidTypeConversion, _u("unknown"), _u("unknown")); //throw for a release build
  2117. #endif
  2118. }
  2119. // This will be called in the beginning of the try_finally.
  2120. inline void InterpreterStackFrame::CacheSp()
  2121. {
  2122. // Before caching the current m_outSp, we will be storing the previous the previously stored value in the m_outSpCached.
  2123. *m_outSp++ = (Var)m_outSpCached;
  2124. *m_outSp++ = (Var)m_outParams;
  2125. m_outSpCached = m_outSp - 2;
  2126. }
  2127. inline void InterpreterStackFrame::RestoreSp()
  2128. {
  2129. // This will be called in the Finally block to restore from the previous SP cached.
  2130. // m_outSpCached can be null if the catch block is called.
  2131. if (m_outSpCached != nullptr)
  2132. {
  2133. Assert(m_outSpCached < m_outSp);
  2134. m_outSp = m_outSpCached;
  2135. m_outSpCached = (Var*)*m_outSp;
  2136. Assert(m_outSpCached == nullptr || m_outSpCached <= m_outSp);
  2137. m_outParams = (Var*)*(m_outSp + 1);
  2138. }
  2139. else
  2140. {
  2141. ResetOut();
  2142. }
  2143. }
  2144. inline void InterpreterStackFrame::PushOut(Var aValue)
  2145. {
  2146. *m_outSp++ = aValue;
  2147. }
  2148. inline void InterpreterStackFrame::PopOut(ArgSlot argCount)
  2149. {
  2150. ArgSlotMath::Inc(argCount);
  2151. m_outSp -= argCount;
  2152. m_outParams = (Var*)*m_outSp;
  2153. AssertMsg(m_localSlots + this->m_functionBody->GetLocalsCount() <= m_outSp &&
  2154. m_outSp < (m_localSlots + this->m_functionBody->GetLocalsCount() + this->m_functionBody->GetOutParamMaxDepth()),
  2155. "out args Stack pointer not in range after Pop");
  2156. }
  2157. void InterpreterStackFrame::ResetOut()
  2158. {
  2159. //
  2160. // Reset the m_outParams and m_outSp
  2161. //
  2162. m_outParams = m_localSlots + this->m_functionBody->GetLocalsCount();
  2163. m_outSp = m_outParams;
  2164. m_outSpCached = nullptr;
  2165. }
  2166. #ifdef ENABLE_SCRIPT_DEBUGGING
  2167. _NOINLINE
  2168. Var InterpreterStackFrame::DebugProcessThunk(void* returnAddress, void* addressOfReturnAddress)
  2169. {
  2170. PushPopFrameHelper pushPopFrameHelper(this, returnAddress, addressOfReturnAddress);
  2171. return this->DebugProcess();
  2172. }
  2173. //
  2174. // Under debug mode allow the exception to be swallowed and execution to continue
  2175. // if the debugger has specified that behavior.
  2176. //
  2177. Var InterpreterStackFrame::DebugProcess()
  2178. {
  2179. Assert(this->returnAddress != nullptr);
  2180. while (true)
  2181. {
  2182. JavascriptExceptionObject *exception = nullptr;
  2183. try
  2184. {
  2185. #if ENABLE_TTD
  2186. if (SHOULD_DO_TTD_STACK_STMT_OP(this->scriptContext))
  2187. {
  2188. return this->ProcessWithDebugging_PreviousStmtTracking();
  2189. }
  2190. else
  2191. {
  2192. return this->ProcessWithDebugging();
  2193. }
  2194. #else
  2195. return this->ProcessWithDebugging();
  2196. #endif
  2197. }
  2198. catch (const Js::JavascriptException& err)
  2199. {
  2200. JavascriptExceptionObject *exception_ = err.GetAndClear();
  2201. Assert(exception_);
  2202. exception = exception_;
  2203. }
  2204. if (exception)
  2205. {
  2206. bool skipException = false;
  2207. if (!exception->IsGeneratorReturnException() &&
  2208. exception != scriptContext->GetThreadContext()->GetPendingSOErrorObject() &&
  2209. exception != scriptContext->GetThreadContext()->GetPendingOOMErrorObject())
  2210. {
  2211. skipException = exception->IsDebuggerSkip();
  2212. }
  2213. if (skipException)
  2214. {
  2215. // If we are going to swallow the exception then advance to the beginning of the next user statement
  2216. if (exception->IsIgnoreAdvanceToNextStatement()
  2217. || this->scriptContext->GetDebugContext()->GetProbeContainer()->AdvanceToNextUserStatement(this->m_functionBody, &this->m_reader))
  2218. {
  2219. // We must fix up the return value to at least be undefined:
  2220. this->SetReg((RegSlot)0, this->scriptContext->GetLibrary()->GetUndefined());
  2221. // If we recover from the exception, there may be a chance the out pointers in the InterpreterStackframe are not in a proper state.
  2222. // Reset them to correct the stack.
  2223. ResetOut();
  2224. // If we can successfully advance then continuing processing
  2225. continue;
  2226. }
  2227. }
  2228. JavascriptExceptionOperators::DoThrowCheckClone(exception, scriptContext);
  2229. }
  2230. }
  2231. }
  2232. #endif
  2233. template<typename OpCodeType, Js::OpCode(ReadOpFunc)(const byte*&), void (TracingFunc)(InterpreterStackFrame*, OpCodeType)>
  2234. OpCodeType InterpreterStackFrame::ReadOp(const byte *& ip)
  2235. {
  2236. #if DBG || DBG_DUMP
  2237. //
  2238. // For debugging byte-code, store the current offset before the instruction is read:
  2239. // - We convert this to "void *" to encourage the debugger to always display in hex,
  2240. // which matches the displayed offsets used by ByteCodeDumper.
  2241. //
  2242. this->DEBUG_currentByteOffset = (void *)m_reader.GetCurrentOffset();
  2243. #endif
  2244. OpCodeType op = (OpCodeType)ReadOpFunc(ip);
  2245. #if DBG_DUMP
  2246. TracingFunc(this, op);
  2247. #endif
  2248. return op;
  2249. }
  2250. void InterpreterStackFrame::TraceOpCode(InterpreterStackFrame* that, Js::OpCode op)
  2251. {
  2252. #if DBG_DUMP
  2253. that->scriptContext->byteCodeHistogram[(int)op]++;
  2254. if (PHASE_TRACE(Js::InterpreterPhase, that->m_functionBody))
  2255. {
  2256. Output::Print(_u("%d.%d:Executing %s at offset 0x%X\n"), that->m_functionBody->GetSourceContextId(), that->m_functionBody->GetLocalFunctionId(), Js::OpCodeUtil::GetOpCodeName(op), that->DEBUG_currentByteOffset);
  2257. }
  2258. #endif
  2259. }
  2260. void InterpreterStackFrame::TraceAsmJsOpCode(InterpreterStackFrame* that, Js::OpCodeAsmJs op)
  2261. {
  2262. #if DBG_DUMP && defined(ASMJS_PLAT)
  2263. if (PHASE_TRACE(Js::AsmjsInterpreterPhase, that->m_functionBody))
  2264. {
  2265. Output::Print(_u("%d.%d:Executing %s at offset 0x%X\n"), that->m_functionBody->GetSourceContextId(), that->m_functionBody->GetLocalFunctionId(), Js::OpCodeUtilAsmJs::GetOpCodeName(op), that->DEBUG_currentByteOffset);
  2266. }
  2267. #endif
  2268. }
  2269. #if ENABLE_TTD
  2270. template<typename OpCodeType, Js::OpCode(ReadOpFunc)(const byte*&), void (TracingFunc)(InterpreterStackFrame*, OpCodeType)>
  2271. OpCodeType InterpreterStackFrame::ReadOp_WPreviousStmtTracking(const byte *& ip)
  2272. {
  2273. #if DBG || DBG_DUMP
  2274. //
  2275. // For debugging byte-code, store the current offset before the instruction is read:
  2276. // - We convert this to "void *" to encourage the debugger to always display in hex,
  2277. // which matches the displayed offsets used by ByteCodeDumper.
  2278. //
  2279. this->DEBUG_currentByteOffset = (void *)m_reader.GetCurrentOffset();
  2280. #endif
  2281. #if ENABLE_TTD
  2282. AssertMsg(this->scriptContext->GetThreadContext()->IsRuntimeInTTDMode(), "We never be fetching an opcode via this path if this is not true!!!");
  2283. #endif
  2284. if (SHOULD_DO_TTD_STACK_STMT_OP(this->scriptContext))
  2285. {
  2286. TTD::ExecutionInfoManager* executionMgr = this->scriptContext->GetThreadContext()->TTDExecutionInfo;
  2287. executionMgr->UpdateCurrentStatementInfo(m_reader.GetCurrentOffset());
  2288. }
  2289. OpCodeType op = (OpCodeType)ReadOpFunc(ip);
  2290. #if DBG_DUMP
  2291. TracingFunc(this, op);
  2292. #endif
  2293. return op;
  2294. }
  2295. #endif
  2296. _NOINLINE
  2297. Var InterpreterStackFrame::ProcessThunk(void* address, void* addressOfReturnAddress)
  2298. {
  2299. PushPopFrameHelper pushPopFrameHelper(this, address, addressOfReturnAddress);
  2300. return this->Process();
  2301. }
  2302. #ifdef ASMJS_PLAT
  2303. Var InterpreterStackFrame::ProcessAsmJsModule()
  2304. {
  2305. FunctionBody* asmJsModuleFunctionBody = GetFunctionBody();
  2306. AsmJsModuleInfo* info = asmJsModuleFunctionBody->GetAsmJsModuleInfo();
  2307. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  2308. if (Configuration::Global.flags.ForceAsmJsLinkFail)
  2309. {
  2310. AsmJSCompiler::OutputError(this->scriptContext, _u("Asm.js Runtime Error : Forcing link failure"));
  2311. return this->ProcessLinkFailedAsmJsModule();
  2312. }
  2313. #endif
  2314. if (m_inSlotsCount != info->GetArgInCount() + 1)
  2315. {
  2316. // Error reparse without asm.js
  2317. AsmJSCompiler::OutputError(this->scriptContext, _u("Asm.js Runtime Error : Invalid module argument count"));
  2318. return this->ProcessLinkFailedAsmJsModule();
  2319. }
  2320. const AsmJsModuleMemory& moduleMemory = info->GetModuleMemory();
  2321. Field(Var)* moduleMemoryPtr = RecyclerNewArray(scriptContext->GetRecycler(), Field(Var), moduleMemory.mMemorySize);
  2322. Field(Var)* arrayBufferPtr = moduleMemoryPtr + moduleMemory.mArrayBufferOffset;
  2323. Assert(moduleMemory.mArrayBufferOffset == AsmJsModuleMemory::MemoryTableBeginOffset);
  2324. Field(Var)* stdLibPtr = moduleMemoryPtr + moduleMemory.mStdLibOffset;
  2325. int* localIntSlots = (int*)(moduleMemoryPtr + moduleMemory.mIntOffset);
  2326. float* localFloatSlots = (float*)(moduleMemoryPtr + moduleMemory.mFloatOffset);
  2327. double* localDoubleSlots = (double*)(moduleMemoryPtr + moduleMemory.mDoubleOffset);
  2328. Field(Var)* localFunctionImports = moduleMemoryPtr + moduleMemory.mFFIOffset;
  2329. Field(Var)* localModuleFunctions = moduleMemoryPtr + moduleMemory.mFuncOffset;
  2330. Field(Field(Var)*)* localFunctionTables = (Field(Field(Var)*)*)(moduleMemoryPtr + moduleMemory.mFuncPtrOffset);
  2331. ThreadContext* threadContext = this->scriptContext->GetThreadContext();
  2332. *stdLibPtr = (m_inSlotsCount > 1) ? m_inParams[1] : nullptr;
  2333. Var foreign = (m_inSlotsCount > 2) ? m_inParams[2] : nullptr;
  2334. *arrayBufferPtr = (m_inSlotsCount > 3) ? m_inParams[3] : nullptr;
  2335. //cache the current state of the disable implicit call flag
  2336. DisableImplicitFlags prevDisableImplicitFlags = threadContext->GetDisableImplicitFlags();
  2337. ImplicitCallFlags saveImplicitcallFlags = threadContext->GetImplicitCallFlags();
  2338. // Disable implicit calls to check if any of the VarImport or Function Import leads to implicit calls
  2339. threadContext->DisableImplicitCall();
  2340. threadContext->SetImplicitCallFlags(ImplicitCallFlags::ImplicitCall_None);
  2341. bool checkParamResult = ASMLink::CheckParams(this->scriptContext, info, *stdLibPtr, foreign, *arrayBufferPtr);
  2342. if (!checkParamResult)
  2343. {
  2344. // don't need to print, because checkParams will do it for us
  2345. goto linkFailure;
  2346. }
  2347. else if (this->CheckAndResetImplicitCall(prevDisableImplicitFlags, saveImplicitcallFlags))
  2348. {
  2349. AsmJSCompiler::OutputError(this->scriptContext, _u("Asm.js Runtime Error : Params have side effects"));
  2350. return this->ProcessLinkFailedAsmJsModule();
  2351. }
  2352. // Initialize Variables
  2353. for (int i = 0; i < info->GetVarCount(); i++)
  2354. {
  2355. const auto& var = info->GetVar(i);
  2356. const AsmJsVarType type(var.type);
  2357. if (type.isInt())
  2358. {
  2359. localIntSlots[var.location] = var.initialiser.intInit;
  2360. }
  2361. else if (type.isFloat())
  2362. {
  2363. localFloatSlots[var.location] = var.initialiser.floatInit;
  2364. }
  2365. else if (type.isDouble())
  2366. {
  2367. localDoubleSlots[var.location] = var.initialiser.doubleInit;
  2368. }
  2369. else
  2370. {
  2371. Assert(UNREACHED);
  2372. }
  2373. }
  2374. // Load constant variables
  2375. for (int i = 0; i < info->GetVarImportCount(); i++)
  2376. {
  2377. const auto& import = info->GetVarImport(i);
  2378. const AsmJsVarType type(import.type);
  2379. // this might throw, but it would anyway in non-asm.js
  2380. Var value = JavascriptOperators::OP_GetProperty(foreign, import.field, scriptContext);
  2381. // check if there is implicit call and if there is implicit call then clear the disableimplicitcall flag
  2382. if (this->CheckAndResetImplicitCall(prevDisableImplicitFlags, saveImplicitcallFlags))
  2383. {
  2384. AsmJSCompiler::OutputError(this->scriptContext, _u("Asm.js Runtime Error : Accessing var import %s has side effects"), this->scriptContext->GetPropertyName(import.field)->GetBuffer());
  2385. return this->ProcessLinkFailedAsmJsModule();
  2386. }
  2387. if (CONFIG_FLAG(AsmJsEdge))
  2388. {
  2389. // emscripten had a bug which caused this check to fail in some circumstances, so this check fails for some demos
  2390. if (!TaggedNumber::Is(value) && (!VarIs<RecyclableObject>(value) || DynamicType::Is(VarTo<RecyclableObject>(value)->GetTypeId())))
  2391. {
  2392. AsmJSCompiler::OutputError(this->scriptContext, _u("Asm.js Runtime Error : Var import %s must be primitive"), this->scriptContext->GetPropertyName(import.field)->GetBuffer());
  2393. goto linkFailure;
  2394. }
  2395. }
  2396. if (type.isInt())
  2397. {
  2398. int val = JavascriptMath::ToInt32(value, scriptContext);
  2399. localIntSlots[import.location] = val;
  2400. }
  2401. else if (type.isFloat())
  2402. {
  2403. float val = (float)JavascriptConversion::ToNumber(value, scriptContext);
  2404. localFloatSlots[import.location] = val;
  2405. }
  2406. else if (type.isDouble())
  2407. {
  2408. double val = JavascriptConversion::ToNumber(value, scriptContext);
  2409. localDoubleSlots[import.location] = val;
  2410. }
  2411. // check for implicit call after converting to number
  2412. if (this->CheckAndResetImplicitCall(prevDisableImplicitFlags, saveImplicitcallFlags))
  2413. {
  2414. // Runtime error
  2415. AsmJSCompiler::OutputError(this->scriptContext, _u("Asm.js Runtime Error : Accessing var import %s has side effects"), this->scriptContext->GetPropertyName(import.field)->GetBuffer());
  2416. return this->ProcessLinkFailedAsmJsModule();
  2417. }
  2418. }
  2419. // Load external functions
  2420. for (int i = 0; i < info->GetFunctionImportCount(); i++)
  2421. {
  2422. const auto& import = info->GetFunctionImport(i);
  2423. // this might throw, but it would anyway in non-asm.js
  2424. Var importFunc = JavascriptOperators::OP_GetProperty(foreign, import.field, scriptContext);
  2425. // check if there is implicit call and if there is implicit call then clear the disableimplicitcall flag
  2426. if (this->CheckAndResetImplicitCall(prevDisableImplicitFlags, saveImplicitcallFlags))
  2427. {
  2428. AsmJSCompiler::OutputError(this->scriptContext, _u("Asm.js Runtime Error : Accessing foreign function import %s has side effects"), this->scriptContext->GetPropertyName(import.field)->GetBuffer());
  2429. return this->ProcessLinkFailedAsmJsModule();
  2430. }
  2431. if (!VarIs<JavascriptFunction>(importFunc))
  2432. {
  2433. AsmJSCompiler::OutputError(this->scriptContext, _u("Asm.js Runtime Error : Foreign function import %s is not a function"), this->scriptContext->GetPropertyName(import.field)->GetBuffer());
  2434. goto linkFailure;
  2435. }
  2436. localFunctionImports[import.location] = importFunc;
  2437. }
  2438. threadContext->SetDisableImplicitFlags(prevDisableImplicitFlags);
  2439. threadContext->SetImplicitCallFlags(saveImplicitcallFlags);
  2440. // scope
  2441. {
  2442. FrameDisplay* pDisplay = RecyclerNewPlus(scriptContext->GetRecycler(), sizeof(void*), FrameDisplay, 0);
  2443. //DynamicObject* asmModule = scriptContext->GetLibrary()->CreateObject(false, 1);
  2444. //JavascriptOperators::OP_SetProperty(asmModule, PropertyIds::module, moduleMemoryPtr, scriptContext);
  2445. //pDisplay->SetItem(0, this->function);
  2446. for (int i = 0; i < info->GetFunctionCount(); i++)
  2447. {
  2448. const auto& modFunc = info->GetFunction(i);
  2449. // TODO: add more runtime checks here
  2450. FunctionInfoPtrPtr functionInfo = m_functionBody->GetNestedFuncReference(i);
  2451. AsmJsScriptFunction* scriptFuncObj = AsmJsScriptFunction::OP_NewAsmJsFunc(pDisplay, functionInfo);
  2452. localModuleFunctions[modFunc.location] = scriptFuncObj;
  2453. if (scriptFuncObj->GetDynamicType()->GetEntryPoint() == DefaultDeferredDeserializeThunk)
  2454. {
  2455. JavascriptFunction::DeferredDeserialize(scriptFuncObj);
  2456. }
  2457. scriptFuncObj->GetDynamicType()->SetEntryPoint(AsmJsExternalEntryPoint);
  2458. scriptFuncObj->GetFunctionBody()->GetAsmJsFunctionInfo()->SetModuleFunctionBody(asmJsModuleFunctionBody);
  2459. scriptFuncObj->SetModuleEnvironment(moduleMemoryPtr);
  2460. if (!info->IsRuntimeProcessed())
  2461. {
  2462. // don't reset entrypoint upon relinking
  2463. FunctionEntryPointInfo* entrypointInfo = (FunctionEntryPointInfo*)scriptFuncObj->GetEntryPointInfo();
  2464. entrypointInfo->SetIsAsmJSFunction(true);
  2465. #if DYNAMIC_INTERPRETER_THUNK
  2466. if (!PHASE_ON1(AsmJsJITTemplatePhase))
  2467. {
  2468. entrypointInfo->jsMethod = AsmJsDefaultEntryThunk;
  2469. }
  2470. #endif
  2471. }
  2472. }
  2473. }
  2474. // Initialize function table arrays
  2475. for (int i = 0; i < info->GetFunctionTableCount(); i++)
  2476. {
  2477. const auto& modFuncTable = info->GetFunctionTable(i);
  2478. Field(Var)* funcTableArray = RecyclerNewArray(scriptContext->GetRecycler(), Field(Var), modFuncTable.size);
  2479. for (uint j = 0; j < modFuncTable.size; j++)
  2480. {
  2481. // get the module function index
  2482. const RegSlot index = modFuncTable.moduleFunctionIndex[j];
  2483. // assign the module function pointer to the array
  2484. Var functionPtr = localModuleFunctions[index];
  2485. funcTableArray[j] = functionPtr;
  2486. }
  2487. localFunctionTables[i] = funcTableArray;
  2488. }
  2489. // Do MTJRC/MAIC:0 check
  2490. #if ENABLE_DEBUG_CONFIG_OPTIONS
  2491. if (
  2492. (PHASE_ON1(Js::AsmJsJITTemplatePhase) && CONFIG_FLAG(MaxTemplatizedJitRunCount) == 0) ||
  2493. (!PHASE_ON1(Js::AsmJsJITTemplatePhase) && (CONFIG_FLAG(MaxAsmJsInterpreterRunCount) == 0 || CONFIG_FLAG(ForceNative)))
  2494. )
  2495. {
  2496. if (PHASE_TRACE1(AsmjsEntryPointInfoPhase))
  2497. {
  2498. Output::Print(_u("%s Scheduling For Full JIT at callcount:%d\n"), asmJsModuleFunctionBody->GetDisplayName(), 0);
  2499. Output::Flush();
  2500. }
  2501. for (int i = 0; i < info->GetFunctionCount(); i++)
  2502. {
  2503. ScriptFunction* functionObj = (ScriptFunction*)PointerValue(localModuleFunctions[i]);
  2504. AnalysisAssert(functionObj != nullptr);
  2505. // don't want to generate code for APIs like changeHeap
  2506. if (functionObj->GetEntryPoint() == Js::AsmJsExternalEntryPoint)
  2507. {
  2508. WAsmJs::JitFunctionIfReady(functionObj);
  2509. }
  2510. }
  2511. }
  2512. #endif
  2513. info->SetIsRuntimeProcessed(true);
  2514. // create export object
  2515. if (info->GetExportsCount())
  2516. {
  2517. Var newObj = JavascriptOperators::NewScObjectLiteral(GetScriptContext(), info->GetExportsIdArray(),
  2518. this->GetFunctionBody()->GetObjectLiteralTypeRef(0));
  2519. for (int i = 0; i < info->GetExportsCount(); i++)
  2520. {
  2521. auto ex = info->GetExport(i);
  2522. Var func = localModuleFunctions[*ex.location];
  2523. JavascriptOperators::OP_InitProperty(newObj, *ex.id, func);
  2524. }
  2525. SetReg((RegSlot)0, newObj);
  2526. return newObj;
  2527. }
  2528. // export only 1 function
  2529. {
  2530. Var exportFunc = localModuleFunctions[info->GetExportFunctionIndex()];
  2531. SetReg((RegSlot)0, exportFunc);
  2532. return exportFunc;
  2533. }
  2534. linkFailure:
  2535. threadContext->SetDisableImplicitFlags(prevDisableImplicitFlags);
  2536. threadContext->SetImplicitCallFlags(saveImplicitcallFlags);
  2537. return this->ProcessLinkFailedAsmJsModule();
  2538. }
  2539. Var InterpreterStackFrame::ProcessLinkFailedAsmJsModule()
  2540. {
  2541. AsmJSCompiler::OutputError(this->scriptContext, _u("asm.js linking failed."));
  2542. Js::FunctionBody* asmJsModuleFunctionBody = GetFunctionBody();
  2543. AsmJsModuleInfo* info = asmJsModuleFunctionBody->GetAsmJsModuleInfo();
  2544. // do not support relinking with failed relink
  2545. if (info->IsRuntimeProcessed())
  2546. {
  2547. Js::Throw::OutOfMemory();
  2548. }
  2549. ScriptFunction * funcObj = GetJavascriptFunction();
  2550. ScriptFunction::ReparseAsmJsModule(&funcObj);
  2551. const bool doProfile =
  2552. funcObj->GetFunctionBody()->GetInterpreterExecutionMode(false) == ExecutionMode::ProfilingInterpreter ||
  2553. (funcObj->GetFunctionBody()->IsInDebugMode() && DynamicProfileInfo::IsEnabled(funcObj->GetFunctionBody()));
  2554. DynamicProfileInfo * dynamicProfileInfo = nullptr;
  2555. if (doProfile)
  2556. {
  2557. dynamicProfileInfo = funcObj->GetFunctionBody()->GetDynamicProfileInfo();
  2558. funcObj->GetScriptContext()->GetThreadContext()->ClearImplicitCallFlags();
  2559. }
  2560. // after reparsing, we want to also use a new interpreter stack frame, as it will have different characteristics than the asm.js version
  2561. InterpreterStackFrame::Setup setup(funcObj, m_inParams, m_inSlotsCount);
  2562. size_t varAllocCount = setup.GetAllocationVarCount();
  2563. size_t stackVarAllocCount = setup.GetStackAllocationVarCount();
  2564. size_t varSizeInBytes;
  2565. Var* allocation = nullptr;
  2566. Var* stackAllocation = nullptr;
  2567. DWORD_PTR stackAddr;
  2568. bool fReleaseAlloc = false;
  2569. if ((varAllocCount + stackVarAllocCount) > InterpreterStackFrame::LocalsThreshold)
  2570. {
  2571. ArenaAllocator *tmpAlloc = nullptr;
  2572. fReleaseAlloc = GetScriptContext()->EnsureInterpreterArena(&tmpAlloc);
  2573. varSizeInBytes = varAllocCount * sizeof(Var);
  2574. allocation = (Var*)tmpAlloc->Alloc(varSizeInBytes);
  2575. if (stackVarAllocCount != 0)
  2576. {
  2577. size_t stackVarSizeInBytes = stackVarAllocCount * sizeof(Var);
  2578. PROBE_STACK_PARTIAL_INITIALIZED_INTERPRETER_FRAME(GetScriptContext(), Js::Constants::MinStackInterpreter + stackVarSizeInBytes);
  2579. stackAllocation = (Var*)_alloca(stackVarSizeInBytes);
  2580. }
  2581. // use a stack address so the debugger stepping logic works (step-out, for example, compares stack depths to determine when to complete the step)
  2582. // debugger stepping does not matter here, but it's worth being consistent with normal stack frame
  2583. stackAddr = reinterpret_cast<DWORD_PTR>(&allocation);
  2584. }
  2585. else
  2586. {
  2587. varSizeInBytes = (varAllocCount + stackVarAllocCount) * sizeof(Var);
  2588. PROBE_STACK_PARTIAL_INITIALIZED_INTERPRETER_FRAME(GetScriptContext(), Js::Constants::MinStackInterpreter + varSizeInBytes);
  2589. allocation = (Var*)_alloca(varSizeInBytes);
  2590. stackAddr = reinterpret_cast<DWORD_PTR>(allocation);
  2591. }
  2592. #if DBG
  2593. Var invalidStackVar = (Js::RecyclableObject*)_alloca(sizeof(Js::RecyclableObject));
  2594. memset(invalidStackVar, 0xFE, sizeof(Js::RecyclableObject));
  2595. #endif
  2596. InterpreterStackFrame * newInstance = setup.InitializeAllocation(allocation, stackAllocation, funcObj->GetFunctionBody()->GetHasImplicitArgIns(), doProfile, nullptr, stackAddr
  2597. #if DBG
  2598. , invalidStackVar
  2599. #endif
  2600. );
  2601. newInstance->m_reader.Create(funcObj->GetFunctionBody());
  2602. // now that we have set up the new frame, let's interpret it!
  2603. funcObj->GetFunctionBody()->BeginExecution();
  2604. PushPopFrameHelper pushPopFrameHelper(newInstance, this->returnAddress, this->addressOfReturnAddress);
  2605. Var retVal = newInstance->ProcessUnprofiled();
  2606. if (doProfile)
  2607. {
  2608. dynamicProfileInfo->RecordImplicitCallFlags(GetScriptContext()->GetThreadContext()->GetImplicitCallFlags());
  2609. }
  2610. if (fReleaseAlloc)
  2611. {
  2612. GetScriptContext()->ReleaseInterpreterArena();
  2613. }
  2614. return retVal;
  2615. }
  2616. #if ENABLE_DEBUG_CONFIG_OPTIONS
  2617. int AsmJsCallDepth = 0;
  2618. #endif
  2619. // Function memory allocation should be done the same way as
  2620. // T AsmJsCommunEntryPoint(Js::ScriptFunction* func, ...) (AsmJSJitTemplate.cpp)
  2621. // update any changes there
  2622. /*
  2623. This function does the following fixup
  2624. Stack Before Stack After
  2625. ============== ================
  2626. | VarConstants | | VarConstants |
  2627. |--------------| |-----------------
  2628. | IntConstants | | IntConstants |
  2629. |--------------| | ------------ |
  2630. | FloatConst | | Int Vars+Tmps |
  2631. |--------------| |----------------|
  2632. | DoubleConst | | FloatConst |
  2633. |--------------| | ---------- |
  2634. | Var&Temps | | Flt Vars+tmps |
  2635. |==============| |----------------|
  2636. | DoubleConst |
  2637. | ----------- |
  2638. | Dbl Vars+Tmps |
  2639. ================
  2640. intSrc,FltSrc&DblSrc are pointers to the stack before the change
  2641. m_localIntSlots,m_localFloatSlots,m_localDoubleSlots are pointers to the stack after the change
  2642. */
  2643. void InterpreterStackFrame::AlignMemoryForAsmJs()
  2644. {
  2645. FunctionBody *const functionBody = GetFunctionBody();
  2646. ScriptFunction* func = GetJavascriptFunction();
  2647. uint32& callCount = ((FunctionEntryPointInfo*)func->GetEntryPointInfo())->callsCount;
  2648. WAsmJs::JitFunctionIfReady(func, ++callCount);
  2649. AsmJsFunctionInfo* info = functionBody->GetAsmJsFunctionInfo();
  2650. // The const table is copied after the FirstRegSlot
  2651. byte* constTable = (byte*)(m_localSlots + FunctionBody::FirstRegSlot);
  2652. byte* slotsStart = (byte*)m_localSlots;
  2653. // Must do in reverse order to avoid overwriting const of other type as we move things around
  2654. for (int i = WAsmJs::LIMIT - 1; i >= 0; --i)
  2655. {
  2656. WAsmJs::Types type = (WAsmJs::Types)i;
  2657. auto typeInfo = info->GetTypedSlotInfo(type);
  2658. byte* destination = slotsStart + typeInfo->byteOffset;
  2659. switch (type)
  2660. {
  2661. case WAsmJs::INT32: m_localIntSlots = (int*)destination; break;
  2662. case WAsmJs::INT64: m_localInt64Slots = (int64*)destination; break;
  2663. case WAsmJs::FLOAT32: m_localFloatSlots = (float*)destination; break;
  2664. case WAsmJs::FLOAT64: m_localDoubleSlots = (double*)destination; break;
  2665. case WAsmJs::SIMD: m_localSimdSlots = (AsmJsSIMDValue*)destination; break;
  2666. default:
  2667. CompileAssert(WAsmJs::SIMD == WAsmJs::LastType);
  2668. Assert(false);
  2669. break;
  2670. }
  2671. byte* source = constTable + typeInfo->constSrcByteOffset;
  2672. if (typeInfo->constCount > 0 && source != destination)
  2673. {
  2674. // Make sure slots are aligned for this type
  2675. Assert(::Math::Align<intptr_t>((intptr_t)destination, (intptr_t)WAsmJs::GetTypeByteSize(type)) == (intptr_t)destination);
  2676. Assert(typeInfo->constSrcByteOffset != Js::Constants::InvalidOffset);
  2677. uint constByteSize = typeInfo->constCount * WAsmJs::GetTypeByteSize(type);
  2678. memmove_s(destination, constByteSize, source, constByteSize);
  2679. }
  2680. }
  2681. // Load module environment
  2682. AsmJsScriptFunction* asmJsFunc = VarTo<AsmJsScriptFunction>(this->function);
  2683. m_localSlots[AsmJsFunctionMemory::ModuleEnvRegister] = asmJsFunc->GetModuleEnvironment();
  2684. m_localSlots[AsmJsFunctionMemory::ArrayBufferRegister] = nullptr;
  2685. #ifdef ENABLE_WASM
  2686. if (VarIs<WasmScriptFunction>(func))
  2687. {
  2688. WasmScriptFunction* wasmFunc = VarTo<WasmScriptFunction>(func);
  2689. m_wasmMemory = wasmFunc->GetWebAssemblyMemory();
  2690. m_signatures = func->GetFunctionBody()->GetAsmJsFunctionInfo()->GetWebAssemblyModule()->GetSignatures();
  2691. }
  2692. else
  2693. #endif
  2694. {
  2695. m_asmJsBuffer = asmJsFunc->GetAsmJsArrayBuffer();
  2696. }
  2697. m_localSlots[AsmJsFunctionMemory::ArraySizeRegister] = 0; // do not cache ArraySize in the interpreter
  2698. m_localSlots[AsmJsFunctionMemory::ScriptContextBufferRegister] = functionBody->GetScriptContext();
  2699. int* intArg = m_localIntSlots + info->GetTypedSlotInfo(WAsmJs::INT32)->constCount;
  2700. int64* int64Arg = m_localInt64Slots + info->GetTypedSlotInfo(WAsmJs::INT64)->constCount;
  2701. double* doubleArg = m_localDoubleSlots + info->GetTypedSlotInfo(WAsmJs::FLOAT64)->constCount;
  2702. float* floatArg = m_localFloatSlots + info->GetTypedSlotInfo(WAsmJs::FLOAT32)->constCount;
  2703. #ifdef ENABLE_WASM_SIMD
  2704. AsmJsSIMDValue* simdArg = m_localSimdSlots + info->GetTypedSlotInfo(WAsmJs::SIMD)->constCount;
  2705. #endif
  2706. // Move the arguments to the right location
  2707. ArgSlot argCount = info->GetArgCount();
  2708. #if _M_X64 && _WIN32
  2709. uint homingAreaSize = 0;
  2710. #endif
  2711. #if ENABLE_DEBUG_CONFIG_OPTIONS
  2712. const bool tracingFunc = PHASE_TRACE(AsmjsFunctionEntryPhase, functionBody);
  2713. if (tracingFunc)
  2714. {
  2715. if (AsmJsCallDepth)
  2716. {
  2717. Output::Print(_u("%*c"), AsmJsCallDepth, ' ');
  2718. }
  2719. Output::Print(_u("Executing function %s("), functionBody->GetDisplayName());
  2720. ++AsmJsCallDepth;
  2721. }
  2722. #endif
  2723. uintptr_t argAddress = (uintptr_t)m_inParams;
  2724. for (ArgSlot i = 0; i < argCount; i++)
  2725. {
  2726. #if _M_X64 && _WIN32
  2727. // 3rd Argument should be at the end of the homing area.
  2728. Assert(i != 3 || argAddress == (uintptr_t)m_inParams + homingAreaSize);
  2729. if (i < 3)
  2730. {
  2731. // for x64 we spill the first 3 floating point args below the rest of the arguments on the stack
  2732. // m_inParams will be from DynamicInterpreterThunk's frame. Floats are in InterpreterAsmThunk's frame. Stack will be set up like so:
  2733. // DIT arg2 <- first scriptArg, m_inParams points here
  2734. // DIT arg1
  2735. // padding
  2736. // IAT r9 home
  2737. // IAT r8 home
  2738. // IAT rdx home
  2739. // IAT rcx home
  2740. // IAT return address
  2741. // IAT push rbp
  2742. // IAT padding
  2743. // IAT xmm3 spill
  2744. // IAT xmm2 spill
  2745. // IAT xmm1 spill <- floatSpillAddress for arg1
  2746. #define FLOAT_SPILL_ADDRESS_OFFSET_WORDS 15
  2747. // floats are spilled as xmmwords
  2748. uintptr_t floatSpillAddress = (uintptr_t)m_inParams - MachPtr * (FLOAT_SPILL_ADDRESS_OFFSET_WORDS - 2 * i);
  2749. if (info->GetArgType(i).isInt())
  2750. {
  2751. *intArg = *(int*)argAddress;
  2752. #if ENABLE_DEBUG_CONFIG_OPTIONS
  2753. if (tracingFunc)
  2754. {
  2755. Output::Print(_u("%d, "), *intArg);
  2756. }
  2757. #endif
  2758. ++intArg;
  2759. homingAreaSize += MachPtr;
  2760. }
  2761. else if (info->GetArgType(i).isInt64())
  2762. {
  2763. *int64Arg = *(int64*)argAddress;
  2764. #if ENABLE_DEBUG_CONFIG_OPTIONS
  2765. if (tracingFunc)
  2766. {
  2767. Output::Print(_u("%lld, "), *int64Arg);
  2768. }
  2769. #endif
  2770. ++int64Arg;
  2771. homingAreaSize += MachPtr;
  2772. }
  2773. else if (info->GetArgType(i).isFloat())
  2774. {
  2775. *floatArg = *(float*)floatSpillAddress;
  2776. #if ENABLE_DEBUG_CONFIG_OPTIONS
  2777. if (tracingFunc)
  2778. {
  2779. Output::Print(_u("%.2f, "), *floatArg);
  2780. }
  2781. #endif
  2782. ++floatArg;
  2783. homingAreaSize += MachPtr;
  2784. }
  2785. else if (info->GetArgType(i).isDouble())
  2786. {
  2787. *doubleArg = *(double*)floatSpillAddress;
  2788. #if ENABLE_DEBUG_CONFIG_OPTIONS
  2789. if (tracingFunc)
  2790. {
  2791. Output::Print(_u("%.2f, "), *doubleArg);
  2792. }
  2793. #endif
  2794. ++doubleArg;
  2795. homingAreaSize += MachPtr;
  2796. }
  2797. else
  2798. {
  2799. #ifdef ENABLE_WASM_SIMD
  2800. Assert(info->GetArgType(i).isSIMD());
  2801. *simdArg = *(AsmJsSIMDValue*)floatSpillAddress;
  2802. ++simdArg;
  2803. homingAreaSize += sizeof(AsmJsSIMDValue);
  2804. #else
  2805. Assert(UNREACHED);
  2806. #endif
  2807. }
  2808. if (Wasm::Simd::IsEnabled() && i == 2) // last argument ?
  2809. {
  2810. // If we have simd arguments, the homing area in m_inParams can be larger than 3 64-bit slots. This is because SIMD values are unboxed there too.
  2811. // After unboxing, the homing area is overwritten by rdx, r8 and r9, and we read/skip 64-bit slots from the homing area (argAddress += MachPtr).
  2812. // After the last argument of the 3 is read, we need to advance argAddress to skip over the possible extra space and to the start of the rest of the arguments.
  2813. argAddress = (uintptr_t)m_inParams + homingAreaSize;
  2814. }
  2815. else
  2816. {
  2817. argAddress += MachPtr;
  2818. }
  2819. }
  2820. else
  2821. #endif
  2822. if (info->GetArgType(i).isInt())
  2823. {
  2824. *intArg = *(int*)argAddress;
  2825. #if ENABLE_DEBUG_CONFIG_OPTIONS
  2826. if (tracingFunc)
  2827. {
  2828. Output::Print(_u("%d, "), *intArg);
  2829. }
  2830. #endif
  2831. ++intArg;
  2832. argAddress += MachPtr;
  2833. }
  2834. else if (info->GetArgType(i).isInt64())
  2835. {
  2836. *int64Arg = *(int64*)argAddress;
  2837. #if ENABLE_DEBUG_CONFIG_OPTIONS
  2838. if (tracingFunc)
  2839. {
  2840. Output::Print(_u("%lld, "), *int64Arg);
  2841. }
  2842. #endif
  2843. ++int64Arg;
  2844. argAddress += sizeof(int64);
  2845. }
  2846. else if (info->GetArgType(i).isFloat())
  2847. {
  2848. *floatArg = *(float*)argAddress;
  2849. #if ENABLE_DEBUG_CONFIG_OPTIONS
  2850. if (tracingFunc)
  2851. {
  2852. Output::Print(_u("%.2f, "), *floatArg);
  2853. }
  2854. #endif
  2855. ++floatArg;
  2856. argAddress += MachPtr;
  2857. }
  2858. else if (info->GetArgType(i).isDouble())
  2859. {
  2860. Assert(info->GetArgType(i).isDouble());
  2861. *doubleArg = *(double*)argAddress;
  2862. #if ENABLE_DEBUG_CONFIG_OPTIONS
  2863. if (tracingFunc)
  2864. {
  2865. Output::Print(_u("%.2f, "), *doubleArg);
  2866. }
  2867. #endif
  2868. ++doubleArg;
  2869. argAddress += sizeof(double);
  2870. }
  2871. #ifdef ENABLE_WASM_SIMD
  2872. else if (Wasm::Simd::IsEnabled() && info->GetArgType(i).isSIMD())
  2873. {
  2874. *simdArg = *(AsmJsSIMDValue*)argAddress;
  2875. ++simdArg;
  2876. argAddress += sizeof(AsmJsSIMDValue);
  2877. }
  2878. #endif
  2879. else
  2880. {
  2881. AssertMsg(UNREACHED, "Invalid function arg type.");
  2882. }
  2883. }
  2884. #if ENABLE_DEBUG_CONFIG_OPTIONS
  2885. if (tracingFunc)
  2886. {
  2887. Output::Print(_u("){\n"));
  2888. Output::Flush();
  2889. }
  2890. #endif
  2891. }
  2892. #endif
  2893. ///----------------------------------------------------------------------------
  2894. ///
  2895. /// InterpreterStackFrame::Process
  2896. ///
  2897. /// Process() processes a single loop of execution for the current
  2898. /// JavascriptFunction being executed:
  2899. /// - Individual instructions are dispatched to specific handlers for different
  2900. /// OpCodes.
  2901. ///
  2902. ///----------------------------------------------------------------------------
  2903. #if ENABLE_PROFILE_INFO
  2904. #define INTERPRETERLOOPNAME ProcessProfiled
  2905. #define PROVIDE_INTERPRETERPROFILE
  2906. #include "InterpreterLoop.inl"
  2907. #undef PROVIDE_INTERPRETERPROFILE
  2908. #undef INTERPRETERLOOPNAME
  2909. #endif
  2910. #define INTERPRETERLOOPNAME ProcessUnprofiled
  2911. #include "InterpreterLoop.inl"
  2912. #undef INTERPRETERLOOPNAME
  2913. #if ENABLE_TTD_DIAGNOSTICS_TRACING
  2914. #define PROVIDE_INTERPRETER_STMTS
  2915. #define INTERPRETERLOOPNAME ProcessUnprofiled_PreviousStmtTracking
  2916. #include "InterpreterLoop.inl"
  2917. #undef INTERPRETERLOOPNAME
  2918. #undef PROVIDE_INTERPRETER_STMTS
  2919. #endif
  2920. #ifdef ASMJS_PLAT
  2921. #define INTERPRETERLOOPNAME ProcessAsmJs
  2922. #define INTERPRETER_ASMJS
  2923. #include "InterpreterProcessOpCodeAsmJs.h"
  2924. #include "InterpreterLoop.inl"
  2925. #undef INTERPRETER_ASMJS
  2926. #undef INTERPRETERLOOPNAME
  2927. #endif
  2928. // For now, always collect profile data when debugging,
  2929. // otherwise the backend will be confused if there's no profile data.
  2930. #ifdef ENABLE_SCRIPT_DEBUGGING
  2931. #define INTERPRETERLOOPNAME ProcessWithDebugging
  2932. #define PROVIDE_DEBUGGING
  2933. #if ENABLE_PROFILE_INFO
  2934. #define PROVIDE_INTERPRETERPROFILE
  2935. #endif
  2936. #include "InterpreterLoop.inl"
  2937. #if ENABLE_PROFILE_INFO
  2938. #undef PROVIDE_INTERPRETERPROFILE
  2939. #endif
  2940. #undef PROVIDE_DEBUGGING
  2941. #undef INTERPRETERLOOPNAME
  2942. #endif
  2943. #if ENABLE_TTD
  2944. #define PROVIDE_INTERPRETER_STMTS
  2945. #define INTERPRETERLOOPNAME ProcessWithDebugging_PreviousStmtTracking
  2946. #define PROVIDE_DEBUGGING
  2947. #if ENABLE_PROFILE_INFO
  2948. #define PROVIDE_INTERPRETERPROFILE
  2949. #endif
  2950. #include "InterpreterLoop.inl"
  2951. #if ENABLE_PROFILE_INFO
  2952. #undef PROVIDE_INTERPRETERPROFILE
  2953. #endif
  2954. #undef PROVIDE_DEBUGGING
  2955. #undef INTERPRETERLOOPNAME
  2956. #undef PROVIDE_INTERPRETER_STMTS
  2957. #endif
  2958. Var InterpreterStackFrame::Process()
  2959. {
  2960. #if ENABLE_PROFILE_INFO
  2961. class AutoRestore
  2962. {
  2963. private:
  2964. InterpreterStackFrame * const interpreterStackFrame;
  2965. const uint32 savedSwitchProfileModeOnLoopEndNumber;
  2966. const bool savedIsAutoProfiling;
  2967. const bool savedSwitchProfileMode;
  2968. public:
  2969. AutoRestore(InterpreterStackFrame *const interpreterStackFrame)
  2970. : interpreterStackFrame(interpreterStackFrame),
  2971. savedIsAutoProfiling(interpreterStackFrame->isAutoProfiling),
  2972. savedSwitchProfileMode(interpreterStackFrame->switchProfileMode),
  2973. savedSwitchProfileModeOnLoopEndNumber(interpreterStackFrame->switchProfileModeOnLoopEndNumber)
  2974. {
  2975. }
  2976. ~AutoRestore()
  2977. {
  2978. interpreterStackFrame->isAutoProfiling = savedIsAutoProfiling;
  2979. interpreterStackFrame->switchProfileMode = savedSwitchProfileMode;
  2980. interpreterStackFrame->switchProfileModeOnLoopEndNumber = savedSwitchProfileModeOnLoopEndNumber;
  2981. }
  2982. } autoRestore(this);
  2983. #endif
  2984. if (this->ehBailoutData && !this->TestFlags(InterpreterStackFrameFlags_FromBailOutInInlinee))
  2985. {
  2986. if (this->TestFlags(Js::InterpreterStackFrameFlags_FromBailOut) && !this->TestFlags(InterpreterStackFrameFlags_ProcessingBailOutFromEHCode))
  2987. {
  2988. this->OrFlags(Js::InterpreterStackFrameFlags_ProcessingBailOutFromEHCode);
  2989. EHBailoutData * topLevelEHBailoutData = this->ehBailoutData;
  2990. while (topLevelEHBailoutData->parent->nestingDepth != -1)
  2991. {
  2992. topLevelEHBailoutData->parent->child = topLevelEHBailoutData;
  2993. topLevelEHBailoutData = topLevelEHBailoutData->parent;
  2994. }
  2995. ProcessTryHandlerBailout(topLevelEHBailoutData, this->ehBailoutData->nestingDepth);
  2996. this->ClearFlags(Js::InterpreterStackFrameFlags_ProcessingBailOutFromEHCode);
  2997. this->ehBailoutData = nullptr;
  2998. }
  2999. }
  3000. #ifdef ASMJS_PLAT
  3001. if (GetFunctionBody()->GetIsAsmjsMode())
  3002. {
  3003. FunctionBody *const functionBody = GetFunctionBody();
  3004. AsmJsFunctionInfo* asmInfo = functionBody->GetAsmJsFunctionInfo();
  3005. if (asmInfo)
  3006. {
  3007. AlignMemoryForAsmJs();
  3008. Var returnVar = ProcessAsmJs();
  3009. #if ENABLE_DEBUG_CONFIG_OPTIONS
  3010. if (PHASE_TRACE(AsmjsFunctionEntryPhase, functionBody))
  3011. {
  3012. --AsmJsCallDepth;
  3013. if (AsmJsCallDepth)
  3014. {
  3015. Output::Print(_u("%*c}"), AsmJsCallDepth, ' ');
  3016. }
  3017. else
  3018. {
  3019. Output::Print(_u("}"));
  3020. }
  3021. switch (asmInfo->GetReturnType().which())
  3022. {
  3023. case AsmJsRetType::Void:
  3024. break;
  3025. case AsmJsRetType::Signed:
  3026. Output::Print(_u(" = %d"), m_localIntSlots[0]);
  3027. break;
  3028. case AsmJsRetType::Int64:
  3029. Output::Print(_u(" = %lld"), m_localInt64Slots[0]);
  3030. break;
  3031. case AsmJsRetType::Float:
  3032. Output::Print(_u(" = %.4f"), m_localFloatSlots[0]);
  3033. break;
  3034. case AsmJsRetType::Double:
  3035. Output::Print(_u(" = %.4f"), m_localDoubleSlots[0]);
  3036. break;
  3037. default:
  3038. break;
  3039. }
  3040. Output::Print(_u(";\n"));
  3041. Output::Flush();
  3042. }
  3043. #endif
  3044. return returnVar;
  3045. }
  3046. else
  3047. {
  3048. Assert(functionBody->GetAsmJsModuleInfo());
  3049. return ProcessAsmJsModule();
  3050. }
  3051. }
  3052. #endif
  3053. #if ENABLE_PROFILE_INFO
  3054. switchProfileMode = false;
  3055. switchProfileModeOnLoopEndNumber = 0u - 1;
  3056. #endif
  3057. #if ENABLE_PROFILE_INFO
  3058. FunctionBody *const functionBody = GetFunctionBody();
  3059. const ExecutionMode interpreterExecutionMode =
  3060. functionBody->GetInterpreterExecutionMode(TestFlags(InterpreterStackFrameFlags_FromBailOut));
  3061. if (interpreterExecutionMode == ExecutionMode::ProfilingInterpreter)
  3062. {
  3063. #if ENABLE_TTD
  3064. AssertMsg(!SHOULD_DO_TTD_STACK_STMT_OP(this->scriptContext), "We should have pinned into Interpreter mode in this case!!!");
  3065. #endif
  3066. isAutoProfiling = false;
  3067. return ProcessProfiled();
  3068. }
  3069. Assert(
  3070. interpreterExecutionMode == ExecutionMode::Interpreter ||
  3071. interpreterExecutionMode == ExecutionMode::AutoProfilingInterpreter);
  3072. isAutoProfiling = interpreterExecutionMode == ExecutionMode::AutoProfilingInterpreter;
  3073. Var result;
  3074. while (true)
  3075. {
  3076. Assert(!switchProfileMode);
  3077. #if ENABLE_TTD_DIAGNOSTICS_TRACING
  3078. if (this->scriptContext->ShouldPerformRecordOrReplayAction())
  3079. {
  3080. result = ProcessUnprofiled_PreviousStmtTracking();
  3081. }
  3082. else
  3083. {
  3084. result = ProcessUnprofiled();
  3085. }
  3086. #else
  3087. result = ProcessUnprofiled();
  3088. #endif
  3089. Assert(!(switchProfileMode && result));
  3090. if (switchProfileMode)
  3091. {
  3092. switchProfileMode = false;
  3093. }
  3094. else
  3095. {
  3096. break;
  3097. }
  3098. Assert(isAutoProfiling);
  3099. #if DBG_DUMP
  3100. if (PHASE_TRACE(InterpreterAutoProfilePhase, functionBody))
  3101. {
  3102. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  3103. Output::Print(_u("InterpreterAutoProfile - Func %s - Started profiling\n"), functionBody->GetDebugNumberSet(debugStringBuffer));
  3104. Output::Flush();
  3105. }
  3106. #endif
  3107. Assert(!switchProfileMode);
  3108. result = ProcessProfiled();
  3109. Assert(!(switchProfileMode && result));
  3110. if (switchProfileMode)
  3111. {
  3112. switchProfileMode = false;
  3113. }
  3114. else
  3115. {
  3116. break;
  3117. }
  3118. Assert(isAutoProfiling);
  3119. #if DBG_DUMP
  3120. if (PHASE_TRACE(InterpreterAutoProfilePhase, functionBody))
  3121. {
  3122. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  3123. Output::Print(_u("InterpreterAutoProfile - Func %s - Stopped profiling\n"), functionBody->GetDebugNumberSet(debugStringBuffer));
  3124. Output::Flush();
  3125. }
  3126. #endif
  3127. }
  3128. return result;
  3129. #else
  3130. #if ENABLE_TTD_DIAGNOSTICS_TRACING
  3131. if (this->scriptContext->ShouldPerformRecordOrReplayAction())
  3132. {
  3133. return ProcessUnprofiled_PreviousStmtTracking();
  3134. }
  3135. else
  3136. {
  3137. return ProcessUnprofiled();
  3138. }
  3139. #else
  3140. return ProcessUnprofiled();
  3141. #endif
  3142. #endif
  3143. }
  3144. template <class T>
  3145. void InterpreterStackFrame::OP_GetMethodProperty(unaligned T *playout)
  3146. {
  3147. Var varInstance = GetReg(playout->Instance);
  3148. OP_GetMethodProperty(varInstance, playout);
  3149. }
  3150. template <class T>
  3151. void InterpreterStackFrame::OP_GetLocalMethodProperty(unaligned T *playout)
  3152. {
  3153. OP_GetMethodProperty(this->localClosure, playout);
  3154. }
  3155. template <class T>
  3156. void InterpreterStackFrame::OP_GetMethodProperty(Var varInstance, unaligned T *playout)
  3157. {
  3158. #if ENABLE_COPYONACCESS_ARRAY
  3159. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(varInstance);
  3160. #endif
  3161. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  3162. RecyclableObject* obj = JavascriptOperators::TryFromVar<RecyclableObject>(varInstance);
  3163. if (obj)
  3164. {
  3165. ScriptFunction *fn = JavascriptOperators::TryFromVar<ScriptFunction>(obj);
  3166. if ((propertyId == PropertyIds::apply || propertyId == PropertyIds::call) && fn)
  3167. {
  3168. // If the property being loaded is "apply"/"call", make an optimistic assumption that apply/call is not overridden and
  3169. // undefer the function right here if it was defer parsed before. This is required so that the load of "apply"/"call"
  3170. // happens from the same "type". Otherwise, we will have a polymorphic cache for load of "apply"/"call".
  3171. if (fn->GetType()->GetEntryPoint() == JavascriptFunction::DeferredParsingThunk)
  3172. {
  3173. JavascriptFunction::DeferredParse(&fn);
  3174. }
  3175. }
  3176. }
  3177. InlineCache *inlineCache = this->GetInlineCache(playout->inlineCacheIndex);
  3178. PropertyValueInfo info;
  3179. PropertyValueInfo::SetCacheInfo(&info, GetFunctionBody(), inlineCache, playout->inlineCacheIndex, true);
  3180. Var aValue;
  3181. if (obj &&
  3182. CacheOperators::TryGetProperty<true, true, false, false, false, false, true, false, false, false>(
  3183. obj, false, obj, propertyId, &aValue, GetScriptContext(), nullptr, &info))
  3184. {
  3185. SetReg(playout->Value, aValue);
  3186. return;
  3187. }
  3188. OP_GetMethodProperty_NoFastPath(varInstance, playout);
  3189. }
  3190. template <class T>
  3191. _NOINLINE void InterpreterStackFrame::OP_GetMethodProperty_NoFastPath(Var instance, unaligned T *playout)
  3192. {
  3193. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  3194. Var value = JavascriptOperators::PatchGetMethod<false>(
  3195. GetFunctionBody(),
  3196. GetInlineCache(playout->inlineCacheIndex),
  3197. playout->inlineCacheIndex,
  3198. instance,
  3199. propertyId
  3200. );
  3201. #ifdef TELEMETRY_INTERPRETER
  3202. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  3203. {
  3204. // `successful` will be true as PatchGetMethod throws an exception if not found.
  3205. this->scriptContext->GetTelemetry().GetOpcodeTelemetry().GetMethodProperty(instance, propertyId, value, true);
  3206. }
  3207. #endif
  3208. SetReg(playout->Value, value);
  3209. }
  3210. template <class T>
  3211. void InterpreterStackFrame::OP_GetRootMethodProperty(unaligned T *playout)
  3212. {
  3213. Assert(playout->inlineCacheIndex >= this->m_functionBody->GetRootObjectLoadInlineCacheStart());
  3214. Js::Var instance = this->GetRootObject();
  3215. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  3216. InlineCache *inlineCache = this->GetInlineCache(playout->inlineCacheIndex);
  3217. DynamicObject *obj = UnsafeVarTo<DynamicObject>(instance);
  3218. PropertyValueInfo info;
  3219. PropertyValueInfo::SetCacheInfo(&info, GetFunctionBody(), inlineCache, playout->inlineCacheIndex, true);
  3220. Var aValue;
  3221. if (CacheOperators::TryGetProperty<true, true, false, false, false, false, true, false, false, false>(
  3222. obj, true, obj, propertyId, &aValue, GetScriptContext(), nullptr, &info))
  3223. {
  3224. SetReg(playout->Value, aValue);
  3225. return;
  3226. }
  3227. OP_GetRootMethodProperty_NoFastPath(playout);
  3228. }
  3229. template <class T>
  3230. _NOINLINE void InterpreterStackFrame::OP_GetRootMethodProperty_NoFastPath(unaligned T *playout)
  3231. {
  3232. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  3233. Var rootInstance = this->GetRootObject();
  3234. Var value = JavascriptOperators::PatchGetRootMethod<false>(
  3235. GetFunctionBody(),
  3236. GetInlineCache(playout->inlineCacheIndex),
  3237. playout->inlineCacheIndex,
  3238. UnsafeVarTo<DynamicObject>(rootInstance),
  3239. propertyId
  3240. );
  3241. #ifdef TELEMETRY_INTERPRETER
  3242. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  3243. {
  3244. // `successful` will be true as PatchGetMethod throws an exception if not found.
  3245. this->scriptContext->GetTelemetry().GetOpcodeTelemetry().GetMethodProperty(rootInstance, propertyId, value, true);
  3246. }
  3247. #endif
  3248. SetReg(playout->Value, value);
  3249. }
  3250. template <class T>
  3251. void InterpreterStackFrame::OP_GetMethodPropertyScoped(unaligned T *playout)
  3252. {
  3253. ThreadContext* threadContext = this->GetScriptContext()->GetThreadContext();
  3254. ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  3255. threadContext->ClearImplicitCallFlags();
  3256. Var varInstance = GetReg(playout->Instance);
  3257. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  3258. RecyclableObject* obj = NULL;
  3259. if (VarIs<RecyclableObject>(varInstance))
  3260. {
  3261. obj = VarTo<RecyclableObject>(varInstance);
  3262. }
  3263. InlineCache *inlineCache = this->GetInlineCache(playout->inlineCacheIndex);
  3264. PropertyValueInfo info;
  3265. PropertyValueInfo::SetCacheInfo(&info, GetFunctionBody(), inlineCache, playout->inlineCacheIndex, true);
  3266. Var aValue;
  3267. if (obj &&
  3268. CacheOperators::TryGetProperty<true, true, false, false, false, false, true, false, false, false>(
  3269. obj, false, obj, propertyId, &aValue, GetScriptContext(), nullptr, &info))
  3270. {
  3271. threadContext->CheckAndResetImplicitCallAccessorFlag();
  3272. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  3273. SetReg(playout->Value, aValue);
  3274. return;
  3275. }
  3276. OP_GetMethodPropertyScoped_NoFastPath(playout);
  3277. threadContext->CheckAndResetImplicitCallAccessorFlag();
  3278. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  3279. }
  3280. template <class T>
  3281. _NOINLINE void InterpreterStackFrame::OP_GetMethodPropertyScoped_NoFastPath(unaligned T *playout)
  3282. {
  3283. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  3284. Js::Var instance = GetReg(playout->Instance);
  3285. Js::Var value = JavascriptOperators::PatchScopedGetMethod<false>(
  3286. GetFunctionBody(),
  3287. GetInlineCache(playout->inlineCacheIndex),
  3288. playout->inlineCacheIndex,
  3289. instance,
  3290. propertyId
  3291. );
  3292. SetReg(playout->Value, value);
  3293. #ifdef TELEMETRY_INTERPRETER
  3294. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  3295. {
  3296. // `successful` will be true as PatchGetMethod throws an exception if not found.
  3297. this->scriptContext->GetTelemetry().GetOpcodeTelemetry().GetMethodProperty(instance, propertyId, value, true);
  3298. }
  3299. #endif
  3300. }
  3301. template <class T>
  3302. void InterpreterStackFrame::OP_ProfiledGetMethodProperty(unaligned T *playout)
  3303. {
  3304. ProfiledGetProperty<T, false, true, false>(playout, GetReg(playout->Instance));
  3305. }
  3306. template <class T>
  3307. void InterpreterStackFrame::OP_ProfiledGetLocalMethodProperty(unaligned T *playout)
  3308. {
  3309. ProfiledGetProperty<T, false, true, false>(playout, this->localClosure);
  3310. }
  3311. template <class T>
  3312. void InterpreterStackFrame::OP_ProfiledGetRootMethodProperty(unaligned T *playout)
  3313. {
  3314. ProfiledGetProperty<T, true, true, false>(playout, GetRootObject());
  3315. }
  3316. RecyclableObject *
  3317. InterpreterStackFrame::OP_CallGetFunc(Var target)
  3318. {
  3319. return JavascriptOperators::GetCallableObjectOrThrow(target, GetScriptContext());
  3320. }
  3321. void InterpreterStackFrame::OP_AsmStartCall(const unaligned OpLayoutStartCall * playout)
  3322. {
  3323. OP_StartCall(playout->ArgCount / sizeof(Var));
  3324. m_outParams[0] = scriptContext->GetLibrary()->GetUndefined();
  3325. }
  3326. void InterpreterStackFrame::OP_StartCall(const unaligned OpLayoutStartCall * playout)
  3327. {
  3328. OP_StartCall(playout->ArgCount);
  3329. }
  3330. void InterpreterStackFrame::OP_StartCall(uint outParamCount)
  3331. {
  3332. // Save the outParams for the current callsite on the outparam stack
  3333. PushOut(m_outParams);
  3334. // Update outParams for the indicated callsite
  3335. m_outParams = m_outSp;
  3336. m_outSp += outParamCount;
  3337. AssertMsg(m_localSlots + this->m_functionBody->GetLocalsCount() < m_outSp &&
  3338. m_outSp <= (m_localSlots + this->m_functionBody->GetLocalsCount() + this->m_functionBody->GetOutParamMaxDepth()),
  3339. "out args Stack pointer not in range after Push");
  3340. }
  3341. #ifdef ASMJS_PLAT
  3342. void InterpreterStackFrame::OP_ProfiledCallAsmInternal(RegSlot funcReg, RegSlot returnReg, ProfileId profileId)
  3343. {
  3344. Var target = GetRegRawPtr(funcReg);
  3345. ScriptFunction* function = (ScriptFunction*)OP_CallGetFunc(target);
  3346. Js::FunctionBody * body = function->GetFunctionBody();
  3347. DynamicProfileInfo * dynamicProfileInfo = GetFunctionBody()->GetDynamicProfileInfo();
  3348. dynamicProfileInfo->RecordAsmJsCallSiteInfo(GetFunctionBody(), profileId, body);
  3349. return OP_CallAsmInternalCommon(function, returnReg);
  3350. }
  3351. void InterpreterStackFrame::OP_CallAsmInternal(RegSlot funcReg, RegSlot returnReg)
  3352. {
  3353. Var target = GetRegRawPtr(funcReg);
  3354. ScriptFunction* function = (ScriptFunction*)OP_CallGetFunc(target);
  3355. return OP_CallAsmInternalCommon(function, returnReg);
  3356. }
  3357. void InterpreterStackFrame::OP_CallAsmInternalCommon(ScriptFunction* function, RegSlot returnReg)
  3358. {
  3359. AsmJsScriptFunction* scriptFunc = VarTo<AsmJsScriptFunction>(function);
  3360. AsmJsFunctionInfo* asmInfo = scriptFunc->GetFunctionBody()->GetAsmJsFunctionInfo();
  3361. uint alignedArgsSize = ::Math::Align<uint32>(asmInfo->GetArgByteSize(), 16);
  3362. #if _M_X64 && _WIN32
  3363. // convention is to always allocate spill space for rcx,rdx,r8,r9
  3364. if (alignedArgsSize < 0x20) alignedArgsSize = 0x20;
  3365. uint* argSizes = asmInfo->GetArgsSizesArray();
  3366. Assert(asmInfo->GetArgSizeArrayLength() >= 2);
  3367. byte* curOutParams = (byte*)m_outParams + sizeof(Var);
  3368. Assert(curOutParams + argSizes[0] + argSizes[1] + 16 <= (byte*)this->m_outParamsEnd);
  3369. // Prepare in advance the possible arguments that will need to be put in register
  3370. byte _declspec(align(16)) reg[3 * 16];
  3371. CompileAssert((FunctionBody::MinAsmJsOutParams() * sizeof(Var)) == (sizeof(Var) * 2 + sizeof(reg)));
  3372. js_memcpy_s(reg, 16, curOutParams, 16);
  3373. js_memcpy_s(reg + 16, 16, curOutParams + argSizes[0], 16);
  3374. js_memcpy_s(reg + 32, 16, curOutParams + argSizes[0] + argSizes[1], 16);
  3375. #else
  3376. byte* reg = nullptr;
  3377. #endif
  3378. ScriptContext * scriptContext = function->GetScriptContext();
  3379. Js::FunctionEntryPointInfo* entrypointInfo = (Js::FunctionEntryPointInfo*)function->GetEntryPointInfo();
  3380. PROBE_STACK_CALL(scriptContext, function, alignedArgsSize + Js::Constants::MinStackDefault);
  3381. // Calling the jsMethod might change the entrypoint, adding the variable here
  3382. // will save the method on the stack helping debug what really got called
  3383. JavascriptMethod jsMethod = entrypointInfo->jsMethod;
  3384. switch (asmInfo->GetReturnType().which())
  3385. {
  3386. case AsmJsRetType::Void:
  3387. JavascriptFunction::CallAsmJsFunction<int>(function, jsMethod, m_outParams, alignedArgsSize, reg);
  3388. break;
  3389. case AsmJsRetType::Signed:
  3390. m_localIntSlots[returnReg] = JavascriptFunction::CallAsmJsFunction<int>(function, jsMethod, m_outParams, alignedArgsSize, reg);
  3391. break;
  3392. case AsmJsRetType::Int64:
  3393. m_localInt64Slots[returnReg] = JavascriptFunction::CallAsmJsFunction<int64>(function, jsMethod, m_outParams, alignedArgsSize, reg);
  3394. break;
  3395. case AsmJsRetType::Double:
  3396. m_localDoubleSlots[returnReg] = JavascriptFunction::CallAsmJsFunction<double>(function, jsMethod, m_outParams, alignedArgsSize, reg);
  3397. break;
  3398. case AsmJsRetType::Float:
  3399. m_localFloatSlots[returnReg] = JavascriptFunction::CallAsmJsFunction<float>(function, jsMethod, m_outParams, alignedArgsSize, reg);
  3400. break;
  3401. #ifdef ENABLE_WASM_SIMD
  3402. case AsmJsRetType::Float32x4:
  3403. case AsmJsRetType::Int32x4:
  3404. case AsmJsRetType::Bool32x4:
  3405. case AsmJsRetType::Bool16x8:
  3406. case AsmJsRetType::Bool8x16:
  3407. case AsmJsRetType::Float64x2:
  3408. case AsmJsRetType::Int16x8:
  3409. case AsmJsRetType::Int8x16:
  3410. case AsmJsRetType::Uint32x4:
  3411. case AsmJsRetType::Uint16x8:
  3412. case AsmJsRetType::Uint8x16:
  3413. #if _WIN32 //WASM.SIMD ToDo: Enable thunk for Xplat
  3414. #if _M_X64
  3415. #if !defined(__clang__)
  3416. X86SIMDValue simdVal;
  3417. simdVal.m128_value = JavascriptFunction::CallAsmJsFunction<__m128>(function, jsMethod, m_outParams, alignedArgsSize, reg);
  3418. m_localSimdSlots[returnReg] = X86SIMDValue::ToSIMDValue(simdVal);
  3419. #else
  3420. AssertOrFailFastMsg(false, "This particular path causes linking issues in clang on windows; potential difference in name mangling?");
  3421. #endif // !defined(__clang__)
  3422. #else
  3423. m_localSimdSlots[returnReg] = JavascriptFunction::CallAsmJsFunction<AsmJsSIMDValue>(function, jsMethod, m_outParams, alignedArgsSize, reg);
  3424. #endif // _M_X64
  3425. #endif // _WIN32
  3426. break;
  3427. #endif // ENABLE_WASM_SIMD
  3428. default:
  3429. Assume(UNREACHED);
  3430. }
  3431. const ArgSlot nVarToPop = (asmInfo->GetArgByteSize() / sizeof(Var)) + 1;
  3432. PopOut(nVarToPop);
  3433. Assert(function);
  3434. }
  3435. #endif
  3436. template <class T>
  3437. void InterpreterStackFrame::OP_AsmCall(const unaligned T* playout)
  3438. {
  3439. Var target = GetRegRawPtr(playout->Function);
  3440. RecyclableObject * function = OP_CallGetFunc(target);
  3441. #if DBG
  3442. if (this->IsInDebugMode())
  3443. {
  3444. JavascriptFunction::CheckValidDebugThunk(scriptContext, function);
  3445. }
  3446. #endif
  3447. BEGIN_SAFE_REENTRANT_REGION(this->scriptContext->GetThreadContext())
  3448. {
  3449. if (playout->Return == Js::Constants::NoRegister)
  3450. {
  3451. Arguments args(CallInfo(CallFlags_NotUsed, playout->ArgCount), m_outParams);
  3452. JavascriptFunction::CallFunction<true>(function, function->GetEntryPoint(), args);
  3453. }
  3454. else
  3455. {
  3456. Arguments args(CallInfo(CallFlags_Value, playout->ArgCount), m_outParams);
  3457. Var retVal = JavascriptFunction::CallFunction<true>(function, function->GetEntryPoint(), args);
  3458. SetRegRawPtr(playout->Return, retVal);
  3459. }
  3460. }
  3461. END_SAFE_REENTRANT_REGION
  3462. PopOut(playout->ArgCount);
  3463. }
  3464. template <class T>
  3465. void InterpreterStackFrame::OP_EnsureHeapAttached(const unaligned T* playout)
  3466. {
  3467. AsmJsModuleInfo::EnsureHeapAttached(this->function);
  3468. }
  3469. template <class T>
  3470. void InterpreterStackFrame::OP_CallCommon(const unaligned T * playout, RecyclableObject * function, unsigned flags, const Js::AuxArray<uint32> *spreadIndices)
  3471. {
  3472. // Always save and restore implicit call flags when calling out
  3473. // REVIEW: Can we avoid it if we don't collect dynamic profile info?
  3474. ThreadContext * threadContext = scriptContext->GetThreadContext();
  3475. Js::ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  3476. #ifdef ENABLE_SCRIPT_DEBUGGING
  3477. #if DBG
  3478. if (this->IsInDebugMode())
  3479. {
  3480. JavascriptFunction::CheckValidDebugThunk(scriptContext, function);
  3481. }
  3482. #endif
  3483. #endif
  3484. ArgSlot argCount = playout->ArgCount;
  3485. BEGIN_SAFE_REENTRANT_REGION(threadContext)
  3486. {
  3487. if (playout->Return == Js::Constants::NoRegister)
  3488. {
  3489. flags |= CallFlags_NotUsed;
  3490. Arguments args(CallInfo((CallFlags)flags, argCount), m_outParams);
  3491. AssertMsg(static_cast<unsigned>(args.Info.Flags) == flags, "Flags don't fit into the CallInfo field?");
  3492. argCount = args.GetArgCountWithExtraArgs();
  3493. if (spreadIndices != nullptr)
  3494. {
  3495. JavascriptFunction::CallSpreadFunction(function, args, spreadIndices);
  3496. }
  3497. else
  3498. {
  3499. JavascriptFunction::CallFunction<true>(function, function->GetEntryPoint(), args);
  3500. }
  3501. }
  3502. else
  3503. {
  3504. flags |= CallFlags_Value;
  3505. Arguments args(CallInfo((CallFlags)flags, argCount), m_outParams);
  3506. AssertMsg(static_cast<unsigned>(args.Info.Flags) == flags, "Flags don't fit into the CallInfo field?");
  3507. argCount = args.GetArgCountWithExtraArgs();
  3508. if (spreadIndices != nullptr)
  3509. {
  3510. SetReg((RegSlot)playout->Return, JavascriptFunction::CallSpreadFunction(function, args, spreadIndices));
  3511. }
  3512. else
  3513. {
  3514. SetReg((RegSlot)playout->Return, JavascriptFunction::CallFunction<true>(function, function->GetEntryPoint(), args));
  3515. }
  3516. }
  3517. }
  3518. END_SAFE_REENTRANT_REGION
  3519. threadContext->SetImplicitCallFlags(savedImplicitCallFlags);
  3520. PopOut(argCount);
  3521. }
  3522. template <class T>
  3523. void InterpreterStackFrame::OP_CallCommonI(const unaligned T * playout, RecyclableObject * function, unsigned flags)
  3524. {
  3525. OP_CallCommon(playout, function, flags); // CallCommon doesn't do anything with Member
  3526. }
  3527. #if ENABLE_PROFILE_INFO
  3528. template <class T>
  3529. void InterpreterStackFrame::OP_ProfileCallCommon(const unaligned T * playout, RecyclableObject * function, unsigned flags, ProfileId profileId, InlineCacheIndex inlineCacheIndex, const Js::AuxArray<uint32> *spreadIndices)
  3530. {
  3531. JavascriptFunction * targetFunction = VarIs<JavascriptFunction>(m_outParams[0]) ? UnsafeVarTo<JavascriptFunction>(m_outParams[0]) : nullptr;
  3532. FunctionBody* functionBody = this->m_functionBody;
  3533. FunctionInfo* functionInfo = function->GetTypeId() == TypeIds_Function ?
  3534. VarTo<JavascriptFunction>(function)->GetFunctionInfo() : nullptr;
  3535. DynamicProfileInfo* dynamicProfileInfo = functionBody->GetDynamicProfileInfo();
  3536. bool isConstructorCall = (CallFlags_New & flags) == CallFlags_New;
  3537. OP_CallCommon<T>(playout, function, flags, spreadIndices);
  3538. // Profile call site
  3539. dynamicProfileInfo->RecordCallSiteInfo(functionBody, profileId, functionInfo, functionInfo ? static_cast<JavascriptFunction*>(function) : nullptr, playout->ArgCount, isConstructorCall, inlineCacheIndex);
  3540. if (functionInfo && !functionInfo->HasBody())
  3541. {
  3542. if ((functionInfo->IsBuiltInApplyFunction() || functionInfo->IsBuiltInCallFunction()) && targetFunction)
  3543. {
  3544. Js::ProfileId* callSiteToCallApplyCallSiteMap = this->m_functionBody->GetCallSiteToCallApplyCallSiteArray();
  3545. if (callSiteToCallApplyCallSiteMap)
  3546. {
  3547. Js::ProfileId callApplyCallSiteId = callSiteToCallApplyCallSiteMap[profileId];
  3548. if (callApplyCallSiteId < functionBody->GetProfiledCallApplyCallSiteCount())
  3549. {
  3550. dynamicProfileInfo->RecordCallApplyTargetInfo(functionBody, callApplyCallSiteId, targetFunction->GetFunctionInfo(), targetFunction);
  3551. }
  3552. else
  3553. {
  3554. Assert(callApplyCallSiteId == Js::Constants::NoProfileId);
  3555. }
  3556. }
  3557. }
  3558. }
  3559. if (playout->Return != Js::Constants::NoRegister)
  3560. {
  3561. dynamicProfileInfo->RecordReturnTypeOnCallSiteInfo(functionBody, profileId, GetReg((RegSlot)playout->Return));
  3562. }
  3563. }
  3564. template <class T>
  3565. void InterpreterStackFrame::OP_ProfileReturnTypeCallCommon(const unaligned T * playout, RecyclableObject * function, unsigned flags, ProfileId profileId, const Js::AuxArray<uint32> *spreadIndices)
  3566. {
  3567. OP_CallCommon<T>(playout, function, flags, spreadIndices);
  3568. FunctionBody* functionBody = this->m_functionBody;
  3569. DynamicProfileInfo * dynamicProfileInfo = functionBody->GetDynamicProfileInfo();
  3570. if (playout->Return != Js::Constants::NoRegister)
  3571. {
  3572. dynamicProfileInfo->RecordReturnType(functionBody, profileId, GetReg((RegSlot)playout->Return));
  3573. }
  3574. }
  3575. #endif
  3576. template <class T>
  3577. void InterpreterStackFrame::OP_GetRootProperty(unaligned T* playout)
  3578. {
  3579. // Same fast path as in the backend.
  3580. Assert(playout->inlineCacheIndex >= this->m_functionBody->GetRootObjectLoadInlineCacheStart());
  3581. Js::Var instance = this->GetRootObject();
  3582. InlineCache *inlineCache = this->GetInlineCache(playout->inlineCacheIndex);
  3583. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  3584. DynamicObject * obj = UnsafeVarTo<DynamicObject>(instance);
  3585. PropertyValueInfo info;
  3586. PropertyValueInfo::SetCacheInfo(&info, GetFunctionBody(), inlineCache, playout->inlineCacheIndex, true);
  3587. Var value;
  3588. if (CacheOperators::TryGetProperty<true, false, false, false, false, false, true, false, false, false>(
  3589. obj, true, obj, propertyId, &value, GetScriptContext(), nullptr, &info))
  3590. {
  3591. SetReg(playout->Value, value);
  3592. return;
  3593. }
  3594. OP_GetRootProperty_NoFastPath(playout);
  3595. }
  3596. template <class T>
  3597. void InterpreterStackFrame::OP_GetRootPropertyForTypeOf(unaligned T* playout)
  3598. {
  3599. Var rootInstance = GetRootObject();
  3600. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  3601. Var value = JavascriptOperators::PatchGetRootValueForTypeOf<false>(
  3602. GetFunctionBody(),
  3603. GetInlineCache(playout->inlineCacheIndex),
  3604. playout->inlineCacheIndex,
  3605. UnsafeVarTo<DynamicObject>(rootInstance),
  3606. propertyId
  3607. );
  3608. SetReg(playout->Value, value);
  3609. #ifdef TELEMETRY_INTERPRETER
  3610. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  3611. {
  3612. // `successful` will be true as PatchGetRootValue throws an exception if not found.
  3613. this->scriptContext->GetTelemetry().GetOpcodeTelemetry().GetProperty(rootInstance, propertyId, value, /*successful:*/true);
  3614. }
  3615. #endif
  3616. }
  3617. template <class T>
  3618. _NOINLINE void InterpreterStackFrame::OP_GetRootProperty_NoFastPath(unaligned T* playout)
  3619. {
  3620. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  3621. Var rootInstance = this->GetRootObject();
  3622. Var value = JavascriptOperators::PatchGetRootValue<false>(
  3623. GetFunctionBody(),
  3624. GetInlineCache(playout->inlineCacheIndex),
  3625. playout->inlineCacheIndex,
  3626. UnsafeVarTo<DynamicObject>(rootInstance),
  3627. propertyId
  3628. );
  3629. SetReg(playout->Value, value);
  3630. #ifdef TELEMETRY_INTERPRETER
  3631. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  3632. {
  3633. // `successful` will be true as PatchGetRootValue throws an exception if not found.
  3634. this->scriptContext->GetTelemetry().GetOpcodeTelemetry().GetProperty(rootInstance, propertyId, value, /*successful:*/true);
  3635. }
  3636. #endif
  3637. }
  3638. #if ENABLE_PROFILE_INFO
  3639. template <class T>
  3640. void InterpreterStackFrame::UpdateFldInfoFlagsForGetSetInlineCandidate(unaligned T* playout, FldInfoFlags& fldInfoFlags, CacheType cacheType,
  3641. DynamicProfileInfo * dynamicProfileInfo, uint inlineCacheIndex, RecyclableObject * obj)
  3642. {
  3643. RecyclableObject *callee = nullptr;
  3644. //TODO: Setter case once we stop sharing inline caches for these callsites.
  3645. if ((cacheType & (CacheType_Getter | CacheType_Setter)) && GetInlineCache(inlineCacheIndex)->GetGetterSetter(obj->GetType(), &callee))
  3646. {
  3647. const auto functionBody = this->m_functionBody;
  3648. bool canInline = dynamicProfileInfo->RecordLdFldCallSiteInfo(functionBody, callee, false /*callApplyTarget*/);
  3649. if (canInline)
  3650. {
  3651. //updates this fldInfoFlags passed by reference.
  3652. fldInfoFlags = DynamicProfileInfo::MergeFldInfoFlags(fldInfoFlags, FldInfo_InlineCandidate);
  3653. }
  3654. }
  3655. }
  3656. template <class T>
  3657. void InterpreterStackFrame::UpdateFldInfoFlagsForCallApplyInlineCandidate(unaligned T* playout, FldInfoFlags& fldInfoFlags, CacheType cacheType,
  3658. DynamicProfileInfo * dynamicProfileInfo, uint inlineCacheIndex, RecyclableObject * obj)
  3659. {
  3660. RecyclableObject *callee = nullptr;
  3661. if (!(fldInfoFlags & FldInfo_Polymorphic) && GetInlineCache(inlineCacheIndex)->GetCallApplyTarget(obj, &callee))
  3662. {
  3663. const auto functionBody = this->m_functionBody;
  3664. bool canInline = dynamicProfileInfo->RecordLdFldCallSiteInfo(functionBody, callee, true /*callApplyTarget*/);
  3665. if (canInline)
  3666. {
  3667. //updates this fldInfoFlags passed by reference.
  3668. fldInfoFlags = DynamicProfileInfo::MergeFldInfoFlags(fldInfoFlags, FldInfo_InlineCandidate);
  3669. }
  3670. }
  3671. }
  3672. template <class T, bool Root, bool Method, bool CallApplyTarget>
  3673. void InterpreterStackFrame::ProfiledGetProperty(unaligned T* playout, const Var instance)
  3674. {
  3675. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  3676. Var value = ProfilingHelpers::ProfiledLdFld<Root, Method, CallApplyTarget>(
  3677. instance,
  3678. propertyId,
  3679. GetInlineCache(playout->inlineCacheIndex),
  3680. playout->inlineCacheIndex,
  3681. GetFunctionBody(),
  3682. instance);
  3683. SetReg(playout->Value, value);
  3684. #ifdef TELEMETRY_INTERPRETER
  3685. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  3686. {
  3687. // `successful` will be true as PatchGetRootValue throws an exception if not found.
  3688. this->scriptContext->GetTelemetry().GetOpcodeTelemetry().GetProperty(instance, propertyId, value, /*successful:*/true);
  3689. }
  3690. #endif
  3691. }
  3692. template <class T>
  3693. void InterpreterStackFrame::OP_ProfiledGetRootProperty(unaligned T* playout)
  3694. {
  3695. ProfiledGetProperty<T, true, false, false>(playout, GetRootObject());
  3696. }
  3697. template <class T>
  3698. void InterpreterStackFrame::OP_ProfiledGetRootPropertyForTypeOf(unaligned T* playout)
  3699. {
  3700. Var rootInstance = GetRootObject();
  3701. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  3702. Var value = ProfilingHelpers::ProfiledLdFldForTypeOf<true, false, false>(
  3703. rootInstance,
  3704. propertyId,
  3705. GetInlineCache(playout->inlineCacheIndex),
  3706. playout->inlineCacheIndex,
  3707. GetFunctionBody());
  3708. SetReg(playout->Value, value);
  3709. #ifdef TELEMETRY_INTERPRETER
  3710. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  3711. {
  3712. // `successful` will be true as PatchGetRootValue throws an exception if not found.
  3713. this->scriptContext->GetTelemetry().GetOpcodeTelemetry().GetProperty(rootInstance, propertyId, value, /*successful:*/true);
  3714. }
  3715. #endif
  3716. }
  3717. #endif
  3718. template <class T>
  3719. void InterpreterStackFrame::OP_GetPropertyForTypeOf(unaligned T* playout)
  3720. {
  3721. Var instance = GetReg(playout->Instance);
  3722. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  3723. Var value = JavascriptOperators::PatchGetValueForTypeOf<false>(
  3724. GetFunctionBody(),
  3725. GetInlineCache(playout->inlineCacheIndex),
  3726. playout->inlineCacheIndex,
  3727. instance,
  3728. propertyId
  3729. );
  3730. SetReg(playout->Value, value);
  3731. #ifdef TELEMETRY_INTERPRETER
  3732. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  3733. {
  3734. // `successful` will be true as PatchGetRootValue throws an exception if not found.
  3735. this->scriptContext->GetTelemetry().GetOpcodeTelemetry().GetProperty(instance, propertyId, value, /*successful:*/true);
  3736. }
  3737. #endif
  3738. }
  3739. template <class T>
  3740. void InterpreterStackFrame::OP_GetProperty(unaligned T* playout)
  3741. {
  3742. // Same fast path as in the backend.
  3743. Var instance = GetReg(playout->Instance);
  3744. OP_GetProperty(instance, playout);
  3745. }
  3746. template <class T>
  3747. void InterpreterStackFrame::OP_GetLocalProperty(unaligned T* playout)
  3748. {
  3749. // Same fast path as in the backend.
  3750. Var instance = this->localClosure;
  3751. OP_GetProperty(instance, playout);
  3752. }
  3753. template <class T>
  3754. void InterpreterStackFrame::OP_GetProperty(Var instance, unaligned T* playout)
  3755. {
  3756. InlineCache *inlineCache = GetInlineCache(playout->inlineCacheIndex);
  3757. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  3758. RecyclableObject* obj = JavascriptOperators::TryFromVar<RecyclableObject>(instance);
  3759. if (obj)
  3760. {
  3761. PropertyValueInfo info;
  3762. PropertyValueInfo::SetCacheInfo(&info, GetFunctionBody(), inlineCache, playout->inlineCacheIndex, true);
  3763. Var value;
  3764. if (CacheOperators::TryGetProperty<true, false, false, false, false, false, true, false, false, false>(
  3765. obj, false, obj, propertyId, &value, GetScriptContext(), nullptr, &info))
  3766. {
  3767. SetReg(playout->Value, value);
  3768. return;
  3769. }
  3770. }
  3771. OP_GetProperty_NoFastPath(instance, playout);
  3772. }
  3773. template <class T>
  3774. void InterpreterStackFrame::OP_GetSuperProperty(unaligned T* playout)
  3775. {
  3776. // Same fast path as in the backend.
  3777. Var instance = GetReg(playout->Instance);
  3778. Var thisInstance = GetReg(playout->Value2);
  3779. InlineCache *inlineCache = GetInlineCache(playout->PropertyIdIndex);
  3780. PropertyId propertyId = GetPropertyIdFromCacheId(playout->PropertyIdIndex);
  3781. if (VarIs<RecyclableObject>(instance) && VarIs<RecyclableObject>(thisInstance))
  3782. {
  3783. RecyclableObject* superObj = VarTo<RecyclableObject>(instance);
  3784. RecyclableObject* thisObj = VarTo<RecyclableObject>(thisInstance);
  3785. PropertyValueInfo info;
  3786. PropertyValueInfo::SetCacheInfo(&info, GetFunctionBody(), inlineCache, playout->PropertyIdIndex, true);
  3787. Var value;
  3788. if (CacheOperators::TryGetProperty<true, false, false, false, false, false, true, false, false, false>(
  3789. thisObj, false, superObj, propertyId, &value, GetScriptContext(), nullptr, &info))
  3790. {
  3791. SetReg(playout->Value, value);
  3792. return;
  3793. }
  3794. }
  3795. SetReg(
  3796. playout->Value,
  3797. JavascriptOperators::PatchGetValueWithThisPtr<false>(
  3798. GetFunctionBody(),
  3799. GetInlineCache(playout->PropertyIdIndex),
  3800. playout->PropertyIdIndex,
  3801. GetReg(playout->Instance),
  3802. GetPropertyIdFromCacheId(playout->PropertyIdIndex),
  3803. GetReg(playout->Value2)));
  3804. }
  3805. template <class T>
  3806. _NOINLINE void InterpreterStackFrame::OP_GetProperty_NoFastPath(Var instance, unaligned T* playout)
  3807. {
  3808. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  3809. Var value = JavascriptOperators::PatchGetValue<false>(
  3810. GetFunctionBody(),
  3811. GetInlineCache(playout->inlineCacheIndex),
  3812. playout->inlineCacheIndex,
  3813. instance,
  3814. propertyId
  3815. );
  3816. #ifdef TELEMETRY_INTERPRETER
  3817. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  3818. {
  3819. // `successful` will be true as PatchGetMethod throws an exception if not found.
  3820. this->scriptContext->GetTelemetry().GetOpcodeTelemetry().GetProperty(instance, propertyId, value, true);
  3821. }
  3822. #endif
  3823. SetReg(playout->Value, value);
  3824. }
  3825. #if ENABLE_PROFILE_INFO
  3826. template <class T>
  3827. void InterpreterStackFrame::OP_ProfiledGetProperty(unaligned T* playout)
  3828. {
  3829. ProfiledGetProperty<T, false, false, false>(playout, GetReg(playout->Instance));
  3830. }
  3831. template <class T>
  3832. void InterpreterStackFrame::OP_ProfiledGetLocalProperty(unaligned T* playout)
  3833. {
  3834. ProfiledGetProperty<T, false, false, false>(playout, this->localClosure);
  3835. }
  3836. template <class T>
  3837. void InterpreterStackFrame::OP_ProfiledGetSuperProperty(unaligned T* playout)
  3838. {
  3839. SetReg(
  3840. playout->Value,
  3841. ProfilingHelpers::ProfiledLdFld<false, false, false>(
  3842. GetReg(playout->Instance),
  3843. GetPropertyIdFromCacheId(playout->PropertyIdIndex),
  3844. GetInlineCache(playout->PropertyIdIndex),
  3845. playout->PropertyIdIndex,
  3846. GetFunctionBody(),
  3847. GetReg(playout->Value2)));
  3848. }
  3849. template <class T>
  3850. void InterpreterStackFrame::OP_ProfiledGetPropertyForTypeOf(unaligned T* playout)
  3851. {
  3852. Var instance = GetReg(playout->Instance);
  3853. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  3854. Var value = ProfilingHelpers::ProfiledLdFldForTypeOf<false, false, false>(
  3855. instance,
  3856. propertyId,
  3857. GetInlineCache(playout->inlineCacheIndex),
  3858. playout->inlineCacheIndex,
  3859. GetFunctionBody()
  3860. );
  3861. SetReg(playout->Value, value);
  3862. #ifdef TELEMETRY_INTERPRETER
  3863. if (TELEMETRY_PROPERTY_OPCODE_FILTER(propertyId))
  3864. {
  3865. // `successful` will be true as PatchGetMethod throws an exception if not found.
  3866. this->scriptContext->GetTelemetry().GetOpcodeTelemetry().GetProperty(instance, propertyId, value, true);
  3867. }
  3868. #endif
  3869. }
  3870. template <class T>
  3871. void InterpreterStackFrame::OP_ProfiledGetPropertyCallApplyTarget(unaligned T* playout)
  3872. {
  3873. ProfiledGetProperty<T, false, false, true>(playout, GetReg(playout->Instance));
  3874. }
  3875. #endif
  3876. template <typename T>
  3877. void InterpreterStackFrame::OP_GetPropertyScoped(const unaligned OpLayoutT_ElementP<T>* playout)
  3878. {
  3879. ThreadContext* threadContext = this->GetScriptContext()->GetThreadContext();
  3880. ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  3881. threadContext->ClearImplicitCallFlags();
  3882. // Get the property, using a scope stack rather than an individual instance.
  3883. // Use the cache
  3884. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  3885. FrameDisplay *pScope = this->GetEnvForEvalCode();
  3886. InlineCache *inlineCache = this->GetInlineCache(playout->inlineCacheIndex);
  3887. ScriptContext* scriptContext = GetScriptContext();
  3888. int length = pScope->GetLength();
  3889. if (1 == length)
  3890. {
  3891. RecyclableObject *obj = VarTo<RecyclableObject>(pScope->GetItem(0));
  3892. PropertyValueInfo info;
  3893. PropertyValueInfo::SetCacheInfo(&info, GetFunctionBody(), inlineCache, playout->inlineCacheIndex, true);
  3894. Var value;
  3895. if (CacheOperators::TryGetProperty<true, false, false, false, false, false, true, false, false, false>(
  3896. obj, false, obj, propertyId, &value, scriptContext, nullptr, &info))
  3897. {
  3898. threadContext->CheckAndResetImplicitCallAccessorFlag();
  3899. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  3900. SetReg(playout->Value, value);
  3901. return;
  3902. }
  3903. }
  3904. OP_GetPropertyScoped_NoFastPath(playout);
  3905. threadContext->CheckAndResetImplicitCallAccessorFlag();
  3906. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  3907. }
  3908. template <typename T>
  3909. void InterpreterStackFrame::OP_GetPropertyForTypeOfScoped(const unaligned OpLayoutT_ElementP<T>* playout)
  3910. {
  3911. ThreadContext* threadContext = this->GetScriptContext()->GetThreadContext();
  3912. ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  3913. threadContext->ClearImplicitCallFlags();
  3914. // Get the property, using a scope stack rather than an individual instance.
  3915. // Use the cache
  3916. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  3917. FrameDisplay *pScope = this->GetEnvForEvalCode();
  3918. InlineCache *inlineCache = this->GetInlineCache(playout->inlineCacheIndex);
  3919. ScriptContext* scriptContext = GetScriptContext();
  3920. int length = pScope->GetLength();
  3921. if (1 == length)
  3922. {
  3923. DynamicObject *obj = (DynamicObject*)pScope->GetItem(0);
  3924. PropertyValueInfo info;
  3925. PropertyValueInfo::SetCacheInfo(&info, GetFunctionBody(), inlineCache, playout->inlineCacheIndex, true);
  3926. Var value;
  3927. if (CacheOperators::TryGetProperty<true, false, false, false, false, false, true, false, false, false>(
  3928. obj, false, obj, propertyId, &value, scriptContext, nullptr, &info))
  3929. {
  3930. threadContext->CheckAndResetImplicitCallAccessorFlag();
  3931. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  3932. SetReg(playout->Value, value);
  3933. return;
  3934. }
  3935. }
  3936. SetReg(
  3937. playout->Value,
  3938. JavascriptOperators::PatchGetPropertyForTypeOfScoped<false>(
  3939. GetFunctionBody(),
  3940. GetInlineCache(playout->inlineCacheIndex),
  3941. playout->inlineCacheIndex,
  3942. GetEnvForEvalCode(),
  3943. GetPropertyIdFromCacheId(playout->inlineCacheIndex),
  3944. GetReg(Js::FunctionBody::RootObjectRegSlot)));
  3945. threadContext->CheckAndResetImplicitCallAccessorFlag();
  3946. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  3947. }
  3948. template <typename T>
  3949. _NOINLINE void InterpreterStackFrame::OP_GetPropertyScoped_NoFastPath(const unaligned OpLayoutT_ElementP<T>* playout)
  3950. {
  3951. // Implicit root object as default instance
  3952. Var defaultInstance = GetReg(Js::FunctionBody::RootObjectRegSlot);
  3953. // PatchGetPropertyScoped doesn't update type and slotIndex if the scope is not an array of length 1.
  3954. SetReg(
  3955. playout->Value,
  3956. JavascriptOperators::PatchGetPropertyScoped<false>(
  3957. GetFunctionBody(),
  3958. GetInlineCache(playout->inlineCacheIndex),
  3959. playout->inlineCacheIndex,
  3960. GetEnvForEvalCode(),
  3961. GetPropertyIdFromCacheId(playout->inlineCacheIndex),
  3962. defaultInstance));
  3963. }
  3964. template <class T>
  3965. void InterpreterStackFrame::OP_SetPropertyScoped(unaligned T* playout, PropertyOperationFlags flags)
  3966. {
  3967. ThreadContext* threadContext = this->GetScriptContext()->GetThreadContext();
  3968. ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  3969. threadContext->ClearImplicitCallFlags();
  3970. // Set the property, using a scope stack rather than an individual instance.
  3971. // Use the cache
  3972. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  3973. FrameDisplay *pScope = this->GetEnvForEvalCode();
  3974. InlineCache *inlineCache = GetInlineCache(playout->inlineCacheIndex);
  3975. ScriptContext* scriptContext = GetScriptContext();
  3976. Var value = GetReg(playout->Value);
  3977. int length = pScope->GetLength();
  3978. if (1 == length)
  3979. {
  3980. RecyclableObject* obj = VarTo<RecyclableObject>(pScope->GetItem(0));
  3981. PropertyValueInfo info;
  3982. PropertyValueInfo::SetCacheInfo(&info, GetFunctionBody(), inlineCache, playout->inlineCacheIndex, true);
  3983. if (CacheOperators::TrySetProperty<true, false, false, false, false, true, false, false>(
  3984. obj, false, propertyId, value, scriptContext, flags, nullptr, &info))
  3985. {
  3986. threadContext->CheckAndResetImplicitCallAccessorFlag();
  3987. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  3988. return;
  3989. }
  3990. }
  3991. OP_SetPropertyScoped_NoFastPath(playout, flags);
  3992. threadContext->CheckAndResetImplicitCallAccessorFlag();
  3993. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  3994. }
  3995. template <class T>
  3996. _NOINLINE void InterpreterStackFrame::OP_SetPropertyScoped_NoFastPath(unaligned T* playout, PropertyOperationFlags flags)
  3997. {
  3998. // Implicit root object as default instance
  3999. Var defaultInstance = GetReg(Js::FunctionBody::RootObjectRegSlot);
  4000. // PatchSetPropertyScoped doesn't update type and slotIndex if the scope is not an array of length 1.
  4001. JavascriptOperators::PatchSetPropertyScoped<false>(
  4002. GetFunctionBody(),
  4003. GetInlineCache(playout->inlineCacheIndex),
  4004. playout->inlineCacheIndex,
  4005. GetEnvForEvalCode(),
  4006. GetPropertyIdFromCacheId(playout->inlineCacheIndex),
  4007. GetReg(playout->Value),
  4008. defaultInstance,
  4009. flags);
  4010. }
  4011. template <class T>
  4012. void InterpreterStackFrame::OP_SetPropertyScopedStrict(unaligned T* playout)
  4013. {
  4014. OP_SetPropertyScoped(playout, PropertyOperation_StrictMode);
  4015. }
  4016. template <class T>
  4017. void InterpreterStackFrame::OP_ConsoleSetPropertyScoped(unaligned T* playout)
  4018. {
  4019. OP_SetPropertyScoped(playout, PropertyOperation_AllowUndeclInConsoleScope);
  4020. }
  4021. template <class T>
  4022. void InterpreterStackFrame::OP_ConsoleSetPropertyScopedStrict(unaligned T* playout)
  4023. {
  4024. OP_SetPropertyScoped(playout, (PropertyOperationFlags)(PropertyOperation_StrictMode | PropertyOperation_AllowUndeclInConsoleScope));
  4025. }
  4026. template <class T>
  4027. inline bool InterpreterStackFrame::TrySetPropertyLocalFastPath(unaligned T* playout, PropertyId pid, RecyclableObject* instance, InlineCache*& inlineCache, PropertyOperationFlags flags)
  4028. {
  4029. inlineCache = this->GetInlineCache(playout->inlineCacheIndex);
  4030. PropertyValueInfo info;
  4031. PropertyValueInfo::SetCacheInfo(&info, GetFunctionBody(), inlineCache, playout->inlineCacheIndex, true);
  4032. return
  4033. CacheOperators::TrySetProperty<true, false, false, false, false, true, false, false>(
  4034. instance,
  4035. !!(flags & PropertyOperation_Root),
  4036. pid,
  4037. GetReg(playout->Value),
  4038. GetScriptContext(),
  4039. flags,
  4040. nullptr,
  4041. &info);
  4042. }
  4043. template <class T>
  4044. inline void InterpreterStackFrame::DoSetProperty(unaligned T* playout, Var instance, PropertyOperationFlags flags)
  4045. {
  4046. // Same fast path as in the backend.
  4047. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  4048. InlineCache *inlineCache;
  4049. if (!TaggedNumber::Is(instance)
  4050. && TrySetPropertyLocalFastPath(playout, propertyId, UnsafeVarTo<RecyclableObject>(instance), inlineCache, flags))
  4051. {
  4052. if (GetJavascriptFunction()->GetConstructorCache()->NeedsUpdateAfterCtor())
  4053. {
  4054. // This function has only 'this' statements and is being used as a constructor. When the constructor exits, the
  4055. // function object's constructor cache will be updated with the type produced by the constructor. From that
  4056. // point on, when the same function object is used as a constructor, the a new object with the final type will
  4057. // be created. Whatever is stored in the inline cache currently will cause cache misses after the constructor
  4058. // cache update. So, just clear it now so that the caches won't be flagged as polymorphic.
  4059. inlineCache->RemoveFromInvalidationListAndClear(this->GetScriptContext()->GetThreadContext());
  4060. }
  4061. return;
  4062. }
  4063. DoSetProperty_NoFastPath(playout, instance, flags);
  4064. }
  4065. template <class T>
  4066. inline void InterpreterStackFrame::DoSetSuperProperty(unaligned T* playout, Var instance, PropertyOperationFlags flags)
  4067. {
  4068. DoSetSuperProperty_NoFastPath(playout, instance, m_functionBody->GetIsStrictMode() ?
  4069. (PropertyOperationFlags)(flags | PropertyOperation_StrictMode) : flags);
  4070. }
  4071. template <class T>
  4072. _NOINLINE void InterpreterStackFrame::DoSetProperty_NoFastPath(unaligned T* playout, Var instance, PropertyOperationFlags flags)
  4073. {
  4074. #if ENABLE_COPYONACCESS_ARRAY
  4075. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  4076. #endif
  4077. InlineCache *const inlineCache = GetInlineCache(playout->inlineCacheIndex);
  4078. const auto PatchPutRootValue = &JavascriptOperators::PatchPutRootValueNoLocalFastPath<false, InlineCache>;
  4079. const auto PatchPutValue = &JavascriptOperators::PatchPutValueNoLocalFastPath<false, InlineCache>;
  4080. const auto PatchPut = flags & PropertyOperation_Root ? PatchPutRootValue : PatchPutValue;
  4081. PatchPut(
  4082. GetFunctionBody(),
  4083. inlineCache,
  4084. playout->inlineCacheIndex,
  4085. instance,
  4086. GetPropertyIdFromCacheId(playout->inlineCacheIndex),
  4087. GetReg(playout->Value),
  4088. flags);
  4089. if (!TaggedNumber::Is(instance) && GetJavascriptFunction()->GetConstructorCache()->NeedsUpdateAfterCtor())
  4090. {
  4091. // This function has only 'this' statements and is being used as a constructor. When the constructor exits, the
  4092. // function object's constructor cache will be updated with the type produced by the constructor. From that
  4093. // point on, when the same function object is used as a constructor, the a new object with the final type will
  4094. // be created. Whatever is stored in the inline cache currently will cause cache misses after the constructor
  4095. // cache update. So, just clear it now so that the caches won't be flagged as polymorphic.
  4096. inlineCache->RemoveFromInvalidationListAndClear(this->GetScriptContext()->GetThreadContext());
  4097. }
  4098. }
  4099. template <class T>
  4100. _NOINLINE void InterpreterStackFrame::DoSetSuperProperty_NoFastPath(unaligned T* playout, Var instance, PropertyOperationFlags flags)
  4101. {
  4102. #if ENABLE_COPYONACCESS_ARRAY
  4103. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  4104. #endif
  4105. InlineCache *const inlineCache = GetInlineCache(playout->PropertyIdIndex);
  4106. JavascriptOperators::PatchPutValueWithThisPtrNoLocalFastPath<false, InlineCache>(
  4107. GetFunctionBody(),
  4108. inlineCache,
  4109. playout->PropertyIdIndex,
  4110. instance,
  4111. GetPropertyIdFromCacheId(playout->PropertyIdIndex),
  4112. GetReg(playout->Value),
  4113. GetReg(playout->Value2),
  4114. flags);
  4115. if (!TaggedNumber::Is(instance) && GetJavascriptFunction()->GetConstructorCache()->NeedsUpdateAfterCtor())
  4116. {
  4117. // This function has only 'this' statements and is being used as a constructor. When the constructor exits, the
  4118. // function object's constructor cache will be updated with the type produced by the constructor. From that
  4119. // point on, when the same function object is used as a constructor, the a new object with the final type will
  4120. // be created. Whatever is stored in the inline cache currently will cause cache misses after the constructor
  4121. // cache update. So, just clear it now so that the caches won't be flagged as polymorphic.
  4122. inlineCache->RemoveFromInvalidationListAndClear(this->GetScriptContext()->GetThreadContext());
  4123. }
  4124. }
  4125. #if ENABLE_PROFILE_INFO
  4126. template <class T, bool Root>
  4127. void InterpreterStackFrame::ProfiledSetProperty(unaligned T* playout, Var instance, PropertyOperationFlags flags)
  4128. {
  4129. Assert(!Root || flags & PropertyOperation_Root);
  4130. ProfilingHelpers::ProfiledStFld<Root>(
  4131. instance,
  4132. GetPropertyIdFromCacheId(playout->inlineCacheIndex),
  4133. GetInlineCache(playout->inlineCacheIndex),
  4134. playout->inlineCacheIndex,
  4135. GetReg(playout->Value),
  4136. flags,
  4137. GetJavascriptFunction(),
  4138. instance);
  4139. }
  4140. template <class T, bool Root>
  4141. void InterpreterStackFrame::ProfiledSetSuperProperty(unaligned T* playout, Var instance, Var thisInstance, PropertyOperationFlags flags)
  4142. {
  4143. Assert(!Root || flags & PropertyOperation_Root);
  4144. ProfilingHelpers::ProfiledStFld<Root>(
  4145. instance,
  4146. GetPropertyIdFromCacheId(playout->PropertyIdIndex),
  4147. GetInlineCache(playout->PropertyIdIndex),
  4148. playout->PropertyIdIndex,
  4149. GetReg(playout->Value),
  4150. flags,
  4151. GetJavascriptFunction(),
  4152. thisInstance);
  4153. }
  4154. #endif
  4155. template <class T>
  4156. void InterpreterStackFrame::OP_SetProperty(unaligned T* playout)
  4157. {
  4158. DoSetProperty(playout, GetReg(playout->Instance), PropertyOperation_None);
  4159. }
  4160. template <class T>
  4161. void InterpreterStackFrame::OP_SetLocalProperty(unaligned T* playout)
  4162. {
  4163. DoSetProperty(playout, this->localClosure, PropertyOperation_None);
  4164. }
  4165. template <class T>
  4166. void InterpreterStackFrame::OP_SetSuperProperty(unaligned T* playout)
  4167. {
  4168. DoSetSuperProperty(playout, GetReg(playout->Instance), PropertyOperation_None);
  4169. }
  4170. template <class T>
  4171. void InterpreterStackFrame::OP_SetSuperPropertyStrict(unaligned T* playout)
  4172. {
  4173. DoSetSuperProperty(playout, GetReg(playout->Instance), PropertyOperation_StrictMode);
  4174. }
  4175. template <class T>
  4176. void InterpreterStackFrame::OP_ProfiledSetProperty(unaligned T* playout)
  4177. {
  4178. ProfiledSetProperty<T, false>(playout, GetReg(playout->Instance), PropertyOperation_None);
  4179. }
  4180. template <class T>
  4181. void InterpreterStackFrame::OP_ProfiledSetLocalProperty(unaligned T* playout)
  4182. {
  4183. ProfiledSetProperty<T, false>(playout, this->localClosure, PropertyOperation_None);
  4184. }
  4185. template <class T>
  4186. void InterpreterStackFrame::OP_ProfiledSetSuperProperty(unaligned T* playout)
  4187. {
  4188. ProfiledSetSuperProperty<T, false>(playout, GetReg(playout->Instance), GetReg(playout->Value2), PropertyOperation_None);
  4189. }
  4190. template <class T>
  4191. void InterpreterStackFrame::OP_ProfiledSetSuperPropertyStrict(unaligned T* playout)
  4192. {
  4193. ProfiledSetSuperProperty<T, false>(playout, GetReg(playout->Instance), GetReg(playout->Value2), PropertyOperation_StrictMode);
  4194. }
  4195. template <class T>
  4196. void InterpreterStackFrame::OP_SetRootProperty(unaligned T* playout)
  4197. {
  4198. DoSetProperty(playout, this->GetRootObject(), PropertyOperation_Root);
  4199. }
  4200. template <class T>
  4201. void InterpreterStackFrame::OP_ProfiledSetRootProperty(unaligned T* playout)
  4202. {
  4203. ProfiledSetProperty<T, true>(playout, this->GetRootObject(), PropertyOperation_Root);
  4204. }
  4205. template <class T>
  4206. void InterpreterStackFrame::OP_SetPropertyStrict(unaligned T* playout)
  4207. {
  4208. DoSetProperty(playout, GetReg(playout->Instance), PropertyOperation_StrictMode);
  4209. }
  4210. template <class T>
  4211. void InterpreterStackFrame::OP_ProfiledSetPropertyStrict(unaligned T* playout)
  4212. {
  4213. ProfiledSetProperty<T, false>(playout, GetReg(playout->Instance), PropertyOperation_StrictMode);
  4214. }
  4215. template <class T>
  4216. void InterpreterStackFrame::OP_SetRootPropertyStrict(unaligned T* playout)
  4217. {
  4218. DoSetProperty(playout, this->GetRootObject(), PropertyOperation_StrictModeRoot);
  4219. }
  4220. template <class T>
  4221. void InterpreterStackFrame::OP_ProfiledSetRootPropertyStrict(unaligned T* playout)
  4222. {
  4223. ProfiledSetProperty<T, true>(playout, this->GetRootObject(), PropertyOperation_StrictModeRoot);
  4224. }
  4225. #if ENABLE_PROFILE_INFO
  4226. template <bool doProfile>
  4227. Var InterpreterStackFrame::ProfiledDivide(Var aLeft, Var aRight, ScriptContext* scriptContext, ProfileId profileId)
  4228. {
  4229. Var result = JavascriptMath::Divide(aLeft, aRight, scriptContext);
  4230. if (doProfile)
  4231. {
  4232. Js::FunctionBody* body = this->m_functionBody;
  4233. body->GetDynamicProfileInfo()->RecordDivideResultType(body, profileId, result);
  4234. }
  4235. return result;
  4236. }
  4237. template <bool doProfile>
  4238. Var InterpreterStackFrame::ProfileModulus(Var aLeft, Var aRight, ScriptContext* scriptContext, ProfileId profileId)
  4239. {
  4240. // If both arguments are TaggedInt, then try to do integer division
  4241. // This case is not handled by the lowerer.
  4242. if (doProfile)
  4243. {
  4244. Js::FunctionBody* body = this->function->GetFunctionBody();
  4245. if (TaggedInt::IsPair(aLeft, aRight))
  4246. {
  4247. int nLeft = TaggedInt::ToInt32(aLeft);
  4248. int nRight = TaggedInt::ToInt32(aRight);
  4249. // nLeft is positive and nRight is +2^i
  4250. // Fast path for Power of 2 divisor
  4251. if (nLeft > 0 && ::Math::IsPow2(nRight))
  4252. {
  4253. body->GetDynamicProfileInfo()->RecordModulusOpType(body, profileId, /*isModByPowerOf2*/ true);
  4254. return TaggedInt::ToVarUnchecked(nLeft & (nRight - 1));
  4255. }
  4256. }
  4257. body->GetDynamicProfileInfo()->RecordModulusOpType(body, profileId, /*isModByPowerOf2*/ false);
  4258. }
  4259. return JavascriptMath::Modulus(aLeft, aRight, scriptContext);
  4260. }
  4261. template <bool doProfile>
  4262. Var InterpreterStackFrame::ProfiledSwitch(Var exp, ProfileId profileId)
  4263. {
  4264. if (doProfile)
  4265. {
  4266. Js::FunctionBody* body = this->m_functionBody;
  4267. body->GetDynamicProfileInfo()->RecordSwitchType(body, profileId, exp);
  4268. }
  4269. return exp;
  4270. }
  4271. #else
  4272. template <bool doProfile>
  4273. Var InterpreterStackFrame::ProfiledDivide(Var aLeft, Var aRight, ScriptContext* scriptContext, ProfileId profileId)
  4274. {
  4275. Assert(!doProfile);
  4276. return JavascriptMath::Divide(aLeft, aRight, scriptContext);
  4277. }
  4278. template <bool doProfile>
  4279. Var InterpreterStackFrame::ProfileModulus(Var aLeft, Var aRight, ScriptContext* scriptContext, ProfileId profileId)
  4280. {
  4281. Assert(!doProfile);
  4282. return JavascriptMath::Modulus(aLeft, aRight, scriptContext);
  4283. }
  4284. template <bool doProfile>
  4285. Var InterpreterStackFrame::ProfiledSwitch(Var exp, ProfileId profileId)
  4286. {
  4287. Assert(!doProfile);
  4288. return exp;
  4289. }
  4290. #endif
  4291. template <class T>
  4292. void InterpreterStackFrame::DoInitProperty(unaligned T* playout, Var instance)
  4293. {
  4294. // Same fast path as in the backend.
  4295. InlineCache *inlineCache = nullptr;
  4296. Assert(!TaggedNumber::Is(instance));
  4297. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  4298. if (TrySetPropertyLocalFastPath(playout, propertyId, UnsafeVarTo<RecyclableObject>(instance), inlineCache))
  4299. {
  4300. return;
  4301. }
  4302. DoInitProperty_NoFastPath(playout, instance);
  4303. }
  4304. template <class T>
  4305. _NOINLINE void InterpreterStackFrame::DoInitProperty_NoFastPath(unaligned T* playout, Var instance)
  4306. {
  4307. JavascriptOperators::PatchInitValue<false>(
  4308. GetFunctionBody(),
  4309. GetInlineCache(playout->inlineCacheIndex),
  4310. playout->inlineCacheIndex,
  4311. VarTo<RecyclableObject>(instance),
  4312. GetPropertyIdFromCacheId(playout->inlineCacheIndex),
  4313. GetReg(playout->Value));
  4314. }
  4315. template <class T>
  4316. void InterpreterStackFrame::OP_InitClassMember(const unaligned T * playout)
  4317. {
  4318. uint inlineCacheIndex = playout->inlineCacheIndex;
  4319. InlineCache * inlineCache = this->GetInlineCache(inlineCacheIndex);
  4320. Var instance = GetReg(playout->Instance);
  4321. PropertyOperationFlags flags = PropertyOperation_None;
  4322. Assert(!TaggedNumber::Is(instance));
  4323. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  4324. if (!TrySetPropertyLocalFastPath(playout, propertyId, UnsafeVarTo<RecyclableObject>(instance), inlineCache, flags))
  4325. {
  4326. JavascriptOperators::OP_InitClassMember(instance, propertyId, GetReg(playout->Value));
  4327. }
  4328. }
  4329. template <class T>
  4330. void InterpreterStackFrame::OP_InitClassMemberGet(const unaligned T * playout)
  4331. {
  4332. JavascriptOperators::OP_InitClassMemberGet(
  4333. GetReg(playout->Instance),
  4334. m_functionBody->GetReferencedPropertyId(playout->PropertyIdIndex),
  4335. GetReg(playout->Value));
  4336. }
  4337. template <class T>
  4338. void InterpreterStackFrame::OP_InitClassMemberSet(const unaligned T * playout)
  4339. {
  4340. JavascriptOperators::OP_InitClassMemberSet(
  4341. GetReg(playout->Instance),
  4342. m_functionBody->GetReferencedPropertyId(playout->PropertyIdIndex),
  4343. GetReg(playout->Value));
  4344. }
  4345. template <class T>
  4346. void InterpreterStackFrame::OP_InitClassMemberSetComputedName(const unaligned T * playout)
  4347. {
  4348. JavascriptOperators::OP_InitClassMemberSetComputedName(
  4349. GetReg(playout->Instance),
  4350. GetReg(playout->Element),
  4351. GetReg(playout->Value),
  4352. m_functionBody->GetScriptContext());
  4353. }
  4354. template <class T>
  4355. void InterpreterStackFrame::OP_InitClassMemberGetComputedName(const unaligned T * playout)
  4356. {
  4357. JavascriptOperators::OP_InitClassMemberGetComputedName(
  4358. GetReg(playout->Instance),
  4359. GetReg(playout->Element),
  4360. GetReg(playout->Value),
  4361. m_functionBody->GetScriptContext());
  4362. }
  4363. template <class T>
  4364. void InterpreterStackFrame::OP_InitClassMemberComputedName(const unaligned T * playout)
  4365. {
  4366. JavascriptOperators::OP_InitClassMemberComputedName(
  4367. GetReg(playout->Instance),
  4368. GetReg(playout->Element),
  4369. GetReg(playout->Value),
  4370. m_functionBody->GetScriptContext());
  4371. }
  4372. template <class T>
  4373. void InterpreterStackFrame::DoInitLetFld(const unaligned T * playout, Var instance, PropertyOperationFlags flags)
  4374. {
  4375. uint inlineCacheIndex = playout->inlineCacheIndex;
  4376. InlineCache * inlineCache = this->GetInlineCache(inlineCacheIndex);
  4377. Assert(!TaggedNumber::Is(instance));
  4378. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  4379. if (!TrySetPropertyLocalFastPath(playout, propertyId, UnsafeVarTo<RecyclableObject>(instance), inlineCache, flags))
  4380. {
  4381. JavascriptOperators::OP_InitLetProperty(instance, propertyId, GetReg(playout->Value));
  4382. }
  4383. }
  4384. template <class T>
  4385. void InterpreterStackFrame::DoInitConstFld(const unaligned T * playout, Var instance, PropertyOperationFlags flags)
  4386. {
  4387. uint inlineCacheIndex = playout->inlineCacheIndex;
  4388. InlineCache * inlineCache = this->GetInlineCache(inlineCacheIndex);
  4389. Assert(!TaggedNumber::Is(instance));
  4390. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  4391. if (!TrySetPropertyLocalFastPath(playout, propertyId, UnsafeVarTo<RecyclableObject>(instance), inlineCache, flags))
  4392. {
  4393. JavascriptOperators::OP_InitConstProperty(instance, propertyId, GetReg(playout->Value));
  4394. }
  4395. }
  4396. template <class T>
  4397. void InterpreterStackFrame::OP_InitProperty(unaligned T* playout)
  4398. {
  4399. DoInitProperty(playout, GetReg(playout->Instance));
  4400. }
  4401. template <class T>
  4402. void InterpreterStackFrame::OP_InitLocalProperty(unaligned T* playout)
  4403. {
  4404. DoInitProperty(playout, this->localClosure);
  4405. }
  4406. template <class T>
  4407. void InterpreterStackFrame::OP_InitInnerFld(const unaligned T* playout)
  4408. {
  4409. DoInitProperty(playout, InnerScopeFromIndex(playout->scopeIndex));
  4410. }
  4411. template <class T>
  4412. void InterpreterStackFrame::OP_InitLetFld(const unaligned T * playout)
  4413. {
  4414. DoInitLetFld(playout, GetReg(playout->Instance));
  4415. }
  4416. template <class T>
  4417. void InterpreterStackFrame::OP_InitInnerLetFld(const unaligned T * playout)
  4418. {
  4419. DoInitLetFld(playout, InnerScopeFromIndex(playout->scopeIndex));
  4420. }
  4421. template <class T>
  4422. void InterpreterStackFrame::OP_InitLocalLetFld(const unaligned T * playout)
  4423. {
  4424. DoInitLetFld(playout, this->localClosure);
  4425. }
  4426. template <class T>
  4427. void InterpreterStackFrame::OP_InitConstFld(const unaligned T * playout)
  4428. {
  4429. DoInitConstFld(playout, GetReg(playout->Instance));
  4430. }
  4431. template <class T>
  4432. void InterpreterStackFrame::OP_InitRootProperty(unaligned T* playout)
  4433. {
  4434. Assert(playout->inlineCacheIndex >= this->m_functionBody->GetRootObjectLoadInlineCacheStart());
  4435. DoInitProperty(playout, this->GetRootObject());
  4436. }
  4437. template <class T>
  4438. void InterpreterStackFrame::OP_InitRootLetFld(const unaligned T * playout)
  4439. {
  4440. Assert(playout->inlineCacheIndex >= this->m_functionBody->GetRootObjectLoadInlineCacheStart());
  4441. DoInitLetFld(playout, this->GetRootObject(), PropertyOperation_Root);
  4442. }
  4443. template <class T>
  4444. void InterpreterStackFrame::OP_InitRootConstFld(const unaligned T * playout)
  4445. {
  4446. Assert(playout->inlineCacheIndex >= this->m_functionBody->GetRootObjectLoadInlineCacheStart());
  4447. DoInitConstFld(playout, this->GetRootObject(), PropertyOperation_Root);
  4448. }
  4449. template <class T>
  4450. void InterpreterStackFrame::OP_InitUndeclLetProperty(unaligned T* playout)
  4451. {
  4452. Var instance = InnerScopeFromIndex(playout->scopeIndex);
  4453. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  4454. JavascriptOperators::OP_InitLetProperty(instance, propertyId, this->scriptContext->GetLibrary()->GetUndeclBlockVar());
  4455. }
  4456. template <class T>
  4457. void InterpreterStackFrame::OP_InitUndeclLocalLetProperty(unaligned T* playout)
  4458. {
  4459. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  4460. JavascriptOperators::OP_InitLetProperty(this->localClosure, propertyId, this->scriptContext->GetLibrary()->GetUndeclBlockVar());
  4461. }
  4462. void InterpreterStackFrame::OP_InitUndeclRootLetProperty(uint propertyIdIndex)
  4463. {
  4464. Var instance = this->GetRootObject();
  4465. PropertyId propertyId = this->m_functionBody->GetReferencedPropertyId(propertyIdIndex);
  4466. JavascriptOperators::OP_InitUndeclRootLetProperty(instance, propertyId);
  4467. }
  4468. template <class T>
  4469. void InterpreterStackFrame::OP_InitUndeclConstProperty(unaligned T* playout)
  4470. {
  4471. Var instance = InnerScopeFromIndex(playout->scopeIndex);
  4472. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  4473. JavascriptOperators::OP_InitConstProperty(instance, propertyId, this->scriptContext->GetLibrary()->GetUndeclBlockVar());
  4474. }
  4475. template <class T>
  4476. void InterpreterStackFrame::OP_InitUndeclLocalConstProperty(unaligned T* playout)
  4477. {
  4478. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  4479. JavascriptOperators::OP_InitConstProperty(this->localClosure, propertyId, this->scriptContext->GetLibrary()->GetUndeclBlockVar());
  4480. }
  4481. void InterpreterStackFrame::OP_InitUndeclRootConstProperty(uint propertyIdIndex)
  4482. {
  4483. Var instance = this->GetRootObject();
  4484. PropertyId propertyId = this->m_functionBody->GetReferencedPropertyId(propertyIdIndex);
  4485. JavascriptOperators::OP_InitUndeclRootConstProperty(instance, propertyId);
  4486. }
  4487. template <class T>
  4488. void InterpreterStackFrame::OP_InitUndeclConsoleLetProperty(unaligned T* playout)
  4489. {
  4490. FrameDisplay* pScope = (FrameDisplay*)this->LdEnv();
  4491. AssertMsg(VarIs<ConsoleScopeActivationObject>((DynamicObject*)pScope->GetItem(pScope->GetLength() - 1)), "How come we got this opcode without ConsoleScopeActivationObject?");
  4492. PropertyId propertyId = m_functionBody->GetReferencedPropertyId(playout->PropertyIdIndex);
  4493. JavascriptOperators::OP_InitLetProperty(pScope->GetItem(0), propertyId, this->scriptContext->GetLibrary()->GetUndeclBlockVar());
  4494. }
  4495. template <class T>
  4496. void InterpreterStackFrame::OP_InitUndeclConsoleConstProperty(unaligned T* playout)
  4497. {
  4498. FrameDisplay* pScope = (FrameDisplay*)this->LdEnv();
  4499. AssertMsg(VarIs<ConsoleScopeActivationObject>((DynamicObject*)pScope->GetItem(pScope->GetLength() - 1)), "How come we got this opcode without ConsoleScopeActivationObject?");
  4500. PropertyId propertyId = m_functionBody->GetReferencedPropertyId(playout->PropertyIdIndex);
  4501. JavascriptOperators::OP_InitConstProperty(pScope->GetItem(0), propertyId, this->scriptContext->GetLibrary()->GetUndeclBlockVar());
  4502. }
  4503. #if ENABLE_PROFILE_INFO
  4504. template <class T>
  4505. void InterpreterStackFrame::ProfiledInitProperty(unaligned T* playout, Var instance)
  4506. {
  4507. ProfilingHelpers::ProfiledInitFld(
  4508. VarTo<RecyclableObject>(instance),
  4509. GetPropertyIdFromCacheId(playout->inlineCacheIndex),
  4510. GetInlineCache(playout->inlineCacheIndex),
  4511. playout->inlineCacheIndex,
  4512. GetReg(playout->Value),
  4513. GetFunctionBody());
  4514. }
  4515. template <class T>
  4516. void InterpreterStackFrame::OP_ProfiledInitProperty(unaligned T* playout)
  4517. {
  4518. ProfiledInitProperty(playout, GetReg(playout->Instance));
  4519. }
  4520. template <class T>
  4521. void InterpreterStackFrame::OP_ProfiledInitLocalProperty(unaligned T* playout)
  4522. {
  4523. ProfiledInitProperty(playout, this->localClosure);
  4524. }
  4525. template <class T>
  4526. void InterpreterStackFrame::OP_ProfiledInitRootProperty(unaligned T* playout)
  4527. {
  4528. ProfiledInitProperty(playout, this->GetRootObject());
  4529. }
  4530. template <class T>
  4531. void InterpreterStackFrame::OP_ProfiledGetElementI(const unaligned OpLayoutDynamicProfile<T>* playout)
  4532. {
  4533. ThreadContext* threadContext = this->GetScriptContext()->GetThreadContext();
  4534. ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  4535. threadContext->ClearImplicitCallFlags();
  4536. SetReg(
  4537. playout->Value,
  4538. ProfilingHelpers::ProfiledLdElem(
  4539. GetReg(playout->Instance),
  4540. GetReg(playout->Element),
  4541. m_functionBody,
  4542. playout->profileId,
  4543. this->TestFlags(InterpreterStackFrameFlags_ProcessingBailOutOnArrayAccessHelperCall),
  4544. this->TestFlags(InterpreterStackFrameFlags_ProcessingBailOutOnArraySpecialization)));
  4545. this->ClearFlags(InterpreterStackFrameFlags_ProcessingBailOutOnArrayAccessHelperCall | InterpreterStackFrameFlags_ProcessingBailOutOnArraySpecialization);
  4546. threadContext->CheckAndResetImplicitCallAccessorFlag();
  4547. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  4548. }
  4549. #endif
  4550. template <typename T>
  4551. void InterpreterStackFrame::OP_GetElementI(const unaligned T* playout)
  4552. {
  4553. ThreadContext* threadContext = this->GetScriptContext()->GetThreadContext();
  4554. ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  4555. threadContext->ClearImplicitCallFlags();
  4556. // Same fast path as in the backend.
  4557. Var instance = GetReg(playout->Instance);
  4558. // Only enable fast path if the javascript array is not cross site
  4559. Var element;
  4560. #if ENABLE_PROFILE_INFO
  4561. if (!TaggedNumber::Is(instance) && VirtualTableInfo<JavascriptArray>::HasVirtualTable(instance))
  4562. {
  4563. element =
  4564. ProfilingHelpers::ProfiledLdElem_FastPath(
  4565. UnsafeVarTo<JavascriptArray>(instance),
  4566. GetReg(playout->Element),
  4567. GetScriptContext());
  4568. }
  4569. else
  4570. #endif
  4571. {
  4572. element = JavascriptOperators::OP_GetElementI(instance, GetReg(playout->Element), GetScriptContext());
  4573. }
  4574. this->ClearFlags(InterpreterStackFrameFlags_ProcessingBailOutOnArrayAccessHelperCall);
  4575. threadContext->CheckAndResetImplicitCallAccessorFlag();
  4576. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  4577. SetReg(playout->Value, element);
  4578. }
  4579. template <typename T>
  4580. void InterpreterStackFrame::OP_SetElementI(const unaligned T* playout, PropertyOperationFlags flags)
  4581. {
  4582. ThreadContext* threadContext = this->GetScriptContext()->GetThreadContext();
  4583. ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  4584. threadContext->ClearImplicitCallFlags();
  4585. // Same fast path as in the backend.
  4586. Var instance = GetReg(playout->Instance);
  4587. const Var varIndex = GetReg(playout->Element);
  4588. const Var value = GetReg(playout->Value);
  4589. #if ENABLE_PROFILE_INFO
  4590. // Only enable fast path if the javascript array is not cross site
  4591. if (!TaggedNumber::Is(instance) &&
  4592. VirtualTableInfo<JavascriptArray>::HasVirtualTable(instance) &&
  4593. !JavascriptOperators::SetElementMayHaveImplicitCalls(GetScriptContext()))
  4594. {
  4595. ProfilingHelpers::ProfiledStElem_FastPath(
  4596. UnsafeVarTo<JavascriptArray>(instance),
  4597. varIndex,
  4598. value,
  4599. GetScriptContext(),
  4600. flags);
  4601. }
  4602. else
  4603. #endif
  4604. {
  4605. JavascriptOperators::OP_SetElementI(instance, varIndex, value, GetScriptContext(), flags);
  4606. }
  4607. this->ClearFlags(InterpreterStackFrameFlags_ProcessingBailOutOnArrayAccessHelperCall);
  4608. threadContext->CheckAndResetImplicitCallAccessorFlag();
  4609. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  4610. }
  4611. #if ENABLE_PROFILE_INFO
  4612. template <typename T>
  4613. void InterpreterStackFrame::OP_ProfiledSetElementI(
  4614. const unaligned OpLayoutDynamicProfile<T>* playout,
  4615. PropertyOperationFlags flags)
  4616. {
  4617. ThreadContext* threadContext = this->GetScriptContext()->GetThreadContext();
  4618. ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  4619. threadContext->ClearImplicitCallFlags();
  4620. ProfilingHelpers::ProfiledStElem(
  4621. GetReg(playout->Instance),
  4622. GetReg(playout->Element),
  4623. GetReg(playout->Value),
  4624. m_functionBody,
  4625. playout->profileId,
  4626. flags,
  4627. this->TestFlags(InterpreterStackFrameFlags_ProcessingBailOutOnArrayAccessHelperCall),
  4628. this->TestFlags(InterpreterStackFrameFlags_ProcessingBailOutOnArraySpecialization));
  4629. this->ClearFlags(InterpreterStackFrameFlags_ProcessingBailOutOnArrayAccessHelperCall | InterpreterStackFrameFlags_ProcessingBailOutOnArraySpecialization);
  4630. threadContext->CheckAndResetImplicitCallAccessorFlag();
  4631. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  4632. }
  4633. #endif
  4634. template <typename T>
  4635. void InterpreterStackFrame::OP_SetElementIStrict(const unaligned T* playout)
  4636. {
  4637. ThreadContext* threadContext = this->GetScriptContext()->GetThreadContext();
  4638. ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  4639. threadContext->ClearImplicitCallFlags();
  4640. OP_SetElementI(playout, PropertyOperation_StrictMode);
  4641. threadContext->CheckAndResetImplicitCallAccessorFlag();
  4642. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  4643. }
  4644. #if ENABLE_PROFILE_INFO
  4645. template <typename T>
  4646. void InterpreterStackFrame::OP_ProfiledSetElementIStrict(const unaligned OpLayoutDynamicProfile<T>* playout)
  4647. {
  4648. ThreadContext* threadContext = this->GetScriptContext()->GetThreadContext();
  4649. ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  4650. threadContext->ClearImplicitCallFlags();
  4651. OP_ProfiledSetElementI(playout, PropertyOperation_StrictMode);
  4652. threadContext->CheckAndResetImplicitCallAccessorFlag();
  4653. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  4654. }
  4655. #endif
  4656. template <class T>
  4657. void InterpreterStackFrame::OP_LdArrayHeadSegment(const unaligned T* playout)
  4658. {
  4659. JavascriptArray* array = JavascriptArray::FromAnyArray(GetReg(playout->R1));
  4660. // The array is create by the built-in on the same script context
  4661. Assert(array->GetScriptContext() == GetScriptContext());
  4662. SetNonVarReg(playout->R0, array->GetHead());
  4663. }
  4664. template <class T>
  4665. void InterpreterStackFrame::OP_SetArraySegmentItem_CI4(const unaligned T* playout)
  4666. {
  4667. SparseArraySegment<Var> * segment = (SparseArraySegment<Var> *)GetNonVarReg(playout->Instance);
  4668. uint32 index = playout->Element;
  4669. Var value = GetReg(playout->Value);
  4670. Assert(segment->left == 0);
  4671. Assert(index < segment->length);
  4672. segment->elements[index] = value;
  4673. }
  4674. template <class T>
  4675. void InterpreterStackFrame::OP_NewScArray(const unaligned T * playout)
  4676. {
  4677. JavascriptArray *arr;
  4678. arr = scriptContext->GetLibrary()->CreateArrayLiteral(playout->C1);
  4679. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  4680. arr->CheckForceES5Array();
  4681. #endif
  4682. SetReg(playout->R0, arr);
  4683. }
  4684. #if ENABLE_PROFILE_INFO
  4685. template <bool Profiled, class T>
  4686. void InterpreterStackFrame::ProfiledNewScArray(const unaligned OpLayoutDynamicProfile<T> * playout)
  4687. {
  4688. if (!Profiled && !isAutoProfiling)
  4689. {
  4690. OP_NewScArray(playout);
  4691. return;
  4692. }
  4693. SetReg(
  4694. playout->R0,
  4695. ProfilingHelpers::ProfiledNewScArray(
  4696. playout->C1,
  4697. m_functionBody,
  4698. playout->profileId));
  4699. }
  4700. #else
  4701. template <bool Profiled, class T>
  4702. void InterpreterStackFrame::ProfiledNewScArray(const unaligned OpLayoutDynamicProfile<T> * playout)
  4703. {
  4704. Assert(!Profiled);
  4705. OP_NewScArray(playout);
  4706. }
  4707. #endif
  4708. void InterpreterStackFrame::OP_NewScIntArray(const unaligned OpLayoutAuxiliary * playout)
  4709. {
  4710. const Js::AuxArray<int32> *ints = Js::ByteCodeReader::ReadAuxArray<int32>(playout->Offset, this->GetFunctionBody());
  4711. JavascriptNativeIntArray *arr = scriptContext->GetLibrary()->CreateNativeIntArrayLiteral(ints->count);
  4712. SparseArraySegment<int32> * segment = (SparseArraySegment<int32>*)arr->GetHead();
  4713. JavascriptOperators::AddIntsToArraySegment(segment, ints);
  4714. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  4715. arr->CheckForceES5Array();
  4716. #endif
  4717. SetReg(playout->R0, arr);
  4718. }
  4719. #if ENABLE_PROFILE_INFO
  4720. template <bool Profiled>
  4721. void InterpreterStackFrame::ProfiledNewScIntArray(const unaligned OpLayoutDynamicProfile<OpLayoutAuxiliary> * playout)
  4722. {
  4723. if (!Profiled && !isAutoProfiling)
  4724. {
  4725. OP_NewScIntArray(playout);
  4726. return;
  4727. }
  4728. const Js::AuxArray<int32> *ints = Js::ByteCodeReader::ReadAuxArray<int32>(playout->Offset, this->GetFunctionBody());
  4729. Js::ProfileId profileId = playout->profileId;
  4730. FunctionBody *functionBody = this->m_functionBody;
  4731. ArrayCallSiteInfo *arrayInfo = functionBody->GetDynamicProfileInfo()->GetArrayCallSiteInfo(functionBody, profileId);
  4732. Assert(arrayInfo);
  4733. JavascriptArray *arr;
  4734. if (arrayInfo && arrayInfo->IsNativeIntArray())
  4735. {
  4736. #if ENABLE_COPYONACCESS_ARRAY
  4737. JavascriptLibrary *lib = scriptContext->GetLibrary();
  4738. if (JavascriptLibrary::IsCopyOnAccessArrayCallSite(lib, arrayInfo, ints->count))
  4739. {
  4740. Assert(lib->cacheForCopyOnAccessArraySegments);
  4741. arr = scriptContext->GetLibrary()->CreateCopyOnAccessNativeIntArrayLiteral(arrayInfo, functionBody, ints);
  4742. }
  4743. else
  4744. #endif
  4745. {
  4746. arr = scriptContext->GetLibrary()->CreateNativeIntArrayLiteral(ints->count);
  4747. SparseArraySegment<int32> *segment = (SparseArraySegment<int32>*)arr->GetHead();
  4748. JavascriptOperators::AddIntsToArraySegment(segment, ints);
  4749. }
  4750. JavascriptNativeIntArray *intArray = reinterpret_cast<JavascriptNativeIntArray*>(arr);
  4751. Recycler *recycler = scriptContext->GetRecycler();
  4752. intArray->SetArrayCallSite(profileId, recycler->CreateWeakReferenceHandle(functionBody));
  4753. }
  4754. else if (arrayInfo && arrayInfo->IsNativeFloatArray())
  4755. {
  4756. arr = scriptContext->GetLibrary()->CreateNativeFloatArrayLiteral(ints->count);
  4757. SparseArraySegment<double> * segment = (SparseArraySegment<double>*)arr->GetHead();
  4758. for (uint i = 0; i < ints->count; i++)
  4759. {
  4760. segment->elements[i] = (double)ints->elements[i];
  4761. }
  4762. JavascriptNativeFloatArray *floatArray = reinterpret_cast<JavascriptNativeFloatArray*>(arr);
  4763. Recycler *recycler = scriptContext->GetRecycler();
  4764. floatArray->SetArrayCallSite(profileId, recycler->CreateWeakReferenceHandle(functionBody));
  4765. }
  4766. else
  4767. {
  4768. arr = scriptContext->GetLibrary()->CreateArrayLiteral(ints->count);
  4769. SparseArraySegment<Var> * segment = (SparseArraySegment<Var>*)arr->GetHead();
  4770. for (uint i = 0; i < ints->count; i++)
  4771. {
  4772. segment->elements[i] = JavascriptNumber::ToVar(ints->elements[i], scriptContext);
  4773. }
  4774. }
  4775. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  4776. arr->CheckForceES5Array();
  4777. #endif
  4778. SetReg(playout->R0, arr);
  4779. }
  4780. #else
  4781. template <bool Profiled>
  4782. void InterpreterStackFrame::ProfiledNewScIntArray(const unaligned OpLayoutDynamicProfile<OpLayoutAuxiliary> * playout)
  4783. {
  4784. OP_NewScIntArray(playout);
  4785. }
  4786. #endif
  4787. void InterpreterStackFrame::OP_NewScFltArray(const unaligned OpLayoutAuxiliary * playout)
  4788. {
  4789. const Js::AuxArray<double> *doubles = Js::ByteCodeReader::ReadAuxArray<double>(playout->Offset, this->GetFunctionBody());
  4790. JavascriptNativeFloatArray *arr = scriptContext->GetLibrary()->CreateNativeFloatArrayLiteral(doubles->count);
  4791. SparseArraySegment<double> * segment = (SparseArraySegment<double>*)arr->GetHead();
  4792. JavascriptOperators::AddFloatsToArraySegment(segment, doubles);
  4793. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  4794. arr->CheckForceES5Array();
  4795. #endif
  4796. SetReg(playout->R0, arr);
  4797. }
  4798. #if ENABLE_PROFILE_INFO
  4799. template <bool Profiled>
  4800. void InterpreterStackFrame::ProfiledNewScFltArray(const unaligned OpLayoutDynamicProfile<OpLayoutAuxiliary> * playout)
  4801. {
  4802. if (!Profiled && !isAutoProfiling)
  4803. {
  4804. OP_NewScFltArray(playout);
  4805. return;
  4806. }
  4807. const Js::AuxArray<double> *doubles = Js::ByteCodeReader::ReadAuxArray<double>(playout->Offset, this->GetFunctionBody());
  4808. Js::ProfileId profileId = playout->profileId;
  4809. FunctionBody *functionBody = this->m_functionBody;
  4810. ArrayCallSiteInfo *arrayInfo = functionBody->GetDynamicProfileInfo()->GetArrayCallSiteInfo(functionBody, profileId);
  4811. Assert(arrayInfo);
  4812. JavascriptArray *arr;
  4813. if (arrayInfo && arrayInfo->IsNativeFloatArray())
  4814. {
  4815. arrayInfo->SetIsNotNativeIntArray();
  4816. arr = scriptContext->GetLibrary()->CreateNativeFloatArrayLiteral(doubles->count);
  4817. SparseArraySegment<double> * segment = (SparseArraySegment<double>*)arr->GetHead();
  4818. JavascriptOperators::AddFloatsToArraySegment(segment, doubles);
  4819. JavascriptNativeFloatArray *floatArray = reinterpret_cast<JavascriptNativeFloatArray*>(arr);
  4820. Recycler *recycler = scriptContext->GetRecycler();
  4821. floatArray->SetArrayCallSite(profileId, recycler->CreateWeakReferenceHandle(functionBody));
  4822. }
  4823. else
  4824. {
  4825. arr = scriptContext->GetLibrary()->CreateArrayLiteral(doubles->count);
  4826. SparseArraySegment<Var> * segment = (SparseArraySegment<Var>*)arr->GetHead();
  4827. for (uint i = 0; i < doubles->count; i++)
  4828. {
  4829. segment->elements[i] = JavascriptNumber::ToVarNoCheck(doubles->elements[i], scriptContext);
  4830. }
  4831. }
  4832. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  4833. arr->CheckForceES5Array();
  4834. #endif
  4835. SetReg(playout->R0, arr);
  4836. }
  4837. #else
  4838. template <bool Profiled>
  4839. void InterpreterStackFrame::ProfiledNewScFltArray(const unaligned OpLayoutDynamicProfile<OpLayoutAuxiliary> * playout)
  4840. {
  4841. OP_NewScFltArray(playout);
  4842. }
  4843. #endif
  4844. void InterpreterStackFrame::OP_SetArraySegmentVars(const unaligned OpLayoutAuxiliary * playout)
  4845. {
  4846. const Js::VarArray *vars = Js::ByteCodeReader::ReadAuxArray<Var>(playout->Offset, this->GetFunctionBody());
  4847. SparseArraySegment<Var> * segment = (SparseArraySegment<Var> *)GetNonVarReg(playout->R0);
  4848. JavascriptOperators::AddVarsToArraySegment(segment, vars);
  4849. }
  4850. template <class T>
  4851. void InterpreterStackFrame::OP_SetArrayItemC_CI4(const unaligned T* playout)
  4852. {
  4853. JavascriptArray* array = JavascriptArray::FromAnyArray(GetReg(playout->Instance));
  4854. uint32 index = playout->Element;
  4855. Var value = GetReg(playout->Value);
  4856. #if ENABLE_COPYONACCESS_ARRAY
  4857. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(value);
  4858. #endif
  4859. // The array is create by the built-in on the same script context
  4860. Assert(array->GetScriptContext() == GetScriptContext());
  4861. TypeId typeId = array->GetTypeId();
  4862. if (typeId == TypeIds_NativeIntArray)
  4863. {
  4864. JavascriptArray::OP_SetNativeIntElementC(reinterpret_cast<JavascriptNativeIntArray*>(array), index, value, array->GetScriptContext());
  4865. }
  4866. else if (typeId == TypeIds_NativeFloatArray)
  4867. {
  4868. JavascriptArray::OP_SetNativeFloatElementC(reinterpret_cast<JavascriptNativeFloatArray*>(array), index, value, array->GetScriptContext());
  4869. }
  4870. else
  4871. {
  4872. array->SetArrayLiteralItem(index, value);
  4873. }
  4874. }
  4875. template <class T>
  4876. void InterpreterStackFrame::OP_SetArrayItemI_CI4(const unaligned T* playout)
  4877. {
  4878. // Note that this code assumes that we only get here when we see an array literal,
  4879. // so we know that the instance is truly an array, and the index is a uint32.
  4880. // If/when we use this for cases like "a[0] = x", we'll at least have to check
  4881. // whether "a" is really an array.
  4882. JavascriptArray* array = JavascriptArray::FromAnyArray(GetReg(playout->Instance));
  4883. // The array is create by the built-in on the same script context
  4884. Assert(array->GetScriptContext() == GetScriptContext());
  4885. uint32 index = playout->Element;
  4886. Var value = GetReg(playout->Value);
  4887. Assert(VirtualTableInfo<JavascriptArray>::HasVirtualTable(array));
  4888. SparseArraySegment<Var>* lastUsedSeg = (SparseArraySegment<Var>*)array->GetLastUsedSegment();
  4889. if (index >= lastUsedSeg->left)
  4890. {
  4891. uint32 index2 = index - lastUsedSeg->left;
  4892. if (index2 < lastUsedSeg->size)
  4893. {
  4894. // Successful fastpath
  4895. array->DirectSetItemInLastUsedSegmentAt(index2, value);
  4896. return;
  4897. }
  4898. }
  4899. ScriptContext* scriptContext = array->GetScriptContext();
  4900. JavascriptOperators::SetItem(array, array, index, value, scriptContext);
  4901. }
  4902. #if ENABLE_PROFILE_INFO
  4903. Var InterpreterStackFrame::OP_ProfiledLdThis(Var thisVar, int moduleID, ScriptContext *scriptContext)
  4904. {
  4905. FunctionBody * functionBody = this->m_functionBody;
  4906. DynamicProfileInfo * dynamicProfileInfo = functionBody->GetDynamicProfileInfo();
  4907. TypeId typeId = JavascriptOperators::GetTypeId(thisVar);
  4908. if (JavascriptOperators::IsThisSelf(typeId))
  4909. {
  4910. Assert(typeId != TypeIds_GlobalObject || ((Js::GlobalObject*)thisVar)->ToThis() == thisVar);
  4911. Assert(typeId != TypeIds_ModuleRoot || JavascriptOperators::GetThisFromModuleRoot(thisVar) == thisVar);
  4912. // Record the fact that we saw a trivial LdThis.
  4913. dynamicProfileInfo->RecordThisInfo(thisVar, ThisType_Simple);
  4914. return thisVar;
  4915. }
  4916. thisVar = JavascriptOperators::OP_GetThis(thisVar, moduleID, scriptContext);
  4917. // Record the fact that we saw a LdThis that had to map its source to something else, or at least
  4918. // forced us to call a helper, e.g., a FastDOM object with an unrecognized type ID.
  4919. dynamicProfileInfo->RecordThisInfo(thisVar, ThisType_Mapped);
  4920. return thisVar;
  4921. }
  4922. Var InterpreterStackFrame::OP_ProfiledStrictLdThis(Var thisVar, ScriptContext* scriptContext)
  4923. {
  4924. FunctionBody * functionBody = this->m_functionBody;
  4925. DynamicProfileInfo * dynamicProfileInfo = functionBody->GetDynamicProfileInfo();
  4926. TypeId typeId = JavascriptOperators::GetTypeId(thisVar);
  4927. if (typeId == TypeIds_ActivationObject)
  4928. {
  4929. thisVar = scriptContext->GetLibrary()->GetUndefined();
  4930. dynamicProfileInfo->RecordThisInfo(thisVar, ThisType_Mapped);
  4931. return thisVar;
  4932. }
  4933. dynamicProfileInfo->RecordThisInfo(thisVar, ThisType_Simple);
  4934. return thisVar;
  4935. }
  4936. #endif
  4937. void InterpreterStackFrame::OP_InitCachedFuncs(const unaligned OpLayoutAuxNoReg * playout)
  4938. {
  4939. const FuncInfoArray *info = Js::ByteCodeReader::ReadAuxArray<FuncInfoEntry>(playout->Offset, this->GetFunctionBody());
  4940. JavascriptOperators::OP_InitCachedFuncs(this->localClosure, GetLocalFrameDisplay(), info, GetScriptContext());
  4941. }
  4942. Var InterpreterStackFrame::OP_GetCachedFunc(Var instance, int32 index)
  4943. {
  4944. ActivationObjectEx *obj = VarTo<ActivationObjectEx>(instance);
  4945. FuncCacheEntry *entry = obj->GetFuncCacheEntry((uint)index);
  4946. return entry->func;
  4947. }
  4948. void InterpreterStackFrame::OP_CommitScope()
  4949. {
  4950. const Js::PropertyIdArray *propIds = this->m_functionBody->GetFormalsPropIdArray();
  4951. this->OP_CommitScopeHelper(propIds);
  4952. }
  4953. void InterpreterStackFrame::OP_CommitScopeHelper(const PropertyIdArray *propIds)
  4954. {
  4955. ActivationObjectEx *obj = VarTo<ActivationObjectEx>(this->localClosure);
  4956. ScriptFunction *func = obj->GetParentFunc();
  4957. Assert(obj->GetParentFunc() == func);
  4958. if (func->GetCachedScope() == obj)
  4959. {
  4960. PropertyId firstVarSlot = ActivationObjectEx::GetFirstVarSlot(propIds);
  4961. Var undef = scriptContext->GetLibrary()->GetUndefined();
  4962. for (uint i = firstVarSlot; i < propIds->count; i++)
  4963. {
  4964. obj->SetSlot(SetSlotArguments(propIds->elements[i], i, undef));
  4965. }
  4966. obj->SetCommit(true);
  4967. }
  4968. }
  4969. Var InterpreterStackFrame::OP_NewScObjectSimple()
  4970. {
  4971. Var object = scriptContext->GetLibrary()->CreateObject(true);
  4972. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(object));
  4973. #if ENABLE_DEBUG_CONFIG_OPTIONS
  4974. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  4975. {
  4976. object = JavascriptProxy::AutoProxyWrapper(object);
  4977. }
  4978. #endif
  4979. return object;
  4980. }
  4981. void InterpreterStackFrame::OP_NewScObjectLiteral(const unaligned OpLayoutAuxiliary * playout)
  4982. {
  4983. const Js::PropertyIdArray *propIds = Js::ByteCodeReader::ReadPropertyIdArray(playout->Offset, this->GetFunctionBody());
  4984. Var newObj = JavascriptOperators::NewScObjectLiteral(GetScriptContext(), propIds,
  4985. this->GetFunctionBody()->GetObjectLiteralTypeRef(playout->C1));
  4986. SetReg(playout->R0, newObj);
  4987. }
  4988. void InterpreterStackFrame::OP_NewScObjectLiteral_LS(const unaligned OpLayoutAuxiliary * playout, RegSlot& target)
  4989. {
  4990. const Js::PropertyIdArray *propIds = Js::ByteCodeReader::ReadPropertyIdArray(playout->Offset, this->GetFunctionBody());
  4991. target = playout->R0;
  4992. Var newObj = JavascriptOperators::NewScObjectLiteral(GetScriptContext(), propIds,
  4993. this->GetFunctionBody()->GetObjectLiteralTypeRef(playout->C1));
  4994. SetReg(playout->R0, newObj);
  4995. target = Js::Constants::NoRegister;
  4996. }
  4997. void InterpreterStackFrame::OP_LdPropIds(const unaligned OpLayoutAuxiliary * playout)
  4998. {
  4999. const Js::PropertyIdArray *propIds = Js::ByteCodeReader::ReadPropertyIdArray(playout->Offset, this->GetFunctionBody());
  5000. SetNonVarReg(playout->R0, (Var)propIds);
  5001. }
  5002. void InterpreterStackFrame::OP_StPropIdArrFromVar(Var instance, uint32 index, Var value, ScriptContext* scriptContext)
  5003. {
  5004. Js::PropertyIdArray * propIds = reinterpret_cast<Js::PropertyIdArray *>(instance);
  5005. AssertOrFailFast(index < propIds->count);
  5006. Js::PropertyId id = JavascriptOperators::GetPropertyId(value, scriptContext);
  5007. propIds->elements[index] = id;
  5008. }
  5009. Js::PropertyIdArray * InterpreterStackFrame::OP_NewPropIdArrForCompProps(uint32 size, ScriptContext* scriptContext)
  5010. {
  5011. uint extraAlloc = UInt32Math::Mul(size, sizeof(Js::PropertyId));
  5012. Js::PropertyIdArray * propIdArr = RecyclerNewPlusLeaf(scriptContext->GetRecycler(), extraAlloc, Js::PropertyIdArray, size, 0);
  5013. return propIdArr;
  5014. }
  5015. bool InterpreterStackFrame::IsCurrentLoopNativeAddr(void * codeAddr) const
  5016. {
  5017. if (this->GetCurrentLoopNum() == LoopHeader::NoLoop)
  5018. {
  5019. return false;
  5020. }
  5021. // TODO: Do more verification?
  5022. return true;
  5023. }
  5024. template <LayoutSize layoutSize, bool profiled>
  5025. const byte * InterpreterStackFrame::OP_ProfiledLoopBodyStart(const byte * ip)
  5026. {
  5027. uint32 loopId = m_reader.GetLayout<OpLayoutT_Unsigned1<LayoutSizePolicy<layoutSize>>>(ip)->C1;
  5028. return OP_ProfiledLoopBodyStart<layoutSize, profiled>(loopId);
  5029. }
  5030. #ifdef ASMJS_PLAT
  5031. template <LayoutSize layoutSize, bool profiled>
  5032. const byte * InterpreterStackFrame::OP_ProfiledWasmLoopBodyStart(const byte * ip)
  5033. {
  5034. uint32 loopId = m_reader.GetLayout<OpLayoutT_WasmLoopStart<LayoutSizePolicy<layoutSize>>>(ip)->loopId;
  5035. return OP_ProfiledLoopBodyStart<layoutSize, profiled>(loopId);
  5036. }
  5037. #endif
  5038. #if ENABLE_PROFILE_INFO
  5039. void InterpreterStackFrame::OP_RecordImplicitCall(uint loopNumber)
  5040. {
  5041. Assert(Js::DynamicProfileInfo::EnableImplicitCallFlags(GetFunctionBody()));
  5042. Assert(loopNumber < this->m_functionBody->GetLoopCount());
  5043. FunctionBody* functionBody = this->m_functionBody;
  5044. DynamicProfileInfo * dynamicProfileInfo = functionBody->GetDynamicProfileInfo();
  5045. ThreadContext * threadContext = scriptContext->GetThreadContext();
  5046. dynamicProfileInfo->RecordLoopImplicitCallFlags(functionBody, loopNumber, threadContext->GetImplicitCallFlags());
  5047. }
  5048. template <LayoutSize layoutSize, bool profiled>
  5049. const byte * InterpreterStackFrame::OP_ProfiledLoopStart(const byte * ip)
  5050. {
  5051. const uint32 C1 = m_reader.GetLayout<OpLayoutT_Unsigned1<LayoutSizePolicy<layoutSize>>>(ip)->C1;
  5052. if (!profiled && !isAutoProfiling)
  5053. {
  5054. return ip;
  5055. }
  5056. ThreadContext *const threadContext = GetScriptContext()->GetThreadContext();
  5057. threadContext->IncrementLoopDepth();
  5058. // Save the implicit call flags. The interpreter may switch to profiling mode during LoopBodyStart, so always do this.
  5059. Assert(Js::DynamicProfileInfo::EnableImplicitCallFlags(GetFunctionBody()));
  5060. this->savedLoopImplicitCallFlags[C1] = threadContext->GetImplicitCallFlags();
  5061. threadContext->SetImplicitCallFlags(ImplicitCall_None);
  5062. this->currentLoopCounter = 0;
  5063. if (!profiled)
  5064. {
  5065. return ip;
  5066. }
  5067. LayoutSize localLayoutSize;
  5068. OpCode peekOp = m_reader.PeekOp(ip, localLayoutSize);
  5069. Assert(peekOp != OpCode::LoopBodyStart);
  5070. if (peekOp == OpCode::ProfiledLoopBodyStart)
  5071. {
  5072. Assert(localLayoutSize == layoutSize);
  5073. ip += Js::OpCodeUtil::EncodedSize(peekOp, layoutSize);
  5074. // We are doing JIT loop body. Process the first ProfiledLoopBodyStart to avoid recording
  5075. // the implicit call before the first iteration
  5076. uint32 C2 = m_reader.GetLayout<OpLayoutT_Unsigned1<LayoutSizePolicy<layoutSize>>>(ip)->C1;
  5077. Assert(C1 == C2);
  5078. (this->*opProfiledLoopBodyStart)(C1, layoutSize, true /* isFirstIteration */);
  5079. return m_reader.GetIP();
  5080. }
  5081. return ip;
  5082. }
  5083. template <LayoutSize layoutSize, bool profiled>
  5084. const byte * InterpreterStackFrame::OP_ProfiledLoopEnd(const byte * ip)
  5085. {
  5086. uint32 loopNumber = m_reader.GetLayout<OpLayoutT_Unsigned1<LayoutSizePolicy<layoutSize>>>(ip)->C1;
  5087. if (!profiled && !isAutoProfiling)
  5088. {
  5089. return ip;
  5090. }
  5091. this->CheckIfLoopIsHot(this->currentLoopCounter);
  5092. Js::FunctionBody *fn = this->function->GetFunctionBody();
  5093. if (fn->HasDynamicProfileInfo())
  5094. {
  5095. fn->GetAnyDynamicProfileInfo()->SetLoopInterpreted(loopNumber);
  5096. // If the counter is 0, there is a high chance that some config disabled tracking that information. (ie: -off:jitloopbody)
  5097. // Assume it is valid for memop in this case.
  5098. if (this->currentLoopCounter >= (uint)CONFIG_FLAG(MinMemOpCount) ||
  5099. (this->currentLoopCounter == 0 && !this->m_functionBody->DoJITLoopBody())
  5100. )
  5101. {
  5102. // This flag becomes relevant only if the loop has been interpreted
  5103. fn->GetAnyDynamicProfileInfo()->SetMemOpMinReached(loopNumber);
  5104. }
  5105. }
  5106. this->currentLoopCounter = 0;
  5107. if (profiled)
  5108. {
  5109. Assert(Js::DynamicProfileInfo::EnableImplicitCallFlags(GetFunctionBody()));
  5110. OP_RecordImplicitCall(loopNumber);
  5111. if (switchProfileModeOnLoopEndNumber == loopNumber)
  5112. {
  5113. // Stop profiling since the jitted loop body would be exiting the loop
  5114. Assert(!switchProfileMode);
  5115. switchProfileMode = true;
  5116. switchProfileModeOnLoopEndNumber = 0u - 1;
  5117. }
  5118. }
  5119. // Restore the implicit call flags state and add with flags in the loop as well
  5120. ThreadContext *const threadContext = GetScriptContext()->GetThreadContext();
  5121. threadContext->AddImplicitCallFlags(this->savedLoopImplicitCallFlags[loopNumber]);
  5122. threadContext->DecrementLoopDepth();
  5123. return ip;
  5124. }
  5125. template <LayoutSize layoutSize, bool profiled>
  5126. const byte * InterpreterStackFrame::OP_ProfiledLoopBodyStart(uint loopId)
  5127. {
  5128. if (profiled || isAutoProfiling)
  5129. {
  5130. this->currentLoopCounter++;
  5131. }
  5132. if (profiled)
  5133. {
  5134. OP_RecordImplicitCall(loopId);
  5135. }
  5136. (this->*(profiled ? opProfiledLoopBodyStart : opLoopBodyStart))(loopId, layoutSize, false /* isFirstIteration */);
  5137. return m_reader.GetIP();
  5138. }
  5139. template<bool InterruptProbe, bool JITLoopBody>
  5140. void InterpreterStackFrame::ProfiledLoopBodyStart(uint32 loopNumber, LayoutSize layoutSize, bool isFirstIteration)
  5141. {
  5142. Assert(Js::DynamicProfileInfo::EnableImplicitCallFlags(GetFunctionBody()));
  5143. if (InterruptProbe)
  5144. {
  5145. this->DoInterruptProbe();
  5146. }
  5147. #if ENABLE_TTD
  5148. if (SHOULD_DO_TTD_STACK_STMT_OP(this->scriptContext))
  5149. {
  5150. this->scriptContext->GetThreadContext()->TTDExecutionInfo->UpdateLoopCountInfo();
  5151. }
  5152. #endif
  5153. if (!JITLoopBody || this->IsInCatchOrFinallyBlock())
  5154. {
  5155. // For functions having try-catch-finally, jit loop bodies for loops that are contained only in a try block,
  5156. // not even indirect containment in a Catch or Finally.
  5157. return;
  5158. }
  5159. LoopHeader const * loopHeader = DoLoopBodyStart(loopNumber, layoutSize, false, isFirstIteration);
  5160. Assert(loopHeader == nullptr || this->m_functionBody->GetLoopNumber(loopHeader) == loopNumber);
  5161. if (loopHeader != nullptr)
  5162. {
  5163. // We executed jitted loop body, no implicit call information available for this loop
  5164. uint currentOffset = m_reader.GetCurrentOffset();
  5165. if (!loopHeader->Contains(currentOffset) || (m_reader.PeekOp() == OpCode::ProfiledLoopEnd))
  5166. {
  5167. // Restore the outer loop's implicit call flags
  5168. scriptContext->GetThreadContext()->SetImplicitCallFlags(this->savedLoopImplicitCallFlags[loopNumber]);
  5169. }
  5170. else
  5171. {
  5172. // We bailout from the loop, just continue collect implicit call flags for this loop
  5173. }
  5174. }
  5175. }
  5176. #else
  5177. template <LayoutSize layoutSize, bool profiled>
  5178. const byte * InterpreterStackFrame::OP_ProfiledLoopStart(const byte * ip)
  5179. {
  5180. Assert(!profiled);
  5181. return ip;
  5182. }
  5183. template <LayoutSize layoutSize, bool profiled>
  5184. const byte * InterpreterStackFrame::OP_ProfiledLoopEnd(const byte * ip)
  5185. {
  5186. Assert(!profiled);
  5187. return ip;
  5188. }
  5189. template <LayoutSize layoutSize, bool profiled>
  5190. const byte * InterpreterStackFrame::OP_ProfiledLoopBodyStart(uint loopId)
  5191. {
  5192. Assert(!profiled);
  5193. (this->*opLoopBodyStart)(loopId, layoutSize, false /* isFirstIteration */);
  5194. return m_reader.GetIP();
  5195. }
  5196. #endif
  5197. template<bool InterruptProbe, bool JITLoopBody>
  5198. void InterpreterStackFrame::LoopBodyStart(uint32 loopNumber, LayoutSize layoutSize, bool isFirstIteration)
  5199. {
  5200. if (InterruptProbe)
  5201. {
  5202. this->DoInterruptProbe();
  5203. }
  5204. #if ENABLE_TTD
  5205. if (SHOULD_DO_TTD_STACK_STMT_OP(this->scriptContext))
  5206. {
  5207. this->scriptContext->GetThreadContext()->TTDExecutionInfo->UpdateLoopCountInfo();
  5208. }
  5209. #endif
  5210. if (!JITLoopBody || this->IsInCatchOrFinallyBlock())
  5211. {
  5212. // For functions having try-catch-finally, jit loop bodies for loops that are contained only in a try block,
  5213. // not even indirect containment in a Catch or Finally.
  5214. return;
  5215. }
  5216. DoLoopBodyStart(loopNumber, layoutSize, true, isFirstIteration);
  5217. }
  5218. LoopHeader const * InterpreterStackFrame::DoLoopBodyStart(uint32 loopNumber, LayoutSize layoutSize, const bool doProfileLoopCheck, const bool isFirstIteration)
  5219. {
  5220. #if ENABLE_PROFILE_INFO
  5221. class AutoRestoreLoopNumbers
  5222. {
  5223. private:
  5224. InterpreterStackFrame * const interpreterStackFrame;
  5225. uint32 loopNumber;
  5226. bool doProfileLoopCheck;
  5227. public:
  5228. AutoRestoreLoopNumbers(InterpreterStackFrame *const interpreterStackFrame, uint32 loopNumber, bool doProfileLoopCheck)
  5229. : interpreterStackFrame(interpreterStackFrame), loopNumber(loopNumber), doProfileLoopCheck(doProfileLoopCheck)
  5230. {
  5231. Assert(interpreterStackFrame->currentLoopNum == LoopHeader::NoLoop);
  5232. interpreterStackFrame->currentLoopNum = loopNumber;
  5233. interpreterStackFrame->m_functionBody->SetRecentlyBailedOutOfJittedLoopBody(false);
  5234. }
  5235. ~AutoRestoreLoopNumbers()
  5236. {
  5237. interpreterStackFrame->currentLoopNum = LoopHeader::NoLoop;
  5238. interpreterStackFrame->currentLoopCounter = 0;
  5239. Js::FunctionBody* fn = interpreterStackFrame->m_functionBody;
  5240. if (fn->RecentlyBailedOutOfJittedLoopBody())
  5241. {
  5242. if (doProfileLoopCheck && interpreterStackFrame->isAutoProfiling)
  5243. {
  5244. // Start profiling the loop after a bailout. Some bailouts require subsequent profile data collection such
  5245. // that the rejitted loop body would not bail out again for the same reason.
  5246. Assert(!interpreterStackFrame->switchProfileMode);
  5247. interpreterStackFrame->switchProfileMode = true;
  5248. Assert(interpreterStackFrame->switchProfileModeOnLoopEndNumber == 0u - 1);
  5249. interpreterStackFrame->switchProfileModeOnLoopEndNumber = loopNumber;
  5250. }
  5251. }
  5252. else
  5253. {
  5254. if (interpreterStackFrame->switchProfileModeOnLoopEndNumber == loopNumber)
  5255. {
  5256. // Stop profiling since the jitted loop body would be exiting the loop
  5257. Assert(!interpreterStackFrame->switchProfileMode);
  5258. interpreterStackFrame->switchProfileMode = true;
  5259. interpreterStackFrame->switchProfileModeOnLoopEndNumber = 0u - 1;
  5260. }
  5261. interpreterStackFrame->scriptContext->GetThreadContext()->DecrementLoopDepth();
  5262. }
  5263. }
  5264. };
  5265. #endif
  5266. Js::FunctionBody* fn = this->m_functionBody;
  5267. Assert(loopNumber < fn->GetLoopCount());
  5268. Assert(!this->IsInCatchOrFinallyBlock());
  5269. Js::LoopHeader *loopHeader = fn->GetLoopHeader(loopNumber);
  5270. loopHeader->isInTry = this->TestFlags(Js::InterpreterStackFrameFlags_WithinTryBlock);
  5271. loopHeader->isInTryFinally = this->TestFlags(Js::InterpreterStackFrameFlags_WithinTryFinallyBlock);
  5272. Js::LoopEntryPointInfo * entryPointInfo = loopHeader->GetCurrentEntryPointInfo();
  5273. if (fn->ForceJITLoopBody() && loopHeader->interpretCount == 0 &&
  5274. (entryPointInfo != NULL && entryPointInfo->IsNotScheduled()))
  5275. {
  5276. #if ENABLE_PROFILE_INFO
  5277. if (Js::DynamicProfileInfo::EnableImplicitCallFlags(GetFunctionBody()))
  5278. {
  5279. scriptContext->GetThreadContext()->AddImplicitCallFlags(this->savedLoopImplicitCallFlags[loopNumber]);
  5280. }
  5281. #endif
  5282. #if ENABLE_NATIVE_CODEGEN
  5283. GenerateLoopBody(scriptContext->GetNativeCodeGenerator(), fn, loopHeader, entryPointInfo, fn->GetLocalsCount(), this->m_localSlots);
  5284. #endif
  5285. }
  5286. #if ENABLE_NATIVE_CODEGEN
  5287. // If we have JITted the loop, call the JITted code
  5288. if (entryPointInfo != NULL && entryPointInfo->IsCodeGenDone())
  5289. {
  5290. #if DBG_DUMP
  5291. if (PHASE_TRACE1(Js::JITLoopBodyPhase) && CONFIG_FLAG(Verbose))
  5292. {
  5293. fn->DumpFunctionId(true);
  5294. Output::Print(_u(": %-20s LoopBody Execute Loop: %2d\n"), fn->GetDisplayName(), loopNumber);
  5295. Output::Flush();
  5296. }
  5297. loopHeader->nativeCount++;
  5298. #endif
  5299. #ifdef BGJIT_STATS
  5300. entryPointInfo->MarkAsUsed();
  5301. #endif
  5302. entryPointInfo->EnsureIsReadyToCall();
  5303. entryPointInfo->SetNativeEntryPointProcessed();
  5304. RegSlot envReg = this->m_functionBody->GetEnvRegister();
  5305. if (envReg != Constants::NoRegister)
  5306. {
  5307. this->SetNonVarReg(envReg, this->LdEnv());
  5308. }
  5309. RegSlot localClosureReg = this->m_functionBody->GetLocalClosureRegister();
  5310. RegSlot localFrameDisplayReg = this->m_functionBody->GetLocalFrameDisplayRegister();
  5311. RegSlot paramClosureReg = this->m_functionBody->GetParamClosureRegister();
  5312. if (entryPointInfo->HasJittedStackClosure())
  5313. {
  5314. // The jitted code is expecting the closure registers to point to known stack locations where
  5315. // the closures can be found and possibly boxed.
  5316. // In a jitted loop body, those locations are the local closure fields on the interpreter instance.
  5317. if (localClosureReg != Constants::NoRegister)
  5318. {
  5319. this->SetNonVarReg(localClosureReg, &this->localClosure);
  5320. }
  5321. if (localFrameDisplayReg != Constants::NoRegister)
  5322. {
  5323. this->SetNonVarReg(localFrameDisplayReg, &this->localFrameDisplay);
  5324. }
  5325. if (paramClosureReg != Constants::NoRegister)
  5326. {
  5327. this->SetNonVarReg(paramClosureReg, &this->paramClosure);
  5328. }
  5329. }
  5330. else
  5331. {
  5332. // In non-stack-closure jitted code, the closure registers are expected to hold the addresses
  5333. // of the actual structures.
  5334. if (localClosureReg != Constants::NoRegister)
  5335. {
  5336. this->SetNonVarReg(localClosureReg, this->localClosure);
  5337. }
  5338. if (localFrameDisplayReg != Constants::NoRegister)
  5339. {
  5340. this->SetNonVarReg(localFrameDisplayReg, this->localFrameDisplay);
  5341. }
  5342. if (paramClosureReg != Constants::NoRegister)
  5343. {
  5344. this->SetNonVarReg(paramClosureReg, this->paramClosure);
  5345. }
  5346. }
  5347. uint32 innerScopeCount = this->m_functionBody->GetInnerScopeCount();
  5348. for (uint32 i = 0; i < innerScopeCount; i++)
  5349. {
  5350. // As with the function-level scope, transfer the inner scopes from the interpreter's side storage
  5351. // to their dedicated register slots.
  5352. SetNonVarReg(this->m_functionBody->GetFirstInnerScopeRegister() + i, InnerScopeFromIndex(i));
  5353. }
  5354. uint newOffset = 0;
  5355. if (fn->GetIsAsmJsFunction())
  5356. {
  5357. AutoRestoreLoopNumbers autoRestore(this, loopNumber, doProfileLoopCheck);
  5358. newOffset = this->CallAsmJsLoopBody(entryPointInfo->jsMethod);
  5359. }
  5360. else
  5361. {
  5362. AutoRestoreLoopNumbers autoRestore(this, loopNumber, doProfileLoopCheck);
  5363. newOffset = this->CallLoopBody(entryPointInfo->jsMethod);
  5364. }
  5365. if (envReg != Constants::NoRegister)
  5366. {
  5367. SetNonVarReg(envReg, nullptr);
  5368. }
  5369. if (localClosureReg != Constants::NoRegister)
  5370. {
  5371. SetNonVarReg(localClosureReg, nullptr);
  5372. }
  5373. if (localFrameDisplayReg != Constants::NoRegister)
  5374. {
  5375. SetNonVarReg(localFrameDisplayReg, nullptr);
  5376. }
  5377. if (paramClosureReg != Constants::NoRegister)
  5378. {
  5379. SetNonVarReg(paramClosureReg, nullptr);
  5380. }
  5381. for (uint32 i = 0; i < innerScopeCount; i++)
  5382. {
  5383. // Get the (possibly updated) scopes from their registers and put them back in side storage.
  5384. // (Getting the updated values may not be necessary, actually, but it can't hurt.)
  5385. // Then null out the registers.
  5386. RegSlot reg = this->m_functionBody->GetFirstInnerScopeRegister() + i;
  5387. SetInnerScopeFromIndex(i, GetNonVarReg(reg));
  5388. SetNonVarReg(reg, nullptr);
  5389. }
  5390. Assert(Js::OpCodeUtil::GetOpCodeLayout(OpCode::ProfiledLoopBodyStart) == Js::OpLayoutType::Unsigned1);
  5391. Assert(Js::OpCodeUtil::GetOpCodeLayout(OpCode::LoopBodyStart) == Js::OpLayoutType::Unsigned1);
  5392. Assert(Js::OpCodeUtil::EncodedSize(Js::OpCode::LoopBodyStart, layoutSize) == Js::OpCodeUtil::EncodedSize(Js::OpCode::ProfiledLoopBodyStart, layoutSize));
  5393. uint byteCodeSize = Js::OpCodeUtil::EncodedSize(Js::OpCode::LoopBodyStart, layoutSize);
  5394. if (layoutSize == SmallLayout)
  5395. {
  5396. byteCodeSize += sizeof(OpLayoutUnsigned1_Small);
  5397. }
  5398. else if (layoutSize == MediumLayout)
  5399. {
  5400. byteCodeSize += sizeof(OpLayoutUnsigned1_Medium);
  5401. }
  5402. else
  5403. {
  5404. byteCodeSize += sizeof(OpLayoutUnsigned1_Large);
  5405. }
  5406. if (newOffset == loopHeader->startOffset || newOffset == m_reader.GetCurrentOffset() - byteCodeSize)
  5407. {
  5408. // If we bail out back the start of the loop, or start of this LoopBodyStart just skip and interpret the loop
  5409. // instead of trying to start the loop body again
  5410. // Increment the interpret count of the loop
  5411. loopHeader->interpretCount++;
  5412. }
  5413. else
  5414. {
  5415. this->CheckIfLoopIsHot(loopHeader->profiledLoopCounter);
  5416. if (newOffset >= loopHeader->endOffset)
  5417. {
  5418. // Reset the totalJittedLoopIterations for the next invocation of this loop entry point
  5419. entryPointInfo->totalJittedLoopIterations =
  5420. static_cast<uint8>(
  5421. min(
  5422. static_cast<uint>(static_cast<uint8>(CONFIG_FLAG(MinBailOutsBeforeRejit))) *
  5423. (Js::LoopEntryPointInfo::GetDecrLoopCountPerBailout() - 1),
  5424. entryPointInfo->totalJittedLoopIterations));
  5425. entryPointInfo->jittedLoopIterationsSinceLastBailout = 0;
  5426. }
  5427. m_reader.SetCurrentOffset(newOffset);
  5428. }
  5429. return loopHeader;
  5430. }
  5431. #endif
  5432. // Increment the interpret count of the loop
  5433. loopHeader->interpretCount += !isFirstIteration;
  5434. const uint loopInterpretCount = GetFunctionBody()->GetLoopInterpretCount(loopHeader);
  5435. if (loopHeader->interpretCount > loopInterpretCount)
  5436. {
  5437. if (this->scriptContext->GetConfig()->IsNoNative())
  5438. {
  5439. return nullptr;
  5440. }
  5441. if (!fn->DoJITLoopBody())
  5442. {
  5443. return nullptr;
  5444. }
  5445. #if ENABLE_NATIVE_CODEGEN
  5446. #if ENABLE_OOP_NATIVE_CODEGEN
  5447. // If for some reason OOP JIT isn't connected (e.g. it crashed), don't attempt to JIT a loop body
  5448. if (JITManager::GetJITManager()->IsOOPJITEnabled() && !JITManager::GetJITManager()->IsConnected())
  5449. {
  5450. return nullptr;
  5451. }
  5452. #endif
  5453. // If the job is not scheduled then we need to schedule it now.
  5454. // It is possible a job was scheduled earlier and we find ourselves looking at the same entry point
  5455. // again. For example, if the function with the loop was JITed and bailed out then as we finish
  5456. // the call in the interpreter we might encounter a loop for which we had scheduled a JIT job before
  5457. // the function was initially scheduled. In such cases, that old JIT job will complete. If it completes
  5458. // successfully then we can go ahead and use it. If it fails then it will eventually revert to the
  5459. // NotScheduled state. Since transitions from NotScheduled can only occur on the main thread,
  5460. // by checking the state we are safe from racing with the JIT thread when looking at the other fields
  5461. // of the entry point.
  5462. if (entryPointInfo != NULL && entryPointInfo->IsNotScheduled())
  5463. {
  5464. GenerateLoopBody(scriptContext->GetNativeCodeGenerator(), fn, loopHeader, entryPointInfo, fn->GetLocalsCount(), this->m_localSlots);
  5465. }
  5466. #endif
  5467. }
  5468. #if ENABLE_PROFILE_INFO
  5469. else if (
  5470. doProfileLoopCheck &&
  5471. isAutoProfiling &&
  5472. loopHeader->interpretCount > fn->GetLoopProfileThreshold(loopInterpretCount))
  5473. {
  5474. // Start profiling the loop so that the jitted loop body will have some profile data to use
  5475. Assert(!switchProfileMode);
  5476. switchProfileMode = true;
  5477. Assert(switchProfileModeOnLoopEndNumber == 0u - 1);
  5478. switchProfileModeOnLoopEndNumber = loopNumber;
  5479. }
  5480. #endif
  5481. return nullptr;
  5482. }
  5483. void
  5484. InterpreterStackFrame::CheckIfLoopIsHot(uint profiledLoopCounter)
  5485. {
  5486. Js::FunctionBody *fn = this->function->GetFunctionBody();
  5487. if (!fn->GetHasHotLoop() && profiledLoopCounter > (uint)CONFIG_FLAG(JitLoopBodyHotLoopThreshold))
  5488. {
  5489. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  5490. if (PHASE_TRACE(Js::JITLoopBodyPhase, fn))
  5491. {
  5492. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  5493. Output::Print(
  5494. _u("Speculate Jit set for this function with loopbody: function: %s (%s)\n"),
  5495. fn->GetDisplayName(),
  5496. fn->GetDebugNumberSet(debugStringBuffer));
  5497. Output::Flush();
  5498. }
  5499. #endif
  5500. fn->SetHasHotLoop();
  5501. }
  5502. }
  5503. bool InterpreterStackFrame::CheckAndResetImplicitCall(DisableImplicitFlags prevDisableImplicitFlags, ImplicitCallFlags savedImplicitCallFlags)
  5504. {
  5505. ImplicitCallFlags curImplicitCallFlags = this->scriptContext->GetThreadContext()->GetImplicitCallFlags();
  5506. if (curImplicitCallFlags > ImplicitCall_None)
  5507. {
  5508. //error implicit bit is set , reparse without asmjs
  5509. this->scriptContext->GetThreadContext()->SetDisableImplicitFlags(prevDisableImplicitFlags);
  5510. this->scriptContext->GetThreadContext()->SetImplicitCallFlags(savedImplicitCallFlags);
  5511. return true;
  5512. }
  5513. return false;
  5514. }
  5515. uint
  5516. InterpreterStackFrame::CallLoopBody(JavascriptMethod address)
  5517. {
  5518. #ifdef _M_IX86
  5519. void *savedEsp = NULL;
  5520. __asm
  5521. {
  5522. // Save ESP
  5523. mov savedEsp, esp
  5524. // 8-byte align frame to improve floating point perf of our JIT'd code.
  5525. and esp, -8
  5526. // Add an extra 4-bytes to the stack since we'll be pushing 3 arguments
  5527. push eax
  5528. }
  5529. #endif
  5530. #if defined(_M_ARM32_OR_ARM64)
  5531. // For ARM we need to make sure that pipeline is synchronized with memory/cache for newly jitted code.
  5532. // Note: this does not seem to affect perf, but if it was, we could add a boolean isCalled to EntryPointInfo
  5533. // and do ISB only for 1st time this entry point is called (potential working set regression though).
  5534. _InstructionSynchronizationBarrier();
  5535. #endif
  5536. uint newOffset = ::Math::PointerCastToIntegral<uint>(
  5537. CALL_ENTRYPOINT_NOASSERT(address, function, CallInfo(CallFlags_InternalFrame, 1), this));
  5538. #ifdef _M_IX86
  5539. _asm
  5540. {
  5541. // Restore ESP
  5542. mov esp, savedEsp
  5543. }
  5544. #endif
  5545. return newOffset;
  5546. }
  5547. uint
  5548. InterpreterStackFrame::CallAsmJsLoopBody(JavascriptMethod address)
  5549. {
  5550. #ifdef _M_IX86
  5551. void *savedEsp = NULL;
  5552. __asm
  5553. {
  5554. // Save ESP
  5555. mov savedEsp, esp
  5556. // Add an extra 4-bytes to the stack since we'll be pushing 3 arguments
  5557. push eax
  5558. }
  5559. #endif
  5560. #if defined(_M_ARM32_OR_ARM64)
  5561. // For ARM we need to make sure that pipeline is synchronized with memory/cache for newly jitted code.
  5562. // Note: this does not seem to affect perf, but if it was, we could add a boolean isCalled to EntryPointInfo
  5563. // and do ISB only for 1st time this entry point is called (potential working set regression though).
  5564. _InstructionSynchronizationBarrier();
  5565. #endif
  5566. uint newOffset = ::Math::PointerCastToIntegral<uint>(
  5567. CALL_ENTRYPOINT_NOASSERT(address, function, CallInfo(CallFlags_InternalFrame, 1), this));
  5568. #ifdef _M_IX86
  5569. _asm
  5570. {
  5571. // Restore ESP
  5572. mov esp, savedEsp
  5573. }
  5574. #endif
  5575. return newOffset;
  5576. }
  5577. template <class T>
  5578. void InterpreterStackFrame::OP_NewScObjectNoCtorFull(const unaligned T* playout)
  5579. {
  5580. Var function = GetReg(playout->R1);
  5581. Var newObj = JavascriptOperators::NewScObjectNoCtorFull(function, GetScriptContext());
  5582. SetReg(playout->R0, newObj);
  5583. }
  5584. ///----------------------------------------------------------------------------
  5585. ///
  5586. /// InterpreterStackFrame::OP_NewScObject
  5587. ///
  5588. /// OP_NewScObject() allocates a new DynamicObject and initializes it with an
  5589. /// optional "constructor" function.
  5590. ///
  5591. /// NOTE: The return register must be carefully chosen to ensure proper
  5592. /// behavior:
  5593. /// 1. OpCode::NewInstance should never specify "R0" as the register to
  5594. /// store the new instance, because it will get whacked from the
  5595. /// "constructor" function's return value:
  5596. ///
  5597. /// var a1 = Date(); <-- a1 = string returned from Date() function
  5598. /// var a2 = new Date(); <-- a2 = instance return from NewInstance.
  5599. /// Date()'s return value is thrown away.
  5600. ///
  5601. /// 2. If an exception is thrown during construction, the destination
  5602. /// variable / field should __not__ be modified. Therefore, the destination
  5603. /// register should always be a temporary and never a valid local variable.
  5604. /// After successfully returning from the constructor function, the new
  5605. /// instance is valid and may be stored in its final destination variable /
  5606. /// field.
  5607. ///
  5608. /// OPCODE NewObject:
  5609. /// T1 = new DynamicObject(Function.Prototype)
  5610. /// OutArg[0] = T1
  5611. /// Call(Function, ArgCount)
  5612. /// Local[Return] = T1
  5613. ///
  5614. /// - R0: Destination "local" register
  5615. /// - R1: Optional constructor JavascriptFunction instance or 'null'
  5616. ///
  5617. ///----------------------------------------------------------------------------
  5618. template <class T, bool Profiled, bool ICIndex>
  5619. void InterpreterStackFrame::OP_NewScObject_Impl(const unaligned T* playout, InlineCacheIndex inlineCacheIndex, const Js::AuxArray<uint32> *spreadIndices)
  5620. {
  5621. if (ICIndex)
  5622. {
  5623. Assert(inlineCacheIndex != Js::Constants::NoInlineCacheIndex);
  5624. }
  5625. Var newVarInstance =
  5626. #if ENABLE_PROFILE_INFO
  5627. Profiled ?
  5628. ProfiledNewScObject_Helper(
  5629. GetReg(playout->Function),
  5630. playout->ArgCount,
  5631. static_cast<const unaligned OpLayoutDynamicProfile<T> *>(playout)->profileId,
  5632. inlineCacheIndex,
  5633. spreadIndices) :
  5634. #endif
  5635. NewScObject_Helper(GetReg(playout->Function), playout->ArgCount, spreadIndices);
  5636. SetReg((RegSlot)playout->Return, newVarInstance);
  5637. }
  5638. template <class T, bool Profiled>
  5639. void InterpreterStackFrame::OP_ProfiledNewScObjArray_Impl(const unaligned T* playout, const Js::AuxArray<uint32> *spreadIndices)
  5640. {
  5641. // Always profile this operation when auto-profiling so that array type changes are tracked
  5642. #if ENABLE_PROFILE_INFO
  5643. if (!Profiled && !isAutoProfiling)
  5644. #else
  5645. Assert(!Profiled);
  5646. #endif
  5647. {
  5648. OP_NewScObjArray_Impl<T, Profiled>(playout, spreadIndices);
  5649. return;
  5650. }
  5651. #if ENABLE_PROFILE_INFO
  5652. Arguments args(CallInfo(CallFlags_New, playout->ArgCount), m_outParams);
  5653. uint32 spreadSize = 0;
  5654. if (spreadIndices != nullptr)
  5655. {
  5656. spreadSize = JavascriptFunction::GetSpreadSize(args, spreadIndices, scriptContext);
  5657. // Allocate room on the stack for the spread args.
  5658. Arguments outArgs(CallInfo(CallFlags_New, 0), nullptr);
  5659. outArgs.Info.Count = spreadSize;
  5660. const unsigned STACK_ARGS_ALLOCA_THRESHOLD = 8; // Number of stack args we allow before using _alloca
  5661. Var stackArgs[STACK_ARGS_ALLOCA_THRESHOLD];
  5662. size_t outArgsSize = 0;
  5663. if (outArgs.Info.Count > STACK_ARGS_ALLOCA_THRESHOLD)
  5664. {
  5665. PROBE_STACK(scriptContext, outArgs.Info.Count * sizeof(Var) + Js::Constants::MinStackDefault); // args + function call
  5666. outArgsSize = outArgs.Info.Count * sizeof(Var);
  5667. outArgs.Values = (Var*)_alloca(outArgsSize);
  5668. ZeroMemory(outArgs.Values, outArgsSize);
  5669. }
  5670. else
  5671. {
  5672. outArgs.Values = stackArgs;
  5673. outArgsSize = STACK_ARGS_ALLOCA_THRESHOLD * sizeof(Var);
  5674. ZeroMemory(outArgs.Values, outArgsSize); // We may not use all of the elements
  5675. }
  5676. JavascriptFunction::SpreadArgs(args, outArgs, spreadIndices, scriptContext);
  5677. SetReg(
  5678. (RegSlot)playout->Return,
  5679. ProfilingHelpers::ProfiledNewScObjArray(
  5680. GetReg(playout->Function),
  5681. outArgs,
  5682. function,
  5683. static_cast<const unaligned OpLayoutDynamicProfile2<T> *>(playout)->profileId,
  5684. static_cast<const unaligned OpLayoutDynamicProfile2<T> *>(playout)->profileId2));
  5685. }
  5686. else
  5687. {
  5688. SetReg(
  5689. (RegSlot)playout->Return,
  5690. ProfilingHelpers::ProfiledNewScObjArray(
  5691. GetReg(playout->Function),
  5692. args,
  5693. function,
  5694. static_cast<const unaligned OpLayoutDynamicProfile2<T> *>(playout)->profileId,
  5695. static_cast<const unaligned OpLayoutDynamicProfile2<T> *>(playout)->profileId2));
  5696. }
  5697. PopOut(playout->ArgCount);
  5698. #endif
  5699. }
  5700. void InterpreterStackFrame::OP_NewScObject_A_Impl(const unaligned OpLayoutAuxiliary * playout, RegSlot *target)
  5701. {
  5702. const Js::VarArrayVarCount * vars = Js::ByteCodeReader::ReadVarArrayVarCount(playout->Offset, this->GetFunctionBody());
  5703. int count = Js::TaggedInt::ToInt32(vars->count);
  5704. // Push the parameters to stack
  5705. for (int i = 0; i < count; i++)
  5706. {
  5707. SetOut((ArgSlot)(i + 1), vars->elements[i]);
  5708. }
  5709. Var newVarInstance = NewScObject_Helper(GetReg((RegSlot)playout->C1), (ArgSlot)count + 1);
  5710. SetReg((RegSlot)playout->R0, newVarInstance);
  5711. }
  5712. Var InterpreterStackFrame::NewScObject_Helper(Var target, ArgSlot ArgCount, const Js::AuxArray<uint32> *spreadIndices)
  5713. {
  5714. Arguments args(CallInfo(CallFlags_New, ArgCount), m_outParams);
  5715. Var newVarInstance = nullptr;
  5716. BEGIN_SAFE_REENTRANT_CALL(this->scriptContext->GetThreadContext())
  5717. {
  5718. newVarInstance = JavascriptOperators::NewScObject(target, args, GetScriptContext(), spreadIndices);
  5719. }
  5720. END_SAFE_REENTRANT_CALL
  5721. PopOut(ArgCount);
  5722. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(newVarInstance));
  5723. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5724. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5725. {
  5726. newVarInstance = JavascriptProxy::AutoProxyWrapper(newVarInstance);
  5727. // this might come from a different scriptcontext.
  5728. newVarInstance = CrossSite::MarshalVar(GetScriptContext(), newVarInstance);
  5729. }
  5730. #endif
  5731. #ifdef ENABLE_BASIC_TELEMETRY
  5732. {
  5733. this->scriptContext->GetTelemetry().GetOpcodeTelemetry().NewScriptObject(target, args, newVarInstance);
  5734. }
  5735. #endif
  5736. return newVarInstance;
  5737. }
  5738. #if ENABLE_PROFILE_INFO
  5739. Var InterpreterStackFrame::ProfiledNewScObject_Helper(Var target, ArgSlot ArgCount, ProfileId profileId, InlineCacheIndex inlineCacheIndex, const Js::AuxArray<uint32> *spreadIndices)
  5740. {
  5741. Arguments args(CallInfo(CallFlags_New, ArgCount), m_outParams);
  5742. Var newVarInstance = nullptr;
  5743. BEGIN_SAFE_REENTRANT_CALL(this->scriptContext->GetThreadContext())
  5744. {
  5745. newVarInstance = ProfilingHelpers::ProfiledNewScObject(
  5746. target,
  5747. args,
  5748. GetFunctionBody(),
  5749. profileId,
  5750. inlineCacheIndex,
  5751. spreadIndices);
  5752. }
  5753. END_SAFE_REENTRANT_CALL
  5754. PopOut(ArgCount);
  5755. JS_ETW(EventWriteJSCRIPT_RECYCLER_ALLOCATE_OBJECT(newVarInstance));
  5756. #if ENABLE_DEBUG_CONFIG_OPTIONS
  5757. if (Js::Configuration::Global.flags.IsEnabled(Js::autoProxyFlag))
  5758. {
  5759. newVarInstance = JavascriptProxy::AutoProxyWrapper(newVarInstance);
  5760. // this might come from a different scriptcontext.
  5761. newVarInstance = CrossSite::MarshalVar(GetScriptContext(), newVarInstance);
  5762. }
  5763. #endif
  5764. #ifdef TELEMETRY_PROFILED
  5765. {
  5766. this->scriptContext->GetTelemetry().GetOpcodeTelemetry().NewScriptObject(target, args, newVarInstance);
  5767. }
  5768. #endif
  5769. return newVarInstance;
  5770. }
  5771. #endif
  5772. template <typename T>
  5773. void InterpreterStackFrame::OP_LdElementUndefined(const unaligned OpLayoutT_ElementU<T>* playout)
  5774. {
  5775. if (this->m_functionBody->IsEval())
  5776. {
  5777. JavascriptOperators::OP_LoadUndefinedToElementDynamic(GetReg(playout->Instance),
  5778. this->m_functionBody->GetReferencedPropertyId(playout->PropertyIdIndex), GetScriptContext());
  5779. }
  5780. else
  5781. {
  5782. JavascriptOperators::OP_LoadUndefinedToElement(GetReg(playout->Instance),
  5783. this->m_functionBody->GetReferencedPropertyId(playout->PropertyIdIndex));
  5784. }
  5785. }
  5786. template <typename T>
  5787. void InterpreterStackFrame::OP_LdLocalElementUndefined(const unaligned OpLayoutT_ElementRootU<T>* playout)
  5788. {
  5789. if (this->m_functionBody->IsEval())
  5790. {
  5791. JavascriptOperators::OP_LoadUndefinedToElementDynamic(this->localClosure,
  5792. this->m_functionBody->GetReferencedPropertyId(playout->PropertyIdIndex), GetScriptContext());
  5793. }
  5794. else
  5795. {
  5796. JavascriptOperators::OP_LoadUndefinedToElement(this->localClosure,
  5797. this->m_functionBody->GetReferencedPropertyId(playout->PropertyIdIndex));
  5798. }
  5799. }
  5800. template <typename T>
  5801. void InterpreterStackFrame::OP_LdElementUndefinedScoped(const unaligned OpLayoutT_ElementScopedU<T>* playout)
  5802. {
  5803. // Implicit root object as default instance
  5804. JavascriptOperators::OP_LoadUndefinedToElementScoped(GetEnvForEvalCode(),
  5805. this->m_functionBody->GetReferencedPropertyId(playout->PropertyIdIndex), GetReg(Js::FunctionBody::RootObjectRegSlot), GetScriptContext());
  5806. }
  5807. void InterpreterStackFrame::OP_ChkUndecl(Var aValue)
  5808. {
  5809. if (this->scriptContext->IsUndeclBlockVar(aValue))
  5810. {
  5811. JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
  5812. }
  5813. }
  5814. void InterpreterStackFrame::OP_ChkNewCallFlag()
  5815. {
  5816. if (!(this->m_callFlags & CallFlags_New) && !this->TestFlags(InterpreterStackFrameFlags_FromBailOutInInlinee))
  5817. {
  5818. JavascriptError::ThrowTypeError(scriptContext, JSERR_ClassConstructorCannotBeCalledWithoutNew);
  5819. }
  5820. }
  5821. void InterpreterStackFrame::OP_EnsureNoRootProperty(uint propertyIdIndex)
  5822. {
  5823. Var instance = this->GetRootObject();
  5824. JavascriptOperators::OP_EnsureNoRootProperty(instance, this->m_functionBody->GetReferencedPropertyId(propertyIdIndex));
  5825. }
  5826. void InterpreterStackFrame::OP_EnsureNoRootRedeclProperty(uint propertyIdIndex)
  5827. {
  5828. Var instance = this->GetRootObject();
  5829. JavascriptOperators::OP_EnsureNoRootRedeclProperty(instance, this->m_functionBody->GetReferencedPropertyId(propertyIdIndex));
  5830. }
  5831. void InterpreterStackFrame::OP_EnsureCanDeclGloFunc(uint propertyIdIndex)
  5832. {
  5833. Var instance = this->GetRootObject();
  5834. JavascriptOperators::OP_EnsureCanDeclGloFunc(instance, this->m_functionBody->GetReferencedPropertyId(propertyIdIndex));
  5835. }
  5836. void InterpreterStackFrame::OP_ScopedEnsureNoRedeclProperty(Var aValue, uint propertyIdIndex, Var aValue2)
  5837. {
  5838. Js::PropertyId propertyId = this->m_functionBody->GetReferencedPropertyId(propertyIdIndex);
  5839. JavascriptOperators::OP_ScopedEnsureNoRedeclProperty((FrameDisplay*)aValue, propertyId, aValue2);
  5840. }
  5841. Var InterpreterStackFrame::OP_InitUndecl()
  5842. {
  5843. return this->scriptContext->GetLibrary()->GetUndeclBlockVar();
  5844. }
  5845. void InterpreterStackFrame::OP_InitUndeclSlot(Var aValue, int32 slot)
  5846. {
  5847. this->OP_StSlot(aValue, slot, this->scriptContext->GetLibrary()->GetUndeclBlockVar());
  5848. }
  5849. void InterpreterStackFrame::OP_TryCatch(const unaligned OpLayoutBr* playout)
  5850. {
  5851. Js::JavascriptExceptionObject* exception = NULL;
  5852. try
  5853. {
  5854. this->nestedTryDepth++;
  5855. // mark the stackFrame as 'in try block'
  5856. this->OrFlags(InterpreterStackFrameFlags_WithinTryBlock);
  5857. Js::JavascriptExceptionOperators::AutoCatchHandlerExists autoCatchHandlerExists(scriptContext);
  5858. void * addrOfReturnAddr = _AddressOfReturnAddress();
  5859. Js::JavascriptExceptionOperators::TryHandlerAddrOfReturnAddrStack tryHandlerAddrOfReturnAddrStack(scriptContext, addrOfReturnAddr);
  5860. #ifdef ENABLE_SCRIPT_DEBUGGING
  5861. if (this->IsInDebugMode())
  5862. {
  5863. #if ENABLE_TTD
  5864. if (SHOULD_DO_TTD_STACK_STMT_OP(this->scriptContext))
  5865. {
  5866. this->ProcessWithDebugging_PreviousStmtTracking();
  5867. }
  5868. else
  5869. {
  5870. this->ProcessWithDebugging();
  5871. }
  5872. #else
  5873. this->ProcessWithDebugging();
  5874. #endif
  5875. this->TrySetRetOffset();
  5876. }
  5877. else
  5878. #endif
  5879. {
  5880. this->Process();
  5881. this->TrySetRetOffset();
  5882. }
  5883. }
  5884. catch (const Js::JavascriptException& err)
  5885. {
  5886. // We are using C++ exception handling which does not unwind the stack in the catch block.
  5887. // For stack overflow and OOM exceptions, we cannot run user code here because the stack is not unwind.
  5888. exception = err.GetAndClear();
  5889. }
  5890. if (--this->nestedTryDepth == -1)
  5891. {
  5892. // unmark the stackFrame as 'in try block'
  5893. this->ClearFlags(InterpreterStackFrameFlags_WithinTryBlock);
  5894. }
  5895. // Now that the stack is unwound, let's run the catch block.
  5896. if (exception)
  5897. {
  5898. if (exception->IsGeneratorReturnException())
  5899. {
  5900. // Generator return scenario, so no need to go into the catch block and we must rethrow to propagate the exception to down level
  5901. JavascriptExceptionOperators::DoThrow(exception, scriptContext);
  5902. }
  5903. exception = exception->CloneIfStaticExceptionObject(scriptContext);
  5904. // We've got a JS exception. Grab the exception object and assign it to the
  5905. // catch object's location, then call the handler (i.e., we consume the Catch op here).
  5906. Var catchObject = exception->GetThrownObject(scriptContext);
  5907. m_reader.SetCurrentRelativeOffset((const byte *)(playout + 1), playout->RelativeJumpOffset);
  5908. LayoutSize layoutSize;
  5909. OpCode catchOp = m_reader.ReadOp(layoutSize);
  5910. #ifdef BYTECODE_BRANCH_ISLAND
  5911. if (catchOp == Js::OpCode::BrLong)
  5912. {
  5913. Assert(layoutSize == SmallLayout);
  5914. auto playoutBrLong = m_reader.BrLong();
  5915. m_reader.SetCurrentRelativeOffset((const byte *)(playoutBrLong + 1), playoutBrLong->RelativeJumpOffset);
  5916. catchOp = m_reader.ReadOp(layoutSize);
  5917. }
  5918. #endif
  5919. AssertMsg(catchOp == OpCode::Catch, "Catch op not found at catch offset");
  5920. RegSlot reg = layoutSize == SmallLayout ? m_reader.Reg1_Small()->R0 :
  5921. layoutSize == MediumLayout ? m_reader.Reg1_Medium()->R0 : m_reader.Reg1_Large()->R0;
  5922. SetReg(reg, catchObject);
  5923. ResetOut();
  5924. this->nestedCatchDepth++;
  5925. // mark the stackFrame as 'in catch block'
  5926. this->OrFlags(InterpreterStackFrameFlags_WithinCatchBlock);
  5927. this->ProcessCatch();
  5928. if (--this->nestedCatchDepth == -1)
  5929. {
  5930. // unmark the stackFrame as 'in catch block'
  5931. this->ClearFlags(InterpreterStackFrameFlags_WithinCatchBlock);
  5932. }
  5933. }
  5934. }
  5935. void InterpreterStackFrame::ProcessCatch()
  5936. {
  5937. #if ENABLE_TTD
  5938. //Clear any previous Exception Info
  5939. if (SHOULD_DO_TTD_STACK_STMT_OP(this->scriptContext))
  5940. {
  5941. this->scriptContext->GetThreadContext()->TTDExecutionInfo->ProcessCatchInfoForLastExecutedStatements();
  5942. }
  5943. #endif
  5944. #ifdef ENABLE_SCRIPT_DEBUGGING
  5945. if (this->IsInDebugMode())
  5946. {
  5947. this->DebugProcess();
  5948. }
  5949. else
  5950. #endif
  5951. {
  5952. this->Process();
  5953. }
  5954. }
  5955. int InterpreterStackFrame::ProcessFinally()
  5956. {
  5957. this->nestedFinallyDepth++;
  5958. int newOffset = 0;
  5959. #ifdef ENABLE_SCRIPT_DEBUGGING
  5960. if (this->IsInDebugMode())
  5961. {
  5962. newOffset = ::Math::PointerCastToIntegral<int>(this->DebugProcess());
  5963. }
  5964. else
  5965. #endif
  5966. {
  5967. newOffset = ::Math::PointerCastToIntegral<int>(this->Process());
  5968. }
  5969. return newOffset;
  5970. }
  5971. void InterpreterStackFrame::ProcessTryHandlerBailout(EHBailoutData * ehBailoutData, uint32 tryNestingDepth)
  5972. {
  5973. int catchOffset = ehBailoutData->catchOffset;
  5974. int finallyOffset = ehBailoutData->finallyOffset;
  5975. Js::JavascriptExceptionObject* exception = NULL;
  5976. if (catchOffset != 0 || finallyOffset != 0)
  5977. {
  5978. try
  5979. {
  5980. this->nestedTryDepth++;
  5981. // mark the stackFrame as 'in try block'
  5982. this->OrFlags(InterpreterStackFrameFlags_WithinTryBlock);
  5983. if (finallyOffset != 0)
  5984. {
  5985. this->OrFlags(InterpreterStackFrameFlags_WithinTryFinallyBlock);
  5986. }
  5987. if (tryNestingDepth != 0)
  5988. {
  5989. this->ProcessTryHandlerBailout(ehBailoutData->child, --tryNestingDepth);
  5990. }
  5991. if (catchOffset != 0)
  5992. {
  5993. Js::JavascriptExceptionOperators::AutoCatchHandlerExists autoCatchHandlerExists(scriptContext);
  5994. }
  5995. #ifdef ENABLE_SCRIPT_DEBUGGING
  5996. if (this->IsInDebugMode())
  5997. {
  5998. #if ENABLE_TTD
  5999. if (SHOULD_DO_TTD_STACK_STMT_OP(this->scriptContext))
  6000. {
  6001. this->ProcessWithDebugging_PreviousStmtTracking();
  6002. }
  6003. else
  6004. {
  6005. this->ProcessWithDebugging();
  6006. }
  6007. #else
  6008. this->ProcessWithDebugging();
  6009. #endif
  6010. this->TrySetRetOffset();
  6011. }
  6012. else
  6013. #endif
  6014. {
  6015. this->Process();
  6016. this->TrySetRetOffset();
  6017. }
  6018. }
  6019. catch (const Js::JavascriptException& err)
  6020. {
  6021. // We are using C++ exception handling which does not unwind the stack in the catch block.
  6022. // For stack overflow and OOM exceptions, we cannot run user code here because the stack is not unwind.
  6023. exception = err.GetAndClear();
  6024. }
  6025. }
  6026. else if (ehBailoutData->ht == HandlerType::HT_Catch)
  6027. {
  6028. this->nestedCatchDepth++;
  6029. // mark the stackFrame as 'in catch block'
  6030. this->OrFlags(InterpreterStackFrameFlags_WithinCatchBlock);
  6031. if (tryNestingDepth != 0)
  6032. {
  6033. this->ProcessTryHandlerBailout(ehBailoutData->child, --tryNestingDepth);
  6034. }
  6035. this->ProcessCatch();
  6036. if (--this->nestedCatchDepth == -1)
  6037. {
  6038. // unmark the stackFrame as 'in catch block'
  6039. this->ClearFlags(InterpreterStackFrameFlags_WithinCatchBlock);
  6040. }
  6041. return;
  6042. }
  6043. else
  6044. {
  6045. Assert(ehBailoutData->ht == HandlerType::HT_Finally);
  6046. this->nestedFinallyDepth++;
  6047. // mark the stackFrame as 'in finally block'
  6048. this->OrFlags(InterpreterStackFrameFlags_WithinFinallyBlock);
  6049. if (tryNestingDepth != 0)
  6050. {
  6051. this->ProcessTryHandlerBailout(ehBailoutData->child, --tryNestingDepth);
  6052. }
  6053. Js::JavascriptExceptionObject * exceptionObj = this->scriptContext->GetThreadContext()->GetPendingFinallyException();
  6054. this->scriptContext->GetThreadContext()->SetPendingFinallyException(nullptr);
  6055. int finallyEndOffset = this->ProcessFinally();
  6056. if (--this->nestedFinallyDepth == -1)
  6057. {
  6058. // unmark the stackFrame as 'in finally block'
  6059. this->ClearFlags(InterpreterStackFrameFlags_WithinFinallyBlock);
  6060. }
  6061. // Finally exited with LeaveNull, We don't throw for early returns
  6062. if (finallyEndOffset == 0 && exceptionObj)
  6063. {
  6064. #if ENABLE_NATIVE_CODEGEN
  6065. if (scriptContext->GetThreadContext()->GetTryHandlerAddrOfReturnAddr() != nullptr)
  6066. {
  6067. JavascriptExceptionOperators::WalkStackForCleaningUpInlineeInfo(scriptContext, nullptr, scriptContext->GetThreadContext()->GetTryHandlerAddrOfReturnAddr());
  6068. }
  6069. #endif
  6070. JavascriptExceptionOperators::DoThrow(const_cast<Js::JavascriptExceptionObject *>(exceptionObj), scriptContext);
  6071. }
  6072. if (finallyEndOffset != 0)
  6073. {
  6074. m_reader.SetCurrentOffset(finallyEndOffset);
  6075. }
  6076. return;
  6077. }
  6078. if (--this->nestedTryDepth == -1)
  6079. {
  6080. // unmark the stackFrame as 'in try block'
  6081. this->ClearFlags(InterpreterStackFrameFlags_WithinTryBlock | InterpreterStackFrameFlags_WithinTryFinallyBlock);
  6082. }
  6083. // Now that the stack is unwound, let's run the catch block.
  6084. if (exception)
  6085. {
  6086. if (exception->IsGeneratorReturnException())
  6087. {
  6088. // Generator return scenario, so no need to go into the catch block and we must rethrow to propagate the exception to down level
  6089. JavascriptExceptionOperators::DoThrow(exception, scriptContext);
  6090. }
  6091. if (catchOffset != 0)
  6092. {
  6093. exception = exception->CloneIfStaticExceptionObject(scriptContext);
  6094. // We've got a JS exception. Grab the exception object and assign it to the
  6095. // catch object's location, then call the handler (i.e., we consume the Catch op here).
  6096. Var catchObject = exception->GetThrownObject(scriptContext);
  6097. m_reader.SetCurrentOffset(catchOffset);
  6098. LayoutSize layoutSize;
  6099. OpCode catchOp = m_reader.ReadOp(layoutSize);
  6100. #ifdef BYTECODE_BRANCH_ISLAND
  6101. if (catchOp == Js::OpCode::BrLong)
  6102. {
  6103. Assert(layoutSize == SmallLayout);
  6104. auto playoutBrLong = m_reader.BrLong();
  6105. m_reader.SetCurrentRelativeOffset((const byte *)(playoutBrLong + 1), playoutBrLong->RelativeJumpOffset);
  6106. catchOp = m_reader.ReadOp(layoutSize);
  6107. }
  6108. #endif
  6109. AssertMsg(catchOp == OpCode::Catch, "Catch op not found at catch offset");
  6110. RegSlot reg = layoutSize == SmallLayout ? m_reader.Reg1_Small()->R0 :
  6111. layoutSize == MediumLayout ? m_reader.Reg1_Medium()->R0 : m_reader.Reg1_Large()->R0;
  6112. SetReg(reg, catchObject);
  6113. ResetOut();
  6114. this->nestedCatchDepth++;
  6115. // mark the stackFrame as 'in catch block'
  6116. this->OrFlags(InterpreterStackFrameFlags_WithinCatchBlock);
  6117. this->ProcessCatch();
  6118. if (--this->nestedCatchDepth == -1)
  6119. {
  6120. // unmark the stackFrame as 'in catch block'
  6121. this->ClearFlags(InterpreterStackFrameFlags_WithinCatchBlock);
  6122. }
  6123. }
  6124. else
  6125. {
  6126. Assert(finallyOffset != 0);
  6127. exception = exception->CloneIfStaticExceptionObject(scriptContext);
  6128. m_reader.SetCurrentOffset(finallyOffset);
  6129. ResetOut();
  6130. this->nestedFinallyDepth++;
  6131. // mark the stackFrame as 'in finally block'
  6132. this->OrFlags(InterpreterStackFrameFlags_WithinFinallyBlock);
  6133. LayoutSize layoutSize;
  6134. OpCode finallyOp = m_reader.ReadOp(layoutSize);
  6135. #ifdef BYTECODE_BRANCH_ISLAND
  6136. if (finallyOp == Js::OpCode::BrLong)
  6137. {
  6138. Assert(layoutSize == SmallLayout);
  6139. auto playoutBrLong = m_reader.BrLong();
  6140. m_reader.SetCurrentRelativeOffset((const byte *)(playoutBrLong + 1), playoutBrLong->RelativeJumpOffset);
  6141. finallyOp = m_reader.ReadOp(layoutSize);
  6142. }
  6143. #endif
  6144. Assert(finallyOp == Js::OpCode::Finally);
  6145. int finallyEndOffset = this->ProcessFinally();
  6146. if (--this->nestedFinallyDepth == -1)
  6147. {
  6148. // unmark the stackFrame as 'in finally block'
  6149. this->ClearFlags(InterpreterStackFrameFlags_WithinFinallyBlock);
  6150. }
  6151. if (finallyEndOffset == 0)
  6152. {
  6153. JavascriptExceptionOperators::DoThrow(exception, scriptContext);
  6154. }
  6155. m_reader.SetCurrentOffset(finallyEndOffset);
  6156. }
  6157. }
  6158. else
  6159. {
  6160. if (finallyOffset != 0)
  6161. {
  6162. int currOffset = m_reader.GetCurrentOffset();
  6163. m_reader.SetCurrentOffset(finallyOffset);
  6164. this->nestedFinallyDepth++;
  6165. // mark the stackFrame as 'in finally block'
  6166. this->OrFlags(InterpreterStackFrameFlags_WithinFinallyBlock);
  6167. LayoutSize layoutSize;
  6168. OpCode finallyOp = m_reader.ReadOp(layoutSize);
  6169. #ifdef BYTECODE_BRANCH_ISLAND
  6170. if (finallyOp == Js::OpCode::BrLong)
  6171. {
  6172. Assert(layoutSize == SmallLayout);
  6173. auto playoutBrLong = m_reader.BrLong();
  6174. m_reader.SetCurrentRelativeOffset((const byte *)(playoutBrLong + 1), playoutBrLong->RelativeJumpOffset);
  6175. finallyOp = m_reader.ReadOp(layoutSize);
  6176. }
  6177. #endif
  6178. Assert(finallyOp == Js::OpCode::Finally);
  6179. int finallyEndOffset = this->ProcessFinally();
  6180. if (--this->nestedFinallyDepth == -1)
  6181. {
  6182. // unmark the stackFrame as 'in finally block'
  6183. this->ClearFlags(InterpreterStackFrameFlags_WithinFinallyBlock);
  6184. }
  6185. if (finallyEndOffset == 0)
  6186. {
  6187. m_reader.SetCurrentOffset(currOffset);
  6188. }
  6189. }
  6190. }
  6191. }
  6192. void InterpreterStackFrame::TrySetRetOffset()
  6193. {
  6194. Assert(this->TestFlags(Js::InterpreterStackFrameFlags_WithinTryBlock));
  6195. // It may happen that a JITted loop body returned the offset of RET. If the loop body was
  6196. // called from a try, the interpreter "Process()" should also just return.
  6197. if (this->retOffset != 0)
  6198. {
  6199. m_reader.SetCurrentOffset(this->retOffset);
  6200. }
  6201. }
  6202. bool InterpreterStackFrame::IsInCatchOrFinallyBlock()
  6203. {
  6204. return this->TestFlags(Js::InterpreterStackFrameFlags_WithinCatchBlock) ||
  6205. this->TestFlags(Js::InterpreterStackFrameFlags_WithinFinallyBlock);
  6206. }
  6207. void InterpreterStackFrame::OP_BeginBodyScope()
  6208. {
  6209. // Currently we are using the closures created for the param scope.
  6210. // This marks the beginning of the body scope, so let's create new closures for the body scope.
  6211. FunctionBody *executeFunction = this->function->GetFunctionBody();
  6212. Assert(!this->IsParamScopeDone() && !executeFunction->IsParamAndBodyScopeMerged());
  6213. // Save the current closure. We have to use this while copying the initial value of body symbols
  6214. // from the corresponding symbols in the param.
  6215. this->SetParamClosure(this->GetLocalClosure());
  6216. this->SetNonVarReg(executeFunction->GetParamClosureRegister(), nullptr);
  6217. this->SetIsParamScopeDone(true);
  6218. // Create a new local closure for the body when either body scope has scope slots allocated or
  6219. // eval is present which can leak declarations.
  6220. if (executeFunction->scopeSlotArraySize > 0 || executeFunction->HasScopeObject())
  6221. {
  6222. this->InitializeClosures();
  6223. }
  6224. }
  6225. void InterpreterStackFrame::OP_ResumeCatch()
  6226. {
  6227. this->OrFlags(InterpreterStackFrameFlags_WithinCatchBlock);
  6228. #ifdef ENABLE_SCRIPT_DEBUGGING
  6229. if (this->IsInDebugMode())
  6230. {
  6231. this->DebugProcess();
  6232. }
  6233. else
  6234. #endif
  6235. {
  6236. this->Process();
  6237. }
  6238. this->ClearFlags(InterpreterStackFrameFlags_WithinCatchBlock);
  6239. }
  6240. /// ---------------------------------------------------------------------------------------------------
  6241. /// The behavior we want is the following:
  6242. /// - If the control leaves the user's try without throwing, execute the finally and continue
  6243. /// after the end of the try.
  6244. /// - If the user code throws, catch this exception and then execute this finally while unwinding to
  6245. /// the handler (if any).
  6246. /// ---------------------------------------------------------------------------------------------------
  6247. void InterpreterStackFrame::ProcessTryFinally(const byte* ip, Js::JumpOffset jumpOffset, Js::RegSlot regException, Js::RegSlot regOffset, bool hasYield)
  6248. {
  6249. Js::JavascriptExceptionObject* pExceptionObject = nullptr;
  6250. bool skipFinallyBlock = false;
  6251. try
  6252. {
  6253. Js::Var result = nullptr;
  6254. this->nestedTryDepth++;
  6255. // mark the stackFrame as 'in try block'
  6256. this->OrFlags(InterpreterStackFrameFlags_WithinTryBlock | InterpreterStackFrameFlags_WithinTryFinallyBlock);
  6257. if (shouldCacheSP)
  6258. {
  6259. CacheSp();
  6260. }
  6261. #ifdef ENABLE_SCRIPT_DEBUGGING
  6262. if (this->IsInDebugMode())
  6263. {
  6264. #if ENABLE_TTD
  6265. if (SHOULD_DO_TTD_STACK_STMT_OP(this->scriptContext))
  6266. {
  6267. result = this->ProcessWithDebugging_PreviousStmtTracking();
  6268. }
  6269. else
  6270. {
  6271. result = this->ProcessWithDebugging();
  6272. }
  6273. #else
  6274. result = this->ProcessWithDebugging();
  6275. #endif
  6276. }
  6277. else
  6278. #endif
  6279. {
  6280. result = this->Process();
  6281. }
  6282. if (result == nullptr)
  6283. {
  6284. Assert(hasYield);
  6285. skipFinallyBlock = true;
  6286. }
  6287. }
  6288. catch (const Js::JavascriptException& err)
  6289. {
  6290. pExceptionObject = err.GetAndClear();
  6291. }
  6292. if (--this->nestedTryDepth == -1)
  6293. {
  6294. // unmark the stackFrame as 'in try block'
  6295. this->ClearFlags(InterpreterStackFrameFlags_WithinTryBlock | InterpreterStackFrameFlags_WithinTryFinallyBlock);
  6296. }
  6297. shouldCacheSP = !skipFinallyBlock;
  6298. if (skipFinallyBlock)
  6299. {
  6300. // A leave occurred due to a yield
  6301. return;
  6302. }
  6303. // Save the current IP so execution can continue there if the finally doesn't
  6304. // take control of the flow.
  6305. int newOffset = 0;
  6306. int currOffset = m_reader.GetCurrentOffset();
  6307. if (hasYield)
  6308. {
  6309. // save the exception if there is one to a register in case we yield during the finally block
  6310. // and need to get that exception object back upon resume in OP_ResumeFinally
  6311. SetNonVarReg(regException, pExceptionObject);
  6312. SetNonVarReg(regOffset, reinterpret_cast<Js::Var>(currOffset));
  6313. }
  6314. if (pExceptionObject && !pExceptionObject->IsGeneratorReturnException())
  6315. {
  6316. // Clone static exception object early in case finally block overwrites it
  6317. pExceptionObject = pExceptionObject->CloneIfStaticExceptionObject(scriptContext);
  6318. }
  6319. #ifdef ENABLE_SCRIPT_DEBUGGING
  6320. if (pExceptionObject && this->IsInDebugMode() &&
  6321. pExceptionObject != scriptContext->GetThreadContext()->GetPendingSOErrorObject())
  6322. {
  6323. // Swallowing an exception that has triggered a finally is not implemented
  6324. // (This appears to be the same behavior as ie8)
  6325. pExceptionObject->SetDebuggerSkip(false);
  6326. }
  6327. #endif
  6328. // Call into the finally by setting the IP, consuming the Finally, and letting the interpreter recurse.
  6329. m_reader.SetCurrentRelativeOffset(ip, jumpOffset);
  6330. RestoreSp();
  6331. // mark the stackFrame as 'in finally block'
  6332. this->OrFlags(InterpreterStackFrameFlags_WithinFinallyBlock);
  6333. LayoutSize layoutSize;
  6334. OpCode finallyOp = m_reader.ReadOp(layoutSize);
  6335. #ifdef BYTECODE_BRANCH_ISLAND
  6336. if (finallyOp == Js::OpCode::BrLong)
  6337. {
  6338. Assert(layoutSize == SmallLayout);
  6339. auto playoutBrLong = m_reader.BrLong();
  6340. m_reader.SetCurrentRelativeOffset((const byte *)(playoutBrLong + 1), playoutBrLong->RelativeJumpOffset);
  6341. finallyOp = m_reader.ReadOp(layoutSize);
  6342. }
  6343. #endif
  6344. AssertMsg(finallyOp == OpCode::Finally, "Finally op not found at catch offset");
  6345. newOffset = this->ProcessFinally();
  6346. if (--this->nestedFinallyDepth == -1)
  6347. {
  6348. // unmark the stackFrame as 'in finally block'
  6349. this->ClearFlags(InterpreterStackFrameFlags_WithinFinallyBlock);
  6350. }
  6351. bool endOfFinallyBlock = newOffset == 0;
  6352. if (endOfFinallyBlock)
  6353. {
  6354. // Finally completed without taking over the flow. Resume where we left off before calling it.
  6355. m_reader.SetCurrentOffset(currOffset);
  6356. }
  6357. else
  6358. {
  6359. // Finally seized the flow with a jump out of its scope. Resume at the jump target and
  6360. // force the runtime to return to this frame without executing the catch.
  6361. m_reader.SetCurrentOffset(newOffset);
  6362. return;
  6363. }
  6364. if (pExceptionObject && (endOfFinallyBlock || !pExceptionObject->IsGeneratorReturnException()))
  6365. {
  6366. JavascriptExceptionOperators::DoThrow(pExceptionObject, scriptContext);
  6367. }
  6368. }
  6369. void InterpreterStackFrame::OP_TryFinally(const unaligned OpLayoutBr* playout)
  6370. {
  6371. ProcessTryFinally((const byte*)(playout + 1), playout->RelativeJumpOffset);
  6372. }
  6373. void InterpreterStackFrame::OP_TryFinallyWithYield(const byte* ip, Js::JumpOffset jumpOffset, Js::RegSlot regException, Js::RegSlot regOffset)
  6374. {
  6375. ProcessTryFinally(ip, jumpOffset, regException, regOffset, true);
  6376. }
  6377. void InterpreterStackFrame::OP_ResumeFinally(const byte* ip, Js::JumpOffset jumpOffset, RegSlot exceptionRegSlot, RegSlot offsetRegSlot)
  6378. {
  6379. this->OrFlags(InterpreterStackFrameFlags_WithinFinallyBlock);
  6380. int newOffset = 0;
  6381. #ifdef ENABLE_SCRIPT_DEBUGGING
  6382. if (this->IsInDebugMode())
  6383. {
  6384. newOffset = ::Math::PointerCastToIntegral<int>(this->DebugProcess());
  6385. }
  6386. else
  6387. #endif
  6388. {
  6389. newOffset = ::Math::PointerCastToIntegral<int>(this->Process());
  6390. }
  6391. this->ClearFlags(InterpreterStackFrameFlags_WithinFinallyBlock);
  6392. bool endOfFinallyBlock = newOffset == 0;
  6393. if (endOfFinallyBlock)
  6394. {
  6395. // Finally completed without taking over the flow. Resume where we left off before calling it.
  6396. int currOffset = ::Math::PointerCastToIntegral<int>(GetNonVarReg(offsetRegSlot));
  6397. m_reader.SetCurrentOffset(currOffset);
  6398. }
  6399. else
  6400. {
  6401. // Finally seized the flow with a jump out of its scope. Resume at the jump target and
  6402. // force the runtime to return to this frame without executing the catch.
  6403. m_reader.SetCurrentOffset(newOffset);
  6404. return;
  6405. }
  6406. Js::JavascriptExceptionObject* exceptionObj = (Js::JavascriptExceptionObject*)GetNonVarReg(exceptionRegSlot);
  6407. if (exceptionObj && (endOfFinallyBlock || !exceptionObj->IsGeneratorReturnException()))
  6408. {
  6409. JavascriptExceptionOperators::DoThrow(exceptionObj, scriptContext);
  6410. }
  6411. }
  6412. template <typename T>
  6413. void InterpreterStackFrame::OP_IsInst(const unaligned T* playout)
  6414. {
  6415. Var instance = GetReg(playout->R1);
  6416. Var function = GetReg(playout->R2);
  6417. IsInstInlineCache *inlineCache = this->GetIsInstInlineCache(playout->inlineCacheIndex);
  6418. ScriptContext* scriptContext = GetScriptContext();
  6419. Var result = JavascriptOperators::OP_IsInst(instance, function, scriptContext, inlineCache);
  6420. #ifdef ENABLE_BASIC_TELEMETRY
  6421. {
  6422. this->scriptContext->GetTelemetry().GetOpcodeTelemetry().IsInstanceOf(instance, function, result);
  6423. }
  6424. #endif
  6425. SetReg(playout->R0, result);
  6426. }
  6427. template <typename T>
  6428. void InterpreterStackFrame::OP_ApplyArgs(const unaligned OpLayoutT_Reg5<T> * playout)
  6429. {
  6430. // Always save and restore implicit call flags when calling out
  6431. // REVIEW: Can we avoid it if we don't collect dynamic profile info?
  6432. ThreadContext * threadContext = scriptContext->GetThreadContext();
  6433. Js::ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  6434. // Currently ApplyArgs is equivalent to CallFldVoid (where we don't use the return value)
  6435. Var v = GetNonVarReg(playout->R4);
  6436. JavascriptOperators::OP_ApplyArgs(GetReg(playout->R1), GetReg(playout->R2),
  6437. (void**)GetNonVarReg(playout->R3), *((CallInfo*)&v), GetScriptContext());
  6438. threadContext->SetImplicitCallFlags(savedImplicitCallFlags);
  6439. }
  6440. void InterpreterStackFrame::OP_SpreadArrayLiteral(const unaligned OpLayoutReg2Aux * playout)
  6441. {
  6442. ThreadContext* threadContext = this->GetScriptContext()->GetThreadContext();
  6443. ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  6444. threadContext->ClearImplicitCallFlags();
  6445. Var instance = GetReg(playout->R1);
  6446. #if ENABLE_COPYONACCESS_ARRAY
  6447. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(instance);
  6448. #endif
  6449. const Js::AuxArray<uint32> *spreadIndices = m_reader.ReadAuxArray<uint32>(playout->Offset, this->GetFunctionBody());
  6450. ScriptContext* scriptContext = GetScriptContext();
  6451. Var result = JavascriptArray::SpreadArrayArgs(instance, spreadIndices, scriptContext);
  6452. threadContext->CheckAndResetImplicitCallAccessorFlag();
  6453. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  6454. SetReg(playout->R0, result);
  6455. }
  6456. FrameDisplay *
  6457. InterpreterStackFrame::OP_LdInnerFrameDisplay(void *argHead, void *argEnv, ScriptContext *scriptContext)
  6458. {
  6459. JavascriptOperators::CheckInnerFrameDisplayArgument(argHead);
  6460. return OP_LdFrameDisplay(argHead, argEnv, scriptContext);
  6461. }
  6462. FrameDisplay *
  6463. InterpreterStackFrame::OP_LdInnerFrameDisplayNoParent(void *argHead, ScriptContext *scriptContext)
  6464. {
  6465. JavascriptOperators::CheckInnerFrameDisplayArgument(argHead);
  6466. return OP_LdFrameDisplayNoParent<true>(argHead, scriptContext);
  6467. }
  6468. FrameDisplay *
  6469. InterpreterStackFrame::OP_LdFrameDisplay(void *argHead, void *argEnv, ScriptContext *scriptContext)
  6470. {
  6471. FrameDisplay *frameDisplay;
  6472. bool strict = this->m_functionBody->GetIsStrictMode();
  6473. if (strict)
  6474. {
  6475. frameDisplay = JavascriptOperators::OP_LdStrictFrameDisplay(argHead, argEnv, scriptContext);
  6476. }
  6477. else
  6478. {
  6479. frameDisplay = JavascriptOperators::OP_LdFrameDisplay(argHead, argEnv, scriptContext);
  6480. }
  6481. return frameDisplay;
  6482. }
  6483. FrameDisplay *
  6484. InterpreterStackFrame::OP_LdFrameDisplaySetLocal(void *argHead, void *argEnv, ScriptContext *scriptContext)
  6485. {
  6486. FrameDisplay *frameDisplay = OP_LdFrameDisplay(argHead, argEnv, scriptContext);
  6487. this->SetLocalFrameDisplay(frameDisplay);
  6488. return frameDisplay;
  6489. }
  6490. FrameDisplay *
  6491. InterpreterStackFrame::NewFrameDisplay(void *argHead, void *argEnv)
  6492. {
  6493. FrameDisplay *frameDisplay;
  6494. bool strict = this->m_functionBody->GetIsStrictMode();
  6495. if (!this->m_functionBody->DoStackFrameDisplay() || !this->GetLocalFrameDisplay())
  6496. {
  6497. // Null local frame display probably indicates that we bailed out of an inlinee.
  6498. // Once we support stack closures in inlined functions, we can just assert that this value
  6499. // is never null if we should be allocating on the stack.
  6500. return this->OP_LdFrameDisplaySetLocal(argHead, argEnv, this->GetScriptContext());
  6501. }
  6502. frameDisplay = this->GetLocalFrameDisplay();
  6503. Assert(frameDisplay != nullptr);
  6504. frameDisplay->SetTag(true);
  6505. frameDisplay->SetStrictMode(strict);
  6506. frameDisplay->SetLength(this->m_functionBody->GetEnvDepth() + 1);
  6507. Assert(frameDisplay->GetLength() == ((FrameDisplay*)argEnv)->GetLength() + 1);
  6508. for (uint i = 0; i < ((FrameDisplay*)argEnv)->GetLength(); i++)
  6509. {
  6510. frameDisplay->SetItem(i + 1, ((FrameDisplay*)argEnv)->GetItem(i));
  6511. }
  6512. frameDisplay->SetItem(0, argHead);
  6513. return frameDisplay;
  6514. }
  6515. template<bool innerFD>
  6516. FrameDisplay *
  6517. InterpreterStackFrame::OP_LdFrameDisplayNoParent(void *argHead, ScriptContext *scriptContext)
  6518. {
  6519. FrameDisplay *frameDisplay;
  6520. bool strict = this->m_functionBody->GetIsStrictMode();
  6521. Var argEnv = nullptr;
  6522. if (innerFD && this->m_functionBody->GetLocalFrameDisplayRegister() != Constants::NoRegister)
  6523. {
  6524. argEnv = this->GetLocalFrameDisplay();
  6525. }
  6526. if (argEnv == nullptr && this->m_functionBody->GetEnvRegister() != Constants::NoRegister)
  6527. {
  6528. argEnv = this->LdEnv();
  6529. }
  6530. if (argEnv == nullptr)
  6531. {
  6532. if (strict)
  6533. {
  6534. frameDisplay = JavascriptOperators::OP_LdStrictFrameDisplayNoParent(argHead, scriptContext);
  6535. }
  6536. else
  6537. {
  6538. frameDisplay = JavascriptOperators::OP_LdFrameDisplayNoParent(argHead, scriptContext);
  6539. }
  6540. }
  6541. else
  6542. {
  6543. if (strict)
  6544. {
  6545. frameDisplay = JavascriptOperators::OP_LdStrictFrameDisplay(argHead, argEnv, scriptContext);
  6546. }
  6547. else
  6548. {
  6549. frameDisplay = JavascriptOperators::OP_LdFrameDisplay(argHead, argEnv, scriptContext);
  6550. }
  6551. }
  6552. return frameDisplay;
  6553. }
  6554. FrameDisplay *
  6555. InterpreterStackFrame::OP_LdFuncExprFrameDisplaySetLocal(void *argHead1, void *argHead2, ScriptContext *scriptContext)
  6556. {
  6557. FrameDisplay *frameDisplay = OP_LdFrameDisplayNoParent<false>(argHead2, scriptContext);
  6558. frameDisplay = OP_LdFrameDisplay(argHead1, frameDisplay, scriptContext);
  6559. this->SetLocalFrameDisplay(frameDisplay);
  6560. return frameDisplay;
  6561. }
  6562. FrameDisplay* InterpreterStackFrame::GetLocalFrameDisplay() const
  6563. {
  6564. Assert(this->localFrameDisplay == nullptr || this->IsClosureInitDone() || this->localFrameDisplay->GetLength() == 0);
  6565. return this->localFrameDisplay;
  6566. }
  6567. void InterpreterStackFrame::SetLocalFrameDisplay(FrameDisplay* frameDisplay)
  6568. {
  6569. this->localFrameDisplay = frameDisplay;
  6570. }
  6571. Var InterpreterStackFrame::GetLocalClosure() const
  6572. {
  6573. return this->localClosure;
  6574. }
  6575. void InterpreterStackFrame::SetLocalClosure(Var closure)
  6576. {
  6577. this->localClosure = closure;
  6578. }
  6579. Var InterpreterStackFrame::GetParamClosure() const
  6580. {
  6581. return this->paramClosure;
  6582. }
  6583. void InterpreterStackFrame::SetParamClosure(Var closure)
  6584. {
  6585. this->paramClosure = closure;
  6586. }
  6587. void
  6588. InterpreterStackFrame::OP_NewInnerScopeSlots(uint innerScopeIndex, uint count, int scopeIndex, ScriptContext *scriptContext, FunctionBody *functionBody)
  6589. {
  6590. Field(Var)* slotArray =
  6591. JavascriptOperators::OP_NewScopeSlotsWithoutPropIds(count, scopeIndex, scriptContext, functionBody);
  6592. this->SetInnerScopeFromIndex(innerScopeIndex, slotArray);
  6593. }
  6594. template <typename T>
  6595. void InterpreterStackFrame::OP_CloneInnerScopeSlots(const unaligned OpLayoutT_Unsigned1<T> *playout)
  6596. {
  6597. uint innerScopeIndex = playout->C1;
  6598. Field(Var) * slotArray;
  6599. slotArray = (Field(Var)*)this->InnerScopeFromIndex(innerScopeIndex);
  6600. slotArray = JavascriptOperators::OP_CloneScopeSlots(slotArray, scriptContext);
  6601. this->SetInnerScopeFromIndex(innerScopeIndex, slotArray);
  6602. }
  6603. template <typename T>
  6604. void InterpreterStackFrame::OP_CloneBlockScope(const unaligned OpLayoutT_Unsigned1<T> *playout)
  6605. {
  6606. uint innerScopeIndex = playout->C1;
  6607. Var scope = this->InnerScopeFromIndex(innerScopeIndex);
  6608. BlockActivationObject* blockScope = VarTo<BlockActivationObject>(scope);
  6609. scope = JavascriptOperators::OP_CloneBlockScope(blockScope, scriptContext);
  6610. this->SetInnerScopeFromIndex(innerScopeIndex, scope);
  6611. }
  6612. Field(Var)*
  6613. InterpreterStackFrame::NewScopeSlots(unsigned int size, ScriptContext *scriptContext, Var scope)
  6614. {
  6615. Field(Var)* slotArray = JavascriptOperators::OP_NewScopeSlots(size, scriptContext, scope);
  6616. this->SetLocalClosure(slotArray);
  6617. return slotArray;
  6618. }
  6619. Field(Var)*
  6620. InterpreterStackFrame::NewScopeSlots()
  6621. {
  6622. Field(Var)* slotArray;
  6623. FunctionBody * functionBody = this->m_functionBody;
  6624. uint scopeSlotCount = this->IsParamScopeDone() ? functionBody->scopeSlotArraySize : functionBody->paramScopeSlotArraySize;
  6625. Assert(scopeSlotCount != 0);
  6626. if (!functionBody->DoStackScopeSlots())
  6627. {
  6628. return this->NewScopeSlots(
  6629. scopeSlotCount + ScopeSlots::FirstSlotIndex, this->GetScriptContext(), (Var)functionBody->GetFunctionInfo());
  6630. }
  6631. slotArray = (Field(Var)*)this->GetLocalClosure();
  6632. Assert(slotArray != nullptr);
  6633. ScopeSlots scopeSlots(slotArray);
  6634. scopeSlots.SetCount(scopeSlotCount);
  6635. scopeSlots.SetScopeMetadata((Var)functionBody->GetFunctionInfo());
  6636. Var undef = functionBody->GetScriptContext()->GetLibrary()->GetUndefined();
  6637. for (unsigned int i = 0; i < scopeSlotCount; i++)
  6638. {
  6639. scopeSlots.Set(i, undef);
  6640. }
  6641. return slotArray;
  6642. }
  6643. Var
  6644. InterpreterStackFrame::NewScopeObject()
  6645. {
  6646. Var scopeObject;
  6647. if (m_functionBody->HasCachedScopePropIds())
  6648. {
  6649. const Js::PropertyIdArray *propIds = this->m_functionBody->GetFormalsPropIdArray();
  6650. JavascriptFunction* funcExpr = this->GetFunctionExpression();
  6651. PropertyId objectId = ActivationObjectEx::GetLiteralObjectRef(propIds);
  6652. scopeObject = JavascriptOperators::OP_InitCachedScope(funcExpr, propIds,
  6653. this->GetFunctionBody()->GetObjectLiteralTypeRef(objectId),
  6654. propIds->hasNonSimpleParams, GetScriptContext());
  6655. }
  6656. else
  6657. {
  6658. scopeObject = JavascriptOperators::OP_NewScopeObject(GetScriptContext());
  6659. }
  6660. this->SetLocalClosure(scopeObject);
  6661. return scopeObject;
  6662. }
  6663. FrameDisplay *
  6664. InterpreterStackFrame::GetFrameDisplayForNestedFunc() const
  6665. {
  6666. if (this->localFrameDisplay == nullptr)
  6667. {
  6668. return (FrameDisplay*)LdEnv();
  6669. }
  6670. return this->localFrameDisplay;
  6671. }
  6672. template <class T>
  6673. void InterpreterStackFrame::OP_NewStackScFunc(const unaligned T * playout)
  6674. {
  6675. uint funcIndex = playout->SlotIndex;
  6676. FrameDisplay *frameDisplay = this->GetFrameDisplayForNestedFunc();
  6677. SetRegAllowStackVarEnableOnly(playout->Value,
  6678. StackScriptFunction::OP_NewStackScFunc(frameDisplay,
  6679. this->m_functionBody->GetNestedFuncReference(funcIndex),
  6680. this->GetStackNestedFunction(funcIndex)));
  6681. }
  6682. template <class T>
  6683. void InterpreterStackFrame::OP_NewInnerStackScFunc(const unaligned T * playout)
  6684. {
  6685. uint funcIndex = playout->SlotIndex;
  6686. FrameDisplay *frameDisplay = (FrameDisplay*)GetNonVarReg(playout->Instance);
  6687. SetRegAllowStackVarEnableOnly(playout->Value,
  6688. StackScriptFunction::OP_NewStackScFunc(frameDisplay,
  6689. this->m_functionBody->GetNestedFuncReference(funcIndex),
  6690. this->GetStackNestedFunction(funcIndex)));
  6691. }
  6692. template <class T>
  6693. void InterpreterStackFrame::OP_DeleteFld(const unaligned T * playout)
  6694. {
  6695. Var result = JavascriptOperators::OP_DeleteProperty(GetReg(playout->Instance), m_functionBody->GetReferencedPropertyId(playout->PropertyIdIndex), GetScriptContext());
  6696. SetReg(playout->Value, result);
  6697. }
  6698. template <class T>
  6699. void InterpreterStackFrame::OP_DeleteLocalFld(const unaligned T * playout)
  6700. {
  6701. Var result = JavascriptOperators::OP_DeleteProperty(this->localClosure, m_functionBody->GetReferencedPropertyId(playout->PropertyIdIndex), GetScriptContext());
  6702. SetReg(playout->Instance, result);
  6703. }
  6704. template <class T>
  6705. void InterpreterStackFrame::OP_DeleteRootFld(const unaligned T * playout)
  6706. {
  6707. Var result = JavascriptOperators::OP_DeleteRootProperty(GetReg(playout->Instance), m_functionBody->GetReferencedPropertyId(playout->PropertyIdIndex), GetScriptContext());
  6708. SetReg(playout->Value, result);
  6709. }
  6710. template <class T>
  6711. void InterpreterStackFrame::OP_DeleteFldStrict(const unaligned T * playout)
  6712. {
  6713. Var result = JavascriptOperators::OP_DeleteProperty(GetReg(playout->Instance), m_functionBody->GetReferencedPropertyId(playout->PropertyIdIndex), GetScriptContext(), PropertyOperation_StrictMode);
  6714. SetReg(playout->Value, result);
  6715. }
  6716. template <class T>
  6717. void InterpreterStackFrame::OP_DeleteRootFldStrict(const unaligned T * playout)
  6718. {
  6719. Var result = JavascriptOperators::OP_DeleteRootProperty(GetReg(playout->Instance), m_functionBody->GetReferencedPropertyId(playout->PropertyIdIndex), GetScriptContext(), PropertyOperation_StrictMode);
  6720. SetReg(playout->Value, result);
  6721. }
  6722. template <typename T>
  6723. void InterpreterStackFrame::OP_ScopedDeleteFld(const unaligned OpLayoutT_ElementScopedC<T> * playout)
  6724. {
  6725. // Implicit root object as default instance
  6726. Var result = JavascriptOperators::OP_DeletePropertyScoped(GetEnvForEvalCode(),
  6727. m_functionBody->GetReferencedPropertyId(playout->PropertyIdIndex),
  6728. GetReg(Js::FunctionBody::RootObjectRegSlot), GetScriptContext());
  6729. SetReg(playout->Value, result);
  6730. }
  6731. template <typename T>
  6732. void InterpreterStackFrame::OP_ScopedDeleteFldStrict(const unaligned OpLayoutT_ElementScopedC<T> * playout)
  6733. {
  6734. // Implicit root object as default instance
  6735. Var result = JavascriptOperators::OP_DeletePropertyScoped(GetEnvForEvalCode(),
  6736. m_functionBody->GetReferencedPropertyId(playout->PropertyIdIndex),
  6737. GetReg(Js::FunctionBody::RootObjectRegSlot), GetScriptContext(), PropertyOperation_StrictMode);
  6738. SetReg(playout->Value, result);
  6739. }
  6740. template <class T>
  6741. void InterpreterStackFrame::OP_ScopedLdInst(const unaligned T * playout)
  6742. {
  6743. Var thisVar = nullptr;
  6744. Var rootObject = GetFunctionBody()->GetRootObject();
  6745. Var result = JavascriptOperators::OP_GetInstanceScoped(GetEnvForEvalCode(),
  6746. m_functionBody->GetReferencedPropertyId(playout->PropertyIdIndex), rootObject, &thisVar, GetScriptContext());
  6747. SetReg(playout->Value, result);
  6748. SetReg(playout->Value2, thisVar);
  6749. }
  6750. template <typename T>
  6751. void InterpreterStackFrame::OP_ScopedInitFunc(const unaligned OpLayoutT_ElementScopedC<T> * playout)
  6752. {
  6753. JavascriptOperators::OP_InitFuncScoped(GetEnvForEvalCode(),
  6754. m_functionBody->GetReferencedPropertyId(playout->PropertyIdIndex),
  6755. GetReg(playout->Value), GetReg(Js::FunctionBody::RootObjectRegSlot), GetScriptContext());
  6756. }
  6757. template <class T>
  6758. void InterpreterStackFrame::OP_ClearAttributes(const unaligned T * playout)
  6759. {
  6760. JavascriptOperators::OP_ClearAttributes(GetReg(playout->Instance), m_functionBody->GetReferencedPropertyId(playout->PropertyIdIndex));
  6761. }
  6762. template <class T>
  6763. void InterpreterStackFrame::OP_InitGetFld(const unaligned T * playout)
  6764. {
  6765. JavascriptOperators::OP_InitGetter(GetReg(playout->Instance), m_functionBody->GetReferencedPropertyId(playout->PropertyIdIndex), GetReg(playout->Value));
  6766. }
  6767. template <class T>
  6768. void InterpreterStackFrame::OP_InitSetFld(const unaligned T * playout)
  6769. {
  6770. JavascriptOperators::OP_InitSetter(GetReg(playout->Instance), m_functionBody->GetReferencedPropertyId(playout->PropertyIdIndex), GetReg(playout->Value));
  6771. }
  6772. template <class T>
  6773. void InterpreterStackFrame::OP_InitSetElemI(const unaligned T * playout)
  6774. {
  6775. JavascriptOperators::OP_InitElemSetter(
  6776. GetReg(playout->Instance),
  6777. GetReg(playout->Element),
  6778. GetReg(playout->Value),
  6779. m_functionBody->GetScriptContext()
  6780. );
  6781. }
  6782. template <class T>
  6783. void InterpreterStackFrame::OP_InitGetElemI(const unaligned T * playout)
  6784. {
  6785. JavascriptOperators::OP_InitElemGetter(
  6786. GetReg(playout->Instance),
  6787. GetReg(playout->Element),
  6788. GetReg(playout->Value),
  6789. m_functionBody->GetScriptContext()
  6790. );
  6791. }
  6792. template <class T>
  6793. void InterpreterStackFrame::OP_InitComputedProperty(const unaligned T * playout)
  6794. {
  6795. JavascriptOperators::OP_InitComputedProperty(
  6796. GetReg(playout->Instance),
  6797. GetReg(playout->Element),
  6798. GetReg(playout->Value),
  6799. m_functionBody->GetScriptContext()
  6800. );
  6801. }
  6802. template <class T>
  6803. void InterpreterStackFrame::OP_InitProto(const unaligned T * playout)
  6804. {
  6805. JavascriptOperators::OP_InitProto(GetReg(playout->Instance), m_functionBody->GetReferencedPropertyId(playout->PropertyIdIndex), GetReg(playout->Value));
  6806. }
  6807. void InterpreterStackFrame::DoInterruptProbe()
  6808. {
  6809. PROBE_STACK(scriptContext, 0);
  6810. }
  6811. void InterpreterStackFrame::InitializeStackFunctions(StackScriptFunction * scriptFunctions)
  6812. {
  6813. this->stackNestedFunctions = scriptFunctions;
  6814. FunctionBody * functionBody = this->m_functionBody;
  6815. uint nestedCount = functionBody->GetNestedCount();
  6816. for (uint i = 0; i < nestedCount; i++)
  6817. {
  6818. StackScriptFunction * stackScriptFunction = scriptFunctions + i;
  6819. FunctionProxy* nestedProxy = functionBody->GetNestedFunctionProxy(i);
  6820. ScriptFunctionType* type = nestedProxy->EnsureDeferredPrototypeType();
  6821. new (stackScriptFunction)StackScriptFunction(nestedProxy, type);
  6822. }
  6823. }
  6824. StackScriptFunction * InterpreterStackFrame::GetStackNestedFunction(uint index)
  6825. {
  6826. Assert(index < this->m_functionBody->GetNestedCount());
  6827. // Re-check if we have disable stack nested function
  6828. if (this->m_functionBody->DoStackNestedFunc())
  6829. {
  6830. return this->stackNestedFunctions + index;
  6831. }
  6832. return nullptr;
  6833. }
  6834. void InterpreterStackFrame::SetExecutingStackFunction(ScriptFunction * scriptFunction)
  6835. {
  6836. Assert(ThreadContext::IsOnStack(this->function));
  6837. Assert(this->m_functionBody == scriptFunction->GetFunctionBody());
  6838. this->function = scriptFunction;
  6839. }
  6840. DWORD_PTR InterpreterStackFrame::GetStackAddress() const
  6841. {
  6842. return m_stackAddress;
  6843. }
  6844. void* InterpreterStackFrame::GetAddressOfReturnAddress() const
  6845. {
  6846. return this->addressOfReturnAddress;
  6847. }
  6848. template <class T>
  6849. const byte * InterpreterStackFrame::OP_Br(const unaligned T * playout)
  6850. {
  6851. return m_reader.SetCurrentRelativeOffset((const byte *)(playout + 1), playout->RelativeJumpOffset);
  6852. }
  6853. Var InterpreterStackFrame::OP_InitBaseClass(FrameDisplay *environment, FunctionInfoPtrPtr infoRef, RegSlot protoReg)
  6854. {
  6855. RecyclableObject * protoParent = scriptContext->GetLibrary()->GetObjectPrototype();
  6856. RecyclableObject * constructorParent = scriptContext->GetLibrary()->GetFunctionPrototype();
  6857. return InitClassHelper(environment, infoRef, protoParent, constructorParent, protoReg);
  6858. }
  6859. bool InterpreterStackFrame::OP_CheckExtends(RegSlot regCtorParent, RegSlot regProtoParent, RegSlot regExtends)
  6860. {
  6861. Var extends = GetReg(regExtends);
  6862. if (JavascriptOperators::IsNull(extends))
  6863. {
  6864. SetReg(regProtoParent, scriptContext->GetLibrary()->GetNull());
  6865. SetReg(regCtorParent, scriptContext->GetLibrary()->GetFunctionPrototype());
  6866. return true;
  6867. }
  6868. if (!JavascriptOperators::IsConstructor(extends))
  6869. {
  6870. JavascriptError::ThrowTypeError(scriptContext, JSERR_ErrorOnNew);
  6871. }
  6872. return false;
  6873. }
  6874. Var InterpreterStackFrame::OP_InitClass(FrameDisplay *environment, FunctionInfoPtrPtr infoRef, Var constructorParent, Var protoParent, RegSlot protoReg)
  6875. {
  6876. return InitClassHelper(environment, infoRef, VarTo<RecyclableObject>(protoParent), VarTo<RecyclableObject>(constructorParent), protoReg);
  6877. }
  6878. Var InterpreterStackFrame::InitClassHelper(FrameDisplay *environment, FunctionInfoPtrPtr infoRef, RecyclableObject *protoParent, RecyclableObject *constructorParent, RegSlot protoReg)
  6879. {
  6880. Assert(protoParent && JavascriptOperators::IsObjectOrNull(protoParent));
  6881. Assert(constructorParent && (JavascriptOperators::IsConstructor(constructorParent) || constructorParent == scriptContext->GetLibrary()->GetFunctionPrototype()));
  6882. // Create prototype object with the default class prototype object shape {'constructor': W:T, E:F, C:T} and [[Prototype]] == protoParent
  6883. DynamicObject * proto = scriptContext->GetLibrary()->CreateClassPrototypeObject(protoParent);
  6884. // Create class constructor object for the constructor function, with default constructor shape:
  6885. // {'prototype': W:F, E:F, C:F}, {'length': W:F, E:F, C:T}, {'name': W:F, E:F, C:T}
  6886. // [[Prototype]] == constructorParent and [[HomeObject]] == proto
  6887. // The callee initializes the object and the 3 properties.
  6888. ScriptFunction * constructor = ScriptFunction::OP_NewClassConstructor(environment, infoRef, proto, constructorParent);
  6889. proto->SetSlot(SetSlotArguments(Constants::NoProperty, 0, constructor));
  6890. constructor->SetSlot(SetSlotArguments(Constants::NoProperty, 0, proto));
  6891. SetReg(protoReg, proto);
  6892. return constructor;
  6893. }
  6894. #ifdef ENABLE_SCRIPT_DEBUGGING
  6895. template <class T>
  6896. void InterpreterStackFrame::OP_EmitTmpRegCount(const unaligned OpLayoutT_Unsigned1<T> * playout)
  6897. {
  6898. this->scriptContext->GetDebugContext()->GetProbeContainer()->SetCurrentTmpRegCount(playout->C1);
  6899. }
  6900. #endif
  6901. Var InterpreterStackFrame::OP_LdHomeObj(ScriptContext * scriptContext)
  6902. {
  6903. return JavascriptOperators::OP_LdHomeObj(function, scriptContext);
  6904. }
  6905. Var InterpreterStackFrame::OP_LdFuncObj(ScriptContext * scriptContext)
  6906. {
  6907. return JavascriptOperators::OP_LdFuncObj(function, scriptContext);
  6908. }
  6909. Var InterpreterStackFrame::OP_ImportCall(Var specifier, ScriptContext *scriptContext)
  6910. {
  6911. return JavascriptOperators::OP_ImportCall(function, specifier, scriptContext);
  6912. }
  6913. void InterpreterStackFrame::ValidateRegValue(Var value, bool allowStackVar, bool allowStackVarOnDisabledStackNestedFunc) const
  6914. {
  6915. #if DBG
  6916. if (value != nullptr && !TaggedNumber::Is(value))
  6917. {
  6918. if (!allowStackVar || !this->m_functionBody->DoStackNestedFunc())
  6919. {
  6920. Assert(!ThreadContext::IsOnStack(value)
  6921. || (allowStackVar && allowStackVarOnDisabledStackNestedFunc && StackScriptFunction::IsBoxed(value)));
  6922. }
  6923. Assert(!CrossSite::NeedMarshalVar(value, GetScriptContext()));
  6924. }
  6925. #endif
  6926. }
  6927. template <typename RegSlotType>
  6928. Var InterpreterStackFrame::GetReg(RegSlotType localRegisterID) const
  6929. {
  6930. Var value = m_localSlots[localRegisterID];
  6931. ValidateRegValue(value);
  6932. return value;
  6933. }
  6934. template <typename RegSlotType>
  6935. void InterpreterStackFrame::SetReg(RegSlotType localRegisterID, Var value)
  6936. {
  6937. Assert(localRegisterID == 0 || localRegisterID >= m_functionBody->GetConstantCount());
  6938. ValidateRegValue(value);
  6939. m_localSlots[localRegisterID] = value;
  6940. #if ENABLE_OBJECT_SOURCE_TRACKING
  6941. if (value != nullptr && DynamicType::Is(Js::JavascriptOperators::GetTypeId(value)))
  6942. {
  6943. static_cast<DynamicObject*>(value)->SetDiagOriginInfoAsNeeded();
  6944. }
  6945. #endif
  6946. #if ENABLE_VALUE_TRACE
  6947. if (this->function->GetScriptContext()->ShouldPerformRecordOrReplayAction())
  6948. {
  6949. this->function->GetScriptContext()->GetThreadContext()->TTDExecutionInfo->GetTraceLogger()->WriteTraceValue(value);
  6950. }
  6951. #endif
  6952. }
  6953. template <typename T>
  6954. T InterpreterStackFrame::GetRegRaw(RegSlot localRegisterID) const
  6955. {
  6956. return (T)m_localIntSlots[localRegisterID];
  6957. }
  6958. // specialized version for doubles
  6959. template <>
  6960. double VECTORCALL InterpreterStackFrame::GetRegRaw(RegSlot localRegisterID) const
  6961. {
  6962. return (double)m_localDoubleSlots[localRegisterID];
  6963. }
  6964. template <>
  6965. float VECTORCALL InterpreterStackFrame::GetRegRaw(RegSlot localRegisterID) const
  6966. {
  6967. return (float)m_localFloatSlots[localRegisterID];
  6968. }
  6969. template <>
  6970. int64 InterpreterStackFrame::GetRegRaw(RegSlot localRegisterID) const
  6971. {
  6972. return m_localInt64Slots[localRegisterID];
  6973. }
  6974. template <typename T>
  6975. void InterpreterStackFrame::SetRegRaw(RegSlot localRegisterID, T bValue)
  6976. {
  6977. m_localIntSlots[localRegisterID] = (int)bValue;
  6978. }
  6979. template <>
  6980. void InterpreterStackFrame::SetRegRaw(RegSlot localRegisterID, float bValue)
  6981. {
  6982. m_localFloatSlots[localRegisterID] = (float)bValue;
  6983. }
  6984. template <>
  6985. void InterpreterStackFrame::SetRegRaw(RegSlot localRegisterID, double bValue)
  6986. {
  6987. m_localDoubleSlots[localRegisterID] = bValue;
  6988. }
  6989. template <>
  6990. void InterpreterStackFrame::SetRegRaw(RegSlot localRegisterID, int64 bValue)
  6991. {
  6992. m_localInt64Slots[localRegisterID] = bValue;
  6993. }
  6994. template <typename RegSlotType>
  6995. int InterpreterStackFrame::GetRegRawInt(RegSlotType localRegisterID) const
  6996. {
  6997. return m_localIntSlots[localRegisterID];
  6998. }
  6999. template <typename RegSlotType>
  7000. double InterpreterStackFrame::GetRegRawDouble(RegSlotType localRegisterID) const
  7001. {
  7002. return m_localDoubleSlots[localRegisterID];
  7003. }
  7004. template <typename RegSlotType>
  7005. float InterpreterStackFrame::GetRegRawFloat(RegSlotType localRegisterID) const
  7006. {
  7007. return m_localFloatSlots[localRegisterID];
  7008. }
  7009. template <typename RegSlotType>
  7010. void InterpreterStackFrame::SetRegRawInt(RegSlotType localRegisterID, int bValue)
  7011. {
  7012. m_localIntSlots[localRegisterID] = bValue;
  7013. }
  7014. template <typename RegSlotType>
  7015. int64 InterpreterStackFrame::GetRegRawInt64(RegSlotType localRegisterID) const
  7016. {
  7017. return m_localInt64Slots[localRegisterID];
  7018. }
  7019. template <typename RegSlotType>
  7020. void* InterpreterStackFrame::GetRegRawPtr(RegSlotType localRegisterID) const
  7021. {
  7022. #if TARGET_32
  7023. return (void*)m_localIntSlots[localRegisterID];
  7024. #elif TARGET_64
  7025. return (void*)m_localInt64Slots[localRegisterID];
  7026. #endif
  7027. }
  7028. template <typename RegSlotType>
  7029. void InterpreterStackFrame::SetRegRawPtr(RegSlotType localRegisterID, void* val)
  7030. {
  7031. #if TARGET_32
  7032. m_localIntSlots[localRegisterID] = (int32)val;
  7033. #elif TARGET_64
  7034. m_localInt64Slots[localRegisterID] = (int64)val;
  7035. #endif
  7036. }
  7037. template <typename RegSlotType>
  7038. void InterpreterStackFrame::SetRegRawInt64(RegSlotType localRegisterID, int64 bValue)
  7039. {
  7040. m_localInt64Slots[localRegisterID] = bValue;
  7041. }
  7042. template <typename RegSlotType>
  7043. void InterpreterStackFrame::SetRegRawDouble(RegSlotType localRegisterID, double bValue)
  7044. {
  7045. m_localDoubleSlots[localRegisterID] = bValue;
  7046. }
  7047. template <typename RegSlotType>
  7048. void InterpreterStackFrame::SetRegRawFloat(RegSlotType localRegisterID, float bValue)
  7049. {
  7050. m_localFloatSlots[localRegisterID] = bValue;
  7051. }
  7052. template <typename RegSlotType>
  7053. Var InterpreterStackFrame::GetRegAllowStackVar(RegSlotType localRegisterID) const
  7054. {
  7055. Var value = m_localSlots[localRegisterID];
  7056. ValidateRegValue(value, true);
  7057. return value;
  7058. }
  7059. template <typename RegSlotType>
  7060. void InterpreterStackFrame::SetRegAllowStackVar(RegSlotType localRegisterID, Var value)
  7061. {
  7062. Assert(localRegisterID == 0 || localRegisterID >= m_functionBody->GetConstantCount());
  7063. ValidateRegValue(value, true);
  7064. m_localSlots[localRegisterID] = value;
  7065. #if ENABLE_OBJECT_SOURCE_TRACKING
  7066. if (value != nullptr && DynamicType::Is(Js::JavascriptOperators::GetTypeId(value)))
  7067. {
  7068. static_cast<DynamicObject*>(value)->SetDiagOriginInfoAsNeeded();
  7069. }
  7070. #endif
  7071. #if ENABLE_VALUE_TRACE
  7072. if (this->function->GetScriptContext()->ShouldPerformRecordOrReplayAction())
  7073. {
  7074. this->function->GetScriptContext()->GetThreadContext()->TTDExecutionInfo->GetTraceLogger()->WriteTraceValue(value);
  7075. }
  7076. #endif
  7077. }
  7078. template <typename RegSlotType>
  7079. Var InterpreterStackFrame::GetRegAllowStackVarEnableOnly(RegSlotType localRegisterID) const
  7080. {
  7081. Var value = m_localSlots[localRegisterID];
  7082. ValidateRegValue(value, true, false);
  7083. return value;
  7084. }
  7085. template <typename RegSlotType>
  7086. void InterpreterStackFrame::SetRegAllowStackVarEnableOnly(RegSlotType localRegisterID, Var value)
  7087. {
  7088. Assert(localRegisterID == 0 || localRegisterID >= m_functionBody->GetConstantCount());
  7089. ValidateRegValue(value, true, false);
  7090. m_localSlots[localRegisterID] = value;
  7091. #if ENABLE_OBJECT_SOURCE_TRACKING
  7092. if (value != nullptr && DynamicType::Is(Js::JavascriptOperators::GetTypeId(value)))
  7093. {
  7094. static_cast<DynamicObject*>(value)->SetDiagOriginInfoAsNeeded();
  7095. }
  7096. #endif
  7097. #if ENABLE_VALUE_TRACE
  7098. if (this->function->GetScriptContext()->ShouldPerformRecordOrReplayAction())
  7099. {
  7100. this->function->GetScriptContext()->GetThreadContext()->TTDExecutionInfo->GetTraceLogger()->WriteTraceValue(value);
  7101. }
  7102. #endif
  7103. }
  7104. template <>
  7105. AsmJsSIMDValue InterpreterStackFrame::GetRegRaw(RegSlot localRegisterID) const
  7106. {
  7107. return (AsmJsSIMDValue)m_localSimdSlots[localRegisterID];
  7108. }
  7109. template<>
  7110. void InterpreterStackFrame::SetRegRaw(RegSlot localRegisterID, AsmJsSIMDValue bValue)
  7111. {
  7112. m_localSimdSlots[localRegisterID] = bValue;
  7113. }
  7114. template <typename RegSlotType>
  7115. AsmJsSIMDValue InterpreterStackFrame::GetRegRawSimd(RegSlotType localRegisterID) const
  7116. {
  7117. return m_localSimdSlots[localRegisterID];
  7118. }
  7119. template <typename RegSlotType>
  7120. void InterpreterStackFrame::SetRegRawSimd(RegSlotType localRegisterID, AsmJsSIMDValue bValue)
  7121. {
  7122. m_localSimdSlots[localRegisterID] = bValue;
  7123. }
  7124. int InterpreterStackFrame::OP_GetMemorySize()
  7125. {
  7126. #ifdef ENABLE_WASM
  7127. return (int)GetWebAssemblyMemory()->GetCurrentMemoryPages();
  7128. #else
  7129. Assert(UNREACHED);
  7130. return 0;
  7131. #endif
  7132. }
  7133. int InterpreterStackFrame::OP_GrowMemory(int32 delta)
  7134. {
  7135. #ifdef ENABLE_WASM
  7136. return GetWebAssemblyMemory()->GrowInternal((uint32)delta);
  7137. #else
  7138. Assert(UNREACHED);
  7139. return 0;
  7140. #endif
  7141. }
  7142. template <typename T, InterpreterStackFrame::AsmJsMathPtr<T> func> T InterpreterStackFrame::OP_UnsignedDivRemCheck(T aLeft, T aRight, ScriptContext* scriptContext)
  7143. {
  7144. if (aRight == 0)
  7145. {
  7146. JavascriptError::ThrowWebAssemblyRuntimeError(scriptContext, WASMERR_DivideByZero);
  7147. }
  7148. return func(aLeft, aRight);
  7149. }
  7150. template <typename T, InterpreterStackFrame::AsmJsMathPtr<T> func> T InterpreterStackFrame::OP_DivOverflow(T aLeft, T aRight, ScriptContext* scriptContext)
  7151. {
  7152. if (aRight == 0)
  7153. {
  7154. JavascriptError::ThrowWebAssemblyRuntimeError(scriptContext, WASMERR_DivideByZero);
  7155. }
  7156. if (aLeft == SignedTypeTraits<T>::MinValue && aRight == -1)
  7157. {
  7158. JavascriptError::ThrowWebAssemblyRuntimeError(scriptContext, VBSERR_Overflow);
  7159. }
  7160. return func(aLeft, aRight);
  7161. }
  7162. template <typename T, InterpreterStackFrame::AsmJsMathPtr<T> func> T InterpreterStackFrame::OP_RemOverflow(T aLeft, T aRight, ScriptContext* scriptContext)
  7163. {
  7164. if (aRight == 0)
  7165. {
  7166. JavascriptError::ThrowWebAssemblyRuntimeError(scriptContext, WASMERR_DivideByZero);
  7167. }
  7168. if (aLeft == SignedTypeTraits<T>::MinValue && aRight == -1)
  7169. {
  7170. return 0;
  7171. }
  7172. return func(aLeft, aRight);
  7173. }
  7174. void InterpreterStackFrame::OP_Unreachable()
  7175. {
  7176. JavascriptError::ThrowUnreachable(scriptContext);
  7177. }
  7178. template <class T>
  7179. void InterpreterStackFrame::OP_SimdLdArrGeneric(const unaligned T* playout)
  7180. {
  7181. Assert(playout->ViewType < Js::ArrayBufferView::TYPE_COUNT);
  7182. if (GetRegRawInt(playout->SlotIndex) < 0) {
  7183. JavascriptError::ThrowRangeError(scriptContext, JSERR_ArgumentOutOfRange, _u("Simd typed array access"));
  7184. }
  7185. const uint64 index = (uint64)GetRegRawInt(playout->SlotIndex) + playout->Offset;
  7186. ArrayBufferBase* arr =
  7187. #ifdef ENABLE_WASM_SIMD
  7188. (m_functionBody->IsWasmFunction()) ?
  7189. m_wasmMemory->GetBuffer() :
  7190. #endif
  7191. GetAsmJsBuffer();
  7192. BYTE* buffer = arr->GetBuffer();
  7193. uint8 dataWidth = playout->DataWidth;
  7194. RegSlot dstReg = playout->Value;
  7195. if (index + dataWidth > arr->GetByteLength())
  7196. {
  7197. JavascriptError::ThrowRangeError(scriptContext, JSERR_ArgumentOutOfRange, _u("Simd typed array access"));
  7198. }
  7199. AsmJsSIMDValue *data = (AsmJsSIMDValue*)(buffer + index);
  7200. AsmJsSIMDValue value;
  7201. value = SIMDUtils::SIMDLdData(data, dataWidth);
  7202. SetRegRawSimd(dstReg, value);
  7203. }
  7204. template <class T>
  7205. void InterpreterStackFrame::OP_SimdLdArrConstIndex(const unaligned T* playout)
  7206. {
  7207. Assert(playout->ViewType < Js::ArrayBufferView::TYPE_COUNT);
  7208. const uint64 index = (uint32)playout->SlotIndex;
  7209. JavascriptArrayBuffer* arr = GetAsmJsBuffer();
  7210. BYTE* buffer = arr->GetBuffer();
  7211. uint8 dataWidth = playout->DataWidth;
  7212. RegSlot dstReg = playout->Value;
  7213. if (index + dataWidth > arr->GetByteLength())
  7214. {
  7215. JavascriptError::ThrowRangeError(scriptContext, JSERR_ArgumentOutOfRange, _u("Simd typed array access"));
  7216. }
  7217. AsmJsSIMDValue *data = (AsmJsSIMDValue*)(buffer + index);
  7218. AsmJsSIMDValue value;
  7219. value = SIMDUtils::SIMDLdData(data, dataWidth);
  7220. SetRegRawSimd(dstReg, value);
  7221. }
  7222. template <class T>
  7223. void InterpreterStackFrame::OP_SimdStArrGeneric(const unaligned T* playout)
  7224. {
  7225. Assert(playout->ViewType < Js::ArrayBufferView::TYPE_COUNT);
  7226. if (GetRegRawInt(playout->SlotIndex) < 0)
  7227. {
  7228. JavascriptError::ThrowRangeError(scriptContext, JSERR_ArgumentOutOfRange, _u("Simd typed array access"));
  7229. }
  7230. const uint64 index = (uint64)GetRegRawInt(playout->SlotIndex) + playout->Offset;
  7231. ArrayBufferBase* arr =
  7232. #ifdef ENABLE_WASM_SIMD
  7233. (m_functionBody->IsWasmFunction()) ?
  7234. m_wasmMemory->GetBuffer() :
  7235. #endif
  7236. GetAsmJsBuffer();
  7237. BYTE* buffer = arr->GetBuffer();
  7238. uint8 dataWidth = playout->DataWidth;
  7239. RegSlot srcReg = playout->Value;
  7240. if (index + dataWidth > arr->GetByteLength())
  7241. {
  7242. JavascriptError::ThrowRangeError(scriptContext, JSERR_ArgumentOutOfRange, _u("Simd typed array access"));
  7243. }
  7244. AsmJsSIMDValue *data = (AsmJsSIMDValue*)(buffer + index);
  7245. AsmJsSIMDValue value = GetRegRawSimd(srcReg);
  7246. SIMDUtils::SIMDStData(data, value, dataWidth);
  7247. }
  7248. template <class T>
  7249. void InterpreterStackFrame::OP_SimdStArrConstIndex(const unaligned T* playout)
  7250. {
  7251. Assert(playout->ViewType < Js::ArrayBufferView::TYPE_COUNT);
  7252. const uint64 index = (uint32)playout->SlotIndex;
  7253. JavascriptArrayBuffer* arr = GetAsmJsBuffer();
  7254. BYTE* buffer = arr->GetBuffer();
  7255. uint8 dataWidth = playout->DataWidth;
  7256. RegSlot srcReg = playout->Value;
  7257. if (index + dataWidth > arr->GetByteLength())
  7258. {
  7259. JavascriptError::ThrowRangeError(scriptContext, JSERR_ArgumentOutOfRange, _u("Simd typed array access"));
  7260. }
  7261. AsmJsSIMDValue *data = (AsmJsSIMDValue*)(buffer + index);
  7262. AsmJsSIMDValue value = GetRegRawSimd(srcReg);
  7263. SIMDUtils::SIMDStData(data, value, dataWidth);
  7264. }
  7265. bool InterpreterStackFrame::SIMDAnyNaN(AsmJsSIMDValue& input)
  7266. {
  7267. if (!GetFunctionBody()->IsWasmFunction())
  7268. {
  7269. return false;
  7270. }
  7271. AsmJsSIMDValue compResult = SIMDFloat32x4Operation::OpEqual(input, input);
  7272. return !SIMDBool32x4Operation::OpAllTrue(compResult);
  7273. }
  7274. // handler for SIMD.Int32x4.FromFloat32x4
  7275. template <class T>
  7276. void InterpreterStackFrame::OP_SimdInt32x4FromFloat32x4(const unaligned T* playout)
  7277. {
  7278. bool throws = false;
  7279. AsmJsSIMDValue input = GetRegRawSimd(playout->F4_1);
  7280. AsmJsSIMDValue result{ 0 };
  7281. #ifdef ENABLE_WASM_SIMD
  7282. throws = SIMDAnyNaN(input);
  7283. if (!throws)
  7284. #endif
  7285. {
  7286. result = SIMDInt32x4Operation::OpFromFloat32x4(input, throws);
  7287. }
  7288. // value is out of bound
  7289. if (throws)
  7290. {
  7291. JavascriptError::ThrowRangeError(scriptContext, JSERR_ArgumentOutOfRange, _u("SIMD.Int32x4.FromFloat32x4"));
  7292. }
  7293. SetRegRawSimd(playout->I4_0, result);
  7294. }
  7295. // handler for SIMD.Uint32x4.FromFloat32x4
  7296. template <class T>
  7297. void InterpreterStackFrame::OP_SimdUint32x4FromFloat32x4(const unaligned T* playout)
  7298. {
  7299. bool throws = false;
  7300. AsmJsSIMDValue input = GetRegRawSimd(playout->F4_1);
  7301. AsmJsSIMDValue result{ 0 };
  7302. #ifdef ENABLE_WASM_SIMD
  7303. throws = SIMDAnyNaN(input);
  7304. if (!throws)
  7305. #endif
  7306. {
  7307. result = SIMDUint32x4Operation::OpFromFloat32x4(input, throws);
  7308. }
  7309. if (throws)
  7310. {
  7311. JavascriptError::ThrowRangeError(scriptContext, JSERR_ArgumentOutOfRange, _u("SIMD.Int32x4.FromFloat32x4"));
  7312. }
  7313. SetRegRawSimd(playout->U4_0, result);
  7314. }
  7315. template <class T>
  7316. void InterpreterStackFrame::OP_WasmSimdConst(const unaligned T* playout)
  7317. {
  7318. AsmJsSIMDValue result{ playout->C1, playout->C2, playout->C3, playout->C4 };
  7319. SetRegRawSimd(playout->F4_0, result);
  7320. }
  7321. template <class T>
  7322. void InterpreterStackFrame::OP_SimdShuffleV8X16(const unaligned T* playout)
  7323. {
  7324. uint32 lanes[Wasm::Simd::MAX_LANES];
  7325. for (uint32 i = 0; i < Wasm::Simd::MAX_LANES; i++)
  7326. {
  7327. Assert(playout->INDICES[i] < Wasm::Simd::MAX_LANES * 2);
  7328. lanes[i] = playout->INDICES[i];
  7329. }
  7330. SetRegRawSimd(playout->R0, SIMDUtils::SIMD128InnerShuffle(GetRegRawSimd(playout->R1), GetRegRawSimd(playout->R2), Wasm::Simd::MAX_LANES, lanes));
  7331. }
  7332. template <class T>
  7333. void InterpreterStackFrame::OP_SimdInt16x8(const unaligned T* playout)
  7334. {
  7335. int16 values[8];
  7336. values[0] = (int16)GetRegRawInt(playout->I1);
  7337. values[1] = (int16)GetRegRawInt(playout->I2);
  7338. values[2] = (int16)GetRegRawInt(playout->I3);
  7339. values[3] = (int16)GetRegRawInt(playout->I4);
  7340. values[4] = (int16)GetRegRawInt(playout->I5);
  7341. values[5] = (int16)GetRegRawInt(playout->I6);
  7342. values[6] = (int16)GetRegRawInt(playout->I7);
  7343. values[7] = (int16)GetRegRawInt(playout->I8);
  7344. AsmJsSIMDValue result = SIMDInt16x8Operation::OpInt16x8(values);
  7345. SetRegRawSimd(playout->I8_0, result);
  7346. }
  7347. template <class T>
  7348. void InterpreterStackFrame::OP_SimdInt8x16(const unaligned T* playout)
  7349. {
  7350. int8 values[16];
  7351. values[0] = (int8)GetRegRawInt(playout->I1);
  7352. values[1] = (int8)GetRegRawInt(playout->I2);
  7353. values[2] = (int8)GetRegRawInt(playout->I3);
  7354. values[3] = (int8)GetRegRawInt(playout->I4);
  7355. values[4] = (int8)GetRegRawInt(playout->I5);
  7356. values[5] = (int8)GetRegRawInt(playout->I6);
  7357. values[6] = (int8)GetRegRawInt(playout->I7);
  7358. values[7] = (int8)GetRegRawInt(playout->I8);
  7359. values[8] = (int8)GetRegRawInt(playout->I9);
  7360. values[9] = (int8)GetRegRawInt(playout->I10);
  7361. values[10] = (int8)GetRegRawInt(playout->I11);
  7362. values[11] = (int8)GetRegRawInt(playout->I12);
  7363. values[12] = (int8)GetRegRawInt(playout->I13);
  7364. values[13] = (int8)GetRegRawInt(playout->I14);
  7365. values[14] = (int8)GetRegRawInt(playout->I15);
  7366. values[15] = (int8)GetRegRawInt(playout->I16);
  7367. AsmJsSIMDValue result = SIMDInt8x16Operation::OpInt8x16(values);
  7368. SetRegRawSimd(playout->I16_0, result);
  7369. }
  7370. template <class T>
  7371. void InterpreterStackFrame::OP_SimdUint16x8(const unaligned T* playout)
  7372. {
  7373. uint16 values[8];
  7374. values[0] = (uint16)GetRegRawInt(playout->I1);
  7375. values[1] = (uint16)GetRegRawInt(playout->I2);
  7376. values[2] = (uint16)GetRegRawInt(playout->I3);
  7377. values[3] = (uint16)GetRegRawInt(playout->I4);
  7378. values[4] = (uint16)GetRegRawInt(playout->I5);
  7379. values[5] = (uint16)GetRegRawInt(playout->I6);
  7380. values[6] = (uint16)GetRegRawInt(playout->I7);
  7381. values[7] = (uint16)GetRegRawInt(playout->I8);
  7382. AsmJsSIMDValue result = SIMDUint16x8Operation::OpUint16x8(values);
  7383. SetRegRawSimd(playout->U8_0, result);
  7384. }
  7385. template <class T>
  7386. void InterpreterStackFrame::OP_SimdUint8x16(const unaligned T* playout)
  7387. {
  7388. uint8 values[16];
  7389. values[0] = (uint8)GetRegRawInt(playout->I1);
  7390. values[1] = (uint8)GetRegRawInt(playout->I2);
  7391. values[2] = (uint8)GetRegRawInt(playout->I3);
  7392. values[3] = (uint8)GetRegRawInt(playout->I4);
  7393. values[4] = (uint8)GetRegRawInt(playout->I5);
  7394. values[5] = (uint8)GetRegRawInt(playout->I6);
  7395. values[6] = (uint8)GetRegRawInt(playout->I7);
  7396. values[7] = (uint8)GetRegRawInt(playout->I8);
  7397. values[8] = (uint8)GetRegRawInt(playout->I9);
  7398. values[9] = (uint8)GetRegRawInt(playout->I10);
  7399. values[10] = (uint8)GetRegRawInt(playout->I11);
  7400. values[11] = (uint8)GetRegRawInt(playout->I12);
  7401. values[12] = (uint8)GetRegRawInt(playout->I13);
  7402. values[13] = (uint8)GetRegRawInt(playout->I14);
  7403. values[14] = (uint8)GetRegRawInt(playout->I15);
  7404. values[15] = (uint8)GetRegRawInt(playout->I16);
  7405. AsmJsSIMDValue result = SIMDUint8x16Operation::OpUint8x16(values);
  7406. SetRegRawSimd(playout->U16_0, result);
  7407. }
  7408. // Bool constructors
  7409. template <class T>
  7410. void InterpreterStackFrame::OP_SimdBool32x4(const unaligned T* playout)
  7411. {
  7412. bool arg1 = GetRegRawInt(playout->I1) ? true : false;
  7413. bool arg2 = GetRegRawInt(playout->I2) ? true : false;
  7414. bool arg3 = GetRegRawInt(playout->I3) ? true : false;
  7415. bool arg4 = GetRegRawInt(playout->I4) ? true : false;
  7416. AsmJsSIMDValue result = SIMDBool32x4Operation::OpBool32x4(arg1, arg2, arg3, arg4);
  7417. SetRegRawSimd(playout->B4_0, result);
  7418. }
  7419. template <class T>
  7420. void InterpreterStackFrame::OP_SimdBool16x8(const unaligned T* playout)
  7421. {
  7422. bool values[8];
  7423. values[0] = GetRegRawInt(playout->I1) ? true : false;
  7424. values[1] = GetRegRawInt(playout->I2) ? true : false;
  7425. values[2] = GetRegRawInt(playout->I3) ? true : false;
  7426. values[3] = GetRegRawInt(playout->I4) ? true : false;
  7427. values[4] = GetRegRawInt(playout->I5) ? true : false;
  7428. values[5] = GetRegRawInt(playout->I6) ? true : false;
  7429. values[6] = GetRegRawInt(playout->I7) ? true : false;
  7430. values[7] = GetRegRawInt(playout->I8) ? true : false;
  7431. AsmJsSIMDValue result = SIMDBool16x8Operation::OpBool16x8(values);
  7432. SetRegRawSimd(playout->B8_0, result);
  7433. }
  7434. template <class T>
  7435. void InterpreterStackFrame::OP_SimdBool8x16(const unaligned T* playout)
  7436. {
  7437. bool values[16];
  7438. values[0] = GetRegRawInt(playout->I1) ? true : false;
  7439. values[1] = GetRegRawInt(playout->I2) ? true : false;
  7440. values[2] = GetRegRawInt(playout->I3) ? true : false;
  7441. values[3] = GetRegRawInt(playout->I4) ? true : false;
  7442. values[4] = GetRegRawInt(playout->I5) ? true : false;
  7443. values[5] = GetRegRawInt(playout->I6) ? true : false;
  7444. values[6] = GetRegRawInt(playout->I7) ? true : false;
  7445. values[7] = GetRegRawInt(playout->I8) ? true : false;
  7446. values[8] = GetRegRawInt(playout->I9) ? true : false;
  7447. values[9] = GetRegRawInt(playout->I10) ? true : false;
  7448. values[10] = GetRegRawInt(playout->I11) ? true : false;
  7449. values[11] = GetRegRawInt(playout->I12) ? true : false;
  7450. values[12] = GetRegRawInt(playout->I13) ? true : false;
  7451. values[13] = GetRegRawInt(playout->I14) ? true : false;
  7452. values[14] = GetRegRawInt(playout->I15) ? true : false;
  7453. values[15] = GetRegRawInt(playout->I16) ? true : false;
  7454. AsmJsSIMDValue result = SIMDBool8x16Operation::OpBool8x16(values);
  7455. SetRegRawSimd(playout->B16_0, result);
  7456. }
  7457. Var InterpreterStackFrame::GetNonVarReg(RegSlot localRegisterID) const
  7458. {
  7459. return m_localSlots[localRegisterID];
  7460. }
  7461. void InterpreterStackFrame::SetNonVarReg(RegSlot localRegisterID, Var aValue)
  7462. {
  7463. m_localSlots[localRegisterID] = aValue;
  7464. }
  7465. Var InterpreterStackFrame::GetRootObject() const
  7466. {
  7467. Assert(!this->GetFunctionBody()->IsJsBuiltInCode());
  7468. Var rootObject = GetReg(Js::FunctionBody::RootObjectRegSlot);
  7469. Assert(rootObject == this->GetFunctionBody()->LoadRootObject());
  7470. return rootObject;
  7471. }
  7472. Var InterpreterStackFrame::OP_ArgIn0()
  7473. {
  7474. return m_inParams[0];
  7475. }
  7476. #if ENABLE_PROFILE_INFO
  7477. template <class T>
  7478. void InterpreterStackFrame::OP_ProfiledArgOut_A(const unaligned T * playout)
  7479. {
  7480. FunctionBody* functionBody = this->m_functionBody;
  7481. DynamicProfileInfo * dynamicProfileInfo = functionBody->GetDynamicProfileInfo();
  7482. Assert(playout->Reg > FunctionBody::FirstRegSlot);
  7483. Var value = GetReg(playout->Reg);
  7484. dynamicProfileInfo->RecordParameterAtCallSite(functionBody, playout->profileId, value, playout->Arg, playout->Reg);
  7485. SetOut(playout->Arg, value);
  7486. }
  7487. #endif
  7488. template <class T>
  7489. void InterpreterStackFrame::OP_ArgOut_A(const unaligned T * playout)
  7490. {
  7491. SetOut(playout->Arg, GetReg(playout->Reg));
  7492. }
  7493. #if DBG
  7494. template <class T>
  7495. void InterpreterStackFrame::OP_ArgOut_ANonVar(const unaligned T * playout)
  7496. {
  7497. SetOut(playout->Arg, GetNonVarReg(playout->Reg));
  7498. }
  7499. #endif
  7500. template <class T>
  7501. void InterpreterStackFrame::OP_ArgOut_Env(const unaligned T * playout)
  7502. {
  7503. Var argEnv;
  7504. if (this->m_functionBody->GetLocalFrameDisplayRegister() != Constants::NoRegister)
  7505. {
  7506. argEnv = this->GetLocalFrameDisplay();
  7507. }
  7508. else
  7509. {
  7510. argEnv = this->LdEnv();
  7511. }
  7512. SetOut(playout->Arg, argEnv);
  7513. }
  7514. BOOL InterpreterStackFrame::OP_BrFalse_A(Var aValue, ScriptContext* scriptContext)
  7515. {
  7516. return !JavascriptConversion::ToBoolean(aValue, scriptContext);
  7517. }
  7518. BOOL InterpreterStackFrame::OP_BrTrue_A(Var aValue, ScriptContext* scriptContext)
  7519. {
  7520. return JavascriptConversion::ToBoolean(aValue, scriptContext);
  7521. }
  7522. BOOL InterpreterStackFrame::OP_BrNotNull_A(Var aValue)
  7523. {
  7524. return aValue != NULL;
  7525. }
  7526. BOOL InterpreterStackFrame::OP_BrUndecl_A(Var aValue)
  7527. {
  7528. return this->scriptContext->GetLibrary()->IsUndeclBlockVar(aValue);
  7529. }
  7530. BOOL InterpreterStackFrame::OP_BrNotUndecl_A(Var aValue)
  7531. {
  7532. return !this->scriptContext->GetLibrary()->IsUndeclBlockVar(aValue);
  7533. }
  7534. BOOL InterpreterStackFrame::OP_BrOnHasProperty(Var argInstance, uint propertyIdIndex, ScriptContext* scriptContext)
  7535. {
  7536. return JavascriptOperators::OP_HasProperty(argInstance,
  7537. this->m_functionBody->GetReferencedPropertyId(propertyIdIndex), scriptContext);
  7538. }
  7539. BOOL InterpreterStackFrame::OP_BrOnNoProperty(Var argInstance, uint propertyIdIndex, ScriptContext* scriptContext)
  7540. {
  7541. return !JavascriptOperators::OP_HasProperty(argInstance,
  7542. this->m_functionBody->GetReferencedPropertyId(propertyIdIndex), scriptContext);
  7543. }
  7544. BOOL InterpreterStackFrame::OP_BrOnHasEnvProperty(Var envInstance, int32 slotIndex, uint propertyIdIndex, ScriptContext* scriptContext)
  7545. {
  7546. Var instance = OP_LdFrameDisplaySlot(envInstance, slotIndex);
  7547. return JavascriptOperators::OP_HasProperty(instance,
  7548. this->m_functionBody->GetReferencedPropertyId(propertyIdIndex), scriptContext);
  7549. }
  7550. BOOL InterpreterStackFrame::OP_BrOnClassConstructor(Var aValue)
  7551. {
  7552. return JavascriptOperators::IsClassConstructor(aValue);
  7553. }
  7554. BOOL InterpreterStackFrame::OP_BrOnBaseConstructorKind(Var aValue)
  7555. {
  7556. return JavascriptOperators::IsBaseConstructorKind(aValue);
  7557. }
  7558. template<class T>
  7559. void InterpreterStackFrame::OP_LdLen(const unaligned T * const playout)
  7560. {
  7561. Assert(playout);
  7562. ThreadContext* threadContext = this->GetScriptContext()->GetThreadContext();
  7563. ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  7564. threadContext->ClearImplicitCallFlags();
  7565. Var instance = GetReg(playout->Instance);
  7566. Var length = JavascriptOperators::OP_GetLength(instance, GetScriptContext());
  7567. threadContext->CheckAndResetImplicitCallAccessorFlag();
  7568. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  7569. SetReg(playout->Value, length);
  7570. }
  7571. #if ENABLE_PROFILE_INFO
  7572. template<class T>
  7573. void InterpreterStackFrame::OP_ProfiledLdLen(const unaligned OpLayoutDynamicProfile<T> *const playout)
  7574. {
  7575. Assert(playout);
  7576. FunctionBody * functionBody = m_functionBody;
  7577. DynamicProfileInfo * profileData = functionBody->GetDynamicProfileInfo();
  7578. Var instance = GetReg(playout->Instance);
  7579. LdLenInfo ldLenInfo;
  7580. ldLenInfo.arrayType = ValueType::Uninitialized.Merge(instance);
  7581. if (this->TestFlags(InterpreterStackFrameFlags_ProcessingBailOutOnArraySpecialization))
  7582. {
  7583. ldLenInfo.disableAggressiveSpecialization = true;
  7584. this->ClearFlags(InterpreterStackFrameFlags_ProcessingBailOutOnArraySpecialization);
  7585. }
  7586. profileData->RecordLengthLoad(functionBody, playout->profileId, ldLenInfo);
  7587. ThreadContext* threadContext = this->GetScriptContext()->GetThreadContext();
  7588. ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
  7589. threadContext->ClearImplicitCallFlags();
  7590. PropertyId propertyId = GetPropertyIdFromCacheId(playout->inlineCacheIndex);
  7591. Var value = ProfilingHelpers::ProfiledLdFld<false, false, false>(
  7592. instance,
  7593. propertyId,
  7594. GetInlineCache(playout->inlineCacheIndex),
  7595. playout->inlineCacheIndex,
  7596. GetFunctionBody(),
  7597. instance);
  7598. SetReg(playout->Value, value);
  7599. threadContext->CheckAndResetImplicitCallAccessorFlag();
  7600. threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
  7601. }
  7602. #endif
  7603. #if ENABLE_PROFILE_INFO
  7604. template <bool doProfile>
  7605. Var InterpreterStackFrame::ProfiledIsIn(Var argProperty, Var instance, ScriptContext* scriptContext, ProfileId profileId)
  7606. {
  7607. if (doProfile)
  7608. {
  7609. DynamicProfileInfo * profileData = m_functionBody->GetDynamicProfileInfo();
  7610. LdElemInfo ldElemInfo;
  7611. ldElemInfo.arrayType = ValueType::Uninitialized.Merge(instance);
  7612. if (this->TestFlags(InterpreterStackFrameFlags_ProcessingBailOutOnArraySpecialization))
  7613. {
  7614. ldElemInfo.disableAggressiveSpecialization = true;
  7615. }
  7616. this->ClearFlags(InterpreterStackFrameFlags_ProcessingBailOutOnArraySpecialization);
  7617. profileData->RecordElementLoad(m_functionBody, profileId, ldElemInfo);
  7618. }
  7619. return JavascriptOperators::IsIn(argProperty, instance, scriptContext);
  7620. }
  7621. #else
  7622. template <bool doProfile>
  7623. Var InterpreterStackFrame::ProfiledIsIn(Var argProperty, Var instance, ScriptContext* scriptContext, ProfileId profileId)
  7624. {
  7625. Assert(!doProfile);
  7626. return JavascriptOperators::IsIn(argProperty, instance, scriptContext);
  7627. }
  7628. #endif
  7629. JavascriptFunction* InterpreterStackFrame::GetFunctionExpression()
  7630. {
  7631. // Make sure we get the boxed function object if is there, (or the function itself)
  7632. return StackScriptFunction::GetCurrentFunctionObject(this->function->GetRealFunctionObject());
  7633. }
  7634. template <class T>
  7635. void InterpreterStackFrame::OP_LdFunctionExpression(const unaligned T * playout)
  7636. {
  7637. SetRegAllowStackVar(playout->R0, this->GetFunctionExpression());
  7638. }
  7639. template <class T>
  7640. void InterpreterStackFrame::OP_StFunctionExpression(const unaligned T * playout)
  7641. {
  7642. OP_StFunctionExpression(GetReg(playout->Instance), GetReg(playout->Value), playout->PropertyIdIndex);
  7643. }
  7644. template <class T>
  7645. void InterpreterStackFrame::OP_StLocalFunctionExpression(const unaligned T * playout)
  7646. {
  7647. OP_StFunctionExpression(this->localClosure, GetReg(playout->Instance), playout->PropertyIdIndex);
  7648. }
  7649. void InterpreterStackFrame::OP_StFunctionExpression(Var instance, Var value, PropertyIdIndexType index)
  7650. {
  7651. JavascriptOperators::OP_StFunctionExpression(instance,
  7652. this->m_functionBody->GetReferencedPropertyId(index), value);
  7653. }
  7654. template <class T>
  7655. void InterpreterStackFrame::OP_LdNewTarget(const unaligned T* playout)
  7656. {
  7657. if (Js::CallInfo::HasNewTarget(this->m_callFlags))
  7658. {
  7659. SetRegAllowStackVar(playout->R0, (Js::RecyclableObject*)Js::CallInfo::GetNewTarget(this->m_callFlags, this->m_inParams, (ArgSlot)this->m_inSlotsCount));
  7660. }
  7661. else if (this->m_callFlags & CallFlags_New)
  7662. {
  7663. SetRegAllowStackVar(playout->R0, this->GetFunctionExpression());
  7664. }
  7665. else
  7666. {
  7667. SetReg(playout->R0, this->GetScriptContext()->GetLibrary()->GetUndefined());
  7668. }
  7669. }
  7670. template <class T>
  7671. void InterpreterStackFrame::OP_LdImportMeta(const unaligned T* playout)
  7672. {
  7673. SetReg(playout->R0, JavascriptOperators::OP_LdImportMeta(playout->C1, scriptContext));
  7674. }
  7675. Var InterpreterStackFrame::OP_Ld_A(Var aValue)
  7676. {
  7677. return aValue;
  7678. }
  7679. Var InterpreterStackFrame::LdEnv() const
  7680. {
  7681. return this->function->GetEnvironment();
  7682. }
  7683. void InterpreterStackFrame::SetEnv(FrameDisplay *frameDisplay)
  7684. {
  7685. this->function->SetEnvironment(frameDisplay);
  7686. }
  7687. Var InterpreterStackFrame::OP_LdLocalObj()
  7688. {
  7689. if (!VirtualTableInfo<ActivationObject>::HasVirtualTable(this->localClosure) &&
  7690. !VirtualTableInfo<ActivationObjectEx>::HasVirtualTable(this->localClosure))
  7691. {
  7692. Js::Throw::FatalInternalError();
  7693. }
  7694. return this->localClosure;
  7695. }
  7696. Var InterpreterStackFrame::OP_LdParamObj()
  7697. {
  7698. if (!VirtualTableInfo<ActivationObject>::HasVirtualTable(this->paramClosure) &&
  7699. !VirtualTableInfo<ActivationObjectEx>::HasVirtualTable(this->paramClosure))
  7700. {
  7701. Js::Throw::FatalInternalError();
  7702. }
  7703. return this->paramClosure;
  7704. }
  7705. #ifdef ASMJS_PLAT
  7706. template <typename ArrayType, typename RegType>
  7707. void InterpreterStackFrame::OP_StArr(uint32 index, RegSlot regSlot)
  7708. {
  7709. JavascriptArrayBuffer* arr = GetAsmJsBuffer();
  7710. if (index < arr->GetByteLength())
  7711. {
  7712. BYTE* buffer = arr->GetBuffer();
  7713. *(ArrayType*)(buffer + index) = (ArrayType)GetRegRaw<RegType>(regSlot);
  7714. }
  7715. }
  7716. #endif
  7717. template<> inline double InterpreterStackFrame::GetArrayViewOverflowVal()
  7718. {
  7719. return *(double*)&NumberConstants::k_Nan;
  7720. }
  7721. template<> inline float InterpreterStackFrame::GetArrayViewOverflowVal()
  7722. {
  7723. return (float)*(double*)&NumberConstants::k_Nan;
  7724. }
  7725. template<typename T> T InterpreterStackFrame::GetArrayViewOverflowVal()
  7726. {
  7727. return 0;
  7728. }
  7729. template <class T>
  7730. void InterpreterStackFrame::OP_LdArrFunc(const unaligned T* playout)
  7731. {
  7732. Var* arr = (Var*)GetRegRawPtr(playout->Instance);
  7733. const uint32 index = (uint32)GetRegRawInt(playout->SlotIndex);
  7734. SetRegRawPtr(playout->Value, arr[index]);
  7735. }
  7736. template <class T>
  7737. void InterpreterStackFrame::OP_LdArrWasmFunc(const unaligned T* playout)
  7738. {
  7739. #ifdef ENABLE_WASM
  7740. WebAssemblyTable * table = VarTo<WebAssemblyTable>(GetRegRawPtr(playout->Instance));
  7741. const uint32 index = (uint32)GetRegRawInt(playout->SlotIndex);
  7742. if (index >= table->GetCurrentLength())
  7743. {
  7744. JavascriptError::ThrowWebAssemblyRuntimeError(GetScriptContext(), WASMERR_TableIndexOutOfRange);
  7745. }
  7746. Var func = table->DirectGetValue(index);
  7747. if (!func)
  7748. {
  7749. JavascriptError::ThrowWebAssemblyRuntimeError(GetScriptContext(), WASMERR_NeedWebAssemblyFunc);
  7750. }
  7751. SetRegRawPtr(playout->Value, func);
  7752. #endif
  7753. }
  7754. template <class T>
  7755. void InterpreterStackFrame::OP_CheckSignature(const unaligned T* playout)
  7756. {
  7757. #ifdef ENABLE_WASM
  7758. ScriptFunction * func = VarTo<ScriptFunction>(GetRegRawPtr(playout->R0));
  7759. int sigIndex = playout->C1;
  7760. Wasm::WasmSignature * expected = &m_signatures[sigIndex];
  7761. if (func->GetFunctionInfo()->IsDeferredParseFunction())
  7762. {
  7763. // TODO: should be able to assert this once imports are converted to wasm functions
  7764. JavascriptError::ThrowWebAssemblyRuntimeError(GetScriptContext(), WASMERR_NeedWebAssemblyFunc);
  7765. }
  7766. AsmJsFunctionInfo * asmInfo = func->GetFunctionBody()->GetAsmJsFunctionInfo();
  7767. if (!asmInfo)
  7768. {
  7769. // TODO: should be able to assert this once imports are converted to wasm functions
  7770. JavascriptError::ThrowWebAssemblyRuntimeError(GetScriptContext(), WASMERR_NeedWebAssemblyFunc);
  7771. }
  7772. if (!expected->IsEquivalent(asmInfo->GetWasmSignature()))
  7773. {
  7774. JavascriptError::ThrowWebAssemblyRuntimeError(GetScriptContext(), WASMERR_SignatureMismatch);
  7775. }
  7776. #endif
  7777. }
  7778. #ifdef ASMJS_PLAT
  7779. template <typename ArrayType, typename RegType>
  7780. void InterpreterStackFrame::OP_LdArr(uint32 index, RegSlot regSlot)
  7781. {
  7782. JavascriptArrayBuffer* arr = GetAsmJsBuffer();
  7783. BYTE* buffer = arr->GetBuffer();
  7784. ArrayType val = index < (arr->GetByteLength()) ? *(ArrayType*)(buffer + index) : GetArrayViewOverflowVal<ArrayType>();
  7785. SetRegRaw<RegType>(regSlot, (RegType)val);
  7786. }
  7787. #endif
  7788. template <class T, typename T2>
  7789. void InterpreterStackFrame::OP_StSlotPrimitive(const unaligned T* playout)
  7790. {
  7791. T2* buffer = (T2*)GetNonVarReg(playout->Instance);
  7792. buffer[playout->SlotIndex] = GetRegRaw<T2>(playout->Value);
  7793. }
  7794. template <class T>
  7795. void InterpreterStackFrame::OP_LdAsmJsSlot(const unaligned T* playout)
  7796. {
  7797. Var * slotArray = (Var*)GetNonVarReg(playout->Instance);
  7798. SetRegRawPtr(playout->Value, slotArray[playout->SlotIndex]);
  7799. }
  7800. template <class T, typename T2>
  7801. void InterpreterStackFrame::OP_LdSlotPrimitive(const unaligned T* playout)
  7802. {
  7803. T2* buffer = (T2*)GetNonVarReg(playout->Instance);
  7804. SetRegRaw<T2>(playout->Value, buffer[playout->SlotIndex]);
  7805. }
  7806. #ifndef TEMP_DISABLE_ASMJS
  7807. template <class T>
  7808. void InterpreterStackFrame::OP_LdArrGeneric(const unaligned T* playout)
  7809. {
  7810. const uint32 index = (uint32)GetRegRawInt(playout->SlotIndex);
  7811. switch (playout->ViewType)
  7812. {
  7813. #define ARRAYBUFFER_VIEW(name, align, RegType, MemType, ...) \
  7814. case ArrayBufferView::ViewType::TYPE_##name: \
  7815. OP_LdArr<MemType, RegType>(index & ARRAYBUFFER_VIEW_MASK(align), playout->Value); \
  7816. return;
  7817. #include "AsmJsArrayBufferViews.h"
  7818. default:Assert(UNREACHED);
  7819. }
  7820. }
  7821. template<typename MemType>
  7822. void InterpreterStackFrame::WasmArrayBoundsCheck(uint64 index, uint32 byteLength)
  7823. {
  7824. if (index + sizeof(MemType) > byteLength)
  7825. {
  7826. JavascriptError::ThrowWebAssemblyRuntimeError(scriptContext, WASMERR_ArrayIndexOutOfRange);
  7827. }
  7828. }
  7829. template<typename MemType>
  7830. MemType* InterpreterStackFrame::WasmAtomicsArrayBoundsCheck(byte* buffer, uint64 index, uint32 byteLength)
  7831. {
  7832. MemType* readBuffer = (MemType*)(buffer + index);
  7833. // Do alignment check to be coherent with the order the jit does the checks
  7834. if (!::Math::IsAligned<intptr_t>((intptr_t)readBuffer, sizeof(MemType)))
  7835. {
  7836. JavascriptError::ThrowWebAssemblyRuntimeError(scriptContext, WASMERR_UnalignedAtomicAccess);
  7837. }
  7838. WasmArrayBoundsCheck<MemType>(index, byteLength);
  7839. return readBuffer;
  7840. }
  7841. template <class T>
  7842. void InterpreterStackFrame::OP_LdArrWasm(const unaligned T* playout)
  7843. {
  7844. #ifdef ENABLE_WASM
  7845. Assert(playout->ViewType < Js::ArrayBufferView::TYPE_COUNT);
  7846. const uint64 index = playout->Offset + (uint64)(uint32)GetRegRawInt(playout->SlotIndex);
  7847. ArrayBufferBase* arr = GetWebAssemblyMemory()->GetBuffer();
  7848. uint32 byteLength = arr->GetByteLength();
  7849. BYTE* buffer = arr->GetBuffer();
  7850. switch (playout->ViewType)
  7851. {
  7852. #define ARRAYBUFFER_VIEW(name, align, RegType, MemType, ...) \
  7853. case ArrayBufferView::ViewType::TYPE_##name: \
  7854. WasmArrayBoundsCheck<MemType>(index, byteLength); \
  7855. SetRegRaw<RegType>(playout->Value, (RegType)*(MemType*)(buffer + index)); \
  7856. return;
  7857. #include "AsmJsArrayBufferViews.h"
  7858. default:Assert(UNREACHED);
  7859. }
  7860. #else
  7861. Assert(UNREACHED);
  7862. #endif
  7863. }
  7864. template <class T>
  7865. void InterpreterStackFrame::OP_LdArrAtomic(const unaligned T* playout)
  7866. {
  7867. #ifdef ENABLE_WASM
  7868. Assert(Wasm::Threads::IsEnabled());
  7869. Assert(playout->ViewType < Js::ArrayBufferView::TYPE_COUNT);
  7870. const uint64 index = playout->Offset + (uint64)(uint32)GetRegRawInt(playout->SlotIndex);
  7871. ArrayBufferBase* arr = GetWebAssemblyMemory()->GetBuffer();
  7872. uint32 byteLength = arr->GetByteLength();
  7873. BYTE* buffer = arr->GetBuffer();
  7874. switch (playout->ViewType)
  7875. {
  7876. #define ARRAYBUFFER_VIEW_INT(name, align, RegType, MemType, ...) \
  7877. case ArrayBufferView::ViewType::TYPE_##name: {\
  7878. MemType* readBuffer = WasmAtomicsArrayBoundsCheck<MemType>(buffer, index, byteLength); \
  7879. MemType value = AtomicsOperations::Load<MemType>(readBuffer); \
  7880. SetRegRaw<RegType>(playout->Value, (RegType)value); \
  7881. return; \
  7882. }
  7883. #include "AsmJsArrayBufferViews.h"
  7884. default:Assert(UNREACHED);
  7885. }
  7886. #else
  7887. Assert(UNREACHED);
  7888. #endif
  7889. }
  7890. template <class T>
  7891. void InterpreterStackFrame::OP_StArrAtomic(const unaligned T* playout)
  7892. {
  7893. #ifdef ENABLE_WASM
  7894. Assert(Wasm::Threads::IsEnabled());
  7895. Assert(playout->ViewType < Js::ArrayBufferView::TYPE_COUNT);
  7896. const uint64 index = playout->Offset + (uint64)(uint32)GetRegRawInt(playout->SlotIndex);
  7897. ArrayBufferBase* arr = GetWebAssemblyMemory()->GetBuffer();
  7898. uint32 byteLength = arr->GetByteLength();
  7899. BYTE* buffer = arr->GetBuffer();
  7900. switch (playout->ViewType)
  7901. {
  7902. #define ARRAYBUFFER_VIEW_INT(name, align, RegType, MemType, ...) \
  7903. case ArrayBufferView::ViewType::TYPE_##name: {\
  7904. MemType* readBuffer = WasmAtomicsArrayBoundsCheck<MemType>(buffer, index, byteLength); \
  7905. MemType value = (MemType)GetRegRaw<RegType>(playout->Value); \
  7906. MemType storedValue = AtomicsOperations::Store<MemType>(readBuffer, value); \
  7907. Assert(storedValue == value); \
  7908. return; \
  7909. }
  7910. #include "AsmJsArrayBufferViews.h"
  7911. default:Assert(UNREACHED);
  7912. }
  7913. #else
  7914. Assert(UNREACHED);
  7915. #endif
  7916. }
  7917. template <class T>
  7918. void InterpreterStackFrame::OP_LdArrConstIndex(const unaligned T* playout)
  7919. {
  7920. switch (playout->ViewType)
  7921. {
  7922. #define ARRAYBUFFER_VIEW(name, align, RegType, MemType, ...) \
  7923. case ArrayBufferView::ViewType::TYPE_##name: \
  7924. OP_LdArr<MemType, RegType>(playout->SlotIndex, playout->Value); \
  7925. return;
  7926. #include "AsmJsArrayBufferViews.h"
  7927. default:Assert(UNREACHED);
  7928. }
  7929. }
  7930. template <class T>
  7931. void InterpreterStackFrame::OP_StArrGeneric(const unaligned T* playout)
  7932. {
  7933. const uint32 index = (uint32)GetRegRawInt(playout->SlotIndex);
  7934. switch (playout->ViewType)
  7935. {
  7936. #define ARRAYBUFFER_VIEW(name, align, RegType, MemType, ...) \
  7937. case ArrayBufferView::ViewType::TYPE_##name: \
  7938. OP_StArr<MemType, RegType>(index & ARRAYBUFFER_VIEW_MASK(align), playout->Value); \
  7939. return;
  7940. #include "AsmJsArrayBufferViews.h"
  7941. default:Assert(UNREACHED);
  7942. }
  7943. }
  7944. template <class T>
  7945. void InterpreterStackFrame::OP_StArrWasm(const unaligned T* playout)
  7946. {
  7947. #ifdef ENABLE_WASM
  7948. Assert(playout->ViewType < Js::ArrayBufferView::TYPE_COUNT);
  7949. const uint64 index = playout->Offset + (uint64)(uint32)GetRegRawInt(playout->SlotIndex);
  7950. ArrayBufferBase* arr = GetWebAssemblyMemory()->GetBuffer();
  7951. uint32 byteLength = arr->GetByteLength();
  7952. BYTE* buffer = arr->GetBuffer();
  7953. switch (playout->ViewType)
  7954. {
  7955. #define ARRAYBUFFER_VIEW(name, align, RegType, MemType, ...) \
  7956. case ArrayBufferView::ViewType::TYPE_##name: \
  7957. WasmArrayBoundsCheck<MemType>(index, byteLength); \
  7958. *(MemType*)(buffer + index) = (MemType)(GetRegRaw<RegType>(playout->Value)); \
  7959. break;
  7960. #include "AsmJsArrayBufferViews.h"
  7961. default:Assert(UNREACHED);
  7962. }
  7963. CompileAssert(ArrayBufferView::ViewType::TYPE_COUNT == 15);
  7964. #if DBG
  7965. if (PHASE_TRACE(WasmMemWritesPhase, m_functionBody))
  7966. {
  7967. GetWebAssemblyMemory()->TraceMemWrite(GetWebAssemblyMemory(), (uint32)GetRegRawInt(playout->SlotIndex), playout->Offset, playout->ViewType, (uint32)(size_t)this->DEBUG_currentByteOffset, scriptContext);
  7968. }
  7969. #endif
  7970. return;
  7971. #else
  7972. Assert(UNREACHED);
  7973. #endif
  7974. }
  7975. template <class T>
  7976. void InterpreterStackFrame::OP_StArrConstIndex(const unaligned T* playout)
  7977. {
  7978. switch (playout->ViewType)
  7979. {
  7980. #define ARRAYBUFFER_VIEW(name, align, RegType, MemType, ...) \
  7981. case ArrayBufferView::ViewType::TYPE_##name: \
  7982. OP_StArr<MemType, RegType>(playout->SlotIndex, playout->Value); \
  7983. return;
  7984. #include "AsmJsArrayBufferViews.h"
  7985. default:Assert(UNREACHED);
  7986. }
  7987. }
  7988. #endif
  7989. Var InterpreterStackFrame::OP_LdSlot(Var instance, int32 slotIndex)
  7990. {
  7991. if (!PHASE_OFF(ClosureRangeCheckPhase, this->m_functionBody))
  7992. {
  7993. if ((uintptr_t)((Var*)instance)[ScopeSlots::EncodedSlotCountSlotIndex] <= (uintptr_t)(slotIndex - ScopeSlots::FirstSlotIndex))
  7994. {
  7995. Js::Throw::FatalInternalError();
  7996. }
  7997. }
  7998. return ((Var*)(instance))[slotIndex];
  7999. }
  8000. template <class T>
  8001. Var InterpreterStackFrame::OP_LdSlot(Var instance, const unaligned T* playout)
  8002. {
  8003. return OP_LdSlot(instance, playout->SlotIndex);
  8004. }
  8005. #if ENABLE_PROFILE_INFO
  8006. template <class T>
  8007. Var InterpreterStackFrame::OP_ProfiledLdSlot(Var instance, const unaligned T* playout)
  8008. {
  8009. Var value = OP_LdSlot(instance, playout->SlotIndex);
  8010. ProfilingHelpers::ProfileLdSlot(value, GetFunctionBody(), playout->profileId);
  8011. return value;
  8012. }
  8013. #endif
  8014. template <class T>
  8015. Var InterpreterStackFrame::OP_LdInnerSlot(Var slotArray, const unaligned T* playout)
  8016. {
  8017. return OP_LdSlot(slotArray, playout->SlotIndex2);
  8018. }
  8019. #if ENABLE_PROFILE_INFO
  8020. template <class T>
  8021. Var InterpreterStackFrame::OP_ProfiledLdInnerSlot(Var slotArray, const unaligned T* playout)
  8022. {
  8023. Var value = OP_LdInnerSlot(slotArray, playout);
  8024. ProfilingHelpers::ProfileLdSlot(value, GetFunctionBody(), playout->profileId);
  8025. return value;
  8026. }
  8027. #endif
  8028. template <class T>
  8029. Var InterpreterStackFrame::OP_LdInnerObjSlot(Var slotArray, const unaligned T* playout)
  8030. {
  8031. return OP_LdObjSlot(slotArray, playout->SlotIndex2);
  8032. }
  8033. #if ENABLE_PROFILE_INFO
  8034. template <class T>
  8035. Var InterpreterStackFrame::OP_ProfiledLdInnerObjSlot(Var slotArray, const unaligned T* playout)
  8036. {
  8037. Var value = OP_LdInnerObjSlot(slotArray, playout);
  8038. ProfilingHelpers::ProfileLdSlot(value, GetFunctionBody(), playout->profileId);
  8039. return value;
  8040. }
  8041. #endif
  8042. Var InterpreterStackFrame::OP_LdFrameDisplaySlot(Var instance, int32 slotIndex)
  8043. {
  8044. if (!PHASE_OFF(ClosureRangeCheckPhase, this->m_functionBody))
  8045. {
  8046. if (((FrameDisplay*)instance)->GetLength() <= slotIndex - Js::FrameDisplay::GetOffsetOfScopes() / sizeof(Var))
  8047. {
  8048. Js::Throw::FatalInternalError();
  8049. }
  8050. }
  8051. return ((Var*)instance)[slotIndex];
  8052. }
  8053. template <class T>
  8054. Var InterpreterStackFrame::OP_LdEnvObj(Var instance, const unaligned T* playout)
  8055. {
  8056. return OP_LdFrameDisplaySlot(instance, playout->SlotIndex);
  8057. }
  8058. template <class T>
  8059. Var InterpreterStackFrame::OP_LdEnvSlot(Var instance, const unaligned T* playout)
  8060. {
  8061. Var slotArray = OP_LdFrameDisplaySlot(instance, playout->SlotIndex1);
  8062. return OP_LdSlot(slotArray, playout->SlotIndex2);
  8063. }
  8064. #if ENABLE_PROFILE_INFO
  8065. template <class T>
  8066. Var InterpreterStackFrame::OP_ProfiledLdEnvSlot(Var instance, const unaligned T* playout)
  8067. {
  8068. Var value = OP_LdEnvSlot(instance, playout);
  8069. ProfilingHelpers::ProfileLdSlot(value, GetFunctionBody(), playout->profileId);
  8070. return value;
  8071. }
  8072. #endif
  8073. Var InterpreterStackFrame::OP_LdObjSlot(Var instance, int32 slotIndex)
  8074. {
  8075. Var *slotArray = *(Var**)((char*)instance + DynamicObject::GetOffsetOfAuxSlots());
  8076. return slotArray[slotIndex];
  8077. }
  8078. template <class T>
  8079. Var InterpreterStackFrame::OP_LdObjSlot(Var instance, const unaligned T* playout)
  8080. {
  8081. return OP_LdObjSlot(instance, playout->SlotIndex);
  8082. }
  8083. #if ENABLE_PROFILE_INFO
  8084. template <class T>
  8085. Var InterpreterStackFrame::OP_ProfiledLdObjSlot(Var instance, const unaligned T* playout)
  8086. {
  8087. Var value = OP_LdObjSlot(instance, playout->SlotIndex);
  8088. ProfilingHelpers::ProfileLdSlot(value, GetFunctionBody(), playout->profileId);
  8089. return value;
  8090. }
  8091. #endif
  8092. template <class T>
  8093. Var InterpreterStackFrame::OP_LdEnvObjSlot(Var instance, const unaligned T* playout)
  8094. {
  8095. Var slotArray = OP_LdFrameDisplaySlot(instance, playout->SlotIndex1);
  8096. return OP_LdObjSlot(slotArray, playout->SlotIndex2);
  8097. }
  8098. template <class T>
  8099. Var InterpreterStackFrame::OP_LdModuleSlot(Var instance, const unaligned T* playout)
  8100. {
  8101. return JavascriptOperators::OP_LdModuleSlot(playout->SlotIndex1, playout->SlotIndex2, scriptContext);
  8102. }
  8103. inline void InterpreterStackFrame::OP_StModuleSlot(Var instance, uint32 slotIndex1, uint32 slotIndex2, Var value)
  8104. {
  8105. JavascriptOperators::OP_StModuleSlot(slotIndex1, slotIndex2, value, scriptContext);
  8106. }
  8107. #if ENABLE_PROFILE_INFO
  8108. template <class T>
  8109. Var InterpreterStackFrame::OP_ProfiledLdEnvObjSlot(Var instance, const unaligned T* playout)
  8110. {
  8111. Var value = OP_LdEnvObjSlot(instance, playout);
  8112. ProfilingHelpers::ProfileLdSlot(value, GetFunctionBody(), playout->profileId);
  8113. return value;
  8114. }
  8115. #endif
  8116. void InterpreterStackFrame::OP_StSlot(Var instance, int32 slotIndex, Var value)
  8117. {
  8118. // We emit OpCode::StSlot in the bytecode only for scope slot arrays, which are not recyclable objects.
  8119. if (!PHASE_OFF(ClosureRangeCheckPhase, this->m_functionBody))
  8120. {
  8121. if ((uintptr_t)((Var*)instance)[ScopeSlots::EncodedSlotCountSlotIndex] <= (uintptr_t)(slotIndex - ScopeSlots::FirstSlotIndex))
  8122. {
  8123. Js::Throw::FatalInternalError();
  8124. }
  8125. }
  8126. ((Field(Var)*)(instance))[slotIndex] = value;
  8127. }
  8128. void InterpreterStackFrame::OP_StEnvSlot(Var instance, int32 slotIndex1, int32 slotIndex2, Var value)
  8129. {
  8130. Var slotArray = (Var*)OP_LdFrameDisplaySlot(instance, slotIndex1);
  8131. OP_StSlot(slotArray, slotIndex2, value);
  8132. }
  8133. void InterpreterStackFrame::OP_StSlotChkUndecl(Var instance, int32 slotIndex, Var value)
  8134. {
  8135. // We emit OpCode::StSlot in the bytecode only for scope slot arrays, which are not recyclable objects.
  8136. if (!PHASE_OFF(ClosureRangeCheckPhase, this->m_functionBody))
  8137. {
  8138. if ((uintptr_t)((Var*)instance)[ScopeSlots::EncodedSlotCountSlotIndex] <= (uintptr_t)(slotIndex - ScopeSlots::FirstSlotIndex))
  8139. {
  8140. Js::Throw::FatalInternalError();
  8141. }
  8142. }
  8143. OP_ChkUndecl(((Field(Var)*)instance)[slotIndex]);
  8144. ((Field(Var)*)(instance))[slotIndex] = value;
  8145. }
  8146. void InterpreterStackFrame::OP_StEnvSlotChkUndecl(Var instance, int32 slotIndex1, int32 slotIndex2, Var value)
  8147. {
  8148. Var slotArray = OP_LdFrameDisplaySlot(instance, slotIndex1);
  8149. OP_StSlotChkUndecl(slotArray, slotIndex2, value);
  8150. }
  8151. void InterpreterStackFrame::OP_StObjSlot(Var instance, int32 slotIndex, Var value)
  8152. {
  8153. // It would be nice to assert that it's ok to store directly to slot, but we don't have the propertyId.
  8154. Field(Var) *slotArray = *(Field(Var)**)((char*)instance + DynamicObject::GetOffsetOfAuxSlots());
  8155. slotArray[slotIndex] = value;
  8156. }
  8157. void InterpreterStackFrame::OP_StObjSlotChkUndecl(Var instance, int32 slotIndex, Var value)
  8158. {
  8159. // It would be nice to assert that it's ok to store directly to slot, but we don't have the propertyId.
  8160. Field(Var) *slotArray = *(Field(Var)**)((char*)instance + DynamicObject::GetOffsetOfAuxSlots());
  8161. OP_ChkUndecl(slotArray[slotIndex]);
  8162. slotArray[slotIndex] = value;
  8163. }
  8164. void InterpreterStackFrame::OP_StEnvObjSlot(Var instance, int32 slotIndex1, int32 slotIndex2, Var value)
  8165. {
  8166. // It would be nice to assert that it's ok to store directly to slot, but we don't have the propertyId.
  8167. Var envInstance = (Var*)OP_LdFrameDisplaySlot(instance, slotIndex1);
  8168. OP_StObjSlot(envInstance, slotIndex2, value);
  8169. }
  8170. void InterpreterStackFrame::OP_StEnvObjSlotChkUndecl(Var instance, int32 slotIndex1, int32 slotIndex2, Var value)
  8171. {
  8172. // It would be nice to assert that it's ok to store directly to slot, but we don't have the propertyId.
  8173. Var envInstance = (Var*)OP_LdFrameDisplaySlot(instance, slotIndex1);
  8174. OP_StObjSlotChkUndecl(envInstance, slotIndex2, value);
  8175. }
  8176. Var InterpreterStackFrame::OP_LdStackArgPtr(void)
  8177. {
  8178. // Return the address of the first param after "this".
  8179. return m_inParams + 1;
  8180. }
  8181. ForInObjectEnumerator * InterpreterStackFrame::GetForInEnumerator(uint forInLoopLevel)
  8182. {
  8183. Assert(forInLoopLevel < this->m_functionBody->GetForInLoopDepth());
  8184. return &this->forInObjectEnumerators[forInLoopLevel];
  8185. }
  8186. void InterpreterStackFrame::OP_InitForInEnumerator(Var object, uint forInLoopLevel)
  8187. {
  8188. JavascriptOperators::OP_InitForInEnumerator(object, GetForInEnumerator(forInLoopLevel), this->GetScriptContext());
  8189. }
  8190. void InterpreterStackFrame::OP_InitForInEnumeratorWithCache(Var object, uint forInLoopLevel, ProfileId profileId)
  8191. {
  8192. JavascriptOperators::OP_InitForInEnumerator(object, GetForInEnumerator(forInLoopLevel), this->GetScriptContext(),
  8193. m_functionBody->GetForInCache(profileId));
  8194. }
  8195. // Called for the debug purpose, to create the arguments object explicitly even though script has not declared it.
  8196. Var InterpreterStackFrame::CreateHeapArguments(ScriptContext* scriptContext)
  8197. {
  8198. return JavascriptOperators::LoadHeapArguments(this->function->GetRealFunctionObject(), this->m_inSlotsCount - 1, &this->m_inParams[1], scriptContext->GetLibrary()->GetNull(), scriptContext->GetLibrary()->GetNull(), scriptContext, false);
  8199. }
  8200. template <bool letArgs>
  8201. Var InterpreterStackFrame::LdHeapArgumentsImpl(Var argsArray, ScriptContext* scriptContext)
  8202. {
  8203. Var frameObj;
  8204. if (m_functionBody->HasScopeObject() && argsArray != scriptContext->GetLibrary()->GetNull())
  8205. {
  8206. frameObj = this->localClosure;
  8207. Assert(frameObj);
  8208. }
  8209. else
  8210. {
  8211. frameObj = scriptContext->GetLibrary()->GetNull();
  8212. }
  8213. Var args = JavascriptOperators::LoadHeapArguments(this->function->GetRealFunctionObject(), this->m_inSlotsCount - 1, &this->m_inParams[1], frameObj, argsArray, scriptContext, letArgs);
  8214. this->m_arguments = args;
  8215. return args;
  8216. }
  8217. Var InterpreterStackFrame::OP_LdHeapArguments(ScriptContext* scriptContext)
  8218. {
  8219. Var argsArray = m_functionBody->GetFormalsPropIdArrayOrNullObj();
  8220. return LdHeapArgumentsImpl<false>(argsArray, scriptContext);
  8221. }
  8222. Var InterpreterStackFrame::OP_LdLetHeapArguments(ScriptContext* scriptContext)
  8223. {
  8224. Var argsArray = m_functionBody->GetFormalsPropIdArrayOrNullObj();
  8225. return LdHeapArgumentsImpl<true>(argsArray, scriptContext);
  8226. }
  8227. Var InterpreterStackFrame::OP_LdHeapArgsCached(ScriptContext* scriptContext)
  8228. {
  8229. uint32 formalsCount = this->m_functionBody->GetInParamsCount() - 1;
  8230. Var args = JavascriptOperators::LoadHeapArgsCached(this->function->GetRealFunctionObject(), this->m_inSlotsCount - 1, formalsCount, &this->m_inParams[1], this->localClosure, scriptContext, false);
  8231. this->m_arguments = args;
  8232. return args;
  8233. }
  8234. Var InterpreterStackFrame::OP_LdLetHeapArgsCached(ScriptContext* scriptContext)
  8235. {
  8236. uint32 formalsCount = this->m_functionBody->GetInParamsCount() - 1;
  8237. Var args = JavascriptOperators::LoadHeapArgsCached(this->function->GetRealFunctionObject(), this->m_inSlotsCount - 1, formalsCount, &this->m_inParams[1], this->localClosure, scriptContext, true);
  8238. this->m_arguments = args;
  8239. return args;
  8240. }
  8241. HeapArgumentsObject * InterpreterStackFrame::CreateEmptyHeapArgumentsObject(ScriptContext* scriptContext)
  8242. {
  8243. HeapArgumentsObject * args = JavascriptOperators::CreateHeapArguments(this->function->GetRealFunctionObject(), this->m_inSlotsCount - 1, 0, nullptr, scriptContext);
  8244. this->m_arguments = args;
  8245. return args;
  8246. }
  8247. void InterpreterStackFrame::TrySetFrameObjectInHeapArgObj(ScriptContext * scriptContext, bool hasNonSimpleParams, bool isScopeObjRestored)
  8248. {
  8249. Var frameObject = nullptr;
  8250. uint32 formalsCount = this->m_functionBody->GetInParamsCount() - 1;
  8251. Js::PropertyIdArray * propIds = nullptr;
  8252. Js::HeapArgumentsObject* heapArgObj = nullptr;
  8253. //We always set the Frame object to nullptr in BailOutRecord::EnsureArguments for stack args optimization.
  8254. if (m_arguments != nullptr && ((Js::HeapArgumentsObject*)(m_arguments))->GetFrameObject() == nullptr)
  8255. {
  8256. heapArgObj = (Js::HeapArgumentsObject*)m_arguments;
  8257. }
  8258. bool isCachedScope = false;
  8259. //For Non-simple params, we don't have a scope object created.
  8260. if (this->m_functionBody->NeedScopeObjectForArguments(hasNonSimpleParams))
  8261. {
  8262. frameObject = GetLocalClosure();
  8263. isCachedScope = m_functionBody->HasCachedScopePropIds();
  8264. propIds = this->m_functionBody->GetFormalsPropIdArray();
  8265. if (isScopeObjRestored && VarIs<ActivationObject>(frameObject))
  8266. {
  8267. Assert(this->GetFunctionBody()->GetDoScopeObjectCreation());
  8268. isCachedScope = true;
  8269. if (PHASE_VERBOSE_TRACE1(Js::StackArgFormalsOptPhase) && m_functionBody->GetInParamsCount() > 1)
  8270. {
  8271. Output::Print(_u("StackArgFormals : %s (%d) :Using the restored scope object in the bail out path. \n"), m_functionBody->GetDisplayName(), m_functionBody->GetFunctionNumber());
  8272. Output::Flush();
  8273. }
  8274. }
  8275. else
  8276. {
  8277. if (isCachedScope)
  8278. {
  8279. Field(DynamicType*) literalType = nullptr;
  8280. Assert(!propIds->hasNonSimpleParams && !hasNonSimpleParams);
  8281. frameObject = JavascriptOperators::OP_InitCachedScope(this->GetJavascriptFunction(), propIds, &literalType, hasNonSimpleParams, scriptContext);
  8282. }
  8283. else
  8284. {
  8285. frameObject = JavascriptOperators::OP_NewScopeObject(GetScriptContext());
  8286. }
  8287. Assert(propIds != nullptr);
  8288. SetLocalClosure(frameObject);
  8289. if (PHASE_VERBOSE_TRACE1(Js::StackArgFormalsOptPhase) && m_functionBody->GetInParamsCount() > 1)
  8290. {
  8291. Output::Print(_u("StackArgFormals : %s (%d) :Creating scope object in the bail out path. \n"), m_functionBody->GetDisplayName(), m_functionBody->GetFunctionNumber());
  8292. Output::Flush();
  8293. }
  8294. }
  8295. }
  8296. else
  8297. {
  8298. //We reached here because, either we don't have any formals or we don't have a scope object (it could be in strict mode or have non-simple param list)
  8299. Assert(formalsCount == 0 || (m_functionBody->GetIsStrictMode() || hasNonSimpleParams));
  8300. frameObject = nullptr;
  8301. formalsCount = 0;
  8302. if (PHASE_VERBOSE_TRACE1(Js::StackArgOptPhase))
  8303. {
  8304. Output::Print(_u("StackArgOpt : %s (%d) :Creating NULL scope object in the bail out path. \n"), m_functionBody->GetDisplayName(), m_functionBody->GetFunctionNumber());
  8305. Output::Flush();
  8306. }
  8307. }
  8308. if (heapArgObj)
  8309. {
  8310. Assert(frameObject == nullptr || VarIs<ActivationObject>(frameObject));
  8311. heapArgObj->SetFormalCount(formalsCount);
  8312. heapArgObj->SetFrameObject(frameObject != nullptr ?
  8313. static_cast<ActivationObject*>(frameObject) : nullptr);
  8314. if (PHASE_TRACE1(Js::StackArgFormalsOptPhase) && formalsCount > 0)
  8315. {
  8316. Output::Print(_u("StackArgFormals : %s (%d) :Attaching the scope object with the heap arguments object in the bail out path. \n"), m_functionBody->GetDisplayName(), m_functionBody->GetFunctionNumber());
  8317. Output::Flush();
  8318. }
  8319. }
  8320. //Fill the Heap arguments and scope object with values
  8321. // If there is no heap arguments object, then fill only the scope object with actuals.
  8322. JavascriptOperators::FillScopeObject(this->function->GetRealFunctionObject(), this->m_inSlotsCount - 1, formalsCount, frameObject, &this->m_inParams[1], propIds, heapArgObj, scriptContext, hasNonSimpleParams, isCachedScope);
  8323. }
  8324. Var InterpreterStackFrame::OP_LdArgumentsFromFrame()
  8325. {
  8326. return this->m_arguments;
  8327. }
  8328. void* InterpreterStackFrame::OP_LdArgCnt()
  8329. {
  8330. return (void*)m_inSlotsCount;
  8331. }
  8332. void InterpreterStackFrame::OP_AsyncYieldStar(Var yieldDataVar, Var value, ScriptContext* scriptContext)
  8333. {
  8334. ResumeYieldData* yieldData = static_cast<ResumeYieldData*>(yieldDataVar);
  8335. JavascriptOperators::OP_AsyncYieldStar(yieldData->generator, value, scriptContext);
  8336. }
  8337. void InterpreterStackFrame::OP_AsyncYield(Var yieldDataVar, Var value, ScriptContext* scriptContext)
  8338. {
  8339. ResumeYieldData* yieldData = static_cast<ResumeYieldData*>(yieldDataVar);
  8340. JavascriptOperators::OP_AsyncYield(yieldData->generator, value, scriptContext);
  8341. }
  8342. void InterpreterStackFrame::OP_Await(Var yieldDataVar, Var value, ScriptContext* scriptContext)
  8343. {
  8344. ResumeYieldData* yieldData = static_cast<ResumeYieldData*>(yieldDataVar);
  8345. JavascriptOperators::OP_Await(yieldData->generator, value, scriptContext);
  8346. }
  8347. Var InterpreterStackFrame::OP_AsyncYieldIsReturn(Var yieldDataVar)
  8348. {
  8349. ResumeYieldData* yieldData = static_cast<ResumeYieldData*>(yieldDataVar);
  8350. return JavascriptOperators::OP_AsyncYieldIsReturn(yieldData);
  8351. }
  8352. Var InterpreterStackFrame::OP_ResumeYield(Var yieldDataVar, RegSlot yieldStarIterator)
  8353. {
  8354. ResumeYieldData* yieldData = static_cast<ResumeYieldData*>(yieldDataVar);
  8355. RecyclableObject* iterator = yieldStarIterator != Constants::NoRegister ? VarTo<RecyclableObject>(GetNonVarReg(yieldStarIterator)) : nullptr;
  8356. return JavascriptOperators::OP_ResumeYield(yieldData, iterator);
  8357. }
  8358. void* InterpreterStackFrame::operator new(size_t byteSize, void* previousAllocation) throw()
  8359. {
  8360. //
  8361. // Placement 'new' is used by InterpreterStackFrame to initialize the C++ object on the RcInterpreter's
  8362. // program stack:
  8363. // - Unlike most other allocations, the previously allocated memory will __not__ be
  8364. // zero-initialized, as we do not want the overhead of zero-initializing the frame when
  8365. // calling functions.
  8366. //
  8367. // NOTE: If we wanted to add C# semantics of all locals are automatically zero-initialized,
  8368. // need to determine the most efficient mechanism for this.
  8369. //
  8370. return previousAllocation;
  8371. }
  8372. void __cdecl InterpreterStackFrame::operator delete(void * allocationToFree, void * previousAllocation) throw()
  8373. {
  8374. AssertMsg(allocationToFree == previousAllocation, "Memory locations should match");
  8375. AssertMsg(false, "This function should never actually be called");
  8376. }
  8377. void InterpreterStackFrame::OP_WasmPrintFunc(int regIndex)
  8378. {
  8379. #if defined(ENABLE_DEBUG_CONFIG_OPTIONS) && defined(ENABLE_WASM)
  8380. Assert(m_functionBody->IsWasmFunction());
  8381. uint index = GetRegRawInt(regIndex);
  8382. Wasm::WasmFunctionInfo* info = m_functionBody->GetAsmJsFunctionInfo()->GetWebAssemblyModule()->GetWasmFunctionInfo(index);
  8383. int col = WAsmJs::Tracing::GetPrintCol();
  8384. if (col > 0)
  8385. {
  8386. Output::SkipToColumn(col);
  8387. }
  8388. info->GetBody()->DumpFullFunctionName();
  8389. Output::Print(_u("("));
  8390. #endif
  8391. }
  8392. JavascriptArrayBuffer* InterpreterStackFrame::GetAsmJsBuffer() const
  8393. {
  8394. AssertMsg(!m_functionBody->IsWasmFunction(), "Do not use GetAsmJsBuffer for WebAssembly, Use GetWebAssemblyMemory instead");
  8395. return m_asmJsBuffer;
  8396. }
  8397. #ifdef ENABLE_WASM
  8398. WebAssemblyMemory* InterpreterStackFrame::GetWebAssemblyMemory() const
  8399. {
  8400. AssertMsg(m_functionBody->IsWasmFunction(), "Do not use GetWebAssemblyMemory for Asm.js, Use GetAsmJsBuffer instead");
  8401. return m_wasmMemory;
  8402. }
  8403. #endif
  8404. template void* Js::InterpreterStackFrame::GetReg<unsigned int>(unsigned int) const;
  8405. template void Js::InterpreterStackFrame::SetReg<unsigned int>(unsigned int, void*);
  8406. } // namespace Js
  8407. // Make sure the macro and the layout for the op is consistent
  8408. #define DEF2(x, op, ...) \
  8409. CompileAssert(!Js::OpCodeInfo<Js::OpCode::op>::HasMultiSizeLayout); \
  8410. CompileAssert(!Js::OpCodeInfo<Js::OpCode::op>::IsExtendedOpcode);
  8411. #define DEF3(x, op, ...) DEF2(x, op)
  8412. #define EXDEF2(x, op, ...) \
  8413. CompileAssert(!Js::OpCodeInfo<Js::OpCode::op>::HasMultiSizeLayout); \
  8414. CompileAssert(Js::OpCodeInfo<Js::OpCode::op>::IsExtendedOpcode);
  8415. #define EXDEF3(x, op, ...) EXDEF2(x, op)
  8416. #define DEF2_WMS(x, op, ...) \
  8417. CompileAssert(Js::OpCodeInfo<Js::OpCode::op>::HasMultiSizeLayout); \
  8418. CompileAssert(!Js::OpCodeInfo<Js::OpCode::op>::IsExtendedOpcode);
  8419. #define DEF3_WMS(x, op, ...) DEF2_WMS(x, op)
  8420. #define EXDEF2_WMS(x, op, ...) \
  8421. CompileAssert(Js::OpCodeInfo<Js::OpCode::op>::HasMultiSizeLayout); \
  8422. CompileAssert(Js::OpCodeInfo<Js::OpCode::op>::IsExtendedOpcode);
  8423. #define EXDEF3_WMS(x, op, ...) EXDEF2_WMS(x, op)
  8424. #include "InterpreterHandler.inl"
  8425. // Make sure the macro and the layout for the op is consistent
  8426. #define DEF2(x, op, ...) \
  8427. CompileAssert(!Js::OpCodeInfoAsmJs<Js::OpCodeAsmJs::op>::HasMultiSizeLayout); \
  8428. CompileAssert(!Js::OpCodeInfoAsmJs<Js::OpCodeAsmJs::op>::IsExtendedOpcode);
  8429. #define DEF3(x, op, ...) DEF2(x, op)
  8430. #define DEF4(x, op, ...) DEF2(x, op)
  8431. #define EXDEF2(x, op, ...) \
  8432. CompileAssert(!Js::OpCodeInfoAsmJs<Js::OpCodeAsmJs::op>::HasMultiSizeLayout); \
  8433. CompileAssert(Js::OpCodeInfoAsmJs<Js::OpCodeAsmJs::op>::IsExtendedOpcode);
  8434. #define EXDEF3(x, op, ...) EXDEF2(x, op)
  8435. #define EXDEF4(x, op, ...) EXDEF2(x, op)
  8436. #define DEF2_WMS(x, op, ...) \
  8437. CompileAssert(Js::OpCodeInfoAsmJs<Js::OpCodeAsmJs::op>::HasMultiSizeLayout); \
  8438. CompileAssert(!Js::OpCodeInfoAsmJs<Js::OpCodeAsmJs::op>::IsExtendedOpcode);
  8439. #define DEF3_WMS(x, op, ...) DEF2_WMS(x, op)
  8440. #define DEF4_WMS(x, op, ...) DEF2_WMS(x, op)
  8441. #define EXDEF2_WMS(x, op, ...) \
  8442. CompileAssert(Js::OpCodeInfoAsmJs<Js::OpCodeAsmJs::op>::HasMultiSizeLayout); \
  8443. CompileAssert(Js::OpCodeInfoAsmJs<Js::OpCodeAsmJs::op>::IsExtendedOpcode);
  8444. #define EXDEF3_WMS(x, op, ...) EXDEF2_WMS(x, op)
  8445. #define EXDEF4_WMS(x, op, ...) EXDEF2_WMS(x, op)
  8446. #include "InterpreterHandlerAsmJs.inl"