[opengtl-commits] [438] support for the C functions in shiva

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


Revision: 438
Author:   cyrille
Date:     2008-10-10 00:33:17 +0200 (Fri, 10 Oct 2008)

Log Message:
-----------
support for the C functions in shiva

Modified Paths:
--------------
    trunk/OpenGTL/OpenCTL/OpenCTL/compiler/Compiler.cpp
    trunk/OpenGTL/OpenCTL/OpenCTL/compiler/Compiler.h
    trunk/OpenGTL/OpenGTL/GTLCore/CompilerBase_p.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/CompilerBase_p.h
    trunk/OpenGTL/OpenGTL/GTLCore/Function.h
    trunk/OpenGTL/OpenShiva/OpenShiva/Compiler_p.cpp
    trunk/OpenGTL/OpenShiva/tests/imagegenerators/raytracer.shiva


Modified: trunk/OpenGTL/OpenCTL/OpenCTL/compiler/Compiler.cpp
===================================================================
--- trunk/OpenGTL/OpenCTL/OpenCTL/compiler/Compiler.cpp	2008-10-09 22:02:03 UTC (rev 437)
+++ trunk/OpenGTL/OpenCTL/OpenCTL/compiler/Compiler.cpp	2008-10-09 22:33:17 UTC (rev 438)
@@ -24,7 +24,6 @@
 #include <map>
 #include <sstream>
 #include <float.h>
-#include <cstdarg>
 #include <climits>
 
 // LLVM
@@ -68,7 +67,6 @@
   LexerNG* lexer;
   ParserNG* parser;
   int lineNumber;
-  std::vector<GTLCore::Function*> functionsToDelete; ///< List of functions which are not visible in the generated module (like functions coming from an imported module, or standard library functions, and that should be removed at the end of compilation
   GTLCore::String errorMessage;
   std::list< GTLCore::String > importedModules;
   GTLCore::CodeGenerator* codeGenerator;
@@ -106,24 +104,6 @@
   return u.f;
 }
 
-void Compiler::createStdLibFunction(const GTLCore::String& _name, const GTLCore::String& _symbolName, const GTLCore::Type* retType, int count, ...)
-{
-  std::vector<GTLCore::Parameter> arguments;
-  va_list argp;
-  va_start(argp, count);
-  for(int i = 0; i < count; ++i)
-  {
-    const GTLCore::Type* type = va_arg(argp, const GTLCore::Type*);
-    arguments.push_back(GTLCore::Parameter("", type, false, false, GTLCore::Value() ) );
-  }
-  va_end(argp);
-  GTLCore::Function* function = GTLCore::Function::Private::createExternalFunction( d->module, _name, _symbolName, retType, arguments );
-  bool success = declareFunction(GTLCore::ScopedName("", _name), function);
-  OCTL_ASSERT( success );
-  UNUSED( success );
-  d->functionsToDelete.push_back(function);
-}
-
 bool Compiler::compile(const GTLCore::String& sourceCode, const GTLCore::String& moduleName, GTLCore::ModuleData* moduleData)
 {
   OCTL_DEBUG("Compile: " << moduleName << " : " << sourceCode);
@@ -136,7 +116,8 @@
   d->module = moduleData->llvmModule();
   d->moduleData = moduleData;
   d->codeGenerator = new GTLCore::CodeGenerator( moduleData );
-
+  setModuleData(d->moduleData);
+  
   // Create Standard Library functions
   // CtlStdLib functions (except print which get a special treatement)
   createStdLibFunction( "assert", "assert", GTLCore::Type::Void, 1, GTLCore::Type::Boolean);
@@ -164,7 +145,7 @@
   createStdLibFunction( "sqrt", "sqrtf", GTLCore::Type::Float, 1, GTLCore::Type::Float);
   createStdLibFunction( "fabs", "fabsf", GTLCore::Type::Float, 1, GTLCore::Type::Float);
   createStdLibFunction( "floor", "floorf", GTLCore::Type::Float, 1, GTLCore::Type::Float);
-  createStdLibFunction( "fmod", "fmodf", GTLCore::Type::Float, 1, GTLCore::Type::Float);
+  createStdLibFunction( "fmod", "fmodf", GTLCore::Type::Float, 2, GTLCore::Type::Float, GTLCore::Type::Float);
   
   if( moduleName != "ctlstdlib")
   { // of course you don't want to import the standard library when building the standard library
@@ -205,10 +186,9 @@
   
   delete tree;
 
-  for(std::vector<GTLCore::Function*>::iterator it = d->functionsToDelete.begin();
-      it != d->functionsToDelete.end(); ++it)
+  foreach(GTLCore::Function* function, functionsToDelete())
   {
-    delete *it;
+    delete function;
   }
   delete d->lexer;
   d->lexer = 0;
@@ -279,7 +259,7 @@
       data->setModule( d->module );
       GTLCore::Function* function = new GTLCore::Function( (*it)->name(), (*it)->returnType(), data);
       declareFunction( (*it)->name(), function );
-      d->functionsToDelete.push_back( function );
+      functionsToDelete().push_back( function );
     }
   }
   return m;

