[opengtl-commits] [219] use visitors to access an element at a given index (i.e. |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/opengtl-commits Archives
]
Revision: 219
Author: cyrille
Date: 2008-06-22 18:10:57 +0200 (Sun, 22 Jun 2008)
Log Message:
-----------
use visitors to access an element at a given index (i.e. for generating stuff like 'hello[3] = 2')
Modified Paths:
--------------
trunk/OpenGTL/OpenGTL/GTLCore/AST/AccessorExpression.cpp
trunk/OpenGTL/OpenGTL/GTLCore/CodeGenerator_p.cpp
trunk/OpenGTL/OpenGTL/GTLCore/Type_p.cpp
trunk/OpenGTL/OpenGTL/GTLCore/Type_p.h
trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp
trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.h
Modified: trunk/OpenGTL/OpenGTL/GTLCore/AST/AccessorExpression.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/AST/AccessorExpression.cpp 2008-06-22 14:08:43 UTC (rev 218)
+++ trunk/OpenGTL/OpenGTL/GTLCore/AST/AccessorExpression.cpp 2008-06-22 16:10:57 UTC (rev 219)
@@ -112,8 +112,11 @@
llvm::Value* ArrayAccessorExpression::pointer(GenerationContext& _gc, llvm::BasicBlock* _bb) const
{
GTL_DEBUG("ArrayAccessorExpression::pointer");
+ llvm::Value* ptr_ = m_parent->pointer( _gc, _bb);
+ GTL_ASSERT(ptr_);
+ const Visitor* visitor = Visitor::getVisitorFor( m_parent->type() );
llvm::Value* index = m_index->generateValue(_gc, _bb).value();
- return _gc.codeGenerator()->accessArrayValue(_bb, m_parent->pointer( _gc, _bb) , index);
+ return visitor->pointerToIndex(_gc, _bb, ptr_, m_parent->type(), index );
}
bool ArrayAccessorExpression::isConstant() const
@@ -174,5 +177,6 @@
llvm::Value* FunctionMemberAccessorExpression::pointer(GenerationContext& _gc, llvm::BasicBlock* bb) const
{
+ GTL_ABORT("no pointer for function member accessor");
return 0;
}
Modified: trunk/OpenGTL/OpenGTL/GTLCore/CodeGenerator_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/CodeGenerator_p.cpp 2008-06-22 14:08:43 UTC (rev 218)
+++ trunk/OpenGTL/OpenGTL/GTLCore/CodeGenerator_p.cpp 2008-06-22 16:10:57 UTC (rev 219)
@@ -604,6 +604,7 @@
llvm::Value* CodeGenerator::accessArrayValue( llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, llvm::Value* _index )
{
+ GTL_DEBUG( *_pointer << " " << *_index );
std::vector<llvm::Value*> indexes;
indexes.push_back( llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); // Access the structure of the array
indexes.push_back( llvm::ConstantInt::get(llvm::Type::Int32Ty, 1)); // Access the size of the array
Modified: trunk/OpenGTL/OpenGTL/GTLCore/Type_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Type_p.cpp 2008-06-22 14:08:43 UTC (rev 218)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Type_p.cpp 2008-06-22 16:10:57 UTC (rev 219)
@@ -187,7 +187,7 @@
return m_visitor;
}
-void Type::Private::setVisitor( Visitor* _visitor )
+void Type::Private::setVisitor( const Visitor* _visitor )
{
GTL_ASSERT( m_visitor == 0 );
m_visitor = _visitor;
Modified: trunk/OpenGTL/OpenGTL/GTLCore/Type_p.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Type_p.h 2008-06-22 14:08:43 UTC (rev 218)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Type_p.h 2008-06-22 16:10:57 UTC (rev 219)
@@ -88,7 +88,7 @@
*/
const Type::StructFunctionMember* privateFunctionMember( const GTLCore::String&) const;
const Visitor* visitor() const;
- void setVisitor( Visitor* );
+ void setVisitor( const Visitor* );
private:
void setType( const llvm::Type* _type);
const llvm::Type* m_type;
@@ -99,7 +99,7 @@
std::vector<StructDataMember>* structDataMembers;
std::vector<Type::StructFunctionMember>* structFunctionMembers;
std::vector<Type::StructFunctionMember>* structPrivateFunctionMembers;
- Visitor* m_visitor;
+ const Visitor* m_visitor;
};
}
Modified: trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp 2008-06-22 14:08:43 UTC (rev 218)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.cpp 2008-06-22 16:10:57 UTC (rev 219)
@@ -65,6 +65,7 @@
const Visitor* Visitor::getVisitorFor(const GTLCore::Type* _type)
{
+ GTL_ASSERT( _type );
if( _type->d->visitor() )
{
return _type->d->visitor();
@@ -79,20 +80,6 @@
return 0;
}
-// llvm::BasicBlock* Visitor::initialiseValue( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, llvm::Value* _value, const GTLCore::Type* _valueType )
-// {
-// GTL_ASSERT(not m_initialisedValue);
-// m_initialisedValue = true;
-// if( _value )
-// {
-// bool constant = m_constant;
-// m_constant = false;
-// _currentBlock = set( _generationContext, _currentBlock, _pointer, _value, _valueType );
-// m_constant = constant;
-// }
-// return _currentBlock;
-// }
-
//--------- PrimitiveVisitor ---------///
PrimitiveVisitor::PrimitiveVisitor() : Visitor( )
@@ -103,6 +90,11 @@
{
}
+llvm::Value* PrimitiveVisitor::pointerToIndex(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _type, llvm::Value* _index) const
+{
+ GTL_ABORT("Primitive type doesn't allow access using indexes"); //TODO except vectors
+}
+
llvm::Value* PrimitiveVisitor::get( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer) const
{
return new llvm::LoadInst( _pointer, "", _currentBlock);
@@ -133,16 +125,11 @@
{
}
-#if 0
-llvm::Value* ArrayVisitor::pointerToValue(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock,
- llvm::Value* _pointer, MemberArray* _ma )
+llvm::Value* ArrayVisitor::pointerToIndex(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _type, llvm::Value* _index) const
{
- return _generationContext.codeGenerator()->accessArrayValue( _currentBlock, _pointer, _ma->index->generateValue( _generationContext, _currentBlock).value() );
+ return _generationContext.codeGenerator()->accessArrayValue( _currentBlock, _pointer, _index);
}
-#endif
-
-
llvm::Value* ArrayVisitor::get( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock,
llvm::Value* _pointer ) const
{
@@ -285,26 +272,11 @@
return new llvm::GetElementPtrInst( _pointer, indexes.begin(), indexes.end(), "", _currentBlock);
}
-#if 0
-
-int StructureVisitor::memberToIndex(MemberArray* _ma)
+llvm::Value* StructureVisitor::pointerToIndex(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _type, llvm::Value* _index) const
{
- const std::vector<GTLCore::Type::StructDataMember>* sm = type()->structDataMembers();
- GTL_ASSERT(sm);
- int count = 0;
- for( std::vector<GTLCore::Type::StructDataMember>::const_iterator it = sm->begin();
- it != sm->end(); ++it, ++count)
- {
- if( it->name() == _ma->identifier.name() )
- {
- return count;
- }
- }
- return -1;
+ GTL_ABORT("Structure doesn't allow access using indexes");
}
-#endif
-
llvm::Value* StructureVisitor::get( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer ) const
{
return _pointer;
Modified: trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.h 2008-06-22 14:08:43 UTC (rev 218)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Visitor_p.h 2008-06-22 16:10:57 UTC (rev 219)
@@ -52,6 +52,11 @@
Visitor();
virtual ~Visitor();
/**
+ * Allow to access to the element of an object through an index, used for vectors,
+ * arrays and some stucture (like pixels).
+ */
+ virtual llvm::Value* pointerToIndex(GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _type, llvm::Value* _index) const = 0;
+ /**
* Allow to get the value
* @param _errMsg a pointer to a pointer (usually initialised to 0), the pointer will be
* set if an error occurs with the content of the error
@@ -100,6 +105,7 @@
PrimitiveVisitor();
virtual ~PrimitiveVisitor();
public:
+ 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) const;
@@ -115,6 +121,7 @@
ArrayVisitor();
virtual ~ArrayVisitor();
public:
+ 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) const;
virtual llvm::BasicBlock* initialise( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _pointerType, const std::list< llvm::Value*>& _sizes) const;
@@ -150,6 +157,7 @@
StructureVisitor( );
virtual ~StructureVisitor();
public:
+ 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) const;
virtual llvm::BasicBlock* initialise( GenerationContext& _generationContext, llvm::BasicBlock* _currentBlock, llvm::Value* _pointer, const GTLCore::Type* _pointerType, const std::list< llvm::Value*>& _sizes) const;