1 //===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This class implements a parser for assembly files similar to gas syntax.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ADT/APFloat.h"
14 #include "llvm/ADT/APInt.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/None.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/ADT/Twine.h"
24 #include "llvm/BinaryFormat/Dwarf.h"
25 #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
26 #include "llvm/MC/MCAsmInfo.h"
27 #include "llvm/MC/MCCodeView.h"
28 #include "llvm/MC/MCContext.h"
29 #include "llvm/MC/MCDirectives.h"
30 #include "llvm/MC/MCDwarf.h"
31 #include "llvm/MC/MCExpr.h"
32 #include "llvm/MC/MCInstPrinter.h"
33 #include "llvm/MC/MCInstrDesc.h"
34 #include "llvm/MC/MCInstrInfo.h"
35 #include "llvm/MC/MCObjectFileInfo.h"
36 #include "llvm/MC/MCParser/AsmCond.h"
37 #include "llvm/MC/MCParser/AsmLexer.h"
38 #include "llvm/MC/MCParser/MCAsmLexer.h"
39 #include "llvm/MC/MCParser/MCAsmParser.h"
40 #include "llvm/MC/MCParser/MCAsmParserExtension.h"
41 #include "llvm/MC/MCParser/MCAsmParserUtils.h"
42 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
43 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
44 #include "llvm/MC/MCRegisterInfo.h"
45 #include "llvm/MC/MCSection.h"
46 #include "llvm/MC/MCStreamer.h"
47 #include "llvm/MC/MCSymbol.h"
48 #include "llvm/MC/MCTargetOptions.h"
49 #include "llvm/MC/MCValue.h"
50 #include "llvm/Support/Casting.h"
51 #include "llvm/Support/CommandLine.h"
52 #include "llvm/Support/ErrorHandling.h"
53 #include "llvm/Support/MD5.h"
54 #include "llvm/Support/MathExtras.h"
55 #include "llvm/Support/MemoryBuffer.h"
56 #include "llvm/Support/SMLoc.h"
57 #include "llvm/Support/SourceMgr.h"
58 #include "llvm/Support/raw_ostream.h"
75 MCAsmParserSemaCallback::~MCAsmParserSemaCallback() = default;
77 extern cl::opt<unsigned> AsmMacroMaxNestingDepth;
81 /// Helper types for tracking macro definitions.
82 typedef std::vector<AsmToken> MCAsmMacroArgument;
83 typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
85 /// Helper class for storing information about an active macro
87 struct MacroInstantiation {
88 /// The location of the instantiation.
89 SMLoc InstantiationLoc;
91 /// The buffer where parsing should resume upon instantiation completion.
94 /// The location where parsing should resume upon instantiation completion.
97 /// The depth of TheCondStack at the start of the instantiation.
98 size_t CondStackDepth;
101 struct ParseStatementInfo {
102 /// The parsed operands from the last parsed statement.
103 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands;
105 /// The opcode from the last parsed instruction.
106 unsigned Opcode = ~0U;
108 /// Was there an error parsing the inline assembly?
109 bool ParseError = false;
111 SmallVectorImpl<AsmRewrite> *AsmRewrites = nullptr;
113 ParseStatementInfo() = delete;
114 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
115 : AsmRewrites(rewrites) {}
118 /// The concrete assembly parser instance.
119 class AsmParser : public MCAsmParser {
124 const MCAsmInfo &MAI;
126 SourceMgr::DiagHandlerTy SavedDiagHandler;
127 void *SavedDiagContext;
128 std::unique_ptr<MCAsmParserExtension> PlatformParser;
130 /// This is the current buffer index we're lexing from as managed by the
131 /// SourceMgr object.
134 AsmCond TheCondState;
135 std::vector<AsmCond> TheCondStack;
137 /// maps directive names to handler methods in parser
138 /// extensions. Extensions register themselves in this map by calling
139 /// addDirectiveHandler.
140 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
142 /// Stack of active macro instantiations.
143 std::vector<MacroInstantiation*> ActiveMacros;
145 /// List of bodies of anonymous macros.
146 std::deque<MCAsmMacro> MacroLikeBodies;
148 /// Boolean tracking whether macro substitution is enabled.
149 unsigned MacrosEnabledFlag : 1;
151 /// Keeps track of how many .macro's have been instantiated.
152 unsigned NumOfMacroInstantiations;
154 /// The values from the last parsed cpp hash file line comment if any.
155 struct CppHashInfoTy {
160 CppHashInfoTy() : Filename(), LineNumber(0), Loc(), Buf(0) {}
162 CppHashInfoTy CppHashInfo;
164 /// The filename from the first cpp hash file line comment, if any.
165 StringRef FirstCppHashFilename;
167 /// List of forward directional labels for diagnosis at the end.
168 SmallVector<std::tuple<SMLoc, CppHashInfoTy, MCSymbol *>, 4> DirLabels;
170 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
171 unsigned AssemblerDialect = ~0U;
173 /// is Darwin compatibility enabled?
174 bool IsDarwin = false;
176 /// Are we parsing ms-style inline assembly?
177 bool ParsingMSInlineAsm = false;
179 /// Did we already inform the user about inconsistent MD5 usage?
180 bool ReportedInconsistentMD5 = false;
182 // Is alt macro mode enabled.
183 bool AltMacroMode = false;
186 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
187 const MCAsmInfo &MAI, unsigned CB);
188 AsmParser(const AsmParser &) = delete;
189 AsmParser &operator=(const AsmParser &) = delete;
190 ~AsmParser() override;
192 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
194 void addDirectiveHandler(StringRef Directive,
195 ExtensionDirectiveHandler Handler) override {
196 ExtensionDirectiveMap[Directive] = Handler;
199 void addAliasForDirective(StringRef Directive, StringRef Alias) override {
200 DirectiveKindMap[Directive.lower()] = DirectiveKindMap[Alias.lower()];
203 /// @name MCAsmParser Interface
206 SourceMgr &getSourceManager() override { return SrcMgr; }
207 MCAsmLexer &getLexer() override { return Lexer; }
208 MCContext &getContext() override { return Ctx; }
209 MCStreamer &getStreamer() override { return Out; }
211 CodeViewContext &getCVContext() { return Ctx.getCVContext(); }
213 unsigned getAssemblerDialect() override {
214 if (AssemblerDialect == ~0U)
215 return MAI.getAssemblerDialect();
217 return AssemblerDialect;
219 void setAssemblerDialect(unsigned i) override {
220 AssemblerDialect = i;
223 void Note(SMLoc L, const Twine &Msg, SMRange Range = None) override;
224 bool Warning(SMLoc L, const Twine &Msg, SMRange Range = None) override;
225 bool printError(SMLoc L, const Twine &Msg, SMRange Range = None) override;
227 const AsmToken &Lex() override;
229 void setParsingMSInlineAsm(bool V) override {
230 ParsingMSInlineAsm = V;
231 // When parsing MS inline asm, we must lex 0b1101 and 0ABCH as binary and
232 // hex integer literals.
233 Lexer.setLexMasmIntegers(V);
235 bool isParsingMSInlineAsm() override { return ParsingMSInlineAsm; }
237 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
238 unsigned &NumOutputs, unsigned &NumInputs,
239 SmallVectorImpl<std::pair<void *,bool>> &OpDecls,
240 SmallVectorImpl<std::string> &Constraints,
241 SmallVectorImpl<std::string> &Clobbers,
242 const MCInstrInfo *MII, const MCInstPrinter *IP,
243 MCAsmParserSemaCallback &SI) override;
245 bool parseExpression(const MCExpr *&Res);
246 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
247 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
248 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
249 bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
250 SMLoc &EndLoc) override;
251 bool parseAbsoluteExpression(int64_t &Res) override;
253 /// Parse a floating point expression using the float \p Semantics
254 /// and set \p Res to the value.
255 bool parseRealValue(const fltSemantics &Semantics, APInt &Res);
257 /// Parse an identifier or string (as a quoted identifier)
258 /// and set \p Res to the identifier contents.
259 bool parseIdentifier(StringRef &Res) override;
260 void eatToEndOfStatement() override;
262 bool checkForValidSection() override;
267 bool parseStatement(ParseStatementInfo &Info,
268 MCAsmParserSemaCallback *SI);
269 bool parseCurlyBlockScope(SmallVectorImpl<AsmRewrite>& AsmStrRewrites);
270 bool parseCppHashLineFilenameComment(SMLoc L, bool SaveLocInfo = true);
272 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
273 ArrayRef<MCAsmMacroParameter> Parameters);
274 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
275 ArrayRef<MCAsmMacroParameter> Parameters,
276 ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable,
279 /// Are macros enabled in the parser?
280 bool areMacrosEnabled() {return MacrosEnabledFlag;}
282 /// Control a flag in the parser that enables or disables macros.
283 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
285 /// Are we inside a macro instantiation?
286 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
288 /// Handle entry to macro instantiation.
290 /// \param M The macro.
291 /// \param NameLoc Instantiation location.
292 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
294 /// Handle exit from macro instantiation.
295 void handleMacroExit();
297 /// Extract AsmTokens for a macro argument.
298 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
300 /// Parse all macro arguments for a given macro.
301 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
303 void printMacroInstantiations();
304 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
305 SMRange Range = None) const {
306 ArrayRef<SMRange> Ranges(Range);
307 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
309 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
311 /// Should we emit DWARF describing this assembler source? (Returns false if
312 /// the source has .file directives, which means we don't want to generate
313 /// info describing the assembler source itself.)
314 bool enabledGenDwarfForAssembly();
316 /// Enter the specified file. This returns true on failure.
317 bool enterIncludeFile(const std::string &Filename);
319 /// Process the specified file for the .incbin directive.
320 /// This returns true on failure.
321 bool processIncbinFile(const std::string &Filename, int64_t Skip = 0,
322 const MCExpr *Count = nullptr, SMLoc Loc = SMLoc());
324 /// Reset the current lexer position to that given by \p Loc. The
325 /// current token is not set; clients should ensure Lex() is called
328 /// \param InBuffer If not 0, should be the known buffer id that contains the
330 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
332 /// Parse up to the end of statement and a return the contents from the
333 /// current token until the end of the statement; the current token on exit
334 /// will be either the EndOfStatement or EOF.
335 StringRef parseStringToEndOfStatement() override;
337 /// Parse until the end of a statement or a comma is encountered,
338 /// return the contents from the current token up to the end or comma.
339 StringRef parseStringToComma();
341 bool parseAssignment(StringRef Name, bool allow_redef,
342 bool NoDeadStrip = false);
344 unsigned getBinOpPrecedence(AsmToken::TokenKind K,
345 MCBinaryExpr::Opcode &Kind);
347 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
348 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
349 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
351 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
353 bool parseCVFunctionId(int64_t &FunctionId, StringRef DirectiveName);
354 bool parseCVFileId(int64_t &FileId, StringRef DirectiveName);
356 // Generic (target and platform independent) directive parsing.
358 DK_NO_DIRECTIVE, // Placeholder
413 DK_BUNDLE_ALIGN_MODE,
427 DK_WEAK_DEF_CAN_BE_HIDDEN,
467 DK_CV_INLINE_SITE_ID,
470 DK_CV_INLINE_LINETABLE,
475 DK_CV_FILECHECKSUM_OFFSET,
481 DK_CFI_DEF_CFA_OFFSET,
482 DK_CFI_ADJUST_CFA_OFFSET,
483 DK_CFI_DEF_CFA_REGISTER,
488 DK_CFI_REMEMBER_STATE,
489 DK_CFI_RESTORE_STATE,
493 DK_CFI_RETURN_COLUMN,
519 /// Maps directive name --> DirectiveKind enum, for
520 /// directives parsed by this class.
521 StringMap<DirectiveKind> DirectiveKindMap;
523 // Codeview def_range type parsing.
524 enum CVDefRangeType {
525 CVDR_DEFRANGE = 0, // Placeholder
526 CVDR_DEFRANGE_REGISTER,
527 CVDR_DEFRANGE_FRAMEPOINTER_REL,
528 CVDR_DEFRANGE_SUBFIELD_REGISTER,
529 CVDR_DEFRANGE_REGISTER_REL
532 /// Maps Codeview def_range types --> CVDefRangeType enum, for
533 /// Codeview def_range types parsed by this class.
534 StringMap<CVDefRangeType> CVDefRangeTypeMap;
536 // ".ascii", ".asciz", ".string"
537 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
538 bool parseDirectiveReloc(SMLoc DirectiveLoc); // ".reloc"
539 bool parseDirectiveValue(StringRef IDVal,
540 unsigned Size); // ".byte", ".long", ...
541 bool parseDirectiveOctaValue(StringRef IDVal); // ".octa", ...
542 bool parseDirectiveRealValue(StringRef IDVal,
543 const fltSemantics &); // ".single", ...
544 bool parseDirectiveFill(); // ".fill"
545 bool parseDirectiveZero(); // ".zero"
546 // ".set", ".equ", ".equiv"
547 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
548 bool parseDirectiveOrg(); // ".org"
549 // ".align{,32}", ".p2align{,w,l}"
550 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
552 // ".file", ".line", ".loc", ".stabs"
553 bool parseDirectiveFile(SMLoc DirectiveLoc);
554 bool parseDirectiveLine();
555 bool parseDirectiveLoc();
556 bool parseDirectiveStabs();
558 // ".cv_file", ".cv_func_id", ".cv_inline_site_id", ".cv_loc", ".cv_linetable",
559 // ".cv_inline_linetable", ".cv_def_range", ".cv_string"
560 bool parseDirectiveCVFile();
561 bool parseDirectiveCVFuncId();
562 bool parseDirectiveCVInlineSiteId();
563 bool parseDirectiveCVLoc();
564 bool parseDirectiveCVLinetable();
565 bool parseDirectiveCVInlineLinetable();
566 bool parseDirectiveCVDefRange();
567 bool parseDirectiveCVString();
568 bool parseDirectiveCVStringTable();
569 bool parseDirectiveCVFileChecksums();
570 bool parseDirectiveCVFileChecksumOffset();
571 bool parseDirectiveCVFPOData();
574 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
575 bool parseDirectiveCFIWindowSave();
576 bool parseDirectiveCFISections();
577 bool parseDirectiveCFIStartProc();
578 bool parseDirectiveCFIEndProc();
579 bool parseDirectiveCFIDefCfaOffset();
580 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
581 bool parseDirectiveCFIAdjustCfaOffset();
582 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
583 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
584 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
585 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
586 bool parseDirectiveCFIRememberState();
587 bool parseDirectiveCFIRestoreState();
588 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
589 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
590 bool parseDirectiveCFIEscape();
591 bool parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc);
592 bool parseDirectiveCFISignalFrame();
593 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
596 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
597 bool parseDirectiveExitMacro(StringRef Directive);
598 bool parseDirectiveEndMacro(StringRef Directive);
599 bool parseDirectiveMacro(SMLoc DirectiveLoc);
600 bool parseDirectiveMacrosOnOff(StringRef Directive);
601 // alternate macro mode directives
602 bool parseDirectiveAltmacro(StringRef Directive);
603 // ".bundle_align_mode"
604 bool parseDirectiveBundleAlignMode();
606 bool parseDirectiveBundleLock();
608 bool parseDirectiveBundleUnlock();
611 bool parseDirectiveSpace(StringRef IDVal);
614 bool parseDirectiveDCB(StringRef IDVal, unsigned Size);
615 bool parseDirectiveRealDCB(StringRef IDVal, const fltSemantics &);
617 bool parseDirectiveDS(StringRef IDVal, unsigned Size);
619 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
620 bool parseDirectiveLEB128(bool Signed);
622 /// Parse a directive like ".globl" which
623 /// accepts a single symbol (which should be a label or an external).
624 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
626 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
628 bool parseDirectiveAbort(); // ".abort"
629 bool parseDirectiveInclude(); // ".include"
630 bool parseDirectiveIncbin(); // ".incbin"
632 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
633 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
634 // ".ifb" or ".ifnb", depending on ExpectBlank.
635 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
636 // ".ifc" or ".ifnc", depending on ExpectEqual.
637 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
638 // ".ifeqs" or ".ifnes", depending on ExpectEqual.
639 bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual);
640 // ".ifdef" or ".ifndef", depending on expect_defined
641 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
642 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
643 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
644 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
645 bool parseEscapedString(std::string &Data) override;
646 bool parseAngleBracketString(std::string &Data) override;
648 const MCExpr *applyModifierToExpr(const MCExpr *E,
649 MCSymbolRefExpr::VariantKind Variant);
651 // Macro-like directives
652 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
653 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
654 raw_svector_ostream &OS);
655 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
656 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
657 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
658 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
660 // "_emit" or "__emit"
661 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
665 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
668 bool parseDirectiveEnd(SMLoc DirectiveLoc);
670 // ".err" or ".error"
671 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
674 bool parseDirectiveWarning(SMLoc DirectiveLoc);
676 // .print <double-quotes-string>
677 bool parseDirectivePrint(SMLoc DirectiveLoc);
679 // Directives to support address-significance tables.
680 bool parseDirectiveAddrsig();
681 bool parseDirectiveAddrsigSym();
683 void initializeDirectiveKindMap();
684 void initializeCVDefRangeTypeMap();
687 } // end anonymous namespace
691 extern MCAsmParserExtension *createDarwinAsmParser();
692 extern MCAsmParserExtension *createELFAsmParser();
693 extern MCAsmParserExtension *createCOFFAsmParser();
694 extern MCAsmParserExtension *createWasmAsmParser();
696 } // end namespace llvm
698 enum { DEFAULT_ADDRSPACE = 0 };
700 AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
701 const MCAsmInfo &MAI, unsigned CB = 0)
702 : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM),
703 CurBuffer(CB ? CB : SM.getMainFileID()), MacrosEnabledFlag(true) {
705 // Save the old handler.
706 SavedDiagHandler = SrcMgr.getDiagHandler();
707 SavedDiagContext = SrcMgr.getDiagContext();
708 // Set our own handler which calls the saved handler.
709 SrcMgr.setDiagHandler(DiagHandler, this);
710 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
712 // Initialize the platform / file format parser.
713 switch (Ctx.getObjectFileInfo()->getObjectFileType()) {
714 case MCObjectFileInfo::IsCOFF:
715 PlatformParser.reset(createCOFFAsmParser());
717 case MCObjectFileInfo::IsMachO:
718 PlatformParser.reset(createDarwinAsmParser());
721 case MCObjectFileInfo::IsELF:
722 PlatformParser.reset(createELFAsmParser());
724 case MCObjectFileInfo::IsWasm:
725 PlatformParser.reset(createWasmAsmParser());
727 case MCObjectFileInfo::IsXCOFF:
729 "Need to implement createXCOFFAsmParser for XCOFF format.");
733 PlatformParser->Initialize(*this);
734 initializeDirectiveKindMap();
735 initializeCVDefRangeTypeMap();
737 NumOfMacroInstantiations = 0;
740 AsmParser::~AsmParser() {
741 assert((HadError || ActiveMacros.empty()) &&
742 "Unexpected active macro instantiation!");
744 // Restore the saved diagnostics handler and context for use during
746 SrcMgr.setDiagHandler(SavedDiagHandler, SavedDiagContext);
749 void AsmParser::printMacroInstantiations() {
750 // Print the active macro instantiation stack.
751 for (std::vector<MacroInstantiation *>::const_reverse_iterator
752 it = ActiveMacros.rbegin(),
753 ie = ActiveMacros.rend();
755 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
756 "while in macro instantiation");
759 void AsmParser::Note(SMLoc L, const Twine &Msg, SMRange Range) {
760 printPendingErrors();
761 printMessage(L, SourceMgr::DK_Note, Msg, Range);
762 printMacroInstantiations();
765 bool AsmParser::Warning(SMLoc L, const Twine &Msg, SMRange Range) {
766 if(getTargetParser().getTargetOptions().MCNoWarn)
768 if (getTargetParser().getTargetOptions().MCFatalWarnings)
769 return Error(L, Msg, Range);
770 printMessage(L, SourceMgr::DK_Warning, Msg, Range);
771 printMacroInstantiations();
775 bool AsmParser::printError(SMLoc L, const Twine &Msg, SMRange Range) {
777 printMessage(L, SourceMgr::DK_Error, Msg, Range);
778 printMacroInstantiations();
782 bool AsmParser::enterIncludeFile(const std::string &Filename) {
783 std::string IncludedFile;
785 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
790 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
794 /// Process the specified .incbin file by searching for it in the include paths
795 /// then just emitting the byte contents of the file to the streamer. This
796 /// returns true on failure.
797 bool AsmParser::processIncbinFile(const std::string &Filename, int64_t Skip,
798 const MCExpr *Count, SMLoc Loc) {
799 std::string IncludedFile;
801 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
805 // Pick up the bytes from the file and emit them.
806 StringRef Bytes = SrcMgr.getMemoryBuffer(NewBuf)->getBuffer();
807 Bytes = Bytes.drop_front(Skip);
810 if (!Count->evaluateAsAbsolute(Res, getStreamer().getAssemblerPtr()))
811 return Error(Loc, "expected absolute expression");
813 return Warning(Loc, "negative count has no effect");
814 Bytes = Bytes.take_front(Res);
816 getStreamer().emitBytes(Bytes);
820 void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
821 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
822 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
826 const AsmToken &AsmParser::Lex() {
827 if (Lexer.getTok().is(AsmToken::Error))
828 Error(Lexer.getErrLoc(), Lexer.getErr());
830 // if it's a end of statement with a comment in it
831 if (getTok().is(AsmToken::EndOfStatement)) {
832 // if this is a line comment output it.
833 if (!getTok().getString().empty() && getTok().getString().front() != '\n' &&
834 getTok().getString().front() != '\r' && MAI.preserveAsmComments())
835 Out.addExplicitComment(Twine(getTok().getString()));
838 const AsmToken *tok = &Lexer.Lex();
840 // Parse comments here to be deferred until end of next statement.
841 while (tok->is(AsmToken::Comment)) {
842 if (MAI.preserveAsmComments())
843 Out.addExplicitComment(Twine(tok->getString()));
847 if (tok->is(AsmToken::Eof)) {
848 // If this is the end of an included file, pop the parent file off the
850 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
851 if (ParentIncludeLoc != SMLoc()) {
852 jumpToLoc(ParentIncludeLoc);
860 bool AsmParser::enabledGenDwarfForAssembly() {
861 // Check whether the user specified -g.
862 if (!getContext().getGenDwarfForAssembly())
864 // If we haven't encountered any .file directives (which would imply that
865 // the assembler source was produced with debug info already) then emit one
866 // describing the assembler source file itself.
867 if (getContext().getGenDwarfFileNumber() == 0) {
868 // Use the first #line directive for this, if any. It's preprocessed, so
869 // there is no checksum, and of course no source directive.
870 if (!FirstCppHashFilename.empty())
871 getContext().setMCLineTableRootFile(/*CUID=*/0,
872 getContext().getCompilationDir(),
873 FirstCppHashFilename,
874 /*Cksum=*/None, /*Source=*/None);
875 const MCDwarfFile &RootFile =
876 getContext().getMCDwarfLineTable(/*CUID=*/0).getRootFile();
877 getContext().setGenDwarfFileNumber(getStreamer().emitDwarfFileDirective(
878 /*CUID=*/0, getContext().getCompilationDir(), RootFile.Name,
879 RootFile.Checksum, RootFile.Source));
884 bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
885 // Create the initial section, if requested.
886 if (!NoInitialTextSection)
887 Out.InitSections(false);
893 AsmCond StartingCondState = TheCondState;
894 SmallVector<AsmRewrite, 4> AsmStrRewrites;
896 // If we are generating dwarf for assembly source files save the initial text
897 // section. (Don't use enabledGenDwarfForAssembly() here, as we aren't
898 // emitting any actual debug info yet and haven't had a chance to parse any
899 // embedded .file directives.)
900 if (getContext().getGenDwarfForAssembly()) {
901 MCSection *Sec = getStreamer().getCurrentSectionOnly();
902 if (!Sec->getBeginSymbol()) {
903 MCSymbol *SectionStartSym = getContext().createTempSymbol();
904 getStreamer().emitLabel(SectionStartSym);
905 Sec->setBeginSymbol(SectionStartSym);
907 bool InsertResult = getContext().addGenDwarfSection(Sec);
908 assert(InsertResult && ".text section should not have debug info yet");
912 // While we have input, parse each statement.
913 while (Lexer.isNot(AsmToken::Eof)) {
914 ParseStatementInfo Info(&AsmStrRewrites);
915 bool Parsed = parseStatement(Info, nullptr);
917 // If we have a Lexer Error we are on an Error Token. Load in Lexer Error
918 // for printing ErrMsg via Lex() only if no (presumably better) parser error
920 if (Parsed && !hasPendingError() && Lexer.getTok().is(AsmToken::Error)) {
924 // parseStatement returned true so may need to emit an error.
925 printPendingErrors();
927 // Skipping to the next line if needed.
928 if (Parsed && !getLexer().isAtStartOfStatement())
929 eatToEndOfStatement();
932 getTargetParser().onEndOfFile();
933 printPendingErrors();
935 // All errors should have been emitted.
936 assert(!hasPendingError() && "unexpected error from parseStatement");
938 getTargetParser().flushPendingInstructions(getStreamer());
940 if (TheCondState.TheCond != StartingCondState.TheCond ||
941 TheCondState.Ignore != StartingCondState.Ignore)
942 printError(getTok().getLoc(), "unmatched .ifs or .elses");
943 // Check to see there are no empty DwarfFile slots.
944 const auto &LineTables = getContext().getMCDwarfLineTables();
945 if (!LineTables.empty()) {
947 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
948 if (File.Name.empty() && Index != 0)
949 printError(getTok().getLoc(), "unassigned file number: " +
951 " for .file directives");
956 // Check to see that all assembler local symbols were actually defined.
957 // Targets that don't do subsections via symbols may not want this, though,
958 // so conservatively exclude them. Only do this if we're finalizing, though,
959 // as otherwise we won't necessarilly have seen everything yet.
961 if (MAI.hasSubsectionsViaSymbols()) {
962 for (const auto &TableEntry : getContext().getSymbols()) {
963 MCSymbol *Sym = TableEntry.getValue();
964 // Variable symbols may not be marked as defined, so check those
965 // explicitly. If we know it's a variable, we have a definition for
966 // the purposes of this check.
967 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
968 // FIXME: We would really like to refer back to where the symbol was
969 // first referenced for a source location. We need to add something
970 // to track that. Currently, we just point to the end of the file.
971 printError(getTok().getLoc(), "assembler local symbol '" +
972 Sym->getName() + "' not defined");
976 // Temporary symbols like the ones for directional jumps don't go in the
977 // symbol table. They also need to be diagnosed in all (final) cases.
978 for (std::tuple<SMLoc, CppHashInfoTy, MCSymbol *> &LocSym : DirLabels) {
979 if (std::get<2>(LocSym)->isUndefined()) {
980 // Reset the state of any "# line file" directives we've seen to the
981 // context as it was at the diagnostic site.
982 CppHashInfo = std::get<1>(LocSym);
983 printError(std::get<0>(LocSym), "directional label undefined");
988 // Finalize the output stream if there are no errors and if the client wants
990 if (!HadError && !NoFinalize)
993 return HadError || getContext().hadError();
996 bool AsmParser::checkForValidSection() {
997 if (!ParsingMSInlineAsm && !getStreamer().getCurrentSectionOnly()) {
998 Out.InitSections(false);
999 return Error(getTok().getLoc(),
1000 "expected section directive before assembly directive");
1005 /// Throw away the rest of the line for testing purposes.
1006 void AsmParser::eatToEndOfStatement() {
1007 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
1011 if (Lexer.is(AsmToken::EndOfStatement))
1015 StringRef AsmParser::parseStringToEndOfStatement() {
1016 const char *Start = getTok().getLoc().getPointer();
1018 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
1021 const char *End = getTok().getLoc().getPointer();
1022 return StringRef(Start, End - Start);
1025 StringRef AsmParser::parseStringToComma() {
1026 const char *Start = getTok().getLoc().getPointer();
1028 while (Lexer.isNot(AsmToken::EndOfStatement) &&
1029 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
1032 const char *End = getTok().getLoc().getPointer();
1033 return StringRef(Start, End - Start);
1036 /// Parse a paren expression and return it.
1037 /// NOTE: This assumes the leading '(' has already been consumed.
1039 /// parenexpr ::= expr)
1041 bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
1042 if (parseExpression(Res))
1044 if (Lexer.isNot(AsmToken::RParen))
1045 return TokError("expected ')' in parentheses expression");
1046 EndLoc = Lexer.getTok().getEndLoc();
1051 /// Parse a bracket expression and return it.
1052 /// NOTE: This assumes the leading '[' has already been consumed.
1054 /// bracketexpr ::= expr]
1056 bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
1057 if (parseExpression(Res))
1059 EndLoc = getTok().getEndLoc();
1060 if (parseToken(AsmToken::RBrac, "expected ']' in brackets expression"))
1065 /// Parse a primary expression and return it.
1066 /// primaryexpr ::= (parenexpr
1067 /// primaryexpr ::= symbol
1068 /// primaryexpr ::= number
1069 /// primaryexpr ::= '.'
1070 /// primaryexpr ::= ~,+,- primaryexpr
1071 bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
1072 SMLoc FirstTokenLoc = getLexer().getLoc();
1073 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
1074 switch (FirstTokenKind) {
1076 return TokError("unknown token in expression");
1077 // If we have an error assume that we've already handled it.
1078 case AsmToken::Error:
1080 case AsmToken::Exclaim:
1081 Lex(); // Eat the operator.
1082 if (parsePrimaryExpr(Res, EndLoc))
1084 Res = MCUnaryExpr::createLNot(Res, getContext(), FirstTokenLoc);
1086 case AsmToken::Dollar:
1088 case AsmToken::String:
1089 case AsmToken::Identifier: {
1090 StringRef Identifier;
1091 if (parseIdentifier(Identifier)) {
1092 // We may have failed but $ may be a valid token.
1093 if (getTok().is(AsmToken::Dollar)) {
1094 if (Lexer.getMAI().getDollarIsPC()) {
1096 // This is a '$' reference, which references the current PC. Emit a
1097 // temporary label to the streamer and refer to it.
1098 MCSymbol *Sym = Ctx.createTempSymbol();
1100 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None,
1102 EndLoc = FirstTokenLoc;
1105 return Error(FirstTokenLoc, "invalid token in expression");
1108 // Parse symbol variant
1109 std::pair<StringRef, StringRef> Split;
1110 if (!MAI.useParensForSymbolVariant()) {
1111 if (FirstTokenKind == AsmToken::String) {
1112 if (Lexer.is(AsmToken::At)) {
1114 SMLoc AtLoc = getLexer().getLoc();
1116 if (parseIdentifier(VName))
1117 return Error(AtLoc, "expected symbol variant after '@'");
1119 Split = std::make_pair(Identifier, VName);
1122 Split = Identifier.split('@');
1124 } else if (Lexer.is(AsmToken::LParen)) {
1127 parseIdentifier(VName);
1129 if (parseToken(AsmToken::RParen,
1130 "unexpected token in variant, expected ')'"))
1132 Split = std::make_pair(Identifier, VName);
1135 EndLoc = SMLoc::getFromPointer(Identifier.end());
1137 // This is a symbol reference.
1138 StringRef SymbolName = Identifier;
1139 if (SymbolName.empty())
1140 return Error(getLexer().getLoc(), "expected a symbol reference");
1142 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
1144 // Lookup the symbol variant if used.
1145 if (!Split.second.empty()) {
1146 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
1147 if (Variant != MCSymbolRefExpr::VK_Invalid) {
1148 SymbolName = Split.first;
1149 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
1150 Variant = MCSymbolRefExpr::VK_None;
1152 return Error(SMLoc::getFromPointer(Split.second.begin()),
1153 "invalid variant '" + Split.second + "'");
1157 MCSymbol *Sym = getContext().getInlineAsmLabel(SymbolName);
1159 Sym = getContext().getOrCreateSymbol(SymbolName);
1161 // If this is an absolute variable reference, substitute it now to preserve
1162 // semantics in the face of reassignment.
1163 if (Sym->isVariable()) {
1164 auto V = Sym->getVariableValue(/*SetUsed*/ false);
1165 bool DoInline = isa<MCConstantExpr>(V) && !Variant;
1166 if (auto TV = dyn_cast<MCTargetExpr>(V))
1167 DoInline = TV->inlineAssignedExpr();
1170 return Error(EndLoc, "unexpected modifier on variable reference");
1171 Res = Sym->getVariableValue(/*SetUsed*/ false);
1176 // Otherwise create a symbol ref.
1177 Res = MCSymbolRefExpr::create(Sym, Variant, getContext(), FirstTokenLoc);
1180 case AsmToken::BigNum:
1181 return TokError("literal value out of range for directive");
1182 case AsmToken::Integer: {
1183 SMLoc Loc = getTok().getLoc();
1184 int64_t IntVal = getTok().getIntVal();
1185 Res = MCConstantExpr::create(IntVal, getContext());
1186 EndLoc = Lexer.getTok().getEndLoc();
1187 Lex(); // Eat token.
1188 // Look for 'b' or 'f' following an Integer as a directional label
1189 if (Lexer.getKind() == AsmToken::Identifier) {
1190 StringRef IDVal = getTok().getString();
1191 // Lookup the symbol variant if used.
1192 std::pair<StringRef, StringRef> Split = IDVal.split('@');
1193 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
1194 if (Split.first.size() != IDVal.size()) {
1195 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
1196 if (Variant == MCSymbolRefExpr::VK_Invalid)
1197 return TokError("invalid variant '" + Split.second + "'");
1198 IDVal = Split.first;
1200 if (IDVal == "f" || IDVal == "b") {
1202 Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b");
1203 Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
1204 if (IDVal == "b" && Sym->isUndefined())
1205 return Error(Loc, "directional label undefined");
1206 DirLabels.push_back(std::make_tuple(Loc, CppHashInfo, Sym));
1207 EndLoc = Lexer.getTok().getEndLoc();
1208 Lex(); // Eat identifier.
1213 case AsmToken::Real: {
1214 APFloat RealVal(APFloat::IEEEdouble(), getTok().getString());
1215 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
1216 Res = MCConstantExpr::create(IntVal, getContext());
1217 EndLoc = Lexer.getTok().getEndLoc();
1218 Lex(); // Eat token.
1221 case AsmToken::Dot: {
1222 // This is a '.' reference, which references the current PC. Emit a
1223 // temporary label to the streamer and refer to it.
1224 MCSymbol *Sym = Ctx.createTempSymbol();
1226 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
1227 EndLoc = Lexer.getTok().getEndLoc();
1228 Lex(); // Eat identifier.
1231 case AsmToken::LParen:
1232 Lex(); // Eat the '('.
1233 return parseParenExpr(Res, EndLoc);
1234 case AsmToken::LBrac:
1235 if (!PlatformParser->HasBracketExpressions())
1236 return TokError("brackets expression not supported on this target");
1237 Lex(); // Eat the '['.
1238 return parseBracketExpr(Res, EndLoc);
1239 case AsmToken::Minus:
1240 Lex(); // Eat the operator.
1241 if (parsePrimaryExpr(Res, EndLoc))
1243 Res = MCUnaryExpr::createMinus(Res, getContext(), FirstTokenLoc);
1245 case AsmToken::Plus:
1246 Lex(); // Eat the operator.
1247 if (parsePrimaryExpr(Res, EndLoc))
1249 Res = MCUnaryExpr::createPlus(Res, getContext(), FirstTokenLoc);
1251 case AsmToken::Tilde:
1252 Lex(); // Eat the operator.
1253 if (parsePrimaryExpr(Res, EndLoc))
1255 Res = MCUnaryExpr::createNot(Res, getContext(), FirstTokenLoc);
1257 // MIPS unary expression operators. The lexer won't generate these tokens if
1258 // MCAsmInfo::HasMipsExpressions is false for the target.
1259 case AsmToken::PercentCall16:
1260 case AsmToken::PercentCall_Hi:
1261 case AsmToken::PercentCall_Lo:
1262 case AsmToken::PercentDtprel_Hi:
1263 case AsmToken::PercentDtprel_Lo:
1264 case AsmToken::PercentGot:
1265 case AsmToken::PercentGot_Disp:
1266 case AsmToken::PercentGot_Hi:
1267 case AsmToken::PercentGot_Lo:
1268 case AsmToken::PercentGot_Ofst:
1269 case AsmToken::PercentGot_Page:
1270 case AsmToken::PercentGottprel:
1271 case AsmToken::PercentGp_Rel:
1272 case AsmToken::PercentHi:
1273 case AsmToken::PercentHigher:
1274 case AsmToken::PercentHighest:
1275 case AsmToken::PercentLo:
1276 case AsmToken::PercentNeg:
1277 case AsmToken::PercentPcrel_Hi:
1278 case AsmToken::PercentPcrel_Lo:
1279 case AsmToken::PercentTlsgd:
1280 case AsmToken::PercentTlsldm:
1281 case AsmToken::PercentTprel_Hi:
1282 case AsmToken::PercentTprel_Lo:
1283 Lex(); // Eat the operator.
1284 if (Lexer.isNot(AsmToken::LParen))
1285 return TokError("expected '(' after operator");
1286 Lex(); // Eat the operator.
1287 if (parseExpression(Res, EndLoc))
1289 if (Lexer.isNot(AsmToken::RParen))
1290 return TokError("expected ')'");
1291 Lex(); // Eat the operator.
1292 Res = getTargetParser().createTargetUnaryExpr(Res, FirstTokenKind, Ctx);
1297 bool AsmParser::parseExpression(const MCExpr *&Res) {
1299 return parseExpression(Res, EndLoc);
1303 AsmParser::applyModifierToExpr(const MCExpr *E,
1304 MCSymbolRefExpr::VariantKind Variant) {
1305 // Ask the target implementation about this expression first.
1306 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
1309 // Recurse over the given expression, rebuilding it to apply the given variant
1310 // if there is exactly one symbol.
1311 switch (E->getKind()) {
1312 case MCExpr::Target:
1313 case MCExpr::Constant:
1316 case MCExpr::SymbolRef: {
1317 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1319 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
1320 TokError("invalid variant on expression '" + getTok().getIdentifier() +
1321 "' (already modified)");
1325 return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, getContext());
1328 case MCExpr::Unary: {
1329 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
1330 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
1333 return MCUnaryExpr::create(UE->getOpcode(), Sub, getContext());
1336 case MCExpr::Binary: {
1337 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1338 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
1339 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
1349 return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, getContext());
1353 llvm_unreachable("Invalid expression kind!");
1356 /// This function checks if the next token is <string> type or arithmetic.
1357 /// string that begin with character '<' must end with character '>'.
1358 /// otherwise it is arithmetics.
1359 /// If the function returns a 'true' value,
1360 /// the End argument will be filled with the last location pointed to the '>'
1363 /// There is a gap between the AltMacro's documentation and the single quote
1364 /// implementation. GCC does not fully support this feature and so we will not
1366 /// TODO: Adding single quote as a string.
1367 static bool isAngleBracketString(SMLoc &StrLoc, SMLoc &EndLoc) {
1368 assert((StrLoc.getPointer() != nullptr) &&
1369 "Argument to the function cannot be a NULL value");
1370 const char *CharPtr = StrLoc.getPointer();
1371 while ((*CharPtr != '>') && (*CharPtr != '\n') && (*CharPtr != '\r') &&
1372 (*CharPtr != '\0')) {
1373 if (*CharPtr == '!')
1377 if (*CharPtr == '>') {
1378 EndLoc = StrLoc.getFromPointer(CharPtr + 1);
1384 /// creating a string without the escape characters '!'.
1385 static std::string angleBracketString(StringRef AltMacroStr) {
1387 for (size_t Pos = 0; Pos < AltMacroStr.size(); Pos++) {
1388 if (AltMacroStr[Pos] == '!')
1390 Res += AltMacroStr[Pos];
1395 /// Parse an expression and return it.
1397 /// expr ::= expr &&,|| expr -> lowest.
1398 /// expr ::= expr |,^,&,! expr
1399 /// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1400 /// expr ::= expr <<,>> expr
1401 /// expr ::= expr +,- expr
1402 /// expr ::= expr *,/,% expr -> highest.
1403 /// expr ::= primaryexpr
1405 bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
1406 // Parse the expression.
1408 if (getTargetParser().parsePrimaryExpr(Res, EndLoc) ||
1409 parseBinOpRHS(1, Res, EndLoc))
1412 // As a special case, we support 'a op b @ modifier' by rewriting the
1413 // expression to include the modifier. This is inefficient, but in general we
1414 // expect users to use 'a@modifier op b'.
1415 if (Lexer.getKind() == AsmToken::At) {
1418 if (Lexer.isNot(AsmToken::Identifier))
1419 return TokError("unexpected symbol modifier following '@'");
1421 MCSymbolRefExpr::VariantKind Variant =
1422 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
1423 if (Variant == MCSymbolRefExpr::VK_Invalid)
1424 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1426 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
1428 return TokError("invalid modifier '" + getTok().getIdentifier() +
1429 "' (no symbols present)");
1436 // Try to constant fold it up front, if possible. Do not exploit
1439 if (Res->evaluateAsAbsolute(Value))
1440 Res = MCConstantExpr::create(Value, getContext());
1445 bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
1447 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
1450 bool AsmParser::parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
1452 if (parseParenExpr(Res, EndLoc))
1455 for (; ParenDepth > 0; --ParenDepth) {
1456 if (parseBinOpRHS(1, Res, EndLoc))
1459 // We don't Lex() the last RParen.
1460 // This is the same behavior as parseParenExpression().
1461 if (ParenDepth - 1 > 0) {
1462 EndLoc = getTok().getEndLoc();
1463 if (parseToken(AsmToken::RParen,
1464 "expected ')' in parentheses expression"))
1471 bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
1474 SMLoc StartLoc = Lexer.getLoc();
1475 if (parseExpression(Expr))
1478 if (!Expr->evaluateAsAbsolute(Res, getStreamer().getAssemblerPtr()))
1479 return Error(StartLoc, "expected absolute expression");
1484 static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K,
1485 MCBinaryExpr::Opcode &Kind,
1486 bool ShouldUseLogicalShr) {
1489 return 0; // not a binop.
1491 // Lowest Precedence: &&, ||
1492 case AsmToken::AmpAmp:
1493 Kind = MCBinaryExpr::LAnd;
1495 case AsmToken::PipePipe:
1496 Kind = MCBinaryExpr::LOr;
1499 // Low Precedence: |, &, ^
1501 // FIXME: gas seems to support '!' as an infix operator?
1502 case AsmToken::Pipe:
1503 Kind = MCBinaryExpr::Or;
1505 case AsmToken::Caret:
1506 Kind = MCBinaryExpr::Xor;
1509 Kind = MCBinaryExpr::And;
1512 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
1513 case AsmToken::EqualEqual:
1514 Kind = MCBinaryExpr::EQ;
1516 case AsmToken::ExclaimEqual:
1517 case AsmToken::LessGreater:
1518 Kind = MCBinaryExpr::NE;
1520 case AsmToken::Less:
1521 Kind = MCBinaryExpr::LT;
1523 case AsmToken::LessEqual:
1524 Kind = MCBinaryExpr::LTE;
1526 case AsmToken::Greater:
1527 Kind = MCBinaryExpr::GT;
1529 case AsmToken::GreaterEqual:
1530 Kind = MCBinaryExpr::GTE;
1533 // Intermediate Precedence: <<, >>
1534 case AsmToken::LessLess:
1535 Kind = MCBinaryExpr::Shl;
1537 case AsmToken::GreaterGreater:
1538 Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
1541 // High Intermediate Precedence: +, -
1542 case AsmToken::Plus:
1543 Kind = MCBinaryExpr::Add;
1545 case AsmToken::Minus:
1546 Kind = MCBinaryExpr::Sub;
1549 // Highest Precedence: *, /, %
1550 case AsmToken::Star:
1551 Kind = MCBinaryExpr::Mul;
1553 case AsmToken::Slash:
1554 Kind = MCBinaryExpr::Div;
1556 case AsmToken::Percent:
1557 Kind = MCBinaryExpr::Mod;
1562 static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K,
1563 MCBinaryExpr::Opcode &Kind,
1564 bool ShouldUseLogicalShr) {
1567 return 0; // not a binop.
1569 // Lowest Precedence: &&, ||
1570 case AsmToken::AmpAmp:
1571 Kind = MCBinaryExpr::LAnd;
1573 case AsmToken::PipePipe:
1574 Kind = MCBinaryExpr::LOr;
1577 // Low Precedence: ==, !=, <>, <, <=, >, >=
1578 case AsmToken::EqualEqual:
1579 Kind = MCBinaryExpr::EQ;
1581 case AsmToken::ExclaimEqual:
1582 case AsmToken::LessGreater:
1583 Kind = MCBinaryExpr::NE;
1585 case AsmToken::Less:
1586 Kind = MCBinaryExpr::LT;
1588 case AsmToken::LessEqual:
1589 Kind = MCBinaryExpr::LTE;
1591 case AsmToken::Greater:
1592 Kind = MCBinaryExpr::GT;
1594 case AsmToken::GreaterEqual:
1595 Kind = MCBinaryExpr::GTE;
1598 // Low Intermediate Precedence: +, -
1599 case AsmToken::Plus:
1600 Kind = MCBinaryExpr::Add;
1602 case AsmToken::Minus:
1603 Kind = MCBinaryExpr::Sub;
1606 // High Intermediate Precedence: |, &, ^
1608 // FIXME: gas seems to support '!' as an infix operator?
1609 case AsmToken::Pipe:
1610 Kind = MCBinaryExpr::Or;
1612 case AsmToken::Caret:
1613 Kind = MCBinaryExpr::Xor;
1616 Kind = MCBinaryExpr::And;
1619 // Highest Precedence: *, /, %, <<, >>
1620 case AsmToken::Star:
1621 Kind = MCBinaryExpr::Mul;
1623 case AsmToken::Slash:
1624 Kind = MCBinaryExpr::Div;
1626 case AsmToken::Percent:
1627 Kind = MCBinaryExpr::Mod;
1629 case AsmToken::LessLess:
1630 Kind = MCBinaryExpr::Shl;
1632 case AsmToken::GreaterGreater:
1633 Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
1638 unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K,
1639 MCBinaryExpr::Opcode &Kind) {
1640 bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr();
1641 return IsDarwin ? getDarwinBinOpPrecedence(K, Kind, ShouldUseLogicalShr)
1642 : getGNUBinOpPrecedence(K, Kind, ShouldUseLogicalShr);
1645 /// Parse all binary operators with precedence >= 'Precedence'.
1646 /// Res contains the LHS of the expression on input.
1647 bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
1649 SMLoc StartLoc = Lexer.getLoc();
1651 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
1652 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
1654 // If the next token is lower precedence than we are allowed to eat, return
1655 // successfully with what we ate already.
1656 if (TokPrec < Precedence)
1661 // Eat the next primary expression.
1663 if (getTargetParser().parsePrimaryExpr(RHS, EndLoc))
1666 // If BinOp binds less tightly with RHS than the operator after RHS, let
1667 // the pending operator take RHS as its LHS.
1668 MCBinaryExpr::Opcode Dummy;
1669 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
1670 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1673 // Merge LHS and RHS according to operator.
1674 Res = MCBinaryExpr::create(Kind, Res, RHS, getContext(), StartLoc);
1679 /// ::= EndOfStatement
1680 /// ::= Label* Directive ...Operands... EndOfStatement
1681 /// ::= Label* Identifier OperandList* EndOfStatement
1682 bool AsmParser::parseStatement(ParseStatementInfo &Info,
1683 MCAsmParserSemaCallback *SI) {
1684 assert(!hasPendingError() && "parseStatement started with pending error");
1685 // Eat initial spaces and comments
1686 while (Lexer.is(AsmToken::Space))
1688 if (Lexer.is(AsmToken::EndOfStatement)) {
1689 // if this is a line comment we can drop it safely
1690 if (getTok().getString().empty() || getTok().getString().front() == '\r' ||
1691 getTok().getString().front() == '\n')
1696 // Statements always start with an identifier.
1697 AsmToken ID = getTok();
1698 SMLoc IDLoc = ID.getLoc();
1700 int64_t LocalLabelVal = -1;
1701 if (Lexer.is(AsmToken::HashDirective))
1702 return parseCppHashLineFilenameComment(IDLoc,
1703 !isInsideMacroInstantiation());
1705 // Allow an integer followed by a ':' as a directional local label.
1706 if (Lexer.is(AsmToken::Integer)) {
1707 LocalLabelVal = getTok().getIntVal();
1708 if (LocalLabelVal < 0) {
1709 if (!TheCondState.Ignore) {
1710 Lex(); // always eat a token
1711 return Error(IDLoc, "unexpected token at start of statement");
1715 IDVal = getTok().getString();
1716 Lex(); // Consume the integer token to be used as an identifier token.
1717 if (Lexer.getKind() != AsmToken::Colon) {
1718 if (!TheCondState.Ignore) {
1719 Lex(); // always eat a token
1720 return Error(IDLoc, "unexpected token at start of statement");
1724 } else if (Lexer.is(AsmToken::Dot)) {
1725 // Treat '.' as a valid identifier in this context.
1728 } else if (Lexer.is(AsmToken::LCurly)) {
1729 // Treat '{' as a valid identifier in this context.
1733 } else if (Lexer.is(AsmToken::RCurly)) {
1734 // Treat '}' as a valid identifier in this context.
1737 } else if (Lexer.is(AsmToken::Star) &&
1738 getTargetParser().starIsStartOfStatement()) {
1739 // Accept '*' as a valid start of statement.
1742 } else if (parseIdentifier(IDVal)) {
1743 if (!TheCondState.Ignore) {
1744 Lex(); // always eat a token
1745 return Error(IDLoc, "unexpected token at start of statement");
1750 // Handle conditional assembly here before checking for skipping. We
1751 // have to do this so that .endif isn't skipped in a ".if 0" block for
1753 StringMap<DirectiveKind>::const_iterator DirKindIt =
1754 DirectiveKindMap.find(IDVal.lower());
1755 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1758 : DirKindIt->getValue();
1769 return parseDirectiveIf(IDLoc, DirKind);
1771 return parseDirectiveIfb(IDLoc, true);
1773 return parseDirectiveIfb(IDLoc, false);
1775 return parseDirectiveIfc(IDLoc, true);
1777 return parseDirectiveIfeqs(IDLoc, true);
1779 return parseDirectiveIfc(IDLoc, false);
1781 return parseDirectiveIfeqs(IDLoc, false);
1783 return parseDirectiveIfdef(IDLoc, true);
1786 return parseDirectiveIfdef(IDLoc, false);
1788 return parseDirectiveElseIf(IDLoc);
1790 return parseDirectiveElse(IDLoc);
1792 return parseDirectiveEndIf(IDLoc);
1795 // Ignore the statement if in the middle of inactive conditional
1797 if (TheCondState.Ignore) {
1798 eatToEndOfStatement();
1802 // FIXME: Recurse on local labels?
1804 // See what kind of statement we have.
1805 switch (Lexer.getKind()) {
1806 case AsmToken::Colon: {
1807 if (!getTargetParser().isLabel(ID))
1809 if (checkForValidSection())
1812 // identifier ':' -> Label.
1815 // Diagnose attempt to use '.' as a label.
1817 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1819 // Diagnose attempt to use a variable as a label.
1821 // FIXME: Diagnostics. Note the location of the definition as a label.
1822 // FIXME: This doesn't diagnose assignment to a symbol which has been
1823 // implicitly marked as external.
1825 if (LocalLabelVal == -1) {
1826 if (ParsingMSInlineAsm && SI) {
1827 StringRef RewrittenLabel =
1828 SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
1829 assert(!RewrittenLabel.empty() &&
1830 "We should have an internal name here.");
1831 Info.AsmRewrites->emplace_back(AOK_Label, IDLoc, IDVal.size(),
1833 IDVal = RewrittenLabel;
1835 Sym = getContext().getOrCreateSymbol(IDVal);
1837 Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal);
1838 // End of Labels should be treated as end of line for lexing
1839 // purposes but that information is not available to the Lexer who
1840 // does not understand Labels. This may cause us to see a Hash
1841 // here instead of a preprocessor line comment.
1842 if (getTok().is(AsmToken::Hash)) {
1843 StringRef CommentStr = parseStringToEndOfStatement();
1845 Lexer.UnLex(AsmToken(AsmToken::EndOfStatement, CommentStr));
1848 // Consume any end of statement token, if present, to avoid spurious
1849 // AddBlankLine calls().
1850 if (getTok().is(AsmToken::EndOfStatement)) {
1854 getTargetParser().doBeforeLabelEmit(Sym);
1857 if (!getTargetParser().isParsingMSInlineAsm())
1858 Out.emitLabel(Sym, IDLoc);
1860 // If we are generating dwarf for assembly source files then gather the
1861 // info to make a dwarf label entry for this label if needed.
1862 if (enabledGenDwarfForAssembly())
1863 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1866 getTargetParser().onLabelParsed(Sym);
1871 case AsmToken::Equal:
1872 if (!getTargetParser().equalIsAsmAssignment())
1874 // identifier '=' ... -> assignment statement
1877 return parseAssignment(IDVal, true);
1879 default: // Normal instruction or directive.
1883 // If macros are enabled, check to see if this is a macro instantiation.
1884 if (areMacrosEnabled())
1885 if (const MCAsmMacro *M = getContext().lookupMacro(IDVal)) {
1886 return handleMacroEntry(M, IDLoc);
1889 // Otherwise, we have a normal instruction or directive.
1891 // Directives start with "."
1892 if (IDVal.startswith(".") && IDVal != ".") {
1893 // There are several entities interested in parsing directives:
1895 // 1. The target-specific assembly parser. Some directives are target
1896 // specific or may potentially behave differently on certain targets.
1897 // 2. Asm parser extensions. For example, platform-specific parsers
1898 // (like the ELF parser) register themselves as extensions.
1899 // 3. The generic directive parser implemented by this class. These are
1900 // all the directives that behave in a target and platform independent
1901 // manner, or at least have a default behavior that's shared between
1902 // all targets and platforms.
1904 getTargetParser().flushPendingInstructions(getStreamer());
1906 SMLoc StartTokLoc = getTok().getLoc();
1907 bool TPDirectiveReturn = getTargetParser().ParseDirective(ID);
1909 if (hasPendingError())
1911 // Currently the return value should be true if we are
1912 // uninterested but as this is at odds with the standard parsing
1913 // convention (return true = error) we have instances of a parsed
1914 // directive that fails returning true as an error. Catch these
1915 // cases as best as possible errors here.
1916 if (TPDirectiveReturn && StartTokLoc != getTok().getLoc())
1918 // Return if we did some parsing or believe we succeeded.
1919 if (!TPDirectiveReturn || StartTokLoc != getTok().getLoc())
1922 // Next, check the extension directive map to see if any extension has
1923 // registered itself to parse this directive.
1924 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1925 ExtensionDirectiveMap.lookup(IDVal);
1927 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1929 // Finally, if no one else is interested in this directive, it must be
1930 // generic and familiar to this class.
1936 return parseDirectiveSet(IDVal, true);
1938 return parseDirectiveSet(IDVal, false);
1940 return parseDirectiveAscii(IDVal, false);
1943 return parseDirectiveAscii(IDVal, true);
1946 return parseDirectiveValue(IDVal, 1);
1952 return parseDirectiveValue(IDVal, 2);
1957 return parseDirectiveValue(IDVal, 4);
1960 return parseDirectiveValue(IDVal, 8);
1962 return parseDirectiveValue(
1963 IDVal, getContext().getAsmInfo()->getCodePointerSize());
1965 return parseDirectiveOctaValue(IDVal);
1969 return parseDirectiveRealValue(IDVal, APFloat::IEEEsingle());
1972 return parseDirectiveRealValue(IDVal, APFloat::IEEEdouble());
1974 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1975 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1978 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1979 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1982 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1984 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1986 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1988 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1990 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1992 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1994 return parseDirectiveOrg();
1996 return parseDirectiveFill();
1998 return parseDirectiveZero();
2000 eatToEndOfStatement(); // .extern is the default, ignore it.
2004 return parseDirectiveSymbolAttribute(MCSA_Global);
2005 case DK_LAZY_REFERENCE:
2006 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
2007 case DK_NO_DEAD_STRIP:
2008 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
2009 case DK_SYMBOL_RESOLVER:
2010 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
2011 case DK_PRIVATE_EXTERN:
2012 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
2014 return parseDirectiveSymbolAttribute(MCSA_Reference);
2015 case DK_WEAK_DEFINITION:
2016 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
2017 case DK_WEAK_REFERENCE:
2018 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
2019 case DK_WEAK_DEF_CAN_BE_HIDDEN:
2020 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
2022 return parseDirectiveSymbolAttribute(MCSA_Cold);
2025 return parseDirectiveComm(/*IsLocal=*/false);
2027 return parseDirectiveComm(/*IsLocal=*/true);
2029 return parseDirectiveAbort();
2031 return parseDirectiveInclude();
2033 return parseDirectiveIncbin();
2036 return TokError(Twine(IDVal) +
2037 " not currently supported for this target");
2039 return parseDirectiveRept(IDLoc, IDVal);
2041 return parseDirectiveIrp(IDLoc);
2043 return parseDirectiveIrpc(IDLoc);
2045 return parseDirectiveEndr(IDLoc);
2046 case DK_BUNDLE_ALIGN_MODE:
2047 return parseDirectiveBundleAlignMode();
2048 case DK_BUNDLE_LOCK:
2049 return parseDirectiveBundleLock();
2050 case DK_BUNDLE_UNLOCK:
2051 return parseDirectiveBundleUnlock();
2053 return parseDirectiveLEB128(true);
2055 return parseDirectiveLEB128(false);
2058 return parseDirectiveSpace(IDVal);
2060 return parseDirectiveFile(IDLoc);
2062 return parseDirectiveLine();
2064 return parseDirectiveLoc();
2066 return parseDirectiveStabs();
2068 return parseDirectiveCVFile();
2070 return parseDirectiveCVFuncId();
2071 case DK_CV_INLINE_SITE_ID:
2072 return parseDirectiveCVInlineSiteId();
2074 return parseDirectiveCVLoc();
2075 case DK_CV_LINETABLE:
2076 return parseDirectiveCVLinetable();
2077 case DK_CV_INLINE_LINETABLE:
2078 return parseDirectiveCVInlineLinetable();
2079 case DK_CV_DEF_RANGE:
2080 return parseDirectiveCVDefRange();
2082 return parseDirectiveCVString();
2083 case DK_CV_STRINGTABLE:
2084 return parseDirectiveCVStringTable();
2085 case DK_CV_FILECHECKSUMS:
2086 return parseDirectiveCVFileChecksums();
2087 case DK_CV_FILECHECKSUM_OFFSET:
2088 return parseDirectiveCVFileChecksumOffset();
2089 case DK_CV_FPO_DATA:
2090 return parseDirectiveCVFPOData();
2091 case DK_CFI_SECTIONS:
2092 return parseDirectiveCFISections();
2093 case DK_CFI_STARTPROC:
2094 return parseDirectiveCFIStartProc();
2095 case DK_CFI_ENDPROC:
2096 return parseDirectiveCFIEndProc();
2097 case DK_CFI_DEF_CFA:
2098 return parseDirectiveCFIDefCfa(IDLoc);
2099 case DK_CFI_DEF_CFA_OFFSET:
2100 return parseDirectiveCFIDefCfaOffset();
2101 case DK_CFI_ADJUST_CFA_OFFSET:
2102 return parseDirectiveCFIAdjustCfaOffset();
2103 case DK_CFI_DEF_CFA_REGISTER:
2104 return parseDirectiveCFIDefCfaRegister(IDLoc);
2106 return parseDirectiveCFIOffset(IDLoc);
2107 case DK_CFI_REL_OFFSET:
2108 return parseDirectiveCFIRelOffset(IDLoc);
2109 case DK_CFI_PERSONALITY:
2110 return parseDirectiveCFIPersonalityOrLsda(true);
2112 return parseDirectiveCFIPersonalityOrLsda(false);
2113 case DK_CFI_REMEMBER_STATE:
2114 return parseDirectiveCFIRememberState();
2115 case DK_CFI_RESTORE_STATE:
2116 return parseDirectiveCFIRestoreState();
2117 case DK_CFI_SAME_VALUE:
2118 return parseDirectiveCFISameValue(IDLoc);
2119 case DK_CFI_RESTORE:
2120 return parseDirectiveCFIRestore(IDLoc);
2122 return parseDirectiveCFIEscape();
2123 case DK_CFI_RETURN_COLUMN:
2124 return parseDirectiveCFIReturnColumn(IDLoc);
2125 case DK_CFI_SIGNAL_FRAME:
2126 return parseDirectiveCFISignalFrame();
2127 case DK_CFI_UNDEFINED:
2128 return parseDirectiveCFIUndefined(IDLoc);
2129 case DK_CFI_REGISTER:
2130 return parseDirectiveCFIRegister(IDLoc);
2131 case DK_CFI_WINDOW_SAVE:
2132 return parseDirectiveCFIWindowSave();
2135 return parseDirectiveMacrosOnOff(IDVal);
2137 return parseDirectiveMacro(IDLoc);
2140 return parseDirectiveAltmacro(IDVal);
2142 return parseDirectiveExitMacro(IDVal);
2145 return parseDirectiveEndMacro(IDVal);
2147 return parseDirectivePurgeMacro(IDLoc);
2149 return parseDirectiveEnd(IDLoc);
2151 return parseDirectiveError(IDLoc, false);
2153 return parseDirectiveError(IDLoc, true);
2155 return parseDirectiveWarning(IDLoc);
2157 return parseDirectiveReloc(IDLoc);
2160 return parseDirectiveDCB(IDVal, 2);
2162 return parseDirectiveDCB(IDVal, 1);
2164 return parseDirectiveRealDCB(IDVal, APFloat::IEEEdouble());
2166 return parseDirectiveDCB(IDVal, 4);
2168 return parseDirectiveRealDCB(IDVal, APFloat::IEEEsingle());
2171 return TokError(Twine(IDVal) +
2172 " not currently supported for this target");
2175 return parseDirectiveDS(IDVal, 2);
2177 return parseDirectiveDS(IDVal, 1);
2179 return parseDirectiveDS(IDVal, 8);
2182 return parseDirectiveDS(IDVal, 4);
2185 return parseDirectiveDS(IDVal, 12);
2187 return parseDirectivePrint(IDLoc);
2189 return parseDirectiveAddrsig();
2190 case DK_ADDRSIG_SYM:
2191 return parseDirectiveAddrsigSym();
2194 return Error(IDLoc, "unknown directive");
2197 // __asm _emit or __asm __emit
2198 if (ParsingMSInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
2199 IDVal == "_EMIT" || IDVal == "__EMIT"))
2200 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
2203 if (ParsingMSInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
2204 return parseDirectiveMSAlign(IDLoc, Info);
2206 if (ParsingMSInlineAsm && (IDVal == "even" || IDVal == "EVEN"))
2207 Info.AsmRewrites->emplace_back(AOK_EVEN, IDLoc, 4);
2208 if (checkForValidSection())
2211 // Canonicalize the opcode to lower case.
2212 std::string OpcodeStr = IDVal.lower();
2213 ParseInstructionInfo IInfo(Info.AsmRewrites);
2214 bool ParseHadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, ID,
2215 Info.ParsedOperands);
2216 Info.ParseError = ParseHadError;
2218 // Dump the parsed representation, if requested.
2219 if (getShowParsedOperands()) {
2220 SmallString<256> Str;
2221 raw_svector_ostream OS(Str);
2222 OS << "parsed instruction: [";
2223 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
2226 Info.ParsedOperands[i]->print(OS);
2230 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
2233 // Fail even if ParseInstruction erroneously returns false.
2234 if (hasPendingError() || ParseHadError)
2237 // If we are generating dwarf for the current section then generate a .loc
2238 // directive for the instruction.
2239 if (!ParseHadError && enabledGenDwarfForAssembly() &&
2240 getContext().getGenDwarfSectionSyms().count(
2241 getStreamer().getCurrentSectionOnly())) {
2243 if (ActiveMacros.empty())
2244 Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
2246 Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc,
2247 ActiveMacros.front()->ExitBuffer);
2249 // If we previously parsed a cpp hash file line comment then make sure the
2250 // current Dwarf File is for the CppHashFilename if not then emit the
2251 // Dwarf File table for it and adjust the line number for the .loc.
2252 if (!CppHashInfo.Filename.empty()) {
2253 unsigned FileNumber = getStreamer().emitDwarfFileDirective(
2254 0, StringRef(), CppHashInfo.Filename);
2255 getContext().setGenDwarfFileNumber(FileNumber);
2257 unsigned CppHashLocLineNo =
2258 SrcMgr.FindLineNumber(CppHashInfo.Loc, CppHashInfo.Buf);
2259 Line = CppHashInfo.LineNumber - 1 + (Line - CppHashLocLineNo);
2262 getStreamer().emitDwarfLocDirective(
2263 getContext().getGenDwarfFileNumber(), Line, 0,
2264 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
2268 // If parsing succeeded, match the instruction.
2269 if (!ParseHadError) {
2271 if (getTargetParser().MatchAndEmitInstruction(
2272 IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo,
2273 getTargetParser().isParsingMSInlineAsm()))
2279 // Parse and erase curly braces marking block start/end
2281 AsmParser::parseCurlyBlockScope(SmallVectorImpl<AsmRewrite> &AsmStrRewrites) {
2282 // Identify curly brace marking block start/end
2283 if (Lexer.isNot(AsmToken::LCurly) && Lexer.isNot(AsmToken::RCurly))
2286 SMLoc StartLoc = Lexer.getLoc();
2287 Lex(); // Eat the brace
2288 if (Lexer.is(AsmToken::EndOfStatement))
2289 Lex(); // Eat EndOfStatement following the brace
2291 // Erase the block start/end brace from the output asm string
2292 AsmStrRewrites.emplace_back(AOK_Skip, StartLoc, Lexer.getLoc().getPointer() -
2293 StartLoc.getPointer());
2297 /// parseCppHashLineFilenameComment as this:
2298 /// ::= # number "filename"
2299 bool AsmParser::parseCppHashLineFilenameComment(SMLoc L, bool SaveLocInfo) {
2300 Lex(); // Eat the hash token.
2301 // Lexer only ever emits HashDirective if it fully formed if it's
2302 // done the checking already so this is an internal error.
2303 assert(getTok().is(AsmToken::Integer) &&
2304 "Lexing Cpp line comment: Expected Integer");
2305 int64_t LineNumber = getTok().getIntVal();
2307 assert(getTok().is(AsmToken::String) &&
2308 "Lexing Cpp line comment: Expected String");
2309 StringRef Filename = getTok().getString();
2315 // Get rid of the enclosing quotes.
2316 Filename = Filename.substr(1, Filename.size() - 2);
2318 // Save the SMLoc, Filename and LineNumber for later use by diagnostics
2319 // and possibly DWARF file info.
2320 CppHashInfo.Loc = L;
2321 CppHashInfo.Filename = Filename;
2322 CppHashInfo.LineNumber = LineNumber;
2323 CppHashInfo.Buf = CurBuffer;
2324 if (FirstCppHashFilename.empty())
2325 FirstCppHashFilename = Filename;
2329 /// will use the last parsed cpp hash line filename comment
2330 /// for the Filename and LineNo if any in the diagnostic.
2331 void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
2332 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
2333 raw_ostream &OS = errs();
2335 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
2336 SMLoc DiagLoc = Diag.getLoc();
2337 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
2338 unsigned CppHashBuf =
2339 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashInfo.Loc);
2341 // Like SourceMgr::printMessage() we need to print the include stack if any
2342 // before printing the message.
2343 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
2344 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
2345 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
2346 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
2347 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
2350 // If we have not parsed a cpp hash line filename comment or the source
2351 // manager changed or buffer changed (like in a nested include) then just
2352 // print the normal diagnostic using its Filename and LineNo.
2353 if (!Parser->CppHashInfo.LineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
2354 DiagBuf != CppHashBuf) {
2355 if (Parser->SavedDiagHandler)
2356 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
2358 Diag.print(nullptr, OS);
2362 // Use the CppHashFilename and calculate a line number based on the
2363 // CppHashInfo.Loc and CppHashInfo.LineNumber relative to this Diag's SMLoc
2364 // for the diagnostic.
2365 const std::string &Filename = std::string(Parser->CppHashInfo.Filename);
2367 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
2368 int CppHashLocLineNo =
2369 Parser->SrcMgr.FindLineNumber(Parser->CppHashInfo.Loc, CppHashBuf);
2371 Parser->CppHashInfo.LineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
2373 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
2374 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
2375 Diag.getLineContents(), Diag.getRanges());
2377 if (Parser->SavedDiagHandler)
2378 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
2380 NewDiag.print(nullptr, OS);
2383 // FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
2384 // difference being that that function accepts '@' as part of identifiers and
2385 // we can't do that. AsmLexer.cpp should probably be changed to handle
2386 // '@' as a special case when needed.
2387 static bool isIdentifierChar(char c) {
2388 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
2392 bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
2393 ArrayRef<MCAsmMacroParameter> Parameters,
2394 ArrayRef<MCAsmMacroArgument> A,
2395 bool EnableAtPseudoVariable, SMLoc L) {
2396 unsigned NParameters = Parameters.size();
2397 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
2398 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
2399 return Error(L, "Wrong number of arguments");
2401 // A macro without parameters is handled differently on Darwin:
2402 // gas accepts no arguments and does no substitutions
2403 while (!Body.empty()) {
2404 // Scan for the next substitution.
2405 std::size_t End = Body.size(), Pos = 0;
2406 for (; Pos != End; ++Pos) {
2407 // Check for a substitution or escape.
2408 if (IsDarwin && !NParameters) {
2409 // This macro has no parameters, look for $0, $1, etc.
2410 if (Body[Pos] != '$' || Pos + 1 == End)
2413 char Next = Body[Pos + 1];
2414 if (Next == '$' || Next == 'n' ||
2415 isdigit(static_cast<unsigned char>(Next)))
2418 // This macro has parameters, look for \foo, \bar, etc.
2419 if (Body[Pos] == '\\' && Pos + 1 != End)
2425 OS << Body.slice(0, Pos);
2427 // Check if we reached the end.
2431 if (IsDarwin && !NParameters) {
2432 switch (Body[Pos + 1]) {
2438 // $n => number of arguments
2443 // $[0-9] => argument
2445 // Missing arguments are ignored.
2446 unsigned Index = Body[Pos + 1] - '0';
2447 if (Index >= A.size())
2450 // Otherwise substitute with the token values, with spaces eliminated.
2451 for (const AsmToken &Token : A[Index])
2452 OS << Token.getString();
2458 unsigned I = Pos + 1;
2460 // Check for the \@ pseudo-variable.
2461 if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End)
2464 while (isIdentifierChar(Body[I]) && I + 1 != End)
2467 const char *Begin = Body.data() + Pos + 1;
2468 StringRef Argument(Begin, I - (Pos + 1));
2471 if (Argument == "@") {
2472 OS << NumOfMacroInstantiations;
2475 for (; Index < NParameters; ++Index)
2476 if (Parameters[Index].Name == Argument)
2479 if (Index == NParameters) {
2480 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
2483 OS << '\\' << Argument;
2487 bool VarargParameter = HasVararg && Index == (NParameters - 1);
2488 for (const AsmToken &Token : A[Index])
2489 // For altmacro mode, you can write '%expr'.
2490 // The prefix '%' evaluates the expression 'expr'
2491 // and uses the result as a string (e.g. replace %(1+2) with the
2493 // Here, we identify the integer token which is the result of the
2494 // absolute expression evaluation and replace it with its string
2496 if (AltMacroMode && Token.getString().front() == '%' &&
2497 Token.is(AsmToken::Integer))
2498 // Emit an integer value to the buffer.
2499 OS << Token.getIntVal();
2500 // Only Token that was validated as a string and begins with '<'
2501 // is considered altMacroString!!!
2502 else if (AltMacroMode && Token.getString().front() == '<' &&
2503 Token.is(AsmToken::String)) {
2504 OS << angleBracketString(Token.getStringContents());
2506 // We expect no quotes around the string's contents when
2507 // parsing for varargs.
2508 else if (Token.isNot(AsmToken::String) || VarargParameter)
2509 OS << Token.getString();
2511 OS << Token.getStringContents();
2513 Pos += 1 + Argument.size();
2517 // Update the scan point.
2518 Body = Body.substr(Pos);
2524 static bool isOperator(AsmToken::TokenKind kind) {
2528 case AsmToken::Plus:
2529 case AsmToken::Minus:
2530 case AsmToken::Tilde:
2531 case AsmToken::Slash:
2532 case AsmToken::Star:
2534 case AsmToken::Equal:
2535 case AsmToken::EqualEqual:
2536 case AsmToken::Pipe:
2537 case AsmToken::PipePipe:
2538 case AsmToken::Caret:
2540 case AsmToken::AmpAmp:
2541 case AsmToken::Exclaim:
2542 case AsmToken::ExclaimEqual:
2543 case AsmToken::Less:
2544 case AsmToken::LessEqual:
2545 case AsmToken::LessLess:
2546 case AsmToken::LessGreater:
2547 case AsmToken::Greater:
2548 case AsmToken::GreaterEqual:
2549 case AsmToken::GreaterGreater:
2556 class AsmLexerSkipSpaceRAII {
2558 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
2559 Lexer.setSkipSpace(SkipSpace);
2562 ~AsmLexerSkipSpaceRAII() {
2563 Lexer.setSkipSpace(true);
2570 } // end anonymous namespace
2572 bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
2575 if (Lexer.isNot(AsmToken::EndOfStatement)) {
2576 StringRef Str = parseStringToEndOfStatement();
2577 MA.emplace_back(AsmToken::String, Str);
2582 unsigned ParenLevel = 0;
2584 // Darwin doesn't use spaces to delmit arguments.
2585 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
2591 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
2592 return TokError("unexpected token in macro instantiation");
2594 if (ParenLevel == 0) {
2596 if (Lexer.is(AsmToken::Comma))
2599 if (Lexer.is(AsmToken::Space)) {
2601 Lexer.Lex(); // Eat spaces
2604 // Spaces can delimit parameters, but could also be part an expression.
2605 // If the token after a space is an operator, add the token and the next
2606 // one into this argument
2608 if (isOperator(Lexer.getKind())) {
2609 MA.push_back(getTok());
2612 // Whitespace after an operator can be ignored.
2613 if (Lexer.is(AsmToken::Space))
2623 // handleMacroEntry relies on not advancing the lexer here
2624 // to be able to fill in the remaining default parameter values
2625 if (Lexer.is(AsmToken::EndOfStatement))
2628 // Adjust the current parentheses level.
2629 if (Lexer.is(AsmToken::LParen))
2631 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
2634 // Append the token to the current argument list.
2635 MA.push_back(getTok());
2639 if (ParenLevel != 0)
2640 return TokError("unbalanced parentheses in macro argument");
2644 // Parse the macro instantiation arguments.
2645 bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
2646 MCAsmMacroArguments &A) {
2647 const unsigned NParameters = M ? M->Parameters.size() : 0;
2648 bool NamedParametersFound = false;
2649 SmallVector<SMLoc, 4> FALocs;
2651 A.resize(NParameters);
2652 FALocs.resize(NParameters);
2654 // Parse two kinds of macro invocations:
2655 // - macros defined without any parameters accept an arbitrary number of them
2656 // - macros defined with parameters accept at most that many of them
2657 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
2658 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
2660 SMLoc IDLoc = Lexer.getLoc();
2661 MCAsmMacroParameter FA;
2663 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
2664 if (parseIdentifier(FA.Name))
2665 return Error(IDLoc, "invalid argument identifier for formal argument");
2667 if (Lexer.isNot(AsmToken::Equal))
2668 return TokError("expected '=' after formal parameter identifier");
2672 NamedParametersFound = true;
2674 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2676 if (NamedParametersFound && FA.Name.empty())
2677 return Error(IDLoc, "cannot mix positional and keyword arguments");
2679 SMLoc StrLoc = Lexer.getLoc();
2681 if (AltMacroMode && Lexer.is(AsmToken::Percent)) {
2682 const MCExpr *AbsoluteExp;
2686 if (parseExpression(AbsoluteExp, EndLoc))
2688 if (!AbsoluteExp->evaluateAsAbsolute(Value,
2689 getStreamer().getAssemblerPtr()))
2690 return Error(StrLoc, "expected absolute expression");
2691 const char *StrChar = StrLoc.getPointer();
2692 const char *EndChar = EndLoc.getPointer();
2693 AsmToken newToken(AsmToken::Integer,
2694 StringRef(StrChar, EndChar - StrChar), Value);
2695 FA.Value.push_back(newToken);
2696 } else if (AltMacroMode && Lexer.is(AsmToken::Less) &&
2697 isAngleBracketString(StrLoc, EndLoc)) {
2698 const char *StrChar = StrLoc.getPointer();
2699 const char *EndChar = EndLoc.getPointer();
2700 jumpToLoc(EndLoc, CurBuffer);
2701 /// Eat from '<' to '>'
2703 AsmToken newToken(AsmToken::String,
2704 StringRef(StrChar, EndChar - StrChar));
2705 FA.Value.push_back(newToken);
2706 } else if(parseMacroArgument(FA.Value, Vararg))
2709 unsigned PI = Parameter;
2710 if (!FA.Name.empty()) {
2712 for (FAI = 0; FAI < NParameters; ++FAI)
2713 if (M->Parameters[FAI].Name == FA.Name)
2716 if (FAI >= NParameters) {
2717 assert(M && "expected macro to be defined");
2718 return Error(IDLoc, "parameter named '" + FA.Name +
2719 "' does not exist for macro '" + M->Name + "'");
2724 if (!FA.Value.empty()) {
2729 if (FALocs.size() <= PI)
2730 FALocs.resize(PI + 1);
2732 FALocs[PI] = Lexer.getLoc();
2735 // At the end of the statement, fill in remaining arguments that have
2736 // default values. If there aren't any, then the next argument is
2737 // required but missing
2738 if (Lexer.is(AsmToken::EndOfStatement)) {
2739 bool Failure = false;
2740 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2741 if (A[FAI].empty()) {
2742 if (M->Parameters[FAI].Required) {
2743 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2744 "missing value for required parameter "
2745 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2749 if (!M->Parameters[FAI].Value.empty())
2750 A[FAI] = M->Parameters[FAI].Value;
2756 if (Lexer.is(AsmToken::Comma))
2760 return TokError("too many positional arguments");
2763 bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
2764 // Arbitrarily limit macro nesting depth (default matches 'as'). We can
2765 // eliminate this, although we should protect against infinite loops.
2766 unsigned MaxNestingDepth = AsmMacroMaxNestingDepth;
2767 if (ActiveMacros.size() == MaxNestingDepth) {
2768 std::ostringstream MaxNestingDepthError;
2769 MaxNestingDepthError << "macros cannot be nested more than "
2770 << MaxNestingDepth << " levels deep."
2771 << " Use -asm-macro-max-nesting-depth to increase "
2773 return TokError(MaxNestingDepthError.str());
2776 MCAsmMacroArguments A;
2777 if (parseMacroArguments(M, A))
2780 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2781 // to hold the macro body with substitutions.
2782 SmallString<256> Buf;
2783 StringRef Body = M->Body;
2784 raw_svector_ostream OS(Buf);
2786 if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc()))
2789 // We include the .endmacro in the buffer as our cue to exit the macro
2791 OS << ".endmacro\n";
2793 std::unique_ptr<MemoryBuffer> Instantiation =
2794 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
2796 // Create the macro instantiation object and add to the current macro
2797 // instantiation stack.
2798 MacroInstantiation *MI = new MacroInstantiation{
2799 NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()};
2800 ActiveMacros.push_back(MI);
2802 ++NumOfMacroInstantiations;
2804 // Jump to the macro instantiation and prime the lexer.
2805 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
2806 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
2812 void AsmParser::handleMacroExit() {
2813 // Jump to the EndOfStatement we should return to, and consume it.
2814 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
2817 // Pop the instantiation entry.
2818 delete ActiveMacros.back();
2819 ActiveMacros.pop_back();
2822 bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
2825 const MCExpr *Value;
2826 if (MCParserUtils::parseAssignmentExpression(Name, allow_redef, *this, Sym,
2831 // In the case where we parse an expression starting with a '.', we will
2832 // not generate an error, nor will we create a symbol. In this case we
2833 // should just return out.
2837 // Do the assignment.
2838 Out.emitAssignment(Sym, Value);
2840 Out.emitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2845 /// parseIdentifier:
2848 bool AsmParser::parseIdentifier(StringRef &Res) {
2849 // The assembler has relaxed rules for accepting identifiers, in particular we
2850 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2851 // separate tokens. At this level, we have already lexed so we cannot (currently)
2852 // handle this as a context dependent token, instead we detect adjacent tokens
2853 // and return the combined identifier.
2854 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2855 SMLoc PrefixLoc = getLexer().getLoc();
2857 // Consume the prefix character, and check for a following identifier.
2860 Lexer.peekTokens(Buf, false);
2862 if (Buf[0].isNot(AsmToken::Identifier) && Buf[0].isNot(AsmToken::Integer))
2865 // We have a '$' or '@' followed by an identifier or integer token, make
2866 // sure they are adjacent.
2867 if (PrefixLoc.getPointer() + 1 != Buf[0].getLoc().getPointer())
2871 Lexer.Lex(); // Lexer's Lex guarantees consecutive token.
2872 // Construct the joined identifier and consume the token.
2873 Res = StringRef(PrefixLoc.getPointer(), getTok().getString().size() + 1);
2874 Lex(); // Parser Lex to maintain invariants.
2878 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
2881 Res = getTok().getIdentifier();
2883 Lex(); // Consume the identifier token.
2888 /// parseDirectiveSet:
2889 /// ::= .equ identifier ',' expression
2890 /// ::= .equiv identifier ',' expression
2891 /// ::= .set identifier ',' expression
2892 bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
2894 if (check(parseIdentifier(Name), "expected identifier") ||
2895 parseToken(AsmToken::Comma) || parseAssignment(Name, allow_redef, true))
2896 return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
2900 bool AsmParser::parseEscapedString(std::string &Data) {
2901 if (check(getTok().isNot(AsmToken::String), "expected string"))
2905 StringRef Str = getTok().getStringContents();
2906 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2907 if (Str[i] != '\\') {
2912 // Recognize escaped characters. Note that this escape semantics currently
2913 // loosely follows Darwin 'as'.
2916 return TokError("unexpected backslash at end of string");
2918 // Recognize hex sequences similarly to GNU 'as'.
2919 if (Str[i] == 'x' || Str[i] == 'X') {
2920 size_t length = Str.size();
2921 if (i + 1 >= length || !isHexDigit(Str[i + 1]))
2922 return TokError("invalid hexadecimal escape sequence");
2924 // Consume hex characters. GNU 'as' reads all hexadecimal characters and
2925 // then truncates to the lower 16 bits. Seems reasonable.
2927 while (i + 1 < length && isHexDigit(Str[i + 1]))
2928 Value = Value * 16 + hexDigitValue(Str[++i]);
2930 Data += (unsigned char)(Value & 0xFF);
2934 // Recognize octal sequences.
2935 if ((unsigned)(Str[i] - '0') <= 7) {
2936 // Consume up to three octal characters.
2937 unsigned Value = Str[i] - '0';
2939 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
2941 Value = Value * 8 + (Str[i] - '0');
2943 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
2945 Value = Value * 8 + (Str[i] - '0');
2950 return TokError("invalid octal escape sequence (out of range)");
2952 Data += (unsigned char)Value;
2956 // Otherwise recognize individual escapes.
2959 // Just reject invalid escape sequences for now.
2960 return TokError("invalid escape sequence (unrecognized character)");
2962 case 'b': Data += '\b'; break;
2963 case 'f': Data += '\f'; break;
2964 case 'n': Data += '\n'; break;
2965 case 'r': Data += '\r'; break;
2966 case 't': Data += '\t'; break;
2967 case '"': Data += '"'; break;
2968 case '\\': Data += '\\'; break;
2976 bool AsmParser::parseAngleBracketString(std::string &Data) {
2977 SMLoc EndLoc, StartLoc = getTok().getLoc();
2978 if (isAngleBracketString(StartLoc, EndLoc)) {
2979 const char *StartChar = StartLoc.getPointer() + 1;
2980 const char *EndChar = EndLoc.getPointer() - 1;
2981 jumpToLoc(EndLoc, CurBuffer);
2982 /// Eat from '<' to '>'
2985 Data = angleBracketString(StringRef(StartChar, EndChar - StartChar));
2991 /// parseDirectiveAscii:
2992 /// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
2993 bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
2994 auto parseOp = [&]() -> bool {
2996 if (checkForValidSection() || parseEscapedString(Data))
2998 getStreamer().emitBytes(Data);
3000 getStreamer().emitBytes(StringRef("\0", 1));
3004 if (parseMany(parseOp))
3005 return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
3009 /// parseDirectiveReloc
3010 /// ::= .reloc expression , identifier [ , expression ]
3011 bool AsmParser::parseDirectiveReloc(SMLoc DirectiveLoc) {
3012 const MCExpr *Offset;
3013 const MCExpr *Expr = nullptr;
3014 SMLoc OffsetLoc = Lexer.getTok().getLoc();
3016 if (parseExpression(Offset))
3018 if (parseToken(AsmToken::Comma, "expected comma") ||
3019 check(getTok().isNot(AsmToken::Identifier), "expected relocation name"))
3022 SMLoc NameLoc = Lexer.getTok().getLoc();
3023 StringRef Name = Lexer.getTok().getIdentifier();
3026 if (Lexer.is(AsmToken::Comma)) {
3028 SMLoc ExprLoc = Lexer.getLoc();
3029 if (parseExpression(Expr))
3033 if (!Expr->evaluateAsRelocatable(Value, nullptr, nullptr))
3034 return Error(ExprLoc, "expression must be relocatable");
3037 if (parseToken(AsmToken::EndOfStatement,
3038 "unexpected token in .reloc directive"))
3041 const MCTargetAsmParser &MCT = getTargetParser();
3042 const MCSubtargetInfo &STI = MCT.getSTI();
3043 if (Optional<std::pair<bool, std::string>> Err =
3044 getStreamer().emitRelocDirective(*Offset, Name, Expr, DirectiveLoc,
3046 return Error(Err->first ? NameLoc : OffsetLoc, Err->second);
3051 /// parseDirectiveValue
3052 /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
3053 bool AsmParser::parseDirectiveValue(StringRef IDVal, unsigned Size) {
3054 auto parseOp = [&]() -> bool {
3055 const MCExpr *Value;
3056 SMLoc ExprLoc = getLexer().getLoc();
3057 if (checkForValidSection() || parseExpression(Value))
3059 // Special case constant expressions to match code generator.
3060 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3061 assert(Size <= 8 && "Invalid size");
3062 uint64_t IntValue = MCE->getValue();
3063 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
3064 return Error(ExprLoc, "out of range literal value");
3065 getStreamer().emitIntValue(IntValue, Size);
3067 getStreamer().emitValue(Value, Size, ExprLoc);
3071 if (parseMany(parseOp))
3072 return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
3076 static bool parseHexOcta(AsmParser &Asm, uint64_t &hi, uint64_t &lo) {
3077 if (Asm.getTok().isNot(AsmToken::Integer) &&
3078 Asm.getTok().isNot(AsmToken::BigNum))
3079 return Asm.TokError("unknown token in expression");
3080 SMLoc ExprLoc = Asm.getTok().getLoc();
3081 APInt IntValue = Asm.getTok().getAPIntVal();
3083 if (!IntValue.isIntN(128))
3084 return Asm.Error(ExprLoc, "out of range literal value");
3085 if (!IntValue.isIntN(64)) {
3086 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
3087 lo = IntValue.getLoBits(64).getZExtValue();
3090 lo = IntValue.getZExtValue();
3095 /// ParseDirectiveOctaValue
3096 /// ::= .octa [ hexconstant (, hexconstant)* ]
3098 bool AsmParser::parseDirectiveOctaValue(StringRef IDVal) {
3099 auto parseOp = [&]() -> bool {
3100 if (checkForValidSection())
3103 if (parseHexOcta(*this, hi, lo))
3105 if (MAI.isLittleEndian()) {
3106 getStreamer().emitInt64(lo);
3107 getStreamer().emitInt64(hi);
3109 getStreamer().emitInt64(hi);
3110 getStreamer().emitInt64(lo);
3115 if (parseMany(parseOp))
3116 return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
3120 bool AsmParser::parseRealValue(const fltSemantics &Semantics, APInt &Res) {
3121 // We don't truly support arithmetic on floating point expressions, so we
3122 // have to manually parse unary prefixes.
3124 if (getLexer().is(AsmToken::Minus)) {
3127 } else if (getLexer().is(AsmToken::Plus))
3130 if (Lexer.is(AsmToken::Error))
3131 return TokError(Lexer.getErr());
3132 if (Lexer.isNot(AsmToken::Integer) && Lexer.isNot(AsmToken::Real) &&
3133 Lexer.isNot(AsmToken::Identifier))
3134 return TokError("unexpected token in directive");
3136 // Convert to an APFloat.
3137 APFloat Value(Semantics);
3138 StringRef IDVal = getTok().getString();
3139 if (getLexer().is(AsmToken::Identifier)) {
3140 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
3141 Value = APFloat::getInf(Semantics);
3142 else if (!IDVal.compare_lower("nan"))
3143 Value = APFloat::getNaN(Semantics, false, ~0);
3145 return TokError("invalid floating point literal");
3146 } else if (errorToBool(
3147 Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven)
3149 return TokError("invalid floating point literal");
3153 // Consume the numeric token.
3156 Res = Value.bitcastToAPInt();
3161 /// parseDirectiveRealValue
3162 /// ::= (.single | .double) [ expression (, expression)* ]
3163 bool AsmParser::parseDirectiveRealValue(StringRef IDVal,
3164 const fltSemantics &Semantics) {
3165 auto parseOp = [&]() -> bool {
3167 if (checkForValidSection() || parseRealValue(Semantics, AsInt))
3169 getStreamer().emitIntValue(AsInt.getLimitedValue(),
3170 AsInt.getBitWidth() / 8);
3174 if (parseMany(parseOp))
3175 return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
3179 /// parseDirectiveZero
3180 /// ::= .zero expression
3181 bool AsmParser::parseDirectiveZero() {
3182 SMLoc NumBytesLoc = Lexer.getLoc();
3183 const MCExpr *NumBytes;
3184 if (checkForValidSection() || parseExpression(NumBytes))
3188 if (getLexer().is(AsmToken::Comma)) {
3190 if (parseAbsoluteExpression(Val))
3194 if (parseToken(AsmToken::EndOfStatement,
3195 "unexpected token in '.zero' directive"))
3197 getStreamer().emitFill(*NumBytes, Val, NumBytesLoc);
3202 /// parseDirectiveFill
3203 /// ::= .fill expression [ , expression [ , expression ] ]
3204 bool AsmParser::parseDirectiveFill() {
3205 SMLoc NumValuesLoc = Lexer.getLoc();
3206 const MCExpr *NumValues;
3207 if (checkForValidSection() || parseExpression(NumValues))
3210 int64_t FillSize = 1;
3211 int64_t FillExpr = 0;
3213 SMLoc SizeLoc, ExprLoc;
3215 if (parseOptionalToken(AsmToken::Comma)) {
3216 SizeLoc = getTok().getLoc();
3217 if (parseAbsoluteExpression(FillSize))
3219 if (parseOptionalToken(AsmToken::Comma)) {
3220 ExprLoc = getTok().getLoc();
3221 if (parseAbsoluteExpression(FillExpr))
3225 if (parseToken(AsmToken::EndOfStatement,
3226 "unexpected token in '.fill' directive"))
3230 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
3234 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
3238 if (!isUInt<32>(FillExpr) && FillSize > 4)
3239 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
3241 getStreamer().emitFill(*NumValues, FillSize, FillExpr, NumValuesLoc);
3246 /// parseDirectiveOrg
3247 /// ::= .org expression [ , expression ]
3248 bool AsmParser::parseDirectiveOrg() {
3249 const MCExpr *Offset;
3250 SMLoc OffsetLoc = Lexer.getLoc();
3251 if (checkForValidSection() || parseExpression(Offset))
3254 // Parse optional fill expression.
3255 int64_t FillExpr = 0;
3256 if (parseOptionalToken(AsmToken::Comma))
3257 if (parseAbsoluteExpression(FillExpr))
3258 return addErrorSuffix(" in '.org' directive");
3259 if (parseToken(AsmToken::EndOfStatement))
3260 return addErrorSuffix(" in '.org' directive");
3262 getStreamer().emitValueToOffset(Offset, FillExpr, OffsetLoc);
3266 /// parseDirectiveAlign
3267 /// ::= {.align, ...} expression [ , expression [ , expression ]]
3268 bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
3269 SMLoc AlignmentLoc = getLexer().getLoc();
3272 bool HasFillExpr = false;
3273 int64_t FillExpr = 0;
3274 int64_t MaxBytesToFill = 0;
3276 auto parseAlign = [&]() -> bool {
3277 if (parseAbsoluteExpression(Alignment))
3279 if (parseOptionalToken(AsmToken::Comma)) {
3280 // The fill expression can be omitted while specifying a maximum number of
3281 // alignment bytes, e.g:
3283 if (getTok().isNot(AsmToken::Comma)) {
3285 if (parseAbsoluteExpression(FillExpr))
3288 if (parseOptionalToken(AsmToken::Comma))
3289 if (parseTokenLoc(MaxBytesLoc) ||
3290 parseAbsoluteExpression(MaxBytesToFill))
3293 return parseToken(AsmToken::EndOfStatement);
3296 if (checkForValidSection())
3297 return addErrorSuffix(" in directive");
3298 // Ignore empty '.p2align' directives for GNU-as compatibility
3299 if (IsPow2 && (ValueSize == 1) && getTok().is(AsmToken::EndOfStatement)) {
3300 Warning(AlignmentLoc, "p2align directive with no operand(s) is ignored");
3301 return parseToken(AsmToken::EndOfStatement);
3304 return addErrorSuffix(" in directive");
3306 // Always emit an alignment here even if we thrown an error.
3307 bool ReturnVal = false;
3309 // Compute alignment in bytes.
3311 // FIXME: Diagnose overflow.
3312 if (Alignment >= 32) {
3313 ReturnVal |= Error(AlignmentLoc, "invalid alignment value");
3317 Alignment = 1ULL << Alignment;
3319 // Reject alignments that aren't either a power of two or zero,
3320 // for gas compatibility. Alignment of zero is silently rounded
3324 if (!isPowerOf2_64(Alignment))
3325 ReturnVal |= Error(AlignmentLoc, "alignment must be a power of 2");
3328 // Diagnose non-sensical max bytes to align.
3329 if (MaxBytesLoc.isValid()) {
3330 if (MaxBytesToFill < 1) {
3331 ReturnVal |= Error(MaxBytesLoc,
3332 "alignment directive can never be satisfied in this "
3333 "many bytes, ignoring maximum bytes expression");
3337 if (MaxBytesToFill >= Alignment) {
3338 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
3344 // Check whether we should use optimal code alignment for this .align
3346 const MCSection *Section = getStreamer().getCurrentSectionOnly();
3347 assert(Section && "must have section to emit alignment");
3348 bool UseCodeAlign = Section->UseCodeAlign();
3349 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
3350 ValueSize == 1 && UseCodeAlign) {
3351 getStreamer().emitCodeAlignment(Alignment, MaxBytesToFill);
3353 // FIXME: Target specific behavior about how the "extra" bytes are filled.
3354 getStreamer().emitValueToAlignment(Alignment, FillExpr, ValueSize,
3361 /// parseDirectiveFile
3362 /// ::= .file filename
3363 /// ::= .file number [directory] filename [md5 checksum] [source source-text]
3364 bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
3365 // FIXME: I'm not sure what this is.
3366 int64_t FileNumber = -1;
3367 if (getLexer().is(AsmToken::Integer)) {
3368 FileNumber = getTok().getIntVal();
3372 return TokError("negative file number");
3377 // Usually the directory and filename together, otherwise just the directory.
3378 // Allow the strings to have escaped octal character sequence.
3379 if (check(getTok().isNot(AsmToken::String),
3380 "unexpected token in '.file' directive") ||
3381 parseEscapedString(Path))
3384 StringRef Directory;
3386 std::string FilenameData;
3387 if (getLexer().is(AsmToken::String)) {
3388 if (check(FileNumber == -1,
3389 "explicit path specified, but no file number") ||
3390 parseEscapedString(FilenameData))
3392 Filename = FilenameData;
3398 uint64_t MD5Hi, MD5Lo;
3399 bool HasMD5 = false;
3401 Optional<StringRef> Source;
3402 bool HasSource = false;
3403 std::string SourceString;
3405 while (!parseOptionalToken(AsmToken::EndOfStatement)) {
3407 if (check(getTok().isNot(AsmToken::Identifier),
3408 "unexpected token in '.file' directive") ||
3409 parseIdentifier(Keyword))
3411 if (Keyword == "md5") {
3413 if (check(FileNumber == -1,
3414 "MD5 checksum specified, but no file number") ||
3415 parseHexOcta(*this, MD5Hi, MD5Lo))
3417 } else if (Keyword == "source") {
3419 if (check(FileNumber == -1,
3420 "source specified, but no file number") ||
3421 check(getTok().isNot(AsmToken::String),
3422 "unexpected token in '.file' directive") ||
3423 parseEscapedString(SourceString))
3426 return TokError("unexpected token in '.file' directive");
3430 if (FileNumber == -1) {
3431 // Ignore the directive if there is no number and the target doesn't support
3432 // numberless .file directives. This allows some portability of assembler
3433 // between different object file formats.
3434 if (getContext().getAsmInfo()->hasSingleParameterDotFile())
3435 getStreamer().emitFileDirective(Filename);
3437 // In case there is a -g option as well as debug info from directive .file,
3438 // we turn off the -g option, directly use the existing debug info instead.
3439 // Throw away any implicit file table for the assembler source.
3440 if (Ctx.getGenDwarfForAssembly()) {
3441 Ctx.getMCDwarfLineTable(0).resetFileTable();
3442 Ctx.setGenDwarfForAssembly(false);
3445 Optional<MD5::MD5Result> CKMem;
3448 for (unsigned i = 0; i != 8; ++i) {
3449 Sum.Bytes[i] = uint8_t(MD5Hi >> ((7 - i) * 8));
3450 Sum.Bytes[i + 8] = uint8_t(MD5Lo >> ((7 - i) * 8));
3455 char *SourceBuf = static_cast<char *>(Ctx.allocate(SourceString.size()));
3456 memcpy(SourceBuf, SourceString.data(), SourceString.size());
3457 Source = StringRef(SourceBuf, SourceString.size());
3459 if (FileNumber == 0) {
3460 if (Ctx.getDwarfVersion() < 5)
3461 return Warning(DirectiveLoc, "file 0 not supported prior to DWARF-5");
3462 getStreamer().emitDwarfFile0Directive(Directory, Filename, CKMem, Source);
3464 Expected<unsigned> FileNumOrErr = getStreamer().tryEmitDwarfFileDirective(
3465 FileNumber, Directory, Filename, CKMem, Source);
3467 return Error(DirectiveLoc, toString(FileNumOrErr.takeError()));
3469 // Alert the user if there are some .file directives with MD5 and some not.
3470 // But only do that once.
3471 if (!ReportedInconsistentMD5 && !Ctx.isDwarfMD5UsageConsistent(0)) {
3472 ReportedInconsistentMD5 = true;
3473 return Warning(DirectiveLoc, "inconsistent use of MD5 checksums");
3480 /// parseDirectiveLine
3481 /// ::= .line [number]
3482 bool AsmParser::parseDirectiveLine() {
3484 if (getLexer().is(AsmToken::Integer)) {
3485 if (parseIntToken(LineNumber, "unexpected token in '.line' directive"))
3488 // FIXME: Do something with the .line.
3490 if (parseToken(AsmToken::EndOfStatement,
3491 "unexpected token in '.line' directive"))
3497 /// parseDirectiveLoc
3498 /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
3499 /// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
3500 /// The first number is a file number, must have been previously assigned with
3501 /// a .file directive, the second number is the line number and optionally the
3502 /// third number is a column position (zero if not specified). The remaining
3503 /// optional items are .loc sub-directives.
3504 bool AsmParser::parseDirectiveLoc() {
3505 int64_t FileNumber = 0, LineNumber = 0;
3506 SMLoc Loc = getTok().getLoc();
3507 if (parseIntToken(FileNumber, "unexpected token in '.loc' directive") ||
3508 check(FileNumber < 1 && Ctx.getDwarfVersion() < 5, Loc,
3509 "file number less than one in '.loc' directive") ||
3510 check(!getContext().isValidDwarfFileNumber(FileNumber), Loc,
3511 "unassigned file number in '.loc' directive"))
3515 if (getLexer().is(AsmToken::Integer)) {
3516 LineNumber = getTok().getIntVal();
3518 return TokError("line number less than zero in '.loc' directive");
3522 int64_t ColumnPos = 0;
3523 if (getLexer().is(AsmToken::Integer)) {
3524 ColumnPos = getTok().getIntVal();
3526 return TokError("column position less than zero in '.loc' directive");
3530 auto PrevFlags = getContext().getCurrentDwarfLoc().getFlags();
3531 unsigned Flags = PrevFlags & DWARF2_FLAG_IS_STMT;
3533 int64_t Discriminator = 0;
3535 auto parseLocOp = [&]() -> bool {
3537 SMLoc Loc = getTok().getLoc();
3538 if (parseIdentifier(Name))
3539 return TokError("unexpected token in '.loc' directive");
3541 if (Name == "basic_block")
3542 Flags |= DWARF2_FLAG_BASIC_BLOCK;
3543 else if (Name == "prologue_end")
3544 Flags |= DWARF2_FLAG_PROLOGUE_END;
3545 else if (Name == "epilogue_begin")
3546 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
3547 else if (Name == "is_stmt") {
3548 Loc = getTok().getLoc();
3549 const MCExpr *Value;
3550 if (parseExpression(Value))
3552 // The expression must be the constant 0 or 1.
3553 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3554 int Value = MCE->getValue();
3556 Flags &= ~DWARF2_FLAG_IS_STMT;
3557 else if (Value == 1)
3558 Flags |= DWARF2_FLAG_IS_STMT;
3560 return Error(Loc, "is_stmt value not 0 or 1");
3562 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
3564 } else if (Name == "isa") {
3565 Loc = getTok().getLoc();
3566 const MCExpr *Value;
3567 if (parseExpression(Value))
3569 // The expression must be a constant greater or equal to 0.
3570 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3571 int Value = MCE->getValue();
3573 return Error(Loc, "isa number less than zero");
3576 return Error(Loc, "isa number not a constant value");
3578 } else if (Name == "discriminator") {
3579 if (parseAbsoluteExpression(Discriminator))
3582 return Error(Loc, "unknown sub-directive in '.loc' directive");
3587 if (parseMany(parseLocOp, false /*hasComma*/))
3590 getStreamer().emitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
3591 Isa, Discriminator, StringRef());
3596 /// parseDirectiveStabs
3597 /// ::= .stabs string, number, number, number
3598 bool AsmParser::parseDirectiveStabs() {
3599 return TokError("unsupported directive '.stabs'");
3602 /// parseDirectiveCVFile
3603 /// ::= .cv_file number filename [checksum] [checksumkind]
3604 bool AsmParser::parseDirectiveCVFile() {
3605 SMLoc FileNumberLoc = getTok().getLoc();
3607 std::string Filename;
3608 std::string Checksum;
3609 int64_t ChecksumKind = 0;
3611 if (parseIntToken(FileNumber,
3612 "expected file number in '.cv_file' directive") ||
3613 check(FileNumber < 1, FileNumberLoc, "file number less than one") ||
3614 check(getTok().isNot(AsmToken::String),
3615 "unexpected token in '.cv_file' directive") ||
3616 parseEscapedString(Filename))
3618 if (!parseOptionalToken(AsmToken::EndOfStatement)) {
3619 if (check(getTok().isNot(AsmToken::String),
3620 "unexpected token in '.cv_file' directive") ||
3621 parseEscapedString(Checksum) ||
3622 parseIntToken(ChecksumKind,
3623 "expected checksum kind in '.cv_file' directive") ||
3624 parseToken(AsmToken::EndOfStatement,
3625 "unexpected token in '.cv_file' directive"))
3629 Checksum = fromHex(Checksum);
3630 void *CKMem = Ctx.allocate(Checksum.size(), 1);
3631 memcpy(CKMem, Checksum.data(), Checksum.size());
3632 ArrayRef<uint8_t> ChecksumAsBytes(reinterpret_cast<const uint8_t *>(CKMem),
3635 if (!getStreamer().EmitCVFileDirective(FileNumber, Filename, ChecksumAsBytes,
3636 static_cast<uint8_t>(ChecksumKind)))
3637 return Error(FileNumberLoc, "file number already allocated");
3642 bool AsmParser::parseCVFunctionId(int64_t &FunctionId,
3643 StringRef DirectiveName) {
3645 return parseTokenLoc(Loc) ||
3646 parseIntToken(FunctionId, "expected function id in '" + DirectiveName +
3648 check(FunctionId < 0 || FunctionId >= UINT_MAX, Loc,
3649 "expected function id within range [0, UINT_MAX)");
3652 bool AsmParser::parseCVFileId(int64_t &FileNumber, StringRef DirectiveName) {
3654 return parseTokenLoc(Loc) ||
3655 parseIntToken(FileNumber, "expected integer in '" + DirectiveName +
3657 check(FileNumber < 1, Loc, "file number less than one in '" +
3658 DirectiveName + "' directive") ||
3659 check(!getCVContext().isValidFileNumber(FileNumber), Loc,
3660 "unassigned file number in '" + DirectiveName + "' directive");
3663 /// parseDirectiveCVFuncId
3664 /// ::= .cv_func_id FunctionId
3666 /// Introduces a function ID that can be used with .cv_loc.
3667 bool AsmParser::parseDirectiveCVFuncId() {
3668 SMLoc FunctionIdLoc = getTok().getLoc();
3671 if (parseCVFunctionId(FunctionId, ".cv_func_id") ||
3672 parseToken(AsmToken::EndOfStatement,
3673 "unexpected token in '.cv_func_id' directive"))
3676 if (!getStreamer().EmitCVFuncIdDirective(FunctionId))
3677 return Error(FunctionIdLoc, "function id already allocated");
3682 /// parseDirectiveCVInlineSiteId
3683 /// ::= .cv_inline_site_id FunctionId
3685 /// "inlined_at" IAFile IALine [IACol]
3687 /// Introduces a function ID that can be used with .cv_loc. Includes "inlined
3688 /// at" source location information for use in the line table of the caller,
3689 /// whether the caller is a real function or another inlined call site.
3690 bool AsmParser::parseDirectiveCVInlineSiteId() {
3691 SMLoc FunctionIdLoc = getTok().getLoc();
3699 if (parseCVFunctionId(FunctionId, ".cv_inline_site_id"))
3703 if (check((getLexer().isNot(AsmToken::Identifier) ||
3704 getTok().getIdentifier() != "within"),
3705 "expected 'within' identifier in '.cv_inline_site_id' directive"))
3710 if (parseCVFunctionId(IAFunc, ".cv_inline_site_id"))
3714 if (check((getLexer().isNot(AsmToken::Identifier) ||
3715 getTok().getIdentifier() != "inlined_at"),
3716 "expected 'inlined_at' identifier in '.cv_inline_site_id' "
3722 if (parseCVFileId(IAFile, ".cv_inline_site_id") ||
3723 parseIntToken(IALine, "expected line number after 'inlined_at'"))
3727 if (getLexer().is(AsmToken::Integer)) {
3728 IACol = getTok().getIntVal();
3732 if (parseToken(AsmToken::EndOfStatement,
3733 "unexpected token in '.cv_inline_site_id' directive"))
3736 if (!getStreamer().EmitCVInlineSiteIdDirective(FunctionId, IAFunc, IAFile,
3737 IALine, IACol, FunctionIdLoc))
3738 return Error(FunctionIdLoc, "function id already allocated");
3743 /// parseDirectiveCVLoc
3744 /// ::= .cv_loc FunctionId FileNumber [LineNumber] [ColumnPos] [prologue_end]
3746 /// The first number is a file number, must have been previously assigned with
3747 /// a .file directive, the second number is the line number and optionally the
3748 /// third number is a column position (zero if not specified). The remaining
3749 /// optional items are .loc sub-directives.
3750 bool AsmParser::parseDirectiveCVLoc() {
3751 SMLoc DirectiveLoc = getTok().getLoc();
3752 int64_t FunctionId, FileNumber;
3753 if (parseCVFunctionId(FunctionId, ".cv_loc") ||
3754 parseCVFileId(FileNumber, ".cv_loc"))
3757 int64_t LineNumber = 0;
3758 if (getLexer().is(AsmToken::Integer)) {
3759 LineNumber = getTok().getIntVal();
3761 return TokError("line number less than zero in '.cv_loc' directive");
3765 int64_t ColumnPos = 0;
3766 if (getLexer().is(AsmToken::Integer)) {
3767 ColumnPos = getTok().getIntVal();
3769 return TokError("column position less than zero in '.cv_loc' directive");
3773 bool PrologueEnd = false;
3774 uint64_t IsStmt = 0;
3776 auto parseOp = [&]() -> bool {
3778 SMLoc Loc = getTok().getLoc();
3779 if (parseIdentifier(Name))
3780 return TokError("unexpected token in '.cv_loc' directive");
3781 if (Name == "prologue_end")
3783 else if (Name == "is_stmt") {
3784 Loc = getTok().getLoc();
3785 const MCExpr *Value;
3786 if (parseExpression(Value))
3788 // The expression must be the constant 0 or 1.
3790 if (const auto *MCE = dyn_cast<MCConstantExpr>(Value))
3791 IsStmt = MCE->getValue();
3794 return Error(Loc, "is_stmt value not 0 or 1");
3796 return Error(Loc, "unknown sub-directive in '.cv_loc' directive");
3801 if (parseMany(parseOp, false /*hasComma*/))
3804 getStreamer().emitCVLocDirective(FunctionId, FileNumber, LineNumber,
3805 ColumnPos, PrologueEnd, IsStmt, StringRef(),
3810 /// parseDirectiveCVLinetable
3811 /// ::= .cv_linetable FunctionId, FnStart, FnEnd
3812 bool AsmParser::parseDirectiveCVLinetable() {
3814 StringRef FnStartName, FnEndName;
3815 SMLoc Loc = getTok().getLoc();
3816 if (parseCVFunctionId(FunctionId, ".cv_linetable") ||
3817 parseToken(AsmToken::Comma,
3818 "unexpected token in '.cv_linetable' directive") ||
3819 parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc,
3820 "expected identifier in directive") ||
3821 parseToken(AsmToken::Comma,
3822 "unexpected token in '.cv_linetable' directive") ||
3823 parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc,
3824 "expected identifier in directive"))
3827 MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
3828 MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
3830 getStreamer().emitCVLinetableDirective(FunctionId, FnStartSym, FnEndSym);
3834 /// parseDirectiveCVInlineLinetable
3835 /// ::= .cv_inline_linetable PrimaryFunctionId FileId LineNum FnStart FnEnd
3836 bool AsmParser::parseDirectiveCVInlineLinetable() {
3837 int64_t PrimaryFunctionId, SourceFileId, SourceLineNum;
3838 StringRef FnStartName, FnEndName;
3839 SMLoc Loc = getTok().getLoc();
3840 if (parseCVFunctionId(PrimaryFunctionId, ".cv_inline_linetable") ||
3841 parseTokenLoc(Loc) ||
3844 "expected SourceField in '.cv_inline_linetable' directive") ||
3845 check(SourceFileId <= 0, Loc,
3846 "File id less than zero in '.cv_inline_linetable' directive") ||
3847 parseTokenLoc(Loc) ||
3850 "expected SourceLineNum in '.cv_inline_linetable' directive") ||
3851 check(SourceLineNum < 0, Loc,
3852 "Line number less than zero in '.cv_inline_linetable' directive") ||
3853 parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc,
3854 "expected identifier in directive") ||
3855 parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc,
3856 "expected identifier in directive"))
3859 if (parseToken(AsmToken::EndOfStatement, "Expected End of Statement"))
3862 MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
3863 MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName);
3864 getStreamer().emitCVInlineLinetableDirective(PrimaryFunctionId, SourceFileId,
3865 SourceLineNum, FnStartSym,
3870 void AsmParser::initializeCVDefRangeTypeMap() {
3871 CVDefRangeTypeMap["reg"] = CVDR_DEFRANGE_REGISTER;
3872 CVDefRangeTypeMap["frame_ptr_rel"] = CVDR_DEFRANGE_FRAMEPOINTER_REL;
3873 CVDefRangeTypeMap["subfield_reg"] = CVDR_DEFRANGE_SUBFIELD_REGISTER;
3874 CVDefRangeTypeMap["reg_rel"] = CVDR_DEFRANGE_REGISTER_REL;
3877 /// parseDirectiveCVDefRange
3878 /// ::= .cv_def_range RangeStart RangeEnd (GapStart GapEnd)*, bytes*
3879 bool AsmParser::parseDirectiveCVDefRange() {
3881 std::vector<std::pair<const MCSymbol *, const MCSymbol *>> Ranges;
3882 while (getLexer().is(AsmToken::Identifier)) {
3883 Loc = getLexer().getLoc();
3884 StringRef GapStartName;
3885 if (parseIdentifier(GapStartName))
3886 return Error(Loc, "expected identifier in directive");
3887 MCSymbol *GapStartSym = getContext().getOrCreateSymbol(GapStartName);
3889 Loc = getLexer().getLoc();
3890 StringRef GapEndName;
3891 if (parseIdentifier(GapEndName))
3892 return Error(Loc, "expected identifier in directive");
3893 MCSymbol *GapEndSym = getContext().getOrCreateSymbol(GapEndName);
3895 Ranges.push_back({GapStartSym, GapEndSym});
3898 StringRef CVDefRangeTypeStr;
3901 "expected comma before def_range type in .cv_def_range directive") ||
3902 parseIdentifier(CVDefRangeTypeStr))
3903 return Error(Loc, "expected def_range type in directive");
3905 StringMap<CVDefRangeType>::const_iterator CVTypeIt =
3906 CVDefRangeTypeMap.find(CVDefRangeTypeStr);
3907 CVDefRangeType CVDRType = (CVTypeIt == CVDefRangeTypeMap.end())
3909 : CVTypeIt->getValue();
3911 case CVDR_DEFRANGE_REGISTER: {
3913 if (parseToken(AsmToken::Comma, "expected comma before register number in "
3914 ".cv_def_range directive") ||
3915 parseAbsoluteExpression(DRRegister))
3916 return Error(Loc, "expected register number");
3918 codeview::DefRangeRegisterHeader DRHdr;
3919 DRHdr.Register = DRRegister;
3920 DRHdr.MayHaveNoName = 0;
3921 getStreamer().emitCVDefRangeDirective(Ranges, DRHdr);
3924 case CVDR_DEFRANGE_FRAMEPOINTER_REL: {
3926 if (parseToken(AsmToken::Comma,
3927 "expected comma before offset in .cv_def_range directive") ||
3928 parseAbsoluteExpression(DROffset))
3929 return Error(Loc, "expected offset value");
3931 codeview::DefRangeFramePointerRelHeader DRHdr;
3932 DRHdr.Offset = DROffset;
3933 getStreamer().emitCVDefRangeDirective(Ranges, DRHdr);
3936 case CVDR_DEFRANGE_SUBFIELD_REGISTER: {
3938 int64_t DROffsetInParent;
3939 if (parseToken(AsmToken::Comma, "expected comma before register number in "
3940 ".cv_def_range directive") ||
3941 parseAbsoluteExpression(DRRegister))
3942 return Error(Loc, "expected register number");
3943 if (parseToken(AsmToken::Comma,
3944 "expected comma before offset in .cv_def_range directive") ||
3945 parseAbsoluteExpression(DROffsetInParent))
3946 return Error(Loc, "expected offset value");
3948 codeview::DefRangeSubfieldRegisterHeader DRHdr;
3949 DRHdr.Register = DRRegister;
3950 DRHdr.MayHaveNoName = 0;
3951 DRHdr.OffsetInParent = DROffsetInParent;
3952 getStreamer().emitCVDefRangeDirective(Ranges, DRHdr);
3955 case CVDR_DEFRANGE_REGISTER_REL: {
3958 int64_t DRBasePointerOffset;
3959 if (parseToken(AsmToken::Comma, "expected comma before register number in "
3960 ".cv_def_range directive") ||
3961 parseAbsoluteExpression(DRRegister))
3962 return Error(Loc, "expected register value");
3965 "expected comma before flag value in .cv_def_range directive") ||
3966 parseAbsoluteExpression(DRFlags))
3967 return Error(Loc, "expected flag value");
3968 if (parseToken(AsmToken::Comma, "expected comma before base pointer offset "
3969 "in .cv_def_range directive") ||
3970 parseAbsoluteExpression(DRBasePointerOffset))
3971 return Error(Loc, "expected base pointer offset value");
3973 codeview::DefRangeRegisterRelHeader DRHdr;
3974 DRHdr.Register = DRRegister;
3975 DRHdr.Flags = DRFlags;
3976 DRHdr.BasePointerOffset = DRBasePointerOffset;
3977 getStreamer().emitCVDefRangeDirective(Ranges, DRHdr);
3981 return Error(Loc, "unexpected def_range type in .cv_def_range directive");
3986 /// parseDirectiveCVString
3987 /// ::= .cv_stringtable "string"
3988 bool AsmParser::parseDirectiveCVString() {
3990 if (checkForValidSection() || parseEscapedString(Data))
3991 return addErrorSuffix(" in '.cv_string' directive");
3993 // Put the string in the table and emit the offset.
3994 std::pair<StringRef, unsigned> Insertion =
3995 getCVContext().addToStringTable(Data);
3996 getStreamer().emitInt32(Insertion.second);
4000 /// parseDirectiveCVStringTable
4001 /// ::= .cv_stringtable
4002 bool AsmParser::parseDirectiveCVStringTable() {
4003 getStreamer().emitCVStringTableDirective();
4007 /// parseDirectiveCVFileChecksums
4008 /// ::= .cv_filechecksums
4009 bool AsmParser::parseDirectiveCVFileChecksums() {
4010 getStreamer().emitCVFileChecksumsDirective();
4014 /// parseDirectiveCVFileChecksumOffset
4015 /// ::= .cv_filechecksumoffset fileno
4016 bool AsmParser::parseDirectiveCVFileChecksumOffset() {
4018 if (parseIntToken(FileNo, "expected identifier in directive"))
4020 if (parseToken(AsmToken::EndOfStatement, "Expected End of Statement"))
4022 getStreamer().emitCVFileChecksumOffsetDirective(FileNo);
4026 /// parseDirectiveCVFPOData
4027 /// ::= .cv_fpo_data procsym
4028 bool AsmParser::parseDirectiveCVFPOData() {
4029 SMLoc DirLoc = getLexer().getLoc();
4031 if (parseIdentifier(ProcName))
4032 return TokError("expected symbol name");
4033 if (parseEOL("unexpected tokens"))
4034 return addErrorSuffix(" in '.cv_fpo_data' directive");
4035 MCSymbol *ProcSym = getContext().getOrCreateSymbol(ProcName);
4036 getStreamer().EmitCVFPOData(ProcSym, DirLoc);
4040 /// parseDirectiveCFISections
4041 /// ::= .cfi_sections section [, section]
4042 bool AsmParser::parseDirectiveCFISections() {
4047 if (parseIdentifier(Name))
4048 return TokError("Expected an identifier");
4050 if (Name == ".eh_frame")
4052 else if (Name == ".debug_frame")
4055 if (getLexer().is(AsmToken::Comma)) {
4058 if (parseIdentifier(Name))
4059 return TokError("Expected an identifier");
4061 if (Name == ".eh_frame")
4063 else if (Name == ".debug_frame")
4067 getStreamer().emitCFISections(EH, Debug);
4071 /// parseDirectiveCFIStartProc
4072 /// ::= .cfi_startproc [simple]
4073 bool AsmParser::parseDirectiveCFIStartProc() {
4075 if (!parseOptionalToken(AsmToken::EndOfStatement)) {
4076 if (check(parseIdentifier(Simple) || Simple != "simple",
4077 "unexpected token") ||
4078 parseToken(AsmToken::EndOfStatement))
4079 return addErrorSuffix(" in '.cfi_startproc' directive");
4082 // TODO(kristina): Deal with a corner case of incorrect diagnostic context
4083 // being produced if this directive is emitted as part of preprocessor macro
4084 // expansion which can *ONLY* happen if Clang's cc1as is the API consumer.
4085 // Tools like llvm-mc on the other hand are not affected by it, and report
4086 // correct context information.
4087 getStreamer().emitCFIStartProc(!Simple.empty(), Lexer.getLoc());
4091 /// parseDirectiveCFIEndProc
4092 /// ::= .cfi_endproc
4093 bool AsmParser::parseDirectiveCFIEndProc() {
4094 getStreamer().emitCFIEndProc();
4098 /// parse register name or number.
4099 bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
4100 SMLoc DirectiveLoc) {
4103 if (getLexer().isNot(AsmToken::Integer)) {
4104 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
4106 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
4108 return parseAbsoluteExpression(Register);
4113 /// parseDirectiveCFIDefCfa
4114 /// ::= .cfi_def_cfa register, offset
4115 bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
4116 int64_t Register = 0, Offset = 0;
4117 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) ||
4118 parseToken(AsmToken::Comma, "unexpected token in directive") ||
4119 parseAbsoluteExpression(Offset))
4122 getStreamer().emitCFIDefCfa(Register, Offset);
4126 /// parseDirectiveCFIDefCfaOffset
4127 /// ::= .cfi_def_cfa_offset offset
4128 bool AsmParser::parseDirectiveCFIDefCfaOffset() {
4130 if (parseAbsoluteExpression(Offset))
4133 getStreamer().emitCFIDefCfaOffset(Offset);
4137 /// parseDirectiveCFIRegister
4138 /// ::= .cfi_register register, register
4139 bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
4140 int64_t Register1 = 0, Register2 = 0;
4141 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc) ||
4142 parseToken(AsmToken::Comma, "unexpected token in directive") ||
4143 parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
4146 getStreamer().emitCFIRegister(Register1, Register2);
4150 /// parseDirectiveCFIWindowSave
4151 /// ::= .cfi_window_save
4152 bool AsmParser::parseDirectiveCFIWindowSave() {
4153 getStreamer().emitCFIWindowSave();
4157 /// parseDirectiveCFIAdjustCfaOffset
4158 /// ::= .cfi_adjust_cfa_offset adjustment
4159 bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
4160 int64_t Adjustment = 0;
4161 if (parseAbsoluteExpression(Adjustment))
4164 getStreamer().emitCFIAdjustCfaOffset(Adjustment);
4168 /// parseDirectiveCFIDefCfaRegister
4169 /// ::= .cfi_def_cfa_register register
4170 bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
4171 int64_t Register = 0;
4172 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
4175 getStreamer().emitCFIDefCfaRegister(Register);
4179 /// parseDirectiveCFIOffset
4180 /// ::= .cfi_offset register, offset
4181 bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
4182 int64_t Register = 0;
4185 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) ||
4186 parseToken(AsmToken::Comma, "unexpected token in directive") ||
4187 parseAbsoluteExpression(Offset))
4190 getStreamer().emitCFIOffset(Register, Offset);
4194 /// parseDirectiveCFIRelOffset
4195 /// ::= .cfi_rel_offset register, offset
4196 bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
4197 int64_t Register = 0, Offset = 0;
4199 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) ||
4200 parseToken(AsmToken::Comma, "unexpected token in directive") ||
4201 parseAbsoluteExpression(Offset))
4204 getStreamer().emitCFIRelOffset(Register, Offset);
4208 static bool isValidEncoding(int64_t Encoding) {
4209 if (Encoding & ~0xff)
4212 if (Encoding == dwarf::DW_EH_PE_omit)
4215 const unsigned Format = Encoding & 0xf;
4216 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
4217 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
4218 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
4219 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
4222 const unsigned Application = Encoding & 0x70;
4223 if (Application != dwarf::DW_EH_PE_absptr &&
4224 Application != dwarf::DW_EH_PE_pcrel)
4230 /// parseDirectiveCFIPersonalityOrLsda
4231 /// IsPersonality true for cfi_personality, false for cfi_lsda
4232 /// ::= .cfi_personality encoding, [symbol_name]
4233 /// ::= .cfi_lsda encoding, [symbol_name]
4234 bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
4235 int64_t Encoding = 0;
4236 if (parseAbsoluteExpression(Encoding))
4238 if (Encoding == dwarf::DW_EH_PE_omit)
4242 if (check(!isValidEncoding(Encoding), "unsupported encoding.") ||
4243 parseToken(AsmToken::Comma, "unexpected token in directive") ||
4244 check(parseIdentifier(Name), "expected identifier in directive"))
4247 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
4250 getStreamer().emitCFIPersonality(Sym, Encoding);
4252 getStreamer().emitCFILsda(Sym, Encoding);
4256 /// parseDirectiveCFIRememberState
4257 /// ::= .cfi_remember_state
4258 bool AsmParser::parseDirectiveCFIRememberState() {
4259 getStreamer().emitCFIRememberState();
4263 /// parseDirectiveCFIRestoreState
4264 /// ::= .cfi_remember_state
4265 bool AsmParser::parseDirectiveCFIRestoreState() {
4266 getStreamer().emitCFIRestoreState();
4270 /// parseDirectiveCFISameValue
4271 /// ::= .cfi_same_value register
4272 bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
4273 int64_t Register = 0;
4275 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
4278 getStreamer().emitCFISameValue(Register);
4282 /// parseDirectiveCFIRestore
4283 /// ::= .cfi_restore register
4284 bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
4285 int64_t Register = 0;
4286 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
4289 getStreamer().emitCFIRestore(Register);
4293 /// parseDirectiveCFIEscape
4294 /// ::= .cfi_escape expression[,...]
4295 bool AsmParser::parseDirectiveCFIEscape() {
4298 if (parseAbsoluteExpression(CurrValue))
4301 Values.push_back((uint8_t)CurrValue);
4303 while (getLexer().is(AsmToken::Comma)) {
4306 if (parseAbsoluteExpression(CurrValue))
4309 Values.push_back((uint8_t)CurrValue);
4312 getStreamer().emitCFIEscape(Values);
4316 /// parseDirectiveCFIReturnColumn
4317 /// ::= .cfi_return_column register
4318 bool AsmParser::parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc) {
4319 int64_t Register = 0;
4320 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
4322 getStreamer().emitCFIReturnColumn(Register);
4326 /// parseDirectiveCFISignalFrame
4327 /// ::= .cfi_signal_frame
4328 bool AsmParser::parseDirectiveCFISignalFrame() {
4329 if (parseToken(AsmToken::EndOfStatement,
4330 "unexpected token in '.cfi_signal_frame'"))
4333 getStreamer().emitCFISignalFrame();
4337 /// parseDirectiveCFIUndefined
4338 /// ::= .cfi_undefined register
4339 bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
4340 int64_t Register = 0;
4342 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
4345 getStreamer().emitCFIUndefined(Register);
4349 /// parseDirectiveAltmacro
4352 bool AsmParser::parseDirectiveAltmacro(StringRef Directive) {
4353 if (getLexer().isNot(AsmToken::EndOfStatement))
4354 return TokError("unexpected token in '" + Directive + "' directive");
4355 AltMacroMode = (Directive == ".altmacro");
4359 /// parseDirectiveMacrosOnOff
4362 bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
4363 if (parseToken(AsmToken::EndOfStatement,
4364 "unexpected token in '" + Directive + "' directive"))
4367 setMacrosEnabled(Directive == ".macros_on");
4371 /// parseDirectiveMacro
4372 /// ::= .macro name[,] [parameters]
4373 bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
4375 if (parseIdentifier(Name))
4376 return TokError("expected identifier in '.macro' directive");
4378 if (getLexer().is(AsmToken::Comma))
4381 MCAsmMacroParameters Parameters;
4382 while (getLexer().isNot(AsmToken::EndOfStatement)) {
4384 if (!Parameters.empty() && Parameters.back().Vararg)
4385 return Error(Lexer.getLoc(), "vararg parameter '" +
4386 Parameters.back().Name +
4387 "' should be the last parameter");
4389 MCAsmMacroParameter Parameter;
4390 if (parseIdentifier(Parameter.Name))
4391 return TokError("expected identifier in '.macro' directive");
4393 // Emit an error if two (or more) named parameters share the same name
4394 for (const MCAsmMacroParameter& CurrParam : Parameters)
4395 if (CurrParam.Name.equals(Parameter.Name))
4396 return TokError("macro '" + Name + "' has multiple parameters"
4397 " named '" + Parameter.Name + "'");
4399 if (Lexer.is(AsmToken::Colon)) {
4400 Lex(); // consume ':'
4403 StringRef Qualifier;
4405 QualLoc = Lexer.getLoc();
4406 if (parseIdentifier(Qualifier))
4407 return Error(QualLoc, "missing parameter qualifier for "
4408 "'" + Parameter.Name + "' in macro '" + Name + "'");
4410 if (Qualifier == "req")
4411 Parameter.Required = true;
4412 else if (Qualifier == "vararg")
4413 Parameter.Vararg = true;
4415 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
4416 "for '" + Parameter.Name + "' in macro '" + Name + "'");
4419 if (getLexer().is(AsmToken::Equal)) {
4424 ParamLoc = Lexer.getLoc();
4425 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
4428 if (Parameter.Required)
4429 Warning(ParamLoc, "pointless default value for required parameter "
4430 "'" + Parameter.Name + "' in macro '" + Name + "'");
4433 Parameters.push_back(std::move(Parameter));
4435 if (getLexer().is(AsmToken::Comma))
4439 // Eat just the end of statement.
4442 // Consuming deferred text, so use Lexer.Lex to ignore Lexing Errors
4443 AsmToken EndToken, StartToken = getTok();
4444 unsigned MacroDepth = 0;
4445 // Lex the macro definition.
4447 // Ignore Lexing errors in macros.
4448 while (Lexer.is(AsmToken::Error)) {
4452 // Check whether we have reached the end of the file.
4453 if (getLexer().is(AsmToken::Eof))
4454 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
4456 // Otherwise, check whether we have reach the .endmacro or the start of a
4457 // preprocessor line marker.
4458 if (getLexer().is(AsmToken::Identifier)) {
4459 if (getTok().getIdentifier() == ".endm" ||
4460 getTok().getIdentifier() == ".endmacro") {
4461 if (MacroDepth == 0) { // Outermost macro.
4462 EndToken = getTok();
4464 if (getLexer().isNot(AsmToken::EndOfStatement))
4465 return TokError("unexpected token in '" + EndToken.getIdentifier() +
4469 // Otherwise we just found the end of an inner macro.
4472 } else if (getTok().getIdentifier() == ".macro") {
4473 // We allow nested macros. Those aren't instantiated until the outermost
4474 // macro is expanded so just ignore them for now.
4477 } else if (Lexer.is(AsmToken::HashDirective)) {
4478 (void)parseCppHashLineFilenameComment(getLexer().getLoc());
4481 // Otherwise, scan til the end of the statement.
4482 eatToEndOfStatement();
4485 if (getContext().lookupMacro(Name)) {
4486 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
4489 const char *BodyStart = StartToken.getLoc().getPointer();
4490 const char *BodyEnd = EndToken.getLoc().getPointer();
4491 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4492 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
4493 MCAsmMacro Macro(Name, Body, std::move(Parameters));
4494 DEBUG_WITH_TYPE("asm-macros", dbgs() << "Defining new macro:\n";
4496 getContext().defineMacro(Name, std::move(Macro));
4500 /// checkForBadMacro
4502 /// With the support added for named parameters there may be code out there that
4503 /// is transitioning from positional parameters. In versions of gas that did
4504 /// not support named parameters they would be ignored on the macro definition.
4505 /// But to support both styles of parameters this is not possible so if a macro
4506 /// definition has named parameters but does not use them and has what appears
4507 /// to be positional parameters, strings like $1, $2, ... and $n, then issue a
4508 /// warning that the positional parameter found in body which have no effect.
4509 /// Hoping the developer will either remove the named parameters from the macro
4510 /// definition so the positional parameters get used if that was what was
4511 /// intended or change the macro to use the named parameters. It is possible
4512 /// this warning will trigger when the none of the named parameters are used
4513 /// and the strings like $1 are infact to simply to be passed trough unchanged.
4514 void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
4516 ArrayRef<MCAsmMacroParameter> Parameters) {
4517 // If this macro is not defined with named parameters the warning we are
4518 // checking for here doesn't apply.
4519 unsigned NParameters = Parameters.size();
4520 if (NParameters == 0)
4523 bool NamedParametersFound = false;
4524 bool PositionalParametersFound = false;
4526 // Look at the body of the macro for use of both the named parameters and what
4527 // are likely to be positional parameters. This is what expandMacro() is
4528 // doing when it finds the parameters in the body.
4529 while (!Body.empty()) {
4530 // Scan for the next possible parameter.
4531 std::size_t End = Body.size(), Pos = 0;
4532 for (; Pos != End; ++Pos) {
4533 // Check for a substitution or escape.
4534 // This macro is defined with parameters, look for \foo, \bar, etc.
4535 if (Body[Pos] == '\\' && Pos + 1 != End)
4538 // This macro should have parameters, but look for $0, $1, ..., $n too.
4539 if (Body[Pos] != '$' || Pos + 1 == End)
4541 char Next = Body[Pos + 1];
4542 if (Next == '$' || Next == 'n' ||
4543 isdigit(static_cast<unsigned char>(Next)))
4547 // Check if we reached the end.
4551 if (Body[Pos] == '$') {
4552 switch (Body[Pos + 1]) {
4557 // $n => number of arguments
4559 PositionalParametersFound = true;
4562 // $[0-9] => argument
4564 PositionalParametersFound = true;
4570 unsigned I = Pos + 1;
4571 while (isIdentifierChar(Body[I]) && I + 1 != End)
4574 const char *Begin = Body.data() + Pos + 1;
4575 StringRef Argument(Begin, I - (Pos + 1));
4577 for (; Index < NParameters; ++Index)
4578 if (Parameters[Index].Name == Argument)
4581 if (Index == NParameters) {
4582 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
4588 NamedParametersFound = true;
4589 Pos += 1 + Argument.size();
4592 // Update the scan point.
4593 Body = Body.substr(Pos);
4596 if (!NamedParametersFound && PositionalParametersFound)
4597 Warning(DirectiveLoc, "macro defined with named parameters which are not "
4598 "used in macro body, possible positional parameter "
4599 "found in body which will have no effect");
4602 /// parseDirectiveExitMacro
4604 bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
4605 if (parseToken(AsmToken::EndOfStatement,
4606 "unexpected token in '" + Directive + "' directive"))
4609 if (!isInsideMacroInstantiation())
4610 return TokError("unexpected '" + Directive + "' in file, "
4611 "no current macro definition");
4613 // Exit all conditionals that are active in the current macro.
4614 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
4615 TheCondState = TheCondStack.back();
4616 TheCondStack.pop_back();
4623 /// parseDirectiveEndMacro
4626 bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
4627 if (getLexer().isNot(AsmToken::EndOfStatement))
4628 return TokError("unexpected token in '" + Directive + "' directive");
4630 // If we are inside a macro instantiation, terminate the current
4632 if (isInsideMacroInstantiation()) {
4637 // Otherwise, this .endmacro is a stray entry in the file; well formed
4638 // .endmacro directives are handled during the macro definition parsing.
4639 return TokError("unexpected '" + Directive + "' in file, "
4640 "no current macro definition");
4643 /// parseDirectivePurgeMacro
4645 bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
4648 if (parseTokenLoc(Loc) ||
4649 check(parseIdentifier(Name), Loc,
4650 "expected identifier in '.purgem' directive") ||
4651 parseToken(AsmToken::EndOfStatement,
4652 "unexpected token in '.purgem' directive"))
4655 if (!getContext().lookupMacro(Name))
4656 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
4658 getContext().undefineMacro(Name);
4659 DEBUG_WITH_TYPE("asm-macros", dbgs()
4660 << "Un-defining macro: " << Name << "\n");
4664 /// parseDirectiveBundleAlignMode
4665 /// ::= {.bundle_align_mode} expression
4666 bool AsmParser::parseDirectiveBundleAlignMode() {
4667 // Expect a single argument: an expression that evaluates to a constant
4668 // in the inclusive range 0-30.
4669 SMLoc ExprLoc = getLexer().getLoc();
4670 int64_t AlignSizePow2;
4671 if (checkForValidSection() || parseAbsoluteExpression(AlignSizePow2) ||
4672 parseToken(AsmToken::EndOfStatement, "unexpected token after expression "
4673 "in '.bundle_align_mode' "
4675 check(AlignSizePow2 < 0 || AlignSizePow2 > 30, ExprLoc,
4676 "invalid bundle alignment size (expected between 0 and 30)"))
4679 // Because of AlignSizePow2's verified range we can safely truncate it to
4681 getStreamer().emitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
4685 /// parseDirectiveBundleLock
4686 /// ::= {.bundle_lock} [align_to_end]
4687 bool AsmParser::parseDirectiveBundleLock() {
4688 if (checkForValidSection())
4690 bool AlignToEnd = false;
4693 SMLoc Loc = getTok().getLoc();
4694 const char *kInvalidOptionError =
4695 "invalid option for '.bundle_lock' directive";
4697 if (!parseOptionalToken(AsmToken::EndOfStatement)) {
4698 if (check(parseIdentifier(Option), Loc, kInvalidOptionError) ||
4699 check(Option != "align_to_end", Loc, kInvalidOptionError) ||
4700 parseToken(AsmToken::EndOfStatement,
4701 "unexpected token after '.bundle_lock' directive option"))
4706 getStreamer().emitBundleLock(AlignToEnd);
4710 /// parseDirectiveBundleLock
4711 /// ::= {.bundle_lock}
4712 bool AsmParser::parseDirectiveBundleUnlock() {
4713 if (checkForValidSection() ||
4714 parseToken(AsmToken::EndOfStatement,
4715 "unexpected token in '.bundle_unlock' directive"))
4718 getStreamer().emitBundleUnlock();
4722 /// parseDirectiveSpace
4723 /// ::= (.skip | .space) expression [ , expression ]
4724 bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
4725 SMLoc NumBytesLoc = Lexer.getLoc();
4726 const MCExpr *NumBytes;
4727 if (checkForValidSection() || parseExpression(NumBytes))
4730 int64_t FillExpr = 0;
4731 if (parseOptionalToken(AsmToken::Comma))
4732 if (parseAbsoluteExpression(FillExpr))
4733 return addErrorSuffix("in '" + Twine(IDVal) + "' directive");
4734 if (parseToken(AsmToken::EndOfStatement))
4735 return addErrorSuffix("in '" + Twine(IDVal) + "' directive");
4737 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
4738 getStreamer().emitFill(*NumBytes, FillExpr, NumBytesLoc);
4743 /// parseDirectiveDCB
4744 /// ::= .dcb.{b, l, w} expression, expression
4745 bool AsmParser::parseDirectiveDCB(StringRef IDVal, unsigned Size) {
4746 SMLoc NumValuesLoc = Lexer.getLoc();
4748 if (checkForValidSection() || parseAbsoluteExpression(NumValues))
4751 if (NumValues < 0) {
4752 Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect");
4756 if (parseToken(AsmToken::Comma,
4757 "unexpected token in '" + Twine(IDVal) + "' directive"))
4760 const MCExpr *Value;
4761 SMLoc ExprLoc = getLexer().getLoc();
4762 if (parseExpression(Value))
4765 // Special case constant expressions to match code generator.
4766 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
4767 assert(Size <= 8 && "Invalid size");
4768 uint64_t IntValue = MCE->getValue();
4769 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
4770 return Error(ExprLoc, "literal value out of range for directive");
4771 for (uint64_t i = 0, e = NumValues; i != e; ++i)
4772 getStreamer().emitIntValue(IntValue, Size);
4774 for (uint64_t i = 0, e = NumValues; i != e; ++i)
4775 getStreamer().emitValue(Value, Size, ExprLoc);
4778 if (parseToken(AsmToken::EndOfStatement,
4779 "unexpected token in '" + Twine(IDVal) + "' directive"))
4785 /// parseDirectiveRealDCB
4786 /// ::= .dcb.{d, s} expression, expression
4787 bool AsmParser::parseDirectiveRealDCB(StringRef IDVal, const fltSemantics &Semantics) {
4788 SMLoc NumValuesLoc = Lexer.getLoc();
4790 if (checkForValidSection() || parseAbsoluteExpression(NumValues))
4793 if (NumValues < 0) {
4794 Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect");
4798 if (parseToken(AsmToken::Comma,
4799 "unexpected token in '" + Twine(IDVal) + "' directive"))
4803 if (parseRealValue(Semantics, AsInt))
4806 if (parseToken(AsmToken::EndOfStatement,
4807 "unexpected token in '" + Twine(IDVal) + "' directive"))
4810 for (uint64_t i = 0, e = NumValues; i != e; ++i)
4811 getStreamer().emitIntValue(AsInt.getLimitedValue(),
4812 AsInt.getBitWidth() / 8);
4817 /// parseDirectiveDS
4818 /// ::= .ds.{b, d, l, p, s, w, x} expression
4819 bool AsmParser::parseDirectiveDS(StringRef IDVal, unsigned Size) {
4820 SMLoc NumValuesLoc = Lexer.getLoc();
4822 if (checkForValidSection() || parseAbsoluteExpression(NumValues))
4825 if (NumValues < 0) {
4826 Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect");
4830 if (parseToken(AsmToken::EndOfStatement,
4831 "unexpected token in '" + Twine(IDVal) + "' directive"))
4834 for (uint64_t i = 0, e = NumValues; i != e; ++i)
4835 getStreamer().emitFill(Size, 0);
4840 /// parseDirectiveLEB128
4841 /// ::= (.sleb128 | .uleb128) [ expression (, expression)* ]
4842 bool AsmParser::parseDirectiveLEB128(bool Signed) {
4843 if (checkForValidSection())
4846 auto parseOp = [&]() -> bool {
4847 const MCExpr *Value;
4848 if (parseExpression(Value))
4851 getStreamer().emitSLEB128Value(Value);
4853 getStreamer().emitULEB128Value(Value);
4857 if (parseMany(parseOp))
4858 return addErrorSuffix(" in directive");
4863 /// parseDirectiveSymbolAttribute
4864 /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
4865 bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
4866 auto parseOp = [&]() -> bool {
4868 SMLoc Loc = getTok().getLoc();
4869 if (parseIdentifier(Name))
4870 return Error(Loc, "expected identifier");
4871 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
4873 // Assembler local symbols don't make any sense here. Complain loudly.
4874 if (Sym->isTemporary())
4875 return Error(Loc, "non-local symbol required");
4877 if (!getStreamer().emitSymbolAttribute(Sym, Attr))
4878 return Error(Loc, "unable to emit symbol attribute");
4882 if (parseMany(parseOp))
4883 return addErrorSuffix(" in directive");
4887 /// parseDirectiveComm
4888 /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
4889 bool AsmParser::parseDirectiveComm(bool IsLocal) {
4890 if (checkForValidSection())
4893 SMLoc IDLoc = getLexer().getLoc();
4895 if (parseIdentifier(Name))
4896 return TokError("expected identifier in directive");
4898 // Handle the identifier as the key symbol.
4899 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
4901 if (getLexer().isNot(AsmToken::Comma))
4902 return TokError("unexpected token in directive");
4906 SMLoc SizeLoc = getLexer().getLoc();
4907 if (parseAbsoluteExpression(Size))
4910 int64_t Pow2Alignment = 0;
4911 SMLoc Pow2AlignmentLoc;
4912 if (getLexer().is(AsmToken::Comma)) {
4914 Pow2AlignmentLoc = getLexer().getLoc();
4915 if (parseAbsoluteExpression(Pow2Alignment))
4918 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
4919 if (IsLocal && LCOMM == LCOMM::NoAlignment)
4920 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
4922 // If this target takes alignments in bytes (not log) validate and convert.
4923 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
4924 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
4925 if (!isPowerOf2_64(Pow2Alignment))
4926 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
4927 Pow2Alignment = Log2_64(Pow2Alignment);
4931 if (parseToken(AsmToken::EndOfStatement,
4932 "unexpected token in '.comm' or '.lcomm' directive"))
4935 // NOTE: a size of zero for a .comm should create a undefined symbol
4936 // but a size of .lcomm creates a bss symbol of size zero.
4938 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
4939 "be less than zero");
4941 // NOTE: The alignment in the directive is a power of 2 value, the assembler
4942 // may internally end up wanting an alignment in bytes.
4943 // FIXME: Diagnose overflow.
4944 if (Pow2Alignment < 0)
4945 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
4946 "alignment, can't be less than zero");
4948 Sym->redefineIfPossible();
4949 if (!Sym->isUndefined())
4950 return Error(IDLoc, "invalid symbol redefinition");
4952 // Create the Symbol as a common or local common with Size and Pow2Alignment
4954 getStreamer().emitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
4958 getStreamer().emitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
4962 /// parseDirectiveAbort
4963 /// ::= .abort [... message ...]
4964 bool AsmParser::parseDirectiveAbort() {
4965 // FIXME: Use loc from directive.
4966 SMLoc Loc = getLexer().getLoc();
4968 StringRef Str = parseStringToEndOfStatement();
4969 if (parseToken(AsmToken::EndOfStatement,
4970 "unexpected token in '.abort' directive"))
4974 return Error(Loc, ".abort detected. Assembly stopping.");
4976 return Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
4977 // FIXME: Actually abort assembly here.
4982 /// parseDirectiveInclude
4983 /// ::= .include "filename"
4984 bool AsmParser::parseDirectiveInclude() {
4985 // Allow the strings to have escaped octal character sequence.
4986 std::string Filename;
4987 SMLoc IncludeLoc = getTok().getLoc();
4989 if (check(getTok().isNot(AsmToken::String),
4990 "expected string in '.include' directive") ||
4991 parseEscapedString(Filename) ||
4992 check(getTok().isNot(AsmToken::EndOfStatement),
4993 "unexpected token in '.include' directive") ||
4994 // Attempt to switch the lexer to the included file before consuming the
4995 // end of statement to avoid losing it when we switch.
4996 check(enterIncludeFile(Filename), IncludeLoc,
4997 "Could not find include file '" + Filename + "'"))
5003 /// parseDirectiveIncbin
5004 /// ::= .incbin "filename" [ , skip [ , count ] ]
5005 bool AsmParser::parseDirectiveIncbin() {
5006 // Allow the strings to have escaped octal character sequence.
5007 std::string Filename;
5008 SMLoc IncbinLoc = getTok().getLoc();
5009 if (check(getTok().isNot(AsmToken::String),
5010 "expected string in '.incbin' directive") ||
5011 parseEscapedString(Filename))
5015 const MCExpr *Count = nullptr;
5016 SMLoc SkipLoc, CountLoc;
5017 if (parseOptionalToken(AsmToken::Comma)) {
5018 // The skip expression can be omitted while specifying the count, e.g:
5019 // .incbin "filename",,4
5020 if (getTok().isNot(AsmToken::Comma)) {
5021 if (parseTokenLoc(SkipLoc) || parseAbsoluteExpression(Skip))
5024 if (parseOptionalToken(AsmToken::Comma)) {
5025 CountLoc = getTok().getLoc();
5026 if (parseExpression(Count))
5031 if (parseToken(AsmToken::EndOfStatement,
5032 "unexpected token in '.incbin' directive"))
5035 if (check(Skip < 0, SkipLoc, "skip is negative"))
5038 // Attempt to process the included file.
5039 if (processIncbinFile(Filename, Skip, Count, CountLoc))
5040 return Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
5044 /// parseDirectiveIf
5045 /// ::= .if{,eq,ge,gt,le,lt,ne} expression
5046 bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
5047 TheCondStack.push_back(TheCondState);
5048 TheCondState.TheCond = AsmCond::IfCond;
5049 if (TheCondState.Ignore) {
5050 eatToEndOfStatement();
5053 if (parseAbsoluteExpression(ExprValue) ||
5054 parseToken(AsmToken::EndOfStatement,
5055 "unexpected token in '.if' directive"))
5060 llvm_unreachable("unsupported directive");
5065 ExprValue = ExprValue == 0;
5068 ExprValue = ExprValue >= 0;
5071 ExprValue = ExprValue > 0;
5074 ExprValue = ExprValue <= 0;
5077 ExprValue = ExprValue < 0;
5081 TheCondState.CondMet = ExprValue;
5082 TheCondState.Ignore = !TheCondState.CondMet;
5088 /// parseDirectiveIfb
5090 bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
5091 TheCondStack.push_back(TheCondState);
5092 TheCondState.TheCond = AsmCond::IfCond;
5094 if (TheCondState.Ignore) {
5095 eatToEndOfStatement();
5097 StringRef Str = parseStringToEndOfStatement();
5099 if (parseToken(AsmToken::EndOfStatement,
5100 "unexpected token in '.ifb' directive"))
5103 TheCondState.CondMet = ExpectBlank == Str.empty();
5104 TheCondState.Ignore = !TheCondState.CondMet;
5110 /// parseDirectiveIfc
5111 /// ::= .ifc string1, string2
5112 /// ::= .ifnc string1, string2
5113 bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
5114 TheCondStack.push_back(TheCondState);
5115 TheCondState.TheCond = AsmCond::IfCond;
5117 if (TheCondState.Ignore) {
5118 eatToEndOfStatement();
5120 StringRef Str1 = parseStringToComma();
5122 if (parseToken(AsmToken::Comma, "unexpected token in '.ifc' directive"))
5125 StringRef Str2 = parseStringToEndOfStatement();
5127 if (parseToken(AsmToken::EndOfStatement,
5128 "unexpected token in '.ifc' directive"))
5131 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
5132 TheCondState.Ignore = !TheCondState.CondMet;
5138 /// parseDirectiveIfeqs
5139 /// ::= .ifeqs string1, string2
5140 bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) {
5141 if (Lexer.isNot(AsmToken::String)) {
5143 return TokError("expected string parameter for '.ifeqs' directive");
5144 return TokError("expected string parameter for '.ifnes' directive");
5147 StringRef String1 = getTok().getStringContents();
5150 if (Lexer.isNot(AsmToken::Comma)) {
5153 "expected comma after first string for '.ifeqs' directive");
5154 return TokError("expected comma after first string for '.ifnes' directive");
5159 if (Lexer.isNot(AsmToken::String)) {
5161 return TokError("expected string parameter for '.ifeqs' directive");
5162 return TokError("expected string parameter for '.ifnes' directive");
5165 StringRef String2 = getTok().getStringContents();
5168 TheCondStack.push_back(TheCondState);
5169 TheCondState.TheCond = AsmCond::IfCond;
5170 TheCondState.CondMet = ExpectEqual == (String1 == String2);
5171 TheCondState.Ignore = !TheCondState.CondMet;
5176 /// parseDirectiveIfdef
5177 /// ::= .ifdef symbol
5178 bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
5180 TheCondStack.push_back(TheCondState);
5181 TheCondState.TheCond = AsmCond::IfCond;
5183 if (TheCondState.Ignore) {
5184 eatToEndOfStatement();
5186 if (check(parseIdentifier(Name), "expected identifier after '.ifdef'") ||
5187 parseToken(AsmToken::EndOfStatement, "unexpected token in '.ifdef'"))
5190 MCSymbol *Sym = getContext().lookupSymbol(Name);
5193 TheCondState.CondMet = (Sym && !Sym->isUndefined(false));
5195 TheCondState.CondMet = (!Sym || Sym->isUndefined(false));
5196 TheCondState.Ignore = !TheCondState.CondMet;
5202 /// parseDirectiveElseIf
5203 /// ::= .elseif expression
5204 bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
5205 if (TheCondState.TheCond != AsmCond::IfCond &&
5206 TheCondState.TheCond != AsmCond::ElseIfCond)
5207 return Error(DirectiveLoc, "Encountered a .elseif that doesn't follow an"
5208 " .if or an .elseif");
5209 TheCondState.TheCond = AsmCond::ElseIfCond;
5211 bool LastIgnoreState = false;
5212 if (!TheCondStack.empty())
5213 LastIgnoreState = TheCondStack.back().Ignore;
5214 if (LastIgnoreState || TheCondState.CondMet) {
5215 TheCondState.Ignore = true;
5216 eatToEndOfStatement();
5219 if (parseAbsoluteExpression(ExprValue))
5222 if (parseToken(AsmToken::EndOfStatement,
5223 "unexpected token in '.elseif' directive"))
5226 TheCondState.CondMet = ExprValue;
5227 TheCondState.Ignore = !TheCondState.CondMet;
5233 /// parseDirectiveElse
5235 bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
5236 if (parseToken(AsmToken::EndOfStatement,
5237 "unexpected token in '.else' directive"))
5240 if (TheCondState.TheCond != AsmCond::IfCond &&
5241 TheCondState.TheCond != AsmCond::ElseIfCond)
5242 return Error(DirectiveLoc, "Encountered a .else that doesn't follow "
5243 " an .if or an .elseif");
5244 TheCondState.TheCond = AsmCond::ElseCond;
5245 bool LastIgnoreState = false;
5246 if (!TheCondStack.empty())
5247 LastIgnoreState = TheCondStack.back().Ignore;
5248 if (LastIgnoreState || TheCondState.CondMet)
5249 TheCondState.Ignore = true;
5251 TheCondState.Ignore = false;
5256 /// parseDirectiveEnd
5258 bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
5259 if (parseToken(AsmToken::EndOfStatement,
5260 "unexpected token in '.end' directive"))
5263 while (Lexer.isNot(AsmToken::Eof))
5269 /// parseDirectiveError
5271 /// ::= .error [string]
5272 bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
5273 if (!TheCondStack.empty()) {
5274 if (TheCondStack.back().Ignore) {
5275 eatToEndOfStatement();
5281 return Error(L, ".err encountered");
5283 StringRef Message = ".error directive invoked in source file";
5284 if (Lexer.isNot(AsmToken::EndOfStatement)) {
5285 if (Lexer.isNot(AsmToken::String))
5286 return TokError(".error argument must be a string");
5288 Message = getTok().getStringContents();
5292 return Error(L, Message);
5295 /// parseDirectiveWarning
5296 /// ::= .warning [string]
5297 bool AsmParser::parseDirectiveWarning(SMLoc L) {
5298 if (!TheCondStack.empty()) {
5299 if (TheCondStack.back().Ignore) {
5300 eatToEndOfStatement();
5305 StringRef Message = ".warning directive invoked in source file";
5307 if (!parseOptionalToken(AsmToken::EndOfStatement)) {
5308 if (Lexer.isNot(AsmToken::String))
5309 return TokError(".warning argument must be a string");
5311 Message = getTok().getStringContents();
5313 if (parseToken(AsmToken::EndOfStatement,
5314 "expected end of statement in '.warning' directive"))
5318 return Warning(L, Message);
5321 /// parseDirectiveEndIf
5323 bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
5324 if (parseToken(AsmToken::EndOfStatement,
5325 "unexpected token in '.endif' directive"))
5328 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
5329 return Error(DirectiveLoc, "Encountered a .endif that doesn't follow "
5331 if (!TheCondStack.empty()) {
5332 TheCondState = TheCondStack.back();
5333 TheCondStack.pop_back();
5339 void AsmParser::initializeDirectiveKindMap() {
5340 /* Lookup will be done with the directive
5341 * converted to lower case, so all these
5342 * keys should be lower case.
5343 * (target specific directives are handled
5346 DirectiveKindMap[".set"] = DK_SET;
5347 DirectiveKindMap[".equ"] = DK_EQU;
5348 DirectiveKindMap[".equiv"] = DK_EQUIV;
5349 DirectiveKindMap[".ascii"] = DK_ASCII;
5350 DirectiveKindMap[".asciz"] = DK_ASCIZ;
5351 DirectiveKindMap[".string"] = DK_STRING;
5352 DirectiveKindMap[".byte"] = DK_BYTE;
5353 DirectiveKindMap[".short"] = DK_SHORT;
5354 DirectiveKindMap[".value"] = DK_VALUE;
5355 DirectiveKindMap[".2byte"] = DK_2BYTE;
5356 DirectiveKindMap[".long"] = DK_LONG;
5357 DirectiveKindMap[".int"] = DK_INT;
5358 DirectiveKindMap[".4byte"] = DK_4BYTE;
5359 DirectiveKindMap[".quad"] = DK_QUAD;
5360 DirectiveKindMap[".8byte"] = DK_8BYTE;
5361 DirectiveKindMap[".octa"] = DK_OCTA;
5362 DirectiveKindMap[".single"] = DK_SINGLE;
5363 DirectiveKindMap[".float"] = DK_FLOAT;
5364 DirectiveKindMap[".double"] = DK_DOUBLE;
5365 DirectiveKindMap[".align"] = DK_ALIGN;
5366 DirectiveKindMap[".align32"] = DK_ALIGN32;
5367 DirectiveKindMap[".balign"] = DK_BALIGN;
5368 DirectiveKindMap[".balignw"] = DK_BALIGNW;
5369 DirectiveKindMap[".balignl"] = DK_BALIGNL;
5370 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
5371 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
5372 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
5373 DirectiveKindMap[".org"] = DK_ORG;
5374 DirectiveKindMap[".fill"] = DK_FILL;
5375 DirectiveKindMap[".zero"] = DK_ZERO;
5376 DirectiveKindMap[".extern"] = DK_EXTERN;
5377 DirectiveKindMap[".globl"] = DK_GLOBL;
5378 DirectiveKindMap[".global"] = DK_GLOBAL;
5379 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
5380 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
5381 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
5382 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
5383 DirectiveKindMap[".reference"] = DK_REFERENCE;
5384 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
5385 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
5386 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
5387 DirectiveKindMap[".cold"] = DK_COLD;
5388 DirectiveKindMap[".comm"] = DK_COMM;
5389 DirectiveKindMap[".common"] = DK_COMMON;
5390 DirectiveKindMap[".lcomm"] = DK_LCOMM;
5391 DirectiveKindMap[".abort"] = DK_ABORT;
5392 DirectiveKindMap[".include"] = DK_INCLUDE;
5393 DirectiveKindMap[".incbin"] = DK_INCBIN;
5394 DirectiveKindMap[".code16"] = DK_CODE16;
5395 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
5396 DirectiveKindMap[".rept"] = DK_REPT;
5397 DirectiveKindMap[".rep"] = DK_REPT;
5398 DirectiveKindMap[".irp"] = DK_IRP;
5399 DirectiveKindMap[".irpc"] = DK_IRPC;
5400 DirectiveKindMap[".endr"] = DK_ENDR;
5401 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
5402 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
5403 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
5404 DirectiveKindMap[".if"] = DK_IF;
5405 DirectiveKindMap[".ifeq"] = DK_IFEQ;
5406 DirectiveKindMap[".ifge"] = DK_IFGE;
5407 DirectiveKindMap[".ifgt"] = DK_IFGT;
5408 DirectiveKindMap[".ifle"] = DK_IFLE;
5409 DirectiveKindMap[".iflt"] = DK_IFLT;
5410 DirectiveKindMap[".ifne"] = DK_IFNE;
5411 DirectiveKindMap[".ifb"] = DK_IFB;
5412 DirectiveKindMap[".ifnb"] = DK_IFNB;
5413 DirectiveKindMap[".ifc"] = DK_IFC;
5414 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
5415 DirectiveKindMap[".ifnc"] = DK_IFNC;
5416 DirectiveKindMap[".ifnes"] = DK_IFNES;
5417 DirectiveKindMap[".ifdef"] = DK_IFDEF;
5418 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
5419 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
5420 DirectiveKindMap[".elseif"] = DK_ELSEIF;
5421 DirectiveKindMap[".else"] = DK_ELSE;
5422 DirectiveKindMap[".end"] = DK_END;
5423 DirectiveKindMap[".endif"] = DK_ENDIF;
5424 DirectiveKindMap[".skip"] = DK_SKIP;
5425 DirectiveKindMap[".space"] = DK_SPACE;
5426 DirectiveKindMap[".file"] = DK_FILE;
5427 DirectiveKindMap[".line"] = DK_LINE;
5428 DirectiveKindMap[".loc"] = DK_LOC;
5429 DirectiveKindMap[".stabs"] = DK_STABS;
5430 DirectiveKindMap[".cv_file"] = DK_CV_FILE;
5431 DirectiveKindMap[".cv_func_id"] = DK_CV_FUNC_ID;
5432 DirectiveKindMap[".cv_loc"] = DK_CV_LOC;
5433 DirectiveKindMap[".cv_linetable"] = DK_CV_LINETABLE;
5434 DirectiveKindMap[".cv_inline_linetable"] = DK_CV_INLINE_LINETABLE;
5435 DirectiveKindMap[".cv_inline_site_id"] = DK_CV_INLINE_SITE_ID;
5436 DirectiveKindMap[".cv_def_range"] = DK_CV_DEF_RANGE;
5437 DirectiveKindMap[".cv_string"] = DK_CV_STRING;
5438 DirectiveKindMap[".cv_stringtable"] = DK_CV_STRINGTABLE;
5439 DirectiveKindMap[".cv_filechecksums"] = DK_CV_FILECHECKSUMS;
5440 DirectiveKindMap[".cv_filechecksumoffset"] = DK_CV_FILECHECKSUM_OFFSET;
5441 DirectiveKindMap[".cv_fpo_data"] = DK_CV_FPO_DATA;
5442 DirectiveKindMap[".sleb128"] = DK_SLEB128;
5443 DirectiveKindMap[".uleb128"] = DK_ULEB128;
5444 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
5445 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
5446 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
5447 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
5448 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
5449 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
5450 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
5451 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
5452 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
5453 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
5454 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
5455 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
5456 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
5457 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
5458 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
5459 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
5460 DirectiveKindMap[".cfi_return_column"] = DK_CFI_RETURN_COLUMN;
5461 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
5462 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
5463 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
5464 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
5465 DirectiveKindMap[".cfi_b_key_frame"] = DK_CFI_B_KEY_FRAME;
5466 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
5467 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
5468 DirectiveKindMap[".macro"] = DK_MACRO;
5469 DirectiveKindMap[".exitm"] = DK_EXITM;
5470 DirectiveKindMap[".endm"] = DK_ENDM;
5471 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
5472 DirectiveKindMap[".purgem"] = DK_PURGEM;
5473 DirectiveKindMap[".err"] = DK_ERR;
5474 DirectiveKindMap[".error"] = DK_ERROR;
5475 DirectiveKindMap[".warning"] = DK_WARNING;
5476 DirectiveKindMap[".altmacro"] = DK_ALTMACRO;
5477 DirectiveKindMap[".noaltmacro"] = DK_NOALTMACRO;
5478 DirectiveKindMap[".reloc"] = DK_RELOC;
5479 DirectiveKindMap[".dc"] = DK_DC;
5480 DirectiveKindMap[".dc.a"] = DK_DC_A;
5481 DirectiveKindMap[".dc.b"] = DK_DC_B;
5482 DirectiveKindMap[".dc.d"] = DK_DC_D;
5483 DirectiveKindMap[".dc.l"] = DK_DC_L;
5484 DirectiveKindMap[".dc.s"] = DK_DC_S;
5485 DirectiveKindMap[".dc.w"] = DK_DC_W;
5486 DirectiveKindMap[".dc.x"] = DK_DC_X;
5487 DirectiveKindMap[".dcb"] = DK_DCB;
5488 DirectiveKindMap[".dcb.b"] = DK_DCB_B;
5489 DirectiveKindMap[".dcb.d"] = DK_DCB_D;
5490 DirectiveKindMap[".dcb.l"] = DK_DCB_L;
5491 DirectiveKindMap[".dcb.s"] = DK_DCB_S;
5492 DirectiveKindMap[".dcb.w"] = DK_DCB_W;
5493 DirectiveKindMap[".dcb.x"] = DK_DCB_X;
5494 DirectiveKindMap[".ds"] = DK_DS;
5495 DirectiveKindMap[".ds.b"] = DK_DS_B;
5496 DirectiveKindMap[".ds.d"] = DK_DS_D;
5497 DirectiveKindMap[".ds.l"] = DK_DS_L;
5498 DirectiveKindMap[".ds.p"] = DK_DS_P;
5499 DirectiveKindMap[".ds.s"] = DK_DS_S;
5500 DirectiveKindMap[".ds.w"] = DK_DS_W;
5501 DirectiveKindMap[".ds.x"] = DK_DS_X;
5502 DirectiveKindMap[".print"] = DK_PRINT;
5503 DirectiveKindMap[".addrsig"] = DK_ADDRSIG;
5504 DirectiveKindMap[".addrsig_sym"] = DK_ADDRSIG_SYM;
5507 MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
5508 AsmToken EndToken, StartToken = getTok();
5510 unsigned NestLevel = 0;
5512 // Check whether we have reached the end of the file.
5513 if (getLexer().is(AsmToken::Eof)) {
5514 printError(DirectiveLoc, "no matching '.endr' in definition");
5518 if (Lexer.is(AsmToken::Identifier) &&
5519 (getTok().getIdentifier() == ".rep" ||
5520 getTok().getIdentifier() == ".rept" ||
5521 getTok().getIdentifier() == ".irp" ||
5522 getTok().getIdentifier() == ".irpc")) {
5526 // Otherwise, check whether we have reached the .endr.
5527 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
5528 if (NestLevel == 0) {
5529 EndToken = getTok();
5531 if (Lexer.isNot(AsmToken::EndOfStatement)) {
5532 printError(getTok().getLoc(),
5533 "unexpected token in '.endr' directive");
5541 // Otherwise, scan till the end of the statement.
5542 eatToEndOfStatement();
5545 const char *BodyStart = StartToken.getLoc().getPointer();
5546 const char *BodyEnd = EndToken.getLoc().getPointer();
5547 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
5549 // We Are Anonymous.
5550 MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters());
5551 return &MacroLikeBodies.back();
5554 void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
5555 raw_svector_ostream &OS) {
5558 std::unique_ptr<MemoryBuffer> Instantiation =
5559 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
5561 // Create the macro instantiation object and add to the current macro
5562 // instantiation stack.
5563 MacroInstantiation *MI = new MacroInstantiation{
5564 DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()};
5565 ActiveMacros.push_back(MI);
5567 // Jump to the macro instantiation and prime the lexer.
5568 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
5569 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
5573 /// parseDirectiveRept
5574 /// ::= .rep | .rept count
5575 bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
5576 const MCExpr *CountExpr;
5577 SMLoc CountLoc = getTok().getLoc();
5578 if (parseExpression(CountExpr))
5582 if (!CountExpr->evaluateAsAbsolute(Count, getStreamer().getAssemblerPtr())) {
5583 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
5586 if (check(Count < 0, CountLoc, "Count is negative") ||
5587 parseToken(AsmToken::EndOfStatement,
5588 "unexpected token in '" + Dir + "' directive"))
5591 // Lex the rept definition.
5592 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
5596 // Macro instantiation is lexical, unfortunately. We construct a new buffer
5597 // to hold the macro body with substitutions.
5598 SmallString<256> Buf;
5599 raw_svector_ostream OS(Buf);
5601 // Note that the AtPseudoVariable is disabled for instantiations of .rep(t).
5602 if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc()))
5605 instantiateMacroLikeBody(M, DirectiveLoc, OS);
5610 /// parseDirectiveIrp
5611 /// ::= .irp symbol,values
5612 bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
5613 MCAsmMacroParameter Parameter;
5614 MCAsmMacroArguments A;
5615 if (check(parseIdentifier(Parameter.Name),
5616 "expected identifier in '.irp' directive") ||
5617 parseToken(AsmToken::Comma, "expected comma in '.irp' directive") ||
5618 parseMacroArguments(nullptr, A) ||
5619 parseToken(AsmToken::EndOfStatement, "expected End of Statement"))
5622 // Lex the irp definition.
5623 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
5627 // Macro instantiation is lexical, unfortunately. We construct a new buffer
5628 // to hold the macro body with substitutions.
5629 SmallString<256> Buf;
5630 raw_svector_ostream OS(Buf);
5632 for (const MCAsmMacroArgument &Arg : A) {
5633 // Note that the AtPseudoVariable is enabled for instantiations of .irp.
5634 // This is undocumented, but GAS seems to support it.
5635 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
5639 instantiateMacroLikeBody(M, DirectiveLoc, OS);
5644 /// parseDirectiveIrpc
5645 /// ::= .irpc symbol,values
5646 bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
5647 MCAsmMacroParameter Parameter;
5648 MCAsmMacroArguments A;
5650 if (check(parseIdentifier(Parameter.Name),
5651 "expected identifier in '.irpc' directive") ||
5652 parseToken(AsmToken::Comma, "expected comma in '.irpc' directive") ||
5653 parseMacroArguments(nullptr, A))
5656 if (A.size() != 1 || A.front().size() != 1)
5657 return TokError("unexpected token in '.irpc' directive");
5659 // Eat the end of statement.
5660 if (parseToken(AsmToken::EndOfStatement, "expected end of statement"))
5663 // Lex the irpc definition.
5664 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
5668 // Macro instantiation is lexical, unfortunately. We construct a new buffer
5669 // to hold the macro body with substitutions.
5670 SmallString<256> Buf;
5671 raw_svector_ostream OS(Buf);
5673 StringRef Values = A.front().front().getString();
5674 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
5675 MCAsmMacroArgument Arg;
5676 Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1));
5678 // Note that the AtPseudoVariable is enabled for instantiations of .irpc.
5679 // This is undocumented, but GAS seems to support it.
5680 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc()))
5684 instantiateMacroLikeBody(M, DirectiveLoc, OS);
5689 bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
5690 if (ActiveMacros.empty())
5691 return TokError("unmatched '.endr' directive");
5693 // The only .repl that should get here are the ones created by
5694 // instantiateMacroLikeBody.
5695 assert(getLexer().is(AsmToken::EndOfStatement));
5701 bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
5703 const MCExpr *Value;
5704 SMLoc ExprLoc = getLexer().getLoc();
5705 if (parseExpression(Value))
5707 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
5709 return Error(ExprLoc, "unexpected expression in _emit");
5710 uint64_t IntValue = MCE->getValue();
5711 if (!isUInt<8>(IntValue) && !isInt<8>(IntValue))
5712 return Error(ExprLoc, "literal value out of range for directive");
5714 Info.AsmRewrites->emplace_back(AOK_Emit, IDLoc, Len);
5718 bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
5719 const MCExpr *Value;
5720 SMLoc ExprLoc = getLexer().getLoc();
5721 if (parseExpression(Value))
5723 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
5725 return Error(ExprLoc, "unexpected expression in align");
5726 uint64_t IntValue = MCE->getValue();
5727 if (!isPowerOf2_64(IntValue))
5728 return Error(ExprLoc, "literal value not a power of two greater then zero");
5730 Info.AsmRewrites->emplace_back(AOK_Align, IDLoc, 5, Log2_64(IntValue));
5734 bool AsmParser::parseDirectivePrint(SMLoc DirectiveLoc) {
5735 const AsmToken StrTok = getTok();
5737 if (StrTok.isNot(AsmToken::String) || StrTok.getString().front() != '"')
5738 return Error(DirectiveLoc, "expected double quoted string after .print");
5739 if (parseToken(AsmToken::EndOfStatement, "expected end of statement"))
5741 llvm::outs() << StrTok.getStringContents() << '\n';
5745 bool AsmParser::parseDirectiveAddrsig() {
5746 getStreamer().emitAddrsig();
5750 bool AsmParser::parseDirectiveAddrsigSym() {
5752 if (check(parseIdentifier(Name),
5753 "expected identifier in '.addrsig_sym' directive"))
5755 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
5756 getStreamer().emitAddrsigSym(Sym);
5760 // We are comparing pointers, but the pointers are relative to a single string.
5761 // Thus, this should always be deterministic.
5762 static int rewritesSort(const AsmRewrite *AsmRewriteA,
5763 const AsmRewrite *AsmRewriteB) {
5764 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
5766 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
5769 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
5770 // rewrite to the same location. Make sure the SizeDirective rewrite is
5771 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
5772 // ensures the sort algorithm is stable.
5773 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
5774 AsmRewritePrecedence[AsmRewriteB->Kind])
5777 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
5778 AsmRewritePrecedence[AsmRewriteB->Kind])
5780 llvm_unreachable("Unstable rewrite sort.");
5783 bool AsmParser::parseMSInlineAsm(
5784 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
5785 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool>> &OpDecls,
5786 SmallVectorImpl<std::string> &Constraints,
5787 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
5788 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
5789 SmallVector<void *, 4> InputDecls;
5790 SmallVector<void *, 4> OutputDecls;
5791 SmallVector<bool, 4> InputDeclsAddressOf;
5792 SmallVector<bool, 4> OutputDeclsAddressOf;
5793 SmallVector<std::string, 4> InputConstraints;
5794 SmallVector<std::string, 4> OutputConstraints;
5795 SmallVector<unsigned, 4> ClobberRegs;
5797 SmallVector<AsmRewrite, 4> AsmStrRewrites;
5802 // While we have input, parse each statement.
5803 unsigned InputIdx = 0;
5804 unsigned OutputIdx = 0;
5805 while (getLexer().isNot(AsmToken::Eof)) {
5806 // Parse curly braces marking block start/end
5807 if (parseCurlyBlockScope(AsmStrRewrites))
5810 ParseStatementInfo Info(&AsmStrRewrites);
5811 bool StatementErr = parseStatement(Info, &SI);
5813 if (StatementErr || Info.ParseError) {
5814 // Emit pending errors if any exist.
5815 printPendingErrors();
5819 // No pending error should exist here.
5820 assert(!hasPendingError() && "unexpected error from parseStatement");
5822 if (Info.Opcode == ~0U)
5825 const MCInstrDesc &Desc = MII->get(Info.Opcode);
5827 // Build the list of clobbers, outputs and inputs.
5828 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
5829 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
5831 // Register operand.
5832 if (Operand.isReg() && !Operand.needAddressOf() &&
5833 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
5834 unsigned NumDefs = Desc.getNumDefs();
5836 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
5837 ClobberRegs.push_back(Operand.getReg());
5841 // Expr/Input or Output.
5842 StringRef SymName = Operand.getSymName();
5843 if (SymName.empty())
5846 void *OpDecl = Operand.getOpDecl();
5850 StringRef Constraint = Operand.getConstraint();
5851 if (Operand.isImm()) {
5852 // Offset as immediate
5853 if (Operand.isOffsetOfLocal())
5859 bool isOutput = (i == 1) && Desc.mayStore();
5860 SMLoc Start = SMLoc::getFromPointer(SymName.data());
5863 OutputDecls.push_back(OpDecl);
5864 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
5865 OutputConstraints.push_back(("=" + Constraint).str());
5866 AsmStrRewrites.emplace_back(AOK_Output, Start, SymName.size());
5868 InputDecls.push_back(OpDecl);
5869 InputDeclsAddressOf.push_back(Operand.needAddressOf());
5870 InputConstraints.push_back(Constraint.str());
5871 if (Desc.OpInfo[i - 1].isBranchTarget())
5872 AsmStrRewrites.emplace_back(AOK_CallInput, Start, SymName.size());
5874 AsmStrRewrites.emplace_back(AOK_Input, Start, SymName.size());
5878 // Consider implicit defs to be clobbers. Think of cpuid and push.
5879 ArrayRef<MCPhysReg> ImpDefs(Desc.getImplicitDefs(),
5880 Desc.getNumImplicitDefs());
5881 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
5884 // Set the number of Outputs and Inputs.
5885 NumOutputs = OutputDecls.size();
5886 NumInputs = InputDecls.size();
5888 // Set the unique clobbers.
5889 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
5890 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
5892 Clobbers.assign(ClobberRegs.size(), std::string());
5893 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
5894 raw_string_ostream OS(Clobbers[I]);
5895 IP->printRegName(OS, ClobberRegs[I]);
5898 // Merge the various outputs and inputs. Output are expected first.
5899 if (NumOutputs || NumInputs) {
5900 unsigned NumExprs = NumOutputs + NumInputs;
5901 OpDecls.resize(NumExprs);
5902 Constraints.resize(NumExprs);
5903 for (unsigned i = 0; i < NumOutputs; ++i) {
5904 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
5905 Constraints[i] = OutputConstraints[i];
5907 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
5908 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
5909 Constraints[j] = InputConstraints[i];
5913 // Build the IR assembly string.
5914 std::string AsmStringIR;
5915 raw_string_ostream OS(AsmStringIR);
5916 StringRef ASMString =
5917 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
5918 const char *AsmStart = ASMString.begin();
5919 const char *AsmEnd = ASMString.end();
5920 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
5921 for (auto it = AsmStrRewrites.begin(); it != AsmStrRewrites.end(); ++it) {
5922 const AsmRewrite &AR = *it;
5923 // Check if this has already been covered by another rewrite...
5926 AsmRewriteKind Kind = AR.Kind;
5928 const char *Loc = AR.Loc.getPointer();
5929 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
5931 // Emit everything up to the immediate/expression.
5932 if (unsigned Len = Loc - AsmStart)
5933 OS << StringRef(AsmStart, Len);
5935 // Skip the original expression.
5936 if (Kind == AOK_Skip) {
5937 AsmStart = Loc + AR.Len;
5941 unsigned AdditionalSkip = 0;
5942 // Rewrite expressions in $N notation.
5947 assert(AR.IntelExp.isValid() && "cannot write invalid intel expression");
5948 if (AR.IntelExp.NeedBracs)
5950 if (AR.IntelExp.hasBaseReg())
5951 OS << AR.IntelExp.BaseReg;
5952 if (AR.IntelExp.hasIndexReg())
5953 OS << (AR.IntelExp.hasBaseReg() ? " + " : "")
5954 << AR.IntelExp.IndexReg;
5955 if (AR.IntelExp.Scale > 1)
5956 OS << " * $$" << AR.IntelExp.Scale;
5957 if (AR.IntelExp.hasOffset()) {
5958 if (AR.IntelExp.hasRegs())
5960 // Fuse this rewrite with a rewrite of the offset name, if present.
5961 StringRef OffsetName = AR.IntelExp.OffsetName;
5962 SMLoc OffsetLoc = SMLoc::getFromPointer(AR.IntelExp.OffsetName.data());
5963 size_t OffsetLen = OffsetName.size();
5964 auto rewrite_it = std::find_if(
5965 it, AsmStrRewrites.end(), [&](const AsmRewrite &FusingAR) {
5966 return FusingAR.Loc == OffsetLoc && FusingAR.Len == OffsetLen &&
5967 (FusingAR.Kind == AOK_Input ||
5968 FusingAR.Kind == AOK_CallInput);
5970 if (rewrite_it == AsmStrRewrites.end()) {
5971 OS << "offset " << OffsetName;
5972 } else if (rewrite_it->Kind == AOK_CallInput) {
5973 OS << "${" << InputIdx++ << ":P}";
5974 rewrite_it->Done = true;
5976 OS << '$' << InputIdx++;
5977 rewrite_it->Done = true;
5980 if (AR.IntelExp.Imm || AR.IntelExp.emitImm())
5981 OS << (AR.IntelExp.emitImm() ? "$$" : " + $$") << AR.IntelExp.Imm;
5982 if (AR.IntelExp.NeedBracs)
5986 OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
5989 OS << '$' << InputIdx++;
5992 OS << "${" << InputIdx++ << ":P}";
5995 OS << '$' << OutputIdx++;
5997 case AOK_SizeDirective:
6000 case 8: OS << "byte ptr "; break;
6001 case 16: OS << "word ptr "; break;
6002 case 32: OS << "dword ptr "; break;
6003 case 64: OS << "qword ptr "; break;
6004 case 80: OS << "xword ptr "; break;
6005 case 128: OS << "xmmword ptr "; break;
6006 case 256: OS << "ymmword ptr "; break;
6013 // MS alignment directives are measured in bytes. If the native assembler
6014 // measures alignment in bytes, we can pass it straight through.
6016 if (getContext().getAsmInfo()->getAlignmentIsInBytes())
6019 // Alignment is in log2 form, so print that instead and skip the original
6021 unsigned Val = AR.Val;
6023 assert(Val < 10 && "Expected alignment less then 2^10.");
6024 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
6030 case AOK_EndOfStatement:
6035 // Skip the original expression.
6036 AsmStart = Loc + AR.Len + AdditionalSkip;
6039 // Emit the remainder of the asm string.
6040 if (AsmStart != AsmEnd)
6041 OS << StringRef(AsmStart, AsmEnd - AsmStart);
6043 AsmString = OS.str();
6048 namespace MCParserUtils {
6050 /// Returns whether the given symbol is used anywhere in the given expression,
6051 /// or subexpressions.
6052 static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) {
6053 switch (Value->getKind()) {
6054 case MCExpr::Binary: {
6055 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
6056 return isSymbolUsedInExpression(Sym, BE->getLHS()) ||
6057 isSymbolUsedInExpression(Sym, BE->getRHS());
6059 case MCExpr::Target:
6060 case MCExpr::Constant:
6062 case MCExpr::SymbolRef: {
6064 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
6066 return isSymbolUsedInExpression(Sym, S.getVariableValue());
6070 return isSymbolUsedInExpression(
6071 Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
6074 llvm_unreachable("Unknown expr kind!");
6077 bool parseAssignmentExpression(StringRef Name, bool allow_redef,
6078 MCAsmParser &Parser, MCSymbol *&Sym,
6079 const MCExpr *&Value) {
6081 // FIXME: Use better location, we should use proper tokens.
6082 SMLoc EqualLoc = Parser.getTok().getLoc();
6083 if (Parser.parseExpression(Value))
6084 return Parser.TokError("missing expression");
6086 // Note: we don't count b as used in "a = b". This is to allow
6090 if (Parser.parseToken(AsmToken::EndOfStatement))
6093 // Validate that the LHS is allowed to be a variable (either it has not been
6094 // used as a symbol, or it is an absolute symbol).
6095 Sym = Parser.getContext().lookupSymbol(Name);
6097 // Diagnose assignment to a label.
6099 // FIXME: Diagnostics. Note the location of the definition as a label.
6100 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
6101 if (isSymbolUsedInExpression(Sym, Value))
6102 return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'");
6103 else if (Sym->isUndefined(/*SetUsed*/ false) && !Sym->isUsed() &&
6105 ; // Allow redefinitions of undefined symbols only used in directives.
6106 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
6107 ; // Allow redefinitions of variables that haven't yet been used.
6108 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
6109 return Parser.Error(EqualLoc, "redefinition of '" + Name + "'");
6110 else if (!Sym->isVariable())
6111 return Parser.Error(EqualLoc, "invalid assignment to '" + Name + "'");
6112 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
6113 return Parser.Error(EqualLoc,
6114 "invalid reassignment of non-absolute variable '" +
6116 } else if (Name == ".") {
6117 Parser.getStreamer().emitValueToOffset(Value, 0, EqualLoc);
6120 Sym = Parser.getContext().getOrCreateSymbol(Name);
6122 Sym->setRedefinable(allow_redef);
6127 } // end namespace MCParserUtils
6128 } // end namespace llvm
6130 /// Create an MCAsmParser instance.
6131 MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
6132 MCStreamer &Out, const MCAsmInfo &MAI,
6134 return new AsmParser(SM, C, Out, MAI, CB);