Home | History | Annotate | Download | only in MCParser
      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 
     14 #include "llvm/ADT/APFloat.h"
     15 #include "llvm/ADT/SmallString.h"
     16 #include "llvm/ADT/StringMap.h"
     17 #include "llvm/ADT/StringSwitch.h"
     18 #include "llvm/ADT/Twine.h"
     19 #include "llvm/MC/MCAsmInfo.h"
     20 #include "llvm/MC/MCContext.h"
     21 #include "llvm/MC/MCDwarf.h"
     22 #include "llvm/MC/MCExpr.h"
     23 #include "llvm/MC/MCParser/AsmCond.h"
     24 #include "llvm/MC/MCParser/AsmLexer.h"
     25 #include "llvm/MC/MCParser/MCAsmParser.h"
     26 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
     27 #include "llvm/MC/MCRegisterInfo.h"
     28 #include "llvm/MC/MCSectionMachO.h"
     29 #include "llvm/MC/MCStreamer.h"
     30 #include "llvm/MC/MCSymbol.h"
     31 #include "llvm/MC/MCTargetAsmParser.h"
     32 #include "llvm/Support/CommandLine.h"
     33 #include "llvm/Support/MathExtras.h"
     34 #include "llvm/Support/MemoryBuffer.h"
     35 #include "llvm/Support/SourceMgr.h"
     36 #include "llvm/Support/raw_ostream.h"
     37 #include <cctype>
     38 #include <vector>
     39 using namespace llvm;
     40 
     41 static cl::opt<bool>
     42 FatalAssemblerWarnings("fatal-assembler-warnings",
     43                        cl::desc("Consider warnings as error"));
     44 
     45 namespace {
     46 
     47 /// \brief Helper class for tracking macro definitions.
     48 struct Macro {
     49   StringRef Name;
     50   StringRef Body;
     51   std::vector<StringRef> Parameters;
     52 
     53 public:
     54   Macro(StringRef N, StringRef B, const std::vector<StringRef> &P) :
     55     Name(N), Body(B), Parameters(P) {}
     56 };
     57 
     58 /// \brief Helper class for storing information about an active macro
     59 /// instantiation.
     60 struct MacroInstantiation {
     61   /// The macro being instantiated.
     62   const Macro *TheMacro;
     63 
     64   /// The macro instantiation with substitutions.
     65   MemoryBuffer *Instantiation;
     66 
     67   /// The location of the instantiation.
     68   SMLoc InstantiationLoc;
     69 
     70   /// The location where parsing should resume upon instantiation completion.
     71   SMLoc ExitLoc;
     72 
     73 public:
     74   MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
     75                      MemoryBuffer *I);
     76 };
     77 
     78 /// \brief The concrete assembly parser instance.
     79 class AsmParser : public MCAsmParser {
     80   friend class GenericAsmParser;
     81 
     82   AsmParser(const AsmParser &);   // DO NOT IMPLEMENT
     83   void operator=(const AsmParser &);  // DO NOT IMPLEMENT
     84 private:
     85   AsmLexer Lexer;
     86   MCContext &Ctx;
     87   MCStreamer &Out;
     88   const MCAsmInfo &MAI;
     89   SourceMgr &SrcMgr;
     90   SourceMgr::DiagHandlerTy SavedDiagHandler;
     91   void *SavedDiagContext;
     92   MCAsmParserExtension *GenericParser;
     93   MCAsmParserExtension *PlatformParser;
     94 
     95   /// This is the current buffer index we're lexing from as managed by the
     96   /// SourceMgr object.
     97   int CurBuffer;
     98 
     99   AsmCond TheCondState;
    100   std::vector<AsmCond> TheCondStack;
    101 
    102   /// DirectiveMap - This is a table handlers for directives.  Each handler is
    103   /// invoked after the directive identifier is read and is responsible for
    104   /// parsing and validating the rest of the directive.  The handler is passed
    105   /// in the directive name and the location of the directive keyword.
    106   StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
    107 
    108   /// MacroMap - Map of currently defined macros.
    109   StringMap<Macro*> MacroMap;
    110 
    111   /// ActiveMacros - Stack of active macro instantiations.
    112   std::vector<MacroInstantiation*> ActiveMacros;
    113 
    114   /// Boolean tracking whether macro substitution is enabled.
    115   unsigned MacrosEnabled : 1;
    116 
    117   /// Flag tracking whether any errors have been encountered.
    118   unsigned HadError : 1;
    119 
    120   /// The values from the last parsed cpp hash file line comment if any.
    121   StringRef CppHashFilename;
    122   int64_t CppHashLineNumber;
    123   SMLoc CppHashLoc;
    124 
    125 public:
    126   AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
    127             const MCAsmInfo &MAI);
    128   ~AsmParser();
    129 
    130   virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
    131 
    132   void AddDirectiveHandler(MCAsmParserExtension *Object,
    133                            StringRef Directive,
    134                            DirectiveHandler Handler) {
    135     DirectiveMap[Directive] = std::make_pair(Object, Handler);
    136   }
    137 
    138 public:
    139   /// @name MCAsmParser Interface
    140   /// {
    141 
    142   virtual SourceMgr &getSourceManager() { return SrcMgr; }
    143   virtual MCAsmLexer &getLexer() { return Lexer; }
    144   virtual MCContext &getContext() { return Ctx; }
    145   virtual MCStreamer &getStreamer() { return Out; }
    146 
    147   virtual bool Warning(SMLoc L, const Twine &Msg,
    148                        ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
    149   virtual bool Error(SMLoc L, const Twine &Msg,
    150                      ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
    151 
    152   const AsmToken &Lex();
    153 
    154   bool ParseExpression(const MCExpr *&Res);
    155   virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
    156   virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
    157   virtual bool ParseAbsoluteExpression(int64_t &Res);
    158 
    159   /// }
    160 
    161 private:
    162   void CheckForValidSection();
    163 
    164   bool ParseStatement();
    165   void EatToEndOfLine();
    166   bool ParseCppHashLineFilenameComment(const SMLoc &L);
    167 
    168   bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
    169   bool expandMacro(SmallString<256> &Buf, StringRef Body,
    170                    const std::vector<StringRef> &Parameters,
    171                    const std::vector<std::vector<AsmToken> > &A,
    172                    const SMLoc &L);
    173   void HandleMacroExit();
    174 
    175   void PrintMacroInstantiations();
    176   void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
    177                     ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
    178     SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
    179   }
    180   static void DiagHandler(const SMDiagnostic &Diag, void *Context);
    181 
    182   /// EnterIncludeFile - Enter the specified file. This returns true on failure.
    183   bool EnterIncludeFile(const std::string &Filename);
    184 
    185   /// \brief Reset the current lexer position to that given by \arg Loc. The
    186   /// current token is not set; clients should ensure Lex() is called
    187   /// subsequently.
    188   void JumpToLoc(SMLoc Loc);
    189 
    190   void EatToEndOfStatement();
    191 
    192   /// \brief Parse up to the end of statement and a return the contents from the
    193   /// current token until the end of the statement; the current token on exit
    194   /// will be either the EndOfStatement or EOF.
    195   StringRef ParseStringToEndOfStatement();
    196 
    197   bool ParseAssignment(StringRef Name, bool allow_redef);
    198 
    199   bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
    200   bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
    201   bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
    202   bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
    203 
    204   /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
    205   /// and set \arg Res to the identifier contents.
    206   bool ParseIdentifier(StringRef &Res);
    207 
    208   // Directive Parsing.
    209 
    210  // ".ascii", ".asciiz", ".string"
    211   bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
    212   bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
    213   bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
    214   bool ParseDirectiveFill(); // ".fill"
    215   bool ParseDirectiveSpace(); // ".space"
    216   bool ParseDirectiveZero(); // ".zero"
    217   bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
    218   bool ParseDirectiveOrg(); // ".org"
    219   // ".align{,32}", ".p2align{,w,l}"
    220   bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
    221 
    222   /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
    223   /// accepts a single symbol (which should be a label or an external).
    224   bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
    225 
    226   bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
    227 
    228   bool ParseDirectiveAbort(); // ".abort"
    229   bool ParseDirectiveInclude(); // ".include"
    230 
    231   bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
    232   // ".ifdef" or ".ifndef", depending on expect_defined
    233   bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
    234   bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
    235   bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
    236   bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
    237 
    238   /// ParseEscapedString - Parse the current token as a string which may include
    239   /// escaped characters and return the string contents.
    240   bool ParseEscapedString(std::string &Data);
    241 
    242   const MCExpr *ApplyModifierToExpr(const MCExpr *E,
    243                                     MCSymbolRefExpr::VariantKind Variant);
    244 };
    245 
    246 /// \brief Generic implementations of directive handling, etc. which is shared
    247 /// (or the default, at least) for all assembler parser.
    248 class GenericAsmParser : public MCAsmParserExtension {
    249   template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
    250   void AddDirectiveHandler(StringRef Directive) {
    251     getParser().AddDirectiveHandler(this, Directive,
    252                                     HandleDirective<GenericAsmParser, Handler>);
    253   }
    254 public:
    255   GenericAsmParser() {}
    256 
    257   AsmParser &getParser() {
    258     return (AsmParser&) this->MCAsmParserExtension::getParser();
    259   }
    260 
    261   virtual void Initialize(MCAsmParser &Parser) {
    262     // Call the base implementation.
    263     this->MCAsmParserExtension::Initialize(Parser);
    264 
    265     // Debugging directives.
    266     AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
    267     AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
    268     AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
    269     AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
    270 
    271     // CFI directives.
    272     AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
    273                                                                ".cfi_sections");
    274     AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
    275                                                               ".cfi_startproc");
    276     AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
    277                                                                 ".cfi_endproc");
    278     AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
    279                                                          ".cfi_def_cfa");
    280     AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
    281                                                          ".cfi_def_cfa_offset");
    282     AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
    283                                                       ".cfi_adjust_cfa_offset");
    284     AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
    285                                                        ".cfi_def_cfa_register");
    286     AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
    287                                                                  ".cfi_offset");
    288     AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
    289                                                              ".cfi_rel_offset");
    290     AddDirectiveHandler<
    291      &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
    292     AddDirectiveHandler<
    293             &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
    294     AddDirectiveHandler<
    295       &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
    296     AddDirectiveHandler<
    297       &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
    298     AddDirectiveHandler<
    299       &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
    300 
    301     // Macro directives.
    302     AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
    303       ".macros_on");
    304     AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
    305       ".macros_off");
    306     AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
    307     AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
    308     AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
    309 
    310     AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
    311     AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
    312   }
    313 
    314   bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
    315 
    316   bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
    317   bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
    318   bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
    319   bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
    320   bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
    321   bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
    322   bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
    323   bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
    324   bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
    325   bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
    326   bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
    327   bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
    328   bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
    329   bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
    330   bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
    331   bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
    332   bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
    333 
    334   bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
    335   bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
    336   bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
    337 
    338   bool ParseDirectiveLEB128(StringRef, SMLoc);
    339 };
    340 
    341 }
    342 
    343 namespace llvm {
    344 
    345 extern MCAsmParserExtension *createDarwinAsmParser();
    346 extern MCAsmParserExtension *createELFAsmParser();
    347 extern MCAsmParserExtension *createCOFFAsmParser();
    348 
    349 }
    350 
    351 enum { DEFAULT_ADDRSPACE = 0 };
    352 
    353 AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
    354                      MCStreamer &_Out, const MCAsmInfo &_MAI)
    355   : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
    356     GenericParser(new GenericAsmParser), PlatformParser(0),
    357     CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0) {
    358   // Save the old handler.
    359   SavedDiagHandler = SrcMgr.getDiagHandler();
    360   SavedDiagContext = SrcMgr.getDiagContext();
    361   // Set our own handler which calls the saved handler.
    362   SrcMgr.setDiagHandler(DiagHandler, this);
    363   Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
    364 
    365   // Initialize the generic parser.
    366   GenericParser->Initialize(*this);
    367 
    368   // Initialize the platform / file format parser.
    369   //
    370   // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
    371   // created.
    372   if (_MAI.hasMicrosoftFastStdCallMangling()) {
    373     PlatformParser = createCOFFAsmParser();
    374     PlatformParser->Initialize(*this);
    375   } else if (_MAI.hasSubsectionsViaSymbols()) {
    376     PlatformParser = createDarwinAsmParser();
    377     PlatformParser->Initialize(*this);
    378   } else {
    379     PlatformParser = createELFAsmParser();
    380     PlatformParser->Initialize(*this);
    381   }
    382 }
    383 
    384 AsmParser::~AsmParser() {
    385   assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
    386 
    387   // Destroy any macros.
    388   for (StringMap<Macro*>::iterator it = MacroMap.begin(),
    389          ie = MacroMap.end(); it != ie; ++it)
    390     delete it->getValue();
    391 
    392   delete PlatformParser;
    393   delete GenericParser;
    394 }
    395 
    396 void AsmParser::PrintMacroInstantiations() {
    397   // Print the active macro instantiation stack.
    398   for (std::vector<MacroInstantiation*>::const_reverse_iterator
    399          it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
    400     PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
    401                  "while in macro instantiation");
    402 }
    403 
    404 bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
    405   if (FatalAssemblerWarnings)
    406     return Error(L, Msg, Ranges);
    407   PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
    408   PrintMacroInstantiations();
    409   return false;
    410 }
    411 
    412 bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
    413   HadError = true;
    414   PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
    415   PrintMacroInstantiations();
    416   return true;
    417 }
    418 
    419 bool AsmParser::EnterIncludeFile(const std::string &Filename) {
    420   std::string IncludedFile;
    421   int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
    422   if (NewBuf == -1)
    423     return true;
    424 
    425   CurBuffer = NewBuf;
    426 
    427   Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
    428 
    429   return false;
    430 }
    431 
    432 void AsmParser::JumpToLoc(SMLoc Loc) {
    433   CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
    434   Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
    435 }
    436 
    437 const AsmToken &AsmParser::Lex() {
    438   const AsmToken *tok = &Lexer.Lex();
    439 
    440   if (tok->is(AsmToken::Eof)) {
    441     // If this is the end of an included file, pop the parent file off the
    442     // include stack.
    443     SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
    444     if (ParentIncludeLoc != SMLoc()) {
    445       JumpToLoc(ParentIncludeLoc);
    446       tok = &Lexer.Lex();
    447     }
    448   }
    449 
    450   if (tok->is(AsmToken::Error))
    451     Error(Lexer.getErrLoc(), Lexer.getErr());
    452 
    453   return *tok;
    454 }
    455 
    456 bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
    457   // Create the initial section, if requested.
    458   if (!NoInitialTextSection)
    459     Out.InitSections();
    460 
    461   // Prime the lexer.
    462   Lex();
    463 
    464   HadError = false;
    465   AsmCond StartingCondState = TheCondState;
    466 
    467   // While we have input, parse each statement.
    468   while (Lexer.isNot(AsmToken::Eof)) {
    469     if (!ParseStatement()) continue;
    470 
    471     // We had an error, validate that one was emitted and recover by skipping to
    472     // the next line.
    473     assert(HadError && "Parse statement returned an error, but none emitted!");
    474     EatToEndOfStatement();
    475   }
    476 
    477   if (TheCondState.TheCond != StartingCondState.TheCond ||
    478       TheCondState.Ignore != StartingCondState.Ignore)
    479     return TokError("unmatched .ifs or .elses");
    480 
    481   // Check to see there are no empty DwarfFile slots.
    482   const std::vector<MCDwarfFile *> &MCDwarfFiles =
    483     getContext().getMCDwarfFiles();
    484   for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
    485     if (!MCDwarfFiles[i])
    486       TokError("unassigned file number: " + Twine(i) + " for .file directives");
    487   }
    488 
    489   // Check to see that all assembler local symbols were actually defined.
    490   // Targets that don't do subsections via symbols may not want this, though,
    491   // so conservatively exclude them. Only do this if we're finalizing, though,
    492   // as otherwise we won't necessarilly have seen everything yet.
    493   if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
    494     const MCContext::SymbolTable &Symbols = getContext().getSymbols();
    495     for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
    496          e = Symbols.end();
    497          i != e; ++i) {
    498       MCSymbol *Sym = i->getValue();
    499       // Variable symbols may not be marked as defined, so check those
    500       // explicitly. If we know it's a variable, we have a definition for
    501       // the purposes of this check.
    502       if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
    503         // FIXME: We would really like to refer back to where the symbol was
    504         // first referenced for a source location. We need to add something
    505         // to track that. Currently, we just point to the end of the file.
    506         PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
    507                      "assembler local symbol '" + Sym->getName() +
    508                      "' not defined");
    509     }
    510   }
    511 
    512 
    513   // Finalize the output stream if there are no errors and if the client wants
    514   // us to.
    515   if (!HadError && !NoFinalize)
    516     Out.Finish();
    517 
    518   return HadError;
    519 }
    520 
    521 void AsmParser::CheckForValidSection() {
    522   if (!getStreamer().getCurrentSection()) {
    523     TokError("expected section directive before assembly directive");
    524     Out.SwitchSection(Ctx.getMachOSection(
    525                         "__TEXT", "__text",
    526                         MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
    527                         0, SectionKind::getText()));
    528   }
    529 }
    530 
    531 /// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
    532 void AsmParser::EatToEndOfStatement() {
    533   while (Lexer.isNot(AsmToken::EndOfStatement) &&
    534          Lexer.isNot(AsmToken::Eof))
    535     Lex();
    536 
    537   // Eat EOL.
    538   if (Lexer.is(AsmToken::EndOfStatement))
    539     Lex();
    540 }
    541 
    542 StringRef AsmParser::ParseStringToEndOfStatement() {
    543   const char *Start = getTok().getLoc().getPointer();
    544 
    545   while (Lexer.isNot(AsmToken::EndOfStatement) &&
    546          Lexer.isNot(AsmToken::Eof))
    547     Lex();
    548 
    549   const char *End = getTok().getLoc().getPointer();
    550   return StringRef(Start, End - Start);
    551 }
    552 
    553 /// ParseParenExpr - Parse a paren expression and return it.
    554 /// NOTE: This assumes the leading '(' has already been consumed.
    555 ///
    556 /// parenexpr ::= expr)
    557 ///
    558 bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
    559   if (ParseExpression(Res)) return true;
    560   if (Lexer.isNot(AsmToken::RParen))
    561     return TokError("expected ')' in parentheses expression");
    562   EndLoc = Lexer.getLoc();
    563   Lex();
    564   return false;
    565 }
    566 
    567 /// ParseBracketExpr - Parse a bracket expression and return it.
    568 /// NOTE: This assumes the leading '[' has already been consumed.
    569 ///
    570 /// bracketexpr ::= expr]
    571 ///
    572 bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
    573   if (ParseExpression(Res)) return true;
    574   if (Lexer.isNot(AsmToken::RBrac))
    575     return TokError("expected ']' in brackets expression");
    576   EndLoc = Lexer.getLoc();
    577   Lex();
    578   return false;
    579 }
    580 
    581 /// ParsePrimaryExpr - Parse a primary expression and return it.
    582 ///  primaryexpr ::= (parenexpr
    583 ///  primaryexpr ::= symbol
    584 ///  primaryexpr ::= number
    585 ///  primaryexpr ::= '.'
    586 ///  primaryexpr ::= ~,+,- primaryexpr
    587 bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
    588   switch (Lexer.getKind()) {
    589   default:
    590     return TokError("unknown token in expression");
    591   // If we have an error assume that we've already handled it.
    592   case AsmToken::Error:
    593     return true;
    594   case AsmToken::Exclaim:
    595     Lex(); // Eat the operator.
    596     if (ParsePrimaryExpr(Res, EndLoc))
    597       return true;
    598     Res = MCUnaryExpr::CreateLNot(Res, getContext());
    599     return false;
    600   case AsmToken::Dollar:
    601   case AsmToken::String:
    602   case AsmToken::Identifier: {
    603     EndLoc = Lexer.getLoc();
    604 
    605     StringRef Identifier;
    606     if (ParseIdentifier(Identifier))
    607       return true;
    608 
    609     // This is a symbol reference.
    610     std::pair<StringRef, StringRef> Split = Identifier.split('@');
    611     MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
    612 
    613     // Lookup the symbol variant if used.
    614     MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
    615     if (Split.first.size() != Identifier.size()) {
    616       Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
    617       if (Variant == MCSymbolRefExpr::VK_Invalid) {
    618         Variant = MCSymbolRefExpr::VK_None;
    619         return TokError("invalid variant '" + Split.second + "'");
    620       }
    621     }
    622 
    623     // If this is an absolute variable reference, substitute it now to preserve
    624     // semantics in the face of reassignment.
    625     if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
    626       if (Variant)
    627         return Error(EndLoc, "unexpected modifier on variable reference");
    628 
    629       Res = Sym->getVariableValue();
    630       return false;
    631     }
    632 
    633     // Otherwise create a symbol ref.
    634     Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
    635     return false;
    636   }
    637   case AsmToken::Integer: {
    638     SMLoc Loc = getTok().getLoc();
    639     int64_t IntVal = getTok().getIntVal();
    640     Res = MCConstantExpr::Create(IntVal, getContext());
    641     EndLoc = Lexer.getLoc();
    642     Lex(); // Eat token.
    643     // Look for 'b' or 'f' following an Integer as a directional label
    644     if (Lexer.getKind() == AsmToken::Identifier) {
    645       StringRef IDVal = getTok().getString();
    646       if (IDVal == "f" || IDVal == "b"){
    647         MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
    648                                                       IDVal == "f" ? 1 : 0);
    649         Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
    650                                       getContext());
    651         if(IDVal == "b" && Sym->isUndefined())
    652           return Error(Loc, "invalid reference to undefined symbol");
    653         EndLoc = Lexer.getLoc();
    654         Lex(); // Eat identifier.
    655       }
    656     }
    657     return false;
    658   }
    659   case AsmToken::Real: {
    660     APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
    661     uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
    662     Res = MCConstantExpr::Create(IntVal, getContext());
    663     Lex(); // Eat token.
    664     return false;
    665   }
    666   case AsmToken::Dot: {
    667     // This is a '.' reference, which references the current PC.  Emit a
    668     // temporary label to the streamer and refer to it.
    669     MCSymbol *Sym = Ctx.CreateTempSymbol();
    670     Out.EmitLabel(Sym);
    671     Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
    672     EndLoc = Lexer.getLoc();
    673     Lex(); // Eat identifier.
    674     return false;
    675   }
    676   case AsmToken::LParen:
    677     Lex(); // Eat the '('.
    678     return ParseParenExpr(Res, EndLoc);
    679   case AsmToken::LBrac:
    680     if (!PlatformParser->HasBracketExpressions())
    681       return TokError("brackets expression not supported on this target");
    682     Lex(); // Eat the '['.
    683     return ParseBracketExpr(Res, EndLoc);
    684   case AsmToken::Minus:
    685     Lex(); // Eat the operator.
    686     if (ParsePrimaryExpr(Res, EndLoc))
    687       return true;
    688     Res = MCUnaryExpr::CreateMinus(Res, getContext());
    689     return false;
    690   case AsmToken::Plus:
    691     Lex(); // Eat the operator.
    692     if (ParsePrimaryExpr(Res, EndLoc))
    693       return true;
    694     Res = MCUnaryExpr::CreatePlus(Res, getContext());
    695     return false;
    696   case AsmToken::Tilde:
    697     Lex(); // Eat the operator.
    698     if (ParsePrimaryExpr(Res, EndLoc))
    699       return true;
    700     Res = MCUnaryExpr::CreateNot(Res, getContext());
    701     return false;
    702   }
    703 }
    704 
    705 bool AsmParser::ParseExpression(const MCExpr *&Res) {
    706   SMLoc EndLoc;
    707   return ParseExpression(Res, EndLoc);
    708 }
    709 
    710 const MCExpr *
    711 AsmParser::ApplyModifierToExpr(const MCExpr *E,
    712                                MCSymbolRefExpr::VariantKind Variant) {
    713   // Recurse over the given expression, rebuilding it to apply the given variant
    714   // if there is exactly one symbol.
    715   switch (E->getKind()) {
    716   case MCExpr::Target:
    717   case MCExpr::Constant:
    718     return 0;
    719 
    720   case MCExpr::SymbolRef: {
    721     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
    722 
    723     if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
    724       TokError("invalid variant on expression '" +
    725                getTok().getIdentifier() + "' (already modified)");
    726       return E;
    727     }
    728 
    729     return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
    730   }
    731 
    732   case MCExpr::Unary: {
    733     const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
    734     const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
    735     if (!Sub)
    736       return 0;
    737     return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
    738   }
    739 
    740   case MCExpr::Binary: {
    741     const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
    742     const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
    743     const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
    744 
    745     if (!LHS && !RHS)
    746       return 0;
    747 
    748     if (!LHS) LHS = BE->getLHS();
    749     if (!RHS) RHS = BE->getRHS();
    750 
    751     return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
    752   }
    753   }
    754 
    755   assert(0 && "Invalid expression kind!");
    756   return 0;
    757 }
    758 
    759 /// ParseExpression - Parse an expression and return it.
    760 ///
    761 ///  expr ::= expr &&,|| expr               -> lowest.
    762 ///  expr ::= expr |,^,&,! expr
    763 ///  expr ::= expr ==,!=,<>,<,<=,>,>= expr
    764 ///  expr ::= expr <<,>> expr
    765 ///  expr ::= expr +,- expr
    766 ///  expr ::= expr *,/,% expr               -> highest.
    767 ///  expr ::= primaryexpr
    768 ///
    769 bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
    770   // Parse the expression.
    771   Res = 0;
    772   if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
    773     return true;
    774 
    775   // As a special case, we support 'a op b @ modifier' by rewriting the
    776   // expression to include the modifier. This is inefficient, but in general we
    777   // expect users to use 'a@modifier op b'.
    778   if (Lexer.getKind() == AsmToken::At) {
    779     Lex();
    780 
    781     if (Lexer.isNot(AsmToken::Identifier))
    782       return TokError("unexpected symbol modifier following '@'");
    783 
    784     MCSymbolRefExpr::VariantKind Variant =
    785       MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
    786     if (Variant == MCSymbolRefExpr::VK_Invalid)
    787       return TokError("invalid variant '" + getTok().getIdentifier() + "'");
    788 
    789     const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
    790     if (!ModifiedRes) {
    791       return TokError("invalid modifier '" + getTok().getIdentifier() +
    792                       "' (no symbols present)");
    793       return true;
    794     }
    795 
    796     Res = ModifiedRes;
    797     Lex();
    798   }
    799 
    800   // Try to constant fold it up front, if possible.
    801   int64_t Value;
    802   if (Res->EvaluateAsAbsolute(Value))
    803     Res = MCConstantExpr::Create(Value, getContext());
    804 
    805   return false;
    806 }
    807 
    808 bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
    809   Res = 0;
    810   return ParseParenExpr(Res, EndLoc) ||
    811          ParseBinOpRHS(1, Res, EndLoc);
    812 }
    813 
    814 bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
    815   const MCExpr *Expr;
    816 
    817   SMLoc StartLoc = Lexer.getLoc();
    818   if (ParseExpression(Expr))
    819     return true;
    820 
    821   if (!Expr->EvaluateAsAbsolute(Res))
    822     return Error(StartLoc, "expected absolute expression");
    823 
    824   return false;
    825 }
    826 
    827 static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
    828                                    MCBinaryExpr::Opcode &Kind) {
    829   switch (K) {
    830   default:
    831     return 0;    // not a binop.
    832 
    833     // Lowest Precedence: &&, ||
    834   case AsmToken::AmpAmp:
    835     Kind = MCBinaryExpr::LAnd;
    836     return 1;
    837   case AsmToken::PipePipe:
    838     Kind = MCBinaryExpr::LOr;
    839     return 1;
    840 
    841 
    842     // Low Precedence: |, &, ^
    843     //
    844     // FIXME: gas seems to support '!' as an infix operator?
    845   case AsmToken::Pipe:
    846     Kind = MCBinaryExpr::Or;
    847     return 2;
    848   case AsmToken::Caret:
    849     Kind = MCBinaryExpr::Xor;
    850     return 2;
    851   case AsmToken::Amp:
    852     Kind = MCBinaryExpr::And;
    853     return 2;
    854 
    855     // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
    856   case AsmToken::EqualEqual:
    857     Kind = MCBinaryExpr::EQ;
    858     return 3;
    859   case AsmToken::ExclaimEqual:
    860   case AsmToken::LessGreater:
    861     Kind = MCBinaryExpr::NE;
    862     return 3;
    863   case AsmToken::Less:
    864     Kind = MCBinaryExpr::LT;
    865     return 3;
    866   case AsmToken::LessEqual:
    867     Kind = MCBinaryExpr::LTE;
    868     return 3;
    869   case AsmToken::Greater:
    870     Kind = MCBinaryExpr::GT;
    871     return 3;
    872   case AsmToken::GreaterEqual:
    873     Kind = MCBinaryExpr::GTE;
    874     return 3;
    875 
    876     // Intermediate Precedence: <<, >>
    877   case AsmToken::LessLess:
    878     Kind = MCBinaryExpr::Shl;
    879     return 4;
    880   case AsmToken::GreaterGreater:
    881     Kind = MCBinaryExpr::Shr;
    882     return 4;
    883 
    884     // High Intermediate Precedence: +, -
    885   case AsmToken::Plus:
    886     Kind = MCBinaryExpr::Add;
    887     return 5;
    888   case AsmToken::Minus:
    889     Kind = MCBinaryExpr::Sub;
    890     return 5;
    891 
    892     // Highest Precedence: *, /, %
    893   case AsmToken::Star:
    894     Kind = MCBinaryExpr::Mul;
    895     return 6;
    896   case AsmToken::Slash:
    897     Kind = MCBinaryExpr::Div;
    898     return 6;
    899   case AsmToken::Percent:
    900     Kind = MCBinaryExpr::Mod;
    901     return 6;
    902   }
    903 }
    904 
    905 
    906 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
    907 /// Res contains the LHS of the expression on input.
    908 bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
    909                               SMLoc &EndLoc) {
    910   while (1) {
    911     MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
    912     unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
    913 
    914     // If the next token is lower precedence than we are allowed to eat, return
    915     // successfully with what we ate already.
    916     if (TokPrec < Precedence)
    917       return false;
    918 
    919     Lex();
    920 
    921     // Eat the next primary expression.
    922     const MCExpr *RHS;
    923     if (ParsePrimaryExpr(RHS, EndLoc)) return true;
    924 
    925     // If BinOp binds less tightly with RHS than the operator after RHS, let
    926     // the pending operator take RHS as its LHS.
    927     MCBinaryExpr::Opcode Dummy;
    928     unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
    929     if (TokPrec < NextTokPrec) {
    930       if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
    931     }
    932 
    933     // Merge LHS and RHS according to operator.
    934     Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
    935   }
    936 }
    937 
    938 
    939 
    940 
    941 /// ParseStatement:
    942 ///   ::= EndOfStatement
    943 ///   ::= Label* Directive ...Operands... EndOfStatement
    944 ///   ::= Label* Identifier OperandList* EndOfStatement
    945 bool AsmParser::ParseStatement() {
    946   if (Lexer.is(AsmToken::EndOfStatement)) {
    947     Out.AddBlankLine();
    948     Lex();
    949     return false;
    950   }
    951 
    952   // Statements always start with an identifier or are a full line comment.
    953   AsmToken ID = getTok();
    954   SMLoc IDLoc = ID.getLoc();
    955   StringRef IDVal;
    956   int64_t LocalLabelVal = -1;
    957   // A full line comment is a '#' as the first token.
    958   if (Lexer.is(AsmToken::Hash))
    959     return ParseCppHashLineFilenameComment(IDLoc);
    960 
    961   // Allow an integer followed by a ':' as a directional local label.
    962   if (Lexer.is(AsmToken::Integer)) {
    963     LocalLabelVal = getTok().getIntVal();
    964     if (LocalLabelVal < 0) {
    965       if (!TheCondState.Ignore)
    966         return TokError("unexpected token at start of statement");
    967       IDVal = "";
    968     }
    969     else {
    970       IDVal = getTok().getString();
    971       Lex(); // Consume the integer token to be used as an identifier token.
    972       if (Lexer.getKind() != AsmToken::Colon) {
    973         if (!TheCondState.Ignore)
    974           return TokError("unexpected token at start of statement");
    975       }
    976     }
    977 
    978   } else if (Lexer.is(AsmToken::Dot)) {
    979     // Treat '.' as a valid identifier in this context.
    980     Lex();
    981     IDVal = ".";
    982 
    983   } else if (ParseIdentifier(IDVal)) {
    984     if (!TheCondState.Ignore)
    985       return TokError("unexpected token at start of statement");
    986     IDVal = "";
    987   }
    988 
    989 
    990   // Handle conditional assembly here before checking for skipping.  We
    991   // have to do this so that .endif isn't skipped in a ".if 0" block for
    992   // example.
    993   if (IDVal == ".if")
    994     return ParseDirectiveIf(IDLoc);
    995   if (IDVal == ".ifdef")
    996     return ParseDirectiveIfdef(IDLoc, true);
    997   if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
    998     return ParseDirectiveIfdef(IDLoc, false);
    999   if (IDVal == ".elseif")
   1000     return ParseDirectiveElseIf(IDLoc);
   1001   if (IDVal == ".else")
   1002     return ParseDirectiveElse(IDLoc);
   1003   if (IDVal == ".endif")
   1004     return ParseDirectiveEndIf(IDLoc);
   1005 
   1006   // If we are in a ".if 0" block, ignore this statement.
   1007   if (TheCondState.Ignore) {
   1008     EatToEndOfStatement();
   1009     return false;
   1010   }
   1011 
   1012   // FIXME: Recurse on local labels?
   1013 
   1014   // See what kind of statement we have.
   1015   switch (Lexer.getKind()) {
   1016   case AsmToken::Colon: {
   1017     CheckForValidSection();
   1018 
   1019     // identifier ':'   -> Label.
   1020     Lex();
   1021 
   1022     // Diagnose attempt to use '.' as a label.
   1023     if (IDVal == ".")
   1024       return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
   1025 
   1026     // Diagnose attempt to use a variable as a label.
   1027     //
   1028     // FIXME: Diagnostics. Note the location of the definition as a label.
   1029     // FIXME: This doesn't diagnose assignment to a symbol which has been
   1030     // implicitly marked as external.
   1031     MCSymbol *Sym;
   1032     if (LocalLabelVal == -1)
   1033       Sym = getContext().GetOrCreateSymbol(IDVal);
   1034     else
   1035       Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
   1036     if (!Sym->isUndefined() || Sym->isVariable())
   1037       return Error(IDLoc, "invalid symbol redefinition");
   1038 
   1039     // Emit the label.
   1040     Out.EmitLabel(Sym);
   1041 
   1042     // Consume any end of statement token, if present, to avoid spurious
   1043     // AddBlankLine calls().
   1044     if (Lexer.is(AsmToken::EndOfStatement)) {
   1045       Lex();
   1046       if (Lexer.is(AsmToken::Eof))
   1047         return false;
   1048     }
   1049 
   1050     return ParseStatement();
   1051   }
   1052 
   1053   case AsmToken::Equal:
   1054     // identifier '=' ... -> assignment statement
   1055     Lex();
   1056 
   1057     return ParseAssignment(IDVal, true);
   1058 
   1059   default: // Normal instruction or directive.
   1060     break;
   1061   }
   1062 
   1063   // If macros are enabled, check to see if this is a macro instantiation.
   1064   if (MacrosEnabled)
   1065     if (const Macro *M = MacroMap.lookup(IDVal))
   1066       return HandleMacroEntry(IDVal, IDLoc, M);
   1067 
   1068   // Otherwise, we have a normal instruction or directive.
   1069   if (IDVal[0] == '.' && IDVal != ".") {
   1070     // Assembler features
   1071     if (IDVal == ".set" || IDVal == ".equ")
   1072       return ParseDirectiveSet(IDVal, true);
   1073     if (IDVal == ".equiv")
   1074       return ParseDirectiveSet(IDVal, false);
   1075 
   1076     // Data directives
   1077 
   1078     if (IDVal == ".ascii")
   1079       return ParseDirectiveAscii(IDVal, false);
   1080     if (IDVal == ".asciz" || IDVal == ".string")
   1081       return ParseDirectiveAscii(IDVal, true);
   1082 
   1083     if (IDVal == ".byte")
   1084       return ParseDirectiveValue(1);
   1085     if (IDVal == ".short")
   1086       return ParseDirectiveValue(2);
   1087     if (IDVal == ".value")
   1088       return ParseDirectiveValue(2);
   1089     if (IDVal == ".2byte")
   1090       return ParseDirectiveValue(2);
   1091     if (IDVal == ".long")
   1092       return ParseDirectiveValue(4);
   1093     if (IDVal == ".int")
   1094       return ParseDirectiveValue(4);
   1095     if (IDVal == ".4byte")
   1096       return ParseDirectiveValue(4);
   1097     if (IDVal == ".quad")
   1098       return ParseDirectiveValue(8);
   1099     if (IDVal == ".8byte")
   1100       return ParseDirectiveValue(8);
   1101     if (IDVal == ".single" || IDVal == ".float")
   1102       return ParseDirectiveRealValue(APFloat::IEEEsingle);
   1103     if (IDVal == ".double")
   1104       return ParseDirectiveRealValue(APFloat::IEEEdouble);
   1105 
   1106     if (IDVal == ".align") {
   1107       bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
   1108       return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
   1109     }
   1110     if (IDVal == ".align32") {
   1111       bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
   1112       return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
   1113     }
   1114     if (IDVal == ".balign")
   1115       return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
   1116     if (IDVal == ".balignw")
   1117       return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
   1118     if (IDVal == ".balignl")
   1119       return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
   1120     if (IDVal == ".p2align")
   1121       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
   1122     if (IDVal == ".p2alignw")
   1123       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
   1124     if (IDVal == ".p2alignl")
   1125       return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
   1126 
   1127     if (IDVal == ".org")
   1128       return ParseDirectiveOrg();
   1129 
   1130     if (IDVal == ".fill")
   1131       return ParseDirectiveFill();
   1132     if (IDVal == ".space" || IDVal == ".skip")
   1133       return ParseDirectiveSpace();
   1134     if (IDVal == ".zero")
   1135       return ParseDirectiveZero();
   1136 
   1137     // Symbol attribute directives
   1138 
   1139     if (IDVal == ".globl" || IDVal == ".global")
   1140       return ParseDirectiveSymbolAttribute(MCSA_Global);
   1141     if (IDVal == ".indirect_symbol")
   1142       return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
   1143     if (IDVal == ".lazy_reference")
   1144       return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
   1145     if (IDVal == ".no_dead_strip")
   1146       return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
   1147     if (IDVal == ".symbol_resolver")
   1148       return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
   1149     if (IDVal == ".private_extern")
   1150       return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
   1151     if (IDVal == ".reference")
   1152       return ParseDirectiveSymbolAttribute(MCSA_Reference);
   1153     if (IDVal == ".weak_definition")
   1154       return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
   1155     if (IDVal == ".weak_reference")
   1156       return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
   1157     if (IDVal == ".weak_def_can_be_hidden")
   1158       return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
   1159 
   1160     if (IDVal == ".comm" || IDVal == ".common")
   1161       return ParseDirectiveComm(/*IsLocal=*/false);
   1162     if (IDVal == ".lcomm")
   1163       return ParseDirectiveComm(/*IsLocal=*/true);
   1164 
   1165     if (IDVal == ".abort")
   1166       return ParseDirectiveAbort();
   1167     if (IDVal == ".include")
   1168       return ParseDirectiveInclude();
   1169 
   1170     if (IDVal == ".code16")
   1171       return TokError(Twine(IDVal) + " not supported yet");
   1172 
   1173     // Look up the handler in the handler table.
   1174     std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
   1175       DirectiveMap.lookup(IDVal);
   1176     if (Handler.first)
   1177       return (*Handler.second)(Handler.first, IDVal, IDLoc);
   1178 
   1179     // Target hook for parsing target specific directives.
   1180     if (!getTargetParser().ParseDirective(ID))
   1181       return false;
   1182 
   1183     bool retval = Warning(IDLoc, "ignoring directive for now");
   1184     EatToEndOfStatement();
   1185     return retval;
   1186   }
   1187 
   1188   CheckForValidSection();
   1189 
   1190   // Canonicalize the opcode to lower case.
   1191   SmallString<128> Opcode;
   1192   for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
   1193     Opcode.push_back(tolower(IDVal[i]));
   1194 
   1195   SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
   1196   bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
   1197                                                      ParsedOperands);
   1198 
   1199   // Dump the parsed representation, if requested.
   1200   if (getShowParsedOperands()) {
   1201     SmallString<256> Str;
   1202     raw_svector_ostream OS(Str);
   1203     OS << "parsed instruction: [";
   1204     for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
   1205       if (i != 0)
   1206         OS << ", ";
   1207       ParsedOperands[i]->print(OS);
   1208     }
   1209     OS << "]";
   1210 
   1211     PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
   1212   }
   1213 
   1214   // If parsing succeeded, match the instruction.
   1215   if (!HadError)
   1216     HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
   1217                                                          Out);
   1218 
   1219   // Free any parsed operands.
   1220   for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
   1221     delete ParsedOperands[i];
   1222 
   1223   // Don't skip the rest of the line, the instruction parser is responsible for
   1224   // that.
   1225   return false;
   1226 }
   1227 
   1228 /// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
   1229 /// since they may not be able to be tokenized to get to the end of line token.
   1230 void AsmParser::EatToEndOfLine() {
   1231  Lexer.LexUntilEndOfLine();
   1232  // Eat EOL.
   1233  Lex();
   1234 }
   1235 
   1236 /// ParseCppHashLineFilenameComment as this:
   1237 ///   ::= # number "filename"
   1238 /// or just as a full line comment if it doesn't have a number and a string.
   1239 bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
   1240   Lex(); // Eat the hash token.
   1241 
   1242   if (getLexer().isNot(AsmToken::Integer)) {
   1243     // Consume the line since in cases it is not a well-formed line directive,
   1244     // as if were simply a full line comment.
   1245     EatToEndOfLine();
   1246     return false;
   1247   }
   1248 
   1249   int64_t LineNumber = getTok().getIntVal();
   1250   Lex();
   1251 
   1252   if (getLexer().isNot(AsmToken::String)) {
   1253     EatToEndOfLine();
   1254     return false;
   1255   }
   1256 
   1257   StringRef Filename = getTok().getString();
   1258   // Get rid of the enclosing quotes.
   1259   Filename = Filename.substr(1, Filename.size()-2);
   1260 
   1261   // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
   1262   CppHashLoc = L;
   1263   CppHashFilename = Filename;
   1264   CppHashLineNumber = LineNumber;
   1265 
   1266   // Ignore any trailing characters, they're just comment.
   1267   EatToEndOfLine();
   1268   return false;
   1269 }
   1270 
   1271 /// DiagHandler - will use the the last parsed cpp hash line filename comment
   1272 /// for the Filename and LineNo if any in the diagnostic.
   1273 void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
   1274   const AsmParser *Parser = static_cast<const AsmParser*>(Context);
   1275   raw_ostream &OS = errs();
   1276 
   1277   const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
   1278   const SMLoc &DiagLoc = Diag.getLoc();
   1279   int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
   1280   int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
   1281 
   1282   // Like SourceMgr::PrintMessage() we need to print the include stack if any
   1283   // before printing the message.
   1284   int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
   1285   if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
   1286      SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
   1287      DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
   1288   }
   1289 
   1290   // If we have not parsed a cpp hash line filename comment or the source
   1291   // manager changed or buffer changed (like in a nested include) then just
   1292   // print the normal diagnostic using its Filename and LineNo.
   1293   if (!Parser->CppHashLineNumber ||
   1294       &DiagSrcMgr != &Parser->SrcMgr ||
   1295       DiagBuf != CppHashBuf) {
   1296     if (Parser->SavedDiagHandler)
   1297       Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
   1298     else
   1299       Diag.print(0, OS);
   1300     return;
   1301   }
   1302 
   1303   // Use the CppHashFilename and calculate a line number based on the
   1304   // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
   1305   // the diagnostic.
   1306   const std::string Filename = Parser->CppHashFilename;
   1307 
   1308   int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
   1309   int CppHashLocLineNo =
   1310       Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
   1311   int LineNo = Parser->CppHashLineNumber - 1 +
   1312                (DiagLocLineNo - CppHashLocLineNo);
   1313 
   1314   SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
   1315                        Filename, LineNo, Diag.getColumnNo(),
   1316                        Diag.getKind(), Diag.getMessage(),
   1317                        Diag.getLineContents(), Diag.getRanges());
   1318 
   1319   if (Parser->SavedDiagHandler)
   1320     Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
   1321   else
   1322     NewDiag.print(0, OS);
   1323 }
   1324 
   1325 bool AsmParser::expandMacro(SmallString<256> &Buf, StringRef Body,
   1326                             const std::vector<StringRef> &Parameters,
   1327                             const std::vector<std::vector<AsmToken> > &A,
   1328                             const SMLoc &L) {
   1329   raw_svector_ostream OS(Buf);
   1330   unsigned NParameters = Parameters.size();
   1331   if (NParameters != 0 && NParameters != A.size())
   1332     return Error(L, "Wrong number of arguments");
   1333 
   1334   while (!Body.empty()) {
   1335     // Scan for the next substitution.
   1336     std::size_t End = Body.size(), Pos = 0;
   1337     for (; Pos != End; ++Pos) {
   1338       // Check for a substitution or escape.
   1339       if (!NParameters) {
   1340         // This macro has no parameters, look for $0, $1, etc.
   1341         if (Body[Pos] != '$' || Pos + 1 == End)
   1342           continue;
   1343 
   1344         char Next = Body[Pos + 1];
   1345         if (Next == '$' || Next == 'n' || isdigit(Next))
   1346           break;
   1347       } else {
   1348         // This macro has parameters, look for \foo, \bar, etc.
   1349         if (Body[Pos] == '\\' && Pos + 1 != End)
   1350           break;
   1351       }
   1352     }
   1353 
   1354     // Add the prefix.
   1355     OS << Body.slice(0, Pos);
   1356 
   1357     // Check if we reached the end.
   1358     if (Pos == End)
   1359       break;
   1360 
   1361     if (!NParameters) {
   1362       switch (Body[Pos+1]) {
   1363         // $$ => $
   1364       case '$':
   1365         OS << '$';
   1366         break;
   1367 
   1368         // $n => number of arguments
   1369       case 'n':
   1370         OS << A.size();
   1371         break;
   1372 
   1373         // $[0-9] => argument
   1374       default: {
   1375         // Missing arguments are ignored.
   1376         unsigned Index = Body[Pos+1] - '0';
   1377         if (Index >= A.size())
   1378           break;
   1379 
   1380         // Otherwise substitute with the token values, with spaces eliminated.
   1381         for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
   1382                ie = A[Index].end(); it != ie; ++it)
   1383           OS << it->getString();
   1384         break;
   1385       }
   1386       }
   1387       Pos += 2;
   1388     } else {
   1389       unsigned I = Pos + 1;
   1390       while (isalnum(Body[I]) && I + 1 != End)
   1391         ++I;
   1392 
   1393       const char *Begin = Body.data() + Pos +1;
   1394       StringRef Argument(Begin, I - (Pos +1));
   1395       unsigned Index = 0;
   1396       for (; Index < NParameters; ++Index)
   1397         if (Parameters[Index] == Argument)
   1398           break;
   1399 
   1400       // FIXME: We should error at the macro definition.
   1401       if (Index == NParameters)
   1402         return Error(L, "Parameter not found");
   1403 
   1404       for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
   1405              ie = A[Index].end(); it != ie; ++it)
   1406         OS << it->getString();
   1407 
   1408       Pos += 1 + Argument.size();
   1409     }
   1410     // Update the scan point.
   1411     Body = Body.substr(Pos);
   1412   }
   1413 
   1414   // We include the .endmacro in the buffer as our queue to exit the macro
   1415   // instantiation.
   1416   OS << ".endmacro\n";
   1417   return false;
   1418 }
   1419 
   1420 MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
   1421                                        MemoryBuffer *I)
   1422   : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
   1423 {
   1424 }
   1425 
   1426 bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
   1427                                  const Macro *M) {
   1428   // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
   1429   // this, although we should protect against infinite loops.
   1430   if (ActiveMacros.size() == 20)
   1431     return TokError("macros cannot be nested more than 20 levels deep");
   1432 
   1433   // Parse the macro instantiation arguments.
   1434   std::vector<std::vector<AsmToken> > MacroArguments;
   1435   MacroArguments.push_back(std::vector<AsmToken>());
   1436   unsigned ParenLevel = 0;
   1437   for (;;) {
   1438     if (Lexer.is(AsmToken::Eof))
   1439       return TokError("unexpected token in macro instantiation");
   1440     if (Lexer.is(AsmToken::EndOfStatement))
   1441       break;
   1442 
   1443     // If we aren't inside parentheses and this is a comma, start a new token
   1444     // list.
   1445     if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
   1446       MacroArguments.push_back(std::vector<AsmToken>());
   1447     } else {
   1448       // Adjust the current parentheses level.
   1449       if (Lexer.is(AsmToken::LParen))
   1450         ++ParenLevel;
   1451       else if (Lexer.is(AsmToken::RParen) && ParenLevel)
   1452         --ParenLevel;
   1453 
   1454       // Append the token to the current argument list.
   1455       MacroArguments.back().push_back(getTok());
   1456     }
   1457     Lex();
   1458   }
   1459 
   1460   // Macro instantiation is lexical, unfortunately. We construct a new buffer
   1461   // to hold the macro body with substitutions.
   1462   SmallString<256> Buf;
   1463   StringRef Body = M->Body;
   1464 
   1465   if (expandMacro(Buf, Body, M->Parameters, MacroArguments, getTok().getLoc()))
   1466     return true;
   1467 
   1468   MemoryBuffer *Instantiation =
   1469     MemoryBuffer::getMemBufferCopy(Buf.str(), "<instantiation>");
   1470 
   1471   // Create the macro instantiation object and add to the current macro
   1472   // instantiation stack.
   1473   MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
   1474                                                   getTok().getLoc(),
   1475                                                   Instantiation);
   1476   ActiveMacros.push_back(MI);
   1477 
   1478   // Jump to the macro instantiation and prime the lexer.
   1479   CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
   1480   Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
   1481   Lex();
   1482 
   1483   return false;
   1484 }
   1485 
   1486 void AsmParser::HandleMacroExit() {
   1487   // Jump to the EndOfStatement we should return to, and consume it.
   1488   JumpToLoc(ActiveMacros.back()->ExitLoc);
   1489   Lex();
   1490 
   1491   // Pop the instantiation entry.
   1492   delete ActiveMacros.back();
   1493   ActiveMacros.pop_back();
   1494 }
   1495 
   1496 static void MarkUsed(const MCExpr *Value) {
   1497   switch (Value->getKind()) {
   1498   case MCExpr::Binary:
   1499     MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
   1500     MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
   1501     break;
   1502   case MCExpr::Target:
   1503   case MCExpr::Constant:
   1504     break;
   1505   case MCExpr::SymbolRef: {
   1506     static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
   1507     break;
   1508   }
   1509   case MCExpr::Unary:
   1510     MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
   1511     break;
   1512   }
   1513 }
   1514 
   1515 bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
   1516   // FIXME: Use better location, we should use proper tokens.
   1517   SMLoc EqualLoc = Lexer.getLoc();
   1518 
   1519   const MCExpr *Value;
   1520   if (ParseExpression(Value))
   1521     return true;
   1522 
   1523   MarkUsed(Value);
   1524 
   1525   if (Lexer.isNot(AsmToken::EndOfStatement))
   1526     return TokError("unexpected token in assignment");
   1527 
   1528   // Error on assignment to '.'.
   1529   if (Name == ".") {
   1530     return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
   1531                             "(use '.space' or '.org').)"));
   1532   }
   1533 
   1534   // Eat the end of statement marker.
   1535   Lex();
   1536 
   1537   // Validate that the LHS is allowed to be a variable (either it has not been
   1538   // used as a symbol, or it is an absolute symbol).
   1539   MCSymbol *Sym = getContext().LookupSymbol(Name);
   1540   if (Sym) {
   1541     // Diagnose assignment to a label.
   1542     //
   1543     // FIXME: Diagnostics. Note the location of the definition as a label.
   1544     // FIXME: Diagnose assignment to protected identifier (e.g., register name).
   1545     if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
   1546       ; // Allow redefinitions of undefined symbols only used in directives.
   1547     else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
   1548       return Error(EqualLoc, "redefinition of '" + Name + "'");
   1549     else if (!Sym->isVariable())
   1550       return Error(EqualLoc, "invalid assignment to '" + Name + "'");
   1551     else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
   1552       return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
   1553                    Name + "'");
   1554 
   1555     // Don't count these checks as uses.
   1556     Sym->setUsed(false);
   1557   } else
   1558     Sym = getContext().GetOrCreateSymbol(Name);
   1559 
   1560   // FIXME: Handle '.'.
   1561 
   1562   // Do the assignment.
   1563   Out.EmitAssignment(Sym, Value);
   1564 
   1565   return false;
   1566 }
   1567 
   1568 /// ParseIdentifier:
   1569 ///   ::= identifier
   1570 ///   ::= string
   1571 bool AsmParser::ParseIdentifier(StringRef &Res) {
   1572   // The assembler has relaxed rules for accepting identifiers, in particular we
   1573   // allow things like '.globl $foo', which would normally be separate
   1574   // tokens. At this level, we have already lexed so we cannot (currently)
   1575   // handle this as a context dependent token, instead we detect adjacent tokens
   1576   // and return the combined identifier.
   1577   if (Lexer.is(AsmToken::Dollar)) {
   1578     SMLoc DollarLoc = getLexer().getLoc();
   1579 
   1580     // Consume the dollar sign, and check for a following identifier.
   1581     Lex();
   1582     if (Lexer.isNot(AsmToken::Identifier))
   1583       return true;
   1584 
   1585     // We have a '$' followed by an identifier, make sure they are adjacent.
   1586     if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
   1587       return true;
   1588 
   1589     // Construct the joined identifier and consume the token.
   1590     Res = StringRef(DollarLoc.getPointer(),
   1591                     getTok().getIdentifier().size() + 1);
   1592     Lex();
   1593     return false;
   1594   }
   1595 
   1596   if (Lexer.isNot(AsmToken::Identifier) &&
   1597       Lexer.isNot(AsmToken::String))
   1598     return true;
   1599 
   1600   Res = getTok().getIdentifier();
   1601 
   1602   Lex(); // Consume the identifier token.
   1603 
   1604   return false;
   1605 }
   1606 
   1607 /// ParseDirectiveSet:
   1608 ///   ::= .equ identifier ',' expression
   1609 ///   ::= .equiv identifier ',' expression
   1610 ///   ::= .set identifier ',' expression
   1611 bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
   1612   StringRef Name;
   1613 
   1614   if (ParseIdentifier(Name))
   1615     return TokError("expected identifier after '" + Twine(IDVal) + "'");
   1616 
   1617   if (getLexer().isNot(AsmToken::Comma))
   1618     return TokError("unexpected token in '" + Twine(IDVal) + "'");
   1619   Lex();
   1620 
   1621   return ParseAssignment(Name, allow_redef);
   1622 }
   1623 
   1624 bool AsmParser::ParseEscapedString(std::string &Data) {
   1625   assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
   1626 
   1627   Data = "";
   1628   StringRef Str = getTok().getStringContents();
   1629   for (unsigned i = 0, e = Str.size(); i != e; ++i) {
   1630     if (Str[i] != '\\') {
   1631       Data += Str[i];
   1632       continue;
   1633     }
   1634 
   1635     // Recognize escaped characters. Note that this escape semantics currently
   1636     // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
   1637     ++i;
   1638     if (i == e)
   1639       return TokError("unexpected backslash at end of string");
   1640 
   1641     // Recognize octal sequences.
   1642     if ((unsigned) (Str[i] - '0') <= 7) {
   1643       // Consume up to three octal characters.
   1644       unsigned Value = Str[i] - '0';
   1645 
   1646       if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
   1647         ++i;
   1648         Value = Value * 8 + (Str[i] - '0');
   1649 
   1650         if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
   1651           ++i;
   1652           Value = Value * 8 + (Str[i] - '0');
   1653         }
   1654       }
   1655 
   1656       if (Value > 255)
   1657         return TokError("invalid octal escape sequence (out of range)");
   1658 
   1659       Data += (unsigned char) Value;
   1660       continue;
   1661     }
   1662 
   1663     // Otherwise recognize individual escapes.
   1664     switch (Str[i]) {
   1665     default:
   1666       // Just reject invalid escape sequences for now.
   1667       return TokError("invalid escape sequence (unrecognized character)");
   1668 
   1669     case 'b': Data += '\b'; break;
   1670     case 'f': Data += '\f'; break;
   1671     case 'n': Data += '\n'; break;
   1672     case 'r': Data += '\r'; break;
   1673     case 't': Data += '\t'; break;
   1674     case '"': Data += '"'; break;
   1675     case '\\': Data += '\\'; break;
   1676     }
   1677   }
   1678 
   1679   return false;
   1680 }
   1681 
   1682 /// ParseDirectiveAscii:
   1683 ///   ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
   1684 bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
   1685   if (getLexer().isNot(AsmToken::EndOfStatement)) {
   1686     CheckForValidSection();
   1687 
   1688     for (;;) {
   1689       if (getLexer().isNot(AsmToken::String))
   1690         return TokError("expected string in '" + Twine(IDVal) + "' directive");
   1691 
   1692       std::string Data;
   1693       if (ParseEscapedString(Data))
   1694         return true;
   1695 
   1696       getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
   1697       if (ZeroTerminated)
   1698         getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
   1699 
   1700       Lex();
   1701 
   1702       if (getLexer().is(AsmToken::EndOfStatement))
   1703         break;
   1704 
   1705       if (getLexer().isNot(AsmToken::Comma))
   1706         return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
   1707       Lex();
   1708     }
   1709   }
   1710 
   1711   Lex();
   1712   return false;
   1713 }
   1714 
   1715 /// ParseDirectiveValue
   1716 ///  ::= (.byte | .short | ... ) [ expression (, expression)* ]
   1717 bool AsmParser::ParseDirectiveValue(unsigned Size) {
   1718   if (getLexer().isNot(AsmToken::EndOfStatement)) {
   1719     CheckForValidSection();
   1720 
   1721     for (;;) {
   1722       const MCExpr *Value;
   1723       SMLoc ExprLoc = getLexer().getLoc();
   1724       if (ParseExpression(Value))
   1725         return true;
   1726 
   1727       // Special case constant expressions to match code generator.
   1728       if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
   1729         assert(Size <= 8 && "Invalid size");
   1730         uint64_t IntValue = MCE->getValue();
   1731         if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
   1732           return Error(ExprLoc, "literal value out of range for directive");
   1733         getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
   1734       } else
   1735         getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
   1736 
   1737       if (getLexer().is(AsmToken::EndOfStatement))
   1738         break;
   1739 
   1740       // FIXME: Improve diagnostic.
   1741       if (getLexer().isNot(AsmToken::Comma))
   1742         return TokError("unexpected token in directive");
   1743       Lex();
   1744     }
   1745   }
   1746 
   1747   Lex();
   1748   return false;
   1749 }
   1750 
   1751 /// ParseDirectiveRealValue
   1752 ///  ::= (.single | .double) [ expression (, expression)* ]
   1753 bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
   1754   if (getLexer().isNot(AsmToken::EndOfStatement)) {
   1755     CheckForValidSection();
   1756 
   1757     for (;;) {
   1758       // We don't truly support arithmetic on floating point expressions, so we
   1759       // have to manually parse unary prefixes.
   1760       bool IsNeg = false;
   1761       if (getLexer().is(AsmToken::Minus)) {
   1762         Lex();
   1763         IsNeg = true;
   1764       } else if (getLexer().is(AsmToken::Plus))
   1765         Lex();
   1766 
   1767       if (getLexer().isNot(AsmToken::Integer) &&
   1768           getLexer().isNot(AsmToken::Real) &&
   1769           getLexer().isNot(AsmToken::Identifier))
   1770         return TokError("unexpected token in directive");
   1771 
   1772       // Convert to an APFloat.
   1773       APFloat Value(Semantics);
   1774       StringRef IDVal = getTok().getString();
   1775       if (getLexer().is(AsmToken::Identifier)) {
   1776         if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
   1777           Value = APFloat::getInf(Semantics);
   1778         else if (!IDVal.compare_lower("nan"))
   1779           Value = APFloat::getNaN(Semantics, false, ~0);
   1780         else
   1781           return TokError("invalid floating point literal");
   1782       } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
   1783           APFloat::opInvalidOp)
   1784         return TokError("invalid floating point literal");
   1785       if (IsNeg)
   1786         Value.changeSign();
   1787 
   1788       // Consume the numeric token.
   1789       Lex();
   1790 
   1791       // Emit the value as an integer.
   1792       APInt AsInt = Value.bitcastToAPInt();
   1793       getStreamer().EmitIntValue(AsInt.getLimitedValue(),
   1794                                  AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
   1795 
   1796       if (getLexer().is(AsmToken::EndOfStatement))
   1797         break;
   1798 
   1799       if (getLexer().isNot(AsmToken::Comma))
   1800         return TokError("unexpected token in directive");
   1801       Lex();
   1802     }
   1803   }
   1804 
   1805   Lex();
   1806   return false;
   1807 }
   1808 
   1809 /// ParseDirectiveSpace
   1810 ///  ::= .space expression [ , expression ]
   1811 bool AsmParser::ParseDirectiveSpace() {
   1812   CheckForValidSection();
   1813 
   1814   int64_t NumBytes;
   1815   if (ParseAbsoluteExpression(NumBytes))
   1816     return true;
   1817 
   1818   int64_t FillExpr = 0;
   1819   if (getLexer().isNot(AsmToken::EndOfStatement)) {
   1820     if (getLexer().isNot(AsmToken::Comma))
   1821       return TokError("unexpected token in '.space' directive");
   1822     Lex();
   1823 
   1824     if (ParseAbsoluteExpression(FillExpr))
   1825       return true;
   1826 
   1827     if (getLexer().isNot(AsmToken::EndOfStatement))
   1828       return TokError("unexpected token in '.space' directive");
   1829   }
   1830 
   1831   Lex();
   1832 
   1833   if (NumBytes <= 0)
   1834     return TokError("invalid number of bytes in '.space' directive");
   1835 
   1836   // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
   1837   getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
   1838 
   1839   return false;
   1840 }
   1841 
   1842 /// ParseDirectiveZero
   1843 ///  ::= .zero expression
   1844 bool AsmParser::ParseDirectiveZero() {
   1845   CheckForValidSection();
   1846 
   1847   int64_t NumBytes;
   1848   if (ParseAbsoluteExpression(NumBytes))
   1849     return true;
   1850 
   1851   int64_t Val = 0;
   1852   if (getLexer().is(AsmToken::Comma)) {
   1853     Lex();
   1854     if (ParseAbsoluteExpression(Val))
   1855       return true;
   1856   }
   1857 
   1858   if (getLexer().isNot(AsmToken::EndOfStatement))
   1859     return TokError("unexpected token in '.zero' directive");
   1860 
   1861   Lex();
   1862 
   1863   getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
   1864 
   1865   return false;
   1866 }
   1867 
   1868 /// ParseDirectiveFill
   1869 ///  ::= .fill expression , expression , expression
   1870 bool AsmParser::ParseDirectiveFill() {
   1871   CheckForValidSection();
   1872 
   1873   int64_t NumValues;
   1874   if (ParseAbsoluteExpression(NumValues))
   1875     return true;
   1876 
   1877   if (getLexer().isNot(AsmToken::Comma))
   1878     return TokError("unexpected token in '.fill' directive");
   1879   Lex();
   1880 
   1881   int64_t FillSize;
   1882   if (ParseAbsoluteExpression(FillSize))
   1883     return true;
   1884 
   1885   if (getLexer().isNot(AsmToken::Comma))
   1886     return TokError("unexpected token in '.fill' directive");
   1887   Lex();
   1888 
   1889   int64_t FillExpr;
   1890   if (ParseAbsoluteExpression(FillExpr))
   1891     return true;
   1892 
   1893   if (getLexer().isNot(AsmToken::EndOfStatement))
   1894     return TokError("unexpected token in '.fill' directive");
   1895 
   1896   Lex();
   1897 
   1898   if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
   1899     return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
   1900 
   1901   for (uint64_t i = 0, e = NumValues; i != e; ++i)
   1902     getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
   1903 
   1904   return false;
   1905 }
   1906 
   1907 /// ParseDirectiveOrg
   1908 ///  ::= .org expression [ , expression ]
   1909 bool AsmParser::ParseDirectiveOrg() {
   1910   CheckForValidSection();
   1911 
   1912   const MCExpr *Offset;
   1913   if (ParseExpression(Offset))
   1914     return true;
   1915 
   1916   // Parse optional fill expression.
   1917   int64_t FillExpr = 0;
   1918   if (getLexer().isNot(AsmToken::EndOfStatement)) {
   1919     if (getLexer().isNot(AsmToken::Comma))
   1920       return TokError("unexpected token in '.org' directive");
   1921     Lex();
   1922 
   1923     if (ParseAbsoluteExpression(FillExpr))
   1924       return true;
   1925 
   1926     if (getLexer().isNot(AsmToken::EndOfStatement))
   1927       return TokError("unexpected token in '.org' directive");
   1928   }
   1929 
   1930   Lex();
   1931 
   1932   // FIXME: Only limited forms of relocatable expressions are accepted here, it
   1933   // has to be relative to the current section.
   1934   getStreamer().EmitValueToOffset(Offset, FillExpr);
   1935 
   1936   return false;
   1937 }
   1938 
   1939 /// ParseDirectiveAlign
   1940 ///  ::= {.align, ...} expression [ , expression [ , expression ]]
   1941 bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
   1942   CheckForValidSection();
   1943 
   1944   SMLoc AlignmentLoc = getLexer().getLoc();
   1945   int64_t Alignment;
   1946   if (ParseAbsoluteExpression(Alignment))
   1947     return true;
   1948 
   1949   SMLoc MaxBytesLoc;
   1950   bool HasFillExpr = false;
   1951   int64_t FillExpr = 0;
   1952   int64_t MaxBytesToFill = 0;
   1953   if (getLexer().isNot(AsmToken::EndOfStatement)) {
   1954     if (getLexer().isNot(AsmToken::Comma))
   1955       return TokError("unexpected token in directive");
   1956     Lex();
   1957 
   1958     // The fill expression can be omitted while specifying a maximum number of
   1959     // alignment bytes, e.g:
   1960     //  .align 3,,4
   1961     if (getLexer().isNot(AsmToken::Comma)) {
   1962       HasFillExpr = true;
   1963       if (ParseAbsoluteExpression(FillExpr))
   1964         return true;
   1965     }
   1966 
   1967     if (getLexer().isNot(AsmToken::EndOfStatement)) {
   1968       if (getLexer().isNot(AsmToken::Comma))
   1969         return TokError("unexpected token in directive");
   1970       Lex();
   1971 
   1972       MaxBytesLoc = getLexer().getLoc();
   1973       if (ParseAbsoluteExpression(MaxBytesToFill))
   1974         return true;
   1975 
   1976       if (getLexer().isNot(AsmToken::EndOfStatement))
   1977         return TokError("unexpected token in directive");
   1978     }
   1979   }
   1980 
   1981   Lex();
   1982 
   1983   if (!HasFillExpr)
   1984     FillExpr = 0;
   1985 
   1986   // Compute alignment in bytes.
   1987   if (IsPow2) {
   1988     // FIXME: Diagnose overflow.
   1989     if (Alignment >= 32) {
   1990       Error(AlignmentLoc, "invalid alignment value");
   1991       Alignment = 31;
   1992     }
   1993 
   1994     Alignment = 1ULL << Alignment;
   1995   }
   1996 
   1997   // Diagnose non-sensical max bytes to align.
   1998   if (MaxBytesLoc.isValid()) {
   1999     if (MaxBytesToFill < 1) {
   2000       Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
   2001             "many bytes, ignoring maximum bytes expression");
   2002       MaxBytesToFill = 0;
   2003     }
   2004 
   2005     if (MaxBytesToFill >= Alignment) {
   2006       Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
   2007               "has no effect");
   2008       MaxBytesToFill = 0;
   2009     }
   2010   }
   2011 
   2012   // Check whether we should use optimal code alignment for this .align
   2013   // directive.
   2014   bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
   2015   if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
   2016       ValueSize == 1 && UseCodeAlign) {
   2017     getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
   2018   } else {
   2019     // FIXME: Target specific behavior about how the "extra" bytes are filled.
   2020     getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
   2021                                        MaxBytesToFill);
   2022   }
   2023 
   2024   return false;
   2025 }
   2026 
   2027 /// ParseDirectiveSymbolAttribute
   2028 ///  ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
   2029 bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
   2030   if (getLexer().isNot(AsmToken::EndOfStatement)) {
   2031     for (;;) {
   2032       StringRef Name;
   2033       SMLoc Loc = getTok().getLoc();
   2034 
   2035       if (ParseIdentifier(Name))
   2036         return Error(Loc, "expected identifier in directive");
   2037 
   2038       MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
   2039 
   2040       // Assembler local symbols don't make any sense here. Complain loudly.
   2041       if (Sym->isTemporary())
   2042         return Error(Loc, "non-local symbol required in directive");
   2043 
   2044       getStreamer().EmitSymbolAttribute(Sym, Attr);
   2045 
   2046       if (getLexer().is(AsmToken::EndOfStatement))
   2047         break;
   2048 
   2049       if (getLexer().isNot(AsmToken::Comma))
   2050         return TokError("unexpected token in directive");
   2051       Lex();
   2052     }
   2053   }
   2054 
   2055   Lex();
   2056   return false;
   2057 }
   2058 
   2059 /// ParseDirectiveComm
   2060 ///  ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
   2061 bool AsmParser::ParseDirectiveComm(bool IsLocal) {
   2062   CheckForValidSection();
   2063 
   2064   SMLoc IDLoc = getLexer().getLoc();
   2065   StringRef Name;
   2066   if (ParseIdentifier(Name))
   2067     return TokError("expected identifier in directive");
   2068 
   2069   // Handle the identifier as the key symbol.
   2070   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
   2071 
   2072   if (getLexer().isNot(AsmToken::Comma))
   2073     return TokError("unexpected token in directive");
   2074   Lex();
   2075 
   2076   int64_t Size;
   2077   SMLoc SizeLoc = getLexer().getLoc();
   2078   if (ParseAbsoluteExpression(Size))
   2079     return true;
   2080 
   2081   int64_t Pow2Alignment = 0;
   2082   SMLoc Pow2AlignmentLoc;
   2083   if (getLexer().is(AsmToken::Comma)) {
   2084     Lex();
   2085     Pow2AlignmentLoc = getLexer().getLoc();
   2086     if (ParseAbsoluteExpression(Pow2Alignment))
   2087       return true;
   2088 
   2089     // If this target takes alignments in bytes (not log) validate and convert.
   2090     if (Lexer.getMAI().getAlignmentIsInBytes()) {
   2091       if (!isPowerOf2_64(Pow2Alignment))
   2092         return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
   2093       Pow2Alignment = Log2_64(Pow2Alignment);
   2094     }
   2095   }
   2096 
   2097   if (getLexer().isNot(AsmToken::EndOfStatement))
   2098     return TokError("unexpected token in '.comm' or '.lcomm' directive");
   2099 
   2100   Lex();
   2101 
   2102   // NOTE: a size of zero for a .comm should create a undefined symbol
   2103   // but a size of .lcomm creates a bss symbol of size zero.
   2104   if (Size < 0)
   2105     return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
   2106                  "be less than zero");
   2107 
   2108   // NOTE: The alignment in the directive is a power of 2 value, the assembler
   2109   // may internally end up wanting an alignment in bytes.
   2110   // FIXME: Diagnose overflow.
   2111   if (Pow2Alignment < 0)
   2112     return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
   2113                  "alignment, can't be less than zero");
   2114 
   2115   if (!Sym->isUndefined())
   2116     return Error(IDLoc, "invalid symbol redefinition");
   2117 
   2118   // '.lcomm' is equivalent to '.zerofill'.
   2119   // Create the Symbol as a common or local common with Size and Pow2Alignment
   2120   if (IsLocal) {
   2121     getStreamer().EmitZerofill(Ctx.getMachOSection(
   2122                                  "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
   2123                                  0, SectionKind::getBSS()),
   2124                                Sym, Size, 1 << Pow2Alignment);
   2125     return false;
   2126   }
   2127 
   2128   getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
   2129   return false;
   2130 }
   2131 
   2132 /// ParseDirectiveAbort
   2133 ///  ::= .abort [... message ...]
   2134 bool AsmParser::ParseDirectiveAbort() {
   2135   // FIXME: Use loc from directive.
   2136   SMLoc Loc = getLexer().getLoc();
   2137 
   2138   StringRef Str = ParseStringToEndOfStatement();
   2139   if (getLexer().isNot(AsmToken::EndOfStatement))
   2140     return TokError("unexpected token in '.abort' directive");
   2141 
   2142   Lex();
   2143 
   2144   if (Str.empty())
   2145     Error(Loc, ".abort detected. Assembly stopping.");
   2146   else
   2147     Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
   2148   // FIXME: Actually abort assembly here.
   2149 
   2150   return false;
   2151 }
   2152 
   2153 /// ParseDirectiveInclude
   2154 ///  ::= .include "filename"
   2155 bool AsmParser::ParseDirectiveInclude() {
   2156   if (getLexer().isNot(AsmToken::String))
   2157     return TokError("expected string in '.include' directive");
   2158 
   2159   std::string Filename = getTok().getString();
   2160   SMLoc IncludeLoc = getLexer().getLoc();
   2161   Lex();
   2162 
   2163   if (getLexer().isNot(AsmToken::EndOfStatement))
   2164     return TokError("unexpected token in '.include' directive");
   2165 
   2166   // Strip the quotes.
   2167   Filename = Filename.substr(1, Filename.size()-2);
   2168 
   2169   // Attempt to switch the lexer to the included file before consuming the end
   2170   // of statement to avoid losing it when we switch.
   2171   if (EnterIncludeFile(Filename)) {
   2172     Error(IncludeLoc, "Could not find include file '" + Filename + "'");
   2173     return true;
   2174   }
   2175 
   2176   return false;
   2177 }
   2178 
   2179 /// ParseDirectiveIf
   2180 /// ::= .if expression
   2181 bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
   2182   TheCondStack.push_back(TheCondState);
   2183   TheCondState.TheCond = AsmCond::IfCond;
   2184   if(TheCondState.Ignore) {
   2185     EatToEndOfStatement();
   2186   }
   2187   else {
   2188     int64_t ExprValue;
   2189     if (ParseAbsoluteExpression(ExprValue))
   2190       return true;
   2191 
   2192     if (getLexer().isNot(AsmToken::EndOfStatement))
   2193       return TokError("unexpected token in '.if' directive");
   2194 
   2195     Lex();
   2196 
   2197     TheCondState.CondMet = ExprValue;
   2198     TheCondState.Ignore = !TheCondState.CondMet;
   2199   }
   2200 
   2201   return false;
   2202 }
   2203 
   2204 bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
   2205   StringRef Name;
   2206   TheCondStack.push_back(TheCondState);
   2207   TheCondState.TheCond = AsmCond::IfCond;
   2208 
   2209   if (TheCondState.Ignore) {
   2210     EatToEndOfStatement();
   2211   } else {
   2212     if (ParseIdentifier(Name))
   2213       return TokError("expected identifier after '.ifdef'");
   2214 
   2215     Lex();
   2216 
   2217     MCSymbol *Sym = getContext().LookupSymbol(Name);
   2218 
   2219     if (expect_defined)
   2220       TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
   2221     else
   2222       TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
   2223     TheCondState.Ignore = !TheCondState.CondMet;
   2224   }
   2225 
   2226   return false;
   2227 }
   2228 
   2229 /// ParseDirectiveElseIf
   2230 /// ::= .elseif expression
   2231 bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
   2232   if (TheCondState.TheCond != AsmCond::IfCond &&
   2233       TheCondState.TheCond != AsmCond::ElseIfCond)
   2234       Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
   2235                           " an .elseif");
   2236   TheCondState.TheCond = AsmCond::ElseIfCond;
   2237 
   2238   bool LastIgnoreState = false;
   2239   if (!TheCondStack.empty())
   2240       LastIgnoreState = TheCondStack.back().Ignore;
   2241   if (LastIgnoreState || TheCondState.CondMet) {
   2242     TheCondState.Ignore = true;
   2243     EatToEndOfStatement();
   2244   }
   2245   else {
   2246     int64_t ExprValue;
   2247     if (ParseAbsoluteExpression(ExprValue))
   2248       return true;
   2249 
   2250     if (getLexer().isNot(AsmToken::EndOfStatement))
   2251       return TokError("unexpected token in '.elseif' directive");
   2252 
   2253     Lex();
   2254     TheCondState.CondMet = ExprValue;
   2255     TheCondState.Ignore = !TheCondState.CondMet;
   2256   }
   2257 
   2258   return false;
   2259 }
   2260 
   2261 /// ParseDirectiveElse
   2262 /// ::= .else
   2263 bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
   2264   if (getLexer().isNot(AsmToken::EndOfStatement))
   2265     return TokError("unexpected token in '.else' directive");
   2266 
   2267   Lex();
   2268 
   2269   if (TheCondState.TheCond != AsmCond::IfCond &&
   2270       TheCondState.TheCond != AsmCond::ElseIfCond)
   2271       Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
   2272                           ".elseif");
   2273   TheCondState.TheCond = AsmCond::ElseCond;
   2274   bool LastIgnoreState = false;
   2275   if (!TheCondStack.empty())
   2276     LastIgnoreState = TheCondStack.back().Ignore;
   2277   if (LastIgnoreState || TheCondState.CondMet)
   2278     TheCondState.Ignore = true;
   2279   else
   2280     TheCondState.Ignore = false;
   2281 
   2282   return false;
   2283 }
   2284 
   2285 /// ParseDirectiveEndIf
   2286 /// ::= .endif
   2287 bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
   2288   if (getLexer().isNot(AsmToken::EndOfStatement))
   2289     return TokError("unexpected token in '.endif' directive");
   2290 
   2291   Lex();
   2292 
   2293   if ((TheCondState.TheCond == AsmCond::NoCond) ||
   2294       TheCondStack.empty())
   2295     Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
   2296                         ".else");
   2297   if (!TheCondStack.empty()) {
   2298     TheCondState = TheCondStack.back();
   2299     TheCondStack.pop_back();
   2300   }
   2301 
   2302   return false;
   2303 }
   2304 
   2305 /// ParseDirectiveFile
   2306 /// ::= .file [number] filename
   2307 /// ::= .file number directory filename
   2308 bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
   2309   // FIXME: I'm not sure what this is.
   2310   int64_t FileNumber = -1;
   2311   SMLoc FileNumberLoc = getLexer().getLoc();
   2312   if (getLexer().is(AsmToken::Integer)) {
   2313     FileNumber = getTok().getIntVal();
   2314     Lex();
   2315 
   2316     if (FileNumber < 1)
   2317       return TokError("file number less than one");
   2318   }
   2319 
   2320   if (getLexer().isNot(AsmToken::String))
   2321     return TokError("unexpected token in '.file' directive");
   2322 
   2323   // Usually the directory and filename together, otherwise just the directory.
   2324   StringRef Path = getTok().getString();
   2325   Path = Path.substr(1, Path.size()-2);
   2326   Lex();
   2327 
   2328   StringRef Directory;
   2329   StringRef Filename;
   2330   if (getLexer().is(AsmToken::String)) {
   2331     if (FileNumber == -1)
   2332       return TokError("explicit path specified, but no file number");
   2333     Filename = getTok().getString();
   2334     Filename = Filename.substr(1, Filename.size()-2);
   2335     Directory = Path;
   2336     Lex();
   2337   } else {
   2338     Filename = Path;
   2339   }
   2340 
   2341   if (getLexer().isNot(AsmToken::EndOfStatement))
   2342     return TokError("unexpected token in '.file' directive");
   2343 
   2344   if (FileNumber == -1)
   2345     getStreamer().EmitFileDirective(Filename);
   2346   else {
   2347     if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
   2348       Error(FileNumberLoc, "file number already allocated");
   2349   }
   2350 
   2351   return false;
   2352 }
   2353 
   2354 /// ParseDirectiveLine
   2355 /// ::= .line [number]
   2356 bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
   2357   if (getLexer().isNot(AsmToken::EndOfStatement)) {
   2358     if (getLexer().isNot(AsmToken::Integer))
   2359       return TokError("unexpected token in '.line' directive");
   2360 
   2361     int64_t LineNumber = getTok().getIntVal();
   2362     (void) LineNumber;
   2363     Lex();
   2364 
   2365     // FIXME: Do something with the .line.
   2366   }
   2367 
   2368   if (getLexer().isNot(AsmToken::EndOfStatement))
   2369     return TokError("unexpected token in '.line' directive");
   2370 
   2371   return false;
   2372 }
   2373 
   2374 
   2375 /// ParseDirectiveLoc
   2376 /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
   2377 ///                                [epilogue_begin] [is_stmt VALUE] [isa VALUE]
   2378 /// The first number is a file number, must have been previously assigned with
   2379 /// a .file directive, the second number is the line number and optionally the
   2380 /// third number is a column position (zero if not specified).  The remaining
   2381 /// optional items are .loc sub-directives.
   2382 bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
   2383 
   2384   if (getLexer().isNot(AsmToken::Integer))
   2385     return TokError("unexpected token in '.loc' directive");
   2386   int64_t FileNumber = getTok().getIntVal();
   2387   if (FileNumber < 1)
   2388     return TokError("file number less than one in '.loc' directive");
   2389   if (!getContext().isValidDwarfFileNumber(FileNumber))
   2390     return TokError("unassigned file number in '.loc' directive");
   2391   Lex();
   2392 
   2393   int64_t LineNumber = 0;
   2394   if (getLexer().is(AsmToken::Integer)) {
   2395     LineNumber = getTok().getIntVal();
   2396     if (LineNumber < 1)
   2397       return TokError("line number less than one in '.loc' directive");
   2398     Lex();
   2399   }
   2400 
   2401   int64_t ColumnPos = 0;
   2402   if (getLexer().is(AsmToken::Integer)) {
   2403     ColumnPos = getTok().getIntVal();
   2404     if (ColumnPos < 0)
   2405       return TokError("column position less than zero in '.loc' directive");
   2406     Lex();
   2407   }
   2408 
   2409   unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
   2410   unsigned Isa = 0;
   2411   int64_t Discriminator = 0;
   2412   if (getLexer().isNot(AsmToken::EndOfStatement)) {
   2413     for (;;) {
   2414       if (getLexer().is(AsmToken::EndOfStatement))
   2415         break;
   2416 
   2417       StringRef Name;
   2418       SMLoc Loc = getTok().getLoc();
   2419       if (getParser().ParseIdentifier(Name))
   2420         return TokError("unexpected token in '.loc' directive");
   2421 
   2422       if (Name == "basic_block")
   2423         Flags |= DWARF2_FLAG_BASIC_BLOCK;
   2424       else if (Name == "prologue_end")
   2425         Flags |= DWARF2_FLAG_PROLOGUE_END;
   2426       else if (Name == "epilogue_begin")
   2427         Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
   2428       else if (Name == "is_stmt") {
   2429         SMLoc Loc = getTok().getLoc();
   2430         const MCExpr *Value;
   2431         if (getParser().ParseExpression(Value))
   2432           return true;
   2433         // The expression must be the constant 0 or 1.
   2434         if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
   2435           int Value = MCE->getValue();
   2436           if (Value == 0)
   2437             Flags &= ~DWARF2_FLAG_IS_STMT;
   2438           else if (Value == 1)
   2439             Flags |= DWARF2_FLAG_IS_STMT;
   2440           else
   2441             return Error(Loc, "is_stmt value not 0 or 1");
   2442         }
   2443         else {
   2444           return Error(Loc, "is_stmt value not the constant value of 0 or 1");
   2445         }
   2446       }
   2447       else if (Name == "isa") {
   2448         SMLoc Loc = getTok().getLoc();
   2449         const MCExpr *Value;
   2450         if (getParser().ParseExpression(Value))
   2451           return true;
   2452         // The expression must be a constant greater or equal to 0.
   2453         if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
   2454           int Value = MCE->getValue();
   2455           if (Value < 0)
   2456             return Error(Loc, "isa number less than zero");
   2457           Isa = Value;
   2458         }
   2459         else {
   2460           return Error(Loc, "isa number not a constant value");
   2461         }
   2462       }
   2463       else if (Name == "discriminator") {
   2464         if (getParser().ParseAbsoluteExpression(Discriminator))
   2465           return true;
   2466       }
   2467       else {
   2468         return Error(Loc, "unknown sub-directive in '.loc' directive");
   2469       }
   2470 
   2471       if (getLexer().is(AsmToken::EndOfStatement))
   2472         break;
   2473     }
   2474   }
   2475 
   2476   getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
   2477                                       Isa, Discriminator, StringRef());
   2478 
   2479   return false;
   2480 }
   2481 
   2482 /// ParseDirectiveStabs
   2483 /// ::= .stabs string, number, number, number
   2484 bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
   2485                                            SMLoc DirectiveLoc) {
   2486   return TokError("unsupported directive '" + Directive + "'");
   2487 }
   2488 
   2489 /// ParseDirectiveCFISections
   2490 /// ::= .cfi_sections section [, section]
   2491 bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
   2492                                                  SMLoc DirectiveLoc) {
   2493   StringRef Name;
   2494   bool EH = false;
   2495   bool Debug = false;
   2496 
   2497   if (getParser().ParseIdentifier(Name))
   2498     return TokError("Expected an identifier");
   2499 
   2500   if (Name == ".eh_frame")
   2501     EH = true;
   2502   else if (Name == ".debug_frame")
   2503     Debug = true;
   2504 
   2505   if (getLexer().is(AsmToken::Comma)) {
   2506     Lex();
   2507 
   2508     if (getParser().ParseIdentifier(Name))
   2509       return TokError("Expected an identifier");
   2510 
   2511     if (Name == ".eh_frame")
   2512       EH = true;
   2513     else if (Name == ".debug_frame")
   2514       Debug = true;
   2515   }
   2516 
   2517   getStreamer().EmitCFISections(EH, Debug);
   2518 
   2519   return false;
   2520 }
   2521 
   2522 /// ParseDirectiveCFIStartProc
   2523 /// ::= .cfi_startproc
   2524 bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
   2525                                                   SMLoc DirectiveLoc) {
   2526   getStreamer().EmitCFIStartProc();
   2527   return false;
   2528 }
   2529 
   2530 /// ParseDirectiveCFIEndProc
   2531 /// ::= .cfi_endproc
   2532 bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
   2533   getStreamer().EmitCFIEndProc();
   2534   return false;
   2535 }
   2536 
   2537 /// ParseRegisterOrRegisterNumber - parse register name or number.
   2538 bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
   2539                                                      SMLoc DirectiveLoc) {
   2540   unsigned RegNo;
   2541 
   2542   if (getLexer().isNot(AsmToken::Integer)) {
   2543     if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
   2544       DirectiveLoc))
   2545       return true;
   2546     Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
   2547   } else
   2548     return getParser().ParseAbsoluteExpression(Register);
   2549 
   2550   return false;
   2551 }
   2552 
   2553 /// ParseDirectiveCFIDefCfa
   2554 /// ::= .cfi_def_cfa register,  offset
   2555 bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
   2556                                                SMLoc DirectiveLoc) {
   2557   int64_t Register = 0;
   2558   if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
   2559     return true;
   2560 
   2561   if (getLexer().isNot(AsmToken::Comma))
   2562     return TokError("unexpected token in directive");
   2563   Lex();
   2564 
   2565   int64_t Offset = 0;
   2566   if (getParser().ParseAbsoluteExpression(Offset))
   2567     return true;
   2568 
   2569   getStreamer().EmitCFIDefCfa(Register, Offset);
   2570   return false;
   2571 }
   2572 
   2573 /// ParseDirectiveCFIDefCfaOffset
   2574 /// ::= .cfi_def_cfa_offset offset
   2575 bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
   2576                                                      SMLoc DirectiveLoc) {
   2577   int64_t Offset = 0;
   2578   if (getParser().ParseAbsoluteExpression(Offset))
   2579     return true;
   2580 
   2581   getStreamer().EmitCFIDefCfaOffset(Offset);
   2582   return false;
   2583 }
   2584 
   2585 /// ParseDirectiveCFIAdjustCfaOffset
   2586 /// ::= .cfi_adjust_cfa_offset adjustment
   2587 bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
   2588                                                         SMLoc DirectiveLoc) {
   2589   int64_t Adjustment = 0;
   2590   if (getParser().ParseAbsoluteExpression(Adjustment))
   2591     return true;
   2592 
   2593   getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
   2594   return false;
   2595 }
   2596 
   2597 /// ParseDirectiveCFIDefCfaRegister
   2598 /// ::= .cfi_def_cfa_register register
   2599 bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
   2600                                                        SMLoc DirectiveLoc) {
   2601   int64_t Register = 0;
   2602   if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
   2603     return true;
   2604 
   2605   getStreamer().EmitCFIDefCfaRegister(Register);
   2606   return false;
   2607 }
   2608 
   2609 /// ParseDirectiveCFIOffset
   2610 /// ::= .cfi_offset register, offset
   2611 bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
   2612   int64_t Register = 0;
   2613   int64_t Offset = 0;
   2614 
   2615   if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
   2616     return true;
   2617 
   2618   if (getLexer().isNot(AsmToken::Comma))
   2619     return TokError("unexpected token in directive");
   2620   Lex();
   2621 
   2622   if (getParser().ParseAbsoluteExpression(Offset))
   2623     return true;
   2624 
   2625   getStreamer().EmitCFIOffset(Register, Offset);
   2626   return false;
   2627 }
   2628 
   2629 /// ParseDirectiveCFIRelOffset
   2630 /// ::= .cfi_rel_offset register, offset
   2631 bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
   2632                                                   SMLoc DirectiveLoc) {
   2633   int64_t Register = 0;
   2634 
   2635   if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
   2636     return true;
   2637 
   2638   if (getLexer().isNot(AsmToken::Comma))
   2639     return TokError("unexpected token in directive");
   2640   Lex();
   2641 
   2642   int64_t Offset = 0;
   2643   if (getParser().ParseAbsoluteExpression(Offset))
   2644     return true;
   2645 
   2646   getStreamer().EmitCFIRelOffset(Register, Offset);
   2647   return false;
   2648 }
   2649 
   2650 static bool isValidEncoding(int64_t Encoding) {
   2651   if (Encoding & ~0xff)
   2652     return false;
   2653 
   2654   if (Encoding == dwarf::DW_EH_PE_omit)
   2655     return true;
   2656 
   2657   const unsigned Format = Encoding & 0xf;
   2658   if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
   2659       Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
   2660       Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
   2661       Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
   2662     return false;
   2663 
   2664   const unsigned Application = Encoding & 0x70;
   2665   if (Application != dwarf::DW_EH_PE_absptr &&
   2666       Application != dwarf::DW_EH_PE_pcrel)
   2667     return false;
   2668 
   2669   return true;
   2670 }
   2671 
   2672 /// ParseDirectiveCFIPersonalityOrLsda
   2673 /// ::= .cfi_personality encoding, [symbol_name]
   2674 /// ::= .cfi_lsda encoding, [symbol_name]
   2675 bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
   2676                                                     SMLoc DirectiveLoc) {
   2677   int64_t Encoding = 0;
   2678   if (getParser().ParseAbsoluteExpression(Encoding))
   2679     return true;
   2680   if (Encoding == dwarf::DW_EH_PE_omit)
   2681     return false;
   2682 
   2683   if (!isValidEncoding(Encoding))
   2684     return TokError("unsupported encoding.");
   2685 
   2686   if (getLexer().isNot(AsmToken::Comma))
   2687     return TokError("unexpected token in directive");
   2688   Lex();
   2689 
   2690   StringRef Name;
   2691   if (getParser().ParseIdentifier(Name))
   2692     return TokError("expected identifier in directive");
   2693 
   2694   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
   2695 
   2696   if (IDVal == ".cfi_personality")
   2697     getStreamer().EmitCFIPersonality(Sym, Encoding);
   2698   else {
   2699     assert(IDVal == ".cfi_lsda");
   2700     getStreamer().EmitCFILsda(Sym, Encoding);
   2701   }
   2702   return false;
   2703 }
   2704 
   2705 /// ParseDirectiveCFIRememberState
   2706 /// ::= .cfi_remember_state
   2707 bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
   2708                                                       SMLoc DirectiveLoc) {
   2709   getStreamer().EmitCFIRememberState();
   2710   return false;
   2711 }
   2712 
   2713 /// ParseDirectiveCFIRestoreState
   2714 /// ::= .cfi_remember_state
   2715 bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
   2716                                                      SMLoc DirectiveLoc) {
   2717   getStreamer().EmitCFIRestoreState();
   2718   return false;
   2719 }
   2720 
   2721 /// ParseDirectiveCFISameValue
   2722 /// ::= .cfi_same_value register
   2723 bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
   2724                                                   SMLoc DirectiveLoc) {
   2725   int64_t Register = 0;
   2726 
   2727   if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
   2728     return true;
   2729 
   2730   getStreamer().EmitCFISameValue(Register);
   2731 
   2732   return false;
   2733 }
   2734 
   2735 /// ParseDirectiveMacrosOnOff
   2736 /// ::= .macros_on
   2737 /// ::= .macros_off
   2738 bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
   2739                                                  SMLoc DirectiveLoc) {
   2740   if (getLexer().isNot(AsmToken::EndOfStatement))
   2741     return Error(getLexer().getLoc(),
   2742                  "unexpected token in '" + Directive + "' directive");
   2743 
   2744   getParser().MacrosEnabled = Directive == ".macros_on";
   2745 
   2746   return false;
   2747 }
   2748 
   2749 /// ParseDirectiveMacro
   2750 /// ::= .macro name [parameters]
   2751 bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
   2752                                            SMLoc DirectiveLoc) {
   2753   StringRef Name;
   2754   if (getParser().ParseIdentifier(Name))
   2755     return TokError("expected identifier in directive");
   2756 
   2757   std::vector<StringRef> Parameters;
   2758   if (getLexer().isNot(AsmToken::EndOfStatement)) {
   2759     for(;;) {
   2760       StringRef Parameter;
   2761       if (getParser().ParseIdentifier(Parameter))
   2762         return TokError("expected identifier in directive");
   2763       Parameters.push_back(Parameter);
   2764 
   2765       if (getLexer().isNot(AsmToken::Comma))
   2766         break;
   2767       Lex();
   2768     }
   2769   }
   2770 
   2771   if (getLexer().isNot(AsmToken::EndOfStatement))
   2772     return TokError("unexpected token in '.macro' directive");
   2773 
   2774   // Eat the end of statement.
   2775   Lex();
   2776 
   2777   AsmToken EndToken, StartToken = getTok();
   2778 
   2779   // Lex the macro definition.
   2780   for (;;) {
   2781     // Check whether we have reached the end of the file.
   2782     if (getLexer().is(AsmToken::Eof))
   2783       return Error(DirectiveLoc, "no matching '.endmacro' in definition");
   2784 
   2785     // Otherwise, check whether we have reach the .endmacro.
   2786     if (getLexer().is(AsmToken::Identifier) &&
   2787         (getTok().getIdentifier() == ".endm" ||
   2788          getTok().getIdentifier() == ".endmacro")) {
   2789       EndToken = getTok();
   2790       Lex();
   2791       if (getLexer().isNot(AsmToken::EndOfStatement))
   2792         return TokError("unexpected token in '" + EndToken.getIdentifier() +
   2793                         "' directive");
   2794       break;
   2795     }
   2796 
   2797     // Otherwise, scan til the end of the statement.
   2798     getParser().EatToEndOfStatement();
   2799   }
   2800 
   2801   if (getParser().MacroMap.lookup(Name)) {
   2802     return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
   2803   }
   2804 
   2805   const char *BodyStart = StartToken.getLoc().getPointer();
   2806   const char *BodyEnd = EndToken.getLoc().getPointer();
   2807   StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
   2808   getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
   2809   return false;
   2810 }
   2811 
   2812 /// ParseDirectiveEndMacro
   2813 /// ::= .endm
   2814 /// ::= .endmacro
   2815 bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
   2816                                            SMLoc DirectiveLoc) {
   2817   if (getLexer().isNot(AsmToken::EndOfStatement))
   2818     return TokError("unexpected token in '" + Directive + "' directive");
   2819 
   2820   // If we are inside a macro instantiation, terminate the current
   2821   // instantiation.
   2822   if (!getParser().ActiveMacros.empty()) {
   2823     getParser().HandleMacroExit();
   2824     return false;
   2825   }
   2826 
   2827   // Otherwise, this .endmacro is a stray entry in the file; well formed
   2828   // .endmacro directives are handled during the macro definition parsing.
   2829   return TokError("unexpected '" + Directive + "' in file, "
   2830                   "no current macro definition");
   2831 }
   2832 
   2833 bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
   2834   getParser().CheckForValidSection();
   2835 
   2836   const MCExpr *Value;
   2837 
   2838   if (getParser().ParseExpression(Value))
   2839     return true;
   2840 
   2841   if (getLexer().isNot(AsmToken::EndOfStatement))
   2842     return TokError("unexpected token in directive");
   2843 
   2844   if (DirName[1] == 's')
   2845     getStreamer().EmitSLEB128Value(Value);
   2846   else
   2847     getStreamer().EmitULEB128Value(Value);
   2848 
   2849   return false;
   2850 }
   2851 
   2852 
   2853 /// \brief Create an MCAsmParser instance.
   2854 MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
   2855                                      MCContext &C, MCStreamer &Out,
   2856                                      const MCAsmInfo &MAI) {
   2857   return new AsmParser(SM, C, Out, MAI);
   2858 }
   2859