[cfe-commits] r45339 - in /cfe/trunk/Driver: ASTConsumers.cpp ASTConsumers.h clang.cpp

Seo Sanghyeon sanxiyn at gmail.com
Sun Dec 23 17:52:35 PST 2007


Author: sanxiyn
Date: Sun Dec 23 19:52:34 2007
New Revision: 45339

URL: http://llvm.org/viewvc/llvm-project?rev=45339&view=rev
Log:
Implement -emit-llvm-bc option

Modified:
    cfe/trunk/Driver/ASTConsumers.cpp
    cfe/trunk/Driver/ASTConsumers.h
    cfe/trunk/Driver/clang.cpp

Modified: cfe/trunk/Driver/ASTConsumers.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/ASTConsumers.cpp?rev=45339&r1=45338&r2=45339&view=diff

==============================================================================
--- cfe/trunk/Driver/ASTConsumers.cpp (original)
+++ cfe/trunk/Driver/ASTConsumers.cpp Sun Dec 23 19:52:34 2007
@@ -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
 

Modified: cfe/trunk/Driver/ASTConsumers.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/ASTConsumers.h?rev=45339&r1=45338&r2=45339&view=diff

==============================================================================
--- cfe/trunk/Driver/ASTConsumers.h (original)
+++ cfe/trunk/Driver/ASTConsumers.h Sun Dec 23 19:52:34 2007
@@ -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,

Modified: cfe/trunk/Driver/clang.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/clang.cpp?rev=45339&r1=45338&r2=45339&view=diff

==============================================================================
--- cfe/trunk/Driver/clang.cpp (original)
+++ cfe/trunk/Driver/clang.cpp Sun Dec 23 19:52:34 2007
@@ -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-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 @@
       
     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);





More information about the cfe-commits mailing list