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