[opengtl-commits] [249] add a visitor for Vectors

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


Revision: 249
Author:   cyrille
Date:     2008-06-27 22:33:04 +0200 (Fri, 27 Jun 2008)

Log Message:
-----------
add a visitor for Vectors

Modified Paths:
--------------
    trunk/OpenGTL/OpenGTL/GTLCore/Type.h
    trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.h


Modified: trunk/OpenGTL/OpenGTL/GTLCore/Type.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Type.h	2008-06-27 20:32:30 UTC (rev 248)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Type.h	2008-06-27 20:33:04 UTC (rev 249)
@@ -62,6 +62,7 @@
     friend class Function;
     friend class TypeManager;
     friend class Visitor;
+    friend class VectorVisitor;
     public:
       enum DataType {
         UNDEFINED,

Modified: trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp	2008-06-27 20:32:30 UTC (rev 248)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp	2008-06-27 20:33:04 UTC (rev 249)
@@ -45,12 +45,13 @@
 PrimitiveVisitor* primitiveVisitor = 0;
 ArrayVisitor* arrayVisitor = 0;
 StructureVisitor* structureVisitor = 0;
-
+VectorVisitor* vectorVisitor = 0;
 STATIC_INITIALISATION( Visitors )
 {
   primitiveVisitor = new PrimitiveVisitor;
   arrayVisitor = new ArrayVisitor;
   structureVisitor = new StructureVisitor;
+  vectorVisitor = new VectorVisitor;
 }
 
 //--------- Visitor ---------///
@@ -64,7 +65,7 @@
   
 }
 
-const Visitor* Visitor::getVisitorFor(const GTLCore::Type* _type)
+const Visitor* Visitor::getVisitorFor(const Type* _type)
 {
   GTL_ASSERT( _type );
   if( _type->d->visitor() )
@@ -75,6 +76,8 @@
     return arrayVisitor;
   } else if( _type->dataType() == Type::STRUCTURE ) {
     return structureVisitor;
+  } else if( _type->dataType() == Type::VECTOR ) {
+    return vectorVisitor;
   } else {
     return primitiveVisitor;
   }
@@ -91,12 +94,12 @@
 {
 }
 
-const GTLCore::Type* PrimitiveVisitor::pointerToIndexType( const GTLCore::Type* ) const
+const Type* PrimitiveVisitor::pointerToIndexType( const Type* ) const
 {
   return 0;
 }
 
-llvm::Value* PrimitiveVisitor::pointerToIndex(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _type, llvm::Value* _index) const
+llvm::Value* PrimitiveVisitor::pointerToIndex(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _type, llvm::Value* _index) const
 {
   GTL_ABORT("Primitive type doesn't allow access using indexes"); //TODO except vectors
 }
@@ -106,22 +109,23 @@
   return new llvm::LoadInst( _pointer, "", _currentBlock);
 }
 
-llvm::BasicBlock* PrimitiveVisitor::set( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _pointerType, llvm::Value* _value, const GTLCore::Type* _valueType, bool _allocatedInMemory ) const
+llvm::BasicBlock* PrimitiveVisitor::set( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, llvm::Value* _value, const Type* _valueType, bool _allocatedInMemory ) const
 {
-  GTL_ASSERT( _pointerType->dataType() != Type::STRUCTURE and _pointerType->dataType() != Type::ARRAY );
+  GTL_ASSERT( _pointerType->dataType() != Type::STRUCTURE and _pointerType->dataType() != Type::ARRAY and _pointerType->dataType() != Type::VECTOR );
+  GTL_DEBUG( *_pointer );
   new llvm::StoreInst(
           _generationContext.codeGenerator()->convertValueTo( _currentBlock, _value, _valueType, _pointerType ),
           _pointer, "", _currentBlock);
   return _currentBlock;
 }
 
-llvm::BasicBlock* PrimitiveVisitor::initialise(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _pointerType, const std::list< llvm::Value*>& _sizes, bool _allocatedInMemory) const
+llvm::BasicBlock* PrimitiveVisitor::initialise(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, const std::list< llvm::Value*>& _sizes, bool _allocatedInMemory) const
 {
   // Don't do nothing
   return _currentBlock;
 }
 
