glsl: Implement ast-to-hir for binary shifts in GLSL 1.30
[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->symbols->language_version = state->language_version;
66
67 state->current_function = NULL;
68
69 /* Section 4.2 of the GLSL 1.20 specification states:
70 * "The built-in functions are scoped in a scope outside the global scope
71 * users declare global variables in. That is, a shader's global scope,
72 * available for user-defined functions and global variables, is nested
73 * inside the scope containing the built-in functions."
74 *
75 * Since built-in functions like ftransform() access built-in variables,
76 * it follows that those must be in the outer scope as well.
77 *
78 * We push scope here to create this nesting effect...but don't pop.
79 * This way, a shader's globals are still in the symbol table for use
80 * by the linker.
81 */
82 state->symbols->push_scope();
83
84 foreach_list_typed (ast_node, ast, link, & state->translation_unit)
85 ast->hir(instructions, state);
86 }
87
88
89 /**
90 * If a conversion is available, convert one operand to a different type
91 *
92 * The \c from \c ir_rvalue is converted "in place".
93 *
94 * \param to Type that the operand it to be converted to
95 * \param from Operand that is being converted
96 * \param state GLSL compiler state
97 *
98 * \return
99 * If a conversion is possible (or unnecessary), \c true is returned.
100 * Otherwise \c false is returned.
101 */
102 bool
103 apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
104 struct _mesa_glsl_parse_state *state)
105 {
106 void *ctx = state;
107 if (to->base_type == from->type->base_type)
108 return true;
109
110 /* This conversion was added in GLSL 1.20. If the compilation mode is
111 * GLSL 1.10, the conversion is skipped.
112 */
113 if (state->language_version < 120)
114 return false;
115
116 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
117 *
118 * "There are no implicit array or structure conversions. For
119 * example, an array of int cannot be implicitly converted to an
120 * array of float. There are no implicit conversions between
121 * signed and unsigned integers."
122 */
123 /* FINISHME: The above comment is partially a lie. There is int/uint
124 * FINISHME: conversion for immediate constants.
125 */
126 if (!to->is_float() || !from->type->is_numeric())
127 return false;
128
129 /* Convert to a floating point type with the same number of components
130 * as the original type - i.e. int to float, not int to vec4.
131 */
132 to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements,
133 from->type->matrix_columns);
134
135 switch (from->type->base_type) {
136 case GLSL_TYPE_INT:
137 from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL);
138 break;
139 case GLSL_TYPE_UINT:
140 from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL);
141 break;
142 case GLSL_TYPE_BOOL:
143 from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL);
144 break;
145 default:
146 assert(0);
147 }
148
149 return true;
150 }
151
152
153 static const struct glsl_type *
154 arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
155 bool multiply,
156 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
157 {
158 const glsl_type *type_a = value_a->type;
159 const glsl_type *type_b = value_b->type;
160
161 /* From GLSL 1.50 spec, page 56:
162 *
163 * "The arithmetic binary operators add (+), subtract (-),
164 * multiply (*), and divide (/) operate on integer and
165 * floating-point scalars, vectors, and matrices."
166 */
167 if (!type_a->is_numeric() || !type_b->is_numeric()) {
168 _mesa_glsl_error(loc, state,
169 "Operands to arithmetic operators must be numeric");
170 return glsl_type::error_type;
171 }
172
173
174 /* "If one operand is floating-point based and the other is
175 * not, then the conversions from Section 4.1.10 "Implicit
176 * Conversions" are applied to the non-floating-point-based operand."
177 */
178 if (!apply_implicit_conversion(type_a, value_b, state)
179 && !apply_implicit_conversion(type_b, value_a, state)) {
180 _mesa_glsl_error(loc, state,
181 "Could not implicitly convert operands to "
182 "arithmetic operator");
183 return glsl_type::error_type;
184 }
185 type_a = value_a->type;
186 type_b = value_b->type;
187
188 /* "If the operands are integer types, they must both be signed or
189 * both be unsigned."
190 *
191 * From this rule and the preceeding conversion it can be inferred that
192 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
193 * The is_numeric check above already filtered out the case where either
194 * type is not one of these, so now the base types need only be tested for
195 * equality.
196 */
197 if (type_a->base_type != type_b->base_type) {
198 _mesa_glsl_error(loc, state,
199 "base type mismatch for arithmetic operator");
200 return glsl_type::error_type;
201 }
202
203 /* "All arithmetic binary operators result in the same fundamental type
204 * (signed integer, unsigned integer, or floating-point) as the
205 * operands they operate on, after operand type conversion. After
206 * conversion, the following cases are valid
207 *
208 * * The two operands are scalars. In this case the operation is
209 * applied, resulting in a scalar."
210 */
211 if (type_a->is_scalar() && type_b->is_scalar())
212 return type_a;
213
214 /* "* One operand is a scalar, and the other is a vector or matrix.
215 * In this case, the scalar operation is applied independently to each
216 * component of the vector or matrix, resulting in the same size
217 * vector or matrix."
218 */
219 if (type_a->is_scalar()) {
220 if (!type_b->is_scalar())
221 return type_b;
222 } else if (type_b->is_scalar()) {
223 return type_a;
224 }
225
226 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
227 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
228 * handled.
229 */
230 assert(!type_a->is_scalar());
231 assert(!type_b->is_scalar());
232
233 /* "* The two operands are vectors of the same size. In this case, the
234 * operation is done component-wise resulting in the same size
235 * vector."
236 */
237 if (type_a->is_vector() && type_b->is_vector()) {
238 if (type_a == type_b) {
239 return type_a;
240 } else {
241 _mesa_glsl_error(loc, state,
242 "vector size mismatch for arithmetic operator");
243 return glsl_type::error_type;
244 }
245 }
246
247 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
248 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
249 * <vector, vector> have been handled. At least one of the operands must
250 * be matrix. Further, since there are no integer matrix types, the base
251 * type of both operands must be float.
252 */
253 assert(type_a->is_matrix() || type_b->is_matrix());
254 assert(type_a->base_type == GLSL_TYPE_FLOAT);
255 assert(type_b->base_type == GLSL_TYPE_FLOAT);
256
257 /* "* The operator is add (+), subtract (-), or divide (/), and the
258 * operands are matrices with the same number of rows and the same
259 * number of columns. In this case, the operation is done component-
260 * wise resulting in the same size matrix."
261 * * The operator is multiply (*), where both operands are matrices or
262 * one operand is a vector and the other a matrix. A right vector
263 * operand is treated as a column vector and a left vector operand as a
264 * row vector. In all these cases, it is required that the number of
265 * columns of the left operand is equal to the number of rows of the
266 * right operand. Then, the multiply (*) operation does a linear
267 * algebraic multiply, yielding an object that has the same number of
268 * rows as the left operand and the same number of columns as the right
269 * operand. Section 5.10 "Vector and Matrix Operations" explains in
270 * more detail how vectors and matrices are operated on."
271 */
272 if (! multiply) {
273 if (type_a == type_b)
274 return type_a;
275 } else {
276 if (type_a->is_matrix() && type_b->is_matrix()) {
277 /* Matrix multiply. The columns of A must match the rows of B. Given
278 * the other previously tested constraints, this means the vector type
279 * of a row from A must be the same as the vector type of a column from
280 * B.
281 */
282 if (type_a->row_type() == type_b->column_type()) {
283 /* The resulting matrix has the number of columns of matrix B and
284 * the number of rows of matrix A. We get the row count of A by
285 * looking at the size of a vector that makes up a column. The
286 * transpose (size of a row) is done for B.
287 */
288 const glsl_type *const type =
289 glsl_type::get_instance(type_a->base_type,
290 type_a->column_type()->vector_elements,
291 type_b->row_type()->vector_elements);
292 assert(type != glsl_type::error_type);
293
294 return type;
295 }
296 } else if (type_a->is_matrix()) {
297 /* A is a matrix and B is a column vector. Columns of A must match
298 * rows of B. Given the other previously tested constraints, this
299 * means the vector type of a row from A must be the same as the
300 * vector the type of B.
301 */
302 if (type_a->row_type() == type_b) {
303 /* The resulting vector has a number of elements equal to
304 * the number of rows of matrix A. */
305 const glsl_type *const type =
306 glsl_type::get_instance(type_a->base_type,
307 type_a->column_type()->vector_elements,
308 1);
309 assert(type != glsl_type::error_type);
310
311 return type;
312 }
313 } else {
314 assert(type_b->is_matrix());
315
316 /* A is a row vector and B is a matrix. Columns of A must match rows
317 * of B. Given the other previously tested constraints, this means
318 * the type of A must be the same as the vector type of a column from
319 * B.
320 */
321 if (type_a == type_b->column_type()) {
322 /* The resulting vector has a number of elements equal to
323 * the number of columns of matrix B. */
324 const glsl_type *const type =
325 glsl_type::get_instance(type_a->base_type,
326 type_b->row_type()->vector_elements,
327 1);
328 assert(type != glsl_type::error_type);
329
330 return type;
331 }
332 }
333
334 _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
335 return glsl_type::error_type;
336 }
337
338
339 /* "All other cases are illegal."
340 */
341 _mesa_glsl_error(loc, state, "type mismatch");
342 return glsl_type::error_type;
343 }
344
345
346 static const struct glsl_type *
347 unary_arithmetic_result_type(const struct glsl_type *type,
348 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
349 {
350 /* From GLSL 1.50 spec, page 57:
351 *
352 * "The arithmetic unary operators negate (-), post- and pre-increment
353 * and decrement (-- and ++) operate on integer or floating-point
354 * values (including vectors and matrices). All unary operators work
355 * component-wise on their operands. These result with the same type
356 * they operated on."
357 */
358 if (!type->is_numeric()) {
359 _mesa_glsl_error(loc, state,
360 "Operands to arithmetic operators must be numeric");
361 return glsl_type::error_type;
362 }
363
364 return type;
365 }
366
367
368 static const struct glsl_type *
369 modulus_result_type(const struct glsl_type *type_a,
370 const struct glsl_type *type_b,
371 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
372 {
373 /* From GLSL 1.50 spec, page 56:
374 * "The operator modulus (%) operates on signed or unsigned integers or
375 * integer vectors. The operand types must both be signed or both be
376 * unsigned."
377 */
378 if (!type_a->is_integer() || !type_b->is_integer()
379 || (type_a->base_type != type_b->base_type)) {
380 _mesa_glsl_error(loc, state, "type mismatch");
381 return glsl_type::error_type;
382 }
383
384 /* "The operands cannot be vectors of differing size. If one operand is
385 * a scalar and the other vector, then the scalar is applied component-
386 * wise to the vector, resulting in the same type as the vector. If both
387 * are vectors of the same size, the result is computed component-wise."
388 */
389 if (type_a->is_vector()) {
390 if (!type_b->is_vector()
391 || (type_a->vector_elements == type_b->vector_elements))
392 return type_a;
393 } else
394 return type_b;
395
396 /* "The operator modulus (%) is not defined for any other data types
397 * (non-integer types)."
398 */
399 _mesa_glsl_error(loc, state, "type mismatch");
400 return glsl_type::error_type;
401 }
402
403
404 static const struct glsl_type *
405 relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
406 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
407 {
408 const glsl_type *type_a = value_a->type;
409 const glsl_type *type_b = value_b->type;
410
411 /* From GLSL 1.50 spec, page 56:
412 * "The relational operators greater than (>), less than (<), greater
413 * than or equal (>=), and less than or equal (<=) operate only on
414 * scalar integer and scalar floating-point expressions."
415 */
416 if (!type_a->is_numeric()
417 || !type_b->is_numeric()
418 || !type_a->is_scalar()
419 || !type_b->is_scalar()) {
420 _mesa_glsl_error(loc, state,
421 "Operands to relational operators must be scalar and "
422 "numeric");
423 return glsl_type::error_type;
424 }
425
426 /* "Either the operands' types must match, or the conversions from
427 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
428 * operand, after which the types must match."
429 */
430 if (!apply_implicit_conversion(type_a, value_b, state)
431 && !apply_implicit_conversion(type_b, value_a, state)) {
432 _mesa_glsl_error(loc, state,
433 "Could not implicitly convert operands to "
434 "relational operator");
435 return glsl_type::error_type;
436 }
437 type_a = value_a->type;
438 type_b = value_b->type;
439
440 if (type_a->base_type != type_b->base_type) {
441 _mesa_glsl_error(loc, state, "base type mismatch");
442 return glsl_type::error_type;
443 }
444
445 /* "The result is scalar Boolean."
446 */
447 return glsl_type::bool_type;
448 }
449
450
451 /**
452 * Validates that a value can be assigned to a location with a specified type
453 *
454 * Validates that \c rhs can be assigned to some location. If the types are
455 * not an exact match but an automatic conversion is possible, \c rhs will be
456 * converted.
457 *
458 * \return
459 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
460 * Otherwise the actual RHS to be assigned will be returned. This may be
461 * \c rhs, or it may be \c rhs after some type conversion.
462 *
463 * \note
464 * In addition to being used for assignments, this function is used to
465 * type-check return values.
466 */
467 ir_rvalue *
468 validate_assignment(struct _mesa_glsl_parse_state *state,
469 const glsl_type *lhs_type, ir_rvalue *rhs)
470 {
471 const glsl_type *rhs_type = rhs->type;
472
473 /* If there is already some error in the RHS, just return it. Anything
474 * else will lead to an avalanche of error message back to the user.
475 */
476 if (rhs_type->is_error())
477 return rhs;
478
479 /* If the types are identical, the assignment can trivially proceed.
480 */
481 if (rhs_type == lhs_type)
482 return rhs;
483
484 /* If the array element types are the same and the size of the LHS is zero,
485 * the assignment is okay.
486 *
487 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
488 * is handled by ir_dereference::is_lvalue.
489 */
490 if (lhs_type->is_array() && rhs->type->is_array()
491 && (lhs_type->element_type() == rhs->type->element_type())
492 && (lhs_type->array_size() == 0)) {
493 return rhs;
494 }
495
496 /* Check for implicit conversion in GLSL 1.20 */
497 if (apply_implicit_conversion(lhs_type, rhs, state)) {
498 rhs_type = rhs->type;
499 if (rhs_type == lhs_type)
500 return rhs;
501 }
502
503 return NULL;
504 }
505
506 ir_rvalue *
507 do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
508 ir_rvalue *lhs, ir_rvalue *rhs,
509 YYLTYPE lhs_loc)
510 {
511 void *ctx = state;
512 bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
513
514 if (!error_emitted) {
515 if (!lhs->is_lvalue()) {
516 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
517 error_emitted = true;
518 }
519
520 if (state->es_shader && lhs->type->is_array()) {
521 _mesa_glsl_error(&lhs_loc, state, "whole array assignment is not "
522 "allowed in GLSL ES 1.00.");
523 error_emitted = true;
524 }
525 }
526
527 ir_rvalue *new_rhs = validate_assignment(state, lhs->type, rhs);
528 if (new_rhs == NULL) {
529 _mesa_glsl_error(& lhs_loc, state, "type mismatch");
530 } else {
531 rhs = new_rhs;
532
533 /* If the LHS array was not declared with a size, it takes it size from
534 * the RHS. If the LHS is an l-value and a whole array, it must be a
535 * dereference of a variable. Any other case would require that the LHS
536 * is either not an l-value or not a whole array.
537 */
538 if (lhs->type->array_size() == 0) {
539 ir_dereference *const d = lhs->as_dereference();
540
541 assert(d != NULL);
542
543 ir_variable *const var = d->variable_referenced();
544
545 assert(var != NULL);
546
547 if (var->max_array_access >= unsigned(rhs->type->array_size())) {
548 /* FINISHME: This should actually log the location of the RHS. */
549 _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
550 "previous access",
551 var->max_array_access);
552 }
553
554 var->type = glsl_type::get_array_instance(lhs->type->element_type(),
555 rhs->type->array_size());
556 d->type = var->type;
557 }
558 }
559
560 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
561 * but not post_inc) need the converted assigned value as an rvalue
562 * to handle things like:
563 *
564 * i = j += 1;
565 *
566 * So we always just store the computed value being assigned to a
567 * temporary and return a deref of that temporary. If the rvalue
568 * ends up not being used, the temp will get copy-propagated out.
569 */
570 ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp",
571 ir_var_temporary);
572 ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var);
573 instructions->push_tail(var);
574 instructions->push_tail(new(ctx) ir_assignment(deref_var,
575 rhs,
576 NULL));
577 deref_var = new(ctx) ir_dereference_variable(var);
578
579 if (!error_emitted)
580 instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var, NULL));
581
582 return new(ctx) ir_dereference_variable(var);
583 }
584
585 static ir_rvalue *
586 get_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
587 {
588 void *ctx = talloc_parent(lvalue);
589 ir_variable *var;
590
591 var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
592 ir_var_temporary);
593 instructions->push_tail(var);
594 var->mode = ir_var_auto;
595
596 instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
597 lvalue, NULL));
598
599 /* Once we've created this temporary, mark it read only so it's no
600 * longer considered an lvalue.
601 */
602 var->read_only = true;
603
604 return new(ctx) ir_dereference_variable(var);
605 }
606
607
608 ir_rvalue *
609 ast_node::hir(exec_list *instructions,
610 struct _mesa_glsl_parse_state *state)
611 {
612 (void) instructions;
613 (void) state;
614
615 return NULL;
616 }
617
618
619 ir_rvalue *
620 ast_expression::hir(exec_list *instructions,
621 struct _mesa_glsl_parse_state *state)
622 {
623 void *ctx = state;
624 static const int operations[AST_NUM_OPERATORS] = {
625 -1, /* ast_assign doesn't convert to ir_expression. */
626 -1, /* ast_plus doesn't convert to ir_expression. */
627 ir_unop_neg,
628 ir_binop_add,
629 ir_binop_sub,
630 ir_binop_mul,
631 ir_binop_div,
632 ir_binop_mod,
633 ir_binop_lshift,
634 ir_binop_rshift,
635 ir_binop_less,
636 ir_binop_greater,
637 ir_binop_lequal,
638 ir_binop_gequal,
639 ir_binop_all_equal,
640 ir_binop_any_nequal,
641 ir_binop_bit_and,
642 ir_binop_bit_xor,
643 ir_binop_bit_or,
644 ir_unop_bit_not,
645 ir_binop_logic_and,
646 ir_binop_logic_xor,
647 ir_binop_logic_or,
648 ir_unop_logic_not,
649
650 /* Note: The following block of expression types actually convert
651 * to multiple IR instructions.
652 */
653 ir_binop_mul, /* ast_mul_assign */
654 ir_binop_div, /* ast_div_assign */
655 ir_binop_mod, /* ast_mod_assign */
656 ir_binop_add, /* ast_add_assign */
657 ir_binop_sub, /* ast_sub_assign */
658 ir_binop_lshift, /* ast_ls_assign */
659 ir_binop_rshift, /* ast_rs_assign */
660 ir_binop_bit_and, /* ast_and_assign */
661 ir_binop_bit_xor, /* ast_xor_assign */
662 ir_binop_bit_or, /* ast_or_assign */
663
664 -1, /* ast_conditional doesn't convert to ir_expression. */
665 ir_binop_add, /* ast_pre_inc. */
666 ir_binop_sub, /* ast_pre_dec. */
667 ir_binop_add, /* ast_post_inc. */
668 ir_binop_sub, /* ast_post_dec. */
669 -1, /* ast_field_selection doesn't conv to ir_expression. */
670 -1, /* ast_array_index doesn't convert to ir_expression. */
671 -1, /* ast_function_call doesn't conv to ir_expression. */
672 -1, /* ast_identifier doesn't convert to ir_expression. */
673 -1, /* ast_int_constant doesn't convert to ir_expression. */
674 -1, /* ast_uint_constant doesn't conv to ir_expression. */
675 -1, /* ast_float_constant doesn't conv to ir_expression. */
676 -1, /* ast_bool_constant doesn't conv to ir_expression. */
677 -1, /* ast_sequence doesn't convert to ir_expression. */
678 };
679 ir_rvalue *result = NULL;
680 ir_rvalue *op[3];
681 const struct glsl_type *type = glsl_type::error_type;
682 bool error_emitted = false;
683 YYLTYPE loc;
684
685 loc = this->get_location();
686
687 switch (this->oper) {
688 case ast_assign: {
689 op[0] = this->subexpressions[0]->hir(instructions, state);
690 op[1] = this->subexpressions[1]->hir(instructions, state);
691
692 result = do_assignment(instructions, state, op[0], op[1],
693 this->subexpressions[0]->get_location());
694 error_emitted = result->type->is_error();
695 type = result->type;
696 break;
697 }
698
699 case ast_plus:
700 op[0] = this->subexpressions[0]->hir(instructions, state);
701
702 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
703
704 error_emitted = type->is_error();
705
706 result = op[0];
707 break;
708
709 case ast_neg:
710 op[0] = this->subexpressions[0]->hir(instructions, state);
711
712 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
713
714 error_emitted = type->is_error();
715
716 result = new(ctx) ir_expression(operations[this->oper], type,
717 op[0], NULL);
718 break;
719
720 case ast_add:
721 case ast_sub:
722 case ast_mul:
723 case ast_div:
724 op[0] = this->subexpressions[0]->hir(instructions, state);
725 op[1] = this->subexpressions[1]->hir(instructions, state);
726
727 type = arithmetic_result_type(op[0], op[1],
728 (this->oper == ast_mul),
729 state, & loc);
730 error_emitted = type->is_error();
731
732 result = new(ctx) ir_expression(operations[this->oper], type,
733 op[0], op[1]);
734 break;
735
736 case ast_mod:
737 op[0] = this->subexpressions[0]->hir(instructions, state);
738 op[1] = this->subexpressions[1]->hir(instructions, state);
739
740 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
741
742 assert(operations[this->oper] == ir_binop_mod);
743
744 result = new(ctx) ir_expression(operations[this->oper], type,
745 op[0], op[1]);
746 error_emitted = type->is_error();
747 break;
748
749 case ast_lshift:
750 case ast_rshift:
751 if (state->language_version < 130) {
752 _mesa_glsl_error(&loc, state, "operator %s requires GLSL 1.30",
753 operator_string(this->oper));
754 error_emitted = true;
755 break;
756 }
757
758 /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
759 *
760 * The shift operators (<<) and (>>). For both operators, the operands
761 * must be signed or unsigned integers or integer vectors. One operand
762 * can be signed while the other is unsigned. In all cases, the
763 * resulting type will be the same type as the left operand. If the
764 * first operand is a scalar, the second operand has to be a scalar as
765 * well. If the first operand is a vector, the second operand must be
766 * a scalar or a vector, [...]
767 */
768
769 op[0] = this->subexpressions[0]->hir(instructions, state);
770 op[1] = this->subexpressions[1]->hir(instructions, state);
771
772 if (!op[0]->type->is_integer()) {
773 _mesa_glsl_error(& loc, state,
774 "LHS of operator %s must be an integer or integer vector",
775 operator_string(this->oper));
776 error_emitted = true;
777 break;
778 }
779 if (!op[1]->type->is_integer()) {
780 _mesa_glsl_error(& loc, state,
781 "RHS of operator %s must be an integer or integer vector",
782 operator_string(this->oper));
783 error_emitted = true;
784 break;
785 }
786 if (op[0]->type->is_scalar() && !op[1]->type->is_scalar()) {
787 _mesa_glsl_error(& loc, state,
788 "If the first operand of %s is scalar, the second must be"
789 "scalar as well", operator_string(this->oper));
790 error_emitted = true;
791 break;
792 }
793 if (op[0]->type->is_vector() &&
794 op[1]->type->is_vector() &&
795 op[0]->type->components() != op[1]->type->components()) {
796
797 _mesa_glsl_error(& loc, state,
798 "Vector operands of %s must have same number of components",
799 operator_string(this->oper));
800 error_emitted = true;
801 break;
802 }
803
804 type = op[0]->type;
805 result = new(ctx) ir_expression(operations[this->oper], type,
806 op[0], op[1]);
807 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
808 break;
809
810 case ast_less:
811 case ast_greater:
812 case ast_lequal:
813 case ast_gequal:
814 op[0] = this->subexpressions[0]->hir(instructions, state);
815 op[1] = this->subexpressions[1]->hir(instructions, state);
816
817 type = relational_result_type(op[0], op[1], state, & loc);
818
819 /* The relational operators must either generate an error or result
820 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
821 */
822 assert(type->is_error()
823 || ((type->base_type == GLSL_TYPE_BOOL)
824 && type->is_scalar()));
825
826 result = new(ctx) ir_expression(operations[this->oper], type,
827 op[0], op[1]);
828 error_emitted = type->is_error();
829 break;
830
831 case ast_nequal:
832 case ast_equal:
833 op[0] = this->subexpressions[0]->hir(instructions, state);
834 op[1] = this->subexpressions[1]->hir(instructions, state);
835
836 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
837 *
838 * "The equality operators equal (==), and not equal (!=)
839 * operate on all types. They result in a scalar Boolean. If
840 * the operand types do not match, then there must be a
841 * conversion from Section 4.1.10 "Implicit Conversions"
842 * applied to one operand that can make them match, in which
843 * case this conversion is done."
844 */
845 if ((!apply_implicit_conversion(op[0]->type, op[1], state)
846 && !apply_implicit_conversion(op[1]->type, op[0], state))
847 || (op[0]->type != op[1]->type)) {
848 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
849 "type", (this->oper == ast_equal) ? "==" : "!=");
850 error_emitted = true;
851 } else if ((state->language_version <= 110)
852 && (op[0]->type->is_array() || op[1]->type->is_array())) {
853 _mesa_glsl_error(& loc, state, "array comparisons forbidden in "
854 "GLSL 1.10");
855 error_emitted = true;
856 }
857
858 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
859 op[0], op[1]);
860 type = glsl_type::bool_type;
861
862 assert(result->type == glsl_type::bool_type);
863 break;
864
865 case ast_bit_and:
866 case ast_bit_xor:
867 case ast_bit_or:
868 op[0] = this->subexpressions[0]->hir(instructions, state);
869 op[1] = this->subexpressions[1]->hir(instructions, state);
870
871 if (state->language_version < 130) {
872 _mesa_glsl_error(&loc, state, "bit-wise operations require GLSL 1.30");
873 error_emitted = true;
874 }
875
876 if (!op[0]->type->is_integer()) {
877 _mesa_glsl_error(&loc, state, "LHS of `%s' must be an integer",
878 operator_string(this->oper));
879 error_emitted = true;
880 }
881
882 if (!op[1]->type->is_integer()) {
883 _mesa_glsl_error(&loc, state, "RHS of `%s' must be an integer",
884 operator_string(this->oper));
885 error_emitted = true;
886 }
887
888 if (op[0]->type->base_type != op[1]->type->base_type) {
889 _mesa_glsl_error(&loc, state, "operands of `%s' must have the same "
890 "base type", operator_string(this->oper));
891 error_emitted = true;
892 }
893
894 if (op[0]->type->is_vector() && op[1]->type->is_vector()
895 && op[0]->type->vector_elements != op[1]->type->vector_elements) {
896 _mesa_glsl_error(&loc, state, "operands of `%s' cannot be vectors of "
897 "different sizes", operator_string(this->oper));
898 error_emitted = true;
899 }
900
901 type = op[0]->type->is_scalar() ? op[1]->type : op[0]->type;
902 result = new(ctx) ir_expression(operations[this->oper], type,
903 op[0], op[1]);
904 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
905 break;
906
907 case ast_bit_not:
908 op[0] = this->subexpressions[0]->hir(instructions, state);
909
910 if (state->language_version < 130) {
911 _mesa_glsl_error(&loc, state, "bit-wise operations require GLSL 1.30");
912 error_emitted = true;
913 }
914
915 if (!op[0]->type->is_integer()) {
916 _mesa_glsl_error(&loc, state, "operand of `~' must be an integer");
917 error_emitted = true;
918 }
919
920 type = op[0]->type;
921 result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL);
922 break;
923
924 case ast_logic_and: {
925 op[0] = this->subexpressions[0]->hir(instructions, state);
926
927 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
928 YYLTYPE loc = this->subexpressions[0]->get_location();
929
930 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
931 operator_string(this->oper));
932 error_emitted = true;
933 }
934
935 ir_constant *op0_const = op[0]->constant_expression_value();
936 if (op0_const) {
937 if (op0_const->value.b[0]) {
938 op[1] = this->subexpressions[1]->hir(instructions, state);
939
940 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
941 YYLTYPE loc = this->subexpressions[1]->get_location();
942
943 _mesa_glsl_error(& loc, state,
944 "RHS of `%s' must be scalar boolean",
945 operator_string(this->oper));
946 error_emitted = true;
947 }
948 result = op[1];
949 } else {
950 result = op0_const;
951 }
952 type = glsl_type::bool_type;
953 } else {
954 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
955 "and_tmp",
956 ir_var_temporary);
957 instructions->push_tail(tmp);
958
959 ir_if *const stmt = new(ctx) ir_if(op[0]);
960 instructions->push_tail(stmt);
961
962 op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state);
963
964 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
965 YYLTYPE loc = this->subexpressions[1]->get_location();
966
967 _mesa_glsl_error(& loc, state,
968 "RHS of `%s' must be scalar boolean",
969 operator_string(this->oper));
970 error_emitted = true;
971 }
972
973 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
974 ir_assignment *const then_assign =
975 new(ctx) ir_assignment(then_deref, op[1], NULL);
976 stmt->then_instructions.push_tail(then_assign);
977
978 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
979 ir_assignment *const else_assign =
980 new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false), NULL);
981 stmt->else_instructions.push_tail(else_assign);
982
983 result = new(ctx) ir_dereference_variable(tmp);
984 type = tmp->type;
985 }
986 break;
987 }
988
989 case ast_logic_or: {
990 op[0] = this->subexpressions[0]->hir(instructions, state);
991
992 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
993 YYLTYPE loc = this->subexpressions[0]->get_location();
994
995 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
996 operator_string(this->oper));
997 error_emitted = true;
998 }
999
1000 ir_constant *op0_const = op[0]->constant_expression_value();
1001 if (op0_const) {
1002 if (op0_const->value.b[0]) {
1003 result = op0_const;
1004 } else {
1005 op[1] = this->subexpressions[1]->hir(instructions, state);
1006
1007 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
1008 YYLTYPE loc = this->subexpressions[1]->get_location();
1009
1010 _mesa_glsl_error(& loc, state,
1011 "RHS of `%s' must be scalar boolean",
1012 operator_string(this->oper));
1013 error_emitted = true;
1014 }
1015 result = op[1];
1016 }
1017 type = glsl_type::bool_type;
1018 } else {
1019 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
1020 "or_tmp",
1021 ir_var_temporary);
1022 instructions->push_tail(tmp);
1023
1024 ir_if *const stmt = new(ctx) ir_if(op[0]);
1025 instructions->push_tail(stmt);
1026
1027 op[1] = this->subexpressions[1]->hir(&stmt->else_instructions, state);
1028
1029 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
1030 YYLTYPE loc = this->subexpressions[1]->get_location();
1031
1032 _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean",
1033 operator_string(this->oper));
1034 error_emitted = true;
1035 }
1036
1037 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
1038 ir_assignment *const then_assign =
1039 new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true), NULL);
1040 stmt->then_instructions.push_tail(then_assign);
1041
1042 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
1043 ir_assignment *const else_assign =
1044 new(ctx) ir_assignment(else_deref, op[1], NULL);
1045 stmt->else_instructions.push_tail(else_assign);
1046
1047 result = new(ctx) ir_dereference_variable(tmp);
1048 type = tmp->type;
1049 }
1050 break;
1051 }
1052
1053 case ast_logic_xor:
1054 op[0] = this->subexpressions[0]->hir(instructions, state);
1055 op[1] = this->subexpressions[1]->hir(instructions, state);
1056
1057
1058 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
1059 op[0], op[1]);
1060 type = glsl_type::bool_type;
1061 break;
1062
1063 case ast_logic_not:
1064 op[0] = this->subexpressions[0]->hir(instructions, state);
1065
1066 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
1067 YYLTYPE loc = this->subexpressions[0]->get_location();
1068
1069 _mesa_glsl_error(& loc, state,
1070 "operand of `!' must be scalar boolean");
1071 error_emitted = true;
1072 }
1073
1074 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
1075 op[0], NULL);
1076 type = glsl_type::bool_type;
1077 break;
1078
1079 case ast_mul_assign:
1080 case ast_div_assign:
1081 case ast_add_assign:
1082 case ast_sub_assign: {
1083 op[0] = this->subexpressions[0]->hir(instructions, state);
1084 op[1] = this->subexpressions[1]->hir(instructions, state);
1085
1086 type = arithmetic_result_type(op[0], op[1],
1087 (this->oper == ast_mul_assign),
1088 state, & loc);
1089
1090 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1091 op[0], op[1]);
1092
1093 result = do_assignment(instructions, state,
1094 op[0]->clone(ctx, NULL), temp_rhs,
1095 this->subexpressions[0]->get_location());
1096 type = result->type;
1097 error_emitted = (op[0]->type->is_error());
1098
1099 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1100 * explicitly test for this because none of the binary expression
1101 * operators allow array operands either.
1102 */
1103
1104 break;
1105 }
1106
1107 case ast_mod_assign: {
1108 op[0] = this->subexpressions[0]->hir(instructions, state);
1109 op[1] = this->subexpressions[1]->hir(instructions, state);
1110
1111 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
1112
1113 assert(operations[this->oper] == ir_binop_mod);
1114
1115 ir_rvalue *temp_rhs;
1116 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1117 op[0], op[1]);
1118
1119 result = do_assignment(instructions, state,
1120 op[0]->clone(ctx, NULL), temp_rhs,
1121 this->subexpressions[0]->get_location());
1122 type = result->type;
1123 error_emitted = type->is_error();
1124 break;
1125 }
1126
1127 case ast_ls_assign:
1128 case ast_rs_assign:
1129 _mesa_glsl_error(& loc, state,
1130 "FINISHME: implement bit-shift assignment operators");
1131 error_emitted = true;
1132 break;
1133
1134 case ast_and_assign:
1135 case ast_xor_assign:
1136 case ast_or_assign:
1137 _mesa_glsl_error(& loc, state,
1138 "FINISHME: implement logic assignment operators");
1139 error_emitted = true;
1140 break;
1141
1142 case ast_conditional: {
1143 op[0] = this->subexpressions[0]->hir(instructions, state);
1144
1145 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1146 *
1147 * "The ternary selection operator (?:). It operates on three
1148 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1149 * first expression, which must result in a scalar Boolean."
1150 */
1151 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
1152 YYLTYPE loc = this->subexpressions[0]->get_location();
1153
1154 _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean");
1155 error_emitted = true;
1156 }
1157
1158 /* The :? operator is implemented by generating an anonymous temporary
1159 * followed by an if-statement. The last instruction in each branch of
1160 * the if-statement assigns a value to the anonymous temporary. This
1161 * temporary is the r-value of the expression.
1162 */
1163 exec_list then_instructions;
1164 exec_list else_instructions;
1165
1166 op[1] = this->subexpressions[1]->hir(&then_instructions, state);
1167 op[2] = this->subexpressions[2]->hir(&else_instructions, state);
1168
1169 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1170 *
1171 * "The second and third expressions can be any type, as
1172 * long their types match, or there is a conversion in
1173 * Section 4.1.10 "Implicit Conversions" that can be applied
1174 * to one of the expressions to make their types match. This
1175 * resulting matching type is the type of the entire
1176 * expression."
1177 */
1178 if ((!apply_implicit_conversion(op[1]->type, op[2], state)
1179 && !apply_implicit_conversion(op[2]->type, op[1], state))
1180 || (op[1]->type != op[2]->type)) {
1181 YYLTYPE loc = this->subexpressions[1]->get_location();
1182
1183 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
1184 "operator must have matching types.");
1185 error_emitted = true;
1186 type = glsl_type::error_type;
1187 } else {
1188 type = op[1]->type;
1189 }
1190
1191 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1192 *
1193 * "The second and third expressions must be the same type, but can
1194 * be of any type other than an array."
1195 */
1196 if ((state->language_version <= 110) && type->is_array()) {
1197 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
1198 "operator must not be arrays.");
1199 error_emitted = true;
1200 }
1201
1202 ir_constant *cond_val = op[0]->constant_expression_value();
1203 ir_constant *then_val = op[1]->constant_expression_value();
1204 ir_constant *else_val = op[2]->constant_expression_value();
1205
1206 if (then_instructions.is_empty()
1207 && else_instructions.is_empty()
1208 && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) {
1209 result = (cond_val->value.b[0]) ? then_val : else_val;
1210 } else {
1211 ir_variable *const tmp =
1212 new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
1213 instructions->push_tail(tmp);
1214
1215 ir_if *const stmt = new(ctx) ir_if(op[0]);
1216 instructions->push_tail(stmt);
1217
1218 then_instructions.move_nodes_to(& stmt->then_instructions);
1219 ir_dereference *const then_deref =
1220 new(ctx) ir_dereference_variable(tmp);
1221 ir_assignment *const then_assign =
1222 new(ctx) ir_assignment(then_deref, op[1], NULL);
1223 stmt->then_instructions.push_tail(then_assign);
1224
1225 else_instructions.move_nodes_to(& stmt->else_instructions);
1226 ir_dereference *const else_deref =
1227 new(ctx) ir_dereference_variable(tmp);
1228 ir_assignment *const else_assign =
1229 new(ctx) ir_assignment(else_deref, op[2], NULL);
1230 stmt->else_instructions.push_tail(else_assign);
1231
1232 result = new(ctx) ir_dereference_variable(tmp);
1233 }
1234 break;
1235 }
1236
1237 case ast_pre_inc:
1238 case ast_pre_dec: {
1239 op[0] = this->subexpressions[0]->hir(instructions, state);
1240 if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
1241 op[1] = new(ctx) ir_constant(1.0f);
1242 else
1243 op[1] = new(ctx) ir_constant(1);
1244
1245 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1246
1247 ir_rvalue *temp_rhs;
1248 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1249 op[0], op[1]);
1250
1251 result = do_assignment(instructions, state,
1252 op[0]->clone(ctx, NULL), temp_rhs,
1253 this->subexpressions[0]->get_location());
1254 type = result->type;
1255 error_emitted = op[0]->type->is_error();
1256 break;
1257 }
1258
1259 case ast_post_inc:
1260 case ast_post_dec: {
1261 op[0] = this->subexpressions[0]->hir(instructions, state);
1262 if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
1263 op[1] = new(ctx) ir_constant(1.0f);
1264 else
1265 op[1] = new(ctx) ir_constant(1);
1266
1267 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1268
1269 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1270
1271 ir_rvalue *temp_rhs;
1272 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1273 op[0], op[1]);
1274
1275 /* Get a temporary of a copy of the lvalue before it's modified.
1276 * This may get thrown away later.
1277 */
1278 result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
1279
1280 (void)do_assignment(instructions, state,
1281 op[0]->clone(ctx, NULL), temp_rhs,
1282 this->subexpressions[0]->get_location());
1283
1284 type = result->type;
1285 error_emitted = op[0]->type->is_error();
1286 break;
1287 }
1288
1289 case ast_field_selection:
1290 result = _mesa_ast_field_selection_to_hir(this, instructions, state);
1291 type = result->type;
1292 break;
1293
1294 case ast_array_index: {
1295 YYLTYPE index_loc = subexpressions[1]->get_location();
1296
1297 op[0] = subexpressions[0]->hir(instructions, state);
1298 op[1] = subexpressions[1]->hir(instructions, state);
1299
1300 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1301
1302 ir_rvalue *const array = op[0];
1303
1304 result = new(ctx) ir_dereference_array(op[0], op[1]);
1305
1306 /* Do not use op[0] after this point. Use array.
1307 */
1308 op[0] = NULL;
1309
1310
1311 if (error_emitted)
1312 break;
1313
1314 if (!array->type->is_array()
1315 && !array->type->is_matrix()
1316 && !array->type->is_vector()) {
1317 _mesa_glsl_error(& index_loc, state,
1318 "cannot dereference non-array / non-matrix / "
1319 "non-vector");
1320 error_emitted = true;
1321 }
1322
1323 if (!op[1]->type->is_integer()) {
1324 _mesa_glsl_error(& index_loc, state,
1325 "array index must be integer type");
1326 error_emitted = true;
1327 } else if (!op[1]->type->is_scalar()) {
1328 _mesa_glsl_error(& index_loc, state,
1329 "array index must be scalar");
1330 error_emitted = true;
1331 }
1332
1333 /* If the array index is a constant expression and the array has a
1334 * declared size, ensure that the access is in-bounds. If the array
1335 * index is not a constant expression, ensure that the array has a
1336 * declared size.
1337 */
1338 ir_constant *const const_index = op[1]->constant_expression_value();
1339 if (const_index != NULL) {
1340 const int idx = const_index->value.i[0];
1341 const char *type_name;
1342 unsigned bound = 0;
1343
1344 if (array->type->is_matrix()) {
1345 type_name = "matrix";
1346 } else if (array->type->is_vector()) {
1347 type_name = "vector";
1348 } else {
1349 type_name = "array";
1350 }
1351
1352 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
1353 *
1354 * "It is illegal to declare an array with a size, and then
1355 * later (in the same shader) index the same array with an
1356 * integral constant expression greater than or equal to the
1357 * declared size. It is also illegal to index an array with a
1358 * negative constant expression."
1359 */
1360 if (array->type->is_matrix()) {
1361 if (array->type->row_type()->vector_elements <= idx) {
1362 bound = array->type->row_type()->vector_elements;
1363 }
1364 } else if (array->type->is_vector()) {
1365 if (array->type->vector_elements <= idx) {
1366 bound = array->type->vector_elements;
1367 }
1368 } else {
1369 if ((array->type->array_size() > 0)
1370 && (array->type->array_size() <= idx)) {
1371 bound = array->type->array_size();
1372 }
1373 }
1374
1375 if (bound > 0) {
1376 _mesa_glsl_error(& loc, state, "%s index must be < %u",
1377 type_name, bound);
1378 error_emitted = true;
1379 } else if (idx < 0) {
1380 _mesa_glsl_error(& loc, state, "%s index must be >= 0",
1381 type_name);
1382 error_emitted = true;
1383 }
1384
1385 if (array->type->is_array()) {
1386 /* If the array is a variable dereference, it dereferences the
1387 * whole array, by definition. Use this to get the variable.
1388 *
1389 * FINISHME: Should some methods for getting / setting / testing
1390 * FINISHME: array access limits be added to ir_dereference?
1391 */
1392 ir_variable *const v = array->whole_variable_referenced();
1393 if ((v != NULL) && (unsigned(idx) > v->max_array_access))
1394 v->max_array_access = idx;
1395 }
1396 } else if (array->type->array_size() == 0) {
1397 _mesa_glsl_error(&loc, state, "unsized array index must be constant");
1398 } else {
1399 if (array->type->is_array()) {
1400 /* whole_variable_referenced can return NULL if the array is a
1401 * member of a structure. In this case it is safe to not update
1402 * the max_array_access field because it is never used for fields
1403 * of structures.
1404 */
1405 ir_variable *v = array->whole_variable_referenced();
1406 if (v != NULL)
1407 v->max_array_access = array->type->array_size();
1408 }
1409 }
1410
1411 if (error_emitted)
1412 result->type = glsl_type::error_type;
1413
1414 type = result->type;
1415 break;
1416 }
1417
1418 case ast_function_call:
1419 /* Should *NEVER* get here. ast_function_call should always be handled
1420 * by ast_function_expression::hir.
1421 */
1422 assert(0);
1423 break;
1424
1425 case ast_identifier: {
1426 /* ast_identifier can appear several places in a full abstract syntax
1427 * tree. This particular use must be at location specified in the grammar
1428 * as 'variable_identifier'.
1429 */
1430 ir_variable *var =
1431 state->symbols->get_variable(this->primary_expression.identifier);
1432
1433 result = new(ctx) ir_dereference_variable(var);
1434
1435 if (var != NULL) {
1436 type = result->type;
1437 } else {
1438 _mesa_glsl_error(& loc, state, "`%s' undeclared",
1439 this->primary_expression.identifier);
1440
1441 error_emitted = true;
1442 }
1443 break;
1444 }
1445
1446 case ast_int_constant:
1447 type = glsl_type::int_type;
1448 result = new(ctx) ir_constant(this->primary_expression.int_constant);
1449 break;
1450
1451 case ast_uint_constant:
1452 type = glsl_type::uint_type;
1453 result = new(ctx) ir_constant(this->primary_expression.uint_constant);
1454 break;
1455
1456 case ast_float_constant:
1457 type = glsl_type::float_type;
1458 result = new(ctx) ir_constant(this->primary_expression.float_constant);
1459 break;
1460
1461 case ast_bool_constant:
1462 type = glsl_type::bool_type;
1463 result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
1464 break;
1465
1466 case ast_sequence: {
1467 /* It should not be possible to generate a sequence in the AST without
1468 * any expressions in it.
1469 */
1470 assert(!this->expressions.is_empty());
1471
1472 /* The r-value of a sequence is the last expression in the sequence. If
1473 * the other expressions in the sequence do not have side-effects (and
1474 * therefore add instructions to the instruction list), they get dropped
1475 * on the floor.
1476 */
1477 foreach_list_typed (ast_node, ast, link, &this->expressions)
1478 result = ast->hir(instructions, state);
1479
1480 type = result->type;
1481
1482 /* Any errors should have already been emitted in the loop above.
1483 */
1484 error_emitted = true;
1485 break;
1486 }
1487 }
1488
1489 if (type->is_error() && !error_emitted)
1490 _mesa_glsl_error(& loc, state, "type mismatch");
1491
1492 return result;
1493 }
1494
1495
1496 ir_rvalue *
1497 ast_expression_statement::hir(exec_list *instructions,
1498 struct _mesa_glsl_parse_state *state)
1499 {
1500 /* It is possible to have expression statements that don't have an
1501 * expression. This is the solitary semicolon:
1502 *
1503 * for (i = 0; i < 5; i++)
1504 * ;
1505 *
1506 * In this case the expression will be NULL. Test for NULL and don't do
1507 * anything in that case.
1508 */
1509 if (expression != NULL)
1510 expression->hir(instructions, state);
1511
1512 /* Statements do not have r-values.
1513 */
1514 return NULL;
1515 }
1516
1517
1518 ir_rvalue *
1519 ast_compound_statement::hir(exec_list *instructions,
1520 struct _mesa_glsl_parse_state *state)
1521 {
1522 if (new_scope)
1523 state->symbols->push_scope();
1524
1525 foreach_list_typed (ast_node, ast, link, &this->statements)
1526 ast->hir(instructions, state);
1527
1528 if (new_scope)
1529 state->symbols->pop_scope();
1530
1531 /* Compound statements do not have r-values.
1532 */
1533 return NULL;
1534 }
1535
1536
1537 static const glsl_type *
1538 process_array_type(YYLTYPE *loc, const glsl_type *base, ast_node *array_size,
1539 struct _mesa_glsl_parse_state *state)
1540 {
1541 unsigned length = 0;
1542
1543 /* FINISHME: Reject delcarations of multidimensional arrays. */
1544
1545 if (array_size != NULL) {
1546 exec_list dummy_instructions;
1547 ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
1548 YYLTYPE loc = array_size->get_location();
1549
1550 /* FINISHME: Verify that the grammar forbids side-effects in array
1551 * FINISHME: sizes. i.e., 'vec4 [x = 12] data'
1552 */
1553 assert(dummy_instructions.is_empty());
1554
1555 if (ir != NULL) {
1556 if (!ir->type->is_integer()) {
1557 _mesa_glsl_error(& loc, state, "array size must be integer type");
1558 } else if (!ir->type->is_scalar()) {
1559 _mesa_glsl_error(& loc, state, "array size must be scalar type");
1560 } else {
1561 ir_constant *const size = ir->constant_expression_value();
1562
1563 if (size == NULL) {
1564 _mesa_glsl_error(& loc, state, "array size must be a "
1565 "constant valued expression");
1566 } else if (size->value.i[0] <= 0) {
1567 _mesa_glsl_error(& loc, state, "array size must be > 0");
1568 } else {
1569 assert(size->type == ir->type);
1570 length = size->value.u[0];
1571 }
1572 }
1573 }
1574 } else if (state->es_shader) {
1575 /* Section 10.17 of the GLSL ES 1.00 specification states that unsized
1576 * array declarations have been removed from the language.
1577 */
1578 _mesa_glsl_error(loc, state, "unsized array declarations are not "
1579 "allowed in GLSL ES 1.00.");
1580 }
1581
1582 return glsl_type::get_array_instance(base, length);
1583 }
1584
1585
1586 const glsl_type *
1587 ast_type_specifier::glsl_type(const char **name,
1588 struct _mesa_glsl_parse_state *state) const
1589 {
1590 const struct glsl_type *type;
1591
1592 type = state->symbols->get_type(this->type_name);
1593 *name = this->type_name;
1594
1595 if (this->is_array) {
1596 YYLTYPE loc = this->get_location();
1597 type = process_array_type(&loc, type, this->array_size, state);
1598 }
1599
1600 return type;
1601 }
1602
1603
1604 static void
1605 apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
1606 ir_variable *var,
1607 struct _mesa_glsl_parse_state *state,
1608 YYLTYPE *loc)
1609 {
1610 if (qual->flags.q.invariant)
1611 var->invariant = 1;
1612
1613 /* FINISHME: Mark 'in' variables at global scope as read-only. */
1614 if (qual->flags.q.constant || qual->flags.q.attribute
1615 || qual->flags.q.uniform
1616 || (qual->flags.q.varying && (state->target == fragment_shader)))
1617 var->read_only = 1;
1618
1619 if (qual->flags.q.centroid)
1620 var->centroid = 1;
1621
1622 if (qual->flags.q.attribute && state->target != vertex_shader) {
1623 var->type = glsl_type::error_type;
1624 _mesa_glsl_error(loc, state,
1625 "`attribute' variables may not be declared in the "
1626 "%s shader",
1627 _mesa_glsl_shader_target_name(state->target));
1628 }
1629
1630 /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
1631 *
1632 * "The varying qualifier can be used only with the data types
1633 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
1634 * these."
1635 */
1636 if (qual->flags.q.varying) {
1637 const glsl_type *non_array_type;
1638
1639 if (var->type && var->type->is_array())
1640 non_array_type = var->type->fields.array;
1641 else
1642 non_array_type = var->type;
1643
1644 if (non_array_type && non_array_type->base_type != GLSL_TYPE_FLOAT) {
1645 var->type = glsl_type::error_type;
1646 _mesa_glsl_error(loc, state,
1647 "varying variables must be of base type float");
1648 }
1649 }
1650
1651 /* If there is no qualifier that changes the mode of the variable, leave
1652 * the setting alone.
1653 */
1654 if (qual->flags.q.in && qual->flags.q.out)
1655 var->mode = ir_var_inout;
1656 else if (qual->flags.q.attribute || qual->flags.q.in
1657 || (qual->flags.q.varying && (state->target == fragment_shader)))
1658 var->mode = ir_var_in;
1659 else if (qual->flags.q.out
1660 || (qual->flags.q.varying && (state->target == vertex_shader)))
1661 var->mode = ir_var_out;
1662 else if (qual->flags.q.uniform)
1663 var->mode = ir_var_uniform;
1664
1665 if (qual->flags.q.flat)
1666 var->interpolation = ir_var_flat;
1667 else if (qual->flags.q.noperspective)
1668 var->interpolation = ir_var_noperspective;
1669 else
1670 var->interpolation = ir_var_smooth;
1671
1672 var->pixel_center_integer = qual->flags.q.pixel_center_integer;
1673 var->origin_upper_left = qual->flags.q.origin_upper_left;
1674 if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer)
1675 && (strcmp(var->name, "gl_FragCoord") != 0)) {
1676 const char *const qual_string = (qual->flags.q.origin_upper_left)
1677 ? "origin_upper_left" : "pixel_center_integer";
1678
1679 _mesa_glsl_error(loc, state,
1680 "layout qualifier `%s' can only be applied to "
1681 "fragment shader input `gl_FragCoord'",
1682 qual_string);
1683 }
1684
1685 if (qual->flags.q.explicit_location) {
1686 const bool global_scope = (state->current_function == NULL);
1687 bool fail = false;
1688 const char *string = "";
1689
1690 /* In the vertex shader only shader inputs can be given explicit
1691 * locations.
1692 *
1693 * In the fragment shader only shader outputs can be given explicit
1694 * locations.
1695 */
1696 switch (state->target) {
1697 case vertex_shader:
1698 if (!global_scope || (var->mode != ir_var_in)) {
1699 fail = true;
1700 string = "input";
1701 }
1702 break;
1703
1704 case geometry_shader:
1705 _mesa_glsl_error(loc, state,
1706 "geometry shader variables cannot be given "
1707 "explicit locations\n");
1708 break;
1709
1710 case fragment_shader:
1711 if (!global_scope || (var->mode != ir_var_in)) {
1712 fail = true;
1713 string = "output";
1714 }
1715 break;
1716 }
1717
1718 if (fail) {
1719 _mesa_glsl_error(loc, state,
1720 "only %s shader %s variables can be given an "
1721 "explicit location\n",
1722 _mesa_glsl_shader_target_name(state->target),
1723 string);
1724 } else {
1725 var->explicit_location = true;
1726
1727 /* This bit of silliness is needed because invalid explicit locations
1728 * are supposed to be flagged during linking. Small negative values
1729 * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
1730 * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
1731 * The linker needs to be able to differentiate these cases. This
1732 * ensures that negative values stay negative.
1733 */
1734 if (qual->location >= 0) {
1735 var->location = (state->target == vertex_shader)
1736 ? (qual->location + VERT_ATTRIB_GENERIC0)
1737 : (qual->location + FRAG_RESULT_DATA0);
1738 } else {
1739 var->location = qual->location;
1740 }
1741 }
1742 }
1743
1744 if (var->type->is_array() && state->language_version != 110) {
1745 var->array_lvalue = true;
1746 }
1747 }
1748
1749
1750 ir_rvalue *
1751 ast_declarator_list::hir(exec_list *instructions,
1752 struct _mesa_glsl_parse_state *state)
1753 {
1754 void *ctx = state;
1755 const struct glsl_type *decl_type;
1756 const char *type_name = NULL;
1757 ir_rvalue *result = NULL;
1758 YYLTYPE loc = this->get_location();
1759
1760 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
1761 *
1762 * "To ensure that a particular output variable is invariant, it is
1763 * necessary to use the invariant qualifier. It can either be used to
1764 * qualify a previously declared variable as being invariant
1765 *
1766 * invariant gl_Position; // make existing gl_Position be invariant"
1767 *
1768 * In these cases the parser will set the 'invariant' flag in the declarator
1769 * list, and the type will be NULL.
1770 */
1771 if (this->invariant) {
1772 assert(this->type == NULL);
1773
1774 if (state->current_function != NULL) {
1775 _mesa_glsl_error(& loc, state,
1776 "All uses of `invariant' keyword must be at global "
1777 "scope\n");
1778 }
1779
1780 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
1781 assert(!decl->is_array);
1782 assert(decl->array_size == NULL);
1783 assert(decl->initializer == NULL);
1784
1785 ir_variable *const earlier =
1786 state->symbols->get_variable(decl->identifier);
1787 if (earlier == NULL) {
1788 _mesa_glsl_error(& loc, state,
1789 "Undeclared variable `%s' cannot be marked "
1790 "invariant\n", decl->identifier);
1791 } else if ((state->target == vertex_shader)
1792 && (earlier->mode != ir_var_out)) {
1793 _mesa_glsl_error(& loc, state,
1794 "`%s' cannot be marked invariant, vertex shader "
1795 "outputs only\n", decl->identifier);
1796 } else if ((state->target == fragment_shader)
1797 && (earlier->mode != ir_var_in)) {
1798 _mesa_glsl_error(& loc, state,
1799 "`%s' cannot be marked invariant, fragment shader "
1800 "inputs only\n", decl->identifier);
1801 } else {
1802 earlier->invariant = true;
1803 }
1804 }
1805
1806 /* Invariant redeclarations do not have r-values.
1807 */
1808 return NULL;
1809 }
1810
1811 assert(this->type != NULL);
1812 assert(!this->invariant);
1813
1814 /* The type specifier may contain a structure definition. Process that
1815 * before any of the variable declarations.
1816 */
1817 (void) this->type->specifier->hir(instructions, state);
1818
1819 decl_type = this->type->specifier->glsl_type(& type_name, state);
1820 if (this->declarations.is_empty()) {
1821 /* The only valid case where the declaration list can be empty is when
1822 * the declaration is setting the default precision of a built-in type
1823 * (e.g., 'precision highp vec4;').
1824 */
1825
1826 if (decl_type != NULL) {
1827 } else {
1828 _mesa_glsl_error(& loc, state, "incomplete declaration");
1829 }
1830 }
1831
1832 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
1833 const struct glsl_type *var_type;
1834 ir_variable *var;
1835
1836 /* FINISHME: Emit a warning if a variable declaration shadows a
1837 * FINISHME: declaration at a higher scope.
1838 */
1839
1840 if ((decl_type == NULL) || decl_type->is_void()) {
1841 if (type_name != NULL) {
1842 _mesa_glsl_error(& loc, state,
1843 "invalid type `%s' in declaration of `%s'",
1844 type_name, decl->identifier);
1845 } else {
1846 _mesa_glsl_error(& loc, state,
1847 "invalid type in declaration of `%s'",
1848 decl->identifier);
1849 }
1850 continue;
1851 }
1852
1853 if (decl->is_array) {
1854 var_type = process_array_type(&loc, decl_type, decl->array_size,
1855 state);
1856 } else {
1857 var_type = decl_type;
1858 }
1859
1860 var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto);
1861
1862 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
1863 *
1864 * "Global variables can only use the qualifiers const,
1865 * attribute, uni form, or varying. Only one may be
1866 * specified.
1867 *
1868 * Local variables can only use the qualifier const."
1869 *
1870 * This is relaxed in GLSL 1.30.
1871 */
1872 if (state->language_version < 120) {
1873 if (this->type->qualifier.flags.q.out) {
1874 _mesa_glsl_error(& loc, state,
1875 "`out' qualifier in declaration of `%s' "
1876 "only valid for function parameters in GLSL 1.10.",
1877 decl->identifier);
1878 }
1879 if (this->type->qualifier.flags.q.in) {
1880 _mesa_glsl_error(& loc, state,
1881 "`in' qualifier in declaration of `%s' "
1882 "only valid for function parameters in GLSL 1.10.",
1883 decl->identifier);
1884 }
1885 /* FINISHME: Test for other invalid qualifiers. */
1886 }
1887
1888 apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
1889 & loc);
1890
1891 if (this->type->qualifier.flags.q.invariant) {
1892 if ((state->target == vertex_shader) && !(var->mode == ir_var_out ||
1893 var->mode == ir_var_inout)) {
1894 /* FINISHME: Note that this doesn't work for invariant on
1895 * a function signature outval
1896 */
1897 _mesa_glsl_error(& loc, state,
1898 "`%s' cannot be marked invariant, vertex shader "
1899 "outputs only\n", var->name);
1900 } else if ((state->target == fragment_shader) &&
1901 !(var->mode == ir_var_in || var->mode == ir_var_inout)) {
1902 /* FINISHME: Note that this doesn't work for invariant on
1903 * a function signature inval
1904 */
1905 _mesa_glsl_error(& loc, state,
1906 "`%s' cannot be marked invariant, fragment shader "
1907 "inputs only\n", var->name);
1908 }
1909 }
1910
1911 if (state->current_function != NULL) {
1912 const char *mode = NULL;
1913 const char *extra = "";
1914
1915 /* There is no need to check for 'inout' here because the parser will
1916 * only allow that in function parameter lists.
1917 */
1918 if (this->type->qualifier.flags.q.attribute) {
1919 mode = "attribute";
1920 } else if (this->type->qualifier.flags.q.uniform) {
1921 mode = "uniform";
1922 } else if (this->type->qualifier.flags.q.varying) {
1923 mode = "varying";
1924 } else if (this->type->qualifier.flags.q.in) {
1925 mode = "in";
1926 extra = " or in function parameter list";
1927 } else if (this->type->qualifier.flags.q.out) {
1928 mode = "out";
1929 extra = " or in function parameter list";
1930 }
1931
1932 if (mode) {
1933 _mesa_glsl_error(& loc, state,
1934 "%s variable `%s' must be declared at "
1935 "global scope%s",
1936 mode, var->name, extra);
1937 }
1938 } else if (var->mode == ir_var_in) {
1939 if (state->target == vertex_shader) {
1940 bool error_emitted = false;
1941
1942 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1943 *
1944 * "Vertex shader inputs can only be float, floating-point
1945 * vectors, matrices, signed and unsigned integers and integer
1946 * vectors. Vertex shader inputs can also form arrays of these
1947 * types, but not structures."
1948 *
1949 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
1950 *
1951 * "Vertex shader inputs can only be float, floating-point
1952 * vectors, matrices, signed and unsigned integers and integer
1953 * vectors. They cannot be arrays or structures."
1954 *
1955 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1956 *
1957 * "The attribute qualifier can be used only with float,
1958 * floating-point vectors, and matrices. Attribute variables
1959 * cannot be declared as arrays or structures."
1960 */
1961 const glsl_type *check_type = var->type->is_array()
1962 ? var->type->fields.array : var->type;
1963
1964 switch (check_type->base_type) {
1965 case GLSL_TYPE_FLOAT:
1966 break;
1967 case GLSL_TYPE_UINT:
1968 case GLSL_TYPE_INT:
1969 if (state->language_version > 120)
1970 break;
1971 /* FALLTHROUGH */
1972 default:
1973 _mesa_glsl_error(& loc, state,
1974 "vertex shader input / attribute cannot have "
1975 "type %s`%s'",
1976 var->type->is_array() ? "array of " : "",
1977 check_type->name);
1978 error_emitted = true;
1979 }
1980
1981 if (!error_emitted && (state->language_version <= 130)
1982 && var->type->is_array()) {
1983 _mesa_glsl_error(& loc, state,
1984 "vertex shader input / attribute cannot have "
1985 "array type");
1986 error_emitted = true;
1987 }
1988 }
1989 }
1990
1991 /* Process the initializer and add its instructions to a temporary
1992 * list. This list will be added to the instruction stream (below) after
1993 * the declaration is added. This is done because in some cases (such as
1994 * redeclarations) the declaration may not actually be added to the
1995 * instruction stream.
1996 */
1997 exec_list initializer_instructions;
1998 if (decl->initializer != NULL) {
1999 YYLTYPE initializer_loc = decl->initializer->get_location();
2000
2001 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
2002 *
2003 * "All uniform variables are read-only and are initialized either
2004 * directly by an application via API commands, or indirectly by
2005 * OpenGL."
2006 */
2007 if ((state->language_version <= 110)
2008 && (var->mode == ir_var_uniform)) {
2009 _mesa_glsl_error(& initializer_loc, state,
2010 "cannot initialize uniforms in GLSL 1.10");
2011 }
2012
2013 if (var->type->is_sampler()) {
2014 _mesa_glsl_error(& initializer_loc, state,
2015 "cannot initialize samplers");
2016 }
2017
2018 if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
2019 _mesa_glsl_error(& initializer_loc, state,
2020 "cannot initialize %s shader input / %s",
2021 _mesa_glsl_shader_target_name(state->target),
2022 (state->target == vertex_shader)
2023 ? "attribute" : "varying");
2024 }
2025
2026 ir_dereference *const lhs = new(ctx) ir_dereference_variable(var);
2027 ir_rvalue *rhs = decl->initializer->hir(&initializer_instructions,
2028 state);
2029
2030 /* Calculate the constant value if this is a const or uniform
2031 * declaration.
2032 */
2033 if (this->type->qualifier.flags.q.constant
2034 || this->type->qualifier.flags.q.uniform) {
2035 ir_rvalue *new_rhs = validate_assignment(state, var->type, rhs);
2036 if (new_rhs != NULL) {
2037 rhs = new_rhs;
2038
2039 ir_constant *constant_value = rhs->constant_expression_value();
2040 if (!constant_value) {
2041 _mesa_glsl_error(& initializer_loc, state,
2042 "initializer of %s variable `%s' must be a "
2043 "constant expression",
2044 (this->type->qualifier.flags.q.constant)
2045 ? "const" : "uniform",
2046 decl->identifier);
2047 if (var->type->is_numeric()) {
2048 /* Reduce cascading errors. */
2049 var->constant_value = ir_constant::zero(ctx, var->type);
2050 }
2051 } else {
2052 rhs = constant_value;
2053 var->constant_value = constant_value;
2054 }
2055 } else {
2056 _mesa_glsl_error(&initializer_loc, state,
2057 "initializer of type %s cannot be assigned to "
2058 "variable of type %s",
2059 rhs->type->name, var->type->name);
2060 if (var->type->is_numeric()) {
2061 /* Reduce cascading errors. */
2062 var->constant_value = ir_constant::zero(ctx, var->type);
2063 }
2064 }
2065 }
2066
2067 if (rhs && !rhs->type->is_error()) {
2068 bool temp = var->read_only;
2069 if (this->type->qualifier.flags.q.constant)
2070 var->read_only = false;
2071
2072 /* Never emit code to initialize a uniform.
2073 */
2074 if (!this->type->qualifier.flags.q.uniform)
2075 result = do_assignment(&initializer_instructions, state,
2076 lhs, rhs,
2077 this->get_location());
2078 var->read_only = temp;
2079 }
2080 }
2081
2082 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
2083 *
2084 * "It is an error to write to a const variable outside of
2085 * its declaration, so they must be initialized when
2086 * declared."
2087 */
2088 if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) {
2089 _mesa_glsl_error(& loc, state,
2090 "const declaration of `%s' must be initialized");
2091 }
2092
2093 /* Check if this declaration is actually a re-declaration, either to
2094 * resize an array or add qualifiers to an existing variable.
2095 *
2096 * This is allowed for variables in the current scope, or when at
2097 * global scope (for built-ins in the implicit outer scope).
2098 */
2099 ir_variable *earlier = state->symbols->get_variable(decl->identifier);
2100 if (earlier != NULL && (state->current_function == NULL ||
2101 state->symbols->name_declared_this_scope(decl->identifier))) {
2102
2103 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
2104 *
2105 * "It is legal to declare an array without a size and then
2106 * later re-declare the same name as an array of the same
2107 * type and specify a size."
2108 */
2109 if ((earlier->type->array_size() == 0)
2110 && var->type->is_array()
2111 && (var->type->element_type() == earlier->type->element_type())) {
2112 /* FINISHME: This doesn't match the qualifiers on the two
2113 * FINISHME: declarations. It's not 100% clear whether this is
2114 * FINISHME: required or not.
2115 */
2116
2117 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
2118 *
2119 * "The size [of gl_TexCoord] can be at most
2120 * gl_MaxTextureCoords."
2121 */
2122 const unsigned size = unsigned(var->type->array_size());
2123 if ((strcmp("gl_TexCoord", var->name) == 0)
2124 && (size > state->Const.MaxTextureCoords)) {
2125 YYLTYPE loc = this->get_location();
2126
2127 _mesa_glsl_error(& loc, state, "`gl_TexCoord' array size cannot "
2128 "be larger than gl_MaxTextureCoords (%u)\n",
2129 state->Const.MaxTextureCoords);
2130 } else if ((size > 0) && (size <= earlier->max_array_access)) {
2131 YYLTYPE loc = this->get_location();
2132
2133 _mesa_glsl_error(& loc, state, "array size must be > %u due to "
2134 "previous access",
2135 earlier->max_array_access);
2136 }
2137
2138 earlier->type = var->type;
2139 delete var;
2140 var = NULL;
2141 } else if (state->extensions->ARB_fragment_coord_conventions
2142 && strcmp(var->name, "gl_FragCoord") == 0
2143 && earlier->type == var->type
2144 && earlier->mode == var->mode) {
2145 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
2146 * qualifiers.
2147 */
2148 earlier->origin_upper_left = var->origin_upper_left;
2149 earlier->pixel_center_integer = var->pixel_center_integer;
2150 } else {
2151 YYLTYPE loc = this->get_location();
2152 _mesa_glsl_error(&loc, state, "`%s' redeclared", decl->identifier);
2153 }
2154
2155 continue;
2156 }
2157
2158 /* By now, we know it's a new variable declaration (we didn't hit the
2159 * above "continue").
2160 *
2161 * From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
2162 *
2163 * "Identifiers starting with "gl_" are reserved for use by
2164 * OpenGL, and may not be declared in a shader as either a
2165 * variable or a function."
2166 */
2167 if (strncmp(decl->identifier, "gl_", 3) == 0)
2168 _mesa_glsl_error(& loc, state,
2169 "identifier `%s' uses reserved `gl_' prefix",
2170 decl->identifier);
2171
2172 /* Add the variable to the symbol table. Note that the initializer's
2173 * IR was already processed earlier (though it hasn't been emitted yet),
2174 * without the variable in scope.
2175 *
2176 * This differs from most C-like languages, but it follows the GLSL
2177 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
2178 * spec:
2179 *
2180 * "Within a declaration, the scope of a name starts immediately
2181 * after the initializer if present or immediately after the name
2182 * being declared if not."
2183 */
2184 if (!state->symbols->add_variable(var->name, var)) {
2185 YYLTYPE loc = this->get_location();
2186 _mesa_glsl_error(&loc, state, "name `%s' already taken in the "
2187 "current scope", decl->identifier);
2188 continue;
2189 }
2190
2191 /* Push the variable declaration to the top. It means that all
2192 * the variable declarations will appear in a funny
2193 * last-to-first order, but otherwise we run into trouble if a
2194 * function is prototyped, a global var is decled, then the
2195 * function is defined with usage of the global var. See
2196 * glslparsertest's CorrectModule.frag.
2197 */
2198 instructions->push_head(var);
2199 instructions->append_list(&initializer_instructions);
2200 }
2201
2202
2203 /* Generally, variable declarations do not have r-values. However,
2204 * one is used for the declaration in
2205 *
2206 * while (bool b = some_condition()) {
2207 * ...
2208 * }
2209 *
2210 * so we return the rvalue from the last seen declaration here.
2211 */
2212 return result;
2213 }
2214
2215
2216 ir_rvalue *
2217 ast_parameter_declarator::hir(exec_list *instructions,
2218 struct _mesa_glsl_parse_state *state)
2219 {
2220 void *ctx = state;
2221 const struct glsl_type *type;
2222 const char *name = NULL;
2223 YYLTYPE loc = this->get_location();
2224
2225 type = this->type->specifier->glsl_type(& name, state);
2226
2227 if (type == NULL) {
2228 if (name != NULL) {
2229 _mesa_glsl_error(& loc, state,
2230 "invalid type `%s' in declaration of `%s'",
2231 name, this->identifier);
2232 } else {
2233 _mesa_glsl_error(& loc, state,
2234 "invalid type in declaration of `%s'",
2235 this->identifier);
2236 }
2237
2238 type = glsl_type::error_type;
2239 }
2240
2241 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
2242 *
2243 * "Functions that accept no input arguments need not use void in the
2244 * argument list because prototypes (or definitions) are required and
2245 * therefore there is no ambiguity when an empty argument list "( )" is
2246 * declared. The idiom "(void)" as a parameter list is provided for
2247 * convenience."
2248 *
2249 * Placing this check here prevents a void parameter being set up
2250 * for a function, which avoids tripping up checks for main taking
2251 * parameters and lookups of an unnamed symbol.
2252 */
2253 if (type->is_void()) {
2254 if (this->identifier != NULL)
2255 _mesa_glsl_error(& loc, state,
2256 "named parameter cannot have type `void'");
2257
2258 is_void = true;
2259 return NULL;
2260 }
2261
2262 if (formal_parameter && (this->identifier == NULL)) {
2263 _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
2264 return NULL;
2265 }
2266
2267 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
2268 * call already handled the "vec4[..] foo" case.
2269 */
2270 if (this->is_array) {
2271 type = process_array_type(&loc, type, this->array_size, state);
2272 }
2273
2274 if (type->array_size() == 0) {
2275 _mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
2276 "a declared size.");
2277 type = glsl_type::error_type;
2278 }
2279
2280 is_void = false;
2281 ir_variable *var = new(ctx) ir_variable(type, this->identifier, ir_var_in);
2282
2283 /* Apply any specified qualifiers to the parameter declaration. Note that
2284 * for function parameters the default mode is 'in'.
2285 */
2286 apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
2287
2288 instructions->push_tail(var);
2289
2290 /* Parameter declarations do not have r-values.
2291 */
2292 return NULL;
2293 }
2294
2295
2296 void
2297 ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
2298 bool formal,
2299 exec_list *ir_parameters,
2300 _mesa_glsl_parse_state *state)
2301 {
2302 ast_parameter_declarator *void_param = NULL;
2303 unsigned count = 0;
2304
2305 foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
2306 param->formal_parameter = formal;
2307 param->hir(ir_parameters, state);
2308
2309 if (param->is_void)
2310 void_param = param;
2311
2312 count++;
2313 }
2314
2315 if ((void_param != NULL) && (count > 1)) {
2316 YYLTYPE loc = void_param->get_location();
2317
2318 _mesa_glsl_error(& loc, state,
2319 "`void' parameter must be only parameter");
2320 }
2321 }
2322
2323
2324 ir_rvalue *
2325 ast_function::hir(exec_list *instructions,
2326 struct _mesa_glsl_parse_state *state)
2327 {
2328 void *ctx = state;
2329 ir_function *f = NULL;
2330 ir_function_signature *sig = NULL;
2331 exec_list hir_parameters;
2332
2333 const char *const name = identifier;
2334
2335 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
2336 *
2337 * "Function declarations (prototypes) cannot occur inside of functions;
2338 * they must be at global scope, or for the built-in functions, outside
2339 * the global scope."
2340 *
2341 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
2342 *
2343 * "User defined functions may only be defined within the global scope."
2344 *
2345 * Note that this language does not appear in GLSL 1.10.
2346 */
2347 if ((state->current_function != NULL) && (state->language_version != 110)) {
2348 YYLTYPE loc = this->get_location();
2349 _mesa_glsl_error(&loc, state,
2350 "declaration of function `%s' not allowed within "
2351 "function body", name);
2352 }
2353
2354 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
2355 *
2356 * "Identifiers starting with "gl_" are reserved for use by
2357 * OpenGL, and may not be declared in a shader as either a
2358 * variable or a function."
2359 */
2360 if (strncmp(name, "gl_", 3) == 0) {
2361 YYLTYPE loc = this->get_location();
2362 _mesa_glsl_error(&loc, state,
2363 "identifier `%s' uses reserved `gl_' prefix", name);
2364 }
2365
2366 /* Convert the list of function parameters to HIR now so that they can be
2367 * used below to compare this function's signature with previously seen
2368 * signatures for functions with the same name.
2369 */
2370 ast_parameter_declarator::parameters_to_hir(& this->parameters,
2371 is_definition,
2372 & hir_parameters, state);
2373
2374 const char *return_type_name;
2375 const glsl_type *return_type =
2376 this->return_type->specifier->glsl_type(& return_type_name, state);
2377
2378 if (!return_type) {
2379 YYLTYPE loc = this->get_location();
2380 _mesa_glsl_error(&loc, state,
2381 "function `%s' has undeclared return type `%s'",
2382 name, return_type_name);
2383 return_type = glsl_type::error_type;
2384 }
2385
2386 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
2387 * "No qualifier is allowed on the return type of a function."
2388 */
2389 if (this->return_type->has_qualifiers()) {
2390 YYLTYPE loc = this->get_location();
2391 _mesa_glsl_error(& loc, state,
2392 "function `%s' return type has qualifiers", name);
2393 }
2394
2395 /* Verify that this function's signature either doesn't match a previously
2396 * seen signature for a function with the same name, or, if a match is found,
2397 * that the previously seen signature does not have an associated definition.
2398 */
2399 f = state->symbols->get_function(name);
2400 if (f != NULL && (state->es_shader || f->has_user_signature())) {
2401 sig = f->exact_matching_signature(&hir_parameters);
2402 if (sig != NULL) {
2403 const char *badvar = sig->qualifiers_match(&hir_parameters);
2404 if (badvar != NULL) {
2405 YYLTYPE loc = this->get_location();
2406
2407 _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
2408 "qualifiers don't match prototype", name, badvar);
2409 }
2410
2411 if (sig->return_type != return_type) {
2412 YYLTYPE loc = this->get_location();
2413
2414 _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
2415 "match prototype", name);
2416 }
2417
2418 if (is_definition && sig->is_defined) {
2419 YYLTYPE loc = this->get_location();
2420
2421 _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
2422 }
2423 }
2424 } else {
2425 f = new(ctx) ir_function(name);
2426 if (!state->symbols->add_function(f->name, f)) {
2427 /* This function name shadows a non-function use of the same name. */
2428 YYLTYPE loc = this->get_location();
2429
2430 _mesa_glsl_error(&loc, state, "function name `%s' conflicts with "
2431 "non-function", name);
2432 return NULL;
2433 }
2434
2435 /* Emit the new function header */
2436 if (state->current_function == NULL)
2437 instructions->push_tail(f);
2438 else {
2439 /* IR invariants disallow function declarations or definitions nested
2440 * within other function definitions. Insert the new ir_function
2441 * block in the instruction sequence before the ir_function block
2442 * containing the current ir_function_signature.
2443 *
2444 * This can only happen in a GLSL 1.10 shader. In all other GLSL
2445 * versions this nesting is disallowed. There is a check for this at
2446 * the top of this function.
2447 */
2448 ir_function *const curr =
2449 const_cast<ir_function *>(state->current_function->function());
2450
2451 curr->insert_before(f);
2452 }
2453 }
2454
2455 /* Verify the return type of main() */
2456 if (strcmp(name, "main") == 0) {
2457 if (! return_type->is_void()) {
2458 YYLTYPE loc = this->get_location();
2459
2460 _mesa_glsl_error(& loc, state, "main() must return void");
2461 }
2462
2463 if (!hir_parameters.is_empty()) {
2464 YYLTYPE loc = this->get_location();
2465
2466 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
2467 }
2468 }
2469
2470 /* Finish storing the information about this new function in its signature.
2471 */
2472 if (sig == NULL) {
2473 sig = new(ctx) ir_function_signature(return_type);
2474 f->add_signature(sig);
2475 }
2476
2477 sig->replace_parameters(&hir_parameters);
2478 signature = sig;
2479
2480 /* Function declarations (prototypes) do not have r-values.
2481 */
2482 return NULL;
2483 }
2484
2485
2486 ir_rvalue *
2487 ast_function_definition::hir(exec_list *instructions,
2488 struct _mesa_glsl_parse_state *state)
2489 {
2490 prototype->is_definition = true;
2491 prototype->hir(instructions, state);
2492
2493 ir_function_signature *signature = prototype->signature;
2494 if (signature == NULL)
2495 return NULL;
2496
2497 assert(state->current_function == NULL);
2498 state->current_function = signature;
2499 state->found_return = false;
2500
2501 /* Duplicate parameters declared in the prototype as concrete variables.
2502 * Add these to the symbol table.
2503 */
2504 state->symbols->push_scope();
2505 foreach_iter(exec_list_iterator, iter, signature->parameters) {
2506 ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
2507
2508 assert(var != NULL);
2509
2510 /* The only way a parameter would "exist" is if two parameters have
2511 * the same name.
2512 */
2513 if (state->symbols->name_declared_this_scope(var->name)) {
2514 YYLTYPE loc = this->get_location();
2515
2516 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
2517 } else {
2518 state->symbols->add_variable(var->name, var);
2519 }
2520 }
2521
2522 /* Convert the body of the function to HIR. */
2523 this->body->hir(&signature->body, state);
2524 signature->is_defined = true;
2525
2526 state->symbols->pop_scope();
2527
2528 assert(state->current_function == signature);
2529 state->current_function = NULL;
2530
2531 if (!signature->return_type->is_void() && !state->found_return) {
2532 YYLTYPE loc = this->get_location();
2533 _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
2534 "%s, but no return statement",
2535 signature->function_name(),
2536 signature->return_type->name);
2537 }
2538
2539 /* Function definitions do not have r-values.
2540 */
2541 return NULL;
2542 }
2543
2544
2545 ir_rvalue *
2546 ast_jump_statement::hir(exec_list *instructions,
2547 struct _mesa_glsl_parse_state *state)
2548 {
2549 void *ctx = state;
2550
2551 switch (mode) {
2552 case ast_return: {
2553 ir_return *inst;
2554 assert(state->current_function);
2555
2556 if (opt_return_value) {
2557 if (state->current_function->return_type->base_type ==
2558 GLSL_TYPE_VOID) {
2559 YYLTYPE loc = this->get_location();
2560
2561 _mesa_glsl_error(& loc, state,
2562 "`return` with a value, in function `%s' "
2563 "returning void",
2564 state->current_function->function_name());
2565 }
2566
2567 ir_expression *const ret = (ir_expression *)
2568 opt_return_value->hir(instructions, state);
2569 assert(ret != NULL);
2570
2571 /* Implicit conversions are not allowed for return values. */
2572 if (state->current_function->return_type != ret->type) {
2573 YYLTYPE loc = this->get_location();
2574
2575 _mesa_glsl_error(& loc, state,
2576 "`return' with wrong type %s, in function `%s' "
2577 "returning %s",
2578 ret->type->name,
2579 state->current_function->function_name(),
2580 state->current_function->return_type->name);
2581 }
2582
2583 inst = new(ctx) ir_return(ret);
2584 } else {
2585 if (state->current_function->return_type->base_type !=
2586 GLSL_TYPE_VOID) {
2587 YYLTYPE loc = this->get_location();
2588
2589 _mesa_glsl_error(& loc, state,
2590 "`return' with no value, in function %s returning "
2591 "non-void",
2592 state->current_function->function_name());
2593 }
2594 inst = new(ctx) ir_return;
2595 }
2596
2597 state->found_return = true;
2598 instructions->push_tail(inst);
2599 break;
2600 }
2601
2602 case ast_discard:
2603 if (state->target != fragment_shader) {
2604 YYLTYPE loc = this->get_location();
2605
2606 _mesa_glsl_error(& loc, state,
2607 "`discard' may only appear in a fragment shader");
2608 }
2609 instructions->push_tail(new(ctx) ir_discard);
2610 break;
2611
2612 case ast_break:
2613 case ast_continue:
2614 /* FINISHME: Handle switch-statements. They cannot contain 'continue',
2615 * FINISHME: and they use a different IR instruction for 'break'.
2616 */
2617 /* FINISHME: Correctly handle the nesting. If a switch-statement is
2618 * FINISHME: inside a loop, a 'continue' is valid and will bind to the
2619 * FINISHME: loop.
2620 */
2621 if (state->loop_or_switch_nesting == NULL) {
2622 YYLTYPE loc = this->get_location();
2623
2624 _mesa_glsl_error(& loc, state,
2625 "`%s' may only appear in a loop",
2626 (mode == ast_break) ? "break" : "continue");
2627 } else {
2628 ir_loop *const loop = state->loop_or_switch_nesting->as_loop();
2629
2630 /* Inline the for loop expression again, since we don't know
2631 * where near the end of the loop body the normal copy of it
2632 * is going to be placed.
2633 */
2634 if (mode == ast_continue &&
2635 state->loop_or_switch_nesting_ast->rest_expression) {
2636 state->loop_or_switch_nesting_ast->rest_expression->hir(instructions,
2637 state);
2638 }
2639
2640 if (loop != NULL) {
2641 ir_loop_jump *const jump =
2642 new(ctx) ir_loop_jump((mode == ast_break)
2643 ? ir_loop_jump::jump_break
2644 : ir_loop_jump::jump_continue);
2645 instructions->push_tail(jump);
2646 }
2647 }
2648
2649 break;
2650 }
2651
2652 /* Jump instructions do not have r-values.
2653 */
2654 return NULL;
2655 }
2656
2657
2658 ir_rvalue *
2659 ast_selection_statement::hir(exec_list *instructions,
2660 struct _mesa_glsl_parse_state *state)
2661 {
2662 void *ctx = state;
2663
2664 ir_rvalue *const condition = this->condition->hir(instructions, state);
2665
2666 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
2667 *
2668 * "Any expression whose type evaluates to a Boolean can be used as the
2669 * conditional expression bool-expression. Vector types are not accepted
2670 * as the expression to if."
2671 *
2672 * The checks are separated so that higher quality diagnostics can be
2673 * generated for cases where both rules are violated.
2674 */
2675 if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
2676 YYLTYPE loc = this->condition->get_location();
2677
2678 _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
2679 "boolean");
2680 }
2681
2682 ir_if *const stmt = new(ctx) ir_if(condition);
2683
2684 if (then_statement != NULL) {
2685 state->symbols->push_scope();
2686 then_statement->hir(& stmt->then_instructions, state);
2687 state->symbols->pop_scope();
2688 }
2689
2690 if (else_statement != NULL) {
2691 state->symbols->push_scope();
2692 else_statement->hir(& stmt->else_instructions, state);
2693 state->symbols->pop_scope();
2694 }
2695
2696 instructions->push_tail(stmt);
2697
2698 /* if-statements do not have r-values.
2699 */
2700 return NULL;
2701 }
2702
2703
2704 void
2705 ast_iteration_statement::condition_to_hir(ir_loop *stmt,
2706 struct _mesa_glsl_parse_state *state)
2707 {
2708 void *ctx = state;
2709
2710 if (condition != NULL) {
2711 ir_rvalue *const cond =
2712 condition->hir(& stmt->body_instructions, state);
2713
2714 if ((cond == NULL)
2715 || !cond->type->is_boolean() || !cond->type->is_scalar()) {
2716 YYLTYPE loc = condition->get_location();
2717
2718 _mesa_glsl_error(& loc, state,
2719 "loop condition must be scalar boolean");
2720 } else {
2721 /* As the first code in the loop body, generate a block that looks
2722 * like 'if (!condition) break;' as the loop termination condition.
2723 */
2724 ir_rvalue *const not_cond =
2725 new(ctx) ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond,
2726 NULL);
2727
2728 ir_if *const if_stmt = new(ctx) ir_if(not_cond);
2729
2730 ir_jump *const break_stmt =
2731 new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
2732
2733 if_stmt->then_instructions.push_tail(break_stmt);
2734 stmt->body_instructions.push_tail(if_stmt);
2735 }
2736 }
2737 }
2738
2739
2740 ir_rvalue *
2741 ast_iteration_statement::hir(exec_list *instructions,
2742 struct _mesa_glsl_parse_state *state)
2743 {
2744 void *ctx = state;
2745
2746 /* For-loops and while-loops start a new scope, but do-while loops do not.
2747 */
2748 if (mode != ast_do_while)
2749 state->symbols->push_scope();
2750
2751 if (init_statement != NULL)
2752 init_statement->hir(instructions, state);
2753
2754 ir_loop *const stmt = new(ctx) ir_loop();
2755 instructions->push_tail(stmt);
2756
2757 /* Track the current loop and / or switch-statement nesting.
2758 */
2759 ir_instruction *const nesting = state->loop_or_switch_nesting;
2760 ast_iteration_statement *nesting_ast = state->loop_or_switch_nesting_ast;
2761
2762 state->loop_or_switch_nesting = stmt;
2763 state->loop_or_switch_nesting_ast = this;
2764
2765 if (mode != ast_do_while)
2766 condition_to_hir(stmt, state);
2767
2768 if (body != NULL)
2769 body->hir(& stmt->body_instructions, state);
2770
2771 if (rest_expression != NULL)
2772 rest_expression->hir(& stmt->body_instructions, state);
2773
2774 if (mode == ast_do_while)
2775 condition_to_hir(stmt, state);
2776
2777 if (mode != ast_do_while)
2778 state->symbols->pop_scope();
2779
2780 /* Restore previous nesting before returning.
2781 */
2782 state->loop_or_switch_nesting = nesting;
2783 state->loop_or_switch_nesting_ast = nesting_ast;
2784
2785 /* Loops do not have r-values.
2786 */
2787 return NULL;
2788 }
2789
2790
2791 ir_rvalue *
2792 ast_type_specifier::hir(exec_list *instructions,
2793 struct _mesa_glsl_parse_state *state)
2794 {
2795 if (this->structure != NULL)
2796 return this->structure->hir(instructions, state);
2797
2798 return NULL;
2799 }
2800
2801
2802 ir_rvalue *
2803 ast_struct_specifier::hir(exec_list *instructions,
2804 struct _mesa_glsl_parse_state *state)
2805 {
2806 unsigned decl_count = 0;
2807
2808 /* Make an initial pass over the list of structure fields to determine how
2809 * many there are. Each element in this list is an ast_declarator_list.
2810 * This means that we actually need to count the number of elements in the
2811 * 'declarations' list in each of the elements.
2812 */
2813 foreach_list_typed (ast_declarator_list, decl_list, link,
2814 &this->declarations) {
2815 foreach_list_const (decl_ptr, & decl_list->declarations) {
2816 decl_count++;
2817 }
2818 }
2819
2820 /* Allocate storage for the structure fields and process the field
2821 * declarations. As the declarations are processed, try to also convert
2822 * the types to HIR. This ensures that structure definitions embedded in
2823 * other structure definitions are processed.
2824 */
2825 glsl_struct_field *const fields = talloc_array(state, glsl_struct_field,
2826 decl_count);
2827
2828 unsigned i = 0;
2829 foreach_list_typed (ast_declarator_list, decl_list, link,
2830 &this->declarations) {
2831 const char *type_name;
2832
2833 decl_list->type->specifier->hir(instructions, state);
2834
2835 /* Section 10.9 of the GLSL ES 1.00 specification states that
2836 * embedded structure definitions have been removed from the language.
2837 */
2838 if (state->es_shader && decl_list->type->specifier->structure != NULL) {
2839 YYLTYPE loc = this->get_location();
2840 _mesa_glsl_error(&loc, state, "Embedded structure definitions are "
2841 "not allowed in GLSL ES 1.00.");
2842 }
2843
2844 const glsl_type *decl_type =
2845 decl_list->type->specifier->glsl_type(& type_name, state);
2846
2847 foreach_list_typed (ast_declaration, decl, link,
2848 &decl_list->declarations) {
2849 const struct glsl_type *field_type = decl_type;
2850 if (decl->is_array) {
2851 YYLTYPE loc = decl->get_location();
2852 field_type = process_array_type(&loc, decl_type, decl->array_size,
2853 state);
2854 }
2855 fields[i].type = (field_type != NULL)
2856 ? field_type : glsl_type::error_type;
2857 fields[i].name = decl->identifier;
2858 i++;
2859 }
2860 }
2861
2862 assert(i == decl_count);
2863
2864 const glsl_type *t =
2865 glsl_type::get_record_instance(fields, decl_count, this->name);
2866
2867 YYLTYPE loc = this->get_location();
2868 if (!state->symbols->add_type(name, t)) {
2869 _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
2870 } else {
2871
2872 const glsl_type **s = (const glsl_type **)
2873 realloc(state->user_structures,
2874 sizeof(state->user_structures[0]) *
2875 (state->num_user_structures + 1));
2876 if (s != NULL) {
2877 s[state->num_user_structures] = t;
2878 state->user_structures = s;
2879 state->num_user_structures++;
2880 }
2881 }
2882
2883 /* Structure type definitions do not have r-values.
2884 */
2885 return NULL;
2886 }