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