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