Perform constant folding on array indices.
[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 case ast_logic_xor:
766 case ast_logic_or:
767 op[0] = this->subexpressions[0]->hir(instructions, state);
768 op[1] = this->subexpressions[1]->hir(instructions, state);
769
770 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
771 YYLTYPE loc = this->subexpressions[0]->get_location();
772
773 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
774 operator_string(this->oper));
775 error_emitted = true;
776 }
777
778 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
779 YYLTYPE loc = this->subexpressions[1]->get_location();
780
781 _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean",
782 operator_string(this->oper));
783 error_emitted = true;
784 }
785
786 result = new ir_expression(operations[this->oper], glsl_type::bool_type,
787 op[0], op[1]);
788 type = glsl_type::bool_type;
789 break;
790
791 case ast_logic_not:
792 op[0] = this->subexpressions[0]->hir(instructions, state);
793
794 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
795 YYLTYPE loc = this->subexpressions[0]->get_location();
796
797 _mesa_glsl_error(& loc, state,
798 "operand of `!' must be scalar boolean");
799 error_emitted = true;
800 }
801
802 result = new ir_expression(operations[this->oper], glsl_type::bool_type,
803 op[0], NULL);
804 type = glsl_type::bool_type;
805 break;
806
807 case ast_mul_assign:
808 case ast_div_assign:
809 case ast_add_assign:
810 case ast_sub_assign: {
811 op[0] = this->subexpressions[0]->hir(instructions, state);
812 op[1] = this->subexpressions[1]->hir(instructions, state);
813
814 type = arithmetic_result_type(op[0], op[1],
815 (this->oper == ast_mul_assign),
816 state, & loc);
817
818 ir_rvalue *temp_rhs = new ir_expression(operations[this->oper], type,
819 op[0], op[1]);
820
821 result = do_assignment(instructions, state, op[0], temp_rhs,
822 this->subexpressions[0]->get_location());
823 type = result->type;
824 error_emitted = (op[0]->type->is_error());
825
826 /* GLSL 1.10 does not allow array assignment. However, we don't have to
827 * explicitly test for this because none of the binary expression
828 * operators allow array operands either.
829 */
830
831 break;
832 }
833
834 case ast_mod_assign: {
835 op[0] = this->subexpressions[0]->hir(instructions, state);
836 op[1] = this->subexpressions[1]->hir(instructions, state);
837
838 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
839
840 assert(operations[this->oper] == ir_binop_mod);
841
842 struct ir_rvalue *temp_rhs;
843 temp_rhs = new ir_expression(operations[this->oper], type,
844 op[0], op[1]);
845
846 result = do_assignment(instructions, state, op[0], temp_rhs,
847 this->subexpressions[0]->get_location());
848 type = result->type;
849 error_emitted = type->is_error();
850 break;
851 }
852
853 case ast_ls_assign:
854 case ast_rs_assign:
855 _mesa_glsl_error(& loc, state,
856 "FINISHME: implement bit-shift assignment operators");
857 error_emitted = true;
858 break;
859
860 case ast_and_assign:
861 case ast_xor_assign:
862 case ast_or_assign:
863 _mesa_glsl_error(& loc, state,
864 "FINISHME: implement logic assignment operators");
865 error_emitted = true;
866 break;
867
868 case ast_conditional: {
869 op[0] = this->subexpressions[0]->hir(instructions, state);
870
871 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
872 *
873 * "The ternary selection operator (?:). It operates on three
874 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
875 * first expression, which must result in a scalar Boolean."
876 */
877 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
878 YYLTYPE loc = this->subexpressions[0]->get_location();
879
880 _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean");
881 error_emitted = true;
882 }
883
884 /* The :? operator is implemented by generating an anonymous temporary
885 * followed by an if-statement. The last instruction in each branch of
886 * the if-statement assigns a value to the anonymous temporary. This
887 * temporary is the r-value of the expression.
888 */
889 ir_variable *const tmp = generate_temporary(glsl_type::error_type,
890 instructions, state);
891
892 ir_if *const stmt = new ir_if(op[0]);
893 instructions->push_tail(stmt);
894
895 op[1] = this->subexpressions[1]->hir(& stmt->then_instructions, state);
896 ir_dereference *const then_deref = new ir_dereference(tmp);
897 ir_assignment *const then_assign =
898 new ir_assignment(then_deref, op[1], NULL);
899 stmt->then_instructions.push_tail(then_assign);
900
901 op[2] = this->subexpressions[2]->hir(& stmt->else_instructions, state);
902 ir_dereference *const else_deref = new ir_dereference(tmp);
903 ir_assignment *const else_assign =
904 new ir_assignment(else_deref, op[2], NULL);
905 stmt->else_instructions.push_tail(else_assign);
906
907 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
908 *
909 * "The second and third expressions can be any type, as
910 * long their types match, or there is a conversion in
911 * Section 4.1.10 "Implicit Conversions" that can be applied
912 * to one of the expressions to make their types match. This
913 * resulting matching type is the type of the entire
914 * expression."
915 */
916 if ((!apply_implicit_conversion(op[1]->type, op[2], state)
917 && !apply_implicit_conversion(op[2]->type, op[1], state))
918 || (op[1]->type != op[2]->type)) {
919 YYLTYPE loc = this->subexpressions[1]->get_location();
920
921 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
922 "operator must have matching types.");
923 error_emitted = true;
924 } else {
925 tmp->type = op[1]->type;
926 }
927
928 result = new ir_dereference(tmp);
929 type = tmp->type;
930 break;
931 }
932
933 case ast_pre_inc:
934 case ast_pre_dec: {
935 op[0] = this->subexpressions[0]->hir(instructions, state);
936 if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
937 op[1] = new ir_constant(1.0f);
938 else
939 op[1] = new ir_constant(1);
940
941 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
942
943 struct ir_rvalue *temp_rhs;
944 temp_rhs = new ir_expression(operations[this->oper], type,
945 op[0], op[1]);
946
947 result = do_assignment(instructions, state, op[0], temp_rhs,
948 this->subexpressions[0]->get_location());
949 type = result->type;
950 error_emitted = op[0]->type->is_error();
951 break;
952 }
953
954 case ast_post_inc:
955 case ast_post_dec: {
956 op[0] = this->subexpressions[0]->hir(instructions, state);
957 if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
958 op[1] = new ir_constant(1.0f);
959 else
960 op[1] = new ir_constant(1);
961
962 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
963
964 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
965
966 struct ir_rvalue *temp_rhs;
967 temp_rhs = new ir_expression(operations[this->oper], type,
968 op[0], op[1]);
969
970 /* Get a temporary of a copy of the lvalue before it's modified.
971 * This may get thrown away later.
972 */
973 result = get_lvalue_copy(instructions, state, op[0],
974 this->subexpressions[0]->get_location());
975
976 (void)do_assignment(instructions, state, op[0], temp_rhs,
977 this->subexpressions[0]->get_location());
978
979 type = result->type;
980 error_emitted = op[0]->type->is_error();
981 break;
982 }
983
984 case ast_field_selection:
985 result = _mesa_ast_field_selection_to_hir(this, instructions, state);
986 type = result->type;
987 break;
988
989 case ast_array_index: {
990 YYLTYPE index_loc = subexpressions[1]->get_location();
991
992 op[0] = subexpressions[0]->hir(instructions, state);
993 op[1] = subexpressions[1]->hir(instructions, state);
994
995 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
996
997 ir_dereference *const lhs = op[0]->as_dereference();
998 ir_instruction *array;
999 if ((lhs != NULL)
1000 && (lhs->mode == ir_dereference::ir_reference_variable)) {
1001 result = new ir_dereference(lhs->var, op[1]);
1002
1003 delete op[0];
1004 array = lhs->var;
1005 } else {
1006 result = new ir_dereference(op[0], op[1]);
1007 array = op[0];
1008 }
1009
1010 /* Do not use op[0] after this point. Use array.
1011 */
1012 op[0] = NULL;
1013
1014
1015 if (error_emitted)
1016 break;
1017
1018 if (!array->type->is_array()
1019 && !array->type->is_matrix()
1020 && !array->type->is_vector()) {
1021 _mesa_glsl_error(& index_loc, state,
1022 "cannot dereference non-array / non-matrix / "
1023 "non-vector");
1024 error_emitted = true;
1025 }
1026
1027 if (!op[1]->type->is_integer()) {
1028 _mesa_glsl_error(& index_loc, state,
1029 "array index must be integer type");
1030 error_emitted = true;
1031 } else if (!op[1]->type->is_scalar()) {
1032 _mesa_glsl_error(& index_loc, state,
1033 "array index must be scalar");
1034 error_emitted = true;
1035 }
1036
1037 /* If the array index is a constant expression and the array has a
1038 * declared size, ensure that the access is in-bounds. If the array
1039 * index is not a constant expression, ensure that the array has a
1040 * declared size.
1041 */
1042 ir_constant *const const_index = op[1]->constant_expression_value();
1043 if (const_index != NULL) {
1044 const int idx = const_index->value.i[0];
1045 const char *type_name;
1046 unsigned bound = 0;
1047
1048 if (array->type->is_matrix()) {
1049 type_name = "matrix";
1050 } else if (array->type->is_vector()) {
1051 type_name = "vector";
1052 } else {
1053 type_name = "array";
1054 }
1055
1056 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
1057 *
1058 * "It is illegal to declare an array with a size, and then
1059 * later (in the same shader) index the same array with an
1060 * integral constant expression greater than or equal to the
1061 * declared size. It is also illegal to index an array with a
1062 * negative constant expression."
1063 */
1064 if (array->type->is_matrix()) {
1065 if (array->type->row_type()->vector_elements <= idx) {
1066 bound = array->type->row_type()->vector_elements;
1067 }
1068 } else if (array->type->is_vector()) {
1069 if (array->type->vector_elements <= idx) {
1070 bound = array->type->vector_elements;
1071 }
1072 } else {
1073 if ((array->type->array_size() > 0)
1074 && (array->type->array_size() <= idx)) {
1075 bound = array->type->array_size();
1076 }
1077 }
1078
1079 if (bound > 0) {
1080 _mesa_glsl_error(& loc, state, "%s index must be < %u",
1081 type_name, bound);
1082 error_emitted = true;
1083 } else if (idx < 0) {
1084 _mesa_glsl_error(& loc, state, "%s index must be >= 0",
1085 type_name);
1086 error_emitted = true;
1087 }
1088
1089 if (array->type->is_array()) {
1090 ir_variable *const v = array->as_variable();
1091 if ((v != NULL) && (unsigned(idx) > v->max_array_access))
1092 v->max_array_access = idx;
1093 }
1094 }
1095
1096 if (error_emitted)
1097 result->type = glsl_type::error_type;
1098
1099 type = result->type;
1100 break;
1101 }
1102
1103 case ast_function_call:
1104 /* Should *NEVER* get here. ast_function_call should always be handled
1105 * by ast_function_expression::hir.
1106 */
1107 assert(0);
1108 break;
1109
1110 case ast_identifier: {
1111 /* ast_identifier can appear several places in a full abstract syntax
1112 * tree. This particular use must be at location specified in the grammar
1113 * as 'variable_identifier'.
1114 */
1115 ir_variable *var =
1116 state->symbols->get_variable(this->primary_expression.identifier);
1117
1118 result = new ir_dereference(var);
1119
1120 if (var != NULL) {
1121 type = result->type;
1122 } else {
1123 _mesa_glsl_error(& loc, state, "`%s' undeclared",
1124 this->primary_expression.identifier);
1125
1126 error_emitted = true;
1127 }
1128 break;
1129 }
1130
1131 case ast_int_constant:
1132 type = glsl_type::int_type;
1133 result = new ir_constant(type, & this->primary_expression);
1134 break;
1135
1136 case ast_uint_constant:
1137 type = glsl_type::uint_type;
1138 result = new ir_constant(type, & this->primary_expression);
1139 break;
1140
1141 case ast_float_constant:
1142 type = glsl_type::float_type;
1143 result = new ir_constant(type, & this->primary_expression);
1144 break;
1145
1146 case ast_bool_constant:
1147 type = glsl_type::bool_type;
1148 result = new ir_constant(type, & this->primary_expression);
1149 break;
1150
1151 case ast_sequence: {
1152 struct simple_node *ptr;
1153
1154 /* It should not be possible to generate a sequence in the AST without
1155 * any expressions in it.
1156 */
1157 assert(!is_empty_list(&this->expressions));
1158
1159 /* The r-value of a sequence is the last expression in the sequence. If
1160 * the other expressions in the sequence do not have side-effects (and
1161 * therefore add instructions to the instruction list), they get dropped
1162 * on the floor.
1163 */
1164 foreach (ptr, &this->expressions)
1165 result = ((ast_node *)ptr)->hir(instructions, state);
1166
1167 type = result->type;
1168
1169 /* Any errors should have already been emitted in the loop above.
1170 */
1171 error_emitted = true;
1172 break;
1173 }
1174 }
1175
1176 if (type->is_error() && !error_emitted)
1177 _mesa_glsl_error(& loc, state, "type mismatch");
1178
1179 return result;
1180 }
1181
1182
1183 ir_rvalue *
1184 ast_expression_statement::hir(exec_list *instructions,
1185 struct _mesa_glsl_parse_state *state)
1186 {
1187 /* It is possible to have expression statements that don't have an
1188 * expression. This is the solitary semicolon:
1189 *
1190 * for (i = 0; i < 5; i++)
1191 * ;
1192 *
1193 * In this case the expression will be NULL. Test for NULL and don't do
1194 * anything in that case.
1195 */
1196 if (expression != NULL)
1197 expression->hir(instructions, state);
1198
1199 /* Statements do not have r-values.
1200 */
1201 return NULL;
1202 }
1203
1204
1205 ir_rvalue *
1206 ast_compound_statement::hir(exec_list *instructions,
1207 struct _mesa_glsl_parse_state *state)
1208 {
1209 struct simple_node *ptr;
1210
1211
1212 if (new_scope)
1213 state->symbols->push_scope();
1214
1215 foreach (ptr, &statements)
1216 ((ast_node *)ptr)->hir(instructions, state);
1217
1218 if (new_scope)
1219 state->symbols->pop_scope();
1220
1221 /* Compound statements do not have r-values.
1222 */
1223 return NULL;
1224 }
1225
1226
1227 static const glsl_type *
1228 process_array_type(const glsl_type *base, ast_node *array_size,
1229 struct _mesa_glsl_parse_state *state)
1230 {
1231 unsigned length = 0;
1232
1233 /* FINISHME: Reject delcarations of multidimensional arrays. */
1234
1235 if (array_size != NULL) {
1236 exec_list dummy_instructions;
1237 ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
1238 YYLTYPE loc = array_size->get_location();
1239
1240 /* FINISHME: Verify that the grammar forbids side-effects in array
1241 * FINISHME: sizes. i.e., 'vec4 [x = 12] data'
1242 */
1243 assert(dummy_instructions.is_empty());
1244
1245 if (ir != NULL) {
1246 if (!ir->type->is_integer()) {
1247 _mesa_glsl_error(& loc, state, "array size must be integer type");
1248 } else if (!ir->type->is_scalar()) {
1249 _mesa_glsl_error(& loc, state, "array size must be scalar type");
1250 } else {
1251 ir_constant *const size = ir->constant_expression_value();
1252
1253 if (size == NULL) {
1254 _mesa_glsl_error(& loc, state, "array size must be a "
1255 "constant valued expression");
1256 } else if (size->value.i[0] <= 0) {
1257 _mesa_glsl_error(& loc, state, "array size must be > 0");
1258 } else {
1259 assert(size->type == ir->type);
1260 length = size->value.u[0];
1261 }
1262 }
1263 }
1264 }
1265
1266 return glsl_type::get_array_instance(base, length);
1267 }
1268
1269
1270 const glsl_type *
1271 ast_type_specifier::glsl_type(const char **name,
1272 struct _mesa_glsl_parse_state *state) const
1273 {
1274 const struct glsl_type *type;
1275
1276 if (this->type_specifier == ast_struct) {
1277 /* FINISHME: Handle annonymous structures. */
1278 type = NULL;
1279 } else {
1280 type = state->symbols->get_type(this->type_name);
1281 *name = this->type_name;
1282
1283 if (this->is_array) {
1284 type = process_array_type(type, this->array_size, state);
1285 }
1286 }
1287
1288 return type;
1289 }
1290
1291
1292 static void
1293 apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
1294 struct ir_variable *var,
1295 struct _mesa_glsl_parse_state *state,
1296 YYLTYPE *loc)
1297 {
1298 if (qual->invariant)
1299 var->invariant = 1;
1300
1301 /* FINISHME: Mark 'in' variables at global scope as read-only. */
1302 if (qual->constant || qual->attribute || qual->uniform
1303 || (qual->varying && (state->target == fragment_shader)))
1304 var->read_only = 1;
1305
1306 if (qual->centroid)
1307 var->centroid = 1;
1308
1309 if (qual->attribute && state->target == fragment_shader) {
1310 var->type = glsl_type::error_type;
1311 _mesa_glsl_error(loc, state,
1312 "`attribute' variables may not be declared in the "
1313 "fragment shader");
1314 }
1315
1316 /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
1317 *
1318 * "The varying qualifier can be used only with the data types
1319 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
1320 * these."
1321 */
1322 if (qual->varying && var->type->base_type != GLSL_TYPE_FLOAT) {
1323 var->type = glsl_type::error_type;
1324 _mesa_glsl_error(loc, state,
1325 "varying variables must be of base type float");
1326 }
1327
1328 if (qual->in && qual->out)
1329 var->mode = ir_var_inout;
1330 else if (qual->attribute || qual->in
1331 || (qual->varying && (state->target == fragment_shader)))
1332 var->mode = ir_var_in;
1333 else if (qual->out || (qual->varying && (state->target == vertex_shader)))
1334 var->mode = ir_var_out;
1335 else if (qual->uniform)
1336 var->mode = ir_var_uniform;
1337 else
1338 var->mode = ir_var_auto;
1339
1340 if (qual->flat)
1341 var->interpolation = ir_var_flat;
1342 else if (qual->noperspective)
1343 var->interpolation = ir_var_noperspective;
1344 else
1345 var->interpolation = ir_var_smooth;
1346
1347 if (var->type->is_array() && (state->language_version >= 120)) {
1348 var->array_lvalue = true;
1349 }
1350 }
1351
1352
1353 ir_rvalue *
1354 ast_declarator_list::hir(exec_list *instructions,
1355 struct _mesa_glsl_parse_state *state)
1356 {
1357 struct simple_node *ptr;
1358 const struct glsl_type *decl_type;
1359 const char *type_name = NULL;
1360
1361
1362 /* FINISHME: Handle vertex shader "invariant" declarations that do not
1363 * FINISHME: include a type. These re-declare built-in variables to be
1364 * FINISHME: invariant.
1365 */
1366
1367 decl_type = this->type->specifier->glsl_type(& type_name, state);
1368
1369 foreach (ptr, &this->declarations) {
1370 struct ast_declaration *const decl = (struct ast_declaration * )ptr;
1371 const struct glsl_type *var_type;
1372 struct ir_variable *var;
1373 YYLTYPE loc = this->get_location();
1374
1375 /* FINISHME: Emit a warning if a variable declaration shadows a
1376 * FINISHME: declaration at a higher scope.
1377 */
1378
1379 if ((decl_type == NULL) || decl_type->is_void()) {
1380 if (type_name != NULL) {
1381 _mesa_glsl_error(& loc, state,
1382 "invalid type `%s' in declaration of `%s'",
1383 type_name, decl->identifier);
1384 } else {
1385 _mesa_glsl_error(& loc, state,
1386 "invalid type in declaration of `%s'",
1387 decl->identifier);
1388 }
1389 continue;
1390 }
1391
1392 if (decl->is_array) {
1393 var_type = process_array_type(decl_type, decl->array_size, state);
1394 } else {
1395 var_type = decl_type;
1396 }
1397
1398 var = new ir_variable(var_type, decl->identifier);
1399
1400 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
1401 *
1402 * "Global variables can only use the qualifiers const,
1403 * attribute, uni form, or varying. Only one may be
1404 * specified.
1405 *
1406 * Local variables can only use the qualifier const."
1407 *
1408 * This is relaxed in GLSL 1.30.
1409 */
1410 if (state->language_version < 120) {
1411 if (this->type->qualifier.out) {
1412 _mesa_glsl_error(& loc, state,
1413 "`out' qualifier in declaration of `%s' "
1414 "only valid for function parameters in GLSL 1.10.",
1415 decl->identifier);
1416 }
1417 if (this->type->qualifier.in) {
1418 _mesa_glsl_error(& loc, state,
1419 "`in' qualifier in declaration of `%s' "
1420 "only valid for function parameters in GLSL 1.10.",
1421 decl->identifier);
1422 }
1423 /* FINISHME: Test for other invalid qualifiers. */
1424 }
1425
1426 apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
1427 & loc);
1428
1429 /* Attempt to add the variable to the symbol table. If this fails, it
1430 * means the variable has already been declared at this scope. Arrays
1431 * fudge this rule a little bit.
1432 *
1433 * From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
1434 *
1435 * "It is legal to declare an array without a size and then
1436 * later re-declare the same name as an array of the same
1437 * type and specify a size."
1438 */
1439 if (state->symbols->name_declared_this_scope(decl->identifier)) {
1440 ir_variable *const earlier =
1441 state->symbols->get_variable(decl->identifier);
1442
1443 if ((earlier != NULL)
1444 && (earlier->type->array_size() == 0)
1445 && var->type->is_array()
1446 && (var->type->element_type() == earlier->type->element_type())) {
1447 /* FINISHME: This doesn't match the qualifiers on the two
1448 * FINISHME: declarations. It's not 100% clear whether this is
1449 * FINISHME: required or not.
1450 */
1451
1452 if (var->type->array_size() <= earlier->max_array_access) {
1453 YYLTYPE loc = this->get_location();
1454
1455 _mesa_glsl_error(& loc, state, "array size must be > %u due to "
1456 "previous access",
1457 earlier->max_array_access);
1458 }
1459
1460 earlier->type = var->type;
1461 delete var;
1462 var = NULL;
1463 } else {
1464 YYLTYPE loc = this->get_location();
1465
1466 _mesa_glsl_error(& loc, state, "`%s' redeclared",
1467 decl->identifier);
1468 }
1469
1470 continue;
1471 }
1472
1473 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
1474 *
1475 * "Identifiers starting with "gl_" are reserved for use by
1476 * OpenGL, and may not be declared in a shader as either a
1477 * variable or a function."
1478 */
1479 if (strncmp(decl->identifier, "gl_", 3) == 0) {
1480 /* FINISHME: This should only trigger if we're not redefining
1481 * FINISHME: a builtin (to add a qualifier, for example).
1482 */
1483 _mesa_glsl_error(& loc, state,
1484 "identifier `%s' uses reserved `gl_' prefix",
1485 decl->identifier);
1486 }
1487
1488 instructions->push_tail(var);
1489
1490 if (state->current_function != NULL) {
1491 const char *mode = NULL;
1492 const char *extra = "";
1493
1494 /* There is no need to check for 'inout' here because the parser will
1495 * only allow that in function parameter lists.
1496 */
1497 if (this->type->qualifier.attribute) {
1498 mode = "attribute";
1499 } else if (this->type->qualifier.uniform) {
1500 mode = "uniform";
1501 } else if (this->type->qualifier.varying) {
1502 mode = "varying";
1503 } else if (this->type->qualifier.in) {
1504 mode = "in";
1505 extra = " or in function parameter list";
1506 } else if (this->type->qualifier.out) {
1507 mode = "out";
1508 extra = " or in function parameter list";
1509 }
1510
1511 if (mode) {
1512 _mesa_glsl_error(& loc, state,
1513 "%s variable `%s' must be declared at "
1514 "global scope%s",
1515 mode, var->name, extra);
1516 }
1517 } else if (var->mode == ir_var_in) {
1518 if (state->target == vertex_shader) {
1519 bool error_emitted = false;
1520
1521 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1522 *
1523 * "Vertex shader inputs can only be float, floating-point
1524 * vectors, matrices, signed and unsigned integers and integer
1525 * vectors. Vertex shader inputs can also form arrays of these
1526 * types, but not structures."
1527 *
1528 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
1529 *
1530 * "Vertex shader inputs can only be float, floating-point
1531 * vectors, matrices, signed and unsigned integers and integer
1532 * vectors. They cannot be arrays or structures."
1533 *
1534 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1535 *
1536 * "The attribute qualifier can be used only with float,
1537 * floating-point vectors, and matrices. Attribute variables
1538 * cannot be declared as arrays or structures."
1539 */
1540 const glsl_type *check_type = var->type->is_array()
1541 ? var->type->fields.array : var->type;
1542
1543 switch (check_type->base_type) {
1544 case GLSL_TYPE_FLOAT:
1545 break;
1546 case GLSL_TYPE_UINT:
1547 case GLSL_TYPE_INT:
1548 if (state->language_version > 120)
1549 break;
1550 /* FALLTHROUGH */
1551 default:
1552 _mesa_glsl_error(& loc, state,
1553 "vertex shader input / attribute cannot have "
1554 "type %s`%s'",
1555 var->type->is_array() ? "array of " : "",
1556 check_type->name);
1557 error_emitted = true;
1558 }
1559
1560 if (!error_emitted && (state->language_version <= 130)
1561 && var->type->is_array()) {
1562 _mesa_glsl_error(& loc, state,
1563 "vertex shader input / attribute cannot have "
1564 "array type");
1565 error_emitted = true;
1566 }
1567 }
1568 }
1569
1570 if (decl->initializer != NULL) {
1571 YYLTYPE initializer_loc = decl->initializer->get_location();
1572
1573 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
1574 *
1575 * "All uniform variables are read-only and are initialized either
1576 * directly by an application via API commands, or indirectly by
1577 * OpenGL."
1578 */
1579 if ((state->language_version <= 110)
1580 && (var->mode == ir_var_uniform)) {
1581 _mesa_glsl_error(& initializer_loc, state,
1582 "cannot initialize uniforms in GLSL 1.10");
1583 }
1584
1585 if (var->type->is_sampler()) {
1586 _mesa_glsl_error(& initializer_loc, state,
1587 "cannot initialize samplers");
1588 }
1589
1590 if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
1591 _mesa_glsl_error(& initializer_loc, state,
1592 "cannot initialize %s shader input / %s",
1593 (state->target == vertex_shader)
1594 ? "vertex" : "fragment",
1595 (state->target == vertex_shader)
1596 ? "attribute" : "varying");
1597 }
1598
1599 ir_dereference *const lhs = new ir_dereference(var);
1600 ir_rvalue *rhs = decl->initializer->hir(instructions, state);
1601
1602 /* Calculate the constant value if this is a const
1603 * declaration.
1604 */
1605 if (this->type->qualifier.constant) {
1606 ir_constant *constant_value = rhs->constant_expression_value();
1607 if (!constant_value) {
1608 _mesa_glsl_error(& initializer_loc, state,
1609 "initializer of const variable `%s' must be a "
1610 "constant expression",
1611 decl->identifier);
1612 } else {
1613 rhs = constant_value;
1614 var->constant_value = constant_value;
1615 }
1616 }
1617
1618 if (rhs && !rhs->type->is_error()) {
1619 bool temp = var->read_only;
1620 if (this->type->qualifier.constant)
1621 var->read_only = false;
1622 (void) do_assignment(instructions, state, lhs, rhs,
1623 this->get_location());
1624 var->read_only = temp;
1625 }
1626 }
1627
1628 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
1629 *
1630 * "It is an error to write to a const variable outside of
1631 * its declaration, so they must be initialized when
1632 * declared."
1633 */
1634 if (this->type->qualifier.constant && decl->initializer == NULL) {
1635 _mesa_glsl_error(& loc, state,
1636 "const declaration of `%s' must be initialized");
1637 }
1638
1639 /* Add the vairable to the symbol table after processing the initializer.
1640 * This differs from most C-like languages, but it follows the GLSL
1641 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
1642 * spec:
1643 *
1644 * "Within a declaration, the scope of a name starts immediately
1645 * after the initializer if present or immediately after the name
1646 * being declared if not."
1647 */
1648 const bool added_variable =
1649 state->symbols->add_variable(decl->identifier, var);
1650 assert(added_variable);
1651 }
1652
1653 /* Variable declarations do not have r-values.
1654 */
1655 return NULL;
1656 }
1657
1658
1659 ir_rvalue *
1660 ast_parameter_declarator::hir(exec_list *instructions,
1661 struct _mesa_glsl_parse_state *state)
1662 {
1663 const struct glsl_type *type;
1664 const char *name = NULL;
1665 YYLTYPE loc = this->get_location();
1666
1667 type = this->type->specifier->glsl_type(& name, state);
1668
1669 if (type == NULL) {
1670 if (name != NULL) {
1671 _mesa_glsl_error(& loc, state,
1672 "invalid type `%s' in declaration of `%s'",
1673 name, this->identifier);
1674 } else {
1675 _mesa_glsl_error(& loc, state,
1676 "invalid type in declaration of `%s'",
1677 this->identifier);
1678 }
1679
1680 type = glsl_type::error_type;
1681 }
1682
1683 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
1684 *
1685 * "Functions that accept no input arguments need not use void in the
1686 * argument list because prototypes (or definitions) are required and
1687 * therefore there is no ambiguity when an empty argument list "( )" is
1688 * declared. The idiom "(void)" as a parameter list is provided for
1689 * convenience."
1690 *
1691 * Placing this check here prevents a void parameter being set up
1692 * for a function, which avoids tripping up checks for main taking
1693 * parameters and lookups of an unnamed symbol.
1694 */
1695 if (type->is_void()) {
1696 if (this->identifier != NULL)
1697 _mesa_glsl_error(& loc, state,
1698 "named parameter cannot have type `void'");
1699
1700 is_void = true;
1701 return NULL;
1702 }
1703
1704 if (formal_parameter && (this->identifier == NULL)) {
1705 _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
1706 return NULL;
1707 }
1708
1709 is_void = false;
1710 ir_variable *var = new ir_variable(type, this->identifier);
1711
1712 /* FINISHME: Handle array declarations. Note that this requires
1713 * FINISHME: complete handling of constant expressions.
1714 */
1715
1716 /* Apply any specified qualifiers to the parameter declaration. Note that
1717 * for function parameters the default mode is 'in'.
1718 */
1719 apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
1720 if (var->mode == ir_var_auto)
1721 var->mode = ir_var_in;
1722
1723 instructions->push_tail(var);
1724
1725 /* Parameter declarations do not have r-values.
1726 */
1727 return NULL;
1728 }
1729
1730
1731 void
1732 ast_parameter_declarator::parameters_to_hir(struct simple_node *ast_parameters,
1733 bool formal,
1734 exec_list *ir_parameters,
1735 _mesa_glsl_parse_state *state)
1736 {
1737 struct simple_node *ptr;
1738 ast_parameter_declarator *void_param = NULL;
1739 unsigned count = 0;
1740
1741 foreach (ptr, ast_parameters) {
1742 ast_parameter_declarator *param = (ast_parameter_declarator *)ptr;
1743 param->formal_parameter = formal;
1744 param->hir(ir_parameters, state);
1745
1746 if (param->is_void)
1747 void_param = param;
1748
1749 count++;
1750 }
1751
1752 if ((void_param != NULL) && (count > 1)) {
1753 YYLTYPE loc = void_param->get_location();
1754
1755 _mesa_glsl_error(& loc, state,
1756 "`void' parameter must be only parameter");
1757 }
1758 }
1759
1760
1761 static bool
1762 parameter_lists_match(exec_list *list_a, exec_list *list_b)
1763 {
1764 exec_list_iterator iter_a = list_a->iterator();
1765 exec_list_iterator iter_b = list_b->iterator();
1766
1767 while (iter_a.has_next()) {
1768 ir_variable *a = (ir_variable *)iter_a.get();
1769 ir_variable *b = (ir_variable *)iter_b.get();
1770
1771 /* If all of the parameters from the other parameter list have been
1772 * exhausted, the lists have different length and, by definition,
1773 * do not match.
1774 */
1775 if (!iter_b.has_next())
1776 return false;
1777
1778 /* If the types of the parameters do not match, the parameters lists
1779 * are different.
1780 */
1781 if (a->type != b->type)
1782 return false;
1783
1784 iter_a.next();
1785 iter_b.next();
1786 }
1787
1788 return true;
1789 }
1790
1791
1792 ir_rvalue *
1793 ast_function::hir(exec_list *instructions,
1794 struct _mesa_glsl_parse_state *state)
1795 {
1796 ir_function *f = NULL;
1797 ir_function_signature *sig = NULL;
1798 exec_list hir_parameters;
1799
1800
1801 /* The prototype part of a function does not generate anything in the IR
1802 * instruction stream.
1803 */
1804 (void) instructions;
1805
1806 /* Convert the list of function parameters to HIR now so that they can be
1807 * used below to compare this function's signature with previously seen
1808 * signatures for functions with the same name.
1809 */
1810 ast_parameter_declarator::parameters_to_hir(& this->parameters,
1811 is_definition,
1812 & hir_parameters, state);
1813
1814 const char *return_type_name;
1815 const glsl_type *return_type =
1816 this->return_type->specifier->glsl_type(& return_type_name, state);
1817
1818 assert(return_type != NULL);
1819
1820 /* Verify that this function's signature either doesn't match a previously
1821 * seen signature for a function with the same name, or, if a match is found,
1822 * that the previously seen signature does not have an associated definition.
1823 */
1824 const char *const name = identifier;
1825 f = state->symbols->get_function(name);
1826 if (f != NULL) {
1827 foreach_iter(exec_list_iterator, iter, *f) {
1828 sig = (struct ir_function_signature *) iter.get();
1829
1830 /* Compare the parameter list of the function being defined to the
1831 * existing function. If the parameter lists match, then the return
1832 * type must also match and the existing function must not have a
1833 * definition.
1834 */
1835 if (parameter_lists_match(& hir_parameters, & sig->parameters)) {
1836 /* FINISHME: Compare return types. */
1837
1838 if (is_definition && (sig->definition != NULL)) {
1839 YYLTYPE loc = this->get_location();
1840
1841 _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
1842 sig = NULL;
1843 break;
1844 }
1845 }
1846
1847 sig = NULL;
1848 }
1849
1850 } else if (state->symbols->name_declared_this_scope(name)) {
1851 /* This function name shadows a non-function use of the same name.
1852 */
1853 YYLTYPE loc = this->get_location();
1854
1855 _mesa_glsl_error(& loc, state, "function name `%s' conflicts with "
1856 "non-function", name);
1857 sig = NULL;
1858 } else {
1859 f = new ir_function(name);
1860 state->symbols->add_function(f->name, f);
1861 }
1862
1863 /* Verify the return type of main() */
1864 if (strcmp(name, "main") == 0) {
1865 if (! return_type->is_void()) {
1866 YYLTYPE loc = this->get_location();
1867
1868 _mesa_glsl_error(& loc, state, "main() must return void");
1869 }
1870
1871 if (!hir_parameters.is_empty()) {
1872 YYLTYPE loc = this->get_location();
1873
1874 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
1875 }
1876 }
1877
1878 /* Finish storing the information about this new function in its signature.
1879 */
1880 if (sig == NULL) {
1881 sig = new ir_function_signature(return_type);
1882 f->add_signature(sig);
1883 } else if (is_definition) {
1884 /* Destroy all of the previous parameter information. The previous
1885 * parameter information comes from the function prototype, and it can
1886 * either include invalid parameter names or may not have names at all.
1887 */
1888 foreach_iter(exec_list_iterator, iter, sig->parameters) {
1889 assert(((ir_instruction *) iter.get())->as_variable() != NULL);
1890
1891 iter.remove();
1892 delete iter.get();
1893 }
1894 }
1895
1896 hir_parameters.move_nodes_to(& sig->parameters);
1897 signature = sig;
1898
1899 /* Function declarations (prototypes) do not have r-values.
1900 */
1901 return NULL;
1902 }
1903
1904
1905 ir_rvalue *
1906 ast_function_definition::hir(exec_list *instructions,
1907 struct _mesa_glsl_parse_state *state)
1908 {
1909 prototype->is_definition = true;
1910 prototype->hir(instructions, state);
1911
1912 ir_function_signature *signature = prototype->signature;
1913
1914 assert(state->current_function == NULL);
1915 state->current_function = signature;
1916
1917 ir_label *label = new ir_label(signature->function_name());
1918 if (signature->definition == NULL) {
1919 signature->definition = label;
1920 }
1921 instructions->push_tail(label);
1922
1923 /* Duplicate parameters declared in the prototype as concrete variables.
1924 * Add these to the symbol table.
1925 */
1926 state->symbols->push_scope();
1927 foreach_iter(exec_list_iterator, iter, signature->parameters) {
1928 ir_variable *const proto = ((ir_instruction *) iter.get())->as_variable();
1929
1930 assert(proto != NULL);
1931
1932 ir_variable *const var = proto->clone();
1933
1934 instructions->push_tail(var);
1935
1936 /* The only way a parameter would "exist" is if two parameters have
1937 * the same name.
1938 */
1939 if (state->symbols->name_declared_this_scope(var->name)) {
1940 YYLTYPE loc = this->get_location();
1941
1942 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
1943 } else {
1944 state->symbols->add_variable(var->name, var);
1945 }
1946 }
1947
1948 /* Convert the body of the function to HIR, and append the resulting
1949 * instructions to the list that currently consists of the function label
1950 * and the function parameters.
1951 */
1952 this->body->hir(instructions, state);
1953
1954 state->symbols->pop_scope();
1955
1956 assert(state->current_function == signature);
1957 state->current_function = NULL;
1958
1959 /* Function definitions do not have r-values.
1960 */
1961 return NULL;
1962 }
1963
1964
1965 ir_rvalue *
1966 ast_jump_statement::hir(exec_list *instructions,
1967 struct _mesa_glsl_parse_state *state)
1968 {
1969
1970 if (mode == ast_return) {
1971 ir_return *inst;
1972 assert(state->current_function);
1973
1974 if (opt_return_value) {
1975 if (state->current_function->return_type->base_type ==
1976 GLSL_TYPE_VOID) {
1977 YYLTYPE loc = this->get_location();
1978
1979 _mesa_glsl_error(& loc, state,
1980 "`return` with a value, in function `%s' "
1981 "returning void",
1982 state->current_function->definition->label);
1983 }
1984
1985 ir_expression *const ret = (ir_expression *)
1986 opt_return_value->hir(instructions, state);
1987 assert(ret != NULL);
1988
1989 /* FINISHME: Make sure the type of the return value matches the return
1990 * FINISHME: type of the enclosing function.
1991 */
1992
1993 inst = new ir_return(ret);
1994 } else {
1995 if (state->current_function->return_type->base_type !=
1996 GLSL_TYPE_VOID) {
1997 YYLTYPE loc = this->get_location();
1998
1999 _mesa_glsl_error(& loc, state,
2000 "`return' with no value, in function %s returning "
2001 "non-void",
2002 state->current_function->definition->label);
2003 }
2004 inst = new ir_return;
2005 }
2006
2007 instructions->push_tail(inst);
2008 }
2009
2010 if (mode == ast_discard) {
2011 /* FINISHME: discard support */
2012 if (state->target != fragment_shader) {
2013 YYLTYPE loc = this->get_location();
2014
2015 _mesa_glsl_error(& loc, state,
2016 "`discard' may only appear in a fragment shader");
2017 }
2018 }
2019
2020 /* Jump instructions do not have r-values.
2021 */
2022 return NULL;
2023 }
2024
2025
2026 ir_rvalue *
2027 ast_selection_statement::hir(exec_list *instructions,
2028 struct _mesa_glsl_parse_state *state)
2029 {
2030 ir_rvalue *const condition = this->condition->hir(instructions, state);
2031
2032 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
2033 *
2034 * "Any expression whose type evaluates to a Boolean can be used as the
2035 * conditional expression bool-expression. Vector types are not accepted
2036 * as the expression to if."
2037 *
2038 * The checks are separated so that higher quality diagnostics can be
2039 * generated for cases where both rules are violated.
2040 */
2041 if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
2042 YYLTYPE loc = this->condition->get_location();
2043
2044 _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
2045 "boolean");
2046 }
2047
2048 ir_if *const stmt = new ir_if(condition);
2049
2050 if (then_statement != NULL) {
2051 ast_node *node = (ast_node *) then_statement;
2052 do {
2053 node->hir(& stmt->then_instructions, state);
2054 node = (ast_node *) node->next;
2055 } while (node != then_statement);
2056 }
2057
2058 if (else_statement != NULL) {
2059 ast_node *node = (ast_node *) else_statement;
2060 do {
2061 node->hir(& stmt->else_instructions, state);
2062 node = (ast_node *) node->next;
2063 } while (node != else_statement);
2064 }
2065
2066 instructions->push_tail(stmt);
2067
2068 /* if-statements do not have r-values.
2069 */
2070 return NULL;
2071 }