Merge remote branch 'origin/master' into nv50-compiler
[mesa.git] / src / glsl / ast_to_hir.cpp
index 548b3d8e5aa2f7ba24e972c23528716b8bf3f7d2..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)
 {
@@ -510,7 +510,6 @@ do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
    bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
 
    if (!error_emitted) {
-      /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
       if (!lhs->is_lvalue()) {
         _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
         error_emitted = true;
@@ -581,7 +580,6 @@ get_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
    void *ctx = talloc_parent(lvalue);
    ir_variable *var;
 
-   /* FINISHME: Give unique names to the temporaries. */
    var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
                              ir_var_temporary);
    instructions->push_tail(var);
@@ -804,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: {
@@ -1976,14 +2025,10 @@ ast_declarator_list::hir(exec_list *instructions,
        *   OpenGL, and may not be declared in a shader as either a
        *   variable or a function."
        */
-      if (strncmp(decl->identifier, "gl_", 3) == 0) {
-        /* FINISHME: This should only trigger if we're not redefining
-         * FINISHME: a builtin (to add a qualifier, for example).
-         */
+      if (strncmp(decl->identifier, "gl_", 3) == 0)
         _mesa_glsl_error(& loc, state,
                          "identifier `%s' uses reserved `gl_' prefix",
                          decl->identifier);
-      }
 
       /* Add the variable to the symbol table.  Note that the initializer's
        * IR was already processed earlier (though it hasn't been emitted yet),
@@ -2148,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
@@ -2193,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) {
@@ -2230,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() */
@@ -2650,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 {