glsl: Skip processing of expression trees in discard simplification.
[mesa.git] / src / glsl / link_functions.cpp
index 721bec65f3ecf09a8a001169bfa62cc5999ef8c0..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 *
@@ -51,6 +43,20 @@ public:
       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)
@@ -93,11 +99,17 @@ public:
        * details that may be missing.
        */
       ir_function *f = linked->symbols->get_function(name);
-      if (f == NULL)
+      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->matching_signature(&callee->parameters);
+        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);
@@ -129,7 +141,7 @@ 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);
       }
 
@@ -138,17 +150,55 @@ public:
       foreach_list_const(node, &sig->body) {
         const ir_instruction *const original = (ir_instruction *) node;
 
-        ir_instruction *copy = original->clone(ht);
+        ir_instruction *copy = original->clone(linked, ht);
         linked_sig->body.push_tail(copy);
       }
 
       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;
    }
@@ -178,6 +228,11 @@ private:
     * global variables from the shader where the function originated.
     */
    gl_shader *linked;
+
+   /**
+    * Table of variables local to the function.
+    */
+   hash_table *locals;
 };