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