InterpreterStackFrame.cpp 356 KB

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