Modified: trunk/OpenGTL/OpenCTL/OpenCTL/compiler/Compiler.h
===================================================================
--- trunk/OpenGTL/OpenCTL/OpenCTL/compiler/Compiler.h	2008-10-09 22:02:03 UTC (rev 437)
+++ trunk/OpenGTL/OpenCTL/OpenCTL/compiler/Compiler.h	2008-10-09 22:33:17 UTC (rev 438)
@@ -62,9 +62,6 @@
     public:
       GTLCore::AST::Expression* standardConstant( const GTLCore::String& _name );
     private:
-      // Standard library function generation
-      void createStdLibFunction(const GTLCore::String& _name, const GTLCore::String& _symbolName, const GTLCore::Type* retType, int count, ...);
-    private:
       struct Private;
       Private* const d;
   };

Modified: trunk/OpenGTL/OpenGTL/GTLCore/CompilerBase_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/CompilerBase_p.cpp	2008-10-09 22:02:03 UTC (rev 437)
+++ trunk/OpenGTL/OpenGTL/GTLCore/CompilerBase_p.cpp	2008-10-09 22:33:17 UTC (rev 438)
@@ -26,14 +26,22 @@
 #include "ErrorMessage.h"
 #include "ScopedName.h"
 #include "Function.h"
+#include "GTLCore/Function_p.h"
+#include "Value.h"
+#include "Macros_p.h"
+#include "ModuleData_p.h"
 
+#include <cstdarg>
+
 using namespace GTLCore;
 
 struct CompilerBase::Private {
   GTLCore::String moduleName;
   std::list<ErrorMessage> errorMessages;
   std::map< GTLCore::ScopedName, GTLCore::Function* > functions;
+  std::list<GTLCore::Function*> functionsToDelete;
   ConvertCenter* convertCenter;
+  llvm::Module* module;
 };
 
 CompilerBase::CompilerBase() : d(new Private)
@@ -117,3 +125,30 @@
   return d->convertCenter;
 }
 
+std::list<GTLCore::Function*>& CompilerBase::functionsToDelete()
+{
+  return d->functionsToDelete;
+}
+
+void CompilerBase::createStdLibFunction(const GTLCore::String& _name, const GTLCore::String& _symbolName, const GTLCore::Type* retType, int count, ...)
+{
+  std::vector<GTLCore::Parameter> arguments;
+  va_list argp;
+  va_start(argp, count);
+  for(int i = 0; i < count; ++i)
+  {
+    const GTLCore::Type* type = va_arg(argp, const GTLCore::Type*);
+    arguments.push_back(GTLCore::Parameter("", type, false, false, GTLCore::Value() ) );
+  }
+  va_end(argp);
+  GTLCore::Function* function = GTLCore::Function::Private::createExternalFunction( d->module, _name, _symbolName, retType, arguments );
+  bool success = declareFunction(GTLCore::ScopedName("", _name), function);
+  GTL_ASSERT( success );
+  UNUSED( success );
+  functionsToDelete().push_back(function);
+}
+
+void CompilerBase::setModuleData( ModuleData* moduleData )
+{
+  d->module = moduleData->llvmModule();
+}

Modified: trunk/OpenGTL/OpenGTL/GTLCore/CompilerBase_p.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/CompilerBase_p.h	2008-10-09 22:02:03 UTC (rev 437)
+++ trunk/OpenGTL/OpenGTL/GTLCore/CompilerBase_p.h	2008-10-09 22:33:17 UTC (rev 438)
@@ -29,6 +29,8 @@
   class Function;
   class ScopedName;
   class TypeManager;
