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