vm_x86.dasc 166 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374
  1. |// Low-level VM code for x86 CPUs.
  2. |// Bytecode interpreter, fast functions and helper functions.
  3. |// Copyright (C) 2005-2014 Mike Pall. See Copyright Notice in luajit.h
  4. |
  5. |.if P64
  6. |.arch x64
  7. |.else
  8. |.arch x86
  9. |.endif
  10. |.section code_op, code_sub
  11. |
  12. |.actionlist build_actionlist
  13. |.globals GLOB_
  14. |.globalnames globnames
  15. |.externnames extnames
  16. |
  17. |//-----------------------------------------------------------------------
  18. |
  19. |.if P64
  20. |.define X64, 1
  21. |.define SSE, 1
  22. |.if WIN
  23. |.define X64WIN, 1
  24. |.endif
  25. |.endif
  26. |
  27. |// Fixed register assignments for the interpreter.
  28. |// This is very fragile and has many dependencies. Caveat emptor.
  29. |.define BASE, edx // Not C callee-save, refetched anyway.
  30. |.if not X64
  31. |.define KBASE, edi // Must be C callee-save.
  32. |.define KBASEa, KBASE
  33. |.define PC, esi // Must be C callee-save.
  34. |.define PCa, PC
  35. |.define DISPATCH, ebx // Must be C callee-save.
  36. |.elif X64WIN
  37. |.define KBASE, edi // Must be C callee-save.
  38. |.define KBASEa, rdi
  39. |.define PC, esi // Must be C callee-save.
  40. |.define PCa, rsi
  41. |.define DISPATCH, ebx // Must be C callee-save.
  42. |.else
  43. |.define KBASE, r15d // Must be C callee-save.
  44. |.define KBASEa, r15
  45. |.define PC, ebx // Must be C callee-save.
  46. |.define PCa, rbx
  47. |.define DISPATCH, r14d // Must be C callee-save.
  48. |.endif
  49. |
  50. |.define RA, ecx
  51. |.define RAH, ch
  52. |.define RAL, cl
  53. |.define RB, ebp // Must be ebp (C callee-save).
  54. |.define RC, eax // Must be eax.
  55. |.define RCW, ax
  56. |.define RCH, ah
  57. |.define RCL, al
  58. |.define OP, RB
  59. |.define RD, RC
  60. |.define RDW, RCW
  61. |.define RDL, RCL
  62. |.if X64
  63. |.define RAa, rcx
  64. |.define RBa, rbp
  65. |.define RCa, rax
  66. |.define RDa, rax
  67. |.else
  68. |.define RAa, RA
  69. |.define RBa, RB
  70. |.define RCa, RC
  71. |.define RDa, RD
  72. |.endif
  73. |
  74. |.if not X64
  75. |.define FCARG1, ecx // x86 fastcall arguments.
  76. |.define FCARG2, edx
  77. |.elif X64WIN
  78. |.define CARG1, rcx // x64/WIN64 C call arguments.
  79. |.define CARG2, rdx
  80. |.define CARG3, r8
  81. |.define CARG4, r9
  82. |.define CARG1d, ecx
  83. |.define CARG2d, edx
  84. |.define CARG3d, r8d
  85. |.define CARG4d, r9d
  86. |.define FCARG1, CARG1d // Upwards compatible to x86 fastcall.
  87. |.define FCARG2, CARG2d
  88. |.else
  89. |.define CARG1, rdi // x64/POSIX C call arguments.
  90. |.define CARG2, rsi
  91. |.define CARG3, rdx
  92. |.define CARG4, rcx
  93. |.define CARG5, r8
  94. |.define CARG6, r9
  95. |.define CARG1d, edi
  96. |.define CARG2d, esi
  97. |.define CARG3d, edx
  98. |.define CARG4d, ecx
  99. |.define CARG5d, r8d
  100. |.define CARG6d, r9d
  101. |.define FCARG1, CARG1d // Simulate x86 fastcall.
  102. |.define FCARG2, CARG2d
  103. |.endif
  104. |
  105. |// Type definitions. Some of these are only used for documentation.
  106. |.type L, lua_State
  107. |.type GL, global_State
  108. |.type TVALUE, TValue
  109. |.type GCOBJ, GCobj
  110. |.type STR, GCstr
  111. |.type TAB, GCtab
  112. |.type LFUNC, GCfuncL
  113. |.type CFUNC, GCfuncC
  114. |.type PROTO, GCproto
  115. |.type UPVAL, GCupval
  116. |.type NODE, Node
  117. |.type NARGS, int
  118. |.type TRACE, GCtrace
  119. |
  120. |// Stack layout while in interpreter. Must match with lj_frame.h.
  121. |//-----------------------------------------------------------------------
  122. |.if not X64 // x86 stack layout.
  123. |
  124. |.define CFRAME_SPACE, aword*7 // Delta for esp (see <--).
  125. |.macro saveregs_
  126. | push edi; push esi; push ebx
  127. | sub esp, CFRAME_SPACE
  128. |.endmacro
  129. |.macro saveregs
  130. | push ebp; saveregs_
  131. |.endmacro
  132. |.macro restoreregs
  133. | add esp, CFRAME_SPACE
  134. | pop ebx; pop esi; pop edi; pop ebp
  135. |.endmacro
  136. |
  137. |.define SAVE_ERRF, aword [esp+aword*15] // vm_pcall/vm_cpcall only.
  138. |.define SAVE_NRES, aword [esp+aword*14]
  139. |.define SAVE_CFRAME, aword [esp+aword*13]
  140. |.define SAVE_L, aword [esp+aword*12]
  141. |//----- 16 byte aligned, ^^^ arguments from C caller
  142. |.define SAVE_RET, aword [esp+aword*11] //<-- esp entering interpreter.
  143. |.define SAVE_R4, aword [esp+aword*10]
  144. |.define SAVE_R3, aword [esp+aword*9]
  145. |.define SAVE_R2, aword [esp+aword*8]
  146. |//----- 16 byte aligned
  147. |.define SAVE_R1, aword [esp+aword*7] //<-- esp after register saves.
  148. |.define SAVE_PC, aword [esp+aword*6]
  149. |.define TMP2, aword [esp+aword*5]
  150. |.define TMP1, aword [esp+aword*4]
  151. |//----- 16 byte aligned
  152. |.define ARG4, aword [esp+aword*3]
  153. |.define ARG3, aword [esp+aword*2]
  154. |.define ARG2, aword [esp+aword*1]
  155. |.define ARG1, aword [esp] //<-- esp while in interpreter.
  156. |//----- 16 byte aligned, ^^^ arguments for C callee
  157. |
  158. |// FPARGx overlaps ARGx and ARG(x+1) on x86.
  159. |.define FPARG3, qword [esp+qword*1]
  160. |.define FPARG1, qword [esp]
  161. |// TMPQ overlaps TMP1/TMP2. ARG5/MULTRES overlap TMP1/TMP2 (and TMPQ).
  162. |.define TMPQ, qword [esp+aword*4]
  163. |.define TMP3, ARG4
  164. |.define ARG5, TMP1
  165. |.define TMPa, TMP1
  166. |.define MULTRES, TMP2
  167. |
  168. |// Arguments for vm_call and vm_pcall.
  169. |.define INARG_BASE, SAVE_CFRAME // Overwritten by SAVE_CFRAME!
  170. |
  171. |// Arguments for vm_cpcall.
  172. |.define INARG_CP_CALL, SAVE_ERRF
  173. |.define INARG_CP_UD, SAVE_NRES
  174. |.define INARG_CP_FUNC, SAVE_CFRAME
  175. |
  176. |//-----------------------------------------------------------------------
  177. |.elif X64WIN // x64/Windows stack layout
  178. |
  179. |.define CFRAME_SPACE, aword*5 // Delta for rsp (see <--).
  180. |.macro saveregs_
  181. | push rdi; push rsi; push rbx
  182. | sub rsp, CFRAME_SPACE
  183. |.endmacro
  184. |.macro saveregs
  185. | push rbp; saveregs_
  186. |.endmacro
  187. |.macro restoreregs
  188. | add rsp, CFRAME_SPACE
  189. | pop rbx; pop rsi; pop rdi; pop rbp
  190. |.endmacro
  191. |
  192. |.define SAVE_CFRAME, aword [rsp+aword*13]
  193. |.define SAVE_PC, dword [rsp+dword*25]
  194. |.define SAVE_L, dword [rsp+dword*24]
  195. |.define SAVE_ERRF, dword [rsp+dword*23]
  196. |.define SAVE_NRES, dword [rsp+dword*22]
  197. |.define TMP2, dword [rsp+dword*21]
  198. |.define TMP1, dword [rsp+dword*20]
  199. |//----- 16 byte aligned, ^^^ 32 byte register save area, owned by interpreter
  200. |.define SAVE_RET, aword [rsp+aword*9] //<-- rsp entering interpreter.
  201. |.define SAVE_R4, aword [rsp+aword*8]
  202. |.define SAVE_R3, aword [rsp+aword*7]
  203. |.define SAVE_R2, aword [rsp+aword*6]
  204. |.define SAVE_R1, aword [rsp+aword*5] //<-- rsp after register saves.
  205. |.define ARG5, aword [rsp+aword*4]
  206. |.define CSAVE_4, aword [rsp+aword*3]
  207. |.define CSAVE_3, aword [rsp+aword*2]
  208. |.define CSAVE_2, aword [rsp+aword*1]
  209. |.define CSAVE_1, aword [rsp] //<-- rsp while in interpreter.
  210. |//----- 16 byte aligned, ^^^ 32 byte register save area, owned by callee
  211. |
  212. |// TMPQ overlaps TMP1/TMP2. MULTRES overlaps TMP2 (and TMPQ).
  213. |.define TMPQ, qword [rsp+aword*10]
  214. |.define MULTRES, TMP2
  215. |.define TMPa, ARG5
  216. |.define ARG5d, dword [rsp+aword*4]
  217. |.define TMP3, ARG5d
  218. |
  219. |//-----------------------------------------------------------------------
  220. |.else // x64/POSIX stack layout
  221. |
  222. |.define CFRAME_SPACE, aword*5 // Delta for rsp (see <--).
  223. |.macro saveregs_
  224. | push rbx; push r15; push r14
  225. | sub rsp, CFRAME_SPACE
  226. |.endmacro
  227. |.macro saveregs
  228. | push rbp; saveregs_
  229. |.endmacro
  230. |.macro restoreregs
  231. | add rsp, CFRAME_SPACE
  232. | pop r14; pop r15; pop rbx; pop rbp
  233. |.endmacro
  234. |
  235. |//----- 16 byte aligned,
  236. |.define SAVE_RET, aword [rsp+aword*9] //<-- rsp entering interpreter.
  237. |.define SAVE_R4, aword [rsp+aword*8]
  238. |.define SAVE_R3, aword [rsp+aword*7]
  239. |.define SAVE_R2, aword [rsp+aword*6]
  240. |.define SAVE_R1, aword [rsp+aword*5] //<-- rsp after register saves.
  241. |.define SAVE_CFRAME, aword [rsp+aword*4]
  242. |.define SAVE_PC, dword [rsp+dword*7]
  243. |.define SAVE_L, dword [rsp+dword*6]
  244. |.define SAVE_ERRF, dword [rsp+dword*5]
  245. |.define SAVE_NRES, dword [rsp+dword*4]
  246. |.define TMPa, aword [rsp+aword*1]
  247. |.define TMP2, dword [rsp+dword*1]
  248. |.define TMP1, dword [rsp] //<-- rsp while in interpreter.
  249. |//----- 16 byte aligned
  250. |
  251. |// TMPQ overlaps TMP1/TMP2. MULTRES overlaps TMP2 (and TMPQ).
  252. |.define TMPQ, qword [rsp]
  253. |.define TMP3, dword [rsp+aword*1]
  254. |.define MULTRES, TMP2
  255. |
  256. |.endif
  257. |
  258. |//-----------------------------------------------------------------------
  259. |
  260. |// Instruction headers.
  261. |.macro ins_A; .endmacro
  262. |.macro ins_AD; .endmacro
  263. |.macro ins_AJ; .endmacro
  264. |.macro ins_ABC; movzx RB, RCH; movzx RC, RCL; .endmacro
  265. |.macro ins_AB_; movzx RB, RCH; .endmacro
  266. |.macro ins_A_C; movzx RC, RCL; .endmacro
  267. |.macro ins_AND; not RDa; .endmacro
  268. |
  269. |// Instruction decode+dispatch. Carefully tuned (nope, lodsd is not faster).
  270. |.macro ins_NEXT
  271. | mov RC, [PC]
  272. | movzx RA, RCH
  273. | movzx OP, RCL
  274. | add PC, 4
  275. | shr RC, 16
  276. |.if X64
  277. | jmp aword [DISPATCH+OP*8]
  278. |.else
  279. | jmp aword [DISPATCH+OP*4]
  280. |.endif
  281. |.endmacro
  282. |
  283. |// Instruction footer.
  284. |.if 1
  285. | // Replicated dispatch. Less unpredictable branches, but higher I-Cache use.
  286. | .define ins_next, ins_NEXT
  287. | .define ins_next_, ins_NEXT
  288. |.else
  289. | // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch.
  290. | // Affects only certain kinds of benchmarks (and only with -j off).
  291. | // Around 10%-30% slower on Core2, a lot more slower on P4.
  292. | .macro ins_next
  293. | jmp ->ins_next
  294. | .endmacro
  295. | .macro ins_next_
  296. | ->ins_next:
  297. | ins_NEXT
  298. | .endmacro
  299. |.endif
  300. |
  301. |// Call decode and dispatch.
  302. |.macro ins_callt
  303. | // BASE = new base, RB = LFUNC, RD = nargs+1, [BASE-4] = PC
  304. | mov PC, LFUNC:RB->pc
  305. | mov RA, [PC]
  306. | movzx OP, RAL
  307. | movzx RA, RAH
  308. | add PC, 4
  309. |.if X64
  310. | jmp aword [DISPATCH+OP*8]
  311. |.else
  312. | jmp aword [DISPATCH+OP*4]
  313. |.endif
  314. |.endmacro
  315. |
  316. |.macro ins_call
  317. | // BASE = new base, RB = LFUNC, RD = nargs+1
  318. | mov [BASE-4], PC
  319. | ins_callt
  320. |.endmacro
  321. |
  322. |//-----------------------------------------------------------------------
  323. |
  324. |// Macros to test operand types.
  325. |.macro checktp, reg, tp; cmp dword [BASE+reg*8+4], tp; .endmacro
  326. |.macro checknum, reg, target; checktp reg, LJ_TISNUM; jae target; .endmacro
  327. |.macro checkint, reg, target; checktp reg, LJ_TISNUM; jne target; .endmacro
  328. |.macro checkstr, reg, target; checktp reg, LJ_TSTR; jne target; .endmacro
  329. |.macro checktab, reg, target; checktp reg, LJ_TTAB; jne target; .endmacro
  330. |
  331. |// These operands must be used with movzx.
  332. |.define PC_OP, byte [PC-4]
  333. |.define PC_RA, byte [PC-3]
  334. |.define PC_RB, byte [PC-1]
  335. |.define PC_RC, byte [PC-2]
  336. |.define PC_RD, word [PC-2]
  337. |
  338. |.macro branchPC, reg
  339. | lea PC, [PC+reg*4-BCBIAS_J*4]
  340. |.endmacro
  341. |
  342. |// Assumes DISPATCH is relative to GL.
  343. #define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field))
  344. #define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field))
  345. |
  346. #define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto))
  347. |
  348. |// Decrement hashed hotcount and trigger trace recorder if zero.
  349. |.macro hotloop, reg
  350. | mov reg, PC
  351. | shr reg, 1
  352. | and reg, HOTCOUNT_PCMASK
  353. | sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_LOOP
  354. | jb ->vm_hotloop
  355. |.endmacro
  356. |
  357. |.macro hotcall, reg
  358. | mov reg, PC
  359. | shr reg, 1
  360. | and reg, HOTCOUNT_PCMASK
  361. | sub word [DISPATCH+reg+GG_DISP2HOT], HOTCOUNT_CALL
  362. | jb ->vm_hotcall
  363. |.endmacro
  364. |
  365. |// Set current VM state.
  366. |.macro set_vmstate, st
  367. | mov dword [DISPATCH+DISPATCH_GL(vmstate)], ~LJ_VMST_..st
  368. |.endmacro
  369. |
  370. |// x87 compares.
  371. |.macro fcomparepp // Compare and pop st0 >< st1.
  372. | fucomip st1
  373. | fpop
  374. |.endmacro
  375. |
  376. |.macro fdup; fld st0; .endmacro
  377. |.macro fpop1; fstp st1; .endmacro
  378. |
  379. |// Synthesize SSE FP constants.
  380. |.macro sseconst_abs, reg, tmp // Synthesize abs mask.
  381. |.if X64
  382. | mov64 tmp, U64x(7fffffff,ffffffff); movd reg, tmp
  383. |.else
  384. | pxor reg, reg; pcmpeqd reg, reg; psrlq reg, 1
  385. |.endif
  386. |.endmacro
  387. |
  388. |.macro sseconst_hi, reg, tmp, val // Synthesize hi-32 bit const.
  389. |.if X64
  390. | mov64 tmp, U64x(val,00000000); movd reg, tmp
  391. |.else
  392. | mov tmp, 0x .. val; movd reg, tmp; pshufd reg, reg, 0x51
  393. |.endif
  394. |.endmacro
  395. |
  396. |.macro sseconst_sign, reg, tmp // Synthesize sign mask.
  397. | sseconst_hi reg, tmp, 80000000
  398. |.endmacro
  399. |.macro sseconst_1, reg, tmp // Synthesize 1.0.
  400. | sseconst_hi reg, tmp, 3ff00000
  401. |.endmacro
  402. |.macro sseconst_m1, reg, tmp // Synthesize -1.0.
  403. | sseconst_hi reg, tmp, bff00000
  404. |.endmacro
  405. |.macro sseconst_2p52, reg, tmp // Synthesize 2^52.
  406. | sseconst_hi reg, tmp, 43300000
  407. |.endmacro
  408. |.macro sseconst_tobit, reg, tmp // Synthesize 2^52 + 2^51.
  409. | sseconst_hi reg, tmp, 43380000
  410. |.endmacro
  411. |
  412. |// Move table write barrier back. Overwrites reg.
  413. |.macro barrierback, tab, reg
  414. | and byte tab->marked, (uint8_t)~LJ_GC_BLACK // black2gray(tab)
  415. | mov reg, [DISPATCH+DISPATCH_GL(gc.grayagain)]
  416. | mov [DISPATCH+DISPATCH_GL(gc.grayagain)], tab
  417. | mov tab->gclist, reg
  418. |.endmacro
  419. |
  420. |//-----------------------------------------------------------------------
  421. /* Generate subroutines used by opcodes and other parts of the VM. */
  422. /* The .code_sub section should be last to help static branch prediction. */
  423. static void build_subroutines(BuildCtx *ctx)
  424. {
  425. |.code_sub
  426. |
  427. |//-----------------------------------------------------------------------
  428. |//-- Return handling ----------------------------------------------------
  429. |//-----------------------------------------------------------------------
  430. |
  431. |->vm_returnp:
  432. | test PC, FRAME_P
  433. | jz ->cont_dispatch
  434. |
  435. | // Return from pcall or xpcall fast func.
  436. | and PC, -8
  437. | sub BASE, PC // Restore caller base.
  438. | lea RAa, [RA+PC-8] // Rebase RA and prepend one result.
  439. | mov PC, [BASE-4] // Fetch PC of previous frame.
  440. | // Prepending may overwrite the pcall frame, so do it at the end.
  441. | mov dword [BASE+RA+4], LJ_TTRUE // Prepend true to results.
  442. |
  443. |->vm_returnc:
  444. | add RD, 1 // RD = nresults+1
  445. | jz ->vm_unwind_yield
  446. | mov MULTRES, RD
  447. | test PC, FRAME_TYPE
  448. | jz ->BC_RET_Z // Handle regular return to Lua.
  449. |
  450. |->vm_return:
  451. | // BASE = base, RA = resultofs, RD = nresults+1 (= MULTRES), PC = return
  452. | xor PC, FRAME_C
  453. | test PC, FRAME_TYPE
  454. | jnz ->vm_returnp
  455. |
  456. | // Return to C.
  457. | set_vmstate C
  458. | and PC, -8
  459. | sub PC, BASE
  460. | neg PC // Previous base = BASE - delta.
  461. |
  462. | sub RD, 1
  463. | jz >2
  464. |1: // Move results down.
  465. |.if X64
  466. | mov RBa, [BASE+RA]
  467. | mov [BASE-8], RBa
  468. |.else
  469. | mov RB, [BASE+RA]
  470. | mov [BASE-8], RB
  471. | mov RB, [BASE+RA+4]
  472. | mov [BASE-4], RB
  473. |.endif
  474. | add BASE, 8
  475. | sub RD, 1
  476. | jnz <1
  477. |2:
  478. | mov L:RB, SAVE_L
  479. | mov L:RB->base, PC
  480. |3:
  481. | mov RD, MULTRES
  482. | mov RA, SAVE_NRES // RA = wanted nresults+1
  483. |4:
  484. | cmp RA, RD
  485. | jne >6 // More/less results wanted?
  486. |5:
  487. | sub BASE, 8
  488. | mov L:RB->top, BASE
  489. |
  490. |->vm_leave_cp:
  491. | mov RAa, SAVE_CFRAME // Restore previous C frame.
  492. | mov L:RB->cframe, RAa
  493. | xor eax, eax // Ok return status for vm_pcall.
  494. |
  495. |->vm_leave_unw:
  496. | restoreregs
  497. | ret
  498. |
  499. |6:
  500. | jb >7 // Less results wanted?
  501. | // More results wanted. Check stack size and fill up results with nil.
  502. | cmp BASE, L:RB->maxstack
  503. | ja >8
  504. | mov dword [BASE-4], LJ_TNIL
  505. | add BASE, 8
  506. | add RD, 1
  507. | jmp <4
  508. |
  509. |7: // Less results wanted.
  510. | test RA, RA
  511. | jz <5 // But check for LUA_MULTRET+1.
  512. | sub RA, RD // Negative result!
  513. | lea BASE, [BASE+RA*8] // Correct top.
  514. | jmp <5
  515. |
  516. |8: // Corner case: need to grow stack for filling up results.
  517. | // This can happen if:
  518. | // - A C function grows the stack (a lot).
  519. | // - The GC shrinks the stack in between.
  520. | // - A return back from a lua_call() with (high) nresults adjustment.
  521. | mov L:RB->top, BASE // Save current top held in BASE (yes).
  522. | mov MULTRES, RD // Need to fill only remainder with nil.
  523. | mov FCARG2, RA
  524. | mov FCARG1, L:RB
  525. | call extern lj_state_growstack@8 // (lua_State *L, int n)
  526. | mov BASE, L:RB->top // Need the (realloced) L->top in BASE.
  527. | jmp <3
  528. |
  529. |->vm_unwind_yield:
  530. | mov al, LUA_YIELD
  531. | jmp ->vm_unwind_c_eh
  532. |
  533. |->vm_unwind_c@8: // Unwind C stack, return from vm_pcall.
  534. | // (void *cframe, int errcode)
  535. |.if X64
  536. | mov eax, CARG2d // Error return status for vm_pcall.
  537. | mov rsp, CARG1
  538. |.else
  539. | mov eax, FCARG2 // Error return status for vm_pcall.
  540. | mov esp, FCARG1
  541. |.endif
  542. |->vm_unwind_c_eh: // Landing pad for external unwinder.
  543. | mov L:RB, SAVE_L
  544. | mov GL:RB, L:RB->glref
  545. | mov dword GL:RB->vmstate, ~LJ_VMST_C
  546. | jmp ->vm_leave_unw
  547. |
  548. |->vm_unwind_rethrow:
  549. |.if X64 and not X64WIN
  550. | mov FCARG1, SAVE_L
  551. | mov FCARG2, eax
  552. | restoreregs
  553. | jmp extern lj_err_throw@8 // (lua_State *L, int errcode)
  554. |.endif
  555. |
  556. |->vm_unwind_ff@4: // Unwind C stack, return from ff pcall.
  557. | // (void *cframe)
  558. |.if X64
  559. | and CARG1, CFRAME_RAWMASK
  560. | mov rsp, CARG1
  561. |.else
  562. | and FCARG1, CFRAME_RAWMASK
  563. | mov esp, FCARG1
  564. |.endif
  565. |->vm_unwind_ff_eh: // Landing pad for external unwinder.
  566. | mov L:RB, SAVE_L
  567. | mov RAa, -8 // Results start at BASE+RA = BASE-8.
  568. | mov RD, 1+1 // Really 1+2 results, incr. later.
  569. | mov BASE, L:RB->base
  570. | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table.
  571. | add DISPATCH, GG_G2DISP
  572. | mov PC, [BASE-4] // Fetch PC of previous frame.
  573. | mov dword [BASE-4], LJ_TFALSE // Prepend false to error message.
  574. | set_vmstate INTERP
  575. | jmp ->vm_returnc // Increments RD/MULTRES and returns.
  576. |
  577. |//-----------------------------------------------------------------------
  578. |//-- Grow stack for calls -----------------------------------------------
  579. |//-----------------------------------------------------------------------
  580. |
  581. |->vm_growstack_c: // Grow stack for C function.
  582. | mov FCARG2, LUA_MINSTACK
  583. | jmp >2
  584. |
  585. |->vm_growstack_v: // Grow stack for vararg Lua function.
  586. | sub RD, 8
  587. | jmp >1
  588. |
  589. |->vm_growstack_f: // Grow stack for fixarg Lua function.
  590. | // BASE = new base, RD = nargs+1, RB = L, PC = first PC
  591. | lea RD, [BASE+NARGS:RD*8-8]
  592. |1:
  593. | movzx RA, byte [PC-4+PC2PROTO(framesize)]
  594. | add PC, 4 // Must point after first instruction.
  595. | mov L:RB->base, BASE
  596. | mov L:RB->top, RD
  597. | mov SAVE_PC, PC
  598. | mov FCARG2, RA
  599. |2:
  600. | // RB = L, L->base = new base, L->top = top
  601. | mov FCARG1, L:RB
  602. | call extern lj_state_growstack@8 // (lua_State *L, int n)
  603. | mov BASE, L:RB->base
  604. | mov RD, L:RB->top
  605. | mov LFUNC:RB, [BASE-8]
  606. | sub RD, BASE
  607. | shr RD, 3
  608. | add NARGS:RD, 1
  609. | // BASE = new base, RB = LFUNC, RD = nargs+1
  610. | ins_callt // Just retry the call.
  611. |
  612. |//-----------------------------------------------------------------------
  613. |//-- Entry points into the assembler VM ---------------------------------
  614. |//-----------------------------------------------------------------------
  615. |
  616. |->vm_resume: // Setup C frame and resume thread.
  617. | // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0)
  618. | saveregs
  619. |.if X64
  620. | mov L:RB, CARG1d // Caveat: CARG1d may be RA.
  621. | mov SAVE_L, CARG1d
  622. | mov RA, CARG2d
  623. |.else
  624. | mov L:RB, SAVE_L
  625. | mov RA, INARG_BASE // Caveat: overlaps SAVE_CFRAME!
  626. |.endif
  627. | mov PC, FRAME_CP
  628. | xor RD, RD
  629. | lea KBASEa, [esp+CFRAME_RESUME]
  630. | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table.
  631. | add DISPATCH, GG_G2DISP
  632. | mov L:RB->cframe, KBASEa
  633. | mov SAVE_PC, RD // Any value outside of bytecode is ok.
  634. | mov SAVE_CFRAME, RDa
  635. |.if X64
  636. | mov SAVE_NRES, RD
  637. | mov SAVE_ERRF, RD
  638. |.endif
  639. | cmp byte L:RB->status, RDL
  640. | je >3 // Initial resume (like a call).
  641. |
  642. | // Resume after yield (like a return).
  643. | set_vmstate INTERP
  644. | mov byte L:RB->status, RDL
  645. | mov BASE, L:RB->base
  646. | mov RD, L:RB->top
  647. | sub RD, RA
  648. | shr RD, 3
  649. | add RD, 1 // RD = nresults+1
  650. | sub RA, BASE // RA = resultofs
  651. | mov PC, [BASE-4]
  652. | mov MULTRES, RD
  653. | test PC, FRAME_TYPE
  654. | jz ->BC_RET_Z
  655. | jmp ->vm_return
  656. |
  657. |->vm_pcall: // Setup protected C frame and enter VM.
  658. | // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef)
  659. | saveregs
  660. | mov PC, FRAME_CP
  661. |.if X64
  662. | mov SAVE_ERRF, CARG4d
  663. |.endif
  664. | jmp >1
  665. |
  666. |->vm_call: // Setup C frame and enter VM.
  667. | // (lua_State *L, TValue *base, int nres1)
  668. | saveregs
  669. | mov PC, FRAME_C
  670. |
  671. |1: // Entry point for vm_pcall above (PC = ftype).
  672. |.if X64
  673. | mov SAVE_NRES, CARG3d
  674. | mov L:RB, CARG1d // Caveat: CARG1d may be RA.
  675. | mov SAVE_L, CARG1d
  676. | mov RA, CARG2d
  677. |.else
  678. | mov L:RB, SAVE_L
  679. | mov RA, INARG_BASE // Caveat: overlaps SAVE_CFRAME!
  680. |.endif
  681. |
  682. | mov KBASEa, L:RB->cframe // Add our C frame to cframe chain.
  683. | mov SAVE_CFRAME, KBASEa
  684. | mov SAVE_PC, L:RB // Any value outside of bytecode is ok.
  685. |.if X64
  686. | mov L:RB->cframe, rsp
  687. |.else
  688. | mov L:RB->cframe, esp
  689. |.endif
  690. |
  691. |2: // Entry point for vm_cpcall below (RA = base, RB = L, PC = ftype).
  692. | mov DISPATCH, L:RB->glref // Setup pointer to dispatch table.
  693. | add DISPATCH, GG_G2DISP
  694. |
  695. |3: // Entry point for vm_resume above (RA = base, RB = L, PC = ftype).
  696. | set_vmstate INTERP
  697. | mov BASE, L:RB->base // BASE = old base (used in vmeta_call).
  698. | add PC, RA
  699. | sub PC, BASE // PC = frame delta + frame type
  700. |
  701. | mov RD, L:RB->top
  702. | sub RD, RA
  703. | shr NARGS:RD, 3
  704. | add NARGS:RD, 1 // RD = nargs+1
  705. |
  706. |->vm_call_dispatch:
  707. | mov LFUNC:RB, [RA-8]
  708. | cmp dword [RA-4], LJ_TFUNC
  709. | jne ->vmeta_call // Ensure KBASE defined and != BASE.
  710. |
  711. |->vm_call_dispatch_f:
  712. | mov BASE, RA
  713. | ins_call
  714. | // BASE = new base, RB = func, RD = nargs+1, PC = caller PC
  715. |
  716. |->vm_cpcall: // Setup protected C frame, call C.
  717. | // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp)
  718. | saveregs
  719. |.if X64
  720. | mov L:RB, CARG1d // Caveat: CARG1d may be RA.
  721. | mov SAVE_L, CARG1d
  722. |.else
  723. | mov L:RB, SAVE_L
  724. | // Caveat: INARG_CP_* and SAVE_CFRAME/SAVE_NRES/SAVE_ERRF overlap!
  725. | mov RC, INARG_CP_UD // Get args before they are overwritten.
  726. | mov RA, INARG_CP_FUNC
  727. | mov BASE, INARG_CP_CALL
  728. |.endif
  729. | mov SAVE_PC, L:RB // Any value outside of bytecode is ok.
  730. |
  731. | mov KBASE, L:RB->stack // Compute -savestack(L, L->top).
  732. | sub KBASE, L:RB->top
  733. | mov SAVE_ERRF, 0 // No error function.
  734. | mov SAVE_NRES, KBASE // Neg. delta means cframe w/o frame.
  735. | // Handler may change cframe_nres(L->cframe) or cframe_errfunc(L->cframe).
  736. |
  737. |.if X64
  738. | mov KBASEa, L:RB->cframe // Add our C frame to cframe chain.
  739. | mov SAVE_CFRAME, KBASEa
  740. | mov L:RB->cframe, rsp
  741. |
  742. | call CARG4 // (lua_State *L, lua_CFunction func, void *ud)
  743. |.else
  744. | mov ARG3, RC // Have to copy args downwards.
  745. | mov ARG2, RA
  746. | mov ARG1, L:RB
  747. |
  748. | mov KBASE, L:RB->cframe // Add our C frame to cframe chain.
  749. | mov SAVE_CFRAME, KBASE
  750. | mov L:RB->cframe, esp
  751. |
  752. | call BASE // (lua_State *L, lua_CFunction func, void *ud)
  753. |.endif
  754. | // TValue * (new base) or NULL returned in eax (RC).
  755. | test RC, RC
  756. | jz ->vm_leave_cp // No base? Just remove C frame.
  757. | mov RA, RC
  758. | mov PC, FRAME_CP
  759. | jmp <2 // Else continue with the call.
  760. |
  761. |//-----------------------------------------------------------------------
  762. |//-- Metamethod handling ------------------------------------------------
  763. |//-----------------------------------------------------------------------
  764. |
  765. |//-- Continuation dispatch ----------------------------------------------
  766. |
  767. |->cont_dispatch:
  768. | // BASE = meta base, RA = resultofs, RD = nresults+1 (also in MULTRES)
  769. | add RA, BASE
  770. | and PC, -8
  771. | mov RB, BASE
  772. | sub BASE, PC // Restore caller BASE.
  773. | mov dword [RA+RD*8-4], LJ_TNIL // Ensure one valid arg.
  774. | mov RC, RA // ... in [RC]
  775. | mov PC, [RB-12] // Restore PC from [cont|PC].
  776. |.if X64
  777. | movsxd RAa, dword [RB-16] // May be negative on WIN64 with debug.
  778. |.if FFI
  779. | cmp RA, 1
  780. | jbe >1
  781. |.endif
  782. | lea KBASEa, qword [=>0]
  783. | add RAa, KBASEa
  784. |.else
  785. | mov RA, dword [RB-16]
  786. |.if FFI
  787. | cmp RA, 1
  788. | jbe >1
  789. |.endif
  790. |.endif
  791. | mov LFUNC:KBASE, [BASE-8]
  792. | mov KBASE, LFUNC:KBASE->pc
  793. | mov KBASE, [KBASE+PC2PROTO(k)]
  794. | // BASE = base, RC = result, RB = meta base
  795. | jmp RAa // Jump to continuation.
  796. |
  797. |.if FFI
  798. |1:
  799. | je ->cont_ffi_callback // cont = 1: return from FFI callback.
  800. | // cont = 0: Tail call from C function.
  801. | sub RB, BASE
  802. | shr RB, 3
  803. | lea RD, [RB-1]
  804. | jmp ->vm_call_tail
  805. |.endif
  806. |
  807. |->cont_cat: // BASE = base, RC = result, RB = mbase
  808. | movzx RA, PC_RB
  809. | sub RB, 16
  810. | lea RA, [BASE+RA*8]
  811. | sub RA, RB
  812. | je ->cont_ra
  813. | neg RA
  814. | shr RA, 3
  815. |.if X64WIN
  816. | mov CARG3d, RA
  817. | mov L:CARG1d, SAVE_L
  818. | mov L:CARG1d->base, BASE
  819. | mov RCa, [RC]
  820. | mov [RB], RCa
  821. | mov CARG2d, RB
  822. |.elif X64
  823. | mov L:CARG1d, SAVE_L
  824. | mov L:CARG1d->base, BASE
  825. | mov CARG3d, RA
  826. | mov RAa, [RC]
  827. | mov [RB], RAa
  828. | mov CARG2d, RB
  829. |.else
  830. | mov ARG3, RA
  831. | mov RA, [RC+4]
  832. | mov RC, [RC]
  833. | mov [RB+4], RA
  834. | mov [RB], RC
  835. | mov ARG2, RB
  836. |.endif
  837. | jmp ->BC_CAT_Z
  838. |
  839. |//-- Table indexing metamethods -----------------------------------------
  840. |
  841. |->vmeta_tgets:
  842. | mov TMP1, RC // RC = GCstr *
  843. | mov TMP2, LJ_TSTR
  844. | lea RCa, TMP1 // Store temp. TValue in TMP1/TMP2.
  845. | cmp PC_OP, BC_GGET
  846. | jne >1
  847. | lea RA, [DISPATCH+DISPATCH_GL(tmptv)] // Store fn->l.env in g->tmptv.
  848. | mov [RA], TAB:RB // RB = GCtab *
  849. | mov dword [RA+4], LJ_TTAB
  850. | mov RB, RA
  851. | jmp >2
  852. |
  853. |->vmeta_tgetb:
  854. | movzx RC, PC_RC
  855. |.if DUALNUM
  856. | mov TMP2, LJ_TISNUM
  857. | mov TMP1, RC
  858. |.elif SSE
  859. | cvtsi2sd xmm0, RC
  860. | movsd TMPQ, xmm0
  861. |.else
  862. | mov ARG4, RC
  863. | fild ARG4
  864. | fstp TMPQ
  865. |.endif
  866. | lea RCa, TMPQ // Store temp. TValue in TMPQ.
  867. | jmp >1
  868. |
  869. |->vmeta_tgetv:
  870. | movzx RC, PC_RC // Reload TValue *k from RC.
  871. | lea RC, [BASE+RC*8]
  872. |1:
  873. | movzx RB, PC_RB // Reload TValue *t from RB.
  874. | lea RB, [BASE+RB*8]
  875. |2:
  876. |.if X64
  877. | mov L:CARG1d, SAVE_L
  878. | mov L:CARG1d->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
  879. | mov CARG2d, RB
  880. | mov CARG3, RCa // May be 64 bit ptr to stack.
  881. | mov L:RB, L:CARG1d
  882. |.else
  883. | mov ARG2, RB
  884. | mov L:RB, SAVE_L
  885. | mov ARG3, RC
  886. | mov ARG1, L:RB
  887. | mov L:RB->base, BASE
  888. |.endif
  889. | mov SAVE_PC, PC
  890. | call extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k)
  891. | // TValue * (finished) or NULL (metamethod) returned in eax (RC).
  892. | mov BASE, L:RB->base
  893. | test RC, RC
  894. | jz >3
  895. |->cont_ra: // BASE = base, RC = result
  896. | movzx RA, PC_RA
  897. |.if X64
  898. | mov RBa, [RC]
  899. | mov [BASE+RA*8], RBa
  900. |.else
  901. | mov RB, [RC+4]
  902. | mov RC, [RC]
  903. | mov [BASE+RA*8+4], RB
  904. | mov [BASE+RA*8], RC
  905. |.endif
  906. | ins_next
  907. |
  908. |3: // Call __index metamethod.
  909. | // BASE = base, L->top = new base, stack = cont/func/t/k
  910. | mov RA, L:RB->top
  911. | mov [RA-12], PC // [cont|PC]
  912. | lea PC, [RA+FRAME_CONT]
  913. | sub PC, BASE
  914. | mov LFUNC:RB, [RA-8] // Guaranteed to be a function here.
  915. | mov NARGS:RD, 2+1 // 2 args for func(t, k).
  916. | jmp ->vm_call_dispatch_f
  917. |
  918. |//-----------------------------------------------------------------------
  919. |
  920. |->vmeta_tsets:
  921. | mov TMP1, RC // RC = GCstr *
  922. | mov TMP2, LJ_TSTR
  923. | lea RCa, TMP1 // Store temp. TValue in TMP1/TMP2.
  924. | cmp PC_OP, BC_GSET
  925. | jne >1
  926. | lea RA, [DISPATCH+DISPATCH_GL(tmptv)] // Store fn->l.env in g->tmptv.
  927. | mov [RA], TAB:RB // RB = GCtab *
  928. | mov dword [RA+4], LJ_TTAB
  929. | mov RB, RA
  930. | jmp >2
  931. |
  932. |->vmeta_tsetb:
  933. | movzx RC, PC_RC
  934. |.if DUALNUM
  935. | mov TMP2, LJ_TISNUM
  936. | mov TMP1, RC
  937. |.elif SSE
  938. | cvtsi2sd xmm0, RC
  939. | movsd TMPQ, xmm0
  940. |.else
  941. | mov ARG4, RC
  942. | fild ARG4
  943. | fstp TMPQ
  944. |.endif
  945. | lea RCa, TMPQ // Store temp. TValue in TMPQ.
  946. | jmp >1
  947. |
  948. |->vmeta_tsetv:
  949. | movzx RC, PC_RC // Reload TValue *k from RC.
  950. | lea RC, [BASE+RC*8]
  951. |1:
  952. | movzx RB, PC_RB // Reload TValue *t from RB.
  953. | lea RB, [BASE+RB*8]
  954. |2:
  955. |.if X64
  956. | mov L:CARG1d, SAVE_L
  957. | mov L:CARG1d->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
  958. | mov CARG2d, RB
  959. | mov CARG3, RCa // May be 64 bit ptr to stack.
  960. | mov L:RB, L:CARG1d
  961. |.else
  962. | mov ARG2, RB
  963. | mov L:RB, SAVE_L
  964. | mov ARG3, RC
  965. | mov ARG1, L:RB
  966. | mov L:RB->base, BASE
  967. |.endif
  968. | mov SAVE_PC, PC
  969. | call extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k)
  970. | // TValue * (finished) or NULL (metamethod) returned in eax (RC).
  971. | mov BASE, L:RB->base
  972. | test RC, RC
  973. | jz >3
  974. | // NOBARRIER: lj_meta_tset ensures the table is not black.
  975. | movzx RA, PC_RA
  976. |.if X64
  977. | mov RBa, [BASE+RA*8]
  978. | mov [RC], RBa
  979. |.else
  980. | mov RB, [BASE+RA*8+4]
  981. | mov RA, [BASE+RA*8]
  982. | mov [RC+4], RB
  983. | mov [RC], RA
  984. |.endif
  985. |->cont_nop: // BASE = base, (RC = result)
  986. | ins_next
  987. |
  988. |3: // Call __newindex metamethod.
  989. | // BASE = base, L->top = new base, stack = cont/func/t/k/(v)
  990. | mov RA, L:RB->top
  991. | mov [RA-12], PC // [cont|PC]
  992. | movzx RC, PC_RA
  993. | // Copy value to third argument.
  994. |.if X64
  995. | mov RBa, [BASE+RC*8]
  996. | mov [RA+16], RBa
  997. |.else
  998. | mov RB, [BASE+RC*8+4]
  999. | mov RC, [BASE+RC*8]
  1000. | mov [RA+20], RB
  1001. | mov [RA+16], RC
  1002. |.endif
  1003. | lea PC, [RA+FRAME_CONT]
  1004. | sub PC, BASE
  1005. | mov LFUNC:RB, [RA-8] // Guaranteed to be a function here.
  1006. | mov NARGS:RD, 3+1 // 3 args for func(t, k, v).
  1007. | jmp ->vm_call_dispatch_f
  1008. |
  1009. |//-- Comparison metamethods ---------------------------------------------
  1010. |
  1011. |->vmeta_comp:
  1012. |.if X64
  1013. | mov L:RB, SAVE_L
  1014. | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d == BASE.
  1015. |.if X64WIN
  1016. | lea CARG3d, [BASE+RD*8]
  1017. | lea CARG2d, [BASE+RA*8]
  1018. |.else
  1019. | lea CARG2d, [BASE+RA*8]
  1020. | lea CARG3d, [BASE+RD*8]
  1021. |.endif
  1022. | mov CARG1d, L:RB // Caveat: CARG1d/CARG4d == RA.
  1023. | movzx CARG4d, PC_OP
  1024. |.else
  1025. | movzx RB, PC_OP
  1026. | lea RD, [BASE+RD*8]
  1027. | lea RA, [BASE+RA*8]
  1028. | mov ARG4, RB
  1029. | mov L:RB, SAVE_L
  1030. | mov ARG3, RD
  1031. | mov ARG2, RA
  1032. | mov ARG1, L:RB
  1033. | mov L:RB->base, BASE
  1034. |.endif
  1035. | mov SAVE_PC, PC
  1036. | call extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op)
  1037. | // 0/1 or TValue * (metamethod) returned in eax (RC).
  1038. |3:
  1039. | mov BASE, L:RB->base
  1040. | cmp RC, 1
  1041. | ja ->vmeta_binop
  1042. |4:
  1043. | lea PC, [PC+4]
  1044. | jb >6
  1045. |5:
  1046. | movzx RD, PC_RD
  1047. | branchPC RD
  1048. |6:
  1049. | ins_next
  1050. |
  1051. |->cont_condt: // BASE = base, RC = result
  1052. | add PC, 4
  1053. | cmp dword [RC+4], LJ_TISTRUECOND // Branch if result is true.
  1054. | jb <5
  1055. | jmp <6
  1056. |
  1057. |->cont_condf: // BASE = base, RC = result
  1058. | cmp dword [RC+4], LJ_TISTRUECOND // Branch if result is false.
  1059. | jmp <4
  1060. |
  1061. |->vmeta_equal:
  1062. | sub PC, 4
  1063. |.if X64WIN
  1064. | mov CARG3d, RD
  1065. | mov CARG4d, RB
  1066. | mov L:RB, SAVE_L
  1067. | mov L:RB->base, BASE // Caveat: CARG2d == BASE.
  1068. | mov CARG2d, RA
  1069. | mov CARG1d, L:RB // Caveat: CARG1d == RA.
  1070. |.elif X64
  1071. | mov CARG2d, RA
  1072. | mov CARG4d, RB // Caveat: CARG4d == RA.
  1073. | mov L:RB, SAVE_L
  1074. | mov L:RB->base, BASE // Caveat: CARG3d == BASE.
  1075. | mov CARG3d, RD
  1076. | mov CARG1d, L:RB
  1077. |.else
  1078. | mov ARG4, RB
  1079. | mov L:RB, SAVE_L
  1080. | mov ARG3, RD
  1081. | mov ARG2, RA
  1082. | mov ARG1, L:RB
  1083. | mov L:RB->base, BASE
  1084. |.endif
  1085. | mov SAVE_PC, PC
  1086. | call extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne)
  1087. | // 0/1 or TValue * (metamethod) returned in eax (RC).
  1088. | jmp <3
  1089. |
  1090. |->vmeta_equal_cd:
  1091. |.if FFI
  1092. | sub PC, 4
  1093. | mov L:RB, SAVE_L
  1094. | mov L:RB->base, BASE
  1095. | mov FCARG1, L:RB
  1096. | mov FCARG2, dword [PC-4]
  1097. | mov SAVE_PC, PC
  1098. | call extern lj_meta_equal_cd@8 // (lua_State *L, BCIns ins)
  1099. | // 0/1 or TValue * (metamethod) returned in eax (RC).
  1100. | jmp <3
  1101. |.endif
  1102. |
  1103. |//-- Arithmetic metamethods ---------------------------------------------
  1104. |
  1105. |->vmeta_arith_vno:
  1106. |.if DUALNUM
  1107. | movzx RB, PC_RB
  1108. |.endif
  1109. |->vmeta_arith_vn:
  1110. | lea RC, [KBASE+RC*8]
  1111. | jmp >1
  1112. |
  1113. |->vmeta_arith_nvo:
  1114. |.if DUALNUM
  1115. | movzx RC, PC_RC
  1116. |.endif
  1117. |->vmeta_arith_nv:
  1118. | lea RC, [KBASE+RC*8]
  1119. | lea RB, [BASE+RB*8]
  1120. | xchg RB, RC
  1121. | jmp >2
  1122. |
  1123. |->vmeta_unm:
  1124. | lea RC, [BASE+RD*8]
  1125. | mov RB, RC
  1126. | jmp >2
  1127. |
  1128. |->vmeta_arith_vvo:
  1129. |.if DUALNUM
  1130. | movzx RB, PC_RB
  1131. |.endif
  1132. |->vmeta_arith_vv:
  1133. | lea RC, [BASE+RC*8]
  1134. |1:
  1135. | lea RB, [BASE+RB*8]
  1136. |2:
  1137. | lea RA, [BASE+RA*8]
  1138. |.if X64WIN
  1139. | mov CARG3d, RB
  1140. | mov CARG4d, RC
  1141. | movzx RC, PC_OP
  1142. | mov ARG5d, RC
  1143. | mov L:RB, SAVE_L
  1144. | mov L:RB->base, BASE // Caveat: CARG2d == BASE.
  1145. | mov CARG2d, RA
  1146. | mov CARG1d, L:RB // Caveat: CARG1d == RA.
  1147. |.elif X64
  1148. | movzx CARG5d, PC_OP
  1149. | mov CARG2d, RA
  1150. | mov CARG4d, RC // Caveat: CARG4d == RA.
  1151. | mov L:CARG1d, SAVE_L
  1152. | mov L:CARG1d->base, BASE // Caveat: CARG3d == BASE.
  1153. | mov CARG3d, RB
  1154. | mov L:RB, L:CARG1d
  1155. |.else
  1156. | mov ARG3, RB
  1157. | mov L:RB, SAVE_L
  1158. | mov ARG4, RC
  1159. | movzx RC, PC_OP
  1160. | mov ARG2, RA
  1161. | mov ARG5, RC
  1162. | mov ARG1, L:RB
  1163. | mov L:RB->base, BASE
  1164. |.endif
  1165. | mov SAVE_PC, PC
  1166. | call extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op)
  1167. | // NULL (finished) or TValue * (metamethod) returned in eax (RC).
  1168. | mov BASE, L:RB->base
  1169. | test RC, RC
  1170. | jz ->cont_nop
  1171. |
  1172. | // Call metamethod for binary op.
  1173. |->vmeta_binop:
  1174. | // BASE = base, RC = new base, stack = cont/func/o1/o2
  1175. | mov RA, RC
  1176. | sub RC, BASE
  1177. | mov [RA-12], PC // [cont|PC]
  1178. | lea PC, [RC+FRAME_CONT]
  1179. | mov NARGS:RD, 2+1 // 2 args for func(o1, o2).
  1180. | jmp ->vm_call_dispatch
  1181. |
  1182. |->vmeta_len:
  1183. | mov L:RB, SAVE_L
  1184. | mov L:RB->base, BASE
  1185. | lea FCARG2, [BASE+RD*8] // Caveat: FCARG2 == BASE
  1186. | mov L:FCARG1, L:RB
  1187. | mov SAVE_PC, PC
  1188. | call extern lj_meta_len@8 // (lua_State *L, TValue *o)
  1189. | // NULL (retry) or TValue * (metamethod) returned in eax (RC).
  1190. | mov BASE, L:RB->base
  1191. #if LJ_52
  1192. | test RC, RC
  1193. | jne ->vmeta_binop // Binop call for compatibility.
  1194. | movzx RD, PC_RD
  1195. | mov TAB:FCARG1, [BASE+RD*8]
  1196. | jmp ->BC_LEN_Z
  1197. #else
  1198. | jmp ->vmeta_binop // Binop call for compatibility.
  1199. #endif
  1200. |
  1201. |//-- Call metamethod ----------------------------------------------------
  1202. |
  1203. |->vmeta_call_ra:
  1204. | lea RA, [BASE+RA*8+8]
  1205. |->vmeta_call: // Resolve and call __call metamethod.
  1206. | // BASE = old base, RA = new base, RC = nargs+1, PC = return
  1207. | mov TMP2, RA // Save RA, RC for us.
  1208. | mov TMP1, NARGS:RD
  1209. | sub RA, 8
  1210. |.if X64
  1211. | mov L:RB, SAVE_L
  1212. | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
  1213. | mov CARG2d, RA
  1214. | lea CARG3d, [RA+NARGS:RD*8]
  1215. | mov CARG1d, L:RB // Caveat: CARG1d may be RA.
  1216. |.else
  1217. | lea RC, [RA+NARGS:RD*8]
  1218. | mov L:RB, SAVE_L
  1219. | mov ARG2, RA
  1220. | mov ARG3, RC
  1221. | mov ARG1, L:RB
  1222. | mov L:RB->base, BASE // This is the callers base!
  1223. |.endif
  1224. | mov SAVE_PC, PC
  1225. | call extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
  1226. | mov BASE, L:RB->base
  1227. | mov RA, TMP2
  1228. | mov NARGS:RD, TMP1
  1229. | mov LFUNC:RB, [RA-8]
  1230. | add NARGS:RD, 1
  1231. | // This is fragile. L->base must not move, KBASE must always be defined.
  1232. | cmp KBASE, BASE // Continue with CALLT if flag set.
  1233. | je ->BC_CALLT_Z
  1234. | mov BASE, RA
  1235. | ins_call // Otherwise call resolved metamethod.
  1236. |
  1237. |//-- Argument coercion for 'for' statement ------------------------------
  1238. |
  1239. |->vmeta_for:
  1240. | mov L:RB, SAVE_L
  1241. | mov L:RB->base, BASE
  1242. | mov FCARG2, RA // Caveat: FCARG2 == BASE
  1243. | mov L:FCARG1, L:RB // Caveat: FCARG1 == RA
  1244. | mov SAVE_PC, PC
  1245. | call extern lj_meta_for@8 // (lua_State *L, TValue *base)
  1246. | mov BASE, L:RB->base
  1247. | mov RC, [PC-4]
  1248. | movzx RA, RCH
  1249. | movzx OP, RCL
  1250. | shr RC, 16
  1251. |.if X64
  1252. | jmp aword [DISPATCH+OP*8+GG_DISP2STATIC] // Retry FORI or JFORI.
  1253. |.else
  1254. | jmp aword [DISPATCH+OP*4+GG_DISP2STATIC] // Retry FORI or JFORI.
  1255. |.endif
  1256. |
  1257. |//-----------------------------------------------------------------------
  1258. |//-- Fast functions -----------------------------------------------------
  1259. |//-----------------------------------------------------------------------
  1260. |
  1261. |.macro .ffunc, name
  1262. |->ff_ .. name:
  1263. |.endmacro
  1264. |
  1265. |.macro .ffunc_1, name
  1266. |->ff_ .. name:
  1267. | cmp NARGS:RD, 1+1; jb ->fff_fallback
  1268. |.endmacro
  1269. |
  1270. |.macro .ffunc_2, name
  1271. |->ff_ .. name:
  1272. | cmp NARGS:RD, 2+1; jb ->fff_fallback
  1273. |.endmacro
  1274. |
  1275. |.macro .ffunc_n, name
  1276. | .ffunc_1 name
  1277. | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
  1278. | fld qword [BASE]
  1279. |.endmacro
  1280. |
  1281. |.macro .ffunc_n, name, op
  1282. | .ffunc_1 name
  1283. | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
  1284. | op
  1285. | fld qword [BASE]
  1286. |.endmacro
  1287. |
  1288. |.macro .ffunc_nsse, name, op
  1289. | .ffunc_1 name
  1290. | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
  1291. | op xmm0, qword [BASE]
  1292. |.endmacro
  1293. |
  1294. |.macro .ffunc_nsse, name
  1295. | .ffunc_nsse name, movsd
  1296. |.endmacro
  1297. |
  1298. |.macro .ffunc_nn, name
  1299. | .ffunc_2 name
  1300. | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
  1301. | cmp dword [BASE+12], LJ_TISNUM; jae ->fff_fallback
  1302. | fld qword [BASE]
  1303. | fld qword [BASE+8]
  1304. |.endmacro
  1305. |
  1306. |.macro .ffunc_nnsse, name
  1307. | .ffunc_2 name
  1308. | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
  1309. | cmp dword [BASE+12], LJ_TISNUM; jae ->fff_fallback
  1310. | movsd xmm0, qword [BASE]
  1311. | movsd xmm1, qword [BASE+8]
  1312. |.endmacro
  1313. |
  1314. |.macro .ffunc_nnr, name
  1315. | .ffunc_2 name
  1316. | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
  1317. | cmp dword [BASE+12], LJ_TISNUM; jae ->fff_fallback
  1318. | fld qword [BASE+8]
  1319. | fld qword [BASE]
  1320. |.endmacro
  1321. |
  1322. |// Inlined GC threshold check. Caveat: uses label 1.
  1323. |.macro ffgccheck
  1324. | mov RB, [DISPATCH+DISPATCH_GL(gc.total)]
  1325. | cmp RB, [DISPATCH+DISPATCH_GL(gc.threshold)]
  1326. | jb >1
  1327. | call ->fff_gcstep
  1328. |1:
  1329. |.endmacro
  1330. |
  1331. |//-- Base library: checks -----------------------------------------------
  1332. |
  1333. |.ffunc_1 assert
  1334. | mov RB, [BASE+4]
  1335. | cmp RB, LJ_TISTRUECOND; jae ->fff_fallback
  1336. | mov PC, [BASE-4]
  1337. | mov MULTRES, RD
  1338. | mov [BASE-4], RB
  1339. | mov RB, [BASE]
  1340. | mov [BASE-8], RB
  1341. | sub RD, 2
  1342. | jz >2
  1343. | mov RA, BASE
  1344. |1:
  1345. | add RA, 8
  1346. |.if X64
  1347. | mov RBa, [RA]
  1348. | mov [RA-8], RBa
  1349. |.else
  1350. | mov RB, [RA+4]
  1351. | mov [RA-4], RB
  1352. | mov RB, [RA]
  1353. | mov [RA-8], RB
  1354. |.endif
  1355. | sub RD, 1
  1356. | jnz <1
  1357. |2:
  1358. | mov RD, MULTRES
  1359. | jmp ->fff_res_
  1360. |
  1361. |.ffunc_1 type
  1362. | mov RB, [BASE+4]
  1363. |.if X64
  1364. | mov RA, RB
  1365. | sar RA, 15
  1366. | cmp RA, -2
  1367. | je >3
  1368. |.endif
  1369. | mov RC, ~LJ_TNUMX
  1370. | not RB
  1371. | cmp RC, RB
  1372. | cmova RC, RB
  1373. |2:
  1374. | mov CFUNC:RB, [BASE-8]
  1375. | mov STR:RC, [CFUNC:RB+RC*8+((char *)(&((GCfuncC *)0)->upvalue))]
  1376. | mov PC, [BASE-4]
  1377. | mov dword [BASE-4], LJ_TSTR
  1378. | mov [BASE-8], STR:RC
  1379. | jmp ->fff_res1
  1380. |.if X64
  1381. |3:
  1382. | mov RC, ~LJ_TLIGHTUD
  1383. | jmp <2
  1384. |.endif
  1385. |
  1386. |//-- Base library: getters and setters ---------------------------------
  1387. |
  1388. |.ffunc_1 getmetatable
  1389. | mov RB, [BASE+4]
  1390. | mov PC, [BASE-4]
  1391. | cmp RB, LJ_TTAB; jne >6
  1392. |1: // Field metatable must be at same offset for GCtab and GCudata!
  1393. | mov TAB:RB, [BASE]
  1394. | mov TAB:RB, TAB:RB->metatable
  1395. |2:
  1396. | test TAB:RB, TAB:RB
  1397. | mov dword [BASE-4], LJ_TNIL
  1398. | jz ->fff_res1
  1399. | mov STR:RC, [DISPATCH+DISPATCH_GL(gcroot)+4*(GCROOT_MMNAME+MM_metatable)]
  1400. | mov dword [BASE-4], LJ_TTAB // Store metatable as default result.
  1401. | mov [BASE-8], TAB:RB
  1402. | mov RA, TAB:RB->hmask
  1403. | and RA, STR:RC->hash
  1404. | imul RA, #NODE
  1405. | add NODE:RA, TAB:RB->node
  1406. |3: // Rearranged logic, because we expect _not_ to find the key.
  1407. | cmp dword NODE:RA->key.it, LJ_TSTR
  1408. | jne >4
  1409. | cmp dword NODE:RA->key.gcr, STR:RC
  1410. | je >5
  1411. |4:
  1412. | mov NODE:RA, NODE:RA->next
  1413. | test NODE:RA, NODE:RA
  1414. | jnz <3
  1415. | jmp ->fff_res1 // Not found, keep default result.
  1416. |5:
  1417. | mov RB, [RA+4]
  1418. | cmp RB, LJ_TNIL; je ->fff_res1 // Ditto for nil value.
  1419. | mov RC, [RA]
  1420. | mov [BASE-4], RB // Return value of mt.__metatable.
  1421. | mov [BASE-8], RC
  1422. | jmp ->fff_res1
  1423. |
  1424. |6:
  1425. | cmp RB, LJ_TUDATA; je <1
  1426. |.if X64
  1427. | cmp RB, LJ_TNUMX; ja >8
  1428. | cmp RB, LJ_TISNUM; jbe >7
  1429. | mov RB, LJ_TLIGHTUD
  1430. | jmp >8
  1431. |7:
  1432. |.else
  1433. | cmp RB, LJ_TISNUM; ja >8
  1434. |.endif
  1435. | mov RB, LJ_TNUMX
  1436. |8:
  1437. | not RB
  1438. | mov TAB:RB, [DISPATCH+RB*4+DISPATCH_GL(gcroot[GCROOT_BASEMT])]
  1439. | jmp <2
  1440. |
  1441. |.ffunc_2 setmetatable
  1442. | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
  1443. | // Fast path: no mt for table yet and not clearing the mt.
  1444. | mov TAB:RB, [BASE]
  1445. | cmp dword TAB:RB->metatable, 0; jne ->fff_fallback
  1446. | cmp dword [BASE+12], LJ_TTAB; jne ->fff_fallback
  1447. | mov TAB:RC, [BASE+8]
  1448. | mov TAB:RB->metatable, TAB:RC
  1449. | mov PC, [BASE-4]
  1450. | mov dword [BASE-4], LJ_TTAB // Return original table.
  1451. | mov [BASE-8], TAB:RB
  1452. | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
  1453. | jz >1
  1454. | // Possible write barrier. Table is black, but skip iswhite(mt) check.
  1455. | barrierback TAB:RB, RC
  1456. |1:
  1457. | jmp ->fff_res1
  1458. |
  1459. |.ffunc_2 rawget
  1460. | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
  1461. |.if X64WIN
  1462. | mov RB, BASE // Save BASE.
  1463. | lea CARG3d, [BASE+8]
  1464. | mov CARG2d, [BASE] // Caveat: CARG2d == BASE.
  1465. | mov CARG1d, SAVE_L
  1466. |.elif X64
  1467. | mov RB, BASE // Save BASE.
  1468. | mov CARG2d, [BASE]
  1469. | lea CARG3d, [BASE+8] // Caveat: CARG3d == BASE.
  1470. | mov CARG1d, SAVE_L
  1471. |.else
  1472. | mov TAB:RD, [BASE]
  1473. | mov L:RB, SAVE_L
  1474. | mov ARG2, TAB:RD
  1475. | mov ARG1, L:RB
  1476. | mov RB, BASE // Save BASE.
  1477. | add BASE, 8
  1478. | mov ARG3, BASE
  1479. |.endif
  1480. | call extern lj_tab_get // (lua_State *L, GCtab *t, cTValue *key)
  1481. | // cTValue * returned in eax (RD).
  1482. | mov BASE, RB // Restore BASE.
  1483. | // Copy table slot.
  1484. |.if X64
  1485. | mov RBa, [RD]
  1486. | mov PC, [BASE-4]
  1487. | mov [BASE-8], RBa
  1488. |.else
  1489. | mov RB, [RD]
  1490. | mov RD, [RD+4]
  1491. | mov PC, [BASE-4]
  1492. | mov [BASE-8], RB
  1493. | mov [BASE-4], RD
  1494. |.endif
  1495. | jmp ->fff_res1
  1496. |
  1497. |//-- Base library: conversions ------------------------------------------
  1498. |
  1499. |.ffunc tonumber
  1500. | // Only handles the number case inline (without a base argument).
  1501. | cmp NARGS:RD, 1+1; jne ->fff_fallback // Exactly one argument.
  1502. | cmp dword [BASE+4], LJ_TISNUM
  1503. |.if DUALNUM
  1504. | jne >1
  1505. | mov RB, dword [BASE]; jmp ->fff_resi
  1506. |1:
  1507. | ja ->fff_fallback
  1508. |.else
  1509. | jae ->fff_fallback
  1510. |.endif
  1511. |.if SSE
  1512. | movsd xmm0, qword [BASE]; jmp ->fff_resxmm0
  1513. |.else
  1514. | fld qword [BASE]; jmp ->fff_resn
  1515. |.endif
  1516. |
  1517. |.ffunc_1 tostring
  1518. | // Only handles the string or number case inline.
  1519. | mov PC, [BASE-4]
  1520. | cmp dword [BASE+4], LJ_TSTR; jne >3
  1521. | // A __tostring method in the string base metatable is ignored.
  1522. | mov STR:RD, [BASE]
  1523. |2:
  1524. | mov dword [BASE-4], LJ_TSTR
  1525. | mov [BASE-8], STR:RD
  1526. | jmp ->fff_res1
  1527. |3: // Handle numbers inline, unless a number base metatable is present.
  1528. | cmp dword [BASE+4], LJ_TISNUM; ja ->fff_fallback
  1529. | cmp dword [DISPATCH+DISPATCH_GL(gcroot[GCROOT_BASEMT_NUM])], 0
  1530. | jne ->fff_fallback
  1531. | ffgccheck // Caveat: uses label 1.
  1532. | mov L:RB, SAVE_L
  1533. | mov L:RB->base, BASE // Add frame since C call can throw.
  1534. | mov SAVE_PC, PC // Redundant (but a defined value).
  1535. |.if X64 and not X64WIN
  1536. | mov FCARG2, BASE // Otherwise: FCARG2 == BASE
  1537. |.endif
  1538. | mov L:FCARG1, L:RB
  1539. |.if DUALNUM
  1540. | call extern lj_str_fromnumber@8 // (lua_State *L, cTValue *o)
  1541. |.else
  1542. | call extern lj_str_fromnum@8 // (lua_State *L, lua_Number *np)
  1543. |.endif
  1544. | // GCstr returned in eax (RD).
  1545. | mov BASE, L:RB->base
  1546. | jmp <2
  1547. |
  1548. |//-- Base library: iterators -------------------------------------------
  1549. |
  1550. |.ffunc_1 next
  1551. | je >2 // Missing 2nd arg?
  1552. |1:
  1553. | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
  1554. | mov L:RB, SAVE_L
  1555. | mov L:RB->base, BASE // Add frame since C call can throw.
  1556. | mov L:RB->top, BASE // Dummy frame length is ok.
  1557. | mov PC, [BASE-4]
  1558. |.if X64WIN
  1559. | lea CARG3d, [BASE+8]
  1560. | mov CARG2d, [BASE] // Caveat: CARG2d == BASE.
  1561. | mov CARG1d, L:RB
  1562. |.elif X64
  1563. | mov CARG2d, [BASE]
  1564. | lea CARG3d, [BASE+8] // Caveat: CARG3d == BASE.
  1565. | mov CARG1d, L:RB
  1566. |.else
  1567. | mov TAB:RD, [BASE]
  1568. | mov ARG2, TAB:RD
  1569. | mov ARG1, L:RB
  1570. | add BASE, 8
  1571. | mov ARG3, BASE
  1572. |.endif
  1573. | mov SAVE_PC, PC // Needed for ITERN fallback.
  1574. | call extern lj_tab_next // (lua_State *L, GCtab *t, TValue *key)
  1575. | // Flag returned in eax (RD).
  1576. | mov BASE, L:RB->base
  1577. | test RD, RD; jz >3 // End of traversal?
  1578. | // Copy key and value to results.
  1579. |.if X64
  1580. | mov RBa, [BASE+8]
  1581. | mov RDa, [BASE+16]
  1582. | mov [BASE-8], RBa
  1583. | mov [BASE], RDa
  1584. |.else
  1585. | mov RB, [BASE+8]
  1586. | mov RD, [BASE+12]
  1587. | mov [BASE-8], RB
  1588. | mov [BASE-4], RD
  1589. | mov RB, [BASE+16]
  1590. | mov RD, [BASE+20]
  1591. | mov [BASE], RB
  1592. | mov [BASE+4], RD
  1593. |.endif
  1594. |->fff_res2:
  1595. | mov RD, 1+2
  1596. | jmp ->fff_res
  1597. |2: // Set missing 2nd arg to nil.
  1598. | mov dword [BASE+12], LJ_TNIL
  1599. | jmp <1
  1600. |3: // End of traversal: return nil.
  1601. | mov dword [BASE-4], LJ_TNIL
  1602. | jmp ->fff_res1
  1603. |
  1604. |.ffunc_1 pairs
  1605. | mov TAB:RB, [BASE]
  1606. | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
  1607. #if LJ_52
  1608. | cmp dword TAB:RB->metatable, 0; jne ->fff_fallback
  1609. #endif
  1610. | mov CFUNC:RB, [BASE-8]
  1611. | mov CFUNC:RD, CFUNC:RB->upvalue[0]
  1612. | mov PC, [BASE-4]
  1613. | mov dword [BASE-4], LJ_TFUNC
  1614. | mov [BASE-8], CFUNC:RD
  1615. | mov dword [BASE+12], LJ_TNIL
  1616. | mov RD, 1+3
  1617. | jmp ->fff_res
  1618. |
  1619. |.ffunc_1 ipairs_aux
  1620. | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
  1621. | cmp dword [BASE+12], LJ_TISNUM
  1622. |.if DUALNUM
  1623. | jne ->fff_fallback
  1624. |.else
  1625. | jae ->fff_fallback
  1626. |.endif
  1627. | mov PC, [BASE-4]
  1628. |.if DUALNUM
  1629. | mov RD, dword [BASE+8]
  1630. | add RD, 1
  1631. | mov dword [BASE-4], LJ_TISNUM
  1632. | mov dword [BASE-8], RD
  1633. |.elif SSE
  1634. | movsd xmm0, qword [BASE+8]
  1635. | sseconst_1 xmm1, RBa
  1636. | addsd xmm0, xmm1
  1637. | cvtsd2si RD, xmm0
  1638. | movsd qword [BASE-8], xmm0
  1639. |.else
  1640. | fld qword [BASE+8]
  1641. | fld1
  1642. | faddp st1
  1643. | fist ARG1
  1644. | fstp qword [BASE-8]
  1645. | mov RD, ARG1
  1646. |.endif
  1647. | mov TAB:RB, [BASE]
  1648. | cmp RD, TAB:RB->asize; jae >2 // Not in array part?
  1649. | shl RD, 3
  1650. | add RD, TAB:RB->array
  1651. |1:
  1652. | cmp dword [RD+4], LJ_TNIL; je ->fff_res0
  1653. | // Copy array slot.
  1654. |.if X64
  1655. | mov RBa, [RD]
  1656. | mov [BASE], RBa
  1657. |.else
  1658. | mov RB, [RD]
  1659. | mov RD, [RD+4]
  1660. | mov [BASE], RB
  1661. | mov [BASE+4], RD
  1662. |.endif
  1663. | jmp ->fff_res2
  1664. |2: // Check for empty hash part first. Otherwise call C function.
  1665. | cmp dword TAB:RB->hmask, 0; je ->fff_res0
  1666. | mov FCARG1, TAB:RB
  1667. | mov RB, BASE // Save BASE.
  1668. | mov FCARG2, RD // Caveat: FCARG2 == BASE
  1669. | call extern lj_tab_getinth@8 // (GCtab *t, int32_t key)
  1670. | // cTValue * or NULL returned in eax (RD).
  1671. | mov BASE, RB
  1672. | test RD, RD
  1673. | jnz <1
  1674. |->fff_res0:
  1675. | mov RD, 1+0
  1676. | jmp ->fff_res
  1677. |
  1678. |.ffunc_1 ipairs
  1679. | mov TAB:RB, [BASE]
  1680. | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
  1681. #if LJ_52
  1682. | cmp dword TAB:RB->metatable, 0; jne ->fff_fallback
  1683. #endif
  1684. | mov CFUNC:RB, [BASE-8]
  1685. | mov CFUNC:RD, CFUNC:RB->upvalue[0]
  1686. | mov PC, [BASE-4]
  1687. | mov dword [BASE-4], LJ_TFUNC
  1688. | mov [BASE-8], CFUNC:RD
  1689. |.if DUALNUM
  1690. | mov dword [BASE+12], LJ_TISNUM
  1691. | mov dword [BASE+8], 0
  1692. |.elif SSE
  1693. | xorps xmm0, xmm0
  1694. | movsd qword [BASE+8], xmm0
  1695. |.else
  1696. | fldz
  1697. | fstp qword [BASE+8]
  1698. |.endif
  1699. | mov RD, 1+3
  1700. | jmp ->fff_res
  1701. |
  1702. |//-- Base library: catch errors ----------------------------------------
  1703. |
  1704. |.ffunc_1 pcall
  1705. | lea RA, [BASE+8]
  1706. | sub NARGS:RD, 1
  1707. | mov PC, 8+FRAME_PCALL
  1708. |1:
  1709. | movzx RB, byte [DISPATCH+DISPATCH_GL(hookmask)]
  1710. | shr RB, HOOK_ACTIVE_SHIFT
  1711. | and RB, 1
  1712. | add PC, RB // Remember active hook before pcall.
  1713. | jmp ->vm_call_dispatch
  1714. |
  1715. |.ffunc_2 xpcall
  1716. | cmp dword [BASE+12], LJ_TFUNC; jne ->fff_fallback
  1717. | mov RB, [BASE+4] // Swap function and traceback.
  1718. | mov [BASE+12], RB
  1719. | mov dword [BASE+4], LJ_TFUNC
  1720. | mov LFUNC:RB, [BASE]
  1721. | mov PC, [BASE+8]
  1722. | mov [BASE+8], LFUNC:RB
  1723. | mov [BASE], PC
  1724. | lea RA, [BASE+16]
  1725. | sub NARGS:RD, 2
  1726. | mov PC, 16+FRAME_PCALL
  1727. | jmp <1
  1728. |
  1729. |//-- Coroutine library --------------------------------------------------
  1730. |
  1731. |.macro coroutine_resume_wrap, resume
  1732. |.if resume
  1733. |.ffunc_1 coroutine_resume
  1734. | mov L:RB, [BASE]
  1735. |.else
  1736. |.ffunc coroutine_wrap_aux
  1737. | mov CFUNC:RB, [BASE-8]
  1738. | mov L:RB, CFUNC:RB->upvalue[0].gcr
  1739. |.endif
  1740. | mov PC, [BASE-4]
  1741. | mov SAVE_PC, PC
  1742. |.if X64
  1743. | mov TMP1, L:RB
  1744. |.else
  1745. | mov ARG1, L:RB
  1746. |.endif
  1747. |.if resume
  1748. | cmp dword [BASE+4], LJ_TTHREAD; jne ->fff_fallback
  1749. |.endif
  1750. | cmp aword L:RB->cframe, 0; jne ->fff_fallback
  1751. | cmp byte L:RB->status, LUA_YIELD; ja ->fff_fallback
  1752. | mov RA, L:RB->top
  1753. | je >1 // Status != LUA_YIELD (i.e. 0)?
  1754. | cmp RA, L:RB->base // Check for presence of initial func.
  1755. | je ->fff_fallback
  1756. |1:
  1757. |.if resume
  1758. | lea PC, [RA+NARGS:RD*8-16] // Check stack space (-1-thread).
  1759. |.else
  1760. | lea PC, [RA+NARGS:RD*8-8] // Check stack space (-1).
  1761. |.endif
  1762. | cmp PC, L:RB->maxstack; ja ->fff_fallback
  1763. | mov L:RB->top, PC
  1764. |
  1765. | mov L:RB, SAVE_L
  1766. | mov L:RB->base, BASE
  1767. |.if resume
  1768. | add BASE, 8 // Keep resumed thread in stack for GC.
  1769. |.endif
  1770. | mov L:RB->top, BASE
  1771. |.if resume
  1772. | lea RB, [BASE+NARGS:RD*8-24] // RB = end of source for stack move.
  1773. |.else
  1774. | lea RB, [BASE+NARGS:RD*8-16] // RB = end of source for stack move.
  1775. |.endif
  1776. | sub RBa, PCa // Relative to PC.
  1777. |
  1778. | cmp PC, RA
  1779. | je >3
  1780. |2: // Move args to coroutine.
  1781. |.if X64
  1782. | mov RCa, [PC+RB]
  1783. | mov [PC-8], RCa
  1784. |.else
  1785. | mov RC, [PC+RB+4]
  1786. | mov [PC-4], RC
  1787. | mov RC, [PC+RB]
  1788. | mov [PC-8], RC
  1789. |.endif
  1790. | sub PC, 8
  1791. | cmp PC, RA
  1792. | jne <2
  1793. |3:
  1794. |.if X64
  1795. | mov CARG2d, RA
  1796. | mov CARG1d, TMP1
  1797. |.else
  1798. | mov ARG2, RA
  1799. | xor RA, RA
  1800. | mov ARG4, RA
  1801. | mov ARG3, RA
  1802. |.endif
  1803. | call ->vm_resume // (lua_State *L, TValue *base, 0, 0)
  1804. | set_vmstate INTERP
  1805. |
  1806. | mov L:RB, SAVE_L
  1807. |.if X64
  1808. | mov L:PC, TMP1
  1809. |.else
  1810. | mov L:PC, ARG1 // The callee doesn't modify SAVE_L.
  1811. |.endif
  1812. | mov BASE, L:RB->base
  1813. | cmp eax, LUA_YIELD
  1814. | ja >8
  1815. |4:
  1816. | mov RA, L:PC->base
  1817. | mov KBASE, L:PC->top
  1818. | mov L:PC->top, RA // Clear coroutine stack.
  1819. | mov PC, KBASE
  1820. | sub PC, RA
  1821. | je >6 // No results?
  1822. | lea RD, [BASE+PC]
  1823. | shr PC, 3
  1824. | cmp RD, L:RB->maxstack
  1825. | ja >9 // Need to grow stack?
  1826. |
  1827. | mov RB, BASE
  1828. | sub RBa, RAa
  1829. |5: // Move results from coroutine.
  1830. |.if X64
  1831. | mov RDa, [RA]
  1832. | mov [RA+RB], RDa
  1833. |.else
  1834. | mov RD, [RA]
  1835. | mov [RA+RB], RD
  1836. | mov RD, [RA+4]
  1837. | mov [RA+RB+4], RD
  1838. |.endif
  1839. | add RA, 8
  1840. | cmp RA, KBASE
  1841. | jne <5
  1842. |6:
  1843. |.if resume
  1844. | lea RD, [PC+2] // nresults+1 = 1 + true + results.
  1845. | mov dword [BASE-4], LJ_TTRUE // Prepend true to results.
  1846. |.else
  1847. | lea RD, [PC+1] // nresults+1 = 1 + results.
  1848. |.endif
  1849. |7:
  1850. | mov PC, SAVE_PC
  1851. | mov MULTRES, RD
  1852. |.if resume
  1853. | mov RAa, -8
  1854. |.else
  1855. | xor RA, RA
  1856. |.endif
  1857. | test PC, FRAME_TYPE
  1858. | jz ->BC_RET_Z
  1859. | jmp ->vm_return
  1860. |
  1861. |8: // Coroutine returned with error (at co->top-1).
  1862. |.if resume
  1863. | mov dword [BASE-4], LJ_TFALSE // Prepend false to results.
  1864. | mov RA, L:PC->top
  1865. | sub RA, 8
  1866. | mov L:PC->top, RA // Clear error from coroutine stack.
  1867. | // Copy error message.
  1868. |.if X64
  1869. | mov RDa, [RA]
  1870. | mov [BASE], RDa
  1871. |.else
  1872. | mov RD, [RA]
  1873. | mov [BASE], RD
  1874. | mov RD, [RA+4]
  1875. | mov [BASE+4], RD
  1876. |.endif
  1877. | mov RD, 1+2 // nresults+1 = 1 + false + error.
  1878. | jmp <7
  1879. |.else
  1880. | mov FCARG2, L:PC
  1881. | mov FCARG1, L:RB
  1882. | call extern lj_ffh_coroutine_wrap_err@8 // (lua_State *L, lua_State *co)
  1883. | // Error function does not return.
  1884. |.endif
  1885. |
  1886. |9: // Handle stack expansion on return from yield.
  1887. |.if X64
  1888. | mov L:RA, TMP1
  1889. |.else
  1890. | mov L:RA, ARG1 // The callee doesn't modify SAVE_L.
  1891. |.endif
  1892. | mov L:RA->top, KBASE // Undo coroutine stack clearing.
  1893. | mov FCARG2, PC
  1894. | mov FCARG1, L:RB
  1895. | call extern lj_state_growstack@8 // (lua_State *L, int n)
  1896. |.if X64
  1897. | mov L:PC, TMP1
  1898. |.else
  1899. | mov L:PC, ARG1
  1900. |.endif
  1901. | mov BASE, L:RB->base
  1902. | jmp <4 // Retry the stack move.
  1903. |.endmacro
  1904. |
  1905. | coroutine_resume_wrap 1 // coroutine.resume
  1906. | coroutine_resume_wrap 0 // coroutine.wrap
  1907. |
  1908. |.ffunc coroutine_yield
  1909. | mov L:RB, SAVE_L
  1910. | test aword L:RB->cframe, CFRAME_RESUME
  1911. | jz ->fff_fallback
  1912. | mov L:RB->base, BASE
  1913. | lea RD, [BASE+NARGS:RD*8-8]
  1914. | mov L:RB->top, RD
  1915. | xor RD, RD
  1916. | mov aword L:RB->cframe, RDa
  1917. | mov al, LUA_YIELD
  1918. | mov byte L:RB->status, al
  1919. | jmp ->vm_leave_unw
  1920. |
  1921. |//-- Math library -------------------------------------------------------
  1922. |
  1923. |.if not DUALNUM
  1924. |->fff_resi: // Dummy.
  1925. |.endif
  1926. |
  1927. |.if SSE
  1928. |->fff_resn:
  1929. | mov PC, [BASE-4]
  1930. | fstp qword [BASE-8]
  1931. | jmp ->fff_res1
  1932. |.endif
  1933. |
  1934. | .ffunc_1 math_abs
  1935. |.if DUALNUM
  1936. | cmp dword [BASE+4], LJ_TISNUM; jne >2
  1937. | mov RB, dword [BASE]
  1938. | cmp RB, 0; jns ->fff_resi
  1939. | neg RB; js >1
  1940. |->fff_resbit:
  1941. |->fff_resi:
  1942. | mov PC, [BASE-4]
  1943. | mov dword [BASE-4], LJ_TISNUM
  1944. | mov dword [BASE-8], RB
  1945. | jmp ->fff_res1
  1946. |1:
  1947. | mov PC, [BASE-4]
  1948. | mov dword [BASE-4], 0x41e00000 // 2^31.
  1949. | mov dword [BASE-8], 0
  1950. | jmp ->fff_res1
  1951. |2:
  1952. | ja ->fff_fallback
  1953. |.else
  1954. | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
  1955. |.endif
  1956. |
  1957. |.if SSE
  1958. | movsd xmm0, qword [BASE]
  1959. | sseconst_abs xmm1, RDa
  1960. | andps xmm0, xmm1
  1961. |->fff_resxmm0:
  1962. | mov PC, [BASE-4]
  1963. | movsd qword [BASE-8], xmm0
  1964. | // fallthrough
  1965. |.else
  1966. | fld qword [BASE]
  1967. | fabs
  1968. | // fallthrough
  1969. |->fff_resxmm0: // Dummy.
  1970. |->fff_resn:
  1971. | mov PC, [BASE-4]
  1972. | fstp qword [BASE-8]
  1973. |.endif
  1974. |
  1975. |->fff_res1:
  1976. | mov RD, 1+1
  1977. |->fff_res:
  1978. | mov MULTRES, RD
  1979. |->fff_res_:
  1980. | test PC, FRAME_TYPE
  1981. | jnz >7
  1982. |5:
  1983. | cmp PC_RB, RDL // More results expected?
  1984. | ja >6
  1985. | // Adjust BASE. KBASE is assumed to be set for the calling frame.
  1986. | movzx RA, PC_RA
  1987. | not RAa // Note: ~RA = -(RA+1)
  1988. | lea BASE, [BASE+RA*8] // base = base - (RA+1)*8
  1989. | ins_next
  1990. |
  1991. |6: // Fill up results with nil.
  1992. | mov dword [BASE+RD*8-12], LJ_TNIL
  1993. | add RD, 1
  1994. | jmp <5
  1995. |
  1996. |7: // Non-standard return case.
  1997. | mov RAa, -8 // Results start at BASE+RA = BASE-8.
  1998. | jmp ->vm_return
  1999. |
  2000. |.macro math_round, func
  2001. | .ffunc math_ .. func
  2002. |.if DUALNUM
  2003. | cmp dword [BASE+4], LJ_TISNUM; jne >1
  2004. | mov RB, dword [BASE]; jmp ->fff_resi
  2005. |1:
  2006. | ja ->fff_fallback
  2007. |.else
  2008. | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
  2009. |.endif
  2010. |.if SSE
  2011. | movsd xmm0, qword [BASE]
  2012. | call ->vm_ .. func
  2013. | .if DUALNUM
  2014. | cvtsd2si RB, xmm0
  2015. | cmp RB, 0x80000000
  2016. | jne ->fff_resi
  2017. | cvtsi2sd xmm1, RB
  2018. | ucomisd xmm0, xmm1
  2019. | jp ->fff_resxmm0
  2020. | je ->fff_resi
  2021. | .endif
  2022. | jmp ->fff_resxmm0
  2023. |.else
  2024. | fld qword [BASE]
  2025. | call ->vm_ .. func
  2026. | .if DUALNUM
  2027. | fist ARG1
  2028. | mov RB, ARG1
  2029. | cmp RB, 0x80000000; jne >2
  2030. | fdup
  2031. | fild ARG1
  2032. | fcomparepp
  2033. | jp ->fff_resn
  2034. | jne ->fff_resn
  2035. |2:
  2036. | fpop
  2037. | jmp ->fff_resi
  2038. | .else
  2039. | jmp ->fff_resn
  2040. | .endif
  2041. |.endif
  2042. |.endmacro
  2043. |
  2044. | math_round floor
  2045. | math_round ceil
  2046. |
  2047. |.if SSE
  2048. |.ffunc_nsse math_sqrt, sqrtsd; jmp ->fff_resxmm0
  2049. |.else
  2050. |.ffunc_n math_sqrt; fsqrt; jmp ->fff_resn
  2051. |.endif
  2052. |
  2053. |.ffunc math_log
  2054. | cmp NARGS:RD, 1+1; jne ->fff_fallback // Exactly one argument.
  2055. | cmp dword [BASE+4], LJ_TISNUM; jae ->fff_fallback
  2056. | fldln2; fld qword [BASE]; fyl2x; jmp ->fff_resn
  2057. |
  2058. |.ffunc_n math_log10, fldlg2; fyl2x; jmp ->fff_resn
  2059. |.ffunc_n math_exp; call ->vm_exp_x87; jmp ->fff_resn
  2060. |
  2061. |.ffunc_n math_sin; fsin; jmp ->fff_resn
  2062. |.ffunc_n math_cos; fcos; jmp ->fff_resn
  2063. |.ffunc_n math_tan; fptan; fpop; jmp ->fff_resn
  2064. |
  2065. |.ffunc_n math_asin
  2066. | fdup; fmul st0; fld1; fsubrp st1; fsqrt; fpatan
  2067. | jmp ->fff_resn
  2068. |.ffunc_n math_acos
  2069. | fdup; fmul st0; fld1; fsubrp st1; fsqrt; fxch; fpatan
  2070. | jmp ->fff_resn
  2071. |.ffunc_n math_atan; fld1; fpatan; jmp ->fff_resn
  2072. |
  2073. |.macro math_extern, func
  2074. |.if SSE
  2075. | .ffunc_nsse math_ .. func
  2076. | .if not X64
  2077. | movsd FPARG1, xmm0
  2078. | .endif
  2079. |.else
  2080. | .ffunc_n math_ .. func
  2081. | fstp FPARG1
  2082. |.endif
  2083. | mov RB, BASE
  2084. | call extern lj_vm_ .. func
  2085. | mov BASE, RB
  2086. | .if X64
  2087. | jmp ->fff_resxmm0
  2088. | .else
  2089. | jmp ->fff_resn
  2090. | .endif
  2091. |.endmacro
  2092. |
  2093. | math_extern sinh
  2094. | math_extern cosh
  2095. | math_extern tanh
  2096. |
  2097. |->ff_math_deg:
  2098. |.if SSE
  2099. |.ffunc_nsse math_rad
  2100. | mov CFUNC:RB, [BASE-8]
  2101. | mulsd xmm0, qword CFUNC:RB->upvalue[0]
  2102. | jmp ->fff_resxmm0
  2103. |.else
  2104. |.ffunc_n math_rad
  2105. | mov CFUNC:RB, [BASE-8]
  2106. | fmul qword CFUNC:RB->upvalue[0]
  2107. | jmp ->fff_resn
  2108. |.endif
  2109. |
  2110. |.ffunc_nn math_atan2; fpatan; jmp ->fff_resn
  2111. |.ffunc_nnr math_ldexp; fscale; fpop1; jmp ->fff_resn
  2112. |
  2113. |.ffunc_1 math_frexp
  2114. | mov RB, [BASE+4]
  2115. | cmp RB, LJ_TISNUM; jae ->fff_fallback
  2116. | mov PC, [BASE-4]
  2117. | mov RC, [BASE]
  2118. | mov [BASE-4], RB; mov [BASE-8], RC
  2119. | shl RB, 1; cmp RB, 0xffe00000; jae >3
  2120. | or RC, RB; jz >3
  2121. | mov RC, 1022
  2122. | cmp RB, 0x00200000; jb >4
  2123. |1:
  2124. | shr RB, 21; sub RB, RC // Extract and unbias exponent.
  2125. |.if SSE
  2126. | cvtsi2sd xmm0, RB
  2127. |.else
  2128. | mov TMP1, RB; fild TMP1
  2129. |.endif
  2130. | mov RB, [BASE-4]
  2131. | and RB, 0x800fffff // Mask off exponent.
  2132. | or RB, 0x3fe00000 // Put mantissa in range [0.5,1) or 0.
  2133. | mov [BASE-4], RB
  2134. |2:
  2135. |.if SSE
  2136. | movsd qword [BASE], xmm0
  2137. |.else
  2138. | fstp qword [BASE]
  2139. |.endif
  2140. | mov RD, 1+2
  2141. | jmp ->fff_res
  2142. |3: // Return +-0, +-Inf, NaN unmodified and an exponent of 0.
  2143. |.if SSE
  2144. | xorps xmm0, xmm0; jmp <2
  2145. |.else
  2146. | fldz; jmp <2
  2147. |.endif
  2148. |4: // Handle denormals by multiplying with 2^54 and adjusting the bias.
  2149. |.if SSE
  2150. | movsd xmm0, qword [BASE]
  2151. | sseconst_hi xmm1, RBa, 43500000 // 2^54.
  2152. | mulsd xmm0, xmm1
  2153. | movsd qword [BASE-8], xmm0
  2154. |.else
  2155. | fld qword [BASE]
  2156. | mov TMP1, 0x5a800000; fmul TMP1 // x = x*2^54
  2157. | fstp qword [BASE-8]
  2158. |.endif
  2159. | mov RB, [BASE-4]; mov RC, 1076; shl RB, 1; jmp <1
  2160. |
  2161. |.if SSE
  2162. |.ffunc_nsse math_modf
  2163. |.else
  2164. |.ffunc_n math_modf
  2165. |.endif
  2166. | mov RB, [BASE+4]
  2167. | mov PC, [BASE-4]
  2168. | shl RB, 1; cmp RB, 0xffe00000; je >4 // +-Inf?
  2169. |.if SSE
  2170. | movaps xmm4, xmm0
  2171. | call ->vm_trunc
  2172. | subsd xmm4, xmm0
  2173. |1:
  2174. | movsd qword [BASE-8], xmm0
  2175. | movsd qword [BASE], xmm4
  2176. |.else
  2177. | fdup
  2178. | call ->vm_trunc
  2179. | fsub st1, st0
  2180. |1:
  2181. | fstp qword [BASE-8]
  2182. | fstp qword [BASE]
  2183. |.endif
  2184. | mov RC, [BASE-4]; mov RB, [BASE+4]
  2185. | xor RC, RB; js >3 // Need to adjust sign?
  2186. |2:
  2187. | mov RD, 1+2
  2188. | jmp ->fff_res
  2189. |3:
  2190. | xor RB, 0x80000000; mov [BASE+4], RB // Flip sign of fraction.
  2191. | jmp <2
  2192. |4:
  2193. |.if SSE
  2194. | xorps xmm4, xmm4; jmp <1 // Return +-Inf and +-0.
  2195. |.else
  2196. | fldz; fxch; jmp <1 // Return +-Inf and +-0.
  2197. |.endif
  2198. |
  2199. |.ffunc_nnr math_fmod
  2200. |1: ; fprem; fnstsw ax; sahf; jp <1
  2201. | fpop1
  2202. | jmp ->fff_resn
  2203. |
  2204. |.if SSE
  2205. |.ffunc_nnsse math_pow; call ->vm_pow; jmp ->fff_resxmm0
  2206. |.else
  2207. |.ffunc_nn math_pow; call ->vm_pow; jmp ->fff_resn
  2208. |.endif
  2209. |
  2210. |.macro math_minmax, name, cmovop, fcmovop, sseop
  2211. | .ffunc name
  2212. | mov RA, 2
  2213. | cmp dword [BASE+4], LJ_TISNUM
  2214. |.if DUALNUM
  2215. | jne >4
  2216. | mov RB, dword [BASE]
  2217. |1: // Handle integers.
  2218. | cmp RA, RD; jae ->fff_resi
  2219. | cmp dword [BASE+RA*8-4], LJ_TISNUM; jne >3
  2220. | cmp RB, dword [BASE+RA*8-8]
  2221. | cmovop RB, dword [BASE+RA*8-8]
  2222. | add RA, 1
  2223. | jmp <1
  2224. |3:
  2225. | ja ->fff_fallback
  2226. | // Convert intermediate result to number and continue below.
  2227. |.if SSE
  2228. | cvtsi2sd xmm0, RB
  2229. |.else
  2230. | mov TMP1, RB
  2231. | fild TMP1
  2232. |.endif
  2233. | jmp >6
  2234. |4:
  2235. | ja ->fff_fallback
  2236. |.else
  2237. | jae ->fff_fallback
  2238. |.endif
  2239. |
  2240. |.if SSE
  2241. | movsd xmm0, qword [BASE]
  2242. |5: // Handle numbers or integers.
  2243. | cmp RA, RD; jae ->fff_resxmm0
  2244. | cmp dword [BASE+RA*8-4], LJ_TISNUM
  2245. |.if DUALNUM
  2246. | jb >6
  2247. | ja ->fff_fallback
  2248. | cvtsi2sd xmm1, dword [BASE+RA*8-8]
  2249. | jmp >7
  2250. |.else
  2251. | jae ->fff_fallback
  2252. |.endif
  2253. |6:
  2254. | movsd xmm1, qword [BASE+RA*8-8]
  2255. |7:
  2256. | sseop xmm0, xmm1
  2257. | add RA, 1
  2258. | jmp <5
  2259. |.else
  2260. | fld qword [BASE]
  2261. |5: // Handle numbers or integers.
  2262. | cmp RA, RD; jae ->fff_resn
  2263. | cmp dword [BASE+RA*8-4], LJ_TISNUM
  2264. |.if DUALNUM
  2265. | jb >6
  2266. | ja >9
  2267. | fild dword [BASE+RA*8-8]
  2268. | jmp >7
  2269. |.else
  2270. | jae >9
  2271. |.endif
  2272. |6:
  2273. | fld qword [BASE+RA*8-8]
  2274. |7:
  2275. | fucomi st1; fcmovop st1; fpop1
  2276. | add RA, 1
  2277. | jmp <5
  2278. |.endif
  2279. |.endmacro
  2280. |
  2281. | math_minmax math_min, cmovg, fcmovnbe, minsd
  2282. | math_minmax math_max, cmovl, fcmovbe, maxsd
  2283. |.if not SSE
  2284. |9:
  2285. | fpop; jmp ->fff_fallback
  2286. |.endif
  2287. |
  2288. |//-- String library -----------------------------------------------------
  2289. |
  2290. |.ffunc_1 string_len
  2291. | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
  2292. | mov STR:RB, [BASE]
  2293. |.if DUALNUM
  2294. | mov RB, dword STR:RB->len; jmp ->fff_resi
  2295. |.elif SSE
  2296. | cvtsi2sd xmm0, dword STR:RB->len; jmp ->fff_resxmm0
  2297. |.else
  2298. | fild dword STR:RB->len; jmp ->fff_resn
  2299. |.endif
  2300. |
  2301. |.ffunc string_byte // Only handle the 1-arg case here.
  2302. | cmp NARGS:RD, 1+1; jne ->fff_fallback
  2303. | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
  2304. | mov STR:RB, [BASE]
  2305. | mov PC, [BASE-4]
  2306. | cmp dword STR:RB->len, 1
  2307. | jb ->fff_res0 // Return no results for empty string.
  2308. | movzx RB, byte STR:RB[1]
  2309. |.if DUALNUM
  2310. | jmp ->fff_resi
  2311. |.elif SSE
  2312. | cvtsi2sd xmm0, RB; jmp ->fff_resxmm0
  2313. |.else
  2314. | mov TMP1, RB; fild TMP1; jmp ->fff_resn
  2315. |.endif
  2316. |
  2317. |.ffunc string_char // Only handle the 1-arg case here.
  2318. | ffgccheck
  2319. | cmp NARGS:RD, 1+1; jne ->fff_fallback // *Exactly* 1 arg.
  2320. | cmp dword [BASE+4], LJ_TISNUM
  2321. |.if DUALNUM
  2322. | jne ->fff_fallback
  2323. | mov RB, dword [BASE]
  2324. | cmp RB, 255; ja ->fff_fallback
  2325. | mov TMP2, RB
  2326. |.elif SSE
  2327. | jae ->fff_fallback
  2328. | cvttsd2si RB, qword [BASE]
  2329. | cmp RB, 255; ja ->fff_fallback
  2330. | mov TMP2, RB
  2331. |.else
  2332. | jae ->fff_fallback
  2333. | fld qword [BASE]
  2334. | fistp TMP2
  2335. | cmp TMP2, 255; ja ->fff_fallback
  2336. |.endif
  2337. |.if X64
  2338. | mov TMP3, 1
  2339. |.else
  2340. | mov ARG3, 1
  2341. |.endif
  2342. | lea RDa, TMP2 // Points to stack. Little-endian.
  2343. |->fff_newstr:
  2344. | mov L:RB, SAVE_L
  2345. | mov L:RB->base, BASE
  2346. |.if X64
  2347. | mov CARG3d, TMP3 // Zero-extended to size_t.
  2348. | mov CARG2, RDa // May be 64 bit ptr to stack.
  2349. | mov CARG1d, L:RB
  2350. |.else
  2351. | mov ARG2, RD
  2352. | mov ARG1, L:RB
  2353. |.endif
  2354. | mov SAVE_PC, PC
  2355. | call extern lj_str_new // (lua_State *L, char *str, size_t l)
  2356. | // GCstr * returned in eax (RD).
  2357. | mov BASE, L:RB->base
  2358. | mov PC, [BASE-4]
  2359. | mov dword [BASE-4], LJ_TSTR
  2360. | mov [BASE-8], STR:RD
  2361. | jmp ->fff_res1
  2362. |
  2363. |.ffunc string_sub
  2364. | ffgccheck
  2365. | mov TMP2, -1
  2366. | cmp NARGS:RD, 1+2; jb ->fff_fallback
  2367. | jna >1
  2368. | cmp dword [BASE+20], LJ_TISNUM
  2369. |.if DUALNUM
  2370. | jne ->fff_fallback
  2371. | mov RB, dword [BASE+16]
  2372. | mov TMP2, RB
  2373. |.elif SSE
  2374. | jae ->fff_fallback
  2375. | cvttsd2si RB, qword [BASE+16]
  2376. | mov TMP2, RB
  2377. |.else
  2378. | jae ->fff_fallback
  2379. | fld qword [BASE+16]
  2380. | fistp TMP2
  2381. |.endif
  2382. |1:
  2383. | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
  2384. | cmp dword [BASE+12], LJ_TISNUM
  2385. |.if DUALNUM
  2386. | jne ->fff_fallback
  2387. |.else
  2388. | jae ->fff_fallback
  2389. |.endif
  2390. | mov STR:RB, [BASE]
  2391. | mov TMP3, STR:RB
  2392. | mov RB, STR:RB->len
  2393. |.if DUALNUM
  2394. | mov RA, dword [BASE+8]
  2395. |.elif SSE
  2396. | cvttsd2si RA, qword [BASE+8]
  2397. |.else
  2398. | fld qword [BASE+8]
  2399. | fistp ARG3
  2400. | mov RA, ARG3
  2401. |.endif
  2402. | mov RC, TMP2
  2403. | cmp RB, RC // len < end? (unsigned compare)
  2404. | jb >5
  2405. |2:
  2406. | test RA, RA // start <= 0?
  2407. | jle >7
  2408. |3:
  2409. | mov STR:RB, TMP3
  2410. | sub RC, RA // start > end?
  2411. | jl ->fff_emptystr
  2412. | lea RB, [STR:RB+RA+#STR-1]
  2413. | add RC, 1
  2414. |4:
  2415. |.if X64
  2416. | mov TMP3, RC
  2417. |.else
  2418. | mov ARG3, RC
  2419. |.endif
  2420. | mov RD, RB
  2421. | jmp ->fff_newstr
  2422. |
  2423. |5: // Negative end or overflow.
  2424. | jl >6
  2425. | lea RC, [RC+RB+1] // end = end+(len+1)
  2426. | jmp <2
  2427. |6: // Overflow.
  2428. | mov RC, RB // end = len
  2429. | jmp <2
  2430. |
  2431. |7: // Negative start or underflow.
  2432. | je >8
  2433. | add RA, RB // start = start+(len+1)
  2434. | add RA, 1
  2435. | jg <3 // start > 0?
  2436. |8: // Underflow.
  2437. | mov RA, 1 // start = 1
  2438. | jmp <3
  2439. |
  2440. |->fff_emptystr: // Range underflow.
  2441. | xor RC, RC // Zero length. Any ptr in RB is ok.
  2442. | jmp <4
  2443. |
  2444. |.ffunc string_rep // Only handle the 1-char case inline.
  2445. | ffgccheck
  2446. | cmp NARGS:RD, 2+1; jne ->fff_fallback // Exactly 2 arguments.
  2447. | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
  2448. | cmp dword [BASE+12], LJ_TISNUM
  2449. | mov STR:RB, [BASE]
  2450. |.if DUALNUM
  2451. | jne ->fff_fallback
  2452. | mov RC, dword [BASE+8]
  2453. |.elif SSE
  2454. | jae ->fff_fallback
  2455. | cvttsd2si RC, qword [BASE+8]
  2456. |.else
  2457. | jae ->fff_fallback
  2458. | fld qword [BASE+8]
  2459. | fistp TMP2
  2460. | mov RC, TMP2
  2461. |.endif
  2462. | test RC, RC
  2463. | jle ->fff_emptystr // Count <= 0? (or non-int)
  2464. | cmp dword STR:RB->len, 1
  2465. | jb ->fff_emptystr // Zero length string?
  2466. | jne ->fff_fallback_2 // Fallback for > 1-char strings.
  2467. | cmp [DISPATCH+DISPATCH_GL(tmpbuf.sz)], RC; jb ->fff_fallback_2
  2468. | movzx RA, byte STR:RB[1]
  2469. | mov RB, [DISPATCH+DISPATCH_GL(tmpbuf.buf)]
  2470. |.if X64
  2471. | mov TMP3, RC
  2472. |.else
  2473. | mov ARG3, RC
  2474. |.endif
  2475. |1: // Fill buffer with char. Yes, this is suboptimal code (do you care?).
  2476. | mov [RB], RAL
  2477. | add RB, 1
  2478. | sub RC, 1
  2479. | jnz <1
  2480. | mov RD, [DISPATCH+DISPATCH_GL(tmpbuf.buf)]
  2481. | jmp ->fff_newstr
  2482. |
  2483. |.ffunc_1 string_reverse
  2484. | ffgccheck
  2485. | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
  2486. | mov STR:RB, [BASE]
  2487. | mov RC, STR:RB->len
  2488. | test RC, RC
  2489. | jz ->fff_emptystr // Zero length string?
  2490. | cmp [DISPATCH+DISPATCH_GL(tmpbuf.sz)], RC; jb ->fff_fallback_1
  2491. | add RB, #STR
  2492. | mov TMP2, PC // Need another temp register.
  2493. |.if X64
  2494. | mov TMP3, RC
  2495. |.else
  2496. | mov ARG3, RC
  2497. |.endif
  2498. | mov PC, [DISPATCH+DISPATCH_GL(tmpbuf.buf)]
  2499. |1:
  2500. | movzx RA, byte [RB]
  2501. | add RB, 1
  2502. | sub RC, 1
  2503. | mov [PC+RC], RAL
  2504. | jnz <1
  2505. | mov RD, PC
  2506. | mov PC, TMP2
  2507. | jmp ->fff_newstr
  2508. |
  2509. |.macro ffstring_case, name, lo, hi
  2510. | .ffunc_1 name
  2511. | ffgccheck
  2512. | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback
  2513. | mov STR:RB, [BASE]
  2514. | mov RC, STR:RB->len
  2515. | cmp [DISPATCH+DISPATCH_GL(tmpbuf.sz)], RC; jb ->fff_fallback_1
  2516. | add RB, #STR
  2517. | mov TMP2, PC // Need another temp register.
  2518. |.if X64
  2519. | mov TMP3, RC
  2520. |.else
  2521. | mov ARG3, RC
  2522. |.endif
  2523. | mov PC, [DISPATCH+DISPATCH_GL(tmpbuf.buf)]
  2524. | jmp >3
  2525. |1: // ASCII case conversion. Yes, this is suboptimal code (do you care?).
  2526. | movzx RA, byte [RB+RC]
  2527. | cmp RA, lo
  2528. | jb >2
  2529. | cmp RA, hi
  2530. | ja >2
  2531. | xor RA, 0x20
  2532. |2:
  2533. | mov [PC+RC], RAL
  2534. |3:
  2535. | sub RC, 1
  2536. | jns <1
  2537. | mov RD, PC
  2538. | mov PC, TMP2
  2539. | jmp ->fff_newstr
  2540. |.endmacro
  2541. |
  2542. |ffstring_case string_lower, 0x41, 0x5a
  2543. |ffstring_case string_upper, 0x61, 0x7a
  2544. |
  2545. |//-- Table library ------------------------------------------------------
  2546. |
  2547. |.ffunc_1 table_getn
  2548. | cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
  2549. | mov RB, BASE // Save BASE.
  2550. | mov TAB:FCARG1, [BASE]
  2551. | call extern lj_tab_len@4 // LJ_FASTCALL (GCtab *t)
  2552. | // Length of table returned in eax (RD).
  2553. | mov BASE, RB // Restore BASE.
  2554. |.if DUALNUM
  2555. | mov RB, RD; jmp ->fff_resi
  2556. |.elif SSE
  2557. | cvtsi2sd xmm0, RD; jmp ->fff_resxmm0
  2558. |.else
  2559. | mov ARG1, RD; fild ARG1; jmp ->fff_resn
  2560. |.endif
  2561. |
  2562. |//-- Bit library --------------------------------------------------------
  2563. |
  2564. |.define TOBIT_BIAS, 0x59c00000 // 2^52 + 2^51 (float, not double!).
  2565. |
  2566. |.macro .ffunc_bit, name, kind
  2567. | .ffunc_1 name
  2568. |.if kind == 2
  2569. |.if SSE
  2570. | sseconst_tobit xmm1, RBa
  2571. |.else
  2572. | mov TMP1, TOBIT_BIAS
  2573. |.endif
  2574. |.endif
  2575. | cmp dword [BASE+4], LJ_TISNUM
  2576. |.if DUALNUM
  2577. | jne >1
  2578. | mov RB, dword [BASE]
  2579. |.if kind > 0
  2580. | jmp >2
  2581. |.else
  2582. | jmp ->fff_resbit
  2583. |.endif
  2584. |1:
  2585. | ja ->fff_fallback
  2586. |.else
  2587. | jae ->fff_fallback
  2588. |.endif
  2589. |.if SSE
  2590. | movsd xmm0, qword [BASE]
  2591. |.if kind < 2
  2592. | sseconst_tobit xmm1, RBa
  2593. |.endif
  2594. | addsd xmm0, xmm1
  2595. | movd RB, xmm0
  2596. |.else
  2597. | fld qword [BASE]
  2598. |.if kind < 2
  2599. | mov TMP1, TOBIT_BIAS
  2600. |.endif
  2601. | fadd TMP1
  2602. | fstp FPARG1
  2603. |.if kind > 0
  2604. | mov RB, ARG1
  2605. |.endif
  2606. |.endif
  2607. |2:
  2608. |.endmacro
  2609. |
  2610. |.ffunc_bit bit_tobit, 0
  2611. |.if DUALNUM or SSE
  2612. |.if not SSE
  2613. | mov RB, ARG1
  2614. |.endif
  2615. | jmp ->fff_resbit
  2616. |.else
  2617. | fild ARG1
  2618. | jmp ->fff_resn
  2619. |.endif
  2620. |
  2621. |.macro .ffunc_bit_op, name, ins
  2622. | .ffunc_bit name, 2
  2623. | mov TMP2, NARGS:RD // Save for fallback.
  2624. | lea RD, [BASE+NARGS:RD*8-16]
  2625. |1:
  2626. | cmp RD, BASE
  2627. | jbe ->fff_resbit
  2628. | cmp dword [RD+4], LJ_TISNUM
  2629. |.if DUALNUM
  2630. | jne >2
  2631. | ins RB, dword [RD]
  2632. | sub RD, 8
  2633. | jmp <1
  2634. |2:
  2635. | ja ->fff_fallback_bit_op
  2636. |.else
  2637. | jae ->fff_fallback_bit_op
  2638. |.endif
  2639. |.if SSE
  2640. | movsd xmm0, qword [RD]
  2641. | addsd xmm0, xmm1
  2642. | movd RA, xmm0
  2643. | ins RB, RA
  2644. |.else
  2645. | fld qword [RD]
  2646. | fadd TMP1
  2647. | fstp FPARG1
  2648. | ins RB, ARG1
  2649. |.endif
  2650. | sub RD, 8
  2651. | jmp <1
  2652. |.endmacro
  2653. |
  2654. |.ffunc_bit_op bit_band, and
  2655. |.ffunc_bit_op bit_bor, or
  2656. |.ffunc_bit_op bit_bxor, xor
  2657. |
  2658. |.ffunc_bit bit_bswap, 1
  2659. | bswap RB
  2660. | jmp ->fff_resbit
  2661. |
  2662. |.ffunc_bit bit_bnot, 1
  2663. | not RB
  2664. |.if DUALNUM
  2665. | jmp ->fff_resbit
  2666. |.elif SSE
  2667. |->fff_resbit:
  2668. | cvtsi2sd xmm0, RB
  2669. | jmp ->fff_resxmm0
  2670. |.else
  2671. |->fff_resbit:
  2672. | mov ARG1, RB
  2673. | fild ARG1
  2674. | jmp ->fff_resn
  2675. |.endif
  2676. |
  2677. |->fff_fallback_bit_op:
  2678. | mov NARGS:RD, TMP2 // Restore for fallback
  2679. | jmp ->fff_fallback
  2680. |
  2681. |.macro .ffunc_bit_sh, name, ins
  2682. |.if DUALNUM
  2683. | .ffunc_bit name, 1
  2684. | // Note: no inline conversion from number for 2nd argument!
  2685. | cmp dword [BASE+12], LJ_TISNUM; jne ->fff_fallback
  2686. | mov RA, dword [BASE+8]
  2687. |.elif SSE
  2688. | .ffunc_nnsse name
  2689. | sseconst_tobit xmm2, RBa
  2690. | addsd xmm0, xmm2
  2691. | addsd xmm1, xmm2
  2692. | movd RB, xmm0
  2693. | movd RA, xmm1
  2694. |.else
  2695. | .ffunc_nn name
  2696. | mov TMP1, TOBIT_BIAS
  2697. | fadd TMP1
  2698. | fstp FPARG3
  2699. | fadd TMP1
  2700. | fstp FPARG1
  2701. | mov RA, ARG3
  2702. | mov RB, ARG1
  2703. |.endif
  2704. | ins RB, cl // Assumes RA is ecx.
  2705. | jmp ->fff_resbit
  2706. |.endmacro
  2707. |
  2708. |.ffunc_bit_sh bit_lshift, shl
  2709. |.ffunc_bit_sh bit_rshift, shr
  2710. |.ffunc_bit_sh bit_arshift, sar
  2711. |.ffunc_bit_sh bit_rol, rol
  2712. |.ffunc_bit_sh bit_ror, ror
  2713. |
  2714. |//-----------------------------------------------------------------------
  2715. |
  2716. |->fff_fallback_2:
  2717. | mov NARGS:RD, 1+2 // Other args are ignored, anyway.
  2718. | jmp ->fff_fallback
  2719. |->fff_fallback_1:
  2720. | mov NARGS:RD, 1+1 // Other args are ignored, anyway.
  2721. |->fff_fallback: // Call fast function fallback handler.
  2722. | // BASE = new base, RD = nargs+1
  2723. | mov L:RB, SAVE_L
  2724. | mov PC, [BASE-4] // Fallback may overwrite PC.
  2725. | mov SAVE_PC, PC // Redundant (but a defined value).
  2726. | mov L:RB->base, BASE
  2727. | lea RD, [BASE+NARGS:RD*8-8]
  2728. | lea RA, [RD+8*LUA_MINSTACK] // Ensure enough space for handler.
  2729. | mov L:RB->top, RD
  2730. | mov CFUNC:RD, [BASE-8]
  2731. | cmp RA, L:RB->maxstack
  2732. | ja >5 // Need to grow stack.
  2733. |.if X64
  2734. | mov CARG1d, L:RB
  2735. |.else
  2736. | mov ARG1, L:RB
  2737. |.endif
  2738. | call aword CFUNC:RD->f // (lua_State *L)
  2739. | mov BASE, L:RB->base
  2740. | // Either throws an error, or recovers and returns -1, 0 or nresults+1.
  2741. | test RD, RD; jg ->fff_res // Returned nresults+1?
  2742. |1:
  2743. | mov RA, L:RB->top
  2744. | sub RA, BASE
  2745. | shr RA, 3
  2746. | test RD, RD
  2747. | lea NARGS:RD, [RA+1]
  2748. | mov LFUNC:RB, [BASE-8]
  2749. | jne ->vm_call_tail // Returned -1?
  2750. | ins_callt // Returned 0: retry fast path.
  2751. |
  2752. |// Reconstruct previous base for vmeta_call during tailcall.
  2753. |->vm_call_tail:
  2754. | mov RA, BASE
  2755. | test PC, FRAME_TYPE
  2756. | jnz >3
  2757. | movzx RB, PC_RA
  2758. | not RBa // Note: ~RB = -(RB+1)
  2759. | lea BASE, [BASE+RB*8] // base = base - (RB+1)*8
  2760. | jmp ->vm_call_dispatch // Resolve again for tailcall.
  2761. |3:
  2762. | mov RB, PC
  2763. | and RB, -8
  2764. | sub BASE, RB
  2765. | jmp ->vm_call_dispatch // Resolve again for tailcall.
  2766. |
  2767. |5: // Grow stack for fallback handler.
  2768. | mov FCARG2, LUA_MINSTACK
  2769. | mov FCARG1, L:RB
  2770. | call extern lj_state_growstack@8 // (lua_State *L, int n)
  2771. | mov BASE, L:RB->base
  2772. | xor RD, RD // Simulate a return 0.
  2773. | jmp <1 // Dumb retry (goes through ff first).
  2774. |
  2775. |->fff_gcstep: // Call GC step function.
  2776. | // BASE = new base, RD = nargs+1
  2777. | pop RBa // Must keep stack at same level.
  2778. | mov TMPa, RBa // Save return address
  2779. | mov L:RB, SAVE_L
  2780. | mov SAVE_PC, PC // Redundant (but a defined value).
  2781. | mov L:RB->base, BASE
  2782. | lea RD, [BASE+NARGS:RD*8-8]
  2783. | mov FCARG1, L:RB
  2784. | mov L:RB->top, RD
  2785. | call extern lj_gc_step@4 // (lua_State *L)
  2786. | mov BASE, L:RB->base
  2787. | mov RD, L:RB->top
  2788. | sub RD, BASE
  2789. | shr RD, 3
  2790. | add NARGS:RD, 1
  2791. | mov RBa, TMPa
  2792. | push RBa // Restore return address.
  2793. | ret
  2794. |
  2795. |//-----------------------------------------------------------------------
  2796. |//-- Special dispatch targets -------------------------------------------
  2797. |//-----------------------------------------------------------------------
  2798. |
  2799. |->vm_record: // Dispatch target for recording phase.
  2800. |.if JIT
  2801. | movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)]
  2802. | test RDL, HOOK_VMEVENT // No recording while in vmevent.
  2803. | jnz >5
  2804. | // Decrement the hookcount for consistency, but always do the call.
  2805. | test RDL, HOOK_ACTIVE
  2806. | jnz >1
  2807. | test RDL, LUA_MASKLINE|LUA_MASKCOUNT
  2808. | jz >1
  2809. | dec dword [DISPATCH+DISPATCH_GL(hookcount)]
  2810. | jmp >1
  2811. |.endif
  2812. |
  2813. |->vm_rethook: // Dispatch target for return hooks.
  2814. | movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)]
  2815. | test RDL, HOOK_ACTIVE // Hook already active?
  2816. | jnz >5
  2817. | jmp >1
  2818. |
  2819. |->vm_inshook: // Dispatch target for instr/line hooks.
  2820. | movzx RD, byte [DISPATCH+DISPATCH_GL(hookmask)]
  2821. | test RDL, HOOK_ACTIVE // Hook already active?
  2822. | jnz >5
  2823. |
  2824. | test RDL, LUA_MASKLINE|LUA_MASKCOUNT
  2825. | jz >5
  2826. | dec dword [DISPATCH+DISPATCH_GL(hookcount)]
  2827. | jz >1
  2828. | test RDL, LUA_MASKLINE
  2829. | jz >5
  2830. |1:
  2831. | mov L:RB, SAVE_L
  2832. | mov L:RB->base, BASE
  2833. | mov FCARG2, PC // Caveat: FCARG2 == BASE
  2834. | mov FCARG1, L:RB
  2835. | // SAVE_PC must hold the _previous_ PC. The callee updates it with PC.
  2836. | call extern lj_dispatch_ins@8 // (lua_State *L, BCIns *pc)
  2837. |3:
  2838. | mov BASE, L:RB->base
  2839. |4:
  2840. | movzx RA, PC_RA
  2841. |5:
  2842. | movzx OP, PC_OP
  2843. | movzx RD, PC_RD
  2844. |.if X64
  2845. | jmp aword [DISPATCH+OP*8+GG_DISP2STATIC] // Re-dispatch to static ins.
  2846. |.else
  2847. | jmp aword [DISPATCH+OP*4+GG_DISP2STATIC] // Re-dispatch to static ins.
  2848. |.endif
  2849. |
  2850. |->cont_hook: // Continue from hook yield.
  2851. | add PC, 4
  2852. | mov RA, [RB-24]
  2853. | mov MULTRES, RA // Restore MULTRES for *M ins.
  2854. | jmp <4
  2855. |
  2856. |->vm_hotloop: // Hot loop counter underflow.
  2857. |.if JIT
  2858. | mov LFUNC:RB, [BASE-8] // Same as curr_topL(L).
  2859. | mov RB, LFUNC:RB->pc
  2860. | movzx RD, byte [RB+PC2PROTO(framesize)]
  2861. | lea RD, [BASE+RD*8]
  2862. | mov L:RB, SAVE_L
  2863. | mov L:RB->base, BASE
  2864. | mov L:RB->top, RD
  2865. | mov FCARG2, PC
  2866. | lea FCARG1, [DISPATCH+GG_DISP2J]
  2867. | mov aword [DISPATCH+DISPATCH_J(L)], L:RBa
  2868. | mov SAVE_PC, PC
  2869. | call extern lj_trace_hot@8 // (jit_State *J, const BCIns *pc)
  2870. | jmp <3
  2871. |.endif
  2872. |
  2873. |->vm_callhook: // Dispatch target for call hooks.
  2874. | mov SAVE_PC, PC
  2875. |.if JIT
  2876. | jmp >1
  2877. |.endif
  2878. |
  2879. |->vm_hotcall: // Hot call counter underflow.
  2880. |.if JIT
  2881. | mov SAVE_PC, PC
  2882. | or PC, 1 // Marker for hot call.
  2883. |1:
  2884. |.endif
  2885. | lea RD, [BASE+NARGS:RD*8-8]
  2886. | mov L:RB, SAVE_L
  2887. | mov L:RB->base, BASE
  2888. | mov L:RB->top, RD
  2889. | mov FCARG2, PC
  2890. | mov FCARG1, L:RB
  2891. | call extern lj_dispatch_call@8 // (lua_State *L, const BCIns *pc)
  2892. | // ASMFunction returned in eax/rax (RDa).
  2893. | mov SAVE_PC, 0 // Invalidate for subsequent line hook.
  2894. |.if JIT
  2895. | and PC, -2
  2896. |.endif
  2897. | mov BASE, L:RB->base
  2898. | mov RAa, RDa
  2899. | mov RD, L:RB->top
  2900. | sub RD, BASE
  2901. | mov RBa, RAa
  2902. | movzx RA, PC_RA
  2903. | shr RD, 3
  2904. | add NARGS:RD, 1
  2905. | jmp RBa
  2906. |
  2907. |//-----------------------------------------------------------------------
  2908. |//-- Trace exit handler -------------------------------------------------
  2909. |//-----------------------------------------------------------------------
  2910. |
  2911. |// Called from an exit stub with the exit number on the stack.
  2912. |// The 16 bit exit number is stored with two (sign-extended) push imm8.
  2913. |->vm_exit_handler:
  2914. |.if JIT
  2915. |.if X64
  2916. | push r13; push r12
  2917. | push r11; push r10; push r9; push r8
  2918. | push rdi; push rsi; push rbp; lea rbp, [rsp+88]; push rbp
  2919. | push rbx; push rdx; push rcx; push rax
  2920. | movzx RC, byte [rbp-8] // Reconstruct exit number.
  2921. | mov RCH, byte [rbp-16]
  2922. | mov [rbp-8], r15; mov [rbp-16], r14
  2923. |.else
  2924. | push ebp; lea ebp, [esp+12]; push ebp
  2925. | push ebx; push edx; push ecx; push eax
  2926. | movzx RC, byte [ebp-4] // Reconstruct exit number.
  2927. | mov RCH, byte [ebp-8]
  2928. | mov [ebp-4], edi; mov [ebp-8], esi
  2929. |.endif
  2930. | // Caveat: DISPATCH is ebx.
  2931. | mov DISPATCH, [ebp]
  2932. | mov RA, [DISPATCH+DISPATCH_GL(vmstate)] // Get trace number.
  2933. | set_vmstate EXIT
  2934. | mov [DISPATCH+DISPATCH_J(exitno)], RC
  2935. | mov [DISPATCH+DISPATCH_J(parent)], RA
  2936. |.if X64
  2937. |.if X64WIN
  2938. | sub rsp, 16*8+4*8 // Room for SSE regs + save area.
  2939. |.else
  2940. | sub rsp, 16*8 // Room for SSE regs.
  2941. |.endif
  2942. | add rbp, -128
  2943. | movsd qword [rbp-8], xmm15; movsd qword [rbp-16], xmm14
  2944. | movsd qword [rbp-24], xmm13; movsd qword [rbp-32], xmm12
  2945. | movsd qword [rbp-40], xmm11; movsd qword [rbp-48], xmm10
  2946. | movsd qword [rbp-56], xmm9; movsd qword [rbp-64], xmm8
  2947. | movsd qword [rbp-72], xmm7; movsd qword [rbp-80], xmm6
  2948. | movsd qword [rbp-88], xmm5; movsd qword [rbp-96], xmm4
  2949. | movsd qword [rbp-104], xmm3; movsd qword [rbp-112], xmm2
  2950. | movsd qword [rbp-120], xmm1; movsd qword [rbp-128], xmm0
  2951. |.else
  2952. | sub esp, 8*8+16 // Room for SSE regs + args.
  2953. | movsd qword [ebp-40], xmm7; movsd qword [ebp-48], xmm6
  2954. | movsd qword [ebp-56], xmm5; movsd qword [ebp-64], xmm4
  2955. | movsd qword [ebp-72], xmm3; movsd qword [ebp-80], xmm2
  2956. | movsd qword [ebp-88], xmm1; movsd qword [ebp-96], xmm0
  2957. |.endif
  2958. | // Caveat: RB is ebp.
  2959. | mov L:RB, [DISPATCH+DISPATCH_GL(jit_L)]
  2960. | mov BASE, [DISPATCH+DISPATCH_GL(jit_base)]
  2961. | mov aword [DISPATCH+DISPATCH_J(L)], L:RBa
  2962. | mov dword [DISPATCH+DISPATCH_GL(jit_L)], 0
  2963. | mov L:RB->base, BASE
  2964. |.if X64WIN
  2965. | lea CARG2, [rsp+4*8]
  2966. |.elif X64
  2967. | mov CARG2, rsp
  2968. |.else
  2969. | lea FCARG2, [esp+16]
  2970. |.endif
  2971. | lea FCARG1, [DISPATCH+GG_DISP2J]
  2972. | call extern lj_trace_exit@8 // (jit_State *J, ExitState *ex)
  2973. | // MULTRES or negated error code returned in eax (RD).
  2974. | mov RAa, L:RB->cframe
  2975. | and RAa, CFRAME_RAWMASK
  2976. |.if X64WIN
  2977. | // Reposition stack later.
  2978. |.elif X64
  2979. | mov rsp, RAa // Reposition stack to C frame.
  2980. |.else
  2981. | mov esp, RAa // Reposition stack to C frame.
  2982. |.endif
  2983. | mov [RAa+CFRAME_OFS_L], L:RB // Set SAVE_L (on-trace resume/yield).
  2984. | mov BASE, L:RB->base
  2985. | mov PC, [RAa+CFRAME_OFS_PC] // Get SAVE_PC.
  2986. |.if X64
  2987. | jmp >1
  2988. |.endif
  2989. |.endif
  2990. |->vm_exit_interp:
  2991. | // RD = MULTRES or negated error code, BASE, PC and DISPATCH set.
  2992. |.if JIT
  2993. |.if X64
  2994. | // Restore additional callee-save registers only used in compiled code.
  2995. |.if X64WIN
  2996. | lea RAa, [rsp+9*16+4*8]
  2997. |1:
  2998. | movdqa xmm15, [RAa-9*16]
  2999. | movdqa xmm14, [RAa-8*16]
  3000. | movdqa xmm13, [RAa-7*16]
  3001. | movdqa xmm12, [RAa-6*16]
  3002. | movdqa xmm11, [RAa-5*16]
  3003. | movdqa xmm10, [RAa-4*16]
  3004. | movdqa xmm9, [RAa-3*16]
  3005. | movdqa xmm8, [RAa-2*16]
  3006. | movdqa xmm7, [RAa-1*16]
  3007. | mov rsp, RAa // Reposition stack to C frame.
  3008. | movdqa xmm6, [RAa]
  3009. | mov r15, CSAVE_3
  3010. | mov r14, CSAVE_4
  3011. |.else
  3012. | add rsp, 16 // Reposition stack to C frame.
  3013. |1:
  3014. |.endif
  3015. | mov r13, TMPa
  3016. | mov r12, TMPQ
  3017. |.endif
  3018. | test RD, RD; js >3 // Check for error from exit.
  3019. | mov MULTRES, RD
  3020. | mov LFUNC:KBASE, [BASE-8]
  3021. | mov KBASE, LFUNC:KBASE->pc
  3022. | mov KBASE, [KBASE+PC2PROTO(k)]
  3023. | mov dword [DISPATCH+DISPATCH_GL(jit_L)], 0
  3024. | set_vmstate INTERP
  3025. | // Modified copy of ins_next which handles function header dispatch, too.
  3026. | mov RC, [PC]
  3027. | movzx RA, RCH
  3028. | movzx OP, RCL
  3029. | add PC, 4
  3030. | shr RC, 16
  3031. | cmp OP, BC_FUNCF // Function header?
  3032. | jb >2
  3033. | mov RC, MULTRES // RC/RD holds nres+1.
  3034. |2:
  3035. |.if X64
  3036. | jmp aword [DISPATCH+OP*8]
  3037. |.else
  3038. | jmp aword [DISPATCH+OP*4]
  3039. |.endif
  3040. |
  3041. |3: // Rethrow error from the right C frame.
  3042. | neg RD
  3043. | mov FCARG1, L:RB
  3044. | mov FCARG2, RD
  3045. | call extern lj_err_throw@8 // (lua_State *L, int errcode)
  3046. |.endif
  3047. |
  3048. |//-----------------------------------------------------------------------
  3049. |//-- Math helper functions ----------------------------------------------
  3050. |//-----------------------------------------------------------------------
  3051. |
  3052. |// FP value rounding. Called by math.floor/math.ceil fast functions
  3053. |// and from JIT code.
  3054. |
  3055. |// x87 variant: Arg/ret on x87 stack. No int/xmm registers modified.
  3056. |.macro vm_round_x87, mode1, mode2
  3057. | fnstcw word [esp+4] // Caveat: overwrites ARG1 and ARG2.
  3058. | mov [esp+8], eax
  3059. | mov ax, mode1
  3060. | or ax, [esp+4]
  3061. |.if mode2 ~= 0xffff
  3062. | and ax, mode2
  3063. |.endif
  3064. | mov [esp+6], ax
  3065. | fldcw word [esp+6]
  3066. | frndint
  3067. | fldcw word [esp+4]
  3068. | mov eax, [esp+8]
  3069. | ret
  3070. |.endmacro
  3071. |
  3072. |// SSE variant: arg/ret is xmm0. xmm0-xmm3 and RD (eax) modified.
  3073. |.macro vm_round_sse, mode
  3074. | sseconst_abs xmm2, RDa
  3075. | sseconst_2p52 xmm3, RDa
  3076. | movaps xmm1, xmm0
  3077. | andpd xmm1, xmm2 // |x|
  3078. | ucomisd xmm3, xmm1 // No truncation if 2^52 <= |x|.
  3079. | jbe >1
  3080. | andnpd xmm2, xmm0 // Isolate sign bit.
  3081. |.if mode == 2 // trunc(x)?
  3082. | movaps xmm0, xmm1
  3083. | addsd xmm1, xmm3 // (|x| + 2^52) - 2^52
  3084. | subsd xmm1, xmm3
  3085. | sseconst_1 xmm3, RDa
  3086. | cmpsd xmm0, xmm1, 1 // |x| < result?
  3087. | andpd xmm0, xmm3
  3088. | subsd xmm1, xmm0 // If yes, subtract -1.
  3089. | orpd xmm1, xmm2 // Merge sign bit back in.
  3090. |.else
  3091. | addsd xmm1, xmm3 // (|x| + 2^52) - 2^52
  3092. | subsd xmm1, xmm3
  3093. | orpd xmm1, xmm2 // Merge sign bit back in.
  3094. | .if mode == 1 // ceil(x)?
  3095. | sseconst_m1 xmm2, RDa // Must subtract -1 to preserve -0.
  3096. | cmpsd xmm0, xmm1, 6 // x > result?
  3097. | .else // floor(x)?
  3098. | sseconst_1 xmm2, RDa
  3099. | cmpsd xmm0, xmm1, 1 // x < result?
  3100. | .endif
  3101. | andpd xmm0, xmm2
  3102. | subsd xmm1, xmm0 // If yes, subtract +-1.
  3103. |.endif
  3104. | movaps xmm0, xmm1
  3105. |1:
  3106. | ret
  3107. |.endmacro
  3108. |
  3109. |.macro vm_round, name, ssemode, mode1, mode2
  3110. |->name:
  3111. |.if not SSE
  3112. | vm_round_x87 mode1, mode2
  3113. |.endif
  3114. |->name .. _sse:
  3115. | vm_round_sse ssemode
  3116. |.endmacro
  3117. |
  3118. | vm_round vm_floor, 0, 0x0400, 0xf7ff
  3119. | vm_round vm_ceil, 1, 0x0800, 0xfbff
  3120. | vm_round vm_trunc, 2, 0x0c00, 0xffff
  3121. |
  3122. |// FP modulo x%y. Called by BC_MOD* and vm_arith.
  3123. |->vm_mod:
  3124. |.if SSE
  3125. |// Args in xmm0/xmm1, return value in xmm0.
  3126. |// Caveat: xmm0-xmm5 and RC (eax) modified!
  3127. | movaps xmm5, xmm0
  3128. | divsd xmm0, xmm1
  3129. | sseconst_abs xmm2, RDa
  3130. | sseconst_2p52 xmm3, RDa
  3131. | movaps xmm4, xmm0
  3132. | andpd xmm4, xmm2 // |x/y|
  3133. | ucomisd xmm3, xmm4 // No truncation if 2^52 <= |x/y|.
  3134. | jbe >1
  3135. | andnpd xmm2, xmm0 // Isolate sign bit.
  3136. | addsd xmm4, xmm3 // (|x/y| + 2^52) - 2^52
  3137. | subsd xmm4, xmm3
  3138. | orpd xmm4, xmm2 // Merge sign bit back in.
  3139. | sseconst_1 xmm2, RDa
  3140. | cmpsd xmm0, xmm4, 1 // x/y < result?
  3141. | andpd xmm0, xmm2
  3142. | subsd xmm4, xmm0 // If yes, subtract 1.0.
  3143. | movaps xmm0, xmm5
  3144. | mulsd xmm1, xmm4
  3145. | subsd xmm0, xmm1
  3146. | ret
  3147. |1:
  3148. | mulsd xmm1, xmm0
  3149. | movaps xmm0, xmm5
  3150. | subsd xmm0, xmm1
  3151. | ret
  3152. |.else
  3153. |// Args/ret on x87 stack (y on top). No xmm registers modified.
  3154. |// Caveat: needs 3 slots on x87 stack! RC (eax) modified!
  3155. | fld st1
  3156. | fdiv st1
  3157. | fnstcw word [esp+4]
  3158. | mov ax, 0x0400
  3159. | or ax, [esp+4]
  3160. | and ax, 0xf7ff
  3161. | mov [esp+6], ax
  3162. | fldcw word [esp+6]
  3163. | frndint
  3164. | fldcw word [esp+4]
  3165. | fmulp st1
  3166. | fsubp st1
  3167. | ret
  3168. |.endif
  3169. |
  3170. |// FP log2(x). Called by math.log(x, base).
  3171. |->vm_log2:
  3172. |.if X64WIN
  3173. | movsd qword [rsp+8], xmm0 // Use scratch area.
  3174. | fld1
  3175. | fld qword [rsp+8]
  3176. | fyl2x
  3177. | fstp qword [rsp+8]
  3178. | movsd xmm0, qword [rsp+8]
  3179. |.elif X64
  3180. | movsd qword [rsp-8], xmm0 // Use red zone.
  3181. | fld1
  3182. | fld qword [rsp-8]
  3183. | fyl2x
  3184. | fstp qword [rsp-8]
  3185. | movsd xmm0, qword [rsp-8]
  3186. |.else
  3187. | fld1
  3188. | fld qword [esp+4]
  3189. | fyl2x
  3190. |.endif
  3191. | ret
  3192. |
  3193. |// FP exponentiation e^x and 2^x. Called by math.exp fast function and
  3194. |// from JIT code. Arg/ret on x87 stack. No int/xmm regs modified.
  3195. |// Caveat: needs 3 slots on x87 stack!
  3196. |->vm_exp_x87:
  3197. | fldl2e; fmulp st1 // e^x ==> 2^(x*log2(e))
  3198. |->vm_exp2_x87:
  3199. | .if X64WIN
  3200. | .define expscratch, dword [rsp+8] // Use scratch area.
  3201. | .elif X64
  3202. | .define expscratch, dword [rsp-8] // Use red zone.
  3203. | .else
  3204. | .define expscratch, dword [esp+4] // Needs 4 byte scratch area.
  3205. | .endif
  3206. | fst expscratch // Caveat: overwrites ARG1.
  3207. | cmp expscratch, 0x7f800000; je >1 // Special case: e^+Inf = +Inf
  3208. | cmp expscratch, 0xff800000; je >2 // Special case: e^-Inf = 0
  3209. |->vm_exp2raw: // Entry point for vm_pow. Without +-Inf check.
  3210. | fdup; frndint; fsub st1, st0; fxch // Split into frac/int part.
  3211. | f2xm1; fld1; faddp st1; fscale; fpop1 // ==> (2^frac-1 +1) << int
  3212. |1:
  3213. | ret
  3214. |2:
  3215. | fpop; fldz; ret
  3216. |
  3217. |// Generic power function x^y. Called by BC_POW, math.pow fast function,
  3218. |// and vm_arith.
  3219. |// Args/ret on x87 stack (y on top). RC (eax) modified.
  3220. |// Caveat: needs 3 slots on x87 stack!
  3221. |->vm_pow:
  3222. |.if not SSE
  3223. | fist dword [esp+4] // Store/reload int before comparison.
  3224. | fild dword [esp+4] // Integral exponent used in vm_powi.
  3225. | fucomip st1
  3226. | jnz >8 // Branch for FP exponents.
  3227. | jp >9 // Branch for NaN exponent.
  3228. | fpop // Pop y and fallthrough to vm_powi.
  3229. |
  3230. |// FP/int power function x^i. Arg1/ret on x87 stack.
  3231. |// Arg2 (int) on C stack. RC (eax) modified.
  3232. |// Caveat: needs 2 slots on x87 stack!
  3233. | mov eax, [esp+4]
  3234. | cmp eax, 1; jle >6 // i<=1?
  3235. | // Now 1 < (unsigned)i <= 0x80000000.
  3236. |1: // Handle leading zeros.
  3237. | test eax, 1; jnz >2
  3238. | fmul st0
  3239. | shr eax, 1
  3240. | jmp <1
  3241. |2:
  3242. | shr eax, 1; jz >5
  3243. | fdup
  3244. |3: // Handle trailing bits.
  3245. | fmul st0
  3246. | shr eax, 1; jz >4
  3247. | jnc <3
  3248. | fmul st1, st0
  3249. | jmp <3
  3250. |4:
  3251. | fmulp st1
  3252. |5:
  3253. | ret
  3254. |6:
  3255. | je <5 // x^1 ==> x
  3256. | jb >7
  3257. | fld1; fdivrp st1
  3258. | neg eax
  3259. | cmp eax, 1; je <5 // x^-1 ==> 1/x
  3260. | jmp <1 // x^-i ==> (1/x)^i
  3261. |7:
  3262. | fpop; fld1 // x^0 ==> 1
  3263. | ret
  3264. |
  3265. |8: // FP/FP power function x^y.
  3266. | fst dword [esp+4]
  3267. | fxch
  3268. | fst dword [esp+8]
  3269. | mov eax, [esp+4]; shl eax, 1
  3270. | cmp eax, 0xff000000; je >2 // x^+-Inf?
  3271. | mov eax, [esp+8]; shl eax, 1; je >4 // +-0^y?
  3272. | cmp eax, 0xff000000; je >4 // +-Inf^y?
  3273. | fyl2x
  3274. | jmp ->vm_exp2raw
  3275. |
  3276. |9: // Handle x^NaN.
  3277. | fld1
  3278. | fucomip st2
  3279. | je >1 // 1^NaN ==> 1
  3280. | fxch // x^NaN ==> NaN
  3281. |1:
  3282. | fpop
  3283. | ret
  3284. |
  3285. |2: // Handle x^+-Inf.
  3286. | fabs
  3287. | fld1
  3288. | fucomip st1
  3289. | je >3 // +-1^+-Inf ==> 1
  3290. | fpop; fabs; fldz; mov eax, 0; setc al
  3291. | ror eax, 1; xor eax, [esp+4]; jns >3 // |x|<>1, x^+-Inf ==> +Inf/0
  3292. | fxch
  3293. |3:
  3294. | fpop1; fabs
  3295. | ret
  3296. |
  3297. |4: // Handle +-0^y or +-Inf^y.
  3298. | cmp dword [esp+4], 0; jge <3 // y >= 0, x^y ==> |x|
  3299. | fpop; fpop
  3300. | test eax, eax; jz >5 // y < 0, +-0^y ==> +Inf
  3301. | fldz // y < 0, +-Inf^y ==> 0
  3302. | ret
  3303. |5:
  3304. | mov dword [esp+4], 0x7f800000 // Return +Inf.
  3305. | fld dword [esp+4]
  3306. | ret
  3307. |.endif
  3308. |
  3309. |// Args in xmm0/xmm1. Ret in xmm0. xmm0-xmm2 and RC (eax) modified.
  3310. |// Needs 16 byte scratch area for x86. Also called from JIT code.
  3311. |->vm_pow_sse:
  3312. | cvtsd2si eax, xmm1
  3313. | cvtsi2sd xmm2, eax
  3314. | ucomisd xmm1, xmm2
  3315. | jnz >8 // Branch for FP exponents.
  3316. | jp >9 // Branch for NaN exponent.
  3317. | // Fallthrough to vm_powi_sse.
  3318. |
  3319. |// Args in xmm0/eax. Ret in xmm0. xmm0-xmm1 and eax modified.
  3320. |->vm_powi_sse:
  3321. | cmp eax, 1; jle >6 // i<=1?
  3322. | // Now 1 < (unsigned)i <= 0x80000000.
  3323. |1: // Handle leading zeros.
  3324. | test eax, 1; jnz >2
  3325. | mulsd xmm0, xmm0
  3326. | shr eax, 1
  3327. | jmp <1
  3328. |2:
  3329. | shr eax, 1; jz >5
  3330. | movaps xmm1, xmm0
  3331. |3: // Handle trailing bits.
  3332. | mulsd xmm0, xmm0
  3333. | shr eax, 1; jz >4
  3334. | jnc <3
  3335. | mulsd xmm1, xmm0
  3336. | jmp <3
  3337. |4:
  3338. | mulsd xmm0, xmm1
  3339. |5:
  3340. | ret
  3341. |6:
  3342. | je <5 // x^1 ==> x
  3343. | jb >7 // x^0 ==> 1
  3344. | neg eax
  3345. | call <1
  3346. | sseconst_1 xmm1, RDa
  3347. | divsd xmm1, xmm0
  3348. | movaps xmm0, xmm1
  3349. | ret
  3350. |7:
  3351. | sseconst_1 xmm0, RDa
  3352. | ret
  3353. |
  3354. |8: // FP/FP power function x^y.
  3355. |.if X64
  3356. | movd rax, xmm1; shl rax, 1
  3357. | rol rax, 12; cmp rax, 0xffe; je >2 // x^+-Inf?
  3358. | movd rax, xmm0; shl rax, 1; je >4 // +-0^y?
  3359. | rol rax, 12; cmp rax, 0xffe; je >5 // +-Inf^y?
  3360. | .if X64WIN
  3361. | movsd qword [rsp+16], xmm1 // Use scratch area.
  3362. | movsd qword [rsp+8], xmm0
  3363. | fld qword [rsp+16]
  3364. | fld qword [rsp+8]
  3365. | .else
  3366. | movsd qword [rsp-16], xmm1 // Use red zone.
  3367. | movsd qword [rsp-8], xmm0
  3368. | fld qword [rsp-16]
  3369. | fld qword [rsp-8]
  3370. | .endif
  3371. |.else
  3372. | movsd qword [esp+12], xmm1 // Needs 16 byte scratch area.
  3373. | movsd qword [esp+4], xmm0
  3374. | cmp dword [esp+12], 0; jne >1
  3375. | mov eax, [esp+16]; shl eax, 1
  3376. | cmp eax, 0xffe00000; je >2 // x^+-Inf?
  3377. |1:
  3378. | cmp dword [esp+4], 0; jne >1
  3379. | mov eax, [esp+8]; shl eax, 1; je >4 // +-0^y?
  3380. | cmp eax, 0xffe00000; je >5 // +-Inf^y?
  3381. |1:
  3382. | fld qword [esp+12]
  3383. | fld qword [esp+4]
  3384. |.endif
  3385. | fyl2x // y*log2(x)
  3386. | fdup; frndint; fsub st1, st0; fxch // Split into frac/int part.
  3387. | f2xm1; fld1; faddp st1; fscale; fpop1 // ==> (2^frac-1 +1) << int
  3388. |.if X64WIN
  3389. | fstp qword [rsp+8] // Use scratch area.
  3390. | movsd xmm0, qword [rsp+8]
  3391. |.elif X64
  3392. | fstp qword [rsp-8] // Use red zone.
  3393. | movsd xmm0, qword [rsp-8]
  3394. |.else
  3395. | fstp qword [esp+4] // Needs 8 byte scratch area.
  3396. | movsd xmm0, qword [esp+4]
  3397. |.endif
  3398. | ret
  3399. |
  3400. |9: // Handle x^NaN.
  3401. | sseconst_1 xmm2, RDa
  3402. | ucomisd xmm0, xmm2; je >1 // 1^NaN ==> 1
  3403. | movaps xmm0, xmm1 // x^NaN ==> NaN
  3404. |1:
  3405. | ret
  3406. |
  3407. |2: // Handle x^+-Inf.
  3408. | sseconst_abs xmm2, RDa
  3409. | andpd xmm0, xmm2 // |x|
  3410. | sseconst_1 xmm2, RDa
  3411. | ucomisd xmm0, xmm2; je <1 // +-1^+-Inf ==> 1
  3412. | movmskpd eax, xmm1
  3413. | xorps xmm0, xmm0
  3414. | mov ah, al; setc al; xor al, ah; jne <1 // |x|<>1, x^+-Inf ==> +Inf/0
  3415. |3:
  3416. | sseconst_hi xmm0, RDa, 7ff00000 // +Inf
  3417. | ret
  3418. |
  3419. |4: // Handle +-0^y.
  3420. | movmskpd eax, xmm1; test eax, eax; jnz <3 // y < 0, +-0^y ==> +Inf
  3421. | xorps xmm0, xmm0 // y >= 0, +-0^y ==> 0
  3422. | ret
  3423. |
  3424. |5: // Handle +-Inf^y.
  3425. | movmskpd eax, xmm1; test eax, eax; jz <3 // y >= 0, +-Inf^y ==> +Inf
  3426. | xorps xmm0, xmm0 // y < 0, +-Inf^y ==> 0
  3427. | ret
  3428. |
  3429. |// Callable from C: double lj_vm_foldfpm(double x, int fpm)
  3430. |// Computes fpm(x) for extended math functions. ORDER FPM.
  3431. |->vm_foldfpm:
  3432. |.if JIT
  3433. |.if X64
  3434. | .if X64WIN
  3435. | .define fpmop, CARG2d
  3436. | .else
  3437. | .define fpmop, CARG1d
  3438. | .endif
  3439. | cmp fpmop, 1; jb ->vm_floor; je ->vm_ceil
  3440. | cmp fpmop, 3; jb ->vm_trunc; ja >2
  3441. | sqrtsd xmm0, xmm0; ret
  3442. |2:
  3443. | .if X64WIN
  3444. | movsd qword [rsp+8], xmm0 // Use scratch area.
  3445. | fld qword [rsp+8]
  3446. | .else
  3447. | movsd qword [rsp-8], xmm0 // Use red zone.
  3448. | fld qword [rsp-8]
  3449. | .endif
  3450. | cmp fpmop, 5; ja >2
  3451. | .if X64WIN; pop rax; .endif
  3452. | je >1
  3453. | call ->vm_exp_x87
  3454. | .if X64WIN; push rax; .endif
  3455. | jmp >7
  3456. |1:
  3457. | call ->vm_exp2_x87
  3458. | .if X64WIN; push rax; .endif
  3459. | jmp >7
  3460. |2: ; cmp fpmop, 7; je >1; ja >2
  3461. | fldln2; fxch; fyl2x; jmp >7
  3462. |1: ; fld1; fxch; fyl2x; jmp >7
  3463. |2: ; cmp fpmop, 9; je >1; ja >2
  3464. | fldlg2; fxch; fyl2x; jmp >7
  3465. |1: ; fsin; jmp >7
  3466. |2: ; cmp fpmop, 11; je >1; ja >9
  3467. | fcos; jmp >7
  3468. |1: ; fptan; fpop
  3469. |7:
  3470. | .if X64WIN
  3471. | fstp qword [rsp+8] // Use scratch area.
  3472. | movsd xmm0, qword [rsp+8]
  3473. | .else
  3474. | fstp qword [rsp-8] // Use red zone.
  3475. | movsd xmm0, qword [rsp-8]
  3476. | .endif
  3477. | ret
  3478. |.else // x86 calling convention.
  3479. | .define fpmop, eax
  3480. |.if SSE
  3481. | mov fpmop, [esp+12]
  3482. | movsd xmm0, qword [esp+4]
  3483. | cmp fpmop, 1; je >1; ja >2
  3484. | call ->vm_floor; jmp >7
  3485. |1: ; call ->vm_ceil; jmp >7
  3486. |2: ; cmp fpmop, 3; je >1; ja >2
  3487. | call ->vm_trunc; jmp >7
  3488. |1:
  3489. | sqrtsd xmm0, xmm0
  3490. |7:
  3491. | movsd qword [esp+4], xmm0 // Overwrite callee-owned args.
  3492. | fld qword [esp+4]
  3493. | ret
  3494. |2: ; fld qword [esp+4]
  3495. | cmp fpmop, 5; jb ->vm_exp_x87; je ->vm_exp2_x87
  3496. |2: ; cmp fpmop, 7; je >1; ja >2
  3497. | fldln2; fxch; fyl2x; ret
  3498. |1: ; fld1; fxch; fyl2x; ret
  3499. |2: ; cmp fpmop, 9; je >1; ja >2
  3500. | fldlg2; fxch; fyl2x; ret
  3501. |1: ; fsin; ret
  3502. |2: ; cmp fpmop, 11; je >1; ja >9
  3503. | fcos; ret
  3504. |1: ; fptan; fpop; ret
  3505. |.else
  3506. | mov fpmop, [esp+12]
  3507. | fld qword [esp+4]
  3508. | cmp fpmop, 1; jb ->vm_floor; je ->vm_ceil
  3509. | cmp fpmop, 3; jb ->vm_trunc; ja >2
  3510. | fsqrt; ret
  3511. |2: ; cmp fpmop, 5; jb ->vm_exp_x87; je ->vm_exp2_x87
  3512. | cmp fpmop, 7; je >1; ja >2
  3513. | fldln2; fxch; fyl2x; ret
  3514. |1: ; fld1; fxch; fyl2x; ret
  3515. |2: ; cmp fpmop, 9; je >1; ja >2
  3516. | fldlg2; fxch; fyl2x; ret
  3517. |1: ; fsin; ret
  3518. |2: ; cmp fpmop, 11; je >1; ja >9
  3519. | fcos; ret
  3520. |1: ; fptan; fpop; ret
  3521. |.endif
  3522. |.endif
  3523. |9: ; int3 // Bad fpm.
  3524. |.endif
  3525. |
  3526. |// Callable from C: double lj_vm_foldarith(double x, double y, int op)
  3527. |// Compute x op y for basic arithmetic operators (+ - * / % ^ and unary -)
  3528. |// and basic math functions. ORDER ARITH
  3529. |->vm_foldarith:
  3530. |.if X64
  3531. |
  3532. | .if X64WIN
  3533. | .define foldop, CARG3d
  3534. | .else
  3535. | .define foldop, CARG1d
  3536. | .endif
  3537. | cmp foldop, 1; je >1; ja >2
  3538. | addsd xmm0, xmm1; ret
  3539. |1: ; subsd xmm0, xmm1; ret
  3540. |2: ; cmp foldop, 3; je >1; ja >2
  3541. | mulsd xmm0, xmm1; ret
  3542. |1: ; divsd xmm0, xmm1; ret
  3543. |2: ; cmp foldop, 5; jb ->vm_mod; je ->vm_pow
  3544. | cmp foldop, 7; je >1; ja >2
  3545. | sseconst_sign xmm1, RDa; xorps xmm0, xmm1; ret
  3546. |1: ; sseconst_abs xmm1, RDa; andps xmm0, xmm1; ret
  3547. |2: ; cmp foldop, 9; ja >2
  3548. |.if X64WIN
  3549. | movsd qword [rsp+8], xmm0 // Use scratch area.
  3550. | movsd qword [rsp+16], xmm1
  3551. | fld qword [rsp+8]
  3552. | fld qword [rsp+16]
  3553. |.else
  3554. | movsd qword [rsp-8], xmm0 // Use red zone.
  3555. | movsd qword [rsp-16], xmm1
  3556. | fld qword [rsp-8]
  3557. | fld qword [rsp-16]
  3558. |.endif
  3559. | je >1
  3560. | fpatan
  3561. |7:
  3562. |.if X64WIN
  3563. | fstp qword [rsp+8] // Use scratch area.
  3564. | movsd xmm0, qword [rsp+8]
  3565. |.else
  3566. | fstp qword [rsp-8] // Use red zone.
  3567. | movsd xmm0, qword [rsp-8]
  3568. |.endif
  3569. | ret
  3570. |1: ; fxch; fscale; fpop1; jmp <7
  3571. |2: ; cmp foldop, 11; je >1; ja >9
  3572. | minsd xmm0, xmm1; ret
  3573. |1: ; maxsd xmm0, xmm1; ret
  3574. |9: ; int3 // Bad op.
  3575. |
  3576. |.elif SSE // x86 calling convention with SSE ops.
  3577. |
  3578. | .define foldop, eax
  3579. | mov foldop, [esp+20]
  3580. | movsd xmm0, qword [esp+4]
  3581. | movsd xmm1, qword [esp+12]
  3582. | cmp foldop, 1; je >1; ja >2
  3583. | addsd xmm0, xmm1
  3584. |7:
  3585. | movsd qword [esp+4], xmm0 // Overwrite callee-owned args.
  3586. | fld qword [esp+4]
  3587. | ret
  3588. |1: ; subsd xmm0, xmm1; jmp <7
  3589. |2: ; cmp foldop, 3; je >1; ja >2
  3590. | mulsd xmm0, xmm1; jmp <7
  3591. |1: ; divsd xmm0, xmm1; jmp <7
  3592. |2: ; cmp foldop, 5
  3593. | je >1; ja >2
  3594. | call ->vm_mod; jmp <7
  3595. |1: ; pop edx; call ->vm_pow; push edx; jmp <7 // Writes to scratch area.
  3596. |2: ; cmp foldop, 7; je >1; ja >2
  3597. | sseconst_sign xmm1, RDa; xorps xmm0, xmm1; jmp <7
  3598. |1: ; sseconst_abs xmm1, RDa; andps xmm0, xmm1; jmp <7
  3599. |2: ; cmp foldop, 9; ja >2
  3600. | fld qword [esp+4] // Reload from stack
  3601. | fld qword [esp+12]
  3602. | je >1
  3603. | fpatan; ret
  3604. |1: ; fxch; fscale; fpop1; ret
  3605. |2: ; cmp foldop, 11; je >1; ja >9
  3606. | minsd xmm0, xmm1; jmp <7
  3607. |1: ; maxsd xmm0, xmm1; jmp <7
  3608. |9: ; int3 // Bad op.
  3609. |
  3610. |.else // x86 calling convention with x87 ops.
  3611. |
  3612. | mov eax, [esp+20]
  3613. | fld qword [esp+4]
  3614. | fld qword [esp+12]
  3615. | cmp eax, 1; je >1; ja >2
  3616. | faddp st1; ret
  3617. |1: ; fsubp st1; ret
  3618. |2: ; cmp eax, 3; je >1; ja >2
  3619. | fmulp st1; ret
  3620. |1: ; fdivp st1; ret
  3621. |2: ; cmp eax, 5; jb ->vm_mod; je ->vm_pow
  3622. | cmp eax, 7; je >1; ja >2
  3623. | fpop; fchs; ret
  3624. |1: ; fpop; fabs; ret
  3625. |2: ; cmp eax, 9; je >1; ja >2
  3626. | fpatan; ret
  3627. |1: ; fxch; fscale; fpop1; ret
  3628. |2: ; cmp eax, 11; je >1; ja >9
  3629. | fucomi st1; fcmovnbe st1; fpop1; ret
  3630. |1: ; fucomi st1; fcmovbe st1; fpop1; ret
  3631. |9: ; int3 // Bad op.
  3632. |
  3633. |.endif
  3634. |
  3635. |//-----------------------------------------------------------------------
  3636. |//-- Miscellaneous functions --------------------------------------------
  3637. |//-----------------------------------------------------------------------
  3638. |
  3639. |// int lj_vm_cpuid(uint32_t f, uint32_t res[4])
  3640. |->vm_cpuid:
  3641. |.if X64
  3642. | mov eax, CARG1d
  3643. | .if X64WIN; push rsi; mov rsi, CARG2; .endif
  3644. | push rbx
  3645. | cpuid
  3646. | mov [rsi], eax
  3647. | mov [rsi+4], ebx
  3648. | mov [rsi+8], ecx
  3649. | mov [rsi+12], edx
  3650. | pop rbx
  3651. | .if X64WIN; pop rsi; .endif
  3652. | ret
  3653. |.else
  3654. | pushfd
  3655. | pop edx
  3656. | mov ecx, edx
  3657. | xor edx, 0x00200000 // Toggle ID bit in flags.
  3658. | push edx
  3659. | popfd
  3660. | pushfd
  3661. | pop edx
  3662. | xor eax, eax // Zero means no features supported.
  3663. | cmp ecx, edx
  3664. | jz >1 // No ID toggle means no CPUID support.
  3665. | mov eax, [esp+4] // Argument 1 is function number.
  3666. | push edi
  3667. | push ebx
  3668. | cpuid
  3669. | mov edi, [esp+16] // Argument 2 is result area.
  3670. | mov [edi], eax
  3671. | mov [edi+4], ebx
  3672. | mov [edi+8], ecx
  3673. | mov [edi+12], edx
  3674. | pop ebx
  3675. | pop edi
  3676. |1:
  3677. | ret
  3678. |.endif
  3679. |
  3680. |//-----------------------------------------------------------------------
  3681. |//-- Assertions ---------------------------------------------------------
  3682. |//-----------------------------------------------------------------------
  3683. |
  3684. |->assert_bad_for_arg_type:
  3685. #ifdef LUA_USE_ASSERT
  3686. | int3
  3687. #endif
  3688. | int3
  3689. |
  3690. |//-----------------------------------------------------------------------
  3691. |//-- FFI helper functions -----------------------------------------------
  3692. |//-----------------------------------------------------------------------
  3693. |
  3694. |// Handler for callback functions. Callback slot number in ah/al.
  3695. |->vm_ffi_callback:
  3696. |.if FFI
  3697. |.type CTSTATE, CTState, PC
  3698. |.if not X64
  3699. | sub esp, 16 // Leave room for SAVE_ERRF etc.
  3700. |.endif
  3701. | saveregs_ // ebp/rbp already saved. ebp now holds global_State *.
  3702. | lea DISPATCH, [ebp+GG_G2DISP]
  3703. | mov CTSTATE, GL:ebp->ctype_state
  3704. | movzx eax, ax
  3705. | mov CTSTATE->cb.slot, eax
  3706. |.if X64
  3707. | mov CTSTATE->cb.gpr[0], CARG1
  3708. | mov CTSTATE->cb.gpr[1], CARG2
  3709. | mov CTSTATE->cb.gpr[2], CARG3
  3710. | mov CTSTATE->cb.gpr[3], CARG4
  3711. | movsd qword CTSTATE->cb.fpr[0], xmm0
  3712. | movsd qword CTSTATE->cb.fpr[1], xmm1
  3713. | movsd qword CTSTATE->cb.fpr[2], xmm2
  3714. | movsd qword CTSTATE->cb.fpr[3], xmm3
  3715. |.if X64WIN
  3716. | lea rax, [rsp+CFRAME_SIZE+4*8]
  3717. |.else
  3718. | lea rax, [rsp+CFRAME_SIZE]
  3719. | mov CTSTATE->cb.gpr[4], CARG5
  3720. | mov CTSTATE->cb.gpr[5], CARG6
  3721. | movsd qword CTSTATE->cb.fpr[4], xmm4
  3722. | movsd qword CTSTATE->cb.fpr[5], xmm5
  3723. | movsd qword CTSTATE->cb.fpr[6], xmm6
  3724. | movsd qword CTSTATE->cb.fpr[7], xmm7
  3725. |.endif
  3726. | mov CTSTATE->cb.stack, rax
  3727. | mov CARG2, rsp
  3728. |.else
  3729. | lea eax, [esp+CFRAME_SIZE+16]
  3730. | mov CTSTATE->cb.gpr[0], FCARG1
  3731. | mov CTSTATE->cb.gpr[1], FCARG2
  3732. | mov CTSTATE->cb.stack, eax
  3733. | mov FCARG1, [esp+CFRAME_SIZE+12] // Move around misplaced retaddr/ebp.
  3734. | mov FCARG2, [esp+CFRAME_SIZE+8]
  3735. | mov SAVE_RET, FCARG1
  3736. | mov SAVE_R4, FCARG2
  3737. | mov FCARG2, esp
  3738. |.endif
  3739. | mov SAVE_PC, CTSTATE // Any value outside of bytecode is ok.
  3740. | mov FCARG1, CTSTATE
  3741. | call extern lj_ccallback_enter@8 // (CTState *cts, void *cf)
  3742. | // lua_State * returned in eax (RD).
  3743. | set_vmstate INTERP
  3744. | mov BASE, L:RD->base
  3745. | mov RD, L:RD->top
  3746. | sub RD, BASE
  3747. | mov LFUNC:RB, [BASE-8]
  3748. | shr RD, 3
  3749. | add RD, 1
  3750. | ins_callt
  3751. |.endif
  3752. |
  3753. |->cont_ffi_callback: // Return from FFI callback.
  3754. |.if FFI
  3755. | mov L:RA, SAVE_L
  3756. | mov CTSTATE, [DISPATCH+DISPATCH_GL(ctype_state)]
  3757. | mov aword CTSTATE->L, L:RAa
  3758. | mov L:RA->base, BASE
  3759. | mov L:RA->top, RB
  3760. | mov FCARG1, CTSTATE
  3761. | mov FCARG2, RC
  3762. | call extern lj_ccallback_leave@8 // (CTState *cts, TValue *o)
  3763. |.if X64
  3764. | mov rax, CTSTATE->cb.gpr[0]
  3765. | movsd xmm0, qword CTSTATE->cb.fpr[0]
  3766. | jmp ->vm_leave_unw
  3767. |.else
  3768. | mov L:RB, SAVE_L
  3769. | mov eax, CTSTATE->cb.gpr[0]
  3770. | mov edx, CTSTATE->cb.gpr[1]
  3771. | cmp dword CTSTATE->cb.gpr[2], 1
  3772. | jb >7
  3773. | je >6
  3774. | fld qword CTSTATE->cb.fpr[0].d
  3775. | jmp >7
  3776. |6:
  3777. | fld dword CTSTATE->cb.fpr[0].f
  3778. |7:
  3779. | mov ecx, L:RB->top
  3780. | movzx ecx, word [ecx+6] // Get stack adjustment and copy up.
  3781. | mov SAVE_L, ecx // Must be one slot above SAVE_RET
  3782. | restoreregs
  3783. | pop ecx // Move return addr from SAVE_RET.
  3784. | add esp, [esp] // Adjust stack.
  3785. | add esp, 16
  3786. | push ecx
  3787. | ret
  3788. |.endif
  3789. |.endif
  3790. |
  3791. |->vm_ffi_call@4: // Call C function via FFI.
  3792. | // Caveat: needs special frame unwinding, see below.
  3793. |.if FFI
  3794. |.if X64
  3795. | .type CCSTATE, CCallState, rbx
  3796. | push rbp; mov rbp, rsp; push rbx; mov CCSTATE, CARG1
  3797. |.else
  3798. | .type CCSTATE, CCallState, ebx
  3799. | push ebp; mov ebp, esp; push ebx; mov CCSTATE, FCARG1
  3800. |.endif
  3801. |
  3802. | // Readjust stack.
  3803. |.if X64
  3804. | mov eax, CCSTATE->spadj
  3805. | sub rsp, rax
  3806. |.else
  3807. | sub esp, CCSTATE->spadj
  3808. |.if WIN
  3809. | mov CCSTATE->spadj, esp
  3810. |.endif
  3811. |.endif
  3812. |
  3813. | // Copy stack slots.
  3814. | movzx ecx, byte CCSTATE->nsp
  3815. | sub ecx, 1
  3816. | js >2
  3817. |1:
  3818. |.if X64
  3819. | mov rax, [CCSTATE+rcx*8+offsetof(CCallState, stack)]
  3820. | mov [rsp+rcx*8+CCALL_SPS_EXTRA*8], rax
  3821. |.else
  3822. | mov eax, [CCSTATE+ecx*4+offsetof(CCallState, stack)]
  3823. | mov [esp+ecx*4], eax
  3824. |.endif
  3825. | sub ecx, 1
  3826. | jns <1
  3827. |2:
  3828. |
  3829. |.if X64
  3830. | movzx eax, byte CCSTATE->nfpr
  3831. | mov CARG1, CCSTATE->gpr[0]
  3832. | mov CARG2, CCSTATE->gpr[1]
  3833. | mov CARG3, CCSTATE->gpr[2]
  3834. | mov CARG4, CCSTATE->gpr[3]
  3835. |.if not X64WIN
  3836. | mov CARG5, CCSTATE->gpr[4]
  3837. | mov CARG6, CCSTATE->gpr[5]
  3838. |.endif
  3839. | test eax, eax; jz >5
  3840. | movaps xmm0, CCSTATE->fpr[0]
  3841. | movaps xmm1, CCSTATE->fpr[1]
  3842. | movaps xmm2, CCSTATE->fpr[2]
  3843. | movaps xmm3, CCSTATE->fpr[3]
  3844. |.if not X64WIN
  3845. | cmp eax, 4; jbe >5
  3846. | movaps xmm4, CCSTATE->fpr[4]
  3847. | movaps xmm5, CCSTATE->fpr[5]
  3848. | movaps xmm6, CCSTATE->fpr[6]
  3849. | movaps xmm7, CCSTATE->fpr[7]
  3850. |.endif
  3851. |5:
  3852. |.else
  3853. | mov FCARG1, CCSTATE->gpr[0]
  3854. | mov FCARG2, CCSTATE->gpr[1]
  3855. |.endif
  3856. |
  3857. | call aword CCSTATE->func
  3858. |
  3859. |.if X64
  3860. | mov CCSTATE->gpr[0], rax
  3861. | movaps CCSTATE->fpr[0], xmm0
  3862. |.if not X64WIN
  3863. | mov CCSTATE->gpr[1], rdx
  3864. | movaps CCSTATE->fpr[1], xmm1
  3865. |.endif
  3866. |.else
  3867. | mov CCSTATE->gpr[0], eax
  3868. | mov CCSTATE->gpr[1], edx
  3869. | cmp byte CCSTATE->resx87, 1
  3870. | jb >7
  3871. | je >6
  3872. | fstp qword CCSTATE->fpr[0].d[0]
  3873. | jmp >7
  3874. |6:
  3875. | fstp dword CCSTATE->fpr[0].f[0]
  3876. |7:
  3877. |.if WIN
  3878. | sub CCSTATE->spadj, esp
  3879. |.endif
  3880. |.endif
  3881. |
  3882. |.if X64
  3883. | mov rbx, [rbp-8]; leave; ret
  3884. |.else
  3885. | mov ebx, [ebp-4]; leave; ret
  3886. |.endif
  3887. |.endif
  3888. |// Note: vm_ffi_call must be the last function in this object file!
  3889. |
  3890. |//-----------------------------------------------------------------------
  3891. }
  3892. /* Generate the code for a single instruction. */
  3893. static void build_ins(BuildCtx *ctx, BCOp op, int defop)
  3894. {
  3895. int vk = 0;
  3896. |// Note: aligning all instructions does not pay off.
  3897. |=>defop:
  3898. switch (op) {
  3899. /* -- Comparison ops ---------------------------------------------------- */
  3900. /* Remember: all ops branch for a true comparison, fall through otherwise. */
  3901. |.macro jmp_comp, lt, ge, le, gt, target
  3902. ||switch (op) {
  3903. ||case BC_ISLT:
  3904. | lt target
  3905. ||break;
  3906. ||case BC_ISGE:
  3907. | ge target
  3908. ||break;
  3909. ||case BC_ISLE:
  3910. | le target
  3911. ||break;
  3912. ||case BC_ISGT:
  3913. | gt target
  3914. ||break;
  3915. ||default: break; /* Shut up GCC. */
  3916. ||}
  3917. |.endmacro
  3918. case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT:
  3919. | // RA = src1, RD = src2, JMP with RD = target
  3920. | ins_AD
  3921. |.if DUALNUM
  3922. | checkint RA, >7
  3923. | checkint RD, >8
  3924. | mov RB, dword [BASE+RA*8]
  3925. | add PC, 4
  3926. | cmp RB, dword [BASE+RD*8]
  3927. | jmp_comp jge, jl, jg, jle, >9
  3928. |6:
  3929. | movzx RD, PC_RD
  3930. | branchPC RD
  3931. |9:
  3932. | ins_next
  3933. |
  3934. |7: // RA is not an integer.
  3935. | ja ->vmeta_comp
  3936. | // RA is a number.
  3937. | cmp dword [BASE+RD*8+4], LJ_TISNUM; jb >1; jne ->vmeta_comp
  3938. | // RA is a number, RD is an integer.
  3939. |.if SSE
  3940. | cvtsi2sd xmm0, dword [BASE+RD*8]
  3941. | jmp >2
  3942. |.else
  3943. | fld qword [BASE+RA*8]
  3944. | fild dword [BASE+RD*8]
  3945. | jmp >3
  3946. |.endif
  3947. |
  3948. |8: // RA is an integer, RD is not an integer.
  3949. | ja ->vmeta_comp
  3950. | // RA is an integer, RD is a number.
  3951. |.if SSE
  3952. | cvtsi2sd xmm1, dword [BASE+RA*8]
  3953. | movsd xmm0, qword [BASE+RD*8]
  3954. | add PC, 4
  3955. | ucomisd xmm0, xmm1
  3956. | jmp_comp jbe, ja, jb, jae, <9
  3957. | jmp <6
  3958. |.else
  3959. | fild dword [BASE+RA*8]
  3960. | jmp >2
  3961. |.endif
  3962. |.else
  3963. | checknum RA, ->vmeta_comp
  3964. | checknum RD, ->vmeta_comp
  3965. |.endif
  3966. |.if SSE
  3967. |1:
  3968. | movsd xmm0, qword [BASE+RD*8]
  3969. |2:
  3970. | add PC, 4
  3971. | ucomisd xmm0, qword [BASE+RA*8]
  3972. |3:
  3973. |.else
  3974. |1:
  3975. | fld qword [BASE+RA*8] // Reverse order, i.e like cmp D, A.
  3976. |2:
  3977. | fld qword [BASE+RD*8]
  3978. |3:
  3979. | add PC, 4
  3980. | fcomparepp
  3981. |.endif
  3982. | // Unordered: all of ZF CF PF set, ordered: PF clear.
  3983. | // To preserve NaN semantics GE/GT branch on unordered, but LT/LE don't.
  3984. |.if DUALNUM
  3985. | jmp_comp jbe, ja, jb, jae, <9
  3986. | jmp <6
  3987. |.else
  3988. | jmp_comp jbe, ja, jb, jae, >1
  3989. | movzx RD, PC_RD
  3990. | branchPC RD
  3991. |1:
  3992. | ins_next
  3993. |.endif
  3994. break;
  3995. case BC_ISEQV: case BC_ISNEV:
  3996. vk = op == BC_ISEQV;
  3997. | ins_AD // RA = src1, RD = src2, JMP with RD = target
  3998. | mov RB, [BASE+RD*8+4]
  3999. | add PC, 4
  4000. |.if DUALNUM
  4001. | cmp RB, LJ_TISNUM; jne >7
  4002. | checkint RA, >8
  4003. | mov RB, dword [BASE+RD*8]
  4004. | cmp RB, dword [BASE+RA*8]
  4005. if (vk) {
  4006. | jne >9
  4007. } else {
  4008. | je >9
  4009. }
  4010. | movzx RD, PC_RD
  4011. | branchPC RD
  4012. |9:
  4013. | ins_next
  4014. |
  4015. |7: // RD is not an integer.
  4016. | ja >5
  4017. | // RD is a number.
  4018. | cmp dword [BASE+RA*8+4], LJ_TISNUM; jb >1; jne >5
  4019. | // RD is a number, RA is an integer.
  4020. |.if SSE
  4021. | cvtsi2sd xmm0, dword [BASE+RA*8]
  4022. |.else
  4023. | fild dword [BASE+RA*8]
  4024. |.endif
  4025. | jmp >2
  4026. |
  4027. |8: // RD is an integer, RA is not an integer.
  4028. | ja >5
  4029. | // RD is an integer, RA is a number.
  4030. |.if SSE
  4031. | cvtsi2sd xmm0, dword [BASE+RD*8]
  4032. | ucomisd xmm0, qword [BASE+RA*8]
  4033. |.else
  4034. | fild dword [BASE+RD*8]
  4035. | fld qword [BASE+RA*8]
  4036. |.endif
  4037. | jmp >4
  4038. |
  4039. |.else
  4040. | cmp RB, LJ_TISNUM; jae >5
  4041. | checknum RA, >5
  4042. |.endif
  4043. |.if SSE
  4044. |1:
  4045. | movsd xmm0, qword [BASE+RA*8]
  4046. |2:
  4047. | ucomisd xmm0, qword [BASE+RD*8]
  4048. |4:
  4049. |.else
  4050. |1:
  4051. | fld qword [BASE+RA*8]
  4052. |2:
  4053. | fld qword [BASE+RD*8]
  4054. |4:
  4055. | fcomparepp
  4056. |.endif
  4057. iseqne_fp:
  4058. if (vk) {
  4059. | jp >2 // Unordered means not equal.
  4060. | jne >2
  4061. } else {
  4062. | jp >2 // Unordered means not equal.
  4063. | je >1
  4064. }
  4065. iseqne_end:
  4066. if (vk) {
  4067. |1: // EQ: Branch to the target.
  4068. | movzx RD, PC_RD
  4069. | branchPC RD
  4070. |2: // NE: Fallthrough to next instruction.
  4071. |.if not FFI
  4072. |3:
  4073. |.endif
  4074. } else {
  4075. |.if not FFI
  4076. |3:
  4077. |.endif
  4078. |2: // NE: Branch to the target.
  4079. | movzx RD, PC_RD
  4080. | branchPC RD
  4081. |1: // EQ: Fallthrough to next instruction.
  4082. }
  4083. if (LJ_DUALNUM && (op == BC_ISEQV || op == BC_ISNEV ||
  4084. op == BC_ISEQN || op == BC_ISNEN)) {
  4085. | jmp <9
  4086. } else {
  4087. | ins_next
  4088. }
  4089. |
  4090. if (op == BC_ISEQV || op == BC_ISNEV) {
  4091. |5: // Either or both types are not numbers.
  4092. |.if FFI
  4093. | cmp RB, LJ_TCDATA; je ->vmeta_equal_cd
  4094. | checktp RA, LJ_TCDATA; je ->vmeta_equal_cd
  4095. |.endif
  4096. | checktp RA, RB // Compare types.
  4097. | jne <2 // Not the same type?
  4098. | cmp RB, LJ_TISPRI
  4099. | jae <1 // Same type and primitive type?
  4100. |
  4101. | // Same types and not a primitive type. Compare GCobj or pvalue.
  4102. | mov RA, [BASE+RA*8]
  4103. | mov RD, [BASE+RD*8]
  4104. | cmp RA, RD
  4105. | je <1 // Same GCobjs or pvalues?
  4106. | cmp RB, LJ_TISTABUD
  4107. | ja <2 // Different objects and not table/ud?
  4108. |.if X64
  4109. | cmp RB, LJ_TUDATA // And not 64 bit lightuserdata.
  4110. | jb <2
  4111. |.endif
  4112. |
  4113. | // Different tables or userdatas. Need to check __eq metamethod.
  4114. | // Field metatable must be at same offset for GCtab and GCudata!
  4115. | mov TAB:RB, TAB:RA->metatable
  4116. | test TAB:RB, TAB:RB
  4117. | jz <2 // No metatable?
  4118. | test byte TAB:RB->nomm, 1<<MM_eq
  4119. | jnz <2 // Or 'no __eq' flag set?
  4120. if (vk) {
  4121. | xor RB, RB // ne = 0
  4122. } else {
  4123. | mov RB, 1 // ne = 1
  4124. }
  4125. | jmp ->vmeta_equal // Handle __eq metamethod.
  4126. } else {
  4127. |.if FFI
  4128. |3:
  4129. | cmp RB, LJ_TCDATA
  4130. if (LJ_DUALNUM && vk) {
  4131. | jne <9
  4132. } else {
  4133. | jne <2
  4134. }
  4135. | jmp ->vmeta_equal_cd
  4136. |.endif
  4137. }
  4138. break;
  4139. case BC_ISEQS: case BC_ISNES:
  4140. vk = op == BC_ISEQS;
  4141. | ins_AND // RA = src, RD = str const, JMP with RD = target
  4142. | mov RB, [BASE+RA*8+4]
  4143. | add PC, 4
  4144. | cmp RB, LJ_TSTR; jne >3
  4145. | mov RA, [BASE+RA*8]
  4146. | cmp RA, [KBASE+RD*4]
  4147. iseqne_test:
  4148. if (vk) {
  4149. | jne >2
  4150. } else {
  4151. | je >1
  4152. }
  4153. goto iseqne_end;
  4154. case BC_ISEQN: case BC_ISNEN:
  4155. vk = op == BC_ISEQN;
  4156. | ins_AD // RA = src, RD = num const, JMP with RD = target
  4157. | mov RB, [BASE+RA*8+4]
  4158. | add PC, 4
  4159. |.if DUALNUM
  4160. | cmp RB, LJ_TISNUM; jne >7
  4161. | cmp dword [KBASE+RD*8+4], LJ_TISNUM; jne >8
  4162. | mov RB, dword [KBASE+RD*8]
  4163. | cmp RB, dword [BASE+RA*8]
  4164. if (vk) {
  4165. | jne >9
  4166. } else {
  4167. | je >9
  4168. }
  4169. | movzx RD, PC_RD
  4170. | branchPC RD
  4171. |9:
  4172. | ins_next
  4173. |
  4174. |7: // RA is not an integer.
  4175. | ja >3
  4176. | // RA is a number.
  4177. | cmp dword [KBASE+RD*8+4], LJ_TISNUM; jb >1
  4178. | // RA is a number, RD is an integer.
  4179. |.if SSE
  4180. | cvtsi2sd xmm0, dword [KBASE+RD*8]
  4181. |.else
  4182. | fild dword [KBASE+RD*8]
  4183. |.endif
  4184. | jmp >2
  4185. |
  4186. |8: // RA is an integer, RD is a number.
  4187. |.if SSE
  4188. | cvtsi2sd xmm0, dword [BASE+RA*8]
  4189. | ucomisd xmm0, qword [KBASE+RD*8]
  4190. |.else
  4191. | fild dword [BASE+RA*8]
  4192. | fld qword [KBASE+RD*8]
  4193. |.endif
  4194. | jmp >4
  4195. |.else
  4196. | cmp RB, LJ_TISNUM; jae >3
  4197. |.endif
  4198. |.if SSE
  4199. |1:
  4200. | movsd xmm0, qword [KBASE+RD*8]
  4201. |2:
  4202. | ucomisd xmm0, qword [BASE+RA*8]
  4203. |4:
  4204. |.else
  4205. |1:
  4206. | fld qword [KBASE+RD*8]
  4207. |2:
  4208. | fld qword [BASE+RA*8]
  4209. |4:
  4210. | fcomparepp
  4211. |.endif
  4212. goto iseqne_fp;
  4213. case BC_ISEQP: case BC_ISNEP:
  4214. vk = op == BC_ISEQP;
  4215. | ins_AND // RA = src, RD = primitive type (~), JMP with RD = target
  4216. | mov RB, [BASE+RA*8+4]
  4217. | add PC, 4
  4218. | cmp RB, RD
  4219. if (!LJ_HASFFI) goto iseqne_test;
  4220. if (vk) {
  4221. | jne >3
  4222. | movzx RD, PC_RD
  4223. | branchPC RD
  4224. |2:
  4225. | ins_next
  4226. |3:
  4227. | cmp RB, LJ_TCDATA; jne <2
  4228. | jmp ->vmeta_equal_cd
  4229. } else {
  4230. | je >2
  4231. | cmp RB, LJ_TCDATA; je ->vmeta_equal_cd
  4232. | movzx RD, PC_RD
  4233. | branchPC RD
  4234. |2:
  4235. | ins_next
  4236. }
  4237. break;
  4238. /* -- Unary test and copy ops ------------------------------------------- */
  4239. case BC_ISTC: case BC_ISFC: case BC_IST: case BC_ISF:
  4240. | ins_AD // RA = dst or unused, RD = src, JMP with RD = target
  4241. | mov RB, [BASE+RD*8+4]
  4242. | add PC, 4
  4243. | cmp RB, LJ_TISTRUECOND
  4244. if (op == BC_IST || op == BC_ISTC) {
  4245. | jae >1
  4246. } else {
  4247. | jb >1
  4248. }
  4249. if (op == BC_ISTC || op == BC_ISFC) {
  4250. | mov [BASE+RA*8+4], RB
  4251. | mov RB, [BASE+RD*8]
  4252. | mov [BASE+RA*8], RB
  4253. }
  4254. | movzx RD, PC_RD
  4255. | branchPC RD
  4256. |1: // Fallthrough to the next instruction.
  4257. | ins_next
  4258. break;
  4259. /* -- Unary ops --------------------------------------------------------- */
  4260. case BC_MOV:
  4261. | ins_AD // RA = dst, RD = src
  4262. |.if X64
  4263. | mov RBa, [BASE+RD*8]
  4264. | mov [BASE+RA*8], RBa
  4265. |.else
  4266. | mov RB, [BASE+RD*8+4]
  4267. | mov RD, [BASE+RD*8]
  4268. | mov [BASE+RA*8+4], RB
  4269. | mov [BASE+RA*8], RD
  4270. |.endif
  4271. | ins_next_
  4272. break;
  4273. case BC_NOT:
  4274. | ins_AD // RA = dst, RD = src
  4275. | xor RB, RB
  4276. | checktp RD, LJ_TISTRUECOND
  4277. | adc RB, LJ_TTRUE
  4278. | mov [BASE+RA*8+4], RB
  4279. | ins_next
  4280. break;
  4281. case BC_UNM:
  4282. | ins_AD // RA = dst, RD = src
  4283. |.if DUALNUM
  4284. | checkint RD, >5
  4285. | mov RB, [BASE+RD*8]
  4286. | neg RB
  4287. | jo >4
  4288. | mov dword [BASE+RA*8+4], LJ_TISNUM
  4289. | mov dword [BASE+RA*8], RB
  4290. |9:
  4291. | ins_next
  4292. |4:
  4293. | mov dword [BASE+RA*8+4], 0x41e00000 // 2^31.
  4294. | mov dword [BASE+RA*8], 0
  4295. | jmp <9
  4296. |5:
  4297. | ja ->vmeta_unm
  4298. |.else
  4299. | checknum RD, ->vmeta_unm
  4300. |.endif
  4301. |.if SSE
  4302. | movsd xmm0, qword [BASE+RD*8]
  4303. | sseconst_sign xmm1, RDa
  4304. | xorps xmm0, xmm1
  4305. | movsd qword [BASE+RA*8], xmm0
  4306. |.else
  4307. | fld qword [BASE+RD*8]
  4308. | fchs
  4309. | fstp qword [BASE+RA*8]
  4310. |.endif
  4311. |.if DUALNUM
  4312. | jmp <9
  4313. |.else
  4314. | ins_next
  4315. |.endif
  4316. break;
  4317. case BC_LEN:
  4318. | ins_AD // RA = dst, RD = src
  4319. | checkstr RD, >2
  4320. | mov STR:RD, [BASE+RD*8]
  4321. |.if DUALNUM
  4322. | mov RD, dword STR:RD->len
  4323. |1:
  4324. | mov dword [BASE+RA*8+4], LJ_TISNUM
  4325. | mov dword [BASE+RA*8], RD
  4326. |.elif SSE
  4327. | xorps xmm0, xmm0
  4328. | cvtsi2sd xmm0, dword STR:RD->len
  4329. |1:
  4330. | movsd qword [BASE+RA*8], xmm0
  4331. |.else
  4332. | fild dword STR:RD->len
  4333. |1:
  4334. | fstp qword [BASE+RA*8]
  4335. |.endif
  4336. | ins_next
  4337. |2:
  4338. | checktab RD, ->vmeta_len
  4339. | mov TAB:FCARG1, [BASE+RD*8]
  4340. #if LJ_52
  4341. | mov TAB:RB, TAB:FCARG1->metatable
  4342. | cmp TAB:RB, 0
  4343. | jnz >9
  4344. |3:
  4345. #endif
  4346. |->BC_LEN_Z:
  4347. | mov RB, BASE // Save BASE.
  4348. | call extern lj_tab_len@4 // (GCtab *t)
  4349. | // Length of table returned in eax (RD).
  4350. |.if DUALNUM
  4351. | // Nothing to do.
  4352. |.elif SSE
  4353. | cvtsi2sd xmm0, RD
  4354. |.else
  4355. | mov ARG1, RD
  4356. | fild ARG1
  4357. |.endif
  4358. | mov BASE, RB // Restore BASE.
  4359. | movzx RA, PC_RA
  4360. | jmp <1
  4361. #if LJ_52
  4362. |9: // Check for __len.
  4363. | test byte TAB:RB->nomm, 1<<MM_len
  4364. | jnz <3
  4365. | jmp ->vmeta_len // 'no __len' flag NOT set: check.
  4366. #endif
  4367. break;
  4368. /* -- Binary ops -------------------------------------------------------- */
  4369. |.macro ins_arithpre, x87ins, sseins, ssereg
  4370. | ins_ABC
  4371. ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
  4372. ||switch (vk) {
  4373. ||case 0:
  4374. | checknum RB, ->vmeta_arith_vn
  4375. | .if DUALNUM
  4376. | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jae ->vmeta_arith_vn
  4377. | .endif
  4378. | .if SSE
  4379. | movsd xmm0, qword [BASE+RB*8]
  4380. | sseins ssereg, qword [KBASE+RC*8]
  4381. | .else
  4382. | fld qword [BASE+RB*8]
  4383. | x87ins qword [KBASE+RC*8]
  4384. | .endif
  4385. || break;
  4386. ||case 1:
  4387. | checknum RB, ->vmeta_arith_nv
  4388. | .if DUALNUM
  4389. | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jae ->vmeta_arith_nv
  4390. | .endif
  4391. | .if SSE
  4392. | movsd xmm0, qword [KBASE+RC*8]
  4393. | sseins ssereg, qword [BASE+RB*8]
  4394. | .else
  4395. | fld qword [KBASE+RC*8]
  4396. | x87ins qword [BASE+RB*8]
  4397. | .endif
  4398. || break;
  4399. ||default:
  4400. | checknum RB, ->vmeta_arith_vv
  4401. | checknum RC, ->vmeta_arith_vv
  4402. | .if SSE
  4403. | movsd xmm0, qword [BASE+RB*8]
  4404. | sseins ssereg, qword [BASE+RC*8]
  4405. | .else
  4406. | fld qword [BASE+RB*8]
  4407. | x87ins qword [BASE+RC*8]
  4408. | .endif
  4409. || break;
  4410. ||}
  4411. |.endmacro
  4412. |
  4413. |.macro ins_arithdn, intins
  4414. | ins_ABC
  4415. ||vk = ((int)op - BC_ADDVN) / (BC_ADDNV-BC_ADDVN);
  4416. ||switch (vk) {
  4417. ||case 0:
  4418. | checkint RB, ->vmeta_arith_vn
  4419. | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jne ->vmeta_arith_vn
  4420. | mov RB, [BASE+RB*8]
  4421. | intins RB, [KBASE+RC*8]; jo ->vmeta_arith_vno
  4422. || break;
  4423. ||case 1:
  4424. | checkint RB, ->vmeta_arith_nv
  4425. | cmp dword [KBASE+RC*8+4], LJ_TISNUM; jne ->vmeta_arith_nv
  4426. | mov RC, [KBASE+RC*8]
  4427. | intins RC, [BASE+RB*8]; jo ->vmeta_arith_nvo
  4428. || break;
  4429. ||default:
  4430. | checkint RB, ->vmeta_arith_vv
  4431. | checkint RC, ->vmeta_arith_vv
  4432. | mov RB, [BASE+RB*8]
  4433. | intins RB, [BASE+RC*8]; jo ->vmeta_arith_vvo
  4434. || break;
  4435. ||}
  4436. | mov dword [BASE+RA*8+4], LJ_TISNUM
  4437. ||if (vk == 1) {
  4438. | mov dword [BASE+RA*8], RC
  4439. ||} else {
  4440. | mov dword [BASE+RA*8], RB
  4441. ||}
  4442. | ins_next
  4443. |.endmacro
  4444. |
  4445. |.macro ins_arithpost
  4446. |.if SSE
  4447. | movsd qword [BASE+RA*8], xmm0
  4448. |.else
  4449. | fstp qword [BASE+RA*8]
  4450. |.endif
  4451. |.endmacro
  4452. |
  4453. |.macro ins_arith, x87ins, sseins
  4454. | ins_arithpre x87ins, sseins, xmm0
  4455. | ins_arithpost
  4456. | ins_next
  4457. |.endmacro
  4458. |
  4459. |.macro ins_arith, intins, x87ins, sseins
  4460. |.if DUALNUM
  4461. | ins_arithdn intins
  4462. |.else
  4463. | ins_arith, x87ins, sseins
  4464. |.endif
  4465. |.endmacro
  4466. | // RA = dst, RB = src1 or num const, RC = src2 or num const
  4467. case BC_ADDVN: case BC_ADDNV: case BC_ADDVV:
  4468. | ins_arith add, fadd, addsd
  4469. break;
  4470. case BC_SUBVN: case BC_SUBNV: case BC_SUBVV:
  4471. | ins_arith sub, fsub, subsd
  4472. break;
  4473. case BC_MULVN: case BC_MULNV: case BC_MULVV:
  4474. | ins_arith imul, fmul, mulsd
  4475. break;
  4476. case BC_DIVVN: case BC_DIVNV: case BC_DIVVV:
  4477. | ins_arith fdiv, divsd
  4478. break;
  4479. case BC_MODVN:
  4480. | ins_arithpre fld, movsd, xmm1
  4481. |->BC_MODVN_Z:
  4482. | call ->vm_mod
  4483. | ins_arithpost
  4484. | ins_next
  4485. break;
  4486. case BC_MODNV: case BC_MODVV:
  4487. | ins_arithpre fld, movsd, xmm1
  4488. | jmp ->BC_MODVN_Z // Avoid 3 copies. It's slow anyway.
  4489. break;
  4490. case BC_POW:
  4491. | ins_arithpre fld, movsd, xmm1
  4492. | call ->vm_pow
  4493. | ins_arithpost
  4494. | ins_next
  4495. break;
  4496. case BC_CAT:
  4497. | ins_ABC // RA = dst, RB = src_start, RC = src_end
  4498. |.if X64
  4499. | mov L:CARG1d, SAVE_L
  4500. | mov L:CARG1d->base, BASE
  4501. | lea CARG2d, [BASE+RC*8]
  4502. | mov CARG3d, RC
  4503. | sub CARG3d, RB
  4504. |->BC_CAT_Z:
  4505. | mov L:RB, L:CARG1d
  4506. |.else
  4507. | lea RA, [BASE+RC*8]
  4508. | sub RC, RB
  4509. | mov ARG2, RA
  4510. | mov ARG3, RC
  4511. |->BC_CAT_Z:
  4512. | mov L:RB, SAVE_L
  4513. | mov ARG1, L:RB
  4514. | mov L:RB->base, BASE
  4515. |.endif
  4516. | mov SAVE_PC, PC
  4517. | call extern lj_meta_cat // (lua_State *L, TValue *top, int left)
  4518. | // NULL (finished) or TValue * (metamethod) returned in eax (RC).
  4519. | mov BASE, L:RB->base
  4520. | test RC, RC
  4521. | jnz ->vmeta_binop
  4522. | movzx RB, PC_RB // Copy result to Stk[RA] from Stk[RB].
  4523. | movzx RA, PC_RA
  4524. |.if X64
  4525. | mov RCa, [BASE+RB*8]
  4526. | mov [BASE+RA*8], RCa
  4527. |.else
  4528. | mov RC, [BASE+RB*8+4]
  4529. | mov RB, [BASE+RB*8]
  4530. | mov [BASE+RA*8+4], RC
  4531. | mov [BASE+RA*8], RB
  4532. |.endif
  4533. | ins_next
  4534. break;
  4535. /* -- Constant ops ------------------------------------------------------ */
  4536. case BC_KSTR:
  4537. | ins_AND // RA = dst, RD = str const (~)
  4538. | mov RD, [KBASE+RD*4]
  4539. | mov dword [BASE+RA*8+4], LJ_TSTR
  4540. | mov [BASE+RA*8], RD
  4541. | ins_next
  4542. break;
  4543. case BC_KCDATA:
  4544. |.if FFI
  4545. | ins_AND // RA = dst, RD = cdata const (~)
  4546. | mov RD, [KBASE+RD*4]
  4547. | mov dword [BASE+RA*8+4], LJ_TCDATA
  4548. | mov [BASE+RA*8], RD
  4549. | ins_next
  4550. |.endif
  4551. break;
  4552. case BC_KSHORT:
  4553. | ins_AD // RA = dst, RD = signed int16 literal
  4554. |.if DUALNUM
  4555. | movsx RD, RDW
  4556. | mov dword [BASE+RA*8+4], LJ_TISNUM
  4557. | mov dword [BASE+RA*8], RD
  4558. |.elif SSE
  4559. | movsx RD, RDW // Sign-extend literal.
  4560. | cvtsi2sd xmm0, RD
  4561. | movsd qword [BASE+RA*8], xmm0
  4562. |.else
  4563. | fild PC_RD // Refetch signed RD from instruction.
  4564. | fstp qword [BASE+RA*8]
  4565. |.endif
  4566. | ins_next
  4567. break;
  4568. case BC_KNUM:
  4569. | ins_AD // RA = dst, RD = num const
  4570. |.if SSE
  4571. | movsd xmm0, qword [KBASE+RD*8]
  4572. | movsd qword [BASE+RA*8], xmm0
  4573. |.else
  4574. | fld qword [KBASE+RD*8]
  4575. | fstp qword [BASE+RA*8]
  4576. |.endif
  4577. | ins_next
  4578. break;
  4579. case BC_KPRI:
  4580. | ins_AND // RA = dst, RD = primitive type (~)
  4581. | mov [BASE+RA*8+4], RD
  4582. | ins_next
  4583. break;
  4584. case BC_KNIL:
  4585. | ins_AD // RA = dst_start, RD = dst_end
  4586. | lea RA, [BASE+RA*8+12]
  4587. | lea RD, [BASE+RD*8+4]
  4588. | mov RB, LJ_TNIL
  4589. | mov [RA-8], RB // Sets minimum 2 slots.
  4590. |1:
  4591. | mov [RA], RB
  4592. | add RA, 8
  4593. | cmp RA, RD
  4594. | jbe <1
  4595. | ins_next
  4596. break;
  4597. /* -- Upvalue and function ops ------------------------------------------ */
  4598. case BC_UGET:
  4599. | ins_AD // RA = dst, RD = upvalue #
  4600. | mov LFUNC:RB, [BASE-8]
  4601. | mov UPVAL:RB, [LFUNC:RB+RD*4+offsetof(GCfuncL, uvptr)]
  4602. | mov RB, UPVAL:RB->v
  4603. |.if X64
  4604. | mov RDa, [RB]
  4605. | mov [BASE+RA*8], RDa
  4606. |.else
  4607. | mov RD, [RB+4]
  4608. | mov RB, [RB]
  4609. | mov [BASE+RA*8+4], RD
  4610. | mov [BASE+RA*8], RB
  4611. |.endif
  4612. | ins_next
  4613. break;
  4614. case BC_USETV:
  4615. #define TV2MARKOFS \
  4616. ((int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv))
  4617. | ins_AD // RA = upvalue #, RD = src
  4618. | mov LFUNC:RB, [BASE-8]
  4619. | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
  4620. | cmp byte UPVAL:RB->closed, 0
  4621. | mov RB, UPVAL:RB->v
  4622. | mov RA, [BASE+RD*8]
  4623. | mov RD, [BASE+RD*8+4]
  4624. | mov [RB], RA
  4625. | mov [RB+4], RD
  4626. | jz >1
  4627. | // Check barrier for closed upvalue.
  4628. | test byte [RB+TV2MARKOFS], LJ_GC_BLACK // isblack(uv)
  4629. | jnz >2
  4630. |1:
  4631. | ins_next
  4632. |
  4633. |2: // Upvalue is black. Check if new value is collectable and white.
  4634. | sub RD, LJ_TISGCV
  4635. | cmp RD, LJ_TNUMX - LJ_TISGCV // tvisgcv(v)
  4636. | jbe <1
  4637. | test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES // iswhite(v)
  4638. | jz <1
  4639. | // Crossed a write barrier. Move the barrier forward.
  4640. |.if X64 and not X64WIN
  4641. | mov FCARG2, RB
  4642. | mov RB, BASE // Save BASE.
  4643. |.else
  4644. | xchg FCARG2, RB // Save BASE (FCARG2 == BASE).
  4645. |.endif
  4646. | lea GL:FCARG1, [DISPATCH+GG_DISP2G]
  4647. | call extern lj_gc_barrieruv@8 // (global_State *g, TValue *tv)
  4648. | mov BASE, RB // Restore BASE.
  4649. | jmp <1
  4650. break;
  4651. #undef TV2MARKOFS
  4652. case BC_USETS:
  4653. | ins_AND // RA = upvalue #, RD = str const (~)
  4654. | mov LFUNC:RB, [BASE-8]
  4655. | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
  4656. | mov GCOBJ:RA, [KBASE+RD*4]
  4657. | mov RD, UPVAL:RB->v
  4658. | mov [RD], GCOBJ:RA
  4659. | mov dword [RD+4], LJ_TSTR
  4660. | test byte UPVAL:RB->marked, LJ_GC_BLACK // isblack(uv)
  4661. | jnz >2
  4662. |1:
  4663. | ins_next
  4664. |
  4665. |2: // Check if string is white and ensure upvalue is closed.
  4666. | test byte GCOBJ:RA->gch.marked, LJ_GC_WHITES // iswhite(str)
  4667. | jz <1
  4668. | cmp byte UPVAL:RB->closed, 0
  4669. | jz <1
  4670. | // Crossed a write barrier. Move the barrier forward.
  4671. | mov RB, BASE // Save BASE (FCARG2 == BASE).
  4672. | mov FCARG2, RD
  4673. | lea GL:FCARG1, [DISPATCH+GG_DISP2G]
  4674. | call extern lj_gc_barrieruv@8 // (global_State *g, TValue *tv)
  4675. | mov BASE, RB // Restore BASE.
  4676. | jmp <1
  4677. break;
  4678. case BC_USETN:
  4679. | ins_AD // RA = upvalue #, RD = num const
  4680. | mov LFUNC:RB, [BASE-8]
  4681. |.if SSE
  4682. | movsd xmm0, qword [KBASE+RD*8]
  4683. |.else
  4684. | fld qword [KBASE+RD*8]
  4685. |.endif
  4686. | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
  4687. | mov RA, UPVAL:RB->v
  4688. |.if SSE
  4689. | movsd qword [RA], xmm0
  4690. |.else
  4691. | fstp qword [RA]
  4692. |.endif
  4693. | ins_next
  4694. break;
  4695. case BC_USETP:
  4696. | ins_AND // RA = upvalue #, RD = primitive type (~)
  4697. | mov LFUNC:RB, [BASE-8]
  4698. | mov UPVAL:RB, [LFUNC:RB+RA*4+offsetof(GCfuncL, uvptr)]
  4699. | mov RA, UPVAL:RB->v
  4700. | mov [RA+4], RD
  4701. | ins_next
  4702. break;
  4703. case BC_UCLO:
  4704. | ins_AD // RA = level, RD = target
  4705. | branchPC RD // Do this first to free RD.
  4706. | mov L:RB, SAVE_L
  4707. | cmp dword L:RB->openupval, 0
  4708. | je >1
  4709. | mov L:RB->base, BASE
  4710. | lea FCARG2, [BASE+RA*8] // Caveat: FCARG2 == BASE
  4711. | mov L:FCARG1, L:RB // Caveat: FCARG1 == RA
  4712. | call extern lj_func_closeuv@8 // (lua_State *L, TValue *level)
  4713. | mov BASE, L:RB->base
  4714. |1:
  4715. | ins_next
  4716. break;
  4717. case BC_FNEW:
  4718. | ins_AND // RA = dst, RD = proto const (~) (holding function prototype)
  4719. |.if X64
  4720. | mov L:RB, SAVE_L
  4721. | mov L:RB->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
  4722. | mov CARG3d, [BASE-8]
  4723. | mov CARG2d, [KBASE+RD*4] // Fetch GCproto *.
  4724. | mov CARG1d, L:RB
  4725. |.else
  4726. | mov LFUNC:RA, [BASE-8]
  4727. | mov PROTO:RD, [KBASE+RD*4] // Fetch GCproto *.
  4728. | mov L:RB, SAVE_L
  4729. | mov ARG3, LFUNC:RA
  4730. | mov ARG2, PROTO:RD
  4731. | mov ARG1, L:RB
  4732. | mov L:RB->base, BASE
  4733. |.endif
  4734. | mov SAVE_PC, PC
  4735. | // (lua_State *L, GCproto *pt, GCfuncL *parent)
  4736. | call extern lj_func_newL_gc
  4737. | // GCfuncL * returned in eax (RC).
  4738. | mov BASE, L:RB->base
  4739. | movzx RA, PC_RA
  4740. | mov [BASE+RA*8], LFUNC:RC
  4741. | mov dword [BASE+RA*8+4], LJ_TFUNC
  4742. | ins_next
  4743. break;
  4744. /* -- Table ops --------------------------------------------------------- */
  4745. case BC_TNEW:
  4746. | ins_AD // RA = dst, RD = hbits|asize
  4747. | mov L:RB, SAVE_L
  4748. | mov L:RB->base, BASE
  4749. | mov RA, [DISPATCH+DISPATCH_GL(gc.total)]
  4750. | cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)]
  4751. | mov SAVE_PC, PC
  4752. | jae >5
  4753. |1:
  4754. |.if X64
  4755. | mov CARG3d, RD
  4756. | and RD, 0x7ff
  4757. | shr CARG3d, 11
  4758. |.else
  4759. | mov RA, RD
  4760. | and RD, 0x7ff
  4761. | shr RA, 11
  4762. | mov ARG3, RA
  4763. |.endif
  4764. | cmp RD, 0x7ff
  4765. | je >3
  4766. |2:
  4767. |.if X64
  4768. | mov L:CARG1d, L:RB
  4769. | mov CARG2d, RD
  4770. |.else
  4771. | mov ARG1, L:RB
  4772. | mov ARG2, RD
  4773. |.endif
  4774. | call extern lj_tab_new // (lua_State *L, int32_t asize, uint32_t hbits)
  4775. | // Table * returned in eax (RC).
  4776. | mov BASE, L:RB->base
  4777. | movzx RA, PC_RA
  4778. | mov [BASE+RA*8], TAB:RC
  4779. | mov dword [BASE+RA*8+4], LJ_TTAB
  4780. | ins_next
  4781. |3: // Turn 0x7ff into 0x801.
  4782. | mov RD, 0x801
  4783. | jmp <2
  4784. |5:
  4785. | mov L:FCARG1, L:RB
  4786. | call extern lj_gc_step_fixtop@4 // (lua_State *L)
  4787. | movzx RD, PC_RD
  4788. | jmp <1
  4789. break;
  4790. case BC_TDUP:
  4791. | ins_AND // RA = dst, RD = table const (~) (holding template table)
  4792. | mov L:RB, SAVE_L
  4793. | mov RA, [DISPATCH+DISPATCH_GL(gc.total)]
  4794. | mov SAVE_PC, PC
  4795. | cmp RA, [DISPATCH+DISPATCH_GL(gc.threshold)]
  4796. | mov L:RB->base, BASE
  4797. | jae >3
  4798. |2:
  4799. | mov TAB:FCARG2, [KBASE+RD*4] // Caveat: FCARG2 == BASE
  4800. | mov L:FCARG1, L:RB // Caveat: FCARG1 == RA
  4801. | call extern lj_tab_dup@8 // (lua_State *L, Table *kt)
  4802. | // Table * returned in eax (RC).
  4803. | mov BASE, L:RB->base
  4804. | movzx RA, PC_RA
  4805. | mov [BASE+RA*8], TAB:RC
  4806. | mov dword [BASE+RA*8+4], LJ_TTAB
  4807. | ins_next
  4808. |3:
  4809. | mov L:FCARG1, L:RB
  4810. | call extern lj_gc_step_fixtop@4 // (lua_State *L)
  4811. | movzx RD, PC_RD // Need to reload RD.
  4812. | not RDa
  4813. | jmp <2
  4814. break;
  4815. case BC_GGET:
  4816. | ins_AND // RA = dst, RD = str const (~)
  4817. | mov LFUNC:RB, [BASE-8]
  4818. | mov TAB:RB, LFUNC:RB->env
  4819. | mov STR:RC, [KBASE+RD*4]
  4820. | jmp ->BC_TGETS_Z
  4821. break;
  4822. case BC_GSET:
  4823. | ins_AND // RA = src, RD = str const (~)
  4824. | mov LFUNC:RB, [BASE-8]
  4825. | mov TAB:RB, LFUNC:RB->env
  4826. | mov STR:RC, [KBASE+RD*4]
  4827. | jmp ->BC_TSETS_Z
  4828. break;
  4829. case BC_TGETV:
  4830. | ins_ABC // RA = dst, RB = table, RC = key
  4831. | checktab RB, ->vmeta_tgetv
  4832. | mov TAB:RB, [BASE+RB*8]
  4833. |
  4834. | // Integer key?
  4835. |.if DUALNUM
  4836. | checkint RC, >5
  4837. | mov RC, dword [BASE+RC*8]
  4838. |.else
  4839. | // Convert number to int and back and compare.
  4840. | checknum RC, >5
  4841. |.if SSE
  4842. | movsd xmm0, qword [BASE+RC*8]
  4843. | cvtsd2si RC, xmm0
  4844. | cvtsi2sd xmm1, RC
  4845. | ucomisd xmm0, xmm1
  4846. |.else
  4847. | fld qword [BASE+RC*8]
  4848. | fist ARG1
  4849. | fild ARG1
  4850. | fcomparepp
  4851. | mov RC, ARG1
  4852. |.endif
  4853. | jne ->vmeta_tgetv // Generic numeric key? Use fallback.
  4854. |.endif
  4855. | cmp RC, TAB:RB->asize // Takes care of unordered, too.
  4856. | jae ->vmeta_tgetv // Not in array part? Use fallback.
  4857. | shl RC, 3
  4858. | add RC, TAB:RB->array
  4859. | cmp dword [RC+4], LJ_TNIL // Avoid overwriting RB in fastpath.
  4860. | je >2
  4861. | // Get array slot.
  4862. |.if X64
  4863. | mov RBa, [RC]
  4864. | mov [BASE+RA*8], RBa
  4865. |.else
  4866. | mov RB, [RC]
  4867. | mov RC, [RC+4]
  4868. | mov [BASE+RA*8], RB
  4869. | mov [BASE+RA*8+4], RC
  4870. |.endif
  4871. |1:
  4872. | ins_next
  4873. |
  4874. |2: // Check for __index if table value is nil.
  4875. | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
  4876. | jz >3
  4877. | mov TAB:RA, TAB:RB->metatable
  4878. | test byte TAB:RA->nomm, 1<<MM_index
  4879. | jz ->vmeta_tgetv // 'no __index' flag NOT set: check.
  4880. | movzx RA, PC_RA // Restore RA.
  4881. |3:
  4882. | mov dword [BASE+RA*8+4], LJ_TNIL
  4883. | jmp <1
  4884. |
  4885. |5: // String key?
  4886. | checkstr RC, ->vmeta_tgetv
  4887. | mov STR:RC, [BASE+RC*8]
  4888. | jmp ->BC_TGETS_Z
  4889. break;
  4890. case BC_TGETS:
  4891. | ins_ABC // RA = dst, RB = table, RC = str const (~)
  4892. | not RCa
  4893. | mov STR:RC, [KBASE+RC*4]
  4894. | checktab RB, ->vmeta_tgets
  4895. | mov TAB:RB, [BASE+RB*8]
  4896. |->BC_TGETS_Z: // RB = GCtab *, RC = GCstr *, refetches PC_RA.
  4897. | mov RA, TAB:RB->hmask
  4898. | and RA, STR:RC->hash
  4899. | imul RA, #NODE
  4900. | add NODE:RA, TAB:RB->node
  4901. |1:
  4902. | cmp dword NODE:RA->key.it, LJ_TSTR
  4903. | jne >4
  4904. | cmp dword NODE:RA->key.gcr, STR:RC
  4905. | jne >4
  4906. | // Ok, key found. Assumes: offsetof(Node, val) == 0
  4907. | cmp dword [RA+4], LJ_TNIL // Avoid overwriting RB in fastpath.
  4908. | je >5 // Key found, but nil value?
  4909. | movzx RC, PC_RA
  4910. | // Get node value.
  4911. |.if X64
  4912. | mov RBa, [RA]
  4913. | mov [BASE+RC*8], RBa
  4914. |.else
  4915. | mov RB, [RA]
  4916. | mov RA, [RA+4]
  4917. | mov [BASE+RC*8], RB
  4918. | mov [BASE+RC*8+4], RA
  4919. |.endif
  4920. |2:
  4921. | ins_next
  4922. |
  4923. |3:
  4924. | movzx RC, PC_RA
  4925. | mov dword [BASE+RC*8+4], LJ_TNIL
  4926. | jmp <2
  4927. |
  4928. |4: // Follow hash chain.
  4929. | mov NODE:RA, NODE:RA->next
  4930. | test NODE:RA, NODE:RA
  4931. | jnz <1
  4932. | // End of hash chain: key not found, nil result.
  4933. |
  4934. |5: // Check for __index if table value is nil.
  4935. | mov TAB:RA, TAB:RB->metatable
  4936. | test TAB:RA, TAB:RA
  4937. | jz <3 // No metatable: done.
  4938. | test byte TAB:RA->nomm, 1<<MM_index
  4939. | jnz <3 // 'no __index' flag set: done.
  4940. | jmp ->vmeta_tgets // Caveat: preserve STR:RC.
  4941. break;
  4942. case BC_TGETB:
  4943. | ins_ABC // RA = dst, RB = table, RC = byte literal
  4944. | checktab RB, ->vmeta_tgetb
  4945. | mov TAB:RB, [BASE+RB*8]
  4946. | cmp RC, TAB:RB->asize
  4947. | jae ->vmeta_tgetb
  4948. | shl RC, 3
  4949. | add RC, TAB:RB->array
  4950. | cmp dword [RC+4], LJ_TNIL // Avoid overwriting RB in fastpath.
  4951. | je >2
  4952. | // Get array slot.
  4953. |.if X64
  4954. | mov RBa, [RC]
  4955. | mov [BASE+RA*8], RBa
  4956. |.else
  4957. | mov RB, [RC]
  4958. | mov RC, [RC+4]
  4959. | mov [BASE+RA*8], RB
  4960. | mov [BASE+RA*8+4], RC
  4961. |.endif
  4962. |1:
  4963. | ins_next
  4964. |
  4965. |2: // Check for __index if table value is nil.
  4966. | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
  4967. | jz >3
  4968. | mov TAB:RA, TAB:RB->metatable
  4969. | test byte TAB:RA->nomm, 1<<MM_index
  4970. | jz ->vmeta_tgetb // 'no __index' flag NOT set: check.
  4971. | movzx RA, PC_RA // Restore RA.
  4972. |3:
  4973. | mov dword [BASE+RA*8+4], LJ_TNIL
  4974. | jmp <1
  4975. break;
  4976. case BC_TSETV:
  4977. | ins_ABC // RA = src, RB = table, RC = key
  4978. | checktab RB, ->vmeta_tsetv
  4979. | mov TAB:RB, [BASE+RB*8]
  4980. |
  4981. | // Integer key?
  4982. |.if DUALNUM
  4983. | checkint RC, >5
  4984. | mov RC, dword [BASE+RC*8]
  4985. |.else
  4986. | // Convert number to int and back and compare.
  4987. | checknum RC, >5
  4988. |.if SSE
  4989. | movsd xmm0, qword [BASE+RC*8]
  4990. | cvtsd2si RC, xmm0
  4991. | cvtsi2sd xmm1, RC
  4992. | ucomisd xmm0, xmm1
  4993. |.else
  4994. | fld qword [BASE+RC*8]
  4995. | fist ARG1
  4996. | fild ARG1
  4997. | fcomparepp
  4998. | mov RC, ARG1
  4999. |.endif
  5000. | jne ->vmeta_tsetv // Generic numeric key? Use fallback.
  5001. |.endif
  5002. | cmp RC, TAB:RB->asize // Takes care of unordered, too.
  5003. | jae ->vmeta_tsetv
  5004. | shl RC, 3
  5005. | add RC, TAB:RB->array
  5006. | cmp dword [RC+4], LJ_TNIL
  5007. | je >3 // Previous value is nil?
  5008. |1:
  5009. | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
  5010. | jnz >7
  5011. |2: // Set array slot.
  5012. |.if X64
  5013. | mov RBa, [BASE+RA*8]
  5014. | mov [RC], RBa
  5015. |.else
  5016. | mov RB, [BASE+RA*8+4]
  5017. | mov RA, [BASE+RA*8]
  5018. | mov [RC+4], RB
  5019. | mov [RC], RA
  5020. |.endif
  5021. | ins_next
  5022. |
  5023. |3: // Check for __newindex if previous value is nil.
  5024. | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
  5025. | jz <1
  5026. | mov TAB:RA, TAB:RB->metatable
  5027. | test byte TAB:RA->nomm, 1<<MM_newindex
  5028. | jz ->vmeta_tsetv // 'no __newindex' flag NOT set: check.
  5029. | movzx RA, PC_RA // Restore RA.
  5030. | jmp <1
  5031. |
  5032. |5: // String key?
  5033. | checkstr RC, ->vmeta_tsetv
  5034. | mov STR:RC, [BASE+RC*8]
  5035. | jmp ->BC_TSETS_Z
  5036. |
  5037. |7: // Possible table write barrier for the value. Skip valiswhite check.
  5038. | barrierback TAB:RB, RA
  5039. | movzx RA, PC_RA // Restore RA.
  5040. | jmp <2
  5041. break;
  5042. case BC_TSETS:
  5043. | ins_ABC // RA = src, RB = table, RC = str const (~)
  5044. | not RCa
  5045. | mov STR:RC, [KBASE+RC*4]
  5046. | checktab RB, ->vmeta_tsets
  5047. | mov TAB:RB, [BASE+RB*8]
  5048. |->BC_TSETS_Z: // RB = GCtab *, RC = GCstr *, refetches PC_RA.
  5049. | mov RA, TAB:RB->hmask
  5050. | and RA, STR:RC->hash
  5051. | imul RA, #NODE
  5052. | mov byte TAB:RB->nomm, 0 // Clear metamethod cache.
  5053. | add NODE:RA, TAB:RB->node
  5054. |1:
  5055. | cmp dword NODE:RA->key.it, LJ_TSTR
  5056. | jne >5
  5057. | cmp dword NODE:RA->key.gcr, STR:RC
  5058. | jne >5
  5059. | // Ok, key found. Assumes: offsetof(Node, val) == 0
  5060. | cmp dword [RA+4], LJ_TNIL
  5061. | je >4 // Previous value is nil?
  5062. |2:
  5063. | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
  5064. | jnz >7
  5065. |3: // Set node value.
  5066. | movzx RC, PC_RA
  5067. |.if X64
  5068. | mov RBa, [BASE+RC*8]
  5069. | mov [RA], RBa
  5070. |.else
  5071. | mov RB, [BASE+RC*8+4]
  5072. | mov RC, [BASE+RC*8]
  5073. | mov [RA+4], RB
  5074. | mov [RA], RC
  5075. |.endif
  5076. | ins_next
  5077. |
  5078. |4: // Check for __newindex if previous value is nil.
  5079. | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
  5080. | jz <2
  5081. | mov TMP1, RA // Save RA.
  5082. | mov TAB:RA, TAB:RB->metatable
  5083. | test byte TAB:RA->nomm, 1<<MM_newindex
  5084. | jz ->vmeta_tsets // 'no __newindex' flag NOT set: check.
  5085. | mov RA, TMP1 // Restore RA.
  5086. | jmp <2
  5087. |
  5088. |5: // Follow hash chain.
  5089. | mov NODE:RA, NODE:RA->next
  5090. | test NODE:RA, NODE:RA
  5091. | jnz <1
  5092. | // End of hash chain: key not found, add a new one.
  5093. |
  5094. | // But check for __newindex first.
  5095. | mov TAB:RA, TAB:RB->metatable
  5096. | test TAB:RA, TAB:RA
  5097. | jz >6 // No metatable: continue.
  5098. | test byte TAB:RA->nomm, 1<<MM_newindex
  5099. | jz ->vmeta_tsets // 'no __newindex' flag NOT set: check.
  5100. |6:
  5101. | mov TMP1, STR:RC
  5102. | mov TMP2, LJ_TSTR
  5103. | mov TMP3, TAB:RB // Save TAB:RB for us.
  5104. |.if X64
  5105. | mov L:CARG1d, SAVE_L
  5106. | mov L:CARG1d->base, BASE
  5107. | lea CARG3, TMP1
  5108. | mov CARG2d, TAB:RB
  5109. | mov L:RB, L:CARG1d
  5110. |.else
  5111. | lea RC, TMP1 // Store temp. TValue in TMP1/TMP2.
  5112. | mov ARG2, TAB:RB
  5113. | mov L:RB, SAVE_L
  5114. | mov ARG3, RC
  5115. | mov ARG1, L:RB
  5116. | mov L:RB->base, BASE
  5117. |.endif
  5118. | mov SAVE_PC, PC
  5119. | call extern lj_tab_newkey // (lua_State *L, GCtab *t, TValue *k)
  5120. | // Handles write barrier for the new key. TValue * returned in eax (RC).
  5121. | mov BASE, L:RB->base
  5122. | mov TAB:RB, TMP3 // Need TAB:RB for barrier.
  5123. | mov RA, eax
  5124. | jmp <2 // Must check write barrier for value.
  5125. |
  5126. |7: // Possible table write barrier for the value. Skip valiswhite check.
  5127. | barrierback TAB:RB, RC // Destroys STR:RC.
  5128. | jmp <3
  5129. break;
  5130. case BC_TSETB:
  5131. | ins_ABC // RA = src, RB = table, RC = byte literal
  5132. | checktab RB, ->vmeta_tsetb
  5133. | mov TAB:RB, [BASE+RB*8]
  5134. | cmp RC, TAB:RB->asize
  5135. | jae ->vmeta_tsetb
  5136. | shl RC, 3
  5137. | add RC, TAB:RB->array
  5138. | cmp dword [RC+4], LJ_TNIL
  5139. | je >3 // Previous value is nil?
  5140. |1:
  5141. | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
  5142. | jnz >7
  5143. |2: // Set array slot.
  5144. |.if X64
  5145. | mov RAa, [BASE+RA*8]
  5146. | mov [RC], RAa
  5147. |.else
  5148. | mov RB, [BASE+RA*8+4]
  5149. | mov RA, [BASE+RA*8]
  5150. | mov [RC+4], RB
  5151. | mov [RC], RA
  5152. |.endif
  5153. | ins_next
  5154. |
  5155. |3: // Check for __newindex if previous value is nil.
  5156. | cmp dword TAB:RB->metatable, 0 // Shouldn't overwrite RA for fastpath.
  5157. | jz <1
  5158. | mov TAB:RA, TAB:RB->metatable
  5159. | test byte TAB:RA->nomm, 1<<MM_newindex
  5160. | jz ->vmeta_tsetb // 'no __newindex' flag NOT set: check.
  5161. | movzx RA, PC_RA // Restore RA.
  5162. | jmp <1
  5163. |
  5164. |7: // Possible table write barrier for the value. Skip valiswhite check.
  5165. | barrierback TAB:RB, RA
  5166. | movzx RA, PC_RA // Restore RA.
  5167. | jmp <2
  5168. break;
  5169. case BC_TSETM:
  5170. | ins_AD // RA = base (table at base-1), RD = num const (start index)
  5171. | mov TMP1, KBASE // Need one more free register.
  5172. | mov KBASE, dword [KBASE+RD*8] // Integer constant is in lo-word.
  5173. |1:
  5174. | lea RA, [BASE+RA*8]
  5175. | mov TAB:RB, [RA-8] // Guaranteed to be a table.
  5176. | test byte TAB:RB->marked, LJ_GC_BLACK // isblack(table)
  5177. | jnz >7
  5178. |2:
  5179. | mov RD, MULTRES
  5180. | sub RD, 1
  5181. | jz >4 // Nothing to copy?
  5182. | add RD, KBASE // Compute needed size.
  5183. | cmp RD, TAB:RB->asize
  5184. | ja >5 // Doesn't fit into array part?
  5185. | sub RD, KBASE
  5186. | shl KBASE, 3
  5187. | add KBASE, TAB:RB->array
  5188. |3: // Copy result slots to table.
  5189. |.if X64
  5190. | mov RBa, [RA]
  5191. | add RA, 8
  5192. | mov [KBASE], RBa
  5193. |.else
  5194. | mov RB, [RA]
  5195. | mov [KBASE], RB
  5196. | mov RB, [RA+4]
  5197. | add RA, 8
  5198. | mov [KBASE+4], RB
  5199. |.endif
  5200. | add KBASE, 8
  5201. | sub RD, 1
  5202. | jnz <3
  5203. |4:
  5204. | mov KBASE, TMP1
  5205. | ins_next
  5206. |
  5207. |5: // Need to resize array part.
  5208. |.if X64
  5209. | mov L:CARG1d, SAVE_L
  5210. | mov L:CARG1d->base, BASE // Caveat: CARG2d/CARG3d may be BASE.
  5211. | mov CARG2d, TAB:RB
  5212. | mov CARG3d, RD
  5213. | mov L:RB, L:CARG1d
  5214. |.else
  5215. | mov ARG2, TAB:RB
  5216. | mov L:RB, SAVE_L
  5217. | mov L:RB->base, BASE
  5218. | mov ARG3, RD
  5219. | mov ARG1, L:RB
  5220. |.endif
  5221. | mov SAVE_PC, PC
  5222. | call extern lj_tab_reasize // (lua_State *L, GCtab *t, int nasize)
  5223. | mov BASE, L:RB->base
  5224. | movzx RA, PC_RA // Restore RA.
  5225. | jmp <1 // Retry.
  5226. |
  5227. |7: // Possible table write barrier for any value. Skip valiswhite check.
  5228. | barrierback TAB:RB, RD
  5229. | jmp <2
  5230. break;
  5231. /* -- Calls and vararg handling ----------------------------------------- */
  5232. case BC_CALL: case BC_CALLM:
  5233. | ins_A_C // RA = base, (RB = nresults+1,) RC = nargs+1 | extra_nargs
  5234. if (op == BC_CALLM) {
  5235. | add NARGS:RD, MULTRES
  5236. }
  5237. | cmp dword [BASE+RA*8+4], LJ_TFUNC
  5238. | mov LFUNC:RB, [BASE+RA*8]
  5239. | jne ->vmeta_call_ra
  5240. | lea BASE, [BASE+RA*8+8]
  5241. | ins_call
  5242. break;
  5243. case BC_CALLMT:
  5244. | ins_AD // RA = base, RD = extra_nargs
  5245. | add NARGS:RD, MULTRES
  5246. | // Fall through. Assumes BC_CALLT follows and ins_AD is a no-op.
  5247. break;
  5248. case BC_CALLT:
  5249. | ins_AD // RA = base, RD = nargs+1
  5250. | lea RA, [BASE+RA*8+8]
  5251. | mov KBASE, BASE // Use KBASE for move + vmeta_call hint.
  5252. | mov LFUNC:RB, [RA-8]
  5253. | cmp dword [RA-4], LJ_TFUNC
  5254. | jne ->vmeta_call
  5255. |->BC_CALLT_Z:
  5256. | mov PC, [BASE-4]
  5257. | test PC, FRAME_TYPE
  5258. | jnz >7
  5259. |1:
  5260. | mov [BASE-8], LFUNC:RB // Copy function down, reloaded below.
  5261. | mov MULTRES, NARGS:RD
  5262. | sub NARGS:RD, 1
  5263. | jz >3
  5264. |2: // Move args down.
  5265. |.if X64
  5266. | mov RBa, [RA]
  5267. | add RA, 8
  5268. | mov [KBASE], RBa
  5269. |.else
  5270. | mov RB, [RA]
  5271. | mov [KBASE], RB
  5272. | mov RB, [RA+4]
  5273. | add RA, 8
  5274. | mov [KBASE+4], RB
  5275. |.endif
  5276. | add KBASE, 8
  5277. | sub NARGS:RD, 1
  5278. | jnz <2
  5279. |
  5280. | mov LFUNC:RB, [BASE-8]
  5281. |3:
  5282. | mov NARGS:RD, MULTRES
  5283. | cmp byte LFUNC:RB->ffid, 1 // (> FF_C) Calling a fast function?
  5284. | ja >5
  5285. |4:
  5286. | ins_callt
  5287. |
  5288. |5: // Tailcall to a fast function.
  5289. | test PC, FRAME_TYPE // Lua frame below?
  5290. | jnz <4
  5291. | movzx RA, PC_RA
  5292. | not RAa
  5293. | lea RA, [BASE+RA*8]
  5294. | mov LFUNC:KBASE, [RA-8] // Need to prepare KBASE.
  5295. | mov KBASE, LFUNC:KBASE->pc
  5296. | mov KBASE, [KBASE+PC2PROTO(k)]
  5297. | jmp <4
  5298. |
  5299. |7: // Tailcall from a vararg function.
  5300. | sub PC, FRAME_VARG
  5301. | test PC, FRAME_TYPEP
  5302. | jnz >8 // Vararg frame below?
  5303. | sub BASE, PC // Need to relocate BASE/KBASE down.
  5304. | mov KBASE, BASE
  5305. | mov PC, [BASE-4]
  5306. | jmp <1
  5307. |8:
  5308. | add PC, FRAME_VARG
  5309. | jmp <1
  5310. break;
  5311. case BC_ITERC:
  5312. | ins_A // RA = base, (RB = nresults+1,) RC = nargs+1 (2+1)
  5313. | lea RA, [BASE+RA*8+8] // fb = base+1
  5314. |.if X64
  5315. | mov RBa, [RA-24] // Copy state. fb[0] = fb[-3].
  5316. | mov RCa, [RA-16] // Copy control var. fb[1] = fb[-2].
  5317. | mov [RA], RBa
  5318. | mov [RA+8], RCa
  5319. |.else
  5320. | mov RB, [RA-24] // Copy state. fb[0] = fb[-3].
  5321. | mov RC, [RA-20]
  5322. | mov [RA], RB
  5323. | mov [RA+4], RC
  5324. | mov RB, [RA-16] // Copy control var. fb[1] = fb[-2].
  5325. | mov RC, [RA-12]
  5326. | mov [RA+8], RB
  5327. | mov [RA+12], RC
  5328. |.endif
  5329. | mov LFUNC:RB, [RA-32] // Copy callable. fb[-1] = fb[-4]
  5330. | mov RC, [RA-28]
  5331. | mov [RA-8], LFUNC:RB
  5332. | mov [RA-4], RC
  5333. | cmp RC, LJ_TFUNC // Handle like a regular 2-arg call.
  5334. | mov NARGS:RD, 2+1
  5335. | jne ->vmeta_call
  5336. | mov BASE, RA
  5337. | ins_call
  5338. break;
  5339. case BC_ITERN:
  5340. | ins_A // RA = base, (RB = nresults+1, RC = nargs+1 (2+1))
  5341. |.if JIT
  5342. | // NYI: add hotloop, record BC_ITERN.
  5343. |.endif
  5344. | mov TMP1, KBASE // Need two more free registers.
  5345. | mov TMP2, DISPATCH
  5346. | mov TAB:RB, [BASE+RA*8-16]
  5347. | mov RC, [BASE+RA*8-8] // Get index from control var.
  5348. | mov DISPATCH, TAB:RB->asize
  5349. | add PC, 4
  5350. | mov KBASE, TAB:RB->array
  5351. |1: // Traverse array part.
  5352. | cmp RC, DISPATCH; jae >5 // Index points after array part?
  5353. | cmp dword [KBASE+RC*8+4], LJ_TNIL; je >4
  5354. |.if DUALNUM
  5355. | mov dword [BASE+RA*8+4], LJ_TISNUM
  5356. | mov dword [BASE+RA*8], RC
  5357. |.elif SSE
  5358. | cvtsi2sd xmm0, RC
  5359. |.else
  5360. | fild dword [BASE+RA*8-8]
  5361. |.endif
  5362. | // Copy array slot to returned value.
  5363. |.if X64
  5364. | mov RBa, [KBASE+RC*8]
  5365. | mov [BASE+RA*8+8], RBa
  5366. |.else
  5367. | mov RB, [KBASE+RC*8+4]
  5368. | mov [BASE+RA*8+12], RB
  5369. | mov RB, [KBASE+RC*8]
  5370. | mov [BASE+RA*8+8], RB
  5371. |.endif
  5372. | add RC, 1
  5373. | // Return array index as a numeric key.
  5374. |.if DUALNUM
  5375. | // See above.
  5376. |.elif SSE
  5377. | movsd qword [BASE+RA*8], xmm0
  5378. |.else
  5379. | fstp qword [BASE+RA*8]
  5380. |.endif
  5381. | mov [BASE+RA*8-8], RC // Update control var.
  5382. |2:
  5383. | movzx RD, PC_RD // Get target from ITERL.
  5384. | branchPC RD
  5385. |3:
  5386. | mov DISPATCH, TMP2
  5387. | mov KBASE, TMP1
  5388. | ins_next
  5389. |
  5390. |4: // Skip holes in array part.
  5391. | add RC, 1
  5392. |.if not (DUALNUM or SSE)
  5393. | mov [BASE+RA*8-8], RC
  5394. |.endif
  5395. | jmp <1
  5396. |
  5397. |5: // Traverse hash part.
  5398. | sub RC, DISPATCH
  5399. |6:
  5400. | cmp RC, TAB:RB->hmask; ja <3 // End of iteration? Branch to ITERL+1.
  5401. | imul KBASE, RC, #NODE
  5402. | add NODE:KBASE, TAB:RB->node
  5403. | cmp dword NODE:KBASE->val.it, LJ_TNIL; je >7
  5404. | lea DISPATCH, [RC+DISPATCH+1]
  5405. | // Copy key and value from hash slot.
  5406. |.if X64
  5407. | mov RBa, NODE:KBASE->key
  5408. | mov RCa, NODE:KBASE->val
  5409. | mov [BASE+RA*8], RBa
  5410. | mov [BASE+RA*8+8], RCa
  5411. |.else
  5412. | mov RB, NODE:KBASE->key.gcr
  5413. | mov RC, NODE:KBASE->key.it
  5414. | mov [BASE+RA*8], RB
  5415. | mov [BASE+RA*8+4], RC
  5416. | mov RB, NODE:KBASE->val.gcr
  5417. | mov RC, NODE:KBASE->val.it
  5418. | mov [BASE+RA*8+8], RB
  5419. | mov [BASE+RA*8+12], RC
  5420. |.endif
  5421. | mov [BASE+RA*8-8], DISPATCH
  5422. | jmp <2
  5423. |
  5424. |7: // Skip holes in hash part.
  5425. | add RC, 1
  5426. | jmp <6
  5427. break;
  5428. case BC_ISNEXT:
  5429. | ins_AD // RA = base, RD = target (points to ITERN)
  5430. | cmp dword [BASE+RA*8-20], LJ_TFUNC; jne >5
  5431. | mov CFUNC:RB, [BASE+RA*8-24]
  5432. | cmp dword [BASE+RA*8-12], LJ_TTAB; jne >5
  5433. | cmp dword [BASE+RA*8-4], LJ_TNIL; jne >5
  5434. | cmp byte CFUNC:RB->ffid, FF_next_N; jne >5
  5435. | branchPC RD
  5436. | mov dword [BASE+RA*8-8], 0 // Initialize control var.
  5437. | mov dword [BASE+RA*8-4], 0xfffe7fff
  5438. |1:
  5439. | ins_next
  5440. |5: // Despecialize bytecode if any of the checks fail.
  5441. | mov PC_OP, BC_JMP
  5442. | branchPC RD
  5443. | mov byte [PC], BC_ITERC
  5444. | jmp <1
  5445. break;
  5446. case BC_VARG:
  5447. | ins_ABC // RA = base, RB = nresults+1, RC = numparams
  5448. | mov TMP1, KBASE // Need one more free register.
  5449. | lea KBASE, [BASE+RC*8+(8+FRAME_VARG)]
  5450. | lea RA, [BASE+RA*8]
  5451. | sub KBASE, [BASE-4]
  5452. | // Note: KBASE may now be even _above_ BASE if nargs was < numparams.
  5453. | test RB, RB
  5454. | jz >5 // Copy all varargs?
  5455. | lea RB, [RA+RB*8-8]
  5456. | cmp KBASE, BASE // No vararg slots?
  5457. | jnb >2
  5458. |1: // Copy vararg slots to destination slots.
  5459. |.if X64
  5460. | mov RCa, [KBASE-8]
  5461. | add KBASE, 8
  5462. | mov [RA], RCa
  5463. |.else
  5464. | mov RC, [KBASE-8]
  5465. | mov [RA], RC
  5466. | mov RC, [KBASE-4]
  5467. | add KBASE, 8
  5468. | mov [RA+4], RC
  5469. |.endif
  5470. | add RA, 8
  5471. | cmp RA, RB // All destination slots filled?
  5472. | jnb >3
  5473. | cmp KBASE, BASE // No more vararg slots?
  5474. | jb <1
  5475. |2: // Fill up remainder with nil.
  5476. | mov dword [RA+4], LJ_TNIL
  5477. | add RA, 8
  5478. | cmp RA, RB
  5479. | jb <2
  5480. |3:
  5481. | mov KBASE, TMP1
  5482. | ins_next
  5483. |
  5484. |5: // Copy all varargs.
  5485. | mov MULTRES, 1 // MULTRES = 0+1
  5486. | mov RC, BASE
  5487. | sub RC, KBASE
  5488. | jbe <3 // No vararg slots?
  5489. | mov RB, RC
  5490. | shr RB, 3
  5491. | add RB, 1
  5492. | mov MULTRES, RB // MULTRES = #varargs+1
  5493. | mov L:RB, SAVE_L
  5494. | add RC, RA
  5495. | cmp RC, L:RB->maxstack
  5496. | ja >7 // Need to grow stack?
  5497. |6: // Copy all vararg slots.
  5498. |.if X64
  5499. | mov RCa, [KBASE-8]
  5500. | add KBASE, 8
  5501. | mov [RA], RCa
  5502. |.else
  5503. | mov RC, [KBASE-8]
  5504. | mov [RA], RC
  5505. | mov RC, [KBASE-4]
  5506. | add KBASE, 8
  5507. | mov [RA+4], RC
  5508. |.endif
  5509. | add RA, 8
  5510. | cmp KBASE, BASE // No more vararg slots?
  5511. | jb <6
  5512. | jmp <3
  5513. |
  5514. |7: // Grow stack for varargs.
  5515. | mov L:RB->base, BASE
  5516. | mov L:RB->top, RA
  5517. | mov SAVE_PC, PC
  5518. | sub KBASE, BASE // Need delta, because BASE may change.
  5519. | mov FCARG2, MULTRES
  5520. | sub FCARG2, 1
  5521. | mov FCARG1, L:RB
  5522. | call extern lj_state_growstack@8 // (lua_State *L, int n)
  5523. | mov BASE, L:RB->base
  5524. | mov RA, L:RB->top
  5525. | add KBASE, BASE
  5526. | jmp <6
  5527. break;
  5528. /* -- Returns ----------------------------------------------------------- */
  5529. case BC_RETM:
  5530. | ins_AD // RA = results, RD = extra_nresults
  5531. | add RD, MULTRES // MULTRES >=1, so RD >=1.
  5532. | // Fall through. Assumes BC_RET follows and ins_AD is a no-op.
  5533. break;
  5534. case BC_RET: case BC_RET0: case BC_RET1:
  5535. | ins_AD // RA = results, RD = nresults+1
  5536. if (op != BC_RET0) {
  5537. | shl RA, 3
  5538. }
  5539. |1:
  5540. | mov PC, [BASE-4]
  5541. | mov MULTRES, RD // Save nresults+1.
  5542. | test PC, FRAME_TYPE // Check frame type marker.
  5543. | jnz >7 // Not returning to a fixarg Lua func?
  5544. switch (op) {
  5545. case BC_RET:
  5546. |->BC_RET_Z:
  5547. | mov KBASE, BASE // Use KBASE for result move.
  5548. | sub RD, 1
  5549. | jz >3
  5550. |2: // Move results down.
  5551. |.if X64
  5552. | mov RBa, [KBASE+RA]
  5553. | mov [KBASE-8], RBa
  5554. |.else
  5555. | mov RB, [KBASE+RA]
  5556. | mov [KBASE-8], RB
  5557. | mov RB, [KBASE+RA+4]
  5558. | mov [KBASE-4], RB
  5559. |.endif
  5560. | add KBASE, 8
  5561. | sub RD, 1
  5562. | jnz <2
  5563. |3:
  5564. | mov RD, MULTRES // Note: MULTRES may be >255.
  5565. | movzx RB, PC_RB // So cannot compare with RDL!
  5566. |5:
  5567. | cmp RB, RD // More results expected?
  5568. | ja >6
  5569. break;
  5570. case BC_RET1:
  5571. |.if X64
  5572. | mov RBa, [BASE+RA]
  5573. | mov [BASE-8], RBa
  5574. |.else
  5575. | mov RB, [BASE+RA+4]
  5576. | mov [BASE-4], RB
  5577. | mov RB, [BASE+RA]
  5578. | mov [BASE-8], RB
  5579. |.endif
  5580. /* fallthrough */
  5581. case BC_RET0:
  5582. |5:
  5583. | cmp PC_RB, RDL // More results expected?
  5584. | ja >6
  5585. default:
  5586. break;
  5587. }
  5588. | movzx RA, PC_RA
  5589. | not RAa // Note: ~RA = -(RA+1)
  5590. | lea BASE, [BASE+RA*8] // base = base - (RA+1)*8
  5591. | mov LFUNC:KBASE, [BASE-8]
  5592. | mov KBASE, LFUNC:KBASE->pc
  5593. | mov KBASE, [KBASE+PC2PROTO(k)]
  5594. | ins_next
  5595. |
  5596. |6: // Fill up results with nil.
  5597. if (op == BC_RET) {
  5598. | mov dword [KBASE-4], LJ_TNIL // Note: relies on shifted base.
  5599. | add KBASE, 8
  5600. } else {
  5601. | mov dword [BASE+RD*8-12], LJ_TNIL
  5602. }
  5603. | add RD, 1
  5604. | jmp <5
  5605. |
  5606. |7: // Non-standard return case.
  5607. | lea RB, [PC-FRAME_VARG]
  5608. | test RB, FRAME_TYPEP
  5609. | jnz ->vm_return
  5610. | // Return from vararg function: relocate BASE down and RA up.
  5611. | sub BASE, RB
  5612. if (op != BC_RET0) {
  5613. | add RA, RB
  5614. }
  5615. | jmp <1
  5616. break;
  5617. /* -- Loops and branches ------------------------------------------------ */
  5618. |.define FOR_IDX, [RA]; .define FOR_TIDX, dword [RA+4]
  5619. |.define FOR_STOP, [RA+8]; .define FOR_TSTOP, dword [RA+12]
  5620. |.define FOR_STEP, [RA+16]; .define FOR_TSTEP, dword [RA+20]
  5621. |.define FOR_EXT, [RA+24]; .define FOR_TEXT, dword [RA+28]
  5622. case BC_FORL:
  5623. |.if JIT
  5624. | hotloop RB
  5625. |.endif
  5626. | // Fall through. Assumes BC_IFORL follows and ins_AJ is a no-op.
  5627. break;
  5628. case BC_JFORI:
  5629. case BC_JFORL:
  5630. #if !LJ_HASJIT
  5631. break;
  5632. #endif
  5633. case BC_FORI:
  5634. case BC_IFORL:
  5635. vk = (op == BC_IFORL || op == BC_JFORL);
  5636. | ins_AJ // RA = base, RD = target (after end of loop or start of loop)
  5637. | lea RA, [BASE+RA*8]
  5638. if (LJ_DUALNUM) {
  5639. | cmp FOR_TIDX, LJ_TISNUM; jne >9
  5640. if (!vk) {
  5641. | cmp FOR_TSTOP, LJ_TISNUM; jne ->vmeta_for
  5642. | cmp FOR_TSTEP, LJ_TISNUM; jne ->vmeta_for
  5643. | mov RB, dword FOR_IDX
  5644. | cmp dword FOR_STEP, 0; jl >5
  5645. } else {
  5646. #ifdef LUA_USE_ASSERT
  5647. | cmp FOR_TSTOP, LJ_TISNUM; jne ->assert_bad_for_arg_type
  5648. | cmp FOR_TSTEP, LJ_TISNUM; jne ->assert_bad_for_arg_type
  5649. #endif
  5650. | mov RB, dword FOR_STEP
  5651. | test RB, RB; js >5
  5652. | add RB, dword FOR_IDX; jo >1
  5653. | mov dword FOR_IDX, RB
  5654. }
  5655. | cmp RB, dword FOR_STOP
  5656. | mov FOR_TEXT, LJ_TISNUM
  5657. | mov dword FOR_EXT, RB
  5658. if (op == BC_FORI) {
  5659. | jle >7
  5660. |1:
  5661. |6:
  5662. | branchPC RD
  5663. } else if (op == BC_JFORI) {
  5664. | branchPC RD
  5665. | movzx RD, PC_RD
  5666. | jle =>BC_JLOOP
  5667. |1:
  5668. |6:
  5669. } else if (op == BC_IFORL) {
  5670. | jg >7
  5671. |6:
  5672. | branchPC RD
  5673. |1:
  5674. } else {
  5675. | jle =>BC_JLOOP
  5676. |1:
  5677. |6:
  5678. }
  5679. |7:
  5680. | ins_next
  5681. |
  5682. |5: // Invert check for negative step.
  5683. if (vk) {
  5684. | add RB, dword FOR_IDX; jo <1
  5685. | mov dword FOR_IDX, RB
  5686. }
  5687. | cmp RB, dword FOR_STOP
  5688. | mov FOR_TEXT, LJ_TISNUM
  5689. | mov dword FOR_EXT, RB
  5690. if (op == BC_FORI) {
  5691. | jge <7
  5692. } else if (op == BC_JFORI) {
  5693. | branchPC RD
  5694. | movzx RD, PC_RD
  5695. | jge =>BC_JLOOP
  5696. } else if (op == BC_IFORL) {
  5697. | jl <7
  5698. } else {
  5699. | jge =>BC_JLOOP
  5700. }
  5701. | jmp <6
  5702. |9: // Fallback to FP variant.
  5703. } else if (!vk) {
  5704. | cmp FOR_TIDX, LJ_TISNUM
  5705. }
  5706. if (!vk) {
  5707. | jae ->vmeta_for
  5708. | cmp FOR_TSTOP, LJ_TISNUM; jae ->vmeta_for
  5709. } else {
  5710. #ifdef LUA_USE_ASSERT
  5711. | cmp FOR_TSTOP, LJ_TISNUM; jae ->assert_bad_for_arg_type
  5712. | cmp FOR_TSTEP, LJ_TISNUM; jae ->assert_bad_for_arg_type
  5713. #endif
  5714. }
  5715. | mov RB, FOR_TSTEP // Load type/hiword of for step.
  5716. if (!vk) {
  5717. | cmp RB, LJ_TISNUM; jae ->vmeta_for
  5718. }
  5719. |.if SSE
  5720. | movsd xmm0, qword FOR_IDX
  5721. | movsd xmm1, qword FOR_STOP
  5722. if (vk) {
  5723. | addsd xmm0, qword FOR_STEP
  5724. | movsd qword FOR_IDX, xmm0
  5725. | test RB, RB; js >3
  5726. } else {
  5727. | jl >3
  5728. }
  5729. | ucomisd xmm1, xmm0
  5730. |1:
  5731. | movsd qword FOR_EXT, xmm0
  5732. |.else
  5733. | fld qword FOR_STOP
  5734. | fld qword FOR_IDX
  5735. if (vk) {
  5736. | fadd qword FOR_STEP // nidx = idx + step
  5737. | fst qword FOR_IDX
  5738. | fst qword FOR_EXT
  5739. | test RB, RB; js >1
  5740. } else {
  5741. | fst qword FOR_EXT
  5742. | jl >1
  5743. }
  5744. | fxch // Swap lim/(n)idx if step non-negative.
  5745. |1:
  5746. | fcomparepp
  5747. |.endif
  5748. if (op == BC_FORI) {
  5749. |.if DUALNUM
  5750. | jnb <7
  5751. |.else
  5752. | jnb >2
  5753. | branchPC RD
  5754. |.endif
  5755. } else if (op == BC_JFORI) {
  5756. | branchPC RD
  5757. | movzx RD, PC_RD
  5758. | jnb =>BC_JLOOP
  5759. } else if (op == BC_IFORL) {
  5760. |.if DUALNUM
  5761. | jb <7
  5762. |.else
  5763. | jb >2
  5764. | branchPC RD
  5765. |.endif
  5766. } else {
  5767. | jnb =>BC_JLOOP
  5768. }
  5769. |.if DUALNUM
  5770. | jmp <6
  5771. |.else
  5772. |2:
  5773. | ins_next
  5774. |.endif
  5775. |.if SSE
  5776. |3: // Invert comparison if step is negative.
  5777. | ucomisd xmm0, xmm1
  5778. | jmp <1
  5779. |.endif
  5780. break;
  5781. case BC_ITERL:
  5782. |.if JIT
  5783. | hotloop RB
  5784. |.endif
  5785. | // Fall through. Assumes BC_IITERL follows and ins_AJ is a no-op.
  5786. break;
  5787. case BC_JITERL:
  5788. #if !LJ_HASJIT
  5789. break;
  5790. #endif
  5791. case BC_IITERL:
  5792. | ins_AJ // RA = base, RD = target
  5793. | lea RA, [BASE+RA*8]
  5794. | mov RB, [RA+4]
  5795. | cmp RB, LJ_TNIL; je >1 // Stop if iterator returned nil.
  5796. if (op == BC_JITERL) {
  5797. | mov [RA-4], RB
  5798. | mov RB, [RA]
  5799. | mov [RA-8], RB
  5800. | jmp =>BC_JLOOP
  5801. } else {
  5802. | branchPC RD // Otherwise save control var + branch.
  5803. | mov RD, [RA]
  5804. | mov [RA-4], RB
  5805. | mov [RA-8], RD
  5806. }
  5807. |1:
  5808. | ins_next
  5809. break;
  5810. case BC_LOOP:
  5811. | ins_A // RA = base, RD = target (loop extent)
  5812. | // Note: RA/RD is only used by trace recorder to determine scope/extent
  5813. | // This opcode does NOT jump, it's only purpose is to detect a hot loop.
  5814. |.if JIT
  5815. | hotloop RB
  5816. |.endif
  5817. | // Fall through. Assumes BC_ILOOP follows and ins_A is a no-op.
  5818. break;
  5819. case BC_ILOOP:
  5820. | ins_A // RA = base, RD = target (loop extent)
  5821. | ins_next
  5822. break;
  5823. case BC_JLOOP:
  5824. |.if JIT
  5825. | ins_AD // RA = base (ignored), RD = traceno
  5826. | mov RA, [DISPATCH+DISPATCH_J(trace)]
  5827. | mov TRACE:RD, [RA+RD*4]
  5828. | mov RDa, TRACE:RD->mcode
  5829. | mov L:RB, SAVE_L
  5830. | mov [DISPATCH+DISPATCH_GL(jit_base)], BASE
  5831. | mov [DISPATCH+DISPATCH_GL(jit_L)], L:RB
  5832. | // Save additional callee-save registers only used in compiled code.
  5833. |.if X64WIN
  5834. | mov TMPQ, r12
  5835. | mov TMPa, r13
  5836. | mov CSAVE_4, r14
  5837. | mov CSAVE_3, r15
  5838. | mov RAa, rsp
  5839. | sub rsp, 9*16+4*8
  5840. | movdqa [RAa], xmm6
  5841. | movdqa [RAa-1*16], xmm7
  5842. | movdqa [RAa-2*16], xmm8
  5843. | movdqa [RAa-3*16], xmm9
  5844. | movdqa [RAa-4*16], xmm10
  5845. | movdqa [RAa-5*16], xmm11
  5846. | movdqa [RAa-6*16], xmm12
  5847. | movdqa [RAa-7*16], xmm13
  5848. | movdqa [RAa-8*16], xmm14
  5849. | movdqa [RAa-9*16], xmm15
  5850. |.elif X64
  5851. | mov TMPQ, r12
  5852. | mov TMPa, r13
  5853. | sub rsp, 16
  5854. |.endif
  5855. | jmp RDa
  5856. |.endif
  5857. break;
  5858. case BC_JMP:
  5859. | ins_AJ // RA = unused, RD = target
  5860. | branchPC RD
  5861. | ins_next
  5862. break;
  5863. /* -- Function headers -------------------------------------------------- */
  5864. /*
  5865. ** Reminder: A function may be called with func/args above L->maxstack,
  5866. ** i.e. occupying EXTRA_STACK slots. And vmeta_call may add one extra slot,
  5867. ** too. This means all FUNC* ops (including fast functions) must check
  5868. ** for stack overflow _before_ adding more slots!
  5869. */
  5870. case BC_FUNCF:
  5871. |.if JIT
  5872. | hotcall RB
  5873. |.endif
  5874. case BC_FUNCV: /* NYI: compiled vararg functions. */
  5875. | // Fall through. Assumes BC_IFUNCF/BC_IFUNCV follow and ins_AD is a no-op.
  5876. break;
  5877. case BC_JFUNCF:
  5878. #if !LJ_HASJIT
  5879. break;
  5880. #endif
  5881. case BC_IFUNCF:
  5882. | ins_AD // BASE = new base, RA = framesize, RD = nargs+1
  5883. | mov KBASE, [PC-4+PC2PROTO(k)]
  5884. | mov L:RB, SAVE_L
  5885. | lea RA, [BASE+RA*8] // Top of frame.
  5886. | cmp RA, L:RB->maxstack
  5887. | ja ->vm_growstack_f
  5888. | movzx RA, byte [PC-4+PC2PROTO(numparams)]
  5889. | cmp NARGS:RD, RA // Check for missing parameters.
  5890. | jbe >3
  5891. |2:
  5892. if (op == BC_JFUNCF) {
  5893. | movzx RD, PC_RD
  5894. | jmp =>BC_JLOOP
  5895. } else {
  5896. | ins_next
  5897. }
  5898. |
  5899. |3: // Clear missing parameters.
  5900. | mov dword [BASE+NARGS:RD*8-4], LJ_TNIL
  5901. | add NARGS:RD, 1
  5902. | cmp NARGS:RD, RA
  5903. | jbe <3
  5904. | jmp <2
  5905. break;
  5906. case BC_JFUNCV:
  5907. #if !LJ_HASJIT
  5908. break;
  5909. #endif
  5910. | int3 // NYI: compiled vararg functions
  5911. break; /* NYI: compiled vararg functions. */
  5912. case BC_IFUNCV:
  5913. | ins_AD // BASE = new base, RA = framesize, RD = nargs+1
  5914. | lea RB, [NARGS:RD*8+FRAME_VARG]
  5915. | lea RD, [BASE+NARGS:RD*8]
  5916. | mov LFUNC:KBASE, [BASE-8]
  5917. | mov [RD-4], RB // Store delta + FRAME_VARG.
  5918. | mov [RD-8], LFUNC:KBASE // Store copy of LFUNC.
  5919. | mov L:RB, SAVE_L
  5920. | lea RA, [RD+RA*8]
  5921. | cmp RA, L:RB->maxstack
  5922. | ja ->vm_growstack_v // Need to grow stack.
  5923. | mov RA, BASE
  5924. | mov BASE, RD
  5925. | movzx RB, byte [PC-4+PC2PROTO(numparams)]
  5926. | test RB, RB
  5927. | jz >2
  5928. |1: // Copy fixarg slots up to new frame.
  5929. | add RA, 8
  5930. | cmp RA, BASE
  5931. | jnb >3 // Less args than parameters?
  5932. | mov KBASE, [RA-8]
  5933. | mov [RD], KBASE
  5934. | mov KBASE, [RA-4]
  5935. | mov [RD+4], KBASE
  5936. | add RD, 8
  5937. | mov dword [RA-4], LJ_TNIL // Clear old fixarg slot (help the GC).
  5938. | sub RB, 1
  5939. | jnz <1
  5940. |2:
  5941. if (op == BC_JFUNCV) {
  5942. | movzx RD, PC_RD
  5943. | jmp =>BC_JLOOP
  5944. } else {
  5945. | mov KBASE, [PC-4+PC2PROTO(k)]
  5946. | ins_next
  5947. }
  5948. |
  5949. |3: // Clear missing parameters.
  5950. | mov dword [RD+4], LJ_TNIL
  5951. | add RD, 8
  5952. | sub RB, 1
  5953. | jnz <3
  5954. | jmp <2
  5955. break;
  5956. case BC_FUNCC:
  5957. case BC_FUNCCW:
  5958. | ins_AD // BASE = new base, RA = ins RA|RD (unused), RD = nargs+1
  5959. | mov CFUNC:RB, [BASE-8]
  5960. | mov KBASEa, CFUNC:RB->f
  5961. | mov L:RB, SAVE_L
  5962. | lea RD, [BASE+NARGS:RD*8-8]
  5963. | mov L:RB->base, BASE
  5964. | lea RA, [RD+8*LUA_MINSTACK]
  5965. | cmp RA, L:RB->maxstack
  5966. | mov L:RB->top, RD
  5967. if (op == BC_FUNCC) {
  5968. |.if X64
  5969. | mov CARG1d, L:RB // Caveat: CARG1d may be RA.
  5970. |.else
  5971. | mov ARG1, L:RB
  5972. |.endif
  5973. } else {
  5974. |.if X64
  5975. | mov CARG2, KBASEa
  5976. | mov CARG1d, L:RB // Caveat: CARG1d may be RA.
  5977. |.else
  5978. | mov ARG2, KBASEa
  5979. | mov ARG1, L:RB
  5980. |.endif
  5981. }
  5982. | ja ->vm_growstack_c // Need to grow stack.
  5983. | set_vmstate C
  5984. if (op == BC_FUNCC) {
  5985. | call KBASEa // (lua_State *L)
  5986. } else {
  5987. | // (lua_State *L, lua_CFunction f)
  5988. | call aword [DISPATCH+DISPATCH_GL(wrapf)]
  5989. }
  5990. | set_vmstate INTERP
  5991. | // nresults returned in eax (RD).
  5992. | mov BASE, L:RB->base
  5993. | lea RA, [BASE+RD*8]
  5994. | neg RA
  5995. | add RA, L:RB->top // RA = (L->top-(L->base+nresults))*8
  5996. | mov PC, [BASE-4] // Fetch PC of caller.
  5997. | jmp ->vm_returnc
  5998. break;
  5999. /* ---------------------------------------------------------------------- */
  6000. default:
  6001. fprintf(stderr, "Error: undefined opcode BC_%s\n", bc_names[op]);
  6002. exit(2);
  6003. break;
  6004. }
  6005. }
  6006. static int build_backend(BuildCtx *ctx)
  6007. {
  6008. int op;
  6009. dasm_growpc(Dst, BC__MAX);
  6010. build_subroutines(ctx);
  6011. |.code_op
  6012. for (op = 0; op < BC__MAX; op++)
  6013. build_ins(ctx, (BCOp)op, op);
  6014. return BC__MAX;
  6015. }
  6016. /* Emit pseudo frame-info for all assembler functions. */
  6017. static void emit_asm_debug(BuildCtx *ctx)
  6018. {
  6019. int fcofs = (int)((uint8_t *)ctx->glob[GLOB_vm_ffi_call] - ctx->code);
  6020. #if LJ_64
  6021. #define SZPTR "8"
  6022. #define BSZPTR "3"
  6023. #define REG_SP "0x7"
  6024. #define REG_RA "0x10"
  6025. #else
  6026. #define SZPTR "4"
  6027. #define BSZPTR "2"
  6028. #define REG_SP "0x4"
  6029. #define REG_RA "0x8"
  6030. #endif
  6031. switch (ctx->mode) {
  6032. case BUILD_elfasm:
  6033. fprintf(ctx->fp, "\t.section .debug_frame,\"\",@progbits\n");
  6034. fprintf(ctx->fp,
  6035. ".Lframe0:\n"
  6036. "\t.long .LECIE0-.LSCIE0\n"
  6037. ".LSCIE0:\n"
  6038. "\t.long 0xffffffff\n"
  6039. "\t.byte 0x1\n"
  6040. "\t.string \"\"\n"
  6041. "\t.uleb128 0x1\n"
  6042. "\t.sleb128 -" SZPTR "\n"
  6043. "\t.byte " REG_RA "\n"
  6044. "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"
  6045. "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"
  6046. "\t.align " SZPTR "\n"
  6047. ".LECIE0:\n\n");
  6048. fprintf(ctx->fp,
  6049. ".LSFDE0:\n"
  6050. "\t.long .LEFDE0-.LASFDE0\n"
  6051. ".LASFDE0:\n"
  6052. "\t.long .Lframe0\n"
  6053. #if LJ_64
  6054. "\t.quad .Lbegin\n"
  6055. "\t.quad %d\n"
  6056. "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
  6057. "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
  6058. "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
  6059. "\t.byte 0x8f\n\t.uleb128 0x4\n" /* offset r15 */
  6060. "\t.byte 0x8e\n\t.uleb128 0x5\n" /* offset r14 */
  6061. #else
  6062. "\t.long .Lbegin\n"
  6063. "\t.long %d\n"
  6064. "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
  6065. "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
  6066. "\t.byte 0x87\n\t.uleb128 0x3\n" /* offset edi */
  6067. "\t.byte 0x86\n\t.uleb128 0x4\n" /* offset esi */
  6068. "\t.byte 0x83\n\t.uleb128 0x5\n" /* offset ebx */
  6069. #endif
  6070. "\t.align " SZPTR "\n"
  6071. ".LEFDE0:\n\n", fcofs, CFRAME_SIZE);
  6072. #if LJ_HASFFI
  6073. fprintf(ctx->fp,
  6074. ".LSFDE1:\n"
  6075. "\t.long .LEFDE1-.LASFDE1\n"
  6076. ".LASFDE1:\n"
  6077. "\t.long .Lframe0\n"
  6078. #if LJ_64
  6079. "\t.quad lj_vm_ffi_call\n"
  6080. "\t.quad %d\n"
  6081. "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */
  6082. "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
  6083. "\t.byte 0xd\n\t.uleb128 0x6\n" /* def_cfa_register rbp */
  6084. "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
  6085. #else
  6086. "\t.long lj_vm_ffi_call\n"
  6087. "\t.long %d\n"
  6088. "\t.byte 0xe\n\t.uleb128 8\n" /* def_cfa_offset */
  6089. "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
  6090. "\t.byte 0xd\n\t.uleb128 0x5\n" /* def_cfa_register ebp */
  6091. "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset ebx */
  6092. #endif
  6093. "\t.align " SZPTR "\n"
  6094. ".LEFDE1:\n\n", (int)ctx->codesz - fcofs);
  6095. #endif
  6096. #if (defined(__sun__) && defined(__svr4__))
  6097. #if LJ_64
  6098. fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@unwind\n");
  6099. #else
  6100. fprintf(ctx->fp, "\t.section .eh_frame,\"aw\",@progbits\n");
  6101. #endif
  6102. #else
  6103. fprintf(ctx->fp, "\t.section .eh_frame,\"a\",@progbits\n");
  6104. #endif
  6105. fprintf(ctx->fp,
  6106. ".Lframe1:\n"
  6107. "\t.long .LECIE1-.LSCIE1\n"
  6108. ".LSCIE1:\n"
  6109. "\t.long 0\n"
  6110. "\t.byte 0x1\n"
  6111. "\t.string \"zPR\"\n"
  6112. "\t.uleb128 0x1\n"
  6113. "\t.sleb128 -" SZPTR "\n"
  6114. "\t.byte " REG_RA "\n"
  6115. "\t.uleb128 6\n" /* augmentation length */
  6116. "\t.byte 0x1b\n" /* pcrel|sdata4 */
  6117. "\t.long lj_err_unwind_dwarf-.\n"
  6118. "\t.byte 0x1b\n" /* pcrel|sdata4 */
  6119. "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"
  6120. "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"
  6121. "\t.align " SZPTR "\n"
  6122. ".LECIE1:\n\n");
  6123. fprintf(ctx->fp,
  6124. ".LSFDE2:\n"
  6125. "\t.long .LEFDE2-.LASFDE2\n"
  6126. ".LASFDE2:\n"
  6127. "\t.long .LASFDE2-.Lframe1\n"
  6128. "\t.long .Lbegin-.\n"
  6129. "\t.long %d\n"
  6130. "\t.uleb128 0\n" /* augmentation length */
  6131. "\t.byte 0xe\n\t.uleb128 %d\n" /* def_cfa_offset */
  6132. #if LJ_64
  6133. "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
  6134. "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
  6135. "\t.byte 0x8f\n\t.uleb128 0x4\n" /* offset r15 */
  6136. "\t.byte 0x8e\n\t.uleb128 0x5\n" /* offset r14 */
  6137. #else
  6138. "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
  6139. "\t.byte 0x87\n\t.uleb128 0x3\n" /* offset edi */
  6140. "\t.byte 0x86\n\t.uleb128 0x4\n" /* offset esi */
  6141. "\t.byte 0x83\n\t.uleb128 0x5\n" /* offset ebx */
  6142. #endif
  6143. "\t.align " SZPTR "\n"
  6144. ".LEFDE2:\n\n", fcofs, CFRAME_SIZE);
  6145. #if LJ_HASFFI
  6146. fprintf(ctx->fp,
  6147. ".Lframe2:\n"
  6148. "\t.long .LECIE2-.LSCIE2\n"
  6149. ".LSCIE2:\n"
  6150. "\t.long 0\n"
  6151. "\t.byte 0x1\n"
  6152. "\t.string \"zR\"\n"
  6153. "\t.uleb128 0x1\n"
  6154. "\t.sleb128 -" SZPTR "\n"
  6155. "\t.byte " REG_RA "\n"
  6156. "\t.uleb128 1\n" /* augmentation length */
  6157. "\t.byte 0x1b\n" /* pcrel|sdata4 */
  6158. "\t.byte 0xc\n\t.uleb128 " REG_SP "\n\t.uleb128 " SZPTR "\n"
  6159. "\t.byte 0x80+" REG_RA "\n\t.uleb128 0x1\n"
  6160. "\t.align " SZPTR "\n"
  6161. ".LECIE2:\n\n");
  6162. fprintf(ctx->fp,
  6163. ".LSFDE3:\n"
  6164. "\t.long .LEFDE3-.LASFDE3\n"
  6165. ".LASFDE3:\n"
  6166. "\t.long .LASFDE3-.Lframe2\n"
  6167. "\t.long lj_vm_ffi_call-.\n"
  6168. "\t.long %d\n"
  6169. "\t.uleb128 0\n" /* augmentation length */
  6170. #if LJ_64
  6171. "\t.byte 0xe\n\t.uleb128 16\n" /* def_cfa_offset */
  6172. "\t.byte 0x86\n\t.uleb128 0x2\n" /* offset rbp */
  6173. "\t.byte 0xd\n\t.uleb128 0x6\n" /* def_cfa_register rbp */
  6174. "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset rbx */
  6175. #else
  6176. "\t.byte 0xe\n\t.uleb128 8\n" /* def_cfa_offset */
  6177. "\t.byte 0x85\n\t.uleb128 0x2\n" /* offset ebp */
  6178. "\t.byte 0xd\n\t.uleb128 0x5\n" /* def_cfa_register ebp */
  6179. "\t.byte 0x83\n\t.uleb128 0x3\n" /* offset ebx */
  6180. #endif
  6181. "\t.align " SZPTR "\n"
  6182. ".LEFDE3:\n\n", (int)ctx->codesz - fcofs);
  6183. #endif
  6184. break;
  6185. /* Mental note: never let Apple design an assembler.
  6186. ** Or a linker. Or a plastic case. But I digress.
  6187. */
  6188. case BUILD_machasm: {
  6189. #if LJ_HASFFI
  6190. int fcsize = 0;
  6191. #endif
  6192. int i;
  6193. fprintf(ctx->fp, "\t.section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support\n");
  6194. fprintf(ctx->fp,
  6195. "EH_frame1:\n"
  6196. "\t.set L$set$x,LECIEX-LSCIEX\n"
  6197. "\t.long L$set$x\n"
  6198. "LSCIEX:\n"
  6199. "\t.long 0\n"
  6200. "\t.byte 0x1\n"
  6201. "\t.ascii \"zPR\\0\"\n"
  6202. "\t.byte 0x1\n"
  6203. "\t.byte 128-" SZPTR "\n"
  6204. "\t.byte " REG_RA "\n"
  6205. "\t.byte 6\n" /* augmentation length */
  6206. "\t.byte 0x9b\n" /* indirect|pcrel|sdata4 */
  6207. #if LJ_64
  6208. "\t.long _lj_err_unwind_dwarf+4@GOTPCREL\n"
  6209. "\t.byte 0x1b\n" /* pcrel|sdata4 */
  6210. "\t.byte 0xc\n\t.byte " REG_SP "\n\t.byte " SZPTR "\n"
  6211. #else
  6212. "\t.long L_lj_err_unwind_dwarf$non_lazy_ptr-.\n"
  6213. "\t.byte 0x1b\n" /* pcrel|sdata4 */
  6214. "\t.byte 0xc\n\t.byte 0x5\n\t.byte 0x4\n" /* esp=5 on 32 bit MACH-O. */
  6215. #endif
  6216. "\t.byte 0x80+" REG_RA "\n\t.byte 0x1\n"
  6217. "\t.align " BSZPTR "\n"
  6218. "LECIEX:\n\n");
  6219. for (i = 0; i < ctx->nsym; i++) {
  6220. const char *name = ctx->sym[i].name;
  6221. int32_t size = ctx->sym[i+1].ofs - ctx->sym[i].ofs;
  6222. if (size == 0) continue;
  6223. #if LJ_HASFFI
  6224. if (!strcmp(name, "_lj_vm_ffi_call")) { fcsize = size; continue; }
  6225. #endif
  6226. fprintf(ctx->fp,
  6227. "%s.eh:\n"
  6228. "LSFDE%d:\n"
  6229. "\t.set L$set$%d,LEFDE%d-LASFDE%d\n"
  6230. "\t.long L$set$%d\n"
  6231. "LASFDE%d:\n"
  6232. "\t.long LASFDE%d-EH_frame1\n"
  6233. "\t.long %s-.\n"
  6234. "\t.long %d\n"
  6235. "\t.byte 0\n" /* augmentation length */
  6236. "\t.byte 0xe\n\t.byte %d\n" /* def_cfa_offset */
  6237. #if LJ_64
  6238. "\t.byte 0x86\n\t.byte 0x2\n" /* offset rbp */
  6239. "\t.byte 0x83\n\t.byte 0x3\n" /* offset rbx */
  6240. "\t.byte 0x8f\n\t.byte 0x4\n" /* offset r15 */
  6241. "\t.byte 0x8e\n\t.byte 0x5\n" /* offset r14 */
  6242. #else
  6243. "\t.byte 0x84\n\t.byte 0x2\n" /* offset ebp (4 for MACH-O)*/
  6244. "\t.byte 0x87\n\t.byte 0x3\n" /* offset edi */
  6245. "\t.byte 0x86\n\t.byte 0x4\n" /* offset esi */
  6246. "\t.byte 0x83\n\t.byte 0x5\n" /* offset ebx */
  6247. #endif
  6248. "\t.align " BSZPTR "\n"
  6249. "LEFDE%d:\n\n",
  6250. name, i, i, i, i, i, i, i, name, size, CFRAME_SIZE, i);
  6251. }
  6252. #if LJ_HASFFI
  6253. if (fcsize) {
  6254. fprintf(ctx->fp,
  6255. "EH_frame2:\n"
  6256. "\t.set L$set$y,LECIEY-LSCIEY\n"
  6257. "\t.long L$set$y\n"
  6258. "LSCIEY:\n"
  6259. "\t.long 0\n"
  6260. "\t.byte 0x1\n"
  6261. "\t.ascii \"zR\\0\"\n"
  6262. "\t.byte 0x1\n"
  6263. "\t.byte 128-" SZPTR "\n"
  6264. "\t.byte " REG_RA "\n"
  6265. "\t.byte 1\n" /* augmentation length */
  6266. #if LJ_64
  6267. "\t.byte 0x1b\n" /* pcrel|sdata4 */
  6268. "\t.byte 0xc\n\t.byte " REG_SP "\n\t.byte " SZPTR "\n"
  6269. #else
  6270. "\t.byte 0x1b\n" /* pcrel|sdata4 */
  6271. "\t.byte 0xc\n\t.byte 0x5\n\t.byte 0x4\n" /* esp=5 on 32 bit MACH. */
  6272. #endif
  6273. "\t.byte 0x80+" REG_RA "\n\t.byte 0x1\n"
  6274. "\t.align " BSZPTR "\n"
  6275. "LECIEY:\n\n");
  6276. fprintf(ctx->fp,
  6277. "_lj_vm_ffi_call.eh:\n"
  6278. "LSFDEY:\n"
  6279. "\t.set L$set$yy,LEFDEY-LASFDEY\n"
  6280. "\t.long L$set$yy\n"
  6281. "LASFDEY:\n"
  6282. "\t.long LASFDEY-EH_frame2\n"
  6283. "\t.long _lj_vm_ffi_call-.\n"
  6284. "\t.long %d\n"
  6285. "\t.byte 0\n" /* augmentation length */
  6286. #if LJ_64
  6287. "\t.byte 0xe\n\t.byte 16\n" /* def_cfa_offset */
  6288. "\t.byte 0x86\n\t.byte 0x2\n" /* offset rbp */
  6289. "\t.byte 0xd\n\t.byte 0x6\n" /* def_cfa_register rbp */
  6290. "\t.byte 0x83\n\t.byte 0x3\n" /* offset rbx */
  6291. #else
  6292. "\t.byte 0xe\n\t.byte 8\n" /* def_cfa_offset */
  6293. "\t.byte 0x84\n\t.byte 0x2\n" /* offset ebp (4 for MACH-O)*/
  6294. "\t.byte 0xd\n\t.byte 0x4\n" /* def_cfa_register ebp */
  6295. "\t.byte 0x83\n\t.byte 0x3\n" /* offset ebx */
  6296. #endif
  6297. "\t.align " BSZPTR "\n"
  6298. "LEFDEY:\n\n", fcsize);
  6299. }
  6300. #endif
  6301. #if LJ_64
  6302. fprintf(ctx->fp, "\t.subsections_via_symbols\n");
  6303. #else
  6304. fprintf(ctx->fp,
  6305. "\t.non_lazy_symbol_pointer\n"
  6306. "L_lj_err_unwind_dwarf$non_lazy_ptr:\n"
  6307. ".indirect_symbol _lj_err_unwind_dwarf\n"
  6308. ".long 0\n");
  6309. #endif
  6310. }
  6311. break;
  6312. default: /* Difficult for other modes. */
  6313. break;
  6314. }
  6315. }