InterpreterStackFrame.cpp 358 KB

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