AsmParser.cpp 202 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232
  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 a parser for assembly files similar to gas syntax.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/ADT/APFloat.h"
  13. #include "llvm/ADT/APInt.h"
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/ADT/None.h"
  16. #include "llvm/ADT/STLExtras.h"
  17. #include "llvm/ADT/SmallString.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/ADT/StringExtras.h"
  20. #include "llvm/ADT/StringMap.h"
  21. #include "llvm/ADT/StringRef.h"
  22. #include "llvm/ADT/Twine.h"
  23. #include "llvm/BinaryFormat/Dwarf.h"
  24. #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
  25. #include "llvm/MC/MCAsmInfo.h"
  26. #include "llvm/MC/MCCodeView.h"
  27. #include "llvm/MC/MCContext.h"
  28. #include "llvm/MC/MCDirectives.h"
  29. #include "llvm/MC/MCDwarf.h"
  30. #include "llvm/MC/MCExpr.h"
  31. #include "llvm/MC/MCInstPrinter.h"
  32. #include "llvm/MC/MCInstrDesc.h"
  33. #include "llvm/MC/MCInstrInfo.h"
  34. #include "llvm/MC/MCObjectFileInfo.h"
  35. #include "llvm/MC/MCParser/AsmCond.h"
  36. #include "llvm/MC/MCParser/AsmLexer.h"
  37. #include "llvm/MC/MCParser/MCAsmLexer.h"
  38. #include "llvm/MC/MCParser/MCAsmParser.h"
  39. #include "llvm/MC/MCParser/MCAsmParserExtension.h"
  40. #include "llvm/MC/MCParser/MCAsmParserUtils.h"
  41. #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
  42. #include "llvm/MC/MCParser/MCTargetAsmParser.h"
  43. #include "llvm/MC/MCRegisterInfo.h"
  44. #include "llvm/MC/MCSection.h"
  45. #include "llvm/MC/MCStreamer.h"
  46. #include "llvm/MC/MCSymbol.h"
  47. #include "llvm/MC/MCTargetOptions.h"
  48. #include "llvm/MC/MCValue.h"
  49. #include "llvm/Support/Casting.h"
  50. #include "llvm/Support/CommandLine.h"
  51. #include "llvm/Support/ErrorHandling.h"
  52. #include "llvm/Support/MD5.h"
  53. #include "llvm/Support/MathExtras.h"
  54. #include "llvm/Support/MemoryBuffer.h"
  55. #include "llvm/Support/SMLoc.h"
  56. #include "llvm/Support/SourceMgr.h"
  57. #include "llvm/Support/raw_ostream.h"
  58. #include <algorithm>
  59. #include <cassert>
  60. #include <cctype>
  61. #include <climits>
  62. #include <cstddef>
  63. #include <cstdint>
  64. #include <deque>
  65. #include <memory>
  66. #include <sstream>
  67. #include <string>
  68. #include <tuple>
  69. #include <utility>
  70. #include <vector>
  71. using namespace llvm;
  72. MCAsmParserSemaCallback::~MCAsmParserSemaCallback() = default;
  73. extern cl::opt<unsigned> AsmMacroMaxNestingDepth;
  74. namespace {
  75. /// Helper types for tracking macro definitions.
  76. typedef std::vector<AsmToken> MCAsmMacroArgument;
  77. typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
  78. /// Helper class for storing information about an active macro
  79. /// instantiation.
  80. struct MacroInstantiation {
  81. /// The location of the instantiation.
  82. SMLoc InstantiationLoc;
  83. /// The buffer where parsing should resume upon instantiation completion.
  84. unsigned ExitBuffer;
  85. /// The location where parsing should resume upon instantiation completion.
  86. SMLoc ExitLoc;
  87. /// The depth of TheCondStack at the start of the instantiation.
  88. size_t CondStackDepth;
  89. };
  90. struct ParseStatementInfo {
  91. /// The parsed operands from the last parsed statement.
  92. SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands;
  93. /// The opcode from the last parsed instruction.
  94. unsigned Opcode = ~0U;
  95. /// Was there an error parsing the inline assembly?
  96. bool ParseError = false;
  97. SmallVectorImpl<AsmRewrite> *AsmRewrites = nullptr;
  98. ParseStatementInfo() = delete;
  99. ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
  100. : AsmRewrites(rewrites) {}
  101. };
  102. /// The concrete assembly parser instance.
  103. class AsmParser : public MCAsmParser {
  104. private:
  105. AsmLexer Lexer;
  106. MCContext &Ctx;
  107. MCStreamer &Out;
  108. const MCAsmInfo &MAI;
  109. SourceMgr &SrcMgr;
  110. SourceMgr::DiagHandlerTy SavedDiagHandler;
  111. void *SavedDiagContext;
  112. std::unique_ptr<MCAsmParserExtension> PlatformParser;
  113. SMLoc StartTokLoc;
  114. /// This is the current buffer index we're lexing from as managed by the
  115. /// SourceMgr object.
  116. unsigned CurBuffer;
  117. AsmCond TheCondState;
  118. std::vector<AsmCond> TheCondStack;
  119. /// maps directive names to handler methods in parser
  120. /// extensions. Extensions register themselves in this map by calling
  121. /// addDirectiveHandler.
  122. StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
  123. /// Stack of active macro instantiations.
  124. std::vector<MacroInstantiation*> ActiveMacros;
  125. /// List of bodies of anonymous macros.
  126. std::deque<MCAsmMacro> MacroLikeBodies;
  127. /// Boolean tracking whether macro substitution is enabled.
  128. unsigned MacrosEnabledFlag : 1;
  129. /// Keeps track of how many .macro's have been instantiated.
  130. unsigned NumOfMacroInstantiations;
  131. /// The values from the last parsed cpp hash file line comment if any.
  132. struct CppHashInfoTy {
  133. StringRef Filename;
  134. int64_t LineNumber;
  135. SMLoc Loc;
  136. unsigned Buf;
  137. CppHashInfoTy() : Filename(), LineNumber(0), Loc(), Buf(0) {}
  138. };
  139. CppHashInfoTy CppHashInfo;
  140. /// The filename from the first cpp hash file line comment, if any.
  141. StringRef FirstCppHashFilename;
  142. /// List of forward directional labels for diagnosis at the end.
  143. SmallVector<std::tuple<SMLoc, CppHashInfoTy, MCSymbol *>, 4> DirLabels;
  144. /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
  145. unsigned AssemblerDialect = ~0U;
  146. /// is Darwin compatibility enabled?
  147. bool IsDarwin = false;
  148. /// Are we parsing ms-style inline assembly?
  149. bool ParsingMSInlineAsm = false;
  150. /// Did we already inform the user about inconsistent MD5 usage?
  151. bool ReportedInconsistentMD5 = false;
  152. // Is alt macro mode enabled.
  153. bool AltMacroMode = false;
  154. public:
  155. AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
  156. const MCAsmInfo &MAI, unsigned CB);
  157. AsmParser(const AsmParser &) = delete;
  158. AsmParser &operator=(const AsmParser &) = delete;
  159. ~AsmParser() override;
  160. bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
  161. void addDirectiveHandler(StringRef Directive,
  162. ExtensionDirectiveHandler Handler) override {
  163. ExtensionDirectiveMap[Directive] = Handler;
  164. }
  165. void addAliasForDirective(StringRef Directive, StringRef Alias) override {
  166. DirectiveKindMap[Directive.lower()] = DirectiveKindMap[Alias.lower()];
  167. }
  168. /// @name MCAsmParser Interface
  169. /// {
  170. SourceMgr &getSourceManager() override { return SrcMgr; }
  171. MCAsmLexer &getLexer() override { return Lexer; }
  172. MCContext &getContext() override { return Ctx; }
  173. MCStreamer &getStreamer() override { return Out; }
  174. CodeViewContext &getCVContext() { return Ctx.getCVContext(); }
  175. unsigned getAssemblerDialect() override {
  176. if (AssemblerDialect == ~0U)
  177. return MAI.getAssemblerDialect();
  178. else
  179. return AssemblerDialect;
  180. }
  181. void setAssemblerDialect(unsigned i) override {
  182. AssemblerDialect = i;
  183. }
  184. void Note(SMLoc L, const Twine &Msg, SMRange Range = None) override;
  185. bool Warning(SMLoc L, const Twine &Msg, SMRange Range = None) override;
  186. bool printError(SMLoc L, const Twine &Msg, SMRange Range = None) override;
  187. const AsmToken &Lex() override;
  188. void setParsingMSInlineAsm(bool V) override {
  189. ParsingMSInlineAsm = V;
  190. // When parsing MS inline asm, we must lex 0b1101 and 0ABCH as binary and
  191. // hex integer literals.
  192. Lexer.setLexMasmIntegers(V);
  193. }
  194. bool isParsingMSInlineAsm() override { return ParsingMSInlineAsm; }
  195. bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
  196. unsigned &NumOutputs, unsigned &NumInputs,
  197. SmallVectorImpl<std::pair<void *,bool>> &OpDecls,
  198. SmallVectorImpl<std::string> &Constraints,
  199. SmallVectorImpl<std::string> &Clobbers,
  200. const MCInstrInfo *MII, const MCInstPrinter *IP,
  201. MCAsmParserSemaCallback &SI) override;
  202. bool parseExpression(const MCExpr *&Res);
  203. bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
  204. bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
  205. AsmTypeInfo *TypeInfo) override;
  206. bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
  207. bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
  208. SMLoc &EndLoc) override;
  209. bool parseAbsoluteExpression(int64_t &Res) override;
  210. /// Parse a floating point expression using the float \p Semantics
  211. /// and set \p Res to the value.
  212. bool parseRealValue(const fltSemantics &Semantics, APInt &Res);
  213. /// Parse an identifier or string (as a quoted identifier)
  214. /// and set \p Res to the identifier contents.
  215. bool parseIdentifier(StringRef &Res) override;
  216. void eatToEndOfStatement() override;
  217. bool checkForValidSection() override;
  218. /// }
  219. private:
  220. bool parseStatement(ParseStatementInfo &Info,
  221. MCAsmParserSemaCallback *SI);
  222. bool parseCurlyBlockScope(SmallVectorImpl<AsmRewrite>& AsmStrRewrites);
  223. bool parseCppHashLineFilenameComment(SMLoc L, bool SaveLocInfo = true);
  224. void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
  225. ArrayRef<MCAsmMacroParameter> Parameters);
  226. bool expandMacro(raw_svector_ostream &OS, StringRef Body,
  227. ArrayRef<MCAsmMacroParameter> Parameters,
  228. ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable,
  229. SMLoc L);
  230. /// Are macros enabled in the parser?
  231. bool areMacrosEnabled() {return MacrosEnabledFlag;}
  232. /// Control a flag in the parser that enables or disables macros.
  233. void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
  234. /// Are we inside a macro instantiation?
  235. bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
  236. /// Handle entry to macro instantiation.
  237. ///
  238. /// \param M The macro.
  239. /// \param NameLoc Instantiation location.
  240. bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
  241. /// Handle exit from macro instantiation.
  242. void handleMacroExit();
  243. /// Extract AsmTokens for a macro argument.
  244. bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
  245. /// Parse all macro arguments for a given macro.
  246. bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
  247. void printMacroInstantiations();
  248. void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
  249. SMRange Range = None) const {
  250. ArrayRef<SMRange> Ranges(Range);
  251. SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
  252. }
  253. static void DiagHandler(const SMDiagnostic &Diag, void *Context);
  254. /// Should we emit DWARF describing this assembler source? (Returns false if
  255. /// the source has .file directives, which means we don't want to generate
  256. /// info describing the assembler source itself.)
  257. bool enabledGenDwarfForAssembly();
  258. /// Enter the specified file. This returns true on failure.
  259. bool enterIncludeFile(const std::string &Filename);
  260. /// Process the specified file for the .incbin directive.
  261. /// This returns true on failure.
  262. bool processIncbinFile(const std::string &Filename, int64_t Skip = 0,
  263. const MCExpr *Count = nullptr, SMLoc Loc = SMLoc());
  264. /// Reset the current lexer position to that given by \p Loc. The
  265. /// current token is not set; clients should ensure Lex() is called
  266. /// subsequently.
  267. ///
  268. /// \param InBuffer If not 0, should be the known buffer id that contains the
  269. /// location.
  270. void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
  271. /// Parse up to the end of statement and a return the contents from the
  272. /// current token until the end of the statement; the current token on exit
  273. /// will be either the EndOfStatement or EOF.
  274. StringRef parseStringToEndOfStatement() override;
  275. /// Parse until the end of a statement or a comma is encountered,
  276. /// return the contents from the current token up to the end or comma.
  277. StringRef parseStringToComma();
  278. bool parseAssignment(StringRef Name, bool allow_redef,
  279. bool NoDeadStrip = false);
  280. unsigned getBinOpPrecedence(AsmToken::TokenKind K,
  281. MCBinaryExpr::Opcode &Kind);
  282. bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
  283. bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
  284. bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
  285. bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
  286. bool parseCVFunctionId(int64_t &FunctionId, StringRef DirectiveName);
  287. bool parseCVFileId(int64_t &FileId, StringRef DirectiveName);
  288. // Generic (target and platform independent) directive parsing.
  289. enum DirectiveKind {
  290. DK_NO_DIRECTIVE, // Placeholder
  291. DK_SET,
  292. DK_EQU,
  293. DK_EQUIV,
  294. DK_ASCII,
  295. DK_ASCIZ,
  296. DK_STRING,
  297. DK_BYTE,
  298. DK_SHORT,
  299. DK_RELOC,
  300. DK_VALUE,
  301. DK_2BYTE,
  302. DK_LONG,
  303. DK_INT,
  304. DK_4BYTE,
  305. DK_QUAD,
  306. DK_8BYTE,
  307. DK_OCTA,
  308. DK_DC,
  309. DK_DC_A,
  310. DK_DC_B,
  311. DK_DC_D,
  312. DK_DC_L,
  313. DK_DC_S,
  314. DK_DC_W,
  315. DK_DC_X,
  316. DK_DCB,
  317. DK_DCB_B,
  318. DK_DCB_D,
  319. DK_DCB_L,
  320. DK_DCB_S,
  321. DK_DCB_W,
  322. DK_DCB_X,
  323. DK_DS,
  324. DK_DS_B,
  325. DK_DS_D,
  326. DK_DS_L,
  327. DK_DS_P,
  328. DK_DS_S,
  329. DK_DS_W,
  330. DK_DS_X,
  331. DK_SINGLE,
  332. DK_FLOAT,
  333. DK_DOUBLE,
  334. DK_ALIGN,
  335. DK_ALIGN32,
  336. DK_BALIGN,
  337. DK_BALIGNW,
  338. DK_BALIGNL,
  339. DK_P2ALIGN,
  340. DK_P2ALIGNW,
  341. DK_P2ALIGNL,
  342. DK_ORG,
  343. DK_FILL,
  344. DK_ENDR,
  345. DK_BUNDLE_ALIGN_MODE,
  346. DK_BUNDLE_LOCK,
  347. DK_BUNDLE_UNLOCK,
  348. DK_ZERO,
  349. DK_EXTERN,
  350. DK_GLOBL,
  351. DK_GLOBAL,
  352. DK_LAZY_REFERENCE,
  353. DK_NO_DEAD_STRIP,
  354. DK_SYMBOL_RESOLVER,
  355. DK_PRIVATE_EXTERN,
  356. DK_REFERENCE,
  357. DK_WEAK_DEFINITION,
  358. DK_WEAK_REFERENCE,
  359. DK_WEAK_DEF_CAN_BE_HIDDEN,
  360. DK_COLD,
  361. DK_COMM,
  362. DK_COMMON,
  363. DK_LCOMM,
  364. DK_ABORT,
  365. DK_INCLUDE,
  366. DK_INCBIN,
  367. DK_CODE16,
  368. DK_CODE16GCC,
  369. DK_REPT,
  370. DK_IRP,
  371. DK_IRPC,
  372. DK_IF,
  373. DK_IFEQ,
  374. DK_IFGE,
  375. DK_IFGT,
  376. DK_IFLE,
  377. DK_IFLT,
  378. DK_IFNE,
  379. DK_IFB,
  380. DK_IFNB,
  381. DK_IFC,
  382. DK_IFEQS,
  383. DK_IFNC,
  384. DK_IFNES,
  385. DK_IFDEF,
  386. DK_IFNDEF,
  387. DK_IFNOTDEF,
  388. DK_ELSEIF,
  389. DK_ELSE,
  390. DK_ENDIF,
  391. DK_SPACE,
  392. DK_SKIP,
  393. DK_FILE,
  394. DK_LINE,
  395. DK_LOC,
  396. DK_STABS,
  397. DK_CV_FILE,
  398. DK_CV_FUNC_ID,
  399. DK_CV_INLINE_SITE_ID,
  400. DK_CV_LOC,
  401. DK_CV_LINETABLE,
  402. DK_CV_INLINE_LINETABLE,
  403. DK_CV_DEF_RANGE,
  404. DK_CV_STRINGTABLE,
  405. DK_CV_STRING,
  406. DK_CV_FILECHECKSUMS,
  407. DK_CV_FILECHECKSUM_OFFSET,
  408. DK_CV_FPO_DATA,
  409. DK_CFI_SECTIONS,
  410. DK_CFI_STARTPROC,
  411. DK_CFI_ENDPROC,
  412. DK_CFI_DEF_CFA,
  413. DK_CFI_DEF_CFA_OFFSET,
  414. DK_CFI_ADJUST_CFA_OFFSET,
  415. DK_CFI_DEF_CFA_REGISTER,
  416. DK_CFI_OFFSET,
  417. DK_CFI_REL_OFFSET,
  418. DK_CFI_PERSONALITY,
  419. DK_CFI_LSDA,
  420. DK_CFI_REMEMBER_STATE,
  421. DK_CFI_RESTORE_STATE,
  422. DK_CFI_SAME_VALUE,
  423. DK_CFI_RESTORE,
  424. DK_CFI_ESCAPE,
  425. DK_CFI_RETURN_COLUMN,
  426. DK_CFI_SIGNAL_FRAME,
  427. DK_CFI_UNDEFINED,
  428. DK_CFI_REGISTER,
  429. DK_CFI_WINDOW_SAVE,
  430. DK_CFI_B_KEY_FRAME,
  431. DK_MACROS_ON,
  432. DK_MACROS_OFF,
  433. DK_ALTMACRO,
  434. DK_NOALTMACRO,
  435. DK_MACRO,
  436. DK_EXITM,
  437. DK_ENDM,
  438. DK_ENDMACRO,
  439. DK_PURGEM,
  440. DK_SLEB128,
  441. DK_ULEB128,
  442. DK_ERR,
  443. DK_ERROR,
  444. DK_WARNING,
  445. DK_PRINT,
  446. DK_ADDRSIG,
  447. DK_ADDRSIG_SYM,
  448. DK_PSEUDO_PROBE,
  449. DK_END
  450. };
  451. /// Maps directive name --> DirectiveKind enum, for
  452. /// directives parsed by this class.
  453. StringMap<DirectiveKind> DirectiveKindMap;
  454. // Codeview def_range type parsing.
  455. enum CVDefRangeType {
  456. CVDR_DEFRANGE = 0, // Placeholder
  457. CVDR_DEFRANGE_REGISTER,
  458. CVDR_DEFRANGE_FRAMEPOINTER_REL,
  459. CVDR_DEFRANGE_SUBFIELD_REGISTER,
  460. CVDR_DEFRANGE_REGISTER_REL
  461. };
  462. /// Maps Codeview def_range types --> CVDefRangeType enum, for
  463. /// Codeview def_range types parsed by this class.
  464. StringMap<CVDefRangeType> CVDefRangeTypeMap;
  465. // ".ascii", ".asciz", ".string"
  466. bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
  467. bool parseDirectiveReloc(SMLoc DirectiveLoc); // ".reloc"
  468. bool parseDirectiveValue(StringRef IDVal,
  469. unsigned Size); // ".byte", ".long", ...
  470. bool parseDirectiveOctaValue(StringRef IDVal); // ".octa", ...
  471. bool parseDirectiveRealValue(StringRef IDVal,
  472. const fltSemantics &); // ".single", ...
  473. bool parseDirectiveFill(); // ".fill"
  474. bool parseDirectiveZero(); // ".zero"
  475. // ".set", ".equ", ".equiv"
  476. bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
  477. bool parseDirectiveOrg(); // ".org"
  478. // ".align{,32}", ".p2align{,w,l}"
  479. bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
  480. // ".file", ".line", ".loc", ".stabs"
  481. bool parseDirectiveFile(SMLoc DirectiveLoc);
  482. bool parseDirectiveLine();
  483. bool parseDirectiveLoc();
  484. bool parseDirectiveStabs();
  485. // ".cv_file", ".cv_func_id", ".cv_inline_site_id", ".cv_loc", ".cv_linetable",
  486. // ".cv_inline_linetable", ".cv_def_range", ".cv_string"
  487. bool parseDirectiveCVFile();
  488. bool parseDirectiveCVFuncId();
  489. bool parseDirectiveCVInlineSiteId();
  490. bool parseDirectiveCVLoc();
  491. bool parseDirectiveCVLinetable();
  492. bool parseDirectiveCVInlineLinetable();
  493. bool parseDirectiveCVDefRange();
  494. bool parseDirectiveCVString();
  495. bool parseDirectiveCVStringTable();
  496. bool parseDirectiveCVFileChecksums();
  497. bool parseDirectiveCVFileChecksumOffset();
  498. bool parseDirectiveCVFPOData();
  499. // .cfi directives
  500. bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
  501. bool parseDirectiveCFIWindowSave();
  502. bool parseDirectiveCFISections();
  503. bool parseDirectiveCFIStartProc();
  504. bool parseDirectiveCFIEndProc();
  505. bool parseDirectiveCFIDefCfaOffset();
  506. bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
  507. bool parseDirectiveCFIAdjustCfaOffset();
  508. bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
  509. bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
  510. bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
  511. bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
  512. bool parseDirectiveCFIRememberState();
  513. bool parseDirectiveCFIRestoreState();
  514. bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
  515. bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
  516. bool parseDirectiveCFIEscape();
  517. bool parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc);
  518. bool parseDirectiveCFISignalFrame();
  519. bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
  520. // macro directives
  521. bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
  522. bool parseDirectiveExitMacro(StringRef Directive);
  523. bool parseDirectiveEndMacro(StringRef Directive);
  524. bool parseDirectiveMacro(SMLoc DirectiveLoc);
  525. bool parseDirectiveMacrosOnOff(StringRef Directive);
  526. // alternate macro mode directives
  527. bool parseDirectiveAltmacro(StringRef Directive);
  528. // ".bundle_align_mode"
  529. bool parseDirectiveBundleAlignMode();
  530. // ".bundle_lock"
  531. bool parseDirectiveBundleLock();
  532. // ".bundle_unlock"
  533. bool parseDirectiveBundleUnlock();
  534. // ".space", ".skip"
  535. bool parseDirectiveSpace(StringRef IDVal);
  536. // ".dcb"
  537. bool parseDirectiveDCB(StringRef IDVal, unsigned Size);
  538. bool parseDirectiveRealDCB(StringRef IDVal, const fltSemantics &);
  539. // ".ds"
  540. bool parseDirectiveDS(StringRef IDVal, unsigned Size);
  541. // .sleb128 (Signed=true) and .uleb128 (Signed=false)
  542. bool parseDirectiveLEB128(bool Signed);
  543. /// Parse a directive like ".globl" which
  544. /// accepts a single symbol (which should be a label or an external).
  545. bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
  546. bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
  547. bool parseDirectiveAbort(); // ".abort"
  548. bool parseDirectiveInclude(); // ".include"
  549. bool parseDirectiveIncbin(); // ".incbin"
  550. // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
  551. bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
  552. // ".ifb" or ".ifnb", depending on ExpectBlank.
  553. bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
  554. // ".ifc" or ".ifnc", depending on ExpectEqual.
  555. bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
  556. // ".ifeqs" or ".ifnes", depending on ExpectEqual.
  557. bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual);
  558. // ".ifdef" or ".ifndef", depending on expect_defined
  559. bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
  560. bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
  561. bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
  562. bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
  563. bool parseEscapedString(std::string &Data) override;
  564. bool parseAngleBracketString(std::string &Data) override;
  565. const MCExpr *applyModifierToExpr(const MCExpr *E,
  566. MCSymbolRefExpr::VariantKind Variant);
  567. // Macro-like directives
  568. MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
  569. void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
  570. raw_svector_ostream &OS);
  571. bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
  572. bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
  573. bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
  574. bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
  575. // "_emit" or "__emit"
  576. bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
  577. size_t Len);
  578. // "align"
  579. bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
  580. // "end"
  581. bool parseDirectiveEnd(SMLoc DirectiveLoc);
  582. // ".err" or ".error"
  583. bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
  584. // ".warning"
  585. bool parseDirectiveWarning(SMLoc DirectiveLoc);
  586. // .print <double-quotes-string>
  587. bool parseDirectivePrint(SMLoc DirectiveLoc);
  588. // .pseudoprobe
  589. bool parseDirectivePseudoProbe();
  590. // Directives to support address-significance tables.
  591. bool parseDirectiveAddrsig();
  592. bool parseDirectiveAddrsigSym();
  593. void initializeDirectiveKindMap();
  594. void initializeCVDefRangeTypeMap();
  595. };
  596. } // end anonymous namespace
  597. namespace llvm {
  598. extern MCAsmParserExtension *createDarwinAsmParser();
  599. extern MCAsmParserExtension *createELFAsmParser();
  600. extern MCAsmParserExtension *createCOFFAsmParser();
  601. extern MCAsmParserExtension *createWasmAsmParser();
  602. } // end namespace llvm
  603. enum { DEFAULT_ADDRSPACE = 0 };
  604. AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
  605. const MCAsmInfo &MAI, unsigned CB = 0)
  606. : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM),
  607. CurBuffer(CB ? CB : SM.getMainFileID()), MacrosEnabledFlag(true) {
  608. HadError = false;
  609. // Save the old handler.
  610. SavedDiagHandler = SrcMgr.getDiagHandler();
  611. SavedDiagContext = SrcMgr.getDiagContext();
  612. // Set our own handler which calls the saved handler.
  613. SrcMgr.setDiagHandler(DiagHandler, this);
  614. Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
  615. // Make MCStreamer aware of the StartTokLoc for locations in diagnostics.
  616. Out.setStartTokLocPtr(&StartTokLoc);
  617. // Initialize the platform / file format parser.
  618. switch (Ctx.getObjectFileInfo()->getObjectFileType()) {
  619. case MCObjectFileInfo::IsCOFF:
  620. PlatformParser.reset(createCOFFAsmParser());
  621. break;
  622. case MCObjectFileInfo::IsMachO:
  623. PlatformParser.reset(createDarwinAsmParser());
  624. IsDarwin = true;
  625. break;
  626. case MCObjectFileInfo::IsELF:
  627. PlatformParser.reset(createELFAsmParser());
  628. break;
  629. case MCObjectFileInfo::IsWasm:
  630. PlatformParser.reset(createWasmAsmParser());
  631. break;
  632. case MCObjectFileInfo::IsXCOFF:
  633. report_fatal_error(
  634. "Need to implement createXCOFFAsmParser for XCOFF format.");
  635. break;
  636. }
  637. PlatformParser->Initialize(*this);
  638. initializeDirectiveKindMap();
  639. initializeCVDefRangeTypeMap();
  640. NumOfMacroInstantiations = 0;
  641. }
  642. AsmParser::~AsmParser() {
  643. assert((HadError || ActiveMacros.empty()) &&
  644. "Unexpected active macro instantiation!");
  645. // Remove MCStreamer's reference to the parser SMLoc.
  646. Out.setStartTokLocPtr(nullptr);
  647. // Restore the saved diagnostics handler and context for use during
  648. // finalization.
  649. SrcMgr.setDiagHandler(SavedDiagHandler, SavedDiagContext);
  650. }
  651. void AsmParser::printMacroInstantiations() {
  652. // Print the active macro instantiation stack.
  653. for (std::vector<MacroInstantiation *>::const_reverse_iterator
  654. it = ActiveMacros.rbegin(),
  655. ie = ActiveMacros.rend();
  656. it != ie; ++it)
  657. printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
  658. "while in macro instantiation");
  659. }
  660. void AsmParser::Note(SMLoc L, const Twine &Msg, SMRange Range) {
  661. printPendingErrors();
  662. printMessage(L, SourceMgr::DK_Note, Msg, Range);
  663. printMacroInstantiations();
  664. }
  665. bool AsmParser::Warning(SMLoc L, const Twine &Msg, SMRange Range) {
  666. if(getTargetParser().getTargetOptions().MCNoWarn)
  667. return false;
  668. if (getTargetParser().getTargetOptions().MCFatalWarnings)
  669. return Error(L, Msg, Range);
  670. printMessage(L, SourceMgr::DK_Warning, Msg, Range);
  671. printMacroInstantiations();
  672. return false;
  673. }
  674. bool AsmParser::printError(SMLoc L, const Twine &Msg, SMRange Range) {
  675. HadError = true;
  676. printMessage(L, SourceMgr::DK_Error, Msg, Range);
  677. printMacroInstantiations();
  678. return true;
  679. }
  680. bool AsmParser::enterIncludeFile(const std::string &Filename) {
  681. std::string IncludedFile;
  682. unsigned NewBuf =
  683. SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
  684. if (!NewBuf)
  685. return true;
  686. CurBuffer = NewBuf;
  687. Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
  688. return false;
  689. }
  690. /// Process the specified .incbin file by searching for it in the include paths
  691. /// then just emitting the byte contents of the file to the streamer. This
  692. /// returns true on failure.
  693. bool AsmParser::processIncbinFile(const std::string &Filename, int64_t Skip,
  694. const MCExpr *Count, SMLoc Loc) {
  695. std::string IncludedFile;
  696. unsigned NewBuf =
  697. SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
  698. if (!NewBuf)
  699. return true;
  700. // Pick up the bytes from the file and emit them.
  701. StringRef Bytes = SrcMgr.getMemoryBuffer(NewBuf)->getBuffer();
  702. Bytes = Bytes.drop_front(Skip);
  703. if (Count) {
  704. int64_t Res;
  705. if (!Count->evaluateAsAbsolute(Res, getStreamer().getAssemblerPtr()))
  706. return Error(Loc, "expected absolute expression");
  707. if (Res < 0)
  708. return Warning(Loc, "negative count has no effect");
  709. Bytes = Bytes.take_front(Res);
  710. }
  711. getStreamer().emitBytes(Bytes);
  712. return false;
  713. }
  714. void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
  715. CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
  716. Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
  717. Loc.getPointer());
  718. }
  719. const AsmToken &AsmParser::Lex() {
  720. if (Lexer.getTok().is(AsmToken::Error))
  721. Error(Lexer.getErrLoc(), Lexer.getErr());
  722. // if it's a end of statement with a comment in it
  723. if (getTok().is(AsmToken::EndOfStatement)) {
  724. // if this is a line comment output it.
  725. if (!getTok().getString().empty() && getTok().getString().front() != '\n' &&
  726. getTok().getString().front() != '\r' && MAI.preserveAsmComments())
  727. Out.addExplicitComment(Twine(getTok().getString()));
  728. }
  729. const AsmToken *tok = &Lexer.Lex();
  730. // Parse comments here to be deferred until end of next statement.
  731. while (tok->is(AsmToken::Comment)) {
  732. if (MAI.preserveAsmComments())
  733. Out.addExplicitComment(Twine(tok->getString()));
  734. tok = &Lexer.Lex();
  735. }
  736. if (tok->is(AsmToken::Eof)) {
  737. // If this is the end of an included file, pop the parent file off the
  738. // include stack.
  739. SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
  740. if (ParentIncludeLoc != SMLoc()) {
  741. jumpToLoc(ParentIncludeLoc);
  742. return Lex();
  743. }
  744. }
  745. return *tok;
  746. }
  747. bool AsmParser::enabledGenDwarfForAssembly() {
  748. // Check whether the user specified -g.
  749. if (!getContext().getGenDwarfForAssembly())
  750. return false;
  751. // If we haven't encountered any .file directives (which would imply that
  752. // the assembler source was produced with debug info already) then emit one
  753. // describing the assembler source file itself.
  754. if (getContext().getGenDwarfFileNumber() == 0) {
  755. // Use the first #line directive for this, if any. It's preprocessed, so
  756. // there is no checksum, and of course no source directive.
  757. if (!FirstCppHashFilename.empty())
  758. getContext().setMCLineTableRootFile(/*CUID=*/0,
  759. getContext().getCompilationDir(),
  760. FirstCppHashFilename,
  761. /*Cksum=*/None, /*Source=*/None);
  762. const MCDwarfFile &RootFile =
  763. getContext().getMCDwarfLineTable(/*CUID=*/0).getRootFile();
  764. getContext().setGenDwarfFileNumber(getStreamer().emitDwarfFileDirective(
  765. /*CUID=*/0, getContext().getCompilationDir(), RootFile.Name,
  766. RootFile.Checksum, RootFile.Source));
  767. }
  768. return true;
  769. }
  770. bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
  771. // Create the initial section, if requested.
  772. if (!NoInitialTextSection)
  773. Out.InitSections(false);
  774. // Prime the lexer.
  775. Lex();
  776. HadError = false;
  777. AsmCond StartingCondState = TheCondState;
  778. SmallVector<AsmRewrite, 4> AsmStrRewrites;
  779. // If we are generating dwarf for assembly source files save the initial text
  780. // section. (Don't use enabledGenDwarfForAssembly() here, as we aren't
  781. // emitting any actual debug info yet and haven't had a chance to parse any
  782. // embedded .file directives.)
  783. if (getContext().getGenDwarfForAssembly()) {
  784. MCSection *Sec = getStreamer().getCurrentSectionOnly();
  785. if (!Sec->getBeginSymbol()) {
  786. MCSymbol *SectionStartSym = getContext().createTempSymbol();
  787. getStreamer().emitLabel(SectionStartSym);
  788. Sec->setBeginSymbol(SectionStartSym);
  789. }
  790. bool InsertResult = getContext().addGenDwarfSection(Sec);
  791. assert(InsertResult && ".text section should not have debug info yet");
  792. (void)InsertResult;
  793. }
  794. // While we have input, parse each statement.
  795. while (Lexer.isNot(AsmToken::Eof)) {
  796. ParseStatementInfo Info(&AsmStrRewrites);
  797. bool Parsed = parseStatement(Info, nullptr);
  798. // If we have a Lexer Error we are on an Error Token. Load in Lexer Error
  799. // for printing ErrMsg via Lex() only if no (presumably better) parser error
  800. // exists.
  801. if (Parsed && !hasPendingError() && Lexer.getTok().is(AsmToken::Error)) {
  802. Lex();
  803. }
  804. // parseStatement returned true so may need to emit an error.
  805. printPendingErrors();
  806. // Skipping to the next line if needed.
  807. if (Parsed && !getLexer().isAtStartOfStatement())
  808. eatToEndOfStatement();
  809. }
  810. getTargetParser().onEndOfFile();
  811. printPendingErrors();
  812. // All errors should have been emitted.
  813. assert(!hasPendingError() && "unexpected error from parseStatement");
  814. getTargetParser().flushPendingInstructions(getStreamer());
  815. if (TheCondState.TheCond != StartingCondState.TheCond ||
  816. TheCondState.Ignore != StartingCondState.Ignore)
  817. printError(getTok().getLoc(), "unmatched .ifs or .elses");
  818. // Check to see there are no empty DwarfFile slots.
  819. const auto &LineTables = getContext().getMCDwarfLineTables();
  820. if (!LineTables.empty()) {
  821. unsigned Index = 0;
  822. for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
  823. if (File.Name.empty() && Index != 0)
  824. printError(getTok().getLoc(), "unassigned file number: " +
  825. Twine(Index) +
  826. " for .file directives");
  827. ++Index;
  828. }
  829. }
  830. // Check to see that all assembler local symbols were actually defined.
  831. // Targets that don't do subsections via symbols may not want this, though,
  832. // so conservatively exclude them. Only do this if we're finalizing, though,
  833. // as otherwise we won't necessarilly have seen everything yet.
  834. if (!NoFinalize) {
  835. if (MAI.hasSubsectionsViaSymbols()) {
  836. for (const auto &TableEntry : getContext().getSymbols()) {
  837. MCSymbol *Sym = TableEntry.getValue();
  838. // Variable symbols may not be marked as defined, so check those
  839. // explicitly. If we know it's a variable, we have a definition for
  840. // the purposes of this check.
  841. if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
  842. // FIXME: We would really like to refer back to where the symbol was
  843. // first referenced for a source location. We need to add something
  844. // to track that. Currently, we just point to the end of the file.
  845. printError(getTok().getLoc(), "assembler local symbol '" +
  846. Sym->getName() + "' not defined");
  847. }
  848. }
  849. // Temporary symbols like the ones for directional jumps don't go in the
  850. // symbol table. They also need to be diagnosed in all (final) cases.
  851. for (std::tuple<SMLoc, CppHashInfoTy, MCSymbol *> &LocSym : DirLabels) {
  852. if (std::get<2>(LocSym)->isUndefined()) {
  853. // Reset the state of any "# line file" directives we've seen to the
  854. // context as it was at the diagnostic site.
  855. CppHashInfo = std::get<1>(LocSym);
  856. printError(std::get<0>(LocSym), "directional label undefined");
  857. }
  858. }
  859. }
  860. // Finalize the output stream if there are no errors and if the client wants
  861. // us to.
  862. if (!HadError && !NoFinalize)
  863. Out.Finish(Lexer.getLoc());
  864. return HadError || getContext().hadError();
  865. }
  866. bool AsmParser::checkForValidSection() {
  867. if (!ParsingMSInlineAsm && !getStreamer().getCurrentSectionOnly()) {
  868. Out.InitSections(false);
  869. return Error(getTok().getLoc(),
  870. "expected section directive before assembly directive");
  871. }
  872. return false;
  873. }
  874. /// Throw away the rest of the line for testing purposes.
  875. void AsmParser::eatToEndOfStatement() {
  876. while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
  877. Lexer.Lex();
  878. // Eat EOL.
  879. if (Lexer.is(AsmToken::EndOfStatement))
  880. Lexer.Lex();
  881. }
  882. StringRef AsmParser::parseStringToEndOfStatement() {
  883. const char *Start = getTok().getLoc().getPointer();
  884. while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
  885. Lexer.Lex();
  886. const char *End = getTok().getLoc().getPointer();
  887. return StringRef(Start, End - Start);
  888. }
  889. StringRef AsmParser::parseStringToComma() {
  890. const char *Start = getTok().getLoc().getPointer();
  891. while (Lexer.isNot(AsmToken::EndOfStatement) &&
  892. Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
  893. Lexer.Lex();
  894. const char *End = getTok().getLoc().getPointer();
  895. return StringRef(Start, End - Start);
  896. }
  897. /// Parse a paren expression and return it.
  898. /// NOTE: This assumes the leading '(' has already been consumed.
  899. ///
  900. /// parenexpr ::= expr)
  901. ///
  902. bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
  903. if (parseExpression(Res))
  904. return true;
  905. if (Lexer.isNot(AsmToken::RParen))
  906. return TokError("expected ')' in parentheses expression");
  907. EndLoc = Lexer.getTok().getEndLoc();
  908. Lex();
  909. return false;
  910. }
  911. /// Parse a bracket expression and return it.
  912. /// NOTE: This assumes the leading '[' has already been consumed.
  913. ///
  914. /// bracketexpr ::= expr]
  915. ///
  916. bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
  917. if (parseExpression(Res))
  918. return true;
  919. EndLoc = getTok().getEndLoc();
  920. if (parseToken(AsmToken::RBrac, "expected ']' in brackets expression"))
  921. return true;
  922. return false;
  923. }
  924. /// Parse a primary expression and return it.
  925. /// primaryexpr ::= (parenexpr
  926. /// primaryexpr ::= symbol
  927. /// primaryexpr ::= number
  928. /// primaryexpr ::= '.'
  929. /// primaryexpr ::= ~,+,- primaryexpr
  930. bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
  931. AsmTypeInfo *TypeInfo) {
  932. SMLoc FirstTokenLoc = getLexer().getLoc();
  933. AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
  934. switch (FirstTokenKind) {
  935. default:
  936. return TokError("unknown token in expression");
  937. // If we have an error assume that we've already handled it.
  938. case AsmToken::Error:
  939. return true;
  940. case AsmToken::Exclaim:
  941. Lex(); // Eat the operator.
  942. if (parsePrimaryExpr(Res, EndLoc, TypeInfo))
  943. return true;
  944. Res = MCUnaryExpr::createLNot(Res, getContext(), FirstTokenLoc);
  945. return false;
  946. case AsmToken::Dollar:
  947. case AsmToken::At:
  948. case AsmToken::String:
  949. case AsmToken::Identifier: {
  950. StringRef Identifier;
  951. if (parseIdentifier(Identifier)) {
  952. // We may have failed but $ may be a valid token.
  953. if (getTok().is(AsmToken::Dollar)) {
  954. if (Lexer.getMAI().getDollarIsPC()) {
  955. Lex();
  956. // This is a '$' reference, which references the current PC. Emit a
  957. // temporary label to the streamer and refer to it.
  958. MCSymbol *Sym = Ctx.createTempSymbol();
  959. Out.emitLabel(Sym);
  960. Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None,
  961. getContext());
  962. EndLoc = FirstTokenLoc;
  963. return false;
  964. }
  965. return Error(FirstTokenLoc, "invalid token in expression");
  966. }
  967. }
  968. // Parse symbol variant
  969. std::pair<StringRef, StringRef> Split;
  970. if (!MAI.useParensForSymbolVariant()) {
  971. if (FirstTokenKind == AsmToken::String) {
  972. if (Lexer.is(AsmToken::At)) {
  973. Lex(); // eat @
  974. SMLoc AtLoc = getLexer().getLoc();
  975. StringRef VName;
  976. if (parseIdentifier(VName))
  977. return Error(AtLoc, "expected symbol variant after '@'");
  978. Split = std::make_pair(Identifier, VName);
  979. }
  980. } else {
  981. Split = Identifier.split('@');
  982. }
  983. } else if (Lexer.is(AsmToken::LParen)) {
  984. Lex(); // eat '('.
  985. StringRef VName;
  986. parseIdentifier(VName);
  987. // eat ')'.
  988. if (parseToken(AsmToken::RParen,
  989. "unexpected token in variant, expected ')'"))
  990. return true;
  991. Split = std::make_pair(Identifier, VName);
  992. }
  993. EndLoc = SMLoc::getFromPointer(Identifier.end());
  994. // This is a symbol reference.
  995. StringRef SymbolName = Identifier;
  996. if (SymbolName.empty())
  997. return Error(getLexer().getLoc(), "expected a symbol reference");
  998. MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
  999. // Lookup the symbol variant if used.
  1000. if (!Split.second.empty()) {
  1001. Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
  1002. if (Variant != MCSymbolRefExpr::VK_Invalid) {
  1003. SymbolName = Split.first;
  1004. } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
  1005. Variant = MCSymbolRefExpr::VK_None;
  1006. } else {
  1007. return Error(SMLoc::getFromPointer(Split.second.begin()),
  1008. "invalid variant '" + Split.second + "'");
  1009. }
  1010. }
  1011. MCSymbol *Sym = getContext().getInlineAsmLabel(SymbolName);
  1012. if (!Sym)
  1013. Sym = getContext().getOrCreateSymbol(SymbolName);
  1014. // If this is an absolute variable reference, substitute it now to preserve
  1015. // semantics in the face of reassignment.
  1016. if (Sym->isVariable()) {
  1017. auto V = Sym->getVariableValue(/*SetUsed*/ false);
  1018. bool DoInline = isa<MCConstantExpr>(V) && !Variant;
  1019. if (auto TV = dyn_cast<MCTargetExpr>(V))
  1020. DoInline = TV->inlineAssignedExpr();
  1021. if (DoInline) {
  1022. if (Variant)
  1023. return Error(EndLoc, "unexpected modifier on variable reference");
  1024. Res = Sym->getVariableValue(/*SetUsed*/ false);
  1025. return false;
  1026. }
  1027. }
  1028. // Otherwise create a symbol ref.
  1029. Res = MCSymbolRefExpr::create(Sym, Variant, getContext(), FirstTokenLoc);
  1030. return false;
  1031. }
  1032. case AsmToken::BigNum:
  1033. return TokError("literal value out of range for directive");
  1034. case AsmToken::Integer: {
  1035. SMLoc Loc = getTok().getLoc();
  1036. int64_t IntVal = getTok().getIntVal();
  1037. Res = MCConstantExpr::create(IntVal, getContext());
  1038. EndLoc = Lexer.getTok().getEndLoc();
  1039. Lex(); // Eat token.
  1040. // Look for 'b' or 'f' following an Integer as a directional label
  1041. if (Lexer.getKind() == AsmToken::Identifier) {
  1042. StringRef IDVal = getTok().getString();
  1043. // Lookup the symbol variant if used.
  1044. std::pair<StringRef, StringRef> Split = IDVal.split('@');
  1045. MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
  1046. if (Split.first.size() != IDVal.size()) {
  1047. Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
  1048. if (Variant == MCSymbolRefExpr::VK_Invalid)
  1049. return TokError("invalid variant '" + Split.second + "'");
  1050. IDVal = Split.first;
  1051. }
  1052. if (IDVal == "f" || IDVal == "b") {
  1053. MCSymbol *Sym =
  1054. Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b");
  1055. Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
  1056. if (IDVal == "b" && Sym->isUndefined())
  1057. return Error(Loc, "directional label undefined");
  1058. DirLabels.push_back(std::make_tuple(Loc, CppHashInfo, Sym));
  1059. EndLoc = Lexer.getTok().getEndLoc();
  1060. Lex(); // Eat identifier.
  1061. }
  1062. }
  1063. return false;
  1064. }
  1065. case AsmToken::Real: {
  1066. APFloat RealVal(APFloat::IEEEdouble(), getTok().getString());
  1067. uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
  1068. Res = MCConstantExpr::create(IntVal, getContext());
  1069. EndLoc = Lexer.getTok().getEndLoc();
  1070. Lex(); // Eat token.
  1071. return false;
  1072. }
  1073. case AsmToken::Dot: {
  1074. // This is a '.' reference, which references the current PC. Emit a
  1075. // temporary label to the streamer and refer to it.
  1076. MCSymbol *Sym = Ctx.createTempSymbol();
  1077. Out.emitLabel(Sym);
  1078. Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
  1079. EndLoc = Lexer.getTok().getEndLoc();
  1080. Lex(); // Eat identifier.
  1081. return false;
  1082. }
  1083. case AsmToken::LParen:
  1084. Lex(); // Eat the '('.
  1085. return parseParenExpr(Res, EndLoc);
  1086. case AsmToken::LBrac:
  1087. if (!PlatformParser->HasBracketExpressions())
  1088. return TokError("brackets expression not supported on this target");
  1089. Lex(); // Eat the '['.
  1090. return parseBracketExpr(Res, EndLoc);
  1091. case AsmToken::Minus:
  1092. Lex(); // Eat the operator.
  1093. if (parsePrimaryExpr(Res, EndLoc, TypeInfo))
  1094. return true;
  1095. Res = MCUnaryExpr::createMinus(Res, getContext(), FirstTokenLoc);
  1096. return false;
  1097. case AsmToken::Plus:
  1098. Lex(); // Eat the operator.
  1099. if (parsePrimaryExpr(Res, EndLoc, TypeInfo))
  1100. return true;
  1101. Res = MCUnaryExpr::createPlus(Res, getContext(), FirstTokenLoc);
  1102. return false;
  1103. case AsmToken::Tilde:
  1104. Lex(); // Eat the operator.
  1105. if (parsePrimaryExpr(Res, EndLoc, TypeInfo))
  1106. return true;
  1107. Res = MCUnaryExpr::createNot(Res, getContext(), FirstTokenLoc);
  1108. return false;
  1109. // MIPS unary expression operators. The lexer won't generate these tokens if
  1110. // MCAsmInfo::HasMipsExpressions is false for the target.
  1111. case AsmToken::PercentCall16:
  1112. case AsmToken::PercentCall_Hi:
  1113. case AsmToken::PercentCall_Lo:
  1114. case AsmToken::PercentDtprel_Hi:
  1115. case AsmToken::PercentDtprel_Lo:
  1116. case AsmToken::PercentGot:
  1117. case AsmToken::PercentGot_Disp:
  1118. case AsmToken::PercentGot_Hi:
  1119. case AsmToken::PercentGot_Lo:
  1120. case AsmToken::PercentGot_Ofst:
  1121. case AsmToken::PercentGot_Page:
  1122. case AsmToken::PercentGottprel:
  1123. case AsmToken::PercentGp_Rel:
  1124. case AsmToken::PercentHi:
  1125. case AsmToken::PercentHigher:
  1126. case AsmToken::PercentHighest:
  1127. case AsmToken::PercentLo:
  1128. case AsmToken::PercentNeg:
  1129. case AsmToken::PercentPcrel_Hi:
  1130. case AsmToken::PercentPcrel_Lo:
  1131. case AsmToken::PercentTlsgd:
  1132. case AsmToken::PercentTlsldm:
  1133. case AsmToken::PercentTprel_Hi:
  1134. case AsmToken::PercentTprel_Lo:
  1135. Lex(); // Eat the operator.
  1136. if (Lexer.isNot(AsmToken::LParen))
  1137. return TokError("expected '(' after operator");
  1138. Lex(); // Eat the operator.
  1139. if (parseExpression(Res, EndLoc))
  1140. return true;
  1141. if (Lexer.isNot(AsmToken::RParen))
  1142. return TokError("expected ')'");
  1143. Lex(); // Eat the operator.
  1144. Res = getTargetParser().createTargetUnaryExpr(Res, FirstTokenKind, Ctx);
  1145. return !Res;
  1146. }
  1147. }
  1148. bool AsmParser::parseExpression(const MCExpr *&Res) {
  1149. SMLoc EndLoc;
  1150. return parseExpression(Res, EndLoc);
  1151. }
  1152. const MCExpr *
  1153. AsmParser::applyModifierToExpr(const MCExpr *E,
  1154. MCSymbolRefExpr::VariantKind Variant) {
  1155. // Ask the target implementation about this expression first.
  1156. const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
  1157. if (NewE)
  1158. return NewE;
  1159. // Recurse over the given expression, rebuilding it to apply the given variant
  1160. // if there is exactly one symbol.
  1161. switch (E->getKind()) {
  1162. case MCExpr::Target:
  1163. case MCExpr::Constant:
  1164. return nullptr;
  1165. case MCExpr::SymbolRef: {
  1166. const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
  1167. if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
  1168. TokError("invalid variant on expression '" + getTok().getIdentifier() +
  1169. "' (already modified)");
  1170. return E;
  1171. }
  1172. return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, getContext());
  1173. }
  1174. case MCExpr::Unary: {
  1175. const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
  1176. const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
  1177. if (!Sub)
  1178. return nullptr;
  1179. return MCUnaryExpr::create(UE->getOpcode(), Sub, getContext());
  1180. }
  1181. case MCExpr::Binary: {
  1182. const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
  1183. const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
  1184. const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
  1185. if (!LHS && !RHS)
  1186. return nullptr;
  1187. if (!LHS)
  1188. LHS = BE->getLHS();
  1189. if (!RHS)
  1190. RHS = BE->getRHS();
  1191. return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, getContext());
  1192. }
  1193. }
  1194. llvm_unreachable("Invalid expression kind!");
  1195. }
  1196. /// This function checks if the next token is <string> type or arithmetic.
  1197. /// string that begin with character '<' must end with character '>'.
  1198. /// otherwise it is arithmetics.
  1199. /// If the function returns a 'true' value,
  1200. /// the End argument will be filled with the last location pointed to the '>'
  1201. /// character.
  1202. /// There is a gap between the AltMacro's documentation and the single quote
  1203. /// implementation. GCC does not fully support this feature and so we will not
  1204. /// support it.
  1205. /// TODO: Adding single quote as a string.
  1206. static bool isAngleBracketString(SMLoc &StrLoc, SMLoc &EndLoc) {
  1207. assert((StrLoc.getPointer() != nullptr) &&
  1208. "Argument to the function cannot be a NULL value");
  1209. const char *CharPtr = StrLoc.getPointer();
  1210. while ((*CharPtr != '>') && (*CharPtr != '\n') && (*CharPtr != '\r') &&
  1211. (*CharPtr != '\0')) {
  1212. if (*CharPtr == '!')
  1213. CharPtr++;
  1214. CharPtr++;
  1215. }
  1216. if (*CharPtr == '>') {
  1217. EndLoc = StrLoc.getFromPointer(CharPtr + 1);
  1218. return true;
  1219. }
  1220. return false;
  1221. }
  1222. /// creating a string without the escape characters '!'.
  1223. static std::string angleBracketString(StringRef AltMacroStr) {
  1224. std::string Res;
  1225. for (size_t Pos = 0; Pos < AltMacroStr.size(); Pos++) {
  1226. if (AltMacroStr[Pos] == '!')
  1227. Pos++;
  1228. Res += AltMacroStr[Pos];
  1229. }
  1230. return Res;
  1231. }
  1232. /// Parse an expression and return it.
  1233. ///
  1234. /// expr ::= expr &&,|| expr -> lowest.
  1235. /// expr ::= expr |,^,&,! expr
  1236. /// expr ::= expr ==,!=,<>,<,<=,>,>= expr
  1237. /// expr ::= expr <<,>> expr
  1238. /// expr ::= expr +,- expr
  1239. /// expr ::= expr *,/,% expr -> highest.
  1240. /// expr ::= primaryexpr
  1241. ///
  1242. bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
  1243. // Parse the expression.
  1244. Res = nullptr;
  1245. if (getTargetParser().parsePrimaryExpr(Res, EndLoc) ||
  1246. parseBinOpRHS(1, Res, EndLoc))
  1247. return true;
  1248. // As a special case, we support 'a op b @ modifier' by rewriting the
  1249. // expression to include the modifier. This is inefficient, but in general we
  1250. // expect users to use 'a@modifier op b'.
  1251. if (Lexer.getKind() == AsmToken::At) {
  1252. Lex();
  1253. if (Lexer.isNot(AsmToken::Identifier))
  1254. return TokError("unexpected symbol modifier following '@'");
  1255. MCSymbolRefExpr::VariantKind Variant =
  1256. MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
  1257. if (Variant == MCSymbolRefExpr::VK_Invalid)
  1258. return TokError("invalid variant '" + getTok().getIdentifier() + "'");
  1259. const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
  1260. if (!ModifiedRes) {
  1261. return TokError("invalid modifier '" + getTok().getIdentifier() +
  1262. "' (no symbols present)");
  1263. }
  1264. Res = ModifiedRes;
  1265. Lex();
  1266. }
  1267. // Try to constant fold it up front, if possible. Do not exploit
  1268. // assembler here.
  1269. int64_t Value;
  1270. if (Res->evaluateAsAbsolute(Value))
  1271. Res = MCConstantExpr::create(Value, getContext());
  1272. return false;
  1273. }
  1274. bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
  1275. Res = nullptr;
  1276. return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
  1277. }
  1278. bool AsmParser::parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
  1279. SMLoc &EndLoc) {
  1280. if (parseParenExpr(Res, EndLoc))
  1281. return true;
  1282. for (; ParenDepth > 0; --ParenDepth) {
  1283. if (parseBinOpRHS(1, Res, EndLoc))
  1284. return true;
  1285. // We don't Lex() the last RParen.
  1286. // This is the same behavior as parseParenExpression().
  1287. if (ParenDepth - 1 > 0) {
  1288. EndLoc = getTok().getEndLoc();
  1289. if (parseToken(AsmToken::RParen,
  1290. "expected ')' in parentheses expression"))
  1291. return true;
  1292. }
  1293. }
  1294. return false;
  1295. }
  1296. bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
  1297. const MCExpr *Expr;
  1298. SMLoc StartLoc = Lexer.getLoc();
  1299. if (parseExpression(Expr))
  1300. return true;
  1301. if (!Expr->evaluateAsAbsolute(Res, getStreamer().getAssemblerPtr()))
  1302. return Error(StartLoc, "expected absolute expression");
  1303. return false;
  1304. }
  1305. static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K,
  1306. MCBinaryExpr::Opcode &Kind,
  1307. bool ShouldUseLogicalShr) {
  1308. switch (K) {
  1309. default:
  1310. return 0; // not a binop.
  1311. // Lowest Precedence: &&, ||
  1312. case AsmToken::AmpAmp:
  1313. Kind = MCBinaryExpr::LAnd;
  1314. return 1;
  1315. case AsmToken::PipePipe:
  1316. Kind = MCBinaryExpr::LOr;
  1317. return 1;
  1318. // Low Precedence: |, &, ^
  1319. case AsmToken::Pipe:
  1320. Kind = MCBinaryExpr::Or;
  1321. return 2;
  1322. case AsmToken::Caret:
  1323. Kind = MCBinaryExpr::Xor;
  1324. return 2;
  1325. case AsmToken::Amp:
  1326. Kind = MCBinaryExpr::And;
  1327. return 2;
  1328. // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
  1329. case AsmToken::EqualEqual:
  1330. Kind = MCBinaryExpr::EQ;
  1331. return 3;
  1332. case AsmToken::ExclaimEqual:
  1333. case AsmToken::LessGreater:
  1334. Kind = MCBinaryExpr::NE;
  1335. return 3;
  1336. case AsmToken::Less:
  1337. Kind = MCBinaryExpr::LT;
  1338. return 3;
  1339. case AsmToken::LessEqual:
  1340. Kind = MCBinaryExpr::LTE;
  1341. return 3;
  1342. case AsmToken::Greater:
  1343. Kind = MCBinaryExpr::GT;
  1344. return 3;
  1345. case AsmToken::GreaterEqual:
  1346. Kind = MCBinaryExpr::GTE;
  1347. return 3;
  1348. // Intermediate Precedence: <<, >>
  1349. case AsmToken::LessLess:
  1350. Kind = MCBinaryExpr::Shl;
  1351. return 4;
  1352. case AsmToken::GreaterGreater:
  1353. Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
  1354. return 4;
  1355. // High Intermediate Precedence: +, -
  1356. case AsmToken::Plus:
  1357. Kind = MCBinaryExpr::Add;
  1358. return 5;
  1359. case AsmToken::Minus:
  1360. Kind = MCBinaryExpr::Sub;
  1361. return 5;
  1362. // Highest Precedence: *, /, %
  1363. case AsmToken::Star:
  1364. Kind = MCBinaryExpr::Mul;
  1365. return 6;
  1366. case AsmToken::Slash:
  1367. Kind = MCBinaryExpr::Div;
  1368. return 6;
  1369. case AsmToken::Percent:
  1370. Kind = MCBinaryExpr::Mod;
  1371. return 6;
  1372. }
  1373. }
  1374. static unsigned getGNUBinOpPrecedence(const MCAsmInfo &MAI,
  1375. AsmToken::TokenKind K,
  1376. MCBinaryExpr::Opcode &Kind,
  1377. bool ShouldUseLogicalShr) {
  1378. switch (K) {
  1379. default:
  1380. return 0; // not a binop.
  1381. // Lowest Precedence: &&, ||
  1382. case AsmToken::AmpAmp:
  1383. Kind = MCBinaryExpr::LAnd;
  1384. return 2;
  1385. case AsmToken::PipePipe:
  1386. Kind = MCBinaryExpr::LOr;
  1387. return 1;
  1388. // Low Precedence: ==, !=, <>, <, <=, >, >=
  1389. case AsmToken::EqualEqual:
  1390. Kind = MCBinaryExpr::EQ;
  1391. return 3;
  1392. case AsmToken::ExclaimEqual:
  1393. case AsmToken::LessGreater:
  1394. Kind = MCBinaryExpr::NE;
  1395. return 3;
  1396. case AsmToken::Less:
  1397. Kind = MCBinaryExpr::LT;
  1398. return 3;
  1399. case AsmToken::LessEqual:
  1400. Kind = MCBinaryExpr::LTE;
  1401. return 3;
  1402. case AsmToken::Greater:
  1403. Kind = MCBinaryExpr::GT;
  1404. return 3;
  1405. case AsmToken::GreaterEqual:
  1406. Kind = MCBinaryExpr::GTE;
  1407. return 3;
  1408. // Low Intermediate Precedence: +, -
  1409. case AsmToken::Plus:
  1410. Kind = MCBinaryExpr::Add;
  1411. return 4;
  1412. case AsmToken::Minus:
  1413. Kind = MCBinaryExpr::Sub;
  1414. return 4;
  1415. // High Intermediate Precedence: |, !, &, ^
  1416. //
  1417. case AsmToken::Pipe:
  1418. Kind = MCBinaryExpr::Or;
  1419. return 5;
  1420. case AsmToken::Exclaim:
  1421. // Hack to support ARM compatible aliases (implied 'sp' operand in 'srs*'
  1422. // instructions like 'srsda #31!') and not parse ! as an infix operator.
  1423. if (MAI.getCommentString() == "@")
  1424. return 0;
  1425. Kind = MCBinaryExpr::OrNot;
  1426. return 5;
  1427. case AsmToken::Caret:
  1428. Kind = MCBinaryExpr::Xor;
  1429. return 5;
  1430. case AsmToken::Amp:
  1431. Kind = MCBinaryExpr::And;
  1432. return 5;
  1433. // Highest Precedence: *, /, %, <<, >>
  1434. case AsmToken::Star:
  1435. Kind = MCBinaryExpr::Mul;
  1436. return 6;
  1437. case AsmToken::Slash:
  1438. Kind = MCBinaryExpr::Div;
  1439. return 6;
  1440. case AsmToken::Percent:
  1441. Kind = MCBinaryExpr::Mod;
  1442. return 6;
  1443. case AsmToken::LessLess:
  1444. Kind = MCBinaryExpr::Shl;
  1445. return 6;
  1446. case AsmToken::GreaterGreater:
  1447. Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
  1448. return 6;
  1449. }
  1450. }
  1451. unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K,
  1452. MCBinaryExpr::Opcode &Kind) {
  1453. bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr();
  1454. return IsDarwin ? getDarwinBinOpPrecedence(K, Kind, ShouldUseLogicalShr)
  1455. : getGNUBinOpPrecedence(MAI, K, Kind, ShouldUseLogicalShr);
  1456. }
  1457. /// Parse all binary operators with precedence >= 'Precedence'.
  1458. /// Res contains the LHS of the expression on input.
  1459. bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
  1460. SMLoc &EndLoc) {
  1461. SMLoc StartLoc = Lexer.getLoc();
  1462. while (true) {
  1463. MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
  1464. unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
  1465. // If the next token is lower precedence than we are allowed to eat, return
  1466. // successfully with what we ate already.
  1467. if (TokPrec < Precedence)
  1468. return false;
  1469. Lex();
  1470. // Eat the next primary expression.
  1471. const MCExpr *RHS;
  1472. if (getTargetParser().parsePrimaryExpr(RHS, EndLoc))
  1473. return true;
  1474. // If BinOp binds less tightly with RHS than the operator after RHS, let
  1475. // the pending operator take RHS as its LHS.
  1476. MCBinaryExpr::Opcode Dummy;
  1477. unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
  1478. if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
  1479. return true;
  1480. // Merge LHS and RHS according to operator.
  1481. Res = MCBinaryExpr::create(Kind, Res, RHS, getContext(), StartLoc);
  1482. }
  1483. }
  1484. /// ParseStatement:
  1485. /// ::= EndOfStatement
  1486. /// ::= Label* Directive ...Operands... EndOfStatement
  1487. /// ::= Label* Identifier OperandList* EndOfStatement
  1488. bool AsmParser::parseStatement(ParseStatementInfo &Info,
  1489. MCAsmParserSemaCallback *SI) {
  1490. assert(!hasPendingError() && "parseStatement started with pending error");
  1491. // Eat initial spaces and comments
  1492. while (Lexer.is(AsmToken::Space))
  1493. Lex();
  1494. if (Lexer.is(AsmToken::EndOfStatement)) {
  1495. // if this is a line comment we can drop it safely
  1496. if (getTok().getString().empty() || getTok().getString().front() == '\r' ||
  1497. getTok().getString().front() == '\n')
  1498. Out.AddBlankLine();
  1499. Lex();
  1500. return false;
  1501. }
  1502. // Statements always start with an identifier.
  1503. AsmToken ID = getTok();
  1504. SMLoc IDLoc = ID.getLoc();
  1505. StringRef IDVal;
  1506. int64_t LocalLabelVal = -1;
  1507. StartTokLoc = ID.getLoc();
  1508. if (Lexer.is(AsmToken::HashDirective))
  1509. return parseCppHashLineFilenameComment(IDLoc,
  1510. !isInsideMacroInstantiation());
  1511. // Allow an integer followed by a ':' as a directional local label.
  1512. if (Lexer.is(AsmToken::Integer)) {
  1513. LocalLabelVal = getTok().getIntVal();
  1514. if (LocalLabelVal < 0) {
  1515. if (!TheCondState.Ignore) {
  1516. Lex(); // always eat a token
  1517. return Error(IDLoc, "unexpected token at start of statement");
  1518. }
  1519. IDVal = "";
  1520. } else {
  1521. IDVal = getTok().getString();
  1522. Lex(); // Consume the integer token to be used as an identifier token.
  1523. if (Lexer.getKind() != AsmToken::Colon) {
  1524. if (!TheCondState.Ignore) {
  1525. Lex(); // always eat a token
  1526. return Error(IDLoc, "unexpected token at start of statement");
  1527. }
  1528. }
  1529. }
  1530. } else if (Lexer.is(AsmToken::Dot)) {
  1531. // Treat '.' as a valid identifier in this context.
  1532. Lex();
  1533. IDVal = ".";
  1534. } else if (Lexer.is(AsmToken::LCurly)) {
  1535. // Treat '{' as a valid identifier in this context.
  1536. Lex();
  1537. IDVal = "{";
  1538. } else if (Lexer.is(AsmToken::RCurly)) {
  1539. // Treat '}' as a valid identifier in this context.
  1540. Lex();
  1541. IDVal = "}";
  1542. } else if (Lexer.is(AsmToken::Star) &&
  1543. getTargetParser().starIsStartOfStatement()) {
  1544. // Accept '*' as a valid start of statement.
  1545. Lex();
  1546. IDVal = "*";
  1547. } else if (parseIdentifier(IDVal)) {
  1548. if (!TheCondState.Ignore) {
  1549. Lex(); // always eat a token
  1550. return Error(IDLoc, "unexpected token at start of statement");
  1551. }
  1552. IDVal = "";
  1553. }
  1554. // Handle conditional assembly here before checking for skipping. We
  1555. // have to do this so that .endif isn't skipped in a ".if 0" block for
  1556. // example.
  1557. StringMap<DirectiveKind>::const_iterator DirKindIt =
  1558. DirectiveKindMap.find(IDVal.lower());
  1559. DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
  1560. ? DK_NO_DIRECTIVE
  1561. : DirKindIt->getValue();
  1562. switch (DirKind) {
  1563. default:
  1564. break;
  1565. case DK_IF:
  1566. case DK_IFEQ:
  1567. case DK_IFGE:
  1568. case DK_IFGT:
  1569. case DK_IFLE:
  1570. case DK_IFLT:
  1571. case DK_IFNE:
  1572. return parseDirectiveIf(IDLoc, DirKind);
  1573. case DK_IFB:
  1574. return parseDirectiveIfb(IDLoc, true);
  1575. case DK_IFNB:
  1576. return parseDirectiveIfb(IDLoc, false);
  1577. case DK_IFC:
  1578. return parseDirectiveIfc(IDLoc, true);
  1579. case DK_IFEQS:
  1580. return parseDirectiveIfeqs(IDLoc, true);
  1581. case DK_IFNC:
  1582. return parseDirectiveIfc(IDLoc, false);
  1583. case DK_IFNES:
  1584. return parseDirectiveIfeqs(IDLoc, false);
  1585. case DK_IFDEF:
  1586. return parseDirectiveIfdef(IDLoc, true);
  1587. case DK_IFNDEF:
  1588. case DK_IFNOTDEF:
  1589. return parseDirectiveIfdef(IDLoc, false);
  1590. case DK_ELSEIF:
  1591. return parseDirectiveElseIf(IDLoc);
  1592. case DK_ELSE:
  1593. return parseDirectiveElse(IDLoc);
  1594. case DK_ENDIF:
  1595. return parseDirectiveEndIf(IDLoc);
  1596. }
  1597. // Ignore the statement if in the middle of inactive conditional
  1598. // (e.g. ".if 0").
  1599. if (TheCondState.Ignore) {
  1600. eatToEndOfStatement();
  1601. return false;
  1602. }
  1603. // FIXME: Recurse on local labels?
  1604. // See what kind of statement we have.
  1605. switch (Lexer.getKind()) {
  1606. case AsmToken::Colon: {
  1607. if (!getTargetParser().isLabel(ID))
  1608. break;
  1609. if (checkForValidSection())
  1610. return true;
  1611. // identifier ':' -> Label.
  1612. Lex();
  1613. // Diagnose attempt to use '.' as a label.
  1614. if (IDVal == ".")
  1615. return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
  1616. // Diagnose attempt to use a variable as a label.
  1617. //
  1618. // FIXME: Diagnostics. Note the location of the definition as a label.
  1619. // FIXME: This doesn't diagnose assignment to a symbol which has been
  1620. // implicitly marked as external.
  1621. MCSymbol *Sym;
  1622. if (LocalLabelVal == -1) {
  1623. if (ParsingMSInlineAsm && SI) {
  1624. StringRef RewrittenLabel =
  1625. SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
  1626. assert(!RewrittenLabel.empty() &&
  1627. "We should have an internal name here.");
  1628. Info.AsmRewrites->emplace_back(AOK_Label, IDLoc, IDVal.size(),
  1629. RewrittenLabel);
  1630. IDVal = RewrittenLabel;
  1631. }
  1632. Sym = getContext().getOrCreateSymbol(IDVal);
  1633. } else
  1634. Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal);
  1635. // End of Labels should be treated as end of line for lexing
  1636. // purposes but that information is not available to the Lexer who
  1637. // does not understand Labels. This may cause us to see a Hash
  1638. // here instead of a preprocessor line comment.
  1639. if (getTok().is(AsmToken::Hash)) {
  1640. StringRef CommentStr = parseStringToEndOfStatement();
  1641. Lexer.Lex();
  1642. Lexer.UnLex(AsmToken(AsmToken::EndOfStatement, CommentStr));
  1643. }
  1644. // Consume any end of statement token, if present, to avoid spurious
  1645. // AddBlankLine calls().
  1646. if (getTok().is(AsmToken::EndOfStatement)) {
  1647. Lex();
  1648. }
  1649. getTargetParser().doBeforeLabelEmit(Sym);
  1650. // Emit the label.
  1651. if (!getTargetParser().isParsingMSInlineAsm())
  1652. Out.emitLabel(Sym, IDLoc);
  1653. // If we are generating dwarf for assembly source files then gather the
  1654. // info to make a dwarf label entry for this label if needed.
  1655. if (enabledGenDwarfForAssembly())
  1656. MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
  1657. IDLoc);
  1658. getTargetParser().onLabelParsed(Sym);
  1659. return false;
  1660. }
  1661. case AsmToken::Equal:
  1662. if (!getTargetParser().equalIsAsmAssignment())
  1663. break;
  1664. // identifier '=' ... -> assignment statement
  1665. Lex();
  1666. return parseAssignment(IDVal, true);
  1667. default: // Normal instruction or directive.
  1668. break;
  1669. }
  1670. // If macros are enabled, check to see if this is a macro instantiation.
  1671. if (areMacrosEnabled())
  1672. if (const MCAsmMacro *M = getContext().lookupMacro(IDVal)) {
  1673. return handleMacroEntry(M, IDLoc);
  1674. }
  1675. // Otherwise, we have a normal instruction or directive.
  1676. // Directives start with "."
  1677. if (IDVal.startswith(".") && IDVal != ".") {
  1678. // There are several entities interested in parsing directives:
  1679. //
  1680. // 1. The target-specific assembly parser. Some directives are target
  1681. // specific or may potentially behave differently on certain targets.
  1682. // 2. Asm parser extensions. For example, platform-specific parsers
  1683. // (like the ELF parser) register themselves as extensions.
  1684. // 3. The generic directive parser implemented by this class. These are
  1685. // all the directives that behave in a target and platform independent
  1686. // manner, or at least have a default behavior that's shared between
  1687. // all targets and platforms.
  1688. getTargetParser().flushPendingInstructions(getStreamer());
  1689. SMLoc StartTokLoc = getTok().getLoc();
  1690. bool TPDirectiveReturn = getTargetParser().ParseDirective(ID);
  1691. if (hasPendingError())
  1692. return true;
  1693. // Currently the return value should be true if we are
  1694. // uninterested but as this is at odds with the standard parsing
  1695. // convention (return true = error) we have instances of a parsed
  1696. // directive that fails returning true as an error. Catch these
  1697. // cases as best as possible errors here.
  1698. if (TPDirectiveReturn && StartTokLoc != getTok().getLoc())
  1699. return true;
  1700. // Return if we did some parsing or believe we succeeded.
  1701. if (!TPDirectiveReturn || StartTokLoc != getTok().getLoc())
  1702. return false;
  1703. // Next, check the extension directive map to see if any extension has
  1704. // registered itself to parse this directive.
  1705. std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
  1706. ExtensionDirectiveMap.lookup(IDVal);
  1707. if (Handler.first)
  1708. return (*Handler.second)(Handler.first, IDVal, IDLoc);
  1709. // Finally, if no one else is interested in this directive, it must be
  1710. // generic and familiar to this class.
  1711. switch (DirKind) {
  1712. default:
  1713. break;
  1714. case DK_SET:
  1715. case DK_EQU:
  1716. return parseDirectiveSet(IDVal, true);
  1717. case DK_EQUIV:
  1718. return parseDirectiveSet(IDVal, false);
  1719. case DK_ASCII:
  1720. return parseDirectiveAscii(IDVal, false);
  1721. case DK_ASCIZ:
  1722. case DK_STRING:
  1723. return parseDirectiveAscii(IDVal, true);
  1724. case DK_BYTE:
  1725. case DK_DC_B:
  1726. return parseDirectiveValue(IDVal, 1);
  1727. case DK_DC:
  1728. case DK_DC_W:
  1729. case DK_SHORT:
  1730. case DK_VALUE:
  1731. case DK_2BYTE:
  1732. return parseDirectiveValue(IDVal, 2);
  1733. case DK_LONG:
  1734. case DK_INT:
  1735. case DK_4BYTE:
  1736. case DK_DC_L:
  1737. return parseDirectiveValue(IDVal, 4);
  1738. case DK_QUAD:
  1739. case DK_8BYTE:
  1740. return parseDirectiveValue(IDVal, 8);
  1741. case DK_DC_A:
  1742. return parseDirectiveValue(
  1743. IDVal, getContext().getAsmInfo()->getCodePointerSize());
  1744. case DK_OCTA:
  1745. return parseDirectiveOctaValue(IDVal);
  1746. case DK_SINGLE:
  1747. case DK_FLOAT:
  1748. case DK_DC_S:
  1749. return parseDirectiveRealValue(IDVal, APFloat::IEEEsingle());
  1750. case DK_DOUBLE:
  1751. case DK_DC_D:
  1752. return parseDirectiveRealValue(IDVal, APFloat::IEEEdouble());
  1753. case DK_ALIGN: {
  1754. bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
  1755. return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
  1756. }
  1757. case DK_ALIGN32: {
  1758. bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
  1759. return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
  1760. }
  1761. case DK_BALIGN:
  1762. return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
  1763. case DK_BALIGNW:
  1764. return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
  1765. case DK_BALIGNL:
  1766. return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
  1767. case DK_P2ALIGN:
  1768. return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
  1769. case DK_P2ALIGNW:
  1770. return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
  1771. case DK_P2ALIGNL:
  1772. return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
  1773. case DK_ORG:
  1774. return parseDirectiveOrg();
  1775. case DK_FILL:
  1776. return parseDirectiveFill();
  1777. case DK_ZERO:
  1778. return parseDirectiveZero();
  1779. case DK_EXTERN:
  1780. eatToEndOfStatement(); // .extern is the default, ignore it.
  1781. return false;
  1782. case DK_GLOBL:
  1783. case DK_GLOBAL:
  1784. return parseDirectiveSymbolAttribute(MCSA_Global);
  1785. case DK_LAZY_REFERENCE:
  1786. return parseDirectiveSymbolAttribute(MCSA_LazyReference);
  1787. case DK_NO_DEAD_STRIP:
  1788. return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
  1789. case DK_SYMBOL_RESOLVER:
  1790. return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
  1791. case DK_PRIVATE_EXTERN:
  1792. return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
  1793. case DK_REFERENCE:
  1794. return parseDirectiveSymbolAttribute(MCSA_Reference);
  1795. case DK_WEAK_DEFINITION:
  1796. return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
  1797. case DK_WEAK_REFERENCE:
  1798. return parseDirectiveSymbolAttribute(MCSA_WeakReference);
  1799. case DK_WEAK_DEF_CAN_BE_HIDDEN:
  1800. return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
  1801. case DK_COLD:
  1802. return parseDirectiveSymbolAttribute(MCSA_Cold);
  1803. case DK_COMM:
  1804. case DK_COMMON:
  1805. return parseDirectiveComm(/*IsLocal=*/false);
  1806. case DK_LCOMM:
  1807. return parseDirectiveComm(/*IsLocal=*/true);
  1808. case DK_ABORT:
  1809. return parseDirectiveAbort();
  1810. case DK_INCLUDE:
  1811. return parseDirectiveInclude();
  1812. case DK_INCBIN:
  1813. return parseDirectiveIncbin();
  1814. case DK_CODE16:
  1815. case DK_CODE16GCC:
  1816. return TokError(Twine(IDVal) +
  1817. " not currently supported for this target");
  1818. case DK_REPT:
  1819. return parseDirectiveRept(IDLoc, IDVal);
  1820. case DK_IRP:
  1821. return parseDirectiveIrp(IDLoc);
  1822. case DK_IRPC:
  1823. return parseDirectiveIrpc(IDLoc);
  1824. case DK_ENDR:
  1825. return parseDirectiveEndr(IDLoc);
  1826. case DK_BUNDLE_ALIGN_MODE:
  1827. return parseDirectiveBundleAlignMode();
  1828. case DK_BUNDLE_LOCK:
  1829. return parseDirectiveBundleLock();
  1830. case DK_BUNDLE_UNLOCK:
  1831. return parseDirectiveBundleUnlock();
  1832. case DK_SLEB128:
  1833. return parseDirectiveLEB128(true);
  1834. case DK_ULEB128:
  1835. return parseDirectiveLEB128(false);
  1836. case DK_SPACE:
  1837. case DK_SKIP:
  1838. return parseDirectiveSpace(IDVal);
  1839. case DK_FILE:
  1840. return parseDirectiveFile(IDLoc);
  1841. case DK_LINE:
  1842. return parseDirectiveLine();
  1843. case DK_LOC:
  1844. return parseDirectiveLoc();
  1845. case DK_STABS:
  1846. return parseDirectiveStabs();
  1847. case DK_CV_FILE:
  1848. return parseDirectiveCVFile();
  1849. case DK_CV_FUNC_ID:
  1850. return parseDirectiveCVFuncId();
  1851. case DK_CV_INLINE_SITE_ID:
  1852. return parseDirectiveCVInlineSiteId();
  1853. case DK_CV_LOC:
  1854. return parseDirectiveCVLoc();
  1855. case DK_CV_LINETABLE:
  1856. return parseDirectiveCVLinetable();
  1857. case DK_CV_INLINE_LINETABLE:
  1858. return parseDirectiveCVInlineLinetable();
  1859. case DK_CV_DEF_RANGE:
  1860. return parseDirectiveCVDefRange();
  1861. case DK_CV_STRING:
  1862. return parseDirectiveCVString();
  1863. case DK_CV_STRINGTABLE:
  1864. return parseDirectiveCVStringTable();
  1865. case DK_CV_FILECHECKSUMS:
  1866. return parseDirectiveCVFileChecksums();
  1867. case DK_CV_FILECHECKSUM_OFFSET:
  1868. return parseDirectiveCVFileChecksumOffset();
  1869. case DK_CV_FPO_DATA:
  1870. return parseDirectiveCVFPOData();
  1871. case DK_CFI_SECTIONS:
  1872. return parseDirectiveCFISections();
  1873. case DK_CFI_STARTPROC:
  1874. return parseDirectiveCFIStartProc();
  1875. case DK_CFI_ENDPROC:
  1876. return parseDirectiveCFIEndProc();
  1877. case DK_CFI_DEF_CFA:
  1878. return parseDirectiveCFIDefCfa(IDLoc);
  1879. case DK_CFI_DEF_CFA_OFFSET:
  1880. return parseDirectiveCFIDefCfaOffset();
  1881. case DK_CFI_ADJUST_CFA_OFFSET:
  1882. return parseDirectiveCFIAdjustCfaOffset();
  1883. case DK_CFI_DEF_CFA_REGISTER:
  1884. return parseDirectiveCFIDefCfaRegister(IDLoc);
  1885. case DK_CFI_OFFSET:
  1886. return parseDirectiveCFIOffset(IDLoc);
  1887. case DK_CFI_REL_OFFSET:
  1888. return parseDirectiveCFIRelOffset(IDLoc);
  1889. case DK_CFI_PERSONALITY:
  1890. return parseDirectiveCFIPersonalityOrLsda(true);
  1891. case DK_CFI_LSDA:
  1892. return parseDirectiveCFIPersonalityOrLsda(false);
  1893. case DK_CFI_REMEMBER_STATE:
  1894. return parseDirectiveCFIRememberState();
  1895. case DK_CFI_RESTORE_STATE:
  1896. return parseDirectiveCFIRestoreState();
  1897. case DK_CFI_SAME_VALUE:
  1898. return parseDirectiveCFISameValue(IDLoc);
  1899. case DK_CFI_RESTORE:
  1900. return parseDirectiveCFIRestore(IDLoc);
  1901. case DK_CFI_ESCAPE:
  1902. return parseDirectiveCFIEscape();
  1903. case DK_CFI_RETURN_COLUMN:
  1904. return parseDirectiveCFIReturnColumn(IDLoc);
  1905. case DK_CFI_SIGNAL_FRAME:
  1906. return parseDirectiveCFISignalFrame();
  1907. case DK_CFI_UNDEFINED:
  1908. return parseDirectiveCFIUndefined(IDLoc);
  1909. case DK_CFI_REGISTER:
  1910. return parseDirectiveCFIRegister(IDLoc);
  1911. case DK_CFI_WINDOW_SAVE:
  1912. return parseDirectiveCFIWindowSave();
  1913. case DK_MACROS_ON:
  1914. case DK_MACROS_OFF:
  1915. return parseDirectiveMacrosOnOff(IDVal);
  1916. case DK_MACRO:
  1917. return parseDirectiveMacro(IDLoc);
  1918. case DK_ALTMACRO:
  1919. case DK_NOALTMACRO:
  1920. return parseDirectiveAltmacro(IDVal);
  1921. case DK_EXITM:
  1922. return parseDirectiveExitMacro(IDVal);
  1923. case DK_ENDM:
  1924. case DK_ENDMACRO:
  1925. return parseDirectiveEndMacro(IDVal);
  1926. case DK_PURGEM:
  1927. return parseDirectivePurgeMacro(IDLoc);
  1928. case DK_END:
  1929. return parseDirectiveEnd(IDLoc);
  1930. case DK_ERR:
  1931. return parseDirectiveError(IDLoc, false);
  1932. case DK_ERROR:
  1933. return parseDirectiveError(IDLoc, true);
  1934. case DK_WARNING:
  1935. return parseDirectiveWarning(IDLoc);
  1936. case DK_RELOC:
  1937. return parseDirectiveReloc(IDLoc);
  1938. case DK_DCB:
  1939. case DK_DCB_W:
  1940. return parseDirectiveDCB(IDVal, 2);
  1941. case DK_DCB_B:
  1942. return parseDirectiveDCB(IDVal, 1);
  1943. case DK_DCB_D:
  1944. return parseDirectiveRealDCB(IDVal, APFloat::IEEEdouble());
  1945. case DK_DCB_L:
  1946. return parseDirectiveDCB(IDVal, 4);
  1947. case DK_DCB_S:
  1948. return parseDirectiveRealDCB(IDVal, APFloat::IEEEsingle());
  1949. case DK_DC_X:
  1950. case DK_DCB_X:
  1951. return TokError(Twine(IDVal) +
  1952. " not currently supported for this target");
  1953. case DK_DS:
  1954. case DK_DS_W:
  1955. return parseDirectiveDS(IDVal, 2);
  1956. case DK_DS_B:
  1957. return parseDirectiveDS(IDVal, 1);
  1958. case DK_DS_D:
  1959. return parseDirectiveDS(IDVal, 8);
  1960. case DK_DS_L:
  1961. case DK_DS_S:
  1962. return parseDirectiveDS(IDVal, 4);
  1963. case DK_DS_P:
  1964. case DK_DS_X:
  1965. return parseDirectiveDS(IDVal, 12);
  1966. case DK_PRINT:
  1967. return parseDirectivePrint(IDLoc);
  1968. case DK_ADDRSIG:
  1969. return parseDirectiveAddrsig();
  1970. case DK_ADDRSIG_SYM:
  1971. return parseDirectiveAddrsigSym();
  1972. case DK_PSEUDO_PROBE:
  1973. return parseDirectivePseudoProbe();
  1974. }
  1975. return Error(IDLoc, "unknown directive");
  1976. }
  1977. // __asm _emit or __asm __emit
  1978. if (ParsingMSInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
  1979. IDVal == "_EMIT" || IDVal == "__EMIT"))
  1980. return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
  1981. // __asm align
  1982. if (ParsingMSInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
  1983. return parseDirectiveMSAlign(IDLoc, Info);
  1984. if (ParsingMSInlineAsm && (IDVal == "even" || IDVal == "EVEN"))
  1985. Info.AsmRewrites->emplace_back(AOK_EVEN, IDLoc, 4);
  1986. if (checkForValidSection())
  1987. return true;
  1988. // Canonicalize the opcode to lower case.
  1989. std::string OpcodeStr = IDVal.lower();
  1990. ParseInstructionInfo IInfo(Info.AsmRewrites);
  1991. bool ParseHadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, ID,
  1992. Info.ParsedOperands);
  1993. Info.ParseError = ParseHadError;
  1994. // Dump the parsed representation, if requested.
  1995. if (getShowParsedOperands()) {
  1996. SmallString<256> Str;
  1997. raw_svector_ostream OS(Str);
  1998. OS << "parsed instruction: [";
  1999. for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
  2000. if (i != 0)
  2001. OS << ", ";
  2002. Info.ParsedOperands[i]->print(OS);
  2003. }
  2004. OS << "]";
  2005. printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
  2006. }
  2007. // Fail even if ParseInstruction erroneously returns false.
  2008. if (hasPendingError() || ParseHadError)
  2009. return true;
  2010. // If we are generating dwarf for the current section then generate a .loc
  2011. // directive for the instruction.
  2012. if (!ParseHadError && enabledGenDwarfForAssembly() &&
  2013. getContext().getGenDwarfSectionSyms().count(
  2014. getStreamer().getCurrentSectionOnly())) {
  2015. unsigned Line;
  2016. if (ActiveMacros.empty())
  2017. Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
  2018. else
  2019. Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc,
  2020. ActiveMacros.front()->ExitBuffer);
  2021. // If we previously parsed a cpp hash file line comment then make sure the
  2022. // current Dwarf File is for the CppHashFilename if not then emit the
  2023. // Dwarf File table for it and adjust the line number for the .loc.
  2024. if (!CppHashInfo.Filename.empty()) {
  2025. unsigned FileNumber = getStreamer().emitDwarfFileDirective(
  2026. 0, StringRef(), CppHashInfo.Filename);
  2027. getContext().setGenDwarfFileNumber(FileNumber);
  2028. unsigned CppHashLocLineNo =
  2029. SrcMgr.FindLineNumber(CppHashInfo.Loc, CppHashInfo.Buf);
  2030. Line = CppHashInfo.LineNumber - 1 + (Line - CppHashLocLineNo);
  2031. }
  2032. getStreamer().emitDwarfLocDirective(
  2033. getContext().getGenDwarfFileNumber(), Line, 0,
  2034. DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
  2035. StringRef());
  2036. }
  2037. // If parsing succeeded, match the instruction.
  2038. if (!ParseHadError) {
  2039. uint64_t ErrorInfo;
  2040. if (getTargetParser().MatchAndEmitInstruction(
  2041. IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo,
  2042. getTargetParser().isParsingMSInlineAsm()))
  2043. return true;
  2044. }
  2045. return false;
  2046. }
  2047. // Parse and erase curly braces marking block start/end
  2048. bool
  2049. AsmParser::parseCurlyBlockScope(SmallVectorImpl<AsmRewrite> &AsmStrRewrites) {
  2050. // Identify curly brace marking block start/end
  2051. if (Lexer.isNot(AsmToken::LCurly) && Lexer.isNot(AsmToken::RCurly))
  2052. return false;
  2053. SMLoc StartLoc = Lexer.getLoc();
  2054. Lex(); // Eat the brace
  2055. if (Lexer.is(AsmToken::EndOfStatement))
  2056. Lex(); // Eat EndOfStatement following the brace
  2057. // Erase the block start/end brace from the output asm string
  2058. AsmStrRewrites.emplace_back(AOK_Skip, StartLoc, Lexer.getLoc().getPointer() -
  2059. StartLoc.getPointer());
  2060. return true;
  2061. }
  2062. /// parseCppHashLineFilenameComment as this:
  2063. /// ::= # number "filename"
  2064. bool AsmParser::parseCppHashLineFilenameComment(SMLoc L, bool SaveLocInfo) {
  2065. Lex(); // Eat the hash token.
  2066. // Lexer only ever emits HashDirective if it fully formed if it's
  2067. // done the checking already so this is an internal error.
  2068. assert(getTok().is(AsmToken::Integer) &&
  2069. "Lexing Cpp line comment: Expected Integer");
  2070. int64_t LineNumber = getTok().getIntVal();
  2071. Lex();
  2072. assert(getTok().is(AsmToken::String) &&
  2073. "Lexing Cpp line comment: Expected String");
  2074. StringRef Filename = getTok().getString();
  2075. Lex();
  2076. if (!SaveLocInfo)
  2077. return false;
  2078. // Get rid of the enclosing quotes.
  2079. Filename = Filename.substr(1, Filename.size() - 2);
  2080. // Save the SMLoc, Filename and LineNumber for later use by diagnostics
  2081. // and possibly DWARF file info.
  2082. CppHashInfo.Loc = L;
  2083. CppHashInfo.Filename = Filename;
  2084. CppHashInfo.LineNumber = LineNumber;
  2085. CppHashInfo.Buf = CurBuffer;
  2086. if (FirstCppHashFilename.empty())
  2087. FirstCppHashFilename = Filename;
  2088. return false;
  2089. }
  2090. /// will use the last parsed cpp hash line filename comment
  2091. /// for the Filename and LineNo if any in the diagnostic.
  2092. void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
  2093. const AsmParser *Parser = static_cast<const AsmParser *>(Context);
  2094. raw_ostream &OS = errs();
  2095. const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
  2096. SMLoc DiagLoc = Diag.getLoc();
  2097. unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
  2098. unsigned CppHashBuf =
  2099. Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashInfo.Loc);
  2100. // Like SourceMgr::printMessage() we need to print the include stack if any
  2101. // before printing the message.
  2102. unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
  2103. if (!Parser->SavedDiagHandler && DiagCurBuffer &&
  2104. DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
  2105. SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
  2106. DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
  2107. }
  2108. // If we have not parsed a cpp hash line filename comment or the source
  2109. // manager changed or buffer changed (like in a nested include) then just
  2110. // print the normal diagnostic using its Filename and LineNo.
  2111. if (!Parser->CppHashInfo.LineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
  2112. DiagBuf != CppHashBuf) {
  2113. if (Parser->SavedDiagHandler)
  2114. Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
  2115. else
  2116. Diag.print(nullptr, OS);
  2117. return;
  2118. }
  2119. // Use the CppHashFilename and calculate a line number based on the
  2120. // CppHashInfo.Loc and CppHashInfo.LineNumber relative to this Diag's SMLoc
  2121. // for the diagnostic.
  2122. const std::string &Filename = std::string(Parser->CppHashInfo.Filename);
  2123. int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
  2124. int CppHashLocLineNo =
  2125. Parser->SrcMgr.FindLineNumber(Parser->CppHashInfo.Loc, CppHashBuf);
  2126. int LineNo =
  2127. Parser->CppHashInfo.LineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
  2128. SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
  2129. Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
  2130. Diag.getLineContents(), Diag.getRanges());
  2131. if (Parser->SavedDiagHandler)
  2132. Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
  2133. else
  2134. NewDiag.print(nullptr, OS);
  2135. }
  2136. // FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
  2137. // difference being that that function accepts '@' as part of identifiers and
  2138. // we can't do that. AsmLexer.cpp should probably be changed to handle
  2139. // '@' as a special case when needed.
  2140. static bool isIdentifierChar(char c) {
  2141. return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
  2142. c == '.';
  2143. }
  2144. bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
  2145. ArrayRef<MCAsmMacroParameter> Parameters,
  2146. ArrayRef<MCAsmMacroArgument> A,
  2147. bool EnableAtPseudoVariable, SMLoc L) {
  2148. unsigned NParameters = Parameters.size();
  2149. bool HasVararg = NParameters ? Parameters.back().Vararg : false;
  2150. if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
  2151. return Error(L, "Wrong number of arguments");
  2152. // A macro without parameters is handled differently on Darwin:
  2153. // gas accepts no arguments and does no substitutions
  2154. while (!Body.empty()) {
  2155. // Scan for the next substitution.
  2156. std::size_t End = Body.size(), Pos = 0;
  2157. for (; Pos != End; ++Pos) {
  2158. // Check for a substitution or escape.
  2159. if (IsDarwin && !NParameters) {
  2160. // This macro has no parameters, look for $0, $1, etc.
  2161. if (Body[Pos] != '$' || Pos + 1 == End)
  2162. continue;
  2163. char Next = Body[Pos + 1];
  2164. if (Next == '$' || Next == 'n' ||
  2165. isdigit(static_cast<unsigned char>(Next)))
  2166. break;
  2167. } else {
  2168. // This macro has parameters, look for \foo, \bar, etc.
  2169. if (Body[Pos] == '\\' && Pos + 1 != End)
  2170. break;
  2171. }
  2172. }
  2173. // Add the prefix.
  2174. OS << Body.slice(0, Pos);
  2175. // Check if we reached the end.
  2176. if (Pos == End)
  2177. break;
  2178. if (IsDarwin && !NParameters) {
  2179. switch (Body[Pos + 1]) {
  2180. // $$ => $
  2181. case '$':
  2182. OS << '$';
  2183. break;
  2184. // $n => number of arguments
  2185. case 'n':
  2186. OS << A.size();
  2187. break;
  2188. // $[0-9] => argument
  2189. default: {
  2190. // Missing arguments are ignored.
  2191. unsigned Index = Body[Pos + 1] - '0';
  2192. if (Index >= A.size())
  2193. break;
  2194. // Otherwise substitute with the token values, with spaces eliminated.
  2195. for (const AsmToken &Token : A[Index])
  2196. OS << Token.getString();
  2197. break;
  2198. }
  2199. }
  2200. Pos += 2;
  2201. } else {
  2202. unsigned I = Pos + 1;
  2203. // Check for the \@ pseudo-variable.
  2204. if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End)
  2205. ++I;
  2206. else
  2207. while (isIdentifierChar(Body[I]) && I + 1 != End)
  2208. ++I;
  2209. const char *Begin = Body.data() + Pos + 1;
  2210. StringRef Argument(Begin, I - (Pos + 1));
  2211. unsigned Index = 0;
  2212. if (Argument == "@") {
  2213. OS << NumOfMacroInstantiations;
  2214. Pos += 2;
  2215. } else {
  2216. for (; Index < NParameters; ++Index)
  2217. if (Parameters[Index].Name == Argument)
  2218. break;
  2219. if (Index == NParameters) {
  2220. if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
  2221. Pos += 3;
  2222. else {
  2223. OS << '\\' << Argument;
  2224. Pos = I;
  2225. }
  2226. } else {
  2227. bool VarargParameter = HasVararg && Index == (NParameters - 1);
  2228. for (const AsmToken &Token : A[Index])
  2229. // For altmacro mode, you can write '%expr'.
  2230. // The prefix '%' evaluates the expression 'expr'
  2231. // and uses the result as a string (e.g. replace %(1+2) with the
  2232. // string "3").
  2233. // Here, we identify the integer token which is the result of the
  2234. // absolute expression evaluation and replace it with its string
  2235. // representation.
  2236. if (AltMacroMode && Token.getString().front() == '%' &&
  2237. Token.is(AsmToken::Integer))
  2238. // Emit an integer value to the buffer.
  2239. OS << Token.getIntVal();
  2240. // Only Token that was validated as a string and begins with '<'
  2241. // is considered altMacroString!!!
  2242. else if (AltMacroMode && Token.getString().front() == '<' &&
  2243. Token.is(AsmToken::String)) {
  2244. OS << angleBracketString(Token.getStringContents());
  2245. }
  2246. // We expect no quotes around the string's contents when
  2247. // parsing for varargs.
  2248. else if (Token.isNot(AsmToken::String) || VarargParameter)
  2249. OS << Token.getString();
  2250. else
  2251. OS << Token.getStringContents();
  2252. Pos += 1 + Argument.size();
  2253. }
  2254. }
  2255. }
  2256. // Update the scan point.
  2257. Body = Body.substr(Pos);
  2258. }
  2259. return false;
  2260. }
  2261. static bool isOperator(AsmToken::TokenKind kind) {
  2262. switch (kind) {
  2263. default:
  2264. return false;
  2265. case AsmToken::Plus:
  2266. case AsmToken::Minus:
  2267. case AsmToken::Tilde:
  2268. case AsmToken::Slash:
  2269. case AsmToken::Star:
  2270. case AsmToken::Dot:
  2271. case AsmToken::Equal:
  2272. case AsmToken::EqualEqual:
  2273. case AsmToken::Pipe:
  2274. case AsmToken::PipePipe:
  2275. case AsmToken::Caret:
  2276. case AsmToken::Amp:
  2277. case AsmToken::AmpAmp:
  2278. case AsmToken::Exclaim:
  2279. case AsmToken::ExclaimEqual:
  2280. case AsmToken::Less:
  2281. case AsmToken::LessEqual:
  2282. case AsmToken::LessLess:
  2283. case AsmToken::LessGreater:
  2284. case AsmToken::Greater:
  2285. case AsmToken::GreaterEqual:
  2286. case AsmToken::GreaterGreater:
  2287. return true;
  2288. }
  2289. }
  2290. namespace {
  2291. class AsmLexerSkipSpaceRAII {
  2292. public:
  2293. AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
  2294. Lexer.setSkipSpace(SkipSpace);
  2295. }
  2296. ~AsmLexerSkipSpaceRAII() {
  2297. Lexer.setSkipSpace(true);
  2298. }
  2299. private:
  2300. AsmLexer &Lexer;
  2301. };
  2302. } // end anonymous namespace
  2303. bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
  2304. if (Vararg) {
  2305. if (Lexer.isNot(AsmToken::EndOfStatement)) {
  2306. StringRef Str = parseStringToEndOfStatement();
  2307. MA.emplace_back(AsmToken::String, Str);
  2308. }
  2309. return false;
  2310. }
  2311. unsigned ParenLevel = 0;
  2312. // Darwin doesn't use spaces to delmit arguments.
  2313. AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
  2314. bool SpaceEaten;
  2315. while (true) {
  2316. SpaceEaten = false;
  2317. if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
  2318. return TokError("unexpected token in macro instantiation");
  2319. if (ParenLevel == 0) {
  2320. if (Lexer.is(AsmToken::Comma))
  2321. break;
  2322. if (Lexer.is(AsmToken::Space)) {
  2323. SpaceEaten = true;
  2324. Lexer.Lex(); // Eat spaces
  2325. }
  2326. // Spaces can delimit parameters, but could also be part an expression.
  2327. // If the token after a space is an operator, add the token and the next
  2328. // one into this argument
  2329. if (!IsDarwin) {
  2330. if (isOperator(Lexer.getKind())) {
  2331. MA.push_back(getTok());
  2332. Lexer.Lex();
  2333. // Whitespace after an operator can be ignored.
  2334. if (Lexer.is(AsmToken::Space))
  2335. Lexer.Lex();
  2336. continue;
  2337. }
  2338. }
  2339. if (SpaceEaten)
  2340. break;
  2341. }
  2342. // handleMacroEntry relies on not advancing the lexer here
  2343. // to be able to fill in the remaining default parameter values
  2344. if (Lexer.is(AsmToken::EndOfStatement))
  2345. break;
  2346. // Adjust the current parentheses level.
  2347. if (Lexer.is(AsmToken::LParen))
  2348. ++ParenLevel;
  2349. else if (Lexer.is(AsmToken::RParen) && ParenLevel)
  2350. --ParenLevel;
  2351. // Append the token to the current argument list.
  2352. MA.push_back(getTok());
  2353. Lexer.Lex();
  2354. }
  2355. if (ParenLevel != 0)
  2356. return TokError("unbalanced parentheses in macro argument");
  2357. return false;
  2358. }
  2359. // Parse the macro instantiation arguments.
  2360. bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
  2361. MCAsmMacroArguments &A) {
  2362. const unsigned NParameters = M ? M->Parameters.size() : 0;
  2363. bool NamedParametersFound = false;
  2364. SmallVector<SMLoc, 4> FALocs;
  2365. A.resize(NParameters);
  2366. FALocs.resize(NParameters);
  2367. // Parse two kinds of macro invocations:
  2368. // - macros defined without any parameters accept an arbitrary number of them
  2369. // - macros defined with parameters accept at most that many of them
  2370. bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
  2371. for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
  2372. ++Parameter) {
  2373. SMLoc IDLoc = Lexer.getLoc();
  2374. MCAsmMacroParameter FA;
  2375. if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
  2376. if (parseIdentifier(FA.Name))
  2377. return Error(IDLoc, "invalid argument identifier for formal argument");
  2378. if (Lexer.isNot(AsmToken::Equal))
  2379. return TokError("expected '=' after formal parameter identifier");
  2380. Lex();
  2381. NamedParametersFound = true;
  2382. }
  2383. bool Vararg = HasVararg && Parameter == (NParameters - 1);
  2384. if (NamedParametersFound && FA.Name.empty())
  2385. return Error(IDLoc, "cannot mix positional and keyword arguments");
  2386. SMLoc StrLoc = Lexer.getLoc();
  2387. SMLoc EndLoc;
  2388. if (AltMacroMode && Lexer.is(AsmToken::Percent)) {
  2389. const MCExpr *AbsoluteExp;
  2390. int64_t Value;
  2391. /// Eat '%'
  2392. Lex();
  2393. if (parseExpression(AbsoluteExp, EndLoc))
  2394. return false;
  2395. if (!AbsoluteExp->evaluateAsAbsolute(Value,
  2396. getStreamer().getAssemblerPtr()))
  2397. return Error(StrLoc, "expected absolute expression");
  2398. const char *StrChar = StrLoc.getPointer();
  2399. const char *EndChar = EndLoc.getPointer();
  2400. AsmToken newToken(AsmToken::Integer,
  2401. StringRef(StrChar, EndChar - StrChar), Value);
  2402. FA.Value.push_back(newToken);
  2403. } else if (AltMacroMode && Lexer.is(AsmToken::Less) &&
  2404. isAngleBracketString(StrLoc, EndLoc)) {
  2405. const char *StrChar = StrLoc.getPointer();
  2406. const char *EndChar = EndLoc.getPointer();
  2407. jumpToLoc(EndLoc, CurBuffer);
  2408. /// Eat from '<' to '>'
  2409. Lex();
  2410. AsmToken newToken(AsmToken::String,
  2411. StringRef(StrChar, EndChar - StrChar));
  2412. FA.Value.push_back(newToken);
  2413. } else if(parseMacroArgument(FA.Value, Vararg))
  2414. return true;
  2415. unsigned PI = Parameter;
  2416. if (!FA.Name.empty()) {
  2417. unsigned FAI = 0;
  2418. for (FAI = 0; FAI < NParameters; ++FAI)
  2419. if (M->Parameters[FAI].Name == FA.Name)
  2420. break;
  2421. if (FAI >= NParameters) {
  2422. assert(M && "expected macro to be defined");
  2423. return Error(IDLoc, "parameter named '" + FA.Name +
  2424. "' does not exist for macro '" + M->Name + "'");
  2425. }
  2426. PI = FAI;
  2427. }
  2428. if (!FA.Value.empty()) {
  2429. if (A.size() <= PI)
  2430. A.resize(PI + 1);
  2431. A[PI] = FA.Value;
  2432. if (FALocs.size() <= PI)
  2433. FALocs.resize(PI + 1);
  2434. FALocs[PI] = Lexer.getLoc();
  2435. }
  2436. // At the end of the statement, fill in remaining arguments that have
  2437. // default values. If there aren't any, then the next argument is
  2438. // required but missing
  2439. if (Lexer.is(AsmToken::EndOfStatement)) {
  2440. bool Failure = false;
  2441. for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
  2442. if (A[FAI].empty()) {
  2443. if (M->Parameters[FAI].Required) {
  2444. Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
  2445. "missing value for required parameter "
  2446. "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
  2447. Failure = true;
  2448. }
  2449. if (!M->Parameters[FAI].Value.empty())
  2450. A[FAI] = M->Parameters[FAI].Value;
  2451. }
  2452. }
  2453. return Failure;
  2454. }
  2455. if (Lexer.is(AsmToken::Comma))
  2456. Lex();
  2457. }
  2458. return TokError("too many positional arguments");
  2459. }
  2460. bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
  2461. // Arbitrarily limit macro nesting depth (default matches 'as'). We can
  2462. // eliminate this, although we should protect against infinite loops.
  2463. unsigned MaxNestingDepth = AsmMacroMaxNestingDepth;
  2464. if (ActiveMacros.size() == MaxNestingDepth) {
  2465. std::ostringstream MaxNestingDepthError;
  2466. MaxNestingDepthError << "macros cannot be nested more than "
  2467. << MaxNestingDepth << " levels deep."
  2468. << " Use -asm-macro-max-nesting-depth to increase "
  2469. "this limit.";
  2470. return TokError(MaxNestingDepthError.str());
  2471. }
  2472. MCAsmMacroArguments A;
  2473. if (parseMacroArguments(M, A))
  2474. return true;
  2475. // Macro instantiation is lexical, unfortunately. We construct a new buffer
  2476. // to hold the macro body with substitutions.
  2477. SmallString<256> Buf;
  2478. StringRef Body = M->Body;
  2479. raw_svector_ostream OS(Buf);
  2480. if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc()))
  2481. return true;
  2482. // We include the .endmacro in the buffer as our cue to exit the macro
  2483. // instantiation.
  2484. OS << ".endmacro\n";
  2485. std::unique_ptr<MemoryBuffer> Instantiation =
  2486. MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
  2487. // Create the macro instantiation object and add to the current macro
  2488. // instantiation stack.
  2489. MacroInstantiation *MI = new MacroInstantiation{
  2490. NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()};
  2491. ActiveMacros.push_back(MI);
  2492. ++NumOfMacroInstantiations;
  2493. // Jump to the macro instantiation and prime the lexer.
  2494. CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
  2495. Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
  2496. Lex();
  2497. return false;
  2498. }
  2499. void AsmParser::handleMacroExit() {
  2500. // Jump to the EndOfStatement we should return to, and consume it.
  2501. jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
  2502. Lex();
  2503. // Pop the instantiation entry.
  2504. delete ActiveMacros.back();
  2505. ActiveMacros.pop_back();
  2506. }
  2507. bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
  2508. bool NoDeadStrip) {
  2509. MCSymbol *Sym;
  2510. const MCExpr *Value;
  2511. if (MCParserUtils::parseAssignmentExpression(Name, allow_redef, *this, Sym,
  2512. Value))
  2513. return true;
  2514. if (!Sym) {
  2515. // In the case where we parse an expression starting with a '.', we will
  2516. // not generate an error, nor will we create a symbol. In this case we
  2517. // should just return out.
  2518. return false;
  2519. }
  2520. // Do the assignment.
  2521. Out.emitAssignment(Sym, Value);
  2522. if (NoDeadStrip)
  2523. Out.emitSymbolAttribute(Sym, MCSA_NoDeadStrip);
  2524. return false;
  2525. }
  2526. /// parseIdentifier:
  2527. /// ::= identifier
  2528. /// ::= string
  2529. bool AsmParser::parseIdentifier(StringRef &Res) {
  2530. // The assembler has relaxed rules for accepting identifiers, in particular we
  2531. // allow things like '.globl $foo' and '.def @feat.00', which would normally be
  2532. // separate tokens. At this level, we have already lexed so we cannot (currently)
  2533. // handle this as a context dependent token, instead we detect adjacent tokens
  2534. // and return the combined identifier.
  2535. if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
  2536. SMLoc PrefixLoc = getLexer().getLoc();
  2537. // Consume the prefix character, and check for a following identifier.
  2538. AsmToken Buf[1];
  2539. Lexer.peekTokens(Buf, false);
  2540. if (Buf[0].isNot(AsmToken::Identifier) && Buf[0].isNot(AsmToken::Integer))
  2541. return true;
  2542. // We have a '$' or '@' followed by an identifier or integer token, make
  2543. // sure they are adjacent.
  2544. if (PrefixLoc.getPointer() + 1 != Buf[0].getLoc().getPointer())
  2545. return true;
  2546. // eat $ or @
  2547. Lexer.Lex(); // Lexer's Lex guarantees consecutive token.
  2548. // Construct the joined identifier and consume the token.
  2549. Res = StringRef(PrefixLoc.getPointer(), getTok().getString().size() + 1);
  2550. Lex(); // Parser Lex to maintain invariants.
  2551. return false;
  2552. }
  2553. if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
  2554. return true;
  2555. Res = getTok().getIdentifier();
  2556. Lex(); // Consume the identifier token.
  2557. return false;
  2558. }
  2559. /// parseDirectiveSet:
  2560. /// ::= .equ identifier ',' expression
  2561. /// ::= .equiv identifier ',' expression
  2562. /// ::= .set identifier ',' expression
  2563. bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
  2564. StringRef Name;
  2565. if (check(parseIdentifier(Name), "expected identifier") ||
  2566. parseToken(AsmToken::Comma) || parseAssignment(Name, allow_redef, true))
  2567. return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
  2568. return false;
  2569. }
  2570. bool AsmParser::parseEscapedString(std::string &Data) {
  2571. if (check(getTok().isNot(AsmToken::String), "expected string"))
  2572. return true;
  2573. Data = "";
  2574. StringRef Str = getTok().getStringContents();
  2575. for (unsigned i = 0, e = Str.size(); i != e; ++i) {
  2576. if (Str[i] != '\\') {
  2577. Data += Str[i];
  2578. continue;
  2579. }
  2580. // Recognize escaped characters. Note that this escape semantics currently
  2581. // loosely follows Darwin 'as'.
  2582. ++i;
  2583. if (i == e)
  2584. return TokError("unexpected backslash at end of string");
  2585. // Recognize hex sequences similarly to GNU 'as'.
  2586. if (Str[i] == 'x' || Str[i] == 'X') {
  2587. size_t length = Str.size();
  2588. if (i + 1 >= length || !isHexDigit(Str[i + 1]))
  2589. return TokError("invalid hexadecimal escape sequence");
  2590. // Consume hex characters. GNU 'as' reads all hexadecimal characters and
  2591. // then truncates to the lower 16 bits. Seems reasonable.
  2592. unsigned Value = 0;
  2593. while (i + 1 < length && isHexDigit(Str[i + 1]))
  2594. Value = Value * 16 + hexDigitValue(Str[++i]);
  2595. Data += (unsigned char)(Value & 0xFF);
  2596. continue;
  2597. }
  2598. // Recognize octal sequences.
  2599. if ((unsigned)(Str[i] - '0') <= 7) {
  2600. // Consume up to three octal characters.
  2601. unsigned Value = Str[i] - '0';
  2602. if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
  2603. ++i;
  2604. Value = Value * 8 + (Str[i] - '0');
  2605. if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
  2606. ++i;
  2607. Value = Value * 8 + (Str[i] - '0');
  2608. }
  2609. }
  2610. if (Value > 255)
  2611. return TokError("invalid octal escape sequence (out of range)");
  2612. Data += (unsigned char)Value;
  2613. continue;
  2614. }
  2615. // Otherwise recognize individual escapes.
  2616. switch (Str[i]) {
  2617. default:
  2618. // Just reject invalid escape sequences for now.
  2619. return TokError("invalid escape sequence (unrecognized character)");
  2620. case 'b': Data += '\b'; break;
  2621. case 'f': Data += '\f'; break;
  2622. case 'n': Data += '\n'; break;
  2623. case 'r': Data += '\r'; break;
  2624. case 't': Data += '\t'; break;
  2625. case '"': Data += '"'; break;
  2626. case '\\': Data += '\\'; break;
  2627. }
  2628. }
  2629. Lex();
  2630. return false;
  2631. }
  2632. bool AsmParser::parseAngleBracketString(std::string &Data) {
  2633. SMLoc EndLoc, StartLoc = getTok().getLoc();
  2634. if (isAngleBracketString(StartLoc, EndLoc)) {
  2635. const char *StartChar = StartLoc.getPointer() + 1;
  2636. const char *EndChar = EndLoc.getPointer() - 1;
  2637. jumpToLoc(EndLoc, CurBuffer);
  2638. /// Eat from '<' to '>'
  2639. Lex();
  2640. Data = angleBracketString(StringRef(StartChar, EndChar - StartChar));
  2641. return false;
  2642. }
  2643. return true;
  2644. }
  2645. /// parseDirectiveAscii:
  2646. // ::= .ascii [ "string"+ ( , "string"+ )* ]
  2647. /// ::= ( .asciz | .string ) [ "string" ( , "string" )* ]
  2648. bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
  2649. auto parseOp = [&]() -> bool {
  2650. std::string Data;
  2651. if (checkForValidSection())
  2652. return true;
  2653. // Only support spaces as separators for .ascii directive for now. See the
  2654. // discusssion at https://reviews.llvm.org/D91460 for more details.
  2655. do {
  2656. if (parseEscapedString(Data))
  2657. return true;
  2658. getStreamer().emitBytes(Data);
  2659. } while (!ZeroTerminated && getTok().is(AsmToken::String));
  2660. if (ZeroTerminated)
  2661. getStreamer().emitBytes(StringRef("\0", 1));
  2662. return false;
  2663. };
  2664. if (parseMany(parseOp))
  2665. return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
  2666. return false;
  2667. }
  2668. /// parseDirectiveReloc
  2669. /// ::= .reloc expression , identifier [ , expression ]
  2670. bool AsmParser::parseDirectiveReloc(SMLoc DirectiveLoc) {
  2671. const MCExpr *Offset;
  2672. const MCExpr *Expr = nullptr;
  2673. SMLoc OffsetLoc = Lexer.getTok().getLoc();
  2674. if (parseExpression(Offset))
  2675. return true;
  2676. if (parseToken(AsmToken::Comma, "expected comma") ||
  2677. check(getTok().isNot(AsmToken::Identifier), "expected relocation name"))
  2678. return true;
  2679. SMLoc NameLoc = Lexer.getTok().getLoc();
  2680. StringRef Name = Lexer.getTok().getIdentifier();
  2681. Lex();
  2682. if (Lexer.is(AsmToken::Comma)) {
  2683. Lex();
  2684. SMLoc ExprLoc = Lexer.getLoc();
  2685. if (parseExpression(Expr))
  2686. return true;
  2687. MCValue Value;
  2688. if (!Expr->evaluateAsRelocatable(Value, nullptr, nullptr))
  2689. return Error(ExprLoc, "expression must be relocatable");
  2690. }
  2691. if (parseToken(AsmToken::EndOfStatement,
  2692. "unexpected token in .reloc directive"))
  2693. return true;
  2694. const MCTargetAsmParser &MCT = getTargetParser();
  2695. const MCSubtargetInfo &STI = MCT.getSTI();
  2696. if (Optional<std::pair<bool, std::string>> Err =
  2697. getStreamer().emitRelocDirective(*Offset, Name, Expr, DirectiveLoc,
  2698. STI))
  2699. return Error(Err->first ? NameLoc : OffsetLoc, Err->second);
  2700. return false;
  2701. }
  2702. /// parseDirectiveValue
  2703. /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
  2704. bool AsmParser::parseDirectiveValue(StringRef IDVal, unsigned Size) {
  2705. auto parseOp = [&]() -> bool {
  2706. const MCExpr *Value;
  2707. SMLoc ExprLoc = getLexer().getLoc();
  2708. if (checkForValidSection() || parseExpression(Value))
  2709. return true;
  2710. // Special case constant expressions to match code generator.
  2711. if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
  2712. assert(Size <= 8 && "Invalid size");
  2713. uint64_t IntValue = MCE->getValue();
  2714. if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
  2715. return Error(ExprLoc, "out of range literal value");
  2716. getStreamer().emitIntValue(IntValue, Size);
  2717. } else
  2718. getStreamer().emitValue(Value, Size, ExprLoc);
  2719. return false;
  2720. };
  2721. if (parseMany(parseOp))
  2722. return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
  2723. return false;
  2724. }
  2725. static bool parseHexOcta(AsmParser &Asm, uint64_t &hi, uint64_t &lo) {
  2726. if (Asm.getTok().isNot(AsmToken::Integer) &&
  2727. Asm.getTok().isNot(AsmToken::BigNum))
  2728. return Asm.TokError("unknown token in expression");
  2729. SMLoc ExprLoc = Asm.getTok().getLoc();
  2730. APInt IntValue = Asm.getTok().getAPIntVal();
  2731. Asm.Lex();
  2732. if (!IntValue.isIntN(128))
  2733. return Asm.Error(ExprLoc, "out of range literal value");
  2734. if (!IntValue.isIntN(64)) {
  2735. hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
  2736. lo = IntValue.getLoBits(64).getZExtValue();
  2737. } else {
  2738. hi = 0;
  2739. lo = IntValue.getZExtValue();
  2740. }
  2741. return false;
  2742. }
  2743. /// ParseDirectiveOctaValue
  2744. /// ::= .octa [ hexconstant (, hexconstant)* ]
  2745. bool AsmParser::parseDirectiveOctaValue(StringRef IDVal) {
  2746. auto parseOp = [&]() -> bool {
  2747. if (checkForValidSection())
  2748. return true;
  2749. uint64_t hi, lo;
  2750. if (parseHexOcta(*this, hi, lo))
  2751. return true;
  2752. if (MAI.isLittleEndian()) {
  2753. getStreamer().emitInt64(lo);
  2754. getStreamer().emitInt64(hi);
  2755. } else {
  2756. getStreamer().emitInt64(hi);
  2757. getStreamer().emitInt64(lo);
  2758. }
  2759. return false;
  2760. };
  2761. if (parseMany(parseOp))
  2762. return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
  2763. return false;
  2764. }
  2765. bool AsmParser::parseRealValue(const fltSemantics &Semantics, APInt &Res) {
  2766. // We don't truly support arithmetic on floating point expressions, so we
  2767. // have to manually parse unary prefixes.
  2768. bool IsNeg = false;
  2769. if (getLexer().is(AsmToken::Minus)) {
  2770. Lexer.Lex();
  2771. IsNeg = true;
  2772. } else if (getLexer().is(AsmToken::Plus))
  2773. Lexer.Lex();
  2774. if (Lexer.is(AsmToken::Error))
  2775. return TokError(Lexer.getErr());
  2776. if (Lexer.isNot(AsmToken::Integer) && Lexer.isNot(AsmToken::Real) &&
  2777. Lexer.isNot(AsmToken::Identifier))
  2778. return TokError("unexpected token in directive");
  2779. // Convert to an APFloat.
  2780. APFloat Value(Semantics);
  2781. StringRef IDVal = getTok().getString();
  2782. if (getLexer().is(AsmToken::Identifier)) {
  2783. if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
  2784. Value = APFloat::getInf(Semantics);
  2785. else if (!IDVal.compare_lower("nan"))
  2786. Value = APFloat::getNaN(Semantics, false, ~0);
  2787. else
  2788. return TokError("invalid floating point literal");
  2789. } else if (errorToBool(
  2790. Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven)
  2791. .takeError()))
  2792. return TokError("invalid floating point literal");
  2793. if (IsNeg)
  2794. Value.changeSign();
  2795. // Consume the numeric token.
  2796. Lex();
  2797. Res = Value.bitcastToAPInt();
  2798. return false;
  2799. }
  2800. /// parseDirectiveRealValue
  2801. /// ::= (.single | .double) [ expression (, expression)* ]
  2802. bool AsmParser::parseDirectiveRealValue(StringRef IDVal,
  2803. const fltSemantics &Semantics) {
  2804. auto parseOp = [&]() -> bool {
  2805. APInt AsInt;
  2806. if (checkForValidSection() || parseRealValue(Semantics, AsInt))
  2807. return true;
  2808. getStreamer().emitIntValue(AsInt.getLimitedValue(),
  2809. AsInt.getBitWidth() / 8);
  2810. return false;
  2811. };
  2812. if (parseMany(parseOp))
  2813. return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
  2814. return false;
  2815. }
  2816. /// parseDirectiveZero
  2817. /// ::= .zero expression
  2818. bool AsmParser::parseDirectiveZero() {
  2819. SMLoc NumBytesLoc = Lexer.getLoc();
  2820. const MCExpr *NumBytes;
  2821. if (checkForValidSection() || parseExpression(NumBytes))
  2822. return true;
  2823. int64_t Val = 0;
  2824. if (getLexer().is(AsmToken::Comma)) {
  2825. Lex();
  2826. if (parseAbsoluteExpression(Val))
  2827. return true;
  2828. }
  2829. if (parseToken(AsmToken::EndOfStatement,
  2830. "unexpected token in '.zero' directive"))
  2831. return true;
  2832. getStreamer().emitFill(*NumBytes, Val, NumBytesLoc);
  2833. return false;
  2834. }
  2835. /// parseDirectiveFill
  2836. /// ::= .fill expression [ , expression [ , expression ] ]
  2837. bool AsmParser::parseDirectiveFill() {
  2838. SMLoc NumValuesLoc = Lexer.getLoc();
  2839. const MCExpr *NumValues;
  2840. if (checkForValidSection() || parseExpression(NumValues))
  2841. return true;
  2842. int64_t FillSize = 1;
  2843. int64_t FillExpr = 0;
  2844. SMLoc SizeLoc, ExprLoc;
  2845. if (parseOptionalToken(AsmToken::Comma)) {
  2846. SizeLoc = getTok().getLoc();
  2847. if (parseAbsoluteExpression(FillSize))
  2848. return true;
  2849. if (parseOptionalToken(AsmToken::Comma)) {
  2850. ExprLoc = getTok().getLoc();
  2851. if (parseAbsoluteExpression(FillExpr))
  2852. return true;
  2853. }
  2854. }
  2855. if (parseToken(AsmToken::EndOfStatement,
  2856. "unexpected token in '.fill' directive"))
  2857. return true;
  2858. if (FillSize < 0) {
  2859. Warning(SizeLoc, "'.fill' directive with negative size has no effect");
  2860. return false;
  2861. }
  2862. if (FillSize > 8) {
  2863. Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
  2864. FillSize = 8;
  2865. }
  2866. if (!isUInt<32>(FillExpr) && FillSize > 4)
  2867. Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
  2868. getStreamer().emitFill(*NumValues, FillSize, FillExpr, NumValuesLoc);
  2869. return false;
  2870. }
  2871. /// parseDirectiveOrg
  2872. /// ::= .org expression [ , expression ]
  2873. bool AsmParser::parseDirectiveOrg() {
  2874. const MCExpr *Offset;
  2875. SMLoc OffsetLoc = Lexer.getLoc();
  2876. if (checkForValidSection() || parseExpression(Offset))
  2877. return true;
  2878. // Parse optional fill expression.
  2879. int64_t FillExpr = 0;
  2880. if (parseOptionalToken(AsmToken::Comma))
  2881. if (parseAbsoluteExpression(FillExpr))
  2882. return addErrorSuffix(" in '.org' directive");
  2883. if (parseToken(AsmToken::EndOfStatement))
  2884. return addErrorSuffix(" in '.org' directive");
  2885. getStreamer().emitValueToOffset(Offset, FillExpr, OffsetLoc);
  2886. return false;
  2887. }
  2888. /// parseDirectiveAlign
  2889. /// ::= {.align, ...} expression [ , expression [ , expression ]]
  2890. bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
  2891. SMLoc AlignmentLoc = getLexer().getLoc();
  2892. int64_t Alignment;
  2893. SMLoc MaxBytesLoc;
  2894. bool HasFillExpr = false;
  2895. int64_t FillExpr = 0;
  2896. int64_t MaxBytesToFill = 0;
  2897. auto parseAlign = [&]() -> bool {
  2898. if (parseAbsoluteExpression(Alignment))
  2899. return true;
  2900. if (parseOptionalToken(AsmToken::Comma)) {
  2901. // The fill expression can be omitted while specifying a maximum number of
  2902. // alignment bytes, e.g:
  2903. // .align 3,,4
  2904. if (getTok().isNot(AsmToken::Comma)) {
  2905. HasFillExpr = true;
  2906. if (parseAbsoluteExpression(FillExpr))
  2907. return true;
  2908. }
  2909. if (parseOptionalToken(AsmToken::Comma))
  2910. if (parseTokenLoc(MaxBytesLoc) ||
  2911. parseAbsoluteExpression(MaxBytesToFill))
  2912. return true;
  2913. }
  2914. return parseToken(AsmToken::EndOfStatement);
  2915. };
  2916. if (checkForValidSection())
  2917. return addErrorSuffix(" in directive");
  2918. // Ignore empty '.p2align' directives for GNU-as compatibility
  2919. if (IsPow2 && (ValueSize == 1) && getTok().is(AsmToken::EndOfStatement)) {
  2920. Warning(AlignmentLoc, "p2align directive with no operand(s) is ignored");
  2921. return parseToken(AsmToken::EndOfStatement);
  2922. }
  2923. if (parseAlign())
  2924. return addErrorSuffix(" in directive");
  2925. // Always emit an alignment here even if we thrown an error.
  2926. bool ReturnVal = false;
  2927. // Compute alignment in bytes.
  2928. if (IsPow2) {
  2929. // FIXME: Diagnose overflow.
  2930. if (Alignment >= 32) {
  2931. ReturnVal |= Error(AlignmentLoc, "invalid alignment value");
  2932. Alignment = 31;
  2933. }
  2934. Alignment = 1ULL << Alignment;
  2935. } else {
  2936. // Reject alignments that aren't either a power of two or zero,
  2937. // for gas compatibility. Alignment of zero is silently rounded
  2938. // up to one.
  2939. if (Alignment == 0)
  2940. Alignment = 1;
  2941. if (!isPowerOf2_64(Alignment))
  2942. ReturnVal |= Error(AlignmentLoc, "alignment must be a power of 2");
  2943. if (!isUInt<32>(Alignment))
  2944. ReturnVal |= Error(AlignmentLoc, "alignment must be smaller than 2**32");
  2945. }
  2946. // Diagnose non-sensical max bytes to align.
  2947. if (MaxBytesLoc.isValid()) {
  2948. if (MaxBytesToFill < 1) {
  2949. ReturnVal |= Error(MaxBytesLoc,
  2950. "alignment directive can never be satisfied in this "
  2951. "many bytes, ignoring maximum bytes expression");
  2952. MaxBytesToFill = 0;
  2953. }
  2954. if (MaxBytesToFill >= Alignment) {
  2955. Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
  2956. "has no effect");
  2957. MaxBytesToFill = 0;
  2958. }
  2959. }
  2960. // Check whether we should use optimal code alignment for this .align
  2961. // directive.
  2962. const MCSection *Section = getStreamer().getCurrentSectionOnly();
  2963. assert(Section && "must have section to emit alignment");
  2964. bool UseCodeAlign = Section->UseCodeAlign();
  2965. if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
  2966. ValueSize == 1 && UseCodeAlign) {
  2967. getStreamer().emitCodeAlignment(Alignment, MaxBytesToFill);
  2968. } else {
  2969. // FIXME: Target specific behavior about how the "extra" bytes are filled.
  2970. getStreamer().emitValueToAlignment(Alignment, FillExpr, ValueSize,
  2971. MaxBytesToFill);
  2972. }
  2973. return ReturnVal;
  2974. }
  2975. /// parseDirectiveFile
  2976. /// ::= .file filename
  2977. /// ::= .file number [directory] filename [md5 checksum] [source source-text]
  2978. bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
  2979. // FIXME: I'm not sure what this is.
  2980. int64_t FileNumber = -1;
  2981. if (getLexer().is(AsmToken::Integer)) {
  2982. FileNumber = getTok().getIntVal();
  2983. Lex();
  2984. if (FileNumber < 0)
  2985. return TokError("negative file number");
  2986. }
  2987. std::string Path;
  2988. // Usually the directory and filename together, otherwise just the directory.
  2989. // Allow the strings to have escaped octal character sequence.
  2990. if (check(getTok().isNot(AsmToken::String),
  2991. "unexpected token in '.file' directive") ||
  2992. parseEscapedString(Path))
  2993. return true;
  2994. StringRef Directory;
  2995. StringRef Filename;
  2996. std::string FilenameData;
  2997. if (getLexer().is(AsmToken::String)) {
  2998. if (check(FileNumber == -1,
  2999. "explicit path specified, but no file number") ||
  3000. parseEscapedString(FilenameData))
  3001. return true;
  3002. Filename = FilenameData;
  3003. Directory = Path;
  3004. } else {
  3005. Filename = Path;
  3006. }
  3007. uint64_t MD5Hi, MD5Lo;
  3008. bool HasMD5 = false;
  3009. Optional<StringRef> Source;
  3010. bool HasSource = false;
  3011. std::string SourceString;
  3012. while (!parseOptionalToken(AsmToken::EndOfStatement)) {
  3013. StringRef Keyword;
  3014. if (check(getTok().isNot(AsmToken::Identifier),
  3015. "unexpected token in '.file' directive") ||
  3016. parseIdentifier(Keyword))
  3017. return true;
  3018. if (Keyword == "md5") {
  3019. HasMD5 = true;
  3020. if (check(FileNumber == -1,
  3021. "MD5 checksum specified, but no file number") ||
  3022. parseHexOcta(*this, MD5Hi, MD5Lo))
  3023. return true;
  3024. } else if (Keyword == "source") {
  3025. HasSource = true;
  3026. if (check(FileNumber == -1,
  3027. "source specified, but no file number") ||
  3028. check(getTok().isNot(AsmToken::String),
  3029. "unexpected token in '.file' directive") ||
  3030. parseEscapedString(SourceString))
  3031. return true;
  3032. } else {
  3033. return TokError("unexpected token in '.file' directive");
  3034. }
  3035. }
  3036. if (FileNumber == -1) {
  3037. // Ignore the directive if there is no number and the target doesn't support
  3038. // numberless .file directives. This allows some portability of assembler
  3039. // between different object file formats.
  3040. if (getContext().getAsmInfo()->hasSingleParameterDotFile())
  3041. getStreamer().emitFileDirective(Filename);
  3042. } else {
  3043. // In case there is a -g option as well as debug info from directive .file,
  3044. // we turn off the -g option, directly use the existing debug info instead.
  3045. // Throw away any implicit file table for the assembler source.
  3046. if (Ctx.getGenDwarfForAssembly()) {
  3047. Ctx.getMCDwarfLineTable(0).resetFileTable();
  3048. Ctx.setGenDwarfForAssembly(false);
  3049. }
  3050. Optional<MD5::MD5Result> CKMem;
  3051. if (HasMD5) {
  3052. MD5::MD5Result Sum;
  3053. for (unsigned i = 0; i != 8; ++i) {
  3054. Sum.Bytes[i] = uint8_t(MD5Hi >> ((7 - i) * 8));
  3055. Sum.Bytes[i + 8] = uint8_t(MD5Lo >> ((7 - i) * 8));
  3056. }
  3057. CKMem = Sum;
  3058. }
  3059. if (HasSource) {
  3060. char *SourceBuf = static_cast<char *>(Ctx.allocate(SourceString.size()));
  3061. memcpy(SourceBuf, SourceString.data(), SourceString.size());
  3062. Source = StringRef(SourceBuf, SourceString.size());
  3063. }
  3064. if (FileNumber == 0) {
  3065. if (Ctx.getDwarfVersion() < 5)
  3066. return Warning(DirectiveLoc, "file 0 not supported prior to DWARF-5");
  3067. getStreamer().emitDwarfFile0Directive(Directory, Filename, CKMem, Source);
  3068. } else {
  3069. Expected<unsigned> FileNumOrErr = getStreamer().tryEmitDwarfFileDirective(
  3070. FileNumber, Directory, Filename, CKMem, Source);
  3071. if (!FileNumOrErr)
  3072. return Error(DirectiveLoc, toString(FileNumOrErr.takeError()));
  3073. }
  3074. // Alert the user if there are some .file directives with MD5 and some not.
  3075. // But only do that once.
  3076. if (!ReportedInconsistentMD5 && !Ctx.isDwarfMD5UsageConsistent(0)) {
  3077. ReportedInconsistentMD5 = true;
  3078. return Warning(DirectiveLoc, "inconsistent use of MD5 checksums");
  3079. }
  3080. }
  3081. return false;
  3082. }
  3083. /// parseDirectiveLine
  3084. /// ::= .line [number]
  3085. bool AsmParser::parseDirectiveLine() {
  3086. int64_t LineNumber;
  3087. if (getLexer().is(AsmToken::Integer)) {
  3088. if (parseIntToken(LineNumber, "unexpected token in '.line' directive"))
  3089. return true;
  3090. (void)LineNumber;
  3091. // FIXME: Do something with the .line.
  3092. }
  3093. if (parseToken(AsmToken::EndOfStatement,
  3094. "unexpected token in '.line' directive"))
  3095. return true;
  3096. return false;
  3097. }
  3098. /// parseDirectiveLoc
  3099. /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
  3100. /// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
  3101. /// The first number is a file number, must have been previously assigned with
  3102. /// a .file directive, the second number is the line number and optionally the
  3103. /// third number is a column position (zero if not specified). The remaining
  3104. /// optional items are .loc sub-directives.
  3105. bool AsmParser::parseDirectiveLoc() {
  3106. int64_t FileNumber = 0, LineNumber = 0;
  3107. SMLoc Loc = getTok().getLoc();
  3108. if (parseIntToken(FileNumber, "unexpected token in '.loc' directive") ||
  3109. check(FileNumber < 1 && Ctx.getDwarfVersion() < 5, Loc,
  3110. "file number less than one in '.loc' directive") ||
  3111. check(!getContext().isValidDwarfFileNumber(FileNumber), Loc,
  3112. "unassigned file number in '.loc' directive"))
  3113. return true;
  3114. // optional
  3115. if (getLexer().is(AsmToken::Integer)) {
  3116. LineNumber = getTok().getIntVal();
  3117. if (LineNumber < 0)
  3118. return TokError("line number less than zero in '.loc' directive");
  3119. Lex();
  3120. }
  3121. int64_t ColumnPos = 0;
  3122. if (getLexer().is(AsmToken::Integer)) {
  3123. ColumnPos = getTok().getIntVal();
  3124. if (ColumnPos < 0)
  3125. return TokError("column position less than zero in '.loc' directive");
  3126. Lex();
  3127. }
  3128. auto PrevFlags = getContext().getCurrentDwarfLoc().getFlags();
  3129. unsigned Flags = PrevFlags & DWARF2_FLAG_IS_STMT;
  3130. unsigned Isa = 0;
  3131. int64_t Discriminator = 0;
  3132. auto parseLocOp = [&]() -> bool {
  3133. StringRef Name;
  3134. SMLoc Loc = getTok().getLoc();
  3135. if (parseIdentifier(Name))
  3136. return TokError("unexpected token in '.loc' directive");
  3137. if (Name == "basic_block")
  3138. Flags |= DWARF2_FLAG_BASIC_BLOCK;
  3139. else if (Name == "prologue_end")
  3140. Flags |= DWARF2_FLAG_PROLOGUE_END;
  3141. else if (Name == "epilogue_begin")
  3142. Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
  3143. else if (Name == "is_stmt") {
  3144. Loc = getTok().getLoc();
  3145. const MCExpr *Value;
  3146. if (parseExpression(Value))
  3147. return true;
  3148. // The expression must be the constant 0 or 1.
  3149. if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
  3150. int Value = MCE->getValue();
  3151. if (Value == 0)
  3152. Flags &= ~DWARF2_FLAG_IS_STMT;
  3153. else if (Value == 1)
  3154. Flags |= DWARF2_FLAG_IS_STMT;
  3155. else
  3156. return Error(Loc, "is_stmt value not 0 or 1");
  3157. } else {
  3158. return Error(Loc, "is_stmt value not the constant value of 0 or 1");
  3159. }
  3160. } else if (Name == "isa") {
  3161. Loc = getTok().getLoc();
  3162. const MCExpr *Value;
  3163. if (parseExpression(Value))
  3164. return true;
  3165. // The expression must be a constant greater or equal to 0.
  3166. if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
  3167. int Value = MCE->getValue();
  3168. if (Value < 0)
  3169. return Error(Loc, "isa number less than zero");
  3170. Isa = Value;
  3171. } else {
  3172. return Error(Loc, "isa number not a constant value");
  3173. }
  3174. } else if (Name == "discriminator") {
  3175. if (parseAbsoluteExpression(Discriminator))
  3176. return true;
  3177. } else {
  3178. return Error(Loc, "unknown sub-directive in '.loc' directive");
  3179. }
  3180. return false;
  3181. };
  3182. if (parseMany(parseLocOp, false /*hasComma*/))
  3183. return true;
  3184. getStreamer().emitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
  3185. Isa, Discriminator, StringRef());
  3186. return false;
  3187. }
  3188. /// parseDirectiveStabs
  3189. /// ::= .stabs string, number, number, number
  3190. bool AsmParser::parseDirectiveStabs() {
  3191. return TokError("unsupported directive '.stabs'");
  3192. }
  3193. /// parseDirectiveCVFile
  3194. /// ::= .cv_file number filename [checksum] [checksumkind]
  3195. bool AsmParser::parseDirectiveCVFile() {
  3196. SMLoc FileNumberLoc = getTok().getLoc();
  3197. int64_t FileNumber;
  3198. std::string Filename;
  3199. std::string Checksum;
  3200. int64_t ChecksumKind = 0;
  3201. if (parseIntToken(FileNumber,
  3202. "expected file number in '.cv_file' directive") ||
  3203. check(FileNumber < 1, FileNumberLoc, "file number less than one") ||
  3204. check(getTok().isNot(AsmToken::String),
  3205. "unexpected token in '.cv_file' directive") ||
  3206. parseEscapedString(Filename))
  3207. return true;
  3208. if (!parseOptionalToken(AsmToken::EndOfStatement)) {
  3209. if (check(getTok().isNot(AsmToken::String),
  3210. "unexpected token in '.cv_file' directive") ||
  3211. parseEscapedString(Checksum) ||
  3212. parseIntToken(ChecksumKind,
  3213. "expected checksum kind in '.cv_file' directive") ||
  3214. parseToken(AsmToken::EndOfStatement,
  3215. "unexpected token in '.cv_file' directive"))
  3216. return true;
  3217. }
  3218. Checksum = fromHex(Checksum);
  3219. void *CKMem = Ctx.allocate(Checksum.size(), 1);
  3220. memcpy(CKMem, Checksum.data(), Checksum.size());
  3221. ArrayRef<uint8_t> ChecksumAsBytes(reinterpret_cast<const uint8_t *>(CKMem),
  3222. Checksum.size());
  3223. if (!getStreamer().EmitCVFileDirective(FileNumber, Filename, ChecksumAsBytes,
  3224. static_cast<uint8_t>(ChecksumKind)))
  3225. return Error(FileNumberLoc, "file number already allocated");
  3226. return false;
  3227. }
  3228. bool AsmParser::parseCVFunctionId(int64_t &FunctionId,
  3229. StringRef DirectiveName) {
  3230. SMLoc Loc;
  3231. return parseTokenLoc(Loc) ||
  3232. parseIntToken(FunctionId, "expected function id in '" + DirectiveName +
  3233. "' directive") ||
  3234. check(FunctionId < 0 || FunctionId >= UINT_MAX, Loc,
  3235. "expected function id within range [0, UINT_MAX)");
  3236. }
  3237. bool AsmParser::parseCVFileId(int64_t &FileNumber, StringRef DirectiveName) {
  3238. SMLoc Loc;
  3239. return parseTokenLoc(Loc) ||
  3240. parseIntToken(FileNumber, "expected integer in '" + DirectiveName +
  3241. "' directive") ||
  3242. check(FileNumber < 1, Loc, "file number less than one in '" +
  3243. DirectiveName + "' directive") ||
  3244. check(!getCVContext().isValidFileNumber(FileNumber), Loc,
  3245. "unassigned file number in '" + DirectiveName + "' directive");
  3246. }
  3247. /// parseDirectiveCVFuncId
  3248. /// ::= .cv_func_id FunctionId
  3249. ///
  3250. /// Introduces a function ID that can be used with .cv_loc.
  3251. bool AsmParser::parseDirectiveCVFuncId() {
  3252. SMLoc FunctionIdLoc = getTok().getLoc();
  3253. int64_t FunctionId;
  3254. if (parseCVFunctionId(FunctionId, ".cv_func_id") ||
  3255. parseToken(AsmToken::EndOfStatement,
  3256. "unexpected token in '.cv_func_id' directive"))
  3257. return true;
  3258. if (!getStreamer().EmitCVFuncIdDirective(FunctionId))
  3259. return Error(FunctionIdLoc, "function id already allocated");
  3260. return false;
  3261. }
  3262. /// parseDirectiveCVInlineSiteId
  3263. /// ::= .cv_inline_site_id FunctionId
  3264. /// "within" IAFunc
  3265. /// "inlined_at" IAFile IALine [IACol]
  3266. ///
  3267. /// Introduces a function ID that can be used with .cv_loc. Includes "inlined
  3268. /// at" source location information for use in the line table of the caller,
  3269. /// whether the caller is a real function or another inlined call site.
  3270. bool AsmParser::parseDirectiveCVInlineSiteId() {
  3271. SMLoc FunctionIdLoc = getTok().getLoc();
  3272. int64_t FunctionId;
  3273. int64_t IAFunc;
  3274. int64_t IAFile;
  3275. int64_t IALine;
  3276. int64_t IACol = 0;
  3277. // FunctionId
  3278. if (parseCVFunctionId(FunctionId, ".cv_inline_site_id"))
  3279. return true;
  3280. // "within"
  3281. if (check((getLexer().isNot(AsmToken::Identifier) ||
  3282. getTok().getIdentifier() != "within"),
  3283. "expected 'within' identifier in '.cv_inline_site_id' directive"))
  3284. return true;
  3285. Lex();
  3286. // IAFunc
  3287. if (parseCVFunctionId(IAFunc, ".cv_inline_site_id"))
  3288. return true;
  3289. // "inlined_at"
  3290. if (check((getLexer().isNot(AsmToken::Identifier) ||
  3291. getTok().getIdentifier() != "inlined_at"),
  3292. "expected 'inlined_at' identifier in '.cv_inline_site_id' "
  3293. "directive") )
  3294. return true;
  3295. Lex();
  3296. // IAFile IALine
  3297. if (parseCVFileId(IAFile, ".cv_inline_site_id") ||
  3298. parseIntToken(IALine, "expected line number after 'inlined_at'"))
  3299. return true;
  3300. // [IACol]
  3301. if (getLexer().is(AsmToken::Integer)) {
  3302. IACol = getTok().getIntVal();
  3303. Lex();
  3304. }
  3305. if (parseToken(AsmToken::EndOfStatement,
  3306. "unexpected token in '.cv_inline_site_id' directive"))
  3307. return true;
  3308. if (!getStreamer().EmitCVInlineSiteIdDirective(FunctionId, IAFunc, IAFile,
  3309. IALine, IACol, FunctionIdLoc))
  3310. return Error(FunctionIdLoc, "function id already allocated");
  3311. return false;
  3312. }
  3313. /// parseDirectiveCVLoc
  3314. /// ::= .cv_loc FunctionId FileNumber [LineNumber] [ColumnPos] [prologue_end]
  3315. /// [is_stmt VALUE]
  3316. /// The first number is a file number, must have been previously assigned with
  3317. /// a .file directive, the second number is the line number and optionally the
  3318. /// third number is a column position (zero if not specified). The remaining
  3319. /// optional items are .loc sub-directives.
  3320. bool AsmParser::parseDirectiveCVLoc() {
  3321. SMLoc DirectiveLoc = getTok().getLoc();
  3322. int64_t FunctionId, FileNumber;
  3323. if (parseCVFunctionId(FunctionId, ".cv_loc") ||
  3324. parseCVFileId(FileNumber, ".cv_loc"))
  3325. return true;
  3326. int64_t LineNumber = 0;
  3327. if (getLexer().is(AsmToken::Integer)) {
  3328. LineNumber = getTok().getIntVal();
  3329. if (LineNumber < 0)
  3330. return TokError("line number less than zero in '.cv_loc' directive");
  3331. Lex();
  3332. }
  3333. int64_t ColumnPos = 0;
  3334. if (getLexer().is(AsmToken::Integer)) {
  3335. ColumnPos = getTok().getIntVal();
  3336. if (ColumnPos < 0)
  3337. return TokError("column position less than zero in '.cv_loc' directive");
  3338. Lex();
  3339. }
  3340. bool PrologueEnd = false;
  3341. uint64_t IsStmt = 0;
  3342. auto parseOp = [&]() -> bool {
  3343. StringRef Name;
  3344. SMLoc Loc = getTok().getLoc();
  3345. if (parseIdentifier(Name))
  3346. return TokError("unexpected token in '.cv_loc' directive");
  3347. if (Name == "prologue_end")
  3348. PrologueEnd = true;
  3349. else if (Name == "is_stmt") {
  3350. Loc = getTok().getLoc();
  3351. const MCExpr *Value;
  3352. if (parseExpression(Value))
  3353. return true;
  3354. // The expression must be the constant 0 or 1.
  3355. IsStmt = ~0ULL;
  3356. if (const auto *MCE = dyn_cast<MCConstantExpr>(Value))
  3357. IsStmt = MCE->getValue();
  3358. if (IsStmt > 1)
  3359. return Error(Loc, "is_stmt value not 0 or 1");
  3360. } else {
  3361. return Error(Loc, "unknown sub-directive in '.cv_loc' directive");
  3362. }
  3363. return false;
  3364. };
  3365. if (parseMany(parseOp, false /*hasComma*/))
  3366. return true;
  3367. getStreamer().emitCVLocDirective(FunctionId, FileNumber, LineNumber,
  3368. ColumnPos, PrologueEnd, IsStmt, StringRef(),
  3369. DirectiveLoc);
  3370. return false;
  3371. }
  3372. /// parseDirectiveCVLinetable
  3373. /// ::= .cv_linetable FunctionId, FnStart, FnEnd
  3374. bool AsmParser::parseDirectiveCVLinetable() {
  3375. int64_t FunctionId;
  3376. StringRef FnStartName, FnEndName;
  3377. SMLoc Loc = getTok().getLoc();
  3378. if (parseCVFunctionId(FunctionId, ".cv_linetable") ||
  3379. parseToken(AsmToken::Comma,
  3380. "unexpected token in '.cv_linetable' directive") ||
  3381. parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc,
  3382. "expected identifier in directive") ||
  3383. parseToken(AsmToken::Comma,
  3384. "unexpected token in '.cv_linetable' directive") ||
  3385. parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc,
  3386. "expected identifier in directive"))
  3387. return true;
  3388. MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
  3389. MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
  3390. getStreamer().emitCVLinetableDirective(FunctionId, FnStartSym, FnEndSym);
  3391. return false;
  3392. }
  3393. /// parseDirectiveCVInlineLinetable
  3394. /// ::= .cv_inline_linetable PrimaryFunctionId FileId LineNum FnStart FnEnd
  3395. bool AsmParser::parseDirectiveCVInlineLinetable() {
  3396. int64_t PrimaryFunctionId, SourceFileId, SourceLineNum;
  3397. StringRef FnStartName, FnEndName;
  3398. SMLoc Loc = getTok().getLoc();
  3399. if (parseCVFunctionId(PrimaryFunctionId, ".cv_inline_linetable") ||
  3400. parseTokenLoc(Loc) ||
  3401. parseIntToken(
  3402. SourceFileId,
  3403. "expected SourceField in '.cv_inline_linetable' directive") ||
  3404. check(SourceFileId <= 0, Loc,
  3405. "File id less than zero in '.cv_inline_linetable' directive") ||
  3406. parseTokenLoc(Loc) ||
  3407. parseIntToken(
  3408. SourceLineNum,
  3409. "expected SourceLineNum in '.cv_inline_linetable' directive") ||
  3410. check(SourceLineNum < 0, Loc,
  3411. "Line number less than zero in '.cv_inline_linetable' directive") ||
  3412. parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc,
  3413. "expected identifier in directive") ||
  3414. parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc,
  3415. "expected identifier in directive"))
  3416. return true;
  3417. if (parseToken(AsmToken::EndOfStatement, "Expected End of Statement"))
  3418. return true;
  3419. MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
  3420. MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
  3421. getStreamer().emitCVInlineLinetableDirective(PrimaryFunctionId, SourceFileId,
  3422. SourceLineNum, FnStartSym,
  3423. FnEndSym);
  3424. return false;
  3425. }
  3426. void AsmParser::initializeCVDefRangeTypeMap() {
  3427. CVDefRangeTypeMap["reg"] = CVDR_DEFRANGE_REGISTER;
  3428. CVDefRangeTypeMap["frame_ptr_rel"] = CVDR_DEFRANGE_FRAMEPOINTER_REL;
  3429. CVDefRangeTypeMap["subfield_reg"] = CVDR_DEFRANGE_SUBFIELD_REGISTER;
  3430. CVDefRangeTypeMap["reg_rel"] = CVDR_DEFRANGE_REGISTER_REL;
  3431. }
  3432. /// parseDirectiveCVDefRange
  3433. /// ::= .cv_def_range RangeStart RangeEnd (GapStart GapEnd)*, bytes*
  3434. bool AsmParser::parseDirectiveCVDefRange() {
  3435. SMLoc Loc;
  3436. std::vector<std::pair<const MCSymbol *, const MCSymbol *>> Ranges;
  3437. while (getLexer().is(AsmToken::Identifier)) {
  3438. Loc = getLexer().getLoc();
  3439. StringRef GapStartName;
  3440. if (parseIdentifier(GapStartName))
  3441. return Error(Loc, "expected identifier in directive");
  3442. MCSymbol *GapStartSym = getContext().getOrCreateSymbol(GapStartName);
  3443. Loc = getLexer().getLoc();
  3444. StringRef GapEndName;
  3445. if (parseIdentifier(GapEndName))
  3446. return Error(Loc, "expected identifier in directive");
  3447. MCSymbol *GapEndSym = getContext().getOrCreateSymbol(GapEndName);
  3448. Ranges.push_back({GapStartSym, GapEndSym});
  3449. }
  3450. StringRef CVDefRangeTypeStr;
  3451. if (parseToken(
  3452. AsmToken::Comma,
  3453. "expected comma before def_range type in .cv_def_range directive") ||
  3454. parseIdentifier(CVDefRangeTypeStr))
  3455. return Error(Loc, "expected def_range type in directive");
  3456. StringMap<CVDefRangeType>::const_iterator CVTypeIt =
  3457. CVDefRangeTypeMap.find(CVDefRangeTypeStr);
  3458. CVDefRangeType CVDRType = (CVTypeIt == CVDefRangeTypeMap.end())
  3459. ? CVDR_DEFRANGE
  3460. : CVTypeIt->getValue();
  3461. switch (CVDRType) {
  3462. case CVDR_DEFRANGE_REGISTER: {
  3463. int64_t DRRegister;
  3464. if (parseToken(AsmToken::Comma, "expected comma before register number in "
  3465. ".cv_def_range directive") ||
  3466. parseAbsoluteExpression(DRRegister))
  3467. return Error(Loc, "expected register number");
  3468. codeview::DefRangeRegisterHeader DRHdr;
  3469. DRHdr.Register = DRRegister;
  3470. DRHdr.MayHaveNoName = 0;
  3471. getStreamer().emitCVDefRangeDirective(Ranges, DRHdr);
  3472. break;
  3473. }
  3474. case CVDR_DEFRANGE_FRAMEPOINTER_REL: {
  3475. int64_t DROffset;
  3476. if (parseToken(AsmToken::Comma,
  3477. "expected comma before offset in .cv_def_range directive") ||
  3478. parseAbsoluteExpression(DROffset))
  3479. return Error(Loc, "expected offset value");
  3480. codeview::DefRangeFramePointerRelHeader DRHdr;
  3481. DRHdr.Offset = DROffset;
  3482. getStreamer().emitCVDefRangeDirective(Ranges, DRHdr);
  3483. break;
  3484. }
  3485. case CVDR_DEFRANGE_SUBFIELD_REGISTER: {
  3486. int64_t DRRegister;
  3487. int64_t DROffsetInParent;
  3488. if (parseToken(AsmToken::Comma, "expected comma before register number in "
  3489. ".cv_def_range directive") ||
  3490. parseAbsoluteExpression(DRRegister))
  3491. return Error(Loc, "expected register number");
  3492. if (parseToken(AsmToken::Comma,
  3493. "expected comma before offset in .cv_def_range directive") ||
  3494. parseAbsoluteExpression(DROffsetInParent))
  3495. return Error(Loc, "expected offset value");
  3496. codeview::DefRangeSubfieldRegisterHeader DRHdr;
  3497. DRHdr.Register = DRRegister;
  3498. DRHdr.MayHaveNoName = 0;
  3499. DRHdr.OffsetInParent = DROffsetInParent;
  3500. getStreamer().emitCVDefRangeDirective(Ranges, DRHdr);
  3501. break;
  3502. }
  3503. case CVDR_DEFRANGE_REGISTER_REL: {
  3504. int64_t DRRegister;
  3505. int64_t DRFlags;
  3506. int64_t DRBasePointerOffset;
  3507. if (parseToken(AsmToken::Comma, "expected comma before register number in "
  3508. ".cv_def_range directive") ||
  3509. parseAbsoluteExpression(DRRegister))
  3510. return Error(Loc, "expected register value");
  3511. if (parseToken(
  3512. AsmToken::Comma,
  3513. "expected comma before flag value in .cv_def_range directive") ||
  3514. parseAbsoluteExpression(DRFlags))
  3515. return Error(Loc, "expected flag value");
  3516. if (parseToken(AsmToken::Comma, "expected comma before base pointer offset "
  3517. "in .cv_def_range directive") ||
  3518. parseAbsoluteExpression(DRBasePointerOffset))
  3519. return Error(Loc, "expected base pointer offset value");
  3520. codeview::DefRangeRegisterRelHeader DRHdr;
  3521. DRHdr.Register = DRRegister;
  3522. DRHdr.Flags = DRFlags;
  3523. DRHdr.BasePointerOffset = DRBasePointerOffset;
  3524. getStreamer().emitCVDefRangeDirective(Ranges, DRHdr);
  3525. break;
  3526. }
  3527. default:
  3528. return Error(Loc, "unexpected def_range type in .cv_def_range directive");
  3529. }
  3530. return true;
  3531. }
  3532. /// parseDirectiveCVString
  3533. /// ::= .cv_stringtable "string"
  3534. bool AsmParser::parseDirectiveCVString() {
  3535. std::string Data;
  3536. if (checkForValidSection() || parseEscapedString(Data))
  3537. return addErrorSuffix(" in '.cv_string' directive");
  3538. // Put the string in the table and emit the offset.
  3539. std::pair<StringRef, unsigned> Insertion =
  3540. getCVContext().addToStringTable(Data);
  3541. getStreamer().emitInt32(Insertion.second);
  3542. return false;
  3543. }
  3544. /// parseDirectiveCVStringTable
  3545. /// ::= .cv_stringtable
  3546. bool AsmParser::parseDirectiveCVStringTable() {
  3547. getStreamer().emitCVStringTableDirective();
  3548. return false;
  3549. }
  3550. /// parseDirectiveCVFileChecksums
  3551. /// ::= .cv_filechecksums
  3552. bool AsmParser::parseDirectiveCVFileChecksums() {
  3553. getStreamer().emitCVFileChecksumsDirective();
  3554. return false;
  3555. }
  3556. /// parseDirectiveCVFileChecksumOffset
  3557. /// ::= .cv_filechecksumoffset fileno
  3558. bool AsmParser::parseDirectiveCVFileChecksumOffset() {
  3559. int64_t FileNo;
  3560. if (parseIntToken(FileNo, "expected identifier in directive"))
  3561. return true;
  3562. if (parseToken(AsmToken::EndOfStatement, "Expected End of Statement"))
  3563. return true;
  3564. getStreamer().emitCVFileChecksumOffsetDirective(FileNo);
  3565. return false;
  3566. }
  3567. /// parseDirectiveCVFPOData
  3568. /// ::= .cv_fpo_data procsym
  3569. bool AsmParser::parseDirectiveCVFPOData() {
  3570. SMLoc DirLoc = getLexer().getLoc();
  3571. StringRef ProcName;
  3572. if (parseIdentifier(ProcName))
  3573. return TokError("expected symbol name");
  3574. if (parseEOL("unexpected tokens"))
  3575. return addErrorSuffix(" in '.cv_fpo_data' directive");
  3576. MCSymbol *ProcSym = getContext().getOrCreateSymbol(ProcName);
  3577. getStreamer().EmitCVFPOData(ProcSym, DirLoc);
  3578. return false;
  3579. }
  3580. /// parseDirectiveCFISections
  3581. /// ::= .cfi_sections section [, section]
  3582. bool AsmParser::parseDirectiveCFISections() {
  3583. StringRef Name;
  3584. bool EH = false;
  3585. bool Debug = false;
  3586. if (parseIdentifier(Name))
  3587. return TokError("Expected an identifier");
  3588. if (Name == ".eh_frame")
  3589. EH = true;
  3590. else if (Name == ".debug_frame")
  3591. Debug = true;
  3592. if (getLexer().is(AsmToken::Comma)) {
  3593. Lex();
  3594. if (parseIdentifier(Name))
  3595. return TokError("Expected an identifier");
  3596. if (Name == ".eh_frame")
  3597. EH = true;
  3598. else if (Name == ".debug_frame")
  3599. Debug = true;
  3600. }
  3601. if (parseToken(AsmToken::EndOfStatement))
  3602. return addErrorSuffix(" in '.cfi_sections' directive");
  3603. getStreamer().emitCFISections(EH, Debug);
  3604. return false;
  3605. }
  3606. /// parseDirectiveCFIStartProc
  3607. /// ::= .cfi_startproc [simple]
  3608. bool AsmParser::parseDirectiveCFIStartProc() {
  3609. StringRef Simple;
  3610. if (!parseOptionalToken(AsmToken::EndOfStatement)) {
  3611. if (check(parseIdentifier(Simple) || Simple != "simple",
  3612. "unexpected token") ||
  3613. parseToken(AsmToken::EndOfStatement))
  3614. return addErrorSuffix(" in '.cfi_startproc' directive");
  3615. }
  3616. // TODO(kristina): Deal with a corner case of incorrect diagnostic context
  3617. // being produced if this directive is emitted as part of preprocessor macro
  3618. // expansion which can *ONLY* happen if Clang's cc1as is the API consumer.
  3619. // Tools like llvm-mc on the other hand are not affected by it, and report
  3620. // correct context information.
  3621. getStreamer().emitCFIStartProc(!Simple.empty(), Lexer.getLoc());
  3622. return false;
  3623. }
  3624. /// parseDirectiveCFIEndProc
  3625. /// ::= .cfi_endproc
  3626. bool AsmParser::parseDirectiveCFIEndProc() {
  3627. if (parseToken(AsmToken::EndOfStatement))
  3628. return addErrorSuffix(" in '.cfi_endproc' directive");
  3629. getStreamer().emitCFIEndProc();
  3630. return false;
  3631. }
  3632. /// parse register name or number.
  3633. bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
  3634. SMLoc DirectiveLoc) {
  3635. unsigned RegNo;
  3636. if (getLexer().isNot(AsmToken::Integer)) {
  3637. if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
  3638. return true;
  3639. Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
  3640. } else
  3641. return parseAbsoluteExpression(Register);
  3642. return false;
  3643. }
  3644. /// parseDirectiveCFIDefCfa
  3645. /// ::= .cfi_def_cfa register, offset
  3646. bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
  3647. int64_t Register = 0, Offset = 0;
  3648. if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) ||
  3649. parseToken(AsmToken::Comma, "unexpected token in directive") ||
  3650. parseAbsoluteExpression(Offset))
  3651. return true;
  3652. getStreamer().emitCFIDefCfa(Register, Offset);
  3653. return false;
  3654. }
  3655. /// parseDirectiveCFIDefCfaOffset
  3656. /// ::= .cfi_def_cfa_offset offset
  3657. bool AsmParser::parseDirectiveCFIDefCfaOffset() {
  3658. int64_t Offset = 0;
  3659. if (parseAbsoluteExpression(Offset))
  3660. return true;
  3661. getStreamer().emitCFIDefCfaOffset(Offset);
  3662. return false;
  3663. }
  3664. /// parseDirectiveCFIRegister
  3665. /// ::= .cfi_register register, register
  3666. bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
  3667. int64_t Register1 = 0, Register2 = 0;
  3668. if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc) ||
  3669. parseToken(AsmToken::Comma, "unexpected token in directive") ||
  3670. parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
  3671. return true;
  3672. getStreamer().emitCFIRegister(Register1, Register2);
  3673. return false;
  3674. }
  3675. /// parseDirectiveCFIWindowSave
  3676. /// ::= .cfi_window_save
  3677. bool AsmParser::parseDirectiveCFIWindowSave() {
  3678. getStreamer().emitCFIWindowSave();
  3679. return false;
  3680. }
  3681. /// parseDirectiveCFIAdjustCfaOffset
  3682. /// ::= .cfi_adjust_cfa_offset adjustment
  3683. bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
  3684. int64_t Adjustment = 0;
  3685. if (parseAbsoluteExpression(Adjustment))
  3686. return true;
  3687. getStreamer().emitCFIAdjustCfaOffset(Adjustment);
  3688. return false;
  3689. }
  3690. /// parseDirectiveCFIDefCfaRegister
  3691. /// ::= .cfi_def_cfa_register register
  3692. bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
  3693. int64_t Register = 0;
  3694. if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
  3695. return true;
  3696. getStreamer().emitCFIDefCfaRegister(Register);
  3697. return false;
  3698. }
  3699. /// parseDirectiveCFIOffset
  3700. /// ::= .cfi_offset register, offset
  3701. bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
  3702. int64_t Register = 0;
  3703. int64_t Offset = 0;
  3704. if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) ||
  3705. parseToken(AsmToken::Comma, "unexpected token in directive") ||
  3706. parseAbsoluteExpression(Offset))
  3707. return true;
  3708. getStreamer().emitCFIOffset(Register, Offset);
  3709. return false;
  3710. }
  3711. /// parseDirectiveCFIRelOffset
  3712. /// ::= .cfi_rel_offset register, offset
  3713. bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
  3714. int64_t Register = 0, Offset = 0;
  3715. if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) ||
  3716. parseToken(AsmToken::Comma, "unexpected token in directive") ||
  3717. parseAbsoluteExpression(Offset))
  3718. return true;
  3719. getStreamer().emitCFIRelOffset(Register, Offset);
  3720. return false;
  3721. }
  3722. static bool isValidEncoding(int64_t Encoding) {
  3723. if (Encoding & ~0xff)
  3724. return false;
  3725. if (Encoding == dwarf::DW_EH_PE_omit)
  3726. return true;
  3727. const unsigned Format = Encoding & 0xf;
  3728. if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
  3729. Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
  3730. Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
  3731. Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
  3732. return false;
  3733. const unsigned Application = Encoding & 0x70;
  3734. if (Application != dwarf::DW_EH_PE_absptr &&
  3735. Application != dwarf::DW_EH_PE_pcrel)
  3736. return false;
  3737. return true;
  3738. }
  3739. /// parseDirectiveCFIPersonalityOrLsda
  3740. /// IsPersonality true for cfi_personality, false for cfi_lsda
  3741. /// ::= .cfi_personality encoding, [symbol_name]
  3742. /// ::= .cfi_lsda encoding, [symbol_name]
  3743. bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
  3744. int64_t Encoding = 0;
  3745. if (parseAbsoluteExpression(Encoding))
  3746. return true;
  3747. if (Encoding == dwarf::DW_EH_PE_omit)
  3748. return false;
  3749. StringRef Name;
  3750. if (check(!isValidEncoding(Encoding), "unsupported encoding.") ||
  3751. parseToken(AsmToken::Comma, "unexpected token in directive") ||
  3752. check(parseIdentifier(Name), "expected identifier in directive"))
  3753. return true;
  3754. MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
  3755. if (IsPersonality)
  3756. getStreamer().emitCFIPersonality(Sym, Encoding);
  3757. else
  3758. getStreamer().emitCFILsda(Sym, Encoding);
  3759. return false;
  3760. }
  3761. /// parseDirectiveCFIRememberState
  3762. /// ::= .cfi_remember_state
  3763. bool AsmParser::parseDirectiveCFIRememberState() {
  3764. getStreamer().emitCFIRememberState();
  3765. return false;
  3766. }
  3767. /// parseDirectiveCFIRestoreState
  3768. /// ::= .cfi_remember_state
  3769. bool AsmParser::parseDirectiveCFIRestoreState() {
  3770. getStreamer().emitCFIRestoreState();
  3771. return false;
  3772. }
  3773. /// parseDirectiveCFISameValue
  3774. /// ::= .cfi_same_value register
  3775. bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
  3776. int64_t Register = 0;
  3777. if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
  3778. return true;
  3779. getStreamer().emitCFISameValue(Register);
  3780. return false;
  3781. }
  3782. /// parseDirectiveCFIRestore
  3783. /// ::= .cfi_restore register
  3784. bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
  3785. int64_t Register = 0;
  3786. if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
  3787. return true;
  3788. getStreamer().emitCFIRestore(Register);
  3789. return false;
  3790. }
  3791. /// parseDirectiveCFIEscape
  3792. /// ::= .cfi_escape expression[,...]
  3793. bool AsmParser::parseDirectiveCFIEscape() {
  3794. std::string Values;
  3795. int64_t CurrValue;
  3796. if (parseAbsoluteExpression(CurrValue))
  3797. return true;
  3798. Values.push_back((uint8_t)CurrValue);
  3799. while (getLexer().is(AsmToken::Comma)) {
  3800. Lex();
  3801. if (parseAbsoluteExpression(CurrValue))
  3802. return true;
  3803. Values.push_back((uint8_t)CurrValue);
  3804. }
  3805. getStreamer().emitCFIEscape(Values);
  3806. return false;
  3807. }
  3808. /// parseDirectiveCFIReturnColumn
  3809. /// ::= .cfi_return_column register
  3810. bool AsmParser::parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc) {
  3811. int64_t Register = 0;
  3812. if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
  3813. return true;
  3814. getStreamer().emitCFIReturnColumn(Register);
  3815. return false;
  3816. }
  3817. /// parseDirectiveCFISignalFrame
  3818. /// ::= .cfi_signal_frame
  3819. bool AsmParser::parseDirectiveCFISignalFrame() {
  3820. if (parseToken(AsmToken::EndOfStatement,
  3821. "unexpected token in '.cfi_signal_frame'"))
  3822. return true;
  3823. getStreamer().emitCFISignalFrame();
  3824. return false;
  3825. }
  3826. /// parseDirectiveCFIUndefined
  3827. /// ::= .cfi_undefined register
  3828. bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
  3829. int64_t Register = 0;
  3830. if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
  3831. return true;
  3832. getStreamer().emitCFIUndefined(Register);
  3833. return false;
  3834. }
  3835. /// parseDirectiveAltmacro
  3836. /// ::= .altmacro
  3837. /// ::= .noaltmacro
  3838. bool AsmParser::parseDirectiveAltmacro(StringRef Directive) {
  3839. if (getLexer().isNot(AsmToken::EndOfStatement))
  3840. return TokError("unexpected token in '" + Directive + "' directive");
  3841. AltMacroMode = (Directive == ".altmacro");
  3842. return false;
  3843. }
  3844. /// parseDirectiveMacrosOnOff
  3845. /// ::= .macros_on
  3846. /// ::= .macros_off
  3847. bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
  3848. if (parseToken(AsmToken::EndOfStatement,
  3849. "unexpected token in '" + Directive + "' directive"))
  3850. return true;
  3851. setMacrosEnabled(Directive == ".macros_on");
  3852. return false;
  3853. }
  3854. /// parseDirectiveMacro
  3855. /// ::= .macro name[,] [parameters]
  3856. bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
  3857. StringRef Name;
  3858. if (parseIdentifier(Name))
  3859. return TokError("expected identifier in '.macro' directive");
  3860. if (getLexer().is(AsmToken::Comma))
  3861. Lex();
  3862. MCAsmMacroParameters Parameters;
  3863. while (getLexer().isNot(AsmToken::EndOfStatement)) {
  3864. if (!Parameters.empty() && Parameters.back().Vararg)
  3865. return Error(Lexer.getLoc(), "vararg parameter '" +
  3866. Parameters.back().Name +
  3867. "' should be the last parameter");
  3868. MCAsmMacroParameter Parameter;
  3869. if (parseIdentifier(Parameter.Name))
  3870. return TokError("expected identifier in '.macro' directive");
  3871. // Emit an error if two (or more) named parameters share the same name
  3872. for (const MCAsmMacroParameter& CurrParam : Parameters)
  3873. if (CurrParam.Name.equals(Parameter.Name))
  3874. return TokError("macro '" + Name + "' has multiple parameters"
  3875. " named '" + Parameter.Name + "'");
  3876. if (Lexer.is(AsmToken::Colon)) {
  3877. Lex(); // consume ':'
  3878. SMLoc QualLoc;
  3879. StringRef Qualifier;
  3880. QualLoc = Lexer.getLoc();
  3881. if (parseIdentifier(Qualifier))
  3882. return Error(QualLoc, "missing parameter qualifier for "
  3883. "'" + Parameter.Name + "' in macro '" + Name + "'");
  3884. if (Qualifier == "req")
  3885. Parameter.Required = true;
  3886. else if (Qualifier == "vararg")
  3887. Parameter.Vararg = true;
  3888. else
  3889. return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
  3890. "for '" + Parameter.Name + "' in macro '" + Name + "'");
  3891. }
  3892. if (getLexer().is(AsmToken::Equal)) {
  3893. Lex();
  3894. SMLoc ParamLoc;
  3895. ParamLoc = Lexer.getLoc();
  3896. if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
  3897. return true;
  3898. if (Parameter.Required)
  3899. Warning(ParamLoc, "pointless default value for required parameter "
  3900. "'" + Parameter.Name + "' in macro '" + Name + "'");
  3901. }
  3902. Parameters.push_back(std::move(Parameter));
  3903. if (getLexer().is(AsmToken::Comma))
  3904. Lex();
  3905. }
  3906. // Eat just the end of statement.
  3907. Lexer.Lex();
  3908. // Consuming deferred text, so use Lexer.Lex to ignore Lexing Errors
  3909. AsmToken EndToken, StartToken = getTok();
  3910. unsigned MacroDepth = 0;
  3911. // Lex the macro definition.
  3912. while (true) {
  3913. // Ignore Lexing errors in macros.
  3914. while (Lexer.is(AsmToken::Error)) {
  3915. Lexer.Lex();
  3916. }
  3917. // Check whether we have reached the end of the file.
  3918. if (getLexer().is(AsmToken::Eof))
  3919. return Error(DirectiveLoc, "no matching '.endmacro' in definition");
  3920. // Otherwise, check whether we have reach the .endmacro or the start of a
  3921. // preprocessor line marker.
  3922. if (getLexer().is(AsmToken::Identifier)) {
  3923. if (getTok().getIdentifier() == ".endm" ||
  3924. getTok().getIdentifier() == ".endmacro") {
  3925. if (MacroDepth == 0) { // Outermost macro.
  3926. EndToken = getTok();
  3927. Lexer.Lex();
  3928. if (getLexer().isNot(AsmToken::EndOfStatement))
  3929. return TokError("unexpected token in '" + EndToken.getIdentifier() +
  3930. "' directive");
  3931. break;
  3932. } else {
  3933. // Otherwise we just found the end of an inner macro.
  3934. --MacroDepth;
  3935. }
  3936. } else if (getTok().getIdentifier() == ".macro") {
  3937. // We allow nested macros. Those aren't instantiated until the outermost
  3938. // macro is expanded so just ignore them for now.
  3939. ++MacroDepth;
  3940. }
  3941. } else if (Lexer.is(AsmToken::HashDirective)) {
  3942. (void)parseCppHashLineFilenameComment(getLexer().getLoc());
  3943. }
  3944. // Otherwise, scan til the end of the statement.
  3945. eatToEndOfStatement();
  3946. }
  3947. if (getContext().lookupMacro(Name)) {
  3948. return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
  3949. }
  3950. const char *BodyStart = StartToken.getLoc().getPointer();
  3951. const char *BodyEnd = EndToken.getLoc().getPointer();
  3952. StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
  3953. checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
  3954. MCAsmMacro Macro(Name, Body, std::move(Parameters));
  3955. DEBUG_WITH_TYPE("asm-macros", dbgs() << "Defining new macro:\n";
  3956. Macro.dump());
  3957. getContext().defineMacro(Name, std::move(Macro));
  3958. return false;
  3959. }
  3960. /// checkForBadMacro
  3961. ///
  3962. /// With the support added for named parameters there may be code out there that
  3963. /// is transitioning from positional parameters. In versions of gas that did
  3964. /// not support named parameters they would be ignored on the macro definition.
  3965. /// But to support both styles of parameters this is not possible so if a macro
  3966. /// definition has named parameters but does not use them and has what appears
  3967. /// to be positional parameters, strings like $1, $2, ... and $n, then issue a
  3968. /// warning that the positional parameter found in body which have no effect.
  3969. /// Hoping the developer will either remove the named parameters from the macro
  3970. /// definition so the positional parameters get used if that was what was
  3971. /// intended or change the macro to use the named parameters. It is possible
  3972. /// this warning will trigger when the none of the named parameters are used
  3973. /// and the strings like $1 are infact to simply to be passed trough unchanged.
  3974. void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
  3975. StringRef Body,
  3976. ArrayRef<MCAsmMacroParameter> Parameters) {
  3977. // If this macro is not defined with named parameters the warning we are
  3978. // checking for here doesn't apply.
  3979. unsigned NParameters = Parameters.size();
  3980. if (NParameters == 0)
  3981. return;
  3982. bool NamedParametersFound = false;
  3983. bool PositionalParametersFound = false;
  3984. // Look at the body of the macro for use of both the named parameters and what
  3985. // are likely to be positional parameters. This is what expandMacro() is
  3986. // doing when it finds the parameters in the body.
  3987. while (!Body.empty()) {
  3988. // Scan for the next possible parameter.
  3989. std::size_t End = Body.size(), Pos = 0;
  3990. for (; Pos != End; ++Pos) {
  3991. // Check for a substitution or escape.
  3992. // This macro is defined with parameters, look for \foo, \bar, etc.
  3993. if (Body[Pos] == '\\' && Pos + 1 != End)
  3994. break;
  3995. // This macro should have parameters, but look for $0, $1, ..., $n too.
  3996. if (Body[Pos] != '$' || Pos + 1 == End)
  3997. continue;
  3998. char Next = Body[Pos + 1];
  3999. if (Next == '$' || Next == 'n' ||
  4000. isdigit(static_cast<unsigned char>(Next)))
  4001. break;
  4002. }
  4003. // Check if we reached the end.
  4004. if (Pos == End)
  4005. break;
  4006. if (Body[Pos] == '$') {
  4007. switch (Body[Pos + 1]) {
  4008. // $$ => $
  4009. case '$':
  4010. break;
  4011. // $n => number of arguments
  4012. case 'n':
  4013. PositionalParametersFound = true;
  4014. break;
  4015. // $[0-9] => argument
  4016. default: {
  4017. PositionalParametersFound = true;
  4018. break;
  4019. }
  4020. }
  4021. Pos += 2;
  4022. } else {
  4023. unsigned I = Pos + 1;
  4024. while (isIdentifierChar(Body[I]) && I + 1 != End)
  4025. ++I;
  4026. const char *Begin = Body.data() + Pos + 1;
  4027. StringRef Argument(Begin, I - (Pos + 1));
  4028. unsigned Index = 0;
  4029. for (; Index < NParameters; ++Index)
  4030. if (Parameters[Index].Name == Argument)
  4031. break;
  4032. if (Index == NParameters) {
  4033. if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
  4034. Pos += 3;
  4035. else {
  4036. Pos = I;
  4037. }
  4038. } else {
  4039. NamedParametersFound = true;
  4040. Pos += 1 + Argument.size();
  4041. }
  4042. }
  4043. // Update the scan point.
  4044. Body = Body.substr(Pos);
  4045. }
  4046. if (!NamedParametersFound && PositionalParametersFound)
  4047. Warning(DirectiveLoc, "macro defined with named parameters which are not "
  4048. "used in macro body, possible positional parameter "
  4049. "found in body which will have no effect");
  4050. }
  4051. /// parseDirectiveExitMacro
  4052. /// ::= .exitm
  4053. bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
  4054. if (parseToken(AsmToken::EndOfStatement,
  4055. "unexpected token in '" + Directive + "' directive"))
  4056. return true;
  4057. if (!isInsideMacroInstantiation())
  4058. return TokError("unexpected '" + Directive + "' in file, "
  4059. "no current macro definition");
  4060. // Exit all conditionals that are active in the current macro.
  4061. while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
  4062. TheCondState = TheCondStack.back();
  4063. TheCondStack.pop_back();
  4064. }
  4065. handleMacroExit();
  4066. return false;
  4067. }
  4068. /// parseDirectiveEndMacro
  4069. /// ::= .endm
  4070. /// ::= .endmacro
  4071. bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
  4072. if (getLexer().isNot(AsmToken::EndOfStatement))
  4073. return TokError("unexpected token in '" + Directive + "' directive");
  4074. // If we are inside a macro instantiation, terminate the current
  4075. // instantiation.
  4076. if (isInsideMacroInstantiation()) {
  4077. handleMacroExit();
  4078. return false;
  4079. }
  4080. // Otherwise, this .endmacro is a stray entry in the file; well formed
  4081. // .endmacro directives are handled during the macro definition parsing.
  4082. return TokError("unexpected '" + Directive + "' in file, "
  4083. "no current macro definition");
  4084. }
  4085. /// parseDirectivePurgeMacro
  4086. /// ::= .purgem
  4087. bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
  4088. StringRef Name;
  4089. SMLoc Loc;
  4090. if (parseTokenLoc(Loc) ||
  4091. check(parseIdentifier(Name), Loc,
  4092. "expected identifier in '.purgem' directive") ||
  4093. parseToken(AsmToken::EndOfStatement,
  4094. "unexpected token in '.purgem' directive"))
  4095. return true;
  4096. if (!getContext().lookupMacro(Name))
  4097. return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
  4098. getContext().undefineMacro(Name);
  4099. DEBUG_WITH_TYPE("asm-macros", dbgs()
  4100. << "Un-defining macro: " << Name << "\n");
  4101. return false;
  4102. }
  4103. /// parseDirectiveBundleAlignMode
  4104. /// ::= {.bundle_align_mode} expression
  4105. bool AsmParser::parseDirectiveBundleAlignMode() {
  4106. // Expect a single argument: an expression that evaluates to a constant
  4107. // in the inclusive range 0-30.
  4108. SMLoc ExprLoc = getLexer().getLoc();
  4109. int64_t AlignSizePow2;
  4110. if (checkForValidSection() || parseAbsoluteExpression(AlignSizePow2) ||
  4111. parseToken(AsmToken::EndOfStatement, "unexpected token after expression "
  4112. "in '.bundle_align_mode' "
  4113. "directive") ||
  4114. check(AlignSizePow2 < 0 || AlignSizePow2 > 30, ExprLoc,
  4115. "invalid bundle alignment size (expected between 0 and 30)"))
  4116. return true;
  4117. // Because of AlignSizePow2's verified range we can safely truncate it to
  4118. // unsigned.
  4119. getStreamer().emitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
  4120. return false;
  4121. }
  4122. /// parseDirectiveBundleLock
  4123. /// ::= {.bundle_lock} [align_to_end]
  4124. bool AsmParser::parseDirectiveBundleLock() {
  4125. if (checkForValidSection())
  4126. return true;
  4127. bool AlignToEnd = false;
  4128. StringRef Option;
  4129. SMLoc Loc = getTok().getLoc();
  4130. const char *kInvalidOptionError =
  4131. "invalid option for '.bundle_lock' directive";
  4132. if (!parseOptionalToken(AsmToken::EndOfStatement)) {
  4133. if (check(parseIdentifier(Option), Loc, kInvalidOptionError) ||
  4134. check(Option != "align_to_end", Loc, kInvalidOptionError) ||
  4135. parseToken(AsmToken::EndOfStatement,
  4136. "unexpected token after '.bundle_lock' directive option"))
  4137. return true;
  4138. AlignToEnd = true;
  4139. }
  4140. getStreamer().emitBundleLock(AlignToEnd);
  4141. return false;
  4142. }
  4143. /// parseDirectiveBundleLock
  4144. /// ::= {.bundle_lock}
  4145. bool AsmParser::parseDirectiveBundleUnlock() {
  4146. if (checkForValidSection() ||
  4147. parseToken(AsmToken::EndOfStatement,
  4148. "unexpected token in '.bundle_unlock' directive"))
  4149. return true;
  4150. getStreamer().emitBundleUnlock();
  4151. return false;
  4152. }
  4153. /// parseDirectiveSpace
  4154. /// ::= (.skip | .space) expression [ , expression ]
  4155. bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
  4156. SMLoc NumBytesLoc = Lexer.getLoc();
  4157. const MCExpr *NumBytes;
  4158. if (checkForValidSection() || parseExpression(NumBytes))
  4159. return true;
  4160. int64_t FillExpr = 0;
  4161. if (parseOptionalToken(AsmToken::Comma))
  4162. if (parseAbsoluteExpression(FillExpr))
  4163. return addErrorSuffix("in '" + Twine(IDVal) + "' directive");
  4164. if (parseToken(AsmToken::EndOfStatement))
  4165. return addErrorSuffix("in '" + Twine(IDVal) + "' directive");
  4166. // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
  4167. getStreamer().emitFill(*NumBytes, FillExpr, NumBytesLoc);
  4168. return false;
  4169. }
  4170. /// parseDirectiveDCB
  4171. /// ::= .dcb.{b, l, w} expression, expression
  4172. bool AsmParser::parseDirectiveDCB(StringRef IDVal, unsigned Size) {
  4173. SMLoc NumValuesLoc = Lexer.getLoc();
  4174. int64_t NumValues;
  4175. if (checkForValidSection() || parseAbsoluteExpression(NumValues))
  4176. return true;
  4177. if (NumValues < 0) {
  4178. Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect");
  4179. return false;
  4180. }
  4181. if (parseToken(AsmToken::Comma,
  4182. "unexpected token in '" + Twine(IDVal) + "' directive"))
  4183. return true;
  4184. const MCExpr *Value;
  4185. SMLoc ExprLoc = getLexer().getLoc();
  4186. if (parseExpression(Value))
  4187. return true;
  4188. // Special case constant expressions to match code generator.
  4189. if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
  4190. assert(Size <= 8 && "Invalid size");
  4191. uint64_t IntValue = MCE->getValue();
  4192. if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
  4193. return Error(ExprLoc, "literal value out of range for directive");
  4194. for (uint64_t i = 0, e = NumValues; i != e; ++i)
  4195. getStreamer().emitIntValue(IntValue, Size);
  4196. } else {
  4197. for (uint64_t i = 0, e = NumValues; i != e; ++i)
  4198. getStreamer().emitValue(Value, Size, ExprLoc);
  4199. }
  4200. if (parseToken(AsmToken::EndOfStatement,
  4201. "unexpected token in '" + Twine(IDVal) + "' directive"))
  4202. return true;
  4203. return false;
  4204. }
  4205. /// parseDirectiveRealDCB
  4206. /// ::= .dcb.{d, s} expression, expression
  4207. bool AsmParser::parseDirectiveRealDCB(StringRef IDVal, const fltSemantics &Semantics) {
  4208. SMLoc NumValuesLoc = Lexer.getLoc();
  4209. int64_t NumValues;
  4210. if (checkForValidSection() || parseAbsoluteExpression(NumValues))
  4211. return true;
  4212. if (NumValues < 0) {
  4213. Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect");
  4214. return false;
  4215. }
  4216. if (parseToken(AsmToken::Comma,
  4217. "unexpected token in '" + Twine(IDVal) + "' directive"))
  4218. return true;
  4219. APInt AsInt;
  4220. if (parseRealValue(Semantics, AsInt))
  4221. return true;
  4222. if (parseToken(AsmToken::EndOfStatement,
  4223. "unexpected token in '" + Twine(IDVal) + "' directive"))
  4224. return true;
  4225. for (uint64_t i = 0, e = NumValues; i != e; ++i)
  4226. getStreamer().emitIntValue(AsInt.getLimitedValue(),
  4227. AsInt.getBitWidth() / 8);
  4228. return false;
  4229. }
  4230. /// parseDirectiveDS
  4231. /// ::= .ds.{b, d, l, p, s, w, x} expression
  4232. bool AsmParser::parseDirectiveDS(StringRef IDVal, unsigned Size) {
  4233. SMLoc NumValuesLoc = Lexer.getLoc();
  4234. int64_t NumValues;
  4235. if (checkForValidSection() || parseAbsoluteExpression(NumValues))
  4236. return true;
  4237. if (NumValues < 0) {
  4238. Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect");
  4239. return false;
  4240. }
  4241. if (parseToken(AsmToken::EndOfStatement,
  4242. "unexpected token in '" + Twine(IDVal) + "' directive"))
  4243. return true;
  4244. for (uint64_t i = 0, e = NumValues; i != e; ++i)
  4245. getStreamer().emitFill(Size, 0);
  4246. return false;
  4247. }
  4248. /// parseDirectiveLEB128
  4249. /// ::= (.sleb128 | .uleb128) [ expression (, expression)* ]
  4250. bool AsmParser::parseDirectiveLEB128(bool Signed) {
  4251. if (checkForValidSection())
  4252. return true;
  4253. auto parseOp = [&]() -> bool {
  4254. const MCExpr *Value;
  4255. if (parseExpression(Value))
  4256. return true;
  4257. if (Signed)
  4258. getStreamer().emitSLEB128Value(Value);
  4259. else
  4260. getStreamer().emitULEB128Value(Value);
  4261. return false;
  4262. };
  4263. if (parseMany(parseOp))
  4264. return addErrorSuffix(" in directive");
  4265. return false;
  4266. }
  4267. /// parseDirectiveSymbolAttribute
  4268. /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
  4269. bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
  4270. auto parseOp = [&]() -> bool {
  4271. StringRef Name;
  4272. SMLoc Loc = getTok().getLoc();
  4273. if (parseIdentifier(Name))
  4274. return Error(Loc, "expected identifier");
  4275. MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
  4276. // Assembler local symbols don't make any sense here. Complain loudly.
  4277. if (Sym->isTemporary())
  4278. return Error(Loc, "non-local symbol required");
  4279. if (!getStreamer().emitSymbolAttribute(Sym, Attr))
  4280. return Error(Loc, "unable to emit symbol attribute");
  4281. return false;
  4282. };
  4283. if (parseMany(parseOp))
  4284. return addErrorSuffix(" in directive");
  4285. return false;
  4286. }
  4287. /// parseDirectiveComm
  4288. /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
  4289. bool AsmParser::parseDirectiveComm(bool IsLocal) {
  4290. if (checkForValidSection())
  4291. return true;
  4292. SMLoc IDLoc = getLexer().getLoc();
  4293. StringRef Name;
  4294. if (parseIdentifier(Name))
  4295. return TokError("expected identifier in directive");
  4296. // Handle the identifier as the key symbol.
  4297. MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
  4298. if (getLexer().isNot(AsmToken::Comma))
  4299. return TokError("unexpected token in directive");
  4300. Lex();
  4301. int64_t Size;
  4302. SMLoc SizeLoc = getLexer().getLoc();
  4303. if (parseAbsoluteExpression(Size))
  4304. return true;
  4305. int64_t Pow2Alignment = 0;
  4306. SMLoc Pow2AlignmentLoc;
  4307. if (getLexer().is(AsmToken::Comma)) {
  4308. Lex();
  4309. Pow2AlignmentLoc = getLexer().getLoc();
  4310. if (parseAbsoluteExpression(Pow2Alignment))
  4311. return true;
  4312. LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
  4313. if (IsLocal && LCOMM == LCOMM::NoAlignment)
  4314. return Error(Pow2AlignmentLoc, "alignment not supported on this target");
  4315. // If this target takes alignments in bytes (not log) validate and convert.
  4316. if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
  4317. (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
  4318. if (!isPowerOf2_64(Pow2Alignment))
  4319. return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
  4320. Pow2Alignment = Log2_64(Pow2Alignment);
  4321. }
  4322. }
  4323. if (parseToken(AsmToken::EndOfStatement,
  4324. "unexpected token in '.comm' or '.lcomm' directive"))
  4325. return true;
  4326. // NOTE: a size of zero for a .comm should create a undefined symbol
  4327. // but a size of .lcomm creates a bss symbol of size zero.
  4328. if (Size < 0)
  4329. return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
  4330. "be less than zero");
  4331. // NOTE: The alignment in the directive is a power of 2 value, the assembler
  4332. // may internally end up wanting an alignment in bytes.
  4333. // FIXME: Diagnose overflow.
  4334. if (Pow2Alignment < 0)
  4335. return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
  4336. "alignment, can't be less than zero");
  4337. Sym->redefineIfPossible();
  4338. if (!Sym->isUndefined())
  4339. return Error(IDLoc, "invalid symbol redefinition");
  4340. // Create the Symbol as a common or local common with Size and Pow2Alignment
  4341. if (IsLocal) {
  4342. getStreamer().emitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
  4343. return false;
  4344. }
  4345. getStreamer().emitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
  4346. return false;
  4347. }
  4348. /// parseDirectiveAbort
  4349. /// ::= .abort [... message ...]
  4350. bool AsmParser::parseDirectiveAbort() {
  4351. // FIXME: Use loc from directive.
  4352. SMLoc Loc = getLexer().getLoc();
  4353. StringRef Str = parseStringToEndOfStatement();
  4354. if (parseToken(AsmToken::EndOfStatement,
  4355. "unexpected token in '.abort' directive"))
  4356. return true;
  4357. if (Str.empty())
  4358. return Error(Loc, ".abort detected. Assembly stopping.");
  4359. else
  4360. return Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
  4361. // FIXME: Actually abort assembly here.
  4362. return false;
  4363. }
  4364. /// parseDirectiveInclude
  4365. /// ::= .include "filename"
  4366. bool AsmParser::parseDirectiveInclude() {
  4367. // Allow the strings to have escaped octal character sequence.
  4368. std::string Filename;
  4369. SMLoc IncludeLoc = getTok().getLoc();
  4370. if (check(getTok().isNot(AsmToken::String),
  4371. "expected string in '.include' directive") ||
  4372. parseEscapedString(Filename) ||
  4373. check(getTok().isNot(AsmToken::EndOfStatement),
  4374. "unexpected token in '.include' directive") ||
  4375. // Attempt to switch the lexer to the included file before consuming the
  4376. // end of statement to avoid losing it when we switch.
  4377. check(enterIncludeFile(Filename), IncludeLoc,
  4378. "Could not find include file '" + Filename + "'"))
  4379. return true;
  4380. return false;
  4381. }
  4382. /// parseDirectiveIncbin
  4383. /// ::= .incbin "filename" [ , skip [ , count ] ]
  4384. bool AsmParser::parseDirectiveIncbin() {
  4385. // Allow the strings to have escaped octal character sequence.
  4386. std::string Filename;
  4387. SMLoc IncbinLoc = getTok().getLoc();
  4388. if (check(getTok().isNot(AsmToken::String),
  4389. "expected string in '.incbin' directive") ||
  4390. parseEscapedString(Filename))
  4391. return true;
  4392. int64_t Skip = 0;
  4393. const MCExpr *Count = nullptr;
  4394. SMLoc SkipLoc, CountLoc;
  4395. if (parseOptionalToken(AsmToken::Comma)) {
  4396. // The skip expression can be omitted while specifying the count, e.g:
  4397. // .incbin "filename",,4
  4398. if (getTok().isNot(AsmToken::Comma)) {
  4399. if (parseTokenLoc(SkipLoc) || parseAbsoluteExpression(Skip))
  4400. return true;
  4401. }
  4402. if (parseOptionalToken(AsmToken::Comma)) {
  4403. CountLoc = getTok().getLoc();
  4404. if (parseExpression(Count))
  4405. return true;
  4406. }
  4407. }
  4408. if (parseToken(AsmToken::EndOfStatement,
  4409. "unexpected token in '.incbin' directive"))
  4410. return true;
  4411. if (check(Skip < 0, SkipLoc, "skip is negative"))
  4412. return true;
  4413. // Attempt to process the included file.
  4414. if (processIncbinFile(Filename, Skip, Count, CountLoc))
  4415. return Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
  4416. return false;
  4417. }
  4418. /// parseDirectiveIf
  4419. /// ::= .if{,eq,ge,gt,le,lt,ne} expression
  4420. bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
  4421. TheCondStack.push_back(TheCondState);
  4422. TheCondState.TheCond = AsmCond::IfCond;
  4423. if (TheCondState.Ignore) {
  4424. eatToEndOfStatement();
  4425. } else {
  4426. int64_t ExprValue;
  4427. if (parseAbsoluteExpression(ExprValue) ||
  4428. parseToken(AsmToken::EndOfStatement,
  4429. "unexpected token in '.if' directive"))
  4430. return true;
  4431. switch (DirKind) {
  4432. default:
  4433. llvm_unreachable("unsupported directive");
  4434. case DK_IF:
  4435. case DK_IFNE:
  4436. break;
  4437. case DK_IFEQ:
  4438. ExprValue = ExprValue == 0;
  4439. break;
  4440. case DK_IFGE:
  4441. ExprValue = ExprValue >= 0;
  4442. break;
  4443. case DK_IFGT:
  4444. ExprValue = ExprValue > 0;
  4445. break;
  4446. case DK_IFLE:
  4447. ExprValue = ExprValue <= 0;
  4448. break;
  4449. case DK_IFLT:
  4450. ExprValue = ExprValue < 0;
  4451. break;
  4452. }
  4453. TheCondState.CondMet = ExprValue;
  4454. TheCondState.Ignore = !TheCondState.CondMet;
  4455. }
  4456. return false;
  4457. }
  4458. /// parseDirectiveIfb
  4459. /// ::= .ifb string
  4460. bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
  4461. TheCondStack.push_back(TheCondState);
  4462. TheCondState.TheCond = AsmCond::IfCond;
  4463. if (TheCondState.Ignore) {
  4464. eatToEndOfStatement();
  4465. } else {
  4466. StringRef Str = parseStringToEndOfStatement();
  4467. if (parseToken(AsmToken::EndOfStatement,
  4468. "unexpected token in '.ifb' directive"))
  4469. return true;
  4470. TheCondState.CondMet = ExpectBlank == Str.empty();
  4471. TheCondState.Ignore = !TheCondState.CondMet;
  4472. }
  4473. return false;
  4474. }
  4475. /// parseDirectiveIfc
  4476. /// ::= .ifc string1, string2
  4477. /// ::= .ifnc string1, string2
  4478. bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
  4479. TheCondStack.push_back(TheCondState);
  4480. TheCondState.TheCond = AsmCond::IfCond;
  4481. if (TheCondState.Ignore) {
  4482. eatToEndOfStatement();
  4483. } else {
  4484. StringRef Str1 = parseStringToComma();
  4485. if (parseToken(AsmToken::Comma, "unexpected token in '.ifc' directive"))
  4486. return true;
  4487. StringRef Str2 = parseStringToEndOfStatement();
  4488. if (parseToken(AsmToken::EndOfStatement,
  4489. "unexpected token in '.ifc' directive"))
  4490. return true;
  4491. TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
  4492. TheCondState.Ignore = !TheCondState.CondMet;
  4493. }
  4494. return false;
  4495. }
  4496. /// parseDirectiveIfeqs
  4497. /// ::= .ifeqs string1, string2
  4498. bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) {
  4499. if (Lexer.isNot(AsmToken::String)) {
  4500. if (ExpectEqual)
  4501. return TokError("expected string parameter for '.ifeqs' directive");
  4502. return TokError("expected string parameter for '.ifnes' directive");
  4503. }
  4504. StringRef String1 = getTok().getStringContents();
  4505. Lex();
  4506. if (Lexer.isNot(AsmToken::Comma)) {
  4507. if (ExpectEqual)
  4508. return TokError(
  4509. "expected comma after first string for '.ifeqs' directive");
  4510. return TokError("expected comma after first string for '.ifnes' directive");
  4511. }
  4512. Lex();
  4513. if (Lexer.isNot(AsmToken::String)) {
  4514. if (ExpectEqual)
  4515. return TokError("expected string parameter for '.ifeqs' directive");
  4516. return TokError("expected string parameter for '.ifnes' directive");
  4517. }
  4518. StringRef String2 = getTok().getStringContents();
  4519. Lex();
  4520. TheCondStack.push_back(TheCondState);
  4521. TheCondState.TheCond = AsmCond::IfCond;
  4522. TheCondState.CondMet = ExpectEqual == (String1 == String2);
  4523. TheCondState.Ignore = !TheCondState.CondMet;
  4524. return false;
  4525. }
  4526. /// parseDirectiveIfdef
  4527. /// ::= .ifdef symbol
  4528. bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
  4529. StringRef Name;
  4530. TheCondStack.push_back(TheCondState);
  4531. TheCondState.TheCond = AsmCond::IfCond;
  4532. if (TheCondState.Ignore) {
  4533. eatToEndOfStatement();
  4534. } else {
  4535. if (check(parseIdentifier(Name), "expected identifier after '.ifdef'") ||
  4536. parseToken(AsmToken::EndOfStatement, "unexpected token in '.ifdef'"))
  4537. return true;
  4538. MCSymbol *Sym = getContext().lookupSymbol(Name);
  4539. if (expect_defined)
  4540. TheCondState.CondMet = (Sym && !Sym->isUndefined(false));
  4541. else
  4542. TheCondState.CondMet = (!Sym || Sym->isUndefined(false));
  4543. TheCondState.Ignore = !TheCondState.CondMet;
  4544. }
  4545. return false;
  4546. }
  4547. /// parseDirectiveElseIf
  4548. /// ::= .elseif expression
  4549. bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
  4550. if (TheCondState.TheCond != AsmCond::IfCond &&
  4551. TheCondState.TheCond != AsmCond::ElseIfCond)
  4552. return Error(DirectiveLoc, "Encountered a .elseif that doesn't follow an"
  4553. " .if or an .elseif");
  4554. TheCondState.TheCond = AsmCond::ElseIfCond;
  4555. bool LastIgnoreState = false;
  4556. if (!TheCondStack.empty())
  4557. LastIgnoreState = TheCondStack.back().Ignore;
  4558. if (LastIgnoreState || TheCondState.CondMet) {
  4559. TheCondState.Ignore = true;
  4560. eatToEndOfStatement();
  4561. } else {
  4562. int64_t ExprValue;
  4563. if (parseAbsoluteExpression(ExprValue))
  4564. return true;
  4565. if (parseToken(AsmToken::EndOfStatement,
  4566. "unexpected token in '.elseif' directive"))
  4567. return true;
  4568. TheCondState.CondMet = ExprValue;
  4569. TheCondState.Ignore = !TheCondState.CondMet;
  4570. }
  4571. return false;
  4572. }
  4573. /// parseDirectiveElse
  4574. /// ::= .else
  4575. bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
  4576. if (parseToken(AsmToken::EndOfStatement,
  4577. "unexpected token in '.else' directive"))
  4578. return true;
  4579. if (TheCondState.TheCond != AsmCond::IfCond &&
  4580. TheCondState.TheCond != AsmCond::ElseIfCond)
  4581. return Error(DirectiveLoc, "Encountered a .else that doesn't follow "
  4582. " an .if or an .elseif");
  4583. TheCondState.TheCond = AsmCond::ElseCond;
  4584. bool LastIgnoreState = false;
  4585. if (!TheCondStack.empty())
  4586. LastIgnoreState = TheCondStack.back().Ignore;
  4587. if (LastIgnoreState || TheCondState.CondMet)
  4588. TheCondState.Ignore = true;
  4589. else
  4590. TheCondState.Ignore = false;
  4591. return false;
  4592. }
  4593. /// parseDirectiveEnd
  4594. /// ::= .end
  4595. bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
  4596. if (parseToken(AsmToken::EndOfStatement,
  4597. "unexpected token in '.end' directive"))
  4598. return true;
  4599. while (Lexer.isNot(AsmToken::Eof))
  4600. Lexer.Lex();
  4601. return false;
  4602. }
  4603. /// parseDirectiveError
  4604. /// ::= .err
  4605. /// ::= .error [string]
  4606. bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
  4607. if (!TheCondStack.empty()) {
  4608. if (TheCondStack.back().Ignore) {
  4609. eatToEndOfStatement();
  4610. return false;
  4611. }
  4612. }
  4613. if (!WithMessage)
  4614. return Error(L, ".err encountered");
  4615. StringRef Message = ".error directive invoked in source file";
  4616. if (Lexer.isNot(AsmToken::EndOfStatement)) {
  4617. if (Lexer.isNot(AsmToken::String))
  4618. return TokError(".error argument must be a string");
  4619. Message = getTok().getStringContents();
  4620. Lex();
  4621. }
  4622. return Error(L, Message);
  4623. }
  4624. /// parseDirectiveWarning
  4625. /// ::= .warning [string]
  4626. bool AsmParser::parseDirectiveWarning(SMLoc L) {
  4627. if (!TheCondStack.empty()) {
  4628. if (TheCondStack.back().Ignore) {
  4629. eatToEndOfStatement();
  4630. return false;
  4631. }
  4632. }
  4633. StringRef Message = ".warning directive invoked in source file";
  4634. if (!parseOptionalToken(AsmToken::EndOfStatement)) {
  4635. if (Lexer.isNot(AsmToken::String))
  4636. return TokError(".warning argument must be a string");
  4637. Message = getTok().getStringContents();
  4638. Lex();
  4639. if (parseToken(AsmToken::EndOfStatement,
  4640. "expected end of statement in '.warning' directive"))
  4641. return true;
  4642. }
  4643. return Warning(L, Message);
  4644. }
  4645. /// parseDirectiveEndIf
  4646. /// ::= .endif
  4647. bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
  4648. if (parseToken(AsmToken::EndOfStatement,
  4649. "unexpected token in '.endif' directive"))
  4650. return true;
  4651. if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
  4652. return Error(DirectiveLoc, "Encountered a .endif that doesn't follow "
  4653. "an .if or .else");
  4654. if (!TheCondStack.empty()) {
  4655. TheCondState = TheCondStack.back();
  4656. TheCondStack.pop_back();
  4657. }
  4658. return false;
  4659. }
  4660. void AsmParser::initializeDirectiveKindMap() {
  4661. /* Lookup will be done with the directive
  4662. * converted to lower case, so all these
  4663. * keys should be lower case.
  4664. * (target specific directives are handled
  4665. * elsewhere)
  4666. */
  4667. DirectiveKindMap[".set"] = DK_SET;
  4668. DirectiveKindMap[".equ"] = DK_EQU;
  4669. DirectiveKindMap[".equiv"] = DK_EQUIV;
  4670. DirectiveKindMap[".ascii"] = DK_ASCII;
  4671. DirectiveKindMap[".asciz"] = DK_ASCIZ;
  4672. DirectiveKindMap[".string"] = DK_STRING;
  4673. DirectiveKindMap[".byte"] = DK_BYTE;
  4674. DirectiveKindMap[".short"] = DK_SHORT;
  4675. DirectiveKindMap[".value"] = DK_VALUE;
  4676. DirectiveKindMap[".2byte"] = DK_2BYTE;
  4677. DirectiveKindMap[".long"] = DK_LONG;
  4678. DirectiveKindMap[".int"] = DK_INT;
  4679. DirectiveKindMap[".4byte"] = DK_4BYTE;
  4680. DirectiveKindMap[".quad"] = DK_QUAD;
  4681. DirectiveKindMap[".8byte"] = DK_8BYTE;
  4682. DirectiveKindMap[".octa"] = DK_OCTA;
  4683. DirectiveKindMap[".single"] = DK_SINGLE;
  4684. DirectiveKindMap[".float"] = DK_FLOAT;
  4685. DirectiveKindMap[".double"] = DK_DOUBLE;
  4686. DirectiveKindMap[".align"] = DK_ALIGN;
  4687. DirectiveKindMap[".align32"] = DK_ALIGN32;
  4688. DirectiveKindMap[".balign"] = DK_BALIGN;
  4689. DirectiveKindMap[".balignw"] = DK_BALIGNW;
  4690. DirectiveKindMap[".balignl"] = DK_BALIGNL;
  4691. DirectiveKindMap[".p2align"] = DK_P2ALIGN;
  4692. DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
  4693. DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
  4694. DirectiveKindMap[".org"] = DK_ORG;
  4695. DirectiveKindMap[".fill"] = DK_FILL;
  4696. DirectiveKindMap[".zero"] = DK_ZERO;
  4697. DirectiveKindMap[".extern"] = DK_EXTERN;
  4698. DirectiveKindMap[".globl"] = DK_GLOBL;
  4699. DirectiveKindMap[".global"] = DK_GLOBAL;
  4700. DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
  4701. DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
  4702. DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
  4703. DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
  4704. DirectiveKindMap[".reference"] = DK_REFERENCE;
  4705. DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
  4706. DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
  4707. DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
  4708. DirectiveKindMap[".cold"] = DK_COLD;
  4709. DirectiveKindMap[".comm"] = DK_COMM;
  4710. DirectiveKindMap[".common"] = DK_COMMON;
  4711. DirectiveKindMap[".lcomm"] = DK_LCOMM;
  4712. DirectiveKindMap[".abort"] = DK_ABORT;
  4713. DirectiveKindMap[".include"] = DK_INCLUDE;
  4714. DirectiveKindMap[".incbin"] = DK_INCBIN;
  4715. DirectiveKindMap[".code16"] = DK_CODE16;
  4716. DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
  4717. DirectiveKindMap[".rept"] = DK_REPT;
  4718. DirectiveKindMap[".rep"] = DK_REPT;
  4719. DirectiveKindMap[".irp"] = DK_IRP;
  4720. DirectiveKindMap[".irpc"] = DK_IRPC;
  4721. DirectiveKindMap[".endr"] = DK_ENDR;
  4722. DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
  4723. DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
  4724. DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
  4725. DirectiveKindMap[".if"] = DK_IF;
  4726. DirectiveKindMap[".ifeq"] = DK_IFEQ;
  4727. DirectiveKindMap[".ifge"] = DK_IFGE;
  4728. DirectiveKindMap[".ifgt"] = DK_IFGT;
  4729. DirectiveKindMap[".ifle"] = DK_IFLE;
  4730. DirectiveKindMap[".iflt"] = DK_IFLT;
  4731. DirectiveKindMap[".ifne"] = DK_IFNE;
  4732. DirectiveKindMap[".ifb"] = DK_IFB;
  4733. DirectiveKindMap[".ifnb"] = DK_IFNB;
  4734. DirectiveKindMap[".ifc"] = DK_IFC;
  4735. DirectiveKindMap[".ifeqs"] = DK_IFEQS;
  4736. DirectiveKindMap[".ifnc"] = DK_IFNC;
  4737. DirectiveKindMap[".ifnes"] = DK_IFNES;
  4738. DirectiveKindMap[".ifdef"] = DK_IFDEF;
  4739. DirectiveKindMap[".ifndef"] = DK_IFNDEF;
  4740. DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
  4741. DirectiveKindMap[".elseif"] = DK_ELSEIF;
  4742. DirectiveKindMap[".else"] = DK_ELSE;
  4743. DirectiveKindMap[".end"] = DK_END;
  4744. DirectiveKindMap[".endif"] = DK_ENDIF;
  4745. DirectiveKindMap[".skip"] = DK_SKIP;
  4746. DirectiveKindMap[".space"] = DK_SPACE;
  4747. DirectiveKindMap[".file"] = DK_FILE;
  4748. DirectiveKindMap[".line"] = DK_LINE;
  4749. DirectiveKindMap[".loc"] = DK_LOC;
  4750. DirectiveKindMap[".stabs"] = DK_STABS;
  4751. DirectiveKindMap[".cv_file"] = DK_CV_FILE;
  4752. DirectiveKindMap[".cv_func_id"] = DK_CV_FUNC_ID;
  4753. DirectiveKindMap[".cv_loc"] = DK_CV_LOC;
  4754. DirectiveKindMap[".cv_linetable"] = DK_CV_LINETABLE;
  4755. DirectiveKindMap[".cv_inline_linetable"] = DK_CV_INLINE_LINETABLE;
  4756. DirectiveKindMap[".cv_inline_site_id"] = DK_CV_INLINE_SITE_ID;
  4757. DirectiveKindMap[".cv_def_range"] = DK_CV_DEF_RANGE;
  4758. DirectiveKindMap[".cv_string"] = DK_CV_STRING;
  4759. DirectiveKindMap[".cv_stringtable"] = DK_CV_STRINGTABLE;
  4760. DirectiveKindMap[".cv_filechecksums"] = DK_CV_FILECHECKSUMS;
  4761. DirectiveKindMap[".cv_filechecksumoffset"] = DK_CV_FILECHECKSUM_OFFSET;
  4762. DirectiveKindMap[".cv_fpo_data"] = DK_CV_FPO_DATA;
  4763. DirectiveKindMap[".sleb128"] = DK_SLEB128;
  4764. DirectiveKindMap[".uleb128"] = DK_ULEB128;
  4765. DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
  4766. DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
  4767. DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
  4768. DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
  4769. DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
  4770. DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
  4771. DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
  4772. DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
  4773. DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
  4774. DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
  4775. DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
  4776. DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
  4777. DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
  4778. DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
  4779. DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
  4780. DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
  4781. DirectiveKindMap[".cfi_return_column"] = DK_CFI_RETURN_COLUMN;
  4782. DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
  4783. DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
  4784. DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
  4785. DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
  4786. DirectiveKindMap[".cfi_b_key_frame"] = DK_CFI_B_KEY_FRAME;
  4787. DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
  4788. DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
  4789. DirectiveKindMap[".macro"] = DK_MACRO;
  4790. DirectiveKindMap[".exitm"] = DK_EXITM;
  4791. DirectiveKindMap[".endm"] = DK_ENDM;
  4792. DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
  4793. DirectiveKindMap[".purgem"] = DK_PURGEM;
  4794. DirectiveKindMap[".err"] = DK_ERR;
  4795. DirectiveKindMap[".error"] = DK_ERROR;
  4796. DirectiveKindMap[".warning"] = DK_WARNING;
  4797. DirectiveKindMap[".altmacro"] = DK_ALTMACRO;
  4798. DirectiveKindMap[".noaltmacro"] = DK_NOALTMACRO;
  4799. DirectiveKindMap[".reloc"] = DK_RELOC;
  4800. DirectiveKindMap[".dc"] = DK_DC;
  4801. DirectiveKindMap[".dc.a"] = DK_DC_A;
  4802. DirectiveKindMap[".dc.b"] = DK_DC_B;
  4803. DirectiveKindMap[".dc.d"] = DK_DC_D;
  4804. DirectiveKindMap[".dc.l"] = DK_DC_L;
  4805. DirectiveKindMap[".dc.s"] = DK_DC_S;
  4806. DirectiveKindMap[".dc.w"] = DK_DC_W;
  4807. DirectiveKindMap[".dc.x"] = DK_DC_X;
  4808. DirectiveKindMap[".dcb"] = DK_DCB;
  4809. DirectiveKindMap[".dcb.b"] = DK_DCB_B;
  4810. DirectiveKindMap[".dcb.d"] = DK_DCB_D;
  4811. DirectiveKindMap[".dcb.l"] = DK_DCB_L;
  4812. DirectiveKindMap[".dcb.s"] = DK_DCB_S;
  4813. DirectiveKindMap[".dcb.w"] = DK_DCB_W;
  4814. DirectiveKindMap[".dcb.x"] = DK_DCB_X;
  4815. DirectiveKindMap[".ds"] = DK_DS;
  4816. DirectiveKindMap[".ds.b"] = DK_DS_B;
  4817. DirectiveKindMap[".ds.d"] = DK_DS_D;
  4818. DirectiveKindMap[".ds.l"] = DK_DS_L;
  4819. DirectiveKindMap[".ds.p"] = DK_DS_P;
  4820. DirectiveKindMap[".ds.s"] = DK_DS_S;
  4821. DirectiveKindMap[".ds.w"] = DK_DS_W;
  4822. DirectiveKindMap[".ds.x"] = DK_DS_X;
  4823. DirectiveKindMap[".print"] = DK_PRINT;
  4824. DirectiveKindMap[".addrsig"] = DK_ADDRSIG;
  4825. DirectiveKindMap[".addrsig_sym"] = DK_ADDRSIG_SYM;
  4826. DirectiveKindMap[".pseudoprobe"] = DK_PSEUDO_PROBE;
  4827. }
  4828. MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
  4829. AsmToken EndToken, StartToken = getTok();
  4830. unsigned NestLevel = 0;
  4831. while (true) {
  4832. // Check whether we have reached the end of the file.
  4833. if (getLexer().is(AsmToken::Eof)) {
  4834. printError(DirectiveLoc, "no matching '.endr' in definition");
  4835. return nullptr;
  4836. }
  4837. if (Lexer.is(AsmToken::Identifier) &&
  4838. (getTok().getIdentifier() == ".rep" ||
  4839. getTok().getIdentifier() == ".rept" ||
  4840. getTok().getIdentifier() == ".irp" ||
  4841. getTok().getIdentifier() == ".irpc")) {
  4842. ++NestLevel;
  4843. }
  4844. // Otherwise, check whether we have reached the .endr.
  4845. if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
  4846. if (NestLevel == 0) {
  4847. EndToken = getTok();
  4848. Lex();
  4849. if (Lexer.isNot(AsmToken::EndOfStatement)) {
  4850. printError(getTok().getLoc(),
  4851. "unexpected token in '.endr' directive");
  4852. return nullptr;
  4853. }
  4854. break;
  4855. }
  4856. --NestLevel;
  4857. }
  4858. // Otherwise, scan till the end of the statement.
  4859. eatToEndOfStatement();
  4860. }
  4861. const char *BodyStart = StartToken.getLoc().getPointer();
  4862. const char *BodyEnd = EndToken.getLoc().getPointer();
  4863. StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
  4864. // We Are Anonymous.
  4865. MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters());
  4866. return &MacroLikeBodies.back();
  4867. }
  4868. void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
  4869. raw_svector_ostream &OS) {
  4870. OS << ".endr\n";
  4871. std::unique_ptr<MemoryBuffer> Instantiation =
  4872. MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
  4873. // Create the macro instantiation object and add to the current macro
  4874. // instantiation stack.
  4875. MacroInstantiation *MI = new MacroInstantiation{
  4876. DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()};
  4877. ActiveMacros.push_back(MI);
  4878. // Jump to the macro instantiation and prime the lexer.
  4879. CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
  4880. Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
  4881. Lex();
  4882. }
  4883. /// parseDirectiveRept
  4884. /// ::= .rep | .rept count
  4885. bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
  4886. const MCExpr *CountExpr;
  4887. SMLoc CountLoc = getTok().getLoc();
  4888. if (parseExpression(CountExpr))
  4889. return true;
  4890. int64_t Count;
  4891. if (!CountExpr->evaluateAsAbsolute(Count, getStreamer().getAssemblerPtr())) {
  4892. return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
  4893. }
  4894. if (check(Count < 0, CountLoc, "Count is negative") ||
  4895. parseToken(AsmToken::EndOfStatement,
  4896. "unexpected token in '" + Dir + "' directive"))
  4897. return true;
  4898. // Lex the rept definition.
  4899. MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
  4900. if (!M)
  4901. return true;
  4902. // Macro instantiation is lexical, unfortunately. We construct a new buffer
  4903. // to hold the macro body with substitutions.
  4904. SmallString<256> Buf;
  4905. raw_svector_ostream OS(Buf);
  4906. while (Count--) {
  4907. // Note that the AtPseudoVariable is disabled for instantiations of .rep(t).
  4908. if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc()))
  4909. return true;
  4910. }
  4911. instantiateMacroLikeBody(M, DirectiveLoc, OS);
  4912. return false;
  4913. }
  4914. /// parseDirectiveIrp
  4915. /// ::= .irp symbol,values
  4916. bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
  4917. MCAsmMacroParameter Parameter;
  4918. MCAsmMacroArguments A;
  4919. if (check(parseIdentifier(Parameter.Name),
  4920. "expected identifier in '.irp' directive") ||
  4921. parseToken(AsmToken::Comma, "expected comma in '.irp' directive") ||
  4922. parseMacroArguments(nullptr, A) ||
  4923. parseToken(AsmToken::EndOfStatement, "expected End of Statement"))
  4924. return true;
  4925. // Lex the irp definition.
  4926. MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
  4927. if (!M)
  4928. return true;
  4929. // Macro instantiation is lexical, unfortunately. We construct a new buffer
  4930. // to hold the macro body with substitutions.
  4931. SmallString<256> Buf;
  4932. raw_svector_ostream OS(Buf);
  4933. for (const MCAsmMacroArgument &Arg : A) {
  4934. // Note that the AtPseudoVariable is enabled for instantiations of .irp.
  4935. // This is undocumented, but GAS seems to support it.
  4936. if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
  4937. return true;
  4938. }
  4939. instantiateMacroLikeBody(M, DirectiveLoc, OS);
  4940. return false;
  4941. }
  4942. /// parseDirectiveIrpc
  4943. /// ::= .irpc symbol,values
  4944. bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
  4945. MCAsmMacroParameter Parameter;
  4946. MCAsmMacroArguments A;
  4947. if (check(parseIdentifier(Parameter.Name),
  4948. "expected identifier in '.irpc' directive") ||
  4949. parseToken(AsmToken::Comma, "expected comma in '.irpc' directive") ||
  4950. parseMacroArguments(nullptr, A))
  4951. return true;
  4952. if (A.size() != 1 || A.front().size() != 1)
  4953. return TokError("unexpected token in '.irpc' directive");
  4954. // Eat the end of statement.
  4955. if (parseToken(AsmToken::EndOfStatement, "expected end of statement"))
  4956. return true;
  4957. // Lex the irpc definition.
  4958. MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
  4959. if (!M)
  4960. return true;
  4961. // Macro instantiation is lexical, unfortunately. We construct a new buffer
  4962. // to hold the macro body with substitutions.
  4963. SmallString<256> Buf;
  4964. raw_svector_ostream OS(Buf);
  4965. StringRef Values = A.front().front().getString();
  4966. for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
  4967. MCAsmMacroArgument Arg;
  4968. Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1));
  4969. // Note that the AtPseudoVariable is enabled for instantiations of .irpc.
  4970. // This is undocumented, but GAS seems to support it.
  4971. if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
  4972. return true;
  4973. }
  4974. instantiateMacroLikeBody(M, DirectiveLoc, OS);
  4975. return false;
  4976. }
  4977. bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
  4978. if (ActiveMacros.empty())
  4979. return TokError("unmatched '.endr' directive");
  4980. // The only .repl that should get here are the ones created by
  4981. // instantiateMacroLikeBody.
  4982. assert(getLexer().is(AsmToken::EndOfStatement));
  4983. handleMacroExit();
  4984. return false;
  4985. }
  4986. bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
  4987. size_t Len) {
  4988. const MCExpr *Value;
  4989. SMLoc ExprLoc = getLexer().getLoc();
  4990. if (parseExpression(Value))
  4991. return true;
  4992. const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
  4993. if (!MCE)
  4994. return Error(ExprLoc, "unexpected expression in _emit");
  4995. uint64_t IntValue = MCE->getValue();
  4996. if (!isUInt<8>(IntValue) && !isInt<8>(IntValue))
  4997. return Error(ExprLoc, "literal value out of range for directive");
  4998. Info.AsmRewrites->emplace_back(AOK_Emit, IDLoc, Len);
  4999. return false;
  5000. }
  5001. bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
  5002. const MCExpr *Value;
  5003. SMLoc ExprLoc = getLexer().getLoc();
  5004. if (parseExpression(Value))
  5005. return true;
  5006. const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
  5007. if (!MCE)
  5008. return Error(ExprLoc, "unexpected expression in align");
  5009. uint64_t IntValue = MCE->getValue();
  5010. if (!isPowerOf2_64(IntValue))
  5011. return Error(ExprLoc, "literal value not a power of two greater then zero");
  5012. Info.AsmRewrites->emplace_back(AOK_Align, IDLoc, 5, Log2_64(IntValue));
  5013. return false;
  5014. }
  5015. bool AsmParser::parseDirectivePrint(SMLoc DirectiveLoc) {
  5016. const AsmToken StrTok = getTok();
  5017. Lex();
  5018. if (StrTok.isNot(AsmToken::String) || StrTok.getString().front() != '"')
  5019. return Error(DirectiveLoc, "expected double quoted string after .print");
  5020. if (parseToken(AsmToken::EndOfStatement, "expected end of statement"))
  5021. return true;
  5022. llvm::outs() << StrTok.getStringContents() << '\n';
  5023. return false;
  5024. }
  5025. bool AsmParser::parseDirectiveAddrsig() {
  5026. getStreamer().emitAddrsig();
  5027. return false;
  5028. }
  5029. bool AsmParser::parseDirectiveAddrsigSym() {
  5030. StringRef Name;
  5031. if (check(parseIdentifier(Name),
  5032. "expected identifier in '.addrsig_sym' directive"))
  5033. return true;
  5034. MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
  5035. getStreamer().emitAddrsigSym(Sym);
  5036. return false;
  5037. }
  5038. bool AsmParser::parseDirectivePseudoProbe() {
  5039. int64_t Guid;
  5040. int64_t Index;
  5041. int64_t Type;
  5042. int64_t Attr;
  5043. if (getLexer().is(AsmToken::Integer)) {
  5044. if (parseIntToken(Guid, "unexpected token in '.pseudoprobe' directive"))
  5045. return true;
  5046. }
  5047. if (getLexer().is(AsmToken::Integer)) {
  5048. if (parseIntToken(Index, "unexpected token in '.pseudoprobe' directive"))
  5049. return true;
  5050. }
  5051. if (getLexer().is(AsmToken::Integer)) {
  5052. if (parseIntToken(Type, "unexpected token in '.pseudoprobe' directive"))
  5053. return true;
  5054. }
  5055. if (getLexer().is(AsmToken::Integer)) {
  5056. if (parseIntToken(Attr, "unexpected token in '.pseudoprobe' directive"))
  5057. return true;
  5058. }
  5059. // Parse inline stack like @ GUID:11:12 @ GUID:1:11 @ GUID:3:21
  5060. MCPseudoProbeInlineStack InlineStack;
  5061. while (getLexer().is(AsmToken::At)) {
  5062. // eat @
  5063. Lex();
  5064. int64_t CallerGuid = 0;
  5065. if (getLexer().is(AsmToken::Integer)) {
  5066. if (parseIntToken(CallerGuid,
  5067. "unexpected token in '.pseudoprobe' directive"))
  5068. return true;
  5069. }
  5070. // eat colon
  5071. if (getLexer().is(AsmToken::Colon))
  5072. Lex();
  5073. int64_t CallerProbeId = 0;
  5074. if (getLexer().is(AsmToken::Integer)) {
  5075. if (parseIntToken(CallerProbeId,
  5076. "unexpected token in '.pseudoprobe' directive"))
  5077. return true;
  5078. }
  5079. InlineSite Site(CallerGuid, CallerProbeId);
  5080. InlineStack.push_back(Site);
  5081. }
  5082. if (parseToken(AsmToken::EndOfStatement,
  5083. "unexpected token in '.pseudoprobe' directive"))
  5084. return true;
  5085. getStreamer().emitPseudoProbe(Guid, Index, Type, Attr, InlineStack);
  5086. return false;
  5087. }
  5088. // We are comparing pointers, but the pointers are relative to a single string.
  5089. // Thus, this should always be deterministic.
  5090. static int rewritesSort(const AsmRewrite *AsmRewriteA,
  5091. const AsmRewrite *AsmRewriteB) {
  5092. if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
  5093. return -1;
  5094. if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
  5095. return 1;
  5096. // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
  5097. // rewrite to the same location. Make sure the SizeDirective rewrite is
  5098. // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
  5099. // ensures the sort algorithm is stable.
  5100. if (AsmRewritePrecedence[AsmRewriteA->Kind] >
  5101. AsmRewritePrecedence[AsmRewriteB->Kind])
  5102. return -1;
  5103. if (AsmRewritePrecedence[AsmRewriteA->Kind] <
  5104. AsmRewritePrecedence[AsmRewriteB->Kind])
  5105. return 1;
  5106. llvm_unreachable("Unstable rewrite sort.");
  5107. }
  5108. bool AsmParser::parseMSInlineAsm(
  5109. void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
  5110. unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool>> &OpDecls,
  5111. SmallVectorImpl<std::string> &Constraints,
  5112. SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
  5113. const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
  5114. SmallVector<void *, 4> InputDecls;
  5115. SmallVector<void *, 4> OutputDecls;
  5116. SmallVector<bool, 4> InputDeclsAddressOf;
  5117. SmallVector<bool, 4> OutputDeclsAddressOf;
  5118. SmallVector<std::string, 4> InputConstraints;
  5119. SmallVector<std::string, 4> OutputConstraints;
  5120. SmallVector<unsigned, 4> ClobberRegs;
  5121. SmallVector<AsmRewrite, 4> AsmStrRewrites;
  5122. // Prime the lexer.
  5123. Lex();
  5124. // While we have input, parse each statement.
  5125. unsigned InputIdx = 0;
  5126. unsigned OutputIdx = 0;
  5127. while (getLexer().isNot(AsmToken::Eof)) {
  5128. // Parse curly braces marking block start/end
  5129. if (parseCurlyBlockScope(AsmStrRewrites))
  5130. continue;
  5131. ParseStatementInfo Info(&AsmStrRewrites);
  5132. bool StatementErr = parseStatement(Info, &SI);
  5133. if (StatementErr || Info.ParseError) {
  5134. // Emit pending errors if any exist.
  5135. printPendingErrors();
  5136. return true;
  5137. }
  5138. // No pending error should exist here.
  5139. assert(!hasPendingError() && "unexpected error from parseStatement");
  5140. if (Info.Opcode == ~0U)
  5141. continue;
  5142. const MCInstrDesc &Desc = MII->get(Info.Opcode);
  5143. // Build the list of clobbers, outputs and inputs.
  5144. for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
  5145. MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
  5146. // Register operand.
  5147. if (Operand.isReg() && !Operand.needAddressOf() &&
  5148. !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
  5149. unsigned NumDefs = Desc.getNumDefs();
  5150. // Clobber.
  5151. if (NumDefs && Operand.getMCOperandNum() < NumDefs)
  5152. ClobberRegs.push_back(Operand.getReg());
  5153. continue;
  5154. }
  5155. // Expr/Input or Output.
  5156. StringRef SymName = Operand.getSymName();
  5157. if (SymName.empty())
  5158. continue;
  5159. void *OpDecl = Operand.getOpDecl();
  5160. if (!OpDecl)
  5161. continue;
  5162. StringRef Constraint = Operand.getConstraint();
  5163. if (Operand.isImm()) {
  5164. // Offset as immediate
  5165. if (Operand.isOffsetOfLocal())
  5166. Constraint = "r";
  5167. else
  5168. Constraint = "i";
  5169. }
  5170. bool isOutput = (i == 1) && Desc.mayStore();
  5171. SMLoc Start = SMLoc::getFromPointer(SymName.data());
  5172. if (isOutput) {
  5173. ++InputIdx;
  5174. OutputDecls.push_back(OpDecl);
  5175. OutputDeclsAddressOf.push_back(Operand.needAddressOf());
  5176. OutputConstraints.push_back(("=" + Constraint).str());
  5177. AsmStrRewrites.emplace_back(AOK_Output, Start, SymName.size());
  5178. } else {
  5179. InputDecls.push_back(OpDecl);
  5180. InputDeclsAddressOf.push_back(Operand.needAddressOf());
  5181. InputConstraints.push_back(Constraint.str());
  5182. if (Desc.OpInfo[i - 1].isBranchTarget())
  5183. AsmStrRewrites.emplace_back(AOK_CallInput, Start, SymName.size());
  5184. else
  5185. AsmStrRewrites.emplace_back(AOK_Input, Start, SymName.size());
  5186. }
  5187. }
  5188. // Consider implicit defs to be clobbers. Think of cpuid and push.
  5189. ArrayRef<MCPhysReg> ImpDefs(Desc.getImplicitDefs(),
  5190. Desc.getNumImplicitDefs());
  5191. llvm::append_range(ClobberRegs, ImpDefs);
  5192. }
  5193. // Set the number of Outputs and Inputs.
  5194. NumOutputs = OutputDecls.size();
  5195. NumInputs = InputDecls.size();
  5196. // Set the unique clobbers.
  5197. array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
  5198. ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
  5199. ClobberRegs.end());
  5200. Clobbers.assign(ClobberRegs.size(), std::string());
  5201. for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
  5202. raw_string_ostream OS(Clobbers[I]);
  5203. IP->printRegName(OS, ClobberRegs[I]);
  5204. }
  5205. // Merge the various outputs and inputs. Output are expected first.
  5206. if (NumOutputs || NumInputs) {
  5207. unsigned NumExprs = NumOutputs + NumInputs;
  5208. OpDecls.resize(NumExprs);
  5209. Constraints.resize(NumExprs);
  5210. for (unsigned i = 0; i < NumOutputs; ++i) {
  5211. OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
  5212. Constraints[i] = OutputConstraints[i];
  5213. }
  5214. for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
  5215. OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
  5216. Constraints[j] = InputConstraints[i];
  5217. }
  5218. }
  5219. // Build the IR assembly string.
  5220. std::string AsmStringIR;
  5221. raw_string_ostream OS(AsmStringIR);
  5222. StringRef ASMString =
  5223. SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
  5224. const char *AsmStart = ASMString.begin();
  5225. const char *AsmEnd = ASMString.end();
  5226. array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
  5227. for (auto it = AsmStrRewrites.begin(); it != AsmStrRewrites.end(); ++it) {
  5228. const AsmRewrite &AR = *it;
  5229. // Check if this has already been covered by another rewrite...
  5230. if (AR.Done)
  5231. continue;
  5232. AsmRewriteKind Kind = AR.Kind;
  5233. const char *Loc = AR.Loc.getPointer();
  5234. assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
  5235. // Emit everything up to the immediate/expression.
  5236. if (unsigned Len = Loc - AsmStart)
  5237. OS << StringRef(AsmStart, Len);
  5238. // Skip the original expression.
  5239. if (Kind == AOK_Skip) {
  5240. AsmStart = Loc + AR.Len;
  5241. continue;
  5242. }
  5243. unsigned AdditionalSkip = 0;
  5244. // Rewrite expressions in $N notation.
  5245. switch (Kind) {
  5246. default:
  5247. break;
  5248. case AOK_IntelExpr:
  5249. assert(AR.IntelExp.isValid() && "cannot write invalid intel expression");
  5250. if (AR.IntelExp.NeedBracs)
  5251. OS << "[";
  5252. if (AR.IntelExp.hasBaseReg())
  5253. OS << AR.IntelExp.BaseReg;
  5254. if (AR.IntelExp.hasIndexReg())
  5255. OS << (AR.IntelExp.hasBaseReg() ? " + " : "")
  5256. << AR.IntelExp.IndexReg;
  5257. if (AR.IntelExp.Scale > 1)
  5258. OS << " * $$" << AR.IntelExp.Scale;
  5259. if (AR.IntelExp.hasOffset()) {
  5260. if (AR.IntelExp.hasRegs())
  5261. OS << " + ";
  5262. // Fuse this rewrite with a rewrite of the offset name, if present.
  5263. StringRef OffsetName = AR.IntelExp.OffsetName;
  5264. SMLoc OffsetLoc = SMLoc::getFromPointer(AR.IntelExp.OffsetName.data());
  5265. size_t OffsetLen = OffsetName.size();
  5266. auto rewrite_it = std::find_if(
  5267. it, AsmStrRewrites.end(), [&](const AsmRewrite &FusingAR) {
  5268. return FusingAR.Loc == OffsetLoc && FusingAR.Len == OffsetLen &&
  5269. (FusingAR.Kind == AOK_Input ||
  5270. FusingAR.Kind == AOK_CallInput);
  5271. });
  5272. if (rewrite_it == AsmStrRewrites.end()) {
  5273. OS << "offset " << OffsetName;
  5274. } else if (rewrite_it->Kind == AOK_CallInput) {
  5275. OS << "${" << InputIdx++ << ":P}";
  5276. rewrite_it->Done = true;
  5277. } else {
  5278. OS << '$' << InputIdx++;
  5279. rewrite_it->Done = true;
  5280. }
  5281. }
  5282. if (AR.IntelExp.Imm || AR.IntelExp.emitImm())
  5283. OS << (AR.IntelExp.emitImm() ? "$$" : " + $$") << AR.IntelExp.Imm;
  5284. if (AR.IntelExp.NeedBracs)
  5285. OS << "]";
  5286. break;
  5287. case AOK_Label:
  5288. OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
  5289. break;
  5290. case AOK_Input:
  5291. OS << '$' << InputIdx++;
  5292. break;
  5293. case AOK_CallInput:
  5294. OS << "${" << InputIdx++ << ":P}";
  5295. break;
  5296. case AOK_Output:
  5297. OS << '$' << OutputIdx++;
  5298. break;
  5299. case AOK_SizeDirective:
  5300. switch (AR.Val) {
  5301. default: break;
  5302. case 8: OS << "byte ptr "; break;
  5303. case 16: OS << "word ptr "; break;
  5304. case 32: OS << "dword ptr "; break;
  5305. case 64: OS << "qword ptr "; break;
  5306. case 80: OS << "xword ptr "; break;
  5307. case 128: OS << "xmmword ptr "; break;
  5308. case 256: OS << "ymmword ptr "; break;
  5309. }
  5310. break;
  5311. case AOK_Emit:
  5312. OS << ".byte";
  5313. break;
  5314. case AOK_Align: {
  5315. // MS alignment directives are measured in bytes. If the native assembler
  5316. // measures alignment in bytes, we can pass it straight through.
  5317. OS << ".align";
  5318. if (getContext().getAsmInfo()->getAlignmentIsInBytes())
  5319. break;
  5320. // Alignment is in log2 form, so print that instead and skip the original
  5321. // immediate.
  5322. unsigned Val = AR.Val;
  5323. OS << ' ' << Val;
  5324. assert(Val < 10 && "Expected alignment less then 2^10.");
  5325. AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
  5326. break;
  5327. }
  5328. case AOK_EVEN:
  5329. OS << ".even";
  5330. break;
  5331. case AOK_EndOfStatement:
  5332. OS << "\n\t";
  5333. break;
  5334. }
  5335. // Skip the original expression.
  5336. AsmStart = Loc + AR.Len + AdditionalSkip;
  5337. }
  5338. // Emit the remainder of the asm string.
  5339. if (AsmStart != AsmEnd)
  5340. OS << StringRef(AsmStart, AsmEnd - AsmStart);
  5341. AsmString = OS.str();
  5342. return false;
  5343. }
  5344. namespace llvm {
  5345. namespace MCParserUtils {
  5346. /// Returns whether the given symbol is used anywhere in the given expression,
  5347. /// or subexpressions.
  5348. static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) {
  5349. switch (Value->getKind()) {
  5350. case MCExpr::Binary: {
  5351. const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
  5352. return isSymbolUsedInExpression(Sym, BE->getLHS()) ||
  5353. isSymbolUsedInExpression(Sym, BE->getRHS());
  5354. }
  5355. case MCExpr::Target:
  5356. case MCExpr::Constant:
  5357. return false;
  5358. case MCExpr::SymbolRef: {
  5359. const MCSymbol &S =
  5360. static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
  5361. if (S.isVariable())
  5362. return isSymbolUsedInExpression(Sym, S.getVariableValue());
  5363. return &S == Sym;
  5364. }
  5365. case MCExpr::Unary:
  5366. return isSymbolUsedInExpression(
  5367. Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
  5368. }
  5369. llvm_unreachable("Unknown expr kind!");
  5370. }
  5371. bool parseAssignmentExpression(StringRef Name, bool allow_redef,
  5372. MCAsmParser &Parser, MCSymbol *&Sym,
  5373. const MCExpr *&Value) {
  5374. // FIXME: Use better location, we should use proper tokens.
  5375. SMLoc EqualLoc = Parser.getTok().getLoc();
  5376. if (Parser.parseExpression(Value))
  5377. return Parser.TokError("missing expression");
  5378. // Note: we don't count b as used in "a = b". This is to allow
  5379. // a = b
  5380. // b = c
  5381. if (Parser.parseToken(AsmToken::EndOfStatement))
  5382. return true;
  5383. // Validate that the LHS is allowed to be a variable (either it has not been
  5384. // used as a symbol, or it is an absolute symbol).
  5385. Sym = Parser.getContext().lookupSymbol(Name);
  5386. if (Sym) {
  5387. // Diagnose assignment to a label.
  5388. //
  5389. // FIXME: Diagnostics. Note the location of the definition as a label.
  5390. // FIXME: Diagnose assignment to protected identifier (e.g., register name).
  5391. if (isSymbolUsedInExpression(Sym, Value))
  5392. return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'");
  5393. else if (Sym->isUndefined(/*SetUsed*/ false) && !Sym->isUsed() &&
  5394. !Sym->isVariable())
  5395. ; // Allow redefinitions of undefined symbols only used in directives.
  5396. else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
  5397. ; // Allow redefinitions of variables that haven't yet been used.
  5398. else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
  5399. return Parser.Error(EqualLoc, "redefinition of '" + Name + "'");
  5400. else if (!Sym->isVariable())
  5401. return Parser.Error(EqualLoc, "invalid assignment to '" + Name + "'");
  5402. else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
  5403. return Parser.Error(EqualLoc,
  5404. "invalid reassignment of non-absolute variable '" +
  5405. Name + "'");
  5406. } else if (Name == ".") {
  5407. Parser.getStreamer().emitValueToOffset(Value, 0, EqualLoc);
  5408. return false;
  5409. } else
  5410. Sym = Parser.getContext().getOrCreateSymbol(Name);
  5411. Sym->setRedefinable(allow_redef);
  5412. return false;
  5413. }
  5414. } // end namespace MCParserUtils
  5415. } // end namespace llvm
  5416. /// Create an MCAsmParser instance.
  5417. MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
  5418. MCStreamer &Out, const MCAsmInfo &MAI,
  5419. unsigned CB) {
  5420. return new AsmParser(SM, C, Out, MAI, CB);
  5421. }