+  class Type;
+  class ModuleData;
   namespace AST {
     class Expression;
   }
@@ -65,6 +67,12 @@
       void appendErrors( const std::list<GTLCore::ErrorMessage>& _msgs);
       void setModuleName( const GTLCore::String& moduleName);
       const GTLCore::String& moduleName() const;
+       /// List of functions which are not visible in the generated module (like functions coming from an imported module, or standard library functions, and that should be removed at the end of compilation
+      std::list<GTLCore::Function*>& functionsToDelete();
+    protected:
+      // Standard library function generation
+      void createStdLibFunction(const String& _name, const String& _symbolName, const Type* retType, int count, ...);
+      void setModuleData( ModuleData* moduleData );
     private:
       struct Private;
       Private* const d;

Modified: trunk/OpenGTL/OpenGTL/GTLCore/Function.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Function.h	2008-10-09 22:02:03 UTC (rev 437)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Function.h	2008-10-09 22:33:17 UTC (rev 438)
@@ -51,6 +51,7 @@
       friend class AST::FunctionCallExpression;
       friend class AST::FunctionDeclaration;
       friend class ParserBase;
+      friend class CompilerBase;
       friend class OpenCTL::Compiler;
       friend class OpenCTL::Program;
       friend class OpenShiva::Wrapper;

Modified: trunk/OpenGTL/OpenShiva/OpenShiva/Compiler_p.cpp
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/Compiler_p.cpp	2008-10-09 22:02:03 UTC (rev 437)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/Compiler_p.cpp	2008-10-09 22:33:17 UTC (rev 438)
@@ -29,6 +29,7 @@
 #include "GTLCore/ModuleData_p.h"
 #include "GTLCore/ErrorMessage.h"
 #include "GTLCore/Optimiser_p.h"
+#include "GTLCore/Type.h"
 
 #include "Debug.h"
 #include "Lexer_p.h"
@@ -64,6 +65,36 @@
   d->module = _moduleData->llvmModule();
   d->moduleData = _moduleData;
   d->codeGenerator = new GTLCore::CodeGenerator( d->moduleData );
+  setModuleData(d->moduleData);
+
+  // Create Standard Library functions
+  // CtlStdLib functions (except print which get a special treatement)
+  createStdLibFunction( "assert", "assert", GTLCore::Type::Void, 1, GTLCore::Type::Boolean);
+  createStdLibFunction( "isnan_f", "isnan_f", GTLCore::Type::Boolean, 1, GTLCore::Type::Float);
+  createStdLibFunction( "isnan_h", "isnan_h", GTLCore::Type::Boolean, 1, GTLCore::Type::Float);
+  // C Math functions
+  createStdLibFunction( "acos", "acosf", GTLCore::Type::Float, 1, GTLCore::Type::Float);
+  createStdLibFunction( "asin", "asinf", GTLCore::Type::Float, 1, GTLCore::Type::Float);
+  createStdLibFunction( "atan", "atanf", GTLCore::Type::Float, 1, GTLCore::Type::Float);
+  createStdLibFunction( "atan2", "atan2f", GTLCore::Type::Float, 2, GTLCore::Type::Float, GTLCore::Type::Float);
+  createStdLibFunction( "cos", "cosf", GTLCore::Type::Float, 1, GTLCore::Type::Float);
+  createStdLibFunction( "sin", "sinf", GTLCore::Type::Float, 1, GTLCore::Type::Float);
+  createStdLibFunction( "tan", "tanf", GTLCore::Type::Float, 1, GTLCore::Type::Float);
+  createStdLibFunction( "cosh", "coshf", GTLCore::Type::Float, 1, GTLCore::Type::Float);
+  createStdLibFunction( "sinh", "sinhf", GTLCore::Type::Float, 1, GTLCore::Type::Float);
+  createStdLibFunction( "tanh", "tanhf", GTLCore::Type::Float, 1, GTLCore::Type::Float);
+  createStdLibFunction( "exp", "expf", GTLCore::Type::Float, 1, GTLCore::Type::Float);
+  createStdLibFunction( "exp_h", "expf", GTLCore::Type::Float, 1, GTLCore::Type::Float);
+  createStdLibFunction( "log", "logf", GTLCore::Type::Float, 1, GTLCore::Type::Float);
+  createStdLibFunction( "log_h", "logf", GTLCore::Type::Float, 1, GTLCore::Type::Float);
+  createStdLibFunction( "log10", "log10f", GTLCore::Type::Float, 1, GTLCore::Type::Float);
+  createStdLibFunction( "log10_h", "log10f", GTLCore::Type::Float, 1, GTLCore::Type::Float);
+  createStdLibFunction( "pow", "powf", GTLCore::Type::Float, 2, GTLCore::Type::Float, GTLCore::Type::Float);
+  createStdLibFunction( "pow_h", "powf", GTLCore::Type::Float, 2, GTLCore::Type::Float, GTLCore::Type::Float);
+  createStdLibFunction( "sqrt", "sqrtf", GTLCore::Type::Float, 1, GTLCore::Type::Float);
+  createStdLibFunction( "fabs", "fabsf", GTLCore::Type::Float, 1, GTLCore::Type::Float);
+  createStdLibFunction( "floor", "floorf", GTLCore::Type::Float, 1, GTLCore::Type::Float);
+  createStdLibFunction( "fmod", "fmodf", GTLCore::Type::Float, 2, GTLCore::Type::Float, GTLCore::Type::Float);
   
   // Init the lexer
   std::istringstream iss(_sourceCode);
