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