New _slang_adapt_call() function.
authorBrian <brian@yutani.localnet.net>
Wed, 17 Jan 2007 23:11:50 +0000 (16:11 -0700)
committerBrian <brian@yutani.localnet.net>
Wed, 17 Jan 2007 23:11:50 +0000 (16:11 -0700)
This is used to modify function calls (when possible) to make the arguments
map to the function parameters.  This includes "unrolling" vector types
and doing casts.

Example:
   vec2 v2 = vec2(1.2, 3.4)
   ivec3 iv = ivec3(false, v2);
Is converted into:
   ivec3 iv = ivec3(int(false), int(v2[0]), int(v2[1]))

src/mesa/shader/slang/slang_simplify.c
src/mesa/shader/slang/slang_simplify.h

index dfae746a40d98745e04005cce110262aeacd14a5..821a716f58c5fd4514165903535304dd3f3c2438 100644 (file)
@@ -31,7 +31,9 @@
 #include "imports.h"
 #include "macros.h"
 #include "slang_compile.h"
+#include "slang_codegen.h"
 #include "slang_simplify.h"
+#include "slang_print.h"
 
 
 /**
@@ -182,3 +184,149 @@ slang_simplify(slang_operation *oper,
    }
 }
 
+
+
+/**
+ * Adapt the arguments for a function call to match the parameters of
+ * the given function.
+ * This is for:
+ * 1. converting/casting argument types to match parameters
+ * 2. breaking up vector/matrix types into individual components to
+ *    satisfy constructors.
+ */
+GLboolean
+_slang_adapt_call(slang_operation *callOper, const slang_function *fun,
+                  const slang_assembly_name_space * space,
+                  slang_atom_pool * atoms)
+{
+   const GLboolean haveRetValue = _slang_function_has_return_value(fun);
+   const int numParams = fun->param_count - haveRetValue;
+   int i;
+   int dbg = 0;
+
+   if (dbg) printf("Adapt %d args to %d parameters\n",
+                   callOper->num_children, numParams);
+
+   if (callOper->num_children != numParams) {
+      /* number of arguments doesn't match number of parameters */
+
+      if (fun->kind == slang_func_constructor) {
+         /* For constructor calls, we can try to unroll vector/matrix args
+          * into individual floats/ints and try to match the function params.
+          */
+         for (i = 0; i < numParams; i++) {
+            slang_assembly_typeinfo argType;
+            GLint argSz, j;
+
+            /* Get type of arg[i] */
+            if (!slang_assembly_typeinfo_construct(&argType))
+               return GL_FALSE;
+            if (!_slang_typeof_operation_(&callOper->children[i], space,
+                                          &argType, atoms)) {
+               slang_assembly_typeinfo_destruct(&argType);
+               return GL_FALSE;
+            }
+
+            /*
+            paramSz = _slang_sizeof_type_specifier(&paramVar->type.specifier);
+            assert(paramSz == 1);
+            */
+            argSz = _slang_sizeof_type_specifier(&argType.spec);
+            if (argSz > 1) {
+               slang_operation origArg;
+               /* break up arg[i] into components */
+               if (dbg)
+                  printf("Break up arg %d from 1 to %d elements\n", i, argSz);
+
+               slang_operation_construct(&origArg);
+               slang_operation_copy(&origArg,
+                                    &callOper->children[i]);
+
+               /* insert argSz-1 new children/args */
+               for (j = 0; j < argSz - 1; j++) {
+                  (void) slang_operation_insert(&callOper->num_children,
+                                                &callOper->children, i);
+               }
+
+               /* replace arg[i+j] with subscript/index oper */
+               for (j = 0; j < argSz; j++) {
+                  callOper->children[i + j].type = slang_oper_subscript;
+                  callOper->children[i + j].num_children = 2;
+                  callOper->children[i + j].children = slang_operation_new(2);
+                  slang_operation_copy(&callOper->children[i + j].children[0],
+                                       &origArg);
+                  callOper->children[i + j].children[1].type
+                     = slang_oper_literal_int;
+                  callOper->children[i + j].children[1].literal[0] = j;
+               }
+
+            }
+         } /* for i */
+      }
+      else {
+         /* non-constructor function: number of args must match number
+          * of function params.
+          */
+         return GL_FALSE; /* caller will record an error msg */
+      }
+   }
+
+   if (callOper->num_children < numParams) {
+      /* still not enough args for all params */
+      return GL_FALSE;
+   }
+   else if (callOper->num_children > numParams) {
+      /* now too many arguments */
+      /* XXX this isn't always an error, see spec */
+      return GL_FALSE;
+   }
+
+   /*
+    * Second phase, argument casting.
+    * Example:
+    *   void foo(int i, bool b) {}
+    *   x = foo(3.15, 9);
+    * Gets translated into:
+    *   x = foo(int(3.15), bool(9))
+    */
+   for (i = 0; i < numParams; i++) {
+      slang_assembly_typeinfo argType;
+      slang_variable *paramVar = fun->parameters->variables[i];
+
+      /* Get type of arg[i] */
+      if (!slang_assembly_typeinfo_construct(&argType))
+         return GL_FALSE;
+      if (!_slang_typeof_operation_(&callOper->children[i], space,
+                                    &argType, atoms)) {
+         slang_assembly_typeinfo_destruct(&argType);
+         return GL_FALSE;
+      }
+
+      /* see if arg type matches parameter type */
+      if (!slang_type_specifier_equal(&argType.spec,
+                                      &paramVar->type.specifier)) {
+         /* need to adapt arg type to match param type */
+         const char *constructorName =
+            slang_type_specifier_type_to_string(paramVar->type.specifier.type);
+         slang_operation *child = slang_operation_new(1);
+
+         slang_operation_copy(child, &callOper->children[i]);
+         child->locals->outer_scope = callOper->locals;
+
+         callOper->children[i].type = slang_oper_call;
+         callOper->children[i].a_id = slang_atom_pool_atom(atoms, constructorName);
+         callOper->children[i].num_children = 1;
+         callOper->children[i].children = child;
+      }
+
+      slang_assembly_typeinfo_destruct(&argType);
+   }
+
+   if (dbg) {
+      printf("===== New call to %s with adapted arguments ===============\n",
+             (char*) fun->header.a_name);
+      slang_print_tree(callOper, 5);
+   }
+
+   return GL_TRUE;
+}
index 69be3092a5820445c0d209aae844c7771cfbba57..0ca1fa2f0d24cf75a6f9ea55092b3bbed1c90ea5 100644 (file)
@@ -8,5 +8,11 @@ slang_simplify(slang_operation *oper,
                slang_atom_pool * atoms);
 
 
+extern GLboolean
+_slang_adapt_call(slang_operation *callOper, const slang_function *fun,
+                  const slang_assembly_name_space * space,
+                  slang_atom_pool * atoms);
+
+
 
 #endif /* SLANG_SIMPLIFY_H */