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