Merge remote branch 'origin/master' into nv50-compiler
[mesa.git] / src / glsl / ast_to_hir.cpp
index e0a510cc4572200bad74ca5bb7b04eab70ea52aa..762f802c2b76ddeb08f56062e7d28fb44a533542 100644 (file)
@@ -97,7 +97,7 @@ _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
  * If a conversion is possible (or unnecessary), \c true is returned.
  * Otherwise \c false is returned.
  */
-static bool
+bool
 apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
                          struct _mesa_glsl_parse_state *state)
 {
@@ -802,9 +802,60 @@ ast_expression::hir(exec_list *instructions,
    case ast_bit_and:
    case ast_bit_xor:
    case ast_bit_or:
+      op[0] = this->subexpressions[0]->hir(instructions, state);
+      op[1] = this->subexpressions[1]->hir(instructions, state);
+
+      if (state->language_version < 130) {
+        _mesa_glsl_error(&loc, state, "bit-wise operations require GLSL 1.30");
+        error_emitted = true;
+      }
+
+      if (!op[0]->type->is_integer()) {
+        _mesa_glsl_error(&loc, state, "LHS of `%s' must be an integer",
+                         operator_string(this->oper));
+        error_emitted = true;
+      }
+
+      if (!op[1]->type->is_integer()) {
+        _mesa_glsl_error(&loc, state, "RHS of `%s' must be an integer",
+                         operator_string(this->oper));
+        error_emitted = true;
+      }
+
+      if (op[0]->type->base_type != op[1]->type->base_type) {
+        _mesa_glsl_error(&loc, state, "operands of `%s' must have the same "
+                         "base type", operator_string(this->oper));
+        error_emitted = true;
+      }
+
+      if (op[0]->type->is_vector() && op[1]->type->is_vector()
+         && op[0]->type->vector_elements != op[1]->type->vector_elements) {
+        _mesa_glsl_error(&loc, state, "operands of `%s' cannot be vectors of "
+                         "different sizes", operator_string(this->oper));
+        error_emitted = true;
+      }
+
+      type = op[0]->type->is_scalar() ? op[1]->type : op[0]->type;
+      result = new(ctx) ir_expression(operations[this->oper], type,
+                                     op[0], op[1]);
+      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
+      break;
+
    case ast_bit_not:
-      _mesa_glsl_error(& loc, state, "FINISHME: implement bit-wise operators");
-      error_emitted = true;
+      op[0] = this->subexpressions[0]->hir(instructions, state);
+
+      if (state->language_version < 130) {
+        _mesa_glsl_error(&loc, state, "bit-wise operations require GLSL 1.30");
+        error_emitted = true;
+      }
+
+      if (!op[0]->type->is_integer()) {
+        _mesa_glsl_error(&loc, state, "operand of `~' must be an integer");
+        error_emitted = true;
+      }
+
+      type = op[0]->type;
+      result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL);
       break;
 
    case ast_logic_and: {
@@ -2142,6 +2193,25 @@ ast_function::hir(exec_list *instructions,
 
    const char *const name = identifier;
 
+   /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
+    *
+    *   "Function declarations (prototypes) cannot occur inside of functions;
+    *   they must be at global scope, or for the built-in functions, outside
+    *   the global scope."
+    *
+    * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
+    *
+    *   "User defined functions may only be defined within the global scope."
+    *
+    * Note that this language does not appear in GLSL 1.10.
+    */
+   if ((state->current_function != NULL) && (state->language_version != 110)) {
+      YYLTYPE loc = this->get_location();
+      _mesa_glsl_error(&loc, state,
+                      "declaration of function `%s' not allowed within "
+                      "function body", name);
+   }
+
    /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
     *
     *   "Identifiers starting with "gl_" are reserved for use by
@@ -2187,7 +2257,7 @@ ast_function::hir(exec_list *instructions,
     * seen signature for a function with the same name, or, if a match is found,
     * that the previously seen signature does not have an associated definition.
     */
-   f = state->symbols->get_function(name, false);
+   f = state->symbols->get_function(name);
    if (f != NULL && !f->is_builtin) {
       sig = f->exact_matching_signature(&hir_parameters);
       if (sig != NULL) {
@@ -2224,7 +2294,23 @@ ast_function::hir(exec_list *instructions,
       }
 
       /* Emit the new function header */
-      instructions->push_tail(f);
+      if (state->current_function == NULL)
+        instructions->push_tail(f);
+      else {
+        /* IR invariants disallow function declarations or definitions nested
+         * within other function definitions.  Insert the new ir_function
+         * block in the instruction sequence before the ir_function block
+         * containing the current ir_function_signature.
+         *
+         * This can only happen in a GLSL 1.10 shader.  In all other GLSL
+         * versions this nesting is disallowed.  There is a check for this at
+         * the top of this function.
+         */
+        ir_function *const curr =
+           const_cast<ir_function *>(state->current_function->function());
+
+        curr->insert_before(f);
+      }
    }
 
    /* Verify the return type of main() */
@@ -2644,8 +2730,7 @@ ast_struct_specifier::hir(exec_list *instructions,
       glsl_type::get_record_instance(fields, decl_count, name);
 
    YYLTYPE loc = this->get_location();
-   ir_function *ctor = t->generate_constructor();
-   if (!state->symbols->add_type(name, t, ctor)) {
+   if (!state->symbols->add_type(name, t)) {
       _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
    } else {