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