Ignore the generated codegen files for now.
[mesa.git] / ir.cpp
diff --git a/ir.cpp b/ir.cpp
index 7ada145d53b6e600c39002bcc9618c8646e608c8..9252ccfd3a3431c52bd1bc87341c56264342b030 100644 (file)
--- a/ir.cpp
+++ b/ir.cpp
@@ -22,7 +22,6 @@
  */
 #include <string.h>
 #include "main/imports.h"
-#include "main/simple_list.h"
 #include "ir.h"
 #include "ir_visitor.h"
 #include "glsl_types.h"
@@ -54,6 +53,7 @@ ir_expression::get_num_operands(ir_expression_operation op)
       1, /* ir_unop_logic_not */
       1, /* ir_unop_neg */
       1, /* ir_unop_abs */
+      1, /* ir_unop_sign */
       1, /* ir_unop_rcp */
       1, /* ir_unop_rsq */
       1, /* ir_unop_sqrt */
@@ -73,6 +73,12 @@ ir_expression::get_num_operands(ir_expression_operation op)
       1, /* ir_unop_ceil */
       1, /* ir_unop_floor */
 
+      1, /* ir_unop_sin */
+      1, /* ir_unop_cos */
+
+      1, /* ir_unop_dFdx */
+      1, /* ir_unop_dFdy */
+
       2, /* ir_binop_add */
       2, /* ir_binop_sub */
       2, /* ir_binop_mul */
@@ -113,6 +119,7 @@ static const char *const operator_strs[] = {
    "!",
    "neg",
    "abs",
+   "sign",
    "rcp",
    "rsq",
    "sqrt",
@@ -130,6 +137,10 @@ static const char *const operator_strs[] = {
    "trunc",
    "ceil",
    "floor",
+   "sin",
+   "cos",
+   "dFdx",
+   "dFdy",
    "+",
    "-",
    "*",
@@ -173,23 +184,19 @@ ir_expression::get_operator(const char *str)
    return (ir_expression_operation) -1;
 }
 
-ir_constant::ir_constant(const struct glsl_type *type, const void *data)
+ir_constant::ir_constant()
 {
-   unsigned size = 0;
+   /* empty */
+}
 
-   this->type = type;
-   switch (type->base_type) {
-   case GLSL_TYPE_UINT:  size = sizeof(this->value.u[0]); break;
-   case GLSL_TYPE_INT:   size = sizeof(this->value.i[0]); break;
-   case GLSL_TYPE_FLOAT: size = sizeof(this->value.f[0]); break;
-   case GLSL_TYPE_BOOL:  size = sizeof(this->value.b[0]); break;
-   default:
-      /* FINISHME: What to do?  Exceptions are not the answer.
-       */
-      break;
-   }
+ir_constant::ir_constant(const struct glsl_type *type,
+                        const ir_constant_data *data)
+{
+   assert((type->base_type >= GLSL_TYPE_UINT)
+         && (type->base_type <= GLSL_TYPE_BOOL));
 
-   memcpy(& this->value, data, size * type->components());
+   this->type = type;
+   memcpy(& this->value, data, sizeof(this->value));
 }
 
 ir_constant::ir_constant(float f)
@@ -216,23 +223,262 @@ ir_constant::ir_constant(bool b)
    this->value.b[0] = b;
 }
 
+ir_constant::ir_constant(const ir_constant *c, unsigned i)
+{
+   this->type = c->type->get_base_type();
+
+   switch (this->type->base_type) {
+   case GLSL_TYPE_UINT:  this->value.u[0] = c->value.u[i]; break;
+   case GLSL_TYPE_INT:   this->value.i[0] = c->value.i[i]; break;
+   case GLSL_TYPE_FLOAT: this->value.f[0] = c->value.f[i]; break;
+   case GLSL_TYPE_BOOL:  this->value.b[0] = c->value.b[i]; break;
+   default:              assert(!"Should not get here."); break;
+   }
+}
 