-llvm::BasicBlock* PrimitiveVisitor::cleanUp( GenerationContext& , llvm::BasicBlock* _currentBlock, llvm::Value* , const GTLCore::Type* , llvm::Value* , bool ) const
+llvm::BasicBlock* PrimitiveVisitor::cleanUp( GenerationContext& , llvm::BasicBlock* _currentBlock, llvm::Value* , const Type* , llvm::Value* , bool ) const
 {
   return _currentBlock;
 }
@@ -136,12 +140,12 @@
 {
 }
 
-const GTLCore::Type* ArrayVisitor::pointerToIndexType( const GTLCore::Type* _type) const
+const Type* ArrayVisitor::pointerToIndexType( const Type* _type) const
 {
   return _type->embeddedType();
 }
 
-llvm::Value* ArrayVisitor::pointerToIndex(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _type, llvm::Value* _index) const
+llvm::Value* ArrayVisitor::pointerToIndex(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _type, llvm::Value* _index) const
 {
   return _generationContext.codeGenerator()->accessArrayValue( _currentBlock, _pointer, _index);
 }
@@ -152,7 +156,7 @@
   return _pointer;
 }
 
-llvm::BasicBlock* ArrayVisitor::set( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _pointerType, llvm::Value* _value, const GTLCore::Type* _valueType, bool _allocatedInMemory) const
+llvm::BasicBlock* ArrayVisitor::set( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, llvm::Value* _value, const Type* _valueType, bool _allocatedInMemory) const
 {
     // Check if size are identical or update the size of this array
     {
@@ -168,7 +172,7 @@
     
     }
     //   int i = 0;
-    GTLCore::VariableNG* index = new GTLCore::VariableNG( GTLCore::Type::Integer32, false);
+    VariableNG* index = new VariableNG( Type::Integer32, false);
     index->initialise( _generationContext, _currentBlock, _generationContext.codeGenerator()->integerToConstant(0), Type::Integer32, std::list<llvm::Value*>());
     
     // Construct the body of the for loop
@@ -198,7 +202,7 @@
                     endBodyBlock );
 }
 
-void ArrayVisitor::setSize(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _pointerType, llvm::Value* _size, bool _allocatedInMemory, bool _allreadyInitialised) const
+void ArrayVisitor::setSize(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, llvm::Value* _size, bool _allocatedInMemory, bool _allreadyInitialised) const
 {
   GTL_ASSERT( _pointerType->dataType() == Type::ARRAY );
   GTL_DEBUG( *_size );
@@ -207,7 +211,7 @@
   indexes.push_back( llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); // Access the size of the array
   { // Init the size
     llvm::Value* ptr = new llvm::GetElementPtrInst( _pointer, indexes.begin(), indexes.end(), "", _currentBlock);
-    new llvm::StoreInst(_generationContext.codeGenerator()->convertValueTo( _currentBlock, _size, GTLCore::Type::Integer32, GTLCore::Type::Integer32 ), ptr, "", _currentBlock);
+    new llvm::StoreInst(_generationContext.codeGenerator()->convertValueTo( _currentBlock, _size, Type::Integer32, Type::Integer32 ), ptr, "", _currentBlock);
   }
   // Allocate the Array
   indexes[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1);
@@ -239,7 +243,7 @@
   return new llvm::LoadInst( ptr, "", currentBlock);
 }
 
-llvm::BasicBlock* ArrayVisitor::initialise(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _pointerType, const std::list< llvm::Value*>& _sizes, bool _allocatedInMemory) const
+llvm::BasicBlock* ArrayVisitor::initialise(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, const std::list< llvm::Value*>& _sizes, bool _allocatedInMemory) const
 {
   GTL_ASSERT( _pointerType->dataType() == Type::ARRAY );
   GTL_DEBUG( _sizes.empty() );
@@ -250,7 +254,7 @@
     std::list< llvm::Value*> sizeAfter = _sizes;
     sizeAfter.pop_front();
     //   int i = 0;
-    GTLCore::VariableNG* index = new GTLCore::VariableNG( GTLCore::Type::Integer32, false);
+    VariableNG* index = new VariableNG( Type::Integer32, false);
     index->initialise( _generationContext, _currentBlock, _generationContext.codeGenerator()->integerToConstant(0), Type::Integer32, std::list<llvm::Value*>());
     
     // Construct the body of the for loop
@@ -279,14 +283,62 @@
   return _currentBlock;
 }
 
