From 686ed8d2489c4ec25d4b20756cd70240a01bd345 Mon Sep 17 00:00:00 2001 From: Sean Callanan Date: Tue, 19 Jan 2010 20:22:31 +0000 Subject: [PATCH] Added a Lex function to the AsmParser, to allow handling of include directives to occur within the parser itself. This will break the lexer's dependency on a SourceMgr as input. llvm-svn: 93899 --- llvm/tools/llvm-mc/AsmParser.cpp | 152 ++++++++++++++++--------------- llvm/tools/llvm-mc/AsmParser.h | 3 + 2 files changed, 81 insertions(+), 74 deletions(-) diff --git a/llvm/tools/llvm-mc/AsmParser.cpp b/llvm/tools/llvm-mc/AsmParser.cpp index 3b05f1569c7a..6d06001c43e1 100644 --- a/llvm/tools/llvm-mc/AsmParser.cpp +++ b/llvm/tools/llvm-mc/AsmParser.cpp @@ -100,6 +100,10 @@ bool AsmParser::TokError(const char *Msg) { return true; } +const AsmToken &AsmParser::Lex() { + return Lexer.Lex(); +} + bool AsmParser::Run() { // Create the initial section. // @@ -111,7 +115,7 @@ bool AsmParser::Run() { // Prime the lexer. - Lexer.Lex(); + Lex(); bool HadError = false; @@ -178,11 +182,11 @@ bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive, void AsmParser::EatToEndOfStatement() { while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof)) - Lexer.Lex(); + Lex(); // Eat EOL. if (Lexer.is(AsmToken::EndOfStatement)) - Lexer.Lex(); + Lex(); } @@ -196,7 +200,7 @@ bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) { if (Lexer.isNot(AsmToken::RParen)) return TokError("expected ')' in parentheses expression"); EndLoc = Lexer.getLoc(); - Lexer.Lex(); + Lex(); return false; } @@ -221,7 +225,7 @@ bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { default: return TokError("unknown token in expression"); case AsmToken::Exclaim: - Lexer.Lex(); // Eat the operator. + Lex(); // Eat the operator. if (ParsePrimaryExpr(Res, EndLoc)) return true; Res = MCUnaryExpr::CreateLNot(Res, getContext()); @@ -231,7 +235,7 @@ bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { // This is a symbol reference. MCSymbol *Sym = CreateSymbol(Lexer.getTok().getIdentifier()); EndLoc = Lexer.getLoc(); - Lexer.Lex(); // Eat identifier. + Lex(); // Eat identifier. // If this is an absolute variable reference, substitute it now to preserve // semantics in the face of reassignment. @@ -247,25 +251,25 @@ bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { case AsmToken::Integer: Res = MCConstantExpr::Create(Lexer.getTok().getIntVal(), getContext()); EndLoc = Lexer.getLoc(); - Lexer.Lex(); // Eat token. + Lex(); // Eat token. return false; case AsmToken::LParen: - Lexer.Lex(); // Eat the '('. + Lex(); // Eat the '('. return ParseParenExpr(Res, EndLoc); case AsmToken::Minus: - Lexer.Lex(); // Eat the operator. + Lex(); // Eat the operator. if (ParsePrimaryExpr(Res, EndLoc)) return true; Res = MCUnaryExpr::CreateMinus(Res, getContext()); return false; case AsmToken::Plus: - Lexer.Lex(); // Eat the operator. + Lex(); // Eat the operator. if (ParsePrimaryExpr(Res, EndLoc)) return true; Res = MCUnaryExpr::CreatePlus(Res, getContext()); return false; case AsmToken::Tilde: - Lexer.Lex(); // Eat the operator. + Lex(); // Eat the operator. if (ParsePrimaryExpr(Res, EndLoc)) return true; Res = MCUnaryExpr::CreateNot(Res, getContext()); @@ -398,7 +402,7 @@ bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, if (TokPrec < Precedence) return false; - Lexer.Lex(); + Lex(); // Eat the next primary expression. const MCExpr *RHS; @@ -426,7 +430,7 @@ bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, /// ::= Label* Identifier OperandList* EndOfStatement bool AsmParser::ParseStatement() { if (Lexer.is(AsmToken::EndOfStatement)) { - Lexer.Lex(); + Lex(); return false; } @@ -443,7 +447,7 @@ bool AsmParser::ParseStatement() { switch (Lexer.getKind()) { case AsmToken::Colon: { // identifier ':' -> Label. - Lexer.Lex(); + Lex(); // Diagnose attempt to use a variable as a label. // @@ -462,7 +466,7 @@ bool AsmParser::ParseStatement() { case AsmToken::Equal: // identifier '=' ... -> assignment statement - Lexer.Lex(); + Lex(); return ParseAssignment(IDVal); @@ -734,7 +738,7 @@ bool AsmParser::ParseStatement() { return TokError("unexpected token in argument list"); // Eat the end of statement marker. - Lexer.Lex(); + Lex(); MCInst Inst; @@ -771,7 +775,7 @@ bool AsmParser::ParseAssignment(const StringRef &Name) { return TokError("unexpected token in assignment"); // Eat the end of statement marker. - Lexer.Lex(); + Lex(); // Validate that the LHS is allowed to be a variable (either it has not been // used as a symbol, or it is an absolute symbol). @@ -809,7 +813,7 @@ bool AsmParser::ParseIdentifier(StringRef &Res) { Res = Lexer.getTok().getIdentifier(); - Lexer.Lex(); // Consume the identifier token. + Lex(); // Consume the identifier token. return false; } @@ -824,7 +828,7 @@ bool AsmParser::ParseDirectiveSet() { if (Lexer.isNot(AsmToken::Comma)) return TokError("unexpected token in '.set'"); - Lexer.Lex(); + Lex(); return ParseAssignment(Name); } @@ -852,10 +856,10 @@ bool AsmParser::ParseDirectiveDarwinSection() { StringRef EOL = Lexer.LexUntilEndOfStatement(); SectionSpec.append(EOL.begin(), EOL.end()); - Lexer.Lex(); + Lex(); if (Lexer.isNot(AsmToken::EndOfStatement)) return TokError("unexpected token in '.section' directive"); - Lexer.Lex(); + Lex(); StringRef Segment, Section; @@ -880,7 +884,7 @@ bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment, unsigned StubSize) { if (Lexer.isNot(AsmToken::EndOfStatement)) return TokError("unexpected token in section switching directive"); - Lexer.Lex(); + Lex(); // FIXME: Arch specific. Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize, @@ -974,18 +978,18 @@ bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) { if (ZeroTerminated) Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE); - Lexer.Lex(); + Lex(); if (Lexer.is(AsmToken::EndOfStatement)) break; if (Lexer.isNot(AsmToken::Comma)) return TokError("unexpected token in '.ascii' or '.asciz' directive"); - Lexer.Lex(); + Lex(); } } - Lexer.Lex(); + Lex(); return false; } @@ -1007,11 +1011,11 @@ bool AsmParser::ParseDirectiveValue(unsigned Size) { // FIXME: Improve diagnostic. if (Lexer.isNot(AsmToken::Comma)) return TokError("unexpected token in directive"); - Lexer.Lex(); + Lex(); } } - Lexer.Lex(); + Lex(); return false; } @@ -1027,7 +1031,7 @@ bool AsmParser::ParseDirectiveSpace() { if (Lexer.isNot(AsmToken::EndOfStatement)) { if (Lexer.isNot(AsmToken::Comma)) return TokError("unexpected token in '.space' directive"); - Lexer.Lex(); + Lex(); if (ParseAbsoluteExpression(FillExpr)) return true; @@ -1038,7 +1042,7 @@ bool AsmParser::ParseDirectiveSpace() { return TokError("unexpected token in '.space' directive"); } - Lexer.Lex(); + Lex(); if (NumBytes <= 0) return TokError("invalid number of bytes in '.space' directive"); @@ -1058,7 +1062,7 @@ bool AsmParser::ParseDirectiveFill() { if (Lexer.isNot(AsmToken::Comma)) return TokError("unexpected token in '.fill' directive"); - Lexer.Lex(); + Lex(); int64_t FillSize; if (ParseAbsoluteExpression(FillSize)) @@ -1066,7 +1070,7 @@ bool AsmParser::ParseDirectiveFill() { if (Lexer.isNot(AsmToken::Comma)) return TokError("unexpected token in '.fill' directive"); - Lexer.Lex(); + Lex(); int64_t FillExpr; if (ParseAbsoluteExpression(FillExpr)) @@ -1075,7 +1079,7 @@ bool AsmParser::ParseDirectiveFill() { if (Lexer.isNot(AsmToken::EndOfStatement)) return TokError("unexpected token in '.fill' directive"); - Lexer.Lex(); + Lex(); if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8) return TokError("invalid '.fill' size, expected 1, 2, 4, or 8"); @@ -1100,7 +1104,7 @@ bool AsmParser::ParseDirectiveOrg() { if (Lexer.isNot(AsmToken::EndOfStatement)) { if (Lexer.isNot(AsmToken::Comma)) return TokError("unexpected token in '.org' directive"); - Lexer.Lex(); + Lex(); if (ParseAbsoluteExpression(FillExpr)) return true; @@ -1109,7 +1113,7 @@ bool AsmParser::ParseDirectiveOrg() { return TokError("unexpected token in '.org' directive"); } - Lexer.Lex(); + Lex(); // FIXME: Only limited forms of relocatable expressions are accepted here, it // has to be relative to the current section. @@ -1133,7 +1137,7 @@ bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) { if (Lexer.isNot(AsmToken::EndOfStatement)) { if (Lexer.isNot(AsmToken::Comma)) return TokError("unexpected token in directive"); - Lexer.Lex(); + Lex(); // The fill expression can be omitted while specifying a maximum number of // alignment bytes, e.g: @@ -1147,7 +1151,7 @@ bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) { if (Lexer.isNot(AsmToken::EndOfStatement)) { if (Lexer.isNot(AsmToken::Comma)) return TokError("unexpected token in directive"); - Lexer.Lex(); + Lex(); MaxBytesLoc = Lexer.getLoc(); if (ParseAbsoluteExpression(MaxBytesToFill)) @@ -1158,7 +1162,7 @@ bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) { } } - Lexer.Lex(); + Lex(); if (!HasFillExpr) { // FIXME: Sometimes fill with nop. @@ -1216,11 +1220,11 @@ bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) { if (Lexer.isNot(AsmToken::Comma)) return TokError("unexpected token in directive"); - Lexer.Lex(); + Lex(); } } - Lexer.Lex(); + Lex(); return false; } @@ -1236,7 +1240,7 @@ bool AsmParser::ParseDirectiveDarwinSymbolDesc() { if (Lexer.isNot(AsmToken::Comma)) return TokError("unexpected token in '.desc' directive"); - Lexer.Lex(); + Lex(); SMLoc DescLoc = Lexer.getLoc(); int64_t DescValue; @@ -1246,7 +1250,7 @@ bool AsmParser::ParseDirectiveDarwinSymbolDesc() { if (Lexer.isNot(AsmToken::EndOfStatement)) return TokError("unexpected token in '.desc' directive"); - Lexer.Lex(); + Lex(); // Set the n_desc field of this Symbol to this DescValue Out.EmitSymbolDesc(Sym, DescValue); @@ -1267,7 +1271,7 @@ bool AsmParser::ParseDirectiveComm(bool IsLocal) { if (Lexer.isNot(AsmToken::Comma)) return TokError("unexpected token in directive"); - Lexer.Lex(); + Lex(); int64_t Size; SMLoc SizeLoc = Lexer.getLoc(); @@ -1277,7 +1281,7 @@ bool AsmParser::ParseDirectiveComm(bool IsLocal) { int64_t Pow2Alignment = 0; SMLoc Pow2AlignmentLoc; if (Lexer.is(AsmToken::Comma)) { - Lexer.Lex(); + Lex(); Pow2AlignmentLoc = Lexer.getLoc(); if (ParseAbsoluteExpression(Pow2Alignment)) return true; @@ -1293,7 +1297,7 @@ bool AsmParser::ParseDirectiveComm(bool IsLocal) { if (Lexer.isNot(AsmToken::EndOfStatement)) return TokError("unexpected token in '.comm' or '.lcomm' directive"); - Lexer.Lex(); + Lex(); // NOTE: a size of zero for a .comm should create a undefined symbol // but a size of .lcomm creates a bss symbol of size zero. @@ -1334,17 +1338,17 @@ bool AsmParser::ParseDirectiveDarwinZerofill() { if (Lexer.isNot(AsmToken::Identifier)) return TokError("expected segment name after '.zerofill' directive"); StringRef Segment = Lexer.getTok().getString(); - Lexer.Lex(); + Lex(); if (Lexer.isNot(AsmToken::Comma)) return TokError("unexpected token in directive"); - Lexer.Lex(); + Lex(); if (Lexer.isNot(AsmToken::Identifier)) return TokError("expected section name after comma in '.zerofill' " "directive"); StringRef Section = Lexer.getTok().getString(); - Lexer.Lex(); + Lex(); // If this is the end of the line all that was wanted was to create the // the section but with no symbol. @@ -1358,7 +1362,7 @@ bool AsmParser::ParseDirectiveDarwinZerofill() { if (Lexer.isNot(AsmToken::Comma)) return TokError("unexpected token in directive"); - Lexer.Lex(); + Lex(); if (Lexer.isNot(AsmToken::Identifier)) return TokError("expected identifier in directive"); @@ -1366,11 +1370,11 @@ bool AsmParser::ParseDirectiveDarwinZerofill() { // handle the identifier as the key symbol. SMLoc IDLoc = Lexer.getLoc(); MCSymbol *Sym = CreateSymbol(Lexer.getTok().getString()); - Lexer.Lex(); + Lex(); if (Lexer.isNot(AsmToken::Comma)) return TokError("unexpected token in directive"); - Lexer.Lex(); + Lex(); int64_t Size; SMLoc SizeLoc = Lexer.getLoc(); @@ -1380,7 +1384,7 @@ bool AsmParser::ParseDirectiveDarwinZerofill() { int64_t Pow2Alignment = 0; SMLoc Pow2AlignmentLoc; if (Lexer.is(AsmToken::Comma)) { - Lexer.Lex(); + Lex(); Pow2AlignmentLoc = Lexer.getLoc(); if (ParseAbsoluteExpression(Pow2Alignment)) return true; @@ -1389,7 +1393,7 @@ bool AsmParser::ParseDirectiveDarwinZerofill() { if (Lexer.isNot(AsmToken::EndOfStatement)) return TokError("unexpected token in '.zerofill' directive"); - Lexer.Lex(); + Lex(); if (Size < 0) return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less " @@ -1422,7 +1426,7 @@ bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() { if (Lexer.isNot(AsmToken::EndOfStatement)) return TokError("unexpected token in '.subsections_via_symbols' directive"); - Lexer.Lex(); + Lex(); Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols); @@ -1442,13 +1446,13 @@ bool AsmParser::ParseDirectiveAbort() { Str = Lexer.getTok().getString(); - Lexer.Lex(); + Lex(); } if (Lexer.isNot(AsmToken::EndOfStatement)) return TokError("unexpected token in '.abort' directive"); - Lexer.Lex(); + Lex(); // FIXME: Handle here. if (Str.empty()) @@ -1471,7 +1475,7 @@ bool AsmParser::ParseDirectiveDarwinLsym() { if (Lexer.isNot(AsmToken::Comma)) return TokError("unexpected token in '.lsym' directive"); - Lexer.Lex(); + Lex(); const MCExpr *Value; SMLoc StartLoc = Lexer.getLoc(); @@ -1481,7 +1485,7 @@ bool AsmParser::ParseDirectiveDarwinLsym() { if (Lexer.isNot(AsmToken::EndOfStatement)) return TokError("unexpected token in '.lsym' directive"); - Lexer.Lex(); + Lex(); // We don't currently support this directive. // @@ -1498,7 +1502,7 @@ bool AsmParser::ParseDirectiveInclude() { std::string Filename = Lexer.getTok().getString(); SMLoc IncludeLoc = Lexer.getLoc(); - Lexer.Lex(); + Lex(); if (Lexer.isNot(AsmToken::EndOfStatement)) return TokError("unexpected token in '.include' directive"); @@ -1524,12 +1528,12 @@ bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) { if (Lexer.isNot(AsmToken::String)) return TokError("expected string in '.dump' or '.load' directive"); - Lexer.Lex(); + Lex(); if (Lexer.isNot(AsmToken::EndOfStatement)) return TokError("unexpected token in '.dump' or '.load' directive"); - Lexer.Lex(); + Lex(); // FIXME: If/when .dump and .load are implemented they will be done in the // the assembly parser and not have any need for an MCStreamer API. @@ -1545,7 +1549,7 @@ bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) { /// ::= .if expression bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) { // Consume the identifier that was the .if directive - Lexer.Lex(); + Lex(); TheCondStack.push_back(TheCondState); TheCondState.TheCond = AsmCond::IfCond; @@ -1560,7 +1564,7 @@ bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) { if (Lexer.isNot(AsmToken::EndOfStatement)) return TokError("unexpected token in '.if' directive"); - Lexer.Lex(); + Lex(); TheCondState.CondMet = ExprValue; TheCondState.Ignore = !TheCondState.CondMet; @@ -1579,7 +1583,7 @@ bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) { TheCondState.TheCond = AsmCond::ElseIfCond; // Consume the identifier that was the .elseif directive - Lexer.Lex(); + Lex(); bool LastIgnoreState = false; if (!TheCondStack.empty()) @@ -1596,7 +1600,7 @@ bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) { if (Lexer.isNot(AsmToken::EndOfStatement)) return TokError("unexpected token in '.elseif' directive"); - Lexer.Lex(); + Lex(); TheCondState.CondMet = ExprValue; TheCondState.Ignore = !TheCondState.CondMet; } @@ -1608,12 +1612,12 @@ bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) { /// ::= .else bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) { // Consume the identifier that was the .else directive - Lexer.Lex(); + Lex(); if (Lexer.isNot(AsmToken::EndOfStatement)) return TokError("unexpected token in '.else' directive"); - Lexer.Lex(); + Lex(); if (TheCondState.TheCond != AsmCond::IfCond && TheCondState.TheCond != AsmCond::ElseIfCond) @@ -1635,12 +1639,12 @@ bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) { /// ::= .endif bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) { // Consume the identifier that was the .endif directive - Lexer.Lex(); + Lex(); if (Lexer.isNot(AsmToken::EndOfStatement)) return TokError("unexpected token in '.endif' directive"); - Lexer.Lex(); + Lex(); if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty()) @@ -1661,7 +1665,7 @@ bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) { int64_t FileNumber = -1; if (Lexer.is(AsmToken::Integer)) { FileNumber = Lexer.getTok().getIntVal(); - Lexer.Lex(); + Lex(); if (FileNumber < 1) return TokError("file number less than one"); @@ -1671,7 +1675,7 @@ bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) { return TokError("unexpected token in '.file' directive"); StringRef ATTRIBUTE_UNUSED FileName = Lexer.getTok().getString(); - Lexer.Lex(); + Lex(); if (Lexer.isNot(AsmToken::EndOfStatement)) return TokError("unexpected token in '.file' directive"); @@ -1690,7 +1694,7 @@ bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) { int64_t LineNumber = Lexer.getTok().getIntVal(); (void) LineNumber; - Lexer.Lex(); + Lex(); // FIXME: Do something with the .line. } @@ -1713,14 +1717,14 @@ bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) { (void) FileNumber; // FIXME: Validate file. - Lexer.Lex(); + Lex(); if (Lexer.isNot(AsmToken::EndOfStatement)) { if (Lexer.isNot(AsmToken::Integer)) return TokError("unexpected token in '.loc' directive"); int64_t Param2 = Lexer.getTok().getIntVal(); (void) Param2; - Lexer.Lex(); + Lex(); if (Lexer.isNot(AsmToken::EndOfStatement)) { if (Lexer.isNot(AsmToken::Integer)) @@ -1728,7 +1732,7 @@ bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) { int64_t Param3 = Lexer.getTok().getIntVal(); (void) Param3; - Lexer.Lex(); + Lex(); // FIXME: Do something with the .loc. } diff --git a/llvm/tools/llvm-mc/AsmParser.h b/llvm/tools/llvm-mc/AsmParser.h index 5aefb5b622d0..f1c330c132c3 100644 --- a/llvm/tools/llvm-mc/AsmParser.h +++ b/llvm/tools/llvm-mc/AsmParser.h @@ -25,6 +25,7 @@ namespace llvm { class AsmCond; +class AsmToken; class MCContext; class MCExpr; class MCInst; @@ -79,6 +80,8 @@ public: virtual void Warning(SMLoc L, const Twine &Meg); virtual bool Error(SMLoc L, const Twine &Msg); + const AsmToken &Lex(); + bool ParseExpression(const MCExpr *&Res); virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc); virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);