[opengtl-commits] [424] introduce an untested CoumpoundExpression and move ConstantCoumpoundExpression in its own file

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


Revision: 424
Author:   cyrille
Date:     2008-10-06 13:34:22 +0200 (Mon, 06 Oct 2008)

Log Message:
-----------
introduce an untested CoumpoundExpression and move ConstantCoumpoundExpression in its own file

Modified Paths:
--------------
    trunk/OpenGTL/OpenGTL/GTLCore/AST/CoumpoundExpression.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/AST/CoumpoundExpression.h
    trunk/OpenGTL/OpenGTL/GTLCore/CMakeLists.txt
    trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/Type.h

Added Paths:
-----------
    trunk/OpenGTL/OpenGTL/GTLCore/AST/ConstantCoumpoundExpression.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/AST/ConstantCoumpoundExpression.h


Copied: trunk/OpenGTL/OpenGTL/GTLCore/AST/ConstantCoumpoundExpression.cpp (from rev 422, trunk/OpenGTL/OpenGTL/GTLCore/AST/CoumpoundExpression.cpp)
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/AST/ConstantCoumpoundExpression.cpp	                        (rev 0)
+++ trunk/OpenGTL/OpenGTL/GTLCore/AST/ConstantCoumpoundExpression.cpp	2008-10-06 11:34:22 UTC (rev 424)
@@ -0,0 +1,118 @@
+/*
+ *  Copyright (c) 2008 Cyrille Berger <cberger@xxxxxxxxxxx>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include "ConstantCoumpoundExpression.h"
+
+#include <llvm/Constants.h>
+#include <llvm/DerivedTypes.h>
+#include <llvm/GlobalVariable.h>
+#include <llvm/Instructions.h>
+
+#include "../CodeGenerator_p.h"
+#include "../Debug.h"
+#include "../Type.h"
+#include "../Type_p.h"
+#include "../ExpressionResult_p.h"
+
+using namespace GTLCore::AST;
+
+ConstantCoumpoundExpression::ConstantCoumpoundExpression( const GTLCore::Type* _type, const std::vector<Expression*>& _expressions) : m_expressions(_expressions), m_type(_type)
+{
+  GTL_ASSERT(_type);
+}
+
+ConstantCoumpoundExpression::~ConstantCoumpoundExpression( )
+{
+  for( std::vector<Expression*>::iterator it = m_expressions.begin();
+       it != m_expressions.end(); ++it)
+  {
+    delete *it;
+  }
+}
+
+const GTLCore::Type* ConstantCoumpoundExpression::type() const
+{
+  return m_type;
+}
+
+bool ConstantCoumpoundExpression::isConstant() const
+{
+  return m_constant;
+}
+
+GTLCore::ExpressionResult ConstantCoumpoundExpression::generateValue( GenerationContext& _gc, llvm::BasicBlock* _bb ) const
+{
+  GTL_DEBUG( m_type->dataType() << " " << Type::ARRAY << " " << Type::STRUCTURE );
+  if( m_type->dataType() == Type::ARRAY or m_type->dataType() == Type::VECTOR)
+  {
+    const GTLCore::Type* arrayType = m_type->embeddedType();
+    std::vector< llvm::Constant* > members;
+    for(  std::vector<Expression*>::const_iterator it = m_expressions.begin();
+       it != m_expressions.end(); ++it)
+    {
+      members.push_back( _gc.codeGenerator()->convertConstantTo( (*it)->generateValue( _gc, _bb).constant(), (*it)->type(), arrayType ) );
+    }
+    if( m_type->dataType() == Type::ARRAY )
+    {
+      std::vector<llvm::Constant*> arrayStruct;
+      arrayStruct.push_back( _gc.codeGenerator()->integerToConstant( 0 ) );
+      arrayStruct.push_back( _gc.codeGenerator()->integerToConstant( members.size() ) );
+      llvm::Constant* constant = llvm::ConstantArray::get(
+                    llvm::ArrayType::get( arrayType->d->type(), members.size()), members );
+      llvm::GlobalVariable* gvar = new llvm::GlobalVariable( constant->getType(), true, llvm::GlobalValue::ExternalLinkage, constant, "", _gc.llvmModule() );
+      
+      llvm::Constant* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); // Access the structure of the array
+      llvm::Constant *Ops[] = {zero, zero };
+      llvm::Constant* iptr = llvm::ConstantExpr::getGetElementPtr( gvar, Ops, 2);
+      
+      arrayStruct.push_back( iptr );
+      
+      const llvm::StructType* structType = dynamic_cast<const llvm::StructType*>( m_type->d->type() );
+      GTL_ASSERT( structType );
+      
+      return GTLCore::ExpressionResult(
+            llvm::ConstantStruct::get( structType, arrayStruct ), type() );
+    } else if( m_type->dataType() == Type::VECTOR )
+    {
+      const llvm::VectorType* vectorType = dynamic_cast<const llvm::VectorType*>( m_type->d->type() );
+      GTL_ASSERT( vectorType );
+      llvm::Constant* constant = llvm::ConstantVector::get( vectorType, members );
+      GTL_DEBUG( "Coumpound constant = " << *constant << " type = " << *m_type );
+      return GTLCore::ExpressionResult( constant, m_type );
+    }
+  } else if( m_type->dataType() == Type::STRUCTURE )
+  {
+    std::vector<llvm::Constant*> members;
+    members.push_back( CodeGenerator::integerToConstant( 0 ) ); // GC constant
+    for( uint i = 0; i < m_expressions.size(); ++i)
+    {
+      members.push_back( _gc.codeGenerator()->convertConstantTo( m_expressions[i]->generateValue( _gc, _bb).constant(), m_expressions[i]->type(), m_type->structDataMember(i).type() ) );
+    }
+    const llvm::StructType* structType = dynamic_cast<const llvm::StructType*>( m_type->d->type() );
+    return GTLCore::ExpressionResult(
+          llvm::ConstantStruct::get( structType, members ), type() );
+  }
+  GTL_ABORT("Can't generate coumpound value, neither array, neither vector.");
+  return GTLCore::ExpressionResult();
+}
+
+void ConstantCoumpoundExpression::markAsReturnExpression()
+{
+  GTL_ABORT("ConstantCoumpoundExpression can't be in a return statement");
+}


Property changes on: trunk/OpenGTL/OpenGTL/GTLCore/AST/ConstantCoumpoundExpression.cpp
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:mergeinfo
   + 
Name: svn:eol-style
   + native

Copied: trunk/OpenGTL/OpenGTL/GTLCore/AST/ConstantCoumpoundExpression.h (from rev 422, trunk/OpenGTL/OpenGTL/GTLCore/AST/CoumpoundExpression.h)
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/AST/ConstantCoumpoundExpression.h	                        (rev 0)
+++ trunk/OpenGTL/OpenGTL/GTLCore/AST/ConstantCoumpoundExpression.h	2008-10-06 11:34:22 UTC (rev 424)
@@ -0,0 +1,49 @@
+/*
+ *  Copyright (c) 2008 Cyrille Berger <cberger@xxxxxxxxxxx>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifndef _AST_COUMPOUND_EXPRESSION_H_
+#define _AST_COUMPOUND_EXPRESSION_H_
+
+#include <vector>
+#include "GTLCore/AST/Expression.h"
+
+namespace GTLCore {
+  namespace AST {
+    /**
+     * @internal
+     * @ingroup GTLCore_AST
+     */
+    class ConstantCoumpoundExpression : public Expression {
+      public:
+        ConstantCoumpoundExpression( const GTLCore::Type* _type, const std::vector<Expression*>& _expressions);
+        ~ConstantCoumpoundExpression( );
+        virtual const GTLCore::Type* type() const;
+        virtual bool isConstant() const;
+        virtual GTLCore::ExpressionResult generateValue( GenerationContext& _gc, llvm::BasicBlock* _bb ) const;
+        int size() { return m_expressions.size(); }
+        void markAsReturnExpression();
+      private:
+        std::vector<Expression*> m_expressions;
+        const GTLCore::Type* m_type;
+        bool m_constant;
+    };
+  }
+}
+
+#endif