-llvm::BasicBlock* ArrayVisitor::cleanUp( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _pointerType, llvm::Value* _donttouch, bool _allocatedInMemory ) const
+llvm::BasicBlock* ArrayVisitor::cleanUp( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, llvm::Value* _donttouch, bool _allocatedInMemory ) const
 {
   
   return _currentBlock;
 }
 
-//--------- PrimitiveVisitor ---------///
+//--------- VectorVisitor ---------///
 
+
+VectorVisitor::VectorVisitor( )
+{}
+VectorVisitor::~VectorVisitor()
+{}
+const Type* VectorVisitor::pointerToIndexType( const Type* _type ) const
+{
+  return _type->embeddedType();
+}
+
+llvm::Value* VectorVisitor::pointerToIndex(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, llvm::Value* _index) const
+{
+  GTL_ASSERT( _pointerType->dataType() == Type::VECTOR );
+  
+  return new llvm::GetElementPtrInst( 
+              CodeGenerator::convertPointerTo( _currentBlock, _pointer, _pointerType->embeddedType()->d->type() ),
+              _index, "", _currentBlock);
+}
+
+llvm::Value* VectorVisitor::get( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer) const
+{
+  return new llvm::LoadInst( _pointer, "", _currentBlock);
+}
+
+llvm::BasicBlock* VectorVisitor::set(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, llvm::Value* _value, const Type* _valueType, bool _allocatedInMemory) const
+{
+  GTL_ASSERT( _pointerType->dataType() == Type::VECTOR );
+  new llvm::StoreInst(
+          _generationContext.codeGenerator()->convertValueTo( _currentBlock, _value, _valueType, _pointerType ),
+          _pointer, "", _currentBlock);
+  return _currentBlock;
+}
+
+llvm::BasicBlock* VectorVisitor::initialise( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, const std::list< llvm::Value*>& _sizes, bool _allocatedInMemory) const
+{
+  // Don't do nothing
+  return _currentBlock;
+}
+
+llvm::BasicBlock* VectorVisitor::cleanUp( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, llvm::Value* _donttouch, bool _allocatedInMemory ) const
+{
+  // Don't do nothing
+  return _currentBlock;
+}
+
+
+//--------- StructureVisitor ---------///
+
 StructureVisitor::StructureVisitor() : Visitor( )
 {
 }
@@ -295,7 +347,7 @@
 {
 }
 
-const GTLCore::Type* StructureVisitor::pointerToIndexType( const GTLCore::Type* ) const
+const Type* StructureVisitor::pointerToIndexType( const Type* ) const
 {
   return 0;
 }
@@ -310,7 +362,7 @@
   return new llvm::GetElementPtrInst( _pointer, indexes.begin(), indexes.end(), "", _currentBlock);
 }
 
-llvm::Value* StructureVisitor::pointerToIndex(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _type, llvm::Value* _index) const
+llvm::Value* StructureVisitor::pointerToIndex(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _type, llvm::Value* _index) const
 {
   GTL_ABORT("Structure doesn't allow access using indexes");
 }
@@ -320,12 +372,12 @@
   return _pointer;
 }
 
