AsmParser.cpp 151 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844
  1. //===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This class implements the parser for assembly files.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/ADT/APFloat.h"
  14. #include "llvm/ADT/STLExtras.h"
  15. #include "llvm/ADT/SmallString.h"
  16. #include "llvm/ADT/StringMap.h"
  17. #include "llvm/ADT/Twine.h"
  18. #include "llvm/MC/MCAsmInfo.h"
  19. #include "llvm/MC/MCContext.h"
  20. #include "llvm/MC/MCDwarf.h"
  21. #include "llvm/MC/MCExpr.h"
  22. #include "llvm/MC/MCInstPrinter.h"
  23. #include "llvm/MC/MCInstrInfo.h"
  24. #include "llvm/MC/MCObjectFileInfo.h"
  25. #include "llvm/MC/MCParser/AsmCond.h"
  26. #include "llvm/MC/MCParser/AsmLexer.h"
  27. #include "llvm/MC/MCParser/MCAsmParser.h"
  28. #include "llvm/MC/MCParser/MCAsmParserUtils.h"
  29. #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
  30. #include "llvm/MC/MCRegisterInfo.h"
  31. #include "llvm/MC/MCSectionMachO.h"
  32. #include "llvm/MC/MCStreamer.h"
  33. #include "llvm/MC/MCSymbol.h"
  34. #include "llvm/MC/MCTargetAsmParser.h"
  35. #include "llvm/Support/CommandLine.h"
  36. #include "llvm/Support/ErrorHandling.h"
  37. #include "llvm/Support/MathExtras.h"
  38. #include "llvm/Support/MemoryBuffer.h"
  39. #include "llvm/Support/SourceMgr.h"
  40. #include "llvm/Support/raw_ostream.h"
  41. #include <cctype>
  42. #include <deque>
  43. #include <set>
  44. #include <string>
  45. #include <vector>
  46. using namespace llvm;
  47. MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
  48. namespace {
  49. /// \brief Helper types for tracking macro definitions.
  50. typedef std::vector<AsmToken> MCAsmMacroArgument;
  51. typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
  52. struct MCAsmMacroParameter {
  53. StringRef Name;
  54. MCAsmMacroArgument Value;
  55. bool Required;
  56. bool Vararg;
  57. MCAsmMacroParameter() : Required(false), Vararg(false) {}
  58. };
  59. typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
  60. struct MCAsmMacro {
  61. StringRef Name;
  62. StringRef Body;
  63. MCAsmMacroParameters Parameters;
  64. public:
  65. MCAsmMacro(StringRef N, StringRef B, MCAsmMacroParameters P)
  66. : Name(N), Body(B), Parameters(std::move(P)) {}
  67. };
  68. /// \brief Helper class for storing information about an active macro
  69. /// instantiation.
  70. struct MacroInstantiation {
  71. /// The location of the instantiation.
  72. SMLoc InstantiationLoc;
  73. /// The buffer where parsing should resume upon instantiation completion.
  74. int ExitBuffer;
  75. /// The location where parsing should resume upon instantiation completion.
  76. SMLoc ExitLoc;
  77. /// The depth of TheCondStack at the start of the instantiation.
  78. size_t CondStackDepth;
  79. public:
  80. MacroInstantiation(SMLoc IL, int EB, SMLoc EL, size_t CondStackDepth);
  81. };
  82. struct ParseStatementInfo {
  83. /// \brief The parsed operands from the last parsed statement.
  84. SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands;
  85. /// \brief The opcode from the last parsed instruction.
  86. unsigned Opcode;
  87. /// \brief Was there an error parsing the inline assembly?
  88. bool ParseError;
  89. SmallVectorImpl<AsmRewrite> *AsmRewrites;
  90. ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(nullptr) {}
  91. ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
  92. : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
  93. };
  94. /// \brief The concrete assembly parser instance.
  95. class AsmParser : public MCAsmParser {
  96. AsmParser(const AsmParser &) = delete;
  97. void operator=(const AsmParser &) = delete;
  98. private:
  99. AsmLexer Lexer;
  100. MCContext &Ctx;
  101. MCStreamer &Out;
  102. const MCAsmInfo &MAI;
  103. SourceMgr &SrcMgr;
  104. SourceMgr::DiagHandlerTy SavedDiagHandler;
  105. void *SavedDiagContext;
  106. std::unique_ptr<MCAsmParserExtension> PlatformParser;
  107. /// This is the current buffer index we're lexing from as managed by the
  108. /// SourceMgr object.
  109. unsigned CurBuffer;
  110. AsmCond TheCondState;
  111. std::vector<AsmCond> TheCondStack;
  112. /// \brief maps directive names to handler methods in parser
  113. /// extensions. Extensions register themselves in this map by calling
  114. /// addDirectiveHandler.
  115. StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
  116. /// \brief Map of currently defined macros.
  117. StringMap<MCAsmMacro> MacroMap;
  118. /// \brief Stack of active macro instantiations.
  119. std::vector<MacroInstantiation*> ActiveMacros;
  120. /// \brief List of bodies of anonymous macros.
  121. std::deque<MCAsmMacro> MacroLikeBodies;
  122. /// Boolean tracking whether macro substitution is enabled.
  123. unsigned MacrosEnabledFlag : 1;
  124. /// \brief Keeps track of how many .macro's have been instantiated.
  125. unsigned NumOfMacroInstantiations;
  126. /// Flag tracking whether any errors have been encountered.
  127. unsigned HadError : 1;
  128. /// The values from the last parsed cpp hash file line comment if any.
  129. StringRef CppHashFilename;
  130. int64_t CppHashLineNumber;
  131. SMLoc CppHashLoc;
  132. unsigned CppHashBuf;
  133. /// When generating dwarf for assembly source files we need to calculate the
  134. /// logical line number based on the last parsed cpp hash file line comment
  135. /// and current line. Since this is slow and messes up the SourceMgr's
  136. /// cache we save the last info we queried with SrcMgr.FindLineNumber().
  137. SMLoc LastQueryIDLoc;
  138. unsigned LastQueryBuffer;
  139. unsigned LastQueryLine;
  140. /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
  141. unsigned AssemblerDialect;
  142. /// \brief is Darwin compatibility enabled?
  143. bool IsDarwin;
  144. /// \brief Are we parsing ms-style inline assembly?
  145. bool ParsingInlineAsm;
  146. public:
  147. AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
  148. const MCAsmInfo &MAI);
  149. ~AsmParser() override;
  150. bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
  151. void addDirectiveHandler(StringRef Directive,
  152. ExtensionDirectiveHandler Handler) override {
  153. ExtensionDirectiveMap[Directive] = Handler;
  154. }
  155. void addAliasForDirective(StringRef Directive, StringRef Alias) override {
  156. DirectiveKindMap[Directive] = DirectiveKindMap[Alias];
  157. }
  158. public:
  159. /// @name MCAsmParser Interface
  160. /// {
  161. SourceMgr &getSourceManager() override { return SrcMgr; }
  162. MCAsmLexer &getLexer() override { return Lexer; }
  163. MCContext &getContext() override { return Ctx; }
  164. MCStreamer &getStreamer() override { return Out; }
  165. unsigned getAssemblerDialect() override {
  166. if (AssemblerDialect == ~0U)
  167. return MAI.getAssemblerDialect();
  168. else
  169. return AssemblerDialect;
  170. }
  171. void setAssemblerDialect(unsigned i) override {
  172. AssemblerDialect = i;
  173. }
  174. void Note(SMLoc L, const Twine &Msg,
  175. ArrayRef<SMRange> Ranges = None) override;
  176. bool Warning(SMLoc L, const Twine &Msg,
  177. ArrayRef<SMRange> Ranges = None) override;
  178. bool Error(SMLoc L, const Twine &Msg,
  179. ArrayRef<SMRange> Ranges = None) override;
  180. const AsmToken &Lex() override;
  181. void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
  182. bool isParsingInlineAsm() override { return ParsingInlineAsm; }
  183. bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
  184. unsigned &NumOutputs, unsigned &NumInputs,
  185. SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
  186. SmallVectorImpl<std::string> &Constraints,
  187. SmallVectorImpl<std::string> &Clobbers,
  188. const MCInstrInfo *MII, const MCInstPrinter *IP,
  189. MCAsmParserSemaCallback &SI) override;
  190. bool parseExpression(const MCExpr *&Res);
  191. bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
  192. bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
  193. bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
  194. bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
  195. SMLoc &EndLoc) override;
  196. bool parseAbsoluteExpression(int64_t &Res) override;
  197. /// \brief Parse an identifier or string (as a quoted identifier)
  198. /// and set \p Res to the identifier contents.
  199. bool parseIdentifier(StringRef &Res) override;
  200. void eatToEndOfStatement() override;
  201. void checkForValidSection() override;
  202. /// }
  203. private:
  204. bool parseStatement(ParseStatementInfo &Info,
  205. MCAsmParserSemaCallback *SI);
  206. void eatToEndOfLine();
  207. bool parseCppHashLineFilenameComment(const SMLoc &L);
  208. void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
  209. ArrayRef<MCAsmMacroParameter> Parameters);
  210. bool expandMacro(raw_svector_ostream &OS, StringRef Body,
  211. ArrayRef<MCAsmMacroParameter> Parameters,
  212. ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable,
  213. const SMLoc &L);
  214. /// \brief Are macros enabled in the parser?
  215. bool areMacrosEnabled() {return MacrosEnabledFlag;}
  216. /// \brief Control a flag in the parser that enables or disables macros.
  217. void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
  218. /// \brief Lookup a previously defined macro.
  219. /// \param Name Macro name.
  220. /// \returns Pointer to macro. NULL if no such macro was defined.
  221. const MCAsmMacro* lookupMacro(StringRef Name);
  222. /// \brief Define a new macro with the given name and information.
  223. void defineMacro(StringRef Name, MCAsmMacro Macro);
  224. /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
  225. void undefineMacro(StringRef Name);
  226. /// \brief Are we inside a macro instantiation?
  227. bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
  228. /// \brief Handle entry to macro instantiation.
  229. ///
  230. /// \param M The macro.
  231. /// \param NameLoc Instantiation location.
  232. bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
  233. /// \brief Handle exit from macro instantiation.
  234. void handleMacroExit();
  235. /// \brief Extract AsmTokens for a macro argument.
  236. bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
  237. /// \brief Parse all macro arguments for a given macro.
  238. bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
  239. void printMacroInstantiations();
  240. void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
  241. ArrayRef<SMRange> Ranges = None) const {
  242. SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
  243. }
  244. static void DiagHandler(const SMDiagnostic &Diag, void *Context);
  245. /// \brief Enter the specified file. This returns true on failure.
  246. bool enterIncludeFile(const std::string &Filename);
  247. /// \brief Process the specified file for the .incbin directive.
  248. /// This returns true on failure.
  249. bool processIncbinFile(const std::string &Filename);
  250. /// \brief Reset the current lexer position to that given by \p Loc. The
  251. /// current token is not set; clients should ensure Lex() is called
  252. /// subsequently.
  253. ///
  254. /// \param InBuffer If not 0, should be the known buffer id that contains the
  255. /// location.
  256. void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
  257. /// \brief Parse up to the end of statement and a return the contents from the
  258. /// current token until the end of the statement; the current token on exit
  259. /// will be either the EndOfStatement or EOF.
  260. StringRef parseStringToEndOfStatement() override;
  261. /// \brief Parse until the end of a statement or a comma is encountered,
  262. /// return the contents from the current token up to the end or comma.
  263. StringRef parseStringToComma();
  264. bool parseAssignment(StringRef Name, bool allow_redef,
  265. bool NoDeadStrip = false);
  266. unsigned getBinOpPrecedence(AsmToken::TokenKind K,
  267. MCBinaryExpr::Opcode &Kind);
  268. bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
  269. bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
  270. bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
  271. bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
  272. // Generic (target and platform independent) directive parsing.
  273. enum DirectiveKind {
  274. DK_NO_DIRECTIVE, // Placeholder
  275. DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
  276. DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
  277. DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
  278. DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
  279. DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
  280. DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
  281. DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
  282. DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
  283. DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
  284. DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
  285. DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
  286. DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFNES, DK_IFDEF, DK_IFNDEF,
  287. DK_IFNOTDEF, DK_ELSEIF, DK_ELSE, DK_ENDIF,
  288. DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
  289. DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
  290. DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
  291. DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
  292. DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
  293. DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
  294. DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
  295. DK_MACROS_ON, DK_MACROS_OFF,
  296. DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
  297. DK_SLEB128, DK_ULEB128,
  298. DK_ERR, DK_ERROR, DK_WARNING,
  299. DK_END
  300. };
  301. /// \brief Maps directive name --> DirectiveKind enum, for
  302. /// directives parsed by this class.
  303. StringMap<DirectiveKind> DirectiveKindMap;
  304. // ".ascii", ".asciz", ".string"
  305. bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
  306. bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
  307. bool parseDirectiveOctaValue(); // ".octa"
  308. bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
  309. bool parseDirectiveFill(); // ".fill"
  310. bool parseDirectiveZero(); // ".zero"
  311. // ".set", ".equ", ".equiv"
  312. bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
  313. bool parseDirectiveOrg(); // ".org"
  314. // ".align{,32}", ".p2align{,w,l}"
  315. bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
  316. // ".file", ".line", ".loc", ".stabs"
  317. bool parseDirectiveFile(SMLoc DirectiveLoc);
  318. bool parseDirectiveLine();
  319. bool parseDirectiveLoc();
  320. bool parseDirectiveStabs();
  321. // .cfi directives
  322. bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
  323. bool parseDirectiveCFIWindowSave();
  324. bool parseDirectiveCFISections();
  325. bool parseDirectiveCFIStartProc();
  326. bool parseDirectiveCFIEndProc();
  327. bool parseDirectiveCFIDefCfaOffset();
  328. bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
  329. bool parseDirectiveCFIAdjustCfaOffset();
  330. bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
  331. bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
  332. bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
  333. bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
  334. bool parseDirectiveCFIRememberState();
  335. bool parseDirectiveCFIRestoreState();
  336. bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
  337. bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
  338. bool parseDirectiveCFIEscape();
  339. bool parseDirectiveCFISignalFrame();
  340. bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
  341. // macro directives
  342. bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
  343. bool parseDirectiveExitMacro(StringRef Directive);
  344. bool parseDirectiveEndMacro(StringRef Directive);
  345. bool parseDirectiveMacro(SMLoc DirectiveLoc);
  346. bool parseDirectiveMacrosOnOff(StringRef Directive);
  347. // ".bundle_align_mode"
  348. bool parseDirectiveBundleAlignMode();
  349. // ".bundle_lock"
  350. bool parseDirectiveBundleLock();
  351. // ".bundle_unlock"
  352. bool parseDirectiveBundleUnlock();
  353. // ".space", ".skip"
  354. bool parseDirectiveSpace(StringRef IDVal);
  355. // .sleb128 (Signed=true) and .uleb128 (Signed=false)
  356. bool parseDirectiveLEB128(bool Signed);
  357. /// \brief Parse a directive like ".globl" which
  358. /// accepts a single symbol (which should be a label or an external).
  359. bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
  360. bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
  361. bool parseDirectiveAbort(); // ".abort"
  362. bool parseDirectiveInclude(); // ".include"
  363. bool parseDirectiveIncbin(); // ".incbin"
  364. // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
  365. bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
  366. // ".ifb" or ".ifnb", depending on ExpectBlank.
  367. bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
  368. // ".ifc" or ".ifnc", depending on ExpectEqual.
  369. bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
  370. // ".ifeqs" or ".ifnes", depending on ExpectEqual.
  371. bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual);
  372. // ".ifdef" or ".ifndef", depending on expect_defined
  373. bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
  374. bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
  375. bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
  376. bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
  377. bool parseEscapedString(std::string &Data) override;
  378. const MCExpr *applyModifierToExpr(const MCExpr *E,
  379. MCSymbolRefExpr::VariantKind Variant);
  380. // Macro-like directives
  381. MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
  382. void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
  383. raw_svector_ostream &OS);
  384. bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
  385. bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
  386. bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
  387. bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
  388. // "_emit" or "__emit"
  389. bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
  390. size_t Len);
  391. // "align"
  392. bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
  393. // "end"
  394. bool parseDirectiveEnd(SMLoc DirectiveLoc);
  395. // ".err" or ".error"
  396. bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
  397. // ".warning"
  398. bool parseDirectiveWarning(SMLoc DirectiveLoc);
  399. void initializeDirectiveKindMap();
  400. };
  401. }
  402. namespace llvm {
  403. extern MCAsmParserExtension *createDarwinAsmParser();
  404. extern MCAsmParserExtension *createELFAsmParser();
  405. extern MCAsmParserExtension *createCOFFAsmParser();
  406. }
  407. enum { DEFAULT_ADDRSPACE = 0 };
  408. AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
  409. const MCAsmInfo &MAI)
  410. : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM),
  411. PlatformParser(nullptr), CurBuffer(SM.getMainFileID()),
  412. MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0),
  413. AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
  414. // Save the old handler.
  415. SavedDiagHandler = SrcMgr.getDiagHandler();
  416. SavedDiagContext = SrcMgr.getDiagContext();
  417. // Set our own handler which calls the saved handler.
  418. SrcMgr.setDiagHandler(DiagHandler, this);
  419. Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
  420. // Initialize the platform / file format parser.
  421. switch (Ctx.getObjectFileInfo()->getObjectFileType()) {
  422. case MCObjectFileInfo::IsCOFF:
  423. PlatformParser.reset(createCOFFAsmParser());
  424. break;
  425. case MCObjectFileInfo::IsMachO:
  426. PlatformParser.reset(createDarwinAsmParser());
  427. IsDarwin = true;
  428. break;
  429. case MCObjectFileInfo::IsELF:
  430. PlatformParser.reset(createELFAsmParser());
  431. break;
  432. }
  433. PlatformParser->Initialize(*this);
  434. initializeDirectiveKindMap();
  435. NumOfMacroInstantiations = 0;
  436. }
  437. AsmParser::~AsmParser() {
  438. assert((HadError || ActiveMacros.empty()) &&
  439. "Unexpected active macro instantiation!");
  440. }
  441. void AsmParser::printMacroInstantiations() {
  442. // Print the active macro instantiation stack.
  443. for (std::vector<MacroInstantiation *>::const_reverse_iterator
  444. it = ActiveMacros.rbegin(),
  445. ie = ActiveMacros.rend();
  446. it != ie; ++it)
  447. printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
  448. "while in macro instantiation");
  449. }
  450. void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
  451. printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
  452. printMacroInstantiations();
  453. }
  454. bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
  455. if (getTargetParser().getTargetOptions().MCFatalWarnings)
  456. return Error(L, Msg, Ranges);
  457. printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
  458. printMacroInstantiations();
  459. return false;
  460. }
  461. bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
  462. HadError = true;
  463. printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
  464. printMacroInstantiations();
  465. return true;
  466. }
  467. bool AsmParser::enterIncludeFile(const std::string &Filename) {
  468. std::string IncludedFile;
  469. unsigned NewBuf =
  470. SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
  471. if (!NewBuf)
  472. return true;
  473. CurBuffer = NewBuf;
  474. Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
  475. return false;
  476. }
  477. /// Process the specified .incbin file by searching for it in the include paths
  478. /// then just emitting the byte contents of the file to the streamer. This
  479. /// returns true on failure.
  480. bool AsmParser::processIncbinFile(const std::string &Filename) {
  481. std::string IncludedFile;
  482. unsigned NewBuf =
  483. SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
  484. if (!NewBuf)
  485. return true;
  486. // Pick up the bytes from the file and emit them.
  487. getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
  488. return false;
  489. }
  490. void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
  491. CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
  492. Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
  493. Loc.getPointer());
  494. }
  495. const AsmToken &AsmParser::Lex() {
  496. const AsmToken *tok = &Lexer.Lex();
  497. if (tok->is(AsmToken::Eof)) {
  498. // If this is the end of an included file, pop the parent file off the
  499. // include stack.
  500. SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
  501. if (ParentIncludeLoc != SMLoc()) {
  502. jumpToLoc(ParentIncludeLoc);
  503. tok = &Lexer.Lex();
  504. }
  505. }
  506. if (tok->is(AsmToken::Error))
  507. Error(Lexer.getErrLoc(), Lexer.getErr());
  508. return *tok;
  509. }
  510. bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
  511. // Create the initial section, if requested.
  512. if (!NoInitialTextSection)
  513. Out.InitSections(false);
  514. // Prime the lexer.
  515. Lex();
  516. HadError = false;
  517. AsmCond StartingCondState = TheCondState;
  518. // If we are generating dwarf for assembly source files save the initial text
  519. // section and generate a .file directive.
  520. if (getContext().getGenDwarfForAssembly()) {
  521. MCSection *Sec = getStreamer().getCurrentSection().first;
  522. if (!Sec->getBeginSymbol()) {
  523. MCSymbol *SectionStartSym = getContext().createTempSymbol();
  524. getStreamer().EmitLabel(SectionStartSym);
  525. Sec->setBeginSymbol(SectionStartSym);
  526. }
  527. bool InsertResult = getContext().addGenDwarfSection(Sec);
  528. assert(InsertResult && ".text section should not have debug info yet");
  529. (void)InsertResult;
  530. getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
  531. 0, StringRef(), getContext().getMainFileName()));
  532. }
  533. // While we have input, parse each statement.
  534. while (Lexer.isNot(AsmToken::Eof)) {
  535. ParseStatementInfo Info;
  536. if (!parseStatement(Info, nullptr))
  537. continue;
  538. // We had an error, validate that one was emitted and recover by skipping to
  539. // the next line.
  540. assert(HadError && "Parse statement returned an error, but none emitted!");
  541. eatToEndOfStatement();
  542. }
  543. if (TheCondState.TheCond != StartingCondState.TheCond ||
  544. TheCondState.Ignore != StartingCondState.Ignore)
  545. return TokError("unmatched .ifs or .elses");
  546. // Check to see there are no empty DwarfFile slots.
  547. const auto &LineTables = getContext().getMCDwarfLineTables();
  548. if (!LineTables.empty()) {
  549. unsigned Index = 0;
  550. for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
  551. if (File.Name.empty() && Index != 0)
  552. TokError("unassigned file number: " + Twine(Index) +
  553. " for .file directives");
  554. ++Index;
  555. }
  556. }
  557. // Check to see that all assembler local symbols were actually defined.
  558. // Targets that don't do subsections via symbols may not want this, though,
  559. // so conservatively exclude them. Only do this if we're finalizing, though,
  560. // as otherwise we won't necessarilly have seen everything yet.
  561. if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
  562. const MCContext::SymbolTable &Symbols = getContext().getSymbols();
  563. for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
  564. e = Symbols.end();
  565. i != e; ++i) {
  566. MCSymbol *Sym = i->getValue();
  567. // Variable symbols may not be marked as defined, so check those
  568. // explicitly. If we know it's a variable, we have a definition for
  569. // the purposes of this check.
  570. if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
  571. // FIXME: We would really like to refer back to where the symbol was
  572. // first referenced for a source location. We need to add something
  573. // to track that. Currently, we just point to the end of the file.
  574. printMessage(
  575. getLexer().getLoc(), SourceMgr::DK_Error,
  576. "assembler local symbol '" + Sym->getName() + "' not defined");
  577. }
  578. }
  579. // Finalize the output stream if there are no errors and if the client wants
  580. // us to.
  581. if (!HadError && !NoFinalize)
  582. Out.Finish();
  583. return HadError;
  584. }
  585. void AsmParser::checkForValidSection() {
  586. if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
  587. TokError("expected section directive before assembly directive");
  588. Out.InitSections(false);
  589. }
  590. }
  591. /// \brief Throw away the rest of the line for testing purposes.
  592. void AsmParser::eatToEndOfStatement() {
  593. while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
  594. Lex();
  595. // Eat EOL.
  596. if (Lexer.is(AsmToken::EndOfStatement))
  597. Lex();
  598. }
  599. StringRef AsmParser::parseStringToEndOfStatement() {
  600. const char *Start = getTok().getLoc().getPointer();
  601. while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
  602. Lex();
  603. const char *End = getTok().getLoc().getPointer();
  604. return StringRef(Start, End - Start);
  605. }
  606. StringRef AsmParser::parseStringToComma() {
  607. const char *Start = getTok().getLoc().getPointer();
  608. while (Lexer.isNot(AsmToken::EndOfStatement) &&
  609. Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
  610. Lex();
  611. const char *End = getTok().getLoc().getPointer();
  612. return StringRef(Start, End - Start);
  613. }
  614. /// \brief Parse a paren expression and return it.
  615. /// NOTE: This assumes the leading '(' has already been consumed.
  616. ///
  617. /// parenexpr ::= expr)
  618. ///
  619. bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
  620. if (parseExpression(Res))
  621. return true;
  622. if (Lexer.isNot(AsmToken::RParen))
  623. return TokError("expected ')' in parentheses expression");
  624. EndLoc = Lexer.getTok().getEndLoc();
  625. Lex();
  626. return false;
  627. }
  628. /// \brief Parse a bracket expression and return it.
  629. /// NOTE: This assumes the leading '[' has already been consumed.
  630. ///
  631. /// bracketexpr ::= expr]
  632. ///
  633. bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
  634. if (parseExpression(Res))
  635. return true;
  636. if (Lexer.isNot(AsmToken::RBrac))
  637. return TokError("expected ']' in brackets expression");
  638. EndLoc = Lexer.getTok().getEndLoc();
  639. Lex();
  640. return false;
  641. }
  642. /// \brief Parse a primary expression and return it.
  643. /// primaryexpr ::= (parenexpr
  644. /// primaryexpr ::= symbol
  645. /// primaryexpr ::= number
  646. /// primaryexpr ::= '.'
  647. /// primaryexpr ::= ~,+,- primaryexpr
  648. bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
  649. SMLoc FirstTokenLoc = getLexer().getLoc();
  650. AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
  651. switch (FirstTokenKind) {
  652. default:
  653. return TokError("unknown token in expression");
  654. // If we have an error assume that we've already handled it.
  655. case AsmToken::Error:
  656. return true;
  657. case AsmToken::Exclaim:
  658. Lex(); // Eat the operator.
  659. if (parsePrimaryExpr(Res, EndLoc))
  660. return true;
  661. Res = MCUnaryExpr::createLNot(Res, getContext());
  662. return false;
  663. case AsmToken::Dollar:
  664. case AsmToken::At:
  665. case AsmToken::String:
  666. case AsmToken::Identifier: {
  667. StringRef Identifier;
  668. if (parseIdentifier(Identifier)) {
  669. if (FirstTokenKind == AsmToken::Dollar) {
  670. if (Lexer.getMAI().getDollarIsPC()) {
  671. // This is a '$' reference, which references the current PC. Emit a
  672. // temporary label to the streamer and refer to it.
  673. MCSymbol *Sym = Ctx.createTempSymbol();
  674. Out.EmitLabel(Sym);
  675. Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None,
  676. getContext());
  677. EndLoc = FirstTokenLoc;
  678. return false;
  679. }
  680. return Error(FirstTokenLoc, "invalid token in expression");
  681. }
  682. }
  683. // Parse symbol variant
  684. std::pair<StringRef, StringRef> Split;
  685. if (!MAI.useParensForSymbolVariant()) {
  686. if (FirstTokenKind == AsmToken::String) {
  687. if (Lexer.is(AsmToken::At)) {
  688. Lexer.Lex(); // eat @
  689. SMLoc AtLoc = getLexer().getLoc();
  690. StringRef VName;
  691. if (parseIdentifier(VName))
  692. return Error(AtLoc, "expected symbol variant after '@'");
  693. Split = std::make_pair(Identifier, VName);
  694. }
  695. } else {
  696. Split = Identifier.split('@');
  697. }
  698. } else if (Lexer.is(AsmToken::LParen)) {
  699. Lexer.Lex(); // eat (
  700. StringRef VName;
  701. parseIdentifier(VName);
  702. if (Lexer.isNot(AsmToken::RParen)) {
  703. return Error(Lexer.getTok().getLoc(),
  704. "unexpected token in variant, expected ')'");
  705. }
  706. Lexer.Lex(); // eat )
  707. Split = std::make_pair(Identifier, VName);
  708. }
  709. EndLoc = SMLoc::getFromPointer(Identifier.end());
  710. // This is a symbol reference.
  711. StringRef SymbolName = Identifier;
  712. MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
  713. // Lookup the symbol variant if used.
  714. if (Split.second.size()) {
  715. Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
  716. if (Variant != MCSymbolRefExpr::VK_Invalid) {
  717. SymbolName = Split.first;
  718. } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
  719. Variant = MCSymbolRefExpr::VK_None;
  720. } else {
  721. return Error(SMLoc::getFromPointer(Split.second.begin()),
  722. "invalid variant '" + Split.second + "'");
  723. }
  724. }
  725. MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName);
  726. // If this is an absolute variable reference, substitute it now to preserve
  727. // semantics in the face of reassignment.
  728. if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
  729. if (Variant)
  730. return Error(EndLoc, "unexpected modifier on variable reference");
  731. Res = Sym->getVariableValue();
  732. return false;
  733. }
  734. // Otherwise create a symbol ref.
  735. Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
  736. return false;
  737. }
  738. case AsmToken::BigNum:
  739. return TokError("literal value out of range for directive");
  740. case AsmToken::Integer: {
  741. SMLoc Loc = getTok().getLoc();
  742. int64_t IntVal = getTok().getIntVal();
  743. Res = MCConstantExpr::create(IntVal, getContext());
  744. EndLoc = Lexer.getTok().getEndLoc();
  745. Lex(); // Eat token.
  746. // Look for 'b' or 'f' following an Integer as a directional label
  747. if (Lexer.getKind() == AsmToken::Identifier) {
  748. StringRef IDVal = getTok().getString();
  749. // Lookup the symbol variant if used.
  750. std::pair<StringRef, StringRef> Split = IDVal.split('@');
  751. MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
  752. if (Split.first.size() != IDVal.size()) {
  753. Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
  754. if (Variant == MCSymbolRefExpr::VK_Invalid)
  755. return TokError("invalid variant '" + Split.second + "'");
  756. IDVal = Split.first;
  757. }
  758. if (IDVal == "f" || IDVal == "b") {
  759. MCSymbol *Sym =
  760. Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b");
  761. Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
  762. if (IDVal == "b" && Sym->isUndefined())
  763. return Error(Loc, "invalid reference to undefined symbol");
  764. EndLoc = Lexer.getTok().getEndLoc();
  765. Lex(); // Eat identifier.
  766. }
  767. }
  768. return false;
  769. }
  770. case AsmToken::Real: {
  771. APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
  772. uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
  773. Res = MCConstantExpr::create(IntVal, getContext());
  774. EndLoc = Lexer.getTok().getEndLoc();
  775. Lex(); // Eat token.
  776. return false;
  777. }
  778. case AsmToken::Dot: {
  779. // This is a '.' reference, which references the current PC. Emit a
  780. // temporary label to the streamer and refer to it.
  781. MCSymbol *Sym = Ctx.createTempSymbol();
  782. Out.EmitLabel(Sym);
  783. Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
  784. EndLoc = Lexer.getTok().getEndLoc();
  785. Lex(); // Eat identifier.
  786. return false;
  787. }
  788. case AsmToken::LParen:
  789. Lex(); // Eat the '('.
  790. return parseParenExpr(Res, EndLoc);
  791. case AsmToken::LBrac:
  792. if (!PlatformParser->HasBracketExpressions())
  793. return TokError("brackets expression not supported on this target");
  794. Lex(); // Eat the '['.
  795. return parseBracketExpr(Res, EndLoc);
  796. case AsmToken::Minus:
  797. Lex(); // Eat the operator.
  798. if (parsePrimaryExpr(Res, EndLoc))
  799. return true;
  800. Res = MCUnaryExpr::createMinus(Res, getContext());
  801. return false;
  802. case AsmToken::Plus:
  803. Lex(); // Eat the operator.
  804. if (parsePrimaryExpr(Res, EndLoc))
  805. return true;
  806. Res = MCUnaryExpr::createPlus(Res, getContext());
  807. return false;
  808. case AsmToken::Tilde:
  809. Lex(); // Eat the operator.
  810. if (parsePrimaryExpr(Res, EndLoc))
  811. return true;
  812. Res = MCUnaryExpr::createNot(Res, getContext());
  813. return false;
  814. }
  815. }
  816. bool AsmParser::parseExpression(const MCExpr *&Res) {
  817. SMLoc EndLoc;
  818. return parseExpression(Res, EndLoc);
  819. }
  820. const MCExpr *
  821. AsmParser::applyModifierToExpr(const MCExpr *E,
  822. MCSymbolRefExpr::VariantKind Variant) {
  823. // Ask the target implementation about this expression first.
  824. const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
  825. if (NewE)
  826. return NewE;
  827. // Recurse over the given expression, rebuilding it to apply the given variant
  828. // if there is exactly one symbol.
  829. switch (E->getKind()) {
  830. case MCExpr::Target:
  831. case MCExpr::Constant:
  832. return nullptr;
  833. case MCExpr::SymbolRef: {
  834. const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
  835. if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
  836. TokError("invalid variant on expression '" + getTok().getIdentifier() +
  837. "' (already modified)");
  838. return E;
  839. }
  840. return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, getContext());
  841. }
  842. case MCExpr::Unary: {
  843. const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
  844. const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
  845. if (!Sub)
  846. return nullptr;
  847. return MCUnaryExpr::create(UE->getOpcode(), Sub, getContext());
  848. }
  849. case MCExpr::Binary: {
  850. const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
  851. const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
  852. const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
  853. if (!LHS && !RHS)
  854. return nullptr;
  855. if (!LHS)
  856. LHS = BE->getLHS();
  857. if (!RHS)
  858. RHS = BE->getRHS();
  859. return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, getContext());
  860. }
  861. }
  862. llvm_unreachable("Invalid expression kind!");
  863. }
  864. /// \brief Parse an expression and return it.
  865. ///
  866. /// expr ::= expr &&,|| expr -> lowest.
  867. /// expr ::= expr |,^,&,! expr
  868. /// expr ::= expr ==,!=,<>,<,<=,>,>= expr
  869. /// expr ::= expr <<,>> expr
  870. /// expr ::= expr +,- expr
  871. /// expr ::= expr *,/,% expr -> highest.
  872. /// expr ::= primaryexpr
  873. ///
  874. bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
  875. // Parse the expression.
  876. Res = nullptr;
  877. if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
  878. return true;
  879. // As a special case, we support 'a op b @ modifier' by rewriting the
  880. // expression to include the modifier. This is inefficient, but in general we
  881. // expect users to use 'a@modifier op b'.
  882. if (Lexer.getKind() == AsmToken::At) {
  883. Lex();
  884. if (Lexer.isNot(AsmToken::Identifier))
  885. return TokError("unexpected symbol modifier following '@'");
  886. MCSymbolRefExpr::VariantKind Variant =
  887. MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
  888. if (Variant == MCSymbolRefExpr::VK_Invalid)
  889. return TokError("invalid variant '" + getTok().getIdentifier() + "'");
  890. const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
  891. if (!ModifiedRes) {
  892. return TokError("invalid modifier '" + getTok().getIdentifier() +
  893. "' (no symbols present)");
  894. }
  895. Res = ModifiedRes;
  896. Lex();
  897. }
  898. // Try to constant fold it up front, if possible.
  899. int64_t Value;
  900. if (Res->evaluateAsAbsolute(Value))
  901. Res = MCConstantExpr::create(Value, getContext());
  902. return false;
  903. }
  904. bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
  905. Res = nullptr;
  906. return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
  907. }
  908. bool AsmParser::parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
  909. SMLoc &EndLoc) {
  910. if (parseParenExpr(Res, EndLoc))
  911. return true;
  912. for (; ParenDepth > 0; --ParenDepth) {
  913. if (parseBinOpRHS(1, Res, EndLoc))
  914. return true;
  915. // We don't Lex() the last RParen.
  916. // This is the same behavior as parseParenExpression().
  917. if (ParenDepth - 1 > 0) {
  918. if (Lexer.isNot(AsmToken::RParen))
  919. return TokError("expected ')' in parentheses expression");
  920. EndLoc = Lexer.getTok().getEndLoc();
  921. Lex();
  922. }
  923. }
  924. return false;
  925. }
  926. bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
  927. const MCExpr *Expr;
  928. SMLoc StartLoc = Lexer.getLoc();
  929. if (parseExpression(Expr))
  930. return true;
  931. if (!Expr->evaluateAsAbsolute(Res))
  932. return Error(StartLoc, "expected absolute expression");
  933. return false;
  934. }
  935. unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K,
  936. MCBinaryExpr::Opcode &Kind) {
  937. switch (K) {
  938. default:
  939. return 0; // not a binop.
  940. // Lowest Precedence: &&, ||
  941. case AsmToken::AmpAmp:
  942. Kind = MCBinaryExpr::LAnd;
  943. return 1;
  944. case AsmToken::PipePipe:
  945. Kind = MCBinaryExpr::LOr;
  946. return 1;
  947. // Low Precedence: |, &, ^
  948. //
  949. // FIXME: gas seems to support '!' as an infix operator?
  950. case AsmToken::Pipe:
  951. Kind = MCBinaryExpr::Or;
  952. return 2;
  953. case AsmToken::Caret:
  954. Kind = MCBinaryExpr::Xor;
  955. return 2;
  956. case AsmToken::Amp:
  957. Kind = MCBinaryExpr::And;
  958. return 2;
  959. // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
  960. case AsmToken::EqualEqual:
  961. Kind = MCBinaryExpr::EQ;
  962. return 3;
  963. case AsmToken::ExclaimEqual:
  964. case AsmToken::LessGreater:
  965. Kind = MCBinaryExpr::NE;
  966. return 3;
  967. case AsmToken::Less:
  968. Kind = MCBinaryExpr::LT;
  969. return 3;
  970. case AsmToken::LessEqual:
  971. Kind = MCBinaryExpr::LTE;
  972. return 3;
  973. case AsmToken::Greater:
  974. Kind = MCBinaryExpr::GT;
  975. return 3;
  976. case AsmToken::GreaterEqual:
  977. Kind = MCBinaryExpr::GTE;
  978. return 3;
  979. // Intermediate Precedence: <<, >>
  980. case AsmToken::LessLess:
  981. Kind = MCBinaryExpr::Shl;
  982. return 4;
  983. case AsmToken::GreaterGreater:
  984. Kind = MAI.shouldUseLogicalShr() ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
  985. return 4;
  986. // High Intermediate Precedence: +, -
  987. case AsmToken::Plus:
  988. Kind = MCBinaryExpr::Add;
  989. return 5;
  990. case AsmToken::Minus:
  991. Kind = MCBinaryExpr::Sub;
  992. return 5;
  993. // Highest Precedence: *, /, %
  994. case AsmToken::Star:
  995. Kind = MCBinaryExpr::Mul;
  996. return 6;
  997. case AsmToken::Slash:
  998. Kind = MCBinaryExpr::Div;
  999. return 6;
  1000. case AsmToken::Percent:
  1001. Kind = MCBinaryExpr::Mod;
  1002. return 6;
  1003. }
  1004. }
  1005. /// \brief Parse all binary operators with precedence >= 'Precedence'.
  1006. /// Res contains the LHS of the expression on input.
  1007. bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
  1008. SMLoc &EndLoc) {
  1009. while (1) {
  1010. MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
  1011. unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
  1012. // If the next token is lower precedence than we are allowed to eat, return
  1013. // successfully with what we ate already.
  1014. if (TokPrec < Precedence)
  1015. return false;
  1016. Lex();
  1017. // Eat the next primary expression.
  1018. const MCExpr *RHS;
  1019. if (parsePrimaryExpr(RHS, EndLoc))
  1020. return true;
  1021. // If BinOp binds less tightly with RHS than the operator after RHS, let
  1022. // the pending operator take RHS as its LHS.
  1023. MCBinaryExpr::Opcode Dummy;
  1024. unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
  1025. if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
  1026. return true;
  1027. // Merge LHS and RHS according to operator.
  1028. Res = MCBinaryExpr::create(Kind, Res, RHS, getContext());
  1029. }
  1030. }
  1031. /// ParseStatement:
  1032. /// ::= EndOfStatement
  1033. /// ::= Label* Directive ...Operands... EndOfStatement
  1034. /// ::= Label* Identifier OperandList* EndOfStatement
  1035. bool AsmParser::parseStatement(ParseStatementInfo &Info,
  1036. MCAsmParserSemaCallback *SI) {
  1037. if (Lexer.is(AsmToken::EndOfStatement)) {
  1038. Out.AddBlankLine();
  1039. Lex();
  1040. return false;
  1041. }
  1042. // Statements always start with an identifier or are a full line comment.
  1043. AsmToken ID = getTok();
  1044. SMLoc IDLoc = ID.getLoc();
  1045. StringRef IDVal;
  1046. int64_t LocalLabelVal = -1;
  1047. // A full line comment is a '#' as the first token.
  1048. if (Lexer.is(AsmToken::Hash))
  1049. return parseCppHashLineFilenameComment(IDLoc);
  1050. // Allow an integer followed by a ':' as a directional local label.
  1051. if (Lexer.is(AsmToken::Integer)) {
  1052. LocalLabelVal = getTok().getIntVal();
  1053. if (LocalLabelVal < 0) {
  1054. if (!TheCondState.Ignore)
  1055. return TokError("unexpected token at start of statement");
  1056. IDVal = "";
  1057. } else {
  1058. IDVal = getTok().getString();
  1059. Lex(); // Consume the integer token to be used as an identifier token.
  1060. if (Lexer.getKind() != AsmToken::Colon) {
  1061. if (!TheCondState.Ignore)
  1062. return TokError("unexpected token at start of statement");
  1063. }
  1064. }
  1065. } else if (Lexer.is(AsmToken::Dot)) {
  1066. // Treat '.' as a valid identifier in this context.
  1067. Lex();
  1068. IDVal = ".";
  1069. } else if (parseIdentifier(IDVal)) {
  1070. if (!TheCondState.Ignore)
  1071. return TokError("unexpected token at start of statement");
  1072. IDVal = "";
  1073. }
  1074. // Handle conditional assembly here before checking for skipping. We
  1075. // have to do this so that .endif isn't skipped in a ".if 0" block for
  1076. // example.
  1077. StringMap<DirectiveKind>::const_iterator DirKindIt =
  1078. DirectiveKindMap.find(IDVal);
  1079. DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
  1080. ? DK_NO_DIRECTIVE
  1081. : DirKindIt->getValue();
  1082. switch (DirKind) {
  1083. default:
  1084. break;
  1085. case DK_IF:
  1086. case DK_IFEQ:
  1087. case DK_IFGE:
  1088. case DK_IFGT:
  1089. case DK_IFLE:
  1090. case DK_IFLT:
  1091. case DK_IFNE:
  1092. return parseDirectiveIf(IDLoc, DirKind);
  1093. case DK_IFB:
  1094. return parseDirectiveIfb(IDLoc, true);
  1095. case DK_IFNB:
  1096. return parseDirectiveIfb(IDLoc, false);
  1097. case DK_IFC:
  1098. return parseDirectiveIfc(IDLoc, true);
  1099. case DK_IFEQS:
  1100. return parseDirectiveIfeqs(IDLoc, true);
  1101. case DK_IFNC:
  1102. return parseDirectiveIfc(IDLoc, false);
  1103. case DK_IFNES:
  1104. return parseDirectiveIfeqs(IDLoc, false);
  1105. case DK_IFDEF:
  1106. return parseDirectiveIfdef(IDLoc, true);
  1107. case DK_IFNDEF:
  1108. case DK_IFNOTDEF:
  1109. return parseDirectiveIfdef(IDLoc, false);
  1110. case DK_ELSEIF:
  1111. return parseDirectiveElseIf(IDLoc);
  1112. case DK_ELSE:
  1113. return parseDirectiveElse(IDLoc);
  1114. case DK_ENDIF:
  1115. return parseDirectiveEndIf(IDLoc);
  1116. }
  1117. // Ignore the statement if in the middle of inactive conditional
  1118. // (e.g. ".if 0").
  1119. if (TheCondState.Ignore) {
  1120. eatToEndOfStatement();
  1121. return false;
  1122. }
  1123. // FIXME: Recurse on local labels?
  1124. // See what kind of statement we have.
  1125. switch (Lexer.getKind()) {
  1126. case AsmToken::Colon: {
  1127. checkForValidSection();
  1128. // identifier ':' -> Label.
  1129. Lex();
  1130. // Diagnose attempt to use '.' as a label.
  1131. if (IDVal == ".")
  1132. return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
  1133. // Diagnose attempt to use a variable as a label.
  1134. //
  1135. // FIXME: Diagnostics. Note the location of the definition as a label.
  1136. // FIXME: This doesn't diagnose assignment to a symbol which has been
  1137. // implicitly marked as external.
  1138. MCSymbol *Sym;
  1139. if (LocalLabelVal == -1) {
  1140. if (ParsingInlineAsm && SI) {
  1141. StringRef RewrittenLabel =
  1142. SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
  1143. assert(RewrittenLabel.size() &&
  1144. "We should have an internal name here.");
  1145. Info.AsmRewrites->push_back(AsmRewrite(AOK_Label, IDLoc,
  1146. IDVal.size(), RewrittenLabel));
  1147. IDVal = RewrittenLabel;
  1148. }
  1149. Sym = getContext().getOrCreateSymbol(IDVal);
  1150. } else
  1151. Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal);
  1152. Sym->redefineIfPossible();
  1153. if (!Sym->isUndefined() || Sym->isVariable())
  1154. return Error(IDLoc, "invalid symbol redefinition");
  1155. // Emit the label.
  1156. if (!ParsingInlineAsm)
  1157. Out.EmitLabel(Sym);
  1158. // If we are generating dwarf for assembly source files then gather the
  1159. // info to make a dwarf label entry for this label if needed.
  1160. if (getContext().getGenDwarfForAssembly())
  1161. MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
  1162. IDLoc);
  1163. getTargetParser().onLabelParsed(Sym);
  1164. // Consume any end of statement token, if present, to avoid spurious
  1165. // AddBlankLine calls().
  1166. if (Lexer.is(AsmToken::EndOfStatement)) {
  1167. Lex();
  1168. if (Lexer.is(AsmToken::Eof))
  1169. return false;
  1170. }
  1171. return false;
  1172. }
  1173. case AsmToken::Equal:
  1174. // identifier '=' ... -> assignment statement
  1175. Lex();
  1176. return parseAssignment(IDVal, true);
  1177. default: // Normal instruction or directive.
  1178. break;
  1179. }
  1180. // If macros are enabled, check to see if this is a macro instantiation.
  1181. if (areMacrosEnabled())
  1182. if (const MCAsmMacro *M = lookupMacro(IDVal)) {
  1183. return handleMacroEntry(M, IDLoc);
  1184. }
  1185. // Otherwise, we have a normal instruction or directive.
  1186. // Directives start with "."
  1187. if (IDVal[0] == '.' && IDVal != ".") {
  1188. // There are several entities interested in parsing directives:
  1189. //
  1190. // 1. The target-specific assembly parser. Some directives are target
  1191. // specific or may potentially behave differently on certain targets.
  1192. // 2. Asm parser extensions. For example, platform-specific parsers
  1193. // (like the ELF parser) register themselves as extensions.
  1194. // 3. The generic directive parser implemented by this class. These are
  1195. // all the directives that behave in a target and platform independent
  1196. // manner, or at least have a default behavior that's shared between
  1197. // all targets and platforms.
  1198. // First query the target-specific parser. It will return 'true' if it
  1199. // isn't interested in this directive.
  1200. if (!getTargetParser().ParseDirective(ID))
  1201. return false;
  1202. // Next, check the extension directive map to see if any extension has
  1203. // registered itself to parse this directive.
  1204. std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
  1205. ExtensionDirectiveMap.lookup(IDVal);
  1206. if (Handler.first)
  1207. return (*Handler.second)(Handler.first, IDVal, IDLoc);
  1208. // Finally, if no one else is interested in this directive, it must be
  1209. // generic and familiar to this class.
  1210. switch (DirKind) {
  1211. default:
  1212. break;
  1213. case DK_SET:
  1214. case DK_EQU:
  1215. return parseDirectiveSet(IDVal, true);
  1216. case DK_EQUIV:
  1217. return parseDirectiveSet(IDVal, false);
  1218. case DK_ASCII:
  1219. return parseDirectiveAscii(IDVal, false);
  1220. case DK_ASCIZ:
  1221. case DK_STRING:
  1222. return parseDirectiveAscii(IDVal, true);
  1223. case DK_BYTE:
  1224. return parseDirectiveValue(1);
  1225. case DK_SHORT:
  1226. case DK_VALUE:
  1227. case DK_2BYTE:
  1228. return parseDirectiveValue(2);
  1229. case DK_LONG:
  1230. case DK_INT:
  1231. case DK_4BYTE:
  1232. return parseDirectiveValue(4);
  1233. case DK_QUAD:
  1234. case DK_8BYTE:
  1235. return parseDirectiveValue(8);
  1236. case DK_OCTA:
  1237. return parseDirectiveOctaValue();
  1238. case DK_SINGLE:
  1239. case DK_FLOAT:
  1240. return parseDirectiveRealValue(APFloat::IEEEsingle);
  1241. case DK_DOUBLE:
  1242. return parseDirectiveRealValue(APFloat::IEEEdouble);
  1243. case DK_ALIGN: {
  1244. bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
  1245. return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
  1246. }
  1247. case DK_ALIGN32: {
  1248. bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
  1249. return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
  1250. }
  1251. case DK_BALIGN:
  1252. return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
  1253. case DK_BALIGNW:
  1254. return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
  1255. case DK_BALIGNL:
  1256. return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
  1257. case DK_P2ALIGN:
  1258. return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
  1259. case DK_P2ALIGNW:
  1260. return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
  1261. case DK_P2ALIGNL:
  1262. return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
  1263. case DK_ORG:
  1264. return parseDirectiveOrg();
  1265. case DK_FILL:
  1266. return parseDirectiveFill();
  1267. case DK_ZERO:
  1268. return parseDirectiveZero();
  1269. case DK_EXTERN:
  1270. eatToEndOfStatement(); // .extern is the default, ignore it.
  1271. return false;
  1272. case DK_GLOBL:
  1273. case DK_GLOBAL:
  1274. return parseDirectiveSymbolAttribute(MCSA_Global);
  1275. case DK_LAZY_REFERENCE:
  1276. return parseDirectiveSymbolAttribute(MCSA_LazyReference);
  1277. case DK_NO_DEAD_STRIP:
  1278. return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
  1279. case DK_SYMBOL_RESOLVER:
  1280. return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
  1281. case DK_PRIVATE_EXTERN:
  1282. return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
  1283. case DK_REFERENCE:
  1284. return parseDirectiveSymbolAttribute(MCSA_Reference);
  1285. case DK_WEAK_DEFINITION:
  1286. return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
  1287. case DK_WEAK_REFERENCE:
  1288. return parseDirectiveSymbolAttribute(MCSA_WeakReference);
  1289. case DK_WEAK_DEF_CAN_BE_HIDDEN:
  1290. return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
  1291. case DK_COMM:
  1292. case DK_COMMON:
  1293. return parseDirectiveComm(/*IsLocal=*/false);
  1294. case DK_LCOMM:
  1295. return parseDirectiveComm(/*IsLocal=*/true);
  1296. case DK_ABORT:
  1297. return parseDirectiveAbort();
  1298. case DK_INCLUDE:
  1299. return parseDirectiveInclude();
  1300. case DK_INCBIN:
  1301. return parseDirectiveIncbin();
  1302. case DK_CODE16:
  1303. case DK_CODE16GCC:
  1304. return TokError(Twine(IDVal) + " not supported yet");
  1305. case DK_REPT:
  1306. return parseDirectiveRept(IDLoc, IDVal);
  1307. case DK_IRP:
  1308. return parseDirectiveIrp(IDLoc);
  1309. case DK_IRPC:
  1310. return parseDirectiveIrpc(IDLoc);
  1311. case DK_ENDR:
  1312. return parseDirectiveEndr(IDLoc);
  1313. case DK_BUNDLE_ALIGN_MODE:
  1314. return parseDirectiveBundleAlignMode();
  1315. case DK_BUNDLE_LOCK:
  1316. return parseDirectiveBundleLock();
  1317. case DK_BUNDLE_UNLOCK:
  1318. return parseDirectiveBundleUnlock();
  1319. case DK_SLEB128:
  1320. return parseDirectiveLEB128(true);
  1321. case DK_ULEB128:
  1322. return parseDirectiveLEB128(false);
  1323. case DK_SPACE:
  1324. case DK_SKIP:
  1325. return parseDirectiveSpace(IDVal);
  1326. case DK_FILE:
  1327. return parseDirectiveFile(IDLoc);
  1328. case DK_LINE:
  1329. return parseDirectiveLine();
  1330. case DK_LOC:
  1331. return parseDirectiveLoc();
  1332. case DK_STABS:
  1333. return parseDirectiveStabs();
  1334. case DK_CFI_SECTIONS:
  1335. return parseDirectiveCFISections();
  1336. case DK_CFI_STARTPROC:
  1337. return parseDirectiveCFIStartProc();
  1338. case DK_CFI_ENDPROC:
  1339. return parseDirectiveCFIEndProc();
  1340. case DK_CFI_DEF_CFA:
  1341. return parseDirectiveCFIDefCfa(IDLoc);
  1342. case DK_CFI_DEF_CFA_OFFSET:
  1343. return parseDirectiveCFIDefCfaOffset();
  1344. case DK_CFI_ADJUST_CFA_OFFSET:
  1345. return parseDirectiveCFIAdjustCfaOffset();
  1346. case DK_CFI_DEF_CFA_REGISTER:
  1347. return parseDirectiveCFIDefCfaRegister(IDLoc);
  1348. case DK_CFI_OFFSET:
  1349. return parseDirectiveCFIOffset(IDLoc);
  1350. case DK_CFI_REL_OFFSET:
  1351. return parseDirectiveCFIRelOffset(IDLoc);
  1352. case DK_CFI_PERSONALITY:
  1353. return parseDirectiveCFIPersonalityOrLsda(true);
  1354. case DK_CFI_LSDA:
  1355. return parseDirectiveCFIPersonalityOrLsda(false);
  1356. case DK_CFI_REMEMBER_STATE:
  1357. return parseDirectiveCFIRememberState();
  1358. case DK_CFI_RESTORE_STATE:
  1359. return parseDirectiveCFIRestoreState();
  1360. case DK_CFI_SAME_VALUE:
  1361. return parseDirectiveCFISameValue(IDLoc);
  1362. case DK_CFI_RESTORE:
  1363. return parseDirectiveCFIRestore(IDLoc);
  1364. case DK_CFI_ESCAPE:
  1365. return parseDirectiveCFIEscape();
  1366. case DK_CFI_SIGNAL_FRAME:
  1367. return parseDirectiveCFISignalFrame();
  1368. case DK_CFI_UNDEFINED:
  1369. return parseDirectiveCFIUndefined(IDLoc);
  1370. case DK_CFI_REGISTER:
  1371. return parseDirectiveCFIRegister(IDLoc);
  1372. case DK_CFI_WINDOW_SAVE:
  1373. return parseDirectiveCFIWindowSave();
  1374. case DK_MACROS_ON:
  1375. case DK_MACROS_OFF:
  1376. return parseDirectiveMacrosOnOff(IDVal);
  1377. case DK_MACRO:
  1378. return parseDirectiveMacro(IDLoc);
  1379. case DK_EXITM:
  1380. return parseDirectiveExitMacro(IDVal);
  1381. case DK_ENDM:
  1382. case DK_ENDMACRO:
  1383. return parseDirectiveEndMacro(IDVal);
  1384. case DK_PURGEM:
  1385. return parseDirectivePurgeMacro(IDLoc);
  1386. case DK_END:
  1387. return parseDirectiveEnd(IDLoc);
  1388. case DK_ERR:
  1389. return parseDirectiveError(IDLoc, false);
  1390. case DK_ERROR:
  1391. return parseDirectiveError(IDLoc, true);
  1392. case DK_WARNING:
  1393. return parseDirectiveWarning(IDLoc);
  1394. }
  1395. return Error(IDLoc, "unknown directive");
  1396. }
  1397. // __asm _emit or __asm __emit
  1398. if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
  1399. IDVal == "_EMIT" || IDVal == "__EMIT"))
  1400. return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
  1401. // __asm align
  1402. if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
  1403. return parseDirectiveMSAlign(IDLoc, Info);
  1404. checkForValidSection();
  1405. // Canonicalize the opcode to lower case.
  1406. std::string OpcodeStr = IDVal.lower();
  1407. ParseInstructionInfo IInfo(Info.AsmRewrites);
  1408. bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
  1409. Info.ParsedOperands);
  1410. Info.ParseError = HadError;
  1411. // Dump the parsed representation, if requested.
  1412. if (getShowParsedOperands()) {
  1413. SmallString<256> Str;
  1414. raw_svector_ostream OS(Str);
  1415. OS << "parsed instruction: [";
  1416. for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
  1417. if (i != 0)
  1418. OS << ", ";
  1419. Info.ParsedOperands[i]->print(OS);
  1420. }
  1421. OS << "]";
  1422. printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
  1423. }
  1424. // If we are generating dwarf for the current section then generate a .loc
  1425. // directive for the instruction.
  1426. if (!HadError && getContext().getGenDwarfForAssembly() &&
  1427. getContext().getGenDwarfSectionSyms().count(
  1428. getStreamer().getCurrentSection().first)) {
  1429. unsigned Line;
  1430. if (ActiveMacros.empty())
  1431. Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
  1432. else
  1433. Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc,
  1434. ActiveMacros.front()->ExitBuffer);
  1435. // If we previously parsed a cpp hash file line comment then make sure the
  1436. // current Dwarf File is for the CppHashFilename if not then emit the
  1437. // Dwarf File table for it and adjust the line number for the .loc.
  1438. if (CppHashFilename.size()) {
  1439. unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
  1440. 0, StringRef(), CppHashFilename);
  1441. getContext().setGenDwarfFileNumber(FileNumber);
  1442. // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
  1443. // cache with the different Loc from the call above we save the last
  1444. // info we queried here with SrcMgr.FindLineNumber().
  1445. unsigned CppHashLocLineNo;
  1446. if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
  1447. CppHashLocLineNo = LastQueryLine;
  1448. else {
  1449. CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
  1450. LastQueryLine = CppHashLocLineNo;
  1451. LastQueryIDLoc = CppHashLoc;
  1452. LastQueryBuffer = CppHashBuf;
  1453. }
  1454. Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
  1455. }
  1456. getStreamer().EmitDwarfLocDirective(
  1457. getContext().getGenDwarfFileNumber(), Line, 0,
  1458. DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
  1459. StringRef());
  1460. }
  1461. // If parsing succeeded, match the instruction.
  1462. if (!HadError) {
  1463. uint64_t ErrorInfo;
  1464. getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
  1465. Info.ParsedOperands, Out,
  1466. ErrorInfo, ParsingInlineAsm);
  1467. }
  1468. // Don't skip the rest of the line, the instruction parser is responsible for
  1469. // that.
  1470. return false;
  1471. }
  1472. /// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
  1473. /// since they may not be able to be tokenized to get to the end of line token.
  1474. void AsmParser::eatToEndOfLine() {
  1475. if (!Lexer.is(AsmToken::EndOfStatement))
  1476. Lexer.LexUntilEndOfLine();
  1477. // Eat EOL.
  1478. Lex();
  1479. }
  1480. /// parseCppHashLineFilenameComment as this:
  1481. /// ::= # number "filename"
  1482. /// or just as a full line comment if it doesn't have a number and a string.
  1483. bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
  1484. Lex(); // Eat the hash token.
  1485. if (getLexer().isNot(AsmToken::Integer)) {
  1486. // Consume the line since in cases it is not a well-formed line directive,
  1487. // as if were simply a full line comment.
  1488. eatToEndOfLine();
  1489. return false;
  1490. }
  1491. int64_t LineNumber = getTok().getIntVal();
  1492. Lex();
  1493. if (getLexer().isNot(AsmToken::String)) {
  1494. eatToEndOfLine();
  1495. return false;
  1496. }
  1497. StringRef Filename = getTok().getString();
  1498. // Get rid of the enclosing quotes.
  1499. Filename = Filename.substr(1, Filename.size() - 2);
  1500. // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
  1501. CppHashLoc = L;
  1502. CppHashFilename = Filename;
  1503. CppHashLineNumber = LineNumber;
  1504. CppHashBuf = CurBuffer;
  1505. // Ignore any trailing characters, they're just comment.
  1506. eatToEndOfLine();
  1507. return false;
  1508. }
  1509. /// \brief will use the last parsed cpp hash line filename comment
  1510. /// for the Filename and LineNo if any in the diagnostic.
  1511. void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
  1512. const AsmParser *Parser = static_cast<const AsmParser *>(Context);
  1513. raw_ostream &OS = errs();
  1514. const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
  1515. const SMLoc &DiagLoc = Diag.getLoc();
  1516. unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
  1517. unsigned CppHashBuf =
  1518. Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
  1519. // Like SourceMgr::printMessage() we need to print the include stack if any
  1520. // before printing the message.
  1521. unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
  1522. if (!Parser->SavedDiagHandler && DiagCurBuffer &&
  1523. DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
  1524. SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
  1525. DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
  1526. }
  1527. // If we have not parsed a cpp hash line filename comment or the source
  1528. // manager changed or buffer changed (like in a nested include) then just
  1529. // print the normal diagnostic using its Filename and LineNo.
  1530. if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
  1531. DiagBuf != CppHashBuf) {
  1532. if (Parser->SavedDiagHandler)
  1533. Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
  1534. else
  1535. Diag.print(nullptr, OS);
  1536. return;
  1537. }
  1538. // Use the CppHashFilename and calculate a line number based on the
  1539. // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
  1540. // the diagnostic.
  1541. const std::string &Filename = Parser->CppHashFilename;
  1542. int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
  1543. int CppHashLocLineNo =
  1544. Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
  1545. int LineNo =
  1546. Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
  1547. SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
  1548. Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
  1549. Diag.getLineContents(), Diag.getRanges());
  1550. if (Parser->SavedDiagHandler)
  1551. Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
  1552. else
  1553. NewDiag.print(nullptr, OS);
  1554. }
  1555. // FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
  1556. // difference being that that function accepts '@' as part of identifiers and
  1557. // we can't do that. AsmLexer.cpp should probably be changed to handle
  1558. // '@' as a special case when needed.
  1559. static bool isIdentifierChar(char c) {
  1560. return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
  1561. c == '.';
  1562. }
  1563. bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
  1564. ArrayRef<MCAsmMacroParameter> Parameters,
  1565. ArrayRef<MCAsmMacroArgument> A,
  1566. bool EnableAtPseudoVariable, const SMLoc &L) {
  1567. unsigned NParameters = Parameters.size();
  1568. bool HasVararg = NParameters ? Parameters.back().Vararg : false;
  1569. if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
  1570. return Error(L, "Wrong number of arguments");
  1571. // A macro without parameters is handled differently on Darwin:
  1572. // gas accepts no arguments and does no substitutions
  1573. while (!Body.empty()) {
  1574. // Scan for the next substitution.
  1575. std::size_t End = Body.size(), Pos = 0;
  1576. for (; Pos != End; ++Pos) {
  1577. // Check for a substitution or escape.
  1578. if (IsDarwin && !NParameters) {
  1579. // This macro has no parameters, look for $0, $1, etc.
  1580. if (Body[Pos] != '$' || Pos + 1 == End)
  1581. continue;
  1582. char Next = Body[Pos + 1];
  1583. if (Next == '$' || Next == 'n' ||
  1584. isdigit(static_cast<unsigned char>(Next)))
  1585. break;
  1586. } else {
  1587. // This macro has parameters, look for \foo, \bar, etc.
  1588. if (Body[Pos] == '\\' && Pos + 1 != End)
  1589. break;
  1590. }
  1591. }
  1592. // Add the prefix.
  1593. OS << Body.slice(0, Pos);
  1594. // Check if we reached the end.
  1595. if (Pos == End)
  1596. break;
  1597. if (IsDarwin && !NParameters) {
  1598. switch (Body[Pos + 1]) {
  1599. // $$ => $
  1600. case '$':
  1601. OS << '$';
  1602. break;
  1603. // $n => number of arguments
  1604. case 'n':
  1605. OS << A.size();
  1606. break;
  1607. // $[0-9] => argument
  1608. default: {
  1609. // Missing arguments are ignored.
  1610. unsigned Index = Body[Pos + 1] - '0';
  1611. if (Index >= A.size())
  1612. break;
  1613. // Otherwise substitute with the token values, with spaces eliminated.
  1614. for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
  1615. ie = A[Index].end();
  1616. it != ie; ++it)
  1617. OS << it->getString();
  1618. break;
  1619. }
  1620. }
  1621. Pos += 2;
  1622. } else {
  1623. unsigned I = Pos + 1;
  1624. // Check for the \@ pseudo-variable.
  1625. if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End)
  1626. ++I;
  1627. else
  1628. while (isIdentifierChar(Body[I]) && I + 1 != End)
  1629. ++I;
  1630. const char *Begin = Body.data() + Pos + 1;
  1631. StringRef Argument(Begin, I - (Pos + 1));
  1632. unsigned Index = 0;
  1633. if (Argument == "@") {
  1634. OS << NumOfMacroInstantiations;
  1635. Pos += 2;
  1636. } else {
  1637. for (; Index < NParameters; ++Index)
  1638. if (Parameters[Index].Name == Argument)
  1639. break;
  1640. if (Index == NParameters) {
  1641. if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
  1642. Pos += 3;
  1643. else {
  1644. OS << '\\' << Argument;
  1645. Pos = I;
  1646. }
  1647. } else {
  1648. bool VarargParameter = HasVararg && Index == (NParameters - 1);
  1649. for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
  1650. ie = A[Index].end();
  1651. it != ie; ++it)
  1652. // We expect no quotes around the string's contents when
  1653. // parsing for varargs.
  1654. if (it->getKind() != AsmToken::String || VarargParameter)
  1655. OS << it->getString();
  1656. else
  1657. OS << it->getStringContents();
  1658. Pos += 1 + Argument.size();
  1659. }
  1660. }
  1661. }
  1662. // Update the scan point.
  1663. Body = Body.substr(Pos);
  1664. }
  1665. return false;
  1666. }
  1667. MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL,
  1668. size_t CondStackDepth)
  1669. : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL),
  1670. CondStackDepth(CondStackDepth) {}
  1671. static bool isOperator(AsmToken::TokenKind kind) {
  1672. switch (kind) {
  1673. default:
  1674. return false;
  1675. case AsmToken::Plus:
  1676. case AsmToken::Minus:
  1677. case AsmToken::Tilde:
  1678. case AsmToken::Slash:
  1679. case AsmToken::Star:
  1680. case AsmToken::Dot:
  1681. case AsmToken::Equal:
  1682. case AsmToken::EqualEqual:
  1683. case AsmToken::Pipe:
  1684. case AsmToken::PipePipe:
  1685. case AsmToken::Caret:
  1686. case AsmToken::Amp:
  1687. case AsmToken::AmpAmp:
  1688. case AsmToken::Exclaim:
  1689. case AsmToken::ExclaimEqual:
  1690. case AsmToken::Percent:
  1691. case AsmToken::Less:
  1692. case AsmToken::LessEqual:
  1693. case AsmToken::LessLess:
  1694. case AsmToken::LessGreater:
  1695. case AsmToken::Greater:
  1696. case AsmToken::GreaterEqual:
  1697. case AsmToken::GreaterGreater:
  1698. return true;
  1699. }
  1700. }
  1701. namespace {
  1702. class AsmLexerSkipSpaceRAII {
  1703. public:
  1704. AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
  1705. Lexer.setSkipSpace(SkipSpace);
  1706. }
  1707. ~AsmLexerSkipSpaceRAII() {
  1708. Lexer.setSkipSpace(true);
  1709. }
  1710. private:
  1711. AsmLexer &Lexer;
  1712. };
  1713. }
  1714. bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
  1715. if (Vararg) {
  1716. if (Lexer.isNot(AsmToken::EndOfStatement)) {
  1717. StringRef Str = parseStringToEndOfStatement();
  1718. MA.emplace_back(AsmToken::String, Str);
  1719. }
  1720. return false;
  1721. }
  1722. unsigned ParenLevel = 0;
  1723. unsigned AddTokens = 0;
  1724. // Darwin doesn't use spaces to delmit arguments.
  1725. AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
  1726. for (;;) {
  1727. if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
  1728. return TokError("unexpected token in macro instantiation");
  1729. if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
  1730. break;
  1731. if (Lexer.is(AsmToken::Space)) {
  1732. Lex(); // Eat spaces
  1733. // Spaces can delimit parameters, but could also be part an expression.
  1734. // If the token after a space is an operator, add the token and the next
  1735. // one into this argument
  1736. if (!IsDarwin) {
  1737. if (isOperator(Lexer.getKind())) {
  1738. // Check to see whether the token is used as an operator,
  1739. // or part of an identifier
  1740. const char *NextChar = getTok().getEndLoc().getPointer();
  1741. if (*NextChar == ' ')
  1742. AddTokens = 2;
  1743. }
  1744. if (!AddTokens && ParenLevel == 0) {
  1745. break;
  1746. }
  1747. }
  1748. }
  1749. // handleMacroEntry relies on not advancing the lexer here
  1750. // to be able to fill in the remaining default parameter values
  1751. if (Lexer.is(AsmToken::EndOfStatement))
  1752. break;
  1753. // Adjust the current parentheses level.
  1754. if (Lexer.is(AsmToken::LParen))
  1755. ++ParenLevel;
  1756. else if (Lexer.is(AsmToken::RParen) && ParenLevel)
  1757. --ParenLevel;
  1758. // Append the token to the current argument list.
  1759. MA.push_back(getTok());
  1760. if (AddTokens)
  1761. AddTokens--;
  1762. Lex();
  1763. }
  1764. if (ParenLevel != 0)
  1765. return TokError("unbalanced parentheses in macro argument");
  1766. return false;
  1767. }
  1768. // Parse the macro instantiation arguments.
  1769. bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
  1770. MCAsmMacroArguments &A) {
  1771. const unsigned NParameters = M ? M->Parameters.size() : 0;
  1772. bool NamedParametersFound = false;
  1773. SmallVector<SMLoc, 4> FALocs;
  1774. A.resize(NParameters);
  1775. FALocs.resize(NParameters);
  1776. // Parse two kinds of macro invocations:
  1777. // - macros defined without any parameters accept an arbitrary number of them
  1778. // - macros defined with parameters accept at most that many of them
  1779. bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
  1780. for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
  1781. ++Parameter) {
  1782. SMLoc IDLoc = Lexer.getLoc();
  1783. MCAsmMacroParameter FA;
  1784. if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
  1785. if (parseIdentifier(FA.Name)) {
  1786. Error(IDLoc, "invalid argument identifier for formal argument");
  1787. eatToEndOfStatement();
  1788. return true;
  1789. }
  1790. if (!Lexer.is(AsmToken::Equal)) {
  1791. TokError("expected '=' after formal parameter identifier");
  1792. eatToEndOfStatement();
  1793. return true;
  1794. }
  1795. Lex();
  1796. NamedParametersFound = true;
  1797. }
  1798. if (NamedParametersFound && FA.Name.empty()) {
  1799. Error(IDLoc, "cannot mix positional and keyword arguments");
  1800. eatToEndOfStatement();
  1801. return true;
  1802. }
  1803. bool Vararg = HasVararg && Parameter == (NParameters - 1);
  1804. if (parseMacroArgument(FA.Value, Vararg))
  1805. return true;
  1806. unsigned PI = Parameter;
  1807. if (!FA.Name.empty()) {
  1808. unsigned FAI = 0;
  1809. for (FAI = 0; FAI < NParameters; ++FAI)
  1810. if (M->Parameters[FAI].Name == FA.Name)
  1811. break;
  1812. if (FAI >= NParameters) {
  1813. assert(M && "expected macro to be defined");
  1814. Error(IDLoc,
  1815. "parameter named '" + FA.Name + "' does not exist for macro '" +
  1816. M->Name + "'");
  1817. return true;
  1818. }
  1819. PI = FAI;
  1820. }
  1821. if (!FA.Value.empty()) {
  1822. if (A.size() <= PI)
  1823. A.resize(PI + 1);
  1824. A[PI] = FA.Value;
  1825. if (FALocs.size() <= PI)
  1826. FALocs.resize(PI + 1);
  1827. FALocs[PI] = Lexer.getLoc();
  1828. }
  1829. // At the end of the statement, fill in remaining arguments that have
  1830. // default values. If there aren't any, then the next argument is
  1831. // required but missing
  1832. if (Lexer.is(AsmToken::EndOfStatement)) {
  1833. bool Failure = false;
  1834. for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
  1835. if (A[FAI].empty()) {
  1836. if (M->Parameters[FAI].Required) {
  1837. Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
  1838. "missing value for required parameter "
  1839. "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
  1840. Failure = true;
  1841. }
  1842. if (!M->Parameters[FAI].Value.empty())
  1843. A[FAI] = M->Parameters[FAI].Value;
  1844. }
  1845. }
  1846. return Failure;
  1847. }
  1848. if (Lexer.is(AsmToken::Comma))
  1849. Lex();
  1850. }
  1851. return TokError("too many positional arguments");
  1852. }
  1853. const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
  1854. StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name);
  1855. return (I == MacroMap.end()) ? nullptr : &I->getValue();
  1856. }
  1857. void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) {
  1858. MacroMap.insert(std::make_pair(Name, std::move(Macro)));
  1859. }
  1860. void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); }
  1861. bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
  1862. // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
  1863. // this, although we should protect against infinite loops.
  1864. if (ActiveMacros.size() == 20)
  1865. return TokError("macros cannot be nested more than 20 levels deep");
  1866. MCAsmMacroArguments A;
  1867. if (parseMacroArguments(M, A))
  1868. return true;
  1869. // Macro instantiation is lexical, unfortunately. We construct a new buffer
  1870. // to hold the macro body with substitutions.
  1871. SmallString<256> Buf;
  1872. StringRef Body = M->Body;
  1873. raw_svector_ostream OS(Buf);
  1874. if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc()))
  1875. return true;
  1876. // We include the .endmacro in the buffer as our cue to exit the macro
  1877. // instantiation.
  1878. OS << ".endmacro\n";
  1879. std::unique_ptr<MemoryBuffer> Instantiation =
  1880. MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
  1881. // Create the macro instantiation object and add to the current macro
  1882. // instantiation stack.
  1883. MacroInstantiation *MI = new MacroInstantiation(
  1884. NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
  1885. ActiveMacros.push_back(MI);
  1886. ++NumOfMacroInstantiations;
  1887. // Jump to the macro instantiation and prime the lexer.
  1888. CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
  1889. Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
  1890. Lex();
  1891. return false;
  1892. }
  1893. void AsmParser::handleMacroExit() {
  1894. // Jump to the EndOfStatement we should return to, and consume it.
  1895. jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
  1896. Lex();
  1897. // Pop the instantiation entry.
  1898. delete ActiveMacros.back();
  1899. ActiveMacros.pop_back();
  1900. }
  1901. bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
  1902. bool NoDeadStrip) {
  1903. MCSymbol *Sym;
  1904. const MCExpr *Value;
  1905. if (MCParserUtils::parseAssignmentExpression(Name, allow_redef, *this, Sym,
  1906. Value))
  1907. return true;
  1908. if (!Sym) {
  1909. // In the case where we parse an expression starting with a '.', we will
  1910. // not generate an error, nor will we create a symbol. In this case we
  1911. // should just return out.
  1912. return false;
  1913. }
  1914. // Do the assignment.
  1915. Out.EmitAssignment(Sym, Value);
  1916. if (NoDeadStrip)
  1917. Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
  1918. return false;
  1919. }
  1920. /// parseIdentifier:
  1921. /// ::= identifier
  1922. /// ::= string
  1923. bool AsmParser::parseIdentifier(StringRef &Res) {
  1924. // The assembler has relaxed rules for accepting identifiers, in particular we
  1925. // allow things like '.globl $foo' and '.def @feat.00', which would normally be
  1926. // separate tokens. At this level, we have already lexed so we cannot (currently)
  1927. // handle this as a context dependent token, instead we detect adjacent tokens
  1928. // and return the combined identifier.
  1929. if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
  1930. SMLoc PrefixLoc = getLexer().getLoc();
  1931. // Consume the prefix character, and check for a following identifier.
  1932. Lex();
  1933. if (Lexer.isNot(AsmToken::Identifier))
  1934. return true;
  1935. // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
  1936. if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
  1937. return true;
  1938. // Construct the joined identifier and consume the token.
  1939. Res =
  1940. StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
  1941. Lex();
  1942. return false;
  1943. }
  1944. if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
  1945. return true;
  1946. Res = getTok().getIdentifier();
  1947. Lex(); // Consume the identifier token.
  1948. return false;
  1949. }
  1950. /// parseDirectiveSet:
  1951. /// ::= .equ identifier ',' expression
  1952. /// ::= .equiv identifier ',' expression
  1953. /// ::= .set identifier ',' expression
  1954. bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
  1955. StringRef Name;
  1956. if (parseIdentifier(Name))
  1957. return TokError("expected identifier after '" + Twine(IDVal) + "'");
  1958. if (getLexer().isNot(AsmToken::Comma))
  1959. return TokError("unexpected token in '" + Twine(IDVal) + "'");
  1960. Lex();
  1961. return parseAssignment(Name, allow_redef, true);
  1962. }
  1963. bool AsmParser::parseEscapedString(std::string &Data) {
  1964. assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
  1965. Data = "";
  1966. StringRef Str = getTok().getStringContents();
  1967. for (unsigned i = 0, e = Str.size(); i != e; ++i) {
  1968. if (Str[i] != '\\') {
  1969. Data += Str[i];
  1970. continue;
  1971. }
  1972. // Recognize escaped characters. Note that this escape semantics currently
  1973. // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
  1974. ++i;
  1975. if (i == e)
  1976. return TokError("unexpected backslash at end of string");
  1977. // Recognize octal sequences.
  1978. if ((unsigned)(Str[i] - '0') <= 7) {
  1979. // Consume up to three octal characters.
  1980. unsigned Value = Str[i] - '0';
  1981. if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
  1982. ++i;
  1983. Value = Value * 8 + (Str[i] - '0');
  1984. if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
  1985. ++i;
  1986. Value = Value * 8 + (Str[i] - '0');
  1987. }
  1988. }
  1989. if (Value > 255)
  1990. return TokError("invalid octal escape sequence (out of range)");
  1991. Data += (unsigned char)Value;
  1992. continue;
  1993. }
  1994. // Otherwise recognize individual escapes.
  1995. switch (Str[i]) {
  1996. default:
  1997. // Just reject invalid escape sequences for now.
  1998. return TokError("invalid escape sequence (unrecognized character)");
  1999. case 'b': Data += '\b'; break;
  2000. case 'f': Data += '\f'; break;
  2001. case 'n': Data += '\n'; break;
  2002. case 'r': Data += '\r'; break;
  2003. case 't': Data += '\t'; break;
  2004. case '"': Data += '"'; break;
  2005. case '\\': Data += '\\'; break;
  2006. }
  2007. }
  2008. return false;
  2009. }
  2010. /// parseDirectiveAscii:
  2011. /// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
  2012. bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
  2013. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  2014. checkForValidSection();
  2015. for (;;) {
  2016. if (getLexer().isNot(AsmToken::String))
  2017. return TokError("expected string in '" + Twine(IDVal) + "' directive");
  2018. std::string Data;
  2019. if (parseEscapedString(Data))
  2020. return true;
  2021. getStreamer().EmitBytes(Data);
  2022. if (ZeroTerminated)
  2023. getStreamer().EmitBytes(StringRef("\0", 1));
  2024. Lex();
  2025. if (getLexer().is(AsmToken::EndOfStatement))
  2026. break;
  2027. if (getLexer().isNot(AsmToken::Comma))
  2028. return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
  2029. Lex();
  2030. }
  2031. }
  2032. Lex();
  2033. return false;
  2034. }
  2035. /// parseDirectiveValue
  2036. /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
  2037. bool AsmParser::parseDirectiveValue(unsigned Size) {
  2038. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  2039. checkForValidSection();
  2040. for (;;) {
  2041. const MCExpr *Value;
  2042. SMLoc ExprLoc = getLexer().getLoc();
  2043. if (parseExpression(Value))
  2044. return true;
  2045. // Special case constant expressions to match code generator.
  2046. if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
  2047. assert(Size <= 8 && "Invalid size");
  2048. uint64_t IntValue = MCE->getValue();
  2049. if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
  2050. return Error(ExprLoc, "literal value out of range for directive");
  2051. getStreamer().EmitIntValue(IntValue, Size);
  2052. } else
  2053. getStreamer().EmitValue(Value, Size, ExprLoc);
  2054. if (getLexer().is(AsmToken::EndOfStatement))
  2055. break;
  2056. // FIXME: Improve diagnostic.
  2057. if (getLexer().isNot(AsmToken::Comma))
  2058. return TokError("unexpected token in directive");
  2059. Lex();
  2060. }
  2061. }
  2062. Lex();
  2063. return false;
  2064. }
  2065. /// ParseDirectiveOctaValue
  2066. /// ::= .octa [ hexconstant (, hexconstant)* ]
  2067. bool AsmParser::parseDirectiveOctaValue() {
  2068. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  2069. checkForValidSection();
  2070. for (;;) {
  2071. if (Lexer.getKind() == AsmToken::Error)
  2072. return true;
  2073. if (Lexer.getKind() != AsmToken::Integer &&
  2074. Lexer.getKind() != AsmToken::BigNum)
  2075. return TokError("unknown token in expression");
  2076. SMLoc ExprLoc = getLexer().getLoc();
  2077. APInt IntValue = getTok().getAPIntVal();
  2078. Lex();
  2079. uint64_t hi, lo;
  2080. if (IntValue.isIntN(64)) {
  2081. hi = 0;
  2082. lo = IntValue.getZExtValue();
  2083. } else if (IntValue.isIntN(128)) {
  2084. // It might actually have more than 128 bits, but the top ones are zero.
  2085. hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
  2086. lo = IntValue.getLoBits(64).getZExtValue();
  2087. } else
  2088. return Error(ExprLoc, "literal value out of range for directive");
  2089. if (MAI.isLittleEndian()) {
  2090. getStreamer().EmitIntValue(lo, 8);
  2091. getStreamer().EmitIntValue(hi, 8);
  2092. } else {
  2093. getStreamer().EmitIntValue(hi, 8);
  2094. getStreamer().EmitIntValue(lo, 8);
  2095. }
  2096. if (getLexer().is(AsmToken::EndOfStatement))
  2097. break;
  2098. // FIXME: Improve diagnostic.
  2099. if (getLexer().isNot(AsmToken::Comma))
  2100. return TokError("unexpected token in directive");
  2101. Lex();
  2102. }
  2103. }
  2104. Lex();
  2105. return false;
  2106. }
  2107. /// parseDirectiveRealValue
  2108. /// ::= (.single | .double) [ expression (, expression)* ]
  2109. bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
  2110. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  2111. checkForValidSection();
  2112. for (;;) {
  2113. // We don't truly support arithmetic on floating point expressions, so we
  2114. // have to manually parse unary prefixes.
  2115. bool IsNeg = false;
  2116. if (getLexer().is(AsmToken::Minus)) {
  2117. Lex();
  2118. IsNeg = true;
  2119. } else if (getLexer().is(AsmToken::Plus))
  2120. Lex();
  2121. if (getLexer().isNot(AsmToken::Integer) &&
  2122. getLexer().isNot(AsmToken::Real) &&
  2123. getLexer().isNot(AsmToken::Identifier))
  2124. return TokError("unexpected token in directive");
  2125. // Convert to an APFloat.
  2126. APFloat Value(Semantics);
  2127. StringRef IDVal = getTok().getString();
  2128. if (getLexer().is(AsmToken::Identifier)) {
  2129. if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
  2130. Value = APFloat::getInf(Semantics);
  2131. else if (!IDVal.compare_lower("nan"))
  2132. Value = APFloat::getNaN(Semantics, false, ~0);
  2133. else
  2134. return TokError("invalid floating point literal");
  2135. } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
  2136. APFloat::opInvalidOp)
  2137. return TokError("invalid floating point literal");
  2138. if (IsNeg)
  2139. Value.changeSign();
  2140. // Consume the numeric token.
  2141. Lex();
  2142. // Emit the value as an integer.
  2143. APInt AsInt = Value.bitcastToAPInt();
  2144. getStreamer().EmitIntValue(AsInt.getLimitedValue(),
  2145. AsInt.getBitWidth() / 8);
  2146. if (getLexer().is(AsmToken::EndOfStatement))
  2147. break;
  2148. if (getLexer().isNot(AsmToken::Comma))
  2149. return TokError("unexpected token in directive");
  2150. Lex();
  2151. }
  2152. }
  2153. Lex();
  2154. return false;
  2155. }
  2156. /// parseDirectiveZero
  2157. /// ::= .zero expression
  2158. bool AsmParser::parseDirectiveZero() {
  2159. checkForValidSection();
  2160. int64_t NumBytes;
  2161. if (parseAbsoluteExpression(NumBytes))
  2162. return true;
  2163. int64_t Val = 0;
  2164. if (getLexer().is(AsmToken::Comma)) {
  2165. Lex();
  2166. if (parseAbsoluteExpression(Val))
  2167. return true;
  2168. }
  2169. if (getLexer().isNot(AsmToken::EndOfStatement))
  2170. return TokError("unexpected token in '.zero' directive");
  2171. Lex();
  2172. getStreamer().EmitFill(NumBytes, Val);
  2173. return false;
  2174. }
  2175. /// parseDirectiveFill
  2176. /// ::= .fill expression [ , expression [ , expression ] ]
  2177. bool AsmParser::parseDirectiveFill() {
  2178. checkForValidSection();
  2179. SMLoc RepeatLoc = getLexer().getLoc();
  2180. int64_t NumValues;
  2181. if (parseAbsoluteExpression(NumValues))
  2182. return true;
  2183. if (NumValues < 0) {
  2184. Warning(RepeatLoc,
  2185. "'.fill' directive with negative repeat count has no effect");
  2186. NumValues = 0;
  2187. }
  2188. int64_t FillSize = 1;
  2189. int64_t FillExpr = 0;
  2190. SMLoc SizeLoc, ExprLoc;
  2191. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  2192. if (getLexer().isNot(AsmToken::Comma))
  2193. return TokError("unexpected token in '.fill' directive");
  2194. Lex();
  2195. SizeLoc = getLexer().getLoc();
  2196. if (parseAbsoluteExpression(FillSize))
  2197. return true;
  2198. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  2199. if (getLexer().isNot(AsmToken::Comma))
  2200. return TokError("unexpected token in '.fill' directive");
  2201. Lex();
  2202. ExprLoc = getLexer().getLoc();
  2203. if (parseAbsoluteExpression(FillExpr))
  2204. return true;
  2205. if (getLexer().isNot(AsmToken::EndOfStatement))
  2206. return TokError("unexpected token in '.fill' directive");
  2207. Lex();
  2208. }
  2209. }
  2210. if (FillSize < 0) {
  2211. Warning(SizeLoc, "'.fill' directive with negative size has no effect");
  2212. NumValues = 0;
  2213. }
  2214. if (FillSize > 8) {
  2215. Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
  2216. FillSize = 8;
  2217. }
  2218. if (!isUInt<32>(FillExpr) && FillSize > 4)
  2219. Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
  2220. if (NumValues > 0) {
  2221. int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
  2222. FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
  2223. for (uint64_t i = 0, e = NumValues; i != e; ++i) {
  2224. getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
  2225. if (NonZeroFillSize < FillSize)
  2226. getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
  2227. }
  2228. }
  2229. return false;
  2230. }
  2231. /// parseDirectiveOrg
  2232. /// ::= .org expression [ , expression ]
  2233. bool AsmParser::parseDirectiveOrg() {
  2234. checkForValidSection();
  2235. const MCExpr *Offset;
  2236. SMLoc Loc = getTok().getLoc();
  2237. if (parseExpression(Offset))
  2238. return true;
  2239. // Parse optional fill expression.
  2240. int64_t FillExpr = 0;
  2241. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  2242. if (getLexer().isNot(AsmToken::Comma))
  2243. return TokError("unexpected token in '.org' directive");
  2244. Lex();
  2245. if (parseAbsoluteExpression(FillExpr))
  2246. return true;
  2247. if (getLexer().isNot(AsmToken::EndOfStatement))
  2248. return TokError("unexpected token in '.org' directive");
  2249. }
  2250. Lex();
  2251. // Only limited forms of relocatable expressions are accepted here, it
  2252. // has to be relative to the current section. The streamer will return
  2253. // 'true' if the expression wasn't evaluatable.
  2254. if (getStreamer().EmitValueToOffset(Offset, FillExpr))
  2255. return Error(Loc, "expected assembly-time absolute expression");
  2256. return false;
  2257. }
  2258. /// parseDirectiveAlign
  2259. /// ::= {.align, ...} expression [ , expression [ , expression ]]
  2260. bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
  2261. checkForValidSection();
  2262. SMLoc AlignmentLoc = getLexer().getLoc();
  2263. int64_t Alignment;
  2264. if (parseAbsoluteExpression(Alignment))
  2265. return true;
  2266. SMLoc MaxBytesLoc;
  2267. bool HasFillExpr = false;
  2268. int64_t FillExpr = 0;
  2269. int64_t MaxBytesToFill = 0;
  2270. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  2271. if (getLexer().isNot(AsmToken::Comma))
  2272. return TokError("unexpected token in directive");
  2273. Lex();
  2274. // The fill expression can be omitted while specifying a maximum number of
  2275. // alignment bytes, e.g:
  2276. // .align 3,,4
  2277. if (getLexer().isNot(AsmToken::Comma)) {
  2278. HasFillExpr = true;
  2279. if (parseAbsoluteExpression(FillExpr))
  2280. return true;
  2281. }
  2282. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  2283. if (getLexer().isNot(AsmToken::Comma))
  2284. return TokError("unexpected token in directive");
  2285. Lex();
  2286. MaxBytesLoc = getLexer().getLoc();
  2287. if (parseAbsoluteExpression(MaxBytesToFill))
  2288. return true;
  2289. if (getLexer().isNot(AsmToken::EndOfStatement))
  2290. return TokError("unexpected token in directive");
  2291. }
  2292. }
  2293. Lex();
  2294. if (!HasFillExpr)
  2295. FillExpr = 0;
  2296. // Compute alignment in bytes.
  2297. if (IsPow2) {
  2298. // FIXME: Diagnose overflow.
  2299. if (Alignment >= 32) {
  2300. Error(AlignmentLoc, "invalid alignment value");
  2301. Alignment = 31;
  2302. }
  2303. Alignment = 1ULL << Alignment;
  2304. } else {
  2305. // Reject alignments that aren't a power of two, for gas compatibility.
  2306. if (!isPowerOf2_64(Alignment))
  2307. Error(AlignmentLoc, "alignment must be a power of 2");
  2308. }
  2309. // Diagnose non-sensical max bytes to align.
  2310. if (MaxBytesLoc.isValid()) {
  2311. if (MaxBytesToFill < 1) {
  2312. Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
  2313. "many bytes, ignoring maximum bytes expression");
  2314. MaxBytesToFill = 0;
  2315. }
  2316. if (MaxBytesToFill >= Alignment) {
  2317. Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
  2318. "has no effect");
  2319. MaxBytesToFill = 0;
  2320. }
  2321. }
  2322. // Check whether we should use optimal code alignment for this .align
  2323. // directive.
  2324. const MCSection *Section = getStreamer().getCurrentSection().first;
  2325. assert(Section && "must have section to emit alignment");
  2326. bool UseCodeAlign = Section->UseCodeAlign();
  2327. if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
  2328. ValueSize == 1 && UseCodeAlign) {
  2329. getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
  2330. } else {
  2331. // FIXME: Target specific behavior about how the "extra" bytes are filled.
  2332. getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
  2333. MaxBytesToFill);
  2334. }
  2335. return false;
  2336. }
  2337. /// parseDirectiveFile
  2338. /// ::= .file [number] filename
  2339. /// ::= .file number directory filename
  2340. bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
  2341. // FIXME: I'm not sure what this is.
  2342. int64_t FileNumber = -1;
  2343. SMLoc FileNumberLoc = getLexer().getLoc();
  2344. if (getLexer().is(AsmToken::Integer)) {
  2345. FileNumber = getTok().getIntVal();
  2346. Lex();
  2347. if (FileNumber < 1)
  2348. return TokError("file number less than one");
  2349. }
  2350. if (getLexer().isNot(AsmToken::String))
  2351. return TokError("unexpected token in '.file' directive");
  2352. // Usually the directory and filename together, otherwise just the directory.
  2353. // Allow the strings to have escaped octal character sequence.
  2354. std::string Path = getTok().getString();
  2355. if (parseEscapedString(Path))
  2356. return true;
  2357. Lex();
  2358. StringRef Directory;
  2359. StringRef Filename;
  2360. std::string FilenameData;
  2361. if (getLexer().is(AsmToken::String)) {
  2362. if (FileNumber == -1)
  2363. return TokError("explicit path specified, but no file number");
  2364. if (parseEscapedString(FilenameData))
  2365. return true;
  2366. Filename = FilenameData;
  2367. Directory = Path;
  2368. Lex();
  2369. } else {
  2370. Filename = Path;
  2371. }
  2372. if (getLexer().isNot(AsmToken::EndOfStatement))
  2373. return TokError("unexpected token in '.file' directive");
  2374. if (FileNumber == -1)
  2375. getStreamer().EmitFileDirective(Filename);
  2376. else {
  2377. if (getContext().getGenDwarfForAssembly())
  2378. Error(DirectiveLoc,
  2379. "input can't have .file dwarf directives when -g is "
  2380. "used to generate dwarf debug info for assembly code");
  2381. if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
  2382. 0)
  2383. Error(FileNumberLoc, "file number already allocated");
  2384. }
  2385. return false;
  2386. }
  2387. /// parseDirectiveLine
  2388. /// ::= .line [number]
  2389. bool AsmParser::parseDirectiveLine() {
  2390. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  2391. if (getLexer().isNot(AsmToken::Integer))
  2392. return TokError("unexpected token in '.line' directive");
  2393. int64_t LineNumber = getTok().getIntVal();
  2394. (void)LineNumber;
  2395. Lex();
  2396. // FIXME: Do something with the .line.
  2397. }
  2398. if (getLexer().isNot(AsmToken::EndOfStatement))
  2399. return TokError("unexpected token in '.line' directive");
  2400. return false;
  2401. }
  2402. /// parseDirectiveLoc
  2403. /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
  2404. /// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
  2405. /// The first number is a file number, must have been previously assigned with
  2406. /// a .file directive, the second number is the line number and optionally the
  2407. /// third number is a column position (zero if not specified). The remaining
  2408. /// optional items are .loc sub-directives.
  2409. bool AsmParser::parseDirectiveLoc() {
  2410. if (getLexer().isNot(AsmToken::Integer))
  2411. return TokError("unexpected token in '.loc' directive");
  2412. int64_t FileNumber = getTok().getIntVal();
  2413. if (FileNumber < 1)
  2414. return TokError("file number less than one in '.loc' directive");
  2415. if (!getContext().isValidDwarfFileNumber(FileNumber))
  2416. return TokError("unassigned file number in '.loc' directive");
  2417. Lex();
  2418. int64_t LineNumber = 0;
  2419. if (getLexer().is(AsmToken::Integer)) {
  2420. LineNumber = getTok().getIntVal();
  2421. if (LineNumber < 0)
  2422. return TokError("line number less than zero in '.loc' directive");
  2423. Lex();
  2424. }
  2425. int64_t ColumnPos = 0;
  2426. if (getLexer().is(AsmToken::Integer)) {
  2427. ColumnPos = getTok().getIntVal();
  2428. if (ColumnPos < 0)
  2429. return TokError("column position less than zero in '.loc' directive");
  2430. Lex();
  2431. }
  2432. unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
  2433. unsigned Isa = 0;
  2434. int64_t Discriminator = 0;
  2435. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  2436. for (;;) {
  2437. if (getLexer().is(AsmToken::EndOfStatement))
  2438. break;
  2439. StringRef Name;
  2440. SMLoc Loc = getTok().getLoc();
  2441. if (parseIdentifier(Name))
  2442. return TokError("unexpected token in '.loc' directive");
  2443. if (Name == "basic_block")
  2444. Flags |= DWARF2_FLAG_BASIC_BLOCK;
  2445. else if (Name == "prologue_end")
  2446. Flags |= DWARF2_FLAG_PROLOGUE_END;
  2447. else if (Name == "epilogue_begin")
  2448. Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
  2449. else if (Name == "is_stmt") {
  2450. Loc = getTok().getLoc();
  2451. const MCExpr *Value;
  2452. if (parseExpression(Value))
  2453. return true;
  2454. // The expression must be the constant 0 or 1.
  2455. if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
  2456. int Value = MCE->getValue();
  2457. if (Value == 0)
  2458. Flags &= ~DWARF2_FLAG_IS_STMT;
  2459. else if (Value == 1)
  2460. Flags |= DWARF2_FLAG_IS_STMT;
  2461. else
  2462. return Error(Loc, "is_stmt value not 0 or 1");
  2463. } else {
  2464. return Error(Loc, "is_stmt value not the constant value of 0 or 1");
  2465. }
  2466. } else if (Name == "isa") {
  2467. Loc = getTok().getLoc();
  2468. const MCExpr *Value;
  2469. if (parseExpression(Value))
  2470. return true;
  2471. // The expression must be a constant greater or equal to 0.
  2472. if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
  2473. int Value = MCE->getValue();
  2474. if (Value < 0)
  2475. return Error(Loc, "isa number less than zero");
  2476. Isa = Value;
  2477. } else {
  2478. return Error(Loc, "isa number not a constant value");
  2479. }
  2480. } else if (Name == "discriminator") {
  2481. if (parseAbsoluteExpression(Discriminator))
  2482. return true;
  2483. } else {
  2484. return Error(Loc, "unknown sub-directive in '.loc' directive");
  2485. }
  2486. if (getLexer().is(AsmToken::EndOfStatement))
  2487. break;
  2488. }
  2489. }
  2490. getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
  2491. Isa, Discriminator, StringRef());
  2492. return false;
  2493. }
  2494. /// parseDirectiveStabs
  2495. /// ::= .stabs string, number, number, number
  2496. bool AsmParser::parseDirectiveStabs() {
  2497. return TokError("unsupported directive '.stabs'");
  2498. }
  2499. /// parseDirectiveCFISections
  2500. /// ::= .cfi_sections section [, section]
  2501. bool AsmParser::parseDirectiveCFISections() {
  2502. StringRef Name;
  2503. bool EH = false;
  2504. bool Debug = false;
  2505. if (parseIdentifier(Name))
  2506. return TokError("Expected an identifier");
  2507. if (Name == ".eh_frame")
  2508. EH = true;
  2509. else if (Name == ".debug_frame")
  2510. Debug = true;
  2511. if (getLexer().is(AsmToken::Comma)) {
  2512. Lex();
  2513. if (parseIdentifier(Name))
  2514. return TokError("Expected an identifier");
  2515. if (Name == ".eh_frame")
  2516. EH = true;
  2517. else if (Name == ".debug_frame")
  2518. Debug = true;
  2519. }
  2520. getStreamer().EmitCFISections(EH, Debug);
  2521. return false;
  2522. }
  2523. /// parseDirectiveCFIStartProc
  2524. /// ::= .cfi_startproc [simple]
  2525. bool AsmParser::parseDirectiveCFIStartProc() {
  2526. StringRef Simple;
  2527. if (getLexer().isNot(AsmToken::EndOfStatement))
  2528. if (parseIdentifier(Simple) || Simple != "simple")
  2529. return TokError("unexpected token in .cfi_startproc directive");
  2530. getStreamer().EmitCFIStartProc(!Simple.empty());
  2531. return false;
  2532. }
  2533. /// parseDirectiveCFIEndProc
  2534. /// ::= .cfi_endproc
  2535. bool AsmParser::parseDirectiveCFIEndProc() {
  2536. getStreamer().EmitCFIEndProc();
  2537. return false;
  2538. }
  2539. /// \brief parse register name or number.
  2540. bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
  2541. SMLoc DirectiveLoc) {
  2542. unsigned RegNo;
  2543. if (getLexer().isNot(AsmToken::Integer)) {
  2544. if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
  2545. return true;
  2546. Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
  2547. } else
  2548. return parseAbsoluteExpression(Register);
  2549. return false;
  2550. }
  2551. /// parseDirectiveCFIDefCfa
  2552. /// ::= .cfi_def_cfa register, offset
  2553. bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
  2554. int64_t Register = 0;
  2555. if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
  2556. return true;
  2557. if (getLexer().isNot(AsmToken::Comma))
  2558. return TokError("unexpected token in directive");
  2559. Lex();
  2560. int64_t Offset = 0;
  2561. if (parseAbsoluteExpression(Offset))
  2562. return true;
  2563. getStreamer().EmitCFIDefCfa(Register, Offset);
  2564. return false;
  2565. }
  2566. /// parseDirectiveCFIDefCfaOffset
  2567. /// ::= .cfi_def_cfa_offset offset
  2568. bool AsmParser::parseDirectiveCFIDefCfaOffset() {
  2569. int64_t Offset = 0;
  2570. if (parseAbsoluteExpression(Offset))
  2571. return true;
  2572. getStreamer().EmitCFIDefCfaOffset(Offset);
  2573. return false;
  2574. }
  2575. /// parseDirectiveCFIRegister
  2576. /// ::= .cfi_register register, register
  2577. bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
  2578. int64_t Register1 = 0;
  2579. if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
  2580. return true;
  2581. if (getLexer().isNot(AsmToken::Comma))
  2582. return TokError("unexpected token in directive");
  2583. Lex();
  2584. int64_t Register2 = 0;
  2585. if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
  2586. return true;
  2587. getStreamer().EmitCFIRegister(Register1, Register2);
  2588. return false;
  2589. }
  2590. /// parseDirectiveCFIWindowSave
  2591. /// ::= .cfi_window_save
  2592. bool AsmParser::parseDirectiveCFIWindowSave() {
  2593. getStreamer().EmitCFIWindowSave();
  2594. return false;
  2595. }
  2596. /// parseDirectiveCFIAdjustCfaOffset
  2597. /// ::= .cfi_adjust_cfa_offset adjustment
  2598. bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
  2599. int64_t Adjustment = 0;
  2600. if (parseAbsoluteExpression(Adjustment))
  2601. return true;
  2602. getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
  2603. return false;
  2604. }
  2605. /// parseDirectiveCFIDefCfaRegister
  2606. /// ::= .cfi_def_cfa_register register
  2607. bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
  2608. int64_t Register = 0;
  2609. if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
  2610. return true;
  2611. getStreamer().EmitCFIDefCfaRegister(Register);
  2612. return false;
  2613. }
  2614. /// parseDirectiveCFIOffset
  2615. /// ::= .cfi_offset register, offset
  2616. bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
  2617. int64_t Register = 0;
  2618. int64_t Offset = 0;
  2619. if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
  2620. return true;
  2621. if (getLexer().isNot(AsmToken::Comma))
  2622. return TokError("unexpected token in directive");
  2623. Lex();
  2624. if (parseAbsoluteExpression(Offset))
  2625. return true;
  2626. getStreamer().EmitCFIOffset(Register, Offset);
  2627. return false;
  2628. }
  2629. /// parseDirectiveCFIRelOffset
  2630. /// ::= .cfi_rel_offset register, offset
  2631. bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
  2632. int64_t Register = 0;
  2633. if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
  2634. return true;
  2635. if (getLexer().isNot(AsmToken::Comma))
  2636. return TokError("unexpected token in directive");
  2637. Lex();
  2638. int64_t Offset = 0;
  2639. if (parseAbsoluteExpression(Offset))
  2640. return true;
  2641. getStreamer().EmitCFIRelOffset(Register, Offset);
  2642. return false;
  2643. }
  2644. static bool isValidEncoding(int64_t Encoding) {
  2645. if (Encoding & ~0xff)
  2646. return false;
  2647. if (Encoding == dwarf::DW_EH_PE_omit)
  2648. return true;
  2649. const unsigned Format = Encoding & 0xf;
  2650. if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
  2651. Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
  2652. Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
  2653. Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
  2654. return false;
  2655. const unsigned Application = Encoding & 0x70;
  2656. if (Application != dwarf::DW_EH_PE_absptr &&
  2657. Application != dwarf::DW_EH_PE_pcrel)
  2658. return false;
  2659. return true;
  2660. }
  2661. /// parseDirectiveCFIPersonalityOrLsda
  2662. /// IsPersonality true for cfi_personality, false for cfi_lsda
  2663. /// ::= .cfi_personality encoding, [symbol_name]
  2664. /// ::= .cfi_lsda encoding, [symbol_name]
  2665. bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
  2666. int64_t Encoding = 0;
  2667. if (parseAbsoluteExpression(Encoding))
  2668. return true;
  2669. if (Encoding == dwarf::DW_EH_PE_omit)
  2670. return false;
  2671. if (!isValidEncoding(Encoding))
  2672. return TokError("unsupported encoding.");
  2673. if (getLexer().isNot(AsmToken::Comma))
  2674. return TokError("unexpected token in directive");
  2675. Lex();
  2676. StringRef Name;
  2677. if (parseIdentifier(Name))
  2678. return TokError("expected identifier in directive");
  2679. MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
  2680. if (IsPersonality)
  2681. getStreamer().EmitCFIPersonality(Sym, Encoding);
  2682. else
  2683. getStreamer().EmitCFILsda(Sym, Encoding);
  2684. return false;
  2685. }
  2686. /// parseDirectiveCFIRememberState
  2687. /// ::= .cfi_remember_state
  2688. bool AsmParser::parseDirectiveCFIRememberState() {
  2689. getStreamer().EmitCFIRememberState();
  2690. return false;
  2691. }
  2692. /// parseDirectiveCFIRestoreState
  2693. /// ::= .cfi_remember_state
  2694. bool AsmParser::parseDirectiveCFIRestoreState() {
  2695. getStreamer().EmitCFIRestoreState();
  2696. return false;
  2697. }
  2698. /// parseDirectiveCFISameValue
  2699. /// ::= .cfi_same_value register
  2700. bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
  2701. int64_t Register = 0;
  2702. if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
  2703. return true;
  2704. getStreamer().EmitCFISameValue(Register);
  2705. return false;
  2706. }
  2707. /// parseDirectiveCFIRestore
  2708. /// ::= .cfi_restore register
  2709. bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
  2710. int64_t Register = 0;
  2711. if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
  2712. return true;
  2713. getStreamer().EmitCFIRestore(Register);
  2714. return false;
  2715. }
  2716. /// parseDirectiveCFIEscape
  2717. /// ::= .cfi_escape expression[,...]
  2718. bool AsmParser::parseDirectiveCFIEscape() {
  2719. std::string Values;
  2720. int64_t CurrValue;
  2721. if (parseAbsoluteExpression(CurrValue))
  2722. return true;
  2723. Values.push_back((uint8_t)CurrValue);
  2724. while (getLexer().is(AsmToken::Comma)) {
  2725. Lex();
  2726. if (parseAbsoluteExpression(CurrValue))
  2727. return true;
  2728. Values.push_back((uint8_t)CurrValue);
  2729. }
  2730. getStreamer().EmitCFIEscape(Values);
  2731. return false;
  2732. }
  2733. /// parseDirectiveCFISignalFrame
  2734. /// ::= .cfi_signal_frame
  2735. bool AsmParser::parseDirectiveCFISignalFrame() {
  2736. if (getLexer().isNot(AsmToken::EndOfStatement))
  2737. return Error(getLexer().getLoc(),
  2738. "unexpected token in '.cfi_signal_frame'");
  2739. getStreamer().EmitCFISignalFrame();
  2740. return false;
  2741. }
  2742. /// parseDirectiveCFIUndefined
  2743. /// ::= .cfi_undefined register
  2744. bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
  2745. int64_t Register = 0;
  2746. if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
  2747. return true;
  2748. getStreamer().EmitCFIUndefined(Register);
  2749. return false;
  2750. }
  2751. /// parseDirectiveMacrosOnOff
  2752. /// ::= .macros_on
  2753. /// ::= .macros_off
  2754. bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
  2755. if (getLexer().isNot(AsmToken::EndOfStatement))
  2756. return Error(getLexer().getLoc(),
  2757. "unexpected token in '" + Directive + "' directive");
  2758. setMacrosEnabled(Directive == ".macros_on");
  2759. return false;
  2760. }
  2761. /// parseDirectiveMacro
  2762. /// ::= .macro name[,] [parameters]
  2763. bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
  2764. StringRef Name;
  2765. if (parseIdentifier(Name))
  2766. return TokError("expected identifier in '.macro' directive");
  2767. if (getLexer().is(AsmToken::Comma))
  2768. Lex();
  2769. MCAsmMacroParameters Parameters;
  2770. while (getLexer().isNot(AsmToken::EndOfStatement)) {
  2771. if (!Parameters.empty() && Parameters.back().Vararg)
  2772. return Error(Lexer.getLoc(),
  2773. "Vararg parameter '" + Parameters.back().Name +
  2774. "' should be last one in the list of parameters.");
  2775. MCAsmMacroParameter Parameter;
  2776. if (parseIdentifier(Parameter.Name))
  2777. return TokError("expected identifier in '.macro' directive");
  2778. if (Lexer.is(AsmToken::Colon)) {
  2779. Lex(); // consume ':'
  2780. SMLoc QualLoc;
  2781. StringRef Qualifier;
  2782. QualLoc = Lexer.getLoc();
  2783. if (parseIdentifier(Qualifier))
  2784. return Error(QualLoc, "missing parameter qualifier for "
  2785. "'" + Parameter.Name + "' in macro '" + Name + "'");
  2786. if (Qualifier == "req")
  2787. Parameter.Required = true;
  2788. else if (Qualifier == "vararg")
  2789. Parameter.Vararg = true;
  2790. else
  2791. return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
  2792. "for '" + Parameter.Name + "' in macro '" + Name + "'");
  2793. }
  2794. if (getLexer().is(AsmToken::Equal)) {
  2795. Lex();
  2796. SMLoc ParamLoc;
  2797. ParamLoc = Lexer.getLoc();
  2798. if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
  2799. return true;
  2800. if (Parameter.Required)
  2801. Warning(ParamLoc, "pointless default value for required parameter "
  2802. "'" + Parameter.Name + "' in macro '" + Name + "'");
  2803. }
  2804. Parameters.push_back(std::move(Parameter));
  2805. if (getLexer().is(AsmToken::Comma))
  2806. Lex();
  2807. }
  2808. // Eat the end of statement.
  2809. Lex();
  2810. AsmToken EndToken, StartToken = getTok();
  2811. unsigned MacroDepth = 0;
  2812. // Lex the macro definition.
  2813. for (;;) {
  2814. // Check whether we have reached the end of the file.
  2815. if (getLexer().is(AsmToken::Eof))
  2816. return Error(DirectiveLoc, "no matching '.endmacro' in definition");
  2817. // Otherwise, check whether we have reach the .endmacro.
  2818. if (getLexer().is(AsmToken::Identifier)) {
  2819. if (getTok().getIdentifier() == ".endm" ||
  2820. getTok().getIdentifier() == ".endmacro") {
  2821. if (MacroDepth == 0) { // Outermost macro.
  2822. EndToken = getTok();
  2823. Lex();
  2824. if (getLexer().isNot(AsmToken::EndOfStatement))
  2825. return TokError("unexpected token in '" + EndToken.getIdentifier() +
  2826. "' directive");
  2827. break;
  2828. } else {
  2829. // Otherwise we just found the end of an inner macro.
  2830. --MacroDepth;
  2831. }
  2832. } else if (getTok().getIdentifier() == ".macro") {
  2833. // We allow nested macros. Those aren't instantiated until the outermost
  2834. // macro is expanded so just ignore them for now.
  2835. ++MacroDepth;
  2836. }
  2837. }
  2838. // Otherwise, scan til the end of the statement.
  2839. eatToEndOfStatement();
  2840. }
  2841. if (lookupMacro(Name)) {
  2842. return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
  2843. }
  2844. const char *BodyStart = StartToken.getLoc().getPointer();
  2845. const char *BodyEnd = EndToken.getLoc().getPointer();
  2846. StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
  2847. checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
  2848. defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters)));
  2849. return false;
  2850. }
  2851. /// checkForBadMacro
  2852. ///
  2853. /// With the support added for named parameters there may be code out there that
  2854. /// is transitioning from positional parameters. In versions of gas that did
  2855. /// not support named parameters they would be ignored on the macro definition.
  2856. /// But to support both styles of parameters this is not possible so if a macro
  2857. /// definition has named parameters but does not use them and has what appears
  2858. /// to be positional parameters, strings like $1, $2, ... and $n, then issue a
  2859. /// warning that the positional parameter found in body which have no effect.
  2860. /// Hoping the developer will either remove the named parameters from the macro
  2861. /// definition so the positional parameters get used if that was what was
  2862. /// intended or change the macro to use the named parameters. It is possible
  2863. /// this warning will trigger when the none of the named parameters are used
  2864. /// and the strings like $1 are infact to simply to be passed trough unchanged.
  2865. void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
  2866. StringRef Body,
  2867. ArrayRef<MCAsmMacroParameter> Parameters) {
  2868. // If this macro is not defined with named parameters the warning we are
  2869. // checking for here doesn't apply.
  2870. unsigned NParameters = Parameters.size();
  2871. if (NParameters == 0)
  2872. return;
  2873. bool NamedParametersFound = false;
  2874. bool PositionalParametersFound = false;
  2875. // Look at the body of the macro for use of both the named parameters and what
  2876. // are likely to be positional parameters. This is what expandMacro() is
  2877. // doing when it finds the parameters in the body.
  2878. while (!Body.empty()) {
  2879. // Scan for the next possible parameter.
  2880. std::size_t End = Body.size(), Pos = 0;
  2881. for (; Pos != End; ++Pos) {
  2882. // Check for a substitution or escape.
  2883. // This macro is defined with parameters, look for \foo, \bar, etc.
  2884. if (Body[Pos] == '\\' && Pos + 1 != End)
  2885. break;
  2886. // This macro should have parameters, but look for $0, $1, ..., $n too.
  2887. if (Body[Pos] != '$' || Pos + 1 == End)
  2888. continue;
  2889. char Next = Body[Pos + 1];
  2890. if (Next == '$' || Next == 'n' ||
  2891. isdigit(static_cast<unsigned char>(Next)))
  2892. break;
  2893. }
  2894. // Check if we reached the end.
  2895. if (Pos == End)
  2896. break;
  2897. if (Body[Pos] == '$') {
  2898. switch (Body[Pos + 1]) {
  2899. // $$ => $
  2900. case '$':
  2901. break;
  2902. // $n => number of arguments
  2903. case 'n':
  2904. PositionalParametersFound = true;
  2905. break;
  2906. // $[0-9] => argument
  2907. default: {
  2908. PositionalParametersFound = true;
  2909. break;
  2910. }
  2911. }
  2912. Pos += 2;
  2913. } else {
  2914. unsigned I = Pos + 1;
  2915. while (isIdentifierChar(Body[I]) && I + 1 != End)
  2916. ++I;
  2917. const char *Begin = Body.data() + Pos + 1;
  2918. StringRef Argument(Begin, I - (Pos + 1));
  2919. unsigned Index = 0;
  2920. for (; Index < NParameters; ++Index)
  2921. if (Parameters[Index].Name == Argument)
  2922. break;
  2923. if (Index == NParameters) {
  2924. if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
  2925. Pos += 3;
  2926. else {
  2927. Pos = I;
  2928. }
  2929. } else {
  2930. NamedParametersFound = true;
  2931. Pos += 1 + Argument.size();
  2932. }
  2933. }
  2934. // Update the scan point.
  2935. Body = Body.substr(Pos);
  2936. }
  2937. if (!NamedParametersFound && PositionalParametersFound)
  2938. Warning(DirectiveLoc, "macro defined with named parameters which are not "
  2939. "used in macro body, possible positional parameter "
  2940. "found in body which will have no effect");
  2941. }
  2942. /// parseDirectiveExitMacro
  2943. /// ::= .exitm
  2944. bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
  2945. if (getLexer().isNot(AsmToken::EndOfStatement))
  2946. return TokError("unexpected token in '" + Directive + "' directive");
  2947. if (!isInsideMacroInstantiation())
  2948. return TokError("unexpected '" + Directive + "' in file, "
  2949. "no current macro definition");
  2950. // Exit all conditionals that are active in the current macro.
  2951. while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
  2952. TheCondState = TheCondStack.back();
  2953. TheCondStack.pop_back();
  2954. }
  2955. handleMacroExit();
  2956. return false;
  2957. }
  2958. /// parseDirectiveEndMacro
  2959. /// ::= .endm
  2960. /// ::= .endmacro
  2961. bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
  2962. if (getLexer().isNot(AsmToken::EndOfStatement))
  2963. return TokError("unexpected token in '" + Directive + "' directive");
  2964. // If we are inside a macro instantiation, terminate the current
  2965. // instantiation.
  2966. if (isInsideMacroInstantiation()) {
  2967. handleMacroExit();
  2968. return false;
  2969. }
  2970. // Otherwise, this .endmacro is a stray entry in the file; well formed
  2971. // .endmacro directives are handled during the macro definition parsing.
  2972. return TokError("unexpected '" + Directive + "' in file, "
  2973. "no current macro definition");
  2974. }
  2975. /// parseDirectivePurgeMacro
  2976. /// ::= .purgem
  2977. bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
  2978. StringRef Name;
  2979. if (parseIdentifier(Name))
  2980. return TokError("expected identifier in '.purgem' directive");
  2981. if (getLexer().isNot(AsmToken::EndOfStatement))
  2982. return TokError("unexpected token in '.purgem' directive");
  2983. if (!lookupMacro(Name))
  2984. return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
  2985. undefineMacro(Name);
  2986. return false;
  2987. }
  2988. /// parseDirectiveBundleAlignMode
  2989. /// ::= {.bundle_align_mode} expression
  2990. bool AsmParser::parseDirectiveBundleAlignMode() {
  2991. checkForValidSection();
  2992. // Expect a single argument: an expression that evaluates to a constant
  2993. // in the inclusive range 0-30.
  2994. SMLoc ExprLoc = getLexer().getLoc();
  2995. int64_t AlignSizePow2;
  2996. if (parseAbsoluteExpression(AlignSizePow2))
  2997. return true;
  2998. else if (getLexer().isNot(AsmToken::EndOfStatement))
  2999. return TokError("unexpected token after expression in"
  3000. " '.bundle_align_mode' directive");
  3001. else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
  3002. return Error(ExprLoc,
  3003. "invalid bundle alignment size (expected between 0 and 30)");
  3004. Lex();
  3005. // Because of AlignSizePow2's verified range we can safely truncate it to
  3006. // unsigned.
  3007. getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
  3008. return false;
  3009. }
  3010. /// parseDirectiveBundleLock
  3011. /// ::= {.bundle_lock} [align_to_end]
  3012. bool AsmParser::parseDirectiveBundleLock() {
  3013. checkForValidSection();
  3014. bool AlignToEnd = false;
  3015. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  3016. StringRef Option;
  3017. SMLoc Loc = getTok().getLoc();
  3018. const char *kInvalidOptionError =
  3019. "invalid option for '.bundle_lock' directive";
  3020. if (parseIdentifier(Option))
  3021. return Error(Loc, kInvalidOptionError);
  3022. if (Option != "align_to_end")
  3023. return Error(Loc, kInvalidOptionError);
  3024. else if (getLexer().isNot(AsmToken::EndOfStatement))
  3025. return Error(Loc,
  3026. "unexpected token after '.bundle_lock' directive option");
  3027. AlignToEnd = true;
  3028. }
  3029. Lex();
  3030. getStreamer().EmitBundleLock(AlignToEnd);
  3031. return false;
  3032. }
  3033. /// parseDirectiveBundleLock
  3034. /// ::= {.bundle_lock}
  3035. bool AsmParser::parseDirectiveBundleUnlock() {
  3036. checkForValidSection();
  3037. if (getLexer().isNot(AsmToken::EndOfStatement))
  3038. return TokError("unexpected token in '.bundle_unlock' directive");
  3039. Lex();
  3040. getStreamer().EmitBundleUnlock();
  3041. return false;
  3042. }
  3043. /// parseDirectiveSpace
  3044. /// ::= (.skip | .space) expression [ , expression ]
  3045. bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
  3046. checkForValidSection();
  3047. int64_t NumBytes;
  3048. if (parseAbsoluteExpression(NumBytes))
  3049. return true;
  3050. int64_t FillExpr = 0;
  3051. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  3052. if (getLexer().isNot(AsmToken::Comma))
  3053. return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
  3054. Lex();
  3055. if (parseAbsoluteExpression(FillExpr))
  3056. return true;
  3057. if (getLexer().isNot(AsmToken::EndOfStatement))
  3058. return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
  3059. }
  3060. Lex();
  3061. if (NumBytes <= 0)
  3062. return TokError("invalid number of bytes in '" + Twine(IDVal) +
  3063. "' directive");
  3064. // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
  3065. getStreamer().EmitFill(NumBytes, FillExpr);
  3066. return false;
  3067. }
  3068. /// parseDirectiveLEB128
  3069. /// ::= (.sleb128 | .uleb128) [ expression (, expression)* ]
  3070. bool AsmParser::parseDirectiveLEB128(bool Signed) {
  3071. checkForValidSection();
  3072. const MCExpr *Value;
  3073. for (;;) {
  3074. if (parseExpression(Value))
  3075. return true;
  3076. if (Signed)
  3077. getStreamer().EmitSLEB128Value(Value);
  3078. else
  3079. getStreamer().EmitULEB128Value(Value);
  3080. if (getLexer().is(AsmToken::EndOfStatement))
  3081. break;
  3082. if (getLexer().isNot(AsmToken::Comma))
  3083. return TokError("unexpected token in directive");
  3084. Lex();
  3085. }
  3086. return false;
  3087. }
  3088. /// parseDirectiveSymbolAttribute
  3089. /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
  3090. bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
  3091. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  3092. for (;;) {
  3093. StringRef Name;
  3094. SMLoc Loc = getTok().getLoc();
  3095. if (parseIdentifier(Name))
  3096. return Error(Loc, "expected identifier in directive");
  3097. MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
  3098. // Assembler local symbols don't make any sense here. Complain loudly.
  3099. if (Sym->isTemporary())
  3100. return Error(Loc, "non-local symbol required in directive");
  3101. if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
  3102. return Error(Loc, "unable to emit symbol attribute");
  3103. if (getLexer().is(AsmToken::EndOfStatement))
  3104. break;
  3105. if (getLexer().isNot(AsmToken::Comma))
  3106. return TokError("unexpected token in directive");
  3107. Lex();
  3108. }
  3109. }
  3110. Lex();
  3111. return false;
  3112. }
  3113. /// parseDirectiveComm
  3114. /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
  3115. bool AsmParser::parseDirectiveComm(bool IsLocal) {
  3116. checkForValidSection();
  3117. SMLoc IDLoc = getLexer().getLoc();
  3118. StringRef Name;
  3119. if (parseIdentifier(Name))
  3120. return TokError("expected identifier in directive");
  3121. // Handle the identifier as the key symbol.
  3122. MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
  3123. if (getLexer().isNot(AsmToken::Comma))
  3124. return TokError("unexpected token in directive");
  3125. Lex();
  3126. int64_t Size;
  3127. SMLoc SizeLoc = getLexer().getLoc();
  3128. if (parseAbsoluteExpression(Size))
  3129. return true;
  3130. int64_t Pow2Alignment = 0;
  3131. SMLoc Pow2AlignmentLoc;
  3132. if (getLexer().is(AsmToken::Comma)) {
  3133. Lex();
  3134. Pow2AlignmentLoc = getLexer().getLoc();
  3135. if (parseAbsoluteExpression(Pow2Alignment))
  3136. return true;
  3137. LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
  3138. if (IsLocal && LCOMM == LCOMM::NoAlignment)
  3139. return Error(Pow2AlignmentLoc, "alignment not supported on this target");
  3140. // If this target takes alignments in bytes (not log) validate and convert.
  3141. if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
  3142. (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
  3143. if (!isPowerOf2_64(Pow2Alignment))
  3144. return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
  3145. Pow2Alignment = Log2_64(Pow2Alignment);
  3146. }
  3147. }
  3148. if (getLexer().isNot(AsmToken::EndOfStatement))
  3149. return TokError("unexpected token in '.comm' or '.lcomm' directive");
  3150. Lex();
  3151. // NOTE: a size of zero for a .comm should create a undefined symbol
  3152. // but a size of .lcomm creates a bss symbol of size zero.
  3153. if (Size < 0)
  3154. return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
  3155. "be less than zero");
  3156. // NOTE: The alignment in the directive is a power of 2 value, the assembler
  3157. // may internally end up wanting an alignment in bytes.
  3158. // FIXME: Diagnose overflow.
  3159. if (Pow2Alignment < 0)
  3160. return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
  3161. "alignment, can't be less than zero");
  3162. if (!Sym->isUndefined())
  3163. return Error(IDLoc, "invalid symbol redefinition");
  3164. // Create the Symbol as a common or local common with Size and Pow2Alignment
  3165. if (IsLocal) {
  3166. getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
  3167. return false;
  3168. }
  3169. getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
  3170. return false;
  3171. }
  3172. /// parseDirectiveAbort
  3173. /// ::= .abort [... message ...]
  3174. bool AsmParser::parseDirectiveAbort() {
  3175. // FIXME: Use loc from directive.
  3176. SMLoc Loc = getLexer().getLoc();
  3177. StringRef Str = parseStringToEndOfStatement();
  3178. if (getLexer().isNot(AsmToken::EndOfStatement))
  3179. return TokError("unexpected token in '.abort' directive");
  3180. Lex();
  3181. if (Str.empty())
  3182. Error(Loc, ".abort detected. Assembly stopping.");
  3183. else
  3184. Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
  3185. // FIXME: Actually abort assembly here.
  3186. return false;
  3187. }
  3188. /// parseDirectiveInclude
  3189. /// ::= .include "filename"
  3190. bool AsmParser::parseDirectiveInclude() {
  3191. if (getLexer().isNot(AsmToken::String))
  3192. return TokError("expected string in '.include' directive");
  3193. // Allow the strings to have escaped octal character sequence.
  3194. std::string Filename;
  3195. if (parseEscapedString(Filename))
  3196. return true;
  3197. SMLoc IncludeLoc = getLexer().getLoc();
  3198. Lex();
  3199. if (getLexer().isNot(AsmToken::EndOfStatement))
  3200. return TokError("unexpected token in '.include' directive");
  3201. // Attempt to switch the lexer to the included file before consuming the end
  3202. // of statement to avoid losing it when we switch.
  3203. if (enterIncludeFile(Filename)) {
  3204. Error(IncludeLoc, "Could not find include file '" + Filename + "'");
  3205. return true;
  3206. }
  3207. return false;
  3208. }
  3209. /// parseDirectiveIncbin
  3210. /// ::= .incbin "filename"
  3211. bool AsmParser::parseDirectiveIncbin() {
  3212. if (getLexer().isNot(AsmToken::String))
  3213. return TokError("expected string in '.incbin' directive");
  3214. // Allow the strings to have escaped octal character sequence.
  3215. std::string Filename;
  3216. if (parseEscapedString(Filename))
  3217. return true;
  3218. SMLoc IncbinLoc = getLexer().getLoc();
  3219. Lex();
  3220. if (getLexer().isNot(AsmToken::EndOfStatement))
  3221. return TokError("unexpected token in '.incbin' directive");
  3222. // Attempt to process the included file.
  3223. if (processIncbinFile(Filename)) {
  3224. Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
  3225. return true;
  3226. }
  3227. return false;
  3228. }
  3229. /// parseDirectiveIf
  3230. /// ::= .if{,eq,ge,gt,le,lt,ne} expression
  3231. bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
  3232. TheCondStack.push_back(TheCondState);
  3233. TheCondState.TheCond = AsmCond::IfCond;
  3234. if (TheCondState.Ignore) {
  3235. eatToEndOfStatement();
  3236. } else {
  3237. int64_t ExprValue;
  3238. if (parseAbsoluteExpression(ExprValue))
  3239. return true;
  3240. if (getLexer().isNot(AsmToken::EndOfStatement))
  3241. return TokError("unexpected token in '.if' directive");
  3242. Lex();
  3243. switch (DirKind) {
  3244. default:
  3245. llvm_unreachable("unsupported directive");
  3246. case DK_IF:
  3247. case DK_IFNE:
  3248. break;
  3249. case DK_IFEQ:
  3250. ExprValue = ExprValue == 0;
  3251. break;
  3252. case DK_IFGE:
  3253. ExprValue = ExprValue >= 0;
  3254. break;
  3255. case DK_IFGT:
  3256. ExprValue = ExprValue > 0;
  3257. break;
  3258. case DK_IFLE:
  3259. ExprValue = ExprValue <= 0;
  3260. break;
  3261. case DK_IFLT:
  3262. ExprValue = ExprValue < 0;
  3263. break;
  3264. }
  3265. TheCondState.CondMet = ExprValue;
  3266. TheCondState.Ignore = !TheCondState.CondMet;
  3267. }
  3268. return false;
  3269. }
  3270. /// parseDirectiveIfb
  3271. /// ::= .ifb string
  3272. bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
  3273. TheCondStack.push_back(TheCondState);
  3274. TheCondState.TheCond = AsmCond::IfCond;
  3275. if (TheCondState.Ignore) {
  3276. eatToEndOfStatement();
  3277. } else {
  3278. StringRef Str = parseStringToEndOfStatement();
  3279. if (getLexer().isNot(AsmToken::EndOfStatement))
  3280. return TokError("unexpected token in '.ifb' directive");
  3281. Lex();
  3282. TheCondState.CondMet = ExpectBlank == Str.empty();
  3283. TheCondState.Ignore = !TheCondState.CondMet;
  3284. }
  3285. return false;
  3286. }
  3287. /// parseDirectiveIfc
  3288. /// ::= .ifc string1, string2
  3289. /// ::= .ifnc string1, string2
  3290. bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
  3291. TheCondStack.push_back(TheCondState);
  3292. TheCondState.TheCond = AsmCond::IfCond;
  3293. if (TheCondState.Ignore) {
  3294. eatToEndOfStatement();
  3295. } else {
  3296. StringRef Str1 = parseStringToComma();
  3297. if (getLexer().isNot(AsmToken::Comma))
  3298. return TokError("unexpected token in '.ifc' directive");
  3299. Lex();
  3300. StringRef Str2 = parseStringToEndOfStatement();
  3301. if (getLexer().isNot(AsmToken::EndOfStatement))
  3302. return TokError("unexpected token in '.ifc' directive");
  3303. Lex();
  3304. TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
  3305. TheCondState.Ignore = !TheCondState.CondMet;
  3306. }
  3307. return false;
  3308. }
  3309. /// parseDirectiveIfeqs
  3310. /// ::= .ifeqs string1, string2
  3311. bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) {
  3312. if (Lexer.isNot(AsmToken::String)) {
  3313. if (ExpectEqual)
  3314. TokError("expected string parameter for '.ifeqs' directive");
  3315. else
  3316. TokError("expected string parameter for '.ifnes' directive");
  3317. eatToEndOfStatement();
  3318. return true;
  3319. }
  3320. StringRef String1 = getTok().getStringContents();
  3321. Lex();
  3322. if (Lexer.isNot(AsmToken::Comma)) {
  3323. if (ExpectEqual)
  3324. TokError("expected comma after first string for '.ifeqs' directive");
  3325. else
  3326. TokError("expected comma after first string for '.ifnes' directive");
  3327. eatToEndOfStatement();
  3328. return true;
  3329. }
  3330. Lex();
  3331. if (Lexer.isNot(AsmToken::String)) {
  3332. if (ExpectEqual)
  3333. TokError("expected string parameter for '.ifeqs' directive");
  3334. else
  3335. TokError("expected string parameter for '.ifnes' directive");
  3336. eatToEndOfStatement();
  3337. return true;
  3338. }
  3339. StringRef String2 = getTok().getStringContents();
  3340. Lex();
  3341. TheCondStack.push_back(TheCondState);
  3342. TheCondState.TheCond = AsmCond::IfCond;
  3343. TheCondState.CondMet = ExpectEqual == (String1 == String2);
  3344. TheCondState.Ignore = !TheCondState.CondMet;
  3345. return false;
  3346. }
  3347. /// parseDirectiveIfdef
  3348. /// ::= .ifdef symbol
  3349. bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
  3350. StringRef Name;
  3351. TheCondStack.push_back(TheCondState);
  3352. TheCondState.TheCond = AsmCond::IfCond;
  3353. if (TheCondState.Ignore) {
  3354. eatToEndOfStatement();
  3355. } else {
  3356. if (parseIdentifier(Name))
  3357. return TokError("expected identifier after '.ifdef'");
  3358. Lex();
  3359. MCSymbol *Sym = getContext().lookupSymbol(Name);
  3360. if (expect_defined)
  3361. TheCondState.CondMet = (Sym && !Sym->isUndefined());
  3362. else
  3363. TheCondState.CondMet = (!Sym || Sym->isUndefined());
  3364. TheCondState.Ignore = !TheCondState.CondMet;
  3365. }
  3366. return false;
  3367. }
  3368. /// parseDirectiveElseIf
  3369. /// ::= .elseif expression
  3370. bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
  3371. if (TheCondState.TheCond != AsmCond::IfCond &&
  3372. TheCondState.TheCond != AsmCond::ElseIfCond)
  3373. Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
  3374. " an .elseif");
  3375. TheCondState.TheCond = AsmCond::ElseIfCond;
  3376. bool LastIgnoreState = false;
  3377. if (!TheCondStack.empty())
  3378. LastIgnoreState = TheCondStack.back().Ignore;
  3379. if (LastIgnoreState || TheCondState.CondMet) {
  3380. TheCondState.Ignore = true;
  3381. eatToEndOfStatement();
  3382. } else {
  3383. int64_t ExprValue;
  3384. if (parseAbsoluteExpression(ExprValue))
  3385. return true;
  3386. if (getLexer().isNot(AsmToken::EndOfStatement))
  3387. return TokError("unexpected token in '.elseif' directive");
  3388. Lex();
  3389. TheCondState.CondMet = ExprValue;
  3390. TheCondState.Ignore = !TheCondState.CondMet;
  3391. }
  3392. return false;
  3393. }
  3394. /// parseDirectiveElse
  3395. /// ::= .else
  3396. bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
  3397. if (getLexer().isNot(AsmToken::EndOfStatement))
  3398. return TokError("unexpected token in '.else' directive");
  3399. Lex();
  3400. if (TheCondState.TheCond != AsmCond::IfCond &&
  3401. TheCondState.TheCond != AsmCond::ElseIfCond)
  3402. Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
  3403. ".elseif");
  3404. TheCondState.TheCond = AsmCond::ElseCond;
  3405. bool LastIgnoreState = false;
  3406. if (!TheCondStack.empty())
  3407. LastIgnoreState = TheCondStack.back().Ignore;
  3408. if (LastIgnoreState || TheCondState.CondMet)
  3409. TheCondState.Ignore = true;
  3410. else
  3411. TheCondState.Ignore = false;
  3412. return false;
  3413. }
  3414. /// parseDirectiveEnd
  3415. /// ::= .end
  3416. bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
  3417. if (getLexer().isNot(AsmToken::EndOfStatement))
  3418. return TokError("unexpected token in '.end' directive");
  3419. Lex();
  3420. while (Lexer.isNot(AsmToken::Eof))
  3421. Lex();
  3422. return false;
  3423. }
  3424. /// parseDirectiveError
  3425. /// ::= .err
  3426. /// ::= .error [string]
  3427. bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
  3428. if (!TheCondStack.empty()) {
  3429. if (TheCondStack.back().Ignore) {
  3430. eatToEndOfStatement();
  3431. return false;
  3432. }
  3433. }
  3434. if (!WithMessage)
  3435. return Error(L, ".err encountered");
  3436. StringRef Message = ".error directive invoked in source file";
  3437. if (Lexer.isNot(AsmToken::EndOfStatement)) {
  3438. if (Lexer.isNot(AsmToken::String)) {
  3439. TokError(".error argument must be a string");
  3440. eatToEndOfStatement();
  3441. return true;
  3442. }
  3443. Message = getTok().getStringContents();
  3444. Lex();
  3445. }
  3446. Error(L, Message);
  3447. return true;
  3448. }
  3449. /// parseDirectiveWarning
  3450. /// ::= .warning [string]
  3451. bool AsmParser::parseDirectiveWarning(SMLoc L) {
  3452. if (!TheCondStack.empty()) {
  3453. if (TheCondStack.back().Ignore) {
  3454. eatToEndOfStatement();
  3455. return false;
  3456. }
  3457. }
  3458. StringRef Message = ".warning directive invoked in source file";
  3459. if (Lexer.isNot(AsmToken::EndOfStatement)) {
  3460. if (Lexer.isNot(AsmToken::String)) {
  3461. TokError(".warning argument must be a string");
  3462. eatToEndOfStatement();
  3463. return true;
  3464. }
  3465. Message = getTok().getStringContents();
  3466. Lex();
  3467. }
  3468. Warning(L, Message);
  3469. return false;
  3470. }
  3471. /// parseDirectiveEndIf
  3472. /// ::= .endif
  3473. bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
  3474. if (getLexer().isNot(AsmToken::EndOfStatement))
  3475. return TokError("unexpected token in '.endif' directive");
  3476. Lex();
  3477. if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
  3478. Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
  3479. ".else");
  3480. if (!TheCondStack.empty()) {
  3481. TheCondState = TheCondStack.back();
  3482. TheCondStack.pop_back();
  3483. }
  3484. return false;
  3485. }
  3486. void AsmParser::initializeDirectiveKindMap() {
  3487. DirectiveKindMap[".set"] = DK_SET;
  3488. DirectiveKindMap[".equ"] = DK_EQU;
  3489. DirectiveKindMap[".equiv"] = DK_EQUIV;
  3490. DirectiveKindMap[".ascii"] = DK_ASCII;
  3491. DirectiveKindMap[".asciz"] = DK_ASCIZ;
  3492. DirectiveKindMap[".string"] = DK_STRING;
  3493. DirectiveKindMap[".byte"] = DK_BYTE;
  3494. DirectiveKindMap[".short"] = DK_SHORT;
  3495. DirectiveKindMap[".value"] = DK_VALUE;
  3496. DirectiveKindMap[".2byte"] = DK_2BYTE;
  3497. DirectiveKindMap[".long"] = DK_LONG;
  3498. DirectiveKindMap[".int"] = DK_INT;
  3499. DirectiveKindMap[".4byte"] = DK_4BYTE;
  3500. DirectiveKindMap[".quad"] = DK_QUAD;
  3501. DirectiveKindMap[".8byte"] = DK_8BYTE;
  3502. DirectiveKindMap[".octa"] = DK_OCTA;
  3503. DirectiveKindMap[".single"] = DK_SINGLE;
  3504. DirectiveKindMap[".float"] = DK_FLOAT;
  3505. DirectiveKindMap[".double"] = DK_DOUBLE;
  3506. DirectiveKindMap[".align"] = DK_ALIGN;
  3507. DirectiveKindMap[".align32"] = DK_ALIGN32;
  3508. DirectiveKindMap[".balign"] = DK_BALIGN;
  3509. DirectiveKindMap[".balignw"] = DK_BALIGNW;
  3510. DirectiveKindMap[".balignl"] = DK_BALIGNL;
  3511. DirectiveKindMap[".p2align"] = DK_P2ALIGN;
  3512. DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
  3513. DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
  3514. DirectiveKindMap[".org"] = DK_ORG;
  3515. DirectiveKindMap[".fill"] = DK_FILL;
  3516. DirectiveKindMap[".zero"] = DK_ZERO;
  3517. DirectiveKindMap[".extern"] = DK_EXTERN;
  3518. DirectiveKindMap[".globl"] = DK_GLOBL;
  3519. DirectiveKindMap[".global"] = DK_GLOBAL;
  3520. DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
  3521. DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
  3522. DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
  3523. DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
  3524. DirectiveKindMap[".reference"] = DK_REFERENCE;
  3525. DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
  3526. DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
  3527. DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
  3528. DirectiveKindMap[".comm"] = DK_COMM;
  3529. DirectiveKindMap[".common"] = DK_COMMON;
  3530. DirectiveKindMap[".lcomm"] = DK_LCOMM;
  3531. DirectiveKindMap[".abort"] = DK_ABORT;
  3532. DirectiveKindMap[".include"] = DK_INCLUDE;
  3533. DirectiveKindMap[".incbin"] = DK_INCBIN;
  3534. DirectiveKindMap[".code16"] = DK_CODE16;
  3535. DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
  3536. DirectiveKindMap[".rept"] = DK_REPT;
  3537. DirectiveKindMap[".rep"] = DK_REPT;
  3538. DirectiveKindMap[".irp"] = DK_IRP;
  3539. DirectiveKindMap[".irpc"] = DK_IRPC;
  3540. DirectiveKindMap[".endr"] = DK_ENDR;
  3541. DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
  3542. DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
  3543. DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
  3544. DirectiveKindMap[".if"] = DK_IF;
  3545. DirectiveKindMap[".ifeq"] = DK_IFEQ;
  3546. DirectiveKindMap[".ifge"] = DK_IFGE;
  3547. DirectiveKindMap[".ifgt"] = DK_IFGT;
  3548. DirectiveKindMap[".ifle"] = DK_IFLE;
  3549. DirectiveKindMap[".iflt"] = DK_IFLT;
  3550. DirectiveKindMap[".ifne"] = DK_IFNE;
  3551. DirectiveKindMap[".ifb"] = DK_IFB;
  3552. DirectiveKindMap[".ifnb"] = DK_IFNB;
  3553. DirectiveKindMap[".ifc"] = DK_IFC;
  3554. DirectiveKindMap[".ifeqs"] = DK_IFEQS;
  3555. DirectiveKindMap[".ifnc"] = DK_IFNC;
  3556. DirectiveKindMap[".ifnes"] = DK_IFNES;
  3557. DirectiveKindMap[".ifdef"] = DK_IFDEF;
  3558. DirectiveKindMap[".ifndef"] = DK_IFNDEF;
  3559. DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
  3560. DirectiveKindMap[".elseif"] = DK_ELSEIF;
  3561. DirectiveKindMap[".else"] = DK_ELSE;
  3562. DirectiveKindMap[".end"] = DK_END;
  3563. DirectiveKindMap[".endif"] = DK_ENDIF;
  3564. DirectiveKindMap[".skip"] = DK_SKIP;
  3565. DirectiveKindMap[".space"] = DK_SPACE;
  3566. DirectiveKindMap[".file"] = DK_FILE;
  3567. DirectiveKindMap[".line"] = DK_LINE;
  3568. DirectiveKindMap[".loc"] = DK_LOC;
  3569. DirectiveKindMap[".stabs"] = DK_STABS;
  3570. DirectiveKindMap[".sleb128"] = DK_SLEB128;
  3571. DirectiveKindMap[".uleb128"] = DK_ULEB128;
  3572. DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
  3573. DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
  3574. DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
  3575. DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
  3576. DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
  3577. DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
  3578. DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
  3579. DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
  3580. DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
  3581. DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
  3582. DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
  3583. DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
  3584. DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
  3585. DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
  3586. DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
  3587. DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
  3588. DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
  3589. DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
  3590. DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
  3591. DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
  3592. DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
  3593. DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
  3594. DirectiveKindMap[".macro"] = DK_MACRO;
  3595. DirectiveKindMap[".exitm"] = DK_EXITM;
  3596. DirectiveKindMap[".endm"] = DK_ENDM;
  3597. DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
  3598. DirectiveKindMap[".purgem"] = DK_PURGEM;
  3599. DirectiveKindMap[".err"] = DK_ERR;
  3600. DirectiveKindMap[".error"] = DK_ERROR;
  3601. DirectiveKindMap[".warning"] = DK_WARNING;
  3602. }
  3603. MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
  3604. AsmToken EndToken, StartToken = getTok();
  3605. unsigned NestLevel = 0;
  3606. for (;;) {
  3607. // Check whether we have reached the end of the file.
  3608. if (getLexer().is(AsmToken::Eof)) {
  3609. Error(DirectiveLoc, "no matching '.endr' in definition");
  3610. return nullptr;
  3611. }
  3612. if (Lexer.is(AsmToken::Identifier) &&
  3613. (getTok().getIdentifier() == ".rept")) {
  3614. ++NestLevel;
  3615. }
  3616. // Otherwise, check whether we have reached the .endr.
  3617. if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
  3618. if (NestLevel == 0) {
  3619. EndToken = getTok();
  3620. Lex();
  3621. if (Lexer.isNot(AsmToken::EndOfStatement)) {
  3622. TokError("unexpected token in '.endr' directive");
  3623. return nullptr;
  3624. }
  3625. break;
  3626. }
  3627. --NestLevel;
  3628. }
  3629. // Otherwise, scan till the end of the statement.
  3630. eatToEndOfStatement();
  3631. }
  3632. const char *BodyStart = StartToken.getLoc().getPointer();
  3633. const char *BodyEnd = EndToken.getLoc().getPointer();
  3634. StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
  3635. // We Are Anonymous.
  3636. MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters());
  3637. return &MacroLikeBodies.back();
  3638. }
  3639. void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
  3640. raw_svector_ostream &OS) {
  3641. OS << ".endr\n";
  3642. std::unique_ptr<MemoryBuffer> Instantiation =
  3643. MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
  3644. // Create the macro instantiation object and add to the current macro
  3645. // instantiation stack.
  3646. MacroInstantiation *MI = new MacroInstantiation(
  3647. DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
  3648. ActiveMacros.push_back(MI);
  3649. // Jump to the macro instantiation and prime the lexer.
  3650. CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
  3651. Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
  3652. Lex();
  3653. }
  3654. /// parseDirectiveRept
  3655. /// ::= .rep | .rept count
  3656. bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
  3657. const MCExpr *CountExpr;
  3658. SMLoc CountLoc = getTok().getLoc();
  3659. if (parseExpression(CountExpr))
  3660. return true;
  3661. int64_t Count;
  3662. if (!CountExpr->evaluateAsAbsolute(Count)) {
  3663. eatToEndOfStatement();
  3664. return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
  3665. }
  3666. if (Count < 0)
  3667. return Error(CountLoc, "Count is negative");
  3668. if (Lexer.isNot(AsmToken::EndOfStatement))
  3669. return TokError("unexpected token in '" + Dir + "' directive");
  3670. // Eat the end of statement.
  3671. Lex();
  3672. // Lex the rept definition.
  3673. MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
  3674. if (!M)
  3675. return true;
  3676. // Macro instantiation is lexical, unfortunately. We construct a new buffer
  3677. // to hold the macro body with substitutions.
  3678. SmallString<256> Buf;
  3679. raw_svector_ostream OS(Buf);
  3680. while (Count--) {
  3681. // Note that the AtPseudoVariable is disabled for instantiations of .rep(t).
  3682. if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc()))
  3683. return true;
  3684. }
  3685. instantiateMacroLikeBody(M, DirectiveLoc, OS);
  3686. return false;
  3687. }
  3688. /// parseDirectiveIrp
  3689. /// ::= .irp symbol,values
  3690. bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
  3691. MCAsmMacroParameter Parameter;
  3692. if (parseIdentifier(Parameter.Name))
  3693. return TokError("expected identifier in '.irp' directive");
  3694. if (Lexer.isNot(AsmToken::Comma))
  3695. return TokError("expected comma in '.irp' directive");
  3696. Lex();
  3697. MCAsmMacroArguments A;
  3698. if (parseMacroArguments(nullptr, A))
  3699. return true;
  3700. // Eat the end of statement.
  3701. Lex();
  3702. // Lex the irp definition.
  3703. MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
  3704. if (!M)
  3705. return true;
  3706. // Macro instantiation is lexical, unfortunately. We construct a new buffer
  3707. // to hold the macro body with substitutions.
  3708. SmallString<256> Buf;
  3709. raw_svector_ostream OS(Buf);
  3710. for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
  3711. // Note that the AtPseudoVariable is enabled for instantiations of .irp.
  3712. // This is undocumented, but GAS seems to support it.
  3713. if (expandMacro(OS, M->Body, Parameter, *i, true, getTok().getLoc()))
  3714. return true;
  3715. }
  3716. instantiateMacroLikeBody(M, DirectiveLoc, OS);
  3717. return false;
  3718. }
  3719. /// parseDirectiveIrpc
  3720. /// ::= .irpc symbol,values
  3721. bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
  3722. MCAsmMacroParameter Parameter;
  3723. if (parseIdentifier(Parameter.Name))
  3724. return TokError("expected identifier in '.irpc' directive");
  3725. if (Lexer.isNot(AsmToken::Comma))
  3726. return TokError("expected comma in '.irpc' directive");
  3727. Lex();
  3728. MCAsmMacroArguments A;
  3729. if (parseMacroArguments(nullptr, A))
  3730. return true;
  3731. if (A.size() != 1 || A.front().size() != 1)
  3732. return TokError("unexpected token in '.irpc' directive");
  3733. // Eat the end of statement.
  3734. Lex();
  3735. // Lex the irpc definition.
  3736. MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
  3737. if (!M)
  3738. return true;
  3739. // Macro instantiation is lexical, unfortunately. We construct a new buffer
  3740. // to hold the macro body with substitutions.
  3741. SmallString<256> Buf;
  3742. raw_svector_ostream OS(Buf);
  3743. StringRef Values = A.front().front().getString();
  3744. for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
  3745. MCAsmMacroArgument Arg;
  3746. Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1));
  3747. // Note that the AtPseudoVariable is enabled for instantiations of .irpc.
  3748. // This is undocumented, but GAS seems to support it.
  3749. if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
  3750. return true;
  3751. }
  3752. instantiateMacroLikeBody(M, DirectiveLoc, OS);
  3753. return false;
  3754. }
  3755. bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
  3756. if (ActiveMacros.empty())
  3757. return TokError("unmatched '.endr' directive");
  3758. // The only .repl that should get here are the ones created by
  3759. // instantiateMacroLikeBody.
  3760. assert(getLexer().is(AsmToken::EndOfStatement));
  3761. handleMacroExit();
  3762. return false;
  3763. }
  3764. bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
  3765. size_t Len) {
  3766. const MCExpr *Value;
  3767. SMLoc ExprLoc = getLexer().getLoc();
  3768. if (parseExpression(Value))
  3769. return true;
  3770. const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
  3771. if (!MCE)
  3772. return Error(ExprLoc, "unexpected expression in _emit");
  3773. uint64_t IntValue = MCE->getValue();
  3774. if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
  3775. return Error(ExprLoc, "literal value out of range for directive");
  3776. Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
  3777. return false;
  3778. }
  3779. bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
  3780. const MCExpr *Value;
  3781. SMLoc ExprLoc = getLexer().getLoc();
  3782. if (parseExpression(Value))
  3783. return true;
  3784. const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
  3785. if (!MCE)
  3786. return Error(ExprLoc, "unexpected expression in align");
  3787. uint64_t IntValue = MCE->getValue();
  3788. if (!isPowerOf2_64(IntValue))
  3789. return Error(ExprLoc, "literal value not a power of two greater then zero");
  3790. Info.AsmRewrites->push_back(
  3791. AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
  3792. return false;
  3793. }
  3794. // We are comparing pointers, but the pointers are relative to a single string.
  3795. // Thus, this should always be deterministic.
  3796. // HLSL Change: changed calling convention to __cdecl
  3797. static int __cdecl rewritesSort(const AsmRewrite *AsmRewriteA,
  3798. const AsmRewrite *AsmRewriteB) {
  3799. if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
  3800. return -1;
  3801. if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
  3802. return 1;
  3803. // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
  3804. // rewrite to the same location. Make sure the SizeDirective rewrite is
  3805. // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
  3806. // ensures the sort algorithm is stable.
  3807. if (AsmRewritePrecedence[AsmRewriteA->Kind] >
  3808. AsmRewritePrecedence[AsmRewriteB->Kind])
  3809. return -1;
  3810. if (AsmRewritePrecedence[AsmRewriteA->Kind] <
  3811. AsmRewritePrecedence[AsmRewriteB->Kind])
  3812. return 1;
  3813. llvm_unreachable("Unstable rewrite sort.");
  3814. }
  3815. bool AsmParser::parseMSInlineAsm(
  3816. void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
  3817. unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
  3818. SmallVectorImpl<std::string> &Constraints,
  3819. SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
  3820. const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
  3821. SmallVector<void *, 4> InputDecls;
  3822. SmallVector<void *, 4> OutputDecls;
  3823. SmallVector<bool, 4> InputDeclsAddressOf;
  3824. SmallVector<bool, 4> OutputDeclsAddressOf;
  3825. SmallVector<std::string, 4> InputConstraints;
  3826. SmallVector<std::string, 4> OutputConstraints;
  3827. SmallVector<unsigned, 4> ClobberRegs;
  3828. SmallVector<AsmRewrite, 4> AsmStrRewrites;
  3829. // Prime the lexer.
  3830. Lex();
  3831. // While we have input, parse each statement.
  3832. unsigned InputIdx = 0;
  3833. unsigned OutputIdx = 0;
  3834. while (getLexer().isNot(AsmToken::Eof)) {
  3835. ParseStatementInfo Info(&AsmStrRewrites);
  3836. if (parseStatement(Info, &SI))
  3837. return true;
  3838. if (Info.ParseError)
  3839. return true;
  3840. if (Info.Opcode == ~0U)
  3841. continue;
  3842. const MCInstrDesc &Desc = MII->get(Info.Opcode);
  3843. // Build the list of clobbers, outputs and inputs.
  3844. for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
  3845. MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
  3846. // Immediate.
  3847. if (Operand.isImm())
  3848. continue;
  3849. // Register operand.
  3850. if (Operand.isReg() && !Operand.needAddressOf() &&
  3851. !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
  3852. unsigned NumDefs = Desc.getNumDefs();
  3853. // Clobber.
  3854. if (NumDefs && Operand.getMCOperandNum() < NumDefs)
  3855. ClobberRegs.push_back(Operand.getReg());
  3856. continue;
  3857. }
  3858. // Expr/Input or Output.
  3859. StringRef SymName = Operand.getSymName();
  3860. if (SymName.empty())
  3861. continue;
  3862. void *OpDecl = Operand.getOpDecl();
  3863. if (!OpDecl)
  3864. continue;
  3865. bool isOutput = (i == 1) && Desc.mayStore();
  3866. SMLoc Start = SMLoc::getFromPointer(SymName.data());
  3867. if (isOutput) {
  3868. ++InputIdx;
  3869. OutputDecls.push_back(OpDecl);
  3870. OutputDeclsAddressOf.push_back(Operand.needAddressOf());
  3871. OutputConstraints.push_back(("=" + Operand.getConstraint()).str());
  3872. AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
  3873. } else {
  3874. InputDecls.push_back(OpDecl);
  3875. InputDeclsAddressOf.push_back(Operand.needAddressOf());
  3876. InputConstraints.push_back(Operand.getConstraint().str());
  3877. AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
  3878. }
  3879. }
  3880. // Consider implicit defs to be clobbers. Think of cpuid and push.
  3881. ArrayRef<uint16_t> ImpDefs(Desc.getImplicitDefs(),
  3882. Desc.getNumImplicitDefs());
  3883. ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
  3884. }
  3885. // Set the number of Outputs and Inputs.
  3886. NumOutputs = OutputDecls.size();
  3887. NumInputs = InputDecls.size();
  3888. // Set the unique clobbers.
  3889. array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
  3890. ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
  3891. ClobberRegs.end());
  3892. Clobbers.assign(ClobberRegs.size(), std::string());
  3893. for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
  3894. raw_string_ostream OS(Clobbers[I]);
  3895. IP->printRegName(OS, ClobberRegs[I]);
  3896. }
  3897. // Merge the various outputs and inputs. Output are expected first.
  3898. if (NumOutputs || NumInputs) {
  3899. unsigned NumExprs = NumOutputs + NumInputs;
  3900. OpDecls.resize(NumExprs);
  3901. Constraints.resize(NumExprs);
  3902. for (unsigned i = 0; i < NumOutputs; ++i) {
  3903. OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
  3904. Constraints[i] = OutputConstraints[i];
  3905. }
  3906. for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
  3907. OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
  3908. Constraints[j] = InputConstraints[i];
  3909. }
  3910. }
  3911. // Build the IR assembly string.
  3912. std::string AsmStringIR;
  3913. raw_string_ostream OS(AsmStringIR);
  3914. StringRef ASMString =
  3915. SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
  3916. const char *AsmStart = ASMString.begin();
  3917. const char *AsmEnd = ASMString.end();
  3918. array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
  3919. for (const AsmRewrite &AR : AsmStrRewrites) {
  3920. AsmRewriteKind Kind = AR.Kind;
  3921. if (Kind == AOK_Delete)
  3922. continue;
  3923. const char *Loc = AR.Loc.getPointer();
  3924. assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
  3925. // Emit everything up to the immediate/expression.
  3926. if (unsigned Len = Loc - AsmStart)
  3927. OS << StringRef(AsmStart, Len);
  3928. // Skip the original expression.
  3929. if (Kind == AOK_Skip) {
  3930. AsmStart = Loc + AR.Len;
  3931. continue;
  3932. }
  3933. unsigned AdditionalSkip = 0;
  3934. // Rewrite expressions in $N notation.
  3935. switch (Kind) {
  3936. default:
  3937. break;
  3938. case AOK_Imm:
  3939. OS << "$$" << AR.Val;
  3940. break;
  3941. case AOK_ImmPrefix:
  3942. OS << "$$";
  3943. break;
  3944. case AOK_Label:
  3945. OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
  3946. break;
  3947. case AOK_Input:
  3948. OS << '$' << InputIdx++;
  3949. break;
  3950. case AOK_Output:
  3951. OS << '$' << OutputIdx++;
  3952. break;
  3953. case AOK_SizeDirective:
  3954. switch (AR.Val) {
  3955. default: break;
  3956. case 8: OS << "byte ptr "; break;
  3957. case 16: OS << "word ptr "; break;
  3958. case 32: OS << "dword ptr "; break;
  3959. case 64: OS << "qword ptr "; break;
  3960. case 80: OS << "xword ptr "; break;
  3961. case 128: OS << "xmmword ptr "; break;
  3962. case 256: OS << "ymmword ptr "; break;
  3963. }
  3964. break;
  3965. case AOK_Emit:
  3966. OS << ".byte";
  3967. break;
  3968. case AOK_Align: {
  3969. unsigned Val = AR.Val;
  3970. OS << ".align " << Val;
  3971. // Skip the original immediate.
  3972. assert(Val < 10 && "Expected alignment less then 2^10.");
  3973. AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
  3974. break;
  3975. }
  3976. case AOK_DotOperator:
  3977. // Insert the dot if the user omitted it.
  3978. OS.flush();
  3979. if (AsmStringIR.back() != '.')
  3980. OS << '.';
  3981. OS << AR.Val;
  3982. break;
  3983. }
  3984. // Skip the original expression.
  3985. AsmStart = Loc + AR.Len + AdditionalSkip;
  3986. }
  3987. // Emit the remainder of the asm string.
  3988. if (AsmStart != AsmEnd)
  3989. OS << StringRef(AsmStart, AsmEnd - AsmStart);
  3990. AsmString = OS.str();
  3991. return false;
  3992. }
  3993. namespace llvm {
  3994. namespace MCParserUtils {
  3995. /// Returns whether the given symbol is used anywhere in the given expression,
  3996. /// or subexpressions.
  3997. static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) {
  3998. switch (Value->getKind()) {
  3999. case MCExpr::Binary: {
  4000. const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
  4001. return isSymbolUsedInExpression(Sym, BE->getLHS()) ||
  4002. isSymbolUsedInExpression(Sym, BE->getRHS());
  4003. }
  4004. case MCExpr::Target:
  4005. case MCExpr::Constant:
  4006. return false;
  4007. case MCExpr::SymbolRef: {
  4008. const MCSymbol &S =
  4009. static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
  4010. if (S.isVariable())
  4011. return isSymbolUsedInExpression(Sym, S.getVariableValue());
  4012. return &S == Sym;
  4013. }
  4014. case MCExpr::Unary:
  4015. return isSymbolUsedInExpression(
  4016. Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
  4017. }
  4018. llvm_unreachable("Unknown expr kind!");
  4019. }
  4020. bool parseAssignmentExpression(StringRef Name, bool allow_redef,
  4021. MCAsmParser &Parser, MCSymbol *&Sym,
  4022. const MCExpr *&Value) {
  4023. MCAsmLexer &Lexer = Parser.getLexer();
  4024. // FIXME: Use better location, we should use proper tokens.
  4025. SMLoc EqualLoc = Lexer.getLoc();
  4026. if (Parser.parseExpression(Value)) {
  4027. Parser.TokError("missing expression");
  4028. Parser.eatToEndOfStatement();
  4029. return true;
  4030. }
  4031. // Note: we don't count b as used in "a = b". This is to allow
  4032. // a = b
  4033. // b = c
  4034. if (Lexer.isNot(AsmToken::EndOfStatement))
  4035. return Parser.TokError("unexpected token in assignment");
  4036. // Eat the end of statement marker.
  4037. Parser.Lex();
  4038. // Validate that the LHS is allowed to be a variable (either it has not been
  4039. // used as a symbol, or it is an absolute symbol).
  4040. Sym = Parser.getContext().lookupSymbol(Name);
  4041. if (Sym) {
  4042. // Diagnose assignment to a label.
  4043. //
  4044. // FIXME: Diagnostics. Note the location of the definition as a label.
  4045. // FIXME: Diagnose assignment to protected identifier (e.g., register name).
  4046. if (isSymbolUsedInExpression(Sym, Value))
  4047. return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'");
  4048. else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
  4049. ; // Allow redefinitions of undefined symbols only used in directives.
  4050. else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
  4051. ; // Allow redefinitions of variables that haven't yet been used.
  4052. else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
  4053. return Parser.Error(EqualLoc, "redefinition of '" + Name + "'");
  4054. else if (!Sym->isVariable())
  4055. return Parser.Error(EqualLoc, "invalid assignment to '" + Name + "'");
  4056. else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
  4057. return Parser.Error(EqualLoc,
  4058. "invalid reassignment of non-absolute variable '" +
  4059. Name + "'");
  4060. // Don't count these checks as uses.
  4061. Sym->setUsed(false);
  4062. } else if (Name == ".") {
  4063. if (Parser.getStreamer().EmitValueToOffset(Value, 0)) {
  4064. Parser.Error(EqualLoc, "expected absolute expression");
  4065. Parser.eatToEndOfStatement();
  4066. return true;
  4067. }
  4068. return false;
  4069. } else
  4070. Sym = Parser.getContext().getOrCreateSymbol(Name);
  4071. Sym->setRedefinable(allow_redef);
  4072. return false;
  4073. }
  4074. } // namespace MCParserUtils
  4075. } // namespace llvm
  4076. /// \brief Create an MCAsmParser instance.
  4077. MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
  4078. MCStreamer &Out, const MCAsmInfo &MAI) {
  4079. return new AsmParser(SM, C, Out, MAI);
  4080. }