Add PASS / FAIL annotations to tests missing them.
[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 struct simple_node *ptr;
63
64 _mesa_glsl_initialize_variables(instructions, state);
65 _mesa_glsl_initialize_constructors(instructions, state);
66 _mesa_glsl_initialize_functions(instructions, state);
67
68 state->current_function = NULL;
69
70 foreach (ptr, & state->translation_unit) {
71 ((ast_node *)ptr)->hir(instructions, state);
72 }
73 }
74
75
76 /**
77 * If a conversion is available, convert one operand to a different type
78 *
79 * The \c from \c ir_rvalue is converted "in place".
80 *
81 * \param to Type that the operand it to be converted to
82 * \param from Operand that is being converted
83 * \param state GLSL compiler state
84 *
85 * \return
86 * If a conversion is possible (or unnecessary), \c true is returned.
87 * Otherwise \c false is returned.
88 */
89 static bool
90 apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
91 struct _mesa_glsl_parse_state *state)
92 {
93 if (to->base_type == from->type->base_type)
94 return true;
95
96 /* This conversion was added in GLSL 1.20. If the compilation mode is
97 * GLSL 1.10, the conversion is skipped.
98 */
99 if (state->language_version < 120)
100 return false;
101
102 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
103 *
104 * "There are no implicit array or structure conversions. For
105 * example, an array of int cannot be implicitly converted to an
106 * array of float. There are no implicit conversions between
107 * signed and unsigned integers."
108 */
109 /* FINISHME: The above comment is partially a lie. There is int/uint
110 * FINISHME: conversion for immediate constants.
111 */
112 if (!to->is_float() || !from->type->is_numeric())
113 return false;
114
115 switch (from->type->base_type) {
116 case GLSL_TYPE_INT:
117 from = new ir_expression(ir_unop_i2f, to, from, NULL);
118 break;
119 case GLSL_TYPE_UINT:
120 from = new ir_expression(ir_unop_u2f, to, from, NULL);
121 break;
122 case GLSL_TYPE_BOOL:
123 assert(!"FINISHME: Convert bool to float.");
124 default:
125 assert(0);
126 }
127
128 return true;
129 }
130
131
132 static const struct glsl_type *
133 arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
134 bool multiply,
135 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
136 {
137 const glsl_type *const type_a = value_a->type;
138 const glsl_type *const type_b = value_b->type;
139
140 /* From GLSL 1.50 spec, page 56:
141 *
142 * "The arithmetic binary operators add (+), subtract (-),
143 * multiply (*), and divide (/) operate on integer and
144 * floating-point scalars, vectors, and matrices."
145 */
146 if (!type_a->is_numeric() || !type_b->is_numeric()) {
147 _mesa_glsl_error(loc, state,
148 "Operands to arithmetic operators must be numeric");
149 return glsl_type::error_type;
150 }
151
152
153 /* "If one operand is floating-point based and the other is
154 * not, then the conversions from Section 4.1.10 "Implicit
155 * Conversions" are applied to the non-floating-point-based operand."
156 */
157 if (!apply_implicit_conversion(type_a, value_b, state)
158 && !apply_implicit_conversion(type_b, value_a, state)) {
159 _mesa_glsl_error(loc, state,
160 "Could not implicitly convert operands to "
161 "arithmetic operator");
162 return glsl_type::error_type;
163 }
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 *const type_a = value_a->type;
368 const glsl_type *const 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
397 if (type_a->base_type != type_b->base_type) {
398 _mesa_glsl_error(loc, state, "base type mismatch");
399 return glsl_type::error_type;
400 }
401
402 /* "The result is scalar Boolean."
403 */
404 return glsl_type::bool_type;
405 }
406
407
408 /**
409 * Validates that a value can be assigned to a location with a specified type
410 *
411 * Validates that \c rhs can be assigned to some location. If the types are
412 * not an exact match but an automatic conversion is possible, \c rhs will be
413 * converted.
414 *
415 * \return
416 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
417 * Otherwise the actual RHS to be assigned will be returned. This may be
418 * \c rhs, or it may be \c rhs after some type conversion.
419 *
420 * \note
421 * In addition to being used for assignments, this function is used to
422 * type-check return values.
423 */
424 ir_rvalue *
425 validate_assignment(const glsl_type *lhs_type, ir_rvalue *rhs)
426 {
427 const glsl_type *const rhs_type = rhs->type;
428
429 /* If there is already some error in the RHS, just return it. Anything
430 * else will lead to an avalanche of error message back to the user.
431 */
432 if (rhs_type->is_error())
433 return rhs;
434
435 /* FINISHME: For GLSL 1.10, check that the types are not arrays. */
436
437 /* If the types are identical, the assignment can trivially proceed.
438 */
439 if (rhs_type == lhs_type)
440 return rhs;
441
442 /* FINISHME: Check for and apply automatic conversions. */
443 return NULL;
444 }
445
446 ir_rvalue *
447 do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
448 ir_rvalue *lhs, ir_rvalue *rhs,
449 YYLTYPE lhs_loc)
450 {
451 bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
452
453 if (!error_emitted) {
454 /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
455 if (!lhs->is_lvalue()) {
456 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
457 error_emitted = true;
458 }
459 }
460
461 ir_rvalue *new_rhs = validate_assignment(lhs->type, rhs);
462 if (new_rhs == NULL) {
463 _mesa_glsl_error(& lhs_loc, state, "type mismatch");
464 } else {
465 rhs = new_rhs;
466 }
467
468 ir_instruction *tmp = new ir_assignment(lhs, rhs, NULL);
469 instructions->push_tail(tmp);
470
471 return rhs;
472 }
473
474
475 /**
476 * Generate a new temporary and add its declaration to the instruction stream
477 */
478 static ir_variable *
479 generate_temporary(const glsl_type *type, exec_list *instructions,
480 struct _mesa_glsl_parse_state *state)
481 {
482 char *name = (char *) malloc(sizeof(char) * 13);
483
484 snprintf(name, 13, "tmp_%08X", state->temp_index);
485 state->temp_index++;
486
487 ir_variable *const var = new ir_variable(type, name);
488 instructions->push_tail(var);
489
490 return var;
491 }
492
493
494 static ir_rvalue *
495 get_lvalue_copy(exec_list *instructions, struct _mesa_glsl_parse_state *state,
496 ir_rvalue *lvalue, YYLTYPE loc)
497 {
498 ir_variable *var;
499 ir_rvalue *var_deref;
500
501 /* FINISHME: Give unique names to the temporaries. */
502 var = new ir_variable(lvalue->type, "_internal_tmp");
503 var->mode = ir_var_auto;
504
505 var_deref = new ir_dereference(var);
506 do_assignment(instructions, state, var_deref, lvalue, loc);
507
508 /* Once we've created this temporary, mark it read only so it's no
509 * longer considered an lvalue.
510 */
511 var->read_only = true;
512
513 return var_deref;
514 }
515
516
517 ir_rvalue *
518 ast_node::hir(exec_list *instructions,
519 struct _mesa_glsl_parse_state *state)
520 {
521 (void) instructions;
522 (void) state;
523
524 return NULL;
525 }
526
527
528 ir_rvalue *
529 ast_expression::hir(exec_list *instructions,
530 struct _mesa_glsl_parse_state *state)
531 {
532 static const int operations[AST_NUM_OPERATORS] = {
533 -1, /* ast_assign doesn't convert to ir_expression. */
534 -1, /* ast_plus doesn't convert to ir_expression. */
535 ir_unop_neg,
536 ir_binop_add,
537 ir_binop_sub,
538 ir_binop_mul,
539 ir_binop_div,
540 ir_binop_mod,
541 ir_binop_lshift,
542 ir_binop_rshift,
543 ir_binop_less,
544 ir_binop_greater,
545 ir_binop_lequal,
546 ir_binop_gequal,
547 ir_binop_equal,
548 ir_binop_nequal,
549 ir_binop_bit_and,
550 ir_binop_bit_xor,
551 ir_binop_bit_or,
552 ir_unop_bit_not,
553 ir_binop_logic_and,
554 ir_binop_logic_xor,
555 ir_binop_logic_or,
556 ir_unop_logic_not,
557
558 /* Note: The following block of expression types actually convert
559 * to multiple IR instructions.
560 */
561 ir_binop_mul, /* ast_mul_assign */
562 ir_binop_div, /* ast_div_assign */
563 ir_binop_mod, /* ast_mod_assign */
564 ir_binop_add, /* ast_add_assign */
565 ir_binop_sub, /* ast_sub_assign */
566 ir_binop_lshift, /* ast_ls_assign */
567 ir_binop_rshift, /* ast_rs_assign */
568 ir_binop_bit_and, /* ast_and_assign */
569 ir_binop_bit_xor, /* ast_xor_assign */
570 ir_binop_bit_or, /* ast_or_assign */
571
572 -1, /* ast_conditional doesn't convert to ir_expression. */
573 ir_binop_add, /* ast_pre_inc. */
574 ir_binop_sub, /* ast_pre_dec. */
575 ir_binop_add, /* ast_post_inc. */
576 ir_binop_sub, /* ast_post_dec. */
577 -1, /* ast_field_selection doesn't conv to ir_expression. */
578 -1, /* ast_array_index doesn't convert to ir_expression. */
579 -1, /* ast_function_call doesn't conv to ir_expression. */
580 -1, /* ast_identifier doesn't convert to ir_expression. */
581 -1, /* ast_int_constant doesn't convert to ir_expression. */
582 -1, /* ast_uint_constant doesn't conv to ir_expression. */
583 -1, /* ast_float_constant doesn't conv to ir_expression. */
584 -1, /* ast_bool_constant doesn't conv to ir_expression. */
585 -1, /* ast_sequence doesn't convert to ir_expression. */
586 };
587 ir_rvalue *result = NULL;
588 ir_rvalue *op[2];
589 struct simple_node op_list;
590 const struct glsl_type *type = glsl_type::error_type;
591 bool error_emitted = false;
592 YYLTYPE loc;
593
594 loc = this->get_location();
595 make_empty_list(& op_list);
596
597 switch (this->oper) {
598 case ast_assign: {
599 op[0] = this->subexpressions[0]->hir(instructions, state);
600 op[1] = this->subexpressions[1]->hir(instructions, state);
601
602 result = do_assignment(instructions, state, op[0], op[1],
603 this->subexpressions[0]->get_location());
604 error_emitted = result->type->is_error();
605 type = result->type;
606 break;
607 }
608
609 case ast_plus:
610 op[0] = this->subexpressions[0]->hir(instructions, state);
611
612 error_emitted = op[0]->type->is_error();
613 if (type->is_error())
614 op[0]->type = type;
615
616 result = op[0];
617 break;
618
619 case ast_neg:
620 op[0] = this->subexpressions[0]->hir(instructions, state);
621
622 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
623
624 error_emitted = type->is_error();
625
626 result = new ir_expression(operations[this->oper], type,
627 op[0], NULL);
628 break;
629
630 case ast_add:
631 case ast_sub:
632 case ast_mul:
633 case ast_div:
634 op[0] = this->subexpressions[0]->hir(instructions, state);
635 op[1] = this->subexpressions[1]->hir(instructions, state);
636
637 type = arithmetic_result_type(op[0], op[1],
638 (this->oper == ast_mul),
639 state, & loc);
640 error_emitted = type->is_error();
641
642 result = new ir_expression(operations[this->oper], type,
643 op[0], op[1]);
644 break;
645
646 case ast_mod:
647 op[0] = this->subexpressions[0]->hir(instructions, state);
648 op[1] = this->subexpressions[1]->hir(instructions, state);
649
650 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
651
652 assert(operations[this->oper] == ir_binop_mod);
653
654 result = new ir_expression(operations[this->oper], type,
655 op[0], op[1]);
656 error_emitted = type->is_error();
657 break;
658
659 case ast_lshift:
660 case ast_rshift:
661 _mesa_glsl_error(& loc, state, "FINISHME: implement bit-shift operators");
662 error_emitted = true;
663 break;
664
665 case ast_less:
666 case ast_greater:
667 case ast_lequal:
668 case ast_gequal:
669 op[0] = this->subexpressions[0]->hir(instructions, state);
670 op[1] = this->subexpressions[1]->hir(instructions, state);
671
672 type = relational_result_type(op[0], op[1], state, & loc);
673
674 /* The relational operators must either generate an error or result
675 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
676 */
677 assert(type->is_error()
678 || ((type->base_type == GLSL_TYPE_BOOL)
679 && type->is_scalar()));
680
681 result = new ir_expression(operations[this->oper], type,
682 op[0], op[1]);
683 error_emitted = type->is_error();
684 break;
685
686 case ast_nequal:
687 case ast_equal:
688 op[0] = this->subexpressions[0]->hir(instructions, state);
689 op[1] = this->subexpressions[1]->hir(instructions, state);
690
691 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
692 *
693 * "The equality operators equal (==), and not equal (!=)
694 * operate on all types. They result in a scalar Boolean. If
695 * the operand types do not match, then there must be a
696 * conversion from Section 4.1.10 "Implicit Conversions"
697 * applied to one operand that can make them match, in which
698 * case this conversion is done."
699 */
700 if ((!apply_implicit_conversion(op[0]->type, op[1], state)
701 && !apply_implicit_conversion(op[1]->type, op[0], state))
702 || (op[0]->type != op[1]->type)) {
703 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
704 "type", (this->oper == ast_equal) ? "==" : "!=");
705 error_emitted = true;
706 } else if ((state->language_version <= 110)
707 && (op[0]->type->is_array() || op[1]->type->is_array())) {
708 _mesa_glsl_error(& loc, state, "array comparisons forbidden in "
709 "GLSL 1.10");
710 error_emitted = true;
711 }
712
713 result = new ir_expression(operations[this->oper], glsl_type::bool_type,
714 op[0], op[1]);
715 type = glsl_type::bool_type;
716
717 assert(result->type == glsl_type::bool_type);
718 break;
719
720 case ast_bit_and:
721 case ast_bit_xor:
722 case ast_bit_or:
723 case ast_bit_not:
724 _mesa_glsl_error(& loc, state, "FINISHME: implement bit-wise operators");
725 error_emitted = true;
726 break;
727
728 case ast_logic_and:
729 case ast_logic_xor:
730 case ast_logic_or:
731 op[0] = this->subexpressions[0]->hir(instructions, state);
732 op[1] = this->subexpressions[1]->hir(instructions, state);
733
734 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
735 YYLTYPE loc = this->subexpressions[0]->get_location();
736
737 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
738 operator_string(this->oper));
739 error_emitted = true;
740 }
741
742 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
743 YYLTYPE loc = this->subexpressions[1]->get_location();
744
745 _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean",
746 operator_string(this->oper));
747 error_emitted = true;
748 }
749
750 result = new ir_expression(operations[this->oper], glsl_type::bool_type,
751 op[0], op[1]);
752 type = glsl_type::bool_type;
753 break;
754
755 case ast_logic_not:
756 op[0] = this->subexpressions[0]->hir(instructions, state);
757
758 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
759 YYLTYPE loc = this->subexpressions[0]->get_location();
760
761 _mesa_glsl_error(& loc, state,
762 "operand of `!' must be scalar boolean");
763 error_emitted = true;
764 }
765
766 result = new ir_expression(operations[this->oper], glsl_type::bool_type,
767 op[0], NULL);
768 type = glsl_type::bool_type;
769 break;
770
771 case ast_mul_assign:
772 case ast_div_assign:
773 case ast_add_assign:
774 case ast_sub_assign: {
775 op[0] = this->subexpressions[0]->hir(instructions, state);
776 op[1] = this->subexpressions[1]->hir(instructions, state);
777
778 type = arithmetic_result_type(op[0], op[1],
779 (this->oper == ast_mul_assign),
780 state, & loc);
781
782 ir_rvalue *temp_rhs = new ir_expression(operations[this->oper], type,
783 op[0], op[1]);
784
785 result = do_assignment(instructions, state, op[0], temp_rhs,
786 this->subexpressions[0]->get_location());
787 type = result->type;
788 error_emitted = (op[0]->type->is_error());
789
790 /* GLSL 1.10 does not allow array assignment. However, we don't have to
791 * explicitly test for this because none of the binary expression
792 * operators allow array operands either.
793 */
794
795 break;
796 }
797
798 case ast_mod_assign: {
799 op[0] = this->subexpressions[0]->hir(instructions, state);
800 op[1] = this->subexpressions[1]->hir(instructions, state);
801
802 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
803
804 assert(operations[this->oper] == ir_binop_mod);
805
806 struct ir_rvalue *temp_rhs;
807 temp_rhs = new ir_expression(operations[this->oper], type,
808 op[0], op[1]);
809
810 result = do_assignment(instructions, state, op[0], temp_rhs,
811 this->subexpressions[0]->get_location());
812 type = result->type;
813 error_emitted = type->is_error();
814 break;
815 }
816
817 case ast_ls_assign:
818 case ast_rs_assign:
819 _mesa_glsl_error(& loc, state,
820 "FINISHME: implement bit-shift assignment operators");
821 error_emitted = true;
822 break;
823
824 case ast_and_assign:
825 case ast_xor_assign:
826 case ast_or_assign:
827 _mesa_glsl_error(& loc, state,
828 "FINISHME: implement logic assignment operators");
829 error_emitted = true;
830 break;
831
832 case ast_conditional: {
833 op[0] = this->subexpressions[0]->hir(instructions, state);
834
835 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
836 *
837 * "The ternary selection operator (?:). It operates on three
838 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
839 * first expression, which must result in a scalar Boolean."
840 */
841 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
842 YYLTYPE loc = this->subexpressions[0]->get_location();
843
844 _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean");
845 error_emitted = true;
846 }
847
848 /* The :? operator is implemented by generating an anonymous temporary
849 * followed by an if-statement. The last instruction in each branch of
850 * the if-statement assigns a value to the anonymous temporary. This
851 * temporary is the r-value of the expression.
852 */
853 ir_variable *const tmp = generate_temporary(glsl_type::error_type,
854 instructions, state);
855
856 ir_if *const stmt = new ir_if(op[0]);
857 instructions->push_tail(stmt);
858
859 op[1] = this->subexpressions[1]->hir(& stmt->then_instructions, state);
860 ir_dereference *const then_deref = new ir_dereference(tmp);
861 ir_assignment *const then_assign =
862 new ir_assignment(then_deref, op[1], NULL);
863 stmt->then_instructions.push_tail(then_assign);
864
865 op[2] = this->subexpressions[2]->hir(& stmt->else_instructions, state);
866 ir_dereference *const else_deref = new ir_dereference(tmp);
867 ir_assignment *const else_assign =
868 new ir_assignment(else_deref, op[2], NULL);
869 stmt->else_instructions.push_tail(else_assign);
870
871 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
872 *
873 * "The second and third expressions can be any type, as
874 * long their types match, or there is a conversion in
875 * Section 4.1.10 "Implicit Conversions" that can be applied
876 * to one of the expressions to make their types match. This
877 * resulting matching type is the type of the entire
878 * expression."
879 */
880 if ((!apply_implicit_conversion(op[1]->type, op[2], state)
881 && !apply_implicit_conversion(op[2]->type, op[1], state))
882 || (op[1]->type != op[2]->type)) {
883 YYLTYPE loc = this->subexpressions[1]->get_location();
884
885 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
886 "operator must have matching types.");
887 error_emitted = true;
888 } else {
889 tmp->type = op[1]->type;
890 }
891
892 result = new ir_dereference(tmp);
893 type = tmp->type;
894 break;
895 }
896
897 case ast_pre_inc:
898 case ast_pre_dec: {
899 op[0] = this->subexpressions[0]->hir(instructions, state);
900 if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
901 op[1] = new ir_constant(1.0f);
902 else
903 op[1] = new ir_constant(1);
904
905 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
906
907 struct ir_rvalue *temp_rhs;
908 temp_rhs = new ir_expression(operations[this->oper], type,
909 op[0], op[1]);
910
911 result = do_assignment(instructions, state, op[0], temp_rhs,
912 this->subexpressions[0]->get_location());
913 type = result->type;
914 error_emitted = op[0]->type->is_error();
915 break;
916 }
917
918 case ast_post_inc:
919 case ast_post_dec: {
920 op[0] = this->subexpressions[0]->hir(instructions, state);
921 if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
922 op[1] = new ir_constant(1.0f);
923 else
924 op[1] = new ir_constant(1);
925
926 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
927
928 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
929
930 struct ir_rvalue *temp_rhs;
931 temp_rhs = new ir_expression(operations[this->oper], type,
932 op[0], op[1]);
933
934 /* Get a temporary of a copy of the lvalue before it's modified.
935 * This may get thrown away later.
936 */
937 result = get_lvalue_copy(instructions, state, op[0],
938 this->subexpressions[0]->get_location());
939
940 (void)do_assignment(instructions, state, op[0], temp_rhs,
941 this->subexpressions[0]->get_location());
942
943 type = result->type;
944 error_emitted = op[0]->type->is_error();
945 break;
946 }
947
948 case ast_field_selection:
949 result = _mesa_ast_field_selection_to_hir(this, instructions, state);
950 type = result->type;
951 break;
952
953 case ast_array_index: {
954 YYLTYPE index_loc = subexpressions[1]->get_location();
955
956 op[0] = subexpressions[0]->hir(instructions, state);
957 op[1] = subexpressions[1]->hir(instructions, state);
958
959 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
960
961 ir_dereference *const lhs = op[0]->as_dereference();
962 ir_instruction *array;
963 if ((lhs != NULL)
964 && (lhs->mode == ir_dereference::ir_reference_variable)) {
965 result = new ir_dereference(lhs->var, op[1]);
966
967 delete op[0];
968 array = lhs->var;
969 } else {
970 result = new ir_dereference(op[0], op[1]);
971 array = op[0];
972 }
973
974 /* Do not use op[0] after this point. Use array.
975 */
976 op[0] = NULL;
977
978
979 if (error_emitted)
980 break;
981
982 /* FINISHME: Handle vectors and matrices accessed with []. */
983 if (!array->type->is_array()) {
984 _mesa_glsl_error(& index_loc, state,
985 "cannot dereference non-array");
986 error_emitted = true;
987 }
988
989 if (!op[1]->type->is_integer()) {
990 _mesa_glsl_error(& index_loc, state,
991 "array index must be integer type");
992 error_emitted = true;
993 } else if (!op[1]->type->is_scalar()) {
994 _mesa_glsl_error(& index_loc, state,
995 "array index must be scalar");
996 error_emitted = true;
997 }
998
999 /* If the array index is a constant expression and the array has a
1000 * declared size, ensure that the access is in-bounds. If the array
1001 * index is not a constant expression, ensure that the array has a
1002 * declared size.
1003 */
1004 ir_constant *const const_index = op[1]->constant_expression_value();
1005 if (const_index != NULL) {
1006 const int idx = const_index->value.i[0];
1007
1008 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
1009 *
1010 * "It is illegal to declare an array with a size, and then
1011 * later (in the same shader) index the same array with an
1012 * integral constant expression greater than or equal to the
1013 * declared size. It is also illegal to index an array with a
1014 * negative constant expression."
1015 */
1016 if ((array->type->array_size() > 0)
1017 && (array->type->array_size() <= idx)) {
1018 _mesa_glsl_error(& loc, state,
1019 "array index must be < %u",
1020 array->type->array_size());
1021 error_emitted = true;
1022 }
1023
1024 if (idx < 0) {
1025 _mesa_glsl_error(& loc, state,
1026 "array index must be >= 0");
1027 error_emitted = true;
1028 }
1029
1030 ir_variable *const v = array->as_variable();
1031 if ((v != NULL) && (unsigned(idx) > v->max_array_access))
1032 v->max_array_access = idx;
1033 }
1034
1035 if (error_emitted)
1036 result->type = glsl_type::error_type;
1037
1038 type = result->type;
1039 break;
1040 }
1041
1042 case ast_function_call:
1043 /* Should *NEVER* get here. ast_function_call should always be handled
1044 * by ast_function_expression::hir.
1045 */
1046 assert(0);
1047 break;
1048
1049 case ast_identifier: {
1050 /* ast_identifier can appear several places in a full abstract syntax
1051 * tree. This particular use must be at location specified in the grammar
1052 * as 'variable_identifier'.
1053 */
1054 ir_variable *var =
1055 state->symbols->get_variable(this->primary_expression.identifier);
1056
1057 result = new ir_dereference(var);
1058
1059 if (var != NULL) {
1060 type = result->type;
1061 } else {
1062 _mesa_glsl_error(& loc, state, "`%s' undeclared",
1063 this->primary_expression.identifier);
1064
1065 error_emitted = true;
1066 }
1067 break;
1068 }
1069
1070 case ast_int_constant:
1071 type = glsl_type::int_type;
1072 result = new ir_constant(type, & this->primary_expression);
1073 break;
1074
1075 case ast_uint_constant:
1076 type = glsl_type::uint_type;
1077 result = new ir_constant(type, & this->primary_expression);
1078 break;
1079
1080 case ast_float_constant:
1081 type = glsl_type::float_type;
1082 result = new ir_constant(type, & this->primary_expression);
1083 break;
1084
1085 case ast_bool_constant:
1086 type = glsl_type::bool_type;
1087 result = new ir_constant(type, & this->primary_expression);
1088 break;
1089
1090 case ast_sequence: {
1091 struct simple_node *ptr;
1092
1093 /* It should not be possible to generate a sequence in the AST without
1094 * any expressions in it.
1095 */
1096 assert(!is_empty_list(&this->expressions));
1097
1098 /* The r-value of a sequence is the last expression in the sequence. If
1099 * the other expressions in the sequence do not have side-effects (and
1100 * therefore add instructions to the instruction list), they get dropped
1101 * on the floor.
1102 */
1103 foreach (ptr, &this->expressions)
1104 result = ((ast_node *)ptr)->hir(instructions, state);
1105
1106 type = result->type;
1107
1108 /* Any errors should have already been emitted in the loop above.
1109 */
1110 error_emitted = true;
1111 break;
1112 }
1113 }
1114
1115 if (type->is_error() && !error_emitted)
1116 _mesa_glsl_error(& loc, state, "type mismatch");
1117
1118 return result;
1119 }
1120
1121
1122 ir_rvalue *
1123 ast_expression_statement::hir(exec_list *instructions,
1124 struct _mesa_glsl_parse_state *state)
1125 {
1126 /* It is possible to have expression statements that don't have an
1127 * expression. This is the solitary semicolon:
1128 *
1129 * for (i = 0; i < 5; i++)
1130 * ;
1131 *
1132 * In this case the expression will be NULL. Test for NULL and don't do
1133 * anything in that case.
1134 */
1135 if (expression != NULL)
1136 expression->hir(instructions, state);
1137
1138 /* Statements do not have r-values.
1139 */
1140 return NULL;
1141 }
1142
1143
1144 ir_rvalue *
1145 ast_compound_statement::hir(exec_list *instructions,
1146 struct _mesa_glsl_parse_state *state)
1147 {
1148 struct simple_node *ptr;
1149
1150
1151 if (new_scope)
1152 state->symbols->push_scope();
1153
1154 foreach (ptr, &statements)
1155 ((ast_node *)ptr)->hir(instructions, state);
1156
1157 if (new_scope)
1158 state->symbols->pop_scope();
1159
1160 /* Compound statements do not have r-values.
1161 */
1162 return NULL;
1163 }
1164
1165
1166 static const glsl_type *
1167 process_array_type(const glsl_type *base, ast_node *array_size,
1168 struct _mesa_glsl_parse_state *state)
1169 {
1170 unsigned length = 0;
1171
1172 /* FINISHME: Reject delcarations of multidimensional arrays. */
1173
1174 if (array_size != NULL) {
1175 exec_list dummy_instructions;
1176 ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
1177 YYLTYPE loc = array_size->get_location();
1178
1179 /* FINISHME: Verify that the grammar forbids side-effects in array
1180 * FINISHME: sizes. i.e., 'vec4 [x = 12] data'
1181 */
1182 assert(dummy_instructions.is_empty());
1183
1184 if (ir != NULL) {
1185 if (!ir->type->is_integer()) {
1186 _mesa_glsl_error(& loc, state, "array size must be integer type");
1187 } else if (!ir->type->is_scalar()) {
1188 _mesa_glsl_error(& loc, state, "array size must be scalar type");
1189 } else {
1190 ir_constant *const size = ir->constant_expression_value();
1191
1192 if (size == NULL) {
1193 _mesa_glsl_error(& loc, state, "array size must be a "
1194 "constant valued expression");
1195 } else if (size->value.i[0] <= 0) {
1196 _mesa_glsl_error(& loc, state, "array size must be > 0");
1197 } else {
1198 assert(size->type == ir->type);
1199 length = size->value.u[0];
1200 }
1201 }
1202 }
1203 }
1204
1205 return glsl_type::get_array_instance(base, length);
1206 }
1207
1208
1209 const glsl_type *
1210 ast_type_specifier::glsl_type(const char **name,
1211 struct _mesa_glsl_parse_state *state) const
1212 {
1213 const struct glsl_type *type;
1214
1215 if (this->type_specifier == ast_struct) {
1216 /* FINISHME: Handle annonymous structures. */
1217 type = NULL;
1218 } else {
1219 type = state->symbols->get_type(this->type_name);
1220 *name = this->type_name;
1221
1222 if (this->is_array) {
1223 type = process_array_type(type, this->array_size, state);
1224 }
1225 }
1226
1227 return type;
1228 }
1229
1230
1231 static void
1232 apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
1233 struct ir_variable *var,
1234 struct _mesa_glsl_parse_state *state,
1235 YYLTYPE *loc)
1236 {
1237 if (qual->invariant)
1238 var->invariant = 1;
1239
1240 /* FINISHME: Mark 'in' variables at global scope as read-only. */
1241 if (qual->constant || qual->attribute || qual->uniform
1242 || (qual->varying && (state->target == fragment_shader)))
1243 var->read_only = 1;
1244
1245 if (qual->centroid)
1246 var->centroid = 1;
1247
1248 if (qual->attribute && state->target == fragment_shader) {
1249 var->type = glsl_type::error_type;
1250 _mesa_glsl_error(loc, state,
1251 "`attribute' variables may not be declared in the "
1252 "fragment shader");
1253 }
1254
1255 /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
1256 *
1257 * "The varying qualifier can be used only with the data types
1258 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
1259 * these."
1260 */
1261 if (qual->varying && var->type->base_type != GLSL_TYPE_FLOAT) {
1262 var->type = glsl_type::error_type;
1263 _mesa_glsl_error(loc, state,
1264 "varying variables must be of base type float");
1265 }
1266
1267 if (qual->in && qual->out)
1268 var->mode = ir_var_inout;
1269 else if (qual->attribute || qual->in
1270 || (qual->varying && (state->target == fragment_shader)))
1271 var->mode = ir_var_in;
1272 else if (qual->out || (qual->varying && (state->target == vertex_shader)))
1273 var->mode = ir_var_out;
1274 else if (qual->uniform)
1275 var->mode = ir_var_uniform;
1276 else
1277 var->mode = ir_var_auto;
1278
1279 if (qual->flat)
1280 var->interpolation = ir_var_flat;
1281 else if (qual->noperspective)
1282 var->interpolation = ir_var_noperspective;
1283 else
1284 var->interpolation = ir_var_smooth;
1285 }
1286
1287
1288 ir_rvalue *
1289 ast_declarator_list::hir(exec_list *instructions,
1290 struct _mesa_glsl_parse_state *state)
1291 {
1292 struct simple_node *ptr;
1293 const struct glsl_type *decl_type;
1294 const char *type_name = NULL;
1295
1296
1297 /* FINISHME: Handle vertex shader "invariant" declarations that do not
1298 * FINISHME: include a type. These re-declare built-in variables to be
1299 * FINISHME: invariant.
1300 */
1301
1302 decl_type = this->type->specifier->glsl_type(& type_name, state);
1303
1304 foreach (ptr, &this->declarations) {
1305 struct ast_declaration *const decl = (struct ast_declaration * )ptr;
1306 const struct glsl_type *var_type;
1307 struct ir_variable *var;
1308 YYLTYPE loc = this->get_location();
1309
1310 /* FINISHME: Emit a warning if a variable declaration shadows a
1311 * FINISHME: declaration at a higher scope.
1312 */
1313
1314 if ((decl_type == NULL) || decl_type->is_void()) {
1315 if (type_name != NULL) {
1316 _mesa_glsl_error(& loc, state,
1317 "invalid type `%s' in declaration of `%s'",
1318 type_name, decl->identifier);
1319 } else {
1320 _mesa_glsl_error(& loc, state,
1321 "invalid type in declaration of `%s'",
1322 decl->identifier);
1323 }
1324 continue;
1325 }
1326
1327 if (decl->is_array) {
1328 var_type = process_array_type(decl_type, decl->array_size, state);
1329 } else {
1330 var_type = decl_type;
1331 }
1332
1333 var = new ir_variable(var_type, decl->identifier);
1334
1335 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
1336 *
1337 * "Global variables can only use the qualifiers const,
1338 * attribute, uni form, or varying. Only one may be
1339 * specified.
1340 *
1341 * Local variables can only use the qualifier const."
1342 *
1343 * This is relaxed in GLSL 1.30.
1344 */
1345 if (state->language_version < 120) {
1346 if (this->type->qualifier.out) {
1347 _mesa_glsl_error(& loc, state,
1348 "`out' qualifier in declaration of `%s' "
1349 "only valid for function parameters in GLSL 1.10.",
1350 decl->identifier);
1351 }
1352 if (this->type->qualifier.in) {
1353 _mesa_glsl_error(& loc, state,
1354 "`in' qualifier in declaration of `%s' "
1355 "only valid for function parameters in GLSL 1.10.",
1356 decl->identifier);
1357 }
1358 /* FINISHME: Test for other invalid qualifiers. */
1359 }
1360
1361 apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
1362 & loc);
1363
1364 /* Attempt to add the variable to the symbol table. If this fails, it
1365 * means the variable has already been declared at this scope. Arrays
1366 * fudge this rule a little bit.
1367 *
1368 * From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
1369 *
1370 * "It is legal to declare an array without a size and then
1371 * later re-declare the same name as an array of the same
1372 * type and specify a size."
1373 */
1374 if (state->symbols->name_declared_this_scope(decl->identifier)) {
1375 ir_variable *const earlier =
1376 state->symbols->get_variable(decl->identifier);
1377
1378 if ((earlier != NULL)
1379 && (earlier->type->array_size() == 0)
1380 && var->type->is_array()
1381 && (var->type->element_type() == earlier->type->element_type())) {
1382 /* FINISHME: This doesn't match the qualifiers on the two
1383 * FINISHME: declarations. It's not 100% clear whether this is
1384 * FINISHME: required or not.
1385 */
1386
1387 if (var->type->array_size() <= earlier->max_array_access) {
1388 YYLTYPE loc = this->get_location();
1389
1390 _mesa_glsl_error(& loc, state, "array size must be > %u due to "
1391 "previous access",
1392 earlier->max_array_access);
1393 }
1394
1395 earlier->type = var->type;
1396 delete var;
1397 var = NULL;
1398 } else {
1399 YYLTYPE loc = this->get_location();
1400
1401 _mesa_glsl_error(& loc, state, "`%s' redeclared",
1402 decl->identifier);
1403 }
1404
1405 continue;
1406 }
1407
1408 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
1409 *
1410 * "Identifiers starting with "gl_" are reserved for use by
1411 * OpenGL, and may not be declared in a shader as either a
1412 * variable or a function."
1413 */
1414 if (strncmp(decl->identifier, "gl_", 3) == 0) {
1415 /* FINISHME: This should only trigger if we're not redefining
1416 * FINISHME: a builtin (to add a qualifier, for example).
1417 */
1418 _mesa_glsl_error(& loc, state,
1419 "identifier `%s' uses reserved `gl_' prefix",
1420 decl->identifier);
1421 }
1422
1423 instructions->push_tail(var);
1424
1425 if (state->current_function != NULL) {
1426 const char *mode = NULL;
1427 const char *extra = "";
1428
1429 /* There is no need to check for 'inout' here because the parser will
1430 * only allow that in function parameter lists.
1431 */
1432 if (this->type->qualifier.attribute) {
1433 mode = "attribute";
1434 } else if (this->type->qualifier.uniform) {
1435 mode = "uniform";
1436 } else if (this->type->qualifier.varying) {
1437 mode = "varying";
1438 } else if (this->type->qualifier.in) {
1439 mode = "in";
1440 extra = " or in function parameter list";
1441 } else if (this->type->qualifier.out) {
1442 mode = "out";
1443 extra = " or in function parameter list";
1444 }
1445
1446 if (mode) {
1447 _mesa_glsl_error(& loc, state,
1448 "%s variable `%s' must be declared at "
1449 "global scope%s",
1450 mode, var->name, extra);
1451 }
1452 } else if (var->mode == ir_var_in) {
1453 if (state->target == vertex_shader) {
1454 bool error_emitted = false;
1455
1456 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1457 *
1458 * "Vertex shader inputs can only be float, floating-point
1459 * vectors, matrices, signed and unsigned integers and integer
1460 * vectors. Vertex shader inputs can also form arrays of these
1461 * types, but not structures."
1462 *
1463 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
1464 *
1465 * "Vertex shader inputs can only be float, floating-point
1466 * vectors, matrices, signed and unsigned integers and integer
1467 * vectors. They cannot be arrays or structures."
1468 *
1469 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1470 *
1471 * "The attribute qualifier can be used only with float,
1472 * floating-point vectors, and matrices. Attribute variables
1473 * cannot be declared as arrays or structures."
1474 */
1475 const glsl_type *check_type = var->type->is_array()
1476 ? var->type->fields.array : var->type;
1477
1478 switch (check_type->base_type) {
1479 case GLSL_TYPE_FLOAT:
1480 break;
1481 case GLSL_TYPE_UINT:
1482 case GLSL_TYPE_INT:
1483 if (state->language_version > 120)
1484 break;
1485 /* FALLTHROUGH */
1486 default:
1487 _mesa_glsl_error(& loc, state,
1488 "vertex shader input / attribute cannot have "
1489 "type %s`%s'",
1490 var->type->is_array() ? "array of " : "",
1491 check_type->name);
1492 error_emitted = true;
1493 }
1494
1495 if (!error_emitted && (state->language_version <= 130)
1496 && var->type->is_array()) {
1497 _mesa_glsl_error(& loc, state,
1498 "vertex shader input / attribute cannot have "
1499 "array type");
1500 error_emitted = true;
1501 }
1502 }
1503 }
1504
1505 if (decl->initializer != NULL) {
1506 YYLTYPE initializer_loc = decl->initializer->get_location();
1507
1508 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
1509 *
1510 * "All uniform variables are read-only and are initialized either
1511 * directly by an application via API commands, or indirectly by
1512 * OpenGL."
1513 */
1514 if ((state->language_version <= 110)
1515 && (var->mode == ir_var_uniform)) {
1516 _mesa_glsl_error(& initializer_loc, state,
1517 "cannot initialize uniforms in GLSL 1.10");
1518 }
1519
1520 if (var->type->is_sampler()) {
1521 _mesa_glsl_error(& initializer_loc, state,
1522 "cannot initialize samplers");
1523 }
1524
1525 if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
1526 _mesa_glsl_error(& initializer_loc, state,
1527 "cannot initialize %s shader input / %s",
1528 (state->target == vertex_shader)
1529 ? "vertex" : "fragment",
1530 (state->target == vertex_shader)
1531 ? "attribute" : "varying");
1532 }
1533
1534 ir_dereference *const lhs = new ir_dereference(var);
1535 ir_rvalue *rhs = decl->initializer->hir(instructions, state);
1536
1537 /* Calculate the constant value if this is a const
1538 * declaration.
1539 */
1540 if (this->type->qualifier.constant) {
1541 rhs = rhs->constant_expression_value();
1542 if (!rhs) {
1543 _mesa_glsl_error(& initializer_loc, state,
1544 "initializer of const variable `%s' must be a "
1545 "constant expression",
1546 decl->identifier);
1547 }
1548 }
1549
1550 if (rhs && !rhs->type->is_error()) {
1551 bool temp = var->read_only;
1552 if (this->type->qualifier.constant)
1553 var->read_only = false;
1554 (void) do_assignment(instructions, state, lhs, rhs,
1555 this->get_location());
1556 var->read_only = temp;
1557 }
1558 }
1559
1560 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
1561 *
1562 * "It is an error to write to a const variable outside of
1563 * its declaration, so they must be initialized when
1564 * declared."
1565 */
1566 if (this->type->qualifier.constant && decl->initializer == NULL) {
1567 _mesa_glsl_error(& loc, state,
1568 "const declaration of `%s' must be initialized");
1569 }
1570
1571 /* Add the vairable to the symbol table after processing the initializer.
1572 * This differs from most C-like languages, but it follows the GLSL
1573 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
1574 * spec:
1575 *
1576 * "Within a declaration, the scope of a name starts immediately
1577 * after the initializer if present or immediately after the name
1578 * being declared if not."
1579 */
1580 const bool added_variable =
1581 state->symbols->add_variable(decl->identifier, var);
1582 assert(added_variable);
1583 }
1584
1585 /* Variable declarations do not have r-values.
1586 */
1587 return NULL;
1588 }
1589
1590
1591 ir_rvalue *
1592 ast_parameter_declarator::hir(exec_list *instructions,
1593 struct _mesa_glsl_parse_state *state)
1594 {
1595 const struct glsl_type *type;
1596 const char *name = NULL;
1597 YYLTYPE loc = this->get_location();
1598
1599 type = this->type->specifier->glsl_type(& name, state);
1600
1601 if (type == NULL) {
1602 if (name != NULL) {
1603 _mesa_glsl_error(& loc, state,
1604 "invalid type `%s' in declaration of `%s'",
1605 name, this->identifier);
1606 } else {
1607 _mesa_glsl_error(& loc, state,
1608 "invalid type in declaration of `%s'",
1609 this->identifier);
1610 }
1611
1612 type = glsl_type::error_type;
1613 }
1614
1615 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
1616 *
1617 * "Functions that accept no input arguments need not use void in the
1618 * argument list because prototypes (or definitions) are required and
1619 * therefore there is no ambiguity when an empty argument list "( )" is
1620 * declared. The idiom "(void)" as a parameter list is provided for
1621 * convenience."
1622 *
1623 * Placing this check here prevents a void parameter being set up
1624 * for a function, which avoids tripping up checks for main taking
1625 * parameters and lookups of an unnamed symbol.
1626 */
1627 if (type->is_void() && (this->identifier == NULL))
1628 return NULL;
1629
1630 ir_variable *var = new ir_variable(type, this->identifier);
1631
1632 /* FINISHME: Handle array declarations. Note that this requires
1633 * FINISHME: complete handling of constant expressions.
1634 */
1635
1636 /* Apply any specified qualifiers to the parameter declaration. Note that
1637 * for function parameters the default mode is 'in'.
1638 */
1639 apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
1640 if (var->mode == ir_var_auto)
1641 var->mode = ir_var_in;
1642
1643 instructions->push_tail(var);
1644
1645 /* Parameter declarations do not have r-values.
1646 */
1647 return NULL;
1648 }
1649
1650
1651 static void
1652 ast_function_parameters_to_hir(struct simple_node *ast_parameters,
1653 exec_list *ir_parameters,
1654 struct _mesa_glsl_parse_state *state)
1655 {
1656 struct simple_node *ptr;
1657
1658 foreach (ptr, ast_parameters) {
1659 ast_node *param = (ast_node *)ptr;
1660 param->hir(ir_parameters, state);
1661
1662 }
1663 }
1664
1665
1666 static bool
1667 parameter_lists_match(exec_list *list_a, exec_list *list_b)
1668 {
1669 exec_list_iterator iter_a = list_a->iterator();
1670 exec_list_iterator iter_b = list_b->iterator();
1671
1672 while (iter_a.has_next()) {
1673 ir_variable *a = (ir_variable *)iter_a.get();
1674 ir_variable *b = (ir_variable *)iter_b.get();
1675
1676 /* If all of the parameters from the other parameter list have been
1677 * exhausted, the lists have different length and, by definition,
1678 * do not match.
1679 */
1680 if (!iter_b.has_next())
1681 return false;
1682
1683 /* If the types of the parameters do not match, the parameters lists
1684 * are different.
1685 */
1686 if (a->type != b->type)
1687 return false;
1688
1689 iter_a.next();
1690 iter_b.next();
1691 }
1692
1693 return true;
1694 }
1695
1696
1697 ir_rvalue *
1698 ast_function::hir(exec_list *instructions,
1699 struct _mesa_glsl_parse_state *state)
1700 {
1701 ir_function *f = NULL;
1702 ir_function_signature *sig = NULL;
1703 exec_list hir_parameters;
1704
1705
1706 /* The prototype part of a function does not generate anything in the IR
1707 * instruction stream.
1708 */
1709 (void) instructions;
1710
1711 /* Convert the list of function parameters to HIR now so that they can be
1712 * used below to compare this function's signature with previously seen
1713 * signatures for functions with the same name.
1714 */
1715 ast_function_parameters_to_hir(& this->parameters, & hir_parameters, state);
1716
1717 const char *return_type_name;
1718 const glsl_type *return_type =
1719 this->return_type->specifier->glsl_type(& return_type_name, state);
1720
1721 assert(return_type != NULL);
1722
1723 /* Verify that this function's signature either doesn't match a previously
1724 * seen signature for a function with the same name, or, if a match is found,
1725 * that the previously seen signature does not have an associated definition.
1726 */
1727 const char *const name = identifier;
1728 f = state->symbols->get_function(name);
1729 if (f != NULL) {
1730 foreach_iter(exec_list_iterator, iter, *f) {
1731 sig = (struct ir_function_signature *) iter.get();
1732
1733 /* Compare the parameter list of the function being defined to the
1734 * existing function. If the parameter lists match, then the return
1735 * type must also match and the existing function must not have a
1736 * definition.
1737 */
1738 if (parameter_lists_match(& hir_parameters, & sig->parameters)) {
1739 /* FINISHME: Compare return types. */
1740
1741 if (is_definition && (sig->definition != NULL)) {
1742 YYLTYPE loc = this->get_location();
1743
1744 _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
1745 sig = NULL;
1746 break;
1747 }
1748 }
1749
1750 sig = NULL;
1751 }
1752
1753 } else if (state->symbols->name_declared_this_scope(name)) {
1754 /* This function name shadows a non-function use of the same name.
1755 */
1756 YYLTYPE loc = this->get_location();
1757
1758 _mesa_glsl_error(& loc, state, "function name `%s' conflicts with "
1759 "non-function", name);
1760 sig = NULL;
1761 } else {
1762 f = new ir_function(name);
1763 state->symbols->add_function(f->name, f);
1764 }
1765
1766 /* Verify the return type of main() */
1767 if (strcmp(name, "main") == 0) {
1768 if (! return_type->is_void()) {
1769 YYLTYPE loc = this->get_location();
1770
1771 _mesa_glsl_error(& loc, state, "main() must return void");
1772 }
1773
1774 if (!hir_parameters.is_empty()) {
1775 YYLTYPE loc = this->get_location();
1776
1777 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
1778 }
1779 }
1780
1781 /* Finish storing the information about this new function in its signature.
1782 */
1783 if (sig == NULL) {
1784 sig = new ir_function_signature(return_type);
1785 f->add_signature(sig);
1786 } else if (is_definition) {
1787 /* Destroy all of the previous parameter information. The previous
1788 * parameter information comes from the function prototype, and it can
1789 * either include invalid parameter names or may not have names at all.
1790 */
1791 foreach_iter(exec_list_iterator, iter, sig->parameters) {
1792 assert(((ir_instruction *) iter.get())->as_variable() != NULL);
1793
1794 iter.remove();
1795 delete iter.get();
1796 }
1797 }
1798
1799 hir_parameters.move_nodes_to(& sig->parameters);
1800 signature = sig;
1801
1802 /* Function declarations (prototypes) do not have r-values.
1803 */
1804 return NULL;
1805 }
1806
1807
1808 ir_rvalue *
1809 ast_function_definition::hir(exec_list *instructions,
1810 struct _mesa_glsl_parse_state *state)
1811 {
1812 prototype->is_definition = true;
1813 prototype->hir(instructions, state);
1814
1815 ir_function_signature *signature = prototype->signature;
1816
1817 assert(state->current_function == NULL);
1818 state->current_function = signature;
1819
1820 ir_label *label = new ir_label(signature->function_name());
1821 if (signature->definition == NULL) {
1822 signature->definition = label;
1823 }
1824 instructions->push_tail(label);
1825
1826 /* Duplicate parameters declared in the prototype as concrete variables.
1827 * Add these to the symbol table.
1828 */
1829 state->symbols->push_scope();
1830 foreach_iter(exec_list_iterator, iter, signature->parameters) {
1831 ir_variable *const proto = ((ir_instruction *) iter.get())->as_variable();
1832
1833 assert(proto != NULL);
1834
1835 ir_variable *const var = proto->clone();
1836
1837 instructions->push_tail(var);
1838
1839 /* The only way a parameter would "exist" is if two parameters have
1840 * the same name.
1841 */
1842 if (state->symbols->name_declared_this_scope(var->name)) {
1843 YYLTYPE loc = this->get_location();
1844
1845 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
1846 } else {
1847 state->symbols->add_variable(var->name, var);
1848 }
1849 }
1850
1851 /* Convert the body of the function to HIR, and append the resulting
1852 * instructions to the list that currently consists of the function label
1853 * and the function parameters.
1854 */
1855 this->body->hir(instructions, state);
1856
1857 state->symbols->pop_scope();
1858
1859 assert(state->current_function == signature);
1860 state->current_function = NULL;
1861
1862 /* Function definitions do not have r-values.
1863 */
1864 return NULL;
1865 }
1866
1867
1868 ir_rvalue *
1869 ast_jump_statement::hir(exec_list *instructions,
1870 struct _mesa_glsl_parse_state *state)
1871 {
1872
1873 if (mode == ast_return) {
1874 ir_return *inst;
1875 assert(state->current_function);
1876
1877 if (opt_return_value) {
1878 if (state->current_function->return_type->base_type ==
1879 GLSL_TYPE_VOID) {
1880 YYLTYPE loc = this->get_location();
1881
1882 _mesa_glsl_error(& loc, state,
1883 "`return` with a value, in function `%s' "
1884 "returning void",
1885 state->current_function->definition->label);
1886 }
1887
1888 ir_expression *const ret = (ir_expression *)
1889 opt_return_value->hir(instructions, state);
1890 assert(ret != NULL);
1891
1892 /* FINISHME: Make sure the type of the return value matches the return
1893 * FINISHME: type of the enclosing function.
1894 */
1895
1896 inst = new ir_return(ret);
1897 } else {
1898 if (state->current_function->return_type->base_type !=
1899 GLSL_TYPE_VOID) {
1900 YYLTYPE loc = this->get_location();
1901
1902 _mesa_glsl_error(& loc, state,
1903 "`return' with no value, in function %s returning "
1904 "non-void",
1905 state->current_function->definition->label);
1906 }
1907 inst = new ir_return;
1908 }
1909
1910 instructions->push_tail(inst);
1911 }
1912
1913 if (mode == ast_discard) {
1914 /* FINISHME: discard support */
1915 if (state->target != fragment_shader) {
1916 YYLTYPE loc = this->get_location();
1917
1918 _mesa_glsl_error(& loc, state,
1919 "`discard' may only appear in a fragment shader");
1920 }
1921 }
1922
1923 /* Jump instructions do not have r-values.
1924 */
1925 return NULL;
1926 }
1927
1928
1929 ir_rvalue *
1930 ast_selection_statement::hir(exec_list *instructions,
1931 struct _mesa_glsl_parse_state *state)
1932 {
1933 ir_rvalue *const condition = this->condition->hir(instructions, state);
1934
1935 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
1936 *
1937 * "Any expression whose type evaluates to a Boolean can be used as the
1938 * conditional expression bool-expression. Vector types are not accepted
1939 * as the expression to if."
1940 *
1941 * The checks are separated so that higher quality diagnostics can be
1942 * generated for cases where both rules are violated.
1943 */
1944 if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
1945 YYLTYPE loc = this->condition->get_location();
1946
1947 _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
1948 "boolean");
1949 }
1950
1951 ir_if *const stmt = new ir_if(condition);
1952
1953 if (then_statement != NULL) {
1954 ast_node *node = (ast_node *) then_statement;
1955 do {
1956 node->hir(& stmt->then_instructions, state);
1957 node = (ast_node *) node->next;
1958 } while (node != then_statement);
1959 }
1960
1961 if (else_statement != NULL) {
1962 ast_node *node = (ast_node *) else_statement;
1963 do {
1964 node->hir(& stmt->else_instructions, state);
1965 node = (ast_node *) node->next;
1966 } while (node != else_statement);
1967 }
1968
1969 instructions->push_tail(stmt);
1970
1971 /* if-statements do not have r-values.
1972 */
1973 return NULL;
1974 }