glsl: Skip processing of expression trees in discard simplification.
[mesa.git] / src / glsl / link_functions.cpp
index 28e56cb0fa291458f2cff893d56c7c5b9976b24a..ae8818be8718ef54291fac451e8d62b7f1e3138e 100644 (file)
  * DEALINGS IN THE SOFTWARE.
  */
 
-#include <cstdlib>
-#include <cstdio>
-#include <cstdarg>
-
-extern "C" {
-#include <talloc.h>
-}
-
-#include "main/mtypes.h"
+#include "main/core.h"
 #include "glsl_symbol_table.h"
 #include "glsl_parser_extras.h"
 #include "ir.h"
 #include "program.h"
-#include "hash_table.h"
+#include "program/hash_table.h"
 #include "linker.h"
 
 static ir_function_signature *
@@ -43,36 +35,57 @@ find_matching_signature(const char *name, const exec_list *actual_parameters,
 
 class call_link_visitor : public ir_hierarchical_visitor {
 public:
-   call_link_visitor(gl_shader_program *prog, gl_shader **shader_list,
-                    unsigned num_shaders)
+   call_link_visitor(gl_shader_program *prog, gl_shader *linked,
+                    gl_shader **shader_list, unsigned num_shaders)
    {
       this->prog = prog;
       this->shader_list = shader_list;
       this->num_shaders = num_shaders;
       this->success = true;
+      this->linked = linked;
+
+      this->locals = hash_table_ctor(0, hash_table_pointer_hash,
+                                    hash_table_pointer_compare);
+   }
+
+   ~call_link_visitor()
+   {
+      hash_table_dtor(this->locals);
+   }
+
+   virtual ir_visitor_status visit(ir_variable *ir)
+   {
+      hash_table_insert(locals, ir, ir);
+      return visit_continue;
    }
 
    virtual ir_visitor_status visit_enter(ir_call *ir)
    {
-      /* If the function call references a function signature that does not
-       * have a definition, try to find the definition in one of the other
-       * shaders.
+      /* If ir is an ir_call from a function that was imported from another
+       * shader callee will point to an ir_function_signature in the original
+       * shader.  In this case the function signature MUST NOT BE MODIFIED.
+       * Doing so will modify the original shader.  This may prevent that
+       * shader from being linkable in other programs.
        */
-      ir_function_signature *callee =
-        const_cast<ir_function_signature *>(ir->get_callee());
+      const ir_function_signature *const callee = ir->get_callee();
       assert(callee != NULL);
-
-      if (callee->is_defined)
-        /* FINISHME: Do children need to be processed, or are all parameters
-         * FINISHME: with function calls already flattend?
-         */
-        return visit_continue;
-
       const char *const name = callee->function_name();
 
+      /* Determine if the requested function signature already exists in the
+       * final linked shader.  If it does, use it as the target of the call.
+       */
       ir_function_signature *sig =
-        find_matching_signature(name, &ir->actual_parameters, shader_list,
-                                num_shaders);
+        find_matching_signature(name, &callee->parameters, &linked, 1);
+      if (sig != NULL) {
+        ir->set_callee(sig);
+        return visit_continue;
+      }
+
+      /* Try to find the signature in one of the other shaders that is being
+       * linked.  If it's not found there, return an error.
+       */
+      sig = find_matching_signature(name, &ir->actual_parameters, shader_list,
+                                   num_shaders);
       if (sig == NULL) {
         /* FINISHME: Log the full signature of unresolved function.
          */
@@ -82,6 +95,33 @@ public:
         return visit_stop;
       }
 
+      /* Find the prototype information in the linked shader.  Generate any
+       * details that may be missing.
+       */
+      ir_function *f = linked->symbols->get_function(name);
+      if (f == NULL) {
+        f = new(linked) ir_function(name);
+
+        /* Add the new function to the linked IR.
+         */
+        linked->symbols->add_function(f);
+        linked->ir->push_head(f);
+      }
+
+      ir_function_signature *linked_sig =
+        f->exact_matching_signature(&callee->parameters);
+      if (linked_sig == NULL) {
+        linked_sig = new(linked) ir_function_signature(callee->return_type);
+        f->add_signature(linked_sig);
+      }
+
+      /* At this point linked_sig and called may be the same.  If ir is an
+       * ir_call from linked then linked_sig and callee will be
+       * ir_function_signatures that have no definitions (is_defined is false).
+       */
+      assert(!linked_sig->is_defined);
+      assert(linked_sig->body.is_empty());
+
       /* Create an in-place clone of the function definition.  This multistep
        * process introduces some complexity here, but it has some advantages.
        * The parameter list and the and function body are cloned separately.
@@ -90,9 +130,9 @@ public:
        *
        * The big advantage is that the ir_function_signature does not change.
        * This means that we don't have to process the rest of the IR tree to
-       * patch ir_call nodes.  In addition, there is no way to remove or replace
-       * signature stored in a function.  One could easily be added, but this
-       * avoids the need.
+       * patch ir_call nodes.  In addition, there is no way to remove or
+       * replace signature stored in a function.  One could easily be added,
+       * but this avoids the need.
        */
       struct hash_table *ht = hash_table_ctor(0, hash_table_pointer_hash,
                                              hash_table_pointer_compare);
@@ -101,27 +141,64 @@ public:
         const ir_instruction *const original = (ir_instruction *) node;
         assert(const_cast<ir_instruction *>(original)->as_variable());
 
-        ir_instruction *copy = original->clone(ht);
+        ir_instruction *copy = original->clone(linked, ht);
         formal_parameters.push_tail(copy);
       }
 
-      callee->replace_parameters(&formal_parameters);
+      linked_sig->replace_parameters(&formal_parameters);
 
-      assert(callee->body.is_empty());
       foreach_list_const(node, &sig->body) {
         const ir_instruction *const original = (ir_instruction *) node;
 
-        ir_instruction *copy = original->clone(ht);
-        callee->body.push_tail(copy);
+        ir_instruction *copy = original->clone(linked, ht);
+        linked_sig->body.push_tail(copy);
       }
 
-      callee->is_defined = true;
+      linked_sig->is_defined = true;
+      hash_table_dtor(ht);
 
-      /* FINISHME: Patch references inside the function to things outside the
-       * FINISHME: function (i.e., function calls and global variables).
+      /* Patch references inside the function to things outside the function
+       * (i.e., function calls and global variables).
        */
+      linked_sig->accept(this);
 
-      hash_table_dtor(ht);
+      ir->set_callee(linked_sig);
+
+      return visit_continue;
+   }
+
+   virtual ir_visitor_status visit(ir_dereference_variable *ir)
+   {
+      if (hash_table_find(locals, ir->var) == NULL) {
+        /* The non-function variable must be a global, so try to find the
+         * variable in the shader's symbol table.  If the variable is not
+         * found, then it's a global that *MUST* be defined in the original
+         * shader.
+         */
+        ir_variable *var = linked->symbols->get_variable(ir->var->name);
+        if (var == NULL) {
+           /* Clone the ir_variable that the dereference already has and add
+            * it to the linked shader.
+            */
+           var = ir->var->clone(linked, NULL);
+           linked->symbols->add_variable(var);
+           linked->ir->push_head(var);
+        } else if (var->type->is_array()) {
+           /* It is possible to have a global array declared in multiple
+            * shaders without a size.  The array is implicitly sized by the
+            * maximal access to it in *any* shader.  Because of this, we
+            * need to track the maximal access to the array as linking pulls
+            * more functions in that access the array.
+            */
+           var->max_array_access =
+              MAX2(var->max_array_access, ir->var->max_array_access);
+
+           if (var->type->length == 0 && ir->var->type->length != 0)
+              var->type = ir->var->type;
+        }
+
+        ir->var = var;
+      }
 
       return visit_continue;
    }
@@ -143,6 +220,19 @@ private:
    /** Number of shaders available for linking. */
    unsigned num_shaders;
 
+   /**
+    * Final linked shader
+    *
+    * This is used two ways.  It is used to find global variables in the
+    * linked shader that are accessed by the function.  It is also used to add
+    * global variables from the shader where the function originated.
+    */
+   gl_shader *linked;
+
+   /**
+    * Table of variables local to the function.
+    */
+   hash_table *locals;
 };
 
 
@@ -175,7 +265,7 @@ bool
 link_function_calls(gl_shader_program *prog, gl_shader *main,
                    gl_shader **shader_list, unsigned num_shaders)
 {
-   call_link_visitor v(prog, shader_list, num_shaders);
+   call_link_visitor v(prog, main, shader_list, num_shaders);
 
    v.run(main->ir);
    return v.success;