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