From a3af370dc12f6d5100da5d614ab0a62da135569a Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Mon, 20 Jul 2009 18:55:04 +0000 Subject: [PATCH] Add MCAsmParser interface. - This provides the AsmParser interface to the target specific assembly parsers. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@76453 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/MC/MCAsmParser.h | 33 +++++++++++++++++++++++ include/llvm/Target/TargetAsmParser.h | 17 ++++++++++++ lib/MC/CMakeLists.txt | 1 + lib/MC/MCAsmParser.cpp | 18 +++++++++++++ lib/Target/X86/AsmParser/X86AsmParser.cpp | 9 +++++++ tools/llvm-mc/AsmParser.cpp | 4 ++- tools/llvm-mc/AsmParser.h | 12 ++++++--- tools/llvm-mc/llvm-mc.cpp | 5 ++-- 8 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 include/llvm/MC/MCAsmParser.h create mode 100644 lib/MC/MCAsmParser.cpp diff --git a/include/llvm/MC/MCAsmParser.h b/include/llvm/MC/MCAsmParser.h new file mode 100644 index 00000000000..2379f21e5af --- /dev/null +++ b/include/llvm/MC/MCAsmParser.h @@ -0,0 +1,33 @@ +//===-- llvm/MC/MCAsmParser.h - Abstract Asm Parser Interface ---*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_MC_MCASMPARSER_H +#define LLVM_MC_MCASMPARSER_H + +namespace llvm { +class MCAsmParser; +class MCInst; +class Target; +class TargetAsmParser; + +/// MCAsmParser - Generic assembler parser interface, for use by target specific +/// assembly parsers. +class MCAsmParser { + MCAsmParser(const MCAsmParser &); // DO NOT IMPLEMENT + void operator=(const MCAsmParser &); // DO NOT IMPLEMENT +protected: // Can only create subclasses. + MCAsmParser(); + +public: + virtual ~MCAsmParser(); +}; + +} // End llvm namespace + +#endif diff --git a/include/llvm/Target/TargetAsmParser.h b/include/llvm/Target/TargetAsmParser.h index bf96bf76ee8..c179991871e 100644 --- a/include/llvm/Target/TargetAsmParser.h +++ b/include/llvm/Target/TargetAsmParser.h @@ -11,6 +11,8 @@ #define LLVM_TARGET_TARGETPARSER_H namespace llvm { +class MCAsmParser; +class MCInst; class Target; /// TargetAsmParser - Generic interface to target specific assembly parsers. @@ -27,6 +29,21 @@ public: virtual ~TargetAsmParser(); const Target &getTarget() const { return TheTarget; } + + /// ParseInstruction - Parse one assembly instruction. + /// + /// The parser is positioned following the instruction name. The target + /// specific instruction parser should parse the entire instruction and + /// construct the appropriate MCInst, or emit an error. On success, the entire + /// line should be parsed up to and including the end-of-statement token. On + /// failure, the parser is not required to read to the end of the line. + // + /// \param AP - The current parser object. + /// \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, + MCInst &Inst) = 0; }; } // End llvm namespace diff --git a/lib/MC/CMakeLists.txt b/lib/MC/CMakeLists.txt index 4e16388fec6..f0af7df959d 100644 --- a/lib/MC/CMakeLists.txt +++ b/lib/MC/CMakeLists.txt @@ -1,4 +1,5 @@ add_llvm_library(LLVMMC + MCAsmParser.cpp MCAsmStreamer.cpp MCContext.cpp MCStreamer.cpp diff --git a/lib/MC/MCAsmParser.cpp b/lib/MC/MCAsmParser.cpp new file mode 100644 index 00000000000..2287e8965d7 --- /dev/null +++ b/lib/MC/MCAsmParser.cpp @@ -0,0 +1,18 @@ +//===-- MCAsmParser.cpp - Abstract Asm Parser Interface -------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/MC/MCAsmParser.h" + +using namespace llvm; + +MCAsmParser::MCAsmParser() { +} + +MCAsmParser::~MCAsmParser() { +} diff --git a/lib/Target/X86/AsmParser/X86AsmParser.cpp b/lib/Target/X86/AsmParser/X86AsmParser.cpp index e548391e4cc..b5f6ce608dd 100644 --- a/lib/Target/X86/AsmParser/X86AsmParser.cpp +++ b/lib/Target/X86/AsmParser/X86AsmParser.cpp @@ -8,6 +8,7 @@ //===----------------------------------------------------------------------===// #include "X86.h" +#include "llvm/MC/MCAsmParser.h" #include "llvm/Target/TargetRegistry.h" #include "llvm/Target/TargetAsmParser.h" using namespace llvm; @@ -17,6 +18,9 @@ namespace { class X86ATTAsmParser : public TargetAsmParser { public: explicit X86ATTAsmParser(const Target &); + + virtual bool ParseInstruction(MCAsmParser &AP, const char *Name, + MCInst &Inst); }; } @@ -26,6 +30,11 @@ X86ATTAsmParser::X86ATTAsmParser(const Target &T) { } +bool X86ATTAsmParser::ParseInstruction(MCAsmParser &AP, const char *Name, + MCInst &Inst) { + return true; +} + namespace { TargetAsmParser *createAsmParser(const Target &T) { return new X86ATTAsmParser(T); diff --git a/tools/llvm-mc/AsmParser.cpp b/tools/llvm-mc/AsmParser.cpp index 4629cfc34b9..066879ff48d 100644 --- a/tools/llvm-mc/AsmParser.cpp +++ b/tools/llvm-mc/AsmParser.cpp @@ -20,6 +20,7 @@ #include "llvm/MC/MCSymbol.h" #include "llvm/Support/SourceMgr.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Target/TargetAsmParser.h" using namespace llvm; void AsmParser::Warning(SMLoc L, const char *Msg) { @@ -548,7 +549,8 @@ bool AsmParser::ParseStatement() { } MCInst Inst; - if (ParseX86InstOperands(IDVal, Inst)) + if (ParseX86InstOperands(IDVal, Inst) && + getTargetParser().ParseInstruction(*this, IDVal, Inst)) return true; if (Lexer.isNot(asmtok::EndOfStatement)) diff --git a/tools/llvm-mc/AsmParser.h b/tools/llvm-mc/AsmParser.h index 62aa4ef44c0..b9967585080 100644 --- a/tools/llvm-mc/AsmParser.h +++ b/tools/llvm-mc/AsmParser.h @@ -15,6 +15,7 @@ #define ASMPARSER_H #include "AsmLexer.h" +#include "llvm/MC/MCAsmParser.h" #include "llvm/MC/MCStreamer.h" namespace llvm { @@ -24,7 +25,7 @@ class MCInst; class MCStreamer; class MCValue; -class AsmParser { +class AsmParser : MCAsmParser { public: struct X86Operand; @@ -32,14 +33,19 @@ private: AsmLexer Lexer; MCContext &Ctx; MCStreamer &Out; + TargetAsmParser &TargetParser; public: - AsmParser(SourceMgr &SM, MCContext &ctx, MCStreamer &OutStr) - : Lexer(SM), Ctx(ctx), Out(OutStr) {} + AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out, + TargetAsmParser &_TargetParser) + : Lexer(_SM), Ctx(_Ctx), Out(_Out), TargetParser(_TargetParser) {} ~AsmParser() {} bool Run(); +public: + TargetAsmParser &getTargetParser() const { return TargetParser; } + private: bool ParseStatement(); diff --git a/tools/llvm-mc/llvm-mc.cpp b/tools/llvm-mc/llvm-mc.cpp index 6855a830e8a..ffc9b559f07 100644 --- a/tools/llvm-mc/llvm-mc.cpp +++ b/tools/llvm-mc/llvm-mc.cpp @@ -186,9 +186,10 @@ static int AssembleInput(const char *ProgName) { OwningPtr Str(createAsmStreamer(Ctx, outs())); // FIXME: Target hook & command line option for initial section. - Str.get()->SwitchSection(Ctx.GetSection("__TEXT,__text,regular,pure_instructions")); + Str.get()->SwitchSection(Ctx.GetSection("__TEXT,__text," + "regular,pure_instructions")); - AsmParser Parser(SrcMgr, Ctx, *Str.get()); + AsmParser Parser(SrcMgr, Ctx, *Str.get(), *TAP); return Parser.Run(); }