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