glsl: fix crash with variable indexing into array in a struct
[mesa.git] / src / glsl / ast_to_hir.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
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:
10 *
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.
14 *
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.
22 */
23
24 /**
25 * \file ast_to_hir.c
26 * Convert abstract syntax to to high-level intermediate reprensentation (HIR).
27 *
28 * During the conversion to HIR, the majority of the symantic checking is
29 * preformed on the program. This includes:
30 *
31 * * Symbol table management
32 * * Type checking
33 * * Function binding
34 *
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.
43 *
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.
47 *
48 * As a result, my preference is to put as little C code as possible in the
49 * parser (and lexer) sources.
50 */
51
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"
58
59 void
60 _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
61 {
62 _mesa_glsl_initialize_variables(instructions, state);
63 _mesa_glsl_initialize_functions(instructions, state);
64
65 state->current_function = NULL;
66
67 foreach_list_typed (ast_node, ast, link, & state->translation_unit)
68 ast->hir(instructions, state);
69 }
70
71
72 /**
73 * If a conversion is available, convert one operand to a different type
74 *
75 * The \c from \c ir_rvalue is converted "in place".
76 *
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
80 *
81 * \return
82 * If a conversion is possible (or unnecessary), \c true is returned.
83 * Otherwise \c false is returned.
84 */
85 static bool
86 apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
87 struct _mesa_glsl_parse_state *state)
88 {
89 void *ctx = state;
90 if (to->base_type == from->type->base_type)
91 return true;
92
93 /* This conversion was added in GLSL 1.20. If the compilation mode is
94 * GLSL 1.10, the conversion is skipped.
95 */
96 if (state->language_version < 120)
97 return false;
98
99 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
100 *
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."
105 */
106 /* FINISHME: The above comment is partially a lie. There is int/uint
107 * FINISHME: conversion for immediate constants.
108 */
109 if (!to->is_float() || !from->type->is_numeric())
110 return false;
111
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.
114 */
115 to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements,
116 from->type->matrix_columns);
117
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);
130 }
131
132 return true;
133 }
134
135
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)
140 {
141 const glsl_type *type_a = value_a->type;
142 const glsl_type *type_b = value_b->type;
143
144 /* From GLSL 1.50 spec, page 56:
145 *
146 * "The arithmetic binary operators add (+), subtract (-),
147 * multiply (*), and divide (/) operate on integer and
148 * floating-point scalars, vectors, and matrices."
149 */
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;
154 }
155
156
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."
160 */
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;
167 }
168 type_a = value_a->type;
169 type_b = value_b->type;
170
171 /* "If the operands are integer types, they must both be signed or
172 * both be unsigned."
173 *
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.
179 */
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;
184 }
185
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
190 *
191 * * The two operands are scalars. In this case the operation is
192 * applied, resulting in a scalar."
193 */
194 if (type_a->is_scalar() && type_b->is_scalar())
195 return type_a;
196
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."
201 */
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;
207 }
208
209 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
210 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
211 * handled.
212 */
213 assert(!type_a->is_scalar());
214 assert(!type_b->is_scalar());
215
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."
219 */
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;
227 }
228 }
229
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.
235 */
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);
239
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."
254 */
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.
264 */
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.
270 */
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);
276
277 return type;
278 }
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.
284 */
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,
291 1);
292 assert(type != glsl_type::error_type);
293
294 return type;
295 }
296 } else {
297 assert(type_b->is_matrix());
298
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.
303 */
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,
310 1);
311 assert(type != glsl_type::error_type);
312
313 return type;
314 }
315 }
316
317 _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
318 return glsl_type::error_type;
319 }
320
321
322 /* "All other cases are illegal."
323 */
324 _mesa_glsl_error(loc, state, "type mismatch");
325 return glsl_type::error_type;
326 }
327
328
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)
332 {
333 /* From GLSL 1.50 spec, page 57:
334 *
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."
340 */
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;
345 }
346
347 return type;
348 }
349
350
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)
355 {
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."
360 */
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;
365 }
366
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."
371 */
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;
378
379 /* "The operator modulus (%) is not defined for any other data types
380 * (non-integer types)."
381 */
382 _mesa_glsl_error(loc, state, "type mismatch");
383 return glsl_type::error_type;
384 }
385
386
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)
390 {
391 const glsl_type *type_a = value_a->type;
392 const glsl_type *type_b = value_b->type;
393
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."
398 */
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;
407 }
408
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."
412 */
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;
419 }
420 type_a = value_a->type;
421 type_b = value_b->type;
422
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;
426 }
427
428 /* "The result is scalar Boolean."
429 */
430 return glsl_type::bool_type;
431 }
432
433
434 /**
435 * Validates that a value can be assigned to a location with a specified type
436 *
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.
440 *
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.
445 *
446 * \note
447 * In addition to being used for assignments, this function is used to
448 * type-check return values.
449 */
450 ir_rvalue *
451 validate_assignment(struct _mesa_glsl_parse_state *state,
452 const glsl_type *lhs_type, ir_rvalue *rhs)
453 {
454 const glsl_type *rhs_type = rhs->type;
455
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.
458 */
459 if (rhs_type->is_error())
460 return rhs;
461
462 /* If the types are identical, the assignment can trivially proceed.
463 */
464 if (rhs_type == lhs_type)
465 return rhs;
466
467 /* If the array element types are the same and the size of the LHS is zero,
468 * the assignment is okay.
469 *
470 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
471 * is handled by ir_dereference::is_lvalue.
472 */
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;
477 }
478
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;
484 }
485
486 return NULL;
487 }
488
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)
493 {
494 void *ctx = state;
495 bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
496
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;
502 }
503 }
504
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;
510
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.
515 */
516 if (lhs->type->array_size() == 0) {
517 ir_dereference *const d = lhs->as_dereference();
518
519 assert(d != NULL);
520
521 ir_variable *const var = d->variable_referenced();
522
523 assert(var != NULL);
524
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);
530 }
531
532 var->type = glsl_type::get_array_instance(lhs->type->element_type(),
533 rhs->type->array_size());
534 d->type = var->type;
535 }
536 }
537
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:
541 *
542 * i = j += 1;
543 *
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.
547 */
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);
556
557 if (!error_emitted)
558 instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var, NULL));
559
560 return new(ctx) ir_dereference_variable(var);
561 }
562
563 static ir_rvalue *
564 get_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
565 {
566 void *ctx = talloc_parent(lvalue);
567 ir_variable *var;
568
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;
574
575 instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
576 lvalue, NULL));
577
578 /* Once we've created this temporary, mark it read only so it's no
579 * longer considered an lvalue.
580 */
581 var->read_only = true;
582
583 return new(ctx) ir_dereference_variable(var);
584 }
585
586
587 ir_rvalue *
588 ast_node::hir(exec_list *instructions,
589 struct _mesa_glsl_parse_state *state)
590 {
591 (void) instructions;
592 (void) state;
593
594 return NULL;
595 }
596
597
598 ir_rvalue *
599 ast_expression::hir(exec_list *instructions,
600 struct _mesa_glsl_parse_state *state)
601 {
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,
628
629 /* Note: The following block of expression types actually convert
630 * to multiple IR instructions.
631 */
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 */
642
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. */
657 };
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;
663
664 loc = this->get_location();
665
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);
670
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;
676 }
677
678 case ast_plus:
679 op[0] = this->subexpressions[0]->hir(instructions, state);
680
681 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
682
683 error_emitted = type->is_error();
684
685 result = op[0];
686 break;
687
688 case ast_neg:
689 op[0] = this->subexpressions[0]->hir(instructions, state);
690
691 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
692
693 error_emitted = type->is_error();
694
695 result = new(ctx) ir_expression(operations[this->oper], type,
696 op[0], NULL);
697 break;
698
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);
705
706 type = arithmetic_result_type(op[0], op[1],
707 (this->oper == ast_mul),
708 state, & loc);
709 error_emitted = type->is_error();
710
711 result = new(ctx) ir_expression(operations[this->oper], type,
712 op[0], op[1]);
713 break;
714
715 case ast_mod:
716 op[0] = this->subexpressions[0]->hir(instructions, state);
717 op[1] = this->subexpressions[1]->hir(instructions, state);
718
719 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
720
721 assert(operations[this->oper] == ir_binop_mod);
722
723 result = new(ctx) ir_expression(operations[this->oper], type,
724 op[0], op[1]);
725 error_emitted = type->is_error();
726 break;
727
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;
733
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);
740
741 type = relational_result_type(op[0], op[1], state, & loc);
742
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.
745 */
746 assert(type->is_error()
747 || ((type->base_type == GLSL_TYPE_BOOL)
748 && type->is_scalar()));
749
750 result = new(ctx) ir_expression(operations[this->oper], type,
751 op[0], op[1]);
752 error_emitted = type->is_error();
753 break;
754
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);
759
760 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
761 *
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."
768 */
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;
780 }
781
782 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
783 op[0], op[1]);
784 type = glsl_type::bool_type;
785
786 assert(result->type == glsl_type::bool_type);
787 break;
788
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;
796
797 case ast_logic_and: {
798 op[0] = this->subexpressions[0]->hir(instructions, state);
799
800 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
801 YYLTYPE loc = this->subexpressions[0]->get_location();
802
803 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
804 operator_string(this->oper));
805 error_emitted = true;
806 }
807
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);
812
813 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
814 YYLTYPE loc = this->subexpressions[1]->get_location();
815
816 _mesa_glsl_error(& loc, state,
817 "RHS of `%s' must be scalar boolean",
818 operator_string(this->oper));
819 error_emitted = true;
820 }
821 result = op[1];
822 } else {
823 result = op0_const;
824 }
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);
831
832 ir_if *const stmt = new(ctx) ir_if(op[0]);
833 instructions->push_tail(stmt);
834
835 op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state);
836
837 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
838 YYLTYPE loc = this->subexpressions[1]->get_location();
839
840 _mesa_glsl_error(& loc, state,
841 "RHS of `%s' must be scalar boolean",
842 operator_string(this->oper));
843 error_emitted = true;
844 }
845
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);
850
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);
855
856 result = new(ctx) ir_dereference_variable(tmp);
857 type = tmp->type;
858 }
859 break;
860 }
861
862 case ast_logic_or: {
863 op[0] = this->subexpressions[0]->hir(instructions, state);
864
865 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
866 YYLTYPE loc = this->subexpressions[0]->get_location();
867
868 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
869 operator_string(this->oper));
870 error_emitted = true;
871 }
872
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);
879
880 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
881 YYLTYPE loc = this->subexpressions[1]->get_location();
882
883 _mesa_glsl_error(& loc, state,
884 "RHS of `%s' must be scalar boolean",
885 operator_string(this->oper));
886 error_emitted = true;
887 }
888 result = op[1];
889 }
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);
896
897 ir_if *const stmt = new(ctx) ir_if(op[0]);
898 instructions->push_tail(stmt);
899
900 op[1] = this->subexpressions[1]->hir(&stmt->else_instructions, state);
901
902 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
903 YYLTYPE loc = this->subexpressions[1]->get_location();
904
905 _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean",
906 operator_string(this->oper));
907 error_emitted = true;
908 }
909
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);
914
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);
919
920 result = new(ctx) ir_dereference_variable(tmp);
921 type = tmp->type;
922 }
923 break;
924 }
925
926 case ast_logic_xor:
927 op[0] = this->subexpressions[0]->hir(instructions, state);
928 op[1] = this->subexpressions[1]->hir(instructions, state);
929
930
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;
935
936 case ast_logic_not:
937 op[0] = this->subexpressions[0]->hir(instructions, state);
938
939 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
940 YYLTYPE loc = this->subexpressions[0]->get_location();
941
942 _mesa_glsl_error(& loc, state,
943 "operand of `!' must be scalar boolean");
944 error_emitted = true;
945 }
946
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;
951
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);
958
959 type = arithmetic_result_type(op[0], op[1],
960 (this->oper == ast_mul_assign),
961 state, & loc);
962
963 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
964 op[0], op[1]);
965
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());
971
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.
975 */
976
977 break;
978 }
979
980 case ast_mod_assign: {
981 op[0] = this->subexpressions[0]->hir(instructions, state);
982 op[1] = this->subexpressions[1]->hir(instructions, state);
983
984 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
985
986 assert(operations[this->oper] == ir_binop_mod);
987
988 ir_rvalue *temp_rhs;
989 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
990 op[0], op[1]);
991
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;
998 }
999
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;
1006
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;
1014
1015 case ast_conditional: {
1016 op[0] = this->subexpressions[0]->hir(instructions, state);
1017
1018 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1019 *
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."
1023 */
1024 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
1025 YYLTYPE loc = this->subexpressions[0]->get_location();
1026
1027 _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean");
1028 error_emitted = true;
1029 }
1030
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.
1035 */
1036 exec_list then_instructions;
1037 exec_list else_instructions;
1038
1039 op[1] = this->subexpressions[1]->hir(&then_instructions, state);
1040 op[2] = this->subexpressions[2]->hir(&else_instructions, state);
1041
1042 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1043 *
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."
1050 */
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();
1055
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;
1062 }
1063
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();
1067
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);
1076
1077 ir_if *const stmt = new(ctx) ir_if(op[0]);
1078 instructions->push_tail(stmt);
1079
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);
1086
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);
1093
1094 result = new(ctx) ir_dereference_variable(tmp);
1095 }
1096 break;
1097 }
1098
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);
1106
1107 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1108
1109 ir_rvalue *temp_rhs;
1110 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1111 op[0], op[1]);
1112
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;
1119 }
1120
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);
1128
1129 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1130
1131 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1132
1133 ir_rvalue *temp_rhs;
1134 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1135 op[0], op[1]);
1136
1137 /* Get a temporary of a copy of the lvalue before it's modified.
1138 * This may get thrown away later.
1139 */
1140 result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
1141
1142 (void)do_assignment(instructions, state,
1143 op[0]->clone(ctx, NULL), temp_rhs,
1144 this->subexpressions[0]->get_location());
1145
1146 type = result->type;
1147 error_emitted = op[0]->type->is_error();
1148 break;
1149 }
1150
1151 case ast_field_selection:
1152 result = _mesa_ast_field_selection_to_hir(this, instructions, state);
1153 type = result->type;
1154 break;
1155
1156 case ast_array_index: {
1157 YYLTYPE index_loc = subexpressions[1]->get_location();
1158
1159 op[0] = subexpressions[0]->hir(instructions, state);
1160 op[1] = subexpressions[1]->hir(instructions, state);
1161
1162 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1163
1164 ir_rvalue *const array = op[0];
1165
1166 result = new(ctx) ir_dereference_array(op[0], op[1]);
1167
1168 /* Do not use op[0] after this point. Use array.
1169 */
1170 op[0] = NULL;
1171
1172
1173 if (error_emitted)
1174 break;
1175
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;
1183 }
1184
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;
1193 }
1194
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.
1199 */
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;
1205
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";
1212 }
1213
1214 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
1215 *
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."
1221 */
1222 if (array->type->is_matrix()) {
1223 if (array->type->row_type()->vector_elements <= idx) {
1224 bound = array->type->row_type()->vector_elements;
1225 }
1226 } else if (array->type->is_vector()) {
1227 if (array->type->vector_elements <= idx) {
1228 bound = array->type->vector_elements;
1229 }
1230 } else {
1231 if ((array->type->array_size() > 0)
1232 && (array->type->array_size() <= idx)) {
1233 bound = array->type->array_size();
1234 }
1235 }
1236
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;
1245 }
1246
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.
1250 *
1251 * FINISHME: Should some methods for getting / setting / testing
1252 * FINISHME: array access limits be added to ir_dereference?
1253 */
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;
1257 }
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.
1266 */
1267 ir_variable *v = array->whole_variable_referenced();
1268 if (v != NULL)
1269 v->max_array_access = array->type->array_size();
1270 }
1271 }
1272
1273 if (error_emitted)
1274 result->type = glsl_type::error_type;
1275
1276 type = result->type;
1277 break;
1278 }
1279
1280 case ast_function_call:
1281 /* Should *NEVER* get here. ast_function_call should always be handled
1282 * by ast_function_expression::hir.
1283 */
1284 assert(0);
1285 break;
1286
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'.
1291 */
1292 ir_variable *var =
1293 state->symbols->get_variable(this->primary_expression.identifier);
1294
1295 result = new(ctx) ir_dereference_variable(var);
1296
1297 if (var != NULL) {
1298 type = result->type;
1299 } else {
1300 _mesa_glsl_error(& loc, state, "`%s' undeclared",
1301 this->primary_expression.identifier);
1302
1303 error_emitted = true;
1304 }
1305 break;
1306 }
1307
1308 case ast_int_constant:
1309 type = glsl_type::int_type;
1310 result = new(ctx) ir_constant(this->primary_expression.int_constant);
1311 break;
1312
1313 case ast_uint_constant:
1314 type = glsl_type::uint_type;
1315 result = new(ctx) ir_constant(this->primary_expression.uint_constant);
1316 break;
1317
1318 case ast_float_constant:
1319 type = glsl_type::float_type;
1320 result = new(ctx) ir_constant(this->primary_expression.float_constant);
1321 break;
1322
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;
1327
1328 case ast_sequence: {
1329 /* It should not be possible to generate a sequence in the AST without
1330 * any expressions in it.
1331 */
1332 assert(!this->expressions.is_empty());
1333
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.
1338 */
1339 foreach_list_typed (ast_node, ast, link, &this->expressions)
1340 result = ast->hir(instructions, state);
1341
1342 type = result->type;
1343
1344 /* Any errors should have already been emitted in the loop above.
1345 */
1346 error_emitted = true;
1347 break;
1348 }
1349 }
1350
1351 if (type->is_error() && !error_emitted)
1352 _mesa_glsl_error(& loc, state, "type mismatch");
1353
1354 return result;
1355 }
1356
1357
1358 ir_rvalue *
1359 ast_expression_statement::hir(exec_list *instructions,
1360 struct _mesa_glsl_parse_state *state)
1361 {
1362 /* It is possible to have expression statements that don't have an
1363 * expression. This is the solitary semicolon:
1364 *
1365 * for (i = 0; i < 5; i++)
1366 * ;
1367 *
1368 * In this case the expression will be NULL. Test for NULL and don't do
1369 * anything in that case.
1370 */
1371 if (expression != NULL)
1372 expression->hir(instructions, state);
1373
1374 /* Statements do not have r-values.
1375 */
1376 return NULL;
1377 }
1378
1379
1380 ir_rvalue *
1381 ast_compound_statement::hir(exec_list *instructions,
1382 struct _mesa_glsl_parse_state *state)
1383 {
1384 if (new_scope)
1385 state->symbols->push_scope();
1386
1387 foreach_list_typed (ast_node, ast, link, &this->statements)
1388 ast->hir(instructions, state);
1389
1390 if (new_scope)
1391 state->symbols->pop_scope();
1392
1393 /* Compound statements do not have r-values.
1394 */
1395 return NULL;
1396 }
1397
1398
1399 static const glsl_type *
1400 process_array_type(const glsl_type *base, ast_node *array_size,
1401 struct _mesa_glsl_parse_state *state)
1402 {
1403 unsigned length = 0;
1404
1405 /* FINISHME: Reject delcarations of multidimensional arrays. */
1406
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();
1411
1412 /* FINISHME: Verify that the grammar forbids side-effects in array
1413 * FINISHME: sizes. i.e., 'vec4 [x = 12] data'
1414 */
1415 assert(dummy_instructions.is_empty());
1416
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();
1424
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];
1433 }
1434 }
1435 }
1436 }
1437
1438 return glsl_type::get_array_instance(base, length);
1439 }
1440
1441
1442 const glsl_type *
1443 ast_type_specifier::glsl_type(const char **name,
1444 struct _mesa_glsl_parse_state *state) const
1445 {
1446 const struct glsl_type *type;
1447
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;
1454
1455 if (this->is_array) {
1456 type = process_array_type(type, this->array_size, state);
1457 }
1458 }
1459
1460 return type;
1461 }
1462
1463
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)
1469 {
1470 if (qual->invariant)
1471 var->invariant = 1;
1472
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;
1477
1478 if (qual->centroid)
1479 var->centroid = 1;
1480
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));
1487 }
1488
1489 /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
1490 *
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."
1494 */
1495 if (qual->varying) {
1496 const glsl_type *non_array_type;
1497
1498 if (var->type && var->type->is_array())
1499 non_array_type = var->type->fields.array;
1500 else
1501 non_array_type = var->type;
1502
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");
1507 }
1508 }
1509
1510 /* If there is no qualifier that changes the mode of the variable, leave
1511 * the setting alone.
1512 */
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;
1522
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;
1529
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";
1536
1537 _mesa_glsl_error(loc, state,
1538 "layout qualifier `%s' can only be applied to "
1539 "fragment shader input `gl_FragCoord'",
1540 qual_string);
1541 }
1542
1543 if (var->type->is_array() && (state->language_version >= 120)) {
1544 var->array_lvalue = true;
1545 }
1546 }
1547
1548
1549 ir_rvalue *
1550 ast_declarator_list::hir(exec_list *instructions,
1551 struct _mesa_glsl_parse_state *state)
1552 {
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();
1558
1559 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
1560 *
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
1564 *
1565 * invariant gl_Position; // make existing gl_Position be invariant"
1566 *
1567 * In these cases the parser will set the 'invariant' flag in the declarator
1568 * list, and the type will be NULL.
1569 */
1570 if (this->invariant) {
1571 assert(this->type == NULL);
1572
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");
1577 }
1578
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);
1583
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;
1602 }
1603 }
1604
1605 /* Invariant redeclarations do not have r-values.
1606 */
1607 return NULL;
1608 }
1609
1610 assert(this->type != NULL);
1611 assert(!this->invariant);
1612
1613 /* The type specifier may contain a structure definition. Process that
1614 * before any of the variable declarations.
1615 */
1616 (void) this->type->specifier->hir(instructions, state);
1617
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;').
1623 */
1624
1625 if (decl_type != NULL) {
1626 } else {
1627 _mesa_glsl_error(& loc, state, "incomplete declaration");
1628 }
1629 }
1630
1631 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
1632 const struct glsl_type *var_type;
1633 ir_variable *var;
1634
1635 /* FINISHME: Emit a warning if a variable declaration shadows a
1636 * FINISHME: declaration at a higher scope.
1637 */
1638
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);
1648 }
1649 continue;
1650 }
1651
1652 if (decl->is_array) {
1653 var_type = process_array_type(decl_type, decl->array_size, state);
1654 } else {
1655 var_type = decl_type;
1656 }
1657
1658 var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto);
1659
1660 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
1661 *
1662 * "Global variables can only use the qualifiers const,
1663 * attribute, uni form, or varying. Only one may be
1664 * specified.
1665 *
1666 * Local variables can only use the qualifier const."
1667 *
1668 * This is relaxed in GLSL 1.30.
1669 */
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);
1676 }
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);
1682 }
1683 /* FINISHME: Test for other invalid qualifiers. */
1684 }
1685
1686 apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
1687 & loc);
1688
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
1694 */
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
1702 */
1703 _mesa_glsl_error(& loc, state,
1704 "`%s' cannot be marked invariant, fragment shader "
1705 "inputs only\n", var->name);
1706 }
1707 }
1708
1709 if (state->current_function != NULL) {
1710 const char *mode = NULL;
1711 const char *extra = "";
1712
1713 /* There is no need to check for 'inout' here because the parser will
1714 * only allow that in function parameter lists.
1715 */
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";
1728 }
1729
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);
1735 }
1736 } else if (var->mode == ir_var_in) {
1737 if (state->target == vertex_shader) {
1738 bool error_emitted = false;
1739
1740 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1741 *
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."
1746 *
1747 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
1748 *
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."
1752 *
1753 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1754 *
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."
1758 */
1759 const glsl_type *check_type = var->type->is_array()
1760 ? var->type->fields.array : var->type;
1761
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;
1777 }
1778
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;
1785 }
1786 }
1787 }
1788
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.
1794 */
1795 exec_list initializer_instructions;
1796 if (decl->initializer != NULL) {
1797 YYLTYPE initializer_loc = decl->initializer->get_location();
1798
1799 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
1800 *
1801 * "All uniform variables are read-only and are initialized either
1802 * directly by an application via API commands, or indirectly by
1803 * OpenGL."
1804 */
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");
1809 }
1810
1811 if (var->type->is_sampler()) {
1812 _mesa_glsl_error(& initializer_loc, state,
1813 "cannot initialize samplers");
1814 }
1815
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");
1822 }
1823
1824 ir_dereference *const lhs = new(ctx) ir_dereference_variable(var);
1825 ir_rvalue *rhs = decl->initializer->hir(&initializer_instructions,
1826 state);
1827
1828 /* Calculate the constant value if this is a const or uniform
1829 * declaration.
1830 */
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;
1835
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);
1847 }
1848 } else {
1849 rhs = constant_value;
1850 var->constant_value = constant_value;
1851 }
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);
1860 }
1861 }
1862 }
1863
1864 if (rhs && !rhs->type->is_error()) {
1865 bool temp = var->read_only;
1866 if (this->type->qualifier.constant)
1867 var->read_only = false;
1868
1869 /* Never emit code to initialize a uniform.
1870 */
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;
1876 }
1877 }
1878
1879 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
1880 *
1881 * "It is an error to write to a const variable outside of
1882 * its declaration, so they must be initialized when
1883 * declared."
1884 */
1885 if (this->type->qualifier.constant && decl->initializer == NULL) {
1886 _mesa_glsl_error(& loc, state,
1887 "const declaration of `%s' must be initialized");
1888 }
1889
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.
1893 *
1894 * From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
1895 *
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."
1899 */
1900 if (state->symbols->name_declared_this_scope(decl->identifier)) {
1901 ir_variable *const earlier =
1902 state->symbols->get_variable(decl->identifier);
1903
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.
1911 */
1912
1913 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1914 *
1915 * "The size [of gl_TexCoord] can be at most
1916 * gl_MaxTextureCoords."
1917 */
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();
1922
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();
1928
1929 _mesa_glsl_error(& loc, state, "array size must be > %u due to "
1930 "previous access",
1931 earlier->max_array_access);
1932 }
1933
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.
1944 */
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();
1949
1950 _mesa_glsl_error(& loc, state, "`%s' redeclared",
1951 decl->identifier);
1952 }
1953
1954 continue;
1955 }
1956
1957 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
1958 *
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."
1962 */
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).
1966 */
1967 _mesa_glsl_error(& loc, state,
1968 "identifier `%s' uses reserved `gl_' prefix",
1969 decl->identifier);
1970 }
1971
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.
1978 */
1979 instructions->push_head(var);
1980 instructions->append_list(&initializer_instructions);
1981
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:
1986 *
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."
1990 */
1991 const bool added_variable =
1992 state->symbols->add_variable(var->name, var);
1993 assert(added_variable);
1994 (void) added_variable;
1995 }
1996
1997
1998 /* Generally, variable declarations do not have r-values. However,
1999 * one is used for the declaration in
2000 *
2001 * while (bool b = some_condition()) {
2002 * ...
2003 * }
2004 *
2005 * so we return the rvalue from the last seen declaration here.
2006 */
2007 return result;
2008 }
2009
2010
2011 ir_rvalue *
2012 ast_parameter_declarator::hir(exec_list *instructions,
2013 struct _mesa_glsl_parse_state *state)
2014 {
2015 void *ctx = state;
2016 const struct glsl_type *type;
2017 const char *name = NULL;
2018 YYLTYPE loc = this->get_location();
2019
2020 type = this->type->specifier->glsl_type(& name, state);
2021
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);
2031 }
2032
2033 type = glsl_type::error_type;
2034 }
2035
2036 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
2037 *
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."
2043 *
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.
2047 */
2048 if (type->is_void()) {
2049 if (this->identifier != NULL)
2050 _mesa_glsl_error(& loc, state,
2051 "named parameter cannot have type `void'");
2052
2053 is_void = true;
2054 return NULL;
2055 }
2056
2057 if (formal_parameter && (this->identifier == NULL)) {
2058 _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
2059 return NULL;
2060 }
2061
2062 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
2063 * call already handled the "vec4[..] foo" case.
2064 */
2065 if (this->is_array) {
2066 type = process_array_type(type, this->array_size, state);
2067 }
2068
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;
2073 }
2074
2075 is_void = false;
2076 ir_variable *var = new(ctx) ir_variable(type, this->identifier, ir_var_in);
2077
2078 /* Apply any specified qualifiers to the parameter declaration. Note that
2079 * for function parameters the default mode is 'in'.
2080 */
2081 apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
2082
2083 instructions->push_tail(var);
2084
2085 /* Parameter declarations do not have r-values.
2086 */
2087 return NULL;
2088 }
2089
2090
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)
2096 {
2097 ast_parameter_declarator *void_param = NULL;
2098 unsigned count = 0;
2099
2100 foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
2101 param->formal_parameter = formal;
2102 param->hir(ir_parameters, state);
2103
2104 if (param->is_void)
2105 void_param = param;
2106
2107 count++;
2108 }
2109
2110 if ((void_param != NULL) && (count > 1)) {
2111 YYLTYPE loc = void_param->get_location();
2112
2113 _mesa_glsl_error(& loc, state,
2114 "`void' parameter must be only parameter");
2115 }
2116 }
2117
2118
2119 ir_rvalue *
2120 ast_function::hir(exec_list *instructions,
2121 struct _mesa_glsl_parse_state *state)
2122 {
2123 void *ctx = state;
2124 ir_function *f = NULL;
2125 ir_function_signature *sig = NULL;
2126 exec_list hir_parameters;
2127
2128 const char *const name = identifier;
2129
2130 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
2131 *
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."
2135 */
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);
2140 }
2141
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.
2145 */
2146 ast_parameter_declarator::parameters_to_hir(& this->parameters,
2147 is_definition,
2148 & hir_parameters, state);
2149
2150 const char *return_type_name;
2151 const glsl_type *return_type =
2152 this->return_type->specifier->glsl_type(& return_type_name, state);
2153
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;
2160 }
2161
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."
2164 */
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);
2169 }
2170
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.
2174 */
2175 f = state->symbols->get_function(name);
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();
2182
2183 _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
2184 "qualifiers don't match prototype", name, badvar);
2185 }
2186
2187 if (sig->return_type != return_type) {
2188 YYLTYPE loc = this->get_location();
2189
2190 _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
2191 "match prototype", name);
2192 }
2193
2194 if (is_definition && sig->is_defined) {
2195 YYLTYPE loc = this->get_location();
2196
2197 _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
2198 }
2199 }
2200 } else if (state->symbols->name_declared_this_scope(name)) {
2201 /* This function name shadows a non-function use of the same name.
2202 */
2203 YYLTYPE loc = this->get_location();
2204
2205 _mesa_glsl_error(& loc, state, "function name `%s' conflicts with "
2206 "non-function", name);
2207 return NULL;
2208 } else {
2209 f = new(ctx) ir_function(name);
2210 state->symbols->add_function(f->name, f);
2211
2212 /* Emit the new function header */
2213 instructions->push_tail(f);
2214 }
2215
2216 /* Verify the return type of main() */
2217 if (strcmp(name, "main") == 0) {
2218 if (! return_type->is_void()) {
2219 YYLTYPE loc = this->get_location();
2220
2221 _mesa_glsl_error(& loc, state, "main() must return void");
2222 }
2223
2224 if (!hir_parameters.is_empty()) {
2225 YYLTYPE loc = this->get_location();
2226
2227 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
2228 }
2229 }
2230
2231 /* Finish storing the information about this new function in its signature.
2232 */
2233 if (sig == NULL) {
2234 sig = new(ctx) ir_function_signature(return_type);
2235 f->add_signature(sig);
2236 }
2237
2238 sig->replace_parameters(&hir_parameters);
2239 signature = sig;
2240
2241 /* Function declarations (prototypes) do not have r-values.
2242 */
2243 return NULL;
2244 }
2245
2246
2247 ir_rvalue *
2248 ast_function_definition::hir(exec_list *instructions,
2249 struct _mesa_glsl_parse_state *state)
2250 {
2251 prototype->is_definition = true;
2252 prototype->hir(instructions, state);
2253
2254 ir_function_signature *signature = prototype->signature;
2255 if (signature == NULL)
2256 return NULL;
2257
2258 assert(state->current_function == NULL);
2259 state->current_function = signature;
2260 state->found_return = false;
2261
2262 /* Duplicate parameters declared in the prototype as concrete variables.
2263 * Add these to the symbol table.
2264 */
2265 state->symbols->push_scope();
2266 foreach_iter(exec_list_iterator, iter, signature->parameters) {
2267 ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
2268
2269 assert(var != NULL);
2270
2271 /* The only way a parameter would "exist" is if two parameters have
2272 * the same name.
2273 */
2274 if (state->symbols->name_declared_this_scope(var->name)) {
2275 YYLTYPE loc = this->get_location();
2276
2277 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
2278 } else {
2279 state->symbols->add_variable(var->name, var);
2280 }
2281 }
2282
2283 /* Convert the body of the function to HIR. */
2284 this->body->hir(&signature->body, state);
2285 signature->is_defined = true;
2286
2287 state->symbols->pop_scope();
2288
2289 assert(state->current_function == signature);
2290 state->current_function = NULL;
2291
2292 if (!signature->return_type->is_void() && !state->found_return) {
2293 YYLTYPE loc = this->get_location();
2294 _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
2295 "%s, but no return statement",
2296 signature->function_name(),
2297 signature->return_type->name);
2298 }
2299
2300 /* Function definitions do not have r-values.
2301 */
2302 return NULL;
2303 }
2304
2305
2306 ir_rvalue *
2307 ast_jump_statement::hir(exec_list *instructions,
2308 struct _mesa_glsl_parse_state *state)
2309 {
2310 void *ctx = state;
2311
2312 switch (mode) {
2313 case ast_return: {
2314 ir_return *inst;
2315 assert(state->current_function);
2316
2317 if (opt_return_value) {
2318 if (state->current_function->return_type->base_type ==
2319 GLSL_TYPE_VOID) {
2320 YYLTYPE loc = this->get_location();
2321
2322 _mesa_glsl_error(& loc, state,
2323 "`return` with a value, in function `%s' "
2324 "returning void",
2325 state->current_function->function_name());
2326 }
2327
2328 ir_expression *const ret = (ir_expression *)
2329 opt_return_value->hir(instructions, state);
2330 assert(ret != NULL);
2331
2332 /* Implicit conversions are not allowed for return values. */
2333 if (state->current_function->return_type != ret->type) {
2334 YYLTYPE loc = this->get_location();
2335
2336 _mesa_glsl_error(& loc, state,
2337 "`return' with wrong type %s, in function `%s' "
2338 "returning %s",
2339 ret->type->name,
2340 state->current_function->function_name(),
2341 state->current_function->return_type->name);
2342 }
2343
2344 inst = new(ctx) ir_return(ret);
2345 } else {
2346 if (state->current_function->return_type->base_type !=
2347 GLSL_TYPE_VOID) {
2348 YYLTYPE loc = this->get_location();
2349
2350 _mesa_glsl_error(& loc, state,
2351 "`return' with no value, in function %s returning "
2352 "non-void",
2353 state->current_function->function_name());
2354 }
2355 inst = new(ctx) ir_return;
2356 }
2357
2358 state->found_return = true;
2359 instructions->push_tail(inst);
2360 break;
2361 }
2362
2363 case ast_discard:
2364 if (state->target != fragment_shader) {
2365 YYLTYPE loc = this->get_location();
2366
2367 _mesa_glsl_error(& loc, state,
2368 "`discard' may only appear in a fragment shader");
2369 }
2370 instructions->push_tail(new(ctx) ir_discard);
2371 break;
2372
2373 case ast_break:
2374 case ast_continue:
2375 /* FINISHME: Handle switch-statements. They cannot contain 'continue',
2376 * FINISHME: and they use a different IR instruction for 'break'.
2377 */
2378 /* FINISHME: Correctly handle the nesting. If a switch-statement is
2379 * FINISHME: inside a loop, a 'continue' is valid and will bind to the
2380 * FINISHME: loop.
2381 */
2382 if (state->loop_or_switch_nesting == NULL) {
2383 YYLTYPE loc = this->get_location();
2384
2385 _mesa_glsl_error(& loc, state,
2386 "`%s' may only appear in a loop",
2387 (mode == ast_break) ? "break" : "continue");
2388 } else {
2389 ir_loop *const loop = state->loop_or_switch_nesting->as_loop();
2390
2391 /* Inline the for loop expression again, since we don't know
2392 * where near the end of the loop body the normal copy of it
2393 * is going to be placed.
2394 */
2395 if (mode == ast_continue &&
2396 state->loop_or_switch_nesting_ast->rest_expression) {
2397 state->loop_or_switch_nesting_ast->rest_expression->hir(instructions,
2398 state);
2399 }
2400
2401 if (loop != NULL) {
2402 ir_loop_jump *const jump =
2403 new(ctx) ir_loop_jump((mode == ast_break)
2404 ? ir_loop_jump::jump_break
2405 : ir_loop_jump::jump_continue);
2406 instructions->push_tail(jump);
2407 }
2408 }
2409
2410 break;
2411 }
2412
2413 /* Jump instructions do not have r-values.
2414 */
2415 return NULL;
2416 }
2417
2418
2419 ir_rvalue *
2420 ast_selection_statement::hir(exec_list *instructions,
2421 struct _mesa_glsl_parse_state *state)
2422 {
2423 void *ctx = state;
2424
2425 ir_rvalue *const condition = this->condition->hir(instructions, state);
2426
2427 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
2428 *
2429 * "Any expression whose type evaluates to a Boolean can be used as the
2430 * conditional expression bool-expression. Vector types are not accepted
2431 * as the expression to if."
2432 *
2433 * The checks are separated so that higher quality diagnostics can be
2434 * generated for cases where both rules are violated.
2435 */
2436 if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
2437 YYLTYPE loc = this->condition->get_location();
2438
2439 _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
2440 "boolean");
2441 }
2442
2443 ir_if *const stmt = new(ctx) ir_if(condition);
2444
2445 if (then_statement != NULL) {
2446 state->symbols->push_scope();
2447 then_statement->hir(& stmt->then_instructions, state);
2448 state->symbols->pop_scope();
2449 }
2450
2451 if (else_statement != NULL) {
2452 state->symbols->push_scope();
2453 else_statement->hir(& stmt->else_instructions, state);
2454 state->symbols->pop_scope();
2455 }
2456
2457 instructions->push_tail(stmt);
2458
2459 /* if-statements do not have r-values.
2460 */
2461 return NULL;
2462 }
2463
2464
2465 void
2466 ast_iteration_statement::condition_to_hir(ir_loop *stmt,
2467 struct _mesa_glsl_parse_state *state)
2468 {
2469 void *ctx = state;
2470
2471 if (condition != NULL) {
2472 ir_rvalue *const cond =
2473 condition->hir(& stmt->body_instructions, state);
2474
2475 if ((cond == NULL)
2476 || !cond->type->is_boolean() || !cond->type->is_scalar()) {
2477 YYLTYPE loc = condition->get_location();
2478
2479 _mesa_glsl_error(& loc, state,
2480 "loop condition must be scalar boolean");
2481 } else {
2482 /* As the first code in the loop body, generate a block that looks
2483 * like 'if (!condition) break;' as the loop termination condition.
2484 */
2485 ir_rvalue *const not_cond =
2486 new(ctx) ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond,
2487 NULL);
2488
2489 ir_if *const if_stmt = new(ctx) ir_if(not_cond);
2490
2491 ir_jump *const break_stmt =
2492 new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
2493
2494 if_stmt->then_instructions.push_tail(break_stmt);
2495 stmt->body_instructions.push_tail(if_stmt);
2496 }
2497 }
2498 }
2499
2500
2501 ir_rvalue *
2502 ast_iteration_statement::hir(exec_list *instructions,
2503 struct _mesa_glsl_parse_state *state)
2504 {
2505 void *ctx = state;
2506
2507 /* For-loops and while-loops start a new scope, but do-while loops do not.
2508 */
2509 if (mode != ast_do_while)
2510 state->symbols->push_scope();
2511
2512 if (init_statement != NULL)
2513 init_statement->hir(instructions, state);
2514
2515 ir_loop *const stmt = new(ctx) ir_loop();
2516 instructions->push_tail(stmt);
2517
2518 /* Track the current loop and / or switch-statement nesting.
2519 */
2520 ir_instruction *const nesting = state->loop_or_switch_nesting;
2521 ast_iteration_statement *nesting_ast = state->loop_or_switch_nesting_ast;
2522
2523 state->loop_or_switch_nesting = stmt;
2524 state->loop_or_switch_nesting_ast = this;
2525
2526 if (mode != ast_do_while)
2527 condition_to_hir(stmt, state);
2528
2529 if (body != NULL)
2530 body->hir(& stmt->body_instructions, state);
2531
2532 if (rest_expression != NULL)
2533 rest_expression->hir(& stmt->body_instructions, state);
2534
2535 if (mode == ast_do_while)
2536 condition_to_hir(stmt, state);
2537
2538 if (mode != ast_do_while)
2539 state->symbols->pop_scope();
2540
2541 /* Restore previous nesting before returning.
2542 */
2543 state->loop_or_switch_nesting = nesting;
2544 state->loop_or_switch_nesting_ast = nesting_ast;
2545
2546 /* Loops do not have r-values.
2547 */
2548 return NULL;
2549 }
2550
2551
2552 ir_rvalue *
2553 ast_type_specifier::hir(exec_list *instructions,
2554 struct _mesa_glsl_parse_state *state)
2555 {
2556 if (this->structure != NULL)
2557 return this->structure->hir(instructions, state);
2558
2559 return NULL;
2560 }
2561
2562
2563 ir_rvalue *
2564 ast_struct_specifier::hir(exec_list *instructions,
2565 struct _mesa_glsl_parse_state *state)
2566 {
2567 unsigned decl_count = 0;
2568
2569 /* Make an initial pass over the list of structure fields to determine how
2570 * many there are. Each element in this list is an ast_declarator_list.
2571 * This means that we actually need to count the number of elements in the
2572 * 'declarations' list in each of the elements.
2573 */
2574 foreach_list_typed (ast_declarator_list, decl_list, link,
2575 &this->declarations) {
2576 foreach_list_const (decl_ptr, & decl_list->declarations) {
2577 decl_count++;
2578 }
2579 }
2580
2581
2582 /* Allocate storage for the structure fields and process the field
2583 * declarations. As the declarations are processed, try to also convert
2584 * the types to HIR. This ensures that structure definitions embedded in
2585 * other structure definitions are processed.
2586 */
2587 glsl_struct_field *const fields = talloc_array(state, glsl_struct_field,
2588 decl_count);
2589
2590 unsigned i = 0;
2591 foreach_list_typed (ast_declarator_list, decl_list, link,
2592 &this->declarations) {
2593 const char *type_name;
2594
2595 decl_list->type->specifier->hir(instructions, state);
2596
2597 const glsl_type *decl_type =
2598 decl_list->type->specifier->glsl_type(& type_name, state);
2599
2600 foreach_list_typed (ast_declaration, decl, link,
2601 &decl_list->declarations) {
2602 const struct glsl_type *const field_type =
2603 (decl->is_array)
2604 ? process_array_type(decl_type, decl->array_size, state)
2605 : decl_type;
2606
2607 fields[i].type = (field_type != NULL)
2608 ? field_type : glsl_type::error_type;
2609 fields[i].name = decl->identifier;
2610 i++;
2611 }
2612 }
2613
2614 assert(i == decl_count);
2615
2616 const char *name;
2617 if (this->name == NULL) {
2618 static unsigned anon_count = 1;
2619 char buf[32];
2620
2621 snprintf(buf, sizeof(buf), "#anon_struct_%04x", anon_count);
2622 anon_count++;
2623
2624 name = strdup(buf);
2625 } else {
2626 name = this->name;
2627 }
2628
2629 const glsl_type *t =
2630 glsl_type::get_record_instance(fields, decl_count, name);
2631
2632 YYLTYPE loc = this->get_location();
2633 if (!state->symbols->add_type(name, t)) {
2634 _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
2635 } else {
2636 /* This logic is a bit tricky. It is an error to declare a structure at
2637 * global scope if there is also a function with the same name.
2638 */
2639 if ((state->current_function == NULL)
2640 && (state->symbols->get_function(name) != NULL)) {
2641 _mesa_glsl_error(& loc, state, "name `%s' previously defined", name);
2642 } else {
2643 t->generate_constructor(state->symbols);
2644 }
2645
2646 const glsl_type **s = (const glsl_type **)
2647 realloc(state->user_structures,
2648 sizeof(state->user_structures[0]) *
2649 (state->num_user_structures + 1));
2650 if (s != NULL) {
2651 s[state->num_user_structures] = t;
2652 state->user_structures = s;
2653 state->num_user_structures++;
2654 }
2655 }
2656
2657 /* Structure type definitions do not have r-values.
2658 */
2659 return NULL;
2660 }