-ir_dereference::ir_dereference(ir_instruction *var)
+ir_constant::ir_constant(const struct glsl_type *type, exec_list *value_list)
+{
+   this->type = type;
+
+   /* FINISHME: Support array types. */
+   assert(type->is_scalar() || type->is_vector() || type->is_matrix()
+         || type->is_record());
+
+   /* If the constant is a record, the types of each of the entries in
+    * value_list must be a 1-for-1 match with the structure components.  Each
+    * entry must also be a constant.  Just move the nodes from the value_list
+    * to the list in the ir_constant.
+    */
+   /* FINISHME: Should there be some type checking and / or assertions here? */
+   /* FINISHME: Should the new constant take ownership of the nodes from
+    * FINISHME: value_list, or should it make copies?
+    */
+   if (type->is_record()) {
+      value_list->move_nodes_to(& this->components);
+      return;
+   }
+
+
+   ir_constant *value = (ir_constant *) (value_list->head);
+
+   /* Use each component from each entry in the value_list to initialize one
+    * component of the constant being constructed.
+    */
+   for (unsigned i = 0; i < type->components(); /* empty */) {
+      assert(value->as_constant() != NULL);
+      assert(!value->is_tail_sentinal());
+
+      for (unsigned j = 0; j < value->type->components(); j++) {
+        switch (type->base_type) {
+        case GLSL_TYPE_UINT:
+           this->value.u[i] = value->get_uint_component(j);
+           break;
+        case GLSL_TYPE_INT:
+           this->value.i[i] = value->get_int_component(j);
+           break;
+        case GLSL_TYPE_FLOAT:
+           this->value.f[i] = value->get_float_component(j);
+           break;
+        case GLSL_TYPE_BOOL:
+           this->value.b[i] = value->get_bool_component(j);
+           break;
+        default:
+           /* FINISHME: What to do?  Exceptions are not the answer.
+            */
+           break;
+        }
+
+        i++;
+        if (i >= type->components())
+           break;
+      }
+
+      value = (ir_constant *) value->next;
+   }
+}
+
+bool
+ir_constant::get_bool_component(unsigned i) const
+{
+   switch (this->type->base_type) {
+   case GLSL_TYPE_UINT:  return this->value.u[i] != 0;
+   case GLSL_TYPE_INT:   return this->value.i[i] != 0;
+   case GLSL_TYPE_FLOAT: return ((int)this->value.f[i]) != 0;
+   case GLSL_TYPE_BOOL:  return this->value.b[i];
+   default:              assert(!"Should not get here."); break;
+   }
+
+   /* Must return something to make the compiler happy.  This is clearly an
+    * error case.
+    */
+   return false;
+}
+
+float
+ir_constant::get_float_component(unsigned i) const
+{
+   switch (this->type->base_type) {
+   case GLSL_TYPE_UINT:  return (float) this->value.u[i];
+   case GLSL_TYPE_INT:   return (float) this->value.i[i];
+   case GLSL_TYPE_FLOAT: return this->value.f[i];
+   case GLSL_TYPE_BOOL:  return this->value.b[i] ? 1.0 : 0.0;
+   default:              assert(!"Should not get here."); break;
+   }
+
+   /* Must return something to make the compiler happy.  This is clearly an
+    * error case.
+    */
+   return 0.0;
+}
+
+int
+ir_constant::get_int_component(unsigned i) const
+{
+   switch (this->type->base_type) {
+   case GLSL_TYPE_UINT:  return this->value.u[i];
+   case GLSL_TYPE_INT:   return this->value.i[i];
+   case GLSL_TYPE_FLOAT: return (int) this->value.f[i];
+   case GLSL_TYPE_BOOL:  return this->value.b[i] ? 1 : 0;
+   default:              assert(!"Should not get here."); break;
+   }
+
+   /* Must return something to make the compiler happy.  This is clearly an
+    * error case.
+    */
+   return 0;
+}
+
+unsigned
+ir_constant::get_uint_component(unsigned i) const
+{
+   switch (this->type->base_type) {
+   case GLSL_TYPE_UINT:  return this->value.u[i];
+   case GLSL_TYPE_INT:   return this->value.i[i];
+   case GLSL_TYPE_FLOAT: return (unsigned) this->value.f[i];
+   case GLSL_TYPE_BOOL:  return this->value.b[i] ? 1 : 0;
+   default:              assert(!"Should not get here."); break;
+   }
+
+   /* Must return something to make the compiler happy.  This is clearly an
+    * error case.
+    */
+   return 0;
+}
+
+
+ir_constant *
+ir_constant::get_record_field(const char *name)
+{
+   int idx = this->type->field_index(name);
+
+   if (idx < 0)
+      return NULL;
+
+   if (this->components.is_empty())
+      return NULL;
+
+   exec_node *node = this->components.head;
+   for (int i = 0; i < idx; i++) {
+      node = node->next;
+
+      /* If the end of the list is encountered before the element matching the
+       * requested field is found, return NULL.
+       */
+      if (node->is_tail_sentinal())
+        return NULL;
+   }
+
+   return (ir_constant *) node;
+}
+
+
+bool
+ir_constant::has_value(const ir_constant *c) const
+{
+   if (this->type != c->type)
+      return false;
+
+   /* FINISHME: This will probably also handle constant arrays as soon as those
+    * FINISHME: are supported.
+    */
+   if (this->type->base_type == GLSL_TYPE_STRUCT) {
+      const exec_node *a_node = this->components.head;
+      const exec_node *b_node = c->components.head;
+
+      while (!a_node->is_tail_sentinal()) {
+        assert(!b_node->is_tail_sentinal());
+
+        const ir_constant *const a_field = (ir_constant *) a_node;
+        const ir_constant *const b_field = (ir_constant *) b_node;
+
+        if (!a_field->has_value(b_field))
+           return false;
+
+        a_node = a_node->next;
+        b_node = b_node->next;
+      }
+
+      return true;
+   }
+
+   for (unsigned i = 0; i < this->type->components(); i++) {
+      switch (this->type->base_type) {
+      case GLSL_TYPE_UINT:
+        if (this->value.u[i] != c->value.u[i])
+           return false;
+        break;
+      case GLSL_TYPE_INT:
+        if (this->value.i[i] != c->value.i[i])
+           return false;
+        break;
+      case GLSL_TYPE_FLOAT:
+        if (this->value.f[i] != c->value.f[i])
+           return false;
+        break;
+      case GLSL_TYPE_BOOL:
+        if (this->value.b[i] != c->value.b[i])
+           return false;
+        break;
+      default:
+        assert(!"Should not get here.");
+        return false;
+      }
+   }
+
+   return true;
+}
+
+ir_dereference_variable::ir_dereference_variable(ir_variable *var)
 {
-   this->mode = ir_reference_variable;
    this->var = var;
    this->type = (var != NULL) ? var->type : glsl_type::error_type;
 }
 
 