Property changes on: trunk/OpenGTL/OpenGTL/GTLCore/AST/ConstantCoumpoundExpression.h
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:mergeinfo
   + 
Name: svn:eol-style
   + native

Modified: trunk/OpenGTL/OpenGTL/GTLCore/AST/CoumpoundExpression.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/AST/CoumpoundExpression.cpp	2008-10-06 11:33:47 UTC (rev 423)
+++ trunk/OpenGTL/OpenGTL/GTLCore/AST/CoumpoundExpression.cpp	2008-10-06 11:34:22 UTC (rev 424)
@@ -32,12 +32,12 @@
 
 using namespace GTLCore::AST;
 
-ConstantCoumpoundExpression::ConstantCoumpoundExpression( const GTLCore::Type* _type, const std::vector<Expression*>& _expressions) : m_expressions(_expressions), m_type(_type)
+CoumpoundExpression::CoumpoundExpression( const GTLCore::Type* _type, const std::vector<Expression*>& _expressions) : m_expressions(_expressions), m_type(_type)
 {
   GTL_ASSERT(_type);
 }
 
-ConstantCoumpoundExpression::~ConstantCoumpoundExpression( )
+CoumpoundExpression::~CoumpoundExpression( )
 {
   for( std::vector<Expression*>::iterator it = m_expressions.begin();
        it != m_expressions.end(); ++it)
@@ -46,73 +46,47 @@
   }
 }
 
