From: Seo Sanghyeon Date: Mon, 24 Dec 2007 01:52:34 +0000 (+0000) Subject: Implement -emit-llvm-bc option X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fe947ad3624ad45bc5ef9fd36a3f9ff00efdb1fd;p=clang Implement -emit-llvm-bc option git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@45339 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/Driver/ASTConsumers.cpp b/Driver/ASTConsumers.cpp index 712bf55027..c3a6b67562 100644 --- a/Driver/ASTConsumers.cpp +++ b/Driver/ASTConsumers.cpp @@ -21,6 +21,7 @@ #include "clang/Analysis/Analyses/LiveVariables.h" #include "clang/Analysis/LocalCheckers.h" #include "llvm/Support/Streams.h" +#include using namespace clang; @@ -554,17 +555,19 @@ ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) { #include "llvm/Module.h" #include "llvm/Target/TargetData.h" #include "llvm/Target/TargetMachine.h" +#include "llvm/Bitcode/ReaderWriter.h" namespace { - class LLVMEmitter : public ASTConsumer { + class CodeGenerator : public ASTConsumer { Diagnostic &Diags; - llvm::Module *M; const llvm::TargetData *TD; ASTContext *Ctx; const LangOptions &Features; + protected: + llvm::Module *M; CodeGen::CodeGenModule *Builder; public: - LLVMEmitter(Diagnostic &diags, const LangOptions &LO) + CodeGenerator(Diagnostic &diags, const LangOptions &LO) : Diags(diags) , Features(LO) {} virtual void Initialize(ASTContext &Context) { @@ -593,7 +596,15 @@ namespace { // << D->getName() << "'\n"; } } - + }; +} + +namespace { + class LLVMEmitter : public CodeGenerator { + public: + LLVMEmitter(Diagnostic &diags, const LangOptions &LO) + : CodeGenerator(diags,LO) {} + ~LLVMEmitter() { CodeGen::Terminate(Builder); @@ -601,14 +612,47 @@ namespace { M->print(llvm::cout.stream()); delete M; } - }; -} // end anonymous namespace + }; +} ASTConsumer *clang::CreateLLVMEmitter(Diagnostic &Diags, const LangOptions &Features) { return new LLVMEmitter(Diags, Features); } +namespace { + class BCWriter : public CodeGenerator { + public: + std::ostream& Out; + + BCWriter(std::ostream* out, Diagnostic &diags, const LangOptions &LO) + : CodeGenerator(diags,LO) + , Out(*out) {} + + ~BCWriter() { + CodeGen::Terminate(Builder); + llvm::WriteBitcodeToFile(M, Out); + delete M; + } + }; +} + +ASTConsumer *clang::CreateBCWriter(const std::string& InFile, + const std::string& OutputFile, + Diagnostic &Diags, + const LangOptions &Features) { + std::string FileName = OutputFile; + if (!OutputFile.size()) { + llvm::sys::Path Path(InFile); + Path.eraseSuffix(); + Path.appendSuffix("bc"); + FileName = Path.toString(); + } + + std::ofstream *Out = new std::ofstream(FileName.c_str()); + return new BCWriter(Out, Diags, Features); +} + //===----------------------------------------------------------------------===// // AST Serializer diff --git a/Driver/ASTConsumers.h b/Driver/ASTConsumers.h index ab8880d8d3..0412c7a962 100644 --- a/Driver/ASTConsumers.h +++ b/Driver/ASTConsumers.h @@ -41,6 +41,11 @@ ASTConsumer *CreateUnitValsChecker(Diagnostic &Diags); ASTConsumer *CreateLLVMEmitter(Diagnostic &Diags, const LangOptions &Features); +ASTConsumer *CreateBCWriter(const std::string& InFile, + const std::string& OutFile, + Diagnostic &Diags, + const LangOptions &LOpts); + ASTConsumer *CreateCodeRewriterTest(Diagnostic &Diags); ASTConsumer *CreateSerializationTest(Diagnostic &Diags, diff --git a/Driver/clang.cpp b/Driver/clang.cpp index 91bd117200..ddfa8ef4ba 100644 --- a/Driver/clang.cpp +++ b/Driver/clang.cpp @@ -55,6 +55,7 @@ Stats("stats", llvm::cl::desc("Print performance metrics and statistics")); enum ProgActions { RewriteTest, // Rewriter testing stuff. EmitLLVM, // Emit a .ll file. + EmitBC, // Emit a .bc file. SerializeAST, // Emit a .ast file. ASTPrint, // Parse ASTs and print them. ASTDump, // Parse ASTs and dump them. @@ -110,6 +111,8 @@ ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore, "Run prototype serializtion code."), clEnumValN(EmitLLVM, "emit-llvm", "Build ASTs then convert to LLVM, emit .ll file"), + clEnumValN(EmitBC, "emit-llvm-bc", + "Build ASTs then convert to LLVM, emit .bc file"), clEnumValN(SerializeAST, "serialize", "Build ASTs and emit .ast file"), clEnumValN(RewriteTest, "rewrite-test", @@ -925,7 +928,10 @@ static ASTConsumer* CreateASTConsumer(const std::string& InFile, case EmitLLVM: return CreateLLVMEmitter(Diag, LangOpts); - + + case EmitBC: + return CreateBCWriter(InFile, OutputFile, Diag, LangOpts); + case SerializeAST: // FIXME: Allow user to tailor where the file is written. return CreateASTSerializer(InFile, OutputFile, Diag, LangOpts);