@@ -93,10 +124,9 @@
   
   delete tree;
 
-/*  for(std::vector<GTLCore::Function*>::iterator it = d->functionsToDelete.begin();
-      it != d->functionsToDelete.end(); ++it)
+/*  foreach(GTLCore::Function* function, functionsToDelete())
   {
-    delete *it;
+    delete function;
   }*/
   delete d->lexer;
   d->lexer = 0;

Modified: trunk/OpenGTL/OpenShiva/tests/imagegenerators/raytracer.shiva
===================================================================
--- trunk/OpenGTL/OpenShiva/tests/imagegenerators/raytracer.shiva	2008-10-09 22:02:03 UTC (rev 437)
+++ trunk/OpenGTL/OpenShiva/tests/imagegenerators/raytracer.shiva	2008-10-09 22:33:17 UTC (rev 438)
@@ -74,33 +74,22 @@
     
     
     const float PI = 3.14;
-    float sin(float v)
-    {
-      return v;
-    }
-    float cos(float v)
-    {
-      return v;
-    }
-    float acos(float v)
-    {
-      return v;
-    }
-    float sqrt(float v)
-    {
-      return v;
-    }
-    float pow(float v, float v2)
-    {
-      return v;
-    }
     float max(float v, float v2)
     {
-      return v;
+      if( v > v2 )
+      {
+        return v;
+      }
+      return v2;
     }
     float dot(float3 v, float3 v2)
     {
-      return 0.0;
+      float res = 0.0;
+      for( int i = 0; i < 3; ++i)
+      {
+        res += v[i] * v2[i];
+      }
+      return res;
     }
     float3 normalize( float3 v)
     {
@@ -108,22 +97,10 @@
     }
     float length( float3 v)
     {
-      return 0.0;
+      v *= v;
+      return sqrt( v[0] + v[1] + v[2 ] );
     }
-    float mod( float v, float v2)
-    {
-      return v;
-    }
-    float floor( float v)
-    {
-      return v;
-    }
     
-    
-    
-    
-    
-    
     // initialize our sphere parameters
     void evaluateDependents()
     {
@@ -272,7 +249,7 @@
         while( rayShots > 0 )
         {
             // let's make sure dir is properly normalized
-            dir = normalize(dir);
+            dir /= length(dir);
             
             // INTERSECTION TEST
             // find the first sphere we intersect with
@@ -319,7 +296,7 @@
                     );
                  
                     // we could do sampleLinear here to do some actual texturing. :)
-                    if( mod(floor(uv.x*2000.0)+floor(uv.y*2000.0),2.0)==0.0 )
+                    if( fmod(floor(uv.x*2000.0)+floor(uv.y*2000.0),2.0)==0.0 )
                     {
                       sphereColor *= 0.5;
                     }
@@ -334,7 +311,7 @@
                 if(sphereMaterial.w > 0.0)
                 {
                     dirReflect = dir - 2.0*dot(dir, n)*n; // reflect our view vector
-                    dirReflect = normalize(dirReflect);
+                    dirReflect /= length(dirReflect);
                     // originate at our hit position, fire at reflected angle
                     origin = hitPoint;
                     dir = dirReflect;


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