glsl: Don't add overloads to existing structure constructors.
[mesa/nouveau-pmpeg.git] / src / glsl / ast_to_hir.cpp
blobc666da300b7703d21d4b56c1db824c2bb01d8853
1 /*
2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 /**
25 * \file ast_to_hir.c
26 * Convert abstract syntax to to high-level intermediate reprensentation (HIR).
28 * During the conversion to HIR, the majority of the symantic checking is
29 * preformed on the program. This includes:
31 * * Symbol table management
32 * * Type checking
33 * * Function binding
35 * The majority of this work could be done during parsing, and the parser could
36 * probably generate HIR directly. However, this results in frequent changes
37 * to the parser code. Since we do not assume that every system this complier
38 * is built on will have Flex and Bison installed, we have to store the code
39 * generated by these tools in our version control system. In other parts of
40 * the system we've seen problems where a parser was changed but the generated
41 * code was not committed, merge conflicts where created because two developers
42 * had slightly different versions of Bison installed, etc.
44 * I have also noticed that running Bison generated parsers in GDB is very
45 * irritating. When you get a segfault on '$$ = $1->foo', you can't very
46 * well 'print $1' in GDB.
48 * As a result, my preference is to put as little C code as possible in the
49 * parser (and lexer) sources.
52 #include "main/core.h" /* for struct gl_extensions */
53 #include "glsl_symbol_table.h"
54 #include "glsl_parser_extras.h"
55 #include "ast.h"
56 #include "glsl_types.h"
57 #include "ir.h"
59 void
60 _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
62 _mesa_glsl_initialize_variables(instructions, state);
63 _mesa_glsl_initialize_functions(instructions, state);
65 state->current_function = NULL;
67 foreach_list_typed (ast_node, ast, link, & state->translation_unit)
68 ast->hir(instructions, state);
72 /**
73 * If a conversion is available, convert one operand to a different type
75 * The \c from \c ir_rvalue is converted "in place".
77 * \param to Type that the operand it to be converted to
78 * \param from Operand that is being converted
79 * \param state GLSL compiler state
81 * \return
82 * If a conversion is possible (or unnecessary), \c true is returned.
83 * Otherwise \c false is returned.
85 static bool
86 apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
87 struct _mesa_glsl_parse_state *state)
89 void *ctx = state;
90 if (to->base_type == from->type->base_type)
91 return true;
93 /* This conversion was added in GLSL 1.20. If the compilation mode is
94 * GLSL 1.10, the conversion is skipped.
96 if (state->language_version < 120)
97 return false;
99 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
101 * "There are no implicit array or structure conversions. For
102 * example, an array of int cannot be implicitly converted to an
103 * array of float. There are no implicit conversions between
104 * signed and unsigned integers."
106 /* FINISHME: The above comment is partially a lie. There is int/uint
107 * FINISHME: conversion for immediate constants.
109 if (!to->is_float() || !from->type->is_numeric())
110 return false;
112 /* Convert to a floating point type with the same number of components
113 * as the original type - i.e. int to float, not int to vec4.
115 to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements,
116 from->type->matrix_columns);
118 switch (from->type->base_type) {
119 case GLSL_TYPE_INT:
120 from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL);
121 break;
122 case GLSL_TYPE_UINT:
123 from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL);
124 break;
125 case GLSL_TYPE_BOOL:
126 from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL);
127 break;
128 default:
129 assert(0);
132 return true;
136 static const struct glsl_type *
137 arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
138 bool multiply,
139 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
141 const glsl_type *type_a = value_a->type;
142 const glsl_type *type_b = value_b->type;
144 /* From GLSL 1.50 spec, page 56:
146 * "The arithmetic binary operators add (+), subtract (-),
147 * multiply (*), and divide (/) operate on integer and
148 * floating-point scalars, vectors, and matrices."
150 if (!type_a->is_numeric() || !type_b->is_numeric()) {
151 _mesa_glsl_error(loc, state,
152 "Operands to arithmetic operators must be numeric");
153 return glsl_type::error_type;
157 /* "If one operand is floating-point based and the other is
158 * not, then the conversions from Section 4.1.10 "Implicit
159 * Conversions" are applied to the non-floating-point-based operand."
161 if (!apply_implicit_conversion(type_a, value_b, state)
162 && !apply_implicit_conversion(type_b, value_a, state)) {
163 _mesa_glsl_error(loc, state,
164 "Could not implicitly convert operands to "
165 "arithmetic operator");
166 return glsl_type::error_type;
168 type_a = value_a->type;
169 type_b = value_b->type;
171 /* "If the operands are integer types, they must both be signed or
172 * both be unsigned."
174 * From this rule and the preceeding conversion it can be inferred that
175 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
176 * The is_numeric check above already filtered out the case where either
177 * type is not one of these, so now the base types need only be tested for
178 * equality.
180 if (type_a->base_type != type_b->base_type) {
181 _mesa_glsl_error(loc, state,
182 "base type mismatch for arithmetic operator");
183 return glsl_type::error_type;
186 /* "All arithmetic binary operators result in the same fundamental type
187 * (signed integer, unsigned integer, or floating-point) as the
188 * operands they operate on, after operand type conversion. After
189 * conversion, the following cases are valid
191 * * The two operands are scalars. In this case the operation is
192 * applied, resulting in a scalar."
194 if (type_a->is_scalar() && type_b->is_scalar())
195 return type_a;
197 /* "* One operand is a scalar, and the other is a vector or matrix.
198 * In this case, the scalar operation is applied independently to each
199 * component of the vector or matrix, resulting in the same size
200 * vector or matrix."
202 if (type_a->is_scalar()) {
203 if (!type_b->is_scalar())
204 return type_b;
205 } else if (type_b->is_scalar()) {
206 return type_a;
209 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
210 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
211 * handled.
213 assert(!type_a->is_scalar());
214 assert(!type_b->is_scalar());
216 /* "* The two operands are vectors of the same size. In this case, the
217 * operation is done component-wise resulting in the same size
218 * vector."
220 if (type_a->is_vector() && type_b->is_vector()) {
221 if (type_a == type_b) {
222 return type_a;
223 } else {
224 _mesa_glsl_error(loc, state,
225 "vector size mismatch for arithmetic operator");
226 return glsl_type::error_type;
230 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
231 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
232 * <vector, vector> have been handled. At least one of the operands must
233 * be matrix. Further, since there are no integer matrix types, the base
234 * type of both operands must be float.
236 assert(type_a->is_matrix() || type_b->is_matrix());
237 assert(type_a->base_type == GLSL_TYPE_FLOAT);
238 assert(type_b->base_type == GLSL_TYPE_FLOAT);
240 /* "* The operator is add (+), subtract (-), or divide (/), and the
241 * operands are matrices with the same number of rows and the same
242 * number of columns. In this case, the operation is done component-
243 * wise resulting in the same size matrix."
244 * * The operator is multiply (*), where both operands are matrices or
245 * one operand is a vector and the other a matrix. A right vector
246 * operand is treated as a column vector and a left vector operand as a
247 * row vector. In all these cases, it is required that the number of
248 * columns of the left operand is equal to the number of rows of the
249 * right operand. Then, the multiply (*) operation does a linear
250 * algebraic multiply, yielding an object that has the same number of
251 * rows as the left operand and the same number of columns as the right
252 * operand. Section 5.10 "Vector and Matrix Operations" explains in
253 * more detail how vectors and matrices are operated on."
255 if (! multiply) {
256 if (type_a == type_b)
257 return type_a;
258 } else {
259 if (type_a->is_matrix() && type_b->is_matrix()) {
260 /* Matrix multiply. The columns of A must match the rows of B. Given
261 * the other previously tested constraints, this means the vector type
262 * of a row from A must be the same as the vector type of a column from
263 * B.
265 if (type_a->row_type() == type_b->column_type()) {
266 /* The resulting matrix has the number of columns of matrix B and
267 * the number of rows of matrix A. We get the row count of A by
268 * looking at the size of a vector that makes up a column. The
269 * transpose (size of a row) is done for B.
271 const glsl_type *const type =
272 glsl_type::get_instance(type_a->base_type,
273 type_a->column_type()->vector_elements,
274 type_b->row_type()->vector_elements);
275 assert(type != glsl_type::error_type);
277 return type;
279 } else if (type_a->is_matrix()) {
280 /* A is a matrix and B is a column vector. Columns of A must match
281 * rows of B. Given the other previously tested constraints, this
282 * means the vector type of a row from A must be the same as the
283 * vector the type of B.
285 if (type_a->row_type() == type_b) {
286 /* The resulting vector has a number of elements equal to
287 * the number of rows of matrix A. */
288 const glsl_type *const type =
289 glsl_type::get_instance(type_a->base_type,
290 type_a->column_type()->vector_elements,
292 assert(type != glsl_type::error_type);
294 return type;
296 } else {
297 assert(type_b->is_matrix());
299 /* A is a row vector and B is a matrix. Columns of A must match rows
300 * of B. Given the other previously tested constraints, this means
301 * the type of A must be the same as the vector type of a column from
302 * B.
304 if (type_a == type_b->column_type()) {
305 /* The resulting vector has a number of elements equal to
306 * the number of columns of matrix B. */
307 const glsl_type *const type =
308 glsl_type::get_instance(type_a->base_type,
309 type_b->row_type()->vector_elements,
311 assert(type != glsl_type::error_type);
313 return type;
317 _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
318 return glsl_type::error_type;
322 /* "All other cases are illegal."
324 _mesa_glsl_error(loc, state, "type mismatch");
325 return glsl_type::error_type;
329 static const struct glsl_type *
330 unary_arithmetic_result_type(const struct glsl_type *type,
331 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
333 /* From GLSL 1.50 spec, page 57:
335 * "The arithmetic unary operators negate (-), post- and pre-increment
336 * and decrement (-- and ++) operate on integer or floating-point
337 * values (including vectors and matrices). All unary operators work
338 * component-wise on their operands. These result with the same type
339 * they operated on."
341 if (!type->is_numeric()) {
342 _mesa_glsl_error(loc, state,
343 "Operands to arithmetic operators must be numeric");
344 return glsl_type::error_type;
347 return type;
351 static const struct glsl_type *
352 modulus_result_type(const struct glsl_type *type_a,
353 const struct glsl_type *type_b,
354 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
356 /* From GLSL 1.50 spec, page 56:
357 * "The operator modulus (%) operates on signed or unsigned integers or
358 * integer vectors. The operand types must both be signed or both be
359 * unsigned."
361 if (!type_a->is_integer() || !type_b->is_integer()
362 || (type_a->base_type != type_b->base_type)) {
363 _mesa_glsl_error(loc, state, "type mismatch");
364 return glsl_type::error_type;
367 /* "The operands cannot be vectors of differing size. If one operand is
368 * a scalar and the other vector, then the scalar is applied component-
369 * wise to the vector, resulting in the same type as the vector. If both
370 * are vectors of the same size, the result is computed component-wise."
372 if (type_a->is_vector()) {
373 if (!type_b->is_vector()
374 || (type_a->vector_elements == type_b->vector_elements))
375 return type_a;
376 } else
377 return type_b;
379 /* "The operator modulus (%) is not defined for any other data types
380 * (non-integer types)."
382 _mesa_glsl_error(loc, state, "type mismatch");
383 return glsl_type::error_type;
387 static const struct glsl_type *
388 relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
389 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
391 const glsl_type *type_a = value_a->type;
392 const glsl_type *type_b = value_b->type;
394 /* From GLSL 1.50 spec, page 56:
395 * "The relational operators greater than (>), less than (<), greater
396 * than or equal (>=), and less than or equal (<=) operate only on
397 * scalar integer and scalar floating-point expressions."
399 if (!type_a->is_numeric()
400 || !type_b->is_numeric()
401 || !type_a->is_scalar()
402 || !type_b->is_scalar()) {
403 _mesa_glsl_error(loc, state,
404 "Operands to relational operators must be scalar and "
405 "numeric");
406 return glsl_type::error_type;
409 /* "Either the operands' types must match, or the conversions from
410 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
411 * operand, after which the types must match."
413 if (!apply_implicit_conversion(type_a, value_b, state)
414 && !apply_implicit_conversion(type_b, value_a, state)) {
415 _mesa_glsl_error(loc, state,
416 "Could not implicitly convert operands to "
417 "relational operator");
418 return glsl_type::error_type;
420 type_a = value_a->type;
421 type_b = value_b->type;
423 if (type_a->base_type != type_b->base_type) {
424 _mesa_glsl_error(loc, state, "base type mismatch");
425 return glsl_type::error_type;
428 /* "The result is scalar Boolean."
430 return glsl_type::bool_type;
435 * Validates that a value can be assigned to a location with a specified type
437 * Validates that \c rhs can be assigned to some location. If the types are
438 * not an exact match but an automatic conversion is possible, \c rhs will be
439 * converted.
441 * \return
442 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
443 * Otherwise the actual RHS to be assigned will be returned. This may be
444 * \c rhs, or it may be \c rhs after some type conversion.
446 * \note
447 * In addition to being used for assignments, this function is used to
448 * type-check return values.
450 ir_rvalue *
451 validate_assignment(struct _mesa_glsl_parse_state *state,
452 const glsl_type *lhs_type, ir_rvalue *rhs)
454 const glsl_type *rhs_type = rhs->type;
456 /* If there is already some error in the RHS, just return it. Anything
457 * else will lead to an avalanche of error message back to the user.
459 if (rhs_type->is_error())
460 return rhs;
462 /* If the types are identical, the assignment can trivially proceed.
464 if (rhs_type == lhs_type)
465 return rhs;
467 /* If the array element types are the same and the size of the LHS is zero,
468 * the assignment is okay.
470 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
471 * is handled by ir_dereference::is_lvalue.
473 if (lhs_type->is_array() && rhs->type->is_array()
474 && (lhs_type->element_type() == rhs->type->element_type())
475 && (lhs_type->array_size() == 0)) {
476 return rhs;
479 /* Check for implicit conversion in GLSL 1.20 */
480 if (apply_implicit_conversion(lhs_type, rhs, state)) {
481 rhs_type = rhs->type;
482 if (rhs_type == lhs_type)
483 return rhs;
486 return NULL;
489 ir_rvalue *
490 do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
491 ir_rvalue *lhs, ir_rvalue *rhs,
492 YYLTYPE lhs_loc)
494 void *ctx = state;
495 bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
497 if (!error_emitted) {
498 /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
499 if (!lhs->is_lvalue()) {
500 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
501 error_emitted = true;
505 ir_rvalue *new_rhs = validate_assignment(state, lhs->type, rhs);
506 if (new_rhs == NULL) {
507 _mesa_glsl_error(& lhs_loc, state, "type mismatch");
508 } else {
509 rhs = new_rhs;
511 /* If the LHS array was not declared with a size, it takes it size from
512 * the RHS. If the LHS is an l-value and a whole array, it must be a
513 * dereference of a variable. Any other case would require that the LHS
514 * is either not an l-value or not a whole array.
516 if (lhs->type->array_size() == 0) {
517 ir_dereference *const d = lhs->as_dereference();
519 assert(d != NULL);
521 ir_variable *const var = d->variable_referenced();
523 assert(var != NULL);
525 if (var->max_array_access >= unsigned(rhs->type->array_size())) {
526 /* FINISHME: This should actually log the location of the RHS. */
527 _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
528 "previous access",
529 var->max_array_access);
532 var->type = glsl_type::get_array_instance(lhs->type->element_type(),
533 rhs->type->array_size());
534 d->type = var->type;
538 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
539 * but not post_inc) need the converted assigned value as an rvalue
540 * to handle things like:
542 * i = j += 1;
544 * So we always just store the computed value being assigned to a
545 * temporary and return a deref of that temporary. If the rvalue
546 * ends up not being used, the temp will get copy-propagated out.
548 ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp",
549 ir_var_temporary);
550 ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var);
551 instructions->push_tail(var);
552 instructions->push_tail(new(ctx) ir_assignment(deref_var,
553 rhs,
554 NULL));
555 deref_var = new(ctx) ir_dereference_variable(var);
557 if (!error_emitted)
558 instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var, NULL));
560 return new(ctx) ir_dereference_variable(var);
563 static ir_rvalue *
564 get_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
566 void *ctx = talloc_parent(lvalue);
567 ir_variable *var;
569 /* FINISHME: Give unique names to the temporaries. */
570 var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
571 ir_var_temporary);
572 instructions->push_tail(var);
573 var->mode = ir_var_auto;
575 instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
576 lvalue, NULL));
578 /* Once we've created this temporary, mark it read only so it's no
579 * longer considered an lvalue.
581 var->read_only = true;
583 return new(ctx) ir_dereference_variable(var);
587 ir_rvalue *
588 ast_node::hir(exec_list *instructions,
589 struct _mesa_glsl_parse_state *state)
591 (void) instructions;
592 (void) state;
594 return NULL;
598 ir_rvalue *
599 ast_expression::hir(exec_list *instructions,
600 struct _mesa_glsl_parse_state *state)
602 void *ctx = state;
603 static const int operations[AST_NUM_OPERATORS] = {
604 -1, /* ast_assign doesn't convert to ir_expression. */
605 -1, /* ast_plus doesn't convert to ir_expression. */
606 ir_unop_neg,
607 ir_binop_add,
608 ir_binop_sub,
609 ir_binop_mul,
610 ir_binop_div,
611 ir_binop_mod,
612 ir_binop_lshift,
613 ir_binop_rshift,
614 ir_binop_less,
615 ir_binop_greater,
616 ir_binop_lequal,
617 ir_binop_gequal,
618 ir_binop_equal,
619 ir_binop_nequal,
620 ir_binop_bit_and,
621 ir_binop_bit_xor,
622 ir_binop_bit_or,
623 ir_unop_bit_not,
624 ir_binop_logic_and,
625 ir_binop_logic_xor,
626 ir_binop_logic_or,
627 ir_unop_logic_not,
629 /* Note: The following block of expression types actually convert
630 * to multiple IR instructions.
632 ir_binop_mul, /* ast_mul_assign */
633 ir_binop_div, /* ast_div_assign */
634 ir_binop_mod, /* ast_mod_assign */
635 ir_binop_add, /* ast_add_assign */
636 ir_binop_sub, /* ast_sub_assign */
637 ir_binop_lshift, /* ast_ls_assign */
638 ir_binop_rshift, /* ast_rs_assign */
639 ir_binop_bit_and, /* ast_and_assign */
640 ir_binop_bit_xor, /* ast_xor_assign */
641 ir_binop_bit_or, /* ast_or_assign */
643 -1, /* ast_conditional doesn't convert to ir_expression. */
644 ir_binop_add, /* ast_pre_inc. */
645 ir_binop_sub, /* ast_pre_dec. */
646 ir_binop_add, /* ast_post_inc. */
647 ir_binop_sub, /* ast_post_dec. */
648 -1, /* ast_field_selection doesn't conv to ir_expression. */
649 -1, /* ast_array_index doesn't convert to ir_expression. */
650 -1, /* ast_function_call doesn't conv to ir_expression. */
651 -1, /* ast_identifier doesn't convert to ir_expression. */
652 -1, /* ast_int_constant doesn't convert to ir_expression. */
653 -1, /* ast_uint_constant doesn't conv to ir_expression. */
654 -1, /* ast_float_constant doesn't conv to ir_expression. */
655 -1, /* ast_bool_constant doesn't conv to ir_expression. */
656 -1, /* ast_sequence doesn't convert to ir_expression. */
658 ir_rvalue *result = NULL;
659 ir_rvalue *op[3];
660 const struct glsl_type *type = glsl_type::error_type;
661 bool error_emitted = false;
662 YYLTYPE loc;
664 loc = this->get_location();
666 switch (this->oper) {
667 case ast_assign: {
668 op[0] = this->subexpressions[0]->hir(instructions, state);
669 op[1] = this->subexpressions[1]->hir(instructions, state);
671 result = do_assignment(instructions, state, op[0], op[1],
672 this->subexpressions[0]->get_location());
673 error_emitted = result->type->is_error();
674 type = result->type;
675 break;
678 case ast_plus:
679 op[0] = this->subexpressions[0]->hir(instructions, state);
681 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
683 error_emitted = type->is_error();
685 result = op[0];
686 break;
688 case ast_neg:
689 op[0] = this->subexpressions[0]->hir(instructions, state);
691 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
693 error_emitted = type->is_error();
695 result = new(ctx) ir_expression(operations[this->oper], type,
696 op[0], NULL);
697 break;
699 case ast_add:
700 case ast_sub:
701 case ast_mul:
702 case ast_div:
703 op[0] = this->subexpressions[0]->hir(instructions, state);
704 op[1] = this->subexpressions[1]->hir(instructions, state);
706 type = arithmetic_result_type(op[0], op[1],
707 (this->oper == ast_mul),
708 state, & loc);
709 error_emitted = type->is_error();
711 result = new(ctx) ir_expression(operations[this->oper], type,
712 op[0], op[1]);
713 break;
715 case ast_mod:
716 op[0] = this->subexpressions[0]->hir(instructions, state);
717 op[1] = this->subexpressions[1]->hir(instructions, state);
719 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
721 assert(operations[this->oper] == ir_binop_mod);
723 result = new(ctx) ir_expression(operations[this->oper], type,
724 op[0], op[1]);
725 error_emitted = type->is_error();
726 break;
728 case ast_lshift:
729 case ast_rshift:
730 _mesa_glsl_error(& loc, state, "FINISHME: implement bit-shift operators");
731 error_emitted = true;
732 break;
734 case ast_less:
735 case ast_greater:
736 case ast_lequal:
737 case ast_gequal:
738 op[0] = this->subexpressions[0]->hir(instructions, state);
739 op[1] = this->subexpressions[1]->hir(instructions, state);
741 type = relational_result_type(op[0], op[1], state, & loc);
743 /* The relational operators must either generate an error or result
744 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
746 assert(type->is_error()
747 || ((type->base_type == GLSL_TYPE_BOOL)
748 && type->is_scalar()));
750 result = new(ctx) ir_expression(operations[this->oper], type,
751 op[0], op[1]);
752 error_emitted = type->is_error();
753 break;
755 case ast_nequal:
756 case ast_equal:
757 op[0] = this->subexpressions[0]->hir(instructions, state);
758 op[1] = this->subexpressions[1]->hir(instructions, state);
760 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
762 * "The equality operators equal (==), and not equal (!=)
763 * operate on all types. They result in a scalar Boolean. If
764 * the operand types do not match, then there must be a
765 * conversion from Section 4.1.10 "Implicit Conversions"
766 * applied to one operand that can make them match, in which
767 * case this conversion is done."
769 if ((!apply_implicit_conversion(op[0]->type, op[1], state)
770 && !apply_implicit_conversion(op[1]->type, op[0], state))
771 || (op[0]->type != op[1]->type)) {
772 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
773 "type", (this->oper == ast_equal) ? "==" : "!=");
774 error_emitted = true;
775 } else if ((state->language_version <= 110)
776 && (op[0]->type->is_array() || op[1]->type->is_array())) {
777 _mesa_glsl_error(& loc, state, "array comparisons forbidden in "
778 "GLSL 1.10");
779 error_emitted = true;
782 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
783 op[0], op[1]);
784 type = glsl_type::bool_type;
786 assert(result->type == glsl_type::bool_type);
787 break;
789 case ast_bit_and:
790 case ast_bit_xor:
791 case ast_bit_or:
792 case ast_bit_not:
793 _mesa_glsl_error(& loc, state, "FINISHME: implement bit-wise operators");
794 error_emitted = true;
795 break;
797 case ast_logic_and: {
798 op[0] = this->subexpressions[0]->hir(instructions, state);
800 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
801 YYLTYPE loc = this->subexpressions[0]->get_location();
803 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
804 operator_string(this->oper));
805 error_emitted = true;
808 ir_constant *op0_const = op[0]->constant_expression_value();
809 if (op0_const) {
810 if (op0_const->value.b[0]) {
811 op[1] = this->subexpressions[1]->hir(instructions, state);
813 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
814 YYLTYPE loc = this->subexpressions[1]->get_location();
816 _mesa_glsl_error(& loc, state,
817 "RHS of `%s' must be scalar boolean",
818 operator_string(this->oper));
819 error_emitted = true;
821 result = op[1];
822 } else {
823 result = op0_const;
825 type = glsl_type::bool_type;
826 } else {
827 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
828 "and_tmp",
829 ir_var_temporary);
830 instructions->push_tail(tmp);
832 ir_if *const stmt = new(ctx) ir_if(op[0]);
833 instructions->push_tail(stmt);
835 op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state);
837 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
838 YYLTYPE loc = this->subexpressions[1]->get_location();
840 _mesa_glsl_error(& loc, state,
841 "RHS of `%s' must be scalar boolean",
842 operator_string(this->oper));
843 error_emitted = true;
846 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
847 ir_assignment *const then_assign =
848 new(ctx) ir_assignment(then_deref, op[1], NULL);
849 stmt->then_instructions.push_tail(then_assign);
851 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
852 ir_assignment *const else_assign =
853 new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false), NULL);
854 stmt->else_instructions.push_tail(else_assign);
856 result = new(ctx) ir_dereference_variable(tmp);
857 type = tmp->type;
859 break;
862 case ast_logic_or: {
863 op[0] = this->subexpressions[0]->hir(instructions, state);
865 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
866 YYLTYPE loc = this->subexpressions[0]->get_location();
868 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
869 operator_string(this->oper));
870 error_emitted = true;
873 ir_constant *op0_const = op[0]->constant_expression_value();
874 if (op0_const) {
875 if (op0_const->value.b[0]) {
876 result = op0_const;
877 } else {
878 op[1] = this->subexpressions[1]->hir(instructions, state);
880 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
881 YYLTYPE loc = this->subexpressions[1]->get_location();
883 _mesa_glsl_error(& loc, state,
884 "RHS of `%s' must be scalar boolean",
885 operator_string(this->oper));
886 error_emitted = true;
888 result = op[1];
890 type = glsl_type::bool_type;
891 } else {
892 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
893 "or_tmp",
894 ir_var_temporary);
895 instructions->push_tail(tmp);
897 ir_if *const stmt = new(ctx) ir_if(op[0]);
898 instructions->push_tail(stmt);
900 op[1] = this->subexpressions[1]->hir(&stmt->else_instructions, state);
902 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
903 YYLTYPE loc = this->subexpressions[1]->get_location();
905 _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean",
906 operator_string(this->oper));
907 error_emitted = true;
910 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
911 ir_assignment *const then_assign =
912 new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true), NULL);
913 stmt->then_instructions.push_tail(then_assign);
915 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
916 ir_assignment *const else_assign =
917 new(ctx) ir_assignment(else_deref, op[1], NULL);
918 stmt->else_instructions.push_tail(else_assign);
920 result = new(ctx) ir_dereference_variable(tmp);
921 type = tmp->type;
923 break;
926 case ast_logic_xor:
927 op[0] = this->subexpressions[0]->hir(instructions, state);
928 op[1] = this->subexpressions[1]->hir(instructions, state);
931 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
932 op[0], op[1]);
933 type = glsl_type::bool_type;
934 break;
936 case ast_logic_not:
937 op[0] = this->subexpressions[0]->hir(instructions, state);
939 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
940 YYLTYPE loc = this->subexpressions[0]->get_location();
942 _mesa_glsl_error(& loc, state,
943 "operand of `!' must be scalar boolean");
944 error_emitted = true;
947 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
948 op[0], NULL);
949 type = glsl_type::bool_type;
950 break;
952 case ast_mul_assign:
953 case ast_div_assign:
954 case ast_add_assign:
955 case ast_sub_assign: {
956 op[0] = this->subexpressions[0]->hir(instructions, state);
957 op[1] = this->subexpressions[1]->hir(instructions, state);
959 type = arithmetic_result_type(op[0], op[1],
960 (this->oper == ast_mul_assign),
961 state, & loc);
963 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
964 op[0], op[1]);
966 result = do_assignment(instructions, state,
967 op[0]->clone(ctx, NULL), temp_rhs,
968 this->subexpressions[0]->get_location());
969 type = result->type;
970 error_emitted = (op[0]->type->is_error());
972 /* GLSL 1.10 does not allow array assignment. However, we don't have to
973 * explicitly test for this because none of the binary expression
974 * operators allow array operands either.
977 break;
980 case ast_mod_assign: {
981 op[0] = this->subexpressions[0]->hir(instructions, state);
982 op[1] = this->subexpressions[1]->hir(instructions, state);
984 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
986 assert(operations[this->oper] == ir_binop_mod);
988 ir_rvalue *temp_rhs;
989 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
990 op[0], op[1]);
992 result = do_assignment(instructions, state,
993 op[0]->clone(ctx, NULL), temp_rhs,
994 this->subexpressions[0]->get_location());
995 type = result->type;
996 error_emitted = type->is_error();
997 break;
1000 case ast_ls_assign:
1001 case ast_rs_assign:
1002 _mesa_glsl_error(& loc, state,
1003 "FINISHME: implement bit-shift assignment operators");
1004 error_emitted = true;
1005 break;
1007 case ast_and_assign:
1008 case ast_xor_assign:
1009 case ast_or_assign:
1010 _mesa_glsl_error(& loc, state,
1011 "FINISHME: implement logic assignment operators");
1012 error_emitted = true;
1013 break;
1015 case ast_conditional: {
1016 op[0] = this->subexpressions[0]->hir(instructions, state);
1018 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1020 * "The ternary selection operator (?:). It operates on three
1021 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1022 * first expression, which must result in a scalar Boolean."
1024 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
1025 YYLTYPE loc = this->subexpressions[0]->get_location();
1027 _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean");
1028 error_emitted = true;
1031 /* The :? operator is implemented by generating an anonymous temporary
1032 * followed by an if-statement. The last instruction in each branch of
1033 * the if-statement assigns a value to the anonymous temporary. This
1034 * temporary is the r-value of the expression.
1036 exec_list then_instructions;
1037 exec_list else_instructions;
1039 op[1] = this->subexpressions[1]->hir(&then_instructions, state);
1040 op[2] = this->subexpressions[2]->hir(&else_instructions, state);
1042 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1044 * "The second and third expressions can be any type, as
1045 * long their types match, or there is a conversion in
1046 * Section 4.1.10 "Implicit Conversions" that can be applied
1047 * to one of the expressions to make their types match. This
1048 * resulting matching type is the type of the entire
1049 * expression."
1051 if ((!apply_implicit_conversion(op[1]->type, op[2], state)
1052 && !apply_implicit_conversion(op[2]->type, op[1], state))
1053 || (op[1]->type != op[2]->type)) {
1054 YYLTYPE loc = this->subexpressions[1]->get_location();
1056 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
1057 "operator must have matching types.");
1058 error_emitted = true;
1059 type = glsl_type::error_type;
1060 } else {
1061 type = op[1]->type;
1064 ir_constant *cond_val = op[0]->constant_expression_value();
1065 ir_constant *then_val = op[1]->constant_expression_value();
1066 ir_constant *else_val = op[2]->constant_expression_value();
1068 if (then_instructions.is_empty()
1069 && else_instructions.is_empty()
1070 && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) {
1071 result = (cond_val->value.b[0]) ? then_val : else_val;
1072 } else {
1073 ir_variable *const tmp =
1074 new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
1075 instructions->push_tail(tmp);
1077 ir_if *const stmt = new(ctx) ir_if(op[0]);
1078 instructions->push_tail(stmt);
1080 then_instructions.move_nodes_to(& stmt->then_instructions);
1081 ir_dereference *const then_deref =
1082 new(ctx) ir_dereference_variable(tmp);
1083 ir_assignment *const then_assign =
1084 new(ctx) ir_assignment(then_deref, op[1], NULL);
1085 stmt->then_instructions.push_tail(then_assign);
1087 else_instructions.move_nodes_to(& stmt->else_instructions);
1088 ir_dereference *const else_deref =
1089 new(ctx) ir_dereference_variable(tmp);
1090 ir_assignment *const else_assign =
1091 new(ctx) ir_assignment(else_deref, op[2], NULL);
1092 stmt->else_instructions.push_tail(else_assign);
1094 result = new(ctx) ir_dereference_variable(tmp);
1096 break;
1099 case ast_pre_inc:
1100 case ast_pre_dec: {
1101 op[0] = this->subexpressions[0]->hir(instructions, state);
1102 if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
1103 op[1] = new(ctx) ir_constant(1.0f);
1104 else
1105 op[1] = new(ctx) ir_constant(1);
1107 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1109 ir_rvalue *temp_rhs;
1110 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1111 op[0], op[1]);
1113 result = do_assignment(instructions, state,
1114 op[0]->clone(ctx, NULL), temp_rhs,
1115 this->subexpressions[0]->get_location());
1116 type = result->type;
1117 error_emitted = op[0]->type->is_error();
1118 break;
1121 case ast_post_inc:
1122 case ast_post_dec: {
1123 op[0] = this->subexpressions[0]->hir(instructions, state);
1124 if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
1125 op[1] = new(ctx) ir_constant(1.0f);
1126 else
1127 op[1] = new(ctx) ir_constant(1);
1129 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1131 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1133 ir_rvalue *temp_rhs;
1134 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1135 op[0], op[1]);
1137 /* Get a temporary of a copy of the lvalue before it's modified.
1138 * This may get thrown away later.
1140 result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
1142 (void)do_assignment(instructions, state,
1143 op[0]->clone(ctx, NULL), temp_rhs,
1144 this->subexpressions[0]->get_location());
1146 type = result->type;
1147 error_emitted = op[0]->type->is_error();
1148 break;
1151 case ast_field_selection:
1152 result = _mesa_ast_field_selection_to_hir(this, instructions, state);
1153 type = result->type;
1154 break;
1156 case ast_array_index: {
1157 YYLTYPE index_loc = subexpressions[1]->get_location();
1159 op[0] = subexpressions[0]->hir(instructions, state);
1160 op[1] = subexpressions[1]->hir(instructions, state);
1162 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1164 ir_rvalue *const array = op[0];
1166 result = new(ctx) ir_dereference_array(op[0], op[1]);
1168 /* Do not use op[0] after this point. Use array.
1170 op[0] = NULL;
1173 if (error_emitted)
1174 break;
1176 if (!array->type->is_array()
1177 && !array->type->is_matrix()
1178 && !array->type->is_vector()) {
1179 _mesa_glsl_error(& index_loc, state,
1180 "cannot dereference non-array / non-matrix / "
1181 "non-vector");
1182 error_emitted = true;
1185 if (!op[1]->type->is_integer()) {
1186 _mesa_glsl_error(& index_loc, state,
1187 "array index must be integer type");
1188 error_emitted = true;
1189 } else if (!op[1]->type->is_scalar()) {
1190 _mesa_glsl_error(& index_loc, state,
1191 "array index must be scalar");
1192 error_emitted = true;
1195 /* If the array index is a constant expression and the array has a
1196 * declared size, ensure that the access is in-bounds. If the array
1197 * index is not a constant expression, ensure that the array has a
1198 * declared size.
1200 ir_constant *const const_index = op[1]->constant_expression_value();
1201 if (const_index != NULL) {
1202 const int idx = const_index->value.i[0];
1203 const char *type_name;
1204 unsigned bound = 0;
1206 if (array->type->is_matrix()) {
1207 type_name = "matrix";
1208 } else if (array->type->is_vector()) {
1209 type_name = "vector";
1210 } else {
1211 type_name = "array";
1214 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
1216 * "It is illegal to declare an array with a size, and then
1217 * later (in the same shader) index the same array with an
1218 * integral constant expression greater than or equal to the
1219 * declared size. It is also illegal to index an array with a
1220 * negative constant expression."
1222 if (array->type->is_matrix()) {
1223 if (array->type->row_type()->vector_elements <= idx) {
1224 bound = array->type->row_type()->vector_elements;
1226 } else if (array->type->is_vector()) {
1227 if (array->type->vector_elements <= idx) {
1228 bound = array->type->vector_elements;
1230 } else {
1231 if ((array->type->array_size() > 0)
1232 && (array->type->array_size() <= idx)) {
1233 bound = array->type->array_size();
1237 if (bound > 0) {
1238 _mesa_glsl_error(& loc, state, "%s index must be < %u",
1239 type_name, bound);
1240 error_emitted = true;
1241 } else if (idx < 0) {
1242 _mesa_glsl_error(& loc, state, "%s index must be >= 0",
1243 type_name);
1244 error_emitted = true;
1247 if (array->type->is_array()) {
1248 /* If the array is a variable dereference, it dereferences the
1249 * whole array, by definition. Use this to get the variable.
1251 * FINISHME: Should some methods for getting / setting / testing
1252 * FINISHME: array access limits be added to ir_dereference?
1254 ir_variable *const v = array->whole_variable_referenced();
1255 if ((v != NULL) && (unsigned(idx) > v->max_array_access))
1256 v->max_array_access = idx;
1258 } else if (array->type->array_size() == 0) {
1259 _mesa_glsl_error(&loc, state, "unsized array index must be constant");
1260 } else {
1261 if (array->type->is_array()) {
1262 /* whole_variable_referenced can return NULL if the array is a
1263 * member of a structure. In this case it is safe to not update
1264 * the max_array_access field because it is never used for fields
1265 * of structures.
1267 ir_variable *v = array->whole_variable_referenced();
1268 if (v != NULL)
1269 v->max_array_access = array->type->array_size();
1273 if (error_emitted)
1274 result->type = glsl_type::error_type;
1276 type = result->type;
1277 break;
1280 case ast_function_call:
1281 /* Should *NEVER* get here. ast_function_call should always be handled
1282 * by ast_function_expression::hir.
1284 assert(0);
1285 break;
1287 case ast_identifier: {
1288 /* ast_identifier can appear several places in a full abstract syntax
1289 * tree. This particular use must be at location specified in the grammar
1290 * as 'variable_identifier'.
1292 ir_variable *var =
1293 state->symbols->get_variable(this->primary_expression.identifier);
1295 result = new(ctx) ir_dereference_variable(var);
1297 if (var != NULL) {
1298 type = result->type;
1299 } else {
1300 _mesa_glsl_error(& loc, state, "`%s' undeclared",
1301 this->primary_expression.identifier);
1303 error_emitted = true;
1305 break;
1308 case ast_int_constant:
1309 type = glsl_type::int_type;
1310 result = new(ctx) ir_constant(this->primary_expression.int_constant);
1311 break;
1313 case ast_uint_constant:
1314 type = glsl_type::uint_type;
1315 result = new(ctx) ir_constant(this->primary_expression.uint_constant);
1316 break;
1318 case ast_float_constant:
1319 type = glsl_type::float_type;
1320 result = new(ctx) ir_constant(this->primary_expression.float_constant);
1321 break;
1323 case ast_bool_constant:
1324 type = glsl_type::bool_type;
1325 result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
1326 break;
1328 case ast_sequence: {
1329 /* It should not be possible to generate a sequence in the AST without
1330 * any expressions in it.
1332 assert(!this->expressions.is_empty());
1334 /* The r-value of a sequence is the last expression in the sequence. If
1335 * the other expressions in the sequence do not have side-effects (and
1336 * therefore add instructions to the instruction list), they get dropped
1337 * on the floor.
1339 foreach_list_typed (ast_node, ast, link, &this->expressions)
1340 result = ast->hir(instructions, state);
1342 type = result->type;
1344 /* Any errors should have already been emitted in the loop above.
1346 error_emitted = true;
1347 break;
1351 if (type->is_error() && !error_emitted)
1352 _mesa_glsl_error(& loc, state, "type mismatch");
1354 return result;
1358 ir_rvalue *
1359 ast_expression_statement::hir(exec_list *instructions,
1360 struct _mesa_glsl_parse_state *state)
1362 /* It is possible to have expression statements that don't have an
1363 * expression. This is the solitary semicolon:
1365 * for (i = 0; i < 5; i++)
1368 * In this case the expression will be NULL. Test for NULL and don't do
1369 * anything in that case.
1371 if (expression != NULL)
1372 expression->hir(instructions, state);
1374 /* Statements do not have r-values.
1376 return NULL;
1380 ir_rvalue *
1381 ast_compound_statement::hir(exec_list *instructions,
1382 struct _mesa_glsl_parse_state *state)
1384 if (new_scope)
1385 state->symbols->push_scope();
1387 foreach_list_typed (ast_node, ast, link, &this->statements)
1388 ast->hir(instructions, state);
1390 if (new_scope)
1391 state->symbols->pop_scope();
1393 /* Compound statements do not have r-values.
1395 return NULL;
1399 static const glsl_type *
1400 process_array_type(const glsl_type *base, ast_node *array_size,
1401 struct _mesa_glsl_parse_state *state)
1403 unsigned length = 0;
1405 /* FINISHME: Reject delcarations of multidimensional arrays. */
1407 if (array_size != NULL) {
1408 exec_list dummy_instructions;
1409 ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
1410 YYLTYPE loc = array_size->get_location();
1412 /* FINISHME: Verify that the grammar forbids side-effects in array
1413 * FINISHME: sizes. i.e., 'vec4 [x = 12] data'
1415 assert(dummy_instructions.is_empty());
1417 if (ir != NULL) {
1418 if (!ir->type->is_integer()) {
1419 _mesa_glsl_error(& loc, state, "array size must be integer type");
1420 } else if (!ir->type->is_scalar()) {
1421 _mesa_glsl_error(& loc, state, "array size must be scalar type");
1422 } else {
1423 ir_constant *const size = ir->constant_expression_value();
1425 if (size == NULL) {
1426 _mesa_glsl_error(& loc, state, "array size must be a "
1427 "constant valued expression");
1428 } else if (size->value.i[0] <= 0) {
1429 _mesa_glsl_error(& loc, state, "array size must be > 0");
1430 } else {
1431 assert(size->type == ir->type);
1432 length = size->value.u[0];
1438 return glsl_type::get_array_instance(base, length);
1442 const glsl_type *
1443 ast_type_specifier::glsl_type(const char **name,
1444 struct _mesa_glsl_parse_state *state) const
1446 const struct glsl_type *type;
1448 if ((this->type_specifier == ast_struct) && (this->type_name == NULL)) {
1449 /* FINISHME: Handle annonymous structures. */
1450 type = NULL;
1451 } else {
1452 type = state->symbols->get_type(this->type_name);
1453 *name = this->type_name;
1455 if (this->is_array) {
1456 type = process_array_type(type, this->array_size, state);
1460 return type;
1464 static void
1465 apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
1466 ir_variable *var,
1467 struct _mesa_glsl_parse_state *state,
1468 YYLTYPE *loc)
1470 if (qual->invariant)
1471 var->invariant = 1;
1473 /* FINISHME: Mark 'in' variables at global scope as read-only. */
1474 if (qual->constant || qual->attribute || qual->uniform
1475 || (qual->varying && (state->target == fragment_shader)))
1476 var->read_only = 1;
1478 if (qual->centroid)
1479 var->centroid = 1;
1481 if (qual->attribute && state->target != vertex_shader) {
1482 var->type = glsl_type::error_type;
1483 _mesa_glsl_error(loc, state,
1484 "`attribute' variables may not be declared in the "
1485 "%s shader",
1486 _mesa_glsl_shader_target_name(state->target));
1489 /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
1491 * "The varying qualifier can be used only with the data types
1492 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
1493 * these."
1495 if (qual->varying) {
1496 const glsl_type *non_array_type;
1498 if (var->type && var->type->is_array())
1499 non_array_type = var->type->fields.array;
1500 else
1501 non_array_type = var->type;
1503 if (non_array_type && non_array_type->base_type != GLSL_TYPE_FLOAT) {
1504 var->type = glsl_type::error_type;
1505 _mesa_glsl_error(loc, state,
1506 "varying variables must be of base type float");
1510 /* If there is no qualifier that changes the mode of the variable, leave
1511 * the setting alone.
1513 if (qual->in && qual->out)
1514 var->mode = ir_var_inout;
1515 else if (qual->attribute || qual->in
1516 || (qual->varying && (state->target == fragment_shader)))
1517 var->mode = ir_var_in;
1518 else if (qual->out || (qual->varying && (state->target == vertex_shader)))
1519 var->mode = ir_var_out;
1520 else if (qual->uniform)
1521 var->mode = ir_var_uniform;
1523 if (qual->flat)
1524 var->interpolation = ir_var_flat;
1525 else if (qual->noperspective)
1526 var->interpolation = ir_var_noperspective;
1527 else
1528 var->interpolation = ir_var_smooth;
1530 var->pixel_center_integer = qual->pixel_center_integer;
1531 var->origin_upper_left = qual->origin_upper_left;
1532 if ((qual->origin_upper_left || qual->pixel_center_integer)
1533 && (strcmp(var->name, "gl_FragCoord") != 0)) {
1534 const char *const qual_string = (qual->origin_upper_left)
1535 ? "origin_upper_left" : "pixel_center_integer";
1537 _mesa_glsl_error(loc, state,
1538 "layout qualifier `%s' can only be applied to "
1539 "fragment shader input `gl_FragCoord'",
1540 qual_string);
1543 if (var->type->is_array() && (state->language_version >= 120)) {
1544 var->array_lvalue = true;
1549 ir_rvalue *
1550 ast_declarator_list::hir(exec_list *instructions,
1551 struct _mesa_glsl_parse_state *state)
1553 void *ctx = state;
1554 const struct glsl_type *decl_type;
1555 const char *type_name = NULL;
1556 ir_rvalue *result = NULL;
1557 YYLTYPE loc = this->get_location();
1559 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
1561 * "To ensure that a particular output variable is invariant, it is
1562 * necessary to use the invariant qualifier. It can either be used to
1563 * qualify a previously declared variable as being invariant
1565 * invariant gl_Position; // make existing gl_Position be invariant"
1567 * In these cases the parser will set the 'invariant' flag in the declarator
1568 * list, and the type will be NULL.
1570 if (this->invariant) {
1571 assert(this->type == NULL);
1573 if (state->current_function != NULL) {
1574 _mesa_glsl_error(& loc, state,
1575 "All uses of `invariant' keyword must be at global "
1576 "scope\n");
1579 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
1580 assert(!decl->is_array);
1581 assert(decl->array_size == NULL);
1582 assert(decl->initializer == NULL);
1584 ir_variable *const earlier =
1585 state->symbols->get_variable(decl->identifier);
1586 if (earlier == NULL) {
1587 _mesa_glsl_error(& loc, state,
1588 "Undeclared variable `%s' cannot be marked "
1589 "invariant\n", decl->identifier);
1590 } else if ((state->target == vertex_shader)
1591 && (earlier->mode != ir_var_out)) {
1592 _mesa_glsl_error(& loc, state,
1593 "`%s' cannot be marked invariant, vertex shader "
1594 "outputs only\n", decl->identifier);
1595 } else if ((state->target == fragment_shader)
1596 && (earlier->mode != ir_var_in)) {
1597 _mesa_glsl_error(& loc, state,
1598 "`%s' cannot be marked invariant, fragment shader "
1599 "inputs only\n", decl->identifier);
1600 } else {
1601 earlier->invariant = true;
1605 /* Invariant redeclarations do not have r-values.
1607 return NULL;
1610 assert(this->type != NULL);
1611 assert(!this->invariant);
1613 /* The type specifier may contain a structure definition. Process that
1614 * before any of the variable declarations.
1616 (void) this->type->specifier->hir(instructions, state);
1618 decl_type = this->type->specifier->glsl_type(& type_name, state);
1619 if (this->declarations.is_empty()) {
1620 /* The only valid case where the declaration list can be empty is when
1621 * the declaration is setting the default precision of a built-in type
1622 * (e.g., 'precision highp vec4;').
1625 if (decl_type != NULL) {
1626 } else {
1627 _mesa_glsl_error(& loc, state, "incomplete declaration");
1631 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
1632 const struct glsl_type *var_type;
1633 ir_variable *var;
1635 /* FINISHME: Emit a warning if a variable declaration shadows a
1636 * FINISHME: declaration at a higher scope.
1639 if ((decl_type == NULL) || decl_type->is_void()) {
1640 if (type_name != NULL) {
1641 _mesa_glsl_error(& loc, state,
1642 "invalid type `%s' in declaration of `%s'",
1643 type_name, decl->identifier);
1644 } else {
1645 _mesa_glsl_error(& loc, state,
1646 "invalid type in declaration of `%s'",
1647 decl->identifier);
1649 continue;
1652 if (decl->is_array) {
1653 var_type = process_array_type(decl_type, decl->array_size, state);
1654 } else {
1655 var_type = decl_type;
1658 var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto);
1660 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
1662 * "Global variables can only use the qualifiers const,
1663 * attribute, uni form, or varying. Only one may be
1664 * specified.
1666 * Local variables can only use the qualifier const."
1668 * This is relaxed in GLSL 1.30.
1670 if (state->language_version < 120) {
1671 if (this->type->qualifier.out) {
1672 _mesa_glsl_error(& loc, state,
1673 "`out' qualifier in declaration of `%s' "
1674 "only valid for function parameters in GLSL 1.10.",
1675 decl->identifier);
1677 if (this->type->qualifier.in) {
1678 _mesa_glsl_error(& loc, state,
1679 "`in' qualifier in declaration of `%s' "
1680 "only valid for function parameters in GLSL 1.10.",
1681 decl->identifier);
1683 /* FINISHME: Test for other invalid qualifiers. */
1686 apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
1687 & loc);
1689 if (this->type->qualifier.invariant) {
1690 if ((state->target == vertex_shader) && !(var->mode == ir_var_out ||
1691 var->mode == ir_var_inout)) {
1692 /* FINISHME: Note that this doesn't work for invariant on
1693 * a function signature outval
1695 _mesa_glsl_error(& loc, state,
1696 "`%s' cannot be marked invariant, vertex shader "
1697 "outputs only\n", var->name);
1698 } else if ((state->target == fragment_shader) &&
1699 !(var->mode == ir_var_in || var->mode == ir_var_inout)) {
1700 /* FINISHME: Note that this doesn't work for invariant on
1701 * a function signature inval
1703 _mesa_glsl_error(& loc, state,
1704 "`%s' cannot be marked invariant, fragment shader "
1705 "inputs only\n", var->name);
1709 if (state->current_function != NULL) {
1710 const char *mode = NULL;
1711 const char *extra = "";
1713 /* There is no need to check for 'inout' here because the parser will
1714 * only allow that in function parameter lists.
1716 if (this->type->qualifier.attribute) {
1717 mode = "attribute";
1718 } else if (this->type->qualifier.uniform) {
1719 mode = "uniform";
1720 } else if (this->type->qualifier.varying) {
1721 mode = "varying";
1722 } else if (this->type->qualifier.in) {
1723 mode = "in";
1724 extra = " or in function parameter list";
1725 } else if (this->type->qualifier.out) {
1726 mode = "out";
1727 extra = " or in function parameter list";
1730 if (mode) {
1731 _mesa_glsl_error(& loc, state,
1732 "%s variable `%s' must be declared at "
1733 "global scope%s",
1734 mode, var->name, extra);
1736 } else if (var->mode == ir_var_in) {
1737 if (state->target == vertex_shader) {
1738 bool error_emitted = false;
1740 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1742 * "Vertex shader inputs can only be float, floating-point
1743 * vectors, matrices, signed and unsigned integers and integer
1744 * vectors. Vertex shader inputs can also form arrays of these
1745 * types, but not structures."
1747 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
1749 * "Vertex shader inputs can only be float, floating-point
1750 * vectors, matrices, signed and unsigned integers and integer
1751 * vectors. They cannot be arrays or structures."
1753 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1755 * "The attribute qualifier can be used only with float,
1756 * floating-point vectors, and matrices. Attribute variables
1757 * cannot be declared as arrays or structures."
1759 const glsl_type *check_type = var->type->is_array()
1760 ? var->type->fields.array : var->type;
1762 switch (check_type->base_type) {
1763 case GLSL_TYPE_FLOAT:
1764 break;
1765 case GLSL_TYPE_UINT:
1766 case GLSL_TYPE_INT:
1767 if (state->language_version > 120)
1768 break;
1769 /* FALLTHROUGH */
1770 default:
1771 _mesa_glsl_error(& loc, state,
1772 "vertex shader input / attribute cannot have "
1773 "type %s`%s'",
1774 var->type->is_array() ? "array of " : "",
1775 check_type->name);
1776 error_emitted = true;
1779 if (!error_emitted && (state->language_version <= 130)
1780 && var->type->is_array()) {
1781 _mesa_glsl_error(& loc, state,
1782 "vertex shader input / attribute cannot have "
1783 "array type");
1784 error_emitted = true;
1789 /* Process the initializer and add its instructions to a temporary
1790 * list. This list will be added to the instruction stream (below) after
1791 * the declaration is added. This is done because in some cases (such as
1792 * redeclarations) the declaration may not actually be added to the
1793 * instruction stream.
1795 exec_list initializer_instructions;
1796 if (decl->initializer != NULL) {
1797 YYLTYPE initializer_loc = decl->initializer->get_location();
1799 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
1801 * "All uniform variables are read-only and are initialized either
1802 * directly by an application via API commands, or indirectly by
1803 * OpenGL."
1805 if ((state->language_version <= 110)
1806 && (var->mode == ir_var_uniform)) {
1807 _mesa_glsl_error(& initializer_loc, state,
1808 "cannot initialize uniforms in GLSL 1.10");
1811 if (var->type->is_sampler()) {
1812 _mesa_glsl_error(& initializer_loc, state,
1813 "cannot initialize samplers");
1816 if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
1817 _mesa_glsl_error(& initializer_loc, state,
1818 "cannot initialize %s shader input / %s",
1819 _mesa_glsl_shader_target_name(state->target),
1820 (state->target == vertex_shader)
1821 ? "attribute" : "varying");
1824 ir_dereference *const lhs = new(ctx) ir_dereference_variable(var);
1825 ir_rvalue *rhs = decl->initializer->hir(&initializer_instructions,
1826 state);
1828 /* Calculate the constant value if this is a const or uniform
1829 * declaration.
1831 if (this->type->qualifier.constant || this->type->qualifier.uniform) {
1832 ir_rvalue *new_rhs = validate_assignment(state, var->type, rhs);
1833 if (new_rhs != NULL) {
1834 rhs = new_rhs;
1836 ir_constant *constant_value = rhs->constant_expression_value();
1837 if (!constant_value) {
1838 _mesa_glsl_error(& initializer_loc, state,
1839 "initializer of %s variable `%s' must be a "
1840 "constant expression",
1841 (this->type->qualifier.constant)
1842 ? "const" : "uniform",
1843 decl->identifier);
1844 if (var->type->is_numeric()) {
1845 /* Reduce cascading errors. */
1846 var->constant_value = ir_constant::zero(ctx, var->type);
1848 } else {
1849 rhs = constant_value;
1850 var->constant_value = constant_value;
1852 } else {
1853 _mesa_glsl_error(&initializer_loc, state,
1854 "initializer of type %s cannot be assigned to "
1855 "variable of type %s",
1856 rhs->type->name, var->type->name);
1857 if (var->type->is_numeric()) {
1858 /* Reduce cascading errors. */
1859 var->constant_value = ir_constant::zero(ctx, var->type);
1864 if (rhs && !rhs->type->is_error()) {
1865 bool temp = var->read_only;
1866 if (this->type->qualifier.constant)
1867 var->read_only = false;
1869 /* Never emit code to initialize a uniform.
1871 if (!this->type->qualifier.uniform)
1872 result = do_assignment(&initializer_instructions, state,
1873 lhs, rhs,
1874 this->get_location());
1875 var->read_only = temp;
1879 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
1881 * "It is an error to write to a const variable outside of
1882 * its declaration, so they must be initialized when
1883 * declared."
1885 if (this->type->qualifier.constant && decl->initializer == NULL) {
1886 _mesa_glsl_error(& loc, state,
1887 "const declaration of `%s' must be initialized");
1890 /* Attempt to add the variable to the symbol table. If this fails, it
1891 * means the variable has already been declared at this scope. Arrays
1892 * fudge this rule a little bit.
1894 * From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
1896 * "It is legal to declare an array without a size and then
1897 * later re-declare the same name as an array of the same
1898 * type and specify a size."
1900 if (state->symbols->name_declared_this_scope(decl->identifier)) {
1901 ir_variable *const earlier =
1902 state->symbols->get_variable(decl->identifier);
1904 if ((earlier != NULL)
1905 && (earlier->type->array_size() == 0)
1906 && var->type->is_array()
1907 && (var->type->element_type() == earlier->type->element_type())) {
1908 /* FINISHME: This doesn't match the qualifiers on the two
1909 * FINISHME: declarations. It's not 100% clear whether this is
1910 * FINISHME: required or not.
1913 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1915 * "The size [of gl_TexCoord] can be at most
1916 * gl_MaxTextureCoords."
1918 const unsigned size = unsigned(var->type->array_size());
1919 if ((strcmp("gl_TexCoord", var->name) == 0)
1920 && (size > state->Const.MaxTextureCoords)) {
1921 YYLTYPE loc = this->get_location();
1923 _mesa_glsl_error(& loc, state, "`gl_TexCoord' array size cannot "
1924 "be larger than gl_MaxTextureCoords (%u)\n",
1925 state->Const.MaxTextureCoords);
1926 } else if ((size > 0) && (size <= earlier->max_array_access)) {
1927 YYLTYPE loc = this->get_location();
1929 _mesa_glsl_error(& loc, state, "array size must be > %u due to "
1930 "previous access",
1931 earlier->max_array_access);
1934 earlier->type = var->type;
1935 delete var;
1936 var = NULL;
1937 } else if (state->extensions->ARB_fragment_coord_conventions &&
1938 (earlier != NULL) &&
1939 (strcmp(var->name, "gl_FragCoord") == 0) &&
1940 earlier->type == var->type &&
1941 earlier->mode == var->mode) {
1942 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
1943 * qualifiers.
1945 earlier->origin_upper_left = var->origin_upper_left;
1946 earlier->pixel_center_integer = var->pixel_center_integer;
1947 } else {
1948 YYLTYPE loc = this->get_location();
1950 _mesa_glsl_error(& loc, state, "`%s' redeclared",
1951 decl->identifier);
1954 continue;
1957 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
1959 * "Identifiers starting with "gl_" are reserved for use by
1960 * OpenGL, and may not be declared in a shader as either a
1961 * variable or a function."
1963 if (strncmp(decl->identifier, "gl_", 3) == 0) {
1964 /* FINISHME: This should only trigger if we're not redefining
1965 * FINISHME: a builtin (to add a qualifier, for example).
1967 _mesa_glsl_error(& loc, state,
1968 "identifier `%s' uses reserved `gl_' prefix",
1969 decl->identifier);
1972 /* Push the variable declaration to the top. It means that all
1973 * the variable declarations will appear in a funny
1974 * last-to-first order, but otherwise we run into trouble if a
1975 * function is prototyped, a global var is decled, then the
1976 * function is defined with usage of the global var. See
1977 * glslparsertest's CorrectModule.frag.
1979 instructions->push_head(var);
1980 instructions->append_list(&initializer_instructions);
1982 /* Add the variable to the symbol table after processing the initializer.
1983 * This differs from most C-like languages, but it follows the GLSL
1984 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
1985 * spec:
1987 * "Within a declaration, the scope of a name starts immediately
1988 * after the initializer if present or immediately after the name
1989 * being declared if not."
1991 const bool added_variable =
1992 state->symbols->add_variable(var->name, var);
1993 assert(added_variable);
1994 (void) added_variable;
1998 /* Generally, variable declarations do not have r-values. However,
1999 * one is used for the declaration in
2001 * while (bool b = some_condition()) {
2002 * ...
2005 * so we return the rvalue from the last seen declaration here.
2007 return result;
2011 ir_rvalue *
2012 ast_parameter_declarator::hir(exec_list *instructions,
2013 struct _mesa_glsl_parse_state *state)
2015 void *ctx = state;
2016 const struct glsl_type *type;
2017 const char *name = NULL;
2018 YYLTYPE loc = this->get_location();
2020 type = this->type->specifier->glsl_type(& name, state);
2022 if (type == NULL) {
2023 if (name != NULL) {
2024 _mesa_glsl_error(& loc, state,
2025 "invalid type `%s' in declaration of `%s'",
2026 name, this->identifier);
2027 } else {
2028 _mesa_glsl_error(& loc, state,
2029 "invalid type in declaration of `%s'",
2030 this->identifier);
2033 type = glsl_type::error_type;
2036 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
2038 * "Functions that accept no input arguments need not use void in the
2039 * argument list because prototypes (or definitions) are required and
2040 * therefore there is no ambiguity when an empty argument list "( )" is
2041 * declared. The idiom "(void)" as a parameter list is provided for
2042 * convenience."
2044 * Placing this check here prevents a void parameter being set up
2045 * for a function, which avoids tripping up checks for main taking
2046 * parameters and lookups of an unnamed symbol.
2048 if (type->is_void()) {
2049 if (this->identifier != NULL)
2050 _mesa_glsl_error(& loc, state,
2051 "named parameter cannot have type `void'");
2053 is_void = true;
2054 return NULL;
2057 if (formal_parameter && (this->identifier == NULL)) {
2058 _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
2059 return NULL;
2062 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
2063 * call already handled the "vec4[..] foo" case.
2065 if (this->is_array) {
2066 type = process_array_type(type, this->array_size, state);
2069 if (type->array_size() == 0) {
2070 _mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
2071 "a declared size.");
2072 type = glsl_type::error_type;
2075 is_void = false;
2076 ir_variable *var = new(ctx) ir_variable(type, this->identifier, ir_var_in);
2078 /* Apply any specified qualifiers to the parameter declaration. Note that
2079 * for function parameters the default mode is 'in'.
2081 apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
2083 instructions->push_tail(var);
2085 /* Parameter declarations do not have r-values.
2087 return NULL;
2091 void
2092 ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
2093 bool formal,
2094 exec_list *ir_parameters,
2095 _mesa_glsl_parse_state *state)
2097 ast_parameter_declarator *void_param = NULL;
2098 unsigned count = 0;
2100 foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
2101 param->formal_parameter = formal;
2102 param->hir(ir_parameters, state);
2104 if (param->is_void)
2105 void_param = param;
2107 count++;
2110 if ((void_param != NULL) && (count > 1)) {
2111 YYLTYPE loc = void_param->get_location();
2113 _mesa_glsl_error(& loc, state,
2114 "`void' parameter must be only parameter");
2119 ir_rvalue *
2120 ast_function::hir(exec_list *instructions,
2121 struct _mesa_glsl_parse_state *state)
2123 void *ctx = state;
2124 ir_function *f = NULL;
2125 ir_function_signature *sig = NULL;
2126 exec_list hir_parameters;
2128 const char *const name = identifier;
2130 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
2132 * "Identifiers starting with "gl_" are reserved for use by
2133 * OpenGL, and may not be declared in a shader as either a
2134 * variable or a function."
2136 if (strncmp(name, "gl_", 3) == 0) {
2137 YYLTYPE loc = this->get_location();
2138 _mesa_glsl_error(&loc, state,
2139 "identifier `%s' uses reserved `gl_' prefix", name);
2142 /* Convert the list of function parameters to HIR now so that they can be
2143 * used below to compare this function's signature with previously seen
2144 * signatures for functions with the same name.
2146 ast_parameter_declarator::parameters_to_hir(& this->parameters,
2147 is_definition,
2148 & hir_parameters, state);
2150 const char *return_type_name;
2151 const glsl_type *return_type =
2152 this->return_type->specifier->glsl_type(& return_type_name, state);
2154 if (!return_type) {
2155 YYLTYPE loc = this->get_location();
2156 _mesa_glsl_error(&loc, state,
2157 "function `%s' has undeclared return type `%s'",
2158 name, return_type_name);
2159 return_type = glsl_type::error_type;
2162 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
2163 * "No qualifier is allowed on the return type of a function."
2165 if (this->return_type->has_qualifiers()) {
2166 YYLTYPE loc = this->get_location();
2167 _mesa_glsl_error(& loc, state,
2168 "function `%s' return type has qualifiers", name);
2171 /* Verify that this function's signature either doesn't match a previously
2172 * seen signature for a function with the same name, or, if a match is found,
2173 * that the previously seen signature does not have an associated definition.
2175 f = state->symbols->get_function(name, false);
2176 if (f != NULL) {
2177 sig = f->exact_matching_signature(&hir_parameters);
2178 if (sig != NULL) {
2179 const char *badvar = sig->qualifiers_match(&hir_parameters);
2180 if (badvar != NULL) {
2181 YYLTYPE loc = this->get_location();
2183 _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
2184 "qualifiers don't match prototype", name, badvar);
2187 if (sig->return_type != return_type) {
2188 YYLTYPE loc = this->get_location();
2190 _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
2191 "match prototype", name);
2194 if (is_definition && sig->is_defined) {
2195 YYLTYPE loc = this->get_location();
2197 _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
2200 } else {
2201 f = new(ctx) ir_function(name);
2202 if (!state->symbols->add_function(f->name, f)) {
2203 /* This function name shadows a non-function use of the same name. */
2204 YYLTYPE loc = this->get_location();
2206 _mesa_glsl_error(&loc, state, "function name `%s' conflicts with "
2207 "non-function", name);
2208 return NULL;
2211 /* Emit the new function header */
2212 instructions->push_tail(f);
2215 /* Verify the return type of main() */
2216 if (strcmp(name, "main") == 0) {
2217 if (! return_type->is_void()) {
2218 YYLTYPE loc = this->get_location();
2220 _mesa_glsl_error(& loc, state, "main() must return void");
2223 if (!hir_parameters.is_empty()) {
2224 YYLTYPE loc = this->get_location();
2226 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
2230 /* Finish storing the information about this new function in its signature.
2232 if (sig == NULL) {
2233 sig = new(ctx) ir_function_signature(return_type);
2234 f->add_signature(sig);
2237 sig->replace_parameters(&hir_parameters);
2238 signature = sig;
2240 /* Function declarations (prototypes) do not have r-values.
2242 return NULL;
2246 ir_rvalue *
2247 ast_function_definition::hir(exec_list *instructions,
2248 struct _mesa_glsl_parse_state *state)
2250 prototype->is_definition = true;
2251 prototype->hir(instructions, state);
2253 ir_function_signature *signature = prototype->signature;
2254 if (signature == NULL)
2255 return NULL;
2257 assert(state->current_function == NULL);
2258 state->current_function = signature;
2259 state->found_return = false;
2261 /* Duplicate parameters declared in the prototype as concrete variables.
2262 * Add these to the symbol table.
2264 state->symbols->push_scope();
2265 foreach_iter(exec_list_iterator, iter, signature->parameters) {
2266 ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
2268 assert(var != NULL);
2270 /* The only way a parameter would "exist" is if two parameters have
2271 * the same name.
2273 if (state->symbols->name_declared_this_scope(var->name)) {
2274 YYLTYPE loc = this->get_location();
2276 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
2277 } else {
2278 state->symbols->add_variable(var->name, var);
2282 /* Convert the body of the function to HIR. */
2283 this->body->hir(&signature->body, state);
2284 signature->is_defined = true;
2286 state->symbols->pop_scope();
2288 assert(state->current_function == signature);
2289 state->current_function = NULL;
2291 if (!signature->return_type->is_void() && !state->found_return) {
2292 YYLTYPE loc = this->get_location();
2293 _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
2294 "%s, but no return statement",
2295 signature->function_name(),
2296 signature->return_type->name);
2299 /* Function definitions do not have r-values.
2301 return NULL;
2305 ir_rvalue *
2306 ast_jump_statement::hir(exec_list *instructions,
2307 struct _mesa_glsl_parse_state *state)
2309 void *ctx = state;
2311 switch (mode) {
2312 case ast_return: {
2313 ir_return *inst;
2314 assert(state->current_function);
2316 if (opt_return_value) {
2317 if (state->current_function->return_type->base_type ==
2318 GLSL_TYPE_VOID) {
2319 YYLTYPE loc = this->get_location();
2321 _mesa_glsl_error(& loc, state,
2322 "`return` with a value, in function `%s' "
2323 "returning void",
2324 state->current_function->function_name());
2327 ir_expression *const ret = (ir_expression *)
2328 opt_return_value->hir(instructions, state);
2329 assert(ret != NULL);
2331 /* Implicit conversions are not allowed for return values. */
2332 if (state->current_function->return_type != ret->type) {
2333 YYLTYPE loc = this->get_location();
2335 _mesa_glsl_error(& loc, state,
2336 "`return' with wrong type %s, in function `%s' "
2337 "returning %s",
2338 ret->type->name,
2339 state->current_function->function_name(),
2340 state->current_function->return_type->name);
2343 inst = new(ctx) ir_return(ret);
2344 } else {
2345 if (state->current_function->return_type->base_type !=
2346 GLSL_TYPE_VOID) {
2347 YYLTYPE loc = this->get_location();
2349 _mesa_glsl_error(& loc, state,
2350 "`return' with no value, in function %s returning "
2351 "non-void",
2352 state->current_function->function_name());
2354 inst = new(ctx) ir_return;
2357 state->found_return = true;
2358 instructions->push_tail(inst);
2359 break;
2362 case ast_discard:
2363 if (state->target != fragment_shader) {
2364 YYLTYPE loc = this->get_location();
2366 _mesa_glsl_error(& loc, state,
2367 "`discard' may only appear in a fragment shader");
2369 instructions->push_tail(new(ctx) ir_discard);
2370 break;
2372 case ast_break:
2373 case ast_continue:
2374 /* FINISHME: Handle switch-statements. They cannot contain 'continue',
2375 * FINISHME: and they use a different IR instruction for 'break'.
2377 /* FINISHME: Correctly handle the nesting. If a switch-statement is
2378 * FINISHME: inside a loop, a 'continue' is valid and will bind to the
2379 * FINISHME: loop.
2381 if (state->loop_or_switch_nesting == NULL) {
2382 YYLTYPE loc = this->get_location();
2384 _mesa_glsl_error(& loc, state,
2385 "`%s' may only appear in a loop",
2386 (mode == ast_break) ? "break" : "continue");
2387 } else {
2388 ir_loop *const loop = state->loop_or_switch_nesting->as_loop();
2390 /* Inline the for loop expression again, since we don't know
2391 * where near the end of the loop body the normal copy of it
2392 * is going to be placed.
2394 if (mode == ast_continue &&
2395 state->loop_or_switch_nesting_ast->rest_expression) {
2396 state->loop_or_switch_nesting_ast->rest_expression->hir(instructions,
2397 state);
2400 if (loop != NULL) {
2401 ir_loop_jump *const jump =
2402 new(ctx) ir_loop_jump((mode == ast_break)
2403 ? ir_loop_jump::jump_break
2404 : ir_loop_jump::jump_continue);
2405 instructions->push_tail(jump);
2409 break;
2412 /* Jump instructions do not have r-values.
2414 return NULL;
2418 ir_rvalue *
2419 ast_selection_statement::hir(exec_list *instructions,
2420 struct _mesa_glsl_parse_state *state)
2422 void *ctx = state;
2424 ir_rvalue *const condition = this->condition->hir(instructions, state);
2426 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
2428 * "Any expression whose type evaluates to a Boolean can be used as the
2429 * conditional expression bool-expression. Vector types are not accepted
2430 * as the expression to if."
2432 * The checks are separated so that higher quality diagnostics can be
2433 * generated for cases where both rules are violated.
2435 if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
2436 YYLTYPE loc = this->condition->get_location();
2438 _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
2439 "boolean");
2442 ir_if *const stmt = new(ctx) ir_if(condition);
2444 if (then_statement != NULL) {
2445 state->symbols->push_scope();
2446 then_statement->hir(& stmt->then_instructions, state);
2447 state->symbols->pop_scope();
2450 if (else_statement != NULL) {
2451 state->symbols->push_scope();
2452 else_statement->hir(& stmt->else_instructions, state);
2453 state->symbols->pop_scope();
2456 instructions->push_tail(stmt);
2458 /* if-statements do not have r-values.
2460 return NULL;
2464 void
2465 ast_iteration_statement::condition_to_hir(ir_loop *stmt,
2466 struct _mesa_glsl_parse_state *state)
2468 void *ctx = state;
2470 if (condition != NULL) {
2471 ir_rvalue *const cond =
2472 condition->hir(& stmt->body_instructions, state);
2474 if ((cond == NULL)
2475 || !cond->type->is_boolean() || !cond->type->is_scalar()) {
2476 YYLTYPE loc = condition->get_location();
2478 _mesa_glsl_error(& loc, state,
2479 "loop condition must be scalar boolean");
2480 } else {
2481 /* As the first code in the loop body, generate a block that looks
2482 * like 'if (!condition) break;' as the loop termination condition.
2484 ir_rvalue *const not_cond =
2485 new(ctx) ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond,
2486 NULL);
2488 ir_if *const if_stmt = new(ctx) ir_if(not_cond);
2490 ir_jump *const break_stmt =
2491 new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
2493 if_stmt->then_instructions.push_tail(break_stmt);
2494 stmt->body_instructions.push_tail(if_stmt);
2500 ir_rvalue *
2501 ast_iteration_statement::hir(exec_list *instructions,
2502 struct _mesa_glsl_parse_state *state)
2504 void *ctx = state;
2506 /* For-loops and while-loops start a new scope, but do-while loops do not.
2508 if (mode != ast_do_while)
2509 state->symbols->push_scope();
2511 if (init_statement != NULL)
2512 init_statement->hir(instructions, state);
2514 ir_loop *const stmt = new(ctx) ir_loop();
2515 instructions->push_tail(stmt);
2517 /* Track the current loop and / or switch-statement nesting.
2519 ir_instruction *const nesting = state->loop_or_switch_nesting;
2520 ast_iteration_statement *nesting_ast = state->loop_or_switch_nesting_ast;
2522 state->loop_or_switch_nesting = stmt;
2523 state->loop_or_switch_nesting_ast = this;
2525 if (mode != ast_do_while)
2526 condition_to_hir(stmt, state);
2528 if (body != NULL)
2529 body->hir(& stmt->body_instructions, state);
2531 if (rest_expression != NULL)
2532 rest_expression->hir(& stmt->body_instructions, state);
2534 if (mode == ast_do_while)
2535 condition_to_hir(stmt, state);
2537 if (mode != ast_do_while)
2538 state->symbols->pop_scope();
2540 /* Restore previous nesting before returning.
2542 state->loop_or_switch_nesting = nesting;
2543 state->loop_or_switch_nesting_ast = nesting_ast;
2545 /* Loops do not have r-values.
2547 return NULL;
2551 ir_rvalue *
2552 ast_type_specifier::hir(exec_list *instructions,
2553 struct _mesa_glsl_parse_state *state)
2555 if (this->structure != NULL)
2556 return this->structure->hir(instructions, state);
2558 return NULL;
2562 ir_rvalue *
2563 ast_struct_specifier::hir(exec_list *instructions,
2564 struct _mesa_glsl_parse_state *state)
2566 unsigned decl_count = 0;
2568 /* Make an initial pass over the list of structure fields to determine how
2569 * many there are. Each element in this list is an ast_declarator_list.
2570 * This means that we actually need to count the number of elements in the
2571 * 'declarations' list in each of the elements.
2573 foreach_list_typed (ast_declarator_list, decl_list, link,
2574 &this->declarations) {
2575 foreach_list_const (decl_ptr, & decl_list->declarations) {
2576 decl_count++;
2581 /* Allocate storage for the structure fields and process the field
2582 * declarations. As the declarations are processed, try to also convert
2583 * the types to HIR. This ensures that structure definitions embedded in
2584 * other structure definitions are processed.
2586 glsl_struct_field *const fields = talloc_array(state, glsl_struct_field,
2587 decl_count);
2589 unsigned i = 0;
2590 foreach_list_typed (ast_declarator_list, decl_list, link,
2591 &this->declarations) {
2592 const char *type_name;
2594 decl_list->type->specifier->hir(instructions, state);
2596 const glsl_type *decl_type =
2597 decl_list->type->specifier->glsl_type(& type_name, state);
2599 foreach_list_typed (ast_declaration, decl, link,
2600 &decl_list->declarations) {
2601 const struct glsl_type *const field_type =
2602 (decl->is_array)
2603 ? process_array_type(decl_type, decl->array_size, state)
2604 : decl_type;
2606 fields[i].type = (field_type != NULL)
2607 ? field_type : glsl_type::error_type;
2608 fields[i].name = decl->identifier;
2609 i++;
2613 assert(i == decl_count);
2615 const char *name;
2616 if (this->name == NULL) {
2617 static unsigned anon_count = 1;
2618 char buf[32];
2620 snprintf(buf, sizeof(buf), "#anon_struct_%04x", anon_count);
2621 anon_count++;
2623 name = strdup(buf);
2624 } else {
2625 name = this->name;
2628 const glsl_type *t =
2629 glsl_type::get_record_instance(fields, decl_count, name);
2631 YYLTYPE loc = this->get_location();
2632 ir_function *ctor = t->generate_constructor();
2633 if (!state->symbols->add_type(name, t, ctor)) {
2634 _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
2635 } else {
2637 const glsl_type **s = (const glsl_type **)
2638 realloc(state->user_structures,
2639 sizeof(state->user_structures[0]) *
2640 (state->num_user_structures + 1));
2641 if (s != NULL) {
2642 s[state->num_user_structures] = t;
2643 state->user_structures = s;
2644 state->num_user_structures++;
2648 /* Structure type definitions do not have r-values.
2650 return NULL;