[opengtl-commits] [387] make it possible to export a ModuleData to C

[ Thread Index | Date Index | More lists.tuxfamily.org/opengtl-commits Archives ]


Revision: 387
Author:   cyrille
Date:     2008-09-12 22:46:55 +0200 (Fri, 12 Sep 2008)

Log Message:
-----------
make it possible to export a ModuleData to C

Modified Paths:
--------------
    trunk/OpenGTL/OpenGTL/GTLCore/CMakeLists.txt
    trunk/OpenGTL/OpenGTL/GTLCore/ModuleData_p.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/ModuleData_p.h
    trunk/OpenGTL/OpenShiva/OpenShiva/Kernel.cpp
    trunk/OpenGTL/OpenShiva/OpenShiva/Kernel.h
    trunk/OpenGTL/OpenShiva/tools/compiler/ShivaC.cpp
    trunk/OpenGTL/TODO


Modified: trunk/OpenGTL/OpenGTL/GTLCore/CMakeLists.txt
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/CMakeLists.txt	2008-09-11 23:11:34 UTC (rev 386)
+++ trunk/OpenGTL/OpenGTL/GTLCore/CMakeLists.txt	2008-09-12 20:46:55 UTC (rev 387)
@@ -1,7 +1,7 @@
 add_subdirectory(tests)
 
 # Find llvm jit, interpreter and native
-FIND_LLVM_LIBS( ${LLVM_CONFIG_EXECUTABLE} "native bitwriter linker bitreader jit interpreter support ipo" LLVM_LIBS LLVM_NATIVE_OBJECTS )
+FIND_LLVM_LIBS( ${LLVM_CONFIG_EXECUTABLE} "native bitwriter linker bitreader jit interpreter support ipo cbackend" LLVM_LIBS LLVM_NATIVE_OBJECTS )
 
 include_directories( ${LLVM_INCLUDE_DIR} )
 

Modified: trunk/OpenGTL/OpenGTL/GTLCore/ModuleData_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/ModuleData_p.cpp	2008-09-11 23:11:34 UTC (rev 386)
+++ trunk/OpenGTL/OpenGTL/GTLCore/ModuleData_p.cpp	2008-09-12 20:46:55 UTC (rev 387)
@@ -19,11 +19,19 @@
 
 #include "ModuleData_p.h"
 
+#include <sstream>
+
 #include "Debug.h"
 #include "GTLCore/Function.h"
 #include "TypeManager.h"
+#include "Macros_p.h"
 
 #include <llvm/Module.h>
+#include <llvm/PassManager.h>
+#include <llvm/Target/TargetData.h>
+#include <llvm/Target/TargetMachine.h>
+#include <llvm/Target/TargetMachineRegistry.h>
+#include <llvm/Analysis/Verifier.h>
 
 using namespace GTLCore;
 
@@ -90,3 +98,36 @@
   }
   return functions;
 }
+
+GTLCore::String ModuleData::asCCode() const
+{
+  llvm::TargetMachine* ctarget = 0;
+  std::string cId = "c";
+  for( llvm::TargetMachineRegistry::iterator it = llvm::TargetMachineRegistry::begin();
+        it != llvm::TargetMachineRegistry::end(); ++it)
+  {
+    GTL_DEBUG( it->Name );
+    if( it->Name == cId )
+    {
+      ctarget = it->CtorFn(*m_llvmModule, "");
+      break;
+    }
+  }
+  
+  GTL_ASSERT(ctarget);
+  GTL_ASSERT(ctarget->WantsWholeFile());
+  
+  llvm::PassManager PM;
+  PM.add(new llvm::TargetData(*ctarget->getTargetData()));
+  PM.add(llvm::createVerifierPass());
+  
+  std::ostringstream os;
+
+  // Ask the target to add backend passes as necessary.
+  bool succ = ctarget->addPassesToEmitWholeFile( PM, os, llvm::TargetMachine::AssemblyFile, false);
+  GTL_ASSERT(not succ);
+  UNUSED(succ);
+  PM.run(*m_llvmModule);
+  
+  return os.str();
+}

Modified: trunk/OpenGTL/OpenGTL/GTLCore/ModuleData_p.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/ModuleData_p.h	2008-09-11 23:11:34 UTC (rev 386)
+++ trunk/OpenGTL/OpenGTL/GTLCore/ModuleData_p.h	2008-09-12 20:46:55 UTC (rev 387)
@@ -54,6 +54,10 @@
       const llvm::Module* llvmModule() const { return m_llvmModule; }
       TypeManager* typeManager() { return m_typeManager; }
       const TypeManager* typeManager() const { return m_typeManager; }
