InterpreterStackFrame.cpp 312 KB

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