blob: 7383c42cbc75e93a380d9cdfd569856b16138b3b [file] [log] [blame]
Kenneth Graunke34350be2010-04-07 14:38:03 -07001/*
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 */
Eric Anholtac95f2f2010-06-22 10:38:52 -070023
Kenneth Graunke34350be2010-04-07 14:38:03 -070024#include <cstdarg>
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -070025
26extern "C" {
27#include <talloc.h>
28}
29
Kenneth Graunke34350be2010-04-07 14:38:03 -070030#include "ir_reader.h"
31#include "glsl_parser_extras.h"
32#include "glsl_types.h"
33#include "s_expression.h"
34
Kenneth Graunke4ec982f2010-04-21 15:47:34 -070035static void ir_read_error(_mesa_glsl_parse_state *, s_expression *,
36 const char *fmt, ...);
Kenneth Graunke57944972010-04-10 01:06:44 -070037static const glsl_type *read_type(_mesa_glsl_parse_state *, s_expression *);
Kenneth Graunked1d2ada2010-04-09 17:40:51 -070038
Kenneth Graunke09cad132010-04-21 17:52:36 -070039static void scan_for_prototypes(_mesa_glsl_parse_state *, exec_list *,
40 s_expression *);
Kenneth Graunke8df335d2010-04-21 18:14:06 -070041static ir_function *read_function(_mesa_glsl_parse_state *, s_list *,
42 bool skip_body);
Kenneth Graunkeb142aee2010-04-28 12:45:18 -070043static void read_function_sig(_mesa_glsl_parse_state *, ir_function *,
44 s_list *, bool skip_body);
Kenneth Graunke09cad132010-04-21 17:52:36 -070045
Kenneth Graunke3ea05822010-04-10 01:45:08 -070046static void read_instructions(_mesa_glsl_parse_state *, exec_list *,
Kenneth Graunke396fa9e2010-04-12 16:02:48 -070047 s_expression *, ir_loop *);
Kenneth Graunked1d2ada2010-04-09 17:40:51 -070048static ir_instruction *read_instruction(_mesa_glsl_parse_state *,
Kenneth Graunke396fa9e2010-04-12 16:02:48 -070049 s_expression *, ir_loop *);
Kenneth Graunked1d2ada2010-04-09 17:40:51 -070050static ir_variable *read_declaration(_mesa_glsl_parse_state *, s_list *);
Kenneth Graunke396fa9e2010-04-12 16:02:48 -070051static ir_if *read_if(_mesa_glsl_parse_state *, s_list *, ir_loop *);
Kenneth Graunke32b30522010-04-12 15:48:27 -070052static ir_loop *read_loop(_mesa_glsl_parse_state *st, s_list *list);
Kenneth Graunke9d2ff762010-04-09 17:56:22 -070053static ir_return *read_return(_mesa_glsl_parse_state *, s_list *);
Kenneth Graunked1d2ada2010-04-09 17:40:51 -070054
Kenneth Graunkef9556492010-04-07 17:24:44 -070055static ir_rvalue *read_rvalue(_mesa_glsl_parse_state *, s_expression *);
56static ir_assignment *read_assignment(_mesa_glsl_parse_state *, s_list *);
57static ir_expression *read_expression(_mesa_glsl_parse_state *, s_list *);
Kenneth Graunkeb51557f2010-04-28 12:54:21 -070058static ir_call *read_call(_mesa_glsl_parse_state *, s_list *);
Kenneth Graunkef9556492010-04-07 17:24:44 -070059static ir_swizzle *read_swizzle(_mesa_glsl_parse_state *, s_list *);
60static ir_constant *read_constant(_mesa_glsl_parse_state *, s_list *);
Kenneth Graunkedd5b4a52010-05-26 17:55:10 -070061static ir_texture *read_texture(_mesa_glsl_parse_state *, s_list *);
Kenneth Graunke3c7934b2010-05-26 17:52:44 -070062
63static ir_dereference *read_dereference(_mesa_glsl_parse_state *,
64 s_expression *);
Kenneth Graunke451381c2010-04-12 14:27:39 -070065static ir_dereference *read_var_ref(_mesa_glsl_parse_state *, s_list *);
66static ir_dereference *read_array_ref(_mesa_glsl_parse_state *, s_list *);
67static ir_dereference *read_record_ref(_mesa_glsl_parse_state *, s_list *);
Kenneth Graunkef9556492010-04-07 17:24:44 -070068
Kenneth Graunke34350be2010-04-07 14:38:03 -070069void
70_mesa_glsl_read_ir(_mesa_glsl_parse_state *state, exec_list *instructions,
71 const char *src)
72{
Carl Worth1660a292010-06-23 18:11:51 -070073 void *ctx = talloc_parent(state);
74 s_expression *expr = s_expression::read_expression(ctx, src);
Kenneth Graunke34350be2010-04-07 14:38:03 -070075 if (expr == NULL) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -070076 ir_read_error(state, NULL, "couldn't parse S-Expression.");
Kenneth Graunke34350be2010-04-07 14:38:03 -070077 return;
78 }
Kenneth Graunke34350be2010-04-07 14:38:03 -070079
Kenneth Graunke09cad132010-04-21 17:52:36 -070080 scan_for_prototypes(state, instructions, expr);
81 if (state->error)
82 return;
83
Kenneth Graunke396fa9e2010-04-12 16:02:48 -070084 read_instructions(state, instructions, expr, NULL);
Kenneth Graunke34350be2010-04-07 14:38:03 -070085}
86
Kenneth Graunkef9556492010-04-07 17:24:44 -070087static void
Kenneth Graunke4ec982f2010-04-21 15:47:34 -070088ir_read_error(_mesa_glsl_parse_state *state, s_expression *expr,
89 const char *fmt, ...)
Kenneth Graunkef9556492010-04-07 17:24:44 -070090{
Kenneth Graunkef9556492010-04-07 17:24:44 -070091 va_list ap;
92
Kenneth Graunke4ec982f2010-04-21 15:47:34 -070093 state->error = true;
Kenneth Graunkef9556492010-04-07 17:24:44 -070094
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -070095 state->info_log = talloc_strdup_append(state->info_log, "error: ");
Kenneth Graunkef9556492010-04-07 17:24:44 -070096
97 va_start(ap, fmt);
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -070098 state->info_log = talloc_vasprintf_append(state->info_log, fmt, ap);
Kenneth Graunkef9556492010-04-07 17:24:44 -070099 va_end(ap);
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -0700100 state->info_log = talloc_strdup_append(state->info_log, "\n");
Kenneth Graunkef9556492010-04-07 17:24:44 -0700101
Kenneth Graunke46a22322010-04-21 23:23:23 -0700102 if (expr != NULL) {
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -0700103 state->info_log = talloc_strdup_append(state->info_log,
104 "...in this context:\n ");
Kenneth Graunke46a22322010-04-21 23:23:23 -0700105 expr->print();
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -0700106 state->info_log = talloc_strdup_append(state->info_log, "\n\n");
Kenneth Graunke46a22322010-04-21 23:23:23 -0700107 }
Kenneth Graunkef9556492010-04-07 17:24:44 -0700108}
109
Kenneth Graunke57944972010-04-10 01:06:44 -0700110static const glsl_type *
Kenneth Graunkef9556492010-04-07 17:24:44 -0700111read_type(_mesa_glsl_parse_state *st, s_expression *expr)
112{
113 s_list *list = SX_AS_LIST(expr);
114 if (list != NULL) {
115 s_symbol *type_sym = SX_AS_SYMBOL(list->subexpressions.get_head());
116 if (type_sym == NULL) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700117 ir_read_error(st, expr, "expected type (array ...) or (struct ...)");
Kenneth Graunkef9556492010-04-07 17:24:44 -0700118 return NULL;
119 }
Kenneth Graunke57944972010-04-10 01:06:44 -0700120 if (strcmp(type_sym->value(), "array") == 0) {
121 if (list->length() != 3) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700122 ir_read_error(st, expr, "expected type (array <type> <int>)");
Kenneth Graunke57944972010-04-10 01:06:44 -0700123 return NULL;
124 }
125
126 // Read base type
127 s_expression *base_expr = (s_expression*) type_sym->next;
128 const glsl_type *base_type = read_type(st, base_expr);
129 if (base_type == NULL) {
Kenneth Graunke46a22322010-04-21 23:23:23 -0700130 ir_read_error(st, NULL, "when reading base type of array");
Kenneth Graunke57944972010-04-10 01:06:44 -0700131 return NULL;
132 }
133
134 // Read array size
135 s_int *size = SX_AS_INT(base_expr->next);
136 if (size == NULL) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700137 ir_read_error(st, expr, "found non-integer array size");
Kenneth Graunke57944972010-04-10 01:06:44 -0700138 return NULL;
139 }
140
Carl Worth12c41152010-06-18 17:52:59 -0700141 return glsl_type::get_array_instance(st, base_type, size->value());
Kenneth Graunke57944972010-04-10 01:06:44 -0700142 } else if (strcmp(type_sym->value(), "struct") == 0) {
Kenneth Graunkef9556492010-04-07 17:24:44 -0700143 assert(false); // FINISHME
Kenneth Graunke57944972010-04-10 01:06:44 -0700144 } else {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700145 ir_read_error(st, expr, "expected (array ...) or (struct ...); "
146 "found (%s ...)", type_sym->value());
Kenneth Graunke57944972010-04-10 01:06:44 -0700147 return NULL;
148 }
Kenneth Graunkef9556492010-04-07 17:24:44 -0700149 }
150
151 s_symbol *type_sym = SX_AS_SYMBOL(expr);
152 if (type_sym == NULL) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700153 ir_read_error(st, expr, "expected <type> (symbol or list)");
Kenneth Graunkef9556492010-04-07 17:24:44 -0700154 return NULL;
155 }
156
Kenneth Graunke57944972010-04-10 01:06:44 -0700157 const glsl_type *type = st->symbols->get_type(type_sym->value());
Kenneth Graunkef9556492010-04-07 17:24:44 -0700158 if (type == NULL)
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700159 ir_read_error(st, expr, "invalid type: %s", type_sym->value());
Kenneth Graunkef9556492010-04-07 17:24:44 -0700160
161 return type;
162}
163
Kenneth Graunked1d2ada2010-04-09 17:40:51 -0700164
Kenneth Graunke3ea05822010-04-10 01:45:08 -0700165static void
Kenneth Graunke09cad132010-04-21 17:52:36 -0700166scan_for_prototypes(_mesa_glsl_parse_state *st, exec_list *instructions,
167 s_expression *expr)
168{
169 s_list *list = SX_AS_LIST(expr);
170 if (list == NULL) {
171 ir_read_error(st, expr, "Expected (<instruction> ...); found an atom.");
172 return;
173 }
174
175 foreach_iter(exec_list_iterator, it, list->subexpressions) {
176 s_list *sub = SX_AS_LIST(it.get());
177 if (sub == NULL)
178 continue; // not a (function ...); ignore it.
179
180 s_symbol *tag = SX_AS_SYMBOL(sub->subexpressions.get_head());
181 if (tag == NULL || strcmp(tag->value(), "function") != 0)
182 continue; // not a (function ...); ignore it.
183
Kenneth Graunke8df335d2010-04-21 18:14:06 -0700184 ir_function *f = read_function(st, sub, true);
185 if (f == NULL)
Kenneth Graunke09cad132010-04-21 17:52:36 -0700186 return;
Kenneth Graunke8df335d2010-04-21 18:14:06 -0700187 instructions->push_tail(f);
Kenneth Graunke09cad132010-04-21 17:52:36 -0700188 }
189}
190
Kenneth Graunke8df335d2010-04-21 18:14:06 -0700191static ir_function *
192read_function(_mesa_glsl_parse_state *st, s_list *list, bool skip_body)
Kenneth Graunke09cad132010-04-21 17:52:36 -0700193{
Carl Worth1660a292010-06-23 18:11:51 -0700194 void *ctx = talloc_parent(st);
Kenneth Graunke09cad132010-04-21 17:52:36 -0700195 if (list->length() < 3) {
196 ir_read_error(st, list, "Expected (function <name> (signature ...) ...)");
Kenneth Graunke8df335d2010-04-21 18:14:06 -0700197 return NULL;
Kenneth Graunke09cad132010-04-21 17:52:36 -0700198 }
199
200 s_symbol *name = SX_AS_SYMBOL(list->subexpressions.head->next);
201 if (name == NULL) {
202 ir_read_error(st, list, "Expected (function <name> ...)");
Kenneth Graunke8df335d2010-04-21 18:14:06 -0700203 return NULL;
Kenneth Graunke09cad132010-04-21 17:52:36 -0700204 }
205
Kenneth Graunke8df335d2010-04-21 18:14:06 -0700206 ir_function *f = st->symbols->get_function(name->value());
207 if (f == NULL) {
Carl Worth1660a292010-06-23 18:11:51 -0700208 f = new(ctx) ir_function(name->value());
Kenneth Graunke8df335d2010-04-21 18:14:06 -0700209 bool added = st->symbols->add_function(name->value(), f);
210 assert(added);
Kenneth Graunke09cad132010-04-21 17:52:36 -0700211 }
212
Kenneth Graunke09cad132010-04-21 17:52:36 -0700213 exec_list_iterator it = list->subexpressions.iterator();
214 it.next(); // skip "function" tag
215 it.next(); // skip function name
216 for (/* nothing */; it.has_next(); it.next()) {
217 s_list *siglist = SX_AS_LIST(it.get());
218 if (siglist == NULL) {
219 ir_read_error(st, list, "Expected (function (signature ...) ...)");
Kenneth Graunke8df335d2010-04-21 18:14:06 -0700220 return NULL;
Kenneth Graunke09cad132010-04-21 17:52:36 -0700221 }
222
223 s_symbol *tag = SX_AS_SYMBOL(siglist->subexpressions.get_head());
224 if (tag == NULL || strcmp(tag->value(), "signature") != 0) {
225 ir_read_error(st, siglist, "Expected (signature ...)");
Kenneth Graunke8df335d2010-04-21 18:14:06 -0700226 return NULL;
Kenneth Graunke09cad132010-04-21 17:52:36 -0700227 }
228
Kenneth Graunkeb142aee2010-04-28 12:45:18 -0700229 read_function_sig(st, f, siglist, skip_body);
Kenneth Graunke09cad132010-04-21 17:52:36 -0700230 }
Kenneth Graunke8df335d2010-04-21 18:14:06 -0700231 return f;
Kenneth Graunke09cad132010-04-21 17:52:36 -0700232}
233
Kenneth Graunkeb142aee2010-04-28 12:45:18 -0700234static void
235read_function_sig(_mesa_glsl_parse_state *st, ir_function *f, s_list *list,
236 bool skip_body)
Kenneth Graunke09cad132010-04-21 17:52:36 -0700237{
Carl Worth1660a292010-06-23 18:11:51 -0700238 void *ctx = talloc_parent(st);
Kenneth Graunke09cad132010-04-21 17:52:36 -0700239 if (list->length() != 4) {
240 ir_read_error(st, list, "Expected (signature <type> (parameters ...) "
241 "(<instruction> ...))");
Kenneth Graunkeb142aee2010-04-28 12:45:18 -0700242 return;
Kenneth Graunke09cad132010-04-21 17:52:36 -0700243 }
244
245 s_expression *type_expr = (s_expression*) list->subexpressions.head->next;
246 const glsl_type *return_type = read_type(st, type_expr);
247 if (return_type == NULL)
Kenneth Graunkeb142aee2010-04-28 12:45:18 -0700248 return;
Kenneth Graunke09cad132010-04-21 17:52:36 -0700249
250 s_list *paramlist = SX_AS_LIST(type_expr->next);
Kenneth Graunke8df335d2010-04-21 18:14:06 -0700251 s_list *body_list = SX_AS_LIST(paramlist->next);
252 if (paramlist == NULL || body_list == NULL) {
253 ir_read_error(st, list, "Expected (signature <type> (parameters ...) "
254 "(<instruction> ...))");
Kenneth Graunkeb142aee2010-04-28 12:45:18 -0700255 return;
Kenneth Graunke09cad132010-04-21 17:52:36 -0700256 }
257 s_symbol *paramtag = SX_AS_SYMBOL(paramlist->subexpressions.get_head());
258 if (paramtag == NULL || strcmp(paramtag->value(), "parameters") != 0) {
259 ir_read_error(st, paramlist, "Expected (parameters ...)");
Kenneth Graunkeb142aee2010-04-28 12:45:18 -0700260 return;
Kenneth Graunke09cad132010-04-21 17:52:36 -0700261 }
262
Kenneth Graunkeb142aee2010-04-28 12:45:18 -0700263 // Read the parameters list into a temporary place.
264 exec_list hir_parameters;
Kenneth Graunke8df335d2010-04-21 18:14:06 -0700265 st->symbols->push_scope();
266
Kenneth Graunke09cad132010-04-21 17:52:36 -0700267 exec_list_iterator it = paramlist->subexpressions.iterator();
268 for (it.next() /* skip "parameters" */; it.has_next(); it.next()) {
269 s_list *decl = SX_AS_LIST(it.get());
270 ir_variable *var = read_declaration(st, decl);
Kenneth Graunkeb142aee2010-04-28 12:45:18 -0700271 if (var == NULL)
272 return;
Kenneth Graunke09cad132010-04-21 17:52:36 -0700273
Kenneth Graunkeb142aee2010-04-28 12:45:18 -0700274 hir_parameters.push_tail(var);
Kenneth Graunke09cad132010-04-21 17:52:36 -0700275 }
276
Kenneth Graunkeb142aee2010-04-28 12:45:18 -0700277 ir_function_signature *sig = f->exact_matching_signature(&hir_parameters);
278 if (sig != NULL) {
279 const char *badvar = sig->qualifiers_match(&hir_parameters);
280 if (badvar != NULL) {
281 ir_read_error(st, list, "function `%s' parameter `%s' qualifiers "
282 "don't match prototype", f->name, badvar);
283 return;
284 }
285
286 if (sig->return_type != return_type) {
287 ir_read_error(st, list, "function `%s' return type doesn't "
288 "match prototype", f->name);
289 return;
290 }
291 } else {
Carl Worth1660a292010-06-23 18:11:51 -0700292 sig = new(ctx) ir_function_signature(return_type);
Kenneth Graunkeb142aee2010-04-28 12:45:18 -0700293 f->add_signature(sig);
294 }
295
296 sig->replace_parameters(&hir_parameters);
297
298 if (!skip_body) {
299 if (sig->is_defined) {
300 ir_read_error(st, list, "function %s redefined", f->name);
301 return;
302 }
Kenneth Graunke8df335d2010-04-21 18:14:06 -0700303 read_instructions(st, &sig->body, body_list, NULL);
Kenneth Graunkef6c90d82010-04-28 23:17:58 -0700304 sig->is_defined = true;
Kenneth Graunkeb142aee2010-04-28 12:45:18 -0700305 }
Kenneth Graunke8df335d2010-04-21 18:14:06 -0700306
307 st->symbols->pop_scope();
Kenneth Graunke09cad132010-04-21 17:52:36 -0700308}
309
310static void
Kenneth Graunke3ea05822010-04-10 01:45:08 -0700311read_instructions(_mesa_glsl_parse_state *st, exec_list *instructions,
Kenneth Graunke396fa9e2010-04-12 16:02:48 -0700312 s_expression *expr, ir_loop *loop_ctx)
Kenneth Graunke3ea05822010-04-10 01:45:08 -0700313{
314 // Read in a list of instructions
315 s_list *list = SX_AS_LIST(expr);
316 if (list == NULL) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700317 ir_read_error(st, expr, "Expected (<instruction> ...); found an atom.");
Kenneth Graunke3ea05822010-04-10 01:45:08 -0700318 return;
319 }
320
321 foreach_iter(exec_list_iterator, it, list->subexpressions) {
322 s_expression *sub = (s_expression*) it.get();
Kenneth Graunke396fa9e2010-04-12 16:02:48 -0700323 ir_instruction *ir = read_instruction(st, sub, loop_ctx);
Kenneth Graunke3ea05822010-04-10 01:45:08 -0700324 if (ir == NULL) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700325 ir_read_error(st, sub, "Invalid instruction.\n");
Kenneth Graunke3ea05822010-04-10 01:45:08 -0700326 return;
327 }
328 instructions->push_tail(ir);
329 }
330}
331
332
Kenneth Graunked1d2ada2010-04-09 17:40:51 -0700333static ir_instruction *
Kenneth Graunke396fa9e2010-04-12 16:02:48 -0700334read_instruction(_mesa_glsl_parse_state *st, s_expression *expr,
335 ir_loop *loop_ctx)
Kenneth Graunked1d2ada2010-04-09 17:40:51 -0700336{
Carl Worth1660a292010-06-23 18:11:51 -0700337 void *ctx = talloc_parent(st);
Kenneth Graunke396fa9e2010-04-12 16:02:48 -0700338 s_symbol *symbol = SX_AS_SYMBOL(expr);
339 if (symbol != NULL) {
340 if (strcmp(symbol->value(), "break") == 0 && loop_ctx != NULL)
Carl Worth1660a292010-06-23 18:11:51 -0700341 return new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
Kenneth Graunke396fa9e2010-04-12 16:02:48 -0700342 if (strcmp(symbol->value(), "continue") == 0 && loop_ctx != NULL)
Carl Worth1660a292010-06-23 18:11:51 -0700343 return new(ctx) ir_loop_jump(ir_loop_jump::jump_continue);
Kenneth Graunke396fa9e2010-04-12 16:02:48 -0700344 }
345
Kenneth Graunked1d2ada2010-04-09 17:40:51 -0700346 s_list *list = SX_AS_LIST(expr);
347 if (list == NULL || list->subexpressions.is_empty())
348 return NULL;
349
350 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
351 if (tag == NULL) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700352 ir_read_error(st, expr, "expected instruction tag");
Kenneth Graunked1d2ada2010-04-09 17:40:51 -0700353 return NULL;
354 }
355
356 ir_instruction *inst = NULL;
Kenneth Graunke46ef8f12010-04-12 14:25:41 -0700357 if (strcmp(tag->value(), "declare") == 0) {
Kenneth Graunked1d2ada2010-04-09 17:40:51 -0700358 inst = read_declaration(st, list);
Kenneth Graunke46ef8f12010-04-12 14:25:41 -0700359 } else if (strcmp(tag->value(), "if") == 0) {
Kenneth Graunke396fa9e2010-04-12 16:02:48 -0700360 inst = read_if(st, list, loop_ctx);
Kenneth Graunke32b30522010-04-12 15:48:27 -0700361 } else if (strcmp(tag->value(), "loop") == 0) {
362 inst = read_loop(st, list);
Kenneth Graunke46ef8f12010-04-12 14:25:41 -0700363 } else if (strcmp(tag->value(), "return") == 0) {
Kenneth Graunke9d2ff762010-04-09 17:56:22 -0700364 inst = read_return(st, list);
Kenneth Graunke8df335d2010-04-21 18:14:06 -0700365 } else if (strcmp(tag->value(), "function") == 0) {
366 inst = read_function(st, list, false);
Kenneth Graunke46ef8f12010-04-12 14:25:41 -0700367 } else {
368 inst = read_rvalue(st, list);
369 if (inst == NULL)
Kenneth Graunke46a22322010-04-21 23:23:23 -0700370 ir_read_error(st, NULL, "when reading instruction");
Kenneth Graunke46ef8f12010-04-12 14:25:41 -0700371 }
Kenneth Graunked1d2ada2010-04-09 17:40:51 -0700372 return inst;
373}
374
375
376static ir_variable *
377read_declaration(_mesa_glsl_parse_state *st, s_list *list)
378{
Carl Worth1660a292010-06-23 18:11:51 -0700379 void *ctx = talloc_parent(st);
Kenneth Graunked1d2ada2010-04-09 17:40:51 -0700380 if (list->length() != 4) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700381 ir_read_error(st, list, "expected (declare (<qualifiers>) <type> "
382 "<name>)");
Kenneth Graunked1d2ada2010-04-09 17:40:51 -0700383 return NULL;
384 }
385
386 s_list *quals = SX_AS_LIST(list->subexpressions.head->next);
387 if (quals == NULL) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700388 ir_read_error(st, list, "expected a list of variable qualifiers");
Kenneth Graunked1d2ada2010-04-09 17:40:51 -0700389 return NULL;
390 }
391
392 s_expression *type_expr = (s_expression*) quals->next;
Kenneth Graunke57944972010-04-10 01:06:44 -0700393 const glsl_type *type = read_type(st, type_expr);
Kenneth Graunked1d2ada2010-04-09 17:40:51 -0700394 if (type == NULL)
395 return NULL;
396
397 s_symbol *var_name = SX_AS_SYMBOL(type_expr->next);
398 if (var_name == NULL) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700399 ir_read_error(st, list, "expected variable name, found non-symbol");
Kenneth Graunked1d2ada2010-04-09 17:40:51 -0700400 return NULL;
401 }
402
Carl Worth1660a292010-06-23 18:11:51 -0700403 ir_variable *var = new(ctx) ir_variable(type, var_name->value());
Kenneth Graunked1d2ada2010-04-09 17:40:51 -0700404
405 foreach_iter(exec_list_iterator, it, quals->subexpressions) {
406 s_symbol *qualifier = SX_AS_SYMBOL(it.get());
407 if (qualifier == NULL) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700408 ir_read_error(st, list, "qualifier list must contain only symbols");
Kenneth Graunked1d2ada2010-04-09 17:40:51 -0700409 delete var;
410 return NULL;
411 }
412
413 // FINISHME: Check for duplicate/conflicting qualifiers.
414 if (strcmp(qualifier->value(), "centroid") == 0) {
415 var->centroid = 1;
416 } else if (strcmp(qualifier->value(), "invariant") == 0) {
417 var->invariant = 1;
418 } else if (strcmp(qualifier->value(), "uniform") == 0) {
419 var->mode = ir_var_uniform;
420 } else if (strcmp(qualifier->value(), "auto") == 0) {
421 var->mode = ir_var_auto;
422 } else if (strcmp(qualifier->value(), "in") == 0) {
423 var->mode = ir_var_in;
424 } else if (strcmp(qualifier->value(), "out") == 0) {
425 var->mode = ir_var_out;
426 } else if (strcmp(qualifier->value(), "inout") == 0) {
427 var->mode = ir_var_inout;
428 } else if (strcmp(qualifier->value(), "smooth") == 0) {
429 var->interpolation = ir_var_smooth;
430 } else if (strcmp(qualifier->value(), "flat") == 0) {
431 var->interpolation = ir_var_flat;
432 } else if (strcmp(qualifier->value(), "noperspective") == 0) {
433 var->interpolation = ir_var_noperspective;
434 } else {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700435 ir_read_error(st, list, "unknown qualifier: %s", qualifier->value());
Kenneth Graunked1d2ada2010-04-09 17:40:51 -0700436 delete var;
437 return NULL;
438 }
439 }
440
441 // Add the variable to the symbol table
442 st->symbols->add_variable(var_name->value(), var);
443
444 return var;
445}
446
447
Kenneth Graunke3ea05822010-04-10 01:45:08 -0700448static ir_if *
Kenneth Graunke396fa9e2010-04-12 16:02:48 -0700449read_if(_mesa_glsl_parse_state *st, s_list *list, ir_loop *loop_ctx)
Kenneth Graunke3ea05822010-04-10 01:45:08 -0700450{
Carl Worth1660a292010-06-23 18:11:51 -0700451 void *ctx = talloc_parent(st);
Kenneth Graunke3ea05822010-04-10 01:45:08 -0700452 if (list->length() != 4) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700453 ir_read_error(st, list, "expected (if <condition> (<then> ...) "
Kenneth Graunke3ea05822010-04-10 01:45:08 -0700454 "(<else> ...))");
455 return NULL;
456 }
457
458 s_expression *cond_expr = (s_expression*) list->subexpressions.head->next;
459 ir_rvalue *condition = read_rvalue(st, cond_expr);
460 if (condition == NULL) {
Kenneth Graunke46a22322010-04-21 23:23:23 -0700461 ir_read_error(st, NULL, "when reading condition of (if ...)");
Kenneth Graunke3ea05822010-04-10 01:45:08 -0700462 return NULL;
463 }
464
465 s_expression *then_expr = (s_expression*) cond_expr->next;
466 s_expression *else_expr = (s_expression*) then_expr->next;
467
Carl Worth1660a292010-06-23 18:11:51 -0700468 ir_if *iff = new(ctx) ir_if(condition);
Kenneth Graunke3ea05822010-04-10 01:45:08 -0700469
Kenneth Graunke396fa9e2010-04-12 16:02:48 -0700470 read_instructions(st, &iff->then_instructions, then_expr, loop_ctx);
471 read_instructions(st, &iff->else_instructions, else_expr, loop_ctx);
Kenneth Graunke3ea05822010-04-10 01:45:08 -0700472 if (st->error) {
473 delete iff;
474 iff = NULL;
475 }
476 return iff;
477}
478
479
Kenneth Graunke32b30522010-04-12 15:48:27 -0700480static ir_loop *
481read_loop(_mesa_glsl_parse_state *st, s_list *list)
482{
Carl Worth1660a292010-06-23 18:11:51 -0700483 void *ctx = talloc_parent(st);
Kenneth Graunke32b30522010-04-12 15:48:27 -0700484 if (list->length() != 6) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700485 ir_read_error(st, list, "expected (loop <counter> <from> <to> "
486 "<increment> <body>)");
Kenneth Graunke32b30522010-04-12 15:48:27 -0700487 return NULL;
488 }
489
490 s_expression *count_expr = (s_expression*) list->subexpressions.head->next;
491 s_expression *from_expr = (s_expression*) count_expr->next;
492 s_expression *to_expr = (s_expression*) from_expr->next;
493 s_expression *inc_expr = (s_expression*) to_expr->next;
494 s_expression *body_expr = (s_expression*) inc_expr->next;
495
496 // FINISHME: actually read the count/from/to fields.
497
Carl Worth1660a292010-06-23 18:11:51 -0700498 ir_loop *loop = new(ctx) ir_loop;
Kenneth Graunke396fa9e2010-04-12 16:02:48 -0700499 read_instructions(st, &loop->body_instructions, body_expr, loop);
Kenneth Graunke32b30522010-04-12 15:48:27 -0700500 if (st->error) {
501 delete loop;
502 loop = NULL;
503 }
504 return loop;
505}
506
507
Kenneth Graunke9d2ff762010-04-09 17:56:22 -0700508static ir_return *
509read_return(_mesa_glsl_parse_state *st, s_list *list)
510{
Carl Worth1660a292010-06-23 18:11:51 -0700511 void *ctx = talloc_parent(st);
Kenneth Graunke9d2ff762010-04-09 17:56:22 -0700512 if (list->length() != 2) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700513 ir_read_error(st, list, "expected (return <rvalue>)");
Kenneth Graunke9d2ff762010-04-09 17:56:22 -0700514 return NULL;
515 }
516
517 s_expression *expr = (s_expression*) list->subexpressions.head->next;
518
519 ir_rvalue *retval = read_rvalue(st, expr);
520 if (retval == NULL) {
Kenneth Graunke46a22322010-04-21 23:23:23 -0700521 ir_read_error(st, NULL, "when reading return value");
Kenneth Graunke9d2ff762010-04-09 17:56:22 -0700522 return NULL;
523 }
524
Carl Worth1660a292010-06-23 18:11:51 -0700525 return new(ctx) ir_return(retval);
Kenneth Graunke9d2ff762010-04-09 17:56:22 -0700526}
527
528
Kenneth Graunkef9556492010-04-07 17:24:44 -0700529static ir_rvalue *
530read_rvalue(_mesa_glsl_parse_state *st, s_expression *expr)
531{
532 s_list *list = SX_AS_LIST(expr);
533 if (list == NULL || list->subexpressions.is_empty())
534 return NULL;
535
536 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
537 if (tag == NULL) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700538 ir_read_error(st, expr, "expected rvalue tag");
Kenneth Graunkef9556492010-04-07 17:24:44 -0700539 return NULL;
540 }
541
Kenneth Graunke3c7934b2010-05-26 17:52:44 -0700542 ir_rvalue *rvalue = read_dereference(st, list);
543 if (rvalue != NULL || st->error)
544 return rvalue;
545 else if (strcmp(tag->value(), "swiz") == 0) {
Kenneth Graunkef9556492010-04-07 17:24:44 -0700546 rvalue = read_swizzle(st, list);
Kenneth Graunke451381c2010-04-12 14:27:39 -0700547 } else if (strcmp(tag->value(), "assign") == 0) {
Kenneth Graunkef9556492010-04-07 17:24:44 -0700548 rvalue = read_assignment(st, list);
Kenneth Graunke451381c2010-04-12 14:27:39 -0700549 } else if (strcmp(tag->value(), "expression") == 0) {
Kenneth Graunkef9556492010-04-07 17:24:44 -0700550 rvalue = read_expression(st, list);
Kenneth Graunkeb51557f2010-04-28 12:54:21 -0700551 } else if (strcmp(tag->value(), "call") == 0) {
552 rvalue = read_call(st, list);
Kenneth Graunke451381c2010-04-12 14:27:39 -0700553 } else if (strcmp(tag->value(), "constant") == 0) {
Kenneth Graunkef9556492010-04-07 17:24:44 -0700554 rvalue = read_constant(st, list);
Kenneth Graunke451381c2010-04-12 14:27:39 -0700555 } else {
Kenneth Graunkedd5b4a52010-05-26 17:55:10 -0700556 rvalue = read_texture(st, list);
557 if (rvalue == NULL && !st->error)
558 ir_read_error(st, expr, "unrecognized rvalue tag: %s", tag->value());
Kenneth Graunke451381c2010-04-12 14:27:39 -0700559 }
Kenneth Graunkef9556492010-04-07 17:24:44 -0700560
561 return rvalue;
562}
563
564static ir_assignment *
565read_assignment(_mesa_glsl_parse_state *st, s_list *list)
566{
Carl Worth1660a292010-06-23 18:11:51 -0700567 void *ctx = talloc_parent(st);
Kenneth Graunkef9556492010-04-07 17:24:44 -0700568 if (list->length() != 4) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700569 ir_read_error(st, list, "expected (assign <condition> <lhs> <rhs>)");
Kenneth Graunkef9556492010-04-07 17:24:44 -0700570 return NULL;
571 }
572
573 s_expression *cond_expr = (s_expression*) list->subexpressions.head->next;
574 s_expression *lhs_expr = (s_expression*) cond_expr->next;
575 s_expression *rhs_expr = (s_expression*) lhs_expr->next;
576
577 // FINISHME: Deal with "true" condition
578 ir_rvalue *condition = read_rvalue(st, cond_expr);
579 if (condition == NULL) {
Kenneth Graunke46a22322010-04-21 23:23:23 -0700580 ir_read_error(st, NULL, "when reading condition of assignment");
Kenneth Graunkef9556492010-04-07 17:24:44 -0700581 return NULL;
582 }
583
584 ir_rvalue *lhs = read_rvalue(st, lhs_expr);
585 if (lhs == NULL) {
Kenneth Graunke46a22322010-04-21 23:23:23 -0700586 ir_read_error(st, NULL, "when reading left-hand side of assignment");
Kenneth Graunkef9556492010-04-07 17:24:44 -0700587 return NULL;
588 }
589
590 ir_rvalue *rhs = read_rvalue(st, rhs_expr);
591 if (rhs == NULL) {
Kenneth Graunke46a22322010-04-21 23:23:23 -0700592 ir_read_error(st, NULL, "when reading right-hand side of assignment");
Kenneth Graunkef9556492010-04-07 17:24:44 -0700593 return NULL;
594 }
595
Carl Worth1660a292010-06-23 18:11:51 -0700596 return new(ctx) ir_assignment(lhs, rhs, condition);
Kenneth Graunkef9556492010-04-07 17:24:44 -0700597}
598
Kenneth Graunkeb51557f2010-04-28 12:54:21 -0700599static ir_call *
600read_call(_mesa_glsl_parse_state *st, s_list *list)
601{
Carl Worth1660a292010-06-23 18:11:51 -0700602 void *ctx = talloc_parent(st);
Kenneth Graunkeb51557f2010-04-28 12:54:21 -0700603 if (list->length() != 3) {
604 ir_read_error(st, list, "expected (call <name> (<param> ...))");
605 return NULL;
606 }
607
608 s_symbol *name = SX_AS_SYMBOL(list->subexpressions.head->next);
609 s_list *params = SX_AS_LIST(name->next);
610 if (name == NULL || params == NULL) {
611 ir_read_error(st, list, "expected (call <name> (<param> ...))");
612 return NULL;
613 }
614
615 exec_list parameters;
616
617 foreach_iter(exec_list_iterator, it, params->subexpressions) {
618 s_expression *expr = (s_expression*) it.get();
619 ir_rvalue *param = read_rvalue(st, expr);
620 if (param == NULL) {
621 ir_read_error(st, list, "when reading parameter to function call");
622 return NULL;
623 }
624 parameters.push_tail(param);
625 }
626
627 ir_function *f = st->symbols->get_function(name->value());
628 if (f == NULL) {
629 ir_read_error(st, list, "found call to undefined function %s",
630 name->value());
631 return NULL;
632 }
633
634 const ir_function_signature *callee = f->matching_signature(&parameters);
635 if (callee == NULL) {
636 ir_read_error(st, list, "couldn't find matching signature for function "
637 "%s", name->value());
638 return NULL;
639 }
640
Carl Worth1660a292010-06-23 18:11:51 -0700641 return new(ctx) ir_call(callee, &parameters);
Kenneth Graunkeb51557f2010-04-28 12:54:21 -0700642}
Kenneth Graunkef9556492010-04-07 17:24:44 -0700643
644static ir_expression *
645read_expression(_mesa_glsl_parse_state *st, s_list *list)
646{
Carl Worth1660a292010-06-23 18:11:51 -0700647 void *ctx = talloc_parent(st);
Kenneth Graunkef9556492010-04-07 17:24:44 -0700648 const unsigned list_length = list->length();
649 if (list_length < 4) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700650 ir_read_error(st, list, "expected (expression <type> <operator> "
651 "<operand> [<operand>])");
Kenneth Graunkef9556492010-04-07 17:24:44 -0700652 return NULL;
653 }
654
655 s_expression *type_expr = (s_expression*) list->subexpressions.head->next;
Kenneth Graunke57944972010-04-10 01:06:44 -0700656 const glsl_type *type = read_type(st, type_expr);
Kenneth Graunkef9556492010-04-07 17:24:44 -0700657 if (type == NULL)
658 return NULL;
659
660 /* Read the operator */
661 s_symbol *op_sym = SX_AS_SYMBOL(type_expr->next);
662 if (op_sym == NULL) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700663 ir_read_error(st, list, "expected operator, found non-symbol");
Kenneth Graunkef9556492010-04-07 17:24:44 -0700664 return NULL;
665 }
666
667 ir_expression_operation op = ir_expression::get_operator(op_sym->value());
668 if (op == (ir_expression_operation) -1) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700669 ir_read_error(st, list, "invalid operator: %s", op_sym->value());
Kenneth Graunkef9556492010-04-07 17:24:44 -0700670 return NULL;
671 }
672
673 /* Now that we know the operator, check for the right number of operands */
674 if (ir_expression::get_num_operands(op) == 2) {
675 if (list_length != 5) {
Kenneth Graunke21128c22010-04-21 23:46:22 -0700676 ir_read_error(st, list, "expected (expression <type> %s <operand> "
677 " <operand>)", op_sym->value());
Kenneth Graunkef9556492010-04-07 17:24:44 -0700678 return NULL;
679 }
680 } else {
681 if (list_length != 4) {
Kenneth Graunke21128c22010-04-21 23:46:22 -0700682 ir_read_error(st, list, "expected (expression <type> %s <operand>)",
Kenneth Graunkef9556492010-04-07 17:24:44 -0700683 op_sym->value());
684 return NULL;
685 }
686 }
687
688 s_expression *exp1 = (s_expression*) (op_sym->next);
689 ir_rvalue *arg1 = read_rvalue(st, exp1);
690 if (arg1 == NULL) {
Kenneth Graunke46a22322010-04-21 23:23:23 -0700691 ir_read_error(st, NULL, "when reading first operand of %s",
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700692 op_sym->value());
Kenneth Graunkef9556492010-04-07 17:24:44 -0700693 return NULL;
694 }
695
696 ir_rvalue *arg2 = NULL;
697 if (ir_expression::get_num_operands(op) == 2) {
698 s_expression *exp2 = (s_expression*) (exp1->next);
699 arg2 = read_rvalue(st, exp2);
700 if (arg2 == NULL) {
Kenneth Graunke46a22322010-04-21 23:23:23 -0700701 ir_read_error(st, NULL, "when reading second operand of %s",
Kenneth Graunkef9556492010-04-07 17:24:44 -0700702 op_sym->value());
703 return NULL;
704 }
705 }
706
Carl Worth1660a292010-06-23 18:11:51 -0700707 return new(ctx) ir_expression(op, type, arg1, arg2);
Kenneth Graunkef9556492010-04-07 17:24:44 -0700708}
709
710static ir_swizzle *
711read_swizzle(_mesa_glsl_parse_state *st, s_list *list)
712{
713 if (list->length() != 3) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700714 ir_read_error(st, list, "expected (swiz <swizzle> <rvalue>)");
Kenneth Graunkef9556492010-04-07 17:24:44 -0700715 return NULL;
716 }
717
718 s_symbol *swiz = SX_AS_SYMBOL(list->subexpressions.head->next);
719 if (swiz == NULL) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700720 ir_read_error(st, list, "expected a valid swizzle; found non-symbol");
Kenneth Graunkef9556492010-04-07 17:24:44 -0700721 return NULL;
722 }
723
Kenneth Graunke95163222010-04-21 23:15:08 -0700724 if (strlen(swiz->value()) > 4) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700725 ir_read_error(st, list, "expected a valid swizzle; found %s",
726 swiz->value());
Kenneth Graunkef9556492010-04-07 17:24:44 -0700727 return NULL;
728 }
729
730 s_expression *sub = (s_expression*) swiz->next;
731 if (sub == NULL) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700732 ir_read_error(st, list, "expected rvalue: (swizzle %s <rvalue>)",
733 swiz->value());
Kenneth Graunkef9556492010-04-07 17:24:44 -0700734 return NULL;
735 }
736
737 ir_rvalue *rvalue = read_rvalue(st, sub);
738 if (rvalue == NULL)
739 return NULL;
740
Kenneth Graunkebf783ec2010-04-28 13:16:31 -0700741 ir_swizzle *ir = ir_swizzle::create(rvalue, swiz->value(),
742 rvalue->type->vector_elements);
743 if (ir == NULL)
744 ir_read_error(st, list, "invalid swizzle");
745
746 return ir;
Kenneth Graunkef9556492010-04-07 17:24:44 -0700747}
748
749static ir_constant *
750read_constant(_mesa_glsl_parse_state *st, s_list *list)
751{
Carl Worth1660a292010-06-23 18:11:51 -0700752 void *ctx = talloc_parent(st);
Kenneth Graunkef9556492010-04-07 17:24:44 -0700753 if (list->length() != 3) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700754 ir_read_error(st, list, "expected (constant <type> (<num> ... <num>))");
Kenneth Graunkef9556492010-04-07 17:24:44 -0700755 return NULL;
756 }
757
758 s_expression *type_expr = (s_expression*) list->subexpressions.head->next;
Kenneth Graunke57944972010-04-10 01:06:44 -0700759 const glsl_type *type = read_type(st, type_expr);
Kenneth Graunkef9556492010-04-07 17:24:44 -0700760 if (type == NULL)
761 return NULL;
762
763 s_list *values = SX_AS_LIST(type_expr->next);
764 if (values == NULL) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700765 ir_read_error(st, list, "expected (constant <type> (<num> ... <num>))");
Kenneth Graunkef9556492010-04-07 17:24:44 -0700766 return NULL;
767 }
768
769 const glsl_type *const base_type = type->get_base_type();
770
Ian Romanick4976e572010-06-11 16:43:42 -0700771 ir_constant_data data;
Kenneth Graunkef9556492010-04-07 17:24:44 -0700772
773 // Read in list of values (at most 16).
774 int k = 0;
775 foreach_iter(exec_list_iterator, it, values->subexpressions) {
776 if (k >= 16) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700777 ir_read_error(st, values, "expected at most 16 numbers");
Kenneth Graunkef9556492010-04-07 17:24:44 -0700778 return NULL;
779 }
780
781 s_expression *expr = (s_expression*) it.get();
782
783 if (base_type->base_type == GLSL_TYPE_FLOAT) {
784 s_number *value = SX_AS_NUMBER(expr);
785 if (value == NULL) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700786 ir_read_error(st, values, "expected numbers");
Kenneth Graunkef9556492010-04-07 17:24:44 -0700787 return NULL;
788 }
Ian Romanick4976e572010-06-11 16:43:42 -0700789 data.f[k] = value->fvalue();
Kenneth Graunkef9556492010-04-07 17:24:44 -0700790 } else {
791 s_int *value = SX_AS_INT(expr);
792 if (value == NULL) {
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700793 ir_read_error(st, values, "expected integers");
Kenneth Graunkef9556492010-04-07 17:24:44 -0700794 return NULL;
795 }
796
797 switch (base_type->base_type) {
798 case GLSL_TYPE_UINT: {
Ian Romanick4976e572010-06-11 16:43:42 -0700799 data.u[k] = value->value();
Kenneth Graunkef9556492010-04-07 17:24:44 -0700800 break;
801 }
802 case GLSL_TYPE_INT: {
Ian Romanick4976e572010-06-11 16:43:42 -0700803 data.i[k] = value->value();
Kenneth Graunkef9556492010-04-07 17:24:44 -0700804 break;
805 }
806 case GLSL_TYPE_BOOL: {
Ian Romanick4976e572010-06-11 16:43:42 -0700807 data.b[k] = value->value();
Kenneth Graunkef9556492010-04-07 17:24:44 -0700808 break;
809 }
810 default:
Kenneth Graunke4ec982f2010-04-21 15:47:34 -0700811 ir_read_error(st, values, "unsupported constant type");
Kenneth Graunkef9556492010-04-07 17:24:44 -0700812 return NULL;
813 }
814 }
815 ++k;
816 }
Ian Romanick4976e572010-06-11 16:43:42 -0700817
Carl Worth1660a292010-06-23 18:11:51 -0700818 return new(ctx) ir_constant(type, &data);
Kenneth Graunkef9556492010-04-07 17:24:44 -0700819}
Kenneth Graunke451381c2010-04-12 14:27:39 -0700820
Kenneth Graunke451381c2010-04-12 14:27:39 -0700821static ir_dereference *
Kenneth Graunke3c7934b2010-05-26 17:52:44 -0700822read_dereference(_mesa_glsl_parse_state *st, s_expression *expr)
823{
824 s_list *list = SX_AS_LIST(expr);
825 if (list == NULL || list->subexpressions.is_empty())
826 return NULL;
827
828 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.head);
829 assert(tag != NULL);
830
831 if (strcmp(tag->value(), "var_ref") == 0)
832 return read_var_ref(st, list);
833 if (strcmp(tag->value(), "array_ref") == 0)
834 return read_array_ref(st, list);
835 if (strcmp(tag->value(), "record_ref") == 0)
836 return read_record_ref(st, list);
837 return NULL;
838}
839
840static ir_dereference *
Kenneth Graunke451381c2010-04-12 14:27:39 -0700841read_var_ref(_mesa_glsl_parse_state *st, s_list *list)
842{
Carl Worth1660a292010-06-23 18:11:51 -0700843 void *ctx = talloc_parent(st);
Kenneth Graunke451381c2010-04-12 14:27:39 -0700844 if (list->length() != 2) {
Kenneth Graunke350bd702010-05-26 13:03:14 -0700845 ir_read_error(st, list, "expected (var_ref <variable name>)");
Kenneth Graunke451381c2010-04-12 14:27:39 -0700846 return NULL;
847 }
Kenneth Graunke350bd702010-05-26 13:03:14 -0700848 s_symbol *var_name = SX_AS_SYMBOL(list->subexpressions.head->next);
849 if (var_name == NULL) {
850 ir_read_error(st, list, "expected (var_ref <variable name>)");
Kenneth Graunke451381c2010-04-12 14:27:39 -0700851 return NULL;
Kenneth Graunke350bd702010-05-26 13:03:14 -0700852 }
853
854 ir_variable *var = st->symbols->get_variable(var_name->value());
855 if (var == NULL) {
856 ir_read_error(st, list, "undeclared variable: %s", var_name->value());
857 return NULL;
858 }
859
Carl Worth1660a292010-06-23 18:11:51 -0700860 return new(ctx) ir_dereference_variable(var);
Kenneth Graunke451381c2010-04-12 14:27:39 -0700861}
862
863static ir_dereference *
864read_array_ref(_mesa_glsl_parse_state *st, s_list *list)
865{
Carl Worth1660a292010-06-23 18:11:51 -0700866 void *ctx = talloc_parent(st);
Kenneth Graunke451381c2010-04-12 14:27:39 -0700867 if (list->length() != 3) {
Kenneth Graunke350bd702010-05-26 13:03:14 -0700868 ir_read_error(st, list, "expected (array_ref <rvalue> <index>)");
Kenneth Graunke451381c2010-04-12 14:27:39 -0700869 return NULL;
870 }
871
872 s_expression *subj_expr = (s_expression*) list->subexpressions.head->next;
Kenneth Graunke350bd702010-05-26 13:03:14 -0700873 ir_rvalue *subject = read_rvalue(st, subj_expr);
874 if (subject == NULL) {
875 ir_read_error(st, NULL, "when reading the subject of an array_ref");
Kenneth Graunke451381c2010-04-12 14:27:39 -0700876 return NULL;
Kenneth Graunke350bd702010-05-26 13:03:14 -0700877 }
Kenneth Graunke451381c2010-04-12 14:27:39 -0700878
879 s_expression *idx_expr = (s_expression*) subj_expr->next;
880 ir_rvalue *idx = read_rvalue(st, idx_expr);
Carl Worth1660a292010-06-23 18:11:51 -0700881 return new(ctx) ir_dereference_array(subject, idx);
Kenneth Graunke451381c2010-04-12 14:27:39 -0700882}
883
884static ir_dereference *
885read_record_ref(_mesa_glsl_parse_state *st, s_list *list)
886{
Carl Worth1660a292010-06-23 18:11:51 -0700887 void *ctx = talloc_parent(st);
Kenneth Graunke13e1b6b2010-05-26 15:20:59 -0700888 if (list->length() != 3) {
889 ir_read_error(st, list, "expected (record_ref <rvalue> <field>)");
890 return NULL;
891 }
892
893 s_expression *subj_expr = (s_expression*) list->subexpressions.head->next;
894 ir_rvalue *subject = read_rvalue(st, subj_expr);
895 if (subject == NULL) {
896 ir_read_error(st, NULL, "when reading the subject of a record_ref");
897 return NULL;
898 }
899
900 s_symbol *field = SX_AS_SYMBOL(subj_expr->next);
901 if (field == NULL) {
902 ir_read_error(st, list, "expected (record_ref ... <field name>)");
903 return NULL;
904 }
Carl Worth1660a292010-06-23 18:11:51 -0700905 return new(ctx) ir_dereference_record(subject, field->value());
Kenneth Graunke451381c2010-04-12 14:27:39 -0700906}
Kenneth Graunkedd5b4a52010-05-26 17:55:10 -0700907
908static bool
909valid_texture_list_length(ir_texture_opcode op, s_list *list)
910{
911 unsigned required_length = 7;
912 if (op == ir_txf)
913 required_length = 5;
914 else if (op == ir_tex)
915 required_length = 6;
916
917 return list->length() == required_length;
918}
919
920static ir_texture *
921read_texture(_mesa_glsl_parse_state *st, s_list *list)
922{
Carl Worth1660a292010-06-23 18:11:51 -0700923 void *ctx = talloc_parent(st);
Kenneth Graunkedd5b4a52010-05-26 17:55:10 -0700924 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.head);
925 assert(tag != NULL);
926
927 ir_texture_opcode op = ir_texture::get_opcode(tag->value());
928 if (op == (ir_texture_opcode) -1)
929 return NULL;
930
931 if (!valid_texture_list_length(op, list)) {
932 ir_read_error(st, NULL, "invalid list size in (%s ...)", tag->value());
933 return NULL;
934 }
935
Carl Worth1660a292010-06-23 18:11:51 -0700936 ir_texture *tex = new(ctx) ir_texture(op);
Kenneth Graunkedd5b4a52010-05-26 17:55:10 -0700937
938 // Read sampler (must be a deref)
939 s_expression *sampler_expr = (s_expression *) tag->next;
Kenneth Graunke56d3f6a2010-06-03 15:07:34 -0700940 ir_dereference *sampler = read_dereference(st, sampler_expr);
941 if (sampler == NULL) {
Kenneth Graunkedd5b4a52010-05-26 17:55:10 -0700942 ir_read_error(st, NULL, "when reading sampler in (%s ...)", tag->value());
943 return NULL;
944 }
Kenneth Graunke56d3f6a2010-06-03 15:07:34 -0700945 tex->set_sampler(sampler);
Kenneth Graunkedd5b4a52010-05-26 17:55:10 -0700946
947 // Read coordinate (any rvalue)
948 s_expression *coordinate_expr = (s_expression *) sampler_expr->next;
949 tex->coordinate = read_rvalue(st, coordinate_expr);
950 if (tex->coordinate == NULL) {
951 ir_read_error(st, NULL, "when reading coordinate in (%s ...)",
952 tag->value());
953 return NULL;
954 }
955
956 // Read texel offset, i.e. (0 0 0)
957 s_list *offset_list = SX_AS_LIST(coordinate_expr->next);
958 if (offset_list == NULL || offset_list->length() != 3) {
959 ir_read_error(st, offset_list, "expected (<int> <int> <int>)");
960 return NULL;
961 }
962 s_int *offset_x = SX_AS_INT(offset_list->subexpressions.head);
963 s_int *offset_y = SX_AS_INT(offset_x->next);
964 s_int *offset_z = SX_AS_INT(offset_y->next);
965 if (offset_x == NULL || offset_y == NULL || offset_z == NULL) {
966 ir_read_error(st, offset_list, "expected (<int> <int> <int>)");
967 return NULL;
968 }
969 tex->offsets[0] = offset_x->value();
970 tex->offsets[1] = offset_y->value();
971 tex->offsets[2] = offset_z->value();
972
973 if (op == ir_txf) {
974 s_expression *lod_expr = (s_expression *) offset_list->next;
975 tex->lod_info.lod = read_rvalue(st, lod_expr);
976 if (tex->lod_info.lod == NULL) {
977 ir_read_error(st, NULL, "when reading LOD in (txf ...)");
978 return NULL;
979 }
980 } else {
981 s_expression *proj_expr = (s_expression *) offset_list->next;
982 s_int *proj_as_int = SX_AS_INT(proj_expr);
983 if (proj_as_int && proj_as_int->value() == 1) {
984 tex->projector = NULL;
985 } else {
986 tex->projector = read_rvalue(st, proj_expr);
987 if (tex->projector == NULL) {
988 ir_read_error(st, NULL, "when reading projective divide in (%s ..)",
989 tag->value());
990 return NULL;
991 }
992 }
993
994 s_list *shadow_list = SX_AS_LIST(proj_expr->next);
995 if (shadow_list == NULL) {
996 ir_read_error(st, NULL, "shadow comparitor must be a list");
997 return NULL;
998 }
999 if (shadow_list->subexpressions.is_empty()) {
1000 tex->shadow_comparitor= NULL;
1001 } else {
1002 tex->shadow_comparitor = read_rvalue(st, shadow_list);
1003 if (tex->shadow_comparitor == NULL) {
1004 ir_read_error(st, NULL, "when reading shadow comparitor in (%s ..)",
1005 tag->value());
1006 return NULL;
1007 }
1008 }
1009 s_expression *lod_expr = (s_expression *) shadow_list->next;
1010
1011 switch (op) {
1012 case ir_txb:
1013 tex->lod_info.bias = read_rvalue(st, lod_expr);
1014 if (tex->lod_info.bias == NULL) {
1015 ir_read_error(st, NULL, "when reading LOD bias in (txb ...)");
1016 return NULL;
1017 }
1018 break;
1019 case ir_txl:
1020 tex->lod_info.lod = read_rvalue(st, lod_expr);
1021 if (tex->lod_info.lod == NULL) {
1022 ir_read_error(st, NULL, "when reading LOD in (txl ...)");
1023 return NULL;
1024 }
1025 break;
1026 case ir_txd: {
1027 s_list *lod_list = SX_AS_LIST(lod_expr);
1028 if (lod_list->length() != 2) {
1029 ir_read_error(st, lod_expr, "expected (dPdx dPdy) in (txd ...)");
1030 return NULL;
1031 }
1032 s_expression *dx_expr = (s_expression *) lod_list->subexpressions.head;
1033 s_expression *dy_expr = (s_expression *) dx_expr->next;
1034
1035 tex->lod_info.grad.dPdx = read_rvalue(st, dx_expr);
1036 if (tex->lod_info.grad.dPdx == NULL) {
1037 ir_read_error(st, NULL, "when reading dPdx in (txd ...)");
1038 return NULL;
1039 }
1040 tex->lod_info.grad.dPdy = read_rvalue(st, dy_expr);
1041 if (tex->lod_info.grad.dPdy == NULL) {
1042 ir_read_error(st, NULL, "when reading dPdy in (txd ...)");
1043 return NULL;
1044 }
1045 break;
1046 }
1047 default:
1048 // tex doesn't have any extra parameters and txf was handled earlier.
1049 break;
1050 };
1051 }
1052 return tex;
1053}