+      /**
+       * @return a string containing the module compiled as C
+       */
+      GTLCore::String asCCode() const;
     private:
       llvm::Module* m_llvmModule;
       std::map<ScopedName, Function*> m_functions;

Modified: trunk/OpenGTL/OpenShiva/OpenShiva/Kernel.cpp
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/Kernel.cpp	2008-09-11 23:11:34 UTC (rev 386)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/Kernel.cpp	2008-09-12 20:46:55 UTC (rev 387)
@@ -270,3 +270,9 @@
   os << *d->moduleData->llvmModule() << std::endl;
   return os.str();
 }
+
+GTLCore::String Kernel::cSourceCode() const
+{
+  return d->moduleData->asCCode();
+}
+

Modified: trunk/OpenGTL/OpenShiva/OpenShiva/Kernel.h
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/Kernel.h	2008-09-11 23:11:34 UTC (rev 386)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/Kernel.h	2008-09-12 20:46:55 UTC (rev 387)
@@ -23,9 +23,10 @@
 #include <list>
 
 namespace GTLCore {
+  class AbstractImage;
   class ErrorMessage;
+  class ModuleData;
   class Region;
-  class AbstractImage;
 }
 
 #include <GTLCore/String.h>
@@ -76,6 +77,10 @@
        * @return the assembly source code, it's mostly usefull for testing purpose
        */
       GTLCore::String asmSourceCode() const;
+      /**
+       * @return the C source code, it's mostly usefull for testing purpose
+       */
+      GTLCore::String cSourceCode() const;
     public: // Function call
       void evaluatePixeles( const GTLCore::Region& _region, const std::list< GTLCore::AbstractImage* >& _inputImages, GTLCore::AbstractImage* _outputImage) const;
       /**

Modified: trunk/OpenGTL/OpenShiva/tools/compiler/ShivaC.cpp
===================================================================
--- trunk/OpenGTL/OpenShiva/tools/compiler/ShivaC.cpp	2008-09-11 23:11:34 UTC (rev 386)
+++ trunk/OpenGTL/OpenShiva/tools/compiler/ShivaC.cpp	2008-09-12 20:46:55 UTC (rev 387)
@@ -38,6 +38,7 @@
   std::cout << std::endl;
   std::cout << "Options : " << std::endl;
   std::cout << "  -S --asm-source         print the assembly source code generated" << std::endl;
+  std::cout << "  -C --c-source           print the source code as C" << std::endl;
   std::cout << "  -o --output [filename]  save the output to a file" << std::endl;
 //   std::cout << "  -L --module-dir   add a location where to find modules" << std::endl;
   std::cout << std::endl;
@@ -51,6 +52,7 @@
   GTLCore::String fileName = "";
   GTLCore::String output = "";
   bool showAssembly = false;
+  bool showAsC = false;
   for(int ai = 1; ai < argc; ai++)
   {
     if(ARG_IS("-h","--help"))
@@ -63,6 +65,8 @@
       return EXIT_SUCCESS;
     } else if(ARG_IS("-S","--asm-source")) {
       showAssembly = true;
+    } else if(ARG_IS("-C","--c-source")) {
+      showAsC = true;
     } else if(ARG_IS("-o","--output")) {
       if( ai == argc )
       {
@@ -121,6 +125,10 @@
     {
       std::cout << p.asmSourceCode();
     }
+    if( showAsC )
+    {
+      std::cout << p.cSourceCode();
+    }
   }
   return EXIT_SUCCESS;
 }

Modified: trunk/OpenGTL/TODO
===================================================================
--- trunk/OpenGTL/TODO	2008-09-11 23:11:34 UTC (rev 386)
+++ trunk/OpenGTL/TODO	2008-09-12 20:46:55 UTC (rev 387)
@@ -2,7 +2,10 @@
  * array overflow
  * array/structure leak in return function (when assigne to a member or an index of an array) (<= might be fixed)
  * remove all DEPRECATED functions
-
+ * Variable::replacePointer should replace pointer, not value
+ * all kind of return should garbage collect
+ * garbage collecting need to be done at the end of a context
+ 
 Known bugs:
  * sounds like you can have two variables of the same name
 


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/