-llvm::BasicBlock* StructureVisitor::set( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _pointerType, llvm::Value* _value, const GTLCore::Type* _valueType, bool _allocatedInMemory ) const
+llvm::BasicBlock* StructureVisitor::set( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, llvm::Value* _value, const Type* _valueType, bool _allocatedInMemory ) const
 {
   GTL_ASSERT( _pointerType->dataType() == Type::STRUCTURE );
   for(uint i = 0; i < _pointerType->structDataMembers()->size(); ++i)
   {
-    const GTLCore::Type* type = (*_pointerType->structDataMembers())[i].type();
+    const Type* type = (*_pointerType->structDataMembers())[i].type();
     llvm::Value* nptrToOwnMember = pointerToValue(_generationContext, _currentBlock, _pointer, i);
     llvm::Value* nptrToValueMember = pointerToValue( _generationContext, _currentBlock, _value, i);
     const Visitor* visitor = Visitor::getVisitorFor( type );
@@ -335,10 +387,10 @@
   return _currentBlock;
 }
 
-llvm::BasicBlock* StructureVisitor::initialise(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _pointerType, const std::list< llvm::Value*>& _sizes, bool _allocatedInMemory) const
+llvm::BasicBlock* StructureVisitor::initialise(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, const std::list< llvm::Value*>& _sizes, bool _allocatedInMemory) const
 {
   GTL_ASSERT( _pointerType->dataType() == Type::STRUCTURE );
-  const std::vector<GTLCore::Type::StructDataMember>* sm = _pointerType->structDataMembers();
+  const std::vector<Type::StructDataMember>* sm = _pointerType->structDataMembers();
   
   for( uint i = 0; i < sm->size(); ++i)
   {
@@ -348,7 +400,7 @@
     {
       sizes.push_back( _generationContext.codeGenerator()->integerToConstant( *it ) );
     }
-    const GTLCore::Type* type = (*sm)[i].type();
+    const Type* type = (*sm)[i].type();
     const Visitor* visitor = Visitor::getVisitorFor( type );
     _currentBlock = visitor->initialise( _generationContext, _currentBlock,
                             pointerToValue( _generationContext, _currentBlock, _pointer, i ),
@@ -357,15 +409,15 @@
   return _currentBlock;
 }
 
-llvm::BasicBlock* StructureVisitor::cleanUp( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _pointerType, llvm::Value* _donttouch, bool _allocatedInMemory ) const
+llvm::BasicBlock* StructureVisitor::cleanUp( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, llvm::Value* _donttouch, bool _allocatedInMemory ) const
 {
   if( _pointer == _donttouch ) return _currentBlock;
   GTL_ASSERT( _pointerType->dataType() == Type::STRUCTURE );
-  const std::vector<GTLCore::Type::StructDataMember>* sm = _pointerType->structDataMembers();
+  const std::vector<Type::StructDataMember>* sm = _pointerType->structDataMembers();
   
   for( uint i = 0; i < sm->size(); ++i)
   {
-    const GTLCore::Type* type = (*sm)[i].type();
+    const Type* type = (*sm)[i].type();
     const Visitor* visitor = Visitor::getVisitorFor( type );
     _currentBlock = visitor->cleanUp( _generationContext, _currentBlock, pointerToValue( _generationContext, _currentBlock, _pointer, i ), type, _donttouch, _allocatedInMemory );
   }

Modified: trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.h	2008-06-27 20:32:30 UTC (rev 248)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.h	2008-06-27 20:33:04 UTC (rev 249)
@@ -156,6 +156,19 @@
       void setSize(GenerationContext& _generationContext, llvm::BasicBlock* currentBlock, llvm::Value* _pointer, const GTLCore::Type* _pointerType, llvm::Value* _size, bool _allocatedInMemory, bool _allreadyInitialised ) const;
   };
 
+  class VectorVisitor : public Visitor {
+      friend class ::VisitorsFactory;
+      VectorVisitor( );
+      virtual ~VectorVisitor();
+    public:
+      virtual const GTLCore::Type* pointerToIndexType( const GTLCore::Type* _type ) const;
+      virtual llvm::Value* pointerToIndex(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _type, llvm::Value* _index) const;
+      virtual llvm::Value* get( GenerationContext& _generationContext, llvm::BasicBlock* currentBlock, llvm::Value* pointer) const;
+      virtual llvm::BasicBlock* set(GenerationContext& _generationContext, llvm::BasicBlock* currentBlock, llvm::Value* pointer, const GTLCore::Type* _pointerType, llvm::Value*, const GTLCore::Type* _valueType, bool _allocatedInMemory) const;
+      virtual llvm::BasicBlock* initialise( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _pointerType, const std::list< llvm::Value*>& _sizes, bool _allocatedInMemory) const;
+      virtual llvm::BasicBlock* cleanUp( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _pointerType, llvm::Value* _donttouch, bool _allocatedInMemory ) const;
+  };
+
   /**
    * @internal
    * @brief Give access to members of a structure
@@ -175,7 +188,6 @@
       llvm::Value* pointerToValue(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, int index ) const;
   };
 
-
 }
 
 


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