ir_reader: Don't initialize globals, builtins, or constructors.
[mesa.git] / 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 #include <stdio.h>
52 #include "main/imports.h"
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 struct simple_node *ptr;
63
64 _mesa_glsl_initialize_variables(instructions, state);
65 _mesa_glsl_initialize_constructors(instructions, state);
66 _mesa_glsl_initialize_functions(instructions, state);
67
68 state->current_function = NULL;
69
70 foreach (ptr, & state->translation_unit) {
71 ((ast_node *)ptr)->hir(instructions, state);
72 }
73 }
74
75
76 /**
77 * If a conversion is available, convert one operand to a different type
78 *
79 * The \c from \c ir_rvalue is converted "in place".
80 *
81 * \param to Type that the operand it to be converted to
82 * \param from Operand that is being converted
83 * \param state GLSL compiler state
84 *
85 * \return
86 * If a conversion is possible (or unnecessary), \c true is returned.
87 * Otherwise \c false is returned.
88 */
89 static bool
90 apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
91 struct _mesa_glsl_parse_state *state)
92 {
93 if (to->base_type == from->type->base_type)
94 return true;
95
96 /* This conversion was added in GLSL 1.20. If the compilation mode is
97 * GLSL 1.10, the conversion is skipped.
98 */
99 if (state->language_version < 120)
100 return false;
101
102 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
103 *
104 * "There are no implicit array or structure conversions. For
105 * example, an array of int cannot be implicitly converted to an
106 * array of float. There are no implicit conversions between
107 * signed and unsigned integers."
108 */
109 /* FINISHME: The above comment is partially a lie. There is int/uint
110 * FINISHME: conversion for immediate constants.
111 */
112 if (!to->is_float() || !from->type->is_numeric())
113 return false;
114
115 switch (from->type->base_type) {
116 case GLSL_TYPE_INT:
117 from = new ir_expression(ir_unop_i2f, to, from, NULL);
118 break;
119 case GLSL_TYPE_UINT:
120 from = new ir_expression(ir_unop_u2f, to, from, NULL);
121 break;
122 case GLSL_TYPE_BOOL:
123 from = new ir_expression(ir_unop_b2f, to, from, NULL);
124 break;
125 default:
126 assert(0);
127 }
128
129 return true;
130 }
131
132
133 static const struct glsl_type *
134 arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
135 bool multiply,
136 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
137 {
138 const glsl_type *const type_a = value_a->type;
139 const glsl_type *const type_b = value_b->type;
140
141 /* From GLSL 1.50 spec, page 56:
142 *
143 * "The arithmetic binary operators add (+), subtract (-),
144 * multiply (*), and divide (/) operate on integer and
145 * floating-point scalars, vectors, and matrices."
146 */
147 if (!type_a->is_numeric() || !type_b->is_numeric()) {
148 _mesa_glsl_error(loc, state,
149 "Operands to arithmetic operators must be numeric");
150 return glsl_type::error_type;
151 }
152
153
154 /* "If one operand is floating-point based and the other is
155 * not, then the conversions from Section 4.1.10 "Implicit
156 * Conversions" are applied to the non-floating-point-based operand."
157 */
158 if (!apply_implicit_conversion(type_a, value_b, state)
159 && !apply_implicit_conversion(type_b, value_a, state)) {
160 _mesa_glsl_error(loc, state,
161 "Could not implicitly convert operands to "
162 "arithmetic operator");
163 return glsl_type::error_type;
164 }
165
166 /* "If the operands are integer types, they must both be signed or
167 * both be unsigned."
168 *
169 * From this rule and the preceeding conversion it can be inferred that
170 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
171 * The is_numeric check above already filtered out the case where either
172 * type is not one of these, so now the base types need only be tested for
173 * equality.
174 */
175 if (type_a->base_type != type_b->base_type) {
176 _mesa_glsl_error(loc, state,
177 "base type mismatch for arithmetic operator");
178 return glsl_type::error_type;
179 }
180
181 /* "All arithmetic binary operators result in the same fundamental type
182 * (signed integer, unsigned integer, or floating-point) as the
183 * operands they operate on, after operand type conversion. After
184 * conversion, the following cases are valid
185 *
186 * * The two operands are scalars. In this case the operation is
187 * applied, resulting in a scalar."
188 */
189 if (type_a->is_scalar() && type_b->is_scalar())
190 return type_a;
191
192 /* "* One operand is a scalar, and the other is a vector or matrix.
193 * In this case, the scalar operation is applied independently to each
194 * component of the vector or matrix, resulting in the same size
195 * vector or matrix."
196 */
197 if (type_a->is_scalar()) {
198 if (!type_b->is_scalar())
199 return type_b;
200 } else if (type_b->is_scalar()) {
201 return type_a;
202 }
203
204 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
205 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
206 * handled.
207 */
208 assert(!type_a->is_scalar());
209 assert(!type_b->is_scalar());
210
211 /* "* The two operands are vectors of the same size. In this case, the
212 * operation is done component-wise resulting in the same size
213 * vector."
214 */
215 if (type_a->is_vector() && type_b->is_vector()) {
216 if (type_a == type_b) {
217 return type_a;
218 } else {
219 _mesa_glsl_error(loc, state,
220 "vector size mismatch for arithmetic operator");
221 return glsl_type::error_type;
222 }
223 }
224
225 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
226 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
227 * <vector, vector> have been handled. At least one of the operands must
228 * be matrix. Further, since there are no integer matrix types, the base
229 * type of both operands must be float.
230 */
231 assert(type_a->is_matrix() || type_b->is_matrix());
232 assert(type_a->base_type == GLSL_TYPE_FLOAT);
233 assert(type_b->base_type == GLSL_TYPE_FLOAT);
234
235 /* "* The operator is add (+), subtract (-), or divide (/), and the
236 * operands are matrices with the same number of rows and the same
237 * number of columns. In this case, the operation is done component-
238 * wise resulting in the same size matrix."
239 * * The operator is multiply (*), where both operands are matrices or
240 * one operand is a vector and the other a matrix. A right vector
241 * operand is treated as a column vector and a left vector operand as a
242 * row vector. In all these cases, it is required that the number of
243 * columns of the left operand is equal to the number of rows of the
244 * right operand. Then, the multiply (*) operation does a linear
245 * algebraic multiply, yielding an object that has the same number of
246 * rows as the left operand and the same number of columns as the right
247 * operand. Section 5.10 "Vector and Matrix Operations" explains in
248 * more detail how vectors and matrices are operated on."
249 */
250 if (! multiply) {
251 if (type_a == type_b)
252 return type_a;
253 } else {
254 if (type_a->is_matrix() && type_b->is_matrix()) {
255 /* Matrix multiply. The columns of A must match the rows of B. Given
256 * the other previously tested constraints, this means the vector type
257 * of a row from A must be the same as the vector type of a column from
258 * B.
259 */
260 if (type_a->row_type() == type_b->column_type()) {
261 /* The resulting matrix has the number of columns of matrix B and
262 * the number of rows of matrix A. We get the row count of A by
263 * looking at the size of a vector that makes up a column. The
264 * transpose (size of a row) is done for B.
265 */
266 const glsl_type *const type =
267 glsl_type::get_instance(type_a->base_type,
268 type_a->column_type()->vector_elements,
269 type_b->row_type()->vector_elements);
270 assert(type != glsl_type::error_type);
271
272 return type;
273 }
274 } else if (type_a->is_matrix()) {
275 /* A is a matrix and B is a column vector. Columns of A must match
276 * rows of B. Given the other previously tested constraints, this
277 * means the vector type of a row from A must be the same as the
278 * vector the type of B.
279 */
280 if (type_a->row_type() == type_b)
281 return type_b;
282 } else {
283 assert(type_b->is_matrix());
284
285 /* A is a row vector and B is a matrix. Columns of A must match rows
286 * of B. Given the other previously tested constraints, this means
287 * the type of A must be the same as the vector type of a column from
288 * B.
289 */
290 if (type_a == type_b->column_type())
291 return type_a;
292 }
293
294 _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
295 return glsl_type::error_type;
296 }
297
298
299 /* "All other cases are illegal."
300 */
301 _mesa_glsl_error(loc, state, "type mismatch");
302 return glsl_type::error_type;
303 }
304
305
306 static const struct glsl_type *
307 unary_arithmetic_result_type(const struct glsl_type *type,
308 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
309 {
310 /* From GLSL 1.50 spec, page 57:
311 *
312 * "The arithmetic unary operators negate (-), post- and pre-increment
313 * and decrement (-- and ++) operate on integer or floating-point
314 * values (including vectors and matrices). All unary operators work
315 * component-wise on their operands. These result with the same type
316 * they operated on."
317 */
318 if (!type->is_numeric()) {
319 _mesa_glsl_error(loc, state,
320 "Operands to arithmetic operators must be numeric");
321 return glsl_type::error_type;
322 }
323
324 return type;
325 }
326
327
328 static const struct glsl_type *
329 modulus_result_type(const struct glsl_type *type_a,
330 const struct glsl_type *type_b,
331 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
332 {
333 /* From GLSL 1.50 spec, page 56:
334 * "The operator modulus (%) operates on signed or unsigned integers or
335 * integer vectors. The operand types must both be signed or both be
336 * unsigned."
337 */
338 if (!type_a->is_integer() || !type_b->is_integer()
339 || (type_a->base_type != type_b->base_type)) {
340 _mesa_glsl_error(loc, state, "type mismatch");
341 return glsl_type::error_type;
342 }
343
344 /* "The operands cannot be vectors of differing size. If one operand is
345 * a scalar and the other vector, then the scalar is applied component-
346 * wise to the vector, resulting in the same type as the vector. If both
347 * are vectors of the same size, the result is computed component-wise."
348 */
349 if (type_a->is_vector()) {
350 if (!type_b->is_vector()
351 || (type_a->vector_elements == type_b->vector_elements))
352 return type_a;
353 } else
354 return type_b;
355
356 /* "The operator modulus (%) is not defined for any other data types
357 * (non-integer types)."
358 */
359 _mesa_glsl_error(loc, state, "type mismatch");
360 return glsl_type::error_type;
361 }
362
363
364 static const struct glsl_type *
365 relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
366 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
367 {
368 const glsl_type *const type_a = value_a->type;
369 const glsl_type *const type_b = value_b->type;
370
371 /* From GLSL 1.50 spec, page 56:
372 * "The relational operators greater than (>), less than (<), greater
373 * than or equal (>=), and less than or equal (<=) operate only on
374 * scalar integer and scalar floating-point expressions."
375 */
376 if (!type_a->is_numeric()
377 || !type_b->is_numeric()
378 || !type_a->is_scalar()
379 || !type_b->is_scalar()) {
380 _mesa_glsl_error(loc, state,
381 "Operands to relational operators must be scalar and "
382 "numeric");
383 return glsl_type::error_type;
384 }
385
386 /* "Either the operands' types must match, or the conversions from
387 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
388 * operand, after which the types must match."
389 */
390 if (!apply_implicit_conversion(type_a, value_b, state)
391 && !apply_implicit_conversion(type_b, value_a, state)) {
392 _mesa_glsl_error(loc, state,
393 "Could not implicitly convert operands to "
394 "relational operator");
395 return glsl_type::error_type;
396 }
397
398 if (type_a->base_type != type_b->base_type) {
399 _mesa_glsl_error(loc, state, "base type mismatch");
400 return glsl_type::error_type;
401 }
402
403 /* "The result is scalar Boolean."
404 */
405 return glsl_type::bool_type;
406 }
407
408
409 /**
410 * Validates that a value can be assigned to a location with a specified type
411 *
412 * Validates that \c rhs can be assigned to some location. If the types are
413 * not an exact match but an automatic conversion is possible, \c rhs will be
414 * converted.
415 *
416 * \return
417 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
418 * Otherwise the actual RHS to be assigned will be returned. This may be
419 * \c rhs, or it may be \c rhs after some type conversion.
420 *
421 * \note
422 * In addition to being used for assignments, this function is used to
423 * type-check return values.
424 */
425 ir_rvalue *
426 validate_assignment(const glsl_type *lhs_type, ir_rvalue *rhs)
427 {
428 const glsl_type *const rhs_type = rhs->type;
429
430 /* If there is already some error in the RHS, just return it. Anything
431 * else will lead to an avalanche of error message back to the user.
432 */
433 if (rhs_type->is_error())
434 return rhs;
435
436 /* If the types are identical, the assignment can trivially proceed.
437 */
438 if (rhs_type == lhs_type)
439 return rhs;
440
441 /* If the array element types are the same and the size of the LHS is zero,
442 * the assignment is okay.
443 *
444 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
445 * is handled by ir_dereference::is_lvalue.
446 */
447 if (lhs_type->is_array() && rhs->type->is_array()
448 && (lhs_type->element_type() == rhs->type->element_type())
449 && (lhs_type->array_size() == 0)) {
450 return rhs;
451 }
452
453 /* FINISHME: Check for and apply automatic conversions. */
454 return NULL;
455 }
456
457 ir_rvalue *
458 do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
459 ir_rvalue *lhs, ir_rvalue *rhs,
460 YYLTYPE lhs_loc)
461 {
462 bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
463
464 if (!error_emitted) {
465 /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
466 if (!lhs->is_lvalue()) {
467 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
468 error_emitted = true;
469 }
470 }
471
472 ir_rvalue *new_rhs = validate_assignment(lhs->type, rhs);
473 if (new_rhs == NULL) {
474 _mesa_glsl_error(& lhs_loc, state, "type mismatch");
475 } else {
476 rhs = new_rhs;
477
478 /* If the LHS array was not declared with a size, it takes it size from
479 * the RHS. If the LHS is an l-value and a whole array, it must be a
480 * dereference of a variable. Any other case would require that the LHS
481 * is either not an l-value or not a whole array.
482 */
483 if (lhs->type->array_size() == 0) {
484 ir_dereference *const d = lhs->as_dereference();
485
486 assert(d != NULL);
487
488 ir_variable *const var = d->var->as_variable();
489
490 assert(var != NULL);
491
492 if (var->max_array_access >= unsigned(rhs->type->array_size())) {
493 /* FINISHME: This should actually log the location of the RHS. */
494 _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
495 "previous access",
496 var->max_array_access);
497 }
498
499 var->type = glsl_type::get_array_instance(lhs->type->element_type(),
500 rhs->type->array_size());
501 }
502 }
503
504 ir_instruction *tmp = new ir_assignment(lhs, rhs, NULL);
505 instructions->push_tail(tmp);
506
507 return rhs;
508 }
509
510
511 /**
512 * Generate a new temporary and add its declaration to the instruction stream
513 */
514 static ir_variable *
515 generate_temporary(const glsl_type *type, exec_list *instructions,
516 struct _mesa_glsl_parse_state *state)
517 {
518 char *name = (char *) malloc(sizeof(char) * 13);
519
520 snprintf(name, 13, "tmp_%08X", state->temp_index);
521 state->temp_index++;
522
523 ir_variable *const var = new ir_variable(type, name);
524 instructions->push_tail(var);
525
526 return var;
527 }
528
529
530 static ir_rvalue *
531 get_lvalue_copy(exec_list *instructions, struct _mesa_glsl_parse_state *state,
532 ir_rvalue *lvalue, YYLTYPE loc)
533 {
534 ir_variable *var;
535 ir_rvalue *var_deref;
536
537 /* FINISHME: Give unique names to the temporaries. */
538 var = new ir_variable(lvalue->type, "_internal_tmp");
539 var->mode = ir_var_auto;
540
541 var_deref = new ir_dereference(var);
542 do_assignment(instructions, state, var_deref, lvalue, loc);
543
544 /* Once we've created this temporary, mark it read only so it's no
545 * longer considered an lvalue.
546 */
547 var->read_only = true;
548
549 return var_deref;
550 }
551
552
553 ir_rvalue *
554 ast_node::hir(exec_list *instructions,
555 struct _mesa_glsl_parse_state *state)
556 {
557 (void) instructions;
558 (void) state;
559
560 return NULL;
561 }
562
563
564 ir_rvalue *
565 ast_expression::hir(exec_list *instructions,
566 struct _mesa_glsl_parse_state *state)
567 {
568 static const int operations[AST_NUM_OPERATORS] = {
569 -1, /* ast_assign doesn't convert to ir_expression. */
570 -1, /* ast_plus doesn't convert to ir_expression. */
571 ir_unop_neg,
572 ir_binop_add,
573 ir_binop_sub,
574 ir_binop_mul,
575 ir_binop_div,
576 ir_binop_mod,
577 ir_binop_lshift,
578 ir_binop_rshift,
579 ir_binop_less,
580 ir_binop_greater,
581 ir_binop_lequal,
582 ir_binop_gequal,
583 ir_binop_equal,
584 ir_binop_nequal,
585 ir_binop_bit_and,
586 ir_binop_bit_xor,
587 ir_binop_bit_or,
588 ir_unop_bit_not,
589 ir_binop_logic_and,
590 ir_binop_logic_xor,
591 ir_binop_logic_or,
592 ir_unop_logic_not,
593
594 /* Note: The following block of expression types actually convert
595 * to multiple IR instructions.
596 */
597 ir_binop_mul, /* ast_mul_assign */
598 ir_binop_div, /* ast_div_assign */
599 ir_binop_mod, /* ast_mod_assign */
600 ir_binop_add, /* ast_add_assign */
601 ir_binop_sub, /* ast_sub_assign */
602 ir_binop_lshift, /* ast_ls_assign */
603 ir_binop_rshift, /* ast_rs_assign */
604 ir_binop_bit_and, /* ast_and_assign */
605 ir_binop_bit_xor, /* ast_xor_assign */
606 ir_binop_bit_or, /* ast_or_assign */
607
608 -1, /* ast_conditional doesn't convert to ir_expression. */
609 ir_binop_add, /* ast_pre_inc. */
610 ir_binop_sub, /* ast_pre_dec. */
611 ir_binop_add, /* ast_post_inc. */
612 ir_binop_sub, /* ast_post_dec. */
613 -1, /* ast_field_selection doesn't conv to ir_expression. */
614 -1, /* ast_array_index doesn't convert to ir_expression. */
615 -1, /* ast_function_call doesn't conv to ir_expression. */
616 -1, /* ast_identifier doesn't convert to ir_expression. */
617 -1, /* ast_int_constant doesn't convert to ir_expression. */
618 -1, /* ast_uint_constant doesn't conv to ir_expression. */
619 -1, /* ast_float_constant doesn't conv to ir_expression. */
620 -1, /* ast_bool_constant doesn't conv to ir_expression. */
621 -1, /* ast_sequence doesn't convert to ir_expression. */
622 };
623 ir_rvalue *result = NULL;
624 ir_rvalue *op[2];
625 struct simple_node op_list;
626 const struct glsl_type *type = glsl_type::error_type;
627 bool error_emitted = false;
628 YYLTYPE loc;
629
630 loc = this->get_location();
631 make_empty_list(& op_list);
632
633 switch (this->oper) {
634 case ast_assign: {
635 op[0] = this->subexpressions[0]->hir(instructions, state);
636 op[1] = this->subexpressions[1]->hir(instructions, state);
637
638 result = do_assignment(instructions, state, op[0], op[1],
639 this->subexpressions[0]->get_location());
640 error_emitted = result->type->is_error();
641 type = result->type;
642 break;
643 }
644
645 case ast_plus:
646 op[0] = this->subexpressions[0]->hir(instructions, state);
647
648 error_emitted = op[0]->type->is_error();
649 if (type->is_error())
650 op[0]->type = type;
651
652 result = op[0];
653 break;
654
655 case ast_neg:
656 op[0] = this->subexpressions[0]->hir(instructions, state);
657
658 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
659
660 error_emitted = type->is_error();
661
662 result = new ir_expression(operations[this->oper], type,
663 op[0], NULL);
664 break;
665
666 case ast_add:
667 case ast_sub:
668 case ast_mul:
669 case ast_div:
670 op[0] = this->subexpressions[0]->hir(instructions, state);
671 op[1] = this->subexpressions[1]->hir(instructions, state);
672
673 type = arithmetic_result_type(op[0], op[1],
674 (this->oper == ast_mul),
675 state, & loc);
676 error_emitted = type->is_error();
677
678 result = new ir_expression(operations[this->oper], type,
679 op[0], op[1]);
680 break;
681
682 case ast_mod:
683 op[0] = this->subexpressions[0]->hir(instructions, state);
684 op[1] = this->subexpressions[1]->hir(instructions, state);
685
686 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
687
688 assert(operations[this->oper] == ir_binop_mod);
689
690 result = new ir_expression(operations[this->oper], type,
691 op[0], op[1]);
692 error_emitted = type->is_error();
693 break;
694
695 case ast_lshift:
696 case ast_rshift:
697 _mesa_glsl_error(& loc, state, "FINISHME: implement bit-shift operators");
698 error_emitted = true;
699 break;
700
701 case ast_less:
702 case ast_greater:
703 case ast_lequal:
704 case ast_gequal:
705 op[0] = this->subexpressions[0]->hir(instructions, state);
706 op[1] = this->subexpressions[1]->hir(instructions, state);
707
708 type = relational_result_type(op[0], op[1], state, & loc);
709
710 /* The relational operators must either generate an error or result
711 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
712 */
713 assert(type->is_error()
714 || ((type->base_type == GLSL_TYPE_BOOL)
715 && type->is_scalar()));
716
717 result = new ir_expression(operations[this->oper], type,
718 op[0], op[1]);
719 error_emitted = type->is_error();
720 break;
721
722 case ast_nequal:
723 case ast_equal:
724 op[0] = this->subexpressions[0]->hir(instructions, state);
725 op[1] = this->subexpressions[1]->hir(instructions, state);
726
727 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
728 *
729 * "The equality operators equal (==), and not equal (!=)
730 * operate on all types. They result in a scalar Boolean. If
731 * the operand types do not match, then there must be a
732 * conversion from Section 4.1.10 "Implicit Conversions"
733 * applied to one operand that can make them match, in which
734 * case this conversion is done."
735 */
736 if ((!apply_implicit_conversion(op[0]->type, op[1], state)
737 && !apply_implicit_conversion(op[1]->type, op[0], state))
738 || (op[0]->type != op[1]->type)) {
739 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
740 "type", (this->oper == ast_equal) ? "==" : "!=");
741 error_emitted = true;
742 } else if ((state->language_version <= 110)
743 && (op[0]->type->is_array() || op[1]->type->is_array())) {
744 _mesa_glsl_error(& loc, state, "array comparisons forbidden in "
745 "GLSL 1.10");
746 error_emitted = true;
747 }
748
749 result = new ir_expression(operations[this->oper], glsl_type::bool_type,
750 op[0], op[1]);
751 type = glsl_type::bool_type;
752
753 assert(result->type == glsl_type::bool_type);
754 break;
755
756 case ast_bit_and:
757 case ast_bit_xor:
758 case ast_bit_or:
759 case ast_bit_not:
760 _mesa_glsl_error(& loc, state, "FINISHME: implement bit-wise operators");
761 error_emitted = true;
762 break;
763
764 case ast_logic_and: {
765 op[0] = this->subexpressions[0]->hir(instructions, state);
766
767 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
768 YYLTYPE loc = this->subexpressions[0]->get_location();
769
770 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
771 operator_string(this->oper));
772 error_emitted = true;
773 }
774
775 ir_constant *op0_const = op[0]->constant_expression_value();
776 if (op0_const) {
777 if (op0_const->value.b[0]) {
778 op[1] = this->subexpressions[1]->hir(instructions, state);
779
780 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
781 YYLTYPE loc = this->subexpressions[1]->get_location();
782
783 _mesa_glsl_error(& loc, state,
784 "RHS of `%s' must be scalar boolean",
785 operator_string(this->oper));
786 error_emitted = true;
787 }
788 result = op[1];
789 } else {
790 result = op0_const;
791 }
792 type = glsl_type::bool_type;
793 } else {
794 ir_if *const stmt = new ir_if(op[0]);
795 instructions->push_tail(stmt);
796
797 op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state);
798
799 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
800 YYLTYPE loc = this->subexpressions[1]->get_location();
801
802 _mesa_glsl_error(& loc, state,
803 "RHS of `%s' must be scalar boolean",
804 operator_string(this->oper));
805 error_emitted = true;
806 }
807
808 ir_variable *const tmp = generate_temporary(glsl_type::bool_type,
809 instructions, state);
810
811 ir_dereference *const then_deref = new ir_dereference(tmp);
812 ir_assignment *const then_assign =
813 new ir_assignment(then_deref, op[1], NULL);
814 stmt->then_instructions.push_tail(then_assign);
815
816 ir_dereference *const else_deref = new ir_dereference(tmp);
817 ir_assignment *const else_assign =
818 new ir_assignment(else_deref, new ir_constant(false), NULL);
819 stmt->else_instructions.push_tail(else_assign);
820
821 result = new ir_dereference(tmp);
822 type = tmp->type;
823 }
824 break;
825 }
826
827 case ast_logic_or: {
828 op[0] = this->subexpressions[0]->hir(instructions, state);
829
830 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
831 YYLTYPE loc = this->subexpressions[0]->get_location();
832
833 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
834 operator_string(this->oper));
835 error_emitted = true;
836 }
837
838 ir_constant *op0_const = op[0]->constant_expression_value();
839 if (op0_const) {
840 if (op0_const->value.b[0]) {
841 result = op0_const;
842 } else {
843 op[1] = this->subexpressions[1]->hir(instructions, state);
844
845 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
846 YYLTYPE loc = this->subexpressions[1]->get_location();
847
848 _mesa_glsl_error(& loc, state,
849 "RHS of `%s' must be scalar boolean",
850 operator_string(this->oper));
851 error_emitted = true;
852 }
853 result = op[1];
854 }
855 type = glsl_type::bool_type;
856 } else {
857 ir_if *const stmt = new ir_if(op[0]);
858 instructions->push_tail(stmt);
859
860 ir_variable *const tmp = generate_temporary(glsl_type::bool_type,
861 instructions, state);
862
863 op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state);
864
865 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
866 YYLTYPE loc = this->subexpressions[1]->get_location();
867
868 _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean",
869 operator_string(this->oper));
870 error_emitted = true;
871 }
872
873 ir_dereference *const then_deref = new ir_dereference(tmp);
874 ir_assignment *const then_assign =
875 new ir_assignment(then_deref, new ir_constant(true), NULL);
876 stmt->then_instructions.push_tail(then_assign);
877
878 ir_dereference *const else_deref = new ir_dereference(tmp);
879 ir_assignment *const else_assign =
880 new ir_assignment(else_deref, op[1], NULL);
881 stmt->else_instructions.push_tail(else_assign);
882
883 result = new ir_dereference(tmp);
884 type = tmp->type;
885 }
886 break;
887 }
888
889 case ast_logic_xor:
890 op[0] = this->subexpressions[0]->hir(instructions, state);
891 op[1] = this->subexpressions[1]->hir(instructions, state);
892
893
894 result = new ir_expression(operations[this->oper], glsl_type::bool_type,
895 op[0], op[1]);
896 type = glsl_type::bool_type;
897 break;
898
899 case ast_logic_not:
900 op[0] = this->subexpressions[0]->hir(instructions, state);
901
902 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
903 YYLTYPE loc = this->subexpressions[0]->get_location();
904
905 _mesa_glsl_error(& loc, state,
906 "operand of `!' must be scalar boolean");
907 error_emitted = true;
908 }
909
910 result = new ir_expression(operations[this->oper], glsl_type::bool_type,
911 op[0], NULL);
912 type = glsl_type::bool_type;
913 break;
914
915 case ast_mul_assign:
916 case ast_div_assign:
917 case ast_add_assign:
918 case ast_sub_assign: {
919 op[0] = this->subexpressions[0]->hir(instructions, state);
920 op[1] = this->subexpressions[1]->hir(instructions, state);
921
922 type = arithmetic_result_type(op[0], op[1],
923 (this->oper == ast_mul_assign),
924 state, & loc);
925
926 ir_rvalue *temp_rhs = new ir_expression(operations[this->oper], type,
927 op[0], op[1]);
928
929 result = do_assignment(instructions, state, op[0], temp_rhs,
930 this->subexpressions[0]->get_location());
931 type = result->type;
932 error_emitted = (op[0]->type->is_error());
933
934 /* GLSL 1.10 does not allow array assignment. However, we don't have to
935 * explicitly test for this because none of the binary expression
936 * operators allow array operands either.
937 */
938
939 break;
940 }
941
942 case ast_mod_assign: {
943 op[0] = this->subexpressions[0]->hir(instructions, state);
944 op[1] = this->subexpressions[1]->hir(instructions, state);
945
946 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
947
948 assert(operations[this->oper] == ir_binop_mod);
949
950 struct ir_rvalue *temp_rhs;
951 temp_rhs = new ir_expression(operations[this->oper], type,
952 op[0], op[1]);
953
954 result = do_assignment(instructions, state, op[0], temp_rhs,
955 this->subexpressions[0]->get_location());
956 type = result->type;
957 error_emitted = type->is_error();
958 break;
959 }
960
961 case ast_ls_assign:
962 case ast_rs_assign:
963 _mesa_glsl_error(& loc, state,
964 "FINISHME: implement bit-shift assignment operators");
965 error_emitted = true;
966 break;
967
968 case ast_and_assign:
969 case ast_xor_assign:
970 case ast_or_assign:
971 _mesa_glsl_error(& loc, state,
972 "FINISHME: implement logic assignment operators");
973 error_emitted = true;
974 break;
975
976 case ast_conditional: {
977 op[0] = this->subexpressions[0]->hir(instructions, state);
978
979 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
980 *
981 * "The ternary selection operator (?:). It operates on three
982 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
983 * first expression, which must result in a scalar Boolean."
984 */
985 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
986 YYLTYPE loc = this->subexpressions[0]->get_location();
987
988 _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean");
989 error_emitted = true;
990 }
991
992 /* The :? operator is implemented by generating an anonymous temporary
993 * followed by an if-statement. The last instruction in each branch of
994 * the if-statement assigns a value to the anonymous temporary. This
995 * temporary is the r-value of the expression.
996 */
997 ir_variable *const tmp = generate_temporary(glsl_type::error_type,
998 instructions, state);
999
1000 ir_if *const stmt = new ir_if(op[0]);
1001 instructions->push_tail(stmt);
1002
1003 op[1] = this->subexpressions[1]->hir(& stmt->then_instructions, state);
1004 ir_dereference *const then_deref = new ir_dereference(tmp);
1005 ir_assignment *const then_assign =
1006 new ir_assignment(then_deref, op[1], NULL);
1007 stmt->then_instructions.push_tail(then_assign);
1008
1009 op[2] = this->subexpressions[2]->hir(& stmt->else_instructions, state);
1010 ir_dereference *const else_deref = new ir_dereference(tmp);
1011 ir_assignment *const else_assign =
1012 new ir_assignment(else_deref, op[2], NULL);
1013 stmt->else_instructions.push_tail(else_assign);
1014
1015 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1016 *
1017 * "The second and third expressions can be any type, as
1018 * long their types match, or there is a conversion in
1019 * Section 4.1.10 "Implicit Conversions" that can be applied
1020 * to one of the expressions to make their types match. This
1021 * resulting matching type is the type of the entire
1022 * expression."
1023 */
1024 if ((!apply_implicit_conversion(op[1]->type, op[2], state)
1025 && !apply_implicit_conversion(op[2]->type, op[1], state))
1026 || (op[1]->type != op[2]->type)) {
1027 YYLTYPE loc = this->subexpressions[1]->get_location();
1028
1029 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
1030 "operator must have matching types.");
1031 error_emitted = true;
1032 } else {
1033 tmp->type = op[1]->type;
1034 }
1035
1036 result = new ir_dereference(tmp);
1037 type = tmp->type;
1038 break;
1039 }
1040
1041 case ast_pre_inc:
1042 case ast_pre_dec: {
1043 op[0] = this->subexpressions[0]->hir(instructions, state);
1044 if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
1045 op[1] = new ir_constant(1.0f);
1046 else
1047 op[1] = new ir_constant(1);
1048
1049 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1050
1051 struct ir_rvalue *temp_rhs;
1052 temp_rhs = new ir_expression(operations[this->oper], type,
1053 op[0], op[1]);
1054
1055 result = do_assignment(instructions, state, op[0], temp_rhs,
1056 this->subexpressions[0]->get_location());
1057 type = result->type;
1058 error_emitted = op[0]->type->is_error();
1059 break;
1060 }
1061
1062 case ast_post_inc:
1063 case ast_post_dec: {
1064 op[0] = this->subexpressions[0]->hir(instructions, state);
1065 if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
1066 op[1] = new ir_constant(1.0f);
1067 else
1068 op[1] = new ir_constant(1);
1069
1070 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1071
1072 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1073
1074 struct ir_rvalue *temp_rhs;
1075 temp_rhs = new ir_expression(operations[this->oper], type,
1076 op[0], op[1]);
1077
1078 /* Get a temporary of a copy of the lvalue before it's modified.
1079 * This may get thrown away later.
1080 */
1081 result = get_lvalue_copy(instructions, state, op[0],
1082 this->subexpressions[0]->get_location());
1083
1084 (void)do_assignment(instructions, state, op[0], temp_rhs,
1085 this->subexpressions[0]->get_location());
1086
1087 type = result->type;
1088 error_emitted = op[0]->type->is_error();
1089 break;
1090 }
1091
1092 case ast_field_selection:
1093 result = _mesa_ast_field_selection_to_hir(this, instructions, state);
1094 type = result->type;
1095 break;
1096
1097 case ast_array_index: {
1098 YYLTYPE index_loc = subexpressions[1]->get_location();
1099
1100 op[0] = subexpressions[0]->hir(instructions, state);
1101 op[1] = subexpressions[1]->hir(instructions, state);
1102
1103 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1104
1105 ir_dereference *const lhs = op[0]->as_dereference();
1106 ir_instruction *array;
1107 if ((lhs != NULL)
1108 && (lhs->mode == ir_dereference::ir_reference_variable)) {
1109 result = new ir_dereference(lhs->var, op[1]);
1110
1111 delete op[0];
1112 array = lhs->var;
1113 } else {
1114 result = new ir_dereference(op[0], op[1]);
1115 array = op[0];
1116 }
1117
1118 /* Do not use op[0] after this point. Use array.
1119 */
1120 op[0] = NULL;
1121
1122
1123 if (error_emitted)
1124 break;
1125
1126 if (!array->type->is_array()
1127 && !array->type->is_matrix()
1128 && !array->type->is_vector()) {
1129 _mesa_glsl_error(& index_loc, state,
1130 "cannot dereference non-array / non-matrix / "
1131 "non-vector");
1132 error_emitted = true;
1133 }
1134
1135 if (!op[1]->type->is_integer()) {
1136 _mesa_glsl_error(& index_loc, state,
1137 "array index must be integer type");
1138 error_emitted = true;
1139 } else if (!op[1]->type->is_scalar()) {
1140 _mesa_glsl_error(& index_loc, state,
1141 "array index must be scalar");
1142 error_emitted = true;
1143 }
1144
1145 /* If the array index is a constant expression and the array has a
1146 * declared size, ensure that the access is in-bounds. If the array
1147 * index is not a constant expression, ensure that the array has a
1148 * declared size.
1149 */
1150 ir_constant *const const_index = op[1]->constant_expression_value();
1151 if (const_index != NULL) {
1152 const int idx = const_index->value.i[0];
1153 const char *type_name;
1154 unsigned bound = 0;
1155
1156 if (array->type->is_matrix()) {
1157 type_name = "matrix";
1158 } else if (array->type->is_vector()) {
1159 type_name = "vector";
1160 } else {
1161 type_name = "array";
1162 }
1163
1164 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
1165 *
1166 * "It is illegal to declare an array with a size, and then
1167 * later (in the same shader) index the same array with an
1168 * integral constant expression greater than or equal to the
1169 * declared size. It is also illegal to index an array with a
1170 * negative constant expression."
1171 */
1172 if (array->type->is_matrix()) {
1173 if (array->type->row_type()->vector_elements <= idx) {
1174 bound = array->type->row_type()->vector_elements;
1175 }
1176 } else if (array->type->is_vector()) {
1177 if (array->type->vector_elements <= idx) {
1178 bound = array->type->vector_elements;
1179 }
1180 } else {
1181 if ((array->type->array_size() > 0)
1182 && (array->type->array_size() <= idx)) {
1183 bound = array->type->array_size();
1184 }
1185 }
1186
1187 if (bound > 0) {
1188 _mesa_glsl_error(& loc, state, "%s index must be < %u",
1189 type_name, bound);
1190 error_emitted = true;
1191 } else if (idx < 0) {
1192 _mesa_glsl_error(& loc, state, "%s index must be >= 0",
1193 type_name);
1194 error_emitted = true;
1195 }
1196
1197 if (array->type->is_array()) {
1198 ir_variable *const v = array->as_variable();
1199 if ((v != NULL) && (unsigned(idx) > v->max_array_access))
1200 v->max_array_access = idx;
1201 }
1202 }
1203
1204 if (error_emitted)
1205 result->type = glsl_type::error_type;
1206
1207 type = result->type;
1208 break;
1209 }
1210
1211 case ast_function_call:
1212 /* Should *NEVER* get here. ast_function_call should always be handled
1213 * by ast_function_expression::hir.
1214 */
1215 assert(0);
1216 break;
1217
1218 case ast_identifier: {
1219 /* ast_identifier can appear several places in a full abstract syntax
1220 * tree. This particular use must be at location specified in the grammar
1221 * as 'variable_identifier'.
1222 */
1223 ir_variable *var =
1224 state->symbols->get_variable(this->primary_expression.identifier);
1225
1226 result = new ir_dereference(var);
1227
1228 if (var != NULL) {
1229 type = result->type;
1230 } else {
1231 _mesa_glsl_error(& loc, state, "`%s' undeclared",
1232 this->primary_expression.identifier);
1233
1234 error_emitted = true;
1235 }
1236 break;
1237 }
1238
1239 case ast_int_constant:
1240 type = glsl_type::int_type;
1241 result = new ir_constant(type, & this->primary_expression);
1242 break;
1243
1244 case ast_uint_constant:
1245 type = glsl_type::uint_type;
1246 result = new ir_constant(type, & this->primary_expression);
1247 break;
1248
1249 case ast_float_constant:
1250 type = glsl_type::float_type;
1251 result = new ir_constant(type, & this->primary_expression);
1252 break;
1253
1254 case ast_bool_constant:
1255 type = glsl_type::bool_type;
1256 result = new ir_constant(type, & this->primary_expression);
1257 break;
1258
1259 case ast_sequence: {
1260 struct simple_node *ptr;
1261
1262 /* It should not be possible to generate a sequence in the AST without
1263 * any expressions in it.
1264 */
1265 assert(!is_empty_list(&this->expressions));
1266
1267 /* The r-value of a sequence is the last expression in the sequence. If
1268 * the other expressions in the sequence do not have side-effects (and
1269 * therefore add instructions to the instruction list), they get dropped
1270 * on the floor.
1271 */
1272 foreach (ptr, &this->expressions)
1273 result = ((ast_node *)ptr)->hir(instructions, state);
1274
1275 type = result->type;
1276
1277 /* Any errors should have already been emitted in the loop above.
1278 */
1279 error_emitted = true;
1280 break;
1281 }
1282 }
1283
1284 if (type->is_error() && !error_emitted)
1285 _mesa_glsl_error(& loc, state, "type mismatch");
1286
1287 return result;
1288 }
1289
1290
1291 ir_rvalue *
1292 ast_expression_statement::hir(exec_list *instructions,
1293 struct _mesa_glsl_parse_state *state)
1294 {
1295 /* It is possible to have expression statements that don't have an
1296 * expression. This is the solitary semicolon:
1297 *
1298 * for (i = 0; i < 5; i++)
1299 * ;
1300 *
1301 * In this case the expression will be NULL. Test for NULL and don't do
1302 * anything in that case.
1303 */
1304 if (expression != NULL)
1305 expression->hir(instructions, state);
1306
1307 /* Statements do not have r-values.
1308 */
1309 return NULL;
1310 }
1311
1312
1313 ir_rvalue *
1314 ast_compound_statement::hir(exec_list *instructions,
1315 struct _mesa_glsl_parse_state *state)
1316 {
1317 struct simple_node *ptr;
1318
1319
1320 if (new_scope)
1321 state->symbols->push_scope();
1322
1323 foreach (ptr, &statements)
1324 ((ast_node *)ptr)->hir(instructions, state);
1325
1326 if (new_scope)
1327 state->symbols->pop_scope();
1328
1329 /* Compound statements do not have r-values.
1330 */
1331 return NULL;
1332 }
1333
1334
1335 static const glsl_type *
1336 process_array_type(const glsl_type *base, ast_node *array_size,
1337 struct _mesa_glsl_parse_state *state)
1338 {
1339 unsigned length = 0;
1340
1341 /* FINISHME: Reject delcarations of multidimensional arrays. */
1342
1343 if (array_size != NULL) {
1344 exec_list dummy_instructions;
1345 ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
1346 YYLTYPE loc = array_size->get_location();
1347
1348 /* FINISHME: Verify that the grammar forbids side-effects in array
1349 * FINISHME: sizes. i.e., 'vec4 [x = 12] data'
1350 */
1351 assert(dummy_instructions.is_empty());
1352
1353 if (ir != NULL) {
1354 if (!ir->type->is_integer()) {
1355 _mesa_glsl_error(& loc, state, "array size must be integer type");
1356 } else if (!ir->type->is_scalar()) {
1357 _mesa_glsl_error(& loc, state, "array size must be scalar type");
1358 } else {
1359 ir_constant *const size = ir->constant_expression_value();
1360
1361 if (size == NULL) {
1362 _mesa_glsl_error(& loc, state, "array size must be a "
1363 "constant valued expression");
1364 } else if (size->value.i[0] <= 0) {
1365 _mesa_glsl_error(& loc, state, "array size must be > 0");
1366 } else {
1367 assert(size->type == ir->type);
1368 length = size->value.u[0];
1369 }
1370 }
1371 }
1372 }
1373
1374 return glsl_type::get_array_instance(base, length);
1375 }
1376
1377
1378 const glsl_type *
1379 ast_type_specifier::glsl_type(const char **name,
1380 struct _mesa_glsl_parse_state *state) const
1381 {
1382 const struct glsl_type *type;
1383
1384 if (this->type_specifier == ast_struct) {
1385 /* FINISHME: Handle annonymous structures. */
1386 type = NULL;
1387 } else {
1388 type = state->symbols->get_type(this->type_name);
1389 *name = this->type_name;
1390
1391 if (this->is_array) {
1392 type = process_array_type(type, this->array_size, state);
1393 }
1394 }
1395
1396 return type;
1397 }
1398
1399
1400 static void
1401 apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
1402 struct ir_variable *var,
1403 struct _mesa_glsl_parse_state *state,
1404 YYLTYPE *loc)
1405 {
1406 if (qual->invariant)
1407 var->invariant = 1;
1408
1409 /* FINISHME: Mark 'in' variables at global scope as read-only. */
1410 if (qual->constant || qual->attribute || qual->uniform
1411 || (qual->varying && (state->target == fragment_shader)))
1412 var->read_only = 1;
1413
1414 if (qual->centroid)
1415 var->centroid = 1;
1416
1417 if (qual->attribute && state->target != vertex_shader) {
1418 var->type = glsl_type::error_type;
1419 _mesa_glsl_error(loc, state,
1420 "`attribute' variables may not be declared in the "
1421 "%s shader",
1422 _mesa_glsl_shader_target_name(state->target));
1423 }
1424
1425 /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
1426 *
1427 * "The varying qualifier can be used only with the data types
1428 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
1429 * these."
1430 */
1431 if (qual->varying && var->type->base_type != GLSL_TYPE_FLOAT) {
1432 var->type = glsl_type::error_type;
1433 _mesa_glsl_error(loc, state,
1434 "varying variables must be of base type float");
1435 }
1436
1437 if (qual->in && qual->out)
1438 var->mode = ir_var_inout;
1439 else if (qual->attribute || qual->in
1440 || (qual->varying && (state->target == fragment_shader)))
1441 var->mode = ir_var_in;
1442 else if (qual->out || (qual->varying && (state->target == vertex_shader)))
1443 var->mode = ir_var_out;
1444 else if (qual->uniform)
1445 var->mode = ir_var_uniform;
1446 else
1447 var->mode = ir_var_auto;
1448
1449 if (qual->uniform)
1450 var->shader_in = true;
1451 if (qual->varying) {
1452 if (qual->in)
1453 var->shader_in = true;
1454 if (qual->out)
1455 var->shader_out = true;
1456 }
1457
1458 if (qual->flat)
1459 var->interpolation = ir_var_flat;
1460 else if (qual->noperspective)
1461 var->interpolation = ir_var_noperspective;
1462 else
1463 var->interpolation = ir_var_smooth;
1464
1465 if (var->type->is_array() && (state->language_version >= 120)) {
1466 var->array_lvalue = true;
1467 }
1468 }
1469
1470
1471 ir_rvalue *
1472 ast_declarator_list::hir(exec_list *instructions,
1473 struct _mesa_glsl_parse_state *state)
1474 {
1475 struct simple_node *ptr;
1476 const struct glsl_type *decl_type;
1477 const char *type_name = NULL;
1478 ir_rvalue *result = NULL;
1479
1480 /* FINISHME: Handle vertex shader "invariant" declarations that do not
1481 * FINISHME: include a type. These re-declare built-in variables to be
1482 * FINISHME: invariant.
1483 */
1484
1485 decl_type = this->type->specifier->glsl_type(& type_name, state);
1486
1487 foreach (ptr, &this->declarations) {
1488 struct ast_declaration *const decl = (struct ast_declaration * )ptr;
1489 const struct glsl_type *var_type;
1490 struct ir_variable *var;
1491 YYLTYPE loc = this->get_location();
1492
1493 /* FINISHME: Emit a warning if a variable declaration shadows a
1494 * FINISHME: declaration at a higher scope.
1495 */
1496
1497 if ((decl_type == NULL) || decl_type->is_void()) {
1498 if (type_name != NULL) {
1499 _mesa_glsl_error(& loc, state,
1500 "invalid type `%s' in declaration of `%s'",
1501 type_name, decl->identifier);
1502 } else {
1503 _mesa_glsl_error(& loc, state,
1504 "invalid type in declaration of `%s'",
1505 decl->identifier);
1506 }
1507 continue;
1508 }
1509
1510 if (decl->is_array) {
1511 var_type = process_array_type(decl_type, decl->array_size, state);
1512 } else {
1513 var_type = decl_type;
1514 }
1515
1516 var = new ir_variable(var_type, decl->identifier);
1517
1518 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
1519 *
1520 * "Global variables can only use the qualifiers const,
1521 * attribute, uni form, or varying. Only one may be
1522 * specified.
1523 *
1524 * Local variables can only use the qualifier const."
1525 *
1526 * This is relaxed in GLSL 1.30.
1527 */
1528 if (state->language_version < 120) {
1529 if (this->type->qualifier.out) {
1530 _mesa_glsl_error(& loc, state,
1531 "`out' qualifier in declaration of `%s' "
1532 "only valid for function parameters in GLSL 1.10.",
1533 decl->identifier);
1534 }
1535 if (this->type->qualifier.in) {
1536 _mesa_glsl_error(& loc, state,
1537 "`in' qualifier in declaration of `%s' "
1538 "only valid for function parameters in GLSL 1.10.",
1539 decl->identifier);
1540 }
1541 /* FINISHME: Test for other invalid qualifiers. */
1542 }
1543
1544 apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
1545 & loc);
1546
1547 /* Attempt to add the variable to the symbol table. If this fails, it
1548 * means the variable has already been declared at this scope. Arrays
1549 * fudge this rule a little bit.
1550 *
1551 * From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
1552 *
1553 * "It is legal to declare an array without a size and then
1554 * later re-declare the same name as an array of the same
1555 * type and specify a size."
1556 */
1557 if (state->symbols->name_declared_this_scope(decl->identifier)) {
1558 ir_variable *const earlier =
1559 state->symbols->get_variable(decl->identifier);
1560
1561 if ((earlier != NULL)
1562 && (earlier->type->array_size() == 0)
1563 && var->type->is_array()
1564 && (var->type->element_type() == earlier->type->element_type())) {
1565 /* FINISHME: This doesn't match the qualifiers on the two
1566 * FINISHME: declarations. It's not 100% clear whether this is
1567 * FINISHME: required or not.
1568 */
1569
1570 if (var->type->array_size() <= (int)earlier->max_array_access) {
1571 YYLTYPE loc = this->get_location();
1572
1573 _mesa_glsl_error(& loc, state, "array size must be > %u due to "
1574 "previous access",
1575 earlier->max_array_access);
1576 }
1577
1578 earlier->type = var->type;
1579 delete var;
1580 var = NULL;
1581 } else {
1582 YYLTYPE loc = this->get_location();
1583
1584 _mesa_glsl_error(& loc, state, "`%s' redeclared",
1585 decl->identifier);
1586 }
1587
1588 continue;
1589 }
1590
1591 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
1592 *
1593 * "Identifiers starting with "gl_" are reserved for use by
1594 * OpenGL, and may not be declared in a shader as either a
1595 * variable or a function."
1596 */
1597 if (strncmp(decl->identifier, "gl_", 3) == 0) {
1598 /* FINISHME: This should only trigger if we're not redefining
1599 * FINISHME: a builtin (to add a qualifier, for example).
1600 */
1601 _mesa_glsl_error(& loc, state,
1602 "identifier `%s' uses reserved `gl_' prefix",
1603 decl->identifier);
1604 }
1605
1606 instructions->push_tail(var);
1607
1608 if (state->current_function != NULL) {
1609 const char *mode = NULL;
1610 const char *extra = "";
1611
1612 /* There is no need to check for 'inout' here because the parser will
1613 * only allow that in function parameter lists.
1614 */
1615 if (this->type->qualifier.attribute) {
1616 mode = "attribute";
1617 } else if (this->type->qualifier.uniform) {
1618 mode = "uniform";
1619 } else if (this->type->qualifier.varying) {
1620 mode = "varying";
1621 } else if (this->type->qualifier.in) {
1622 mode = "in";
1623 extra = " or in function parameter list";
1624 } else if (this->type->qualifier.out) {
1625 mode = "out";
1626 extra = " or in function parameter list";
1627 }
1628
1629 if (mode) {
1630 _mesa_glsl_error(& loc, state,
1631 "%s variable `%s' must be declared at "
1632 "global scope%s",
1633 mode, var->name, extra);
1634 }
1635 } else if (var->mode == ir_var_in) {
1636 if (state->target == vertex_shader) {
1637 bool error_emitted = false;
1638
1639 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1640 *
1641 * "Vertex shader inputs can only be float, floating-point
1642 * vectors, matrices, signed and unsigned integers and integer
1643 * vectors. Vertex shader inputs can also form arrays of these
1644 * types, but not structures."
1645 *
1646 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
1647 *
1648 * "Vertex shader inputs can only be float, floating-point
1649 * vectors, matrices, signed and unsigned integers and integer
1650 * vectors. They cannot be arrays or structures."
1651 *
1652 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1653 *
1654 * "The attribute qualifier can be used only with float,
1655 * floating-point vectors, and matrices. Attribute variables
1656 * cannot be declared as arrays or structures."
1657 */
1658 const glsl_type *check_type = var->type->is_array()
1659 ? var->type->fields.array : var->type;
1660
1661 switch (check_type->base_type) {
1662 case GLSL_TYPE_FLOAT:
1663 break;
1664 case GLSL_TYPE_UINT:
1665 case GLSL_TYPE_INT:
1666 if (state->language_version > 120)
1667 break;
1668 /* FALLTHROUGH */
1669 default:
1670 _mesa_glsl_error(& loc, state,
1671 "vertex shader input / attribute cannot have "
1672 "type %s`%s'",
1673 var->type->is_array() ? "array of " : "",
1674 check_type->name);
1675 error_emitted = true;
1676 }
1677
1678 if (!error_emitted && (state->language_version <= 130)
1679 && var->type->is_array()) {
1680 _mesa_glsl_error(& loc, state,
1681 "vertex shader input / attribute cannot have "
1682 "array type");
1683 error_emitted = true;
1684 }
1685 }
1686 }
1687
1688 if (decl->initializer != NULL) {
1689 YYLTYPE initializer_loc = decl->initializer->get_location();
1690
1691 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
1692 *
1693 * "All uniform variables are read-only and are initialized either
1694 * directly by an application via API commands, or indirectly by
1695 * OpenGL."
1696 */
1697 if ((state->language_version <= 110)
1698 && (var->mode == ir_var_uniform)) {
1699 _mesa_glsl_error(& initializer_loc, state,
1700 "cannot initialize uniforms in GLSL 1.10");
1701 }
1702
1703 if (var->type->is_sampler()) {
1704 _mesa_glsl_error(& initializer_loc, state,
1705 "cannot initialize samplers");
1706 }
1707
1708 if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
1709 _mesa_glsl_error(& initializer_loc, state,
1710 "cannot initialize %s shader input / %s",
1711 _mesa_glsl_shader_target_name(state->target),
1712 (state->target == vertex_shader)
1713 ? "attribute" : "varying");
1714 }
1715
1716 ir_dereference *const lhs = new ir_dereference(var);
1717 ir_rvalue *rhs = decl->initializer->hir(instructions, state);
1718
1719 /* Calculate the constant value if this is a const
1720 * declaration.
1721 */
1722 if (this->type->qualifier.constant) {
1723 ir_constant *constant_value = rhs->constant_expression_value();
1724 if (!constant_value) {
1725 _mesa_glsl_error(& initializer_loc, state,
1726 "initializer of const variable `%s' must be a "
1727 "constant expression",
1728 decl->identifier);
1729 } else {
1730 rhs = constant_value;
1731 var->constant_value = constant_value;
1732 }
1733 }
1734
1735 if (rhs && !rhs->type->is_error()) {
1736 bool temp = var->read_only;
1737 if (this->type->qualifier.constant)
1738 var->read_only = false;
1739 result = do_assignment(instructions, state, lhs, rhs,
1740 this->get_location());
1741 var->read_only = temp;
1742 }
1743 }
1744
1745 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
1746 *
1747 * "It is an error to write to a const variable outside of
1748 * its declaration, so they must be initialized when
1749 * declared."
1750 */
1751 if (this->type->qualifier.constant && decl->initializer == NULL) {
1752 _mesa_glsl_error(& loc, state,
1753 "const declaration of `%s' must be initialized");
1754 }
1755
1756 /* Add the vairable to the symbol table after processing the initializer.
1757 * This differs from most C-like languages, but it follows the GLSL
1758 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
1759 * spec:
1760 *
1761 * "Within a declaration, the scope of a name starts immediately
1762 * after the initializer if present or immediately after the name
1763 * being declared if not."
1764 */
1765 const bool added_variable =
1766 state->symbols->add_variable(decl->identifier, var);
1767 assert(added_variable);
1768 }
1769
1770
1771 /* Generally, variable declarations do not have r-values. However,
1772 * one is used for the declaration in
1773 *
1774 * while (bool b = some_condition()) {
1775 * ...
1776 * }
1777 *
1778 * so we return the rvalue from the last seen declaration here.
1779 */
1780 return result;
1781 }
1782
1783
1784 ir_rvalue *
1785 ast_parameter_declarator::hir(exec_list *instructions,
1786 struct _mesa_glsl_parse_state *state)
1787 {
1788 const struct glsl_type *type;
1789 const char *name = NULL;
1790 YYLTYPE loc = this->get_location();
1791
1792 type = this->type->specifier->glsl_type(& name, state);
1793
1794 if (type == NULL) {
1795 if (name != NULL) {
1796 _mesa_glsl_error(& loc, state,
1797 "invalid type `%s' in declaration of `%s'",
1798 name, this->identifier);
1799 } else {
1800 _mesa_glsl_error(& loc, state,
1801 "invalid type in declaration of `%s'",
1802 this->identifier);
1803 }
1804
1805 type = glsl_type::error_type;
1806 }
1807
1808 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
1809 *
1810 * "Functions that accept no input arguments need not use void in the
1811 * argument list because prototypes (or definitions) are required and
1812 * therefore there is no ambiguity when an empty argument list "( )" is
1813 * declared. The idiom "(void)" as a parameter list is provided for
1814 * convenience."
1815 *
1816 * Placing this check here prevents a void parameter being set up
1817 * for a function, which avoids tripping up checks for main taking
1818 * parameters and lookups of an unnamed symbol.
1819 */
1820 if (type->is_void()) {
1821 if (this->identifier != NULL)
1822 _mesa_glsl_error(& loc, state,
1823 "named parameter cannot have type `void'");
1824
1825 is_void = true;
1826 return NULL;
1827 }
1828
1829 if (formal_parameter && (this->identifier == NULL)) {
1830 _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
1831 return NULL;
1832 }
1833
1834 is_void = false;
1835 ir_variable *var = new ir_variable(type, this->identifier);
1836
1837 /* FINISHME: Handle array declarations. Note that this requires
1838 * FINISHME: complete handling of constant expressions.
1839 */
1840
1841 /* Apply any specified qualifiers to the parameter declaration. Note that
1842 * for function parameters the default mode is 'in'.
1843 */
1844 apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
1845 if (var->mode == ir_var_auto)
1846 var->mode = ir_var_in;
1847
1848 instructions->push_tail(var);
1849
1850 /* Parameter declarations do not have r-values.
1851 */
1852 return NULL;
1853 }
1854
1855
1856 void
1857 ast_parameter_declarator::parameters_to_hir(struct simple_node *ast_parameters,
1858 bool formal,
1859 exec_list *ir_parameters,
1860 _mesa_glsl_parse_state *state)
1861 {
1862 struct simple_node *ptr;
1863 ast_parameter_declarator *void_param = NULL;
1864 unsigned count = 0;
1865
1866 foreach (ptr, ast_parameters) {
1867 ast_parameter_declarator *param = (ast_parameter_declarator *)ptr;
1868 param->formal_parameter = formal;
1869 param->hir(ir_parameters, state);
1870
1871 if (param->is_void)
1872 void_param = param;
1873
1874 count++;
1875 }
1876
1877 if ((void_param != NULL) && (count > 1)) {
1878 YYLTYPE loc = void_param->get_location();
1879
1880 _mesa_glsl_error(& loc, state,
1881 "`void' parameter must be only parameter");
1882 }
1883 }
1884
1885
1886 ir_rvalue *
1887 ast_function::hir(exec_list *instructions,
1888 struct _mesa_glsl_parse_state *state)
1889 {
1890 ir_function *f = NULL;
1891 ir_function_signature *sig = NULL;
1892 exec_list hir_parameters;
1893
1894
1895 /* Convert the list of function parameters to HIR now so that they can be
1896 * used below to compare this function's signature with previously seen
1897 * signatures for functions with the same name.
1898 */
1899 ast_parameter_declarator::parameters_to_hir(& this->parameters,
1900 is_definition,
1901 & hir_parameters, state);
1902
1903 const char *return_type_name;
1904 const glsl_type *return_type =
1905 this->return_type->specifier->glsl_type(& return_type_name, state);
1906
1907 assert(return_type != NULL);
1908
1909 /* Verify that this function's signature either doesn't match a previously
1910 * seen signature for a function with the same name, or, if a match is found,
1911 * that the previously seen signature does not have an associated definition.
1912 */
1913 const char *const name = identifier;
1914 f = state->symbols->get_function(name);
1915 if (f != NULL) {
1916 ir_function_signature *sig = f->exact_matching_signature(&hir_parameters);
1917 if (sig != NULL) {
1918 const char *badvar = sig->qualifiers_match(&hir_parameters);
1919 if (badvar != NULL) {
1920 YYLTYPE loc = this->get_location();
1921
1922 _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
1923 "qualifiers don't match prototype", name, badvar);
1924 }
1925
1926 if (sig->return_type != return_type) {
1927 YYLTYPE loc = this->get_location();
1928
1929 _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
1930 "match prototype", name);
1931 }
1932
1933 if (is_definition && sig->is_defined) {
1934 YYLTYPE loc = this->get_location();
1935
1936 _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
1937 sig = NULL;
1938 }
1939 }
1940 } else if (state->symbols->name_declared_this_scope(name)) {
1941 /* This function name shadows a non-function use of the same name.
1942 */
1943 YYLTYPE loc = this->get_location();
1944
1945 _mesa_glsl_error(& loc, state, "function name `%s' conflicts with "
1946 "non-function", name);
1947 sig = NULL;
1948 } else {
1949 f = new ir_function(name);
1950 state->symbols->add_function(f->name, f);
1951
1952 /* Emit the new function header */
1953 instructions->push_tail(f);
1954 }
1955
1956 /* Verify the return type of main() */
1957 if (strcmp(name, "main") == 0) {
1958 if (! return_type->is_void()) {
1959 YYLTYPE loc = this->get_location();
1960
1961 _mesa_glsl_error(& loc, state, "main() must return void");
1962 }
1963
1964 if (!hir_parameters.is_empty()) {
1965 YYLTYPE loc = this->get_location();
1966
1967 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
1968 }
1969 }
1970
1971 /* Finish storing the information about this new function in its signature.
1972 */
1973 if (sig == NULL) {
1974 sig = new ir_function_signature(return_type);
1975 f->add_signature(sig);
1976 }
1977
1978 sig->replace_parameters(&hir_parameters);
1979 signature = sig;
1980
1981 /* Function declarations (prototypes) do not have r-values.
1982 */
1983 return NULL;
1984 }
1985
1986
1987 ir_rvalue *
1988 ast_function_definition::hir(exec_list *instructions,
1989 struct _mesa_glsl_parse_state *state)
1990 {
1991 prototype->is_definition = true;
1992 prototype->hir(instructions, state);
1993
1994 ir_function_signature *signature = prototype->signature;
1995
1996 assert(state->current_function == NULL);
1997 state->current_function = signature;
1998
1999 /* Duplicate parameters declared in the prototype as concrete variables.
2000 * Add these to the symbol table.
2001 */
2002 state->symbols->push_scope();
2003 foreach_iter(exec_list_iterator, iter, signature->parameters) {
2004 ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
2005
2006 assert(var != NULL);
2007
2008 /* The only way a parameter would "exist" is if two parameters have
2009 * the same name.
2010 */
2011 if (state->symbols->name_declared_this_scope(var->name)) {
2012 YYLTYPE loc = this->get_location();
2013
2014 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
2015 } else {
2016 state->symbols->add_variable(var->name, var);
2017 }
2018 }
2019
2020 /* Convert the body of the function to HIR. */
2021 this->body->hir(&signature->body, state);
2022 signature->is_defined = true;
2023
2024 state->symbols->pop_scope();
2025
2026 assert(state->current_function == signature);
2027 state->current_function = NULL;
2028
2029 /* Function definitions do not have r-values.
2030 */
2031 return NULL;
2032 }
2033
2034
2035 ir_rvalue *
2036 ast_jump_statement::hir(exec_list *instructions,
2037 struct _mesa_glsl_parse_state *state)
2038 {
2039
2040 switch (mode) {
2041 case ast_return: {
2042 ir_return *inst;
2043 assert(state->current_function);
2044
2045 if (opt_return_value) {
2046 if (state->current_function->return_type->base_type ==
2047 GLSL_TYPE_VOID) {
2048 YYLTYPE loc = this->get_location();
2049
2050 _mesa_glsl_error(& loc, state,
2051 "`return` with a value, in function `%s' "
2052 "returning void",
2053 state->current_function->function_name());
2054 }
2055
2056 ir_expression *const ret = (ir_expression *)
2057 opt_return_value->hir(instructions, state);
2058 assert(ret != NULL);
2059
2060 /* FINISHME: Make sure the type of the return value matches the return
2061 * FINISHME: type of the enclosing function.
2062 */
2063
2064 inst = new ir_return(ret);
2065 } else {
2066 if (state->current_function->return_type->base_type !=
2067 GLSL_TYPE_VOID) {
2068 YYLTYPE loc = this->get_location();
2069
2070 _mesa_glsl_error(& loc, state,
2071 "`return' with no value, in function %s returning "
2072 "non-void",
2073 state->current_function->function_name());
2074 }
2075 inst = new ir_return;
2076 }
2077
2078 instructions->push_tail(inst);
2079 break;
2080 }
2081
2082 case ast_discard:
2083 /* FINISHME: discard support */
2084 if (state->target != fragment_shader) {
2085 YYLTYPE loc = this->get_location();
2086
2087 _mesa_glsl_error(& loc, state,
2088 "`discard' may only appear in a fragment shader");
2089 }
2090 break;
2091
2092 case ast_break:
2093 case ast_continue:
2094 /* FINISHME: Handle switch-statements. They cannot contain 'continue',
2095 * FINISHME: and they use a different IR instruction for 'break'.
2096 */
2097 /* FINISHME: Correctly handle the nesting. If a switch-statement is
2098 * FINISHME: inside a loop, a 'continue' is valid and will bind to the
2099 * FINISHME: loop.
2100 */
2101 if (state->loop_or_switch_nesting == NULL) {
2102 YYLTYPE loc = this->get_location();
2103
2104 _mesa_glsl_error(& loc, state,
2105 "`%s' may only appear in a loop",
2106 (mode == ast_break) ? "break" : "continue");
2107 } else {
2108 ir_loop *const loop = state->loop_or_switch_nesting->as_loop();
2109
2110 if (loop != NULL) {
2111 ir_loop_jump *const jump =
2112 new ir_loop_jump(loop,
2113 (mode == ast_break)
2114 ? ir_loop_jump::jump_break
2115 : ir_loop_jump::jump_continue);
2116 instructions->push_tail(jump);
2117 }
2118 }
2119
2120 break;
2121 }
2122
2123 /* Jump instructions do not have r-values.
2124 */
2125 return NULL;
2126 }
2127
2128
2129 ir_rvalue *
2130 ast_selection_statement::hir(exec_list *instructions,
2131 struct _mesa_glsl_parse_state *state)
2132 {
2133 ir_rvalue *const condition = this->condition->hir(instructions, state);
2134
2135 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
2136 *
2137 * "Any expression whose type evaluates to a Boolean can be used as the
2138 * conditional expression bool-expression. Vector types are not accepted
2139 * as the expression to if."
2140 *
2141 * The checks are separated so that higher quality diagnostics can be
2142 * generated for cases where both rules are violated.
2143 */
2144 if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
2145 YYLTYPE loc = this->condition->get_location();
2146
2147 _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
2148 "boolean");
2149 }
2150
2151 ir_if *const stmt = new ir_if(condition);
2152
2153 if (then_statement != NULL) {
2154 ast_node *node = (ast_node *) then_statement;
2155 do {
2156 node->hir(& stmt->then_instructions, state);
2157 node = (ast_node *) node->next;
2158 } while (node != then_statement);
2159 }
2160
2161 if (else_statement != NULL) {
2162 ast_node *node = (ast_node *) else_statement;
2163 do {
2164 node->hir(& stmt->else_instructions, state);
2165 node = (ast_node *) node->next;
2166 } while (node != else_statement);
2167 }
2168
2169 instructions->push_tail(stmt);
2170
2171 /* if-statements do not have r-values.
2172 */
2173 return NULL;
2174 }
2175
2176
2177 void
2178 ast_iteration_statement::condition_to_hir(ir_loop *stmt,
2179 struct _mesa_glsl_parse_state *state)
2180 {
2181 if (condition != NULL) {
2182 ir_rvalue *const cond =
2183 condition->hir(& stmt->body_instructions, state);
2184
2185 if ((cond == NULL)
2186 || !cond->type->is_boolean() || !cond->type->is_scalar()) {
2187 YYLTYPE loc = condition->get_location();
2188
2189 _mesa_glsl_error(& loc, state,
2190 "loop condition must be scalar boolean");
2191 } else {
2192 /* As the first code in the loop body, generate a block that looks
2193 * like 'if (!condition) break;' as the loop termination condition.
2194 */
2195 ir_rvalue *const not_cond =
2196 new ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond,
2197 NULL);
2198
2199 ir_if *const if_stmt = new ir_if(not_cond);
2200
2201 ir_jump *const break_stmt =
2202 new ir_loop_jump(stmt, ir_loop_jump::jump_break);
2203
2204 if_stmt->then_instructions.push_tail(break_stmt);
2205 stmt->body_instructions.push_tail(if_stmt);
2206 }
2207 }
2208 }
2209
2210
2211 ir_rvalue *
2212 ast_iteration_statement::hir(exec_list *instructions,
2213 struct _mesa_glsl_parse_state *state)
2214 {
2215 /* For-loops and while-loops start a new scope, but do-while loops do not.
2216 */
2217 if (mode != ast_do_while)
2218 state->symbols->push_scope();
2219
2220 if (init_statement != NULL)
2221 init_statement->hir(instructions, state);
2222
2223 ir_loop *const stmt = new ir_loop();
2224 instructions->push_tail(stmt);
2225
2226 /* Track the current loop and / or switch-statement nesting.
2227 */
2228 ir_instruction *const nesting = state->loop_or_switch_nesting;
2229 state->loop_or_switch_nesting = stmt;
2230
2231 if (mode != ast_do_while)
2232 condition_to_hir(stmt, state);
2233
2234 if (body != NULL) {
2235 ast_node *node = (ast_node *) body;
2236 do {
2237 node->hir(& stmt->body_instructions, state);
2238 node = (ast_node *) node->next;
2239 } while (node != body);
2240 }
2241
2242 if (rest_expression != NULL)
2243 rest_expression->hir(& stmt->body_instructions, state);
2244
2245 if (mode == ast_do_while)
2246 condition_to_hir(stmt, state);
2247
2248 if (mode != ast_do_while)
2249 state->symbols->pop_scope();
2250
2251 /* Restore previous nesting before returning.
2252 */
2253 state->loop_or_switch_nesting = nesting;
2254
2255 /* Loops do not have r-values.
2256 */
2257 return NULL;
2258 }