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