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