blob: 4a04b608bfb9f4cf1e4b76cd16a700d0928e98d7 [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"
Eric Anholt14063212012-01-30 08:50:14 -080057#include "program/hash_table.h"
Ian Romanicka87ac252010-02-22 13:19:34 -080058#include "ir.h"
59
Eric Anholt4b2a4cb2012-03-29 17:29:20 -070060static void
61detect_conflicting_assignments(struct _mesa_glsl_parse_state *state,
62 exec_list *instructions);
63
Ian Romanickd949a9a2010-03-10 09:55:22 -080064void
65_mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
66{
Ian Romanickadfb0cd2010-03-10 10:43:16 -080067 _mesa_glsl_initialize_variables(instructions, state);
68
Paul Berry53e572f2012-08-01 17:44:02 -070069 state->symbols->separate_function_namespace = state->language_version == 110;
Kenneth Graunke814c89a2010-09-05 00:31:28 -070070
Ian Romanick41ec6a42010-03-19 17:08:05 -070071 state->current_function = NULL;
72
Paul Berry0d81b0e2011-07-29 15:28:52 -070073 state->toplevel_ir = instructions;
74
Kenneth Graunkea0442852010-08-23 14:52:06 -070075 /* Section 4.2 of the GLSL 1.20 specification states:
76 * "The built-in functions are scoped in a scope outside the global scope
77 * users declare global variables in. That is, a shader's global scope,
78 * available for user-defined functions and global variables, is nested
79 * inside the scope containing the built-in functions."
80 *
81 * Since built-in functions like ftransform() access built-in variables,
82 * it follows that those must be in the outer scope as well.
83 *
84 * We push scope here to create this nesting effect...but don't pop.
85 * This way, a shader's globals are still in the symbol table for use
86 * by the linker.
87 */
88 state->symbols->push_scope();
89
Ian Romanick2b97dc62010-05-10 17:42:05 -070090 foreach_list_typed (ast_node, ast, link, & state->translation_unit)
Ian Romanick304ea902010-05-10 11:17:53 -070091 ast->hir(instructions, state);
Ian Romanick02c5ae12011-07-11 10:46:01 -070092
93 detect_recursion_unlinked(state, instructions);
Eric Anholt4b2a4cb2012-03-29 17:29:20 -070094 detect_conflicting_assignments(state, instructions);
Paul Berry0d81b0e2011-07-29 15:28:52 -070095
96 state->toplevel_ir = NULL;
Ian Romanickc170c902013-06-07 17:05:22 -070097
98 /* Move all of the variable declarations to the front of the IR list, and
99 * reverse the order. This has the (intended!) side effect that vertex
100 * shader inputs and fragment shader outputs will appear in the IR in the
101 * same order that they appeared in the shader code. This results in the
102 * locations being assigned in the declared order. Many (arguably buggy)
103 * applications depend on this behavior, and it matches what nearly all
104 * other drivers do.
105 */
106 foreach_list_safe(node, instructions) {
107 ir_variable *const var = ((ir_instruction *) node)->as_variable();
108
109 if (var == NULL)
110 continue;
111
112 var->remove();
113 instructions->push_head(var);
114 }
Ian Romanickd949a9a2010-03-10 09:55:22 -0800115}
116
117
Ian Romanick01045362010-03-29 16:17:56 -0700118/**
119 * If a conversion is available, convert one operand to a different type
120 *
121 * The \c from \c ir_rvalue is converted "in place".
122 *
123 * \param to Type that the operand it to be converted to
124 * \param from Operand that is being converted
125 * \param state GLSL compiler state
126 *
127 * \return
128 * If a conversion is possible (or unnecessary), \c true is returned.
129 * Otherwise \c false is returned.
130 */
Kenneth Graunkef32d3df2010-09-01 20:03:17 -0700131bool
Ian Romanickbfb09c22010-03-29 16:32:55 -0700132apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
Ian Romanick01045362010-03-29 16:17:56 -0700133 struct _mesa_glsl_parse_state *state)
134{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700135 void *ctx = state;
Ian Romanickbfb09c22010-03-29 16:32:55 -0700136 if (to->base_type == from->type->base_type)
Ian Romanick01045362010-03-29 16:17:56 -0700137 return true;
138
139 /* This conversion was added in GLSL 1.20. If the compilation mode is
140 * GLSL 1.10, the conversion is skipped.
141 */
Paul Berrye3ded7f2012-08-01 14:50:05 -0700142 if (!state->is_version(120, 0))
Ian Romanick01045362010-03-29 16:17:56 -0700143 return false;
144
145 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
146 *
147 * "There are no implicit array or structure conversions. For
148 * example, an array of int cannot be implicitly converted to an
149 * array of float. There are no implicit conversions between
150 * signed and unsigned integers."
151 */
152 /* FINISHME: The above comment is partially a lie. There is int/uint
153 * FINISHME: conversion for immediate constants.
154 */
Ian Romanickbfb09c22010-03-29 16:32:55 -0700155 if (!to->is_float() || !from->type->is_numeric())
Ian Romanick01045362010-03-29 16:17:56 -0700156 return false;
157
Kenneth Graunke506199b2010-06-29 15:59:27 -0700158 /* Convert to a floating point type with the same number of components
159 * as the original type - i.e. int to float, not int to vec4.
160 */
161 to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements,
162 from->type->matrix_columns);
163
Ian Romanickbfb09c22010-03-29 16:32:55 -0700164 switch (from->type->base_type) {
Ian Romanick01045362010-03-29 16:17:56 -0700165 case GLSL_TYPE_INT:
Carl Worth1660a292010-06-23 18:11:51 -0700166 from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL);
Ian Romanick01045362010-03-29 16:17:56 -0700167 break;
168 case GLSL_TYPE_UINT:
Carl Worth1660a292010-06-23 18:11:51 -0700169 from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL);
Ian Romanick01045362010-03-29 16:17:56 -0700170 break;
171 case GLSL_TYPE_BOOL:
Carl Worth1660a292010-06-23 18:11:51 -0700172 from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL);
Eric Anholtdc58b3f2010-04-02 02:13:43 -1000173 break;
Ian Romanick01045362010-03-29 16:17:56 -0700174 default:
175 assert(0);
176 }
177
178 return true;
179}
180
181
Ian Romanicka87ac252010-02-22 13:19:34 -0800182static const struct glsl_type *
Ian Romanickbfb09c22010-03-29 16:32:55 -0700183arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
Ian Romanicka87ac252010-02-22 13:19:34 -0800184 bool multiply,
Eric Anholta13bb142010-03-31 16:38:11 -1000185 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800186{
Eric Anholt336b4ad2010-05-19 10:38:37 -0700187 const glsl_type *type_a = value_a->type;
188 const glsl_type *type_b = value_b->type;
Ian Romanick01045362010-03-29 16:17:56 -0700189
Ian Romanicka87ac252010-02-22 13:19:34 -0800190 /* From GLSL 1.50 spec, page 56:
191 *
192 * "The arithmetic binary operators add (+), subtract (-),
193 * multiply (*), and divide (/) operate on integer and
194 * floating-point scalars, vectors, and matrices."
195 */
Ian Romanick60b54d92010-03-24 17:08:13 -0700196 if (!type_a->is_numeric() || !type_b->is_numeric()) {
Eric Anholta13bb142010-03-31 16:38:11 -1000197 _mesa_glsl_error(loc, state,
198 "Operands to arithmetic operators must be numeric");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700199 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800200 }
201
202
203 /* "If one operand is floating-point based and the other is
204 * not, then the conversions from Section 4.1.10 "Implicit
205 * Conversions" are applied to the non-floating-point-based operand."
Ian Romanicka87ac252010-02-22 13:19:34 -0800206 */
Ian Romanick01045362010-03-29 16:17:56 -0700207 if (!apply_implicit_conversion(type_a, value_b, state)
208 && !apply_implicit_conversion(type_b, value_a, state)) {
Eric Anholta13bb142010-03-31 16:38:11 -1000209 _mesa_glsl_error(loc, state,
210 "Could not implicitly convert operands to "
211 "arithmetic operator");
Ian Romanick01045362010-03-29 16:17:56 -0700212 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800213 }
Eric Anholt336b4ad2010-05-19 10:38:37 -0700214 type_a = value_a->type;
215 type_b = value_b->type;
216
Ian Romanicka87ac252010-02-22 13:19:34 -0800217 /* "If the operands are integer types, they must both be signed or
218 * both be unsigned."
219 *
220 * From this rule and the preceeding conversion it can be inferred that
221 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
Ian Romanick60b54d92010-03-24 17:08:13 -0700222 * The is_numeric check above already filtered out the case where either
223 * type is not one of these, so now the base types need only be tested for
224 * equality.
Ian Romanicka87ac252010-02-22 13:19:34 -0800225 */
226 if (type_a->base_type != type_b->base_type) {
Eric Anholta13bb142010-03-31 16:38:11 -1000227 _mesa_glsl_error(loc, state,
228 "base type mismatch for arithmetic operator");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700229 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800230 }
231
232 /* "All arithmetic binary operators result in the same fundamental type
233 * (signed integer, unsigned integer, or floating-point) as the
234 * operands they operate on, after operand type conversion. After
235 * conversion, the following cases are valid
236 *
237 * * The two operands are scalars. In this case the operation is
238 * applied, resulting in a scalar."
239 */
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800240 if (type_a->is_scalar() && type_b->is_scalar())
Ian Romanicka87ac252010-02-22 13:19:34 -0800241 return type_a;
242
243 /* "* One operand is a scalar, and the other is a vector or matrix.
244 * In this case, the scalar operation is applied independently to each
245 * component of the vector or matrix, resulting in the same size
246 * vector or matrix."
247 */
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800248 if (type_a->is_scalar()) {
249 if (!type_b->is_scalar())
Ian Romanicka87ac252010-02-22 13:19:34 -0800250 return type_b;
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800251 } else if (type_b->is_scalar()) {
Ian Romanicka87ac252010-02-22 13:19:34 -0800252 return type_a;
253 }
254
255 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
256 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
257 * handled.
258 */
Ian Romanick60b54d92010-03-24 17:08:13 -0700259 assert(!type_a->is_scalar());
260 assert(!type_b->is_scalar());
Ian Romanicka87ac252010-02-22 13:19:34 -0800261
262 /* "* The two operands are vectors of the same size. In this case, the
263 * operation is done component-wise resulting in the same size
264 * vector."
265 */
Ian Romanicka2dd22f2010-03-09 15:55:16 -0800266 if (type_a->is_vector() && type_b->is_vector()) {
Eric Anholta13bb142010-03-31 16:38:11 -1000267 if (type_a == type_b) {
268 return type_a;
269 } else {
270 _mesa_glsl_error(loc, state,
271 "vector size mismatch for arithmetic operator");
272 return glsl_type::error_type;
273 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800274 }
275
276 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
277 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
278 * <vector, vector> have been handled. At least one of the operands must
279 * be matrix. Further, since there are no integer matrix types, the base
280 * type of both operands must be float.
281 */
Ian Romanick60b54d92010-03-24 17:08:13 -0700282 assert(type_a->is_matrix() || type_b->is_matrix());
Ian Romanicka87ac252010-02-22 13:19:34 -0800283 assert(type_a->base_type == GLSL_TYPE_FLOAT);
284 assert(type_b->base_type == GLSL_TYPE_FLOAT);
285
286 /* "* The operator is add (+), subtract (-), or divide (/), and the
287 * operands are matrices with the same number of rows and the same
288 * number of columns. In this case, the operation is done component-
289 * wise resulting in the same size matrix."
290 * * The operator is multiply (*), where both operands are matrices or
291 * one operand is a vector and the other a matrix. A right vector
292 * operand is treated as a column vector and a left vector operand as a
293 * row vector. In all these cases, it is required that the number of
294 * columns of the left operand is equal to the number of rows of the
295 * right operand. Then, the multiply (*) operation does a linear
296 * algebraic multiply, yielding an object that has the same number of
297 * rows as the left operand and the same number of columns as the right
298 * operand. Section 5.10 "Vector and Matrix Operations" explains in
299 * more detail how vectors and matrices are operated on."
300 */
301 if (! multiply) {
Eric Anholta13bb142010-03-31 16:38:11 -1000302 if (type_a == type_b)
303 return type_a;
Ian Romanicka87ac252010-02-22 13:19:34 -0800304 } else {
Ian Romanickfce11502010-03-09 15:58:52 -0800305 if (type_a->is_matrix() && type_b->is_matrix()) {
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700306 /* Matrix multiply. The columns of A must match the rows of B. Given
307 * the other previously tested constraints, this means the vector type
308 * of a row from A must be the same as the vector type of a column from
309 * B.
310 */
311 if (type_a->row_type() == type_b->column_type()) {
312 /* The resulting matrix has the number of columns of matrix B and
313 * the number of rows of matrix A. We get the row count of A by
314 * looking at the size of a vector that makes up a column. The
315 * transpose (size of a row) is done for B.
316 */
Eric Anholta13bb142010-03-31 16:38:11 -1000317 const glsl_type *const type =
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700318 glsl_type::get_instance(type_a->base_type,
319 type_a->column_type()->vector_elements,
320 type_b->row_type()->vector_elements);
Eric Anholta13bb142010-03-31 16:38:11 -1000321 assert(type != glsl_type::error_type);
322
323 return type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800324 }
Ian Romanickfce11502010-03-09 15:58:52 -0800325 } else if (type_a->is_matrix()) {
Ian Romanicka87ac252010-02-22 13:19:34 -0800326 /* A is a matrix and B is a column vector. Columns of A must match
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700327 * rows of B. Given the other previously tested constraints, this
328 * means the vector type of a row from A must be the same as the
329 * vector the type of B.
Ian Romanicka87ac252010-02-22 13:19:34 -0800330 */
Carl Worth47c90b12010-07-22 14:56:14 -0700331 if (type_a->row_type() == type_b) {
332 /* The resulting vector has a number of elements equal to
333 * the number of rows of matrix A. */
334 const glsl_type *const type =
335 glsl_type::get_instance(type_a->base_type,
336 type_a->column_type()->vector_elements,
337 1);
338 assert(type != glsl_type::error_type);
339
340 return type;
341 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800342 } else {
Ian Romanickfce11502010-03-09 15:58:52 -0800343 assert(type_b->is_matrix());
Ian Romanicka87ac252010-02-22 13:19:34 -0800344
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700345 /* A is a row vector and B is a matrix. Columns of A must match rows
346 * of B. Given the other previously tested constraints, this means
347 * the type of A must be the same as the vector type of a column from
348 * B.
Ian Romanicka87ac252010-02-22 13:19:34 -0800349 */
Carl Worth47c90b12010-07-22 14:56:14 -0700350 if (type_a == type_b->column_type()) {
351 /* The resulting vector has a number of elements equal to
352 * the number of columns of matrix B. */
353 const glsl_type *const type =
354 glsl_type::get_instance(type_a->base_type,
355 type_b->row_type()->vector_elements,
356 1);
357 assert(type != glsl_type::error_type);
358
359 return type;
360 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800361 }
Eric Anholta13bb142010-03-31 16:38:11 -1000362
363 _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
364 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800365 }
366
367
368 /* "All other cases are illegal."
369 */
Eric Anholta13bb142010-03-31 16:38:11 -1000370 _mesa_glsl_error(loc, state, "type mismatch");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700371 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800372}
373
374
375static const struct glsl_type *
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000376unary_arithmetic_result_type(const struct glsl_type *type,
377 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800378{
379 /* From GLSL 1.50 spec, page 57:
380 *
381 * "The arithmetic unary operators negate (-), post- and pre-increment
382 * and decrement (-- and ++) operate on integer or floating-point
383 * values (including vectors and matrices). All unary operators work
384 * component-wise on their operands. These result with the same type
385 * they operated on."
386 */
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000387 if (!type->is_numeric()) {
388 _mesa_glsl_error(loc, state,
389 "Operands to arithmetic operators must be numeric");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700390 return glsl_type::error_type;
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000391 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800392
393 return type;
394}
395
Chad Versacecfdbf8b2010-10-15 11:28:05 -0700396/**
397 * \brief Return the result type of a bit-logic operation.
398 *
399 * If the given types to the bit-logic operator are invalid, return
400 * glsl_type::error_type.
401 *
402 * \param type_a Type of LHS of bit-logic op
403 * \param type_b Type of RHS of bit-logic op
404 */
405static const struct glsl_type *
406bit_logic_result_type(const struct glsl_type *type_a,
407 const struct glsl_type *type_b,
408 ast_operators op,
409 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
410{
Paul Berry0d9bba62012-08-05 09:57:01 -0700411 if (!state->check_bitwise_operations_allowed(loc)) {
Chad Versacecfdbf8b2010-10-15 11:28:05 -0700412 return glsl_type::error_type;
413 }
414
415 /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
416 *
417 * "The bitwise operators and (&), exclusive-or (^), and inclusive-or
418 * (|). The operands must be of type signed or unsigned integers or
419 * integer vectors."
420 */
421 if (!type_a->is_integer()) {
422 _mesa_glsl_error(loc, state, "LHS of `%s' must be an integer",
423 ast_expression::operator_string(op));
424 return glsl_type::error_type;
425 }
426 if (!type_b->is_integer()) {
427 _mesa_glsl_error(loc, state, "RHS of `%s' must be an integer",
428 ast_expression::operator_string(op));
429 return glsl_type::error_type;
430 }
431
432 /* "The fundamental types of the operands (signed or unsigned) must
433 * match,"
434 */
435 if (type_a->base_type != type_b->base_type) {
436 _mesa_glsl_error(loc, state, "operands of `%s' must have the same "
437 "base type", ast_expression::operator_string(op));
438 return glsl_type::error_type;
439 }
440
441 /* "The operands cannot be vectors of differing size." */
442 if (type_a->is_vector() &&
443 type_b->is_vector() &&
444 type_a->vector_elements != type_b->vector_elements) {
445 _mesa_glsl_error(loc, state, "operands of `%s' cannot be vectors of "
446 "different sizes", ast_expression::operator_string(op));
447 return glsl_type::error_type;
448 }
449
450 /* "If one operand is a scalar and the other a vector, the scalar is
451 * applied component-wise to the vector, resulting in the same type as
452 * the vector. The fundamental types of the operands [...] will be the
453 * resulting fundamental type."
454 */
455 if (type_a->is_scalar())
456 return type_b;
457 else
458 return type_a;
459}
Ian Romanicka87ac252010-02-22 13:19:34 -0800460
461static const struct glsl_type *
462modulus_result_type(const struct glsl_type *type_a,
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000463 const struct glsl_type *type_b,
464 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800465{
Paul Berryd4a24742012-08-02 08:18:12 -0700466 if (!state->check_version(130, 300, loc, "operator '%%' is reserved")) {
Chad Versace82f994f2011-02-04 12:18:56 -0800467 return glsl_type::error_type;
468 }
469
Ian Romanicka87ac252010-02-22 13:19:34 -0800470 /* From GLSL 1.50 spec, page 56:
471 * "The operator modulus (%) operates on signed or unsigned integers or
472 * integer vectors. The operand types must both be signed or both be
473 * unsigned."
474 */
Kenneth Graunke8eb97532011-06-14 22:21:41 -0700475 if (!type_a->is_integer()) {
476 _mesa_glsl_error(loc, state, "LHS of operator %% must be an integer.");
477 return glsl_type::error_type;
478 }
479 if (!type_b->is_integer()) {
480 _mesa_glsl_error(loc, state, "RHS of operator %% must be an integer.");
481 return glsl_type::error_type;
482 }
483 if (type_a->base_type != type_b->base_type) {
484 _mesa_glsl_error(loc, state,
485 "operands of %% must have the same base type");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700486 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800487 }
488
489 /* "The operands cannot be vectors of differing size. If one operand is
490 * a scalar and the other vector, then the scalar is applied component-
491 * wise to the vector, resulting in the same type as the vector. If both
492 * are vectors of the same size, the result is computed component-wise."
493 */
Ian Romanicka2dd22f2010-03-09 15:55:16 -0800494 if (type_a->is_vector()) {
495 if (!type_b->is_vector()
Ian Romanicka87ac252010-02-22 13:19:34 -0800496 || (type_a->vector_elements == type_b->vector_elements))
497 return type_a;
498 } else
499 return type_b;
500
501 /* "The operator modulus (%) is not defined for any other data types
502 * (non-integer types)."
503 */
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000504 _mesa_glsl_error(loc, state, "type mismatch");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700505 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800506}
507
508
509static const struct glsl_type *
Ian Romanickbfb09c22010-03-29 16:32:55 -0700510relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000511 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800512{
Eric Anholt336b4ad2010-05-19 10:38:37 -0700513 const glsl_type *type_a = value_a->type;
514 const glsl_type *type_b = value_b->type;
Ian Romanick0150f5f2010-03-29 16:20:07 -0700515
Ian Romanicka87ac252010-02-22 13:19:34 -0800516 /* From GLSL 1.50 spec, page 56:
517 * "The relational operators greater than (>), less than (<), greater
518 * than or equal (>=), and less than or equal (<=) operate only on
519 * scalar integer and scalar floating-point expressions."
520 */
Ian Romanicka6d653d2010-03-26 14:40:37 -0700521 if (!type_a->is_numeric()
522 || !type_b->is_numeric()
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800523 || !type_a->is_scalar()
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000524 || !type_b->is_scalar()) {
525 _mesa_glsl_error(loc, state,
526 "Operands to relational operators must be scalar and "
527 "numeric");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700528 return glsl_type::error_type;
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000529 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800530
531 /* "Either the operands' types must match, or the conversions from
532 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
533 * operand, after which the types must match."
Ian Romanicka87ac252010-02-22 13:19:34 -0800534 */
Ian Romanick0150f5f2010-03-29 16:20:07 -0700535 if (!apply_implicit_conversion(type_a, value_b, state)
536 && !apply_implicit_conversion(type_b, value_a, state)) {
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000537 _mesa_glsl_error(loc, state,
538 "Could not implicitly convert operands to "
539 "relational operator");
Ian Romanick0150f5f2010-03-29 16:20:07 -0700540 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800541 }
Eric Anholt336b4ad2010-05-19 10:38:37 -0700542 type_a = value_a->type;
543 type_b = value_b->type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800544
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000545 if (type_a->base_type != type_b->base_type) {
546 _mesa_glsl_error(loc, state, "base type mismatch");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700547 return glsl_type::error_type;
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000548 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800549
550 /* "The result is scalar Boolean."
551 */
Ian Romanick0471e8b2010-03-26 14:33:41 -0700552 return glsl_type::bool_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800553}
554
Chad Versacec0197ab2010-10-15 09:49:46 -0700555/**
556 * \brief Return the result type of a bit-shift operation.
557 *
558 * If the given types to the bit-shift operator are invalid, return
559 * glsl_type::error_type.
560 *
561 * \param type_a Type of LHS of bit-shift op
562 * \param type_b Type of RHS of bit-shift op
563 */
564static const struct glsl_type *
565shift_result_type(const struct glsl_type *type_a,
566 const struct glsl_type *type_b,
567 ast_operators op,
568 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
569{
Paul Berry0d9bba62012-08-05 09:57:01 -0700570 if (!state->check_bitwise_operations_allowed(loc)) {
Chad Versacec0197ab2010-10-15 09:49:46 -0700571 return glsl_type::error_type;
572 }
573
574 /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
575 *
576 * "The shift operators (<<) and (>>). For both operators, the operands
577 * must be signed or unsigned integers or integer vectors. One operand
578 * can be signed while the other is unsigned."
579 */
580 if (!type_a->is_integer()) {
581 _mesa_glsl_error(loc, state, "LHS of operator %s must be an integer or "
582 "integer vector", ast_expression::operator_string(op));
583 return glsl_type::error_type;
584
585 }
586 if (!type_b->is_integer()) {
587 _mesa_glsl_error(loc, state, "RHS of operator %s must be an integer or "
588 "integer vector", ast_expression::operator_string(op));
589 return glsl_type::error_type;
590 }
591
592 /* "If the first operand is a scalar, the second operand has to be
593 * a scalar as well."
594 */
595 if (type_a->is_scalar() && !type_b->is_scalar()) {
596 _mesa_glsl_error(loc, state, "If the first operand of %s is scalar, the "
597 "second must be scalar as well",
598 ast_expression::operator_string(op));
599 return glsl_type::error_type;
600 }
601
602 /* If both operands are vectors, check that they have same number of
603 * elements.
604 */
605 if (type_a->is_vector() &&
606 type_b->is_vector() &&
607 type_a->vector_elements != type_b->vector_elements) {
608 _mesa_glsl_error(loc, state, "Vector operands to operator %s must "
609 "have same number of elements",
610 ast_expression::operator_string(op));
611 return glsl_type::error_type;
612 }
613
614 /* "In all cases, the resulting type will be the same type as the left
615 * operand."
616 */
617 return type_a;
618}
Ian Romanicka87ac252010-02-22 13:19:34 -0800619
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700620/**
621 * Validates that a value can be assigned to a location with a specified type
622 *
623 * Validates that \c rhs can be assigned to some location. If the types are
624 * not an exact match but an automatic conversion is possible, \c rhs will be
625 * converted.
626 *
627 * \return
628 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
629 * Otherwise the actual RHS to be assigned will be returned. This may be
630 * \c rhs, or it may be \c rhs after some type conversion.
631 *
632 * \note
633 * In addition to being used for assignments, this function is used to
634 * type-check return values.
635 */
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700636ir_rvalue *
Eric Anholt336b4ad2010-05-19 10:38:37 -0700637validate_assignment(struct _mesa_glsl_parse_state *state,
Ian Romanick85caea22011-03-15 16:33:27 -0700638 const glsl_type *lhs_type, ir_rvalue *rhs,
639 bool is_initializer)
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700640{
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700641 /* If there is already some error in the RHS, just return it. Anything
642 * else will lead to an avalanche of error message back to the user.
643 */
Ian Romanickec530102010-12-10 15:47:11 -0800644 if (rhs->type->is_error())
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700645 return rhs;
646
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700647 /* If the types are identical, the assignment can trivially proceed.
648 */
Ian Romanickec530102010-12-10 15:47:11 -0800649 if (rhs->type == lhs_type)
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700650 return rhs;
651
Ian Romanick0157f412010-04-02 17:44:39 -0700652 /* If the array element types are the same and the size of the LHS is zero,
Ian Romanick85caea22011-03-15 16:33:27 -0700653 * the assignment is okay for initializers embedded in variable
654 * declarations.
Ian Romanick0157f412010-04-02 17:44:39 -0700655 *
656 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
657 * is handled by ir_dereference::is_lvalue.
658 */
Ian Romanick85caea22011-03-15 16:33:27 -0700659 if (is_initializer && lhs_type->is_array() && rhs->type->is_array()
Ian Romanick0157f412010-04-02 17:44:39 -0700660 && (lhs_type->element_type() == rhs->type->element_type())
661 && (lhs_type->array_size() == 0)) {
662 return rhs;
663 }
664
Eric Anholt336b4ad2010-05-19 10:38:37 -0700665 /* Check for implicit conversion in GLSL 1.20 */
666 if (apply_implicit_conversion(lhs_type, rhs, state)) {
Ian Romanickec530102010-12-10 15:47:11 -0800667 if (rhs->type == lhs_type)
Eric Anholt336b4ad2010-05-19 10:38:37 -0700668 return rhs;
669 }
670
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700671 return NULL;
672}
673
Eric Anholta313c292011-08-05 21:40:50 -0700674static void
675mark_whole_array_access(ir_rvalue *access)
676{
677 ir_dereference_variable *deref = access->as_dereference_variable();
678
679 if (deref && deref->var) {
680 deref->var->max_array_access = deref->type->length - 1;
681 }
682}
683
Eric Anholt10a68522010-03-26 11:53:37 -0700684ir_rvalue *
685do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
Ian Romanicke9015e92011-12-23 09:56:29 -0800686 const char *non_lvalue_description,
Ian Romanick85caea22011-03-15 16:33:27 -0700687 ir_rvalue *lhs, ir_rvalue *rhs, bool is_initializer,
Eric Anholt10a68522010-03-26 11:53:37 -0700688 YYLTYPE lhs_loc)
689{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700690 void *ctx = state;
Eric Anholt10a68522010-03-26 11:53:37 -0700691 bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
692
Ian Romanick89704eb2013-03-15 18:06:11 -0700693 /* If the assignment LHS comes back as an ir_binop_vector_extract
694 * expression, move it to the RHS as an ir_triop_vector_insert.
695 */
696 if (lhs->ir_type == ir_type_expression) {
697 ir_expression *const expr = lhs->as_expression();
698
699 if (unlikely(expr->operation == ir_binop_vector_extract)) {
700 ir_rvalue *new_rhs =
701 validate_assignment(state, lhs->type, rhs, is_initializer);
702
703 if (new_rhs == NULL) {
704 _mesa_glsl_error(& lhs_loc, state, "type mismatch");
705 return lhs;
706 } else {
707 rhs = new(ctx) ir_expression(ir_triop_vector_insert,
708 expr->operands[0]->type,
709 expr->operands[0],
710 new_rhs,
711 expr->operands[1]);
712 lhs = expr->operands[0]->clone(ctx, NULL);
713 }
714 }
715 }
716
Eric Anholtf2475ca2012-03-29 17:02:15 -0700717 ir_variable *lhs_var = lhs->variable_referenced();
718 if (lhs_var)
719 lhs_var->assigned = true;
720
Eric Anholt10a68522010-03-26 11:53:37 -0700721 if (!error_emitted) {
Ian Romanicke9015e92011-12-23 09:56:29 -0800722 if (non_lvalue_description != NULL) {
723 _mesa_glsl_error(&lhs_loc, state,
724 "assignment to %s",
725 non_lvalue_description);
726 error_emitted = true;
727 } else if (lhs->variable_referenced() != NULL
728 && lhs->variable_referenced()->read_only) {
Chad Versaceb66be752011-01-21 13:44:08 -0800729 _mesa_glsl_error(&lhs_loc, state,
730 "assignment to read-only variable '%s'",
731 lhs->variable_referenced()->name);
732 error_emitted = true;
733
Paul Berry0d9bba62012-08-05 09:57:01 -0700734 } else if (lhs->type->is_array() &&
Paul Berryd4a24742012-08-02 08:18:12 -0700735 !state->check_version(120, 300, &lhs_loc,
Paul Berry0d9bba62012-08-05 09:57:01 -0700736 "whole array assignment forbidden")) {
Eric Anholt525cec92011-09-07 12:03:36 -0700737 /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
738 *
739 * "Other binary or unary expressions, non-dereferenced
740 * arrays, function names, swizzles with repeated fields,
741 * and constants cannot be l-values."
Paul Berryd4a24742012-08-02 08:18:12 -0700742 *
743 * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00.
Eric Anholt525cec92011-09-07 12:03:36 -0700744 */
Eric Anholt525cec92011-09-07 12:03:36 -0700745 error_emitted = true;
Chad Versaceb66be752011-01-21 13:44:08 -0800746 } else if (!lhs->is_lvalue()) {
Eric Anholt10a68522010-03-26 11:53:37 -0700747 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
748 error_emitted = true;
749 }
750 }
751
Ian Romanick85caea22011-03-15 16:33:27 -0700752 ir_rvalue *new_rhs =
753 validate_assignment(state, lhs->type, rhs, is_initializer);
Eric Anholt10a68522010-03-26 11:53:37 -0700754 if (new_rhs == NULL) {
755 _mesa_glsl_error(& lhs_loc, state, "type mismatch");
756 } else {
757 rhs = new_rhs;
Ian Romanick0157f412010-04-02 17:44:39 -0700758
759 /* If the LHS array was not declared with a size, it takes it size from
760 * the RHS. If the LHS is an l-value and a whole array, it must be a
761 * dereference of a variable. Any other case would require that the LHS
762 * is either not an l-value or not a whole array.
763 */
764 if (lhs->type->array_size() == 0) {
765 ir_dereference *const d = lhs->as_dereference();
766
767 assert(d != NULL);
768
Ian Romanick36ea2862010-05-19 13:52:29 +0200769 ir_variable *const var = d->variable_referenced();
Ian Romanick0157f412010-04-02 17:44:39 -0700770
771 assert(var != NULL);
772
Ian Romanick63f39422010-04-05 14:35:47 -0700773 if (var->max_array_access >= unsigned(rhs->type->array_size())) {
774 /* FINISHME: This should actually log the location of the RHS. */
775 _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
776 "previous access",
777 var->max_array_access);
778 }
779
Ian Romanickf38d15b2010-07-20 15:33:40 -0700780 var->type = glsl_type::get_array_instance(lhs->type->element_type(),
Ian Romanick0157f412010-04-02 17:44:39 -0700781 rhs->type->array_size());
Eric Anholt9703ed02010-07-22 15:50:37 -0700782 d->type = var->type;
Ian Romanick0157f412010-04-02 17:44:39 -0700783 }
Eric Anholt407a1002011-09-07 11:53:20 -0700784 mark_whole_array_access(rhs);
Eric Anholta313c292011-08-05 21:40:50 -0700785 mark_whole_array_access(lhs);
Eric Anholt10a68522010-03-26 11:53:37 -0700786 }
787
Eric Anholt2731a732010-06-23 14:51:14 -0700788 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
789 * but not post_inc) need the converted assigned value as an rvalue
790 * to handle things like:
791 *
792 * i = j += 1;
793 *
794 * So we always just store the computed value being assigned to a
795 * temporary and return a deref of that temporary. If the rvalue
796 * ends up not being used, the temp will get copy-propagated out.
797 */
Ian Romanick7e2aa912010-07-19 17:12:42 -0700798 ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp",
799 ir_var_temporary);
Eric Anholte33c1032010-06-24 15:13:03 -0700800 ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var);
Eric Anholtae805922010-06-24 09:06:12 -0700801 instructions->push_tail(var);
Eric Anholtaa5ec132012-05-14 09:14:54 -0700802 instructions->push_tail(new(ctx) ir_assignment(deref_var, rhs));
Eric Anholte33c1032010-06-24 15:13:03 -0700803 deref_var = new(ctx) ir_dereference_variable(var);
Eric Anholt10a68522010-03-26 11:53:37 -0700804
Ian Romanick8e9ce2e2010-08-03 15:02:35 -0700805 if (!error_emitted)
Eric Anholtaa5ec132012-05-14 09:14:54 -0700806 instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var));
Eric Anholt2731a732010-06-23 14:51:14 -0700807
Carl Worth1660a292010-06-23 18:11:51 -0700808 return new(ctx) ir_dereference_variable(var);
Eric Anholt10a68522010-03-26 11:53:37 -0700809}
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700810
Eric Anholtde38f0e2010-03-26 12:14:54 -0700811static ir_rvalue *
Eric Anholt959a9ec2010-06-23 14:43:50 -0700812get_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
Eric Anholtde38f0e2010-03-26 12:14:54 -0700813{
Kenneth Graunked3073f52011-01-21 14:32:31 -0800814 void *ctx = ralloc_parent(lvalue);
Eric Anholtde38f0e2010-03-26 12:14:54 -0700815 ir_variable *var;
Eric Anholtde38f0e2010-03-26 12:14:54 -0700816
Ian Romanick7e2aa912010-07-19 17:12:42 -0700817 var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
818 ir_var_temporary);
Eric Anholt43b5b032010-07-07 14:04:30 -0700819 instructions->push_tail(var);
Eric Anholtde38f0e2010-03-26 12:14:54 -0700820 var->mode = ir_var_auto;
821
Carl Worth1660a292010-06-23 18:11:51 -0700822 instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
Eric Anholtaa5ec132012-05-14 09:14:54 -0700823 lvalue));
Eric Anholtde38f0e2010-03-26 12:14:54 -0700824
Carl Worth1660a292010-06-23 18:11:51 -0700825 return new(ctx) ir_dereference_variable(var);
Eric Anholtde38f0e2010-03-26 12:14:54 -0700826}
827
828
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700829ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -0800830ast_node::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -0800831 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -0800832{
Ian Romanick18238de2010-03-01 13:49:10 -0800833 (void) instructions;
834 (void) state;
835
836 return NULL;
837}
838
Eric Anholtff796332010-11-30 11:23:28 -0800839static ir_rvalue *
840do_comparison(void *mem_ctx, int operation, ir_rvalue *op0, ir_rvalue *op1)
841{
842 int join_op;
Ian Romanick6d36be52010-12-02 12:17:36 -0800843 ir_rvalue *cmp = NULL;
Eric Anholtff796332010-11-30 11:23:28 -0800844
845 if (operation == ir_binop_all_equal)
846 join_op = ir_binop_logic_and;
847 else
848 join_op = ir_binop_logic_or;
849
850 switch (op0->type->base_type) {
851 case GLSL_TYPE_FLOAT:
852 case GLSL_TYPE_UINT:
853 case GLSL_TYPE_INT:
854 case GLSL_TYPE_BOOL:
855 return new(mem_ctx) ir_expression(operation, op0, op1);
856
857 case GLSL_TYPE_ARRAY: {
Eric Anholtff796332010-11-30 11:23:28 -0800858 for (unsigned int i = 0; i < op0->type->length; i++) {
859 ir_rvalue *e0, *e1, *result;
860
861 e0 = new(mem_ctx) ir_dereference_array(op0->clone(mem_ctx, NULL),
862 new(mem_ctx) ir_constant(i));
863 e1 = new(mem_ctx) ir_dereference_array(op1->clone(mem_ctx, NULL),
864 new(mem_ctx) ir_constant(i));
865 result = do_comparison(mem_ctx, operation, e0, e1);
866
Ian Romanick6d36be52010-12-02 12:17:36 -0800867 if (cmp) {
868 cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
Eric Anholtff796332010-11-30 11:23:28 -0800869 } else {
Ian Romanick6d36be52010-12-02 12:17:36 -0800870 cmp = result;
Eric Anholtff796332010-11-30 11:23:28 -0800871 }
872 }
Eric Anholtb4f58562010-12-01 15:55:53 -0800873
874 mark_whole_array_access(op0);
875 mark_whole_array_access(op1);
Ian Romanick6d36be52010-12-02 12:17:36 -0800876 break;
Eric Anholtff796332010-11-30 11:23:28 -0800877 }
878
879 case GLSL_TYPE_STRUCT: {
Eric Anholtff796332010-11-30 11:23:28 -0800880 for (unsigned int i = 0; i < op0->type->length; i++) {
881 ir_rvalue *e0, *e1, *result;
882 const char *field_name = op0->type->fields.structure[i].name;
883
884 e0 = new(mem_ctx) ir_dereference_record(op0->clone(mem_ctx, NULL),
885 field_name);
886 e1 = new(mem_ctx) ir_dereference_record(op1->clone(mem_ctx, NULL),
887 field_name);
888 result = do_comparison(mem_ctx, operation, e0, e1);
889
Ian Romanick6d36be52010-12-02 12:17:36 -0800890 if (cmp) {
891 cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
Eric Anholtff796332010-11-30 11:23:28 -0800892 } else {
Ian Romanick6d36be52010-12-02 12:17:36 -0800893 cmp = result;
Eric Anholtff796332010-11-30 11:23:28 -0800894 }
895 }
Ian Romanick6d36be52010-12-02 12:17:36 -0800896 break;
Eric Anholtff796332010-11-30 11:23:28 -0800897 }
898
899 case GLSL_TYPE_ERROR:
900 case GLSL_TYPE_VOID:
901 case GLSL_TYPE_SAMPLER:
Ian Romanick491364e2012-12-11 12:11:16 -0800902 case GLSL_TYPE_INTERFACE:
Eric Anholtff796332010-11-30 11:23:28 -0800903 /* I assume a comparison of a struct containing a sampler just
904 * ignores the sampler present in the type.
905 */
Ian Romanick6d36be52010-12-02 12:17:36 -0800906 break;
Eric Anholtff796332010-11-30 11:23:28 -0800907 }
Eric Anholtd56c9742010-11-30 13:28:47 -0800908
Ian Romanick6d36be52010-12-02 12:17:36 -0800909 if (cmp == NULL)
910 cmp = new(mem_ctx) ir_constant(true);
911
912 return cmp;
Eric Anholtff796332010-11-30 11:23:28 -0800913}
Ian Romanick18238de2010-03-01 13:49:10 -0800914
Eric Anholt01822702011-04-09 15:59:20 -1000915/* For logical operations, we want to ensure that the operands are
916 * scalar booleans. If it isn't, emit an error and return a constant
917 * boolean to avoid triggering cascading error messages.
918 */
919ir_rvalue *
920get_scalar_boolean_operand(exec_list *instructions,
921 struct _mesa_glsl_parse_state *state,
922 ast_expression *parent_expr,
923 int operand,
924 const char *operand_name,
925 bool *error_emitted)
926{
927 ast_expression *expr = parent_expr->subexpressions[operand];
928 void *ctx = state;
929 ir_rvalue *val = expr->hir(instructions, state);
930
931 if (val->type->is_boolean() && val->type->is_scalar())
932 return val;
933
934 if (!*error_emitted) {
935 YYLTYPE loc = expr->get_location();
936 _mesa_glsl_error(&loc, state, "%s of `%s' must be scalar boolean",
937 operand_name,
938 parent_expr->operator_string(parent_expr->oper));
939 *error_emitted = true;
940 }
941
942 return new(ctx) ir_constant(true);
943}
944
Paul Berry93b97582011-09-06 10:01:51 -0700945/**
946 * If name refers to a builtin array whose maximum allowed size is less than
947 * size, report an error and return true. Otherwise return false.
948 */
Ian Romanick2c333a82013-03-15 14:33:01 -0700949void
Paul Berry93b97582011-09-06 10:01:51 -0700950check_builtin_array_max_size(const char *name, unsigned size,
951 YYLTYPE loc, struct _mesa_glsl_parse_state *state)
952{
953 if ((strcmp("gl_TexCoord", name) == 0)
954 && (size > state->Const.MaxTextureCoords)) {
955 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
956 *
957 * "The size [of gl_TexCoord] can be at most
958 * gl_MaxTextureCoords."
959 */
960 _mesa_glsl_error(&loc, state, "`gl_TexCoord' array size cannot "
961 "be larger than gl_MaxTextureCoords (%u)\n",
962 state->Const.MaxTextureCoords);
Paul Berry37bb1c42011-08-11 15:23:33 -0700963 } else if (strcmp("gl_ClipDistance", name) == 0
964 && size > state->Const.MaxClipPlanes) {
965 /* From section 7.1 (Vertex Shader Special Variables) of the
966 * GLSL 1.30 spec:
967 *
968 * "The gl_ClipDistance array is predeclared as unsized and
969 * must be sized by the shader either redeclaring it with a
970 * size or indexing it only with integral constant
971 * expressions. ... The size can be at most
972 * gl_MaxClipDistances."
973 */
974 _mesa_glsl_error(&loc, state, "`gl_ClipDistance' array size cannot "
975 "be larger than gl_MaxClipDistances (%u)\n",
976 state->Const.MaxClipPlanes);
Paul Berry93b97582011-09-06 10:01:51 -0700977 }
Paul Berry93b97582011-09-06 10:01:51 -0700978}
979
Paul Berry9abda922011-10-31 18:22:48 -0700980/**
981 * Create the constant 1, of a which is appropriate for incrementing and
982 * decrementing values of the given GLSL type. For example, if type is vec4,
983 * this creates a constant value of 1.0 having type float.
984 *
985 * If the given type is invalid for increment and decrement operators, return
986 * a floating point 1--the error will be detected later.
987 */
988static ir_rvalue *
989constant_one_for_inc_dec(void *ctx, const glsl_type *type)
990{
991 switch (type->base_type) {
992 case GLSL_TYPE_UINT:
993 return new(ctx) ir_constant((unsigned) 1);
994 case GLSL_TYPE_INT:
995 return new(ctx) ir_constant(1);
996 default:
997 case GLSL_TYPE_FLOAT:
998 return new(ctx) ir_constant(1.0f);
999 }
1000}
1001
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001002ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08001003ast_expression::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08001004 struct _mesa_glsl_parse_state *state)
1005{
Kenneth Graunke953ff122010-06-25 13:14:37 -07001006 void *ctx = state;
Ian Romanicka87ac252010-02-22 13:19:34 -08001007 static const int operations[AST_NUM_OPERATORS] = {
1008 -1, /* ast_assign doesn't convert to ir_expression. */
1009 -1, /* ast_plus doesn't convert to ir_expression. */
1010 ir_unop_neg,
1011 ir_binop_add,
1012 ir_binop_sub,
1013 ir_binop_mul,
1014 ir_binop_div,
1015 ir_binop_mod,
1016 ir_binop_lshift,
1017 ir_binop_rshift,
1018 ir_binop_less,
1019 ir_binop_greater,
1020 ir_binop_lequal,
1021 ir_binop_gequal,
Luca Barbieri4dfb8992010-09-08 01:31:39 +02001022 ir_binop_all_equal,
1023 ir_binop_any_nequal,
Ian Romanicka87ac252010-02-22 13:19:34 -08001024 ir_binop_bit_and,
1025 ir_binop_bit_xor,
1026 ir_binop_bit_or,
1027 ir_unop_bit_not,
1028 ir_binop_logic_and,
1029 ir_binop_logic_xor,
1030 ir_binop_logic_or,
1031 ir_unop_logic_not,
1032
1033 /* Note: The following block of expression types actually convert
1034 * to multiple IR instructions.
1035 */
1036 ir_binop_mul, /* ast_mul_assign */
1037 ir_binop_div, /* ast_div_assign */
1038 ir_binop_mod, /* ast_mod_assign */
1039 ir_binop_add, /* ast_add_assign */
1040 ir_binop_sub, /* ast_sub_assign */
1041 ir_binop_lshift, /* ast_ls_assign */
1042 ir_binop_rshift, /* ast_rs_assign */
1043 ir_binop_bit_and, /* ast_and_assign */
1044 ir_binop_bit_xor, /* ast_xor_assign */
1045 ir_binop_bit_or, /* ast_or_assign */
1046
1047 -1, /* ast_conditional doesn't convert to ir_expression. */
Eric Anholtde38f0e2010-03-26 12:14:54 -07001048 ir_binop_add, /* ast_pre_inc. */
1049 ir_binop_sub, /* ast_pre_dec. */
1050 ir_binop_add, /* ast_post_inc. */
1051 ir_binop_sub, /* ast_post_dec. */
Ian Romanicka87ac252010-02-22 13:19:34 -08001052 -1, /* ast_field_selection doesn't conv to ir_expression. */
1053 -1, /* ast_array_index doesn't convert to ir_expression. */
1054 -1, /* ast_function_call doesn't conv to ir_expression. */
1055 -1, /* ast_identifier doesn't convert to ir_expression. */
1056 -1, /* ast_int_constant doesn't convert to ir_expression. */
1057 -1, /* ast_uint_constant doesn't conv to ir_expression. */
1058 -1, /* ast_float_constant doesn't conv to ir_expression. */
1059 -1, /* ast_bool_constant doesn't conv to ir_expression. */
1060 -1, /* ast_sequence doesn't convert to ir_expression. */
1061 };
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001062 ir_rvalue *result = NULL;
Aras Pranckevicius1c325af2010-07-29 15:35:22 +03001063 ir_rvalue *op[3];
Kenneth Graunke08ba9772011-04-14 17:21:59 -07001064 const struct glsl_type *type; /* a temporary variable for switch cases */
Ian Romanicka87ac252010-02-22 13:19:34 -08001065 bool error_emitted = false;
1066 YYLTYPE loc;
1067
Ian Romanick18238de2010-03-01 13:49:10 -08001068 loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08001069
Ian Romanick18238de2010-03-01 13:49:10 -08001070 switch (this->oper) {
Matt Turnerae79e862013-06-29 19:27:50 -07001071 case ast_aggregate:
1072 assert(!"ast_aggregate: Should never get here.");
1073 break;
1074
Ian Romanick6652af32010-03-09 16:38:02 -08001075 case ast_assign: {
Ian Romanick18238de2010-03-01 13:49:10 -08001076 op[0] = this->subexpressions[0]->hir(instructions, state);
1077 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001078
Ian Romanicke9015e92011-12-23 09:56:29 -08001079 result = do_assignment(instructions, state,
1080 this->subexpressions[0]->non_lvalue_description,
1081 op[0], op[1], false,
Eric Anholt10a68522010-03-26 11:53:37 -07001082 this->subexpressions[0]->get_location());
1083 error_emitted = result->type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -08001084 break;
Ian Romanick6652af32010-03-09 16:38:02 -08001085 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001086
1087 case ast_plus:
Ian Romanick18238de2010-03-01 13:49:10 -08001088 op[0] = this->subexpressions[0]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001089
Carl Worthc24bcad2010-07-21 11:23:51 -07001090 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
1091
1092 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -08001093
1094 result = op[0];
1095 break;
1096
1097 case ast_neg:
Ian Romanick18238de2010-03-01 13:49:10 -08001098 op[0] = this->subexpressions[0]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001099
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001100 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -08001101
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001102 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -08001103
Carl Worth1660a292010-06-23 18:11:51 -07001104 result = new(ctx) ir_expression(operations[this->oper], type,
1105 op[0], NULL);
Ian Romanicka87ac252010-02-22 13:19:34 -08001106 break;
1107
1108 case ast_add:
1109 case ast_sub:
1110 case ast_mul:
1111 case ast_div:
Ian Romanick18238de2010-03-01 13:49:10 -08001112 op[0] = this->subexpressions[0]->hir(instructions, state);
1113 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001114
Ian Romanickbfb09c22010-03-29 16:32:55 -07001115 type = arithmetic_result_type(op[0], op[1],
Ian Romanick18238de2010-03-01 13:49:10 -08001116 (this->oper == ast_mul),
Eric Anholta13bb142010-03-31 16:38:11 -10001117 state, & loc);
1118 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -08001119
Carl Worth1660a292010-06-23 18:11:51 -07001120 result = new(ctx) ir_expression(operations[this->oper], type,
1121 op[0], op[1]);
Ian Romanicka87ac252010-02-22 13:19:34 -08001122 break;
1123
1124 case ast_mod:
Ian Romanick18238de2010-03-01 13:49:10 -08001125 op[0] = this->subexpressions[0]->hir(instructions, state);
1126 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001127
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001128 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -08001129
Ian Romanick18238de2010-03-01 13:49:10 -08001130 assert(operations[this->oper] == ir_binop_mod);
Ian Romanicka87ac252010-02-22 13:19:34 -08001131
Carl Worth1660a292010-06-23 18:11:51 -07001132 result = new(ctx) ir_expression(operations[this->oper], type,
1133 op[0], op[1]);
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001134 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -08001135 break;
1136
1137 case ast_lshift:
1138 case ast_rshift:
Paul Berry0d9bba62012-08-05 09:57:01 -07001139 if (!state->check_bitwise_operations_allowed(&loc)) {
Chad Versace5c4c36f2010-10-08 16:22:28 -07001140 error_emitted = true;
Chad Versace5c4c36f2010-10-08 16:22:28 -07001141 }
1142
Chad Versace5c4c36f2010-10-08 16:22:28 -07001143 op[0] = this->subexpressions[0]->hir(instructions, state);
1144 op[1] = this->subexpressions[1]->hir(instructions, state);
Chad Versacec0197ab2010-10-15 09:49:46 -07001145 type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1146 &loc);
Chad Versace5c4c36f2010-10-08 16:22:28 -07001147 result = new(ctx) ir_expression(operations[this->oper], type,
1148 op[0], op[1]);
1149 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1150 break;
Ian Romanicka87ac252010-02-22 13:19:34 -08001151
1152 case ast_less:
1153 case ast_greater:
1154 case ast_lequal:
1155 case ast_gequal:
Ian Romanick18238de2010-03-01 13:49:10 -08001156 op[0] = this->subexpressions[0]->hir(instructions, state);
1157 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001158
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001159 type = relational_result_type(op[0], op[1], state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -08001160
1161 /* The relational operators must either generate an error or result
1162 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1163 */
Ian Romanicka43817a2010-03-26 14:27:23 -07001164 assert(type->is_error()
Ian Romanicka87ac252010-02-22 13:19:34 -08001165 || ((type->base_type == GLSL_TYPE_BOOL)
Ian Romanickcb36f8a2010-03-09 15:51:22 -08001166 && type->is_scalar()));
Ian Romanicka87ac252010-02-22 13:19:34 -08001167
Carl Worth1660a292010-06-23 18:11:51 -07001168 result = new(ctx) ir_expression(operations[this->oper], type,
1169 op[0], op[1]);
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001170 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -08001171 break;
1172
1173 case ast_nequal:
1174 case ast_equal:
Ian Romanick6e659ca2010-03-29 15:11:05 -07001175 op[0] = this->subexpressions[0]->hir(instructions, state);
1176 op[1] = this->subexpressions[1]->hir(instructions, state);
1177
1178 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1179 *
1180 * "The equality operators equal (==), and not equal (!=)
1181 * operate on all types. They result in a scalar Boolean. If
1182 * the operand types do not match, then there must be a
1183 * conversion from Section 4.1.10 "Implicit Conversions"
1184 * applied to one operand that can make them match, in which
1185 * case this conversion is done."
1186 */
Ian Romanickbfb09c22010-03-29 16:32:55 -07001187 if ((!apply_implicit_conversion(op[0]->type, op[1], state)
1188 && !apply_implicit_conversion(op[1]->type, op[0], state))
Ian Romanick212b0322010-03-29 16:22:38 -07001189 || (op[0]->type != op[1]->type)) {
Ian Romanick6e659ca2010-03-29 15:11:05 -07001190 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
1191 "type", (this->oper == ast_equal) ? "==" : "!=");
1192 error_emitted = true;
Paul Berry0d9bba62012-08-05 09:57:01 -07001193 } else if ((op[0]->type->is_array() || op[1]->type->is_array()) &&
Paul Berryd4a24742012-08-02 08:18:12 -07001194 !state->check_version(120, 300, &loc,
Paul Berry0d9bba62012-08-05 09:57:01 -07001195 "array comparisons forbidden")) {
Ian Romanicka80cbd62010-03-30 17:04:48 -07001196 error_emitted = true;
Ian Romanick6e659ca2010-03-29 15:11:05 -07001197 }
1198
Eric Anholt175829f2011-04-09 12:54:34 -10001199 if (error_emitted) {
1200 result = new(ctx) ir_constant(false);
1201 } else {
1202 result = do_comparison(ctx, operations[this->oper], op[0], op[1]);
1203 assert(result->type == glsl_type::bool_type);
Eric Anholt175829f2011-04-09 12:54:34 -10001204 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001205 break;
1206
1207 case ast_bit_and:
1208 case ast_bit_xor:
1209 case ast_bit_or:
Kenneth Graunke1eea9632010-08-31 10:56:24 -07001210 op[0] = this->subexpressions[0]->hir(instructions, state);
1211 op[1] = this->subexpressions[1]->hir(instructions, state);
Chad Versacecfdbf8b2010-10-15 11:28:05 -07001212 type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
1213 state, &loc);
Kenneth Graunke1eea9632010-08-31 10:56:24 -07001214 result = new(ctx) ir_expression(operations[this->oper], type,
1215 op[0], op[1]);
1216 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1217 break;
1218
Ian Romanicka87ac252010-02-22 13:19:34 -08001219 case ast_bit_not:
Kenneth Graunke1eea9632010-08-31 10:56:24 -07001220 op[0] = this->subexpressions[0]->hir(instructions, state);
1221
Paul Berry0d9bba62012-08-05 09:57:01 -07001222 if (!state->check_bitwise_operations_allowed(&loc)) {
Kenneth Graunke1eea9632010-08-31 10:56:24 -07001223 error_emitted = true;
1224 }
1225
1226 if (!op[0]->type->is_integer()) {
1227 _mesa_glsl_error(&loc, state, "operand of `~' must be an integer");
1228 error_emitted = true;
1229 }
1230
Ian Romanick39464482011-12-23 17:16:43 -08001231 type = error_emitted ? glsl_type::error_type : op[0]->type;
Kenneth Graunke1eea9632010-08-31 10:56:24 -07001232 result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL);
Ian Romanicka87ac252010-02-22 13:19:34 -08001233 break;
1234
Eric Anholt4950a682010-04-16 01:10:32 -07001235 case ast_logic_and: {
Eric Anholt7ec0c972011-04-09 10:27:02 -10001236 exec_list rhs_instructions;
Eric Anholt01822702011-04-09 15:59:20 -10001237 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1238 "LHS", &error_emitted);
Eric Anholt7ec0c972011-04-09 10:27:02 -10001239 op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
1240 "RHS", &error_emitted);
Eric Anholtb82c0c32010-03-31 09:11:39 -10001241
Eric Anholtead35892012-02-07 22:59:24 -08001242 if (rhs_instructions.is_empty()) {
1243 result = new(ctx) ir_expression(ir_binop_logic_and, op[0], op[1]);
1244 type = result->type;
Eric Anholt44b694e2010-04-16 13:49:04 -07001245 } else {
Ian Romanick81d664f2010-07-12 15:18:55 -07001246 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
Ian Romanick7e2aa912010-07-19 17:12:42 -07001247 "and_tmp",
1248 ir_var_temporary);
Ian Romanick81d664f2010-07-12 15:18:55 -07001249 instructions->push_tail(tmp);
1250
Carl Worth1660a292010-06-23 18:11:51 -07001251 ir_if *const stmt = new(ctx) ir_if(op[0]);
Eric Anholt44b694e2010-04-16 13:49:04 -07001252 instructions->push_tail(stmt);
Eric Anholt4950a682010-04-16 01:10:32 -07001253
Eric Anholt7ec0c972011-04-09 10:27:02 -10001254 stmt->then_instructions.append_list(&rhs_instructions);
Carl Worth1660a292010-06-23 18:11:51 -07001255 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -07001256 ir_assignment *const then_assign =
Eric Anholtaa5ec132012-05-14 09:14:54 -07001257 new(ctx) ir_assignment(then_deref, op[1]);
Eric Anholt44b694e2010-04-16 13:49:04 -07001258 stmt->then_instructions.push_tail(then_assign);
1259
Carl Worth1660a292010-06-23 18:11:51 -07001260 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -07001261 ir_assignment *const else_assign =
Eric Anholtaa5ec132012-05-14 09:14:54 -07001262 new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false));
Eric Anholt44b694e2010-04-16 13:49:04 -07001263 stmt->else_instructions.push_tail(else_assign);
1264
Carl Worth1660a292010-06-23 18:11:51 -07001265 result = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -07001266 type = tmp->type;
Eric Anholtb82c0c32010-03-31 09:11:39 -10001267 }
Eric Anholt4950a682010-04-16 01:10:32 -07001268 break;
1269 }
1270
1271 case ast_logic_or: {
Eric Anholt9e04b192011-04-09 10:27:02 -10001272 exec_list rhs_instructions;
Eric Anholt01822702011-04-09 15:59:20 -10001273 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1274 "LHS", &error_emitted);
Eric Anholt9e04b192011-04-09 10:27:02 -10001275 op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
1276 "RHS", &error_emitted);
Eric Anholt4950a682010-04-16 01:10:32 -07001277
Eric Anholtead35892012-02-07 22:59:24 -08001278 if (rhs_instructions.is_empty()) {
1279 result = new(ctx) ir_expression(ir_binop_logic_or, op[0], op[1]);
1280 type = result->type;
Eric Anholt44b694e2010-04-16 13:49:04 -07001281 } else {
Kenneth Graunkedfd30ca2010-07-08 12:40:52 -07001282 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
Ian Romanick7e2aa912010-07-19 17:12:42 -07001283 "or_tmp",
1284 ir_var_temporary);
Ian Romanick0b9ae3b2010-07-12 14:22:05 -07001285 instructions->push_tail(tmp);
Eric Anholt4950a682010-04-16 01:10:32 -07001286
Ian Romanick81d664f2010-07-12 15:18:55 -07001287 ir_if *const stmt = new(ctx) ir_if(op[0]);
1288 instructions->push_tail(stmt);
1289
Carl Worth1660a292010-06-23 18:11:51 -07001290 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -07001291 ir_assignment *const then_assign =
Eric Anholtaa5ec132012-05-14 09:14:54 -07001292 new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true));
Eric Anholt44b694e2010-04-16 13:49:04 -07001293 stmt->then_instructions.push_tail(then_assign);
1294
Eric Anholt9e04b192011-04-09 10:27:02 -10001295 stmt->else_instructions.append_list(&rhs_instructions);
Carl Worth1660a292010-06-23 18:11:51 -07001296 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -07001297 ir_assignment *const else_assign =
Eric Anholtaa5ec132012-05-14 09:14:54 -07001298 new(ctx) ir_assignment(else_deref, op[1]);
Eric Anholt44b694e2010-04-16 13:49:04 -07001299 stmt->else_instructions.push_tail(else_assign);
1300
Carl Worth1660a292010-06-23 18:11:51 -07001301 result = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -07001302 type = tmp->type;
Eric Anholt4950a682010-04-16 01:10:32 -07001303 }
Eric Anholt4950a682010-04-16 01:10:32 -07001304 break;
1305 }
1306
1307 case ast_logic_xor:
Eric Anholt756c2622011-04-09 14:57:17 -10001308 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1309 *
1310 * "The logical binary operators and (&&), or ( | | ), and
1311 * exclusive or (^^). They operate only on two Boolean
1312 * expressions and result in a Boolean expression."
1313 */
1314 op[0] = get_scalar_boolean_operand(instructions, state, this, 0, "LHS",
1315 &error_emitted);
1316 op[1] = get_scalar_boolean_operand(instructions, state, this, 1, "RHS",
1317 &error_emitted);
Eric Anholt4950a682010-04-16 01:10:32 -07001318
Carl Worth1660a292010-06-23 18:11:51 -07001319 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
1320 op[0], op[1]);
Ian Romanicka87ac252010-02-22 13:19:34 -08001321 break;
1322
Eric Anholta5827fe2010-03-31 16:50:55 -10001323 case ast_logic_not:
Eric Anholt01822702011-04-09 15:59:20 -10001324 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1325 "operand", &error_emitted);
Eric Anholta5827fe2010-03-31 16:50:55 -10001326
Carl Worth1660a292010-06-23 18:11:51 -07001327 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
1328 op[0], NULL);
Eric Anholta5827fe2010-03-31 16:50:55 -10001329 break;
1330
Ian Romanicka87ac252010-02-22 13:19:34 -08001331 case ast_mul_assign:
1332 case ast_div_assign:
1333 case ast_add_assign:
1334 case ast_sub_assign: {
Ian Romanick18238de2010-03-01 13:49:10 -08001335 op[0] = this->subexpressions[0]->hir(instructions, state);
1336 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001337
Ian Romanickbfb09c22010-03-29 16:32:55 -07001338 type = arithmetic_result_type(op[0], op[1],
Ian Romanick18238de2010-03-01 13:49:10 -08001339 (this->oper == ast_mul_assign),
Eric Anholta13bb142010-03-31 16:38:11 -10001340 state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -08001341
Carl Worth1660a292010-06-23 18:11:51 -07001342 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1343 op[0], op[1]);
Ian Romanicka87ac252010-02-22 13:19:34 -08001344
Eric Anholt3e24ef62010-06-23 12:40:17 -07001345 result = do_assignment(instructions, state,
Ian Romanicke9015e92011-12-23 09:56:29 -08001346 this->subexpressions[0]->non_lvalue_description,
Ian Romanick85caea22011-03-15 16:33:27 -07001347 op[0]->clone(ctx, NULL), temp_rhs, false,
Eric Anholt10a68522010-03-26 11:53:37 -07001348 this->subexpressions[0]->get_location());
Eric Anholt10a68522010-03-26 11:53:37 -07001349 error_emitted = (op[0]->type->is_error());
Ian Romanicka87ac252010-02-22 13:19:34 -08001350
1351 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1352 * explicitly test for this because none of the binary expression
1353 * operators allow array operands either.
1354 */
1355
Ian Romanicka87ac252010-02-22 13:19:34 -08001356 break;
1357 }
1358
Eric Anholt48a0e642010-03-26 11:57:46 -07001359 case ast_mod_assign: {
1360 op[0] = this->subexpressions[0]->hir(instructions, state);
1361 op[1] = this->subexpressions[1]->hir(instructions, state);
1362
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001363 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
Eric Anholt48a0e642010-03-26 11:57:46 -07001364
1365 assert(operations[this->oper] == ir_binop_mod);
1366
Ian Romanick768b55a2010-08-13 16:46:43 -07001367 ir_rvalue *temp_rhs;
Carl Worth1660a292010-06-23 18:11:51 -07001368 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1369 op[0], op[1]);
Eric Anholt48a0e642010-03-26 11:57:46 -07001370
Eric Anholt3e24ef62010-06-23 12:40:17 -07001371 result = do_assignment(instructions, state,
Ian Romanicke9015e92011-12-23 09:56:29 -08001372 this->subexpressions[0]->non_lvalue_description,
Ian Romanick85caea22011-03-15 16:33:27 -07001373 op[0]->clone(ctx, NULL), temp_rhs, false,
Eric Anholt48a0e642010-03-26 11:57:46 -07001374 this->subexpressions[0]->get_location());
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001375 error_emitted = type->is_error();
Eric Anholt48a0e642010-03-26 11:57:46 -07001376 break;
1377 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001378
1379 case ast_ls_assign:
Chad Versace338ed6e2010-10-15 10:05:50 -07001380 case ast_rs_assign: {
1381 op[0] = this->subexpressions[0]->hir(instructions, state);
1382 op[1] = this->subexpressions[1]->hir(instructions, state);
1383 type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1384 &loc);
1385 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1386 type, op[0], op[1]);
Ian Romanicke9015e92011-12-23 09:56:29 -08001387 result = do_assignment(instructions, state,
1388 this->subexpressions[0]->non_lvalue_description,
1389 op[0]->clone(ctx, NULL), temp_rhs, false,
Chad Versace338ed6e2010-10-15 10:05:50 -07001390 this->subexpressions[0]->get_location());
1391 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
Ian Romanick251eb752010-03-29 14:15:05 -07001392 break;
Chad Versace338ed6e2010-10-15 10:05:50 -07001393 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001394
1395 case ast_and_assign:
1396 case ast_xor_assign:
Chad Versaced03ac0f2010-10-15 12:08:28 -07001397 case ast_or_assign: {
1398 op[0] = this->subexpressions[0]->hir(instructions, state);
1399 op[1] = this->subexpressions[1]->hir(instructions, state);
1400 type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
1401 state, &loc);
1402 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1403 type, op[0], op[1]);
Ian Romanicke9015e92011-12-23 09:56:29 -08001404 result = do_assignment(instructions, state,
1405 this->subexpressions[0]->non_lvalue_description,
1406 op[0]->clone(ctx, NULL), temp_rhs, false,
Chad Versaced03ac0f2010-10-15 12:08:28 -07001407 this->subexpressions[0]->get_location());
1408 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
Ian Romanick251eb752010-03-29 14:15:05 -07001409 break;
Chad Versaced03ac0f2010-10-15 12:08:28 -07001410 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001411
Ian Romanick96f9cea2010-03-29 15:33:54 -07001412 case ast_conditional: {
Ian Romanick96f9cea2010-03-29 15:33:54 -07001413 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1414 *
1415 * "The ternary selection operator (?:). It operates on three
1416 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1417 * first expression, which must result in a scalar Boolean."
1418 */
Eric Anholt01822702011-04-09 15:59:20 -10001419 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1420 "condition", &error_emitted);
Ian Romanick96f9cea2010-03-29 15:33:54 -07001421
1422 /* The :? operator is implemented by generating an anonymous temporary
1423 * followed by an if-statement. The last instruction in each branch of
1424 * the if-statement assigns a value to the anonymous temporary. This
1425 * temporary is the r-value of the expression.
1426 */
Ian Romanick0ad76c62010-06-11 12:56:26 -07001427 exec_list then_instructions;
1428 exec_list else_instructions;
Ian Romanick96f9cea2010-03-29 15:33:54 -07001429
Ian Romanick0ad76c62010-06-11 12:56:26 -07001430 op[1] = this->subexpressions[1]->hir(&then_instructions, state);
1431 op[2] = this->subexpressions[2]->hir(&else_instructions, state);
Ian Romanick96f9cea2010-03-29 15:33:54 -07001432
1433 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1434 *
1435 * "The second and third expressions can be any type, as
1436 * long their types match, or there is a conversion in
1437 * Section 4.1.10 "Implicit Conversions" that can be applied
1438 * to one of the expressions to make their types match. This
1439 * resulting matching type is the type of the entire
1440 * expression."
1441 */
Ian Romanickbfb09c22010-03-29 16:32:55 -07001442 if ((!apply_implicit_conversion(op[1]->type, op[2], state)
1443 && !apply_implicit_conversion(op[2]->type, op[1], state))
Ian Romanickdb9be2e2010-03-29 16:25:56 -07001444 || (op[1]->type != op[2]->type)) {
Ian Romanick96f9cea2010-03-29 15:33:54 -07001445 YYLTYPE loc = this->subexpressions[1]->get_location();
1446
1447 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
1448 "operator must have matching types.");
1449 error_emitted = true;
Ian Romanick0ad76c62010-06-11 12:56:26 -07001450 type = glsl_type::error_type;
Ian Romanickdb9be2e2010-03-29 16:25:56 -07001451 } else {
Ian Romanick0ad76c62010-06-11 12:56:26 -07001452 type = op[1]->type;
Ian Romanick96f9cea2010-03-29 15:33:54 -07001453 }
1454
Ian Romanickf09fabc2010-09-07 14:30:06 -07001455 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1456 *
1457 * "The second and third expressions must be the same type, but can
1458 * be of any type other than an array."
1459 */
Paul Berry0d9bba62012-08-05 09:57:01 -07001460 if (type->is_array() &&
Paul Berryd4a24742012-08-02 08:18:12 -07001461 !state->check_version(120, 300, &loc,
Paul Berry0d9bba62012-08-05 09:57:01 -07001462 "Second and third operands of ?: operator "
1463 "cannot be arrays")) {
Ian Romanickf09fabc2010-09-07 14:30:06 -07001464 error_emitted = true;
1465 }
1466
Ian Romanick7825d3d2010-06-11 13:45:51 -07001467 ir_constant *cond_val = op[0]->constant_expression_value();
1468 ir_constant *then_val = op[1]->constant_expression_value();
1469 ir_constant *else_val = op[2]->constant_expression_value();
Ian Romanick0ad76c62010-06-11 12:56:26 -07001470
Ian Romanick7825d3d2010-06-11 13:45:51 -07001471 if (then_instructions.is_empty()
1472 && else_instructions.is_empty()
1473 && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) {
1474 result = (cond_val->value.b[0]) ? then_val : else_val;
1475 } else {
Ian Romanick7e2aa912010-07-19 17:12:42 -07001476 ir_variable *const tmp =
1477 new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
Ian Romanick0b9ae3b2010-07-12 14:22:05 -07001478 instructions->push_tail(tmp);
Ian Romanick0ad76c62010-06-11 12:56:26 -07001479
Carl Worth1660a292010-06-23 18:11:51 -07001480 ir_if *const stmt = new(ctx) ir_if(op[0]);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001481 instructions->push_tail(stmt);
Ian Romanick0ad76c62010-06-11 12:56:26 -07001482
Ian Romanick7825d3d2010-06-11 13:45:51 -07001483 then_instructions.move_nodes_to(& stmt->then_instructions);
Carl Worth1660a292010-06-23 18:11:51 -07001484 ir_dereference *const then_deref =
1485 new(ctx) ir_dereference_variable(tmp);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001486 ir_assignment *const then_assign =
Eric Anholtaa5ec132012-05-14 09:14:54 -07001487 new(ctx) ir_assignment(then_deref, op[1]);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001488 stmt->then_instructions.push_tail(then_assign);
Ian Romanick0ad76c62010-06-11 12:56:26 -07001489
Ian Romanick7825d3d2010-06-11 13:45:51 -07001490 else_instructions.move_nodes_to(& stmt->else_instructions);
Carl Worth1660a292010-06-23 18:11:51 -07001491 ir_dereference *const else_deref =
1492 new(ctx) ir_dereference_variable(tmp);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001493 ir_assignment *const else_assign =
Eric Anholtaa5ec132012-05-14 09:14:54 -07001494 new(ctx) ir_assignment(else_deref, op[2]);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001495 stmt->else_instructions.push_tail(else_assign);
1496
Carl Worth1660a292010-06-23 18:11:51 -07001497 result = new(ctx) ir_dereference_variable(tmp);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001498 }
Ian Romanick251eb752010-03-29 14:15:05 -07001499 break;
Ian Romanick96f9cea2010-03-29 15:33:54 -07001500 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001501
1502 case ast_pre_inc:
Eric Anholt76ea56c2010-03-26 12:16:54 -07001503 case ast_pre_dec: {
Ian Romanickfa0a9ac2011-12-23 09:56:03 -08001504 this->non_lvalue_description = (this->oper == ast_pre_inc)
1505 ? "pre-increment operation" : "pre-decrement operation";
1506
Eric Anholt76ea56c2010-03-26 12:16:54 -07001507 op[0] = this->subexpressions[0]->hir(instructions, state);
Paul Berry9abda922011-10-31 18:22:48 -07001508 op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
Eric Anholt76ea56c2010-03-26 12:16:54 -07001509
Eric Anholta13bb142010-03-31 16:38:11 -10001510 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
Eric Anholt76ea56c2010-03-26 12:16:54 -07001511
Ian Romanick768b55a2010-08-13 16:46:43 -07001512 ir_rvalue *temp_rhs;
Carl Worth1660a292010-06-23 18:11:51 -07001513 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1514 op[0], op[1]);
Eric Anholt76ea56c2010-03-26 12:16:54 -07001515
Eric Anholt3e24ef62010-06-23 12:40:17 -07001516 result = do_assignment(instructions, state,
Ian Romanicke9015e92011-12-23 09:56:29 -08001517 this->subexpressions[0]->non_lvalue_description,
Ian Romanick85caea22011-03-15 16:33:27 -07001518 op[0]->clone(ctx, NULL), temp_rhs, false,
Eric Anholt76ea56c2010-03-26 12:16:54 -07001519 this->subexpressions[0]->get_location());
Eric Anholt76ea56c2010-03-26 12:16:54 -07001520 error_emitted = op[0]->type->is_error();
1521 break;
1522 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001523
1524 case ast_post_inc:
Eric Anholtde38f0e2010-03-26 12:14:54 -07001525 case ast_post_dec: {
Ian Romanickfa0a9ac2011-12-23 09:56:03 -08001526 this->non_lvalue_description = (this->oper == ast_post_inc)
1527 ? "post-increment operation" : "post-decrement operation";
Eric Anholtde38f0e2010-03-26 12:14:54 -07001528 op[0] = this->subexpressions[0]->hir(instructions, state);
Paul Berry9abda922011-10-31 18:22:48 -07001529 op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
Eric Anholtde38f0e2010-03-26 12:14:54 -07001530
1531 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1532
Eric Anholta13bb142010-03-31 16:38:11 -10001533 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
Eric Anholtde38f0e2010-03-26 12:14:54 -07001534
Ian Romanick768b55a2010-08-13 16:46:43 -07001535 ir_rvalue *temp_rhs;
Carl Worth1660a292010-06-23 18:11:51 -07001536 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1537 op[0], op[1]);
Eric Anholtde38f0e2010-03-26 12:14:54 -07001538
1539 /* Get a temporary of a copy of the lvalue before it's modified.
1540 * This may get thrown away later.
1541 */
Eric Anholt8273bd42010-08-04 12:34:56 -07001542 result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
Eric Anholtde38f0e2010-03-26 12:14:54 -07001543
Eric Anholt3e24ef62010-06-23 12:40:17 -07001544 (void)do_assignment(instructions, state,
Ian Romanicke9015e92011-12-23 09:56:29 -08001545 this->subexpressions[0]->non_lvalue_description,
Ian Romanick85caea22011-03-15 16:33:27 -07001546 op[0]->clone(ctx, NULL), temp_rhs, false,
Eric Anholtde38f0e2010-03-26 12:14:54 -07001547 this->subexpressions[0]->get_location());
1548
Eric Anholtde38f0e2010-03-26 12:14:54 -07001549 error_emitted = op[0]->type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -08001550 break;
Eric Anholtde38f0e2010-03-26 12:14:54 -07001551 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001552
1553 case ast_field_selection:
Ian Romanick18238de2010-03-01 13:49:10 -08001554 result = _mesa_ast_field_selection_to_hir(this, instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001555 break;
1556
Ian Romanick27e3cf82010-04-01 18:03:59 -07001557 case ast_array_index: {
1558 YYLTYPE index_loc = subexpressions[1]->get_location();
1559
1560 op[0] = subexpressions[0]->hir(instructions, state);
1561 op[1] = subexpressions[1]->hir(instructions, state);
1562
Ian Romanick46934ad2013-03-15 14:10:12 -07001563 result = _mesa_ast_array_index_to_hir(ctx, state, op[0], op[1],
Ian Romanick58d93e32013-03-15 15:23:19 -07001564 loc, index_loc);
Ian Romanickb8a21cc2010-04-01 18:31:11 -07001565
Ian Romanick46934ad2013-03-15 14:10:12 -07001566 if (result->type->is_error())
Ian Romanick27e3cf82010-04-01 18:03:59 -07001567 error_emitted = true;
Ian Romanick27e3cf82010-04-01 18:03:59 -07001568
Ian Romanicka87ac252010-02-22 13:19:34 -08001569 break;
Ian Romanick27e3cf82010-04-01 18:03:59 -07001570 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001571
1572 case ast_function_call:
Ian Romanick7cfddf12010-03-10 13:26:52 -08001573 /* Should *NEVER* get here. ast_function_call should always be handled
1574 * by ast_function_expression::hir.
Ian Romanicka87ac252010-02-22 13:19:34 -08001575 */
Ian Romanick7cfddf12010-03-10 13:26:52 -08001576 assert(0);
Ian Romanicka87ac252010-02-22 13:19:34 -08001577 break;
1578
1579 case ast_identifier: {
1580 /* ast_identifier can appear several places in a full abstract syntax
1581 * tree. This particular use must be at location specified in the grammar
1582 * as 'variable_identifier'.
1583 */
Ian Romanick8bde4ce2010-03-19 11:57:24 -07001584 ir_variable *var =
1585 state->symbols->get_variable(this->primary_expression.identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08001586
Ian Romanicka87ac252010-02-22 13:19:34 -08001587 if (var != NULL) {
Ian Romanickbd330552011-01-07 18:34:58 -08001588 var->used = true;
Kenneth Graunkedca19a72012-03-13 14:59:42 -07001589 result = new(ctx) ir_dereference_variable(var);
Ian Romanicka87ac252010-02-22 13:19:34 -08001590 } else {
Ian Romanick71d0bbf2010-03-23 13:21:19 -07001591 _mesa_glsl_error(& loc, state, "`%s' undeclared",
Ian Romanick18238de2010-03-01 13:49:10 -08001592 this->primary_expression.identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08001593
Kenneth Graunke807e9672011-09-22 15:04:56 -07001594 result = ir_rvalue::error_value(ctx);
Ian Romanicka87ac252010-02-22 13:19:34 -08001595 error_emitted = true;
1596 }
1597 break;
1598 }
1599
1600 case ast_int_constant:
Carl Worth1660a292010-06-23 18:11:51 -07001601 result = new(ctx) ir_constant(this->primary_expression.int_constant);
Ian Romanicka87ac252010-02-22 13:19:34 -08001602 break;
1603
1604 case ast_uint_constant:
Carl Worth1660a292010-06-23 18:11:51 -07001605 result = new(ctx) ir_constant(this->primary_expression.uint_constant);
Ian Romanicka87ac252010-02-22 13:19:34 -08001606 break;
1607
1608 case ast_float_constant:
Carl Worth1660a292010-06-23 18:11:51 -07001609 result = new(ctx) ir_constant(this->primary_expression.float_constant);
Ian Romanicka87ac252010-02-22 13:19:34 -08001610 break;
1611
1612 case ast_bool_constant:
Carl Worth1660a292010-06-23 18:11:51 -07001613 result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
Ian Romanicka87ac252010-02-22 13:19:34 -08001614 break;
1615
1616 case ast_sequence: {
Ian Romanicka87ac252010-02-22 13:19:34 -08001617 /* It should not be possible to generate a sequence in the AST without
1618 * any expressions in it.
1619 */
Ian Romanick304ea902010-05-10 11:17:53 -07001620 assert(!this->expressions.is_empty());
Ian Romanicka87ac252010-02-22 13:19:34 -08001621
1622 /* The r-value of a sequence is the last expression in the sequence. If
1623 * the other expressions in the sequence do not have side-effects (and
1624 * therefore add instructions to the instruction list), they get dropped
1625 * on the floor.
1626 */
Ian Romanick3d5cfcf2011-04-11 10:10:30 -07001627 exec_node *previous_tail_pred = NULL;
1628 YYLTYPE previous_operand_loc = loc;
1629
1630 foreach_list_typed (ast_node, ast, link, &this->expressions) {
1631 /* If one of the operands of comma operator does not generate any
1632 * code, we want to emit a warning. At each pass through the loop
1633 * previous_tail_pred will point to the last instruction in the
1634 * stream *before* processing the previous operand. Naturally,
1635 * instructions->tail_pred will point to the last instruction in the
1636 * stream *after* processing the previous operand. If the two
1637 * pointers match, then the previous operand had no effect.
1638 *
1639 * The warning behavior here differs slightly from GCC. GCC will
1640 * only emit a warning if none of the left-hand operands have an
1641 * effect. However, it will emit a warning for each. I believe that
1642 * there are some cases in C (especially with GCC extensions) where
1643 * it is useful to have an intermediate step in a sequence have no
1644 * effect, but I don't think these cases exist in GLSL. Either way,
1645 * it would be a giant hassle to replicate that behavior.
1646 */
1647 if (previous_tail_pred == instructions->tail_pred) {
1648 _mesa_glsl_warning(&previous_operand_loc, state,
1649 "left-hand operand of comma expression has "
1650 "no effect");
1651 }
1652
1653 /* tail_pred is directly accessed instead of using the get_tail()
1654 * method for performance reasons. get_tail() has extra code to
1655 * return NULL when the list is empty. We don't care about that
1656 * here, so using tail_pred directly is fine.
1657 */
1658 previous_tail_pred = instructions->tail_pred;
1659 previous_operand_loc = ast->get_location();
1660
Ian Romanick304ea902010-05-10 11:17:53 -07001661 result = ast->hir(instructions, state);
Ian Romanick3d5cfcf2011-04-11 10:10:30 -07001662 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001663
Ian Romanicka87ac252010-02-22 13:19:34 -08001664 /* Any errors should have already been emitted in the loop above.
1665 */
1666 error_emitted = true;
1667 break;
1668 }
1669 }
Kenneth Graunke08ba9772011-04-14 17:21:59 -07001670 type = NULL; /* use result->type, not type. */
1671 assert(result != NULL);
Ian Romanicka87ac252010-02-22 13:19:34 -08001672
Kenneth Graunke08ba9772011-04-14 17:21:59 -07001673 if (result->type->is_error() && !error_emitted)
Ian Romanick71d0bbf2010-03-23 13:21:19 -07001674 _mesa_glsl_error(& loc, state, "type mismatch");
Ian Romanicka87ac252010-02-22 13:19:34 -08001675
1676 return result;
1677}
1678
1679
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001680ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08001681ast_expression_statement::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08001682 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08001683{
Ian Romanicka87ac252010-02-22 13:19:34 -08001684 /* It is possible to have expression statements that don't have an
1685 * expression. This is the solitary semicolon:
1686 *
1687 * for (i = 0; i < 5; i++)
1688 * ;
1689 *
1690 * In this case the expression will be NULL. Test for NULL and don't do
1691 * anything in that case.
1692 */
Ian Romanick18238de2010-03-01 13:49:10 -08001693 if (expression != NULL)
1694 expression->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001695
1696 /* Statements do not have r-values.
1697 */
1698 return NULL;
1699}
1700
1701
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001702ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08001703ast_compound_statement::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08001704 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08001705{
Ian Romanick18238de2010-03-01 13:49:10 -08001706 if (new_scope)
Ian Romanick8bde4ce2010-03-19 11:57:24 -07001707 state->symbols->push_scope();
Ian Romanicka87ac252010-02-22 13:19:34 -08001708
Ian Romanick2b97dc62010-05-10 17:42:05 -07001709 foreach_list_typed (ast_node, ast, link, &this->statements)
Ian Romanick304ea902010-05-10 11:17:53 -07001710 ast->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001711
Ian Romanick18238de2010-03-01 13:49:10 -08001712 if (new_scope)
Ian Romanick8bde4ce2010-03-19 11:57:24 -07001713 state->symbols->pop_scope();
Ian Romanicka87ac252010-02-22 13:19:34 -08001714
1715 /* Compound statements do not have r-values.
1716 */
1717 return NULL;
1718}
1719
1720
Ian Romanick28009cd2010-03-30 16:59:27 -07001721static const glsl_type *
Kenneth Graunked8e34e22010-08-07 02:56:01 -07001722process_array_type(YYLTYPE *loc, const glsl_type *base, ast_node *array_size,
Ian Romanick28009cd2010-03-30 16:59:27 -07001723 struct _mesa_glsl_parse_state *state)
1724{
1725 unsigned length = 0;
1726
Ian Romanickee55b842013-04-08 16:53:46 -07001727 if (base == NULL)
1728 return glsl_type::error_type;
1729
Ian Romanicka04211e2011-10-24 11:45:50 -07001730 /* From page 19 (page 25) of the GLSL 1.20 spec:
1731 *
1732 * "Only one-dimensional arrays may be declared."
1733 */
1734 if (base->is_array()) {
1735 _mesa_glsl_error(loc, state,
1736 "invalid array of `%s' (only one-dimensional arrays "
1737 "may be declared)",
1738 base->name);
1739 return glsl_type::error_type;
1740 }
Ian Romanick28009cd2010-03-30 16:59:27 -07001741
1742 if (array_size != NULL) {
1743 exec_list dummy_instructions;
1744 ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
1745 YYLTYPE loc = array_size->get_location();
1746
Ian Romanick28009cd2010-03-30 16:59:27 -07001747 if (ir != NULL) {
1748 if (!ir->type->is_integer()) {
1749 _mesa_glsl_error(& loc, state, "array size must be integer type");
1750 } else if (!ir->type->is_scalar()) {
1751 _mesa_glsl_error(& loc, state, "array size must be scalar type");
1752 } else {
1753 ir_constant *const size = ir->constant_expression_value();
1754
1755 if (size == NULL) {
1756 _mesa_glsl_error(& loc, state, "array size must be a "
1757 "constant valued expression");
1758 } else if (size->value.i[0] <= 0) {
1759 _mesa_glsl_error(& loc, state, "array size must be > 0");
1760 } else {
1761 assert(size->type == ir->type);
1762 length = size->value.u[0];
Paul Berryd4144a12011-08-01 15:23:07 -07001763
1764 /* If the array size is const (and we've verified that
1765 * it is) then no instructions should have been emitted
1766 * when we converted it to HIR. If they were emitted,
1767 * then either the array size isn't const after all, or
1768 * we are emitting unnecessary instructions.
1769 */
1770 assert(dummy_instructions.is_empty());
Ian Romanick28009cd2010-03-30 16:59:27 -07001771 }
1772 }
1773 }
Kenneth Graunked8e34e22010-08-07 02:56:01 -07001774 } else if (state->es_shader) {
1775 /* Section 10.17 of the GLSL ES 1.00 specification states that unsized
1776 * array declarations have been removed from the language.
1777 */
1778 _mesa_glsl_error(loc, state, "unsized array declarations are not "
1779 "allowed in GLSL ES 1.00.");
Ian Romanick28009cd2010-03-30 16:59:27 -07001780 }
1781
Ian Romanickee55b842013-04-08 16:53:46 -07001782 const glsl_type *array_type = glsl_type::get_array_instance(base, length);
1783 return array_type != NULL ? array_type : glsl_type::error_type;
Ian Romanick28009cd2010-03-30 16:59:27 -07001784}
1785
1786
Ian Romanickd612a122010-03-31 16:22:06 -07001787const glsl_type *
1788ast_type_specifier::glsl_type(const char **name,
1789 struct _mesa_glsl_parse_state *state) const
Ian Romanicka87ac252010-02-22 13:19:34 -08001790{
Ian Romanickd612a122010-03-31 16:22:06 -07001791 const struct glsl_type *type;
Ian Romanicka87ac252010-02-22 13:19:34 -08001792
Kenneth Graunkeca92ae22010-09-18 11:11:09 +02001793 type = state->symbols->get_type(this->type_name);
1794 *name = this->type_name;
Ian Romanicka87ac252010-02-22 13:19:34 -08001795
Kenneth Graunkeca92ae22010-09-18 11:11:09 +02001796 if (this->is_array) {
1797 YYLTYPE loc = this->get_location();
1798 type = process_array_type(&loc, type, this->array_size, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001799 }
1800
1801 return type;
1802}
1803
1804
Paul Berry18720552012-12-18 15:24:39 -08001805/**
1806 * Determine whether a toplevel variable declaration declares a varying. This
1807 * function operates by examining the variable's mode and the shader target,
1808 * so it correctly identifies linkage variables regardless of whether they are
1809 * declared using the deprecated "varying" syntax or the new "in/out" syntax.
1810 *
1811 * Passing a non-toplevel variable declaration (e.g. a function parameter) to
1812 * this function will produce undefined results.
1813 */
1814static bool
1815is_varying_var(ir_variable *var, _mesa_glsl_parser_targets target)
1816{
1817 switch (target) {
1818 case vertex_shader:
Paul Berry42a29d82013-01-11 14:39:32 -08001819 return var->mode == ir_var_shader_out;
Paul Berry18720552012-12-18 15:24:39 -08001820 case fragment_shader:
Paul Berry42a29d82013-01-11 14:39:32 -08001821 return var->mode == ir_var_shader_in;
Paul Berry18720552012-12-18 15:24:39 -08001822 default:
Paul Berry42a29d82013-01-11 14:39:32 -08001823 return var->mode == ir_var_shader_out || var->mode == ir_var_shader_in;
Paul Berry18720552012-12-18 15:24:39 -08001824 }
1825}
1826
1827
Ian Romanickbb47a4d2013-01-16 12:01:50 -08001828/**
1829 * Matrix layout qualifiers are only allowed on certain types
1830 */
1831static void
1832validate_matrix_layout_for_type(struct _mesa_glsl_parse_state *state,
1833 YYLTYPE *loc,
1834 const glsl_type *type)
1835{
1836 if (!type->is_matrix() && !type->is_record()) {
1837 _mesa_glsl_error(loc, state,
1838 "uniform block layout qualifiers row_major and "
1839 "column_major can only be applied to matrix and "
1840 "structure types");
1841 } else if (type->is_record()) {
1842 /* We allow 'layout(row_major)' on structure types because it's the only
1843 * way to get row-major layouts on matrices contained in structures.
1844 */
1845 _mesa_glsl_warning(loc, state,
1846 "uniform block layout qualifiers row_major and "
1847 "column_major applied to structure types is not "
1848 "strictly conformant and my be rejected by other "
1849 "compilers");
1850 }
1851}
1852
Kenneth Graunke5e5e1202013-07-16 12:03:28 -07001853static bool
1854validate_binding_qualifier(struct _mesa_glsl_parse_state *state,
1855 YYLTYPE *loc,
1856 ir_variable *var,
1857 const ast_type_qualifier *qual)
1858{
1859 if (var->mode != ir_var_uniform) {
1860 _mesa_glsl_error(loc, state,
1861 "the \"binding\" qualifier only applies to uniforms.\n");
1862 return false;
1863 }
1864
1865 if (qual->binding < 0) {
1866 _mesa_glsl_error(loc, state, "binding values must be >= 0.\n");
1867 return false;
1868 }
1869
1870 const struct gl_context *const ctx = state->ctx;
1871 unsigned elements = var->type->is_array() ? var->type->length : 1;
1872 unsigned max_index = qual->binding + elements - 1;
1873
1874 if (var->type->is_interface()) {
1875 /* UBOs. From page 60 of the GLSL 4.20 specification:
1876 * "If the binding point for any uniform block instance is less than zero,
1877 * or greater than or equal to the implementation-dependent maximum
1878 * number of uniform buffer bindings, a compilation error will occur.
1879 * When the binding identifier is used with a uniform block instanced as
1880 * an array of size N, all elements of the array from binding through
1881 * binding + N – 1 must be within this range."
1882 *
1883 * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
1884 */
1885 if (max_index >= ctx->Const.MaxUniformBufferBindings) {
1886 _mesa_glsl_error(loc, state, "layout(binding = %d) for %d UBOs exceeds "
1887 "the maximum number of UBO binding points (%d).\n",
1888 qual->binding, elements,
1889 ctx->Const.MaxUniformBufferBindings);
1890 return false;
1891 }
1892 } else if (var->type->is_sampler() ||
1893 (var->type->is_array() && var->type->fields.array->is_sampler())) {
1894 /* Samplers. From page 63 of the GLSL 4.20 specification:
1895 * "If the binding is less than zero, or greater than or equal to the
1896 * implementation-dependent maximum supported number of units, a
1897 * compilation error will occur. When the binding identifier is used
1898 * with an array of size N, all elements of the array from binding
1899 * through binding + N - 1 must be within this range."
1900 */
1901 unsigned limit;
1902 switch (state->target) {
1903 case vertex_shader:
1904 limit = ctx->Const.VertexProgram.MaxTextureImageUnits;
1905 break;
1906 case geometry_shader:
1907 limit = ctx->Const.GeometryProgram.MaxTextureImageUnits;
1908 break;
1909 case fragment_shader:
1910 limit = ctx->Const.FragmentProgram.MaxTextureImageUnits;
1911 break;
1912 }
1913
1914 if (max_index >= limit) {
1915 _mesa_glsl_error(loc, state, "layout(binding = %d) for %d samplers "
1916 "exceeds the maximum number of texture image units "
1917 "(%d).\n", qual->binding, elements, limit);
1918
1919 return false;
1920 }
1921 } else {
1922 _mesa_glsl_error(loc, state,
1923 "the \"binding\" qualifier only applies to uniform "
1924 "blocks, samplers, or arrays of samplers.\n");
1925 return false;
1926 }
1927
1928 return true;
1929}
1930
Ian Romanicka87ac252010-02-22 13:19:34 -08001931static void
1932apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
Ian Romanick768b55a2010-08-13 16:46:43 -07001933 ir_variable *var,
Eric Anholt2e063f12010-03-28 00:56:22 -07001934 struct _mesa_glsl_parse_state *state,
Eric Anholtf7561e82012-04-26 18:19:39 -07001935 YYLTYPE *loc,
Paul Berryc33be482012-12-18 14:49:34 -08001936 bool ubo_qualifiers_valid,
1937 bool is_parameter)
Ian Romanicka87ac252010-02-22 13:19:34 -08001938{
Ian Romanickbd330552011-01-07 18:34:58 -08001939 if (qual->flags.q.invariant) {
1940 if (var->used) {
1941 _mesa_glsl_error(loc, state,
1942 "variable `%s' may not be redeclared "
1943 "`invariant' after being used",
1944 var->name);
1945 } else {
1946 var->invariant = 1;
1947 }
1948 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001949
Ian Romanicke24d35a2010-10-05 16:38:47 -07001950 if (qual->flags.q.constant || qual->flags.q.attribute
1951 || qual->flags.q.uniform
1952 || (qual->flags.q.varying && (state->target == fragment_shader)))
Ian Romanicka87ac252010-02-22 13:19:34 -08001953 var->read_only = 1;
1954
Ian Romanicke24d35a2010-10-05 16:38:47 -07001955 if (qual->flags.q.centroid)
Ian Romanicka87ac252010-02-22 13:19:34 -08001956 var->centroid = 1;
1957
Ian Romanicke24d35a2010-10-05 16:38:47 -07001958 if (qual->flags.q.attribute && state->target != vertex_shader) {
Eric Anholt2e063f12010-03-28 00:56:22 -07001959 var->type = glsl_type::error_type;
1960 _mesa_glsl_error(loc, state,
1961 "`attribute' variables may not be declared in the "
Ian Romanickae4c4c02010-04-07 16:41:40 -07001962 "%s shader",
1963 _mesa_glsl_shader_target_name(state->target));
Eric Anholt2e063f12010-03-28 00:56:22 -07001964 }
1965
Ian Romanick7e2aa912010-07-19 17:12:42 -07001966 /* If there is no qualifier that changes the mode of the variable, leave
1967 * the setting alone.
1968 */
Ian Romanicke24d35a2010-10-05 16:38:47 -07001969 if (qual->flags.q.in && qual->flags.q.out)
Paul Berry42a29d82013-01-11 14:39:32 -08001970 var->mode = ir_var_function_inout;
1971 else if (qual->flags.q.in)
1972 var->mode = is_parameter ? ir_var_function_in : ir_var_shader_in;
1973 else if (qual->flags.q.attribute
Ian Romanicke24d35a2010-10-05 16:38:47 -07001974 || (qual->flags.q.varying && (state->target == fragment_shader)))
Paul Berry42a29d82013-01-11 14:39:32 -08001975 var->mode = ir_var_shader_in;
1976 else if (qual->flags.q.out)
1977 var->mode = is_parameter ? ir_var_function_out : ir_var_shader_out;
1978 else if (qual->flags.q.varying && (state->target == vertex_shader))
1979 var->mode = ir_var_shader_out;
Ian Romanicke24d35a2010-10-05 16:38:47 -07001980 else if (qual->flags.q.uniform)
Ian Romanicka87ac252010-02-22 13:19:34 -08001981 var->mode = ir_var_uniform;
Ian Romanicka87ac252010-02-22 13:19:34 -08001982
Paul Berry18720552012-12-18 15:24:39 -08001983 if (!is_parameter && is_varying_var(var, state->target)) {
1984 /* This variable is being used to link data between shader stages (in
1985 * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
1986 * that is allowed for such purposes.
1987 *
1988 * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
1989 *
1990 * "The varying qualifier can be used only with the data types
1991 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
1992 * these."
1993 *
1994 * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
1995 * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
1996 *
1997 * "Fragment inputs can only be signed and unsigned integers and
1998 * integer vectors, float, floating-point vectors, matrices, or
1999 * arrays of these. Structures cannot be input.
2000 *
2001 * Similar text exists in the section on vertex shader outputs.
2002 *
2003 * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
Paul Berryc6a50dd2013-01-04 10:43:01 -08002004 * 3.00 spec allows structs as well. Varying structs are also allowed
2005 * in GLSL 1.50.
Paul Berry18720552012-12-18 15:24:39 -08002006 */
2007 switch (var->type->get_scalar_type()->base_type) {
2008 case GLSL_TYPE_FLOAT:
2009 /* Ok in all GLSL versions */
2010 break;
2011 case GLSL_TYPE_UINT:
2012 case GLSL_TYPE_INT:
2013 if (state->is_version(130, 300))
2014 break;
2015 _mesa_glsl_error(loc, state,
2016 "varying variables must be of base type float in %s",
2017 state->get_version_string());
2018 break;
2019 case GLSL_TYPE_STRUCT:
Paul Berryc6a50dd2013-01-04 10:43:01 -08002020 if (state->is_version(150, 300))
2021 break;
Paul Berry18720552012-12-18 15:24:39 -08002022 _mesa_glsl_error(loc, state,
2023 "varying variables may not be of type struct");
2024 break;
2025 default:
2026 _mesa_glsl_error(loc, state, "illegal type for a varying variable");
2027 break;
2028 }
2029 }
2030
Ian Romanick86b43982011-01-06 10:49:56 -08002031 if (state->all_invariant && (state->current_function == NULL)) {
2032 switch (state->target) {
2033 case vertex_shader:
Paul Berry42a29d82013-01-11 14:39:32 -08002034 if (var->mode == ir_var_shader_out)
Ian Romanick86b43982011-01-06 10:49:56 -08002035 var->invariant = true;
2036 break;
2037 case geometry_shader:
Paul Berry42a29d82013-01-11 14:39:32 -08002038 if ((var->mode == ir_var_shader_in)
2039 || (var->mode == ir_var_shader_out))
Ian Romanick86b43982011-01-06 10:49:56 -08002040 var->invariant = true;
2041 break;
2042 case fragment_shader:
Paul Berry42a29d82013-01-11 14:39:32 -08002043 if (var->mode == ir_var_shader_in)
Ian Romanick86b43982011-01-06 10:49:56 -08002044 var->invariant = true;
2045 break;
2046 }
2047 }
2048
Ian Romanicke24d35a2010-10-05 16:38:47 -07002049 if (qual->flags.q.flat)
Paul Berrycf459492011-10-25 18:06:37 -07002050 var->interpolation = INTERP_QUALIFIER_FLAT;
Ian Romanicke24d35a2010-10-05 16:38:47 -07002051 else if (qual->flags.q.noperspective)
Paul Berrycf459492011-10-25 18:06:37 -07002052 var->interpolation = INTERP_QUALIFIER_NOPERSPECTIVE;
Paul Berryc4881502011-10-21 07:40:37 -07002053 else if (qual->flags.q.smooth)
Paul Berrycf459492011-10-25 18:06:37 -07002054 var->interpolation = INTERP_QUALIFIER_SMOOTH;
Paul Berryc4881502011-10-21 07:40:37 -07002055 else
2056 var->interpolation = INTERP_QUALIFIER_NONE;
Ian Romanick9d975372010-04-02 17:17:47 -07002057
Eric Anholt916e2062012-01-09 16:40:20 -08002058 if (var->interpolation != INTERP_QUALIFIER_NONE &&
Paul Berry42a29d82013-01-11 14:39:32 -08002059 !(state->target == vertex_shader && var->mode == ir_var_shader_out) &&
2060 !(state->target == fragment_shader && var->mode == ir_var_shader_in)) {
Eric Anholt916e2062012-01-09 16:40:20 -08002061 _mesa_glsl_error(loc, state,
2062 "interpolation qualifier `%s' can only be applied to "
2063 "vertex shader outputs and fragment shader inputs.",
Kenneth Graunked43f4182012-06-06 01:52:47 -07002064 var->interpolation_string());
Eric Anholt916e2062012-01-09 16:40:20 -08002065 }
2066
Ian Romanicke24d35a2010-10-05 16:38:47 -07002067 var->pixel_center_integer = qual->flags.q.pixel_center_integer;
2068 var->origin_upper_left = qual->flags.q.origin_upper_left;
2069 if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer)
Ian Romanick8d8469e2010-06-30 17:48:09 -07002070 && (strcmp(var->name, "gl_FragCoord") != 0)) {
Ian Romanicke24d35a2010-10-05 16:38:47 -07002071 const char *const qual_string = (qual->flags.q.origin_upper_left)
Ian Romanick8d8469e2010-06-30 17:48:09 -07002072 ? "origin_upper_left" : "pixel_center_integer";
2073
2074 _mesa_glsl_error(loc, state,
2075 "layout qualifier `%s' can only be applied to "
2076 "fragment shader input `gl_FragCoord'",
2077 qual_string);
2078 }
2079
Ian Romanickeee68d32010-10-07 15:13:38 -07002080 if (qual->flags.q.explicit_location) {
2081 const bool global_scope = (state->current_function == NULL);
2082 bool fail = false;
2083 const char *string = "";
2084
2085 /* In the vertex shader only shader inputs can be given explicit
2086 * locations.
2087 *
2088 * In the fragment shader only shader outputs can be given explicit
2089 * locations.
2090 */
2091 switch (state->target) {
2092 case vertex_shader:
Paul Berry42a29d82013-01-11 14:39:32 -08002093 if (!global_scope || (var->mode != ir_var_shader_in)) {
Ian Romanickeee68d32010-10-07 15:13:38 -07002094 fail = true;
2095 string = "input";
2096 }
2097 break;
2098
2099 case geometry_shader:
2100 _mesa_glsl_error(loc, state,
2101 "geometry shader variables cannot be given "
2102 "explicit locations\n");
2103 break;
2104
2105 case fragment_shader:
Paul Berry42a29d82013-01-11 14:39:32 -08002106 if (!global_scope || (var->mode != ir_var_shader_out)) {
Ian Romanickeee68d32010-10-07 15:13:38 -07002107 fail = true;
2108 string = "output";
2109 }
2110 break;
Kenneth Graunkea75da2c2010-10-20 14:59:40 -07002111 };
Ian Romanickeee68d32010-10-07 15:13:38 -07002112
2113 if (fail) {
2114 _mesa_glsl_error(loc, state,
2115 "only %s shader %s variables can be given an "
2116 "explicit location\n",
2117 _mesa_glsl_shader_target_name(state->target),
2118 string);
2119 } else {
2120 var->explicit_location = true;
Ian Romanick68a4fc92010-10-07 17:21:22 -07002121
2122 /* This bit of silliness is needed because invalid explicit locations
2123 * are supposed to be flagged during linking. Small negative values
2124 * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
2125 * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
2126 * The linker needs to be able to differentiate these cases. This
2127 * ensures that negative values stay negative.
2128 */
2129 if (qual->location >= 0) {
2130 var->location = (state->target == vertex_shader)
2131 ? (qual->location + VERT_ATTRIB_GENERIC0)
2132 : (qual->location + FRAG_RESULT_DATA0);
2133 } else {
2134 var->location = qual->location;
2135 }
Kenneth Graunke354f2cb2012-08-31 16:04:19 -07002136
Dave Airlie1256a5d2012-03-24 13:33:41 +00002137 if (qual->flags.q.explicit_index) {
Kenneth Graunke354f2cb2012-08-31 16:04:19 -07002138 /* From the GLSL 4.30 specification, section 4.4.2 (Output
2139 * Layout Qualifiers):
2140 *
2141 * "It is also a compile-time error if a fragment shader
2142 * sets a layout index to less than 0 or greater than 1."
2143 *
2144 * Older specifications don't mandate a behavior; we take
2145 * this as a clarification and always generate the error.
2146 */
2147 if (qual->index < 0 || qual->index > 1) {
2148 _mesa_glsl_error(loc, state,
2149 "explicit index may only be 0 or 1\n");
2150 } else {
2151 var->explicit_index = true;
2152 var->index = qual->index;
2153 }
Dave Airlie1256a5d2012-03-24 13:33:41 +00002154 }
Ian Romanickeee68d32010-10-07 15:13:38 -07002155 }
Dave Airlie1256a5d2012-03-24 13:33:41 +00002156 } else if (qual->flags.q.explicit_index) {
2157 _mesa_glsl_error(loc, state,
2158 "explicit index requires explicit location\n");
Ian Romanickeee68d32010-10-07 15:13:38 -07002159 }
2160
Kenneth Graunke5e5e1202013-07-16 12:03:28 -07002161 if (qual->flags.q.explicit_binding)
2162 validate_binding_qualifier(state, loc, var, qual);
2163
Ian Romanick4bcff0c2011-01-07 16:53:59 -08002164 /* Does the declaration use the deprecated 'attribute' or 'varying'
2165 * keywords?
2166 */
2167 const bool uses_deprecated_qualifier = qual->flags.q.attribute
2168 || qual->flags.q.varying;
2169
2170 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
2171 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
2172 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
2173 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
2174 * These extensions and all following extensions that add the 'layout'
2175 * keyword have been modified to require the use of 'in' or 'out'.
2176 *
2177 * The following extension do not allow the deprecated keywords:
2178 *
2179 * GL_AMD_conservative_depth
Marek Olšák6b43d6f2011-11-19 16:41:08 +01002180 * GL_ARB_conservative_depth
Ian Romanick4bcff0c2011-01-07 16:53:59 -08002181 * GL_ARB_gpu_shader5
2182 * GL_ARB_separate_shader_objects
2183 * GL_ARB_tesselation_shader
2184 * GL_ARB_transform_feedback3
2185 * GL_ARB_uniform_buffer_object
2186 *
2187 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
2188 * allow layout with the deprecated keywords.
2189 */
2190 const bool relaxed_layout_qualifier_checking =
2191 state->ARB_fragment_coord_conventions_enable;
2192
Kenneth Graunke56bcde32013-07-16 11:39:01 -07002193 if (qual->has_layout() && uses_deprecated_qualifier) {
Ian Romanick4bcff0c2011-01-07 16:53:59 -08002194 if (relaxed_layout_qualifier_checking) {
2195 _mesa_glsl_warning(loc, state,
2196 "`layout' qualifier may not be used with "
2197 "`attribute' or `varying'");
2198 } else {
2199 _mesa_glsl_error(loc, state,
2200 "`layout' qualifier may not be used with "
2201 "`attribute' or `varying'");
2202 }
2203 }
2204
Chad Versacebc04d242011-01-27 01:40:26 -08002205 /* Layout qualifiers for gl_FragDepth, which are enabled by extension
2206 * AMD_conservative_depth.
2207 */
2208 int depth_layout_count = qual->flags.q.depth_any
2209 + qual->flags.q.depth_greater
2210 + qual->flags.q.depth_less
2211 + qual->flags.q.depth_unchanged;
2212 if (depth_layout_count > 0
Marek Olšák6b43d6f2011-11-19 16:41:08 +01002213 && !state->AMD_conservative_depth_enable
2214 && !state->ARB_conservative_depth_enable) {
Chad Versacebc04d242011-01-27 01:40:26 -08002215 _mesa_glsl_error(loc, state,
Marek Olšák6b43d6f2011-11-19 16:41:08 +01002216 "extension GL_AMD_conservative_depth or "
2217 "GL_ARB_conservative_depth must be enabled "
Chad Versacebc04d242011-01-27 01:40:26 -08002218 "to use depth layout qualifiers");
2219 } else if (depth_layout_count > 0
2220 && strcmp(var->name, "gl_FragDepth") != 0) {
2221 _mesa_glsl_error(loc, state,
2222 "depth layout qualifiers can be applied only to "
2223 "gl_FragDepth");
2224 } else if (depth_layout_count > 1
2225 && strcmp(var->name, "gl_FragDepth") == 0) {
2226 _mesa_glsl_error(loc, state,
2227 "at most one depth layout qualifier can be applied to "
2228 "gl_FragDepth");
2229 }
2230 if (qual->flags.q.depth_any)
2231 var->depth_layout = ir_depth_layout_any;
2232 else if (qual->flags.q.depth_greater)
2233 var->depth_layout = ir_depth_layout_greater;
2234 else if (qual->flags.q.depth_less)
2235 var->depth_layout = ir_depth_layout_less;
2236 else if (qual->flags.q.depth_unchanged)
2237 var->depth_layout = ir_depth_layout_unchanged;
2238 else
2239 var->depth_layout = ir_depth_layout_none;
Eric Anholtf7561e82012-04-26 18:19:39 -07002240
2241 if (qual->flags.q.std140 ||
2242 qual->flags.q.packed ||
2243 qual->flags.q.shared) {
2244 _mesa_glsl_error(loc, state,
2245 "uniform block layout qualifiers std140, packed, and "
2246 "shared can only be applied to uniform blocks, not "
2247 "members");
2248 }
2249
Ian Romanickbb47a4d2013-01-16 12:01:50 -08002250 if (qual->flags.q.row_major || qual->flags.q.column_major) {
2251 if (!ubo_qualifiers_valid) {
2252 _mesa_glsl_error(loc, state,
2253 "uniform block layout qualifiers row_major and "
2254 "column_major can only be applied to uniform block "
2255 "members");
2256 } else
2257 validate_matrix_layout_for_type(state, loc, var->type);
Eric Anholtf7561e82012-04-26 18:19:39 -07002258 }
Ian Romanicka87ac252010-02-22 13:19:34 -08002259}
2260
Ian Romanick8e6cb9f2011-03-04 15:28:40 -08002261/**
2262 * Get the variable that is being redeclared by this declaration
2263 *
2264 * Semantic checks to verify the validity of the redeclaration are also
2265 * performed. If semantic checks fail, compilation error will be emitted via
2266 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
2267 *
2268 * \returns
2269 * A pointer to an existing variable in the current scope if the declaration
2270 * is a redeclaration, \c NULL otherwise.
2271 */
2272ir_variable *
2273get_variable_being_redeclared(ir_variable *var, ast_declaration *decl,
2274 struct _mesa_glsl_parse_state *state)
2275{
2276 /* Check if this declaration is actually a re-declaration, either to
2277 * resize an array or add qualifiers to an existing variable.
2278 *
2279 * This is allowed for variables in the current scope, or when at
2280 * global scope (for built-ins in the implicit outer scope).
2281 */
2282 ir_variable *earlier = state->symbols->get_variable(decl->identifier);
2283 if (earlier == NULL ||
2284 (state->current_function != NULL &&
2285 !state->symbols->name_declared_this_scope(decl->identifier))) {
2286 return NULL;
2287 }
2288
2289
2290 YYLTYPE loc = decl->get_location();
2291
2292 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
2293 *
2294 * "It is legal to declare an array without a size and then
2295 * later re-declare the same name as an array of the same
2296 * type and specify a size."
2297 */
2298 if ((earlier->type->array_size() == 0)
2299 && var->type->is_array()
2300 && (var->type->element_type() == earlier->type->element_type())) {
2301 /* FINISHME: This doesn't match the qualifiers on the two
2302 * FINISHME: declarations. It's not 100% clear whether this is
2303 * FINISHME: required or not.
2304 */
2305
Ian Romanick8e6cb9f2011-03-04 15:28:40 -08002306 const unsigned size = unsigned(var->type->array_size());
Paul Berry93b97582011-09-06 10:01:51 -07002307 check_builtin_array_max_size(var->name, size, loc, state);
2308 if ((size > 0) && (size <= earlier->max_array_access)) {
Ian Romanick8e6cb9f2011-03-04 15:28:40 -08002309 _mesa_glsl_error(& loc, state, "array size must be > %u due to "
2310 "previous access",
2311 earlier->max_array_access);
2312 }
2313
2314 earlier->type = var->type;
2315 delete var;
2316 var = NULL;
2317 } else if (state->ARB_fragment_coord_conventions_enable
2318 && strcmp(var->name, "gl_FragCoord") == 0
2319 && earlier->type == var->type
2320 && earlier->mode == var->mode) {
2321 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
2322 * qualifiers.
2323 */
2324 earlier->origin_upper_left = var->origin_upper_left;
2325 earlier->pixel_center_integer = var->pixel_center_integer;
2326
2327 /* According to section 4.3.7 of the GLSL 1.30 spec,
2328 * the following built-in varaibles can be redeclared with an
2329 * interpolation qualifier:
2330 * * gl_FrontColor
2331 * * gl_BackColor
2332 * * gl_FrontSecondaryColor
2333 * * gl_BackSecondaryColor
2334 * * gl_Color
2335 * * gl_SecondaryColor
2336 */
Paul Berrye3ded7f2012-08-01 14:50:05 -07002337 } else if (state->is_version(130, 0)
Ian Romanick8e6cb9f2011-03-04 15:28:40 -08002338 && (strcmp(var->name, "gl_FrontColor") == 0
2339 || strcmp(var->name, "gl_BackColor") == 0
2340 || strcmp(var->name, "gl_FrontSecondaryColor") == 0
2341 || strcmp(var->name, "gl_BackSecondaryColor") == 0
2342 || strcmp(var->name, "gl_Color") == 0
2343 || strcmp(var->name, "gl_SecondaryColor") == 0)
2344 && earlier->type == var->type
2345 && earlier->mode == var->mode) {
2346 earlier->interpolation = var->interpolation;
2347
2348 /* Layout qualifiers for gl_FragDepth. */
Marek Olšák6b43d6f2011-11-19 16:41:08 +01002349 } else if ((state->AMD_conservative_depth_enable ||
2350 state->ARB_conservative_depth_enable)
Ian Romanick8e6cb9f2011-03-04 15:28:40 -08002351 && strcmp(var->name, "gl_FragDepth") == 0
2352 && earlier->type == var->type
2353 && earlier->mode == var->mode) {
2354
2355 /** From the AMD_conservative_depth spec:
2356 * Within any shader, the first redeclarations of gl_FragDepth
2357 * must appear before any use of gl_FragDepth.
2358 */
2359 if (earlier->used) {
2360 _mesa_glsl_error(&loc, state,
2361 "the first redeclaration of gl_FragDepth "
2362 "must appear before any use of gl_FragDepth");
2363 }
2364
2365 /* Prevent inconsistent redeclaration of depth layout qualifier. */
2366 if (earlier->depth_layout != ir_depth_layout_none
2367 && earlier->depth_layout != var->depth_layout) {
2368 _mesa_glsl_error(&loc, state,
2369 "gl_FragDepth: depth layout is declared here "
2370 "as '%s, but it was previously declared as "
2371 "'%s'",
2372 depth_layout_string(var->depth_layout),
2373 depth_layout_string(earlier->depth_layout));
2374 }
2375
2376 earlier->depth_layout = var->depth_layout;
2377
2378 } else {
2379 _mesa_glsl_error(&loc, state, "`%s' redeclared", decl->identifier);
2380 }
2381
2382 return earlier;
2383}
Ian Romanicka87ac252010-02-22 13:19:34 -08002384
Ian Romanick0292ffb2011-03-04 15:29:33 -08002385/**
2386 * Generate the IR for an initializer in a variable declaration
2387 */
2388ir_rvalue *
2389process_initializer(ir_variable *var, ast_declaration *decl,
2390 ast_fully_specified_type *type,
2391 exec_list *initializer_instructions,
2392 struct _mesa_glsl_parse_state *state)
2393{
2394 ir_rvalue *result = NULL;
2395
2396 YYLTYPE initializer_loc = decl->initializer->get_location();
2397
2398 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
2399 *
2400 * "All uniform variables are read-only and are initialized either
2401 * directly by an application via API commands, or indirectly by
2402 * OpenGL."
2403 */
Paul Berry0d9bba62012-08-05 09:57:01 -07002404 if (var->mode == ir_var_uniform) {
2405 state->check_version(120, 0, &initializer_loc,
2406 "cannot initialize uniforms");
Ian Romanick0292ffb2011-03-04 15:29:33 -08002407 }
2408
2409 if (var->type->is_sampler()) {
2410 _mesa_glsl_error(& initializer_loc, state,
2411 "cannot initialize samplers");
2412 }
2413
Paul Berry42a29d82013-01-11 14:39:32 -08002414 if ((var->mode == ir_var_shader_in) && (state->current_function == NULL)) {
Ian Romanick0292ffb2011-03-04 15:29:33 -08002415 _mesa_glsl_error(& initializer_loc, state,
2416 "cannot initialize %s shader input / %s",
2417 _mesa_glsl_shader_target_name(state->target),
2418 (state->target == vertex_shader)
2419 ? "attribute" : "varying");
2420 }
2421
2422 ir_dereference *const lhs = new(state) ir_dereference_variable(var);
2423 ir_rvalue *rhs = decl->initializer->hir(initializer_instructions,
2424 state);
2425
2426 /* Calculate the constant value if this is a const or uniform
2427 * declaration.
2428 */
2429 if (type->qualifier.flags.q.constant
2430 || type->qualifier.flags.q.uniform) {
Ian Romanick85caea22011-03-15 16:33:27 -07002431 ir_rvalue *new_rhs = validate_assignment(state, var->type, rhs, true);
Ian Romanick0292ffb2011-03-04 15:29:33 -08002432 if (new_rhs != NULL) {
2433 rhs = new_rhs;
2434
2435 ir_constant *constant_value = rhs->constant_expression_value();
2436 if (!constant_value) {
Matt Turnerba7b60d2013-05-23 11:48:40 -07002437 /* If ARB_shading_language_420pack is enabled, initializers of
2438 * const-qualified local variables do not have to be constant
2439 * expressions. Const-qualified global variables must still be
2440 * initialized with constant expressions.
2441 */
2442 if (!state->ARB_shading_language_420pack_enable
2443 || state->current_function == NULL) {
2444 _mesa_glsl_error(& initializer_loc, state,
2445 "initializer of %s variable `%s' must be a "
2446 "constant expression",
2447 (type->qualifier.flags.q.constant)
2448 ? "const" : "uniform",
2449 decl->identifier);
2450 if (var->type->is_numeric()) {
2451 /* Reduce cascading errors. */
2452 var->constant_value = ir_constant::zero(state, var->type);
2453 }
2454 }
2455 } else {
Ian Romanick0292ffb2011-03-04 15:29:33 -08002456 rhs = constant_value;
2457 var->constant_value = constant_value;
2458 }
2459 } else {
2460 _mesa_glsl_error(&initializer_loc, state,
2461 "initializer of type %s cannot be assigned to "
2462 "variable of type %s",
2463 rhs->type->name, var->type->name);
2464 if (var->type->is_numeric()) {
2465 /* Reduce cascading errors. */
2466 var->constant_value = ir_constant::zero(state, var->type);
2467 }
2468 }
2469 }
2470
2471 if (rhs && !rhs->type->is_error()) {
2472 bool temp = var->read_only;
2473 if (type->qualifier.flags.q.constant)
2474 var->read_only = false;
2475
2476 /* Never emit code to initialize a uniform.
2477 */
2478 const glsl_type *initializer_type;
2479 if (!type->qualifier.flags.q.uniform) {
2480 result = do_assignment(initializer_instructions, state,
Ian Romanicke9015e92011-12-23 09:56:29 -08002481 NULL,
Ian Romanick85caea22011-03-15 16:33:27 -07002482 lhs, rhs, true,
Ian Romanick0292ffb2011-03-04 15:29:33 -08002483 type->get_location());
2484 initializer_type = result->type;
2485 } else
2486 initializer_type = rhs->type;
2487
Ian Romanickf37b1ad2011-10-31 14:31:07 -07002488 var->constant_initializer = rhs->constant_expression_value();
2489 var->has_initializer = true;
2490
Ian Romanick0292ffb2011-03-04 15:29:33 -08002491 /* If the declared variable is an unsized array, it must inherrit
2492 * its full type from the initializer. A declaration such as
2493 *
2494 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
2495 *
2496 * becomes
2497 *
2498 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
2499 *
2500 * The assignment generated in the if-statement (below) will also
2501 * automatically handle this case for non-uniforms.
2502 *
2503 * If the declared variable is not an array, the types must
2504 * already match exactly. As a result, the type assignment
2505 * here can be done unconditionally. For non-uniforms the call
2506 * to do_assignment can change the type of the initializer (via
2507 * the implicit conversion rules). For uniforms the initializer
2508 * must be a constant expression, and the type of that expression
2509 * was validated above.
2510 */
2511 var->type = initializer_type;
2512
2513 var->read_only = temp;
2514 }
2515
2516 return result;
2517}
2518
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07002519ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08002520ast_declarator_list::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08002521 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08002522{
Kenneth Graunke953ff122010-06-25 13:14:37 -07002523 void *ctx = state;
Ian Romanicka87ac252010-02-22 13:19:34 -08002524 const struct glsl_type *decl_type;
2525 const char *type_name = NULL;
Eric Anholt85584592010-04-14 15:38:52 -07002526 ir_rvalue *result = NULL;
Ian Romanickc824e352010-04-23 15:55:19 -07002527 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08002528
Ian Romanick6f0823d2010-07-01 20:39:08 -07002529 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
2530 *
2531 * "To ensure that a particular output variable is invariant, it is
2532 * necessary to use the invariant qualifier. It can either be used to
2533 * qualify a previously declared variable as being invariant
2534 *
2535 * invariant gl_Position; // make existing gl_Position be invariant"
2536 *
2537 * In these cases the parser will set the 'invariant' flag in the declarator
2538 * list, and the type will be NULL.
2539 */
2540 if (this->invariant) {
2541 assert(this->type == NULL);
2542
2543 if (state->current_function != NULL) {
2544 _mesa_glsl_error(& loc, state,
2545 "All uses of `invariant' keyword must be at global "
2546 "scope\n");
2547 }
2548
2549 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
2550 assert(!decl->is_array);
2551 assert(decl->array_size == NULL);
2552 assert(decl->initializer == NULL);
2553
2554 ir_variable *const earlier =
2555 state->symbols->get_variable(decl->identifier);
2556 if (earlier == NULL) {
2557 _mesa_glsl_error(& loc, state,
2558 "Undeclared variable `%s' cannot be marked "
2559 "invariant\n", decl->identifier);
2560 } else if ((state->target == vertex_shader)
Paul Berry42a29d82013-01-11 14:39:32 -08002561 && (earlier->mode != ir_var_shader_out)) {
Ian Romanick6f0823d2010-07-01 20:39:08 -07002562 _mesa_glsl_error(& loc, state,
2563 "`%s' cannot be marked invariant, vertex shader "
2564 "outputs only\n", decl->identifier);
2565 } else if ((state->target == fragment_shader)
Paul Berry42a29d82013-01-11 14:39:32 -08002566 && (earlier->mode != ir_var_shader_in)) {
Ian Romanick6f0823d2010-07-01 20:39:08 -07002567 _mesa_glsl_error(& loc, state,
2568 "`%s' cannot be marked invariant, fragment shader "
2569 "inputs only\n", decl->identifier);
Ian Romanickbd330552011-01-07 18:34:58 -08002570 } else if (earlier->used) {
2571 _mesa_glsl_error(& loc, state,
2572 "variable `%s' may not be redeclared "
2573 "`invariant' after being used",
2574 earlier->name);
Ian Romanick6f0823d2010-07-01 20:39:08 -07002575 } else {
2576 earlier->invariant = true;
2577 }
2578 }
2579
2580 /* Invariant redeclarations do not have r-values.
2581 */
2582 return NULL;
2583 }
2584
2585 assert(this->type != NULL);
2586 assert(!this->invariant);
2587
Ian Romanick3455ce62010-04-19 15:13:15 -07002588 /* The type specifier may contain a structure definition. Process that
2589 * before any of the variable declarations.
2590 */
2591 (void) this->type->specifier->hir(instructions, state);
2592
Ian Romanickd612a122010-03-31 16:22:06 -07002593 decl_type = this->type->specifier->glsl_type(& type_name, state);
Ian Romanick304ea902010-05-10 11:17:53 -07002594 if (this->declarations.is_empty()) {
Ian Romanickf5ba4d02011-10-25 17:49:07 -07002595 /* If there is no structure involved in the program text, there are two
2596 * possible scenarios:
2597 *
2598 * - The program text contained something like 'vec4;'. This is an
2599 * empty declaration. It is valid but weird. Emit a warning.
2600 *
2601 * - The program text contained something like 'S;' and 'S' is not the
2602 * name of a known structure type. This is both invalid and weird.
2603 * Emit an error.
2604 *
2605 * Note that if decl_type is NULL and there is a structure involved,
2606 * there must have been some sort of error with the structure. In this
2607 * case we assume that an error was already generated on this line of
2608 * code for the structure. There is no need to generate an additional,
2609 * confusing error.
2610 */
2611 assert(this->type->specifier->structure == NULL || decl_type != NULL
2612 || state->error);
2613 if (this->type->specifier->structure == NULL) {
2614 if (decl_type != NULL) {
Chia-I Wu547212d2011-08-04 00:39:07 +09002615 _mesa_glsl_warning(&loc, state, "empty declaration");
Ian Romanickf5ba4d02011-10-25 17:49:07 -07002616 } else {
2617 _mesa_glsl_error(&loc, state,
2618 "invalid type `%s' in empty declaration",
2619 type_name);
Chia-I Wu547212d2011-08-04 00:39:07 +09002620 }
Ian Romanickc824e352010-04-23 15:55:19 -07002621 }
Kenneth Graunke6eec5022013-07-15 15:58:29 -07002622
2623 if (this->type->qualifier.precision != ast_precision_none &&
2624 this->type->specifier->structure != NULL) {
2625 _mesa_glsl_error(&loc, state, "Precision qualifiers can't be applied "
2626 "to structures.\n");
2627 }
Ian Romanickc824e352010-04-23 15:55:19 -07002628 }
Ian Romanicka87ac252010-02-22 13:19:34 -08002629
Ian Romanick2b97dc62010-05-10 17:42:05 -07002630 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
Ian Romanicka87ac252010-02-22 13:19:34 -08002631 const struct glsl_type *var_type;
Ian Romanick768b55a2010-08-13 16:46:43 -07002632 ir_variable *var;
Ian Romanicka87ac252010-02-22 13:19:34 -08002633
2634 /* FINISHME: Emit a warning if a variable declaration shadows a
2635 * FINISHME: declaration at a higher scope.
2636 */
2637
Ian Romanickcec65a62010-03-23 12:28:44 -07002638 if ((decl_type == NULL) || decl_type->is_void()) {
Ian Romanicka87ac252010-02-22 13:19:34 -08002639 if (type_name != NULL) {
2640 _mesa_glsl_error(& loc, state,
2641 "invalid type `%s' in declaration of `%s'",
2642 type_name, decl->identifier);
2643 } else {
2644 _mesa_glsl_error(& loc, state,
2645 "invalid type in declaration of `%s'",
2646 decl->identifier);
2647 }
2648 continue;
2649 }
2650
2651 if (decl->is_array) {
Kenneth Graunked8e34e22010-08-07 02:56:01 -07002652 var_type = process_array_type(&loc, decl_type, decl->array_size,
2653 state);
Ian Romanicka04211e2011-10-24 11:45:50 -07002654 if (var_type->is_error())
2655 continue;
Ian Romanicka87ac252010-02-22 13:19:34 -08002656 } else {
2657 var_type = decl_type;
2658 }
2659
Ian Romanick7e2aa912010-07-19 17:12:42 -07002660 var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto);
Ian Romanicka87ac252010-02-22 13:19:34 -08002661
Eric Anholt3f151502010-04-02 01:53:57 -10002662 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
2663 *
2664 * "Global variables can only use the qualifiers const,
2665 * attribute, uni form, or varying. Only one may be
2666 * specified.
2667 *
2668 * Local variables can only use the qualifier const."
2669 *
Paul Berryd4a24742012-08-02 08:18:12 -07002670 * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
2671 * any extension that adds the 'layout' keyword.
Eric Anholt3f151502010-04-02 01:53:57 -10002672 */
Paul Berryd4a24742012-08-02 08:18:12 -07002673 if (!state->is_version(130, 300)
Ian Romanick82c4b4f2011-01-07 16:53:07 -08002674 && !state->ARB_explicit_attrib_location_enable
2675 && !state->ARB_fragment_coord_conventions_enable) {
Ian Romanicke24d35a2010-10-05 16:38:47 -07002676 if (this->type->qualifier.flags.q.out) {
Eric Anholt3f151502010-04-02 01:53:57 -10002677 _mesa_glsl_error(& loc, state,
2678 "`out' qualifier in declaration of `%s' "
Ian Romanick469ea692011-01-07 16:05:59 -08002679 "only valid for function parameters in %s.",
Paul Berrydc9f9d82012-08-02 06:45:30 -07002680 decl->identifier, state->get_version_string());
Eric Anholt3f151502010-04-02 01:53:57 -10002681 }
Ian Romanicke24d35a2010-10-05 16:38:47 -07002682 if (this->type->qualifier.flags.q.in) {
Eric Anholt3f151502010-04-02 01:53:57 -10002683 _mesa_glsl_error(& loc, state,
2684 "`in' qualifier in declaration of `%s' "
Ian Romanick469ea692011-01-07 16:05:59 -08002685 "only valid for function parameters in %s.",
Paul Berrydc9f9d82012-08-02 06:45:30 -07002686 decl->identifier, state->get_version_string());
Eric Anholt3f151502010-04-02 01:53:57 -10002687 }
2688 /* FINISHME: Test for other invalid qualifiers. */
2689 }
2690
Eric Anholt2e063f12010-03-28 00:56:22 -07002691 apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
Paul Berryc33be482012-12-18 14:49:34 -08002692 & loc, this->ubo_qualifiers_valid, false);
Ian Romanicka87ac252010-02-22 13:19:34 -08002693
Ian Romanicke24d35a2010-10-05 16:38:47 -07002694 if (this->type->qualifier.flags.q.invariant) {
Paul Berry42a29d82013-01-11 14:39:32 -08002695 if ((state->target == vertex_shader) &&
2696 var->mode != ir_var_shader_out) {
Ian Romanick6f0823d2010-07-01 20:39:08 -07002697 _mesa_glsl_error(& loc, state,
2698 "`%s' cannot be marked invariant, vertex shader "
2699 "outputs only\n", var->name);
Eric Anholt046bef22010-08-04 20:33:57 -07002700 } else if ((state->target == fragment_shader) &&
Paul Berry42a29d82013-01-11 14:39:32 -08002701 var->mode != ir_var_shader_in) {
Eric Anholt046bef22010-08-04 20:33:57 -07002702 /* FINISHME: Note that this doesn't work for invariant on
2703 * a function signature inval
2704 */
Ian Romanick6f0823d2010-07-01 20:39:08 -07002705 _mesa_glsl_error(& loc, state,
2706 "`%s' cannot be marked invariant, fragment shader "
2707 "inputs only\n", var->name);
2708 }
2709 }
2710
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07002711 if (state->current_function != NULL) {
Ian Romanickb168e532010-03-31 12:31:18 -07002712 const char *mode = NULL;
Ian Romanicke0800062010-03-31 13:15:23 -07002713 const char *extra = "";
Ian Romanickb168e532010-03-31 12:31:18 -07002714
Ian Romanicke0800062010-03-31 13:15:23 -07002715 /* There is no need to check for 'inout' here because the parser will
2716 * only allow that in function parameter lists.
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07002717 */
Ian Romanicke24d35a2010-10-05 16:38:47 -07002718 if (this->type->qualifier.flags.q.attribute) {
Ian Romanickb168e532010-03-31 12:31:18 -07002719 mode = "attribute";
Ian Romanicke24d35a2010-10-05 16:38:47 -07002720 } else if (this->type->qualifier.flags.q.uniform) {
Ian Romanickb168e532010-03-31 12:31:18 -07002721 mode = "uniform";
Ian Romanicke24d35a2010-10-05 16:38:47 -07002722 } else if (this->type->qualifier.flags.q.varying) {
Ian Romanickb168e532010-03-31 12:31:18 -07002723 mode = "varying";
Ian Romanicke24d35a2010-10-05 16:38:47 -07002724 } else if (this->type->qualifier.flags.q.in) {
Ian Romanicke0800062010-03-31 13:15:23 -07002725 mode = "in";
2726 extra = " or in function parameter list";
Ian Romanicke24d35a2010-10-05 16:38:47 -07002727 } else if (this->type->qualifier.flags.q.out) {
Ian Romanicke0800062010-03-31 13:15:23 -07002728 mode = "out";
2729 extra = " or in function parameter list";
Ian Romanickb168e532010-03-31 12:31:18 -07002730 }
2731
2732 if (mode) {
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07002733 _mesa_glsl_error(& loc, state,
Ian Romanickb168e532010-03-31 12:31:18 -07002734 "%s variable `%s' must be declared at "
Ian Romanicke0800062010-03-31 13:15:23 -07002735 "global scope%s",
2736 mode, var->name, extra);
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07002737 }
Paul Berry42a29d82013-01-11 14:39:32 -08002738 } else if (var->mode == ir_var_shader_in) {
Chad Versace01a584d2011-01-20 14:12:16 -08002739 var->read_only = true;
2740
Ian Romanickfb9f5b02010-03-29 17:16:35 -07002741 if (state->target == vertex_shader) {
2742 bool error_emitted = false;
2743
2744 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
2745 *
2746 * "Vertex shader inputs can only be float, floating-point
2747 * vectors, matrices, signed and unsigned integers and integer
2748 * vectors. Vertex shader inputs can also form arrays of these
2749 * types, but not structures."
2750 *
Ian Romanick2d816202010-03-29 17:40:11 -07002751 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
2752 *
2753 * "Vertex shader inputs can only be float, floating-point
2754 * vectors, matrices, signed and unsigned integers and integer
2755 * vectors. They cannot be arrays or structures."
2756 *
Ian Romanickfb9f5b02010-03-29 17:16:35 -07002757 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
2758 *
2759 * "The attribute qualifier can be used only with float,
2760 * floating-point vectors, and matrices. Attribute variables
2761 * cannot be declared as arrays or structures."
Paul Berryd4a24742012-08-02 08:18:12 -07002762 *
2763 * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
2764 *
2765 * "Vertex shader inputs can only be float, floating-point
2766 * vectors, matrices, signed and unsigned integers and integer
2767 * vectors. Vertex shader inputs cannot be arrays or
2768 * structures."
Ian Romanickfb9f5b02010-03-29 17:16:35 -07002769 */
2770 const glsl_type *check_type = var->type->is_array()
2771 ? var->type->fields.array : var->type;
2772
2773 switch (check_type->base_type) {
2774 case GLSL_TYPE_FLOAT:
2775 break;
2776 case GLSL_TYPE_UINT:
2777 case GLSL_TYPE_INT:
Paul Berryd4a24742012-08-02 08:18:12 -07002778 if (state->is_version(120, 300))
Ian Romanickfb9f5b02010-03-29 17:16:35 -07002779 break;
2780 /* FALLTHROUGH */
2781 default:
2782 _mesa_glsl_error(& loc, state,
2783 "vertex shader input / attribute cannot have "
2784 "type %s`%s'",
2785 var->type->is_array() ? "array of " : "",
2786 check_type->name);
2787 error_emitted = true;
2788 }
2789
Paul Berry0d9bba62012-08-05 09:57:01 -07002790 if (!error_emitted && var->type->is_array() &&
Paul Berryb2265db2013-07-11 15:40:11 -07002791 !state->check_version(150, 0, &loc,
Paul Berry0d9bba62012-08-05 09:57:01 -07002792 "vertex shader input / attribute "
2793 "cannot have array type")) {
Ian Romanickfb9f5b02010-03-29 17:16:35 -07002794 error_emitted = true;
2795 }
2796 }
2797 }
2798
Paul Berrydfb57e72013-02-08 16:46:20 -08002799 /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
2800 * so must integer vertex outputs.
Chad Versace68d06b12010-12-16 11:06:19 -08002801 *
Paul Berrydfb57e72013-02-08 16:46:20 -08002802 * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
2803 * "Fragment shader inputs that are signed or unsigned integers or
2804 * integer vectors must be qualified with the interpolation qualifier
Chad Versace68d06b12010-12-16 11:06:19 -08002805 * flat."
Paul Berryd4a24742012-08-02 08:18:12 -07002806 *
Paul Berrydfb57e72013-02-08 16:46:20 -08002807 * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
Paul Berry93c91342013-02-06 16:09:39 -08002808 * "Fragment shader inputs that are, or contain, signed or unsigned
2809 * integers or integer vectors must be qualified with the
2810 * interpolation qualifier flat."
Paul Berryd4a24742012-08-02 08:18:12 -07002811 *
Paul Berrydfb57e72013-02-08 16:46:20 -08002812 * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
2813 * "Vertex shader outputs that are, or contain, signed or unsigned
2814 * integers or integer vectors must be qualified with the
2815 * interpolation qualifier flat."
2816 *
2817 * Note that prior to GLSL 1.50, this requirement applied to vertex
2818 * outputs rather than fragment inputs. That creates problems in the
2819 * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
2820 * desktop GL shaders. For GLSL ES shaders, we follow the spec and
2821 * apply the restriction to both vertex outputs and fragment inputs.
2822 *
2823 * Note also that the desktop GLSL specs are missing the text "or
2824 * contain"; this is presumably an oversight, since there is no
2825 * reasonable way to interpolate a fragment shader input that contains
2826 * an integer.
Chad Versace68d06b12010-12-16 11:06:19 -08002827 */
Paul Berrydfb57e72013-02-08 16:46:20 -08002828 if (state->is_version(130, 300) &&
2829 var->type->contains_integer() &&
2830 var->interpolation != INTERP_QUALIFIER_FLAT &&
2831 ((state->target == fragment_shader && var->mode == ir_var_shader_in)
2832 || (state->target == vertex_shader && var->mode == ir_var_shader_out
2833 && state->es_shader))) {
2834 const char *var_type = (state->target == vertex_shader) ?
2835 "vertex output" : "fragment input";
2836 _mesa_glsl_error(&loc, state, "If a %s is (or contains) "
2837 "an integer, then it must be qualified with 'flat'",
2838 var_type);
Chad Versace68d06b12010-12-16 11:06:19 -08002839 }
2840
2841
Chad Versace605aacc2011-01-11 17:21:18 -08002842 /* Interpolation qualifiers cannot be applied to 'centroid' and
2843 * 'centroid varying'.
2844 *
2845 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
2846 * "interpolation qualifiers may only precede the qualifiers in,
2847 * centroid in, out, or centroid out in a declaration. They do not apply
2848 * to the deprecated storage qualifiers varying or centroid varying."
Paul Berryd4a24742012-08-02 08:18:12 -07002849 *
2850 * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
Chad Versace605aacc2011-01-11 17:21:18 -08002851 */
Paul Berrye3ded7f2012-08-01 14:50:05 -07002852 if (state->is_version(130, 0)
Chad Versace605aacc2011-01-11 17:21:18 -08002853 && this->type->qualifier.has_interpolation()
2854 && this->type->qualifier.flags.q.varying) {
2855
2856 const char *i = this->type->qualifier.interpolation_string();
2857 assert(i != NULL);
2858 const char *s;
2859 if (this->type->qualifier.flags.q.centroid)
2860 s = "centroid varying";
2861 else
2862 s = "varying";
2863
2864 _mesa_glsl_error(&loc, state,
2865 "qualifier '%s' cannot be applied to the "
2866 "deprecated storage qualifier '%s'", i, s);
2867 }
2868
2869
Chad Versace8faaa4a2011-01-11 18:13:26 -08002870 /* Interpolation qualifiers can only apply to vertex shader outputs and
2871 * fragment shader inputs.
2872 *
2873 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
2874 * "Outputs from a vertex shader (out) and inputs to a fragment
2875 * shader (in) can be further qualified with one or more of these
2876 * interpolation qualifiers"
Paul Berryd4a24742012-08-02 08:18:12 -07002877 *
2878 * From page 31 (page 37 of the PDF) of the GLSL ES 3.00 spec:
2879 * "These interpolation qualifiers may only precede the qualifiers
2880 * in, centroid in, out, or centroid out in a declaration. They do
2881 * not apply to inputs into a vertex shader or outputs from a
2882 * fragment shader."
Chad Versace8faaa4a2011-01-11 18:13:26 -08002883 */
Paul Berryd4a24742012-08-02 08:18:12 -07002884 if (state->is_version(130, 300)
Chad Versace8faaa4a2011-01-11 18:13:26 -08002885 && this->type->qualifier.has_interpolation()) {
2886
2887 const char *i = this->type->qualifier.interpolation_string();
2888 assert(i != NULL);
2889
2890 switch (state->target) {
2891 case vertex_shader:
2892 if (this->type->qualifier.flags.q.in) {
2893 _mesa_glsl_error(&loc, state,
2894 "qualifier '%s' cannot be applied to vertex "
2895 "shader inputs", i);
2896 }
2897 break;
2898 case fragment_shader:
2899 if (this->type->qualifier.flags.q.out) {
2900 _mesa_glsl_error(&loc, state,
2901 "qualifier '%s' cannot be applied to fragment "
2902 "shader outputs", i);
2903 }
2904 break;
2905 default:
2906 assert(0);
2907 }
2908 }
2909
2910
Chad Versace1eb0f172011-01-11 18:24:17 -08002911 /* From section 4.3.4 of the GLSL 1.30 spec:
2912 * "It is an error to use centroid in in a vertex shader."
Paul Berryd4a24742012-08-02 08:18:12 -07002913 *
2914 * From section 4.3.4 of the GLSL ES 3.00 spec:
2915 * "It is an error to use centroid in or interpolation qualifiers in
2916 * a vertex shader input."
Chad Versace1eb0f172011-01-11 18:24:17 -08002917 */
Paul Berryd4a24742012-08-02 08:18:12 -07002918 if (state->is_version(130, 300)
Chad Versace1eb0f172011-01-11 18:24:17 -08002919 && this->type->qualifier.flags.q.centroid
2920 && this->type->qualifier.flags.q.in
2921 && state->target == vertex_shader) {
2922
2923 _mesa_glsl_error(&loc, state,
2924 "'centroid in' cannot be used in a vertex shader");
2925 }
2926
2927
Chad Versace889e1a52011-01-16 22:38:45 -08002928 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
2929 */
Kenneth Graunke6eec5022013-07-15 15:58:29 -07002930 if (this->type->qualifier.precision != ast_precision_none) {
Paul Berry0d9bba62012-08-05 09:57:01 -07002931 state->check_precision_qualifiers_allowed(&loc);
Chad Versace889e1a52011-01-16 22:38:45 -08002932 }
2933
2934
Chad Versace45e8e6c2011-01-17 15:28:39 -08002935 /* Precision qualifiers only apply to floating point and integer types.
Chad Versace889e1a52011-01-16 22:38:45 -08002936 *
2937 * From section 4.5.2 of the GLSL 1.30 spec:
2938 * "Any floating point or any integer declaration can have the type
2939 * preceded by one of these precision qualifiers [...] Literal
2940 * constants do not have precision qualifiers. Neither do Boolean
2941 * variables.
Kenneth Graunke87528242011-03-26 23:37:09 -07002942 *
2943 * In GLSL ES, sampler types are also allowed.
2944 *
2945 * From page 87 of the GLSL ES spec:
2946 * "RESOLUTION: Allow sampler types to take a precision qualifier."
Chad Versace889e1a52011-01-16 22:38:45 -08002947 */
Kenneth Graunke6eec5022013-07-15 15:58:29 -07002948 if (this->type->qualifier.precision != ast_precision_none
Chad Versace45e8e6c2011-01-17 15:28:39 -08002949 && !var->type->is_float()
2950 && !var->type->is_integer()
Kenneth Graunke6eec5022013-07-15 15:58:29 -07002951 && !var->type->is_record()
Kenneth Graunke87528242011-03-26 23:37:09 -07002952 && !(var->type->is_sampler() && state->es_shader)
Chad Versace45e8e6c2011-01-17 15:28:39 -08002953 && !(var->type->is_array()
2954 && (var->type->fields.array->is_float()
2955 || var->type->fields.array->is_integer()))) {
Chad Versace889e1a52011-01-16 22:38:45 -08002956
2957 _mesa_glsl_error(&loc, state,
Kenneth Graunke87528242011-03-26 23:37:09 -07002958 "precision qualifiers apply only to floating point"
2959 "%s types", state->es_shader ? ", integer, and sampler"
2960 : "and integer");
Chad Versace889e1a52011-01-16 22:38:45 -08002961 }
2962
Paul Berryf0722102011-07-12 12:03:02 -07002963 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
2964 *
2965 * "[Sampler types] can only be declared as function
2966 * parameters or uniform variables (see Section 4.3.5
2967 * "Uniform")".
2968 */
2969 if (var_type->contains_sampler() &&
2970 !this->type->qualifier.flags.q.uniform) {
2971 _mesa_glsl_error(&loc, state, "samplers must be declared uniform");
2972 }
2973
Ian Romanicke78e0fa2010-07-07 12:13:34 -07002974 /* Process the initializer and add its instructions to a temporary
2975 * list. This list will be added to the instruction stream (below) after
2976 * the declaration is added. This is done because in some cases (such as
2977 * redeclarations) the declaration may not actually be added to the
2978 * instruction stream.
2979 */
Eric Anholtfa33d0b2010-07-29 13:50:17 -07002980 exec_list initializer_instructions;
Ian Romanick09a4ba02011-03-04 16:15:20 -08002981 ir_variable *earlier = get_variable_being_redeclared(var, decl, state);
2982
Ian Romanick66faec42010-03-27 18:56:53 -07002983 if (decl->initializer != NULL) {
Ian Romanick09a4ba02011-03-04 16:15:20 -08002984 result = process_initializer((earlier == NULL) ? var : earlier,
2985 decl, this->type,
Ian Romanick0292ffb2011-03-04 15:29:33 -08002986 &initializer_instructions, state);
Ian Romanick19360152010-03-26 18:05:27 -07002987 }
Ian Romanick17d86f42010-03-29 12:59:02 -07002988
Eric Anholt0ed61252010-03-31 09:29:33 -10002989 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
2990 *
2991 * "It is an error to write to a const variable outside of
2992 * its declaration, so they must be initialized when
2993 * declared."
2994 */
Ian Romanicke24d35a2010-10-05 16:38:47 -07002995 if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) {
Eric Anholt0ed61252010-03-31 09:29:33 -10002996 _mesa_glsl_error(& loc, state,
Chad Versace46f71052011-01-18 15:15:19 -08002997 "const declaration of `%s' must be initialized",
2998 decl->identifier);
Eric Anholt0ed61252010-03-31 09:29:33 -10002999 }
3000
Ian Romanick09a4ba02011-03-04 16:15:20 -08003001 /* If the declaration is not a redeclaration, there are a few additional
3002 * semantic checks that must be applied. In addition, variable that was
3003 * created for the declaration should be added to the IR stream.
3004 */
3005 if (earlier == NULL) {
3006 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
3007 *
3008 * "Identifiers starting with "gl_" are reserved for use by
3009 * OpenGL, and may not be declared in a shader as either a
3010 * variable or a function."
3011 */
3012 if (strncmp(decl->identifier, "gl_", 3) == 0)
3013 _mesa_glsl_error(& loc, state,
3014 "identifier `%s' uses reserved `gl_' prefix",
3015 decl->identifier);
Jason Woodc475a542011-10-06 22:37:48 -07003016 else if (strstr(decl->identifier, "__")) {
Eric Anholt684b7012011-10-03 16:27:59 -07003017 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
3018 * spec:
3019 *
3020 * "In addition, all identifiers containing two
3021 * consecutive underscores (__) are reserved as
3022 * possible future keywords."
3023 */
3024 _mesa_glsl_error(& loc, state,
3025 "identifier `%s' uses reserved `__' string",
3026 decl->identifier);
3027 }
Ian Romanick09a4ba02011-03-04 16:15:20 -08003028
3029 /* Add the variable to the symbol table. Note that the initializer's
3030 * IR was already processed earlier (though it hasn't been emitted
3031 * yet), without the variable in scope.
3032 *
3033 * This differs from most C-like languages, but it follows the GLSL
3034 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
3035 * spec:
3036 *
3037 * "Within a declaration, the scope of a name starts immediately
3038 * after the initializer if present or immediately after the name
3039 * being declared if not."
3040 */
3041 if (!state->symbols->add_variable(var)) {
3042 YYLTYPE loc = this->get_location();
3043 _mesa_glsl_error(&loc, state, "name `%s' already taken in the "
3044 "current scope", decl->identifier);
3045 continue;
3046 }
3047
3048 /* Push the variable declaration to the top. It means that all the
3049 * variable declarations will appear in a funny last-to-first order,
3050 * but otherwise we run into trouble if a function is prototyped, a
3051 * global var is decled, then the function is defined with usage of
3052 * the global var. See glslparsertest's CorrectModule.frag.
3053 */
3054 instructions->push_head(var);
Ian Romanick5466b632010-07-01 12:46:55 -07003055 }
3056
Eric Anholtfa33d0b2010-07-29 13:50:17 -07003057 instructions->append_list(&initializer_instructions);
Ian Romanicka87ac252010-02-22 13:19:34 -08003058 }
3059
Eric Anholt85584592010-04-14 15:38:52 -07003060
3061 /* Generally, variable declarations do not have r-values. However,
3062 * one is used for the declaration in
3063 *
3064 * while (bool b = some_condition()) {
3065 * ...
3066 * }
3067 *
3068 * so we return the rvalue from the last seen declaration here.
Ian Romanicka87ac252010-02-22 13:19:34 -08003069 */
Eric Anholt85584592010-04-14 15:38:52 -07003070 return result;
Ian Romanicka87ac252010-02-22 13:19:34 -08003071}
3072
3073
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07003074ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08003075ast_parameter_declarator::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08003076 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08003077{
Kenneth Graunke953ff122010-06-25 13:14:37 -07003078 void *ctx = state;
Ian Romanicka87ac252010-02-22 13:19:34 -08003079 const struct glsl_type *type;
3080 const char *name = NULL;
Eric Anholt2e063f12010-03-28 00:56:22 -07003081 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08003082
Ian Romanickd612a122010-03-31 16:22:06 -07003083 type = this->type->specifier->glsl_type(& name, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08003084
3085 if (type == NULL) {
Ian Romanicka87ac252010-02-22 13:19:34 -08003086 if (name != NULL) {
3087 _mesa_glsl_error(& loc, state,
3088 "invalid type `%s' in declaration of `%s'",
Ian Romanick18238de2010-03-01 13:49:10 -08003089 name, this->identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08003090 } else {
3091 _mesa_glsl_error(& loc, state,
3092 "invalid type in declaration of `%s'",
Ian Romanick18238de2010-03-01 13:49:10 -08003093 this->identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08003094 }
3095
Ian Romanick0471e8b2010-03-26 14:33:41 -07003096 type = glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -08003097 }
3098
Eric Anholt068c80c2010-03-31 09:56:36 -10003099 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
3100 *
3101 * "Functions that accept no input arguments need not use void in the
3102 * argument list because prototypes (or definitions) are required and
3103 * therefore there is no ambiguity when an empty argument list "( )" is
3104 * declared. The idiom "(void)" as a parameter list is provided for
3105 * convenience."
3106 *
3107 * Placing this check here prevents a void parameter being set up
3108 * for a function, which avoids tripping up checks for main taking
3109 * parameters and lookups of an unnamed symbol.
3110 */
Ian Romanickcf37c9e2010-04-02 15:30:45 -07003111 if (type->is_void()) {
3112 if (this->identifier != NULL)
3113 _mesa_glsl_error(& loc, state,
3114 "named parameter cannot have type `void'");
3115
3116 is_void = true;
Eric Anholt068c80c2010-03-31 09:56:36 -10003117 return NULL;
Ian Romanickcf37c9e2010-04-02 15:30:45 -07003118 }
Eric Anholt068c80c2010-03-31 09:56:36 -10003119
Ian Romanick45d8a702010-04-02 15:09:33 -07003120 if (formal_parameter && (this->identifier == NULL)) {
3121 _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
3122 return NULL;
3123 }
3124
Kenneth Graunkee511a352010-08-21 15:30:34 -07003125 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
3126 * call already handled the "vec4[..] foo" case.
3127 */
3128 if (this->is_array) {
Kenneth Graunked8e34e22010-08-07 02:56:01 -07003129 type = process_array_type(&loc, type, this->array_size, state);
Kenneth Graunkee511a352010-08-21 15:30:34 -07003130 }
3131
Ian Romanicka04211e2011-10-24 11:45:50 -07003132 if (!type->is_error() && type->array_size() == 0) {
Kenneth Graunkee511a352010-08-21 15:30:34 -07003133 _mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
3134 "a declared size.");
3135 type = glsl_type::error_type;
3136 }
3137
Ian Romanickcf37c9e2010-04-02 15:30:45 -07003138 is_void = false;
Paul Berry42a29d82013-01-11 14:39:32 -08003139 ir_variable *var = new(ctx)
3140 ir_variable(type, this->identifier, ir_var_function_in);
Ian Romanicka87ac252010-02-22 13:19:34 -08003141
Ian Romanickcdb8d542010-03-11 14:48:51 -08003142 /* Apply any specified qualifiers to the parameter declaration. Note that
3143 * for function parameters the default mode is 'in'.
3144 */
Eric Anholtf7561e82012-04-26 18:19:39 -07003145 apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc,
Paul Berryc33be482012-12-18 14:49:34 -08003146 false, true);
Ian Romanicka87ac252010-02-22 13:19:34 -08003147
Paul Berryf0722102011-07-12 12:03:02 -07003148 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3149 *
3150 * "Samplers cannot be treated as l-values; hence cannot be used
3151 * as out or inout function parameters, nor can they be assigned
3152 * into."
3153 */
Paul Berry42a29d82013-01-11 14:39:32 -08003154 if ((var->mode == ir_var_function_inout || var->mode == ir_var_function_out)
Paul Berryf0722102011-07-12 12:03:02 -07003155 && type->contains_sampler()) {
3156 _mesa_glsl_error(&loc, state, "out and inout parameters cannot contain samplers");
3157 type = glsl_type::error_type;
3158 }
3159
Paul Berry00792e32011-09-10 07:48:46 -07003160 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
3161 *
3162 * "When calling a function, expressions that do not evaluate to
3163 * l-values cannot be passed to parameters declared as out or inout."
3164 *
3165 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
3166 *
3167 * "Other binary or unary expressions, non-dereferenced arrays,
3168 * function names, swizzles with repeated fields, and constants
3169 * cannot be l-values."
3170 *
3171 * So for GLSL 1.10, passing an array as an out or inout parameter is not
3172 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
3173 */
Paul Berry42a29d82013-01-11 14:39:32 -08003174 if ((var->mode == ir_var_function_inout || var->mode == ir_var_function_out)
Paul Berry0d9bba62012-08-05 09:57:01 -07003175 && type->is_array()
3176 && !state->check_version(120, 100, &loc,
3177 "Arrays cannot be out or inout parameters")) {
Paul Berry00792e32011-09-10 07:48:46 -07003178 type = glsl_type::error_type;
3179 }
3180
Ian Romanick0044e7e2010-03-08 23:44:00 -08003181 instructions->push_tail(var);
Ian Romanicka87ac252010-02-22 13:19:34 -08003182
3183 /* Parameter declarations do not have r-values.
3184 */
3185 return NULL;
3186}
3187
3188
Ian Romanick45d8a702010-04-02 15:09:33 -07003189void
Ian Romanick304ea902010-05-10 11:17:53 -07003190ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
Ian Romanick45d8a702010-04-02 15:09:33 -07003191 bool formal,
3192 exec_list *ir_parameters,
3193 _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08003194{
Ian Romanickcf37c9e2010-04-02 15:30:45 -07003195 ast_parameter_declarator *void_param = NULL;
3196 unsigned count = 0;
Ian Romanicka87ac252010-02-22 13:19:34 -08003197
Ian Romanick2b97dc62010-05-10 17:42:05 -07003198 foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
Ian Romanick45d8a702010-04-02 15:09:33 -07003199 param->formal_parameter = formal;
Eric Anholt068c80c2010-03-31 09:56:36 -10003200 param->hir(ir_parameters, state);
Ian Romanickcf37c9e2010-04-02 15:30:45 -07003201
3202 if (param->is_void)
3203 void_param = param;
3204
3205 count++;
3206 }
3207
3208 if ((void_param != NULL) && (count > 1)) {
3209 YYLTYPE loc = void_param->get_location();
3210
3211 _mesa_glsl_error(& loc, state,
3212 "`void' parameter must be only parameter");
Ian Romanicka87ac252010-02-22 13:19:34 -08003213 }
3214}
3215
3216
Kenneth Graunke6fae1e42010-12-06 10:54:05 -08003217void
Paul Berry0d81b0e2011-07-29 15:28:52 -07003218emit_function(_mesa_glsl_parse_state *state, ir_function *f)
Kenneth Graunke6fae1e42010-12-06 10:54:05 -08003219{
Paul Berry0d81b0e2011-07-29 15:28:52 -07003220 /* IR invariants disallow function declarations or definitions
3221 * nested within other function definitions. But there is no
3222 * requirement about the relative order of function declarations
3223 * and definitions with respect to one another. So simply insert
3224 * the new ir_function block at the end of the toplevel instruction
3225 * list.
3226 */
3227 state->toplevel_ir->push_tail(f);
Kenneth Graunke6fae1e42010-12-06 10:54:05 -08003228}
3229
3230
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07003231ir_rvalue *
Ian Romanick92318a92010-03-31 18:23:21 -07003232ast_function::hir(exec_list *instructions,
3233 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08003234{
Kenneth Graunke953ff122010-06-25 13:14:37 -07003235 void *ctx = state;
Ian Romanick18238de2010-03-01 13:49:10 -08003236 ir_function *f = NULL;
Ian Romanick92318a92010-03-31 18:23:21 -07003237 ir_function_signature *sig = NULL;
3238 exec_list hir_parameters;
Ian Romanicka87ac252010-02-22 13:19:34 -08003239
Kenneth Graunkeac04c252010-06-29 00:48:10 -07003240 const char *const name = identifier;
Ian Romanicka87ac252010-02-22 13:19:34 -08003241
Ian Romanick9a3bd5e2011-08-29 14:56:29 -07003242 /* New functions are always added to the top-level IR instruction stream,
3243 * so this instruction list pointer is ignored. See also emit_function
3244 * (called below).
3245 */
3246 (void) instructions;
3247
Ian Romanick63b80f82010-09-01 06:34:58 -07003248 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
3249 *
3250 * "Function declarations (prototypes) cannot occur inside of functions;
3251 * they must be at global scope, or for the built-in functions, outside
3252 * the global scope."
3253 *
3254 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
3255 *
3256 * "User defined functions may only be defined within the global scope."
3257 *
3258 * Note that this language does not appear in GLSL 1.10.
3259 */
Paul Berrye3ded7f2012-08-01 14:50:05 -07003260 if ((state->current_function != NULL) &&
3261 state->is_version(120, 100)) {
Ian Romanick63b80f82010-09-01 06:34:58 -07003262 YYLTYPE loc = this->get_location();
3263 _mesa_glsl_error(&loc, state,
3264 "declaration of function `%s' not allowed within "
3265 "function body", name);
3266 }
3267
Kenneth Graunkeedd180f2010-08-20 02:14:35 -07003268 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
3269 *
3270 * "Identifiers starting with "gl_" are reserved for use by
3271 * OpenGL, and may not be declared in a shader as either a
3272 * variable or a function."
3273 */
3274 if (strncmp(name, "gl_", 3) == 0) {
3275 YYLTYPE loc = this->get_location();
3276 _mesa_glsl_error(&loc, state,
3277 "identifier `%s' uses reserved `gl_' prefix", name);
3278 }
3279
Ian Romanicka87ac252010-02-22 13:19:34 -08003280 /* Convert the list of function parameters to HIR now so that they can be
3281 * used below to compare this function's signature with previously seen
3282 * signatures for functions with the same name.
3283 */
Ian Romanick45d8a702010-04-02 15:09:33 -07003284 ast_parameter_declarator::parameters_to_hir(& this->parameters,
3285 is_definition,
3286 & hir_parameters, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08003287
Ian Romanicke39cc692010-03-23 12:19:13 -07003288 const char *return_type_name;
3289 const glsl_type *return_type =
Ian Romanick92318a92010-03-31 18:23:21 -07003290 this->return_type->specifier->glsl_type(& return_type_name, state);
Ian Romanicke39cc692010-03-23 12:19:13 -07003291
Eric Anholt76e96d72010-08-23 13:26:52 -07003292 if (!return_type) {
3293 YYLTYPE loc = this->get_location();
3294 _mesa_glsl_error(&loc, state,
3295 "function `%s' has undeclared return type `%s'",
3296 name, return_type_name);
3297 return_type = glsl_type::error_type;
3298 }
Ian Romanicke39cc692010-03-23 12:19:13 -07003299
Kenneth Graunkeac04c252010-06-29 00:48:10 -07003300 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
3301 * "No qualifier is allowed on the return type of a function."
3302 */
3303 if (this->return_type->has_qualifiers()) {
3304 YYLTYPE loc = this->get_location();
3305 _mesa_glsl_error(& loc, state,
3306 "function `%s' return type has qualifiers", name);
3307 }
3308
Paul Berryf0722102011-07-12 12:03:02 -07003309 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3310 *
3311 * "[Sampler types] can only be declared as function parameters
3312 * or uniform variables (see Section 4.3.5 "Uniform")".
3313 */
3314 if (return_type->contains_sampler()) {
3315 YYLTYPE loc = this->get_location();
3316 _mesa_glsl_error(&loc, state,
3317 "function `%s' return type can't contain a sampler",
3318 name);
3319 }
3320
Ian Romanicka87ac252010-02-22 13:19:34 -08003321 /* Verify that this function's signature either doesn't match a previously
3322 * seen signature for a function with the same name, or, if a match is found,
3323 * that the previously seen signature does not have an associated definition.
3324 */
Ian Romanicke466b182010-09-01 14:16:53 -07003325 f = state->symbols->get_function(name);
Kenneth Graunke81f03392010-09-16 02:52:25 -07003326 if (f != NULL && (state->es_shader || f->has_user_signature())) {
Ian Romanick202604e2010-08-11 16:58:25 -07003327 sig = f->exact_matching_signature(&hir_parameters);
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07003328 if (sig != NULL) {
3329 const char *badvar = sig->qualifiers_match(&hir_parameters);
3330 if (badvar != NULL) {
3331 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08003332
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07003333 _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
3334 "qualifiers don't match prototype", name, badvar);
Ian Romanicka87ac252010-02-22 13:19:34 -08003335 }
3336
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07003337 if (sig->return_type != return_type) {
3338 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08003339
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07003340 _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
3341 "match prototype", name);
3342 }
3343
Kenneth Graunke6c5cf8b2013-04-30 00:58:09 -07003344 if (sig->is_defined) {
3345 if (is_definition) {
3346 YYLTYPE loc = this->get_location();
3347 _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
3348 } else {
3349 /* We just encountered a prototype that exactly matches a
3350 * function that's already been defined. This is redundant,
3351 * and we should ignore it.
3352 */
3353 return NULL;
3354 }
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07003355 }
3356 }
Ian Romanicka87ac252010-02-22 13:19:34 -08003357 } else {
Carl Worth1660a292010-06-23 18:11:51 -07003358 f = new(ctx) ir_function(name);
Eric Anholte8f5ebf2010-11-05 06:08:45 -07003359 if (!state->symbols->add_function(f)) {
Kenneth Graunkee0959132010-08-25 16:37:46 -07003360 /* This function name shadows a non-function use of the same name. */
3361 YYLTYPE loc = this->get_location();
3362
3363 _mesa_glsl_error(&loc, state, "function name `%s' conflicts with "
3364 "non-function", name);
3365 return NULL;
3366 }
Kenneth Graunke9fa99f32010-04-21 12:30:22 -07003367
Paul Berry0d81b0e2011-07-29 15:28:52 -07003368 emit_function(state, f);
Ian Romanicka87ac252010-02-22 13:19:34 -08003369 }
3370
Eric Anholtab372da2010-03-28 01:24:55 -07003371 /* Verify the return type of main() */
3372 if (strcmp(name, "main") == 0) {
Ian Romanick25711a82010-03-31 17:39:10 -07003373 if (! return_type->is_void()) {
Eric Anholtab372da2010-03-28 01:24:55 -07003374 YYLTYPE loc = this->get_location();
3375
3376 _mesa_glsl_error(& loc, state, "main() must return void");
3377 }
Eric Anholt174cc032010-03-30 23:37:51 -10003378
Ian Romanick92318a92010-03-31 18:23:21 -07003379 if (!hir_parameters.is_empty()) {
Eric Anholt174cc032010-03-30 23:37:51 -10003380 YYLTYPE loc = this->get_location();
3381
3382 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
3383 }
Eric Anholtab372da2010-03-28 01:24:55 -07003384 }
Ian Romanicka87ac252010-02-22 13:19:34 -08003385
3386 /* Finish storing the information about this new function in its signature.
3387 */
Ian Romanick92318a92010-03-31 18:23:21 -07003388 if (sig == NULL) {
Carl Worth1660a292010-06-23 18:11:51 -07003389 sig = new(ctx) ir_function_signature(return_type);
Ian Romanick92318a92010-03-31 18:23:21 -07003390 f->add_signature(sig);
Ian Romanicka87ac252010-02-22 13:19:34 -08003391 }
3392
Kenneth Graunkebff60132010-04-28 12:44:24 -07003393 sig->replace_parameters(&hir_parameters);
Ian Romanick92318a92010-03-31 18:23:21 -07003394 signature = sig;
Ian Romanicke29a5852010-03-31 17:54:26 -07003395
Ian Romanick92318a92010-03-31 18:23:21 -07003396 /* Function declarations (prototypes) do not have r-values.
3397 */
3398 return NULL;
3399}
3400
3401
3402ir_rvalue *
3403ast_function_definition::hir(exec_list *instructions,
3404 struct _mesa_glsl_parse_state *state)
3405{
3406 prototype->is_definition = true;
3407 prototype->hir(instructions, state);
3408
3409 ir_function_signature *signature = prototype->signature;
Kenneth Graunke826a39c2010-08-20 02:04:52 -07003410 if (signature == NULL)
3411 return NULL;
Ian Romanicka87ac252010-02-22 13:19:34 -08003412
Ian Romanick41ec6a42010-03-19 17:08:05 -07003413 assert(state->current_function == NULL);
3414 state->current_function = signature;
Kenneth Graunke6de82562010-06-29 09:59:40 -07003415 state->found_return = false;
Ian Romanick41ec6a42010-03-19 17:08:05 -07003416
Ian Romanicke29a5852010-03-31 17:54:26 -07003417 /* Duplicate parameters declared in the prototype as concrete variables.
3418 * Add these to the symbol table.
Ian Romanicka87ac252010-02-22 13:19:34 -08003419 */
Ian Romanick8bde4ce2010-03-19 11:57:24 -07003420 state->symbols->push_scope();
Ian Romanicke29a5852010-03-31 17:54:26 -07003421 foreach_iter(exec_list_iterator, iter, signature->parameters) {
Eric Anholtfbc7c0b2010-04-07 14:32:53 -07003422 ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
Ian Romanicka87ac252010-02-22 13:19:34 -08003423
Eric Anholtfbc7c0b2010-04-07 14:32:53 -07003424 assert(var != NULL);
Ian Romanicka87ac252010-02-22 13:19:34 -08003425
Ian Romanick3359e582010-03-19 15:38:52 -07003426 /* The only way a parameter would "exist" is if two parameters have
3427 * the same name.
3428 */
3429 if (state->symbols->name_declared_this_scope(var->name)) {
3430 YYLTYPE loc = this->get_location();
3431
3432 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
3433 } else {
Eric Anholt001eee52010-11-05 06:11:24 -07003434 state->symbols->add_variable(var);
Ian Romanick3359e582010-03-19 15:38:52 -07003435 }
Ian Romanicka87ac252010-02-22 13:19:34 -08003436 }
3437
Kenneth Graunke9fa99f32010-04-21 12:30:22 -07003438 /* Convert the body of the function to HIR. */
Eric Anholt894ea972010-04-07 13:19:11 -07003439 this->body->hir(&signature->body, state);
Kenneth Graunke9fa99f32010-04-21 12:30:22 -07003440 signature->is_defined = true;
Ian Romanicka87ac252010-02-22 13:19:34 -08003441
Ian Romanick8bde4ce2010-03-19 11:57:24 -07003442 state->symbols->pop_scope();
Ian Romanicka87ac252010-02-22 13:19:34 -08003443
Ian Romanick41ec6a42010-03-19 17:08:05 -07003444 assert(state->current_function == signature);
3445 state->current_function = NULL;
Ian Romanicka87ac252010-02-22 13:19:34 -08003446
Kenneth Graunke6de82562010-06-29 09:59:40 -07003447 if (!signature->return_type->is_void() && !state->found_return) {
3448 YYLTYPE loc = this->get_location();
3449 _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
3450 "%s, but no return statement",
3451 signature->function_name(),
3452 signature->return_type->name);
3453 }
3454
Ian Romanicka87ac252010-02-22 13:19:34 -08003455 /* Function definitions do not have r-values.
3456 */
3457 return NULL;
3458}
Ian Romanick16a246c2010-03-19 16:45:19 -07003459
3460
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07003461ir_rvalue *
Ian Romanick16a246c2010-03-19 16:45:19 -07003462ast_jump_statement::hir(exec_list *instructions,
3463 struct _mesa_glsl_parse_state *state)
3464{
Kenneth Graunke953ff122010-06-25 13:14:37 -07003465 void *ctx = state;
Ian Romanick16a246c2010-03-19 16:45:19 -07003466
Ian Romanickc0e76d82010-04-05 16:53:19 -07003467 switch (mode) {
3468 case ast_return: {
Ian Romanick16a246c2010-03-19 16:45:19 -07003469 ir_return *inst;
Eric Anholtaad7c772010-03-30 23:28:20 -10003470 assert(state->current_function);
Ian Romanick16a246c2010-03-19 16:45:19 -07003471
3472 if (opt_return_value) {
Matt Turner1a1b03e2013-05-22 12:14:32 -07003473 ir_rvalue *ret = opt_return_value->hir(instructions, state);
Ian Romanick2db46fe2011-01-22 17:47:05 -08003474
3475 /* The value of the return type can be NULL if the shader says
3476 * 'return foo();' and foo() is a function that returns void.
3477 *
3478 * NOTE: The GLSL spec doesn't say that this is an error. The type
3479 * of the return value is void. If the return type of the function is
3480 * also void, then this should compile without error. Seriously.
3481 */
3482 const glsl_type *const ret_type =
3483 (ret == NULL) ? glsl_type::void_type : ret->type;
Ian Romanick16a246c2010-03-19 16:45:19 -07003484
Matt Turner1a1b03e2013-05-22 12:14:32 -07003485 /* Implicit conversions are not allowed for return values prior to
3486 * ARB_shading_language_420pack.
3487 */
3488 if (state->current_function->return_type != ret_type) {
Kenneth Graunke18707eb2010-06-28 23:38:04 -07003489 YYLTYPE loc = this->get_location();
3490
Matt Turner1a1b03e2013-05-22 12:14:32 -07003491 if (state->ARB_shading_language_420pack_enable) {
3492 if (!apply_implicit_conversion(state->current_function->return_type,
3493 ret, state)) {
3494 _mesa_glsl_error(& loc, state,
3495 "Could not implicitly convert return value "
3496 "to %s, in function `%s'",
3497 state->current_function->return_type->name,
3498 state->current_function->function_name());
3499 }
3500 } else {
3501 _mesa_glsl_error(& loc, state,
3502 "`return' with wrong type %s, in function `%s' "
3503 "returning %s",
3504 ret_type->name,
3505 state->current_function->function_name(),
3506 state->current_function->return_type->name);
3507 }
Matt Turnerfcaa48d2013-05-22 14:57:04 -07003508 } else if (state->current_function->return_type->base_type ==
3509 GLSL_TYPE_VOID) {
3510 YYLTYPE loc = this->get_location();
3511
3512 /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
3513 * specs add a clarification:
3514 *
3515 * "A void function can only use return without a return argument, even if
3516 * the return argument has void type. Return statements only accept values:
3517 *
3518 * void func1() { }
3519 * void func2() { return func1(); } // illegal return statement"
3520 */
3521 _mesa_glsl_error(& loc, state,
3522 "void functions can only use `return' without a "
3523 "return argument");
3524 }
Ian Romanick16a246c2010-03-19 16:45:19 -07003525
Carl Worth1660a292010-06-23 18:11:51 -07003526 inst = new(ctx) ir_return(ret);
Ian Romanick16a246c2010-03-19 16:45:19 -07003527 } else {
Eric Anholtaad7c772010-03-30 23:28:20 -10003528 if (state->current_function->return_type->base_type !=
3529 GLSL_TYPE_VOID) {
3530 YYLTYPE loc = this->get_location();
3531
3532 _mesa_glsl_error(& loc, state,
3533 "`return' with no value, in function %s returning "
3534 "non-void",
Kenneth Graunkef96c52b2010-04-21 15:17:26 -07003535 state->current_function->function_name());
Eric Anholtaad7c772010-03-30 23:28:20 -10003536 }
Carl Worth1660a292010-06-23 18:11:51 -07003537 inst = new(ctx) ir_return;
Ian Romanick16a246c2010-03-19 16:45:19 -07003538 }
3539
Kenneth Graunke6de82562010-06-29 09:59:40 -07003540 state->found_return = true;
Ian Romanick16a246c2010-03-19 16:45:19 -07003541 instructions->push_tail(inst);
Ian Romanickc0e76d82010-04-05 16:53:19 -07003542 break;
Ian Romanick16a246c2010-03-19 16:45:19 -07003543 }
3544
Ian Romanickc0e76d82010-04-05 16:53:19 -07003545 case ast_discard:
Eric Anholtb9802072010-03-30 23:40:14 -10003546 if (state->target != fragment_shader) {
3547 YYLTYPE loc = this->get_location();
3548
3549 _mesa_glsl_error(& loc, state,
3550 "`discard' may only appear in a fragment shader");
3551 }
Kenneth Graunke77049a72010-06-30 14:11:00 -07003552 instructions->push_tail(new(ctx) ir_discard);
Ian Romanickc0e76d82010-04-05 16:53:19 -07003553 break;
3554
3555 case ast_break:
3556 case ast_continue:
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003557 if (mode == ast_continue &&
3558 state->loop_nesting_ast == NULL) {
Ian Romanick4cf20cd2010-04-05 17:13:47 -07003559 YYLTYPE loc = this->get_location();
3560
3561 _mesa_glsl_error(& loc, state,
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003562 "continue may only appear in a loop");
3563 } else if (mode == ast_break &&
3564 state->loop_nesting_ast == NULL &&
Eric Anholt22d81f12012-01-28 11:26:02 -08003565 state->switch_state.switch_nesting_ast == NULL) {
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003566 YYLTYPE loc = this->get_location();
Ian Romanick4cf20cd2010-04-05 17:13:47 -07003567
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003568 _mesa_glsl_error(& loc, state,
3569 "break may only appear in a loop or a switch");
3570 } else {
3571 /* For a loop, inline the for loop expression again,
3572 * since we don't know where near the end of
3573 * the loop body the normal copy of it
Eric Anholt2d1ed7b2010-07-22 12:55:16 -07003574 * is going to be placed.
3575 */
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003576 if (state->loop_nesting_ast != NULL &&
3577 mode == ast_continue &&
3578 state->loop_nesting_ast->rest_expression) {
3579 state->loop_nesting_ast->rest_expression->hir(instructions,
3580 state);
Eric Anholt2d1ed7b2010-07-22 12:55:16 -07003581 }
3582
Eric Anholt22d81f12012-01-28 11:26:02 -08003583 if (state->switch_state.is_switch_innermost &&
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003584 mode == ast_break) {
3585 /* Force break out of switch by setting is_break switch state.
3586 */
Eric Anholt22d81f12012-01-28 11:26:02 -08003587 ir_variable *const is_break_var = state->switch_state.is_break_var;
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003588 ir_dereference_variable *const deref_is_break_var =
3589 new(ctx) ir_dereference_variable(is_break_var);
3590 ir_constant *const true_val = new(ctx) ir_constant(true);
3591 ir_assignment *const set_break_var =
Eric Anholtaa5ec132012-05-14 09:14:54 -07003592 new(ctx) ir_assignment(deref_is_break_var, true_val);
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003593
3594 instructions->push_tail(set_break_var);
3595 }
3596 else {
3597 ir_loop_jump *const jump =
Carl Worth1660a292010-06-23 18:11:51 -07003598 new(ctx) ir_loop_jump((mode == ast_break)
3599 ? ir_loop_jump::jump_break
3600 : ir_loop_jump::jump_continue);
Ian Romanick4cf20cd2010-04-05 17:13:47 -07003601 instructions->push_tail(jump);
3602 }
3603 }
3604
Ian Romanickc0e76d82010-04-05 16:53:19 -07003605 break;
Eric Anholtb9802072010-03-30 23:40:14 -10003606 }
3607
Ian Romanick16a246c2010-03-19 16:45:19 -07003608 /* Jump instructions do not have r-values.
3609 */
3610 return NULL;
3611}
Ian Romanick3c6fea32010-03-29 14:11:25 -07003612
3613
3614ir_rvalue *
3615ast_selection_statement::hir(exec_list *instructions,
3616 struct _mesa_glsl_parse_state *state)
3617{
Kenneth Graunke953ff122010-06-25 13:14:37 -07003618 void *ctx = state;
Carl Worth1660a292010-06-23 18:11:51 -07003619
Ian Romanick3c6fea32010-03-29 14:11:25 -07003620 ir_rvalue *const condition = this->condition->hir(instructions, state);
Ian Romanick3c6fea32010-03-29 14:11:25 -07003621
3622 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
3623 *
3624 * "Any expression whose type evaluates to a Boolean can be used as the
3625 * conditional expression bool-expression. Vector types are not accepted
3626 * as the expression to if."
3627 *
3628 * The checks are separated so that higher quality diagnostics can be
3629 * generated for cases where both rules are violated.
3630 */
3631 if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
3632 YYLTYPE loc = this->condition->get_location();
3633
3634 _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
3635 "boolean");
3636 }
3637
Carl Worth1660a292010-06-23 18:11:51 -07003638 ir_if *const stmt = new(ctx) ir_if(condition);
Ian Romanick3c6fea32010-03-29 14:11:25 -07003639
Kenneth Graunke665d75c2010-08-18 13:54:50 -07003640 if (then_statement != NULL) {
3641 state->symbols->push_scope();
Ian Romanick4f9d72f2010-05-10 11:10:26 -07003642 then_statement->hir(& stmt->then_instructions, state);
Kenneth Graunke665d75c2010-08-18 13:54:50 -07003643 state->symbols->pop_scope();
3644 }
Ian Romanick3c6fea32010-03-29 14:11:25 -07003645
Kenneth Graunke665d75c2010-08-18 13:54:50 -07003646 if (else_statement != NULL) {
3647 state->symbols->push_scope();
Ian Romanick4f9d72f2010-05-10 11:10:26 -07003648 else_statement->hir(& stmt->else_instructions, state);
Kenneth Graunke665d75c2010-08-18 13:54:50 -07003649 state->symbols->pop_scope();
3650 }
Ian Romanick3c6fea32010-03-29 14:11:25 -07003651
3652 instructions->push_tail(stmt);
3653
3654 /* if-statements do not have r-values.
3655 */
3656 return NULL;
3657}
Ian Romanick9e7d0102010-04-05 16:37:49 -07003658
3659
Dan McCabe85beb392011-11-07 15:11:04 -08003660ir_rvalue *
3661ast_switch_statement::hir(exec_list *instructions,
3662 struct _mesa_glsl_parse_state *state)
3663{
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003664 void *ctx = state;
3665
3666 ir_rvalue *const test_expression =
3667 this->test_expression->hir(instructions, state);
3668
3669 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
3670 *
3671 * "The type of init-expression in a switch statement must be a
3672 * scalar integer."
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003673 */
Eric Anholtbbbc7c72012-05-14 08:45:59 -07003674 if (!test_expression->type->is_scalar() ||
3675 !test_expression->type->is_integer()) {
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003676 YYLTYPE loc = this->test_expression->get_location();
3677
3678 _mesa_glsl_error(& loc,
3679 state,
3680 "switch-statement expression must be scalar "
3681 "integer");
3682 }
3683
3684 /* Track the switch-statement nesting in a stack-like manner.
3685 */
Eric Anholt22d81f12012-01-28 11:26:02 -08003686 struct glsl_switch_state saved = state->switch_state;
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003687
Eric Anholt22d81f12012-01-28 11:26:02 -08003688 state->switch_state.is_switch_innermost = true;
3689 state->switch_state.switch_nesting_ast = this;
Eric Anholt14063212012-01-30 08:50:14 -08003690 state->switch_state.labels_ht = hash_table_ctor(0, hash_table_pointer_hash,
3691 hash_table_pointer_compare);
Eric Anholt57e44372012-01-30 09:50:35 -08003692 state->switch_state.previous_default = NULL;
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003693
3694 /* Initalize is_fallthru state to false.
3695 */
3696 ir_rvalue *const is_fallthru_val = new (ctx) ir_constant(false);
Eric Anholt22d81f12012-01-28 11:26:02 -08003697 state->switch_state.is_fallthru_var =
3698 new(ctx) ir_variable(glsl_type::bool_type,
3699 "switch_is_fallthru_tmp",
3700 ir_var_temporary);
3701 instructions->push_tail(state->switch_state.is_fallthru_var);
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003702
3703 ir_dereference_variable *deref_is_fallthru_var =
Eric Anholt22d81f12012-01-28 11:26:02 -08003704 new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var);
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003705 instructions->push_tail(new(ctx) ir_assignment(deref_is_fallthru_var,
Eric Anholtaa5ec132012-05-14 09:14:54 -07003706 is_fallthru_val));
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003707
3708 /* Initalize is_break state to false.
3709 */
3710 ir_rvalue *const is_break_val = new (ctx) ir_constant(false);
Eric Anholt22d81f12012-01-28 11:26:02 -08003711 state->switch_state.is_break_var = new(ctx) ir_variable(glsl_type::bool_type,
3712 "switch_is_break_tmp",
3713 ir_var_temporary);
3714 instructions->push_tail(state->switch_state.is_break_var);
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003715
3716 ir_dereference_variable *deref_is_break_var =
Eric Anholt22d81f12012-01-28 11:26:02 -08003717 new(ctx) ir_dereference_variable(state->switch_state.is_break_var);
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003718 instructions->push_tail(new(ctx) ir_assignment(deref_is_break_var,
Eric Anholtaa5ec132012-05-14 09:14:54 -07003719 is_break_val));
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003720
3721 /* Cache test expression.
3722 */
3723 test_to_hir(instructions, state);
Eric Anholt5462f362012-05-14 08:37:50 -07003724
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003725 /* Emit code for body of switch stmt.
3726 */
3727 body->hir(instructions, state);
3728
Eric Anholt14063212012-01-30 08:50:14 -08003729 hash_table_dtor(state->switch_state.labels_ht);
3730
Eric Anholt22d81f12012-01-28 11:26:02 -08003731 state->switch_state = saved;
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003732
Eric Anholt5462f362012-05-14 08:37:50 -07003733 /* Switch statements do not have r-values. */
3734 return NULL;
3735}
Dan McCabe85beb392011-11-07 15:11:04 -08003736
3737
Eric Anholt5462f362012-05-14 08:37:50 -07003738void
3739ast_switch_statement::test_to_hir(exec_list *instructions,
3740 struct _mesa_glsl_parse_state *state)
3741{
3742 void *ctx = state;
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003743
Eric Anholt5462f362012-05-14 08:37:50 -07003744 /* Cache value of test expression. */
3745 ir_rvalue *const test_val =
3746 test_expression->hir(instructions,
3747 state);
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003748
Eric Anholt9c4e9ce2012-05-14 08:51:03 -07003749 state->switch_state.test_var = new(ctx) ir_variable(test_val->type,
Eric Anholt5462f362012-05-14 08:37:50 -07003750 "switch_test_tmp",
3751 ir_var_temporary);
3752 ir_dereference_variable *deref_test_var =
3753 new(ctx) ir_dereference_variable(state->switch_state.test_var);
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003754
Eric Anholt5462f362012-05-14 08:37:50 -07003755 instructions->push_tail(state->switch_state.test_var);
Eric Anholtaa5ec132012-05-14 09:14:54 -07003756 instructions->push_tail(new(ctx) ir_assignment(deref_test_var, test_val));
Eric Anholt5462f362012-05-14 08:37:50 -07003757}
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003758
3759
Eric Anholt5462f362012-05-14 08:37:50 -07003760ir_rvalue *
3761ast_switch_body::hir(exec_list *instructions,
3762 struct _mesa_glsl_parse_state *state)
3763{
3764 if (stmts != NULL)
3765 stmts->hir(instructions, state);
Eric Anholt22d81f12012-01-28 11:26:02 -08003766
Eric Anholt5462f362012-05-14 08:37:50 -07003767 /* Switch bodies do not have r-values. */
3768 return NULL;
3769}
3770
3771ir_rvalue *
3772ast_case_statement_list::hir(exec_list *instructions,
3773 struct _mesa_glsl_parse_state *state)
3774{
3775 foreach_list_typed (ast_case_statement, case_stmt, link, & this->cases)
3776 case_stmt->hir(instructions, state);
3777
3778 /* Case statements do not have r-values. */
3779 return NULL;
3780}
3781
3782ir_rvalue *
3783ast_case_statement::hir(exec_list *instructions,
3784 struct _mesa_glsl_parse_state *state)
3785{
3786 labels->hir(instructions, state);
3787
3788 /* Conditionally set fallthru state based on break state. */
3789 ir_constant *const false_val = new(state) ir_constant(false);
3790 ir_dereference_variable *const deref_is_fallthru_var =
3791 new(state) ir_dereference_variable(state->switch_state.is_fallthru_var);
3792 ir_dereference_variable *const deref_is_break_var =
3793 new(state) ir_dereference_variable(state->switch_state.is_break_var);
3794 ir_assignment *const reset_fallthru_on_break =
3795 new(state) ir_assignment(deref_is_fallthru_var,
3796 false_val,
3797 deref_is_break_var);
3798 instructions->push_tail(reset_fallthru_on_break);
3799
3800 /* Guard case statements depending on fallthru state. */
3801 ir_dereference_variable *const deref_fallthru_guard =
3802 new(state) ir_dereference_variable(state->switch_state.is_fallthru_var);
3803 ir_if *const test_fallthru = new(state) ir_if(deref_fallthru_guard);
3804
3805 foreach_list_typed (ast_node, stmt, link, & this->stmts)
3806 stmt->hir(& test_fallthru->then_instructions, state);
3807
3808 instructions->push_tail(test_fallthru);
3809
3810 /* Case statements do not have r-values. */
3811 return NULL;
3812}
Dan McCabe85beb392011-11-07 15:11:04 -08003813
3814
Eric Anholt5462f362012-05-14 08:37:50 -07003815ir_rvalue *
3816ast_case_label_list::hir(exec_list *instructions,
3817 struct _mesa_glsl_parse_state *state)
3818{
3819 foreach_list_typed (ast_case_label, label, link, & this->labels)
3820 label->hir(instructions, state);
Eric Anholt22d81f12012-01-28 11:26:02 -08003821
Eric Anholt5462f362012-05-14 08:37:50 -07003822 /* Case labels do not have r-values. */
3823 return NULL;
3824}
Dan McCabe85beb392011-11-07 15:11:04 -08003825
Eric Anholt5462f362012-05-14 08:37:50 -07003826ir_rvalue *
3827ast_case_label::hir(exec_list *instructions,
3828 struct _mesa_glsl_parse_state *state)
3829{
3830 void *ctx = state;
Dan McCabe85beb392011-11-07 15:11:04 -08003831
Eric Anholt5462f362012-05-14 08:37:50 -07003832 ir_dereference_variable *deref_fallthru_var =
3833 new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var);
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003834
Eric Anholt5462f362012-05-14 08:37:50 -07003835 ir_rvalue *const true_val = new(ctx) ir_constant(true);
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003836
Eric Anholt5462f362012-05-14 08:37:50 -07003837 /* If not default case, ... */
3838 if (this->test_value != NULL) {
3839 /* Conditionally set fallthru state based on
3840 * comparison of cached test expression value to case label.
3841 */
3842 ir_rvalue *const label_rval = this->test_value->hir(instructions, state);
3843 ir_constant *label_const = label_rval->constant_expression_value();
Eric Anholt22d81f12012-01-28 11:26:02 -08003844
Eric Anholt5462f362012-05-14 08:37:50 -07003845 if (!label_const) {
3846 YYLTYPE loc = this->test_value->get_location();
Eric Anholt22d81f12012-01-28 11:26:02 -08003847
Eric Anholt5462f362012-05-14 08:37:50 -07003848 _mesa_glsl_error(& loc, state,
3849 "switch statement case label must be a "
3850 "constant expression");
Eric Anholt22d81f12012-01-28 11:26:02 -08003851
Eric Anholt5462f362012-05-14 08:37:50 -07003852 /* Stuff a dummy value in to allow processing to continue. */
3853 label_const = new(ctx) ir_constant(0);
3854 } else {
3855 ast_expression *previous_label = (ast_expression *)
3856 hash_table_find(state->switch_state.labels_ht,
3857 (void *)(uintptr_t)label_const->value.u[0]);
Dan McCabe85beb392011-11-07 15:11:04 -08003858
Eric Anholt5462f362012-05-14 08:37:50 -07003859 if (previous_label) {
3860 YYLTYPE loc = this->test_value->get_location();
3861 _mesa_glsl_error(& loc, state,
3862 "duplicate case value");
Dan McCabe85beb392011-11-07 15:11:04 -08003863
Eric Anholt5462f362012-05-14 08:37:50 -07003864 loc = previous_label->get_location();
3865 _mesa_glsl_error(& loc, state,
3866 "this is the previous case label");
3867 } else {
3868 hash_table_insert(state->switch_state.labels_ht,
3869 this->test_value,
Eric Anholt14063212012-01-30 08:50:14 -08003870 (void *)(uintptr_t)label_const->value.u[0]);
Eric Anholt5462f362012-05-14 08:37:50 -07003871 }
3872 }
Eric Anholt14063212012-01-30 08:50:14 -08003873
Eric Anholt5462f362012-05-14 08:37:50 -07003874 ir_dereference_variable *deref_test_var =
3875 new(ctx) ir_dereference_variable(state->switch_state.test_var);
Eric Anholt14063212012-01-30 08:50:14 -08003876
Eric Anholt5462f362012-05-14 08:37:50 -07003877 ir_rvalue *const test_cond = new(ctx) ir_expression(ir_binop_all_equal,
Eric Anholt5462f362012-05-14 08:37:50 -07003878 label_const,
3879 deref_test_var);
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003880
Eric Anholt5462f362012-05-14 08:37:50 -07003881 ir_assignment *set_fallthru_on_test =
3882 new(ctx) ir_assignment(deref_fallthru_var,
3883 true_val,
3884 test_cond);
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003885
Eric Anholt5462f362012-05-14 08:37:50 -07003886 instructions->push_tail(set_fallthru_on_test);
3887 } else { /* default case */
3888 if (state->switch_state.previous_default) {
Eric Anholt5462f362012-05-14 08:37:50 -07003889 YYLTYPE loc = this->get_location();
3890 _mesa_glsl_error(& loc, state,
3891 "multiple default labels in one switch");
Eric Anholt22d81f12012-01-28 11:26:02 -08003892
Eric Anholt5462f362012-05-14 08:37:50 -07003893 loc = state->switch_state.previous_default->get_location();
3894 _mesa_glsl_error(& loc, state,
3895 "this is the first default label");
3896 }
3897 state->switch_state.previous_default = this;
Eric Anholt57e44372012-01-30 09:50:35 -08003898
Eric Anholt5462f362012-05-14 08:37:50 -07003899 /* Set falltrhu state. */
3900 ir_assignment *set_fallthru =
Eric Anholtaa5ec132012-05-14 09:14:54 -07003901 new(ctx) ir_assignment(deref_fallthru_var, true_val);
Eric Anholt57e44372012-01-30 09:50:35 -08003902
Eric Anholt5462f362012-05-14 08:37:50 -07003903 instructions->push_tail(set_fallthru);
3904 }
Eric Anholt57e44372012-01-30 09:50:35 -08003905
Eric Anholt5462f362012-05-14 08:37:50 -07003906 /* Case statements do not have r-values. */
3907 return NULL;
3908}
Eric Anholt22d81f12012-01-28 11:26:02 -08003909
Eric Anholt5462f362012-05-14 08:37:50 -07003910void
3911ast_iteration_statement::condition_to_hir(ir_loop *stmt,
3912 struct _mesa_glsl_parse_state *state)
3913{
3914 void *ctx = state;
Eric Anholt22d81f12012-01-28 11:26:02 -08003915
Eric Anholt5462f362012-05-14 08:37:50 -07003916 if (condition != NULL) {
3917 ir_rvalue *const cond =
3918 condition->hir(& stmt->body_instructions, state);
3919
3920 if ((cond == NULL)
3921 || !cond->type->is_boolean() || !cond->type->is_scalar()) {
3922 YYLTYPE loc = condition->get_location();
3923
3924 _mesa_glsl_error(& loc, state,
3925 "loop condition must be scalar boolean");
3926 } else {
3927 /* As the first code in the loop body, generate a block that looks
3928 * like 'if (!condition) break;' as the loop termination condition.
3929 */
3930 ir_rvalue *const not_cond =
Eric Anholt5d6ea162012-05-14 08:39:54 -07003931 new(ctx) ir_expression(ir_unop_logic_not, cond);
Eric Anholt5462f362012-05-14 08:37:50 -07003932
3933 ir_if *const if_stmt = new(ctx) ir_if(not_cond);
3934
3935 ir_jump *const break_stmt =
3936 new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
3937
3938 if_stmt->then_instructions.push_tail(break_stmt);
3939 stmt->body_instructions.push_tail(if_stmt);
3940 }
3941 }
3942}
Dan McCabe85beb392011-11-07 15:11:04 -08003943
3944
Eric Anholt5462f362012-05-14 08:37:50 -07003945ir_rvalue *
3946ast_iteration_statement::hir(exec_list *instructions,
3947 struct _mesa_glsl_parse_state *state)
3948{
3949 void *ctx = state;
Carl Worth1660a292010-06-23 18:11:51 -07003950
Eric Anholt5462f362012-05-14 08:37:50 -07003951 /* For-loops and while-loops start a new scope, but do-while loops do not.
3952 */
3953 if (mode != ast_do_while)
3954 state->symbols->push_scope();
Ian Romanick9e7d0102010-04-05 16:37:49 -07003955
Eric Anholt5462f362012-05-14 08:37:50 -07003956 if (init_statement != NULL)
3957 init_statement->hir(instructions, state);
Ian Romanick9e7d0102010-04-05 16:37:49 -07003958
Eric Anholt5462f362012-05-14 08:37:50 -07003959 ir_loop *const stmt = new(ctx) ir_loop();
3960 instructions->push_tail(stmt);
Ian Romanick9e7d0102010-04-05 16:37:49 -07003961
Eric Anholt5462f362012-05-14 08:37:50 -07003962 /* Track the current loop nesting. */
3963 ast_iteration_statement *nesting_ast = state->loop_nesting_ast;
Ian Romanick9e7d0102010-04-05 16:37:49 -07003964
Eric Anholt5462f362012-05-14 08:37:50 -07003965 state->loop_nesting_ast = this;
Ian Romanick9e7d0102010-04-05 16:37:49 -07003966
Eric Anholt5462f362012-05-14 08:37:50 -07003967 /* Likewise, indicate that following code is closest to a loop,
3968 * NOT closest to a switch.
3969 */
3970 bool saved_is_switch_innermost = state->switch_state.is_switch_innermost;
3971 state->switch_state.is_switch_innermost = false;
Ian Romanick8c46ed22010-04-05 18:07:27 -07003972
Eric Anholt5462f362012-05-14 08:37:50 -07003973 if (mode != ast_do_while)
3974 condition_to_hir(stmt, state);
Ian Romanick8c46ed22010-04-05 18:07:27 -07003975
Eric Anholt5462f362012-05-14 08:37:50 -07003976 if (body != NULL)
3977 body->hir(& stmt->body_instructions, state);
Carl Worth1660a292010-06-23 18:11:51 -07003978
Eric Anholt5462f362012-05-14 08:37:50 -07003979 if (rest_expression != NULL)
3980 rest_expression->hir(& stmt->body_instructions, state);
Ian Romanick8c46ed22010-04-05 18:07:27 -07003981
Eric Anholt5462f362012-05-14 08:37:50 -07003982 if (mode == ast_do_while)
3983 condition_to_hir(stmt, state);
Ian Romanick8c46ed22010-04-05 18:07:27 -07003984
Eric Anholt5462f362012-05-14 08:37:50 -07003985 if (mode != ast_do_while)
3986 state->symbols->pop_scope();
Ian Romanick8c46ed22010-04-05 18:07:27 -07003987
Eric Anholt5462f362012-05-14 08:37:50 -07003988 /* Restore previous nesting before returning. */
3989 state->loop_nesting_ast = nesting_ast;
3990 state->switch_state.is_switch_innermost = saved_is_switch_innermost;
Ian Romanicke9d0f262010-04-05 17:01:53 -07003991
Ian Romanick9e7d0102010-04-05 16:37:49 -07003992 /* Loops do not have r-values.
3993 */
3994 return NULL;
3995}
Ian Romanick3455ce62010-04-19 15:13:15 -07003996
3997
Paul Berryd5948f22013-02-12 12:36:41 -08003998/**
3999 * Determine if the given type is valid for establishing a default precision
4000 * qualifier.
4001 *
4002 * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
4003 *
4004 * "The precision statement
4005 *
4006 * precision precision-qualifier type;
4007 *
4008 * can be used to establish a default precision qualifier. The type field
4009 * can be either int or float or any of the sampler types, and the
4010 * precision-qualifier can be lowp, mediump, or highp."
4011 *
4012 * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
4013 * qualifiers on sampler types, but this seems like an oversight (since the
4014 * intention of including these in GLSL 1.30 is to allow compatibility with ES
4015 * shaders). So we allow int, float, and all sampler types regardless of GLSL
4016 * version.
4017 */
4018static bool
4019is_valid_default_precision_type(const struct _mesa_glsl_parse_state *state,
4020 const char *type_name)
4021{
4022 const struct glsl_type *type = state->symbols->get_type(type_name);
4023 if (type == NULL)
4024 return false;
4025
4026 switch (type->base_type) {
4027 case GLSL_TYPE_INT:
4028 case GLSL_TYPE_FLOAT:
4029 /* "int" and "float" are valid, but vectors and matrices are not. */
4030 return type->vector_elements == 1 && type->matrix_columns == 1;
4031 case GLSL_TYPE_SAMPLER:
4032 return true;
4033 default:
4034 return false;
4035 }
4036}
4037
4038
Ian Romanick3455ce62010-04-19 15:13:15 -07004039ir_rvalue *
4040ast_type_specifier::hir(exec_list *instructions,
4041 struct _mesa_glsl_parse_state *state)
4042{
Kenneth Graunke308d4c72013-07-15 15:39:35 -07004043 if (this->default_precision == ast_precision_none && this->structure == NULL)
Chad Versace08a286c2011-01-16 21:44:57 -08004044 return NULL;
4045
4046 YYLTYPE loc = this->get_location();
4047
Chad Versace08a286c2011-01-16 21:44:57 -08004048 /* If this is a precision statement, check that the type to which it is
4049 * applied is either float or int.
4050 *
4051 * From section 4.5.3 of the GLSL 1.30 spec:
4052 * "The precision statement
4053 * precision precision-qualifier type;
4054 * can be used to establish a default precision qualifier. The type
4055 * field can be either int or float [...]. Any other types or
4056 * qualifiers will result in an error.
4057 */
Kenneth Graunke308d4c72013-07-15 15:39:35 -07004058 if (this->default_precision != ast_precision_none) {
Kenneth Graunke6eec5022013-07-15 15:58:29 -07004059 if (!state->check_precision_qualifiers_allowed(&loc))
4060 return NULL;
4061
4062 if (this->structure != NULL) {
4063 _mesa_glsl_error(&loc, state,
4064 "precision qualifiers do not apply to structures");
4065 return NULL;
4066 }
4067
Chad Versace08a286c2011-01-16 21:44:57 -08004068 if (this->is_array) {
4069 _mesa_glsl_error(&loc, state,
4070 "default precision statements do not apply to "
4071 "arrays");
4072 return NULL;
4073 }
Paul Berryd5948f22013-02-12 12:36:41 -08004074 if (!is_valid_default_precision_type(state, this->type_name)) {
Chad Versace08a286c2011-01-16 21:44:57 -08004075 _mesa_glsl_error(&loc, state,
4076 "default precision statements apply only to types "
Paul Berryd5948f22013-02-12 12:36:41 -08004077 "float, int, and sampler types");
Chad Versace08a286c2011-01-16 21:44:57 -08004078 return NULL;
4079 }
4080
4081 /* FINISHME: Translate precision statements into IR. */
4082 return NULL;
4083 }
4084
Matt Turner1b0d6ae2013-06-29 19:29:16 -07004085 /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
4086 * process_record_constructor() can do type-checking on C-style initializer
4087 * expressions of structs, but ast_struct_specifier should only be translated
4088 * to HIR if it is declaring the type of a structure.
4089 *
4090 * The ->is_declaration field is false for initializers of variables
4091 * declared separately from the struct's type definition.
4092 *
4093 * struct S { ... }; (is_declaration = true)
4094 * struct T { ... } t = { ... }; (is_declaration = true)
4095 * S s = { ... }; (is_declaration = false)
4096 */
4097 if (this->structure != NULL && this->structure->is_declaration)
Ian Romanick3455ce62010-04-19 15:13:15 -07004098 return this->structure->hir(instructions, state);
Ian Romanick85ba37b2010-04-21 14:33:34 -07004099
4100 return NULL;
Ian Romanick3455ce62010-04-19 15:13:15 -07004101}
4102
4103
Ian Romanick51f740c2012-12-08 17:38:30 -08004104/**
4105 * Process a structure or interface block tree into an array of structure fields
4106 *
4107 * After parsing, where there are some syntax differnces, structures and
4108 * interface blocks are almost identical. They are similar enough that the
4109 * AST for each can be processed the same way into a set of
4110 * \c glsl_struct_field to describe the members.
4111 *
4112 * \return
4113 * The number of fields processed. A pointer to the array structure fields is
4114 * stored in \c *fields_ret.
4115 */
4116unsigned
4117ast_process_structure_or_interface_block(exec_list *instructions,
4118 struct _mesa_glsl_parse_state *state,
4119 exec_list *declarations,
4120 YYLTYPE &loc,
Ian Romanick17e6f192012-12-11 12:14:03 -08004121 glsl_struct_field **fields_ret,
4122 bool is_interface,
4123 bool block_row_major)
Ian Romanick3455ce62010-04-19 15:13:15 -07004124{
Ian Romanick3455ce62010-04-19 15:13:15 -07004125 unsigned decl_count = 0;
4126
Ian Romanick51f740c2012-12-08 17:38:30 -08004127 /* Make an initial pass over the list of fields to determine how
Ian Romanick3455ce62010-04-19 15:13:15 -07004128 * many there are. Each element in this list is an ast_declarator_list.
4129 * This means that we actually need to count the number of elements in the
4130 * 'declarations' list in each of the elements.
4131 */
Ian Romanick51f740c2012-12-08 17:38:30 -08004132 foreach_list_typed (ast_declarator_list, decl_list, link, declarations) {
Ian Romanick304ea902010-05-10 11:17:53 -07004133 foreach_list_const (decl_ptr, & decl_list->declarations) {
Ian Romanick3455ce62010-04-19 15:13:15 -07004134 decl_count++;
4135 }
4136 }
4137
Ian Romanick51f740c2012-12-08 17:38:30 -08004138 /* Allocate storage for the fields and process the field
Ian Romanick3455ce62010-04-19 15:13:15 -07004139 * declarations. As the declarations are processed, try to also convert
4140 * the types to HIR. This ensures that structure definitions embedded in
Ian Romanick51f740c2012-12-08 17:38:30 -08004141 * other structure definitions or in interface blocks are processed.
Ian Romanick3455ce62010-04-19 15:13:15 -07004142 */
Kenneth Graunked3073f52011-01-21 14:32:31 -08004143 glsl_struct_field *const fields = ralloc_array(state, glsl_struct_field,
Eric Anholt21b0dbd2010-07-20 16:47:25 -07004144 decl_count);
Ian Romanick3455ce62010-04-19 15:13:15 -07004145
4146 unsigned i = 0;
Ian Romanick51f740c2012-12-08 17:38:30 -08004147 foreach_list_typed (ast_declarator_list, decl_list, link, declarations) {
Ian Romanick3455ce62010-04-19 15:13:15 -07004148 const char *type_name;
4149
4150 decl_list->type->specifier->hir(instructions, state);
4151
Kenneth Graunkec98deb12010-08-16 14:02:25 -07004152 /* Section 10.9 of the GLSL ES 1.00 specification states that
4153 * embedded structure definitions have been removed from the language.
4154 */
4155 if (state->es_shader && decl_list->type->specifier->structure != NULL) {
Kenneth Graunkec98deb12010-08-16 14:02:25 -07004156 _mesa_glsl_error(&loc, state, "Embedded structure definitions are "
4157 "not allowed in GLSL ES 1.00.");
4158 }
4159
Ian Romanick3455ce62010-04-19 15:13:15 -07004160 const glsl_type *decl_type =
4161 decl_list->type->specifier->glsl_type(& type_name, state);
4162
Ian Romanick2b97dc62010-05-10 17:42:05 -07004163 foreach_list_typed (ast_declaration, decl, link,
4164 &decl_list->declarations) {
Ian Romanick17e6f192012-12-11 12:14:03 -08004165 /* From the GL_ARB_uniform_buffer_object spec:
4166 *
4167 * "Sampler types are not allowed inside of uniform
4168 * blocks. All other types, arrays, and structures
4169 * allowed for uniforms are allowed within a uniform
4170 * block."
Ian Romanick278c9af2013-04-08 16:37:04 -07004171 *
4172 * It should be impossible for decl_type to be NULL here. Cases that
4173 * might naturally lead to decl_type being NULL, especially for the
4174 * is_interface case, will have resulted in compilation having
4175 * already halted due to a syntax error.
Ian Romanick17e6f192012-12-11 12:14:03 -08004176 */
Ian Romanick278c9af2013-04-08 16:37:04 -07004177 const struct glsl_type *field_type =
4178 decl_type != NULL ? decl_type : glsl_type::error_type;
Ian Romanick17e6f192012-12-11 12:14:03 -08004179
4180 if (is_interface && field_type->contains_sampler()) {
4181 YYLTYPE loc = decl_list->get_location();
4182 _mesa_glsl_error(&loc, state,
4183 "Uniform in non-default uniform block contains sampler\n");
4184 }
4185
4186 const struct ast_type_qualifier *const qual =
4187 & decl_list->type->qualifier;
4188 if (qual->flags.q.std140 ||
4189 qual->flags.q.packed ||
4190 qual->flags.q.shared) {
4191 _mesa_glsl_error(&loc, state,
4192 "uniform block layout qualifiers std140, packed, and "
4193 "shared can only be applied to uniform blocks, not "
4194 "members");
4195 }
4196
Kenneth Graunked8e34e22010-08-07 02:56:01 -07004197 if (decl->is_array) {
Kenneth Graunked8e34e22010-08-07 02:56:01 -07004198 field_type = process_array_type(&loc, decl_type, decl->array_size,
4199 state);
4200 }
Ian Romanick278c9af2013-04-08 16:37:04 -07004201 fields[i].type = field_type;
Ian Romanick3455ce62010-04-19 15:13:15 -07004202 fields[i].name = decl->identifier;
Ian Romanick17e6f192012-12-11 12:14:03 -08004203
4204 if (qual->flags.q.row_major || qual->flags.q.column_major) {
Jordan Justencb29a702013-03-23 17:16:28 -07004205 if (!qual->flags.q.uniform) {
4206 _mesa_glsl_error(&loc, state,
4207 "row_major and column_major can only be "
4208 "applied to uniform interface blocks.");
4209 } else if (!field_type->is_matrix() && !field_type->is_record()) {
Ian Romanick17e6f192012-12-11 12:14:03 -08004210 _mesa_glsl_error(&loc, state,
4211 "uniform block layout qualifiers row_major and "
4212 "column_major can only be applied to matrix and "
4213 "structure types");
4214 } else
4215 validate_matrix_layout_for_type(state, &loc, field_type);
4216 }
4217
Jordan Justen067cc082013-03-23 17:14:37 -07004218 if (qual->flags.q.uniform && qual->has_interpolation()) {
4219 _mesa_glsl_error(&loc, state,
4220 "interpolation qualifiers cannot be used "
4221 "with uniform interface blocks");
4222 }
4223
Ian Romanick17e6f192012-12-11 12:14:03 -08004224 if (field_type->is_matrix() ||
4225 (field_type->is_array() && field_type->fields.array->is_matrix())) {
4226 fields[i].row_major = block_row_major;
4227 if (qual->flags.q.row_major)
4228 fields[i].row_major = true;
4229 else if (qual->flags.q.column_major)
4230 fields[i].row_major = false;
4231 }
4232
Ian Romanick3455ce62010-04-19 15:13:15 -07004233 i++;
4234 }
4235 }
4236
4237 assert(i == decl_count);
4238
Ian Romanick51f740c2012-12-08 17:38:30 -08004239 *fields_ret = fields;
4240 return decl_count;
4241}
4242
4243
4244ir_rvalue *
4245ast_struct_specifier::hir(exec_list *instructions,
4246 struct _mesa_glsl_parse_state *state)
4247{
4248 YYLTYPE loc = this->get_location();
4249 glsl_struct_field *fields;
4250 unsigned decl_count =
4251 ast_process_structure_or_interface_block(instructions,
4252 state,
4253 &this->declarations,
4254 loc,
Ian Romanick17e6f192012-12-11 12:14:03 -08004255 &fields,
4256 false,
4257 false);
Ian Romanick51f740c2012-12-08 17:38:30 -08004258
Ian Romanick49e35772010-06-28 11:54:57 -07004259 const glsl_type *t =
Kenneth Graunkeca92ae22010-09-18 11:11:09 +02004260 glsl_type::get_record_instance(fields, decl_count, this->name);
Ian Romanick1d28b612010-04-20 16:48:24 -07004261
Ian Romanicka789ca62010-09-01 14:08:08 -07004262 if (!state->symbols->add_type(name, t)) {
Ian Romanickab899272010-04-23 13:24:08 -07004263 _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
4264 } else {
Kenneth Graunkeeb639342011-02-27 01:17:29 -08004265 const glsl_type **s = reralloc(state, state->user_structures,
4266 const glsl_type *,
4267 state->num_user_structures + 1);
Ian Romanicka2c6df52010-04-28 13:14:53 -07004268 if (s != NULL) {
4269 s[state->num_user_structures] = t;
4270 state->user_structures = s;
4271 state->num_user_structures++;
4272 }
Ian Romanickab899272010-04-23 13:24:08 -07004273 }
Ian Romanick3455ce62010-04-19 15:13:15 -07004274
4275 /* Structure type definitions do not have r-values.
4276 */
4277 return NULL;
4278}
Eric Anholt4b2a4cb2012-03-29 17:29:20 -07004279
Eric Anholt2d03f482012-04-18 13:35:56 -07004280ir_rvalue *
Jordan Justenc9f58542013-03-09 10:40:41 -08004281ast_interface_block::hir(exec_list *instructions,
4282 struct _mesa_glsl_parse_state *state)
Eric Anholt2d03f482012-04-18 13:35:56 -07004283{
Ian Romanick17e6f192012-12-11 12:14:03 -08004284 YYLTYPE loc = this->get_location();
4285
Jordan Justenc9f58542013-03-09 10:40:41 -08004286 /* The ast_interface_block has a list of ast_declarator_lists. We
Eric Anholt2d03f482012-04-18 13:35:56 -07004287 * need to turn those into ir_variables with an association
4288 * with this uniform block.
4289 */
Ian Romanick514f8c72013-01-22 01:09:16 -05004290 enum glsl_interface_packing packing;
Ian Romanick9a971ab2012-12-13 02:13:30 -08004291 if (this->layout.flags.q.shared) {
Ian Romanick514f8c72013-01-22 01:09:16 -05004292 packing = GLSL_INTERFACE_PACKING_SHARED;
Ian Romanick9a971ab2012-12-13 02:13:30 -08004293 } else if (this->layout.flags.q.packed) {
Ian Romanick514f8c72013-01-22 01:09:16 -05004294 packing = GLSL_INTERFACE_PACKING_PACKED;
Ian Romanick9a971ab2012-12-13 02:13:30 -08004295 } else {
4296 /* The default layout is std140.
4297 */
Ian Romanick514f8c72013-01-22 01:09:16 -05004298 packing = GLSL_INTERFACE_PACKING_STD140;
Ian Romanick9a971ab2012-12-13 02:13:30 -08004299 }
4300
Ian Romanick17e6f192012-12-11 12:14:03 -08004301 bool block_row_major = this->layout.flags.q.row_major;
4302 exec_list declared_variables;
4303 glsl_struct_field *fields;
4304 unsigned int num_variables =
4305 ast_process_structure_or_interface_block(&declared_variables,
4306 state,
4307 &this->declarations,
4308 loc,
4309 &fields,
4310 true,
4311 block_row_major);
4312
Jordan Justenb24eeb02013-03-09 16:34:55 -08004313 ir_variable_mode var_mode;
4314 const char *iface_type_name;
4315 if (this->layout.flags.q.in) {
4316 var_mode = ir_var_shader_in;
4317 iface_type_name = "in";
4318 } else if (this->layout.flags.q.out) {
4319 var_mode = ir_var_shader_out;
4320 iface_type_name = "out";
4321 } else if (this->layout.flags.q.uniform) {
4322 var_mode = ir_var_uniform;
4323 iface_type_name = "uniform";
4324 } else {
Emil Velikov4df68232013-07-08 18:29:17 +01004325 var_mode = ir_var_auto;
4326 iface_type_name = "UNKNOWN";
Jordan Justenb24eeb02013-03-09 16:34:55 -08004327 assert(!"interface block layout qualifier not found!");
4328 }
4329
Ian Romanick17e6f192012-12-11 12:14:03 -08004330 const glsl_type *block_type =
4331 glsl_type::get_interface_instance(fields,
4332 num_variables,
Ian Romanick514f8c72013-01-22 01:09:16 -05004333 packing,
Ian Romanick17e6f192012-12-11 12:14:03 -08004334 this->block_name);
4335
Jordan Justenb24eeb02013-03-09 16:34:55 -08004336 if (!state->symbols->add_interface(block_type->name, block_type, var_mode)) {
Ian Romanick53836612013-01-21 23:01:33 -05004337 YYLTYPE loc = this->get_location();
Jordan Justenb24eeb02013-03-09 16:34:55 -08004338 _mesa_glsl_error(&loc, state, "Interface block `%s' with type `%s' "
4339 "already taken in the current scope.\n",
4340 this->block_name, iface_type_name);
Ian Romanick53836612013-01-21 23:01:33 -05004341 }
4342
Ian Romanick17e6f192012-12-11 12:14:03 -08004343 /* Since interface blocks cannot contain statements, it should be
4344 * impossible for the block to generate any instructions.
4345 */
4346 assert(declared_variables.is_empty());
4347
4348 /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
4349 * says:
4350 *
4351 * "If an instance name (instance-name) is used, then it puts all the
4352 * members inside a scope within its own name space, accessed with the
4353 * field selector ( . ) operator (analogously to structures)."
4354 */
4355 if (this->instance_name) {
Ian Romanick25e75b02013-01-21 23:06:45 -05004356 ir_variable *var;
4357
4358 if (this->array_size != NULL) {
4359 const glsl_type *block_array_type =
4360 process_array_type(&loc, block_type, this->array_size, state);
4361
4362 var = new(state) ir_variable(block_array_type,
4363 this->instance_name,
Jordan Justenb24eeb02013-03-09 16:34:55 -08004364 var_mode);
Ian Romanick25e75b02013-01-21 23:06:45 -05004365 } else {
4366 var = new(state) ir_variable(block_type,
4367 this->instance_name,
Jordan Justenb24eeb02013-03-09 16:34:55 -08004368 var_mode);
Ian Romanick25e75b02013-01-21 23:06:45 -05004369 }
Ian Romanick17e6f192012-12-11 12:14:03 -08004370
Ian Romanick7a7b44b2013-01-21 21:51:15 -05004371 var->interface_type = block_type;
Ian Romanick17e6f192012-12-11 12:14:03 -08004372 state->symbols->add_variable(var);
4373 instructions->push_tail(var);
4374 } else {
Ian Romanick25e75b02013-01-21 23:06:45 -05004375 /* In order to have an array size, the block must also be declared with
4376 * an instane name.
4377 */
4378 assert(this->array_size == NULL);
4379
Ian Romanick17e6f192012-12-11 12:14:03 -08004380 for (unsigned i = 0; i < num_variables; i++) {
4381 ir_variable *var =
4382 new(state) ir_variable(fields[i].type,
4383 ralloc_strdup(state, fields[i].name),
Jordan Justenb24eeb02013-03-09 16:34:55 -08004384 var_mode);
Ian Romanick7a7b44b2013-01-21 21:51:15 -05004385 var->interface_type = block_type;
Ian Romanick17e6f192012-12-11 12:14:03 -08004386
4387 state->symbols->add_variable(var);
4388 instructions->push_tail(var);
Eric Anholtb3c093c2012-04-26 18:21:43 -07004389 }
4390 }
4391
Eric Anholt2d03f482012-04-18 13:35:56 -07004392 return NULL;
4393}
4394
Eric Anholt4b2a4cb2012-03-29 17:29:20 -07004395static void
4396detect_conflicting_assignments(struct _mesa_glsl_parse_state *state,
4397 exec_list *instructions)
4398{
4399 bool gl_FragColor_assigned = false;
4400 bool gl_FragData_assigned = false;
4401 bool user_defined_fs_output_assigned = false;
4402 ir_variable *user_defined_fs_output = NULL;
4403
4404 /* It would be nice to have proper location information. */
4405 YYLTYPE loc;
4406 memset(&loc, 0, sizeof(loc));
4407
4408 foreach_list(node, instructions) {
4409 ir_variable *var = ((ir_instruction *)node)->as_variable();
4410
Eric Anholtb2ee5a02012-04-23 16:10:12 -07004411 if (!var || !var->assigned)
Eric Anholt4b2a4cb2012-03-29 17:29:20 -07004412 continue;
4413
4414 if (strcmp(var->name, "gl_FragColor") == 0)
Eric Anholtb2ee5a02012-04-23 16:10:12 -07004415 gl_FragColor_assigned = true;
Eric Anholt4b2a4cb2012-03-29 17:29:20 -07004416 else if (strcmp(var->name, "gl_FragData") == 0)
Eric Anholtb2ee5a02012-04-23 16:10:12 -07004417 gl_FragData_assigned = true;
Eric Anholt4b2a4cb2012-03-29 17:29:20 -07004418 else if (strncmp(var->name, "gl_", 3) != 0) {
4419 if (state->target == fragment_shader &&
Paul Berry42a29d82013-01-11 14:39:32 -08004420 var->mode == ir_var_shader_out) {
Eric Anholt4b2a4cb2012-03-29 17:29:20 -07004421 user_defined_fs_output_assigned = true;
4422 user_defined_fs_output = var;
4423 }
4424 }
4425 }
4426
4427 /* From the GLSL 1.30 spec:
4428 *
4429 * "If a shader statically assigns a value to gl_FragColor, it
4430 * may not assign a value to any element of gl_FragData. If a
4431 * shader statically writes a value to any element of
4432 * gl_FragData, it may not assign a value to
4433 * gl_FragColor. That is, a shader may assign values to either
4434 * gl_FragColor or gl_FragData, but not both. Multiple shaders
4435 * linked together must also consistently write just one of
4436 * these variables. Similarly, if user declared output
4437 * variables are in use (statically assigned to), then the
4438 * built-in variables gl_FragColor and gl_FragData may not be
4439 * assigned to. These incorrect usages all generate compile
4440 * time errors."
4441 */
4442 if (gl_FragColor_assigned && gl_FragData_assigned) {
4443 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
4444 "`gl_FragColor' and `gl_FragData'\n");
4445 } else if (gl_FragColor_assigned && user_defined_fs_output_assigned) {
4446 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
4447 "`gl_FragColor' and `%s'\n",
4448 user_defined_fs_output->name);
4449 } else if (gl_FragData_assigned && user_defined_fs_output_assigned) {
4450 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
4451 "`gl_FragData' and `%s'\n",
4452 user_defined_fs_output->name);
4453 }
4454}