[opengtl-commits] [432] correctly initialize static variables

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


Revision: 432
Author:   cyrille
Date:     2008-10-08 13:26:00 +0200 (Wed, 08 Oct 2008)

Log Message:
-----------
correctly initialize static variables

Modified Paths:
--------------
    trunk/OpenGTL/OpenGTL/GTLCore/AST/Tree.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/AST/Tree.h
    trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/Type.h
    trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.h
    trunk/OpenGTL/OpenShiva/OpenShiva/Parser_p.cpp
    trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.cpp
    trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.h
    trunk/OpenGTL/OpenShiva/tests/imagegenerators/raytracer.shiva

Removed Paths:
-------------
    trunk/OpenGTL/TODO


Modified: trunk/OpenGTL/OpenGTL/GTLCore/AST/Tree.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/AST/Tree.cpp	2008-10-06 22:45:56 UTC (rev 431)
+++ trunk/OpenGTL/OpenGTL/GTLCore/AST/Tree.cpp	2008-10-08 11:26:00 UTC (rev 432)
@@ -32,16 +32,23 @@
 #include "GTLCore/VariableNG_p.h"
 #include "GTLCore/Utils_p.h"
 #include "GTLCore/ModuleData_p.h"
+#include "GTLCore/Visitor_p.h"
 
 #include "Expression.h"
 
 using namespace GTLCore::AST;
 
-GlobalConstantDeclaration::GlobalConstantDeclaration( const GTLCore::ScopedName& _name, const GTLCore::Type* _type, Expression* _initialiser) :
-    m_name( _name), m_initialiser(_initialiser), m_variable( new GTLCore::VariableNG( _type, true ) ), m_type(_type)
+GlobalConstantDeclaration::GlobalConstantDeclaration( const GTLCore::ScopedName& _name, const GTLCore::Type* _type, Expression* _initialiser, bool _constant) :
+    m_name( _name), m_initialiser(_initialiser), m_variable( new GTLCore::VariableNG( _type, true ) ), m_type(_type), m_constant(_constant)
 {
+  GTL_ASSERT( _initialiser );
 }
 
+GlobalConstantDeclaration::GlobalConstantDeclaration( const GTLCore::ScopedName& _name, const GTLCore::Type* _type, const std::list< int >& sizes,bool _constant) :
+    m_name( _name), m_initialiser(0), m_variable( new GTLCore::VariableNG( _type, true ) ), m_type(_type), m_sizes(sizes), m_constant(_constant)
+{
+}
+
 GlobalConstantDeclaration::~GlobalConstantDeclaration()
 {
   delete m_initialiser;
@@ -58,8 +65,10 @@
   {
     value = _codeGenerator->convertConstantTo( m_initialiser->generateValue( gc, 0 ).constant(), m_initialiser->type() , m_type );
     GTL_ASSERT( value );
+  } else {
+    value = Visitor::getVisitorFor( m_type )->createStaticVariable( _module->llvmModule(), m_type, m_sizes );
   }
-  llvm::Value* pointer = new llvm::GlobalVariable( value ? value->getType() : m_type->d->type() , true, llvm::GlobalValue::InternalLinkage, value, "", _module->llvmModule() );
+  llvm::Value* pointer = new llvm::GlobalVariable( value ? value->getType() : m_type->d->type() , m_constant, llvm::GlobalValue::InternalLinkage, value, "", _module->llvmModule() );
   m_variable->initialise( gc, 0, pointer);
 }
 

