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