MasmParser.cpp 257 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756
  1. //===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This class implements the parser for assembly files.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/ADT/APFloat.h"
  13. #include "llvm/ADT/APInt.h"
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/ADT/BitVector.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/None.h"
  18. #include "llvm/ADT/Optional.h"
  19. #include "llvm/ADT/STLExtras.h"
  20. #include "llvm/ADT/SmallString.h"
  21. #include "llvm/ADT/SmallVector.h"
  22. #include "llvm/ADT/StringExtras.h"
  23. #include "llvm/ADT/StringMap.h"
  24. #include "llvm/ADT/StringRef.h"
  25. #include "llvm/ADT/StringSwitch.h"
  26. #include "llvm/ADT/Twine.h"
  27. #include "llvm/BinaryFormat/Dwarf.h"
  28. #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
  29. #include "llvm/MC/MCAsmInfo.h"
  30. #include "llvm/MC/MCCodeView.h"
  31. #include "llvm/MC/MCContext.h"
  32. #include "llvm/MC/MCDirectives.h"
  33. #include "llvm/MC/MCDwarf.h"
  34. #include "llvm/MC/MCExpr.h"
  35. #include "llvm/MC/MCInstPrinter.h"
  36. #include "llvm/MC/MCInstrDesc.h"
  37. #include "llvm/MC/MCInstrInfo.h"
  38. #include "llvm/MC/MCObjectFileInfo.h"
  39. #include "llvm/MC/MCParser/AsmCond.h"
  40. #include "llvm/MC/MCParser/AsmLexer.h"
  41. #include "llvm/MC/MCParser/MCAsmLexer.h"
  42. #include "llvm/MC/MCParser/MCAsmParser.h"
  43. #include "llvm/MC/MCParser/MCAsmParserExtension.h"
  44. #include "llvm/MC/MCParser/MCAsmParserUtils.h"
  45. #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
  46. #include "llvm/MC/MCParser/MCTargetAsmParser.h"
  47. #include "llvm/MC/MCRegisterInfo.h"
  48. #include "llvm/MC/MCSection.h"
  49. #include "llvm/MC/MCStreamer.h"
  50. #include "llvm/MC/MCSymbol.h"
  51. #include "llvm/MC/MCTargetOptions.h"
  52. #include "llvm/MC/MCValue.h"
  53. #include "llvm/Support/Casting.h"
  54. #include "llvm/Support/CommandLine.h"
  55. #include "llvm/Support/ErrorHandling.h"
  56. #include "llvm/Support/Format.h"
  57. #include "llvm/Support/MD5.h"
  58. #include "llvm/Support/MathExtras.h"
  59. #include "llvm/Support/MemoryBuffer.h"
  60. #include "llvm/Support/Path.h"
  61. #include "llvm/Support/SMLoc.h"
  62. #include "llvm/Support/SourceMgr.h"
  63. #include "llvm/Support/raw_ostream.h"
  64. #include <algorithm>
  65. #include <cassert>
  66. #include <cctype>
  67. #include <climits>
  68. #include <cstddef>
  69. #include <cstdint>
  70. #include <ctime>
  71. #include <deque>
  72. #include <memory>
  73. #include <sstream>
  74. #include <string>
  75. #include <tuple>
  76. #include <utility>
  77. #include <vector>
  78. using namespace llvm;
  79. extern cl::opt<unsigned> AsmMacroMaxNestingDepth;
  80. namespace {
  81. /// Helper types for tracking macro definitions.
  82. typedef std::vector<AsmToken> MCAsmMacroArgument;
  83. typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
  84. /// Helper class for storing information about an active macro instantiation.
  85. struct MacroInstantiation {
  86. /// The location of the instantiation.
  87. SMLoc InstantiationLoc;
  88. /// The buffer where parsing should resume upon instantiation completion.
  89. unsigned ExitBuffer;
  90. /// The location where parsing should resume upon instantiation completion.
  91. SMLoc ExitLoc;
  92. /// The depth of TheCondStack at the start of the instantiation.
  93. size_t CondStackDepth;
  94. };
  95. struct ParseStatementInfo {
  96. /// The parsed operands from the last parsed statement.
  97. SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands;
  98. /// The opcode from the last parsed instruction.
  99. unsigned Opcode = ~0U;
  100. /// Was there an error parsing the inline assembly?
  101. bool ParseError = false;
  102. /// The value associated with a macro exit.
  103. Optional<std::string> ExitValue;
  104. SmallVectorImpl<AsmRewrite> *AsmRewrites = nullptr;
  105. ParseStatementInfo() = delete;
  106. ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
  107. : AsmRewrites(rewrites) {}
  108. };
  109. enum FieldType {
  110. FT_INTEGRAL, // Initializer: integer expression, stored as an MCExpr.
  111. FT_REAL, // Initializer: real number, stored as an APInt.
  112. FT_STRUCT // Initializer: struct initializer, stored recursively.
  113. };
  114. struct FieldInfo;
  115. struct StructInfo {
  116. StringRef Name;
  117. bool IsUnion = false;
  118. bool Initializable = true;
  119. unsigned Alignment = 0;
  120. unsigned AlignmentSize = 0;
  121. unsigned NextOffset = 0;
  122. unsigned Size = 0;
  123. std::vector<FieldInfo> Fields;
  124. StringMap<size_t> FieldsByName;
  125. FieldInfo &addField(StringRef FieldName, FieldType FT,
  126. unsigned FieldAlignmentSize);
  127. StructInfo() = default;
  128. StructInfo(StringRef StructName, bool Union, unsigned AlignmentValue);
  129. };
  130. // FIXME: This should probably use a class hierarchy, raw pointers between the
  131. // objects, and dynamic type resolution instead of a union. On the other hand,
  132. // ownership then becomes much more complicated; the obvious thing would be to
  133. // use BumpPtrAllocator, but the lack of a destructor makes that messy.
  134. struct StructInitializer;
  135. struct IntFieldInfo {
  136. SmallVector<const MCExpr *, 1> Values;
  137. IntFieldInfo() = default;
  138. IntFieldInfo(const SmallVector<const MCExpr *, 1> &V) { Values = V; }
  139. IntFieldInfo(SmallVector<const MCExpr *, 1> &&V) { Values = V; }
  140. };
  141. struct RealFieldInfo {
  142. SmallVector<APInt, 1> AsIntValues;
  143. RealFieldInfo() = default;
  144. RealFieldInfo(const SmallVector<APInt, 1> &V) { AsIntValues = V; }
  145. RealFieldInfo(SmallVector<APInt, 1> &&V) { AsIntValues = V; }
  146. };
  147. struct StructFieldInfo {
  148. std::vector<StructInitializer> Initializers;
  149. StructInfo Structure;
  150. StructFieldInfo() = default;
  151. StructFieldInfo(std::vector<StructInitializer> V, StructInfo S);
  152. };
  153. class FieldInitializer {
  154. public:
  155. FieldType FT;
  156. union {
  157. IntFieldInfo IntInfo;
  158. RealFieldInfo RealInfo;
  159. StructFieldInfo StructInfo;
  160. };
  161. ~FieldInitializer();
  162. FieldInitializer(FieldType FT);
  163. FieldInitializer(SmallVector<const MCExpr *, 1> &&Values);
  164. FieldInitializer(SmallVector<APInt, 1> &&AsIntValues);
  165. FieldInitializer(std::vector<StructInitializer> &&Initializers,
  166. struct StructInfo Structure);
  167. FieldInitializer(const FieldInitializer &Initializer);
  168. FieldInitializer(FieldInitializer &&Initializer);
  169. FieldInitializer &operator=(const FieldInitializer &Initializer);
  170. FieldInitializer &operator=(FieldInitializer &&Initializer);
  171. };
  172. struct StructInitializer {
  173. std::vector<FieldInitializer> FieldInitializers;
  174. };
  175. struct FieldInfo {
  176. // Offset of the field within the containing STRUCT.
  177. unsigned Offset = 0;
  178. // Total size of the field (= LengthOf * Type).
  179. unsigned SizeOf = 0;
  180. // Number of elements in the field (1 if scalar, >1 if an array).
  181. unsigned LengthOf = 0;
  182. // Size of a single entry in this field, in bytes ("type" in MASM standards).
  183. unsigned Type = 0;
  184. FieldInitializer Contents;
  185. FieldInfo(FieldType FT) : Contents(FT) {}
  186. };
  187. StructFieldInfo::StructFieldInfo(std::vector<StructInitializer> V,
  188. StructInfo S) {
  189. Initializers = std::move(V);
  190. Structure = S;
  191. }
  192. StructInfo::StructInfo(StringRef StructName, bool Union,
  193. unsigned AlignmentValue)
  194. : Name(StructName), IsUnion(Union), Alignment(AlignmentValue) {}
  195. FieldInfo &StructInfo::addField(StringRef FieldName, FieldType FT,
  196. unsigned FieldAlignmentSize) {
  197. if (!FieldName.empty())
  198. FieldsByName[FieldName.lower()] = Fields.size();
  199. Fields.emplace_back(FT);
  200. FieldInfo &Field = Fields.back();
  201. Field.Offset =
  202. llvm::alignTo(NextOffset, std::min(Alignment, FieldAlignmentSize));
  203. if (!IsUnion) {
  204. NextOffset = std::max(NextOffset, Field.Offset);
  205. }
  206. AlignmentSize = std::max(AlignmentSize, FieldAlignmentSize);
  207. return Field;
  208. }
  209. FieldInitializer::~FieldInitializer() {
  210. switch (FT) {
  211. case FT_INTEGRAL:
  212. IntInfo.~IntFieldInfo();
  213. break;
  214. case FT_REAL:
  215. RealInfo.~RealFieldInfo();
  216. break;
  217. case FT_STRUCT:
  218. StructInfo.~StructFieldInfo();
  219. break;
  220. }
  221. }
  222. FieldInitializer::FieldInitializer(FieldType FT) : FT(FT) {
  223. switch (FT) {
  224. case FT_INTEGRAL:
  225. new (&IntInfo) IntFieldInfo();
  226. break;
  227. case FT_REAL:
  228. new (&RealInfo) RealFieldInfo();
  229. break;
  230. case FT_STRUCT:
  231. new (&StructInfo) StructFieldInfo();
  232. break;
  233. }
  234. }
  235. FieldInitializer::FieldInitializer(SmallVector<const MCExpr *, 1> &&Values)
  236. : FT(FT_INTEGRAL) {
  237. new (&IntInfo) IntFieldInfo(Values);
  238. }
  239. FieldInitializer::FieldInitializer(SmallVector<APInt, 1> &&AsIntValues)
  240. : FT(FT_REAL) {
  241. new (&RealInfo) RealFieldInfo(AsIntValues);
  242. }
  243. FieldInitializer::FieldInitializer(
  244. std::vector<StructInitializer> &&Initializers, struct StructInfo Structure)
  245. : FT(FT_STRUCT) {
  246. new (&StructInfo) StructFieldInfo(std::move(Initializers), Structure);
  247. }
  248. FieldInitializer::FieldInitializer(const FieldInitializer &Initializer)
  249. : FT(Initializer.FT) {
  250. switch (FT) {
  251. case FT_INTEGRAL:
  252. new (&IntInfo) IntFieldInfo(Initializer.IntInfo);
  253. break;
  254. case FT_REAL:
  255. new (&RealInfo) RealFieldInfo(Initializer.RealInfo);
  256. break;
  257. case FT_STRUCT:
  258. new (&StructInfo) StructFieldInfo(Initializer.StructInfo);
  259. break;
  260. }
  261. }
  262. FieldInitializer::FieldInitializer(FieldInitializer &&Initializer)
  263. : FT(Initializer.FT) {
  264. switch (FT) {
  265. case FT_INTEGRAL:
  266. new (&IntInfo) IntFieldInfo(Initializer.IntInfo);
  267. break;
  268. case FT_REAL:
  269. new (&RealInfo) RealFieldInfo(Initializer.RealInfo);
  270. break;
  271. case FT_STRUCT:
  272. new (&StructInfo) StructFieldInfo(Initializer.StructInfo);
  273. break;
  274. }
  275. }
  276. FieldInitializer &
  277. FieldInitializer::operator=(const FieldInitializer &Initializer) {
  278. if (FT != Initializer.FT) {
  279. switch (FT) {
  280. case FT_INTEGRAL:
  281. IntInfo.~IntFieldInfo();
  282. break;
  283. case FT_REAL:
  284. RealInfo.~RealFieldInfo();
  285. break;
  286. case FT_STRUCT:
  287. StructInfo.~StructFieldInfo();
  288. break;
  289. }
  290. }
  291. FT = Initializer.FT;
  292. switch (FT) {
  293. case FT_INTEGRAL:
  294. IntInfo = Initializer.IntInfo;
  295. break;
  296. case FT_REAL:
  297. RealInfo = Initializer.RealInfo;
  298. break;
  299. case FT_STRUCT:
  300. StructInfo = Initializer.StructInfo;
  301. break;
  302. }
  303. return *this;
  304. }
  305. FieldInitializer &FieldInitializer::operator=(FieldInitializer &&Initializer) {
  306. if (FT != Initializer.FT) {
  307. switch (FT) {
  308. case FT_INTEGRAL:
  309. IntInfo.~IntFieldInfo();
  310. break;
  311. case FT_REAL:
  312. RealInfo.~RealFieldInfo();
  313. break;
  314. case FT_STRUCT:
  315. StructInfo.~StructFieldInfo();
  316. break;
  317. }
  318. }
  319. FT = Initializer.FT;
  320. switch (FT) {
  321. case FT_INTEGRAL:
  322. IntInfo = Initializer.IntInfo;
  323. break;
  324. case FT_REAL:
  325. RealInfo = Initializer.RealInfo;
  326. break;
  327. case FT_STRUCT:
  328. StructInfo = Initializer.StructInfo;
  329. break;
  330. }
  331. return *this;
  332. }
  333. /// The concrete assembly parser instance.
  334. // Note that this is a full MCAsmParser, not an MCAsmParserExtension!
  335. // It's a peer of AsmParser, not of COFFAsmParser, WasmAsmParser, etc.
  336. class MasmParser : public MCAsmParser {
  337. private:
  338. AsmLexer Lexer;
  339. MCContext &Ctx;
  340. MCStreamer &Out;
  341. const MCAsmInfo &MAI;
  342. SourceMgr &SrcMgr;
  343. SourceMgr::DiagHandlerTy SavedDiagHandler;
  344. void *SavedDiagContext;
  345. std::unique_ptr<MCAsmParserExtension> PlatformParser;
  346. /// This is the current buffer index we're lexing from as managed by the
  347. /// SourceMgr object.
  348. unsigned CurBuffer;
  349. /// time of assembly
  350. struct tm TM;
  351. BitVector EndStatementAtEOFStack;
  352. AsmCond TheCondState;
  353. std::vector<AsmCond> TheCondStack;
  354. /// maps directive names to handler methods in parser
  355. /// extensions. Extensions register themselves in this map by calling
  356. /// addDirectiveHandler.
  357. StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
  358. /// maps assembly-time variable names to variables.
  359. struct Variable {
  360. enum RedefinableKind { NOT_REDEFINABLE, WARN_ON_REDEFINITION, REDEFINABLE };
  361. StringRef Name;
  362. RedefinableKind Redefinable = REDEFINABLE;
  363. bool IsText = false;
  364. std::string TextValue;
  365. };
  366. StringMap<Variable> Variables;
  367. /// Stack of active struct definitions.
  368. SmallVector<StructInfo, 1> StructInProgress;
  369. /// Maps struct tags to struct definitions.
  370. StringMap<StructInfo> Structs;
  371. /// Maps data location names to types.
  372. StringMap<AsmTypeInfo> KnownType;
  373. /// Stack of active macro instantiations.
  374. std::vector<MacroInstantiation*> ActiveMacros;
  375. /// List of bodies of anonymous macros.
  376. std::deque<MCAsmMacro> MacroLikeBodies;
  377. /// Keeps track of how many .macro's have been instantiated.
  378. unsigned NumOfMacroInstantiations;
  379. /// The values from the last parsed cpp hash file line comment if any.
  380. struct CppHashInfoTy {
  381. StringRef Filename;
  382. int64_t LineNumber;
  383. SMLoc Loc;
  384. unsigned Buf;
  385. CppHashInfoTy() : LineNumber(0), Buf(0) {}
  386. };
  387. CppHashInfoTy CppHashInfo;
  388. /// The filename from the first cpp hash file line comment, if any.
  389. StringRef FirstCppHashFilename;
  390. /// List of forward directional labels for diagnosis at the end.
  391. SmallVector<std::tuple<SMLoc, CppHashInfoTy, MCSymbol *>, 4> DirLabels;
  392. /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
  393. /// Defaults to 1U, meaning Intel.
  394. unsigned AssemblerDialect = 1U;
  395. /// is Darwin compatibility enabled?
  396. bool IsDarwin = false;
  397. /// Are we parsing ms-style inline assembly?
  398. bool ParsingMSInlineAsm = false;
  399. /// Did we already inform the user about inconsistent MD5 usage?
  400. bool ReportedInconsistentMD5 = false;
  401. // Current <...> expression depth.
  402. unsigned AngleBracketDepth = 0U;
  403. // Number of locals defined.
  404. uint16_t LocalCounter = 0;
  405. public:
  406. MasmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
  407. const MCAsmInfo &MAI, struct tm TM, unsigned CB = 0);
  408. MasmParser(const MasmParser &) = delete;
  409. MasmParser &operator=(const MasmParser &) = delete;
  410. ~MasmParser() override;
  411. bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
  412. void addDirectiveHandler(StringRef Directive,
  413. ExtensionDirectiveHandler Handler) override {
  414. ExtensionDirectiveMap[Directive] = Handler;
  415. if (DirectiveKindMap.find(Directive) == DirectiveKindMap.end()) {
  416. DirectiveKindMap[Directive] = DK_HANDLER_DIRECTIVE;
  417. }
  418. }
  419. void addAliasForDirective(StringRef Directive, StringRef Alias) override {
  420. DirectiveKindMap[Directive] = DirectiveKindMap[Alias];
  421. }
  422. /// @name MCAsmParser Interface
  423. /// {
  424. SourceMgr &getSourceManager() override { return SrcMgr; }
  425. MCAsmLexer &getLexer() override { return Lexer; }
  426. MCContext &getContext() override { return Ctx; }
  427. MCStreamer &getStreamer() override { return Out; }
  428. CodeViewContext &getCVContext() { return Ctx.getCVContext(); }
  429. unsigned getAssemblerDialect() override {
  430. if (AssemblerDialect == ~0U)
  431. return MAI.getAssemblerDialect();
  432. else
  433. return AssemblerDialect;
  434. }
  435. void setAssemblerDialect(unsigned i) override {
  436. AssemblerDialect = i;
  437. }
  438. void Note(SMLoc L, const Twine &Msg, SMRange Range = None) override;
  439. bool Warning(SMLoc L, const Twine &Msg, SMRange Range = None) override;
  440. bool printError(SMLoc L, const Twine &Msg, SMRange Range = None) override;
  441. enum ExpandKind { ExpandMacros, DoNotExpandMacros };
  442. const AsmToken &Lex(ExpandKind ExpandNextToken);
  443. const AsmToken &Lex() override { return Lex(ExpandMacros); }
  444. void setParsingMSInlineAsm(bool V) override {
  445. ParsingMSInlineAsm = V;
  446. // When parsing MS inline asm, we must lex 0b1101 and 0ABCH as binary and
  447. // hex integer literals.
  448. Lexer.setLexMasmIntegers(V);
  449. }
  450. bool isParsingMSInlineAsm() override { return ParsingMSInlineAsm; }
  451. bool isParsingMasm() const override { return true; }
  452. bool defineMacro(StringRef Name, StringRef Value) override;
  453. bool lookUpField(StringRef Name, AsmFieldInfo &Info) const override;
  454. bool lookUpField(StringRef Base, StringRef Member,
  455. AsmFieldInfo &Info) const override;
  456. bool lookUpType(StringRef Name, AsmTypeInfo &Info) const override;
  457. bool parseMSInlineAsm(std::string &AsmString, unsigned &NumOutputs,
  458. unsigned &NumInputs,
  459. SmallVectorImpl<std::pair<void *, bool>> &OpDecls,
  460. SmallVectorImpl<std::string> &Constraints,
  461. SmallVectorImpl<std::string> &Clobbers,
  462. const MCInstrInfo *MII, const MCInstPrinter *IP,
  463. MCAsmParserSemaCallback &SI) override;
  464. bool parseExpression(const MCExpr *&Res);
  465. bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
  466. bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
  467. AsmTypeInfo *TypeInfo) override;
  468. bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
  469. bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
  470. SMLoc &EndLoc) override;
  471. bool parseAbsoluteExpression(int64_t &Res) override;
  472. /// Parse a floating point expression using the float \p Semantics
  473. /// and set \p Res to the value.
  474. bool parseRealValue(const fltSemantics &Semantics, APInt &Res);
  475. /// Parse an identifier or string (as a quoted identifier)
  476. /// and set \p Res to the identifier contents.
  477. enum IdentifierPositionKind { StandardPosition, StartOfStatement };
  478. bool parseIdentifier(StringRef &Res, IdentifierPositionKind Position);
  479. bool parseIdentifier(StringRef &Res) override {
  480. return parseIdentifier(Res, StandardPosition);
  481. }
  482. void eatToEndOfStatement() override;
  483. bool checkForValidSection() override;
  484. /// }
  485. private:
  486. bool expandMacros();
  487. const AsmToken peekTok(bool ShouldSkipSpace = true);
  488. bool parseStatement(ParseStatementInfo &Info,
  489. MCAsmParserSemaCallback *SI);
  490. bool parseCurlyBlockScope(SmallVectorImpl<AsmRewrite>& AsmStrRewrites);
  491. bool parseCppHashLineFilenameComment(SMLoc L);
  492. bool expandMacro(raw_svector_ostream &OS, StringRef Body,
  493. ArrayRef<MCAsmMacroParameter> Parameters,
  494. ArrayRef<MCAsmMacroArgument> A,
  495. const std::vector<std::string> &Locals, SMLoc L);
  496. /// Are we inside a macro instantiation?
  497. bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
  498. /// Handle entry to macro instantiation.
  499. ///
  500. /// \param M The macro.
  501. /// \param NameLoc Instantiation location.
  502. bool handleMacroEntry(
  503. const MCAsmMacro *M, SMLoc NameLoc,
  504. AsmToken::TokenKind ArgumentEndTok = AsmToken::EndOfStatement);
  505. /// Handle invocation of macro function.
  506. ///
  507. /// \param M The macro.
  508. /// \param NameLoc Invocation location.
  509. bool handleMacroInvocation(const MCAsmMacro *M, SMLoc NameLoc);
  510. /// Handle exit from macro instantiation.
  511. void handleMacroExit();
  512. /// Extract AsmTokens for a macro argument.
  513. bool
  514. parseMacroArgument(const MCAsmMacroParameter *MP, MCAsmMacroArgument &MA,
  515. AsmToken::TokenKind EndTok = AsmToken::EndOfStatement);
  516. /// Parse all macro arguments for a given macro.
  517. bool
  518. parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A,
  519. AsmToken::TokenKind EndTok = AsmToken::EndOfStatement);
  520. void printMacroInstantiations();
  521. bool expandStatement(SMLoc Loc);
  522. void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
  523. SMRange Range = None) const {
  524. ArrayRef<SMRange> Ranges(Range);
  525. SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
  526. }
  527. static void DiagHandler(const SMDiagnostic &Diag, void *Context);
  528. bool lookUpField(const StructInfo &Structure, StringRef Member,
  529. AsmFieldInfo &Info) const;
  530. /// Should we emit DWARF describing this assembler source? (Returns false if
  531. /// the source has .file directives, which means we don't want to generate
  532. /// info describing the assembler source itself.)
  533. bool enabledGenDwarfForAssembly();
  534. /// Enter the specified file. This returns true on failure.
  535. bool enterIncludeFile(const std::string &Filename);
  536. /// Reset the current lexer position to that given by \p Loc. The
  537. /// current token is not set; clients should ensure Lex() is called
  538. /// subsequently.
  539. ///
  540. /// \param InBuffer If not 0, should be the known buffer id that contains the
  541. /// location.
  542. void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0,
  543. bool EndStatementAtEOF = true);
  544. /// Parse up to a token of kind \p EndTok and return the contents from the
  545. /// current token up to (but not including) this token; the current token on
  546. /// exit will be either this kind or EOF. Reads through instantiated macro
  547. /// functions and text macros.
  548. SmallVector<StringRef, 1> parseStringRefsTo(AsmToken::TokenKind EndTok);
  549. std::string parseStringTo(AsmToken::TokenKind EndTok);
  550. /// Parse up to the end of statement and return the contents from the current
  551. /// token until the end of the statement; the current token on exit will be
  552. /// either the EndOfStatement or EOF.
  553. StringRef parseStringToEndOfStatement() override;
  554. bool parseTextItem(std::string &Data);
  555. unsigned getBinOpPrecedence(AsmToken::TokenKind K,
  556. MCBinaryExpr::Opcode &Kind);
  557. bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
  558. bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
  559. bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
  560. bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
  561. bool parseCVFunctionId(int64_t &FunctionId, StringRef DirectiveName);
  562. bool parseCVFileId(int64_t &FileId, StringRef DirectiveName);
  563. // Generic (target and platform independent) directive parsing.
  564. enum DirectiveKind {
  565. DK_NO_DIRECTIVE, // Placeholder
  566. DK_HANDLER_DIRECTIVE,
  567. DK_ASSIGN,
  568. DK_EQU,
  569. DK_TEXTEQU,
  570. DK_ASCII,
  571. DK_ASCIZ,
  572. DK_STRING,
  573. DK_BYTE,
  574. DK_SBYTE,
  575. DK_WORD,
  576. DK_SWORD,
  577. DK_DWORD,
  578. DK_SDWORD,
  579. DK_FWORD,
  580. DK_QWORD,
  581. DK_SQWORD,
  582. DK_DB,
  583. DK_DD,
  584. DK_DF,
  585. DK_DQ,
  586. DK_DW,
  587. DK_REAL4,
  588. DK_REAL8,
  589. DK_REAL10,
  590. DK_ALIGN,
  591. DK_EVEN,
  592. DK_ORG,
  593. DK_ENDR,
  594. DK_EXTERN,
  595. DK_PUBLIC,
  596. DK_COMM,
  597. DK_COMMENT,
  598. DK_INCLUDE,
  599. DK_REPEAT,
  600. DK_WHILE,
  601. DK_FOR,
  602. DK_FORC,
  603. DK_IF,
  604. DK_IFE,
  605. DK_IFB,
  606. DK_IFNB,
  607. DK_IFDEF,
  608. DK_IFNDEF,
  609. DK_IFDIF,
  610. DK_IFDIFI,
  611. DK_IFIDN,
  612. DK_IFIDNI,
  613. DK_ELSEIF,
  614. DK_ELSEIFE,
  615. DK_ELSEIFB,
  616. DK_ELSEIFNB,
  617. DK_ELSEIFDEF,
  618. DK_ELSEIFNDEF,
  619. DK_ELSEIFDIF,
  620. DK_ELSEIFDIFI,
  621. DK_ELSEIFIDN,
  622. DK_ELSEIFIDNI,
  623. DK_ELSE,
  624. DK_ENDIF,
  625. DK_FILE,
  626. DK_LINE,
  627. DK_LOC,
  628. DK_STABS,
  629. DK_CV_FILE,
  630. DK_CV_FUNC_ID,
  631. DK_CV_INLINE_SITE_ID,
  632. DK_CV_LOC,
  633. DK_CV_LINETABLE,
  634. DK_CV_INLINE_LINETABLE,
  635. DK_CV_DEF_RANGE,
  636. DK_CV_STRINGTABLE,
  637. DK_CV_STRING,
  638. DK_CV_FILECHECKSUMS,
  639. DK_CV_FILECHECKSUM_OFFSET,
  640. DK_CV_FPO_DATA,
  641. DK_CFI_SECTIONS,
  642. DK_CFI_STARTPROC,
  643. DK_CFI_ENDPROC,
  644. DK_CFI_DEF_CFA,
  645. DK_CFI_DEF_CFA_OFFSET,
  646. DK_CFI_ADJUST_CFA_OFFSET,
  647. DK_CFI_DEF_CFA_REGISTER,
  648. DK_CFI_OFFSET,
  649. DK_CFI_REL_OFFSET,
  650. DK_CFI_PERSONALITY,
  651. DK_CFI_LSDA,
  652. DK_CFI_REMEMBER_STATE,
  653. DK_CFI_RESTORE_STATE,
  654. DK_CFI_SAME_VALUE,
  655. DK_CFI_RESTORE,
  656. DK_CFI_ESCAPE,
  657. DK_CFI_RETURN_COLUMN,
  658. DK_CFI_SIGNAL_FRAME,
  659. DK_CFI_UNDEFINED,
  660. DK_CFI_REGISTER,
  661. DK_CFI_WINDOW_SAVE,
  662. DK_CFI_B_KEY_FRAME,
  663. DK_MACRO,
  664. DK_EXITM,
  665. DK_ENDM,
  666. DK_PURGE,
  667. DK_ERR,
  668. DK_ERRB,
  669. DK_ERRNB,
  670. DK_ERRDEF,
  671. DK_ERRNDEF,
  672. DK_ERRDIF,
  673. DK_ERRDIFI,
  674. DK_ERRIDN,
  675. DK_ERRIDNI,
  676. DK_ERRE,
  677. DK_ERRNZ,
  678. DK_ECHO,
  679. DK_STRUCT,
  680. DK_UNION,
  681. DK_ENDS,
  682. DK_END,
  683. DK_PUSHFRAME,
  684. DK_PUSHREG,
  685. DK_SAVEREG,
  686. DK_SAVEXMM128,
  687. DK_SETFRAME,
  688. DK_RADIX,
  689. };
  690. /// Maps directive name --> DirectiveKind enum, for directives parsed by this
  691. /// class.
  692. StringMap<DirectiveKind> DirectiveKindMap;
  693. bool isMacroLikeDirective();
  694. // Codeview def_range type parsing.
  695. enum CVDefRangeType {
  696. CVDR_DEFRANGE = 0, // Placeholder
  697. CVDR_DEFRANGE_REGISTER,
  698. CVDR_DEFRANGE_FRAMEPOINTER_REL,
  699. CVDR_DEFRANGE_SUBFIELD_REGISTER,
  700. CVDR_DEFRANGE_REGISTER_REL
  701. };
  702. /// Maps Codeview def_range types --> CVDefRangeType enum, for Codeview
  703. /// def_range types parsed by this class.
  704. StringMap<CVDefRangeType> CVDefRangeTypeMap;
  705. // Generic (target and platform independent) directive parsing.
  706. enum BuiltinSymbol {
  707. BI_NO_SYMBOL, // Placeholder
  708. BI_DATE,
  709. BI_TIME,
  710. BI_VERSION,
  711. BI_FILECUR,
  712. BI_FILENAME,
  713. BI_LINE,
  714. BI_CURSEG,
  715. BI_CPU,
  716. BI_INTERFACE,
  717. BI_CODE,
  718. BI_DATA,
  719. BI_FARDATA,
  720. BI_WORDSIZE,
  721. BI_CODESIZE,
  722. BI_DATASIZE,
  723. BI_MODEL,
  724. BI_STACK,
  725. };
  726. /// Maps builtin name --> BuiltinSymbol enum, for builtins handled by this
  727. /// class.
  728. StringMap<BuiltinSymbol> BuiltinSymbolMap;
  729. const MCExpr *evaluateBuiltinValue(BuiltinSymbol Symbol, SMLoc StartLoc);
  730. llvm::Optional<std::string> evaluateBuiltinTextMacro(BuiltinSymbol Symbol,
  731. SMLoc StartLoc);
  732. // ".ascii", ".asciz", ".string"
  733. bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
  734. // "byte", "word", ...
  735. bool emitIntValue(const MCExpr *Value, unsigned Size);
  736. bool parseScalarInitializer(unsigned Size,
  737. SmallVectorImpl<const MCExpr *> &Values,
  738. unsigned StringPadLength = 0);
  739. bool parseScalarInstList(
  740. unsigned Size, SmallVectorImpl<const MCExpr *> &Values,
  741. const AsmToken::TokenKind EndToken = AsmToken::EndOfStatement);
  742. bool emitIntegralValues(unsigned Size, unsigned *Count = nullptr);
  743. bool addIntegralField(StringRef Name, unsigned Size);
  744. bool parseDirectiveValue(StringRef IDVal, unsigned Size);
  745. bool parseDirectiveNamedValue(StringRef TypeName, unsigned Size,
  746. StringRef Name, SMLoc NameLoc);
  747. // "real4", "real8", "real10"
  748. bool emitRealValues(const fltSemantics &Semantics, unsigned *Count = nullptr);
  749. bool addRealField(StringRef Name, const fltSemantics &Semantics, size_t Size);
  750. bool parseDirectiveRealValue(StringRef IDVal, const fltSemantics &Semantics,
  751. size_t Size);
  752. bool parseRealInstList(
  753. const fltSemantics &Semantics, SmallVectorImpl<APInt> &Values,
  754. const AsmToken::TokenKind EndToken = AsmToken::EndOfStatement);
  755. bool parseDirectiveNamedRealValue(StringRef TypeName,
  756. const fltSemantics &Semantics,
  757. unsigned Size, StringRef Name,
  758. SMLoc NameLoc);
  759. bool parseOptionalAngleBracketOpen();
  760. bool parseAngleBracketClose(const Twine &Msg = "expected '>'");
  761. bool parseFieldInitializer(const FieldInfo &Field,
  762. FieldInitializer &Initializer);
  763. bool parseFieldInitializer(const FieldInfo &Field,
  764. const IntFieldInfo &Contents,
  765. FieldInitializer &Initializer);
  766. bool parseFieldInitializer(const FieldInfo &Field,
  767. const RealFieldInfo &Contents,
  768. FieldInitializer &Initializer);
  769. bool parseFieldInitializer(const FieldInfo &Field,
  770. const StructFieldInfo &Contents,
  771. FieldInitializer &Initializer);
  772. bool parseStructInitializer(const StructInfo &Structure,
  773. StructInitializer &Initializer);
  774. bool parseStructInstList(
  775. const StructInfo &Structure, std::vector<StructInitializer> &Initializers,
  776. const AsmToken::TokenKind EndToken = AsmToken::EndOfStatement);
  777. bool emitFieldValue(const FieldInfo &Field);
  778. bool emitFieldValue(const FieldInfo &Field, const IntFieldInfo &Contents);
  779. bool emitFieldValue(const FieldInfo &Field, const RealFieldInfo &Contents);
  780. bool emitFieldValue(const FieldInfo &Field, const StructFieldInfo &Contents);
  781. bool emitFieldInitializer(const FieldInfo &Field,
  782. const FieldInitializer &Initializer);
  783. bool emitFieldInitializer(const FieldInfo &Field,
  784. const IntFieldInfo &Contents,
  785. const IntFieldInfo &Initializer);
  786. bool emitFieldInitializer(const FieldInfo &Field,
  787. const RealFieldInfo &Contents,
  788. const RealFieldInfo &Initializer);
  789. bool emitFieldInitializer(const FieldInfo &Field,
  790. const StructFieldInfo &Contents,
  791. const StructFieldInfo &Initializer);
  792. bool emitStructInitializer(const StructInfo &Structure,
  793. const StructInitializer &Initializer);
  794. // User-defined types (structs, unions):
  795. bool emitStructValues(const StructInfo &Structure, unsigned *Count = nullptr);
  796. bool addStructField(StringRef Name, const StructInfo &Structure);
  797. bool parseDirectiveStructValue(const StructInfo &Structure,
  798. StringRef Directive, SMLoc DirLoc);
  799. bool parseDirectiveNamedStructValue(const StructInfo &Structure,
  800. StringRef Directive, SMLoc DirLoc,
  801. StringRef Name);
  802. // "=", "equ", "textequ"
  803. bool parseDirectiveEquate(StringRef IDVal, StringRef Name,
  804. DirectiveKind DirKind, SMLoc NameLoc);
  805. bool parseDirectiveOrg(); // "org"
  806. bool emitAlignTo(int64_t Alignment);
  807. bool parseDirectiveAlign(); // "align"
  808. bool parseDirectiveEven(); // "even"
  809. // ".file", ".line", ".loc", ".stabs"
  810. bool parseDirectiveFile(SMLoc DirectiveLoc);
  811. bool parseDirectiveLine();
  812. bool parseDirectiveLoc();
  813. bool parseDirectiveStabs();
  814. // ".cv_file", ".cv_func_id", ".cv_inline_site_id", ".cv_loc", ".cv_linetable",
  815. // ".cv_inline_linetable", ".cv_def_range", ".cv_string"
  816. bool parseDirectiveCVFile();
  817. bool parseDirectiveCVFuncId();
  818. bool parseDirectiveCVInlineSiteId();
  819. bool parseDirectiveCVLoc();
  820. bool parseDirectiveCVLinetable();
  821. bool parseDirectiveCVInlineLinetable();
  822. bool parseDirectiveCVDefRange();
  823. bool parseDirectiveCVString();
  824. bool parseDirectiveCVStringTable();
  825. bool parseDirectiveCVFileChecksums();
  826. bool parseDirectiveCVFileChecksumOffset();
  827. bool parseDirectiveCVFPOData();
  828. // .cfi directives
  829. bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
  830. bool parseDirectiveCFIWindowSave();
  831. bool parseDirectiveCFISections();
  832. bool parseDirectiveCFIStartProc();
  833. bool parseDirectiveCFIEndProc();
  834. bool parseDirectiveCFIDefCfaOffset();
  835. bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
  836. bool parseDirectiveCFIAdjustCfaOffset();
  837. bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
  838. bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
  839. bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
  840. bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
  841. bool parseDirectiveCFIRememberState();
  842. bool parseDirectiveCFIRestoreState();
  843. bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
  844. bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
  845. bool parseDirectiveCFIEscape();
  846. bool parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc);
  847. bool parseDirectiveCFISignalFrame();
  848. bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
  849. // macro directives
  850. bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
  851. bool parseDirectiveExitMacro(SMLoc DirectiveLoc, StringRef Directive,
  852. std::string &Value);
  853. bool parseDirectiveEndMacro(StringRef Directive);
  854. bool parseDirectiveMacro(StringRef Name, SMLoc NameLoc);
  855. bool parseDirectiveStruct(StringRef Directive, DirectiveKind DirKind,
  856. StringRef Name, SMLoc NameLoc);
  857. bool parseDirectiveNestedStruct(StringRef Directive, DirectiveKind DirKind);
  858. bool parseDirectiveEnds(StringRef Name, SMLoc NameLoc);
  859. bool parseDirectiveNestedEnds();
  860. /// Parse a directive like ".globl" which accepts a single symbol (which
  861. /// should be a label or an external).
  862. bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
  863. bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
  864. bool parseDirectiveComment(SMLoc DirectiveLoc); // "comment"
  865. bool parseDirectiveInclude(); // "include"
  866. // "if" or "ife"
  867. bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
  868. // "ifb" or "ifnb", depending on ExpectBlank.
  869. bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
  870. // "ifidn", "ifdif", "ifidni", or "ifdifi", depending on ExpectEqual and
  871. // CaseInsensitive.
  872. bool parseDirectiveIfidn(SMLoc DirectiveLoc, bool ExpectEqual,
  873. bool CaseInsensitive);
  874. // "ifdef" or "ifndef", depending on expect_defined
  875. bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
  876. // "elseif" or "elseife"
  877. bool parseDirectiveElseIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
  878. // "elseifb" or "elseifnb", depending on ExpectBlank.
  879. bool parseDirectiveElseIfb(SMLoc DirectiveLoc, bool ExpectBlank);
  880. // ".elseifdef" or ".elseifndef", depending on expect_defined
  881. bool parseDirectiveElseIfdef(SMLoc DirectiveLoc, bool expect_defined);
  882. // "elseifidn", "elseifdif", "elseifidni", or "elseifdifi", depending on
  883. // ExpectEqual and CaseInsensitive.
  884. bool parseDirectiveElseIfidn(SMLoc DirectiveLoc, bool ExpectEqual,
  885. bool CaseInsensitive);
  886. bool parseDirectiveElse(SMLoc DirectiveLoc); // "else"
  887. bool parseDirectiveEndIf(SMLoc DirectiveLoc); // "endif"
  888. bool parseEscapedString(std::string &Data) override;
  889. bool parseAngleBracketString(std::string &Data) override;
  890. // Macro-like directives
  891. MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
  892. void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
  893. raw_svector_ostream &OS);
  894. void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
  895. SMLoc ExitLoc, raw_svector_ostream &OS);
  896. bool parseDirectiveRepeat(SMLoc DirectiveLoc, StringRef Directive);
  897. bool parseDirectiveFor(SMLoc DirectiveLoc, StringRef Directive);
  898. bool parseDirectiveForc(SMLoc DirectiveLoc, StringRef Directive);
  899. bool parseDirectiveWhile(SMLoc DirectiveLoc);
  900. // "_emit" or "__emit"
  901. bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
  902. size_t Len);
  903. // "align"
  904. bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
  905. // "end"
  906. bool parseDirectiveEnd(SMLoc DirectiveLoc);
  907. // ".err"
  908. bool parseDirectiveError(SMLoc DirectiveLoc);
  909. // ".errb" or ".errnb", depending on ExpectBlank.
  910. bool parseDirectiveErrorIfb(SMLoc DirectiveLoc, bool ExpectBlank);
  911. // ".errdef" or ".errndef", depending on ExpectBlank.
  912. bool parseDirectiveErrorIfdef(SMLoc DirectiveLoc, bool ExpectDefined);
  913. // ".erridn", ".errdif", ".erridni", or ".errdifi", depending on ExpectEqual
  914. // and CaseInsensitive.
  915. bool parseDirectiveErrorIfidn(SMLoc DirectiveLoc, bool ExpectEqual,
  916. bool CaseInsensitive);
  917. // ".erre" or ".errnz", depending on ExpectZero.
  918. bool parseDirectiveErrorIfe(SMLoc DirectiveLoc, bool ExpectZero);
  919. // ".radix"
  920. bool parseDirectiveRadix(SMLoc DirectiveLoc);
  921. // "echo"
  922. bool parseDirectiveEcho(SMLoc DirectiveLoc);
  923. void initializeDirectiveKindMap();
  924. void initializeCVDefRangeTypeMap();
  925. void initializeBuiltinSymbolMap();
  926. };
  927. } // end anonymous namespace
  928. namespace llvm {
  929. extern MCAsmParserExtension *createCOFFMasmParser();
  930. } // end namespace llvm
  931. enum { DEFAULT_ADDRSPACE = 0 };
  932. MasmParser::MasmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
  933. const MCAsmInfo &MAI, struct tm TM, unsigned CB)
  934. : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM),
  935. CurBuffer(CB ? CB : SM.getMainFileID()), TM(TM) {
  936. HadError = false;
  937. // Save the old handler.
  938. SavedDiagHandler = SrcMgr.getDiagHandler();
  939. SavedDiagContext = SrcMgr.getDiagContext();
  940. // Set our own handler which calls the saved handler.
  941. SrcMgr.setDiagHandler(DiagHandler, this);
  942. Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
  943. EndStatementAtEOFStack.push_back(true);
  944. // Initialize the platform / file format parser.
  945. switch (Ctx.getObjectFileType()) {
  946. case MCContext::IsCOFF:
  947. PlatformParser.reset(createCOFFMasmParser());
  948. break;
  949. default:
  950. report_fatal_error("llvm-ml currently supports only COFF output.");
  951. break;
  952. }
  953. initializeDirectiveKindMap();
  954. PlatformParser->Initialize(*this);
  955. initializeCVDefRangeTypeMap();
  956. initializeBuiltinSymbolMap();
  957. NumOfMacroInstantiations = 0;
  958. }
  959. MasmParser::~MasmParser() {
  960. assert((HadError || ActiveMacros.empty()) &&
  961. "Unexpected active macro instantiation!");
  962. // Restore the saved diagnostics handler and context for use during
  963. // finalization.
  964. SrcMgr.setDiagHandler(SavedDiagHandler, SavedDiagContext);
  965. }
  966. void MasmParser::printMacroInstantiations() {
  967. // Print the active macro instantiation stack.
  968. for (std::vector<MacroInstantiation *>::const_reverse_iterator
  969. it = ActiveMacros.rbegin(),
  970. ie = ActiveMacros.rend();
  971. it != ie; ++it)
  972. printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
  973. "while in macro instantiation");
  974. }
  975. void MasmParser::Note(SMLoc L, const Twine &Msg, SMRange Range) {
  976. printPendingErrors();
  977. printMessage(L, SourceMgr::DK_Note, Msg, Range);
  978. printMacroInstantiations();
  979. }
  980. bool MasmParser::Warning(SMLoc L, const Twine &Msg, SMRange Range) {
  981. if (getTargetParser().getTargetOptions().MCNoWarn)
  982. return false;
  983. if (getTargetParser().getTargetOptions().MCFatalWarnings)
  984. return Error(L, Msg, Range);
  985. printMessage(L, SourceMgr::DK_Warning, Msg, Range);
  986. printMacroInstantiations();
  987. return false;
  988. }
  989. bool MasmParser::printError(SMLoc L, const Twine &Msg, SMRange Range) {
  990. HadError = true;
  991. printMessage(L, SourceMgr::DK_Error, Msg, Range);
  992. printMacroInstantiations();
  993. return true;
  994. }
  995. bool MasmParser::enterIncludeFile(const std::string &Filename) {
  996. std::string IncludedFile;
  997. unsigned NewBuf =
  998. SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
  999. if (!NewBuf)
  1000. return true;
  1001. CurBuffer = NewBuf;
  1002. Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
  1003. EndStatementAtEOFStack.push_back(true);
  1004. return false;
  1005. }
  1006. void MasmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer,
  1007. bool EndStatementAtEOF) {
  1008. CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
  1009. Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
  1010. Loc.getPointer(), EndStatementAtEOF);
  1011. }
  1012. bool MasmParser::expandMacros() {
  1013. const AsmToken &Tok = getTok();
  1014. const std::string IDLower = Tok.getIdentifier().lower();
  1015. const llvm::MCAsmMacro *M = getContext().lookupMacro(IDLower);
  1016. if (M && M->IsFunction && peekTok().is(AsmToken::LParen)) {
  1017. // This is a macro function invocation; expand it in place.
  1018. const SMLoc MacroLoc = Tok.getLoc();
  1019. const StringRef MacroId = Tok.getIdentifier();
  1020. Lexer.Lex();
  1021. if (handleMacroInvocation(M, MacroLoc)) {
  1022. Lexer.UnLex(AsmToken(AsmToken::Error, MacroId));
  1023. Lexer.Lex();
  1024. }
  1025. return false;
  1026. }
  1027. llvm::Optional<std::string> ExpandedValue;
  1028. auto BuiltinIt = BuiltinSymbolMap.find(IDLower);
  1029. if (BuiltinIt != BuiltinSymbolMap.end()) {
  1030. ExpandedValue =
  1031. evaluateBuiltinTextMacro(BuiltinIt->getValue(), Tok.getLoc());
  1032. } else {
  1033. auto VarIt = Variables.find(IDLower);
  1034. if (VarIt != Variables.end() && VarIt->getValue().IsText) {
  1035. ExpandedValue = VarIt->getValue().TextValue;
  1036. }
  1037. }
  1038. if (!ExpandedValue.hasValue())
  1039. return true;
  1040. std::unique_ptr<MemoryBuffer> Instantiation =
  1041. MemoryBuffer::getMemBufferCopy(*ExpandedValue, "<instantiation>");
  1042. // Jump to the macro instantiation and prime the lexer.
  1043. CurBuffer =
  1044. SrcMgr.AddNewSourceBuffer(std::move(Instantiation), Tok.getEndLoc());
  1045. Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(), nullptr,
  1046. /*EndStatementAtEOF=*/false);
  1047. EndStatementAtEOFStack.push_back(false);
  1048. Lexer.Lex();
  1049. return false;
  1050. }
  1051. const AsmToken &MasmParser::Lex(ExpandKind ExpandNextToken) {
  1052. if (Lexer.getTok().is(AsmToken::Error))
  1053. Error(Lexer.getErrLoc(), Lexer.getErr());
  1054. // if it's a end of statement with a comment in it
  1055. if (getTok().is(AsmToken::EndOfStatement)) {
  1056. // if this is a line comment output it.
  1057. if (!getTok().getString().empty() && getTok().getString().front() != '\n' &&
  1058. getTok().getString().front() != '\r' && MAI.preserveAsmComments())
  1059. Out.addExplicitComment(Twine(getTok().getString()));
  1060. }
  1061. const AsmToken *tok = &Lexer.Lex();
  1062. bool StartOfStatement = Lexer.isAtStartOfStatement();
  1063. while (ExpandNextToken == ExpandMacros && tok->is(AsmToken::Identifier)) {
  1064. if (StartOfStatement) {
  1065. AsmToken NextTok;
  1066. MutableArrayRef<AsmToken> Buf(NextTok);
  1067. size_t ReadCount = Lexer.peekTokens(Buf);
  1068. if (ReadCount && NextTok.is(AsmToken::Identifier) &&
  1069. (NextTok.getString().equals_insensitive("equ") ||
  1070. NextTok.getString().equals_insensitive("textequ"))) {
  1071. // This looks like an EQU or TEXTEQU directive; don't expand the
  1072. // identifier, allowing for redefinitions.
  1073. break;
  1074. }
  1075. }
  1076. if (expandMacros())
  1077. break;
  1078. }
  1079. // Parse comments here to be deferred until end of next statement.
  1080. while (tok->is(AsmToken::Comment)) {
  1081. if (MAI.preserveAsmComments())
  1082. Out.addExplicitComment(Twine(tok->getString()));
  1083. tok = &Lexer.Lex();
  1084. }
  1085. // Recognize and bypass line continuations.
  1086. while (tok->is(AsmToken::BackSlash) &&
  1087. peekTok().is(AsmToken::EndOfStatement)) {
  1088. // Eat both the backslash and the end of statement.
  1089. Lexer.Lex();
  1090. tok = &Lexer.Lex();
  1091. }
  1092. if (tok->is(AsmToken::Eof)) {
  1093. // If this is the end of an included file, pop the parent file off the
  1094. // include stack.
  1095. SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
  1096. if (ParentIncludeLoc != SMLoc()) {
  1097. EndStatementAtEOFStack.pop_back();
  1098. jumpToLoc(ParentIncludeLoc, 0, EndStatementAtEOFStack.back());
  1099. return Lex();
  1100. }
  1101. EndStatementAtEOFStack.pop_back();
  1102. assert(EndStatementAtEOFStack.empty());
  1103. }
  1104. return *tok;
  1105. }
  1106. const AsmToken MasmParser::peekTok(bool ShouldSkipSpace) {
  1107. AsmToken Tok;
  1108. MutableArrayRef<AsmToken> Buf(Tok);
  1109. size_t ReadCount = Lexer.peekTokens(Buf, ShouldSkipSpace);
  1110. if (ReadCount == 0) {
  1111. // If this is the end of an included file, pop the parent file off the
  1112. // include stack.
  1113. SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
  1114. if (ParentIncludeLoc != SMLoc()) {
  1115. EndStatementAtEOFStack.pop_back();
  1116. jumpToLoc(ParentIncludeLoc, 0, EndStatementAtEOFStack.back());
  1117. return peekTok(ShouldSkipSpace);
  1118. }
  1119. EndStatementAtEOFStack.pop_back();
  1120. assert(EndStatementAtEOFStack.empty());
  1121. }
  1122. assert(ReadCount == 1);
  1123. return Tok;
  1124. }
  1125. bool MasmParser::enabledGenDwarfForAssembly() {
  1126. // Check whether the user specified -g.
  1127. if (!getContext().getGenDwarfForAssembly())
  1128. return false;
  1129. // If we haven't encountered any .file directives (which would imply that
  1130. // the assembler source was produced with debug info already) then emit one
  1131. // describing the assembler source file itself.
  1132. if (getContext().getGenDwarfFileNumber() == 0) {
  1133. // Use the first #line directive for this, if any. It's preprocessed, so
  1134. // there is no checksum, and of course no source directive.
  1135. if (!FirstCppHashFilename.empty())
  1136. getContext().setMCLineTableRootFile(/*CUID=*/0,
  1137. getContext().getCompilationDir(),
  1138. FirstCppHashFilename,
  1139. /*Cksum=*/None, /*Source=*/None);
  1140. const MCDwarfFile &RootFile =
  1141. getContext().getMCDwarfLineTable(/*CUID=*/0).getRootFile();
  1142. getContext().setGenDwarfFileNumber(getStreamer().emitDwarfFileDirective(
  1143. /*CUID=*/0, getContext().getCompilationDir(), RootFile.Name,
  1144. RootFile.Checksum, RootFile.Source));
  1145. }
  1146. return true;
  1147. }
  1148. bool MasmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
  1149. // Create the initial section, if requested.
  1150. if (!NoInitialTextSection)
  1151. Out.initSections(false, getTargetParser().getSTI());
  1152. // Prime the lexer.
  1153. Lex();
  1154. HadError = false;
  1155. AsmCond StartingCondState = TheCondState;
  1156. SmallVector<AsmRewrite, 4> AsmStrRewrites;
  1157. // If we are generating dwarf for assembly source files save the initial text
  1158. // section. (Don't use enabledGenDwarfForAssembly() here, as we aren't
  1159. // emitting any actual debug info yet and haven't had a chance to parse any
  1160. // embedded .file directives.)
  1161. if (getContext().getGenDwarfForAssembly()) {
  1162. MCSection *Sec = getStreamer().getCurrentSectionOnly();
  1163. if (!Sec->getBeginSymbol()) {
  1164. MCSymbol *SectionStartSym = getContext().createTempSymbol();
  1165. getStreamer().emitLabel(SectionStartSym);
  1166. Sec->setBeginSymbol(SectionStartSym);
  1167. }
  1168. bool InsertResult = getContext().addGenDwarfSection(Sec);
  1169. assert(InsertResult && ".text section should not have debug info yet");
  1170. (void)InsertResult;
  1171. }
  1172. getTargetParser().onBeginOfFile();
  1173. // While we have input, parse each statement.
  1174. while (Lexer.isNot(AsmToken::Eof) ||
  1175. SrcMgr.getParentIncludeLoc(CurBuffer) != SMLoc()) {
  1176. // Skip through the EOF at the end of an inclusion.
  1177. if (Lexer.is(AsmToken::Eof))
  1178. Lex();
  1179. ParseStatementInfo Info(&AsmStrRewrites);
  1180. bool Parsed = parseStatement(Info, nullptr);
  1181. // If we have a Lexer Error we are on an Error Token. Load in Lexer Error
  1182. // for printing ErrMsg via Lex() only if no (presumably better) parser error
  1183. // exists.
  1184. if (Parsed && !hasPendingError() && Lexer.getTok().is(AsmToken::Error)) {
  1185. Lex();
  1186. }
  1187. // parseStatement returned true so may need to emit an error.
  1188. printPendingErrors();
  1189. // Skipping to the next line if needed.
  1190. if (Parsed && !getLexer().isAtStartOfStatement())
  1191. eatToEndOfStatement();
  1192. }
  1193. getTargetParser().onEndOfFile();
  1194. printPendingErrors();
  1195. // All errors should have been emitted.
  1196. assert(!hasPendingError() && "unexpected error from parseStatement");
  1197. getTargetParser().flushPendingInstructions(getStreamer());
  1198. if (TheCondState.TheCond != StartingCondState.TheCond ||
  1199. TheCondState.Ignore != StartingCondState.Ignore)
  1200. printError(getTok().getLoc(), "unmatched .ifs or .elses");
  1201. // Check to see there are no empty DwarfFile slots.
  1202. const auto &LineTables = getContext().getMCDwarfLineTables();
  1203. if (!LineTables.empty()) {
  1204. unsigned Index = 0;
  1205. for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
  1206. if (File.Name.empty() && Index != 0)
  1207. printError(getTok().getLoc(), "unassigned file number: " +
  1208. Twine(Index) +
  1209. " for .file directives");
  1210. ++Index;
  1211. }
  1212. }
  1213. // Check to see that all assembler local symbols were actually defined.
  1214. // Targets that don't do subsections via symbols may not want this, though,
  1215. // so conservatively exclude them. Only do this if we're finalizing, though,
  1216. // as otherwise we won't necessarilly have seen everything yet.
  1217. if (!NoFinalize) {
  1218. if (MAI.hasSubsectionsViaSymbols()) {
  1219. for (const auto &TableEntry : getContext().getSymbols()) {
  1220. MCSymbol *Sym = TableEntry.getValue();
  1221. // Variable symbols may not be marked as defined, so check those
  1222. // explicitly. If we know it's a variable, we have a definition for
  1223. // the purposes of this check.
  1224. if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
  1225. // FIXME: We would really like to refer back to where the symbol was
  1226. // first referenced for a source location. We need to add something
  1227. // to track that. Currently, we just point to the end of the file.
  1228. printError(getTok().getLoc(), "assembler local symbol '" +
  1229. Sym->getName() + "' not defined");
  1230. }
  1231. }
  1232. // Temporary symbols like the ones for directional jumps don't go in the
  1233. // symbol table. They also need to be diagnosed in all (final) cases.
  1234. for (std::tuple<SMLoc, CppHashInfoTy, MCSymbol *> &LocSym : DirLabels) {
  1235. if (std::get<2>(LocSym)->isUndefined()) {
  1236. // Reset the state of any "# line file" directives we've seen to the
  1237. // context as it was at the diagnostic site.
  1238. CppHashInfo = std::get<1>(LocSym);
  1239. printError(std::get<0>(LocSym), "directional label undefined");
  1240. }
  1241. }
  1242. }
  1243. // Finalize the output stream if there are no errors and if the client wants
  1244. // us to.
  1245. if (!HadError && !NoFinalize)
  1246. Out.Finish(Lexer.getLoc());
  1247. return HadError || getContext().hadError();
  1248. }
  1249. bool MasmParser::checkForValidSection() {
  1250. if (!ParsingMSInlineAsm && !getStreamer().getCurrentSectionOnly()) {
  1251. Out.initSections(false, getTargetParser().getSTI());
  1252. return Error(getTok().getLoc(),
  1253. "expected section directive before assembly directive");
  1254. }
  1255. return false;
  1256. }
  1257. /// Throw away the rest of the line for testing purposes.
  1258. void MasmParser::eatToEndOfStatement() {
  1259. while (Lexer.isNot(AsmToken::EndOfStatement)) {
  1260. if (Lexer.is(AsmToken::Eof)) {
  1261. SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
  1262. if (ParentIncludeLoc == SMLoc()) {
  1263. break;
  1264. }
  1265. EndStatementAtEOFStack.pop_back();
  1266. jumpToLoc(ParentIncludeLoc, 0, EndStatementAtEOFStack.back());
  1267. }
  1268. Lexer.Lex();
  1269. }
  1270. // Eat EOL.
  1271. if (Lexer.is(AsmToken::EndOfStatement))
  1272. Lexer.Lex();
  1273. }
  1274. SmallVector<StringRef, 1>
  1275. MasmParser::parseStringRefsTo(AsmToken::TokenKind EndTok) {
  1276. SmallVector<StringRef, 1> Refs;
  1277. const char *Start = getTok().getLoc().getPointer();
  1278. while (Lexer.isNot(EndTok)) {
  1279. if (Lexer.is(AsmToken::Eof)) {
  1280. SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
  1281. if (ParentIncludeLoc == SMLoc()) {
  1282. break;
  1283. }
  1284. Refs.emplace_back(Start, getTok().getLoc().getPointer() - Start);
  1285. EndStatementAtEOFStack.pop_back();
  1286. jumpToLoc(ParentIncludeLoc, 0, EndStatementAtEOFStack.back());
  1287. Lexer.Lex();
  1288. Start = getTok().getLoc().getPointer();
  1289. } else {
  1290. Lexer.Lex();
  1291. }
  1292. }
  1293. Refs.emplace_back(Start, getTok().getLoc().getPointer() - Start);
  1294. return Refs;
  1295. }
  1296. std::string MasmParser::parseStringTo(AsmToken::TokenKind EndTok) {
  1297. SmallVector<StringRef, 1> Refs = parseStringRefsTo(EndTok);
  1298. std::string Str;
  1299. for (StringRef S : Refs) {
  1300. Str.append(S.str());
  1301. }
  1302. return Str;
  1303. }
  1304. StringRef MasmParser::parseStringToEndOfStatement() {
  1305. const char *Start = getTok().getLoc().getPointer();
  1306. while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
  1307. Lexer.Lex();
  1308. const char *End = getTok().getLoc().getPointer();
  1309. return StringRef(Start, End - Start);
  1310. }
  1311. /// Parse a paren expression and return it.
  1312. /// NOTE: This assumes the leading '(' has already been consumed.
  1313. ///
  1314. /// parenexpr ::= expr)
  1315. ///
  1316. bool MasmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
  1317. if (parseExpression(Res))
  1318. return true;
  1319. EndLoc = Lexer.getTok().getEndLoc();
  1320. return parseRParen();
  1321. }
  1322. /// Parse a bracket expression and return it.
  1323. /// NOTE: This assumes the leading '[' has already been consumed.
  1324. ///
  1325. /// bracketexpr ::= expr]
  1326. ///
  1327. bool MasmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
  1328. if (parseExpression(Res))
  1329. return true;
  1330. EndLoc = getTok().getEndLoc();
  1331. if (parseToken(AsmToken::RBrac, "expected ']' in brackets expression"))
  1332. return true;
  1333. return false;
  1334. }
  1335. /// Parse a primary expression and return it.
  1336. /// primaryexpr ::= (parenexpr
  1337. /// primaryexpr ::= symbol
  1338. /// primaryexpr ::= number
  1339. /// primaryexpr ::= '.'
  1340. /// primaryexpr ::= ~,+,-,'not' primaryexpr
  1341. /// primaryexpr ::= string
  1342. /// (a string is interpreted as a 64-bit number in big-endian base-256)
  1343. bool MasmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
  1344. AsmTypeInfo *TypeInfo) {
  1345. SMLoc FirstTokenLoc = getLexer().getLoc();
  1346. AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
  1347. switch (FirstTokenKind) {
  1348. default:
  1349. return TokError("unknown token in expression");
  1350. // If we have an error assume that we've already handled it.
  1351. case AsmToken::Error:
  1352. return true;
  1353. case AsmToken::Exclaim:
  1354. Lex(); // Eat the operator.
  1355. if (parsePrimaryExpr(Res, EndLoc, nullptr))
  1356. return true;
  1357. Res = MCUnaryExpr::createLNot(Res, getContext(), FirstTokenLoc);
  1358. return false;
  1359. case AsmToken::Dollar:
  1360. case AsmToken::At:
  1361. case AsmToken::Identifier: {
  1362. StringRef Identifier;
  1363. if (parseIdentifier(Identifier)) {
  1364. // We may have failed but $ may be a valid token.
  1365. if (getTok().is(AsmToken::Dollar)) {
  1366. if (Lexer.getMAI().getDollarIsPC()) {
  1367. Lex();
  1368. // This is a '$' reference, which references the current PC. Emit a
  1369. // temporary label to the streamer and refer to it.
  1370. MCSymbol *Sym = Ctx.createTempSymbol();
  1371. Out.emitLabel(Sym);
  1372. Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None,
  1373. getContext());
  1374. EndLoc = FirstTokenLoc;
  1375. return false;
  1376. }
  1377. return Error(FirstTokenLoc, "invalid token in expression");
  1378. }
  1379. }
  1380. // Parse named bitwise negation.
  1381. if (Identifier.equals_insensitive("not")) {
  1382. if (parsePrimaryExpr(Res, EndLoc, nullptr))
  1383. return true;
  1384. Res = MCUnaryExpr::createNot(Res, getContext(), FirstTokenLoc);
  1385. return false;
  1386. }
  1387. // Parse symbol variant.
  1388. std::pair<StringRef, StringRef> Split;
  1389. if (!MAI.useParensForSymbolVariant()) {
  1390. if (FirstTokenKind == AsmToken::String) {
  1391. if (Lexer.is(AsmToken::At)) {
  1392. Lex(); // eat @
  1393. SMLoc AtLoc = getLexer().getLoc();
  1394. StringRef VName;
  1395. if (parseIdentifier(VName))
  1396. return Error(AtLoc, "expected symbol variant after '@'");
  1397. Split = std::make_pair(Identifier, VName);
  1398. }
  1399. } else {
  1400. Split = Identifier.split('@');
  1401. }
  1402. } else if (Lexer.is(AsmToken::LParen)) {
  1403. Lex(); // eat '('.
  1404. StringRef VName;
  1405. parseIdentifier(VName);
  1406. // eat ')'.
  1407. if (parseToken(AsmToken::RParen,
  1408. "unexpected token in variant, expected ')'"))
  1409. return true;
  1410. Split = std::make_pair(Identifier, VName);
  1411. }
  1412. EndLoc = SMLoc::getFromPointer(Identifier.end());
  1413. // This is a symbol reference.
  1414. StringRef SymbolName = Identifier;
  1415. if (SymbolName.empty())
  1416. return Error(getLexer().getLoc(), "expected a symbol reference");
  1417. MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
  1418. // Look up the symbol variant if used.
  1419. if (!Split.second.empty()) {
  1420. Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
  1421. if (Variant != MCSymbolRefExpr::VK_Invalid) {
  1422. SymbolName = Split.first;
  1423. } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
  1424. Variant = MCSymbolRefExpr::VK_None;
  1425. } else {
  1426. return Error(SMLoc::getFromPointer(Split.second.begin()),
  1427. "invalid variant '" + Split.second + "'");
  1428. }
  1429. }
  1430. // Find the field offset if used.
  1431. AsmFieldInfo Info;
  1432. Split = SymbolName.split('.');
  1433. if (Split.second.empty()) {
  1434. } else {
  1435. SymbolName = Split.first;
  1436. if (lookUpField(SymbolName, Split.second, Info)) {
  1437. std::pair<StringRef, StringRef> BaseMember = Split.second.split('.');
  1438. StringRef Base = BaseMember.first, Member = BaseMember.second;
  1439. lookUpField(Base, Member, Info);
  1440. } else if (Structs.count(SymbolName.lower())) {
  1441. // This is actually a reference to a field offset.
  1442. Res = MCConstantExpr::create(Info.Offset, getContext());
  1443. return false;
  1444. }
  1445. }
  1446. MCSymbol *Sym = getContext().getInlineAsmLabel(SymbolName);
  1447. if (!Sym) {
  1448. // If this is a built-in numeric value, treat it as a constant.
  1449. auto BuiltinIt = BuiltinSymbolMap.find(SymbolName.lower());
  1450. const BuiltinSymbol Symbol = (BuiltinIt == BuiltinSymbolMap.end())
  1451. ? BI_NO_SYMBOL
  1452. : BuiltinIt->getValue();
  1453. if (Symbol != BI_NO_SYMBOL) {
  1454. const MCExpr *Value = evaluateBuiltinValue(Symbol, FirstTokenLoc);
  1455. if (Value) {
  1456. Res = Value;
  1457. return false;
  1458. }
  1459. }
  1460. // Variables use case-insensitive symbol names; if this is a variable, we
  1461. // find the symbol using its canonical name.
  1462. auto VarIt = Variables.find(SymbolName.lower());
  1463. if (VarIt != Variables.end())
  1464. SymbolName = VarIt->second.Name;
  1465. Sym = getContext().getOrCreateSymbol(SymbolName);
  1466. }
  1467. // If this is an absolute variable reference, substitute it now to preserve
  1468. // semantics in the face of reassignment.
  1469. if (Sym->isVariable()) {
  1470. auto V = Sym->getVariableValue(/*SetUsed=*/false);
  1471. bool DoInline = isa<MCConstantExpr>(V) && !Variant;
  1472. if (auto TV = dyn_cast<MCTargetExpr>(V))
  1473. DoInline = TV->inlineAssignedExpr();
  1474. if (DoInline) {
  1475. if (Variant)
  1476. return Error(EndLoc, "unexpected modifier on variable reference");
  1477. Res = Sym->getVariableValue(/*SetUsed=*/false);
  1478. return false;
  1479. }
  1480. }
  1481. // Otherwise create a symbol ref.
  1482. const MCExpr *SymRef =
  1483. MCSymbolRefExpr::create(Sym, Variant, getContext(), FirstTokenLoc);
  1484. if (Info.Offset) {
  1485. Res = MCBinaryExpr::create(
  1486. MCBinaryExpr::Add, SymRef,
  1487. MCConstantExpr::create(Info.Offset, getContext()), getContext());
  1488. } else {
  1489. Res = SymRef;
  1490. }
  1491. if (TypeInfo) {
  1492. if (Info.Type.Name.empty()) {
  1493. auto TypeIt = KnownType.find(Identifier.lower());
  1494. if (TypeIt != KnownType.end()) {
  1495. Info.Type = TypeIt->second;
  1496. }
  1497. }
  1498. *TypeInfo = Info.Type;
  1499. }
  1500. return false;
  1501. }
  1502. case AsmToken::BigNum:
  1503. return TokError("literal value out of range for directive");
  1504. case AsmToken::Integer: {
  1505. SMLoc Loc = getTok().getLoc();
  1506. int64_t IntVal = getTok().getIntVal();
  1507. Res = MCConstantExpr::create(IntVal, getContext());
  1508. EndLoc = Lexer.getTok().getEndLoc();
  1509. Lex(); // Eat token.
  1510. // Look for 'b' or 'f' following an Integer as a directional label.
  1511. if (Lexer.getKind() == AsmToken::Identifier) {
  1512. StringRef IDVal = getTok().getString();
  1513. // Look up the symbol variant if used.
  1514. std::pair<StringRef, StringRef> Split = IDVal.split('@');
  1515. MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
  1516. if (Split.first.size() != IDVal.size()) {
  1517. Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
  1518. if (Variant == MCSymbolRefExpr::VK_Invalid)
  1519. return TokError("invalid variant '" + Split.second + "'");
  1520. IDVal = Split.first;
  1521. }
  1522. if (IDVal == "f" || IDVal == "b") {
  1523. MCSymbol *Sym =
  1524. Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b");
  1525. Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
  1526. if (IDVal == "b" && Sym->isUndefined())
  1527. return Error(Loc, "directional label undefined");
  1528. DirLabels.push_back(std::make_tuple(Loc, CppHashInfo, Sym));
  1529. EndLoc = Lexer.getTok().getEndLoc();
  1530. Lex(); // Eat identifier.
  1531. }
  1532. }
  1533. return false;
  1534. }
  1535. case AsmToken::String: {
  1536. // MASM strings (used as constants) are interpreted as big-endian base-256.
  1537. SMLoc ValueLoc = getTok().getLoc();
  1538. std::string Value;
  1539. if (parseEscapedString(Value))
  1540. return true;
  1541. if (Value.size() > 8)
  1542. return Error(ValueLoc, "literal value out of range");
  1543. uint64_t IntValue = 0;
  1544. for (const unsigned char CharVal : Value)
  1545. IntValue = (IntValue << 8) | CharVal;
  1546. Res = MCConstantExpr::create(IntValue, getContext());
  1547. return false;
  1548. }
  1549. case AsmToken::Real: {
  1550. APFloat RealVal(APFloat::IEEEdouble(), getTok().getString());
  1551. uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
  1552. Res = MCConstantExpr::create(IntVal, getContext());
  1553. EndLoc = Lexer.getTok().getEndLoc();
  1554. Lex(); // Eat token.
  1555. return false;
  1556. }
  1557. case AsmToken::Dot: {
  1558. // This is a '.' reference, which references the current PC. Emit a
  1559. // temporary label to the streamer and refer to it.
  1560. MCSymbol *Sym = Ctx.createTempSymbol();
  1561. Out.emitLabel(Sym);
  1562. Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
  1563. EndLoc = Lexer.getTok().getEndLoc();
  1564. Lex(); // Eat identifier.
  1565. return false;
  1566. }
  1567. case AsmToken::LParen:
  1568. Lex(); // Eat the '('.
  1569. return parseParenExpr(Res, EndLoc);
  1570. case AsmToken::LBrac:
  1571. if (!PlatformParser->HasBracketExpressions())
  1572. return TokError("brackets expression not supported on this target");
  1573. Lex(); // Eat the '['.
  1574. return parseBracketExpr(Res, EndLoc);
  1575. case AsmToken::Minus:
  1576. Lex(); // Eat the operator.
  1577. if (parsePrimaryExpr(Res, EndLoc, nullptr))
  1578. return true;
  1579. Res = MCUnaryExpr::createMinus(Res, getContext(), FirstTokenLoc);
  1580. return false;
  1581. case AsmToken::Plus:
  1582. Lex(); // Eat the operator.
  1583. if (parsePrimaryExpr(Res, EndLoc, nullptr))
  1584. return true;
  1585. Res = MCUnaryExpr::createPlus(Res, getContext(), FirstTokenLoc);
  1586. return false;
  1587. case AsmToken::Tilde:
  1588. Lex(); // Eat the operator.
  1589. if (parsePrimaryExpr(Res, EndLoc, nullptr))
  1590. return true;
  1591. Res = MCUnaryExpr::createNot(Res, getContext(), FirstTokenLoc);
  1592. return false;
  1593. // MIPS unary expression operators. The lexer won't generate these tokens if
  1594. // MCAsmInfo::HasMipsExpressions is false for the target.
  1595. case AsmToken::PercentCall16:
  1596. case AsmToken::PercentCall_Hi:
  1597. case AsmToken::PercentCall_Lo:
  1598. case AsmToken::PercentDtprel_Hi:
  1599. case AsmToken::PercentDtprel_Lo:
  1600. case AsmToken::PercentGot:
  1601. case AsmToken::PercentGot_Disp:
  1602. case AsmToken::PercentGot_Hi:
  1603. case AsmToken::PercentGot_Lo:
  1604. case AsmToken::PercentGot_Ofst:
  1605. case AsmToken::PercentGot_Page:
  1606. case AsmToken::PercentGottprel:
  1607. case AsmToken::PercentGp_Rel:
  1608. case AsmToken::PercentHi:
  1609. case AsmToken::PercentHigher:
  1610. case AsmToken::PercentHighest:
  1611. case AsmToken::PercentLo:
  1612. case AsmToken::PercentNeg:
  1613. case AsmToken::PercentPcrel_Hi:
  1614. case AsmToken::PercentPcrel_Lo:
  1615. case AsmToken::PercentTlsgd:
  1616. case AsmToken::PercentTlsldm:
  1617. case AsmToken::PercentTprel_Hi:
  1618. case AsmToken::PercentTprel_Lo:
  1619. Lex(); // Eat the operator.
  1620. if (Lexer.isNot(AsmToken::LParen))
  1621. return TokError("expected '(' after operator");
  1622. Lex(); // Eat the operator.
  1623. if (parseExpression(Res, EndLoc))
  1624. return true;
  1625. if (parseRParen())
  1626. return true;
  1627. Res = getTargetParser().createTargetUnaryExpr(Res, FirstTokenKind, Ctx);
  1628. return !Res;
  1629. }
  1630. }
  1631. bool MasmParser::parseExpression(const MCExpr *&Res) {
  1632. SMLoc EndLoc;
  1633. return parseExpression(Res, EndLoc);
  1634. }
  1635. /// This function checks if the next token is <string> type or arithmetic.
  1636. /// string that begin with character '<' must end with character '>'.
  1637. /// otherwise it is arithmetics.
  1638. /// If the function returns a 'true' value,
  1639. /// the End argument will be filled with the last location pointed to the '>'
  1640. /// character.
  1641. static bool isAngleBracketString(SMLoc &StrLoc, SMLoc &EndLoc) {
  1642. assert((StrLoc.getPointer() != nullptr) &&
  1643. "Argument to the function cannot be a NULL value");
  1644. const char *CharPtr = StrLoc.getPointer();
  1645. while ((*CharPtr != '>') && (*CharPtr != '\n') && (*CharPtr != '\r') &&
  1646. (*CharPtr != '\0')) {
  1647. if (*CharPtr == '!')
  1648. CharPtr++;
  1649. CharPtr++;
  1650. }
  1651. if (*CharPtr == '>') {
  1652. EndLoc = StrLoc.getFromPointer(CharPtr + 1);
  1653. return true;
  1654. }
  1655. return false;
  1656. }
  1657. /// creating a string without the escape characters '!'.
  1658. static std::string angleBracketString(StringRef BracketContents) {
  1659. std::string Res;
  1660. for (size_t Pos = 0; Pos < BracketContents.size(); Pos++) {
  1661. if (BracketContents[Pos] == '!')
  1662. Pos++;
  1663. Res += BracketContents[Pos];
  1664. }
  1665. return Res;
  1666. }
  1667. /// Parse an expression and return it.
  1668. ///
  1669. /// expr ::= expr &&,|| expr -> lowest.
  1670. /// expr ::= expr |,^,&,! expr
  1671. /// expr ::= expr ==,!=,<>,<,<=,>,>= expr
  1672. /// expr ::= expr <<,>> expr
  1673. /// expr ::= expr +,- expr
  1674. /// expr ::= expr *,/,% expr -> highest.
  1675. /// expr ::= primaryexpr
  1676. ///
  1677. bool MasmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
  1678. // Parse the expression.
  1679. Res = nullptr;
  1680. if (getTargetParser().parsePrimaryExpr(Res, EndLoc) ||
  1681. parseBinOpRHS(1, Res, EndLoc))
  1682. return true;
  1683. // Try to constant fold it up front, if possible. Do not exploit
  1684. // assembler here.
  1685. int64_t Value;
  1686. if (Res->evaluateAsAbsolute(Value))
  1687. Res = MCConstantExpr::create(Value, getContext());
  1688. return false;
  1689. }
  1690. bool MasmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
  1691. Res = nullptr;
  1692. return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
  1693. }
  1694. bool MasmParser::parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
  1695. SMLoc &EndLoc) {
  1696. if (parseParenExpr(Res, EndLoc))
  1697. return true;
  1698. for (; ParenDepth > 0; --ParenDepth) {
  1699. if (parseBinOpRHS(1, Res, EndLoc))
  1700. return true;
  1701. // We don't Lex() the last RParen.
  1702. // This is the same behavior as parseParenExpression().
  1703. if (ParenDepth - 1 > 0) {
  1704. EndLoc = getTok().getEndLoc();
  1705. if (parseRParen())
  1706. return true;
  1707. }
  1708. }
  1709. return false;
  1710. }
  1711. bool MasmParser::parseAbsoluteExpression(int64_t &Res) {
  1712. const MCExpr *Expr;
  1713. SMLoc StartLoc = Lexer.getLoc();
  1714. if (parseExpression(Expr))
  1715. return true;
  1716. if (!Expr->evaluateAsAbsolute(Res, getStreamer().getAssemblerPtr()))
  1717. return Error(StartLoc, "expected absolute expression");
  1718. return false;
  1719. }
  1720. static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K,
  1721. MCBinaryExpr::Opcode &Kind,
  1722. bool ShouldUseLogicalShr,
  1723. bool EndExpressionAtGreater) {
  1724. switch (K) {
  1725. default:
  1726. return 0; // not a binop.
  1727. // Lowest Precedence: &&, ||
  1728. case AsmToken::AmpAmp:
  1729. Kind = MCBinaryExpr::LAnd;
  1730. return 2;
  1731. case AsmToken::PipePipe:
  1732. Kind = MCBinaryExpr::LOr;
  1733. return 1;
  1734. // Low Precedence: ==, !=, <>, <, <=, >, >=
  1735. case AsmToken::EqualEqual:
  1736. Kind = MCBinaryExpr::EQ;
  1737. return 3;
  1738. case AsmToken::ExclaimEqual:
  1739. case AsmToken::LessGreater:
  1740. Kind = MCBinaryExpr::NE;
  1741. return 3;
  1742. case AsmToken::Less:
  1743. Kind = MCBinaryExpr::LT;
  1744. return 3;
  1745. case AsmToken::LessEqual:
  1746. Kind = MCBinaryExpr::LTE;
  1747. return 3;
  1748. case AsmToken::Greater:
  1749. if (EndExpressionAtGreater)
  1750. return 0;
  1751. Kind = MCBinaryExpr::GT;
  1752. return 3;
  1753. case AsmToken::GreaterEqual:
  1754. Kind = MCBinaryExpr::GTE;
  1755. return 3;
  1756. // Low Intermediate Precedence: +, -
  1757. case AsmToken::Plus:
  1758. Kind = MCBinaryExpr::Add;
  1759. return 4;
  1760. case AsmToken::Minus:
  1761. Kind = MCBinaryExpr::Sub;
  1762. return 4;
  1763. // High Intermediate Precedence: |, &, ^
  1764. case AsmToken::Pipe:
  1765. Kind = MCBinaryExpr::Or;
  1766. return 5;
  1767. case AsmToken::Caret:
  1768. Kind = MCBinaryExpr::Xor;
  1769. return 5;
  1770. case AsmToken::Amp:
  1771. Kind = MCBinaryExpr::And;
  1772. return 5;
  1773. // Highest Precedence: *, /, %, <<, >>
  1774. case AsmToken::Star:
  1775. Kind = MCBinaryExpr::Mul;
  1776. return 6;
  1777. case AsmToken::Slash:
  1778. Kind = MCBinaryExpr::Div;
  1779. return 6;
  1780. case AsmToken::Percent:
  1781. Kind = MCBinaryExpr::Mod;
  1782. return 6;
  1783. case AsmToken::LessLess:
  1784. Kind = MCBinaryExpr::Shl;
  1785. return 6;
  1786. case AsmToken::GreaterGreater:
  1787. if (EndExpressionAtGreater)
  1788. return 0;
  1789. Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
  1790. return 6;
  1791. }
  1792. }
  1793. unsigned MasmParser::getBinOpPrecedence(AsmToken::TokenKind K,
  1794. MCBinaryExpr::Opcode &Kind) {
  1795. bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr();
  1796. return getGNUBinOpPrecedence(K, Kind, ShouldUseLogicalShr,
  1797. AngleBracketDepth > 0);
  1798. }
  1799. /// Parse all binary operators with precedence >= 'Precedence'.
  1800. /// Res contains the LHS of the expression on input.
  1801. bool MasmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
  1802. SMLoc &EndLoc) {
  1803. SMLoc StartLoc = Lexer.getLoc();
  1804. while (true) {
  1805. AsmToken::TokenKind TokKind = Lexer.getKind();
  1806. if (Lexer.getKind() == AsmToken::Identifier) {
  1807. TokKind = StringSwitch<AsmToken::TokenKind>(Lexer.getTok().getString())
  1808. .CaseLower("and", AsmToken::Amp)
  1809. .CaseLower("not", AsmToken::Exclaim)
  1810. .CaseLower("or", AsmToken::Pipe)
  1811. .CaseLower("eq", AsmToken::EqualEqual)
  1812. .CaseLower("ne", AsmToken::ExclaimEqual)
  1813. .CaseLower("lt", AsmToken::Less)
  1814. .CaseLower("le", AsmToken::LessEqual)
  1815. .CaseLower("gt", AsmToken::Greater)
  1816. .CaseLower("ge", AsmToken::GreaterEqual)
  1817. .Default(TokKind);
  1818. }
  1819. MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
  1820. unsigned TokPrec = getBinOpPrecedence(TokKind, Kind);
  1821. // If the next token is lower precedence than we are allowed to eat, return
  1822. // successfully with what we ate already.
  1823. if (TokPrec < Precedence)
  1824. return false;
  1825. Lex();
  1826. // Eat the next primary expression.
  1827. const MCExpr *RHS;
  1828. if (getTargetParser().parsePrimaryExpr(RHS, EndLoc))
  1829. return true;
  1830. // If BinOp binds less tightly with RHS than the operator after RHS, let
  1831. // the pending operator take RHS as its LHS.
  1832. MCBinaryExpr::Opcode Dummy;
  1833. unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
  1834. if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
  1835. return true;
  1836. // Merge LHS and RHS according to operator.
  1837. Res = MCBinaryExpr::create(Kind, Res, RHS, getContext(), StartLoc);
  1838. }
  1839. }
  1840. /// ParseStatement:
  1841. /// ::= % statement
  1842. /// ::= EndOfStatement
  1843. /// ::= Label* Directive ...Operands... EndOfStatement
  1844. /// ::= Label* Identifier OperandList* EndOfStatement
  1845. bool MasmParser::parseStatement(ParseStatementInfo &Info,
  1846. MCAsmParserSemaCallback *SI) {
  1847. assert(!hasPendingError() && "parseStatement started with pending error");
  1848. // Eat initial spaces and comments.
  1849. while (Lexer.is(AsmToken::Space))
  1850. Lex();
  1851. if (Lexer.is(AsmToken::EndOfStatement)) {
  1852. // If this is a line comment we can drop it safely.
  1853. if (getTok().getString().empty() || getTok().getString().front() == '\r' ||
  1854. getTok().getString().front() == '\n')
  1855. Out.AddBlankLine();
  1856. Lex();
  1857. return false;
  1858. }
  1859. // If preceded by an expansion operator, first expand all text macros and
  1860. // macro functions.
  1861. if (getTok().is(AsmToken::Percent)) {
  1862. SMLoc ExpansionLoc = getTok().getLoc();
  1863. if (parseToken(AsmToken::Percent) || expandStatement(ExpansionLoc))
  1864. return true;
  1865. }
  1866. // Statements always start with an identifier, unless we're dealing with a
  1867. // processor directive (.386, .686, etc.) that lexes as a real.
  1868. AsmToken ID = getTok();
  1869. SMLoc IDLoc = ID.getLoc();
  1870. StringRef IDVal;
  1871. int64_t LocalLabelVal = -1;
  1872. if (Lexer.is(AsmToken::HashDirective))
  1873. return parseCppHashLineFilenameComment(IDLoc);
  1874. // Allow an integer followed by a ':' as a directional local label.
  1875. if (Lexer.is(AsmToken::Integer)) {
  1876. LocalLabelVal = getTok().getIntVal();
  1877. if (LocalLabelVal < 0) {
  1878. if (!TheCondState.Ignore) {
  1879. Lex(); // always eat a token
  1880. return Error(IDLoc, "unexpected token at start of statement");
  1881. }
  1882. IDVal = "";
  1883. } else {
  1884. IDVal = getTok().getString();
  1885. Lex(); // Consume the integer token to be used as an identifier token.
  1886. if (Lexer.getKind() != AsmToken::Colon) {
  1887. if (!TheCondState.Ignore) {
  1888. Lex(); // always eat a token
  1889. return Error(IDLoc, "unexpected token at start of statement");
  1890. }
  1891. }
  1892. }
  1893. } else if (Lexer.is(AsmToken::Dot)) {
  1894. // Treat '.' as a valid identifier in this context.
  1895. Lex();
  1896. IDVal = ".";
  1897. } else if (Lexer.is(AsmToken::LCurly)) {
  1898. // Treat '{' as a valid identifier in this context.
  1899. Lex();
  1900. IDVal = "{";
  1901. } else if (Lexer.is(AsmToken::RCurly)) {
  1902. // Treat '}' as a valid identifier in this context.
  1903. Lex();
  1904. IDVal = "}";
  1905. } else if (Lexer.is(AsmToken::Star) &&
  1906. getTargetParser().starIsStartOfStatement()) {
  1907. // Accept '*' as a valid start of statement.
  1908. Lex();
  1909. IDVal = "*";
  1910. } else if (Lexer.is(AsmToken::Real)) {
  1911. // Treat ".<number>" as a valid identifier in this context.
  1912. IDVal = getTok().getString();
  1913. Lex(); // always eat a token
  1914. if (!IDVal.startswith("."))
  1915. return Error(IDLoc, "unexpected token at start of statement");
  1916. } else if (parseIdentifier(IDVal, StartOfStatement)) {
  1917. if (!TheCondState.Ignore) {
  1918. Lex(); // always eat a token
  1919. return Error(IDLoc, "unexpected token at start of statement");
  1920. }
  1921. IDVal = "";
  1922. }
  1923. // Handle conditional assembly here before checking for skipping. We
  1924. // have to do this so that .endif isn't skipped in a ".if 0" block for
  1925. // example.
  1926. StringMap<DirectiveKind>::const_iterator DirKindIt =
  1927. DirectiveKindMap.find(IDVal.lower());
  1928. DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
  1929. ? DK_NO_DIRECTIVE
  1930. : DirKindIt->getValue();
  1931. switch (DirKind) {
  1932. default:
  1933. break;
  1934. case DK_IF:
  1935. case DK_IFE:
  1936. return parseDirectiveIf(IDLoc, DirKind);
  1937. case DK_IFB:
  1938. return parseDirectiveIfb(IDLoc, true);
  1939. case DK_IFNB:
  1940. return parseDirectiveIfb(IDLoc, false);
  1941. case DK_IFDEF:
  1942. return parseDirectiveIfdef(IDLoc, true);
  1943. case DK_IFNDEF:
  1944. return parseDirectiveIfdef(IDLoc, false);
  1945. case DK_IFDIF:
  1946. return parseDirectiveIfidn(IDLoc, /*ExpectEqual=*/false,
  1947. /*CaseInsensitive=*/false);
  1948. case DK_IFDIFI:
  1949. return parseDirectiveIfidn(IDLoc, /*ExpectEqual=*/false,
  1950. /*CaseInsensitive=*/true);
  1951. case DK_IFIDN:
  1952. return parseDirectiveIfidn(IDLoc, /*ExpectEqual=*/true,
  1953. /*CaseInsensitive=*/false);
  1954. case DK_IFIDNI:
  1955. return parseDirectiveIfidn(IDLoc, /*ExpectEqual=*/true,
  1956. /*CaseInsensitive=*/true);
  1957. case DK_ELSEIF:
  1958. case DK_ELSEIFE:
  1959. return parseDirectiveElseIf(IDLoc, DirKind);
  1960. case DK_ELSEIFB:
  1961. return parseDirectiveElseIfb(IDLoc, true);
  1962. case DK_ELSEIFNB:
  1963. return parseDirectiveElseIfb(IDLoc, false);
  1964. case DK_ELSEIFDEF:
  1965. return parseDirectiveElseIfdef(IDLoc, true);
  1966. case DK_ELSEIFNDEF:
  1967. return parseDirectiveElseIfdef(IDLoc, false);
  1968. case DK_ELSEIFDIF:
  1969. return parseDirectiveElseIfidn(IDLoc, /*ExpectEqual=*/false,
  1970. /*CaseInsensitive=*/false);
  1971. case DK_ELSEIFDIFI:
  1972. return parseDirectiveElseIfidn(IDLoc, /*ExpectEqual=*/false,
  1973. /*CaseInsensitive=*/true);
  1974. case DK_ELSEIFIDN:
  1975. return parseDirectiveElseIfidn(IDLoc, /*ExpectEqual=*/true,
  1976. /*CaseInsensitive=*/false);
  1977. case DK_ELSEIFIDNI:
  1978. return parseDirectiveElseIfidn(IDLoc, /*ExpectEqual=*/true,
  1979. /*CaseInsensitive=*/true);
  1980. case DK_ELSE:
  1981. return parseDirectiveElse(IDLoc);
  1982. case DK_ENDIF:
  1983. return parseDirectiveEndIf(IDLoc);
  1984. }
  1985. // Ignore the statement if in the middle of inactive conditional
  1986. // (e.g. ".if 0").
  1987. if (TheCondState.Ignore) {
  1988. eatToEndOfStatement();
  1989. return false;
  1990. }
  1991. // FIXME: Recurse on local labels?
  1992. // See what kind of statement we have.
  1993. switch (Lexer.getKind()) {
  1994. case AsmToken::Colon: {
  1995. if (!getTargetParser().isLabel(ID))
  1996. break;
  1997. if (checkForValidSection())
  1998. return true;
  1999. // identifier ':' -> Label.
  2000. Lex();
  2001. // Diagnose attempt to use '.' as a label.
  2002. if (IDVal == ".")
  2003. return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
  2004. // Diagnose attempt to use a variable as a label.
  2005. //
  2006. // FIXME: Diagnostics. Note the location of the definition as a label.
  2007. // FIXME: This doesn't diagnose assignment to a symbol which has been
  2008. // implicitly marked as external.
  2009. MCSymbol *Sym;
  2010. if (LocalLabelVal == -1) {
  2011. if (ParsingMSInlineAsm && SI) {
  2012. StringRef RewrittenLabel =
  2013. SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
  2014. assert(!RewrittenLabel.empty() &&
  2015. "We should have an internal name here.");
  2016. Info.AsmRewrites->emplace_back(AOK_Label, IDLoc, IDVal.size(),
  2017. RewrittenLabel);
  2018. IDVal = RewrittenLabel;
  2019. }
  2020. Sym = getContext().getOrCreateSymbol(IDVal);
  2021. } else
  2022. Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal);
  2023. // End of Labels should be treated as end of line for lexing
  2024. // purposes but that information is not available to the Lexer who
  2025. // does not understand Labels. This may cause us to see a Hash
  2026. // here instead of a preprocessor line comment.
  2027. if (getTok().is(AsmToken::Hash)) {
  2028. std::string CommentStr = parseStringTo(AsmToken::EndOfStatement);
  2029. Lexer.Lex();
  2030. Lexer.UnLex(AsmToken(AsmToken::EndOfStatement, CommentStr));
  2031. }
  2032. // Consume any end of statement token, if present, to avoid spurious
  2033. // AddBlankLine calls().
  2034. if (getTok().is(AsmToken::EndOfStatement)) {
  2035. Lex();
  2036. }
  2037. getTargetParser().doBeforeLabelEmit(Sym);
  2038. // Emit the label.
  2039. if (!getTargetParser().isParsingMSInlineAsm())
  2040. Out.emitLabel(Sym, IDLoc);
  2041. // If we are generating dwarf for assembly source files then gather the
  2042. // info to make a dwarf label entry for this label if needed.
  2043. if (enabledGenDwarfForAssembly())
  2044. MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
  2045. IDLoc);
  2046. getTargetParser().onLabelParsed(Sym);
  2047. return false;
  2048. }
  2049. default: // Normal instruction or directive.
  2050. break;
  2051. }
  2052. // If macros are enabled, check to see if this is a macro instantiation.
  2053. if (const MCAsmMacro *M = getContext().lookupMacro(IDVal.lower())) {
  2054. return handleMacroEntry(M, IDLoc);
  2055. }
  2056. // Otherwise, we have a normal instruction or directive.
  2057. if (DirKind != DK_NO_DIRECTIVE) {
  2058. // There are several entities interested in parsing directives:
  2059. //
  2060. // 1. Asm parser extensions. For example, platform-specific parsers
  2061. // (like the ELF parser) register themselves as extensions.
  2062. // 2. The target-specific assembly parser. Some directives are target
  2063. // specific or may potentially behave differently on certain targets.
  2064. // 3. The generic directive parser implemented by this class. These are
  2065. // all the directives that behave in a target and platform independent
  2066. // manner, or at least have a default behavior that's shared between
  2067. // all targets and platforms.
  2068. getTargetParser().flushPendingInstructions(getStreamer());
  2069. // Special-case handling of structure-end directives at higher priority,
  2070. // since ENDS is overloaded as a segment-end directive.
  2071. if (IDVal.equals_insensitive("ends") && StructInProgress.size() > 1 &&
  2072. getTok().is(AsmToken::EndOfStatement)) {
  2073. return parseDirectiveNestedEnds();
  2074. }
  2075. // First, check the extension directive map to see if any extension has
  2076. // registered itself to parse this directive.
  2077. std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
  2078. ExtensionDirectiveMap.lookup(IDVal.lower());
  2079. if (Handler.first)
  2080. return (*Handler.second)(Handler.first, IDVal, IDLoc);
  2081. // Next, let the target-specific assembly parser try.
  2082. SMLoc StartTokLoc = getTok().getLoc();
  2083. bool TPDirectiveReturn =
  2084. ID.is(AsmToken::Identifier) && getTargetParser().ParseDirective(ID);
  2085. if (hasPendingError())
  2086. return true;
  2087. // Currently the return value should be true if we are
  2088. // uninterested but as this is at odds with the standard parsing
  2089. // convention (return true = error) we have instances of a parsed
  2090. // directive that fails returning true as an error. Catch these
  2091. // cases as best as possible errors here.
  2092. if (TPDirectiveReturn && StartTokLoc != getTok().getLoc())
  2093. return true;
  2094. // Return if we did some parsing or believe we succeeded.
  2095. if (!TPDirectiveReturn || StartTokLoc != getTok().getLoc())
  2096. return false;
  2097. // Finally, if no one else is interested in this directive, it must be
  2098. // generic and familiar to this class.
  2099. switch (DirKind) {
  2100. default:
  2101. break;
  2102. case DK_ASCII:
  2103. return parseDirectiveAscii(IDVal, false);
  2104. case DK_ASCIZ:
  2105. case DK_STRING:
  2106. return parseDirectiveAscii(IDVal, true);
  2107. case DK_BYTE:
  2108. case DK_SBYTE:
  2109. case DK_DB:
  2110. return parseDirectiveValue(IDVal, 1);
  2111. case DK_WORD:
  2112. case DK_SWORD:
  2113. case DK_DW:
  2114. return parseDirectiveValue(IDVal, 2);
  2115. case DK_DWORD:
  2116. case DK_SDWORD:
  2117. case DK_DD:
  2118. return parseDirectiveValue(IDVal, 4);
  2119. case DK_FWORD:
  2120. case DK_DF:
  2121. return parseDirectiveValue(IDVal, 6);
  2122. case DK_QWORD:
  2123. case DK_SQWORD:
  2124. case DK_DQ:
  2125. return parseDirectiveValue(IDVal, 8);
  2126. case DK_REAL4:
  2127. return parseDirectiveRealValue(IDVal, APFloat::IEEEsingle(), 4);
  2128. case DK_REAL8:
  2129. return parseDirectiveRealValue(IDVal, APFloat::IEEEdouble(), 8);
  2130. case DK_REAL10:
  2131. return parseDirectiveRealValue(IDVal, APFloat::x87DoubleExtended(), 10);
  2132. case DK_STRUCT:
  2133. case DK_UNION:
  2134. return parseDirectiveNestedStruct(IDVal, DirKind);
  2135. case DK_ENDS:
  2136. return parseDirectiveNestedEnds();
  2137. case DK_ALIGN:
  2138. return parseDirectiveAlign();
  2139. case DK_EVEN:
  2140. return parseDirectiveEven();
  2141. case DK_ORG:
  2142. return parseDirectiveOrg();
  2143. case DK_EXTERN:
  2144. eatToEndOfStatement(); // .extern is the default, ignore it.
  2145. return false;
  2146. case DK_PUBLIC:
  2147. return parseDirectiveSymbolAttribute(MCSA_Global);
  2148. case DK_COMM:
  2149. return parseDirectiveComm(/*IsLocal=*/false);
  2150. case DK_COMMENT:
  2151. return parseDirectiveComment(IDLoc);
  2152. case DK_INCLUDE:
  2153. return parseDirectiveInclude();
  2154. case DK_REPEAT:
  2155. return parseDirectiveRepeat(IDLoc, IDVal);
  2156. case DK_WHILE:
  2157. return parseDirectiveWhile(IDLoc);
  2158. case DK_FOR:
  2159. return parseDirectiveFor(IDLoc, IDVal);
  2160. case DK_FORC:
  2161. return parseDirectiveForc(IDLoc, IDVal);
  2162. case DK_FILE:
  2163. return parseDirectiveFile(IDLoc);
  2164. case DK_LINE:
  2165. return parseDirectiveLine();
  2166. case DK_LOC:
  2167. return parseDirectiveLoc();
  2168. case DK_STABS:
  2169. return parseDirectiveStabs();
  2170. case DK_CV_FILE:
  2171. return parseDirectiveCVFile();
  2172. case DK_CV_FUNC_ID:
  2173. return parseDirectiveCVFuncId();
  2174. case DK_CV_INLINE_SITE_ID:
  2175. return parseDirectiveCVInlineSiteId();
  2176. case DK_CV_LOC:
  2177. return parseDirectiveCVLoc();
  2178. case DK_CV_LINETABLE:
  2179. return parseDirectiveCVLinetable();
  2180. case DK_CV_INLINE_LINETABLE:
  2181. return parseDirectiveCVInlineLinetable();
  2182. case DK_CV_DEF_RANGE:
  2183. return parseDirectiveCVDefRange();
  2184. case DK_CV_STRING:
  2185. return parseDirectiveCVString();
  2186. case DK_CV_STRINGTABLE:
  2187. return parseDirectiveCVStringTable();
  2188. case DK_CV_FILECHECKSUMS:
  2189. return parseDirectiveCVFileChecksums();
  2190. case DK_CV_FILECHECKSUM_OFFSET:
  2191. return parseDirectiveCVFileChecksumOffset();
  2192. case DK_CV_FPO_DATA:
  2193. return parseDirectiveCVFPOData();
  2194. case DK_CFI_SECTIONS:
  2195. return parseDirectiveCFISections();
  2196. case DK_CFI_STARTPROC:
  2197. return parseDirectiveCFIStartProc();
  2198. case DK_CFI_ENDPROC:
  2199. return parseDirectiveCFIEndProc();
  2200. case DK_CFI_DEF_CFA:
  2201. return parseDirectiveCFIDefCfa(IDLoc);
  2202. case DK_CFI_DEF_CFA_OFFSET:
  2203. return parseDirectiveCFIDefCfaOffset();
  2204. case DK_CFI_ADJUST_CFA_OFFSET:
  2205. return parseDirectiveCFIAdjustCfaOffset();
  2206. case DK_CFI_DEF_CFA_REGISTER:
  2207. return parseDirectiveCFIDefCfaRegister(IDLoc);
  2208. case DK_CFI_OFFSET:
  2209. return parseDirectiveCFIOffset(IDLoc);
  2210. case DK_CFI_REL_OFFSET:
  2211. return parseDirectiveCFIRelOffset(IDLoc);
  2212. case DK_CFI_PERSONALITY:
  2213. return parseDirectiveCFIPersonalityOrLsda(true);
  2214. case DK_CFI_LSDA:
  2215. return parseDirectiveCFIPersonalityOrLsda(false);
  2216. case DK_CFI_REMEMBER_STATE:
  2217. return parseDirectiveCFIRememberState();
  2218. case DK_CFI_RESTORE_STATE:
  2219. return parseDirectiveCFIRestoreState();
  2220. case DK_CFI_SAME_VALUE:
  2221. return parseDirectiveCFISameValue(IDLoc);
  2222. case DK_CFI_RESTORE:
  2223. return parseDirectiveCFIRestore(IDLoc);
  2224. case DK_CFI_ESCAPE:
  2225. return parseDirectiveCFIEscape();
  2226. case DK_CFI_RETURN_COLUMN:
  2227. return parseDirectiveCFIReturnColumn(IDLoc);
  2228. case DK_CFI_SIGNAL_FRAME:
  2229. return parseDirectiveCFISignalFrame();
  2230. case DK_CFI_UNDEFINED:
  2231. return parseDirectiveCFIUndefined(IDLoc);
  2232. case DK_CFI_REGISTER:
  2233. return parseDirectiveCFIRegister(IDLoc);
  2234. case DK_CFI_WINDOW_SAVE:
  2235. return parseDirectiveCFIWindowSave();
  2236. case DK_EXITM:
  2237. Info.ExitValue = "";
  2238. return parseDirectiveExitMacro(IDLoc, IDVal, *Info.ExitValue);
  2239. case DK_ENDM:
  2240. Info.ExitValue = "";
  2241. return parseDirectiveEndMacro(IDVal);
  2242. case DK_PURGE:
  2243. return parseDirectivePurgeMacro(IDLoc);
  2244. case DK_END:
  2245. return parseDirectiveEnd(IDLoc);
  2246. case DK_ERR:
  2247. return parseDirectiveError(IDLoc);
  2248. case DK_ERRB:
  2249. return parseDirectiveErrorIfb(IDLoc, true);
  2250. case DK_ERRNB:
  2251. return parseDirectiveErrorIfb(IDLoc, false);
  2252. case DK_ERRDEF:
  2253. return parseDirectiveErrorIfdef(IDLoc, true);
  2254. case DK_ERRNDEF:
  2255. return parseDirectiveErrorIfdef(IDLoc, false);
  2256. case DK_ERRDIF:
  2257. return parseDirectiveErrorIfidn(IDLoc, /*ExpectEqual=*/false,
  2258. /*CaseInsensitive=*/false);
  2259. case DK_ERRDIFI:
  2260. return parseDirectiveErrorIfidn(IDLoc, /*ExpectEqual=*/false,
  2261. /*CaseInsensitive=*/true);
  2262. case DK_ERRIDN:
  2263. return parseDirectiveErrorIfidn(IDLoc, /*ExpectEqual=*/true,
  2264. /*CaseInsensitive=*/false);
  2265. case DK_ERRIDNI:
  2266. return parseDirectiveErrorIfidn(IDLoc, /*ExpectEqual=*/true,
  2267. /*CaseInsensitive=*/true);
  2268. case DK_ERRE:
  2269. return parseDirectiveErrorIfe(IDLoc, true);
  2270. case DK_ERRNZ:
  2271. return parseDirectiveErrorIfe(IDLoc, false);
  2272. case DK_RADIX:
  2273. return parseDirectiveRadix(IDLoc);
  2274. case DK_ECHO:
  2275. return parseDirectiveEcho(IDLoc);
  2276. }
  2277. return Error(IDLoc, "unknown directive");
  2278. }
  2279. // We also check if this is allocating memory with user-defined type.
  2280. auto IDIt = Structs.find(IDVal.lower());
  2281. if (IDIt != Structs.end())
  2282. return parseDirectiveStructValue(/*Structure=*/IDIt->getValue(), IDVal,
  2283. IDLoc);
  2284. // Non-conditional Microsoft directives sometimes follow their first argument.
  2285. const AsmToken nextTok = getTok();
  2286. const StringRef nextVal = nextTok.getString();
  2287. const SMLoc nextLoc = nextTok.getLoc();
  2288. const AsmToken afterNextTok = peekTok();
  2289. // There are several entities interested in parsing infix directives:
  2290. //
  2291. // 1. Asm parser extensions. For example, platform-specific parsers
  2292. // (like the ELF parser) register themselves as extensions.
  2293. // 2. The generic directive parser implemented by this class. These are
  2294. // all the directives that behave in a target and platform independent
  2295. // manner, or at least have a default behavior that's shared between
  2296. // all targets and platforms.
  2297. getTargetParser().flushPendingInstructions(getStreamer());
  2298. // Special-case handling of structure-end directives at higher priority, since
  2299. // ENDS is overloaded as a segment-end directive.
  2300. if (nextVal.equals_insensitive("ends") && StructInProgress.size() == 1) {
  2301. Lex();
  2302. return parseDirectiveEnds(IDVal, IDLoc);
  2303. }
  2304. // First, check the extension directive map to see if any extension has
  2305. // registered itself to parse this directive.
  2306. std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
  2307. ExtensionDirectiveMap.lookup(nextVal.lower());
  2308. if (Handler.first) {
  2309. Lex();
  2310. Lexer.UnLex(ID);
  2311. return (*Handler.second)(Handler.first, nextVal, nextLoc);
  2312. }
  2313. // If no one else is interested in this directive, it must be
  2314. // generic and familiar to this class.
  2315. DirKindIt = DirectiveKindMap.find(nextVal.lower());
  2316. DirKind = (DirKindIt == DirectiveKindMap.end())
  2317. ? DK_NO_DIRECTIVE
  2318. : DirKindIt->getValue();
  2319. switch (DirKind) {
  2320. default:
  2321. break;
  2322. case DK_ASSIGN:
  2323. case DK_EQU:
  2324. case DK_TEXTEQU:
  2325. Lex();
  2326. return parseDirectiveEquate(nextVal, IDVal, DirKind, IDLoc);
  2327. case DK_BYTE:
  2328. if (afterNextTok.is(AsmToken::Identifier) &&
  2329. afterNextTok.getString().equals_insensitive("ptr")) {
  2330. // Size directive; part of an instruction.
  2331. break;
  2332. }
  2333. LLVM_FALLTHROUGH;
  2334. case DK_SBYTE:
  2335. case DK_DB:
  2336. Lex();
  2337. return parseDirectiveNamedValue(nextVal, 1, IDVal, IDLoc);
  2338. case DK_WORD:
  2339. if (afterNextTok.is(AsmToken::Identifier) &&
  2340. afterNextTok.getString().equals_insensitive("ptr")) {
  2341. // Size directive; part of an instruction.
  2342. break;
  2343. }
  2344. LLVM_FALLTHROUGH;
  2345. case DK_SWORD:
  2346. case DK_DW:
  2347. Lex();
  2348. return parseDirectiveNamedValue(nextVal, 2, IDVal, IDLoc);
  2349. case DK_DWORD:
  2350. if (afterNextTok.is(AsmToken::Identifier) &&
  2351. afterNextTok.getString().equals_insensitive("ptr")) {
  2352. // Size directive; part of an instruction.
  2353. break;
  2354. }
  2355. LLVM_FALLTHROUGH;
  2356. case DK_SDWORD:
  2357. case DK_DD:
  2358. Lex();
  2359. return parseDirectiveNamedValue(nextVal, 4, IDVal, IDLoc);
  2360. case DK_FWORD:
  2361. if (afterNextTok.is(AsmToken::Identifier) &&
  2362. afterNextTok.getString().equals_insensitive("ptr")) {
  2363. // Size directive; part of an instruction.
  2364. break;
  2365. }
  2366. LLVM_FALLTHROUGH;
  2367. case DK_DF:
  2368. Lex();
  2369. return parseDirectiveNamedValue(nextVal, 6, IDVal, IDLoc);
  2370. case DK_QWORD:
  2371. if (afterNextTok.is(AsmToken::Identifier) &&
  2372. afterNextTok.getString().equals_insensitive("ptr")) {
  2373. // Size directive; part of an instruction.
  2374. break;
  2375. }
  2376. LLVM_FALLTHROUGH;
  2377. case DK_SQWORD:
  2378. case DK_DQ:
  2379. Lex();
  2380. return parseDirectiveNamedValue(nextVal, 8, IDVal, IDLoc);
  2381. case DK_REAL4:
  2382. Lex();
  2383. return parseDirectiveNamedRealValue(nextVal, APFloat::IEEEsingle(), 4,
  2384. IDVal, IDLoc);
  2385. case DK_REAL8:
  2386. Lex();
  2387. return parseDirectiveNamedRealValue(nextVal, APFloat::IEEEdouble(), 8,
  2388. IDVal, IDLoc);
  2389. case DK_REAL10:
  2390. Lex();
  2391. return parseDirectiveNamedRealValue(nextVal, APFloat::x87DoubleExtended(),
  2392. 10, IDVal, IDLoc);
  2393. case DK_STRUCT:
  2394. case DK_UNION:
  2395. Lex();
  2396. return parseDirectiveStruct(nextVal, DirKind, IDVal, IDLoc);
  2397. case DK_ENDS:
  2398. Lex();
  2399. return parseDirectiveEnds(IDVal, IDLoc);
  2400. case DK_MACRO:
  2401. Lex();
  2402. return parseDirectiveMacro(IDVal, IDLoc);
  2403. }
  2404. // Finally, we check if this is allocating a variable with user-defined type.
  2405. auto NextIt = Structs.find(nextVal.lower());
  2406. if (NextIt != Structs.end()) {
  2407. Lex();
  2408. return parseDirectiveNamedStructValue(/*Structure=*/NextIt->getValue(),
  2409. nextVal, nextLoc, IDVal);
  2410. }
  2411. // __asm _emit or __asm __emit
  2412. if (ParsingMSInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
  2413. IDVal == "_EMIT" || IDVal == "__EMIT"))
  2414. return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
  2415. // __asm align
  2416. if (ParsingMSInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
  2417. return parseDirectiveMSAlign(IDLoc, Info);
  2418. if (ParsingMSInlineAsm && (IDVal == "even" || IDVal == "EVEN"))
  2419. Info.AsmRewrites->emplace_back(AOK_EVEN, IDLoc, 4);
  2420. if (checkForValidSection())
  2421. return true;
  2422. // Canonicalize the opcode to lower case.
  2423. std::string OpcodeStr = IDVal.lower();
  2424. ParseInstructionInfo IInfo(Info.AsmRewrites);
  2425. bool ParseHadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, ID,
  2426. Info.ParsedOperands);
  2427. Info.ParseError = ParseHadError;
  2428. // Dump the parsed representation, if requested.
  2429. if (getShowParsedOperands()) {
  2430. SmallString<256> Str;
  2431. raw_svector_ostream OS(Str);
  2432. OS << "parsed instruction: [";
  2433. for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
  2434. if (i != 0)
  2435. OS << ", ";
  2436. Info.ParsedOperands[i]->print(OS);
  2437. }
  2438. OS << "]";
  2439. printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
  2440. }
  2441. // Fail even if ParseInstruction erroneously returns false.
  2442. if (hasPendingError() || ParseHadError)
  2443. return true;
  2444. // If we are generating dwarf for the current section then generate a .loc
  2445. // directive for the instruction.
  2446. if (!ParseHadError && enabledGenDwarfForAssembly() &&
  2447. getContext().getGenDwarfSectionSyms().count(
  2448. getStreamer().getCurrentSectionOnly())) {
  2449. unsigned Line;
  2450. if (ActiveMacros.empty())
  2451. Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
  2452. else
  2453. Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc,
  2454. ActiveMacros.front()->ExitBuffer);
  2455. // If we previously parsed a cpp hash file line comment then make sure the
  2456. // current Dwarf File is for the CppHashFilename if not then emit the
  2457. // Dwarf File table for it and adjust the line number for the .loc.
  2458. if (!CppHashInfo.Filename.empty()) {
  2459. unsigned FileNumber = getStreamer().emitDwarfFileDirective(
  2460. 0, StringRef(), CppHashInfo.Filename);
  2461. getContext().setGenDwarfFileNumber(FileNumber);
  2462. unsigned CppHashLocLineNo =
  2463. SrcMgr.FindLineNumber(CppHashInfo.Loc, CppHashInfo.Buf);
  2464. Line = CppHashInfo.LineNumber - 1 + (Line - CppHashLocLineNo);
  2465. }
  2466. getStreamer().emitDwarfLocDirective(
  2467. getContext().getGenDwarfFileNumber(), Line, 0,
  2468. DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
  2469. StringRef());
  2470. }
  2471. // If parsing succeeded, match the instruction.
  2472. if (!ParseHadError) {
  2473. uint64_t ErrorInfo;
  2474. if (getTargetParser().MatchAndEmitInstruction(
  2475. IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo,
  2476. getTargetParser().isParsingMSInlineAsm()))
  2477. return true;
  2478. }
  2479. return false;
  2480. }
  2481. // Parse and erase curly braces marking block start/end.
  2482. bool MasmParser::parseCurlyBlockScope(
  2483. SmallVectorImpl<AsmRewrite> &AsmStrRewrites) {
  2484. // Identify curly brace marking block start/end.
  2485. if (Lexer.isNot(AsmToken::LCurly) && Lexer.isNot(AsmToken::RCurly))
  2486. return false;
  2487. SMLoc StartLoc = Lexer.getLoc();
  2488. Lex(); // Eat the brace.
  2489. if (Lexer.is(AsmToken::EndOfStatement))
  2490. Lex(); // Eat EndOfStatement following the brace.
  2491. // Erase the block start/end brace from the output asm string.
  2492. AsmStrRewrites.emplace_back(AOK_Skip, StartLoc, Lexer.getLoc().getPointer() -
  2493. StartLoc.getPointer());
  2494. return true;
  2495. }
  2496. /// parseCppHashLineFilenameComment as this:
  2497. /// ::= # number "filename"
  2498. bool MasmParser::parseCppHashLineFilenameComment(SMLoc L) {
  2499. Lex(); // Eat the hash token.
  2500. // Lexer only ever emits HashDirective if it fully formed if it's
  2501. // done the checking already so this is an internal error.
  2502. assert(getTok().is(AsmToken::Integer) &&
  2503. "Lexing Cpp line comment: Expected Integer");
  2504. int64_t LineNumber = getTok().getIntVal();
  2505. Lex();
  2506. assert(getTok().is(AsmToken::String) &&
  2507. "Lexing Cpp line comment: Expected String");
  2508. StringRef Filename = getTok().getString();
  2509. Lex();
  2510. // Get rid of the enclosing quotes.
  2511. Filename = Filename.substr(1, Filename.size() - 2);
  2512. // Save the SMLoc, Filename and LineNumber for later use by diagnostics
  2513. // and possibly DWARF file info.
  2514. CppHashInfo.Loc = L;
  2515. CppHashInfo.Filename = Filename;
  2516. CppHashInfo.LineNumber = LineNumber;
  2517. CppHashInfo.Buf = CurBuffer;
  2518. if (FirstCppHashFilename.empty())
  2519. FirstCppHashFilename = Filename;
  2520. return false;
  2521. }
  2522. /// will use the last parsed cpp hash line filename comment
  2523. /// for the Filename and LineNo if any in the diagnostic.
  2524. void MasmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
  2525. const MasmParser *Parser = static_cast<const MasmParser *>(Context);
  2526. raw_ostream &OS = errs();
  2527. const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
  2528. SMLoc DiagLoc = Diag.getLoc();
  2529. unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
  2530. unsigned CppHashBuf =
  2531. Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashInfo.Loc);
  2532. // Like SourceMgr::printMessage() we need to print the include stack if any
  2533. // before printing the message.
  2534. unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
  2535. if (!Parser->SavedDiagHandler && DiagCurBuffer &&
  2536. DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
  2537. SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
  2538. DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
  2539. }
  2540. // If we have not parsed a cpp hash line filename comment or the source
  2541. // manager changed or buffer changed (like in a nested include) then just
  2542. // print the normal diagnostic using its Filename and LineNo.
  2543. if (!Parser->CppHashInfo.LineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
  2544. DiagBuf != CppHashBuf) {
  2545. if (Parser->SavedDiagHandler)
  2546. Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
  2547. else
  2548. Diag.print(nullptr, OS);
  2549. return;
  2550. }
  2551. // Use the CppHashFilename and calculate a line number based on the
  2552. // CppHashInfo.Loc and CppHashInfo.LineNumber relative to this Diag's SMLoc
  2553. // for the diagnostic.
  2554. const std::string &Filename = std::string(Parser->CppHashInfo.Filename);
  2555. int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
  2556. int CppHashLocLineNo =
  2557. Parser->SrcMgr.FindLineNumber(Parser->CppHashInfo.Loc, CppHashBuf);
  2558. int LineNo =
  2559. Parser->CppHashInfo.LineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
  2560. SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
  2561. Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
  2562. Diag.getLineContents(), Diag.getRanges());
  2563. if (Parser->SavedDiagHandler)
  2564. Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
  2565. else
  2566. NewDiag.print(nullptr, OS);
  2567. }
  2568. // This is similar to the IsIdentifierChar function in AsmLexer.cpp, but does
  2569. // not accept '.'.
  2570. static bool isMacroParameterChar(char C) {
  2571. return isAlnum(C) || C == '_' || C == '$' || C == '@' || C == '?';
  2572. }
  2573. bool MasmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
  2574. ArrayRef<MCAsmMacroParameter> Parameters,
  2575. ArrayRef<MCAsmMacroArgument> A,
  2576. const std::vector<std::string> &Locals, SMLoc L) {
  2577. unsigned NParameters = Parameters.size();
  2578. if (NParameters != A.size())
  2579. return Error(L, "Wrong number of arguments");
  2580. StringMap<std::string> LocalSymbols;
  2581. std::string Name;
  2582. Name.reserve(6);
  2583. for (StringRef Local : Locals) {
  2584. raw_string_ostream LocalName(Name);
  2585. LocalName << "??"
  2586. << format_hex_no_prefix(LocalCounter++, 4, /*Upper=*/true);
  2587. LocalSymbols.insert({Local, LocalName.str()});
  2588. Name.clear();
  2589. }
  2590. Optional<char> CurrentQuote;
  2591. while (!Body.empty()) {
  2592. // Scan for the next substitution.
  2593. std::size_t End = Body.size(), Pos = 0;
  2594. std::size_t IdentifierPos = End;
  2595. for (; Pos != End; ++Pos) {
  2596. // Find the next possible macro parameter, including preceding a '&'
  2597. // inside quotes.
  2598. if (Body[Pos] == '&')
  2599. break;
  2600. if (isMacroParameterChar(Body[Pos])) {
  2601. if (!CurrentQuote.hasValue())
  2602. break;
  2603. if (IdentifierPos == End)
  2604. IdentifierPos = Pos;
  2605. } else {
  2606. IdentifierPos = End;
  2607. }
  2608. // Track quotation status
  2609. if (!CurrentQuote.hasValue()) {
  2610. if (Body[Pos] == '\'' || Body[Pos] == '"')
  2611. CurrentQuote = Body[Pos];
  2612. } else if (Body[Pos] == CurrentQuote) {
  2613. if (Pos + 1 != End && Body[Pos + 1] == CurrentQuote) {
  2614. // Escaped quote, and quotes aren't identifier chars; skip
  2615. ++Pos;
  2616. continue;
  2617. } else {
  2618. CurrentQuote.reset();
  2619. }
  2620. }
  2621. }
  2622. if (IdentifierPos != End) {
  2623. // We've recognized an identifier before an apostrophe inside quotes;
  2624. // check once to see if we can expand it.
  2625. Pos = IdentifierPos;
  2626. IdentifierPos = End;
  2627. }
  2628. // Add the prefix.
  2629. OS << Body.slice(0, Pos);
  2630. // Check if we reached the end.
  2631. if (Pos == End)
  2632. break;
  2633. unsigned I = Pos;
  2634. bool InitialAmpersand = (Body[I] == '&');
  2635. if (InitialAmpersand) {
  2636. ++I;
  2637. ++Pos;
  2638. }
  2639. while (I < End && isMacroParameterChar(Body[I]))
  2640. ++I;
  2641. const char *Begin = Body.data() + Pos;
  2642. StringRef Argument(Begin, I - Pos);
  2643. const std::string ArgumentLower = Argument.lower();
  2644. unsigned Index = 0;
  2645. for (; Index < NParameters; ++Index)
  2646. if (Parameters[Index].Name.equals_insensitive(ArgumentLower))
  2647. break;
  2648. if (Index == NParameters) {
  2649. if (InitialAmpersand)
  2650. OS << '&';
  2651. auto it = LocalSymbols.find(ArgumentLower);
  2652. if (it != LocalSymbols.end())
  2653. OS << it->second;
  2654. else
  2655. OS << Argument;
  2656. Pos = I;
  2657. } else {
  2658. for (const AsmToken &Token : A[Index]) {
  2659. // In MASM, you can write '%expr'.
  2660. // The prefix '%' evaluates the expression 'expr'
  2661. // and uses the result as a string (e.g. replace %(1+2) with the
  2662. // string "3").
  2663. // Here, we identify the integer token which is the result of the
  2664. // absolute expression evaluation and replace it with its string
  2665. // representation.
  2666. if (Token.getString().front() == '%' && Token.is(AsmToken::Integer))
  2667. // Emit an integer value to the buffer.
  2668. OS << Token.getIntVal();
  2669. else
  2670. OS << Token.getString();
  2671. }
  2672. Pos += Argument.size();
  2673. if (Pos < End && Body[Pos] == '&') {
  2674. ++Pos;
  2675. }
  2676. }
  2677. // Update the scan point.
  2678. Body = Body.substr(Pos);
  2679. }
  2680. return false;
  2681. }
  2682. static bool isOperator(AsmToken::TokenKind kind) {
  2683. switch (kind) {
  2684. default:
  2685. return false;
  2686. case AsmToken::Plus:
  2687. case AsmToken::Minus:
  2688. case AsmToken::Tilde:
  2689. case AsmToken::Slash:
  2690. case AsmToken::Star:
  2691. case AsmToken::Dot:
  2692. case AsmToken::Equal:
  2693. case AsmToken::EqualEqual:
  2694. case AsmToken::Pipe:
  2695. case AsmToken::PipePipe:
  2696. case AsmToken::Caret:
  2697. case AsmToken::Amp:
  2698. case AsmToken::AmpAmp:
  2699. case AsmToken::Exclaim:
  2700. case AsmToken::ExclaimEqual:
  2701. case AsmToken::Less:
  2702. case AsmToken::LessEqual:
  2703. case AsmToken::LessLess:
  2704. case AsmToken::LessGreater:
  2705. case AsmToken::Greater:
  2706. case AsmToken::GreaterEqual:
  2707. case AsmToken::GreaterGreater:
  2708. return true;
  2709. }
  2710. }
  2711. namespace {
  2712. class AsmLexerSkipSpaceRAII {
  2713. public:
  2714. AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
  2715. Lexer.setSkipSpace(SkipSpace);
  2716. }
  2717. ~AsmLexerSkipSpaceRAII() {
  2718. Lexer.setSkipSpace(true);
  2719. }
  2720. private:
  2721. AsmLexer &Lexer;
  2722. };
  2723. } // end anonymous namespace
  2724. bool MasmParser::parseMacroArgument(const MCAsmMacroParameter *MP,
  2725. MCAsmMacroArgument &MA,
  2726. AsmToken::TokenKind EndTok) {
  2727. if (MP && MP->Vararg) {
  2728. if (Lexer.isNot(EndTok)) {
  2729. SmallVector<StringRef, 1> Str = parseStringRefsTo(EndTok);
  2730. for (StringRef S : Str) {
  2731. MA.emplace_back(AsmToken::String, S);
  2732. }
  2733. }
  2734. return false;
  2735. }
  2736. SMLoc StrLoc = Lexer.getLoc(), EndLoc;
  2737. if (Lexer.is(AsmToken::Less) && isAngleBracketString(StrLoc, EndLoc)) {
  2738. const char *StrChar = StrLoc.getPointer() + 1;
  2739. const char *EndChar = EndLoc.getPointer() - 1;
  2740. jumpToLoc(EndLoc, CurBuffer, EndStatementAtEOFStack.back());
  2741. /// Eat from '<' to '>'.
  2742. Lex();
  2743. MA.emplace_back(AsmToken::String, StringRef(StrChar, EndChar - StrChar));
  2744. return false;
  2745. }
  2746. unsigned ParenLevel = 0;
  2747. // Darwin doesn't use spaces to delmit arguments.
  2748. AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
  2749. bool SpaceEaten;
  2750. while (true) {
  2751. SpaceEaten = false;
  2752. if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
  2753. return TokError("unexpected token");
  2754. if (ParenLevel == 0) {
  2755. if (Lexer.is(AsmToken::Comma))
  2756. break;
  2757. if (Lexer.is(AsmToken::Space)) {
  2758. SpaceEaten = true;
  2759. Lex(); // Eat spaces.
  2760. }
  2761. // Spaces can delimit parameters, but could also be part an expression.
  2762. // If the token after a space is an operator, add the token and the next
  2763. // one into this argument
  2764. if (!IsDarwin) {
  2765. if (isOperator(Lexer.getKind()) && Lexer.isNot(EndTok)) {
  2766. MA.push_back(getTok());
  2767. Lex();
  2768. // Whitespace after an operator can be ignored.
  2769. if (Lexer.is(AsmToken::Space))
  2770. Lex();
  2771. continue;
  2772. }
  2773. }
  2774. if (SpaceEaten)
  2775. break;
  2776. }
  2777. // handleMacroEntry relies on not advancing the lexer here
  2778. // to be able to fill in the remaining default parameter values
  2779. if (Lexer.is(EndTok) && (EndTok != AsmToken::RParen || ParenLevel == 0))
  2780. break;
  2781. // Adjust the current parentheses level.
  2782. if (Lexer.is(AsmToken::LParen))
  2783. ++ParenLevel;
  2784. else if (Lexer.is(AsmToken::RParen) && ParenLevel)
  2785. --ParenLevel;
  2786. // Append the token to the current argument list.
  2787. MA.push_back(getTok());
  2788. Lex();
  2789. }
  2790. if (ParenLevel != 0)
  2791. return TokError("unbalanced parentheses in argument");
  2792. if (MA.empty() && MP) {
  2793. if (MP->Required) {
  2794. return TokError("missing value for required parameter '" + MP->Name +
  2795. "'");
  2796. } else {
  2797. MA = MP->Value;
  2798. }
  2799. }
  2800. return false;
  2801. }
  2802. // Parse the macro instantiation arguments.
  2803. bool MasmParser::parseMacroArguments(const MCAsmMacro *M,
  2804. MCAsmMacroArguments &A,
  2805. AsmToken::TokenKind EndTok) {
  2806. const unsigned NParameters = M ? M->Parameters.size() : 0;
  2807. bool NamedParametersFound = false;
  2808. SmallVector<SMLoc, 4> FALocs;
  2809. A.resize(NParameters);
  2810. FALocs.resize(NParameters);
  2811. // Parse two kinds of macro invocations:
  2812. // - macros defined without any parameters accept an arbitrary number of them
  2813. // - macros defined with parameters accept at most that many of them
  2814. for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
  2815. ++Parameter) {
  2816. SMLoc IDLoc = Lexer.getLoc();
  2817. MCAsmMacroParameter FA;
  2818. if (Lexer.is(AsmToken::Identifier) && peekTok().is(AsmToken::Equal)) {
  2819. if (parseIdentifier(FA.Name))
  2820. return Error(IDLoc, "invalid argument identifier for formal argument");
  2821. if (Lexer.isNot(AsmToken::Equal))
  2822. return TokError("expected '=' after formal parameter identifier");
  2823. Lex();
  2824. NamedParametersFound = true;
  2825. }
  2826. if (NamedParametersFound && FA.Name.empty())
  2827. return Error(IDLoc, "cannot mix positional and keyword arguments");
  2828. unsigned PI = Parameter;
  2829. if (!FA.Name.empty()) {
  2830. assert(M && "expected macro to be defined");
  2831. unsigned FAI = 0;
  2832. for (FAI = 0; FAI < NParameters; ++FAI)
  2833. if (M->Parameters[FAI].Name == FA.Name)
  2834. break;
  2835. if (FAI >= NParameters) {
  2836. return Error(IDLoc, "parameter named '" + FA.Name +
  2837. "' does not exist for macro '" + M->Name + "'");
  2838. }
  2839. PI = FAI;
  2840. }
  2841. const MCAsmMacroParameter *MP = nullptr;
  2842. if (M && PI < NParameters)
  2843. MP = &M->Parameters[PI];
  2844. SMLoc StrLoc = Lexer.getLoc();
  2845. SMLoc EndLoc;
  2846. if (Lexer.is(AsmToken::Percent)) {
  2847. const MCExpr *AbsoluteExp;
  2848. int64_t Value;
  2849. /// Eat '%'.
  2850. Lex();
  2851. if (parseExpression(AbsoluteExp, EndLoc))
  2852. return false;
  2853. if (!AbsoluteExp->evaluateAsAbsolute(Value,
  2854. getStreamer().getAssemblerPtr()))
  2855. return Error(StrLoc, "expected absolute expression");
  2856. const char *StrChar = StrLoc.getPointer();
  2857. const char *EndChar = EndLoc.getPointer();
  2858. AsmToken newToken(AsmToken::Integer,
  2859. StringRef(StrChar, EndChar - StrChar), Value);
  2860. FA.Value.push_back(newToken);
  2861. } else if (parseMacroArgument(MP, FA.Value, EndTok)) {
  2862. if (M)
  2863. return addErrorSuffix(" in '" + M->Name + "' macro");
  2864. else
  2865. return true;
  2866. }
  2867. if (!FA.Value.empty()) {
  2868. if (A.size() <= PI)
  2869. A.resize(PI + 1);
  2870. A[PI] = FA.Value;
  2871. if (FALocs.size() <= PI)
  2872. FALocs.resize(PI + 1);
  2873. FALocs[PI] = Lexer.getLoc();
  2874. }
  2875. // At the end of the statement, fill in remaining arguments that have
  2876. // default values. If there aren't any, then the next argument is
  2877. // required but missing
  2878. if (Lexer.is(EndTok)) {
  2879. bool Failure = false;
  2880. for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
  2881. if (A[FAI].empty()) {
  2882. if (M->Parameters[FAI].Required) {
  2883. Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
  2884. "missing value for required parameter "
  2885. "'" +
  2886. M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
  2887. Failure = true;
  2888. }
  2889. if (!M->Parameters[FAI].Value.empty())
  2890. A[FAI] = M->Parameters[FAI].Value;
  2891. }
  2892. }
  2893. return Failure;
  2894. }
  2895. if (Lexer.is(AsmToken::Comma))
  2896. Lex();
  2897. }
  2898. return TokError("too many positional arguments");
  2899. }
  2900. bool MasmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc,
  2901. AsmToken::TokenKind ArgumentEndTok) {
  2902. // Arbitrarily limit macro nesting depth (default matches 'as'). We can
  2903. // eliminate this, although we should protect against infinite loops.
  2904. unsigned MaxNestingDepth = AsmMacroMaxNestingDepth;
  2905. if (ActiveMacros.size() == MaxNestingDepth) {
  2906. std::ostringstream MaxNestingDepthError;
  2907. MaxNestingDepthError << "macros cannot be nested more than "
  2908. << MaxNestingDepth << " levels deep."
  2909. << " Use -asm-macro-max-nesting-depth to increase "
  2910. "this limit.";
  2911. return TokError(MaxNestingDepthError.str());
  2912. }
  2913. MCAsmMacroArguments A;
  2914. if (parseMacroArguments(M, A, ArgumentEndTok))
  2915. return true;
  2916. // Macro instantiation is lexical, unfortunately. We construct a new buffer
  2917. // to hold the macro body with substitutions.
  2918. SmallString<256> Buf;
  2919. StringRef Body = M->Body;
  2920. raw_svector_ostream OS(Buf);
  2921. if (expandMacro(OS, Body, M->Parameters, A, M->Locals, getTok().getLoc()))
  2922. return true;
  2923. // We include the endm in the buffer as our cue to exit the macro
  2924. // instantiation.
  2925. OS << "endm\n";
  2926. std::unique_ptr<MemoryBuffer> Instantiation =
  2927. MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
  2928. // Create the macro instantiation object and add to the current macro
  2929. // instantiation stack.
  2930. MacroInstantiation *MI = new MacroInstantiation{
  2931. NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()};
  2932. ActiveMacros.push_back(MI);
  2933. ++NumOfMacroInstantiations;
  2934. // Jump to the macro instantiation and prime the lexer.
  2935. CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
  2936. Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
  2937. EndStatementAtEOFStack.push_back(true);
  2938. Lex();
  2939. return false;
  2940. }
  2941. void MasmParser::handleMacroExit() {
  2942. // Jump to the token we should return to, and consume it.
  2943. EndStatementAtEOFStack.pop_back();
  2944. jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer,
  2945. EndStatementAtEOFStack.back());
  2946. Lex();
  2947. // Pop the instantiation entry.
  2948. delete ActiveMacros.back();
  2949. ActiveMacros.pop_back();
  2950. }
  2951. bool MasmParser::handleMacroInvocation(const MCAsmMacro *M, SMLoc NameLoc) {
  2952. if (!M->IsFunction)
  2953. return Error(NameLoc, "cannot invoke macro procedure as function");
  2954. if (parseToken(AsmToken::LParen, "invoking macro function '" + M->Name +
  2955. "' requires arguments in parentheses") ||
  2956. handleMacroEntry(M, NameLoc, AsmToken::RParen))
  2957. return true;
  2958. // Parse all statements in the macro, retrieving the exit value when it ends.
  2959. std::string ExitValue;
  2960. SmallVector<AsmRewrite, 4> AsmStrRewrites;
  2961. while (Lexer.isNot(AsmToken::Eof)) {
  2962. ParseStatementInfo Info(&AsmStrRewrites);
  2963. bool Parsed = parseStatement(Info, nullptr);
  2964. if (!Parsed && Info.ExitValue.hasValue()) {
  2965. ExitValue = std::move(*Info.ExitValue);
  2966. break;
  2967. }
  2968. // If we have a Lexer Error we are on an Error Token. Load in Lexer Error
  2969. // for printing ErrMsg via Lex() only if no (presumably better) parser error
  2970. // exists.
  2971. if (Parsed && !hasPendingError() && Lexer.getTok().is(AsmToken::Error)) {
  2972. Lex();
  2973. }
  2974. // parseStatement returned true so may need to emit an error.
  2975. printPendingErrors();
  2976. // Skipping to the next line if needed.
  2977. if (Parsed && !getLexer().isAtStartOfStatement())
  2978. eatToEndOfStatement();
  2979. }
  2980. // Consume the right-parenthesis on the other side of the arguments.
  2981. if (parseRParen())
  2982. return true;
  2983. // Exit values may require lexing, unfortunately. We construct a new buffer to
  2984. // hold the exit value.
  2985. std::unique_ptr<MemoryBuffer> MacroValue =
  2986. MemoryBuffer::getMemBufferCopy(ExitValue, "<macro-value>");
  2987. // Jump from this location to the instantiated exit value, and prime the
  2988. // lexer.
  2989. CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(MacroValue), Lexer.getLoc());
  2990. Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(), nullptr,
  2991. /*EndStatementAtEOF=*/false);
  2992. EndStatementAtEOFStack.push_back(false);
  2993. Lex();
  2994. return false;
  2995. }
  2996. /// parseIdentifier:
  2997. /// ::= identifier
  2998. /// ::= string
  2999. bool MasmParser::parseIdentifier(StringRef &Res,
  3000. IdentifierPositionKind Position) {
  3001. // The assembler has relaxed rules for accepting identifiers, in particular we
  3002. // allow things like '.globl $foo' and '.def @feat.00', which would normally
  3003. // be separate tokens. At this level, we have already lexed so we cannot
  3004. // (currently) handle this as a context dependent token, instead we detect
  3005. // adjacent tokens and return the combined identifier.
  3006. if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
  3007. SMLoc PrefixLoc = getLexer().getLoc();
  3008. // Consume the prefix character, and check for a following identifier.
  3009. AsmToken nextTok = peekTok(false);
  3010. if (nextTok.isNot(AsmToken::Identifier))
  3011. return true;
  3012. // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
  3013. if (PrefixLoc.getPointer() + 1 != nextTok.getLoc().getPointer())
  3014. return true;
  3015. // eat $ or @
  3016. Lexer.Lex(); // Lexer's Lex guarantees consecutive token.
  3017. // Construct the joined identifier and consume the token.
  3018. Res =
  3019. StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
  3020. Lex(); // Parser Lex to maintain invariants.
  3021. return false;
  3022. }
  3023. if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
  3024. return true;
  3025. Res = getTok().getIdentifier();
  3026. // Consume the identifier token - but if parsing certain directives, avoid
  3027. // lexical expansion of the next token.
  3028. ExpandKind ExpandNextToken = ExpandMacros;
  3029. if (Position == StartOfStatement &&
  3030. StringSwitch<bool>(Res)
  3031. .CaseLower("echo", true)
  3032. .CasesLower("ifdef", "ifndef", "elseifdef", "elseifndef", true)
  3033. .Default(false)) {
  3034. ExpandNextToken = DoNotExpandMacros;
  3035. }
  3036. Lex(ExpandNextToken);
  3037. return false;
  3038. }
  3039. /// parseDirectiveEquate:
  3040. /// ::= name "=" expression
  3041. /// | name "equ" expression (not redefinable)
  3042. /// | name "equ" text-list
  3043. /// | name "textequ" text-list (redefinability unspecified)
  3044. bool MasmParser::parseDirectiveEquate(StringRef IDVal, StringRef Name,
  3045. DirectiveKind DirKind, SMLoc NameLoc) {
  3046. auto BuiltinIt = BuiltinSymbolMap.find(Name.lower());
  3047. if (BuiltinIt != BuiltinSymbolMap.end())
  3048. return Error(NameLoc, "cannot redefine a built-in symbol");
  3049. Variable &Var = Variables[Name.lower()];
  3050. if (Var.Name.empty()) {
  3051. Var.Name = Name;
  3052. }
  3053. SMLoc StartLoc = Lexer.getLoc();
  3054. if (DirKind == DK_EQU || DirKind == DK_TEXTEQU) {
  3055. // "equ" and "textequ" both allow text expressions.
  3056. std::string Value;
  3057. std::string TextItem;
  3058. if (!parseTextItem(TextItem)) {
  3059. Value += TextItem;
  3060. // Accept a text-list, not just one text-item.
  3061. auto parseItem = [&]() -> bool {
  3062. if (parseTextItem(TextItem))
  3063. return TokError("expected text item");
  3064. Value += TextItem;
  3065. return false;
  3066. };
  3067. if (parseOptionalToken(AsmToken::Comma) && parseMany(parseItem))
  3068. return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
  3069. if (!Var.IsText || Var.TextValue != Value) {
  3070. switch (Var.Redefinable) {
  3071. case Variable::NOT_REDEFINABLE:
  3072. return Error(getTok().getLoc(), "invalid variable redefinition");
  3073. case Variable::WARN_ON_REDEFINITION:
  3074. if (Warning(NameLoc, "redefining '" + Name +
  3075. "', already defined on the command line")) {
  3076. return true;
  3077. }
  3078. break;
  3079. default:
  3080. break;
  3081. }
  3082. }
  3083. Var.IsText = true;
  3084. Var.TextValue = Value;
  3085. Var.Redefinable = Variable::REDEFINABLE;
  3086. return false;
  3087. }
  3088. }
  3089. if (DirKind == DK_TEXTEQU)
  3090. return TokError("expected <text> in '" + Twine(IDVal) + "' directive");
  3091. // Parse as expression assignment.
  3092. const MCExpr *Expr;
  3093. SMLoc EndLoc;
  3094. if (parseExpression(Expr, EndLoc))
  3095. return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
  3096. StringRef ExprAsString = StringRef(
  3097. StartLoc.getPointer(), EndLoc.getPointer() - StartLoc.getPointer());
  3098. int64_t Value;
  3099. if (!Expr->evaluateAsAbsolute(Value, getStreamer().getAssemblerPtr())) {
  3100. if (DirKind == DK_ASSIGN)
  3101. return Error(
  3102. StartLoc,
  3103. "expected absolute expression; not all symbols have known values",
  3104. {StartLoc, EndLoc});
  3105. // Not an absolute expression; define as a text replacement.
  3106. if (!Var.IsText || Var.TextValue != ExprAsString) {
  3107. switch (Var.Redefinable) {
  3108. case Variable::NOT_REDEFINABLE:
  3109. return Error(getTok().getLoc(), "invalid variable redefinition");
  3110. case Variable::WARN_ON_REDEFINITION:
  3111. if (Warning(NameLoc, "redefining '" + Name +
  3112. "', already defined on the command line")) {
  3113. return true;
  3114. }
  3115. break;
  3116. default:
  3117. break;
  3118. }
  3119. }
  3120. Var.IsText = true;
  3121. Var.TextValue = ExprAsString.str();
  3122. Var.Redefinable = Variable::REDEFINABLE;
  3123. return false;
  3124. }
  3125. MCSymbol *Sym = getContext().getOrCreateSymbol(Var.Name);
  3126. const MCConstantExpr *PrevValue =
  3127. Sym->isVariable() ? dyn_cast_or_null<MCConstantExpr>(
  3128. Sym->getVariableValue(/*SetUsed=*/false))
  3129. : nullptr;
  3130. if (Var.IsText || !PrevValue || PrevValue->getValue() != Value) {
  3131. switch (Var.Redefinable) {
  3132. case Variable::NOT_REDEFINABLE:
  3133. return Error(getTok().getLoc(), "invalid variable redefinition");
  3134. case Variable::WARN_ON_REDEFINITION:
  3135. if (Warning(NameLoc, "redefining '" + Name +
  3136. "', already defined on the command line")) {
  3137. return true;
  3138. }
  3139. break;
  3140. default:
  3141. break;
  3142. }
  3143. }
  3144. Var.IsText = false;
  3145. Var.TextValue.clear();
  3146. Var.Redefinable = (DirKind == DK_ASSIGN) ? Variable::REDEFINABLE
  3147. : Variable::NOT_REDEFINABLE;
  3148. Sym->setRedefinable(Var.Redefinable != Variable::NOT_REDEFINABLE);
  3149. Sym->setVariableValue(Expr);
  3150. Sym->setExternal(false);
  3151. return false;
  3152. }
  3153. bool MasmParser::parseEscapedString(std::string &Data) {
  3154. if (check(getTok().isNot(AsmToken::String), "expected string"))
  3155. return true;
  3156. Data = "";
  3157. char Quote = getTok().getString().front();
  3158. StringRef Str = getTok().getStringContents();
  3159. Data.reserve(Str.size());
  3160. for (size_t i = 0, e = Str.size(); i != e; ++i) {
  3161. Data.push_back(Str[i]);
  3162. if (Str[i] == Quote) {
  3163. // MASM treats doubled delimiting quotes as an escaped delimiting quote.
  3164. // If we're escaping the string's trailing delimiter, we're definitely
  3165. // missing a quotation mark.
  3166. if (i + 1 == Str.size())
  3167. return Error(getTok().getLoc(), "missing quotation mark in string");
  3168. if (Str[i + 1] == Quote)
  3169. ++i;
  3170. }
  3171. }
  3172. Lex();
  3173. return false;
  3174. }
  3175. bool MasmParser::parseAngleBracketString(std::string &Data) {
  3176. SMLoc EndLoc, StartLoc = getTok().getLoc();
  3177. if (isAngleBracketString(StartLoc, EndLoc)) {
  3178. const char *StartChar = StartLoc.getPointer() + 1;
  3179. const char *EndChar = EndLoc.getPointer() - 1;
  3180. jumpToLoc(EndLoc, CurBuffer, EndStatementAtEOFStack.back());
  3181. // Eat from '<' to '>'.
  3182. Lex();
  3183. Data = angleBracketString(StringRef(StartChar, EndChar - StartChar));
  3184. return false;
  3185. }
  3186. return true;
  3187. }
  3188. /// textItem ::= textLiteral | textMacroID | % constExpr
  3189. bool MasmParser::parseTextItem(std::string &Data) {
  3190. switch (getTok().getKind()) {
  3191. default:
  3192. return true;
  3193. case AsmToken::Percent: {
  3194. int64_t Res;
  3195. if (parseToken(AsmToken::Percent) || parseAbsoluteExpression(Res))
  3196. return true;
  3197. Data = std::to_string(Res);
  3198. return false;
  3199. }
  3200. case AsmToken::Less:
  3201. case AsmToken::LessEqual:
  3202. case AsmToken::LessLess:
  3203. case AsmToken::LessGreater:
  3204. return parseAngleBracketString(Data);
  3205. case AsmToken::Identifier: {
  3206. // This must be a text macro; we need to expand it accordingly.
  3207. StringRef ID;
  3208. SMLoc StartLoc = getTok().getLoc();
  3209. if (parseIdentifier(ID))
  3210. return true;
  3211. Data = ID.str();
  3212. bool Expanded = false;
  3213. while (true) {
  3214. // Try to resolve as a built-in text macro
  3215. auto BuiltinIt = BuiltinSymbolMap.find(ID.lower());
  3216. if (BuiltinIt != BuiltinSymbolMap.end()) {
  3217. llvm::Optional<std::string> BuiltinText =
  3218. evaluateBuiltinTextMacro(BuiltinIt->getValue(), StartLoc);
  3219. if (!BuiltinText.hasValue()) {
  3220. // Not a text macro; break without substituting
  3221. break;
  3222. }
  3223. Data = std::move(*BuiltinText);
  3224. ID = StringRef(Data);
  3225. Expanded = true;
  3226. continue;
  3227. }
  3228. // Try to resolve as a variable text macro
  3229. auto VarIt = Variables.find(ID.lower());
  3230. if (VarIt != Variables.end()) {
  3231. const Variable &Var = VarIt->getValue();
  3232. if (!Var.IsText) {
  3233. // Not a text macro; break without substituting
  3234. break;
  3235. }
  3236. Data = Var.TextValue;
  3237. ID = StringRef(Data);
  3238. Expanded = true;
  3239. continue;
  3240. }
  3241. break;
  3242. }
  3243. if (!Expanded) {
  3244. // Not a text macro; not usable in TextItem context. Since we haven't used
  3245. // the token, put it back for better error recovery.
  3246. getLexer().UnLex(AsmToken(AsmToken::Identifier, ID));
  3247. return true;
  3248. }
  3249. return false;
  3250. }
  3251. }
  3252. llvm_unreachable("unhandled token kind");
  3253. }
  3254. /// parseDirectiveAscii:
  3255. /// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
  3256. bool MasmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
  3257. auto parseOp = [&]() -> bool {
  3258. std::string Data;
  3259. if (checkForValidSection() || parseEscapedString(Data))
  3260. return true;
  3261. getStreamer().emitBytes(Data);
  3262. if (ZeroTerminated)
  3263. getStreamer().emitBytes(StringRef("\0", 1));
  3264. return false;
  3265. };
  3266. if (parseMany(parseOp))
  3267. return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
  3268. return false;
  3269. }
  3270. bool MasmParser::emitIntValue(const MCExpr *Value, unsigned Size) {
  3271. // Special case constant expressions to match code generator.
  3272. if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
  3273. assert(Size <= 8 && "Invalid size");
  3274. int64_t IntValue = MCE->getValue();
  3275. if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
  3276. return Error(MCE->getLoc(), "out of range literal value");
  3277. getStreamer().emitIntValue(IntValue, Size);
  3278. } else {
  3279. const MCSymbolRefExpr *MSE = dyn_cast<MCSymbolRefExpr>(Value);
  3280. if (MSE && MSE->getSymbol().getName() == "?") {
  3281. // ? initializer; treat as 0.
  3282. getStreamer().emitIntValue(0, Size);
  3283. } else {
  3284. getStreamer().emitValue(Value, Size, Value->getLoc());
  3285. }
  3286. }
  3287. return false;
  3288. }
  3289. bool MasmParser::parseScalarInitializer(unsigned Size,
  3290. SmallVectorImpl<const MCExpr *> &Values,
  3291. unsigned StringPadLength) {
  3292. if (Size == 1 && getTok().is(AsmToken::String)) {
  3293. std::string Value;
  3294. if (parseEscapedString(Value))
  3295. return true;
  3296. // Treat each character as an initializer.
  3297. for (const unsigned char CharVal : Value)
  3298. Values.push_back(MCConstantExpr::create(CharVal, getContext()));
  3299. // Pad the string with spaces to the specified length.
  3300. for (size_t i = Value.size(); i < StringPadLength; ++i)
  3301. Values.push_back(MCConstantExpr::create(' ', getContext()));
  3302. } else {
  3303. const MCExpr *Value;
  3304. if (parseExpression(Value))
  3305. return true;
  3306. if (getTok().is(AsmToken::Identifier) &&
  3307. getTok().getString().equals_insensitive("dup")) {
  3308. Lex(); // Eat 'dup'.
  3309. const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
  3310. if (!MCE)
  3311. return Error(Value->getLoc(),
  3312. "cannot repeat value a non-constant number of times");
  3313. const int64_t Repetitions = MCE->getValue();
  3314. if (Repetitions < 0)
  3315. return Error(Value->getLoc(),
  3316. "cannot repeat value a negative number of times");
  3317. SmallVector<const MCExpr *, 1> DuplicatedValues;
  3318. if (parseToken(AsmToken::LParen,
  3319. "parentheses required for 'dup' contents") ||
  3320. parseScalarInstList(Size, DuplicatedValues) || parseRParen())
  3321. return true;
  3322. for (int i = 0; i < Repetitions; ++i)
  3323. Values.append(DuplicatedValues.begin(), DuplicatedValues.end());
  3324. } else {
  3325. Values.push_back(Value);
  3326. }
  3327. }
  3328. return false;
  3329. }
  3330. bool MasmParser::parseScalarInstList(unsigned Size,
  3331. SmallVectorImpl<const MCExpr *> &Values,
  3332. const AsmToken::TokenKind EndToken) {
  3333. while (getTok().isNot(EndToken) &&
  3334. (EndToken != AsmToken::Greater ||
  3335. getTok().isNot(AsmToken::GreaterGreater))) {
  3336. parseScalarInitializer(Size, Values);
  3337. // If we see a comma, continue, and allow line continuation.
  3338. if (!parseOptionalToken(AsmToken::Comma))
  3339. break;
  3340. parseOptionalToken(AsmToken::EndOfStatement);
  3341. }
  3342. return false;
  3343. }
  3344. bool MasmParser::emitIntegralValues(unsigned Size, unsigned *Count) {
  3345. SmallVector<const MCExpr *, 1> Values;
  3346. if (checkForValidSection() || parseScalarInstList(Size, Values))
  3347. return true;
  3348. for (auto Value : Values) {
  3349. emitIntValue(Value, Size);
  3350. }
  3351. if (Count)
  3352. *Count = Values.size();
  3353. return false;
  3354. }
  3355. // Add a field to the current structure.
  3356. bool MasmParser::addIntegralField(StringRef Name, unsigned Size) {
  3357. StructInfo &Struct = StructInProgress.back();
  3358. FieldInfo &Field = Struct.addField(Name, FT_INTEGRAL, Size);
  3359. IntFieldInfo &IntInfo = Field.Contents.IntInfo;
  3360. Field.Type = Size;
  3361. if (parseScalarInstList(Size, IntInfo.Values))
  3362. return true;
  3363. Field.SizeOf = Field.Type * IntInfo.Values.size();
  3364. Field.LengthOf = IntInfo.Values.size();
  3365. const unsigned FieldEnd = Field.Offset + Field.SizeOf;
  3366. if (!Struct.IsUnion) {
  3367. Struct.NextOffset = FieldEnd;
  3368. }
  3369. Struct.Size = std::max(Struct.Size, FieldEnd);
  3370. return false;
  3371. }
  3372. /// parseDirectiveValue
  3373. /// ::= (byte | word | ... ) [ expression (, expression)* ]
  3374. bool MasmParser::parseDirectiveValue(StringRef IDVal, unsigned Size) {
  3375. if (StructInProgress.empty()) {
  3376. // Initialize data value.
  3377. if (emitIntegralValues(Size))
  3378. return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
  3379. } else if (addIntegralField("", Size)) {
  3380. return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
  3381. }
  3382. return false;
  3383. }
  3384. /// parseDirectiveNamedValue
  3385. /// ::= name (byte | word | ... ) [ expression (, expression)* ]
  3386. bool MasmParser::parseDirectiveNamedValue(StringRef TypeName, unsigned Size,
  3387. StringRef Name, SMLoc NameLoc) {
  3388. if (StructInProgress.empty()) {
  3389. // Initialize named data value.
  3390. MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
  3391. getStreamer().emitLabel(Sym);
  3392. unsigned Count;
  3393. if (emitIntegralValues(Size, &Count))
  3394. return addErrorSuffix(" in '" + Twine(TypeName) + "' directive");
  3395. AsmTypeInfo Type;
  3396. Type.Name = TypeName;
  3397. Type.Size = Size * Count;
  3398. Type.ElementSize = Size;
  3399. Type.Length = Count;
  3400. KnownType[Name.lower()] = Type;
  3401. } else if (addIntegralField(Name, Size)) {
  3402. return addErrorSuffix(" in '" + Twine(TypeName) + "' directive");
  3403. }
  3404. return false;
  3405. }
  3406. static bool parseHexOcta(MasmParser &Asm, uint64_t &hi, uint64_t &lo) {
  3407. if (Asm.getTok().isNot(AsmToken::Integer) &&
  3408. Asm.getTok().isNot(AsmToken::BigNum))
  3409. return Asm.TokError("unknown token in expression");
  3410. SMLoc ExprLoc = Asm.getTok().getLoc();
  3411. APInt IntValue = Asm.getTok().getAPIntVal();
  3412. Asm.Lex();
  3413. if (!IntValue.isIntN(128))
  3414. return Asm.Error(ExprLoc, "out of range literal value");
  3415. if (!IntValue.isIntN(64)) {
  3416. hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
  3417. lo = IntValue.getLoBits(64).getZExtValue();
  3418. } else {
  3419. hi = 0;
  3420. lo = IntValue.getZExtValue();
  3421. }
  3422. return false;
  3423. }
  3424. bool MasmParser::parseRealValue(const fltSemantics &Semantics, APInt &Res) {
  3425. // We don't truly support arithmetic on floating point expressions, so we
  3426. // have to manually parse unary prefixes.
  3427. bool IsNeg = false;
  3428. SMLoc SignLoc;
  3429. if (getLexer().is(AsmToken::Minus)) {
  3430. SignLoc = getLexer().getLoc();
  3431. Lexer.Lex();
  3432. IsNeg = true;
  3433. } else if (getLexer().is(AsmToken::Plus)) {
  3434. SignLoc = getLexer().getLoc();
  3435. Lexer.Lex();
  3436. }
  3437. if (Lexer.is(AsmToken::Error))
  3438. return TokError(Lexer.getErr());
  3439. if (Lexer.isNot(AsmToken::Integer) && Lexer.isNot(AsmToken::Real) &&
  3440. Lexer.isNot(AsmToken::Identifier))
  3441. return TokError("unexpected token in directive");
  3442. // Convert to an APFloat.
  3443. APFloat Value(Semantics);
  3444. StringRef IDVal = getTok().getString();
  3445. if (getLexer().is(AsmToken::Identifier)) {
  3446. if (IDVal.equals_insensitive("infinity") || IDVal.equals_insensitive("inf"))
  3447. Value = APFloat::getInf(Semantics);
  3448. else if (IDVal.equals_insensitive("nan"))
  3449. Value = APFloat::getNaN(Semantics, false, ~0);
  3450. else if (IDVal.equals_insensitive("?"))
  3451. Value = APFloat::getZero(Semantics);
  3452. else
  3453. return TokError("invalid floating point literal");
  3454. } else if (IDVal.consume_back("r") || IDVal.consume_back("R")) {
  3455. // MASM hexadecimal floating-point literal; no APFloat conversion needed.
  3456. // To match ML64.exe, ignore the initial sign.
  3457. unsigned SizeInBits = Value.getSizeInBits(Semantics);
  3458. if (SizeInBits != (IDVal.size() << 2))
  3459. return TokError("invalid floating point literal");
  3460. // Consume the numeric token.
  3461. Lex();
  3462. Res = APInt(SizeInBits, IDVal, 16);
  3463. if (SignLoc.isValid())
  3464. return Warning(SignLoc, "MASM-style hex floats ignore explicit sign");
  3465. return false;
  3466. } else if (errorToBool(
  3467. Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven)
  3468. .takeError())) {
  3469. return TokError("invalid floating point literal");
  3470. }
  3471. if (IsNeg)
  3472. Value.changeSign();
  3473. // Consume the numeric token.
  3474. Lex();
  3475. Res = Value.bitcastToAPInt();
  3476. return false;
  3477. }
  3478. bool MasmParser::parseRealInstList(const fltSemantics &Semantics,
  3479. SmallVectorImpl<APInt> &ValuesAsInt,
  3480. const AsmToken::TokenKind EndToken) {
  3481. while (getTok().isNot(EndToken) ||
  3482. (EndToken == AsmToken::Greater &&
  3483. getTok().isNot(AsmToken::GreaterGreater))) {
  3484. const AsmToken NextTok = peekTok();
  3485. if (NextTok.is(AsmToken::Identifier) &&
  3486. NextTok.getString().equals_insensitive("dup")) {
  3487. const MCExpr *Value;
  3488. if (parseExpression(Value) || parseToken(AsmToken::Identifier))
  3489. return true;
  3490. const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
  3491. if (!MCE)
  3492. return Error(Value->getLoc(),
  3493. "cannot repeat value a non-constant number of times");
  3494. const int64_t Repetitions = MCE->getValue();
  3495. if (Repetitions < 0)
  3496. return Error(Value->getLoc(),
  3497. "cannot repeat value a negative number of times");
  3498. SmallVector<APInt, 1> DuplicatedValues;
  3499. if (parseToken(AsmToken::LParen,
  3500. "parentheses required for 'dup' contents") ||
  3501. parseRealInstList(Semantics, DuplicatedValues) || parseRParen())
  3502. return true;
  3503. for (int i = 0; i < Repetitions; ++i)
  3504. ValuesAsInt.append(DuplicatedValues.begin(), DuplicatedValues.end());
  3505. } else {
  3506. APInt AsInt;
  3507. if (parseRealValue(Semantics, AsInt))
  3508. return true;
  3509. ValuesAsInt.push_back(AsInt);
  3510. }
  3511. // Continue if we see a comma. (Also, allow line continuation.)
  3512. if (!parseOptionalToken(AsmToken::Comma))
  3513. break;
  3514. parseOptionalToken(AsmToken::EndOfStatement);
  3515. }
  3516. return false;
  3517. }
  3518. // Initialize real data values.
  3519. bool MasmParser::emitRealValues(const fltSemantics &Semantics,
  3520. unsigned *Count) {
  3521. if (checkForValidSection())
  3522. return true;
  3523. SmallVector<APInt, 1> ValuesAsInt;
  3524. if (parseRealInstList(Semantics, ValuesAsInt))
  3525. return true;
  3526. for (const APInt &AsInt : ValuesAsInt) {
  3527. getStreamer().emitIntValue(AsInt);
  3528. }
  3529. if (Count)
  3530. *Count = ValuesAsInt.size();
  3531. return false;
  3532. }
  3533. // Add a real field to the current struct.
  3534. bool MasmParser::addRealField(StringRef Name, const fltSemantics &Semantics,
  3535. size_t Size) {
  3536. StructInfo &Struct = StructInProgress.back();
  3537. FieldInfo &Field = Struct.addField(Name, FT_REAL, Size);
  3538. RealFieldInfo &RealInfo = Field.Contents.RealInfo;
  3539. Field.SizeOf = 0;
  3540. if (parseRealInstList(Semantics, RealInfo.AsIntValues))
  3541. return true;
  3542. Field.Type = RealInfo.AsIntValues.back().getBitWidth() / 8;
  3543. Field.LengthOf = RealInfo.AsIntValues.size();
  3544. Field.SizeOf = Field.Type * Field.LengthOf;
  3545. const unsigned FieldEnd = Field.Offset + Field.SizeOf;
  3546. if (!Struct.IsUnion) {
  3547. Struct.NextOffset = FieldEnd;
  3548. }
  3549. Struct.Size = std::max(Struct.Size, FieldEnd);
  3550. return false;
  3551. }
  3552. /// parseDirectiveRealValue
  3553. /// ::= (real4 | real8 | real10) [ expression (, expression)* ]
  3554. bool MasmParser::parseDirectiveRealValue(StringRef IDVal,
  3555. const fltSemantics &Semantics,
  3556. size_t Size) {
  3557. if (StructInProgress.empty()) {
  3558. // Initialize data value.
  3559. if (emitRealValues(Semantics))
  3560. return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
  3561. } else if (addRealField("", Semantics, Size)) {
  3562. return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
  3563. }
  3564. return false;
  3565. }
  3566. /// parseDirectiveNamedRealValue
  3567. /// ::= name (real4 | real8 | real10) [ expression (, expression)* ]
  3568. bool MasmParser::parseDirectiveNamedRealValue(StringRef TypeName,
  3569. const fltSemantics &Semantics,
  3570. unsigned Size, StringRef Name,
  3571. SMLoc NameLoc) {
  3572. if (StructInProgress.empty()) {
  3573. // Initialize named data value.
  3574. MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
  3575. getStreamer().emitLabel(Sym);
  3576. unsigned Count;
  3577. if (emitRealValues(Semantics, &Count))
  3578. return addErrorSuffix(" in '" + TypeName + "' directive");
  3579. AsmTypeInfo Type;
  3580. Type.Name = TypeName;
  3581. Type.Size = Size * Count;
  3582. Type.ElementSize = Size;
  3583. Type.Length = Count;
  3584. KnownType[Name.lower()] = Type;
  3585. } else if (addRealField(Name, Semantics, Size)) {
  3586. return addErrorSuffix(" in '" + TypeName + "' directive");
  3587. }
  3588. return false;
  3589. }
  3590. bool MasmParser::parseOptionalAngleBracketOpen() {
  3591. const AsmToken Tok = getTok();
  3592. if (parseOptionalToken(AsmToken::LessLess)) {
  3593. AngleBracketDepth++;
  3594. Lexer.UnLex(AsmToken(AsmToken::Less, Tok.getString().substr(1)));
  3595. return true;
  3596. } else if (parseOptionalToken(AsmToken::LessGreater)) {
  3597. AngleBracketDepth++;
  3598. Lexer.UnLex(AsmToken(AsmToken::Greater, Tok.getString().substr(1)));
  3599. return true;
  3600. } else if (parseOptionalToken(AsmToken::Less)) {
  3601. AngleBracketDepth++;
  3602. return true;
  3603. }
  3604. return false;
  3605. }
  3606. bool MasmParser::parseAngleBracketClose(const Twine &Msg) {
  3607. const AsmToken Tok = getTok();
  3608. if (parseOptionalToken(AsmToken::GreaterGreater)) {
  3609. Lexer.UnLex(AsmToken(AsmToken::Greater, Tok.getString().substr(1)));
  3610. } else if (parseToken(AsmToken::Greater, Msg)) {
  3611. return true;
  3612. }
  3613. AngleBracketDepth--;
  3614. return false;
  3615. }
  3616. bool MasmParser::parseFieldInitializer(const FieldInfo &Field,
  3617. const IntFieldInfo &Contents,
  3618. FieldInitializer &Initializer) {
  3619. SMLoc Loc = getTok().getLoc();
  3620. SmallVector<const MCExpr *, 1> Values;
  3621. if (parseOptionalToken(AsmToken::LCurly)) {
  3622. if (Field.LengthOf == 1 && Field.Type > 1)
  3623. return Error(Loc, "Cannot initialize scalar field with array value");
  3624. if (parseScalarInstList(Field.Type, Values, AsmToken::RCurly) ||
  3625. parseToken(AsmToken::RCurly))
  3626. return true;
  3627. } else if (parseOptionalAngleBracketOpen()) {
  3628. if (Field.LengthOf == 1 && Field.Type > 1)
  3629. return Error(Loc, "Cannot initialize scalar field with array value");
  3630. if (parseScalarInstList(Field.Type, Values, AsmToken::Greater) ||
  3631. parseAngleBracketClose())
  3632. return true;
  3633. } else if (Field.LengthOf > 1 && Field.Type > 1) {
  3634. return Error(Loc, "Cannot initialize array field with scalar value");
  3635. } else if (parseScalarInitializer(Field.Type, Values,
  3636. /*StringPadLength=*/Field.LengthOf)) {
  3637. return true;
  3638. }
  3639. if (Values.size() > Field.LengthOf) {
  3640. return Error(Loc, "Initializer too long for field; expected at most " +
  3641. std::to_string(Field.LengthOf) + " elements, got " +
  3642. std::to_string(Values.size()));
  3643. }
  3644. // Default-initialize all remaining values.
  3645. Values.append(Contents.Values.begin() + Values.size(), Contents.Values.end());
  3646. Initializer = FieldInitializer(std::move(Values));
  3647. return false;
  3648. }
  3649. bool MasmParser::parseFieldInitializer(const FieldInfo &Field,
  3650. const RealFieldInfo &Contents,
  3651. FieldInitializer &Initializer) {
  3652. const fltSemantics *Semantics;
  3653. switch (Field.Type) {
  3654. case 4:
  3655. Semantics = &APFloat::IEEEsingle();
  3656. break;
  3657. case 8:
  3658. Semantics = &APFloat::IEEEdouble();
  3659. break;
  3660. case 10:
  3661. Semantics = &APFloat::x87DoubleExtended();
  3662. break;
  3663. default:
  3664. llvm_unreachable("unknown real field type");
  3665. }
  3666. SMLoc Loc = getTok().getLoc();
  3667. SmallVector<APInt, 1> AsIntValues;
  3668. if (parseOptionalToken(AsmToken::LCurly)) {
  3669. if (Field.LengthOf == 1)
  3670. return Error(Loc, "Cannot initialize scalar field with array value");
  3671. if (parseRealInstList(*Semantics, AsIntValues, AsmToken::RCurly) ||
  3672. parseToken(AsmToken::RCurly))
  3673. return true;
  3674. } else if (parseOptionalAngleBracketOpen()) {
  3675. if (Field.LengthOf == 1)
  3676. return Error(Loc, "Cannot initialize scalar field with array value");
  3677. if (parseRealInstList(*Semantics, AsIntValues, AsmToken::Greater) ||
  3678. parseAngleBracketClose())
  3679. return true;
  3680. } else if (Field.LengthOf > 1) {
  3681. return Error(Loc, "Cannot initialize array field with scalar value");
  3682. } else {
  3683. AsIntValues.emplace_back();
  3684. if (parseRealValue(*Semantics, AsIntValues.back()))
  3685. return true;
  3686. }
  3687. if (AsIntValues.size() > Field.LengthOf) {
  3688. return Error(Loc, "Initializer too long for field; expected at most " +
  3689. std::to_string(Field.LengthOf) + " elements, got " +
  3690. std::to_string(AsIntValues.size()));
  3691. }
  3692. // Default-initialize all remaining values.
  3693. AsIntValues.append(Contents.AsIntValues.begin() + AsIntValues.size(),
  3694. Contents.AsIntValues.end());
  3695. Initializer = FieldInitializer(std::move(AsIntValues));
  3696. return false;
  3697. }
  3698. bool MasmParser::parseFieldInitializer(const FieldInfo &Field,
  3699. const StructFieldInfo &Contents,
  3700. FieldInitializer &Initializer) {
  3701. SMLoc Loc = getTok().getLoc();
  3702. std::vector<StructInitializer> Initializers;
  3703. if (Field.LengthOf > 1) {
  3704. if (parseOptionalToken(AsmToken::LCurly)) {
  3705. if (parseStructInstList(Contents.Structure, Initializers,
  3706. AsmToken::RCurly) ||
  3707. parseToken(AsmToken::RCurly))
  3708. return true;
  3709. } else if (parseOptionalAngleBracketOpen()) {
  3710. if (parseStructInstList(Contents.Structure, Initializers,
  3711. AsmToken::Greater) ||
  3712. parseAngleBracketClose())
  3713. return true;
  3714. } else {
  3715. return Error(Loc, "Cannot initialize array field with scalar value");
  3716. }
  3717. } else {
  3718. Initializers.emplace_back();
  3719. if (parseStructInitializer(Contents.Structure, Initializers.back()))
  3720. return true;
  3721. }
  3722. if (Initializers.size() > Field.LengthOf) {
  3723. return Error(Loc, "Initializer too long for field; expected at most " +
  3724. std::to_string(Field.LengthOf) + " elements, got " +
  3725. std::to_string(Initializers.size()));
  3726. }
  3727. // Default-initialize all remaining values.
  3728. Initializers.insert(Initializers.end(),
  3729. Contents.Initializers.begin() + Initializers.size(),
  3730. Contents.Initializers.end());
  3731. Initializer = FieldInitializer(std::move(Initializers), Contents.Structure);
  3732. return false;
  3733. }
  3734. bool MasmParser::parseFieldInitializer(const FieldInfo &Field,
  3735. FieldInitializer &Initializer) {
  3736. switch (Field.Contents.FT) {
  3737. case FT_INTEGRAL:
  3738. return parseFieldInitializer(Field, Field.Contents.IntInfo, Initializer);
  3739. case FT_REAL:
  3740. return parseFieldInitializer(Field, Field.Contents.RealInfo, Initializer);
  3741. case FT_STRUCT:
  3742. return parseFieldInitializer(Field, Field.Contents.StructInfo, Initializer);
  3743. }
  3744. llvm_unreachable("Unhandled FieldType enum");
  3745. }
  3746. bool MasmParser::parseStructInitializer(const StructInfo &Structure,
  3747. StructInitializer &Initializer) {
  3748. const AsmToken FirstToken = getTok();
  3749. Optional<AsmToken::TokenKind> EndToken;
  3750. if (parseOptionalToken(AsmToken::LCurly)) {
  3751. EndToken = AsmToken::RCurly;
  3752. } else if (parseOptionalAngleBracketOpen()) {
  3753. EndToken = AsmToken::Greater;
  3754. AngleBracketDepth++;
  3755. } else if (FirstToken.is(AsmToken::Identifier) &&
  3756. FirstToken.getString() == "?") {
  3757. // ? initializer; leave EndToken uninitialized to treat as empty.
  3758. if (parseToken(AsmToken::Identifier))
  3759. return true;
  3760. } else {
  3761. return Error(FirstToken.getLoc(), "Expected struct initializer");
  3762. }
  3763. auto &FieldInitializers = Initializer.FieldInitializers;
  3764. size_t FieldIndex = 0;
  3765. if (EndToken.hasValue()) {
  3766. // Initialize all fields with given initializers.
  3767. while (getTok().isNot(EndToken.getValue()) &&
  3768. FieldIndex < Structure.Fields.size()) {
  3769. const FieldInfo &Field = Structure.Fields[FieldIndex++];
  3770. if (parseOptionalToken(AsmToken::Comma)) {
  3771. // Empty initializer; use the default and continue. (Also, allow line
  3772. // continuation.)
  3773. FieldInitializers.push_back(Field.Contents);
  3774. parseOptionalToken(AsmToken::EndOfStatement);
  3775. continue;
  3776. }
  3777. FieldInitializers.emplace_back(Field.Contents.FT);
  3778. if (parseFieldInitializer(Field, FieldInitializers.back()))
  3779. return true;
  3780. // Continue if we see a comma. (Also, allow line continuation.)
  3781. SMLoc CommaLoc = getTok().getLoc();
  3782. if (!parseOptionalToken(AsmToken::Comma))
  3783. break;
  3784. if (FieldIndex == Structure.Fields.size())
  3785. return Error(CommaLoc, "'" + Structure.Name +
  3786. "' initializer initializes too many fields");
  3787. parseOptionalToken(AsmToken::EndOfStatement);
  3788. }
  3789. }
  3790. // Default-initialize all remaining fields.
  3791. for (auto It = Structure.Fields.begin() + FieldIndex;
  3792. It != Structure.Fields.end(); ++It) {
  3793. const FieldInfo &Field = *It;
  3794. FieldInitializers.push_back(Field.Contents);
  3795. }
  3796. if (EndToken.hasValue()) {
  3797. if (EndToken.getValue() == AsmToken::Greater)
  3798. return parseAngleBracketClose();
  3799. return parseToken(EndToken.getValue());
  3800. }
  3801. return false;
  3802. }
  3803. bool MasmParser::parseStructInstList(
  3804. const StructInfo &Structure, std::vector<StructInitializer> &Initializers,
  3805. const AsmToken::TokenKind EndToken) {
  3806. while (getTok().isNot(EndToken) ||
  3807. (EndToken == AsmToken::Greater &&
  3808. getTok().isNot(AsmToken::GreaterGreater))) {
  3809. const AsmToken NextTok = peekTok();
  3810. if (NextTok.is(AsmToken::Identifier) &&
  3811. NextTok.getString().equals_insensitive("dup")) {
  3812. const MCExpr *Value;
  3813. if (parseExpression(Value) || parseToken(AsmToken::Identifier))
  3814. return true;
  3815. const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
  3816. if (!MCE)
  3817. return Error(Value->getLoc(),
  3818. "cannot repeat value a non-constant number of times");
  3819. const int64_t Repetitions = MCE->getValue();
  3820. if (Repetitions < 0)
  3821. return Error(Value->getLoc(),
  3822. "cannot repeat value a negative number of times");
  3823. std::vector<StructInitializer> DuplicatedValues;
  3824. if (parseToken(AsmToken::LParen,
  3825. "parentheses required for 'dup' contents") ||
  3826. parseStructInstList(Structure, DuplicatedValues) || parseRParen())
  3827. return true;
  3828. for (int i = 0; i < Repetitions; ++i)
  3829. llvm::append_range(Initializers, DuplicatedValues);
  3830. } else {
  3831. Initializers.emplace_back();
  3832. if (parseStructInitializer(Structure, Initializers.back()))
  3833. return true;
  3834. }
  3835. // Continue if we see a comma. (Also, allow line continuation.)
  3836. if (!parseOptionalToken(AsmToken::Comma))
  3837. break;
  3838. parseOptionalToken(AsmToken::EndOfStatement);
  3839. }
  3840. return false;
  3841. }
  3842. bool MasmParser::emitFieldValue(const FieldInfo &Field,
  3843. const IntFieldInfo &Contents) {
  3844. // Default-initialize all values.
  3845. for (const MCExpr *Value : Contents.Values) {
  3846. if (emitIntValue(Value, Field.Type))
  3847. return true;
  3848. }
  3849. return false;
  3850. }
  3851. bool MasmParser::emitFieldValue(const FieldInfo &Field,
  3852. const RealFieldInfo &Contents) {
  3853. for (const APInt &AsInt : Contents.AsIntValues) {
  3854. getStreamer().emitIntValue(AsInt.getLimitedValue(),
  3855. AsInt.getBitWidth() / 8);
  3856. }
  3857. return false;
  3858. }
  3859. bool MasmParser::emitFieldValue(const FieldInfo &Field,
  3860. const StructFieldInfo &Contents) {
  3861. for (const auto &Initializer : Contents.Initializers) {
  3862. size_t Index = 0, Offset = 0;
  3863. for (const auto &SubField : Contents.Structure.Fields) {
  3864. getStreamer().emitZeros(SubField.Offset - Offset);
  3865. Offset = SubField.Offset + SubField.SizeOf;
  3866. emitFieldInitializer(SubField, Initializer.FieldInitializers[Index++]);
  3867. }
  3868. }
  3869. return false;
  3870. }
  3871. bool MasmParser::emitFieldValue(const FieldInfo &Field) {
  3872. switch (Field.Contents.FT) {
  3873. case FT_INTEGRAL:
  3874. return emitFieldValue(Field, Field.Contents.IntInfo);
  3875. case FT_REAL:
  3876. return emitFieldValue(Field, Field.Contents.RealInfo);
  3877. case FT_STRUCT:
  3878. return emitFieldValue(Field, Field.Contents.StructInfo);
  3879. }
  3880. llvm_unreachable("Unhandled FieldType enum");
  3881. }
  3882. bool MasmParser::emitFieldInitializer(const FieldInfo &Field,
  3883. const IntFieldInfo &Contents,
  3884. const IntFieldInfo &Initializer) {
  3885. for (const auto &Value : Initializer.Values) {
  3886. if (emitIntValue(Value, Field.Type))
  3887. return true;
  3888. }
  3889. // Default-initialize all remaining values.
  3890. for (auto it = Contents.Values.begin() + Initializer.Values.size();
  3891. it != Contents.Values.end(); ++it) {
  3892. const auto &Value = *it;
  3893. if (emitIntValue(Value, Field.Type))
  3894. return true;
  3895. }
  3896. return false;
  3897. }
  3898. bool MasmParser::emitFieldInitializer(const FieldInfo &Field,
  3899. const RealFieldInfo &Contents,
  3900. const RealFieldInfo &Initializer) {
  3901. for (const auto &AsInt : Initializer.AsIntValues) {
  3902. getStreamer().emitIntValue(AsInt.getLimitedValue(),
  3903. AsInt.getBitWidth() / 8);
  3904. }
  3905. // Default-initialize all remaining values.
  3906. for (auto It = Contents.AsIntValues.begin() + Initializer.AsIntValues.size();
  3907. It != Contents.AsIntValues.end(); ++It) {
  3908. const auto &AsInt = *It;
  3909. getStreamer().emitIntValue(AsInt.getLimitedValue(),
  3910. AsInt.getBitWidth() / 8);
  3911. }
  3912. return false;
  3913. }
  3914. bool MasmParser::emitFieldInitializer(const FieldInfo &Field,
  3915. const StructFieldInfo &Contents,
  3916. const StructFieldInfo &Initializer) {
  3917. for (const auto &Init : Initializer.Initializers) {
  3918. if (emitStructInitializer(Contents.Structure, Init))
  3919. return true;
  3920. }
  3921. // Default-initialize all remaining values.
  3922. for (auto It =
  3923. Contents.Initializers.begin() + Initializer.Initializers.size();
  3924. It != Contents.Initializers.end(); ++It) {
  3925. const auto &Init = *It;
  3926. if (emitStructInitializer(Contents.Structure, Init))
  3927. return true;
  3928. }
  3929. return false;
  3930. }
  3931. bool MasmParser::emitFieldInitializer(const FieldInfo &Field,
  3932. const FieldInitializer &Initializer) {
  3933. switch (Field.Contents.FT) {
  3934. case FT_INTEGRAL:
  3935. return emitFieldInitializer(Field, Field.Contents.IntInfo,
  3936. Initializer.IntInfo);
  3937. case FT_REAL:
  3938. return emitFieldInitializer(Field, Field.Contents.RealInfo,
  3939. Initializer.RealInfo);
  3940. case FT_STRUCT:
  3941. return emitFieldInitializer(Field, Field.Contents.StructInfo,
  3942. Initializer.StructInfo);
  3943. }
  3944. llvm_unreachable("Unhandled FieldType enum");
  3945. }
  3946. bool MasmParser::emitStructInitializer(const StructInfo &Structure,
  3947. const StructInitializer &Initializer) {
  3948. if (!Structure.Initializable)
  3949. return Error(getLexer().getLoc(),
  3950. "cannot initialize a value of type '" + Structure.Name +
  3951. "'; 'org' was used in the type's declaration");
  3952. size_t Index = 0, Offset = 0;
  3953. for (const auto &Init : Initializer.FieldInitializers) {
  3954. const auto &Field = Structure.Fields[Index++];
  3955. getStreamer().emitZeros(Field.Offset - Offset);
  3956. Offset = Field.Offset + Field.SizeOf;
  3957. if (emitFieldInitializer(Field, Init))
  3958. return true;
  3959. }
  3960. // Default-initialize all remaining fields.
  3961. for (auto It =
  3962. Structure.Fields.begin() + Initializer.FieldInitializers.size();
  3963. It != Structure.Fields.end(); ++It) {
  3964. const auto &Field = *It;
  3965. getStreamer().emitZeros(Field.Offset - Offset);
  3966. Offset = Field.Offset + Field.SizeOf;
  3967. if (emitFieldValue(Field))
  3968. return true;
  3969. }
  3970. // Add final padding.
  3971. if (Offset != Structure.Size)
  3972. getStreamer().emitZeros(Structure.Size - Offset);
  3973. return false;
  3974. }
  3975. // Set data values from initializers.
  3976. bool MasmParser::emitStructValues(const StructInfo &Structure,
  3977. unsigned *Count) {
  3978. std::vector<StructInitializer> Initializers;
  3979. if (parseStructInstList(Structure, Initializers))
  3980. return true;
  3981. for (const auto &Initializer : Initializers) {
  3982. if (emitStructInitializer(Structure, Initializer))
  3983. return true;
  3984. }
  3985. if (Count)
  3986. *Count = Initializers.size();
  3987. return false;
  3988. }
  3989. // Declare a field in the current struct.
  3990. bool MasmParser::addStructField(StringRef Name, const StructInfo &Structure) {
  3991. StructInfo &OwningStruct = StructInProgress.back();
  3992. FieldInfo &Field =
  3993. OwningStruct.addField(Name, FT_STRUCT, Structure.AlignmentSize);
  3994. StructFieldInfo &StructInfo = Field.Contents.StructInfo;
  3995. StructInfo.Structure = Structure;
  3996. Field.Type = Structure.Size;
  3997. if (parseStructInstList(Structure, StructInfo.Initializers))
  3998. return true;
  3999. Field.LengthOf = StructInfo.Initializers.size();
  4000. Field.SizeOf = Field.Type * Field.LengthOf;
  4001. const unsigned FieldEnd = Field.Offset + Field.SizeOf;
  4002. if (!OwningStruct.IsUnion) {
  4003. OwningStruct.NextOffset = FieldEnd;
  4004. }
  4005. OwningStruct.Size = std::max(OwningStruct.Size, FieldEnd);
  4006. return false;
  4007. }
  4008. /// parseDirectiveStructValue
  4009. /// ::= struct-id (<struct-initializer> | {struct-initializer})
  4010. /// [, (<struct-initializer> | {struct-initializer})]*
  4011. bool MasmParser::parseDirectiveStructValue(const StructInfo &Structure,
  4012. StringRef Directive, SMLoc DirLoc) {
  4013. if (StructInProgress.empty()) {
  4014. if (emitStructValues(Structure))
  4015. return true;
  4016. } else if (addStructField("", Structure)) {
  4017. return addErrorSuffix(" in '" + Twine(Directive) + "' directive");
  4018. }
  4019. return false;
  4020. }
  4021. /// parseDirectiveNamedValue
  4022. /// ::= name (byte | word | ... ) [ expression (, expression)* ]
  4023. bool MasmParser::parseDirectiveNamedStructValue(const StructInfo &Structure,
  4024. StringRef Directive,
  4025. SMLoc DirLoc, StringRef Name) {
  4026. if (StructInProgress.empty()) {
  4027. // Initialize named data value.
  4028. MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
  4029. getStreamer().emitLabel(Sym);
  4030. unsigned Count;
  4031. if (emitStructValues(Structure, &Count))
  4032. return true;
  4033. AsmTypeInfo Type;
  4034. Type.Name = Structure.Name;
  4035. Type.Size = Structure.Size * Count;
  4036. Type.ElementSize = Structure.Size;
  4037. Type.Length = Count;
  4038. KnownType[Name.lower()] = Type;
  4039. } else if (addStructField(Name, Structure)) {
  4040. return addErrorSuffix(" in '" + Twine(Directive) + "' directive");
  4041. }
  4042. return false;
  4043. }
  4044. /// parseDirectiveStruct
  4045. /// ::= <name> (STRUC | STRUCT | UNION) [fieldAlign] [, NONUNIQUE]
  4046. /// (dataDir | generalDir | offsetDir | nestedStruct)+
  4047. /// <name> ENDS
  4048. ////// dataDir = data declaration
  4049. ////// offsetDir = EVEN, ORG, ALIGN
  4050. bool MasmParser::parseDirectiveStruct(StringRef Directive,
  4051. DirectiveKind DirKind, StringRef Name,
  4052. SMLoc NameLoc) {
  4053. // We ignore NONUNIQUE; we do not support OPTION M510 or OPTION OLDSTRUCTS
  4054. // anyway, so all field accesses must be qualified.
  4055. AsmToken NextTok = getTok();
  4056. int64_t AlignmentValue = 1;
  4057. if (NextTok.isNot(AsmToken::Comma) &&
  4058. NextTok.isNot(AsmToken::EndOfStatement) &&
  4059. parseAbsoluteExpression(AlignmentValue)) {
  4060. return addErrorSuffix(" in alignment value for '" + Twine(Directive) +
  4061. "' directive");
  4062. }
  4063. if (!isPowerOf2_64(AlignmentValue)) {
  4064. return Error(NextTok.getLoc(), "alignment must be a power of two; was " +
  4065. std::to_string(AlignmentValue));
  4066. }
  4067. StringRef Qualifier;
  4068. SMLoc QualifierLoc;
  4069. if (parseOptionalToken(AsmToken::Comma)) {
  4070. QualifierLoc = getTok().getLoc();
  4071. if (parseIdentifier(Qualifier))
  4072. return addErrorSuffix(" in '" + Twine(Directive) + "' directive");
  4073. if (!Qualifier.equals_insensitive("nonunique"))
  4074. return Error(QualifierLoc, "Unrecognized qualifier for '" +
  4075. Twine(Directive) +
  4076. "' directive; expected none or NONUNIQUE");
  4077. }
  4078. if (parseToken(AsmToken::EndOfStatement))
  4079. return addErrorSuffix(" in '" + Twine(Directive) + "' directive");
  4080. StructInProgress.emplace_back(Name, DirKind == DK_UNION, AlignmentValue);
  4081. return false;
  4082. }
  4083. /// parseDirectiveNestedStruct
  4084. /// ::= (STRUC | STRUCT | UNION) [name]
  4085. /// (dataDir | generalDir | offsetDir | nestedStruct)+
  4086. /// ENDS
  4087. bool MasmParser::parseDirectiveNestedStruct(StringRef Directive,
  4088. DirectiveKind DirKind) {
  4089. if (StructInProgress.empty())
  4090. return TokError("missing name in top-level '" + Twine(Directive) +
  4091. "' directive");
  4092. StringRef Name;
  4093. if (getTok().is(AsmToken::Identifier)) {
  4094. Name = getTok().getIdentifier();
  4095. parseToken(AsmToken::Identifier);
  4096. }
  4097. if (parseToken(AsmToken::EndOfStatement))
  4098. return addErrorSuffix(" in '" + Twine(Directive) + "' directive");
  4099. // Reserve space to ensure Alignment doesn't get invalidated when
  4100. // StructInProgress grows.
  4101. StructInProgress.reserve(StructInProgress.size() + 1);
  4102. StructInProgress.emplace_back(Name, DirKind == DK_UNION,
  4103. StructInProgress.back().Alignment);
  4104. return false;
  4105. }
  4106. bool MasmParser::parseDirectiveEnds(StringRef Name, SMLoc NameLoc) {
  4107. if (StructInProgress.empty())
  4108. return Error(NameLoc, "ENDS directive without matching STRUC/STRUCT/UNION");
  4109. if (StructInProgress.size() > 1)
  4110. return Error(NameLoc, "unexpected name in nested ENDS directive");
  4111. if (StructInProgress.back().Name.compare_insensitive(Name))
  4112. return Error(NameLoc, "mismatched name in ENDS directive; expected '" +
  4113. StructInProgress.back().Name + "'");
  4114. StructInfo Structure = StructInProgress.pop_back_val();
  4115. // Pad to make the structure's size divisible by the smaller of its alignment
  4116. // and the size of its largest field.
  4117. Structure.Size = llvm::alignTo(
  4118. Structure.Size, std::min(Structure.Alignment, Structure.AlignmentSize));
  4119. Structs[Name.lower()] = Structure;
  4120. if (parseToken(AsmToken::EndOfStatement))
  4121. return addErrorSuffix(" in ENDS directive");
  4122. return false;
  4123. }
  4124. bool MasmParser::parseDirectiveNestedEnds() {
  4125. if (StructInProgress.empty())
  4126. return TokError("ENDS directive without matching STRUC/STRUCT/UNION");
  4127. if (StructInProgress.size() == 1)
  4128. return TokError("missing name in top-level ENDS directive");
  4129. if (parseToken(AsmToken::EndOfStatement))
  4130. return addErrorSuffix(" in nested ENDS directive");
  4131. StructInfo Structure = StructInProgress.pop_back_val();
  4132. // Pad to make the structure's size divisible by its alignment.
  4133. Structure.Size = llvm::alignTo(Structure.Size, Structure.Alignment);
  4134. StructInfo &ParentStruct = StructInProgress.back();
  4135. if (Structure.Name.empty()) {
  4136. // Anonymous substructures' fields are addressed as if they belong to the
  4137. // parent structure - so we transfer them to the parent here.
  4138. const size_t OldFields = ParentStruct.Fields.size();
  4139. ParentStruct.Fields.insert(
  4140. ParentStruct.Fields.end(),
  4141. std::make_move_iterator(Structure.Fields.begin()),
  4142. std::make_move_iterator(Structure.Fields.end()));
  4143. for (const auto &FieldByName : Structure.FieldsByName) {
  4144. ParentStruct.FieldsByName[FieldByName.getKey()] =
  4145. FieldByName.getValue() + OldFields;
  4146. }
  4147. unsigned FirstFieldOffset = 0;
  4148. if (!Structure.Fields.empty() && !ParentStruct.IsUnion) {
  4149. FirstFieldOffset = llvm::alignTo(
  4150. ParentStruct.NextOffset,
  4151. std::min(ParentStruct.Alignment, Structure.AlignmentSize));
  4152. }
  4153. if (ParentStruct.IsUnion) {
  4154. ParentStruct.Size = std::max(ParentStruct.Size, Structure.Size);
  4155. } else {
  4156. for (auto FieldIter = ParentStruct.Fields.begin() + OldFields;
  4157. FieldIter != ParentStruct.Fields.end(); ++FieldIter) {
  4158. FieldIter->Offset += FirstFieldOffset;
  4159. }
  4160. const unsigned StructureEnd = FirstFieldOffset + Structure.Size;
  4161. if (!ParentStruct.IsUnion) {
  4162. ParentStruct.NextOffset = StructureEnd;
  4163. }
  4164. ParentStruct.Size = std::max(ParentStruct.Size, StructureEnd);
  4165. }
  4166. } else {
  4167. FieldInfo &Field = ParentStruct.addField(Structure.Name, FT_STRUCT,
  4168. Structure.AlignmentSize);
  4169. StructFieldInfo &StructInfo = Field.Contents.StructInfo;
  4170. Field.Type = Structure.Size;
  4171. Field.LengthOf = 1;
  4172. Field.SizeOf = Structure.Size;
  4173. const unsigned StructureEnd = Field.Offset + Field.SizeOf;
  4174. if (!ParentStruct.IsUnion) {
  4175. ParentStruct.NextOffset = StructureEnd;
  4176. }
  4177. ParentStruct.Size = std::max(ParentStruct.Size, StructureEnd);
  4178. StructInfo.Structure = Structure;
  4179. StructInfo.Initializers.emplace_back();
  4180. auto &FieldInitializers = StructInfo.Initializers.back().FieldInitializers;
  4181. for (const auto &SubField : Structure.Fields) {
  4182. FieldInitializers.push_back(SubField.Contents);
  4183. }
  4184. }
  4185. return false;
  4186. }
  4187. /// parseDirectiveOrg
  4188. /// ::= org expression
  4189. bool MasmParser::parseDirectiveOrg() {
  4190. const MCExpr *Offset;
  4191. SMLoc OffsetLoc = Lexer.getLoc();
  4192. if (checkForValidSection() || parseExpression(Offset))
  4193. return true;
  4194. if (parseToken(AsmToken::EndOfStatement))
  4195. return addErrorSuffix(" in 'org' directive");
  4196. if (StructInProgress.empty()) {
  4197. // Not in a struct; change the offset for the next instruction or data
  4198. if (checkForValidSection())
  4199. return addErrorSuffix(" in 'org' directive");
  4200. getStreamer().emitValueToOffset(Offset, 0, OffsetLoc);
  4201. } else {
  4202. // Offset the next field of this struct
  4203. StructInfo &Structure = StructInProgress.back();
  4204. int64_t OffsetRes;
  4205. if (!Offset->evaluateAsAbsolute(OffsetRes, getStreamer().getAssemblerPtr()))
  4206. return Error(OffsetLoc,
  4207. "expected absolute expression in 'org' directive");
  4208. if (OffsetRes < 0)
  4209. return Error(
  4210. OffsetLoc,
  4211. "expected non-negative value in struct's 'org' directive; was " +
  4212. std::to_string(OffsetRes));
  4213. Structure.NextOffset = static_cast<unsigned>(OffsetRes);
  4214. // ORG-affected structures cannot be initialized
  4215. Structure.Initializable = false;
  4216. }
  4217. return false;
  4218. }
  4219. bool MasmParser::emitAlignTo(int64_t Alignment) {
  4220. if (StructInProgress.empty()) {
  4221. // Not in a struct; align the next instruction or data
  4222. if (checkForValidSection())
  4223. return true;
  4224. // Check whether we should use optimal code alignment for this align
  4225. // directive.
  4226. const MCSection *Section = getStreamer().getCurrentSectionOnly();
  4227. assert(Section && "must have section to emit alignment");
  4228. if (Section->UseCodeAlign()) {
  4229. getStreamer().emitCodeAlignment(Alignment, &getTargetParser().getSTI(),
  4230. /*MaxBytesToEmit=*/0);
  4231. } else {
  4232. // FIXME: Target specific behavior about how the "extra" bytes are filled.
  4233. getStreamer().emitValueToAlignment(Alignment, /*Value=*/0,
  4234. /*ValueSize=*/1,
  4235. /*MaxBytesToEmit=*/0);
  4236. }
  4237. } else {
  4238. // Align the next field of this struct
  4239. StructInfo &Structure = StructInProgress.back();
  4240. Structure.NextOffset = llvm::alignTo(Structure.NextOffset, Alignment);
  4241. }
  4242. return false;
  4243. }
  4244. /// parseDirectiveAlign
  4245. /// ::= align expression
  4246. bool MasmParser::parseDirectiveAlign() {
  4247. SMLoc AlignmentLoc = getLexer().getLoc();
  4248. int64_t Alignment;
  4249. // Ignore empty 'align' directives.
  4250. if (getTok().is(AsmToken::EndOfStatement)) {
  4251. return Warning(AlignmentLoc,
  4252. "align directive with no operand is ignored") &&
  4253. parseToken(AsmToken::EndOfStatement);
  4254. }
  4255. if (parseAbsoluteExpression(Alignment) ||
  4256. parseToken(AsmToken::EndOfStatement))
  4257. return addErrorSuffix(" in align directive");
  4258. // Always emit an alignment here even if we throw an error.
  4259. bool ReturnVal = false;
  4260. // Reject alignments that aren't either a power of two or zero, for ML.exe
  4261. // compatibility. Alignment of zero is silently rounded up to one.
  4262. if (Alignment == 0)
  4263. Alignment = 1;
  4264. if (!isPowerOf2_64(Alignment))
  4265. ReturnVal |= Error(AlignmentLoc, "alignment must be a power of 2; was " +
  4266. std::to_string(Alignment));
  4267. if (emitAlignTo(Alignment))
  4268. ReturnVal |= addErrorSuffix(" in align directive");
  4269. return ReturnVal;
  4270. }
  4271. /// parseDirectiveEven
  4272. /// ::= even
  4273. bool MasmParser::parseDirectiveEven() {
  4274. if (parseToken(AsmToken::EndOfStatement) || emitAlignTo(2))
  4275. return addErrorSuffix(" in even directive");
  4276. return false;
  4277. }
  4278. /// parseDirectiveFile
  4279. /// ::= .file filename
  4280. /// ::= .file number [directory] filename [md5 checksum] [source source-text]
  4281. bool MasmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
  4282. // FIXME: I'm not sure what this is.
  4283. int64_t FileNumber = -1;
  4284. if (getLexer().is(AsmToken::Integer)) {
  4285. FileNumber = getTok().getIntVal();
  4286. Lex();
  4287. if (FileNumber < 0)
  4288. return TokError("negative file number");
  4289. }
  4290. std::string Path;
  4291. // Usually the directory and filename together, otherwise just the directory.
  4292. // Allow the strings to have escaped octal character sequence.
  4293. if (check(getTok().isNot(AsmToken::String),
  4294. "unexpected token in '.file' directive") ||
  4295. parseEscapedString(Path))
  4296. return true;
  4297. StringRef Directory;
  4298. StringRef Filename;
  4299. std::string FilenameData;
  4300. if (getLexer().is(AsmToken::String)) {
  4301. if (check(FileNumber == -1,
  4302. "explicit path specified, but no file number") ||
  4303. parseEscapedString(FilenameData))
  4304. return true;
  4305. Filename = FilenameData;
  4306. Directory = Path;
  4307. } else {
  4308. Filename = Path;
  4309. }
  4310. uint64_t MD5Hi, MD5Lo;
  4311. bool HasMD5 = false;
  4312. Optional<StringRef> Source;
  4313. bool HasSource = false;
  4314. std::string SourceString;
  4315. while (!parseOptionalToken(AsmToken::EndOfStatement)) {
  4316. StringRef Keyword;
  4317. if (check(getTok().isNot(AsmToken::Identifier),
  4318. "unexpected token in '.file' directive") ||
  4319. parseIdentifier(Keyword))
  4320. return true;
  4321. if (Keyword == "md5") {
  4322. HasMD5 = true;
  4323. if (check(FileNumber == -1,
  4324. "MD5 checksum specified, but no file number") ||
  4325. parseHexOcta(*this, MD5Hi, MD5Lo))
  4326. return true;
  4327. } else if (Keyword == "source") {
  4328. HasSource = true;
  4329. if (check(FileNumber == -1,
  4330. "source specified, but no file number") ||
  4331. check(getTok().isNot(AsmToken::String),
  4332. "unexpected token in '.file' directive") ||
  4333. parseEscapedString(SourceString))
  4334. return true;
  4335. } else {
  4336. return TokError("unexpected token in '.file' directive");
  4337. }
  4338. }
  4339. if (FileNumber == -1) {
  4340. // Ignore the directive if there is no number and the target doesn't support
  4341. // numberless .file directives. This allows some portability of assembler
  4342. // between different object file formats.
  4343. if (getContext().getAsmInfo()->hasSingleParameterDotFile())
  4344. getStreamer().emitFileDirective(Filename);
  4345. } else {
  4346. // In case there is a -g option as well as debug info from directive .file,
  4347. // we turn off the -g option, directly use the existing debug info instead.
  4348. // Throw away any implicit file table for the assembler source.
  4349. if (Ctx.getGenDwarfForAssembly()) {
  4350. Ctx.getMCDwarfLineTable(0).resetFileTable();
  4351. Ctx.setGenDwarfForAssembly(false);
  4352. }
  4353. Optional<MD5::MD5Result> CKMem;
  4354. if (HasMD5) {
  4355. MD5::MD5Result Sum;
  4356. for (unsigned i = 0; i != 8; ++i) {
  4357. Sum.Bytes[i] = uint8_t(MD5Hi >> ((7 - i) * 8));
  4358. Sum.Bytes[i + 8] = uint8_t(MD5Lo >> ((7 - i) * 8));
  4359. }
  4360. CKMem = Sum;
  4361. }
  4362. if (HasSource) {
  4363. char *SourceBuf = static_cast<char *>(Ctx.allocate(SourceString.size()));
  4364. memcpy(SourceBuf, SourceString.data(), SourceString.size());
  4365. Source = StringRef(SourceBuf, SourceString.size());
  4366. }
  4367. if (FileNumber == 0) {
  4368. if (Ctx.getDwarfVersion() < 5)
  4369. return Warning(DirectiveLoc, "file 0 not supported prior to DWARF-5");
  4370. getStreamer().emitDwarfFile0Directive(Directory, Filename, CKMem, Source);
  4371. } else {
  4372. Expected<unsigned> FileNumOrErr = getStreamer().tryEmitDwarfFileDirective(
  4373. FileNumber, Directory, Filename, CKMem, Source);
  4374. if (!FileNumOrErr)
  4375. return Error(DirectiveLoc, toString(FileNumOrErr.takeError()));
  4376. }
  4377. // Alert the user if there are some .file directives with MD5 and some not.
  4378. // But only do that once.
  4379. if (!ReportedInconsistentMD5 && !Ctx.isDwarfMD5UsageConsistent(0)) {
  4380. ReportedInconsistentMD5 = true;
  4381. return Warning(DirectiveLoc, "inconsistent use of MD5 checksums");
  4382. }
  4383. }
  4384. return false;
  4385. }
  4386. /// parseDirectiveLine
  4387. /// ::= .line [number]
  4388. bool MasmParser::parseDirectiveLine() {
  4389. int64_t LineNumber;
  4390. if (getLexer().is(AsmToken::Integer)) {
  4391. if (parseIntToken(LineNumber, "unexpected token in '.line' directive"))
  4392. return true;
  4393. (void)LineNumber;
  4394. // FIXME: Do something with the .line.
  4395. }
  4396. if (parseToken(AsmToken::EndOfStatement,
  4397. "unexpected token in '.line' directive"))
  4398. return true;
  4399. return false;
  4400. }
  4401. /// parseDirectiveLoc
  4402. /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
  4403. /// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
  4404. /// The first number is a file number, must have been previously assigned with
  4405. /// a .file directive, the second number is the line number and optionally the
  4406. /// third number is a column position (zero if not specified). The remaining
  4407. /// optional items are .loc sub-directives.
  4408. bool MasmParser::parseDirectiveLoc() {
  4409. int64_t FileNumber = 0, LineNumber = 0;
  4410. SMLoc Loc = getTok().getLoc();
  4411. if (parseIntToken(FileNumber, "unexpected token in '.loc' directive") ||
  4412. check(FileNumber < 1 && Ctx.getDwarfVersion() < 5, Loc,
  4413. "file number less than one in '.loc' directive") ||
  4414. check(!getContext().isValidDwarfFileNumber(FileNumber), Loc,
  4415. "unassigned file number in '.loc' directive"))
  4416. return true;
  4417. // optional
  4418. if (getLexer().is(AsmToken::Integer)) {
  4419. LineNumber = getTok().getIntVal();
  4420. if (LineNumber < 0)
  4421. return TokError("line number less than zero in '.loc' directive");
  4422. Lex();
  4423. }
  4424. int64_t ColumnPos = 0;
  4425. if (getLexer().is(AsmToken::Integer)) {
  4426. ColumnPos = getTok().getIntVal();
  4427. if (ColumnPos < 0)
  4428. return TokError("column position less than zero in '.loc' directive");
  4429. Lex();
  4430. }
  4431. auto PrevFlags = getContext().getCurrentDwarfLoc().getFlags();
  4432. unsigned Flags = PrevFlags & DWARF2_FLAG_IS_STMT;
  4433. unsigned Isa = 0;
  4434. int64_t Discriminator = 0;
  4435. auto parseLocOp = [&]() -> bool {
  4436. StringRef Name;
  4437. SMLoc Loc = getTok().getLoc();
  4438. if (parseIdentifier(Name))
  4439. return TokError("unexpected token in '.loc' directive");
  4440. if (Name == "basic_block")
  4441. Flags |= DWARF2_FLAG_BASIC_BLOCK;
  4442. else if (Name == "prologue_end")
  4443. Flags |= DWARF2_FLAG_PROLOGUE_END;
  4444. else if (Name == "epilogue_begin")
  4445. Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
  4446. else if (Name == "is_stmt") {
  4447. Loc = getTok().getLoc();
  4448. const MCExpr *Value;
  4449. if (parseExpression(Value))
  4450. return true;
  4451. // The expression must be the constant 0 or 1.
  4452. if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
  4453. int Value = MCE->getValue();
  4454. if (Value == 0)
  4455. Flags &= ~DWARF2_FLAG_IS_STMT;
  4456. else if (Value == 1)
  4457. Flags |= DWARF2_FLAG_IS_STMT;
  4458. else
  4459. return Error(Loc, "is_stmt value not 0 or 1");
  4460. } else {
  4461. return Error(Loc, "is_stmt value not the constant value of 0 or 1");
  4462. }
  4463. } else if (Name == "isa") {
  4464. Loc = getTok().getLoc();
  4465. const MCExpr *Value;
  4466. if (parseExpression(Value))
  4467. return true;
  4468. // The expression must be a constant greater or equal to 0.
  4469. if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
  4470. int Value = MCE->getValue();
  4471. if (Value < 0)
  4472. return Error(Loc, "isa number less than zero");
  4473. Isa = Value;
  4474. } else {
  4475. return Error(Loc, "isa number not a constant value");
  4476. }
  4477. } else if (Name == "discriminator") {
  4478. if (parseAbsoluteExpression(Discriminator))
  4479. return true;
  4480. } else {
  4481. return Error(Loc, "unknown sub-directive in '.loc' directive");
  4482. }
  4483. return false;
  4484. };
  4485. if (parseMany(parseLocOp, false /*hasComma*/))
  4486. return true;
  4487. getStreamer().emitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
  4488. Isa, Discriminator, StringRef());
  4489. return false;
  4490. }
  4491. /// parseDirectiveStabs
  4492. /// ::= .stabs string, number, number, number
  4493. bool MasmParser::parseDirectiveStabs() {
  4494. return TokError("unsupported directive '.stabs'");
  4495. }
  4496. /// parseDirectiveCVFile
  4497. /// ::= .cv_file number filename [checksum] [checksumkind]
  4498. bool MasmParser::parseDirectiveCVFile() {
  4499. SMLoc FileNumberLoc = getTok().getLoc();
  4500. int64_t FileNumber;
  4501. std::string Filename;
  4502. std::string Checksum;
  4503. int64_t ChecksumKind = 0;
  4504. if (parseIntToken(FileNumber,
  4505. "expected file number in '.cv_file' directive") ||
  4506. check(FileNumber < 1, FileNumberLoc, "file number less than one") ||
  4507. check(getTok().isNot(AsmToken::String),
  4508. "unexpected token in '.cv_file' directive") ||
  4509. parseEscapedString(Filename))
  4510. return true;
  4511. if (!parseOptionalToken(AsmToken::EndOfStatement)) {
  4512. if (check(getTok().isNot(AsmToken::String),
  4513. "unexpected token in '.cv_file' directive") ||
  4514. parseEscapedString(Checksum) ||
  4515. parseIntToken(ChecksumKind,
  4516. "expected checksum kind in '.cv_file' directive") ||
  4517. parseToken(AsmToken::EndOfStatement,
  4518. "unexpected token in '.cv_file' directive"))
  4519. return true;
  4520. }
  4521. Checksum = fromHex(Checksum);
  4522. void *CKMem = Ctx.allocate(Checksum.size(), 1);
  4523. memcpy(CKMem, Checksum.data(), Checksum.size());
  4524. ArrayRef<uint8_t> ChecksumAsBytes(reinterpret_cast<const uint8_t *>(CKMem),
  4525. Checksum.size());
  4526. if (!getStreamer().EmitCVFileDirective(FileNumber, Filename, ChecksumAsBytes,
  4527. static_cast<uint8_t>(ChecksumKind)))
  4528. return Error(FileNumberLoc, "file number already allocated");
  4529. return false;
  4530. }
  4531. bool MasmParser::parseCVFunctionId(int64_t &FunctionId,
  4532. StringRef DirectiveName) {
  4533. SMLoc Loc;
  4534. return parseTokenLoc(Loc) ||
  4535. parseIntToken(FunctionId, "expected function id in '" + DirectiveName +
  4536. "' directive") ||
  4537. check(FunctionId < 0 || FunctionId >= UINT_MAX, Loc,
  4538. "expected function id within range [0, UINT_MAX)");
  4539. }
  4540. bool MasmParser::parseCVFileId(int64_t &FileNumber, StringRef DirectiveName) {
  4541. SMLoc Loc;
  4542. return parseTokenLoc(Loc) ||
  4543. parseIntToken(FileNumber, "expected integer in '" + DirectiveName +
  4544. "' directive") ||
  4545. check(FileNumber < 1, Loc, "file number less than one in '" +
  4546. DirectiveName + "' directive") ||
  4547. check(!getCVContext().isValidFileNumber(FileNumber), Loc,
  4548. "unassigned file number in '" + DirectiveName + "' directive");
  4549. }
  4550. /// parseDirectiveCVFuncId
  4551. /// ::= .cv_func_id FunctionId
  4552. ///
  4553. /// Introduces a function ID that can be used with .cv_loc.
  4554. bool MasmParser::parseDirectiveCVFuncId() {
  4555. SMLoc FunctionIdLoc = getTok().getLoc();
  4556. int64_t FunctionId;
  4557. if (parseCVFunctionId(FunctionId, ".cv_func_id") ||
  4558. parseToken(AsmToken::EndOfStatement,
  4559. "unexpected token in '.cv_func_id' directive"))
  4560. return true;
  4561. if (!getStreamer().EmitCVFuncIdDirective(FunctionId))
  4562. return Error(FunctionIdLoc, "function id already allocated");
  4563. return false;
  4564. }
  4565. /// parseDirectiveCVInlineSiteId
  4566. /// ::= .cv_inline_site_id FunctionId
  4567. /// "within" IAFunc
  4568. /// "inlined_at" IAFile IALine [IACol]
  4569. ///
  4570. /// Introduces a function ID that can be used with .cv_loc. Includes "inlined
  4571. /// at" source location information for use in the line table of the caller,
  4572. /// whether the caller is a real function or another inlined call site.
  4573. bool MasmParser::parseDirectiveCVInlineSiteId() {
  4574. SMLoc FunctionIdLoc = getTok().getLoc();
  4575. int64_t FunctionId;
  4576. int64_t IAFunc;
  4577. int64_t IAFile;
  4578. int64_t IALine;
  4579. int64_t IACol = 0;
  4580. // FunctionId
  4581. if (parseCVFunctionId(FunctionId, ".cv_inline_site_id"))
  4582. return true;
  4583. // "within"
  4584. if (check((getLexer().isNot(AsmToken::Identifier) ||
  4585. getTok().getIdentifier() != "within"),
  4586. "expected 'within' identifier in '.cv_inline_site_id' directive"))
  4587. return true;
  4588. Lex();
  4589. // IAFunc
  4590. if (parseCVFunctionId(IAFunc, ".cv_inline_site_id"))
  4591. return true;
  4592. // "inlined_at"
  4593. if (check((getLexer().isNot(AsmToken::Identifier) ||
  4594. getTok().getIdentifier() != "inlined_at"),
  4595. "expected 'inlined_at' identifier in '.cv_inline_site_id' "
  4596. "directive") )
  4597. return true;
  4598. Lex();
  4599. // IAFile IALine
  4600. if (parseCVFileId(IAFile, ".cv_inline_site_id") ||
  4601. parseIntToken(IALine, "expected line number after 'inlined_at'"))
  4602. return true;
  4603. // [IACol]
  4604. if (getLexer().is(AsmToken::Integer)) {
  4605. IACol = getTok().getIntVal();
  4606. Lex();
  4607. }
  4608. if (parseToken(AsmToken::EndOfStatement,
  4609. "unexpected token in '.cv_inline_site_id' directive"))
  4610. return true;
  4611. if (!getStreamer().EmitCVInlineSiteIdDirective(FunctionId, IAFunc, IAFile,
  4612. IALine, IACol, FunctionIdLoc))
  4613. return Error(FunctionIdLoc, "function id already allocated");
  4614. return false;
  4615. }
  4616. /// parseDirectiveCVLoc
  4617. /// ::= .cv_loc FunctionId FileNumber [LineNumber] [ColumnPos] [prologue_end]
  4618. /// [is_stmt VALUE]
  4619. /// The first number is a file number, must have been previously assigned with
  4620. /// a .file directive, the second number is the line number and optionally the
  4621. /// third number is a column position (zero if not specified). The remaining
  4622. /// optional items are .loc sub-directives.
  4623. bool MasmParser::parseDirectiveCVLoc() {
  4624. SMLoc DirectiveLoc = getTok().getLoc();
  4625. int64_t FunctionId, FileNumber;
  4626. if (parseCVFunctionId(FunctionId, ".cv_loc") ||
  4627. parseCVFileId(FileNumber, ".cv_loc"))
  4628. return true;
  4629. int64_t LineNumber = 0;
  4630. if (getLexer().is(AsmToken::Integer)) {
  4631. LineNumber = getTok().getIntVal();
  4632. if (LineNumber < 0)
  4633. return TokError("line number less than zero in '.cv_loc' directive");
  4634. Lex();
  4635. }
  4636. int64_t ColumnPos = 0;
  4637. if (getLexer().is(AsmToken::Integer)) {
  4638. ColumnPos = getTok().getIntVal();
  4639. if (ColumnPos < 0)
  4640. return TokError("column position less than zero in '.cv_loc' directive");
  4641. Lex();
  4642. }
  4643. bool PrologueEnd = false;
  4644. uint64_t IsStmt = 0;
  4645. auto parseOp = [&]() -> bool {
  4646. StringRef Name;
  4647. SMLoc Loc = getTok().getLoc();
  4648. if (parseIdentifier(Name))
  4649. return TokError("unexpected token in '.cv_loc' directive");
  4650. if (Name == "prologue_end")
  4651. PrologueEnd = true;
  4652. else if (Name == "is_stmt") {
  4653. Loc = getTok().getLoc();
  4654. const MCExpr *Value;
  4655. if (parseExpression(Value))
  4656. return true;
  4657. // The expression must be the constant 0 or 1.
  4658. IsStmt = ~0ULL;
  4659. if (const auto *MCE = dyn_cast<MCConstantExpr>(Value))
  4660. IsStmt = MCE->getValue();
  4661. if (IsStmt > 1)
  4662. return Error(Loc, "is_stmt value not 0 or 1");
  4663. } else {
  4664. return Error(Loc, "unknown sub-directive in '.cv_loc' directive");
  4665. }
  4666. return false;
  4667. };
  4668. if (parseMany(parseOp, false /*hasComma*/))
  4669. return true;
  4670. getStreamer().emitCVLocDirective(FunctionId, FileNumber, LineNumber,
  4671. ColumnPos, PrologueEnd, IsStmt, StringRef(),
  4672. DirectiveLoc);
  4673. return false;
  4674. }
  4675. /// parseDirectiveCVLinetable
  4676. /// ::= .cv_linetable FunctionId, FnStart, FnEnd
  4677. bool MasmParser::parseDirectiveCVLinetable() {
  4678. int64_t FunctionId;
  4679. StringRef FnStartName, FnEndName;
  4680. SMLoc Loc = getTok().getLoc();
  4681. if (parseCVFunctionId(FunctionId, ".cv_linetable") ||
  4682. parseToken(AsmToken::Comma,
  4683. "unexpected token in '.cv_linetable' directive") ||
  4684. parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc,
  4685. "expected identifier in directive") ||
  4686. parseToken(AsmToken::Comma,
  4687. "unexpected token in '.cv_linetable' directive") ||
  4688. parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc,
  4689. "expected identifier in directive"))
  4690. return true;
  4691. MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
  4692. MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
  4693. getStreamer().emitCVLinetableDirective(FunctionId, FnStartSym, FnEndSym);
  4694. return false;
  4695. }
  4696. /// parseDirectiveCVInlineLinetable
  4697. /// ::= .cv_inline_linetable PrimaryFunctionId FileId LineNum FnStart FnEnd
  4698. bool MasmParser::parseDirectiveCVInlineLinetable() {
  4699. int64_t PrimaryFunctionId, SourceFileId, SourceLineNum;
  4700. StringRef FnStartName, FnEndName;
  4701. SMLoc Loc = getTok().getLoc();
  4702. if (parseCVFunctionId(PrimaryFunctionId, ".cv_inline_linetable") ||
  4703. parseTokenLoc(Loc) ||
  4704. parseIntToken(
  4705. SourceFileId,
  4706. "expected SourceField in '.cv_inline_linetable' directive") ||
  4707. check(SourceFileId <= 0, Loc,
  4708. "File id less than zero in '.cv_inline_linetable' directive") ||
  4709. parseTokenLoc(Loc) ||
  4710. parseIntToken(
  4711. SourceLineNum,
  4712. "expected SourceLineNum in '.cv_inline_linetable' directive") ||
  4713. check(SourceLineNum < 0, Loc,
  4714. "Line number less than zero in '.cv_inline_linetable' directive") ||
  4715. parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc,
  4716. "expected identifier in directive") ||
  4717. parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc,
  4718. "expected identifier in directive"))
  4719. return true;
  4720. if (parseToken(AsmToken::EndOfStatement, "Expected End of Statement"))
  4721. return true;
  4722. MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
  4723. MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
  4724. getStreamer().emitCVInlineLinetableDirective(PrimaryFunctionId, SourceFileId,
  4725. SourceLineNum, FnStartSym,
  4726. FnEndSym);
  4727. return false;
  4728. }
  4729. void MasmParser::initializeCVDefRangeTypeMap() {
  4730. CVDefRangeTypeMap["reg"] = CVDR_DEFRANGE_REGISTER;
  4731. CVDefRangeTypeMap["frame_ptr_rel"] = CVDR_DEFRANGE_FRAMEPOINTER_REL;
  4732. CVDefRangeTypeMap["subfield_reg"] = CVDR_DEFRANGE_SUBFIELD_REGISTER;
  4733. CVDefRangeTypeMap["reg_rel"] = CVDR_DEFRANGE_REGISTER_REL;
  4734. }
  4735. /// parseDirectiveCVDefRange
  4736. /// ::= .cv_def_range RangeStart RangeEnd (GapStart GapEnd)*, bytes*
  4737. bool MasmParser::parseDirectiveCVDefRange() {
  4738. SMLoc Loc;
  4739. std::vector<std::pair<const MCSymbol *, const MCSymbol *>> Ranges;
  4740. while (getLexer().is(AsmToken::Identifier)) {
  4741. Loc = getLexer().getLoc();
  4742. StringRef GapStartName;
  4743. if (parseIdentifier(GapStartName))
  4744. return Error(Loc, "expected identifier in directive");
  4745. MCSymbol *GapStartSym = getContext().getOrCreateSymbol(GapStartName);
  4746. Loc = getLexer().getLoc();
  4747. StringRef GapEndName;
  4748. if (parseIdentifier(GapEndName))
  4749. return Error(Loc, "expected identifier in directive");
  4750. MCSymbol *GapEndSym = getContext().getOrCreateSymbol(GapEndName);
  4751. Ranges.push_back({GapStartSym, GapEndSym});
  4752. }
  4753. StringRef CVDefRangeTypeStr;
  4754. if (parseToken(
  4755. AsmToken::Comma,
  4756. "expected comma before def_range type in .cv_def_range directive") ||
  4757. parseIdentifier(CVDefRangeTypeStr))
  4758. return Error(Loc, "expected def_range type in directive");
  4759. StringMap<CVDefRangeType>::const_iterator CVTypeIt =
  4760. CVDefRangeTypeMap.find(CVDefRangeTypeStr);
  4761. CVDefRangeType CVDRType = (CVTypeIt == CVDefRangeTypeMap.end())
  4762. ? CVDR_DEFRANGE
  4763. : CVTypeIt->getValue();
  4764. switch (CVDRType) {
  4765. case CVDR_DEFRANGE_REGISTER: {
  4766. int64_t DRRegister;
  4767. if (parseToken(AsmToken::Comma, "expected comma before register number in "
  4768. ".cv_def_range directive") ||
  4769. parseAbsoluteExpression(DRRegister))
  4770. return Error(Loc, "expected register number");
  4771. codeview::DefRangeRegisterHeader DRHdr;
  4772. DRHdr.Register = DRRegister;
  4773. DRHdr.MayHaveNoName = 0;
  4774. getStreamer().emitCVDefRangeDirective(Ranges, DRHdr);
  4775. break;
  4776. }
  4777. case CVDR_DEFRANGE_FRAMEPOINTER_REL: {
  4778. int64_t DROffset;
  4779. if (parseToken(AsmToken::Comma,
  4780. "expected comma before offset in .cv_def_range directive") ||
  4781. parseAbsoluteExpression(DROffset))
  4782. return Error(Loc, "expected offset value");
  4783. codeview::DefRangeFramePointerRelHeader DRHdr;
  4784. DRHdr.Offset = DROffset;
  4785. getStreamer().emitCVDefRangeDirective(Ranges, DRHdr);
  4786. break;
  4787. }
  4788. case CVDR_DEFRANGE_SUBFIELD_REGISTER: {
  4789. int64_t DRRegister;
  4790. int64_t DROffsetInParent;
  4791. if (parseToken(AsmToken::Comma, "expected comma before register number in "
  4792. ".cv_def_range directive") ||
  4793. parseAbsoluteExpression(DRRegister))
  4794. return Error(Loc, "expected register number");
  4795. if (parseToken(AsmToken::Comma,
  4796. "expected comma before offset in .cv_def_range directive") ||
  4797. parseAbsoluteExpression(DROffsetInParent))
  4798. return Error(Loc, "expected offset value");
  4799. codeview::DefRangeSubfieldRegisterHeader DRHdr;
  4800. DRHdr.Register = DRRegister;
  4801. DRHdr.MayHaveNoName = 0;
  4802. DRHdr.OffsetInParent = DROffsetInParent;
  4803. getStreamer().emitCVDefRangeDirective(Ranges, DRHdr);
  4804. break;
  4805. }
  4806. case CVDR_DEFRANGE_REGISTER_REL: {
  4807. int64_t DRRegister;
  4808. int64_t DRFlags;
  4809. int64_t DRBasePointerOffset;
  4810. if (parseToken(AsmToken::Comma, "expected comma before register number in "
  4811. ".cv_def_range directive") ||
  4812. parseAbsoluteExpression(DRRegister))
  4813. return Error(Loc, "expected register value");
  4814. if (parseToken(
  4815. AsmToken::Comma,
  4816. "expected comma before flag value in .cv_def_range directive") ||
  4817. parseAbsoluteExpression(DRFlags))
  4818. return Error(Loc, "expected flag value");
  4819. if (parseToken(AsmToken::Comma, "expected comma before base pointer offset "
  4820. "in .cv_def_range directive") ||
  4821. parseAbsoluteExpression(DRBasePointerOffset))
  4822. return Error(Loc, "expected base pointer offset value");
  4823. codeview::DefRangeRegisterRelHeader DRHdr;
  4824. DRHdr.Register = DRRegister;
  4825. DRHdr.Flags = DRFlags;
  4826. DRHdr.BasePointerOffset = DRBasePointerOffset;
  4827. getStreamer().emitCVDefRangeDirective(Ranges, DRHdr);
  4828. break;
  4829. }
  4830. default:
  4831. return Error(Loc, "unexpected def_range type in .cv_def_range directive");
  4832. }
  4833. return true;
  4834. }
  4835. /// parseDirectiveCVString
  4836. /// ::= .cv_stringtable "string"
  4837. bool MasmParser::parseDirectiveCVString() {
  4838. std::string Data;
  4839. if (checkForValidSection() || parseEscapedString(Data))
  4840. return addErrorSuffix(" in '.cv_string' directive");
  4841. // Put the string in the table and emit the offset.
  4842. std::pair<StringRef, unsigned> Insertion =
  4843. getCVContext().addToStringTable(Data);
  4844. getStreamer().emitIntValue(Insertion.second, 4);
  4845. return false;
  4846. }
  4847. /// parseDirectiveCVStringTable
  4848. /// ::= .cv_stringtable
  4849. bool MasmParser::parseDirectiveCVStringTable() {
  4850. getStreamer().emitCVStringTableDirective();
  4851. return false;
  4852. }
  4853. /// parseDirectiveCVFileChecksums
  4854. /// ::= .cv_filechecksums
  4855. bool MasmParser::parseDirectiveCVFileChecksums() {
  4856. getStreamer().emitCVFileChecksumsDirective();
  4857. return false;
  4858. }
  4859. /// parseDirectiveCVFileChecksumOffset
  4860. /// ::= .cv_filechecksumoffset fileno
  4861. bool MasmParser::parseDirectiveCVFileChecksumOffset() {
  4862. int64_t FileNo;
  4863. if (parseIntToken(FileNo, "expected identifier in directive"))
  4864. return true;
  4865. if (parseToken(AsmToken::EndOfStatement, "Expected End of Statement"))
  4866. return true;
  4867. getStreamer().emitCVFileChecksumOffsetDirective(FileNo);
  4868. return false;
  4869. }
  4870. /// parseDirectiveCVFPOData
  4871. /// ::= .cv_fpo_data procsym
  4872. bool MasmParser::parseDirectiveCVFPOData() {
  4873. SMLoc DirLoc = getLexer().getLoc();
  4874. StringRef ProcName;
  4875. if (parseIdentifier(ProcName))
  4876. return TokError("expected symbol name");
  4877. if (parseEOL("unexpected tokens"))
  4878. return addErrorSuffix(" in '.cv_fpo_data' directive");
  4879. MCSymbol *ProcSym = getContext().getOrCreateSymbol(ProcName);
  4880. getStreamer().EmitCVFPOData(ProcSym, DirLoc);
  4881. return false;
  4882. }
  4883. /// parseDirectiveCFISections
  4884. /// ::= .cfi_sections section [, section]
  4885. bool MasmParser::parseDirectiveCFISections() {
  4886. StringRef Name;
  4887. bool EH = false;
  4888. bool Debug = false;
  4889. if (parseIdentifier(Name))
  4890. return TokError("Expected an identifier");
  4891. if (Name == ".eh_frame")
  4892. EH = true;
  4893. else if (Name == ".debug_frame")
  4894. Debug = true;
  4895. if (getLexer().is(AsmToken::Comma)) {
  4896. Lex();
  4897. if (parseIdentifier(Name))
  4898. return TokError("Expected an identifier");
  4899. if (Name == ".eh_frame")
  4900. EH = true;
  4901. else if (Name == ".debug_frame")
  4902. Debug = true;
  4903. }
  4904. getStreamer().emitCFISections(EH, Debug);
  4905. return false;
  4906. }
  4907. /// parseDirectiveCFIStartProc
  4908. /// ::= .cfi_startproc [simple]
  4909. bool MasmParser::parseDirectiveCFIStartProc() {
  4910. StringRef Simple;
  4911. if (!parseOptionalToken(AsmToken::EndOfStatement)) {
  4912. if (check(parseIdentifier(Simple) || Simple != "simple",
  4913. "unexpected token") ||
  4914. parseToken(AsmToken::EndOfStatement))
  4915. return addErrorSuffix(" in '.cfi_startproc' directive");
  4916. }
  4917. // TODO(kristina): Deal with a corner case of incorrect diagnostic context
  4918. // being produced if this directive is emitted as part of preprocessor macro
  4919. // expansion which can *ONLY* happen if Clang's cc1as is the API consumer.
  4920. // Tools like llvm-mc on the other hand are not affected by it, and report
  4921. // correct context information.
  4922. getStreamer().emitCFIStartProc(!Simple.empty(), Lexer.getLoc());
  4923. return false;
  4924. }
  4925. /// parseDirectiveCFIEndProc
  4926. /// ::= .cfi_endproc
  4927. bool MasmParser::parseDirectiveCFIEndProc() {
  4928. getStreamer().emitCFIEndProc();
  4929. return false;
  4930. }
  4931. /// parse register name or number.
  4932. bool MasmParser::parseRegisterOrRegisterNumber(int64_t &Register,
  4933. SMLoc DirectiveLoc) {
  4934. unsigned RegNo;
  4935. if (getLexer().isNot(AsmToken::Integer)) {
  4936. if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
  4937. return true;
  4938. Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
  4939. } else
  4940. return parseAbsoluteExpression(Register);
  4941. return false;
  4942. }
  4943. /// parseDirectiveCFIDefCfa
  4944. /// ::= .cfi_def_cfa register, offset
  4945. bool MasmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
  4946. int64_t Register = 0, Offset = 0;
  4947. if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) ||
  4948. parseToken(AsmToken::Comma, "unexpected token in directive") ||
  4949. parseAbsoluteExpression(Offset))
  4950. return true;
  4951. getStreamer().emitCFIDefCfa(Register, Offset);
  4952. return false;
  4953. }
  4954. /// parseDirectiveCFIDefCfaOffset
  4955. /// ::= .cfi_def_cfa_offset offset
  4956. bool MasmParser::parseDirectiveCFIDefCfaOffset() {
  4957. int64_t Offset = 0;
  4958. if (parseAbsoluteExpression(Offset))
  4959. return true;
  4960. getStreamer().emitCFIDefCfaOffset(Offset);
  4961. return false;
  4962. }
  4963. /// parseDirectiveCFIRegister
  4964. /// ::= .cfi_register register, register
  4965. bool MasmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
  4966. int64_t Register1 = 0, Register2 = 0;
  4967. if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc) ||
  4968. parseToken(AsmToken::Comma, "unexpected token in directive") ||
  4969. parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
  4970. return true;
  4971. getStreamer().emitCFIRegister(Register1, Register2);
  4972. return false;
  4973. }
  4974. /// parseDirectiveCFIWindowSave
  4975. /// ::= .cfi_window_save
  4976. bool MasmParser::parseDirectiveCFIWindowSave() {
  4977. getStreamer().emitCFIWindowSave();
  4978. return false;
  4979. }
  4980. /// parseDirectiveCFIAdjustCfaOffset
  4981. /// ::= .cfi_adjust_cfa_offset adjustment
  4982. bool MasmParser::parseDirectiveCFIAdjustCfaOffset() {
  4983. int64_t Adjustment = 0;
  4984. if (parseAbsoluteExpression(Adjustment))
  4985. return true;
  4986. getStreamer().emitCFIAdjustCfaOffset(Adjustment);
  4987. return false;
  4988. }
  4989. /// parseDirectiveCFIDefCfaRegister
  4990. /// ::= .cfi_def_cfa_register register
  4991. bool MasmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
  4992. int64_t Register = 0;
  4993. if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
  4994. return true;
  4995. getStreamer().emitCFIDefCfaRegister(Register);
  4996. return false;
  4997. }
  4998. /// parseDirectiveCFIOffset
  4999. /// ::= .cfi_offset register, offset
  5000. bool MasmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
  5001. int64_t Register = 0;
  5002. int64_t Offset = 0;
  5003. if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) ||
  5004. parseToken(AsmToken::Comma, "unexpected token in directive") ||
  5005. parseAbsoluteExpression(Offset))
  5006. return true;
  5007. getStreamer().emitCFIOffset(Register, Offset);
  5008. return false;
  5009. }
  5010. /// parseDirectiveCFIRelOffset
  5011. /// ::= .cfi_rel_offset register, offset
  5012. bool MasmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
  5013. int64_t Register = 0, Offset = 0;
  5014. if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) ||
  5015. parseToken(AsmToken::Comma, "unexpected token in directive") ||
  5016. parseAbsoluteExpression(Offset))
  5017. return true;
  5018. getStreamer().emitCFIRelOffset(Register, Offset);
  5019. return false;
  5020. }
  5021. static bool isValidEncoding(int64_t Encoding) {
  5022. if (Encoding & ~0xff)
  5023. return false;
  5024. if (Encoding == dwarf::DW_EH_PE_omit)
  5025. return true;
  5026. const unsigned Format = Encoding & 0xf;
  5027. if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
  5028. Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
  5029. Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
  5030. Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
  5031. return false;
  5032. const unsigned Application = Encoding & 0x70;
  5033. if (Application != dwarf::DW_EH_PE_absptr &&
  5034. Application != dwarf::DW_EH_PE_pcrel)
  5035. return false;
  5036. return true;
  5037. }
  5038. /// parseDirectiveCFIPersonalityOrLsda
  5039. /// IsPersonality true for cfi_personality, false for cfi_lsda
  5040. /// ::= .cfi_personality encoding, [symbol_name]
  5041. /// ::= .cfi_lsda encoding, [symbol_name]
  5042. bool MasmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
  5043. int64_t Encoding = 0;
  5044. if (parseAbsoluteExpression(Encoding))
  5045. return true;
  5046. if (Encoding == dwarf::DW_EH_PE_omit)
  5047. return false;
  5048. StringRef Name;
  5049. if (check(!isValidEncoding(Encoding), "unsupported encoding.") ||
  5050. parseToken(AsmToken::Comma, "unexpected token in directive") ||
  5051. check(parseIdentifier(Name), "expected identifier in directive"))
  5052. return true;
  5053. MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
  5054. if (IsPersonality)
  5055. getStreamer().emitCFIPersonality(Sym, Encoding);
  5056. else
  5057. getStreamer().emitCFILsda(Sym, Encoding);
  5058. return false;
  5059. }
  5060. /// parseDirectiveCFIRememberState
  5061. /// ::= .cfi_remember_state
  5062. bool MasmParser::parseDirectiveCFIRememberState() {
  5063. getStreamer().emitCFIRememberState();
  5064. return false;
  5065. }
  5066. /// parseDirectiveCFIRestoreState
  5067. /// ::= .cfi_remember_state
  5068. bool MasmParser::parseDirectiveCFIRestoreState() {
  5069. getStreamer().emitCFIRestoreState();
  5070. return false;
  5071. }
  5072. /// parseDirectiveCFISameValue
  5073. /// ::= .cfi_same_value register
  5074. bool MasmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
  5075. int64_t Register = 0;
  5076. if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
  5077. return true;
  5078. getStreamer().emitCFISameValue(Register);
  5079. return false;
  5080. }
  5081. /// parseDirectiveCFIRestore
  5082. /// ::= .cfi_restore register
  5083. bool MasmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
  5084. int64_t Register = 0;
  5085. if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
  5086. return true;
  5087. getStreamer().emitCFIRestore(Register);
  5088. return false;
  5089. }
  5090. /// parseDirectiveCFIEscape
  5091. /// ::= .cfi_escape expression[,...]
  5092. bool MasmParser::parseDirectiveCFIEscape() {
  5093. std::string Values;
  5094. int64_t CurrValue;
  5095. if (parseAbsoluteExpression(CurrValue))
  5096. return true;
  5097. Values.push_back((uint8_t)CurrValue);
  5098. while (getLexer().is(AsmToken::Comma)) {
  5099. Lex();
  5100. if (parseAbsoluteExpression(CurrValue))
  5101. return true;
  5102. Values.push_back((uint8_t)CurrValue);
  5103. }
  5104. getStreamer().emitCFIEscape(Values);
  5105. return false;
  5106. }
  5107. /// parseDirectiveCFIReturnColumn
  5108. /// ::= .cfi_return_column register
  5109. bool MasmParser::parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc) {
  5110. int64_t Register = 0;
  5111. if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
  5112. return true;
  5113. getStreamer().emitCFIReturnColumn(Register);
  5114. return false;
  5115. }
  5116. /// parseDirectiveCFISignalFrame
  5117. /// ::= .cfi_signal_frame
  5118. bool MasmParser::parseDirectiveCFISignalFrame() {
  5119. if (parseToken(AsmToken::EndOfStatement,
  5120. "unexpected token in '.cfi_signal_frame'"))
  5121. return true;
  5122. getStreamer().emitCFISignalFrame();
  5123. return false;
  5124. }
  5125. /// parseDirectiveCFIUndefined
  5126. /// ::= .cfi_undefined register
  5127. bool MasmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
  5128. int64_t Register = 0;
  5129. if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
  5130. return true;
  5131. getStreamer().emitCFIUndefined(Register);
  5132. return false;
  5133. }
  5134. /// parseDirectiveMacro
  5135. /// ::= name macro [parameters]
  5136. /// ["LOCAL" identifiers]
  5137. /// parameters ::= parameter [, parameter]*
  5138. /// parameter ::= name ":" qualifier
  5139. /// qualifier ::= "req" | "vararg" | "=" macro_argument
  5140. bool MasmParser::parseDirectiveMacro(StringRef Name, SMLoc NameLoc) {
  5141. MCAsmMacroParameters Parameters;
  5142. while (getLexer().isNot(AsmToken::EndOfStatement)) {
  5143. if (!Parameters.empty() && Parameters.back().Vararg)
  5144. return Error(Lexer.getLoc(),
  5145. "Vararg parameter '" + Parameters.back().Name +
  5146. "' should be last in the list of parameters");
  5147. MCAsmMacroParameter Parameter;
  5148. if (parseIdentifier(Parameter.Name))
  5149. return TokError("expected identifier in 'macro' directive");
  5150. // Emit an error if two (or more) named parameters share the same name.
  5151. for (const MCAsmMacroParameter& CurrParam : Parameters)
  5152. if (CurrParam.Name.equals_insensitive(Parameter.Name))
  5153. return TokError("macro '" + Name + "' has multiple parameters"
  5154. " named '" + Parameter.Name + "'");
  5155. if (Lexer.is(AsmToken::Colon)) {
  5156. Lex(); // consume ':'
  5157. if (parseOptionalToken(AsmToken::Equal)) {
  5158. // Default value
  5159. SMLoc ParamLoc;
  5160. ParamLoc = Lexer.getLoc();
  5161. if (parseMacroArgument(nullptr, Parameter.Value))
  5162. return true;
  5163. } else {
  5164. SMLoc QualLoc;
  5165. StringRef Qualifier;
  5166. QualLoc = Lexer.getLoc();
  5167. if (parseIdentifier(Qualifier))
  5168. return Error(QualLoc, "missing parameter qualifier for "
  5169. "'" +
  5170. Parameter.Name + "' in macro '" + Name +
  5171. "'");
  5172. if (Qualifier.equals_insensitive("req"))
  5173. Parameter.Required = true;
  5174. else if (Qualifier.equals_insensitive("vararg"))
  5175. Parameter.Vararg = true;
  5176. else
  5177. return Error(QualLoc,
  5178. Qualifier + " is not a valid parameter qualifier for '" +
  5179. Parameter.Name + "' in macro '" + Name + "'");
  5180. }
  5181. }
  5182. Parameters.push_back(std::move(Parameter));
  5183. if (getLexer().is(AsmToken::Comma))
  5184. Lex();
  5185. }
  5186. // Eat just the end of statement.
  5187. Lexer.Lex();
  5188. std::vector<std::string> Locals;
  5189. if (getTok().is(AsmToken::Identifier) &&
  5190. getTok().getIdentifier().equals_insensitive("local")) {
  5191. Lex(); // Eat the LOCAL directive.
  5192. StringRef ID;
  5193. while (true) {
  5194. if (parseIdentifier(ID))
  5195. return true;
  5196. Locals.push_back(ID.lower());
  5197. // If we see a comma, continue (and allow line continuation).
  5198. if (!parseOptionalToken(AsmToken::Comma))
  5199. break;
  5200. parseOptionalToken(AsmToken::EndOfStatement);
  5201. }
  5202. }
  5203. // Consuming deferred text, so use Lexer.Lex to ignore Lexing Errors.
  5204. AsmToken EndToken, StartToken = getTok();
  5205. unsigned MacroDepth = 0;
  5206. bool IsMacroFunction = false;
  5207. // Lex the macro definition.
  5208. while (true) {
  5209. // Ignore Lexing errors in macros.
  5210. while (Lexer.is(AsmToken::Error)) {
  5211. Lexer.Lex();
  5212. }
  5213. // Check whether we have reached the end of the file.
  5214. if (getLexer().is(AsmToken::Eof))
  5215. return Error(NameLoc, "no matching 'endm' in definition");
  5216. // Otherwise, check whether we have reached the 'endm'... and determine if
  5217. // this is a macro function.
  5218. if (getLexer().is(AsmToken::Identifier)) {
  5219. if (getTok().getIdentifier().equals_insensitive("endm")) {
  5220. if (MacroDepth == 0) { // Outermost macro.
  5221. EndToken = getTok();
  5222. Lexer.Lex();
  5223. if (getLexer().isNot(AsmToken::EndOfStatement))
  5224. return TokError("unexpected token in '" + EndToken.getIdentifier() +
  5225. "' directive");
  5226. break;
  5227. } else {
  5228. // Otherwise we just found the end of an inner macro.
  5229. --MacroDepth;
  5230. }
  5231. } else if (getTok().getIdentifier().equals_insensitive("exitm")) {
  5232. if (MacroDepth == 0 && peekTok().isNot(AsmToken::EndOfStatement)) {
  5233. IsMacroFunction = true;
  5234. }
  5235. } else if (isMacroLikeDirective()) {
  5236. // We allow nested macros. Those aren't instantiated until the
  5237. // outermost macro is expanded so just ignore them for now.
  5238. ++MacroDepth;
  5239. }
  5240. }
  5241. // Otherwise, scan til the end of the statement.
  5242. eatToEndOfStatement();
  5243. }
  5244. if (getContext().lookupMacro(Name.lower())) {
  5245. return Error(NameLoc, "macro '" + Name + "' is already defined");
  5246. }
  5247. const char *BodyStart = StartToken.getLoc().getPointer();
  5248. const char *BodyEnd = EndToken.getLoc().getPointer();
  5249. StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
  5250. MCAsmMacro Macro(Name, Body, std::move(Parameters), std::move(Locals),
  5251. IsMacroFunction);
  5252. DEBUG_WITH_TYPE("asm-macros", dbgs() << "Defining new macro:\n";
  5253. Macro.dump());
  5254. getContext().defineMacro(Name.lower(), std::move(Macro));
  5255. return false;
  5256. }
  5257. /// parseDirectiveExitMacro
  5258. /// ::= "exitm" [textitem]
  5259. bool MasmParser::parseDirectiveExitMacro(SMLoc DirectiveLoc,
  5260. StringRef Directive,
  5261. std::string &Value) {
  5262. SMLoc EndLoc = getTok().getLoc();
  5263. if (getTok().isNot(AsmToken::EndOfStatement) && parseTextItem(Value))
  5264. return Error(EndLoc,
  5265. "unable to parse text item in '" + Directive + "' directive");
  5266. eatToEndOfStatement();
  5267. if (!isInsideMacroInstantiation())
  5268. return TokError("unexpected '" + Directive + "' in file, "
  5269. "no current macro definition");
  5270. // Exit all conditionals that are active in the current macro.
  5271. while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
  5272. TheCondState = TheCondStack.back();
  5273. TheCondStack.pop_back();
  5274. }
  5275. handleMacroExit();
  5276. return false;
  5277. }
  5278. /// parseDirectiveEndMacro
  5279. /// ::= endm
  5280. bool MasmParser::parseDirectiveEndMacro(StringRef Directive) {
  5281. if (getLexer().isNot(AsmToken::EndOfStatement))
  5282. return TokError("unexpected token in '" + Directive + "' directive");
  5283. // If we are inside a macro instantiation, terminate the current
  5284. // instantiation.
  5285. if (isInsideMacroInstantiation()) {
  5286. handleMacroExit();
  5287. return false;
  5288. }
  5289. // Otherwise, this .endmacro is a stray entry in the file; well formed
  5290. // .endmacro directives are handled during the macro definition parsing.
  5291. return TokError("unexpected '" + Directive + "' in file, "
  5292. "no current macro definition");
  5293. }
  5294. /// parseDirectivePurgeMacro
  5295. /// ::= purge identifier ( , identifier )*
  5296. bool MasmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
  5297. StringRef Name;
  5298. while (true) {
  5299. SMLoc NameLoc;
  5300. if (parseTokenLoc(NameLoc) ||
  5301. check(parseIdentifier(Name), NameLoc,
  5302. "expected identifier in 'purge' directive"))
  5303. return true;
  5304. DEBUG_WITH_TYPE("asm-macros", dbgs()
  5305. << "Un-defining macro: " << Name << "\n");
  5306. if (!getContext().lookupMacro(Name.lower()))
  5307. return Error(NameLoc, "macro '" + Name + "' is not defined");
  5308. getContext().undefineMacro(Name.lower());
  5309. if (!parseOptionalToken(AsmToken::Comma))
  5310. break;
  5311. parseOptionalToken(AsmToken::EndOfStatement);
  5312. }
  5313. return false;
  5314. }
  5315. /// parseDirectiveSymbolAttribute
  5316. /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
  5317. bool MasmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
  5318. auto parseOp = [&]() -> bool {
  5319. StringRef Name;
  5320. SMLoc Loc = getTok().getLoc();
  5321. if (parseIdentifier(Name))
  5322. return Error(Loc, "expected identifier");
  5323. MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
  5324. // Assembler local symbols don't make any sense here. Complain loudly.
  5325. if (Sym->isTemporary())
  5326. return Error(Loc, "non-local symbol required");
  5327. if (!getStreamer().emitSymbolAttribute(Sym, Attr))
  5328. return Error(Loc, "unable to emit symbol attribute");
  5329. return false;
  5330. };
  5331. if (parseMany(parseOp))
  5332. return addErrorSuffix(" in directive");
  5333. return false;
  5334. }
  5335. /// parseDirectiveComm
  5336. /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
  5337. bool MasmParser::parseDirectiveComm(bool IsLocal) {
  5338. if (checkForValidSection())
  5339. return true;
  5340. SMLoc IDLoc = getLexer().getLoc();
  5341. StringRef Name;
  5342. if (parseIdentifier(Name))
  5343. return TokError("expected identifier in directive");
  5344. // Handle the identifier as the key symbol.
  5345. MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
  5346. if (getLexer().isNot(AsmToken::Comma))
  5347. return TokError("unexpected token in directive");
  5348. Lex();
  5349. int64_t Size;
  5350. SMLoc SizeLoc = getLexer().getLoc();
  5351. if (parseAbsoluteExpression(Size))
  5352. return true;
  5353. int64_t Pow2Alignment = 0;
  5354. SMLoc Pow2AlignmentLoc;
  5355. if (getLexer().is(AsmToken::Comma)) {
  5356. Lex();
  5357. Pow2AlignmentLoc = getLexer().getLoc();
  5358. if (parseAbsoluteExpression(Pow2Alignment))
  5359. return true;
  5360. LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
  5361. if (IsLocal && LCOMM == LCOMM::NoAlignment)
  5362. return Error(Pow2AlignmentLoc, "alignment not supported on this target");
  5363. // If this target takes alignments in bytes (not log) validate and convert.
  5364. if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
  5365. (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
  5366. if (!isPowerOf2_64(Pow2Alignment))
  5367. return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
  5368. Pow2Alignment = Log2_64(Pow2Alignment);
  5369. }
  5370. }
  5371. if (parseToken(AsmToken::EndOfStatement,
  5372. "unexpected token in '.comm' or '.lcomm' directive"))
  5373. return true;
  5374. // NOTE: a size of zero for a .comm should create a undefined symbol
  5375. // but a size of .lcomm creates a bss symbol of size zero.
  5376. if (Size < 0)
  5377. return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
  5378. "be less than zero");
  5379. // NOTE: The alignment in the directive is a power of 2 value, the assembler
  5380. // may internally end up wanting an alignment in bytes.
  5381. // FIXME: Diagnose overflow.
  5382. if (Pow2Alignment < 0)
  5383. return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
  5384. "alignment, can't be less than zero");
  5385. Sym->redefineIfPossible();
  5386. if (!Sym->isUndefined())
  5387. return Error(IDLoc, "invalid symbol redefinition");
  5388. // Create the Symbol as a common or local common with Size and Pow2Alignment.
  5389. if (IsLocal) {
  5390. getStreamer().emitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
  5391. return false;
  5392. }
  5393. getStreamer().emitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
  5394. return false;
  5395. }
  5396. /// parseDirectiveComment
  5397. /// ::= comment delimiter [[text]]
  5398. /// [[text]]
  5399. /// [[text]] delimiter [[text]]
  5400. bool MasmParser::parseDirectiveComment(SMLoc DirectiveLoc) {
  5401. std::string FirstLine = parseStringTo(AsmToken::EndOfStatement);
  5402. size_t DelimiterEnd = FirstLine.find_first_of("\b\t\v\f\r\x1A ");
  5403. StringRef Delimiter = StringRef(FirstLine).take_front(DelimiterEnd);
  5404. if (Delimiter.empty())
  5405. return Error(DirectiveLoc, "no delimiter in 'comment' directive");
  5406. do {
  5407. if (getTok().is(AsmToken::Eof))
  5408. return Error(DirectiveLoc, "unmatched delimiter in 'comment' directive");
  5409. Lex(); // eat end of statement
  5410. } while (
  5411. !StringRef(parseStringTo(AsmToken::EndOfStatement)).contains(Delimiter));
  5412. return parseToken(AsmToken::EndOfStatement,
  5413. "unexpected token in 'comment' directive");
  5414. }
  5415. /// parseDirectiveInclude
  5416. /// ::= include <filename>
  5417. /// | include filename
  5418. bool MasmParser::parseDirectiveInclude() {
  5419. // Allow the strings to have escaped octal character sequence.
  5420. std::string Filename;
  5421. SMLoc IncludeLoc = getTok().getLoc();
  5422. if (parseAngleBracketString(Filename))
  5423. Filename = parseStringTo(AsmToken::EndOfStatement);
  5424. if (check(Filename.empty(), "missing filename in 'include' directive") ||
  5425. check(getTok().isNot(AsmToken::EndOfStatement),
  5426. "unexpected token in 'include' directive") ||
  5427. // Attempt to switch the lexer to the included file before consuming the
  5428. // end of statement to avoid losing it when we switch.
  5429. check(enterIncludeFile(Filename), IncludeLoc,
  5430. "Could not find include file '" + Filename + "'"))
  5431. return true;
  5432. return false;
  5433. }
  5434. /// parseDirectiveIf
  5435. /// ::= .if{,eq,ge,gt,le,lt,ne} expression
  5436. bool MasmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
  5437. TheCondStack.push_back(TheCondState);
  5438. TheCondState.TheCond = AsmCond::IfCond;
  5439. if (TheCondState.Ignore) {
  5440. eatToEndOfStatement();
  5441. } else {
  5442. int64_t ExprValue;
  5443. if (parseAbsoluteExpression(ExprValue) ||
  5444. parseToken(AsmToken::EndOfStatement,
  5445. "unexpected token in '.if' directive"))
  5446. return true;
  5447. switch (DirKind) {
  5448. default:
  5449. llvm_unreachable("unsupported directive");
  5450. case DK_IF:
  5451. break;
  5452. case DK_IFE:
  5453. ExprValue = ExprValue == 0;
  5454. break;
  5455. }
  5456. TheCondState.CondMet = ExprValue;
  5457. TheCondState.Ignore = !TheCondState.CondMet;
  5458. }
  5459. return false;
  5460. }
  5461. /// parseDirectiveIfb
  5462. /// ::= .ifb textitem
  5463. bool MasmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
  5464. TheCondStack.push_back(TheCondState);
  5465. TheCondState.TheCond = AsmCond::IfCond;
  5466. if (TheCondState.Ignore) {
  5467. eatToEndOfStatement();
  5468. } else {
  5469. std::string Str;
  5470. if (parseTextItem(Str))
  5471. return TokError("expected text item parameter for 'ifb' directive");
  5472. if (parseToken(AsmToken::EndOfStatement,
  5473. "unexpected token in 'ifb' directive"))
  5474. return true;
  5475. TheCondState.CondMet = ExpectBlank == Str.empty();
  5476. TheCondState.Ignore = !TheCondState.CondMet;
  5477. }
  5478. return false;
  5479. }
  5480. /// parseDirectiveIfidn
  5481. /// ::= ifidn textitem, textitem
  5482. bool MasmParser::parseDirectiveIfidn(SMLoc DirectiveLoc, bool ExpectEqual,
  5483. bool CaseInsensitive) {
  5484. std::string String1, String2;
  5485. if (parseTextItem(String1)) {
  5486. if (ExpectEqual)
  5487. return TokError("expected text item parameter for 'ifidn' directive");
  5488. return TokError("expected text item parameter for 'ifdif' directive");
  5489. }
  5490. if (Lexer.isNot(AsmToken::Comma)) {
  5491. if (ExpectEqual)
  5492. return TokError(
  5493. "expected comma after first string for 'ifidn' directive");
  5494. return TokError("expected comma after first string for 'ifdif' directive");
  5495. }
  5496. Lex();
  5497. if (parseTextItem(String2)) {
  5498. if (ExpectEqual)
  5499. return TokError("expected text item parameter for 'ifidn' directive");
  5500. return TokError("expected text item parameter for 'ifdif' directive");
  5501. }
  5502. TheCondStack.push_back(TheCondState);
  5503. TheCondState.TheCond = AsmCond::IfCond;
  5504. if (CaseInsensitive)
  5505. TheCondState.CondMet =
  5506. ExpectEqual == (StringRef(String1).equals_insensitive(String2));
  5507. else
  5508. TheCondState.CondMet = ExpectEqual == (String1 == String2);
  5509. TheCondState.Ignore = !TheCondState.CondMet;
  5510. return false;
  5511. }
  5512. /// parseDirectiveIfdef
  5513. /// ::= ifdef symbol
  5514. /// | ifdef variable
  5515. bool MasmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
  5516. TheCondStack.push_back(TheCondState);
  5517. TheCondState.TheCond = AsmCond::IfCond;
  5518. if (TheCondState.Ignore) {
  5519. eatToEndOfStatement();
  5520. } else {
  5521. bool is_defined = false;
  5522. unsigned RegNo;
  5523. SMLoc StartLoc, EndLoc;
  5524. is_defined = (getTargetParser().tryParseRegister(
  5525. RegNo, StartLoc, EndLoc) == MatchOperand_Success);
  5526. if (!is_defined) {
  5527. StringRef Name;
  5528. if (check(parseIdentifier(Name), "expected identifier after 'ifdef'") ||
  5529. parseToken(AsmToken::EndOfStatement, "unexpected token in 'ifdef'"))
  5530. return true;
  5531. if (BuiltinSymbolMap.find(Name.lower()) != BuiltinSymbolMap.end()) {
  5532. is_defined = true;
  5533. } else if (Variables.find(Name.lower()) != Variables.end()) {
  5534. is_defined = true;
  5535. } else {
  5536. MCSymbol *Sym = getContext().lookupSymbol(Name.lower());
  5537. is_defined = (Sym && !Sym->isUndefined(false));
  5538. }
  5539. }
  5540. TheCondState.CondMet = (is_defined == expect_defined);
  5541. TheCondState.Ignore = !TheCondState.CondMet;
  5542. }
  5543. return false;
  5544. }
  5545. /// parseDirectiveElseIf
  5546. /// ::= elseif expression
  5547. bool MasmParser::parseDirectiveElseIf(SMLoc DirectiveLoc,
  5548. DirectiveKind DirKind) {
  5549. if (TheCondState.TheCond != AsmCond::IfCond &&
  5550. TheCondState.TheCond != AsmCond::ElseIfCond)
  5551. return Error(DirectiveLoc, "Encountered a .elseif that doesn't follow an"
  5552. " .if or an .elseif");
  5553. TheCondState.TheCond = AsmCond::ElseIfCond;
  5554. bool LastIgnoreState = false;
  5555. if (!TheCondStack.empty())
  5556. LastIgnoreState = TheCondStack.back().Ignore;
  5557. if (LastIgnoreState || TheCondState.CondMet) {
  5558. TheCondState.Ignore = true;
  5559. eatToEndOfStatement();
  5560. } else {
  5561. int64_t ExprValue;
  5562. if (parseAbsoluteExpression(ExprValue))
  5563. return true;
  5564. if (parseToken(AsmToken::EndOfStatement,
  5565. "unexpected token in '.elseif' directive"))
  5566. return true;
  5567. switch (DirKind) {
  5568. default:
  5569. llvm_unreachable("unsupported directive");
  5570. case DK_ELSEIF:
  5571. break;
  5572. case DK_ELSEIFE:
  5573. ExprValue = ExprValue == 0;
  5574. break;
  5575. }
  5576. TheCondState.CondMet = ExprValue;
  5577. TheCondState.Ignore = !TheCondState.CondMet;
  5578. }
  5579. return false;
  5580. }
  5581. /// parseDirectiveElseIfb
  5582. /// ::= elseifb textitem
  5583. bool MasmParser::parseDirectiveElseIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
  5584. if (TheCondState.TheCond != AsmCond::IfCond &&
  5585. TheCondState.TheCond != AsmCond::ElseIfCond)
  5586. return Error(DirectiveLoc, "Encountered an elseif that doesn't follow an"
  5587. " if or an elseif");
  5588. TheCondState.TheCond = AsmCond::ElseIfCond;
  5589. bool LastIgnoreState = false;
  5590. if (!TheCondStack.empty())
  5591. LastIgnoreState = TheCondStack.back().Ignore;
  5592. if (LastIgnoreState || TheCondState.CondMet) {
  5593. TheCondState.Ignore = true;
  5594. eatToEndOfStatement();
  5595. } else {
  5596. std::string Str;
  5597. if (parseTextItem(Str)) {
  5598. if (ExpectBlank)
  5599. return TokError("expected text item parameter for 'elseifb' directive");
  5600. return TokError("expected text item parameter for 'elseifnb' directive");
  5601. }
  5602. if (parseToken(AsmToken::EndOfStatement,
  5603. "unexpected token in 'elseifb' directive"))
  5604. return true;
  5605. TheCondState.CondMet = ExpectBlank == Str.empty();
  5606. TheCondState.Ignore = !TheCondState.CondMet;
  5607. }
  5608. return false;
  5609. }
  5610. /// parseDirectiveElseIfdef
  5611. /// ::= elseifdef symbol
  5612. /// | elseifdef variable
  5613. bool MasmParser::parseDirectiveElseIfdef(SMLoc DirectiveLoc,
  5614. bool expect_defined) {
  5615. if (TheCondState.TheCond != AsmCond::IfCond &&
  5616. TheCondState.TheCond != AsmCond::ElseIfCond)
  5617. return Error(DirectiveLoc, "Encountered an elseif that doesn't follow an"
  5618. " if or an elseif");
  5619. TheCondState.TheCond = AsmCond::ElseIfCond;
  5620. bool LastIgnoreState = false;
  5621. if (!TheCondStack.empty())
  5622. LastIgnoreState = TheCondStack.back().Ignore;
  5623. if (LastIgnoreState || TheCondState.CondMet) {
  5624. TheCondState.Ignore = true;
  5625. eatToEndOfStatement();
  5626. } else {
  5627. bool is_defined = false;
  5628. unsigned RegNo;
  5629. SMLoc StartLoc, EndLoc;
  5630. is_defined = (getTargetParser().tryParseRegister(RegNo, StartLoc, EndLoc) ==
  5631. MatchOperand_Success);
  5632. if (!is_defined) {
  5633. StringRef Name;
  5634. if (check(parseIdentifier(Name),
  5635. "expected identifier after 'elseifdef'") ||
  5636. parseToken(AsmToken::EndOfStatement,
  5637. "unexpected token in 'elseifdef'"))
  5638. return true;
  5639. if (BuiltinSymbolMap.find(Name.lower()) != BuiltinSymbolMap.end()) {
  5640. is_defined = true;
  5641. } else if (Variables.find(Name.lower()) != Variables.end()) {
  5642. is_defined = true;
  5643. } else {
  5644. MCSymbol *Sym = getContext().lookupSymbol(Name);
  5645. is_defined = (Sym && !Sym->isUndefined(false));
  5646. }
  5647. }
  5648. TheCondState.CondMet = (is_defined == expect_defined);
  5649. TheCondState.Ignore = !TheCondState.CondMet;
  5650. }
  5651. return false;
  5652. }
  5653. /// parseDirectiveElseIfidn
  5654. /// ::= elseifidn textitem, textitem
  5655. bool MasmParser::parseDirectiveElseIfidn(SMLoc DirectiveLoc, bool ExpectEqual,
  5656. bool CaseInsensitive) {
  5657. if (TheCondState.TheCond != AsmCond::IfCond &&
  5658. TheCondState.TheCond != AsmCond::ElseIfCond)
  5659. return Error(DirectiveLoc, "Encountered an elseif that doesn't follow an"
  5660. " if or an elseif");
  5661. TheCondState.TheCond = AsmCond::ElseIfCond;
  5662. bool LastIgnoreState = false;
  5663. if (!TheCondStack.empty())
  5664. LastIgnoreState = TheCondStack.back().Ignore;
  5665. if (LastIgnoreState || TheCondState.CondMet) {
  5666. TheCondState.Ignore = true;
  5667. eatToEndOfStatement();
  5668. } else {
  5669. std::string String1, String2;
  5670. if (parseTextItem(String1)) {
  5671. if (ExpectEqual)
  5672. return TokError(
  5673. "expected text item parameter for 'elseifidn' directive");
  5674. return TokError("expected text item parameter for 'elseifdif' directive");
  5675. }
  5676. if (Lexer.isNot(AsmToken::Comma)) {
  5677. if (ExpectEqual)
  5678. return TokError(
  5679. "expected comma after first string for 'elseifidn' directive");
  5680. return TokError(
  5681. "expected comma after first string for 'elseifdif' directive");
  5682. }
  5683. Lex();
  5684. if (parseTextItem(String2)) {
  5685. if (ExpectEqual)
  5686. return TokError(
  5687. "expected text item parameter for 'elseifidn' directive");
  5688. return TokError("expected text item parameter for 'elseifdif' directive");
  5689. }
  5690. if (CaseInsensitive)
  5691. TheCondState.CondMet =
  5692. ExpectEqual == (StringRef(String1).equals_insensitive(String2));
  5693. else
  5694. TheCondState.CondMet = ExpectEqual == (String1 == String2);
  5695. TheCondState.Ignore = !TheCondState.CondMet;
  5696. }
  5697. return false;
  5698. }
  5699. /// parseDirectiveElse
  5700. /// ::= else
  5701. bool MasmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
  5702. if (parseToken(AsmToken::EndOfStatement,
  5703. "unexpected token in 'else' directive"))
  5704. return true;
  5705. if (TheCondState.TheCond != AsmCond::IfCond &&
  5706. TheCondState.TheCond != AsmCond::ElseIfCond)
  5707. return Error(DirectiveLoc, "Encountered an else that doesn't follow an if"
  5708. " or an elseif");
  5709. TheCondState.TheCond = AsmCond::ElseCond;
  5710. bool LastIgnoreState = false;
  5711. if (!TheCondStack.empty())
  5712. LastIgnoreState = TheCondStack.back().Ignore;
  5713. if (LastIgnoreState || TheCondState.CondMet)
  5714. TheCondState.Ignore = true;
  5715. else
  5716. TheCondState.Ignore = false;
  5717. return false;
  5718. }
  5719. /// parseDirectiveEnd
  5720. /// ::= end
  5721. bool MasmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
  5722. if (parseToken(AsmToken::EndOfStatement,
  5723. "unexpected token in 'end' directive"))
  5724. return true;
  5725. while (Lexer.isNot(AsmToken::Eof))
  5726. Lexer.Lex();
  5727. return false;
  5728. }
  5729. /// parseDirectiveError
  5730. /// ::= .err [message]
  5731. bool MasmParser::parseDirectiveError(SMLoc DirectiveLoc) {
  5732. if (!TheCondStack.empty()) {
  5733. if (TheCondStack.back().Ignore) {
  5734. eatToEndOfStatement();
  5735. return false;
  5736. }
  5737. }
  5738. std::string Message = ".err directive invoked in source file";
  5739. if (Lexer.isNot(AsmToken::EndOfStatement))
  5740. Message = parseStringTo(AsmToken::EndOfStatement);
  5741. Lex();
  5742. return Error(DirectiveLoc, Message);
  5743. }
  5744. /// parseDirectiveErrorIfb
  5745. /// ::= .errb textitem[, message]
  5746. bool MasmParser::parseDirectiveErrorIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
  5747. if (!TheCondStack.empty()) {
  5748. if (TheCondStack.back().Ignore) {
  5749. eatToEndOfStatement();
  5750. return false;
  5751. }
  5752. }
  5753. std::string Text;
  5754. if (parseTextItem(Text))
  5755. return Error(getTok().getLoc(), "missing text item in '.errb' directive");
  5756. std::string Message = ".errb directive invoked in source file";
  5757. if (Lexer.isNot(AsmToken::EndOfStatement)) {
  5758. if (parseToken(AsmToken::Comma))
  5759. return addErrorSuffix(" in '.errb' directive");
  5760. Message = parseStringTo(AsmToken::EndOfStatement);
  5761. }
  5762. Lex();
  5763. if (Text.empty() == ExpectBlank)
  5764. return Error(DirectiveLoc, Message);
  5765. return false;
  5766. }
  5767. /// parseDirectiveErrorIfdef
  5768. /// ::= .errdef name[, message]
  5769. bool MasmParser::parseDirectiveErrorIfdef(SMLoc DirectiveLoc,
  5770. bool ExpectDefined) {
  5771. if (!TheCondStack.empty()) {
  5772. if (TheCondStack.back().Ignore) {
  5773. eatToEndOfStatement();
  5774. return false;
  5775. }
  5776. }
  5777. bool IsDefined = false;
  5778. unsigned RegNo;
  5779. SMLoc StartLoc, EndLoc;
  5780. IsDefined = (getTargetParser().tryParseRegister(RegNo, StartLoc, EndLoc) ==
  5781. MatchOperand_Success);
  5782. if (!IsDefined) {
  5783. StringRef Name;
  5784. if (check(parseIdentifier(Name), "expected identifier after '.errdef'"))
  5785. return true;
  5786. if (BuiltinSymbolMap.find(Name.lower()) != BuiltinSymbolMap.end()) {
  5787. IsDefined = true;
  5788. } else if (Variables.find(Name.lower()) != Variables.end()) {
  5789. IsDefined = true;
  5790. } else {
  5791. MCSymbol *Sym = getContext().lookupSymbol(Name);
  5792. IsDefined = (Sym && !Sym->isUndefined(false));
  5793. }
  5794. }
  5795. std::string Message = ".errdef directive invoked in source file";
  5796. if (Lexer.isNot(AsmToken::EndOfStatement)) {
  5797. if (parseToken(AsmToken::Comma))
  5798. return addErrorSuffix(" in '.errdef' directive");
  5799. Message = parseStringTo(AsmToken::EndOfStatement);
  5800. }
  5801. Lex();
  5802. if (IsDefined == ExpectDefined)
  5803. return Error(DirectiveLoc, Message);
  5804. return false;
  5805. }
  5806. /// parseDirectiveErrorIfidn
  5807. /// ::= .erridn textitem, textitem[, message]
  5808. bool MasmParser::parseDirectiveErrorIfidn(SMLoc DirectiveLoc, bool ExpectEqual,
  5809. bool CaseInsensitive) {
  5810. if (!TheCondStack.empty()) {
  5811. if (TheCondStack.back().Ignore) {
  5812. eatToEndOfStatement();
  5813. return false;
  5814. }
  5815. }
  5816. std::string String1, String2;
  5817. if (parseTextItem(String1)) {
  5818. if (ExpectEqual)
  5819. return TokError("expected string parameter for '.erridn' directive");
  5820. return TokError("expected string parameter for '.errdif' directive");
  5821. }
  5822. if (Lexer.isNot(AsmToken::Comma)) {
  5823. if (ExpectEqual)
  5824. return TokError(
  5825. "expected comma after first string for '.erridn' directive");
  5826. return TokError(
  5827. "expected comma after first string for '.errdif' directive");
  5828. }
  5829. Lex();
  5830. if (parseTextItem(String2)) {
  5831. if (ExpectEqual)
  5832. return TokError("expected string parameter for '.erridn' directive");
  5833. return TokError("expected string parameter for '.errdif' directive");
  5834. }
  5835. std::string Message;
  5836. if (ExpectEqual)
  5837. Message = ".erridn directive invoked in source file";
  5838. else
  5839. Message = ".errdif directive invoked in source file";
  5840. if (Lexer.isNot(AsmToken::EndOfStatement)) {
  5841. if (parseToken(AsmToken::Comma))
  5842. return addErrorSuffix(" in '.erridn' directive");
  5843. Message = parseStringTo(AsmToken::EndOfStatement);
  5844. }
  5845. Lex();
  5846. if (CaseInsensitive)
  5847. TheCondState.CondMet =
  5848. ExpectEqual == (StringRef(String1).equals_insensitive(String2));
  5849. else
  5850. TheCondState.CondMet = ExpectEqual == (String1 == String2);
  5851. TheCondState.Ignore = !TheCondState.CondMet;
  5852. if ((CaseInsensitive &&
  5853. ExpectEqual == StringRef(String1).equals_insensitive(String2)) ||
  5854. (ExpectEqual == (String1 == String2)))
  5855. return Error(DirectiveLoc, Message);
  5856. return false;
  5857. }
  5858. /// parseDirectiveErrorIfe
  5859. /// ::= .erre expression[, message]
  5860. bool MasmParser::parseDirectiveErrorIfe(SMLoc DirectiveLoc, bool ExpectZero) {
  5861. if (!TheCondStack.empty()) {
  5862. if (TheCondStack.back().Ignore) {
  5863. eatToEndOfStatement();
  5864. return false;
  5865. }
  5866. }
  5867. int64_t ExprValue;
  5868. if (parseAbsoluteExpression(ExprValue))
  5869. return addErrorSuffix(" in '.erre' directive");
  5870. std::string Message = ".erre directive invoked in source file";
  5871. if (Lexer.isNot(AsmToken::EndOfStatement)) {
  5872. if (parseToken(AsmToken::Comma))
  5873. return addErrorSuffix(" in '.erre' directive");
  5874. Message = parseStringTo(AsmToken::EndOfStatement);
  5875. }
  5876. Lex();
  5877. if ((ExprValue == 0) == ExpectZero)
  5878. return Error(DirectiveLoc, Message);
  5879. return false;
  5880. }
  5881. /// parseDirectiveEndIf
  5882. /// ::= .endif
  5883. bool MasmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
  5884. if (parseToken(AsmToken::EndOfStatement,
  5885. "unexpected token in '.endif' directive"))
  5886. return true;
  5887. if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
  5888. return Error(DirectiveLoc, "Encountered a .endif that doesn't follow "
  5889. "an .if or .else");
  5890. if (!TheCondStack.empty()) {
  5891. TheCondState = TheCondStack.back();
  5892. TheCondStack.pop_back();
  5893. }
  5894. return false;
  5895. }
  5896. void MasmParser::initializeDirectiveKindMap() {
  5897. DirectiveKindMap["="] = DK_ASSIGN;
  5898. DirectiveKindMap["equ"] = DK_EQU;
  5899. DirectiveKindMap["textequ"] = DK_TEXTEQU;
  5900. // DirectiveKindMap[".ascii"] = DK_ASCII;
  5901. // DirectiveKindMap[".asciz"] = DK_ASCIZ;
  5902. // DirectiveKindMap[".string"] = DK_STRING;
  5903. DirectiveKindMap["byte"] = DK_BYTE;
  5904. DirectiveKindMap["sbyte"] = DK_SBYTE;
  5905. DirectiveKindMap["word"] = DK_WORD;
  5906. DirectiveKindMap["sword"] = DK_SWORD;
  5907. DirectiveKindMap["dword"] = DK_DWORD;
  5908. DirectiveKindMap["sdword"] = DK_SDWORD;
  5909. DirectiveKindMap["fword"] = DK_FWORD;
  5910. DirectiveKindMap["qword"] = DK_QWORD;
  5911. DirectiveKindMap["sqword"] = DK_SQWORD;
  5912. DirectiveKindMap["real4"] = DK_REAL4;
  5913. DirectiveKindMap["real8"] = DK_REAL8;
  5914. DirectiveKindMap["real10"] = DK_REAL10;
  5915. DirectiveKindMap["align"] = DK_ALIGN;
  5916. DirectiveKindMap["even"] = DK_EVEN;
  5917. DirectiveKindMap["org"] = DK_ORG;
  5918. DirectiveKindMap["extern"] = DK_EXTERN;
  5919. DirectiveKindMap["public"] = DK_PUBLIC;
  5920. // DirectiveKindMap[".comm"] = DK_COMM;
  5921. DirectiveKindMap["comment"] = DK_COMMENT;
  5922. DirectiveKindMap["include"] = DK_INCLUDE;
  5923. DirectiveKindMap["repeat"] = DK_REPEAT;
  5924. DirectiveKindMap["rept"] = DK_REPEAT;
  5925. DirectiveKindMap["while"] = DK_WHILE;
  5926. DirectiveKindMap["for"] = DK_FOR;
  5927. DirectiveKindMap["irp"] = DK_FOR;
  5928. DirectiveKindMap["forc"] = DK_FORC;
  5929. DirectiveKindMap["irpc"] = DK_FORC;
  5930. DirectiveKindMap["if"] = DK_IF;
  5931. DirectiveKindMap["ife"] = DK_IFE;
  5932. DirectiveKindMap["ifb"] = DK_IFB;
  5933. DirectiveKindMap["ifnb"] = DK_IFNB;
  5934. DirectiveKindMap["ifdef"] = DK_IFDEF;
  5935. DirectiveKindMap["ifndef"] = DK_IFNDEF;
  5936. DirectiveKindMap["ifdif"] = DK_IFDIF;
  5937. DirectiveKindMap["ifdifi"] = DK_IFDIFI;
  5938. DirectiveKindMap["ifidn"] = DK_IFIDN;
  5939. DirectiveKindMap["ifidni"] = DK_IFIDNI;
  5940. DirectiveKindMap["elseif"] = DK_ELSEIF;
  5941. DirectiveKindMap["elseifdef"] = DK_ELSEIFDEF;
  5942. DirectiveKindMap["elseifndef"] = DK_ELSEIFNDEF;
  5943. DirectiveKindMap["elseifdif"] = DK_ELSEIFDIF;
  5944. DirectiveKindMap["elseifidn"] = DK_ELSEIFIDN;
  5945. DirectiveKindMap["else"] = DK_ELSE;
  5946. DirectiveKindMap["end"] = DK_END;
  5947. DirectiveKindMap["endif"] = DK_ENDIF;
  5948. // DirectiveKindMap[".file"] = DK_FILE;
  5949. // DirectiveKindMap[".line"] = DK_LINE;
  5950. // DirectiveKindMap[".loc"] = DK_LOC;
  5951. // DirectiveKindMap[".stabs"] = DK_STABS;
  5952. // DirectiveKindMap[".cv_file"] = DK_CV_FILE;
  5953. // DirectiveKindMap[".cv_func_id"] = DK_CV_FUNC_ID;
  5954. // DirectiveKindMap[".cv_loc"] = DK_CV_LOC;
  5955. // DirectiveKindMap[".cv_linetable"] = DK_CV_LINETABLE;
  5956. // DirectiveKindMap[".cv_inline_linetable"] = DK_CV_INLINE_LINETABLE;
  5957. // DirectiveKindMap[".cv_inline_site_id"] = DK_CV_INLINE_SITE_ID;
  5958. // DirectiveKindMap[".cv_def_range"] = DK_CV_DEF_RANGE;
  5959. // DirectiveKindMap[".cv_string"] = DK_CV_STRING;
  5960. // DirectiveKindMap[".cv_stringtable"] = DK_CV_STRINGTABLE;
  5961. // DirectiveKindMap[".cv_filechecksums"] = DK_CV_FILECHECKSUMS;
  5962. // DirectiveKindMap[".cv_filechecksumoffset"] = DK_CV_FILECHECKSUM_OFFSET;
  5963. // DirectiveKindMap[".cv_fpo_data"] = DK_CV_FPO_DATA;
  5964. // DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
  5965. // DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
  5966. // DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
  5967. // DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
  5968. // DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
  5969. // DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
  5970. // DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
  5971. // DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
  5972. // DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
  5973. // DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
  5974. // DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
  5975. // DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
  5976. // DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
  5977. // DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
  5978. // DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
  5979. // DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
  5980. // DirectiveKindMap[".cfi_return_column"] = DK_CFI_RETURN_COLUMN;
  5981. // DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
  5982. // DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
  5983. // DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
  5984. // DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
  5985. // DirectiveKindMap[".cfi_b_key_frame"] = DK_CFI_B_KEY_FRAME;
  5986. DirectiveKindMap["macro"] = DK_MACRO;
  5987. DirectiveKindMap["exitm"] = DK_EXITM;
  5988. DirectiveKindMap["endm"] = DK_ENDM;
  5989. DirectiveKindMap["purge"] = DK_PURGE;
  5990. DirectiveKindMap[".err"] = DK_ERR;
  5991. DirectiveKindMap[".errb"] = DK_ERRB;
  5992. DirectiveKindMap[".errnb"] = DK_ERRNB;
  5993. DirectiveKindMap[".errdef"] = DK_ERRDEF;
  5994. DirectiveKindMap[".errndef"] = DK_ERRNDEF;
  5995. DirectiveKindMap[".errdif"] = DK_ERRDIF;
  5996. DirectiveKindMap[".errdifi"] = DK_ERRDIFI;
  5997. DirectiveKindMap[".erridn"] = DK_ERRIDN;
  5998. DirectiveKindMap[".erridni"] = DK_ERRIDNI;
  5999. DirectiveKindMap[".erre"] = DK_ERRE;
  6000. DirectiveKindMap[".errnz"] = DK_ERRNZ;
  6001. DirectiveKindMap[".pushframe"] = DK_PUSHFRAME;
  6002. DirectiveKindMap[".pushreg"] = DK_PUSHREG;
  6003. DirectiveKindMap[".savereg"] = DK_SAVEREG;
  6004. DirectiveKindMap[".savexmm128"] = DK_SAVEXMM128;
  6005. DirectiveKindMap[".setframe"] = DK_SETFRAME;
  6006. DirectiveKindMap[".radix"] = DK_RADIX;
  6007. DirectiveKindMap["db"] = DK_DB;
  6008. DirectiveKindMap["dd"] = DK_DD;
  6009. DirectiveKindMap["df"] = DK_DF;
  6010. DirectiveKindMap["dq"] = DK_DQ;
  6011. DirectiveKindMap["dw"] = DK_DW;
  6012. DirectiveKindMap["echo"] = DK_ECHO;
  6013. DirectiveKindMap["struc"] = DK_STRUCT;
  6014. DirectiveKindMap["struct"] = DK_STRUCT;
  6015. DirectiveKindMap["union"] = DK_UNION;
  6016. DirectiveKindMap["ends"] = DK_ENDS;
  6017. }
  6018. bool MasmParser::isMacroLikeDirective() {
  6019. if (getLexer().is(AsmToken::Identifier)) {
  6020. bool IsMacroLike = StringSwitch<bool>(getTok().getIdentifier())
  6021. .CasesLower("repeat", "rept", true)
  6022. .CaseLower("while", true)
  6023. .CasesLower("for", "irp", true)
  6024. .CasesLower("forc", "irpc", true)
  6025. .Default(false);
  6026. if (IsMacroLike)
  6027. return true;
  6028. }
  6029. if (peekTok().is(AsmToken::Identifier) &&
  6030. peekTok().getIdentifier().equals_insensitive("macro"))
  6031. return true;
  6032. return false;
  6033. }
  6034. MCAsmMacro *MasmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
  6035. AsmToken EndToken, StartToken = getTok();
  6036. unsigned NestLevel = 0;
  6037. while (true) {
  6038. // Check whether we have reached the end of the file.
  6039. if (getLexer().is(AsmToken::Eof)) {
  6040. printError(DirectiveLoc, "no matching 'endm' in definition");
  6041. return nullptr;
  6042. }
  6043. if (isMacroLikeDirective())
  6044. ++NestLevel;
  6045. // Otherwise, check whether we have reached the endm.
  6046. if (Lexer.is(AsmToken::Identifier) &&
  6047. getTok().getIdentifier().equals_insensitive("endm")) {
  6048. if (NestLevel == 0) {
  6049. EndToken = getTok();
  6050. Lex();
  6051. if (Lexer.isNot(AsmToken::EndOfStatement)) {
  6052. printError(getTok().getLoc(), "unexpected token in 'endm' directive");
  6053. return nullptr;
  6054. }
  6055. break;
  6056. }
  6057. --NestLevel;
  6058. }
  6059. // Otherwise, scan till the end of the statement.
  6060. eatToEndOfStatement();
  6061. }
  6062. const char *BodyStart = StartToken.getLoc().getPointer();
  6063. const char *BodyEnd = EndToken.getLoc().getPointer();
  6064. StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
  6065. // We Are Anonymous.
  6066. MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters());
  6067. return &MacroLikeBodies.back();
  6068. }
  6069. bool MasmParser::expandStatement(SMLoc Loc) {
  6070. std::string Body = parseStringTo(AsmToken::EndOfStatement);
  6071. SMLoc EndLoc = getTok().getLoc();
  6072. MCAsmMacroParameters Parameters;
  6073. MCAsmMacroArguments Arguments;
  6074. StringMap<std::string> BuiltinValues;
  6075. for (const auto &S : BuiltinSymbolMap) {
  6076. const BuiltinSymbol &Sym = S.getValue();
  6077. if (llvm::Optional<std::string> Text = evaluateBuiltinTextMacro(Sym, Loc)) {
  6078. BuiltinValues[S.getKey().lower()] = std::move(*Text);
  6079. }
  6080. }
  6081. for (const auto &B : BuiltinValues) {
  6082. MCAsmMacroParameter P;
  6083. MCAsmMacroArgument A;
  6084. P.Name = B.getKey();
  6085. P.Required = true;
  6086. A.push_back(AsmToken(AsmToken::String, B.getValue()));
  6087. Parameters.push_back(std::move(P));
  6088. Arguments.push_back(std::move(A));
  6089. }
  6090. for (const auto &V : Variables) {
  6091. const Variable &Var = V.getValue();
  6092. if (Var.IsText) {
  6093. MCAsmMacroParameter P;
  6094. MCAsmMacroArgument A;
  6095. P.Name = Var.Name;
  6096. P.Required = true;
  6097. A.push_back(AsmToken(AsmToken::String, Var.TextValue));
  6098. Parameters.push_back(std::move(P));
  6099. Arguments.push_back(std::move(A));
  6100. }
  6101. }
  6102. MacroLikeBodies.emplace_back(StringRef(), Body, Parameters);
  6103. MCAsmMacro M = MacroLikeBodies.back();
  6104. // Expand the statement in a new buffer.
  6105. SmallString<80> Buf;
  6106. raw_svector_ostream OS(Buf);
  6107. if (expandMacro(OS, M.Body, M.Parameters, Arguments, M.Locals, EndLoc))
  6108. return true;
  6109. std::unique_ptr<MemoryBuffer> Expansion =
  6110. MemoryBuffer::getMemBufferCopy(OS.str(), "<expansion>");
  6111. // Jump to the expanded statement and prime the lexer.
  6112. CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Expansion), EndLoc);
  6113. Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
  6114. EndStatementAtEOFStack.push_back(false);
  6115. Lex();
  6116. return false;
  6117. }
  6118. void MasmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
  6119. raw_svector_ostream &OS) {
  6120. instantiateMacroLikeBody(M, DirectiveLoc, /*ExitLoc=*/getTok().getLoc(), OS);
  6121. }
  6122. void MasmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
  6123. SMLoc ExitLoc,
  6124. raw_svector_ostream &OS) {
  6125. OS << "endm\n";
  6126. std::unique_ptr<MemoryBuffer> Instantiation =
  6127. MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
  6128. // Create the macro instantiation object and add to the current macro
  6129. // instantiation stack.
  6130. MacroInstantiation *MI = new MacroInstantiation{DirectiveLoc, CurBuffer,
  6131. ExitLoc, TheCondStack.size()};
  6132. ActiveMacros.push_back(MI);
  6133. // Jump to the macro instantiation and prime the lexer.
  6134. CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
  6135. Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
  6136. EndStatementAtEOFStack.push_back(true);
  6137. Lex();
  6138. }
  6139. /// parseDirectiveRepeat
  6140. /// ::= ("repeat" | "rept") count
  6141. /// body
  6142. /// endm
  6143. bool MasmParser::parseDirectiveRepeat(SMLoc DirectiveLoc, StringRef Dir) {
  6144. const MCExpr *CountExpr;
  6145. SMLoc CountLoc = getTok().getLoc();
  6146. if (parseExpression(CountExpr))
  6147. return true;
  6148. int64_t Count;
  6149. if (!CountExpr->evaluateAsAbsolute(Count, getStreamer().getAssemblerPtr())) {
  6150. return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
  6151. }
  6152. if (check(Count < 0, CountLoc, "Count is negative") ||
  6153. parseToken(AsmToken::EndOfStatement,
  6154. "unexpected token in '" + Dir + "' directive"))
  6155. return true;
  6156. // Lex the repeat definition.
  6157. MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
  6158. if (!M)
  6159. return true;
  6160. // Macro instantiation is lexical, unfortunately. We construct a new buffer
  6161. // to hold the macro body with substitutions.
  6162. SmallString<256> Buf;
  6163. raw_svector_ostream OS(Buf);
  6164. while (Count--) {
  6165. if (expandMacro(OS, M->Body, None, None, M->Locals, getTok().getLoc()))
  6166. return true;
  6167. }
  6168. instantiateMacroLikeBody(M, DirectiveLoc, OS);
  6169. return false;
  6170. }
  6171. /// parseDirectiveWhile
  6172. /// ::= "while" expression
  6173. /// body
  6174. /// endm
  6175. bool MasmParser::parseDirectiveWhile(SMLoc DirectiveLoc) {
  6176. const MCExpr *CondExpr;
  6177. SMLoc CondLoc = getTok().getLoc();
  6178. if (parseExpression(CondExpr))
  6179. return true;
  6180. // Lex the repeat definition.
  6181. MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
  6182. if (!M)
  6183. return true;
  6184. // Macro instantiation is lexical, unfortunately. We construct a new buffer
  6185. // to hold the macro body with substitutions.
  6186. SmallString<256> Buf;
  6187. raw_svector_ostream OS(Buf);
  6188. int64_t Condition;
  6189. if (!CondExpr->evaluateAsAbsolute(Condition, getStreamer().getAssemblerPtr()))
  6190. return Error(CondLoc, "expected absolute expression in 'while' directive");
  6191. if (Condition) {
  6192. // Instantiate the macro, then resume at this directive to recheck the
  6193. // condition.
  6194. if (expandMacro(OS, M->Body, None, None, M->Locals, getTok().getLoc()))
  6195. return true;
  6196. instantiateMacroLikeBody(M, DirectiveLoc, /*ExitLoc=*/DirectiveLoc, OS);
  6197. }
  6198. return false;
  6199. }
  6200. /// parseDirectiveFor
  6201. /// ::= ("for" | "irp") symbol [":" qualifier], <values>
  6202. /// body
  6203. /// endm
  6204. bool MasmParser::parseDirectiveFor(SMLoc DirectiveLoc, StringRef Dir) {
  6205. MCAsmMacroParameter Parameter;
  6206. MCAsmMacroArguments A;
  6207. if (check(parseIdentifier(Parameter.Name),
  6208. "expected identifier in '" + Dir + "' directive"))
  6209. return true;
  6210. // Parse optional qualifier (default value, or "req")
  6211. if (parseOptionalToken(AsmToken::Colon)) {
  6212. if (parseOptionalToken(AsmToken::Equal)) {
  6213. // Default value
  6214. SMLoc ParamLoc;
  6215. ParamLoc = Lexer.getLoc();
  6216. if (parseMacroArgument(nullptr, Parameter.Value))
  6217. return true;
  6218. } else {
  6219. SMLoc QualLoc;
  6220. StringRef Qualifier;
  6221. QualLoc = Lexer.getLoc();
  6222. if (parseIdentifier(Qualifier))
  6223. return Error(QualLoc, "missing parameter qualifier for "
  6224. "'" +
  6225. Parameter.Name + "' in '" + Dir +
  6226. "' directive");
  6227. if (Qualifier.equals_insensitive("req"))
  6228. Parameter.Required = true;
  6229. else
  6230. return Error(QualLoc,
  6231. Qualifier + " is not a valid parameter qualifier for '" +
  6232. Parameter.Name + "' in '" + Dir + "' directive");
  6233. }
  6234. }
  6235. if (parseToken(AsmToken::Comma,
  6236. "expected comma in '" + Dir + "' directive") ||
  6237. parseToken(AsmToken::Less,
  6238. "values in '" + Dir +
  6239. "' directive must be enclosed in angle brackets"))
  6240. return true;
  6241. while (true) {
  6242. A.emplace_back();
  6243. if (parseMacroArgument(&Parameter, A.back(), /*EndTok=*/AsmToken::Greater))
  6244. return addErrorSuffix(" in arguments for '" + Dir + "' directive");
  6245. // If we see a comma, continue, and allow line continuation.
  6246. if (!parseOptionalToken(AsmToken::Comma))
  6247. break;
  6248. parseOptionalToken(AsmToken::EndOfStatement);
  6249. }
  6250. if (parseToken(AsmToken::Greater,
  6251. "values in '" + Dir +
  6252. "' directive must be enclosed in angle brackets") ||
  6253. parseToken(AsmToken::EndOfStatement, "expected End of Statement"))
  6254. return true;
  6255. // Lex the for definition.
  6256. MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
  6257. if (!M)
  6258. return true;
  6259. // Macro instantiation is lexical, unfortunately. We construct a new buffer
  6260. // to hold the macro body with substitutions.
  6261. SmallString<256> Buf;
  6262. raw_svector_ostream OS(Buf);
  6263. for (const MCAsmMacroArgument &Arg : A) {
  6264. if (expandMacro(OS, M->Body, Parameter, Arg, M->Locals, getTok().getLoc()))
  6265. return true;
  6266. }
  6267. instantiateMacroLikeBody(M, DirectiveLoc, OS);
  6268. return false;
  6269. }
  6270. /// parseDirectiveForc
  6271. /// ::= ("forc" | "irpc") symbol, <string>
  6272. /// body
  6273. /// endm
  6274. bool MasmParser::parseDirectiveForc(SMLoc DirectiveLoc, StringRef Directive) {
  6275. MCAsmMacroParameter Parameter;
  6276. std::string Argument;
  6277. if (check(parseIdentifier(Parameter.Name),
  6278. "expected identifier in '" + Directive + "' directive") ||
  6279. parseToken(AsmToken::Comma,
  6280. "expected comma in '" + Directive + "' directive"))
  6281. return true;
  6282. if (parseAngleBracketString(Argument)) {
  6283. // Match ml64.exe; treat all characters to end of statement as a string,
  6284. // ignoring comment markers, then discard anything following a space (using
  6285. // the C locale).
  6286. Argument = parseStringTo(AsmToken::EndOfStatement);
  6287. if (getTok().is(AsmToken::EndOfStatement))
  6288. Argument += getTok().getString();
  6289. size_t End = 0;
  6290. for (; End < Argument.size(); ++End) {
  6291. if (isSpace(Argument[End]))
  6292. break;
  6293. }
  6294. Argument.resize(End);
  6295. }
  6296. if (parseToken(AsmToken::EndOfStatement, "expected end of statement"))
  6297. return true;
  6298. // Lex the irpc definition.
  6299. MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
  6300. if (!M)
  6301. return true;
  6302. // Macro instantiation is lexical, unfortunately. We construct a new buffer
  6303. // to hold the macro body with substitutions.
  6304. SmallString<256> Buf;
  6305. raw_svector_ostream OS(Buf);
  6306. StringRef Values(Argument);
  6307. for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
  6308. MCAsmMacroArgument Arg;
  6309. Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1));
  6310. if (expandMacro(OS, M->Body, Parameter, Arg, M->Locals, getTok().getLoc()))
  6311. return true;
  6312. }
  6313. instantiateMacroLikeBody(M, DirectiveLoc, OS);
  6314. return false;
  6315. }
  6316. bool MasmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
  6317. size_t Len) {
  6318. const MCExpr *Value;
  6319. SMLoc ExprLoc = getLexer().getLoc();
  6320. if (parseExpression(Value))
  6321. return true;
  6322. const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
  6323. if (!MCE)
  6324. return Error(ExprLoc, "unexpected expression in _emit");
  6325. uint64_t IntValue = MCE->getValue();
  6326. if (!isUInt<8>(IntValue) && !isInt<8>(IntValue))
  6327. return Error(ExprLoc, "literal value out of range for directive");
  6328. Info.AsmRewrites->emplace_back(AOK_Emit, IDLoc, Len);
  6329. return false;
  6330. }
  6331. bool MasmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
  6332. const MCExpr *Value;
  6333. SMLoc ExprLoc = getLexer().getLoc();
  6334. if (parseExpression(Value))
  6335. return true;
  6336. const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
  6337. if (!MCE)
  6338. return Error(ExprLoc, "unexpected expression in align");
  6339. uint64_t IntValue = MCE->getValue();
  6340. if (!isPowerOf2_64(IntValue))
  6341. return Error(ExprLoc, "literal value not a power of two greater then zero");
  6342. Info.AsmRewrites->emplace_back(AOK_Align, IDLoc, 5, Log2_64(IntValue));
  6343. return false;
  6344. }
  6345. bool MasmParser::parseDirectiveRadix(SMLoc DirectiveLoc) {
  6346. const SMLoc Loc = getLexer().getLoc();
  6347. std::string RadixStringRaw = parseStringTo(AsmToken::EndOfStatement);
  6348. StringRef RadixString = StringRef(RadixStringRaw).trim();
  6349. unsigned Radix;
  6350. if (RadixString.getAsInteger(10, Radix)) {
  6351. return Error(Loc,
  6352. "radix must be a decimal number in the range 2 to 16; was " +
  6353. RadixString);
  6354. }
  6355. if (Radix < 2 || Radix > 16)
  6356. return Error(Loc, "radix must be in the range 2 to 16; was " +
  6357. std::to_string(Radix));
  6358. getLexer().setMasmDefaultRadix(Radix);
  6359. return false;
  6360. }
  6361. /// parseDirectiveEcho
  6362. /// ::= "echo" message
  6363. bool MasmParser::parseDirectiveEcho(SMLoc DirectiveLoc) {
  6364. std::string Message = parseStringTo(AsmToken::EndOfStatement);
  6365. llvm::outs() << Message;
  6366. if (!StringRef(Message).endswith("\n"))
  6367. llvm::outs() << '\n';
  6368. return false;
  6369. }
  6370. // We are comparing pointers, but the pointers are relative to a single string.
  6371. // Thus, this should always be deterministic.
  6372. static int rewritesSort(const AsmRewrite *AsmRewriteA,
  6373. const AsmRewrite *AsmRewriteB) {
  6374. if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
  6375. return -1;
  6376. if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
  6377. return 1;
  6378. // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
  6379. // rewrite to the same location. Make sure the SizeDirective rewrite is
  6380. // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
  6381. // ensures the sort algorithm is stable.
  6382. if (AsmRewritePrecedence[AsmRewriteA->Kind] >
  6383. AsmRewritePrecedence[AsmRewriteB->Kind])
  6384. return -1;
  6385. if (AsmRewritePrecedence[AsmRewriteA->Kind] <
  6386. AsmRewritePrecedence[AsmRewriteB->Kind])
  6387. return 1;
  6388. llvm_unreachable("Unstable rewrite sort.");
  6389. }
  6390. bool MasmParser::defineMacro(StringRef Name, StringRef Value) {
  6391. Variable &Var = Variables[Name.lower()];
  6392. if (Var.Name.empty()) {
  6393. Var.Name = Name;
  6394. } else if (Var.Redefinable == Variable::NOT_REDEFINABLE) {
  6395. return Error(SMLoc(), "invalid variable redefinition");
  6396. } else if (Var.Redefinable == Variable::WARN_ON_REDEFINITION &&
  6397. Warning(SMLoc(), "redefining '" + Name +
  6398. "', already defined on the command line")) {
  6399. return true;
  6400. }
  6401. Var.Redefinable = Variable::WARN_ON_REDEFINITION;
  6402. Var.IsText = true;
  6403. Var.TextValue = Value.str();
  6404. return false;
  6405. }
  6406. bool MasmParser::lookUpField(StringRef Name, AsmFieldInfo &Info) const {
  6407. const std::pair<StringRef, StringRef> BaseMember = Name.split('.');
  6408. const StringRef Base = BaseMember.first, Member = BaseMember.second;
  6409. return lookUpField(Base, Member, Info);
  6410. }
  6411. bool MasmParser::lookUpField(StringRef Base, StringRef Member,
  6412. AsmFieldInfo &Info) const {
  6413. if (Base.empty())
  6414. return true;
  6415. AsmFieldInfo BaseInfo;
  6416. if (Base.contains('.') && !lookUpField(Base, BaseInfo))
  6417. Base = BaseInfo.Type.Name;
  6418. auto StructIt = Structs.find(Base.lower());
  6419. auto TypeIt = KnownType.find(Base.lower());
  6420. if (TypeIt != KnownType.end()) {
  6421. StructIt = Structs.find(TypeIt->second.Name.lower());
  6422. }
  6423. if (StructIt != Structs.end())
  6424. return lookUpField(StructIt->second, Member, Info);
  6425. return true;
  6426. }
  6427. bool MasmParser::lookUpField(const StructInfo &Structure, StringRef Member,
  6428. AsmFieldInfo &Info) const {
  6429. if (Member.empty()) {
  6430. Info.Type.Name = Structure.Name;
  6431. Info.Type.Size = Structure.Size;
  6432. Info.Type.ElementSize = Structure.Size;
  6433. Info.Type.Length = 1;
  6434. return false;
  6435. }
  6436. std::pair<StringRef, StringRef> Split = Member.split('.');
  6437. const StringRef FieldName = Split.first, FieldMember = Split.second;
  6438. auto StructIt = Structs.find(FieldName.lower());
  6439. if (StructIt != Structs.end())
  6440. return lookUpField(StructIt->second, FieldMember, Info);
  6441. auto FieldIt = Structure.FieldsByName.find(FieldName.lower());
  6442. if (FieldIt == Structure.FieldsByName.end())
  6443. return true;
  6444. const FieldInfo &Field = Structure.Fields[FieldIt->second];
  6445. if (FieldMember.empty()) {
  6446. Info.Offset += Field.Offset;
  6447. Info.Type.Size = Field.SizeOf;
  6448. Info.Type.ElementSize = Field.Type;
  6449. Info.Type.Length = Field.LengthOf;
  6450. if (Field.Contents.FT == FT_STRUCT)
  6451. Info.Type.Name = Field.Contents.StructInfo.Structure.Name;
  6452. else
  6453. Info.Type.Name = "";
  6454. return false;
  6455. }
  6456. if (Field.Contents.FT != FT_STRUCT)
  6457. return true;
  6458. const StructFieldInfo &StructInfo = Field.Contents.StructInfo;
  6459. if (lookUpField(StructInfo.Structure, FieldMember, Info))
  6460. return true;
  6461. Info.Offset += Field.Offset;
  6462. return false;
  6463. }
  6464. bool MasmParser::lookUpType(StringRef Name, AsmTypeInfo &Info) const {
  6465. unsigned Size = StringSwitch<unsigned>(Name)
  6466. .CasesLower("byte", "db", "sbyte", 1)
  6467. .CasesLower("word", "dw", "sword", 2)
  6468. .CasesLower("dword", "dd", "sdword", 4)
  6469. .CasesLower("fword", "df", 6)
  6470. .CasesLower("qword", "dq", "sqword", 8)
  6471. .CaseLower("real4", 4)
  6472. .CaseLower("real8", 8)
  6473. .CaseLower("real10", 10)
  6474. .Default(0);
  6475. if (Size) {
  6476. Info.Name = Name;
  6477. Info.ElementSize = Size;
  6478. Info.Length = 1;
  6479. Info.Size = Size;
  6480. return false;
  6481. }
  6482. auto StructIt = Structs.find(Name.lower());
  6483. if (StructIt != Structs.end()) {
  6484. const StructInfo &Structure = StructIt->second;
  6485. Info.Name = Name;
  6486. Info.ElementSize = Structure.Size;
  6487. Info.Length = 1;
  6488. Info.Size = Structure.Size;
  6489. return false;
  6490. }
  6491. return true;
  6492. }
  6493. bool MasmParser::parseMSInlineAsm(
  6494. std::string &AsmString, unsigned &NumOutputs, unsigned &NumInputs,
  6495. SmallVectorImpl<std::pair<void *, bool>> &OpDecls,
  6496. SmallVectorImpl<std::string> &Constraints,
  6497. SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
  6498. const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
  6499. SmallVector<void *, 4> InputDecls;
  6500. SmallVector<void *, 4> OutputDecls;
  6501. SmallVector<bool, 4> InputDeclsAddressOf;
  6502. SmallVector<bool, 4> OutputDeclsAddressOf;
  6503. SmallVector<std::string, 4> InputConstraints;
  6504. SmallVector<std::string, 4> OutputConstraints;
  6505. SmallVector<unsigned, 4> ClobberRegs;
  6506. SmallVector<AsmRewrite, 4> AsmStrRewrites;
  6507. // Prime the lexer.
  6508. Lex();
  6509. // While we have input, parse each statement.
  6510. unsigned InputIdx = 0;
  6511. unsigned OutputIdx = 0;
  6512. while (getLexer().isNot(AsmToken::Eof)) {
  6513. // Parse curly braces marking block start/end.
  6514. if (parseCurlyBlockScope(AsmStrRewrites))
  6515. continue;
  6516. ParseStatementInfo Info(&AsmStrRewrites);
  6517. bool StatementErr = parseStatement(Info, &SI);
  6518. if (StatementErr || Info.ParseError) {
  6519. // Emit pending errors if any exist.
  6520. printPendingErrors();
  6521. return true;
  6522. }
  6523. // No pending error should exist here.
  6524. assert(!hasPendingError() && "unexpected error from parseStatement");
  6525. if (Info.Opcode == ~0U)
  6526. continue;
  6527. const MCInstrDesc &Desc = MII->get(Info.Opcode);
  6528. // Build the list of clobbers, outputs and inputs.
  6529. for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
  6530. MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
  6531. // Register operand.
  6532. if (Operand.isReg() && !Operand.needAddressOf() &&
  6533. !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
  6534. unsigned NumDefs = Desc.getNumDefs();
  6535. // Clobber.
  6536. if (NumDefs && Operand.getMCOperandNum() < NumDefs)
  6537. ClobberRegs.push_back(Operand.getReg());
  6538. continue;
  6539. }
  6540. // Expr/Input or Output.
  6541. StringRef SymName = Operand.getSymName();
  6542. if (SymName.empty())
  6543. continue;
  6544. void *OpDecl = Operand.getOpDecl();
  6545. if (!OpDecl)
  6546. continue;
  6547. StringRef Constraint = Operand.getConstraint();
  6548. if (Operand.isImm()) {
  6549. // Offset as immediate.
  6550. if (Operand.isOffsetOfLocal())
  6551. Constraint = "r";
  6552. else
  6553. Constraint = "i";
  6554. }
  6555. bool isOutput = (i == 1) && Desc.mayStore();
  6556. SMLoc Start = SMLoc::getFromPointer(SymName.data());
  6557. if (isOutput) {
  6558. ++InputIdx;
  6559. OutputDecls.push_back(OpDecl);
  6560. OutputDeclsAddressOf.push_back(Operand.needAddressOf());
  6561. OutputConstraints.push_back(("=" + Constraint).str());
  6562. AsmStrRewrites.emplace_back(AOK_Output, Start, SymName.size());
  6563. } else {
  6564. InputDecls.push_back(OpDecl);
  6565. InputDeclsAddressOf.push_back(Operand.needAddressOf());
  6566. InputConstraints.push_back(Constraint.str());
  6567. if (Desc.OpInfo[i - 1].isBranchTarget())
  6568. AsmStrRewrites.emplace_back(AOK_CallInput, Start, SymName.size());
  6569. else
  6570. AsmStrRewrites.emplace_back(AOK_Input, Start, SymName.size());
  6571. }
  6572. }
  6573. // Consider implicit defs to be clobbers. Think of cpuid and push.
  6574. ArrayRef<MCPhysReg> ImpDefs(Desc.getImplicitDefs(),
  6575. Desc.getNumImplicitDefs());
  6576. llvm::append_range(ClobberRegs, ImpDefs);
  6577. }
  6578. // Set the number of Outputs and Inputs.
  6579. NumOutputs = OutputDecls.size();
  6580. NumInputs = InputDecls.size();
  6581. // Set the unique clobbers.
  6582. array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
  6583. ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
  6584. ClobberRegs.end());
  6585. Clobbers.assign(ClobberRegs.size(), std::string());
  6586. for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
  6587. raw_string_ostream OS(Clobbers[I]);
  6588. IP->printRegName(OS, ClobberRegs[I]);
  6589. }
  6590. // Merge the various outputs and inputs. Output are expected first.
  6591. if (NumOutputs || NumInputs) {
  6592. unsigned NumExprs = NumOutputs + NumInputs;
  6593. OpDecls.resize(NumExprs);
  6594. Constraints.resize(NumExprs);
  6595. for (unsigned i = 0; i < NumOutputs; ++i) {
  6596. OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
  6597. Constraints[i] = OutputConstraints[i];
  6598. }
  6599. for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
  6600. OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
  6601. Constraints[j] = InputConstraints[i];
  6602. }
  6603. }
  6604. // Build the IR assembly string.
  6605. std::string AsmStringIR;
  6606. raw_string_ostream OS(AsmStringIR);
  6607. StringRef ASMString =
  6608. SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
  6609. const char *AsmStart = ASMString.begin();
  6610. const char *AsmEnd = ASMString.end();
  6611. array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
  6612. for (auto it = AsmStrRewrites.begin(); it != AsmStrRewrites.end(); ++it) {
  6613. const AsmRewrite &AR = *it;
  6614. // Check if this has already been covered by another rewrite...
  6615. if (AR.Done)
  6616. continue;
  6617. AsmRewriteKind Kind = AR.Kind;
  6618. const char *Loc = AR.Loc.getPointer();
  6619. assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
  6620. // Emit everything up to the immediate/expression.
  6621. if (unsigned Len = Loc - AsmStart)
  6622. OS << StringRef(AsmStart, Len);
  6623. // Skip the original expression.
  6624. if (Kind == AOK_Skip) {
  6625. AsmStart = Loc + AR.Len;
  6626. continue;
  6627. }
  6628. unsigned AdditionalSkip = 0;
  6629. // Rewrite expressions in $N notation.
  6630. switch (Kind) {
  6631. default:
  6632. break;
  6633. case AOK_IntelExpr:
  6634. assert(AR.IntelExp.isValid() && "cannot write invalid intel expression");
  6635. if (AR.IntelExp.NeedBracs)
  6636. OS << "[";
  6637. if (AR.IntelExp.hasBaseReg())
  6638. OS << AR.IntelExp.BaseReg;
  6639. if (AR.IntelExp.hasIndexReg())
  6640. OS << (AR.IntelExp.hasBaseReg() ? " + " : "")
  6641. << AR.IntelExp.IndexReg;
  6642. if (AR.IntelExp.Scale > 1)
  6643. OS << " * $$" << AR.IntelExp.Scale;
  6644. if (AR.IntelExp.hasOffset()) {
  6645. if (AR.IntelExp.hasRegs())
  6646. OS << " + ";
  6647. // Fuse this rewrite with a rewrite of the offset name, if present.
  6648. StringRef OffsetName = AR.IntelExp.OffsetName;
  6649. SMLoc OffsetLoc = SMLoc::getFromPointer(AR.IntelExp.OffsetName.data());
  6650. size_t OffsetLen = OffsetName.size();
  6651. auto rewrite_it = std::find_if(
  6652. it, AsmStrRewrites.end(), [&](const AsmRewrite &FusingAR) {
  6653. return FusingAR.Loc == OffsetLoc && FusingAR.Len == OffsetLen &&
  6654. (FusingAR.Kind == AOK_Input ||
  6655. FusingAR.Kind == AOK_CallInput);
  6656. });
  6657. if (rewrite_it == AsmStrRewrites.end()) {
  6658. OS << "offset " << OffsetName;
  6659. } else if (rewrite_it->Kind == AOK_CallInput) {
  6660. OS << "${" << InputIdx++ << ":P}";
  6661. rewrite_it->Done = true;
  6662. } else {
  6663. OS << '$' << InputIdx++;
  6664. rewrite_it->Done = true;
  6665. }
  6666. }
  6667. if (AR.IntelExp.Imm || AR.IntelExp.emitImm())
  6668. OS << (AR.IntelExp.emitImm() ? "$$" : " + $$") << AR.IntelExp.Imm;
  6669. if (AR.IntelExp.NeedBracs)
  6670. OS << "]";
  6671. break;
  6672. case AOK_Label:
  6673. OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
  6674. break;
  6675. case AOK_Input:
  6676. OS << '$' << InputIdx++;
  6677. break;
  6678. case AOK_CallInput:
  6679. OS << "${" << InputIdx++ << ":P}";
  6680. break;
  6681. case AOK_Output:
  6682. OS << '$' << OutputIdx++;
  6683. break;
  6684. case AOK_SizeDirective:
  6685. switch (AR.Val) {
  6686. default: break;
  6687. case 8: OS << "byte ptr "; break;
  6688. case 16: OS << "word ptr "; break;
  6689. case 32: OS << "dword ptr "; break;
  6690. case 64: OS << "qword ptr "; break;
  6691. case 80: OS << "xword ptr "; break;
  6692. case 128: OS << "xmmword ptr "; break;
  6693. case 256: OS << "ymmword ptr "; break;
  6694. }
  6695. break;
  6696. case AOK_Emit:
  6697. OS << ".byte";
  6698. break;
  6699. case AOK_Align: {
  6700. // MS alignment directives are measured in bytes. If the native assembler
  6701. // measures alignment in bytes, we can pass it straight through.
  6702. OS << ".align";
  6703. if (getContext().getAsmInfo()->getAlignmentIsInBytes())
  6704. break;
  6705. // Alignment is in log2 form, so print that instead and skip the original
  6706. // immediate.
  6707. unsigned Val = AR.Val;
  6708. OS << ' ' << Val;
  6709. assert(Val < 10 && "Expected alignment less then 2^10.");
  6710. AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
  6711. break;
  6712. }
  6713. case AOK_EVEN:
  6714. OS << ".even";
  6715. break;
  6716. case AOK_EndOfStatement:
  6717. OS << "\n\t";
  6718. break;
  6719. }
  6720. // Skip the original expression.
  6721. AsmStart = Loc + AR.Len + AdditionalSkip;
  6722. }
  6723. // Emit the remainder of the asm string.
  6724. if (AsmStart != AsmEnd)
  6725. OS << StringRef(AsmStart, AsmEnd - AsmStart);
  6726. AsmString = OS.str();
  6727. return false;
  6728. }
  6729. void MasmParser::initializeBuiltinSymbolMap() {
  6730. // Numeric built-ins (supported in all versions)
  6731. BuiltinSymbolMap["@version"] = BI_VERSION;
  6732. BuiltinSymbolMap["@line"] = BI_LINE;
  6733. // Text built-ins (supported in all versions)
  6734. BuiltinSymbolMap["@date"] = BI_DATE;
  6735. BuiltinSymbolMap["@time"] = BI_TIME;
  6736. BuiltinSymbolMap["@filecur"] = BI_FILECUR;
  6737. BuiltinSymbolMap["@filename"] = BI_FILENAME;
  6738. BuiltinSymbolMap["@curseg"] = BI_CURSEG;
  6739. // Some built-ins exist only for MASM32 (32-bit x86)
  6740. if (getContext().getSubtargetInfo()->getTargetTriple().getArch() ==
  6741. Triple::x86) {
  6742. // Numeric built-ins
  6743. // BuiltinSymbolMap["@cpu"] = BI_CPU;
  6744. // BuiltinSymbolMap["@interface"] = BI_INTERFACE;
  6745. // BuiltinSymbolMap["@wordsize"] = BI_WORDSIZE;
  6746. // BuiltinSymbolMap["@codesize"] = BI_CODESIZE;
  6747. // BuiltinSymbolMap["@datasize"] = BI_DATASIZE;
  6748. // BuiltinSymbolMap["@model"] = BI_MODEL;
  6749. // Text built-ins
  6750. // BuiltinSymbolMap["@code"] = BI_CODE;
  6751. // BuiltinSymbolMap["@data"] = BI_DATA;
  6752. // BuiltinSymbolMap["@fardata?"] = BI_FARDATA;
  6753. // BuiltinSymbolMap["@stack"] = BI_STACK;
  6754. }
  6755. }
  6756. const MCExpr *MasmParser::evaluateBuiltinValue(BuiltinSymbol Symbol,
  6757. SMLoc StartLoc) {
  6758. switch (Symbol) {
  6759. default:
  6760. return nullptr;
  6761. case BI_VERSION:
  6762. // Match a recent version of ML.EXE.
  6763. return MCConstantExpr::create(1427, getContext());
  6764. case BI_LINE: {
  6765. int64_t Line;
  6766. if (ActiveMacros.empty())
  6767. Line = SrcMgr.FindLineNumber(StartLoc, CurBuffer);
  6768. else
  6769. Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc,
  6770. ActiveMacros.front()->ExitBuffer);
  6771. return MCConstantExpr::create(Line, getContext());
  6772. }
  6773. }
  6774. llvm_unreachable("unhandled built-in symbol");
  6775. }
  6776. llvm::Optional<std::string>
  6777. MasmParser::evaluateBuiltinTextMacro(BuiltinSymbol Symbol, SMLoc StartLoc) {
  6778. switch (Symbol) {
  6779. default:
  6780. return {};
  6781. case BI_DATE: {
  6782. // Current local date, formatted MM/DD/YY
  6783. char TmpBuffer[sizeof("mm/dd/yy")];
  6784. const size_t Len = strftime(TmpBuffer, sizeof(TmpBuffer), "%D", &TM);
  6785. return std::string(TmpBuffer, Len);
  6786. }
  6787. case BI_TIME: {
  6788. // Current local time, formatted HH:MM:SS (24-hour clock)
  6789. char TmpBuffer[sizeof("hh:mm:ss")];
  6790. const size_t Len = strftime(TmpBuffer, sizeof(TmpBuffer), "%T", &TM);
  6791. return std::string(TmpBuffer, Len);
  6792. }
  6793. case BI_FILECUR:
  6794. return SrcMgr
  6795. .getMemoryBuffer(
  6796. ActiveMacros.empty() ? CurBuffer : ActiveMacros.front()->ExitBuffer)
  6797. ->getBufferIdentifier()
  6798. .str();
  6799. case BI_FILENAME:
  6800. return sys::path::stem(SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())
  6801. ->getBufferIdentifier())
  6802. .upper();
  6803. case BI_CURSEG:
  6804. return getStreamer().getCurrentSectionOnly()->getName().str();
  6805. }
  6806. llvm_unreachable("unhandled built-in symbol");
  6807. }
  6808. /// Create an MCAsmParser instance.
  6809. MCAsmParser *llvm::createMCMasmParser(SourceMgr &SM, MCContext &C,
  6810. MCStreamer &Out, const MCAsmInfo &MAI,
  6811. struct tm TM, unsigned CB) {
  6812. return new MasmParser(SM, C, Out, MAI, TM, CB);
  6813. }