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