LowerMD.cpp 288 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901
  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 "Backend.h"
  6. #include "Language/JavascriptFunctionArgIndex.h"
  7. const Js::OpCode LowererMD::MDUncondBranchOpcode = Js::OpCode::B;
  8. const Js::OpCode LowererMD::MDMultiBranchOpcode = Js::OpCode::BX;
  9. const Js::OpCode LowererMD::MDTestOpcode = Js::OpCode::TST;
  10. const Js::OpCode LowererMD::MDOrOpcode = Js::OpCode::ORR;
  11. const Js::OpCode LowererMD::MDXorOpcode = Js::OpCode::EOR;
  12. const Js::OpCode LowererMD::MDOverflowBranchOpcode = Js::OpCode::BVS;
  13. const Js::OpCode LowererMD::MDNotOverflowBranchOpcode = Js::OpCode::BVC;
  14. const Js::OpCode LowererMD::MDConvertFloat32ToFloat64Opcode = Js::OpCode::VCVTF64F32;
  15. const Js::OpCode LowererMD::MDConvertFloat64ToFloat32Opcode = Js::OpCode::VCVTF32F64;
  16. const Js::OpCode LowererMD::MDCallOpcode = Js::OpCode::Call;
  17. const Js::OpCode LowererMD::MDImulOpcode = Js::OpCode::MUL;
  18. const Js::OpCode LowererMD::MDLea = Js::OpCode::LEA;
  19. template<typename T>
  20. inline void Swap(T& x, T& y)
  21. {
  22. T temp = x;
  23. x = y;
  24. y = temp;
  25. }
  26. // Static utility fn()
  27. //
  28. bool
  29. LowererMD::IsAssign(const IR::Instr *instr)
  30. {
  31. return (instr->m_opcode == Js::OpCode::MOV ||
  32. instr->m_opcode == Js::OpCode::VMOV ||
  33. instr->m_opcode == Js::OpCode::LDIMM ||
  34. instr->m_opcode == Js::OpCode::LDR ||
  35. instr->m_opcode == Js::OpCode::VLDR ||
  36. instr->m_opcode == Js::OpCode::VLDR32 ||
  37. instr->m_opcode == Js::OpCode::STR ||
  38. instr->m_opcode == Js::OpCode::VSTR ||
  39. instr->m_opcode == Js::OpCode::VSTR32);
  40. }
  41. ///----------------------------------------------------------------------------
  42. ///
  43. /// LowererMD::IsCall
  44. ///
  45. ///----------------------------------------------------------------------------
  46. bool
  47. LowererMD::IsCall(const IR::Instr *instr)
  48. {
  49. return (instr->m_opcode == Js::OpCode::BL ||
  50. instr->m_opcode == Js::OpCode::BLX);
  51. }
  52. ///----------------------------------------------------------------------------
  53. ///
  54. /// LowererMD::IsIndirectBranch
  55. ///
  56. ///----------------------------------------------------------------------------
  57. bool
  58. LowererMD::IsIndirectBranch(const IR::Instr *instr)
  59. {
  60. return (instr->m_opcode == Js::OpCode::BX);
  61. }
  62. ///----------------------------------------------------------------------------
  63. ///
  64. /// LowererMD::IsUnconditionalBranch
  65. ///
  66. ///----------------------------------------------------------------------------
  67. bool
  68. LowererMD::IsUnconditionalBranch(const IR::Instr *instr)
  69. {
  70. return instr->m_opcode == Js::OpCode::B;
  71. }
  72. bool
  73. LowererMD::IsReturnInstr(const IR::Instr *instr)
  74. {
  75. return instr->m_opcode == Js::OpCode::LDRRET || instr->m_opcode == Js::OpCode::RET;
  76. }
  77. ///----------------------------------------------------------------------------
  78. ///
  79. /// LowererMD::InvertBranch
  80. ///
  81. ///----------------------------------------------------------------------------
  82. void
  83. LowererMD::InvertBranch(IR::BranchInstr *branchInstr)
  84. {
  85. switch (branchInstr->m_opcode)
  86. {
  87. case Js::OpCode::BEQ:
  88. branchInstr->m_opcode = Js::OpCode::BNE;
  89. break;
  90. case Js::OpCode::BNE:
  91. branchInstr->m_opcode = Js::OpCode::BEQ;
  92. break;
  93. case Js::OpCode::BGE:
  94. branchInstr->m_opcode = Js::OpCode::BLT;
  95. break;
  96. case Js::OpCode::BGT:
  97. branchInstr->m_opcode = Js::OpCode::BLE;
  98. break;
  99. case Js::OpCode::BLT:
  100. branchInstr->m_opcode = Js::OpCode::BGE;
  101. break;
  102. case Js::OpCode::BLE:
  103. branchInstr->m_opcode = Js::OpCode::BGT;
  104. break;
  105. case Js::OpCode::BCS:
  106. branchInstr->m_opcode = Js::OpCode::BCC;
  107. break;
  108. case Js::OpCode::BCC:
  109. branchInstr->m_opcode = Js::OpCode::BCS;
  110. break;
  111. case Js::OpCode::BMI:
  112. branchInstr->m_opcode = Js::OpCode::BPL;
  113. break;
  114. case Js::OpCode::BPL:
  115. branchInstr->m_opcode = Js::OpCode::BMI;
  116. break;
  117. case Js::OpCode::BVS:
  118. branchInstr->m_opcode = Js::OpCode::BVC;
  119. break;
  120. case Js::OpCode::BVC:
  121. branchInstr->m_opcode = Js::OpCode::BVS;
  122. break;
  123. case Js::OpCode::BLS:
  124. branchInstr->m_opcode = Js::OpCode::BHI;
  125. break;
  126. case Js::OpCode::BHI:
  127. branchInstr->m_opcode = Js::OpCode::BLS;
  128. break;
  129. default:
  130. AssertMsg(UNREACHED, "B missing in InvertBranch()");
  131. }
  132. }
  133. Js::OpCode
  134. LowererMD::MDConvertFloat64ToInt32Opcode(const RoundMode roundMode)
  135. {
  136. switch (roundMode)
  137. {
  138. case RoundModeTowardZero:
  139. return Js::OpCode::VCVTS32F64;
  140. case RoundModeTowardInteger:
  141. return Js::OpCode::Nop;
  142. case RoundModeHalfToEven:
  143. return Js::OpCode::VCVTRS32F64;
  144. default:
  145. AssertMsg(0, "RoundMode has no MD mapping.");
  146. return Js::OpCode::Nop;
  147. }
  148. }
  149. // GenerateMemRef: Return an opnd that can be used to access the given address.
  150. // ARM can't encode direct accesses to physical addresses, so put the address in a register
  151. // and return an indir. (This facilitates re-use of the loaded address without having to re-load it.)
  152. IR::Opnd *
  153. LowererMD::GenerateMemRef(intptr_t addr, IRType type, IR::Instr *instr, bool dontEncode)
  154. {
  155. IR::RegOpnd *baseOpnd = IR::RegOpnd::New(TyMachReg, this->m_func);
  156. IR::AddrOpnd *addrOpnd = IR::AddrOpnd::New(addr, IR::AddrOpndKindDynamicMisc, this->m_func, dontEncode);
  157. Lowerer::InsertMove(baseOpnd, addrOpnd, instr);
  158. return IR::IndirOpnd::New(baseOpnd, 0, type, this->m_func);
  159. }
  160. void
  161. LowererMD::FlipHelperCallArgsOrder()
  162. {
  163. int left = 0;
  164. int right = helperCallArgsCount - 1;
  165. while (left < right)
  166. {
  167. IR::Opnd *tempOpnd = helperCallArgs[left];
  168. helperCallArgs[left] = helperCallArgs[right];
  169. helperCallArgs[right] = tempOpnd;
  170. left++;
  171. right--;
  172. }
  173. }
  174. IR::Instr *
  175. LowererMD::LowerCallHelper(IR::Instr *instrCall)
  176. {
  177. IR::Opnd *argOpnd = instrCall->UnlinkSrc2();
  178. IR::Instr *prevInstr = instrCall;
  179. IR::JnHelperMethod helperMethod = instrCall->GetSrc1()->AsHelperCallOpnd()->m_fnHelper;
  180. instrCall->FreeSrc1();
  181. while (argOpnd)
  182. {
  183. Assert(argOpnd->IsRegOpnd());
  184. IR::RegOpnd *regArg = argOpnd->AsRegOpnd();
  185. Assert(regArg->m_sym->m_isSingleDef);
  186. IR::Instr *instrArg = regArg->m_sym->m_instrDef;
  187. Assert(instrArg->m_opcode == Js::OpCode::ArgOut_A || instrArg->m_opcode == Js::OpCode::ExtendArg_A &&
  188. (
  189. helperMethod == IR::JnHelperMethod::HelperOP_InitCachedScope ||
  190. helperMethod == IR::JnHelperMethod::HelperScrFunc_OP_NewScFuncHomeObj ||
  191. helperMethod == IR::JnHelperMethod::HelperScrFunc_OP_NewScGenFuncHomeObj ||
  192. helperMethod == IR::JnHelperMethod::HelperRestify ||
  193. helperMethod == IR::JnHelperMethod::HelperStPropIdArrFromVar
  194. ));
  195. prevInstr = this->LoadHelperArgument(prevInstr, instrArg->GetSrc1());
  196. argOpnd = instrArg->GetSrc2();
  197. if (instrArg->m_opcode == Js::OpCode::ArgOut_A)
  198. {
  199. instrArg->UnlinkSrc1();
  200. if (argOpnd)
  201. {
  202. instrArg->UnlinkSrc2();
  203. }
  204. regArg->Free(this->m_func);
  205. instrArg->Remove();
  206. }
  207. else if (instrArg->m_opcode == Js::OpCode::ExtendArg_A)
  208. {
  209. if (instrArg->GetSrc1()->IsRegOpnd())
  210. {
  211. m_lowerer->addToLiveOnBackEdgeSyms->Set(instrArg->GetSrc1()->AsRegOpnd()->GetStackSym()->m_id);
  212. }
  213. }
  214. }
  215. switch (helperMethod)
  216. {
  217. case IR::JnHelperMethod::HelperScrFunc_OP_NewScFuncHomeObj:
  218. case IR::JnHelperMethod::HelperScrFunc_OP_NewScGenFuncHomeObj:
  219. break;
  220. default:
  221. prevInstr = m_lowerer->LoadScriptContext(prevInstr);
  222. break;
  223. }
  224. this->FlipHelperCallArgsOrder();
  225. return this->ChangeToHelperCall(instrCall, helperMethod);
  226. }
  227. // Lower a call: May be either helper or native JS call. Just set the opcode, and
  228. // put the result into the return register. (No stack adjustment required.)
  229. IR::Instr *
  230. LowererMD::LowerCall(IR::Instr * callInstr, Js::ArgSlot argCount)
  231. {
  232. IR::Instr *retInstr = callInstr;
  233. IR::Opnd *targetOpnd = callInstr->GetSrc1();
  234. AssertMsg(targetOpnd, "Call without a target?");
  235. // This is required here due to calls created during lowering
  236. callInstr->m_func->SetHasCallsOnSelfAndParents();
  237. if (targetOpnd->IsRegOpnd())
  238. {
  239. // Indirect call
  240. callInstr->m_opcode = Js::OpCode::BLX;
  241. }
  242. else
  243. {
  244. AssertMsg(targetOpnd->IsHelperCallOpnd(), "Why haven't we loaded the call target?");
  245. // Direct call
  246. //
  247. // load the address into a register because we cannot directly access more than 24 bit constants
  248. // in BL instruction. Non helper call methods will already be accessed indirectly.
  249. //
  250. // Skip this for bailout calls. The register allocator will lower that as appropriate, without affecting spill choices.
  251. if (!callInstr->HasBailOutInfo())
  252. {
  253. IR::RegOpnd *regOpnd = IR::RegOpnd::New(nullptr, RegLR, TyMachPtr, this->m_func);
  254. IR::Instr *movInstr = IR::Instr::New(Js::OpCode::LDIMM, regOpnd, callInstr->GetSrc1(), this->m_func);
  255. regOpnd->m_isCallArg = true;
  256. callInstr->UnlinkSrc1();
  257. callInstr->SetSrc1(regOpnd);
  258. callInstr->InsertBefore(movInstr);
  259. }
  260. callInstr->m_opcode = Js::OpCode::BLX;
  261. }
  262. // For the sake of the prolog/epilog, note that we're not in a leaf. (Deliberately not
  263. // overloading Func::m_isLeaf here, as that's used for other purposes.)
  264. this->m_func->m_unwindInfo.SetHasCalls(true);
  265. IR::Opnd *dstOpnd = callInstr->GetDst();
  266. if (dstOpnd)
  267. {
  268. IR::Instr * movInstr;
  269. if(dstOpnd->IsFloat64())
  270. {
  271. movInstr = callInstr->SinkDst(Js::OpCode::VMOV);
  272. callInstr->GetDst()->AsRegOpnd()->SetReg(RETURN_DBL_REG);
  273. movInstr->GetSrc1()->AsRegOpnd()->SetReg(RETURN_DBL_REG);
  274. retInstr = movInstr;
  275. }
  276. else
  277. {
  278. movInstr = callInstr->SinkDst(Js::OpCode::MOV);
  279. callInstr->GetDst()->AsRegOpnd()->SetReg(RETURN_REG);
  280. movInstr->GetSrc1()->AsRegOpnd()->SetReg(RETURN_REG);
  281. retInstr = movInstr;
  282. }
  283. }
  284. //
  285. // assign the arguments to appropriate positions
  286. //
  287. AssertMsg(this->helperCallArgsCount >= 0, "Fatal. helper call arguments ought to be positive");
  288. AssertMsg(this->helperCallArgsCount <= MaxArgumentsToHelper, "Too many helper call arguments");
  289. uint16 argsLeft = this->helperCallArgsCount;
  290. uint16 doubleArgsLeft = this->helperCallDoubleArgsCount;
  291. uint16 intArgsLeft = argsLeft - doubleArgsLeft;
  292. while(argsLeft > 0)
  293. {
  294. IR::Opnd *helperArgOpnd = this->helperCallArgs[this->helperCallArgsCount - argsLeft];
  295. IR::Opnd * opndParam = nullptr;
  296. if (helperArgOpnd->IsFloat())
  297. {
  298. opndParam = this->GetOpndForArgSlot(doubleArgsLeft - 1, helperArgOpnd);
  299. AssertMsg(opndParam->IsRegOpnd(), "NYI for other kind of operands");
  300. --doubleArgsLeft;
  301. }
  302. else
  303. {
  304. opndParam = this->GetOpndForArgSlot(intArgsLeft - 1, helperArgOpnd);
  305. --intArgsLeft;
  306. }
  307. Lowerer::InsertMove(opndParam, helperArgOpnd, callInstr);
  308. --argsLeft;
  309. }
  310. Assert(doubleArgsLeft == 0 && intArgsLeft == 0 && argsLeft == 0);
  311. // We're done with the args (if any) now, so clear the param location state.
  312. this->FinishArgLowering();
  313. return retInstr;
  314. }
  315. IR::Instr *
  316. LowererMD::LoadDynamicArgument(IR::Instr *instr, uint argNumber)
  317. {
  318. Assert(instr->m_opcode == Js::OpCode::ArgOut_A_Dynamic);
  319. Assert(instr->GetSrc2() == nullptr);
  320. IR::Opnd* dst = GetOpndForArgSlot((Js::ArgSlot) (argNumber - 1));
  321. instr->SetDst(dst);
  322. instr->m_opcode = Js::OpCode::MOV;
  323. LegalizeMD::LegalizeInstr(instr);
  324. return instr;
  325. }
  326. IR::Instr *
  327. LowererMD::LoadDynamicArgumentUsingLength(IR::Instr *instr)
  328. {
  329. Assert(instr->m_opcode == Js::OpCode::ArgOut_A_Dynamic);
  330. IR::RegOpnd* src2 = instr->UnlinkSrc2()->AsRegOpnd();
  331. IR::Instr *add = IR::Instr::New(Js::OpCode::SUB, IR::RegOpnd::New(TyInt32, this->m_func), src2, IR::IntConstOpnd::New(1, TyInt8, this->m_func), this->m_func);
  332. instr->InsertBefore(add);
  333. //We need store nth actuals, so stack location is after function object, callinfo & this pointer
  334. IR::RegOpnd *stackPointer = IR::RegOpnd::New(nullptr, GetRegStackPointer(), TyMachReg, this->m_func);
  335. IR::IndirOpnd *actualsLocation = IR::IndirOpnd::New(stackPointer, add->GetDst()->AsRegOpnd(), GetDefaultIndirScale(), TyMachReg, this->m_func);
  336. instr->SetDst(actualsLocation);
  337. instr->m_opcode = Js::OpCode::LDR;
  338. LegalizeMD::LegalizeInstr(instr);
  339. return instr;
  340. }
  341. void
  342. LowererMD::SetMaxArgSlots(Js::ArgSlot actualCount /*including this*/)
  343. {
  344. Js::ArgSlot offset = 3;//For function object & callInfo & this
  345. if (this->m_func->m_argSlotsForFunctionsCalled < (uint32) (actualCount + offset))
  346. {
  347. this->m_func->m_argSlotsForFunctionsCalled = (uint32)(actualCount + offset);
  348. }
  349. return;
  350. }
  351. void
  352. LowererMD::GenerateMemInit(IR::RegOpnd * opnd, int32 offset, size_t value, IR::Instr * insertBeforeInstr, bool isZeroed)
  353. {
  354. m_lowerer->GenerateMemInit(opnd, offset, (uint32)value, insertBeforeInstr, isZeroed);
  355. }
  356. IR::Instr *
  357. LowererMD::LowerCallIDynamic(IR::Instr *callInstr, IR::Instr*saveThisArgOutInstr, IR::Opnd *argsLength, ushort callFlags, IR::Instr * insertBeforeInstrForCFG)
  358. {
  359. callInstr->InsertBefore(saveThisArgOutInstr); //Move this Argout next to call;
  360. this->LoadDynamicArgument(saveThisArgOutInstr, 3); //this pointer is the 3rd argument
  361. //callInfo
  362. if (callInstr->m_func->IsInlinee())
  363. {
  364. Assert(argsLength->AsIntConstOpnd()->GetValue() == callInstr->m_func->actualCount);
  365. this->SetMaxArgSlots((Js::ArgSlot)callInstr->m_func->actualCount);
  366. }
  367. else
  368. {
  369. callInstr->InsertBefore(IR::Instr::New(Js::OpCode::ADD, argsLength, argsLength, IR::IntConstOpnd::New(1, TyInt8, this->m_func), this->m_func));
  370. this->SetMaxArgSlots(Js::InlineeCallInfo::MaxInlineeArgoutCount);
  371. }
  372. Lowerer::InsertMove( this->GetOpndForArgSlot(1), argsLength, callInstr);
  373. IR::RegOpnd *funcObjOpnd = callInstr->UnlinkSrc1()->AsRegOpnd();
  374. GeneratePreCall(callInstr, funcObjOpnd);
  375. // functionOpnd is the first argument.
  376. IR::Opnd * opndParam = this->GetOpndForArgSlot(0);
  377. Lowerer::InsertMove(opndParam, funcObjOpnd, callInstr);
  378. return this->LowerCall(callInstr, 0);
  379. }
  380. void
  381. LowererMD::GenerateFunctionObjectTest(IR::Instr * callInstr, IR::RegOpnd *functionObjOpnd, bool isHelper, IR::LabelInstr* continueAfterExLabel /* = nullptr */)
  382. {
  383. AssertMsg(!m_func->IsJitInDebugMode() || continueAfterExLabel, "When jit is in debug mode, continueAfterExLabel must be provided otherwise continue after exception may cause AV.");
  384. if (!functionObjOpnd->IsNotTaggedValue())
  385. {
  386. IR::Instr * insertBeforeInstr = callInstr;
  387. // Need check and error if we are calling a tagged int.
  388. if (!functionObjOpnd->IsTaggedInt())
  389. {
  390. // TST functionObjOpnd, 1
  391. IR::Instr * instr = IR::Instr::New(Js::OpCode::TST, this->m_func);
  392. instr->SetSrc1(functionObjOpnd);
  393. instr->SetSrc2(IR::IntConstOpnd::New(Js::AtomTag, TyMachReg, this->m_func));
  394. callInstr->InsertBefore(instr);
  395. // BNE $helper
  396. // B $callLabel
  397. IR::LabelInstr * helperLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  398. instr = IR::BranchInstr::New(Js::OpCode::BNE, helperLabel, this->m_func);
  399. callInstr->InsertBefore(instr);
  400. IR::LabelInstr * callLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, isHelper);
  401. instr = IR::BranchInstr::New(Js::OpCode::B, callLabel, this->m_func);
  402. callInstr->InsertBefore(instr);
  403. callInstr->InsertBefore(helperLabel);
  404. callInstr->InsertBefore(callLabel);
  405. insertBeforeInstr = callLabel;
  406. }
  407. this->m_lowerer->GenerateRuntimeError(insertBeforeInstr, JSERR_NeedFunction);
  408. if (continueAfterExLabel)
  409. {
  410. // Under debugger the RuntimeError (exception) can be ignored, generate branch right after RunTimeError instr
  411. // to jmp to a safe place (which would normally be debugger bailout check).
  412. IR::BranchInstr* continueAfterEx = IR::BranchInstr::New(LowererMD::MDUncondBranchOpcode, continueAfterExLabel, this->m_func);
  413. insertBeforeInstr->InsertBefore(continueAfterEx);
  414. }
  415. }
  416. }
  417. IR::Instr*
  418. LowererMD::GeneratePreCall(IR::Instr * callInstr, IR::Opnd *functionObjOpnd)
  419. {
  420. IR::RegOpnd * functionTypeRegOpnd = nullptr;
  421. // For calls to fixed functions we load the function's type directly from the known (hard-coded) function object address.
  422. // For other calls, we need to load it from the function object stored in a register operand.
  423. if (functionObjOpnd->IsAddrOpnd() && functionObjOpnd->AsAddrOpnd()->m_isFunction)
  424. {
  425. functionTypeRegOpnd = this->m_lowerer->GenerateFunctionTypeFromFixedFunctionObject(callInstr, functionObjOpnd);
  426. }
  427. else if (functionObjOpnd->IsRegOpnd())
  428. {
  429. AssertMsg(functionObjOpnd->AsRegOpnd()->m_sym->IsStackSym(), "Expected call target to be stackSym");
  430. functionTypeRegOpnd = IR::RegOpnd::New(TyMachReg, this->m_func);
  431. IR::IndirOpnd* functionTypeIndirOpnd = IR::IndirOpnd::New(functionObjOpnd->AsRegOpnd(),
  432. Js::RecyclableObject::GetOffsetOfType(), TyMachReg, this->m_func);
  433. Lowerer::InsertMove(functionTypeRegOpnd, functionTypeIndirOpnd, callInstr);
  434. }
  435. else
  436. {
  437. AssertMsg(false, "Unexpected call target operand type.");
  438. }
  439. int entryPointOffset = Js::Type::GetOffsetOfEntryPoint();
  440. IR::IndirOpnd* entryPointOpnd = IR::IndirOpnd::New(functionTypeRegOpnd, entryPointOffset, TyMachPtr, this->m_func);
  441. IR::RegOpnd * targetAddrOpnd = IR::RegOpnd::New(TyMachReg, this->m_func);
  442. IR::Instr * stackParamInsert = Lowerer::InsertMove(targetAddrOpnd, entryPointOpnd, callInstr);
  443. // targetAddrOpnd is the address we'll call.
  444. callInstr->SetSrc1(targetAddrOpnd);
  445. return stackParamInsert;
  446. }
  447. IR::Instr *
  448. LowererMD::LowerCallI(IR::Instr * callInstr, ushort callFlags, bool isHelper, IR::Instr * insertBeforeInstrForCFG)
  449. {
  450. // Indirect call using JS calling convention:
  451. // R0 = callee func object
  452. // R1 = callinfo
  453. // R2 = arg0 ("this")
  454. // R3 = arg1
  455. // [sp] = arg2
  456. // etc.
  457. // First load the target address. Note that we want to wind up with this:
  458. // ...
  459. // [sp+4] = arg3
  460. // [sp] = arg2
  461. // load target addr from func obj
  462. // R3 = arg1
  463. // ...
  464. // R0 = func obj
  465. // BLX target addr
  466. // This way the register containing the target addr interferes with the param regs
  467. // only, not the regs we use to store params to the stack.
  468. // We're sinking the stores of stack params so that the call sequence is contiguous.
  469. // This is required by nested calls, since each call will re-use the same stack slots.
  470. // But if there is no nesting, stack params can be stored as soon as they're computed.
  471. IR::Opnd * functionObjOpnd = callInstr->UnlinkSrc1();
  472. // If this is a call for new, we already pass the function operand through NewScObject,
  473. // which checks if the function operand is a real function or not, don't need to add a check again.
  474. // If this is a call to a fixed function, we've already verified that the target is, indeed, a function.
  475. if (callInstr->m_opcode != Js::OpCode::CallIFixed && !(callFlags & Js::CallFlags_New))
  476. {
  477. IR::LabelInstr* continueAfterExLabel = Lowerer::InsertContinueAfterExceptionLabelForDebugger(m_func, callInstr, isHelper);
  478. GenerateFunctionObjectTest(callInstr, functionObjOpnd->AsRegOpnd(), isHelper, continueAfterExLabel);
  479. // TODO: Remove unreachable code if we have proved that it is a tagged in.
  480. }
  481. // Can't assert until we remove unreachable code if we have proved that it is a tagged int.
  482. // Assert((callFlags & Js::CallFlags_New) || !functionWrapOpnd->IsTaggedInt());
  483. IR::Instr * stackParamInsert = GeneratePreCall(callInstr, functionObjOpnd);
  484. // We need to get the calculated CallInfo in SimpleJit because that doesn't include any changes for stack alignment
  485. IR::IntConstOpnd *callInfo;
  486. int32 argCount = this->LowerCallArgs(callInstr, stackParamInsert, callFlags, 1, &callInfo);
  487. // functionObjOpnd is the first argument.
  488. IR::Opnd * opndParam = this->GetOpndForArgSlot(0);
  489. Lowerer::InsertMove(opndParam, functionObjOpnd, callInstr);
  490. IR::Opnd *const finalDst = callInstr->GetDst();
  491. // Finally, lower the call instruction itself.
  492. IR::Instr* ret = this->LowerCall(callInstr, (Js::ArgSlot)argCount);
  493. IR::AutoReuseOpnd autoReuseSavedFunctionObjOpnd;
  494. if (callInstr->IsJitProfilingInstr())
  495. {
  496. Assert(callInstr->m_func->IsSimpleJit());
  497. Assert(!CONFIG_FLAG(NewSimpleJit));
  498. if(finalDst &&
  499. finalDst->IsRegOpnd() &&
  500. functionObjOpnd->IsRegOpnd() &&
  501. finalDst->AsRegOpnd()->m_sym == functionObjOpnd->AsRegOpnd()->m_sym)
  502. {
  503. // The function object sym is going to be overwritten, so save it in a temp for profiling
  504. IR::RegOpnd *const savedFunctionObjOpnd = IR::RegOpnd::New(functionObjOpnd->GetType(), callInstr->m_func);
  505. autoReuseSavedFunctionObjOpnd.Initialize(savedFunctionObjOpnd, callInstr->m_func);
  506. Lowerer::InsertMove(savedFunctionObjOpnd, functionObjOpnd, callInstr->m_next);
  507. functionObjOpnd = savedFunctionObjOpnd;
  508. }
  509. auto instr = callInstr->AsJitProfilingInstr();
  510. ret = this->m_lowerer->GenerateCallProfiling(
  511. instr->profileId,
  512. instr->inlineCacheIndex,
  513. instr->GetDst(),
  514. functionObjOpnd,
  515. callInfo,
  516. instr->isProfiledReturnCall,
  517. callInstr,
  518. ret);
  519. }
  520. return ret;
  521. }
  522. int32
  523. LowererMD::LowerCallArgs(IR::Instr *callInstr, IR::Instr *stackParamInsert, ushort callFlags, Js::ArgSlot extraParams, IR::IntConstOpnd **callInfoOpndRef)
  524. {
  525. AssertMsg(this->helperCallArgsCount == 0, "We don't support nested helper calls yet");
  526. uint32 argCount = 0;
  527. IR::Opnd* opndParam;
  528. // Now walk the user arguments and remember the arg count.
  529. IR::Instr * argInstr = callInstr;
  530. IR::Opnd *src2Opnd = callInstr->UnlinkSrc2();
  531. while (src2Opnd->IsSymOpnd())
  532. {
  533. // Get the arg instr
  534. IR::SymOpnd * argLinkOpnd = src2Opnd->AsSymOpnd();
  535. StackSym * argLinkSym = argLinkOpnd->m_sym->AsStackSym();
  536. AssertMsg(argLinkSym->IsArgSlotSym() && argLinkSym->m_isSingleDef, "Arg tree not single def...");
  537. argLinkOpnd->Free(this->m_func);
  538. argInstr = argLinkSym->m_instrDef;
  539. // The arg sym isn't assigned a constant directly anymore
  540. argLinkSym->m_isConst = false;
  541. argLinkSym->m_isIntConst = false;
  542. argLinkSym->m_isTaggableIntConst = false;
  543. // The arg slot nums are 1-based, so subtract 1. Then add 1 for the non-user args (callinfo).
  544. auto argSlotNum = argLinkSym->GetArgSlotNum();
  545. if(argSlotNum + extraParams < argSlotNum)
  546. {
  547. Js::Throw::OutOfMemory();
  548. }
  549. opndParam = this->GetOpndForArgSlot(argSlotNum + extraParams);
  550. src2Opnd = argInstr->UnlinkSrc2();
  551. argInstr->ReplaceDst(opndParam);
  552. argInstr->Unlink();
  553. if (opndParam->IsRegOpnd())
  554. {
  555. callInstr->InsertBefore(argInstr);
  556. }
  557. else
  558. {
  559. stackParamInsert->InsertBefore(argInstr);
  560. }
  561. this->ChangeToAssign(argInstr);
  562. argCount++;
  563. }
  564. IR::RegOpnd * argLinkOpnd = src2Opnd->AsRegOpnd();
  565. StackSym *argLinkSym = argLinkOpnd->m_sym->AsStackSym();
  566. AssertMsg(!argLinkSym->IsArgSlotSym() && argLinkSym->m_isSingleDef, "Arg tree not single def...");
  567. IR::Instr *startCallInstr = argLinkSym->m_instrDef;
  568. AssertMsg(startCallInstr->m_opcode == Js::OpCode::StartCall || startCallInstr->m_opcode == Js::OpCode::LoweredStartCall, "Problem with arg chain.");
  569. AssertMsg(startCallInstr->GetArgOutCount(/*getInterpreterArgOutCount*/ false) == argCount,
  570. "ArgCount doesn't match StartCall count");
  571. // Deal with the SC.
  572. this->LowerStartCall(startCallInstr);
  573. // Second argument is the callinfo.
  574. IR::IntConstOpnd *opndCallInfo = Lowerer::MakeCallInfoConst(callFlags, argCount, m_func);
  575. if(callInfoOpndRef)
  576. {
  577. opndCallInfo->Use(m_func);
  578. *callInfoOpndRef = opndCallInfo;
  579. }
  580. opndParam = this->GetOpndForArgSlot(extraParams);
  581. Lowerer::InsertMove(opndParam, opndCallInfo, callInstr);
  582. return argCount + 1 + extraParams; // + 1 for call flags
  583. }
  584. IR::Instr *
  585. LowererMD::LowerStartCall(IR::Instr * instr)
  586. {
  587. // StartCall doesn't need to generate a stack adjustment. Just delete it.
  588. instr->m_opcode = Js::OpCode::LoweredStartCall;
  589. return instr;
  590. }
  591. IR::Instr *
  592. LowererMD::LoadHelperArgument(IR::Instr * instr, IR::Opnd * opndArgValue)
  593. {
  594. // Load the given parameter into the appropriate location.
  595. // We update the current param state so we can do this work without making the caller
  596. // do the work.
  597. Assert(this->helperCallArgsCount < LowererMD::MaxArgumentsToHelper);
  598. __analysis_assume(this->helperCallArgsCount < MaxArgumentsToHelper);
  599. helperCallArgs[helperCallArgsCount++] = opndArgValue;
  600. if (opndArgValue->GetType() == TyMachDouble)
  601. {
  602. this->helperCallDoubleArgsCount++;
  603. }
  604. return instr;
  605. }
  606. void
  607. LowererMD::FinishArgLowering()
  608. {
  609. this->helperCallArgsCount = 0;
  610. this->helperCallDoubleArgsCount = 0;
  611. }
  612. IR::Opnd *
  613. LowererMD::GetOpndForArgSlot(Js::ArgSlot argSlot, IR::Opnd * argOpnd)
  614. {
  615. IR::Opnd * opndParam = nullptr;
  616. IRType type = argOpnd ? argOpnd->GetType() : TyMachReg;
  617. if (argOpnd == nullptr || !argOpnd->IsFloat())
  618. {
  619. if (argSlot < NUM_INT_ARG_REGS)
  620. {
  621. // Return an instance of the next arg register.
  622. IR::RegOpnd *regOpnd;
  623. regOpnd = IR::RegOpnd::New(nullptr, (RegNum)(argSlot + FIRST_INT_ARG_REG), type, this->m_func);
  624. regOpnd->m_isCallArg = true;
  625. opndParam = regOpnd;
  626. }
  627. else
  628. {
  629. // Create a stack slot reference and bump up the size of this function's outgoing param area,
  630. // if necessary.
  631. argSlot = argSlot - NUM_INT_ARG_REGS;
  632. IntConstType offset = argSlot * MachRegInt;
  633. IR::RegOpnd * spBase = IR::RegOpnd::New(nullptr, this->GetRegStackPointer(), TyMachReg, this->m_func);
  634. opndParam = IR::IndirOpnd::New(spBase, offset, type, this->m_func);
  635. if (this->m_func->m_argSlotsForFunctionsCalled < (uint32)(argSlot + 1))
  636. {
  637. this->m_func->m_argSlotsForFunctionsCalled = argSlot + 1;
  638. }
  639. }
  640. }
  641. else
  642. {
  643. if (argSlot < MaxDoubleArgumentsToHelper)
  644. {
  645. // Return an instance of the next arg register.
  646. IR::RegOpnd *regOpnd;
  647. regOpnd = IR::RegOpnd::New(nullptr, (RegNum)(argSlot + FIRST_DOUBLE_ARG_REG), type, this->m_func);
  648. regOpnd->m_isCallArg = true;
  649. opndParam = regOpnd;
  650. }
  651. else
  652. {
  653. AssertMsg(false,"More than 8 double parameter passing disallowed");
  654. }
  655. }
  656. return opndParam;
  657. }
  658. IR::Instr *
  659. LowererMD::LoadDoubleHelperArgument(IR::Instr * instr, IR::Opnd * opndArg)
  660. {
  661. // Load the given parameter into the appropriate location.
  662. // We update the current param state so we can do this work without making the caller
  663. // do the work.
  664. Assert(opndArg->GetType() == TyMachDouble);
  665. return this->LoadHelperArgument(instr, opndArg);
  666. }
  667. void
  668. LowererMD::GenerateStackProbe(IR::Instr *insertInstr, bool afterProlog)
  669. {
  670. //
  671. // Generate a stack overflow check. This can be as simple as a cmp esp, const
  672. // because this function is guaranteed to be called on its base thread only.
  673. // If the check fails call ThreadContext::ProbeCurrentStack which will check again and must throw.
  674. //
  675. // LDIMM r12, ThreadContext::scriptStackLimit + frameSize //Load to register first, as this can be more than 12 bit supported in CMP
  676. // CMP sp, r12
  677. // BGT done
  678. // begin:
  679. // LDIMM r0, frameSize
  680. // LDIMM r1, scriptContext
  681. // LDIMM r2, ThreadContext::ProbeCurrentStack //MUST THROW
  682. // BLX r2 //BX r2 if the stackprobe is before prolog
  683. // done:
  684. //
  685. // For thread context with script interrupt enabled:
  686. // LDIMM r12, &ThreadContext::scriptStackLimitForCurrentThread
  687. // LDR r12, [r12]
  688. // ADD r12, frameSize
  689. // BVS $helper
  690. // CMP sp, r12
  691. // BGT done
  692. // $helper:
  693. // LDIMM r0, frameSize
  694. // LDIMM r1, scriptContext
  695. // LDIMM r2, ThreadContext::ProbeCurrentStack //MUST THROW
  696. // BLX r2 //BX r2 if the stackprobe is before prolog
  697. // done:
  698. //
  699. //m_localStackHeight for ARM contains (m_argSlotsForFunctionsCalled * MachPtr)
  700. uint32 frameSize = this->m_func->m_localStackHeight + Js::Constants::MinStackJIT;
  701. IR::RegOpnd *scratchOpnd = IR::RegOpnd::New(nullptr, SCRATCH_REG, TyMachReg, this->m_func);
  702. IR::LabelInstr *helperLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, afterProlog);
  703. IR::Instr *instr;
  704. bool doInterruptProbe = m_func->GetJITFunctionBody()->DoInterruptProbe();
  705. if (doInterruptProbe || !m_func->GetThreadContextInfo()->IsThreadBound())
  706. {
  707. // Load the current stack limit and add the current frame allocation.
  708. {
  709. intptr_t pLimit = m_func->GetThreadContextInfo()->GetThreadStackLimitAddr();
  710. Lowerer::InsertMove(scratchOpnd, IR::AddrOpnd::New(pLimit, IR::AddrOpndKindDynamicMisc, this->m_func), insertInstr);
  711. Lowerer::InsertMove(scratchOpnd, IR::IndirOpnd::New(scratchOpnd, 0, TyMachReg, this->m_func), insertInstr);
  712. }
  713. if (EncoderMD::CanEncodeModConst12(frameSize))
  714. {
  715. // If the frame size is small enough, just add the constant.
  716. // Does this ever happen with the size of the MinStackJIT constant?
  717. instr = IR::Instr::New(Js::OpCode::ADDS, scratchOpnd, scratchOpnd,
  718. IR::IntConstOpnd::New(frameSize, TyMachReg, this->m_func), this->m_func);
  719. insertInstr->InsertBefore(instr);
  720. }
  721. else
  722. {
  723. // We need a second scratch reg.
  724. // If we're probing after the prolog, the reg has already been saved and will be restored.
  725. // If not, push and pop it here, knowing that we'll never throw while the stack is whacked.
  726. Assert(!afterProlog || this->m_func->m_unwindInfo.GetSavedScratchReg());
  727. BVUnit scratchBit;
  728. IR::Opnd *opnd;
  729. if (!afterProlog)
  730. {
  731. opnd = IR::IndirOpnd::New(IR::RegOpnd::New(nullptr, RegSP, TyMachReg, this->m_func), (int32)0, TyMachReg, this->m_func);
  732. instr = IR::Instr::New(Js::OpCode::PUSH, opnd, this->m_func);
  733. scratchBit.Set(RegEncode[SP_ALLOC_SCRATCH_REG]);
  734. opnd = IR::RegBVOpnd::New(scratchBit, TyMachReg, this->m_func);
  735. instr->SetSrc1(opnd);
  736. insertInstr->InsertBefore(instr);
  737. }
  738. IR::Opnd *scratchOpnd2 = IR::RegOpnd::New(nullptr, SP_ALLOC_SCRATCH_REG, TyMachReg, this->m_func);
  739. Lowerer::InsertMove(scratchOpnd2, IR::IntConstOpnd::New(frameSize, TyMachReg, this->m_func), insertInstr);
  740. instr = IR::Instr::New(Js::OpCode::ADDS, scratchOpnd, scratchOpnd, scratchOpnd2, this->m_func);
  741. insertInstr->InsertBefore(instr);
  742. if (!afterProlog)
  743. {
  744. Assert(scratchBit.Test(RegEncode[SP_ALLOC_SCRATCH_REG]));
  745. opnd = IR::RegBVOpnd::New(scratchBit, TyMachReg, this->m_func);
  746. instr = IR::Instr::New(Js::OpCode::POP, opnd, this->m_func);
  747. opnd = IR::IndirOpnd::New(IR::RegOpnd::New(nullptr, RegSP, TyMachReg, this->m_func), (int32)0, TyMachReg, this->m_func);
  748. instr->SetSrc1(opnd);
  749. insertInstr->InsertBefore(instr);
  750. }
  751. }
  752. // If this add overflows, we have to call the helper.
  753. instr = IR::BranchInstr::New(Js::OpCode::BVS, helperLabel, this->m_func);
  754. insertInstr->InsertBefore(instr);
  755. }
  756. else
  757. {
  758. uint32 scriptStackLimit = (uint32)m_func->GetThreadContextInfo()->GetScriptStackLimit();
  759. IR::Opnd *stackLimitOpnd = IR::IntConstOpnd::New(frameSize + scriptStackLimit, TyMachReg, this->m_func);
  760. Lowerer::InsertMove(scratchOpnd, stackLimitOpnd, insertInstr);
  761. }
  762. IR::LabelInstr *doneLabelInstr = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, false);
  763. if (!IS_FAULTINJECT_STACK_PROBE_ON) // Do stack check fastpath only if not doing StackProbe fault injection
  764. {
  765. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  766. instr->SetSrc1(IR::RegOpnd::New(nullptr, GetRegStackPointer(), TyMachReg, this->m_func));
  767. instr->SetSrc2(scratchOpnd);
  768. insertInstr->InsertBefore(instr);
  769. instr = IR::BranchInstr::New(Js::OpCode::BGT, doneLabelInstr, this->m_func);
  770. insertInstr->InsertBefore(instr);
  771. }
  772. insertInstr->InsertBefore(helperLabel);
  773. // Zero out the pointer to the list of stack nested funcs, since the functions won't be initialized on this path.
  774. scratchOpnd = IR::RegOpnd::New(nullptr, RegR0, TyMachReg, m_func);
  775. IR::RegOpnd *frameReg = IR::RegOpnd::New(nullptr, GetRegFramePointer(), TyMachReg, m_func);
  776. Lowerer::InsertMove(scratchOpnd, IR::IntConstOpnd::New(0, TyMachReg, m_func), insertInstr);
  777. IR::Opnd *indirOpnd = IR::IndirOpnd::New(
  778. frameReg, -(int32)(Js::Constants::StackNestedFuncList * sizeof(Js::Var)), TyMachReg, m_func);
  779. Lowerer::InsertMove(indirOpnd, scratchOpnd, insertInstr);
  780. IR::RegOpnd *r0Opnd = IR::RegOpnd::New(nullptr, RegR0, TyMachReg, this->m_func);
  781. Lowerer::InsertMove(r0Opnd, IR::IntConstOpnd::New(frameSize, TyMachReg, this->m_func, true), insertInstr);
  782. IR::RegOpnd *r1Opnd = IR::RegOpnd::New(nullptr, RegR1, TyMachReg, this->m_func);
  783. Lowerer::InsertMove(r1Opnd, this->m_lowerer->LoadScriptContextOpnd(insertInstr), insertInstr);
  784. IR::RegOpnd *r2Opnd = IR::RegOpnd::New(nullptr, RegR2, TyMachReg, m_func);
  785. Lowerer::InsertMove(r2Opnd, IR::HelperCallOpnd::New(IR::HelperProbeCurrentStack, this->m_func), insertInstr);
  786. instr = IR::Instr::New(afterProlog? Js::OpCode::BLX : Js::OpCode::BX, this->m_func);
  787. instr->SetSrc1(r2Opnd);
  788. insertInstr->InsertBefore(instr);
  789. insertInstr->InsertBefore(doneLabelInstr);
  790. Security::InsertRandomFunctionPad(doneLabelInstr);
  791. }
  792. //
  793. // Emits the code to allocate 'size' amount of space on stack. for values smaller than PAGE_SIZE
  794. // this will just emit sub rsp,size otherwise calls _chkstk.
  795. //
  796. bool
  797. LowererMD::GenerateStackAllocation(IR::Instr *instr, uint32 allocSize, uint32 probeSize)
  798. {
  799. IR::RegOpnd * spOpnd = IR::RegOpnd::New(nullptr, GetRegStackPointer(), TyMachReg, this->m_func);
  800. if (IsSmallStack(probeSize))
  801. {
  802. AssertMsg(!(allocSize & 0xFFFFF000), "Must fit in 12 bits");
  803. // Generate SUB SP, SP, stackSize
  804. IR::IntConstOpnd * stackSizeOpnd = IR::IntConstOpnd::New(allocSize, TyMachReg, this->m_func, true);
  805. IR::Instr * subInstr = IR::Instr::New(Js::OpCode::SUB, spOpnd, spOpnd, stackSizeOpnd, this->m_func);
  806. instr->InsertBefore(subInstr);
  807. return false;
  808. }
  809. //__chkStk is a leaf function and hence alignment is not required.
  810. // Generate _chkstk call
  811. // LDIMM RegR4, stackSize/4 //input: r4 = the number of WORDS (word = 4 bytes) to allocate,
  812. // LDIMM RegR12, HelperCRT_chkstk
  813. // BLX RegR12
  814. // SUB SP, SP, RegR4 //output: r4 = total number of BYTES probed/allocated.
  815. //chkstk expects the stacksize argument in R4 register
  816. IR::RegOpnd *r4Opnd = IR::RegOpnd::New(nullptr, SP_ALLOC_SCRATCH_REG, TyMachReg, this->m_func);
  817. IR::RegOpnd *targetOpnd = IR::RegOpnd::New(nullptr, SCRATCH_REG, TyMachReg, this->m_func);
  818. IR::IntConstOpnd * stackSizeOpnd = IR::IntConstOpnd::New((allocSize/MachPtr), TyMachReg, this->m_func, true);
  819. IR::Instr *movInstr = IR::Instr::New(Js::OpCode::LDIMM, r4Opnd, stackSizeOpnd, this->m_func);
  820. instr->InsertBefore(movInstr);
  821. IR::Instr *movHelperAddrInstr = IR::Instr::New(Js::OpCode::LDIMM, targetOpnd, IR::HelperCallOpnd::New(IR::HelperCRT_chkstk, this->m_func), this->m_func);
  822. instr->InsertBefore(movHelperAddrInstr);
  823. IR::Instr * callInstr = IR::Instr::New(Js::OpCode::BLX, r4Opnd, targetOpnd, this->m_func);
  824. instr->InsertBefore(callInstr);
  825. // Generate SUB SP, SP, R4
  826. IR::Instr * subInstr = IR::Instr::New(Js::OpCode::SUB, spOpnd, spOpnd, r4Opnd, this->m_func);
  827. instr->InsertBefore(subInstr);
  828. // return true to imply scratch register is trashed
  829. return true;
  830. }
  831. void
  832. LowererMD::GenerateStackDeallocation(IR::Instr *instr, uint32 allocSize)
  833. {
  834. IR::RegOpnd * spOpnd = IR::RegOpnd::New(nullptr, this->GetRegStackPointer(), TyMachReg, this->m_func);
  835. IR::Instr * spAdjustInstr = IR::Instr::New(Js::OpCode::ADD,
  836. spOpnd,
  837. spOpnd,
  838. IR::IntConstOpnd::New(allocSize, TyMachReg, this->m_func, true), this->m_func);
  839. instr->InsertBefore(spAdjustInstr);
  840. LegalizeMD::LegalizeInstr(spAdjustInstr);
  841. }
  842. //------------------------------------------------------------------------------------------
  843. //
  844. // Prologs and epilogs on ARM:
  845. //
  846. // 1. Normal non-leaf function:
  847. //
  848. // MOV r12,0 -- prepare to clear the arg obj slot (not in prolog or pdata)
  849. // $PrologStart:
  850. // PUSH {r0-r3} -- home parameters (homes only r0-r1 for global function, r2 as well for eval with "this"
  851. // PUSH {r11,lr} -- save frame pointer and return address
  852. // MOV r11,sp -- set up frame chain (r11 points to saved r11)
  853. // PUSH {r4-r10,r12} -- save non-volatile regs (only used), clear arg obj slot
  854. // VPUSH {d8-d15} -- save non-volatile double regs (only used)
  855. // SUB sp, stack -- allocate locals and arg out area
  856. // <probe stack> -- not in prolog
  857. // ...
  858. // ADD sp, stack -- deallocate locals and args
  859. // POP {r4-r10,r12} -- restore registers
  860. // POP {r11} -- restore frame pointer
  861. // LDR pc,[sp],#20 -- load return address into pc and deallocate remaining stack
  862. // $EpilogEnd:
  863. //
  864. // 2. Function with large stack
  865. //
  866. // <probe stack> -- not in prolog
  867. // MOV r12,0
  868. // $PrologStart:
  869. // <save params and regs, set up frame chain as above>
  870. // MOV r4, stack/4 -- input param to chkstk is a DWORD count
  871. // LDIMM r12, &chkstk
  872. // BLX r12
  873. // SUB sp, r4 -- byte count returned by chkstk in r4
  874. // ...
  875. // <epilog as above>
  876. //
  877. // 3. Function with try-catch-finally
  878. //
  879. // MOV r12,0
  880. // $PrologStart:
  881. // PUSH {r0-r3}
  882. // PUSH {r11,lr}
  883. // MOV r11,sp
  884. // PUSH {r4-r10,r12}
  885. // MOV r6,sp -- save pointer to the saved regs
  886. // SUB sp, locals -- allocate locals area only
  887. // MOV r7,sp -- set up locals pointer; all accesses to locals in the body are through r7
  888. // PUSH {r6} -- store the saved regs pointer on the stack
  889. // SUB sp, args -- allocate space for out args passed on stack
  890. // ...
  891. // ADD sp, args
  892. // POP {r6} -- load the saved regs pointer
  893. // MOV sp,r6 -- restore sp to the saved regs area
  894. // POP {r4-r10,r12}
  895. // POP {r11}
  896. // LDR pc,[sp],#20
  897. // $EpilogEnd:
  898. IR::Instr *
  899. LowererMD::LowerEntryInstr(IR::EntryInstr * entryInstr)
  900. {
  901. IR::Instr *insertInstr = entryInstr->m_next;
  902. BYTE regEncode;
  903. BOOL hasTry = this->m_func->HasTry();
  904. // Begin recording info for later pdata/xdata emission.
  905. UnwindInfoManager *unwindInfo = &this->m_func->m_unwindInfo;
  906. unwindInfo->Init(this->m_func);
  907. // WRT CheckAlignment:
  908. // - The code commented out below (which seems to be copied from x86) causes a hang: it trashes LR to make the call.
  909. // - Ideally, we could save R0-R3, L11, LR to stack (R0-R3 can potentially be trashed + make sure to keep 8 byte alignment)
  910. // then call the HelperScrFunc_CheckAlignment which should take 1 argument:
  911. // whether it's leaf (should be 4 byte aligned) or non-leaf function (should be 8-byte aligned),
  912. // then restore R0-R3, R11, LR from the stack.
  913. // - But since on ARM currently the helper doesn't do anything, let's just comment this code out.
  914. // - On x86 there is no LR and all args go to stack, that's why similar code works fine.
  915. //#ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  916. // if (Js::Configuration::Global.flags.IsEnabled(Js::CheckAlignmentFlag))
  917. // {
  918. // IR::Instr * callInstr = IR::Instr::New(Js::OpCode::Call, this->m_func);
  919. // callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::HelperScrFunc_CheckAlignment, this->m_func));
  920. // insertInstr->InsertBefore(callInstr);
  921. //
  922. // this->LowerCall(callInstr, 0);
  923. // }
  924. //#endif
  925. //First calculate the local stack
  926. if (this->m_func->HasInlinee())
  927. {
  928. // Allocate the inlined arg out stack in the locals. Allocate an additional slot so that
  929. // we can unconditionally clear the first slot past the current frame.
  930. this->m_func->m_localStackHeight += this->m_func->GetInlineeArgumentStackSize();
  931. }
  932. if (hasTry)
  933. {
  934. // If there's a try in the function, then the locals area must be 8-byte-aligned. That's because
  935. // the main function will allocate a locals area, and the try helper will allocate the same stack
  936. // but without a locals area, and both must be 8-byte aligned. So adding the locals area can't change
  937. // the alignment.
  938. this->m_func->m_localStackHeight = Math::Align<int32>(this->m_func->m_localStackHeight, MachStackAlignment);
  939. }
  940. int32 stackAdjust = this->m_func->m_localStackHeight + (this->m_func->m_argSlotsForFunctionsCalled * MachPtr);
  941. if (stackAdjust != 0)
  942. {
  943. //We might need to call ProbeStack or __chkstk hence mark this function as hasCalls
  944. unwindInfo->SetHasCalls(true);
  945. }
  946. bool hasStackNestedFuncList = false;
  947. // We need to have the same register saves in the prolog as the arm_CallEhFrame, so that we can use the same
  948. // epilog. So always allocate a slot for the stack nested func here whether we actually do have any stack
  949. // nested func or not
  950. // TODO-STACK-NESTED-FUNC: May be use a different arm_CallEhFrame for when we have stack nested func?
  951. if (this->m_func->HasAnyStackNestedFunc() || hasTry)
  952. {
  953. // Just force it to have calls if we have stack nested func so we have a stable
  954. // location for the stack nested function list
  955. hasStackNestedFuncList = true;
  956. unwindInfo->SetHasCalls(true);
  957. }
  958. if (Lowerer::IsArgSaveRequired(this->m_func))
  959. {
  960. unwindInfo->SetHasCalls(true);
  961. }
  962. bool hasCalls = unwindInfo->GetHasCalls();
  963. // Home the params. This is done to enable on-the-fly creation of the arguments object,
  964. // Dyno bailout code, etc. For non-global functions, that means homing all the param registers
  965. // (since we have to assume they all have valid parameters). For the global function,
  966. // just home r0 (function object) and r1 (callinfo), which the runtime can't get by any other means.
  967. int32 regSaveArea = 0;
  968. BVUnit paramRegs;
  969. int homedParamRegCount;
  970. // Note: home all the param registers if there's a try, because that's what the try helpers do.
  971. if (this->m_func->IsLoopBody() && !hasTry)
  972. {
  973. // Jitted loop body takes only one "user" param: the pointer to the local slots.
  974. homedParamRegCount = MIN_HOMED_PARAM_REGS + 1;
  975. Assert(homedParamRegCount <= NUM_INT_ARG_REGS);
  976. }
  977. else if (!hasCalls)
  978. {
  979. // A leaf function (no calls of any kind, including helpers) may still need its params, or, if it
  980. // has none, may still need the function object and call info.
  981. homedParamRegCount = MIN_HOMED_PARAM_REGS + this->m_func->GetInParamsCount();
  982. if (homedParamRegCount > NUM_INT_ARG_REGS)
  983. {
  984. homedParamRegCount = NUM_INT_ARG_REGS;
  985. }
  986. }
  987. else
  988. {
  989. homedParamRegCount = NUM_INT_ARG_REGS;
  990. }
  991. Assert((BYTE)homedParamRegCount == homedParamRegCount);
  992. unwindInfo->SetHomedParamCount((BYTE)homedParamRegCount);
  993. for (int i = 0; i < homedParamRegCount; i++)
  994. {
  995. RegNum reg = (RegNum)(FIRST_INT_ARG_REG + i);
  996. paramRegs.Set(RegEncode[reg]);
  997. regSaveArea += MachRegInt;
  998. }
  999. // Record used callee-saved registers. This is in the form of a fixed bitfield.
  1000. BVUnit usedRegs;
  1001. int32 fpOffsetSize = 0;
  1002. for (RegNum reg = FIRST_CALLEE_SAVED_GP_REG; reg <= LAST_CALLEE_SAVED_GP_REG; reg = (RegNum)(reg+1))
  1003. {
  1004. Assert(LinearScan::IsCalleeSaved(reg));
  1005. Assert(reg != RegLR);
  1006. // Save all the regs if there's a try, because that's what the try helpers have to do.
  1007. if (this->m_func->m_regsUsed.Test(reg) || hasTry)
  1008. {
  1009. regEncode = RegEncode[reg];
  1010. usedRegs.Set(regEncode);
  1011. unwindInfo->SetSavedReg(regEncode);
  1012. fpOffsetSize += MachRegInt;
  1013. }
  1014. }
  1015. BVUnit32 usedDoubleRegs;
  1016. short doubleRegCount = 0;
  1017. if (!hasTry)
  1018. {
  1019. for (RegNum reg = FIRST_CALLEE_SAVED_DBL_REG; reg <= LAST_CALLEE_SAVED_DBL_REG; reg = (RegNum)(reg+1))
  1020. {
  1021. Assert(LinearScan::IsCalleeSaved(reg));
  1022. if (this->m_func->m_regsUsed.Test(reg))
  1023. {
  1024. regEncode = RegEncode[reg] - RegEncode[RegD0];
  1025. usedDoubleRegs.Set(regEncode);
  1026. doubleRegCount++;
  1027. }
  1028. }
  1029. if (doubleRegCount)
  1030. {
  1031. BYTE lastDoubleReg = UnwindInfoManager::GetLastSavedReg(usedDoubleRegs.GetWord());
  1032. BYTE firstDoubleReg = UnwindInfoManager::GetFirstSavedReg(usedDoubleRegs.GetWord());
  1033. // We do want to push all the double registers in a single VPUSH instructions
  1034. // This might cause us to VPUSH some registers which are not used
  1035. // But this makes unwind & prolog simple. But if we do see this case a lot
  1036. // then consider adding multiple VPUSH
  1037. short count = lastDoubleReg - firstDoubleReg + 1;
  1038. //Register allocator can allocate a temp reg from the other end of the bit vector so that it can keep it live for longer.
  1039. //Hence count may not be equal to doubleRegCount in all scenarios. These are rare and hence its okay to use single VPUSH instruction.
  1040. //handle these scenarios for free builds
  1041. usedDoubleRegs.SetRange(firstDoubleReg, count);
  1042. doubleRegCount = count;
  1043. }
  1044. }
  1045. else
  1046. {
  1047. // Set for all the callee saved double registers
  1048. usedDoubleRegs.SetRange(RegD8-RegD0, CALLEE_SAVED_DOUBLE_REG_COUNT);
  1049. doubleRegCount = CALLEE_SAVED_DOUBLE_REG_COUNT;
  1050. }
  1051. if (doubleRegCount)
  1052. {
  1053. unwindInfo->SetDoubleSavedRegList(usedDoubleRegs.GetWord());
  1054. fpOffsetSize += (doubleRegCount * MachRegDouble);
  1055. //When there is try-catch we allocate registers even if there are no calls. For scenarios see Win8 487030.
  1056. //This seems to be overkill but consistent with int registers.
  1057. AssertMsg(hasCalls || hasTry, "Assigned double registers without any calls?");
  1058. //Anyway handle it for free builds
  1059. if (!hasCalls)
  1060. {
  1061. this->m_func->m_unwindInfo.SetHasCalls(true);
  1062. hasCalls = true;
  1063. }
  1064. }
  1065. regSaveArea += fpOffsetSize;
  1066. if (hasTry)
  1067. {
  1068. // Account for the saved SP on the stack.
  1069. regSaveArea += MachRegInt;
  1070. }
  1071. this->m_func->m_ArgumentsOffset = fpOffsetSize;
  1072. if (hasStackNestedFuncList)
  1073. {
  1074. // use r11 it allocate one more slot in the register save area
  1075. // We will zero it later
  1076. regEncode = RegEncode[RegR11];
  1077. usedRegs.Set(regEncode);
  1078. unwindInfo->SetSavedReg(regEncode);
  1079. regSaveArea += MachRegInt;
  1080. fpOffsetSize += MachRegInt;
  1081. this->m_func->m_ArgumentsOffset += MachRegInt;
  1082. }
  1083. // NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
  1084. //
  1085. // If you change this->m_func->m_localStackHeight after the following code you MUST take that
  1086. // into account below. Otherwise, the stack will become unbalanced or corrupted.
  1087. //
  1088. // NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
  1089. DWORD stackProbeStackHeight = this->m_func->m_localStackHeight;
  1090. // If we've already got calls and we don't have a try, we need to take adjustments
  1091. // below into account to determine whether our not our final stack height is going to be
  1092. // encodable. We're not going to take into account the adjustment for saving R4, because we're
  1093. // trying to figure out if we will be able to encode if we DON'T save it. If we save it anyway,
  1094. // the point is moot.
  1095. if (hasCalls && !hasTry)
  1096. {
  1097. int32 bytesOnStack = stackAdjust + regSaveArea + 3 * MachRegInt;
  1098. int32 alignPad = Math::Align<int32>(bytesOnStack, MachStackAlignment) - bytesOnStack;
  1099. if (alignPad)
  1100. {
  1101. stackProbeStackHeight += alignPad;
  1102. }
  1103. }
  1104. bool useDynamicStackProbe =
  1105. (m_func->GetJITFunctionBody()->DoInterruptProbe() || !m_func->GetThreadContextInfo()->IsThreadBound()) &&
  1106. !EncoderMD::CanEncodeModConst12(stackProbeStackHeight + Js::Constants::MinStackJIT);
  1107. if (useDynamicStackProbe && !hasCalls)
  1108. {
  1109. this->m_func->m_unwindInfo.SetHasCalls(true);
  1110. hasCalls = true;
  1111. }
  1112. if (hasCalls)
  1113. {
  1114. //If we need a dedicated arguments slot we mark R12 as the save register.
  1115. //This is to imitate PUSH 0 to arguments slot.
  1116. regEncode = RegEncode[SCRATCH_REG];
  1117. usedRegs.Set(regEncode);
  1118. unwindInfo->SetSavedReg(regEncode);
  1119. //Update register save area and offset to actual in params
  1120. //account for r12 push - MachRegInt
  1121. //account for frame register setup push {r11,lr} - 2 * MachRegInt
  1122. regSaveArea += 3 * MachRegInt;
  1123. this->m_func->m_ArgumentsOffset += 3 * MachRegInt;
  1124. //Note: Separate push instruction is generated for r11 & lr push and hence usedRegs mask is not updated with
  1125. //bit mask for these registers.
  1126. if (!IsSmallStack(stackAdjust) || useDynamicStackProbe)
  1127. {
  1128. unwindInfo->SetSavedScratchReg(true);
  1129. if (!usedRegs.Test(RegEncode[SP_ALLOC_SCRATCH_REG])) //If its a large stack and RegR4 is not already saved.
  1130. {
  1131. // If it is not a small stack we have to call __chkstk.
  1132. // __chkstk has special calling convention and trashes R4
  1133. // And if we're probing the stack dynamically, we need an extra reg to do the frame size calculation.
  1134. //
  1135. // Note that it's possible that we now no longer need a dynamic stack probe because
  1136. // m_localStackHeight may be encodable in Mod12. However, this is a chicken-and-egg
  1137. // problem, so we're going to stick with saving R4 even though it's possible it
  1138. // won't get modified.
  1139. usedRegs.Set(RegEncode[SP_ALLOC_SCRATCH_REG]);
  1140. regSaveArea += MachRegInt;
  1141. fpOffsetSize += MachRegInt;
  1142. this->m_func->m_ArgumentsOffset += MachRegInt;
  1143. unwindInfo->SetSavedReg(RegEncode[SP_ALLOC_SCRATCH_REG]);
  1144. }
  1145. }
  1146. // Frame size is local var area plus stack arg area, 8-byte-aligned (if we're in a non-leaf).
  1147. int32 bytesOnStack = stackAdjust + regSaveArea;
  1148. int32 alignPad = Math::Align<int32>(bytesOnStack, MachStackAlignment) - bytesOnStack;
  1149. if (alignPad)
  1150. {
  1151. stackAdjust += alignPad;
  1152. if (hasTry)
  1153. {
  1154. // We have to align the arg area, since the helper won't allocate a locals area.
  1155. Assert(alignPad % MachRegInt == 0);
  1156. this->m_func->m_argSlotsForFunctionsCalled += alignPad / MachRegInt;
  1157. }
  1158. else
  1159. {
  1160. // Treat the alignment pad as part of the locals area, which will put it as far from SP as possible.
  1161. // Note that we've already handled the change to the stack height above in checking
  1162. // for dynamic probes.
  1163. this->m_func->m_localStackHeight += alignPad;
  1164. }
  1165. }
  1166. }
  1167. Assert(fpOffsetSize >= 0);
  1168. if (m_func->GetMaxInlineeArgOutSize() != 0)
  1169. {
  1170. // subtracting 2 for frame pointer & return address
  1171. this->m_func->GetJITOutput()->SetFrameHeight(this->m_func->m_localStackHeight + this->m_func->m_ArgumentsOffset - 2 * MachRegInt);
  1172. }
  1173. //Generate StackProbe for large stack's first even before register push
  1174. bool fStackProbeAfterProlog = IsSmallStack(stackAdjust);
  1175. if (!fStackProbeAfterProlog)
  1176. {
  1177. GenerateStackProbe(insertInstr, false); //stack is already aligned in this case
  1178. }
  1179. IR::RegOpnd * r12Opnd = nullptr;
  1180. // Zero-initialize dedicated arguments slot
  1181. if (hasCalls)
  1182. {
  1183. //R12 acts a dummy zero register which we push to arguments slot
  1184. //mov r12, 0
  1185. Assert(r12Opnd == nullptr);
  1186. r12Opnd = IR::RegOpnd::New(nullptr, SCRATCH_REG, TyMachReg, this->m_func);
  1187. IR::Instr * instrMov = IR::Instr::New(Js::OpCode::MOV, r12Opnd, IR::IntConstOpnd::New(0, TyMachReg, this->m_func), this->m_func);
  1188. insertInstr->InsertBefore(instrMov);
  1189. IR::LabelInstr *prologStartLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  1190. insertInstr->InsertBefore(prologStartLabel);
  1191. this->m_func->m_unwindInfo.SetPrologStartLabel(prologStartLabel->m_id);
  1192. }
  1193. if (!paramRegs.IsEmpty())
  1194. {
  1195. // Generate PUSH {r0-r3}
  1196. IR::Instr * instrPush = IR::Instr::New(Js::OpCode::PUSH, this->m_func);
  1197. instrPush->SetDst(IR::IndirOpnd::New(IR::RegOpnd::New(nullptr, RegSP, TyMachReg, this->m_func), (int32)0, TyMachReg, this->m_func));
  1198. instrPush->SetSrc1(IR::RegBVOpnd::New(paramRegs, TyMachReg, this->m_func));
  1199. insertInstr->InsertBefore(instrPush);
  1200. }
  1201. // Setup Frame pointer
  1202. if (hasCalls)
  1203. {
  1204. BVUnit frameRegs;
  1205. frameRegs.Set(RegEncode[RegR11]);
  1206. frameRegs.Set(RegEncode[RegLR]);
  1207. // Generate PUSH {r11,lr}
  1208. IR::Instr * instrPush = IR::Instr::New(Js::OpCode::PUSH, this->m_func);
  1209. instrPush->SetDst(IR::IndirOpnd::New(IR::RegOpnd::New(nullptr, RegSP, TyMachReg, this->m_func), (int32)0, TyMachReg, this->m_func));
  1210. instrPush->SetSrc1(IR::RegBVOpnd::New(frameRegs, TyMachReg, this->m_func));
  1211. insertInstr->InsertBefore(instrPush);
  1212. // Generate MOV r11,sp
  1213. IR::RegOpnd* spOpnd = IR::RegOpnd::New(nullptr, RegSP, TyMachReg, this->m_func);
  1214. IR::RegOpnd* r11Opnd = IR::RegOpnd::New(nullptr, RegR11, TyMachReg, this->m_func);
  1215. IR::Instr * instrMov = IR::Instr::New(Js::OpCode::MOV, r11Opnd, spOpnd, this->m_func);
  1216. insertInstr->InsertBefore(instrMov);
  1217. }
  1218. if (!usedRegs.IsEmpty())
  1219. {
  1220. // Generate PUSH {r4-r10,r12}
  1221. IR::Instr * instrPush = IR::Instr::New(Js::OpCode::PUSH, this->m_func);
  1222. instrPush->SetDst(IR::IndirOpnd::New(IR::RegOpnd::New(nullptr, RegSP, TyMachReg, this->m_func), (int32)0, TyMachReg, this->m_func));
  1223. instrPush->SetSrc1(IR::RegBVOpnd::New(usedRegs, TyMachReg, this->m_func));
  1224. insertInstr->InsertBefore(instrPush);
  1225. }
  1226. if (!usedDoubleRegs.IsEmpty())
  1227. {
  1228. // Generate VPUSH {d8-d15}
  1229. IR::Instr * instrPush = IR::Instr::New(Js::OpCode::VPUSH, this->m_func);
  1230. instrPush->SetDst(IR::IndirOpnd::New(IR::RegOpnd::New(nullptr, RegSP, TyMachReg, this->m_func), (int32)0, TyMachReg, this->m_func));
  1231. instrPush->SetSrc1(IR::RegBVOpnd::New(usedDoubleRegs, TyMachReg, this->m_func));
  1232. insertInstr->InsertBefore(instrPush);
  1233. }
  1234. if (hasTry)
  1235. {
  1236. // Copy the value of SP before we allocate the locals area. We'll save this value on the stack below.
  1237. Lowerer::InsertMove(
  1238. IR::RegOpnd::New(nullptr, EH_STACK_SAVE_REG, TyMachReg, this->m_func),
  1239. IR::RegOpnd::New(nullptr, RegSP, TyMachReg, this->m_func),
  1240. insertInstr);
  1241. }
  1242. bool isScratchRegisterThrashed = false;
  1243. uint32 probeSize = stackAdjust;
  1244. RegNum localsReg = this->m_func->GetLocalsPointer();
  1245. if (localsReg != RegSP)
  1246. {
  1247. // Allocate just the locals area first and let the locals pointer point to it.
  1248. // This may or may not generate a chkstk.
  1249. uint32 localsSize = this->m_func->m_localStackHeight;
  1250. if (localsSize != 0)
  1251. {
  1252. isScratchRegisterThrashed = GenerateStackAllocation(insertInstr, localsSize, localsSize);
  1253. stackAdjust -= localsSize;
  1254. if (!IsSmallStack(localsSize))
  1255. {
  1256. // The first alloc generated a chkstk, so we only have to probe (again) if the remaining
  1257. // allocation also exceeds a page.
  1258. probeSize = stackAdjust;
  1259. }
  1260. }
  1261. // Set up the locals pointer.
  1262. Lowerer::InsertMove(
  1263. IR::RegOpnd::New(nullptr, localsReg, TyMachReg, this->m_func),
  1264. IR::RegOpnd::New(nullptr, RegSP, TyMachReg, this->m_func),
  1265. insertInstr);
  1266. }
  1267. if (hasTry)
  1268. {
  1269. // Now push the reg we used above to save the address of the top of the locals area.
  1270. BVUnit ehReg;
  1271. ehReg.Set(RegEncode[EH_STACK_SAVE_REG]);
  1272. IR::Instr * instrPush =
  1273. IR::Instr::New(
  1274. Js::OpCode::PUSH,
  1275. IR::IndirOpnd::New(
  1276. IR::RegOpnd::New(nullptr, RegSP, TyMachReg, this->m_func), (int32)0, TyMachReg, this->m_func),
  1277. IR::RegBVOpnd::New(ehReg, TyMachReg, this->m_func),
  1278. this->m_func);
  1279. insertInstr->InsertBefore(instrPush);
  1280. }
  1281. // If the stack size is less than a page allocate the stack first & then do the stack probe
  1282. // stack limit has a buffer of StackOverflowHandlingBufferPages pages and we are okay here
  1283. if (stackAdjust != 0)
  1284. {
  1285. isScratchRegisterThrashed = GenerateStackAllocation(insertInstr, stackAdjust, probeSize);
  1286. }
  1287. //As we have already allocated the stack here, we can safely zero out the inlinee argout slot.
  1288. // Zero initialize the first inlinee frames argc.
  1289. if (m_func->GetMaxInlineeArgOutSize() != 0)
  1290. {
  1291. // This is done post prolog. so we don't have to emit unwind data.
  1292. if (r12Opnd == nullptr || isScratchRegisterThrashed)
  1293. {
  1294. r12Opnd = r12Opnd ? r12Opnd : IR::RegOpnd::New(nullptr, SCRATCH_REG, TyMachReg, this->m_func);
  1295. // mov r12, 0
  1296. IR::Instr * instrMov = IR::Instr::New(Js::OpCode::MOV, r12Opnd, IR::IntConstOpnd::New(0, TyMachReg, this->m_func), this->m_func);
  1297. insertInstr->InsertBefore(instrMov);
  1298. }
  1299. // STR argc, r12
  1300. StackSym *sym = this->m_func->m_symTable->GetArgSlotSym((Js::ArgSlot)-1);
  1301. sym->m_isInlinedArgSlot = true;
  1302. sym->m_offset = 0;
  1303. IR::Opnd *dst = IR::SymOpnd::New(sym, 0, TyMachReg, this->m_func);
  1304. insertInstr->InsertBefore(IR::Instr::New(Js::OpCode::STR,
  1305. dst,
  1306. r12Opnd,
  1307. this->m_func));
  1308. }
  1309. // Now do the stack probe for small stacks
  1310. // hasCalls catches the recursion case
  1311. if ((stackAdjust != 0 || hasCalls) && fStackProbeAfterProlog)
  1312. {
  1313. GenerateStackProbe(insertInstr, true); //stack is already aligned in this case
  1314. }
  1315. return entryInstr;
  1316. }
  1317. IR::Instr *
  1318. LowererMD::LowerExitInstr(IR::ExitInstr * exitInstr)
  1319. {
  1320. // add sp, sp, #local stack space
  1321. // vpop {d8-d15} //restore callee saved double registers.
  1322. // pop {r4-r10, r12} //restore callee saved registers.
  1323. // pop r11 // restore r11 chain.
  1324. // ldr pc, [sp], #offset //homed arguments + 1 for lr
  1325. // See how many params were homed. We don't need to restore the values, just recover the stack space.
  1326. int32 homedParams = this->m_func->m_unwindInfo.GetHomedParamCount();
  1327. BOOL hasTry = this->m_func->HasTry();
  1328. RegNum localsReg = this->m_func->GetLocalsPointer();
  1329. int32 stackAdjust;
  1330. if (hasTry)
  1331. {
  1332. if (this->m_func->DoOptimizeTry())
  1333. {
  1334. this->EnsureEpilogLabel();
  1335. }
  1336. // We'll only deallocate the arg out area then restore SP from the value saved on the stack.
  1337. stackAdjust = (this->m_func->m_argSlotsForFunctionsCalled * MachRegInt);
  1338. }
  1339. else if (localsReg != RegSP)
  1340. {
  1341. // We're going to restore SP from the locals pointer and then deallocate only the locals area.
  1342. Lowerer::InsertMove(
  1343. IR::RegOpnd::New(nullptr, RegSP, TyMachReg, this->m_func),
  1344. IR::RegOpnd::New(nullptr, localsReg, TyMachReg, this->m_func),
  1345. exitInstr);
  1346. stackAdjust = this->m_func->m_localStackHeight;
  1347. }
  1348. else
  1349. {
  1350. // We're going to deallocate the locals and out arg area at once.
  1351. stackAdjust = (this->m_func->m_argSlotsForFunctionsCalled * MachRegInt) + this->m_func->m_localStackHeight;
  1352. }
  1353. // Record used callee-saved registers. This is in the form of a fixed bitfield.
  1354. BVUnit32 usedRegs;
  1355. for (RegNum reg = FIRST_CALLEE_SAVED_GP_REG; reg <= LAST_CALLEE_SAVED_GP_REG; reg = (RegNum)(reg+1))
  1356. {
  1357. Assert(LinearScan::IsCalleeSaved(reg));
  1358. if (this->m_func->m_regsUsed.Test(reg) || hasTry)
  1359. {
  1360. usedRegs.Set(RegEncode[reg]);
  1361. }
  1362. }
  1363. // We need to have the same register saves in the prolog as the arm_CallEhFrame, so that we can use the same
  1364. // epilog. So always allocate a slot for the stack nested func here whether we actually do have any stack
  1365. // nested func or not
  1366. // TODO-STACK-NESTED-FUNC: May be use a different arm_CallEhFrame for when we have stack nested func?
  1367. if (this->m_func->HasAnyStackNestedFunc() || hasTry)
  1368. {
  1369. usedRegs.Set(RegEncode[RegR11]);
  1370. }
  1371. bool hasCalls = this->m_func->m_unwindInfo.GetHasCalls();
  1372. if (hasCalls)
  1373. {
  1374. // __chkstk has special calling convention and uses R4, and dynamic stack probe on large frames use it too
  1375. if (this->m_func->m_unwindInfo.GetSavedScratchReg())
  1376. {
  1377. usedRegs.Set(RegEncode[SP_ALLOC_SCRATCH_REG]);
  1378. }
  1379. //RegR12 acts a dummy register to deallocate stack allocated for arguments object
  1380. usedRegs.Set(RegEncode[SCRATCH_REG]);
  1381. }
  1382. else if (usedRegs.IsEmpty())
  1383. {
  1384. stackAdjust += homedParams * MachRegInt;
  1385. }
  1386. // 1. Deallocate the stack. In the case of a leaf function with no saved registers, let this
  1387. // deallocation also account for the homed params.
  1388. if (stackAdjust != 0)
  1389. {
  1390. GenerateStackDeallocation(exitInstr, stackAdjust);
  1391. }
  1392. // This is the stack size that the pdata cares about.
  1393. this->m_func->m_unwindInfo.SetStackDepth(stackAdjust);
  1394. if (hasTry)
  1395. {
  1396. // Now restore the locals area by popping the stack.
  1397. BVUnit ehReg;
  1398. ehReg.Set(RegEncode[EH_STACK_SAVE_REG]);
  1399. IR::Instr * instrPop = IR::Instr::New(
  1400. Js::OpCode::POP,
  1401. IR::RegBVOpnd::New(ehReg, TyMachReg, this->m_func),
  1402. IR::IndirOpnd::New(
  1403. IR::RegOpnd::New(nullptr, RegSP, TyMachReg, this->m_func), (int32)0, TyMachReg, this->m_func),
  1404. this->m_func);
  1405. exitInstr->InsertBefore(instrPop);
  1406. Lowerer::InsertMove(
  1407. IR::RegOpnd::New(nullptr, RegSP, TyMachReg, this->m_func),
  1408. IR::RegOpnd::New(nullptr, EH_STACK_SAVE_REG, TyMachReg, this->m_func),
  1409. exitInstr);
  1410. }
  1411. // 2. Restore saved double registers. Generate vpop {d8-d15}
  1412. BVUnit32 savedDoubleRegs(this->m_func->m_unwindInfo.GetDoubleSavedRegList());
  1413. if (!savedDoubleRegs.IsEmpty())
  1414. {
  1415. IR::Instr * instrVPop = IR::Instr::New(Js::OpCode::VPOP, this->m_func);
  1416. instrVPop->SetDst(IR::RegBVOpnd::New(savedDoubleRegs, TyMachReg, this->m_func));
  1417. instrVPop->SetSrc1(IR::IndirOpnd::New(IR::RegOpnd::New(nullptr, RegSP,TyMachReg, this->m_func), (int32)0, TyMachReg, this->m_func));
  1418. exitInstr->InsertBefore(instrVPop);
  1419. }
  1420. // 3. Restore saved registers. Generate pop {r4-r10,r12}
  1421. if (!usedRegs.IsEmpty())
  1422. {
  1423. IR::Instr * instrPop = IR::Instr::New(Js::OpCode::POP, this->m_func);
  1424. instrPop->SetDst(IR::RegBVOpnd::New(usedRegs, TyMachReg, this->m_func));
  1425. instrPop->SetSrc1(IR::IndirOpnd::New(IR::RegOpnd::New(nullptr, RegSP,TyMachReg, this->m_func), (int32)0, TyMachReg, this->m_func));
  1426. exitInstr->InsertBefore(instrPop);
  1427. }
  1428. if (!hasCalls)
  1429. {
  1430. if (!usedRegs.IsEmpty())
  1431. {
  1432. // We do need to deallocate the area allocated when we homed the params (since we weren't able to fold
  1433. // it into the first stack deallocation).
  1434. // TODO: Consider folding this into the LDM instruction above by having it restore dummy registers.
  1435. IR::RegOpnd * spOpnd = IR::RegOpnd::New(nullptr, this->GetRegStackPointer(), TyMachReg, this->m_func);
  1436. IR::IntConstOpnd * adjustOpnd = IR::IntConstOpnd::New(homedParams * MachRegInt, TyMachReg, this->m_func, true);
  1437. IR::Instr * spAdjustInstr = IR::Instr::New(Js::OpCode::ADD, spOpnd, spOpnd, adjustOpnd, this->m_func);
  1438. exitInstr->InsertBefore(spAdjustInstr);
  1439. }
  1440. // LR is still valid, so return by branching to it.
  1441. IR::Instr * instrRet = IR::Instr::New(
  1442. Js::OpCode::RET,
  1443. IR::RegOpnd::New(nullptr, RegPC, TyMachReg, this->m_func),
  1444. IR::RegOpnd::New(nullptr, RegLR, TyMachReg, this->m_func),
  1445. this->m_func);
  1446. exitInstr->InsertBefore(instrRet);
  1447. }
  1448. else
  1449. {
  1450. // 3. Set up original frame pointer - pop r11
  1451. usedRegs.ClearAll();
  1452. usedRegs.Set(RegEncode[RegR11]);
  1453. IR::Instr * instrPop = IR::Instr::New(Js::OpCode::POP, this->m_func);
  1454. instrPop->SetDst(IR::RegBVOpnd::New(usedRegs, TyMachReg, this->m_func));
  1455. instrPop->SetSrc1(IR::IndirOpnd::New(IR::RegOpnd::New(nullptr, RegSP,TyMachReg, this->m_func), (int32)0, TyMachReg, this->m_func));
  1456. exitInstr->InsertBefore(instrPop);
  1457. // 4. Deallocate homed param area (if necessary) and return.
  1458. // SP now points to the location where we saved LR.
  1459. // So return by doing a LDR pc,[sp],#n, where the postincrement of SP deallocates what remains of the stack.
  1460. // Note: the offset on this indir indicates the postincrement, which is the homed param area plus the size
  1461. // of LR itself.
  1462. IR::IndirOpnd * spIndir = IR::IndirOpnd::New(
  1463. IR::RegOpnd::New(nullptr, RegSP, TyMachReg, this->m_func),
  1464. (homedParams + 1) * MachRegInt,
  1465. TyMachPtr, this->m_func);
  1466. IR::Instr * instrRet = IR::Instr::New(
  1467. Js::OpCode::LDRRET,
  1468. IR::RegOpnd::New(nullptr, RegPC, TyMachReg, this->m_func),
  1469. spIndir,
  1470. this->m_func);
  1471. exitInstr->InsertBefore(instrRet);
  1472. }
  1473. IR::LabelInstr *epilogEndLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  1474. exitInstr->InsertBefore(epilogEndLabel);
  1475. this->m_func->m_unwindInfo.SetEpilogEndLabel(epilogEndLabel->m_id);
  1476. return exitInstr;
  1477. }
  1478. IR::Instr *
  1479. LowererMD::LoadNewScObjFirstArg(IR::Instr * instr, IR::Opnd * argSrc, ushort extraArgs)
  1480. {
  1481. // Spread moves down the argument slot by one.
  1482. // LowerCallArgs will handle the extraArgs. We only need to specify the argument number
  1483. // i.e 1 and not + extraArgs as done in AMD64
  1484. IR::SymOpnd *argOpnd = IR::SymOpnd::New(this->m_func->m_symTable->GetArgSlotSym(1), TyVar, this->m_func);
  1485. IR::Instr *argInstr = IR::Instr::New(Js::OpCode::ArgOut_A, argOpnd, argSrc, this->m_func);
  1486. instr->InsertBefore(argInstr);
  1487. // Insert the argument into the arg chain.
  1488. if (m_lowerer->IsSpreadCall(instr))
  1489. {
  1490. // Spread calls need LdSpreadIndices as the last arg in the arg chain.
  1491. instr = m_lowerer->GetLdSpreadIndicesInstr(instr);
  1492. }
  1493. IR::Opnd *linkOpnd = instr->UnlinkSrc2();
  1494. argInstr->SetSrc2(linkOpnd);
  1495. instr->SetSrc2(argOpnd);
  1496. return argInstr;
  1497. }
  1498. IR::Instr *
  1499. LowererMD::LowerTry(IR::Instr * tryInstr, IR::JnHelperMethod helperMethod)
  1500. {
  1501. // Mark the entry to the try
  1502. IR::Instr * instr = tryInstr->GetNextRealInstrOrLabel();
  1503. AssertMsg(instr->IsLabelInstr(), "No label at the entry to a try?");
  1504. IR::LabelInstr * tryAddr = instr->AsLabelInstr();
  1505. // Arg 7: ScriptContext
  1506. this->m_lowerer->LoadScriptContext(tryAddr);
  1507. if (tryInstr->m_opcode == Js::OpCode::TryCatch || this->m_func->DoOptimizeTry() || (this->m_func->IsSimpleJit() && this->m_func->hasBailout))
  1508. {
  1509. // Arg 6 : hasBailedOutOffset
  1510. IR::Opnd * hasBailedOutOffset = IR::IntConstOpnd::New(this->m_func->GetHasBailedOutSym()->m_offset + tryInstr->m_func->GetInlineeArgumentStackSize(), TyInt32, this->m_func);
  1511. this->LoadHelperArgument(tryAddr, hasBailedOutOffset);
  1512. }
  1513. // Arg 5: arg out size
  1514. IR::RegOpnd * argOutSize = IR::RegOpnd::New(TyMachReg, this->m_func);
  1515. instr = IR::Instr::New(Js::OpCode::LDARGOUTSZ, argOutSize, this->m_func);
  1516. tryAddr->InsertBefore(instr);
  1517. this->LoadHelperArgument(tryAddr, argOutSize);
  1518. // Arg 4: locals pointer
  1519. IR::RegOpnd * localsPtr = IR::RegOpnd::New(nullptr, this->m_func->GetLocalsPointer(), TyMachReg, this->m_func);
  1520. this->LoadHelperArgument(tryAddr, localsPtr);
  1521. // Arg 3: frame pointer
  1522. IR::RegOpnd * framePtr = IR::RegOpnd::New(nullptr, FRAME_REG, TyMachReg, this->m_func);
  1523. this->LoadHelperArgument(tryAddr, framePtr);
  1524. // Arg 2: helper address
  1525. IR::LabelInstr * helperAddr = tryInstr->AsBranchInstr()->GetTarget();
  1526. this->LoadHelperArgument(tryAddr, IR::LabelOpnd::New(helperAddr, this->m_func));
  1527. // Arg 1: try address
  1528. this->LoadHelperArgument(tryAddr, IR::LabelOpnd::New(tryAddr, this->m_func));
  1529. // Call the helper
  1530. IR::RegOpnd *continuationAddr =
  1531. IR::RegOpnd::New(StackSym::New(TyMachReg,this->m_func), RETURN_REG, TyMachReg, this->m_func);
  1532. IR::Instr * callInstr = IR::Instr::New(
  1533. Js::OpCode::Call, continuationAddr, IR::HelperCallOpnd::New(helperMethod, this->m_func), this->m_func);
  1534. tryAddr->InsertBefore(callInstr);
  1535. this->LowerCall(callInstr, 0);
  1536. // Jump to the continuation address supplied by the helper
  1537. IR::BranchInstr *branchInstr = IR::MultiBranchInstr::New(Js::OpCode::BX, continuationAddr, this->m_func);
  1538. tryAddr->InsertBefore(branchInstr);
  1539. return tryInstr->m_prev;
  1540. }
  1541. IR::Instr *
  1542. LowererMD::LowerLeaveNull(IR::Instr * leaveInstr)
  1543. {
  1544. IR::Instr * instrPrev = leaveInstr->m_prev;
  1545. // Return a NULL continuation address to the caller to indicate that the finally did not seize the flow.
  1546. this->LowerEHRegionReturn(leaveInstr, IR::IntConstOpnd::New(0, TyMachReg, this->m_func));
  1547. leaveInstr->Remove();
  1548. return instrPrev;
  1549. }
  1550. IR::Instr *
  1551. LowererMD::LowerEHRegionReturn(IR::Instr * insertBeforeInstr, IR::Opnd * targetOpnd)
  1552. {
  1553. IR::RegOpnd *retReg = IR::RegOpnd::New(nullptr, RETURN_REG, TyMachReg, this->m_func);
  1554. // Load the continuation address into the return register.
  1555. Lowerer::InsertMove(retReg, targetOpnd, insertBeforeInstr);
  1556. IR::LabelInstr *epilogLabel = this->EnsureEpilogLabel();
  1557. IR::BranchInstr *jmpInstr = IR::BranchInstr::New(Js::OpCode::B, epilogLabel, this->m_func);
  1558. insertBeforeInstr->InsertBefore(jmpInstr);
  1559. // return the last instruction inserted
  1560. return jmpInstr;
  1561. }
  1562. ///----------------------------------------------------------------------------
  1563. ///
  1564. /// LowererMD::Init
  1565. ///
  1566. ///----------------------------------------------------------------------------
  1567. void
  1568. LowererMD::Init(Lowerer *lowerer)
  1569. {
  1570. m_lowerer = lowerer;
  1571. // The arg slot count computed by an earlier phase (e.g., IRBuilder) doesn't work for
  1572. // ARM if it accounts for nesting. Clear it here and let Lower compute its own value.
  1573. this->m_func->m_argSlotsForFunctionsCalled = 0;
  1574. }
  1575. ///----------------------------------------------------------------------------
  1576. ///
  1577. /// LowererMD::LoadInputParamPtr
  1578. ///
  1579. /// Load the address of the start of the passed-in parameters not including
  1580. /// the this parameter.
  1581. ///
  1582. ///----------------------------------------------------------------------------
  1583. IR::Instr *
  1584. LowererMD::LoadInputParamPtr(IR::Instr * instrInsert, IR::RegOpnd * optionalDstOpnd /* = nullptr */)
  1585. {
  1586. if (this->m_func->GetJITFunctionBody()->IsCoroutine())
  1587. {
  1588. IR::RegOpnd * argPtrRegOpnd = Lowerer::LoadGeneratorArgsPtr(instrInsert);
  1589. IR::IndirOpnd * indirOpnd = IR::IndirOpnd::New(argPtrRegOpnd, 1 * MachPtr, TyMachPtr, this->m_func);
  1590. IR::RegOpnd * dstOpnd = optionalDstOpnd != nullptr ? optionalDstOpnd : IR::RegOpnd::New(TyMachPtr, this->m_func);
  1591. return Lowerer::InsertLea(dstOpnd, indirOpnd, instrInsert);
  1592. }
  1593. else
  1594. {
  1595. StackSym * paramSym = GetImplicitParamSlotSym(3);
  1596. return this->m_lowerer->InsertLoadStackAddress(paramSym, instrInsert);
  1597. }
  1598. }
  1599. ///----------------------------------------------------------------------------
  1600. ///
  1601. /// LowererMD::LoadInputParamCount
  1602. ///
  1603. /// Load the passed-in parameter count from the appropriate r11 slot.
  1604. ///
  1605. ///----------------------------------------------------------------------------
  1606. IR::Instr *
  1607. LowererMD::LoadInputParamCount(IR::Instr * instrInsert, int adjust, bool needFlags)
  1608. {
  1609. IR::Instr * instr;
  1610. IR::RegOpnd * dstOpnd;
  1611. IR::SymOpnd * srcOpnd;
  1612. // LDR Rz, CallInfo
  1613. // LSR Rx, Rz, #28 // Get CallEval bit as bottom bit.
  1614. // AND Rx, Rx, #1 // Mask higher 3 bits, Rx has 1 if FrameDisplay is present, zero otherwise
  1615. // LSL Rz, Rz, #8 // Mask higher 8 bits to get the number of arguments
  1616. // LSR Rz, Rz, #8
  1617. // SUB Rz, Rz, Rx // Now Rz has the right number of parameters
  1618. srcOpnd = Lowerer::LoadCallInfo(instrInsert);
  1619. dstOpnd = IR::RegOpnd::New(TyMachReg, this->m_func);
  1620. instr = IR::Instr::New(Js::OpCode::LDR, dstOpnd, srcOpnd, this->m_func);
  1621. instrInsert->InsertBefore(instr);
  1622. // mask the "calling eval" bit and subtract it from the incoming count.
  1623. // ("Calling eval" means the last param is the frame display, which only the eval built-in should see.)
  1624. instr = IR::Instr::New(Js::OpCode::LSL, dstOpnd, dstOpnd, IR::IntConstOpnd::New(Js::CallInfo::ksizeofCallFlags, TyMachReg, this->m_func), this->m_func);
  1625. instrInsert->InsertBefore(instr);
  1626. instr = IR::Instr::New(Js::OpCode::LSR, dstOpnd, dstOpnd, IR::IntConstOpnd::New(Js::CallInfo::ksizeofCallFlags, TyMachReg, this->m_func), this->m_func);
  1627. instrInsert->InsertBefore(instr);
  1628. return Lowerer::InsertSub(needFlags, dstOpnd, dstOpnd, IR::IntConstOpnd::New(-adjust, TyUint32, this->m_func), instrInsert);
  1629. }
  1630. IR::Instr *
  1631. LowererMD::LoadStackArgPtr(IR::Instr * instr)
  1632. {
  1633. if (this->m_func->IsLoopBody())
  1634. {
  1635. // Get the first user param from the interpreter frame instance that was passed in.
  1636. // These args don't include the func object and callinfo; we just need to advance past "this".
  1637. // t1 = LDR [prm1 + m_inParams]
  1638. // dst = ADD t1, sizeof(var)
  1639. Assert(this->m_func->GetLoopParamSym());
  1640. IR::RegOpnd *baseOpnd = IR::RegOpnd::New(this->m_func->GetLoopParamSym(), TyMachReg, this->m_func);
  1641. size_t offset = Js::InterpreterStackFrame::GetOffsetOfInParams();
  1642. IR::IndirOpnd *indirOpnd = IR::IndirOpnd::New(baseOpnd, (int32)offset, TyMachReg, this->m_func);
  1643. IR::RegOpnd *tmpOpnd = IR::RegOpnd::New(TyMachReg, this->m_func);
  1644. Lowerer::InsertMove(tmpOpnd, indirOpnd, instr);
  1645. instr->SetSrc1(tmpOpnd);
  1646. instr->SetSrc2(IR::IntConstOpnd::New(sizeof(Js::Var), TyMachReg, this->m_func));
  1647. }
  1648. else if (this->m_func->GetJITFunctionBody()->IsCoroutine())
  1649. {
  1650. IR::Instr *instr2 = LoadInputParamPtr(instr, instr->UnlinkDst()->AsRegOpnd());
  1651. instr->Remove();
  1652. instr = instr2;
  1653. }
  1654. else
  1655. {
  1656. // Get the args pointer relative to r11. We assume that r11 is set up, since we'll only be looking
  1657. // for the stack arg pointer in a non-leaf.
  1658. // dst = ADD r11, "this" offset + sizeof(var)
  1659. instr->SetSrc1(IR::RegOpnd::New(nullptr, FRAME_REG, TyMachReg, this->m_func));
  1660. instr->SetSrc2(IR::IntConstOpnd::New((ArgOffsetFromFramePtr + Js::JavascriptFunctionArgIndex_SecondScriptArg) * sizeof(Js::Var), TyMachReg, this->m_func));
  1661. }
  1662. instr->m_opcode = Js::OpCode::ADD;
  1663. return instr->m_prev;
  1664. }
  1665. IR::Instr *
  1666. LowererMD::LoadArgumentsFromFrame(IR::Instr * instr)
  1667. {
  1668. IR::RegOpnd *baseOpnd;
  1669. int32 offset;
  1670. if (this->m_func->IsLoopBody())
  1671. {
  1672. // Get the arguments ptr from the interpreter frame instance that was passed in.
  1673. Assert(this->m_func->GetLoopParamSym());
  1674. baseOpnd = IR::RegOpnd::New(this->m_func->GetLoopParamSym(), TyMachReg, this->m_func);
  1675. offset = Js::InterpreterStackFrame::GetOffsetOfArguments();
  1676. }
  1677. else
  1678. {
  1679. // Get the arguments relative to the frame pointer.
  1680. baseOpnd = IR::RegOpnd::New(nullptr, FRAME_REG, TyMachReg, this->m_func);
  1681. offset = -MachArgsSlotOffset;
  1682. }
  1683. instr->SetSrc1(IR::IndirOpnd::New(baseOpnd, offset, TyMachReg, this->m_func));
  1684. this->ChangeToAssign(instr);
  1685. return instr->m_prev;
  1686. }
  1687. // load argument count as I4
  1688. IR::Instr *
  1689. LowererMD::LoadArgumentCount(IR::Instr * instr)
  1690. {
  1691. IR::RegOpnd *baseOpnd;
  1692. int32 offset;
  1693. if (this->m_func->IsLoopBody())
  1694. {
  1695. // Pull the arg count from the interpreter frame instance that was passed in.
  1696. // (The callinfo in the loop body's frame just shows the single parameter, the interpreter frame.)
  1697. Assert(this->m_func->GetLoopParamSym());
  1698. baseOpnd = IR::RegOpnd::New(this->m_func->GetLoopParamSym(), TyMachReg, this->m_func);
  1699. offset = Js::InterpreterStackFrame::GetOffsetOfInSlotsCount();
  1700. }
  1701. else
  1702. {
  1703. baseOpnd = IR::RegOpnd::New(nullptr, FRAME_REG, TyMachReg, this->m_func);
  1704. offset = (ArgOffsetFromFramePtr + Js::JavascriptFunctionArgIndex_CallInfo) * sizeof(Js::Var);
  1705. }
  1706. instr->SetSrc1(IR::IndirOpnd::New(baseOpnd, offset, TyInt32, this->m_func));
  1707. this->ChangeToAssign(instr);
  1708. return instr->m_prev;
  1709. }
  1710. ///----------------------------------------------------------------------------
  1711. ///
  1712. /// LowererMD::LoadHeapArguments
  1713. ///
  1714. /// Load the arguments object
  1715. /// NOTE: The same caveat regarding arguments passed on the stack applies here
  1716. /// as in LoadInputParamCount above.
  1717. ///----------------------------------------------------------------------------
  1718. IR::Instr *
  1719. LowererMD::LoadHeapArguments(IR::Instr * instrArgs)
  1720. {
  1721. ASSERT_INLINEE_FUNC(instrArgs);
  1722. Func *func = instrArgs->m_func;
  1723. IR::Instr * instrPrev = instrArgs->m_prev;
  1724. if (func->IsStackArgsEnabled())
  1725. {
  1726. // The initial args slot value is zero.
  1727. instrArgs->m_opcode = Js::OpCode::MOV;
  1728. instrArgs->ReplaceSrc1(IR::AddrOpnd::NewNull(func));
  1729. if (PHASE_TRACE1(Js::StackArgFormalsOptPhase) && func->GetJITFunctionBody()->GetInParamsCount() > 1)
  1730. {
  1731. Output::Print(_u("StackArgFormals : %s (%d) :Removing Heap Arguments object creation in Lowerer. \n"), instrArgs->m_func->GetJITFunctionBody()->GetDisplayName(), instrArgs->m_func->GetFunctionNumber());
  1732. Output::Flush();
  1733. }
  1734. }
  1735. else
  1736. {
  1737. // s7 = formals are let decls
  1738. // s6 = memory context
  1739. // s5 = array of property ID's
  1740. // s4 = local frame instance
  1741. // s3 = address of first actual argument (after "this")
  1742. // s2 = actual argument count
  1743. // s1 = current function
  1744. // dst = JavascriptOperators::LoadHeapArguments(s1, s2, s3, s4, s5, s6, s7)
  1745. // s7 = formals are let decls
  1746. this->LoadHelperArgument(instrArgs, IR::IntConstOpnd::New(instrArgs->m_opcode == Js::OpCode::LdLetHeapArguments ? TRUE : FALSE, TyUint8, func));
  1747. // s6 = memory context
  1748. this->m_lowerer->LoadScriptContext(instrArgs);
  1749. // s5 = array of property ID's
  1750. intptr_t formalsPropIdArray = instrArgs->m_func->GetJITFunctionBody()->GetFormalsPropIdArrayAddr();
  1751. if (!formalsPropIdArray)
  1752. {
  1753. formalsPropIdArray = instrArgs->m_func->GetScriptContextInfo()->GetNullAddr();
  1754. }
  1755. IR::Opnd * argArray = IR::AddrOpnd::New(formalsPropIdArray, IR::AddrOpndKindDynamicMisc, m_func);
  1756. this->LoadHelperArgument(instrArgs, argArray);
  1757. // s4 = local frame instance
  1758. IR::Opnd * frameObj = instrArgs->UnlinkSrc1();
  1759. this->LoadHelperArgument(instrArgs, frameObj);
  1760. if (func->IsInlinee())
  1761. {
  1762. // s3 = address of first actual argument (after "this").
  1763. StackSym *firstRealArgSlotSym = func->GetInlineeArgvSlotOpnd()->m_sym->AsStackSym();
  1764. this->m_func->SetArgOffset(firstRealArgSlotSym, firstRealArgSlotSym->m_offset + MachPtr);
  1765. IR::Instr *instr = this->m_lowerer->InsertLoadStackAddress(firstRealArgSlotSym, instrArgs);
  1766. this->LoadHelperArgument(instrArgs, instr->GetDst());
  1767. // s2 = actual argument count (without counting "this").
  1768. this->LoadHelperArgument(instrArgs, IR::IntConstOpnd::New(func->actualCount - 1, TyUint32, func));
  1769. // s1 = current function.
  1770. this->LoadHelperArgument(instrArgs, func->GetInlineeFunctionObjectSlotOpnd());
  1771. // Save the newly-created args object to its dedicated stack slot.
  1772. IR::SymOpnd *argObjSlotOpnd = func->GetInlineeArgumentsObjectSlotOpnd();
  1773. Lowerer::InsertMove(argObjSlotOpnd,instrArgs->GetDst(), instrArgs->m_next);
  1774. }
  1775. else
  1776. {
  1777. // s3 = address of first actual argument (after "this")
  1778. // Stack looks like (function object)+0, (arg count)+4, (this)+8, actual args
  1779. IR::Instr * instr = this->LoadInputParamPtr(instrArgs);
  1780. this->LoadHelperArgument(instrArgs, instr->GetDst());
  1781. // s2 = actual argument count (without counting "this")
  1782. instr = this->LoadInputParamCount(instrArgs, -1);
  1783. IR::Opnd * opndInputParamCount = instr->GetDst();
  1784. this->LoadHelperArgument(instrArgs, opndInputParamCount);
  1785. // s1 = current function
  1786. StackSym * paramSym = GetImplicitParamSlotSym(0);
  1787. IR::Opnd * srcOpnd = IR::SymOpnd::New(paramSym, TyMachReg, func);
  1788. this->LoadHelperArgument(instrArgs, srcOpnd);
  1789. // Save the newly-created args object to its dedicated stack slot.
  1790. IR::IndirOpnd *indirOpnd = IR::IndirOpnd::New(IR::RegOpnd::New(nullptr, FRAME_REG , TyMachReg, func),
  1791. -MachArgsSlotOffset, TyMachPtr, m_func);
  1792. Lowerer::InsertMove(indirOpnd, instrArgs->GetDst(), instrArgs->m_next);
  1793. }
  1794. this->ChangeToHelperCall(instrArgs, IR::HelperOp_LoadHeapArguments);
  1795. }
  1796. return instrPrev;
  1797. }
  1798. ///----------------------------------------------------------------------------
  1799. ///
  1800. /// LowererMD::LoadHeapArgsCached
  1801. ///
  1802. /// Load the heap-based arguments object using a cached scope
  1803. ///
  1804. ///----------------------------------------------------------------------------
  1805. IR::Instr *
  1806. LowererMD::LoadHeapArgsCached(IR::Instr * instrArgs)
  1807. {
  1808. Assert(!this->m_func->GetJITFunctionBody()->IsGenerator());
  1809. ASSERT_INLINEE_FUNC(instrArgs);
  1810. Func *func = instrArgs->m_func;
  1811. IR::Instr * instrPrev = instrArgs->m_prev;
  1812. if (instrArgs->m_func->IsStackArgsEnabled())
  1813. {
  1814. instrArgs->m_opcode = Js::OpCode::MOV;
  1815. instrArgs->ReplaceSrc1(IR::AddrOpnd::NewNull(func));
  1816. if (PHASE_TRACE1(Js::StackArgFormalsOptPhase) && func->GetJITFunctionBody()->GetInParamsCount() > 1)
  1817. {
  1818. Output::Print(_u("StackArgFormals : %s (%d) :Removing Heap Arguments object creation in Lowerer. \n"), instrArgs->m_func->GetJITFunctionBody()->GetDisplayName(), instrArgs->m_func->GetFunctionNumber());
  1819. Output::Flush();
  1820. }
  1821. }
  1822. else
  1823. {
  1824. // s7 = formals are let decls
  1825. // s6 = memory context
  1826. // s5 = local frame instance
  1827. // s4 = address of first actual argument (after "this")
  1828. // s3 = formal argument count
  1829. // s2 = actual argument count
  1830. // s1 = current function
  1831. // dst = JavascriptOperators::LoadHeapArgsCached(s1, s2, s3, s4, s5, s6, s7)
  1832. // s7 = formals are let decls
  1833. IR::Opnd * formalsAreLetDecls = IR::IntConstOpnd::New((IntConstType)(instrArgs->m_opcode == Js::OpCode::LdLetHeapArgsCached), TyUint8, func);
  1834. this->LoadHelperArgument(instrArgs, formalsAreLetDecls);
  1835. // s6 = memory context
  1836. this->m_lowerer->LoadScriptContext(instrArgs);
  1837. // s5 = local frame instance
  1838. IR::Opnd * frameObj = instrArgs->UnlinkSrc1();
  1839. this->LoadHelperArgument(instrArgs, frameObj);
  1840. if (func->IsInlinee())
  1841. {
  1842. // s4 = address of first actual argument (after "this").
  1843. StackSym *firstRealArgSlotSym = func->GetInlineeArgvSlotOpnd()->m_sym->AsStackSym();
  1844. this->m_func->SetArgOffset(firstRealArgSlotSym, firstRealArgSlotSym->m_offset + MachPtr);
  1845. IR::Instr *instr = this->m_lowerer->InsertLoadStackAddress(firstRealArgSlotSym, instrArgs);
  1846. this->LoadHelperArgument(instrArgs, instr->GetDst());
  1847. // s3 = formal argument count (without counting "this").
  1848. uint32 formalsCount = func->GetJITFunctionBody()->GetInParamsCount() - 1;
  1849. this->LoadHelperArgument(instrArgs, IR::IntConstOpnd::New(formalsCount, TyUint32, func));
  1850. // s2 = actual argument count (without counting "this").
  1851. this->LoadHelperArgument(instrArgs, IR::IntConstOpnd::New(func->actualCount - 1, TyUint32, func));
  1852. // s1 = current function.
  1853. this->LoadHelperArgument(instrArgs, func->GetInlineeFunctionObjectSlotOpnd());
  1854. // Save the newly-created args object to its dedicated stack slot.
  1855. IR::SymOpnd *argObjSlotOpnd = func->GetInlineeArgumentsObjectSlotOpnd();
  1856. Lowerer::InsertMove(argObjSlotOpnd, instrArgs->GetDst(), instrArgs->m_next);
  1857. }
  1858. else
  1859. {
  1860. // s4 = address of first actual argument (after "this")
  1861. IR::Instr * instr = this->LoadInputParamPtr(instrArgs);
  1862. this->LoadHelperArgument(instrArgs, instr->GetDst());
  1863. // s3 = formal argument count (without counting "this")
  1864. uint32 formalsCount = func->GetInParamsCount() - 1;
  1865. this->LoadHelperArgument(instrArgs, IR::IntConstOpnd::New(formalsCount, TyMachReg, func));
  1866. // s2 = actual argument count (without counting "this")
  1867. instr = this->LoadInputParamCount(instrArgs, -1);
  1868. this->LoadHelperArgument(instrArgs, instr->GetDst());
  1869. // s1 = current function
  1870. StackSym * paramSym = GetImplicitParamSlotSym(0);
  1871. IR::Opnd * srcOpnd = IR::SymOpnd::New(paramSym, TyMachReg, func);
  1872. this->LoadHelperArgument(instrArgs, srcOpnd);
  1873. // Save the newly-created args object to its dedicated stack slot.
  1874. IR::IndirOpnd *indirOpnd = IR::IndirOpnd::New(IR::RegOpnd::New(nullptr, FRAME_REG, TyMachReg, func),
  1875. -MachArgsSlotOffset, TyMachPtr, m_func);
  1876. Lowerer::InsertMove(indirOpnd, instrArgs->GetDst(), instrArgs->m_next);
  1877. }
  1878. this->ChangeToHelperCall(instrArgs, IR::HelperOp_LoadHeapArgsCached);
  1879. }
  1880. return instrPrev;
  1881. }
  1882. ///----------------------------------------------------------------------------
  1883. ///
  1884. /// LowererMD::ChangeToHelperCall
  1885. ///
  1886. /// Change the current instruction to a call to the given helper.
  1887. ///
  1888. ///----------------------------------------------------------------------------
  1889. IR::Instr *
  1890. LowererMD::ChangeToHelperCall(IR::Instr * callInstr, IR::JnHelperMethod helperMethod, IR::LabelInstr *labelBailOut,
  1891. IR::Opnd *opndInstance, IR::PropertySymOpnd *propSymOpnd, bool isHelperContinuation)
  1892. {
  1893. #if DBG
  1894. this->m_lowerer->ReconcileWithLowererStateOnHelperCall(callInstr, helperMethod);
  1895. #endif
  1896. IR::Instr * bailOutInstr = callInstr;
  1897. if (callInstr->HasBailOutInfo())
  1898. {
  1899. const IR::BailOutKind bailOutKind = callInstr->GetBailOutKind();
  1900. if (bailOutKind == IR::BailOutOnNotPrimitive)
  1901. {
  1902. callInstr = IR::Instr::New(callInstr->m_opcode, callInstr->m_func);
  1903. bailOutInstr->TransferTo(callInstr);
  1904. bailOutInstr->InsertBefore(callInstr);
  1905. bailOutInstr->m_opcode = Js::OpCode::BailOnNotPrimitive;
  1906. bailOutInstr->SetSrc1(opndInstance);
  1907. }
  1908. else if (BailOutInfo::IsBailOutOnImplicitCalls(bailOutKind))
  1909. {
  1910. bailOutInstr = this->m_lowerer->SplitBailOnImplicitCall(callInstr);
  1911. }
  1912. else
  1913. {
  1914. AssertMsg(false, "Unexpected BailOutKind, are we adding new BailOutKind on instructions?");
  1915. }
  1916. }
  1917. IR::HelperCallOpnd *helperCallOpnd = Lowerer::CreateHelperCallOpnd(helperMethod, this->GetHelperArgsCount(), m_func);
  1918. if (helperCallOpnd->IsDiagHelperCallOpnd())
  1919. {
  1920. // Load arguments for the wrapper.
  1921. this->LoadHelperArgument(callInstr, IR::AddrOpnd::New((Js::Var)IR::GetMethodOriginalAddress(m_func->GetThreadContextInfo(), helperMethod), IR::AddrOpndKindDynamicMisc, m_func));
  1922. this->m_lowerer->LoadScriptContext(callInstr);
  1923. }
  1924. callInstr->SetSrc1(helperCallOpnd);
  1925. IR::Instr * instrRet = this->LowerCall(callInstr, 0);
  1926. if (bailOutInstr != callInstr)
  1927. {
  1928. // The bailout needs to be lowered after we lower the helper call because the helper argument
  1929. // has already been loaded. We need to drain them on AMD64 before starting another helper call
  1930. if (bailOutInstr->m_opcode == Js::OpCode::BailOnNotPrimitive)
  1931. {
  1932. this->m_lowerer->LowerBailOnTrue(bailOutInstr, labelBailOut);
  1933. }
  1934. else if (bailOutInstr->m_opcode == Js::OpCode::BailOnNotEqual)
  1935. {
  1936. // `SplitBailOnImplicitCall` above changes the opcode to BailOnNotEqual
  1937. Assert(BailOutInfo::IsBailOutOnImplicitCalls(bailOutInstr->GetBailOutKind()));
  1938. this->m_lowerer->LowerBailOnEqualOrNotEqual(bailOutInstr, nullptr, labelBailOut, propSymOpnd, isHelperContinuation);
  1939. }
  1940. else
  1941. {
  1942. AssertMsg(false, "Unexpected OpCode for BailOutInstruction");
  1943. }
  1944. }
  1945. return instrRet;
  1946. }
  1947. IR::Instr* LowererMD::ChangeToHelperCallMem(IR::Instr * instr, IR::JnHelperMethod helperMethod)
  1948. {
  1949. this->m_lowerer->LoadScriptContext(instr);
  1950. return this->ChangeToHelperCall(instr, helperMethod);
  1951. }
  1952. ///----------------------------------------------------------------------------
  1953. ///
  1954. /// LowererMD::ChangeToAssign
  1955. ///
  1956. /// Change to a copy. Handle riscification of operands.
  1957. ///
  1958. ///----------------------------------------------------------------------------
  1959. IR::Instr *
  1960. LowererMD::ChangeToAssignNoBarrierCheck(IR::Instr * instr)
  1961. {
  1962. return ChangeToAssign(instr, instr->GetDst()->GetType());
  1963. }
  1964. IR::Instr *
  1965. LowererMD::ChangeToAssign(IR::Instr * instr)
  1966. {
  1967. return ChangeToWriteBarrierAssign(instr, instr->m_func);
  1968. }
  1969. IR::Instr *
  1970. LowererMD::ChangeToAssign(IR::Instr * instr, IRType type)
  1971. {
  1972. Assert(!instr->HasBailOutInfo() || instr->GetBailOutKind() == IR::BailOutExpectingInteger
  1973. || instr->GetBailOutKind() == IR::BailOutExpectingString);
  1974. IR::Opnd *src = instr->GetSrc1();
  1975. if (src->IsImmediateOpnd() || src->IsLabelOpnd())
  1976. {
  1977. instr->m_opcode = Js::OpCode::LDIMM;
  1978. }
  1979. else if(type == TyFloat32 && instr->GetDst()->IsRegOpnd())
  1980. {
  1981. Assert(instr->GetSrc1()->IsFloat32());
  1982. instr->m_opcode = Js::OpCode::VLDR32;
  1983. // Note that we allocate double register for single precision floats as well, as the register allocator currently
  1984. // does not support 32-bit float registers
  1985. instr->ReplaceDst(instr->GetDst()->UseWithNewType(TyFloat64, instr->m_func));
  1986. if(instr->GetSrc1()->IsRegOpnd())
  1987. {
  1988. instr->ReplaceSrc1(instr->GetSrc1()->UseWithNewType(TyFloat64, instr->m_func));
  1989. }
  1990. }
  1991. else
  1992. {
  1993. instr->m_opcode = LowererMD::GetMoveOp(type);
  1994. }
  1995. AutoRestoreLegalize restore(instr->m_func, false);
  1996. LegalizeMD::LegalizeInstr(instr);
  1997. return instr;
  1998. }
  1999. IR::Instr *
  2000. LowererMD::ChangeToWriteBarrierAssign(IR::Instr * assignInstr, const Func* func)
  2001. {
  2002. #ifdef RECYCLER_WRITE_BARRIER_JIT
  2003. // WriteBarrier-TODO- Implement ARM JIT
  2004. #endif
  2005. return ChangeToAssignNoBarrierCheck(assignInstr);
  2006. }
  2007. ///----------------------------------------------------------------------------
  2008. ///
  2009. /// LowererMD::LowerRet
  2010. ///
  2011. /// Lower Ret to "MOV EAX, src"
  2012. /// The real RET is inserted at the exit of the function when emitting the
  2013. /// epilog.
  2014. ///
  2015. ///----------------------------------------------------------------------------
  2016. IR::Instr *
  2017. LowererMD::LowerRet(IR::Instr * retInstr)
  2018. {
  2019. IR::RegOpnd *retReg = IR::RegOpnd::New(TyMachReg, m_func);
  2020. retReg->SetReg(RETURN_REG);
  2021. Lowerer::InsertMove(retReg, retInstr->UnlinkSrc1(), retInstr);
  2022. retInstr->SetSrc1(retReg);
  2023. return retInstr;
  2024. }
  2025. ///----------------------------------------------------------------------------
  2026. ///
  2027. /// LowererMD::MDBranchOpcode
  2028. ///
  2029. /// Map HIR branch opcode to machine-dependent equivalent.
  2030. ///
  2031. ///----------------------------------------------------------------------------
  2032. Js::OpCode
  2033. LowererMD::MDBranchOpcode(Js::OpCode opcode)
  2034. {
  2035. switch (opcode)
  2036. {
  2037. case Js::OpCode::BrEq_A:
  2038. case Js::OpCode::BrSrEq_A:
  2039. case Js::OpCode::BrNotNeq_A:
  2040. case Js::OpCode::BrSrNotNeq_A:
  2041. case Js::OpCode::BrAddr_A:
  2042. return Js::OpCode::BEQ;
  2043. case Js::OpCode::BrNeq_A:
  2044. case Js::OpCode::BrSrNeq_A:
  2045. case Js::OpCode::BrNotEq_A:
  2046. case Js::OpCode::BrSrNotEq_A:
  2047. case Js::OpCode::BrNotAddr_A:
  2048. return Js::OpCode::BNE;
  2049. case Js::OpCode::BrLt_A:
  2050. case Js::OpCode::BrNotGe_A:
  2051. return Js::OpCode::BLT;
  2052. case Js::OpCode::BrLe_A:
  2053. case Js::OpCode::BrNotGt_A:
  2054. return Js::OpCode::BLE;
  2055. case Js::OpCode::BrGt_A:
  2056. case Js::OpCode::BrNotLe_A:
  2057. return Js::OpCode::BGT;
  2058. case Js::OpCode::BrGe_A:
  2059. case Js::OpCode::BrNotLt_A:
  2060. return Js::OpCode::BGE;
  2061. case Js::OpCode::BrUnGt_A:
  2062. return Js::OpCode::BHI;
  2063. case Js::OpCode::BrUnGe_A:
  2064. return Js::OpCode::BCS;
  2065. case Js::OpCode::BrUnLt_A:
  2066. return Js::OpCode::BCC;
  2067. case Js::OpCode::BrUnLe_A:
  2068. return Js::OpCode::BLS;
  2069. default:
  2070. AssertMsg(0, "NYI");
  2071. return opcode;
  2072. }
  2073. }
  2074. Js::OpCode
  2075. LowererMD::MDUnsignedBranchOpcode(Js::OpCode opcode)
  2076. {
  2077. switch (opcode)
  2078. {
  2079. case Js::OpCode::BrEq_A:
  2080. case Js::OpCode::BrSrEq_A:
  2081. case Js::OpCode::BrSrNotNeq_A:
  2082. case Js::OpCode::BrNotNeq_A:
  2083. case Js::OpCode::BrAddr_A:
  2084. return Js::OpCode::BEQ;
  2085. case Js::OpCode::BrNeq_A:
  2086. case Js::OpCode::BrSrNeq_A:
  2087. case Js::OpCode::BrSrNotEq_A:
  2088. case Js::OpCode::BrNotEq_A:
  2089. case Js::OpCode::BrNotAddr_A:
  2090. return Js::OpCode::BNE;
  2091. case Js::OpCode::BrLt_A:
  2092. case Js::OpCode::BrNotGe_A:
  2093. return Js::OpCode::BCC;
  2094. case Js::OpCode::BrLe_A:
  2095. case Js::OpCode::BrNotGt_A:
  2096. return Js::OpCode::BLS;
  2097. case Js::OpCode::BrGt_A:
  2098. case Js::OpCode::BrNotLe_A:
  2099. return Js::OpCode::BHI;
  2100. case Js::OpCode::BrGe_A:
  2101. case Js::OpCode::BrNotLt_A:
  2102. return Js::OpCode::BCS;
  2103. default:
  2104. AssertMsg(0, "NYI");
  2105. return opcode;
  2106. }
  2107. }
  2108. Js::OpCode LowererMD::MDCompareWithZeroBranchOpcode(Js::OpCode opcode)
  2109. {
  2110. Assert(opcode == Js::OpCode::BrLt_A || opcode == Js::OpCode::BrGe_A);
  2111. return opcode == Js::OpCode::BrLt_A ? Js::OpCode::BMI : Js::OpCode::BPL;
  2112. }
  2113. void LowererMD::ChangeToAdd(IR::Instr *const instr, const bool needFlags)
  2114. {
  2115. Assert(instr);
  2116. Assert(instr->GetDst());
  2117. Assert(instr->GetSrc1());
  2118. Assert(instr->GetSrc2());
  2119. if(instr->GetDst()->IsFloat64())
  2120. {
  2121. Assert(instr->GetSrc1()->IsFloat64());
  2122. Assert(instr->GetSrc2()->IsFloat64());
  2123. Assert(!needFlags);
  2124. instr->m_opcode = Js::OpCode::VADDF64;
  2125. return;
  2126. }
  2127. instr->m_opcode = needFlags ? Js::OpCode::ADDS : Js::OpCode::ADD;
  2128. }
  2129. void LowererMD::ChangeToSub(IR::Instr *const instr, const bool needFlags)
  2130. {
  2131. Assert(instr);
  2132. Assert(instr->GetDst());
  2133. Assert(instr->GetSrc1());
  2134. Assert(instr->GetSrc2());
  2135. if(instr->GetDst()->IsFloat64())
  2136. {
  2137. Assert(instr->GetSrc1()->IsFloat64());
  2138. Assert(instr->GetSrc2()->IsFloat64());
  2139. Assert(!needFlags);
  2140. instr->m_opcode = Js::OpCode::VSUBF64;
  2141. return;
  2142. }
  2143. instr->m_opcode = needFlags ? Js::OpCode::SUBS : Js::OpCode::SUB;
  2144. }
  2145. void LowererMD::ChangeToShift(IR::Instr *const instr, const bool needFlags)
  2146. {
  2147. Assert(instr);
  2148. Assert(instr->GetDst());
  2149. Assert(instr->GetSrc1());
  2150. Assert(instr->GetSrc2());
  2151. Func *const func = instr->m_func;
  2152. switch(instr->m_opcode)
  2153. {
  2154. case Js::OpCode::Shl_A:
  2155. case Js::OpCode::Shl_I4:
  2156. Assert(!needFlags); // not implemented
  2157. instr->m_opcode = Js::OpCode::LSL;
  2158. break;
  2159. case Js::OpCode::Shr_A:
  2160. case Js::OpCode::Shr_I4:
  2161. instr->m_opcode = needFlags ? Js::OpCode::ASRS : Js::OpCode::ASR;
  2162. break;
  2163. case Js::OpCode::ShrU_A:
  2164. case Js::OpCode::ShrU_I4:
  2165. Assert(!needFlags); // not implemented
  2166. instr->m_opcode = Js::OpCode::LSR;
  2167. break;
  2168. default:
  2169. Assert(false);
  2170. __assume(false);
  2171. }
  2172. // Javascript requires the ShiftCount is masked to the bottom 5 bits.
  2173. if(instr->GetSrc2()->IsIntConstOpnd())
  2174. {
  2175. // In the constant case, do the mask manually.
  2176. IntConstType immed = instr->GetSrc2()->AsIntConstOpnd()->GetValue() & 0x1f;
  2177. if (immed == 0)
  2178. {
  2179. // Shift by zero is just a move, and the shift-right instructions
  2180. // don't permit encoding of a zero shift amount.
  2181. instr->m_opcode = Js::OpCode::MOV;
  2182. instr->FreeSrc2();
  2183. }
  2184. else
  2185. {
  2186. instr->GetSrc2()->AsIntConstOpnd()->SetValue(immed);
  2187. }
  2188. }
  2189. else
  2190. {
  2191. // In the variable case, generate code to do the mask.
  2192. IR::Opnd *const src2 = instr->UnlinkSrc2();
  2193. instr->SetSrc2(IR::RegOpnd::New(TyMachReg, func));
  2194. IR::Instr *const newInstr = IR::Instr::New(
  2195. Js::OpCode::AND, instr->GetSrc2(), src2, IR::IntConstOpnd::New(0x1f, TyInt8, func), func);
  2196. instr->InsertBefore(newInstr);
  2197. }
  2198. }
  2199. const uint16
  2200. LowererMD::GetFormalParamOffset()
  2201. {
  2202. //In ARM formal params are offset into the param area.
  2203. //So we only count the non-user params (Function object & CallInfo and let the encoder account for the saved R11 and LR
  2204. return 2;
  2205. }
  2206. ///----------------------------------------------------------------------------
  2207. ///
  2208. /// LowererMD::LowerCondBranch
  2209. ///
  2210. ///----------------------------------------------------------------------------
  2211. IR::Instr *
  2212. LowererMD::LowerCondBranch(IR::Instr * instr)
  2213. {
  2214. AssertMsg(instr->GetSrc1() != nullptr, "Expected src opnds on conditional branch");
  2215. IR::Opnd * opndSrc1 = instr->UnlinkSrc1();
  2216. IR::Instr * instrPrev = nullptr;
  2217. switch (instr->m_opcode)
  2218. {
  2219. case Js::OpCode::BrTrue_A:
  2220. case Js::OpCode::BrOnNotEmpty:
  2221. case Js::OpCode::BrNotNull_A:
  2222. case Js::OpCode::BrOnObject_A:
  2223. case Js::OpCode::BrOnObjectOrNull_A:
  2224. case Js::OpCode::BrOnConstructor_A:
  2225. case Js::OpCode::BrOnClassConstructor:
  2226. case Js::OpCode::BrOnBaseConstructorKind:
  2227. Assert(!opndSrc1->IsFloat64());
  2228. AssertMsg(opndSrc1->IsRegOpnd(),"NYI for other operands");
  2229. AssertMsg(instr->GetSrc2() == nullptr, "Expected 1 src on boolean branch");
  2230. instrPrev = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  2231. instrPrev->SetSrc1(opndSrc1);
  2232. instrPrev->SetSrc2(IR::IntConstOpnd::New(0, TyInt32, m_func));
  2233. instr->InsertBefore(instrPrev);
  2234. instr->m_opcode = Js::OpCode::BNE;
  2235. break;
  2236. case Js::OpCode::BrFalse_A:
  2237. case Js::OpCode::BrOnEmpty:
  2238. Assert(!opndSrc1->IsFloat64());
  2239. AssertMsg(opndSrc1->IsRegOpnd(),"NYI for other operands");
  2240. AssertMsg(instr->GetSrc2() == nullptr, "Expected 1 src on boolean branch");
  2241. instrPrev = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  2242. instrPrev->SetSrc1(opndSrc1);
  2243. instrPrev->SetSrc2(IR::IntConstOpnd::New(0, TyInt32, m_func));
  2244. instr->InsertBefore(instrPrev);
  2245. instr->m_opcode = Js::OpCode::BEQ;
  2246. break;
  2247. default:
  2248. IR::Opnd * opndSrc2 = instr->UnlinkSrc2();
  2249. AssertMsg(opndSrc2 != nullptr, "Expected 2 src's on non-boolean branch");
  2250. if (opndSrc1->IsFloat64())
  2251. {
  2252. AssertMsg(opndSrc1->IsRegOpnd(),"NYI for other operands");
  2253. Assert(opndSrc2->IsFloat64());
  2254. Assert(opndSrc2->IsRegOpnd() && opndSrc1->IsRegOpnd());
  2255. //This comparison updates the FPSCR - floating point status control register
  2256. instrPrev = IR::Instr::New(Js::OpCode::VCMPF64, this->m_func);
  2257. instrPrev->SetSrc1(opndSrc1);
  2258. instrPrev->SetSrc2(opndSrc2);
  2259. instr->InsertBefore(instrPrev);
  2260. LegalizeMD::LegalizeInstr(instrPrev);
  2261. //Transfer the result to ARM status register control register.
  2262. instrPrev = IR::Instr::New(Js::OpCode::VMRS, this->m_func);
  2263. instr->InsertBefore(instrPrev);
  2264. instr->m_opcode = LowererMD::MDBranchOpcode(instr->m_opcode);
  2265. }
  2266. else
  2267. {
  2268. AssertMsg(opndSrc2->IsRegOpnd() || opndSrc2->IsIntConstOpnd() || (opndSrc2->IsAddrOpnd()), "NYI for other operands");
  2269. instrPrev = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  2270. instrPrev->SetSrc1(opndSrc1);
  2271. instrPrev->SetSrc2(opndSrc2);
  2272. instr->InsertBefore(instrPrev);
  2273. LegalizeMD::LegalizeInstr(instrPrev);
  2274. instr->m_opcode = MDBranchOpcode(instr->m_opcode);
  2275. }
  2276. break;
  2277. }
  2278. return instr;
  2279. }
  2280. ///----------------------------------------------------------------------------
  2281. ///
  2282. /// LowererMD::ForceDstToReg
  2283. ///
  2284. ///----------------------------------------------------------------------------
  2285. IR::Instr*
  2286. LowererMD::ForceDstToReg(IR::Instr *instr)
  2287. {
  2288. IR::Opnd * dst = instr->GetDst();
  2289. if (dst->IsRegOpnd())
  2290. {
  2291. return instr;
  2292. }
  2293. IR::Instr * newInstr = instr->SinkDst(Js::OpCode::Ld_A);
  2294. LowererMD::ChangeToAssign(newInstr);
  2295. return newInstr;
  2296. }
  2297. IR::Instr *
  2298. LowererMD::LoadFunctionObjectOpnd(IR::Instr *instr, IR::Opnd *&functionObjOpnd)
  2299. {
  2300. IR::Opnd * src1 = instr->GetSrc1();
  2301. IR::Instr * instrPrev = instr->m_prev;
  2302. if (src1 == nullptr)
  2303. {
  2304. IR::RegOpnd * regOpnd = IR::RegOpnd::New(TyMachPtr, m_func);
  2305. //function object is first argument and mark it as IsParamSlotSym.
  2306. StackSym *paramSym = GetImplicitParamSlotSym(0);
  2307. IR::SymOpnd *paramOpnd = IR::SymOpnd::New(paramSym, TyMachPtr, m_func);
  2308. instrPrev = Lowerer::InsertMove(regOpnd, paramOpnd, instr);
  2309. functionObjOpnd = instrPrev->GetDst();
  2310. }
  2311. else
  2312. {
  2313. // Inlinee LdHomeObj, use the function object opnd on the instruction
  2314. functionObjOpnd = instr->UnlinkSrc1();
  2315. if (!functionObjOpnd->IsRegOpnd())
  2316. {
  2317. Assert(functionObjOpnd->IsAddrOpnd());
  2318. }
  2319. }
  2320. return instrPrev;
  2321. }
  2322. void
  2323. LowererMD::GenerateFastDivByPow2(IR::Instr *instrDiv)
  2324. {
  2325. //// Given:
  2326. //// dst = Div_A src1, src2
  2327. //// where src2 == power of 2
  2328. ////
  2329. //// Generate:
  2330. //// (observation: positive q divides by p equally, where p = power of 2, if q's binary representation
  2331. //// has all zeroes to the right of p's power 2 bit, try to see if that is the case)
  2332. //// s1 = AND src1, 0x80000001 | ((src2Value - 1) << 1)
  2333. //// CMP s1, 1
  2334. //// BNE $doesntDivideEqually
  2335. //// s1 = ASR src1, log2(src2Value) -- do the equal divide
  2336. //// dst = EOR s1, 1 -- restore tagged int bit
  2337. //// B $done
  2338. //// $doesntDivideEqually:
  2339. //// (now check if it divides with the remainder of 1, for which we can do integer divide and accommodate with +0.5
  2340. //// note that we need only the part that is to the left of p's power 2 bit)
  2341. //// s1 = AND s1, 0x80000001 | (src2Value - 1)
  2342. //// CMP s1, 1
  2343. //// BNE $helper
  2344. //// s1 = ASR src1, log2(src2Value) + 1 -- do the integer divide and also shift out the tagged int bit
  2345. //// PUSH 0xXXXXXXXX (ScriptContext)
  2346. //// PUSH s1
  2347. //// dst = CALL Op_FinishOddDivByPow2 -- input: actual value, scriptContext; output: JavascriptNumber with 0.5 added to the input
  2348. //// JMP $done
  2349. //// $helper:
  2350. //// ...
  2351. //// $done:
  2352. //if (instrDiv->GetSrc1()->IsRegOpnd() && instrDiv->GetSrc1()->AsRegOpnd()->m_sym->m_isNotInt)
  2353. //{
  2354. // return;
  2355. //}
  2356. //IR::Opnd *dst = instrDiv->GetDst();
  2357. //IR::Opnd *src1 = instrDiv->GetSrc1();
  2358. //IR::AddrOpnd *src2 = instrDiv->GetSrc2()->IsAddrOpnd() ? instrDiv->GetSrc2()->AsAddrOpnd() : nullptr;
  2359. //IR::LabelInstr *doesntDivideEqually = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  2360. //IR::LabelInstr *helper = IR::LabelInstr::New(Js::OpCode::Label, m_func, true);
  2361. //IR::LabelInstr *done = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  2362. //IR::RegOpnd *s1 = IR::RegOpnd::New(TyVar, m_func);
  2363. //IR::Instr *instr;
  2364. //Assert(src2 && src2->IsVar() && Js::TaggedInt::Is(src2->m_address) && (Math::IsPow2(Js::TaggedInt::ToInt32(src2->m_address))));
  2365. //int32 src2Value = Js::TaggedInt::ToInt32(src2->m_address);
  2366. //// s1 = AND src1, 0x80000001 | ((src2Value - 1) << 1)
  2367. //instr = IR::Instr::New(Js::OpCode::AND, s1, src1, IR::IntConstOpnd::New((0x80000001 | ((src2Value - 1) << 1)), TyInt32, m_func), m_func);
  2368. //instrDiv->InsertBefore(instr);
  2369. //LegalizeMD::LegalizeInstr(instr);
  2370. //// CMP s1, 1
  2371. //instr = IR::Instr::New(Js::OpCode::CMP, m_func);
  2372. //instr->SetSrc1(s1);
  2373. //instr->SetSrc2(IR::IntConstOpnd::New(1, TyInt32, m_func));
  2374. //instrDiv->InsertBefore(instr);
  2375. //// BNE $doesntDivideEqually
  2376. //instr = IR::BranchInstr::New(Js::OpCode::BNE, doesntDivideEqually, m_func);
  2377. //instrDiv->InsertBefore(instr);
  2378. //// s1 = ASR src1, log2(src2Value) -- do the equal divide
  2379. //instr = IR::Instr::New(Js::OpCode::ASR, s1, src1, IR::IntConstOpnd::New(Math::Log2(src2Value), TyInt32, m_func), m_func);
  2380. //instrDiv->InsertBefore(instr);
  2381. //LegalizeMD::LegalizeInstr(instr);
  2382. //// dst = ORR s1, 1 -- restore tagged int bit
  2383. //instr = IR::Instr::New(Js::OpCode::ORR, dst, s1, IR::IntConstOpnd::New(1, TyInt32, m_func), m_func);
  2384. //instrDiv->InsertBefore(instr);
  2385. //LegalizeMD::LegalizeInstr(instr);
  2386. //
  2387. //// B $done
  2388. //instr = IR::BranchInstr::New(Js::OpCode::B, done, m_func);
  2389. //instrDiv->InsertBefore(instr);
  2390. //// $doesntDivideEqually:
  2391. //instrDiv->InsertBefore(doesntDivideEqually);
  2392. //// s1 = AND s1, 0x80000001 | (src2Value - 1)
  2393. //instr = IR::Instr::New(Js::OpCode::AND, s1, s1, IR::IntConstOpnd::New((0x80000001 | (src2Value - 1)), TyInt32, m_func), m_func);
  2394. //instrDiv->InsertBefore(instr);
  2395. //// CMP s1, 1
  2396. //instr = IR::Instr::New(Js::OpCode::CMP, m_func);
  2397. //instr->SetSrc1(s1);
  2398. //instr->SetSrc2(IR::IntConstOpnd::New(1, TyInt32, m_func));
  2399. //instrDiv->InsertBefore(instr);
  2400. //// BNE $helper
  2401. //instrDiv->InsertBefore(IR::BranchInstr::New(Js::OpCode::BNE, helper, m_func));
  2402. //// s1 = ASR src1, log2(src2Value) + 1 -- do the integer divide and also shift out the tagged int bit
  2403. //instr = IR::Instr::New(Js::OpCode::ASR, s1, src1, IR::IntConstOpnd::New(Math::Log2(src2Value) + 1, TyInt32, m_func), m_func);
  2404. //instrDiv->InsertBefore(instr);
  2405. //LegalizeMD::LegalizeInstr(instr);
  2406. //// Arg2: scriptContext
  2407. //IR::JnHelperMethod helperMethod;
  2408. //if (instrDiv->dstIsTempNumber)
  2409. //{
  2410. // // Var JavascriptMath::FinishOddDivByPow2_InPlace(uint32 value, ScriptContext *scriptContext, __out JavascriptNumber* result)
  2411. // helperMethod = IR::HelperOp_FinishOddDivByPow2InPlace;
  2412. // Assert(dst->IsRegOpnd());
  2413. // StackSym * tempNumberSym = this->m_lowerer->GetTempNumberSym(dst, instr->dstIsTempNumberTransferred);
  2414. // instr = this->m_lowerer->InsertLoadStackAddress(tempNumberSym, instrDiv);
  2415. // LegalizeMD::LegalizeInstr(instr);
  2416. // this->LoadHelperArgument(instrDiv, instr->GetDst());
  2417. //}
  2418. //else
  2419. //{
  2420. // // Var JavascriptMath::FinishOddDivByPow2(uint32 value, ScriptContext *scriptContext)
  2421. // helperMethod = IR::HelperOp_FinishOddDivByPow2;
  2422. //}
  2423. //this->m_lowerer->LoadScriptContext(instrDiv);
  2424. //// Arg1: value
  2425. //this->LoadHelperArgument(instrDiv, s1);
  2426. //// dst = CALL Op_FinishOddDivByPow2 -- input: actual value, output: JavascriptNumber with 0.5 added to the input
  2427. //instr = IR::Instr::New(Js::OpCode::Call, dst, IR::HelperCallOpnd::New(helperMethod, m_func), m_func);
  2428. //instrDiv->InsertBefore(instr);
  2429. //this->LowerCall(instr, 0);
  2430. //// JMP $done
  2431. //instrDiv->InsertBefore(IR::BranchInstr::New(Js::OpCode::B, done, m_func));
  2432. //// $helper:
  2433. //instrDiv->InsertBefore(helper);
  2434. //// $done:
  2435. //instrDiv->InsertAfter(done);
  2436. return;
  2437. }
  2438. ///----------------------------------------------------------------------------
  2439. ///
  2440. /// LowererMD::GenerateFastCmSrEqConst
  2441. ///
  2442. ///----------------------------------------------------------------------------
  2443. bool
  2444. LowererMD::GenerateFastCmSrXxConst(IR::Instr *instr)
  2445. {
  2446. //
  2447. // Given:
  2448. // s1 = CmSrXX_A s2, s3
  2449. // where either s2 or s3 is 'null', 'true' or 'false'
  2450. //
  2451. // Generate:
  2452. //
  2453. // CMP s2, s3
  2454. // JEQ $mov_res
  2455. // MOV s1, eq ? Library.GetFalse() : Library.GetTrue()
  2456. // JMP $done
  2457. // $mov_res:
  2458. // MOV s1, eq ? Library.GetTrue() : Library.GetFalse()
  2459. // $done:
  2460. //
  2461. Assert(m_lowerer->IsConstRegOpnd(instr->GetSrc2()->AsRegOpnd()));
  2462. return false;
  2463. }
  2464. void LowererMD::GenerateFastCmXxI4(IR::Instr *instr)
  2465. {
  2466. this->GenerateFastCmXx(instr);
  2467. }
  2468. void LowererMD::GenerateFastCmXxR8(IR::Instr * instr)
  2469. {
  2470. this->GenerateFastCmXx(instr);
  2471. }
  2472. void LowererMD::GenerateFastCmXx(IR::Instr *instr)
  2473. {
  2474. // For float src:
  2475. // LDIMM dst, trueResult
  2476. // FCMP src1, src2
  2477. // - BVS $done (NaN check iff B.cond is BNE)
  2478. // B.cond $done
  2479. // LDIMM dst, falseResult
  2480. // $done
  2481. // For Int src:
  2482. // LDIMM dst, trueResult
  2483. // CMP src1, src2
  2484. // B.cond $done
  2485. // LDIMM dst, falseResult
  2486. // $done:
  2487. IR::Opnd * src1 = instr->UnlinkSrc1();
  2488. IR::Opnd * src2 = instr->UnlinkSrc2();
  2489. IR::Opnd * dst = instr->UnlinkDst();
  2490. bool isIntDst = dst->AsRegOpnd()->m_sym->IsInt32();
  2491. bool isFloatSrc = src1->IsFloat();
  2492. Assert(!isFloatSrc || src2->IsFloat());
  2493. Assert(!src1->IsInt64() || src2->IsInt64());
  2494. Assert(!isFloatSrc || AutoSystemInfo::Data.SSE2Available());
  2495. Assert(src1->IsRegOpnd());
  2496. IR::Opnd * opndTrue;
  2497. IR::Opnd * opndFalse;
  2498. IR::Instr * newInstr;
  2499. IR::LabelInstr * done = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  2500. if (dst->IsEqual(src1))
  2501. {
  2502. IR::RegOpnd *newSrc1 = IR::RegOpnd::New(src1->GetType(), m_func);
  2503. Lowerer::InsertMove(newSrc1, src1, instr);
  2504. src1 = newSrc1;
  2505. }
  2506. if (dst->IsEqual(src2))
  2507. {
  2508. IR::RegOpnd *newSrc2 = IR::RegOpnd::New(src1->GetType(), m_func);
  2509. Lowerer::InsertMove(newSrc2, src2, instr);
  2510. src2 = newSrc2;
  2511. }
  2512. if (isIntDst)
  2513. {
  2514. opndTrue = IR::IntConstOpnd::New(1, TyInt32, this->m_func);
  2515. opndFalse = IR::IntConstOpnd::New(0, TyInt32, this->m_func);
  2516. }
  2517. else
  2518. {
  2519. opndTrue = this->m_lowerer->LoadLibraryValueOpnd(instr, LibraryValue::ValueTrue);
  2520. opndFalse = this->m_lowerer->LoadLibraryValueOpnd(instr, LibraryValue::ValueFalse);
  2521. }
  2522. Lowerer::InsertMove(dst, opndTrue, instr);
  2523. // CMP src1, src2
  2524. newInstr = IR::Instr::New(isFloatSrc ? Js::OpCode::VCMPF64 : Js::OpCode::CMP, this->m_func);
  2525. newInstr->SetSrc1(src1);
  2526. newInstr->SetSrc2(src2);
  2527. instr->InsertBefore(newInstr);
  2528. LowererMD::Legalize(newInstr);
  2529. if (isFloatSrc)
  2530. {
  2531. instr->InsertBefore(IR::Instr::New(Js::OpCode::VMRS, this->m_func));
  2532. }
  2533. bool addNaNCheck = false;
  2534. Js::OpCode opcode = Js::OpCode::InvalidOpCode;
  2535. switch (instr->m_opcode)
  2536. {
  2537. case Js::OpCode::CmEq_A:
  2538. case Js::OpCode::CmSrEq_A:
  2539. case Js::OpCode::CmEq_I4:
  2540. opcode = Js::OpCode::BEQ;
  2541. break;
  2542. case Js::OpCode::CmNeq_A:
  2543. case Js::OpCode::CmSrNeq_A:
  2544. case Js::OpCode::CmNeq_I4:
  2545. opcode = Js::OpCode::BNE;
  2546. addNaNCheck = isFloatSrc;
  2547. break;
  2548. case Js::OpCode::CmGt_A:
  2549. case Js::OpCode::CmGt_I4:
  2550. opcode = Js::OpCode::BGT;
  2551. break;
  2552. case Js::OpCode::CmGe_A:
  2553. case Js::OpCode::CmGe_I4:
  2554. opcode = Js::OpCode::BGE;
  2555. break;
  2556. case Js::OpCode::CmLt_A:
  2557. case Js::OpCode::CmLt_I4:
  2558. //Can't use BLT as is set when the operands are unordered (NaN).
  2559. opcode = isFloatSrc ? Js::OpCode::BCC : Js::OpCode::BLT;
  2560. break;
  2561. case Js::OpCode::CmLe_A:
  2562. case Js::OpCode::CmLe_I4:
  2563. //Can't use BLE as it is set when the operands are unordered (NaN).
  2564. opcode = isFloatSrc ? Js::OpCode::BLS : Js::OpCode::BLE;
  2565. break;
  2566. case Js::OpCode::CmUnGt_A:
  2567. case Js::OpCode::CmUnGt_I4:
  2568. opcode = Js::OpCode::BHI;
  2569. break;
  2570. case Js::OpCode::CmUnGe_A:
  2571. case Js::OpCode::CmUnGe_I4:
  2572. opcode = Js::OpCode::BCS;
  2573. break;
  2574. case Js::OpCode::CmUnLt_A:
  2575. case Js::OpCode::CmUnLt_I4:
  2576. opcode = Js::OpCode::BCC;
  2577. break;
  2578. case Js::OpCode::CmUnLe_A:
  2579. case Js::OpCode::CmUnLe_I4:
  2580. opcode = Js::OpCode::BLS;
  2581. break;
  2582. default: Assert(false);
  2583. }
  2584. if (addNaNCheck)
  2585. {
  2586. newInstr = IR::BranchInstr::New(Js::OpCode::BVS, done, m_func);
  2587. instr->InsertBefore(newInstr);
  2588. }
  2589. newInstr = IR::BranchInstr::New(opcode, done, m_func);
  2590. instr->InsertBefore(newInstr);
  2591. Lowerer::InsertMove(dst, opndFalse, instr);
  2592. instr->InsertBefore(done);
  2593. instr->Remove();
  2594. }
  2595. ///----------------------------------------------------------------------------
  2596. ///
  2597. /// LowererMD::GenerateFastCmXxTaggedInt
  2598. ///
  2599. ///----------------------------------------------------------------------------
  2600. bool LowererMD::GenerateFastCmXxTaggedInt(IR::Instr *instr, bool isInHelper /* = false */)
  2601. {
  2602. // The idea is to do an inline compare if we can prove that both sources
  2603. // are tagged ints (i.e., are vars with the low bit set).
  2604. //
  2605. // Given:
  2606. //
  2607. // Cmxx_A dst, src1, src2
  2608. //
  2609. // Generate:
  2610. //
  2611. // (If not Int31's, goto $helper)
  2612. // LDIMM dst, trueResult
  2613. // CMP src1, src2
  2614. // BEQ $fallthru
  2615. // LDIMM dst, falseResult
  2616. // B $fallthru
  2617. // $helper:
  2618. // (caller will generate normal helper call sequence)
  2619. // $fallthru:
  2620. IR::Opnd * src1 = instr->GetSrc1();
  2621. IR::Opnd * src2 = instr->GetSrc2();
  2622. IR::Opnd * dst = instr->GetDst();
  2623. IR::LabelInstr * helper = IR::LabelInstr::New(Js::OpCode::Label, m_func, true);
  2624. IR::LabelInstr * fallthru = IR::LabelInstr::New(Js::OpCode::Label, m_func, isInHelper);
  2625. Assert(src1 && src2 && dst);
  2626. // Not tagged ints?
  2627. if (src1->IsRegOpnd() && src1->AsRegOpnd()->m_sym->m_isNotNumber)
  2628. {
  2629. return false;
  2630. }
  2631. if (src2->IsRegOpnd() && src2->AsRegOpnd()->m_sym->m_isNotNumber)
  2632. {
  2633. return false;
  2634. }
  2635. Js::OpCode opcode = Js::OpCode::InvalidOpCode;
  2636. switch ( instr->m_opcode)
  2637. {
  2638. case Js::OpCode::CmEq_A:
  2639. case Js::OpCode::CmSrEq_A:
  2640. case Js::OpCode::CmEq_I4:
  2641. opcode = Js::OpCode::BEQ;
  2642. break;
  2643. case Js::OpCode::CmNeq_A:
  2644. case Js::OpCode::CmSrNeq_A:
  2645. case Js::OpCode::CmNeq_I4:
  2646. opcode = Js::OpCode::BNE;
  2647. break;
  2648. case Js::OpCode::CmGt_A:
  2649. case Js::OpCode::CmGt_I4:
  2650. opcode = Js::OpCode::BGT;
  2651. break;
  2652. case Js::OpCode::CmGe_A:
  2653. case Js::OpCode::CmGe_I4:
  2654. opcode = Js::OpCode::BGE;
  2655. break;
  2656. case Js::OpCode::CmLt_A:
  2657. case Js::OpCode::CmLt_I4:
  2658. opcode = Js::OpCode::BLT;
  2659. break;
  2660. case Js::OpCode::CmLe_A:
  2661. case Js::OpCode::CmLe_I4:
  2662. opcode = Js::OpCode::BLE;
  2663. break;
  2664. case Js::OpCode::CmUnGt_A:
  2665. case Js::OpCode::CmUnGt_I4:
  2666. opcode = Js::OpCode::BHI;
  2667. break;
  2668. case Js::OpCode::CmUnGe_A:
  2669. case Js::OpCode::CmUnGe_I4:
  2670. opcode = Js::OpCode::BCS;
  2671. break;
  2672. case Js::OpCode::CmUnLt_A:
  2673. case Js::OpCode::CmUnLt_I4:
  2674. opcode = Js::OpCode::BCC;
  2675. break;
  2676. case Js::OpCode::CmUnLe_A:
  2677. case Js::OpCode::CmUnLe_I4:
  2678. opcode = Js::OpCode::BLS;
  2679. break;
  2680. default: Assert(false);
  2681. }
  2682. // Tagged ints?
  2683. bool isTaggedInts = false;
  2684. if (src1->IsTaggedInt() || src1->IsInt32())
  2685. {
  2686. if (src2->IsTaggedInt() || src2->IsInt32())
  2687. {
  2688. isTaggedInts = true;
  2689. }
  2690. }
  2691. if (!isTaggedInts)
  2692. {
  2693. this->GenerateSmIntPairTest(instr, src1, src2, helper);
  2694. }
  2695. if (dst->IsEqual(src1))
  2696. {
  2697. IR::RegOpnd *newSrc1 = IR::RegOpnd::New(TyMachReg, m_func);
  2698. Lowerer::InsertMove(newSrc1, src1, instr);
  2699. src1 = newSrc1;
  2700. }
  2701. if (dst->IsEqual(src2))
  2702. {
  2703. IR::RegOpnd *newSrc2 = IR::RegOpnd::New(TyMachReg, m_func);
  2704. Lowerer::InsertMove(newSrc2, src2, instr);
  2705. src2 = newSrc2;
  2706. }
  2707. IR::Opnd *opndTrue, *opndFalse;
  2708. if (dst->IsInt32())
  2709. {
  2710. opndTrue = IR::IntConstOpnd::New(1, TyMachReg, this->m_func);
  2711. opndFalse = IR::IntConstOpnd::New(0, TyMachReg, this->m_func);
  2712. }
  2713. else
  2714. {
  2715. opndTrue = m_lowerer->LoadLibraryValueOpnd(instr, LibraryValue::ValueTrue);
  2716. opndFalse = m_lowerer->LoadLibraryValueOpnd(instr, LibraryValue::ValueFalse);
  2717. }
  2718. // LDIMM dst, trueResult
  2719. // CMP src1, src2
  2720. // BEQ $fallthru
  2721. // LDIMM dst, falseResult
  2722. // B $fallthru
  2723. instr->InsertBefore(IR::Instr::New(Js::OpCode::LDIMM, dst, opndTrue, m_func));
  2724. IR::Instr *instrCmp = IR::Instr::New(Js::OpCode::CMP, m_func);
  2725. instrCmp->SetSrc1(src1);
  2726. instrCmp->SetSrc2(src2);
  2727. instr->InsertBefore(instrCmp);
  2728. LegalizeMD::LegalizeInstr(instrCmp);
  2729. instr->InsertBefore(IR::BranchInstr::New(opcode, fallthru, m_func));
  2730. instr->InsertBefore(IR::Instr::New(Js::OpCode::LDIMM, dst, opndFalse, m_func));
  2731. if (isTaggedInts)
  2732. {
  2733. instr->InsertAfter(fallthru);
  2734. instr->Remove();
  2735. return true;
  2736. }
  2737. // B $fallthru
  2738. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::B, fallthru, m_func));
  2739. instr->InsertBefore(helper);
  2740. instr->InsertAfter(fallthru);
  2741. return false;
  2742. }
  2743. IR::Instr * LowererMD::GenerateConvBool(IR::Instr *instr)
  2744. {
  2745. // dst = LDIMM true
  2746. // TST src1, src2
  2747. // BNE fallthrough
  2748. // dst = LDIMM false
  2749. // fallthrough:
  2750. IR::RegOpnd *dst = instr->GetDst()->AsRegOpnd();
  2751. IR::RegOpnd *src1 = instr->GetSrc1()->AsRegOpnd();
  2752. IR::Opnd *opndTrue = m_lowerer->LoadLibraryValueOpnd(instr, LibraryValue::ValueTrue);
  2753. IR::Opnd *opndFalse = m_lowerer->LoadLibraryValueOpnd(instr, LibraryValue::ValueFalse);
  2754. IR::LabelInstr *fallthru = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  2755. // dst = LDIMM true
  2756. IR::Instr *instrFirst = IR::Instr::New(Js::OpCode::LDIMM, dst, opndTrue, m_func);
  2757. instr->InsertBefore(instrFirst);
  2758. // TST src1, src2
  2759. IR::Instr *instrTst = IR::Instr::New(Js::OpCode::TST, m_func);
  2760. instrTst->SetSrc1(src1);
  2761. instrTst->SetSrc2(src1);
  2762. instr->InsertBefore(instrTst);
  2763. LegalizeMD::LegalizeInstr(instrTst);
  2764. // BNE fallthrough
  2765. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::BNE, fallthru, m_func));
  2766. // dst = LDIMM false
  2767. instr->InsertBefore(IR::Instr::New(Js::OpCode::LDIMM, dst, opndFalse, m_func));
  2768. // fallthrough:
  2769. instr->InsertAfter(fallthru);
  2770. instr->Remove();
  2771. return instrFirst;
  2772. }
  2773. ///----------------------------------------------------------------------------
  2774. ///
  2775. /// LowererMD::GenerateFastAdd
  2776. ///
  2777. /// NOTE: We assume that only the sum of two Int31's will have 0x2 set. This
  2778. /// is only true until we have a var type with tag == 0x2.
  2779. ///
  2780. ///----------------------------------------------------------------------------
  2781. bool
  2782. LowererMD::GenerateFastAdd(IR::Instr * instrAdd)
  2783. {
  2784. // Given:
  2785. //
  2786. // dst = Add src1, src2
  2787. //
  2788. // Generate:
  2789. //
  2790. // (If not 2 Int31's, use $helper.)
  2791. // s1 = SUB src1, 1 -- get rid of one of the tag
  2792. // tmp = ADDS s1, src2 -- try an inline add
  2793. // BVS $helper
  2794. // dst = MOV tmp
  2795. // B $done
  2796. // $helper:
  2797. // (caller generates helper call)
  2798. // $done:
  2799. IR::Instr * instr;
  2800. IR::LabelInstr * labelHelper;
  2801. IR::LabelInstr * labelDone;
  2802. IR::Opnd * opndReg;
  2803. IR::Opnd * opndSrc1;
  2804. IR::Opnd * opndSrc2;
  2805. opndSrc1 = instrAdd->GetSrc1();
  2806. opndSrc2 = instrAdd->GetSrc2();
  2807. AssertMsg(opndSrc1 && opndSrc2, "Expected 2 src opnd's on Add instruction");
  2808. // Generate fastpath for Incr_A anyway -
  2809. // Incrementing strings representing integers can be inter-mixed with integers e.g. "1"++ -> converts 1 to an int and thereafter, integer increment is expected.
  2810. if (opndSrc1->IsRegOpnd() && (opndSrc1->AsRegOpnd()->m_sym->m_isNotNumber || opndSrc1->GetValueType().IsString()
  2811. || (instrAdd->m_opcode != Js::OpCode::Incr_A && opndSrc1->GetValueType().IsLikelyString())))
  2812. {
  2813. return false;
  2814. }
  2815. if (opndSrc2->IsRegOpnd() && (opndSrc2->AsRegOpnd()->m_sym->m_isNotNumber ||
  2816. opndSrc2->GetValueType().IsLikelyString()))
  2817. {
  2818. return true;
  2819. }
  2820. // Load src's at the top so we don't have to do it repeatedly.
  2821. if (!opndSrc1->IsRegOpnd())
  2822. {
  2823. opndSrc1 = IR::RegOpnd::New(opndSrc1->GetType(), this->m_func);
  2824. Lowerer::InsertMove(opndSrc1, instrAdd->GetSrc1(), instrAdd);
  2825. }
  2826. if (!opndSrc2->IsRegOpnd())
  2827. {
  2828. opndSrc2 = IR::RegOpnd::New(opndSrc2->GetType(), this->m_func);
  2829. Lowerer::InsertMove(opndSrc2, instrAdd->GetSrc2(), instrAdd);
  2830. }
  2831. labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  2832. // Tagged ints?
  2833. bool isTaggedInts = opndSrc1->IsTaggedInt() && opndSrc2->IsTaggedInt();
  2834. if (!isTaggedInts)
  2835. {
  2836. // (If not 2 Int31's, jump to $helper.)
  2837. this->GenerateSmIntPairTest(instrAdd, opndSrc1, opndSrc2, labelHelper);
  2838. }
  2839. if (opndSrc1->IsAddrOpnd())
  2840. {
  2841. // If opnd1 is a constant, just swap them.
  2842. Swap(opndSrc1, opndSrc2);
  2843. }
  2844. // s1 = SUB src1, 1 -- get rid of one of the tag
  2845. opndReg = IR::RegOpnd::New(TyInt32, this->m_func);
  2846. instr = IR::Instr::New(Js::OpCode::SUB, opndReg, opndSrc1, IR::IntConstOpnd::New(1, TyMachReg, this->m_func), this->m_func);
  2847. instrAdd->InsertBefore(instr);
  2848. // tmp = ADDS s1, src2 -- try an inline add
  2849. IR::RegOpnd *opndTmp = IR::RegOpnd::New(TyMachReg, this->m_func);
  2850. instr = IR::Instr::New(Js::OpCode::ADDS, opndTmp, opndReg, opndSrc2, this->m_func);
  2851. instrAdd->InsertBefore(instr);
  2852. // BVS $helper -- if overflow, branch to helper.
  2853. instr = IR::BranchInstr::New(Js::OpCode::BVS, labelHelper, this->m_func);
  2854. instrAdd->InsertBefore(instr);
  2855. // dst = MOV tmp
  2856. Lowerer::InsertMove(instrAdd->GetDst(), opndTmp, instrAdd);
  2857. // B $done
  2858. labelDone = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  2859. instr = IR::BranchInstr::New(Js::OpCode::B, labelDone, this->m_func);
  2860. instrAdd->InsertBefore(instr);
  2861. // $helper:
  2862. // (caller generates helper call)
  2863. // $done:
  2864. instrAdd->InsertBefore(labelHelper);
  2865. instrAdd->InsertAfter(labelDone);
  2866. // Return true to indicate the original instr must still be lowered.
  2867. return true;
  2868. }
  2869. ///----------------------------------------------------------------------------
  2870. ///
  2871. /// LowererMD::GenerateFastSub
  2872. ///
  2873. ///
  2874. ///----------------------------------------------------------------------------
  2875. bool
  2876. LowererMD::GenerateFastSub(IR::Instr * instrSub)
  2877. {
  2878. // Given:
  2879. //
  2880. // dst = Sub src1, src2
  2881. //
  2882. // Generate:
  2883. //
  2884. // (If not 2 Int31's, jump to $helper.)
  2885. // s1 = SUBS src1, src2 -- try an inline sub
  2886. // BVS $helper -- bail if the subtract overflowed
  2887. // dst = ADD s1, 1 -- restore the var tag on the result
  2888. // B $fallthru
  2889. // $helper:
  2890. // (caller generates helper call)
  2891. // $fallthru:
  2892. IR::Instr * instr;
  2893. IR::LabelInstr * labelHelper;
  2894. IR::LabelInstr * labelFallThru;
  2895. IR::Opnd * opndReg;
  2896. IR::Opnd * opndSrc1;
  2897. IR::Opnd * opndSrc2;
  2898. opndSrc1 = instrSub->GetSrc1();
  2899. opndSrc2 = instrSub->GetSrc2();
  2900. AssertMsg(opndSrc1 && opndSrc2, "Expected 2 src opnd's on Sub instruction");
  2901. // Not tagged ints?
  2902. if (opndSrc1->IsRegOpnd() && opndSrc1->AsRegOpnd()->m_sym->m_isNotNumber ||
  2903. opndSrc2->IsRegOpnd() && opndSrc2->AsRegOpnd()->m_sym->m_isNotNumber)
  2904. {
  2905. return false;
  2906. }
  2907. // Load src's at the top so we don't have to do it repeatedly.
  2908. if (!opndSrc1->IsRegOpnd())
  2909. {
  2910. opndSrc1 = IR::RegOpnd::New(opndSrc1->GetType(), this->m_func);
  2911. Lowerer::InsertMove(opndSrc1, instrSub->GetSrc1(), instrSub);
  2912. }
  2913. if (!opndSrc2->IsRegOpnd())
  2914. {
  2915. opndSrc2 = IR::RegOpnd::New(opndSrc2->GetType(), this->m_func);
  2916. Lowerer::InsertMove(opndSrc2, instrSub->GetSrc2(), instrSub);
  2917. }
  2918. labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  2919. // Tagged ints?
  2920. bool isTaggedInts = opndSrc1->IsTaggedInt() && opndSrc2->IsTaggedInt();
  2921. if (!isTaggedInts)
  2922. {
  2923. // (If not 2 Int31's, jump to $helper.)
  2924. this->GenerateSmIntPairTest(instrSub, opndSrc1, opndSrc2, labelHelper);
  2925. }
  2926. // s1 = SUBS src1, src2 -- try an inline sub
  2927. opndReg = IR::RegOpnd::New(TyInt32, this->m_func);
  2928. instr = IR::Instr::New(Js::OpCode::SUBS, opndReg, opndSrc1, opndSrc2, this->m_func);
  2929. instrSub->InsertBefore(instr);
  2930. // BVS $helper -- bail if the subtract overflowed
  2931. instr = IR::BranchInstr::New(Js::OpCode::BVS, labelHelper, this->m_func);
  2932. instrSub->InsertBefore(instr);
  2933. // dst = ADD s1, 1 -- restore the var tag on the result
  2934. instr = IR::Instr::New(Js::OpCode::ADD, instrSub->GetDst(), opndReg, IR::IntConstOpnd::New(1, TyMachReg, this->m_func), this->m_func);
  2935. instrSub->InsertBefore(instr);
  2936. LegalizeMD::LegalizeInstr(instr);
  2937. // B $fallthru
  2938. labelFallThru = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  2939. instr = IR::BranchInstr::New(Js::OpCode::B, labelFallThru, this->m_func);
  2940. instrSub->InsertBefore(instr);
  2941. // $helper:
  2942. // (caller generates helper call)
  2943. // $fallthru:
  2944. instrSub->InsertBefore(labelHelper);
  2945. instrSub->InsertAfter(labelFallThru);
  2946. // Return true to indicate the original instr must still be lowered.
  2947. return true;
  2948. }
  2949. ///----------------------------------------------------------------------------
  2950. ///
  2951. /// LowererMD::GenerateFastMul
  2952. ///
  2953. ///----------------------------------------------------------------------------
  2954. bool
  2955. LowererMD::GenerateFastMul(IR::Instr * instrMul)
  2956. {
  2957. // Given:
  2958. //
  2959. // dst = Mul src1, src2
  2960. //
  2961. // Generate:
  2962. //
  2963. // (If not 2 Int31's, jump to $helper.)
  2964. // s1 = SUB src1, AtomTag -- clear the var tag from the value to be multiplied
  2965. // s2 = ASR src2, Js::VarTag_Shift -- extract the real src2 amount from the var
  2966. // (r12:)s1 = SMULL s1, (r12,) s1, s2 -- do the signed mul into 64bit r12:s1, the result will be src1 * src2 * 2
  2967. // (SMULL doesn't set the flags but we don't have 32bit overflow <=> r12-unsigned ? r12==0 : all 33 bits of 64bit result are 1's
  2968. // CMP r12, s1, ASR #31 -- check for overflow (== means no overflow)
  2969. // BNE $helper -- bail if the result overflowed
  2970. // TST s1, s1 -- Check 0 vs -0 (Javascript number is technically double, so need to account for -0)
  2971. // BNE $result -- TODO: consider converting 2 instructions into one: CBZ s1, $zero
  2972. // (result of mul was 0. Account for -0)
  2973. // s2 = ADDS s2, src1 -- MUL is 0 => one of (src1, src2) is 0, see if the other one is positive or negative
  2974. // BGT $result -- positive 0. keep it as int31
  2975. // dst= ToVar(-0.0) -- load negative 0
  2976. // B $fallthru
  2977. // $result:
  2978. // dst= ORR s1, AtomTag -- make sure var tag is set on the result
  2979. // B $fallthru
  2980. // $helper:
  2981. // (caller generates helper call)
  2982. // $fallthru:
  2983. IR::LabelInstr * labelHelper;
  2984. IR::LabelInstr * labelFallThru;
  2985. IR::LabelInstr * labelResult;
  2986. IR::Instr * instr;
  2987. IR::RegOpnd * opndReg1;
  2988. IR::RegOpnd * opndReg2;
  2989. IR::Opnd * opndSrc1;
  2990. IR::Opnd * opndSrc2;
  2991. opndSrc1 = instrMul->GetSrc1();
  2992. opndSrc2 = instrMul->GetSrc2();
  2993. AssertMsg(opndSrc1 && opndSrc2, "Expected 2 src opnd's on mul instruction");
  2994. // (If not 2 Int31's, jump to $helper.)
  2995. if (opndSrc1->IsRegOpnd() && opndSrc1->AsRegOpnd()->m_sym->m_isNotNumber ||
  2996. opndSrc2->IsRegOpnd() && opndSrc2->AsRegOpnd()->m_sym->m_isNotNumber)
  2997. {
  2998. return true;
  2999. }
  3000. labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  3001. labelResult = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  3002. labelFallThru = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  3003. // Load src's at the top so we don't have to do it repeatedly.
  3004. if (!opndSrc1->IsRegOpnd())
  3005. {
  3006. opndSrc1 = IR::RegOpnd::New(opndSrc1->GetType(), this->m_func);
  3007. Lowerer::InsertMove(opndSrc1, instrMul->GetSrc1(), instrMul);
  3008. }
  3009. if (!opndSrc2->IsRegOpnd())
  3010. {
  3011. opndSrc2 = IR::RegOpnd::New(opndSrc2->GetType(), this->m_func);
  3012. Lowerer::InsertMove(opndSrc2, instrMul->GetSrc2(), instrMul);
  3013. }
  3014. bool isTaggedInts = opndSrc1->IsTaggedInt() && opndSrc2->IsTaggedInt();
  3015. if (!isTaggedInts)
  3016. {
  3017. // (If not 2 Int31's, jump to $helper.)
  3018. this->GenerateSmIntPairTest(instrMul, opndSrc1->AsRegOpnd(), opndSrc2->AsRegOpnd(), labelHelper);
  3019. }
  3020. // s1 = SUB src1, AtomTag -- clear the var tag from the value to be multiplied
  3021. opndReg1 = IR::RegOpnd::New(TyInt32, this->m_func);
  3022. instr = IR::Instr::New(Js::OpCode::SUB, opndReg1, opndSrc1, IR::IntConstOpnd::New(Js::AtomTag, TyVar, this->m_func), this->m_func); // TODO: TyVar or TyMachReg?
  3023. instrMul->InsertBefore(instr);
  3024. // s2 = ASR src2, Js::VarTag_Shift -- extract the real src2 amount from the var
  3025. opndReg2 = IR::RegOpnd::New(TyInt32, this->m_func);
  3026. instr = IR::Instr::New(Js::OpCode::ASR, opndReg2, opndSrc2,
  3027. IR::IntConstOpnd::New(Js::VarTag_Shift, TyInt8, this->m_func), this->m_func);
  3028. instrMul->InsertBefore(instr);
  3029. // (r12:)s1 = SMULL s1, (r12,) s1, s2 -- do the signed mul into 64bit r12:s1, the result will be src1 * src2 * 2
  3030. instr = IR::Instr::New(Js::OpCode::SMULL, opndReg1, opndReg1, opndReg2, this->m_func);
  3031. instrMul->InsertBefore(instr);
  3032. // (SMULL doesn't set the flags but we don't have 32bit overflow <=> r12-unsigned ? r12==0 : all 33 bits of 64bit result are 1's
  3033. // CMP r12, s1, ASR #31 -- check for overflow (== means no overflow)
  3034. IR::RegOpnd* opndRegScratch = IR::RegOpnd::New(nullptr, SCRATCH_REG, TyMachReg, this->m_func);
  3035. instr = IR::Instr::New(Js::OpCode::CMP_ASR31, this->m_func);
  3036. instr->SetSrc1(opndRegScratch);
  3037. instr->SetSrc2(opndReg1);
  3038. instrMul->InsertBefore(instr);
  3039. // BNE $helper -- bail if the result overflowed
  3040. instr = IR::BranchInstr::New(Js::OpCode::BNE, labelHelper, this->m_func);
  3041. instrMul->InsertBefore(instr);
  3042. // TST s1, s1 -- Check 0 vs -0 (Javascript number is technically double, so need to account for -0)
  3043. instr = IR::Instr::New(Js::OpCode::TST, this->m_func);
  3044. instr->SetSrc1(opndReg1);
  3045. instr->SetSrc2(opndReg1);
  3046. instrMul->InsertBefore(instr);
  3047. // BNE $result -- TODO: consider converting 2 instructions into one: CBZ s1, $zero
  3048. instr = IR::BranchInstr::New(Js::OpCode::BNE, labelResult, this->m_func);
  3049. instrMul->InsertBefore(instr);
  3050. // (result of mul was 0. Account for -0)
  3051. // s2 = ADDS s2, src1 -- MUL is 0 => one of (src1, src2) is 0, see if the other one is positive or negative
  3052. instr = IR::Instr::New(Js::OpCode::ADDS, opndReg2, opndReg2, opndSrc1, this->m_func);
  3053. instrMul->InsertBefore(instr);
  3054. // BGT $result -- positive 0. keep it as int31
  3055. instr = IR::BranchInstr::New(Js::OpCode::BGT, labelResult, this->m_func);
  3056. instrMul->InsertBefore(instr);
  3057. // dst= ToVar(-0.0) -- load negative 0
  3058. instr = Lowerer::InsertMove(instrMul->GetDst(), m_lowerer->LoadLibraryValueOpnd(instrMul, LibraryValue::ValueNegativeZero), instrMul);
  3059. // No need to insert: InsertMove creates legalized instr and inserts it.
  3060. // B $fallthru
  3061. instr = IR::BranchInstr::New(Js::OpCode::B, labelFallThru, this->m_func);
  3062. instrMul->InsertBefore(instr);
  3063. // $result:
  3064. instrMul->InsertBefore(labelResult);
  3065. // dst= ORR s1, AtomTag -- make sure var tag is set on the result
  3066. instr = IR::Instr::New(Js::OpCode::ORR, instrMul->GetDst(), opndReg1, IR::IntConstOpnd::New(Js::AtomTag, TyVar, this->m_func), this->m_func);
  3067. instrMul->InsertBefore(instr);
  3068. LegalizeMD::LegalizeInstr(instr);
  3069. // B $fallthru
  3070. instr = IR::BranchInstr::New(Js::OpCode::B, labelFallThru, this->m_func);
  3071. instrMul->InsertBefore(instr);
  3072. // $helper:
  3073. // (caller generates helper call)
  3074. // $fallthru:
  3075. instrMul->InsertBefore(labelHelper);
  3076. instrMul->InsertAfter(labelFallThru);
  3077. // Return true to indicate the original instr must still be lowered.
  3078. return true;
  3079. }
  3080. ///----------------------------------------------------------------------------
  3081. ///
  3082. /// LowererMD::GenerateFastAnd
  3083. ///
  3084. ///----------------------------------------------------------------------------
  3085. bool
  3086. LowererMD::GenerateFastAnd(IR::Instr * instrAnd)
  3087. {
  3088. // Given:
  3089. //
  3090. // dst = And src1, src2
  3091. //
  3092. // Generate:
  3093. //
  3094. //
  3095. // If dst is reg:
  3096. //
  3097. // dst = AND src1, src2
  3098. // TST dst, 1
  3099. // BNE $done
  3100. // (caller generates helper sequence)
  3101. // $done:
  3102. // If dst is not reg:
  3103. //
  3104. // dstReg = AND src1, src2
  3105. // TST dstReg, 1
  3106. // BEQ $helper
  3107. // dst = STR dstReg
  3108. // B $done
  3109. // $helper
  3110. // (caller generates helper sequence)
  3111. // $done:
  3112. IR::Opnd *dst = instrAnd->GetDst();
  3113. IR::Opnd *src1 = instrAnd->GetSrc1();
  3114. IR::Opnd *src2 = instrAnd->GetSrc2();
  3115. IR::Instr *instr;
  3116. // Not tagged ints?
  3117. if (src1->IsRegOpnd() && src1->AsRegOpnd()->m_sym->m_isNotNumber)
  3118. {
  3119. return true;
  3120. }
  3121. if (src2->IsRegOpnd() && src2->AsRegOpnd()->m_sym->m_isNotNumber)
  3122. {
  3123. return true;
  3124. }
  3125. bool isInt = src1->IsTaggedInt() && src2->IsTaggedInt();
  3126. if (!isInt)
  3127. {
  3128. if (!dst->IsRegOpnd() || dst->IsEqual(src1) || dst->IsEqual(src2))
  3129. {
  3130. // Put the result in a reg and store it only when we know it's final.
  3131. dst = IR::RegOpnd::New(dst->GetType(), this->m_func);
  3132. }
  3133. }
  3134. // dstReg = AND src1, src2
  3135. instr = IR::Instr::New(Js::OpCode::AND, dst, src1, src2, this->m_func);
  3136. instrAnd->InsertBefore(instr);
  3137. LegalizeMD::LegalizeInstr(instr);
  3138. if (isInt)
  3139. {
  3140. // If both src's are ints, then we're done, and we need no helper call.
  3141. instrAnd->Remove();
  3142. return false;
  3143. }
  3144. // TST dstReg, 1
  3145. instr = IR::Instr::New(Js::OpCode::TST, this->m_func);
  3146. instr->SetSrc1(dst);
  3147. instr->SetSrc2(IR::IntConstOpnd::New(Js::AtomTag, TyMachReg, this->m_func));
  3148. instrAnd->InsertBefore(instr);
  3149. IR::LabelInstr *labelDone = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  3150. if (dst == instrAnd->GetDst())
  3151. {
  3152. // BNE $done
  3153. instr = IR::BranchInstr::New(Js::OpCode::BNE, labelDone, this->m_func);
  3154. instrAnd->InsertBefore(instr);
  3155. }
  3156. else
  3157. {
  3158. // BEQ $helper
  3159. IR::LabelInstr *labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  3160. instr = IR::BranchInstr::New(Js::OpCode::BEQ, labelHelper, this->m_func);
  3161. instrAnd->InsertBefore(instr);
  3162. // dst = STR dstReg
  3163. Lowerer::InsertMove(instrAnd->GetDst(), dst, instrAnd);
  3164. // B $done
  3165. instr = IR::BranchInstr::New(Js::OpCode::B, labelDone, this->m_func);
  3166. instrAnd->InsertBefore(instr);
  3167. // $helper
  3168. instrAnd->InsertBefore(labelHelper);
  3169. }
  3170. // (caller generates helper sequence)
  3171. // $done:
  3172. instrAnd->InsertAfter(labelDone);
  3173. // Return true to indicate the original instr must still be lowered.
  3174. return true;
  3175. }
  3176. ///----------------------------------------------------------------------------
  3177. ///
  3178. /// LowererMD::GenerateFastOr
  3179. ///
  3180. ///----------------------------------------------------------------------------
  3181. bool
  3182. LowererMD::GenerateFastOr(IR::Instr * instrOr)
  3183. {
  3184. // Given:
  3185. //
  3186. // dst = Or src1, src2
  3187. //
  3188. // Generate:
  3189. //
  3190. // (If not 2 Int31's, jump to $helper.)
  3191. //
  3192. // dst = OR src1, src2
  3193. // B $done
  3194. // $helper:
  3195. // (caller generates helper sequence)
  3196. // $fallthru:
  3197. IR::Opnd *src1 = instrOr->GetSrc1();
  3198. IR::Opnd *src2 = instrOr->GetSrc2();
  3199. IR::Opnd *dst = instrOr->GetDst();
  3200. IR::Instr *instr;
  3201. IR::LabelInstr *labelHelper = nullptr;
  3202. // Not tagged ints?
  3203. if (src1->IsRegOpnd() && src1->AsRegOpnd()->m_sym->m_isNotNumber)
  3204. {
  3205. return true;
  3206. }
  3207. if (src2->IsRegOpnd() && src2->AsRegOpnd()->m_sym->m_isNotNumber)
  3208. {
  3209. return true;
  3210. }
  3211. // Tagged ints?
  3212. bool isInt = src1->IsTaggedInt() && src2->IsTaggedInt();
  3213. // Load the src's at the top so we don't have to do it repeatedly.
  3214. if (!src1->IsRegOpnd())
  3215. {
  3216. src1 = IR::RegOpnd::New(src1->GetType(), this->m_func);
  3217. Lowerer::InsertMove(src1, instrOr->GetSrc1(), instrOr);
  3218. }
  3219. if (!src2->IsRegOpnd())
  3220. {
  3221. src2 = IR::RegOpnd::New(src2->GetType(), this->m_func);
  3222. Lowerer::InsertMove(src2, instrOr->GetSrc2(), instrOr);
  3223. }
  3224. if (!isInt)
  3225. {
  3226. // (If not 2 Int31's, jump to $helper.)
  3227. labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  3228. this->GenerateSmIntPairTest(instrOr, src1, src2, labelHelper);
  3229. }
  3230. // dst = OR src1, src2
  3231. instr = IR::Instr::New(Js::OpCode::ORR, dst, src1, src2, this->m_func);
  3232. instrOr->InsertBefore(instr);
  3233. LegalizeMD::LegalizeInstr(instr);
  3234. if (isInt)
  3235. {
  3236. // If both src's are ints, then we're done, and we don't need a helper call.
  3237. instrOr->Remove();
  3238. return false;
  3239. }
  3240. // B $done
  3241. IR::LabelInstr *labelDone = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  3242. instr = IR::BranchInstr::New(Js::OpCode::B, labelDone, this->m_func);
  3243. instrOr->InsertBefore(instr);
  3244. // $helper:
  3245. // (caller generates helper sequence)
  3246. // $done:
  3247. instrOr->InsertBefore(labelHelper);
  3248. instrOr->InsertAfter(labelDone);
  3249. // Return true to indicate the original instr must still be lowered.
  3250. return true;
  3251. }
  3252. ///----------------------------------------------------------------------------
  3253. ///
  3254. /// LowererMD::GenerateFastXor
  3255. ///
  3256. ///----------------------------------------------------------------------------
  3257. bool
  3258. LowererMD::GenerateFastXor(IR::Instr * instrXor)
  3259. {
  3260. // Given:
  3261. //
  3262. // dst = Xor src1, src2
  3263. //
  3264. // Generate:
  3265. //
  3266. // (If not 2 Int31's, jump to $helper.)
  3267. //
  3268. // s1 = MOV src1
  3269. // s1 = XOR s1, src2 -- try an inline XOR
  3270. // s1 = INC s1
  3271. // dst = MOV s1
  3272. // JMP $fallthru
  3273. // $helper:
  3274. // (caller generates helper sequence)
  3275. // $fallthru:
  3276. // Return true to indicate the original instr must still be lowered.
  3277. return true;
  3278. }
  3279. //----------------------------------------------------------------------------
  3280. //
  3281. // LowererMD::GenerateFastNot
  3282. //
  3283. //----------------------------------------------------------------------------
  3284. bool
  3285. LowererMD::GenerateFastNot(IR::Instr * instrNot)
  3286. {
  3287. // Given:
  3288. //
  3289. // dst = Not src
  3290. //
  3291. // Generate:
  3292. //
  3293. // TST src, 1
  3294. // BEQ $helper
  3295. // dst = MVN src
  3296. // dst = INC dst
  3297. // JMP $done
  3298. // $helper:
  3299. // (caller generates helper call)
  3300. // $done:
  3301. IR::LabelInstr *labelHelper = nullptr;
  3302. IR::Opnd *src = instrNot->GetSrc1();
  3303. IR::Opnd *dst = instrNot->GetDst();
  3304. IR::Instr *instr;
  3305. bool isInt = src->IsTaggedInt();
  3306. if (!src->IsRegOpnd())
  3307. {
  3308. // Load the src at the top so we don't have to load it twice.
  3309. src = IR::RegOpnd::New(src->GetType(), this->m_func);
  3310. Lowerer::InsertMove(src, instrNot->GetSrc1(), instrNot);
  3311. }
  3312. if (!dst->IsRegOpnd())
  3313. {
  3314. // We'll store the dst when we're done.
  3315. dst = IR::RegOpnd::New(dst->GetType(), this->m_func);
  3316. }
  3317. if (!isInt)
  3318. {
  3319. // TST src, 1
  3320. instr = IR::Instr::New(Js::OpCode::TST, this->m_func);
  3321. instr->SetSrc1(src);
  3322. instr->SetSrc2(IR::IntConstOpnd::New(1, TyMachReg, this->m_func));
  3323. instrNot->InsertBefore(instr);
  3324. // BEQ $helper
  3325. labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  3326. instr = IR::BranchInstr::New(Js::OpCode::BEQ, labelHelper, this->m_func);
  3327. instrNot->InsertBefore(instr);
  3328. }
  3329. // dst = MVN src
  3330. instr = IR::Instr::New(Js::OpCode::MVN, dst, src, this->m_func);
  3331. instrNot->InsertBefore(instr);
  3332. // dst = ADD dst, 1
  3333. instr = IR::Instr::New(Js::OpCode::ADD, dst, dst, IR::IntConstOpnd::New(Js::AtomTag, TyMachReg, this->m_func), this->m_func);
  3334. instrNot->InsertBefore(instr);
  3335. if (dst != instrNot->GetDst())
  3336. {
  3337. // Now store the result.
  3338. Lowerer::InsertMove(instrNot->GetDst(), dst, instrNot);
  3339. }
  3340. if (isInt)
  3341. {
  3342. // If the src is int, then we're done, and we need no helper call.
  3343. instrNot->Remove();
  3344. return false;
  3345. }
  3346. // B $done
  3347. IR::LabelInstr *labelDone = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  3348. instr = IR::BranchInstr::New(Js::OpCode::B, labelDone, this->m_func);
  3349. instrNot->InsertBefore(instr);
  3350. // $helper:
  3351. // (caller generates helper call)
  3352. // $done:
  3353. instrNot->InsertBefore(labelHelper);
  3354. instrNot->InsertAfter(labelDone);
  3355. // Return true to indicate the original instr must still be lowered.
  3356. return true;
  3357. }
  3358. //
  3359. // If value is zero in tagged int representation, jump to $labelHelper.
  3360. //
  3361. void
  3362. LowererMD::GenerateTaggedZeroTest( IR::Opnd * opndSrc, IR::Instr * insertInstr, IR::LabelInstr * labelHelper )
  3363. {
  3364. // CMP src1, AtomTag
  3365. IR::Instr* instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  3366. instr->SetSrc1(opndSrc);
  3367. instr->SetSrc2(IR::IntConstOpnd::New(Js::AtomTag, TyInt32, this->m_func));
  3368. insertInstr->InsertBefore(instr);
  3369. // BEQ $helper
  3370. if(labelHelper != nullptr)
  3371. {
  3372. // BEQ $labelHelper
  3373. instr = IR::BranchInstr::New(Js::OpCode::BEQ, labelHelper, this->m_func);
  3374. insertInstr->InsertBefore(instr);
  3375. }
  3376. }
  3377. bool
  3378. LowererMD::GenerateFastNeg(IR::Instr * instrNeg)
  3379. {
  3380. // Given:
  3381. //
  3382. // dst = Not src
  3383. //
  3384. // Generate:
  3385. //
  3386. // if not int, jump $helper
  3387. // if src == 0 -- test for zero (must be handled by the runtime to preserve
  3388. // BEQ $helper -- Difference between +0 and -0)
  3389. // dst = RSB src, 0 -- do an inline NEG
  3390. // dst = ADD dst, 2 -- restore the var tag on the result
  3391. // BVS $helper
  3392. // B $fallthru
  3393. // $helper:
  3394. // (caller generates helper call)
  3395. // $fallthru:
  3396. IR::Instr * instr;
  3397. IR::LabelInstr * labelHelper = nullptr;
  3398. IR::LabelInstr * labelFallThru = nullptr;
  3399. IR::Opnd * opndSrc1;
  3400. IR::Opnd * opndDst;
  3401. opndSrc1 = instrNeg->GetSrc1();
  3402. AssertMsg(opndSrc1, "Expected src opnd on Neg instruction");
  3403. if (opndSrc1->IsRegOpnd() && opndSrc1->AsRegOpnd()->m_sym->IsIntConst())
  3404. {
  3405. IR::Opnd *newOpnd;
  3406. IntConstType value = opndSrc1->AsRegOpnd()->m_sym->GetIntConstValue();
  3407. if (value == 0)
  3408. {
  3409. // If the negate operand is zero, the result is -0.0, which is a Number rather than an Int31.
  3410. newOpnd = m_lowerer->LoadLibraryValueOpnd(instrNeg, LibraryValue::ValueNegativeZero);
  3411. }
  3412. else
  3413. {
  3414. // negation below can overflow because max negative int32 value > max positive value by 1.
  3415. newOpnd = IR::AddrOpnd::NewFromNumber(-(int64)value, m_func);
  3416. }
  3417. instrNeg->ClearBailOutInfo();
  3418. instrNeg->FreeSrc1();
  3419. instrNeg->SetSrc1(newOpnd);
  3420. instrNeg = this->ChangeToAssign(instrNeg);
  3421. // Skip lowering call to helper
  3422. return false;
  3423. }
  3424. bool isInt = (opndSrc1->IsTaggedInt());
  3425. if (opndSrc1->IsRegOpnd() && opndSrc1->AsRegOpnd()->m_sym->m_isNotNumber)
  3426. {
  3427. return true;
  3428. }
  3429. labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  3430. // Load src's at the top so we don't have to do it repeatedly.
  3431. if (!opndSrc1->IsRegOpnd())
  3432. {
  3433. opndSrc1 = IR::RegOpnd::New(opndSrc1->GetType(), this->m_func);
  3434. Lowerer::InsertMove(opndSrc1, instrNeg->GetSrc1(), instrNeg);
  3435. }
  3436. if (!isInt)
  3437. {
  3438. GenerateSmIntTest(opndSrc1, instrNeg, labelHelper);
  3439. }
  3440. GenerateTaggedZeroTest(opndSrc1, instrNeg, labelHelper);
  3441. opndDst = instrNeg->GetDst();
  3442. if (!opndDst->IsRegOpnd())
  3443. {
  3444. opndDst = IR::RegOpnd::New(opndDst->GetType(), this->m_func);
  3445. }
  3446. // dst = RSB src
  3447. instr = IR::Instr::New(Js::OpCode::RSB, opndDst, opndSrc1, IR::IntConstOpnd::New(0, TyInt32, this->m_func), this->m_func);
  3448. instrNeg->InsertBefore(instr);
  3449. // dst = ADD dst, 2
  3450. instr = IR::Instr::New(Js::OpCode::ADDS, opndDst, opndDst, IR::IntConstOpnd::New(2, TyInt32, this->m_func), this->m_func);
  3451. instrNeg->InsertBefore(instr);
  3452. // BVS $helper
  3453. instr = IR::BranchInstr::New(Js::OpCode::BVS, labelHelper, this->m_func);
  3454. instrNeg->InsertBefore(instr);
  3455. if (opndDst != instrNeg->GetDst())
  3456. {
  3457. //Now store the result.
  3458. Lowerer::InsertMove(instrNeg->GetDst(), opndDst, instrNeg);
  3459. }
  3460. // B $fallthru
  3461. labelFallThru = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  3462. instr = IR::BranchInstr::New(Js::OpCode::B, labelFallThru, this->m_func);
  3463. instrNeg->InsertBefore(instr);
  3464. // $helper:
  3465. // (caller generates helper sequence)
  3466. // $fallthru:
  3467. AssertMsg(labelHelper, "Should not be NULL");
  3468. instrNeg->InsertBefore(labelHelper);
  3469. instrNeg->InsertAfter(labelFallThru);
  3470. return true;
  3471. }
  3472. ///----------------------------------------------------------------------------
  3473. ///
  3474. /// LowererMD::GenerateFastShiftLeft
  3475. ///
  3476. ///----------------------------------------------------------------------------
  3477. bool
  3478. LowererMD::GenerateFastShiftLeft(IR::Instr * instrShift)
  3479. {
  3480. // Given:
  3481. //
  3482. // dst = Shl src1, src2
  3483. //
  3484. // Generate:
  3485. //
  3486. // (If not 2 Int31's, jump to $helper.)
  3487. // s1 = MOV src1
  3488. // s1 = SAR s1, Js::VarTag_Shift -- Remove the var tag from the value to be shifted
  3489. // s2 = MOV src2
  3490. // s2 = SAR s2, Js::VarTag_Shift -- extract the real shift amount from the var
  3491. // s1 = SHL s1, s2 -- do the inline shift
  3492. // s3 = MOV s1
  3493. // s3 = SHL s3, Js::VarTag_Shift -- restore the var tag on the result
  3494. // JO $ToVar
  3495. // s3 = INC s3
  3496. // dst = MOV s3
  3497. // JMP $fallthru
  3498. //$ToVar:
  3499. // PUSH scriptContext
  3500. // PUSH s1
  3501. // dst = ToVar()
  3502. // JMP $fallthru
  3503. // $helper:
  3504. // (caller generates helper call)
  3505. // $fallthru:
  3506. // Return true to indicate the original instr must still be lowered.
  3507. return true;
  3508. }
  3509. ///----------------------------------------------------------------------------
  3510. ///
  3511. /// LowererMD::GenerateFastShiftRight
  3512. ///
  3513. ///----------------------------------------------------------------------------
  3514. bool
  3515. LowererMD::GenerateFastShiftRight(IR::Instr * instrShift)
  3516. {
  3517. // Given:
  3518. //
  3519. // dst = Shr/Sar src1, src2
  3520. //
  3521. // Generate:
  3522. //
  3523. // s1 = MOV src1
  3524. // TEST s1, 1
  3525. // JEQ $S1ToInt
  3526. // s1 = SAR s1, VarTag_Shift -- extract the real shift amount from the var
  3527. // JMP $src2
  3528. //$S1ToInt:
  3529. // PUSH scriptContext
  3530. // PUSH s1
  3531. // s1 = ToInt32()/ToUInt32
  3532. //$src2:
  3533. // Load s2 in ECX
  3534. // TEST s2, 1
  3535. // JEQ $S2ToUInt
  3536. // s2 = SAR s2, VarTag_Shift -- extract the real shift amount from the var
  3537. // JMP $Shr
  3538. //$S2ToUInt:
  3539. // PUSH scriptContext
  3540. // PUSH s2
  3541. // s2 = ToUInt32()
  3542. //$Shr:
  3543. // s1 = SHR/SAR s1, s2 -- do the inline shift
  3544. // s3 = MOV s1
  3545. // s3 = SHL s3, s2 -- To tagInt
  3546. // JO $ToVar
  3547. // JS $ToVar
  3548. // s3 = INC s3
  3549. // JMP $done
  3550. //$ToVar:
  3551. // PUSH scriptContext
  3552. // PUSH s1
  3553. // s3 = ToVar()
  3554. //$Done:
  3555. // dst = MOV s3
  3556. // Return true to indicate the original instr must still be lowered.
  3557. return true;
  3558. }
  3559. void
  3560. LowererMD::GenerateFastBrS(IR::BranchInstr *brInstr)
  3561. {
  3562. IR::Opnd *src1 = brInstr->UnlinkSrc1();
  3563. Assert(src1->IsIntConstOpnd() || src1->IsAddrOpnd() || src1->IsRegOpnd());
  3564. m_lowerer->InsertTest(
  3565. m_lowerer->LoadOptimizationOverridesValueOpnd(
  3566. brInstr, OptimizationOverridesValue::OptimizationOverridesSideEffects),
  3567. src1,
  3568. brInstr);
  3569. Js::OpCode opcode;
  3570. switch(brInstr->m_opcode)
  3571. {
  3572. case Js::OpCode::BrHasSideEffects:
  3573. opcode = Js::OpCode::BNE;
  3574. break;
  3575. case Js::OpCode::BrNotHasSideEffects:
  3576. opcode = Js::OpCode::BEQ;
  3577. break;
  3578. default:
  3579. Assert(UNREACHED);
  3580. __assume(false);
  3581. }
  3582. brInstr->m_opcode = opcode;
  3583. }
  3584. ///----------------------------------------------------------------------------
  3585. ///
  3586. /// LowererMD::GenerateSmIntPairTest
  3587. ///
  3588. /// Generate code to test whether the given operands are both Int31 vars
  3589. /// and branch to the given label if not.
  3590. ///
  3591. ///----------------------------------------------------------------------------
  3592. IR::Instr *
  3593. LowererMD::GenerateSmIntPairTest(
  3594. IR::Instr * instrInsert,
  3595. IR::Opnd * src1,
  3596. IR::Opnd * src2,
  3597. IR::LabelInstr * labelFail)
  3598. {
  3599. IR::Opnd * opndReg;
  3600. IR::Instr * instrPrev = instrInsert->m_prev;
  3601. IR::Instr * instr;
  3602. Assert(src1->GetType() == TyVar);
  3603. Assert(src2->GetType() == TyVar);
  3604. //src1 and src2 can either be RegOpnd or AddrOpnd at this point
  3605. if (src1->IsTaggedInt())
  3606. {
  3607. Swap(src1, src2);
  3608. }
  3609. if (src2->IsTaggedInt())
  3610. {
  3611. if (src1->IsTaggedInt())
  3612. {
  3613. return instrPrev;
  3614. }
  3615. IR::RegOpnd *opndSrc1 = src1->AsRegOpnd();
  3616. // TST src1, AtomTag
  3617. // BEQ $fail
  3618. instr = IR::Instr::New(Js::OpCode::TST, this->m_func);
  3619. instr->SetSrc1(opndSrc1);
  3620. instr->SetSrc2(IR::IntConstOpnd::New(Js::AtomTag, TyVar, this->m_func));
  3621. instrInsert->InsertBefore(instr);
  3622. }
  3623. else
  3624. {
  3625. IR::RegOpnd *opndSrc1 = src1->AsRegOpnd();
  3626. IR::RegOpnd *opndSrc2 = src2->AsRegOpnd();
  3627. // s1 = AND src1, 1
  3628. // TST s1, src2
  3629. // BEQ $fail
  3630. // s1 = AND src1, AtomTag
  3631. opndReg = IR::RegOpnd::New(TyMachReg, this->m_func);
  3632. instr = IR::Instr::New(
  3633. Js::OpCode::AND, opndReg, opndSrc1, IR::IntConstOpnd::New(Js::AtomTag, TyMachReg, this->m_func), this->m_func);
  3634. instrInsert->InsertBefore(instr);
  3635. // TST s1, src2
  3636. instr = IR::Instr::New(Js::OpCode::TST, this->m_func);
  3637. instr->SetSrc1(opndReg);
  3638. instr->SetSrc2(opndSrc2);
  3639. instrInsert->InsertBefore(instr);
  3640. }
  3641. // BEQ $fail
  3642. instr = IR::BranchInstr::New(Js::OpCode::BEQ, labelFail, this->m_func);
  3643. instrInsert->InsertBefore(instr);
  3644. return instrPrev;
  3645. }
  3646. void LowererMD::GenerateObjectPairTest(IR::Opnd * opndSrc1, IR::Opnd * opndSrc2, IR::Instr * insertInstr, IR::LabelInstr * labelTarget)
  3647. {
  3648. // opndOr = ORR opndSrc1, opndSrc2
  3649. // TST opndOr, AtomTag_Ptr
  3650. // BNE $labelTarget
  3651. IR::RegOpnd * opndOr = IR::RegOpnd::New(TyMachPtr, this->m_func);
  3652. IR::Instr * instr = IR::Instr::New(Js::OpCode::ORR, opndOr, opndSrc1, opndSrc2, this->m_func);
  3653. insertInstr->InsertBefore(instr);
  3654. instr = IR::Instr::New(Js::OpCode::TST, this->m_func);
  3655. instr->SetSrc1(opndOr);
  3656. instr->SetSrc2(IR::IntConstOpnd::New(Js::AtomTag_IntPtr, TyMachReg, this->m_func));
  3657. insertInstr->InsertBefore(instr);
  3658. instr = IR::BranchInstr::New(Js::OpCode::BNE, labelTarget, this->m_func);
  3659. insertInstr->InsertBefore(instr);
  3660. }
  3661. bool LowererMD::GenerateObjectTest(IR::Opnd * opndSrc, IR::Instr * insertInstr, IR::LabelInstr * labelTarget, bool fContinueLabel)
  3662. {
  3663. if (opndSrc->IsTaggedValue() && fContinueLabel)
  3664. {
  3665. // Insert delete branch opcode to tell the dbChecks not to assert on the helper label we may fall through into
  3666. IR::Instr *fakeBr = IR::PragmaInstr::New(Js::OpCode::DeletedNonHelperBranch, 0, this->m_func);
  3667. insertInstr->InsertBefore(fakeBr);
  3668. return false;
  3669. }
  3670. else if (opndSrc->IsNotTaggedValue() && !fContinueLabel)
  3671. {
  3672. return false;
  3673. }
  3674. // TST s1, AtomTag_IntPtr | FloatTag_Value
  3675. IR::Instr * instr = IR::Instr::New(Js::OpCode::TST, this->m_func);
  3676. instr->SetSrc1(opndSrc);
  3677. instr->SetSrc2(IR::IntConstOpnd::New(Js::AtomTag_IntPtr, TyMachReg, this->m_func));
  3678. insertInstr->InsertBefore(instr);
  3679. if (fContinueLabel)
  3680. {
  3681. // BEQ $labelTarget
  3682. instr = IR::BranchInstr::New(Js::OpCode::BEQ, labelTarget, this->m_func);
  3683. insertInstr->InsertBefore(instr);
  3684. IR::LabelInstr *labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  3685. insertInstr->InsertBefore(labelHelper);
  3686. }
  3687. else
  3688. {
  3689. // BNE $labelTarget
  3690. instr = IR::BranchInstr::New(Js::OpCode::BNE, labelTarget, this->m_func);
  3691. insertInstr->InsertBefore(instr);
  3692. }
  3693. return true;
  3694. }
  3695. void
  3696. LowererMD::GenerateLoadTaggedType(IR::Instr * instrLdSt, IR::RegOpnd * opndType, IR::RegOpnd * opndTaggedType)
  3697. {
  3698. // taggedType = OR type, InlineCacheAuxSlotTypeTag
  3699. IR::IntConstOpnd * opndAuxSlotTag = IR::IntConstOpnd::New(InlineCacheAuxSlotTypeTag, TyInt8, instrLdSt->m_func);
  3700. IR::Instr * instr = IR::Instr::New(Js::OpCode::ORR, opndTaggedType, opndType, opndAuxSlotTag, instrLdSt->m_func);
  3701. instrLdSt->InsertBefore(instr);
  3702. }
  3703. void
  3704. LowererMD::GenerateLoadPolymorphicInlineCacheSlot(IR::Instr * instrLdSt, IR::RegOpnd * opndInlineCache, IR::RegOpnd * opndType, uint polymorphicInlineCacheSize)
  3705. {
  3706. // Generate
  3707. //
  3708. // LDR r1, type
  3709. // LSR r1, r1, #PolymorphicInlineCacheShift
  3710. // AND r1, r1, #(size - 1)
  3711. // LSL r1, r1, #log2(sizeof(Js::InlineCache))
  3712. // ADD inlineCache, inlineCache, r1
  3713. // MOV r1, type
  3714. IR::RegOpnd * opndOffset = IR::RegOpnd::New(TyMachPtr, instrLdSt->m_func);
  3715. IR::Instr * instr = IR::Instr::New(Js::OpCode::MOV, opndOffset, opndType, instrLdSt->m_func);
  3716. instrLdSt->InsertBefore(instr);
  3717. IntConstType rightShiftAmount = PolymorphicInlineCacheShift;
  3718. IntConstType leftShiftAmount = Math::Log2(sizeof(Js::InlineCache));
  3719. // instead of generating
  3720. // LSR r1, r1, #PolymorphicInlineCacheShift
  3721. // AND r1, r1, #(size - 1)
  3722. // LSL r1, r1, #log2(sizeof(Js::InlineCache))
  3723. //
  3724. // we can generate:
  3725. // LSR r1, r1, #(PolymorphicInlineCacheShift - log2(sizeof(Js::InlineCache))
  3726. // AND r1, r1, #(size - 1) << log2(sizeof(Js::InlineCache))
  3727. Assert(rightShiftAmount > leftShiftAmount);
  3728. instr = IR::Instr::New(Js::OpCode::LSR, opndOffset, opndOffset, IR::IntConstOpnd::New(rightShiftAmount - leftShiftAmount, TyUint8, instrLdSt->m_func, true), instrLdSt->m_func);
  3729. instrLdSt->InsertBefore(instr);
  3730. instr = IR::Instr::New(Js::OpCode::AND, opndOffset, opndOffset, IR::IntConstOpnd::New((polymorphicInlineCacheSize - 1) << leftShiftAmount, TyMachPtr, instrLdSt->m_func, true), instrLdSt->m_func);
  3731. instrLdSt->InsertBefore(instr);
  3732. // ADD inlineCache, inlineCache, r1
  3733. instr = IR::Instr::New(Js::OpCode::ADD, opndInlineCache, opndInlineCache, opndOffset, instrLdSt->m_func);
  3734. instrLdSt->InsertBefore(instr);
  3735. }
  3736. //----------------------------------------------------------------------------
  3737. //
  3738. // LowererMD::GenerateFastScopedFldLookup
  3739. //
  3740. // This is a helper call which generates asm for both
  3741. // ScopedLdFld & ScopedStFld
  3742. //
  3743. //----------------------------------------------------------------------------
  3744. IR::Instr *
  3745. LowererMD::GenerateFastScopedFld(IR::Instr * instrScopedFld, bool isLoad)
  3746. {
  3747. // LDR s1, [base, offset(length)]
  3748. // CMP s1, 1 -- get the length on array and test if it is 1.
  3749. // BNE $helper
  3750. // LDR s2, [base, offset(scopes)] -- load the first scope
  3751. // LDR s3, [s2, offset(type)]
  3752. // LDIMM s4, inlineCache
  3753. // LDR s5, [s4, offset(u.local.type)]
  3754. // CMP s3, s5 -- check type
  3755. // BNE $helper
  3756. // LDR s6, [s2, offset(slots)] -- load the slots array
  3757. // LDR s7 , [s4, offset(u.local.slotIndex)] -- load the cached slot index
  3758. //
  3759. // if (load) {
  3760. // LDR dst, [s6, s7, LSL #2] -- load the value from the slot
  3761. // }
  3762. // else {
  3763. // STR src, [s6, s7, LSL #2]
  3764. // }
  3765. // B $done
  3766. //$helper:
  3767. // dst = BLX PatchGetPropertyScoped(inlineCache, base, field, defaultInstance, scriptContext)
  3768. //$done:
  3769. IR::Instr * instr;
  3770. IR::Instr * instrPrev = instrScopedFld->m_prev;
  3771. IR::RegOpnd * opndBase;
  3772. IR::RegOpnd * opndReg1; //s1
  3773. IR::RegOpnd * opndReg2; //s2
  3774. IR::RegOpnd * opndInlineCache; //s4
  3775. IR::IndirOpnd * indirOpnd;
  3776. IR::Opnd * propertyBase;
  3777. IR::LabelInstr * labelHelper;
  3778. IR::LabelInstr * labelFallThru;
  3779. if (isLoad)
  3780. {
  3781. propertyBase = instrScopedFld->GetSrc1();
  3782. }
  3783. else
  3784. {
  3785. propertyBase = instrScopedFld->GetDst();
  3786. }
  3787. AssertMsg(propertyBase->IsSymOpnd() && propertyBase->AsSymOpnd()->IsPropertySymOpnd() && propertyBase->AsSymOpnd()->m_sym->IsPropertySym(),
  3788. "Expected property sym operand of ScopedLdFld or ScopedStFld");
  3789. IR::PropertySymOpnd * propertySymOpnd = propertyBase->AsPropertySymOpnd();
  3790. opndBase = propertySymOpnd->CreatePropertyOwnerOpnd(m_func);
  3791. AssertMsg(opndBase->m_sym->m_isSingleDef, "We assume this isn't redefined");
  3792. labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  3793. // LDR s1, [base, offset(length)] -- get the length on array and test if it is 1.
  3794. indirOpnd = IR::IndirOpnd::New(opndBase, Js::FrameDisplay::GetOffsetOfLength(), TyInt16, this->m_func);
  3795. opndReg1 = IR::RegOpnd::New(TyInt32, this->m_func);
  3796. instr = IR::Instr::New(Js::OpCode::LDR, opndReg1, indirOpnd, this->m_func);
  3797. instrScopedFld->InsertBefore(instr);
  3798. // CMP s1, 1 -- get the length on array and test if it is 1.
  3799. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  3800. instr->SetSrc1(opndReg1);
  3801. instr->SetSrc2(IR::IntConstOpnd::New(0x1, TyInt8, this->m_func));
  3802. instrScopedFld->InsertBefore(instr);
  3803. // BNE $helper
  3804. instr = IR::BranchInstr::New(Js::OpCode::BNE, labelHelper, this->m_func);
  3805. instrScopedFld->InsertBefore(instr);
  3806. // LDR s2, [base, offset(scopes)] -- load the first scope
  3807. indirOpnd = IR::IndirOpnd::New(opndBase, Js::FrameDisplay::GetOffsetOfScopes(), TyInt32,this->m_func);
  3808. opndReg2 = IR::RegOpnd::New(TyInt32, this->m_func);
  3809. instr = IR::Instr::New(Js::OpCode::LDR, opndReg2, indirOpnd, this->m_func);
  3810. instrScopedFld->InsertBefore(instr);
  3811. // LDR s3, [s2, offset(type)]
  3812. // LDIMM s4, inlineCache
  3813. // LDR s5, [s4, offset(u.local.type)]
  3814. // CMP s3, s5 -- check type
  3815. // BNE $helper
  3816. opndInlineCache = IR::RegOpnd::New(TyInt32, this->m_func);
  3817. opndReg2->m_sym->m_isNotNumber = true;
  3818. IR::RegOpnd * opndType = IR::RegOpnd::New(TyMachReg, this->m_func);
  3819. this->m_lowerer->GenerateObjectTestAndTypeLoad(instrScopedFld, opndReg2, opndType, labelHelper);
  3820. Lowerer::InsertMove(opndInlineCache, m_lowerer->LoadRuntimeInlineCacheOpnd(instrScopedFld, propertySymOpnd), instrScopedFld);
  3821. labelFallThru = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  3822. // Check the local cache with the tagged type
  3823. IR::RegOpnd * opndTaggedType = IR::RegOpnd::New(TyMachReg, this->m_func);
  3824. GenerateLoadTaggedType(instrScopedFld, opndType, opndTaggedType);
  3825. Lowerer::GenerateLocalInlineCacheCheck(instrScopedFld, opndTaggedType, opndInlineCache, labelHelper);
  3826. if (isLoad)
  3827. {
  3828. IR::Opnd *opndDst = instrScopedFld->GetDst();
  3829. Lowerer::GenerateLdFldFromLocalInlineCache(instrScopedFld, opndReg2, opndDst, opndInlineCache, labelFallThru, false);
  3830. }
  3831. else
  3832. {
  3833. IR::Opnd *opndSrc = instrScopedFld->GetSrc1();
  3834. GenerateStFldFromLocalInlineCache(instrScopedFld, opndReg2, opndSrc, opndInlineCache, labelFallThru, false);
  3835. }
  3836. // $helper:
  3837. // if (isLoad) {
  3838. // dst = BLX PatchGetPropertyScoped(inlineCache, opndBase, propertyId, srcBase, scriptContext)
  3839. // }
  3840. // else {
  3841. // BLX PatchSetPropertyScoped(inlineCache, base, field, value, defaultInstance, scriptContext)
  3842. // }
  3843. // $fallthru:
  3844. instrScopedFld->InsertBefore(labelHelper);
  3845. instrScopedFld->InsertAfter(labelFallThru);
  3846. return instrPrev;
  3847. }
  3848. //----------------------------------------------------------------------------
  3849. //
  3850. // LowererMD::GenerateFastScopedLdFld
  3851. //
  3852. // Make use of the helper to cache the type and slot index used to do a ScopedLdFld
  3853. // when the scope is an array of length 1.
  3854. // Extract the only element from array and do an inline load from the appropriate slot
  3855. // if the type hasn't changed since the last time this ScopedLdFld was executed.
  3856. //
  3857. //----------------------------------------------------------------------------
  3858. IR::Instr *
  3859. LowererMD::GenerateFastScopedLdFld(IR::Instr * instrLdScopedFld)
  3860. {
  3861. //Helper GenerateFastScopedFldLookup generates following:
  3862. //
  3863. // LDR s1, [base, offset(length)]
  3864. // CMP s1, 1 -- get the length on array and test if it is 1.
  3865. // BNE $helper
  3866. // LDR s2, [base, offset(scopes)] -- load the first scope
  3867. // LDR s3, [s2, offset(type)]
  3868. // LDIMM s4, inlineCache
  3869. // LDR s5, [s4, offset(u.local.type)]
  3870. // CMP s3, s5 -- check type
  3871. // BNE $helper
  3872. // LDR s6, [s2, offset(slots)] -- load the slots array
  3873. // LDR s7 , [s4, offset(u.local.slotIndex)] -- load the cached slot index
  3874. // LDR dst, [s6, s7, LSL #2] -- load the value from the slot
  3875. // B $done
  3876. //$helper:
  3877. // dst = BLX PatchGetPropertyScoped(inlineCache, base, field, defaultInstance, scriptContext)
  3878. //$done:
  3879. return GenerateFastScopedFld(instrLdScopedFld, true);
  3880. }
  3881. //----------------------------------------------------------------------------
  3882. //
  3883. // LowererMD::GenerateFastScopedStFld
  3884. //
  3885. // Make use of the helper to cache the type and slot index used to do a ScopedStFld
  3886. // when the scope is an array of length 1.
  3887. // Extract the only element from array and do an inline load from the appropriate slot
  3888. // if the type hasn't changed since the last time this ScopedStFld was executed.
  3889. //
  3890. //----------------------------------------------------------------------------
  3891. IR::Instr *
  3892. LowererMD::GenerateFastScopedStFld(IR::Instr * instrStScopedFld)
  3893. {
  3894. // LDR s1, [base, offset(length)]
  3895. // CMP s1, 1 -- get the length on array and test if it is 1.
  3896. // BNE $helper
  3897. // LDR s2, [base, offset(scopes)] -- load the first scope
  3898. // LDR s3, [s2, offset(type)]
  3899. // LDIMM s4, inlineCache
  3900. // LDR s5, [s4, offset(u.local.type)]
  3901. // CMP s3, s5 -- check type
  3902. // BNE $helper
  3903. // LDR s6, [s2, offset(slots)] -- load the slots array
  3904. // LDR s7 , [s4, offset(u.local.slotIndex)] -- load the cached slot index
  3905. // STR src, [s6, s7, LSL #2] -- store the value directly at the slot
  3906. // B $done
  3907. //$helper:
  3908. // BLX PatchSetPropertyScoped(inlineCache, base, field, value, defaultInstance, scriptContext)
  3909. //$done:
  3910. return GenerateFastScopedFld(instrStScopedFld, false);
  3911. }
  3912. void
  3913. LowererMD::GenerateStFldFromLocalInlineCache(
  3914. IR::Instr * instrStFld,
  3915. IR::RegOpnd * opndBase,
  3916. IR::Opnd * opndSrc,
  3917. IR::RegOpnd * opndInlineCache,
  3918. IR::LabelInstr * labelFallThru,
  3919. bool isInlineSlot)
  3920. {
  3921. IR::RegOpnd * opndSlotArray = nullptr;
  3922. IR::IndirOpnd * opndIndir;
  3923. IR::Instr * instr;
  3924. if (!isInlineSlot)
  3925. {
  3926. // s2 = MOV base->slots -- load the slot array
  3927. opndSlotArray = IR::RegOpnd::New(TyMachReg, instrStFld->m_func);
  3928. opndIndir = IR::IndirOpnd::New(opndBase, Js::DynamicObject::GetOffsetOfAuxSlots(), TyMachReg, instrStFld->m_func);
  3929. Lowerer::InsertMove(opndSlotArray, opndIndir, instrStFld);
  3930. }
  3931. // LDR s5, [s2, offset(u.local.slotIndex)] -- load the cached slot index
  3932. IR::RegOpnd *opndSlotIndex = IR::RegOpnd::New(TyUint16, instrStFld->m_func);
  3933. opndIndir = IR::IndirOpnd::New(opndInlineCache, offsetof(Js::InlineCache, u.local.slotIndex), TyUint16, instrStFld->m_func);
  3934. instr = IR::Instr::New(Js::OpCode::LDR, opndSlotIndex, opndIndir, instrStFld->m_func);
  3935. instrStFld->InsertBefore(instr);
  3936. if (isInlineSlot)
  3937. {
  3938. // STR src, [base, s5, LSL #2] -- store the value directly to the slot [s4 + s5 * 4] = src
  3939. opndIndir = IR::IndirOpnd::New(opndBase, opndSlotIndex, LowererMD::GetDefaultIndirScale(), TyMachReg, instrStFld->m_func);
  3940. instr = IR::Instr::New(Js::OpCode::STR, opndIndir, opndSrc, instrStFld->m_func);
  3941. instrStFld->InsertBefore(instr);
  3942. LegalizeMD::LegalizeInstr(instr);
  3943. }
  3944. else
  3945. {
  3946. // STR src, [s4, s5, LSL #2] -- store the value directly to the slot [s4 + s5 * 4] = src
  3947. opndIndir = IR::IndirOpnd::New(opndSlotArray, opndSlotIndex, LowererMD::GetDefaultIndirScale(), TyMachReg, instrStFld->m_func);
  3948. instr = IR::Instr::New(Js::OpCode::STR, opndIndir, opndSrc, instrStFld->m_func);
  3949. instrStFld->InsertBefore(instr);
  3950. LegalizeMD::LegalizeInstr(instr);
  3951. }
  3952. // B $done
  3953. instr = IR::BranchInstr::New(Js::OpCode::B, labelFallThru, instrStFld->m_func);
  3954. instrStFld->InsertBefore(instr);
  3955. }
  3956. IR::Opnd *
  3957. LowererMD::CreateStackArgumentsSlotOpnd(Func *func)
  3958. {
  3959. // Save the newly-created args object to its dedicated stack slot.
  3960. IR::IndirOpnd *indirOpnd = IR::IndirOpnd::New(IR::RegOpnd::New(nullptr, FRAME_REG , TyMachReg, func),
  3961. -MachArgsSlotOffset, TyMachPtr, func);
  3962. return indirOpnd;
  3963. }
  3964. //
  3965. // jump to $labelHelper, based on the result of TST
  3966. //
  3967. void LowererMD::GenerateSmIntTest(IR::Opnd *opndSrc, IR::Instr *insertInstr, IR::LabelInstr *labelHelper, IR::Instr **instrFirst, bool fContinueLabel /* = false */)
  3968. {
  3969. // TEST src1, AtomTag
  3970. IR::Instr* instr = IR::Instr::New(Js::OpCode::TST, this->m_func);
  3971. instr->SetSrc1(opndSrc);
  3972. instr->SetSrc2(IR::IntConstOpnd::New(Js::AtomTag, TyInt32, this->m_func));
  3973. insertInstr->InsertBefore(instr);
  3974. if(fContinueLabel)
  3975. {
  3976. // BNE $labelHelper
  3977. instr = IR::BranchInstr::New(Js::OpCode::BNE, labelHelper, this->m_func);
  3978. insertInstr->InsertBefore(instr);
  3979. }
  3980. else
  3981. {
  3982. // BEQ $labelHelper
  3983. instr = IR::BranchInstr::New(Js::OpCode::BEQ, labelHelper, this->m_func);
  3984. insertInstr->InsertBefore(instr);
  3985. }
  3986. }
  3987. void LowererMD::GenerateInt32ToVarConversion(IR::Opnd * opndSrc, IR::Instr * insertInstr )
  3988. {
  3989. AssertMsg(opndSrc->IsRegOpnd(), "NYI for other types");
  3990. // Shift left & tag.
  3991. // For now this is used only for actual arguments count can only be 24 bits long and non need to check for overflow
  3992. IR:: Instr* instr = IR::Instr::New(Js::OpCode::LSL, opndSrc, opndSrc, IR::IntConstOpnd::New(Js::VarTag_Shift, TyInt8, this->m_func), this->m_func);
  3993. insertInstr->InsertBefore(instr);
  3994. instr = IR::Instr::New(Js::OpCode::ADD, opndSrc, opndSrc,
  3995. IR::IntConstOpnd::New(Js::VarTag_Shift, TyMachReg, this->m_func),
  3996. this->m_func);
  3997. insertInstr->InsertBefore(instr);
  3998. }
  3999. IR::RegOpnd *
  4000. LowererMD::GenerateUntagVar(IR::RegOpnd * opnd, IR::LabelInstr * instrFail, IR::Instr * insertBeforeInstr, bool generateTagCheck)
  4001. {
  4002. // Generates:
  4003. // int32Opnd = ASRS opnd, Js::VarTag_Shift -- shift-out tag from opnd
  4004. // BCC $helper -- if not tagged int, jmp to $helper
  4005. // Returns: index32Opnd
  4006. Assert(opnd->IsVar());
  4007. IR::RegOpnd * int32Opnd = IR::RegOpnd::New(TyInt32, this->m_func);
  4008. // int32Opnd = ASRS opnd, Js::VarTag_Shift -- shift-out tag from indexOpnd
  4009. IR::Instr *instr = IR::Instr::New(Js::OpCode::ASRS, int32Opnd, opnd,
  4010. IR::IntConstOpnd::New(Js::VarTag_Shift, TyInt8, this->m_func), this->m_func);
  4011. insertBeforeInstr->InsertBefore(instr);
  4012. LegalizeMD::LegalizeInstr(instr);
  4013. // No need to check if we already know that it is a tagged int.
  4014. if (generateTagCheck)
  4015. {
  4016. Assert(!opnd->IsTaggedInt());
  4017. // BCC $helper -- if not tagged int, jmp to $helper
  4018. instr = IR::BranchInstr::New(Js::OpCode::BCC, instrFail, this->m_func);
  4019. insertBeforeInstr->InsertBefore(instr);
  4020. }
  4021. return int32Opnd;
  4022. }
  4023. IR::RegOpnd *LowererMD::LoadNonnegativeIndex(
  4024. IR::RegOpnd *indexOpnd,
  4025. const bool skipNegativeCheck,
  4026. IR::LabelInstr *const notTaggedIntLabel,
  4027. IR::LabelInstr *const negativeLabel,
  4028. IR::Instr *const insertBeforeInstr)
  4029. {
  4030. Assert(indexOpnd);
  4031. Assert(indexOpnd->IsVar() || indexOpnd->GetType() == TyInt32 || indexOpnd->GetType() == TyUint32);
  4032. Assert(indexOpnd->GetType() != TyUint32 || skipNegativeCheck);
  4033. Assert(!indexOpnd->IsVar() || notTaggedIntLabel);
  4034. Assert(skipNegativeCheck || negativeLabel);
  4035. Assert(insertBeforeInstr);
  4036. Func *const func = insertBeforeInstr->m_func;
  4037. IR::AutoReuseOpnd autoReuseIndexOpnd;
  4038. if(indexOpnd->IsVar())
  4039. {
  4040. if (indexOpnd->GetValueType().IsLikelyFloat())
  4041. {
  4042. return m_lowerer->LoadIndexFromLikelyFloat(indexOpnd, skipNegativeCheck, notTaggedIntLabel, negativeLabel, insertBeforeInstr);
  4043. }
  4044. // asrs intIndex, index, 1
  4045. // bcc $notTaggedIntOrNegative
  4046. IR::RegOpnd *const intIndexOpnd = IR::RegOpnd::New(TyInt32, func);
  4047. if(skipNegativeCheck)
  4048. {
  4049. intIndexOpnd->SetType(TyUint32);
  4050. }
  4051. autoReuseIndexOpnd.Initialize(intIndexOpnd, func, false);
  4052. const bool isTaggedInt = indexOpnd->IsTaggedInt();
  4053. Lowerer::InsertShift(
  4054. Js::OpCode::Shr_A,
  4055. !(isTaggedInt && skipNegativeCheck),
  4056. intIndexOpnd,
  4057. indexOpnd,
  4058. IR::IntConstOpnd::New(Js::VarTag_Shift, TyInt8, func),
  4059. insertBeforeInstr);
  4060. if(!isTaggedInt)
  4061. {
  4062. Lowerer::InsertBranch(Js::OpCode::BCC, notTaggedIntLabel, insertBeforeInstr);
  4063. }
  4064. indexOpnd = intIndexOpnd;
  4065. }
  4066. else if(!skipNegativeCheck)
  4067. {
  4068. // tst index, index
  4069. Lowerer::InsertTest(indexOpnd, indexOpnd, insertBeforeInstr);
  4070. }
  4071. if(!skipNegativeCheck)
  4072. {
  4073. // bmi $notTaggedIntOrNegative
  4074. Lowerer::InsertBranch(Js::OpCode::BMI, negativeLabel, insertBeforeInstr);
  4075. }
  4076. return indexOpnd;
  4077. }
  4078. // Inlines fast-path for int Mul/Add or int Mul/Sub. If not int, call MulAdd/MulSub helper
  4079. bool LowererMD::TryGenerateFastMulAdd(IR::Instr * instrAdd, IR::Instr ** pInstrPrev)
  4080. {
  4081. IR::Instr *instrMul = instrAdd->GetPrevRealInstrOrLabel();
  4082. IR::Opnd *addSrc;
  4083. IR::RegOpnd *addCommonSrcOpnd;
  4084. Assert(instrAdd->m_opcode == Js::OpCode::Add_A || instrAdd->m_opcode == Js::OpCode::Sub_A);
  4085. if (instrAdd->m_opcode != Js::OpCode::Add_A)
  4086. {
  4087. // For Add_A we can use SMLAL, but there is no analog of that for Sub_A.
  4088. return false;
  4089. }
  4090. // Mul needs to be a single def reg
  4091. if (instrMul->m_opcode != Js::OpCode::Mul_A || !instrMul->GetDst()->IsRegOpnd())
  4092. {
  4093. // Cannot generate MulAdd
  4094. return false;
  4095. }
  4096. if (instrMul->HasBailOutInfo())
  4097. {
  4098. // Bailout will be generated for the Add, but not the Mul.
  4099. // We could handle this, but this path isn't used that much anymore.
  4100. return false;
  4101. }
  4102. IR::RegOpnd *regMulDst = instrMul->GetDst()->AsRegOpnd();
  4103. if (!regMulDst->m_sym->m_isSingleDef)
  4104. {
  4105. // Cannot generate MulAdd
  4106. return false;
  4107. }
  4108. // Only handle a * b + c, so dst of Mul needs to match left source of Add
  4109. if (instrMul->GetDst()->IsEqual(instrAdd->GetSrc1()))
  4110. {
  4111. addCommonSrcOpnd = instrAdd->GetSrc1()->AsRegOpnd();
  4112. addSrc = instrAdd->GetSrc2();
  4113. }
  4114. else if (instrMul->GetDst()->IsEqual(instrAdd->GetSrc2()))
  4115. {
  4116. addSrc = instrAdd->GetSrc1();
  4117. addCommonSrcOpnd = instrAdd->GetSrc2()->AsRegOpnd();
  4118. }
  4119. else
  4120. {
  4121. return false;
  4122. }
  4123. // Only handle a * b + c where c != a * b
  4124. if (instrAdd->GetSrc1()->IsEqual(instrAdd->GetSrc2()))
  4125. {
  4126. return false;
  4127. }
  4128. if (!addCommonSrcOpnd->m_isTempLastUse)
  4129. {
  4130. return false;
  4131. }
  4132. IR::Opnd *mulSrc1 = instrMul->GetSrc1();
  4133. IR::Opnd *mulSrc2 = instrMul->GetSrc2();
  4134. if (mulSrc1->IsRegOpnd() && mulSrc1->AsRegOpnd()->IsTaggedInt()
  4135. && mulSrc2->IsRegOpnd() && mulSrc2->AsRegOpnd()->IsTaggedInt())
  4136. {
  4137. return false;
  4138. }
  4139. IR::LabelInstr *labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  4140. IR::LabelInstr *labelDone = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, false);
  4141. // Save prevInstr for the main lower loop
  4142. *pInstrPrev = instrMul->m_prev;
  4143. // Generate int31 fast-path for Mul-Add, go to MulAdd helper if it fails, or one of the source is marked notInt
  4144. if (!(addSrc->IsRegOpnd() && addSrc->AsRegOpnd()->m_sym->AsStackSym()->m_isNotNumber)
  4145. && !(mulSrc1->IsRegOpnd() && mulSrc1->AsRegOpnd()->m_sym->AsStackSym()->m_isNotNumber)
  4146. && !(mulSrc2->IsRegOpnd() && mulSrc2->AsRegOpnd()->m_sym->AsStackSym()->m_isNotNumber))
  4147. {
  4148. // General idea:
  4149. // - mulSrc1: clear 1 but keep *2 - need special test for tagged int
  4150. // - mulSrc2: shift out the tag - test for overflow inplace
  4151. // - addSrc: keep as is - need special test for tagged int
  4152. //
  4153. // Concerns
  4154. // - we don't need to take care of negative zero/-0, here's why:
  4155. // - per ES5 spec, there are only way to get -0 with add/sub: -0 + -0, -0 -0.
  4156. // - first one is not applicable because -0 would not be a tagged int, so we'll use the helper.
  4157. // - second one is also not applicable because this fast path is only for mul-add, not mul-sub.
  4158. //
  4159. // Steps:
  4160. // (If not mulSrc1 and addSrc are Int31's, jump to $helper)
  4161. // s1 = SUB mulSrc1, 1 -- remove the tag from mulSrc1 but keep it as *2
  4162. // s2 = ASRS mulSrc2, 1 -- shift-out tag from mulSrc2
  4163. // BCC $helper -- if not tagged int, jmp to $helper
  4164. // (Now: mulSrc1 in s1, mulSrc2 in s2)
  4165. // r12 = ASR s3, 31 -- make r12 to be sign-extension of the addSrc.
  4166. // r12:s3 = SMLAL s1, s2 -- note: the add source comes from r12:s3, result is already tagged int = mulSrc1Val*2 * mulSrc2Val + addSrcVal * 2 + 1
  4167. // Overflow check:
  4168. // (SMLAL doesn't set the flags but we don't have 32bit overflow <=> r12-unsigned ? r12==0 : all 33 bits of 64bit result are 1's
  4169. // CMP r12, s3, ASR #31 -- check for overflow (== means no overflow)
  4170. // BNE $helper -- bail if the result overflowed
  4171. // Copy the result into dst
  4172. // dst = s3
  4173. // B $done
  4174. // $helper:
  4175. // ...
  4176. // $done:
  4177. IR::Instr* instr;
  4178. IR::RegOpnd* s1 = IR::RegOpnd::New(mulSrc1->GetType(), this->m_func);
  4179. IR::RegOpnd* s2 = IR::RegOpnd::New(mulSrc2->GetType(), this->m_func);
  4180. IR::RegOpnd* s3 = IR::RegOpnd::New(addSrc->GetType(), this->m_func);
  4181. IR::RegOpnd* opndRegScratch = IR::RegOpnd::New(nullptr, SCRATCH_REG, TyMachReg, this->m_func);
  4182. // (Load mulSrc1 at the top so we don't have to do it repeatedly)
  4183. if (!mulSrc1->IsRegOpnd())
  4184. {
  4185. Lowerer::InsertMove(s1, mulSrc1, instrAdd);
  4186. mulSrc1 = s1;
  4187. }
  4188. // Now: mulSrc1 is regOpnd (in case if it wasn't it's now s1).
  4189. // Load addSrc into s3. We'll use it as source and destination of SMLAL.
  4190. Lowerer::InsertMove(s3, addSrc, instrAdd);
  4191. // (If not mulSrc1 and addSrc are Int31's, jump to $helper)
  4192. bool areTaggedInts = mulSrc1->IsTaggedInt() && s3->IsTaggedInt();
  4193. if (!areTaggedInts)
  4194. {
  4195. this->GenerateSmIntPairTest(instrAdd, mulSrc1->AsRegOpnd(), s3->AsRegOpnd(), labelHelper);
  4196. }
  4197. // s1 = SUB mulSrc1, 1 -- remove the tag from mulSrc1 but keep it as *2
  4198. instr = IR::Instr::New(Js::OpCode::SUB, s1, mulSrc1, IR::IntConstOpnd::New(Js::VarTag_Shift, TyVar, this->m_func), m_func);
  4199. instrAdd->InsertBefore(instr);
  4200. // s2 = ASRS mulSrc2, 1 -- shift-out tag from mulSrc2
  4201. // BCC $helper -- if not tagged int, jmp to $helper
  4202. instr = IR::Instr::New(Js::OpCode::ASRS, s2, mulSrc2, IR::IntConstOpnd::New(Js::VarTag_Shift, TyVar, this->m_func), m_func);
  4203. instrAdd->InsertBefore(instr);
  4204. LegalizeMD::LegalizeInstr(instr);
  4205. if (!mulSrc2->IsTaggedInt()) // If we already pre-know it's tagged int, no need to check.
  4206. {
  4207. instr = IR::BranchInstr::New(Js::OpCode::BCC, labelHelper, this->m_func);
  4208. instrAdd->InsertBefore(instr);
  4209. }
  4210. // Now: mulSrc1 in s1, mulSrc2 in s2.
  4211. // r12 = ASR s3, 31 -- make r12 to be sign-extension of the addSrc.
  4212. instr = IR::Instr::New(Js::OpCode::ASR, opndRegScratch, s3, IR::IntConstOpnd::New(31, TyVar, this->m_func), m_func);
  4213. instrAdd->InsertBefore(instr);
  4214. // r12:s3 = SMLAL s1, s2 -- note: the add source comes from r12:s3, result is already tagged int = mulSrc1Val*2 * mulSrc2Val + addSrcVal * 2 + 1
  4215. instr = IR::Instr::New(Js::OpCode::SMLAL, s3, s1, s2, this->m_func);
  4216. instrAdd->InsertBefore(instr);
  4217. // Overflow check:
  4218. // (SMLAL doesn't set the flags but we don't have 32bit overflow <=> r12-unsigned ? r12==0 : all 33 bits of 64bit result are 1's
  4219. // CMP r12, s3, ASR #31 -- check for overflow (== means no overflow)
  4220. // BNE $helper -- bail if the result overflowed
  4221. instr = IR::Instr::New(Js::OpCode::CMP_ASR31, this->m_func);
  4222. instr->SetSrc1(opndRegScratch);
  4223. instr->SetSrc2(s3);
  4224. instrAdd->InsertBefore(instr);
  4225. instr = IR::BranchInstr::New(Js::OpCode::BNE, labelHelper, this->m_func);
  4226. instrAdd->InsertBefore(instr);
  4227. // Copy the result into dst
  4228. // dst = s3
  4229. Lowerer::InsertMove(instrAdd->GetDst(), s3, instrAdd);
  4230. LegalizeMD::LegalizeInstr(instr);
  4231. // B $done
  4232. instr = IR::BranchInstr::New(Js::OpCode::B, labelDone, this->m_func);
  4233. instrAdd->InsertBefore(instr);
  4234. instrAdd->InsertBefore(labelHelper);
  4235. instrAdd->InsertAfter(labelDone);
  4236. }
  4237. // Generate code to call the Mul-Add helper.
  4238. // Although for the case when one of the source is marked notInt we could just return false from here,
  4239. // it seems that since we did all the checks to see that this is mul+add, it makes sense to use mul-add helper
  4240. // rather than 2 separate helpers - one for mul and one for add (by returning false).
  4241. if (instrAdd->dstIsTempNumber)
  4242. {
  4243. m_lowerer->LoadHelperTemp(instrAdd, instrAdd);
  4244. }
  4245. else
  4246. {
  4247. IR::Opnd *tempOpnd = IR::IntConstOpnd::New(0, TyMachPtr, this->m_func);
  4248. this->LoadHelperArgument(instrAdd, tempOpnd);
  4249. }
  4250. this->m_lowerer->LoadScriptContext(instrAdd);
  4251. IR::JnHelperMethod helper;
  4252. if (addSrc == instrAdd->GetSrc2())
  4253. {
  4254. instrAdd->FreeSrc1();
  4255. IR::Opnd *addOpnd = instrAdd->UnlinkSrc2();
  4256. this->LoadHelperArgument(instrAdd, addOpnd);
  4257. helper = IR::HelperOp_MulAddRight;
  4258. }
  4259. else
  4260. {
  4261. AssertMsg(addSrc == instrAdd->GetSrc1(), "How did we get addSrc which not addInstr->Src1/2");
  4262. instrAdd->FreeSrc2();
  4263. IR::Opnd *addOpnd = instrAdd->UnlinkSrc1();
  4264. this->LoadHelperArgument(instrAdd, addOpnd);
  4265. helper = IR::HelperOp_MulAddLeft;
  4266. }
  4267. // Arg2, Arg1:
  4268. IR::Opnd *src2 = instrMul->UnlinkSrc2();
  4269. this->LoadHelperArgument(instrAdd, src2);
  4270. IR::Opnd *src1 = instrMul->UnlinkSrc1();
  4271. this->LoadHelperArgument(instrAdd, src1);
  4272. this->ChangeToHelperCall(instrAdd, helper);
  4273. instrMul->Remove();
  4274. return true;
  4275. }
  4276. IR::Instr *
  4277. LowererMD::LoadCheckedFloat(
  4278. IR::RegOpnd *opndOrig,
  4279. IR::RegOpnd *opndFloat,
  4280. IR::LabelInstr *labelInline,
  4281. IR::LabelInstr *labelHelper,
  4282. IR::Instr *instrInsert,
  4283. const bool checkForNullInLoopBody)
  4284. {
  4285. // Load one floating-point var into a VFP register, inserting checks to see if it's really a float:
  4286. // Rx = ASRS src, 1
  4287. // BCC $non-int
  4288. // Dx = VMOV Rx
  4289. // flt = VCVT.F64.S32 Dx
  4290. // B $labelInline
  4291. // $non-int
  4292. // LDR Ry, [src]
  4293. // CMP Ry, JavascriptNumber::`vtable'
  4294. // BNE $labelHelper
  4295. // flt = VLDR [t0 + offset(value)]
  4296. IR::Instr * instr = nullptr;
  4297. IR::Opnd * opnd = IR::RegOpnd::New(TyMachReg, this->m_func);
  4298. IR::Instr * instrFirst = IR::Instr::New(Js::OpCode::ASRS, opnd, opndOrig,
  4299. IR::IntConstOpnd::New(Js::AtomTag, TyMachReg, this->m_func),
  4300. this->m_func);
  4301. instrInsert->InsertBefore(instrFirst);
  4302. LegalizeMD::LegalizeInstr(instrFirst);
  4303. IR::LabelInstr * labelVar = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  4304. instr = IR::BranchInstr::New(Js::OpCode::BCC, labelVar, this->m_func);
  4305. instrInsert->InsertBefore(instr);
  4306. if (opndOrig->GetValueType().IsLikelyFloat())
  4307. {
  4308. // Make this path helper if value is likely a float
  4309. instrInsert->InsertBefore(IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true));
  4310. }
  4311. //Convert integer to floating point
  4312. Assert(opndFloat->GetType() == TyMachDouble);
  4313. instr = IR::Instr::New(Js::OpCode::VMOVARMVFP, opndFloat, opnd, this->m_func);
  4314. instrInsert->InsertBefore(instr);
  4315. //VCVT.F64.S32 opndFloat, opndFloat
  4316. instr = IR::Instr::New(Js::OpCode::VCVTF64S32, opndFloat, opndFloat, this->m_func);
  4317. instrInsert->InsertBefore(instr);
  4318. instr = IR::BranchInstr::New(Js::OpCode::B, labelInline, this->m_func);
  4319. instrInsert->InsertBefore(instr);
  4320. instrInsert->InsertBefore(labelVar);
  4321. LoadFloatValue(opndOrig, opndFloat, labelHelper, instrInsert, checkForNullInLoopBody);
  4322. return instrFirst;
  4323. }
  4324. void
  4325. LowererMD::EmitLoadFloatFromNumber(IR::Opnd *dst, IR::Opnd *src, IR::Instr *insertInstr)
  4326. {
  4327. IR::LabelInstr *labelDone;
  4328. IR::Instr *instr;
  4329. labelDone = EmitLoadFloatCommon(dst, src, insertInstr, insertInstr->HasBailOutInfo());
  4330. if (labelDone == nullptr)
  4331. {
  4332. // We're done
  4333. insertInstr->Remove();
  4334. return;
  4335. }
  4336. // $Done note: insertAfter
  4337. insertInstr->InsertAfter(labelDone);
  4338. if (!insertInstr->HasBailOutInfo())
  4339. {
  4340. // $Done
  4341. insertInstr->Remove();
  4342. return;
  4343. }
  4344. IR::LabelInstr *labelNoBailOut = nullptr;
  4345. IR::SymOpnd *tempSymOpnd = nullptr;
  4346. if (insertInstr->GetBailOutKind() == IR::BailOutPrimitiveButString)
  4347. {
  4348. if (!this->m_func->tempSymDouble)
  4349. {
  4350. this->m_func->tempSymDouble = StackSym::New(TyFloat64, this->m_func);
  4351. this->m_func->StackAllocate(this->m_func->tempSymDouble, MachDouble);
  4352. }
  4353. // LEA r3, tempSymDouble
  4354. IR::RegOpnd *reg3Opnd = IR::RegOpnd::New(TyMachReg, this->m_func);
  4355. tempSymOpnd = IR::SymOpnd::New(this->m_func->tempSymDouble, TyFloat64, this->m_func);
  4356. instr = IR::Instr::New(Js::OpCode::LEA, reg3Opnd, tempSymOpnd, this->m_func);
  4357. insertInstr->InsertBefore(instr);
  4358. // regBoolResult = to_number_fromPrimitive(value, &dst, allowUndef, scriptContext);
  4359. this->m_lowerer->LoadScriptContext(insertInstr);
  4360. IR::IntConstOpnd *allowUndefOpnd;
  4361. if (insertInstr->GetBailOutKind() == IR::BailOutPrimitiveButString)
  4362. {
  4363. allowUndefOpnd = IR::IntConstOpnd::New(true, TyInt32, this->m_func);
  4364. }
  4365. else
  4366. {
  4367. Assert(insertInstr->GetBailOutKind() == IR::BailOutNumberOnly);
  4368. allowUndefOpnd = IR::IntConstOpnd::New(false, TyInt32, this->m_func);
  4369. }
  4370. this->LoadHelperArgument(insertInstr, allowUndefOpnd);
  4371. this->LoadHelperArgument(insertInstr, reg3Opnd);
  4372. this->LoadHelperArgument(insertInstr, src);
  4373. IR::RegOpnd *regBoolResult = IR::RegOpnd::New(TyInt32, this->m_func);
  4374. instr = IR::Instr::New(Js::OpCode::Call, regBoolResult, IR::HelperCallOpnd::New(IR::HelperOp_ConvNumber_FromPrimitive, this->m_func), this->m_func);
  4375. insertInstr->InsertBefore(instr);
  4376. this->LowerCall(instr, 0);
  4377. // TEST regBoolResult, regBoolResult
  4378. instr = IR::Instr::New(Js::OpCode::TST, this->m_func);
  4379. instr->SetSrc1(regBoolResult);
  4380. instr->SetSrc2(regBoolResult);
  4381. insertInstr->InsertBefore(instr);
  4382. // BNE $noBailOut
  4383. labelNoBailOut = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  4384. instr = IR::BranchInstr::New(Js::OpCode::BNE, labelNoBailOut, this->m_func);
  4385. insertInstr->InsertBefore(instr);
  4386. }
  4387. // Bailout code
  4388. Assert(insertInstr->m_opcode == Js::OpCode::FromVar);
  4389. insertInstr->UnlinkDst();
  4390. insertInstr->FreeSrc1();
  4391. IR::Instr *bailoutInstr = insertInstr;
  4392. insertInstr = bailoutInstr->m_next;
  4393. this->m_lowerer->GenerateBailOut(bailoutInstr);
  4394. // $noBailOut
  4395. if (labelNoBailOut)
  4396. {
  4397. insertInstr->InsertBefore(labelNoBailOut);
  4398. Assert(dst->IsRegOpnd());
  4399. // VLDR dst, [pResult].f64
  4400. instr = IR::Instr::New(Js::OpCode::VLDR, dst, tempSymOpnd, this->m_func);
  4401. insertInstr->InsertBefore(instr);
  4402. LegalizeMD::LegalizeInstr(instr);
  4403. }
  4404. }
  4405. IR::LabelInstr*
  4406. LowererMD::EmitLoadFloatCommon(IR::Opnd *dst, IR::Opnd *src, IR::Instr *insertInstr, bool needHelperLabel)
  4407. {
  4408. IR::Instr *instr;
  4409. Assert(src->GetType() == TyVar);
  4410. Assert(dst->GetType() == TyFloat64 || TyFloat32);
  4411. bool isFloatConst = false;
  4412. IR::RegOpnd *regFloatOpnd = nullptr;
  4413. if (src->IsRegOpnd() && src->AsRegOpnd()->m_sym->m_isFltConst)
  4414. {
  4415. IR::RegOpnd *regOpnd = src->AsRegOpnd();
  4416. Assert(regOpnd->m_sym->m_isSingleDef);
  4417. Js::Var value = regOpnd->m_sym->GetFloatConstValueAsVar_PostGlobOpt();
  4418. IR::MemRefOpnd *memRef = IR::MemRefOpnd::New((BYTE*)value + Js::JavascriptNumber::GetValueOffset(), TyFloat64, this->m_func, IR::AddrOpndKindDynamicDoubleRef);
  4419. regFloatOpnd = IR::RegOpnd::New(TyFloat64, this->m_func);
  4420. instr = IR::Instr::New(Js::OpCode::VLDR, regFloatOpnd, memRef, this->m_func);
  4421. insertInstr->InsertBefore(instr);
  4422. LegalizeMD::LegalizeInstr(instr);
  4423. isFloatConst = true;
  4424. }
  4425. // Src is constant?
  4426. if (src->IsImmediateOpnd() || src->IsFloatConstOpnd())
  4427. {
  4428. regFloatOpnd = IR::RegOpnd::New(TyFloat64, this->m_func);
  4429. m_lowerer->LoadFloatFromNonReg(src, regFloatOpnd, insertInstr);
  4430. isFloatConst = true;
  4431. }
  4432. if (isFloatConst)
  4433. {
  4434. if (dst->GetType() == TyFloat32)
  4435. {
  4436. // VCVT.F32.F64 regOpnd32.f32, regOpnd.f64 -- Convert regOpnd from f64 to f32
  4437. IR::RegOpnd *regOpnd32 = regFloatOpnd->UseWithNewType(TyFloat32, this->m_func)->AsRegOpnd();
  4438. instr = IR::Instr::New(Js::OpCode::VCVTF32F64, regOpnd32, regFloatOpnd, this->m_func);
  4439. insertInstr->InsertBefore(instr);
  4440. // VSTR32 dst, regOpnd32
  4441. instr = IR::Instr::New(Js::OpCode::VMOV, dst, regOpnd32, this->m_func);
  4442. insertInstr->InsertBefore(instr);
  4443. }
  4444. else
  4445. {
  4446. instr = IR::Instr::New(Js::OpCode::VMOV, dst, regFloatOpnd, this->m_func);
  4447. insertInstr->InsertBefore(instr);
  4448. }
  4449. LegalizeMD::LegalizeInstr(instr);
  4450. return nullptr;
  4451. }
  4452. Assert(src->IsRegOpnd());
  4453. IR::LabelInstr *labelStore = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  4454. IR::LabelInstr *labelHelper;
  4455. IR::LabelInstr *labelDone = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  4456. if (needHelperLabel)
  4457. {
  4458. labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  4459. }
  4460. else
  4461. {
  4462. labelHelper = labelDone;
  4463. }
  4464. IR::RegOpnd *reg2 = IR::RegOpnd::New(TyMachDouble, this->m_func);
  4465. // Load the float value in reg2
  4466. this->LoadCheckedFloat(src->AsRegOpnd(), reg2, labelStore, labelHelper, insertInstr, needHelperLabel);
  4467. // $Store
  4468. insertInstr->InsertBefore(labelStore);
  4469. if (dst->GetType() == TyFloat32)
  4470. {
  4471. IR::RegOpnd *reg2_32 = reg2->UseWithNewType(TyFloat32, this->m_func)->AsRegOpnd();
  4472. // VCVT.F32.F64 r2_32.f32, r2.f64 -- Convert regOpnd from f64 to f32
  4473. instr = IR::Instr::New(Js::OpCode::VCVTF32F64, reg2_32, reg2, this->m_func);
  4474. insertInstr->InsertBefore(instr);
  4475. // VMOV dst, r2_32
  4476. instr = IR::Instr::New(Js::OpCode::VMOV, dst, reg2_32, this->m_func);
  4477. insertInstr->InsertBefore(instr);
  4478. }
  4479. else
  4480. {
  4481. // VMOV dst, r2
  4482. instr = IR::Instr::New(Js::OpCode::VMOV, dst, reg2, this->m_func);
  4483. insertInstr->InsertBefore(instr);
  4484. }
  4485. LegalizeMD::LegalizeInstr(instr);
  4486. // B $Done
  4487. instr = IR::BranchInstr::New(Js::OpCode::B, labelDone, this->m_func);
  4488. insertInstr->InsertBefore(instr);
  4489. if (needHelperLabel)
  4490. {
  4491. // $Helper
  4492. insertInstr->InsertBefore(labelHelper);
  4493. }
  4494. return labelDone;
  4495. }
  4496. void
  4497. LowererMD::EmitLoadFloat(IR::Opnd *dst, IR::Opnd *src, IR::Instr *insertInstr, IR::Instr * instrBailOut, IR::LabelInstr * labelBailOut)
  4498. {
  4499. IR::LabelInstr *labelDone;
  4500. IR::Instr *instr;
  4501. Assert(src->GetType() == TyVar);
  4502. Assert(dst->GetType() == TyFloat64 || TyFloat32);
  4503. Assert(src->IsRegOpnd());
  4504. if (dst->IsIndirOpnd())
  4505. {
  4506. LegalizeMD::LegalizeIndirOpndForVFP(insertInstr, dst->AsIndirOpnd());
  4507. }
  4508. labelDone = EmitLoadFloatCommon(dst, src, insertInstr, true);
  4509. if (labelDone == nullptr)
  4510. {
  4511. // We're done
  4512. return;
  4513. }
  4514. IR::BailOutKind bailOutKind = instrBailOut && instrBailOut->HasBailOutInfo() ? instrBailOut->GetBailOutKind() : IR::BailOutInvalid;
  4515. if (bailOutKind & IR::BailOutOnArrayAccessHelperCall)
  4516. {
  4517. // Bail out instead of making the helper call.
  4518. Assert(labelBailOut);
  4519. m_lowerer->InsertBranch(Js::OpCode::Br, labelBailOut, insertInstr);
  4520. insertInstr->InsertBefore(labelDone);
  4521. return;
  4522. }
  4523. IR::Opnd *memAddress = dst;
  4524. if (dst->IsRegOpnd())
  4525. {
  4526. IR::SymOpnd *symOpnd = nullptr;
  4527. if (dst->GetType() == TyFloat32)
  4528. {
  4529. symOpnd = IR::SymOpnd::New(StackSym::New(TyFloat32, this->m_func), TyFloat32, this->m_func);
  4530. this->m_func->StackAllocate(symOpnd->m_sym->AsStackSym(), sizeof(float));
  4531. }
  4532. else
  4533. {
  4534. symOpnd = IR::SymOpnd::New(StackSym::New(TyFloat64,this->m_func), TyMachDouble, this->m_func);
  4535. this->m_func->StackAllocate(symOpnd->m_sym->AsStackSym(), sizeof(double));
  4536. }
  4537. memAddress = symOpnd;
  4538. }
  4539. // LEA r3, dst
  4540. IR::RegOpnd *reg3Opnd = IR::RegOpnd::New(TyMachReg, this->m_func);
  4541. instr = IR::Instr::New(Js::OpCode::LEA, reg3Opnd, memAddress, this->m_func);
  4542. insertInstr->InsertBefore(instr);
  4543. // to_number_full(value, &dst, scriptContext);
  4544. // Create dummy binary op to convert into helper
  4545. instr = IR::Instr::New(Js::OpCode::Add_A, this->m_func);
  4546. instr->SetSrc1(src);
  4547. instr->SetSrc2(reg3Opnd);
  4548. insertInstr->InsertBefore(instr);
  4549. if (BailOutInfo::IsBailOutOnImplicitCalls(bailOutKind))
  4550. {
  4551. _Analysis_assume_(instrBailOut != nullptr);
  4552. instr = instr->ConvertToBailOutInstr(instrBailOut->GetBailOutInfo(), bailOutKind);
  4553. if (instrBailOut->GetBailOutInfo()->bailOutInstr == instrBailOut)
  4554. {
  4555. IR::Instr * instrShare = instrBailOut->ShareBailOut();
  4556. m_lowerer->LowerBailTarget(instrShare);
  4557. }
  4558. }
  4559. IR::JnHelperMethod helper;
  4560. if (dst->GetType() == TyFloat32)
  4561. {
  4562. helper = IR::HelperOp_ConvFloat_Helper;
  4563. }
  4564. else
  4565. {
  4566. helper = IR::HelperOp_ConvNumber_Helper;
  4567. }
  4568. this->m_lowerer->LowerBinaryHelperMem(instr, helper);
  4569. if (dst->IsRegOpnd())
  4570. {
  4571. Js::OpCode opcode = (dst->GetType() == TyFloat32)? Js::OpCode::VLDR32: Js::OpCode::VLDR;
  4572. instr = IR::Instr::New(opcode, dst , memAddress, this->m_func);
  4573. insertInstr->InsertBefore(instr);
  4574. LegalizeMD::LegalizeInstr(instr);
  4575. }
  4576. // $Done
  4577. insertInstr->InsertBefore(labelDone);
  4578. }
  4579. void
  4580. LowererMD::GenerateNumberAllocation(IR::RegOpnd * opndDst, IR::Instr * instrInsert, bool isHelper)
  4581. {
  4582. size_t alignedAllocSize = Js::RecyclerJavascriptNumberAllocator::GetAlignedAllocSize(
  4583. m_func->GetScriptContextInfo()->IsRecyclerVerifyEnabled(),
  4584. m_func->GetScriptContextInfo()->GetRecyclerVerifyPad());
  4585. IR::RegOpnd * loadAllocatorAddressOpnd = IR::RegOpnd::New(TyMachPtr, this->m_func);
  4586. IR::Instr * loadAllocatorAddressInstr = IR::Instr::New(Js::OpCode::LDIMM, loadAllocatorAddressOpnd,
  4587. m_lowerer->LoadScriptContextValueOpnd(instrInsert, ScriptContextValue::ScriptContextNumberAllocator), this->m_func);
  4588. instrInsert->InsertBefore(loadAllocatorAddressInstr);
  4589. IR::IndirOpnd * endAddressOpnd = IR::IndirOpnd::New(loadAllocatorAddressOpnd,
  4590. Js::RecyclerJavascriptNumberAllocator::GetEndAddressOffset(), TyMachPtr, this->m_func);
  4591. IR::IndirOpnd * freeObjectListOpnd = IR::IndirOpnd::New(loadAllocatorAddressOpnd,
  4592. Js::RecyclerJavascriptNumberAllocator::GetFreeObjectListOffset(), TyMachPtr, this->m_func);
  4593. // LDR dst, allocator->freeObjectList
  4594. IR::Instr * loadMemBlockInstr = IR::Instr::New(Js::OpCode::LDR, opndDst, freeObjectListOpnd, this->m_func);
  4595. instrInsert->InsertBefore(loadMemBlockInstr);
  4596. // nextMemBlock = ADD dst, allocSize
  4597. IR::RegOpnd * nextMemBlockOpnd = IR::RegOpnd::New(TyMachPtr, this->m_func);
  4598. IR::Instr * loadNextMemBlockInstr = IR::Instr::New(Js::OpCode::ADD, nextMemBlockOpnd, opndDst,
  4599. IR::IntConstOpnd::New(alignedAllocSize, TyInt32, this->m_func), this->m_func);
  4600. instrInsert->InsertBefore(loadNextMemBlockInstr);
  4601. // CMP nextMemBlock, allocator->endAddress
  4602. IR::Instr * checkInstr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  4603. checkInstr->SetSrc1(nextMemBlockOpnd);
  4604. checkInstr->SetSrc2(endAddressOpnd);
  4605. instrInsert->InsertBefore(checkInstr);
  4606. LegalizeMD::LegalizeInstr(checkInstr);
  4607. // BHI $helper
  4608. IR::LabelInstr * helperLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  4609. IR::BranchInstr * branchInstr = IR::BranchInstr::New(Js::OpCode::BHI, helperLabel, this->m_func);
  4610. instrInsert->InsertBefore(branchInstr);
  4611. // LDR allocator->freeObjectList, nextMemBlock
  4612. IR::Instr * setFreeObjectListInstr = IR::Instr::New(Js::OpCode::LDR, freeObjectListOpnd, nextMemBlockOpnd, this->m_func);
  4613. instrInsert->InsertBefore(setFreeObjectListInstr);
  4614. // B $done
  4615. IR::LabelInstr * doneLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, isHelper);
  4616. IR::BranchInstr * branchToDoneInstr = IR::BranchInstr::New(Js::OpCode::B, doneLabel, this->m_func);
  4617. instrInsert->InsertBefore(branchToDoneInstr);
  4618. // $helper:
  4619. instrInsert->InsertBefore(helperLabel);
  4620. // arg1 = allocator
  4621. this->LoadHelperArgument(instrInsert, m_lowerer->LoadScriptContextValueOpnd(instrInsert, ScriptContextValue::ScriptContextNumberAllocator));
  4622. // dst = Call AllocUninitializedNumber
  4623. IR::Instr * instrCall = IR::Instr::New(Js::OpCode::Call, opndDst,
  4624. IR::HelperCallOpnd::New(IR::HelperAllocUninitializedNumber, this->m_func), this->m_func);
  4625. instrInsert->InsertBefore(instrCall);
  4626. this->LowerCall(instrCall, 0);
  4627. // $done:
  4628. instrInsert->InsertBefore(doneLabel);
  4629. }
  4630. void
  4631. LowererMD::GenerateFastRecyclerAlloc(size_t allocSize, IR::RegOpnd* newObjDst, IR::Instr* insertionPointInstr, IR::LabelInstr* allocHelperLabel, IR::LabelInstr* allocDoneLabel)
  4632. {
  4633. ScriptContextInfo* scriptContext = this->m_func->GetScriptContextInfo();
  4634. void* allocatorAddress;
  4635. uint32 endAddressOffset;
  4636. uint32 freeListOffset;
  4637. size_t alignedSize = HeapInfo::GetAlignedSizeNoCheck(allocSize);
  4638. bool allowNativeCodeBumpAllocation = scriptContext->GetRecyclerAllowNativeCodeBumpAllocation();
  4639. Recycler::GetNormalHeapBlockAllocatorInfoForNativeAllocation((void*)scriptContext->GetRecyclerAddr(), alignedSize,
  4640. allocatorAddress, endAddressOffset, freeListOffset,
  4641. allowNativeCodeBumpAllocation, this->m_func->IsOOPJIT());
  4642. IR::RegOpnd * allocatorAddressRegOpnd = IR::RegOpnd::New(TyMachPtr, this->m_func);
  4643. // LDIMM allocatorAddressRegOpnd, allocator
  4644. IR::AddrOpnd* allocatorAddressOpnd = IR::AddrOpnd::New(allocatorAddress, IR::AddrOpndKindDynamicMisc, this->m_func);
  4645. IR::Instr * loadAllocatorAddressInstr = IR::Instr::New(Js::OpCode::LDIMM, allocatorAddressRegOpnd, allocatorAddressOpnd, this->m_func);
  4646. insertionPointInstr->InsertBefore(loadAllocatorAddressInstr);
  4647. IR::IndirOpnd * endAddressOpnd = IR::IndirOpnd::New(allocatorAddressRegOpnd, endAddressOffset, TyMachPtr, this->m_func);
  4648. IR::IndirOpnd * freeObjectListOpnd = IR::IndirOpnd::New(allocatorAddressRegOpnd, freeListOffset, TyMachPtr, this->m_func);
  4649. // LDR newObjDst, allocator->freeObjectList
  4650. IR::Instr * loadMemBlockInstr = IR::Instr::New(Js::OpCode::LDR, newObjDst, freeObjectListOpnd, this->m_func);
  4651. insertionPointInstr->InsertBefore(loadMemBlockInstr);
  4652. LegalizeMD::LegalizeInstr(loadMemBlockInstr);
  4653. // nextMemBlock = ADD newObjDst, allocSize
  4654. IR::RegOpnd * nextMemBlockOpnd = IR::RegOpnd::New(TyMachPtr, this->m_func);
  4655. IR::IntConstOpnd* allocSizeOpnd = IR::IntConstOpnd::New((int32)allocSize, TyInt32, this->m_func);
  4656. IR::Instr * loadNextMemBlockInstr = IR::Instr::New(Js::OpCode::ADD, nextMemBlockOpnd, newObjDst, allocSizeOpnd, this->m_func);
  4657. insertionPointInstr->InsertBefore(loadNextMemBlockInstr);
  4658. LegalizeMD::LegalizeInstr(loadNextMemBlockInstr);
  4659. // CMP nextMemBlock, allocator->endAddress
  4660. IR::Instr * checkInstr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  4661. checkInstr->SetSrc1(nextMemBlockOpnd);
  4662. checkInstr->SetSrc2(endAddressOpnd);
  4663. insertionPointInstr->InsertBefore(checkInstr);
  4664. LegalizeMD::LegalizeInstr(checkInstr);
  4665. // BHI $allocHelper
  4666. IR::BranchInstr * branchToAllocHelperInstr = IR::BranchInstr::New(Js::OpCode::BHI, allocHelperLabel, this->m_func);
  4667. insertionPointInstr->InsertBefore(branchToAllocHelperInstr);
  4668. // LDR allocator->freeObjectList, nextMemBlock
  4669. IR::Instr * setFreeObjectListInstr = IR::Instr::New(Js::OpCode::LDR, freeObjectListOpnd, nextMemBlockOpnd, this->m_func);
  4670. insertionPointInstr->InsertBefore(setFreeObjectListInstr);
  4671. LegalizeMD::LegalizeInstr(setFreeObjectListInstr);
  4672. // B $allocDone
  4673. IR::BranchInstr * branchToAllocDoneInstr = IR::BranchInstr::New(Js::OpCode::B, allocDoneLabel, this->m_func);
  4674. insertionPointInstr->InsertBefore(branchToAllocDoneInstr);
  4675. }
  4676. void
  4677. LowererMD::GenerateClz(IR::Instr * instr)
  4678. {
  4679. Assert(instr->GetSrc1()->IsInt32() || instr->GetSrc1()->IsUInt32());
  4680. Assert(IRType_IsNativeInt(instr->GetDst()->GetType()));
  4681. instr->m_opcode = Js::OpCode::CLZ;
  4682. LegalizeMD::LegalizeInstr(instr);
  4683. }
  4684. void
  4685. LowererMD::SaveDoubleToVar(IR::RegOpnd * dstOpnd, IR::RegOpnd *opndFloat, IR::Instr *instrOrig, IR::Instr *instrInsert, bool isHelper)
  4686. {
  4687. // Call JSNumber::ToVar to save the float operand to the result of the original (var) instruction
  4688. IR::Opnd * symVTableDst;
  4689. IR::Opnd * symDblDst;
  4690. IR::Opnd * symTypeDst;
  4691. IR::Instr *newInstr;
  4692. IR::Instr * numberInitInsertInstr = nullptr;
  4693. if (instrOrig->dstIsTempNumber)
  4694. {
  4695. // Use the original dst to get the temp number sym
  4696. StackSym * tempNumberSym = this->m_lowerer->GetTempNumberSym(instrOrig->GetDst(), instrOrig->dstIsTempNumberTransferred);
  4697. // LEA dst, &tempSym
  4698. IR::SymOpnd * symTempSrc = IR::SymOpnd::New(tempNumberSym, TyMachPtr, this->m_func);
  4699. newInstr = IR::Instr::New(Js::OpCode::LEA, dstOpnd, symTempSrc, this->m_func);
  4700. instrInsert->InsertBefore(newInstr);
  4701. LegalizeMD::LegalizeInstr(newInstr);
  4702. symVTableDst = IR::SymOpnd::New(tempNumberSym, TyMachPtr, this->m_func);
  4703. symDblDst = IR::SymOpnd::New(tempNumberSym, (uint32)Js::JavascriptNumber::GetValueOffset(), TyFloat64, this->m_func);
  4704. symTypeDst = IR::SymOpnd::New(tempNumberSym, (uint32)Js::JavascriptNumber::GetOffsetOfType(), TyMachPtr, this->m_func);
  4705. if (this->m_lowerer->outerMostLoopLabel == nullptr)
  4706. {
  4707. // If we are not in loop, just insert in place
  4708. numberInitInsertInstr = instrInsert;
  4709. }
  4710. else
  4711. {
  4712. // Otherwise, initialize in the outer most loop top if we haven't initialize it yet.
  4713. numberInitInsertInstr = this->m_lowerer->initializedTempSym->TestAndSet(tempNumberSym->m_id) ?
  4714. nullptr : this->m_lowerer->outerMostLoopLabel;
  4715. }
  4716. }
  4717. else
  4718. {
  4719. this->GenerateNumberAllocation(dstOpnd, instrInsert, isHelper);
  4720. symVTableDst = IR::IndirOpnd::New(dstOpnd, 0, TyMachPtr, this->m_func);
  4721. symDblDst = IR::IndirOpnd::New(dstOpnd, (uint32)Js::JavascriptNumber::GetValueOffset(), TyFloat64, this->m_func);
  4722. symTypeDst = IR::IndirOpnd::New(dstOpnd, (uint32)Js::JavascriptNumber::GetOffsetOfType(), TyMachPtr, this->m_func);
  4723. numberInitInsertInstr = instrInsert;
  4724. }
  4725. if (numberInitInsertInstr)
  4726. {
  4727. IR::Opnd *jsNumberVTable = m_lowerer->LoadVTableValueOpnd(numberInitInsertInstr, VTableValue::VtableJavascriptNumber);
  4728. // STR dst->vtable, JavascriptNumber::vtable
  4729. newInstr = IR::Instr::New(Js::OpCode::STR, symVTableDst, jsNumberVTable, this->m_func);
  4730. numberInitInsertInstr->InsertBefore(newInstr);
  4731. LegalizeMD::LegalizeInstr(newInstr);
  4732. // STR dst->type, JavascriptNumber_type
  4733. IR::Opnd *typeOpnd = m_lowerer->LoadLibraryValueOpnd(numberInitInsertInstr, LibraryValue::ValueNumberTypeStatic);
  4734. newInstr = IR::Instr::New(Js::OpCode::STR, symTypeDst, typeOpnd, this->m_func);
  4735. numberInitInsertInstr->InsertBefore(newInstr);
  4736. LegalizeMD::LegalizeInstr(newInstr);
  4737. }
  4738. // VSTR dst->value, opndFloat ; copy the float result to the temp JavascriptNumber
  4739. newInstr = IR::Instr::New(Js::OpCode::VSTR, symDblDst, opndFloat, this->m_func);
  4740. instrInsert->InsertBefore(newInstr);
  4741. LegalizeMD::LegalizeInstr(newInstr);
  4742. }
  4743. void
  4744. LowererMD::GenerateFastAbs(IR::Opnd *dst, IR::Opnd *src, IR::Instr *callInstr, IR::Instr *insertInstr, IR::LabelInstr *labelHelper, IR::LabelInstr *labelDone)
  4745. {
  4746. // src32 = ASRS src, VarShift
  4747. // BCC $helper <== float abs if emitFloatAbs
  4748. // dst32 = EOR src32, src32 ASR #31
  4749. // dst32 = SUB dst32, src32 ASR #31
  4750. // TIOFLW src32
  4751. // BMI $helper
  4752. // dst = LSL src32, VarShift
  4753. // dst = ADD dst, AtomTag
  4754. // B $done
  4755. // $float
  4756. // CMP [src], JavascriptNumber.vtable
  4757. // BNE $helper
  4758. // VLDR dx, [src + offsetof(value)]
  4759. // VABS.f64 dx, dx
  4760. // dst = DoubleToVar(dx)
  4761. // $helper:
  4762. // <call helper>
  4763. // $done:
  4764. bool isInt = false;
  4765. bool isNotInt = false;
  4766. IR::Instr *instr;
  4767. IR::LabelInstr *labelFloat = nullptr;
  4768. if (src->IsRegOpnd())
  4769. {
  4770. if (src->AsRegOpnd()->IsTaggedInt())
  4771. {
  4772. isInt = true;
  4773. }
  4774. else if (src->AsRegOpnd()->IsNotInt())
  4775. {
  4776. isNotInt = true;
  4777. }
  4778. }
  4779. else if (src->IsAddrOpnd())
  4780. {
  4781. IR::AddrOpnd *varOpnd = src->AsAddrOpnd();
  4782. Assert(varOpnd->IsVar() && Js::TaggedInt::Is(varOpnd->m_address));
  4783. int absValue = abs(Js::TaggedInt::ToInt32(varOpnd->m_address));
  4784. if (!Js::TaggedInt::IsOverflow(absValue))
  4785. {
  4786. varOpnd->SetAddress(Js::TaggedInt::ToVarUnchecked(absValue), IR::AddrOpndKindConstantVar);
  4787. Lowerer::InsertMove(dst, varOpnd, insertInstr);
  4788. }
  4789. }
  4790. if (src->IsRegOpnd() == false)
  4791. {
  4792. //Lets legalize right away as floating point fast path works on the same src.
  4793. IR::RegOpnd *regOpnd = IR::RegOpnd::New(TyVar, this->m_func);
  4794. instr = IR::Instr::New(Js::OpCode::MOV, regOpnd, src, this->m_func);
  4795. insertInstr->InsertBefore(instr);
  4796. LegalizeMD::LegalizeInstr(instr);
  4797. src = regOpnd;
  4798. }
  4799. bool emitFloatAbs = !isInt;
  4800. if (!isNotInt)
  4801. {
  4802. // src32 = ASRS src, VarTag_Shift
  4803. IR::RegOpnd *src32 = src32 = IR::RegOpnd::New(TyMachReg, this->m_func);
  4804. instr = IR::Instr::New(
  4805. Js::OpCode::ASRS, src32, src, IR::IntConstOpnd::New(Js::VarTag_Shift, TyInt32, this->m_func), this->m_func);
  4806. insertInstr->InsertBefore(instr);
  4807. if (!isInt)
  4808. {
  4809. if (emitFloatAbs)
  4810. {
  4811. // BCC $float
  4812. labelFloat = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  4813. instr = IR::BranchInstr::New(Js::OpCode::BCC, labelFloat, this->m_func);
  4814. insertInstr->InsertBefore(instr);
  4815. }
  4816. else
  4817. {
  4818. instr = IR::BranchInstr::New(Js::OpCode::BCC, labelHelper, this->m_func);
  4819. insertInstr->InsertBefore(instr);
  4820. }
  4821. }
  4822. // dst32 = EOR src32, src32 ASR #31
  4823. IR::RegOpnd *dst32 = IR::RegOpnd::New(TyMachReg, this->m_func);
  4824. instr = IR::Instr::New(Js::OpCode::CLRSIGN, dst32, src32, this->m_func);
  4825. insertInstr->InsertBefore(instr);
  4826. // dst32 = SUB dst32, src32 ASR #31
  4827. instr = IR::Instr::New(Js::OpCode::SBCMPLNT, dst32, dst32, src32, this->m_func);
  4828. insertInstr->InsertBefore(instr);
  4829. // TEQ dst32, dst32 LSL #1
  4830. instr = IR::Instr::New(Js::OpCode::TIOFLW, this->m_func);
  4831. instr->SetSrc1(dst32);
  4832. insertInstr->InsertBefore(instr);
  4833. // BMI $helper
  4834. instr = IR::BranchInstr::New(Js::OpCode::BMI, labelHelper, this->m_func);
  4835. insertInstr->InsertBefore(instr);
  4836. // dst32 = LSL dst32, VarShift
  4837. instr = IR::Instr::New(
  4838. Js::OpCode::LSL, dst32, dst32, IR::IntConstOpnd::New(Js::VarTag_Shift, TyMachReg, this->m_func), this->m_func);
  4839. insertInstr->InsertBefore(instr);
  4840. // dst = ADD dst, AtomTag
  4841. instr = IR::Instr::New(
  4842. Js::OpCode::ADD, dst, dst32, IR::IntConstOpnd::New(Js::AtomTag, TyMachReg, this->m_func), this->m_func);
  4843. insertInstr->InsertBefore(instr);
  4844. LegalizeMD::LegalizeInstr(instr);
  4845. }
  4846. if (labelFloat)
  4847. {
  4848. // B $done
  4849. instr = IR::BranchInstr::New(Js::OpCode::B, labelDone, this->m_func);
  4850. insertInstr->InsertBefore(instr);
  4851. // $float
  4852. insertInstr->InsertBefore(labelFloat);
  4853. }
  4854. if (emitFloatAbs)
  4855. {
  4856. // CMP [src], JavascriptNumber.vtable
  4857. IR::Opnd *opnd = IR::IndirOpnd::New(src->AsRegOpnd(), (int32)0, TyMachPtr, this->m_func);
  4858. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  4859. instr->SetSrc1(opnd);
  4860. instr->SetSrc2(m_lowerer->LoadVTableValueOpnd(insertInstr, VTableValue::VtableJavascriptNumber));
  4861. insertInstr->InsertBefore(instr);
  4862. LegalizeMD::LegalizeInstr(instr);
  4863. // BNE $helper
  4864. instr = IR::BranchInstr::New(Js::OpCode::BNE, labelHelper, this->m_func);
  4865. insertInstr->InsertBefore(instr);
  4866. // VLDR dx, [src + offsetof(value)]
  4867. opnd = IR::IndirOpnd::New(src->AsRegOpnd(), Js::JavascriptNumber::GetValueOffset(), TyMachDouble, this->m_func);
  4868. IR::RegOpnd *regOpnd = IR::RegOpnd::New(TyMachDouble, this->m_func);
  4869. instr = IR::Instr::New(Js::OpCode::VLDR, regOpnd, opnd, this->m_func);
  4870. insertInstr->InsertBefore(instr);
  4871. // VABS.f64 dy, dx
  4872. IR::RegOpnd *resultRegOpnd = IR::RegOpnd::New(TyMachDouble, this->m_func);
  4873. instr = IR::Instr::New(Js::OpCode::VABS, resultRegOpnd, regOpnd, this->m_func);
  4874. insertInstr->InsertBefore(instr);
  4875. // dst = DoubleToVar(dy)
  4876. SaveDoubleToVar(callInstr->GetDst()->AsRegOpnd(), resultRegOpnd, callInstr, insertInstr);
  4877. }
  4878. }
  4879. bool LowererMD::GenerateFastDivAndRem(IR::Instr* instrDiv, IR::LabelInstr* bailOutLabel)
  4880. {
  4881. return false;
  4882. }
  4883. void
  4884. LowererMD::EmitInt4Instr(IR::Instr *instr)
  4885. {
  4886. IR::Instr * newInstr;
  4887. IR::Opnd * src1;
  4888. IR::Opnd * src2;
  4889. switch (instr->m_opcode)
  4890. {
  4891. case Js::OpCode::Neg_I4:
  4892. instr->m_opcode = Js::OpCode::RSB;
  4893. instr->SetSrc2(IR::IntConstOpnd::New(0, TyInt32, instr->m_func));
  4894. break;
  4895. case Js::OpCode::Not_I4:
  4896. instr->m_opcode = Js::OpCode::MVN;
  4897. break;
  4898. case Js::OpCode::Add_I4:
  4899. ChangeToAdd(instr, false /* needFlags */);
  4900. break;
  4901. case Js::OpCode::Sub_I4:
  4902. ChangeToSub(instr, false /* needFlags */);
  4903. break;
  4904. case Js::OpCode::Mul_I4:
  4905. instr->m_opcode = Js::OpCode::MUL;
  4906. break;
  4907. case Js::OpCode::DivU_I4:
  4908. AssertMsg(UNREACHED, "Unsigned div NYI");
  4909. case Js::OpCode::Div_I4:
  4910. instr->m_opcode = Js::OpCode::SDIV;
  4911. break;
  4912. case Js::OpCode::RemU_I4:
  4913. AssertMsg(UNREACHED, "Unsigned rem NYI");
  4914. case Js::OpCode::Rem_I4:
  4915. instr->m_opcode = Js::OpCode::REM;
  4916. break;
  4917. case Js::OpCode::Or_I4:
  4918. instr->m_opcode = Js::OpCode::ORR;
  4919. break;
  4920. case Js::OpCode::Xor_I4:
  4921. instr->m_opcode = Js::OpCode::EOR;
  4922. break;
  4923. case Js::OpCode::And_I4:
  4924. instr->m_opcode = Js::OpCode::AND;
  4925. break;
  4926. case Js::OpCode::Shl_I4:
  4927. case Js::OpCode::ShrU_I4:
  4928. case Js::OpCode::Shr_I4:
  4929. ChangeToShift(instr, false /* needFlags */);
  4930. break;
  4931. case Js::OpCode::BrTrue_I4:
  4932. instr->m_opcode = Js::OpCode::BNE;
  4933. goto br1_Common;
  4934. case Js::OpCode::BrFalse_I4:
  4935. instr->m_opcode = Js::OpCode::BEQ;
  4936. br1_Common:
  4937. src1 = instr->UnlinkSrc1();
  4938. newInstr = IR::Instr::New(Js::OpCode::CMP, instr->m_func);
  4939. instr->InsertBefore(newInstr);
  4940. newInstr->SetSrc1(src1);
  4941. newInstr->SetSrc2(IR::IntConstOpnd::New(0, TyInt32, instr->m_func));
  4942. // We know this CMP is legal.
  4943. return;
  4944. case Js::OpCode::BrEq_I4:
  4945. instr->m_opcode = Js::OpCode::BEQ;
  4946. goto br2_Common;
  4947. case Js::OpCode::BrNeq_I4:
  4948. instr->m_opcode = Js::OpCode::BNE;
  4949. goto br2_Common;
  4950. case Js::OpCode::BrGt_I4:
  4951. instr->m_opcode = Js::OpCode::BGT;
  4952. goto br2_Common;
  4953. case Js::OpCode::BrGe_I4:
  4954. instr->m_opcode = Js::OpCode::BGE;
  4955. goto br2_Common;
  4956. case Js::OpCode::BrLe_I4:
  4957. instr->m_opcode = Js::OpCode::BLE;
  4958. goto br2_Common;
  4959. case Js::OpCode::BrLt_I4:
  4960. instr->m_opcode = Js::OpCode::BLT;
  4961. goto br2_Common;
  4962. case Js::OpCode::BrUnGt_I4:
  4963. instr->m_opcode = Js::OpCode::BHI;
  4964. goto br2_Common;
  4965. case Js::OpCode::BrUnGe_I4:
  4966. instr->m_opcode = Js::OpCode::BCS;
  4967. goto br2_Common;
  4968. case Js::OpCode::BrUnLt_I4:
  4969. instr->m_opcode = Js::OpCode::BCC;
  4970. goto br2_Common;
  4971. case Js::OpCode::BrUnLe_I4:
  4972. instr->m_opcode = Js::OpCode::BLS;
  4973. goto br2_Common;
  4974. br2_Common:
  4975. src1 = instr->UnlinkSrc1();
  4976. src2 = instr->UnlinkSrc2();
  4977. newInstr = IR::Instr::New(Js::OpCode::CMP, instr->m_func);
  4978. instr->InsertBefore(newInstr);
  4979. newInstr->SetSrc1(src1);
  4980. newInstr->SetSrc2(src2);
  4981. // Let instr point to the CMP so we can legalize it.
  4982. instr = newInstr;
  4983. break;
  4984. default:
  4985. AssertMsg(UNREACHED, "NYI I4 instr");
  4986. break;
  4987. }
  4988. LegalizeMD::LegalizeInstr(instr);
  4989. }
  4990. void
  4991. LowererMD::LowerInt4NegWithBailOut(
  4992. IR::Instr *const instr,
  4993. const IR::BailOutKind bailOutKind,
  4994. IR::LabelInstr *const bailOutLabel,
  4995. IR::LabelInstr *const skipBailOutLabel)
  4996. {
  4997. Assert(instr);
  4998. Assert(instr->m_opcode == Js::OpCode::Neg_I4);
  4999. Assert(!instr->HasBailOutInfo());
  5000. Assert(bailOutKind & IR::BailOutOnResultConditions || bailOutKind == IR::BailOutOnFailedHoistedLoopCountBasedBoundCheck);
  5001. Assert(bailOutLabel);
  5002. Assert(instr->m_next == bailOutLabel);
  5003. Assert(skipBailOutLabel);
  5004. Assert(instr->GetDst()->IsInt32());
  5005. Assert(instr->GetSrc1()->IsInt32());
  5006. // RSBS dst, src1, #0
  5007. // BVS $bailOutLabel
  5008. // BEQ $bailOutLabel
  5009. // B $skipBailOut
  5010. // $bailOut:
  5011. // ...
  5012. // $skipBailOut:
  5013. // Lower the instruction
  5014. instr->m_opcode = Js::OpCode::RSBS;
  5015. instr->SetSrc2(IR::IntConstOpnd::New(0, TyInt32, instr->m_func));
  5016. Legalize(instr);
  5017. if(bailOutKind & IR::BailOutOnOverflow)
  5018. {
  5019. bailOutLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::BVS, bailOutLabel, instr->m_func));
  5020. }
  5021. if(bailOutKind & IR::BailOutOnNegativeZero)
  5022. {
  5023. bailOutLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::BEQ, bailOutLabel, instr->m_func));
  5024. }
  5025. // Skip bailout
  5026. bailOutLabel->InsertBefore(IR::BranchInstr::New(LowererMD::MDUncondBranchOpcode, skipBailOutLabel, instr->m_func));
  5027. }
  5028. void
  5029. LowererMD::LowerInt4AddWithBailOut(
  5030. IR::Instr *const instr,
  5031. const IR::BailOutKind bailOutKind,
  5032. IR::LabelInstr *const bailOutLabel,
  5033. IR::LabelInstr *const skipBailOutLabel)
  5034. {
  5035. Assert(instr);
  5036. Assert(instr->m_opcode == Js::OpCode::Add_I4);
  5037. Assert(!instr->HasBailOutInfo());
  5038. Assert(
  5039. (bailOutKind & IR::BailOutOnResultConditions) == IR::BailOutOnOverflow ||
  5040. bailOutKind == IR::BailOutOnFailedHoistedLoopCountBasedBoundCheck);
  5041. Assert(bailOutLabel);
  5042. Assert(instr->m_next == bailOutLabel);
  5043. Assert(skipBailOutLabel);
  5044. Assert(instr->GetDst()->IsInt32());
  5045. Assert(instr->GetSrc1()->IsInt32());
  5046. Assert(instr->GetSrc2()->IsInt32());
  5047. // ADDS dst, src1, src2
  5048. // BVC skipBailOutLabel
  5049. // fallthrough to bailout
  5050. const auto dst = instr->GetDst(), src1 = instr->GetSrc1(), src2 = instr->GetSrc2();
  5051. Assert(dst->IsRegOpnd());
  5052. const bool dstEquSrc1 = dst->IsEqual(src1), dstEquSrc2 = dst->IsEqual(src2);
  5053. if(dstEquSrc1 || dstEquSrc2)
  5054. {
  5055. LowererMD::ChangeToAssign(instr->SinkDst(Js::OpCode::Ld_I4, RegNOREG, skipBailOutLabel));
  5056. }
  5057. // Lower the instruction
  5058. ChangeToAdd(instr, true /* needFlags */);
  5059. Legalize(instr);
  5060. // Skip bailout on no overflow
  5061. bailOutLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::BVC, skipBailOutLabel, instr->m_func));
  5062. // Fall through to bailOutLabel
  5063. }
  5064. void
  5065. LowererMD::LowerInt4SubWithBailOut(
  5066. IR::Instr *const instr,
  5067. const IR::BailOutKind bailOutKind,
  5068. IR::LabelInstr *const bailOutLabel,
  5069. IR::LabelInstr *const skipBailOutLabel)
  5070. {
  5071. Assert(instr);
  5072. Assert(instr->m_opcode == Js::OpCode::Sub_I4);
  5073. Assert(!instr->HasBailOutInfo());
  5074. Assert(
  5075. (bailOutKind & IR::BailOutOnResultConditions) == IR::BailOutOnOverflow ||
  5076. bailOutKind == IR::BailOutOnFailedHoistedLoopCountBasedBoundCheck);
  5077. Assert(bailOutLabel);
  5078. Assert(instr->m_next == bailOutLabel);
  5079. Assert(skipBailOutLabel);
  5080. Assert(instr->GetDst()->IsInt32());
  5081. Assert(instr->GetSrc1()->IsInt32());
  5082. Assert(instr->GetSrc2()->IsInt32());
  5083. // SUBS dst, src1, src2
  5084. // BVC skipBailOutLabel
  5085. // fallthrough to bailout
  5086. const auto dst = instr->GetDst(), src1 = instr->GetSrc1(), src2 = instr->GetSrc2();
  5087. Assert(dst->IsRegOpnd());
  5088. const bool dstEquSrc1 = dst->IsEqual(src1), dstEquSrc2 = dst->IsEqual(src2);
  5089. if(dstEquSrc1 || dstEquSrc2)
  5090. {
  5091. LowererMD::ChangeToAssign(instr->SinkDst(Js::OpCode::Ld_I4, RegNOREG, skipBailOutLabel));
  5092. }
  5093. // Lower the instruction
  5094. ChangeToSub(instr, true /* needFlags */);
  5095. Legalize(instr);
  5096. // Skip bailout on no overflow
  5097. bailOutLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::BVC, skipBailOutLabel, instr->m_func));
  5098. // Fall through to bailOutLabel
  5099. }
  5100. void
  5101. LowererMD::LowerInt4MulWithBailOut(
  5102. IR::Instr *const instr,
  5103. const IR::BailOutKind bailOutKind,
  5104. IR::LabelInstr *const bailOutLabel,
  5105. IR::LabelInstr *const skipBailOutLabel)
  5106. {
  5107. Assert(instr);
  5108. Assert(instr->m_opcode == Js::OpCode::Mul_I4);
  5109. Assert(!instr->HasBailOutInfo());
  5110. Assert(bailOutKind & IR::BailOutOnResultConditions || bailOutKind == IR::BailOutOnFailedHoistedLoopCountBasedBoundCheck);
  5111. Assert(bailOutLabel);
  5112. Assert(instr->m_next == bailOutLabel);
  5113. Assert(skipBailOutLabel);
  5114. IR::Opnd *dst = instr->GetDst();
  5115. IR::Opnd *src1 = instr->GetSrc1();
  5116. IR::Opnd *src2 = instr->GetSrc2();
  5117. IR::Instr *insertInstr;
  5118. Assert(dst->IsInt32());
  5119. Assert(src1->IsInt32());
  5120. Assert(src2->IsInt32());
  5121. // (r12:)dst = SMULL dst, (r12,) src1, src2 -- do the signed mul into 64bit r12:dst, the result will be src1 * src2 * 2
  5122. instr->m_opcode = Js::OpCode::SMULL;
  5123. Legalize(instr);
  5124. //check negative zero
  5125. //
  5126. //If the result is zero, we need to check and only bail out if it would be -0.
  5127. // We know that if the result is 0/-0, at least operand should be zero.
  5128. // We should bailout if src1 + src2 < 0, as this proves that the other operand is negative
  5129. //
  5130. // CMN src1, src2
  5131. // BPL $skipBailOutLabel
  5132. //
  5133. //$bailOutLabel
  5134. // GenerateBailout
  5135. //
  5136. //$skipBailOutLabel
  5137. IR::LabelInstr *checkForNegativeZeroLabel = nullptr;
  5138. if(bailOutKind & IR::BailOutOnNegativeZero)
  5139. {
  5140. checkForNegativeZeroLabel = IR::LabelInstr::New(Js::OpCode::Label, instr->m_func, true);
  5141. bailOutLabel->InsertBefore(checkForNegativeZeroLabel);
  5142. Assert(dst->IsRegOpnd());
  5143. Assert(!src1->IsEqual(src2)); // cannot result in -0 if both operands are the same; GlobOpt should have figured that out
  5144. // CMN src1, src2
  5145. // BPL $skipBailOutLabel
  5146. insertInstr = IR::Instr::New(Js::OpCode::CMN, instr->m_func);
  5147. insertInstr->SetSrc1(src1);
  5148. insertInstr->SetSrc2(src2);
  5149. bailOutLabel->InsertBefore(insertInstr);
  5150. LegalizeMD::LegalizeInstr(insertInstr);
  5151. bailOutLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::BPL, skipBailOutLabel, instr->m_func));
  5152. // Fall through to bailOutLabel
  5153. }
  5154. const auto insertBeforeInstr = checkForNegativeZeroLabel ? checkForNegativeZeroLabel : bailOutLabel;
  5155. //check overflow
  5156. // CMP_ASR31 r12, dst
  5157. // BNE $bailOutLabel
  5158. if(bailOutKind & IR::BailOutOnMulOverflow || bailOutKind == IR::BailOutOnFailedHoistedLoopCountBasedBoundCheck)
  5159. {
  5160. // (SMULL doesn't set the flags but we don't have 32bit overflow <=> r12-unsigned ? r12==0 : all 33 bits of 64bit result are 1's
  5161. // CMP r12, dst, ASR #31 -- check for overflow (== means no overflow)
  5162. IR::RegOpnd* opndRegScratch = IR::RegOpnd::New(nullptr, SCRATCH_REG, TyMachReg, instr->m_func);
  5163. insertInstr = IR::Instr::New(Js::OpCode::CMP_ASR31, instr->m_func);
  5164. insertInstr->SetSrc1(opndRegScratch);
  5165. insertInstr->SetSrc2(dst);
  5166. insertBeforeInstr->InsertBefore(insertInstr);
  5167. // BNE $bailOutLabel -- bail if the result overflowed
  5168. insertInstr = IR::BranchInstr::New(Js::OpCode::BNE, bailOutLabel, instr->m_func);
  5169. insertBeforeInstr->InsertBefore(insertInstr);
  5170. }
  5171. if(bailOutKind & IR::BailOutOnNegativeZero)
  5172. {
  5173. // TST dst, dst
  5174. // BEQ $checkForNegativeZeroLabel
  5175. insertInstr = IR::Instr::New(Js::OpCode::TST, instr->m_func);
  5176. insertInstr->SetSrc1(dst);
  5177. insertInstr->SetSrc2(dst);
  5178. insertBeforeInstr->InsertBefore(insertInstr);
  5179. insertBeforeInstr->InsertBefore(IR::BranchInstr::New(Js::OpCode::BEQ, checkForNegativeZeroLabel, instr->m_func));
  5180. }
  5181. insertBeforeInstr->InsertBefore(IR::BranchInstr::New(Js::OpCode::B, skipBailOutLabel, instr->m_func));
  5182. }
  5183. void
  5184. LowererMD::LowerInt4RemWithBailOut(
  5185. IR::Instr *const instr,
  5186. const IR::BailOutKind bailOutKind,
  5187. IR::LabelInstr *const bailOutLabel,
  5188. IR::LabelInstr *const skipBailOutLabel) const
  5189. {
  5190. Assert(instr);
  5191. Assert(instr->m_opcode == Js::OpCode::Rem_I4);
  5192. Assert(!instr->HasBailOutInfo());
  5193. Assert(bailOutKind & IR::BailOutOnResultConditions);
  5194. Assert(bailOutLabel);
  5195. Assert(instr->m_next == bailOutLabel);
  5196. Assert(skipBailOutLabel);
  5197. IR::Opnd *dst = instr->GetDst();
  5198. IR::Opnd *src1 = instr->GetSrc1();
  5199. IR::Opnd *src2 = instr->GetSrc2();
  5200. Assert(dst->IsInt32());
  5201. Assert(src1->IsInt32());
  5202. Assert(src2->IsInt32());
  5203. //Lower the instruction
  5204. EmitInt4Instr(instr);
  5205. //check for negative zero
  5206. //We have, dst = src1 % src2
  5207. //We need to bailout if dst == 0 and src1 < 0
  5208. // tst dst, dst
  5209. // bne $skipBailOutLabel
  5210. // tst src1,src1
  5211. // bpl $skipBailOutLabel
  5212. //
  5213. //$bailOutLabel
  5214. // GenerateBailout();
  5215. //
  5216. //$skipBailOutLabel
  5217. if(bailOutKind & IR::BailOutOnNegativeZero)
  5218. {
  5219. IR::LabelInstr *checkForNegativeZeroLabel = IR::LabelInstr::New(Js::OpCode::Label, instr->m_func, true);
  5220. bailOutLabel->InsertBefore(checkForNegativeZeroLabel);
  5221. IR::Instr *insertInstr = IR::Instr::New(Js::OpCode::TST, instr->m_func);
  5222. insertInstr->SetSrc1(dst);
  5223. insertInstr->SetSrc2(dst);
  5224. bailOutLabel->InsertBefore(insertInstr);
  5225. IR::Instr *branchInstr = IR::BranchInstr::New(Js::OpCode::BNE, skipBailOutLabel, instr->m_func);
  5226. bailOutLabel->InsertBefore(branchInstr);
  5227. insertInstr = IR::Instr::New(Js::OpCode::TST, instr->m_func);
  5228. insertInstr->SetSrc1(src1);
  5229. insertInstr->SetSrc2(src1);
  5230. bailOutLabel->InsertBefore(insertInstr);
  5231. branchInstr = IR::BranchInstr::New(Js::OpCode::BPL, skipBailOutLabel, instr->m_func);
  5232. bailOutLabel->InsertBefore(branchInstr);
  5233. }
  5234. // Fall through to bailOutLabel
  5235. }
  5236. void
  5237. LowererMD::EmitLoadVar(IR::Instr *instrLoad, bool isFromUint32, bool isHelper)
  5238. {
  5239. // s2 = LSL s1, Js::VarTag_Shift -- restore the var tag on the result
  5240. // BO $ToVar (branch on overflow)
  5241. // dst = OR s2, 1
  5242. // B $done
  5243. //$ToVar:
  5244. // EmitLoadVarNoCheck
  5245. //$Done:
  5246. AssertMsg(instrLoad->GetSrc1()->IsRegOpnd(), "Should be regOpnd");
  5247. bool isInt = false;
  5248. bool isNotInt = false;
  5249. IR::RegOpnd *src1 = instrLoad->GetSrc1()->AsRegOpnd();
  5250. IR::LabelInstr *labelToVar = nullptr;
  5251. IR::LabelInstr *labelDone = nullptr;
  5252. IR::Instr *instr;
  5253. if (src1->IsTaggedInt())
  5254. {
  5255. isInt = true;
  5256. }
  5257. else if (src1->IsNotInt())
  5258. {
  5259. isNotInt = true;
  5260. }
  5261. if (!isNotInt)
  5262. {
  5263. IR::Opnd * opnd32src1 = src1->UseWithNewType(TyInt32, this->m_func);
  5264. IR::RegOpnd * opndReg2 = IR::RegOpnd::New(TyMachReg, this->m_func);
  5265. IR::Opnd * opnd32Reg2 = opndReg2->UseWithNewType(TyInt32, this->m_func);
  5266. if (!isInt)
  5267. {
  5268. labelToVar = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  5269. if (!isFromUint32)
  5270. {
  5271. // TEQ src1,src1 LS_u(#1) - TIOFLW is an alias for this pattern.
  5272. // XOR the src with itself shifted left one. If there's no overflow,
  5273. // the result should be positive (top bit clear).
  5274. instr = IR::Instr::New(Js::OpCode::TIOFLW, this->m_func);
  5275. instr->SetSrc1(opnd32src1);
  5276. instrLoad->InsertBefore(instr);
  5277. // BMI $ToVar
  5278. // Branch on negative result of the preceding test.
  5279. instr = IR::BranchInstr::New(Js::OpCode::BMI, labelToVar, this->m_func);
  5280. instrLoad->InsertBefore(instr);
  5281. }
  5282. else
  5283. {
  5284. //TST src1, 0xC0000000 -- test for length that is negative or overflows tagged int
  5285. instr = IR::Instr::New(Js::OpCode::TST, this->m_func);
  5286. instr->SetSrc1(opnd32src1);
  5287. instr->SetSrc2(IR::IntConstOpnd::New((int32)0x80000000 >> Js::VarTag_Shift, TyInt32, this->m_func));
  5288. instrLoad->InsertBefore(instr);
  5289. LegalizeMD::LegalizeInstr(instr);
  5290. // BNE $helper
  5291. instr = IR::BranchInstr::New(Js::OpCode::BNE, labelToVar, this->m_func);
  5292. instrLoad->InsertBefore(instr);
  5293. }
  5294. }
  5295. // s2 = LSL s1, Js::VarTag_Shift -- restore the var tag on the result
  5296. instr = IR::Instr::New(Js::OpCode::LSL, opnd32Reg2, opnd32src1,
  5297. IR::IntConstOpnd::New(Js::VarTag_Shift, TyInt8, this->m_func),
  5298. this->m_func);
  5299. instrLoad->InsertBefore(instr);
  5300. // dst = ADD s2, 1
  5301. instr = IR::Instr::New(Js::OpCode::ADD, instrLoad->GetDst(), opndReg2, IR::IntConstOpnd::New(1, TyMachReg, this->m_func), this->m_func);
  5302. instrLoad->InsertBefore(instr);
  5303. if (!isInt)
  5304. {
  5305. // B $done
  5306. labelDone = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, isHelper);
  5307. instr = IR::BranchInstr::New(Js::OpCode::B, labelDone, this->m_func);
  5308. instrLoad->InsertBefore(instr);
  5309. }
  5310. }
  5311. instr = instrLoad;
  5312. if (!isInt)
  5313. {
  5314. //$ToVar:
  5315. if (labelToVar)
  5316. {
  5317. instrLoad->InsertBefore(labelToVar);
  5318. }
  5319. this->EmitLoadVarNoCheck(instrLoad->GetDst()->AsRegOpnd(), src1, instrLoad, isFromUint32, isHelper);
  5320. }
  5321. //$Done:
  5322. if (labelDone)
  5323. {
  5324. instr->InsertAfter(labelDone);
  5325. }
  5326. instrLoad->Remove();
  5327. }
  5328. void
  5329. LowererMD::EmitLoadVarNoCheck(IR::RegOpnd * dst, IR::RegOpnd * src, IR::Instr *instrLoad, bool isFromUint32, bool isHelper)
  5330. {
  5331. IR::RegOpnd * floatReg = IR::RegOpnd::New(TyFloat64, this->m_func);
  5332. if (isFromUint32)
  5333. {
  5334. this->EmitUIntToFloat(floatReg, src, instrLoad);
  5335. }
  5336. else
  5337. {
  5338. this->EmitIntToFloat(floatReg, src, instrLoad);
  5339. }
  5340. this->SaveDoubleToVar(dst, floatReg, instrLoad, instrLoad, isHelper);
  5341. }
  5342. bool
  5343. LowererMD::EmitLoadInt32(IR::Instr *instrLoad, bool conversionFromObjectAllowed, bool bailOutOnHelper, IR::LabelInstr * labelBailOut)
  5344. {
  5345. // isInt:
  5346. // dst = ASR r1, AtomTag
  5347. // isNotInt:
  5348. // dst = ToInt32(r1)
  5349. // else:
  5350. // dst = ASRS r1, AtomTag
  5351. // BCS $Done
  5352. // dst = ToInt32(r1)
  5353. // $Done
  5354. AssertMsg(instrLoad->GetSrc1()->IsRegOpnd(), "Should be regOpnd");
  5355. bool isInt = false;
  5356. bool isNotInt = false;
  5357. IR::RegOpnd *src1 = instrLoad->GetSrc1()->AsRegOpnd();
  5358. IR::LabelInstr *labelDone = nullptr;
  5359. IR::LabelInstr *labelFloat = nullptr;
  5360. IR::LabelInstr *labelHelper = nullptr;
  5361. IR::Instr *instr;
  5362. if (src1->IsTaggedInt())
  5363. {
  5364. isInt = true;
  5365. }
  5366. else if (src1->IsNotInt())
  5367. {
  5368. isNotInt = true;
  5369. }
  5370. if (isInt)
  5371. {
  5372. instrLoad->m_opcode = Js::OpCode::ASR;
  5373. instrLoad->SetSrc2(IR::IntConstOpnd::New(Js::AtomTag, TyMachReg, this->m_func));
  5374. }
  5375. else
  5376. {
  5377. const ValueType src1ValueType(src1->GetValueType());
  5378. const bool doFloatToIntFastPath =
  5379. (src1ValueType.IsLikelyFloat() || src1ValueType.IsLikelyUntaggedInt()) &&
  5380. !(instrLoad->HasBailOutInfo() && (instrLoad->GetBailOutKind() == IR::BailOutIntOnly || instrLoad->GetBailOutKind() == IR::BailOutExpectingInteger));
  5381. if (isNotInt)
  5382. {
  5383. // Known to be non-integer. If we are required to bail out on helper call, just re-jit.
  5384. if (!doFloatToIntFastPath && bailOutOnHelper)
  5385. {
  5386. if(!GlobOpt::DoEliminateArrayAccessHelperCall(this->m_func))
  5387. {
  5388. // Array access helper call removal is already off for some reason. Prevent trying to rejit again
  5389. // because it won't help and the same thing will happen again. Just abort jitting this function.
  5390. if(PHASE_TRACE(Js::BailOutPhase, this->m_func))
  5391. {
  5392. Output::Print(_u(" Aborting JIT because EliminateArrayAccessHelperCall is already off\n"));
  5393. Output::Flush();
  5394. }
  5395. throw Js::OperationAbortedException();
  5396. }
  5397. throw Js::RejitException(RejitReason::ArrayAccessHelperCallEliminationDisabled);
  5398. }
  5399. }
  5400. else
  5401. {
  5402. // Could be an integer in this case.
  5403. if (!isInt)
  5404. {
  5405. if(doFloatToIntFastPath)
  5406. {
  5407. labelFloat = IR::LabelInstr::New(Js::OpCode::Label, instrLoad->m_func, false);
  5408. }
  5409. else
  5410. {
  5411. labelHelper = IR::LabelInstr::New(Js::OpCode::Label, instrLoad->m_func, true);
  5412. }
  5413. this->GenerateSmIntTest(src1, instrLoad, labelFloat ? labelFloat : labelHelper);
  5414. }
  5415. instr = IR::Instr::New(
  5416. Js::OpCode::ASRS, instrLoad->GetDst(), src1, IR::IntConstOpnd::New(Js::AtomTag, TyMachReg, this->m_func), this->m_func);
  5417. instrLoad->InsertBefore(instr);
  5418. labelDone = instrLoad->GetOrCreateContinueLabel();
  5419. instr = IR::BranchInstr::New(Js::OpCode::BCS, labelDone, this->m_func);
  5420. instrLoad->InsertBefore(instr);
  5421. }
  5422. if(doFloatToIntFastPath)
  5423. {
  5424. if(labelFloat)
  5425. {
  5426. instrLoad->InsertBefore(labelFloat);
  5427. }
  5428. if(!labelHelper)
  5429. {
  5430. labelHelper = IR::LabelInstr::New(Js::OpCode::Label, instrLoad->m_func, true);
  5431. }
  5432. if(!labelDone)
  5433. {
  5434. labelDone = instrLoad->GetOrCreateContinueLabel();
  5435. }
  5436. IR::RegOpnd *floatReg = IR::RegOpnd::New(TyFloat64, this->m_func);
  5437. this->LoadFloatValue(src1, floatReg, labelHelper, instrLoad, instrLoad->HasBailOutInfo());
  5438. this->ConvertFloatToInt32(instrLoad->GetDst(), floatReg, labelHelper, labelDone, instrLoad);
  5439. }
  5440. if(labelHelper)
  5441. {
  5442. instrLoad->InsertBefore(labelHelper);
  5443. }
  5444. if(instrLoad->HasBailOutInfo() && (instrLoad->GetBailOutKind() == IR::BailOutIntOnly || instrLoad->GetBailOutKind() == IR::BailOutExpectingInteger))
  5445. {
  5446. // Avoid bailout if we have a JavascriptNumber whose value is a signed 32-bit integer
  5447. m_lowerer->LoadInt32FromUntaggedVar(instrLoad);
  5448. // Need to bail out instead of calling a helper
  5449. return true;
  5450. }
  5451. if (bailOutOnHelper)
  5452. {
  5453. Assert(labelBailOut);
  5454. this->m_lowerer->InsertBranch(Js::OpCode::Br, labelBailOut, instrLoad);
  5455. instrLoad->Remove();
  5456. }
  5457. else if (conversionFromObjectAllowed)
  5458. {
  5459. this->m_lowerer->LowerUnaryHelperMem(instrLoad, IR::HelperConv_ToInt32);
  5460. }
  5461. else
  5462. {
  5463. this->m_lowerer->LowerUnaryHelperMemWithBoolReference(instrLoad, IR::HelperConv_ToInt32_NoObjects, true /*useBoolForBailout*/);
  5464. }
  5465. }
  5466. return false;
  5467. }
  5468. void
  5469. LowererMD::ImmedSrcToReg(IR::Instr * instr, IR::Opnd * newOpnd, int srcNum)
  5470. {
  5471. if (srcNum == 2)
  5472. {
  5473. instr->SetSrc2(newOpnd);
  5474. }
  5475. else
  5476. {
  5477. Assert(srcNum == 1);
  5478. instr->SetSrc1(newOpnd);
  5479. }
  5480. switch (instr->m_opcode)
  5481. {
  5482. case Js::OpCode::LDIMM:
  5483. instr->m_opcode = Js::OpCode::MOV;
  5484. break;
  5485. default:
  5486. // Nothing to do (unless we have immed/reg variations for other instructions).
  5487. break;
  5488. }
  5489. }
  5490. IR::LabelInstr *
  5491. LowererMD::GetBailOutStackRestoreLabel(BailOutInfo * bailOutInfo, IR::LabelInstr * exitTargetInstr)
  5492. {
  5493. return exitTargetInstr;
  5494. }
  5495. StackSym *
  5496. LowererMD::GetImplicitParamSlotSym(Js::ArgSlot argSlot)
  5497. {
  5498. return GetImplicitParamSlotSym(argSlot, this->m_func);
  5499. }
  5500. StackSym *
  5501. LowererMD::GetImplicitParamSlotSym(Js::ArgSlot argSlot, Func * func)
  5502. {
  5503. // For ARM, offset for implicit params always start at 0
  5504. // TODO: Consider not to use the argSlot number for the param slot sym, which can
  5505. // be confused with arg slot number from javascript
  5506. StackSym * stackSym = StackSym::NewParamSlotSym(argSlot, func);
  5507. func->SetArgOffset(stackSym, argSlot * MachPtr);
  5508. func->SetHasImplicitParamLoad();
  5509. return stackSym;
  5510. }
  5511. IR::LabelInstr *
  5512. LowererMD::EnsureEpilogLabel()
  5513. {
  5514. if (this->m_func->m_epilogLabel)
  5515. {
  5516. return this->m_func->m_epilogLabel;
  5517. }
  5518. IR::Instr *exitInstr = this->m_func->m_exitInstr;
  5519. IR::Instr *prevInstr = exitInstr->GetPrevRealInstrOrLabel();
  5520. if (prevInstr->IsLabelInstr())
  5521. {
  5522. this->m_func->m_epilogLabel = prevInstr->AsLabelInstr();
  5523. return prevInstr->AsLabelInstr();
  5524. }
  5525. IR::LabelInstr *labelInstr = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  5526. exitInstr->InsertBefore(labelInstr);
  5527. this->m_func->m_epilogLabel = labelInstr;
  5528. return labelInstr;
  5529. }
  5530. // Helper method: inserts legalized assign for given srcOpnd into RegD0 in front of given instr in the following way:
  5531. // dstReg = InsertMove srcOpnd
  5532. // Used to put args of inline built-in call into RegD0 and RegD1 before we call actual CRT function.
  5533. void LowererMD::GenerateAssignForBuiltinArg(RegNum dstReg, IR::Opnd* srcOpnd, IR::Instr* instr)
  5534. {
  5535. IR::RegOpnd* tempDst = IR::RegOpnd::New(nullptr, dstReg, TyMachDouble, this->m_func);
  5536. tempDst->m_isCallArg = true; // This is to make sure that lifetime of opnd is virtually extended until next CALL instr.
  5537. Lowerer::InsertMove(tempDst, srcOpnd, instr);
  5538. }
  5539. // For given InlineMathXXX instr, generate the call to actual CRT function/CPU instr.
  5540. void LowererMD::GenerateFastInlineBuiltInCall(IR::Instr* instr, IR::JnHelperMethod helperMethod)
  5541. {
  5542. switch (instr->m_opcode)
  5543. {
  5544. case Js::OpCode::InlineMathSqrt:
  5545. // Sqrt maps directly to the VFP instruction.
  5546. // src and dst are already float, all we need is just change the opcode and legalize.
  5547. // Before:
  5548. // dst = InlineMathSqrt src1
  5549. // After:
  5550. // <potential VSTR by legalizer if src1 is not a register>
  5551. // dst = VSQRT src1
  5552. Assert(helperMethod == (IR::JnHelperMethod)0);
  5553. Assert(instr->GetSrc2() == nullptr);
  5554. instr->m_opcode = Js::OpCode::VSQRT;
  5555. LegalizeMD::LegalizeInstr(instr);
  5556. break;
  5557. case Js::OpCode::InlineMathAbs:
  5558. Assert(helperMethod == (IR::JnHelperMethod)0);
  5559. return GenerateFastInlineBuiltInMathAbs(instr);
  5560. case Js::OpCode::InlineMathFloor:
  5561. Assert(helperMethod == (IR::JnHelperMethod)0);
  5562. return GenerateFastInlineBuiltInMathFloor(instr);
  5563. case Js::OpCode::InlineMathCeil:
  5564. Assert(helperMethod == (IR::JnHelperMethod)0);
  5565. return GenerateFastInlineBuiltInMathCeil(instr);
  5566. case Js::OpCode::InlineMathRound:
  5567. Assert(helperMethod == (IR::JnHelperMethod)0);
  5568. return GenerateFastInlineBuiltInMathRound(instr);
  5569. case Js::OpCode::InlineMathMin:
  5570. case Js::OpCode::InlineMathMax:
  5571. {
  5572. IR::Opnd* src1 = instr->GetSrc1();
  5573. IR::Opnd* src2 = instr->GetSrc2();
  5574. IR::Opnd* dst = instr->GetDst();
  5575. IR::LabelInstr* doneLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  5576. IR::LabelInstr* labelNaNHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  5577. IR::LabelInstr* labelNegZeroCheckHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  5578. IR::Instr* branchInstr;
  5579. bool min = instr->m_opcode == Js::OpCode::InlineMathMin ? true : false;
  5580. bool dstEqualsSrc1 = dst->IsEqual(src1);
  5581. bool dstEqualsSrc2 = dst->IsEqual(src2);
  5582. IR::Opnd * otherSrc = src2;
  5583. IR::Opnd * compareSrc1 = src1;
  5584. IR::Opnd * compareSrc2 = src2;
  5585. if (dstEqualsSrc2)
  5586. {
  5587. otherSrc = src1;
  5588. compareSrc1 = src2;
  5589. compareSrc2 = src1;
  5590. }
  5591. if (!dstEqualsSrc1 && !dstEqualsSrc2)
  5592. {
  5593. //(V)MOV dst, src1;
  5594. this->m_lowerer->InsertMove(dst, src1, instr);
  5595. }
  5596. if(dst->IsInt32())
  5597. {
  5598. // CMP src1, src2
  5599. if(min)
  5600. {
  5601. // BLT $continueLabel
  5602. branchInstr = IR::BranchInstr::New(Js::OpCode::BrLt_I4, doneLabel, compareSrc1, compareSrc2, instr->m_func);
  5603. instr->InsertBefore(branchInstr);
  5604. this->EmitInt4Instr(branchInstr);
  5605. }
  5606. else
  5607. {
  5608. // BGT $continueLabel
  5609. branchInstr = IR::BranchInstr::New(Js::OpCode::BrGt_I4, doneLabel, compareSrc1, compareSrc2, instr->m_func);
  5610. instr->InsertBefore(branchInstr);
  5611. this->EmitInt4Instr(branchInstr);
  5612. }
  5613. // MOV dst, src2
  5614. this->m_lowerer->InsertMove(dst, otherSrc, instr);
  5615. }
  5616. else if(dst->IsFloat64())
  5617. {
  5618. // VCMPF64 src1, src2
  5619. // BCC (min)/ BGT (max) $doneLabel
  5620. // BVS $labelNaNHelper
  5621. // BEQ $labelNegZeroCheckHelper
  5622. // VMOV dst, src2
  5623. // B $doneLabel
  5624. //
  5625. // $labelNegZeroCheckHelper
  5626. // if(min)
  5627. // {
  5628. // if(src2 == -0.0)
  5629. // VMOV dst, src2
  5630. // }
  5631. // else
  5632. // {
  5633. // if(src1 == -0.0)
  5634. // VMOV dst, src2
  5635. // }
  5636. // B $doneLabel
  5637. //
  5638. // $labelNaNHelper
  5639. // VMOV dst, NaN
  5640. //
  5641. // $doneLabel
  5642. if(min)
  5643. {
  5644. this->m_lowerer->InsertCompareBranch(compareSrc1, compareSrc2, Js::OpCode::BrLt_A, doneLabel, instr); // Lowering of BrLt_A for floats is done to JA with operands swapped
  5645. }
  5646. else
  5647. {
  5648. this->m_lowerer->InsertCompareBranch(compareSrc1, compareSrc2, Js::OpCode::BrGt_A, doneLabel, instr);
  5649. }
  5650. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::BVS, labelNaNHelper, instr->m_func));
  5651. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::BEQ, labelNegZeroCheckHelper, instr->m_func));
  5652. this->m_lowerer->InsertMove(dst, otherSrc, instr);
  5653. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::B, doneLabel, instr->m_func));
  5654. instr->InsertBefore(labelNegZeroCheckHelper);
  5655. IR::Opnd* isNegZero;
  5656. if(min)
  5657. {
  5658. isNegZero = IsOpndNegZero(compareSrc2, instr);
  5659. }
  5660. else
  5661. {
  5662. isNegZero = IsOpndNegZero(compareSrc1, instr);
  5663. }
  5664. this->m_lowerer->InsertCompareBranch(isNegZero, IR::IntConstOpnd::New(0x00000000, IRType::TyInt32, this->m_func), Js::OpCode::BrEq_A, doneLabel, instr);
  5665. this->m_lowerer->InsertMove(dst, otherSrc, instr);
  5666. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::B, doneLabel, instr->m_func));
  5667. instr->InsertBefore(labelNaNHelper);
  5668. IR::Opnd * opndNaN = IR::MemRefOpnd::New(m_func->GetThreadContextInfo()->GetDoubleNaNAddr(), IRType::TyFloat64, this->m_func,
  5669. IR::AddrOpndKindDynamicDoubleRef);
  5670. this->m_lowerer->InsertMove(dst, opndNaN, instr);
  5671. }
  5672. instr->InsertBefore(doneLabel);
  5673. instr->Remove();
  5674. break;
  5675. }
  5676. default:
  5677. // Before:
  5678. // dst = <Built-in call> src1, src2
  5679. // After:
  5680. // d0 = InsertMove src1
  5681. // lr = MOV helperAddr
  5682. // BLX lr
  5683. // dst = InsertMove call->dst (d0)
  5684. // Src1
  5685. AssertMsg(instr->GetDst()->IsFloat(), "Currently accepting only float args for math helpers -- dst.");
  5686. AssertMsg(instr->GetSrc1()->IsFloat(), "Currently accepting only float args for math helpers -- src1.");
  5687. AssertMsg(!instr->GetSrc2() || instr->GetSrc2()->IsFloat(), "Currently accepting only float args for math helpers -- src2.");
  5688. this->GenerateAssignForBuiltinArg((RegNum)FIRST_FLOAT_REG, instr->UnlinkSrc1(), instr);
  5689. // Src2
  5690. if (instr->GetSrc2() != nullptr)
  5691. {
  5692. this->GenerateAssignForBuiltinArg((RegNum)(FIRST_FLOAT_REG + 1), instr->UnlinkSrc2(), instr);
  5693. }
  5694. // Call CRT.
  5695. IR::RegOpnd* floatCallDst = IR::RegOpnd::New(nullptr, (RegNum)(FIRST_FLOAT_REG), TyMachDouble, this->m_func); // Dst in d0.
  5696. IR::Instr* floatCall = IR::Instr::New(Js::OpCode::BLX, floatCallDst, this->m_func);
  5697. instr->InsertBefore(floatCall);
  5698. // lr = MOV helperAddr
  5699. // BLX lr
  5700. IR::AddrOpnd* targetAddr = IR::AddrOpnd::New((Js::Var)IR::GetMethodOriginalAddress(m_func->GetThreadContextInfo(), helperMethod), IR::AddrOpndKind::AddrOpndKindDynamicMisc, this->m_func);
  5701. IR::RegOpnd *targetOpnd = IR::RegOpnd::New(nullptr, RegLR, TyMachPtr, this->m_func);
  5702. IR::Instr *movInstr = IR::Instr::New(Js::OpCode::LDIMM, targetOpnd, targetAddr, this->m_func);
  5703. targetOpnd->m_isCallArg = true;
  5704. floatCall->SetSrc1(targetOpnd);
  5705. floatCall->InsertBefore(movInstr);
  5706. // Save the result.
  5707. Lowerer::InsertMove(instr->UnlinkDst(), floatCall->GetDst(), instr);
  5708. instr->Remove();
  5709. break;
  5710. }
  5711. }
  5712. void
  5713. LowererMD::GenerateFastInlineBuiltInMathAbs(IR::Instr *inlineInstr)
  5714. {
  5715. IR::Opnd* src = inlineInstr->GetSrc1()->Copy(this->m_func);
  5716. IR::Opnd* dst = inlineInstr->UnlinkDst();
  5717. Assert(src);
  5718. IR::Instr* tmpInstr;
  5719. IRType srcType = src->GetType();
  5720. IR::Instr* nextInstr = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  5721. IR::Instr* continueInstr = m_lowerer->LowerBailOnIntMin(inlineInstr);
  5722. continueInstr->InsertAfter(nextInstr);
  5723. if (srcType == IRType::TyInt32)
  5724. {
  5725. // Note: if execution gets so far, we always get (untagged) int32 here.
  5726. // Since -x = ~x + 1, abs(x) = x, abs(-x) = -x, sign-extend(x) = 0, sign_extend(-x) = -1, where 0 <= x.
  5727. // Then: abs(x) = sign-extend(x) XOR x - sign-extend(x)
  5728. // Expected input (otherwise bailout):
  5729. // - src1 is (untagged) int, not equal to int_min (abs(int_min) would produce overflow, as there's no corresponding positive int).
  5730. Assert(src->IsRegOpnd());
  5731. // tmpDst = EOR src, src ASR #31
  5732. IR::RegOpnd *tmpDst = IR::RegOpnd::New(TyMachReg, this->m_func);
  5733. tmpInstr = IR::Instr::New(Js::OpCode::CLRSIGN, tmpDst, src, this->m_func);
  5734. nextInstr->InsertBefore(tmpInstr);
  5735. // tmpDst = SUB tmpDst, src ASR #31
  5736. tmpInstr = IR::Instr::New(Js::OpCode::SBCMPLNT, tmpDst, tmpDst, src, this->m_func);
  5737. nextInstr->InsertBefore(tmpInstr);
  5738. // MOV dst, tmpDst
  5739. tmpInstr = IR::Instr::New(Js::OpCode::MOV, dst, tmpDst, this->m_func);
  5740. nextInstr->InsertBefore(tmpInstr);
  5741. }
  5742. else if (srcType == IRType::TyFloat64)
  5743. {
  5744. // VABS dst, src
  5745. tmpInstr = IR::Instr::New(Js::OpCode::VABS, dst, src, this->m_func);
  5746. nextInstr->InsertBefore(tmpInstr);
  5747. }
  5748. else
  5749. {
  5750. AssertMsg(FALSE, "GenerateFastInlineBuiltInMathAbs: unexpected type of the src!");
  5751. }
  5752. }
  5753. void
  5754. LowererMD::GenerateFastInlineBuiltInMathFloor(IR::Instr* instr)
  5755. {
  5756. Assert(instr->GetDst()->IsInt32());
  5757. IR::LabelInstr * checkNegZeroLabelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  5758. IR::LabelInstr * checkOverflowLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  5759. IR::LabelInstr * doneLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  5760. // VMOV floatOpnd, src
  5761. IR::Opnd * src = instr->UnlinkSrc1();
  5762. IR::RegOpnd* floatOpnd = IR::RegOpnd::New(TyFloat64, this->m_func);
  5763. this->m_lowerer->InsertMove(floatOpnd, src, instr);
  5764. IR::LabelInstr * bailoutLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, /*helperLabel*/true);;
  5765. bool sharedBailout = (instr->GetBailOutInfo()->bailOutInstr != instr) ? true : false;
  5766. // NaN check
  5767. IR::Instr *instrCmp = IR::Instr::New(Js::OpCode::VCMPF64, this->m_func);
  5768. instrCmp->SetSrc1(floatOpnd);
  5769. instrCmp->SetSrc2(floatOpnd);
  5770. instr->InsertBefore(instrCmp);
  5771. LegalizeMD::LegalizeInstr(instrCmp);
  5772. // VMRS APSR, FPSCR
  5773. // BVS $bailoutLabel
  5774. instr->InsertBefore(IR::Instr::New(Js::OpCode::VMRS, this->m_func));
  5775. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::BVS, bailoutLabel, this->m_func));
  5776. IR::Opnd * zeroReg = IR::RegOpnd::New(TyFloat64, this->m_func);
  5777. this->LoadFloatZero(zeroReg, instr);
  5778. // VMRS Rorig, FPSCR
  5779. // VMRS Rt, FPSCR
  5780. // BIC Rt, Rt, 0x400000
  5781. // ORR Rt, Rt, 0x800000
  5782. // VMSR FPSCR, Rt
  5783. IR::Opnd* regOrig = IR::RegOpnd::New(TyInt32, this->m_func);
  5784. IR::Opnd* reg = IR::RegOpnd::New(TyInt32, this->m_func);
  5785. instr->InsertBefore(
  5786. IR::Instr::New(Js::OpCode::VMRSR, regOrig, instr->m_func));
  5787. instr->InsertBefore(
  5788. IR::Instr::New(Js::OpCode::VMRSR, reg, instr->m_func));
  5789. instr->InsertBefore(
  5790. IR::Instr::New(Js::OpCode::BIC, reg, reg, IR::IntConstOpnd::New(0x400000, IRType::TyInt32, this->m_func), instr->m_func));
  5791. instr->InsertBefore(
  5792. IR::Instr::New(Js::OpCode::ORR, reg, reg, IR::IntConstOpnd::New(0x800000, IRType::TyInt32, this->m_func), instr->m_func));
  5793. IR::Instr* setFPSCRInstr = IR::Instr::New(Js::OpCode::VMSR, instr->m_func);
  5794. setFPSCRInstr->SetSrc1(reg);
  5795. instr->InsertBefore(setFPSCRInstr);
  5796. // VCVTRS32F64 floatreg, floatOpnd
  5797. IR::RegOpnd *floatReg = IR::RegOpnd::New(TyFloat32, this->m_func);
  5798. IR::Opnd * intOpnd = IR::RegOpnd::New(TyInt32, this->m_func);
  5799. instr->InsertBefore(
  5800. IR::Instr::New(Js::OpCode::VCVTRS32F64, floatReg, floatOpnd, instr->m_func));
  5801. // VMOVARMVFP intOpnd, floatReg
  5802. instr->InsertBefore(IR::Instr::New(Js::OpCode::VMOVARMVFP, intOpnd, floatReg, this->m_func));
  5803. // VMSR FPSCR, Rorig
  5804. IR::Instr* restoreFPSCRInstr = IR::Instr::New(Js::OpCode::VMSR, instr->m_func);
  5805. restoreFPSCRInstr->SetSrc1(regOrig);
  5806. instr->InsertBefore(restoreFPSCRInstr);
  5807. //negZero bailout
  5808. // TST intOpnd, intOpnd
  5809. // BNE checkOverflowLabel
  5810. this->m_lowerer->InsertTestBranch(intOpnd, intOpnd, Js::OpCode::BNE, checkOverflowLabel, instr);
  5811. instr->InsertBefore(checkNegZeroLabelHelper);
  5812. if(instr->ShouldCheckForNegativeZero())
  5813. {
  5814. IR::Opnd * isNegZero = IR::RegOpnd::New(TyInt32, this->m_func);
  5815. isNegZero = this->IsOpndNegZero(src, instr);
  5816. this->m_lowerer->InsertCompareBranch(isNegZero, IR::IntConstOpnd::New(0x00000000, IRType::TyInt32, this->m_func), Js::OpCode::BrNeq_A, bailoutLabel, instr);
  5817. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::B, doneLabel, instr->m_func));
  5818. }
  5819. instr->InsertBefore(checkOverflowLabel);
  5820. CheckOverflowOnFloatToInt32(instr, intOpnd, bailoutLabel, doneLabel);
  5821. IR::Opnd * dst = instr->UnlinkDst();
  5822. instr->InsertAfter(doneLabel);
  5823. if(!sharedBailout)
  5824. {
  5825. instr->InsertBefore(bailoutLabel);
  5826. }
  5827. // In case of a shared bailout, we should jump to the code that sets some data on the bailout record which is specific
  5828. // to this bailout. Pass the bailoutLabel to GenerateFunction so that it may use the label as the collectRuntimeStatsLabel.
  5829. this->m_lowerer->GenerateBailOut(instr, nullptr, nullptr, sharedBailout ? bailoutLabel : nullptr);
  5830. // MOV dst, intOpnd
  5831. IR::Instr* movInstr = IR::Instr::New(Js::OpCode::MOV, dst, intOpnd, this->m_func);
  5832. doneLabel->InsertAfter(movInstr);
  5833. }
  5834. void
  5835. LowererMD::GenerateFastInlineBuiltInMathCeil(IR::Instr* instr)
  5836. {
  5837. Assert(instr->GetDst()->IsInt32());
  5838. IR::LabelInstr * checkNegZeroLabelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  5839. IR::LabelInstr * checkOverflowLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  5840. IR::LabelInstr * doneLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  5841. // VMOV floatOpnd, src
  5842. IR::Opnd * src = instr->UnlinkSrc1();
  5843. IR::RegOpnd* floatOpnd = IR::RegOpnd::New(TyFloat64, this->m_func);
  5844. this->m_lowerer->InsertMove(floatOpnd, src, instr);
  5845. IR::LabelInstr * bailoutLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, /*helperLabel*/true);;
  5846. bool sharedBailout = (instr->GetBailOutInfo()->bailOutInstr != instr) ? true : false;
  5847. // NaN check
  5848. IR::Instr *instrCmp = IR::Instr::New(Js::OpCode::VCMPF64, this->m_func);
  5849. instrCmp->SetSrc1(floatOpnd);
  5850. instrCmp->SetSrc2(floatOpnd);
  5851. instr->InsertBefore(instrCmp);
  5852. LegalizeMD::LegalizeInstr(instrCmp);
  5853. // VMRS APSR, FPSCR
  5854. // BVS $bailoutLabel
  5855. instr->InsertBefore(IR::Instr::New(Js::OpCode::VMRS, this->m_func));
  5856. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::BVS, bailoutLabel, this->m_func));
  5857. IR::Opnd * zeroReg = IR::RegOpnd::New(TyFloat64, this->m_func);
  5858. this->LoadFloatZero(zeroReg, instr);
  5859. // VMRS Rorig, FPSCR
  5860. // VMRS Rt, FPSCR
  5861. // BIC Rt, Rt, 0x800000
  5862. // ORR Rt, Rt, 0x400000
  5863. // VMSR FPSCR, Rt
  5864. IR::Opnd* regOrig = IR::RegOpnd::New(TyInt32, this->m_func);
  5865. IR::Opnd* reg = IR::RegOpnd::New(TyInt32, this->m_func);
  5866. instr->InsertBefore(
  5867. IR::Instr::New(Js::OpCode::VMRSR, regOrig, instr->m_func));
  5868. instr->InsertBefore(
  5869. IR::Instr::New(Js::OpCode::VMRSR, reg, instr->m_func));
  5870. instr->InsertBefore(
  5871. IR::Instr::New(Js::OpCode::BIC, reg, reg, IR::IntConstOpnd::New(0x800000, IRType::TyInt32, this->m_func), instr->m_func));
  5872. instr->InsertBefore(
  5873. IR::Instr::New(Js::OpCode::ORR, reg, reg, IR::IntConstOpnd::New(0x400000, IRType::TyInt32, this->m_func), instr->m_func));
  5874. IR::Instr* setFPSCRInstr = IR::Instr::New(Js::OpCode::VMSR, instr->m_func);
  5875. setFPSCRInstr->SetSrc1(reg);
  5876. instr->InsertBefore(setFPSCRInstr);
  5877. // VCVTRS32F64 floatreg, floatOpnd
  5878. IR::RegOpnd *floatReg = IR::RegOpnd::New(TyFloat32, this->m_func);
  5879. IR::Opnd * intOpnd = IR::RegOpnd::New(TyInt32, this->m_func);
  5880. instr->InsertBefore(
  5881. IR::Instr::New(Js::OpCode::VCVTRS32F64, floatReg, floatOpnd, instr->m_func));
  5882. // VMOVARMVFP intOpnd, floatReg
  5883. instr->InsertBefore(IR::Instr::New(Js::OpCode::VMOVARMVFP, intOpnd, floatReg, this->m_func));
  5884. // VMSR FPSCR, Rorig
  5885. IR::Instr* restoreFPSCRInstr = IR::Instr::New(Js::OpCode::VMSR, instr->m_func);
  5886. restoreFPSCRInstr->SetSrc1(regOrig);
  5887. instr->InsertBefore(restoreFPSCRInstr);
  5888. //negZero bailout
  5889. // TST intOpnd, intOpnd
  5890. // BNE checkOverflowLabel
  5891. this->m_lowerer->InsertTestBranch(intOpnd, intOpnd, Js::OpCode::BNE, checkOverflowLabel, instr);
  5892. instr->InsertBefore(checkNegZeroLabelHelper);
  5893. if(instr->ShouldCheckForNegativeZero())
  5894. {
  5895. IR::Opnd * isNegZero = IR::RegOpnd::New(TyInt32, this->m_func);
  5896. IR::Opnd * negOne = IR::MemRefOpnd::New(m_func->GetThreadContextInfo()->GetDoubleNegOneAddr(), IRType::TyFloat64, this->m_func, IR::AddrOpndKindDynamicDoubleRef);
  5897. this->m_lowerer->InsertCompareBranch(floatOpnd, negOne, Js::OpCode::BrNotGe_A, doneLabel, instr);
  5898. this->m_lowerer->InsertCompareBranch(floatOpnd, zeroReg, Js::OpCode::BrNotGe_A, bailoutLabel, instr);
  5899. isNegZero = this->IsOpndNegZero(src, instr);
  5900. this->m_lowerer->InsertCompareBranch(isNegZero, IR::IntConstOpnd::New(0x00000000, IRType::TyInt32, this->m_func), Js::OpCode::BrNeq_A, bailoutLabel, instr);
  5901. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::B, doneLabel, instr->m_func));
  5902. }
  5903. instr->InsertBefore(checkOverflowLabel);
  5904. CheckOverflowOnFloatToInt32(instr, intOpnd, bailoutLabel, doneLabel);
  5905. IR::Opnd * dst = instr->UnlinkDst();
  5906. instr->InsertAfter(doneLabel);
  5907. if(!sharedBailout)
  5908. {
  5909. instr->InsertBefore(bailoutLabel);
  5910. }
  5911. // In case of a shared bailout, we should jump to the code that sets some data on the bailout record which is specific
  5912. // to this bailout. Pass the bailoutLabel to GenerateFunction so that it may use the label as the collectRuntimeStatsLabel.
  5913. this->m_lowerer->GenerateBailOut(instr, nullptr, nullptr, sharedBailout ? bailoutLabel : nullptr);
  5914. // MOV dst, intOpnd
  5915. IR::Instr* movInstr = IR::Instr::New(Js::OpCode::MOV, dst, intOpnd, this->m_func);
  5916. doneLabel->InsertAfter(movInstr);
  5917. }
  5918. void
  5919. LowererMD::GenerateFastInlineBuiltInMathRound(IR::Instr* instr)
  5920. {
  5921. Assert(instr->GetDst()->IsInt32());
  5922. IR::LabelInstr * checkNegZeroLabelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  5923. IR::LabelInstr * checkOverflowLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  5924. IR::LabelInstr * doneLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  5925. // VMOV floatOpnd, src
  5926. IR::Opnd * src = instr->UnlinkSrc1();
  5927. IR::RegOpnd* floatOpnd = IR::RegOpnd::New(TyFloat64, this->m_func);
  5928. this->m_lowerer->InsertMove(floatOpnd, src, instr);
  5929. IR::LabelInstr * bailoutLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, /*helperLabel*/true);;
  5930. bool sharedBailout = (instr->GetBailOutInfo()->bailOutInstr != instr) ? true : false;
  5931. // NaN check
  5932. IR::Instr *instrCmp = IR::Instr::New(Js::OpCode::VCMPF64, this->m_func);
  5933. instrCmp->SetSrc1(floatOpnd);
  5934. instrCmp->SetSrc2(floatOpnd);
  5935. instr->InsertBefore(instrCmp);
  5936. LegalizeMD::LegalizeInstr(instrCmp);
  5937. // VMRS APSR, FPSCR
  5938. // BVS $bailoutLabel
  5939. instr->InsertBefore(IR::Instr::New(Js::OpCode::VMRS, this->m_func));
  5940. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::BVS, bailoutLabel, this->m_func));
  5941. IR::Opnd * zeroReg = IR::RegOpnd::New(TyFloat64, this->m_func);
  5942. this->LoadFloatZero(zeroReg, instr);
  5943. // Add 0.5
  5944. IR::Opnd * pointFive = IR::MemRefOpnd::New(m_func->GetThreadContextInfo()->GetDoublePointFiveAddr(), IRType::TyFloat64, this->m_func, IR::AddrOpndKindDynamicDoubleRef);
  5945. this->m_lowerer->InsertAdd(false, floatOpnd, floatOpnd, pointFive, instr);
  5946. // VMRS Rorig, FPSCR
  5947. // VMRS Rt, FPSCR
  5948. // BIC Rt, Rt, 0x400000
  5949. // ORR Rt, Rt, 0x800000
  5950. // VMSR FPSCR, Rt
  5951. IR::Opnd* regOrig = IR::RegOpnd::New(TyInt32, this->m_func);
  5952. IR::Opnd* reg = IR::RegOpnd::New(TyInt32, this->m_func);
  5953. instr->InsertBefore(
  5954. IR::Instr::New(Js::OpCode::VMRSR, regOrig, instr->m_func));
  5955. instr->InsertBefore(
  5956. IR::Instr::New(Js::OpCode::VMRSR, reg, instr->m_func));
  5957. instr->InsertBefore(
  5958. IR::Instr::New(Js::OpCode::BIC, reg, reg, IR::IntConstOpnd::New(0x400000, IRType::TyInt32, this->m_func), instr->m_func));
  5959. instr->InsertBefore(
  5960. IR::Instr::New(Js::OpCode::ORR, reg, reg, IR::IntConstOpnd::New(0x800000, IRType::TyInt32, this->m_func), instr->m_func));
  5961. IR::Instr* setFPSCRInstr = IR::Instr::New(Js::OpCode::VMSR, instr->m_func);
  5962. setFPSCRInstr->SetSrc1(reg);
  5963. instr->InsertBefore(setFPSCRInstr);
  5964. // VCVTRS32F64 floatreg, floatOpnd
  5965. IR::RegOpnd *floatReg = IR::RegOpnd::New(TyFloat32, this->m_func);
  5966. IR::Opnd * intOpnd = IR::RegOpnd::New(TyInt32, this->m_func);
  5967. instr->InsertBefore(
  5968. IR::Instr::New(Js::OpCode::VCVTRS32F64, floatReg, floatOpnd, instr->m_func));
  5969. // VMOVARMVFP intOpnd, floatReg
  5970. instr->InsertBefore(IR::Instr::New(Js::OpCode::VMOVARMVFP, intOpnd, floatReg, this->m_func));
  5971. // VMSR FPSCR, Rorig
  5972. IR::Instr* restoreFPSCRInstr = IR::Instr::New(Js::OpCode::VMSR, instr->m_func);
  5973. restoreFPSCRInstr->SetSrc1(regOrig);
  5974. instr->InsertBefore(restoreFPSCRInstr);
  5975. //negZero bailout
  5976. // TST intOpnd, intOpnd
  5977. // BNE checkOverflowLabel
  5978. this->m_lowerer->InsertTestBranch(intOpnd, intOpnd, Js::OpCode::BNE, checkOverflowLabel, instr);
  5979. instr->InsertBefore(checkNegZeroLabelHelper);
  5980. if(instr->ShouldCheckForNegativeZero())
  5981. {
  5982. IR::Opnd * isNegZero = IR::RegOpnd::New(TyInt32, this->m_func);
  5983. IR::Opnd * negPointFive = IR::MemRefOpnd::New(m_func->GetThreadContextInfo()->GetDoubleNegPointFiveAddr(), IRType::TyFloat64, this->m_func, IR::AddrOpndKindDynamicDoubleRef);
  5984. this->m_lowerer->InsertCompareBranch(src, negPointFive, Js::OpCode::BrNotGe_A, doneLabel, instr);
  5985. this->m_lowerer->InsertCompareBranch(src, zeroReg, Js::OpCode::BrNotGe_A, bailoutLabel, instr);
  5986. isNegZero = this->IsOpndNegZero(src, instr);
  5987. this->m_lowerer->InsertCompareBranch(isNegZero, IR::IntConstOpnd::New(0x00000000, IRType::TyInt32, this->m_func), Js::OpCode::BrNeq_A, bailoutLabel, instr);
  5988. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::B, doneLabel, instr->m_func));
  5989. }
  5990. instr->InsertBefore(checkOverflowLabel);
  5991. CheckOverflowOnFloatToInt32(instr, intOpnd, bailoutLabel, doneLabel);
  5992. IR::Opnd * dst = instr->UnlinkDst();
  5993. instr->InsertAfter(doneLabel);
  5994. if(!sharedBailout)
  5995. {
  5996. instr->InsertBefore(bailoutLabel);
  5997. }
  5998. // In case of a shared bailout, we should jump to the code that sets some data on the bailout record which is specific
  5999. // to this bailout. Pass the bailoutLabel to GenerateFunction so that it may use the label as the collectRuntimeStatsLabel.
  6000. this->m_lowerer->GenerateBailOut(instr, nullptr, nullptr, sharedBailout ? bailoutLabel : nullptr);
  6001. // MOV dst, intOpnd
  6002. IR::Instr* movInstr = IR::Instr::New(Js::OpCode::MOV, dst, intOpnd, this->m_func);
  6003. doneLabel->InsertAfter(movInstr);
  6004. }
  6005. IR::Opnd* LowererMD::IsOpndNegZero(IR::Opnd* opnd, IR::Instr* instr)
  6006. {
  6007. IR::Opnd * isNegZero = IR::RegOpnd::New(TyInt32, this->m_func);
  6008. LoadDoubleHelperArgument(instr, opnd);
  6009. IR::Instr * helperCallInstr = IR::Instr::New(Js::OpCode::Call, isNegZero, this->m_func);
  6010. instr->InsertBefore(helperCallInstr);
  6011. this->ChangeToHelperCall(helperCallInstr, IR::HelperIsNegZero);
  6012. return isNegZero;
  6013. }
  6014. IR::Instr *
  6015. LowererMD::LowerToFloat(IR::Instr *instr)
  6016. {
  6017. switch (instr->m_opcode)
  6018. {
  6019. case Js::OpCode::Add_A:
  6020. instr->m_opcode = Js::OpCode::VADDF64;
  6021. break;
  6022. case Js::OpCode::Sub_A:
  6023. instr->m_opcode = Js::OpCode::VSUBF64;
  6024. break;
  6025. case Js::OpCode::Mul_A:
  6026. instr->m_opcode = Js::OpCode::VMULF64;
  6027. break;
  6028. case Js::OpCode::Div_A:
  6029. instr->m_opcode = Js::OpCode::VDIVF64;
  6030. break;
  6031. case Js::OpCode::Neg_A:
  6032. instr->m_opcode = Js::OpCode::VNEGF64;
  6033. break;
  6034. case Js::OpCode::BrEq_A:
  6035. case Js::OpCode::BrNeq_A:
  6036. case Js::OpCode::BrSrEq_A:
  6037. case Js::OpCode::BrSrNeq_A:
  6038. case Js::OpCode::BrGt_A:
  6039. case Js::OpCode::BrGe_A:
  6040. case Js::OpCode::BrLt_A:
  6041. case Js::OpCode::BrLe_A:
  6042. case Js::OpCode::BrNotEq_A:
  6043. case Js::OpCode::BrNotNeq_A:
  6044. case Js::OpCode::BrSrNotEq_A:
  6045. case Js::OpCode::BrSrNotNeq_A:
  6046. case Js::OpCode::BrNotGt_A:
  6047. case Js::OpCode::BrNotGe_A:
  6048. case Js::OpCode::BrNotLt_A:
  6049. case Js::OpCode::BrNotLe_A:
  6050. return this->LowerFloatCondBranch(instr->AsBranchInstr());
  6051. default:
  6052. Assume(UNREACHED);
  6053. }
  6054. LegalizeMD::LegalizeInstr(instr);
  6055. return instr;
  6056. }
  6057. IR::BranchInstr *
  6058. LowererMD::LowerFloatCondBranch(IR::BranchInstr *instrBranch, bool ignoreNaN)
  6059. {
  6060. IR::Instr *instr;
  6061. Js::OpCode brOpcode = Js::OpCode::InvalidOpCode;
  6062. bool addNaNCheck = false;
  6063. Func * func = instrBranch->m_func;
  6064. IR::Opnd *src1 = instrBranch->UnlinkSrc1();
  6065. IR::Opnd *src2 = instrBranch->UnlinkSrc2();
  6066. IR::Instr *instrCmp = IR::Instr::New(Js::OpCode::VCMPF64, func);
  6067. instrCmp->SetSrc1(src1);
  6068. instrCmp->SetSrc2(src2);
  6069. instrBranch->InsertBefore(instrCmp);
  6070. LegalizeMD::LegalizeInstr(instrCmp);
  6071. instrBranch->InsertBefore(IR::Instr::New(Js::OpCode::VMRS, func));
  6072. switch (instrBranch->m_opcode)
  6073. {
  6074. case Js::OpCode::BrSrEq_A:
  6075. case Js::OpCode::BrEq_A:
  6076. case Js::OpCode::BrNotNeq_A:
  6077. case Js::OpCode::BrSrNotNeq_A:
  6078. brOpcode = Js::OpCode::BEQ;
  6079. break;
  6080. case Js::OpCode::BrNeq_A:
  6081. case Js::OpCode::BrSrNeq_A:
  6082. case Js::OpCode::BrSrNotEq_A:
  6083. case Js::OpCode::BrNotEq_A:
  6084. brOpcode = Js::OpCode::BNE;
  6085. addNaNCheck = !ignoreNaN; //Special check for BNE as it is set when the operands are unordered (NaN).
  6086. break;
  6087. case Js::OpCode::BrLe_A:
  6088. brOpcode = Js::OpCode::BLS; //Can't use BLE as it is set when the operands are unordered (NaN).
  6089. break;
  6090. case Js::OpCode::BrLt_A:
  6091. brOpcode = Js::OpCode::BCC; //Can't use BLT as is set when the operands are unordered (NaN).
  6092. break;
  6093. case Js::OpCode::BrGe_A:
  6094. brOpcode = Js::OpCode::BGE;
  6095. break;
  6096. case Js::OpCode::BrGt_A:
  6097. brOpcode = Js::OpCode::BGT;
  6098. break;
  6099. case Js::OpCode::BrNotLe_A:
  6100. brOpcode = Js::OpCode::BHI;
  6101. break;
  6102. case Js::OpCode::BrNotLt_A:
  6103. brOpcode = Js::OpCode::BPL;
  6104. break;
  6105. case Js::OpCode::BrNotGe_A:
  6106. brOpcode = Js::OpCode::BLT;
  6107. break;
  6108. case Js::OpCode::BrNotGt_A:
  6109. brOpcode = Js::OpCode::BLE;
  6110. break;
  6111. default:
  6112. Assert(false);
  6113. break;
  6114. }
  6115. if (addNaNCheck)
  6116. {
  6117. instr = IR::BranchInstr::New(Js::OpCode::BVS, instrBranch->GetTarget(), func);
  6118. instrBranch->InsertBefore(instr);
  6119. }
  6120. instr = IR::BranchInstr::New(brOpcode, instrBranch->GetTarget(), func);
  6121. instrBranch->InsertBefore(instr);
  6122. instrBranch->Remove();
  6123. return instr->AsBranchInstr();
  6124. }
  6125. void
  6126. LowererMD::EmitIntToFloat(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsert)
  6127. {
  6128. IR::Instr *instr;
  6129. IR::RegOpnd *floatReg = IR::RegOpnd::New(TyFloat64, this->m_func);
  6130. Assert(dst->IsRegOpnd() && dst->IsFloat64());
  6131. Assert(src->IsRegOpnd() && src->IsInt32());
  6132. instr = IR::Instr::New(Js::OpCode::VMOVARMVFP, floatReg, src, this->m_func);
  6133. instrInsert->InsertBefore(instr);
  6134. // Convert to Float
  6135. instr = IR::Instr::New(Js::OpCode::VCVTF64S32, dst, floatReg, this->m_func);
  6136. instrInsert->InsertBefore(instr);
  6137. }
  6138. void
  6139. LowererMD::EmitUIntToFloat(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsert)
  6140. {
  6141. IR::Instr *instr;
  6142. IR::RegOpnd *floatReg = IR::RegOpnd::New(TyFloat64, this->m_func);
  6143. Assert(dst->IsRegOpnd() && dst->IsFloat64());
  6144. Assert(src->IsRegOpnd() && (src->IsInt32() || src->IsUInt32()));
  6145. instr = IR::Instr::New(Js::OpCode::VMOVARMVFP, floatReg, src, this->m_func);
  6146. instrInsert->InsertBefore(instr);
  6147. // Convert to Float
  6148. instr = IR::Instr::New(Js::OpCode::VCVTF64U32, dst, floatReg, this->m_func);
  6149. instrInsert->InsertBefore(instr);
  6150. }
  6151. void LowererMD::ConvertFloatToInt32(IR::Opnd* intOpnd, IR::Opnd* floatOpnd, IR::LabelInstr * labelHelper, IR::LabelInstr * labelDone, IR::Instr * instrInsert)
  6152. {
  6153. Assert(floatOpnd->IsFloat64());
  6154. Assert(intOpnd->IsInt32());
  6155. IR::RegOpnd *floatReg = IR::RegOpnd::New(TyFloat32, this->m_func);
  6156. // VCVTS32F64 dst.i32, src.f64
  6157. // Convert to int
  6158. IR::Instr * instr = IR::Instr::New(Js::OpCode::VCVTS32F64, floatReg, floatOpnd, this->m_func);
  6159. instrInsert->InsertBefore(instr);
  6160. Legalize(instr);
  6161. //Move to integer reg
  6162. instr = IR::Instr::New(Js::OpCode::VMOVARMVFP, intOpnd, floatReg, this->m_func);
  6163. instrInsert->InsertBefore(instr);
  6164. Legalize(instr);
  6165. this->CheckOverflowOnFloatToInt32(instrInsert, intOpnd, labelHelper, labelDone);
  6166. }
  6167. void
  6168. LowererMD::EmitIntToLong(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsert)
  6169. {
  6170. Assert(UNREACHED);
  6171. }
  6172. void
  6173. LowererMD::EmitUIntToLong(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsert)
  6174. {
  6175. Assert(UNREACHED);
  6176. }
  6177. void
  6178. LowererMD::EmitLongToInt(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsert)
  6179. {
  6180. Assert(UNREACHED);
  6181. }
  6182. void
  6183. LowererMD::CheckOverflowOnFloatToInt32(IR::Instr* instrInsert, IR::Opnd* intOpnd, IR::LabelInstr * labelHelper, IR::LabelInstr * labelDone)
  6184. {
  6185. // CMP intOpnd, 0x80000000 -- Check for overflow
  6186. IR::Instr* instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  6187. instr->SetSrc1(intOpnd);
  6188. instr->SetSrc2(IR::IntConstOpnd::New(0x80000000, TyInt32, this->m_func, true));
  6189. instrInsert->InsertBefore(instr);
  6190. LegalizeMD::LegalizeInstr(instr);
  6191. // BEQ $helper
  6192. instr = IR::BranchInstr::New(Js::OpCode::BEQ, labelHelper, this->m_func);
  6193. instrInsert->InsertBefore(instr);
  6194. // CMP intOpnd, 0x7fffffff -- Check for overflow
  6195. IR::RegOpnd *regOpnd= IR::RegOpnd::New(TyMachReg, this->m_func);
  6196. instr = IR::Instr::New(Js::OpCode::MVN,
  6197. regOpnd,
  6198. IR::IntConstOpnd::New(0x80000000, TyInt32, this->m_func, true),
  6199. this->m_func);
  6200. instrInsert->InsertBefore(instr);
  6201. LegalizeMD::LegalizeInstr(instr);
  6202. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  6203. instr->SetSrc1(intOpnd);
  6204. instr->SetSrc2(regOpnd);
  6205. instrInsert->InsertBefore(instr);
  6206. LegalizeMD::LegalizeInstr(instr);
  6207. // BNE $done
  6208. instr = IR::BranchInstr::New(Js::OpCode::BNE, labelDone, this->m_func);
  6209. instrInsert->InsertBefore(instr);
  6210. }
  6211. void
  6212. LowererMD::EmitFloatToInt(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsert, IR::Instr * instrBailOut, IR::LabelInstr * labelBailOut)
  6213. {
  6214. IR::BailOutKind bailOutKind = IR::BailOutInvalid;
  6215. if (instrBailOut && instrBailOut->HasBailOutInfo())
  6216. {
  6217. bailOutKind = instrBailOut->GetBailOutKind();
  6218. if (bailOutKind & IR::BailOutOnArrayAccessHelperCall)
  6219. {
  6220. // Bail out instead of calling helper. If this is happening unconditionally, the caller should instead throw a rejit exception.
  6221. Assert(labelBailOut);
  6222. m_lowerer->InsertBranch(Js::OpCode::Br, labelBailOut, instrInsert);
  6223. return;
  6224. }
  6225. }
  6226. IR::LabelInstr *labelDone = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  6227. IR::LabelInstr *labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  6228. IR::Instr *instr;
  6229. ConvertFloatToInt32(dst, src, labelHelper, labelDone, instrInsert);
  6230. // $Helper
  6231. instrInsert->InsertBefore(labelHelper);
  6232. instr = IR::Instr::New(Js::OpCode::Call, dst, this->m_func);
  6233. instrInsert->InsertBefore(instr);
  6234. if (BailOutInfo::IsBailOutOnImplicitCalls(bailOutKind))
  6235. {
  6236. _Analysis_assume_(instrBailOut != nullptr);
  6237. instr = instr->ConvertToBailOutInstr(instrBailOut->GetBailOutInfo(), bailOutKind);
  6238. if (instrBailOut->GetBailOutInfo()->bailOutInstr == instrBailOut)
  6239. {
  6240. IR::Instr * instrShare = instrBailOut->ShareBailOut();
  6241. m_lowerer->LowerBailTarget(instrShare);
  6242. }
  6243. }
  6244. // dst = ToInt32Core(src);
  6245. LoadDoubleHelperArgument(instr, src);
  6246. this->ChangeToHelperCall(instr, IR::HelperConv_ToInt32Core);
  6247. // $Done
  6248. instrInsert->InsertBefore(labelDone);
  6249. }
  6250. IR::Instr *
  6251. LowererMD::InsertConvertFloat64ToInt32(const RoundMode roundMode, IR::Opnd *const dst, IR::Opnd *const src, IR::Instr *const insertBeforeInstr)
  6252. {
  6253. Assert(dst);
  6254. Assert(dst->IsInt32());
  6255. Assert(src);
  6256. Assert(src->IsFloat64());
  6257. Assert(insertBeforeInstr);
  6258. // The caller is expected to check for overflow. To have that work be done automatically, use LowererMD::EmitFloatToInt.
  6259. Func *const func = insertBeforeInstr->m_func;
  6260. IR::AutoReuseOpnd autoReuseSrcPlusHalf;
  6261. IR::Instr *instr = nullptr;
  6262. switch (roundMode)
  6263. {
  6264. case RoundModeTowardInteger:
  6265. {
  6266. // Conversion with rounding towards nearest integer is not supported by the architecture. Add 0.5 and do a
  6267. // round-toward-zero conversion instead.
  6268. IR::RegOpnd *const srcPlusHalf = IR::RegOpnd::New(TyFloat64, func);
  6269. autoReuseSrcPlusHalf.Initialize(srcPlusHalf, func);
  6270. Lowerer::InsertAdd(
  6271. false /* needFlags */,
  6272. srcPlusHalf,
  6273. src,
  6274. IR::MemRefOpnd::New(insertBeforeInstr->m_func->GetThreadContextInfo()->GetDoublePointFiveAddr(), TyFloat64, func,
  6275. IR::AddrOpndKindDynamicDoubleRef),
  6276. insertBeforeInstr);
  6277. instr = IR::Instr::New(LowererMD::MDConvertFloat64ToInt32Opcode(RoundModeTowardZero), dst, srcPlusHalf, func);
  6278. insertBeforeInstr->InsertBefore(instr);
  6279. LowererMD::Legalize(instr);
  6280. return instr;
  6281. }
  6282. case RoundModeHalfToEven:
  6283. {
  6284. // On ARM we need to set the rounding mode bits of the FPSCR.
  6285. // These are bits 22 and 23 and we need them to be off for "Round to Nearest (RN) mode"
  6286. // After doing the convert (via VCVTRS32F64) we need to restore the original FPSCR state.
  6287. // VMRS Rorig, FPSCR
  6288. // VMRS Rt, FPSCR
  6289. // BIC Rt, Rt, 0xC00000
  6290. // VMSR FPSCR, Rt
  6291. IR::Opnd* regOrig = IR::RegOpnd::New(TyInt32, func);
  6292. IR::Opnd* reg = IR::RegOpnd::New(TyInt32, func);
  6293. insertBeforeInstr->InsertBefore(
  6294. IR::Instr::New(Js::OpCode::VMRSR, regOrig, func));
  6295. insertBeforeInstr->InsertBefore(
  6296. IR::Instr::New(Js::OpCode::VMRSR, reg, func));
  6297. insertBeforeInstr->InsertBefore(
  6298. IR::Instr::New(Js::OpCode::BIC, reg, reg, IR::IntConstOpnd::New(0xC00000, IRType::TyInt32, func), func));
  6299. IR::Instr* setFPSCRInstr = IR::Instr::New(Js::OpCode::VMSR, func);
  6300. setFPSCRInstr->SetSrc1(reg);
  6301. insertBeforeInstr->InsertBefore(setFPSCRInstr);
  6302. // VCVTRS32F64 floatreg, regSrc
  6303. IR::RegOpnd *floatReg = IR::RegOpnd::New(TyFloat32, func);
  6304. insertBeforeInstr->InsertBefore(
  6305. IR::Instr::New(LowererMD::MDConvertFloat64ToInt32Opcode(RoundModeHalfToEven), floatReg, src, func));
  6306. // VMOVARMVFP regOpnd, floatReg
  6307. insertBeforeInstr->InsertBefore(IR::Instr::New(Js::OpCode::VMOVARMVFP, dst, floatReg, func));
  6308. // VMSR FPSCR, Rorig
  6309. IR::Instr* restoreFPSCRInstr = IR::Instr::New(Js::OpCode::VMSR, func);
  6310. restoreFPSCRInstr->SetSrc1(regOrig);
  6311. insertBeforeInstr->InsertBefore(restoreFPSCRInstr);
  6312. return restoreFPSCRInstr;
  6313. }
  6314. default:
  6315. AssertMsg(0, "RoundMode not supported.");
  6316. return nullptr;
  6317. }
  6318. }
  6319. IR::Instr *
  6320. LowererMD::LoadFloatZero(IR::Opnd * opndDst, IR::Instr * instrInsert)
  6321. {
  6322. Assert(opndDst->GetType() == TyFloat64);
  6323. IR::Opnd * zero = IR::MemRefOpnd::New(instrInsert->m_func->GetThreadContextInfo()->GetDoubleZeroAddr(), TyFloat64, instrInsert->m_func, IR::AddrOpndKindDynamicDoubleRef);
  6324. return Lowerer::InsertMove(opndDst, zero, instrInsert);
  6325. }
  6326. IR::Instr *
  6327. LowererMD::LoadFloatValue(IR::Opnd * opndDst, double value, IR::Instr * instrInsert)
  6328. {
  6329. // Floating point zero is a common value to load. Let's use a single memory location instead of allocating new memory for each.
  6330. const bool isFloatZero = value == 0.0 && !Js::JavascriptNumber::IsNegZero(value); // (-0.0 == 0.0) yields true
  6331. if (isFloatZero)
  6332. {
  6333. return LowererMD::LoadFloatZero(opndDst, instrInsert);
  6334. }
  6335. void * pValue = NativeCodeDataNewNoFixup(instrInsert->m_func->GetNativeCodeDataAllocator(), DoubleType<DataDesc_LowererMD_LoadFloatValue_Double>, value);
  6336. IR::Opnd * opnd;
  6337. if (instrInsert->m_func->IsOOPJIT())
  6338. {
  6339. int offset = NativeCodeData::GetDataTotalOffset(pValue);
  6340. auto addressRegOpnd = IR::RegOpnd::New(TyMachPtr, instrInsert->m_func);
  6341. Lowerer::InsertMove(
  6342. addressRegOpnd,
  6343. IR::MemRefOpnd::New(instrInsert->m_func->GetWorkItem()->GetWorkItemData()->nativeDataAddr, TyMachPtr, instrInsert->m_func, IR::AddrOpndKindDynamicNativeCodeDataRef),
  6344. instrInsert);
  6345. opnd = IR::IndirOpnd::New(addressRegOpnd, offset, TyMachDouble,
  6346. #if DBG
  6347. NativeCodeData::GetDataDescription(pValue, instrInsert->m_func->m_alloc),
  6348. #endif
  6349. instrInsert->m_func, true);
  6350. }
  6351. else
  6352. {
  6353. opnd = IR::MemRefOpnd::New((void*)pValue, TyMachDouble, instrInsert->m_func);
  6354. }
  6355. IR::Instr * instr = IR::Instr::New(Js::OpCode::VLDR, opndDst, opnd, instrInsert->m_func);
  6356. instrInsert->InsertBefore(instr);
  6357. LegalizeMD::LegalizeInstr(instr);
  6358. return instr;
  6359. }
  6360. void LowererMD::GenerateFloatTest(IR::RegOpnd * opndSrc, IR::Instr * insertInstr, IR::LabelInstr* labelHelper, const bool checkForNullInLoopBody)
  6361. {
  6362. if (opndSrc->GetValueType().IsFloat())
  6363. {
  6364. return;
  6365. }
  6366. if(checkForNullInLoopBody && m_func->IsLoopBody())
  6367. {
  6368. // It's possible that the value was determined dead by the jitted function and was not restored. The jitted loop
  6369. // body may not realize that it's dead and may try to use it. Check for null in loop bodies.
  6370. // test src1, src1
  6371. // jz $helper (bail out)
  6372. m_lowerer->InsertCompareBranch(
  6373. opndSrc,
  6374. IR::AddrOpnd::NewNull(m_func),
  6375. Js::OpCode::BrEq_A,
  6376. labelHelper,
  6377. insertInstr);
  6378. }
  6379. IR::RegOpnd *vt = IR::RegOpnd::New(TyMachPtr, this->m_func);
  6380. IR::Opnd* opnd = IR::IndirOpnd::New(opndSrc, (int32)0, TyMachPtr, this->m_func);
  6381. Lowerer::InsertMove(vt, opnd, insertInstr);
  6382. // CMP [number], JavascriptNumber::vtable
  6383. IR::Instr* instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  6384. instr->SetSrc1(vt);
  6385. instr->SetSrc2(m_lowerer->LoadVTableValueOpnd(insertInstr, VTableValue::VtableJavascriptNumber));
  6386. insertInstr->InsertBefore(instr);
  6387. LegalizeMD::LegalizeInstr(instr);
  6388. // BNE $helper
  6389. instr = IR::BranchInstr::New(Js::OpCode::BNE, labelHelper, this->m_func);
  6390. insertInstr->InsertBefore(instr);
  6391. }
  6392. void LowererMD::LoadFloatValue(IR::RegOpnd * javascriptNumber, IR::RegOpnd * opndFloat, IR::LabelInstr * labelHelper, IR::Instr * instrInsert, const bool checkForNullInLoopBody)
  6393. {
  6394. IR::Instr* instr;
  6395. IR::Opnd* opnd;
  6396. // Make sure it is float
  6397. this->GenerateFloatTest(javascriptNumber, instrInsert, labelHelper, checkForNullInLoopBody);
  6398. // VLDR opndFloat, [number + offsetof(value)]
  6399. opnd = IR::IndirOpnd::New(javascriptNumber, Js::JavascriptNumber::GetValueOffset(), TyMachDouble, this->m_func);
  6400. instr = IR::Instr::New(Js::OpCode::VLDR, opndFloat, opnd, this->m_func);
  6401. instrInsert->InsertBefore(instr);
  6402. }
  6403. template <bool verify>
  6404. void
  6405. LowererMD::Legalize(IR::Instr *const instr)
  6406. {
  6407. Func *const func = instr->m_func;
  6408. if(instr->m_opcode == Js::OpCode::VCVTS32F64 && instr->GetDst()->IsInt32())
  6409. {
  6410. if (verify)
  6411. {
  6412. AssertMsg(false, "Missing legalization");
  6413. return;
  6414. }
  6415. // This needs to be split into two steps
  6416. IR::RegOpnd *const float32Reg = IR::RegOpnd::New(TyFloat32, func);
  6417. const IR::AutoReuseOpnd autoReuseFloat32Reg(float32Reg, func);
  6418. IR::Instr *const newInstr = IR::Instr::New(Js::OpCode::VCVTS32F64, float32Reg, instr->GetSrc1(), func);
  6419. instr->InsertBefore(newInstr);
  6420. LegalizeMD::LegalizeInstr(newInstr);
  6421. instr->m_opcode = Js::OpCode::VMOVARMVFP;
  6422. instr->ReplaceSrc1(float32Reg);
  6423. }
  6424. if (verify)
  6425. {
  6426. // NYI for the rest of legalization
  6427. return;
  6428. }
  6429. LegalizeMD::LegalizeInstr(instr);
  6430. }
  6431. template void LowererMD::Legalize<false>(IR::Instr *const instr);
  6432. #if DBG
  6433. template void LowererMD::Legalize<true>(IR::Instr *const instr);
  6434. #endif
  6435. void
  6436. LowererMD::FinalLower()
  6437. {
  6438. NoRecoverMemoryArenaAllocator tempAlloc(_u("BE-ARMFinalLower"), m_func->m_alloc->GetPageAllocator(), Js::Throw::OutOfMemory);
  6439. EncodeReloc *pRelocList = nullptr;
  6440. uint32 instrOffset = 0;
  6441. FOREACH_INSTR_BACKWARD_EDITING_IN_RANGE(instr, instrPrev, this->m_func->m_tailInstr, this->m_func->m_headInstr)
  6442. {
  6443. if (instr->IsLowered() == false)
  6444. {
  6445. if (instr->IsLabelInstr())
  6446. {
  6447. //This is not the real set, Real offset gets set in encoder.
  6448. IR::LabelInstr *labelInstr = instr->AsLabelInstr();
  6449. labelInstr->SetOffset(instrOffset);
  6450. }
  6451. switch (instr->m_opcode)
  6452. {
  6453. case Js::OpCode::Ret:
  6454. instr->Remove();
  6455. break;
  6456. case Js::OpCode::Leave:
  6457. Assert(this->m_func->DoOptimizeTry() && !this->m_func->IsLoopBodyInTry());
  6458. instrPrev = m_lowerer->LowerLeave(instr, instr->AsBranchInstr()->GetTarget(), true /*fromFinalLower*/);
  6459. break;
  6460. }
  6461. }
  6462. else
  6463. {
  6464. //We are conservative here, assume each instruction take 4 bytes
  6465. instrOffset = instrOffset + MachMaxInstrSize;
  6466. if (instr->IsBranchInstr())
  6467. {
  6468. IR::BranchInstr *branchInstr = instr->AsBranchInstr();
  6469. if (branchInstr->GetTarget() && !LowererMD::IsUnconditionalBranch(branchInstr)) //Ignore BX register based branches & B
  6470. {
  6471. uint32 targetOffset = branchInstr->GetTarget()->GetOffset();
  6472. if (targetOffset != 0)
  6473. {
  6474. // this is backward reference
  6475. if (LegalizeMD::LegalizeDirectBranch(branchInstr, instrOffset))
  6476. {
  6477. //There might be an instruction inserted for legalizing conditional branch
  6478. instrOffset = instrOffset + MachMaxInstrSize;
  6479. }
  6480. }
  6481. else
  6482. {
  6483. EncodeReloc::New(&pRelocList, RelocTypeBranch20, (BYTE*)instrOffset, branchInstr, &tempAlloc);
  6484. //Assume this is a forward long branch, we shall fix up after complete pass, be conservative here
  6485. instrOffset = instrOffset + MachMaxInstrSize;
  6486. }
  6487. }
  6488. }
  6489. else if (LowererMD::IsAssign(instr) || instr->m_opcode == Js::OpCode::LEA || instr->m_opcode == Js::OpCode::LDARGOUTSZ || instr->m_opcode == Js::OpCode::REM)
  6490. {
  6491. // Cleanup spill code
  6492. // INSTR_BACKWARD_EDITING_IN_RANGE implies that next loop iteration will use instrPrev (instr->m_prev computed before entering current loop iteration).
  6493. IR::Instr* instrNext = instr->m_next;
  6494. bool canExpand = this->FinalLowerAssign(instr);
  6495. if (canExpand)
  6496. {
  6497. uint32 expandedInstrCount = 0; // The number of instrs the LDIMM expands into.
  6498. FOREACH_INSTR_IN_RANGE(instrCount, instrPrev->m_next, instrNext)
  6499. {
  6500. ++expandedInstrCount;
  6501. }
  6502. NEXT_INSTR_IN_RANGE;
  6503. Assert(expandedInstrCount > 0);
  6504. // Adjust the offset for expanded instrs.
  6505. instrOffset += (expandedInstrCount - 1) * MachMaxInstrSize; // We already accounted for one MachMaxInstrSize.
  6506. }
  6507. }
  6508. }
  6509. } NEXT_INSTR_BACKWARD_EDITING_IN_RANGE;
  6510. //Fixup all the forward branches
  6511. for (EncodeReloc *reloc = pRelocList; reloc; reloc = reloc->m_next)
  6512. {
  6513. AssertMsg((uint32)reloc->m_consumerOffset < reloc->m_relocInstr->AsBranchInstr()->GetTarget()->GetOffset(), "Only forward branches require fixup");
  6514. LegalizeMD::LegalizeDirectBranch(reloc->m_relocInstr->AsBranchInstr(), (uint32)reloc->m_consumerOffset);
  6515. }
  6516. return;
  6517. }
  6518. // Returns true, if and only if the assign may expand into multiple instrs.
  6519. bool
  6520. LowererMD::FinalLowerAssign(IR::Instr * instr)
  6521. {
  6522. if (instr->m_opcode == Js::OpCode::LDIMM)
  6523. {
  6524. LegalizeMD::LegalizeInstr(instr);
  6525. // LDIMM can expand into MOV/MOVT when the immediate is more than 16 bytes,
  6526. // it can also expand into multiple different no-op (normally MOV) instrs when we obfuscate it, which is randomly.
  6527. return true;
  6528. }
  6529. else if (EncoderMD::IsLoad(instr) || instr->m_opcode == Js::OpCode::LEA)
  6530. {
  6531. Assert(instr->GetDst()->IsRegOpnd());
  6532. if (!instr->GetSrc1()->IsRegOpnd())
  6533. {
  6534. LegalizeMD::LegalizeSrc(instr, instr->GetSrc1(), 1);
  6535. return true;
  6536. }
  6537. instr->m_opcode = (instr->GetSrc1()->GetType() == TyMachDouble) ? Js::OpCode::VMOV : Js::OpCode::MOV;
  6538. }
  6539. else if (EncoderMD::IsStore(instr))
  6540. {
  6541. Assert(instr->GetSrc1()->IsRegOpnd());
  6542. if (!instr->GetDst()->IsRegOpnd())
  6543. {
  6544. LegalizeMD::LegalizeDst(instr);
  6545. return true;
  6546. }
  6547. instr->m_opcode = (instr->GetDst()->GetType() == TyMachDouble) ? Js::OpCode::VMOV : Js::OpCode::MOV;
  6548. }
  6549. else if (instr->m_opcode == Js::OpCode::LDARGOUTSZ)
  6550. {
  6551. Assert(instr->GetDst()->IsRegOpnd());
  6552. Assert((instr->GetSrc1() == nullptr) && (instr->GetSrc2() == nullptr));
  6553. // dst = LDARGOUTSZ
  6554. // This loads the function's arg out area size into the dst operand. We need a pseudo-op,
  6555. // because we generate the instruction during Lower but don't yet know the value of the constant it needs
  6556. // to load. Change it to the appropriate LDIMM here.
  6557. uint32 argOutSize = UInt32Math::Mul(this->m_func->m_argSlotsForFunctionsCalled, MachRegInt, Js::Throw::OutOfMemory);
  6558. instr->SetSrc1(IR::IntConstOpnd::New(argOutSize, TyMachReg, this->m_func));
  6559. instr->m_opcode = Js::OpCode::LDIMM;
  6560. LegalizeMD::LegalizeInstr(instr);
  6561. return true;
  6562. }
  6563. else if (instr->m_opcode == Js::OpCode::REM)
  6564. {
  6565. IR::Opnd* dst = instr->GetDst();
  6566. IR::Opnd* src1 = instr->GetSrc1();
  6567. IR::Opnd* src2 = instr->GetSrc2();
  6568. Assert(src1->IsRegOpnd() && src1->AsRegOpnd()->GetReg() != SCRATCH_REG);
  6569. Assert(src2->IsRegOpnd() && src2->AsRegOpnd()->GetReg() != SCRATCH_REG);
  6570. //r12 = SDIV src1, src2
  6571. IR::RegOpnd *regScratch = IR::RegOpnd::New(nullptr, SCRATCH_REG, TyMachReg, instr->m_func);
  6572. IR::Instr *insertInstr = IR::Instr::New(Js::OpCode::SDIV, regScratch, src1, src2, instr->m_func);
  6573. instr->InsertBefore(insertInstr);
  6574. // dst = MLS (r12,) src2, src1
  6575. insertInstr = IR::Instr::New(Js::OpCode::MLS, dst, src2, src1, instr->m_func);
  6576. instr->InsertBefore(insertInstr);
  6577. instr->Remove();
  6578. return true;
  6579. }
  6580. return false;
  6581. }
  6582. IR::Opnd *
  6583. LowererMD::GenerateArgOutForStackArgs(IR::Instr* callInstr, IR::Instr* stackArgsInstr)
  6584. {
  6585. return this->m_lowerer->GenerateArgOutForStackArgs(callInstr, stackArgsInstr);
  6586. }
  6587. IR::Instr *
  6588. LowererMD::LowerDivI4AndBailOnReminder(IR::Instr * instr, IR::LabelInstr * bailOutLabel)
  6589. {
  6590. // result = SDIV numerator, denominator
  6591. // mulResult = MUL result, denominator
  6592. // CMP mulResult, numerator
  6593. // BNE bailout
  6594. // <Caller insert more checks here>
  6595. // dst = MOV result <-- insertBeforeInstr
  6596. instr->m_opcode = Js::OpCode::SDIV;
  6597. // delay assigning to the final dst.
  6598. IR::Instr * sinkedInstr = instr->SinkDst(Js::OpCode::MOV);
  6599. LegalizeMD::LegalizeInstr(instr);
  6600. LegalizeMD::LegalizeInstr(sinkedInstr);
  6601. IR::Opnd * resultOpnd = instr->GetDst();
  6602. IR::Opnd * numerator = instr->GetSrc1();
  6603. IR::Opnd * denominatorOpnd = instr->GetSrc2();
  6604. // Insert all check before the assignment to the actual
  6605. IR::Instr * insertBeforeInstr = instr->m_next;
  6606. // Jump to bailout if the reminder is not 0 (or the divResult * denominator is not same as the numerator)
  6607. IR::RegOpnd * mulResult = IR::RegOpnd::New(TyInt32, m_func);
  6608. IR::Instr * mulInstr = IR::Instr::New(Js::OpCode::MUL, mulResult, resultOpnd, denominatorOpnd, m_func);
  6609. insertBeforeInstr->InsertBefore(mulInstr);
  6610. LegalizeMD::LegalizeInstr(mulInstr);
  6611. this->m_lowerer->InsertCompareBranch(mulResult, numerator, Js::OpCode::BrNeq_A, bailOutLabel, insertBeforeInstr);
  6612. return insertBeforeInstr;
  6613. }
  6614. void
  6615. LowererMD::LowerInlineSpreadArgOutLoop(IR::Instr *callInstr, IR::RegOpnd *indexOpnd, IR::RegOpnd *arrayElementsStartOpnd)
  6616. {
  6617. this->m_lowerer->LowerInlineSpreadArgOutLoopUsingRegisters(callInstr, indexOpnd, arrayElementsStartOpnd);
  6618. }
  6619. void
  6620. LowererMD::LowerTypeof(IR::Instr* typeOfInstr)
  6621. {
  6622. Func * func = typeOfInstr->m_func;
  6623. IR::Opnd * src1 = typeOfInstr->GetSrc1();
  6624. IR::Opnd * dst = typeOfInstr->GetDst();
  6625. Assert(src1->IsRegOpnd() && dst->IsRegOpnd());
  6626. IR::LabelInstr * helperLabel = IR::LabelInstr::New(Js::OpCode::Label, func, true);
  6627. IR::LabelInstr * taggedIntLabel = IR::LabelInstr::New(Js::OpCode::Label, func);
  6628. IR::LabelInstr * doneLabel = IR::LabelInstr::New(Js::OpCode::Label, func);
  6629. // MOV typeDisplayStringsArray, &javascriptLibrary->typeDisplayStrings
  6630. IR::RegOpnd * typeDisplayStringsArrayOpnd = IR::RegOpnd::New(TyMachPtr, func);
  6631. m_lowerer->InsertMove(typeDisplayStringsArrayOpnd, IR::AddrOpnd::New((BYTE*)m_func->GetScriptContextInfo()->GetLibraryAddr() + Js::JavascriptLibrary::GetTypeDisplayStringsOffset(), IR::AddrOpndKindConstantAddress, this->m_func), typeOfInstr);
  6632. GenerateObjectTest(src1, typeOfInstr, taggedIntLabel);
  6633. // MOV typeRegOpnd, [src1 + offset(Type)]
  6634. IR::RegOpnd * typeRegOpnd = IR::RegOpnd::New(TyMachReg, func);
  6635. m_lowerer->InsertMove(typeRegOpnd,
  6636. IR::IndirOpnd::New(src1->AsRegOpnd(), Js::RecyclableObject::GetOffsetOfType(), TyMachReg, func),
  6637. typeOfInstr);
  6638. IR::LabelInstr * falsyLabel = IR::LabelInstr::New(Js::OpCode::Label, func);
  6639. m_lowerer->GenerateFalsyObjectTest(typeOfInstr, typeRegOpnd, falsyLabel);
  6640. // <$not falsy>
  6641. // MOV typeId, TypeIds_Object
  6642. // MOV objTypeId, [typeRegOpnd + offsetof(typeId)]
  6643. // CMP objTypeId, TypeIds_Limit /*external object test*/
  6644. // BCS $externalObjectLabel
  6645. // MOV typeId, objTypeId
  6646. // $loadTypeDisplayStringLabel:
  6647. // MOV dst, typeDisplayStrings[typeId]
  6648. // TEST dst, dst
  6649. // BEQ $helper
  6650. // B $done
  6651. IR::RegOpnd * typeIdOpnd = IR::RegOpnd::New(TyUint32, func);
  6652. m_lowerer->InsertMove(typeIdOpnd, IR::IntConstOpnd::New(Js::TypeIds_Object, TyUint32, func), typeOfInstr);
  6653. IR::RegOpnd * objTypeIdOpnd = IR::RegOpnd::New(TyUint32, func);
  6654. m_lowerer->InsertMove(objTypeIdOpnd, IR::IndirOpnd::New(typeRegOpnd, Js::Type::GetOffsetOfTypeId(), TyInt32, func), typeOfInstr);
  6655. IR::LabelInstr * loadTypeDisplayStringLabel = IR::LabelInstr::New(Js::OpCode::Label, func);
  6656. m_lowerer->InsertCompareBranch(objTypeIdOpnd, IR::IntConstOpnd::New(Js::TypeIds_Limit, TyUint32, func), Js::OpCode::BrGe_A, true /*unsigned*/, loadTypeDisplayStringLabel, typeOfInstr);
  6657. m_lowerer->InsertMove(typeIdOpnd, objTypeIdOpnd, typeOfInstr);
  6658. typeOfInstr->InsertBefore(loadTypeDisplayStringLabel);
  6659. if (dst->IsEqual(src1))
  6660. {
  6661. ChangeToAssign(typeOfInstr->HoistSrc1(Js::OpCode::Ld_A));
  6662. }
  6663. m_lowerer->InsertMove(dst, IR::IndirOpnd::New(typeDisplayStringsArrayOpnd, typeIdOpnd, this->GetDefaultIndirScale(), TyMachPtr, func), typeOfInstr);
  6664. m_lowerer->InsertTestBranch(dst, dst, Js::OpCode::BrEq_A, helperLabel, typeOfInstr);
  6665. m_lowerer->InsertBranch(Js::OpCode::Br, doneLabel, typeOfInstr);
  6666. // $taggedInt:
  6667. // MOV dst, typeDisplayStrings[TypeIds_Number]
  6668. // B $done
  6669. typeOfInstr->InsertBefore(taggedIntLabel);
  6670. m_lowerer->InsertMove(dst, IR::IndirOpnd::New(typeDisplayStringsArrayOpnd, Js::TypeIds_Number * sizeof(Js::Var), TyMachPtr, func), typeOfInstr);
  6671. m_lowerer->InsertBranch(Js::OpCode::Br, doneLabel, typeOfInstr);
  6672. // $falsy:
  6673. // MOV dst, "undefined"
  6674. // B $done
  6675. typeOfInstr->InsertBefore(falsyLabel);
  6676. IR::Opnd * undefinedDisplayStringOpnd = IR::IndirOpnd::New(typeDisplayStringsArrayOpnd, Js::TypeIds_Undefined, TyMachPtr, func);
  6677. m_lowerer->InsertMove(dst, undefinedDisplayStringOpnd, typeOfInstr);
  6678. m_lowerer->InsertBranch(Js::OpCode::Br, doneLabel, typeOfInstr);
  6679. // $helper
  6680. // CALL OP_TypeOf
  6681. // $done
  6682. typeOfInstr->InsertBefore(helperLabel);
  6683. typeOfInstr->InsertAfter(doneLabel);
  6684. m_lowerer->LowerUnaryHelperMem(typeOfInstr, IR::HelperOp_Typeof);
  6685. }
  6686. IR::BranchInstr*
  6687. LowererMD::InsertMissingItemCompareBranch(IR::Opnd* compareSrc, IR::Opnd* missingItemOpnd, Js::OpCode opcode, IR::LabelInstr* target, IR::Instr* insertBeforeInstr)
  6688. {
  6689. Assert(compareSrc->IsFloat64() && missingItemOpnd->IsUInt32());
  6690. IR::Opnd * compareSrcUint32Opnd = IR::RegOpnd::New(TyUint32, m_func);
  6691. IR::RegOpnd* tmpDoubleRegOpnd = IR::RegOpnd::New(TyFloat64, m_func);
  6692. if (compareSrc->IsIndirOpnd())
  6693. {
  6694. Lowerer::InsertMove(tmpDoubleRegOpnd, compareSrc, insertBeforeInstr);
  6695. }
  6696. else
  6697. {
  6698. tmpDoubleRegOpnd = compareSrc->AsRegOpnd();
  6699. }
  6700. IR::Instr * movInstr = IR::Instr::New(Js::OpCode::VMOVF64R32U, compareSrcUint32Opnd, tmpDoubleRegOpnd, m_func);
  6701. insertBeforeInstr->InsertBefore(movInstr);
  6702. Legalize(movInstr);
  6703. return m_lowerer->InsertCompareBranch(missingItemOpnd, compareSrcUint32Opnd, opcode, target, insertBeforeInstr);
  6704. }
  6705. #if DBG
  6706. //
  6707. // Helps in debugging of fast paths.
  6708. //
  6709. void LowererMD::GenerateDebugBreak( IR::Instr * insertInstr )
  6710. {
  6711. IR::Instr *int3 = IR::Instr::New(Js::OpCode::DEBUGBREAK, insertInstr->m_func);
  6712. insertInstr->InsertBefore(int3);
  6713. }
  6714. #endif