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