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