InterpreterStackFrame.cpp 363 KB

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