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