-const GTLCore::Type* ConstantCoumpoundExpression::type() const
+const GTLCore::Type* CoumpoundExpression::type() const
 {
   return m_type;
 }
 
-bool ConstantCoumpoundExpression::isConstant() const
+bool CoumpoundExpression::isConstant() const
 {
   return m_constant;
 }
 
-GTLCore::ExpressionResult ConstantCoumpoundExpression::generateValue( GenerationContext& _gc, llvm::BasicBlock* _bb ) const
+GTLCore::ExpressionResult CoumpoundExpression::generateValue( GenerationContext& _gc, llvm::BasicBlock* _bb ) const
 {
-  GTL_DEBUG( m_type->dataType() << " " << Type::ARRAY << " " << Type::STRUCTURE );
-  if( m_type->dataType() == Type::ARRAY or m_type->dataType() == Type::VECTOR)
+  GTL_DEBUG( m_type->dataType() << " " << Type::ARRAY << " " << Type::STRUCTURE << " " << Type::VECTOR );
+  
+  switch( m_type->dataType() )
   {
-    const GTLCore::Type* arrayType = m_type->embeddedType();
-    std::vector< llvm::Constant* > members;
-    for(  std::vector<Expression*>::const_iterator it = m_expressions.begin();
-       it != m_expressions.end(); ++it)
+    case Type::VECTOR:
     {
-      members.push_back( _gc.codeGenerator()->convertConstantTo( (*it)->generateValue( _gc, _bb).constant(), (*it)->type(), arrayType ) );
+      llvm::Value* resultLoad = llvm::ConstantVector::get( static_cast<const llvm::VectorType*>( m_type->d->type()), std::vector<llvm::Constant*>() );
+      // Affect the same value to each element of the vector
+      for(std::size_t i = 0; i < m_expressions.size(); ++i)
+      {
+        llvm::Value* val = m_expressions[i]->generateValue( _gc, _bb ).value();
+        resultLoad = llvm::InsertElementInst::Create( resultLoad, val, i, "", _bb );
+      }
+      return GTLCore::ExpressionResult( resultLoad, m_type );
     }
-    if( m_type->dataType() == Type::ARRAY )
-    {
-      std::vector<llvm::Constant*> arrayStruct;
-      arrayStruct.push_back( _gc.codeGenerator()->integerToConstant( 0 ) );
-      arrayStruct.push_back( _gc.codeGenerator()->integerToConstant( members.size() ) );
-      llvm::Constant* constant = llvm::ConstantArray::get(
-                    llvm::ArrayType::get( arrayType->d->type(), members.size()), members );
-      llvm::GlobalVariable* gvar = new llvm::GlobalVariable( constant->getType(), true, llvm::GlobalValue::ExternalLinkage, constant, "", _gc.llvmModule() );
-      
-      llvm::Constant* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); // Access the structure of the array
-      llvm::Constant *Ops[] = {zero, zero };
-      llvm::Constant* iptr = llvm::ConstantExpr::getGetElementPtr( gvar, Ops, 2);
-      
-      arrayStruct.push_back( iptr );
-      
-      const llvm::StructType* structType = dynamic_cast<const llvm::StructType*>( m_type->d->type() );
-      GTL_ASSERT( structType );
-      
-      return GTLCore::ExpressionResult(
-            llvm::ConstantStruct::get( structType, arrayStruct ), type() );
-    } else if( m_type->dataType() == Type::VECTOR )
-    {
-      const llvm::VectorType* vectorType = dynamic_cast<const llvm::VectorType*>( m_type->d->type() );
-      GTL_ASSERT( vectorType );
-      llvm::Constant* constant = llvm::ConstantVector::get( vectorType, members );
-      GTL_DEBUG( "Coumpound constant = " << *constant << " type = " << *m_type );
-      return GTLCore::ExpressionResult( constant, m_type );
-    }
-  } else if( m_type->dataType() == Type::STRUCTURE )
-  {
-    std::vector<llvm::Constant*> members;
-    members.push_back( CodeGenerator::integerToConstant( 0 ) ); // GC constant
-    for( uint i = 0; i < m_expressions.size(); ++i)
-    {
-      members.push_back( _gc.codeGenerator()->convertConstantTo( m_expressions[i]->generateValue( _gc, _bb).constant(), m_expressions[i]->type(), m_type->structDataMember(i).type() ) );
-    }
-    const llvm::StructType* structType = dynamic_cast<const llvm::StructType*>( m_type->d->type() );
-    return GTLCore::ExpressionResult(
-          llvm::ConstantStruct::get( structType, members ), type() );
+      break;
+    case Type::ARRAY:
+      GTL_ABORT("Unimplemented");
+    case Type::STRUCTURE:
+      GTL_ABORT("Unimplemented");
+    default:
+      GTL_ABORT("Unsupported");
   }
