blob: e5a21fd816810ccb5215f491e6161ccc223c26a5 [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 */
Eric Anholtac95f2f2010-06-22 10:38:52 -070051
Chia-I Wubfd7c9a2010-08-23 17:51:42 +080052#include "main/core.h" /* for struct gl_extensions */
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{
Ian Romanickadfb0cd2010-03-10 10:43:16 -080062 _mesa_glsl_initialize_variables(instructions, state);
Kenneth Graunke81168352011-01-01 12:01:09 -080063 _mesa_glsl_initialize_functions(state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -080064
Kenneth Graunke814c89a2010-09-05 00:31:28 -070065 state->symbols->language_version = state->language_version;
66
Ian Romanick41ec6a42010-03-19 17:08:05 -070067 state->current_function = NULL;
68
Kenneth Graunkea0442852010-08-23 14:52:06 -070069 /* Section 4.2 of the GLSL 1.20 specification states:
70 * "The built-in functions are scoped in a scope outside the global scope
71 * users declare global variables in. That is, a shader's global scope,
72 * available for user-defined functions and global variables, is nested
73 * inside the scope containing the built-in functions."
74 *
75 * Since built-in functions like ftransform() access built-in variables,
76 * it follows that those must be in the outer scope as well.
77 *
78 * We push scope here to create this nesting effect...but don't pop.
79 * This way, a shader's globals are still in the symbol table for use
80 * by the linker.
81 */
82 state->symbols->push_scope();
83
Ian Romanick2b97dc62010-05-10 17:42:05 -070084 foreach_list_typed (ast_node, ast, link, & state->translation_unit)
Ian Romanick304ea902010-05-10 11:17:53 -070085 ast->hir(instructions, state);
Ian Romanickd949a9a2010-03-10 09:55:22 -080086}
87
88
Ian Romanick01045362010-03-29 16:17:56 -070089/**
90 * If a conversion is available, convert one operand to a different type
91 *
92 * The \c from \c ir_rvalue is converted "in place".
93 *
94 * \param to Type that the operand it to be converted to
95 * \param from Operand that is being converted
96 * \param state GLSL compiler state
97 *
98 * \return
99 * If a conversion is possible (or unnecessary), \c true is returned.
100 * Otherwise \c false is returned.
101 */
Kenneth Graunkef32d3df2010-09-01 20:03:17 -0700102bool
Ian Romanickbfb09c22010-03-29 16:32:55 -0700103apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
Ian Romanick01045362010-03-29 16:17:56 -0700104 struct _mesa_glsl_parse_state *state)
105{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700106 void *ctx = state;
Ian Romanickbfb09c22010-03-29 16:32:55 -0700107 if (to->base_type == from->type->base_type)
Ian Romanick01045362010-03-29 16:17:56 -0700108 return true;
109
110 /* This conversion was added in GLSL 1.20. If the compilation mode is
111 * GLSL 1.10, the conversion is skipped.
112 */
113 if (state->language_version < 120)
114 return false;
115
116 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
117 *
118 * "There are no implicit array or structure conversions. For
119 * example, an array of int cannot be implicitly converted to an
120 * array of float. There are no implicit conversions between
121 * signed and unsigned integers."
122 */
123 /* FINISHME: The above comment is partially a lie. There is int/uint
124 * FINISHME: conversion for immediate constants.
125 */
Ian Romanickbfb09c22010-03-29 16:32:55 -0700126 if (!to->is_float() || !from->type->is_numeric())
Ian Romanick01045362010-03-29 16:17:56 -0700127 return false;
128
Kenneth Graunke506199b2010-06-29 15:59:27 -0700129 /* Convert to a floating point type with the same number of components
130 * as the original type - i.e. int to float, not int to vec4.
131 */
132 to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements,
133 from->type->matrix_columns);
134
Ian Romanickbfb09c22010-03-29 16:32:55 -0700135 switch (from->type->base_type) {
Ian Romanick01045362010-03-29 16:17:56 -0700136 case GLSL_TYPE_INT:
Carl Worth1660a292010-06-23 18:11:51 -0700137 from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL);
Ian Romanick01045362010-03-29 16:17:56 -0700138 break;
139 case GLSL_TYPE_UINT:
Carl Worth1660a292010-06-23 18:11:51 -0700140 from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL);
Ian Romanick01045362010-03-29 16:17:56 -0700141 break;
142 case GLSL_TYPE_BOOL:
Carl Worth1660a292010-06-23 18:11:51 -0700143 from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL);
Eric Anholtdc58b3f2010-04-02 02:13:43 -1000144 break;
Ian Romanick01045362010-03-29 16:17:56 -0700145 default:
146 assert(0);
147 }
148
149 return true;
150}
151
152
Ian Romanicka87ac252010-02-22 13:19:34 -0800153static const struct glsl_type *
Ian Romanickbfb09c22010-03-29 16:32:55 -0700154arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
Ian Romanicka87ac252010-02-22 13:19:34 -0800155 bool multiply,
Eric Anholta13bb142010-03-31 16:38:11 -1000156 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800157{
Eric Anholt336b4ad2010-05-19 10:38:37 -0700158 const glsl_type *type_a = value_a->type;
159 const glsl_type *type_b = value_b->type;
Ian Romanick01045362010-03-29 16:17:56 -0700160
Ian Romanicka87ac252010-02-22 13:19:34 -0800161 /* From GLSL 1.50 spec, page 56:
162 *
163 * "The arithmetic binary operators add (+), subtract (-),
164 * multiply (*), and divide (/) operate on integer and
165 * floating-point scalars, vectors, and matrices."
166 */
Ian Romanick60b54d92010-03-24 17:08:13 -0700167 if (!type_a->is_numeric() || !type_b->is_numeric()) {
Eric Anholta13bb142010-03-31 16:38:11 -1000168 _mesa_glsl_error(loc, state,
169 "Operands to arithmetic operators must be numeric");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700170 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800171 }
172
173
174 /* "If one operand is floating-point based and the other is
175 * not, then the conversions from Section 4.1.10 "Implicit
176 * Conversions" are applied to the non-floating-point-based operand."
Ian Romanicka87ac252010-02-22 13:19:34 -0800177 */
Ian Romanick01045362010-03-29 16:17:56 -0700178 if (!apply_implicit_conversion(type_a, value_b, state)
179 && !apply_implicit_conversion(type_b, value_a, state)) {
Eric Anholta13bb142010-03-31 16:38:11 -1000180 _mesa_glsl_error(loc, state,
181 "Could not implicitly convert operands to "
182 "arithmetic operator");
Ian Romanick01045362010-03-29 16:17:56 -0700183 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800184 }
Eric Anholt336b4ad2010-05-19 10:38:37 -0700185 type_a = value_a->type;
186 type_b = value_b->type;
187
Ian Romanicka87ac252010-02-22 13:19:34 -0800188 /* "If the operands are integer types, they must both be signed or
189 * both be unsigned."
190 *
191 * From this rule and the preceeding conversion it can be inferred that
192 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
Ian Romanick60b54d92010-03-24 17:08:13 -0700193 * The is_numeric check above already filtered out the case where either
194 * type is not one of these, so now the base types need only be tested for
195 * equality.
Ian Romanicka87ac252010-02-22 13:19:34 -0800196 */
197 if (type_a->base_type != type_b->base_type) {
Eric Anholta13bb142010-03-31 16:38:11 -1000198 _mesa_glsl_error(loc, state,
199 "base type mismatch for arithmetic operator");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700200 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800201 }
202
203 /* "All arithmetic binary operators result in the same fundamental type
204 * (signed integer, unsigned integer, or floating-point) as the
205 * operands they operate on, after operand type conversion. After
206 * conversion, the following cases are valid
207 *
208 * * The two operands are scalars. In this case the operation is
209 * applied, resulting in a scalar."
210 */
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800211 if (type_a->is_scalar() && type_b->is_scalar())
Ian Romanicka87ac252010-02-22 13:19:34 -0800212 return type_a;
213
214 /* "* One operand is a scalar, and the other is a vector or matrix.
215 * In this case, the scalar operation is applied independently to each
216 * component of the vector or matrix, resulting in the same size
217 * vector or matrix."
218 */
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800219 if (type_a->is_scalar()) {
220 if (!type_b->is_scalar())
Ian Romanicka87ac252010-02-22 13:19:34 -0800221 return type_b;
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800222 } else if (type_b->is_scalar()) {
Ian Romanicka87ac252010-02-22 13:19:34 -0800223 return type_a;
224 }
225
226 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
227 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
228 * handled.
229 */
Ian Romanick60b54d92010-03-24 17:08:13 -0700230 assert(!type_a->is_scalar());
231 assert(!type_b->is_scalar());
Ian Romanicka87ac252010-02-22 13:19:34 -0800232
233 /* "* The two operands are vectors of the same size. In this case, the
234 * operation is done component-wise resulting in the same size
235 * vector."
236 */
Ian Romanicka2dd22f2010-03-09 15:55:16 -0800237 if (type_a->is_vector() && type_b->is_vector()) {
Eric Anholta13bb142010-03-31 16:38:11 -1000238 if (type_a == type_b) {
239 return type_a;
240 } else {
241 _mesa_glsl_error(loc, state,
242 "vector size mismatch for arithmetic operator");
243 return glsl_type::error_type;
244 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800245 }
246
247 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
248 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
249 * <vector, vector> have been handled. At least one of the operands must
250 * be matrix. Further, since there are no integer matrix types, the base
251 * type of both operands must be float.
252 */
Ian Romanick60b54d92010-03-24 17:08:13 -0700253 assert(type_a->is_matrix() || type_b->is_matrix());
Ian Romanicka87ac252010-02-22 13:19:34 -0800254 assert(type_a->base_type == GLSL_TYPE_FLOAT);
255 assert(type_b->base_type == GLSL_TYPE_FLOAT);
256
257 /* "* The operator is add (+), subtract (-), or divide (/), and the
258 * operands are matrices with the same number of rows and the same
259 * number of columns. In this case, the operation is done component-
260 * wise resulting in the same size matrix."
261 * * The operator is multiply (*), where both operands are matrices or
262 * one operand is a vector and the other a matrix. A right vector
263 * operand is treated as a column vector and a left vector operand as a
264 * row vector. In all these cases, it is required that the number of
265 * columns of the left operand is equal to the number of rows of the
266 * right operand. Then, the multiply (*) operation does a linear
267 * algebraic multiply, yielding an object that has the same number of
268 * rows as the left operand and the same number of columns as the right
269 * operand. Section 5.10 "Vector and Matrix Operations" explains in
270 * more detail how vectors and matrices are operated on."
271 */
272 if (! multiply) {
Eric Anholta13bb142010-03-31 16:38:11 -1000273 if (type_a == type_b)
274 return type_a;
Ian Romanicka87ac252010-02-22 13:19:34 -0800275 } else {
Ian Romanickfce11502010-03-09 15:58:52 -0800276 if (type_a->is_matrix() && type_b->is_matrix()) {
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700277 /* Matrix multiply. The columns of A must match the rows of B. Given
278 * the other previously tested constraints, this means the vector type
279 * of a row from A must be the same as the vector type of a column from
280 * B.
281 */
282 if (type_a->row_type() == type_b->column_type()) {
283 /* The resulting matrix has the number of columns of matrix B and
284 * the number of rows of matrix A. We get the row count of A by
285 * looking at the size of a vector that makes up a column. The
286 * transpose (size of a row) is done for B.
287 */
Eric Anholta13bb142010-03-31 16:38:11 -1000288 const glsl_type *const type =
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700289 glsl_type::get_instance(type_a->base_type,
290 type_a->column_type()->vector_elements,
291 type_b->row_type()->vector_elements);
Eric Anholta13bb142010-03-31 16:38:11 -1000292 assert(type != glsl_type::error_type);
293
294 return type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800295 }
Ian Romanickfce11502010-03-09 15:58:52 -0800296 } else if (type_a->is_matrix()) {
Ian Romanicka87ac252010-02-22 13:19:34 -0800297 /* A is a matrix and B is a column vector. Columns of A must match
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700298 * rows of B. Given the other previously tested constraints, this
299 * means the vector type of a row from A must be the same as the
300 * vector the type of B.
Ian Romanicka87ac252010-02-22 13:19:34 -0800301 */
Carl Worth47c90b12010-07-22 14:56:14 -0700302 if (type_a->row_type() == type_b) {
303 /* The resulting vector has a number of elements equal to
304 * the number of rows of matrix A. */
305 const glsl_type *const type =
306 glsl_type::get_instance(type_a->base_type,
307 type_a->column_type()->vector_elements,
308 1);
309 assert(type != glsl_type::error_type);
310
311 return type;
312 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800313 } else {
Ian Romanickfce11502010-03-09 15:58:52 -0800314 assert(type_b->is_matrix());
Ian Romanicka87ac252010-02-22 13:19:34 -0800315
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700316 /* A is a row vector and B is a matrix. Columns of A must match rows
317 * of B. Given the other previously tested constraints, this means
318 * the type of A must be the same as the vector type of a column from
319 * B.
Ian Romanicka87ac252010-02-22 13:19:34 -0800320 */
Carl Worth47c90b12010-07-22 14:56:14 -0700321 if (type_a == type_b->column_type()) {
322 /* The resulting vector has a number of elements equal to
323 * the number of columns of matrix B. */
324 const glsl_type *const type =
325 glsl_type::get_instance(type_a->base_type,
326 type_b->row_type()->vector_elements,
327 1);
328 assert(type != glsl_type::error_type);
329
330 return type;
331 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800332 }
Eric Anholta13bb142010-03-31 16:38:11 -1000333
334 _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
335 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800336 }
337
338
339 /* "All other cases are illegal."
340 */
Eric Anholta13bb142010-03-31 16:38:11 -1000341 _mesa_glsl_error(loc, state, "type mismatch");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700342 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800343}
344
345
346static const struct glsl_type *
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000347unary_arithmetic_result_type(const struct glsl_type *type,
348 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800349{
350 /* From GLSL 1.50 spec, page 57:
351 *
352 * "The arithmetic unary operators negate (-), post- and pre-increment
353 * and decrement (-- and ++) operate on integer or floating-point
354 * values (including vectors and matrices). All unary operators work
355 * component-wise on their operands. These result with the same type
356 * they operated on."
357 */
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000358 if (!type->is_numeric()) {
359 _mesa_glsl_error(loc, state,
360 "Operands to arithmetic operators must be numeric");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700361 return glsl_type::error_type;
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000362 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800363
364 return type;
365}
366
Chad Versacecfdbf8b2010-10-15 11:28:05 -0700367/**
368 * \brief Return the result type of a bit-logic operation.
369 *
370 * If the given types to the bit-logic operator are invalid, return
371 * glsl_type::error_type.
372 *
373 * \param type_a Type of LHS of bit-logic op
374 * \param type_b Type of RHS of bit-logic op
375 */
376static const struct glsl_type *
377bit_logic_result_type(const struct glsl_type *type_a,
378 const struct glsl_type *type_b,
379 ast_operators op,
380 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
381{
382 if (state->language_version < 130) {
383 _mesa_glsl_error(loc, state, "bit operations require GLSL 1.30");
384 return glsl_type::error_type;
385 }
386
387 /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
388 *
389 * "The bitwise operators and (&), exclusive-or (^), and inclusive-or
390 * (|). The operands must be of type signed or unsigned integers or
391 * integer vectors."
392 */
393 if (!type_a->is_integer()) {
394 _mesa_glsl_error(loc, state, "LHS of `%s' must be an integer",
395 ast_expression::operator_string(op));
396 return glsl_type::error_type;
397 }
398 if (!type_b->is_integer()) {
399 _mesa_glsl_error(loc, state, "RHS of `%s' must be an integer",
400 ast_expression::operator_string(op));
401 return glsl_type::error_type;
402 }
403
404 /* "The fundamental types of the operands (signed or unsigned) must
405 * match,"
406 */
407 if (type_a->base_type != type_b->base_type) {
408 _mesa_glsl_error(loc, state, "operands of `%s' must have the same "
409 "base type", ast_expression::operator_string(op));
410 return glsl_type::error_type;
411 }
412
413 /* "The operands cannot be vectors of differing size." */
414 if (type_a->is_vector() &&
415 type_b->is_vector() &&
416 type_a->vector_elements != type_b->vector_elements) {
417 _mesa_glsl_error(loc, state, "operands of `%s' cannot be vectors of "
418 "different sizes", ast_expression::operator_string(op));
419 return glsl_type::error_type;
420 }
421
422 /* "If one operand is a scalar and the other a vector, the scalar is
423 * applied component-wise to the vector, resulting in the same type as
424 * the vector. The fundamental types of the operands [...] will be the
425 * resulting fundamental type."
426 */
427 if (type_a->is_scalar())
428 return type_b;
429 else
430 return type_a;
431}
Ian Romanicka87ac252010-02-22 13:19:34 -0800432
433static const struct glsl_type *
434modulus_result_type(const struct glsl_type *type_a,
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000435 const struct glsl_type *type_b,
436 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800437{
438 /* From GLSL 1.50 spec, page 56:
439 * "The operator modulus (%) operates on signed or unsigned integers or
440 * integer vectors. The operand types must both be signed or both be
441 * unsigned."
442 */
Ian Romanick40176e22010-03-26 14:38:37 -0700443 if (!type_a->is_integer() || !type_b->is_integer()
Ian Romanicka87ac252010-02-22 13:19:34 -0800444 || (type_a->base_type != type_b->base_type)) {
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000445 _mesa_glsl_error(loc, state, "type mismatch");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700446 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800447 }
448
449 /* "The operands cannot be vectors of differing size. If one operand is
450 * a scalar and the other vector, then the scalar is applied component-
451 * wise to the vector, resulting in the same type as the vector. If both
452 * are vectors of the same size, the result is computed component-wise."
453 */
Ian Romanicka2dd22f2010-03-09 15:55:16 -0800454 if (type_a->is_vector()) {
455 if (!type_b->is_vector()
Ian Romanicka87ac252010-02-22 13:19:34 -0800456 || (type_a->vector_elements == type_b->vector_elements))
457 return type_a;
458 } else
459 return type_b;
460
461 /* "The operator modulus (%) is not defined for any other data types
462 * (non-integer types)."
463 */
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000464 _mesa_glsl_error(loc, state, "type mismatch");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700465 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800466}
467
468
469static const struct glsl_type *
Ian Romanickbfb09c22010-03-29 16:32:55 -0700470relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000471 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800472{
Eric Anholt336b4ad2010-05-19 10:38:37 -0700473 const glsl_type *type_a = value_a->type;
474 const glsl_type *type_b = value_b->type;
Ian Romanick0150f5f2010-03-29 16:20:07 -0700475
Ian Romanicka87ac252010-02-22 13:19:34 -0800476 /* From GLSL 1.50 spec, page 56:
477 * "The relational operators greater than (>), less than (<), greater
478 * than or equal (>=), and less than or equal (<=) operate only on
479 * scalar integer and scalar floating-point expressions."
480 */
Ian Romanicka6d653d2010-03-26 14:40:37 -0700481 if (!type_a->is_numeric()
482 || !type_b->is_numeric()
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800483 || !type_a->is_scalar()
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000484 || !type_b->is_scalar()) {
485 _mesa_glsl_error(loc, state,
486 "Operands to relational operators must be scalar and "
487 "numeric");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700488 return glsl_type::error_type;
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000489 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800490
491 /* "Either the operands' types must match, or the conversions from
492 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
493 * operand, after which the types must match."
Ian Romanicka87ac252010-02-22 13:19:34 -0800494 */
Ian Romanick0150f5f2010-03-29 16:20:07 -0700495 if (!apply_implicit_conversion(type_a, value_b, state)
496 && !apply_implicit_conversion(type_b, value_a, state)) {
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000497 _mesa_glsl_error(loc, state,
498 "Could not implicitly convert operands to "
499 "relational operator");
Ian Romanick0150f5f2010-03-29 16:20:07 -0700500 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800501 }
Eric Anholt336b4ad2010-05-19 10:38:37 -0700502 type_a = value_a->type;
503 type_b = value_b->type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800504
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000505 if (type_a->base_type != type_b->base_type) {
506 _mesa_glsl_error(loc, state, "base type mismatch");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700507 return glsl_type::error_type;
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000508 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800509
510 /* "The result is scalar Boolean."
511 */
Ian Romanick0471e8b2010-03-26 14:33:41 -0700512 return glsl_type::bool_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800513}
514
Chad Versacec0197ab2010-10-15 09:49:46 -0700515/**
516 * \brief Return the result type of a bit-shift operation.
517 *
518 * If the given types to the bit-shift operator are invalid, return
519 * glsl_type::error_type.
520 *
521 * \param type_a Type of LHS of bit-shift op
522 * \param type_b Type of RHS of bit-shift op
523 */
524static const struct glsl_type *
525shift_result_type(const struct glsl_type *type_a,
526 const struct glsl_type *type_b,
527 ast_operators op,
528 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
529{
530 if (state->language_version < 130) {
531 _mesa_glsl_error(loc, state, "bit operations require GLSL 1.30");
532 return glsl_type::error_type;
533 }
534
535 /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
536 *
537 * "The shift operators (<<) and (>>). For both operators, the operands
538 * must be signed or unsigned integers or integer vectors. One operand
539 * can be signed while the other is unsigned."
540 */
541 if (!type_a->is_integer()) {
542 _mesa_glsl_error(loc, state, "LHS of operator %s must be an integer or "
543 "integer vector", ast_expression::operator_string(op));
544 return glsl_type::error_type;
545
546 }
547 if (!type_b->is_integer()) {
548 _mesa_glsl_error(loc, state, "RHS of operator %s must be an integer or "
549 "integer vector", ast_expression::operator_string(op));
550 return glsl_type::error_type;
551 }
552
553 /* "If the first operand is a scalar, the second operand has to be
554 * a scalar as well."
555 */
556 if (type_a->is_scalar() && !type_b->is_scalar()) {
557 _mesa_glsl_error(loc, state, "If the first operand of %s is scalar, the "
558 "second must be scalar as well",
559 ast_expression::operator_string(op));
560 return glsl_type::error_type;
561 }
562
563 /* If both operands are vectors, check that they have same number of
564 * elements.
565 */
566 if (type_a->is_vector() &&
567 type_b->is_vector() &&
568 type_a->vector_elements != type_b->vector_elements) {
569 _mesa_glsl_error(loc, state, "Vector operands to operator %s must "
570 "have same number of elements",
571 ast_expression::operator_string(op));
572 return glsl_type::error_type;
573 }
574
575 /* "In all cases, the resulting type will be the same type as the left
576 * operand."
577 */
578 return type_a;
579}
Ian Romanicka87ac252010-02-22 13:19:34 -0800580
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700581/**
582 * Validates that a value can be assigned to a location with a specified type
583 *
584 * Validates that \c rhs can be assigned to some location. If the types are
585 * not an exact match but an automatic conversion is possible, \c rhs will be
586 * converted.
587 *
588 * \return
589 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
590 * Otherwise the actual RHS to be assigned will be returned. This may be
591 * \c rhs, or it may be \c rhs after some type conversion.
592 *
593 * \note
594 * In addition to being used for assignments, this function is used to
595 * type-check return values.
596 */
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700597ir_rvalue *
Eric Anholt336b4ad2010-05-19 10:38:37 -0700598validate_assignment(struct _mesa_glsl_parse_state *state,
599 const glsl_type *lhs_type, ir_rvalue *rhs)
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700600{
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700601 /* If there is already some error in the RHS, just return it. Anything
602 * else will lead to an avalanche of error message back to the user.
603 */
Ian Romanickec530102010-12-10 15:47:11 -0800604 if (rhs->type->is_error())
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700605 return rhs;
606
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700607 /* If the types are identical, the assignment can trivially proceed.
608 */
Ian Romanickec530102010-12-10 15:47:11 -0800609 if (rhs->type == lhs_type)
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700610 return rhs;
611
Ian Romanick0157f412010-04-02 17:44:39 -0700612 /* If the array element types are the same and the size of the LHS is zero,
613 * the assignment is okay.
614 *
615 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
616 * is handled by ir_dereference::is_lvalue.
617 */
618 if (lhs_type->is_array() && rhs->type->is_array()
619 && (lhs_type->element_type() == rhs->type->element_type())
620 && (lhs_type->array_size() == 0)) {
621 return rhs;
622 }
623
Eric Anholt336b4ad2010-05-19 10:38:37 -0700624 /* Check for implicit conversion in GLSL 1.20 */
625 if (apply_implicit_conversion(lhs_type, rhs, state)) {
Ian Romanickec530102010-12-10 15:47:11 -0800626 if (rhs->type == lhs_type)
Eric Anholt336b4ad2010-05-19 10:38:37 -0700627 return rhs;
628 }
629
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700630 return NULL;
631}
632
Eric Anholt10a68522010-03-26 11:53:37 -0700633ir_rvalue *
634do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
635 ir_rvalue *lhs, ir_rvalue *rhs,
636 YYLTYPE lhs_loc)
637{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700638 void *ctx = state;
Eric Anholt10a68522010-03-26 11:53:37 -0700639 bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
640
641 if (!error_emitted) {
Eric Anholt10a68522010-03-26 11:53:37 -0700642 if (!lhs->is_lvalue()) {
643 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
644 error_emitted = true;
645 }
Kenneth Graunke10eaa8b2010-09-07 02:59:38 -0700646
647 if (state->es_shader && lhs->type->is_array()) {
648 _mesa_glsl_error(&lhs_loc, state, "whole array assignment is not "
649 "allowed in GLSL ES 1.00.");
650 error_emitted = true;
651 }
Eric Anholt10a68522010-03-26 11:53:37 -0700652 }
653
Eric Anholt336b4ad2010-05-19 10:38:37 -0700654 ir_rvalue *new_rhs = validate_assignment(state, lhs->type, rhs);
Eric Anholt10a68522010-03-26 11:53:37 -0700655 if (new_rhs == NULL) {
656 _mesa_glsl_error(& lhs_loc, state, "type mismatch");
657 } else {
658 rhs = new_rhs;
Ian Romanick0157f412010-04-02 17:44:39 -0700659
660 /* If the LHS array was not declared with a size, it takes it size from
661 * the RHS. If the LHS is an l-value and a whole array, it must be a
662 * dereference of a variable. Any other case would require that the LHS
663 * is either not an l-value or not a whole array.
664 */
665 if (lhs->type->array_size() == 0) {
666 ir_dereference *const d = lhs->as_dereference();
667
668 assert(d != NULL);
669
Ian Romanick36ea2862010-05-19 13:52:29 +0200670 ir_variable *const var = d->variable_referenced();
Ian Romanick0157f412010-04-02 17:44:39 -0700671
672 assert(var != NULL);
673
Ian Romanick63f39422010-04-05 14:35:47 -0700674 if (var->max_array_access >= unsigned(rhs->type->array_size())) {
675 /* FINISHME: This should actually log the location of the RHS. */
676 _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
677 "previous access",
678 var->max_array_access);
679 }
680
Ian Romanickf38d15b2010-07-20 15:33:40 -0700681 var->type = glsl_type::get_array_instance(lhs->type->element_type(),
Ian Romanick0157f412010-04-02 17:44:39 -0700682 rhs->type->array_size());
Eric Anholt9703ed02010-07-22 15:50:37 -0700683 d->type = var->type;
Ian Romanick0157f412010-04-02 17:44:39 -0700684 }
Eric Anholt10a68522010-03-26 11:53:37 -0700685 }
686
Eric Anholt2731a732010-06-23 14:51:14 -0700687 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
688 * but not post_inc) need the converted assigned value as an rvalue
689 * to handle things like:
690 *
691 * i = j += 1;
692 *
693 * So we always just store the computed value being assigned to a
694 * temporary and return a deref of that temporary. If the rvalue
695 * ends up not being used, the temp will get copy-propagated out.
696 */
Ian Romanick7e2aa912010-07-19 17:12:42 -0700697 ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp",
698 ir_var_temporary);
Eric Anholte33c1032010-06-24 15:13:03 -0700699 ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var);
Eric Anholtae805922010-06-24 09:06:12 -0700700 instructions->push_tail(var);
Eric Anholte33c1032010-06-24 15:13:03 -0700701 instructions->push_tail(new(ctx) ir_assignment(deref_var,
Carl Worth1660a292010-06-23 18:11:51 -0700702 rhs,
703 NULL));
Eric Anholte33c1032010-06-24 15:13:03 -0700704 deref_var = new(ctx) ir_dereference_variable(var);
Eric Anholt10a68522010-03-26 11:53:37 -0700705
Ian Romanick8e9ce2e2010-08-03 15:02:35 -0700706 if (!error_emitted)
707 instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var, NULL));
Eric Anholt2731a732010-06-23 14:51:14 -0700708
Carl Worth1660a292010-06-23 18:11:51 -0700709 return new(ctx) ir_dereference_variable(var);
Eric Anholt10a68522010-03-26 11:53:37 -0700710}
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700711
Eric Anholtde38f0e2010-03-26 12:14:54 -0700712static ir_rvalue *
Eric Anholt959a9ec2010-06-23 14:43:50 -0700713get_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
Eric Anholtde38f0e2010-03-26 12:14:54 -0700714{
Carl Worth1660a292010-06-23 18:11:51 -0700715 void *ctx = talloc_parent(lvalue);
Eric Anholtde38f0e2010-03-26 12:14:54 -0700716 ir_variable *var;
Eric Anholtde38f0e2010-03-26 12:14:54 -0700717
Ian Romanick7e2aa912010-07-19 17:12:42 -0700718 var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
719 ir_var_temporary);
Eric Anholt43b5b032010-07-07 14:04:30 -0700720 instructions->push_tail(var);
Eric Anholtde38f0e2010-03-26 12:14:54 -0700721 var->mode = ir_var_auto;
722
Carl Worth1660a292010-06-23 18:11:51 -0700723 instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
724 lvalue, NULL));
Eric Anholtde38f0e2010-03-26 12:14:54 -0700725
726 /* Once we've created this temporary, mark it read only so it's no
727 * longer considered an lvalue.
728 */
729 var->read_only = true;
730
Carl Worth1660a292010-06-23 18:11:51 -0700731 return new(ctx) ir_dereference_variable(var);
Eric Anholtde38f0e2010-03-26 12:14:54 -0700732}
733
734
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700735ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -0800736ast_node::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -0800737 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -0800738{
Ian Romanick18238de2010-03-01 13:49:10 -0800739 (void) instructions;
740 (void) state;
741
742 return NULL;
743}
744
Eric Anholtb4f58562010-12-01 15:55:53 -0800745static void
746mark_whole_array_access(ir_rvalue *access)
747{
748 ir_dereference_variable *deref = access->as_dereference_variable();
749
750 if (deref) {
751 deref->var->max_array_access = deref->type->length - 1;
752 }
753}
754
Eric Anholtff796332010-11-30 11:23:28 -0800755static ir_rvalue *
756do_comparison(void *mem_ctx, int operation, ir_rvalue *op0, ir_rvalue *op1)
757{
758 int join_op;
Ian Romanick6d36be52010-12-02 12:17:36 -0800759 ir_rvalue *cmp = NULL;
Eric Anholtff796332010-11-30 11:23:28 -0800760
761 if (operation == ir_binop_all_equal)
762 join_op = ir_binop_logic_and;
763 else
764 join_op = ir_binop_logic_or;
765
766 switch (op0->type->base_type) {
767 case GLSL_TYPE_FLOAT:
768 case GLSL_TYPE_UINT:
769 case GLSL_TYPE_INT:
770 case GLSL_TYPE_BOOL:
771 return new(mem_ctx) ir_expression(operation, op0, op1);
772
773 case GLSL_TYPE_ARRAY: {
Eric Anholtff796332010-11-30 11:23:28 -0800774 for (unsigned int i = 0; i < op0->type->length; i++) {
775 ir_rvalue *e0, *e1, *result;
776
777 e0 = new(mem_ctx) ir_dereference_array(op0->clone(mem_ctx, NULL),
778 new(mem_ctx) ir_constant(i));
779 e1 = new(mem_ctx) ir_dereference_array(op1->clone(mem_ctx, NULL),
780 new(mem_ctx) ir_constant(i));
781 result = do_comparison(mem_ctx, operation, e0, e1);
782
Ian Romanick6d36be52010-12-02 12:17:36 -0800783 if (cmp) {
784 cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
Eric Anholtff796332010-11-30 11:23:28 -0800785 } else {
Ian Romanick6d36be52010-12-02 12:17:36 -0800786 cmp = result;
Eric Anholtff796332010-11-30 11:23:28 -0800787 }
788 }
Eric Anholtb4f58562010-12-01 15:55:53 -0800789
790 mark_whole_array_access(op0);
791 mark_whole_array_access(op1);
Ian Romanick6d36be52010-12-02 12:17:36 -0800792 break;
Eric Anholtff796332010-11-30 11:23:28 -0800793 }
794
795 case GLSL_TYPE_STRUCT: {
Eric Anholtff796332010-11-30 11:23:28 -0800796 for (unsigned int i = 0; i < op0->type->length; i++) {
797 ir_rvalue *e0, *e1, *result;
798 const char *field_name = op0->type->fields.structure[i].name;
799
800 e0 = new(mem_ctx) ir_dereference_record(op0->clone(mem_ctx, NULL),
801 field_name);
802 e1 = new(mem_ctx) ir_dereference_record(op1->clone(mem_ctx, NULL),
803 field_name);
804 result = do_comparison(mem_ctx, operation, e0, e1);
805
Ian Romanick6d36be52010-12-02 12:17:36 -0800806 if (cmp) {
807 cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
Eric Anholtff796332010-11-30 11:23:28 -0800808 } else {
Ian Romanick6d36be52010-12-02 12:17:36 -0800809 cmp = result;
Eric Anholtff796332010-11-30 11:23:28 -0800810 }
811 }
Ian Romanick6d36be52010-12-02 12:17:36 -0800812 break;
Eric Anholtff796332010-11-30 11:23:28 -0800813 }
814
815 case GLSL_TYPE_ERROR:
816 case GLSL_TYPE_VOID:
817 case GLSL_TYPE_SAMPLER:
818 /* I assume a comparison of a struct containing a sampler just
819 * ignores the sampler present in the type.
820 */
Ian Romanick6d36be52010-12-02 12:17:36 -0800821 break;
822
823 default:
824 assert(!"Should not get here.");
825 break;
Eric Anholtff796332010-11-30 11:23:28 -0800826 }
Eric Anholtd56c9742010-11-30 13:28:47 -0800827
Ian Romanick6d36be52010-12-02 12:17:36 -0800828 if (cmp == NULL)
829 cmp = new(mem_ctx) ir_constant(true);
830
831 return cmp;
Eric Anholtff796332010-11-30 11:23:28 -0800832}
Ian Romanick18238de2010-03-01 13:49:10 -0800833
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700834ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -0800835ast_expression::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -0800836 struct _mesa_glsl_parse_state *state)
837{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700838 void *ctx = state;
Ian Romanicka87ac252010-02-22 13:19:34 -0800839 static const int operations[AST_NUM_OPERATORS] = {
840 -1, /* ast_assign doesn't convert to ir_expression. */
841 -1, /* ast_plus doesn't convert to ir_expression. */
842 ir_unop_neg,
843 ir_binop_add,
844 ir_binop_sub,
845 ir_binop_mul,
846 ir_binop_div,
847 ir_binop_mod,
848 ir_binop_lshift,
849 ir_binop_rshift,
850 ir_binop_less,
851 ir_binop_greater,
852 ir_binop_lequal,
853 ir_binop_gequal,
Luca Barbieri4dfb8992010-09-08 01:31:39 +0200854 ir_binop_all_equal,
855 ir_binop_any_nequal,
Ian Romanicka87ac252010-02-22 13:19:34 -0800856 ir_binop_bit_and,
857 ir_binop_bit_xor,
858 ir_binop_bit_or,
859 ir_unop_bit_not,
860 ir_binop_logic_and,
861 ir_binop_logic_xor,
862 ir_binop_logic_or,
863 ir_unop_logic_not,
864
865 /* Note: The following block of expression types actually convert
866 * to multiple IR instructions.
867 */
868 ir_binop_mul, /* ast_mul_assign */
869 ir_binop_div, /* ast_div_assign */
870 ir_binop_mod, /* ast_mod_assign */
871 ir_binop_add, /* ast_add_assign */
872 ir_binop_sub, /* ast_sub_assign */
873 ir_binop_lshift, /* ast_ls_assign */
874 ir_binop_rshift, /* ast_rs_assign */
875 ir_binop_bit_and, /* ast_and_assign */
876 ir_binop_bit_xor, /* ast_xor_assign */
877 ir_binop_bit_or, /* ast_or_assign */
878
879 -1, /* ast_conditional doesn't convert to ir_expression. */
Eric Anholtde38f0e2010-03-26 12:14:54 -0700880 ir_binop_add, /* ast_pre_inc. */
881 ir_binop_sub, /* ast_pre_dec. */
882 ir_binop_add, /* ast_post_inc. */
883 ir_binop_sub, /* ast_post_dec. */
Ian Romanicka87ac252010-02-22 13:19:34 -0800884 -1, /* ast_field_selection doesn't conv to ir_expression. */
885 -1, /* ast_array_index doesn't convert to ir_expression. */
886 -1, /* ast_function_call doesn't conv to ir_expression. */
887 -1, /* ast_identifier doesn't convert to ir_expression. */
888 -1, /* ast_int_constant doesn't convert to ir_expression. */
889 -1, /* ast_uint_constant doesn't conv to ir_expression. */
890 -1, /* ast_float_constant doesn't conv to ir_expression. */
891 -1, /* ast_bool_constant doesn't conv to ir_expression. */
892 -1, /* ast_sequence doesn't convert to ir_expression. */
893 };
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700894 ir_rvalue *result = NULL;
Aras Pranckevicius1c325af2010-07-29 15:35:22 +0300895 ir_rvalue *op[3];
Ian Romanick0471e8b2010-03-26 14:33:41 -0700896 const struct glsl_type *type = glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800897 bool error_emitted = false;
898 YYLTYPE loc;
899
Ian Romanick18238de2010-03-01 13:49:10 -0800900 loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -0800901
Ian Romanick18238de2010-03-01 13:49:10 -0800902 switch (this->oper) {
Ian Romanick6652af32010-03-09 16:38:02 -0800903 case ast_assign: {
Ian Romanick18238de2010-03-01 13:49:10 -0800904 op[0] = this->subexpressions[0]->hir(instructions, state);
905 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800906
Eric Anholt10a68522010-03-26 11:53:37 -0700907 result = do_assignment(instructions, state, op[0], op[1],
908 this->subexpressions[0]->get_location());
909 error_emitted = result->type->is_error();
910 type = result->type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800911 break;
Ian Romanick6652af32010-03-09 16:38:02 -0800912 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800913
914 case ast_plus:
Ian Romanick18238de2010-03-01 13:49:10 -0800915 op[0] = this->subexpressions[0]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800916
Carl Worthc24bcad2010-07-21 11:23:51 -0700917 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
918
919 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -0800920
921 result = op[0];
922 break;
923
924 case ast_neg:
Ian Romanick18238de2010-03-01 13:49:10 -0800925 op[0] = this->subexpressions[0]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800926
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000927 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -0800928
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000929 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -0800930
Carl Worth1660a292010-06-23 18:11:51 -0700931 result = new(ctx) ir_expression(operations[this->oper], type,
932 op[0], NULL);
Ian Romanicka87ac252010-02-22 13:19:34 -0800933 break;
934
935 case ast_add:
936 case ast_sub:
937 case ast_mul:
938 case ast_div:
Ian Romanick18238de2010-03-01 13:49:10 -0800939 op[0] = this->subexpressions[0]->hir(instructions, state);
940 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800941
Ian Romanickbfb09c22010-03-29 16:32:55 -0700942 type = arithmetic_result_type(op[0], op[1],
Ian Romanick18238de2010-03-01 13:49:10 -0800943 (this->oper == ast_mul),
Eric Anholta13bb142010-03-31 16:38:11 -1000944 state, & loc);
945 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -0800946
Carl Worth1660a292010-06-23 18:11:51 -0700947 result = new(ctx) ir_expression(operations[this->oper], type,
948 op[0], op[1]);
Ian Romanicka87ac252010-02-22 13:19:34 -0800949 break;
950
951 case ast_mod:
Ian Romanick18238de2010-03-01 13:49:10 -0800952 op[0] = this->subexpressions[0]->hir(instructions, state);
953 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800954
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000955 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -0800956
Ian Romanick18238de2010-03-01 13:49:10 -0800957 assert(operations[this->oper] == ir_binop_mod);
Ian Romanicka87ac252010-02-22 13:19:34 -0800958
Carl Worth1660a292010-06-23 18:11:51 -0700959 result = new(ctx) ir_expression(operations[this->oper], type,
960 op[0], op[1]);
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000961 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -0800962 break;
963
964 case ast_lshift:
965 case ast_rshift:
Chad Versace5c4c36f2010-10-08 16:22:28 -0700966 if (state->language_version < 130) {
967 _mesa_glsl_error(&loc, state, "operator %s requires GLSL 1.30",
968 operator_string(this->oper));
969 error_emitted = true;
Chad Versace5c4c36f2010-10-08 16:22:28 -0700970 }
971
Chad Versace5c4c36f2010-10-08 16:22:28 -0700972 op[0] = this->subexpressions[0]->hir(instructions, state);
973 op[1] = this->subexpressions[1]->hir(instructions, state);
Chad Versacec0197ab2010-10-15 09:49:46 -0700974 type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
975 &loc);
Chad Versace5c4c36f2010-10-08 16:22:28 -0700976 result = new(ctx) ir_expression(operations[this->oper], type,
977 op[0], op[1]);
978 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
979 break;
Ian Romanicka87ac252010-02-22 13:19:34 -0800980
981 case ast_less:
982 case ast_greater:
983 case ast_lequal:
984 case ast_gequal:
Ian Romanick18238de2010-03-01 13:49:10 -0800985 op[0] = this->subexpressions[0]->hir(instructions, state);
986 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800987
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000988 type = relational_result_type(op[0], op[1], state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -0800989
990 /* The relational operators must either generate an error or result
991 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
992 */
Ian Romanicka43817a2010-03-26 14:27:23 -0700993 assert(type->is_error()
Ian Romanicka87ac252010-02-22 13:19:34 -0800994 || ((type->base_type == GLSL_TYPE_BOOL)
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800995 && type->is_scalar()));
Ian Romanicka87ac252010-02-22 13:19:34 -0800996
Carl Worth1660a292010-06-23 18:11:51 -0700997 result = new(ctx) ir_expression(operations[this->oper], type,
998 op[0], op[1]);
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000999 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -08001000 break;
1001
1002 case ast_nequal:
1003 case ast_equal:
Ian Romanick6e659ca2010-03-29 15:11:05 -07001004 op[0] = this->subexpressions[0]->hir(instructions, state);
1005 op[1] = this->subexpressions[1]->hir(instructions, state);
1006
1007 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1008 *
1009 * "The equality operators equal (==), and not equal (!=)
1010 * operate on all types. They result in a scalar Boolean. If
1011 * the operand types do not match, then there must be a
1012 * conversion from Section 4.1.10 "Implicit Conversions"
1013 * applied to one operand that can make them match, in which
1014 * case this conversion is done."
1015 */
Ian Romanickbfb09c22010-03-29 16:32:55 -07001016 if ((!apply_implicit_conversion(op[0]->type, op[1], state)
1017 && !apply_implicit_conversion(op[1]->type, op[0], state))
Ian Romanick212b0322010-03-29 16:22:38 -07001018 || (op[0]->type != op[1]->type)) {
Ian Romanick6e659ca2010-03-29 15:11:05 -07001019 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
1020 "type", (this->oper == ast_equal) ? "==" : "!=");
1021 error_emitted = true;
Ian Romanicka80cbd62010-03-30 17:04:48 -07001022 } else if ((state->language_version <= 110)
1023 && (op[0]->type->is_array() || op[1]->type->is_array())) {
1024 _mesa_glsl_error(& loc, state, "array comparisons forbidden in "
1025 "GLSL 1.10");
1026 error_emitted = true;
Ian Romanick6e659ca2010-03-29 15:11:05 -07001027 }
1028
Eric Anholtff796332010-11-30 11:23:28 -08001029 result = do_comparison(ctx, operations[this->oper], op[0], op[1]);
Ian Romanick6e659ca2010-03-29 15:11:05 -07001030 type = glsl_type::bool_type;
1031
Ian Romanick6d36be52010-12-02 12:17:36 -08001032 assert(error_emitted || (result->type == glsl_type::bool_type));
Ian Romanicka87ac252010-02-22 13:19:34 -08001033 break;
1034
1035 case ast_bit_and:
1036 case ast_bit_xor:
1037 case ast_bit_or:
Kenneth Graunke1eea9632010-08-31 10:56:24 -07001038 op[0] = this->subexpressions[0]->hir(instructions, state);
1039 op[1] = this->subexpressions[1]->hir(instructions, state);
Chad Versacecfdbf8b2010-10-15 11:28:05 -07001040 type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
1041 state, &loc);
Kenneth Graunke1eea9632010-08-31 10:56:24 -07001042 result = new(ctx) ir_expression(operations[this->oper], type,
1043 op[0], op[1]);
1044 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1045 break;
1046
Ian Romanicka87ac252010-02-22 13:19:34 -08001047 case ast_bit_not:
Kenneth Graunke1eea9632010-08-31 10:56:24 -07001048 op[0] = this->subexpressions[0]->hir(instructions, state);
1049
1050 if (state->language_version < 130) {
1051 _mesa_glsl_error(&loc, state, "bit-wise operations require GLSL 1.30");
1052 error_emitted = true;
1053 }
1054
1055 if (!op[0]->type->is_integer()) {
1056 _mesa_glsl_error(&loc, state, "operand of `~' must be an integer");
1057 error_emitted = true;
1058 }
1059
1060 type = op[0]->type;
1061 result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL);
Ian Romanicka87ac252010-02-22 13:19:34 -08001062 break;
1063
Eric Anholt4950a682010-04-16 01:10:32 -07001064 case ast_logic_and: {
Eric Anholtb82c0c32010-03-31 09:11:39 -10001065 op[0] = this->subexpressions[0]->hir(instructions, state);
Eric Anholtb82c0c32010-03-31 09:11:39 -10001066
1067 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
1068 YYLTYPE loc = this->subexpressions[0]->get_location();
1069
1070 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
1071 operator_string(this->oper));
Eric Anholtebbf14b2010-03-31 17:05:32 -10001072 error_emitted = true;
Eric Anholtb82c0c32010-03-31 09:11:39 -10001073 }
1074
Eric Anholt44b694e2010-04-16 13:49:04 -07001075 ir_constant *op0_const = op[0]->constant_expression_value();
1076 if (op0_const) {
1077 if (op0_const->value.b[0]) {
1078 op[1] = this->subexpressions[1]->hir(instructions, state);
Eric Anholt4950a682010-04-16 01:10:32 -07001079
Eric Anholt44b694e2010-04-16 13:49:04 -07001080 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
1081 YYLTYPE loc = this->subexpressions[1]->get_location();
Eric Anholt4950a682010-04-16 01:10:32 -07001082
Eric Anholt44b694e2010-04-16 13:49:04 -07001083 _mesa_glsl_error(& loc, state,
1084 "RHS of `%s' must be scalar boolean",
1085 operator_string(this->oper));
1086 error_emitted = true;
1087 }
1088 result = op[1];
1089 } else {
1090 result = op0_const;
1091 }
1092 type = glsl_type::bool_type;
1093 } else {
Ian Romanick81d664f2010-07-12 15:18:55 -07001094 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
Ian Romanick7e2aa912010-07-19 17:12:42 -07001095 "and_tmp",
1096 ir_var_temporary);
Ian Romanick81d664f2010-07-12 15:18:55 -07001097 instructions->push_tail(tmp);
1098
Carl Worth1660a292010-06-23 18:11:51 -07001099 ir_if *const stmt = new(ctx) ir_if(op[0]);
Eric Anholt44b694e2010-04-16 13:49:04 -07001100 instructions->push_tail(stmt);
Eric Anholt4950a682010-04-16 01:10:32 -07001101
Eric Anholt44b694e2010-04-16 13:49:04 -07001102 op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state);
Eric Anholtb82c0c32010-03-31 09:11:39 -10001103
Eric Anholt44b694e2010-04-16 13:49:04 -07001104 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
1105 YYLTYPE loc = this->subexpressions[1]->get_location();
1106
1107 _mesa_glsl_error(& loc, state,
1108 "RHS of `%s' must be scalar boolean",
1109 operator_string(this->oper));
1110 error_emitted = true;
1111 }
1112
Carl Worth1660a292010-06-23 18:11:51 -07001113 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -07001114 ir_assignment *const then_assign =
Carl Worth1660a292010-06-23 18:11:51 -07001115 new(ctx) ir_assignment(then_deref, op[1], NULL);
Eric Anholt44b694e2010-04-16 13:49:04 -07001116 stmt->then_instructions.push_tail(then_assign);
1117
Carl Worth1660a292010-06-23 18:11:51 -07001118 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -07001119 ir_assignment *const else_assign =
Carl Worth1660a292010-06-23 18:11:51 -07001120 new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false), NULL);
Eric Anholt44b694e2010-04-16 13:49:04 -07001121 stmt->else_instructions.push_tail(else_assign);
1122
Carl Worth1660a292010-06-23 18:11:51 -07001123 result = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -07001124 type = tmp->type;
Eric Anholtb82c0c32010-03-31 09:11:39 -10001125 }
Eric Anholt4950a682010-04-16 01:10:32 -07001126 break;
1127 }
1128
1129 case ast_logic_or: {
1130 op[0] = this->subexpressions[0]->hir(instructions, state);
1131
1132 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
1133 YYLTYPE loc = this->subexpressions[0]->get_location();
1134
1135 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
1136 operator_string(this->oper));
1137 error_emitted = true;
1138 }
1139
Eric Anholt44b694e2010-04-16 13:49:04 -07001140 ir_constant *op0_const = op[0]->constant_expression_value();
1141 if (op0_const) {
1142 if (op0_const->value.b[0]) {
1143 result = op0_const;
1144 } else {
1145 op[1] = this->subexpressions[1]->hir(instructions, state);
Eric Anholt4950a682010-04-16 01:10:32 -07001146
Eric Anholt44b694e2010-04-16 13:49:04 -07001147 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
1148 YYLTYPE loc = this->subexpressions[1]->get_location();
Eric Anholt4950a682010-04-16 01:10:32 -07001149
Eric Anholt44b694e2010-04-16 13:49:04 -07001150 _mesa_glsl_error(& loc, state,
1151 "RHS of `%s' must be scalar boolean",
1152 operator_string(this->oper));
1153 error_emitted = true;
1154 }
1155 result = op[1];
1156 }
1157 type = glsl_type::bool_type;
1158 } else {
Kenneth Graunkedfd30ca2010-07-08 12:40:52 -07001159 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
Ian Romanick7e2aa912010-07-19 17:12:42 -07001160 "or_tmp",
1161 ir_var_temporary);
Ian Romanick0b9ae3b2010-07-12 14:22:05 -07001162 instructions->push_tail(tmp);
Eric Anholt4950a682010-04-16 01:10:32 -07001163
Ian Romanick81d664f2010-07-12 15:18:55 -07001164 ir_if *const stmt = new(ctx) ir_if(op[0]);
1165 instructions->push_tail(stmt);
1166
Eric Anholta0879b92010-07-22 16:30:41 -07001167 op[1] = this->subexpressions[1]->hir(&stmt->else_instructions, state);
Eric Anholt44b694e2010-04-16 13:49:04 -07001168
1169 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
1170 YYLTYPE loc = this->subexpressions[1]->get_location();
1171
1172 _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean",
1173 operator_string(this->oper));
1174 error_emitted = true;
1175 }
1176
Carl Worth1660a292010-06-23 18:11:51 -07001177 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -07001178 ir_assignment *const then_assign =
Carl Worth1660a292010-06-23 18:11:51 -07001179 new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true), NULL);
Eric Anholt44b694e2010-04-16 13:49:04 -07001180 stmt->then_instructions.push_tail(then_assign);
1181
Carl Worth1660a292010-06-23 18:11:51 -07001182 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -07001183 ir_assignment *const else_assign =
Carl Worth1660a292010-06-23 18:11:51 -07001184 new(ctx) ir_assignment(else_deref, op[1], NULL);
Eric Anholt44b694e2010-04-16 13:49:04 -07001185 stmt->else_instructions.push_tail(else_assign);
1186
Carl Worth1660a292010-06-23 18:11:51 -07001187 result = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -07001188 type = tmp->type;
Eric Anholt4950a682010-04-16 01:10:32 -07001189 }
Eric Anholt4950a682010-04-16 01:10:32 -07001190 break;
1191 }
1192
1193 case ast_logic_xor:
1194 op[0] = this->subexpressions[0]->hir(instructions, state);
1195 op[1] = this->subexpressions[1]->hir(instructions, state);
1196
1197
Carl Worth1660a292010-06-23 18:11:51 -07001198 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
1199 op[0], op[1]);
Eric Anholtebbf14b2010-03-31 17:05:32 -10001200 type = glsl_type::bool_type;
Ian Romanicka87ac252010-02-22 13:19:34 -08001201 break;
1202
Eric Anholta5827fe2010-03-31 16:50:55 -10001203 case ast_logic_not:
1204 op[0] = this->subexpressions[0]->hir(instructions, state);
1205
1206 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
1207 YYLTYPE loc = this->subexpressions[0]->get_location();
1208
1209 _mesa_glsl_error(& loc, state,
1210 "operand of `!' must be scalar boolean");
Eric Anholtebbf14b2010-03-31 17:05:32 -10001211 error_emitted = true;
Eric Anholta5827fe2010-03-31 16:50:55 -10001212 }
1213
Carl Worth1660a292010-06-23 18:11:51 -07001214 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
1215 op[0], NULL);
Eric Anholtebbf14b2010-03-31 17:05:32 -10001216 type = glsl_type::bool_type;
Eric Anholta5827fe2010-03-31 16:50:55 -10001217 break;
1218
Ian Romanicka87ac252010-02-22 13:19:34 -08001219 case ast_mul_assign:
1220 case ast_div_assign:
1221 case ast_add_assign:
1222 case ast_sub_assign: {
Ian Romanick18238de2010-03-01 13:49:10 -08001223 op[0] = this->subexpressions[0]->hir(instructions, state);
1224 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001225
Ian Romanickbfb09c22010-03-29 16:32:55 -07001226 type = arithmetic_result_type(op[0], op[1],
Ian Romanick18238de2010-03-01 13:49:10 -08001227 (this->oper == ast_mul_assign),
Eric Anholta13bb142010-03-31 16:38:11 -10001228 state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -08001229
Carl Worth1660a292010-06-23 18:11:51 -07001230 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1231 op[0], op[1]);
Ian Romanicka87ac252010-02-22 13:19:34 -08001232
Eric Anholt3e24ef62010-06-23 12:40:17 -07001233 result = do_assignment(instructions, state,
Eric Anholt8273bd42010-08-04 12:34:56 -07001234 op[0]->clone(ctx, NULL), temp_rhs,
Eric Anholt10a68522010-03-26 11:53:37 -07001235 this->subexpressions[0]->get_location());
1236 type = result->type;
1237 error_emitted = (op[0]->type->is_error());
Ian Romanicka87ac252010-02-22 13:19:34 -08001238
1239 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1240 * explicitly test for this because none of the binary expression
1241 * operators allow array operands either.
1242 */
1243
Ian Romanicka87ac252010-02-22 13:19:34 -08001244 break;
1245 }
1246
Eric Anholt48a0e642010-03-26 11:57:46 -07001247 case ast_mod_assign: {
1248 op[0] = this->subexpressions[0]->hir(instructions, state);
1249 op[1] = this->subexpressions[1]->hir(instructions, state);
1250
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001251 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
Eric Anholt48a0e642010-03-26 11:57:46 -07001252
1253 assert(operations[this->oper] == ir_binop_mod);
1254
Ian Romanick768b55a2010-08-13 16:46:43 -07001255 ir_rvalue *temp_rhs;
Carl Worth1660a292010-06-23 18:11:51 -07001256 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1257 op[0], op[1]);
Eric Anholt48a0e642010-03-26 11:57:46 -07001258
Eric Anholt3e24ef62010-06-23 12:40:17 -07001259 result = do_assignment(instructions, state,
Eric Anholt8273bd42010-08-04 12:34:56 -07001260 op[0]->clone(ctx, NULL), temp_rhs,
Eric Anholt48a0e642010-03-26 11:57:46 -07001261 this->subexpressions[0]->get_location());
1262 type = result->type;
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001263 error_emitted = type->is_error();
Eric Anholt48a0e642010-03-26 11:57:46 -07001264 break;
1265 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001266
1267 case ast_ls_assign:
Chad Versace338ed6e2010-10-15 10:05:50 -07001268 case ast_rs_assign: {
1269 op[0] = this->subexpressions[0]->hir(instructions, state);
1270 op[1] = this->subexpressions[1]->hir(instructions, state);
1271 type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1272 &loc);
1273 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1274 type, op[0], op[1]);
1275 result = do_assignment(instructions, state, op[0]->clone(ctx, NULL),
1276 temp_rhs,
1277 this->subexpressions[0]->get_location());
1278 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
Ian Romanick251eb752010-03-29 14:15:05 -07001279 break;
Chad Versace338ed6e2010-10-15 10:05:50 -07001280 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001281
1282 case ast_and_assign:
1283 case ast_xor_assign:
Chad Versaced03ac0f2010-10-15 12:08:28 -07001284 case ast_or_assign: {
1285 op[0] = this->subexpressions[0]->hir(instructions, state);
1286 op[1] = this->subexpressions[1]->hir(instructions, state);
1287 type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
1288 state, &loc);
1289 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1290 type, op[0], op[1]);
1291 result = do_assignment(instructions, state, op[0]->clone(ctx, NULL),
1292 temp_rhs,
1293 this->subexpressions[0]->get_location());
1294 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
Ian Romanick251eb752010-03-29 14:15:05 -07001295 break;
Chad Versaced03ac0f2010-10-15 12:08:28 -07001296 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001297
Ian Romanick96f9cea2010-03-29 15:33:54 -07001298 case ast_conditional: {
1299 op[0] = this->subexpressions[0]->hir(instructions, state);
1300
1301 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1302 *
1303 * "The ternary selection operator (?:). It operates on three
1304 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1305 * first expression, which must result in a scalar Boolean."
1306 */
1307 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
1308 YYLTYPE loc = this->subexpressions[0]->get_location();
1309
1310 _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean");
1311 error_emitted = true;
1312 }
1313
1314 /* The :? operator is implemented by generating an anonymous temporary
1315 * followed by an if-statement. The last instruction in each branch of
1316 * the if-statement assigns a value to the anonymous temporary. This
1317 * temporary is the r-value of the expression.
1318 */
Ian Romanick0ad76c62010-06-11 12:56:26 -07001319 exec_list then_instructions;
1320 exec_list else_instructions;
Ian Romanick96f9cea2010-03-29 15:33:54 -07001321
Ian Romanick0ad76c62010-06-11 12:56:26 -07001322 op[1] = this->subexpressions[1]->hir(&then_instructions, state);
1323 op[2] = this->subexpressions[2]->hir(&else_instructions, state);
Ian Romanick96f9cea2010-03-29 15:33:54 -07001324
1325 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1326 *
1327 * "The second and third expressions can be any type, as
1328 * long their types match, or there is a conversion in
1329 * Section 4.1.10 "Implicit Conversions" that can be applied
1330 * to one of the expressions to make their types match. This
1331 * resulting matching type is the type of the entire
1332 * expression."
1333 */
Ian Romanickbfb09c22010-03-29 16:32:55 -07001334 if ((!apply_implicit_conversion(op[1]->type, op[2], state)
1335 && !apply_implicit_conversion(op[2]->type, op[1], state))
Ian Romanickdb9be2e2010-03-29 16:25:56 -07001336 || (op[1]->type != op[2]->type)) {
Ian Romanick96f9cea2010-03-29 15:33:54 -07001337 YYLTYPE loc = this->subexpressions[1]->get_location();
1338
1339 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
1340 "operator must have matching types.");
1341 error_emitted = true;
Ian Romanick0ad76c62010-06-11 12:56:26 -07001342 type = glsl_type::error_type;
Ian Romanickdb9be2e2010-03-29 16:25:56 -07001343 } else {
Ian Romanick0ad76c62010-06-11 12:56:26 -07001344 type = op[1]->type;
Ian Romanick96f9cea2010-03-29 15:33:54 -07001345 }
1346
Ian Romanickf09fabc2010-09-07 14:30:06 -07001347 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1348 *
1349 * "The second and third expressions must be the same type, but can
1350 * be of any type other than an array."
1351 */
1352 if ((state->language_version <= 110) && type->is_array()) {
1353 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
1354 "operator must not be arrays.");
1355 error_emitted = true;
1356 }
1357
Ian Romanick7825d3d2010-06-11 13:45:51 -07001358 ir_constant *cond_val = op[0]->constant_expression_value();
1359 ir_constant *then_val = op[1]->constant_expression_value();
1360 ir_constant *else_val = op[2]->constant_expression_value();
Ian Romanick0ad76c62010-06-11 12:56:26 -07001361
Ian Romanick7825d3d2010-06-11 13:45:51 -07001362 if (then_instructions.is_empty()
1363 && else_instructions.is_empty()
1364 && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) {
1365 result = (cond_val->value.b[0]) ? then_val : else_val;
1366 } else {
Ian Romanick7e2aa912010-07-19 17:12:42 -07001367 ir_variable *const tmp =
1368 new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
Ian Romanick0b9ae3b2010-07-12 14:22:05 -07001369 instructions->push_tail(tmp);
Ian Romanick0ad76c62010-06-11 12:56:26 -07001370
Carl Worth1660a292010-06-23 18:11:51 -07001371 ir_if *const stmt = new(ctx) ir_if(op[0]);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001372 instructions->push_tail(stmt);
Ian Romanick0ad76c62010-06-11 12:56:26 -07001373
Ian Romanick7825d3d2010-06-11 13:45:51 -07001374 then_instructions.move_nodes_to(& stmt->then_instructions);
Carl Worth1660a292010-06-23 18:11:51 -07001375 ir_dereference *const then_deref =
1376 new(ctx) ir_dereference_variable(tmp);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001377 ir_assignment *const then_assign =
Carl Worth1660a292010-06-23 18:11:51 -07001378 new(ctx) ir_assignment(then_deref, op[1], NULL);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001379 stmt->then_instructions.push_tail(then_assign);
Ian Romanick0ad76c62010-06-11 12:56:26 -07001380
Ian Romanick7825d3d2010-06-11 13:45:51 -07001381 else_instructions.move_nodes_to(& stmt->else_instructions);
Carl Worth1660a292010-06-23 18:11:51 -07001382 ir_dereference *const else_deref =
1383 new(ctx) ir_dereference_variable(tmp);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001384 ir_assignment *const else_assign =
Carl Worth1660a292010-06-23 18:11:51 -07001385 new(ctx) ir_assignment(else_deref, op[2], NULL);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001386 stmt->else_instructions.push_tail(else_assign);
1387
Carl Worth1660a292010-06-23 18:11:51 -07001388 result = new(ctx) ir_dereference_variable(tmp);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001389 }
Ian Romanick251eb752010-03-29 14:15:05 -07001390 break;
Ian Romanick96f9cea2010-03-29 15:33:54 -07001391 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001392
1393 case ast_pre_inc:
Eric Anholt76ea56c2010-03-26 12:16:54 -07001394 case ast_pre_dec: {
1395 op[0] = this->subexpressions[0]->hir(instructions, state);
1396 if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
Carl Worth1660a292010-06-23 18:11:51 -07001397 op[1] = new(ctx) ir_constant(1.0f);
Eric Anholt76ea56c2010-03-26 12:16:54 -07001398 else
Carl Worth1660a292010-06-23 18:11:51 -07001399 op[1] = new(ctx) ir_constant(1);
Eric Anholt76ea56c2010-03-26 12:16:54 -07001400
Eric Anholta13bb142010-03-31 16:38:11 -10001401 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
Eric Anholt76ea56c2010-03-26 12:16:54 -07001402
Ian Romanick768b55a2010-08-13 16:46:43 -07001403 ir_rvalue *temp_rhs;
Carl Worth1660a292010-06-23 18:11:51 -07001404 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1405 op[0], op[1]);
Eric Anholt76ea56c2010-03-26 12:16:54 -07001406
Eric Anholt3e24ef62010-06-23 12:40:17 -07001407 result = do_assignment(instructions, state,
Eric Anholt8273bd42010-08-04 12:34:56 -07001408 op[0]->clone(ctx, NULL), temp_rhs,
Eric Anholt76ea56c2010-03-26 12:16:54 -07001409 this->subexpressions[0]->get_location());
1410 type = result->type;
1411 error_emitted = op[0]->type->is_error();
1412 break;
1413 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001414
1415 case ast_post_inc:
Eric Anholtde38f0e2010-03-26 12:14:54 -07001416 case ast_post_dec: {
1417 op[0] = this->subexpressions[0]->hir(instructions, state);
1418 if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
Carl Worth1660a292010-06-23 18:11:51 -07001419 op[1] = new(ctx) ir_constant(1.0f);
Eric Anholtde38f0e2010-03-26 12:14:54 -07001420 else
Carl Worth1660a292010-06-23 18:11:51 -07001421 op[1] = new(ctx) ir_constant(1);
Eric Anholtde38f0e2010-03-26 12:14:54 -07001422
1423 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1424
Eric Anholta13bb142010-03-31 16:38:11 -10001425 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
Eric Anholtde38f0e2010-03-26 12:14:54 -07001426
Ian Romanick768b55a2010-08-13 16:46:43 -07001427 ir_rvalue *temp_rhs;
Carl Worth1660a292010-06-23 18:11:51 -07001428 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1429 op[0], op[1]);
Eric Anholtde38f0e2010-03-26 12:14:54 -07001430
1431 /* Get a temporary of a copy of the lvalue before it's modified.
1432 * This may get thrown away later.
1433 */
Eric Anholt8273bd42010-08-04 12:34:56 -07001434 result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
Eric Anholtde38f0e2010-03-26 12:14:54 -07001435
Eric Anholt3e24ef62010-06-23 12:40:17 -07001436 (void)do_assignment(instructions, state,
Eric Anholt8273bd42010-08-04 12:34:56 -07001437 op[0]->clone(ctx, NULL), temp_rhs,
Eric Anholtde38f0e2010-03-26 12:14:54 -07001438 this->subexpressions[0]->get_location());
1439
1440 type = result->type;
1441 error_emitted = op[0]->type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -08001442 break;
Eric Anholtde38f0e2010-03-26 12:14:54 -07001443 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001444
1445 case ast_field_selection:
Ian Romanick18238de2010-03-01 13:49:10 -08001446 result = _mesa_ast_field_selection_to_hir(this, instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001447 type = result->type;
1448 break;
1449
Ian Romanick27e3cf82010-04-01 18:03:59 -07001450 case ast_array_index: {
1451 YYLTYPE index_loc = subexpressions[1]->get_location();
1452
1453 op[0] = subexpressions[0]->hir(instructions, state);
1454 op[1] = subexpressions[1]->hir(instructions, state);
1455
1456 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1457
Ian Romanicka9159f92010-05-26 15:08:11 -07001458 ir_rvalue *const array = op[0];
Ian Romanickb8a21cc2010-04-01 18:31:11 -07001459
Carl Worth1660a292010-06-23 18:11:51 -07001460 result = new(ctx) ir_dereference_array(op[0], op[1]);
Ian Romanickb8a21cc2010-04-01 18:31:11 -07001461
1462 /* Do not use op[0] after this point. Use array.
1463 */
1464 op[0] = NULL;
1465
Ian Romanick27e3cf82010-04-01 18:03:59 -07001466
1467 if (error_emitted)
1468 break;
1469
Ian Romanick63038e12010-04-05 13:16:00 -07001470 if (!array->type->is_array()
1471 && !array->type->is_matrix()
1472 && !array->type->is_vector()) {
Ian Romanick27e3cf82010-04-01 18:03:59 -07001473 _mesa_glsl_error(& index_loc, state,
Ian Romanick63038e12010-04-05 13:16:00 -07001474 "cannot dereference non-array / non-matrix / "
1475 "non-vector");
Ian Romanick27e3cf82010-04-01 18:03:59 -07001476 error_emitted = true;
1477 }
1478
1479 if (!op[1]->type->is_integer()) {
1480 _mesa_glsl_error(& index_loc, state,
1481 "array index must be integer type");
1482 error_emitted = true;
1483 } else if (!op[1]->type->is_scalar()) {
1484 _mesa_glsl_error(& index_loc, state,
1485 "array index must be scalar");
1486 error_emitted = true;
1487 }
1488
1489 /* If the array index is a constant expression and the array has a
1490 * declared size, ensure that the access is in-bounds. If the array
1491 * index is not a constant expression, ensure that the array has a
1492 * declared size.
1493 */
1494 ir_constant *const const_index = op[1]->constant_expression_value();
1495 if (const_index != NULL) {
1496 const int idx = const_index->value.i[0];
Ian Romanick63038e12010-04-05 13:16:00 -07001497 const char *type_name;
1498 unsigned bound = 0;
1499
1500 if (array->type->is_matrix()) {
1501 type_name = "matrix";
1502 } else if (array->type->is_vector()) {
1503 type_name = "vector";
1504 } else {
1505 type_name = "array";
1506 }
Ian Romanick27e3cf82010-04-01 18:03:59 -07001507
1508 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
1509 *
1510 * "It is illegal to declare an array with a size, and then
1511 * later (in the same shader) index the same array with an
1512 * integral constant expression greater than or equal to the
1513 * declared size. It is also illegal to index an array with a
1514 * negative constant expression."
1515 */
Ian Romanick63038e12010-04-05 13:16:00 -07001516 if (array->type->is_matrix()) {
1517 if (array->type->row_type()->vector_elements <= idx) {
1518 bound = array->type->row_type()->vector_elements;
1519 }
1520 } else if (array->type->is_vector()) {
1521 if (array->type->vector_elements <= idx) {
1522 bound = array->type->vector_elements;
1523 }
1524 } else {
1525 if ((array->type->array_size() > 0)
1526 && (array->type->array_size() <= idx)) {
1527 bound = array->type->array_size();
1528 }
1529 }
1530
1531 if (bound > 0) {
1532 _mesa_glsl_error(& loc, state, "%s index must be < %u",
1533 type_name, bound);
1534 error_emitted = true;
1535 } else if (idx < 0) {
1536 _mesa_glsl_error(& loc, state, "%s index must be >= 0",
1537 type_name);
Ian Romanick27e3cf82010-04-01 18:03:59 -07001538 error_emitted = true;
1539 }
1540
Ian Romanick63038e12010-04-05 13:16:00 -07001541 if (array->type->is_array()) {
Ian Romanicka9159f92010-05-26 15:08:11 -07001542 /* If the array is a variable dereference, it dereferences the
1543 * whole array, by definition. Use this to get the variable.
1544 *
1545 * FINISHME: Should some methods for getting / setting / testing
1546 * FINISHME: array access limits be added to ir_dereference?
1547 */
1548 ir_variable *const v = array->whole_variable_referenced();
Ian Romanick63038e12010-04-05 13:16:00 -07001549 if ((v != NULL) && (unsigned(idx) > v->max_array_access))
1550 v->max_array_access = idx;
Ian Romanick27e3cf82010-04-01 18:03:59 -07001551 }
Kenneth Graunke2b7c42b2010-07-16 18:28:44 -07001552 } else if (array->type->array_size() == 0) {
1553 _mesa_glsl_error(&loc, state, "unsized array index must be constant");
Eric Anholta721abf2010-08-23 10:32:01 -07001554 } else {
1555 if (array->type->is_array()) {
Aras Pranckevicius5226f8c2010-08-25 22:42:13 +03001556 /* whole_variable_referenced can return NULL if the array is a
1557 * member of a structure. In this case it is safe to not update
1558 * the max_array_access field because it is never used for fields
1559 * of structures.
1560 */
Eric Anholta721abf2010-08-23 10:32:01 -07001561 ir_variable *v = array->whole_variable_referenced();
Aras Pranckevicius5226f8c2010-08-25 22:42:13 +03001562 if (v != NULL)
1563 v->max_array_access = array->type->array_size();
Eric Anholta721abf2010-08-23 10:32:01 -07001564 }
Ian Romanick27e3cf82010-04-01 18:03:59 -07001565 }
1566
Ian Romanicke942f322011-01-04 16:09:00 -08001567 /* From page 23 (29 of the PDF) of the GLSL 1.30 spec:
1568 *
Chad Versacef0f2ec42010-12-07 10:35:36 -08001569 * "Samplers aggregated into arrays within a shader (using square
1570 * brackets [ ]) can only be indexed with integral constant
1571 * expressions [...]."
Ian Romanicke942f322011-01-04 16:09:00 -08001572 *
1573 * This restriction was added in GLSL 1.30. Shaders using earlier version
1574 * of the language should not be rejected by the compiler front-end for
1575 * using this construct. This allows useful things such as using a loop
1576 * counter as the index to an array of samplers. If the loop in unrolled,
1577 * the code should compile correctly. Instead, emit a warning.
Chad Versacef0f2ec42010-12-07 10:35:36 -08001578 */
1579 if (array->type->is_array() &&
1580 array->type->element_type()->is_sampler() &&
1581 const_index == NULL) {
1582
Ian Romanicke942f322011-01-04 16:09:00 -08001583 if (state->language_version == 100) {
1584 _mesa_glsl_warning(&loc, state,
1585 "sampler arrays indexed with non-constant "
1586 "expressions is optional in GLSL ES 1.00");
1587 } else if (state->language_version < 130) {
1588 _mesa_glsl_warning(&loc, state,
1589 "sampler arrays indexed with non-constant "
1590 "expressions is forbidden in GLSL 1.30 and "
1591 "later");
1592 } else {
1593 _mesa_glsl_error(&loc, state,
1594 "sampler arrays indexed with non-constant "
1595 "expressions is forbidden in GLSL 1.30 and "
1596 "later");
1597 error_emitted = true;
1598 }
Chad Versacef0f2ec42010-12-07 10:35:36 -08001599 }
1600
Ian Romanick27e3cf82010-04-01 18:03:59 -07001601 if (error_emitted)
1602 result->type = glsl_type::error_type;
1603
1604 type = result->type;
Ian Romanicka87ac252010-02-22 13:19:34 -08001605 break;
Ian Romanick27e3cf82010-04-01 18:03:59 -07001606 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001607
1608 case ast_function_call:
Ian Romanick7cfddf12010-03-10 13:26:52 -08001609 /* Should *NEVER* get here. ast_function_call should always be handled
1610 * by ast_function_expression::hir.
Ian Romanicka87ac252010-02-22 13:19:34 -08001611 */
Ian Romanick7cfddf12010-03-10 13:26:52 -08001612 assert(0);
Ian Romanicka87ac252010-02-22 13:19:34 -08001613 break;
1614
1615 case ast_identifier: {
1616 /* ast_identifier can appear several places in a full abstract syntax
1617 * tree. This particular use must be at location specified in the grammar
1618 * as 'variable_identifier'.
1619 */
Ian Romanick8bde4ce2010-03-19 11:57:24 -07001620 ir_variable *var =
1621 state->symbols->get_variable(this->primary_expression.identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08001622
Carl Worth1660a292010-06-23 18:11:51 -07001623 result = new(ctx) ir_dereference_variable(var);
Ian Romanicka87ac252010-02-22 13:19:34 -08001624
1625 if (var != NULL) {
Ian Romanickbd330552011-01-07 18:34:58 -08001626 var->used = true;
Ian Romanicka87ac252010-02-22 13:19:34 -08001627 type = result->type;
1628 } else {
Ian Romanick71d0bbf2010-03-23 13:21:19 -07001629 _mesa_glsl_error(& loc, state, "`%s' undeclared",
Ian Romanick18238de2010-03-01 13:49:10 -08001630 this->primary_expression.identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08001631
1632 error_emitted = true;
1633 }
1634 break;
1635 }
1636
1637 case ast_int_constant:
Ian Romanick0471e8b2010-03-26 14:33:41 -07001638 type = glsl_type::int_type;
Carl Worth1660a292010-06-23 18:11:51 -07001639 result = new(ctx) ir_constant(this->primary_expression.int_constant);
Ian Romanicka87ac252010-02-22 13:19:34 -08001640 break;
1641
1642 case ast_uint_constant:
Ian Romanick0471e8b2010-03-26 14:33:41 -07001643 type = glsl_type::uint_type;
Carl Worth1660a292010-06-23 18:11:51 -07001644 result = new(ctx) ir_constant(this->primary_expression.uint_constant);
Ian Romanicka87ac252010-02-22 13:19:34 -08001645 break;
1646
1647 case ast_float_constant:
Ian Romanick0471e8b2010-03-26 14:33:41 -07001648 type = glsl_type::float_type;
Carl Worth1660a292010-06-23 18:11:51 -07001649 result = new(ctx) ir_constant(this->primary_expression.float_constant);
Ian Romanicka87ac252010-02-22 13:19:34 -08001650 break;
1651
1652 case ast_bool_constant:
Ian Romanick0471e8b2010-03-26 14:33:41 -07001653 type = glsl_type::bool_type;
Carl Worth1660a292010-06-23 18:11:51 -07001654 result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
Ian Romanicka87ac252010-02-22 13:19:34 -08001655 break;
1656
1657 case ast_sequence: {
Ian Romanicka87ac252010-02-22 13:19:34 -08001658 /* It should not be possible to generate a sequence in the AST without
1659 * any expressions in it.
1660 */
Ian Romanick304ea902010-05-10 11:17:53 -07001661 assert(!this->expressions.is_empty());
Ian Romanicka87ac252010-02-22 13:19:34 -08001662
1663 /* The r-value of a sequence is the last expression in the sequence. If
1664 * the other expressions in the sequence do not have side-effects (and
1665 * therefore add instructions to the instruction list), they get dropped
1666 * on the floor.
1667 */
Ian Romanick2b97dc62010-05-10 17:42:05 -07001668 foreach_list_typed (ast_node, ast, link, &this->expressions)
Ian Romanick304ea902010-05-10 11:17:53 -07001669 result = ast->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001670
1671 type = result->type;
1672
1673 /* Any errors should have already been emitted in the loop above.
1674 */
1675 error_emitted = true;
1676 break;
1677 }
1678 }
1679
Ian Romanickcef3bae2010-03-26 14:41:32 -07001680 if (type->is_error() && !error_emitted)
Ian Romanick71d0bbf2010-03-23 13:21:19 -07001681 _mesa_glsl_error(& loc, state, "type mismatch");
Ian Romanicka87ac252010-02-22 13:19:34 -08001682
1683 return result;
1684}
1685
1686
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001687ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08001688ast_expression_statement::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08001689 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08001690{
Ian Romanicka87ac252010-02-22 13:19:34 -08001691 /* It is possible to have expression statements that don't have an
1692 * expression. This is the solitary semicolon:
1693 *
1694 * for (i = 0; i < 5; i++)
1695 * ;
1696 *
1697 * In this case the expression will be NULL. Test for NULL and don't do
1698 * anything in that case.
1699 */
Ian Romanick18238de2010-03-01 13:49:10 -08001700 if (expression != NULL)
1701 expression->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001702
1703 /* Statements do not have r-values.
1704 */
1705 return NULL;
1706}
1707
1708
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001709ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08001710ast_compound_statement::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08001711 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08001712{
Ian Romanick18238de2010-03-01 13:49:10 -08001713 if (new_scope)
Ian Romanick8bde4ce2010-03-19 11:57:24 -07001714 state->symbols->push_scope();
Ian Romanicka87ac252010-02-22 13:19:34 -08001715
Ian Romanick2b97dc62010-05-10 17:42:05 -07001716 foreach_list_typed (ast_node, ast, link, &this->statements)
Ian Romanick304ea902010-05-10 11:17:53 -07001717 ast->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001718
Ian Romanick18238de2010-03-01 13:49:10 -08001719 if (new_scope)
Ian Romanick8bde4ce2010-03-19 11:57:24 -07001720 state->symbols->pop_scope();
Ian Romanicka87ac252010-02-22 13:19:34 -08001721
1722 /* Compound statements do not have r-values.
1723 */
1724 return NULL;
1725}
1726
1727
Ian Romanick28009cd2010-03-30 16:59:27 -07001728static const glsl_type *
Kenneth Graunked8e34e22010-08-07 02:56:01 -07001729process_array_type(YYLTYPE *loc, const glsl_type *base, ast_node *array_size,
Ian Romanick28009cd2010-03-30 16:59:27 -07001730 struct _mesa_glsl_parse_state *state)
1731{
1732 unsigned length = 0;
1733
1734 /* FINISHME: Reject delcarations of multidimensional arrays. */
1735
1736 if (array_size != NULL) {
1737 exec_list dummy_instructions;
1738 ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
1739 YYLTYPE loc = array_size->get_location();
1740
1741 /* FINISHME: Verify that the grammar forbids side-effects in array
1742 * FINISHME: sizes. i.e., 'vec4 [x = 12] data'
1743 */
1744 assert(dummy_instructions.is_empty());
1745
1746 if (ir != NULL) {
1747 if (!ir->type->is_integer()) {
1748 _mesa_glsl_error(& loc, state, "array size must be integer type");
1749 } else if (!ir->type->is_scalar()) {
1750 _mesa_glsl_error(& loc, state, "array size must be scalar type");
1751 } else {
1752 ir_constant *const size = ir->constant_expression_value();
1753
1754 if (size == NULL) {
1755 _mesa_glsl_error(& loc, state, "array size must be a "
1756 "constant valued expression");
1757 } else if (size->value.i[0] <= 0) {
1758 _mesa_glsl_error(& loc, state, "array size must be > 0");
1759 } else {
1760 assert(size->type == ir->type);
1761 length = size->value.u[0];
1762 }
1763 }
1764 }
Kenneth Graunked8e34e22010-08-07 02:56:01 -07001765 } else if (state->es_shader) {
1766 /* Section 10.17 of the GLSL ES 1.00 specification states that unsized
1767 * array declarations have been removed from the language.
1768 */
1769 _mesa_glsl_error(loc, state, "unsized array declarations are not "
1770 "allowed in GLSL ES 1.00.");
Ian Romanick28009cd2010-03-30 16:59:27 -07001771 }
1772
Ian Romanickf38d15b2010-07-20 15:33:40 -07001773 return glsl_type::get_array_instance(base, length);
Ian Romanick28009cd2010-03-30 16:59:27 -07001774}
1775
1776
Ian Romanickd612a122010-03-31 16:22:06 -07001777const glsl_type *
1778ast_type_specifier::glsl_type(const char **name,
1779 struct _mesa_glsl_parse_state *state) const
Ian Romanicka87ac252010-02-22 13:19:34 -08001780{
Ian Romanickd612a122010-03-31 16:22:06 -07001781 const struct glsl_type *type;
Ian Romanicka87ac252010-02-22 13:19:34 -08001782
Kenneth Graunkeca92ae22010-09-18 11:11:09 +02001783 type = state->symbols->get_type(this->type_name);
1784 *name = this->type_name;
Ian Romanicka87ac252010-02-22 13:19:34 -08001785
Kenneth Graunkeca92ae22010-09-18 11:11:09 +02001786 if (this->is_array) {
1787 YYLTYPE loc = this->get_location();
1788 type = process_array_type(&loc, type, this->array_size, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001789 }
1790
1791 return type;
1792}
1793
1794
1795static void
1796apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
Ian Romanick768b55a2010-08-13 16:46:43 -07001797 ir_variable *var,
Eric Anholt2e063f12010-03-28 00:56:22 -07001798 struct _mesa_glsl_parse_state *state,
1799 YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -08001800{
Ian Romanickbd330552011-01-07 18:34:58 -08001801 if (qual->flags.q.invariant) {
1802 if (var->used) {
1803 _mesa_glsl_error(loc, state,
1804 "variable `%s' may not be redeclared "
1805 "`invariant' after being used",
1806 var->name);
1807 } else {
1808 var->invariant = 1;
1809 }
1810 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001811
1812 /* FINISHME: Mark 'in' variables at global scope as read-only. */
Ian Romanicke24d35a2010-10-05 16:38:47 -07001813 if (qual->flags.q.constant || qual->flags.q.attribute
1814 || qual->flags.q.uniform
1815 || (qual->flags.q.varying && (state->target == fragment_shader)))
Ian Romanicka87ac252010-02-22 13:19:34 -08001816 var->read_only = 1;
1817
Ian Romanicke24d35a2010-10-05 16:38:47 -07001818 if (qual->flags.q.centroid)
Ian Romanicka87ac252010-02-22 13:19:34 -08001819 var->centroid = 1;
1820
Ian Romanicke24d35a2010-10-05 16:38:47 -07001821 if (qual->flags.q.attribute && state->target != vertex_shader) {
Eric Anholt2e063f12010-03-28 00:56:22 -07001822 var->type = glsl_type::error_type;
1823 _mesa_glsl_error(loc, state,
1824 "`attribute' variables may not be declared in the "
Ian Romanickae4c4c02010-04-07 16:41:40 -07001825 "%s shader",
1826 _mesa_glsl_shader_target_name(state->target));
Eric Anholt2e063f12010-03-28 00:56:22 -07001827 }
1828
Eric Anholt90b78252010-03-31 21:21:20 -10001829 /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
1830 *
1831 * "The varying qualifier can be used only with the data types
1832 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
1833 * these."
1834 */
Ian Romanicke24d35a2010-10-05 16:38:47 -07001835 if (qual->flags.q.varying) {
Eric Anholt0ca17192010-05-19 13:38:15 -07001836 const glsl_type *non_array_type;
1837
1838 if (var->type && var->type->is_array())
1839 non_array_type = var->type->fields.array;
1840 else
1841 non_array_type = var->type;
1842
1843 if (non_array_type && non_array_type->base_type != GLSL_TYPE_FLOAT) {
1844 var->type = glsl_type::error_type;
1845 _mesa_glsl_error(loc, state,
1846 "varying variables must be of base type float");
1847 }
Eric Anholt90b78252010-03-31 21:21:20 -10001848 }
1849
Ian Romanick7e2aa912010-07-19 17:12:42 -07001850 /* If there is no qualifier that changes the mode of the variable, leave
1851 * the setting alone.
1852 */
Ian Romanicke24d35a2010-10-05 16:38:47 -07001853 if (qual->flags.q.in && qual->flags.q.out)
Ian Romanicka87ac252010-02-22 13:19:34 -08001854 var->mode = ir_var_inout;
Ian Romanicke24d35a2010-10-05 16:38:47 -07001855 else if (qual->flags.q.attribute || qual->flags.q.in
1856 || (qual->flags.q.varying && (state->target == fragment_shader)))
Ian Romanicka87ac252010-02-22 13:19:34 -08001857 var->mode = ir_var_in;
Ian Romanicke24d35a2010-10-05 16:38:47 -07001858 else if (qual->flags.q.out
1859 || (qual->flags.q.varying && (state->target == vertex_shader)))
Ian Romanicka87ac252010-02-22 13:19:34 -08001860 var->mode = ir_var_out;
Ian Romanicke24d35a2010-10-05 16:38:47 -07001861 else if (qual->flags.q.uniform)
Ian Romanicka87ac252010-02-22 13:19:34 -08001862 var->mode = ir_var_uniform;
Ian Romanicka87ac252010-02-22 13:19:34 -08001863
Ian Romanick86b43982011-01-06 10:49:56 -08001864 if (state->all_invariant && (state->current_function == NULL)) {
1865 switch (state->target) {
1866 case vertex_shader:
1867 if (var->mode == ir_var_out)
1868 var->invariant = true;
1869 break;
1870 case geometry_shader:
1871 if ((var->mode == ir_var_in) || (var->mode == ir_var_out))
1872 var->invariant = true;
1873 break;
1874 case fragment_shader:
1875 if (var->mode == ir_var_in)
1876 var->invariant = true;
1877 break;
1878 }
1879 }
1880
Ian Romanicke24d35a2010-10-05 16:38:47 -07001881 if (qual->flags.q.flat)
Ian Romanicka87ac252010-02-22 13:19:34 -08001882 var->interpolation = ir_var_flat;
Ian Romanicke24d35a2010-10-05 16:38:47 -07001883 else if (qual->flags.q.noperspective)
Ian Romanicka87ac252010-02-22 13:19:34 -08001884 var->interpolation = ir_var_noperspective;
1885 else
1886 var->interpolation = ir_var_smooth;
Ian Romanick9d975372010-04-02 17:17:47 -07001887
Ian Romanicke24d35a2010-10-05 16:38:47 -07001888 var->pixel_center_integer = qual->flags.q.pixel_center_integer;
1889 var->origin_upper_left = qual->flags.q.origin_upper_left;
1890 if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer)
Ian Romanick8d8469e2010-06-30 17:48:09 -07001891 && (strcmp(var->name, "gl_FragCoord") != 0)) {
Ian Romanicke24d35a2010-10-05 16:38:47 -07001892 const char *const qual_string = (qual->flags.q.origin_upper_left)
Ian Romanick8d8469e2010-06-30 17:48:09 -07001893 ? "origin_upper_left" : "pixel_center_integer";
1894
1895 _mesa_glsl_error(loc, state,
1896 "layout qualifier `%s' can only be applied to "
1897 "fragment shader input `gl_FragCoord'",
1898 qual_string);
1899 }
1900
Ian Romanickeee68d32010-10-07 15:13:38 -07001901 if (qual->flags.q.explicit_location) {
1902 const bool global_scope = (state->current_function == NULL);
1903 bool fail = false;
1904 const char *string = "";
1905
1906 /* In the vertex shader only shader inputs can be given explicit
1907 * locations.
1908 *
1909 * In the fragment shader only shader outputs can be given explicit
1910 * locations.
1911 */
1912 switch (state->target) {
1913 case vertex_shader:
1914 if (!global_scope || (var->mode != ir_var_in)) {
1915 fail = true;
1916 string = "input";
1917 }
1918 break;
1919
1920 case geometry_shader:
1921 _mesa_glsl_error(loc, state,
1922 "geometry shader variables cannot be given "
1923 "explicit locations\n");
1924 break;
1925
1926 case fragment_shader:
1927 if (!global_scope || (var->mode != ir_var_in)) {
1928 fail = true;
1929 string = "output";
1930 }
1931 break;
Kenneth Graunkea75da2c2010-10-20 14:59:40 -07001932 };
Ian Romanickeee68d32010-10-07 15:13:38 -07001933
1934 if (fail) {
1935 _mesa_glsl_error(loc, state,
1936 "only %s shader %s variables can be given an "
1937 "explicit location\n",
1938 _mesa_glsl_shader_target_name(state->target),
1939 string);
1940 } else {
1941 var->explicit_location = true;
Ian Romanick68a4fc92010-10-07 17:21:22 -07001942
1943 /* This bit of silliness is needed because invalid explicit locations
1944 * are supposed to be flagged during linking. Small negative values
1945 * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
1946 * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
1947 * The linker needs to be able to differentiate these cases. This
1948 * ensures that negative values stay negative.
1949 */
1950 if (qual->location >= 0) {
1951 var->location = (state->target == vertex_shader)
1952 ? (qual->location + VERT_ATTRIB_GENERIC0)
1953 : (qual->location + FRAG_RESULT_DATA0);
1954 } else {
1955 var->location = qual->location;
1956 }
Ian Romanickeee68d32010-10-07 15:13:38 -07001957 }
1958 }
1959
Ian Romanick4bcff0c2011-01-07 16:53:59 -08001960 /* Does the declaration use the 'layout' keyword?
1961 */
1962 const bool uses_layout = qual->flags.q.pixel_center_integer
1963 || qual->flags.q.origin_upper_left
1964 || qual->flags.q.explicit_location;
1965
1966 /* Does the declaration use the deprecated 'attribute' or 'varying'
1967 * keywords?
1968 */
1969 const bool uses_deprecated_qualifier = qual->flags.q.attribute
1970 || qual->flags.q.varying;
1971
1972 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
1973 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
1974 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
1975 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
1976 * These extensions and all following extensions that add the 'layout'
1977 * keyword have been modified to require the use of 'in' or 'out'.
1978 *
1979 * The following extension do not allow the deprecated keywords:
1980 *
1981 * GL_AMD_conservative_depth
1982 * GL_ARB_gpu_shader5
1983 * GL_ARB_separate_shader_objects
1984 * GL_ARB_tesselation_shader
1985 * GL_ARB_transform_feedback3
1986 * GL_ARB_uniform_buffer_object
1987 *
1988 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
1989 * allow layout with the deprecated keywords.
1990 */
1991 const bool relaxed_layout_qualifier_checking =
1992 state->ARB_fragment_coord_conventions_enable;
1993
1994 if (uses_layout && uses_deprecated_qualifier) {
1995 if (relaxed_layout_qualifier_checking) {
1996 _mesa_glsl_warning(loc, state,
1997 "`layout' qualifier may not be used with "
1998 "`attribute' or `varying'");
1999 } else {
2000 _mesa_glsl_error(loc, state,
2001 "`layout' qualifier may not be used with "
2002 "`attribute' or `varying'");
2003 }
2004 }
2005
Kenneth Graunke10eaa8b2010-09-07 02:59:38 -07002006 if (var->type->is_array() && state->language_version != 110) {
Ian Romanick9d975372010-04-02 17:17:47 -07002007 var->array_lvalue = true;
2008 }
Ian Romanicka87ac252010-02-22 13:19:34 -08002009}
2010
2011
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07002012ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08002013ast_declarator_list::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08002014 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08002015{
Kenneth Graunke953ff122010-06-25 13:14:37 -07002016 void *ctx = state;
Ian Romanicka87ac252010-02-22 13:19:34 -08002017 const struct glsl_type *decl_type;
2018 const char *type_name = NULL;
Eric Anholt85584592010-04-14 15:38:52 -07002019 ir_rvalue *result = NULL;
Ian Romanickc824e352010-04-23 15:55:19 -07002020 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08002021
Ian Romanick6f0823d2010-07-01 20:39:08 -07002022 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
2023 *
2024 * "To ensure that a particular output variable is invariant, it is
2025 * necessary to use the invariant qualifier. It can either be used to
2026 * qualify a previously declared variable as being invariant
2027 *
2028 * invariant gl_Position; // make existing gl_Position be invariant"
2029 *
2030 * In these cases the parser will set the 'invariant' flag in the declarator
2031 * list, and the type will be NULL.
2032 */
2033 if (this->invariant) {
2034 assert(this->type == NULL);
2035
2036 if (state->current_function != NULL) {
2037 _mesa_glsl_error(& loc, state,
2038 "All uses of `invariant' keyword must be at global "
2039 "scope\n");
2040 }
2041
2042 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
2043 assert(!decl->is_array);
2044 assert(decl->array_size == NULL);
2045 assert(decl->initializer == NULL);
2046
2047 ir_variable *const earlier =
2048 state->symbols->get_variable(decl->identifier);
2049 if (earlier == NULL) {
2050 _mesa_glsl_error(& loc, state,
2051 "Undeclared variable `%s' cannot be marked "
2052 "invariant\n", decl->identifier);
2053 } else if ((state->target == vertex_shader)
2054 && (earlier->mode != ir_var_out)) {
2055 _mesa_glsl_error(& loc, state,
2056 "`%s' cannot be marked invariant, vertex shader "
2057 "outputs only\n", decl->identifier);
2058 } else if ((state->target == fragment_shader)
2059 && (earlier->mode != ir_var_in)) {
2060 _mesa_glsl_error(& loc, state,
2061 "`%s' cannot be marked invariant, fragment shader "
2062 "inputs only\n", decl->identifier);
Ian Romanickbd330552011-01-07 18:34:58 -08002063 } else if (earlier->used) {
2064 _mesa_glsl_error(& loc, state,
2065 "variable `%s' may not be redeclared "
2066 "`invariant' after being used",
2067 earlier->name);
Ian Romanick6f0823d2010-07-01 20:39:08 -07002068 } else {
2069 earlier->invariant = true;
2070 }
2071 }
2072
2073 /* Invariant redeclarations do not have r-values.
2074 */
2075 return NULL;
2076 }
2077
2078 assert(this->type != NULL);
2079 assert(!this->invariant);
2080
Ian Romanick3455ce62010-04-19 15:13:15 -07002081 /* The type specifier may contain a structure definition. Process that
2082 * before any of the variable declarations.
2083 */
2084 (void) this->type->specifier->hir(instructions, state);
2085
Ian Romanickd612a122010-03-31 16:22:06 -07002086 decl_type = this->type->specifier->glsl_type(& type_name, state);
Ian Romanick304ea902010-05-10 11:17:53 -07002087 if (this->declarations.is_empty()) {
Ian Romanick6f0823d2010-07-01 20:39:08 -07002088 /* The only valid case where the declaration list can be empty is when
2089 * the declaration is setting the default precision of a built-in type
2090 * (e.g., 'precision highp vec4;').
Ian Romanickc824e352010-04-23 15:55:19 -07002091 */
2092
Ian Romanick6f0823d2010-07-01 20:39:08 -07002093 if (decl_type != NULL) {
Ian Romanickc824e352010-04-23 15:55:19 -07002094 } else {
2095 _mesa_glsl_error(& loc, state, "incomplete declaration");
2096 }
2097 }
Ian Romanicka87ac252010-02-22 13:19:34 -08002098
Ian Romanick2b97dc62010-05-10 17:42:05 -07002099 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
Ian Romanicka87ac252010-02-22 13:19:34 -08002100 const struct glsl_type *var_type;
Ian Romanick768b55a2010-08-13 16:46:43 -07002101 ir_variable *var;
Ian Romanicka87ac252010-02-22 13:19:34 -08002102
2103 /* FINISHME: Emit a warning if a variable declaration shadows a
2104 * FINISHME: declaration at a higher scope.
2105 */
2106
Ian Romanickcec65a62010-03-23 12:28:44 -07002107 if ((decl_type == NULL) || decl_type->is_void()) {
Ian Romanicka87ac252010-02-22 13:19:34 -08002108 if (type_name != NULL) {
2109 _mesa_glsl_error(& loc, state,
2110 "invalid type `%s' in declaration of `%s'",
2111 type_name, decl->identifier);
2112 } else {
2113 _mesa_glsl_error(& loc, state,
2114 "invalid type in declaration of `%s'",
2115 decl->identifier);
2116 }
2117 continue;
2118 }
2119
2120 if (decl->is_array) {
Kenneth Graunked8e34e22010-08-07 02:56:01 -07002121 var_type = process_array_type(&loc, decl_type, decl->array_size,
2122 state);
Ian Romanicka87ac252010-02-22 13:19:34 -08002123 } else {
2124 var_type = decl_type;
2125 }
2126
Ian Romanick7e2aa912010-07-19 17:12:42 -07002127 var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto);
Ian Romanicka87ac252010-02-22 13:19:34 -08002128
Eric Anholt3f151502010-04-02 01:53:57 -10002129 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
2130 *
2131 * "Global variables can only use the qualifiers const,
2132 * attribute, uni form, or varying. Only one may be
2133 * specified.
2134 *
2135 * Local variables can only use the qualifier const."
2136 *
Ian Romanick82c4b4f2011-01-07 16:53:07 -08002137 * This is relaxed in GLSL 1.30. It is also relaxed by any extension
2138 * that adds the 'layout' keyword.
Eric Anholt3f151502010-04-02 01:53:57 -10002139 */
Ian Romanick82c4b4f2011-01-07 16:53:07 -08002140 if ((state->language_version < 130)
2141 && !state->ARB_explicit_attrib_location_enable
2142 && !state->ARB_fragment_coord_conventions_enable) {
Ian Romanicke24d35a2010-10-05 16:38:47 -07002143 if (this->type->qualifier.flags.q.out) {
Eric Anholt3f151502010-04-02 01:53:57 -10002144 _mesa_glsl_error(& loc, state,
2145 "`out' qualifier in declaration of `%s' "
Ian Romanick469ea692011-01-07 16:05:59 -08002146 "only valid for function parameters in %s.",
2147 decl->identifier, state->version_string);
Eric Anholt3f151502010-04-02 01:53:57 -10002148 }
Ian Romanicke24d35a2010-10-05 16:38:47 -07002149 if (this->type->qualifier.flags.q.in) {
Eric Anholt3f151502010-04-02 01:53:57 -10002150 _mesa_glsl_error(& loc, state,
2151 "`in' qualifier in declaration of `%s' "
Ian Romanick469ea692011-01-07 16:05:59 -08002152 "only valid for function parameters in %s.",
2153 decl->identifier, state->version_string);
Eric Anholt3f151502010-04-02 01:53:57 -10002154 }
2155 /* FINISHME: Test for other invalid qualifiers. */
2156 }
2157
Eric Anholt2e063f12010-03-28 00:56:22 -07002158 apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
2159 & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -08002160
Ian Romanicke24d35a2010-10-05 16:38:47 -07002161 if (this->type->qualifier.flags.q.invariant) {
Eric Anholt046bef22010-08-04 20:33:57 -07002162 if ((state->target == vertex_shader) && !(var->mode == ir_var_out ||
2163 var->mode == ir_var_inout)) {
2164 /* FINISHME: Note that this doesn't work for invariant on
2165 * a function signature outval
2166 */
Ian Romanick6f0823d2010-07-01 20:39:08 -07002167 _mesa_glsl_error(& loc, state,
2168 "`%s' cannot be marked invariant, vertex shader "
2169 "outputs only\n", var->name);
Eric Anholt046bef22010-08-04 20:33:57 -07002170 } else if ((state->target == fragment_shader) &&
2171 !(var->mode == ir_var_in || var->mode == ir_var_inout)) {
2172 /* FINISHME: Note that this doesn't work for invariant on
2173 * a function signature inval
2174 */
Ian Romanick6f0823d2010-07-01 20:39:08 -07002175 _mesa_glsl_error(& loc, state,
2176 "`%s' cannot be marked invariant, fragment shader "
2177 "inputs only\n", var->name);
2178 }
2179 }
2180
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07002181 if (state->current_function != NULL) {
Ian Romanickb168e532010-03-31 12:31:18 -07002182 const char *mode = NULL;
Ian Romanicke0800062010-03-31 13:15:23 -07002183 const char *extra = "";
Ian Romanickb168e532010-03-31 12:31:18 -07002184
Ian Romanicke0800062010-03-31 13:15:23 -07002185 /* There is no need to check for 'inout' here because the parser will
2186 * only allow that in function parameter lists.
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07002187 */
Ian Romanicke24d35a2010-10-05 16:38:47 -07002188 if (this->type->qualifier.flags.q.attribute) {
Ian Romanickb168e532010-03-31 12:31:18 -07002189 mode = "attribute";
Ian Romanicke24d35a2010-10-05 16:38:47 -07002190 } else if (this->type->qualifier.flags.q.uniform) {
Ian Romanickb168e532010-03-31 12:31:18 -07002191 mode = "uniform";
Ian Romanicke24d35a2010-10-05 16:38:47 -07002192 } else if (this->type->qualifier.flags.q.varying) {
Ian Romanickb168e532010-03-31 12:31:18 -07002193 mode = "varying";
Ian Romanicke24d35a2010-10-05 16:38:47 -07002194 } else if (this->type->qualifier.flags.q.in) {
Ian Romanicke0800062010-03-31 13:15:23 -07002195 mode = "in";
2196 extra = " or in function parameter list";
Ian Romanicke24d35a2010-10-05 16:38:47 -07002197 } else if (this->type->qualifier.flags.q.out) {
Ian Romanicke0800062010-03-31 13:15:23 -07002198 mode = "out";
2199 extra = " or in function parameter list";
Ian Romanickb168e532010-03-31 12:31:18 -07002200 }
2201
2202 if (mode) {
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07002203 _mesa_glsl_error(& loc, state,
Ian Romanickb168e532010-03-31 12:31:18 -07002204 "%s variable `%s' must be declared at "
Ian Romanicke0800062010-03-31 13:15:23 -07002205 "global scope%s",
2206 mode, var->name, extra);
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07002207 }
2208 } else if (var->mode == ir_var_in) {
Ian Romanickfb9f5b02010-03-29 17:16:35 -07002209 if (state->target == vertex_shader) {
2210 bool error_emitted = false;
2211
2212 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
2213 *
2214 * "Vertex shader inputs can only be float, floating-point
2215 * vectors, matrices, signed and unsigned integers and integer
2216 * vectors. Vertex shader inputs can also form arrays of these
2217 * types, but not structures."
2218 *
Ian Romanick2d816202010-03-29 17:40:11 -07002219 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
2220 *
2221 * "Vertex shader inputs can only be float, floating-point
2222 * vectors, matrices, signed and unsigned integers and integer
2223 * vectors. They cannot be arrays or structures."
2224 *
Ian Romanickfb9f5b02010-03-29 17:16:35 -07002225 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
2226 *
2227 * "The attribute qualifier can be used only with float,
2228 * floating-point vectors, and matrices. Attribute variables
2229 * cannot be declared as arrays or structures."
2230 */
2231 const glsl_type *check_type = var->type->is_array()
2232 ? var->type->fields.array : var->type;
2233
2234 switch (check_type->base_type) {
2235 case GLSL_TYPE_FLOAT:
2236 break;
2237 case GLSL_TYPE_UINT:
2238 case GLSL_TYPE_INT:
2239 if (state->language_version > 120)
2240 break;
2241 /* FALLTHROUGH */
2242 default:
2243 _mesa_glsl_error(& loc, state,
2244 "vertex shader input / attribute cannot have "
2245 "type %s`%s'",
2246 var->type->is_array() ? "array of " : "",
2247 check_type->name);
2248 error_emitted = true;
2249 }
2250
Ian Romanick2d816202010-03-29 17:40:11 -07002251 if (!error_emitted && (state->language_version <= 130)
Ian Romanickfb9f5b02010-03-29 17:16:35 -07002252 && var->type->is_array()) {
2253 _mesa_glsl_error(& loc, state,
2254 "vertex shader input / attribute cannot have "
2255 "array type");
2256 error_emitted = true;
2257 }
2258 }
2259 }
2260
Chad Versace68d06b12010-12-16 11:06:19 -08002261 /* Integer vertex outputs must be qualified with 'flat'.
2262 *
2263 * From section 4.3.6 of the GLSL 1.30 spec:
2264 * "If a vertex output is a signed or unsigned integer or integer
2265 * vector, then it must be qualified with the interpolation qualifier
2266 * flat."
2267 */
2268 if (state->language_version >= 130
2269 && state->target == vertex_shader
2270 && state->current_function == NULL
2271 && var->type->is_integer()
2272 && var->mode == ir_var_out
2273 && var->interpolation != ir_var_flat) {
2274
2275 _mesa_glsl_error(&loc, state, "If a vertex output is an integer, "
2276 "then it must be qualified with 'flat'");
2277 }
2278
2279
Chad Versace605aacc2011-01-11 17:21:18 -08002280 /* Interpolation qualifiers cannot be applied to 'centroid' and
2281 * 'centroid varying'.
2282 *
2283 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
2284 * "interpolation qualifiers may only precede the qualifiers in,
2285 * centroid in, out, or centroid out in a declaration. They do not apply
2286 * to the deprecated storage qualifiers varying or centroid varying."
2287 */
2288 if (state->language_version >= 130
2289 && this->type->qualifier.has_interpolation()
2290 && this->type->qualifier.flags.q.varying) {
2291
2292 const char *i = this->type->qualifier.interpolation_string();
2293 assert(i != NULL);
2294 const char *s;
2295 if (this->type->qualifier.flags.q.centroid)
2296 s = "centroid varying";
2297 else
2298 s = "varying";
2299
2300 _mesa_glsl_error(&loc, state,
2301 "qualifier '%s' cannot be applied to the "
2302 "deprecated storage qualifier '%s'", i, s);
2303 }
2304
2305
Chad Versace8faaa4a2011-01-11 18:13:26 -08002306 /* Interpolation qualifiers can only apply to vertex shader outputs and
2307 * fragment shader inputs.
2308 *
2309 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
2310 * "Outputs from a vertex shader (out) and inputs to a fragment
2311 * shader (in) can be further qualified with one or more of these
2312 * interpolation qualifiers"
2313 */
2314 if (state->language_version >= 130
2315 && this->type->qualifier.has_interpolation()) {
2316
2317 const char *i = this->type->qualifier.interpolation_string();
2318 assert(i != NULL);
2319
2320 switch (state->target) {
2321 case vertex_shader:
2322 if (this->type->qualifier.flags.q.in) {
2323 _mesa_glsl_error(&loc, state,
2324 "qualifier '%s' cannot be applied to vertex "
2325 "shader inputs", i);
2326 }
2327 break;
2328 case fragment_shader:
2329 if (this->type->qualifier.flags.q.out) {
2330 _mesa_glsl_error(&loc, state,
2331 "qualifier '%s' cannot be applied to fragment "
2332 "shader outputs", i);
2333 }
2334 break;
2335 default:
2336 assert(0);
2337 }
2338 }
2339
2340
Ian Romanicke78e0fa2010-07-07 12:13:34 -07002341 /* Process the initializer and add its instructions to a temporary
2342 * list. This list will be added to the instruction stream (below) after
2343 * the declaration is added. This is done because in some cases (such as
2344 * redeclarations) the declaration may not actually be added to the
2345 * instruction stream.
2346 */
Eric Anholtfa33d0b2010-07-29 13:50:17 -07002347 exec_list initializer_instructions;
Ian Romanick66faec42010-03-27 18:56:53 -07002348 if (decl->initializer != NULL) {
Ian Romanick43de1722010-03-28 17:03:16 -07002349 YYLTYPE initializer_loc = decl->initializer->get_location();
2350
Ian Romanick66faec42010-03-27 18:56:53 -07002351 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
2352 *
2353 * "All uniform variables are read-only and are initialized either
2354 * directly by an application via API commands, or indirectly by
2355 * OpenGL."
2356 */
2357 if ((state->language_version <= 110)
2358 && (var->mode == ir_var_uniform)) {
Ian Romanick43de1722010-03-28 17:03:16 -07002359 _mesa_glsl_error(& initializer_loc, state,
2360 "cannot initialize uniforms in GLSL 1.10");
2361 }
Ian Romanick19360152010-03-26 18:05:27 -07002362
Ian Romanick43de1722010-03-28 17:03:16 -07002363 if (var->type->is_sampler()) {
2364 _mesa_glsl_error(& initializer_loc, state,
2365 "cannot initialize samplers");
2366 }
2367
2368 if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
2369 _mesa_glsl_error(& initializer_loc, state,
2370 "cannot initialize %s shader input / %s",
Ian Romanickae4c4c02010-04-07 16:41:40 -07002371 _mesa_glsl_shader_target_name(state->target),
Ian Romanick43de1722010-03-28 17:03:16 -07002372 (state->target == vertex_shader)
2373 ? "attribute" : "varying");
Ian Romanick66faec42010-03-27 18:56:53 -07002374 }
2375
Carl Worth1660a292010-06-23 18:11:51 -07002376 ir_dereference *const lhs = new(ctx) ir_dereference_variable(var);
Eric Anholtfa33d0b2010-07-29 13:50:17 -07002377 ir_rvalue *rhs = decl->initializer->hir(&initializer_instructions,
Ian Romanicke78e0fa2010-07-07 12:13:34 -07002378 state);
Ian Romanick66faec42010-03-27 18:56:53 -07002379
Ian Romanickce030882010-06-17 20:09:34 -07002380 /* Calculate the constant value if this is a const or uniform
Eric Anholt307c71b2010-03-31 15:53:26 -10002381 * declaration.
Ian Romanick66faec42010-03-27 18:56:53 -07002382 */
Ian Romanicke24d35a2010-10-05 16:38:47 -07002383 if (this->type->qualifier.flags.q.constant
2384 || this->type->qualifier.flags.q.uniform) {
Kenneth Graunkee1d71852010-07-20 03:53:47 -07002385 ir_rvalue *new_rhs = validate_assignment(state, var->type, rhs);
2386 if (new_rhs != NULL) {
2387 rhs = new_rhs;
Eric Anholte11757b2010-08-23 14:54:06 -07002388
2389 ir_constant *constant_value = rhs->constant_expression_value();
2390 if (!constant_value) {
2391 _mesa_glsl_error(& initializer_loc, state,
2392 "initializer of %s variable `%s' must be a "
2393 "constant expression",
Ian Romanicke24d35a2010-10-05 16:38:47 -07002394 (this->type->qualifier.flags.q.constant)
Eric Anholte11757b2010-08-23 14:54:06 -07002395 ? "const" : "uniform",
2396 decl->identifier);
2397 if (var->type->is_numeric()) {
2398 /* Reduce cascading errors. */
2399 var->constant_value = ir_constant::zero(ctx, var->type);
2400 }
2401 } else {
2402 rhs = constant_value;
2403 var->constant_value = constant_value;
2404 }
Kenneth Graunkee1d71852010-07-20 03:53:47 -07002405 } else {
2406 _mesa_glsl_error(&initializer_loc, state,
2407 "initializer of type %s cannot be assigned to "
2408 "variable of type %s",
2409 rhs->type->name, var->type->name);
Eric Anholte11757b2010-08-23 14:54:06 -07002410 if (var->type->is_numeric()) {
2411 /* Reduce cascading errors. */
2412 var->constant_value = ir_constant::zero(ctx, var->type);
2413 }
Eric Anholt307c71b2010-03-31 15:53:26 -10002414 }
2415 }
Ian Romanick66faec42010-03-27 18:56:53 -07002416
Eric Anholt307c71b2010-03-31 15:53:26 -10002417 if (rhs && !rhs->type->is_error()) {
Eric Anholtac3af372010-03-31 15:44:38 -10002418 bool temp = var->read_only;
Ian Romanicke24d35a2010-10-05 16:38:47 -07002419 if (this->type->qualifier.flags.q.constant)
Eric Anholtac3af372010-03-31 15:44:38 -10002420 var->read_only = false;
Ian Romanickce030882010-06-17 20:09:34 -07002421
Ian Romanickd7f27e22010-12-10 15:48:15 -08002422 /* Never emit code to initialize a uniform.
2423 */
2424 const glsl_type *initializer_type;
2425 if (!this->type->qualifier.flags.q.uniform) {
2426 result = do_assignment(&initializer_instructions, state,
2427 lhs, rhs,
2428 this->get_location());
2429 initializer_type = result->type;
2430 } else
2431 initializer_type = rhs->type;
2432
Ian Romanickb0fc5102010-12-07 16:27:22 -08002433 /* If the declared variable is an unsized array, it must inherrit
2434 * its full type from the initializer. A declaration such as
2435 *
2436 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
2437 *
2438 * becomes
2439 *
2440 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
2441 *
2442 * The assignment generated in the if-statement (below) will also
2443 * automatically handle this case for non-uniforms.
2444 *
2445 * If the declared variable is not an array, the types must
2446 * already match exactly. As a result, the type assignment
Ian Romanickd7f27e22010-12-10 15:48:15 -08002447 * here can be done unconditionally. For non-uniforms the call
2448 * to do_assignment can change the type of the initializer (via
2449 * the implicit conversion rules). For uniforms the initializer
2450 * must be a constant expression, and the type of that expression
2451 * was validated above.
Ian Romanickb0fc5102010-12-07 16:27:22 -08002452 */
Ian Romanickd7f27e22010-12-10 15:48:15 -08002453 var->type = initializer_type;
Ian Romanickb0fc5102010-12-07 16:27:22 -08002454
Eric Anholtac3af372010-03-31 15:44:38 -10002455 var->read_only = temp;
Ian Romanick66faec42010-03-27 18:56:53 -07002456 }
Ian Romanick19360152010-03-26 18:05:27 -07002457 }
Ian Romanick17d86f42010-03-29 12:59:02 -07002458
Eric Anholt0ed61252010-03-31 09:29:33 -10002459 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
2460 *
2461 * "It is an error to write to a const variable outside of
2462 * its declaration, so they must be initialized when
2463 * declared."
2464 */
Ian Romanicke24d35a2010-10-05 16:38:47 -07002465 if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) {
Eric Anholt0ed61252010-03-31 09:29:33 -10002466 _mesa_glsl_error(& loc, state,
2467 "const declaration of `%s' must be initialized");
2468 }
2469
Kenneth Graunke5d257462010-08-24 01:45:49 -07002470 /* Check if this declaration is actually a re-declaration, either to
2471 * resize an array or add qualifiers to an existing variable.
Ian Romanick5466b632010-07-01 12:46:55 -07002472 *
Kenneth Graunkea0442852010-08-23 14:52:06 -07002473 * This is allowed for variables in the current scope, or when at
2474 * global scope (for built-ins in the implicit outer scope).
Ian Romanick5466b632010-07-01 12:46:55 -07002475 */
Kenneth Graunke5d257462010-08-24 01:45:49 -07002476 ir_variable *earlier = state->symbols->get_variable(decl->identifier);
Kenneth Graunkea0442852010-08-23 14:52:06 -07002477 if (earlier != NULL && (state->current_function == NULL ||
2478 state->symbols->name_declared_this_scope(decl->identifier))) {
Ian Romanick5466b632010-07-01 12:46:55 -07002479
Kenneth Graunke5d257462010-08-24 01:45:49 -07002480 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
2481 *
2482 * "It is legal to declare an array without a size and then
2483 * later re-declare the same name as an array of the same
2484 * type and specify a size."
2485 */
2486 if ((earlier->type->array_size() == 0)
Ian Romanick5466b632010-07-01 12:46:55 -07002487 && var->type->is_array()
2488 && (var->type->element_type() == earlier->type->element_type())) {
2489 /* FINISHME: This doesn't match the qualifiers on the two
2490 * FINISHME: declarations. It's not 100% clear whether this is
2491 * FINISHME: required or not.
2492 */
2493
Ian Romanickcd00d5b2010-07-01 13:17:54 -07002494 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
2495 *
2496 * "The size [of gl_TexCoord] can be at most
2497 * gl_MaxTextureCoords."
Ian Romanickcd00d5b2010-07-01 13:17:54 -07002498 */
Ian Romanick127308b2010-07-01 13:30:50 -07002499 const unsigned size = unsigned(var->type->array_size());
Ian Romanickcd00d5b2010-07-01 13:17:54 -07002500 if ((strcmp("gl_TexCoord", var->name) == 0)
Ian Romanick127308b2010-07-01 13:30:50 -07002501 && (size > state->Const.MaxTextureCoords)) {
Ian Romanickcd00d5b2010-07-01 13:17:54 -07002502 YYLTYPE loc = this->get_location();
2503
2504 _mesa_glsl_error(& loc, state, "`gl_TexCoord' array size cannot "
2505 "be larger than gl_MaxTextureCoords (%u)\n",
Ian Romanick127308b2010-07-01 13:30:50 -07002506 state->Const.MaxTextureCoords);
Ian Romanick12873fa2010-07-01 14:10:19 -07002507 } else if ((size > 0) && (size <= earlier->max_array_access)) {
Ian Romanick5466b632010-07-01 12:46:55 -07002508 YYLTYPE loc = this->get_location();
2509
2510 _mesa_glsl_error(& loc, state, "array size must be > %u due to "
2511 "previous access",
2512 earlier->max_array_access);
2513 }
2514
2515 earlier->type = var->type;
2516 delete var;
2517 var = NULL;
Chad Versace6e006272010-10-23 10:40:40 -07002518 } else if (state->ARB_fragment_coord_conventions_enable
Kenneth Graunke5d257462010-08-24 01:45:49 -07002519 && strcmp(var->name, "gl_FragCoord") == 0
2520 && earlier->type == var->type
2521 && earlier->mode == var->mode) {
Eric Anholt4a962172010-07-28 14:41:51 -07002522 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
2523 * qualifiers.
2524 */
2525 earlier->origin_upper_left = var->origin_upper_left;
2526 earlier->pixel_center_integer = var->pixel_center_integer;
Chad Versaceb84e3f52010-12-15 16:32:47 -08002527
2528 /* According to section 4.3.7 of the GLSL 1.30 spec,
2529 * the following built-in varaibles can be redeclared with an
2530 * interpolation qualifier:
2531 * * gl_FrontColor
2532 * * gl_BackColor
2533 * * gl_FrontSecondaryColor
2534 * * gl_BackSecondaryColor
2535 * * gl_Color
2536 * * gl_SecondaryColor
2537 */
2538 } else if (state->language_version >= 130
2539 && (strcmp(var->name, "gl_FrontColor") == 0
2540 || strcmp(var->name, "gl_BackColor") == 0
2541 || strcmp(var->name, "gl_FrontSecondaryColor") == 0
2542 || strcmp(var->name, "gl_BackSecondaryColor") == 0
2543 || strcmp(var->name, "gl_Color") == 0
2544 || strcmp(var->name, "gl_SecondaryColor") == 0)
2545 && earlier->type == var->type
2546 && earlier->mode == var->mode) {
2547 earlier->interpolation = var->interpolation;
Ian Romanick5466b632010-07-01 12:46:55 -07002548 } else {
2549 YYLTYPE loc = this->get_location();
Kenneth Graunke5d257462010-08-24 01:45:49 -07002550 _mesa_glsl_error(&loc, state, "`%s' redeclared", decl->identifier);
Ian Romanick5466b632010-07-01 12:46:55 -07002551 }
2552
2553 continue;
2554 }
2555
Kenneth Graunke5d257462010-08-24 01:45:49 -07002556 /* By now, we know it's a new variable declaration (we didn't hit the
2557 * above "continue").
2558 *
2559 * From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
Ian Romanick5466b632010-07-01 12:46:55 -07002560 *
2561 * "Identifiers starting with "gl_" are reserved for use by
2562 * OpenGL, and may not be declared in a shader as either a
2563 * variable or a function."
2564 */
Ian Romanickde3b40d2010-08-26 09:24:58 -07002565 if (strncmp(decl->identifier, "gl_", 3) == 0)
Ian Romanick5466b632010-07-01 12:46:55 -07002566 _mesa_glsl_error(& loc, state,
2567 "identifier `%s' uses reserved `gl_' prefix",
2568 decl->identifier);
Ian Romanick5466b632010-07-01 12:46:55 -07002569
Kenneth Graunke5d257462010-08-24 01:45:49 -07002570 /* Add the variable to the symbol table. Note that the initializer's
2571 * IR was already processed earlier (though it hasn't been emitted yet),
2572 * without the variable in scope.
2573 *
2574 * This differs from most C-like languages, but it follows the GLSL
2575 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
2576 * spec:
2577 *
2578 * "Within a declaration, the scope of a name starts immediately
2579 * after the initializer if present or immediately after the name
2580 * being declared if not."
2581 */
Eric Anholt001eee52010-11-05 06:11:24 -07002582 if (!state->symbols->add_variable(var)) {
Kenneth Graunke5d257462010-08-24 01:45:49 -07002583 YYLTYPE loc = this->get_location();
2584 _mesa_glsl_error(&loc, state, "name `%s' already taken in the "
2585 "current scope", decl->identifier);
2586 continue;
2587 }
2588
Eric Anholt80482262010-08-05 14:41:09 -07002589 /* Push the variable declaration to the top. It means that all
2590 * the variable declarations will appear in a funny
2591 * last-to-first order, but otherwise we run into trouble if a
2592 * function is prototyped, a global var is decled, then the
2593 * function is defined with usage of the global var. See
2594 * glslparsertest's CorrectModule.frag.
2595 */
2596 instructions->push_head(var);
Eric Anholtfa33d0b2010-07-29 13:50:17 -07002597 instructions->append_list(&initializer_instructions);
Ian Romanicka87ac252010-02-22 13:19:34 -08002598 }
2599
Eric Anholt85584592010-04-14 15:38:52 -07002600
2601 /* Generally, variable declarations do not have r-values. However,
2602 * one is used for the declaration in
2603 *
2604 * while (bool b = some_condition()) {
2605 * ...
2606 * }
2607 *
2608 * so we return the rvalue from the last seen declaration here.
Ian Romanicka87ac252010-02-22 13:19:34 -08002609 */
Eric Anholt85584592010-04-14 15:38:52 -07002610 return result;
Ian Romanicka87ac252010-02-22 13:19:34 -08002611}
2612
2613
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07002614ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08002615ast_parameter_declarator::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08002616 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08002617{
Kenneth Graunke953ff122010-06-25 13:14:37 -07002618 void *ctx = state;
Ian Romanicka87ac252010-02-22 13:19:34 -08002619 const struct glsl_type *type;
2620 const char *name = NULL;
Eric Anholt2e063f12010-03-28 00:56:22 -07002621 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08002622
Ian Romanickd612a122010-03-31 16:22:06 -07002623 type = this->type->specifier->glsl_type(& name, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08002624
2625 if (type == NULL) {
Ian Romanicka87ac252010-02-22 13:19:34 -08002626 if (name != NULL) {
2627 _mesa_glsl_error(& loc, state,
2628 "invalid type `%s' in declaration of `%s'",
Ian Romanick18238de2010-03-01 13:49:10 -08002629 name, this->identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08002630 } else {
2631 _mesa_glsl_error(& loc, state,
2632 "invalid type in declaration of `%s'",
Ian Romanick18238de2010-03-01 13:49:10 -08002633 this->identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08002634 }
2635
Ian Romanick0471e8b2010-03-26 14:33:41 -07002636 type = glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -08002637 }
2638
Eric Anholt068c80c2010-03-31 09:56:36 -10002639 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
2640 *
2641 * "Functions that accept no input arguments need not use void in the
2642 * argument list because prototypes (or definitions) are required and
2643 * therefore there is no ambiguity when an empty argument list "( )" is
2644 * declared. The idiom "(void)" as a parameter list is provided for
2645 * convenience."
2646 *
2647 * Placing this check here prevents a void parameter being set up
2648 * for a function, which avoids tripping up checks for main taking
2649 * parameters and lookups of an unnamed symbol.
2650 */
Ian Romanickcf37c9e2010-04-02 15:30:45 -07002651 if (type->is_void()) {
2652 if (this->identifier != NULL)
2653 _mesa_glsl_error(& loc, state,
2654 "named parameter cannot have type `void'");
2655
2656 is_void = true;
Eric Anholt068c80c2010-03-31 09:56:36 -10002657 return NULL;
Ian Romanickcf37c9e2010-04-02 15:30:45 -07002658 }
Eric Anholt068c80c2010-03-31 09:56:36 -10002659
Ian Romanick45d8a702010-04-02 15:09:33 -07002660 if (formal_parameter && (this->identifier == NULL)) {
2661 _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
2662 return NULL;
2663 }
2664
Kenneth Graunkee511a352010-08-21 15:30:34 -07002665 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
2666 * call already handled the "vec4[..] foo" case.
2667 */
2668 if (this->is_array) {
Kenneth Graunked8e34e22010-08-07 02:56:01 -07002669 type = process_array_type(&loc, type, this->array_size, state);
Kenneth Graunkee511a352010-08-21 15:30:34 -07002670 }
2671
2672 if (type->array_size() == 0) {
2673 _mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
2674 "a declared size.");
2675 type = glsl_type::error_type;
2676 }
2677
Ian Romanickcf37c9e2010-04-02 15:30:45 -07002678 is_void = false;
Ian Romanick7e2aa912010-07-19 17:12:42 -07002679 ir_variable *var = new(ctx) ir_variable(type, this->identifier, ir_var_in);
Ian Romanicka87ac252010-02-22 13:19:34 -08002680
Ian Romanickcdb8d542010-03-11 14:48:51 -08002681 /* Apply any specified qualifiers to the parameter declaration. Note that
2682 * for function parameters the default mode is 'in'.
2683 */
Eric Anholt2e063f12010-03-28 00:56:22 -07002684 apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -08002685
Ian Romanick0044e7e2010-03-08 23:44:00 -08002686 instructions->push_tail(var);
Ian Romanicka87ac252010-02-22 13:19:34 -08002687
2688 /* Parameter declarations do not have r-values.
2689 */
2690 return NULL;
2691}
2692
2693
Ian Romanick45d8a702010-04-02 15:09:33 -07002694void
Ian Romanick304ea902010-05-10 11:17:53 -07002695ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
Ian Romanick45d8a702010-04-02 15:09:33 -07002696 bool formal,
2697 exec_list *ir_parameters,
2698 _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08002699{
Ian Romanickcf37c9e2010-04-02 15:30:45 -07002700 ast_parameter_declarator *void_param = NULL;
2701 unsigned count = 0;
Ian Romanicka87ac252010-02-22 13:19:34 -08002702
Ian Romanick2b97dc62010-05-10 17:42:05 -07002703 foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
Ian Romanick45d8a702010-04-02 15:09:33 -07002704 param->formal_parameter = formal;
Eric Anholt068c80c2010-03-31 09:56:36 -10002705 param->hir(ir_parameters, state);
Ian Romanickcf37c9e2010-04-02 15:30:45 -07002706
2707 if (param->is_void)
2708 void_param = param;
2709
2710 count++;
2711 }
2712
2713 if ((void_param != NULL) && (count > 1)) {
2714 YYLTYPE loc = void_param->get_location();
2715
2716 _mesa_glsl_error(& loc, state,
2717 "`void' parameter must be only parameter");
Ian Romanicka87ac252010-02-22 13:19:34 -08002718 }
2719}
2720
2721
Kenneth Graunke6fae1e42010-12-06 10:54:05 -08002722void
2723emit_function(_mesa_glsl_parse_state *state, exec_list *instructions,
2724 ir_function *f)
2725{
2726 /* Emit the new function header */
2727 if (state->current_function == NULL) {
2728 instructions->push_tail(f);
2729 } else {
2730 /* IR invariants disallow function declarations or definitions nested
2731 * within other function definitions. Insert the new ir_function
2732 * block in the instruction sequence before the ir_function block
2733 * containing the current ir_function_signature.
2734 */
2735 ir_function *const curr =
2736 const_cast<ir_function *>(state->current_function->function());
2737
2738 curr->insert_before(f);
2739 }
2740}
2741
2742
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07002743ir_rvalue *
Ian Romanick92318a92010-03-31 18:23:21 -07002744ast_function::hir(exec_list *instructions,
2745 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08002746{
Kenneth Graunke953ff122010-06-25 13:14:37 -07002747 void *ctx = state;
Ian Romanick18238de2010-03-01 13:49:10 -08002748 ir_function *f = NULL;
Ian Romanick92318a92010-03-31 18:23:21 -07002749 ir_function_signature *sig = NULL;
2750 exec_list hir_parameters;
Ian Romanicka87ac252010-02-22 13:19:34 -08002751
Kenneth Graunkeac04c252010-06-29 00:48:10 -07002752 const char *const name = identifier;
Ian Romanicka87ac252010-02-22 13:19:34 -08002753
Ian Romanick63b80f82010-09-01 06:34:58 -07002754 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
2755 *
2756 * "Function declarations (prototypes) cannot occur inside of functions;
2757 * they must be at global scope, or for the built-in functions, outside
2758 * the global scope."
2759 *
2760 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
2761 *
2762 * "User defined functions may only be defined within the global scope."
2763 *
2764 * Note that this language does not appear in GLSL 1.10.
2765 */
2766 if ((state->current_function != NULL) && (state->language_version != 110)) {
2767 YYLTYPE loc = this->get_location();
2768 _mesa_glsl_error(&loc, state,
2769 "declaration of function `%s' not allowed within "
2770 "function body", name);
2771 }
2772
Kenneth Graunkeedd180f2010-08-20 02:14:35 -07002773 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
2774 *
2775 * "Identifiers starting with "gl_" are reserved for use by
2776 * OpenGL, and may not be declared in a shader as either a
2777 * variable or a function."
2778 */
2779 if (strncmp(name, "gl_", 3) == 0) {
2780 YYLTYPE loc = this->get_location();
2781 _mesa_glsl_error(&loc, state,
2782 "identifier `%s' uses reserved `gl_' prefix", name);
2783 }
2784
Ian Romanicka87ac252010-02-22 13:19:34 -08002785 /* Convert the list of function parameters to HIR now so that they can be
2786 * used below to compare this function's signature with previously seen
2787 * signatures for functions with the same name.
2788 */
Ian Romanick45d8a702010-04-02 15:09:33 -07002789 ast_parameter_declarator::parameters_to_hir(& this->parameters,
2790 is_definition,
2791 & hir_parameters, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08002792
Ian Romanicke39cc692010-03-23 12:19:13 -07002793 const char *return_type_name;
2794 const glsl_type *return_type =
Ian Romanick92318a92010-03-31 18:23:21 -07002795 this->return_type->specifier->glsl_type(& return_type_name, state);
Ian Romanicke39cc692010-03-23 12:19:13 -07002796
Eric Anholt76e96d72010-08-23 13:26:52 -07002797 if (!return_type) {
2798 YYLTYPE loc = this->get_location();
2799 _mesa_glsl_error(&loc, state,
2800 "function `%s' has undeclared return type `%s'",
2801 name, return_type_name);
2802 return_type = glsl_type::error_type;
2803 }
Ian Romanicke39cc692010-03-23 12:19:13 -07002804
Kenneth Graunkeac04c252010-06-29 00:48:10 -07002805 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
2806 * "No qualifier is allowed on the return type of a function."
2807 */
2808 if (this->return_type->has_qualifiers()) {
2809 YYLTYPE loc = this->get_location();
2810 _mesa_glsl_error(& loc, state,
2811 "function `%s' return type has qualifiers", name);
2812 }
2813
Ian Romanicka87ac252010-02-22 13:19:34 -08002814 /* Verify that this function's signature either doesn't match a previously
2815 * seen signature for a function with the same name, or, if a match is found,
2816 * that the previously seen signature does not have an associated definition.
2817 */
Ian Romanicke466b182010-09-01 14:16:53 -07002818 f = state->symbols->get_function(name);
Kenneth Graunke81f03392010-09-16 02:52:25 -07002819 if (f != NULL && (state->es_shader || f->has_user_signature())) {
Ian Romanick202604e2010-08-11 16:58:25 -07002820 sig = f->exact_matching_signature(&hir_parameters);
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07002821 if (sig != NULL) {
2822 const char *badvar = sig->qualifiers_match(&hir_parameters);
2823 if (badvar != NULL) {
2824 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08002825
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07002826 _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
2827 "qualifiers don't match prototype", name, badvar);
Ian Romanicka87ac252010-02-22 13:19:34 -08002828 }
2829
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07002830 if (sig->return_type != return_type) {
2831 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08002832
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07002833 _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
2834 "match prototype", name);
2835 }
2836
2837 if (is_definition && sig->is_defined) {
2838 YYLTYPE loc = this->get_location();
2839
2840 _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07002841 }
2842 }
Ian Romanicka87ac252010-02-22 13:19:34 -08002843 } else {
Carl Worth1660a292010-06-23 18:11:51 -07002844 f = new(ctx) ir_function(name);
Eric Anholte8f5ebf2010-11-05 06:08:45 -07002845 if (!state->symbols->add_function(f)) {
Kenneth Graunkee0959132010-08-25 16:37:46 -07002846 /* This function name shadows a non-function use of the same name. */
2847 YYLTYPE loc = this->get_location();
2848
2849 _mesa_glsl_error(&loc, state, "function name `%s' conflicts with "
2850 "non-function", name);
2851 return NULL;
2852 }
Kenneth Graunke9fa99f32010-04-21 12:30:22 -07002853
Kenneth Graunke6fae1e42010-12-06 10:54:05 -08002854 emit_function(state, instructions, f);
Ian Romanicka87ac252010-02-22 13:19:34 -08002855 }
2856
Eric Anholtab372da2010-03-28 01:24:55 -07002857 /* Verify the return type of main() */
2858 if (strcmp(name, "main") == 0) {
Ian Romanick25711a82010-03-31 17:39:10 -07002859 if (! return_type->is_void()) {
Eric Anholtab372da2010-03-28 01:24:55 -07002860 YYLTYPE loc = this->get_location();
2861
2862 _mesa_glsl_error(& loc, state, "main() must return void");
2863 }
Eric Anholt174cc032010-03-30 23:37:51 -10002864
Ian Romanick92318a92010-03-31 18:23:21 -07002865 if (!hir_parameters.is_empty()) {
Eric Anholt174cc032010-03-30 23:37:51 -10002866 YYLTYPE loc = this->get_location();
2867
2868 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
2869 }
Eric Anholtab372da2010-03-28 01:24:55 -07002870 }
Ian Romanicka87ac252010-02-22 13:19:34 -08002871
2872 /* Finish storing the information about this new function in its signature.
2873 */
Ian Romanick92318a92010-03-31 18:23:21 -07002874 if (sig == NULL) {
Carl Worth1660a292010-06-23 18:11:51 -07002875 sig = new(ctx) ir_function_signature(return_type);
Ian Romanick92318a92010-03-31 18:23:21 -07002876 f->add_signature(sig);
Ian Romanicka87ac252010-02-22 13:19:34 -08002877 }
2878
Kenneth Graunkebff60132010-04-28 12:44:24 -07002879 sig->replace_parameters(&hir_parameters);
Ian Romanick92318a92010-03-31 18:23:21 -07002880 signature = sig;
Ian Romanicke29a5852010-03-31 17:54:26 -07002881
Ian Romanick92318a92010-03-31 18:23:21 -07002882 /* Function declarations (prototypes) do not have r-values.
2883 */
2884 return NULL;
2885}
2886
2887
2888ir_rvalue *
2889ast_function_definition::hir(exec_list *instructions,
2890 struct _mesa_glsl_parse_state *state)
2891{
2892 prototype->is_definition = true;
2893 prototype->hir(instructions, state);
2894
2895 ir_function_signature *signature = prototype->signature;
Kenneth Graunke826a39c2010-08-20 02:04:52 -07002896 if (signature == NULL)
2897 return NULL;
Ian Romanicka87ac252010-02-22 13:19:34 -08002898
Ian Romanick41ec6a42010-03-19 17:08:05 -07002899 assert(state->current_function == NULL);
2900 state->current_function = signature;
Kenneth Graunke6de82562010-06-29 09:59:40 -07002901 state->found_return = false;
Ian Romanick41ec6a42010-03-19 17:08:05 -07002902
Ian Romanicke29a5852010-03-31 17:54:26 -07002903 /* Duplicate parameters declared in the prototype as concrete variables.
2904 * Add these to the symbol table.
Ian Romanicka87ac252010-02-22 13:19:34 -08002905 */
Ian Romanick8bde4ce2010-03-19 11:57:24 -07002906 state->symbols->push_scope();
Ian Romanicke29a5852010-03-31 17:54:26 -07002907 foreach_iter(exec_list_iterator, iter, signature->parameters) {
Eric Anholtfbc7c0b2010-04-07 14:32:53 -07002908 ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
Ian Romanicka87ac252010-02-22 13:19:34 -08002909
Eric Anholtfbc7c0b2010-04-07 14:32:53 -07002910 assert(var != NULL);
Ian Romanicka87ac252010-02-22 13:19:34 -08002911
Ian Romanick3359e582010-03-19 15:38:52 -07002912 /* The only way a parameter would "exist" is if two parameters have
2913 * the same name.
2914 */
2915 if (state->symbols->name_declared_this_scope(var->name)) {
2916 YYLTYPE loc = this->get_location();
2917
2918 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
2919 } else {
Eric Anholt001eee52010-11-05 06:11:24 -07002920 state->symbols->add_variable(var);
Ian Romanick3359e582010-03-19 15:38:52 -07002921 }
Ian Romanicka87ac252010-02-22 13:19:34 -08002922 }
2923
Kenneth Graunke9fa99f32010-04-21 12:30:22 -07002924 /* Convert the body of the function to HIR. */
Eric Anholt894ea972010-04-07 13:19:11 -07002925 this->body->hir(&signature->body, state);
Kenneth Graunke9fa99f32010-04-21 12:30:22 -07002926 signature->is_defined = true;
Ian Romanicka87ac252010-02-22 13:19:34 -08002927
Ian Romanick8bde4ce2010-03-19 11:57:24 -07002928 state->symbols->pop_scope();
Ian Romanicka87ac252010-02-22 13:19:34 -08002929
Ian Romanick41ec6a42010-03-19 17:08:05 -07002930 assert(state->current_function == signature);
2931 state->current_function = NULL;
Ian Romanicka87ac252010-02-22 13:19:34 -08002932
Kenneth Graunke6de82562010-06-29 09:59:40 -07002933 if (!signature->return_type->is_void() && !state->found_return) {
2934 YYLTYPE loc = this->get_location();
2935 _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
2936 "%s, but no return statement",
2937 signature->function_name(),
2938 signature->return_type->name);
2939 }
2940
Ian Romanicka87ac252010-02-22 13:19:34 -08002941 /* Function definitions do not have r-values.
2942 */
2943 return NULL;
2944}
Ian Romanick16a246c2010-03-19 16:45:19 -07002945
2946
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07002947ir_rvalue *
Ian Romanick16a246c2010-03-19 16:45:19 -07002948ast_jump_statement::hir(exec_list *instructions,
2949 struct _mesa_glsl_parse_state *state)
2950{
Kenneth Graunke953ff122010-06-25 13:14:37 -07002951 void *ctx = state;
Ian Romanick16a246c2010-03-19 16:45:19 -07002952
Ian Romanickc0e76d82010-04-05 16:53:19 -07002953 switch (mode) {
2954 case ast_return: {
Ian Romanick16a246c2010-03-19 16:45:19 -07002955 ir_return *inst;
Eric Anholtaad7c772010-03-30 23:28:20 -10002956 assert(state->current_function);
Ian Romanick16a246c2010-03-19 16:45:19 -07002957
2958 if (opt_return_value) {
Eric Anholtab79d4e2010-03-30 23:23:16 -10002959 if (state->current_function->return_type->base_type ==
2960 GLSL_TYPE_VOID) {
2961 YYLTYPE loc = this->get_location();
2962
2963 _mesa_glsl_error(& loc, state,
2964 "`return` with a value, in function `%s' "
2965 "returning void",
Kenneth Graunkef96c52b2010-04-21 15:17:26 -07002966 state->current_function->function_name());
Eric Anholtab79d4e2010-03-30 23:23:16 -10002967 }
Ian Romanick16a246c2010-03-19 16:45:19 -07002968
Chad Versaceb4cdba62010-11-17 10:28:01 -08002969 ir_rvalue *const ret = opt_return_value->hir(instructions, state);
Ian Romanick16a246c2010-03-19 16:45:19 -07002970 assert(ret != NULL);
2971
Kenneth Graunke18707eb2010-06-28 23:38:04 -07002972 /* Implicit conversions are not allowed for return values. */
2973 if (state->current_function->return_type != ret->type) {
2974 YYLTYPE loc = this->get_location();
2975
2976 _mesa_glsl_error(& loc, state,
2977 "`return' with wrong type %s, in function `%s' "
2978 "returning %s",
2979 ret->type->name,
2980 state->current_function->function_name(),
2981 state->current_function->return_type->name);
2982 }
Ian Romanick16a246c2010-03-19 16:45:19 -07002983
Carl Worth1660a292010-06-23 18:11:51 -07002984 inst = new(ctx) ir_return(ret);
Ian Romanick16a246c2010-03-19 16:45:19 -07002985 } else {
Eric Anholtaad7c772010-03-30 23:28:20 -10002986 if (state->current_function->return_type->base_type !=
2987 GLSL_TYPE_VOID) {
2988 YYLTYPE loc = this->get_location();
2989
2990 _mesa_glsl_error(& loc, state,
2991 "`return' with no value, in function %s returning "
2992 "non-void",
Kenneth Graunkef96c52b2010-04-21 15:17:26 -07002993 state->current_function->function_name());
Eric Anholtaad7c772010-03-30 23:28:20 -10002994 }
Carl Worth1660a292010-06-23 18:11:51 -07002995 inst = new(ctx) ir_return;
Ian Romanick16a246c2010-03-19 16:45:19 -07002996 }
2997
Kenneth Graunke6de82562010-06-29 09:59:40 -07002998 state->found_return = true;
Ian Romanick16a246c2010-03-19 16:45:19 -07002999 instructions->push_tail(inst);
Ian Romanickc0e76d82010-04-05 16:53:19 -07003000 break;
Ian Romanick16a246c2010-03-19 16:45:19 -07003001 }
3002
Ian Romanickc0e76d82010-04-05 16:53:19 -07003003 case ast_discard:
Eric Anholtb9802072010-03-30 23:40:14 -10003004 if (state->target != fragment_shader) {
3005 YYLTYPE loc = this->get_location();
3006
3007 _mesa_glsl_error(& loc, state,
3008 "`discard' may only appear in a fragment shader");
3009 }
Kenneth Graunke77049a72010-06-30 14:11:00 -07003010 instructions->push_tail(new(ctx) ir_discard);
Ian Romanickc0e76d82010-04-05 16:53:19 -07003011 break;
3012
3013 case ast_break:
3014 case ast_continue:
Ian Romanick4cf20cd2010-04-05 17:13:47 -07003015 /* FINISHME: Handle switch-statements. They cannot contain 'continue',
3016 * FINISHME: and they use a different IR instruction for 'break'.
3017 */
3018 /* FINISHME: Correctly handle the nesting. If a switch-statement is
3019 * FINISHME: inside a loop, a 'continue' is valid and will bind to the
3020 * FINISHME: loop.
3021 */
3022 if (state->loop_or_switch_nesting == NULL) {
3023 YYLTYPE loc = this->get_location();
3024
3025 _mesa_glsl_error(& loc, state,
3026 "`%s' may only appear in a loop",
3027 (mode == ast_break) ? "break" : "continue");
3028 } else {
3029 ir_loop *const loop = state->loop_or_switch_nesting->as_loop();
3030
Eric Anholt2d1ed7b2010-07-22 12:55:16 -07003031 /* Inline the for loop expression again, since we don't know
3032 * where near the end of the loop body the normal copy of it
3033 * is going to be placed.
3034 */
3035 if (mode == ast_continue &&
3036 state->loop_or_switch_nesting_ast->rest_expression) {
3037 state->loop_or_switch_nesting_ast->rest_expression->hir(instructions,
3038 state);
3039 }
3040
Ian Romanick4cf20cd2010-04-05 17:13:47 -07003041 if (loop != NULL) {
3042 ir_loop_jump *const jump =
Carl Worth1660a292010-06-23 18:11:51 -07003043 new(ctx) ir_loop_jump((mode == ast_break)
3044 ? ir_loop_jump::jump_break
3045 : ir_loop_jump::jump_continue);
Ian Romanick4cf20cd2010-04-05 17:13:47 -07003046 instructions->push_tail(jump);
3047 }
3048 }
3049
Ian Romanickc0e76d82010-04-05 16:53:19 -07003050 break;
Eric Anholtb9802072010-03-30 23:40:14 -10003051 }
3052
Ian Romanick16a246c2010-03-19 16:45:19 -07003053 /* Jump instructions do not have r-values.
3054 */
3055 return NULL;
3056}
Ian Romanick3c6fea32010-03-29 14:11:25 -07003057
3058
3059ir_rvalue *
3060ast_selection_statement::hir(exec_list *instructions,
3061 struct _mesa_glsl_parse_state *state)
3062{
Kenneth Graunke953ff122010-06-25 13:14:37 -07003063 void *ctx = state;
Carl Worth1660a292010-06-23 18:11:51 -07003064
Ian Romanick3c6fea32010-03-29 14:11:25 -07003065 ir_rvalue *const condition = this->condition->hir(instructions, state);
Ian Romanick3c6fea32010-03-29 14:11:25 -07003066
3067 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
3068 *
3069 * "Any expression whose type evaluates to a Boolean can be used as the
3070 * conditional expression bool-expression. Vector types are not accepted
3071 * as the expression to if."
3072 *
3073 * The checks are separated so that higher quality diagnostics can be
3074 * generated for cases where both rules are violated.
3075 */
3076 if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
3077 YYLTYPE loc = this->condition->get_location();
3078
3079 _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
3080 "boolean");
3081 }
3082
Carl Worth1660a292010-06-23 18:11:51 -07003083 ir_if *const stmt = new(ctx) ir_if(condition);
Ian Romanick3c6fea32010-03-29 14:11:25 -07003084
Kenneth Graunke665d75c2010-08-18 13:54:50 -07003085 if (then_statement != NULL) {
3086 state->symbols->push_scope();
Ian Romanick4f9d72f2010-05-10 11:10:26 -07003087 then_statement->hir(& stmt->then_instructions, state);
Kenneth Graunke665d75c2010-08-18 13:54:50 -07003088 state->symbols->pop_scope();
3089 }
Ian Romanick3c6fea32010-03-29 14:11:25 -07003090
Kenneth Graunke665d75c2010-08-18 13:54:50 -07003091 if (else_statement != NULL) {
3092 state->symbols->push_scope();
Ian Romanick4f9d72f2010-05-10 11:10:26 -07003093 else_statement->hir(& stmt->else_instructions, state);
Kenneth Graunke665d75c2010-08-18 13:54:50 -07003094 state->symbols->pop_scope();
3095 }
Ian Romanick3c6fea32010-03-29 14:11:25 -07003096
3097 instructions->push_tail(stmt);
3098
3099 /* if-statements do not have r-values.
3100 */
3101 return NULL;
3102}
Ian Romanick9e7d0102010-04-05 16:37:49 -07003103
3104
Ian Romanick8c46ed22010-04-05 18:07:27 -07003105void
3106ast_iteration_statement::condition_to_hir(ir_loop *stmt,
3107 struct _mesa_glsl_parse_state *state)
Ian Romanick9e7d0102010-04-05 16:37:49 -07003108{
Kenneth Graunke953ff122010-06-25 13:14:37 -07003109 void *ctx = state;
Carl Worth1660a292010-06-23 18:11:51 -07003110
Ian Romanick9e7d0102010-04-05 16:37:49 -07003111 if (condition != NULL) {
3112 ir_rvalue *const cond =
3113 condition->hir(& stmt->body_instructions, state);
3114
3115 if ((cond == NULL)
3116 || !cond->type->is_boolean() || !cond->type->is_scalar()) {
3117 YYLTYPE loc = condition->get_location();
3118
3119 _mesa_glsl_error(& loc, state,
3120 "loop condition must be scalar boolean");
3121 } else {
3122 /* As the first code in the loop body, generate a block that looks
3123 * like 'if (!condition) break;' as the loop termination condition.
3124 */
3125 ir_rvalue *const not_cond =
Carl Worth1660a292010-06-23 18:11:51 -07003126 new(ctx) ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond,
3127 NULL);
Ian Romanick9e7d0102010-04-05 16:37:49 -07003128
Carl Worth1660a292010-06-23 18:11:51 -07003129 ir_if *const if_stmt = new(ctx) ir_if(not_cond);
Ian Romanick9e7d0102010-04-05 16:37:49 -07003130
3131 ir_jump *const break_stmt =
Carl Worth1660a292010-06-23 18:11:51 -07003132 new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
Ian Romanick9e7d0102010-04-05 16:37:49 -07003133
3134 if_stmt->then_instructions.push_tail(break_stmt);
3135 stmt->body_instructions.push_tail(if_stmt);
3136 }
3137 }
Ian Romanick8c46ed22010-04-05 18:07:27 -07003138}
3139
3140
3141ir_rvalue *
3142ast_iteration_statement::hir(exec_list *instructions,
3143 struct _mesa_glsl_parse_state *state)
3144{
Kenneth Graunke953ff122010-06-25 13:14:37 -07003145 void *ctx = state;
Carl Worth1660a292010-06-23 18:11:51 -07003146
Ian Romanick48460662010-04-16 16:42:43 -07003147 /* For-loops and while-loops start a new scope, but do-while loops do not.
Ian Romanick8c46ed22010-04-05 18:07:27 -07003148 */
Ian Romanick48460662010-04-16 16:42:43 -07003149 if (mode != ast_do_while)
Ian Romanick8c46ed22010-04-05 18:07:27 -07003150 state->symbols->push_scope();
3151
3152 if (init_statement != NULL)
3153 init_statement->hir(instructions, state);
3154
Carl Worth1660a292010-06-23 18:11:51 -07003155 ir_loop *const stmt = new(ctx) ir_loop();
Ian Romanick8c46ed22010-04-05 18:07:27 -07003156 instructions->push_tail(stmt);
3157
3158 /* Track the current loop and / or switch-statement nesting.
3159 */
3160 ir_instruction *const nesting = state->loop_or_switch_nesting;
Eric Anholt2d1ed7b2010-07-22 12:55:16 -07003161 ast_iteration_statement *nesting_ast = state->loop_or_switch_nesting_ast;
3162
Ian Romanick8c46ed22010-04-05 18:07:27 -07003163 state->loop_or_switch_nesting = stmt;
Eric Anholt2d1ed7b2010-07-22 12:55:16 -07003164 state->loop_or_switch_nesting_ast = this;
Ian Romanick8c46ed22010-04-05 18:07:27 -07003165
3166 if (mode != ast_do_while)
3167 condition_to_hir(stmt, state);
Ian Romanick9e7d0102010-04-05 16:37:49 -07003168
Ian Romanick4f9d72f2010-05-10 11:10:26 -07003169 if (body != NULL)
3170 body->hir(& stmt->body_instructions, state);
Ian Romanick9e7d0102010-04-05 16:37:49 -07003171
3172 if (rest_expression != NULL)
3173 rest_expression->hir(& stmt->body_instructions, state);
3174
Ian Romanick8c46ed22010-04-05 18:07:27 -07003175 if (mode == ast_do_while)
3176 condition_to_hir(stmt, state);
3177
Ian Romanick48460662010-04-16 16:42:43 -07003178 if (mode != ast_do_while)
Ian Romanick9e7d0102010-04-05 16:37:49 -07003179 state->symbols->pop_scope();
3180
Ian Romanicke9d0f262010-04-05 17:01:53 -07003181 /* Restore previous nesting before returning.
3182 */
3183 state->loop_or_switch_nesting = nesting;
Eric Anholt2d1ed7b2010-07-22 12:55:16 -07003184 state->loop_or_switch_nesting_ast = nesting_ast;
Ian Romanicke9d0f262010-04-05 17:01:53 -07003185
Ian Romanick9e7d0102010-04-05 16:37:49 -07003186 /* Loops do not have r-values.
3187 */
3188 return NULL;
3189}
Ian Romanick3455ce62010-04-19 15:13:15 -07003190
3191
3192ir_rvalue *
3193ast_type_specifier::hir(exec_list *instructions,
3194 struct _mesa_glsl_parse_state *state)
3195{
3196 if (this->structure != NULL)
3197 return this->structure->hir(instructions, state);
Ian Romanick85ba37b2010-04-21 14:33:34 -07003198
3199 return NULL;
Ian Romanick3455ce62010-04-19 15:13:15 -07003200}
3201
3202
3203ir_rvalue *
3204ast_struct_specifier::hir(exec_list *instructions,
3205 struct _mesa_glsl_parse_state *state)
3206{
Ian Romanick3455ce62010-04-19 15:13:15 -07003207 unsigned decl_count = 0;
3208
3209 /* Make an initial pass over the list of structure fields to determine how
3210 * many there are. Each element in this list is an ast_declarator_list.
3211 * This means that we actually need to count the number of elements in the
3212 * 'declarations' list in each of the elements.
3213 */
Ian Romanick2b97dc62010-05-10 17:42:05 -07003214 foreach_list_typed (ast_declarator_list, decl_list, link,
3215 &this->declarations) {
Ian Romanick304ea902010-05-10 11:17:53 -07003216 foreach_list_const (decl_ptr, & decl_list->declarations) {
Ian Romanick3455ce62010-04-19 15:13:15 -07003217 decl_count++;
3218 }
3219 }
3220
Ian Romanick3455ce62010-04-19 15:13:15 -07003221 /* Allocate storage for the structure fields and process the field
3222 * declarations. As the declarations are processed, try to also convert
3223 * the types to HIR. This ensures that structure definitions embedded in
3224 * other structure definitions are processed.
3225 */
Eric Anholt21b0dbd2010-07-20 16:47:25 -07003226 glsl_struct_field *const fields = talloc_array(state, glsl_struct_field,
3227 decl_count);
Ian Romanick3455ce62010-04-19 15:13:15 -07003228
3229 unsigned i = 0;
Ian Romanick2b97dc62010-05-10 17:42:05 -07003230 foreach_list_typed (ast_declarator_list, decl_list, link,
3231 &this->declarations) {
Ian Romanick3455ce62010-04-19 15:13:15 -07003232 const char *type_name;
3233
3234 decl_list->type->specifier->hir(instructions, state);
3235
Kenneth Graunkec98deb12010-08-16 14:02:25 -07003236 /* Section 10.9 of the GLSL ES 1.00 specification states that
3237 * embedded structure definitions have been removed from the language.
3238 */
3239 if (state->es_shader && decl_list->type->specifier->structure != NULL) {
3240 YYLTYPE loc = this->get_location();
3241 _mesa_glsl_error(&loc, state, "Embedded structure definitions are "
3242 "not allowed in GLSL ES 1.00.");
3243 }
3244
Ian Romanick3455ce62010-04-19 15:13:15 -07003245 const glsl_type *decl_type =
3246 decl_list->type->specifier->glsl_type(& type_name, state);
3247
Ian Romanick2b97dc62010-05-10 17:42:05 -07003248 foreach_list_typed (ast_declaration, decl, link,
3249 &decl_list->declarations) {
Kenneth Graunked8e34e22010-08-07 02:56:01 -07003250 const struct glsl_type *field_type = decl_type;
3251 if (decl->is_array) {
3252 YYLTYPE loc = decl->get_location();
3253 field_type = process_array_type(&loc, decl_type, decl->array_size,
3254 state);
3255 }
Ian Romanick73986a72010-04-20 16:49:03 -07003256 fields[i].type = (field_type != NULL)
3257 ? field_type : glsl_type::error_type;
Ian Romanick3455ce62010-04-19 15:13:15 -07003258 fields[i].name = decl->identifier;
3259 i++;
3260 }
3261 }
3262
3263 assert(i == decl_count);
3264
Ian Romanick49e35772010-06-28 11:54:57 -07003265 const glsl_type *t =
Kenneth Graunkeca92ae22010-09-18 11:11:09 +02003266 glsl_type::get_record_instance(fields, decl_count, this->name);
Ian Romanick1d28b612010-04-20 16:48:24 -07003267
Ian Romanickab899272010-04-23 13:24:08 -07003268 YYLTYPE loc = this->get_location();
Ian Romanicka789ca62010-09-01 14:08:08 -07003269 if (!state->symbols->add_type(name, t)) {
Ian Romanickab899272010-04-23 13:24:08 -07003270 _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
3271 } else {
Ian Romanicka2c6df52010-04-28 13:14:53 -07003272
3273 const glsl_type **s = (const glsl_type **)
3274 realloc(state->user_structures,
3275 sizeof(state->user_structures[0]) *
3276 (state->num_user_structures + 1));
3277 if (s != NULL) {
3278 s[state->num_user_structures] = t;
3279 state->user_structures = s;
3280 state->num_user_structures++;
3281 }
Ian Romanickab899272010-04-23 13:24:08 -07003282 }
Ian Romanick3455ce62010-04-19 15:13:15 -07003283
3284 /* Structure type definitions do not have r-values.
3285 */
3286 return NULL;
3287}