InterpreterStackFrame.cpp 350 KB

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