Attached patch implements -emit-bc option, linking LLVM bitcode writer
directly to clang so that one can produce LLVM bitcode without going
through llvm-as.
If output file is given, bitcode is written to the file. If output
file is not given, the extension is replaced by .bc.
This patch was done mainly by copying similar codes around the
codebase. Please review.
--
Seo Sanghyeon
Index: clang/Driver/ASTConsumers.cpp
===================================================================
--- clang.orig/Driver/ASTConsumers.cpp 2007-12-23 22:50:57.000000000 +0900
+++ clang/Driver/ASTConsumers.cpp 2007-12-23 23:43:59.000000000 +0900
@@ -21,6 +21,7 @@
#include "clang/Analysis/Analyses/LiveVariables.h"
#include "clang/Analysis/LocalCheckers.h"
#include "llvm/Support/Streams.h"
+#include <fstream>
using namespace clang;
@@ -554,17 +555,19 @@
#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 @@
// << 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 @@
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
Index: clang/Driver/ASTConsumers.h
===================================================================
--- clang.orig/Driver/ASTConsumers.h 2007-12-23 22:49:05.000000000 +0900
+++ clang/Driver/ASTConsumers.h 2007-12-23 23:14:01.000000000 +0900
@@ -41,6 +41,11 @@
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,
Index: clang/Driver/clang.cpp
===================================================================
--- clang.orig/Driver/clang.cpp 2007-12-23 22:47:04.000000000 +0900
+++ clang/Driver/clang.cpp 2007-12-23 22:48:36.000000000 +0900
@@ -55,6 +55,7 @@
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 @@
"Run prototype serializtion code."),
clEnumValN(EmitLLVM, "emit-llvm",
"Build ASTs then convert to LLVM, emit .ll file"),
+ clEnumValN(EmitBC, "emit-bc",
+ "Build ASTs then convert to LLVM, emit .bc file"),
clEnumValN(SerializeAST, "serialize",
"Build ASTs and emit .ast file"),
clEnumValN(RewriteTest, "rewrite-test",
@@ -911,7 +914,10 @@
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);
_______________________________________________
cfe-dev mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev