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