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