Modified: trunk/OpenGTL/OpenGTL/GTLCore/AST/Tree.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/AST/Tree.h	2008-10-06 22:45:56 UTC (rev 431)
+++ trunk/OpenGTL/OpenGTL/GTLCore/AST/Tree.h	2008-10-08 11:26:00 UTC (rev 432)
@@ -41,7 +41,8 @@
      */
     class GlobalConstantDeclaration {
       public:
-        GlobalConstantDeclaration( const GTLCore::ScopedName& _name, const GTLCore::Type* _type, Expression* _initialiser);
+        GlobalConstantDeclaration( const GTLCore::ScopedName& _name, const GTLCore::Type* _type, Expression* _initialiser, bool _constant);
+        GlobalConstantDeclaration( const GTLCore::ScopedName& _name, const GTLCore::Type* _type, const std::list< int >& sizes,bool _constant);
         ~GlobalConstantDeclaration();
         GTLCore::ScopedName name() const { return m_name; }
         const Expression* initialiser() const { return m_initialiser; }
@@ -53,6 +54,8 @@
         Expression* m_initialiser;
         GTLCore::VariableNG* m_variable;
         const GTLCore::Type* m_type;
+        std::list< int > m_sizes;
+        bool m_constant;
     };
     /**
      * @internal

Modified: trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.cpp	2008-10-06 22:45:56 UTC (rev 431)
+++ trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.cpp	2008-10-08 11:26:00 UTC (rev 432)
@@ -126,7 +126,7 @@
           {
             reportError("Constant '" + scopedName.toString() + "' has allready been declared", currentToken());
           } else {
-            AST::GlobalConstantDeclaration* gcd = new AST::GlobalConstantDeclaration( scopedName, type , expression );
+            AST::GlobalConstantDeclaration* gcd = new AST::GlobalConstantDeclaration( scopedName, type , expression, true );
             variablesManager()->declareConstant( scopedName, gcd->variable() );
             tree()->append( gcd );
           }
@@ -343,6 +343,11 @@
     } else {
       expression = parseExpression( _constantExpression );
     }
+    if( not expression )
+    {
+      deleteAll( expressions_ );
+      return 0;
+    }
     expression = d->compiler->convertCenter()->createConvertExpression( expression, subtype );
     if( not expression )
     {

Modified: trunk/OpenGTL/OpenGTL/GTLCore/Type.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Type.h	2008-10-06 22:45:56 UTC (rev 431)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Type.h	2008-10-08 11:26:00 UTC (rev 432)
@@ -75,6 +75,7 @@
 #endif
     friend class Visitor;
     friend class VectorVisitor;
+    friend class StructureVisitor;
     friend class ExpressionResult;
     friend class ConvertCenter;
     friend std::ostream& operator<< (std::ostream& ostr, const Type& type);

Modified: trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp	2008-10-06 22:45:56 UTC (rev 431)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp	2008-10-08 11:26:00 UTC (rev 432)
@@ -24,6 +24,7 @@
 #include <llvm/Constants.h>
 #include <llvm/Function.h>
 #include <llvm/Instructions.h>
+#include <llvm/GlobalVariable.h>
 
 // GTLCore
 #include "CodeGenerator_p.h"
@@ -136,6 +137,11 @@
   return _currentBlock;
 }
 
+llvm::Constant* PrimitiveVisitor::createStaticVariable( llvm::Module*, const Type* type, const std::list< int>& /*_sizes*/ ) const
+{
+  return CodeGenerator::convertConstantTo( CodeGenerator::integerToConstant( 0), Type::Integer32, type );
+}
+
 //--------- ArrayVisitor ---------///
 
 ArrayVisitor::ArrayVisitor() : Visitor(  )
@@ -407,6 +413,41 @@
   return afterBlock;
 }
 
+llvm::Constant* ArrayVisitor::createStaticVariable( llvm::Module* _module, const Type* type, const std::list< int>& _sizes ) const
+{
+  // Init size
+  int currentSize = _sizes.front();
+  std::list< int> sizeAfter = _sizes;
+  sizeAfter.pop_front();
+ 
+  const GTLCore::Type* embedded = type->embeddedType();
+  //
+  std::vector<llvm::Constant*> initialisers;
+  // Initialize the count
+  initialisers.push_back( CodeGenerator::integerToConstant( 1 ) );
+  // Initialize the current size
+  initialisers.push_back( CodeGenerator::integerToConstant( currentSize ) );
+  // Initialize the data
+  const Visitor* visitor = Visitor::getVisitorFor( embedded );
+  std::vector<llvm::Constant*> arrayInitialisers;
+  for( int i = 0; i < currentSize; ++i)
+  {
+    arrayInitialisers.push_back( visitor->createStaticVariable( _module, embedded, sizeAfter ) );
+  }
+  // Initialize the pointer to the data
+  llvm::ArrayType* arrType = llvm::ArrayType::get(embedded->d->type(), currentSize);
+  llvm::Constant* arrayData = new llvm::GlobalVariable( arrType, false, llvm::GlobalValue::InternalLinkage,  llvm::ConstantArray::get(arrType ,arrayInitialisers), "", _module );
+  // Get the pointer to the data
+  
+  std::vector<llvm::Constant*> indices;
+  indices.push_back( CodeGenerator::integerToConstant( 0 ) );
+  indices.push_back( CodeGenerator::integerToConstant( 0 ) );
+  initialisers.push_back( llvm::ConstantExpr::getGetElementPtr(arrayData, &indices[0], indices.size() ) );
+  
+  GTL_DEBUG(* initialisers[2] << " " <<  *type->d->type());
+  return llvm::ConstantStruct::get( dynamic_cast<const llvm::StructType*>(type->d->type()), initialisers );
+}
+
 //--------- VectorVisitor ---------///
 
 
