| //===- AsmParser.cpp - Parser for Assembly Files --------------------------===// |
| // |
| // The LLVM Compiler Infrastructure |
| // |
| // This file is distributed under the University of Illinois Open Source |
| // License. See LICENSE.TXT for details. |
| // |
| //===----------------------------------------------------------------------===// |
| // |
| // This class implements the parser for assembly files. |
| // |
| //===----------------------------------------------------------------------===// |
| |
| #include "llvm/ADT/APFloat.h" |
| #include "llvm/ADT/APInt.h" |
| #include "llvm/ADT/ArrayRef.h" |
| #include "llvm/ADT/None.h" |
| #include "llvm/ADT/STLExtras.h" |
| #include "llvm/ADT/SmallString.h" |
| #include "llvm/ADT/SmallVector.h" |
| #include "llvm/ADT/StringExtras.h" |
| #include "llvm/ADT/StringMap.h" |
| #include "llvm/ADT/StringRef.h" |
| #include "llvm/ADT/Twine.h" |
| #include "llvm/BinaryFormat/Dwarf.h" |
| #include "llvm/MC/MCAsmInfo.h" |
| #include "llvm/MC/MCCodeView.h" |
| #include "llvm/MC/MCContext.h" |
| #include "llvm/MC/MCDirectives.h" |
| #include "llvm/MC/MCDwarf.h" |
| #include "llvm/MC/MCExpr.h" |
| #include "llvm/MC/MCInstPrinter.h" |
| #include "llvm/MC/MCInstrDesc.h" |
| #include "llvm/MC/MCInstrInfo.h" |
| #include "llvm/MC/MCObjectFileInfo.h" |
| #include "llvm/MC/MCParser/AsmCond.h" |
| #include "llvm/MC/MCParser/AsmLexer.h" |
| #include "llvm/MC/MCParser/MCAsmLexer.h" |
| #include "llvm/MC/MCParser/MCAsmParser.h" |
| #include "llvm/MC/MCParser/MCAsmParserExtension.h" |
| #include "llvm/MC/MCParser/MCAsmParserUtils.h" |
| #include "llvm/MC/MCParser/MCParsedAsmOperand.h" |
| #include "llvm/MC/MCParser/MCTargetAsmParser.h" |
| #include "llvm/MC/MCRegisterInfo.h" |
| #include "llvm/MC/MCSection.h" |
| #include "llvm/MC/MCStreamer.h" |
| #include "llvm/MC/MCSymbol.h" |
| #include "llvm/MC/MCTargetOptions.h" |
| #include "llvm/MC/MCValue.h" |
| #include "llvm/Support/Casting.h" |
| #include "llvm/Support/CommandLine.h" |
| #include "llvm/Support/ErrorHandling.h" |
| #include "llvm/Support/MD5.h" |
| #include "llvm/Support/MathExtras.h" |
| #include "llvm/Support/MemoryBuffer.h" |
| #include "llvm/Support/SMLoc.h" |
| #include "llvm/Support/SourceMgr.h" |
| #include "llvm/Support/raw_ostream.h" |
| #include <algorithm> |
| #include <cassert> |
| #include <cctype> |
| #include <climits> |
| #include <cstddef> |
| #include <cstdint> |
| #include <deque> |
| #include <memory> |
| #include <sstream> |
| #include <string> |
| #include <tuple> |
| #include <utility> |
| #include <vector> |
| |
| using namespace llvm; |
| |
| MCAsmParserSemaCallback::~MCAsmParserSemaCallback() = default; |
| |
| static cl::opt<unsigned> AsmMacroMaxNestingDepth( |
| "asm-macro-max-nesting-depth", cl::init(20), cl::Hidden, |
| cl::desc("The maximum nesting depth allowed for assembly macros.")); |
| |
| namespace { |
| |
| /// Helper types for tracking macro definitions. |
| typedef std::vector<AsmToken> MCAsmMacroArgument; |
| typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments; |
| |
| /// Helper class for storing information about an active macro |
| /// instantiation. |
| struct MacroInstantiation { |
| /// The location of the instantiation. |
| SMLoc InstantiationLoc; |
| |
| /// The buffer where parsing should resume upon instantiation completion. |
| int ExitBuffer; |
| |
| /// The location where parsing should resume upon instantiation completion. |
| SMLoc ExitLoc; |
| |
| /// The depth of TheCondStack at the start of the instantiation. |
| size_t CondStackDepth; |
| |
| public: |
| MacroInstantiation(SMLoc IL, int EB, SMLoc EL, size_t CondStackDepth); |
| }; |
| |
| struct ParseStatementInfo { |
| /// The parsed operands from the last parsed statement. |
| SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands; |
| |
| /// The opcode from the last parsed instruction. |
| unsigned Opcode = ~0U; |
| |
| /// Was there an error parsing the inline assembly? |
| bool ParseError = false; |
| |
| SmallVectorImpl<AsmRewrite> *AsmRewrites = nullptr; |
| |
| ParseStatementInfo() = delete; |
| ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites) |
| : AsmRewrites(rewrites) {} |
| }; |
| |
| /// The concrete assembly parser instance. |
| class AsmParser : public MCAsmParser { |
| private: |
| AsmLexer Lexer; |
| MCContext &Ctx; |
| MCStreamer &Out; |
| const MCAsmInfo &MAI; |
| SourceMgr &SrcMgr; |
| SourceMgr::DiagHandlerTy SavedDiagHandler; |
| void *SavedDiagContext; |
| std::unique_ptr<MCAsmParserExtension> PlatformParser; |
| |
| /// This is the current buffer index we're lexing from as managed by the |
| /// SourceMgr object. |
| unsigned CurBuffer; |
| |
| AsmCond TheCondState; |
| std::vector<AsmCond> TheCondStack; |
| |
| /// maps directive names to handler methods in parser |
| /// extensions. Extensions register themselves in this map by calling |
| /// addDirectiveHandler. |
| StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap; |
| |
| /// Stack of active macro instantiations. |
| std::vector<MacroInstantiation*> ActiveMacros; |
| |
| /// List of bodies of anonymous macros. |
| std::deque<MCAsmMacro> MacroLikeBodies; |
| |
| /// Boolean tracking whether macro substitution is enabled. |
| unsigned MacrosEnabledFlag : 1; |
| |
| /// Keeps track of how many .macro's have been instantiated. |
| unsigned NumOfMacroInstantiations; |
| |
| /// The values from the last parsed cpp hash file line comment if any. |
| struct CppHashInfoTy { |
| StringRef Filename; |
| int64_t LineNumber = 0; |
| SMLoc Loc; |
| unsigned Buf = 0; |
| }; |
| CppHashInfoTy CppHashInfo; |
| |
| /// List of forward directional labels for diagnosis at the end. |
| SmallVector<std::tuple<SMLoc, CppHashInfoTy, MCSymbol *>, 4> DirLabels; |
| |
| /// AssemblerDialect. ~OU means unset value and use value provided by MAI. |
| unsigned AssemblerDialect = ~0U; |
| |
| /// is Darwin compatibility enabled? |
| bool IsDarwin = false; |
| |
| /// Are we parsing ms-style inline assembly? |
| bool ParsingInlineAsm = false; |
| |
| /// Did we already inform the user about inconsistent MD5 usage? |
| bool ReportedInconsistentMD5 = false; |
| |
| public: |
| AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out, |
| const MCAsmInfo &MAI, unsigned CB); |
| AsmParser(const AsmParser &) = delete; |
| AsmParser &operator=(const AsmParser &) = delete; |
| ~AsmParser() override; |
| |
| bool Run(bool NoInitialTextSection, bool NoFinalize = false) override; |
| |
| void addDirectiveHandler(StringRef Directive, |
| ExtensionDirectiveHandler Handler) override { |
| ExtensionDirectiveMap[Directive] = Handler; |
| } |
| |
| void addAliasForDirective(StringRef Directive, StringRef Alias) override { |
| DirectiveKindMap[Directive] = DirectiveKindMap[Alias]; |
| } |
| |
| /// @name MCAsmParser Interface |
| /// { |
| |
| SourceMgr &getSourceManager() override { return SrcMgr; } |
| MCAsmLexer &getLexer() override { return Lexer; } |
| MCContext &getContext() override { return Ctx; } |
| MCStreamer &getStreamer() override { return Out; } |
| |
| CodeViewContext &getCVContext() { return Ctx.getCVContext(); } |
| |
| unsigned getAssemblerDialect() override { |
| if (AssemblerDialect == ~0U) |
| return MAI.getAssemblerDialect(); |
| else |
| return AssemblerDialect; |
| } |
| void setAssemblerDialect(unsigned i) override { |
| AssemblerDialect = i; |
| } |
| |
| void Note(SMLoc L, const Twine &Msg, SMRange Range = None) override; |
| bool Warning(SMLoc L, const Twine &Msg, SMRange Range = None) override; |
| bool printError(SMLoc L, const Twine &Msg, SMRange Range = None) override; |
| |
| const AsmToken &Lex() override; |
| |
| void setParsingInlineAsm(bool V) override { |
| ParsingInlineAsm = V; |
| Lexer.setParsingMSInlineAsm(V); |
| } |
| bool isParsingInlineAsm() override { return ParsingInlineAsm; } |
| |
| bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString, |
| unsigned &NumOutputs, unsigned &NumInputs, |
| SmallVectorImpl<std::pair<void *,bool>> &OpDecls, |
| SmallVectorImpl<std::string> &Constraints, |
| SmallVectorImpl<std::string> &Clobbers, |
| const MCInstrInfo *MII, const MCInstPrinter *IP, |
| MCAsmParserSemaCallback &SI) override; |
| |
| bool parseExpression(const MCExpr *&Res); |
| bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override; |
| bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override; |
| bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override; |
| bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res, |
| SMLoc &EndLoc) override; |
| bool parseAbsoluteExpression(int64_t &Res) override; |
| |
| /// Parse a floating point expression using the float \p Semantics |
| /// and set \p Res to the value. |
| bool parseRealValue(const fltSemantics &Semantics, APInt &Res); |
| |
| /// Parse an identifier or string (as a quoted identifier) |
| /// and set \p Res to the identifier contents. |
| bool parseIdentifier(StringRef &Res) override; |
| void eatToEndOfStatement() override; |
| |
| bool checkForValidSection() override; |
| |
| /// } |
| |
| private: |
| bool isAltmacroString(SMLoc &StrLoc, SMLoc &EndLoc); |
| void altMacroString(StringRef AltMacroStr, std::string &Res); |
| bool parseStatement(ParseStatementInfo &Info, |
| MCAsmParserSemaCallback *SI); |
| bool parseCurlyBlockScope(SmallVectorImpl<AsmRewrite>& AsmStrRewrites); |
| bool parseCppHashLineFilenameComment(SMLoc L); |
| |
| void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body, |
| ArrayRef<MCAsmMacroParameter> Parameters); |
| bool expandMacro(raw_svector_ostream &OS, StringRef Body, |
| ArrayRef<MCAsmMacroParameter> Parameters, |
| ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable, |
| SMLoc L); |
| |
| /// Are macros enabled in the parser? |
| bool areMacrosEnabled() {return MacrosEnabledFlag;} |
| |
| /// Control a flag in the parser that enables or disables macros. |
| void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;} |
| |
| /// Are we inside a macro instantiation? |
| bool isInsideMacroInstantiation() {return !ActiveMacros.empty();} |
| |
| /// Handle entry to macro instantiation. |
| /// |
| /// \param M The macro. |
| /// \param NameLoc Instantiation location. |
| bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc); |
| |
| /// Handle exit from macro instantiation. |
| void handleMacroExit(); |
| |
| /// Extract AsmTokens for a macro argument. |
| bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg); |
| |
| /// Parse all macro arguments for a given macro. |
| bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A); |
| |
| void printMacroInstantiations(); |
| void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg, |
| SMRange Range = None) const { |
| ArrayRef<SMRange> Ranges(Range); |
| SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges); |
| } |
| static void DiagHandler(const SMDiagnostic &Diag, void *Context); |
| |
| /// Should we emit DWARF describing this assembler source? (Returns false if |
| /// the source has .file directives, which means we don't want to generate |
| /// info describing the assembler source itself.) |
| bool enabledGenDwarfForAssembly(); |
| |
| /// Enter the specified file. This returns true on failure. |
| bool enterIncludeFile(const std::string &Filename); |
| |
| /// Process the specified file for the .incbin directive. |
| /// This returns true on failure. |
| bool processIncbinFile(const std::string &Filename, int64_t Skip = 0, |
| const MCExpr *Count = nullptr, SMLoc Loc = SMLoc()); |
| |
| /// Reset the current lexer position to that given by \p Loc. The |
| /// current token is not set; clients should ensure Lex() is called |
| /// subsequently. |
| /// |
| /// \param InBuffer If not 0, should be the known buffer id that contains the |
| /// location. |
| void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0); |
| |
| /// Parse up to the end of statement and a return the contents from the |
| /// current token until the end of the statement; the current token on exit |
| /// will be either the EndOfStatement or EOF. |
| StringRef parseStringToEndOfStatement() override; |
| |
| /// Parse until the end of a statement or a comma is encountered, |
| /// return the contents from the current token up to the end or comma. |
| StringRef parseStringToComma(); |
| |
| bool parseAssignment(StringRef Name, bool allow_redef, |
| bool NoDeadStrip = false); |
| |
| unsigned getBinOpPrecedence(AsmToken::TokenKind K, |
| MCBinaryExpr::Opcode &Kind); |
| |
| bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc); |
| bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc); |
| bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc); |
| |
| bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc); |
| |
| bool parseCVFunctionId(int64_t &FunctionId, StringRef DirectiveName); |
| bool parseCVFileId(int64_t &FileId, StringRef DirectiveName); |
| |
| // Generic (target and platform independent) directive parsing. |
| enum DirectiveKind { |
| DK_NO_DIRECTIVE, // Placeholder |
| DK_SET, |
| DK_EQU, |
| DK_EQUIV, |
| DK_ASCII, |
| DK_ASCIZ, |
| DK_STRING, |
| DK_BYTE, |
| DK_SHORT, |
| DK_RELOC, |
| DK_VALUE, |
| DK_2BYTE, |
| DK_LONG, |
| DK_INT, |
| DK_4BYTE, |
| DK_QUAD, |
| DK_8BYTE, |
| DK_OCTA, |
| DK_DC, |
| DK_DC_A, |
| DK_DC_B, |
| DK_DC_D, |
| DK_DC_L, |
| DK_DC_S, |
| DK_DC_W, |
| DK_DC_X, |
| DK_DCB, |
| DK_DCB_B, |
| DK_DCB_D, |
| DK_DCB_L, |
| DK_DCB_S, |
| DK_DCB_W, |
| DK_DCB_X, |
| DK_DS, |
| DK_DS_B, |
| DK_DS_D, |
| DK_DS_L, |
| DK_DS_P, |
| DK_DS_S, |
| DK_DS_W, |
| DK_DS_X, |
| DK_SINGLE, |
| DK_FLOAT, |
| DK_DOUBLE, |
| DK_ALIGN, |
| DK_ALIGN32, |
| DK_BALIGN, |
| DK_BALIGNW, |
| DK_BALIGNL, |
| DK_P2ALIGN, |
| DK_P2ALIGNW, |
| DK_P2ALIGNL, |
| DK_ORG, |
| DK_FILL, |
| DK_ENDR, |
| DK_BUNDLE_ALIGN_MODE, |
| DK_BUNDLE_LOCK, |
| DK_BUNDLE_UNLOCK, |
| DK_ZERO, |
| DK_EXTERN, |
| DK_GLOBL, |
| DK_GLOBAL, |
| DK_LAZY_REFERENCE, |
| DK_NO_DEAD_STRIP, |
| DK_SYMBOL_RESOLVER, |
| DK_PRIVATE_EXTERN, |
| DK_REFERENCE, |
| DK_WEAK_DEFINITION, |
| DK_WEAK_REFERENCE, |
| DK_WEAK_DEF_CAN_BE_HIDDEN, |
| DK_COMM, |
| DK_COMMON, |
| DK_LCOMM, |
| DK_ABORT, |
| DK_INCLUDE, |
| DK_INCBIN, |
| DK_CODE16, |
| DK_CODE16GCC, |
| DK_REPT, |
| DK_IRP, |
| DK_IRPC, |
| DK_IF, |
| DK_IFEQ, |
| DK_IFGE, |
| DK_IFGT, |
| DK_IFLE, |
| DK_IFLT, |
| DK_IFNE, |
| DK_IFB, |
| DK_IFNB, |
| DK_IFC, |
| DK_IFEQS, |
| DK_IFNC, |
| DK_IFNES, |
| DK_IFDEF, |
| DK_IFNDEF, |
| DK_IFNOTDEF, |
| DK_ELSEIF, |
| DK_ELSE, |
| DK_ENDIF, |
| DK_SPACE, |
| DK_SKIP, |
| DK_FILE, |
| DK_LINE, |
| DK_LOC, |
| DK_STABS, |
| DK_CV_FILE, |
| DK_CV_FUNC_ID, |
| DK_CV_INLINE_SITE_ID, |
| DK_CV_LOC, |
| DK_CV_LINETABLE, |
| DK_CV_INLINE_LINETABLE, |
| DK_CV_DEF_RANGE, |
| DK_CV_STRINGTABLE, |
| DK_CV_FILECHECKSUMS, |
| DK_CV_FILECHECKSUM_OFFSET, |
| DK_CV_FPO_DATA, |
| DK_CFI_SECTIONS, |
| DK_CFI_STARTPROC, |
| DK_CFI_ENDPROC, |
| DK_CFI_DEF_CFA, |
| DK_CFI_DEF_CFA_OFFSET, |
| DK_CFI_ADJUST_CFA_OFFSET, |
| DK_CFI_DEF_CFA_REGISTER, |
| DK_CFI_OFFSET, |
| DK_CFI_REL_OFFSET, |
| DK_CFI_PERSONALITY, |
| DK_CFI_LSDA, |
| DK_CFI_REMEMBER_STATE, |
| DK_CFI_RESTORE_STATE, |
| DK_CFI_SAME_VALUE, |
| DK_CFI_RESTORE, |
| DK_CFI_ESCAPE, |
| DK_CFI_RETURN_COLUMN, |
| DK_CFI_SIGNAL_FRAME, |
| DK_CFI_UNDEFINED, |
| DK_CFI_REGISTER, |
| DK_CFI_WINDOW_SAVE, |
| DK_MACROS_ON, |
| DK_MACROS_OFF, |
| DK_ALTMACRO, |
| DK_NOALTMACRO, |
| DK_MACRO, |
| DK_EXITM, |
| DK_ENDM, |
| DK_ENDMACRO, |
| DK_PURGEM, |
| DK_SLEB128, |
| DK_ULEB128, |
| DK_ERR, |
| DK_ERROR, |
| DK_WARNING, |
| DK_PRINT, |
| DK_ADDRSIG, |
| DK_ADDRSIG_SYM, |
| DK_END |
| }; |
| |
| /// Maps directive name --> DirectiveKind enum, for |
| /// directives parsed by this class. |
| StringMap<DirectiveKind> DirectiveKindMap; |
| |
| // ".ascii", ".asciz", ".string" |
| bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated); |
| bool parseDirectiveReloc(SMLoc DirectiveLoc); // ".reloc" |
| bool parseDirectiveValue(StringRef IDVal, |
| unsigned Size); // ".byte", ".long", ... |
| bool parseDirectiveOctaValue(StringRef IDVal); // ".octa", ... |
| bool parseDirectiveRealValue(StringRef IDVal, |
| const fltSemantics &); // ".single", ... |
| bool parseDirectiveFill(); // ".fill" |
| bool parseDirectiveZero(); // ".zero" |
| // ".set", ".equ", ".equiv" |
| bool parseDirectiveSet(StringRef IDVal, bool allow_redef); |
| bool parseDirectiveOrg(); // ".org" |
| // ".align{,32}", ".p2align{,w,l}" |
| bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize); |
| |
| // ".file", ".line", ".loc", ".stabs" |
| bool parseDirectiveFile(SMLoc DirectiveLoc); |
| bool parseDirectiveLine(); |
| bool parseDirectiveLoc(); |
| bool parseDirectiveStabs(); |
| |
| // ".cv_file", ".cv_func_id", ".cv_inline_site_id", ".cv_loc", ".cv_linetable", |
| // ".cv_inline_linetable", ".cv_def_range" |
| bool parseDirectiveCVFile(); |
| bool parseDirectiveCVFuncId(); |
| bool parseDirectiveCVInlineSiteId(); |
| bool parseDirectiveCVLoc(); |
| bool parseDirectiveCVLinetable(); |
| bool parseDirectiveCVInlineLinetable(); |
| bool parseDirectiveCVDefRange(); |
| bool parseDirectiveCVStringTable(); |
| bool parseDirectiveCVFileChecksums(); |
| bool parseDirectiveCVFileChecksumOffset(); |
| bool parseDirectiveCVFPOData(); |
| |
| // .cfi directives |
| bool parseDirectiveCFIRegister(SMLoc DirectiveLoc); |
| bool parseDirectiveCFIWindowSave(); |
| bool parseDirectiveCFISections(); |
| bool parseDirectiveCFIStartProc(); |
| bool parseDirectiveCFIEndProc(); |
| bool parseDirectiveCFIDefCfaOffset(); |
| bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc); |
| bool parseDirectiveCFIAdjustCfaOffset(); |
| bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc); |
| bool parseDirectiveCFIOffset(SMLoc DirectiveLoc); |
| bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc); |
| bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality); |
| bool parseDirectiveCFIRememberState(); |
| bool parseDirectiveCFIRestoreState(); |
| bool parseDirectiveCFISameValue(SMLoc DirectiveLoc); |
| bool parseDirectiveCFIRestore(SMLoc DirectiveLoc); |
| bool parseDirectiveCFIEscape(); |
| bool parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc); |
| bool parseDirectiveCFISignalFrame(); |
| bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc); |
| |
| // macro directives |
| bool parseDirectivePurgeMacro(SMLoc DirectiveLoc); |
| bool parseDirectiveExitMacro(StringRef Directive); |
| bool parseDirectiveEndMacro(StringRef Directive); |
| bool parseDirectiveMacro(SMLoc DirectiveLoc); |
| bool parseDirectiveMacrosOnOff(StringRef Directive); |
| // alternate macro mode directives |
| bool parseDirectiveAltmacro(StringRef Directive); |
| // ".bundle_align_mode" |
| bool parseDirectiveBundleAlignMode(); |
| // ".bundle_lock" |
| bool parseDirectiveBundleLock(); |
| // ".bundle_unlock" |
| bool parseDirectiveBundleUnlock(); |
| |
| // ".space", ".skip" |
| bool parseDirectiveSpace(StringRef IDVal); |
| |
| // ".dcb" |
| bool parseDirectiveDCB(StringRef IDVal, unsigned Size); |
| bool parseDirectiveRealDCB(StringRef IDVal, const fltSemantics &); |
| // ".ds" |
| bool parseDirectiveDS(StringRef IDVal, unsigned Size); |
| |
| // .sleb128 (Signed=true) and .uleb128 (Signed=false) |
| bool parseDirectiveLEB128(bool Signed); |
| |
| /// Parse a directive like ".globl" which |
| /// accepts a single symbol (which should be a label or an external). |
| bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr); |
| |
| bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm" |
| |
| bool parseDirectiveAbort(); // ".abort" |
| bool parseDirectiveInclude(); // ".include" |
| bool parseDirectiveIncbin(); // ".incbin" |
| |
| // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne" |
| bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind); |
| // ".ifb" or ".ifnb", depending on ExpectBlank. |
| bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank); |
| // ".ifc" or ".ifnc", depending on ExpectEqual. |
| bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual); |
| // ".ifeqs" or ".ifnes", depending on ExpectEqual. |
| bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual); |
| // ".ifdef" or ".ifndef", depending on expect_defined |
| bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined); |
| bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif" |
| bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else" |
| bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif |
| bool parseEscapedString(std::string &Data) override; |
| |
| const MCExpr *applyModifierToExpr(const MCExpr *E, |
| MCSymbolRefExpr::VariantKind Variant); |
| |
| // Macro-like directives |
| MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc); |
| void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, |
| raw_svector_ostream &OS); |
| bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive); |
| bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp" |
| bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc" |
| bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr" |
| |
| // "_emit" or "__emit" |
| bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info, |
| size_t Len); |
| |
| // "align" |
| bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info); |
| |
| // "end" |
| bool parseDirectiveEnd(SMLoc DirectiveLoc); |
| |
| // ".err" or ".error" |
| bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage); |
| |
| // ".warning" |
| bool parseDirectiveWarning(SMLoc DirectiveLoc); |
| |
| // .print <double-quotes-string> |
| bool parseDirectivePrint(SMLoc DirectiveLoc); |
| |
| // Directives to support address-significance tables. |
| bool parseDirectiveAddrsig(); |
| bool parseDirectiveAddrsigSym(); |
| |
| void initializeDirectiveKindMap(); |
| }; |
| |
| } // end anonymous namespace |
| |
| namespace llvm { |
| |
| extern MCAsmParserExtension *createDarwinAsmParser(); |
| extern MCAsmParserExtension *createELFAsmParser(); |
| extern MCAsmParserExtension *createCOFFAsmParser(); |
| |
| } // end namespace llvm |
| |
| enum { DEFAULT_ADDRSPACE = 0 }; |
| |
| AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out, |
| const MCAsmInfo &MAI, unsigned CB = 0) |
| : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM), |
| CurBuffer(CB ? CB : SM.getMainFileID()), MacrosEnabledFlag(true) { |
| HadError = false; |
| // Save the old handler. |
| SavedDiagHandler = SrcMgr.getDiagHandler(); |
| SavedDiagContext = SrcMgr.getDiagContext(); |
| // Set our own handler which calls the saved handler. |
| SrcMgr.setDiagHandler(DiagHandler, this); |
| Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
| |
| // Initialize the platform / file format parser. |
| switch (Ctx.getObjectFileInfo()->getObjectFileType()) { |
| case MCObjectFileInfo::IsCOFF: |
| PlatformParser.reset(createCOFFAsmParser()); |
| break; |
| case MCObjectFileInfo::IsMachO: |
| PlatformParser.reset(createDarwinAsmParser()); |
| IsDarwin = true; |
| break; |
| case MCObjectFileInfo::IsELF: |
| PlatformParser.reset(createELFAsmParser()); |
| break; |
| case MCObjectFileInfo::IsWasm: |
| // TODO: WASM will need its own MCAsmParserExtension implementation, but |
| // for now we can re-use the ELF one, since the directives can be the |
| // same for now. |
| PlatformParser.reset(createELFAsmParser()); |
| break; |
| } |
| |
| PlatformParser->Initialize(*this); |
| initializeDirectiveKindMap(); |
| |
| NumOfMacroInstantiations = 0; |
| } |
| |
| AsmParser::~AsmParser() { |
| assert((HadError || ActiveMacros.empty()) && |
| "Unexpected active macro instantiation!"); |
| |
| // Restore the saved diagnostics handler and context for use during |
| // finalization. |
| SrcMgr.setDiagHandler(SavedDiagHandler, SavedDiagContext); |
| } |
| |
| void AsmParser::printMacroInstantiations() { |
| // Print the active macro instantiation stack. |
| for (std::vector<MacroInstantiation *>::const_reverse_iterator |
| it = ActiveMacros.rbegin(), |
| ie = ActiveMacros.rend(); |
| it != ie; ++it) |
| printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note, |
| "while in macro instantiation"); |
| } |
| |
| void AsmParser::Note(SMLoc L, const Twine &Msg, SMRange Range) { |
| printPendingErrors(); |
| printMessage(L, SourceMgr::DK_Note, Msg, Range); |
| printMacroInstantiations(); |
| } |
| |
| bool AsmParser::Warning(SMLoc L, const Twine &Msg, SMRange Range) { |
| if(getTargetParser().getTargetOptions().MCNoWarn) |
| return false; |
| if (getTargetParser().getTargetOptions().MCFatalWarnings) |
| return Error(L, Msg, Range); |
| printMessage(L, SourceMgr::DK_Warning, Msg, Range); |
| printMacroInstantiations(); |
| return false; |
| } |
| |
| bool AsmParser::printError(SMLoc L, const Twine &Msg, SMRange Range) { |
| HadError = true; |
| printMessage(L, SourceMgr::DK_Error, Msg, Range); |
| printMacroInstantiations(); |
| return true; |
| } |
| |
| bool AsmParser::enterIncludeFile(const std::string &Filename) { |
| std::string IncludedFile; |
| unsigned NewBuf = |
| SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile); |
| if (!NewBuf) |
| return true; |
| |
| CurBuffer = NewBuf; |
| Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
| return false; |
| } |
| |
| /// Process the specified .incbin file by searching for it in the include paths |
| /// then just emitting the byte contents of the file to the streamer. This |
| /// returns true on failure. |
| bool AsmParser::processIncbinFile(const std::string &Filename, int64_t Skip, |
| const MCExpr *Count, SMLoc Loc) { |
| std::string IncludedFile; |
| unsigned NewBuf = |
| SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile); |
| if (!NewBuf) |
| return true; |
| |
| // Pick up the bytes from the file and emit them. |
| StringRef Bytes = SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(); |
| Bytes = Bytes.drop_front(Skip); |
| if (Count) { |
| int64_t Res; |
| if (!Count->evaluateAsAbsolute(Res, getStreamer().getAssemblerPtr())) |
| return Error(Loc, "expected absolute expression"); |
| if (Res < 0) |
| return Warning(Loc, "negative count has no effect"); |
| Bytes = Bytes.take_front(Res); |
| } |
| getStreamer().EmitBytes(Bytes); |
| return false; |
| } |
| |
| void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) { |
| CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc); |
| Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(), |
| Loc.getPointer()); |
| } |
| |
| const AsmToken &AsmParser::Lex() { |
| if (Lexer.getTok().is(AsmToken::Error)) |
| Error(Lexer.getErrLoc(), Lexer.getErr()); |
| |
| // if it's a end of statement with a comment in it |
| if (getTok().is(AsmToken::EndOfStatement)) { |
| // if this is a line comment output it. |
| if (!getTok().getString().empty() && getTok().getString().front() != '\n' && |
| getTok().getString().front() != '\r' && MAI.preserveAsmComments()) |
| Out.addExplicitComment(Twine(getTok().getString())); |
| } |
| |
| const AsmToken *tok = &Lexer.Lex(); |
| |
| // Parse comments here to be deferred until end of next statement. |
| while (tok->is(AsmToken::Comment)) { |
| if (MAI.preserveAsmComments()) |
| Out.addExplicitComment(Twine(tok->getString())); |
| tok = &Lexer.Lex(); |
| } |
| |
| if (tok->is(AsmToken::Eof)) { |
| // If this is the end of an included file, pop the parent file off the |
| // include stack. |
| SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer); |
| if (ParentIncludeLoc != SMLoc()) { |
| jumpToLoc(ParentIncludeLoc); |
| return Lex(); |
| } |
| } |
| |
| return *tok; |
| } |
| |
| bool AsmParser::enabledGenDwarfForAssembly() { |
| // Check whether the user specified -g. |
| if (!getContext().getGenDwarfForAssembly()) |
| return false; |
| // If we haven't encountered any .file directives (which would imply that |
| // the assembler source was produced with debug info already) then emit one |
| // describing the assembler source file itself. |
| if (getContext().getGenDwarfFileNumber() == 0) |
| getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective( |
| 0, StringRef(), getContext().getMainFileName())); |
| return true; |
| } |
| |
| bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) { |
| // Create the initial section, if requested. |
| if (!NoInitialTextSection) |
| Out.InitSections(false); |
| |
| // Prime the lexer. |
| Lex(); |
| |
| HadError = false; |
| AsmCond StartingCondState = TheCondState; |
| SmallVector<AsmRewrite, 4> AsmStrRewrites; |
| |
| // If we are generating dwarf for assembly source files save the initial text |
| // section. (Don't use enabledGenDwarfForAssembly() here, as we aren't |
| // emitting any actual debug info yet and haven't had a chance to parse any |
| // embedded .file directives.) |
| if (getContext().getGenDwarfForAssembly()) { |
| MCSection *Sec = getStreamer().getCurrentSectionOnly(); |
| if (!Sec->getBeginSymbol()) { |
| MCSymbol *SectionStartSym = getContext().createTempSymbol(); |
| getStreamer().EmitLabel(SectionStartSym); |
| Sec->setBeginSymbol(SectionStartSym); |
| } |
| bool InsertResult = getContext().addGenDwarfSection(Sec); |
| assert(InsertResult && ".text section should not have debug info yet"); |
| (void)InsertResult; |
| } |
| |
| // While we have input, parse each statement. |
| while (Lexer.isNot(AsmToken::Eof)) { |
| ParseStatementInfo Info(&AsmStrRewrites); |
| if (!parseStatement(Info, nullptr)) |
| continue; |
| |
| // If we have a Lexer Error we are on an Error Token. Load in Lexer Error |
| // for printing ErrMsg via Lex() only if no (presumably better) parser error |
| // exists. |
| if (!hasPendingError() && Lexer.getTok().is(AsmToken::Error)) { |
| Lex(); |
| } |
| |
| // parseStatement returned true so may need to emit an error. |
| printPendingErrors(); |
| |
| // Skipping to the next line if needed. |
| if (!getLexer().isAtStartOfStatement()) |
| eatToEndOfStatement(); |
| } |
| |
| // All errors should have been emitted. |
| assert(!hasPendingError() && "unexpected error from parseStatement"); |
| |
| getTargetParser().flushPendingInstructions(getStreamer()); |
| |
| if (TheCondState.TheCond != StartingCondState.TheCond || |
| TheCondState.Ignore != StartingCondState.Ignore) |
| printError(getTok().getLoc(), "unmatched .ifs or .elses"); |
| // Check to see there are no empty DwarfFile slots. |
| const auto &LineTables = getContext().getMCDwarfLineTables(); |
| if (!LineTables.empty()) { |
| unsigned Index = 0; |
| for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) { |
| if (File.Name.empty() && Index != 0) |
| printError(getTok().getLoc(), "unassigned file number: " + |
| Twine(Index) + |
| " for .file directives"); |
| ++Index; |
| } |
| } |
| |
| // Check to see that all assembler local symbols were actually defined. |
| // Targets that don't do subsections via symbols may not want this, though, |
| // so conservatively exclude them. Only do this if we're finalizing, though, |
| // as otherwise we won't necessarilly have seen everything yet. |
| if (!NoFinalize) { |
| if (MAI.hasSubsectionsViaSymbols()) { |
| for (const auto &TableEntry : getContext().getSymbols()) { |
| MCSymbol *Sym = TableEntry.getValue(); |
| // Variable symbols may not be marked as defined, so check those |
| // explicitly. If we know it's a variable, we have a definition for |
| // the purposes of this check. |
| if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined()) |
| // FIXME: We would really like to refer back to where the symbol was |
| // first referenced for a source location. We need to add something |
| // to track that. Currently, we just point to the end of the file. |
| printError(getTok().getLoc(), "assembler local symbol '" + |
| Sym->getName() + "' not defined"); |
| } |
| } |
| |
| // Temporary symbols like the ones for directional jumps don't go in the |
| // symbol table. They also need to be diagnosed in all (final) cases. |
| for (std::tuple<SMLoc, CppHashInfoTy, MCSymbol *> &LocSym : DirLabels) { |
| if (std::get<2>(LocSym)->isUndefined()) { |
| // Reset the state of any "# line file" directives we've seen to the |
| // context as it was at the diagnostic site. |
| CppHashInfo = std::get<1>(LocSym); |
| printError(std::get<0>(LocSym), "directional label undefined"); |
| } |
| } |
| } |
| |
| // Finalize the output stream if there are no errors and if the client wants |
| // us to. |
| if (!HadError && !NoFinalize) |
| Out.Finish(); |
| |
| return HadError || getContext().hadError(); |
| } |
| |
| bool AsmParser::checkForValidSection() { |
| if (!ParsingInlineAsm && !getStreamer().getCurrentSectionOnly()) { |
| Out.InitSections(false); |
| return Error(getTok().getLoc(), |
| "expected section directive before assembly directive"); |
| } |
| return false; |
| } |
| |
| /// Throw away the rest of the line for testing purposes. |
| void AsmParser::eatToEndOfStatement() { |
| while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof)) |
| Lexer.Lex(); |
| |
| // Eat EOL. |
| if (Lexer.is(AsmToken::EndOfStatement)) |
| Lexer.Lex(); |
| } |
| |
| StringRef AsmParser::parseStringToEndOfStatement() { |
| const char *Start = getTok().getLoc().getPointer(); |
| |
| while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof)) |
| Lexer.Lex(); |
| |
| const char *End = getTok().getLoc().getPointer(); |
| return StringRef(Start, End - Start); |
| } |
| |
| StringRef AsmParser::parseStringToComma() { |
| const char *Start = getTok().getLoc().getPointer(); |
| |
| while (Lexer.isNot(AsmToken::EndOfStatement) && |
| Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof)) |
| Lexer.Lex(); |
| |
| const char *End = getTok().getLoc().getPointer(); |
| return StringRef(Start, End - Start); |
| } |
| |
| /// Parse a paren expression and return it. |
| /// NOTE: This assumes the leading '(' has already been consumed. |
| /// |
| /// parenexpr ::= expr) |
| /// |
| bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
| if (parseExpression(Res)) |
| return true; |
| if (Lexer.isNot(AsmToken::RParen)) |
| return TokError("expected ')' in parentheses expression"); |
| EndLoc = Lexer.getTok().getEndLoc(); |
| Lex(); |
| return false; |
| } |
| |
| /// Parse a bracket expression and return it. |
| /// NOTE: This assumes the leading '[' has already been consumed. |
| /// |
| /// bracketexpr ::= expr] |
| /// |
| bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
| if (parseExpression(Res)) |
| return true; |
| EndLoc = getTok().getEndLoc(); |
| if (parseToken(AsmToken::RBrac, "expected ']' in brackets expression")) |
| return true; |
| return false; |
| } |
| |
| /// Parse a primary expression and return it. |
| /// primaryexpr ::= (parenexpr |
| /// primaryexpr ::= symbol |
| /// primaryexpr ::= number |
| /// primaryexpr ::= '.' |
| /// primaryexpr ::= ~,+,- primaryexpr |
| bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
| SMLoc FirstTokenLoc = getLexer().getLoc(); |
| AsmToken::TokenKind FirstTokenKind = Lexer.getKind(); |
| switch (FirstTokenKind) { |
| default: |
| return TokError("unknown token in expression"); |
| // If we have an error assume that we've already handled it. |
| case AsmToken::Error: |
| return true; |
| case AsmToken::Exclaim: |
| Lex(); // Eat the operator. |
| if (parsePrimaryExpr(Res, EndLoc)) |
| return true; |
| Res = MCUnaryExpr::createLNot(Res, getContext(), FirstTokenLoc); |
| return false; |
| case AsmToken::Dollar: |
| case AsmToken::At: |
| case AsmToken::String: |
| case AsmToken::Identifier: { |
| StringRef Identifier; |
| if (parseIdentifier(Identifier)) { |
| // We may have failed but $ may be a valid token. |
| if (getTok().is(AsmToken::Dollar)) { |
| if (Lexer.getMAI().getDollarIsPC()) { |
| Lex(); |
| // This is a '$' reference, which references the current PC. Emit a |
| // temporary label to the streamer and refer to it. |
| MCSymbol *Sym = Ctx.createTempSymbol(); |
| Out.EmitLabel(Sym); |
| Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, |
| getContext()); |
| EndLoc = FirstTokenLoc; |
| return false; |
| } |
| return Error(FirstTokenLoc, "invalid token in expression"); |
| } |
| } |
| // Parse symbol variant |
| std::pair<StringRef, StringRef> Split; |
| if (!MAI.useParensForSymbolVariant()) { |
| if (FirstTokenKind == AsmToken::String) { |
| if (Lexer.is(AsmToken::At)) { |
| Lex(); // eat @ |
| SMLoc AtLoc = getLexer().getLoc(); |
| StringRef VName; |
| if (parseIdentifier(VName)) |
| return Error(AtLoc, "expected symbol variant after '@'"); |
| |
| Split = std::make_pair(Identifier, VName); |
| } |
| } else { |
| Split = Identifier.split('@'); |
| } |
| } else if (Lexer.is(AsmToken::LParen)) { |
| Lex(); // eat '('. |
| StringRef VName; |
| parseIdentifier(VName); |
| // eat ')'. |
| if (parseToken(AsmToken::RParen, |
| "unexpected token in variant, expected ')'")) |
| return true; |
| Split = std::make_pair(Identifier, VName); |
| } |
| |
| EndLoc = SMLoc::getFromPointer(Identifier.end()); |
| |
| // This is a symbol reference. |
| StringRef SymbolName = Identifier; |
| if (SymbolName.empty()) |
| return true; |
| |
| MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| |
| // Lookup the symbol variant if used. |
| if (!Split.second.empty()) { |
| Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); |
| if (Variant != MCSymbolRefExpr::VK_Invalid) { |
| SymbolName = Split.first; |
| } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) { |
| Variant = MCSymbolRefExpr::VK_None; |
| } else { |
| return Error(SMLoc::getFromPointer(Split.second.begin()), |
| "invalid variant '" + Split.second + "'"); |
| } |
| } |
| |
| MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName); |
| |
| // If this is an absolute variable reference, substitute it now to preserve |
| // semantics in the face of reassignment. |
| if (Sym->isVariable()) { |
| auto V = Sym->getVariableValue(/*SetUsed*/ false); |
| bool DoInline = isa<MCConstantExpr>(V); |
| if (auto TV = dyn_cast<MCTargetExpr>(V)) |
| DoInline = TV->inlineAssignedExpr(); |
| if (DoInline) { |
| if (Variant) |
| return Error(EndLoc, "unexpected modifier on variable reference"); |
| Res = Sym->getVariableValue(/*SetUsed*/ false); |
| return false; |
| } |
| } |
| |
| // Otherwise create a symbol ref. |
| Res = MCSymbolRefExpr::create(Sym, Variant, getContext(), FirstTokenLoc); |
| return false; |
| } |
| case AsmToken::BigNum: |
| return TokError("literal value out of range for directive"); |
| case AsmToken::Integer: { |
| SMLoc Loc = getTok().getLoc(); |
| int64_t IntVal = getTok().getIntVal(); |
| Res = MCConstantExpr::create(IntVal, getContext()); |
| EndLoc = Lexer.getTok().getEndLoc(); |
| Lex(); // Eat token. |
| // Look for 'b' or 'f' following an Integer as a directional label |
| if (Lexer.getKind() == AsmToken::Identifier) { |
| StringRef IDVal = getTok().getString(); |
| // Lookup the symbol variant if used. |
| std::pair<StringRef, StringRef> Split = IDVal.split('@'); |
| MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| if (Split.first.size() != IDVal.size()) { |
| Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); |
| if (Variant == MCSymbolRefExpr::VK_Invalid) |
| return TokError("invalid variant '" + Split.second + "'"); |
| IDVal = Split.first; |
| } |
| if (IDVal == "f" || IDVal == "b") { |
| MCSymbol *Sym = |
| Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b"); |
| Res = MCSymbolRefExpr::create(Sym, Variant, getContext()); |
| if (IDVal == "b" && Sym->isUndefined()) |
| return Error(Loc, "directional label undefined"); |
| DirLabels.push_back(std::make_tuple(Loc, CppHashInfo, Sym)); |
| EndLoc = Lexer.getTok().getEndLoc(); |
| Lex(); // Eat identifier. |
| } |
| } |
| return false; |
| } |
| case AsmToken::Real: { |
| APFloat RealVal(APFloat::IEEEdouble(), getTok().getString()); |
| uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue(); |
| Res = MCConstantExpr::create(IntVal, getContext()); |
| EndLoc = Lexer.getTok().getEndLoc(); |
| Lex(); // Eat token. |
| return false; |
| } |
| case AsmToken::Dot: { |
| // This is a '.' reference, which references the current PC. Emit a |
| // temporary label to the streamer and refer to it. |
| MCSymbol *Sym = Ctx.createTempSymbol(); |
| Out.EmitLabel(Sym); |
| Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext()); |
| EndLoc = Lexer.getTok().getEndLoc(); |
| Lex(); // Eat identifier. |
| return false; |
| } |
| case AsmToken::LParen: |
| Lex(); // Eat the '('. |
| return parseParenExpr(Res, EndLoc); |
| case AsmToken::LBrac: |
| if (!PlatformParser->HasBracketExpressions()) |
| return TokError("brackets expression not supported on this target"); |
| Lex(); // Eat the '['. |
| return parseBracketExpr(Res, EndLoc); |
| case AsmToken::Minus: |
| Lex(); // Eat the operator. |
| if (parsePrimaryExpr(Res, EndLoc)) |
| return true; |
| Res = MCUnaryExpr::createMinus(Res, getContext(), FirstTokenLoc); |
| return false; |
| case AsmToken::Plus: |
| Lex(); // Eat the operator. |
| if (parsePrimaryExpr(Res, EndLoc)) |
| return true; |
| Res = MCUnaryExpr::createPlus(Res, getContext(), FirstTokenLoc); |
| return false; |
| case AsmToken::Tilde: |
| Lex(); // Eat the operator. |
| if (parsePrimaryExpr(Res, EndLoc)) |
| return true; |
| Res = MCUnaryExpr::createNot(Res, getContext(), FirstTokenLoc); |
| return false; |
| // MIPS unary expression operators. The lexer won't generate these tokens if |
| // MCAsmInfo::HasMipsExpressions is false for the target. |
| case AsmToken::PercentCall16: |
| case AsmToken::PercentCall_Hi: |
| case AsmToken::PercentCall_Lo: |
| case AsmToken::PercentDtprel_Hi: |
| case AsmToken::PercentDtprel_Lo: |
| case AsmToken::PercentGot: |
| case AsmToken::PercentGot_Disp: |
| case AsmToken::PercentGot_Hi: |
| case AsmToken::PercentGot_Lo: |
| case AsmToken::PercentGot_Ofst: |
| case AsmToken::PercentGot_Page: |
| case AsmToken::PercentGottprel: |
| case AsmToken::PercentGp_Rel: |
| case AsmToken::PercentHi: |
| case AsmToken::PercentHigher: |
| case AsmToken::PercentHighest: |
| case AsmToken::PercentLo: |
| case AsmToken::PercentNeg: |
| case AsmToken::PercentPcrel_Hi: |
| case AsmToken::PercentPcrel_Lo: |
| case AsmToken::PercentTlsgd: |
| case AsmToken::PercentTlsldm: |
| case AsmToken::PercentTprel_Hi: |
| case AsmToken::PercentTprel_Lo: |
| Lex(); // Eat the operator. |
| if (Lexer.isNot(AsmToken::LParen)) |
| return TokError("expected '(' after operator"); |
| Lex(); // Eat the operator. |
| if (parseExpression(Res, EndLoc)) |
| return true; |
| if (Lexer.isNot(AsmToken::RParen)) |
| return TokError("expected ')'"); |
| Lex(); // Eat the operator. |
| Res = getTargetParser().createTargetUnaryExpr(Res, FirstTokenKind, Ctx); |
| return !Res; |
| } |
| } |
| |
| bool AsmParser::parseExpression(const MCExpr *&Res) { |
| SMLoc EndLoc; |
| return parseExpression(Res, EndLoc); |
| } |
| |
| const MCExpr * |
| AsmParser::applyModifierToExpr(const MCExpr *E, |
| MCSymbolRefExpr::VariantKind Variant) { |
| // Ask the target implementation about this expression first. |
| const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx); |
| if (NewE) |
| return NewE; |
| // Recurse over the given expression, rebuilding it to apply the given variant |
| // if there is exactly one symbol. |
| switch (E->getKind()) { |
| case MCExpr::Target: |
| case MCExpr::Constant: |
| return nullptr; |
| |
| case MCExpr::SymbolRef: { |
| const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E); |
| |
| if (SRE->getKind() != MCSymbolRefExpr::VK_None) { |
| TokError("invalid variant on expression '" + getTok().getIdentifier() + |
| "' (already modified)"); |
| return E; |
| } |
| |
| return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, getContext()); |
| } |
| |
| case MCExpr::Unary: { |
| const MCUnaryExpr *UE = cast<MCUnaryExpr>(E); |
| const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant); |
| if (!Sub) |
| return nullptr; |
| return MCUnaryExpr::create(UE->getOpcode(), Sub, getContext()); |
| } |
| |
| case MCExpr::Binary: { |
| const MCBinaryExpr *BE = cast<MCBinaryExpr>(E); |
| const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant); |
| const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant); |
| |
| if (!LHS && !RHS) |
| return nullptr; |
| |
| if (!LHS) |
| LHS = BE->getLHS(); |
| if (!RHS) |
| RHS = BE->getRHS(); |
| |
| return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, getContext()); |
| } |
| } |
| |
| llvm_unreachable("Invalid expression kind!"); |
| } |
| |
| /// This function checks if the next token is <string> type or arithmetic. |
| /// string that begin with character '<' must end with character '>'. |
| /// otherwise it is arithmetics. |
| /// If the function returns a 'true' value, |
| /// the End argument will be filled with the last location pointed to the '>' |
| /// character. |
| |
| /// There is a gap between the AltMacro's documentation and the single quote implementation. |
| /// GCC does not fully support this feature and so we will not support it. |
| /// TODO: Adding single quote as a string. |
| bool AsmParser::isAltmacroString(SMLoc &StrLoc, SMLoc &EndLoc) { |
| assert((StrLoc.getPointer() != NULL) && |
| "Argument to the function cannot be a NULL value"); |
| const char *CharPtr = StrLoc.getPointer(); |
| while ((*CharPtr != '>') && (*CharPtr != '\n') && (*CharPtr != '\r') && |
| (*CharPtr != '\0')) { |
| if (*CharPtr == '!') |
| CharPtr++; |
| CharPtr++; |
| } |
| if (*CharPtr == '>') { |
| EndLoc = StrLoc.getFromPointer(CharPtr + 1); |
| return true; |
| } |
| return false; |
| } |
| |
| /// creating a string without the escape characters '!'. |
| void AsmParser::altMacroString(StringRef AltMacroStr,std::string &Res) { |
| for (size_t Pos = 0; Pos < AltMacroStr.size(); Pos++) { |
| if (AltMacroStr[Pos] == '!') |
| Pos++; |
| Res += AltMacroStr[Pos]; |
| } |
| } |
| |
| /// Parse an expression and return it. |
| /// |
| /// expr ::= expr &&,|| expr -> lowest. |
| /// expr ::= expr |,^,&,! expr |
| /// expr ::= expr ==,!=,<>,<,<=,>,>= expr |
| /// expr ::= expr <<,>> expr |
| /// expr ::= expr +,- expr |
| /// expr ::= expr *,/,% expr -> highest. |
| /// expr ::= primaryexpr |
| /// |
| bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
| // Parse the expression. |
| Res = nullptr; |
| if (getTargetParser().parsePrimaryExpr(Res, EndLoc) || |
| parseBinOpRHS(1, Res, EndLoc)) |
| return true; |
| |
| // As a special case, we support 'a op b @ modifier' by rewriting the |
| // expression to include the modifier. This is inefficient, but in general we |
| // expect users to use 'a@modifier op b'. |
| if (Lexer.getKind() == AsmToken::At) { |
| Lex(); |
| |
| if (Lexer.isNot(AsmToken::Identifier)) |
| return TokError("unexpected symbol modifier following '@'"); |
| |
| MCSymbolRefExpr::VariantKind Variant = |
| MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier()); |
| if (Variant == MCSymbolRefExpr::VK_Invalid) |
| return TokError("invalid variant '" + getTok().getIdentifier() + "'"); |
| |
| const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant); |
| if (!ModifiedRes) { |
| return TokError("invalid modifier '" + getTok().getIdentifier() + |
| "' (no symbols present)"); |
| } |
| |
| Res = ModifiedRes; |
| Lex(); |
| } |
| |
| // Try to constant fold it up front, if possible. Do not exploit |
| // assembler here. |
| int64_t Value; |
| if (Res->evaluateAsAbsolute(Value)) |
| Res = MCConstantExpr::create(Value, getContext()); |
| |
| return false; |
| } |
| |
| bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
| Res = nullptr; |
| return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc); |
| } |
| |
| bool AsmParser::parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res, |
| SMLoc &EndLoc) { |
| if (parseParenExpr(Res, EndLoc)) |
| return true; |
| |
| for (; ParenDepth > 0; --ParenDepth) { |
| if (parseBinOpRHS(1, Res, EndLoc)) |
| return true; |
| |
| // We don't Lex() the last RParen. |
| // This is the same behavior as parseParenExpression(). |
| if (ParenDepth - 1 > 0) { |
| EndLoc = getTok().getEndLoc(); |
| if (parseToken(AsmToken::RParen, |
| "expected ')' in parentheses expression")) |
| return true; |
| } |
| } |
| return false; |
| } |
| |
| bool AsmParser::parseAbsoluteExpression(int64_t &Res) { |
| const MCExpr *Expr; |
| |
| SMLoc StartLoc = Lexer.getLoc(); |
| if (parseExpression(Expr)) |
| return true; |
| |
| if (!Expr->evaluateAsAbsolute(Res, getStreamer().getAssemblerPtr())) |
| return Error(StartLoc, "expected absolute expression"); |
| |
| return false; |
| } |
| |
| static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K, |
| MCBinaryExpr::Opcode &Kind, |
| bool ShouldUseLogicalShr) { |
| switch (K) { |
| default: |
| return 0; // not a binop. |
| |
| // Lowest Precedence: &&, || |
| case AsmToken::AmpAmp: |
| Kind = MCBinaryExpr::LAnd; |
| return 1; |
| case AsmToken::PipePipe: |
| Kind = MCBinaryExpr::LOr; |
| return 1; |
| |
| // Low Precedence: |, &, ^ |
| // |
| // FIXME: gas seems to support '!' as an infix operator? |
| case AsmToken::Pipe: |
| Kind = MCBinaryExpr::Or; |
| return 2; |
| case AsmToken::Caret: |
| Kind = MCBinaryExpr::Xor; |
| return 2; |
| case AsmToken::Amp: |
| Kind = MCBinaryExpr::And; |
| return 2; |
| |
| // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >= |
| case AsmToken::EqualEqual: |
| Kind = MCBinaryExpr::EQ; |
| return 3; |
| case AsmToken::ExclaimEqual: |
| case AsmToken::LessGreater: |
| Kind = MCBinaryExpr::NE; |
| return 3; |
| case AsmToken::Less: |
| Kind = MCBinaryExpr::LT; |
| return 3; |
| case AsmToken::LessEqual: |
| Kind = MCBinaryExpr::LTE; |
| return 3; |
| case AsmToken::Greater: |
| Kind = MCBinaryExpr::GT; |
| return 3; |
| case AsmToken::GreaterEqual: |
| Kind = MCBinaryExpr::GTE; |
| return 3; |
| |
| // Intermediate Precedence: <<, >> |
| case AsmToken::LessLess: |
| Kind = MCBinaryExpr::Shl; |
| return 4; |
| case AsmToken::GreaterGreater: |
| Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr; |
| return 4; |
| |
| // High Intermediate Precedence: +, - |
| case AsmToken::Plus: |
| Kind = MCBinaryExpr::Add; |
| return 5; |
| case AsmToken::Minus: |
| Kind = MCBinaryExpr::Sub; |
| return 5; |
| |
| // Highest Precedence: *, /, % |
| case AsmToken::Star: |
| Kind = MCBinaryExpr::Mul; |
| return 6; |
| case AsmToken::Slash: |
| Kind = MCBinaryExpr::Div; |
| return 6; |
| case AsmToken::Percent: |
| Kind = MCBinaryExpr::Mod; |
| return 6; |
| } |
| } |
| |
| static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K, |
| MCBinaryExpr::Opcode &Kind, |
| bool ShouldUseLogicalShr) { |
| switch (K) { |
| default: |
| return 0; // not a binop. |
| |
| // Lowest Precedence: &&, || |
| case AsmToken::AmpAmp: |
| Kind = MCBinaryExpr::LAnd; |
| return 2; |
| case AsmToken::PipePipe: |
| Kind = MCBinaryExpr::LOr; |
| return 1; |
| |
| // Low Precedence: ==, !=, <>, <, <=, >, >= |
| case AsmToken::EqualEqual: |
| Kind = MCBinaryExpr::EQ; |
| return 3; |
| case AsmToken::ExclaimEqual: |
| case AsmToken::LessGreater: |
| Kind = MCBinaryExpr::NE; |
| return 3; |
| case AsmToken::Less: |
| Kind = MCBinaryExpr::LT; |
| return 3; |
| case AsmToken::LessEqual: |
| Kind = MCBinaryExpr::LTE; |
| return 3; |
| case AsmToken::Greater: |
| Kind = MCBinaryExpr::GT; |
| return 3; |
| case AsmToken::GreaterEqual: |
| Kind = MCBinaryExpr::GTE; |
| return 3; |
| |
| // Low Intermediate Precedence: +, - |
| case AsmToken::Plus: |
| Kind = MCBinaryExpr::Add; |
| return 4; |
| case AsmToken::Minus: |
| Kind = MCBinaryExpr::Sub; |
| return 4; |
| |
| // High Intermediate Precedence: |, &, ^ |
| // |
| // FIXME: gas seems to support '!' as an infix operator? |
| case AsmToken::Pipe: |
| Kind = MCBinaryExpr::Or; |
| return 5; |
| case AsmToken::Caret: |
| Kind = MCBinaryExpr::Xor; |
| return 5; |
| case AsmToken::Amp: |
| Kind = MCBinaryExpr::And; |
| return 5; |
| |
| // Highest Precedence: *, /, %, <<, >> |
| case AsmToken::Star: |
| Kind = MCBinaryExpr::Mul; |
| return 6; |
| case AsmToken::Slash: |
| Kind = MCBinaryExpr::Div; |
| return 6; |
| case AsmToken::Percent: |
| Kind = MCBinaryExpr::Mod; |
| return 6; |
| case AsmToken::LessLess: |
| Kind = MCBinaryExpr::Shl; |
| return 6; |
| case AsmToken::GreaterGreater: |
| Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr; |
| return 6; |
| } |
| } |
| |
| unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K, |
| MCBinaryExpr::Opcode &Kind) { |
| bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr(); |
| return IsDarwin ? getDarwinBinOpPrecedence(K, Kind, ShouldUseLogicalShr) |
| : getGNUBinOpPrecedence(K, Kind, ShouldUseLogicalShr); |
| } |
| |
| /// Parse all binary operators with precedence >= 'Precedence'. |
| /// Res contains the LHS of the expression on input. |
| bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, |
| SMLoc &EndLoc) { |
| SMLoc StartLoc = Lexer.getLoc(); |
| while (true) { |
| MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add; |
| unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind); |
| |
| // If the next token is lower precedence than we are allowed to eat, return |
| // successfully with what we ate already. |
| if (TokPrec < Precedence) |
| return false; |
| |
| Lex(); |
| |
| // Eat the next primary expression. |
| const MCExpr *RHS; |
| if (getTargetParser().parsePrimaryExpr(RHS, EndLoc)) |
| return true; |
| |
| // If BinOp binds less tightly with RHS than the operator after RHS, let |
| // the pending operator take RHS as its LHS. |
| MCBinaryExpr::Opcode Dummy; |
| unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy); |
| if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc)) |
| return true; |
| |
| // Merge LHS and RHS according to operator. |
| Res = MCBinaryExpr::create(Kind, Res, RHS, getContext(), StartLoc); |
| } |
| } |
| |
| /// ParseStatement: |
| /// ::= EndOfStatement |
| /// ::= Label* Directive ...Operands... EndOfStatement |
| /// ::= Label* Identifier OperandList* EndOfStatement |
| bool AsmParser::parseStatement(ParseStatementInfo &Info, |
| MCAsmParserSemaCallback *SI) { |
| assert(!hasPendingError() && "parseStatement started with pending error"); |
| // Eat initial spaces and comments |
| while (Lexer.is(AsmToken::Space)) |
| Lex(); |
| if (Lexer.is(AsmToken::EndOfStatement)) { |
| // if this is a line comment we can drop it safely |
| if (getTok().getString().empty() || getTok().getString().front() == '\r' || |
| getTok().getString().front() == '\n') |
| Out.AddBlankLine(); |
| Lex(); |
| return false; |
| } |
| // Statements always start with an identifier. |
| AsmToken ID = getTok(); |
| SMLoc IDLoc = ID.getLoc(); |
| StringRef IDVal; |
| int64_t LocalLabelVal = -1; |
| if (Lexer.is(AsmToken::HashDirective)) |
| return parseCppHashLineFilenameComment(IDLoc); |
| // Allow an integer followed by a ':' as a directional local label. |
| if (Lexer.is(AsmToken::Integer)) { |
| LocalLabelVal = getTok().getIntVal(); |
| if (LocalLabelVal < 0) { |
| if (!TheCondState.Ignore) { |
| Lex(); // always eat a token |
| return Error(IDLoc, "unexpected token at start of statement"); |
| } |
| IDVal = ""; |
| } else { |
| IDVal = getTok().getString(); |
| Lex(); // Consume the integer token to be used as an identifier token. |
| if (Lexer.getKind() != AsmToken::Colon) { |
| if (!TheCondState.Ignore) { |
| Lex(); // always eat a token |
| return Error(IDLoc, "unexpected token at start of statement"); |
| } |
| } |
| } |
| } else if (Lexer.is(AsmToken::Dot)) { |
| // Treat '.' as a valid identifier in this context. |
| Lex(); |
| IDVal = "."; |
| } else if (Lexer.is(AsmToken::LCurly)) { |
| // Treat '{' as a valid identifier in this context. |
| Lex(); |
| IDVal = "{"; |
| |
| } else if (Lexer.is(AsmToken::RCurly)) { |
| // Treat '}' as a valid identifier in this context. |
| Lex(); |
| IDVal = "}"; |
| } else if (Lexer.is(AsmToken::Star) && |
| getTargetParser().starIsStartOfStatement()) { |
| // Accept '*' as a valid start of statement. |
| Lex(); |
| IDVal = "*"; |
| } else if (parseIdentifier(IDVal)) { |
| if (!TheCondState.Ignore) { |
| Lex(); // always eat a token |
| return Error(IDLoc, "unexpected token at start of statement"); |
| } |
| IDVal = ""; |
| } |
| |
| // Handle conditional assembly here before checking for skipping. We |
| // have to do this so that .endif isn't skipped in a ".if 0" block for |
| // example. |
| StringMap<DirectiveKind>::const_iterator DirKindIt = |
| DirectiveKindMap.find(IDVal); |
| DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end()) |
| ? DK_NO_DIRECTIVE |
| : DirKindIt->getValue(); |
| switch (DirKind) { |
| default: |
| break; |
| case DK_IF: |
| case DK_IFEQ: |
| case DK_IFGE: |
| case DK_IFGT: |
| case DK_IFLE: |
| case DK_IFLT: |
| case DK_IFNE: |
| return parseDirectiveIf(IDLoc, DirKind); |
| case DK_IFB: |
| return parseDirectiveIfb(IDLoc, true); |
| case DK_IFNB: |
| return parseDirectiveIfb(IDLoc, false); |
| case DK_IFC: |
| return parseDirectiveIfc(IDLoc, true); |
| case DK_IFEQS: |
| return parseDirectiveIfeqs(IDLoc, true); |
| case DK_IFNC: |
| return parseDirectiveIfc(IDLoc, false); |
| case DK_IFNES: |
| return parseDirectiveIfeqs(IDLoc, false); |
| case DK_IFDEF: |
| return parseDirectiveIfdef(IDLoc, true); |
| case DK_IFNDEF: |
| case DK_IFNOTDEF: |
| return parseDirectiveIfdef(IDLoc, false); |
| case DK_ELSEIF: |
| return parseDirectiveElseIf(IDLoc); |
| case DK_ELSE: |
| return parseDirectiveElse(IDLoc); |
| case DK_ENDIF: |
| return parseDirectiveEndIf(IDLoc); |
| } |
| |
| // Ignore the statement if in the middle of inactive conditional |
| // (e.g. ".if 0"). |
| if (TheCondState.Ignore) { |
| eatToEndOfStatement(); |
| return false; |
| } |
| |
| // FIXME: Recurse on local labels? |
| |
| // See what kind of statement we have. |
| switch (Lexer.getKind()) { |
| case AsmToken::Colon: { |
| if (!getTargetParser().isLabel(ID)) |
| break; |
| if (checkForValidSection()) |
| return true; |
| |
| // identifier ':' -> Label. |
| Lex(); |
| |
| // Diagnose attempt to use '.' as a label. |
| if (IDVal == ".") |
| return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label"); |
| |
| // Diagnose attempt to use a variable as a label. |
| // |
| // FIXME: Diagnostics. Note the location of the definition as a label. |
| // FIXME: This doesn't diagnose assignment to a symbol which has been |
| // implicitly marked as external. |
| MCSymbol *Sym; |
| if (LocalLabelVal == -1) { |
| if (ParsingInlineAsm && SI) { |
| StringRef RewrittenLabel = |
| SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true); |
| assert(!RewrittenLabel.empty() && |
| "We should have an internal name here."); |
| Info.AsmRewrites->emplace_back(AOK_Label, IDLoc, IDVal.size(), |
| RewrittenLabel); |
| IDVal = RewrittenLabel; |
| } |
| Sym = getContext().getOrCreateSymbol(IDVal); |
| } else |
| Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal); |
| // End of Labels should be treated as end of line for lexing |
| // purposes but that information is not available to the Lexer who |
| // does not understand Labels. This may cause us to see a Hash |
| // here instead of a preprocessor line comment. |
| if (getTok().is(AsmToken::Hash)) { |
| StringRef CommentStr = parseStringToEndOfStatement(); |
| Lexer.Lex(); |
| Lexer.UnLex(AsmToken(AsmToken::EndOfStatement, CommentStr)); |
| } |
| |
| // Consume any end of statement token, if present, to avoid spurious |
| // AddBlankLine calls(). |
| if (getTok().is(AsmToken::EndOfStatement)) { |
| Lex(); |
| } |
| |
| // Emit the label. |
| if (!getTargetParser().isParsingInlineAsm()) |
| Out.EmitLabel(Sym, IDLoc); |
| |
| // If we are generating dwarf for assembly source files then gather the |
| // info to make a dwarf label entry for this label if needed. |
| if (enabledGenDwarfForAssembly()) |
| MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(), |
| IDLoc); |
| |
| getTargetParser().onLabelParsed(Sym); |
| |
| return false; |
| } |
| |
| case AsmToken::Equal: |
| if (!getTargetParser().equalIsAsmAssignment()) |
| break; |
| // identifier '=' ... -> assignment statement |
| Lex(); |
| |
| return parseAssignment(IDVal, true); |
| |
| default: // Normal instruction or directive. |
| break; |
| } |
| |
| // If macros are enabled, check to see if this is a macro instantiation. |
| if (areMacrosEnabled()) |
| if (const MCAsmMacro *M = getContext().lookupMacro(IDVal)) { |
| return handleMacroEntry(M, IDLoc); |
| } |
| |
| // Otherwise, we have a normal instruction or directive. |
| |
| // Directives start with "." |
| if (IDVal[0] == '.' && IDVal != ".") { |
| // There are several entities interested in parsing directives: |
| // |
| // 1. The target-specific assembly parser. Some directives are target |
| // specific or may potentially behave differently on certain targets. |
| // 2. Asm parser extensions. For example, platform-specific parsers |
| // (like the ELF parser) register themselves as extensions. |
| // 3. The generic directive parser implemented by this class. These are |
| // all the directives that behave in a target and platform independent |
| // manner, or at least have a default behavior that's shared between |
| // all targets and platforms. |
| |
| getTargetParser().flushPendingInstructions(getStreamer()); |
| |
| SMLoc StartTokLoc = getTok().getLoc(); |
| bool TPDirectiveReturn = getTargetParser().ParseDirective(ID); |
| |
| if (hasPendingError()) |
| return true; |
| // Currently the return value should be true if we are |
| // uninterested but as this is at odds with the standard parsing |
| // convention (return true = error) we have instances of a parsed |
| // directive that fails returning true as an error. Catch these |
| // cases as best as possible errors here. |
| if (TPDirectiveReturn && StartTokLoc != getTok().getLoc()) |
| return true; |
| // Return if we did some parsing or believe we succeeded. |
| if (!TPDirectiveReturn || StartTokLoc != getTok().getLoc()) |
| return false; |
| |
| // Next, check the extension directive map to see if any extension has |
| // registered itself to parse this directive. |
| std::pair<MCAsmParserExtension *, DirectiveHandler> Handler = |
| ExtensionDirectiveMap.lookup(IDVal); |
| if (Handler.first) |
| return (*Handler.second)(Handler.first, IDVal, IDLoc); |
| |
| // Finally, if no one else is interested in this directive, it must be |
| // generic and familiar to this class. |
| switch (DirKind) { |
| default: |
| break; |
| case DK_SET: |
| case DK_EQU: |
| return parseDirectiveSet(IDVal, true); |
| case DK_EQUIV: |
| return parseDirectiveSet(IDVal, false); |
| case DK_ASCII: |
| return parseDirectiveAscii(IDVal, false); |
| case DK_ASCIZ: |
| case DK_STRING: |
| return parseDirectiveAscii(IDVal, true); |
| case DK_BYTE: |
| case DK_DC_B: |
| return parseDirectiveValue(IDVal, 1); |
| case DK_DC: |
| case DK_DC_W: |
| case DK_SHORT: |
| case DK_VALUE: |
| case DK_2BYTE: |
| return parseDirectiveValue(IDVal, 2); |
| case DK_LONG: |
| case DK_INT: |
| case DK_4BYTE: |
| case DK_DC_L: |
| return parseDirectiveValue(IDVal, 4); |
| case DK_QUAD: |
| case DK_8BYTE: |
| return parseDirectiveValue(IDVal, 8); |
| case DK_DC_A: |
| return parseDirectiveValue( |
| IDVal, getContext().getAsmInfo()->getCodePointerSize()); |
| case DK_OCTA: |
| return parseDirectiveOctaValue(IDVal); |
| case DK_SINGLE: |
| case DK_FLOAT: |
| case DK_DC_S: |
| return parseDirectiveRealValue(IDVal, APFloat::IEEEsingle()); |
| case DK_DOUBLE: |
| case DK_DC_D: |
| return parseDirectiveRealValue(IDVal, APFloat::IEEEdouble()); |
| case DK_ALIGN: { |
| bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes(); |
| return parseDirectiveAlign(IsPow2, /*ExprSize=*/1); |
| } |
| case DK_ALIGN32: { |
| bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes(); |
| return parseDirectiveAlign(IsPow2, /*ExprSize=*/4); |
| } |
| case DK_BALIGN: |
| return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1); |
| case DK_BALIGNW: |
| return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2); |
| case DK_BALIGNL: |
| return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4); |
| case DK_P2ALIGN: |
| return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1); |
| case DK_P2ALIGNW: |
| return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2); |
| case DK_P2ALIGNL: |
| return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4); |
| case DK_ORG: |
| return parseDirectiveOrg(); |
| case DK_FILL: |
| return parseDirectiveFill(); |
| case DK_ZERO: |
| return parseDirectiveZero(); |
| case DK_EXTERN: |
| eatToEndOfStatement(); // .extern is the default, ignore it. |
| return false; |
| case DK_GLOBL: |
| case DK_GLOBAL: |
| return parseDirectiveSymbolAttribute(MCSA_Global); |
| case DK_LAZY_REFERENCE: |
| return parseDirectiveSymbolAttribute(MCSA_LazyReference); |
| case DK_NO_DEAD_STRIP: |
| return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip); |
| case DK_SYMBOL_RESOLVER: |
| return parseDirectiveSymbolAttribute(MCSA_SymbolResolver); |
| case DK_PRIVATE_EXTERN: |
| return parseDirectiveSymbolAttribute(MCSA_PrivateExtern); |
| case DK_REFERENCE: |
| return parseDirectiveSymbolAttribute(MCSA_Reference); |
| case DK_WEAK_DEFINITION: |
| return parseDirectiveSymbolAttribute(MCSA_WeakDefinition); |
| case DK_WEAK_REFERENCE: |
| return parseDirectiveSymbolAttribute(MCSA_WeakReference); |
| case DK_WEAK_DEF_CAN_BE_HIDDEN: |
| return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate); |
| case DK_COMM: |
| case DK_COMMON: |
| return parseDirectiveComm(/*IsLocal=*/false); |
| case DK_LCOMM: |
| return parseDirectiveComm(/*IsLocal=*/true); |
| case DK_ABORT: |
| return parseDirectiveAbort(); |
| case DK_INCLUDE: |
| return parseDirectiveInclude(); |
| case DK_INCBIN: |
| return parseDirectiveIncbin(); |
| case DK_CODE16: |
| case DK_CODE16GCC: |
| return TokError(Twine(IDVal) + |
| " not currently supported for this target"); |
| case DK_REPT: |
| return parseDirectiveRept(IDLoc, IDVal); |
| case DK_IRP: |
| return parseDirectiveIrp(IDLoc); |
| case DK_IRPC: |
| return parseDirectiveIrpc(IDLoc); |
| case DK_ENDR: |
| return parseDirectiveEndr(IDLoc); |
| case DK_BUNDLE_ALIGN_MODE: |
| return parseDirectiveBundleAlignMode(); |
| case DK_BUNDLE_LOCK: |
| return parseDirectiveBundleLock(); |
| case DK_BUNDLE_UNLOCK: |
| return parseDirectiveBundleUnlock(); |
| case DK_SLEB128: |
| return parseDirectiveLEB128(true); |
| case DK_ULEB128: |
| return parseDirectiveLEB128(false); |
| case DK_SPACE: |
| case DK_SKIP: |
| return parseDirectiveSpace(IDVal); |
| case DK_FILE: |
| return parseDirectiveFile(IDLoc); |
| case DK_LINE: |
| return parseDirectiveLine(); |
| case DK_LOC: |
| return parseDirectiveLoc(); |
| case DK_STABS: |
| return parseDirectiveStabs(); |
| case DK_CV_FILE: |
| return parseDirectiveCVFile(); |
| case DK_CV_FUNC_ID: |
| return parseDirectiveCVFuncId(); |
| case DK_CV_INLINE_SITE_ID: |
| return parseDirectiveCVInlineSiteId(); |
| case DK_CV_LOC: |
| return parseDirectiveCVLoc(); |
| case DK_CV_LINETABLE: |
| return parseDirectiveCVLinetable(); |
| case DK_CV_INLINE_LINETABLE: |
| return parseDirectiveCVInlineLinetable(); |
| case DK_CV_DEF_RANGE: |
| return parseDirectiveCVDefRange(); |
| case DK_CV_STRINGTABLE: |
| return parseDirectiveCVStringTable(); |
| case DK_CV_FILECHECKSUMS: |
| return parseDirectiveCVFileChecksums(); |
| case DK_CV_FILECHECKSUM_OFFSET: |
| return parseDirectiveCVFileChecksumOffset(); |
| case DK_CV_FPO_DATA: |
| return parseDirectiveCVFPOData(); |
| case DK_CFI_SECTIONS: |
| return parseDirectiveCFISections(); |
| case DK_CFI_STARTPROC: |
| return parseDirectiveCFIStartProc(); |
| case DK_CFI_ENDPROC: |
| return parseDirectiveCFIEndProc(); |
| case DK_CFI_DEF_CFA: |
| return parseDirectiveCFIDefCfa(IDLoc); |
| case DK_CFI_DEF_CFA_OFFSET: |
| return parseDirectiveCFIDefCfaOffset(); |
| case DK_CFI_ADJUST_CFA_OFFSET: |
| return parseDirectiveCFIAdjustCfaOffset(); |
| case DK_CFI_DEF_CFA_REGISTER: |
| return parseDirectiveCFIDefCfaRegister(IDLoc); |
| case DK_CFI_OFFSET: |
| return parseDirectiveCFIOffset(IDLoc); |
| case DK_CFI_REL_OFFSET: |
| return parseDirectiveCFIRelOffset(IDLoc); |
| case DK_CFI_PERSONALITY: |
| return parseDirectiveCFIPersonalityOrLsda(true); |
| case DK_CFI_LSDA: |
| return parseDirectiveCFIPersonalityOrLsda(false); |
| case DK_CFI_REMEMBER_STATE: |
| return parseDirectiveCFIRememberState(); |
| case DK_CFI_RESTORE_STATE: |
| return parseDirectiveCFIRestoreState(); |
| case DK_CFI_SAME_VALUE: |
| return parseDirectiveCFISameValue(IDLoc); |
| case DK_CFI_RESTORE: |
| return parseDirectiveCFIRestore(IDLoc); |
| case DK_CFI_ESCAPE: |
| return parseDirectiveCFIEscape(); |
| case DK_CFI_RETURN_COLUMN: |
| return parseDirectiveCFIReturnColumn(IDLoc); |
| case DK_CFI_SIGNAL_FRAME: |
| return parseDirectiveCFISignalFrame(); |
| case DK_CFI_UNDEFINED: |
| return parseDirectiveCFIUndefined(IDLoc); |
| case DK_CFI_REGISTER: |
| return parseDirectiveCFIRegister(IDLoc); |
| case DK_CFI_WINDOW_SAVE: |
| return parseDirectiveCFIWindowSave(); |
| case DK_MACROS_ON: |
| case DK_MACROS_OFF: |
| return parseDirectiveMacrosOnOff(IDVal); |
| case DK_MACRO: |
| return parseDirectiveMacro(IDLoc); |
| case DK_ALTMACRO: |
| case DK_NOALTMACRO: |
| return parseDirectiveAltmacro(IDVal); |
| case DK_EXITM: |
| return parseDirectiveExitMacro(IDVal); |
| case DK_ENDM: |
| case DK_ENDMACRO: |
| return parseDirectiveEndMacro(IDVal); |
| case DK_PURGEM: |
| return parseDirectivePurgeMacro(IDLoc); |
| case DK_END: |
| return parseDirectiveEnd(IDLoc); |
| case DK_ERR: |
| return parseDirectiveError(IDLoc, false); |
| case DK_ERROR: |
| return parseDirectiveError(IDLoc, true); |
| case DK_WARNING: |
| return parseDirectiveWarning(IDLoc); |
| case DK_RELOC: |
| return parseDirectiveReloc(IDLoc); |
| case DK_DCB: |
| case DK_DCB_W: |
| return parseDirectiveDCB(IDVal, 2); |
| case DK_DCB_B: |
| return parseDirectiveDCB(IDVal, 1); |
| case DK_DCB_D: |
| return parseDirectiveRealDCB(IDVal, APFloat::IEEEdouble()); |
| case DK_DCB_L: |
| return parseDirectiveDCB(IDVal, 4); |
| case DK_DCB_S: |
| return parseDirectiveRealDCB(IDVal, APFloat::IEEEsingle()); |
| case DK_DC_X: |
| case DK_DCB_X: |
| return TokError(Twine(IDVal) + |
| " not currently supported for this target"); |
| case DK_DS: |
| case DK_DS_W: |
| return parseDirectiveDS(IDVal, 2); |
| case DK_DS_B: |
| return parseDirectiveDS(IDVal, 1); |
| case DK_DS_D: |
| return parseDirectiveDS(IDVal, 8); |
| case DK_DS_L: |
| case DK_DS_S: |
| return parseDirectiveDS(IDVal, 4); |
| case DK_DS_P: |
| case DK_DS_X: |
| return parseDirectiveDS(IDVal, 12); |
| case DK_PRINT: |
| return parseDirectivePrint(IDLoc); |
| case DK_ADDRSIG: |
| return parseDirectiveAddrsig(); |
| case DK_ADDRSIG_SYM: |
| return parseDirectiveAddrsigSym(); |
| } |
| |
| return Error(IDLoc, "unknown directive"); |
| } |
| |
| // __asm _emit or __asm __emit |
| if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" || |
| IDVal == "_EMIT" || IDVal == "__EMIT")) |
| return parseDirectiveMSEmit(IDLoc, Info, IDVal.size()); |
| |
| // __asm align |
| if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN")) |
| return parseDirectiveMSAlign(IDLoc, Info); |
| |
| if (ParsingInlineAsm && (IDVal == "even" || IDVal == "EVEN")) |
| Info.AsmRewrites->emplace_back(AOK_EVEN, IDLoc, 4); |
| if (checkForValidSection()) |
| return true; |
| |
| // Canonicalize the opcode to lower case. |
| std::string OpcodeStr = IDVal.lower(); |
| ParseInstructionInfo IInfo(Info.AsmRewrites); |
| bool ParseHadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, ID, |
| Info.ParsedOperands); |
| Info.ParseError = ParseHadError; |
| |
| // Dump the parsed representation, if requested. |
| if (getShowParsedOperands()) { |
| SmallString<256> Str; |
| raw_svector_ostream OS(Str); |
| OS << "parsed instruction: ["; |
| for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) { |
| if (i != 0) |
| OS << ", "; |
| Info.ParsedOperands[i]->print(OS); |
| } |
| OS << "]"; |
| |
| printMessage(IDLoc, SourceMgr::DK_Note, OS.str()); |
| } |
| |
| // Fail even if ParseInstruction erroneously returns false. |
| if (hasPendingError() || ParseHadError) |
| return true; |
| |
| // If we are generating dwarf for the current section then generate a .loc |
| // directive for the instruction. |
| if (!ParseHadError && enabledGenDwarfForAssembly() && |
| getContext().getGenDwarfSectionSyms().count( |
| getStreamer().getCurrentSectionOnly())) { |
| unsigned Line; |
| if (ActiveMacros.empty()) |
| Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer); |
| else |
| Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc, |
| ActiveMacros.front()->ExitBuffer); |
| |
| // If we previously parsed a cpp hash file line comment then make sure the |
| // current Dwarf File is for the CppHashFilename if not then emit the |
| // Dwarf File table for it and adjust the line number for the .loc. |
| if (!CppHashInfo.Filename.empty()) { |
| unsigned FileNumber = getStreamer().EmitDwarfFileDirective( |
| 0, StringRef(), CppHashInfo.Filename); |
| getContext().setGenDwarfFileNumber(FileNumber); |
| |
| unsigned CppHashLocLineNo = |
| SrcMgr.FindLineNumber(CppHashInfo.Loc, CppHashInfo.Buf); |
| Line = CppHashInfo.LineNumber - 1 + (Line - CppHashLocLineNo); |
| } |
| |
| getStreamer().EmitDwarfLocDirective( |
| getContext().getGenDwarfFileNumber(), Line, 0, |
| DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0, |
| StringRef()); |
| } |
| |
| // If parsing succeeded, match the instruction. |
| if (!ParseHadError) { |
| uint64_t ErrorInfo; |
| if (getTargetParser().MatchAndEmitInstruction( |
| IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo, |
| getTargetParser().isParsingInlineAsm())) |
| return true; |
| } |
| return false; |
| } |
| |
| // Parse and erase curly braces marking block start/end |
| bool |
| AsmParser::parseCurlyBlockScope(SmallVectorImpl<AsmRewrite> &AsmStrRewrites) { |
| // Identify curly brace marking block start/end |
| if (Lexer.isNot(AsmToken::LCurly) && Lexer.isNot(AsmToken::RCurly)) |
| return false; |
| |
| SMLoc StartLoc = Lexer.getLoc(); |
| Lex(); // Eat the brace |
| if (Lexer.is(AsmToken::EndOfStatement)) |
| Lex(); // Eat EndOfStatement following the brace |
| |
| // Erase the block start/end brace from the output asm string |
| AsmStrRewrites.emplace_back(AOK_Skip, StartLoc, Lexer.getLoc().getPointer() - |
| StartLoc.getPointer()); |
| return true; |
| } |
| |
| /// parseCppHashLineFilenameComment as this: |
| /// ::= # number "filename" |
| bool AsmParser::parseCppHashLineFilenameComment(SMLoc L) { |
| Lex(); // Eat the hash token. |
| // Lexer only ever emits HashDirective if it fully formed if it's |
| // done the checking already so this is an internal error. |
| assert(getTok().is(AsmToken::Integer) && |
| "Lexing Cpp line comment: Expected Integer"); |
| int64_t LineNumber = getTok().getIntVal(); |
| Lex(); |
| assert(getTok().is(AsmToken::String) && |
| "Lexing Cpp line comment: Expected String"); |
| StringRef Filename = getTok().getString(); |
| Lex(); |
| |
| // Get rid of the enclosing quotes. |
| Filename = Filename.substr(1, Filename.size() - 2); |
| |
| // Save the SMLoc, Filename and LineNumber for later use by diagnostics. |
| CppHashInfo.Loc = L; |
| CppHashInfo.Filename = Filename; |
| CppHashInfo.LineNumber = LineNumber; |
| CppHashInfo.Buf = CurBuffer; |
| return false; |
| } |
| |
| /// will use the last parsed cpp hash line filename comment |
| /// for the Filename and LineNo if any in the diagnostic. |
| void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) { |
| const AsmParser *Parser = static_cast<const AsmParser *>(Context); |
| raw_ostream &OS = errs(); |
| |
| const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr(); |
| SMLoc DiagLoc = Diag.getLoc(); |
| unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); |
| unsigned CppHashBuf = |
| Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashInfo.Loc); |
| |
| // Like SourceMgr::printMessage() we need to print the include stack if any |
| // before printing the message. |
| unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); |
| if (!Parser->SavedDiagHandler && DiagCurBuffer && |
| DiagCurBuffer != DiagSrcMgr.getMainFileID()) { |
| SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer); |
| DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS); |
| } |
| |
| // If we have not parsed a cpp hash line filename comment or the source |
| // manager changed or buffer changed (like in a nested include) then just |
| // print the normal diagnostic using its Filename and LineNo. |
| if (!Parser->CppHashInfo.LineNumber || &DiagSrcMgr != &Parser->SrcMgr || |
| DiagBuf != CppHashBuf) { |
| if (Parser->SavedDiagHandler) |
| Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext); |
| else |
| Diag.print(nullptr, OS); |
| return; |
| } |
| |
| // Use the CppHashFilename and calculate a line number based on the |
| // CppHashInfo.Loc and CppHashInfo.LineNumber relative to this Diag's SMLoc |
| // for the diagnostic. |
| const std::string &Filename = Parser->CppHashInfo.Filename; |
| |
| int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf); |
| int CppHashLocLineNo = |
| Parser->SrcMgr.FindLineNumber(Parser->CppHashInfo.Loc, CppHashBuf); |
| int LineNo = |
| Parser->CppHashInfo.LineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo); |
| |
| SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo, |
| Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(), |
| Diag.getLineContents(), Diag.getRanges()); |
| |
| if (Parser->SavedDiagHandler) |
| Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext); |
| else |
| NewDiag.print(nullptr, OS); |
| } |
| |
| // FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The |
| // difference being that that function accepts '@' as part of identifiers and |
| // we can't do that. AsmLexer.cpp should probably be changed to handle |
| // '@' as a special case when needed. |
| static bool isIdentifierChar(char c) { |
| return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' || |
| c == '.'; |
| } |
| |
| bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body, |
| ArrayRef<MCAsmMacroParameter> Parameters, |
| ArrayRef<MCAsmMacroArgument> A, |
| bool EnableAtPseudoVariable, SMLoc L) { |
| unsigned NParameters = Parameters.size(); |
| bool HasVararg = NParameters ? Parameters.back().Vararg : false; |
| if ((!IsDarwin || NParameters != 0) && NParameters != A.size()) |
| return Error(L, "Wrong number of arguments"); |
| |
| // A macro without parameters is handled differently on Darwin: |
| // gas accepts no arguments and does no substitutions |
| while (!Body.empty()) { |
| // Scan for the next substitution. |
| std::size_t End = Body.size(), Pos = 0; |
| for (; Pos != End; ++Pos) { |
| // Check for a substitution or escape. |
| if (IsDarwin && !NParameters) { |
| // This macro has no parameters, look for $0, $1, etc. |
| if (Body[Pos] != '$' || Pos + 1 == End) |
| continue; |
| |
| char Next = Body[Pos + 1]; |
| if (Next == '$' || Next == 'n' || |
| isdigit(static_cast<unsigned char>(Next))) |
| break; |
| } else { |
| // This macro has parameters, look for \foo, \bar, etc. |
| if (Body[Pos] == '\\' && Pos + 1 != End) |
| break; |
| } |
| } |
| |
| // Add the prefix. |
| OS << Body.slice(0, Pos); |
| |
| // Check if we reached the end. |
| if (Pos == End) |
| break; |
| |
| if (IsDarwin && !NParameters) { |
| switch (Body[Pos + 1]) { |
| // $$ => $ |
| case '$': |
| OS << '$'; |
| break; |
| |
| // $n => number of arguments |
| case 'n': |
| OS << A.size(); |
| break; |
| |
| // $[0-9] => argument |
| default: { |
| // Missing arguments are ignored. |
| unsigned Index = Body[Pos + 1] - '0'; |
| if (Index >= A.size()) |
| break; |
| |
| // Otherwise substitute with the token values, with spaces eliminated. |
| for (const AsmToken &Token : A[Index]) |
| OS << Token.getString(); |
| break; |
| } |
| } |
| Pos += 2; |
| } else { |
| unsigned I = Pos + 1; |
| |
| // Check for the \@ pseudo-variable. |
| if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End) |
| ++I; |
| else |
| while (isIdentifierChar(Body[I]) && I + 1 != End) |
| ++I; |
| |
| const char *Begin = Body.data() + Pos + 1; |
| StringRef Argument(Begin, I - (Pos + 1)); |
| unsigned Index = 0; |
| |
| if (Argument == "@") { |
| OS << NumOfMacroInstantiations; |
| Pos += 2; |
| } else { |
| for (; Index < NParameters; ++Index) |
| if (Parameters[Index].Name == Argument) |
| break; |
| |
| if (Index == NParameters) { |
| if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')') |
| Pos += 3; |
| else { |
| OS << '\\' << Argument; |
| Pos = I; |
| } |
| } else { |
| bool VarargParameter = HasVararg && Index == (NParameters - 1); |
| for (const AsmToken &Token : A[Index]) |
| // For altmacro mode, you can write '%expr'. |
| // The prefix '%' evaluates the expression 'expr' |
| // and uses the result as a string (e.g. replace %(1+2) with the string "3"). |
| // Here, we identify the integer token which is the result of the |
| // absolute expression evaluation and replace it with its string representation. |
| if ((Lexer.IsaAltMacroMode()) && |
| (*(Token.getString().begin()) == '%') && Token.is(AsmToken::Integer)) |
| // Emit an integer value to the buffer. |
| OS << Token.getIntVal(); |
| // Only Token that was validated as a string and begins with '<' |
| // is considered altMacroString!!! |
| else if ((Lexer.IsaAltMacroMode()) && |
| (*(Token.getString().begin()) == '<') && |
| Token.is(AsmToken::String)) { |
| std::string Res; |
| altMacroString(Token.getStringContents(), Res); |
| OS << Res; |
| } |
| // We expect no quotes around the string's contents when |
| // parsing for varargs. |
| else if (Token.isNot(AsmToken::String) || VarargParameter) |
| OS << Token.getString(); |
| else |
| OS << Token.getStringContents(); |
| |
| Pos += 1 + Argument.size(); |
| } |
| } |
| } |
| // Update the scan point. |
| Body = Body.substr(Pos); |
| } |
| |
| return false; |
| } |
| |
| MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL, |
| size_t CondStackDepth) |
| : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL), |
| CondStackDepth(CondStackDepth) {} |
| |
| static bool isOperator(AsmToken::TokenKind kind) { |
| switch (kind) { |
| default: |
| return false; |
| case AsmToken::Plus: |
| case AsmToken::Minus: |
| case AsmToken::Tilde: |
| case AsmToken::Slash: |
| case AsmToken::Star: |
| case AsmToken::Dot: |
| case AsmToken::Equal: |
| case AsmToken::EqualEqual: |
| case AsmToken::Pipe: |
| case AsmToken::PipePipe: |
| case AsmToken::Caret: |
| case AsmToken::Amp: |
| case AsmToken::AmpAmp: |
| case AsmToken::Exclaim: |
| case AsmToken::ExclaimEqual: |
| case AsmToken::Less: |
| case AsmToken::LessEqual: |
| case AsmToken::LessLess: |
| case AsmToken::LessGreater: |
| case AsmToken::Greater: |
| case AsmToken::GreaterEqual: |
| case AsmToken::GreaterGreater: |
| return true; |
| } |
| } |
| |
| namespace { |
| |
| class AsmLexerSkipSpaceRAII { |
| public: |
| AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) { |
| Lexer.setSkipSpace(SkipSpace); |
| } |
| |
| ~AsmLexerSkipSpaceRAII() { |
| Lexer.setSkipSpace(true); |
| } |
| |
| private: |
| AsmLexer &Lexer; |
| }; |
| |
| } // end anonymous namespace |
| |
| bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) { |
| |
| if (Vararg) { |
| if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| StringRef Str = parseStringToEndOfStatement(); |
| MA.emplace_back(AsmToken::String, Str); |
| } |
| return false; |
| } |
| |
| unsigned ParenLevel = 0; |
| |
| // Darwin doesn't use spaces to delmit arguments. |
| AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin); |
| |
| bool SpaceEaten; |
| |
| while (true) { |
| SpaceEaten = false; |
| if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) |
| return TokError("unexpected token in macro instantiation"); |
| |
| if (ParenLevel == 0) { |
| |
| if (Lexer.is(AsmToken::Comma)) |
| break; |
| |
| if (Lexer.is(AsmToken::Space)) { |
| SpaceEaten = true; |
| Lexer.Lex(); // Eat spaces |
| } |
| |
| // Spaces can delimit parameters, but could also be part an expression. |
| // If the token after a space is an operator, add the token and the next |
| // one into this argument |
| if (!IsDarwin) { |
| if (isOperator(Lexer.getKind())) { |
| MA.push_back(getTok()); |
| Lexer.Lex(); |
| |
| // Whitespace after an operator can be ignored. |
| if (Lexer.is(AsmToken::Space)) |
| Lexer.Lex(); |
| |
| continue; |
| } |
| } |
| if (SpaceEaten) |
| break; |
| } |
| |
| // handleMacroEntry relies on not advancing the lexer here |
| // to be able to fill in the remaining default parameter values |
| if (Lexer.is(AsmToken::EndOfStatement)) |
| break; |
| |
| // Adjust the current parentheses level. |
| if (Lexer.is(AsmToken::LParen)) |
| ++ParenLevel; |
| else if (Lexer.is(AsmToken::RParen) && ParenLevel) |
| --ParenLevel; |
| |
| // Append the token to the current argument list. |
| MA.push_back(getTok()); |
| Lexer.Lex(); |
| } |
| |
| if (ParenLevel != 0) |
| return TokError("unbalanced parentheses in macro argument"); |
| return false; |
| } |
| |
| // Parse the macro instantiation arguments. |
| bool AsmParser::parseMacroArguments(const MCAsmMacro *M, |
| MCAsmMacroArguments &A) { |
| const unsigned NParameters = M ? M->Parameters.size() : 0; |
| bool NamedParametersFound = false; |
| SmallVector<SMLoc, 4> FALocs; |
| |
| A.resize(NParameters); |
| FALocs.resize(NParameters); |
| |
| // Parse two kinds of macro invocations: |
| // - macros defined without any parameters accept an arbitrary number of them |
| // - macros defined with parameters accept at most that many of them |
| bool HasVararg = NParameters ? M->Parameters.back().Vararg : false; |
| for (unsigned Parameter = 0; !NParameters || Parameter < NParameters; |
| ++Parameter) { |
| SMLoc IDLoc = Lexer.getLoc(); |
| MCAsmMacroParameter FA; |
| |
| if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) { |
| if (parseIdentifier(FA.Name)) |
| return Error(IDLoc, "invalid argument identifier for formal argument"); |
| |
| if (Lexer.isNot(AsmToken::Equal)) |
| return TokError("expected '=' after formal parameter identifier"); |
| |
| Lex(); |
| |
| NamedParametersFound = true; |
| } |
| bool Vararg = HasVararg && Parameter == (NParameters - 1); |
| |
| if (NamedParametersFound && FA.Name.empty()) |
| return Error(IDLoc, "cannot mix positional and keyword arguments"); |
| |
| SMLoc StrLoc = Lexer.getLoc(); |
| SMLoc EndLoc; |
| if (Lexer.IsaAltMacroMode() && Lexer.is(AsmToken::Percent)) { |
| const MCExpr *AbsoluteExp; |
| int64_t Value; |
| /// Eat '%' |
| Lex(); |
| if (parseExpression(AbsoluteExp, EndLoc)) |
| return false; |
| if (!AbsoluteExp->evaluateAsAbsolute(Value, |
| getStreamer().getAssemblerPtr())) |
| return Error(StrLoc, "expected absolute expression"); |
| const char *StrChar = StrLoc.getPointer(); |
| const char *EndChar = EndLoc.getPointer(); |
| AsmToken newToken(AsmToken::Integer, StringRef(StrChar , EndChar - StrChar), Value); |
| FA.Value.push_back(newToken); |
| } else if (Lexer.IsaAltMacroMode() && Lexer.is(AsmToken::Less) && |
| isAltmacroString(StrLoc, EndLoc)) { |
| const char *StrChar = StrLoc.getPointer(); |
| const char *EndChar = EndLoc.getPointer(); |
| jumpToLoc(EndLoc, CurBuffer); |
| /// Eat from '<' to '>' |
| Lex(); |
| AsmToken newToken(AsmToken::String, StringRef(StrChar, EndChar - StrChar)); |
| FA.Value.push_back(newToken); |
| } else if(parseMacroArgument(FA.Value, Vararg)) |
| return true; |
| |
| unsigned PI = Parameter; |
| if (!FA.Name.empty()) { |
| unsigned FAI = 0; |
| for (FAI = 0; FAI < NParameters; ++FAI) |
| if (M->Parameters[FAI].Name == FA.Name) |
| break; |
| |
| if (FAI >= NParameters) { |
| assert(M && "expected macro to be defined"); |
| return Error(IDLoc, "parameter named '" + FA.Name + |
| "' does not exist for macro '" + M->Name + "'"); |
| } |
| PI = FAI; |
| } |
| |
| if (!FA.Value.empty()) { |
| if (A.size() <= PI) |
| A.resize(PI + 1); |
| A[PI] = FA.Value; |
| |
| if (FALocs.size() <= PI) |
| FALocs.resize(PI + 1); |
| |
| FALocs[PI] = Lexer.getLoc(); |
| } |
| |
| // At the end of the statement, fill in remaining arguments that have |
| // default values. If there aren't any, then the next argument is |
| // required but missing |
| if (Lexer.is(AsmToken::EndOfStatement)) { |
| bool Failure = false; |
| for (unsigned FAI = 0; FAI < NParameters; ++FAI) { |
| if (A[FAI].empty()) { |
| if (M->Parameters[FAI].Required) { |
| Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(), |
| "missing value for required parameter " |
| "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'"); |
| Failure = true; |
| } |
| |
| if (!M->Parameters[FAI].Value.empty()) |
| A[FAI] = M->Parameters[FAI].Value; |
| } |
| } |
| return Failure; |
| } |
| |
| if (Lexer.is(AsmToken::Comma)) |
| Lex(); |
| } |
| |
| return TokError("too many positional arguments"); |
| } |
| |
| bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) { |
| // Arbitrarily limit macro nesting depth (default matches 'as'). We can |
| // eliminate this, although we should protect against infinite loops. |
| unsigned MaxNestingDepth = AsmMacroMaxNestingDepth; |
| if (ActiveMacros.size() == MaxNestingDepth) { |
| std::ostringstream MaxNestingDepthError; |
| MaxNestingDepthError << "macros cannot be nested more than " |
| << MaxNestingDepth << " levels deep." |
| << " Use -asm-macro-max-nesting-depth to increase " |
| "this limit."; |
| return TokError(MaxNestingDepthError.str()); |
| } |
| |
| MCAsmMacroArguments A; |
| if (parseMacroArguments(M, A)) |
| return true; |
| |
| // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| // to hold the macro body with substitutions. |
| SmallString<256> Buf; |
| StringRef Body = M->Body; |
| raw_svector_ostream OS(Buf); |
| |
| if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc())) |
| return true; |
| |
| // We include the .endmacro in the buffer as our cue to exit the macro |
| // instantiation. |
| OS << ".endmacro\n"; |
| |
| std::unique_ptr<MemoryBuffer> Instantiation = |
| MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); |
| |
| // Create the macro instantiation object and add to the current macro |
| // instantiation stack. |
| MacroInstantiation *MI = new MacroInstantiation( |
| NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()); |
| ActiveMacros.push_back(MI); |
| |
| ++NumOfMacroInstantiations; |
| |
| // Jump to the macro instantiation and prime the lexer. |
| CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc()); |
| Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
| Lex(); |
| |
| return false; |
| } |
| |
| void AsmParser::handleMacroExit() { |
| // Jump to the EndOfStatement we should return to, and consume it. |
| jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer); |
| Lex(); |
| |
| // Pop the instantiation entry. |
| delete ActiveMacros.back(); |
| ActiveMacros.pop_back(); |
| } |
| |
| bool AsmParser::parseAssignment(StringRef Name, bool allow_redef, |
| bool NoDeadStrip) { |
| MCSymbol *Sym; |
| const MCExpr *Value; |
| if (MCParserUtils::parseAssignmentExpression(Name, allow_redef, *this, Sym, |
| Value)) |
| return true; |
| |
| if (!Sym) { |
| // In the case where we parse an expression starting with a '.', we will |
| // not generate an error, nor will we create a symbol. In this case we |
| // should just return out. |
| return false; |
| } |
| |
| // Do the assignment. |
| Out.EmitAssignment(Sym, Value); |
| if (NoDeadStrip) |
| Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip); |
| |
| return false; |
| } |
| |
| /// parseIdentifier: |
| /// ::= identifier |
| /// ::= string |
| bool AsmParser::parseIdentifier(StringRef &Res) { |
| // The assembler has relaxed rules for accepting identifiers, in particular we |
| // allow things like '.globl $foo' and '.def @feat.00', which would normally be |
| // separate tokens. At this level, we have already lexed so we cannot (currently) |
| // handle this as a context dependent token, instead we detect adjacent tokens |
| // and return the combined identifier. |
| if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) { |
| SMLoc PrefixLoc = getLexer().getLoc(); |
| |
| // Consume the prefix character, and check for a following identifier. |
| |
| AsmToken Buf[1]; |
| Lexer.peekTokens(Buf, false); |
| |
| if (Buf[0].isNot(AsmToken::Identifier)) |
| return true; |
| |
| // We have a '$' or '@' followed by an identifier, make sure they are adjacent. |
| if (PrefixLoc.getPointer() + 1 != Buf[0].getLoc().getPointer()) |
| return true; |
| |
| // eat $ or @ |
| Lexer.Lex(); // Lexer's Lex guarantees consecutive token. |
| // Construct the joined identifier and consume the token. |
| Res = |
| StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1); |
| Lex(); // Parser Lex to maintain invariants. |
| return false; |
| } |
| |
| if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String)) |
| return true; |
| |
| Res = getTok().getIdentifier(); |
| |
| Lex(); // Consume the identifier token. |
| |
| return false; |
| } |
| |
| /// parseDirectiveSet: |
| /// ::= .equ identifier ',' expression |
| /// ::= .equiv identifier ',' expression |
| /// ::= .set identifier ',' expression |
| bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) { |
| StringRef Name; |
| if (check(parseIdentifier(Name), "expected identifier") || |
| parseToken(AsmToken::Comma) || parseAssignment(Name, allow_redef, true)) |
| return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); |
| return false; |
| } |
|