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