@@ -460,6 +501,11 @@
   return _currentBlock;
 }
 
+llvm::Constant* VectorVisitor::createStaticVariable( llvm::Module*, const Type* type, const std::list< int>& /*_sizes*/ ) const
+{
+  return llvm::ConstantVector::getAllOnesValue( dynamic_cast<const llvm::VectorType*>(type->d->type() ) );
+}
+
 //--------- StructureVisitor ---------///
 
 StructureVisitor::StructureVisitor() : Visitor( )
@@ -584,3 +630,16 @@
   }
   return _currentBlock;
 }
+
+llvm::Constant* StructureVisitor::createStaticVariable( llvm::Module* _module, const Type* type, const std::list< int>& /*_sizes */) const
+{
+  std::vector< llvm::Constant* > initialisers;
+  initialisers.push_back( CodeGenerator::integerToConstant( 1 ) );
+  for( uint i = 0; i < type->countStructDataMembers(); ++i)
+  {
+    const Type* type = type->structDataMember(i).type();
+    const Visitor* visitor = Visitor::getVisitorFor( type );
+    initialisers.push_back( visitor->createStaticVariable( _module, type, type->structDataMember(i).initialSizes() ) );
+  }
+  return llvm::ConstantStruct::get( dynamic_cast<const llvm::StructType*>(type->d->type()), initialisers );
+}

Modified: trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.h	2008-10-06 22:45:56 UTC (rev 431)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.h	2008-10-08 11:26:00 UTC (rev 432)
@@ -27,8 +27,10 @@
 class VisitorsFactory;
 
 namespace llvm {
+  class BasicBlock;
+  class Constant;
   class ErrorMessage;
-  class BasicBlock;
+  class Module;
   class Type;
   class Value;
 }
@@ -95,6 +97,10 @@
       virtual llvm::BasicBlock* initialise(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, const std::list< llvm::Value*>& _sizes, bool _allocatedInMemory) const = 0;
       virtual llvm::BasicBlock* cleanUp( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, llvm::Value* _donttouch, bool _allocatedInMemory, bool _ignoreCount, bool _deletePointer ) const = 0;
       virtual llvm::BasicBlock* mark( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, llvm::Value* increment ) const = 0;
