blob: 5d7c073ca906efb82c0260d87dce86ae8711240e [file] [log] [blame]
Ian Romanicka87ac252010-02-22 13:19:34 -08001/*
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"
Ian Romanick8bde4ce2010-03-19 11:57:24 -070053#include "glsl_symbol_table.h"
Ian Romanicka87ac252010-02-22 13:19:34 -080054#include "glsl_parser_extras.h"
55#include "ast.h"
56#include "glsl_types.h"
57#include "ir.h"
58
Ian Romanickd949a9a2010-03-10 09:55:22 -080059void
60_mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
61{
62 struct simple_node *ptr;
63
Ian Romanickadfb0cd2010-03-10 10:43:16 -080064 _mesa_glsl_initialize_variables(instructions, state);
Ian Romanicka4e92c42010-03-25 17:02:22 -070065 _mesa_glsl_initialize_constructors(instructions, state);
Eric Anholtc22c4002010-03-26 18:20:30 -070066 _mesa_glsl_initialize_functions(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -080067
Ian Romanick41ec6a42010-03-19 17:08:05 -070068 state->current_function = NULL;
69
Ian Romanickd949a9a2010-03-10 09:55:22 -080070 foreach (ptr, & state->translation_unit) {
71 ((ast_node *)ptr)->hir(instructions, state);
72 }
73}
74
75
Ian Romanick01045362010-03-29 16:17:56 -070076/**
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
Ian Romanickbfb09c22010-03-29 16:32:55 -070090apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
Ian Romanick01045362010-03-29 16:17:56 -070091 struct _mesa_glsl_parse_state *state)
92{
Ian Romanickbfb09c22010-03-29 16:32:55 -070093 if (to->base_type == from->type->base_type)
Ian Romanick01045362010-03-29 16:17:56 -070094 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 */
Ian Romanickbfb09c22010-03-29 16:32:55 -0700112 if (!to->is_float() || !from->type->is_numeric())
Ian Romanick01045362010-03-29 16:17:56 -0700113 return false;
114
Ian Romanickbfb09c22010-03-29 16:32:55 -0700115 switch (from->type->base_type) {
Ian Romanick01045362010-03-29 16:17:56 -0700116 case GLSL_TYPE_INT:
Ian Romanickbfb09c22010-03-29 16:32:55 -0700117 from = new ir_expression(ir_unop_i2f, to, from, NULL);
Ian Romanick01045362010-03-29 16:17:56 -0700118 break;
119 case GLSL_TYPE_UINT:
Ian Romanickbfb09c22010-03-29 16:32:55 -0700120 from = new ir_expression(ir_unop_u2f, to, from, NULL);
Ian Romanick01045362010-03-29 16:17:56 -0700121 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
Ian Romanicka87ac252010-02-22 13:19:34 -0800132static const struct glsl_type *
Ian Romanickbfb09c22010-03-29 16:32:55 -0700133arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
Ian Romanicka87ac252010-02-22 13:19:34 -0800134 bool multiply,
Eric Anholta13bb142010-03-31 16:38:11 -1000135 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800136{
Ian Romanickbfb09c22010-03-29 16:32:55 -0700137 const glsl_type *const type_a = value_a->type;
138 const glsl_type *const type_b = value_b->type;
Ian Romanick01045362010-03-29 16:17:56 -0700139
Ian Romanicka87ac252010-02-22 13:19:34 -0800140 /* 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 */
Ian Romanick60b54d92010-03-24 17:08:13 -0700146 if (!type_a->is_numeric() || !type_b->is_numeric()) {
Eric Anholta13bb142010-03-31 16:38:11 -1000147 _mesa_glsl_error(loc, state,
148 "Operands to arithmetic operators must be numeric");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700149 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800150 }
151
152
153 /* "If one operand is floating-point based and the other is
154 * not, then the conversions from Section 4.1.10 "Implicit
155 * Conversions" are applied to the non-floating-point-based operand."
Ian Romanicka87ac252010-02-22 13:19:34 -0800156 */
Ian Romanick01045362010-03-29 16:17:56 -0700157 if (!apply_implicit_conversion(type_a, value_b, state)
158 && !apply_implicit_conversion(type_b, value_a, state)) {
Eric Anholta13bb142010-03-31 16:38:11 -1000159 _mesa_glsl_error(loc, state,
160 "Could not implicitly convert operands to "
161 "arithmetic operator");
Ian Romanick01045362010-03-29 16:17:56 -0700162 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800163 }
164
165 /* "If the operands are integer types, they must both be signed or
166 * both be unsigned."
167 *
168 * From this rule and the preceeding conversion it can be inferred that
169 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
Ian Romanick60b54d92010-03-24 17:08:13 -0700170 * The is_numeric check above already filtered out the case where either
171 * type is not one of these, so now the base types need only be tested for
172 * equality.
Ian Romanicka87ac252010-02-22 13:19:34 -0800173 */
174 if (type_a->base_type != type_b->base_type) {
Eric Anholta13bb142010-03-31 16:38:11 -1000175 _mesa_glsl_error(loc, state,
176 "base type mismatch for arithmetic operator");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700177 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800178 }
179
180 /* "All arithmetic binary operators result in the same fundamental type
181 * (signed integer, unsigned integer, or floating-point) as the
182 * operands they operate on, after operand type conversion. After
183 * conversion, the following cases are valid
184 *
185 * * The two operands are scalars. In this case the operation is
186 * applied, resulting in a scalar."
187 */
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800188 if (type_a->is_scalar() && type_b->is_scalar())
Ian Romanicka87ac252010-02-22 13:19:34 -0800189 return type_a;
190
191 /* "* One operand is a scalar, and the other is a vector or matrix.
192 * In this case, the scalar operation is applied independently to each
193 * component of the vector or matrix, resulting in the same size
194 * vector or matrix."
195 */
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800196 if (type_a->is_scalar()) {
197 if (!type_b->is_scalar())
Ian Romanicka87ac252010-02-22 13:19:34 -0800198 return type_b;
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800199 } else if (type_b->is_scalar()) {
Ian Romanicka87ac252010-02-22 13:19:34 -0800200 return type_a;
201 }
202
203 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
204 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
205 * handled.
206 */
Ian Romanick60b54d92010-03-24 17:08:13 -0700207 assert(!type_a->is_scalar());
208 assert(!type_b->is_scalar());
Ian Romanicka87ac252010-02-22 13:19:34 -0800209
210 /* "* The two operands are vectors of the same size. In this case, the
211 * operation is done component-wise resulting in the same size
212 * vector."
213 */
Ian Romanicka2dd22f2010-03-09 15:55:16 -0800214 if (type_a->is_vector() && type_b->is_vector()) {
Eric Anholta13bb142010-03-31 16:38:11 -1000215 if (type_a == type_b) {
216 return type_a;
217 } else {
218 _mesa_glsl_error(loc, state,
219 "vector size mismatch for arithmetic operator");
220 return glsl_type::error_type;
221 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800222 }
223
224 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
225 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
226 * <vector, vector> have been handled. At least one of the operands must
227 * be matrix. Further, since there are no integer matrix types, the base
228 * type of both operands must be float.
229 */
Ian Romanick60b54d92010-03-24 17:08:13 -0700230 assert(type_a->is_matrix() || type_b->is_matrix());
Ian Romanicka87ac252010-02-22 13:19:34 -0800231 assert(type_a->base_type == GLSL_TYPE_FLOAT);
232 assert(type_b->base_type == GLSL_TYPE_FLOAT);
233
234 /* "* The operator is add (+), subtract (-), or divide (/), and the
235 * operands are matrices with the same number of rows and the same
236 * number of columns. In this case, the operation is done component-
237 * wise resulting in the same size matrix."
238 * * The operator is multiply (*), where both operands are matrices or
239 * one operand is a vector and the other a matrix. A right vector
240 * operand is treated as a column vector and a left vector operand as a
241 * row vector. In all these cases, it is required that the number of
242 * columns of the left operand is equal to the number of rows of the
243 * right operand. Then, the multiply (*) operation does a linear
244 * algebraic multiply, yielding an object that has the same number of
245 * rows as the left operand and the same number of columns as the right
246 * operand. Section 5.10 "Vector and Matrix Operations" explains in
247 * more detail how vectors and matrices are operated on."
248 */
249 if (! multiply) {
Eric Anholta13bb142010-03-31 16:38:11 -1000250 if (type_a == type_b)
251 return type_a;
Ian Romanicka87ac252010-02-22 13:19:34 -0800252 } else {
Ian Romanickfce11502010-03-09 15:58:52 -0800253 if (type_a->is_matrix() && type_b->is_matrix()) {
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700254 /* Matrix multiply. The columns of A must match the rows of B. Given
255 * the other previously tested constraints, this means the vector type
256 * of a row from A must be the same as the vector type of a column from
257 * B.
258 */
259 if (type_a->row_type() == type_b->column_type()) {
260 /* The resulting matrix has the number of columns of matrix B and
261 * the number of rows of matrix A. We get the row count of A by
262 * looking at the size of a vector that makes up a column. The
263 * transpose (size of a row) is done for B.
264 */
Eric Anholta13bb142010-03-31 16:38:11 -1000265 const glsl_type *const type =
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700266 glsl_type::get_instance(type_a->base_type,
267 type_a->column_type()->vector_elements,
268 type_b->row_type()->vector_elements);
Eric Anholta13bb142010-03-31 16:38:11 -1000269 assert(type != glsl_type::error_type);
270
271 return type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800272 }
Ian Romanickfce11502010-03-09 15:58:52 -0800273 } else if (type_a->is_matrix()) {
Ian Romanicka87ac252010-02-22 13:19:34 -0800274 /* A is a matrix and B is a column vector. Columns of A must match
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700275 * rows of B. Given the other previously tested constraints, this
276 * means the vector type of a row from A must be the same as the
277 * vector the type of B.
Ian Romanicka87ac252010-02-22 13:19:34 -0800278 */
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700279 if (type_a->row_type() == type_b)
Ian Romanicka87ac252010-02-22 13:19:34 -0800280 return type_b;
281 } else {
Ian Romanickfce11502010-03-09 15:58:52 -0800282 assert(type_b->is_matrix());
Ian Romanicka87ac252010-02-22 13:19:34 -0800283
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700284 /* A is a row vector and B is a matrix. Columns of A must match rows
285 * of B. Given the other previously tested constraints, this means
286 * the type of A must be the same as the vector type of a column from
287 * B.
Ian Romanicka87ac252010-02-22 13:19:34 -0800288 */
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700289 if (type_a == type_b->column_type())
Ian Romanicka87ac252010-02-22 13:19:34 -0800290 return type_a;
291 }
Eric Anholta13bb142010-03-31 16:38:11 -1000292
293 _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
294 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800295 }
296
297
298 /* "All other cases are illegal."
299 */
Eric Anholta13bb142010-03-31 16:38:11 -1000300 _mesa_glsl_error(loc, state, "type mismatch");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700301 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800302}
303
304
305static const struct glsl_type *
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000306unary_arithmetic_result_type(const struct glsl_type *type,
307 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800308{
309 /* From GLSL 1.50 spec, page 57:
310 *
311 * "The arithmetic unary operators negate (-), post- and pre-increment
312 * and decrement (-- and ++) operate on integer or floating-point
313 * values (including vectors and matrices). All unary operators work
314 * component-wise on their operands. These result with the same type
315 * they operated on."
316 */
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000317 if (!type->is_numeric()) {
318 _mesa_glsl_error(loc, state,
319 "Operands to arithmetic operators must be numeric");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700320 return glsl_type::error_type;
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000321 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800322
323 return type;
324}
325
326
327static const struct glsl_type *
328modulus_result_type(const struct glsl_type *type_a,
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000329 const struct glsl_type *type_b,
330 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800331{
332 /* From GLSL 1.50 spec, page 56:
333 * "The operator modulus (%) operates on signed or unsigned integers or
334 * integer vectors. The operand types must both be signed or both be
335 * unsigned."
336 */
Ian Romanick40176e22010-03-26 14:38:37 -0700337 if (!type_a->is_integer() || !type_b->is_integer()
Ian Romanicka87ac252010-02-22 13:19:34 -0800338 || (type_a->base_type != type_b->base_type)) {
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000339 _mesa_glsl_error(loc, state, "type mismatch");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700340 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800341 }
342
343 /* "The operands cannot be vectors of differing size. If one operand is
344 * a scalar and the other vector, then the scalar is applied component-
345 * wise to the vector, resulting in the same type as the vector. If both
346 * are vectors of the same size, the result is computed component-wise."
347 */
Ian Romanicka2dd22f2010-03-09 15:55:16 -0800348 if (type_a->is_vector()) {
349 if (!type_b->is_vector()
Ian Romanicka87ac252010-02-22 13:19:34 -0800350 || (type_a->vector_elements == type_b->vector_elements))
351 return type_a;
352 } else
353 return type_b;
354
355 /* "The operator modulus (%) is not defined for any other data types
356 * (non-integer types)."
357 */
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000358 _mesa_glsl_error(loc, state, "type mismatch");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700359 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800360}
361
362
363static const struct glsl_type *
Ian Romanickbfb09c22010-03-29 16:32:55 -0700364relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000365 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800366{
Ian Romanickbfb09c22010-03-29 16:32:55 -0700367 const glsl_type *const type_a = value_a->type;
368 const glsl_type *const type_b = value_b->type;
Ian Romanick0150f5f2010-03-29 16:20:07 -0700369
Ian Romanicka87ac252010-02-22 13:19:34 -0800370 /* From GLSL 1.50 spec, page 56:
371 * "The relational operators greater than (>), less than (<), greater
372 * than or equal (>=), and less than or equal (<=) operate only on
373 * scalar integer and scalar floating-point expressions."
374 */
Ian Romanicka6d653d2010-03-26 14:40:37 -0700375 if (!type_a->is_numeric()
376 || !type_b->is_numeric()
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800377 || !type_a->is_scalar()
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000378 || !type_b->is_scalar()) {
379 _mesa_glsl_error(loc, state,
380 "Operands to relational operators must be scalar and "
381 "numeric");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700382 return glsl_type::error_type;
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000383 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800384
385 /* "Either the operands' types must match, or the conversions from
386 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
387 * operand, after which the types must match."
Ian Romanicka87ac252010-02-22 13:19:34 -0800388 */
Ian Romanick0150f5f2010-03-29 16:20:07 -0700389 if (!apply_implicit_conversion(type_a, value_b, state)
390 && !apply_implicit_conversion(type_b, value_a, state)) {
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000391 _mesa_glsl_error(loc, state,
392 "Could not implicitly convert operands to "
393 "relational operator");
Ian Romanick0150f5f2010-03-29 16:20:07 -0700394 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800395 }
396
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000397 if (type_a->base_type != type_b->base_type) {
398 _mesa_glsl_error(loc, state, "base type mismatch");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700399 return glsl_type::error_type;
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000400 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800401
402 /* "The result is scalar Boolean."
403 */
Ian Romanick0471e8b2010-03-26 14:33:41 -0700404 return glsl_type::bool_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800405}
406
407
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700408/**
409 * Validates that a value can be assigned to a location with a specified type
410 *
411 * Validates that \c rhs can be assigned to some location. If the types are
412 * not an exact match but an automatic conversion is possible, \c rhs will be
413 * converted.
414 *
415 * \return
416 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
417 * Otherwise the actual RHS to be assigned will be returned. This may be
418 * \c rhs, or it may be \c rhs after some type conversion.
419 *
420 * \note
421 * In addition to being used for assignments, this function is used to
422 * type-check return values.
423 */
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700424ir_rvalue *
425validate_assignment(const glsl_type *lhs_type, ir_rvalue *rhs)
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700426{
427 const glsl_type *const rhs_type = rhs->type;
428
429 /* If there is already some error in the RHS, just return it. Anything
430 * else will lead to an avalanche of error message back to the user.
431 */
432 if (rhs_type->is_error())
433 return rhs;
434
435 /* FINISHME: For GLSL 1.10, check that the types are not arrays. */
436
437 /* If the types are identical, the assignment can trivially proceed.
438 */
439 if (rhs_type == lhs_type)
440 return rhs;
441
442 /* FINISHME: Check for and apply automatic conversions. */
443 return NULL;
444}
445
Eric Anholt10a68522010-03-26 11:53:37 -0700446ir_rvalue *
447do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
448 ir_rvalue *lhs, ir_rvalue *rhs,
449 YYLTYPE lhs_loc)
450{
451 bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
452
453 if (!error_emitted) {
454 /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
455 if (!lhs->is_lvalue()) {
456 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
457 error_emitted = true;
458 }
459 }
460
461 ir_rvalue *new_rhs = validate_assignment(lhs->type, rhs);
462 if (new_rhs == NULL) {
463 _mesa_glsl_error(& lhs_loc, state, "type mismatch");
464 } else {
465 rhs = new_rhs;
466 }
467
468 ir_instruction *tmp = new ir_assignment(lhs, rhs, NULL);
469 instructions->push_tail(tmp);
470
471 return rhs;
472}
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700473
Ian Romanick5185a5f2010-03-29 15:20:42 -0700474
475/**
476 * Generate a new temporary and add its declaration to the instruction stream
477 */
478static ir_variable *
479generate_temporary(const glsl_type *type, exec_list *instructions,
480 struct _mesa_glsl_parse_state *state)
481{
482 char *name = (char *) malloc(sizeof(char) * 13);
483
484 snprintf(name, 13, "tmp_%08X", state->temp_index);
485 state->temp_index++;
486
487 ir_variable *const var = new ir_variable(type, name);
488 instructions->push_tail(var);
489
490 return var;
491}
492
493
Eric Anholtde38f0e2010-03-26 12:14:54 -0700494static ir_rvalue *
495get_lvalue_copy(exec_list *instructions, struct _mesa_glsl_parse_state *state,
496 ir_rvalue *lvalue, YYLTYPE loc)
497{
498 ir_variable *var;
499 ir_rvalue *var_deref;
500
501 /* FINISHME: Give unique names to the temporaries. */
502 var = new ir_variable(lvalue->type, "_internal_tmp");
503 var->mode = ir_var_auto;
504
505 var_deref = new ir_dereference(var);
506 do_assignment(instructions, state, var_deref, lvalue, loc);
507
508 /* Once we've created this temporary, mark it read only so it's no
509 * longer considered an lvalue.
510 */
511 var->read_only = true;
512
513 return var_deref;
514}
515
516
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700517ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -0800518ast_node::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -0800519 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -0800520{
Ian Romanick18238de2010-03-01 13:49:10 -0800521 (void) instructions;
522 (void) state;
523
524 return NULL;
525}
526
527
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700528ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -0800529ast_expression::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -0800530 struct _mesa_glsl_parse_state *state)
531{
Ian Romanicka87ac252010-02-22 13:19:34 -0800532 static const int operations[AST_NUM_OPERATORS] = {
533 -1, /* ast_assign doesn't convert to ir_expression. */
534 -1, /* ast_plus doesn't convert to ir_expression. */
535 ir_unop_neg,
536 ir_binop_add,
537 ir_binop_sub,
538 ir_binop_mul,
539 ir_binop_div,
540 ir_binop_mod,
541 ir_binop_lshift,
542 ir_binop_rshift,
543 ir_binop_less,
544 ir_binop_greater,
545 ir_binop_lequal,
546 ir_binop_gequal,
547 ir_binop_equal,
548 ir_binop_nequal,
549 ir_binop_bit_and,
550 ir_binop_bit_xor,
551 ir_binop_bit_or,
552 ir_unop_bit_not,
553 ir_binop_logic_and,
554 ir_binop_logic_xor,
555 ir_binop_logic_or,
556 ir_unop_logic_not,
557
558 /* Note: The following block of expression types actually convert
559 * to multiple IR instructions.
560 */
561 ir_binop_mul, /* ast_mul_assign */
562 ir_binop_div, /* ast_div_assign */
563 ir_binop_mod, /* ast_mod_assign */
564 ir_binop_add, /* ast_add_assign */
565 ir_binop_sub, /* ast_sub_assign */
566 ir_binop_lshift, /* ast_ls_assign */
567 ir_binop_rshift, /* ast_rs_assign */
568 ir_binop_bit_and, /* ast_and_assign */
569 ir_binop_bit_xor, /* ast_xor_assign */
570 ir_binop_bit_or, /* ast_or_assign */
571
572 -1, /* ast_conditional doesn't convert to ir_expression. */
Eric Anholtde38f0e2010-03-26 12:14:54 -0700573 ir_binop_add, /* ast_pre_inc. */
574 ir_binop_sub, /* ast_pre_dec. */
575 ir_binop_add, /* ast_post_inc. */
576 ir_binop_sub, /* ast_post_dec. */
Ian Romanicka87ac252010-02-22 13:19:34 -0800577 -1, /* ast_field_selection doesn't conv to ir_expression. */
578 -1, /* ast_array_index doesn't convert to ir_expression. */
579 -1, /* ast_function_call doesn't conv to ir_expression. */
580 -1, /* ast_identifier doesn't convert to ir_expression. */
581 -1, /* ast_int_constant doesn't convert to ir_expression. */
582 -1, /* ast_uint_constant doesn't conv to ir_expression. */
583 -1, /* ast_float_constant doesn't conv to ir_expression. */
584 -1, /* ast_bool_constant doesn't conv to ir_expression. */
585 -1, /* ast_sequence doesn't convert to ir_expression. */
586 };
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700587 ir_rvalue *result = NULL;
588 ir_rvalue *op[2];
Ian Romanicka87ac252010-02-22 13:19:34 -0800589 struct simple_node op_list;
Ian Romanick0471e8b2010-03-26 14:33:41 -0700590 const struct glsl_type *type = glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800591 bool error_emitted = false;
592 YYLTYPE loc;
593
Ian Romanick18238de2010-03-01 13:49:10 -0800594 loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -0800595 make_empty_list(& op_list);
596
Ian Romanick18238de2010-03-01 13:49:10 -0800597 switch (this->oper) {
Ian Romanick6652af32010-03-09 16:38:02 -0800598 case ast_assign: {
Ian Romanick18238de2010-03-01 13:49:10 -0800599 op[0] = this->subexpressions[0]->hir(instructions, state);
600 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800601
Eric Anholt10a68522010-03-26 11:53:37 -0700602 result = do_assignment(instructions, state, op[0], op[1],
603 this->subexpressions[0]->get_location());
604 error_emitted = result->type->is_error();
605 type = result->type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800606 break;
Ian Romanick6652af32010-03-09 16:38:02 -0800607 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800608
609 case ast_plus:
Ian Romanick18238de2010-03-01 13:49:10 -0800610 op[0] = this->subexpressions[0]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800611
Ian Romanicka43817a2010-03-26 14:27:23 -0700612 error_emitted = op[0]->type->is_error();
613 if (type->is_error())
Ian Romanicka87ac252010-02-22 13:19:34 -0800614 op[0]->type = type;
615
616 result = op[0];
617 break;
618
619 case ast_neg:
Ian Romanick18238de2010-03-01 13:49:10 -0800620 op[0] = this->subexpressions[0]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800621
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000622 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -0800623
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000624 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -0800625
Ian Romanick18238de2010-03-01 13:49:10 -0800626 result = new ir_expression(operations[this->oper], type,
Ian Romanicka87ac252010-02-22 13:19:34 -0800627 op[0], NULL);
628 break;
629
630 case ast_add:
631 case ast_sub:
632 case ast_mul:
633 case ast_div:
Ian Romanick18238de2010-03-01 13:49:10 -0800634 op[0] = this->subexpressions[0]->hir(instructions, state);
635 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800636
Ian Romanickbfb09c22010-03-29 16:32:55 -0700637 type = arithmetic_result_type(op[0], op[1],
Ian Romanick18238de2010-03-01 13:49:10 -0800638 (this->oper == ast_mul),
Eric Anholta13bb142010-03-31 16:38:11 -1000639 state, & loc);
640 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -0800641
Ian Romanick18238de2010-03-01 13:49:10 -0800642 result = new ir_expression(operations[this->oper], type,
Ian Romanicka87ac252010-02-22 13:19:34 -0800643 op[0], op[1]);
644 break;
645
646 case ast_mod:
Ian Romanick18238de2010-03-01 13:49:10 -0800647 op[0] = this->subexpressions[0]->hir(instructions, state);
648 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800649
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000650 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -0800651
Ian Romanick18238de2010-03-01 13:49:10 -0800652 assert(operations[this->oper] == ir_binop_mod);
Ian Romanicka87ac252010-02-22 13:19:34 -0800653
Ian Romanick18238de2010-03-01 13:49:10 -0800654 result = new ir_expression(operations[this->oper], type,
Ian Romanicka87ac252010-02-22 13:19:34 -0800655 op[0], op[1]);
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000656 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -0800657 break;
658
659 case ast_lshift:
660 case ast_rshift:
Eric Anholt183d8c62010-03-31 16:53:47 -1000661 _mesa_glsl_error(& loc, state, "FINISHME: implement bit-shift operators");
662 error_emitted = true;
Ian Romanicka87ac252010-02-22 13:19:34 -0800663 break;
664
665 case ast_less:
666 case ast_greater:
667 case ast_lequal:
668 case ast_gequal:
Ian Romanick18238de2010-03-01 13:49:10 -0800669 op[0] = this->subexpressions[0]->hir(instructions, state);
670 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800671
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000672 type = relational_result_type(op[0], op[1], state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -0800673
674 /* The relational operators must either generate an error or result
675 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
676 */
Ian Romanicka43817a2010-03-26 14:27:23 -0700677 assert(type->is_error()
Ian Romanicka87ac252010-02-22 13:19:34 -0800678 || ((type->base_type == GLSL_TYPE_BOOL)
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800679 && type->is_scalar()));
Ian Romanicka87ac252010-02-22 13:19:34 -0800680
Ian Romanick18238de2010-03-01 13:49:10 -0800681 result = new ir_expression(operations[this->oper], type,
Ian Romanicka87ac252010-02-22 13:19:34 -0800682 op[0], op[1]);
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000683 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -0800684 break;
685
686 case ast_nequal:
687 case ast_equal:
Ian Romanick6e659ca2010-03-29 15:11:05 -0700688 op[0] = this->subexpressions[0]->hir(instructions, state);
689 op[1] = this->subexpressions[1]->hir(instructions, state);
690
691 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
692 *
693 * "The equality operators equal (==), and not equal (!=)
694 * operate on all types. They result in a scalar Boolean. If
695 * the operand types do not match, then there must be a
696 * conversion from Section 4.1.10 "Implicit Conversions"
697 * applied to one operand that can make them match, in which
698 * case this conversion is done."
699 */
Ian Romanickbfb09c22010-03-29 16:32:55 -0700700 if ((!apply_implicit_conversion(op[0]->type, op[1], state)
701 && !apply_implicit_conversion(op[1]->type, op[0], state))
Ian Romanick212b0322010-03-29 16:22:38 -0700702 || (op[0]->type != op[1]->type)) {
Ian Romanick6e659ca2010-03-29 15:11:05 -0700703 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
704 "type", (this->oper == ast_equal) ? "==" : "!=");
705 error_emitted = true;
Ian Romanicka80cbd62010-03-30 17:04:48 -0700706 } else if ((state->language_version <= 110)
707 && (op[0]->type->is_array() || op[1]->type->is_array())) {
708 _mesa_glsl_error(& loc, state, "array comparisons forbidden in "
709 "GLSL 1.10");
710 error_emitted = true;
Ian Romanick6e659ca2010-03-29 15:11:05 -0700711 }
712
713 result = new ir_expression(operations[this->oper], glsl_type::bool_type,
714 op[0], op[1]);
715 type = glsl_type::bool_type;
716
717 assert(result->type == glsl_type::bool_type);
Ian Romanicka87ac252010-02-22 13:19:34 -0800718 break;
719
720 case ast_bit_and:
721 case ast_bit_xor:
722 case ast_bit_or:
723 case ast_bit_not:
Eric Anholt183d8c62010-03-31 16:53:47 -1000724 _mesa_glsl_error(& loc, state, "FINISHME: implement bit-wise operators");
725 error_emitted = true;
Ian Romanicka87ac252010-02-22 13:19:34 -0800726 break;
727
728 case ast_logic_and:
729 case ast_logic_xor:
730 case ast_logic_or:
Eric Anholtb82c0c32010-03-31 09:11:39 -1000731 op[0] = this->subexpressions[0]->hir(instructions, state);
732 op[1] = this->subexpressions[1]->hir(instructions, state);
733
734 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
735 YYLTYPE loc = this->subexpressions[0]->get_location();
736
737 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
738 operator_string(this->oper));
739 }
740
741 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
742 YYLTYPE loc = this->subexpressions[1]->get_location();
743
744 _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean",
745 operator_string(this->oper));
746 }
747
748 result = new ir_expression(operations[this->oper], glsl_type::bool_type,
749 op[0], op[1]);
Ian Romanicka87ac252010-02-22 13:19:34 -0800750 break;
751
Eric Anholta5827fe2010-03-31 16:50:55 -1000752 case ast_logic_not:
753 op[0] = this->subexpressions[0]->hir(instructions, state);
754
755 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
756 YYLTYPE loc = this->subexpressions[0]->get_location();
757
758 _mesa_glsl_error(& loc, state,
759 "operand of `!' must be scalar boolean");
760 }
761
762 result = new ir_expression(operations[this->oper], glsl_type::bool_type,
763 op[0], NULL);
764 break;
765
Ian Romanicka87ac252010-02-22 13:19:34 -0800766 case ast_mul_assign:
767 case ast_div_assign:
768 case ast_add_assign:
769 case ast_sub_assign: {
Ian Romanick18238de2010-03-01 13:49:10 -0800770 op[0] = this->subexpressions[0]->hir(instructions, state);
771 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800772
Ian Romanickbfb09c22010-03-29 16:32:55 -0700773 type = arithmetic_result_type(op[0], op[1],
Ian Romanick18238de2010-03-01 13:49:10 -0800774 (this->oper == ast_mul_assign),
Eric Anholta13bb142010-03-31 16:38:11 -1000775 state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -0800776
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700777 ir_rvalue *temp_rhs = new ir_expression(operations[this->oper], type,
778 op[0], op[1]);
Ian Romanicka87ac252010-02-22 13:19:34 -0800779
Eric Anholt10a68522010-03-26 11:53:37 -0700780 result = do_assignment(instructions, state, op[0], temp_rhs,
781 this->subexpressions[0]->get_location());
782 type = result->type;
783 error_emitted = (op[0]->type->is_error());
Ian Romanicka87ac252010-02-22 13:19:34 -0800784
785 /* GLSL 1.10 does not allow array assignment. However, we don't have to
786 * explicitly test for this because none of the binary expression
787 * operators allow array operands either.
788 */
789
Ian Romanicka87ac252010-02-22 13:19:34 -0800790 break;
791 }
792
Eric Anholt48a0e642010-03-26 11:57:46 -0700793 case ast_mod_assign: {
794 op[0] = this->subexpressions[0]->hir(instructions, state);
795 op[1] = this->subexpressions[1]->hir(instructions, state);
796
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000797 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
Eric Anholt48a0e642010-03-26 11:57:46 -0700798
799 assert(operations[this->oper] == ir_binop_mod);
800
801 struct ir_rvalue *temp_rhs;
802 temp_rhs = new ir_expression(operations[this->oper], type,
803 op[0], op[1]);
804
805 result = do_assignment(instructions, state, op[0], temp_rhs,
806 this->subexpressions[0]->get_location());
807 type = result->type;
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000808 error_emitted = type->is_error();
Eric Anholt48a0e642010-03-26 11:57:46 -0700809 break;
810 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800811
812 case ast_ls_assign:
813 case ast_rs_assign:
Eric Anholt183d8c62010-03-31 16:53:47 -1000814 _mesa_glsl_error(& loc, state,
815 "FINISHME: implement bit-shift assignment operators");
816 error_emitted = true;
Ian Romanick251eb752010-03-29 14:15:05 -0700817 break;
Ian Romanicka87ac252010-02-22 13:19:34 -0800818
819 case ast_and_assign:
820 case ast_xor_assign:
821 case ast_or_assign:
Eric Anholt183d8c62010-03-31 16:53:47 -1000822 _mesa_glsl_error(& loc, state,
823 "FINISHME: implement logic assignment operators");
824 error_emitted = true;
Ian Romanick251eb752010-03-29 14:15:05 -0700825 break;
Ian Romanicka87ac252010-02-22 13:19:34 -0800826
Ian Romanick96f9cea2010-03-29 15:33:54 -0700827 case ast_conditional: {
828 op[0] = this->subexpressions[0]->hir(instructions, state);
829
830 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
831 *
832 * "The ternary selection operator (?:). It operates on three
833 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
834 * first expression, which must result in a scalar Boolean."
835 */
836 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
837 YYLTYPE loc = this->subexpressions[0]->get_location();
838
839 _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean");
840 error_emitted = true;
841 }
842
843 /* The :? operator is implemented by generating an anonymous temporary
844 * followed by an if-statement. The last instruction in each branch of
845 * the if-statement assigns a value to the anonymous temporary. This
846 * temporary is the r-value of the expression.
847 */
848 ir_variable *const tmp = generate_temporary(glsl_type::error_type,
849 instructions, state);
850
851 ir_if *const stmt = new ir_if(op[0]);
852 instructions->push_tail(stmt);
853
854 op[1] = this->subexpressions[1]->hir(& stmt->then_instructions, state);
855 ir_dereference *const then_deref = new ir_dereference(tmp);
856 ir_assignment *const then_assign =
857 new ir_assignment(then_deref, op[1], NULL);
858 stmt->then_instructions.push_tail(then_assign);
859
860 op[2] = this->subexpressions[2]->hir(& stmt->else_instructions, state);
861 ir_dereference *const else_deref = new ir_dereference(tmp);
862 ir_assignment *const else_assign =
863 new ir_assignment(else_deref, op[2], NULL);
864 stmt->else_instructions.push_tail(else_assign);
865
866 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
867 *
868 * "The second and third expressions can be any type, as
869 * long their types match, or there is a conversion in
870 * Section 4.1.10 "Implicit Conversions" that can be applied
871 * to one of the expressions to make their types match. This
872 * resulting matching type is the type of the entire
873 * expression."
874 */
Ian Romanickbfb09c22010-03-29 16:32:55 -0700875 if ((!apply_implicit_conversion(op[1]->type, op[2], state)
876 && !apply_implicit_conversion(op[2]->type, op[1], state))
Ian Romanickdb9be2e2010-03-29 16:25:56 -0700877 || (op[1]->type != op[2]->type)) {
Ian Romanick96f9cea2010-03-29 15:33:54 -0700878 YYLTYPE loc = this->subexpressions[1]->get_location();
879
880 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
881 "operator must have matching types.");
882 error_emitted = true;
Ian Romanickdb9be2e2010-03-29 16:25:56 -0700883 } else {
884 tmp->type = op[1]->type;
Ian Romanick96f9cea2010-03-29 15:33:54 -0700885 }
886
Ian Romanick96f9cea2010-03-29 15:33:54 -0700887 result = new ir_dereference(tmp);
888 type = tmp->type;
Ian Romanick251eb752010-03-29 14:15:05 -0700889 break;
Ian Romanick96f9cea2010-03-29 15:33:54 -0700890 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800891
892 case ast_pre_inc:
Eric Anholt76ea56c2010-03-26 12:16:54 -0700893 case ast_pre_dec: {
894 op[0] = this->subexpressions[0]->hir(instructions, state);
895 if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
896 op[1] = new ir_constant(1.0f);
897 else
898 op[1] = new ir_constant(1);
899
Eric Anholta13bb142010-03-31 16:38:11 -1000900 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
Eric Anholt76ea56c2010-03-26 12:16:54 -0700901
902 struct ir_rvalue *temp_rhs;
903 temp_rhs = new ir_expression(operations[this->oper], type,
904 op[0], op[1]);
905
906 result = do_assignment(instructions, state, op[0], temp_rhs,
907 this->subexpressions[0]->get_location());
908 type = result->type;
909 error_emitted = op[0]->type->is_error();
910 break;
911 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800912
913 case ast_post_inc:
Eric Anholtde38f0e2010-03-26 12:14:54 -0700914 case ast_post_dec: {
915 op[0] = this->subexpressions[0]->hir(instructions, state);
916 if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
917 op[1] = new ir_constant(1.0f);
918 else
919 op[1] = new ir_constant(1);
920
921 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
922
Eric Anholta13bb142010-03-31 16:38:11 -1000923 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
Eric Anholtde38f0e2010-03-26 12:14:54 -0700924
925 struct ir_rvalue *temp_rhs;
926 temp_rhs = new ir_expression(operations[this->oper], type,
927 op[0], op[1]);
928
929 /* Get a temporary of a copy of the lvalue before it's modified.
930 * This may get thrown away later.
931 */
932 result = get_lvalue_copy(instructions, state, op[0],
933 this->subexpressions[0]->get_location());
934
935 (void)do_assignment(instructions, state, op[0], temp_rhs,
936 this->subexpressions[0]->get_location());
937
938 type = result->type;
939 error_emitted = op[0]->type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -0800940 break;
Eric Anholtde38f0e2010-03-26 12:14:54 -0700941 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800942
943 case ast_field_selection:
Ian Romanick18238de2010-03-01 13:49:10 -0800944 result = _mesa_ast_field_selection_to_hir(this, instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800945 type = result->type;
946 break;
947
Ian Romanick27e3cf82010-04-01 18:03:59 -0700948 case ast_array_index: {
949 YYLTYPE index_loc = subexpressions[1]->get_location();
950
951 op[0] = subexpressions[0]->hir(instructions, state);
952 op[1] = subexpressions[1]->hir(instructions, state);
953
954 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
955
Ian Romanickb8a21cc2010-04-01 18:31:11 -0700956 ir_dereference *const lhs = op[0]->as_dereference();
957 ir_instruction *array;
958 if ((lhs != NULL)
959 && (lhs->mode == ir_dereference::ir_reference_variable)) {
960 result = new ir_dereference(lhs->var, op[1]);
961
962 delete op[0];
963 array = lhs->var;
964 } else {
965 result = new ir_dereference(op[0], op[1]);
966 array = op[0];
967 }
968
969 /* Do not use op[0] after this point. Use array.
970 */
971 op[0] = NULL;
972
Ian Romanick27e3cf82010-04-01 18:03:59 -0700973
974 if (error_emitted)
975 break;
976
977 /* FINISHME: Handle vectors and matrices accessed with []. */
Ian Romanickb8a21cc2010-04-01 18:31:11 -0700978 if (!array->type->is_array()) {
Ian Romanick27e3cf82010-04-01 18:03:59 -0700979 _mesa_glsl_error(& index_loc, state,
980 "cannot dereference non-array");
981 error_emitted = true;
982 }
983
984 if (!op[1]->type->is_integer()) {
985 _mesa_glsl_error(& index_loc, state,
986 "array index must be integer type");
987 error_emitted = true;
988 } else if (!op[1]->type->is_scalar()) {
989 _mesa_glsl_error(& index_loc, state,
990 "array index must be scalar");
991 error_emitted = true;
992 }
993
994 /* If the array index is a constant expression and the array has a
995 * declared size, ensure that the access is in-bounds. If the array
996 * index is not a constant expression, ensure that the array has a
997 * declared size.
998 */
999 ir_constant *const const_index = op[1]->constant_expression_value();
1000 if (const_index != NULL) {
1001 const int idx = const_index->value.i[0];
1002
1003 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
1004 *
1005 * "It is illegal to declare an array with a size, and then
1006 * later (in the same shader) index the same array with an
1007 * integral constant expression greater than or equal to the
1008 * declared size. It is also illegal to index an array with a
1009 * negative constant expression."
1010 */
Ian Romanickb8a21cc2010-04-01 18:31:11 -07001011 if ((array->type->array_size() > 0)
1012 && (array->type->array_size() <= idx)) {
Ian Romanick27e3cf82010-04-01 18:03:59 -07001013 _mesa_glsl_error(& loc, state,
1014 "array index must be < %u",
Ian Romanickb8a21cc2010-04-01 18:31:11 -07001015 array->type->array_size());
Ian Romanick27e3cf82010-04-01 18:03:59 -07001016 error_emitted = true;
1017 }
1018
1019 if (idx < 0) {
1020 _mesa_glsl_error(& loc, state,
1021 "array index must be >= 0");
1022 error_emitted = true;
1023 }
Ian Romanickb8a21cc2010-04-01 18:31:11 -07001024
1025 ir_variable *const v = array->as_variable();
1026 if ((v != NULL) && (unsigned(idx) > v->max_array_access))
1027 v->max_array_access = idx;
Ian Romanick27e3cf82010-04-01 18:03:59 -07001028 }
1029
1030 if (error_emitted)
1031 result->type = glsl_type::error_type;
1032
1033 type = result->type;
Ian Romanicka87ac252010-02-22 13:19:34 -08001034 break;
Ian Romanick27e3cf82010-04-01 18:03:59 -07001035 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001036
1037 case ast_function_call:
Ian Romanick7cfddf12010-03-10 13:26:52 -08001038 /* Should *NEVER* get here. ast_function_call should always be handled
1039 * by ast_function_expression::hir.
Ian Romanicka87ac252010-02-22 13:19:34 -08001040 */
Ian Romanick7cfddf12010-03-10 13:26:52 -08001041 assert(0);
Ian Romanicka87ac252010-02-22 13:19:34 -08001042 break;
1043
1044 case ast_identifier: {
1045 /* ast_identifier can appear several places in a full abstract syntax
1046 * tree. This particular use must be at location specified in the grammar
1047 * as 'variable_identifier'.
1048 */
Ian Romanick8bde4ce2010-03-19 11:57:24 -07001049 ir_variable *var =
1050 state->symbols->get_variable(this->primary_expression.identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08001051
1052 result = new ir_dereference(var);
1053
1054 if (var != NULL) {
1055 type = result->type;
1056 } else {
Ian Romanick71d0bbf2010-03-23 13:21:19 -07001057 _mesa_glsl_error(& loc, state, "`%s' undeclared",
Ian Romanick18238de2010-03-01 13:49:10 -08001058 this->primary_expression.identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08001059
1060 error_emitted = true;
1061 }
1062 break;
1063 }
1064
1065 case ast_int_constant:
Ian Romanick0471e8b2010-03-26 14:33:41 -07001066 type = glsl_type::int_type;
Ian Romanick18238de2010-03-01 13:49:10 -08001067 result = new ir_constant(type, & this->primary_expression);
Ian Romanicka87ac252010-02-22 13:19:34 -08001068 break;
1069
1070 case ast_uint_constant:
Ian Romanick0471e8b2010-03-26 14:33:41 -07001071 type = glsl_type::uint_type;
Ian Romanick18238de2010-03-01 13:49:10 -08001072 result = new ir_constant(type, & this->primary_expression);
Ian Romanicka87ac252010-02-22 13:19:34 -08001073 break;
1074
1075 case ast_float_constant:
Ian Romanick0471e8b2010-03-26 14:33:41 -07001076 type = glsl_type::float_type;
Ian Romanick18238de2010-03-01 13:49:10 -08001077 result = new ir_constant(type, & this->primary_expression);
Ian Romanicka87ac252010-02-22 13:19:34 -08001078 break;
1079
1080 case ast_bool_constant:
Ian Romanick0471e8b2010-03-26 14:33:41 -07001081 type = glsl_type::bool_type;
Ian Romanick18238de2010-03-01 13:49:10 -08001082 result = new ir_constant(type, & this->primary_expression);
Ian Romanicka87ac252010-02-22 13:19:34 -08001083 break;
1084
1085 case ast_sequence: {
1086 struct simple_node *ptr;
1087
1088 /* It should not be possible to generate a sequence in the AST without
1089 * any expressions in it.
1090 */
Ian Romanick18238de2010-03-01 13:49:10 -08001091 assert(!is_empty_list(&this->expressions));
Ian Romanicka87ac252010-02-22 13:19:34 -08001092
1093 /* The r-value of a sequence is the last expression in the sequence. If
1094 * the other expressions in the sequence do not have side-effects (and
1095 * therefore add instructions to the instruction list), they get dropped
1096 * on the floor.
1097 */
Ian Romanick18238de2010-03-01 13:49:10 -08001098 foreach (ptr, &this->expressions)
1099 result = ((ast_node *)ptr)->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001100
1101 type = result->type;
1102
1103 /* Any errors should have already been emitted in the loop above.
1104 */
1105 error_emitted = true;
1106 break;
1107 }
1108 }
1109
Ian Romanickcef3bae2010-03-26 14:41:32 -07001110 if (type->is_error() && !error_emitted)
Ian Romanick71d0bbf2010-03-23 13:21:19 -07001111 _mesa_glsl_error(& loc, state, "type mismatch");
Ian Romanicka87ac252010-02-22 13:19:34 -08001112
1113 return result;
1114}
1115
1116
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001117ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08001118ast_expression_statement::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08001119 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08001120{
Ian Romanicka87ac252010-02-22 13:19:34 -08001121 /* It is possible to have expression statements that don't have an
1122 * expression. This is the solitary semicolon:
1123 *
1124 * for (i = 0; i < 5; i++)
1125 * ;
1126 *
1127 * In this case the expression will be NULL. Test for NULL and don't do
1128 * anything in that case.
1129 */
Ian Romanick18238de2010-03-01 13:49:10 -08001130 if (expression != NULL)
1131 expression->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001132
1133 /* Statements do not have r-values.
1134 */
1135 return NULL;
1136}
1137
1138
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001139ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08001140ast_compound_statement::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08001141 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08001142{
Ian Romanicka87ac252010-02-22 13:19:34 -08001143 struct simple_node *ptr;
1144
1145
Ian Romanick18238de2010-03-01 13:49:10 -08001146 if (new_scope)
Ian Romanick8bde4ce2010-03-19 11:57:24 -07001147 state->symbols->push_scope();
Ian Romanicka87ac252010-02-22 13:19:34 -08001148
Ian Romanick18238de2010-03-01 13:49:10 -08001149 foreach (ptr, &statements)
1150 ((ast_node *)ptr)->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001151
Ian Romanick18238de2010-03-01 13:49:10 -08001152 if (new_scope)
Ian Romanick8bde4ce2010-03-19 11:57:24 -07001153 state->symbols->pop_scope();
Ian Romanicka87ac252010-02-22 13:19:34 -08001154
1155 /* Compound statements do not have r-values.
1156 */
1157 return NULL;
1158}
1159
1160
Ian Romanick28009cd2010-03-30 16:59:27 -07001161static const glsl_type *
1162process_array_type(const glsl_type *base, ast_node *array_size,
1163 struct _mesa_glsl_parse_state *state)
1164{
1165 unsigned length = 0;
1166
1167 /* FINISHME: Reject delcarations of multidimensional arrays. */
1168
1169 if (array_size != NULL) {
1170 exec_list dummy_instructions;
1171 ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
1172 YYLTYPE loc = array_size->get_location();
1173
1174 /* FINISHME: Verify that the grammar forbids side-effects in array
1175 * FINISHME: sizes. i.e., 'vec4 [x = 12] data'
1176 */
1177 assert(dummy_instructions.is_empty());
1178
1179 if (ir != NULL) {
1180 if (!ir->type->is_integer()) {
1181 _mesa_glsl_error(& loc, state, "array size must be integer type");
1182 } else if (!ir->type->is_scalar()) {
1183 _mesa_glsl_error(& loc, state, "array size must be scalar type");
1184 } else {
1185 ir_constant *const size = ir->constant_expression_value();
1186
1187 if (size == NULL) {
1188 _mesa_glsl_error(& loc, state, "array size must be a "
1189 "constant valued expression");
1190 } else if (size->value.i[0] <= 0) {
1191 _mesa_glsl_error(& loc, state, "array size must be > 0");
1192 } else {
1193 assert(size->type == ir->type);
1194 length = size->value.u[0];
1195 }
1196 }
1197 }
1198 }
1199
1200 return glsl_type::get_array_instance(base, length);
1201}
1202
1203
Ian Romanickd612a122010-03-31 16:22:06 -07001204const glsl_type *
1205ast_type_specifier::glsl_type(const char **name,
1206 struct _mesa_glsl_parse_state *state) const
Ian Romanicka87ac252010-02-22 13:19:34 -08001207{
Ian Romanickd612a122010-03-31 16:22:06 -07001208 const struct glsl_type *type;
Ian Romanicka87ac252010-02-22 13:19:34 -08001209
Ian Romanickd612a122010-03-31 16:22:06 -07001210 if (this->type_specifier == ast_struct) {
Ian Romanicka87ac252010-02-22 13:19:34 -08001211 /* FINISHME: Handle annonymous structures. */
1212 type = NULL;
1213 } else {
Ian Romanickd612a122010-03-31 16:22:06 -07001214 type = state->symbols->get_type(this->type_name);
1215 *name = this->type_name;
Ian Romanicka87ac252010-02-22 13:19:34 -08001216
Ian Romanickd612a122010-03-31 16:22:06 -07001217 if (this->is_array) {
1218 type = process_array_type(type, this->array_size, state);
Ian Romanick28009cd2010-03-30 16:59:27 -07001219 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001220 }
1221
1222 return type;
1223}
1224
1225
1226static void
1227apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
1228 struct ir_variable *var,
Eric Anholt2e063f12010-03-28 00:56:22 -07001229 struct _mesa_glsl_parse_state *state,
1230 YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -08001231{
1232 if (qual->invariant)
1233 var->invariant = 1;
1234
1235 /* FINISHME: Mark 'in' variables at global scope as read-only. */
1236 if (qual->constant || qual->attribute || qual->uniform
1237 || (qual->varying && (state->target == fragment_shader)))
1238 var->read_only = 1;
1239
1240 if (qual->centroid)
1241 var->centroid = 1;
1242
Eric Anholt2e063f12010-03-28 00:56:22 -07001243 if (qual->attribute && state->target == fragment_shader) {
1244 var->type = glsl_type::error_type;
1245 _mesa_glsl_error(loc, state,
1246 "`attribute' variables may not be declared in the "
1247 "fragment shader");
1248 }
1249
Ian Romanicka87ac252010-02-22 13:19:34 -08001250 if (qual->in && qual->out)
1251 var->mode = ir_var_inout;
1252 else if (qual->attribute || qual->in
1253 || (qual->varying && (state->target == fragment_shader)))
1254 var->mode = ir_var_in;
Ian Romanick0b678232010-03-10 00:28:59 -08001255 else if (qual->out || (qual->varying && (state->target == vertex_shader)))
Ian Romanicka87ac252010-02-22 13:19:34 -08001256 var->mode = ir_var_out;
1257 else if (qual->uniform)
1258 var->mode = ir_var_uniform;
1259 else
1260 var->mode = ir_var_auto;
1261
1262 if (qual->flat)
1263 var->interpolation = ir_var_flat;
1264 else if (qual->noperspective)
1265 var->interpolation = ir_var_noperspective;
1266 else
1267 var->interpolation = ir_var_smooth;
1268}
1269
1270
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001271ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08001272ast_declarator_list::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08001273 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08001274{
Ian Romanicka87ac252010-02-22 13:19:34 -08001275 struct simple_node *ptr;
1276 const struct glsl_type *decl_type;
1277 const char *type_name = NULL;
1278
1279
1280 /* FINISHME: Handle vertex shader "invariant" declarations that do not
1281 * FINISHME: include a type. These re-declare built-in variables to be
1282 * FINISHME: invariant.
1283 */
1284
Ian Romanickd612a122010-03-31 16:22:06 -07001285 decl_type = this->type->specifier->glsl_type(& type_name, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001286
Ian Romanick18238de2010-03-01 13:49:10 -08001287 foreach (ptr, &this->declarations) {
Ian Romanicka87ac252010-02-22 13:19:34 -08001288 struct ast_declaration *const decl = (struct ast_declaration * )ptr;
1289 const struct glsl_type *var_type;
1290 struct ir_variable *var;
Eric Anholt2e063f12010-03-28 00:56:22 -07001291 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08001292
1293 /* FINISHME: Emit a warning if a variable declaration shadows a
1294 * FINISHME: declaration at a higher scope.
1295 */
1296
Ian Romanickcec65a62010-03-23 12:28:44 -07001297 if ((decl_type == NULL) || decl_type->is_void()) {
Ian Romanicka87ac252010-02-22 13:19:34 -08001298 if (type_name != NULL) {
1299 _mesa_glsl_error(& loc, state,
1300 "invalid type `%s' in declaration of `%s'",
1301 type_name, decl->identifier);
1302 } else {
1303 _mesa_glsl_error(& loc, state,
1304 "invalid type in declaration of `%s'",
1305 decl->identifier);
1306 }
1307 continue;
1308 }
1309
1310 if (decl->is_array) {
Ian Romanick28009cd2010-03-30 16:59:27 -07001311 var_type = process_array_type(decl_type, decl->array_size, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001312 } else {
1313 var_type = decl_type;
1314 }
1315
1316 var = new ir_variable(var_type, decl->identifier);
1317
Eric Anholt2e063f12010-03-28 00:56:22 -07001318 apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
1319 & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -08001320
1321 /* Attempt to add the variable to the symbol table. If this fails, it
Ian Romanicka4f308f2010-04-01 17:25:11 -07001322 * means the variable has already been declared at this scope. Arrays
1323 * fudge this rule a little bit.
1324 *
1325 * From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
1326 *
1327 * "It is legal to declare an array without a size and then
1328 * later re-declare the same name as an array of the same
1329 * type and specify a size."
Ian Romanicka87ac252010-02-22 13:19:34 -08001330 */
Ian Romanick3359e582010-03-19 15:38:52 -07001331 if (state->symbols->name_declared_this_scope(decl->identifier)) {
Ian Romanicka4f308f2010-04-01 17:25:11 -07001332 ir_variable *const earlier =
1333 state->symbols->get_variable(decl->identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08001334
Ian Romanicka4f308f2010-04-01 17:25:11 -07001335 if ((earlier != NULL)
1336 && (earlier->type->array_size() == 0)
1337 && var->type->is_array()
1338 && (var->type->element_type() == earlier->type->element_type())) {
1339 /* FINISHME: This doesn't match the qualifiers on the two
1340 * FINISHME: declarations. It's not 100% clear whether this is
1341 * FINISHME: required or not.
1342 */
Ian Romanickb8a21cc2010-04-01 18:31:11 -07001343
1344 if (var->type->array_size() <= earlier->max_array_access) {
1345 YYLTYPE loc = this->get_location();
1346
1347 _mesa_glsl_error(& loc, state, "array size must be > %u due to "
1348 "previous access",
1349 earlier->max_array_access);
1350 }
1351
Ian Romanicka4f308f2010-04-01 17:25:11 -07001352 earlier->type = var->type;
1353 delete var;
1354 var = NULL;
1355 } else {
1356 YYLTYPE loc = this->get_location();
1357
1358 _mesa_glsl_error(& loc, state, "`%s' redeclared",
1359 decl->identifier);
1360 }
1361
Ian Romanicka87ac252010-02-22 13:19:34 -08001362 continue;
1363 }
1364
Eric Anholtb97ee2e2010-03-31 08:20:58 -10001365 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
1366 *
1367 * "Identifiers starting with "gl_" are reserved for use by
1368 * OpenGL, and may not be declared in a shader as either a
1369 * variable or a function."
1370 */
1371 if (strncmp(decl->identifier, "gl_", 3) == 0) {
1372 /* FINISHME: This should only trigger if we're not redefining
1373 * FINISHME: a builtin (to add a qualifier, for example).
1374 */
1375 _mesa_glsl_error(& loc, state,
1376 "identifier `%s' uses reserved `gl_' prefix",
1377 decl->identifier);
1378 }
1379
Ian Romanick0044e7e2010-03-08 23:44:00 -08001380 instructions->push_tail(var);
Ian Romanicka87ac252010-02-22 13:19:34 -08001381
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07001382 if (state->current_function != NULL) {
Ian Romanickb168e532010-03-31 12:31:18 -07001383 const char *mode = NULL;
Ian Romanicke0800062010-03-31 13:15:23 -07001384 const char *extra = "";
Ian Romanickb168e532010-03-31 12:31:18 -07001385
Ian Romanicke0800062010-03-31 13:15:23 -07001386 /* There is no need to check for 'inout' here because the parser will
1387 * only allow that in function parameter lists.
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07001388 */
1389 if (this->type->qualifier.attribute) {
Ian Romanickb168e532010-03-31 12:31:18 -07001390 mode = "attribute";
1391 } else if (this->type->qualifier.uniform) {
1392 mode = "uniform";
1393 } else if (this->type->qualifier.varying) {
1394 mode = "varying";
Ian Romanicke0800062010-03-31 13:15:23 -07001395 } else if (this->type->qualifier.in) {
1396 mode = "in";
1397 extra = " or in function parameter list";
1398 } else if (this->type->qualifier.out) {
1399 mode = "out";
1400 extra = " or in function parameter list";
Ian Romanickb168e532010-03-31 12:31:18 -07001401 }
1402
1403 if (mode) {
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07001404 _mesa_glsl_error(& loc, state,
Ian Romanickb168e532010-03-31 12:31:18 -07001405 "%s variable `%s' must be declared at "
Ian Romanicke0800062010-03-31 13:15:23 -07001406 "global scope%s",
1407 mode, var->name, extra);
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07001408 }
1409 } else if (var->mode == ir_var_in) {
Ian Romanickfb9f5b02010-03-29 17:16:35 -07001410 if (state->target == vertex_shader) {
1411 bool error_emitted = false;
1412
1413 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1414 *
1415 * "Vertex shader inputs can only be float, floating-point
1416 * vectors, matrices, signed and unsigned integers and integer
1417 * vectors. Vertex shader inputs can also form arrays of these
1418 * types, but not structures."
1419 *
Ian Romanick2d816202010-03-29 17:40:11 -07001420 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
1421 *
1422 * "Vertex shader inputs can only be float, floating-point
1423 * vectors, matrices, signed and unsigned integers and integer
1424 * vectors. They cannot be arrays or structures."
1425 *
Ian Romanickfb9f5b02010-03-29 17:16:35 -07001426 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1427 *
1428 * "The attribute qualifier can be used only with float,
1429 * floating-point vectors, and matrices. Attribute variables
1430 * cannot be declared as arrays or structures."
1431 */
1432 const glsl_type *check_type = var->type->is_array()
1433 ? var->type->fields.array : var->type;
1434
1435 switch (check_type->base_type) {
1436 case GLSL_TYPE_FLOAT:
1437 break;
1438 case GLSL_TYPE_UINT:
1439 case GLSL_TYPE_INT:
1440 if (state->language_version > 120)
1441 break;
1442 /* FALLTHROUGH */
1443 default:
1444 _mesa_glsl_error(& loc, state,
1445 "vertex shader input / attribute cannot have "
1446 "type %s`%s'",
1447 var->type->is_array() ? "array of " : "",
1448 check_type->name);
1449 error_emitted = true;
1450 }
1451
Ian Romanick2d816202010-03-29 17:40:11 -07001452 if (!error_emitted && (state->language_version <= 130)
Ian Romanickfb9f5b02010-03-29 17:16:35 -07001453 && var->type->is_array()) {
1454 _mesa_glsl_error(& loc, state,
1455 "vertex shader input / attribute cannot have "
1456 "array type");
1457 error_emitted = true;
1458 }
1459 }
1460 }
1461
Ian Romanick66faec42010-03-27 18:56:53 -07001462 if (decl->initializer != NULL) {
Ian Romanick43de1722010-03-28 17:03:16 -07001463 YYLTYPE initializer_loc = decl->initializer->get_location();
1464
Ian Romanick66faec42010-03-27 18:56:53 -07001465 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
1466 *
1467 * "All uniform variables are read-only and are initialized either
1468 * directly by an application via API commands, or indirectly by
1469 * OpenGL."
1470 */
1471 if ((state->language_version <= 110)
1472 && (var->mode == ir_var_uniform)) {
Ian Romanick43de1722010-03-28 17:03:16 -07001473 _mesa_glsl_error(& initializer_loc, state,
1474 "cannot initialize uniforms in GLSL 1.10");
1475 }
Ian Romanick19360152010-03-26 18:05:27 -07001476
Ian Romanick43de1722010-03-28 17:03:16 -07001477 if (var->type->is_sampler()) {
1478 _mesa_glsl_error(& initializer_loc, state,
1479 "cannot initialize samplers");
1480 }
1481
1482 if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
1483 _mesa_glsl_error(& initializer_loc, state,
1484 "cannot initialize %s shader input / %s",
1485 (state->target == vertex_shader)
1486 ? "vertex" : "fragment",
1487 (state->target == vertex_shader)
1488 ? "attribute" : "varying");
Ian Romanick66faec42010-03-27 18:56:53 -07001489 }
1490
1491 ir_dereference *const lhs = new ir_dereference(var);
Eric Anholt307c71b2010-03-31 15:53:26 -10001492 ir_rvalue *rhs = decl->initializer->hir(instructions, state);
Ian Romanick66faec42010-03-27 18:56:53 -07001493
Eric Anholt307c71b2010-03-31 15:53:26 -10001494 /* Calculate the constant value if this is a const
1495 * declaration.
Ian Romanick66faec42010-03-27 18:56:53 -07001496 */
Eric Anholt307c71b2010-03-31 15:53:26 -10001497 if (this->type->qualifier.constant) {
1498 rhs = rhs->constant_expression_value();
1499 if (!rhs) {
1500 _mesa_glsl_error(& initializer_loc, state,
1501 "initializer of const variable `%s' must be a "
1502 "constant expression",
1503 decl->identifier);
1504 }
1505 }
Ian Romanick66faec42010-03-27 18:56:53 -07001506
Eric Anholt307c71b2010-03-31 15:53:26 -10001507 if (rhs && !rhs->type->is_error()) {
Eric Anholtac3af372010-03-31 15:44:38 -10001508 bool temp = var->read_only;
1509 if (this->type->qualifier.constant)
1510 var->read_only = false;
Ian Romanick66faec42010-03-27 18:56:53 -07001511 (void) do_assignment(instructions, state, lhs, rhs,
1512 this->get_location());
Eric Anholtac3af372010-03-31 15:44:38 -10001513 var->read_only = temp;
Ian Romanick66faec42010-03-27 18:56:53 -07001514 }
Ian Romanick19360152010-03-26 18:05:27 -07001515 }
Ian Romanick17d86f42010-03-29 12:59:02 -07001516
Eric Anholt0ed61252010-03-31 09:29:33 -10001517 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
1518 *
1519 * "It is an error to write to a const variable outside of
1520 * its declaration, so they must be initialized when
1521 * declared."
1522 */
1523 if (this->type->qualifier.constant && decl->initializer == NULL) {
1524 _mesa_glsl_error(& loc, state,
1525 "const declaration of `%s' must be initialized");
1526 }
1527
Ian Romanick17d86f42010-03-29 12:59:02 -07001528 /* Add the vairable to the symbol table after processing the initializer.
1529 * This differs from most C-like languages, but it follows the GLSL
1530 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
1531 * spec:
1532 *
1533 * "Within a declaration, the scope of a name starts immediately
1534 * after the initializer if present or immediately after the name
1535 * being declared if not."
1536 */
1537 const bool added_variable =
1538 state->symbols->add_variable(decl->identifier, var);
1539 assert(added_variable);
Ian Romanicka87ac252010-02-22 13:19:34 -08001540 }
1541
1542 /* Variable declarations do not have r-values.
1543 */
1544 return NULL;
1545}
1546
1547
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001548ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08001549ast_parameter_declarator::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08001550 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08001551{
Ian Romanicka87ac252010-02-22 13:19:34 -08001552 const struct glsl_type *type;
1553 const char *name = NULL;
Eric Anholt2e063f12010-03-28 00:56:22 -07001554 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08001555
Ian Romanickd612a122010-03-31 16:22:06 -07001556 type = this->type->specifier->glsl_type(& name, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001557
1558 if (type == NULL) {
Ian Romanicka87ac252010-02-22 13:19:34 -08001559 if (name != NULL) {
1560 _mesa_glsl_error(& loc, state,
1561 "invalid type `%s' in declaration of `%s'",
Ian Romanick18238de2010-03-01 13:49:10 -08001562 name, this->identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08001563 } else {
1564 _mesa_glsl_error(& loc, state,
1565 "invalid type in declaration of `%s'",
Ian Romanick18238de2010-03-01 13:49:10 -08001566 this->identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08001567 }
1568
Ian Romanick0471e8b2010-03-26 14:33:41 -07001569 type = glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -08001570 }
1571
Eric Anholt068c80c2010-03-31 09:56:36 -10001572 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
1573 *
1574 * "Functions that accept no input arguments need not use void in the
1575 * argument list because prototypes (or definitions) are required and
1576 * therefore there is no ambiguity when an empty argument list "( )" is
1577 * declared. The idiom "(void)" as a parameter list is provided for
1578 * convenience."
1579 *
1580 * Placing this check here prevents a void parameter being set up
1581 * for a function, which avoids tripping up checks for main taking
1582 * parameters and lookups of an unnamed symbol.
1583 */
1584 if (type->is_void() && (this->identifier == NULL))
1585 return NULL;
1586
Ian Romanick18238de2010-03-01 13:49:10 -08001587 ir_variable *var = new ir_variable(type, this->identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08001588
1589 /* FINISHME: Handle array declarations. Note that this requires
1590 * FINISHME: complete handling of constant expressions.
1591 */
1592
Ian Romanickcdb8d542010-03-11 14:48:51 -08001593 /* Apply any specified qualifiers to the parameter declaration. Note that
1594 * for function parameters the default mode is 'in'.
1595 */
Eric Anholt2e063f12010-03-28 00:56:22 -07001596 apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
Ian Romanickcdb8d542010-03-11 14:48:51 -08001597 if (var->mode == ir_var_auto)
1598 var->mode = ir_var_in;
Ian Romanicka87ac252010-02-22 13:19:34 -08001599
Ian Romanick0044e7e2010-03-08 23:44:00 -08001600 instructions->push_tail(var);
Ian Romanicka87ac252010-02-22 13:19:34 -08001601
1602 /* Parameter declarations do not have r-values.
1603 */
1604 return NULL;
1605}
1606
1607
1608static void
1609ast_function_parameters_to_hir(struct simple_node *ast_parameters,
Ian Romanick0044e7e2010-03-08 23:44:00 -08001610 exec_list *ir_parameters,
Ian Romanicka87ac252010-02-22 13:19:34 -08001611 struct _mesa_glsl_parse_state *state)
1612{
1613 struct simple_node *ptr;
1614
1615 foreach (ptr, ast_parameters) {
Eric Anholt068c80c2010-03-31 09:56:36 -10001616 ast_node *param = (ast_node *)ptr;
1617 param->hir(ir_parameters, state);
1618
Ian Romanicka87ac252010-02-22 13:19:34 -08001619 }
1620}
1621
1622
1623static bool
Ian Romanick0044e7e2010-03-08 23:44:00 -08001624parameter_lists_match(exec_list *list_a, exec_list *list_b)
Ian Romanicka87ac252010-02-22 13:19:34 -08001625{
Ian Romanick0044e7e2010-03-08 23:44:00 -08001626 exec_list_iterator iter_a = list_a->iterator();
1627 exec_list_iterator iter_b = list_b->iterator();
Ian Romanicka87ac252010-02-22 13:19:34 -08001628
Ian Romanick0044e7e2010-03-08 23:44:00 -08001629 while (iter_a.has_next()) {
Ian Romanicka87ac252010-02-22 13:19:34 -08001630 /* If all of the parameters from the other parameter list have been
1631 * exhausted, the lists have different length and, by definition,
1632 * do not match.
1633 */
Ian Romanick0044e7e2010-03-08 23:44:00 -08001634 if (!iter_b.has_next())
Ian Romanicka87ac252010-02-22 13:19:34 -08001635 return false;
1636
1637 /* If the types of the parameters do not match, the parameters lists
1638 * are different.
1639 */
1640 /* FINISHME */
1641
1642
Ian Romanick0044e7e2010-03-08 23:44:00 -08001643 iter_a.next();
1644 iter_b.next();
Ian Romanicka87ac252010-02-22 13:19:34 -08001645 }
1646
1647 return true;
1648}
1649
1650
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001651ir_rvalue *
Ian Romanick92318a92010-03-31 18:23:21 -07001652ast_function::hir(exec_list *instructions,
1653 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08001654{
Ian Romanick18238de2010-03-01 13:49:10 -08001655 ir_function *f = NULL;
Ian Romanick92318a92010-03-31 18:23:21 -07001656 ir_function_signature *sig = NULL;
1657 exec_list hir_parameters;
Ian Romanicka87ac252010-02-22 13:19:34 -08001658
1659
Ian Romanick92318a92010-03-31 18:23:21 -07001660 /* The prototype part of a function does not generate anything in the IR
1661 * instruction stream.
1662 */
1663 (void) instructions;
1664
Ian Romanicka87ac252010-02-22 13:19:34 -08001665 /* Convert the list of function parameters to HIR now so that they can be
1666 * used below to compare this function's signature with previously seen
1667 * signatures for functions with the same name.
1668 */
Ian Romanick92318a92010-03-31 18:23:21 -07001669 ast_function_parameters_to_hir(& this->parameters, & hir_parameters, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001670
Ian Romanicke39cc692010-03-23 12:19:13 -07001671 const char *return_type_name;
1672 const glsl_type *return_type =
Ian Romanick92318a92010-03-31 18:23:21 -07001673 this->return_type->specifier->glsl_type(& return_type_name, state);
Ian Romanicke39cc692010-03-23 12:19:13 -07001674
1675 assert(return_type != NULL);
1676
Ian Romanicka87ac252010-02-22 13:19:34 -08001677 /* Verify that this function's signature either doesn't match a previously
1678 * seen signature for a function with the same name, or, if a match is found,
1679 * that the previously seen signature does not have an associated definition.
1680 */
Ian Romanick92318a92010-03-31 18:23:21 -07001681 const char *const name = identifier;
Ian Romanick3359e582010-03-19 15:38:52 -07001682 f = state->symbols->get_function(name);
Ian Romanicka87ac252010-02-22 13:19:34 -08001683 if (f != NULL) {
Ian Romanick95cd6cc2010-03-31 16:40:26 -07001684 foreach_iter(exec_list_iterator, iter, *f) {
Ian Romanick92318a92010-03-31 18:23:21 -07001685 sig = (struct ir_function_signature *) iter.get();
Ian Romanicka87ac252010-02-22 13:19:34 -08001686
1687 /* Compare the parameter list of the function being defined to the
1688 * existing function. If the parameter lists match, then the return
1689 * type must also match and the existing function must not have a
1690 * definition.
1691 */
Ian Romanick92318a92010-03-31 18:23:21 -07001692 if (parameter_lists_match(& hir_parameters, & sig->parameters)) {
Ian Romanicka87ac252010-02-22 13:19:34 -08001693 /* FINISHME: Compare return types. */
1694
Ian Romanick92318a92010-03-31 18:23:21 -07001695 if (is_definition && (sig->definition != NULL)) {
Ian Romanick18238de2010-03-01 13:49:10 -08001696 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08001697
Ian Romanick3359e582010-03-19 15:38:52 -07001698 _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
Ian Romanick92318a92010-03-31 18:23:21 -07001699 sig = NULL;
Ian Romanicka87ac252010-02-22 13:19:34 -08001700 break;
1701 }
1702 }
1703
Ian Romanick92318a92010-03-31 18:23:21 -07001704 sig = NULL;
Ian Romanicka87ac252010-02-22 13:19:34 -08001705 }
1706
Ian Romanick3359e582010-03-19 15:38:52 -07001707 } else if (state->symbols->name_declared_this_scope(name)) {
1708 /* This function name shadows a non-function use of the same name.
1709 */
1710 YYLTYPE loc = this->get_location();
1711
1712 _mesa_glsl_error(& loc, state, "function name `%s' conflicts with "
1713 "non-function", name);
Ian Romanick92318a92010-03-31 18:23:21 -07001714 sig = NULL;
Ian Romanicka87ac252010-02-22 13:19:34 -08001715 } else {
Ian Romanick882dad72010-03-23 17:42:04 -07001716 f = new ir_function(name);
Ian Romanick8bde4ce2010-03-19 11:57:24 -07001717 state->symbols->add_function(f->name, f);
Ian Romanicka87ac252010-02-22 13:19:34 -08001718 }
1719
Eric Anholtab372da2010-03-28 01:24:55 -07001720 /* Verify the return type of main() */
1721 if (strcmp(name, "main") == 0) {
Ian Romanick25711a82010-03-31 17:39:10 -07001722 if (! return_type->is_void()) {
Eric Anholtab372da2010-03-28 01:24:55 -07001723 YYLTYPE loc = this->get_location();
1724
1725 _mesa_glsl_error(& loc, state, "main() must return void");
1726 }
Eric Anholt174cc032010-03-30 23:37:51 -10001727
Ian Romanick92318a92010-03-31 18:23:21 -07001728 if (!hir_parameters.is_empty()) {
Eric Anholt174cc032010-03-30 23:37:51 -10001729 YYLTYPE loc = this->get_location();
1730
1731 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
1732 }
Eric Anholtab372da2010-03-28 01:24:55 -07001733 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001734
1735 /* Finish storing the information about this new function in its signature.
1736 */
Ian Romanick92318a92010-03-31 18:23:21 -07001737 if (sig == NULL) {
1738 sig = new ir_function_signature(return_type);
1739 f->add_signature(sig);
1740 } else if (is_definition) {
Ian Romanicka87ac252010-02-22 13:19:34 -08001741 /* Destroy all of the previous parameter information. The previous
1742 * parameter information comes from the function prototype, and it can
1743 * either include invalid parameter names or may not have names at all.
1744 */
Ian Romanick92318a92010-03-31 18:23:21 -07001745 foreach_iter(exec_list_iterator, iter, sig->parameters) {
Kenneth Graunke44e1dfa2010-03-25 23:30:28 -07001746 assert(((ir_instruction *) iter.get())->as_variable() != NULL);
Ian Romanicka87ac252010-02-22 13:19:34 -08001747
Ian Romanick0044e7e2010-03-08 23:44:00 -08001748 iter.remove();
1749 delete iter.get();
Ian Romanicka87ac252010-02-22 13:19:34 -08001750 }
1751 }
1752
Ian Romanick92318a92010-03-31 18:23:21 -07001753 hir_parameters.move_nodes_to(& sig->parameters);
1754 signature = sig;
Ian Romanicke29a5852010-03-31 17:54:26 -07001755
Ian Romanick92318a92010-03-31 18:23:21 -07001756 /* Function declarations (prototypes) do not have r-values.
1757 */
1758 return NULL;
1759}
1760
1761
1762ir_rvalue *
1763ast_function_definition::hir(exec_list *instructions,
1764 struct _mesa_glsl_parse_state *state)
1765{
1766 prototype->is_definition = true;
1767 prototype->hir(instructions, state);
1768
1769 ir_function_signature *signature = prototype->signature;
Ian Romanicka87ac252010-02-22 13:19:34 -08001770
Ian Romanick41ec6a42010-03-19 17:08:05 -07001771 assert(state->current_function == NULL);
1772 state->current_function = signature;
1773
Ian Romanick92318a92010-03-31 18:23:21 -07001774 ir_label *label = new ir_label(signature->function_name());
Ian Romanicka87ac252010-02-22 13:19:34 -08001775 if (signature->definition == NULL) {
1776 signature->definition = label;
1777 }
Ian Romanick0044e7e2010-03-08 23:44:00 -08001778 instructions->push_tail(label);
Ian Romanicka87ac252010-02-22 13:19:34 -08001779
Ian Romanicke29a5852010-03-31 17:54:26 -07001780 /* Duplicate parameters declared in the prototype as concrete variables.
1781 * Add these to the symbol table.
Ian Romanicka87ac252010-02-22 13:19:34 -08001782 */
Ian Romanick8bde4ce2010-03-19 11:57:24 -07001783 state->symbols->push_scope();
Ian Romanicke29a5852010-03-31 17:54:26 -07001784 foreach_iter(exec_list_iterator, iter, signature->parameters) {
1785 ir_variable *const proto = ((ir_instruction *) iter.get())->as_variable();
Ian Romanicka87ac252010-02-22 13:19:34 -08001786
Ian Romanicke29a5852010-03-31 17:54:26 -07001787 assert(proto != NULL);
Ian Romanicka87ac252010-02-22 13:19:34 -08001788
Ian Romanicke29a5852010-03-31 17:54:26 -07001789 ir_variable *const var = proto->clone();
1790
Ian Romanick0044e7e2010-03-08 23:44:00 -08001791 instructions->push_tail(var);
Ian Romanicka87ac252010-02-22 13:19:34 -08001792
Ian Romanick3359e582010-03-19 15:38:52 -07001793 /* The only way a parameter would "exist" is if two parameters have
1794 * the same name.
1795 */
1796 if (state->symbols->name_declared_this_scope(var->name)) {
1797 YYLTYPE loc = this->get_location();
1798
1799 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
1800 } else {
1801 state->symbols->add_variable(var->name, var);
1802 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001803 }
1804
1805 /* Convert the body of the function to HIR, and append the resulting
1806 * instructions to the list that currently consists of the function label
1807 * and the function parameters.
1808 */
Ian Romanick18238de2010-03-01 13:49:10 -08001809 this->body->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001810
Ian Romanick8bde4ce2010-03-19 11:57:24 -07001811 state->symbols->pop_scope();
Ian Romanicka87ac252010-02-22 13:19:34 -08001812
Ian Romanick41ec6a42010-03-19 17:08:05 -07001813 assert(state->current_function == signature);
1814 state->current_function = NULL;
Ian Romanicka87ac252010-02-22 13:19:34 -08001815
1816 /* Function definitions do not have r-values.
1817 */
1818 return NULL;
1819}
Ian Romanick16a246c2010-03-19 16:45:19 -07001820
1821
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001822ir_rvalue *
Ian Romanick16a246c2010-03-19 16:45:19 -07001823ast_jump_statement::hir(exec_list *instructions,
1824 struct _mesa_glsl_parse_state *state)
1825{
1826
1827 if (mode == ast_return) {
1828 ir_return *inst;
Eric Anholtaad7c772010-03-30 23:28:20 -10001829 assert(state->current_function);
Ian Romanick16a246c2010-03-19 16:45:19 -07001830
1831 if (opt_return_value) {
Eric Anholtab79d4e2010-03-30 23:23:16 -10001832 if (state->current_function->return_type->base_type ==
1833 GLSL_TYPE_VOID) {
1834 YYLTYPE loc = this->get_location();
1835
1836 _mesa_glsl_error(& loc, state,
1837 "`return` with a value, in function `%s' "
1838 "returning void",
1839 state->current_function->definition->label);
1840 }
Ian Romanick16a246c2010-03-19 16:45:19 -07001841
1842 ir_expression *const ret = (ir_expression *)
1843 opt_return_value->hir(instructions, state);
1844 assert(ret != NULL);
1845
1846 /* FINISHME: Make sure the type of the return value matches the return
1847 * FINISHME: type of the enclosing function.
1848 */
1849
1850 inst = new ir_return(ret);
1851 } else {
Eric Anholtaad7c772010-03-30 23:28:20 -10001852 if (state->current_function->return_type->base_type !=
1853 GLSL_TYPE_VOID) {
1854 YYLTYPE loc = this->get_location();
1855
1856 _mesa_glsl_error(& loc, state,
1857 "`return' with no value, in function %s returning "
1858 "non-void",
1859 state->current_function->definition->label);
1860 }
Ian Romanick16a246c2010-03-19 16:45:19 -07001861 inst = new ir_return;
1862 }
1863
1864 instructions->push_tail(inst);
1865 }
1866
Eric Anholtb9802072010-03-30 23:40:14 -10001867 if (mode == ast_discard) {
1868 /* FINISHME: discard support */
1869 if (state->target != fragment_shader) {
1870 YYLTYPE loc = this->get_location();
1871
1872 _mesa_glsl_error(& loc, state,
1873 "`discard' may only appear in a fragment shader");
1874 }
1875 }
1876
Ian Romanick16a246c2010-03-19 16:45:19 -07001877 /* Jump instructions do not have r-values.
1878 */
1879 return NULL;
1880}
Ian Romanick3c6fea32010-03-29 14:11:25 -07001881
1882
1883ir_rvalue *
1884ast_selection_statement::hir(exec_list *instructions,
1885 struct _mesa_glsl_parse_state *state)
1886{
1887 ir_rvalue *const condition = this->condition->hir(instructions, state);
Ian Romanick3c6fea32010-03-29 14:11:25 -07001888
1889 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
1890 *
1891 * "Any expression whose type evaluates to a Boolean can be used as the
1892 * conditional expression bool-expression. Vector types are not accepted
1893 * as the expression to if."
1894 *
1895 * The checks are separated so that higher quality diagnostics can be
1896 * generated for cases where both rules are violated.
1897 */
1898 if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
1899 YYLTYPE loc = this->condition->get_location();
1900
1901 _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
1902 "boolean");
1903 }
1904
1905 ir_if *const stmt = new ir_if(condition);
1906
1907 if (then_statement != NULL) {
1908 ast_node *node = (ast_node *) then_statement;
1909 do {
1910 node->hir(& stmt->then_instructions, state);
1911 node = (ast_node *) node->next;
1912 } while (node != then_statement);
1913 }
1914
1915 if (else_statement != NULL) {
1916 ast_node *node = (ast_node *) else_statement;
1917 do {
1918 node->hir(& stmt->else_instructions, state);
1919 node = (ast_node *) node->next;
1920 } while (node != else_statement);
1921 }
1922
1923 instructions->push_tail(stmt);
1924
1925 /* if-statements do not have r-values.
1926 */
1927 return NULL;
1928}