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