glsl2: glsl_type has its own talloc context, don't pass one in
[mesa.git] / src / glsl / ir_reader.cpp
1 /*
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 #include <cstdarg>
25
26 extern "C" {
27 #include <talloc.h>
28 }
29
30 #include "ir_reader.h"
31 #include "glsl_parser_extras.h"
32 #include "glsl_types.h"
33 #include "s_expression.h"
34
35 static void ir_read_error(_mesa_glsl_parse_state *, s_expression *,
36 const char *fmt, ...);
37 static const glsl_type *read_type(_mesa_glsl_parse_state *, s_expression *);
38
39 static void scan_for_prototypes(_mesa_glsl_parse_state *, exec_list *,
40 s_expression *);
41 static ir_function *read_function(_mesa_glsl_parse_state *, s_list *,
42 bool skip_body);
43 static void read_function_sig(_mesa_glsl_parse_state *, ir_function *,
44 s_list *, bool skip_body);
45
46 static void read_instructions(_mesa_glsl_parse_state *, exec_list *,
47 s_expression *, ir_loop *);
48 static ir_instruction *read_instruction(_mesa_glsl_parse_state *,
49 s_expression *, ir_loop *);
50 static ir_variable *read_declaration(_mesa_glsl_parse_state *, s_list *);
51 static ir_if *read_if(_mesa_glsl_parse_state *, s_list *, ir_loop *);
52 static ir_loop *read_loop(_mesa_glsl_parse_state *st, s_list *list);
53 static ir_return *read_return(_mesa_glsl_parse_state *, s_list *);
54
55 static ir_rvalue *read_rvalue(_mesa_glsl_parse_state *, s_expression *);
56 static ir_assignment *read_assignment(_mesa_glsl_parse_state *, s_list *);
57 static ir_expression *read_expression(_mesa_glsl_parse_state *, s_list *);
58 static ir_call *read_call(_mesa_glsl_parse_state *, s_list *);
59 static ir_swizzle *read_swizzle(_mesa_glsl_parse_state *, s_list *);
60 static ir_constant *read_constant(_mesa_glsl_parse_state *, s_list *);
61 static ir_texture *read_texture(_mesa_glsl_parse_state *, s_list *);
62
63 static ir_dereference *read_dereference(_mesa_glsl_parse_state *,
64 s_expression *);
65 static ir_dereference *read_var_ref(_mesa_glsl_parse_state *, s_list *);
66 static ir_dereference *read_array_ref(_mesa_glsl_parse_state *, s_list *);
67 static ir_dereference *read_record_ref(_mesa_glsl_parse_state *, s_list *);
68
69 void
70 _mesa_glsl_read_ir(_mesa_glsl_parse_state *state, exec_list *instructions,
71 const char *src)
72 {
73 s_expression *expr = s_expression::read_expression(state, src);
74 if (expr == NULL) {
75 ir_read_error(state, NULL, "couldn't parse S-Expression.");
76 return;
77 }
78
79 scan_for_prototypes(state, instructions, expr);
80 if (state->error)
81 return;
82
83 read_instructions(state, instructions, expr, NULL);
84 talloc_free(expr);
85 }
86
87 static void
88 ir_read_error(_mesa_glsl_parse_state *state, s_expression *expr,
89 const char *fmt, ...)
90 {
91 va_list ap;
92
93 state->error = true;
94
95 state->info_log = talloc_strdup_append(state->info_log, "error: ");
96
97 va_start(ap, fmt);
98 state->info_log = talloc_vasprintf_append(state->info_log, fmt, ap);
99 va_end(ap);
100 state->info_log = talloc_strdup_append(state->info_log, "\n");
101
102 if (expr != NULL) {
103 state->info_log = talloc_strdup_append(state->info_log,
104 "...in this context:\n ");
105 expr->print();
106 state->info_log = talloc_strdup_append(state->info_log, "\n\n");
107 }
108 }
109
110 static const glsl_type *
111 read_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) {
117 ir_read_error(st, expr, "expected type (array ...) or (struct ...)");
118 return NULL;
119 }
120 if (strcmp(type_sym->value(), "array") == 0) {
121 if (list->length() != 3) {
122 ir_read_error(st, expr, "expected type (array <type> <int>)");
123 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) {
130 ir_read_error(st, NULL, "when reading base type of array");
131 return NULL;
132 }
133
134 // Read array size
135 s_int *size = SX_AS_INT(base_expr->next);
136 if (size == NULL) {
137 ir_read_error(st, expr, "found non-integer array size");
138 return NULL;
139 }
140
141 return glsl_type::get_array_instance(base_type, size->value());
142 } else if (strcmp(type_sym->value(), "struct") == 0) {
143 assert(false); // FINISHME
144 } else {
145 ir_read_error(st, expr, "expected (array ...) or (struct ...); "
146 "found (%s ...)", type_sym->value());
147 return NULL;
148 }
149 }
150
151 s_symbol *type_sym = SX_AS_SYMBOL(expr);
152 if (type_sym == NULL) {
153 ir_read_error(st, expr, "expected <type> (symbol or list)");
154 return NULL;
155 }
156
157 const glsl_type *type = st->symbols->get_type(type_sym->value());
158 if (type == NULL)
159 ir_read_error(st, expr, "invalid type: %s", type_sym->value());
160
161 return type;
162 }
163
164
165 static void
166 scan_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
184 ir_function *f = read_function(st, sub, true);
185 if (f == NULL)
186 return;
187 instructions->push_tail(f);
188 }
189 }
190
191 static ir_function *
192 read_function(_mesa_glsl_parse_state *st, s_list *list, bool skip_body)
193 {
194 void *ctx = st;
195 bool added = false;
196 if (list->length() < 3) {
197 ir_read_error(st, list, "Expected (function <name> (signature ...) ...)");
198 return NULL;
199 }
200
201 s_symbol *name = SX_AS_SYMBOL(list->subexpressions.head->next);
202 if (name == NULL) {
203 ir_read_error(st, list, "Expected (function <name> ...)");
204 return NULL;
205 }
206
207 ir_function *f = st->symbols->get_function(name->value());
208 if (f == NULL) {
209 f = new(ctx) ir_function(name->value());
210 added = st->symbols->add_function(f->name, f);
211 assert(added);
212 }
213
214 exec_list_iterator it = list->subexpressions.iterator();
215 it.next(); // skip "function" tag
216 it.next(); // skip function name
217 for (/* nothing */; it.has_next(); it.next()) {
218 s_list *siglist = SX_AS_LIST(it.get());
219 if (siglist == NULL) {
220 ir_read_error(st, list, "Expected (function (signature ...) ...)");
221 return NULL;
222 }
223
224 s_symbol *tag = SX_AS_SYMBOL(siglist->subexpressions.get_head());
225 if (tag == NULL || strcmp(tag->value(), "signature") != 0) {
226 ir_read_error(st, siglist, "Expected (signature ...)");
227 return NULL;
228 }
229
230 read_function_sig(st, f, siglist, skip_body);
231 }
232 return added ? f : NULL;
233 }
234
235 static void
236 read_function_sig(_mesa_glsl_parse_state *st, ir_function *f, s_list *list,
237 bool skip_body)
238 {
239 void *ctx = st;
240 if (list->length() != 4) {
241 ir_read_error(st, list, "Expected (signature <type> (parameters ...) "
242 "(<instruction> ...))");
243 return;
244 }
245
246 s_expression *type_expr = (s_expression*) list->subexpressions.head->next;
247 const glsl_type *return_type = read_type(st, type_expr);
248 if (return_type == NULL)
249 return;
250
251 s_list *paramlist = SX_AS_LIST(type_expr->next);
252 s_list *body_list = SX_AS_LIST(paramlist->next);
253 if (paramlist == NULL || body_list == NULL) {
254 ir_read_error(st, list, "Expected (signature <type> (parameters ...) "
255 "(<instruction> ...))");
256 return;
257 }
258 s_symbol *paramtag = SX_AS_SYMBOL(paramlist->subexpressions.get_head());
259 if (paramtag == NULL || strcmp(paramtag->value(), "parameters") != 0) {
260 ir_read_error(st, paramlist, "Expected (parameters ...)");
261 return;
262 }
263
264 // Read the parameters list into a temporary place.
265 exec_list hir_parameters;
266 st->symbols->push_scope();
267
268 exec_list_iterator it = paramlist->subexpressions.iterator();
269 for (it.next() /* skip "parameters" */; it.has_next(); it.next()) {
270 s_list *decl = SX_AS_LIST(it.get());
271 ir_variable *var = read_declaration(st, decl);
272 if (var == NULL)
273 return;
274
275 hir_parameters.push_tail(var);
276 }
277
278 ir_function_signature *sig = f->exact_matching_signature(&hir_parameters);
279 if (sig != NULL) {
280 const char *badvar = sig->qualifiers_match(&hir_parameters);
281 if (badvar != NULL) {
282 ir_read_error(st, list, "function `%s' parameter `%s' qualifiers "
283 "don't match prototype", f->name, badvar);
284 return;
285 }
286
287 if (sig->return_type != return_type) {
288 ir_read_error(st, list, "function `%s' return type doesn't "
289 "match prototype", f->name);
290 return;
291 }
292 } else {
293 sig = new(ctx) ir_function_signature(return_type);
294 sig->is_built_in = true;
295 f->add_signature(sig);
296 }
297
298 sig->replace_parameters(&hir_parameters);
299
300 if (!skip_body) {
301 if (sig->is_defined) {
302 ir_read_error(st, list, "function %s redefined", f->name);
303 return;
304 }
305 read_instructions(st, &sig->body, body_list, NULL);
306 sig->is_defined = true;
307 }
308
309 st->symbols->pop_scope();
310 }
311
312 static void
313 read_instructions(_mesa_glsl_parse_state *st, exec_list *instructions,
314 s_expression *expr, ir_loop *loop_ctx)
315 {
316 // Read in a list of instructions
317 s_list *list = SX_AS_LIST(expr);
318 if (list == NULL) {
319 ir_read_error(st, expr, "Expected (<instruction> ...); found an atom.");
320 return;
321 }
322
323 foreach_iter(exec_list_iterator, it, list->subexpressions) {
324 s_expression *sub = (s_expression*) it.get();
325 ir_instruction *ir = read_instruction(st, sub, loop_ctx);
326 if (ir != NULL)
327 instructions->push_tail(ir);
328 }
329 }
330
331
332 static ir_instruction *
333 read_instruction(_mesa_glsl_parse_state *st, s_expression *expr,
334 ir_loop *loop_ctx)
335 {
336 void *ctx = st;
337 s_symbol *symbol = SX_AS_SYMBOL(expr);
338 if (symbol != NULL) {
339 if (strcmp(symbol->value(), "break") == 0 && loop_ctx != NULL)
340 return new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
341 if (strcmp(symbol->value(), "continue") == 0 && loop_ctx != NULL)
342 return new(ctx) ir_loop_jump(ir_loop_jump::jump_continue);
343 }
344
345 s_list *list = SX_AS_LIST(expr);
346 if (list == NULL || list->subexpressions.is_empty()) {
347 ir_read_error(st, expr, "Invalid instruction.\n");
348 return NULL;
349 }
350
351 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
352 if (tag == NULL) {
353 ir_read_error(st, expr, "expected instruction tag");
354 return NULL;
355 }
356
357 ir_instruction *inst = NULL;
358 if (strcmp(tag->value(), "declare") == 0) {
359 inst = read_declaration(st, list);
360 } else if (strcmp(tag->value(), "if") == 0) {
361 inst = read_if(st, list, loop_ctx);
362 } else if (strcmp(tag->value(), "loop") == 0) {
363 inst = read_loop(st, list);
364 } else if (strcmp(tag->value(), "return") == 0) {
365 inst = read_return(st, list);
366 } else if (strcmp(tag->value(), "function") == 0) {
367 inst = read_function(st, list, false);
368 } else {
369 inst = read_rvalue(st, list);
370 if (inst == NULL)
371 ir_read_error(st, NULL, "when reading instruction");
372 }
373 return inst;
374 }
375
376
377 static ir_variable *
378 read_declaration(_mesa_glsl_parse_state *st, s_list *list)
379 {
380 void *ctx = st;
381 if (list->length() != 4) {
382 ir_read_error(st, list, "expected (declare (<qualifiers>) <type> "
383 "<name>)");
384 return NULL;
385 }
386
387 s_list *quals = SX_AS_LIST(list->subexpressions.head->next);
388 if (quals == NULL) {
389 ir_read_error(st, list, "expected a list of variable qualifiers");
390 return NULL;
391 }
392
393 s_expression *type_expr = (s_expression*) quals->next;
394 const glsl_type *type = read_type(st, type_expr);
395 if (type == NULL)
396 return NULL;
397
398 s_symbol *var_name = SX_AS_SYMBOL(type_expr->next);
399 if (var_name == NULL) {
400 ir_read_error(st, list, "expected variable name, found non-symbol");
401 return NULL;
402 }
403
404 ir_variable *var = new(ctx) ir_variable(type, var_name->value(),
405 ir_var_auto);
406
407 foreach_iter(exec_list_iterator, it, quals->subexpressions) {
408 s_symbol *qualifier = SX_AS_SYMBOL(it.get());
409 if (qualifier == NULL) {
410 ir_read_error(st, list, "qualifier list must contain only symbols");
411 delete var;
412 return NULL;
413 }
414
415 // FINISHME: Check for duplicate/conflicting qualifiers.
416 if (strcmp(qualifier->value(), "centroid") == 0) {
417 var->centroid = 1;
418 } else if (strcmp(qualifier->value(), "invariant") == 0) {
419 var->invariant = 1;
420 } else if (strcmp(qualifier->value(), "uniform") == 0) {
421 var->mode = ir_var_uniform;
422 } else if (strcmp(qualifier->value(), "auto") == 0) {
423 var->mode = ir_var_auto;
424 } else if (strcmp(qualifier->value(), "in") == 0) {
425 var->mode = ir_var_in;
426 } else if (strcmp(qualifier->value(), "out") == 0) {
427 var->mode = ir_var_out;
428 } else if (strcmp(qualifier->value(), "inout") == 0) {
429 var->mode = ir_var_inout;
430 } else if (strcmp(qualifier->value(), "smooth") == 0) {
431 var->interpolation = ir_var_smooth;
432 } else if (strcmp(qualifier->value(), "flat") == 0) {
433 var->interpolation = ir_var_flat;
434 } else if (strcmp(qualifier->value(), "noperspective") == 0) {
435 var->interpolation = ir_var_noperspective;
436 } else {
437 ir_read_error(st, list, "unknown qualifier: %s", qualifier->value());
438 delete var;
439 return NULL;
440 }
441 }
442
443 // Add the variable to the symbol table
444 st->symbols->add_variable(var->name, var);
445
446 return var;
447 }
448
449
450 static ir_if *
451 read_if(_mesa_glsl_parse_state *st, s_list *list, ir_loop *loop_ctx)
452 {
453 void *ctx = st;
454 if (list->length() != 4) {
455 ir_read_error(st, list, "expected (if <condition> (<then> ...) "
456 "(<else> ...))");
457 return NULL;
458 }
459
460 s_expression *cond_expr = (s_expression*) list->subexpressions.head->next;
461 ir_rvalue *condition = read_rvalue(st, cond_expr);
462 if (condition == NULL) {
463 ir_read_error(st, NULL, "when reading condition of (if ...)");
464 return NULL;
465 }
466
467 s_expression *then_expr = (s_expression*) cond_expr->next;
468 s_expression *else_expr = (s_expression*) then_expr->next;
469
470 ir_if *iff = new(ctx) ir_if(condition);
471
472 read_instructions(st, &iff->then_instructions, then_expr, loop_ctx);
473 read_instructions(st, &iff->else_instructions, else_expr, loop_ctx);
474 if (st->error) {
475 delete iff;
476 iff = NULL;
477 }
478 return iff;
479 }
480
481
482 static ir_loop *
483 read_loop(_mesa_glsl_parse_state *st, s_list *list)
484 {
485 void *ctx = st;
486 if (list->length() != 6) {
487 ir_read_error(st, list, "expected (loop <counter> <from> <to> "
488 "<increment> <body>)");
489 return NULL;
490 }
491
492 s_expression *count_expr = (s_expression*) list->subexpressions.head->next;
493 s_expression *from_expr = (s_expression*) count_expr->next;
494 s_expression *to_expr = (s_expression*) from_expr->next;
495 s_expression *inc_expr = (s_expression*) to_expr->next;
496 s_expression *body_expr = (s_expression*) inc_expr->next;
497
498 // FINISHME: actually read the count/from/to fields.
499
500 ir_loop *loop = new(ctx) ir_loop;
501 read_instructions(st, &loop->body_instructions, body_expr, loop);
502 if (st->error) {
503 delete loop;
504 loop = NULL;
505 }
506 return loop;
507 }
508
509
510 static ir_return *
511 read_return(_mesa_glsl_parse_state *st, s_list *list)
512 {
513 void *ctx = st;
514 if (list->length() != 2) {
515 ir_read_error(st, list, "expected (return <rvalue>)");
516 return NULL;
517 }
518
519 s_expression *expr = (s_expression*) list->subexpressions.head->next;
520
521 ir_rvalue *retval = read_rvalue(st, expr);
522 if (retval == NULL) {
523 ir_read_error(st, NULL, "when reading return value");
524 return NULL;
525 }
526
527 return new(ctx) ir_return(retval);
528 }
529
530
531 static ir_rvalue *
532 read_rvalue(_mesa_glsl_parse_state *st, s_expression *expr)
533 {
534 s_list *list = SX_AS_LIST(expr);
535 if (list == NULL || list->subexpressions.is_empty())
536 return NULL;
537
538 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
539 if (tag == NULL) {
540 ir_read_error(st, expr, "expected rvalue tag");
541 return NULL;
542 }
543
544 ir_rvalue *rvalue = read_dereference(st, list);
545 if (rvalue != NULL || st->error)
546 return rvalue;
547 else if (strcmp(tag->value(), "swiz") == 0) {
548 rvalue = read_swizzle(st, list);
549 } else if (strcmp(tag->value(), "assign") == 0) {
550 rvalue = read_assignment(st, list);
551 } else if (strcmp(tag->value(), "expression") == 0) {
552 rvalue = read_expression(st, list);
553 } else if (strcmp(tag->value(), "call") == 0) {
554 rvalue = read_call(st, list);
555 } else if (strcmp(tag->value(), "constant") == 0) {
556 rvalue = read_constant(st, list);
557 } else {
558 rvalue = read_texture(st, list);
559 if (rvalue == NULL && !st->error)
560 ir_read_error(st, expr, "unrecognized rvalue tag: %s", tag->value());
561 }
562
563 return rvalue;
564 }
565
566 static ir_assignment *
567 read_assignment(_mesa_glsl_parse_state *st, s_list *list)
568 {
569 void *ctx = st;
570 if (list->length() != 4) {
571 ir_read_error(st, list, "expected (assign <condition> <lhs> <rhs>)");
572 return NULL;
573 }
574
575 s_expression *cond_expr = (s_expression*) list->subexpressions.head->next;
576 s_expression *lhs_expr = (s_expression*) cond_expr->next;
577 s_expression *rhs_expr = (s_expression*) lhs_expr->next;
578
579 // FINISHME: Deal with "true" condition
580 ir_rvalue *condition = read_rvalue(st, cond_expr);
581 if (condition == NULL) {
582 ir_read_error(st, NULL, "when reading condition of assignment");
583 return NULL;
584 }
585
586 ir_rvalue *lhs = read_rvalue(st, lhs_expr);
587 if (lhs == NULL) {
588 ir_read_error(st, NULL, "when reading left-hand side of assignment");
589 return NULL;
590 }
591
592 ir_rvalue *rhs = read_rvalue(st, rhs_expr);
593 if (rhs == NULL) {
594 ir_read_error(st, NULL, "when reading right-hand side of assignment");
595 return NULL;
596 }
597
598 return new(ctx) ir_assignment(lhs, rhs, condition);
599 }
600
601 static ir_call *
602 read_call(_mesa_glsl_parse_state *st, s_list *list)
603 {
604 void *ctx = st;
605 if (list->length() != 3) {
606 ir_read_error(st, list, "expected (call <name> (<param> ...))");
607 return NULL;
608 }
609
610 s_symbol *name = SX_AS_SYMBOL(list->subexpressions.head->next);
611 s_list *params = SX_AS_LIST(name->next);
612 if (name == NULL || params == NULL) {
613 ir_read_error(st, list, "expected (call <name> (<param> ...))");
614 return NULL;
615 }
616
617 exec_list parameters;
618
619 foreach_iter(exec_list_iterator, it, params->subexpressions) {
620 s_expression *expr = (s_expression*) it.get();
621 ir_rvalue *param = read_rvalue(st, expr);
622 if (param == NULL) {
623 ir_read_error(st, list, "when reading parameter to function call");
624 return NULL;
625 }
626 parameters.push_tail(param);
627 }
628
629 ir_function *f = st->symbols->get_function(name->value());
630 if (f == NULL) {
631 ir_read_error(st, list, "found call to undefined function %s",
632 name->value());
633 return NULL;
634 }
635
636 ir_function_signature *callee = f->matching_signature(&parameters);
637 if (callee == NULL) {
638 ir_read_error(st, list, "couldn't find matching signature for function "
639 "%s", name->value());
640 return NULL;
641 }
642
643 return new(ctx) ir_call(callee, &parameters);
644 }
645
646 static ir_expression *
647 read_expression(_mesa_glsl_parse_state *st, s_list *list)
648 {
649 void *ctx = st;
650 const unsigned list_length = list->length();
651 if (list_length < 4) {
652 ir_read_error(st, list, "expected (expression <type> <operator> "
653 "<operand> [<operand>])");
654 return NULL;
655 }
656
657 s_expression *type_expr = (s_expression*) list->subexpressions.head->next;
658 const glsl_type *type = read_type(st, type_expr);
659 if (type == NULL)
660 return NULL;
661
662 /* Read the operator */
663 s_symbol *op_sym = SX_AS_SYMBOL(type_expr->next);
664 if (op_sym == NULL) {
665 ir_read_error(st, list, "expected operator, found non-symbol");
666 return NULL;
667 }
668
669 ir_expression_operation op = ir_expression::get_operator(op_sym->value());
670 if (op == (ir_expression_operation) -1) {
671 ir_read_error(st, list, "invalid operator: %s", op_sym->value());
672 return NULL;
673 }
674
675 /* Now that we know the operator, check for the right number of operands */
676 if (ir_expression::get_num_operands(op) == 2) {
677 if (list_length != 5) {
678 ir_read_error(st, list, "expected (expression <type> %s <operand> "
679 " <operand>)", op_sym->value());
680 return NULL;
681 }
682 } else {
683 if (list_length != 4) {
684 ir_read_error(st, list, "expected (expression <type> %s <operand>)",
685 op_sym->value());
686 return NULL;
687 }
688 }
689
690 s_expression *exp1 = (s_expression*) (op_sym->next);
691 ir_rvalue *arg1 = read_rvalue(st, exp1);
692 if (arg1 == NULL) {
693 ir_read_error(st, NULL, "when reading first operand of %s",
694 op_sym->value());
695 return NULL;
696 }
697
698 ir_rvalue *arg2 = NULL;
699 if (ir_expression::get_num_operands(op) == 2) {
700 s_expression *exp2 = (s_expression*) (exp1->next);
701 arg2 = read_rvalue(st, exp2);
702 if (arg2 == NULL) {
703 ir_read_error(st, NULL, "when reading second operand of %s",
704 op_sym->value());
705 return NULL;
706 }
707 }
708
709 return new(ctx) ir_expression(op, type, arg1, arg2);
710 }
711
712 static ir_swizzle *
713 read_swizzle(_mesa_glsl_parse_state *st, s_list *list)
714 {
715 if (list->length() != 3) {
716 ir_read_error(st, list, "expected (swiz <swizzle> <rvalue>)");
717 return NULL;
718 }
719
720 s_symbol *swiz = SX_AS_SYMBOL(list->subexpressions.head->next);
721 if (swiz == NULL) {
722 ir_read_error(st, list, "expected a valid swizzle; found non-symbol");
723 return NULL;
724 }
725
726 if (strlen(swiz->value()) > 4) {
727 ir_read_error(st, list, "expected a valid swizzle; found %s",
728 swiz->value());
729 return NULL;
730 }
731
732 s_expression *sub = (s_expression*) swiz->next;
733 if (sub == NULL) {
734 ir_read_error(st, list, "expected rvalue: (swizzle %s <rvalue>)",
735 swiz->value());
736 return NULL;
737 }
738
739 ir_rvalue *rvalue = read_rvalue(st, sub);
740 if (rvalue == NULL)
741 return NULL;
742
743 ir_swizzle *ir = ir_swizzle::create(rvalue, swiz->value(),
744 rvalue->type->vector_elements);
745 if (ir == NULL)
746 ir_read_error(st, list, "invalid swizzle");
747
748 return ir;
749 }
750
751 static ir_constant *
752 read_constant(_mesa_glsl_parse_state *st, s_list *list)
753 {
754 void *ctx = st;
755 if (list->length() != 3) {
756 ir_read_error(st, list, "expected (constant <type> (<num> ... <num>))");
757 return NULL;
758 }
759
760 s_expression *type_expr = (s_expression*) list->subexpressions.head->next;
761 const glsl_type *type = read_type(st, type_expr);
762 if (type == NULL)
763 return NULL;
764
765 s_list *values = SX_AS_LIST(type_expr->next);
766 if (values == NULL) {
767 ir_read_error(st, list, "expected (constant <type> (<num> ... <num>))");
768 return NULL;
769 }
770
771 const glsl_type *const base_type = type->get_base_type();
772
773 ir_constant_data data;
774
775 // Read in list of values (at most 16).
776 int k = 0;
777 foreach_iter(exec_list_iterator, it, values->subexpressions) {
778 if (k >= 16) {
779 ir_read_error(st, values, "expected at most 16 numbers");
780 return NULL;
781 }
782
783 s_expression *expr = (s_expression*) it.get();
784
785 if (base_type->base_type == GLSL_TYPE_FLOAT) {
786 s_number *value = SX_AS_NUMBER(expr);
787 if (value == NULL) {
788 ir_read_error(st, values, "expected numbers");
789 return NULL;
790 }
791 data.f[k] = value->fvalue();
792 } else {
793 s_int *value = SX_AS_INT(expr);
794 if (value == NULL) {
795 ir_read_error(st, values, "expected integers");
796 return NULL;
797 }
798
799 switch (base_type->base_type) {
800 case GLSL_TYPE_UINT: {
801 data.u[k] = value->value();
802 break;
803 }
804 case GLSL_TYPE_INT: {
805 data.i[k] = value->value();
806 break;
807 }
808 case GLSL_TYPE_BOOL: {
809 data.b[k] = value->value();
810 break;
811 }
812 default:
813 ir_read_error(st, values, "unsupported constant type");
814 return NULL;
815 }
816 }
817 ++k;
818 }
819
820 return new(ctx) ir_constant(type, &data);
821 }
822
823 static ir_dereference *
824 read_dereference(_mesa_glsl_parse_state *st, s_expression *expr)
825 {
826 s_list *list = SX_AS_LIST(expr);
827 if (list == NULL || list->subexpressions.is_empty())
828 return NULL;
829
830 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.head);
831 assert(tag != NULL);
832
833 if (strcmp(tag->value(), "var_ref") == 0)
834 return read_var_ref(st, list);
835 if (strcmp(tag->value(), "array_ref") == 0)
836 return read_array_ref(st, list);
837 if (strcmp(tag->value(), "record_ref") == 0)
838 return read_record_ref(st, list);
839 return NULL;
840 }
841
842 static ir_dereference *
843 read_var_ref(_mesa_glsl_parse_state *st, s_list *list)
844 {
845 void *ctx = st;
846 if (list->length() != 2) {
847 ir_read_error(st, list, "expected (var_ref <variable name>)");
848 return NULL;
849 }
850 s_symbol *var_name = SX_AS_SYMBOL(list->subexpressions.head->next);
851 if (var_name == NULL) {
852 ir_read_error(st, list, "expected (var_ref <variable name>)");
853 return NULL;
854 }
855
856 ir_variable *var = st->symbols->get_variable(var_name->value());
857 if (var == NULL) {
858 ir_read_error(st, list, "undeclared variable: %s", var_name->value());
859 return NULL;
860 }
861
862 return new(ctx) ir_dereference_variable(var);
863 }
864
865 static ir_dereference *
866 read_array_ref(_mesa_glsl_parse_state *st, s_list *list)
867 {
868 void *ctx = st;
869 if (list->length() != 3) {
870 ir_read_error(st, list, "expected (array_ref <rvalue> <index>)");
871 return NULL;
872 }
873
874 s_expression *subj_expr = (s_expression*) list->subexpressions.head->next;
875 ir_rvalue *subject = read_rvalue(st, subj_expr);
876 if (subject == NULL) {
877 ir_read_error(st, NULL, "when reading the subject of an array_ref");
878 return NULL;
879 }
880
881 s_expression *idx_expr = (s_expression*) subj_expr->next;
882 ir_rvalue *idx = read_rvalue(st, idx_expr);
883 return new(ctx) ir_dereference_array(subject, idx);
884 }
885
886 static ir_dereference *
887 read_record_ref(_mesa_glsl_parse_state *st, s_list *list)
888 {
889 void *ctx = st;
890 if (list->length() != 3) {
891 ir_read_error(st, list, "expected (record_ref <rvalue> <field>)");
892 return NULL;
893 }
894
895 s_expression *subj_expr = (s_expression*) list->subexpressions.head->next;
896 ir_rvalue *subject = read_rvalue(st, subj_expr);
897 if (subject == NULL) {
898 ir_read_error(st, NULL, "when reading the subject of a record_ref");
899 return NULL;
900 }
901
902 s_symbol *field = SX_AS_SYMBOL(subj_expr->next);
903 if (field == NULL) {
904 ir_read_error(st, list, "expected (record_ref ... <field name>)");
905 return NULL;
906 }
907 return new(ctx) ir_dereference_record(subject, field->value());
908 }
909
910 static bool
911 valid_texture_list_length(ir_texture_opcode op, s_list *list)
912 {
913 unsigned required_length = 7;
914 if (op == ir_txf)
915 required_length = 5;
916 else if (op == ir_tex)
917 required_length = 6;
918
919 return list->length() == required_length;
920 }
921
922 static ir_texture *
923 read_texture(_mesa_glsl_parse_state *st, s_list *list)
924 {
925 void *ctx = st;
926 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.head);
927 assert(tag != NULL);
928
929 ir_texture_opcode op = ir_texture::get_opcode(tag->value());
930 if (op == (ir_texture_opcode) -1)
931 return NULL;
932
933 if (!valid_texture_list_length(op, list)) {
934 ir_read_error(st, NULL, "invalid list size in (%s ...)", tag->value());
935 return NULL;
936 }
937
938 ir_texture *tex = new(ctx) ir_texture(op);
939
940 // Read sampler (must be a deref)
941 s_expression *sampler_expr = (s_expression *) tag->next;
942 ir_dereference *sampler = read_dereference(st, sampler_expr);
943 if (sampler == NULL) {
944 ir_read_error(st, NULL, "when reading sampler in (%s ...)", tag->value());
945 return NULL;
946 }
947 tex->set_sampler(sampler);
948
949 // Read coordinate (any rvalue)
950 s_expression *coordinate_expr = (s_expression *) sampler_expr->next;
951 tex->coordinate = read_rvalue(st, coordinate_expr);
952 if (tex->coordinate == NULL) {
953 ir_read_error(st, NULL, "when reading coordinate in (%s ...)",
954 tag->value());
955 return NULL;
956 }
957
958 // Read texel offset, i.e. (0 0 0)
959 s_list *offset_list = SX_AS_LIST(coordinate_expr->next);
960 if (offset_list == NULL || offset_list->length() != 3) {
961 ir_read_error(st, offset_list, "expected (<int> <int> <int>)");
962 return NULL;
963 }
964 s_int *offset_x = SX_AS_INT(offset_list->subexpressions.head);
965 s_int *offset_y = SX_AS_INT(offset_x->next);
966 s_int *offset_z = SX_AS_INT(offset_y->next);
967 if (offset_x == NULL || offset_y == NULL || offset_z == NULL) {
968 ir_read_error(st, offset_list, "expected (<int> <int> <int>)");
969 return NULL;
970 }
971 tex->offsets[0] = offset_x->value();
972 tex->offsets[1] = offset_y->value();
973 tex->offsets[2] = offset_z->value();
974
975 if (op == ir_txf) {
976 s_expression *lod_expr = (s_expression *) offset_list->next;
977 tex->lod_info.lod = read_rvalue(st, lod_expr);
978 if (tex->lod_info.lod == NULL) {
979 ir_read_error(st, NULL, "when reading LOD in (txf ...)");
980 return NULL;
981 }
982 } else {
983 s_expression *proj_expr = (s_expression *) offset_list->next;
984 s_int *proj_as_int = SX_AS_INT(proj_expr);
985 if (proj_as_int && proj_as_int->value() == 1) {
986 tex->projector = NULL;
987 } else {
988 tex->projector = read_rvalue(st, proj_expr);
989 if (tex->projector == NULL) {
990 ir_read_error(st, NULL, "when reading projective divide in (%s ..)",
991 tag->value());
992 return NULL;
993 }
994 }
995
996 s_list *shadow_list = SX_AS_LIST(proj_expr->next);
997 if (shadow_list == NULL) {
998 ir_read_error(st, NULL, "shadow comparitor must be a list");
999 return NULL;
1000 }
1001 if (shadow_list->subexpressions.is_empty()) {
1002 tex->shadow_comparitor= NULL;
1003 } else {
1004 tex->shadow_comparitor = read_rvalue(st, shadow_list);
1005 if (tex->shadow_comparitor == NULL) {
1006 ir_read_error(st, NULL, "when reading shadow comparitor in (%s ..)",
1007 tag->value());
1008 return NULL;
1009 }
1010 }
1011 s_expression *lod_expr = (s_expression *) shadow_list->next;
1012
1013 switch (op) {
1014 case ir_txb:
1015 tex->lod_info.bias = read_rvalue(st, lod_expr);
1016 if (tex->lod_info.bias == NULL) {
1017 ir_read_error(st, NULL, "when reading LOD bias in (txb ...)");
1018 return NULL;
1019 }
1020 break;
1021 case ir_txl:
1022 tex->lod_info.lod = read_rvalue(st, lod_expr);
1023 if (tex->lod_info.lod == NULL) {
1024 ir_read_error(st, NULL, "when reading LOD in (txl ...)");
1025 return NULL;
1026 }
1027 break;
1028 case ir_txd: {
1029 s_list *lod_list = SX_AS_LIST(lod_expr);
1030 if (lod_list->length() != 2) {
1031 ir_read_error(st, lod_expr, "expected (dPdx dPdy) in (txd ...)");
1032 return NULL;
1033 }
1034 s_expression *dx_expr = (s_expression *) lod_list->subexpressions.head;
1035 s_expression *dy_expr = (s_expression *) dx_expr->next;
1036
1037 tex->lod_info.grad.dPdx = read_rvalue(st, dx_expr);
1038 if (tex->lod_info.grad.dPdx == NULL) {
1039 ir_read_error(st, NULL, "when reading dPdx in (txd ...)");
1040 return NULL;
1041 }
1042 tex->lod_info.grad.dPdy = read_rvalue(st, dy_expr);
1043 if (tex->lod_info.grad.dPdy == NULL) {
1044 ir_read_error(st, NULL, "when reading dPdy in (txd ...)");
1045 return NULL;
1046 }
1047 break;
1048 }
1049 default:
1050 // tex doesn't have any extra parameters and txf was handled earlier.
1051 break;
1052 };
1053 }
1054 return tex;
1055 }