-  GTL_ABORT("Can't generate coumpound value, neither array, neither vector.");
-  return GTLCore::ExpressionResult();
 }
 
-void ConstantCoumpoundExpression::markAsReturnExpression()
+void CoumpoundExpression::markAsReturnExpression()
 {
-  GTL_ABORT("ConstantCoumpoundExpression can't be in a return statement");
+  for(std::size_t i = 0; i < m_expressions.size(); ++i)
+  {
+    m_expressions[i]->markAsReturnExpression();
+  }
 }

Modified: trunk/OpenGTL/OpenGTL/GTLCore/AST/CoumpoundExpression.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/AST/CoumpoundExpression.h	2008-10-06 11:33:47 UTC (rev 423)
+++ trunk/OpenGTL/OpenGTL/GTLCore/AST/CoumpoundExpression.h	2008-10-06 11:34:22 UTC (rev 424)
@@ -29,10 +29,10 @@
      * @internal
      * @ingroup GTLCore_AST
      */
-    class ConstantCoumpoundExpression : public Expression {
+    class CoumpoundExpression : public Expression {
       public:
-        ConstantCoumpoundExpression( const GTLCore::Type* _type, const std::vector<Expression*>& _expressions);
-        ~ConstantCoumpoundExpression( );
+        CoumpoundExpression( const GTLCore::Type* _type, const std::vector<Expression*>& _expressions);
+        ~CoumpoundExpression( );
         virtual const GTLCore::Type* type() const;
         virtual bool isConstant() const;
         virtual GTLCore::ExpressionResult generateValue( GenerationContext& _gc, llvm::BasicBlock* _bb ) const;

Modified: trunk/OpenGTL/OpenGTL/GTLCore/CMakeLists.txt
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/CMakeLists.txt	2008-10-06 11:33:47 UTC (rev 423)
+++ trunk/OpenGTL/OpenGTL/GTLCore/CMakeLists.txt	2008-10-06 11:34:22 UTC (rev 424)
@@ -31,6 +31,7 @@
   AST/BinaryExpression.cpp
   AST/ConvertExpression.cpp
   AST/CoumpoundExpression.cpp
+  AST/ConstantCoumpoundExpression.cpp
   AST/GarbageCollectionStatement.cpp
   AST/Expression.cpp
   AST/FunctionDeclaration.cpp

Modified: trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.cpp	2008-10-06 11:33:47 UTC (rev 423)
+++ trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.cpp	2008-10-06 11:34:22 UTC (rev 424)
@@ -34,7 +34,7 @@
 
 #include "AST/AccessorExpression.h"
 #include "AST/BinaryExpression.h"
-#include "AST/CoumpoundExpression.h"
+#include "AST/ConstantCoumpoundExpression.h"
 #include "AST/Expression.h"
 #include "AST/FunctionDeclaration.h"
 #include "AST/Tree.h"

Modified: trunk/OpenGTL/OpenGTL/GTLCore/Type.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Type.h	2008-10-06 11:33:47 UTC (rev 423)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Type.h	2008-10-06 11:34:22 UTC (rev 424)
@@ -43,6 +43,7 @@
 namespace GTLCore {
   class Parameter;
   namespace AST {
+    class CoumpoundExpression;
     class ConstantCoumpoundExpression;
     class FunctionCallExpression;
     class FunctionDeclaration;
@@ -55,6 +56,7 @@
     friend class ArrayAccessor;
     friend class ArrayVisitor;
     friend class AST::ConstantCoumpoundExpression;
+    friend class AST::CoumpoundExpression;
     friend class AST::FunctionCallExpression;
     friend class AST::FunctionDeclaration;
     friend class OpenCTL::Compiler;


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