Merge branch 'anholt'
[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
67 state->current_function = NULL;
68
69 foreach (ptr, & state->translation_unit) {
70 ((ast_node *)ptr)->hir(instructions, state);
71 }
72 }
73
74
75 static const struct glsl_type *
76 arithmetic_result_type(const struct glsl_type *type_a,
77 const struct glsl_type *type_b,
78 bool multiply,
79 struct _mesa_glsl_parse_state *state)
80 {
81 /* From GLSL 1.50 spec, page 56:
82 *
83 * "The arithmetic binary operators add (+), subtract (-),
84 * multiply (*), and divide (/) operate on integer and
85 * floating-point scalars, vectors, and matrices."
86 */
87 if (!type_a->is_numeric() || !type_b->is_numeric()) {
88 return glsl_error_type;
89 }
90
91
92 /* "If one operand is floating-point based and the other is
93 * not, then the conversions from Section 4.1.10 "Implicit
94 * Conversions" are applied to the non-floating-point-based operand."
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 if ((type_a->base_type == GLSL_TYPE_FLOAT)
101 && (type_b->base_type != GLSL_TYPE_FLOAT)) {
102 } else if ((type_a->base_type != GLSL_TYPE_FLOAT)
103 && (type_b->base_type == GLSL_TYPE_FLOAT)) {
104 }
105 }
106
107 /* "If the operands are integer types, they must both be signed or
108 * both be unsigned."
109 *
110 * From this rule and the preceeding conversion it can be inferred that
111 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
112 * The is_numeric check above already filtered out the case where either
113 * type is not one of these, so now the base types need only be tested for
114 * equality.
115 */
116 if (type_a->base_type != type_b->base_type) {
117 return glsl_error_type;
118 }
119
120 /* "All arithmetic binary operators result in the same fundamental type
121 * (signed integer, unsigned integer, or floating-point) as the
122 * operands they operate on, after operand type conversion. After
123 * conversion, the following cases are valid
124 *
125 * * The two operands are scalars. In this case the operation is
126 * applied, resulting in a scalar."
127 */
128 if (type_a->is_scalar() && type_b->is_scalar())
129 return type_a;
130
131 /* "* One operand is a scalar, and the other is a vector or matrix.
132 * In this case, the scalar operation is applied independently to each
133 * component of the vector or matrix, resulting in the same size
134 * vector or matrix."
135 */
136 if (type_a->is_scalar()) {
137 if (!type_b->is_scalar())
138 return type_b;
139 } else if (type_b->is_scalar()) {
140 return type_a;
141 }
142
143 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
144 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
145 * handled.
146 */
147 assert(!type_a->is_scalar());
148 assert(!type_b->is_scalar());
149
150 /* "* The two operands are vectors of the same size. In this case, the
151 * operation is done component-wise resulting in the same size
152 * vector."
153 */
154 if (type_a->is_vector() && type_b->is_vector()) {
155 return (type_a == type_b) ? type_a : glsl_error_type;
156 }
157
158 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
159 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
160 * <vector, vector> have been handled. At least one of the operands must
161 * be matrix. Further, since there are no integer matrix types, the base
162 * type of both operands must be float.
163 */
164 assert(type_a->is_matrix() || type_b->is_matrix());
165 assert(type_a->base_type == GLSL_TYPE_FLOAT);
166 assert(type_b->base_type == GLSL_TYPE_FLOAT);
167
168 /* "* The operator is add (+), subtract (-), or divide (/), and the
169 * operands are matrices with the same number of rows and the same
170 * number of columns. In this case, the operation is done component-
171 * wise resulting in the same size matrix."
172 * * The operator is multiply (*), where both operands are matrices or
173 * one operand is a vector and the other a matrix. A right vector
174 * operand is treated as a column vector and a left vector operand as a
175 * row vector. In all these cases, it is required that the number of
176 * columns of the left operand is equal to the number of rows of the
177 * right operand. Then, the multiply (*) operation does a linear
178 * algebraic multiply, yielding an object that has the same number of
179 * rows as the left operand and the same number of columns as the right
180 * operand. Section 5.10 "Vector and Matrix Operations" explains in
181 * more detail how vectors and matrices are operated on."
182 */
183 if (! multiply) {
184 return (type_a == type_b) ? type_a : glsl_error_type;
185 } else {
186 if (type_a->is_matrix() && type_b->is_matrix()) {
187 /* Matrix multiply. The columns of A must match the rows of B. Given
188 * the other previously tested constraints, this means the vector type
189 * of a row from A must be the same as the vector type of a column from
190 * B.
191 */
192 if (type_a->row_type() == type_b->column_type()) {
193 /* The resulting matrix has the number of columns of matrix B and
194 * the number of rows of matrix A. We get the row count of A by
195 * looking at the size of a vector that makes up a column. The
196 * transpose (size of a row) is done for B.
197 */
198 return
199 glsl_type::get_instance(type_a->base_type,
200 type_a->column_type()->vector_elements,
201 type_b->row_type()->vector_elements);
202 }
203 } else if (type_a->is_matrix()) {
204 /* A is a matrix and B is a column vector. Columns of A must match
205 * rows of B. Given the other previously tested constraints, this
206 * means the vector type of a row from A must be the same as the
207 * vector the type of B.
208 */
209 if (type_a->row_type() == type_b)
210 return type_b;
211 } else {
212 assert(type_b->is_matrix());
213
214 /* A is a row vector and B is a matrix. Columns of A must match rows
215 * of B. Given the other previously tested constraints, this means
216 * the type of A must be the same as the vector type of a column from
217 * B.
218 */
219 if (type_a == type_b->column_type())
220 return type_a;
221 }
222 }
223
224
225 /* "All other cases are illegal."
226 */
227 return glsl_error_type;
228 }
229
230
231 static const struct glsl_type *
232 unary_arithmetic_result_type(const struct glsl_type *type)
233 {
234 /* From GLSL 1.50 spec, page 57:
235 *
236 * "The arithmetic unary operators negate (-), post- and pre-increment
237 * and decrement (-- and ++) operate on integer or floating-point
238 * values (including vectors and matrices). All unary operators work
239 * component-wise on their operands. These result with the same type
240 * they operated on."
241 */
242 if (!is_numeric_base_type(type->base_type))
243 return glsl_error_type;
244
245 return type;
246 }
247
248
249 static const struct glsl_type *
250 modulus_result_type(const struct glsl_type *type_a,
251 const struct glsl_type *type_b)
252 {
253 /* From GLSL 1.50 spec, page 56:
254 * "The operator modulus (%) operates on signed or unsigned integers or
255 * integer vectors. The operand types must both be signed or both be
256 * unsigned."
257 */
258 if (! is_integer_base_type(type_a->base_type)
259 || ! is_integer_base_type(type_b->base_type)
260 || (type_a->base_type != type_b->base_type)) {
261 return glsl_error_type;
262 }
263
264 /* "The operands cannot be vectors of differing size. If one operand is
265 * a scalar and the other vector, then the scalar is applied component-
266 * wise to the vector, resulting in the same type as the vector. If both
267 * are vectors of the same size, the result is computed component-wise."
268 */
269 if (type_a->is_vector()) {
270 if (!type_b->is_vector()
271 || (type_a->vector_elements == type_b->vector_elements))
272 return type_a;
273 } else
274 return type_b;
275
276 /* "The operator modulus (%) is not defined for any other data types
277 * (non-integer types)."
278 */
279 return glsl_error_type;
280 }
281
282
283 static const struct glsl_type *
284 relational_result_type(const struct glsl_type *type_a,
285 const struct glsl_type *type_b,
286 struct _mesa_glsl_parse_state *state)
287 {
288 /* From GLSL 1.50 spec, page 56:
289 * "The relational operators greater than (>), less than (<), greater
290 * than or equal (>=), and less than or equal (<=) operate only on
291 * scalar integer and scalar floating-point expressions."
292 */
293 if (! is_numeric_base_type(type_a->base_type)
294 || ! is_numeric_base_type(type_b->base_type)
295 || !type_a->is_scalar()
296 || !type_b->is_scalar())
297 return glsl_error_type;
298
299 /* "Either the operands' types must match, or the conversions from
300 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
301 * operand, after which the types must match."
302 *
303 * This conversion was added in GLSL 1.20. If the compilation mode is
304 * GLSL 1.10, the conversion is skipped.
305 */
306 if (state->language_version >= 120) {
307 if ((type_a->base_type == GLSL_TYPE_FLOAT)
308 && (type_b->base_type != GLSL_TYPE_FLOAT)) {
309 /* FINISHME: Generate the implicit type conversion. */
310 } else if ((type_a->base_type != GLSL_TYPE_FLOAT)
311 && (type_b->base_type == GLSL_TYPE_FLOAT)) {
312 /* FINISHME: Generate the implicit type conversion. */
313 }
314 }
315
316 if (type_a->base_type != type_b->base_type)
317 return glsl_error_type;
318
319 /* "The result is scalar Boolean."
320 */
321 return glsl_bool_type;
322 }
323
324
325 /**
326 * Validates that a value can be assigned to a location with a specified type
327 *
328 * Validates that \c rhs can be assigned to some location. If the types are
329 * not an exact match but an automatic conversion is possible, \c rhs will be
330 * converted.
331 *
332 * \return
333 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
334 * Otherwise the actual RHS to be assigned will be returned. This may be
335 * \c rhs, or it may be \c rhs after some type conversion.
336 *
337 * \note
338 * In addition to being used for assignments, this function is used to
339 * type-check return values.
340 */
341 ir_instruction *
342 validate_assignment(const glsl_type *lhs_type, ir_instruction *rhs)
343 {
344 const glsl_type *const rhs_type = rhs->type;
345
346 /* If there is already some error in the RHS, just return it. Anything
347 * else will lead to an avalanche of error message back to the user.
348 */
349 if (rhs_type->is_error())
350 return rhs;
351
352 /* FINISHME: For GLSL 1.10, check that the types are not arrays. */
353
354 /* If the types are identical, the assignment can trivially proceed.
355 */
356 if (rhs_type == lhs_type)
357 return rhs;
358
359 /* FINISHME: Check for and apply automatic conversions. */
360 return NULL;
361 }
362
363
364 ir_instruction *
365 ast_node::hir(exec_list *instructions,
366 struct _mesa_glsl_parse_state *state)
367 {
368 (void) instructions;
369 (void) state;
370
371 return NULL;
372 }
373
374
375 ir_instruction *
376 ast_expression::hir(exec_list *instructions,
377 struct _mesa_glsl_parse_state *state)
378 {
379 static const int operations[AST_NUM_OPERATORS] = {
380 -1, /* ast_assign doesn't convert to ir_expression. */
381 -1, /* ast_plus doesn't convert to ir_expression. */
382 ir_unop_neg,
383 ir_binop_add,
384 ir_binop_sub,
385 ir_binop_mul,
386 ir_binop_div,
387 ir_binop_mod,
388 ir_binop_lshift,
389 ir_binop_rshift,
390 ir_binop_less,
391 ir_binop_greater,
392 ir_binop_lequal,
393 ir_binop_gequal,
394 ir_binop_equal,
395 ir_binop_nequal,
396 ir_binop_bit_and,
397 ir_binop_bit_xor,
398 ir_binop_bit_or,
399 ir_unop_bit_not,
400 ir_binop_logic_and,
401 ir_binop_logic_xor,
402 ir_binop_logic_or,
403 ir_unop_logic_not,
404
405 /* Note: The following block of expression types actually convert
406 * to multiple IR instructions.
407 */
408 ir_binop_mul, /* ast_mul_assign */
409 ir_binop_div, /* ast_div_assign */
410 ir_binop_mod, /* ast_mod_assign */
411 ir_binop_add, /* ast_add_assign */
412 ir_binop_sub, /* ast_sub_assign */
413 ir_binop_lshift, /* ast_ls_assign */
414 ir_binop_rshift, /* ast_rs_assign */
415 ir_binop_bit_and, /* ast_and_assign */
416 ir_binop_bit_xor, /* ast_xor_assign */
417 ir_binop_bit_or, /* ast_or_assign */
418
419 -1, /* ast_conditional doesn't convert to ir_expression. */
420 -1, /* ast_pre_inc doesn't convert to ir_expression. */
421 -1, /* ast_pre_dec doesn't convert to ir_expression. */
422 -1, /* ast_post_inc doesn't convert to ir_expression. */
423 -1, /* ast_post_dec doesn't convert to ir_expression. */
424 -1, /* ast_field_selection doesn't conv to ir_expression. */
425 -1, /* ast_array_index doesn't convert to ir_expression. */
426 -1, /* ast_function_call doesn't conv to ir_expression. */
427 -1, /* ast_identifier doesn't convert to ir_expression. */
428 -1, /* ast_int_constant doesn't convert to ir_expression. */
429 -1, /* ast_uint_constant doesn't conv to ir_expression. */
430 -1, /* ast_float_constant doesn't conv to ir_expression. */
431 -1, /* ast_bool_constant doesn't conv to ir_expression. */
432 -1, /* ast_sequence doesn't convert to ir_expression. */
433 };
434 ir_instruction *result = NULL;
435 ir_instruction *op[2];
436 struct simple_node op_list;
437 const struct glsl_type *type = glsl_error_type;
438 bool error_emitted = false;
439 YYLTYPE loc;
440
441 loc = this->get_location();
442 make_empty_list(& op_list);
443
444 switch (this->oper) {
445 case ast_assign: {
446 op[0] = this->subexpressions[0]->hir(instructions, state);
447 op[1] = this->subexpressions[1]->hir(instructions, state);
448
449 error_emitted = ((op[0]->type == glsl_error_type)
450 || (op[1]->type == glsl_error_type));
451
452 type = op[0]->type;
453 if (!error_emitted) {
454 YYLTYPE loc;
455
456 /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
457 loc = this->subexpressions[0]->get_location();
458 if (op[0]->mode != ir_op_dereference) {
459 _mesa_glsl_error(& loc, state, "invalid lvalue in assignment");
460 error_emitted = true;
461
462 type = glsl_error_type;
463 } else {
464 const struct ir_dereference *const ref =
465 (struct ir_dereference *) op[0];
466 const struct ir_variable *const var =
467 (struct ir_variable *) ref->var;
468
469 if ((var != NULL)
470 && (var->mode == ir_op_var_decl)
471 && (var->read_only)) {
472 _mesa_glsl_error(& loc, state, "cannot assign to read-only "
473 "variable `%s'", var->name);
474 error_emitted = true;
475
476 type = glsl_error_type;
477 }
478 }
479 }
480
481 ir_instruction *rhs = validate_assignment(op[0]->type, op[1]);
482 if (rhs == NULL) {
483 type = glsl_error_type;
484 rhs = op[1];
485 }
486
487 ir_instruction *tmp = new ir_assignment(op[0], op[1], NULL);
488 instructions->push_tail(tmp);
489
490 result = op[0];
491 break;
492 }
493
494 case ast_plus:
495 op[0] = this->subexpressions[0]->hir(instructions, state);
496
497 error_emitted = (op[0]->type == glsl_error_type);
498 if (type == glsl_error_type)
499 op[0]->type = type;
500
501 result = op[0];
502 break;
503
504 case ast_neg:
505 op[0] = this->subexpressions[0]->hir(instructions, state);
506
507 type = unary_arithmetic_result_type(op[0]->type);
508
509 error_emitted = (op[0]->type == glsl_error_type);
510
511 result = new ir_expression(operations[this->oper], type,
512 op[0], NULL);
513 break;
514
515 case ast_add:
516 case ast_sub:
517 case ast_mul:
518 case ast_div:
519 op[0] = this->subexpressions[0]->hir(instructions, state);
520 op[1] = this->subexpressions[1]->hir(instructions, state);
521
522 type = arithmetic_result_type(op[0]->type, op[1]->type,
523 (this->oper == ast_mul),
524 state);
525
526 result = new ir_expression(operations[this->oper], type,
527 op[0], op[1]);
528 break;
529
530 case ast_mod:
531 op[0] = this->subexpressions[0]->hir(instructions, state);
532 op[1] = this->subexpressions[1]->hir(instructions, state);
533
534 error_emitted = ((op[0]->type == glsl_error_type)
535 || (op[1]->type == glsl_error_type));
536
537 type = modulus_result_type(op[0]->type, op[1]->type);
538
539 assert(operations[this->oper] == ir_binop_mod);
540
541 result = new ir_expression(operations[this->oper], type,
542 op[0], op[1]);
543 break;
544
545 case ast_lshift:
546 case ast_rshift:
547 /* FINISHME: Implement bit-shift operators. */
548 break;
549
550 case ast_less:
551 case ast_greater:
552 case ast_lequal:
553 case ast_gequal:
554 op[0] = this->subexpressions[0]->hir(instructions, state);
555 op[1] = this->subexpressions[1]->hir(instructions, state);
556
557 error_emitted = ((op[0]->type == glsl_error_type)
558 || (op[1]->type == glsl_error_type));
559
560 type = relational_result_type(op[0]->type, op[1]->type, state);
561
562 /* The relational operators must either generate an error or result
563 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
564 */
565 assert((type == glsl_error_type)
566 || ((type->base_type == GLSL_TYPE_BOOL)
567 && type->is_scalar()));
568
569 result = new ir_expression(operations[this->oper], type,
570 op[0], op[1]);
571 break;
572
573 case ast_nequal:
574 case ast_equal:
575 /* FINISHME: Implement equality operators. */
576 break;
577
578 case ast_bit_and:
579 case ast_bit_xor:
580 case ast_bit_or:
581 case ast_bit_not:
582 /* FINISHME: Implement bit-wise operators. */
583 break;
584
585 case ast_logic_and:
586 case ast_logic_xor:
587 case ast_logic_or:
588 case ast_logic_not:
589 /* FINISHME: Implement logical operators. */
590 break;
591
592 case ast_mul_assign:
593 case ast_div_assign:
594 case ast_add_assign:
595 case ast_sub_assign: {
596 struct ir_instruction *temp_rhs;
597
598 op[0] = this->subexpressions[0]->hir(instructions, state);
599 op[1] = this->subexpressions[1]->hir(instructions, state);
600
601 error_emitted = ((op[0]->type == glsl_error_type)
602 || (op[1]->type == glsl_error_type));
603
604 type = arithmetic_result_type(op[0]->type, op[1]->type,
605 (this->oper == ast_mul_assign),
606 state);
607
608 temp_rhs = new ir_expression(operations[this->oper], type,
609 op[0], op[1]);
610
611 /* FINISHME: This is copied from ast_assign above. It should
612 * FINISHME: probably be consolidated.
613 */
614 error_emitted = ((op[0]->type == glsl_error_type)
615 || (temp_rhs->type == glsl_error_type));
616
617 type = op[0]->type;
618 if (!error_emitted) {
619 YYLTYPE loc;
620
621 /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
622 loc = this->subexpressions[0]->get_location();
623 if (op[0]->mode != ir_op_dereference) {
624 _mesa_glsl_error(& loc, state, "invalid lvalue in assignment");
625 error_emitted = true;
626
627 type = glsl_error_type;
628 } else {
629 const struct ir_dereference *const ref =
630 (struct ir_dereference *) op[0];
631 const struct ir_variable *const var =
632 (struct ir_variable *) ref->var;
633
634 if ((var != NULL)
635 && (var->mode == ir_op_var_decl)
636 && (var->read_only)) {
637 _mesa_glsl_error(& loc, state, "cannot assign to read-only "
638 "variable `%s'", var->name);
639 error_emitted = true;
640
641 type = glsl_error_type;
642 }
643 }
644 }
645
646 ir_instruction *rhs = validate_assignment(op[0]->type, temp_rhs);
647 if (rhs == NULL) {
648 type = glsl_error_type;
649 rhs = temp_rhs;
650 }
651
652 ir_instruction *tmp = new ir_assignment(op[0], rhs, NULL);
653 instructions->push_tail(tmp);
654
655 /* GLSL 1.10 does not allow array assignment. However, we don't have to
656 * explicitly test for this because none of the binary expression
657 * operators allow array operands either.
658 */
659
660 result = op[0];
661 break;
662 }
663
664 case ast_mod_assign:
665
666 case ast_ls_assign:
667 case ast_rs_assign:
668
669 case ast_and_assign:
670 case ast_xor_assign:
671 case ast_or_assign:
672
673 case ast_conditional:
674
675 case ast_pre_inc:
676 case ast_pre_dec:
677
678 case ast_post_inc:
679 case ast_post_dec:
680 break;
681
682 case ast_field_selection:
683 result = _mesa_ast_field_selection_to_hir(this, instructions, state);
684 type = result->type;
685 break;
686
687 case ast_array_index:
688 break;
689
690 case ast_function_call:
691 /* Should *NEVER* get here. ast_function_call should always be handled
692 * by ast_function_expression::hir.
693 */
694 assert(0);
695 break;
696
697 case ast_identifier: {
698 /* ast_identifier can appear several places in a full abstract syntax
699 * tree. This particular use must be at location specified in the grammar
700 * as 'variable_identifier'.
701 */
702 ir_variable *var =
703 state->symbols->get_variable(this->primary_expression.identifier);
704
705 result = new ir_dereference(var);
706
707 if (var != NULL) {
708 type = result->type;
709 } else {
710 _mesa_glsl_error(& loc, state, "`%s' undeclared",
711 this->primary_expression.identifier);
712
713 error_emitted = true;
714 }
715 break;
716 }
717
718 case ast_int_constant:
719 type = glsl_int_type;
720 result = new ir_constant(type, & this->primary_expression);
721 break;
722
723 case ast_uint_constant:
724 type = glsl_uint_type;
725 result = new ir_constant(type, & this->primary_expression);
726 break;
727
728 case ast_float_constant:
729 type = glsl_float_type;
730 result = new ir_constant(type, & this->primary_expression);
731 break;
732
733 case ast_bool_constant:
734 type = glsl_bool_type;
735 result = new ir_constant(type, & this->primary_expression);
736 break;
737
738 case ast_sequence: {
739 struct simple_node *ptr;
740
741 /* It should not be possible to generate a sequence in the AST without
742 * any expressions in it.
743 */
744 assert(!is_empty_list(&this->expressions));
745
746 /* The r-value of a sequence is the last expression in the sequence. If
747 * the other expressions in the sequence do not have side-effects (and
748 * therefore add instructions to the instruction list), they get dropped
749 * on the floor.
750 */
751 foreach (ptr, &this->expressions)
752 result = ((ast_node *)ptr)->hir(instructions, state);
753
754 type = result->type;
755
756 /* Any errors should have already been emitted in the loop above.
757 */
758 error_emitted = true;
759 break;
760 }
761 }
762
763 if (is_error_type(type) && !error_emitted)
764 _mesa_glsl_error(& loc, state, "type mismatch");
765
766 return result;
767 }
768
769
770 ir_instruction *
771 ast_expression_statement::hir(exec_list *instructions,
772 struct _mesa_glsl_parse_state *state)
773 {
774 /* It is possible to have expression statements that don't have an
775 * expression. This is the solitary semicolon:
776 *
777 * for (i = 0; i < 5; i++)
778 * ;
779 *
780 * In this case the expression will be NULL. Test for NULL and don't do
781 * anything in that case.
782 */
783 if (expression != NULL)
784 expression->hir(instructions, state);
785
786 /* Statements do not have r-values.
787 */
788 return NULL;
789 }
790
791
792 ir_instruction *
793 ast_compound_statement::hir(exec_list *instructions,
794 struct _mesa_glsl_parse_state *state)
795 {
796 struct simple_node *ptr;
797
798
799 if (new_scope)
800 state->symbols->push_scope();
801
802 foreach (ptr, &statements)
803 ((ast_node *)ptr)->hir(instructions, state);
804
805 if (new_scope)
806 state->symbols->pop_scope();
807
808 /* Compound statements do not have r-values.
809 */
810 return NULL;
811 }
812
813
814 static const struct glsl_type *
815 type_specifier_to_glsl_type(const struct ast_type_specifier *spec,
816 const char **name,
817 struct _mesa_glsl_parse_state *state)
818 {
819 struct glsl_type *type;
820
821 if (spec->type_specifier == ast_struct) {
822 /* FINISHME: Handle annonymous structures. */
823 type = NULL;
824 } else {
825 type = state->symbols->get_type(spec->type_name);
826 *name = spec->type_name;
827
828 /* FINISHME: Handle array declarations. Note that this requires complete
829 * FINISHME: handling of constant expressions.
830 */
831 }
832
833 return type;
834 }
835
836
837 static void
838 apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
839 struct ir_variable *var,
840 struct _mesa_glsl_parse_state *state)
841 {
842 if (qual->invariant)
843 var->invariant = 1;
844
845 /* FINISHME: Mark 'in' variables at global scope as read-only. */
846 if (qual->constant || qual->attribute || qual->uniform
847 || (qual->varying && (state->target == fragment_shader)))
848 var->read_only = 1;
849
850 if (qual->centroid)
851 var->centroid = 1;
852
853 if (qual->in && qual->out)
854 var->mode = ir_var_inout;
855 else if (qual->attribute || qual->in
856 || (qual->varying && (state->target == fragment_shader)))
857 var->mode = ir_var_in;
858 else if (qual->out || (qual->varying && (state->target == vertex_shader)))
859 var->mode = ir_var_out;
860 else if (qual->uniform)
861 var->mode = ir_var_uniform;
862 else
863 var->mode = ir_var_auto;
864
865 if (qual->flat)
866 var->interpolation = ir_var_flat;
867 else if (qual->noperspective)
868 var->interpolation = ir_var_noperspective;
869 else
870 var->interpolation = ir_var_smooth;
871 }
872
873
874 ir_instruction *
875 ast_declarator_list::hir(exec_list *instructions,
876 struct _mesa_glsl_parse_state *state)
877 {
878 struct simple_node *ptr;
879 const struct glsl_type *decl_type;
880 const char *type_name = NULL;
881
882
883 /* FINISHME: Handle vertex shader "invariant" declarations that do not
884 * FINISHME: include a type. These re-declare built-in variables to be
885 * FINISHME: invariant.
886 */
887
888 decl_type = type_specifier_to_glsl_type(this->type->specifier,
889 & type_name, state);
890
891 foreach (ptr, &this->declarations) {
892 struct ast_declaration *const decl = (struct ast_declaration * )ptr;
893 const struct glsl_type *var_type;
894 struct ir_variable *var;
895
896
897 /* FINISHME: Emit a warning if a variable declaration shadows a
898 * FINISHME: declaration at a higher scope.
899 */
900
901 if ((decl_type == NULL) || decl_type->is_void()) {
902 YYLTYPE loc;
903
904 loc = this->get_location();
905 if (type_name != NULL) {
906 _mesa_glsl_error(& loc, state,
907 "invalid type `%s' in declaration of `%s'",
908 type_name, decl->identifier);
909 } else {
910 _mesa_glsl_error(& loc, state,
911 "invalid type in declaration of `%s'",
912 decl->identifier);
913 }
914 continue;
915 }
916
917 if (decl->is_array) {
918 /* FINISHME: Handle array declarations. Note that this requires
919 * FINISHME: complete handling of constant expressions.
920 */
921
922 /* FINISHME: Reject delcarations of multidimensional arrays. */
923 } else {
924 var_type = decl_type;
925 }
926
927 var = new ir_variable(var_type, decl->identifier);
928
929 /* FINISHME: Variables that are attribute, uniform, varying, in, or
930 * FINISHME: out varibles must be declared either at global scope or
931 * FINISHME: in a parameter list (in and out only).
932 */
933
934 apply_type_qualifier_to_variable(& this->type->qualifier, var, state);
935
936 /* Attempt to add the variable to the symbol table. If this fails, it
937 * means the variable has already been declared at this scope.
938 */
939 if (state->symbols->name_declared_this_scope(decl->identifier)) {
940 YYLTYPE loc = this->get_location();
941
942 _mesa_glsl_error(& loc, state, "`%s' redeclared",
943 decl->identifier);
944 continue;
945 }
946
947 const bool added_variable =
948 state->symbols->add_variable(decl->identifier, var);
949 assert(added_variable);
950
951 instructions->push_tail(var);
952
953 /* FINISHME: Process the declaration initializer. */
954 }
955
956 /* Variable declarations do not have r-values.
957 */
958 return NULL;
959 }
960
961
962 ir_instruction *
963 ast_parameter_declarator::hir(exec_list *instructions,
964 struct _mesa_glsl_parse_state *state)
965 {
966 const struct glsl_type *type;
967 const char *name = NULL;
968
969
970 type = type_specifier_to_glsl_type(this->type->specifier, & name, state);
971
972 if (type == NULL) {
973 YYLTYPE loc = this->get_location();
974 if (name != NULL) {
975 _mesa_glsl_error(& loc, state,
976 "invalid type `%s' in declaration of `%s'",
977 name, this->identifier);
978 } else {
979 _mesa_glsl_error(& loc, state,
980 "invalid type in declaration of `%s'",
981 this->identifier);
982 }
983
984 type = glsl_error_type;
985 }
986
987 ir_variable *var = new ir_variable(type, this->identifier);
988
989 /* FINISHME: Handle array declarations. Note that this requires
990 * FINISHME: complete handling of constant expressions.
991 */
992
993 /* Apply any specified qualifiers to the parameter declaration. Note that
994 * for function parameters the default mode is 'in'.
995 */
996 apply_type_qualifier_to_variable(& this->type->qualifier, var, state);
997 if (var->mode == ir_var_auto)
998 var->mode = ir_var_in;
999
1000 instructions->push_tail(var);
1001
1002 /* Parameter declarations do not have r-values.
1003 */
1004 return NULL;
1005 }
1006
1007
1008 static void
1009 ast_function_parameters_to_hir(struct simple_node *ast_parameters,
1010 exec_list *ir_parameters,
1011 struct _mesa_glsl_parse_state *state)
1012 {
1013 struct simple_node *ptr;
1014
1015 foreach (ptr, ast_parameters) {
1016 ((ast_node *)ptr)->hir(ir_parameters, state);
1017 }
1018 }
1019
1020
1021 static bool
1022 parameter_lists_match(exec_list *list_a, exec_list *list_b)
1023 {
1024 exec_list_iterator iter_a = list_a->iterator();
1025 exec_list_iterator iter_b = list_b->iterator();
1026
1027 while (iter_a.has_next()) {
1028 /* If all of the parameters from the other parameter list have been
1029 * exhausted, the lists have different length and, by definition,
1030 * do not match.
1031 */
1032 if (!iter_b.has_next())
1033 return false;
1034
1035 /* If the types of the parameters do not match, the parameters lists
1036 * are different.
1037 */
1038 /* FINISHME */
1039
1040
1041 iter_a.next();
1042 iter_b.next();
1043 }
1044
1045 return true;
1046 }
1047
1048
1049 ir_instruction *
1050 ast_function_definition::hir(exec_list *instructions,
1051 struct _mesa_glsl_parse_state *state)
1052 {
1053 ir_label *label;
1054 ir_function_signature *signature = NULL;
1055 ir_function *f = NULL;
1056 exec_list parameters;
1057
1058
1059 /* Convert the list of function parameters to HIR now so that they can be
1060 * used below to compare this function's signature with previously seen
1061 * signatures for functions with the same name.
1062 */
1063 ast_function_parameters_to_hir(& this->prototype->parameters, & parameters,
1064 state);
1065
1066 const char *return_type_name;
1067 const glsl_type *return_type =
1068 type_specifier_to_glsl_type(this->prototype->return_type->specifier,
1069 & return_type_name, state);
1070
1071 assert(return_type != NULL);
1072
1073
1074 /* Verify that this function's signature either doesn't match a previously
1075 * seen signature for a function with the same name, or, if a match is found,
1076 * that the previously seen signature does not have an associated definition.
1077 */
1078 const char *const name = this->prototype->identifier;
1079 f = state->symbols->get_function(name);
1080 if (f != NULL) {
1081 foreach_iter(exec_list_iterator, iter, f->signatures) {
1082 signature = (struct ir_function_signature *) iter.get();
1083
1084 /* Compare the parameter list of the function being defined to the
1085 * existing function. If the parameter lists match, then the return
1086 * type must also match and the existing function must not have a
1087 * definition.
1088 */
1089 if (parameter_lists_match(& parameters, & signature->parameters)) {
1090 /* FINISHME: Compare return types. */
1091
1092 if (signature->definition != NULL) {
1093 YYLTYPE loc = this->get_location();
1094
1095 _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
1096 signature = NULL;
1097 break;
1098 }
1099 }
1100
1101 signature = NULL;
1102 }
1103
1104 } else if (state->symbols->name_declared_this_scope(name)) {
1105 /* This function name shadows a non-function use of the same name.
1106 */
1107 YYLTYPE loc = this->get_location();
1108
1109 _mesa_glsl_error(& loc, state, "function name `%s' conflicts with "
1110 "non-function", name);
1111 signature = NULL;
1112 } else {
1113 f = new ir_function(name);
1114 state->symbols->add_function(f->name, f);
1115 }
1116
1117
1118 /* Finish storing the information about this new function in its signature.
1119 */
1120 if (signature == NULL) {
1121 signature = new ir_function_signature(return_type);
1122 f->signatures.push_tail(signature);
1123 } else {
1124 /* Destroy all of the previous parameter information. The previous
1125 * parameter information comes from the function prototype, and it can
1126 * either include invalid parameter names or may not have names at all.
1127 */
1128 foreach_iter(exec_list_iterator, iter, signature->parameters) {
1129 assert(((struct ir_instruction *)iter.get())->mode == ir_op_var_decl);
1130
1131 iter.remove();
1132 delete iter.get();
1133 }
1134 }
1135
1136
1137 assert(state->current_function == NULL);
1138 state->current_function = signature;
1139
1140 ast_function_parameters_to_hir(& this->prototype->parameters,
1141 & signature->parameters,
1142 state);
1143 /* FINISHME: Set signature->return_type */
1144
1145 label = new ir_label(name);
1146 if (signature->definition == NULL) {
1147 signature->definition = label;
1148 }
1149 instructions->push_tail(label);
1150
1151 /* Add the function parameters to the symbol table. During this step the
1152 * parameter declarations are also moved from the temporary "parameters" list
1153 * to the instruction list. There are other more efficient ways to do this,
1154 * but they involve ugly linked-list gymnastics.
1155 */
1156 state->symbols->push_scope();
1157 foreach_iter(exec_list_iterator, iter, parameters) {
1158 ir_variable *const var = (ir_variable *) iter.get();
1159
1160 assert(((ir_instruction *)var)->mode == ir_op_var_decl);
1161
1162 iter.remove();
1163 instructions->push_tail(var);
1164
1165 /* The only way a parameter would "exist" is if two parameters have
1166 * the same name.
1167 */
1168 if (state->symbols->name_declared_this_scope(var->name)) {
1169 YYLTYPE loc = this->get_location();
1170
1171 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
1172 } else {
1173 state->symbols->add_variable(var->name, var);
1174 }
1175 }
1176
1177 /* Convert the body of the function to HIR, and append the resulting
1178 * instructions to the list that currently consists of the function label
1179 * and the function parameters.
1180 */
1181 this->body->hir(instructions, state);
1182
1183 state->symbols->pop_scope();
1184
1185 assert(state->current_function == signature);
1186 state->current_function = NULL;
1187
1188 /* Function definitions do not have r-values.
1189 */
1190 return NULL;
1191 }
1192
1193
1194 ir_instruction *
1195 ast_jump_statement::hir(exec_list *instructions,
1196 struct _mesa_glsl_parse_state *state)
1197 {
1198
1199 if (mode == ast_return) {
1200 ir_return *inst;
1201
1202 if (opt_return_value) {
1203 /* FINISHME: Make sure the enclosing function has a non-void return
1204 * FINISHME: type.
1205 */
1206
1207 ir_expression *const ret = (ir_expression *)
1208 opt_return_value->hir(instructions, state);
1209 assert(ret != NULL);
1210
1211 /* FINISHME: Make sure the type of the return value matches the return
1212 * FINISHME: type of the enclosing function.
1213 */
1214
1215 inst = new ir_return(ret);
1216 } else {
1217 /* FINISHME: Make sure the enclosing function has a void return type.
1218 */
1219 inst = new ir_return;
1220 }
1221
1222 instructions->push_tail(inst);
1223 }
1224
1225 /* Jump instructions do not have r-values.
1226 */
1227 return NULL;
1228 }