1 //===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class implements the parser for assembly files.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/APFloat.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCDwarf.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInstPrinter.h"
24 #include "llvm/MC/MCInstrInfo.h"
25 #include "llvm/MC/MCObjectFileInfo.h"
26 #include "llvm/MC/MCParser/AsmCond.h"
27 #include "llvm/MC/MCParser/AsmLexer.h"
28 #include "llvm/MC/MCParser/MCAsmParser.h"
29 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
30 #include "llvm/MC/MCRegisterInfo.h"
31 #include "llvm/MC/MCSectionMachO.h"
32 #include "llvm/MC/MCStreamer.h"
33 #include "llvm/MC/MCSymbol.h"
34 #include "llvm/MC/MCTargetAsmParser.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/MathExtras.h"
38 #include "llvm/Support/MemoryBuffer.h"
39 #include "llvm/Support/SourceMgr.h"
40 #include "llvm/Support/raw_ostream.h"
49 FatalAssemblerWarnings("fatal-assembler-warnings",
50 cl::desc("Consider warnings as error"));
52 MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
55 /// \brief Helper types for tracking macro definitions.
56 typedef std::vector<AsmToken> MCAsmMacroArgument;
57 typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
59 struct MCAsmMacroParameter {
61 MCAsmMacroArgument Value;
65 MCAsmMacroParameter() : Required(false), Vararg(false) {}
68 typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
73 MCAsmMacroParameters Parameters;
76 MCAsmMacro(StringRef N, StringRef B, ArrayRef<MCAsmMacroParameter> P) :
77 Name(N), Body(B), Parameters(P) {}
80 /// \brief Helper class for storing information about an active macro
82 struct MacroInstantiation {
83 /// The macro being instantiated.
84 const MCAsmMacro *TheMacro;
86 /// The macro instantiation with substitutions.
87 MemoryBuffer *Instantiation;
89 /// The location of the instantiation.
90 SMLoc InstantiationLoc;
92 /// The buffer where parsing should resume upon instantiation completion.
95 /// The location where parsing should resume upon instantiation completion.
99 MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB, SMLoc EL,
103 struct ParseStatementInfo {
104 /// \brief The parsed operands from the last parsed statement.
105 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands;
107 /// \brief The opcode from the last parsed instruction.
110 /// \brief Was there an error parsing the inline assembly?
113 SmallVectorImpl<AsmRewrite> *AsmRewrites;
115 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(nullptr) {}
116 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
117 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
120 /// \brief The concrete assembly parser instance.
121 class AsmParser : public MCAsmParser {
122 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
123 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
128 const MCAsmInfo &MAI;
130 SourceMgr::DiagHandlerTy SavedDiagHandler;
131 void *SavedDiagContext;
132 MCAsmParserExtension *PlatformParser;
134 /// This is the current buffer index we're lexing from as managed by the
135 /// SourceMgr object.
138 AsmCond TheCondState;
139 std::vector<AsmCond> TheCondStack;
141 /// \brief maps directive names to handler methods in parser
142 /// extensions. Extensions register themselves in this map by calling
143 /// addDirectiveHandler.
144 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
146 /// \brief Map of currently defined macros.
147 StringMap<MCAsmMacro*> MacroMap;
149 /// \brief Stack of active macro instantiations.
150 std::vector<MacroInstantiation*> ActiveMacros;
152 /// \brief List of bodies of anonymous macros.
153 std::deque<MCAsmMacro> MacroLikeBodies;
155 /// Boolean tracking whether macro substitution is enabled.
156 unsigned MacrosEnabledFlag : 1;
158 /// Flag tracking whether any errors have been encountered.
159 unsigned HadError : 1;
161 /// The values from the last parsed cpp hash file line comment if any.
162 StringRef CppHashFilename;
163 int64_t CppHashLineNumber;
166 /// When generating dwarf for assembly source files we need to calculate the
167 /// logical line number based on the last parsed cpp hash file line comment
168 /// and current line. Since this is slow and messes up the SourceMgr's
169 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
170 SMLoc LastQueryIDLoc;
171 unsigned LastQueryBuffer;
172 unsigned LastQueryLine;
174 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
175 unsigned AssemblerDialect;
177 /// \brief is Darwin compatibility enabled?
180 /// \brief Are we parsing ms-style inline assembly?
181 bool ParsingInlineAsm;
184 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
185 const MCAsmInfo &MAI);
186 virtual ~AsmParser();
188 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
190 void addDirectiveHandler(StringRef Directive,
191 ExtensionDirectiveHandler Handler) override {
192 ExtensionDirectiveMap[Directive] = Handler;
196 /// @name MCAsmParser Interface
199 SourceMgr &getSourceManager() override { return SrcMgr; }
200 MCAsmLexer &getLexer() override { return Lexer; }
201 MCContext &getContext() override { return Ctx; }
202 MCStreamer &getStreamer() override { return Out; }
203 unsigned getAssemblerDialect() override {
204 if (AssemblerDialect == ~0U)
205 return MAI.getAssemblerDialect();
207 return AssemblerDialect;
209 void setAssemblerDialect(unsigned i) override {
210 AssemblerDialect = i;
213 void Note(SMLoc L, const Twine &Msg,
214 ArrayRef<SMRange> Ranges = None) override;
215 bool Warning(SMLoc L, const Twine &Msg,
216 ArrayRef<SMRange> Ranges = None) override;
217 bool Error(SMLoc L, const Twine &Msg,
218 ArrayRef<SMRange> Ranges = None) override;
220 const AsmToken &Lex() override;
222 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
223 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
225 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
226 unsigned &NumOutputs, unsigned &NumInputs,
227 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
228 SmallVectorImpl<std::string> &Constraints,
229 SmallVectorImpl<std::string> &Clobbers,
230 const MCInstrInfo *MII, const MCInstPrinter *IP,
231 MCAsmParserSemaCallback &SI) override;
233 bool parseExpression(const MCExpr *&Res);
234 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
235 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
236 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
237 bool parseAbsoluteExpression(int64_t &Res) override;
239 /// \brief Parse an identifier or string (as a quoted identifier)
240 /// and set \p Res to the identifier contents.
241 bool parseIdentifier(StringRef &Res) override;
242 void eatToEndOfStatement() override;
244 void checkForValidSection() override;
249 bool parseStatement(ParseStatementInfo &Info);
250 void eatToEndOfLine();
251 bool parseCppHashLineFilenameComment(const SMLoc &L);
253 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
254 ArrayRef<MCAsmMacroParameter> Parameters);
255 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
256 ArrayRef<MCAsmMacroParameter> Parameters,
257 ArrayRef<MCAsmMacroArgument> A,
260 /// \brief Are macros enabled in the parser?
261 bool areMacrosEnabled() {return MacrosEnabledFlag;}
263 /// \brief Control a flag in the parser that enables or disables macros.
264 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
266 /// \brief Lookup a previously defined macro.
267 /// \param Name Macro name.
268 /// \returns Pointer to macro. NULL if no such macro was defined.
269 const MCAsmMacro* lookupMacro(StringRef Name);
271 /// \brief Define a new macro with the given name and information.
272 void defineMacro(StringRef Name, const MCAsmMacro& Macro);
274 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
275 void undefineMacro(StringRef Name);
277 /// \brief Are we inside a macro instantiation?
278 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
280 /// \brief Handle entry to macro instantiation.
282 /// \param M The macro.
283 /// \param NameLoc Instantiation location.
284 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
286 /// \brief Handle exit from macro instantiation.
287 void handleMacroExit();
289 /// \brief Extract AsmTokens for a macro argument.
290 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
292 /// \brief Parse all macro arguments for a given macro.
293 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
295 void printMacroInstantiations();
296 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
297 ArrayRef<SMRange> Ranges = None) const {
298 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
300 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
302 /// \brief Enter the specified file. This returns true on failure.
303 bool enterIncludeFile(const std::string &Filename);
305 /// \brief Process the specified file for the .incbin directive.
306 /// This returns true on failure.
307 bool processIncbinFile(const std::string &Filename);
309 /// \brief Reset the current lexer position to that given by \p Loc. The
310 /// current token is not set; clients should ensure Lex() is called
313 /// \param InBuffer If not 0, should be the known buffer id that contains the
315 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
317 /// \brief Parse up to the end of statement and a return the contents from the
318 /// current token until the end of the statement; the current token on exit
319 /// will be either the EndOfStatement or EOF.
320 StringRef parseStringToEndOfStatement() override;
322 /// \brief Parse until the end of a statement or a comma is encountered,
323 /// return the contents from the current token up to the end or comma.
324 StringRef parseStringToComma();
326 bool parseAssignment(StringRef Name, bool allow_redef,
327 bool NoDeadStrip = false);
329 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
330 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
331 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
333 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
335 // Generic (target and platform independent) directive parsing.
337 DK_NO_DIRECTIVE, // Placeholder
338 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
339 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
340 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
341 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
342 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
343 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
344 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
345 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
346 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
347 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
348 DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
349 DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF,
350 DK_ELSEIF, DK_ELSE, DK_ENDIF,
351 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
352 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
353 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
354 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
355 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
356 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
357 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
358 DK_MACROS_ON, DK_MACROS_OFF, DK_MACRO, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
359 DK_SLEB128, DK_ULEB128,
364 /// \brief Maps directive name --> DirectiveKind enum, for
365 /// directives parsed by this class.
366 StringMap<DirectiveKind> DirectiveKindMap;
368 // ".ascii", ".asciz", ".string"
369 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
370 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
371 bool parseDirectiveOctaValue(); // ".octa"
372 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
373 bool parseDirectiveFill(); // ".fill"
374 bool parseDirectiveZero(); // ".zero"
375 // ".set", ".equ", ".equiv"
376 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
377 bool parseDirectiveOrg(); // ".org"
378 // ".align{,32}", ".p2align{,w,l}"
379 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
381 // ".file", ".line", ".loc", ".stabs"
382 bool parseDirectiveFile(SMLoc DirectiveLoc);
383 bool parseDirectiveLine();
384 bool parseDirectiveLoc();
385 bool parseDirectiveStabs();
388 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
389 bool parseDirectiveCFIWindowSave();
390 bool parseDirectiveCFISections();
391 bool parseDirectiveCFIStartProc();
392 bool parseDirectiveCFIEndProc();
393 bool parseDirectiveCFIDefCfaOffset();
394 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
395 bool parseDirectiveCFIAdjustCfaOffset();
396 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
397 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
398 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
399 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
400 bool parseDirectiveCFIRememberState();
401 bool parseDirectiveCFIRestoreState();
402 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
403 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
404 bool parseDirectiveCFIEscape();
405 bool parseDirectiveCFISignalFrame();
406 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
409 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
410 bool parseDirectiveEndMacro(StringRef Directive);
411 bool parseDirectiveMacro(SMLoc DirectiveLoc);
412 bool parseDirectiveMacrosOnOff(StringRef Directive);
414 // ".bundle_align_mode"
415 bool parseDirectiveBundleAlignMode();
417 bool parseDirectiveBundleLock();
419 bool parseDirectiveBundleUnlock();
422 bool parseDirectiveSpace(StringRef IDVal);
424 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
425 bool parseDirectiveLEB128(bool Signed);
427 /// \brief Parse a directive like ".globl" which
428 /// accepts a single symbol (which should be a label or an external).
429 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
431 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
433 bool parseDirectiveAbort(); // ".abort"
434 bool parseDirectiveInclude(); // ".include"
435 bool parseDirectiveIncbin(); // ".incbin"
437 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
438 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
439 // ".ifb" or ".ifnb", depending on ExpectBlank.
440 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
441 // ".ifc" or ".ifnc", depending on ExpectEqual.
442 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
444 bool parseDirectiveIfeqs(SMLoc DirectiveLoc);
445 // ".ifdef" or ".ifndef", depending on expect_defined
446 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
447 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
448 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
449 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
450 bool parseEscapedString(std::string &Data) override;
452 const MCExpr *applyModifierToExpr(const MCExpr *E,
453 MCSymbolRefExpr::VariantKind Variant);
455 // Macro-like directives
456 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
457 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
458 raw_svector_ostream &OS);
459 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
460 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
461 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
462 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
464 // "_emit" or "__emit"
465 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
469 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
472 bool parseDirectiveEnd(SMLoc DirectiveLoc);
474 // ".err" or ".error"
475 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
477 void initializeDirectiveKindMap();
483 extern MCAsmParserExtension *createDarwinAsmParser();
484 extern MCAsmParserExtension *createELFAsmParser();
485 extern MCAsmParserExtension *createCOFFAsmParser();
489 enum { DEFAULT_ADDRSPACE = 0 };
491 AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
492 const MCAsmInfo &_MAI)
493 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
494 PlatformParser(nullptr), CurBuffer(_SM.getMainFileID()),
495 MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0),
496 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
497 // Save the old handler.
498 SavedDiagHandler = SrcMgr.getDiagHandler();
499 SavedDiagContext = SrcMgr.getDiagContext();
500 // Set our own handler which calls the saved handler.
501 SrcMgr.setDiagHandler(DiagHandler, this);
502 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
504 // Initialize the platform / file format parser.
505 switch (_Ctx.getObjectFileInfo()->getObjectFileType()) {
506 case MCObjectFileInfo::IsCOFF:
507 PlatformParser = createCOFFAsmParser();
508 PlatformParser->Initialize(*this);
510 case MCObjectFileInfo::IsMachO:
511 PlatformParser = createDarwinAsmParser();
512 PlatformParser->Initialize(*this);
515 case MCObjectFileInfo::IsELF:
516 PlatformParser = createELFAsmParser();
517 PlatformParser->Initialize(*this);
521 initializeDirectiveKindMap();
524 AsmParser::~AsmParser() {
525 assert((HadError || ActiveMacros.empty()) &&
526 "Unexpected active macro instantiation!");
528 // Destroy any macros.
529 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
532 delete it->getValue();
534 delete PlatformParser;
537 void AsmParser::printMacroInstantiations() {
538 // Print the active macro instantiation stack.
539 for (std::vector<MacroInstantiation *>::const_reverse_iterator
540 it = ActiveMacros.rbegin(),
541 ie = ActiveMacros.rend();
543 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
544 "while in macro instantiation");
547 void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
548 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
549 printMacroInstantiations();
552 bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
553 if (FatalAssemblerWarnings)
554 return Error(L, Msg, Ranges);
555 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
556 printMacroInstantiations();
560 bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
562 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
563 printMacroInstantiations();
567 bool AsmParser::enterIncludeFile(const std::string &Filename) {
568 std::string IncludedFile;
570 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
575 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
579 /// Process the specified .incbin file by searching for it in the include paths
580 /// then just emitting the byte contents of the file to the streamer. This
581 /// returns true on failure.
582 bool AsmParser::processIncbinFile(const std::string &Filename) {
583 std::string IncludedFile;
585 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
589 // Pick up the bytes from the file and emit them.
590 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
594 void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
595 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
596 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
600 const AsmToken &AsmParser::Lex() {
601 const AsmToken *tok = &Lexer.Lex();
603 if (tok->is(AsmToken::Eof)) {
604 // If this is the end of an included file, pop the parent file off the
606 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
607 if (ParentIncludeLoc != SMLoc()) {
608 jumpToLoc(ParentIncludeLoc);
613 if (tok->is(AsmToken::Error))
614 Error(Lexer.getErrLoc(), Lexer.getErr());
619 bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
620 // Create the initial section, if requested.
621 if (!NoInitialTextSection)
628 AsmCond StartingCondState = TheCondState;
630 // If we are generating dwarf for assembly source files save the initial text
631 // section and generate a .file directive.
632 if (getContext().getGenDwarfForAssembly()) {
633 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
634 getStreamer().EmitLabel(SectionStartSym);
635 auto InsertResult = getContext().addGenDwarfSection(
636 getStreamer().getCurrentSection().first);
637 assert(InsertResult.second && ".text section should not have debug info yet");
638 InsertResult.first->second.first = SectionStartSym;
639 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
640 0, StringRef(), getContext().getMainFileName()));
643 // While we have input, parse each statement.
644 while (Lexer.isNot(AsmToken::Eof)) {
645 ParseStatementInfo Info;
646 if (!parseStatement(Info))
649 // We had an error, validate that one was emitted and recover by skipping to
651 assert(HadError && "Parse statement returned an error, but none emitted!");
652 eatToEndOfStatement();
655 if (TheCondState.TheCond != StartingCondState.TheCond ||
656 TheCondState.Ignore != StartingCondState.Ignore)
657 return TokError("unmatched .ifs or .elses");
659 // Check to see there are no empty DwarfFile slots.
660 const auto &LineTables = getContext().getMCDwarfLineTables();
661 if (!LineTables.empty()) {
663 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
664 if (File.Name.empty() && Index != 0)
665 TokError("unassigned file number: " + Twine(Index) +
666 " for .file directives");
671 // Check to see that all assembler local symbols were actually defined.
672 // Targets that don't do subsections via symbols may not want this, though,
673 // so conservatively exclude them. Only do this if we're finalizing, though,
674 // as otherwise we won't necessarilly have seen everything yet.
675 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
676 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
677 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
680 MCSymbol *Sym = i->getValue();
681 // Variable symbols may not be marked as defined, so check those
682 // explicitly. If we know it's a variable, we have a definition for
683 // the purposes of this check.
684 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
685 // FIXME: We would really like to refer back to where the symbol was
686 // first referenced for a source location. We need to add something
687 // to track that. Currently, we just point to the end of the file.
689 getLexer().getLoc(), SourceMgr::DK_Error,
690 "assembler local symbol '" + Sym->getName() + "' not defined");
694 // Finalize the output stream if there are no errors and if the client wants
696 if (!HadError && !NoFinalize)
702 void AsmParser::checkForValidSection() {
703 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
704 TokError("expected section directive before assembly directive");
709 /// \brief Throw away the rest of the line for testing purposes.
710 void AsmParser::eatToEndOfStatement() {
711 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
715 if (Lexer.is(AsmToken::EndOfStatement))
719 StringRef AsmParser::parseStringToEndOfStatement() {
720 const char *Start = getTok().getLoc().getPointer();
722 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
725 const char *End = getTok().getLoc().getPointer();
726 return StringRef(Start, End - Start);
729 StringRef AsmParser::parseStringToComma() {
730 const char *Start = getTok().getLoc().getPointer();
732 while (Lexer.isNot(AsmToken::EndOfStatement) &&
733 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
736 const char *End = getTok().getLoc().getPointer();
737 return StringRef(Start, End - Start);
740 /// \brief Parse a paren expression and return it.
741 /// NOTE: This assumes the leading '(' has already been consumed.
743 /// parenexpr ::= expr)
745 bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
746 if (parseExpression(Res))
748 if (Lexer.isNot(AsmToken::RParen))
749 return TokError("expected ')' in parentheses expression");
750 EndLoc = Lexer.getTok().getEndLoc();
755 /// \brief Parse a bracket expression and return it.
756 /// NOTE: This assumes the leading '[' has already been consumed.
758 /// bracketexpr ::= expr]
760 bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
761 if (parseExpression(Res))
763 if (Lexer.isNot(AsmToken::RBrac))
764 return TokError("expected ']' in brackets expression");
765 EndLoc = Lexer.getTok().getEndLoc();
770 /// \brief Parse a primary expression and return it.
771 /// primaryexpr ::= (parenexpr
772 /// primaryexpr ::= symbol
773 /// primaryexpr ::= number
774 /// primaryexpr ::= '.'
775 /// primaryexpr ::= ~,+,- primaryexpr
776 bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
777 SMLoc FirstTokenLoc = getLexer().getLoc();
778 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
779 switch (FirstTokenKind) {
781 return TokError("unknown token in expression");
782 // If we have an error assume that we've already handled it.
783 case AsmToken::Error:
785 case AsmToken::Exclaim:
786 Lex(); // Eat the operator.
787 if (parsePrimaryExpr(Res, EndLoc))
789 Res = MCUnaryExpr::CreateLNot(Res, getContext());
791 case AsmToken::Dollar:
793 case AsmToken::String:
794 case AsmToken::Identifier: {
795 StringRef Identifier;
796 if (parseIdentifier(Identifier)) {
797 if (FirstTokenKind == AsmToken::Dollar) {
798 if (Lexer.getMAI().getDollarIsPC()) {
799 // This is a '$' reference, which references the current PC. Emit a
800 // temporary label to the streamer and refer to it.
801 MCSymbol *Sym = Ctx.CreateTempSymbol();
803 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
805 EndLoc = FirstTokenLoc;
808 return Error(FirstTokenLoc, "invalid token in expression");
811 // Parse symbol variant
812 std::pair<StringRef, StringRef> Split;
813 if (!MAI.useParensForSymbolVariant()) {
814 if (FirstTokenKind == AsmToken::String) {
815 if (Lexer.is(AsmToken::At)) {
816 Lexer.Lex(); // eat @
817 SMLoc AtLoc = getLexer().getLoc();
819 if (parseIdentifier(VName))
820 return Error(AtLoc, "expected symbol variant after '@'");
822 Split = std::make_pair(Identifier, VName);
825 Split = Identifier.split('@');
827 } else if (Lexer.is(AsmToken::LParen)) {
828 Lexer.Lex(); // eat (
830 parseIdentifier(VName);
831 if (Lexer.isNot(AsmToken::RParen)) {
832 return Error(Lexer.getTok().getLoc(),
833 "unexpected token in variant, expected ')'");
835 Lexer.Lex(); // eat )
836 Split = std::make_pair(Identifier, VName);
839 EndLoc = SMLoc::getFromPointer(Identifier.end());
841 // This is a symbol reference.
842 StringRef SymbolName = Identifier;
843 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
845 // Lookup the symbol variant if used.
846 if (Split.second.size()) {
847 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
848 if (Variant != MCSymbolRefExpr::VK_Invalid) {
849 SymbolName = Split.first;
850 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
851 Variant = MCSymbolRefExpr::VK_None;
853 return Error(SMLoc::getFromPointer(Split.second.begin()),
854 "invalid variant '" + Split.second + "'");
858 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
860 // If this is an absolute variable reference, substitute it now to preserve
861 // semantics in the face of reassignment.
862 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
864 return Error(EndLoc, "unexpected modifier on variable reference");
866 Res = Sym->getVariableValue();
870 // Otherwise create a symbol ref.
871 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
874 case AsmToken::BigNum:
875 return TokError("literal value out of range for directive");
876 case AsmToken::Integer: {
877 SMLoc Loc = getTok().getLoc();
878 int64_t IntVal = getTok().getIntVal();
879 Res = MCConstantExpr::Create(IntVal, getContext());
880 EndLoc = Lexer.getTok().getEndLoc();
882 // Look for 'b' or 'f' following an Integer as a directional label
883 if (Lexer.getKind() == AsmToken::Identifier) {
884 StringRef IDVal = getTok().getString();
885 // Lookup the symbol variant if used.
886 std::pair<StringRef, StringRef> Split = IDVal.split('@');
887 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
888 if (Split.first.size() != IDVal.size()) {
889 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
890 if (Variant == MCSymbolRefExpr::VK_Invalid)
891 return TokError("invalid variant '" + Split.second + "'");
894 if (IDVal == "f" || IDVal == "b") {
896 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "b");
897 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
898 if (IDVal == "b" && Sym->isUndefined())
899 return Error(Loc, "invalid reference to undefined symbol");
900 EndLoc = Lexer.getTok().getEndLoc();
901 Lex(); // Eat identifier.
906 case AsmToken::Real: {
907 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
908 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
909 Res = MCConstantExpr::Create(IntVal, getContext());
910 EndLoc = Lexer.getTok().getEndLoc();
914 case AsmToken::Dot: {
915 // This is a '.' reference, which references the current PC. Emit a
916 // temporary label to the streamer and refer to it.
917 MCSymbol *Sym = Ctx.CreateTempSymbol();
919 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
920 EndLoc = Lexer.getTok().getEndLoc();
921 Lex(); // Eat identifier.
924 case AsmToken::LParen:
925 Lex(); // Eat the '('.
926 return parseParenExpr(Res, EndLoc);
927 case AsmToken::LBrac:
928 if (!PlatformParser->HasBracketExpressions())
929 return TokError("brackets expression not supported on this target");
930 Lex(); // Eat the '['.
931 return parseBracketExpr(Res, EndLoc);
932 case AsmToken::Minus:
933 Lex(); // Eat the operator.
934 if (parsePrimaryExpr(Res, EndLoc))
936 Res = MCUnaryExpr::CreateMinus(Res, getContext());
939 Lex(); // Eat the operator.
940 if (parsePrimaryExpr(Res, EndLoc))
942 Res = MCUnaryExpr::CreatePlus(Res, getContext());
944 case AsmToken::Tilde:
945 Lex(); // Eat the operator.
946 if (parsePrimaryExpr(Res, EndLoc))
948 Res = MCUnaryExpr::CreateNot(Res, getContext());
953 bool AsmParser::parseExpression(const MCExpr *&Res) {
955 return parseExpression(Res, EndLoc);
959 AsmParser::applyModifierToExpr(const MCExpr *E,
960 MCSymbolRefExpr::VariantKind Variant) {
961 // Ask the target implementation about this expression first.
962 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
965 // Recurse over the given expression, rebuilding it to apply the given variant
966 // if there is exactly one symbol.
967 switch (E->getKind()) {
969 case MCExpr::Constant:
972 case MCExpr::SymbolRef: {
973 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
975 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
976 TokError("invalid variant on expression '" + getTok().getIdentifier() +
977 "' (already modified)");
981 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
984 case MCExpr::Unary: {
985 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
986 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
989 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
992 case MCExpr::Binary: {
993 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
994 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
995 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
1005 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1009 llvm_unreachable("Invalid expression kind!");
1012 /// \brief Parse an expression and return it.
1014 /// expr ::= expr &&,|| expr -> lowest.
1015 /// expr ::= expr |,^,&,! expr
1016 /// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1017 /// expr ::= expr <<,>> expr
1018 /// expr ::= expr +,- expr
1019 /// expr ::= expr *,/,% expr -> highest.
1020 /// expr ::= primaryexpr
1022 bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
1023 // Parse the expression.
1025 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
1028 // As a special case, we support 'a op b @ modifier' by rewriting the
1029 // expression to include the modifier. This is inefficient, but in general we
1030 // expect users to use 'a@modifier op b'.
1031 if (Lexer.getKind() == AsmToken::At) {
1034 if (Lexer.isNot(AsmToken::Identifier))
1035 return TokError("unexpected symbol modifier following '@'");
1037 MCSymbolRefExpr::VariantKind Variant =
1038 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
1039 if (Variant == MCSymbolRefExpr::VK_Invalid)
1040 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1042 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
1044 return TokError("invalid modifier '" + getTok().getIdentifier() +
1045 "' (no symbols present)");
1052 // Try to constant fold it up front, if possible.
1054 if (Res->EvaluateAsAbsolute(Value))
1055 Res = MCConstantExpr::Create(Value, getContext());
1060 bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
1062 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
1065 bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
1068 SMLoc StartLoc = Lexer.getLoc();
1069 if (parseExpression(Expr))
1072 if (!Expr->EvaluateAsAbsolute(Res))
1073 return Error(StartLoc, "expected absolute expression");
1078 static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
1079 MCBinaryExpr::Opcode &Kind) {
1082 return 0; // not a binop.
1084 // Lowest Precedence: &&, ||
1085 case AsmToken::AmpAmp:
1086 Kind = MCBinaryExpr::LAnd;
1088 case AsmToken::PipePipe:
1089 Kind = MCBinaryExpr::LOr;
1092 // Low Precedence: |, &, ^
1094 // FIXME: gas seems to support '!' as an infix operator?
1095 case AsmToken::Pipe:
1096 Kind = MCBinaryExpr::Or;
1098 case AsmToken::Caret:
1099 Kind = MCBinaryExpr::Xor;
1102 Kind = MCBinaryExpr::And;
1105 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
1106 case AsmToken::EqualEqual:
1107 Kind = MCBinaryExpr::EQ;
1109 case AsmToken::ExclaimEqual:
1110 case AsmToken::LessGreater:
1111 Kind = MCBinaryExpr::NE;
1113 case AsmToken::Less:
1114 Kind = MCBinaryExpr::LT;
1116 case AsmToken::LessEqual:
1117 Kind = MCBinaryExpr::LTE;
1119 case AsmToken::Greater:
1120 Kind = MCBinaryExpr::GT;
1122 case AsmToken::GreaterEqual:
1123 Kind = MCBinaryExpr::GTE;
1126 // Intermediate Precedence: <<, >>
1127 case AsmToken::LessLess:
1128 Kind = MCBinaryExpr::Shl;
1130 case AsmToken::GreaterGreater:
1131 Kind = MCBinaryExpr::Shr;
1134 // High Intermediate Precedence: +, -
1135 case AsmToken::Plus:
1136 Kind = MCBinaryExpr::Add;
1138 case AsmToken::Minus:
1139 Kind = MCBinaryExpr::Sub;
1142 // Highest Precedence: *, /, %
1143 case AsmToken::Star:
1144 Kind = MCBinaryExpr::Mul;
1146 case AsmToken::Slash:
1147 Kind = MCBinaryExpr::Div;
1149 case AsmToken::Percent:
1150 Kind = MCBinaryExpr::Mod;
1155 /// \brief Parse all binary operators with precedence >= 'Precedence'.
1156 /// Res contains the LHS of the expression on input.
1157 bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
1160 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
1161 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
1163 // If the next token is lower precedence than we are allowed to eat, return
1164 // successfully with what we ate already.
1165 if (TokPrec < Precedence)
1170 // Eat the next primary expression.
1172 if (parsePrimaryExpr(RHS, EndLoc))
1175 // If BinOp binds less tightly with RHS than the operator after RHS, let
1176 // the pending operator take RHS as its LHS.
1177 MCBinaryExpr::Opcode Dummy;
1178 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
1179 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1182 // Merge LHS and RHS according to operator.
1183 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
1188 /// ::= EndOfStatement
1189 /// ::= Label* Directive ...Operands... EndOfStatement
1190 /// ::= Label* Identifier OperandList* EndOfStatement
1191 bool AsmParser::parseStatement(ParseStatementInfo &Info) {
1192 if (Lexer.is(AsmToken::EndOfStatement)) {
1198 // Statements always start with an identifier or are a full line comment.
1199 AsmToken ID = getTok();
1200 SMLoc IDLoc = ID.getLoc();
1202 int64_t LocalLabelVal = -1;
1203 // A full line comment is a '#' as the first token.
1204 if (Lexer.is(AsmToken::Hash))
1205 return parseCppHashLineFilenameComment(IDLoc);
1207 // Allow an integer followed by a ':' as a directional local label.
1208 if (Lexer.is(AsmToken::Integer)) {
1209 LocalLabelVal = getTok().getIntVal();
1210 if (LocalLabelVal < 0) {
1211 if (!TheCondState.Ignore)
1212 return TokError("unexpected token at start of statement");
1215 IDVal = getTok().getString();
1216 Lex(); // Consume the integer token to be used as an identifier token.
1217 if (Lexer.getKind() != AsmToken::Colon) {
1218 if (!TheCondState.Ignore)
1219 return TokError("unexpected token at start of statement");
1222 } else if (Lexer.is(AsmToken::Dot)) {
1223 // Treat '.' as a valid identifier in this context.
1226 } else if (parseIdentifier(IDVal)) {
1227 if (!TheCondState.Ignore)
1228 return TokError("unexpected token at start of statement");
1232 // Handle conditional assembly here before checking for skipping. We
1233 // have to do this so that .endif isn't skipped in a ".if 0" block for
1235 StringMap<DirectiveKind>::const_iterator DirKindIt =
1236 DirectiveKindMap.find(IDVal);
1237 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1239 : DirKindIt->getValue();
1250 return parseDirectiveIf(IDLoc, DirKind);
1252 return parseDirectiveIfb(IDLoc, true);
1254 return parseDirectiveIfb(IDLoc, false);
1256 return parseDirectiveIfc(IDLoc, true);
1258 return parseDirectiveIfeqs(IDLoc);
1260 return parseDirectiveIfc(IDLoc, false);
1262 return parseDirectiveIfdef(IDLoc, true);
1265 return parseDirectiveIfdef(IDLoc, false);
1267 return parseDirectiveElseIf(IDLoc);
1269 return parseDirectiveElse(IDLoc);
1271 return parseDirectiveEndIf(IDLoc);
1274 // Ignore the statement if in the middle of inactive conditional
1276 if (TheCondState.Ignore) {
1277 eatToEndOfStatement();
1281 // FIXME: Recurse on local labels?
1283 // See what kind of statement we have.
1284 switch (Lexer.getKind()) {
1285 case AsmToken::Colon: {
1286 checkForValidSection();
1288 // identifier ':' -> Label.
1291 // Diagnose attempt to use '.' as a label.
1293 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1295 // Diagnose attempt to use a variable as a label.
1297 // FIXME: Diagnostics. Note the location of the definition as a label.
1298 // FIXME: This doesn't diagnose assignment to a symbol which has been
1299 // implicitly marked as external.
1301 if (LocalLabelVal == -1)
1302 Sym = getContext().GetOrCreateSymbol(IDVal);
1304 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
1305 if (!Sym->isUndefined() || Sym->isVariable())
1306 return Error(IDLoc, "invalid symbol redefinition");
1309 if (!ParsingInlineAsm)
1312 // If we are generating dwarf for assembly source files then gather the
1313 // info to make a dwarf label entry for this label if needed.
1314 if (getContext().getGenDwarfForAssembly())
1315 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1318 getTargetParser().onLabelParsed(Sym);
1320 // Consume any end of statement token, if present, to avoid spurious
1321 // AddBlankLine calls().
1322 if (Lexer.is(AsmToken::EndOfStatement)) {
1324 if (Lexer.is(AsmToken::Eof))
1331 case AsmToken::Equal:
1332 // identifier '=' ... -> assignment statement
1335 return parseAssignment(IDVal, true);
1337 default: // Normal instruction or directive.
1341 // If macros are enabled, check to see if this is a macro instantiation.
1342 if (areMacrosEnabled())
1343 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1344 return handleMacroEntry(M, IDLoc);
1347 // Otherwise, we have a normal instruction or directive.
1349 // Directives start with "."
1350 if (IDVal[0] == '.' && IDVal != ".") {
1351 // There are several entities interested in parsing directives:
1353 // 1. The target-specific assembly parser. Some directives are target
1354 // specific or may potentially behave differently on certain targets.
1355 // 2. Asm parser extensions. For example, platform-specific parsers
1356 // (like the ELF parser) register themselves as extensions.
1357 // 3. The generic directive parser implemented by this class. These are
1358 // all the directives that behave in a target and platform independent
1359 // manner, or at least have a default behavior that's shared between
1360 // all targets and platforms.
1362 // First query the target-specific parser. It will return 'true' if it
1363 // isn't interested in this directive.
1364 if (!getTargetParser().ParseDirective(ID))
1367 // Next, check the extension directive map to see if any extension has
1368 // registered itself to parse this directive.
1369 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1370 ExtensionDirectiveMap.lookup(IDVal);
1372 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1374 // Finally, if no one else is interested in this directive, it must be
1375 // generic and familiar to this class.
1381 return parseDirectiveSet(IDVal, true);
1383 return parseDirectiveSet(IDVal, false);
1385 return parseDirectiveAscii(IDVal, false);
1388 return parseDirectiveAscii(IDVal, true);
1390 return parseDirectiveValue(1);
1394 return parseDirectiveValue(2);
1398 return parseDirectiveValue(4);
1401 return parseDirectiveValue(8);
1403 return parseDirectiveOctaValue();
1406 return parseDirectiveRealValue(APFloat::IEEEsingle);
1408 return parseDirectiveRealValue(APFloat::IEEEdouble);
1410 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1411 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1414 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1415 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1418 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1420 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1422 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1424 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1426 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1428 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1430 return parseDirectiveOrg();
1432 return parseDirectiveFill();
1434 return parseDirectiveZero();
1436 eatToEndOfStatement(); // .extern is the default, ignore it.
1440 return parseDirectiveSymbolAttribute(MCSA_Global);
1441 case DK_LAZY_REFERENCE:
1442 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1443 case DK_NO_DEAD_STRIP:
1444 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1445 case DK_SYMBOL_RESOLVER:
1446 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1447 case DK_PRIVATE_EXTERN:
1448 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1450 return parseDirectiveSymbolAttribute(MCSA_Reference);
1451 case DK_WEAK_DEFINITION:
1452 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1453 case DK_WEAK_REFERENCE:
1454 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1455 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1456 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1459 return parseDirectiveComm(/*IsLocal=*/false);
1461 return parseDirectiveComm(/*IsLocal=*/true);
1463 return parseDirectiveAbort();
1465 return parseDirectiveInclude();
1467 return parseDirectiveIncbin();
1470 return TokError(Twine(IDVal) + " not supported yet");
1472 return parseDirectiveRept(IDLoc, IDVal);
1474 return parseDirectiveIrp(IDLoc);
1476 return parseDirectiveIrpc(IDLoc);
1478 return parseDirectiveEndr(IDLoc);
1479 case DK_BUNDLE_ALIGN_MODE:
1480 return parseDirectiveBundleAlignMode();
1481 case DK_BUNDLE_LOCK:
1482 return parseDirectiveBundleLock();
1483 case DK_BUNDLE_UNLOCK:
1484 return parseDirectiveBundleUnlock();
1486 return parseDirectiveLEB128(true);
1488 return parseDirectiveLEB128(false);
1491 return parseDirectiveSpace(IDVal);
1493 return parseDirectiveFile(IDLoc);
1495 return parseDirectiveLine();
1497 return parseDirectiveLoc();
1499 return parseDirectiveStabs();
1500 case DK_CFI_SECTIONS:
1501 return parseDirectiveCFISections();
1502 case DK_CFI_STARTPROC:
1503 return parseDirectiveCFIStartProc();
1504 case DK_CFI_ENDPROC:
1505 return parseDirectiveCFIEndProc();
1506 case DK_CFI_DEF_CFA:
1507 return parseDirectiveCFIDefCfa(IDLoc);
1508 case DK_CFI_DEF_CFA_OFFSET:
1509 return parseDirectiveCFIDefCfaOffset();
1510 case DK_CFI_ADJUST_CFA_OFFSET:
1511 return parseDirectiveCFIAdjustCfaOffset();
1512 case DK_CFI_DEF_CFA_REGISTER:
1513 return parseDirectiveCFIDefCfaRegister(IDLoc);
1515 return parseDirectiveCFIOffset(IDLoc);
1516 case DK_CFI_REL_OFFSET:
1517 return parseDirectiveCFIRelOffset(IDLoc);
1518 case DK_CFI_PERSONALITY:
1519 return parseDirectiveCFIPersonalityOrLsda(true);
1521 return parseDirectiveCFIPersonalityOrLsda(false);
1522 case DK_CFI_REMEMBER_STATE:
1523 return parseDirectiveCFIRememberState();
1524 case DK_CFI_RESTORE_STATE:
1525 return parseDirectiveCFIRestoreState();
1526 case DK_CFI_SAME_VALUE:
1527 return parseDirectiveCFISameValue(IDLoc);
1528 case DK_CFI_RESTORE:
1529 return parseDirectiveCFIRestore(IDLoc);
1531 return parseDirectiveCFIEscape();
1532 case DK_CFI_SIGNAL_FRAME:
1533 return parseDirectiveCFISignalFrame();
1534 case DK_CFI_UNDEFINED:
1535 return parseDirectiveCFIUndefined(IDLoc);
1536 case DK_CFI_REGISTER:
1537 return parseDirectiveCFIRegister(IDLoc);
1538 case DK_CFI_WINDOW_SAVE:
1539 return parseDirectiveCFIWindowSave();
1542 return parseDirectiveMacrosOnOff(IDVal);
1544 return parseDirectiveMacro(IDLoc);
1547 return parseDirectiveEndMacro(IDVal);
1549 return parseDirectivePurgeMacro(IDLoc);
1551 return parseDirectiveEnd(IDLoc);
1553 return parseDirectiveError(IDLoc, false);
1555 return parseDirectiveError(IDLoc, true);
1558 return Error(IDLoc, "unknown directive");
1561 // __asm _emit or __asm __emit
1562 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1563 IDVal == "_EMIT" || IDVal == "__EMIT"))
1564 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
1567 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
1568 return parseDirectiveMSAlign(IDLoc, Info);
1570 checkForValidSection();
1572 // Canonicalize the opcode to lower case.
1573 std::string OpcodeStr = IDVal.lower();
1574 ParseInstructionInfo IInfo(Info.AsmRewrites);
1575 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
1576 Info.ParsedOperands);
1577 Info.ParseError = HadError;
1579 // Dump the parsed representation, if requested.
1580 if (getShowParsedOperands()) {
1581 SmallString<256> Str;
1582 raw_svector_ostream OS(Str);
1583 OS << "parsed instruction: [";
1584 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
1587 Info.ParsedOperands[i]->print(OS);
1591 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
1594 // If we are generating dwarf for the current section then generate a .loc
1595 // directive for the instruction.
1596 if (!HadError && getContext().getGenDwarfForAssembly() &&
1597 getContext().getGenDwarfSectionSyms().count(
1598 getStreamer().getCurrentSection().first)) {
1600 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1602 // If we previously parsed a cpp hash file line comment then make sure the
1603 // current Dwarf File is for the CppHashFilename if not then emit the
1604 // Dwarf File table for it and adjust the line number for the .loc.
1605 if (CppHashFilename.size() != 0) {
1606 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1607 0, StringRef(), CppHashFilename);
1608 getContext().setGenDwarfFileNumber(FileNumber);
1610 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1611 // cache with the different Loc from the call above we save the last
1612 // info we queried here with SrcMgr.FindLineNumber().
1613 unsigned CppHashLocLineNo;
1614 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1615 CppHashLocLineNo = LastQueryLine;
1617 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1618 LastQueryLine = CppHashLocLineNo;
1619 LastQueryIDLoc = CppHashLoc;
1620 LastQueryBuffer = CppHashBuf;
1622 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
1625 getStreamer().EmitDwarfLocDirective(
1626 getContext().getGenDwarfFileNumber(), Line, 0,
1627 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1631 // If parsing succeeded, match the instruction.
1634 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1635 Info.ParsedOperands, Out,
1636 ErrorInfo, ParsingInlineAsm);
1639 // Don't skip the rest of the line, the instruction parser is responsible for
1644 /// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
1645 /// since they may not be able to be tokenized to get to the end of line token.
1646 void AsmParser::eatToEndOfLine() {
1647 if (!Lexer.is(AsmToken::EndOfStatement))
1648 Lexer.LexUntilEndOfLine();
1653 /// parseCppHashLineFilenameComment as this:
1654 /// ::= # number "filename"
1655 /// or just as a full line comment if it doesn't have a number and a string.
1656 bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
1657 Lex(); // Eat the hash token.
1659 if (getLexer().isNot(AsmToken::Integer)) {
1660 // Consume the line since in cases it is not a well-formed line directive,
1661 // as if were simply a full line comment.
1666 int64_t LineNumber = getTok().getIntVal();
1669 if (getLexer().isNot(AsmToken::String)) {
1674 StringRef Filename = getTok().getString();
1675 // Get rid of the enclosing quotes.
1676 Filename = Filename.substr(1, Filename.size() - 2);
1678 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1680 CppHashFilename = Filename;
1681 CppHashLineNumber = LineNumber;
1682 CppHashBuf = CurBuffer;
1684 // Ignore any trailing characters, they're just comment.
1689 /// \brief will use the last parsed cpp hash line filename comment
1690 /// for the Filename and LineNo if any in the diagnostic.
1691 void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1692 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
1693 raw_ostream &OS = errs();
1695 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1696 const SMLoc &DiagLoc = Diag.getLoc();
1697 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1698 unsigned CppHashBuf =
1699 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1701 // Like SourceMgr::printMessage() we need to print the include stack if any
1702 // before printing the message.
1703 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1704 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1705 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
1706 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1707 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1710 // If we have not parsed a cpp hash line filename comment or the source
1711 // manager changed or buffer changed (like in a nested include) then just
1712 // print the normal diagnostic using its Filename and LineNo.
1713 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
1714 DiagBuf != CppHashBuf) {
1715 if (Parser->SavedDiagHandler)
1716 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1718 Diag.print(nullptr, OS);
1722 // Use the CppHashFilename and calculate a line number based on the
1723 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1725 const std::string &Filename = Parser->CppHashFilename;
1727 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1728 int CppHashLocLineNo =
1729 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1731 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
1733 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1734 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
1735 Diag.getLineContents(), Diag.getRanges());
1737 if (Parser->SavedDiagHandler)
1738 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1740 NewDiag.print(nullptr, OS);
1743 // FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1744 // difference being that that function accepts '@' as part of identifiers and
1745 // we can't do that. AsmLexer.cpp should probably be changed to handle
1746 // '@' as a special case when needed.
1747 static bool isIdentifierChar(char c) {
1748 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1752 bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
1753 ArrayRef<MCAsmMacroParameter> Parameters,
1754 ArrayRef<MCAsmMacroArgument> A, const SMLoc &L) {
1755 unsigned NParameters = Parameters.size();
1756 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
1757 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
1758 return Error(L, "Wrong number of arguments");
1760 // A macro without parameters is handled differently on Darwin:
1761 // gas accepts no arguments and does no substitutions
1762 while (!Body.empty()) {
1763 // Scan for the next substitution.
1764 std::size_t End = Body.size(), Pos = 0;
1765 for (; Pos != End; ++Pos) {
1766 // Check for a substitution or escape.
1767 if (IsDarwin && !NParameters) {
1768 // This macro has no parameters, look for $0, $1, etc.
1769 if (Body[Pos] != '$' || Pos + 1 == End)
1772 char Next = Body[Pos + 1];
1773 if (Next == '$' || Next == 'n' ||
1774 isdigit(static_cast<unsigned char>(Next)))
1777 // This macro has parameters, look for \foo, \bar, etc.
1778 if (Body[Pos] == '\\' && Pos + 1 != End)
1784 OS << Body.slice(0, Pos);
1786 // Check if we reached the end.
1790 if (IsDarwin && !NParameters) {
1791 switch (Body[Pos + 1]) {
1797 // $n => number of arguments
1802 // $[0-9] => argument
1804 // Missing arguments are ignored.
1805 unsigned Index = Body[Pos + 1] - '0';
1806 if (Index >= A.size())
1809 // Otherwise substitute with the token values, with spaces eliminated.
1810 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
1811 ie = A[Index].end();
1813 OS << it->getString();
1819 unsigned I = Pos + 1;
1820 while (isIdentifierChar(Body[I]) && I + 1 != End)
1823 const char *Begin = Body.data() + Pos + 1;
1824 StringRef Argument(Begin, I - (Pos + 1));
1826 for (; Index < NParameters; ++Index)
1827 if (Parameters[Index].Name == Argument)
1830 if (Index == NParameters) {
1831 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1834 OS << '\\' << Argument;
1838 bool VarargParameter = HasVararg && Index == (NParameters - 1);
1839 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
1840 ie = A[Index].end();
1842 // We expect no quotes around the string's contents when
1843 // parsing for varargs.
1844 if (it->getKind() != AsmToken::String || VarargParameter)
1845 OS << it->getString();
1847 OS << it->getStringContents();
1849 Pos += 1 + Argument.size();
1852 // Update the scan point.
1853 Body = Body.substr(Pos);
1859 MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB,
1860 SMLoc EL, MemoryBuffer *I)
1861 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1864 static bool isOperator(AsmToken::TokenKind kind) {
1868 case AsmToken::Plus:
1869 case AsmToken::Minus:
1870 case AsmToken::Tilde:
1871 case AsmToken::Slash:
1872 case AsmToken::Star:
1874 case AsmToken::Equal:
1875 case AsmToken::EqualEqual:
1876 case AsmToken::Pipe:
1877 case AsmToken::PipePipe:
1878 case AsmToken::Caret:
1880 case AsmToken::AmpAmp:
1881 case AsmToken::Exclaim:
1882 case AsmToken::ExclaimEqual:
1883 case AsmToken::Percent:
1884 case AsmToken::Less:
1885 case AsmToken::LessEqual:
1886 case AsmToken::LessLess:
1887 case AsmToken::LessGreater:
1888 case AsmToken::Greater:
1889 case AsmToken::GreaterEqual:
1890 case AsmToken::GreaterGreater:
1896 class AsmLexerSkipSpaceRAII {
1898 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1899 Lexer.setSkipSpace(SkipSpace);
1902 ~AsmLexerSkipSpaceRAII() {
1903 Lexer.setSkipSpace(true);
1911 bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
1914 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1915 StringRef Str = parseStringToEndOfStatement();
1916 MA.push_back(AsmToken(AsmToken::String, Str));
1921 unsigned ParenLevel = 0;
1922 unsigned AddTokens = 0;
1924 // Darwin doesn't use spaces to delmit arguments.
1925 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
1928 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
1929 return TokError("unexpected token in macro instantiation");
1931 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
1934 if (Lexer.is(AsmToken::Space)) {
1935 Lex(); // Eat spaces
1937 // Spaces can delimit parameters, but could also be part an expression.
1938 // If the token after a space is an operator, add the token and the next
1939 // one into this argument
1941 if (isOperator(Lexer.getKind())) {
1942 // Check to see whether the token is used as an operator,
1943 // or part of an identifier
1944 const char *NextChar = getTok().getEndLoc().getPointer();
1945 if (*NextChar == ' ')
1949 if (!AddTokens && ParenLevel == 0) {
1955 // handleMacroEntry relies on not advancing the lexer here
1956 // to be able to fill in the remaining default parameter values
1957 if (Lexer.is(AsmToken::EndOfStatement))
1960 // Adjust the current parentheses level.
1961 if (Lexer.is(AsmToken::LParen))
1963 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1966 // Append the token to the current argument list.
1967 MA.push_back(getTok());
1973 if (ParenLevel != 0)
1974 return TokError("unbalanced parentheses in macro argument");
1978 // Parse the macro instantiation arguments.
1979 bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
1980 MCAsmMacroArguments &A) {
1981 const unsigned NParameters = M ? M->Parameters.size() : 0;
1982 bool NamedParametersFound = false;
1983 SmallVector<SMLoc, 4> FALocs;
1985 A.resize(NParameters);
1986 FALocs.resize(NParameters);
1988 // Parse two kinds of macro invocations:
1989 // - macros defined without any parameters accept an arbitrary number of them
1990 // - macros defined with parameters accept at most that many of them
1991 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
1992 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1994 SMLoc IDLoc = Lexer.getLoc();
1995 MCAsmMacroParameter FA;
1997 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
1998 if (parseIdentifier(FA.Name)) {
1999 Error(IDLoc, "invalid argument identifier for formal argument");
2000 eatToEndOfStatement();
2004 if (!Lexer.is(AsmToken::Equal)) {
2005 TokError("expected '=' after formal parameter identifier");
2006 eatToEndOfStatement();
2011 NamedParametersFound = true;
2014 if (NamedParametersFound && FA.Name.empty()) {
2015 Error(IDLoc, "cannot mix positional and keyword arguments");
2016 eatToEndOfStatement();
2020 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2021 if (parseMacroArgument(FA.Value, Vararg))
2024 unsigned PI = Parameter;
2025 if (!FA.Name.empty()) {
2027 for (FAI = 0; FAI < NParameters; ++FAI)
2028 if (M->Parameters[FAI].Name == FA.Name)
2031 if (FAI >= NParameters) {
2032 assert(M && "expected macro to be defined");
2034 "parameter named '" + FA.Name + "' does not exist for macro '" +
2041 if (!FA.Value.empty()) {
2046 if (FALocs.size() <= PI)
2047 FALocs.resize(PI + 1);
2049 FALocs[PI] = Lexer.getLoc();
2052 // At the end of the statement, fill in remaining arguments that have
2053 // default values. If there aren't any, then the next argument is
2054 // required but missing
2055 if (Lexer.is(AsmToken::EndOfStatement)) {
2056 bool Failure = false;
2057 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2058 if (A[FAI].empty()) {
2059 if (M->Parameters[FAI].Required) {
2060 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2061 "missing value for required parameter "
2062 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2066 if (!M->Parameters[FAI].Value.empty())
2067 A[FAI] = M->Parameters[FAI].Value;
2073 if (Lexer.is(AsmToken::Comma))
2077 return TokError("too many positional arguments");
2080 const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
2081 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
2082 return (I == MacroMap.end()) ? nullptr : I->getValue();
2085 void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
2086 MacroMap[Name] = new MCAsmMacro(Macro);
2089 void AsmParser::undefineMacro(StringRef Name) {
2090 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
2091 if (I != MacroMap.end()) {
2092 delete I->getValue();
2097 bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
2098 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2099 // this, although we should protect against infinite loops.
2100 if (ActiveMacros.size() == 20)
2101 return TokError("macros cannot be nested more than 20 levels deep");
2103 MCAsmMacroArguments A;
2104 if (parseMacroArguments(M, A))
2107 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2108 // to hold the macro body with substitutions.
2109 SmallString<256> Buf;
2110 StringRef Body = M->Body;
2111 raw_svector_ostream OS(Buf);
2113 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
2116 // We include the .endmacro in the buffer as our cue to exit the macro
2118 OS << ".endmacro\n";
2120 MemoryBuffer *Instantiation =
2121 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
2123 // Create the macro instantiation object and add to the current macro
2124 // instantiation stack.
2125 MacroInstantiation *MI = new MacroInstantiation(
2126 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
2127 ActiveMacros.push_back(MI);
2129 // Jump to the macro instantiation and prime the lexer.
2130 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
2131 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
2137 void AsmParser::handleMacroExit() {
2138 // Jump to the EndOfStatement we should return to, and consume it.
2139 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
2142 // Pop the instantiation entry.
2143 delete ActiveMacros.back();
2144 ActiveMacros.pop_back();
2147 static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
2148 switch (Value->getKind()) {
2149 case MCExpr::Binary: {
2150 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2151 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
2153 case MCExpr::Target:
2154 case MCExpr::Constant:
2156 case MCExpr::SymbolRef: {
2158 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
2160 return isUsedIn(Sym, S.getVariableValue());
2164 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
2167 llvm_unreachable("Unknown expr kind!");
2170 bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
2172 // FIXME: Use better location, we should use proper tokens.
2173 SMLoc EqualLoc = Lexer.getLoc();
2175 const MCExpr *Value;
2176 if (parseExpression(Value))
2179 // Note: we don't count b as used in "a = b". This is to allow
2183 if (Lexer.isNot(AsmToken::EndOfStatement))
2184 return TokError("unexpected token in assignment");
2186 // Eat the end of statement marker.
2189 // Validate that the LHS is allowed to be a variable (either it has not been
2190 // used as a symbol, or it is an absolute symbol).
2191 MCSymbol *Sym = getContext().LookupSymbol(Name);
2193 // Diagnose assignment to a label.
2195 // FIXME: Diagnostics. Note the location of the definition as a label.
2196 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
2197 if (isUsedIn(Sym, Value))
2198 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2199 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
2200 ; // Allow redefinitions of undefined symbols only used in directives.
2201 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2202 ; // Allow redefinitions of variables that haven't yet been used.
2203 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
2204 return Error(EqualLoc, "redefinition of '" + Name + "'");
2205 else if (!Sym->isVariable())
2206 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
2207 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
2208 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
2211 // Don't count these checks as uses.
2212 Sym->setUsed(false);
2213 } else if (Name == ".") {
2214 if (Out.EmitValueToOffset(Value, 0)) {
2215 Error(EqualLoc, "expected absolute expression");
2216 eatToEndOfStatement();
2220 Sym = getContext().GetOrCreateSymbol(Name);
2222 // Do the assignment.
2223 Out.EmitAssignment(Sym, Value);
2225 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2230 /// parseIdentifier:
2233 bool AsmParser::parseIdentifier(StringRef &Res) {
2234 // The assembler has relaxed rules for accepting identifiers, in particular we
2235 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2236 // separate tokens. At this level, we have already lexed so we cannot (currently)
2237 // handle this as a context dependent token, instead we detect adjacent tokens
2238 // and return the combined identifier.
2239 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2240 SMLoc PrefixLoc = getLexer().getLoc();
2242 // Consume the prefix character, and check for a following identifier.
2244 if (Lexer.isNot(AsmToken::Identifier))
2247 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2248 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
2251 // Construct the joined identifier and consume the token.
2253 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
2258 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
2261 Res = getTok().getIdentifier();
2263 Lex(); // Consume the identifier token.
2268 /// parseDirectiveSet:
2269 /// ::= .equ identifier ',' expression
2270 /// ::= .equiv identifier ',' expression
2271 /// ::= .set identifier ',' expression
2272 bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
2275 if (parseIdentifier(Name))
2276 return TokError("expected identifier after '" + Twine(IDVal) + "'");
2278 if (getLexer().isNot(AsmToken::Comma))
2279 return TokError("unexpected token in '" + Twine(IDVal) + "'");
2282 return parseAssignment(Name, allow_redef, true);
2285 bool AsmParser::parseEscapedString(std::string &Data) {
2286 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
2289 StringRef Str = getTok().getStringContents();
2290 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2291 if (Str[i] != '\\') {
2296 // Recognize escaped characters. Note that this escape semantics currently
2297 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2300 return TokError("unexpected backslash at end of string");
2302 // Recognize octal sequences.
2303 if ((unsigned)(Str[i] - '0') <= 7) {
2304 // Consume up to three octal characters.
2305 unsigned Value = Str[i] - '0';
2307 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
2309 Value = Value * 8 + (Str[i] - '0');
2311 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
2313 Value = Value * 8 + (Str[i] - '0');
2318 return TokError("invalid octal escape sequence (out of range)");
2320 Data += (unsigned char)Value;
2324 // Otherwise recognize individual escapes.
2327 // Just reject invalid escape sequences for now.
2328 return TokError("invalid escape sequence (unrecognized character)");
2330 case 'b': Data += '\b'; break;
2331 case 'f': Data += '\f'; break;
2332 case 'n': Data += '\n'; break;
2333 case 'r': Data += '\r'; break;
2334 case 't': Data += '\t'; break;
2335 case '"': Data += '"'; break;
2336 case '\\': Data += '\\'; break;
2343 /// parseDirectiveAscii:
2344 /// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
2345 bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
2346 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2347 checkForValidSection();
2350 if (getLexer().isNot(AsmToken::String))
2351 return TokError("expected string in '" + Twine(IDVal) + "' directive");
2354 if (parseEscapedString(Data))
2357 getStreamer().EmitBytes(Data);
2359 getStreamer().EmitBytes(StringRef("\0", 1));
2363 if (getLexer().is(AsmToken::EndOfStatement))
2366 if (getLexer().isNot(AsmToken::Comma))
2367 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
2376 /// parseDirectiveValue
2377 /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
2378 bool AsmParser::parseDirectiveValue(unsigned Size) {
2379 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2380 checkForValidSection();
2383 const MCExpr *Value;
2384 SMLoc ExprLoc = getLexer().getLoc();
2385 if (parseExpression(Value))
2388 // Special case constant expressions to match code generator.
2389 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2390 assert(Size <= 8 && "Invalid size");
2391 uint64_t IntValue = MCE->getValue();
2392 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2393 return Error(ExprLoc, "literal value out of range for directive");
2394 getStreamer().EmitIntValue(IntValue, Size);
2396 getStreamer().EmitValue(Value, Size, ExprLoc);
2398 if (getLexer().is(AsmToken::EndOfStatement))
2401 // FIXME: Improve diagnostic.
2402 if (getLexer().isNot(AsmToken::Comma))
2403 return TokError("unexpected token in directive");
2412 /// ParseDirectiveOctaValue
2413 /// ::= .octa [ hexconstant (, hexconstant)* ]
2414 bool AsmParser::parseDirectiveOctaValue() {
2415 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2416 checkForValidSection();
2419 if (Lexer.getKind() == AsmToken::Error)
2421 if (Lexer.getKind() != AsmToken::Integer &&
2422 Lexer.getKind() != AsmToken::BigNum)
2423 return TokError("unknown token in expression");
2425 SMLoc ExprLoc = getLexer().getLoc();
2426 APInt IntValue = getTok().getAPIntVal();
2430 if (IntValue.isIntN(64)) {
2432 lo = IntValue.getZExtValue();
2433 } else if (IntValue.isIntN(128)) {
2434 // It might actually have more than 128 bits, but the top ones are zero.
2435 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
2436 lo = IntValue.getLoBits(64).getZExtValue();
2438 return Error(ExprLoc, "literal value out of range for directive");
2440 if (MAI.isLittleEndian()) {
2441 getStreamer().EmitIntValue(lo, 8);
2442 getStreamer().EmitIntValue(hi, 8);
2444 getStreamer().EmitIntValue(hi, 8);
2445 getStreamer().EmitIntValue(lo, 8);
2448 if (getLexer().is(AsmToken::EndOfStatement))
2451 // FIXME: Improve diagnostic.
2452 if (getLexer().isNot(AsmToken::Comma))
2453 return TokError("unexpected token in directive");
2462 /// parseDirectiveRealValue
2463 /// ::= (.single | .double) [ expression (, expression)* ]
2464 bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
2465 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2466 checkForValidSection();
2469 // We don't truly support arithmetic on floating point expressions, so we
2470 // have to manually parse unary prefixes.
2472 if (getLexer().is(AsmToken::Minus)) {
2475 } else if (getLexer().is(AsmToken::Plus))
2478 if (getLexer().isNot(AsmToken::Integer) &&
2479 getLexer().isNot(AsmToken::Real) &&
2480 getLexer().isNot(AsmToken::Identifier))
2481 return TokError("unexpected token in directive");
2483 // Convert to an APFloat.
2484 APFloat Value(Semantics);
2485 StringRef IDVal = getTok().getString();
2486 if (getLexer().is(AsmToken::Identifier)) {
2487 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2488 Value = APFloat::getInf(Semantics);
2489 else if (!IDVal.compare_lower("nan"))
2490 Value = APFloat::getNaN(Semantics, false, ~0);
2492 return TokError("invalid floating point literal");
2493 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
2494 APFloat::opInvalidOp)
2495 return TokError("invalid floating point literal");
2499 // Consume the numeric token.
2502 // Emit the value as an integer.
2503 APInt AsInt = Value.bitcastToAPInt();
2504 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
2505 AsInt.getBitWidth() / 8);
2507 if (getLexer().is(AsmToken::EndOfStatement))
2510 if (getLexer().isNot(AsmToken::Comma))
2511 return TokError("unexpected token in directive");
2520 /// parseDirectiveZero
2521 /// ::= .zero expression
2522 bool AsmParser::parseDirectiveZero() {
2523 checkForValidSection();
2526 if (parseAbsoluteExpression(NumBytes))
2530 if (getLexer().is(AsmToken::Comma)) {
2532 if (parseAbsoluteExpression(Val))
2536 if (getLexer().isNot(AsmToken::EndOfStatement))
2537 return TokError("unexpected token in '.zero' directive");
2541 getStreamer().EmitFill(NumBytes, Val);
2546 /// parseDirectiveFill
2547 /// ::= .fill expression [ , expression [ , expression ] ]
2548 bool AsmParser::parseDirectiveFill() {
2549 checkForValidSection();
2551 SMLoc RepeatLoc = getLexer().getLoc();
2553 if (parseAbsoluteExpression(NumValues))
2556 if (NumValues < 0) {
2558 "'.fill' directive with negative repeat count has no effect");
2562 int64_t FillSize = 1;
2563 int64_t FillExpr = 0;
2565 SMLoc SizeLoc, ExprLoc;
2566 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2567 if (getLexer().isNot(AsmToken::Comma))
2568 return TokError("unexpected token in '.fill' directive");
2571 SizeLoc = getLexer().getLoc();
2572 if (parseAbsoluteExpression(FillSize))
2575 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2576 if (getLexer().isNot(AsmToken::Comma))
2577 return TokError("unexpected token in '.fill' directive");
2580 ExprLoc = getLexer().getLoc();
2581 if (parseAbsoluteExpression(FillExpr))
2584 if (getLexer().isNot(AsmToken::EndOfStatement))
2585 return TokError("unexpected token in '.fill' directive");
2592 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2596 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2600 if (!isUInt<32>(FillExpr) && FillSize > 4)
2601 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2603 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2604 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2606 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2607 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2608 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2614 /// parseDirectiveOrg
2615 /// ::= .org expression [ , expression ]
2616 bool AsmParser::parseDirectiveOrg() {
2617 checkForValidSection();
2619 const MCExpr *Offset;
2620 SMLoc Loc = getTok().getLoc();
2621 if (parseExpression(Offset))
2624 // Parse optional fill expression.
2625 int64_t FillExpr = 0;
2626 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2627 if (getLexer().isNot(AsmToken::Comma))
2628 return TokError("unexpected token in '.org' directive");
2631 if (parseAbsoluteExpression(FillExpr))
2634 if (getLexer().isNot(AsmToken::EndOfStatement))
2635 return TokError("unexpected token in '.org' directive");
2640 // Only limited forms of relocatable expressions are accepted here, it
2641 // has to be relative to the current section. The streamer will return
2642 // 'true' if the expression wasn't evaluatable.
2643 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2644 return Error(Loc, "expected assembly-time absolute expression");
2649 /// parseDirectiveAlign
2650 /// ::= {.align, ...} expression [ , expression [ , expression ]]
2651 bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
2652 checkForValidSection();
2654 SMLoc AlignmentLoc = getLexer().getLoc();
2656 if (parseAbsoluteExpression(Alignment))
2660 bool HasFillExpr = false;
2661 int64_t FillExpr = 0;
2662 int64_t MaxBytesToFill = 0;
2663 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2664 if (getLexer().isNot(AsmToken::Comma))
2665 return TokError("unexpected token in directive");
2668 // The fill expression can be omitted while specifying a maximum number of
2669 // alignment bytes, e.g:
2671 if (getLexer().isNot(AsmToken::Comma)) {
2673 if (parseAbsoluteExpression(FillExpr))
2677 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2678 if (getLexer().isNot(AsmToken::Comma))
2679 return TokError("unexpected token in directive");
2682 MaxBytesLoc = getLexer().getLoc();
2683 if (parseAbsoluteExpression(MaxBytesToFill))
2686 if (getLexer().isNot(AsmToken::EndOfStatement))
2687 return TokError("unexpected token in directive");
2696 // Compute alignment in bytes.
2698 // FIXME: Diagnose overflow.
2699 if (Alignment >= 32) {
2700 Error(AlignmentLoc, "invalid alignment value");
2704 Alignment = 1ULL << Alignment;
2706 // Reject alignments that aren't a power of two, for gas compatibility.
2707 if (!isPowerOf2_64(Alignment))
2708 Error(AlignmentLoc, "alignment must be a power of 2");
2711 // Diagnose non-sensical max bytes to align.
2712 if (MaxBytesLoc.isValid()) {
2713 if (MaxBytesToFill < 1) {
2714 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2715 "many bytes, ignoring maximum bytes expression");
2719 if (MaxBytesToFill >= Alignment) {
2720 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2726 // Check whether we should use optimal code alignment for this .align
2728 const MCSection *Section = getStreamer().getCurrentSection().first;
2729 assert(Section && "must have section to emit alignment");
2730 bool UseCodeAlign = Section->UseCodeAlign();
2731 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2732 ValueSize == 1 && UseCodeAlign) {
2733 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
2735 // FIXME: Target specific behavior about how the "extra" bytes are filled.
2736 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2743 /// parseDirectiveFile
2744 /// ::= .file [number] filename
2745 /// ::= .file number directory filename
2746 bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
2747 // FIXME: I'm not sure what this is.
2748 int64_t FileNumber = -1;
2749 SMLoc FileNumberLoc = getLexer().getLoc();
2750 if (getLexer().is(AsmToken::Integer)) {
2751 FileNumber = getTok().getIntVal();
2755 return TokError("file number less than one");
2758 if (getLexer().isNot(AsmToken::String))
2759 return TokError("unexpected token in '.file' directive");
2761 // Usually the directory and filename together, otherwise just the directory.
2762 // Allow the strings to have escaped octal character sequence.
2763 std::string Path = getTok().getString();
2764 if (parseEscapedString(Path))
2768 StringRef Directory;
2770 std::string FilenameData;
2771 if (getLexer().is(AsmToken::String)) {
2772 if (FileNumber == -1)
2773 return TokError("explicit path specified, but no file number");
2774 if (parseEscapedString(FilenameData))
2776 Filename = FilenameData;
2783 if (getLexer().isNot(AsmToken::EndOfStatement))
2784 return TokError("unexpected token in '.file' directive");
2786 if (FileNumber == -1)
2787 getStreamer().EmitFileDirective(Filename);
2789 if (getContext().getGenDwarfForAssembly() == true)
2791 "input can't have .file dwarf directives when -g is "
2792 "used to generate dwarf debug info for assembly code");
2794 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2796 Error(FileNumberLoc, "file number already allocated");
2802 /// parseDirectiveLine
2803 /// ::= .line [number]
2804 bool AsmParser::parseDirectiveLine() {
2805 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2806 if (getLexer().isNot(AsmToken::Integer))
2807 return TokError("unexpected token in '.line' directive");
2809 int64_t LineNumber = getTok().getIntVal();
2813 // FIXME: Do something with the .line.
2816 if (getLexer().isNot(AsmToken::EndOfStatement))
2817 return TokError("unexpected token in '.line' directive");
2822 /// parseDirectiveLoc
2823 /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2824 /// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2825 /// The first number is a file number, must have been previously assigned with
2826 /// a .file directive, the second number is the line number and optionally the
2827 /// third number is a column position (zero if not specified). The remaining
2828 /// optional items are .loc sub-directives.
2829 bool AsmParser::parseDirectiveLoc() {
2830 if (getLexer().isNot(AsmToken::Integer))
2831 return TokError("unexpected token in '.loc' directive");
2832 int64_t FileNumber = getTok().getIntVal();
2834 return TokError("file number less than one in '.loc' directive");
2835 if (!getContext().isValidDwarfFileNumber(FileNumber))
2836 return TokError("unassigned file number in '.loc' directive");
2839 int64_t LineNumber = 0;
2840 if (getLexer().is(AsmToken::Integer)) {
2841 LineNumber = getTok().getIntVal();
2843 return TokError("line number less than zero in '.loc' directive");
2847 int64_t ColumnPos = 0;
2848 if (getLexer().is(AsmToken::Integer)) {
2849 ColumnPos = getTok().getIntVal();
2851 return TokError("column position less than zero in '.loc' directive");
2855 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2857 int64_t Discriminator = 0;
2858 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2860 if (getLexer().is(AsmToken::EndOfStatement))
2864 SMLoc Loc = getTok().getLoc();
2865 if (parseIdentifier(Name))
2866 return TokError("unexpected token in '.loc' directive");
2868 if (Name == "basic_block")
2869 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2870 else if (Name == "prologue_end")
2871 Flags |= DWARF2_FLAG_PROLOGUE_END;
2872 else if (Name == "epilogue_begin")
2873 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2874 else if (Name == "is_stmt") {
2875 Loc = getTok().getLoc();
2876 const MCExpr *Value;
2877 if (parseExpression(Value))
2879 // The expression must be the constant 0 or 1.
2880 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2881 int Value = MCE->getValue();
2883 Flags &= ~DWARF2_FLAG_IS_STMT;
2884 else if (Value == 1)
2885 Flags |= DWARF2_FLAG_IS_STMT;
2887 return Error(Loc, "is_stmt value not 0 or 1");
2889 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2891 } else if (Name == "isa") {
2892 Loc = getTok().getLoc();
2893 const MCExpr *Value;
2894 if (parseExpression(Value))
2896 // The expression must be a constant greater or equal to 0.
2897 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2898 int Value = MCE->getValue();
2900 return Error(Loc, "isa number less than zero");
2903 return Error(Loc, "isa number not a constant value");
2905 } else if (Name == "discriminator") {
2906 if (parseAbsoluteExpression(Discriminator))
2909 return Error(Loc, "unknown sub-directive in '.loc' directive");
2912 if (getLexer().is(AsmToken::EndOfStatement))
2917 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2918 Isa, Discriminator, StringRef());
2923 /// parseDirectiveStabs
2924 /// ::= .stabs string, number, number, number
2925 bool AsmParser::parseDirectiveStabs() {
2926 return TokError("unsupported directive '.stabs'");
2929 /// parseDirectiveCFISections
2930 /// ::= .cfi_sections section [, section]
2931 bool AsmParser::parseDirectiveCFISections() {
2936 if (parseIdentifier(Name))
2937 return TokError("Expected an identifier");
2939 if (Name == ".eh_frame")
2941 else if (Name == ".debug_frame")
2944 if (getLexer().is(AsmToken::Comma)) {
2947 if (parseIdentifier(Name))
2948 return TokError("Expected an identifier");
2950 if (Name == ".eh_frame")
2952 else if (Name == ".debug_frame")
2956 getStreamer().EmitCFISections(EH, Debug);
2960 /// parseDirectiveCFIStartProc
2961 /// ::= .cfi_startproc [simple]
2962 bool AsmParser::parseDirectiveCFIStartProc() {
2964 if (getLexer().isNot(AsmToken::EndOfStatement))
2965 if (parseIdentifier(Simple) || Simple != "simple")
2966 return TokError("unexpected token in .cfi_startproc directive");
2968 getStreamer().EmitCFIStartProc(!Simple.empty());
2972 /// parseDirectiveCFIEndProc
2973 /// ::= .cfi_endproc
2974 bool AsmParser::parseDirectiveCFIEndProc() {
2975 getStreamer().EmitCFIEndProc();
2979 /// \brief parse register name or number.
2980 bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
2981 SMLoc DirectiveLoc) {
2984 if (getLexer().isNot(AsmToken::Integer)) {
2985 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2987 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
2989 return parseAbsoluteExpression(Register);
2994 /// parseDirectiveCFIDefCfa
2995 /// ::= .cfi_def_cfa register, offset
2996 bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
2997 int64_t Register = 0;
2998 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
3001 if (getLexer().isNot(AsmToken::Comma))
3002 return TokError("unexpected token in directive");
3006 if (parseAbsoluteExpression(Offset))
3009 getStreamer().EmitCFIDefCfa(Register, Offset);
3013 /// parseDirectiveCFIDefCfaOffset
3014 /// ::= .cfi_def_cfa_offset offset
3015 bool AsmParser::parseDirectiveCFIDefCfaOffset() {
3017 if (parseAbsoluteExpression(Offset))
3020 getStreamer().EmitCFIDefCfaOffset(Offset);
3024 /// parseDirectiveCFIRegister
3025 /// ::= .cfi_register register, register
3026 bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
3027 int64_t Register1 = 0;
3028 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
3031 if (getLexer().isNot(AsmToken::Comma))
3032 return TokError("unexpected token in directive");
3035 int64_t Register2 = 0;
3036 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
3039 getStreamer().EmitCFIRegister(Register1, Register2);
3043 /// parseDirectiveCFIWindowSave
3044 /// ::= .cfi_window_save
3045 bool AsmParser::parseDirectiveCFIWindowSave() {
3046 getStreamer().EmitCFIWindowSave();
3050 /// parseDirectiveCFIAdjustCfaOffset
3051 /// ::= .cfi_adjust_cfa_offset adjustment
3052 bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
3053 int64_t Adjustment = 0;
3054 if (parseAbsoluteExpression(Adjustment))
3057 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3061 /// parseDirectiveCFIDefCfaRegister
3062 /// ::= .cfi_def_cfa_register register
3063 bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
3064 int64_t Register = 0;
3065 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
3068 getStreamer().EmitCFIDefCfaRegister(Register);
3072 /// parseDirectiveCFIOffset
3073 /// ::= .cfi_offset register, offset
3074 bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
3075 int64_t Register = 0;
3078 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
3081 if (getLexer().isNot(AsmToken::Comma))
3082 return TokError("unexpected token in directive");
3085 if (parseAbsoluteExpression(Offset))
3088 getStreamer().EmitCFIOffset(Register, Offset);
3092 /// parseDirectiveCFIRelOffset
3093 /// ::= .cfi_rel_offset register, offset
3094 bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
3095 int64_t Register = 0;
3097 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
3100 if (getLexer().isNot(AsmToken::Comma))
3101 return TokError("unexpected token in directive");
3105 if (parseAbsoluteExpression(Offset))
3108 getStreamer().EmitCFIRelOffset(Register, Offset);
3112 static bool isValidEncoding(int64_t Encoding) {
3113 if (Encoding & ~0xff)
3116 if (Encoding == dwarf::DW_EH_PE_omit)
3119 const unsigned Format = Encoding & 0xf;
3120 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3121 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3122 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3123 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3126 const unsigned Application = Encoding & 0x70;
3127 if (Application != dwarf::DW_EH_PE_absptr &&
3128 Application != dwarf::DW_EH_PE_pcrel)
3134 /// parseDirectiveCFIPersonalityOrLsda
3135 /// IsPersonality true for cfi_personality, false for cfi_lsda
3136 /// ::= .cfi_personality encoding, [symbol_name]
3137 /// ::= .cfi_lsda encoding, [symbol_name]
3138 bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
3139 int64_t Encoding = 0;
3140 if (parseAbsoluteExpression(Encoding))
3142 if (Encoding == dwarf::DW_EH_PE_omit)
3145 if (!isValidEncoding(Encoding))
3146 return TokError("unsupported encoding.");
3148 if (getLexer().isNot(AsmToken::Comma))
3149 return TokError("unexpected token in directive");
3153 if (parseIdentifier(Name))
3154 return TokError("expected identifier in directive");
3156 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3159 getStreamer().EmitCFIPersonality(Sym, Encoding);
3161 getStreamer().EmitCFILsda(Sym, Encoding);
3165 /// parseDirectiveCFIRememberState
3166 /// ::= .cfi_remember_state
3167 bool AsmParser::parseDirectiveCFIRememberState() {
3168 getStreamer().EmitCFIRememberState();
3172 /// parseDirectiveCFIRestoreState
3173 /// ::= .cfi_remember_state
3174 bool AsmParser::parseDirectiveCFIRestoreState() {
3175 getStreamer().EmitCFIRestoreState();
3179 /// parseDirectiveCFISameValue
3180 /// ::= .cfi_same_value register
3181 bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
3182 int64_t Register = 0;
3184 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
3187 getStreamer().EmitCFISameValue(Register);
3191 /// parseDirectiveCFIRestore
3192 /// ::= .cfi_restore register
3193 bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
3194 int64_t Register = 0;
3195 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
3198 getStreamer().EmitCFIRestore(Register);
3202 /// parseDirectiveCFIEscape
3203 /// ::= .cfi_escape expression[,...]
3204 bool AsmParser::parseDirectiveCFIEscape() {
3207 if (parseAbsoluteExpression(CurrValue))
3210 Values.push_back((uint8_t)CurrValue);
3212 while (getLexer().is(AsmToken::Comma)) {
3215 if (parseAbsoluteExpression(CurrValue))
3218 Values.push_back((uint8_t)CurrValue);
3221 getStreamer().EmitCFIEscape(Values);
3225 /// parseDirectiveCFISignalFrame
3226 /// ::= .cfi_signal_frame
3227 bool AsmParser::parseDirectiveCFISignalFrame() {
3228 if (getLexer().isNot(AsmToken::EndOfStatement))
3229 return Error(getLexer().getLoc(),
3230 "unexpected token in '.cfi_signal_frame'");
3232 getStreamer().EmitCFISignalFrame();
3236 /// parseDirectiveCFIUndefined
3237 /// ::= .cfi_undefined register
3238 bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
3239 int64_t Register = 0;
3241 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
3244 getStreamer().EmitCFIUndefined(Register);
3248 /// parseDirectiveMacrosOnOff
3251 bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
3252 if (getLexer().isNot(AsmToken::EndOfStatement))
3253 return Error(getLexer().getLoc(),
3254 "unexpected token in '" + Directive + "' directive");
3256 setMacrosEnabled(Directive == ".macros_on");
3260 /// parseDirectiveMacro
3261 /// ::= .macro name[,] [parameters]
3262 bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
3264 if (parseIdentifier(Name))
3265 return TokError("expected identifier in '.macro' directive");
3267 if (getLexer().is(AsmToken::Comma))
3270 MCAsmMacroParameters Parameters;
3271 while (getLexer().isNot(AsmToken::EndOfStatement)) {
3273 if (Parameters.size() && Parameters.back().Vararg)
3274 return Error(Lexer.getLoc(),
3275 "Vararg parameter '" + Parameters.back().Name +
3276 "' should be last one in the list of parameters.");
3278 MCAsmMacroParameter Parameter;
3279 if (parseIdentifier(Parameter.Name))
3280 return TokError("expected identifier in '.macro' directive");
3282 if (Lexer.is(AsmToken::Colon)) {
3283 Lex(); // consume ':'
3286 StringRef Qualifier;
3288 QualLoc = Lexer.getLoc();
3289 if (parseIdentifier(Qualifier))
3290 return Error(QualLoc, "missing parameter qualifier for "
3291 "'" + Parameter.Name + "' in macro '" + Name + "'");
3293 if (Qualifier == "req")
3294 Parameter.Required = true;
3295 else if (Qualifier == "vararg" && !IsDarwin)
3296 Parameter.Vararg = true;
3298 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3299 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3302 if (getLexer().is(AsmToken::Equal)) {
3307 ParamLoc = Lexer.getLoc();
3308 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
3311 if (Parameter.Required)
3312 Warning(ParamLoc, "pointless default value for required parameter "
3313 "'" + Parameter.Name + "' in macro '" + Name + "'");
3316 Parameters.push_back(Parameter);
3318 if (getLexer().is(AsmToken::Comma))
3322 // Eat the end of statement.
3325 AsmToken EndToken, StartToken = getTok();
3326 unsigned MacroDepth = 0;
3328 // Lex the macro definition.
3330 // Check whether we have reached the end of the file.
3331 if (getLexer().is(AsmToken::Eof))
3332 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3334 // Otherwise, check whether we have reach the .endmacro.
3335 if (getLexer().is(AsmToken::Identifier)) {
3336 if (getTok().getIdentifier() == ".endm" ||
3337 getTok().getIdentifier() == ".endmacro") {
3338 if (MacroDepth == 0) { // Outermost macro.
3339 EndToken = getTok();
3341 if (getLexer().isNot(AsmToken::EndOfStatement))
3342 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3346 // Otherwise we just found the end of an inner macro.
3349 } else if (getTok().getIdentifier() == ".macro") {
3350 // We allow nested macros. Those aren't instantiated until the outermost
3351 // macro is expanded so just ignore them for now.
3356 // Otherwise, scan til the end of the statement.
3357 eatToEndOfStatement();
3360 if (lookupMacro(Name)) {
3361 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3364 const char *BodyStart = StartToken.getLoc().getPointer();
3365 const char *BodyEnd = EndToken.getLoc().getPointer();
3366 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3367 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3368 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
3372 /// checkForBadMacro
3374 /// With the support added for named parameters there may be code out there that
3375 /// is transitioning from positional parameters. In versions of gas that did
3376 /// not support named parameters they would be ignored on the macro definition.
3377 /// But to support both styles of parameters this is not possible so if a macro
3378 /// definition has named parameters but does not use them and has what appears
3379 /// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3380 /// warning that the positional parameter found in body which have no effect.
3381 /// Hoping the developer will either remove the named parameters from the macro
3382 /// definition so the positional parameters get used if that was what was
3383 /// intended or change the macro to use the named parameters. It is possible
3384 /// this warning will trigger when the none of the named parameters are used
3385 /// and the strings like $1 are infact to simply to be passed trough unchanged.
3386 void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
3388 ArrayRef<MCAsmMacroParameter> Parameters) {
3389 // If this macro is not defined with named parameters the warning we are
3390 // checking for here doesn't apply.
3391 unsigned NParameters = Parameters.size();
3392 if (NParameters == 0)
3395 bool NamedParametersFound = false;
3396 bool PositionalParametersFound = false;
3398 // Look at the body of the macro for use of both the named parameters and what
3399 // are likely to be positional parameters. This is what expandMacro() is
3400 // doing when it finds the parameters in the body.
3401 while (!Body.empty()) {
3402 // Scan for the next possible parameter.
3403 std::size_t End = Body.size(), Pos = 0;
3404 for (; Pos != End; ++Pos) {
3405 // Check for a substitution or escape.
3406 // This macro is defined with parameters, look for \foo, \bar, etc.
3407 if (Body[Pos] == '\\' && Pos + 1 != End)
3410 // This macro should have parameters, but look for $0, $1, ..., $n too.
3411 if (Body[Pos] != '$' || Pos + 1 == End)
3413 char Next = Body[Pos + 1];
3414 if (Next == '$' || Next == 'n' ||
3415 isdigit(static_cast<unsigned char>(Next)))
3419 // Check if we reached the end.
3423 if (Body[Pos] == '$') {
3424 switch (Body[Pos + 1]) {
3429 // $n => number of arguments
3431 PositionalParametersFound = true;
3434 // $[0-9] => argument
3436 PositionalParametersFound = true;
3442 unsigned I = Pos + 1;
3443 while (isIdentifierChar(Body[I]) && I + 1 != End)
3446 const char *Begin = Body.data() + Pos + 1;
3447 StringRef Argument(Begin, I - (Pos + 1));
3449 for (; Index < NParameters; ++Index)
3450 if (Parameters[Index].Name == Argument)
3453 if (Index == NParameters) {
3454 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3460 NamedParametersFound = true;
3461 Pos += 1 + Argument.size();
3464 // Update the scan point.
3465 Body = Body.substr(Pos);
3468 if (!NamedParametersFound && PositionalParametersFound)
3469 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3470 "used in macro body, possible positional parameter "
3471 "found in body which will have no effect");
3474 /// parseDirectiveEndMacro
3477 bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
3478 if (getLexer().isNot(AsmToken::EndOfStatement))
3479 return TokError("unexpected token in '" + Directive + "' directive");
3481 // If we are inside a macro instantiation, terminate the current
3483 if (isInsideMacroInstantiation()) {
3488 // Otherwise, this .endmacro is a stray entry in the file; well formed
3489 // .endmacro directives are handled during the macro definition parsing.
3490 return TokError("unexpected '" + Directive + "' in file, "
3491 "no current macro definition");
3494 /// parseDirectivePurgeMacro
3496 bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
3498 if (parseIdentifier(Name))
3499 return TokError("expected identifier in '.purgem' directive");
3501 if (getLexer().isNot(AsmToken::EndOfStatement))
3502 return TokError("unexpected token in '.purgem' directive");
3504 if (!lookupMacro(Name))
3505 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3507 undefineMacro(Name);
3511 /// parseDirectiveBundleAlignMode
3512 /// ::= {.bundle_align_mode} expression
3513 bool AsmParser::parseDirectiveBundleAlignMode() {
3514 checkForValidSection();
3516 // Expect a single argument: an expression that evaluates to a constant
3517 // in the inclusive range 0-30.
3518 SMLoc ExprLoc = getLexer().getLoc();
3519 int64_t AlignSizePow2;
3520 if (parseAbsoluteExpression(AlignSizePow2))
3522 else if (getLexer().isNot(AsmToken::EndOfStatement))
3523 return TokError("unexpected token after expression in"
3524 " '.bundle_align_mode' directive");
3525 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3526 return Error(ExprLoc,
3527 "invalid bundle alignment size (expected between 0 and 30)");
3531 // Because of AlignSizePow2's verified range we can safely truncate it to
3533 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3537 /// parseDirectiveBundleLock
3538 /// ::= {.bundle_lock} [align_to_end]
3539 bool AsmParser::parseDirectiveBundleLock() {
3540 checkForValidSection();
3541 bool AlignToEnd = false;
3543 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3545 SMLoc Loc = getTok().getLoc();
3546 const char *kInvalidOptionError =
3547 "invalid option for '.bundle_lock' directive";
3549 if (parseIdentifier(Option))
3550 return Error(Loc, kInvalidOptionError);
3552 if (Option != "align_to_end")
3553 return Error(Loc, kInvalidOptionError);
3554 else if (getLexer().isNot(AsmToken::EndOfStatement))
3556 "unexpected token after '.bundle_lock' directive option");
3562 getStreamer().EmitBundleLock(AlignToEnd);
3566 /// parseDirectiveBundleLock
3567 /// ::= {.bundle_lock}
3568 bool AsmParser::parseDirectiveBundleUnlock() {
3569 checkForValidSection();
3571 if (getLexer().isNot(AsmToken::EndOfStatement))
3572 return TokError("unexpected token in '.bundle_unlock' directive");
3575 getStreamer().EmitBundleUnlock();
3579 /// parseDirectiveSpace
3580 /// ::= (.skip | .space) expression [ , expression ]
3581 bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
3582 checkForValidSection();
3585 if (parseAbsoluteExpression(NumBytes))
3588 int64_t FillExpr = 0;
3589 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3590 if (getLexer().isNot(AsmToken::Comma))
3591 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3594 if (parseAbsoluteExpression(FillExpr))
3597 if (getLexer().isNot(AsmToken::EndOfStatement))
3598 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3604 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3607 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
3608 getStreamer().EmitFill(NumBytes, FillExpr);
3613 /// parseDirectiveLEB128
3614 /// ::= (.sleb128 | .uleb128) expression
3615 bool AsmParser::parseDirectiveLEB128(bool Signed) {
3616 checkForValidSection();
3617 const MCExpr *Value;
3619 if (parseExpression(Value))
3622 if (getLexer().isNot(AsmToken::EndOfStatement))
3623 return TokError("unexpected token in directive");
3626 getStreamer().EmitSLEB128Value(Value);
3628 getStreamer().EmitULEB128Value(Value);
3633 /// parseDirectiveSymbolAttribute
3634 /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
3635 bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
3636 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3639 SMLoc Loc = getTok().getLoc();
3641 if (parseIdentifier(Name))
3642 return Error(Loc, "expected identifier in directive");
3644 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3646 // Assembler local symbols don't make any sense here. Complain loudly.
3647 if (Sym->isTemporary())
3648 return Error(Loc, "non-local symbol required in directive");
3650 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3651 return Error(Loc, "unable to emit symbol attribute");
3653 if (getLexer().is(AsmToken::EndOfStatement))
3656 if (getLexer().isNot(AsmToken::Comma))
3657 return TokError("unexpected token in directive");
3666 /// parseDirectiveComm
3667 /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
3668 bool AsmParser::parseDirectiveComm(bool IsLocal) {
3669 checkForValidSection();
3671 SMLoc IDLoc = getLexer().getLoc();
3673 if (parseIdentifier(Name))
3674 return TokError("expected identifier in directive");
3676 // Handle the identifier as the key symbol.
3677 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3679 if (getLexer().isNot(AsmToken::Comma))
3680 return TokError("unexpected token in directive");
3684 SMLoc SizeLoc = getLexer().getLoc();
3685 if (parseAbsoluteExpression(Size))
3688 int64_t Pow2Alignment = 0;
3689 SMLoc Pow2AlignmentLoc;
3690 if (getLexer().is(AsmToken::Comma)) {
3692 Pow2AlignmentLoc = getLexer().getLoc();
3693 if (parseAbsoluteExpression(Pow2Alignment))
3696 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3697 if (IsLocal && LCOMM == LCOMM::NoAlignment)
3698 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3700 // If this target takes alignments in bytes (not log) validate and convert.
3701 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3702 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
3703 if (!isPowerOf2_64(Pow2Alignment))
3704 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3705 Pow2Alignment = Log2_64(Pow2Alignment);
3709 if (getLexer().isNot(AsmToken::EndOfStatement))
3710 return TokError("unexpected token in '.comm' or '.lcomm' directive");
3714 // NOTE: a size of zero for a .comm should create a undefined symbol
3715 // but a size of .lcomm creates a bss symbol of size zero.
3717 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
3718 "be less than zero");
3720 // NOTE: The alignment in the directive is a power of 2 value, the assembler
3721 // may internally end up wanting an alignment in bytes.
3722 // FIXME: Diagnose overflow.
3723 if (Pow2Alignment < 0)
3724 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
3725 "alignment, can't be less than zero");
3727 if (!Sym->isUndefined())
3728 return Error(IDLoc, "invalid symbol redefinition");
3730 // Create the Symbol as a common or local common with Size and Pow2Alignment
3732 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
3736 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
3740 /// parseDirectiveAbort
3741 /// ::= .abort [... message ...]
3742 bool AsmParser::parseDirectiveAbort() {
3743 // FIXME: Use loc from directive.
3744 SMLoc Loc = getLexer().getLoc();
3746 StringRef Str = parseStringToEndOfStatement();
3747 if (getLexer().isNot(AsmToken::EndOfStatement))
3748 return TokError("unexpected token in '.abort' directive");
3753 Error(Loc, ".abort detected. Assembly stopping.");
3755 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
3756 // FIXME: Actually abort assembly here.
3761 /// parseDirectiveInclude
3762 /// ::= .include "filename"
3763 bool AsmParser::parseDirectiveInclude() {
3764 if (getLexer().isNot(AsmToken::String))
3765 return TokError("expected string in '.include' directive");
3767 // Allow the strings to have escaped octal character sequence.
3768 std::string Filename;
3769 if (parseEscapedString(Filename))
3771 SMLoc IncludeLoc = getLexer().getLoc();
3774 if (getLexer().isNot(AsmToken::EndOfStatement))
3775 return TokError("unexpected token in '.include' directive");
3777 // Attempt to switch the lexer to the included file before consuming the end
3778 // of statement to avoid losing it when we switch.
3779 if (enterIncludeFile(Filename)) {
3780 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
3787 /// parseDirectiveIncbin
3788 /// ::= .incbin "filename"
3789 bool AsmParser::parseDirectiveIncbin() {
3790 if (getLexer().isNot(AsmToken::String))
3791 return TokError("expected string in '.incbin' directive");
3793 // Allow the strings to have escaped octal character sequence.
3794 std::string Filename;
3795 if (parseEscapedString(Filename))
3797 SMLoc IncbinLoc = getLexer().getLoc();
3800 if (getLexer().isNot(AsmToken::EndOfStatement))
3801 return TokError("unexpected token in '.incbin' directive");
3803 // Attempt to process the included file.
3804 if (processIncbinFile(Filename)) {
3805 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3812 /// parseDirectiveIf
3813 /// ::= .if{,eq,ge,gt,le,lt,ne} expression
3814 bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
3815 TheCondStack.push_back(TheCondState);
3816 TheCondState.TheCond = AsmCond::IfCond;
3817 if (TheCondState.Ignore) {
3818 eatToEndOfStatement();
3821 if (parseAbsoluteExpression(ExprValue))
3824 if (getLexer().isNot(AsmToken::EndOfStatement))
3825 return TokError("unexpected token in '.if' directive");
3831 llvm_unreachable("unsupported directive");
3836 ExprValue = ExprValue == 0;
3839 ExprValue = ExprValue >= 0;
3842 ExprValue = ExprValue > 0;
3845 ExprValue = ExprValue <= 0;
3848 ExprValue = ExprValue < 0;
3852 TheCondState.CondMet = ExprValue;
3853 TheCondState.Ignore = !TheCondState.CondMet;
3859 /// parseDirectiveIfb
3861 bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
3862 TheCondStack.push_back(TheCondState);
3863 TheCondState.TheCond = AsmCond::IfCond;
3865 if (TheCondState.Ignore) {
3866 eatToEndOfStatement();
3868 StringRef Str = parseStringToEndOfStatement();
3870 if (getLexer().isNot(AsmToken::EndOfStatement))
3871 return TokError("unexpected token in '.ifb' directive");
3875 TheCondState.CondMet = ExpectBlank == Str.empty();
3876 TheCondState.Ignore = !TheCondState.CondMet;
3882 /// parseDirectiveIfc
3883 /// ::= .ifc string1, string2
3884 /// ::= .ifnc string1, string2
3885 bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
3886 TheCondStack.push_back(TheCondState);
3887 TheCondState.TheCond = AsmCond::IfCond;
3889 if (TheCondState.Ignore) {
3890 eatToEndOfStatement();
3892 StringRef Str1 = parseStringToComma();
3894 if (getLexer().isNot(AsmToken::Comma))
3895 return TokError("unexpected token in '.ifc' directive");
3899 StringRef Str2 = parseStringToEndOfStatement();
3901 if (getLexer().isNot(AsmToken::EndOfStatement))
3902 return TokError("unexpected token in '.ifc' directive");
3906 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
3907 TheCondState.Ignore = !TheCondState.CondMet;
3913 /// parseDirectiveIfeqs
3914 /// ::= .ifeqs string1, string2
3915 bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc) {
3916 if (Lexer.isNot(AsmToken::String)) {
3917 TokError("expected string parameter for '.ifeqs' directive");
3918 eatToEndOfStatement();
3922 StringRef String1 = getTok().getStringContents();
3925 if (Lexer.isNot(AsmToken::Comma)) {
3926 TokError("expected comma after first string for '.ifeqs' directive");
3927 eatToEndOfStatement();
3933 if (Lexer.isNot(AsmToken::String)) {
3934 TokError("expected string parameter for '.ifeqs' directive");
3935 eatToEndOfStatement();
3939 StringRef String2 = getTok().getStringContents();
3942 TheCondStack.push_back(TheCondState);
3943 TheCondState.TheCond = AsmCond::IfCond;
3944 TheCondState.CondMet = String1 == String2;
3945 TheCondState.Ignore = !TheCondState.CondMet;
3950 /// parseDirectiveIfdef
3951 /// ::= .ifdef symbol
3952 bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
3954 TheCondStack.push_back(TheCondState);
3955 TheCondState.TheCond = AsmCond::IfCond;
3957 if (TheCondState.Ignore) {
3958 eatToEndOfStatement();
3960 if (parseIdentifier(Name))
3961 return TokError("expected identifier after '.ifdef'");
3965 MCSymbol *Sym = getContext().LookupSymbol(Name);
3968 TheCondState.CondMet = (Sym && !Sym->isUndefined());
3970 TheCondState.CondMet = (!Sym || Sym->isUndefined());
3971 TheCondState.Ignore = !TheCondState.CondMet;
3977 /// parseDirectiveElseIf
3978 /// ::= .elseif expression
3979 bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
3980 if (TheCondState.TheCond != AsmCond::IfCond &&
3981 TheCondState.TheCond != AsmCond::ElseIfCond)
3982 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3984 TheCondState.TheCond = AsmCond::ElseIfCond;
3986 bool LastIgnoreState = false;
3987 if (!TheCondStack.empty())
3988 LastIgnoreState = TheCondStack.back().Ignore;
3989 if (LastIgnoreState || TheCondState.CondMet) {
3990 TheCondState.Ignore = true;
3991 eatToEndOfStatement();
3994 if (parseAbsoluteExpression(ExprValue))
3997 if (getLexer().isNot(AsmToken::EndOfStatement))
3998 return TokError("unexpected token in '.elseif' directive");
4001 TheCondState.CondMet = ExprValue;
4002 TheCondState.Ignore = !TheCondState.CondMet;
4008 /// parseDirectiveElse
4010 bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
4011 if (getLexer().isNot(AsmToken::EndOfStatement))
4012 return TokError("unexpected token in '.else' directive");
4016 if (TheCondState.TheCond != AsmCond::IfCond &&
4017 TheCondState.TheCond != AsmCond::ElseIfCond)
4018 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4020 TheCondState.TheCond = AsmCond::ElseCond;
4021 bool LastIgnoreState = false;
4022 if (!TheCondStack.empty())
4023 LastIgnoreState = TheCondStack.back().Ignore;
4024 if (LastIgnoreState || TheCondState.CondMet)
4025 TheCondState.Ignore = true;
4027 TheCondState.Ignore = false;
4032 /// parseDirectiveEnd
4034 bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4035 if (getLexer().isNot(AsmToken::EndOfStatement))
4036 return TokError("unexpected token in '.end' directive");
4040 while (Lexer.isNot(AsmToken::Eof))
4046 /// parseDirectiveError
4048 /// ::= .error [string]
4049 bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4050 if (!TheCondStack.empty()) {
4051 if (TheCondStack.back().Ignore) {
4052 eatToEndOfStatement();
4058 return Error(L, ".err encountered");
4060 StringRef Message = ".error directive invoked in source file";
4061 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4062 if (Lexer.isNot(AsmToken::String)) {
4063 TokError(".error argument must be a string");
4064 eatToEndOfStatement();
4068 Message = getTok().getStringContents();
4076 /// parseDirectiveEndIf
4078 bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
4079 if (getLexer().isNot(AsmToken::EndOfStatement))
4080 return TokError("unexpected token in '.endif' directive");
4084 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
4085 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4087 if (!TheCondStack.empty()) {
4088 TheCondState = TheCondStack.back();
4089 TheCondStack.pop_back();
4095 void AsmParser::initializeDirectiveKindMap() {
4096 DirectiveKindMap[".set"] = DK_SET;
4097 DirectiveKindMap[".equ"] = DK_EQU;
4098 DirectiveKindMap[".equiv"] = DK_EQUIV;
4099 DirectiveKindMap[".ascii"] = DK_ASCII;
4100 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4101 DirectiveKindMap[".string"] = DK_STRING;
4102 DirectiveKindMap[".byte"] = DK_BYTE;
4103 DirectiveKindMap[".short"] = DK_SHORT;
4104 DirectiveKindMap[".value"] = DK_VALUE;
4105 DirectiveKindMap[".2byte"] = DK_2BYTE;
4106 DirectiveKindMap[".long"] = DK_LONG;
4107 DirectiveKindMap[".int"] = DK_INT;
4108 DirectiveKindMap[".4byte"] = DK_4BYTE;
4109 DirectiveKindMap[".quad"] = DK_QUAD;
4110 DirectiveKindMap[".8byte"] = DK_8BYTE;
4111 DirectiveKindMap[".octa"] = DK_OCTA;
4112 DirectiveKindMap[".single"] = DK_SINGLE;
4113 DirectiveKindMap[".float"] = DK_FLOAT;
4114 DirectiveKindMap[".double"] = DK_DOUBLE;
4115 DirectiveKindMap[".align"] = DK_ALIGN;
4116 DirectiveKindMap[".align32"] = DK_ALIGN32;
4117 DirectiveKindMap[".balign"] = DK_BALIGN;
4118 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4119 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4120 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4121 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4122 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4123 DirectiveKindMap[".org"] = DK_ORG;
4124 DirectiveKindMap[".fill"] = DK_FILL;
4125 DirectiveKindMap[".zero"] = DK_ZERO;
4126 DirectiveKindMap[".extern"] = DK_EXTERN;
4127 DirectiveKindMap[".globl"] = DK_GLOBL;
4128 DirectiveKindMap[".global"] = DK_GLOBAL;
4129 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4130 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4131 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4132 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4133 DirectiveKindMap[".reference"] = DK_REFERENCE;
4134 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4135 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4136 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4137 DirectiveKindMap[".comm"] = DK_COMM;
4138 DirectiveKindMap[".common"] = DK_COMMON;
4139 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4140 DirectiveKindMap[".abort"] = DK_ABORT;
4141 DirectiveKindMap[".include"] = DK_INCLUDE;
4142 DirectiveKindMap[".incbin"] = DK_INCBIN;
4143 DirectiveKindMap[".code16"] = DK_CODE16;
4144 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4145 DirectiveKindMap[".rept"] = DK_REPT;
4146 DirectiveKindMap[".rep"] = DK_REPT;
4147 DirectiveKindMap[".irp"] = DK_IRP;
4148 DirectiveKindMap[".irpc"] = DK_IRPC;
4149 DirectiveKindMap[".endr"] = DK_ENDR;
4150 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4151 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4152 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4153 DirectiveKindMap[".if"] = DK_IF;
4154 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4155 DirectiveKindMap[".ifge"] = DK_IFGE;
4156 DirectiveKindMap[".ifgt"] = DK_IFGT;
4157 DirectiveKindMap[".ifle"] = DK_IFLE;
4158 DirectiveKindMap[".iflt"] = DK_IFLT;
4159 DirectiveKindMap[".ifne"] = DK_IFNE;
4160 DirectiveKindMap[".ifb"] = DK_IFB;
4161 DirectiveKindMap[".ifnb"] = DK_IFNB;
4162 DirectiveKindMap[".ifc"] = DK_IFC;
4163 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
4164 DirectiveKindMap[".ifnc"] = DK_IFNC;
4165 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4166 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4167 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4168 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4169 DirectiveKindMap[".else"] = DK_ELSE;
4170 DirectiveKindMap[".end"] = DK_END;
4171 DirectiveKindMap[".endif"] = DK_ENDIF;
4172 DirectiveKindMap[".skip"] = DK_SKIP;
4173 DirectiveKindMap[".space"] = DK_SPACE;
4174 DirectiveKindMap[".file"] = DK_FILE;
4175 DirectiveKindMap[".line"] = DK_LINE;
4176 DirectiveKindMap[".loc"] = DK_LOC;
4177 DirectiveKindMap[".stabs"] = DK_STABS;
4178 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4179 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4180 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4181 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4182 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4183 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4184 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4185 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4186 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4187 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4188 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4189 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4190 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4191 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4192 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4193 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4194 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4195 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4196 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4197 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4198 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
4199 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
4200 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4201 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4202 DirectiveKindMap[".macro"] = DK_MACRO;
4203 DirectiveKindMap[".endm"] = DK_ENDM;
4204 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4205 DirectiveKindMap[".purgem"] = DK_PURGEM;
4206 DirectiveKindMap[".err"] = DK_ERR;
4207 DirectiveKindMap[".error"] = DK_ERROR;
4210 MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
4211 AsmToken EndToken, StartToken = getTok();
4213 unsigned NestLevel = 0;
4215 // Check whether we have reached the end of the file.
4216 if (getLexer().is(AsmToken::Eof)) {
4217 Error(DirectiveLoc, "no matching '.endr' in definition");
4221 if (Lexer.is(AsmToken::Identifier) &&
4222 (getTok().getIdentifier() == ".rept")) {
4226 // Otherwise, check whether we have reached the .endr.
4227 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
4228 if (NestLevel == 0) {
4229 EndToken = getTok();
4231 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4232 TokError("unexpected token in '.endr' directive");
4240 // Otherwise, scan till the end of the statement.
4241 eatToEndOfStatement();
4244 const char *BodyStart = StartToken.getLoc().getPointer();
4245 const char *BodyEnd = EndToken.getLoc().getPointer();
4246 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4248 // We Are Anonymous.
4249 MacroLikeBodies.push_back(MCAsmMacro(StringRef(), Body, None));
4250 return &MacroLikeBodies.back();
4253 void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
4254 raw_svector_ostream &OS) {
4257 MemoryBuffer *Instantiation =
4258 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
4260 // Create the macro instantiation object and add to the current macro
4261 // instantiation stack.
4262 MacroInstantiation *MI = new MacroInstantiation(
4263 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
4264 ActiveMacros.push_back(MI);
4266 // Jump to the macro instantiation and prime the lexer.
4267 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
4268 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
4272 /// parseDirectiveRept
4273 /// ::= .rep | .rept count
4274 bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
4275 const MCExpr *CountExpr;
4276 SMLoc CountLoc = getTok().getLoc();
4277 if (parseExpression(CountExpr))
4281 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4282 eatToEndOfStatement();
4283 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4287 return Error(CountLoc, "Count is negative");
4289 if (Lexer.isNot(AsmToken::EndOfStatement))
4290 return TokError("unexpected token in '" + Dir + "' directive");
4292 // Eat the end of statement.
4295 // Lex the rept definition.
4296 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
4300 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4301 // to hold the macro body with substitutions.
4302 SmallString<256> Buf;
4303 raw_svector_ostream OS(Buf);
4305 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
4308 instantiateMacroLikeBody(M, DirectiveLoc, OS);
4313 /// parseDirectiveIrp
4314 /// ::= .irp symbol,values
4315 bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
4316 MCAsmMacroParameter Parameter;
4318 if (parseIdentifier(Parameter.Name))
4319 return TokError("expected identifier in '.irp' directive");
4321 if (Lexer.isNot(AsmToken::Comma))
4322 return TokError("expected comma in '.irp' directive");
4326 MCAsmMacroArguments A;
4327 if (parseMacroArguments(nullptr, A))
4330 // Eat the end of statement.
4333 // Lex the irp definition.
4334 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
4338 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4339 // to hold the macro body with substitutions.
4340 SmallString<256> Buf;
4341 raw_svector_ostream OS(Buf);
4343 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
4344 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
4348 instantiateMacroLikeBody(M, DirectiveLoc, OS);
4353 /// parseDirectiveIrpc
4354 /// ::= .irpc symbol,values
4355 bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
4356 MCAsmMacroParameter Parameter;
4358 if (parseIdentifier(Parameter.Name))
4359 return TokError("expected identifier in '.irpc' directive");
4361 if (Lexer.isNot(AsmToken::Comma))
4362 return TokError("expected comma in '.irpc' directive");
4366 MCAsmMacroArguments A;
4367 if (parseMacroArguments(nullptr, A))
4370 if (A.size() != 1 || A.front().size() != 1)
4371 return TokError("unexpected token in '.irpc' directive");
4373 // Eat the end of statement.
4376 // Lex the irpc definition.
4377 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
4381 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4382 // to hold the macro body with substitutions.
4383 SmallString<256> Buf;
4384 raw_svector_ostream OS(Buf);
4386 StringRef Values = A.front().front().getString();
4387 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
4388 MCAsmMacroArgument Arg;
4389 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
4391 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
4395 instantiateMacroLikeBody(M, DirectiveLoc, OS);
4400 bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
4401 if (ActiveMacros.empty())
4402 return TokError("unmatched '.endr' directive");
4404 // The only .repl that should get here are the ones created by
4405 // instantiateMacroLikeBody.
4406 assert(getLexer().is(AsmToken::EndOfStatement));
4412 bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
4414 const MCExpr *Value;
4415 SMLoc ExprLoc = getLexer().getLoc();
4416 if (parseExpression(Value))
4418 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4420 return Error(ExprLoc, "unexpected expression in _emit");
4421 uint64_t IntValue = MCE->getValue();
4422 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4423 return Error(ExprLoc, "literal value out of range for directive");
4425 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4429 bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
4430 const MCExpr *Value;
4431 SMLoc ExprLoc = getLexer().getLoc();
4432 if (parseExpression(Value))
4434 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4436 return Error(ExprLoc, "unexpected expression in align");
4437 uint64_t IntValue = MCE->getValue();
4438 if (!isPowerOf2_64(IntValue))
4439 return Error(ExprLoc, "literal value not a power of two greater then zero");
4441 Info.AsmRewrites->push_back(
4442 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
4446 // We are comparing pointers, but the pointers are relative to a single string.
4447 // Thus, this should always be deterministic.
4448 static int rewritesSort(const AsmRewrite *AsmRewriteA,
4449 const AsmRewrite *AsmRewriteB) {
4450 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4452 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4455 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4456 // rewrite to the same location. Make sure the SizeDirective rewrite is
4457 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4458 // ensures the sort algorithm is stable.
4459 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4460 AsmRewritePrecedence[AsmRewriteB->Kind])
4463 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4464 AsmRewritePrecedence[AsmRewriteB->Kind])
4466 llvm_unreachable("Unstable rewrite sort.");
4469 bool AsmParser::parseMSInlineAsm(
4470 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4471 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4472 SmallVectorImpl<std::string> &Constraints,
4473 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4474 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
4475 SmallVector<void *, 4> InputDecls;
4476 SmallVector<void *, 4> OutputDecls;
4477 SmallVector<bool, 4> InputDeclsAddressOf;
4478 SmallVector<bool, 4> OutputDeclsAddressOf;
4479 SmallVector<std::string, 4> InputConstraints;
4480 SmallVector<std::string, 4> OutputConstraints;
4481 SmallVector<unsigned, 4> ClobberRegs;
4483 SmallVector<AsmRewrite, 4> AsmStrRewrites;
4488 // While we have input, parse each statement.
4489 unsigned InputIdx = 0;
4490 unsigned OutputIdx = 0;
4491 while (getLexer().isNot(AsmToken::Eof)) {
4492 ParseStatementInfo Info(&AsmStrRewrites);
4493 if (parseStatement(Info))
4496 if (Info.ParseError)
4499 if (Info.Opcode == ~0U)
4502 const MCInstrDesc &Desc = MII->get(Info.Opcode);
4504 // Build the list of clobbers, outputs and inputs.
4505 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
4506 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
4509 if (Operand.isImm())
4512 // Register operand.
4513 if (Operand.isReg() && !Operand.needAddressOf()) {
4514 unsigned NumDefs = Desc.getNumDefs();
4516 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
4517 ClobberRegs.push_back(Operand.getReg());
4521 // Expr/Input or Output.
4522 StringRef SymName = Operand.getSymName();
4523 if (SymName.empty())
4526 void *OpDecl = Operand.getOpDecl();
4530 bool isOutput = (i == 1) && Desc.mayStore();
4531 SMLoc Start = SMLoc::getFromPointer(SymName.data());
4534 OutputDecls.push_back(OpDecl);
4535 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
4536 OutputConstraints.push_back('=' + Operand.getConstraint().str());
4537 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
4539 InputDecls.push_back(OpDecl);
4540 InputDeclsAddressOf.push_back(Operand.needAddressOf());
4541 InputConstraints.push_back(Operand.getConstraint().str());
4542 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
4546 // Consider implicit defs to be clobbers. Think of cpuid and push.
4547 ArrayRef<uint16_t> ImpDefs(Desc.getImplicitDefs(),
4548 Desc.getNumImplicitDefs());
4549 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
4552 // Set the number of Outputs and Inputs.
4553 NumOutputs = OutputDecls.size();
4554 NumInputs = InputDecls.size();
4556 // Set the unique clobbers.
4557 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4558 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4560 Clobbers.assign(ClobberRegs.size(), std::string());
4561 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4562 raw_string_ostream OS(Clobbers[I]);
4563 IP->printRegName(OS, ClobberRegs[I]);
4566 // Merge the various outputs and inputs. Output are expected first.
4567 if (NumOutputs || NumInputs) {
4568 unsigned NumExprs = NumOutputs + NumInputs;
4569 OpDecls.resize(NumExprs);
4570 Constraints.resize(NumExprs);
4571 for (unsigned i = 0; i < NumOutputs; ++i) {
4572 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
4573 Constraints[i] = OutputConstraints[i];
4575 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
4576 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
4577 Constraints[j] = InputConstraints[i];
4581 // Build the IR assembly string.
4582 std::string AsmStringIR;
4583 raw_string_ostream OS(AsmStringIR);
4584 StringRef ASMString =
4585 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
4586 const char *AsmStart = ASMString.begin();
4587 const char *AsmEnd = ASMString.end();
4588 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
4589 for (const AsmRewrite &AR : AsmStrRewrites) {
4590 AsmRewriteKind Kind = AR.Kind;
4591 if (Kind == AOK_Delete)
4594 const char *Loc = AR.Loc.getPointer();
4595 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
4597 // Emit everything up to the immediate/expression.
4598 if (unsigned Len = Loc - AsmStart)
4599 OS << StringRef(AsmStart, Len);
4601 // Skip the original expression.
4602 if (Kind == AOK_Skip) {
4603 AsmStart = Loc + AR.Len;
4607 unsigned AdditionalSkip = 0;
4608 // Rewrite expressions in $N notation.
4613 OS << "$$" << AR.Val;
4619 OS << '$' << InputIdx++;
4622 OS << '$' << OutputIdx++;
4624 case AOK_SizeDirective:
4627 case 8: OS << "byte ptr "; break;
4628 case 16: OS << "word ptr "; break;
4629 case 32: OS << "dword ptr "; break;
4630 case 64: OS << "qword ptr "; break;
4631 case 80: OS << "xword ptr "; break;
4632 case 128: OS << "xmmword ptr "; break;
4633 case 256: OS << "ymmword ptr "; break;
4640 unsigned Val = AR.Val;
4641 OS << ".align " << Val;
4643 // Skip the original immediate.
4644 assert(Val < 10 && "Expected alignment less then 2^10.");
4645 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4648 case AOK_DotOperator:
4649 // Insert the dot if the user omitted it.
4651 if (AsmStringIR.back() != '.')
4657 // Skip the original expression.
4658 AsmStart = Loc + AR.Len + AdditionalSkip;
4661 // Emit the remainder of the asm string.
4662 if (AsmStart != AsmEnd)
4663 OS << StringRef(AsmStart, AsmEnd - AsmStart);
4665 AsmString = OS.str();
4669 /// \brief Create an MCAsmParser instance.
4670 MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4671 MCStreamer &Out, const MCAsmInfo &MAI) {
4672 return new AsmParser(SM, C, Out, MAI);