+      /**
+       * Create a static variable. (ie static float v;)
+       */
+      virtual llvm::Constant* createStaticVariable( llvm::Module*, const Type* type, const std::list< int>& _sizes ) const = 0;
     public:
       /**
        * This function create the Visitor for the type given in argument
@@ -119,6 +125,7 @@
       virtual llvm::BasicBlock* initialise(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, const std::list< llvm::Value*>& _sizes, bool _allocatedInMemory) const;
       virtual llvm::BasicBlock* cleanUp( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, llvm::Value* _donttouch, bool _allocatedInMemory, bool _ignoreCount, bool _deletePointer ) const;
       virtual llvm::BasicBlock* mark( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, llvm::Value* increment ) const;
+      virtual llvm::Constant* createStaticVariable( llvm::Module*, const Type* type, const std::list< int>& _sizes ) const;
   };
   /**
    * @internal
@@ -137,6 +144,7 @@
       virtual llvm::BasicBlock* initialise( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, const std::list< llvm::Value*>& _sizes, bool _allocatedInMemory) const;
       virtual llvm::BasicBlock* cleanUp( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, llvm::Value* _donttouch, bool _allocatedInMemory, bool _ignoreCount, bool _deletePointer ) const;
       virtual llvm::BasicBlock* mark( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, llvm::Value* increment ) const;
+      virtual llvm::Constant* createStaticVariable( llvm::Module*, const Type* type, const std::list< int>& _sizes ) const;
     private:
       /**
        * Allow to access the size of an array.
@@ -172,6 +180,7 @@
       virtual llvm::BasicBlock* initialise( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, const std::list< llvm::Value*>& _sizes, bool _allocatedInMemory) const;
       virtual llvm::BasicBlock* cleanUp( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, llvm::Value* _donttouch, bool _allocatedInMemory, bool _ignoreCount, bool _deletePointer ) const;
       virtual llvm::BasicBlock* mark( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, llvm::Value* increment ) const;
+      virtual llvm::Constant* createStaticVariable( llvm::Module*, const Type* type, const std::list< int>& _sizes ) const;
   };
 
   /**
@@ -190,6 +199,7 @@
       virtual llvm::BasicBlock* initialise( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, const std::list< llvm::Value*>& _sizes, bool _allocatedInMemory) const;
       virtual llvm::BasicBlock* cleanUp( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, llvm::Value* _donttouch, bool _allocatedInMemory, bool _ignoreCount, bool _deletePointer ) const;
       virtual llvm::BasicBlock* mark( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const Type* _pointerType, llvm::Value* increment ) const;
+      virtual llvm::Constant* createStaticVariable( llvm::Module*, const Type* type, const std::list< int>& _sizes ) const;
     private:
       llvm::Value* pointerToValue(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, int index ) const;
   };

Modified: trunk/OpenGTL/OpenShiva/OpenShiva/Parser_p.cpp
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/Parser_p.cpp	2008-10-06 22:45:56 UTC (rev 431)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/Parser_p.cpp	2008-10-08 11:26:00 UTC (rev 432)
@@ -219,7 +219,7 @@
       {
         reportError("Constant '" + scopedName.toString() + "' has allready been declared", currentToken());
       } else {
-        AST::GlobalConstantDeclaration* gcd = new AST::GlobalConstantDeclaration( scopedName, type , 0 );
+        AST::GlobalConstantDeclaration* gcd = new AST::GlobalConstantDeclaration( scopedName, type , memberArraySize, false );
         variablesManager()->declareConstant( scopedName, gcd->variable() );
         tree()->append( gcd );
       }

Modified: trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.cpp
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.cpp	2008-10-06 22:45:56 UTC (rev 431)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.cpp	2008-10-08 11:26:00 UTC (rev 432)
@@ -130,3 +130,9 @@
   GTLCore::CodeGenerator::incrementCountFieldOf( _currentBlock, _pointer, _increment );
   return _currentBlock;
 }
+
+llvm::Constant* PixelVisitor::createStaticVariable( llvm::Module* /*_module*/, const GTLCore::Type* /*type*/, const std::list< int>& /*_sizes */) const
+{
+  GTL_ABORT("Unimplemented");
+  return 0; 
+}

Modified: trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.h
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.h	2008-10-06 22:45:56 UTC (rev 431)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/PixelVisitor_p.h	2008-10-08 11:26:00 UTC (rev 432)
@@ -45,6 +45,7 @@
       virtual llvm::BasicBlock* initialise(GTLCore::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( GTLCore::GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _pointerType, llvm::Value* _donttouch, bool _allocatedInMemory, bool _ignoreCount, bool _deletePointer ) const;
       virtual llvm::BasicBlock* mark( GTLCore::GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _pointerType, llvm::Value* increment ) const;
+      virtual llvm::Constant* createStaticVariable( llvm::Module* _module, const GTLCore::Type* type, const std::list< int>& _sizes ) const;
   };
   
 }

Modified: trunk/OpenGTL/OpenShiva/tests/imagegenerators/raytracer.shiva
===================================================================
--- trunk/OpenGTL/OpenShiva/tests/imagegenerators/raytracer.shiva	2008-10-06 22:45:56 UTC (rev 431)
+++ trunk/OpenGTL/OpenShiva/tests/imagegenerators/raytracer.shiva	2008-10-08 11:26:00 UTC (rev 432)
@@ -328,7 +328,7 @@
                 // finally, blend our color into this pixel
 
                 lightVal = (sphereMaterial.x + float(shadowTest)*(diffuse*sphereMaterial.y + specular*sphereMaterial.z));
-                dst += colorScale*lightVal*sphereColor;
+                dst.data += colorScale*lightVal*sphereColor;
                 
                 // reflection
                 if(sphereMaterial.w > 0.0)

Deleted: trunk/OpenGTL/TODO
===================================================================
--- trunk/OpenGTL/TODO	2008-10-06 22:45:56 UTC (rev 431)
+++ trunk/OpenGTL/TODO	2008-10-08 11:26:00 UTC (rev 432)
@@ -1,17 +0,0 @@
-Blocker for 1.0:
- * 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
-
-Important TODO
- * access to vector using specific vector instructions
- * AccessorExpression are leaked
-
-Other:
- * Generate a Doxygen file


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