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