-ir_dereference::ir_dereference(ir_instruction *var,
-                              ir_rvalue *array_index)
-   : mode(ir_reference_array), var(var)
+ir_dereference_array::ir_dereference_array(ir_rvalue *value,
+                                          ir_rvalue *array_index)
+{
+   this->array_index = array_index;
+   this->set_array(value);
+}
+
+
+ir_dereference_array::ir_dereference_array(ir_variable *var,
+                                          ir_rvalue *array_index)
 {
-   type = glsl_type::error_type;
+   this->array_index = array_index;
+   this->set_array(new ir_dereference_variable(var));
+}
 
-   if (var != NULL) {
-      const glsl_type *const vt = var->type;
+
+void
+ir_dereference_array::set_array(ir_rvalue *value)
+{
+   this->array = value;
+   this->type = glsl_type::error_type;
+
+   if (this->array != NULL) {
+      const glsl_type *const vt = this->array->type;
 
       if (vt->is_array()) {
         type = vt->element_type();
@@ -242,42 +488,87 @@ ir_dereference::ir_dereference(ir_instruction *var,
         type = vt->get_base_type();
       }
    }
+}
+
 
-   this->selector.array_index = array_index;
+ir_dereference_record::ir_dereference_record(ir_rvalue *value,
+                                            const char *field)
+{
+   this->record = value;
+   this->field = field;
+   this->type = (this->record != NULL)
+      ? this->record->type->field_type(field) : glsl_type::error_type;
 }
 
-ir_dereference::ir_dereference(ir_instruction *variable, const char *field)
-   : mode(ir_reference_record), var(variable)
+
+ir_dereference_record::ir_dereference_record(ir_variable *var,
+                                            const char *field)
 {
-   this->selector.field = field;
-   this->type = (var != NULL)
-      ? var->type->field_type(field) : glsl_type::error_type;
+   this->record = new ir_dereference_variable(var);
+   this->field = field;
+   this->type = (this->record != NULL)
+      ? this->record->type->field_type(field) : glsl_type::error_type;
 }
 
+
 bool
 ir_dereference::is_lvalue()
 {
-   if (var == NULL)
+   ir_variable *var = this->variable_referenced();
+
+   /* Every l-value derference chain eventually ends in a variable.
+    */
+   if ((var == NULL) || var->read_only)
       return false;
 
-   if (mode == ir_reference_variable) {
-      ir_variable *const as_var = var->as_variable();
-      if (as_var == NULL)
-        return false;
+   if (this->type->is_array() && !var->array_lvalue)
+      return false;
 
-      if (as_var->type->is_array() && !as_var->array_lvalue)
-        return false;
+   return true;
+}
 
-      return !as_var->read_only;
-   } else if (mode == ir_reference_array) {
-      /* FINISHME: Walk up the dereference chain and figure out if
-       * FINISHME: the variable is read-only.
-       */
+
+const char *tex_opcode_strs[] = { "tex", "txb", "txl", "txd", "txf" };
+
+const char *ir_texture::opcode_string()
+{
+   assert((unsigned int) op <=
+         sizeof(tex_opcode_strs) / sizeof(tex_opcode_strs[0]));
+   return tex_opcode_strs[op];
+}
+
+ir_texture_opcode
+ir_texture::get_opcode(const char *str)
+{
+   const int count = sizeof(tex_opcode_strs) / sizeof(tex_opcode_strs[0]);
+   for (int op = 0; op < count; op++) {
+      if (strcmp(str, tex_opcode_strs[op]) == 0)
+        return (ir_texture_opcode) op;
    }
+   return (ir_texture_opcode) -1;
+}
 
-   return true;
+
+void
+ir_texture::set_sampler(ir_dereference *sampler)
+{
+   assert(sampler != NULL);
+   this->sampler = sampler;
+
+   switch (sampler->type->sampler_type) {
+   case GLSL_TYPE_FLOAT:
+      this->type = glsl_type::vec4_type;
+      break;
+   case GLSL_TYPE_INT:
+      this->type = glsl_type::ivec4_type;
+      break;
+   case GLSL_TYPE_UINT:
+      this->type = glsl_type::uvec4_type;
+      break;
+   }
 }
 
+
 ir_swizzle::ir_swizzle(ir_rvalue *val, unsigned x, unsigned y, unsigned z,
                       unsigned w, unsigned count)
    : val(val)
@@ -308,6 +599,14 @@ ir_swizzle::ir_swizzle(ir_rvalue *val, unsigned x, unsigned y, unsigned z,
    type = glsl_type::get_instance(val->type->base_type, mask.num_components, 1);
 }
 
+ir_swizzle::ir_swizzle(ir_rvalue *val, ir_swizzle_mask mask)
+{
+   this->val = val;
+   this->mask = mask;
+   this->type = glsl_type::get_instance(val->type->base_type,
+                                       mask.num_components, 1);
+}
+
 #define X 1
 #define R 5
 #define S 9
@@ -389,13 +688,21 @@ ir_swizzle::create(ir_rvalue *val, const char *str, unsigned vector_length)
 #undef S
 #undef I
 
+ir_variable *
+ir_swizzle::variable_referenced()
+{
+   return this->val->variable_referenced();
+}
 
 ir_variable::ir_variable(const struct glsl_type *type, const char *name)
    : max_array_access(0), read_only(false), centroid(false), invariant(false),
-     mode(ir_var_auto), interpolation(ir_var_smooth)
+     shader_in(false), shader_out(false),
+     mode(ir_var_auto), interpolation(ir_var_smooth), array_lvalue(false)
 {
    this->type = type;
    this->name = name;
+   this->location = -1;
+   this->warn_extension = NULL;
    this->constant_value = NULL;
 
    if (type && type->base_type == GLSL_TYPE_SAMPLER)
@@ -403,6 +710,31 @@ ir_variable::ir_variable(const struct glsl_type *type, const char *name)
 }
 
 
+const char *
+ir_variable::interpolation_string() const
+{
+   if (!this->shader_in && !this->shader_out)
+      return "";
+
+   switch (this->interpolation) {
+   case ir_var_smooth:        return "smooth";
+   case ir_var_flat:          return "flat";
+   case ir_var_noperspective: return "noperspective";
+   }
+
+   assert(!"Should not get here.");
+   return "";
+}
+
+
+unsigned
+ir_variable::component_slots() const
+{
+   /* FINISHME: Sparsely accessed arrays require fewer slots. */
+   return this->type->component_slots();
+}
+
+
 ir_function_signature::ir_function_signature(const glsl_type *return_type)
    : return_type(return_type), is_defined(false)
 {
@@ -422,6 +754,7 @@ ir_function_signature::qualifiers_match(exec_list *params)
       ir_variable *b = (ir_variable *)iter_b.get();
 
       if (a->read_only != b->read_only ||
+         a->mode != b->mode ||
          a->interpolation != b->interpolation ||
          a->centroid != b->centroid) {