cb2ef429f703b9ac71cfc7d5fa65578bb9ec3061
[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
64 state->symbols->language_version = state->language_version;
65
66 state->current_function = NULL;
67
68 state->toplevel_ir = instructions;
69
70 /* Section 4.2 of the GLSL 1.20 specification states:
71 * "The built-in functions are scoped in a scope outside the global scope
72 * users declare global variables in. That is, a shader's global scope,
73 * available for user-defined functions and global variables, is nested
74 * inside the scope containing the built-in functions."
75 *
76 * Since built-in functions like ftransform() access built-in variables,
77 * it follows that those must be in the outer scope as well.
78 *
79 * We push scope here to create this nesting effect...but don't pop.
80 * This way, a shader's globals are still in the symbol table for use
81 * by the linker.
82 */
83 state->symbols->push_scope();
84
85 foreach_list_typed (ast_node, ast, link, & state->translation_unit)
86 ast->hir(instructions, state);
87
88 detect_recursion_unlinked(state, instructions);
89
90 state->toplevel_ir = NULL;
91 }
92
93
94 /**
95 * If a conversion is available, convert one operand to a different type
96 *
97 * The \c from \c ir_rvalue is converted "in place".
98 *
99 * \param to Type that the operand it to be converted to
100 * \param from Operand that is being converted
101 * \param state GLSL compiler state
102 *
103 * \return
104 * If a conversion is possible (or unnecessary), \c true is returned.
105 * Otherwise \c false is returned.
106 */
107 bool
108 apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
109 struct _mesa_glsl_parse_state *state)
110 {
111 void *ctx = state;
112 if (to->base_type == from->type->base_type)
113 return true;
114
115 /* This conversion was added in GLSL 1.20. If the compilation mode is
116 * GLSL 1.10, the conversion is skipped.
117 */
118 if (state->language_version < 120)
119 return false;
120
121 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
122 *
123 * "There are no implicit array or structure conversions. For
124 * example, an array of int cannot be implicitly converted to an
125 * array of float. There are no implicit conversions between
126 * signed and unsigned integers."
127 */
128 /* FINISHME: The above comment is partially a lie. There is int/uint
129 * FINISHME: conversion for immediate constants.
130 */
131 if (!to->is_float() || !from->type->is_numeric())
132 return false;
133
134 /* Convert to a floating point type with the same number of components
135 * as the original type - i.e. int to float, not int to vec4.
136 */
137 to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements,
138 from->type->matrix_columns);
139
140 switch (from->type->base_type) {
141 case GLSL_TYPE_INT:
142 from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL);
143 break;
144 case GLSL_TYPE_UINT:
145 from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL);
146 break;
147 case GLSL_TYPE_BOOL:
148 from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL);
149 break;
150 default:
151 assert(0);
152 }
153
154 return true;
155 }
156
157
158 static const struct glsl_type *
159 arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
160 bool multiply,
161 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
162 {
163 const glsl_type *type_a = value_a->type;
164 const glsl_type *type_b = value_b->type;
165
166 /* From GLSL 1.50 spec, page 56:
167 *
168 * "The arithmetic binary operators add (+), subtract (-),
169 * multiply (*), and divide (/) operate on integer and
170 * floating-point scalars, vectors, and matrices."
171 */
172 if (!type_a->is_numeric() || !type_b->is_numeric()) {
173 _mesa_glsl_error(loc, state,
174 "Operands to arithmetic operators must be numeric");
175 return glsl_type::error_type;
176 }
177
178
179 /* "If one operand is floating-point based and the other is
180 * not, then the conversions from Section 4.1.10 "Implicit
181 * Conversions" are applied to the non-floating-point-based operand."
182 */
183 if (!apply_implicit_conversion(type_a, value_b, state)
184 && !apply_implicit_conversion(type_b, value_a, state)) {
185 _mesa_glsl_error(loc, state,
186 "Could not implicitly convert operands to "
187 "arithmetic operator");
188 return glsl_type::error_type;
189 }
190 type_a = value_a->type;
191 type_b = value_b->type;
192
193 /* "If the operands are integer types, they must both be signed or
194 * both be unsigned."
195 *
196 * From this rule and the preceeding conversion it can be inferred that
197 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
198 * The is_numeric check above already filtered out the case where either
199 * type is not one of these, so now the base types need only be tested for
200 * equality.
201 */
202 if (type_a->base_type != type_b->base_type) {
203 _mesa_glsl_error(loc, state,
204 "base type mismatch for arithmetic operator");
205 return glsl_type::error_type;
206 }
207
208 /* "All arithmetic binary operators result in the same fundamental type
209 * (signed integer, unsigned integer, or floating-point) as the
210 * operands they operate on, after operand type conversion. After
211 * conversion, the following cases are valid
212 *
213 * * The two operands are scalars. In this case the operation is
214 * applied, resulting in a scalar."
215 */
216 if (type_a->is_scalar() && type_b->is_scalar())
217 return type_a;
218
219 /* "* One operand is a scalar, and the other is a vector or matrix.
220 * In this case, the scalar operation is applied independently to each
221 * component of the vector or matrix, resulting in the same size
222 * vector or matrix."
223 */
224 if (type_a->is_scalar()) {
225 if (!type_b->is_scalar())
226 return type_b;
227 } else if (type_b->is_scalar()) {
228 return type_a;
229 }
230
231 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
232 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
233 * handled.
234 */
235 assert(!type_a->is_scalar());
236 assert(!type_b->is_scalar());
237
238 /* "* The two operands are vectors of the same size. In this case, the
239 * operation is done component-wise resulting in the same size
240 * vector."
241 */
242 if (type_a->is_vector() && type_b->is_vector()) {
243 if (type_a == type_b) {
244 return type_a;
245 } else {
246 _mesa_glsl_error(loc, state,
247 "vector size mismatch for arithmetic operator");
248 return glsl_type::error_type;
249 }
250 }
251
252 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
253 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
254 * <vector, vector> have been handled. At least one of the operands must
255 * be matrix. Further, since there are no integer matrix types, the base
256 * type of both operands must be float.
257 */
258 assert(type_a->is_matrix() || type_b->is_matrix());
259 assert(type_a->base_type == GLSL_TYPE_FLOAT);
260 assert(type_b->base_type == GLSL_TYPE_FLOAT);
261
262 /* "* The operator is add (+), subtract (-), or divide (/), and the
263 * operands are matrices with the same number of rows and the same
264 * number of columns. In this case, the operation is done component-
265 * wise resulting in the same size matrix."
266 * * The operator is multiply (*), where both operands are matrices or
267 * one operand is a vector and the other a matrix. A right vector
268 * operand is treated as a column vector and a left vector operand as a
269 * row vector. In all these cases, it is required that the number of
270 * columns of the left operand is equal to the number of rows of the
271 * right operand. Then, the multiply (*) operation does a linear
272 * algebraic multiply, yielding an object that has the same number of
273 * rows as the left operand and the same number of columns as the right
274 * operand. Section 5.10 "Vector and Matrix Operations" explains in
275 * more detail how vectors and matrices are operated on."
276 */
277 if (! multiply) {
278 if (type_a == type_b)
279 return type_a;
280 } else {
281 if (type_a->is_matrix() && type_b->is_matrix()) {
282 /* Matrix multiply. The columns of A must match the rows of B. Given
283 * the other previously tested constraints, this means the vector type
284 * of a row from A must be the same as the vector type of a column from
285 * B.
286 */
287 if (type_a->row_type() == type_b->column_type()) {
288 /* The resulting matrix has the number of columns of matrix B and
289 * the number of rows of matrix A. We get the row count of A by
290 * looking at the size of a vector that makes up a column. The
291 * transpose (size of a row) is done for B.
292 */
293 const glsl_type *const type =
294 glsl_type::get_instance(type_a->base_type,
295 type_a->column_type()->vector_elements,
296 type_b->row_type()->vector_elements);
297 assert(type != glsl_type::error_type);
298
299 return type;
300 }
301 } else if (type_a->is_matrix()) {
302 /* A is a matrix and B is a column vector. Columns of A must match
303 * rows of B. Given the other previously tested constraints, this
304 * means the vector type of a row from A must be the same as the
305 * vector the type of B.
306 */
307 if (type_a->row_type() == type_b) {
308 /* The resulting vector has a number of elements equal to
309 * the number of rows of matrix A. */
310 const glsl_type *const type =
311 glsl_type::get_instance(type_a->base_type,
312 type_a->column_type()->vector_elements,
313 1);
314 assert(type != glsl_type::error_type);
315
316 return type;
317 }
318 } else {
319 assert(type_b->is_matrix());
320
321 /* A is a row vector and B is a matrix. Columns of A must match rows
322 * of B. Given the other previously tested constraints, this means
323 * the type of A must be the same as the vector type of a column from
324 * B.
325 */
326 if (type_a == type_b->column_type()) {
327 /* The resulting vector has a number of elements equal to
328 * the number of columns of matrix B. */
329 const glsl_type *const type =
330 glsl_type::get_instance(type_a->base_type,
331 type_b->row_type()->vector_elements,
332 1);
333 assert(type != glsl_type::error_type);
334
335 return type;
336 }
337 }
338
339 _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
340 return glsl_type::error_type;
341 }
342
343
344 /* "All other cases are illegal."
345 */
346 _mesa_glsl_error(loc, state, "type mismatch");
347 return glsl_type::error_type;
348 }
349
350
351 static const struct glsl_type *
352 unary_arithmetic_result_type(const struct glsl_type *type,
353 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
354 {
355 /* From GLSL 1.50 spec, page 57:
356 *
357 * "The arithmetic unary operators negate (-), post- and pre-increment
358 * and decrement (-- and ++) operate on integer or floating-point
359 * values (including vectors and matrices). All unary operators work
360 * component-wise on their operands. These result with the same type
361 * they operated on."
362 */
363 if (!type->is_numeric()) {
364 _mesa_glsl_error(loc, state,
365 "Operands to arithmetic operators must be numeric");
366 return glsl_type::error_type;
367 }
368
369 return type;
370 }
371
372 /**
373 * \brief Return the result type of a bit-logic operation.
374 *
375 * If the given types to the bit-logic operator are invalid, return
376 * glsl_type::error_type.
377 *
378 * \param type_a Type of LHS of bit-logic op
379 * \param type_b Type of RHS of bit-logic op
380 */
381 static const struct glsl_type *
382 bit_logic_result_type(const struct glsl_type *type_a,
383 const struct glsl_type *type_b,
384 ast_operators op,
385 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
386 {
387 if (state->language_version < 130) {
388 _mesa_glsl_error(loc, state, "bit operations require GLSL 1.30");
389 return glsl_type::error_type;
390 }
391
392 /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
393 *
394 * "The bitwise operators and (&), exclusive-or (^), and inclusive-or
395 * (|). The operands must be of type signed or unsigned integers or
396 * integer vectors."
397 */
398 if (!type_a->is_integer()) {
399 _mesa_glsl_error(loc, state, "LHS of `%s' must be an integer",
400 ast_expression::operator_string(op));
401 return glsl_type::error_type;
402 }
403 if (!type_b->is_integer()) {
404 _mesa_glsl_error(loc, state, "RHS of `%s' must be an integer",
405 ast_expression::operator_string(op));
406 return glsl_type::error_type;
407 }
408
409 /* "The fundamental types of the operands (signed or unsigned) must
410 * match,"
411 */
412 if (type_a->base_type != type_b->base_type) {
413 _mesa_glsl_error(loc, state, "operands of `%s' must have the same "
414 "base type", ast_expression::operator_string(op));
415 return glsl_type::error_type;
416 }
417
418 /* "The operands cannot be vectors of differing size." */
419 if (type_a->is_vector() &&
420 type_b->is_vector() &&
421 type_a->vector_elements != type_b->vector_elements) {
422 _mesa_glsl_error(loc, state, "operands of `%s' cannot be vectors of "
423 "different sizes", ast_expression::operator_string(op));
424 return glsl_type::error_type;
425 }
426
427 /* "If one operand is a scalar and the other a vector, the scalar is
428 * applied component-wise to the vector, resulting in the same type as
429 * the vector. The fundamental types of the operands [...] will be the
430 * resulting fundamental type."
431 */
432 if (type_a->is_scalar())
433 return type_b;
434 else
435 return type_a;
436 }
437
438 static const struct glsl_type *
439 modulus_result_type(const struct glsl_type *type_a,
440 const struct glsl_type *type_b,
441 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
442 {
443 if (state->language_version < 130) {
444 _mesa_glsl_error(loc, state,
445 "operator '%%' is reserved in %s",
446 state->version_string);
447 return glsl_type::error_type;
448 }
449
450 /* From GLSL 1.50 spec, page 56:
451 * "The operator modulus (%) operates on signed or unsigned integers or
452 * integer vectors. The operand types must both be signed or both be
453 * unsigned."
454 */
455 if (!type_a->is_integer()) {
456 _mesa_glsl_error(loc, state, "LHS of operator %% must be an integer.");
457 return glsl_type::error_type;
458 }
459 if (!type_b->is_integer()) {
460 _mesa_glsl_error(loc, state, "RHS of operator %% must be an integer.");
461 return glsl_type::error_type;
462 }
463 if (type_a->base_type != type_b->base_type) {
464 _mesa_glsl_error(loc, state,
465 "operands of %% must have the same base type");
466 return glsl_type::error_type;
467 }
468
469 /* "The operands cannot be vectors of differing size. If one operand is
470 * a scalar and the other vector, then the scalar is applied component-
471 * wise to the vector, resulting in the same type as the vector. If both
472 * are vectors of the same size, the result is computed component-wise."
473 */
474 if (type_a->is_vector()) {
475 if (!type_b->is_vector()
476 || (type_a->vector_elements == type_b->vector_elements))
477 return type_a;
478 } else
479 return type_b;
480
481 /* "The operator modulus (%) is not defined for any other data types
482 * (non-integer types)."
483 */
484 _mesa_glsl_error(loc, state, "type mismatch");
485 return glsl_type::error_type;
486 }
487
488
489 static const struct glsl_type *
490 relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
491 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
492 {
493 const glsl_type *type_a = value_a->type;
494 const glsl_type *type_b = value_b->type;
495
496 /* From GLSL 1.50 spec, page 56:
497 * "The relational operators greater than (>), less than (<), greater
498 * than or equal (>=), and less than or equal (<=) operate only on
499 * scalar integer and scalar floating-point expressions."
500 */
501 if (!type_a->is_numeric()
502 || !type_b->is_numeric()
503 || !type_a->is_scalar()
504 || !type_b->is_scalar()) {
505 _mesa_glsl_error(loc, state,
506 "Operands to relational operators must be scalar and "
507 "numeric");
508 return glsl_type::error_type;
509 }
510
511 /* "Either the operands' types must match, or the conversions from
512 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
513 * operand, after which the types must match."
514 */
515 if (!apply_implicit_conversion(type_a, value_b, state)
516 && !apply_implicit_conversion(type_b, value_a, state)) {
517 _mesa_glsl_error(loc, state,
518 "Could not implicitly convert operands to "
519 "relational operator");
520 return glsl_type::error_type;
521 }
522 type_a = value_a->type;
523 type_b = value_b->type;
524
525 if (type_a->base_type != type_b->base_type) {
526 _mesa_glsl_error(loc, state, "base type mismatch");
527 return glsl_type::error_type;
528 }
529
530 /* "The result is scalar Boolean."
531 */
532 return glsl_type::bool_type;
533 }
534
535 /**
536 * \brief Return the result type of a bit-shift operation.
537 *
538 * If the given types to the bit-shift operator are invalid, return
539 * glsl_type::error_type.
540 *
541 * \param type_a Type of LHS of bit-shift op
542 * \param type_b Type of RHS of bit-shift op
543 */
544 static const struct glsl_type *
545 shift_result_type(const struct glsl_type *type_a,
546 const struct glsl_type *type_b,
547 ast_operators op,
548 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
549 {
550 if (state->language_version < 130) {
551 _mesa_glsl_error(loc, state, "bit operations require GLSL 1.30");
552 return glsl_type::error_type;
553 }
554
555 /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
556 *
557 * "The shift operators (<<) and (>>). For both operators, the operands
558 * must be signed or unsigned integers or integer vectors. One operand
559 * can be signed while the other is unsigned."
560 */
561 if (!type_a->is_integer()) {
562 _mesa_glsl_error(loc, state, "LHS of operator %s must be an integer or "
563 "integer vector", ast_expression::operator_string(op));
564 return glsl_type::error_type;
565
566 }
567 if (!type_b->is_integer()) {
568 _mesa_glsl_error(loc, state, "RHS of operator %s must be an integer or "
569 "integer vector", ast_expression::operator_string(op));
570 return glsl_type::error_type;
571 }
572
573 /* "If the first operand is a scalar, the second operand has to be
574 * a scalar as well."
575 */
576 if (type_a->is_scalar() && !type_b->is_scalar()) {
577 _mesa_glsl_error(loc, state, "If the first operand of %s is scalar, the "
578 "second must be scalar as well",
579 ast_expression::operator_string(op));
580 return glsl_type::error_type;
581 }
582
583 /* If both operands are vectors, check that they have same number of
584 * elements.
585 */
586 if (type_a->is_vector() &&
587 type_b->is_vector() &&
588 type_a->vector_elements != type_b->vector_elements) {
589 _mesa_glsl_error(loc, state, "Vector operands to operator %s must "
590 "have same number of elements",
591 ast_expression::operator_string(op));
592 return glsl_type::error_type;
593 }
594
595 /* "In all cases, the resulting type will be the same type as the left
596 * operand."
597 */
598 return type_a;
599 }
600
601 /**
602 * Validates that a value can be assigned to a location with a specified type
603 *
604 * Validates that \c rhs can be assigned to some location. If the types are
605 * not an exact match but an automatic conversion is possible, \c rhs will be
606 * converted.
607 *
608 * \return
609 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
610 * Otherwise the actual RHS to be assigned will be returned. This may be
611 * \c rhs, or it may be \c rhs after some type conversion.
612 *
613 * \note
614 * In addition to being used for assignments, this function is used to
615 * type-check return values.
616 */
617 ir_rvalue *
618 validate_assignment(struct _mesa_glsl_parse_state *state,
619 const glsl_type *lhs_type, ir_rvalue *rhs,
620 bool is_initializer)
621 {
622 /* If there is already some error in the RHS, just return it. Anything
623 * else will lead to an avalanche of error message back to the user.
624 */
625 if (rhs->type->is_error())
626 return rhs;
627
628 /* If the types are identical, the assignment can trivially proceed.
629 */
630 if (rhs->type == lhs_type)
631 return rhs;
632
633 /* If the array element types are the same and the size of the LHS is zero,
634 * the assignment is okay for initializers embedded in variable
635 * declarations.
636 *
637 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
638 * is handled by ir_dereference::is_lvalue.
639 */
640 if (is_initializer && lhs_type->is_array() && rhs->type->is_array()
641 && (lhs_type->element_type() == rhs->type->element_type())
642 && (lhs_type->array_size() == 0)) {
643 return rhs;
644 }
645
646 /* Check for implicit conversion in GLSL 1.20 */
647 if (apply_implicit_conversion(lhs_type, rhs, state)) {
648 if (rhs->type == lhs_type)
649 return rhs;
650 }
651
652 return NULL;
653 }
654
655 static void
656 mark_whole_array_access(ir_rvalue *access)
657 {
658 ir_dereference_variable *deref = access->as_dereference_variable();
659
660 if (deref && deref->var) {
661 deref->var->max_array_access = deref->type->length - 1;
662 }
663 }
664
665 ir_rvalue *
666 do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
667 const char *non_lvalue_description,
668 ir_rvalue *lhs, ir_rvalue *rhs, bool is_initializer,
669 YYLTYPE lhs_loc)
670 {
671 void *ctx = state;
672 bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
673
674 if (!error_emitted) {
675 if (non_lvalue_description != NULL) {
676 _mesa_glsl_error(&lhs_loc, state,
677 "assignment to %s",
678 non_lvalue_description);
679 error_emitted = true;
680 } else if (lhs->variable_referenced() != NULL
681 && lhs->variable_referenced()->read_only) {
682 _mesa_glsl_error(&lhs_loc, state,
683 "assignment to read-only variable '%s'",
684 lhs->variable_referenced()->name);
685 error_emitted = true;
686
687 } else if (state->language_version <= 110 && lhs->type->is_array()) {
688 /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
689 *
690 * "Other binary or unary expressions, non-dereferenced
691 * arrays, function names, swizzles with repeated fields,
692 * and constants cannot be l-values."
693 */
694 _mesa_glsl_error(&lhs_loc, state, "whole array assignment is not "
695 "allowed in GLSL 1.10 or GLSL ES 1.00.");
696 error_emitted = true;
697 } else if (!lhs->is_lvalue()) {
698 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
699 error_emitted = true;
700 }
701 }
702
703 ir_rvalue *new_rhs =
704 validate_assignment(state, lhs->type, rhs, is_initializer);
705 if (new_rhs == NULL) {
706 _mesa_glsl_error(& lhs_loc, state, "type mismatch");
707 } else {
708 rhs = new_rhs;
709
710 /* If the LHS array was not declared with a size, it takes it size from
711 * the RHS. If the LHS is an l-value and a whole array, it must be a
712 * dereference of a variable. Any other case would require that the LHS
713 * is either not an l-value or not a whole array.
714 */
715 if (lhs->type->array_size() == 0) {
716 ir_dereference *const d = lhs->as_dereference();
717
718 assert(d != NULL);
719
720 ir_variable *const var = d->variable_referenced();
721
722 assert(var != NULL);
723
724 if (var->max_array_access >= unsigned(rhs->type->array_size())) {
725 /* FINISHME: This should actually log the location of the RHS. */
726 _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
727 "previous access",
728 var->max_array_access);
729 }
730
731 var->type = glsl_type::get_array_instance(lhs->type->element_type(),
732 rhs->type->array_size());
733 d->type = var->type;
734 }
735 mark_whole_array_access(rhs);
736 mark_whole_array_access(lhs);
737 }
738
739 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
740 * but not post_inc) need the converted assigned value as an rvalue
741 * to handle things like:
742 *
743 * i = j += 1;
744 *
745 * So we always just store the computed value being assigned to a
746 * temporary and return a deref of that temporary. If the rvalue
747 * ends up not being used, the temp will get copy-propagated out.
748 */
749 ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp",
750 ir_var_temporary);
751 ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var);
752 instructions->push_tail(var);
753 instructions->push_tail(new(ctx) ir_assignment(deref_var,
754 rhs,
755 NULL));
756 deref_var = new(ctx) ir_dereference_variable(var);
757
758 if (!error_emitted)
759 instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var, NULL));
760
761 return new(ctx) ir_dereference_variable(var);
762 }
763
764 static ir_rvalue *
765 get_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
766 {
767 void *ctx = ralloc_parent(lvalue);
768 ir_variable *var;
769
770 var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
771 ir_var_temporary);
772 instructions->push_tail(var);
773 var->mode = ir_var_auto;
774
775 instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
776 lvalue, NULL));
777
778 /* Once we've created this temporary, mark it read only so it's no
779 * longer considered an lvalue.
780 */
781 var->read_only = true;
782
783 return new(ctx) ir_dereference_variable(var);
784 }
785
786
787 ir_rvalue *
788 ast_node::hir(exec_list *instructions,
789 struct _mesa_glsl_parse_state *state)
790 {
791 (void) instructions;
792 (void) state;
793
794 return NULL;
795 }
796
797 static ir_rvalue *
798 do_comparison(void *mem_ctx, int operation, ir_rvalue *op0, ir_rvalue *op1)
799 {
800 int join_op;
801 ir_rvalue *cmp = NULL;
802
803 if (operation == ir_binop_all_equal)
804 join_op = ir_binop_logic_and;
805 else
806 join_op = ir_binop_logic_or;
807
808 switch (op0->type->base_type) {
809 case GLSL_TYPE_FLOAT:
810 case GLSL_TYPE_UINT:
811 case GLSL_TYPE_INT:
812 case GLSL_TYPE_BOOL:
813 return new(mem_ctx) ir_expression(operation, op0, op1);
814
815 case GLSL_TYPE_ARRAY: {
816 for (unsigned int i = 0; i < op0->type->length; i++) {
817 ir_rvalue *e0, *e1, *result;
818
819 e0 = new(mem_ctx) ir_dereference_array(op0->clone(mem_ctx, NULL),
820 new(mem_ctx) ir_constant(i));
821 e1 = new(mem_ctx) ir_dereference_array(op1->clone(mem_ctx, NULL),
822 new(mem_ctx) ir_constant(i));
823 result = do_comparison(mem_ctx, operation, e0, e1);
824
825 if (cmp) {
826 cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
827 } else {
828 cmp = result;
829 }
830 }
831
832 mark_whole_array_access(op0);
833 mark_whole_array_access(op1);
834 break;
835 }
836
837 case GLSL_TYPE_STRUCT: {
838 for (unsigned int i = 0; i < op0->type->length; i++) {
839 ir_rvalue *e0, *e1, *result;
840 const char *field_name = op0->type->fields.structure[i].name;
841
842 e0 = new(mem_ctx) ir_dereference_record(op0->clone(mem_ctx, NULL),
843 field_name);
844 e1 = new(mem_ctx) ir_dereference_record(op1->clone(mem_ctx, NULL),
845 field_name);
846 result = do_comparison(mem_ctx, operation, e0, e1);
847
848 if (cmp) {
849 cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
850 } else {
851 cmp = result;
852 }
853 }
854 break;
855 }
856
857 case GLSL_TYPE_ERROR:
858 case GLSL_TYPE_VOID:
859 case GLSL_TYPE_SAMPLER:
860 /* I assume a comparison of a struct containing a sampler just
861 * ignores the sampler present in the type.
862 */
863 break;
864
865 default:
866 assert(!"Should not get here.");
867 break;
868 }
869
870 if (cmp == NULL)
871 cmp = new(mem_ctx) ir_constant(true);
872
873 return cmp;
874 }
875
876 /* For logical operations, we want to ensure that the operands are
877 * scalar booleans. If it isn't, emit an error and return a constant
878 * boolean to avoid triggering cascading error messages.
879 */
880 ir_rvalue *
881 get_scalar_boolean_operand(exec_list *instructions,
882 struct _mesa_glsl_parse_state *state,
883 ast_expression *parent_expr,
884 int operand,
885 const char *operand_name,
886 bool *error_emitted)
887 {
888 ast_expression *expr = parent_expr->subexpressions[operand];
889 void *ctx = state;
890 ir_rvalue *val = expr->hir(instructions, state);
891
892 if (val->type->is_boolean() && val->type->is_scalar())
893 return val;
894
895 if (!*error_emitted) {
896 YYLTYPE loc = expr->get_location();
897 _mesa_glsl_error(&loc, state, "%s of `%s' must be scalar boolean",
898 operand_name,
899 parent_expr->operator_string(parent_expr->oper));
900 *error_emitted = true;
901 }
902
903 return new(ctx) ir_constant(true);
904 }
905
906 /**
907 * If name refers to a builtin array whose maximum allowed size is less than
908 * size, report an error and return true. Otherwise return false.
909 */
910 static bool
911 check_builtin_array_max_size(const char *name, unsigned size,
912 YYLTYPE loc, struct _mesa_glsl_parse_state *state)
913 {
914 if ((strcmp("gl_TexCoord", name) == 0)
915 && (size > state->Const.MaxTextureCoords)) {
916 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
917 *
918 * "The size [of gl_TexCoord] can be at most
919 * gl_MaxTextureCoords."
920 */
921 _mesa_glsl_error(&loc, state, "`gl_TexCoord' array size cannot "
922 "be larger than gl_MaxTextureCoords (%u)\n",
923 state->Const.MaxTextureCoords);
924 return true;
925 } else if (strcmp("gl_ClipDistance", name) == 0
926 && size > state->Const.MaxClipPlanes) {
927 /* From section 7.1 (Vertex Shader Special Variables) of the
928 * GLSL 1.30 spec:
929 *
930 * "The gl_ClipDistance array is predeclared as unsized and
931 * must be sized by the shader either redeclaring it with a
932 * size or indexing it only with integral constant
933 * expressions. ... The size can be at most
934 * gl_MaxClipDistances."
935 */
936 _mesa_glsl_error(&loc, state, "`gl_ClipDistance' array size cannot "
937 "be larger than gl_MaxClipDistances (%u)\n",
938 state->Const.MaxClipPlanes);
939 return true;
940 }
941 return false;
942 }
943
944 /**
945 * Create the constant 1, of a which is appropriate for incrementing and
946 * decrementing values of the given GLSL type. For example, if type is vec4,
947 * this creates a constant value of 1.0 having type float.
948 *
949 * If the given type is invalid for increment and decrement operators, return
950 * a floating point 1--the error will be detected later.
951 */
952 static ir_rvalue *
953 constant_one_for_inc_dec(void *ctx, const glsl_type *type)
954 {
955 switch (type->base_type) {
956 case GLSL_TYPE_UINT:
957 return new(ctx) ir_constant((unsigned) 1);
958 case GLSL_TYPE_INT:
959 return new(ctx) ir_constant(1);
960 default:
961 case GLSL_TYPE_FLOAT:
962 return new(ctx) ir_constant(1.0f);
963 }
964 }
965
966 ir_rvalue *
967 ast_expression::hir(exec_list *instructions,
968 struct _mesa_glsl_parse_state *state)
969 {
970 void *ctx = state;
971 static const int operations[AST_NUM_OPERATORS] = {
972 -1, /* ast_assign doesn't convert to ir_expression. */
973 -1, /* ast_plus doesn't convert to ir_expression. */
974 ir_unop_neg,
975 ir_binop_add,
976 ir_binop_sub,
977 ir_binop_mul,
978 ir_binop_div,
979 ir_binop_mod,
980 ir_binop_lshift,
981 ir_binop_rshift,
982 ir_binop_less,
983 ir_binop_greater,
984 ir_binop_lequal,
985 ir_binop_gequal,
986 ir_binop_all_equal,
987 ir_binop_any_nequal,
988 ir_binop_bit_and,
989 ir_binop_bit_xor,
990 ir_binop_bit_or,
991 ir_unop_bit_not,
992 ir_binop_logic_and,
993 ir_binop_logic_xor,
994 ir_binop_logic_or,
995 ir_unop_logic_not,
996
997 /* Note: The following block of expression types actually convert
998 * to multiple IR instructions.
999 */
1000 ir_binop_mul, /* ast_mul_assign */
1001 ir_binop_div, /* ast_div_assign */
1002 ir_binop_mod, /* ast_mod_assign */
1003 ir_binop_add, /* ast_add_assign */
1004 ir_binop_sub, /* ast_sub_assign */
1005 ir_binop_lshift, /* ast_ls_assign */
1006 ir_binop_rshift, /* ast_rs_assign */
1007 ir_binop_bit_and, /* ast_and_assign */
1008 ir_binop_bit_xor, /* ast_xor_assign */
1009 ir_binop_bit_or, /* ast_or_assign */
1010
1011 -1, /* ast_conditional doesn't convert to ir_expression. */
1012 ir_binop_add, /* ast_pre_inc. */
1013 ir_binop_sub, /* ast_pre_dec. */
1014 ir_binop_add, /* ast_post_inc. */
1015 ir_binop_sub, /* ast_post_dec. */
1016 -1, /* ast_field_selection doesn't conv to ir_expression. */
1017 -1, /* ast_array_index doesn't convert to ir_expression. */
1018 -1, /* ast_function_call doesn't conv to ir_expression. */
1019 -1, /* ast_identifier doesn't convert to ir_expression. */
1020 -1, /* ast_int_constant doesn't convert to ir_expression. */
1021 -1, /* ast_uint_constant doesn't conv to ir_expression. */
1022 -1, /* ast_float_constant doesn't conv to ir_expression. */
1023 -1, /* ast_bool_constant doesn't conv to ir_expression. */
1024 -1, /* ast_sequence doesn't convert to ir_expression. */
1025 };
1026 ir_rvalue *result = NULL;
1027 ir_rvalue *op[3];
1028 const struct glsl_type *type; /* a temporary variable for switch cases */
1029 bool error_emitted = false;
1030 YYLTYPE loc;
1031
1032 loc = this->get_location();
1033
1034 switch (this->oper) {
1035 case ast_assign: {
1036 op[0] = this->subexpressions[0]->hir(instructions, state);
1037 op[1] = this->subexpressions[1]->hir(instructions, state);
1038
1039 result = do_assignment(instructions, state,
1040 this->subexpressions[0]->non_lvalue_description,
1041 op[0], op[1], false,
1042 this->subexpressions[0]->get_location());
1043 error_emitted = result->type->is_error();
1044 break;
1045 }
1046
1047 case ast_plus:
1048 op[0] = this->subexpressions[0]->hir(instructions, state);
1049
1050 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
1051
1052 error_emitted = type->is_error();
1053
1054 result = op[0];
1055 break;
1056
1057 case ast_neg:
1058 op[0] = this->subexpressions[0]->hir(instructions, state);
1059
1060 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
1061
1062 error_emitted = type->is_error();
1063
1064 result = new(ctx) ir_expression(operations[this->oper], type,
1065 op[0], NULL);
1066 break;
1067
1068 case ast_add:
1069 case ast_sub:
1070 case ast_mul:
1071 case ast_div:
1072 op[0] = this->subexpressions[0]->hir(instructions, state);
1073 op[1] = this->subexpressions[1]->hir(instructions, state);
1074
1075 type = arithmetic_result_type(op[0], op[1],
1076 (this->oper == ast_mul),
1077 state, & loc);
1078 error_emitted = type->is_error();
1079
1080 result = new(ctx) ir_expression(operations[this->oper], type,
1081 op[0], op[1]);
1082 break;
1083
1084 case ast_mod:
1085 op[0] = this->subexpressions[0]->hir(instructions, state);
1086 op[1] = this->subexpressions[1]->hir(instructions, state);
1087
1088 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
1089
1090 assert(operations[this->oper] == ir_binop_mod);
1091
1092 result = new(ctx) ir_expression(operations[this->oper], type,
1093 op[0], op[1]);
1094 error_emitted = type->is_error();
1095 break;
1096
1097 case ast_lshift:
1098 case ast_rshift:
1099 if (state->language_version < 130) {
1100 _mesa_glsl_error(&loc, state, "operator %s requires GLSL 1.30",
1101 operator_string(this->oper));
1102 error_emitted = true;
1103 }
1104
1105 op[0] = this->subexpressions[0]->hir(instructions, state);
1106 op[1] = this->subexpressions[1]->hir(instructions, state);
1107 type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1108 &loc);
1109 result = new(ctx) ir_expression(operations[this->oper], type,
1110 op[0], op[1]);
1111 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1112 break;
1113
1114 case ast_less:
1115 case ast_greater:
1116 case ast_lequal:
1117 case ast_gequal:
1118 op[0] = this->subexpressions[0]->hir(instructions, state);
1119 op[1] = this->subexpressions[1]->hir(instructions, state);
1120
1121 type = relational_result_type(op[0], op[1], state, & loc);
1122
1123 /* The relational operators must either generate an error or result
1124 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1125 */
1126 assert(type->is_error()
1127 || ((type->base_type == GLSL_TYPE_BOOL)
1128 && type->is_scalar()));
1129
1130 result = new(ctx) ir_expression(operations[this->oper], type,
1131 op[0], op[1]);
1132 error_emitted = type->is_error();
1133 break;
1134
1135 case ast_nequal:
1136 case ast_equal:
1137 op[0] = this->subexpressions[0]->hir(instructions, state);
1138 op[1] = this->subexpressions[1]->hir(instructions, state);
1139
1140 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1141 *
1142 * "The equality operators equal (==), and not equal (!=)
1143 * operate on all types. They result in a scalar Boolean. If
1144 * the operand types do not match, then there must be a
1145 * conversion from Section 4.1.10 "Implicit Conversions"
1146 * applied to one operand that can make them match, in which
1147 * case this conversion is done."
1148 */
1149 if ((!apply_implicit_conversion(op[0]->type, op[1], state)
1150 && !apply_implicit_conversion(op[1]->type, op[0], state))
1151 || (op[0]->type != op[1]->type)) {
1152 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
1153 "type", (this->oper == ast_equal) ? "==" : "!=");
1154 error_emitted = true;
1155 } else if ((state->language_version <= 110)
1156 && (op[0]->type->is_array() || op[1]->type->is_array())) {
1157 _mesa_glsl_error(& loc, state, "array comparisons forbidden in "
1158 "GLSL 1.10");
1159 error_emitted = true;
1160 }
1161
1162 if (error_emitted) {
1163 result = new(ctx) ir_constant(false);
1164 } else {
1165 result = do_comparison(ctx, operations[this->oper], op[0], op[1]);
1166 assert(result->type == glsl_type::bool_type);
1167 }
1168 break;
1169
1170 case ast_bit_and:
1171 case ast_bit_xor:
1172 case ast_bit_or:
1173 op[0] = this->subexpressions[0]->hir(instructions, state);
1174 op[1] = this->subexpressions[1]->hir(instructions, state);
1175 type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
1176 state, &loc);
1177 result = new(ctx) ir_expression(operations[this->oper], type,
1178 op[0], op[1]);
1179 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1180 break;
1181
1182 case ast_bit_not:
1183 op[0] = this->subexpressions[0]->hir(instructions, state);
1184
1185 if (state->language_version < 130) {
1186 _mesa_glsl_error(&loc, state, "bit-wise operations require GLSL 1.30");
1187 error_emitted = true;
1188 }
1189
1190 if (!op[0]->type->is_integer()) {
1191 _mesa_glsl_error(&loc, state, "operand of `~' must be an integer");
1192 error_emitted = true;
1193 }
1194
1195 type = error_emitted ? glsl_type::error_type : op[0]->type;
1196 result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL);
1197 break;
1198
1199 case ast_logic_and: {
1200 exec_list rhs_instructions;
1201 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1202 "LHS", &error_emitted);
1203 op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
1204 "RHS", &error_emitted);
1205
1206 ir_constant *op0_const = op[0]->constant_expression_value();
1207 if (op0_const) {
1208 if (op0_const->value.b[0]) {
1209 instructions->append_list(&rhs_instructions);
1210 result = op[1];
1211 } else {
1212 result = op0_const;
1213 }
1214 type = glsl_type::bool_type;
1215 } else {
1216 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
1217 "and_tmp",
1218 ir_var_temporary);
1219 instructions->push_tail(tmp);
1220
1221 ir_if *const stmt = new(ctx) ir_if(op[0]);
1222 instructions->push_tail(stmt);
1223
1224 stmt->then_instructions.append_list(&rhs_instructions);
1225 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
1226 ir_assignment *const then_assign =
1227 new(ctx) ir_assignment(then_deref, op[1], NULL);
1228 stmt->then_instructions.push_tail(then_assign);
1229
1230 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
1231 ir_assignment *const else_assign =
1232 new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false), NULL);
1233 stmt->else_instructions.push_tail(else_assign);
1234
1235 result = new(ctx) ir_dereference_variable(tmp);
1236 type = tmp->type;
1237 }
1238 break;
1239 }
1240
1241 case ast_logic_or: {
1242 exec_list rhs_instructions;
1243 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1244 "LHS", &error_emitted);
1245 op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
1246 "RHS", &error_emitted);
1247
1248 ir_constant *op0_const = op[0]->constant_expression_value();
1249 if (op0_const) {
1250 if (op0_const->value.b[0]) {
1251 result = op0_const;
1252 } else {
1253 result = op[1];
1254 }
1255 type = glsl_type::bool_type;
1256 } else {
1257 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
1258 "or_tmp",
1259 ir_var_temporary);
1260 instructions->push_tail(tmp);
1261
1262 ir_if *const stmt = new(ctx) ir_if(op[0]);
1263 instructions->push_tail(stmt);
1264
1265 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
1266 ir_assignment *const then_assign =
1267 new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true), NULL);
1268 stmt->then_instructions.push_tail(then_assign);
1269
1270 stmt->else_instructions.append_list(&rhs_instructions);
1271 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
1272 ir_assignment *const else_assign =
1273 new(ctx) ir_assignment(else_deref, op[1], NULL);
1274 stmt->else_instructions.push_tail(else_assign);
1275
1276 result = new(ctx) ir_dereference_variable(tmp);
1277 type = tmp->type;
1278 }
1279 break;
1280 }
1281
1282 case ast_logic_xor:
1283 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1284 *
1285 * "The logical binary operators and (&&), or ( | | ), and
1286 * exclusive or (^^). They operate only on two Boolean
1287 * expressions and result in a Boolean expression."
1288 */
1289 op[0] = get_scalar_boolean_operand(instructions, state, this, 0, "LHS",
1290 &error_emitted);
1291 op[1] = get_scalar_boolean_operand(instructions, state, this, 1, "RHS",
1292 &error_emitted);
1293
1294 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
1295 op[0], op[1]);
1296 break;
1297
1298 case ast_logic_not:
1299 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1300 "operand", &error_emitted);
1301
1302 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
1303 op[0], NULL);
1304 break;
1305
1306 case ast_mul_assign:
1307 case ast_div_assign:
1308 case ast_add_assign:
1309 case ast_sub_assign: {
1310 op[0] = this->subexpressions[0]->hir(instructions, state);
1311 op[1] = this->subexpressions[1]->hir(instructions, state);
1312
1313 type = arithmetic_result_type(op[0], op[1],
1314 (this->oper == ast_mul_assign),
1315 state, & loc);
1316
1317 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1318 op[0], op[1]);
1319
1320 result = do_assignment(instructions, state,
1321 this->subexpressions[0]->non_lvalue_description,
1322 op[0]->clone(ctx, NULL), temp_rhs, false,
1323 this->subexpressions[0]->get_location());
1324 error_emitted = (op[0]->type->is_error());
1325
1326 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1327 * explicitly test for this because none of the binary expression
1328 * operators allow array operands either.
1329 */
1330
1331 break;
1332 }
1333
1334 case ast_mod_assign: {
1335 op[0] = this->subexpressions[0]->hir(instructions, state);
1336 op[1] = this->subexpressions[1]->hir(instructions, state);
1337
1338 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
1339
1340 assert(operations[this->oper] == ir_binop_mod);
1341
1342 ir_rvalue *temp_rhs;
1343 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1344 op[0], op[1]);
1345
1346 result = do_assignment(instructions, state,
1347 this->subexpressions[0]->non_lvalue_description,
1348 op[0]->clone(ctx, NULL), temp_rhs, false,
1349 this->subexpressions[0]->get_location());
1350 error_emitted = type->is_error();
1351 break;
1352 }
1353
1354 case ast_ls_assign:
1355 case ast_rs_assign: {
1356 op[0] = this->subexpressions[0]->hir(instructions, state);
1357 op[1] = this->subexpressions[1]->hir(instructions, state);
1358 type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1359 &loc);
1360 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1361 type, op[0], op[1]);
1362 result = do_assignment(instructions, state,
1363 this->subexpressions[0]->non_lvalue_description,
1364 op[0]->clone(ctx, NULL), temp_rhs, false,
1365 this->subexpressions[0]->get_location());
1366 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1367 break;
1368 }
1369
1370 case ast_and_assign:
1371 case ast_xor_assign:
1372 case ast_or_assign: {
1373 op[0] = this->subexpressions[0]->hir(instructions, state);
1374 op[1] = this->subexpressions[1]->hir(instructions, state);
1375 type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
1376 state, &loc);
1377 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1378 type, op[0], op[1]);
1379 result = do_assignment(instructions, state,
1380 this->subexpressions[0]->non_lvalue_description,
1381 op[0]->clone(ctx, NULL), temp_rhs, false,
1382 this->subexpressions[0]->get_location());
1383 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1384 break;
1385 }
1386
1387 case ast_conditional: {
1388 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1389 *
1390 * "The ternary selection operator (?:). It operates on three
1391 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1392 * first expression, which must result in a scalar Boolean."
1393 */
1394 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1395 "condition", &error_emitted);
1396
1397 /* The :? operator is implemented by generating an anonymous temporary
1398 * followed by an if-statement. The last instruction in each branch of
1399 * the if-statement assigns a value to the anonymous temporary. This
1400 * temporary is the r-value of the expression.
1401 */
1402 exec_list then_instructions;
1403 exec_list else_instructions;
1404
1405 op[1] = this->subexpressions[1]->hir(&then_instructions, state);
1406 op[2] = this->subexpressions[2]->hir(&else_instructions, state);
1407
1408 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1409 *
1410 * "The second and third expressions can be any type, as
1411 * long their types match, or there is a conversion in
1412 * Section 4.1.10 "Implicit Conversions" that can be applied
1413 * to one of the expressions to make their types match. This
1414 * resulting matching type is the type of the entire
1415 * expression."
1416 */
1417 if ((!apply_implicit_conversion(op[1]->type, op[2], state)
1418 && !apply_implicit_conversion(op[2]->type, op[1], state))
1419 || (op[1]->type != op[2]->type)) {
1420 YYLTYPE loc = this->subexpressions[1]->get_location();
1421
1422 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
1423 "operator must have matching types.");
1424 error_emitted = true;
1425 type = glsl_type::error_type;
1426 } else {
1427 type = op[1]->type;
1428 }
1429
1430 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1431 *
1432 * "The second and third expressions must be the same type, but can
1433 * be of any type other than an array."
1434 */
1435 if ((state->language_version <= 110) && type->is_array()) {
1436 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
1437 "operator must not be arrays.");
1438 error_emitted = true;
1439 }
1440
1441 ir_constant *cond_val = op[0]->constant_expression_value();
1442 ir_constant *then_val = op[1]->constant_expression_value();
1443 ir_constant *else_val = op[2]->constant_expression_value();
1444
1445 if (then_instructions.is_empty()
1446 && else_instructions.is_empty()
1447 && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) {
1448 result = (cond_val->value.b[0]) ? then_val : else_val;
1449 } else {
1450 ir_variable *const tmp =
1451 new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
1452 instructions->push_tail(tmp);
1453
1454 ir_if *const stmt = new(ctx) ir_if(op[0]);
1455 instructions->push_tail(stmt);
1456
1457 then_instructions.move_nodes_to(& stmt->then_instructions);
1458 ir_dereference *const then_deref =
1459 new(ctx) ir_dereference_variable(tmp);
1460 ir_assignment *const then_assign =
1461 new(ctx) ir_assignment(then_deref, op[1], NULL);
1462 stmt->then_instructions.push_tail(then_assign);
1463
1464 else_instructions.move_nodes_to(& stmt->else_instructions);
1465 ir_dereference *const else_deref =
1466 new(ctx) ir_dereference_variable(tmp);
1467 ir_assignment *const else_assign =
1468 new(ctx) ir_assignment(else_deref, op[2], NULL);
1469 stmt->else_instructions.push_tail(else_assign);
1470
1471 result = new(ctx) ir_dereference_variable(tmp);
1472 }
1473 break;
1474 }
1475
1476 case ast_pre_inc:
1477 case ast_pre_dec: {
1478 this->non_lvalue_description = (this->oper == ast_pre_inc)
1479 ? "pre-increment operation" : "pre-decrement operation";
1480
1481 op[0] = this->subexpressions[0]->hir(instructions, state);
1482 op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
1483
1484 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1485
1486 ir_rvalue *temp_rhs;
1487 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1488 op[0], op[1]);
1489
1490 result = do_assignment(instructions, state,
1491 this->subexpressions[0]->non_lvalue_description,
1492 op[0]->clone(ctx, NULL), temp_rhs, false,
1493 this->subexpressions[0]->get_location());
1494 error_emitted = op[0]->type->is_error();
1495 break;
1496 }
1497
1498 case ast_post_inc:
1499 case ast_post_dec: {
1500 this->non_lvalue_description = (this->oper == ast_post_inc)
1501 ? "post-increment operation" : "post-decrement operation";
1502 op[0] = this->subexpressions[0]->hir(instructions, state);
1503 op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
1504
1505 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1506
1507 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1508
1509 ir_rvalue *temp_rhs;
1510 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1511 op[0], op[1]);
1512
1513 /* Get a temporary of a copy of the lvalue before it's modified.
1514 * This may get thrown away later.
1515 */
1516 result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
1517
1518 (void)do_assignment(instructions, state,
1519 this->subexpressions[0]->non_lvalue_description,
1520 op[0]->clone(ctx, NULL), temp_rhs, false,
1521 this->subexpressions[0]->get_location());
1522
1523 error_emitted = op[0]->type->is_error();
1524 break;
1525 }
1526
1527 case ast_field_selection:
1528 result = _mesa_ast_field_selection_to_hir(this, instructions, state);
1529 break;
1530
1531 case ast_array_index: {
1532 YYLTYPE index_loc = subexpressions[1]->get_location();
1533
1534 op[0] = subexpressions[0]->hir(instructions, state);
1535 op[1] = subexpressions[1]->hir(instructions, state);
1536
1537 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1538
1539 ir_rvalue *const array = op[0];
1540
1541 result = new(ctx) ir_dereference_array(op[0], op[1]);
1542
1543 /* Do not use op[0] after this point. Use array.
1544 */
1545 op[0] = NULL;
1546
1547
1548 if (error_emitted)
1549 break;
1550
1551 if (!array->type->is_array()
1552 && !array->type->is_matrix()
1553 && !array->type->is_vector()) {
1554 _mesa_glsl_error(& index_loc, state,
1555 "cannot dereference non-array / non-matrix / "
1556 "non-vector");
1557 error_emitted = true;
1558 }
1559
1560 if (!op[1]->type->is_integer()) {
1561 _mesa_glsl_error(& index_loc, state,
1562 "array index must be integer type");
1563 error_emitted = true;
1564 } else if (!op[1]->type->is_scalar()) {
1565 _mesa_glsl_error(& index_loc, state,
1566 "array index must be scalar");
1567 error_emitted = true;
1568 }
1569
1570 /* If the array index is a constant expression and the array has a
1571 * declared size, ensure that the access is in-bounds. If the array
1572 * index is not a constant expression, ensure that the array has a
1573 * declared size.
1574 */
1575 ir_constant *const const_index = op[1]->constant_expression_value();
1576 if (const_index != NULL) {
1577 const int idx = const_index->value.i[0];
1578 const char *type_name;
1579 unsigned bound = 0;
1580
1581 if (array->type->is_matrix()) {
1582 type_name = "matrix";
1583 } else if (array->type->is_vector()) {
1584 type_name = "vector";
1585 } else {
1586 type_name = "array";
1587 }
1588
1589 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
1590 *
1591 * "It is illegal to declare an array with a size, and then
1592 * later (in the same shader) index the same array with an
1593 * integral constant expression greater than or equal to the
1594 * declared size. It is also illegal to index an array with a
1595 * negative constant expression."
1596 */
1597 if (array->type->is_matrix()) {
1598 if (array->type->row_type()->vector_elements <= idx) {
1599 bound = array->type->row_type()->vector_elements;
1600 }
1601 } else if (array->type->is_vector()) {
1602 if (array->type->vector_elements <= idx) {
1603 bound = array->type->vector_elements;
1604 }
1605 } else {
1606 if ((array->type->array_size() > 0)
1607 && (array->type->array_size() <= idx)) {
1608 bound = array->type->array_size();
1609 }
1610 }
1611
1612 if (bound > 0) {
1613 _mesa_glsl_error(& loc, state, "%s index must be < %u",
1614 type_name, bound);
1615 error_emitted = true;
1616 } else if (idx < 0) {
1617 _mesa_glsl_error(& loc, state, "%s index must be >= 0",
1618 type_name);
1619 error_emitted = true;
1620 }
1621
1622 if (array->type->is_array()) {
1623 /* If the array is a variable dereference, it dereferences the
1624 * whole array, by definition. Use this to get the variable.
1625 *
1626 * FINISHME: Should some methods for getting / setting / testing
1627 * FINISHME: array access limits be added to ir_dereference?
1628 */
1629 ir_variable *const v = array->whole_variable_referenced();
1630 if ((v != NULL) && (unsigned(idx) > v->max_array_access)) {
1631 v->max_array_access = idx;
1632
1633 /* Check whether this access will, as a side effect, implicitly
1634 * cause the size of a built-in array to be too large.
1635 */
1636 if (check_builtin_array_max_size(v->name, idx+1, loc, state))
1637 error_emitted = true;
1638 }
1639 }
1640 } else if (array->type->array_size() == 0) {
1641 _mesa_glsl_error(&loc, state, "unsized array index must be constant");
1642 } else {
1643 if (array->type->is_array()) {
1644 /* whole_variable_referenced can return NULL if the array is a
1645 * member of a structure. In this case it is safe to not update
1646 * the max_array_access field because it is never used for fields
1647 * of structures.
1648 */
1649 ir_variable *v = array->whole_variable_referenced();
1650 if (v != NULL)
1651 v->max_array_access = array->type->array_size() - 1;
1652 }
1653 }
1654
1655 /* From page 23 (29 of the PDF) of the GLSL 1.30 spec:
1656 *
1657 * "Samplers aggregated into arrays within a shader (using square
1658 * brackets [ ]) can only be indexed with integral constant
1659 * expressions [...]."
1660 *
1661 * This restriction was added in GLSL 1.30. Shaders using earlier version
1662 * of the language should not be rejected by the compiler front-end for
1663 * using this construct. This allows useful things such as using a loop
1664 * counter as the index to an array of samplers. If the loop in unrolled,
1665 * the code should compile correctly. Instead, emit a warning.
1666 */
1667 if (array->type->is_array() &&
1668 array->type->element_type()->is_sampler() &&
1669 const_index == NULL) {
1670
1671 if (state->language_version == 100) {
1672 _mesa_glsl_warning(&loc, state,
1673 "sampler arrays indexed with non-constant "
1674 "expressions is optional in GLSL ES 1.00");
1675 } else if (state->language_version < 130) {
1676 _mesa_glsl_warning(&loc, state,
1677 "sampler arrays indexed with non-constant "
1678 "expressions is forbidden in GLSL 1.30 and "
1679 "later");
1680 } else {
1681 _mesa_glsl_error(&loc, state,
1682 "sampler arrays indexed with non-constant "
1683 "expressions is forbidden in GLSL 1.30 and "
1684 "later");
1685 error_emitted = true;
1686 }
1687 }
1688
1689 if (error_emitted)
1690 result->type = glsl_type::error_type;
1691
1692 break;
1693 }
1694
1695 case ast_function_call:
1696 /* Should *NEVER* get here. ast_function_call should always be handled
1697 * by ast_function_expression::hir.
1698 */
1699 assert(0);
1700 break;
1701
1702 case ast_identifier: {
1703 /* ast_identifier can appear several places in a full abstract syntax
1704 * tree. This particular use must be at location specified in the grammar
1705 * as 'variable_identifier'.
1706 */
1707 ir_variable *var =
1708 state->symbols->get_variable(this->primary_expression.identifier);
1709
1710 result = new(ctx) ir_dereference_variable(var);
1711
1712 if (var != NULL) {
1713 var->used = true;
1714 } else {
1715 _mesa_glsl_error(& loc, state, "`%s' undeclared",
1716 this->primary_expression.identifier);
1717
1718 error_emitted = true;
1719 }
1720 break;
1721 }
1722
1723 case ast_int_constant:
1724 result = new(ctx) ir_constant(this->primary_expression.int_constant);
1725 break;
1726
1727 case ast_uint_constant:
1728 result = new(ctx) ir_constant(this->primary_expression.uint_constant);
1729 break;
1730
1731 case ast_float_constant:
1732 result = new(ctx) ir_constant(this->primary_expression.float_constant);
1733 break;
1734
1735 case ast_bool_constant:
1736 result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
1737 break;
1738
1739 case ast_sequence: {
1740 /* It should not be possible to generate a sequence in the AST without
1741 * any expressions in it.
1742 */
1743 assert(!this->expressions.is_empty());
1744
1745 /* The r-value of a sequence is the last expression in the sequence. If
1746 * the other expressions in the sequence do not have side-effects (and
1747 * therefore add instructions to the instruction list), they get dropped
1748 * on the floor.
1749 */
1750 exec_node *previous_tail_pred = NULL;
1751 YYLTYPE previous_operand_loc = loc;
1752
1753 foreach_list_typed (ast_node, ast, link, &this->expressions) {
1754 /* If one of the operands of comma operator does not generate any
1755 * code, we want to emit a warning. At each pass through the loop
1756 * previous_tail_pred will point to the last instruction in the
1757 * stream *before* processing the previous operand. Naturally,
1758 * instructions->tail_pred will point to the last instruction in the
1759 * stream *after* processing the previous operand. If the two
1760 * pointers match, then the previous operand had no effect.
1761 *
1762 * The warning behavior here differs slightly from GCC. GCC will
1763 * only emit a warning if none of the left-hand operands have an
1764 * effect. However, it will emit a warning for each. I believe that
1765 * there are some cases in C (especially with GCC extensions) where
1766 * it is useful to have an intermediate step in a sequence have no
1767 * effect, but I don't think these cases exist in GLSL. Either way,
1768 * it would be a giant hassle to replicate that behavior.
1769 */
1770 if (previous_tail_pred == instructions->tail_pred) {
1771 _mesa_glsl_warning(&previous_operand_loc, state,
1772 "left-hand operand of comma expression has "
1773 "no effect");
1774 }
1775
1776 /* tail_pred is directly accessed instead of using the get_tail()
1777 * method for performance reasons. get_tail() has extra code to
1778 * return NULL when the list is empty. We don't care about that
1779 * here, so using tail_pred directly is fine.
1780 */
1781 previous_tail_pred = instructions->tail_pred;
1782 previous_operand_loc = ast->get_location();
1783
1784 result = ast->hir(instructions, state);
1785 }
1786
1787 /* Any errors should have already been emitted in the loop above.
1788 */
1789 error_emitted = true;
1790 break;
1791 }
1792 }
1793 type = NULL; /* use result->type, not type. */
1794 assert(result != NULL);
1795
1796 if (result->type->is_error() && !error_emitted)
1797 _mesa_glsl_error(& loc, state, "type mismatch");
1798
1799 return result;
1800 }
1801
1802
1803 ir_rvalue *
1804 ast_expression_statement::hir(exec_list *instructions,
1805 struct _mesa_glsl_parse_state *state)
1806 {
1807 /* It is possible to have expression statements that don't have an
1808 * expression. This is the solitary semicolon:
1809 *
1810 * for (i = 0; i < 5; i++)
1811 * ;
1812 *
1813 * In this case the expression will be NULL. Test for NULL and don't do
1814 * anything in that case.
1815 */
1816 if (expression != NULL)
1817 expression->hir(instructions, state);
1818
1819 /* Statements do not have r-values.
1820 */
1821 return NULL;
1822 }
1823
1824
1825 ir_rvalue *
1826 ast_compound_statement::hir(exec_list *instructions,
1827 struct _mesa_glsl_parse_state *state)
1828 {
1829 if (new_scope)
1830 state->symbols->push_scope();
1831
1832 foreach_list_typed (ast_node, ast, link, &this->statements)
1833 ast->hir(instructions, state);
1834
1835 if (new_scope)
1836 state->symbols->pop_scope();
1837
1838 /* Compound statements do not have r-values.
1839 */
1840 return NULL;
1841 }
1842
1843
1844 static const glsl_type *
1845 process_array_type(YYLTYPE *loc, const glsl_type *base, ast_node *array_size,
1846 struct _mesa_glsl_parse_state *state)
1847 {
1848 unsigned length = 0;
1849
1850 /* From page 19 (page 25) of the GLSL 1.20 spec:
1851 *
1852 * "Only one-dimensional arrays may be declared."
1853 */
1854 if (base->is_array()) {
1855 _mesa_glsl_error(loc, state,
1856 "invalid array of `%s' (only one-dimensional arrays "
1857 "may be declared)",
1858 base->name);
1859 return glsl_type::error_type;
1860 }
1861
1862 if (array_size != NULL) {
1863 exec_list dummy_instructions;
1864 ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
1865 YYLTYPE loc = array_size->get_location();
1866
1867 if (ir != NULL) {
1868 if (!ir->type->is_integer()) {
1869 _mesa_glsl_error(& loc, state, "array size must be integer type");
1870 } else if (!ir->type->is_scalar()) {
1871 _mesa_glsl_error(& loc, state, "array size must be scalar type");
1872 } else {
1873 ir_constant *const size = ir->constant_expression_value();
1874
1875 if (size == NULL) {
1876 _mesa_glsl_error(& loc, state, "array size must be a "
1877 "constant valued expression");
1878 } else if (size->value.i[0] <= 0) {
1879 _mesa_glsl_error(& loc, state, "array size must be > 0");
1880 } else {
1881 assert(size->type == ir->type);
1882 length = size->value.u[0];
1883
1884 /* If the array size is const (and we've verified that
1885 * it is) then no instructions should have been emitted
1886 * when we converted it to HIR. If they were emitted,
1887 * then either the array size isn't const after all, or
1888 * we are emitting unnecessary instructions.
1889 */
1890 assert(dummy_instructions.is_empty());
1891 }
1892 }
1893 }
1894 } else if (state->es_shader) {
1895 /* Section 10.17 of the GLSL ES 1.00 specification states that unsized
1896 * array declarations have been removed from the language.
1897 */
1898 _mesa_glsl_error(loc, state, "unsized array declarations are not "
1899 "allowed in GLSL ES 1.00.");
1900 }
1901
1902 return glsl_type::get_array_instance(base, length);
1903 }
1904
1905
1906 const glsl_type *
1907 ast_type_specifier::glsl_type(const char **name,
1908 struct _mesa_glsl_parse_state *state) const
1909 {
1910 const struct glsl_type *type;
1911
1912 type = state->symbols->get_type(this->type_name);
1913 *name = this->type_name;
1914
1915 if (this->is_array) {
1916 YYLTYPE loc = this->get_location();
1917 type = process_array_type(&loc, type, this->array_size, state);
1918 }
1919
1920 return type;
1921 }
1922
1923
1924 static void
1925 apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
1926 ir_variable *var,
1927 struct _mesa_glsl_parse_state *state,
1928 YYLTYPE *loc)
1929 {
1930 if (qual->flags.q.invariant) {
1931 if (var->used) {
1932 _mesa_glsl_error(loc, state,
1933 "variable `%s' may not be redeclared "
1934 "`invariant' after being used",
1935 var->name);
1936 } else {
1937 var->invariant = 1;
1938 }
1939 }
1940
1941 if (qual->flags.q.constant || qual->flags.q.attribute
1942 || qual->flags.q.uniform
1943 || (qual->flags.q.varying && (state->target == fragment_shader)))
1944 var->read_only = 1;
1945
1946 if (qual->flags.q.centroid)
1947 var->centroid = 1;
1948
1949 if (qual->flags.q.attribute && state->target != vertex_shader) {
1950 var->type = glsl_type::error_type;
1951 _mesa_glsl_error(loc, state,
1952 "`attribute' variables may not be declared in the "
1953 "%s shader",
1954 _mesa_glsl_shader_target_name(state->target));
1955 }
1956
1957 /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
1958 *
1959 * "The varying qualifier can be used only with the data types
1960 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
1961 * these."
1962 */
1963 if (qual->flags.q.varying) {
1964 const glsl_type *non_array_type;
1965
1966 if (var->type && var->type->is_array())
1967 non_array_type = var->type->fields.array;
1968 else
1969 non_array_type = var->type;
1970
1971 if (non_array_type && non_array_type->base_type != GLSL_TYPE_FLOAT) {
1972 var->type = glsl_type::error_type;
1973 _mesa_glsl_error(loc, state,
1974 "varying variables must be of base type float");
1975 }
1976 }
1977
1978 /* If there is no qualifier that changes the mode of the variable, leave
1979 * the setting alone.
1980 */
1981 if (qual->flags.q.in && qual->flags.q.out)
1982 var->mode = ir_var_inout;
1983 else if (qual->flags.q.attribute || qual->flags.q.in
1984 || (qual->flags.q.varying && (state->target == fragment_shader)))
1985 var->mode = ir_var_in;
1986 else if (qual->flags.q.out
1987 || (qual->flags.q.varying && (state->target == vertex_shader)))
1988 var->mode = ir_var_out;
1989 else if (qual->flags.q.uniform)
1990 var->mode = ir_var_uniform;
1991
1992 if (state->all_invariant && (state->current_function == NULL)) {
1993 switch (state->target) {
1994 case vertex_shader:
1995 if (var->mode == ir_var_out)
1996 var->invariant = true;
1997 break;
1998 case geometry_shader:
1999 if ((var->mode == ir_var_in) || (var->mode == ir_var_out))
2000 var->invariant = true;
2001 break;
2002 case fragment_shader:
2003 if (var->mode == ir_var_in)
2004 var->invariant = true;
2005 break;
2006 }
2007 }
2008
2009 if (qual->flags.q.flat)
2010 var->interpolation = INTERP_QUALIFIER_FLAT;
2011 else if (qual->flags.q.noperspective)
2012 var->interpolation = INTERP_QUALIFIER_NOPERSPECTIVE;
2013 else if (qual->flags.q.smooth)
2014 var->interpolation = INTERP_QUALIFIER_SMOOTH;
2015 else
2016 var->interpolation = INTERP_QUALIFIER_NONE;
2017
2018 var->pixel_center_integer = qual->flags.q.pixel_center_integer;
2019 var->origin_upper_left = qual->flags.q.origin_upper_left;
2020 if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer)
2021 && (strcmp(var->name, "gl_FragCoord") != 0)) {
2022 const char *const qual_string = (qual->flags.q.origin_upper_left)
2023 ? "origin_upper_left" : "pixel_center_integer";
2024
2025 _mesa_glsl_error(loc, state,
2026 "layout qualifier `%s' can only be applied to "
2027 "fragment shader input `gl_FragCoord'",
2028 qual_string);
2029 }
2030
2031 if (qual->flags.q.explicit_location) {
2032 const bool global_scope = (state->current_function == NULL);
2033 bool fail = false;
2034 const char *string = "";
2035
2036 /* In the vertex shader only shader inputs can be given explicit
2037 * locations.
2038 *
2039 * In the fragment shader only shader outputs can be given explicit
2040 * locations.
2041 */
2042 switch (state->target) {
2043 case vertex_shader:
2044 if (!global_scope || (var->mode != ir_var_in)) {
2045 fail = true;
2046 string = "input";
2047 }
2048 break;
2049
2050 case geometry_shader:
2051 _mesa_glsl_error(loc, state,
2052 "geometry shader variables cannot be given "
2053 "explicit locations\n");
2054 break;
2055
2056 case fragment_shader:
2057 if (!global_scope || (var->mode != ir_var_out)) {
2058 fail = true;
2059 string = "output";
2060 }
2061 break;
2062 };
2063
2064 if (fail) {
2065 _mesa_glsl_error(loc, state,
2066 "only %s shader %s variables can be given an "
2067 "explicit location\n",
2068 _mesa_glsl_shader_target_name(state->target),
2069 string);
2070 } else {
2071 var->explicit_location = true;
2072
2073 /* This bit of silliness is needed because invalid explicit locations
2074 * are supposed to be flagged during linking. Small negative values
2075 * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
2076 * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
2077 * The linker needs to be able to differentiate these cases. This
2078 * ensures that negative values stay negative.
2079 */
2080 if (qual->location >= 0) {
2081 var->location = (state->target == vertex_shader)
2082 ? (qual->location + VERT_ATTRIB_GENERIC0)
2083 : (qual->location + FRAG_RESULT_DATA0);
2084 } else {
2085 var->location = qual->location;
2086 }
2087 }
2088 }
2089
2090 /* Does the declaration use the 'layout' keyword?
2091 */
2092 const bool uses_layout = qual->flags.q.pixel_center_integer
2093 || qual->flags.q.origin_upper_left
2094 || qual->flags.q.explicit_location;
2095
2096 /* Does the declaration use the deprecated 'attribute' or 'varying'
2097 * keywords?
2098 */
2099 const bool uses_deprecated_qualifier = qual->flags.q.attribute
2100 || qual->flags.q.varying;
2101
2102 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
2103 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
2104 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
2105 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
2106 * These extensions and all following extensions that add the 'layout'
2107 * keyword have been modified to require the use of 'in' or 'out'.
2108 *
2109 * The following extension do not allow the deprecated keywords:
2110 *
2111 * GL_AMD_conservative_depth
2112 * GL_ARB_conservative_depth
2113 * GL_ARB_gpu_shader5
2114 * GL_ARB_separate_shader_objects
2115 * GL_ARB_tesselation_shader
2116 * GL_ARB_transform_feedback3
2117 * GL_ARB_uniform_buffer_object
2118 *
2119 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
2120 * allow layout with the deprecated keywords.
2121 */
2122 const bool relaxed_layout_qualifier_checking =
2123 state->ARB_fragment_coord_conventions_enable;
2124
2125 if (uses_layout && uses_deprecated_qualifier) {
2126 if (relaxed_layout_qualifier_checking) {
2127 _mesa_glsl_warning(loc, state,
2128 "`layout' qualifier may not be used with "
2129 "`attribute' or `varying'");
2130 } else {
2131 _mesa_glsl_error(loc, state,
2132 "`layout' qualifier may not be used with "
2133 "`attribute' or `varying'");
2134 }
2135 }
2136
2137 /* Layout qualifiers for gl_FragDepth, which are enabled by extension
2138 * AMD_conservative_depth.
2139 */
2140 int depth_layout_count = qual->flags.q.depth_any
2141 + qual->flags.q.depth_greater
2142 + qual->flags.q.depth_less
2143 + qual->flags.q.depth_unchanged;
2144 if (depth_layout_count > 0
2145 && !state->AMD_conservative_depth_enable
2146 && !state->ARB_conservative_depth_enable) {
2147 _mesa_glsl_error(loc, state,
2148 "extension GL_AMD_conservative_depth or "
2149 "GL_ARB_conservative_depth must be enabled "
2150 "to use depth layout qualifiers");
2151 } else if (depth_layout_count > 0
2152 && strcmp(var->name, "gl_FragDepth") != 0) {
2153 _mesa_glsl_error(loc, state,
2154 "depth layout qualifiers can be applied only to "
2155 "gl_FragDepth");
2156 } else if (depth_layout_count > 1
2157 && strcmp(var->name, "gl_FragDepth") == 0) {
2158 _mesa_glsl_error(loc, state,
2159 "at most one depth layout qualifier can be applied to "
2160 "gl_FragDepth");
2161 }
2162 if (qual->flags.q.depth_any)
2163 var->depth_layout = ir_depth_layout_any;
2164 else if (qual->flags.q.depth_greater)
2165 var->depth_layout = ir_depth_layout_greater;
2166 else if (qual->flags.q.depth_less)
2167 var->depth_layout = ir_depth_layout_less;
2168 else if (qual->flags.q.depth_unchanged)
2169 var->depth_layout = ir_depth_layout_unchanged;
2170 else
2171 var->depth_layout = ir_depth_layout_none;
2172 }
2173
2174 /**
2175 * Get the variable that is being redeclared by this declaration
2176 *
2177 * Semantic checks to verify the validity of the redeclaration are also
2178 * performed. If semantic checks fail, compilation error will be emitted via
2179 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
2180 *
2181 * \returns
2182 * A pointer to an existing variable in the current scope if the declaration
2183 * is a redeclaration, \c NULL otherwise.
2184 */
2185 ir_variable *
2186 get_variable_being_redeclared(ir_variable *var, ast_declaration *decl,
2187 struct _mesa_glsl_parse_state *state)
2188 {
2189 /* Check if this declaration is actually a re-declaration, either to
2190 * resize an array or add qualifiers to an existing variable.
2191 *
2192 * This is allowed for variables in the current scope, or when at
2193 * global scope (for built-ins in the implicit outer scope).
2194 */
2195 ir_variable *earlier = state->symbols->get_variable(decl->identifier);
2196 if (earlier == NULL ||
2197 (state->current_function != NULL &&
2198 !state->symbols->name_declared_this_scope(decl->identifier))) {
2199 return NULL;
2200 }
2201
2202
2203 YYLTYPE loc = decl->get_location();
2204
2205 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
2206 *
2207 * "It is legal to declare an array without a size and then
2208 * later re-declare the same name as an array of the same
2209 * type and specify a size."
2210 */
2211 if ((earlier->type->array_size() == 0)
2212 && var->type->is_array()
2213 && (var->type->element_type() == earlier->type->element_type())) {
2214 /* FINISHME: This doesn't match the qualifiers on the two
2215 * FINISHME: declarations. It's not 100% clear whether this is
2216 * FINISHME: required or not.
2217 */
2218
2219 const unsigned size = unsigned(var->type->array_size());
2220 check_builtin_array_max_size(var->name, size, loc, state);
2221 if ((size > 0) && (size <= earlier->max_array_access)) {
2222 _mesa_glsl_error(& loc, state, "array size must be > %u due to "
2223 "previous access",
2224 earlier->max_array_access);
2225 }
2226
2227 earlier->type = var->type;
2228 delete var;
2229 var = NULL;
2230 } else if (state->ARB_fragment_coord_conventions_enable
2231 && strcmp(var->name, "gl_FragCoord") == 0
2232 && earlier->type == var->type
2233 && earlier->mode == var->mode) {
2234 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
2235 * qualifiers.
2236 */
2237 earlier->origin_upper_left = var->origin_upper_left;
2238 earlier->pixel_center_integer = var->pixel_center_integer;
2239
2240 /* According to section 4.3.7 of the GLSL 1.30 spec,
2241 * the following built-in varaibles can be redeclared with an
2242 * interpolation qualifier:
2243 * * gl_FrontColor
2244 * * gl_BackColor
2245 * * gl_FrontSecondaryColor
2246 * * gl_BackSecondaryColor
2247 * * gl_Color
2248 * * gl_SecondaryColor
2249 */
2250 } else if (state->language_version >= 130
2251 && (strcmp(var->name, "gl_FrontColor") == 0
2252 || strcmp(var->name, "gl_BackColor") == 0
2253 || strcmp(var->name, "gl_FrontSecondaryColor") == 0
2254 || strcmp(var->name, "gl_BackSecondaryColor") == 0
2255 || strcmp(var->name, "gl_Color") == 0
2256 || strcmp(var->name, "gl_SecondaryColor") == 0)
2257 && earlier->type == var->type
2258 && earlier->mode == var->mode) {
2259 earlier->interpolation = var->interpolation;
2260
2261 /* Layout qualifiers for gl_FragDepth. */
2262 } else if ((state->AMD_conservative_depth_enable ||
2263 state->ARB_conservative_depth_enable)
2264 && strcmp(var->name, "gl_FragDepth") == 0
2265 && earlier->type == var->type
2266 && earlier->mode == var->mode) {
2267
2268 /** From the AMD_conservative_depth spec:
2269 * Within any shader, the first redeclarations of gl_FragDepth
2270 * must appear before any use of gl_FragDepth.
2271 */
2272 if (earlier->used) {
2273 _mesa_glsl_error(&loc, state,
2274 "the first redeclaration of gl_FragDepth "
2275 "must appear before any use of gl_FragDepth");
2276 }
2277
2278 /* Prevent inconsistent redeclaration of depth layout qualifier. */
2279 if (earlier->depth_layout != ir_depth_layout_none
2280 && earlier->depth_layout != var->depth_layout) {
2281 _mesa_glsl_error(&loc, state,
2282 "gl_FragDepth: depth layout is declared here "
2283 "as '%s, but it was previously declared as "
2284 "'%s'",
2285 depth_layout_string(var->depth_layout),
2286 depth_layout_string(earlier->depth_layout));
2287 }
2288
2289 earlier->depth_layout = var->depth_layout;
2290
2291 } else {
2292 _mesa_glsl_error(&loc, state, "`%s' redeclared", decl->identifier);
2293 }
2294
2295 return earlier;
2296 }
2297
2298 /**
2299 * Generate the IR for an initializer in a variable declaration
2300 */
2301 ir_rvalue *
2302 process_initializer(ir_variable *var, ast_declaration *decl,
2303 ast_fully_specified_type *type,
2304 exec_list *initializer_instructions,
2305 struct _mesa_glsl_parse_state *state)
2306 {
2307 ir_rvalue *result = NULL;
2308
2309 YYLTYPE initializer_loc = decl->initializer->get_location();
2310
2311 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
2312 *
2313 * "All uniform variables are read-only and are initialized either
2314 * directly by an application via API commands, or indirectly by
2315 * OpenGL."
2316 */
2317 if ((state->language_version <= 110)
2318 && (var->mode == ir_var_uniform)) {
2319 _mesa_glsl_error(& initializer_loc, state,
2320 "cannot initialize uniforms in GLSL 1.10");
2321 }
2322
2323 if (var->type->is_sampler()) {
2324 _mesa_glsl_error(& initializer_loc, state,
2325 "cannot initialize samplers");
2326 }
2327
2328 if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
2329 _mesa_glsl_error(& initializer_loc, state,
2330 "cannot initialize %s shader input / %s",
2331 _mesa_glsl_shader_target_name(state->target),
2332 (state->target == vertex_shader)
2333 ? "attribute" : "varying");
2334 }
2335
2336 ir_dereference *const lhs = new(state) ir_dereference_variable(var);
2337 ir_rvalue *rhs = decl->initializer->hir(initializer_instructions,
2338 state);
2339
2340 /* Calculate the constant value if this is a const or uniform
2341 * declaration.
2342 */
2343 if (type->qualifier.flags.q.constant
2344 || type->qualifier.flags.q.uniform) {
2345 ir_rvalue *new_rhs = validate_assignment(state, var->type, rhs, true);
2346 if (new_rhs != NULL) {
2347 rhs = new_rhs;
2348
2349 ir_constant *constant_value = rhs->constant_expression_value();
2350 if (!constant_value) {
2351 _mesa_glsl_error(& initializer_loc, state,
2352 "initializer of %s variable `%s' must be a "
2353 "constant expression",
2354 (type->qualifier.flags.q.constant)
2355 ? "const" : "uniform",
2356 decl->identifier);
2357 if (var->type->is_numeric()) {
2358 /* Reduce cascading errors. */
2359 var->constant_value = ir_constant::zero(state, var->type);
2360 }
2361 } else {
2362 rhs = constant_value;
2363 var->constant_value = constant_value;
2364 }
2365 } else {
2366 _mesa_glsl_error(&initializer_loc, state,
2367 "initializer of type %s cannot be assigned to "
2368 "variable of type %s",
2369 rhs->type->name, var->type->name);
2370 if (var->type->is_numeric()) {
2371 /* Reduce cascading errors. */
2372 var->constant_value = ir_constant::zero(state, var->type);
2373 }
2374 }
2375 }
2376
2377 if (rhs && !rhs->type->is_error()) {
2378 bool temp = var->read_only;
2379 if (type->qualifier.flags.q.constant)
2380 var->read_only = false;
2381
2382 /* Never emit code to initialize a uniform.
2383 */
2384 const glsl_type *initializer_type;
2385 if (!type->qualifier.flags.q.uniform) {
2386 result = do_assignment(initializer_instructions, state,
2387 NULL,
2388 lhs, rhs, true,
2389 type->get_location());
2390 initializer_type = result->type;
2391 } else
2392 initializer_type = rhs->type;
2393
2394 var->constant_initializer = rhs->constant_expression_value();
2395 var->has_initializer = true;
2396
2397 /* If the declared variable is an unsized array, it must inherrit
2398 * its full type from the initializer. A declaration such as
2399 *
2400 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
2401 *
2402 * becomes
2403 *
2404 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
2405 *
2406 * The assignment generated in the if-statement (below) will also
2407 * automatically handle this case for non-uniforms.
2408 *
2409 * If the declared variable is not an array, the types must
2410 * already match exactly. As a result, the type assignment
2411 * here can be done unconditionally. For non-uniforms the call
2412 * to do_assignment can change the type of the initializer (via
2413 * the implicit conversion rules). For uniforms the initializer
2414 * must be a constant expression, and the type of that expression
2415 * was validated above.
2416 */
2417 var->type = initializer_type;
2418
2419 var->read_only = temp;
2420 }
2421
2422 return result;
2423 }
2424
2425 ir_rvalue *
2426 ast_declarator_list::hir(exec_list *instructions,
2427 struct _mesa_glsl_parse_state *state)
2428 {
2429 void *ctx = state;
2430 const struct glsl_type *decl_type;
2431 const char *type_name = NULL;
2432 ir_rvalue *result = NULL;
2433 YYLTYPE loc = this->get_location();
2434
2435 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
2436 *
2437 * "To ensure that a particular output variable is invariant, it is
2438 * necessary to use the invariant qualifier. It can either be used to
2439 * qualify a previously declared variable as being invariant
2440 *
2441 * invariant gl_Position; // make existing gl_Position be invariant"
2442 *
2443 * In these cases the parser will set the 'invariant' flag in the declarator
2444 * list, and the type will be NULL.
2445 */
2446 if (this->invariant) {
2447 assert(this->type == NULL);
2448
2449 if (state->current_function != NULL) {
2450 _mesa_glsl_error(& loc, state,
2451 "All uses of `invariant' keyword must be at global "
2452 "scope\n");
2453 }
2454
2455 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
2456 assert(!decl->is_array);
2457 assert(decl->array_size == NULL);
2458 assert(decl->initializer == NULL);
2459
2460 ir_variable *const earlier =
2461 state->symbols->get_variable(decl->identifier);
2462 if (earlier == NULL) {
2463 _mesa_glsl_error(& loc, state,
2464 "Undeclared variable `%s' cannot be marked "
2465 "invariant\n", decl->identifier);
2466 } else if ((state->target == vertex_shader)
2467 && (earlier->mode != ir_var_out)) {
2468 _mesa_glsl_error(& loc, state,
2469 "`%s' cannot be marked invariant, vertex shader "
2470 "outputs only\n", decl->identifier);
2471 } else if ((state->target == fragment_shader)
2472 && (earlier->mode != ir_var_in)) {
2473 _mesa_glsl_error(& loc, state,
2474 "`%s' cannot be marked invariant, fragment shader "
2475 "inputs only\n", decl->identifier);
2476 } else if (earlier->used) {
2477 _mesa_glsl_error(& loc, state,
2478 "variable `%s' may not be redeclared "
2479 "`invariant' after being used",
2480 earlier->name);
2481 } else {
2482 earlier->invariant = true;
2483 }
2484 }
2485
2486 /* Invariant redeclarations do not have r-values.
2487 */
2488 return NULL;
2489 }
2490
2491 assert(this->type != NULL);
2492 assert(!this->invariant);
2493
2494 /* The type specifier may contain a structure definition. Process that
2495 * before any of the variable declarations.
2496 */
2497 (void) this->type->specifier->hir(instructions, state);
2498
2499 decl_type = this->type->specifier->glsl_type(& type_name, state);
2500 if (this->declarations.is_empty()) {
2501 /* If there is no structure involved in the program text, there are two
2502 * possible scenarios:
2503 *
2504 * - The program text contained something like 'vec4;'. This is an
2505 * empty declaration. It is valid but weird. Emit a warning.
2506 *
2507 * - The program text contained something like 'S;' and 'S' is not the
2508 * name of a known structure type. This is both invalid and weird.
2509 * Emit an error.
2510 *
2511 * Note that if decl_type is NULL and there is a structure involved,
2512 * there must have been some sort of error with the structure. In this
2513 * case we assume that an error was already generated on this line of
2514 * code for the structure. There is no need to generate an additional,
2515 * confusing error.
2516 */
2517 assert(this->type->specifier->structure == NULL || decl_type != NULL
2518 || state->error);
2519 if (this->type->specifier->structure == NULL) {
2520 if (decl_type != NULL) {
2521 _mesa_glsl_warning(&loc, state, "empty declaration");
2522 } else {
2523 _mesa_glsl_error(&loc, state,
2524 "invalid type `%s' in empty declaration",
2525 type_name);
2526 }
2527 }
2528 }
2529
2530 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
2531 const struct glsl_type *var_type;
2532 ir_variable *var;
2533
2534 /* FINISHME: Emit a warning if a variable declaration shadows a
2535 * FINISHME: declaration at a higher scope.
2536 */
2537
2538 if ((decl_type == NULL) || decl_type->is_void()) {
2539 if (type_name != NULL) {
2540 _mesa_glsl_error(& loc, state,
2541 "invalid type `%s' in declaration of `%s'",
2542 type_name, decl->identifier);
2543 } else {
2544 _mesa_glsl_error(& loc, state,
2545 "invalid type in declaration of `%s'",
2546 decl->identifier);
2547 }
2548 continue;
2549 }
2550
2551 if (decl->is_array) {
2552 var_type = process_array_type(&loc, decl_type, decl->array_size,
2553 state);
2554 if (var_type->is_error())
2555 continue;
2556 } else {
2557 var_type = decl_type;
2558 }
2559
2560 var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto);
2561
2562 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
2563 *
2564 * "Global variables can only use the qualifiers const,
2565 * attribute, uni form, or varying. Only one may be
2566 * specified.
2567 *
2568 * Local variables can only use the qualifier const."
2569 *
2570 * This is relaxed in GLSL 1.30. It is also relaxed by any extension
2571 * that adds the 'layout' keyword.
2572 */
2573 if ((state->language_version < 130)
2574 && !state->ARB_explicit_attrib_location_enable
2575 && !state->ARB_fragment_coord_conventions_enable) {
2576 if (this->type->qualifier.flags.q.out) {
2577 _mesa_glsl_error(& loc, state,
2578 "`out' qualifier in declaration of `%s' "
2579 "only valid for function parameters in %s.",
2580 decl->identifier, state->version_string);
2581 }
2582 if (this->type->qualifier.flags.q.in) {
2583 _mesa_glsl_error(& loc, state,
2584 "`in' qualifier in declaration of `%s' "
2585 "only valid for function parameters in %s.",
2586 decl->identifier, state->version_string);
2587 }
2588 /* FINISHME: Test for other invalid qualifiers. */
2589 }
2590
2591 apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
2592 & loc);
2593
2594 if (this->type->qualifier.flags.q.invariant) {
2595 if ((state->target == vertex_shader) && !(var->mode == ir_var_out ||
2596 var->mode == ir_var_inout)) {
2597 /* FINISHME: Note that this doesn't work for invariant on
2598 * a function signature outval
2599 */
2600 _mesa_glsl_error(& loc, state,
2601 "`%s' cannot be marked invariant, vertex shader "
2602 "outputs only\n", var->name);
2603 } else if ((state->target == fragment_shader) &&
2604 !(var->mode == ir_var_in || var->mode == ir_var_inout)) {
2605 /* FINISHME: Note that this doesn't work for invariant on
2606 * a function signature inval
2607 */
2608 _mesa_glsl_error(& loc, state,
2609 "`%s' cannot be marked invariant, fragment shader "
2610 "inputs only\n", var->name);
2611 }
2612 }
2613
2614 if (state->current_function != NULL) {
2615 const char *mode = NULL;
2616 const char *extra = "";
2617
2618 /* There is no need to check for 'inout' here because the parser will
2619 * only allow that in function parameter lists.
2620 */
2621 if (this->type->qualifier.flags.q.attribute) {
2622 mode = "attribute";
2623 } else if (this->type->qualifier.flags.q.uniform) {
2624 mode = "uniform";
2625 } else if (this->type->qualifier.flags.q.varying) {
2626 mode = "varying";
2627 } else if (this->type->qualifier.flags.q.in) {
2628 mode = "in";
2629 extra = " or in function parameter list";
2630 } else if (this->type->qualifier.flags.q.out) {
2631 mode = "out";
2632 extra = " or in function parameter list";
2633 }
2634
2635 if (mode) {
2636 _mesa_glsl_error(& loc, state,
2637 "%s variable `%s' must be declared at "
2638 "global scope%s",
2639 mode, var->name, extra);
2640 }
2641 } else if (var->mode == ir_var_in) {
2642 var->read_only = true;
2643
2644 if (state->target == vertex_shader) {
2645 bool error_emitted = false;
2646
2647 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
2648 *
2649 * "Vertex shader inputs can only be float, floating-point
2650 * vectors, matrices, signed and unsigned integers and integer
2651 * vectors. Vertex shader inputs can also form arrays of these
2652 * types, but not structures."
2653 *
2654 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
2655 *
2656 * "Vertex shader inputs can only be float, floating-point
2657 * vectors, matrices, signed and unsigned integers and integer
2658 * vectors. They cannot be arrays or structures."
2659 *
2660 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
2661 *
2662 * "The attribute qualifier can be used only with float,
2663 * floating-point vectors, and matrices. Attribute variables
2664 * cannot be declared as arrays or structures."
2665 */
2666 const glsl_type *check_type = var->type->is_array()
2667 ? var->type->fields.array : var->type;
2668
2669 switch (check_type->base_type) {
2670 case GLSL_TYPE_FLOAT:
2671 break;
2672 case GLSL_TYPE_UINT:
2673 case GLSL_TYPE_INT:
2674 if (state->language_version > 120)
2675 break;
2676 /* FALLTHROUGH */
2677 default:
2678 _mesa_glsl_error(& loc, state,
2679 "vertex shader input / attribute cannot have "
2680 "type %s`%s'",
2681 var->type->is_array() ? "array of " : "",
2682 check_type->name);
2683 error_emitted = true;
2684 }
2685
2686 if (!error_emitted && (state->language_version <= 130)
2687 && var->type->is_array()) {
2688 _mesa_glsl_error(& loc, state,
2689 "vertex shader input / attribute cannot have "
2690 "array type");
2691 error_emitted = true;
2692 }
2693 }
2694 }
2695
2696 /* Integer vertex outputs must be qualified with 'flat'.
2697 *
2698 * From section 4.3.6 of the GLSL 1.30 spec:
2699 * "If a vertex output is a signed or unsigned integer or integer
2700 * vector, then it must be qualified with the interpolation qualifier
2701 * flat."
2702 */
2703 if (state->language_version >= 130
2704 && state->target == vertex_shader
2705 && state->current_function == NULL
2706 && var->type->is_integer()
2707 && var->mode == ir_var_out
2708 && var->interpolation != INTERP_QUALIFIER_FLAT) {
2709
2710 _mesa_glsl_error(&loc, state, "If a vertex output is an integer, "
2711 "then it must be qualified with 'flat'");
2712 }
2713
2714
2715 /* Interpolation qualifiers cannot be applied to 'centroid' and
2716 * 'centroid varying'.
2717 *
2718 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
2719 * "interpolation qualifiers may only precede the qualifiers in,
2720 * centroid in, out, or centroid out in a declaration. They do not apply
2721 * to the deprecated storage qualifiers varying or centroid varying."
2722 */
2723 if (state->language_version >= 130
2724 && this->type->qualifier.has_interpolation()
2725 && this->type->qualifier.flags.q.varying) {
2726
2727 const char *i = this->type->qualifier.interpolation_string();
2728 assert(i != NULL);
2729 const char *s;
2730 if (this->type->qualifier.flags.q.centroid)
2731 s = "centroid varying";
2732 else
2733 s = "varying";
2734
2735 _mesa_glsl_error(&loc, state,
2736 "qualifier '%s' cannot be applied to the "
2737 "deprecated storage qualifier '%s'", i, s);
2738 }
2739
2740
2741 /* Interpolation qualifiers can only apply to vertex shader outputs and
2742 * fragment shader inputs.
2743 *
2744 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
2745 * "Outputs from a vertex shader (out) and inputs to a fragment
2746 * shader (in) can be further qualified with one or more of these
2747 * interpolation qualifiers"
2748 */
2749 if (state->language_version >= 130
2750 && this->type->qualifier.has_interpolation()) {
2751
2752 const char *i = this->type->qualifier.interpolation_string();
2753 assert(i != NULL);
2754
2755 switch (state->target) {
2756 case vertex_shader:
2757 if (this->type->qualifier.flags.q.in) {
2758 _mesa_glsl_error(&loc, state,
2759 "qualifier '%s' cannot be applied to vertex "
2760 "shader inputs", i);
2761 }
2762 break;
2763 case fragment_shader:
2764 if (this->type->qualifier.flags.q.out) {
2765 _mesa_glsl_error(&loc, state,
2766 "qualifier '%s' cannot be applied to fragment "
2767 "shader outputs", i);
2768 }
2769 break;
2770 default:
2771 assert(0);
2772 }
2773 }
2774
2775
2776 /* From section 4.3.4 of the GLSL 1.30 spec:
2777 * "It is an error to use centroid in in a vertex shader."
2778 */
2779 if (state->language_version >= 130
2780 && this->type->qualifier.flags.q.centroid
2781 && this->type->qualifier.flags.q.in
2782 && state->target == vertex_shader) {
2783
2784 _mesa_glsl_error(&loc, state,
2785 "'centroid in' cannot be used in a vertex shader");
2786 }
2787
2788
2789 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
2790 */
2791 if (this->type->specifier->precision != ast_precision_none
2792 && state->language_version != 100
2793 && state->language_version < 130) {
2794
2795 _mesa_glsl_error(&loc, state,
2796 "precision qualifiers are supported only in GLSL ES "
2797 "1.00, and GLSL 1.30 and later");
2798 }
2799
2800
2801 /* Precision qualifiers only apply to floating point and integer types.
2802 *
2803 * From section 4.5.2 of the GLSL 1.30 spec:
2804 * "Any floating point or any integer declaration can have the type
2805 * preceded by one of these precision qualifiers [...] Literal
2806 * constants do not have precision qualifiers. Neither do Boolean
2807 * variables.
2808 *
2809 * In GLSL ES, sampler types are also allowed.
2810 *
2811 * From page 87 of the GLSL ES spec:
2812 * "RESOLUTION: Allow sampler types to take a precision qualifier."
2813 */
2814 if (this->type->specifier->precision != ast_precision_none
2815 && !var->type->is_float()
2816 && !var->type->is_integer()
2817 && !(var->type->is_sampler() && state->es_shader)
2818 && !(var->type->is_array()
2819 && (var->type->fields.array->is_float()
2820 || var->type->fields.array->is_integer()))) {
2821
2822 _mesa_glsl_error(&loc, state,
2823 "precision qualifiers apply only to floating point"
2824 "%s types", state->es_shader ? ", integer, and sampler"
2825 : "and integer");
2826 }
2827
2828 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
2829 *
2830 * "[Sampler types] can only be declared as function
2831 * parameters or uniform variables (see Section 4.3.5
2832 * "Uniform")".
2833 */
2834 if (var_type->contains_sampler() &&
2835 !this->type->qualifier.flags.q.uniform) {
2836 _mesa_glsl_error(&loc, state, "samplers must be declared uniform");
2837 }
2838
2839 /* Process the initializer and add its instructions to a temporary
2840 * list. This list will be added to the instruction stream (below) after
2841 * the declaration is added. This is done because in some cases (such as
2842 * redeclarations) the declaration may not actually be added to the
2843 * instruction stream.
2844 */
2845 exec_list initializer_instructions;
2846 ir_variable *earlier = get_variable_being_redeclared(var, decl, state);
2847
2848 if (decl->initializer != NULL) {
2849 result = process_initializer((earlier == NULL) ? var : earlier,
2850 decl, this->type,
2851 &initializer_instructions, state);
2852 }
2853
2854 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
2855 *
2856 * "It is an error to write to a const variable outside of
2857 * its declaration, so they must be initialized when
2858 * declared."
2859 */
2860 if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) {
2861 _mesa_glsl_error(& loc, state,
2862 "const declaration of `%s' must be initialized",
2863 decl->identifier);
2864 }
2865
2866 /* If the declaration is not a redeclaration, there are a few additional
2867 * semantic checks that must be applied. In addition, variable that was
2868 * created for the declaration should be added to the IR stream.
2869 */
2870 if (earlier == NULL) {
2871 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
2872 *
2873 * "Identifiers starting with "gl_" are reserved for use by
2874 * OpenGL, and may not be declared in a shader as either a
2875 * variable or a function."
2876 */
2877 if (strncmp(decl->identifier, "gl_", 3) == 0)
2878 _mesa_glsl_error(& loc, state,
2879 "identifier `%s' uses reserved `gl_' prefix",
2880 decl->identifier);
2881 else if (strstr(decl->identifier, "__")) {
2882 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
2883 * spec:
2884 *
2885 * "In addition, all identifiers containing two
2886 * consecutive underscores (__) are reserved as
2887 * possible future keywords."
2888 */
2889 _mesa_glsl_error(& loc, state,
2890 "identifier `%s' uses reserved `__' string",
2891 decl->identifier);
2892 }
2893
2894 /* Add the variable to the symbol table. Note that the initializer's
2895 * IR was already processed earlier (though it hasn't been emitted
2896 * yet), without the variable in scope.
2897 *
2898 * This differs from most C-like languages, but it follows the GLSL
2899 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
2900 * spec:
2901 *
2902 * "Within a declaration, the scope of a name starts immediately
2903 * after the initializer if present or immediately after the name
2904 * being declared if not."
2905 */
2906 if (!state->symbols->add_variable(var)) {
2907 YYLTYPE loc = this->get_location();
2908 _mesa_glsl_error(&loc, state, "name `%s' already taken in the "
2909 "current scope", decl->identifier);
2910 continue;
2911 }
2912
2913 /* Push the variable declaration to the top. It means that all the
2914 * variable declarations will appear in a funny last-to-first order,
2915 * but otherwise we run into trouble if a function is prototyped, a
2916 * global var is decled, then the function is defined with usage of
2917 * the global var. See glslparsertest's CorrectModule.frag.
2918 */
2919 instructions->push_head(var);
2920 }
2921
2922 instructions->append_list(&initializer_instructions);
2923 }
2924
2925
2926 /* Generally, variable declarations do not have r-values. However,
2927 * one is used for the declaration in
2928 *
2929 * while (bool b = some_condition()) {
2930 * ...
2931 * }
2932 *
2933 * so we return the rvalue from the last seen declaration here.
2934 */
2935 return result;
2936 }
2937
2938
2939 ir_rvalue *
2940 ast_parameter_declarator::hir(exec_list *instructions,
2941 struct _mesa_glsl_parse_state *state)
2942 {
2943 void *ctx = state;
2944 const struct glsl_type *type;
2945 const char *name = NULL;
2946 YYLTYPE loc = this->get_location();
2947
2948 type = this->type->specifier->glsl_type(& name, state);
2949
2950 if (type == NULL) {
2951 if (name != NULL) {
2952 _mesa_glsl_error(& loc, state,
2953 "invalid type `%s' in declaration of `%s'",
2954 name, this->identifier);
2955 } else {
2956 _mesa_glsl_error(& loc, state,
2957 "invalid type in declaration of `%s'",
2958 this->identifier);
2959 }
2960
2961 type = glsl_type::error_type;
2962 }
2963
2964 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
2965 *
2966 * "Functions that accept no input arguments need not use void in the
2967 * argument list because prototypes (or definitions) are required and
2968 * therefore there is no ambiguity when an empty argument list "( )" is
2969 * declared. The idiom "(void)" as a parameter list is provided for
2970 * convenience."
2971 *
2972 * Placing this check here prevents a void parameter being set up
2973 * for a function, which avoids tripping up checks for main taking
2974 * parameters and lookups of an unnamed symbol.
2975 */
2976 if (type->is_void()) {
2977 if (this->identifier != NULL)
2978 _mesa_glsl_error(& loc, state,
2979 "named parameter cannot have type `void'");
2980
2981 is_void = true;
2982 return NULL;
2983 }
2984
2985 if (formal_parameter && (this->identifier == NULL)) {
2986 _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
2987 return NULL;
2988 }
2989
2990 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
2991 * call already handled the "vec4[..] foo" case.
2992 */
2993 if (this->is_array) {
2994 type = process_array_type(&loc, type, this->array_size, state);
2995 }
2996
2997 if (!type->is_error() && type->array_size() == 0) {
2998 _mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
2999 "a declared size.");
3000 type = glsl_type::error_type;
3001 }
3002
3003 is_void = false;
3004 ir_variable *var = new(ctx) ir_variable(type, this->identifier, ir_var_in);
3005
3006 /* Apply any specified qualifiers to the parameter declaration. Note that
3007 * for function parameters the default mode is 'in'.
3008 */
3009 apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
3010
3011 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3012 *
3013 * "Samplers cannot be treated as l-values; hence cannot be used
3014 * as out or inout function parameters, nor can they be assigned
3015 * into."
3016 */
3017 if ((var->mode == ir_var_inout || var->mode == ir_var_out)
3018 && type->contains_sampler()) {
3019 _mesa_glsl_error(&loc, state, "out and inout parameters cannot contain samplers");
3020 type = glsl_type::error_type;
3021 }
3022
3023 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
3024 *
3025 * "When calling a function, expressions that do not evaluate to
3026 * l-values cannot be passed to parameters declared as out or inout."
3027 *
3028 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
3029 *
3030 * "Other binary or unary expressions, non-dereferenced arrays,
3031 * function names, swizzles with repeated fields, and constants
3032 * cannot be l-values."
3033 *
3034 * So for GLSL 1.10, passing an array as an out or inout parameter is not
3035 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
3036 */
3037 if ((var->mode == ir_var_inout || var->mode == ir_var_out)
3038 && type->is_array() && state->language_version == 110) {
3039 _mesa_glsl_error(&loc, state, "Arrays cannot be out or inout parameters in GLSL 1.10");
3040 type = glsl_type::error_type;
3041 }
3042
3043 instructions->push_tail(var);
3044
3045 /* Parameter declarations do not have r-values.
3046 */
3047 return NULL;
3048 }
3049
3050
3051 void
3052 ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
3053 bool formal,
3054 exec_list *ir_parameters,
3055 _mesa_glsl_parse_state *state)
3056 {
3057 ast_parameter_declarator *void_param = NULL;
3058 unsigned count = 0;
3059
3060 foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
3061 param->formal_parameter = formal;
3062 param->hir(ir_parameters, state);
3063
3064 if (param->is_void)
3065 void_param = param;
3066
3067 count++;
3068 }
3069
3070 if ((void_param != NULL) && (count > 1)) {
3071 YYLTYPE loc = void_param->get_location();
3072
3073 _mesa_glsl_error(& loc, state,
3074 "`void' parameter must be only parameter");
3075 }
3076 }
3077
3078
3079 void
3080 emit_function(_mesa_glsl_parse_state *state, ir_function *f)
3081 {
3082 /* IR invariants disallow function declarations or definitions
3083 * nested within other function definitions. But there is no
3084 * requirement about the relative order of function declarations
3085 * and definitions with respect to one another. So simply insert
3086 * the new ir_function block at the end of the toplevel instruction
3087 * list.
3088 */
3089 state->toplevel_ir->push_tail(f);
3090 }
3091
3092
3093 ir_rvalue *
3094 ast_function::hir(exec_list *instructions,
3095 struct _mesa_glsl_parse_state *state)
3096 {
3097 void *ctx = state;
3098 ir_function *f = NULL;
3099 ir_function_signature *sig = NULL;
3100 exec_list hir_parameters;
3101
3102 const char *const name = identifier;
3103
3104 /* New functions are always added to the top-level IR instruction stream,
3105 * so this instruction list pointer is ignored. See also emit_function
3106 * (called below).
3107 */
3108 (void) instructions;
3109
3110 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
3111 *
3112 * "Function declarations (prototypes) cannot occur inside of functions;
3113 * they must be at global scope, or for the built-in functions, outside
3114 * the global scope."
3115 *
3116 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
3117 *
3118 * "User defined functions may only be defined within the global scope."
3119 *
3120 * Note that this language does not appear in GLSL 1.10.
3121 */
3122 if ((state->current_function != NULL) && (state->language_version != 110)) {
3123 YYLTYPE loc = this->get_location();
3124 _mesa_glsl_error(&loc, state,
3125 "declaration of function `%s' not allowed within "
3126 "function body", name);
3127 }
3128
3129 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
3130 *
3131 * "Identifiers starting with "gl_" are reserved for use by
3132 * OpenGL, and may not be declared in a shader as either a
3133 * variable or a function."
3134 */
3135 if (strncmp(name, "gl_", 3) == 0) {
3136 YYLTYPE loc = this->get_location();
3137 _mesa_glsl_error(&loc, state,
3138 "identifier `%s' uses reserved `gl_' prefix", name);
3139 }
3140
3141 /* Convert the list of function parameters to HIR now so that they can be
3142 * used below to compare this function's signature with previously seen
3143 * signatures for functions with the same name.
3144 */
3145 ast_parameter_declarator::parameters_to_hir(& this->parameters,
3146 is_definition,
3147 & hir_parameters, state);
3148
3149 const char *return_type_name;
3150 const glsl_type *return_type =
3151 this->return_type->specifier->glsl_type(& return_type_name, state);
3152
3153 if (!return_type) {
3154 YYLTYPE loc = this->get_location();
3155 _mesa_glsl_error(&loc, state,
3156 "function `%s' has undeclared return type `%s'",
3157 name, return_type_name);
3158 return_type = glsl_type::error_type;
3159 }
3160
3161 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
3162 * "No qualifier is allowed on the return type of a function."
3163 */
3164 if (this->return_type->has_qualifiers()) {
3165 YYLTYPE loc = this->get_location();
3166 _mesa_glsl_error(& loc, state,
3167 "function `%s' return type has qualifiers", name);
3168 }
3169
3170 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3171 *
3172 * "[Sampler types] can only be declared as function parameters
3173 * or uniform variables (see Section 4.3.5 "Uniform")".
3174 */
3175 if (return_type->contains_sampler()) {
3176 YYLTYPE loc = this->get_location();
3177 _mesa_glsl_error(&loc, state,
3178 "function `%s' return type can't contain a sampler",
3179 name);
3180 }
3181
3182 /* Verify that this function's signature either doesn't match a previously
3183 * seen signature for a function with the same name, or, if a match is found,
3184 * that the previously seen signature does not have an associated definition.
3185 */
3186 f = state->symbols->get_function(name);
3187 if (f != NULL && (state->es_shader || f->has_user_signature())) {
3188 sig = f->exact_matching_signature(&hir_parameters);
3189 if (sig != NULL) {
3190 const char *badvar = sig->qualifiers_match(&hir_parameters);
3191 if (badvar != NULL) {
3192 YYLTYPE loc = this->get_location();
3193
3194 _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
3195 "qualifiers don't match prototype", name, badvar);
3196 }
3197
3198 if (sig->return_type != return_type) {
3199 YYLTYPE loc = this->get_location();
3200
3201 _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
3202 "match prototype", name);
3203 }
3204
3205 if (is_definition && sig->is_defined) {
3206 YYLTYPE loc = this->get_location();
3207
3208 _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
3209 }
3210 }
3211 } else {
3212 f = new(ctx) ir_function(name);
3213 if (!state->symbols->add_function(f)) {
3214 /* This function name shadows a non-function use of the same name. */
3215 YYLTYPE loc = this->get_location();
3216
3217 _mesa_glsl_error(&loc, state, "function name `%s' conflicts with "
3218 "non-function", name);
3219 return NULL;
3220 }
3221
3222 emit_function(state, f);
3223 }
3224
3225 /* Verify the return type of main() */
3226 if (strcmp(name, "main") == 0) {
3227 if (! return_type->is_void()) {
3228 YYLTYPE loc = this->get_location();
3229
3230 _mesa_glsl_error(& loc, state, "main() must return void");
3231 }
3232
3233 if (!hir_parameters.is_empty()) {
3234 YYLTYPE loc = this->get_location();
3235
3236 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
3237 }
3238 }
3239
3240 /* Finish storing the information about this new function in its signature.
3241 */
3242 if (sig == NULL) {
3243 sig = new(ctx) ir_function_signature(return_type);
3244 f->add_signature(sig);
3245 }
3246
3247 sig->replace_parameters(&hir_parameters);
3248 signature = sig;
3249
3250 /* Function declarations (prototypes) do not have r-values.
3251 */
3252 return NULL;
3253 }
3254
3255
3256 ir_rvalue *
3257 ast_function_definition::hir(exec_list *instructions,
3258 struct _mesa_glsl_parse_state *state)
3259 {
3260 prototype->is_definition = true;
3261 prototype->hir(instructions, state);
3262
3263 ir_function_signature *signature = prototype->signature;
3264 if (signature == NULL)
3265 return NULL;
3266
3267 assert(state->current_function == NULL);
3268 state->current_function = signature;
3269 state->found_return = false;
3270
3271 /* Duplicate parameters declared in the prototype as concrete variables.
3272 * Add these to the symbol table.
3273 */
3274 state->symbols->push_scope();
3275 foreach_iter(exec_list_iterator, iter, signature->parameters) {
3276 ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
3277
3278 assert(var != NULL);
3279
3280 /* The only way a parameter would "exist" is if two parameters have
3281 * the same name.
3282 */
3283 if (state->symbols->name_declared_this_scope(var->name)) {
3284 YYLTYPE loc = this->get_location();
3285
3286 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
3287 } else {
3288 state->symbols->add_variable(var);
3289 }
3290 }
3291
3292 /* Convert the body of the function to HIR. */
3293 this->body->hir(&signature->body, state);
3294 signature->is_defined = true;
3295
3296 state->symbols->pop_scope();
3297
3298 assert(state->current_function == signature);
3299 state->current_function = NULL;
3300
3301 if (!signature->return_type->is_void() && !state->found_return) {
3302 YYLTYPE loc = this->get_location();
3303 _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
3304 "%s, but no return statement",
3305 signature->function_name(),
3306 signature->return_type->name);
3307 }
3308
3309 /* Function definitions do not have r-values.
3310 */
3311 return NULL;
3312 }
3313
3314
3315 ir_rvalue *
3316 ast_jump_statement::hir(exec_list *instructions,
3317 struct _mesa_glsl_parse_state *state)
3318 {
3319 void *ctx = state;
3320
3321 switch (mode) {
3322 case ast_return: {
3323 ir_return *inst;
3324 assert(state->current_function);
3325
3326 if (opt_return_value) {
3327 ir_rvalue *const ret = opt_return_value->hir(instructions, state);
3328
3329 /* The value of the return type can be NULL if the shader says
3330 * 'return foo();' and foo() is a function that returns void.
3331 *
3332 * NOTE: The GLSL spec doesn't say that this is an error. The type
3333 * of the return value is void. If the return type of the function is
3334 * also void, then this should compile without error. Seriously.
3335 */
3336 const glsl_type *const ret_type =
3337 (ret == NULL) ? glsl_type::void_type : ret->type;
3338
3339 /* Implicit conversions are not allowed for return values. */
3340 if (state->current_function->return_type != ret_type) {
3341 YYLTYPE loc = this->get_location();
3342
3343 _mesa_glsl_error(& loc, state,
3344 "`return' with wrong type %s, in function `%s' "
3345 "returning %s",
3346 ret_type->name,
3347 state->current_function->function_name(),
3348 state->current_function->return_type->name);
3349 }
3350
3351 inst = new(ctx) ir_return(ret);
3352 } else {
3353 if (state->current_function->return_type->base_type !=
3354 GLSL_TYPE_VOID) {
3355 YYLTYPE loc = this->get_location();
3356
3357 _mesa_glsl_error(& loc, state,
3358 "`return' with no value, in function %s returning "
3359 "non-void",
3360 state->current_function->function_name());
3361 }
3362 inst = new(ctx) ir_return;
3363 }
3364
3365 state->found_return = true;
3366 instructions->push_tail(inst);
3367 break;
3368 }
3369
3370 case ast_discard:
3371 if (state->target != fragment_shader) {
3372 YYLTYPE loc = this->get_location();
3373
3374 _mesa_glsl_error(& loc, state,
3375 "`discard' may only appear in a fragment shader");
3376 }
3377 instructions->push_tail(new(ctx) ir_discard);
3378 break;
3379
3380 case ast_break:
3381 case ast_continue:
3382 if (mode == ast_continue &&
3383 state->loop_nesting_ast == NULL) {
3384 YYLTYPE loc = this->get_location();
3385
3386 _mesa_glsl_error(& loc, state,
3387 "continue may only appear in a loop");
3388 } else if (mode == ast_break &&
3389 state->loop_nesting_ast == NULL &&
3390 state->switch_nesting_ast == NULL) {
3391 YYLTYPE loc = this->get_location();
3392
3393 _mesa_glsl_error(& loc, state,
3394 "break may only appear in a loop or a switch");
3395 } else {
3396 /* For a loop, inline the for loop expression again,
3397 * since we don't know where near the end of
3398 * the loop body the normal copy of it
3399 * is going to be placed.
3400 */
3401 if (state->loop_nesting_ast != NULL &&
3402 mode == ast_continue &&
3403 state->loop_nesting_ast->rest_expression) {
3404 state->loop_nesting_ast->rest_expression->hir(instructions,
3405 state);
3406 }
3407
3408 if (state->is_switch_innermost &&
3409 mode == ast_break) {
3410 /* Force break out of switch by setting is_break switch state.
3411 */
3412 ir_variable *const is_break_var = state->is_break_var;
3413 ir_dereference_variable *const deref_is_break_var =
3414 new(ctx) ir_dereference_variable(is_break_var);
3415 ir_constant *const true_val = new(ctx) ir_constant(true);
3416 ir_assignment *const set_break_var =
3417 new(ctx) ir_assignment(deref_is_break_var,
3418 true_val,
3419 NULL);
3420
3421 instructions->push_tail(set_break_var);
3422 }
3423 else {
3424 ir_loop_jump *const jump =
3425 new(ctx) ir_loop_jump((mode == ast_break)
3426 ? ir_loop_jump::jump_break
3427 : ir_loop_jump::jump_continue);
3428 instructions->push_tail(jump);
3429 }
3430 }
3431
3432 break;
3433 }
3434
3435 /* Jump instructions do not have r-values.
3436 */
3437 return NULL;
3438 }
3439
3440
3441 ir_rvalue *
3442 ast_selection_statement::hir(exec_list *instructions,
3443 struct _mesa_glsl_parse_state *state)
3444 {
3445 void *ctx = state;
3446
3447 ir_rvalue *const condition = this->condition->hir(instructions, state);
3448
3449 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
3450 *
3451 * "Any expression whose type evaluates to a Boolean can be used as the
3452 * conditional expression bool-expression. Vector types are not accepted
3453 * as the expression to if."
3454 *
3455 * The checks are separated so that higher quality diagnostics can be
3456 * generated for cases where both rules are violated.
3457 */
3458 if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
3459 YYLTYPE loc = this->condition->get_location();
3460
3461 _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
3462 "boolean");
3463 }
3464
3465 ir_if *const stmt = new(ctx) ir_if(condition);
3466
3467 if (then_statement != NULL) {
3468 state->symbols->push_scope();
3469 then_statement->hir(& stmt->then_instructions, state);
3470 state->symbols->pop_scope();
3471 }
3472
3473 if (else_statement != NULL) {
3474 state->symbols->push_scope();
3475 else_statement->hir(& stmt->else_instructions, state);
3476 state->symbols->pop_scope();
3477 }
3478
3479 instructions->push_tail(stmt);
3480
3481 /* if-statements do not have r-values.
3482 */
3483 return NULL;
3484 }
3485
3486
3487 ir_rvalue *
3488 ast_switch_statement::hir(exec_list *instructions,
3489 struct _mesa_glsl_parse_state *state)
3490 {
3491 void *ctx = state;
3492
3493 ir_rvalue *const test_expression =
3494 this->test_expression->hir(instructions, state);
3495
3496 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
3497 *
3498 * "The type of init-expression in a switch statement must be a
3499 * scalar integer."
3500 *
3501 * The checks are separated so that higher quality diagnostics can be
3502 * generated for cases where the rule is violated.
3503 */
3504 if (!test_expression->type->is_integer()) {
3505 YYLTYPE loc = this->test_expression->get_location();
3506
3507 _mesa_glsl_error(& loc,
3508 state,
3509 "switch-statement expression must be scalar "
3510 "integer");
3511 }
3512
3513 /* Track the switch-statement nesting in a stack-like manner.
3514 */
3515 ir_variable *saved_test_var = state->test_var;
3516 ir_variable *saved_is_fallthru_var = state->is_fallthru_var;
3517
3518 bool save_is_switch_innermost = state->is_switch_innermost;
3519 ast_switch_statement *saved_nesting_ast = state->switch_nesting_ast;
3520
3521 state->is_switch_innermost = true;
3522 state->switch_nesting_ast = this;
3523
3524 /* Initalize is_fallthru state to false.
3525 */
3526 ir_rvalue *const is_fallthru_val = new (ctx) ir_constant(false);
3527 state->is_fallthru_var = new(ctx) ir_variable(glsl_type::bool_type,
3528 "switch_is_fallthru_tmp",
3529 ir_var_temporary);
3530 instructions->push_tail(state->is_fallthru_var);
3531
3532 ir_dereference_variable *deref_is_fallthru_var =
3533 new(ctx) ir_dereference_variable(state->is_fallthru_var);
3534 instructions->push_tail(new(ctx) ir_assignment(deref_is_fallthru_var,
3535 is_fallthru_val,
3536 NULL));
3537
3538 /* Initalize is_break state to false.
3539 */
3540 ir_rvalue *const is_break_val = new (ctx) ir_constant(false);
3541 state->is_break_var = new(ctx) ir_variable(glsl_type::bool_type,
3542 "switch_is_break_tmp",
3543 ir_var_temporary);
3544 instructions->push_tail(state->is_break_var);
3545
3546 ir_dereference_variable *deref_is_break_var =
3547 new(ctx) ir_dereference_variable(state->is_break_var);
3548 instructions->push_tail(new(ctx) ir_assignment(deref_is_break_var,
3549 is_break_val,
3550 NULL));
3551
3552 /* Cache test expression.
3553 */
3554 test_to_hir(instructions, state);
3555
3556 /* Emit code for body of switch stmt.
3557 */
3558 body->hir(instructions, state);
3559
3560 /* Restore previous nesting before returning.
3561 */
3562 state->switch_nesting_ast = saved_nesting_ast;
3563 state->is_switch_innermost = save_is_switch_innermost;
3564
3565 state->test_var = saved_test_var;
3566 state->is_fallthru_var = saved_is_fallthru_var;
3567
3568 /* Switch statements do not have r-values.
3569 */
3570 return NULL;
3571 }
3572
3573
3574 void
3575 ast_switch_statement::test_to_hir(exec_list *instructions,
3576 struct _mesa_glsl_parse_state *state)
3577 {
3578 void *ctx = state;
3579
3580 /* Cache value of test expression.
3581 */
3582 ir_rvalue *const test_val =
3583 test_expression->hir(instructions,
3584 state);
3585
3586 state->test_var = new(ctx) ir_variable(glsl_type::int_type,
3587 "switch_test_tmp",
3588 ir_var_temporary);
3589 ir_dereference_variable *deref_test_var =
3590 new(ctx) ir_dereference_variable(state->test_var);
3591
3592 instructions->push_tail(state->test_var);
3593 instructions->push_tail(new(ctx) ir_assignment(deref_test_var,
3594 test_val,
3595 NULL));
3596 }
3597
3598
3599 ir_rvalue *
3600 ast_switch_body::hir(exec_list *instructions,
3601 struct _mesa_glsl_parse_state *state)
3602 {
3603 if (stmts != NULL)
3604 stmts->hir(instructions, state);
3605
3606 /* Switch bodies do not have r-values.
3607 */
3608 return NULL;
3609 }
3610
3611
3612 ir_rvalue *
3613 ast_case_statement_list::hir(exec_list *instructions,
3614 struct _mesa_glsl_parse_state *state)
3615 {
3616 foreach_list_typed (ast_case_statement, case_stmt, link, & this->cases)
3617 case_stmt->hir(instructions, state);
3618
3619 /* Case statements do not have r-values.
3620 */
3621 return NULL;
3622 }
3623
3624
3625 ir_rvalue *
3626 ast_case_statement::hir(exec_list *instructions,
3627 struct _mesa_glsl_parse_state *state)
3628 {
3629 labels->hir(instructions, state);
3630
3631 /* Conditionally set fallthru state based on break state.
3632 */
3633 ir_constant *const false_val = new(state) ir_constant(false);
3634 ir_dereference_variable *const deref_is_fallthru_var =
3635 new(state) ir_dereference_variable(state->is_fallthru_var);
3636 ir_dereference_variable *const deref_is_break_var =
3637 new(state) ir_dereference_variable(state->is_break_var);
3638 ir_assignment *const reset_fallthru_on_break =
3639 new(state) ir_assignment(deref_is_fallthru_var,
3640 false_val,
3641 deref_is_break_var);
3642 instructions->push_tail(reset_fallthru_on_break);
3643
3644 /* Guard case statements depending on fallthru state.
3645 */
3646 ir_dereference_variable *const deref_fallthru_guard =
3647 new(state) ir_dereference_variable(state->is_fallthru_var);
3648 ir_if *const test_fallthru = new(state) ir_if(deref_fallthru_guard);
3649
3650 foreach_list_typed (ast_node, stmt, link, & this->stmts)
3651 stmt->hir(& test_fallthru->then_instructions, state);
3652
3653 instructions->push_tail(test_fallthru);
3654
3655 /* Case statements do not have r-values.
3656 */
3657 return NULL;
3658 }
3659
3660
3661 ir_rvalue *
3662 ast_case_label_list::hir(exec_list *instructions,
3663 struct _mesa_glsl_parse_state *state)
3664 {
3665 foreach_list_typed (ast_case_label, label, link, & this->labels)
3666 label->hir(instructions, state);
3667
3668 /* Case labels do not have r-values.
3669 */
3670 return NULL;
3671 }
3672
3673
3674 ir_rvalue *
3675 ast_case_label::hir(exec_list *instructions,
3676 struct _mesa_glsl_parse_state *state)
3677 {
3678 void *ctx = state;
3679
3680 ir_dereference_variable *deref_fallthru_var =
3681 new(ctx) ir_dereference_variable(state->is_fallthru_var);
3682
3683 ir_rvalue *const true_val = new(ctx) ir_constant(true);
3684
3685 /* If not default case, ...
3686 */
3687 if (this->test_value != NULL) {
3688 /* Conditionally set fallthru state based on
3689 * comparison of cached test expression value to case label.
3690 */
3691 ir_rvalue *const test_val = this->test_value->hir(instructions, state);
3692
3693 ir_dereference_variable *deref_test_var =
3694 new(ctx) ir_dereference_variable(state->test_var);
3695
3696 ir_rvalue *const test_cond = new(ctx) ir_expression(ir_binop_all_equal,
3697 glsl_type::bool_type,
3698 test_val,
3699 deref_test_var);
3700
3701 ir_assignment *set_fallthru_on_test =
3702 new(ctx) ir_assignment(deref_fallthru_var,
3703 true_val,
3704 test_cond);
3705
3706 instructions->push_tail(set_fallthru_on_test);
3707 } else { /* default case */
3708 /* Set falltrhu state.
3709 */
3710 ir_assignment *set_fallthru =
3711 new(ctx) ir_assignment(deref_fallthru_var,
3712 true_val,
3713 NULL);
3714
3715 instructions->push_tail(set_fallthru);
3716 }
3717
3718 /* Case statements do not have r-values.
3719 */
3720 return NULL;
3721 }
3722
3723
3724 void
3725 ast_iteration_statement::condition_to_hir(ir_loop *stmt,
3726 struct _mesa_glsl_parse_state *state)
3727 {
3728 void *ctx = state;
3729
3730 if (condition != NULL) {
3731 ir_rvalue *const cond =
3732 condition->hir(& stmt->body_instructions, state);
3733
3734 if ((cond == NULL)
3735 || !cond->type->is_boolean() || !cond->type->is_scalar()) {
3736 YYLTYPE loc = condition->get_location();
3737
3738 _mesa_glsl_error(& loc, state,
3739 "loop condition must be scalar boolean");
3740 } else {
3741 /* As the first code in the loop body, generate a block that looks
3742 * like 'if (!condition) break;' as the loop termination condition.
3743 */
3744 ir_rvalue *const not_cond =
3745 new(ctx) ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond,
3746 NULL);
3747
3748 ir_if *const if_stmt = new(ctx) ir_if(not_cond);
3749
3750 ir_jump *const break_stmt =
3751 new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
3752
3753 if_stmt->then_instructions.push_tail(break_stmt);
3754 stmt->body_instructions.push_tail(if_stmt);
3755 }
3756 }
3757 }
3758
3759
3760 ir_rvalue *
3761 ast_iteration_statement::hir(exec_list *instructions,
3762 struct _mesa_glsl_parse_state *state)
3763 {
3764 void *ctx = state;
3765
3766 /* For-loops and while-loops start a new scope, but do-while loops do not.
3767 */
3768 if (mode != ast_do_while)
3769 state->symbols->push_scope();
3770
3771 if (init_statement != NULL)
3772 init_statement->hir(instructions, state);
3773
3774 ir_loop *const stmt = new(ctx) ir_loop();
3775 instructions->push_tail(stmt);
3776
3777 /* Track the current loop nesting.
3778 */
3779 ast_iteration_statement *nesting_ast = state->loop_nesting_ast;
3780
3781 state->loop_nesting_ast = this;
3782
3783 /* Likewise, indicate that following code is closest to a loop,
3784 * NOT closest to a switch.
3785 */
3786 bool saved_is_switch_innermost = state->is_switch_innermost;
3787 state->is_switch_innermost = false;
3788
3789 if (mode != ast_do_while)
3790 condition_to_hir(stmt, state);
3791
3792 if (body != NULL)
3793 body->hir(& stmt->body_instructions, state);
3794
3795 if (rest_expression != NULL)
3796 rest_expression->hir(& stmt->body_instructions, state);
3797
3798 if (mode == ast_do_while)
3799 condition_to_hir(stmt, state);
3800
3801 if (mode != ast_do_while)
3802 state->symbols->pop_scope();
3803
3804 /* Restore previous nesting before returning.
3805 */
3806 state->loop_nesting_ast = nesting_ast;
3807 state->is_switch_innermost = saved_is_switch_innermost;
3808
3809 /* Loops do not have r-values.
3810 */
3811 return NULL;
3812 }
3813
3814
3815 ir_rvalue *
3816 ast_type_specifier::hir(exec_list *instructions,
3817 struct _mesa_glsl_parse_state *state)
3818 {
3819 if (!this->is_precision_statement && this->structure == NULL)
3820 return NULL;
3821
3822 YYLTYPE loc = this->get_location();
3823
3824 if (this->precision != ast_precision_none
3825 && state->language_version != 100
3826 && state->language_version < 130) {
3827 _mesa_glsl_error(&loc, state,
3828 "precision qualifiers exist only in "
3829 "GLSL ES 1.00, and GLSL 1.30 and later");
3830 return NULL;
3831 }
3832 if (this->precision != ast_precision_none
3833 && this->structure != NULL) {
3834 _mesa_glsl_error(&loc, state,
3835 "precision qualifiers do not apply to structures");
3836 return NULL;
3837 }
3838
3839 /* If this is a precision statement, check that the type to which it is
3840 * applied is either float or int.
3841 *
3842 * From section 4.5.3 of the GLSL 1.30 spec:
3843 * "The precision statement
3844 * precision precision-qualifier type;
3845 * can be used to establish a default precision qualifier. The type
3846 * field can be either int or float [...]. Any other types or
3847 * qualifiers will result in an error.
3848 */
3849 if (this->is_precision_statement) {
3850 assert(this->precision != ast_precision_none);
3851 assert(this->structure == NULL); /* The check for structures was
3852 * performed above. */
3853 if (this->is_array) {
3854 _mesa_glsl_error(&loc, state,
3855 "default precision statements do not apply to "
3856 "arrays");
3857 return NULL;
3858 }
3859 if (this->type_specifier != ast_float
3860 && this->type_specifier != ast_int) {
3861 _mesa_glsl_error(&loc, state,
3862 "default precision statements apply only to types "
3863 "float and int");
3864 return NULL;
3865 }
3866
3867 /* FINISHME: Translate precision statements into IR. */
3868 return NULL;
3869 }
3870
3871 if (this->structure != NULL)
3872 return this->structure->hir(instructions, state);
3873
3874 return NULL;
3875 }
3876
3877
3878 ir_rvalue *
3879 ast_struct_specifier::hir(exec_list *instructions,
3880 struct _mesa_glsl_parse_state *state)
3881 {
3882 unsigned decl_count = 0;
3883
3884 /* Make an initial pass over the list of structure fields to determine how
3885 * many there are. Each element in this list is an ast_declarator_list.
3886 * This means that we actually need to count the number of elements in the
3887 * 'declarations' list in each of the elements.
3888 */
3889 foreach_list_typed (ast_declarator_list, decl_list, link,
3890 &this->declarations) {
3891 foreach_list_const (decl_ptr, & decl_list->declarations) {
3892 decl_count++;
3893 }
3894 }
3895
3896 /* Allocate storage for the structure fields and process the field
3897 * declarations. As the declarations are processed, try to also convert
3898 * the types to HIR. This ensures that structure definitions embedded in
3899 * other structure definitions are processed.
3900 */
3901 glsl_struct_field *const fields = ralloc_array(state, glsl_struct_field,
3902 decl_count);
3903
3904 unsigned i = 0;
3905 foreach_list_typed (ast_declarator_list, decl_list, link,
3906 &this->declarations) {
3907 const char *type_name;
3908
3909 decl_list->type->specifier->hir(instructions, state);
3910
3911 /* Section 10.9 of the GLSL ES 1.00 specification states that
3912 * embedded structure definitions have been removed from the language.
3913 */
3914 if (state->es_shader && decl_list->type->specifier->structure != NULL) {
3915 YYLTYPE loc = this->get_location();
3916 _mesa_glsl_error(&loc, state, "Embedded structure definitions are "
3917 "not allowed in GLSL ES 1.00.");
3918 }
3919
3920 const glsl_type *decl_type =
3921 decl_list->type->specifier->glsl_type(& type_name, state);
3922
3923 foreach_list_typed (ast_declaration, decl, link,
3924 &decl_list->declarations) {
3925 const struct glsl_type *field_type = decl_type;
3926 if (decl->is_array) {
3927 YYLTYPE loc = decl->get_location();
3928 field_type = process_array_type(&loc, decl_type, decl->array_size,
3929 state);
3930 }
3931 fields[i].type = (field_type != NULL)
3932 ? field_type : glsl_type::error_type;
3933 fields[i].name = decl->identifier;
3934 i++;
3935 }
3936 }
3937
3938 assert(i == decl_count);
3939
3940 const glsl_type *t =
3941 glsl_type::get_record_instance(fields, decl_count, this->name);
3942
3943 YYLTYPE loc = this->get_location();
3944 if (!state->symbols->add_type(name, t)) {
3945 _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
3946 } else {
3947 const glsl_type **s = reralloc(state, state->user_structures,
3948 const glsl_type *,
3949 state->num_user_structures + 1);
3950 if (s != NULL) {
3951 s[state->num_user_structures] = t;
3952 state->user_structures = s;
3953 state->num_user_structures++;
3954 }
3955 }
3956
3957 /* Structure type definitions do not have r-values.
3958 */
3959 return NULL;
3960 }