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