InterpreterStackFrame.cpp 361 KB

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