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