llvm-mc: Move AsmLexer::getCurStrVal to StringRef based API.

- My DFS traversal of LLVM is, at least for now, nearly complete! :)

llvm-svn: 77258
This commit is contained in:
Daniel Dunbar 2009-07-27 21:49:56 +00:00
parent 1aefb721fc
commit 52d03b252e
8 changed files with 115 additions and 107 deletions

View File

@ -22,6 +22,7 @@ namespace llvm {
class MCInst;
class MCSection;
class MCSymbol;
class StringRef;
class raw_ostream;
/// MCStreamer - Streaming machine code generation interface. This interface
@ -166,12 +167,11 @@ namespace llvm {
/// @name Generating Data
/// @{
/// EmitBytes - Emit @param Length bytes starting at @param Data into the
/// output.
/// EmitBytes - Emit the bytes in @param Data into the output.
///
/// This is used to implement assembler directives such as .byte, .ascii,
/// etc.
virtual void EmitBytes(const char *Data, unsigned Length) = 0;
virtual void EmitBytes(const StringRef &Data) = 0;
/// EmitValue - Emit the expression @param Value into the output as a native
/// integer of the given @param Size bytes.

View File

@ -13,6 +13,7 @@
namespace llvm {
class MCAsmParser;
class MCInst;
class StringRef;
class Target;
/// TargetAsmParser - Generic interface to target specific assembly parsers.
@ -42,7 +43,7 @@ public:
/// \param Name - The instruction name.
/// \param Inst [out] - On success, the parsed instruction.
/// \return True on failure.
virtual bool ParseInstruction(MCAsmParser &AP, const char *Name,
virtual bool ParseInstruction(MCAsmParser &AP, const StringRef &Name,
MCInst &Inst) = 0;
};

View File

@ -57,7 +57,7 @@ namespace {
virtual void AbortAssembly(const char *AbortReason = NULL);
virtual void EmitBytes(const char *Data, unsigned Length);
virtual void EmitBytes(const StringRef &Data);
virtual void EmitValue(const MCValue &Value, unsigned Size);
@ -208,9 +208,9 @@ void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
OS << '\n';
}
void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
void MCAsmStreamer::EmitBytes(const StringRef &Data) {
assert(CurSection && "Cannot emit contents before setting section!");
for (unsigned i = 0; i != Length; ++i)
for (unsigned i = 0, e = Data.size(); i != e; ++i)
OS << ".byte " << (unsigned) Data[i] << '\n';
}

View File

@ -21,14 +21,14 @@ namespace {
class X86ATTAsmParser : public TargetAsmParser {
bool ParseOperand(X86Operand &Op);
bool MatchInstruction(const char *Name,
bool MatchInstruction(const StringRef &Name,
llvm::SmallVector<X86Operand, 3> &Operands,
MCInst &Inst);
public:
explicit X86ATTAsmParser(const Target &);
virtual bool ParseInstruction(MCAsmParser &AP, const char *Name,
virtual bool ParseInstruction(MCAsmParser &AP, const StringRef &Name,
MCInst &Inst);
};
}
@ -43,13 +43,13 @@ bool X86ATTAsmParser::ParseOperand(X86Operand &Op) {
}
bool
X86ATTAsmParser::MatchInstruction(const char *Name,
X86ATTAsmParser::MatchInstruction(const StringRef &Name,
llvm::SmallVector<X86Operand, 3> &Operands,
MCInst &Inst) {
return false;
}
bool X86ATTAsmParser::ParseInstruction(MCAsmParser &AP, const char *Name,
bool X86ATTAsmParser::ParseInstruction(MCAsmParser &AP, const StringRef &Name,
MCInst &Inst) {
llvm::SmallVector<X86Operand, 3> Operands;

View File

@ -14,6 +14,7 @@
#ifndef ASMLEXER_H
#define ASMLEXER_H
#include "llvm/ADT/StringRef.h"
#include "llvm/MC/MCAsmLexer.h"
#include "llvm/Support/DataTypes.h"
#include <string>
@ -84,8 +85,13 @@ public:
asmtok::TokKind getKind() const { return CurKind; }
bool is(asmtok::TokKind K) const { return CurKind == K; }
bool isNot(asmtok::TokKind K) const { return CurKind != K; }
const char *getCurStrVal() const {
/// getCurStrVal - Get the string for the current token, this includes all
/// characters (for example, the quotes on strings) in the token.
///
/// The returned StringRef points into the source manager's memory buffer, and
/// is safe to store across calls to Lex().
StringRef getCurStrVal() const {
assert((CurKind == asmtok::Identifier || CurKind == asmtok::Register ||
CurKind == asmtok::String) &&
"This token doesn't have a string value");

View File

@ -313,7 +313,7 @@ bool AsmParser::ParseStatement() {
// If we have an identifier, handle it as the key symbol.
SMLoc IDLoc = Lexer.getLoc();
const char *IDVal = Lexer.getCurStrVal();
StringRef IDVal = Lexer.getCurStrVal();
// Consume the identifier, see what is after it.
switch (Lexer.Lex()) {
@ -353,194 +353,194 @@ bool AsmParser::ParseStatement() {
// Otherwise, we have a normal instruction or directive.
if (IDVal[0] == '.') {
// FIXME: This should be driven based on a hash lookup and callback.
if (!strcmp(IDVal, ".section"))
if (IDVal == ".section")
return ParseDirectiveDarwinSection();
if (!strcmp(IDVal, ".text"))
if (IDVal == ".text")
// FIXME: This changes behavior based on the -static flag to the
// assembler.
return ParseDirectiveSectionSwitch("__TEXT,__text",
"regular,pure_instructions");
if (!strcmp(IDVal, ".const"))
if (IDVal == ".const")
return ParseDirectiveSectionSwitch("__TEXT,__const");
if (!strcmp(IDVal, ".static_const"))
if (IDVal == ".static_const")
return ParseDirectiveSectionSwitch("__TEXT,__static_const");
if (!strcmp(IDVal, ".cstring"))
if (IDVal == ".cstring")
return ParseDirectiveSectionSwitch("__TEXT,__cstring",
"cstring_literals");
if (!strcmp(IDVal, ".literal4"))
if (IDVal == ".literal4")
return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
if (!strcmp(IDVal, ".literal8"))
if (IDVal == ".literal8")
return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
if (!strcmp(IDVal, ".literal16"))
if (IDVal == ".literal16")
return ParseDirectiveSectionSwitch("__TEXT,__literal16",
"16byte_literals");
if (!strcmp(IDVal, ".constructor"))
if (IDVal == ".constructor")
return ParseDirectiveSectionSwitch("__TEXT,__constructor");
if (!strcmp(IDVal, ".destructor"))
if (IDVal == ".destructor")
return ParseDirectiveSectionSwitch("__TEXT,__destructor");
if (!strcmp(IDVal, ".fvmlib_init0"))
if (IDVal == ".fvmlib_init0")
return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
if (!strcmp(IDVal, ".fvmlib_init1"))
if (IDVal == ".fvmlib_init1")
return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
if (!strcmp(IDVal, ".symbol_stub")) // FIXME: Different on PPC.
if (IDVal == ".symbol_stub") // FIXME: Different on PPC.
return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
"self_modifying_code+pure_instructions,5");
// FIXME: .picsymbol_stub on PPC.
if (!strcmp(IDVal, ".data"))
if (IDVal == ".data")
return ParseDirectiveSectionSwitch("__DATA,__data");
if (!strcmp(IDVal, ".static_data"))
if (IDVal == ".static_data")
return ParseDirectiveSectionSwitch("__DATA,__static_data");
if (!strcmp(IDVal, ".non_lazy_symbol_pointer"))
if (IDVal == ".non_lazy_symbol_pointer")
return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
"non_lazy_symbol_pointers");
if (!strcmp(IDVal, ".lazy_symbol_pointer"))
if (IDVal == ".lazy_symbol_pointer")
return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
"lazy_symbol_pointers");
if (!strcmp(IDVal, ".dyld"))
if (IDVal == ".dyld")
return ParseDirectiveSectionSwitch("__DATA,__dyld");
if (!strcmp(IDVal, ".mod_init_func"))
if (IDVal == ".mod_init_func")
return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
"mod_init_funcs");
if (!strcmp(IDVal, ".mod_term_func"))
if (IDVal == ".mod_term_func")
return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
"mod_term_funcs");
if (!strcmp(IDVal, ".const_data"))
if (IDVal == ".const_data")
return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
// FIXME: Verify attributes on sections.
if (!strcmp(IDVal, ".objc_class"))
if (IDVal == ".objc_class")
return ParseDirectiveSectionSwitch("__OBJC,__class");
if (!strcmp(IDVal, ".objc_meta_class"))
if (IDVal == ".objc_meta_class")
return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
if (!strcmp(IDVal, ".objc_cat_cls_meth"))
if (IDVal == ".objc_cat_cls_meth")
return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
if (!strcmp(IDVal, ".objc_cat_inst_meth"))
if (IDVal == ".objc_cat_inst_meth")
return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
if (!strcmp(IDVal, ".objc_protocol"))
if (IDVal == ".objc_protocol")
return ParseDirectiveSectionSwitch("__OBJC,__protocol");
if (!strcmp(IDVal, ".objc_string_object"))
if (IDVal == ".objc_string_object")
return ParseDirectiveSectionSwitch("__OBJC,__string_object");
if (!strcmp(IDVal, ".objc_cls_meth"))
if (IDVal == ".objc_cls_meth")
return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
if (!strcmp(IDVal, ".objc_inst_meth"))
if (IDVal == ".objc_inst_meth")
return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
if (!strcmp(IDVal, ".objc_cls_refs"))
if (IDVal == ".objc_cls_refs")
return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
if (!strcmp(IDVal, ".objc_message_refs"))
if (IDVal == ".objc_message_refs")
return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
if (!strcmp(IDVal, ".objc_symbols"))
if (IDVal == ".objc_symbols")
return ParseDirectiveSectionSwitch("__OBJC,__symbols");
if (!strcmp(IDVal, ".objc_category"))
if (IDVal == ".objc_category")
return ParseDirectiveSectionSwitch("__OBJC,__category");
if (!strcmp(IDVal, ".objc_class_vars"))
if (IDVal == ".objc_class_vars")
return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
if (!strcmp(IDVal, ".objc_instance_vars"))
if (IDVal == ".objc_instance_vars")
return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
if (!strcmp(IDVal, ".objc_module_info"))
if (IDVal == ".objc_module_info")
return ParseDirectiveSectionSwitch("__OBJC,__module_info");
if (!strcmp(IDVal, ".objc_class_names"))
if (IDVal == ".objc_class_names")
return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
if (!strcmp(IDVal, ".objc_meth_var_types"))
if (IDVal == ".objc_meth_var_types")
return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
if (!strcmp(IDVal, ".objc_meth_var_names"))
if (IDVal == ".objc_meth_var_names")
return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
if (!strcmp(IDVal, ".objc_selector_strs"))
if (IDVal == ".objc_selector_strs")
return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
// Assembler features
if (!strcmp(IDVal, ".set"))
if (IDVal == ".set")
return ParseDirectiveSet();
// Data directives
if (!strcmp(IDVal, ".ascii"))
if (IDVal == ".ascii")
return ParseDirectiveAscii(false);
if (!strcmp(IDVal, ".asciz"))
if (IDVal == ".asciz")
return ParseDirectiveAscii(true);
// FIXME: Target hooks for size? Also for "word", "hword".
if (!strcmp(IDVal, ".byte"))
if (IDVal == ".byte")
return ParseDirectiveValue(1);
if (!strcmp(IDVal, ".short"))
if (IDVal == ".short")
return ParseDirectiveValue(2);
if (!strcmp(IDVal, ".long"))
if (IDVal == ".long")
return ParseDirectiveValue(4);
if (!strcmp(IDVal, ".quad"))
if (IDVal == ".quad")
return ParseDirectiveValue(8);
// FIXME: Target hooks for IsPow2.
if (!strcmp(IDVal, ".align"))
if (IDVal == ".align")
return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
if (!strcmp(IDVal, ".align32"))
if (IDVal == ".align32")
return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
if (!strcmp(IDVal, ".balign"))
if (IDVal == ".balign")
return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
if (!strcmp(IDVal, ".balignw"))
if (IDVal == ".balignw")
return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
if (!strcmp(IDVal, ".balignl"))
if (IDVal == ".balignl")
return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
if (!strcmp(IDVal, ".p2align"))
if (IDVal == ".p2align")
return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
if (!strcmp(IDVal, ".p2alignw"))
if (IDVal == ".p2alignw")
return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
if (!strcmp(IDVal, ".p2alignl"))
if (IDVal == ".p2alignl")
return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
if (!strcmp(IDVal, ".org"))
if (IDVal == ".org")
return ParseDirectiveOrg();
if (!strcmp(IDVal, ".fill"))
if (IDVal == ".fill")
return ParseDirectiveFill();
if (!strcmp(IDVal, ".space"))
if (IDVal == ".space")
return ParseDirectiveSpace();
// Symbol attribute directives
if (!strcmp(IDVal, ".globl") || !strcmp(IDVal, ".global"))
if (IDVal == ".globl" || IDVal == ".global")
return ParseDirectiveSymbolAttribute(MCStreamer::Global);
if (!strcmp(IDVal, ".hidden"))
if (IDVal == ".hidden")
return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
if (!strcmp(IDVal, ".indirect_symbol"))
if (IDVal == ".indirect_symbol")
return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
if (!strcmp(IDVal, ".internal"))
if (IDVal == ".internal")
return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
if (!strcmp(IDVal, ".lazy_reference"))
if (IDVal == ".lazy_reference")
return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
if (!strcmp(IDVal, ".no_dead_strip"))
if (IDVal == ".no_dead_strip")
return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
if (!strcmp(IDVal, ".private_extern"))
if (IDVal == ".private_extern")
return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
if (!strcmp(IDVal, ".protected"))
if (IDVal == ".protected")
return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
if (!strcmp(IDVal, ".reference"))
if (IDVal == ".reference")
return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
if (!strcmp(IDVal, ".weak"))
if (IDVal == ".weak")
return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
if (!strcmp(IDVal, ".weak_definition"))
if (IDVal == ".weak_definition")
return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
if (!strcmp(IDVal, ".weak_reference"))
if (IDVal == ".weak_reference")
return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
if (!strcmp(IDVal, ".comm"))
if (IDVal == ".comm")
return ParseDirectiveComm(/*IsLocal=*/false);
if (!strcmp(IDVal, ".lcomm"))
if (IDVal == ".lcomm")
return ParseDirectiveComm(/*IsLocal=*/true);
if (!strcmp(IDVal, ".zerofill"))
if (IDVal == ".zerofill")
return ParseDirectiveDarwinZerofill();
if (!strcmp(IDVal, ".desc"))
if (IDVal == ".desc")
return ParseDirectiveDarwinSymbolDesc();
if (!strcmp(IDVal, ".lsym"))
if (IDVal == ".lsym")
return ParseDirectiveDarwinLsym();
if (!strcmp(IDVal, ".subsections_via_symbols"))
if (IDVal == ".subsections_via_symbols")
return ParseDirectiveDarwinSubsectionsViaSymbols();
if (!strcmp(IDVal, ".abort"))
if (IDVal == ".abort")
return ParseDirectiveAbort();
if (!strcmp(IDVal, ".include"))
if (IDVal == ".include")
return ParseDirectiveInclude();
if (!strcmp(IDVal, ".dump"))
if (IDVal == ".dump")
return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
if (!strcmp(IDVal, ".load"))
if (IDVal == ".load")
return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Warning(IDLoc, "ignoring directive for now");
@ -566,7 +566,7 @@ bool AsmParser::ParseStatement() {
return false;
}
bool AsmParser::ParseAssignment(const char *Name, bool IsDotSet) {
bool AsmParser::ParseAssignment(const StringRef &Name, bool IsDotSet) {
// FIXME: Use better location, we should use proper tokens.
SMLoc EqualLoc = Lexer.getLoc();
@ -605,7 +605,7 @@ bool AsmParser::ParseDirectiveSet() {
if (Lexer.isNot(asmtok::Identifier))
return TokError("expected identifier after '.set' directive");
const char *Name = Lexer.getCurStrVal();
StringRef Name = Lexer.getCurStrVal();
if (Lexer.Lex() != asmtok::Comma)
return TokError("unexpected token in '.set'");
@ -632,7 +632,7 @@ bool AsmParser::ParseDirectiveDarwinSection() {
if (Lexer.isNot(asmtok::Identifier))
return TokError("expected identifier in '.section' directive");
Section += ',';
Section += Lexer.getCurStrVal();
Section += Lexer.getCurStrVal().str();
Lexer.Lex();
}
@ -671,10 +671,10 @@ bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
// FIXME: This shouldn't use a const char* + strlen, the string could have
// embedded nulls.
// FIXME: Should have accessor for getting string contents.
const char *Str = Lexer.getCurStrVal();
Out.EmitBytes(Str + 1, strlen(Str) - 2);
StringRef Str = Lexer.getCurStrVal();
Out.EmitBytes(Str.substr(1, Str.size() - 2));
if (ZeroTerminated)
Out.EmitBytes("\0", 1);
Out.EmitBytes(StringRef("\0", 1));
Lexer.Lex();
@ -1026,7 +1026,7 @@ bool AsmParser::ParseDirectiveDarwinZerofill() {
if (Lexer.isNot(asmtok::Identifier))
return TokError("expected section name after comma in '.zerofill' "
"directive");
Section += Lexer.getCurStrVal();
Section += Lexer.getCurStrVal().str();
Lexer.Lex();
// FIXME: we will need to tell GetSection() that this is to be created with or
@ -1117,7 +1117,7 @@ bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
/// ParseDirectiveAbort
/// ::= .abort [ "abort_string" ]
bool AsmParser::ParseDirectiveAbort() {
const char *Str = NULL;
StringRef Str = "";
if (Lexer.isNot(asmtok::EndOfStatement)) {
if (Lexer.isNot(asmtok::String))
return TokError("expected string in '.abort' directive");
@ -1132,7 +1132,8 @@ bool AsmParser::ParseDirectiveAbort() {
Lexer.Lex();
Out.AbortAssembly(Str);
// FIXME: Handle here.
Out.AbortAssembly(Str.str().c_str());
return false;
}

View File

@ -58,7 +58,7 @@ private:
void EatToEndOfStatement();
bool ParseAssignment(const char *Name, bool IsDotSet);
bool ParseAssignment(const StringRef &Name, bool IsDotSet);
/// ParseExpression - Parse a general assembly expression.
///
@ -97,7 +97,7 @@ private:
bool ParseParenExpr(AsmExpr *&Res);
// X86 specific.
bool ParseX86InstOperands(const char *InstName, MCInst &Inst);
bool ParseX86InstOperands(const StringRef &InstName, MCInst &Inst);
bool ParseX86Operand(X86Operand &Op);
bool ParseX86MemOperand(X86Operand &Op);
bool ParseX86Register(X86Operand &Op);

View File

@ -235,7 +235,7 @@ bool AsmParser::ParseX86MemOperand(X86Operand &Op) {
/// MatchX86Inst - Convert a parsed instruction name and operand list into a
/// concrete instruction.
static bool MatchX86Inst(const char *Name,
static bool MatchX86Inst(const StringRef &Name,
llvm::SmallVector<AsmParser::X86Operand, 3> &Operands,
MCInst &Inst) {
return false;
@ -243,7 +243,7 @@ static bool MatchX86Inst(const char *Name,
/// ParseX86InstOperands - Parse the operands of an X86 instruction and return
/// them as the operands of an MCInst.
bool AsmParser::ParseX86InstOperands(const char *InstName, MCInst &Inst) {
bool AsmParser::ParseX86InstOperands(const StringRef &InstName, MCInst &Inst) {
llvm::SmallVector<X86Operand, 3> Operands;
if (Lexer.isNot(asmtok::EndOfStatement)) {