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