[opengtl-commits] [566] add code to to CodeGenerator to use the new memory manager

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


Revision: 566
Author:   cyrille
Date:     2009-02-27 18:29:30 +0100 (Fri, 27 Feb 2009)

Log Message:
-----------
add code to to CodeGenerator to use the new memory manager

Modified Paths:
--------------
    trunk/OpenGTL/OpenGTL/GTLCore/CodeGenerator_p.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/CodeGenerator_p.h
    trunk/OpenGTL/OpenGTL/GTLCore/VariableNG_p.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp
    trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.cpp
    trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.cpp


Modified: trunk/OpenGTL/OpenGTL/GTLCore/CodeGenerator_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/CodeGenerator_p.cpp	2009-02-27 16:56:10 UTC (rev 565)
+++ trunk/OpenGTL/OpenGTL/GTLCore/CodeGenerator_p.cpp	2009-02-27 17:29:30 UTC (rev 566)
@@ -19,6 +19,7 @@
 
 #include "CodeGenerator_p.h"
 
+#include <llvm/CallingConv.h>
 #include <llvm/Constants.h>
 #include <llvm/Function.h>
 #include <llvm/Instructions.h>
@@ -729,12 +730,58 @@
   return val;
 }
 
-llvm::Value* CodeGenerator::allocateMemory( const llvm::Type* _type, llvm::Value* _size, llvm::BasicBlock* _bb)
+// #define _USE_GTL_MM_
+
+#ifdef _USE_GTL_MM_
+
+llvm::Function* createGtlMallocFunction( llvm::Module* _module )
 {
+  std::vector<const llvm::Type*> memcpyTyArgs;
+  memcpyTyArgs.push_back(llvm::IntegerType::get(32));
+  llvm::FunctionType* memcpyTy = llvm::FunctionType::get( llvm::PointerType::get(llvm::IntegerType::get(8), 0), memcpyTyArgs, false);
+  
+  llvm::Function* func_llvm_memcpy_i32 = (llvm::Function*)_module->getOrInsertFunction(
+      "gtl_malloc", memcpyTy); // (external, no body)
+  func_llvm_memcpy_i32->setCallingConv(llvm::CallingConv::C);
+  return func_llvm_memcpy_i32;
+}
+
+llvm::Function* createGtlFreeFunction( llvm::Module* _module )
+{
+  std::vector<const llvm::Type*> memcpyTyArgs;
+  memcpyTyArgs.push_back(llvm::PointerType::get(llvm::IntegerType::get(8), 0));
+  llvm::FunctionType* memcpyTy = llvm::FunctionType::get( llvm::Type::VoidTy, memcpyTyArgs, false);
+  
+  llvm::Function* func_llvm_memcpy_i32 = (llvm::Function*)_module->getOrInsertFunction(
+      "gtl_free", memcpyTy); // (external, no body)
+  func_llvm_memcpy_i32->setCallingConv(llvm::CallingConv::C);
+  return func_llvm_memcpy_i32;
+}
+#endif
+
+llvm::Value* CodeGenerator::allocateMemory( GenerationContext& _gc, const llvm::Type* _type, llvm::Value* _size, llvm::BasicBlock* _bb)
+{
+#ifdef _USE_GTL_MM_
+  llvm::Function* func = createGtlMallocFunction( _gc.module()->llvmModule() );
+  std::vector<llvm::Value*> params;
+  params.push_back( llvm::BinaryOperator::createMul( _size, llvm::ConstantExpr::getSizeOf( _type ), "", _bb  ) );
+  llvm::CallInst *CallFunc = llvm::CallInst::Create(func, params.begin(), params.end(), "", _bb);
+  CallFunc->setTailCall(false);
+  return convertPointerTo( _bb, CallFunc, _type );
+#else
   return new llvm::MallocInst( _type, _size, "", _bb);
+#endif
 }
 
-void CodeGenerator::freeMemory( llvm::Value* _ptr, llvm::BasicBlock* _bb)
+void CodeGenerator::freeMemory( GenerationContext& _gc, llvm::Value* _ptr, llvm::BasicBlock* _bb)
 {
+#ifdef _USE_GTL_MM_
+  llvm::Function* func = createGtlFreeFunction( _gc.module()->llvmModule() );
+  std::vector<llvm::Value*> params;
+  params.push_back( convertPointerTo( _bb, _ptr, llvm::PointerType::get(llvm::IntegerType::get(8), 0) )  );
+  llvm::CallInst *CallFunc = llvm::CallInst::Create(func, params.begin(), params.end(), "", _bb);
+  CallFunc->setTailCall(false);
+#else
   new llvm::FreeInst( _ptr, _bb );
+#endif
 }

Modified: trunk/OpenGTL/OpenGTL/GTLCore/CodeGenerator_p.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/CodeGenerator_p.h	2009-02-27 16:56:10 UTC (rev 565)
+++ trunk/OpenGTL/OpenGTL/GTLCore/CodeGenerator_p.h	2009-02-27 17:29:30 UTC (rev 566)
@@ -266,8 +266,8 @@
     public:
       GTLCore::ExpressionResult callFunction( GenerationContext& _gc, llvm::BasicBlock* _bb, const GTLCore::Function* _function, const std::list<AST::Expression*>& m_arguments );
     public:
