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