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