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