-      static llvm::Value* allocateMemory( const llvm::Type* _type, llvm::Value* _size, llvm::BasicBlock* _bb);
-      static void freeMemory( llvm::Value* _ptr, llvm::BasicBlock* _bb);
+      static llvm::Value* allocateMemory( GenerationContext& _gc, const llvm::Type* _type, llvm::Value* _size, llvm::BasicBlock* _bb);
+      static void freeMemory( GenerationContext& _gc, llvm::Value* _ptr, llvm::BasicBlock* _bb);
     private:
       static llvm::Value* createComparisonExpression(llvm::BasicBlock* currentBlock, llvm::Value* lhs, const Type* lhsType, llvm::Value* rhs, const Type* rhsType, unsigned int unsignedIntegerPred, unsigned int signedIntegerPred, unsigned int floatPred);
       static ExpressionResult createComparisonExpression( llvm::BasicBlock* currentBlock, ExpressionResult lhs, const Type* lhsType, ExpressionResult rhs, const Type* rhsType, unsigned int unsignedIntegerPred, unsigned int signedIntegerPred, unsigned int floatPred);

Modified: trunk/OpenGTL/OpenGTL/GTLCore/VariableNG_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/VariableNG_p.cpp	2009-02-27 16:56:10 UTC (rev 565)
+++ trunk/OpenGTL/OpenGTL/GTLCore/VariableNG_p.cpp	2009-02-27 17:29:30 UTC (rev 566)
@@ -104,7 +104,7 @@
   } else {
     if( d->allocatedInMemory )
     {
-      pointer_ = CodeGenerator::allocateMemory(d->type->d->type(), CodeGenerator::integerToConstant(1), _bb);
+      pointer_ = CodeGenerator::allocateMemory(_generationContext, d->type->d->type(), CodeGenerator::integerToConstant(1), _bb);
     } else {
       pointer_ = new llvm::AllocaInst( d->type->d->type(), llvm::ConstantInt::get(llvm::Type::Int32Ty, 1), "Variable", _bb);
     }

Modified: trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp	2009-02-27 16:56:10 UTC (rev 565)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp	2009-02-27 17:29:30 UTC (rev 566)
@@ -239,7 +239,7 @@
     llvm::Value* array = 0;
     if( _allocatedInMemory )
     {
-      array = CodeGenerator::allocateMemory(
+      array = CodeGenerator::allocateMemory( _generationContext,
                     _pointerType->embeddedType()->d->type(), _size, _currentBlock);
     } else {
       array = new llvm::AllocaInst(
@@ -363,10 +363,10 @@
       indexes.push_back( llvm::ConstantInt::get(llvm::Type::Int32Ty, ArrayWrap::POS_DATA )); // Access the data of the array
       // Free the array
       llvm::Value* ptrToData = llvm::GetElementPtrInst::Create( _pointer, indexes.begin(), indexes.end(), "ArrayVisitor::cleanUp", afterBlock);
-      CodeGenerator::freeMemory(new llvm::LoadInst( ptrToData, "ArrayVisitor::cleanUp", afterBlock ), afterBlock );
+      CodeGenerator::freeMemory(_generationContext, new llvm::LoadInst( ptrToData, "ArrayVisitor::cleanUp", afterBlock ), afterBlock );
       if( _deletePointer )
       {
-        CodeGenerator::freeMemory( _pointer, afterBlock );
+        CodeGenerator::freeMemory( _generationContext, _pointer, afterBlock );
       }
     }
     
@@ -619,7 +619,7 @@
   _generationContext.llvmFunction()->getBasicBlockList().push_back( afterIfBlock);
   if( _allocatedInMemory and _deletePointer )
   {
-    CodeGenerator::freeMemory(_pointer, lastIfBlock );
+    CodeGenerator::freeMemory( _generationContext, _pointer, lastIfBlock );
   }
   CodeGenerator::createIfStatement( _currentBlock, test, Type::Boolean, firstIfBlock, lastIfBlock, afterIfBlock );
   

Modified: trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.cpp
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.cpp	2009-02-27 16:56:10 UTC (rev 565)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.cpp	2009-02-27 17:29:30 UTC (rev 566)
@@ -609,7 +609,7 @@
                           GTLCore::CodeGenerator::integerToConstant( 1 ) );
     llvm::Value* y_i = GTLCore::CodeGenerator::createRound( currentBlock, y_f );
   //    pixelX* px = new pixelX;
-    llvm::Value* px_var = GTLCore::CodeGenerator::allocateMemory( _pixelType->d->type(), GTLCore::CodeGenerator::integerToConstant( 1 ), currentBlock );
+    llvm::Value* px_var = GTLCore::CodeGenerator::allocateMemory( generationContext, _pixelType->d->type(), GTLCore::CodeGenerator::integerToConstant( 1 ), currentBlock );
     GTLCore::CodeGenerator::setCountFieldOf( currentBlock, px_var, GTLCore::CodeGenerator::integerToConstant( 0 ) );
   //    memToPixel( image, px, image_wrap_data(self, x, y ) );
     currentBlock = memToPixel( generationContext, currentBlock,

Modified: trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.cpp
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.cpp	2009-02-27 16:56:10 UTC (rev 565)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.cpp	2009-02-27 17:29:30 UTC (rev 566)
@@ -119,7 +119,7 @@
     _generationContext.llvmFunction()->getBasicBlockList().push_back( afterIfBlock);
     if( _allocatedInMemory )
     {
-      GTLCore::CodeGenerator::freeMemory( _pointer, firstIfBlock );
+      GTLCore::CodeGenerator::freeMemory( _generationContext, _pointer, firstIfBlock );
     }
     GTLCore::CodeGenerator::createIfStatement( _currentBlock, test, GTLCore::Type::Boolean, firstIfBlock, firstIfBlock, afterIfBlock );
     return afterIfBlock;


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