blob: 335df11e3a6e0ce455165722509cee277263618d [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
Eric Anholt624b7ba2013-06-12 14:03:49 -070075 state->gs_input_prim_type_specified = false;
76
Kenneth Graunkea0442852010-08-23 14:52:06 -070077 /* Section 4.2 of the GLSL 1.20 specification states:
78 * "The built-in functions are scoped in a scope outside the global scope
79 * users declare global variables in. That is, a shader's global scope,
80 * available for user-defined functions and global variables, is nested
81 * inside the scope containing the built-in functions."
82 *
83 * Since built-in functions like ftransform() access built-in variables,
84 * it follows that those must be in the outer scope as well.
85 *
86 * We push scope here to create this nesting effect...but don't pop.
87 * This way, a shader's globals are still in the symbol table for use
88 * by the linker.
89 */
90 state->symbols->push_scope();
91
Ian Romanick2b97dc62010-05-10 17:42:05 -070092 foreach_list_typed (ast_node, ast, link, & state->translation_unit)
Ian Romanick304ea902010-05-10 11:17:53 -070093 ast->hir(instructions, state);
Ian Romanick02c5ae12011-07-11 10:46:01 -070094
95 detect_recursion_unlinked(state, instructions);
Eric Anholt4b2a4cb2012-03-29 17:29:20 -070096 detect_conflicting_assignments(state, instructions);
Paul Berry0d81b0e2011-07-29 15:28:52 -070097
98 state->toplevel_ir = NULL;
Ian Romanickc170c902013-06-07 17:05:22 -070099
100 /* Move all of the variable declarations to the front of the IR list, and
101 * reverse the order. This has the (intended!) side effect that vertex
102 * shader inputs and fragment shader outputs will appear in the IR in the
103 * same order that they appeared in the shader code. This results in the
104 * locations being assigned in the declared order. Many (arguably buggy)
105 * applications depend on this behavior, and it matches what nearly all
106 * other drivers do.
107 */
108 foreach_list_safe(node, instructions) {
109 ir_variable *const var = ((ir_instruction *) node)->as_variable();
110
111 if (var == NULL)
112 continue;
113
114 var->remove();
115 instructions->push_head(var);
116 }
Ian Romanickd949a9a2010-03-10 09:55:22 -0800117}
118
119
Ian Romanick01045362010-03-29 16:17:56 -0700120/**
121 * If a conversion is available, convert one operand to a different type
122 *
123 * The \c from \c ir_rvalue is converted "in place".
124 *
125 * \param to Type that the operand it to be converted to
126 * \param from Operand that is being converted
127 * \param state GLSL compiler state
128 *
129 * \return
130 * If a conversion is possible (or unnecessary), \c true is returned.
131 * Otherwise \c false is returned.
132 */
Kenneth Graunkef32d3df2010-09-01 20:03:17 -0700133bool
Ian Romanickbfb09c22010-03-29 16:32:55 -0700134apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
Ian Romanick01045362010-03-29 16:17:56 -0700135 struct _mesa_glsl_parse_state *state)
136{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700137 void *ctx = state;
Ian Romanickbfb09c22010-03-29 16:32:55 -0700138 if (to->base_type == from->type->base_type)
Ian Romanick01045362010-03-29 16:17:56 -0700139 return true;
140
141 /* This conversion was added in GLSL 1.20. If the compilation mode is
142 * GLSL 1.10, the conversion is skipped.
143 */
Paul Berrye3ded7f2012-08-01 14:50:05 -0700144 if (!state->is_version(120, 0))
Ian Romanick01045362010-03-29 16:17:56 -0700145 return false;
146
147 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
148 *
149 * "There are no implicit array or structure conversions. For
150 * example, an array of int cannot be implicitly converted to an
151 * array of float. There are no implicit conversions between
152 * signed and unsigned integers."
153 */
154 /* FINISHME: The above comment is partially a lie. There is int/uint
155 * FINISHME: conversion for immediate constants.
156 */
Ian Romanickbfb09c22010-03-29 16:32:55 -0700157 if (!to->is_float() || !from->type->is_numeric())
Ian Romanick01045362010-03-29 16:17:56 -0700158 return false;
159
Kenneth Graunke506199b2010-06-29 15:59:27 -0700160 /* Convert to a floating point type with the same number of components
161 * as the original type - i.e. int to float, not int to vec4.
162 */
163 to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements,
164 from->type->matrix_columns);
165
Ian Romanickbfb09c22010-03-29 16:32:55 -0700166 switch (from->type->base_type) {
Ian Romanick01045362010-03-29 16:17:56 -0700167 case GLSL_TYPE_INT:
Carl Worth1660a292010-06-23 18:11:51 -0700168 from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL);
Ian Romanick01045362010-03-29 16:17:56 -0700169 break;
170 case GLSL_TYPE_UINT:
Carl Worth1660a292010-06-23 18:11:51 -0700171 from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL);
Ian Romanick01045362010-03-29 16:17:56 -0700172 break;
173 case GLSL_TYPE_BOOL:
Carl Worth1660a292010-06-23 18:11:51 -0700174 from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL);
Eric Anholtdc58b3f2010-04-02 02:13:43 -1000175 break;
Ian Romanick01045362010-03-29 16:17:56 -0700176 default:
177 assert(0);
178 }
179
180 return true;
181}
182
183
Ian Romanicka87ac252010-02-22 13:19:34 -0800184static const struct glsl_type *
Ian Romanickbfb09c22010-03-29 16:32:55 -0700185arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
Ian Romanicka87ac252010-02-22 13:19:34 -0800186 bool multiply,
Eric Anholta13bb142010-03-31 16:38:11 -1000187 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800188{
Eric Anholt336b4ad2010-05-19 10:38:37 -0700189 const glsl_type *type_a = value_a->type;
190 const glsl_type *type_b = value_b->type;
Ian Romanick01045362010-03-29 16:17:56 -0700191
Ian Romanicka87ac252010-02-22 13:19:34 -0800192 /* From GLSL 1.50 spec, page 56:
193 *
194 * "The arithmetic binary operators add (+), subtract (-),
195 * multiply (*), and divide (/) operate on integer and
196 * floating-point scalars, vectors, and matrices."
197 */
Ian Romanick60b54d92010-03-24 17:08:13 -0700198 if (!type_a->is_numeric() || !type_b->is_numeric()) {
Eric Anholta13bb142010-03-31 16:38:11 -1000199 _mesa_glsl_error(loc, state,
Paul Berry4d7899f2013-07-25 19:56:43 -0700200 "operands to arithmetic operators must be numeric");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700201 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800202 }
203
204
205 /* "If one operand is floating-point based and the other is
206 * not, then the conversions from Section 4.1.10 "Implicit
207 * Conversions" are applied to the non-floating-point-based operand."
Ian Romanicka87ac252010-02-22 13:19:34 -0800208 */
Ian Romanick01045362010-03-29 16:17:56 -0700209 if (!apply_implicit_conversion(type_a, value_b, state)
210 && !apply_implicit_conversion(type_b, value_a, state)) {
Eric Anholta13bb142010-03-31 16:38:11 -1000211 _mesa_glsl_error(loc, state,
Paul Berry4d7899f2013-07-25 19:56:43 -0700212 "could not implicitly convert operands to "
Eric Anholta13bb142010-03-31 16:38:11 -1000213 "arithmetic operator");
Ian Romanick01045362010-03-29 16:17:56 -0700214 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800215 }
Eric Anholt336b4ad2010-05-19 10:38:37 -0700216 type_a = value_a->type;
217 type_b = value_b->type;
218
Ian Romanicka87ac252010-02-22 13:19:34 -0800219 /* "If the operands are integer types, they must both be signed or
220 * both be unsigned."
221 *
222 * From this rule and the preceeding conversion it can be inferred that
223 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
Ian Romanick60b54d92010-03-24 17:08:13 -0700224 * The is_numeric check above already filtered out the case where either
225 * type is not one of these, so now the base types need only be tested for
226 * equality.
Ian Romanicka87ac252010-02-22 13:19:34 -0800227 */
228 if (type_a->base_type != type_b->base_type) {
Eric Anholta13bb142010-03-31 16:38:11 -1000229 _mesa_glsl_error(loc, state,
230 "base type mismatch for arithmetic operator");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700231 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800232 }
233
234 /* "All arithmetic binary operators result in the same fundamental type
235 * (signed integer, unsigned integer, or floating-point) as the
236 * operands they operate on, after operand type conversion. After
237 * conversion, the following cases are valid
238 *
239 * * The two operands are scalars. In this case the operation is
240 * applied, resulting in a scalar."
241 */
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800242 if (type_a->is_scalar() && type_b->is_scalar())
Ian Romanicka87ac252010-02-22 13:19:34 -0800243 return type_a;
244
245 /* "* One operand is a scalar, and the other is a vector or matrix.
246 * In this case, the scalar operation is applied independently to each
247 * component of the vector or matrix, resulting in the same size
248 * vector or matrix."
249 */
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800250 if (type_a->is_scalar()) {
251 if (!type_b->is_scalar())
Ian Romanicka87ac252010-02-22 13:19:34 -0800252 return type_b;
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800253 } else if (type_b->is_scalar()) {
Ian Romanicka87ac252010-02-22 13:19:34 -0800254 return type_a;
255 }
256
257 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
258 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
259 * handled.
260 */
Ian Romanick60b54d92010-03-24 17:08:13 -0700261 assert(!type_a->is_scalar());
262 assert(!type_b->is_scalar());
Ian Romanicka87ac252010-02-22 13:19:34 -0800263
264 /* "* The two operands are vectors of the same size. In this case, the
265 * operation is done component-wise resulting in the same size
266 * vector."
267 */
Ian Romanicka2dd22f2010-03-09 15:55:16 -0800268 if (type_a->is_vector() && type_b->is_vector()) {
Eric Anholta13bb142010-03-31 16:38:11 -1000269 if (type_a == type_b) {
270 return type_a;
271 } else {
272 _mesa_glsl_error(loc, state,
273 "vector size mismatch for arithmetic operator");
274 return glsl_type::error_type;
275 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800276 }
277
278 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
279 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
280 * <vector, vector> have been handled. At least one of the operands must
281 * be matrix. Further, since there are no integer matrix types, the base
282 * type of both operands must be float.
283 */
Ian Romanick60b54d92010-03-24 17:08:13 -0700284 assert(type_a->is_matrix() || type_b->is_matrix());
Ian Romanicka87ac252010-02-22 13:19:34 -0800285 assert(type_a->base_type == GLSL_TYPE_FLOAT);
286 assert(type_b->base_type == GLSL_TYPE_FLOAT);
287
288 /* "* The operator is add (+), subtract (-), or divide (/), and the
289 * operands are matrices with the same number of rows and the same
290 * number of columns. In this case, the operation is done component-
291 * wise resulting in the same size matrix."
292 * * The operator is multiply (*), where both operands are matrices or
293 * one operand is a vector and the other a matrix. A right vector
294 * operand is treated as a column vector and a left vector operand as a
295 * row vector. In all these cases, it is required that the number of
296 * columns of the left operand is equal to the number of rows of the
297 * right operand. Then, the multiply (*) operation does a linear
298 * algebraic multiply, yielding an object that has the same number of
299 * rows as the left operand and the same number of columns as the right
300 * operand. Section 5.10 "Vector and Matrix Operations" explains in
301 * more detail how vectors and matrices are operated on."
302 */
303 if (! multiply) {
Eric Anholta13bb142010-03-31 16:38:11 -1000304 if (type_a == type_b)
305 return type_a;
Ian Romanicka87ac252010-02-22 13:19:34 -0800306 } else {
Ian Romanickfce11502010-03-09 15:58:52 -0800307 if (type_a->is_matrix() && type_b->is_matrix()) {
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700308 /* Matrix multiply. The columns of A must match the rows of B. Given
309 * the other previously tested constraints, this means the vector type
310 * of a row from A must be the same as the vector type of a column from
311 * B.
312 */
313 if (type_a->row_type() == type_b->column_type()) {
314 /* The resulting matrix has the number of columns of matrix B and
315 * the number of rows of matrix A. We get the row count of A by
316 * looking at the size of a vector that makes up a column. The
317 * transpose (size of a row) is done for B.
318 */
Eric Anholta13bb142010-03-31 16:38:11 -1000319 const glsl_type *const type =
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700320 glsl_type::get_instance(type_a->base_type,
321 type_a->column_type()->vector_elements,
322 type_b->row_type()->vector_elements);
Eric Anholta13bb142010-03-31 16:38:11 -1000323 assert(type != glsl_type::error_type);
324
325 return type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800326 }
Ian Romanickfce11502010-03-09 15:58:52 -0800327 } else if (type_a->is_matrix()) {
Ian Romanicka87ac252010-02-22 13:19:34 -0800328 /* A is a matrix and B is a column vector. Columns of A must match
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700329 * rows of B. Given the other previously tested constraints, this
330 * means the vector type of a row from A must be the same as the
331 * vector the type of B.
Ian Romanicka87ac252010-02-22 13:19:34 -0800332 */
Carl Worth47c90b12010-07-22 14:56:14 -0700333 if (type_a->row_type() == type_b) {
334 /* The resulting vector has a number of elements equal to
335 * the number of rows of matrix A. */
336 const glsl_type *const type =
337 glsl_type::get_instance(type_a->base_type,
338 type_a->column_type()->vector_elements,
339 1);
340 assert(type != glsl_type::error_type);
341
342 return type;
343 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800344 } else {
Ian Romanickfce11502010-03-09 15:58:52 -0800345 assert(type_b->is_matrix());
Ian Romanicka87ac252010-02-22 13:19:34 -0800346
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700347 /* A is a row vector and B is a matrix. Columns of A must match rows
348 * of B. Given the other previously tested constraints, this means
349 * the type of A must be the same as the vector type of a column from
350 * B.
Ian Romanicka87ac252010-02-22 13:19:34 -0800351 */
Carl Worth47c90b12010-07-22 14:56:14 -0700352 if (type_a == type_b->column_type()) {
353 /* The resulting vector has a number of elements equal to
354 * the number of columns of matrix B. */
355 const glsl_type *const type =
356 glsl_type::get_instance(type_a->base_type,
357 type_b->row_type()->vector_elements,
358 1);
359 assert(type != glsl_type::error_type);
360
361 return type;
362 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800363 }
Eric Anholta13bb142010-03-31 16:38:11 -1000364
365 _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
366 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800367 }
368
369
370 /* "All other cases are illegal."
371 */
Eric Anholta13bb142010-03-31 16:38:11 -1000372 _mesa_glsl_error(loc, state, "type mismatch");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700373 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800374}
375
376
377static const struct glsl_type *
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000378unary_arithmetic_result_type(const struct glsl_type *type,
379 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800380{
381 /* From GLSL 1.50 spec, page 57:
382 *
383 * "The arithmetic unary operators negate (-), post- and pre-increment
384 * and decrement (-- and ++) operate on integer or floating-point
385 * values (including vectors and matrices). All unary operators work
386 * component-wise on their operands. These result with the same type
387 * they operated on."
388 */
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000389 if (!type->is_numeric()) {
390 _mesa_glsl_error(loc, state,
Paul Berry4d7899f2013-07-25 19:56:43 -0700391 "operands to arithmetic operators must be numeric");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700392 return glsl_type::error_type;
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000393 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800394
395 return type;
396}
397
Chad Versacecfdbf8b2010-10-15 11:28:05 -0700398/**
399 * \brief Return the result type of a bit-logic operation.
400 *
401 * If the given types to the bit-logic operator are invalid, return
402 * glsl_type::error_type.
403 *
404 * \param type_a Type of LHS of bit-logic op
405 * \param type_b Type of RHS of bit-logic op
406 */
407static const struct glsl_type *
408bit_logic_result_type(const struct glsl_type *type_a,
409 const struct glsl_type *type_b,
410 ast_operators op,
411 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
412{
Paul Berry0d9bba62012-08-05 09:57:01 -0700413 if (!state->check_bitwise_operations_allowed(loc)) {
Chad Versacecfdbf8b2010-10-15 11:28:05 -0700414 return glsl_type::error_type;
415 }
416
417 /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
418 *
419 * "The bitwise operators and (&), exclusive-or (^), and inclusive-or
420 * (|). The operands must be of type signed or unsigned integers or
421 * integer vectors."
422 */
423 if (!type_a->is_integer()) {
424 _mesa_glsl_error(loc, state, "LHS of `%s' must be an integer",
425 ast_expression::operator_string(op));
426 return glsl_type::error_type;
427 }
428 if (!type_b->is_integer()) {
429 _mesa_glsl_error(loc, state, "RHS of `%s' must be an integer",
430 ast_expression::operator_string(op));
431 return glsl_type::error_type;
432 }
433
434 /* "The fundamental types of the operands (signed or unsigned) must
435 * match,"
436 */
437 if (type_a->base_type != type_b->base_type) {
438 _mesa_glsl_error(loc, state, "operands of `%s' must have the same "
439 "base type", ast_expression::operator_string(op));
440 return glsl_type::error_type;
441 }
442
443 /* "The operands cannot be vectors of differing size." */
444 if (type_a->is_vector() &&
445 type_b->is_vector() &&
446 type_a->vector_elements != type_b->vector_elements) {
447 _mesa_glsl_error(loc, state, "operands of `%s' cannot be vectors of "
448 "different sizes", ast_expression::operator_string(op));
449 return glsl_type::error_type;
450 }
451
452 /* "If one operand is a scalar and the other a vector, the scalar is
453 * applied component-wise to the vector, resulting in the same type as
454 * the vector. The fundamental types of the operands [...] will be the
455 * resulting fundamental type."
456 */
457 if (type_a->is_scalar())
458 return type_b;
459 else
460 return type_a;
461}
Ian Romanicka87ac252010-02-22 13:19:34 -0800462
463static const struct glsl_type *
464modulus_result_type(const struct glsl_type *type_a,
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000465 const struct glsl_type *type_b,
466 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800467{
Paul Berryd4a24742012-08-02 08:18:12 -0700468 if (!state->check_version(130, 300, loc, "operator '%%' is reserved")) {
Chad Versace82f994f2011-02-04 12:18:56 -0800469 return glsl_type::error_type;
470 }
471
Ian Romanicka87ac252010-02-22 13:19:34 -0800472 /* From GLSL 1.50 spec, page 56:
473 * "The operator modulus (%) operates on signed or unsigned integers or
474 * integer vectors. The operand types must both be signed or both be
475 * unsigned."
476 */
Kenneth Graunke8eb97532011-06-14 22:21:41 -0700477 if (!type_a->is_integer()) {
Paul Berry4d7899f2013-07-25 19:56:43 -0700478 _mesa_glsl_error(loc, state, "LHS of operator %% must be an integer");
Kenneth Graunke8eb97532011-06-14 22:21:41 -0700479 return glsl_type::error_type;
480 }
481 if (!type_b->is_integer()) {
Paul Berry4d7899f2013-07-25 19:56:43 -0700482 _mesa_glsl_error(loc, state, "RHS of operator %% must be an integer");
Kenneth Graunke8eb97532011-06-14 22:21:41 -0700483 return glsl_type::error_type;
484 }
485 if (type_a->base_type != type_b->base_type) {
486 _mesa_glsl_error(loc, state,
487 "operands of %% must have the same base type");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700488 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800489 }
490
491 /* "The operands cannot be vectors of differing size. If one operand is
492 * a scalar and the other vector, then the scalar is applied component-
493 * wise to the vector, resulting in the same type as the vector. If both
494 * are vectors of the same size, the result is computed component-wise."
495 */
Ian Romanicka2dd22f2010-03-09 15:55:16 -0800496 if (type_a->is_vector()) {
497 if (!type_b->is_vector()
Ian Romanicka87ac252010-02-22 13:19:34 -0800498 || (type_a->vector_elements == type_b->vector_elements))
499 return type_a;
500 } else
501 return type_b;
502
503 /* "The operator modulus (%) is not defined for any other data types
504 * (non-integer types)."
505 */
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000506 _mesa_glsl_error(loc, state, "type mismatch");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700507 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800508}
509
510
511static const struct glsl_type *
Ian Romanickbfb09c22010-03-29 16:32:55 -0700512relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000513 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800514{
Eric Anholt336b4ad2010-05-19 10:38:37 -0700515 const glsl_type *type_a = value_a->type;
516 const glsl_type *type_b = value_b->type;
Ian Romanick0150f5f2010-03-29 16:20:07 -0700517
Ian Romanicka87ac252010-02-22 13:19:34 -0800518 /* From GLSL 1.50 spec, page 56:
519 * "The relational operators greater than (>), less than (<), greater
520 * than or equal (>=), and less than or equal (<=) operate only on
521 * scalar integer and scalar floating-point expressions."
522 */
Ian Romanicka6d653d2010-03-26 14:40:37 -0700523 if (!type_a->is_numeric()
524 || !type_b->is_numeric()
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800525 || !type_a->is_scalar()
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000526 || !type_b->is_scalar()) {
527 _mesa_glsl_error(loc, state,
Paul Berry4d7899f2013-07-25 19:56:43 -0700528 "operands to relational operators must be scalar and "
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000529 "numeric");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700530 return glsl_type::error_type;
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000531 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800532
533 /* "Either the operands' types must match, or the conversions from
534 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
535 * operand, after which the types must match."
Ian Romanicka87ac252010-02-22 13:19:34 -0800536 */
Ian Romanick0150f5f2010-03-29 16:20:07 -0700537 if (!apply_implicit_conversion(type_a, value_b, state)
538 && !apply_implicit_conversion(type_b, value_a, state)) {
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000539 _mesa_glsl_error(loc, state,
Paul Berry4d7899f2013-07-25 19:56:43 -0700540 "could not implicitly convert operands to "
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000541 "relational operator");
Ian Romanick0150f5f2010-03-29 16:20:07 -0700542 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800543 }
Eric Anholt336b4ad2010-05-19 10:38:37 -0700544 type_a = value_a->type;
545 type_b = value_b->type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800546
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000547 if (type_a->base_type != type_b->base_type) {
548 _mesa_glsl_error(loc, state, "base type mismatch");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700549 return glsl_type::error_type;
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000550 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800551
552 /* "The result is scalar Boolean."
553 */
Ian Romanick0471e8b2010-03-26 14:33:41 -0700554 return glsl_type::bool_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800555}
556
Chad Versacec0197ab2010-10-15 09:49:46 -0700557/**
558 * \brief Return the result type of a bit-shift operation.
559 *
560 * If the given types to the bit-shift operator are invalid, return
561 * glsl_type::error_type.
562 *
563 * \param type_a Type of LHS of bit-shift op
564 * \param type_b Type of RHS of bit-shift op
565 */
566static const struct glsl_type *
567shift_result_type(const struct glsl_type *type_a,
568 const struct glsl_type *type_b,
569 ast_operators op,
570 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
571{
Paul Berry0d9bba62012-08-05 09:57:01 -0700572 if (!state->check_bitwise_operations_allowed(loc)) {
Chad Versacec0197ab2010-10-15 09:49:46 -0700573 return glsl_type::error_type;
574 }
575
576 /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
577 *
578 * "The shift operators (<<) and (>>). For both operators, the operands
579 * must be signed or unsigned integers or integer vectors. One operand
580 * can be signed while the other is unsigned."
581 */
582 if (!type_a->is_integer()) {
583 _mesa_glsl_error(loc, state, "LHS of operator %s must be an integer or "
584 "integer vector", ast_expression::operator_string(op));
585 return glsl_type::error_type;
586
587 }
588 if (!type_b->is_integer()) {
589 _mesa_glsl_error(loc, state, "RHS of operator %s must be an integer or "
590 "integer vector", ast_expression::operator_string(op));
591 return glsl_type::error_type;
592 }
593
594 /* "If the first operand is a scalar, the second operand has to be
595 * a scalar as well."
596 */
597 if (type_a->is_scalar() && !type_b->is_scalar()) {
Paul Berry4d7899f2013-07-25 19:56:43 -0700598 _mesa_glsl_error(loc, state, "if the first operand of %s is scalar, the "
Chad Versacec0197ab2010-10-15 09:49:46 -0700599 "second must be scalar as well",
600 ast_expression::operator_string(op));
601 return glsl_type::error_type;
602 }
603
604 /* If both operands are vectors, check that they have same number of
605 * elements.
606 */
607 if (type_a->is_vector() &&
608 type_b->is_vector() &&
609 type_a->vector_elements != type_b->vector_elements) {
Paul Berry4d7899f2013-07-25 19:56:43 -0700610 _mesa_glsl_error(loc, state, "vector operands to operator %s must "
Chad Versacec0197ab2010-10-15 09:49:46 -0700611 "have same number of elements",
612 ast_expression::operator_string(op));
613 return glsl_type::error_type;
614 }
615
616 /* "In all cases, the resulting type will be the same type as the left
617 * operand."
618 */
619 return type_a;
620}
Ian Romanicka87ac252010-02-22 13:19:34 -0800621
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700622/**
623 * Validates that a value can be assigned to a location with a specified type
624 *
625 * Validates that \c rhs can be assigned to some location. If the types are
626 * not an exact match but an automatic conversion is possible, \c rhs will be
627 * converted.
628 *
629 * \return
630 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
631 * Otherwise the actual RHS to be assigned will be returned. This may be
632 * \c rhs, or it may be \c rhs after some type conversion.
633 *
634 * \note
635 * In addition to being used for assignments, this function is used to
636 * type-check return values.
637 */
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700638ir_rvalue *
Eric Anholt336b4ad2010-05-19 10:38:37 -0700639validate_assignment(struct _mesa_glsl_parse_state *state,
Ian Romanick85caea22011-03-15 16:33:27 -0700640 const glsl_type *lhs_type, ir_rvalue *rhs,
641 bool is_initializer)
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700642{
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700643 /* If there is already some error in the RHS, just return it. Anything
644 * else will lead to an avalanche of error message back to the user.
645 */
Ian Romanickec530102010-12-10 15:47:11 -0800646 if (rhs->type->is_error())
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700647 return rhs;
648
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700649 /* If the types are identical, the assignment can trivially proceed.
650 */
Ian Romanickec530102010-12-10 15:47:11 -0800651 if (rhs->type == lhs_type)
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700652 return rhs;
653
Ian Romanick0157f412010-04-02 17:44:39 -0700654 /* If the array element types are the same and the size of the LHS is zero,
Ian Romanick85caea22011-03-15 16:33:27 -0700655 * the assignment is okay for initializers embedded in variable
656 * declarations.
Ian Romanick0157f412010-04-02 17:44:39 -0700657 *
658 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
659 * is handled by ir_dereference::is_lvalue.
660 */
Ian Romanick85caea22011-03-15 16:33:27 -0700661 if (is_initializer && lhs_type->is_array() && rhs->type->is_array()
Ian Romanick0157f412010-04-02 17:44:39 -0700662 && (lhs_type->element_type() == rhs->type->element_type())
663 && (lhs_type->array_size() == 0)) {
664 return rhs;
665 }
666
Eric Anholt336b4ad2010-05-19 10:38:37 -0700667 /* Check for implicit conversion in GLSL 1.20 */
668 if (apply_implicit_conversion(lhs_type, rhs, state)) {
Ian Romanickec530102010-12-10 15:47:11 -0800669 if (rhs->type == lhs_type)
Eric Anholt336b4ad2010-05-19 10:38:37 -0700670 return rhs;
671 }
672
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700673 return NULL;
674}
675
Eric Anholta313c292011-08-05 21:40:50 -0700676static void
677mark_whole_array_access(ir_rvalue *access)
678{
679 ir_dereference_variable *deref = access->as_dereference_variable();
680
681 if (deref && deref->var) {
682 deref->var->max_array_access = deref->type->length - 1;
683 }
684}
685
Eric Anholt10a68522010-03-26 11:53:37 -0700686ir_rvalue *
687do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
Ian Romanicke9015e92011-12-23 09:56:29 -0800688 const char *non_lvalue_description,
Ian Romanick85caea22011-03-15 16:33:27 -0700689 ir_rvalue *lhs, ir_rvalue *rhs, bool is_initializer,
Eric Anholt10a68522010-03-26 11:53:37 -0700690 YYLTYPE lhs_loc)
691{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700692 void *ctx = state;
Eric Anholt10a68522010-03-26 11:53:37 -0700693 bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
694
Ian Romanick89704eb2013-03-15 18:06:11 -0700695 /* If the assignment LHS comes back as an ir_binop_vector_extract
696 * expression, move it to the RHS as an ir_triop_vector_insert.
697 */
698 if (lhs->ir_type == ir_type_expression) {
699 ir_expression *const expr = lhs->as_expression();
700
701 if (unlikely(expr->operation == ir_binop_vector_extract)) {
702 ir_rvalue *new_rhs =
703 validate_assignment(state, lhs->type, rhs, is_initializer);
704
705 if (new_rhs == NULL) {
706 _mesa_glsl_error(& lhs_loc, state, "type mismatch");
707 return lhs;
708 } else {
709 rhs = new(ctx) ir_expression(ir_triop_vector_insert,
710 expr->operands[0]->type,
711 expr->operands[0],
712 new_rhs,
713 expr->operands[1]);
714 lhs = expr->operands[0]->clone(ctx, NULL);
715 }
716 }
717 }
718
Eric Anholtf2475ca2012-03-29 17:02:15 -0700719 ir_variable *lhs_var = lhs->variable_referenced();
720 if (lhs_var)
721 lhs_var->assigned = true;
722
Eric Anholt10a68522010-03-26 11:53:37 -0700723 if (!error_emitted) {
Ian Romanicke9015e92011-12-23 09:56:29 -0800724 if (non_lvalue_description != NULL) {
725 _mesa_glsl_error(&lhs_loc, state,
726 "assignment to %s",
727 non_lvalue_description);
728 error_emitted = true;
729 } else if (lhs->variable_referenced() != NULL
730 && lhs->variable_referenced()->read_only) {
Chad Versaceb66be752011-01-21 13:44:08 -0800731 _mesa_glsl_error(&lhs_loc, state,
732 "assignment to read-only variable '%s'",
733 lhs->variable_referenced()->name);
734 error_emitted = true;
735
Paul Berry0d9bba62012-08-05 09:57:01 -0700736 } else if (lhs->type->is_array() &&
Paul Berryd4a24742012-08-02 08:18:12 -0700737 !state->check_version(120, 300, &lhs_loc,
Paul Berry0d9bba62012-08-05 09:57:01 -0700738 "whole array assignment forbidden")) {
Eric Anholt525cec92011-09-07 12:03:36 -0700739 /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
740 *
741 * "Other binary or unary expressions, non-dereferenced
742 * arrays, function names, swizzles with repeated fields,
743 * and constants cannot be l-values."
Paul Berryd4a24742012-08-02 08:18:12 -0700744 *
745 * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00.
Eric Anholt525cec92011-09-07 12:03:36 -0700746 */
Eric Anholt525cec92011-09-07 12:03:36 -0700747 error_emitted = true;
Chad Versaceb66be752011-01-21 13:44:08 -0800748 } else if (!lhs->is_lvalue()) {
Eric Anholt10a68522010-03-26 11:53:37 -0700749 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
750 error_emitted = true;
751 }
752 }
753
Ian Romanick85caea22011-03-15 16:33:27 -0700754 ir_rvalue *new_rhs =
755 validate_assignment(state, lhs->type, rhs, is_initializer);
Eric Anholt10a68522010-03-26 11:53:37 -0700756 if (new_rhs == NULL) {
757 _mesa_glsl_error(& lhs_loc, state, "type mismatch");
758 } else {
759 rhs = new_rhs;
Ian Romanick0157f412010-04-02 17:44:39 -0700760
761 /* If the LHS array was not declared with a size, it takes it size from
762 * the RHS. If the LHS is an l-value and a whole array, it must be a
763 * dereference of a variable. Any other case would require that the LHS
764 * is either not an l-value or not a whole array.
765 */
766 if (lhs->type->array_size() == 0) {
767 ir_dereference *const d = lhs->as_dereference();
768
769 assert(d != NULL);
770
Ian Romanick36ea2862010-05-19 13:52:29 +0200771 ir_variable *const var = d->variable_referenced();
Ian Romanick0157f412010-04-02 17:44:39 -0700772
773 assert(var != NULL);
774
Ian Romanick63f39422010-04-05 14:35:47 -0700775 if (var->max_array_access >= unsigned(rhs->type->array_size())) {
776 /* FINISHME: This should actually log the location of the RHS. */
777 _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
778 "previous access",
779 var->max_array_access);
780 }
781
Ian Romanickf38d15b2010-07-20 15:33:40 -0700782 var->type = glsl_type::get_array_instance(lhs->type->element_type(),
Ian Romanick0157f412010-04-02 17:44:39 -0700783 rhs->type->array_size());
Eric Anholt9703ed02010-07-22 15:50:37 -0700784 d->type = var->type;
Ian Romanick0157f412010-04-02 17:44:39 -0700785 }
Eric Anholt407a1002011-09-07 11:53:20 -0700786 mark_whole_array_access(rhs);
Eric Anholta313c292011-08-05 21:40:50 -0700787 mark_whole_array_access(lhs);
Eric Anholt10a68522010-03-26 11:53:37 -0700788 }
789
Eric Anholt2731a732010-06-23 14:51:14 -0700790 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
791 * but not post_inc) need the converted assigned value as an rvalue
792 * to handle things like:
793 *
794 * i = j += 1;
795 *
796 * So we always just store the computed value being assigned to a
797 * temporary and return a deref of that temporary. If the rvalue
798 * ends up not being used, the temp will get copy-propagated out.
799 */
Ian Romanick7e2aa912010-07-19 17:12:42 -0700800 ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp",
801 ir_var_temporary);
Eric Anholte33c1032010-06-24 15:13:03 -0700802 ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var);
Eric Anholtae805922010-06-24 09:06:12 -0700803 instructions->push_tail(var);
Eric Anholtaa5ec132012-05-14 09:14:54 -0700804 instructions->push_tail(new(ctx) ir_assignment(deref_var, rhs));
Eric Anholte33c1032010-06-24 15:13:03 -0700805 deref_var = new(ctx) ir_dereference_variable(var);
Eric Anholt10a68522010-03-26 11:53:37 -0700806
Ian Romanick8e9ce2e2010-08-03 15:02:35 -0700807 if (!error_emitted)
Eric Anholtaa5ec132012-05-14 09:14:54 -0700808 instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var));
Eric Anholt2731a732010-06-23 14:51:14 -0700809
Carl Worth1660a292010-06-23 18:11:51 -0700810 return new(ctx) ir_dereference_variable(var);
Eric Anholt10a68522010-03-26 11:53:37 -0700811}
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700812
Eric Anholtde38f0e2010-03-26 12:14:54 -0700813static ir_rvalue *
Eric Anholt959a9ec2010-06-23 14:43:50 -0700814get_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
Eric Anholtde38f0e2010-03-26 12:14:54 -0700815{
Kenneth Graunked3073f52011-01-21 14:32:31 -0800816 void *ctx = ralloc_parent(lvalue);
Eric Anholtde38f0e2010-03-26 12:14:54 -0700817 ir_variable *var;
Eric Anholtde38f0e2010-03-26 12:14:54 -0700818
Ian Romanick7e2aa912010-07-19 17:12:42 -0700819 var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
820 ir_var_temporary);
Eric Anholt43b5b032010-07-07 14:04:30 -0700821 instructions->push_tail(var);
Eric Anholtde38f0e2010-03-26 12:14:54 -0700822 var->mode = ir_var_auto;
823
Carl Worth1660a292010-06-23 18:11:51 -0700824 instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
Eric Anholtaa5ec132012-05-14 09:14:54 -0700825 lvalue));
Eric Anholtde38f0e2010-03-26 12:14:54 -0700826
Carl Worth1660a292010-06-23 18:11:51 -0700827 return new(ctx) ir_dereference_variable(var);
Eric Anholtde38f0e2010-03-26 12:14:54 -0700828}
829
830
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700831ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -0800832ast_node::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -0800833 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -0800834{
Ian Romanick18238de2010-03-01 13:49:10 -0800835 (void) instructions;
836 (void) state;
837
838 return NULL;
839}
840
Eric Anholtff796332010-11-30 11:23:28 -0800841static ir_rvalue *
842do_comparison(void *mem_ctx, int operation, ir_rvalue *op0, ir_rvalue *op1)
843{
844 int join_op;
Ian Romanick6d36be52010-12-02 12:17:36 -0800845 ir_rvalue *cmp = NULL;
Eric Anholtff796332010-11-30 11:23:28 -0800846
847 if (operation == ir_binop_all_equal)
848 join_op = ir_binop_logic_and;
849 else
850 join_op = ir_binop_logic_or;
851
852 switch (op0->type->base_type) {
853 case GLSL_TYPE_FLOAT:
854 case GLSL_TYPE_UINT:
855 case GLSL_TYPE_INT:
856 case GLSL_TYPE_BOOL:
857 return new(mem_ctx) ir_expression(operation, op0, op1);
858
859 case GLSL_TYPE_ARRAY: {
Eric Anholtff796332010-11-30 11:23:28 -0800860 for (unsigned int i = 0; i < op0->type->length; i++) {
861 ir_rvalue *e0, *e1, *result;
862
863 e0 = new(mem_ctx) ir_dereference_array(op0->clone(mem_ctx, NULL),
864 new(mem_ctx) ir_constant(i));
865 e1 = new(mem_ctx) ir_dereference_array(op1->clone(mem_ctx, NULL),
866 new(mem_ctx) ir_constant(i));
867 result = do_comparison(mem_ctx, operation, e0, e1);
868
Ian Romanick6d36be52010-12-02 12:17:36 -0800869 if (cmp) {
870 cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
Eric Anholtff796332010-11-30 11:23:28 -0800871 } else {
Ian Romanick6d36be52010-12-02 12:17:36 -0800872 cmp = result;
Eric Anholtff796332010-11-30 11:23:28 -0800873 }
874 }
Eric Anholtb4f58562010-12-01 15:55:53 -0800875
876 mark_whole_array_access(op0);
877 mark_whole_array_access(op1);
Ian Romanick6d36be52010-12-02 12:17:36 -0800878 break;
Eric Anholtff796332010-11-30 11:23:28 -0800879 }
880
881 case GLSL_TYPE_STRUCT: {
Eric Anholtff796332010-11-30 11:23:28 -0800882 for (unsigned int i = 0; i < op0->type->length; i++) {
883 ir_rvalue *e0, *e1, *result;
884 const char *field_name = op0->type->fields.structure[i].name;
885
886 e0 = new(mem_ctx) ir_dereference_record(op0->clone(mem_ctx, NULL),
887 field_name);
888 e1 = new(mem_ctx) ir_dereference_record(op1->clone(mem_ctx, NULL),
889 field_name);
890 result = do_comparison(mem_ctx, operation, e0, e1);
891
Ian Romanick6d36be52010-12-02 12:17:36 -0800892 if (cmp) {
893 cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
Eric Anholtff796332010-11-30 11:23:28 -0800894 } else {
Ian Romanick6d36be52010-12-02 12:17:36 -0800895 cmp = result;
Eric Anholtff796332010-11-30 11:23:28 -0800896 }
897 }
Ian Romanick6d36be52010-12-02 12:17:36 -0800898 break;
Eric Anholtff796332010-11-30 11:23:28 -0800899 }
900
901 case GLSL_TYPE_ERROR:
902 case GLSL_TYPE_VOID:
903 case GLSL_TYPE_SAMPLER:
Ian Romanick491364e2012-12-11 12:11:16 -0800904 case GLSL_TYPE_INTERFACE:
Eric Anholtff796332010-11-30 11:23:28 -0800905 /* I assume a comparison of a struct containing a sampler just
906 * ignores the sampler present in the type.
907 */
Ian Romanick6d36be52010-12-02 12:17:36 -0800908 break;
Eric Anholtff796332010-11-30 11:23:28 -0800909 }
Eric Anholtd56c9742010-11-30 13:28:47 -0800910
Ian Romanick6d36be52010-12-02 12:17:36 -0800911 if (cmp == NULL)
912 cmp = new(mem_ctx) ir_constant(true);
913
914 return cmp;
Eric Anholtff796332010-11-30 11:23:28 -0800915}
Ian Romanick18238de2010-03-01 13:49:10 -0800916
Eric Anholt01822702011-04-09 15:59:20 -1000917/* For logical operations, we want to ensure that the operands are
918 * scalar booleans. If it isn't, emit an error and return a constant
919 * boolean to avoid triggering cascading error messages.
920 */
921ir_rvalue *
922get_scalar_boolean_operand(exec_list *instructions,
923 struct _mesa_glsl_parse_state *state,
924 ast_expression *parent_expr,
925 int operand,
926 const char *operand_name,
927 bool *error_emitted)
928{
929 ast_expression *expr = parent_expr->subexpressions[operand];
930 void *ctx = state;
931 ir_rvalue *val = expr->hir(instructions, state);
932
933 if (val->type->is_boolean() && val->type->is_scalar())
934 return val;
935
936 if (!*error_emitted) {
937 YYLTYPE loc = expr->get_location();
938 _mesa_glsl_error(&loc, state, "%s of `%s' must be scalar boolean",
939 operand_name,
940 parent_expr->operator_string(parent_expr->oper));
941 *error_emitted = true;
942 }
943
944 return new(ctx) ir_constant(true);
945}
946
Paul Berry93b97582011-09-06 10:01:51 -0700947/**
948 * If name refers to a builtin array whose maximum allowed size is less than
949 * size, report an error and return true. Otherwise return false.
950 */
Ian Romanick2c333a82013-03-15 14:33:01 -0700951void
Paul Berry93b97582011-09-06 10:01:51 -0700952check_builtin_array_max_size(const char *name, unsigned size,
953 YYLTYPE loc, struct _mesa_glsl_parse_state *state)
954{
955 if ((strcmp("gl_TexCoord", name) == 0)
956 && (size > state->Const.MaxTextureCoords)) {
957 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
958 *
959 * "The size [of gl_TexCoord] can be at most
960 * gl_MaxTextureCoords."
961 */
962 _mesa_glsl_error(&loc, state, "`gl_TexCoord' array size cannot "
Paul Berry4d7899f2013-07-25 19:56:43 -0700963 "be larger than gl_MaxTextureCoords (%u)",
Paul Berry93b97582011-09-06 10:01:51 -0700964 state->Const.MaxTextureCoords);
Paul Berry37bb1c42011-08-11 15:23:33 -0700965 } else if (strcmp("gl_ClipDistance", name) == 0
966 && size > state->Const.MaxClipPlanes) {
967 /* From section 7.1 (Vertex Shader Special Variables) of the
968 * GLSL 1.30 spec:
969 *
970 * "The gl_ClipDistance array is predeclared as unsized and
971 * must be sized by the shader either redeclaring it with a
972 * size or indexing it only with integral constant
973 * expressions. ... The size can be at most
974 * gl_MaxClipDistances."
975 */
976 _mesa_glsl_error(&loc, state, "`gl_ClipDistance' array size cannot "
Paul Berry4d7899f2013-07-25 19:56:43 -0700977 "be larger than gl_MaxClipDistances (%u)",
Paul Berry37bb1c42011-08-11 15:23:33 -0700978 state->Const.MaxClipPlanes);
Paul Berry93b97582011-09-06 10:01:51 -0700979 }
Paul Berry93b97582011-09-06 10:01:51 -0700980}
981
Paul Berry9abda922011-10-31 18:22:48 -0700982/**
983 * Create the constant 1, of a which is appropriate for incrementing and
984 * decrementing values of the given GLSL type. For example, if type is vec4,
985 * this creates a constant value of 1.0 having type float.
986 *
987 * If the given type is invalid for increment and decrement operators, return
988 * a floating point 1--the error will be detected later.
989 */
990static ir_rvalue *
991constant_one_for_inc_dec(void *ctx, const glsl_type *type)
992{
993 switch (type->base_type) {
994 case GLSL_TYPE_UINT:
995 return new(ctx) ir_constant((unsigned) 1);
996 case GLSL_TYPE_INT:
997 return new(ctx) ir_constant(1);
998 default:
999 case GLSL_TYPE_FLOAT:
1000 return new(ctx) ir_constant(1.0f);
1001 }
1002}
1003
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001004ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08001005ast_expression::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08001006 struct _mesa_glsl_parse_state *state)
1007{
Kenneth Graunke953ff122010-06-25 13:14:37 -07001008 void *ctx = state;
Ian Romanicka87ac252010-02-22 13:19:34 -08001009 static const int operations[AST_NUM_OPERATORS] = {
1010 -1, /* ast_assign doesn't convert to ir_expression. */
1011 -1, /* ast_plus doesn't convert to ir_expression. */
1012 ir_unop_neg,
1013 ir_binop_add,
1014 ir_binop_sub,
1015 ir_binop_mul,
1016 ir_binop_div,
1017 ir_binop_mod,
1018 ir_binop_lshift,
1019 ir_binop_rshift,
1020 ir_binop_less,
1021 ir_binop_greater,
1022 ir_binop_lequal,
1023 ir_binop_gequal,
Luca Barbieri4dfb8992010-09-08 01:31:39 +02001024 ir_binop_all_equal,
1025 ir_binop_any_nequal,
Ian Romanicka87ac252010-02-22 13:19:34 -08001026 ir_binop_bit_and,
1027 ir_binop_bit_xor,
1028 ir_binop_bit_or,
1029 ir_unop_bit_not,
1030 ir_binop_logic_and,
1031 ir_binop_logic_xor,
1032 ir_binop_logic_or,
1033 ir_unop_logic_not,
1034
1035 /* Note: The following block of expression types actually convert
1036 * to multiple IR instructions.
1037 */
1038 ir_binop_mul, /* ast_mul_assign */
1039 ir_binop_div, /* ast_div_assign */
1040 ir_binop_mod, /* ast_mod_assign */
1041 ir_binop_add, /* ast_add_assign */
1042 ir_binop_sub, /* ast_sub_assign */
1043 ir_binop_lshift, /* ast_ls_assign */
1044 ir_binop_rshift, /* ast_rs_assign */
1045 ir_binop_bit_and, /* ast_and_assign */
1046 ir_binop_bit_xor, /* ast_xor_assign */
1047 ir_binop_bit_or, /* ast_or_assign */
1048
1049 -1, /* ast_conditional doesn't convert to ir_expression. */
Eric Anholtde38f0e2010-03-26 12:14:54 -07001050 ir_binop_add, /* ast_pre_inc. */
1051 ir_binop_sub, /* ast_pre_dec. */
1052 ir_binop_add, /* ast_post_inc. */
1053 ir_binop_sub, /* ast_post_dec. */
Ian Romanicka87ac252010-02-22 13:19:34 -08001054 -1, /* ast_field_selection doesn't conv to ir_expression. */
1055 -1, /* ast_array_index doesn't convert to ir_expression. */
1056 -1, /* ast_function_call doesn't conv to ir_expression. */
1057 -1, /* ast_identifier doesn't convert to ir_expression. */
1058 -1, /* ast_int_constant doesn't convert to ir_expression. */
1059 -1, /* ast_uint_constant doesn't conv to ir_expression. */
1060 -1, /* ast_float_constant doesn't conv to ir_expression. */
1061 -1, /* ast_bool_constant doesn't conv to ir_expression. */
1062 -1, /* ast_sequence doesn't convert to ir_expression. */
1063 };
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001064 ir_rvalue *result = NULL;
Aras Pranckevicius1c325af2010-07-29 15:35:22 +03001065 ir_rvalue *op[3];
Kenneth Graunke08ba9772011-04-14 17:21:59 -07001066 const struct glsl_type *type; /* a temporary variable for switch cases */
Ian Romanicka87ac252010-02-22 13:19:34 -08001067 bool error_emitted = false;
1068 YYLTYPE loc;
1069
Ian Romanick18238de2010-03-01 13:49:10 -08001070 loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08001071
Ian Romanick18238de2010-03-01 13:49:10 -08001072 switch (this->oper) {
Matt Turnerae79e862013-06-29 19:27:50 -07001073 case ast_aggregate:
1074 assert(!"ast_aggregate: Should never get here.");
1075 break;
1076
Ian Romanick6652af32010-03-09 16:38:02 -08001077 case ast_assign: {
Ian Romanick18238de2010-03-01 13:49:10 -08001078 op[0] = this->subexpressions[0]->hir(instructions, state);
1079 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001080
Ian Romanicke9015e92011-12-23 09:56:29 -08001081 result = do_assignment(instructions, state,
1082 this->subexpressions[0]->non_lvalue_description,
1083 op[0], op[1], false,
Eric Anholt10a68522010-03-26 11:53:37 -07001084 this->subexpressions[0]->get_location());
1085 error_emitted = result->type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -08001086 break;
Ian Romanick6652af32010-03-09 16:38:02 -08001087 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001088
1089 case ast_plus:
Ian Romanick18238de2010-03-01 13:49:10 -08001090 op[0] = this->subexpressions[0]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001091
Carl Worthc24bcad2010-07-21 11:23:51 -07001092 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
1093
1094 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -08001095
1096 result = op[0];
1097 break;
1098
1099 case ast_neg:
Ian Romanick18238de2010-03-01 13:49:10 -08001100 op[0] = this->subexpressions[0]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001101
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001102 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -08001103
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001104 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -08001105
Carl Worth1660a292010-06-23 18:11:51 -07001106 result = new(ctx) ir_expression(operations[this->oper], type,
1107 op[0], NULL);
Ian Romanicka87ac252010-02-22 13:19:34 -08001108 break;
1109
1110 case ast_add:
1111 case ast_sub:
1112 case ast_mul:
1113 case ast_div:
Ian Romanick18238de2010-03-01 13:49:10 -08001114 op[0] = this->subexpressions[0]->hir(instructions, state);
1115 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001116
Ian Romanickbfb09c22010-03-29 16:32:55 -07001117 type = arithmetic_result_type(op[0], op[1],
Ian Romanick18238de2010-03-01 13:49:10 -08001118 (this->oper == ast_mul),
Eric Anholta13bb142010-03-31 16:38:11 -10001119 state, & loc);
1120 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -08001121
Carl Worth1660a292010-06-23 18:11:51 -07001122 result = new(ctx) ir_expression(operations[this->oper], type,
1123 op[0], op[1]);
Ian Romanicka87ac252010-02-22 13:19:34 -08001124 break;
1125
1126 case ast_mod:
Ian Romanick18238de2010-03-01 13:49:10 -08001127 op[0] = this->subexpressions[0]->hir(instructions, state);
1128 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001129
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001130 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -08001131
Ian Romanick18238de2010-03-01 13:49:10 -08001132 assert(operations[this->oper] == ir_binop_mod);
Ian Romanicka87ac252010-02-22 13:19:34 -08001133
Carl Worth1660a292010-06-23 18:11:51 -07001134 result = new(ctx) ir_expression(operations[this->oper], type,
1135 op[0], op[1]);
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001136 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -08001137 break;
1138
1139 case ast_lshift:
1140 case ast_rshift:
Paul Berry0d9bba62012-08-05 09:57:01 -07001141 if (!state->check_bitwise_operations_allowed(&loc)) {
Chad Versace5c4c36f2010-10-08 16:22:28 -07001142 error_emitted = true;
Chad Versace5c4c36f2010-10-08 16:22:28 -07001143 }
1144
Chad Versace5c4c36f2010-10-08 16:22:28 -07001145 op[0] = this->subexpressions[0]->hir(instructions, state);
1146 op[1] = this->subexpressions[1]->hir(instructions, state);
Chad Versacec0197ab2010-10-15 09:49:46 -07001147 type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1148 &loc);
Chad Versace5c4c36f2010-10-08 16:22:28 -07001149 result = new(ctx) ir_expression(operations[this->oper], type,
1150 op[0], op[1]);
1151 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1152 break;
Ian Romanicka87ac252010-02-22 13:19:34 -08001153
1154 case ast_less:
1155 case ast_greater:
1156 case ast_lequal:
1157 case ast_gequal:
Ian Romanick18238de2010-03-01 13:49:10 -08001158 op[0] = this->subexpressions[0]->hir(instructions, state);
1159 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001160
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001161 type = relational_result_type(op[0], op[1], state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -08001162
1163 /* The relational operators must either generate an error or result
1164 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1165 */
Ian Romanicka43817a2010-03-26 14:27:23 -07001166 assert(type->is_error()
Ian Romanicka87ac252010-02-22 13:19:34 -08001167 || ((type->base_type == GLSL_TYPE_BOOL)
Ian Romanickcb36f8a2010-03-09 15:51:22 -08001168 && type->is_scalar()));
Ian Romanicka87ac252010-02-22 13:19:34 -08001169
Carl Worth1660a292010-06-23 18:11:51 -07001170 result = new(ctx) ir_expression(operations[this->oper], type,
1171 op[0], op[1]);
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001172 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -08001173 break;
1174
1175 case ast_nequal:
1176 case ast_equal:
Ian Romanick6e659ca2010-03-29 15:11:05 -07001177 op[0] = this->subexpressions[0]->hir(instructions, state);
1178 op[1] = this->subexpressions[1]->hir(instructions, state);
1179
1180 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1181 *
1182 * "The equality operators equal (==), and not equal (!=)
1183 * operate on all types. They result in a scalar Boolean. If
1184 * the operand types do not match, then there must be a
1185 * conversion from Section 4.1.10 "Implicit Conversions"
1186 * applied to one operand that can make them match, in which
1187 * case this conversion is done."
1188 */
Ian Romanickbfb09c22010-03-29 16:32:55 -07001189 if ((!apply_implicit_conversion(op[0]->type, op[1], state)
1190 && !apply_implicit_conversion(op[1]->type, op[0], state))
Ian Romanick212b0322010-03-29 16:22:38 -07001191 || (op[0]->type != op[1]->type)) {
Ian Romanick6e659ca2010-03-29 15:11:05 -07001192 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
1193 "type", (this->oper == ast_equal) ? "==" : "!=");
1194 error_emitted = true;
Paul Berry0d9bba62012-08-05 09:57:01 -07001195 } else if ((op[0]->type->is_array() || op[1]->type->is_array()) &&
Paul Berryd4a24742012-08-02 08:18:12 -07001196 !state->check_version(120, 300, &loc,
Paul Berry0d9bba62012-08-05 09:57:01 -07001197 "array comparisons forbidden")) {
Ian Romanicka80cbd62010-03-30 17:04:48 -07001198 error_emitted = true;
Ian Romanick6e659ca2010-03-29 15:11:05 -07001199 }
1200
Eric Anholt175829f2011-04-09 12:54:34 -10001201 if (error_emitted) {
1202 result = new(ctx) ir_constant(false);
1203 } else {
1204 result = do_comparison(ctx, operations[this->oper], op[0], op[1]);
1205 assert(result->type == glsl_type::bool_type);
Eric Anholt175829f2011-04-09 12:54:34 -10001206 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001207 break;
1208
1209 case ast_bit_and:
1210 case ast_bit_xor:
1211 case ast_bit_or:
Kenneth Graunke1eea9632010-08-31 10:56:24 -07001212 op[0] = this->subexpressions[0]->hir(instructions, state);
1213 op[1] = this->subexpressions[1]->hir(instructions, state);
Chad Versacecfdbf8b2010-10-15 11:28:05 -07001214 type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
1215 state, &loc);
Kenneth Graunke1eea9632010-08-31 10:56:24 -07001216 result = new(ctx) ir_expression(operations[this->oper], type,
1217 op[0], op[1]);
1218 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1219 break;
1220
Ian Romanicka87ac252010-02-22 13:19:34 -08001221 case ast_bit_not:
Kenneth Graunke1eea9632010-08-31 10:56:24 -07001222 op[0] = this->subexpressions[0]->hir(instructions, state);
1223
Paul Berry0d9bba62012-08-05 09:57:01 -07001224 if (!state->check_bitwise_operations_allowed(&loc)) {
Kenneth Graunke1eea9632010-08-31 10:56:24 -07001225 error_emitted = true;
1226 }
1227
1228 if (!op[0]->type->is_integer()) {
1229 _mesa_glsl_error(&loc, state, "operand of `~' must be an integer");
1230 error_emitted = true;
1231 }
1232
Ian Romanick39464482011-12-23 17:16:43 -08001233 type = error_emitted ? glsl_type::error_type : op[0]->type;
Kenneth Graunke1eea9632010-08-31 10:56:24 -07001234 result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL);
Ian Romanicka87ac252010-02-22 13:19:34 -08001235 break;
1236
Eric Anholt4950a682010-04-16 01:10:32 -07001237 case ast_logic_and: {
Eric Anholt7ec0c972011-04-09 10:27:02 -10001238 exec_list rhs_instructions;
Eric Anholt01822702011-04-09 15:59:20 -10001239 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1240 "LHS", &error_emitted);
Eric Anholt7ec0c972011-04-09 10:27:02 -10001241 op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
1242 "RHS", &error_emitted);
Eric Anholtb82c0c32010-03-31 09:11:39 -10001243
Eric Anholtead35892012-02-07 22:59:24 -08001244 if (rhs_instructions.is_empty()) {
1245 result = new(ctx) ir_expression(ir_binop_logic_and, op[0], op[1]);
1246 type = result->type;
Eric Anholt44b694e2010-04-16 13:49:04 -07001247 } else {
Ian Romanick81d664f2010-07-12 15:18:55 -07001248 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
Ian Romanick7e2aa912010-07-19 17:12:42 -07001249 "and_tmp",
1250 ir_var_temporary);
Ian Romanick81d664f2010-07-12 15:18:55 -07001251 instructions->push_tail(tmp);
1252
Carl Worth1660a292010-06-23 18:11:51 -07001253 ir_if *const stmt = new(ctx) ir_if(op[0]);
Eric Anholt44b694e2010-04-16 13:49:04 -07001254 instructions->push_tail(stmt);
Eric Anholt4950a682010-04-16 01:10:32 -07001255
Eric Anholt7ec0c972011-04-09 10:27:02 -10001256 stmt->then_instructions.append_list(&rhs_instructions);
Carl Worth1660a292010-06-23 18:11:51 -07001257 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -07001258 ir_assignment *const then_assign =
Eric Anholtaa5ec132012-05-14 09:14:54 -07001259 new(ctx) ir_assignment(then_deref, op[1]);
Eric Anholt44b694e2010-04-16 13:49:04 -07001260 stmt->then_instructions.push_tail(then_assign);
1261
Carl Worth1660a292010-06-23 18:11:51 -07001262 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -07001263 ir_assignment *const else_assign =
Eric Anholtaa5ec132012-05-14 09:14:54 -07001264 new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false));
Eric Anholt44b694e2010-04-16 13:49:04 -07001265 stmt->else_instructions.push_tail(else_assign);
1266
Carl Worth1660a292010-06-23 18:11:51 -07001267 result = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -07001268 type = tmp->type;
Eric Anholtb82c0c32010-03-31 09:11:39 -10001269 }
Eric Anholt4950a682010-04-16 01:10:32 -07001270 break;
1271 }
1272
1273 case ast_logic_or: {
Eric Anholt9e04b192011-04-09 10:27:02 -10001274 exec_list rhs_instructions;
Eric Anholt01822702011-04-09 15:59:20 -10001275 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1276 "LHS", &error_emitted);
Eric Anholt9e04b192011-04-09 10:27:02 -10001277 op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
1278 "RHS", &error_emitted);
Eric Anholt4950a682010-04-16 01:10:32 -07001279
Eric Anholtead35892012-02-07 22:59:24 -08001280 if (rhs_instructions.is_empty()) {
1281 result = new(ctx) ir_expression(ir_binop_logic_or, op[0], op[1]);
1282 type = result->type;
Eric Anholt44b694e2010-04-16 13:49:04 -07001283 } else {
Kenneth Graunkedfd30ca2010-07-08 12:40:52 -07001284 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
Ian Romanick7e2aa912010-07-19 17:12:42 -07001285 "or_tmp",
1286 ir_var_temporary);
Ian Romanick0b9ae3b2010-07-12 14:22:05 -07001287 instructions->push_tail(tmp);
Eric Anholt4950a682010-04-16 01:10:32 -07001288
Ian Romanick81d664f2010-07-12 15:18:55 -07001289 ir_if *const stmt = new(ctx) ir_if(op[0]);
1290 instructions->push_tail(stmt);
1291
Carl Worth1660a292010-06-23 18:11:51 -07001292 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -07001293 ir_assignment *const then_assign =
Eric Anholtaa5ec132012-05-14 09:14:54 -07001294 new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true));
Eric Anholt44b694e2010-04-16 13:49:04 -07001295 stmt->then_instructions.push_tail(then_assign);
1296
Eric Anholt9e04b192011-04-09 10:27:02 -10001297 stmt->else_instructions.append_list(&rhs_instructions);
Carl Worth1660a292010-06-23 18:11:51 -07001298 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -07001299 ir_assignment *const else_assign =
Eric Anholtaa5ec132012-05-14 09:14:54 -07001300 new(ctx) ir_assignment(else_deref, op[1]);
Eric Anholt44b694e2010-04-16 13:49:04 -07001301 stmt->else_instructions.push_tail(else_assign);
1302
Carl Worth1660a292010-06-23 18:11:51 -07001303 result = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -07001304 type = tmp->type;
Eric Anholt4950a682010-04-16 01:10:32 -07001305 }
Eric Anholt4950a682010-04-16 01:10:32 -07001306 break;
1307 }
1308
1309 case ast_logic_xor:
Eric Anholt756c2622011-04-09 14:57:17 -10001310 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1311 *
1312 * "The logical binary operators and (&&), or ( | | ), and
1313 * exclusive or (^^). They operate only on two Boolean
1314 * expressions and result in a Boolean expression."
1315 */
1316 op[0] = get_scalar_boolean_operand(instructions, state, this, 0, "LHS",
1317 &error_emitted);
1318 op[1] = get_scalar_boolean_operand(instructions, state, this, 1, "RHS",
1319 &error_emitted);
Eric Anholt4950a682010-04-16 01:10:32 -07001320
Carl Worth1660a292010-06-23 18:11:51 -07001321 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
1322 op[0], op[1]);
Ian Romanicka87ac252010-02-22 13:19:34 -08001323 break;
1324
Eric Anholta5827fe2010-03-31 16:50:55 -10001325 case ast_logic_not:
Eric Anholt01822702011-04-09 15:59:20 -10001326 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1327 "operand", &error_emitted);
Eric Anholta5827fe2010-03-31 16:50:55 -10001328
Carl Worth1660a292010-06-23 18:11:51 -07001329 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
1330 op[0], NULL);
Eric Anholta5827fe2010-03-31 16:50:55 -10001331 break;
1332
Ian Romanicka87ac252010-02-22 13:19:34 -08001333 case ast_mul_assign:
1334 case ast_div_assign:
1335 case ast_add_assign:
1336 case ast_sub_assign: {
Ian Romanick18238de2010-03-01 13:49:10 -08001337 op[0] = this->subexpressions[0]->hir(instructions, state);
1338 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001339
Ian Romanickbfb09c22010-03-29 16:32:55 -07001340 type = arithmetic_result_type(op[0], op[1],
Ian Romanick18238de2010-03-01 13:49:10 -08001341 (this->oper == ast_mul_assign),
Eric Anholta13bb142010-03-31 16:38:11 -10001342 state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -08001343
Carl Worth1660a292010-06-23 18:11:51 -07001344 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1345 op[0], op[1]);
Ian Romanicka87ac252010-02-22 13:19:34 -08001346
Eric Anholt3e24ef62010-06-23 12:40:17 -07001347 result = do_assignment(instructions, state,
Ian Romanicke9015e92011-12-23 09:56:29 -08001348 this->subexpressions[0]->non_lvalue_description,
Ian Romanick85caea22011-03-15 16:33:27 -07001349 op[0]->clone(ctx, NULL), temp_rhs, false,
Eric Anholt10a68522010-03-26 11:53:37 -07001350 this->subexpressions[0]->get_location());
Eric Anholt10a68522010-03-26 11:53:37 -07001351 error_emitted = (op[0]->type->is_error());
Ian Romanicka87ac252010-02-22 13:19:34 -08001352
1353 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1354 * explicitly test for this because none of the binary expression
1355 * operators allow array operands either.
1356 */
1357
Ian Romanicka87ac252010-02-22 13:19:34 -08001358 break;
1359 }
1360
Eric Anholt48a0e642010-03-26 11:57:46 -07001361 case ast_mod_assign: {
1362 op[0] = this->subexpressions[0]->hir(instructions, state);
1363 op[1] = this->subexpressions[1]->hir(instructions, state);
1364
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001365 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
Eric Anholt48a0e642010-03-26 11:57:46 -07001366
1367 assert(operations[this->oper] == ir_binop_mod);
1368
Ian Romanick768b55a2010-08-13 16:46:43 -07001369 ir_rvalue *temp_rhs;
Carl Worth1660a292010-06-23 18:11:51 -07001370 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1371 op[0], op[1]);
Eric Anholt48a0e642010-03-26 11:57:46 -07001372
Eric Anholt3e24ef62010-06-23 12:40:17 -07001373 result = do_assignment(instructions, state,
Ian Romanicke9015e92011-12-23 09:56:29 -08001374 this->subexpressions[0]->non_lvalue_description,
Ian Romanick85caea22011-03-15 16:33:27 -07001375 op[0]->clone(ctx, NULL), temp_rhs, false,
Eric Anholt48a0e642010-03-26 11:57:46 -07001376 this->subexpressions[0]->get_location());
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001377 error_emitted = type->is_error();
Eric Anholt48a0e642010-03-26 11:57:46 -07001378 break;
1379 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001380
1381 case ast_ls_assign:
Chad Versace338ed6e2010-10-15 10:05:50 -07001382 case ast_rs_assign: {
1383 op[0] = this->subexpressions[0]->hir(instructions, state);
1384 op[1] = this->subexpressions[1]->hir(instructions, state);
1385 type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1386 &loc);
1387 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1388 type, op[0], op[1]);
Ian Romanicke9015e92011-12-23 09:56:29 -08001389 result = do_assignment(instructions, state,
1390 this->subexpressions[0]->non_lvalue_description,
1391 op[0]->clone(ctx, NULL), temp_rhs, false,
Chad Versace338ed6e2010-10-15 10:05:50 -07001392 this->subexpressions[0]->get_location());
1393 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
Ian Romanick251eb752010-03-29 14:15:05 -07001394 break;
Chad Versace338ed6e2010-10-15 10:05:50 -07001395 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001396
1397 case ast_and_assign:
1398 case ast_xor_assign:
Chad Versaced03ac0f2010-10-15 12:08:28 -07001399 case ast_or_assign: {
1400 op[0] = this->subexpressions[0]->hir(instructions, state);
1401 op[1] = this->subexpressions[1]->hir(instructions, state);
1402 type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
1403 state, &loc);
1404 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1405 type, op[0], op[1]);
Ian Romanicke9015e92011-12-23 09:56:29 -08001406 result = do_assignment(instructions, state,
1407 this->subexpressions[0]->non_lvalue_description,
1408 op[0]->clone(ctx, NULL), temp_rhs, false,
Chad Versaced03ac0f2010-10-15 12:08:28 -07001409 this->subexpressions[0]->get_location());
1410 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
Ian Romanick251eb752010-03-29 14:15:05 -07001411 break;
Chad Versaced03ac0f2010-10-15 12:08:28 -07001412 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001413
Ian Romanick96f9cea2010-03-29 15:33:54 -07001414 case ast_conditional: {
Ian Romanick96f9cea2010-03-29 15:33:54 -07001415 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1416 *
1417 * "The ternary selection operator (?:). It operates on three
1418 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1419 * first expression, which must result in a scalar Boolean."
1420 */
Eric Anholt01822702011-04-09 15:59:20 -10001421 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1422 "condition", &error_emitted);
Ian Romanick96f9cea2010-03-29 15:33:54 -07001423
1424 /* The :? operator is implemented by generating an anonymous temporary
1425 * followed by an if-statement. The last instruction in each branch of
1426 * the if-statement assigns a value to the anonymous temporary. This
1427 * temporary is the r-value of the expression.
1428 */
Ian Romanick0ad76c62010-06-11 12:56:26 -07001429 exec_list then_instructions;
1430 exec_list else_instructions;
Ian Romanick96f9cea2010-03-29 15:33:54 -07001431
Ian Romanick0ad76c62010-06-11 12:56:26 -07001432 op[1] = this->subexpressions[1]->hir(&then_instructions, state);
1433 op[2] = this->subexpressions[2]->hir(&else_instructions, state);
Ian Romanick96f9cea2010-03-29 15:33:54 -07001434
1435 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1436 *
1437 * "The second and third expressions can be any type, as
1438 * long their types match, or there is a conversion in
1439 * Section 4.1.10 "Implicit Conversions" that can be applied
1440 * to one of the expressions to make their types match. This
1441 * resulting matching type is the type of the entire
1442 * expression."
1443 */
Ian Romanickbfb09c22010-03-29 16:32:55 -07001444 if ((!apply_implicit_conversion(op[1]->type, op[2], state)
1445 && !apply_implicit_conversion(op[2]->type, op[1], state))
Ian Romanickdb9be2e2010-03-29 16:25:56 -07001446 || (op[1]->type != op[2]->type)) {
Ian Romanick96f9cea2010-03-29 15:33:54 -07001447 YYLTYPE loc = this->subexpressions[1]->get_location();
1448
Paul Berry4d7899f2013-07-25 19:56:43 -07001449 _mesa_glsl_error(& loc, state, "second and third operands of ?: "
1450 "operator must have matching types");
Ian Romanick96f9cea2010-03-29 15:33:54 -07001451 error_emitted = true;
Ian Romanick0ad76c62010-06-11 12:56:26 -07001452 type = glsl_type::error_type;
Ian Romanickdb9be2e2010-03-29 16:25:56 -07001453 } else {
Ian Romanick0ad76c62010-06-11 12:56:26 -07001454 type = op[1]->type;
Ian Romanick96f9cea2010-03-29 15:33:54 -07001455 }
1456
Ian Romanickf09fabc2010-09-07 14:30:06 -07001457 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1458 *
1459 * "The second and third expressions must be the same type, but can
1460 * be of any type other than an array."
1461 */
Paul Berry0d9bba62012-08-05 09:57:01 -07001462 if (type->is_array() &&
Paul Berryd4a24742012-08-02 08:18:12 -07001463 !state->check_version(120, 300, &loc,
Paul Berry4d7899f2013-07-25 19:56:43 -07001464 "second and third operands of ?: operator "
Paul Berry0d9bba62012-08-05 09:57:01 -07001465 "cannot be arrays")) {
Ian Romanickf09fabc2010-09-07 14:30:06 -07001466 error_emitted = true;
1467 }
1468
Ian Romanick7825d3d2010-06-11 13:45:51 -07001469 ir_constant *cond_val = op[0]->constant_expression_value();
1470 ir_constant *then_val = op[1]->constant_expression_value();
1471 ir_constant *else_val = op[2]->constant_expression_value();
Ian Romanick0ad76c62010-06-11 12:56:26 -07001472
Ian Romanick7825d3d2010-06-11 13:45:51 -07001473 if (then_instructions.is_empty()
1474 && else_instructions.is_empty()
1475 && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) {
1476 result = (cond_val->value.b[0]) ? then_val : else_val;
1477 } else {
Ian Romanick7e2aa912010-07-19 17:12:42 -07001478 ir_variable *const tmp =
1479 new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
Ian Romanick0b9ae3b2010-07-12 14:22:05 -07001480 instructions->push_tail(tmp);
Ian Romanick0ad76c62010-06-11 12:56:26 -07001481
Carl Worth1660a292010-06-23 18:11:51 -07001482 ir_if *const stmt = new(ctx) ir_if(op[0]);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001483 instructions->push_tail(stmt);
Ian Romanick0ad76c62010-06-11 12:56:26 -07001484
Ian Romanick7825d3d2010-06-11 13:45:51 -07001485 then_instructions.move_nodes_to(& stmt->then_instructions);
Carl Worth1660a292010-06-23 18:11:51 -07001486 ir_dereference *const then_deref =
1487 new(ctx) ir_dereference_variable(tmp);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001488 ir_assignment *const then_assign =
Eric Anholtaa5ec132012-05-14 09:14:54 -07001489 new(ctx) ir_assignment(then_deref, op[1]);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001490 stmt->then_instructions.push_tail(then_assign);
Ian Romanick0ad76c62010-06-11 12:56:26 -07001491
Ian Romanick7825d3d2010-06-11 13:45:51 -07001492 else_instructions.move_nodes_to(& stmt->else_instructions);
Carl Worth1660a292010-06-23 18:11:51 -07001493 ir_dereference *const else_deref =
1494 new(ctx) ir_dereference_variable(tmp);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001495 ir_assignment *const else_assign =
Eric Anholtaa5ec132012-05-14 09:14:54 -07001496 new(ctx) ir_assignment(else_deref, op[2]);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001497 stmt->else_instructions.push_tail(else_assign);
1498
Carl Worth1660a292010-06-23 18:11:51 -07001499 result = new(ctx) ir_dereference_variable(tmp);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001500 }
Ian Romanick251eb752010-03-29 14:15:05 -07001501 break;
Ian Romanick96f9cea2010-03-29 15:33:54 -07001502 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001503
1504 case ast_pre_inc:
Eric Anholt76ea56c2010-03-26 12:16:54 -07001505 case ast_pre_dec: {
Ian Romanickfa0a9ac2011-12-23 09:56:03 -08001506 this->non_lvalue_description = (this->oper == ast_pre_inc)
1507 ? "pre-increment operation" : "pre-decrement operation";
1508
Eric Anholt76ea56c2010-03-26 12:16:54 -07001509 op[0] = this->subexpressions[0]->hir(instructions, state);
Paul Berry9abda922011-10-31 18:22:48 -07001510 op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
Eric Anholt76ea56c2010-03-26 12:16:54 -07001511
Eric Anholta13bb142010-03-31 16:38:11 -10001512 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
Eric Anholt76ea56c2010-03-26 12:16:54 -07001513
Ian Romanick768b55a2010-08-13 16:46:43 -07001514 ir_rvalue *temp_rhs;
Carl Worth1660a292010-06-23 18:11:51 -07001515 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1516 op[0], op[1]);
Eric Anholt76ea56c2010-03-26 12:16:54 -07001517
Eric Anholt3e24ef62010-06-23 12:40:17 -07001518 result = do_assignment(instructions, state,
Ian Romanicke9015e92011-12-23 09:56:29 -08001519 this->subexpressions[0]->non_lvalue_description,
Ian Romanick85caea22011-03-15 16:33:27 -07001520 op[0]->clone(ctx, NULL), temp_rhs, false,
Eric Anholt76ea56c2010-03-26 12:16:54 -07001521 this->subexpressions[0]->get_location());
Eric Anholt76ea56c2010-03-26 12:16:54 -07001522 error_emitted = op[0]->type->is_error();
1523 break;
1524 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001525
1526 case ast_post_inc:
Eric Anholtde38f0e2010-03-26 12:14:54 -07001527 case ast_post_dec: {
Ian Romanickfa0a9ac2011-12-23 09:56:03 -08001528 this->non_lvalue_description = (this->oper == ast_post_inc)
1529 ? "post-increment operation" : "post-decrement operation";
Eric Anholtde38f0e2010-03-26 12:14:54 -07001530 op[0] = this->subexpressions[0]->hir(instructions, state);
Paul Berry9abda922011-10-31 18:22:48 -07001531 op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
Eric Anholtde38f0e2010-03-26 12:14:54 -07001532
1533 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1534
Eric Anholta13bb142010-03-31 16:38:11 -10001535 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
Eric Anholtde38f0e2010-03-26 12:14:54 -07001536
Ian Romanick768b55a2010-08-13 16:46:43 -07001537 ir_rvalue *temp_rhs;
Carl Worth1660a292010-06-23 18:11:51 -07001538 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1539 op[0], op[1]);
Eric Anholtde38f0e2010-03-26 12:14:54 -07001540
1541 /* Get a temporary of a copy of the lvalue before it's modified.
1542 * This may get thrown away later.
1543 */
Eric Anholt8273bd42010-08-04 12:34:56 -07001544 result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
Eric Anholtde38f0e2010-03-26 12:14:54 -07001545
Eric Anholt3e24ef62010-06-23 12:40:17 -07001546 (void)do_assignment(instructions, state,
Ian Romanicke9015e92011-12-23 09:56:29 -08001547 this->subexpressions[0]->non_lvalue_description,
Ian Romanick85caea22011-03-15 16:33:27 -07001548 op[0]->clone(ctx, NULL), temp_rhs, false,
Eric Anholtde38f0e2010-03-26 12:14:54 -07001549 this->subexpressions[0]->get_location());
1550
Eric Anholtde38f0e2010-03-26 12:14:54 -07001551 error_emitted = op[0]->type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -08001552 break;
Eric Anholtde38f0e2010-03-26 12:14:54 -07001553 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001554
1555 case ast_field_selection:
Ian Romanick18238de2010-03-01 13:49:10 -08001556 result = _mesa_ast_field_selection_to_hir(this, instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001557 break;
1558
Ian Romanick27e3cf82010-04-01 18:03:59 -07001559 case ast_array_index: {
1560 YYLTYPE index_loc = subexpressions[1]->get_location();
1561
1562 op[0] = subexpressions[0]->hir(instructions, state);
1563 op[1] = subexpressions[1]->hir(instructions, state);
1564
Ian Romanick46934ad2013-03-15 14:10:12 -07001565 result = _mesa_ast_array_index_to_hir(ctx, state, op[0], op[1],
Ian Romanick58d93e32013-03-15 15:23:19 -07001566 loc, index_loc);
Ian Romanickb8a21cc2010-04-01 18:31:11 -07001567
Ian Romanick46934ad2013-03-15 14:10:12 -07001568 if (result->type->is_error())
Ian Romanick27e3cf82010-04-01 18:03:59 -07001569 error_emitted = true;
Ian Romanick27e3cf82010-04-01 18:03:59 -07001570
Ian Romanicka87ac252010-02-22 13:19:34 -08001571 break;
Ian Romanick27e3cf82010-04-01 18:03:59 -07001572 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001573
1574 case ast_function_call:
Ian Romanick7cfddf12010-03-10 13:26:52 -08001575 /* Should *NEVER* get here. ast_function_call should always be handled
1576 * by ast_function_expression::hir.
Ian Romanicka87ac252010-02-22 13:19:34 -08001577 */
Ian Romanick7cfddf12010-03-10 13:26:52 -08001578 assert(0);
Ian Romanicka87ac252010-02-22 13:19:34 -08001579 break;
1580
1581 case ast_identifier: {
1582 /* ast_identifier can appear several places in a full abstract syntax
1583 * tree. This particular use must be at location specified in the grammar
1584 * as 'variable_identifier'.
1585 */
Ian Romanick8bde4ce2010-03-19 11:57:24 -07001586 ir_variable *var =
1587 state->symbols->get_variable(this->primary_expression.identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08001588
Ian Romanicka87ac252010-02-22 13:19:34 -08001589 if (var != NULL) {
Ian Romanickbd330552011-01-07 18:34:58 -08001590 var->used = true;
Kenneth Graunkedca19a72012-03-13 14:59:42 -07001591 result = new(ctx) ir_dereference_variable(var);
Ian Romanicka87ac252010-02-22 13:19:34 -08001592 } else {
Ian Romanick71d0bbf2010-03-23 13:21:19 -07001593 _mesa_glsl_error(& loc, state, "`%s' undeclared",
Ian Romanick18238de2010-03-01 13:49:10 -08001594 this->primary_expression.identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08001595
Kenneth Graunke807e9672011-09-22 15:04:56 -07001596 result = ir_rvalue::error_value(ctx);
Ian Romanicka87ac252010-02-22 13:19:34 -08001597 error_emitted = true;
1598 }
1599 break;
1600 }
1601
1602 case ast_int_constant:
Carl Worth1660a292010-06-23 18:11:51 -07001603 result = new(ctx) ir_constant(this->primary_expression.int_constant);
Ian Romanicka87ac252010-02-22 13:19:34 -08001604 break;
1605
1606 case ast_uint_constant:
Carl Worth1660a292010-06-23 18:11:51 -07001607 result = new(ctx) ir_constant(this->primary_expression.uint_constant);
Ian Romanicka87ac252010-02-22 13:19:34 -08001608 break;
1609
1610 case ast_float_constant:
Carl Worth1660a292010-06-23 18:11:51 -07001611 result = new(ctx) ir_constant(this->primary_expression.float_constant);
Ian Romanicka87ac252010-02-22 13:19:34 -08001612 break;
1613
1614 case ast_bool_constant:
Carl Worth1660a292010-06-23 18:11:51 -07001615 result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
Ian Romanicka87ac252010-02-22 13:19:34 -08001616 break;
1617
1618 case ast_sequence: {
Ian Romanicka87ac252010-02-22 13:19:34 -08001619 /* It should not be possible to generate a sequence in the AST without
1620 * any expressions in it.
1621 */
Ian Romanick304ea902010-05-10 11:17:53 -07001622 assert(!this->expressions.is_empty());
Ian Romanicka87ac252010-02-22 13:19:34 -08001623
1624 /* The r-value of a sequence is the last expression in the sequence. If
1625 * the other expressions in the sequence do not have side-effects (and
1626 * therefore add instructions to the instruction list), they get dropped
1627 * on the floor.
1628 */
Ian Romanick3d5cfcf2011-04-11 10:10:30 -07001629 exec_node *previous_tail_pred = NULL;
1630 YYLTYPE previous_operand_loc = loc;
1631
1632 foreach_list_typed (ast_node, ast, link, &this->expressions) {
1633 /* If one of the operands of comma operator does not generate any
1634 * code, we want to emit a warning. At each pass through the loop
1635 * previous_tail_pred will point to the last instruction in the
1636 * stream *before* processing the previous operand. Naturally,
1637 * instructions->tail_pred will point to the last instruction in the
1638 * stream *after* processing the previous operand. If the two
1639 * pointers match, then the previous operand had no effect.
1640 *
1641 * The warning behavior here differs slightly from GCC. GCC will
1642 * only emit a warning if none of the left-hand operands have an
1643 * effect. However, it will emit a warning for each. I believe that
1644 * there are some cases in C (especially with GCC extensions) where
1645 * it is useful to have an intermediate step in a sequence have no
1646 * effect, but I don't think these cases exist in GLSL. Either way,
1647 * it would be a giant hassle to replicate that behavior.
1648 */
1649 if (previous_tail_pred == instructions->tail_pred) {
1650 _mesa_glsl_warning(&previous_operand_loc, state,
1651 "left-hand operand of comma expression has "
1652 "no effect");
1653 }
1654
1655 /* tail_pred is directly accessed instead of using the get_tail()
1656 * method for performance reasons. get_tail() has extra code to
1657 * return NULL when the list is empty. We don't care about that
1658 * here, so using tail_pred directly is fine.
1659 */
1660 previous_tail_pred = instructions->tail_pred;
1661 previous_operand_loc = ast->get_location();
1662
Ian Romanick304ea902010-05-10 11:17:53 -07001663 result = ast->hir(instructions, state);
Ian Romanick3d5cfcf2011-04-11 10:10:30 -07001664 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001665
Ian Romanicka87ac252010-02-22 13:19:34 -08001666 /* Any errors should have already been emitted in the loop above.
1667 */
1668 error_emitted = true;
1669 break;
1670 }
1671 }
Kenneth Graunke08ba9772011-04-14 17:21:59 -07001672 type = NULL; /* use result->type, not type. */
1673 assert(result != NULL);
Ian Romanicka87ac252010-02-22 13:19:34 -08001674
Kenneth Graunke08ba9772011-04-14 17:21:59 -07001675 if (result->type->is_error() && !error_emitted)
Ian Romanick71d0bbf2010-03-23 13:21:19 -07001676 _mesa_glsl_error(& loc, state, "type mismatch");
Ian Romanicka87ac252010-02-22 13:19:34 -08001677
1678 return result;
1679}
1680
1681
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001682ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08001683ast_expression_statement::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08001684 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08001685{
Ian Romanicka87ac252010-02-22 13:19:34 -08001686 /* It is possible to have expression statements that don't have an
1687 * expression. This is the solitary semicolon:
1688 *
1689 * for (i = 0; i < 5; i++)
1690 * ;
1691 *
1692 * In this case the expression will be NULL. Test for NULL and don't do
1693 * anything in that case.
1694 */
Ian Romanick18238de2010-03-01 13:49:10 -08001695 if (expression != NULL)
1696 expression->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001697
1698 /* Statements do not have r-values.
1699 */
1700 return NULL;
1701}
1702
1703
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001704ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08001705ast_compound_statement::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08001706 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08001707{
Ian Romanick18238de2010-03-01 13:49:10 -08001708 if (new_scope)
Ian Romanick8bde4ce2010-03-19 11:57:24 -07001709 state->symbols->push_scope();
Ian Romanicka87ac252010-02-22 13:19:34 -08001710
Ian Romanick2b97dc62010-05-10 17:42:05 -07001711 foreach_list_typed (ast_node, ast, link, &this->statements)
Ian Romanick304ea902010-05-10 11:17:53 -07001712 ast->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001713
Ian Romanick18238de2010-03-01 13:49:10 -08001714 if (new_scope)
Ian Romanick8bde4ce2010-03-19 11:57:24 -07001715 state->symbols->pop_scope();
Ian Romanicka87ac252010-02-22 13:19:34 -08001716
1717 /* Compound statements do not have r-values.
1718 */
1719 return NULL;
1720}
1721
1722
Ian Romanick28009cd2010-03-30 16:59:27 -07001723static const glsl_type *
Kenneth Graunked8e34e22010-08-07 02:56:01 -07001724process_array_type(YYLTYPE *loc, const glsl_type *base, ast_node *array_size,
Ian Romanick28009cd2010-03-30 16:59:27 -07001725 struct _mesa_glsl_parse_state *state)
1726{
1727 unsigned length = 0;
1728
Ian Romanickee55b842013-04-08 16:53:46 -07001729 if (base == NULL)
1730 return glsl_type::error_type;
1731
Ian Romanicka04211e2011-10-24 11:45:50 -07001732 /* From page 19 (page 25) of the GLSL 1.20 spec:
1733 *
1734 * "Only one-dimensional arrays may be declared."
1735 */
1736 if (base->is_array()) {
1737 _mesa_glsl_error(loc, state,
1738 "invalid array of `%s' (only one-dimensional arrays "
1739 "may be declared)",
1740 base->name);
1741 return glsl_type::error_type;
1742 }
Ian Romanick28009cd2010-03-30 16:59:27 -07001743
1744 if (array_size != NULL) {
1745 exec_list dummy_instructions;
1746 ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
1747 YYLTYPE loc = array_size->get_location();
1748
Ian Romanick28009cd2010-03-30 16:59:27 -07001749 if (ir != NULL) {
1750 if (!ir->type->is_integer()) {
1751 _mesa_glsl_error(& loc, state, "array size must be integer type");
1752 } else if (!ir->type->is_scalar()) {
1753 _mesa_glsl_error(& loc, state, "array size must be scalar type");
1754 } else {
1755 ir_constant *const size = ir->constant_expression_value();
1756
1757 if (size == NULL) {
1758 _mesa_glsl_error(& loc, state, "array size must be a "
1759 "constant valued expression");
1760 } else if (size->value.i[0] <= 0) {
1761 _mesa_glsl_error(& loc, state, "array size must be > 0");
1762 } else {
1763 assert(size->type == ir->type);
1764 length = size->value.u[0];
Paul Berryd4144a12011-08-01 15:23:07 -07001765
1766 /* If the array size is const (and we've verified that
1767 * it is) then no instructions should have been emitted
1768 * when we converted it to HIR. If they were emitted,
1769 * then either the array size isn't const after all, or
1770 * we are emitting unnecessary instructions.
1771 */
1772 assert(dummy_instructions.is_empty());
Ian Romanick28009cd2010-03-30 16:59:27 -07001773 }
1774 }
1775 }
1776 }
1777
Ian Romanickee55b842013-04-08 16:53:46 -07001778 const glsl_type *array_type = glsl_type::get_array_instance(base, length);
1779 return array_type != NULL ? array_type : glsl_type::error_type;
Ian Romanick28009cd2010-03-30 16:59:27 -07001780}
1781
1782
Ian Romanickd612a122010-03-31 16:22:06 -07001783const glsl_type *
1784ast_type_specifier::glsl_type(const char **name,
1785 struct _mesa_glsl_parse_state *state) const
Ian Romanicka87ac252010-02-22 13:19:34 -08001786{
Ian Romanickd612a122010-03-31 16:22:06 -07001787 const struct glsl_type *type;
Ian Romanicka87ac252010-02-22 13:19:34 -08001788
Kenneth Graunkeca92ae22010-09-18 11:11:09 +02001789 type = state->symbols->get_type(this->type_name);
1790 *name = this->type_name;
Ian Romanicka87ac252010-02-22 13:19:34 -08001791
Kenneth Graunkeca92ae22010-09-18 11:11:09 +02001792 if (this->is_array) {
1793 YYLTYPE loc = this->get_location();
1794 type = process_array_type(&loc, type, this->array_size, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001795 }
1796
1797 return type;
1798}
1799
Ian Romanickcabd4572013-08-09 15:17:18 -07001800const glsl_type *
1801ast_fully_specified_type::glsl_type(const char **name,
1802 struct _mesa_glsl_parse_state *state) const
1803{
1804 const struct glsl_type *type = this->specifier->glsl_type(name, state);
1805
1806 if (type == NULL)
1807 return NULL;
1808
1809 if (type->base_type == GLSL_TYPE_FLOAT
1810 && state->es_shader
1811 && state->target == fragment_shader
1812 && this->qualifier.precision == ast_precision_none
1813 && state->symbols->get_variable("#default precision") == NULL) {
1814 YYLTYPE loc = this->get_location();
1815 _mesa_glsl_error(&loc, state,
1816 "no precision specified this scope for type `%s'",
1817 type->name);
1818 }
1819
1820 return type;
1821}
Ian Romanicka87ac252010-02-22 13:19:34 -08001822
Paul Berry18720552012-12-18 15:24:39 -08001823/**
1824 * Determine whether a toplevel variable declaration declares a varying. This
1825 * function operates by examining the variable's mode and the shader target,
1826 * so it correctly identifies linkage variables regardless of whether they are
1827 * declared using the deprecated "varying" syntax or the new "in/out" syntax.
1828 *
1829 * Passing a non-toplevel variable declaration (e.g. a function parameter) to
1830 * this function will produce undefined results.
1831 */
1832static bool
1833is_varying_var(ir_variable *var, _mesa_glsl_parser_targets target)
1834{
1835 switch (target) {
1836 case vertex_shader:
Paul Berry42a29d82013-01-11 14:39:32 -08001837 return var->mode == ir_var_shader_out;
Paul Berry18720552012-12-18 15:24:39 -08001838 case fragment_shader:
Paul Berry42a29d82013-01-11 14:39:32 -08001839 return var->mode == ir_var_shader_in;
Paul Berry18720552012-12-18 15:24:39 -08001840 default:
Paul Berry42a29d82013-01-11 14:39:32 -08001841 return var->mode == ir_var_shader_out || var->mode == ir_var_shader_in;
Paul Berry18720552012-12-18 15:24:39 -08001842 }
1843}
1844
1845
Ian Romanickbb47a4d2013-01-16 12:01:50 -08001846/**
1847 * Matrix layout qualifiers are only allowed on certain types
1848 */
1849static void
1850validate_matrix_layout_for_type(struct _mesa_glsl_parse_state *state,
1851 YYLTYPE *loc,
1852 const glsl_type *type)
1853{
1854 if (!type->is_matrix() && !type->is_record()) {
1855 _mesa_glsl_error(loc, state,
1856 "uniform block layout qualifiers row_major and "
1857 "column_major can only be applied to matrix and "
1858 "structure types");
1859 } else if (type->is_record()) {
1860 /* We allow 'layout(row_major)' on structure types because it's the only
1861 * way to get row-major layouts on matrices contained in structures.
1862 */
1863 _mesa_glsl_warning(loc, state,
1864 "uniform block layout qualifiers row_major and "
1865 "column_major applied to structure types is not "
1866 "strictly conformant and my be rejected by other "
1867 "compilers");
1868 }
1869}
1870
Kenneth Graunke5e5e1202013-07-16 12:03:28 -07001871static bool
1872validate_binding_qualifier(struct _mesa_glsl_parse_state *state,
1873 YYLTYPE *loc,
1874 ir_variable *var,
1875 const ast_type_qualifier *qual)
1876{
1877 if (var->mode != ir_var_uniform) {
1878 _mesa_glsl_error(loc, state,
Paul Berry4d7899f2013-07-25 19:56:43 -07001879 "the \"binding\" qualifier only applies to uniforms");
Kenneth Graunke5e5e1202013-07-16 12:03:28 -07001880 return false;
1881 }
1882
1883 if (qual->binding < 0) {
Paul Berry4d7899f2013-07-25 19:56:43 -07001884 _mesa_glsl_error(loc, state, "binding values must be >= 0");
Kenneth Graunke5e5e1202013-07-16 12:03:28 -07001885 return false;
1886 }
1887
1888 const struct gl_context *const ctx = state->ctx;
1889 unsigned elements = var->type->is_array() ? var->type->length : 1;
1890 unsigned max_index = qual->binding + elements - 1;
1891
1892 if (var->type->is_interface()) {
1893 /* UBOs. From page 60 of the GLSL 4.20 specification:
1894 * "If the binding point for any uniform block instance is less than zero,
1895 * or greater than or equal to the implementation-dependent maximum
1896 * number of uniform buffer bindings, a compilation error will occur.
1897 * When the binding identifier is used with a uniform block instanced as
1898 * an array of size N, all elements of the array from binding through
1899 * binding + N – 1 must be within this range."
1900 *
1901 * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
1902 */
1903 if (max_index >= ctx->Const.MaxUniformBufferBindings) {
1904 _mesa_glsl_error(loc, state, "layout(binding = %d) for %d UBOs exceeds "
Paul Berry4d7899f2013-07-25 19:56:43 -07001905 "the maximum number of UBO binding points (%d)",
Kenneth Graunke5e5e1202013-07-16 12:03:28 -07001906 qual->binding, elements,
1907 ctx->Const.MaxUniformBufferBindings);
1908 return false;
1909 }
1910 } else if (var->type->is_sampler() ||
1911 (var->type->is_array() && var->type->fields.array->is_sampler())) {
1912 /* Samplers. From page 63 of the GLSL 4.20 specification:
1913 * "If the binding is less than zero, or greater than or equal to the
1914 * implementation-dependent maximum supported number of units, a
1915 * compilation error will occur. When the binding identifier is used
1916 * with an array of size N, all elements of the array from binding
1917 * through binding + N - 1 must be within this range."
1918 */
1919 unsigned limit;
1920 switch (state->target) {
1921 case vertex_shader:
1922 limit = ctx->Const.VertexProgram.MaxTextureImageUnits;
1923 break;
1924 case geometry_shader:
1925 limit = ctx->Const.GeometryProgram.MaxTextureImageUnits;
1926 break;
1927 case fragment_shader:
1928 limit = ctx->Const.FragmentProgram.MaxTextureImageUnits;
1929 break;
1930 }
1931
1932 if (max_index >= limit) {
1933 _mesa_glsl_error(loc, state, "layout(binding = %d) for %d samplers "
1934 "exceeds the maximum number of texture image units "
Paul Berry4d7899f2013-07-25 19:56:43 -07001935 "(%d)", qual->binding, elements, limit);
Kenneth Graunke5e5e1202013-07-16 12:03:28 -07001936
1937 return false;
1938 }
1939 } else {
1940 _mesa_glsl_error(loc, state,
1941 "the \"binding\" qualifier only applies to uniform "
Paul Berry4d7899f2013-07-25 19:56:43 -07001942 "blocks, samplers, or arrays of samplers");
Kenneth Graunke5e5e1202013-07-16 12:03:28 -07001943 return false;
1944 }
1945
1946 return true;
1947}
1948
Ian Romanicka87ac252010-02-22 13:19:34 -08001949static void
1950apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
Ian Romanick768b55a2010-08-13 16:46:43 -07001951 ir_variable *var,
Eric Anholt2e063f12010-03-28 00:56:22 -07001952 struct _mesa_glsl_parse_state *state,
Eric Anholtf7561e82012-04-26 18:19:39 -07001953 YYLTYPE *loc,
Paul Berryc33be482012-12-18 14:49:34 -08001954 bool is_parameter)
Ian Romanicka87ac252010-02-22 13:19:34 -08001955{
Eric Anholtf2e14232013-06-12 13:46:57 -07001956 STATIC_ASSERT(sizeof(qual->flags.q) <= sizeof(qual->flags.i));
1957
Ian Romanickbd330552011-01-07 18:34:58 -08001958 if (qual->flags.q.invariant) {
1959 if (var->used) {
1960 _mesa_glsl_error(loc, state,
1961 "variable `%s' may not be redeclared "
1962 "`invariant' after being used",
1963 var->name);
1964 } else {
1965 var->invariant = 1;
1966 }
1967 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001968
Ian Romanicke24d35a2010-10-05 16:38:47 -07001969 if (qual->flags.q.constant || qual->flags.q.attribute
1970 || qual->flags.q.uniform
1971 || (qual->flags.q.varying && (state->target == fragment_shader)))
Ian Romanicka87ac252010-02-22 13:19:34 -08001972 var->read_only = 1;
1973
Ian Romanicke24d35a2010-10-05 16:38:47 -07001974 if (qual->flags.q.centroid)
Ian Romanicka87ac252010-02-22 13:19:34 -08001975 var->centroid = 1;
1976
Ian Romanicke24d35a2010-10-05 16:38:47 -07001977 if (qual->flags.q.attribute && state->target != vertex_shader) {
Eric Anholt2e063f12010-03-28 00:56:22 -07001978 var->type = glsl_type::error_type;
1979 _mesa_glsl_error(loc, state,
1980 "`attribute' variables may not be declared in the "
Ian Romanickae4c4c02010-04-07 16:41:40 -07001981 "%s shader",
1982 _mesa_glsl_shader_target_name(state->target));
Eric Anholt2e063f12010-03-28 00:56:22 -07001983 }
1984
Ian Romanick58948982013-08-08 16:42:37 -07001985 /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
1986 *
1987 * "However, the const qualifier cannot be used with out or inout."
1988 *
1989 * The same section of the GLSL 4.40 spec further clarifies this saying:
1990 *
1991 * "The const qualifier cannot be used with out or inout, or a
1992 * compile-time error results."
1993 */
1994 if (is_parameter && qual->flags.q.constant && qual->flags.q.out) {
1995 _mesa_glsl_error(loc, state,
1996 "`const' may not be applied to `out' or `inout' "
1997 "function parameters");
1998 }
1999
Ian Romanick7e2aa912010-07-19 17:12:42 -07002000 /* If there is no qualifier that changes the mode of the variable, leave
2001 * the setting alone.
2002 */
Ian Romanicke24d35a2010-10-05 16:38:47 -07002003 if (qual->flags.q.in && qual->flags.q.out)
Paul Berry42a29d82013-01-11 14:39:32 -08002004 var->mode = ir_var_function_inout;
2005 else if (qual->flags.q.in)
2006 var->mode = is_parameter ? ir_var_function_in : ir_var_shader_in;
2007 else if (qual->flags.q.attribute
Ian Romanicke24d35a2010-10-05 16:38:47 -07002008 || (qual->flags.q.varying && (state->target == fragment_shader)))
Paul Berry42a29d82013-01-11 14:39:32 -08002009 var->mode = ir_var_shader_in;
2010 else if (qual->flags.q.out)
2011 var->mode = is_parameter ? ir_var_function_out : ir_var_shader_out;
2012 else if (qual->flags.q.varying && (state->target == vertex_shader))
2013 var->mode = ir_var_shader_out;
Ian Romanicke24d35a2010-10-05 16:38:47 -07002014 else if (qual->flags.q.uniform)
Ian Romanicka87ac252010-02-22 13:19:34 -08002015 var->mode = ir_var_uniform;
Ian Romanicka87ac252010-02-22 13:19:34 -08002016
Paul Berry18720552012-12-18 15:24:39 -08002017 if (!is_parameter && is_varying_var(var, state->target)) {
2018 /* This variable is being used to link data between shader stages (in
2019 * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
2020 * that is allowed for such purposes.
2021 *
2022 * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
2023 *
2024 * "The varying qualifier can be used only with the data types
2025 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
2026 * these."
2027 *
2028 * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
2029 * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
2030 *
2031 * "Fragment inputs can only be signed and unsigned integers and
2032 * integer vectors, float, floating-point vectors, matrices, or
2033 * arrays of these. Structures cannot be input.
2034 *
2035 * Similar text exists in the section on vertex shader outputs.
2036 *
2037 * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
Paul Berryc6a50dd2013-01-04 10:43:01 -08002038 * 3.00 spec allows structs as well. Varying structs are also allowed
2039 * in GLSL 1.50.
Paul Berry18720552012-12-18 15:24:39 -08002040 */
2041 switch (var->type->get_scalar_type()->base_type) {
2042 case GLSL_TYPE_FLOAT:
2043 /* Ok in all GLSL versions */
2044 break;
2045 case GLSL_TYPE_UINT:
2046 case GLSL_TYPE_INT:
2047 if (state->is_version(130, 300))
2048 break;
2049 _mesa_glsl_error(loc, state,
2050 "varying variables must be of base type float in %s",
2051 state->get_version_string());
2052 break;
2053 case GLSL_TYPE_STRUCT:
Paul Berryc6a50dd2013-01-04 10:43:01 -08002054 if (state->is_version(150, 300))
2055 break;
Paul Berry18720552012-12-18 15:24:39 -08002056 _mesa_glsl_error(loc, state,
2057 "varying variables may not be of type struct");
2058 break;
2059 default:
2060 _mesa_glsl_error(loc, state, "illegal type for a varying variable");
2061 break;
2062 }
2063 }
2064
Ian Romanick86b43982011-01-06 10:49:56 -08002065 if (state->all_invariant && (state->current_function == NULL)) {
2066 switch (state->target) {
2067 case vertex_shader:
Paul Berry42a29d82013-01-11 14:39:32 -08002068 if (var->mode == ir_var_shader_out)
Ian Romanick86b43982011-01-06 10:49:56 -08002069 var->invariant = true;
2070 break;
2071 case geometry_shader:
Paul Berry42a29d82013-01-11 14:39:32 -08002072 if ((var->mode == ir_var_shader_in)
2073 || (var->mode == ir_var_shader_out))
Ian Romanick86b43982011-01-06 10:49:56 -08002074 var->invariant = true;
2075 break;
2076 case fragment_shader:
Paul Berry42a29d82013-01-11 14:39:32 -08002077 if (var->mode == ir_var_shader_in)
Ian Romanick86b43982011-01-06 10:49:56 -08002078 var->invariant = true;
2079 break;
2080 }
2081 }
2082
Ian Romanicke24d35a2010-10-05 16:38:47 -07002083 if (qual->flags.q.flat)
Paul Berrycf459492011-10-25 18:06:37 -07002084 var->interpolation = INTERP_QUALIFIER_FLAT;
Ian Romanicke24d35a2010-10-05 16:38:47 -07002085 else if (qual->flags.q.noperspective)
Paul Berrycf459492011-10-25 18:06:37 -07002086 var->interpolation = INTERP_QUALIFIER_NOPERSPECTIVE;
Paul Berryc4881502011-10-21 07:40:37 -07002087 else if (qual->flags.q.smooth)
Paul Berrycf459492011-10-25 18:06:37 -07002088 var->interpolation = INTERP_QUALIFIER_SMOOTH;
Paul Berryc4881502011-10-21 07:40:37 -07002089 else
2090 var->interpolation = INTERP_QUALIFIER_NONE;
Ian Romanick9d975372010-04-02 17:17:47 -07002091
Kenneth Graunkea1ddbd12013-08-02 01:28:58 -07002092 if (var->interpolation != INTERP_QUALIFIER_NONE) {
2093 ir_variable_mode mode = (ir_variable_mode) var->mode;
2094
2095 if (mode != ir_var_shader_in && mode != ir_var_shader_out) {
2096 _mesa_glsl_error(loc, state,
2097 "interpolation qualifier `%s' can only be applied to "
2098 "shader inputs or outputs.",
2099 var->interpolation_string());
2100
2101 }
2102
2103 if ((state->target == vertex_shader && mode == ir_var_shader_in) ||
2104 (state->target == fragment_shader && mode == ir_var_shader_out)) {
2105 _mesa_glsl_error(loc, state,
2106 "interpolation qualifier `%s' cannot be applied to "
2107 "vertex shader inputs or fragment shader outputs",
2108 var->interpolation_string());
2109 }
Eric Anholt916e2062012-01-09 16:40:20 -08002110 }
2111
Ian Romanicke24d35a2010-10-05 16:38:47 -07002112 var->pixel_center_integer = qual->flags.q.pixel_center_integer;
2113 var->origin_upper_left = qual->flags.q.origin_upper_left;
2114 if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer)
Ian Romanick8d8469e2010-06-30 17:48:09 -07002115 && (strcmp(var->name, "gl_FragCoord") != 0)) {
Ian Romanicke24d35a2010-10-05 16:38:47 -07002116 const char *const qual_string = (qual->flags.q.origin_upper_left)
Ian Romanick8d8469e2010-06-30 17:48:09 -07002117 ? "origin_upper_left" : "pixel_center_integer";
2118
2119 _mesa_glsl_error(loc, state,
2120 "layout qualifier `%s' can only be applied to "
2121 "fragment shader input `gl_FragCoord'",
2122 qual_string);
2123 }
2124
Ian Romanickeee68d32010-10-07 15:13:38 -07002125 if (qual->flags.q.explicit_location) {
2126 const bool global_scope = (state->current_function == NULL);
2127 bool fail = false;
2128 const char *string = "";
2129
2130 /* In the vertex shader only shader inputs can be given explicit
2131 * locations.
2132 *
2133 * In the fragment shader only shader outputs can be given explicit
2134 * locations.
2135 */
2136 switch (state->target) {
2137 case vertex_shader:
Paul Berry42a29d82013-01-11 14:39:32 -08002138 if (!global_scope || (var->mode != ir_var_shader_in)) {
Ian Romanickeee68d32010-10-07 15:13:38 -07002139 fail = true;
2140 string = "input";
2141 }
2142 break;
2143
2144 case geometry_shader:
2145 _mesa_glsl_error(loc, state,
2146 "geometry shader variables cannot be given "
Paul Berry4d7899f2013-07-25 19:56:43 -07002147 "explicit locations");
Ian Romanickeee68d32010-10-07 15:13:38 -07002148 break;
2149
2150 case fragment_shader:
Paul Berry42a29d82013-01-11 14:39:32 -08002151 if (!global_scope || (var->mode != ir_var_shader_out)) {
Ian Romanickeee68d32010-10-07 15:13:38 -07002152 fail = true;
2153 string = "output";
2154 }
2155 break;
Kenneth Graunkea75da2c2010-10-20 14:59:40 -07002156 };
Ian Romanickeee68d32010-10-07 15:13:38 -07002157
2158 if (fail) {
2159 _mesa_glsl_error(loc, state,
2160 "only %s shader %s variables can be given an "
Paul Berry4d7899f2013-07-25 19:56:43 -07002161 "explicit location",
Ian Romanickeee68d32010-10-07 15:13:38 -07002162 _mesa_glsl_shader_target_name(state->target),
2163 string);
2164 } else {
2165 var->explicit_location = true;
Ian Romanick68a4fc92010-10-07 17:21:22 -07002166
2167 /* This bit of silliness is needed because invalid explicit locations
2168 * are supposed to be flagged during linking. Small negative values
2169 * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
2170 * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
2171 * The linker needs to be able to differentiate these cases. This
2172 * ensures that negative values stay negative.
2173 */
2174 if (qual->location >= 0) {
2175 var->location = (state->target == vertex_shader)
2176 ? (qual->location + VERT_ATTRIB_GENERIC0)
2177 : (qual->location + FRAG_RESULT_DATA0);
2178 } else {
2179 var->location = qual->location;
2180 }
Kenneth Graunke354f2cb2012-08-31 16:04:19 -07002181
Dave Airlie1256a5d2012-03-24 13:33:41 +00002182 if (qual->flags.q.explicit_index) {
Kenneth Graunke354f2cb2012-08-31 16:04:19 -07002183 /* From the GLSL 4.30 specification, section 4.4.2 (Output
2184 * Layout Qualifiers):
2185 *
2186 * "It is also a compile-time error if a fragment shader
2187 * sets a layout index to less than 0 or greater than 1."
2188 *
2189 * Older specifications don't mandate a behavior; we take
2190 * this as a clarification and always generate the error.
2191 */
2192 if (qual->index < 0 || qual->index > 1) {
2193 _mesa_glsl_error(loc, state,
Paul Berry4d7899f2013-07-25 19:56:43 -07002194 "explicit index may only be 0 or 1");
Kenneth Graunke354f2cb2012-08-31 16:04:19 -07002195 } else {
2196 var->explicit_index = true;
2197 var->index = qual->index;
2198 }
Dave Airlie1256a5d2012-03-24 13:33:41 +00002199 }
Ian Romanickeee68d32010-10-07 15:13:38 -07002200 }
Dave Airlie1256a5d2012-03-24 13:33:41 +00002201 } else if (qual->flags.q.explicit_index) {
2202 _mesa_glsl_error(loc, state,
Paul Berry4d7899f2013-07-25 19:56:43 -07002203 "explicit index requires explicit location");
Ian Romanickeee68d32010-10-07 15:13:38 -07002204 }
2205
Kenneth Graunked4375fc2013-07-18 15:11:57 -07002206 if (qual->flags.q.explicit_binding &&
2207 validate_binding_qualifier(state, loc, var, qual)) {
2208 var->explicit_binding = true;
2209 var->binding = qual->binding;
2210 }
Kenneth Graunke5e5e1202013-07-16 12:03:28 -07002211
Ian Romanick4bcff0c2011-01-07 16:53:59 -08002212 /* Does the declaration use the deprecated 'attribute' or 'varying'
2213 * keywords?
2214 */
2215 const bool uses_deprecated_qualifier = qual->flags.q.attribute
2216 || qual->flags.q.varying;
2217
2218 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
2219 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
2220 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
2221 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
2222 * These extensions and all following extensions that add the 'layout'
2223 * keyword have been modified to require the use of 'in' or 'out'.
2224 *
2225 * The following extension do not allow the deprecated keywords:
2226 *
2227 * GL_AMD_conservative_depth
Marek Olšák6b43d6f2011-11-19 16:41:08 +01002228 * GL_ARB_conservative_depth
Ian Romanick4bcff0c2011-01-07 16:53:59 -08002229 * GL_ARB_gpu_shader5
2230 * GL_ARB_separate_shader_objects
2231 * GL_ARB_tesselation_shader
2232 * GL_ARB_transform_feedback3
2233 * GL_ARB_uniform_buffer_object
2234 *
2235 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
2236 * allow layout with the deprecated keywords.
2237 */
2238 const bool relaxed_layout_qualifier_checking =
2239 state->ARB_fragment_coord_conventions_enable;
2240
Kenneth Graunke56bcde32013-07-16 11:39:01 -07002241 if (qual->has_layout() && uses_deprecated_qualifier) {
Ian Romanick4bcff0c2011-01-07 16:53:59 -08002242 if (relaxed_layout_qualifier_checking) {
2243 _mesa_glsl_warning(loc, state,
2244 "`layout' qualifier may not be used with "
2245 "`attribute' or `varying'");
2246 } else {
2247 _mesa_glsl_error(loc, state,
2248 "`layout' qualifier may not be used with "
2249 "`attribute' or `varying'");
2250 }
2251 }
2252
Chad Versacebc04d242011-01-27 01:40:26 -08002253 /* Layout qualifiers for gl_FragDepth, which are enabled by extension
2254 * AMD_conservative_depth.
2255 */
2256 int depth_layout_count = qual->flags.q.depth_any
2257 + qual->flags.q.depth_greater
2258 + qual->flags.q.depth_less
2259 + qual->flags.q.depth_unchanged;
2260 if (depth_layout_count > 0
Marek Olšák6b43d6f2011-11-19 16:41:08 +01002261 && !state->AMD_conservative_depth_enable
2262 && !state->ARB_conservative_depth_enable) {
Chad Versacebc04d242011-01-27 01:40:26 -08002263 _mesa_glsl_error(loc, state,
Marek Olšák6b43d6f2011-11-19 16:41:08 +01002264 "extension GL_AMD_conservative_depth or "
2265 "GL_ARB_conservative_depth must be enabled "
Chad Versacebc04d242011-01-27 01:40:26 -08002266 "to use depth layout qualifiers");
2267 } else if (depth_layout_count > 0
2268 && strcmp(var->name, "gl_FragDepth") != 0) {
2269 _mesa_glsl_error(loc, state,
2270 "depth layout qualifiers can be applied only to "
2271 "gl_FragDepth");
2272 } else if (depth_layout_count > 1
2273 && strcmp(var->name, "gl_FragDepth") == 0) {
2274 _mesa_glsl_error(loc, state,
2275 "at most one depth layout qualifier can be applied to "
2276 "gl_FragDepth");
2277 }
2278 if (qual->flags.q.depth_any)
2279 var->depth_layout = ir_depth_layout_any;
2280 else if (qual->flags.q.depth_greater)
2281 var->depth_layout = ir_depth_layout_greater;
2282 else if (qual->flags.q.depth_less)
2283 var->depth_layout = ir_depth_layout_less;
2284 else if (qual->flags.q.depth_unchanged)
2285 var->depth_layout = ir_depth_layout_unchanged;
2286 else
2287 var->depth_layout = ir_depth_layout_none;
Eric Anholtf7561e82012-04-26 18:19:39 -07002288
2289 if (qual->flags.q.std140 ||
2290 qual->flags.q.packed ||
2291 qual->flags.q.shared) {
2292 _mesa_glsl_error(loc, state,
2293 "uniform block layout qualifiers std140, packed, and "
2294 "shared can only be applied to uniform blocks, not "
2295 "members");
2296 }
2297
Ian Romanickbb47a4d2013-01-16 12:01:50 -08002298 if (qual->flags.q.row_major || qual->flags.q.column_major) {
Matt Turner77373e02013-08-15 10:46:46 -07002299 validate_matrix_layout_for_type(state, loc, var->type);
Eric Anholtf7561e82012-04-26 18:19:39 -07002300 }
Ian Romanicka87ac252010-02-22 13:19:34 -08002301}
2302
Ian Romanick8e6cb9f2011-03-04 15:28:40 -08002303/**
2304 * Get the variable that is being redeclared by this declaration
2305 *
2306 * Semantic checks to verify the validity of the redeclaration are also
2307 * performed. If semantic checks fail, compilation error will be emitted via
2308 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
2309 *
2310 * \returns
2311 * A pointer to an existing variable in the current scope if the declaration
2312 * is a redeclaration, \c NULL otherwise.
2313 */
2314ir_variable *
2315get_variable_being_redeclared(ir_variable *var, ast_declaration *decl,
2316 struct _mesa_glsl_parse_state *state)
2317{
2318 /* Check if this declaration is actually a re-declaration, either to
2319 * resize an array or add qualifiers to an existing variable.
2320 *
2321 * This is allowed for variables in the current scope, or when at
2322 * global scope (for built-ins in the implicit outer scope).
2323 */
2324 ir_variable *earlier = state->symbols->get_variable(decl->identifier);
2325 if (earlier == NULL ||
2326 (state->current_function != NULL &&
2327 !state->symbols->name_declared_this_scope(decl->identifier))) {
2328 return NULL;
2329 }
2330
2331
2332 YYLTYPE loc = decl->get_location();
2333
2334 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
2335 *
2336 * "It is legal to declare an array without a size and then
2337 * later re-declare the same name as an array of the same
2338 * type and specify a size."
2339 */
2340 if ((earlier->type->array_size() == 0)
2341 && var->type->is_array()
2342 && (var->type->element_type() == earlier->type->element_type())) {
2343 /* FINISHME: This doesn't match the qualifiers on the two
2344 * FINISHME: declarations. It's not 100% clear whether this is
2345 * FINISHME: required or not.
2346 */
2347
Ian Romanick8e6cb9f2011-03-04 15:28:40 -08002348 const unsigned size = unsigned(var->type->array_size());
Paul Berry93b97582011-09-06 10:01:51 -07002349 check_builtin_array_max_size(var->name, size, loc, state);
2350 if ((size > 0) && (size <= earlier->max_array_access)) {
Ian Romanick8e6cb9f2011-03-04 15:28:40 -08002351 _mesa_glsl_error(& loc, state, "array size must be > %u due to "
2352 "previous access",
2353 earlier->max_array_access);
2354 }
2355
2356 earlier->type = var->type;
2357 delete var;
2358 var = NULL;
Paul Berry417dc802013-08-06 12:17:17 -07002359 } else if ((state->ARB_fragment_coord_conventions_enable ||
2360 state->is_version(150, 0))
Ian Romanick8e6cb9f2011-03-04 15:28:40 -08002361 && strcmp(var->name, "gl_FragCoord") == 0
2362 && earlier->type == var->type
2363 && earlier->mode == var->mode) {
2364 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
2365 * qualifiers.
2366 */
2367 earlier->origin_upper_left = var->origin_upper_left;
2368 earlier->pixel_center_integer = var->pixel_center_integer;
2369
2370 /* According to section 4.3.7 of the GLSL 1.30 spec,
2371 * the following built-in varaibles can be redeclared with an
2372 * interpolation qualifier:
2373 * * gl_FrontColor
2374 * * gl_BackColor
2375 * * gl_FrontSecondaryColor
2376 * * gl_BackSecondaryColor
2377 * * gl_Color
2378 * * gl_SecondaryColor
2379 */
Paul Berrye3ded7f2012-08-01 14:50:05 -07002380 } else if (state->is_version(130, 0)
Ian Romanick8e6cb9f2011-03-04 15:28:40 -08002381 && (strcmp(var->name, "gl_FrontColor") == 0
2382 || strcmp(var->name, "gl_BackColor") == 0
2383 || strcmp(var->name, "gl_FrontSecondaryColor") == 0
2384 || strcmp(var->name, "gl_BackSecondaryColor") == 0
2385 || strcmp(var->name, "gl_Color") == 0
2386 || strcmp(var->name, "gl_SecondaryColor") == 0)
2387 && earlier->type == var->type
2388 && earlier->mode == var->mode) {
2389 earlier->interpolation = var->interpolation;
2390
2391 /* Layout qualifiers for gl_FragDepth. */
Marek Olšák6b43d6f2011-11-19 16:41:08 +01002392 } else if ((state->AMD_conservative_depth_enable ||
2393 state->ARB_conservative_depth_enable)
Ian Romanick8e6cb9f2011-03-04 15:28:40 -08002394 && strcmp(var->name, "gl_FragDepth") == 0
2395 && earlier->type == var->type
2396 && earlier->mode == var->mode) {
2397
2398 /** From the AMD_conservative_depth spec:
2399 * Within any shader, the first redeclarations of gl_FragDepth
2400 * must appear before any use of gl_FragDepth.
2401 */
2402 if (earlier->used) {
2403 _mesa_glsl_error(&loc, state,
2404 "the first redeclaration of gl_FragDepth "
2405 "must appear before any use of gl_FragDepth");
2406 }
2407
2408 /* Prevent inconsistent redeclaration of depth layout qualifier. */
2409 if (earlier->depth_layout != ir_depth_layout_none
2410 && earlier->depth_layout != var->depth_layout) {
2411 _mesa_glsl_error(&loc, state,
2412 "gl_FragDepth: depth layout is declared here "
2413 "as '%s, but it was previously declared as "
2414 "'%s'",
2415 depth_layout_string(var->depth_layout),
2416 depth_layout_string(earlier->depth_layout));
2417 }
2418
2419 earlier->depth_layout = var->depth_layout;
2420
2421 } else {
2422 _mesa_glsl_error(&loc, state, "`%s' redeclared", decl->identifier);
2423 }
2424
2425 return earlier;
2426}
Ian Romanicka87ac252010-02-22 13:19:34 -08002427
Ian Romanick0292ffb2011-03-04 15:29:33 -08002428/**
2429 * Generate the IR for an initializer in a variable declaration
2430 */
2431ir_rvalue *
2432process_initializer(ir_variable *var, ast_declaration *decl,
2433 ast_fully_specified_type *type,
2434 exec_list *initializer_instructions,
2435 struct _mesa_glsl_parse_state *state)
2436{
2437 ir_rvalue *result = NULL;
2438
2439 YYLTYPE initializer_loc = decl->initializer->get_location();
2440
2441 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
2442 *
2443 * "All uniform variables are read-only and are initialized either
2444 * directly by an application via API commands, or indirectly by
2445 * OpenGL."
2446 */
Paul Berry0d9bba62012-08-05 09:57:01 -07002447 if (var->mode == ir_var_uniform) {
2448 state->check_version(120, 0, &initializer_loc,
2449 "cannot initialize uniforms");
Ian Romanick0292ffb2011-03-04 15:29:33 -08002450 }
2451
2452 if (var->type->is_sampler()) {
2453 _mesa_glsl_error(& initializer_loc, state,
2454 "cannot initialize samplers");
2455 }
2456
Paul Berry42a29d82013-01-11 14:39:32 -08002457 if ((var->mode == ir_var_shader_in) && (state->current_function == NULL)) {
Ian Romanick0292ffb2011-03-04 15:29:33 -08002458 _mesa_glsl_error(& initializer_loc, state,
2459 "cannot initialize %s shader input / %s",
2460 _mesa_glsl_shader_target_name(state->target),
2461 (state->target == vertex_shader)
2462 ? "attribute" : "varying");
2463 }
2464
2465 ir_dereference *const lhs = new(state) ir_dereference_variable(var);
2466 ir_rvalue *rhs = decl->initializer->hir(initializer_instructions,
2467 state);
2468
2469 /* Calculate the constant value if this is a const or uniform
2470 * declaration.
2471 */
2472 if (type->qualifier.flags.q.constant
2473 || type->qualifier.flags.q.uniform) {
Ian Romanick85caea22011-03-15 16:33:27 -07002474 ir_rvalue *new_rhs = validate_assignment(state, var->type, rhs, true);
Ian Romanick0292ffb2011-03-04 15:29:33 -08002475 if (new_rhs != NULL) {
2476 rhs = new_rhs;
2477
2478 ir_constant *constant_value = rhs->constant_expression_value();
2479 if (!constant_value) {
Matt Turnerba7b60d2013-05-23 11:48:40 -07002480 /* If ARB_shading_language_420pack is enabled, initializers of
2481 * const-qualified local variables do not have to be constant
2482 * expressions. Const-qualified global variables must still be
2483 * initialized with constant expressions.
2484 */
2485 if (!state->ARB_shading_language_420pack_enable
2486 || state->current_function == NULL) {
2487 _mesa_glsl_error(& initializer_loc, state,
2488 "initializer of %s variable `%s' must be a "
2489 "constant expression",
2490 (type->qualifier.flags.q.constant)
2491 ? "const" : "uniform",
2492 decl->identifier);
2493 if (var->type->is_numeric()) {
2494 /* Reduce cascading errors. */
2495 var->constant_value = ir_constant::zero(state, var->type);
2496 }
2497 }
2498 } else {
Ian Romanick0292ffb2011-03-04 15:29:33 -08002499 rhs = constant_value;
2500 var->constant_value = constant_value;
2501 }
2502 } else {
2503 _mesa_glsl_error(&initializer_loc, state,
2504 "initializer of type %s cannot be assigned to "
2505 "variable of type %s",
2506 rhs->type->name, var->type->name);
2507 if (var->type->is_numeric()) {
2508 /* Reduce cascading errors. */
2509 var->constant_value = ir_constant::zero(state, var->type);
2510 }
2511 }
2512 }
2513
2514 if (rhs && !rhs->type->is_error()) {
2515 bool temp = var->read_only;
2516 if (type->qualifier.flags.q.constant)
2517 var->read_only = false;
2518
2519 /* Never emit code to initialize a uniform.
2520 */
2521 const glsl_type *initializer_type;
2522 if (!type->qualifier.flags.q.uniform) {
2523 result = do_assignment(initializer_instructions, state,
Ian Romanicke9015e92011-12-23 09:56:29 -08002524 NULL,
Ian Romanick85caea22011-03-15 16:33:27 -07002525 lhs, rhs, true,
Ian Romanick0292ffb2011-03-04 15:29:33 -08002526 type->get_location());
2527 initializer_type = result->type;
2528 } else
2529 initializer_type = rhs->type;
2530
Ian Romanickf37b1ad2011-10-31 14:31:07 -07002531 var->constant_initializer = rhs->constant_expression_value();
2532 var->has_initializer = true;
2533
Ian Romanick0292ffb2011-03-04 15:29:33 -08002534 /* If the declared variable is an unsized array, it must inherrit
2535 * its full type from the initializer. A declaration such as
2536 *
2537 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
2538 *
2539 * becomes
2540 *
2541 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
2542 *
2543 * The assignment generated in the if-statement (below) will also
2544 * automatically handle this case for non-uniforms.
2545 *
2546 * If the declared variable is not an array, the types must
2547 * already match exactly. As a result, the type assignment
2548 * here can be done unconditionally. For non-uniforms the call
2549 * to do_assignment can change the type of the initializer (via
2550 * the implicit conversion rules). For uniforms the initializer
2551 * must be a constant expression, and the type of that expression
2552 * was validated above.
2553 */
2554 var->type = initializer_type;
2555
2556 var->read_only = temp;
2557 }
2558
2559 return result;
2560}
2561
Paul Berry7cfefe62013-07-30 21:13:48 -07002562
2563/**
Paul Berry91c8fea2013-08-12 06:39:23 -07002564 * Do additional processing necessary for geometry shader input declarations
2565 * (this covers both interface blocks arrays and bare input variables).
Paul Berry7cfefe62013-07-30 21:13:48 -07002566 */
2567static void
2568handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state *state,
2569 YYLTYPE loc, ir_variable *var)
2570{
2571 unsigned num_vertices = 0;
2572 if (state->gs_input_prim_type_specified) {
2573 num_vertices = vertices_per_prim(state->gs_input_prim_type);
2574 }
2575
Paul Berry91c8fea2013-08-12 06:39:23 -07002576 /* Geometry shader input variables must be arrays. Caller should have
2577 * reported an error for this.
2578 */
2579 if (!var->type->is_array()) {
2580 assert(state->error);
2581
2582 /* To avoid cascading failures, short circuit the checks below. */
2583 return;
2584 }
2585
Paul Berry7cfefe62013-07-30 21:13:48 -07002586 if (var->type->length == 0) {
2587 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says:
2588 *
2589 * All geometry shader input unsized array declarations will be
2590 * sized by an earlier input layout qualifier, when present, as per
2591 * the following table.
2592 *
2593 * Followed by a table mapping each allowed input layout qualifier to
2594 * the corresponding input length.
2595 */
2596 if (num_vertices != 0)
2597 var->type = glsl_type::get_array_instance(var->type->fields.array,
2598 num_vertices);
2599 } else {
2600 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec
2601 * includes the following examples of compile-time errors:
2602 *
2603 * // code sequence within one shader...
2604 * in vec4 Color1[]; // size unknown
2605 * ...Color1.length()...// illegal, length() unknown
2606 * in vec4 Color2[2]; // size is 2
2607 * ...Color1.length()...// illegal, Color1 still has no size
2608 * in vec4 Color3[3]; // illegal, input sizes are inconsistent
2609 * layout(lines) in; // legal, input size is 2, matching
2610 * in vec4 Color4[3]; // illegal, contradicts layout
2611 * ...
2612 *
2613 * To detect the case illustrated by Color3, we verify that the size of
2614 * an explicitly-sized array matches the size of any previously declared
2615 * explicitly-sized array. To detect the case illustrated by Color4, we
2616 * verify that the size of an explicitly-sized array is consistent with
2617 * any previously declared input layout.
2618 */
2619 if (num_vertices != 0 && var->type->length != num_vertices) {
2620 _mesa_glsl_error(&loc, state,
2621 "geometry shader input size contradicts previously"
2622 " declared layout (size is %u, but layout requires a"
2623 " size of %u)", var->type->length, num_vertices);
2624 } else if (state->gs_input_size != 0 &&
2625 var->type->length != state->gs_input_size) {
2626 _mesa_glsl_error(&loc, state,
2627 "geometry shader input sizes are "
2628 "inconsistent (size is %u, but a previous "
2629 "declaration has size %u)",
2630 var->type->length, state->gs_input_size);
2631 } else {
2632 state->gs_input_size = var->type->length;
2633 }
2634 }
2635}
2636
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07002637ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08002638ast_declarator_list::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08002639 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08002640{
Kenneth Graunke953ff122010-06-25 13:14:37 -07002641 void *ctx = state;
Ian Romanicka87ac252010-02-22 13:19:34 -08002642 const struct glsl_type *decl_type;
2643 const char *type_name = NULL;
Eric Anholt85584592010-04-14 15:38:52 -07002644 ir_rvalue *result = NULL;
Ian Romanickc824e352010-04-23 15:55:19 -07002645 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08002646
Ian Romanick6f0823d2010-07-01 20:39:08 -07002647 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
2648 *
2649 * "To ensure that a particular output variable is invariant, it is
2650 * necessary to use the invariant qualifier. It can either be used to
2651 * qualify a previously declared variable as being invariant
2652 *
2653 * invariant gl_Position; // make existing gl_Position be invariant"
2654 *
2655 * In these cases the parser will set the 'invariant' flag in the declarator
2656 * list, and the type will be NULL.
2657 */
2658 if (this->invariant) {
2659 assert(this->type == NULL);
2660
2661 if (state->current_function != NULL) {
2662 _mesa_glsl_error(& loc, state,
Paul Berry4d7899f2013-07-25 19:56:43 -07002663 "all uses of `invariant' keyword must be at global "
2664 "scope");
Ian Romanick6f0823d2010-07-01 20:39:08 -07002665 }
2666
2667 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
2668 assert(!decl->is_array);
2669 assert(decl->array_size == NULL);
2670 assert(decl->initializer == NULL);
2671
2672 ir_variable *const earlier =
2673 state->symbols->get_variable(decl->identifier);
2674 if (earlier == NULL) {
2675 _mesa_glsl_error(& loc, state,
Paul Berry4d7899f2013-07-25 19:56:43 -07002676 "undeclared variable `%s' cannot be marked "
2677 "invariant", decl->identifier);
Ian Romanick6f0823d2010-07-01 20:39:08 -07002678 } else if ((state->target == vertex_shader)
Paul Berry42a29d82013-01-11 14:39:32 -08002679 && (earlier->mode != ir_var_shader_out)) {
Ian Romanick6f0823d2010-07-01 20:39:08 -07002680 _mesa_glsl_error(& loc, state,
2681 "`%s' cannot be marked invariant, vertex shader "
Paul Berry4d7899f2013-07-25 19:56:43 -07002682 "outputs only", decl->identifier);
Ian Romanick6f0823d2010-07-01 20:39:08 -07002683 } else if ((state->target == fragment_shader)
Paul Berry42a29d82013-01-11 14:39:32 -08002684 && (earlier->mode != ir_var_shader_in)) {
Ian Romanick6f0823d2010-07-01 20:39:08 -07002685 _mesa_glsl_error(& loc, state,
2686 "`%s' cannot be marked invariant, fragment shader "
Paul Berry4d7899f2013-07-25 19:56:43 -07002687 "inputs only", decl->identifier);
Ian Romanickbd330552011-01-07 18:34:58 -08002688 } else if (earlier->used) {
2689 _mesa_glsl_error(& loc, state,
2690 "variable `%s' may not be redeclared "
2691 "`invariant' after being used",
2692 earlier->name);
Ian Romanick6f0823d2010-07-01 20:39:08 -07002693 } else {
2694 earlier->invariant = true;
2695 }
2696 }
2697
2698 /* Invariant redeclarations do not have r-values.
2699 */
2700 return NULL;
2701 }
2702
2703 assert(this->type != NULL);
2704 assert(!this->invariant);
2705
Ian Romanick3455ce62010-04-19 15:13:15 -07002706 /* The type specifier may contain a structure definition. Process that
2707 * before any of the variable declarations.
2708 */
2709 (void) this->type->specifier->hir(instructions, state);
2710
Ian Romanickcabd4572013-08-09 15:17:18 -07002711 decl_type = this->type->glsl_type(& type_name, state);
Ian Romanick304ea902010-05-10 11:17:53 -07002712 if (this->declarations.is_empty()) {
Ian Romanickf5ba4d02011-10-25 17:49:07 -07002713 /* If there is no structure involved in the program text, there are two
2714 * possible scenarios:
2715 *
2716 * - The program text contained something like 'vec4;'. This is an
2717 * empty declaration. It is valid but weird. Emit a warning.
2718 *
2719 * - The program text contained something like 'S;' and 'S' is not the
2720 * name of a known structure type. This is both invalid and weird.
2721 * Emit an error.
2722 *
Ian Romanick830f4df2013-08-09 13:32:40 -07002723 * - The program text contained something like 'mediump float;'
2724 * when the programmer probably meant 'precision mediump
2725 * float;' Emit a warning with a description of what they
2726 * probably meant to do.
2727 *
Ian Romanickf5ba4d02011-10-25 17:49:07 -07002728 * Note that if decl_type is NULL and there is a structure involved,
2729 * there must have been some sort of error with the structure. In this
2730 * case we assume that an error was already generated on this line of
2731 * code for the structure. There is no need to generate an additional,
2732 * confusing error.
2733 */
2734 assert(this->type->specifier->structure == NULL || decl_type != NULL
2735 || state->error);
Kenneth Graunke6eec5022013-07-15 15:58:29 -07002736
Ian Romanick830f4df2013-08-09 13:32:40 -07002737 if (decl_type == NULL) {
2738 _mesa_glsl_error(&loc, state,
2739 "invalid type `%s' in empty declaration",
2740 type_name);
2741 } else if (this->type->qualifier.precision != ast_precision_none) {
2742 if (this->type->specifier->structure != NULL) {
2743 _mesa_glsl_error(&loc, state,
2744 "precision qualifiers can't be applied "
2745 "to structures");
2746 } else {
2747 static const char *const precision_names[] = {
2748 "highp",
2749 "highp",
2750 "mediump",
2751 "lowp"
2752 };
2753
2754 _mesa_glsl_warning(&loc, state,
2755 "empty declaration with precision qualifier, "
2756 "to set the default precision, use "
2757 "`precision %s %s;'",
2758 precision_names[this->type->qualifier.precision],
2759 type_name);
2760 }
2761 } else {
2762 _mesa_glsl_warning(&loc, state, "empty declaration");
Kenneth Graunke6eec5022013-07-15 15:58:29 -07002763 }
Ian Romanickc824e352010-04-23 15:55:19 -07002764 }
Ian Romanicka87ac252010-02-22 13:19:34 -08002765
Ian Romanick2b97dc62010-05-10 17:42:05 -07002766 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
Ian Romanicka87ac252010-02-22 13:19:34 -08002767 const struct glsl_type *var_type;
Ian Romanick768b55a2010-08-13 16:46:43 -07002768 ir_variable *var;
Ian Romanicka87ac252010-02-22 13:19:34 -08002769
2770 /* FINISHME: Emit a warning if a variable declaration shadows a
2771 * FINISHME: declaration at a higher scope.
2772 */
2773
Ian Romanickcec65a62010-03-23 12:28:44 -07002774 if ((decl_type == NULL) || decl_type->is_void()) {
Ian Romanicka87ac252010-02-22 13:19:34 -08002775 if (type_name != NULL) {
2776 _mesa_glsl_error(& loc, state,
2777 "invalid type `%s' in declaration of `%s'",
2778 type_name, decl->identifier);
2779 } else {
2780 _mesa_glsl_error(& loc, state,
2781 "invalid type in declaration of `%s'",
2782 decl->identifier);
2783 }
2784 continue;
2785 }
2786
2787 if (decl->is_array) {
Kenneth Graunked8e34e22010-08-07 02:56:01 -07002788 var_type = process_array_type(&loc, decl_type, decl->array_size,
2789 state);
Ian Romanicka04211e2011-10-24 11:45:50 -07002790 if (var_type->is_error())
2791 continue;
Ian Romanicka87ac252010-02-22 13:19:34 -08002792 } else {
2793 var_type = decl_type;
2794 }
2795
Ian Romanick7e2aa912010-07-19 17:12:42 -07002796 var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto);
Ian Romanicka87ac252010-02-22 13:19:34 -08002797
Bryan Cain25480922013-02-15 09:46:50 -06002798 /* The 'varying in' and 'varying out' qualifiers can only be used with
2799 * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
2800 * yet.
2801 */
2802 if (this->type->qualifier.flags.q.varying) {
2803 if (this->type->qualifier.flags.q.in) {
2804 _mesa_glsl_error(& loc, state,
2805 "`varying in' qualifier in declaration of "
2806 "`%s' only valid for geometry shaders using "
2807 "ARB_geometry_shader4 or EXT_geometry_shader4",
2808 decl->identifier);
2809 } else if (this->type->qualifier.flags.q.out) {
2810 _mesa_glsl_error(& loc, state,
2811 "`varying out' qualifier in declaration of "
2812 "`%s' only valid for geometry shaders using "
2813 "ARB_geometry_shader4 or EXT_geometry_shader4",
2814 decl->identifier);
2815 }
2816 }
2817
Eric Anholt3f151502010-04-02 01:53:57 -10002818 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
2819 *
2820 * "Global variables can only use the qualifiers const,
2821 * attribute, uni form, or varying. Only one may be
2822 * specified.
2823 *
2824 * Local variables can only use the qualifier const."
2825 *
Paul Berryd4a24742012-08-02 08:18:12 -07002826 * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
2827 * any extension that adds the 'layout' keyword.
Eric Anholt3f151502010-04-02 01:53:57 -10002828 */
Paul Berryd4a24742012-08-02 08:18:12 -07002829 if (!state->is_version(130, 300)
Ian Romanick82c4b4f2011-01-07 16:53:07 -08002830 && !state->ARB_explicit_attrib_location_enable
2831 && !state->ARB_fragment_coord_conventions_enable) {
Ian Romanicke24d35a2010-10-05 16:38:47 -07002832 if (this->type->qualifier.flags.q.out) {
Eric Anholt3f151502010-04-02 01:53:57 -10002833 _mesa_glsl_error(& loc, state,
2834 "`out' qualifier in declaration of `%s' "
Paul Berry4d7899f2013-07-25 19:56:43 -07002835 "only valid for function parameters in %s",
Paul Berrydc9f9d82012-08-02 06:45:30 -07002836 decl->identifier, state->get_version_string());
Eric Anholt3f151502010-04-02 01:53:57 -10002837 }
Ian Romanicke24d35a2010-10-05 16:38:47 -07002838 if (this->type->qualifier.flags.q.in) {
Eric Anholt3f151502010-04-02 01:53:57 -10002839 _mesa_glsl_error(& loc, state,
2840 "`in' qualifier in declaration of `%s' "
Paul Berry4d7899f2013-07-25 19:56:43 -07002841 "only valid for function parameters in %s",
Paul Berrydc9f9d82012-08-02 06:45:30 -07002842 decl->identifier, state->get_version_string());
Eric Anholt3f151502010-04-02 01:53:57 -10002843 }
2844 /* FINISHME: Test for other invalid qualifiers. */
2845 }
2846
Eric Anholt2e063f12010-03-28 00:56:22 -07002847 apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
Matt Turner921ef552013-08-15 11:14:00 -07002848 & loc, false);
Ian Romanicka87ac252010-02-22 13:19:34 -08002849
Ian Romanicke24d35a2010-10-05 16:38:47 -07002850 if (this->type->qualifier.flags.q.invariant) {
Paul Berry42a29d82013-01-11 14:39:32 -08002851 if ((state->target == vertex_shader) &&
2852 var->mode != ir_var_shader_out) {
Ian Romanick6f0823d2010-07-01 20:39:08 -07002853 _mesa_glsl_error(& loc, state,
2854 "`%s' cannot be marked invariant, vertex shader "
Paul Berry4d7899f2013-07-25 19:56:43 -07002855 "outputs only", var->name);
Eric Anholt046bef22010-08-04 20:33:57 -07002856 } else if ((state->target == fragment_shader) &&
Paul Berry42a29d82013-01-11 14:39:32 -08002857 var->mode != ir_var_shader_in) {
Eric Anholt046bef22010-08-04 20:33:57 -07002858 /* FINISHME: Note that this doesn't work for invariant on
2859 * a function signature inval
2860 */
Ian Romanick6f0823d2010-07-01 20:39:08 -07002861 _mesa_glsl_error(& loc, state,
2862 "`%s' cannot be marked invariant, fragment shader "
Paul Berry4d7899f2013-07-25 19:56:43 -07002863 "inputs only", var->name);
Ian Romanick6f0823d2010-07-01 20:39:08 -07002864 }
2865 }
2866
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07002867 if (state->current_function != NULL) {
Ian Romanickb168e532010-03-31 12:31:18 -07002868 const char *mode = NULL;
Ian Romanicke0800062010-03-31 13:15:23 -07002869 const char *extra = "";
Ian Romanickb168e532010-03-31 12:31:18 -07002870
Ian Romanicke0800062010-03-31 13:15:23 -07002871 /* There is no need to check for 'inout' here because the parser will
2872 * only allow that in function parameter lists.
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07002873 */
Ian Romanicke24d35a2010-10-05 16:38:47 -07002874 if (this->type->qualifier.flags.q.attribute) {
Ian Romanickb168e532010-03-31 12:31:18 -07002875 mode = "attribute";
Ian Romanicke24d35a2010-10-05 16:38:47 -07002876 } else if (this->type->qualifier.flags.q.uniform) {
Ian Romanickb168e532010-03-31 12:31:18 -07002877 mode = "uniform";
Ian Romanicke24d35a2010-10-05 16:38:47 -07002878 } else if (this->type->qualifier.flags.q.varying) {
Ian Romanickb168e532010-03-31 12:31:18 -07002879 mode = "varying";
Ian Romanicke24d35a2010-10-05 16:38:47 -07002880 } else if (this->type->qualifier.flags.q.in) {
Ian Romanicke0800062010-03-31 13:15:23 -07002881 mode = "in";
2882 extra = " or in function parameter list";
Ian Romanicke24d35a2010-10-05 16:38:47 -07002883 } else if (this->type->qualifier.flags.q.out) {
Ian Romanicke0800062010-03-31 13:15:23 -07002884 mode = "out";
2885 extra = " or in function parameter list";
Ian Romanickb168e532010-03-31 12:31:18 -07002886 }
2887
2888 if (mode) {
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07002889 _mesa_glsl_error(& loc, state,
Ian Romanickb168e532010-03-31 12:31:18 -07002890 "%s variable `%s' must be declared at "
Ian Romanicke0800062010-03-31 13:15:23 -07002891 "global scope%s",
2892 mode, var->name, extra);
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07002893 }
Paul Berry42a29d82013-01-11 14:39:32 -08002894 } else if (var->mode == ir_var_shader_in) {
Chad Versace01a584d2011-01-20 14:12:16 -08002895 var->read_only = true;
2896
Ian Romanickfb9f5b02010-03-29 17:16:35 -07002897 if (state->target == vertex_shader) {
2898 bool error_emitted = false;
2899
2900 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
2901 *
2902 * "Vertex shader inputs can only be float, floating-point
2903 * vectors, matrices, signed and unsigned integers and integer
2904 * vectors. Vertex shader inputs can also form arrays of these
2905 * types, but not structures."
2906 *
Ian Romanick2d816202010-03-29 17:40:11 -07002907 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
2908 *
2909 * "Vertex shader inputs can only be float, floating-point
2910 * vectors, matrices, signed and unsigned integers and integer
2911 * vectors. They cannot be arrays or structures."
2912 *
Ian Romanickfb9f5b02010-03-29 17:16:35 -07002913 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
2914 *
2915 * "The attribute qualifier can be used only with float,
2916 * floating-point vectors, and matrices. Attribute variables
2917 * cannot be declared as arrays or structures."
Paul Berryd4a24742012-08-02 08:18:12 -07002918 *
2919 * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
2920 *
2921 * "Vertex shader inputs can only be float, floating-point
2922 * vectors, matrices, signed and unsigned integers and integer
2923 * vectors. Vertex shader inputs cannot be arrays or
2924 * structures."
Ian Romanickfb9f5b02010-03-29 17:16:35 -07002925 */
2926 const glsl_type *check_type = var->type->is_array()
2927 ? var->type->fields.array : var->type;
2928
2929 switch (check_type->base_type) {
2930 case GLSL_TYPE_FLOAT:
2931 break;
2932 case GLSL_TYPE_UINT:
2933 case GLSL_TYPE_INT:
Paul Berryd4a24742012-08-02 08:18:12 -07002934 if (state->is_version(120, 300))
Ian Romanickfb9f5b02010-03-29 17:16:35 -07002935 break;
2936 /* FALLTHROUGH */
2937 default:
2938 _mesa_glsl_error(& loc, state,
2939 "vertex shader input / attribute cannot have "
2940 "type %s`%s'",
2941 var->type->is_array() ? "array of " : "",
2942 check_type->name);
2943 error_emitted = true;
2944 }
2945
Paul Berry0d9bba62012-08-05 09:57:01 -07002946 if (!error_emitted && var->type->is_array() &&
Paul Berryb2265db2013-07-11 15:40:11 -07002947 !state->check_version(150, 0, &loc,
Paul Berry0d9bba62012-08-05 09:57:01 -07002948 "vertex shader input / attribute "
2949 "cannot have array type")) {
Ian Romanickfb9f5b02010-03-29 17:16:35 -07002950 error_emitted = true;
2951 }
Paul Berry05234e72013-04-10 07:04:33 -07002952 } else if (state->target == geometry_shader) {
2953 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
2954 *
2955 * Geometry shader input variables get the per-vertex values
2956 * written out by vertex shader output variables of the same
2957 * names. Since a geometry shader operates on a set of
2958 * vertices, each input varying variable (or input block, see
2959 * interface blocks below) needs to be declared as an array.
2960 */
2961 if (!var->type->is_array()) {
2962 _mesa_glsl_error(&loc, state,
2963 "geometry shader inputs must be arrays");
2964 }
Paul Berry7cfefe62013-07-30 21:13:48 -07002965
2966 handle_geometry_shader_input_decl(state, loc, var);
Paul Berry05234e72013-04-10 07:04:33 -07002967 }
Ian Romanickfb9f5b02010-03-29 17:16:35 -07002968 }
2969
Paul Berrydfb57e72013-02-08 16:46:20 -08002970 /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
2971 * so must integer vertex outputs.
Chad Versace68d06b12010-12-16 11:06:19 -08002972 *
Paul Berrydfb57e72013-02-08 16:46:20 -08002973 * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
2974 * "Fragment shader inputs that are signed or unsigned integers or
2975 * integer vectors must be qualified with the interpolation qualifier
Chad Versace68d06b12010-12-16 11:06:19 -08002976 * flat."
Paul Berryd4a24742012-08-02 08:18:12 -07002977 *
Paul Berrydfb57e72013-02-08 16:46:20 -08002978 * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
Paul Berry93c91342013-02-06 16:09:39 -08002979 * "Fragment shader inputs that are, or contain, signed or unsigned
2980 * integers or integer vectors must be qualified with the
2981 * interpolation qualifier flat."
Paul Berryd4a24742012-08-02 08:18:12 -07002982 *
Paul Berrydfb57e72013-02-08 16:46:20 -08002983 * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
2984 * "Vertex shader outputs that are, or contain, signed or unsigned
2985 * integers or integer vectors must be qualified with the
2986 * interpolation qualifier flat."
2987 *
2988 * Note that prior to GLSL 1.50, this requirement applied to vertex
2989 * outputs rather than fragment inputs. That creates problems in the
2990 * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
2991 * desktop GL shaders. For GLSL ES shaders, we follow the spec and
2992 * apply the restriction to both vertex outputs and fragment inputs.
2993 *
2994 * Note also that the desktop GLSL specs are missing the text "or
2995 * contain"; this is presumably an oversight, since there is no
2996 * reasonable way to interpolate a fragment shader input that contains
2997 * an integer.
Chad Versace68d06b12010-12-16 11:06:19 -08002998 */
Paul Berrydfb57e72013-02-08 16:46:20 -08002999 if (state->is_version(130, 300) &&
3000 var->type->contains_integer() &&
3001 var->interpolation != INTERP_QUALIFIER_FLAT &&
3002 ((state->target == fragment_shader && var->mode == ir_var_shader_in)
3003 || (state->target == vertex_shader && var->mode == ir_var_shader_out
3004 && state->es_shader))) {
3005 const char *var_type = (state->target == vertex_shader) ?
3006 "vertex output" : "fragment input";
Paul Berry4d7899f2013-07-25 19:56:43 -07003007 _mesa_glsl_error(&loc, state, "if a %s is (or contains) "
Paul Berrydfb57e72013-02-08 16:46:20 -08003008 "an integer, then it must be qualified with 'flat'",
3009 var_type);
Chad Versace68d06b12010-12-16 11:06:19 -08003010 }
3011
3012
Chad Versace605aacc2011-01-11 17:21:18 -08003013 /* Interpolation qualifiers cannot be applied to 'centroid' and
3014 * 'centroid varying'.
3015 *
3016 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
3017 * "interpolation qualifiers may only precede the qualifiers in,
3018 * centroid in, out, or centroid out in a declaration. They do not apply
3019 * to the deprecated storage qualifiers varying or centroid varying."
Paul Berryd4a24742012-08-02 08:18:12 -07003020 *
3021 * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
Chad Versace605aacc2011-01-11 17:21:18 -08003022 */
Paul Berrye3ded7f2012-08-01 14:50:05 -07003023 if (state->is_version(130, 0)
Chad Versace605aacc2011-01-11 17:21:18 -08003024 && this->type->qualifier.has_interpolation()
3025 && this->type->qualifier.flags.q.varying) {
3026
3027 const char *i = this->type->qualifier.interpolation_string();
3028 assert(i != NULL);
3029 const char *s;
3030 if (this->type->qualifier.flags.q.centroid)
3031 s = "centroid varying";
3032 else
3033 s = "varying";
3034
3035 _mesa_glsl_error(&loc, state,
3036 "qualifier '%s' cannot be applied to the "
3037 "deprecated storage qualifier '%s'", i, s);
3038 }
3039
3040
Chad Versace8faaa4a2011-01-11 18:13:26 -08003041 /* Interpolation qualifiers can only apply to vertex shader outputs and
3042 * fragment shader inputs.
3043 *
3044 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
3045 * "Outputs from a vertex shader (out) and inputs to a fragment
3046 * shader (in) can be further qualified with one or more of these
3047 * interpolation qualifiers"
Paul Berryd4a24742012-08-02 08:18:12 -07003048 *
3049 * From page 31 (page 37 of the PDF) of the GLSL ES 3.00 spec:
3050 * "These interpolation qualifiers may only precede the qualifiers
3051 * in, centroid in, out, or centroid out in a declaration. They do
3052 * not apply to inputs into a vertex shader or outputs from a
3053 * fragment shader."
Chad Versace8faaa4a2011-01-11 18:13:26 -08003054 */
Paul Berryd4a24742012-08-02 08:18:12 -07003055 if (state->is_version(130, 300)
Chad Versace8faaa4a2011-01-11 18:13:26 -08003056 && this->type->qualifier.has_interpolation()) {
3057
3058 const char *i = this->type->qualifier.interpolation_string();
3059 assert(i != NULL);
3060
3061 switch (state->target) {
3062 case vertex_shader:
3063 if (this->type->qualifier.flags.q.in) {
3064 _mesa_glsl_error(&loc, state,
3065 "qualifier '%s' cannot be applied to vertex "
3066 "shader inputs", i);
3067 }
3068 break;
3069 case fragment_shader:
3070 if (this->type->qualifier.flags.q.out) {
3071 _mesa_glsl_error(&loc, state,
3072 "qualifier '%s' cannot be applied to fragment "
3073 "shader outputs", i);
3074 }
3075 break;
3076 default:
Bryan Cain25480922013-02-15 09:46:50 -06003077 break;
Chad Versace8faaa4a2011-01-11 18:13:26 -08003078 }
3079 }
3080
3081
Chad Versace1eb0f172011-01-11 18:24:17 -08003082 /* From section 4.3.4 of the GLSL 1.30 spec:
3083 * "It is an error to use centroid in in a vertex shader."
Paul Berryd4a24742012-08-02 08:18:12 -07003084 *
3085 * From section 4.3.4 of the GLSL ES 3.00 spec:
3086 * "It is an error to use centroid in or interpolation qualifiers in
3087 * a vertex shader input."
Chad Versace1eb0f172011-01-11 18:24:17 -08003088 */
Paul Berryd4a24742012-08-02 08:18:12 -07003089 if (state->is_version(130, 300)
Chad Versace1eb0f172011-01-11 18:24:17 -08003090 && this->type->qualifier.flags.q.centroid
3091 && this->type->qualifier.flags.q.in
3092 && state->target == vertex_shader) {
3093
3094 _mesa_glsl_error(&loc, state,
3095 "'centroid in' cannot be used in a vertex shader");
3096 }
3097
Kenneth Graunke17856722013-07-26 21:18:56 -07003098 /* Section 4.3.6 of the GLSL 1.30 specification states:
3099 * "It is an error to use centroid out in a fragment shader."
3100 *
3101 * The GL_ARB_shading_language_420pack extension specification states:
3102 * "It is an error to use auxiliary storage qualifiers or interpolation
3103 * qualifiers on an output in a fragment shader."
3104 */
3105 if (state->target == fragment_shader &&
3106 this->type->qualifier.flags.q.out &&
3107 this->type->qualifier.has_auxiliary_storage()) {
3108 _mesa_glsl_error(&loc, state,
3109 "auxiliary storage qualifiers cannot be used on "
3110 "fragment shader outputs");
3111 }
Chad Versace1eb0f172011-01-11 18:24:17 -08003112
Chad Versace889e1a52011-01-16 22:38:45 -08003113 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
3114 */
Kenneth Graunke6eec5022013-07-15 15:58:29 -07003115 if (this->type->qualifier.precision != ast_precision_none) {
Paul Berry0d9bba62012-08-05 09:57:01 -07003116 state->check_precision_qualifiers_allowed(&loc);
Chad Versace889e1a52011-01-16 22:38:45 -08003117 }
3118
3119
Chad Versace45e8e6c2011-01-17 15:28:39 -08003120 /* Precision qualifiers only apply to floating point and integer types.
Chad Versace889e1a52011-01-16 22:38:45 -08003121 *
3122 * From section 4.5.2 of the GLSL 1.30 spec:
3123 * "Any floating point or any integer declaration can have the type
3124 * preceded by one of these precision qualifiers [...] Literal
3125 * constants do not have precision qualifiers. Neither do Boolean
3126 * variables.
Kenneth Graunke87528242011-03-26 23:37:09 -07003127 *
3128 * In GLSL ES, sampler types are also allowed.
3129 *
3130 * From page 87 of the GLSL ES spec:
3131 * "RESOLUTION: Allow sampler types to take a precision qualifier."
Chad Versace889e1a52011-01-16 22:38:45 -08003132 */
Kenneth Graunke6eec5022013-07-15 15:58:29 -07003133 if (this->type->qualifier.precision != ast_precision_none
Chad Versace45e8e6c2011-01-17 15:28:39 -08003134 && !var->type->is_float()
3135 && !var->type->is_integer()
Kenneth Graunke6eec5022013-07-15 15:58:29 -07003136 && !var->type->is_record()
Kenneth Graunke87528242011-03-26 23:37:09 -07003137 && !(var->type->is_sampler() && state->es_shader)
Chad Versace45e8e6c2011-01-17 15:28:39 -08003138 && !(var->type->is_array()
3139 && (var->type->fields.array->is_float()
3140 || var->type->fields.array->is_integer()))) {
Chad Versace889e1a52011-01-16 22:38:45 -08003141
3142 _mesa_glsl_error(&loc, state,
Kenneth Graunke87528242011-03-26 23:37:09 -07003143 "precision qualifiers apply only to floating point"
3144 "%s types", state->es_shader ? ", integer, and sampler"
3145 : "and integer");
Chad Versace889e1a52011-01-16 22:38:45 -08003146 }
3147
Paul Berryf0722102011-07-12 12:03:02 -07003148 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3149 *
3150 * "[Sampler types] can only be declared as function
3151 * parameters or uniform variables (see Section 4.3.5
3152 * "Uniform")".
3153 */
3154 if (var_type->contains_sampler() &&
3155 !this->type->qualifier.flags.q.uniform) {
3156 _mesa_glsl_error(&loc, state, "samplers must be declared uniform");
3157 }
3158
Ian Romanicke78e0fa2010-07-07 12:13:34 -07003159 /* Process the initializer and add its instructions to a temporary
3160 * list. This list will be added to the instruction stream (below) after
3161 * the declaration is added. This is done because in some cases (such as
3162 * redeclarations) the declaration may not actually be added to the
3163 * instruction stream.
3164 */
Eric Anholtfa33d0b2010-07-29 13:50:17 -07003165 exec_list initializer_instructions;
Ian Romanick09a4ba02011-03-04 16:15:20 -08003166 ir_variable *earlier = get_variable_being_redeclared(var, decl, state);
3167
Ian Romanick66faec42010-03-27 18:56:53 -07003168 if (decl->initializer != NULL) {
Ian Romanick09a4ba02011-03-04 16:15:20 -08003169 result = process_initializer((earlier == NULL) ? var : earlier,
3170 decl, this->type,
Ian Romanick0292ffb2011-03-04 15:29:33 -08003171 &initializer_instructions, state);
Ian Romanick19360152010-03-26 18:05:27 -07003172 }
Ian Romanick17d86f42010-03-29 12:59:02 -07003173
Eric Anholt0ed61252010-03-31 09:29:33 -10003174 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
3175 *
3176 * "It is an error to write to a const variable outside of
3177 * its declaration, so they must be initialized when
3178 * declared."
3179 */
Ian Romanicke24d35a2010-10-05 16:38:47 -07003180 if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) {
Eric Anholt0ed61252010-03-31 09:29:33 -10003181 _mesa_glsl_error(& loc, state,
Chad Versace46f71052011-01-18 15:15:19 -08003182 "const declaration of `%s' must be initialized",
3183 decl->identifier);
Eric Anholt0ed61252010-03-31 09:29:33 -10003184 }
3185
Ian Romanick42624b12013-08-08 17:23:01 -07003186 if (state->es_shader) {
3187 const glsl_type *const t = (earlier == NULL)
3188 ? var->type : earlier->type;
3189
3190 if (t->is_array() && t->length == 0)
3191 /* Section 10.17 of the GLSL ES 1.00 specification states that
3192 * unsized array declarations have been removed from the language.
3193 * Arrays that are sized using an initializer are still explicitly
3194 * sized. However, GLSL ES 1.00 does not allow array
3195 * initializers. That is only allowed in GLSL ES 3.00.
3196 *
3197 * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
3198 *
3199 * "An array type can also be formed without specifying a size
3200 * if the definition includes an initializer:
3201 *
3202 * float x[] = float[2] (1.0, 2.0); // declares an array of size 2
3203 * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
3204 *
3205 * float a[5];
3206 * float b[] = a;"
3207 */
3208 _mesa_glsl_error(& loc, state,
3209 "unsized array declarations are not allowed in "
3210 "GLSL ES");
3211 }
3212
Ian Romanick09a4ba02011-03-04 16:15:20 -08003213 /* If the declaration is not a redeclaration, there are a few additional
3214 * semantic checks that must be applied. In addition, variable that was
3215 * created for the declaration should be added to the IR stream.
3216 */
3217 if (earlier == NULL) {
3218 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
3219 *
3220 * "Identifiers starting with "gl_" are reserved for use by
3221 * OpenGL, and may not be declared in a shader as either a
3222 * variable or a function."
3223 */
3224 if (strncmp(decl->identifier, "gl_", 3) == 0)
3225 _mesa_glsl_error(& loc, state,
3226 "identifier `%s' uses reserved `gl_' prefix",
3227 decl->identifier);
Jason Woodc475a542011-10-06 22:37:48 -07003228 else if (strstr(decl->identifier, "__")) {
Eric Anholt684b7012011-10-03 16:27:59 -07003229 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
3230 * spec:
3231 *
3232 * "In addition, all identifiers containing two
3233 * consecutive underscores (__) are reserved as
3234 * possible future keywords."
3235 */
3236 _mesa_glsl_error(& loc, state,
3237 "identifier `%s' uses reserved `__' string",
3238 decl->identifier);
3239 }
Ian Romanick09a4ba02011-03-04 16:15:20 -08003240
3241 /* Add the variable to the symbol table. Note that the initializer's
3242 * IR was already processed earlier (though it hasn't been emitted
3243 * yet), without the variable in scope.
3244 *
3245 * This differs from most C-like languages, but it follows the GLSL
3246 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
3247 * spec:
3248 *
3249 * "Within a declaration, the scope of a name starts immediately
3250 * after the initializer if present or immediately after the name
3251 * being declared if not."
3252 */
3253 if (!state->symbols->add_variable(var)) {
3254 YYLTYPE loc = this->get_location();
3255 _mesa_glsl_error(&loc, state, "name `%s' already taken in the "
3256 "current scope", decl->identifier);
3257 continue;
3258 }
3259
3260 /* Push the variable declaration to the top. It means that all the
3261 * variable declarations will appear in a funny last-to-first order,
3262 * but otherwise we run into trouble if a function is prototyped, a
3263 * global var is decled, then the function is defined with usage of
3264 * the global var. See glslparsertest's CorrectModule.frag.
3265 */
3266 instructions->push_head(var);
Ian Romanick5466b632010-07-01 12:46:55 -07003267 }
3268
Eric Anholtfa33d0b2010-07-29 13:50:17 -07003269 instructions->append_list(&initializer_instructions);
Ian Romanicka87ac252010-02-22 13:19:34 -08003270 }
3271
Eric Anholt85584592010-04-14 15:38:52 -07003272
3273 /* Generally, variable declarations do not have r-values. However,
3274 * one is used for the declaration in
3275 *
3276 * while (bool b = some_condition()) {
3277 * ...
3278 * }
3279 *
3280 * so we return the rvalue from the last seen declaration here.
Ian Romanicka87ac252010-02-22 13:19:34 -08003281 */
Eric Anholt85584592010-04-14 15:38:52 -07003282 return result;
Ian Romanicka87ac252010-02-22 13:19:34 -08003283}
3284
3285
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07003286ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08003287ast_parameter_declarator::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08003288 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08003289{
Kenneth Graunke953ff122010-06-25 13:14:37 -07003290 void *ctx = state;
Ian Romanicka87ac252010-02-22 13:19:34 -08003291 const struct glsl_type *type;
3292 const char *name = NULL;
Eric Anholt2e063f12010-03-28 00:56:22 -07003293 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08003294
Ian Romanickcabd4572013-08-09 15:17:18 -07003295 type = this->type->glsl_type(& name, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08003296
3297 if (type == NULL) {
Ian Romanicka87ac252010-02-22 13:19:34 -08003298 if (name != NULL) {
3299 _mesa_glsl_error(& loc, state,
3300 "invalid type `%s' in declaration of `%s'",
Ian Romanick18238de2010-03-01 13:49:10 -08003301 name, this->identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08003302 } else {
3303 _mesa_glsl_error(& loc, state,
3304 "invalid type in declaration of `%s'",
Ian Romanick18238de2010-03-01 13:49:10 -08003305 this->identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08003306 }
3307
Ian Romanick0471e8b2010-03-26 14:33:41 -07003308 type = glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -08003309 }
3310
Eric Anholt068c80c2010-03-31 09:56:36 -10003311 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
3312 *
3313 * "Functions that accept no input arguments need not use void in the
3314 * argument list because prototypes (or definitions) are required and
3315 * therefore there is no ambiguity when an empty argument list "( )" is
3316 * declared. The idiom "(void)" as a parameter list is provided for
3317 * convenience."
3318 *
3319 * Placing this check here prevents a void parameter being set up
3320 * for a function, which avoids tripping up checks for main taking
3321 * parameters and lookups of an unnamed symbol.
3322 */
Ian Romanickcf37c9e2010-04-02 15:30:45 -07003323 if (type->is_void()) {
3324 if (this->identifier != NULL)
3325 _mesa_glsl_error(& loc, state,
3326 "named parameter cannot have type `void'");
3327
3328 is_void = true;
Eric Anholt068c80c2010-03-31 09:56:36 -10003329 return NULL;
Ian Romanickcf37c9e2010-04-02 15:30:45 -07003330 }
Eric Anholt068c80c2010-03-31 09:56:36 -10003331
Ian Romanick45d8a702010-04-02 15:09:33 -07003332 if (formal_parameter && (this->identifier == NULL)) {
3333 _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
3334 return NULL;
3335 }
3336
Kenneth Graunkee511a352010-08-21 15:30:34 -07003337 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
3338 * call already handled the "vec4[..] foo" case.
3339 */
3340 if (this->is_array) {
Kenneth Graunked8e34e22010-08-07 02:56:01 -07003341 type = process_array_type(&loc, type, this->array_size, state);
Kenneth Graunkee511a352010-08-21 15:30:34 -07003342 }
3343
Ian Romanicka04211e2011-10-24 11:45:50 -07003344 if (!type->is_error() && type->array_size() == 0) {
Kenneth Graunkee511a352010-08-21 15:30:34 -07003345 _mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
Paul Berry4d7899f2013-07-25 19:56:43 -07003346 "a declared size");
Kenneth Graunkee511a352010-08-21 15:30:34 -07003347 type = glsl_type::error_type;
3348 }
3349
Ian Romanickcf37c9e2010-04-02 15:30:45 -07003350 is_void = false;
Paul Berry42a29d82013-01-11 14:39:32 -08003351 ir_variable *var = new(ctx)
3352 ir_variable(type, this->identifier, ir_var_function_in);
Ian Romanicka87ac252010-02-22 13:19:34 -08003353
Ian Romanickcdb8d542010-03-11 14:48:51 -08003354 /* Apply any specified qualifiers to the parameter declaration. Note that
3355 * for function parameters the default mode is 'in'.
3356 */
Eric Anholtf7561e82012-04-26 18:19:39 -07003357 apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc,
Matt Turner921ef552013-08-15 11:14:00 -07003358 true);
Ian Romanicka87ac252010-02-22 13:19:34 -08003359
Paul Berryf0722102011-07-12 12:03:02 -07003360 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3361 *
3362 * "Samplers cannot be treated as l-values; hence cannot be used
3363 * as out or inout function parameters, nor can they be assigned
3364 * into."
3365 */
Paul Berry42a29d82013-01-11 14:39:32 -08003366 if ((var->mode == ir_var_function_inout || var->mode == ir_var_function_out)
Paul Berryf0722102011-07-12 12:03:02 -07003367 && type->contains_sampler()) {
3368 _mesa_glsl_error(&loc, state, "out and inout parameters cannot contain samplers");
3369 type = glsl_type::error_type;
3370 }
3371
Paul Berry00792e32011-09-10 07:48:46 -07003372 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
3373 *
3374 * "When calling a function, expressions that do not evaluate to
3375 * l-values cannot be passed to parameters declared as out or inout."
3376 *
3377 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
3378 *
3379 * "Other binary or unary expressions, non-dereferenced arrays,
3380 * function names, swizzles with repeated fields, and constants
3381 * cannot be l-values."
3382 *
3383 * So for GLSL 1.10, passing an array as an out or inout parameter is not
3384 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
3385 */
Paul Berry42a29d82013-01-11 14:39:32 -08003386 if ((var->mode == ir_var_function_inout || var->mode == ir_var_function_out)
Paul Berry0d9bba62012-08-05 09:57:01 -07003387 && type->is_array()
3388 && !state->check_version(120, 100, &loc,
Paul Berry4d7899f2013-07-25 19:56:43 -07003389 "arrays cannot be out or inout parameters")) {
Paul Berry00792e32011-09-10 07:48:46 -07003390 type = glsl_type::error_type;
3391 }
3392
Ian Romanick0044e7e2010-03-08 23:44:00 -08003393 instructions->push_tail(var);
Ian Romanicka87ac252010-02-22 13:19:34 -08003394
3395 /* Parameter declarations do not have r-values.
3396 */
3397 return NULL;
3398}
3399
3400
Ian Romanick45d8a702010-04-02 15:09:33 -07003401void
Ian Romanick304ea902010-05-10 11:17:53 -07003402ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
Ian Romanick45d8a702010-04-02 15:09:33 -07003403 bool formal,
3404 exec_list *ir_parameters,
3405 _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08003406{
Ian Romanickcf37c9e2010-04-02 15:30:45 -07003407 ast_parameter_declarator *void_param = NULL;
3408 unsigned count = 0;
Ian Romanicka87ac252010-02-22 13:19:34 -08003409
Ian Romanick2b97dc62010-05-10 17:42:05 -07003410 foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
Ian Romanick45d8a702010-04-02 15:09:33 -07003411 param->formal_parameter = formal;
Eric Anholt068c80c2010-03-31 09:56:36 -10003412 param->hir(ir_parameters, state);
Ian Romanickcf37c9e2010-04-02 15:30:45 -07003413
3414 if (param->is_void)
3415 void_param = param;
3416
3417 count++;
3418 }
3419
3420 if ((void_param != NULL) && (count > 1)) {
3421 YYLTYPE loc = void_param->get_location();
3422
3423 _mesa_glsl_error(& loc, state,
3424 "`void' parameter must be only parameter");
Ian Romanicka87ac252010-02-22 13:19:34 -08003425 }
3426}
3427
3428
Kenneth Graunke6fae1e42010-12-06 10:54:05 -08003429void
Paul Berry0d81b0e2011-07-29 15:28:52 -07003430emit_function(_mesa_glsl_parse_state *state, ir_function *f)
Kenneth Graunke6fae1e42010-12-06 10:54:05 -08003431{
Paul Berry0d81b0e2011-07-29 15:28:52 -07003432 /* IR invariants disallow function declarations or definitions
3433 * nested within other function definitions. But there is no
3434 * requirement about the relative order of function declarations
3435 * and definitions with respect to one another. So simply insert
3436 * the new ir_function block at the end of the toplevel instruction
3437 * list.
3438 */
3439 state->toplevel_ir->push_tail(f);
Kenneth Graunke6fae1e42010-12-06 10:54:05 -08003440}
3441
3442
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07003443ir_rvalue *
Ian Romanick92318a92010-03-31 18:23:21 -07003444ast_function::hir(exec_list *instructions,
3445 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08003446{
Kenneth Graunke953ff122010-06-25 13:14:37 -07003447 void *ctx = state;
Ian Romanick18238de2010-03-01 13:49:10 -08003448 ir_function *f = NULL;
Ian Romanick92318a92010-03-31 18:23:21 -07003449 ir_function_signature *sig = NULL;
3450 exec_list hir_parameters;
Ian Romanicka87ac252010-02-22 13:19:34 -08003451
Kenneth Graunkeac04c252010-06-29 00:48:10 -07003452 const char *const name = identifier;
Ian Romanicka87ac252010-02-22 13:19:34 -08003453
Ian Romanick9a3bd5e2011-08-29 14:56:29 -07003454 /* New functions are always added to the top-level IR instruction stream,
3455 * so this instruction list pointer is ignored. See also emit_function
3456 * (called below).
3457 */
3458 (void) instructions;
3459
Ian Romanick63b80f82010-09-01 06:34:58 -07003460 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
3461 *
3462 * "Function declarations (prototypes) cannot occur inside of functions;
3463 * they must be at global scope, or for the built-in functions, outside
3464 * the global scope."
3465 *
3466 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
3467 *
3468 * "User defined functions may only be defined within the global scope."
3469 *
3470 * Note that this language does not appear in GLSL 1.10.
3471 */
Paul Berrye3ded7f2012-08-01 14:50:05 -07003472 if ((state->current_function != NULL) &&
3473 state->is_version(120, 100)) {
Ian Romanick63b80f82010-09-01 06:34:58 -07003474 YYLTYPE loc = this->get_location();
3475 _mesa_glsl_error(&loc, state,
3476 "declaration of function `%s' not allowed within "
3477 "function body", name);
3478 }
3479
Kenneth Graunkeedd180f2010-08-20 02:14:35 -07003480 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
3481 *
3482 * "Identifiers starting with "gl_" are reserved for use by
3483 * OpenGL, and may not be declared in a shader as either a
3484 * variable or a function."
3485 */
3486 if (strncmp(name, "gl_", 3) == 0) {
3487 YYLTYPE loc = this->get_location();
3488 _mesa_glsl_error(&loc, state,
3489 "identifier `%s' uses reserved `gl_' prefix", name);
3490 }
3491
Ian Romanicka87ac252010-02-22 13:19:34 -08003492 /* Convert the list of function parameters to HIR now so that they can be
3493 * used below to compare this function's signature with previously seen
3494 * signatures for functions with the same name.
3495 */
Ian Romanick45d8a702010-04-02 15:09:33 -07003496 ast_parameter_declarator::parameters_to_hir(& this->parameters,
3497 is_definition,
3498 & hir_parameters, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08003499
Ian Romanicke39cc692010-03-23 12:19:13 -07003500 const char *return_type_name;
3501 const glsl_type *return_type =
Ian Romanickcabd4572013-08-09 15:17:18 -07003502 this->return_type->glsl_type(& return_type_name, state);
Ian Romanicke39cc692010-03-23 12:19:13 -07003503
Eric Anholt76e96d72010-08-23 13:26:52 -07003504 if (!return_type) {
3505 YYLTYPE loc = this->get_location();
3506 _mesa_glsl_error(&loc, state,
3507 "function `%s' has undeclared return type `%s'",
3508 name, return_type_name);
3509 return_type = glsl_type::error_type;
3510 }
Ian Romanicke39cc692010-03-23 12:19:13 -07003511
Kenneth Graunkeac04c252010-06-29 00:48:10 -07003512 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
3513 * "No qualifier is allowed on the return type of a function."
3514 */
3515 if (this->return_type->has_qualifiers()) {
3516 YYLTYPE loc = this->get_location();
3517 _mesa_glsl_error(& loc, state,
3518 "function `%s' return type has qualifiers", name);
3519 }
3520
Ian Romanick1b35e332013-08-08 17:40:38 -07003521 /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
3522 *
3523 * "Arrays are allowed as arguments and as the return type. In both
3524 * cases, the array must be explicitly sized."
3525 */
3526 if (return_type->is_array() && return_type->length == 0) {
3527 YYLTYPE loc = this->get_location();
3528 _mesa_glsl_error(& loc, state,
3529 "function `%s' return type array must be explicitly "
3530 "sized", name);
3531 }
3532
Paul Berryf0722102011-07-12 12:03:02 -07003533 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3534 *
3535 * "[Sampler types] can only be declared as function parameters
3536 * or uniform variables (see Section 4.3.5 "Uniform")".
3537 */
3538 if (return_type->contains_sampler()) {
3539 YYLTYPE loc = this->get_location();
3540 _mesa_glsl_error(&loc, state,
3541 "function `%s' return type can't contain a sampler",
3542 name);
3543 }
3544
Ian Romanicka87ac252010-02-22 13:19:34 -08003545 /* Verify that this function's signature either doesn't match a previously
3546 * seen signature for a function with the same name, or, if a match is found,
3547 * that the previously seen signature does not have an associated definition.
3548 */
Ian Romanicke466b182010-09-01 14:16:53 -07003549 f = state->symbols->get_function(name);
Kenneth Graunke81f03392010-09-16 02:52:25 -07003550 if (f != NULL && (state->es_shader || f->has_user_signature())) {
Ian Romanick202604e2010-08-11 16:58:25 -07003551 sig = f->exact_matching_signature(&hir_parameters);
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07003552 if (sig != NULL) {
3553 const char *badvar = sig->qualifiers_match(&hir_parameters);
3554 if (badvar != NULL) {
3555 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08003556
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07003557 _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
3558 "qualifiers don't match prototype", name, badvar);
Ian Romanicka87ac252010-02-22 13:19:34 -08003559 }
3560
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07003561 if (sig->return_type != return_type) {
3562 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08003563
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07003564 _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
3565 "match prototype", name);
3566 }
3567
Kenneth Graunke6c5cf8b2013-04-30 00:58:09 -07003568 if (sig->is_defined) {
3569 if (is_definition) {
3570 YYLTYPE loc = this->get_location();
3571 _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
3572 } else {
3573 /* We just encountered a prototype that exactly matches a
3574 * function that's already been defined. This is redundant,
3575 * and we should ignore it.
3576 */
3577 return NULL;
3578 }
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07003579 }
3580 }
Ian Romanicka87ac252010-02-22 13:19:34 -08003581 } else {
Carl Worth1660a292010-06-23 18:11:51 -07003582 f = new(ctx) ir_function(name);
Eric Anholte8f5ebf2010-11-05 06:08:45 -07003583 if (!state->symbols->add_function(f)) {
Kenneth Graunkee0959132010-08-25 16:37:46 -07003584 /* This function name shadows a non-function use of the same name. */
3585 YYLTYPE loc = this->get_location();
3586
3587 _mesa_glsl_error(&loc, state, "function name `%s' conflicts with "
3588 "non-function", name);
3589 return NULL;
3590 }
Kenneth Graunke9fa99f32010-04-21 12:30:22 -07003591
Paul Berry0d81b0e2011-07-29 15:28:52 -07003592 emit_function(state, f);
Ian Romanicka87ac252010-02-22 13:19:34 -08003593 }
3594
Eric Anholtab372da2010-03-28 01:24:55 -07003595 /* Verify the return type of main() */
3596 if (strcmp(name, "main") == 0) {
Ian Romanick25711a82010-03-31 17:39:10 -07003597 if (! return_type->is_void()) {
Eric Anholtab372da2010-03-28 01:24:55 -07003598 YYLTYPE loc = this->get_location();
3599
3600 _mesa_glsl_error(& loc, state, "main() must return void");
3601 }
Eric Anholt174cc032010-03-30 23:37:51 -10003602
Ian Romanick92318a92010-03-31 18:23:21 -07003603 if (!hir_parameters.is_empty()) {
Eric Anholt174cc032010-03-30 23:37:51 -10003604 YYLTYPE loc = this->get_location();
3605
3606 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
3607 }
Eric Anholtab372da2010-03-28 01:24:55 -07003608 }
Ian Romanicka87ac252010-02-22 13:19:34 -08003609
3610 /* Finish storing the information about this new function in its signature.
3611 */
Ian Romanick92318a92010-03-31 18:23:21 -07003612 if (sig == NULL) {
Carl Worth1660a292010-06-23 18:11:51 -07003613 sig = new(ctx) ir_function_signature(return_type);
Ian Romanick92318a92010-03-31 18:23:21 -07003614 f->add_signature(sig);
Ian Romanicka87ac252010-02-22 13:19:34 -08003615 }
3616
Kenneth Graunkebff60132010-04-28 12:44:24 -07003617 sig->replace_parameters(&hir_parameters);
Ian Romanick92318a92010-03-31 18:23:21 -07003618 signature = sig;
Ian Romanicke29a5852010-03-31 17:54:26 -07003619
Ian Romanick92318a92010-03-31 18:23:21 -07003620 /* Function declarations (prototypes) do not have r-values.
3621 */
3622 return NULL;
3623}
3624
3625
3626ir_rvalue *
3627ast_function_definition::hir(exec_list *instructions,
3628 struct _mesa_glsl_parse_state *state)
3629{
3630 prototype->is_definition = true;
3631 prototype->hir(instructions, state);
3632
3633 ir_function_signature *signature = prototype->signature;
Kenneth Graunke826a39c2010-08-20 02:04:52 -07003634 if (signature == NULL)
3635 return NULL;
Ian Romanicka87ac252010-02-22 13:19:34 -08003636
Ian Romanick41ec6a42010-03-19 17:08:05 -07003637 assert(state->current_function == NULL);
3638 state->current_function = signature;
Kenneth Graunke6de82562010-06-29 09:59:40 -07003639 state->found_return = false;
Ian Romanick41ec6a42010-03-19 17:08:05 -07003640
Ian Romanicke29a5852010-03-31 17:54:26 -07003641 /* Duplicate parameters declared in the prototype as concrete variables.
3642 * Add these to the symbol table.
Ian Romanicka87ac252010-02-22 13:19:34 -08003643 */
Ian Romanick8bde4ce2010-03-19 11:57:24 -07003644 state->symbols->push_scope();
Ian Romanicke29a5852010-03-31 17:54:26 -07003645 foreach_iter(exec_list_iterator, iter, signature->parameters) {
Eric Anholtfbc7c0b2010-04-07 14:32:53 -07003646 ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
Ian Romanicka87ac252010-02-22 13:19:34 -08003647
Eric Anholtfbc7c0b2010-04-07 14:32:53 -07003648 assert(var != NULL);
Ian Romanicka87ac252010-02-22 13:19:34 -08003649
Ian Romanick3359e582010-03-19 15:38:52 -07003650 /* The only way a parameter would "exist" is if two parameters have
3651 * the same name.
3652 */
3653 if (state->symbols->name_declared_this_scope(var->name)) {
3654 YYLTYPE loc = this->get_location();
3655
3656 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
3657 } else {
Eric Anholt001eee52010-11-05 06:11:24 -07003658 state->symbols->add_variable(var);
Ian Romanick3359e582010-03-19 15:38:52 -07003659 }
Ian Romanicka87ac252010-02-22 13:19:34 -08003660 }
3661
Kenneth Graunke9fa99f32010-04-21 12:30:22 -07003662 /* Convert the body of the function to HIR. */
Eric Anholt894ea972010-04-07 13:19:11 -07003663 this->body->hir(&signature->body, state);
Kenneth Graunke9fa99f32010-04-21 12:30:22 -07003664 signature->is_defined = true;
Ian Romanicka87ac252010-02-22 13:19:34 -08003665
Ian Romanick8bde4ce2010-03-19 11:57:24 -07003666 state->symbols->pop_scope();
Ian Romanicka87ac252010-02-22 13:19:34 -08003667
Ian Romanick41ec6a42010-03-19 17:08:05 -07003668 assert(state->current_function == signature);
3669 state->current_function = NULL;
Ian Romanicka87ac252010-02-22 13:19:34 -08003670
Kenneth Graunke6de82562010-06-29 09:59:40 -07003671 if (!signature->return_type->is_void() && !state->found_return) {
3672 YYLTYPE loc = this->get_location();
3673 _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
3674 "%s, but no return statement",
3675 signature->function_name(),
3676 signature->return_type->name);
3677 }
3678
Ian Romanicka87ac252010-02-22 13:19:34 -08003679 /* Function definitions do not have r-values.
3680 */
3681 return NULL;
3682}
Ian Romanick16a246c2010-03-19 16:45:19 -07003683
3684
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07003685ir_rvalue *
Ian Romanick16a246c2010-03-19 16:45:19 -07003686ast_jump_statement::hir(exec_list *instructions,
3687 struct _mesa_glsl_parse_state *state)
3688{
Kenneth Graunke953ff122010-06-25 13:14:37 -07003689 void *ctx = state;
Ian Romanick16a246c2010-03-19 16:45:19 -07003690
Ian Romanickc0e76d82010-04-05 16:53:19 -07003691 switch (mode) {
3692 case ast_return: {
Ian Romanick16a246c2010-03-19 16:45:19 -07003693 ir_return *inst;
Eric Anholtaad7c772010-03-30 23:28:20 -10003694 assert(state->current_function);
Ian Romanick16a246c2010-03-19 16:45:19 -07003695
3696 if (opt_return_value) {
Matt Turner1a1b03e2013-05-22 12:14:32 -07003697 ir_rvalue *ret = opt_return_value->hir(instructions, state);
Ian Romanick2db46fe2011-01-22 17:47:05 -08003698
3699 /* The value of the return type can be NULL if the shader says
3700 * 'return foo();' and foo() is a function that returns void.
3701 *
3702 * NOTE: The GLSL spec doesn't say that this is an error. The type
3703 * of the return value is void. If the return type of the function is
3704 * also void, then this should compile without error. Seriously.
3705 */
3706 const glsl_type *const ret_type =
3707 (ret == NULL) ? glsl_type::void_type : ret->type;
Ian Romanick16a246c2010-03-19 16:45:19 -07003708
Matt Turner1a1b03e2013-05-22 12:14:32 -07003709 /* Implicit conversions are not allowed for return values prior to
3710 * ARB_shading_language_420pack.
3711 */
3712 if (state->current_function->return_type != ret_type) {
Kenneth Graunke18707eb2010-06-28 23:38:04 -07003713 YYLTYPE loc = this->get_location();
3714
Matt Turner1a1b03e2013-05-22 12:14:32 -07003715 if (state->ARB_shading_language_420pack_enable) {
3716 if (!apply_implicit_conversion(state->current_function->return_type,
3717 ret, state)) {
3718 _mesa_glsl_error(& loc, state,
Paul Berry4d7899f2013-07-25 19:56:43 -07003719 "could not implicitly convert return value "
Matt Turner1a1b03e2013-05-22 12:14:32 -07003720 "to %s, in function `%s'",
3721 state->current_function->return_type->name,
3722 state->current_function->function_name());
3723 }
3724 } else {
3725 _mesa_glsl_error(& loc, state,
3726 "`return' with wrong type %s, in function `%s' "
3727 "returning %s",
3728 ret_type->name,
3729 state->current_function->function_name(),
3730 state->current_function->return_type->name);
3731 }
Matt Turnerfcaa48d2013-05-22 14:57:04 -07003732 } else if (state->current_function->return_type->base_type ==
3733 GLSL_TYPE_VOID) {
3734 YYLTYPE loc = this->get_location();
3735
3736 /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
3737 * specs add a clarification:
3738 *
3739 * "A void function can only use return without a return argument, even if
3740 * the return argument has void type. Return statements only accept values:
3741 *
3742 * void func1() { }
3743 * void func2() { return func1(); } // illegal return statement"
3744 */
3745 _mesa_glsl_error(& loc, state,
3746 "void functions can only use `return' without a "
3747 "return argument");
3748 }
Ian Romanick16a246c2010-03-19 16:45:19 -07003749
Carl Worth1660a292010-06-23 18:11:51 -07003750 inst = new(ctx) ir_return(ret);
Ian Romanick16a246c2010-03-19 16:45:19 -07003751 } else {
Eric Anholtaad7c772010-03-30 23:28:20 -10003752 if (state->current_function->return_type->base_type !=
3753 GLSL_TYPE_VOID) {
3754 YYLTYPE loc = this->get_location();
3755
3756 _mesa_glsl_error(& loc, state,
3757 "`return' with no value, in function %s returning "
3758 "non-void",
Kenneth Graunkef96c52b2010-04-21 15:17:26 -07003759 state->current_function->function_name());
Eric Anholtaad7c772010-03-30 23:28:20 -10003760 }
Carl Worth1660a292010-06-23 18:11:51 -07003761 inst = new(ctx) ir_return;
Ian Romanick16a246c2010-03-19 16:45:19 -07003762 }
3763
Kenneth Graunke6de82562010-06-29 09:59:40 -07003764 state->found_return = true;
Ian Romanick16a246c2010-03-19 16:45:19 -07003765 instructions->push_tail(inst);
Ian Romanickc0e76d82010-04-05 16:53:19 -07003766 break;
Ian Romanick16a246c2010-03-19 16:45:19 -07003767 }
3768
Ian Romanickc0e76d82010-04-05 16:53:19 -07003769 case ast_discard:
Eric Anholtb9802072010-03-30 23:40:14 -10003770 if (state->target != fragment_shader) {
3771 YYLTYPE loc = this->get_location();
3772
3773 _mesa_glsl_error(& loc, state,
3774 "`discard' may only appear in a fragment shader");
3775 }
Kenneth Graunke77049a72010-06-30 14:11:00 -07003776 instructions->push_tail(new(ctx) ir_discard);
Ian Romanickc0e76d82010-04-05 16:53:19 -07003777 break;
3778
3779 case ast_break:
3780 case ast_continue:
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003781 if (mode == ast_continue &&
3782 state->loop_nesting_ast == NULL) {
Ian Romanick4cf20cd2010-04-05 17:13:47 -07003783 YYLTYPE loc = this->get_location();
3784
3785 _mesa_glsl_error(& loc, state,
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003786 "continue may only appear in a loop");
3787 } else if (mode == ast_break &&
3788 state->loop_nesting_ast == NULL &&
Eric Anholt22d81f12012-01-28 11:26:02 -08003789 state->switch_state.switch_nesting_ast == NULL) {
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003790 YYLTYPE loc = this->get_location();
Ian Romanick4cf20cd2010-04-05 17:13:47 -07003791
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003792 _mesa_glsl_error(& loc, state,
3793 "break may only appear in a loop or a switch");
3794 } else {
3795 /* For a loop, inline the for loop expression again,
3796 * since we don't know where near the end of
3797 * the loop body the normal copy of it
Eric Anholt2d1ed7b2010-07-22 12:55:16 -07003798 * is going to be placed.
3799 */
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003800 if (state->loop_nesting_ast != NULL &&
3801 mode == ast_continue &&
3802 state->loop_nesting_ast->rest_expression) {
3803 state->loop_nesting_ast->rest_expression->hir(instructions,
3804 state);
Eric Anholt2d1ed7b2010-07-22 12:55:16 -07003805 }
3806
Eric Anholt22d81f12012-01-28 11:26:02 -08003807 if (state->switch_state.is_switch_innermost &&
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003808 mode == ast_break) {
3809 /* Force break out of switch by setting is_break switch state.
3810 */
Eric Anholt22d81f12012-01-28 11:26:02 -08003811 ir_variable *const is_break_var = state->switch_state.is_break_var;
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003812 ir_dereference_variable *const deref_is_break_var =
3813 new(ctx) ir_dereference_variable(is_break_var);
3814 ir_constant *const true_val = new(ctx) ir_constant(true);
3815 ir_assignment *const set_break_var =
Eric Anholtaa5ec132012-05-14 09:14:54 -07003816 new(ctx) ir_assignment(deref_is_break_var, true_val);
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003817
3818 instructions->push_tail(set_break_var);
3819 }
3820 else {
3821 ir_loop_jump *const jump =
Carl Worth1660a292010-06-23 18:11:51 -07003822 new(ctx) ir_loop_jump((mode == ast_break)
3823 ? ir_loop_jump::jump_break
3824 : ir_loop_jump::jump_continue);
Ian Romanick4cf20cd2010-04-05 17:13:47 -07003825 instructions->push_tail(jump);
3826 }
3827 }
3828
Ian Romanickc0e76d82010-04-05 16:53:19 -07003829 break;
Eric Anholtb9802072010-03-30 23:40:14 -10003830 }
3831
Ian Romanick16a246c2010-03-19 16:45:19 -07003832 /* Jump instructions do not have r-values.
3833 */
3834 return NULL;
3835}
Ian Romanick3c6fea32010-03-29 14:11:25 -07003836
3837
3838ir_rvalue *
3839ast_selection_statement::hir(exec_list *instructions,
3840 struct _mesa_glsl_parse_state *state)
3841{
Kenneth Graunke953ff122010-06-25 13:14:37 -07003842 void *ctx = state;
Carl Worth1660a292010-06-23 18:11:51 -07003843
Ian Romanick3c6fea32010-03-29 14:11:25 -07003844 ir_rvalue *const condition = this->condition->hir(instructions, state);
Ian Romanick3c6fea32010-03-29 14:11:25 -07003845
3846 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
3847 *
3848 * "Any expression whose type evaluates to a Boolean can be used as the
3849 * conditional expression bool-expression. Vector types are not accepted
3850 * as the expression to if."
3851 *
3852 * The checks are separated so that higher quality diagnostics can be
3853 * generated for cases where both rules are violated.
3854 */
3855 if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
3856 YYLTYPE loc = this->condition->get_location();
3857
3858 _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
3859 "boolean");
3860 }
3861
Carl Worth1660a292010-06-23 18:11:51 -07003862 ir_if *const stmt = new(ctx) ir_if(condition);
Ian Romanick3c6fea32010-03-29 14:11:25 -07003863
Kenneth Graunke665d75c2010-08-18 13:54:50 -07003864 if (then_statement != NULL) {
3865 state->symbols->push_scope();
Ian Romanick4f9d72f2010-05-10 11:10:26 -07003866 then_statement->hir(& stmt->then_instructions, state);
Kenneth Graunke665d75c2010-08-18 13:54:50 -07003867 state->symbols->pop_scope();
3868 }
Ian Romanick3c6fea32010-03-29 14:11:25 -07003869
Kenneth Graunke665d75c2010-08-18 13:54:50 -07003870 if (else_statement != NULL) {
3871 state->symbols->push_scope();
Ian Romanick4f9d72f2010-05-10 11:10:26 -07003872 else_statement->hir(& stmt->else_instructions, state);
Kenneth Graunke665d75c2010-08-18 13:54:50 -07003873 state->symbols->pop_scope();
3874 }
Ian Romanick3c6fea32010-03-29 14:11:25 -07003875
3876 instructions->push_tail(stmt);
3877
3878 /* if-statements do not have r-values.
3879 */
3880 return NULL;
3881}
Ian Romanick9e7d0102010-04-05 16:37:49 -07003882
3883
Dan McCabe85beb392011-11-07 15:11:04 -08003884ir_rvalue *
3885ast_switch_statement::hir(exec_list *instructions,
3886 struct _mesa_glsl_parse_state *state)
3887{
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003888 void *ctx = state;
3889
3890 ir_rvalue *const test_expression =
3891 this->test_expression->hir(instructions, state);
3892
3893 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
3894 *
3895 * "The type of init-expression in a switch statement must be a
3896 * scalar integer."
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003897 */
Eric Anholtbbbc7c72012-05-14 08:45:59 -07003898 if (!test_expression->type->is_scalar() ||
3899 !test_expression->type->is_integer()) {
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003900 YYLTYPE loc = this->test_expression->get_location();
3901
3902 _mesa_glsl_error(& loc,
3903 state,
3904 "switch-statement expression must be scalar "
3905 "integer");
3906 }
3907
3908 /* Track the switch-statement nesting in a stack-like manner.
3909 */
Eric Anholt22d81f12012-01-28 11:26:02 -08003910 struct glsl_switch_state saved = state->switch_state;
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003911
Eric Anholt22d81f12012-01-28 11:26:02 -08003912 state->switch_state.is_switch_innermost = true;
3913 state->switch_state.switch_nesting_ast = this;
Eric Anholt14063212012-01-30 08:50:14 -08003914 state->switch_state.labels_ht = hash_table_ctor(0, hash_table_pointer_hash,
3915 hash_table_pointer_compare);
Eric Anholt57e44372012-01-30 09:50:35 -08003916 state->switch_state.previous_default = NULL;
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003917
3918 /* Initalize is_fallthru state to false.
3919 */
3920 ir_rvalue *const is_fallthru_val = new (ctx) ir_constant(false);
Eric Anholt22d81f12012-01-28 11:26:02 -08003921 state->switch_state.is_fallthru_var =
3922 new(ctx) ir_variable(glsl_type::bool_type,
3923 "switch_is_fallthru_tmp",
3924 ir_var_temporary);
3925 instructions->push_tail(state->switch_state.is_fallthru_var);
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003926
3927 ir_dereference_variable *deref_is_fallthru_var =
Eric Anholt22d81f12012-01-28 11:26:02 -08003928 new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var);
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003929 instructions->push_tail(new(ctx) ir_assignment(deref_is_fallthru_var,
Eric Anholtaa5ec132012-05-14 09:14:54 -07003930 is_fallthru_val));
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003931
3932 /* Initalize is_break state to false.
3933 */
3934 ir_rvalue *const is_break_val = new (ctx) ir_constant(false);
Eric Anholt22d81f12012-01-28 11:26:02 -08003935 state->switch_state.is_break_var = new(ctx) ir_variable(glsl_type::bool_type,
3936 "switch_is_break_tmp",
3937 ir_var_temporary);
3938 instructions->push_tail(state->switch_state.is_break_var);
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003939
3940 ir_dereference_variable *deref_is_break_var =
Eric Anholt22d81f12012-01-28 11:26:02 -08003941 new(ctx) ir_dereference_variable(state->switch_state.is_break_var);
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003942 instructions->push_tail(new(ctx) ir_assignment(deref_is_break_var,
Eric Anholtaa5ec132012-05-14 09:14:54 -07003943 is_break_val));
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003944
3945 /* Cache test expression.
3946 */
3947 test_to_hir(instructions, state);
Eric Anholt5462f362012-05-14 08:37:50 -07003948
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003949 /* Emit code for body of switch stmt.
3950 */
3951 body->hir(instructions, state);
3952
Eric Anholt14063212012-01-30 08:50:14 -08003953 hash_table_dtor(state->switch_state.labels_ht);
3954
Eric Anholt22d81f12012-01-28 11:26:02 -08003955 state->switch_state = saved;
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003956
Eric Anholt5462f362012-05-14 08:37:50 -07003957 /* Switch statements do not have r-values. */
3958 return NULL;
3959}
Dan McCabe85beb392011-11-07 15:11:04 -08003960
3961
Eric Anholt5462f362012-05-14 08:37:50 -07003962void
3963ast_switch_statement::test_to_hir(exec_list *instructions,
3964 struct _mesa_glsl_parse_state *state)
3965{
3966 void *ctx = state;
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003967
Eric Anholt5462f362012-05-14 08:37:50 -07003968 /* Cache value of test expression. */
3969 ir_rvalue *const test_val =
3970 test_expression->hir(instructions,
3971 state);
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003972
Eric Anholt9c4e9ce2012-05-14 08:51:03 -07003973 state->switch_state.test_var = new(ctx) ir_variable(test_val->type,
Eric Anholt5462f362012-05-14 08:37:50 -07003974 "switch_test_tmp",
3975 ir_var_temporary);
3976 ir_dereference_variable *deref_test_var =
3977 new(ctx) ir_dereference_variable(state->switch_state.test_var);
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003978
Eric Anholt5462f362012-05-14 08:37:50 -07003979 instructions->push_tail(state->switch_state.test_var);
Eric Anholtaa5ec132012-05-14 09:14:54 -07003980 instructions->push_tail(new(ctx) ir_assignment(deref_test_var, test_val));
Eric Anholt5462f362012-05-14 08:37:50 -07003981}
Dan McCabe5c02e2e2011-11-07 16:17:58 -08003982
3983
Eric Anholt5462f362012-05-14 08:37:50 -07003984ir_rvalue *
3985ast_switch_body::hir(exec_list *instructions,
3986 struct _mesa_glsl_parse_state *state)
3987{
3988 if (stmts != NULL)
3989 stmts->hir(instructions, state);
Eric Anholt22d81f12012-01-28 11:26:02 -08003990
Eric Anholt5462f362012-05-14 08:37:50 -07003991 /* Switch bodies do not have r-values. */
3992 return NULL;
3993}
3994
3995ir_rvalue *
3996ast_case_statement_list::hir(exec_list *instructions,
3997 struct _mesa_glsl_parse_state *state)
3998{
3999 foreach_list_typed (ast_case_statement, case_stmt, link, & this->cases)
4000 case_stmt->hir(instructions, state);
4001
4002 /* Case statements do not have r-values. */
4003 return NULL;
4004}
4005
4006ir_rvalue *
4007ast_case_statement::hir(exec_list *instructions,
4008 struct _mesa_glsl_parse_state *state)
4009{
4010 labels->hir(instructions, state);
4011
4012 /* Conditionally set fallthru state based on break state. */
4013 ir_constant *const false_val = new(state) ir_constant(false);
4014 ir_dereference_variable *const deref_is_fallthru_var =
4015 new(state) ir_dereference_variable(state->switch_state.is_fallthru_var);
4016 ir_dereference_variable *const deref_is_break_var =
4017 new(state) ir_dereference_variable(state->switch_state.is_break_var);
4018 ir_assignment *const reset_fallthru_on_break =
4019 new(state) ir_assignment(deref_is_fallthru_var,
4020 false_val,
4021 deref_is_break_var);
4022 instructions->push_tail(reset_fallthru_on_break);
4023
4024 /* Guard case statements depending on fallthru state. */
4025 ir_dereference_variable *const deref_fallthru_guard =
4026 new(state) ir_dereference_variable(state->switch_state.is_fallthru_var);
4027 ir_if *const test_fallthru = new(state) ir_if(deref_fallthru_guard);
4028
4029 foreach_list_typed (ast_node, stmt, link, & this->stmts)
4030 stmt->hir(& test_fallthru->then_instructions, state);
4031
4032 instructions->push_tail(test_fallthru);
4033
4034 /* Case statements do not have r-values. */
4035 return NULL;
4036}
Dan McCabe85beb392011-11-07 15:11:04 -08004037
4038
Eric Anholt5462f362012-05-14 08:37:50 -07004039ir_rvalue *
4040ast_case_label_list::hir(exec_list *instructions,
4041 struct _mesa_glsl_parse_state *state)
4042{
4043 foreach_list_typed (ast_case_label, label, link, & this->labels)
4044 label->hir(instructions, state);
Eric Anholt22d81f12012-01-28 11:26:02 -08004045
Eric Anholt5462f362012-05-14 08:37:50 -07004046 /* Case labels do not have r-values. */
4047 return NULL;
4048}
Dan McCabe85beb392011-11-07 15:11:04 -08004049
Eric Anholt5462f362012-05-14 08:37:50 -07004050ir_rvalue *
4051ast_case_label::hir(exec_list *instructions,
4052 struct _mesa_glsl_parse_state *state)
4053{
4054 void *ctx = state;
Dan McCabe85beb392011-11-07 15:11:04 -08004055
Eric Anholt5462f362012-05-14 08:37:50 -07004056 ir_dereference_variable *deref_fallthru_var =
4057 new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var);
Dan McCabe5c02e2e2011-11-07 16:17:58 -08004058
Eric Anholt5462f362012-05-14 08:37:50 -07004059 ir_rvalue *const true_val = new(ctx) ir_constant(true);
Dan McCabe5c02e2e2011-11-07 16:17:58 -08004060
Eric Anholt5462f362012-05-14 08:37:50 -07004061 /* If not default case, ... */
4062 if (this->test_value != NULL) {
4063 /* Conditionally set fallthru state based on
4064 * comparison of cached test expression value to case label.
4065 */
4066 ir_rvalue *const label_rval = this->test_value->hir(instructions, state);
4067 ir_constant *label_const = label_rval->constant_expression_value();
Eric Anholt22d81f12012-01-28 11:26:02 -08004068
Eric Anholt5462f362012-05-14 08:37:50 -07004069 if (!label_const) {
4070 YYLTYPE loc = this->test_value->get_location();
Eric Anholt22d81f12012-01-28 11:26:02 -08004071
Eric Anholt5462f362012-05-14 08:37:50 -07004072 _mesa_glsl_error(& loc, state,
4073 "switch statement case label must be a "
4074 "constant expression");
Eric Anholt22d81f12012-01-28 11:26:02 -08004075
Eric Anholt5462f362012-05-14 08:37:50 -07004076 /* Stuff a dummy value in to allow processing to continue. */
4077 label_const = new(ctx) ir_constant(0);
4078 } else {
4079 ast_expression *previous_label = (ast_expression *)
4080 hash_table_find(state->switch_state.labels_ht,
4081 (void *)(uintptr_t)label_const->value.u[0]);
Dan McCabe85beb392011-11-07 15:11:04 -08004082
Eric Anholt5462f362012-05-14 08:37:50 -07004083 if (previous_label) {
4084 YYLTYPE loc = this->test_value->get_location();
4085 _mesa_glsl_error(& loc, state,
4086 "duplicate case value");
Dan McCabe85beb392011-11-07 15:11:04 -08004087
Eric Anholt5462f362012-05-14 08:37:50 -07004088 loc = previous_label->get_location();
4089 _mesa_glsl_error(& loc, state,
4090 "this is the previous case label");
4091 } else {
4092 hash_table_insert(state->switch_state.labels_ht,
4093 this->test_value,
Eric Anholt14063212012-01-30 08:50:14 -08004094 (void *)(uintptr_t)label_const->value.u[0]);
Eric Anholt5462f362012-05-14 08:37:50 -07004095 }
4096 }
Eric Anholt14063212012-01-30 08:50:14 -08004097
Eric Anholt5462f362012-05-14 08:37:50 -07004098 ir_dereference_variable *deref_test_var =
4099 new(ctx) ir_dereference_variable(state->switch_state.test_var);
Eric Anholt14063212012-01-30 08:50:14 -08004100
Eric Anholt5462f362012-05-14 08:37:50 -07004101 ir_rvalue *const test_cond = new(ctx) ir_expression(ir_binop_all_equal,
Eric Anholt5462f362012-05-14 08:37:50 -07004102 label_const,
4103 deref_test_var);
Dan McCabe5c02e2e2011-11-07 16:17:58 -08004104
Eric Anholt5462f362012-05-14 08:37:50 -07004105 ir_assignment *set_fallthru_on_test =
4106 new(ctx) ir_assignment(deref_fallthru_var,
4107 true_val,
4108 test_cond);
Dan McCabe5c02e2e2011-11-07 16:17:58 -08004109
Eric Anholt5462f362012-05-14 08:37:50 -07004110 instructions->push_tail(set_fallthru_on_test);
4111 } else { /* default case */
4112 if (state->switch_state.previous_default) {
Eric Anholt5462f362012-05-14 08:37:50 -07004113 YYLTYPE loc = this->get_location();
4114 _mesa_glsl_error(& loc, state,
4115 "multiple default labels in one switch");
Eric Anholt22d81f12012-01-28 11:26:02 -08004116
Eric Anholt5462f362012-05-14 08:37:50 -07004117 loc = state->switch_state.previous_default->get_location();
4118 _mesa_glsl_error(& loc, state,
4119 "this is the first default label");
4120 }
4121 state->switch_state.previous_default = this;
Eric Anholt57e44372012-01-30 09:50:35 -08004122
Eric Anholt5462f362012-05-14 08:37:50 -07004123 /* Set falltrhu state. */
4124 ir_assignment *set_fallthru =
Eric Anholtaa5ec132012-05-14 09:14:54 -07004125 new(ctx) ir_assignment(deref_fallthru_var, true_val);
Eric Anholt57e44372012-01-30 09:50:35 -08004126
Eric Anholt5462f362012-05-14 08:37:50 -07004127 instructions->push_tail(set_fallthru);
4128 }
Eric Anholt57e44372012-01-30 09:50:35 -08004129
Eric Anholt5462f362012-05-14 08:37:50 -07004130 /* Case statements do not have r-values. */
4131 return NULL;
4132}
Eric Anholt22d81f12012-01-28 11:26:02 -08004133
Eric Anholt5462f362012-05-14 08:37:50 -07004134void
4135ast_iteration_statement::condition_to_hir(ir_loop *stmt,
4136 struct _mesa_glsl_parse_state *state)
4137{
4138 void *ctx = state;
Eric Anholt22d81f12012-01-28 11:26:02 -08004139
Eric Anholt5462f362012-05-14 08:37:50 -07004140 if (condition != NULL) {
4141 ir_rvalue *const cond =
4142 condition->hir(& stmt->body_instructions, state);
4143
4144 if ((cond == NULL)
4145 || !cond->type->is_boolean() || !cond->type->is_scalar()) {
4146 YYLTYPE loc = condition->get_location();
4147
4148 _mesa_glsl_error(& loc, state,
4149 "loop condition must be scalar boolean");
4150 } else {
4151 /* As the first code in the loop body, generate a block that looks
4152 * like 'if (!condition) break;' as the loop termination condition.
4153 */
4154 ir_rvalue *const not_cond =
Eric Anholt5d6ea162012-05-14 08:39:54 -07004155 new(ctx) ir_expression(ir_unop_logic_not, cond);
Eric Anholt5462f362012-05-14 08:37:50 -07004156
4157 ir_if *const if_stmt = new(ctx) ir_if(not_cond);
4158
4159 ir_jump *const break_stmt =
4160 new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
4161
4162 if_stmt->then_instructions.push_tail(break_stmt);
4163 stmt->body_instructions.push_tail(if_stmt);
4164 }
4165 }
4166}
Dan McCabe85beb392011-11-07 15:11:04 -08004167
4168
Eric Anholt5462f362012-05-14 08:37:50 -07004169ir_rvalue *
4170ast_iteration_statement::hir(exec_list *instructions,
4171 struct _mesa_glsl_parse_state *state)
4172{
4173 void *ctx = state;
Carl Worth1660a292010-06-23 18:11:51 -07004174
Eric Anholt5462f362012-05-14 08:37:50 -07004175 /* For-loops and while-loops start a new scope, but do-while loops do not.
4176 */
4177 if (mode != ast_do_while)
4178 state->symbols->push_scope();
Ian Romanick9e7d0102010-04-05 16:37:49 -07004179
Eric Anholt5462f362012-05-14 08:37:50 -07004180 if (init_statement != NULL)
4181 init_statement->hir(instructions, state);
Ian Romanick9e7d0102010-04-05 16:37:49 -07004182
Eric Anholt5462f362012-05-14 08:37:50 -07004183 ir_loop *const stmt = new(ctx) ir_loop();
4184 instructions->push_tail(stmt);
Ian Romanick9e7d0102010-04-05 16:37:49 -07004185
Eric Anholt5462f362012-05-14 08:37:50 -07004186 /* Track the current loop nesting. */
4187 ast_iteration_statement *nesting_ast = state->loop_nesting_ast;
Ian Romanick9e7d0102010-04-05 16:37:49 -07004188
Eric Anholt5462f362012-05-14 08:37:50 -07004189 state->loop_nesting_ast = this;
Ian Romanick9e7d0102010-04-05 16:37:49 -07004190
Eric Anholt5462f362012-05-14 08:37:50 -07004191 /* Likewise, indicate that following code is closest to a loop,
4192 * NOT closest to a switch.
4193 */
4194 bool saved_is_switch_innermost = state->switch_state.is_switch_innermost;
4195 state->switch_state.is_switch_innermost = false;
Ian Romanick8c46ed22010-04-05 18:07:27 -07004196
Eric Anholt5462f362012-05-14 08:37:50 -07004197 if (mode != ast_do_while)
4198 condition_to_hir(stmt, state);
Ian Romanick8c46ed22010-04-05 18:07:27 -07004199
Eric Anholt5462f362012-05-14 08:37:50 -07004200 if (body != NULL)
4201 body->hir(& stmt->body_instructions, state);
Carl Worth1660a292010-06-23 18:11:51 -07004202
Eric Anholt5462f362012-05-14 08:37:50 -07004203 if (rest_expression != NULL)
4204 rest_expression->hir(& stmt->body_instructions, state);
Ian Romanick8c46ed22010-04-05 18:07:27 -07004205
Eric Anholt5462f362012-05-14 08:37:50 -07004206 if (mode == ast_do_while)
4207 condition_to_hir(stmt, state);
Ian Romanick8c46ed22010-04-05 18:07:27 -07004208
Eric Anholt5462f362012-05-14 08:37:50 -07004209 if (mode != ast_do_while)
4210 state->symbols->pop_scope();
Ian Romanick8c46ed22010-04-05 18:07:27 -07004211
Eric Anholt5462f362012-05-14 08:37:50 -07004212 /* Restore previous nesting before returning. */
4213 state->loop_nesting_ast = nesting_ast;
4214 state->switch_state.is_switch_innermost = saved_is_switch_innermost;
Ian Romanicke9d0f262010-04-05 17:01:53 -07004215
Ian Romanick9e7d0102010-04-05 16:37:49 -07004216 /* Loops do not have r-values.
4217 */
4218 return NULL;
4219}
Ian Romanick3455ce62010-04-19 15:13:15 -07004220
4221
Paul Berryd5948f22013-02-12 12:36:41 -08004222/**
4223 * Determine if the given type is valid for establishing a default precision
4224 * qualifier.
4225 *
4226 * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
4227 *
4228 * "The precision statement
4229 *
4230 * precision precision-qualifier type;
4231 *
4232 * can be used to establish a default precision qualifier. The type field
4233 * can be either int or float or any of the sampler types, and the
4234 * precision-qualifier can be lowp, mediump, or highp."
4235 *
4236 * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
4237 * qualifiers on sampler types, but this seems like an oversight (since the
4238 * intention of including these in GLSL 1.30 is to allow compatibility with ES
4239 * shaders). So we allow int, float, and all sampler types regardless of GLSL
4240 * version.
4241 */
4242static bool
Ian Romanickb15b62c2013-08-09 15:15:45 -07004243is_valid_default_precision_type(const struct glsl_type *const type)
Paul Berryd5948f22013-02-12 12:36:41 -08004244{
Paul Berryd5948f22013-02-12 12:36:41 -08004245 if (type == NULL)
4246 return false;
4247
4248 switch (type->base_type) {
4249 case GLSL_TYPE_INT:
4250 case GLSL_TYPE_FLOAT:
4251 /* "int" and "float" are valid, but vectors and matrices are not. */
4252 return type->vector_elements == 1 && type->matrix_columns == 1;
4253 case GLSL_TYPE_SAMPLER:
4254 return true;
4255 default:
4256 return false;
4257 }
4258}
4259
4260
Ian Romanick3455ce62010-04-19 15:13:15 -07004261ir_rvalue *
4262ast_type_specifier::hir(exec_list *instructions,
4263 struct _mesa_glsl_parse_state *state)
4264{
Kenneth Graunke308d4c72013-07-15 15:39:35 -07004265 if (this->default_precision == ast_precision_none && this->structure == NULL)
Chad Versace08a286c2011-01-16 21:44:57 -08004266 return NULL;
4267
4268 YYLTYPE loc = this->get_location();
4269
Chad Versace08a286c2011-01-16 21:44:57 -08004270 /* If this is a precision statement, check that the type to which it is
4271 * applied is either float or int.
4272 *
4273 * From section 4.5.3 of the GLSL 1.30 spec:
4274 * "The precision statement
4275 * precision precision-qualifier type;
4276 * can be used to establish a default precision qualifier. The type
4277 * field can be either int or float [...]. Any other types or
4278 * qualifiers will result in an error.
4279 */
Kenneth Graunke308d4c72013-07-15 15:39:35 -07004280 if (this->default_precision != ast_precision_none) {
Kenneth Graunke6eec5022013-07-15 15:58:29 -07004281 if (!state->check_precision_qualifiers_allowed(&loc))
4282 return NULL;
4283
4284 if (this->structure != NULL) {
4285 _mesa_glsl_error(&loc, state,
4286 "precision qualifiers do not apply to structures");
4287 return NULL;
4288 }
4289
Chad Versace08a286c2011-01-16 21:44:57 -08004290 if (this->is_array) {
4291 _mesa_glsl_error(&loc, state,
4292 "default precision statements do not apply to "
4293 "arrays");
4294 return NULL;
4295 }
Ian Romanickb15b62c2013-08-09 15:15:45 -07004296
4297 const struct glsl_type *const type =
4298 state->symbols->get_type(this->type_name);
4299 if (!is_valid_default_precision_type(type)) {
Chad Versace08a286c2011-01-16 21:44:57 -08004300 _mesa_glsl_error(&loc, state,
Ian Romanick0b5fb6d2013-08-09 13:44:49 -07004301 "default precision statements apply only to "
Paul Berryd5948f22013-02-12 12:36:41 -08004302 "float, int, and sampler types");
Chad Versace08a286c2011-01-16 21:44:57 -08004303 return NULL;
4304 }
4305
Ian Romanickcabd4572013-08-09 15:17:18 -07004306 if (type->base_type == GLSL_TYPE_FLOAT
4307 && state->es_shader
4308 && state->target == fragment_shader) {
4309 /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
4310 * spec says:
4311 *
4312 * "The fragment language has no default precision qualifier for
4313 * floating point types."
4314 *
4315 * As a result, we have to track whether or not default precision has
4316 * been specified for float in GLSL ES fragment shaders.
4317 *
4318 * Earlier in that same section, the spec says:
4319 *
4320 * "Non-precision qualified declarations will use the precision
4321 * qualifier specified in the most recent precision statement
4322 * that is still in scope. The precision statement has the same
4323 * scoping rules as variable declarations. If it is declared
4324 * inside a compound statement, its effect stops at the end of
4325 * the innermost statement it was declared in. Precision
4326 * statements in nested scopes override precision statements in
4327 * outer scopes. Multiple precision statements for the same basic
4328 * type can appear inside the same scope, with later statements
4329 * overriding earlier statements within that scope."
4330 *
4331 * Default precision specifications follow the same scope rules as
4332 * variables. So, we can track the state of the default float
4333 * precision in the symbol table, and the rules will just work. This
4334 * is a slight abuse of the symbol table, but it has the semantics
4335 * that we want.
4336 */
4337 ir_variable *const junk =
4338 new(state) ir_variable(type, "#default precision",
4339 ir_var_temporary);
4340
4341 state->symbols->add_variable(junk);
4342 }
4343
Chad Versace08a286c2011-01-16 21:44:57 -08004344 /* FINISHME: Translate precision statements into IR. */
4345 return NULL;
4346 }
4347
Matt Turner1b0d6ae2013-06-29 19:29:16 -07004348 /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
4349 * process_record_constructor() can do type-checking on C-style initializer
4350 * expressions of structs, but ast_struct_specifier should only be translated
4351 * to HIR if it is declaring the type of a structure.
4352 *
4353 * The ->is_declaration field is false for initializers of variables
4354 * declared separately from the struct's type definition.
4355 *
4356 * struct S { ... }; (is_declaration = true)
4357 * struct T { ... } t = { ... }; (is_declaration = true)
4358 * S s = { ... }; (is_declaration = false)
4359 */
4360 if (this->structure != NULL && this->structure->is_declaration)
Ian Romanick3455ce62010-04-19 15:13:15 -07004361 return this->structure->hir(instructions, state);
Ian Romanick85ba37b2010-04-21 14:33:34 -07004362
4363 return NULL;
Ian Romanick3455ce62010-04-19 15:13:15 -07004364}
4365
4366
Ian Romanick51f740c2012-12-08 17:38:30 -08004367/**
4368 * Process a structure or interface block tree into an array of structure fields
4369 *
4370 * After parsing, where there are some syntax differnces, structures and
4371 * interface blocks are almost identical. They are similar enough that the
4372 * AST for each can be processed the same way into a set of
4373 * \c glsl_struct_field to describe the members.
4374 *
4375 * \return
4376 * The number of fields processed. A pointer to the array structure fields is
4377 * stored in \c *fields_ret.
4378 */
4379unsigned
4380ast_process_structure_or_interface_block(exec_list *instructions,
4381 struct _mesa_glsl_parse_state *state,
4382 exec_list *declarations,
4383 YYLTYPE &loc,
Ian Romanick17e6f192012-12-11 12:14:03 -08004384 glsl_struct_field **fields_ret,
4385 bool is_interface,
4386 bool block_row_major)
Ian Romanick3455ce62010-04-19 15:13:15 -07004387{
Ian Romanick3455ce62010-04-19 15:13:15 -07004388 unsigned decl_count = 0;
4389
Ian Romanick51f740c2012-12-08 17:38:30 -08004390 /* Make an initial pass over the list of fields to determine how
Ian Romanick3455ce62010-04-19 15:13:15 -07004391 * many there are. Each element in this list is an ast_declarator_list.
4392 * This means that we actually need to count the number of elements in the
4393 * 'declarations' list in each of the elements.
4394 */
Ian Romanick51f740c2012-12-08 17:38:30 -08004395 foreach_list_typed (ast_declarator_list, decl_list, link, declarations) {
Ian Romanick304ea902010-05-10 11:17:53 -07004396 foreach_list_const (decl_ptr, & decl_list->declarations) {
Ian Romanick3455ce62010-04-19 15:13:15 -07004397 decl_count++;
4398 }
4399 }
4400
Ian Romanick51f740c2012-12-08 17:38:30 -08004401 /* Allocate storage for the fields and process the field
Ian Romanick3455ce62010-04-19 15:13:15 -07004402 * declarations. As the declarations are processed, try to also convert
4403 * the types to HIR. This ensures that structure definitions embedded in
Ian Romanick51f740c2012-12-08 17:38:30 -08004404 * other structure definitions or in interface blocks are processed.
Ian Romanick3455ce62010-04-19 15:13:15 -07004405 */
Kenneth Graunked3073f52011-01-21 14:32:31 -08004406 glsl_struct_field *const fields = ralloc_array(state, glsl_struct_field,
Eric Anholt21b0dbd2010-07-20 16:47:25 -07004407 decl_count);
Ian Romanick3455ce62010-04-19 15:13:15 -07004408
4409 unsigned i = 0;
Ian Romanick51f740c2012-12-08 17:38:30 -08004410 foreach_list_typed (ast_declarator_list, decl_list, link, declarations) {
Ian Romanick3455ce62010-04-19 15:13:15 -07004411 const char *type_name;
4412
4413 decl_list->type->specifier->hir(instructions, state);
4414
Kenneth Graunkec98deb12010-08-16 14:02:25 -07004415 /* Section 10.9 of the GLSL ES 1.00 specification states that
4416 * embedded structure definitions have been removed from the language.
4417 */
4418 if (state->es_shader && decl_list->type->specifier->structure != NULL) {
Paul Berry4d7899f2013-07-25 19:56:43 -07004419 _mesa_glsl_error(&loc, state, "embedded structure definitions are "
4420 "not allowed in GLSL ES 1.00");
Kenneth Graunkec98deb12010-08-16 14:02:25 -07004421 }
4422
Ian Romanick3455ce62010-04-19 15:13:15 -07004423 const glsl_type *decl_type =
Ian Romanickcabd4572013-08-09 15:17:18 -07004424 decl_list->type->glsl_type(& type_name, state);
Ian Romanick3455ce62010-04-19 15:13:15 -07004425
Ian Romanick2b97dc62010-05-10 17:42:05 -07004426 foreach_list_typed (ast_declaration, decl, link,
4427 &decl_list->declarations) {
Ian Romanick17e6f192012-12-11 12:14:03 -08004428 /* From the GL_ARB_uniform_buffer_object spec:
4429 *
4430 * "Sampler types are not allowed inside of uniform
4431 * blocks. All other types, arrays, and structures
4432 * allowed for uniforms are allowed within a uniform
4433 * block."
Ian Romanick278c9af2013-04-08 16:37:04 -07004434 *
4435 * It should be impossible for decl_type to be NULL here. Cases that
4436 * might naturally lead to decl_type being NULL, especially for the
4437 * is_interface case, will have resulted in compilation having
4438 * already halted due to a syntax error.
Ian Romanick17e6f192012-12-11 12:14:03 -08004439 */
Ian Romanick278c9af2013-04-08 16:37:04 -07004440 const struct glsl_type *field_type =
4441 decl_type != NULL ? decl_type : glsl_type::error_type;
Ian Romanick17e6f192012-12-11 12:14:03 -08004442
4443 if (is_interface && field_type->contains_sampler()) {
4444 YYLTYPE loc = decl_list->get_location();
4445 _mesa_glsl_error(&loc, state,
Paul Berry4d7899f2013-07-25 19:56:43 -07004446 "uniform in non-default uniform block contains sampler");
Ian Romanick17e6f192012-12-11 12:14:03 -08004447 }
4448
4449 const struct ast_type_qualifier *const qual =
4450 & decl_list->type->qualifier;
4451 if (qual->flags.q.std140 ||
4452 qual->flags.q.packed ||
4453 qual->flags.q.shared) {
4454 _mesa_glsl_error(&loc, state,
4455 "uniform block layout qualifiers std140, packed, and "
4456 "shared can only be applied to uniform blocks, not "
4457 "members");
4458 }
4459
Kenneth Graunked8e34e22010-08-07 02:56:01 -07004460 if (decl->is_array) {
Kenneth Graunked8e34e22010-08-07 02:56:01 -07004461 field_type = process_array_type(&loc, decl_type, decl->array_size,
4462 state);
4463 }
Ian Romanick278c9af2013-04-08 16:37:04 -07004464 fields[i].type = field_type;
Ian Romanick3455ce62010-04-19 15:13:15 -07004465 fields[i].name = decl->identifier;
Ian Romanick17e6f192012-12-11 12:14:03 -08004466
4467 if (qual->flags.q.row_major || qual->flags.q.column_major) {
Jordan Justencb29a702013-03-23 17:16:28 -07004468 if (!qual->flags.q.uniform) {
4469 _mesa_glsl_error(&loc, state,
4470 "row_major and column_major can only be "
Paul Berry4d7899f2013-07-25 19:56:43 -07004471 "applied to uniform interface blocks");
Ian Romanick17e6f192012-12-11 12:14:03 -08004472 } else
4473 validate_matrix_layout_for_type(state, &loc, field_type);
4474 }
4475
Jordan Justen067cc082013-03-23 17:14:37 -07004476 if (qual->flags.q.uniform && qual->has_interpolation()) {
4477 _mesa_glsl_error(&loc, state,
4478 "interpolation qualifiers cannot be used "
4479 "with uniform interface blocks");
4480 }
4481
Ian Romanick17e6f192012-12-11 12:14:03 -08004482 if (field_type->is_matrix() ||
4483 (field_type->is_array() && field_type->fields.array->is_matrix())) {
4484 fields[i].row_major = block_row_major;
4485 if (qual->flags.q.row_major)
4486 fields[i].row_major = true;
4487 else if (qual->flags.q.column_major)
4488 fields[i].row_major = false;
4489 }
4490
Ian Romanick3455ce62010-04-19 15:13:15 -07004491 i++;
4492 }
4493 }
4494
4495 assert(i == decl_count);
4496
Ian Romanick51f740c2012-12-08 17:38:30 -08004497 *fields_ret = fields;
4498 return decl_count;
4499}
4500
4501
4502ir_rvalue *
4503ast_struct_specifier::hir(exec_list *instructions,
4504 struct _mesa_glsl_parse_state *state)
4505{
4506 YYLTYPE loc = this->get_location();
Ian Romanickd9bb8b72013-08-13 09:15:01 -07004507
4508 /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
4509 *
4510 * "Anonymous structures are not supported; so embedded structures must
4511 * have a declarator. A name given to an embedded struct is scoped at
4512 * the same level as the struct it is embedded in."
4513 *
4514 * The same section of the GLSL 1.20 spec says:
4515 *
4516 * "Anonymous structures are not supported. Embedded structures are not
4517 * supported.
4518 *
4519 * struct S { float f; };
4520 * struct T {
4521 * S; // Error: anonymous structures disallowed
4522 * struct { ... }; // Error: embedded structures disallowed
4523 * S s; // Okay: nested structures with name are allowed
4524 * };"
4525 *
4526 * The GLSL ES 1.00 and 3.00 specs have similar langauge and examples. So,
4527 * we allow embedded structures in 1.10 only.
4528 */
4529 if (state->language_version != 110 && state->struct_specifier_depth != 0)
4530 _mesa_glsl_error(&loc, state,
4531 "embedded structure declartions are not allowed");
4532
4533 state->struct_specifier_depth++;
4534
Ian Romanick51f740c2012-12-08 17:38:30 -08004535 glsl_struct_field *fields;
4536 unsigned decl_count =
4537 ast_process_structure_or_interface_block(instructions,
4538 state,
4539 &this->declarations,
4540 loc,
Ian Romanick17e6f192012-12-11 12:14:03 -08004541 &fields,
4542 false,
4543 false);
Ian Romanick51f740c2012-12-08 17:38:30 -08004544
Ian Romanick49e35772010-06-28 11:54:57 -07004545 const glsl_type *t =
Kenneth Graunkeca92ae22010-09-18 11:11:09 +02004546 glsl_type::get_record_instance(fields, decl_count, this->name);
Ian Romanick1d28b612010-04-20 16:48:24 -07004547
Ian Romanicka789ca62010-09-01 14:08:08 -07004548 if (!state->symbols->add_type(name, t)) {
Ian Romanickab899272010-04-23 13:24:08 -07004549 _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
4550 } else {
Kenneth Graunkeeb639342011-02-27 01:17:29 -08004551 const glsl_type **s = reralloc(state, state->user_structures,
4552 const glsl_type *,
4553 state->num_user_structures + 1);
Ian Romanicka2c6df52010-04-28 13:14:53 -07004554 if (s != NULL) {
4555 s[state->num_user_structures] = t;
4556 state->user_structures = s;
4557 state->num_user_structures++;
4558 }
Ian Romanickab899272010-04-23 13:24:08 -07004559 }
Ian Romanick3455ce62010-04-19 15:13:15 -07004560
Ian Romanickd9bb8b72013-08-13 09:15:01 -07004561 state->struct_specifier_depth--;
4562
Ian Romanick3455ce62010-04-19 15:13:15 -07004563 /* Structure type definitions do not have r-values.
4564 */
4565 return NULL;
4566}
Eric Anholt4b2a4cb2012-03-29 17:29:20 -07004567
Eric Anholt2d03f482012-04-18 13:35:56 -07004568ir_rvalue *
Jordan Justenc9f58542013-03-09 10:40:41 -08004569ast_interface_block::hir(exec_list *instructions,
4570 struct _mesa_glsl_parse_state *state)
Eric Anholt2d03f482012-04-18 13:35:56 -07004571{
Ian Romanick17e6f192012-12-11 12:14:03 -08004572 YYLTYPE loc = this->get_location();
4573
Jordan Justenc9f58542013-03-09 10:40:41 -08004574 /* The ast_interface_block has a list of ast_declarator_lists. We
Eric Anholt2d03f482012-04-18 13:35:56 -07004575 * need to turn those into ir_variables with an association
4576 * with this uniform block.
4577 */
Ian Romanick514f8c72013-01-22 01:09:16 -05004578 enum glsl_interface_packing packing;
Ian Romanick9a971ab2012-12-13 02:13:30 -08004579 if (this->layout.flags.q.shared) {
Ian Romanick514f8c72013-01-22 01:09:16 -05004580 packing = GLSL_INTERFACE_PACKING_SHARED;
Ian Romanick9a971ab2012-12-13 02:13:30 -08004581 } else if (this->layout.flags.q.packed) {
Ian Romanick514f8c72013-01-22 01:09:16 -05004582 packing = GLSL_INTERFACE_PACKING_PACKED;
Ian Romanick9a971ab2012-12-13 02:13:30 -08004583 } else {
4584 /* The default layout is std140.
4585 */
Ian Romanick514f8c72013-01-22 01:09:16 -05004586 packing = GLSL_INTERFACE_PACKING_STD140;
Ian Romanick9a971ab2012-12-13 02:13:30 -08004587 }
4588
Ian Romanick17e6f192012-12-11 12:14:03 -08004589 bool block_row_major = this->layout.flags.q.row_major;
4590 exec_list declared_variables;
4591 glsl_struct_field *fields;
4592 unsigned int num_variables =
4593 ast_process_structure_or_interface_block(&declared_variables,
4594 state,
4595 &this->declarations,
4596 loc,
4597 &fields,
4598 true,
4599 block_row_major);
4600
Jordan Justenb24eeb02013-03-09 16:34:55 -08004601 ir_variable_mode var_mode;
4602 const char *iface_type_name;
4603 if (this->layout.flags.q.in) {
4604 var_mode = ir_var_shader_in;
4605 iface_type_name = "in";
4606 } else if (this->layout.flags.q.out) {
4607 var_mode = ir_var_shader_out;
4608 iface_type_name = "out";
4609 } else if (this->layout.flags.q.uniform) {
4610 var_mode = ir_var_uniform;
4611 iface_type_name = "uniform";
4612 } else {
Emil Velikov4df68232013-07-08 18:29:17 +01004613 var_mode = ir_var_auto;
4614 iface_type_name = "UNKNOWN";
Jordan Justenb24eeb02013-03-09 16:34:55 -08004615 assert(!"interface block layout qualifier not found!");
4616 }
4617
Ian Romanick17e6f192012-12-11 12:14:03 -08004618 const glsl_type *block_type =
4619 glsl_type::get_interface_instance(fields,
4620 num_variables,
Ian Romanick514f8c72013-01-22 01:09:16 -05004621 packing,
Ian Romanick17e6f192012-12-11 12:14:03 -08004622 this->block_name);
4623
Jordan Justenb24eeb02013-03-09 16:34:55 -08004624 if (!state->symbols->add_interface(block_type->name, block_type, var_mode)) {
Ian Romanick53836612013-01-21 23:01:33 -05004625 YYLTYPE loc = this->get_location();
Paul Berry4d7899f2013-07-25 19:56:43 -07004626 _mesa_glsl_error(&loc, state, "interface block `%s' with type `%s' "
4627 "already taken in the current scope",
Jordan Justenb24eeb02013-03-09 16:34:55 -08004628 this->block_name, iface_type_name);
Ian Romanick53836612013-01-21 23:01:33 -05004629 }
4630
Ian Romanick17e6f192012-12-11 12:14:03 -08004631 /* Since interface blocks cannot contain statements, it should be
4632 * impossible for the block to generate any instructions.
4633 */
4634 assert(declared_variables.is_empty());
4635
Paul Berry336351e2013-08-12 06:39:23 -07004636 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
4637 *
4638 * Geometry shader input variables get the per-vertex values written
4639 * out by vertex shader output variables of the same names. Since a
4640 * geometry shader operates on a set of vertices, each input varying
4641 * variable (or input block, see interface blocks below) needs to be
4642 * declared as an array.
4643 */
4644 if (state->target == geometry_shader && !this->is_array &&
4645 var_mode == ir_var_shader_in) {
4646 _mesa_glsl_error(&loc, state, "geometry shader inputs must be arrays");
4647 }
4648
Ian Romanick17e6f192012-12-11 12:14:03 -08004649 /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
4650 * says:
4651 *
4652 * "If an instance name (instance-name) is used, then it puts all the
4653 * members inside a scope within its own name space, accessed with the
4654 * field selector ( . ) operator (analogously to structures)."
4655 */
4656 if (this->instance_name) {
Ian Romanick25e75b02013-01-21 23:06:45 -05004657 ir_variable *var;
4658
Paul Berry20ae8e02013-07-24 14:57:24 -07004659 if (this->is_array) {
4660 /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
4661 *
4662 * For uniform blocks declared an array, each individual array
4663 * element corresponds to a separate buffer object backing one
4664 * instance of the block. As the array size indicates the number
4665 * of buffer objects needed, uniform block array declarations
4666 * must specify an array size.
4667 *
4668 * And a few paragraphs later:
4669 *
4670 * Geometry shader input blocks must be declared as arrays and
4671 * follow the array declaration and linking rules for all
4672 * geometry shader inputs. All other input and output block
4673 * arrays must specify an array size.
4674 *
4675 * The upshot of this is that the only circumstance where an
4676 * interface array size *doesn't* need to be specified is on a
4677 * geometry shader input.
4678 */
4679 if (this->array_size == NULL &&
4680 (state->target != geometry_shader || !this->layout.flags.q.in)) {
4681 _mesa_glsl_error(&loc, state,
4682 "only geometry shader inputs may be unsized "
4683 "instance block arrays");
4684
4685 }
4686
Ian Romanick25e75b02013-01-21 23:06:45 -05004687 const glsl_type *block_array_type =
4688 process_array_type(&loc, block_type, this->array_size, state);
4689
4690 var = new(state) ir_variable(block_array_type,
4691 this->instance_name,
Jordan Justenb24eeb02013-03-09 16:34:55 -08004692 var_mode);
Ian Romanick25e75b02013-01-21 23:06:45 -05004693 } else {
4694 var = new(state) ir_variable(block_type,
4695 this->instance_name,
Jordan Justenb24eeb02013-03-09 16:34:55 -08004696 var_mode);
Ian Romanick25e75b02013-01-21 23:06:45 -05004697 }
Ian Romanick17e6f192012-12-11 12:14:03 -08004698
Ian Romanick7a7b44b2013-01-21 21:51:15 -05004699 var->interface_type = block_type;
Paul Berry825f9ff2013-08-12 06:39:23 -07004700 if (state->target == geometry_shader && var_mode == ir_var_shader_in)
Paul Berry7cfefe62013-07-30 21:13:48 -07004701 handle_geometry_shader_input_decl(state, loc, var);
Ian Romanick17e6f192012-12-11 12:14:03 -08004702 state->symbols->add_variable(var);
4703 instructions->push_tail(var);
4704 } else {
Ian Romanick25e75b02013-01-21 23:06:45 -05004705 /* In order to have an array size, the block must also be declared with
4706 * an instane name.
4707 */
Paul Berry20ae8e02013-07-24 14:57:24 -07004708 assert(!this->is_array);
Ian Romanick25e75b02013-01-21 23:06:45 -05004709
Ian Romanick17e6f192012-12-11 12:14:03 -08004710 for (unsigned i = 0; i < num_variables; i++) {
4711 ir_variable *var =
4712 new(state) ir_variable(fields[i].type,
4713 ralloc_strdup(state, fields[i].name),
Jordan Justenb24eeb02013-03-09 16:34:55 -08004714 var_mode);
Ian Romanick7a7b44b2013-01-21 21:51:15 -05004715 var->interface_type = block_type;
Ian Romanick17e6f192012-12-11 12:14:03 -08004716
Kenneth Graunkef25d9402013-07-17 18:06:57 -07004717 /* Propagate the "binding" keyword into this UBO's fields;
4718 * the UBO declaration itself doesn't get an ir_variable unless it
4719 * has an instance name. This is ugly.
4720 */
4721 var->explicit_binding = this->layout.flags.q.explicit_binding;
4722 var->binding = this->layout.binding;
4723
Ian Romanick17e6f192012-12-11 12:14:03 -08004724 state->symbols->add_variable(var);
4725 instructions->push_tail(var);
Eric Anholtb3c093c2012-04-26 18:21:43 -07004726 }
4727 }
4728
Eric Anholt2d03f482012-04-18 13:35:56 -07004729 return NULL;
4730}
4731
Eric Anholt624b7ba2013-06-12 14:03:49 -07004732
4733ir_rvalue *
4734ast_gs_input_layout::hir(exec_list *instructions,
4735 struct _mesa_glsl_parse_state *state)
4736{
4737 YYLTYPE loc = this->get_location();
4738
4739 /* If any geometry input layout declaration preceded this one, make sure it
4740 * was consistent with this one.
4741 */
4742 if (state->gs_input_prim_type_specified &&
4743 state->gs_input_prim_type != this->prim_type) {
4744 _mesa_glsl_error(&loc, state,
4745 "geometry shader input layout does not match"
4746 " previous declaration");
4747 return NULL;
4748 }
4749
Paul Berry7cfefe62013-07-30 21:13:48 -07004750 /* If any shader inputs occurred before this declaration and specified an
4751 * array size, make sure the size they specified is consistent with the
4752 * primitive type.
4753 */
4754 unsigned num_vertices = vertices_per_prim(this->prim_type);
4755 if (state->gs_input_size != 0 && state->gs_input_size != num_vertices) {
4756 _mesa_glsl_error(&loc, state,
4757 "this geometry shader input layout implies %u vertices"
4758 " per primitive, but a previous input is declared"
4759 " with size %u", num_vertices, state->gs_input_size);
4760 return NULL;
4761 }
4762
Eric Anholt624b7ba2013-06-12 14:03:49 -07004763 state->gs_input_prim_type_specified = true;
4764 state->gs_input_prim_type = this->prim_type;
4765
Paul Berry7cfefe62013-07-30 21:13:48 -07004766 /* If any shader inputs occurred before this declaration and did not
4767 * specify an array size, their size is determined now.
4768 */
4769 foreach_list (node, instructions) {
4770 ir_variable *var = ((ir_instruction *) node)->as_variable();
4771 if (var == NULL || var->mode != ir_var_shader_in)
4772 continue;
4773
4774 /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
4775 * array; skip it.
4776 */
4777 if (!var->type->is_array())
4778 continue;
4779
4780 if (var->type->length == 0) {
4781 if (var->max_array_access >= num_vertices) {
4782 _mesa_glsl_error(&loc, state,
4783 "this geometry shader input layout implies %u"
4784 " vertices, but an access to element %u of input"
4785 " `%s' already exists", num_vertices,
4786 var->max_array_access, var->name);
4787 } else {
4788 var->type = glsl_type::get_array_instance(var->type->fields.array,
4789 num_vertices);
4790 }
4791 }
4792 }
4793
Eric Anholt624b7ba2013-06-12 14:03:49 -07004794 return NULL;
4795}
4796
4797
Eric Anholt4b2a4cb2012-03-29 17:29:20 -07004798static void
4799detect_conflicting_assignments(struct _mesa_glsl_parse_state *state,
4800 exec_list *instructions)
4801{
4802 bool gl_FragColor_assigned = false;
4803 bool gl_FragData_assigned = false;
4804 bool user_defined_fs_output_assigned = false;
4805 ir_variable *user_defined_fs_output = NULL;
4806
4807 /* It would be nice to have proper location information. */
4808 YYLTYPE loc;
4809 memset(&loc, 0, sizeof(loc));
4810
4811 foreach_list(node, instructions) {
4812 ir_variable *var = ((ir_instruction *)node)->as_variable();
4813
Eric Anholtb2ee5a02012-04-23 16:10:12 -07004814 if (!var || !var->assigned)
Eric Anholt4b2a4cb2012-03-29 17:29:20 -07004815 continue;
4816
4817 if (strcmp(var->name, "gl_FragColor") == 0)
Eric Anholtb2ee5a02012-04-23 16:10:12 -07004818 gl_FragColor_assigned = true;
Eric Anholt4b2a4cb2012-03-29 17:29:20 -07004819 else if (strcmp(var->name, "gl_FragData") == 0)
Eric Anholtb2ee5a02012-04-23 16:10:12 -07004820 gl_FragData_assigned = true;
Eric Anholt4b2a4cb2012-03-29 17:29:20 -07004821 else if (strncmp(var->name, "gl_", 3) != 0) {
4822 if (state->target == fragment_shader &&
Paul Berry42a29d82013-01-11 14:39:32 -08004823 var->mode == ir_var_shader_out) {
Eric Anholt4b2a4cb2012-03-29 17:29:20 -07004824 user_defined_fs_output_assigned = true;
4825 user_defined_fs_output = var;
4826 }
4827 }
4828 }
4829
4830 /* From the GLSL 1.30 spec:
4831 *
4832 * "If a shader statically assigns a value to gl_FragColor, it
4833 * may not assign a value to any element of gl_FragData. If a
4834 * shader statically writes a value to any element of
4835 * gl_FragData, it may not assign a value to
4836 * gl_FragColor. That is, a shader may assign values to either
4837 * gl_FragColor or gl_FragData, but not both. Multiple shaders
4838 * linked together must also consistently write just one of
4839 * these variables. Similarly, if user declared output
4840 * variables are in use (statically assigned to), then the
4841 * built-in variables gl_FragColor and gl_FragData may not be
4842 * assigned to. These incorrect usages all generate compile
4843 * time errors."
4844 */
4845 if (gl_FragColor_assigned && gl_FragData_assigned) {
4846 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
Paul Berry4d7899f2013-07-25 19:56:43 -07004847 "`gl_FragColor' and `gl_FragData'");
Eric Anholt4b2a4cb2012-03-29 17:29:20 -07004848 } else if (gl_FragColor_assigned && user_defined_fs_output_assigned) {
4849 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
Paul Berry4d7899f2013-07-25 19:56:43 -07004850 "`gl_FragColor' and `%s'",
Eric Anholt4b2a4cb2012-03-29 17:29:20 -07004851 user_defined_fs_output->name);
4852 } else if (gl_FragData_assigned && user_defined_fs_output_assigned) {
4853 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
Paul Berry4d7899f2013-07-25 19:56:43 -07004854 "`gl_FragData' and `%s'",
Eric Anholt4b2a4cb2012-03-29 17:29:20 -07004855 user_defined_fs_output->name);
4856 }
4857}