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