diff options
author | Daniel Dunbar <daniel@zuster.org> | 2009-08-01 00:48:30 +0000 |
---|---|---|
committer | Daniel Dunbar <daniel@zuster.org> | 2009-08-01 00:48:30 +0000 |
commit | b944d14a91a5d59b8b0a53332e0c36503511e095 (patch) | |
tree | ba0cb3b7c9488e21c6e76090bfee87ee268547cc /tools | |
parent | 4021660154c087c5a57a8018569ca41951dcc4bb (diff) | |
download | external_llvm-b944d14a91a5d59b8b0a53332e0c36503511e095.zip external_llvm-b944d14a91a5d59b8b0a53332e0c36503511e095.tar.gz external_llvm-b944d14a91a5d59b8b0a53332e0c36503511e095.tar.bz2 |
llvm-mc: More quoted identifier support.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77761 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools')
-rw-r--r-- | tools/llvm-mc/AsmParser.cpp | 102 | ||||
-rw-r--r-- | tools/llvm-mc/AsmParser.h | 4 |
2 files changed, 63 insertions, 43 deletions
diff --git a/tools/llvm-mc/AsmParser.cpp b/tools/llvm-mc/AsmParser.cpp index 15a7f33..19eaf35 100644 --- a/tools/llvm-mc/AsmParser.cpp +++ b/tools/llvm-mc/AsmParser.cpp @@ -303,25 +303,22 @@ bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) { /// ::= Label* Directive ...Operands... EndOfStatement /// ::= Label* Identifier OperandList* EndOfStatement bool AsmParser::ParseStatement() { - switch (Lexer.getKind()) { - default: - return TokError("unexpected token at start of statement"); - case AsmToken::EndOfStatement: + if (Lexer.is(AsmToken::EndOfStatement)) { Lexer.Lex(); return false; - case AsmToken::Identifier: - case AsmToken::String: - break; - // TODO: Recurse on local labels etc. } - - // If we have an identifier, handle it as the key symbol. + + // Statements always start with an identifier. AsmToken ID = Lexer.getTok(); SMLoc IDLoc = ID.getLoc(); - StringRef IDVal = ID.getIdentifier(); - - // Consume the identifier, see what is after it. - switch (Lexer.Lex().getKind()) { + StringRef IDVal; + if (ParseIdentifier(IDVal)) + return TokError("unexpected token at start of statement"); + + // FIXME: Recurse on local labels? + + // See what kind of statement we have. + switch (Lexer.getKind()) { case AsmToken::Colon: { // identifier ':' -> Label. Lexer.Lex(); @@ -603,15 +600,30 @@ bool AsmParser::ParseAssignment(const StringRef &Name, bool IsDotSet) { return false; } +/// ParseIdentifier: +/// ::= identifier +/// ::= string +bool AsmParser::ParseIdentifier(StringRef &Res) { + if (Lexer.isNot(AsmToken::Identifier) && + Lexer.isNot(AsmToken::String)) + return true; + + Res = Lexer.getTok().getIdentifier(); + + Lexer.Lex(); // Consume the identifier token. + + return false; +} + /// ParseDirectiveSet: /// ::= .set identifier ',' expression bool AsmParser::ParseDirectiveSet() { - if (Lexer.isNot(AsmToken::Identifier)) - return TokError("expected identifier after '.set' directive"); + StringRef Name; - StringRef Name = Lexer.getTok().getString(); + if (ParseIdentifier(Name)) + return TokError("expected identifier after '.set' directive"); - if (Lexer.Lex().isNot(AsmToken::Comma)) + if (Lexer.isNot(AsmToken::Comma)) return TokError("unexpected token in '.set'"); Lexer.Lex(); @@ -623,21 +635,24 @@ bool AsmParser::ParseDirectiveSet() { /// FIXME: This should actually parse out the segment, section, attributes and /// sizeof_stub fields. bool AsmParser::ParseDirectiveDarwinSection() { - if (Lexer.isNot(AsmToken::Identifier)) + StringRef SectionName; + + if (ParseIdentifier(SectionName)) return TokError("expected identifier after '.section' directive"); - std::string Section = Lexer.getTok().getString(); - Lexer.Lex(); - + std::string Section = SectionName; + + // FIXME: This doesn't work, we lose quoting on things + // Accept a comma separated list of modifiers. while (Lexer.is(AsmToken::Comma)) { - Lexer.Lex(); - - if (Lexer.isNot(AsmToken::Identifier)) + Lexer.Lex(); // Consume the comma. + + StringRef ModifierName; + if (ParseIdentifier(ModifierName)) return TokError("expected identifier in '.section' directive"); Section += ','; - Section += Lexer.getTok().getString().str(); - Lexer.Lex(); + Section += ModifierName; } if (Lexer.isNot(AsmToken::EndOfStatement)) @@ -910,11 +925,12 @@ bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) { bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) { if (Lexer.isNot(AsmToken::EndOfStatement)) { for (;;) { - if (Lexer.isNot(AsmToken::Identifier)) + StringRef Name; + + if (ParseIdentifier(Name)) return TokError("expected identifier in directive"); - MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString()); - Lexer.Lex(); + MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name); // If this is use of an undefined symbol then mark it external. if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym)) @@ -938,13 +954,12 @@ bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) { /// ParseDirectiveDarwinSymbolDesc /// ::= .desc identifier , expression bool AsmParser::ParseDirectiveDarwinSymbolDesc() { - if (Lexer.isNot(AsmToken::Identifier)) + StringRef Name; + if (ParseIdentifier(Name)) return TokError("expected identifier in directive"); - // handle the identifier as the key symbol. - SMLoc IDLoc = Lexer.getLoc(); - MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString()); - Lexer.Lex(); + // Handle the identifier as the key symbol. + MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name); if (Lexer.isNot(AsmToken::Comma)) return TokError("unexpected token in '.desc' directive"); @@ -969,13 +984,13 @@ bool AsmParser::ParseDirectiveDarwinSymbolDesc() { /// ParseDirectiveComm /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ] bool AsmParser::ParseDirectiveComm(bool IsLocal) { - if (Lexer.isNot(AsmToken::Identifier)) + SMLoc IDLoc = Lexer.getLoc(); + StringRef Name; + if (ParseIdentifier(Name)) return TokError("expected identifier in directive"); // Handle the identifier as the key symbol. - SMLoc IDLoc = Lexer.getLoc(); - MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString()); - Lexer.Lex(); + MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name); if (Lexer.isNot(AsmToken::Comma)) return TokError("unexpected token in directive"); @@ -1027,6 +1042,8 @@ bool AsmParser::ParseDirectiveComm(bool IsLocal) { /// ::= .zerofill segname , sectname [, identifier , size_expression [ /// , align_expression ]] bool AsmParser::ParseDirectiveDarwinZerofill() { + // FIXME: Handle quoted names here. + if (Lexer.isNot(AsmToken::Identifier)) return TokError("expected segment name after '.zerofill' directive"); std::string Section = Lexer.getTok().getString(); @@ -1171,13 +1188,12 @@ bool AsmParser::ParseDirectiveAbort() { /// ParseDirectiveLsym /// ::= .lsym identifier , expression bool AsmParser::ParseDirectiveDarwinLsym() { - if (Lexer.isNot(AsmToken::Identifier)) + StringRef Name; + if (ParseIdentifier(Name)) return TokError("expected identifier in directive"); // Handle the identifier as the key symbol. - SMLoc IDLoc = Lexer.getLoc(); - MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString()); - Lexer.Lex(); + MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name); if (Lexer.isNot(AsmToken::Comma)) return TokError("unexpected token in '.lsym' directive"); diff --git a/tools/llvm-mc/AsmParser.h b/tools/llvm-mc/AsmParser.h index 2f61461..7a4c832 100644 --- a/tools/llvm-mc/AsmParser.h +++ b/tools/llvm-mc/AsmParser.h @@ -84,6 +84,10 @@ private: bool ParsePrimaryExpr(AsmExpr *&Res); bool ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res); bool ParseParenExpr(AsmExpr *&Res); + + /// ParseIdentifier - Parse an identifier or string (as a quoted identifier) + /// and set \arg Res to the identifier contents. + bool ParseIdentifier(StringRef &Res); // Directive Parsing. bool ParseDirectiveDarwinSection(); // Darwin specific ".section". |