glsl: Use typed foreach_in_list instead of foreach_list.
[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 "ir_reader.h"
25 #include "glsl_parser_extras.h"
26 #include "glsl_types.h"
27 #include "s_expression.h"
28
29 const static bool debug = false;
30
31 namespace {
32
33 class ir_reader {
34 public:
35 ir_reader(_mesa_glsl_parse_state *);
36
37 void read(exec_list *instructions, const char *src, bool scan_for_protos);
38
39 private:
40 void *mem_ctx;
41 _mesa_glsl_parse_state *state;
42
43 void ir_read_error(s_expression *, const char *fmt, ...);
44
45 const glsl_type *read_type(s_expression *);
46
47 void scan_for_prototypes(exec_list *, s_expression *);
48 ir_function *read_function(s_expression *, bool skip_body);
49 void read_function_sig(ir_function *, s_expression *, bool skip_body);
50
51 void read_instructions(exec_list *, s_expression *, ir_loop *);
52 ir_instruction *read_instruction(s_expression *, ir_loop *);
53 ir_variable *read_declaration(s_expression *);
54 ir_if *read_if(s_expression *, ir_loop *);
55 ir_loop *read_loop(s_expression *);
56 ir_call *read_call(s_expression *);
57 ir_return *read_return(s_expression *);
58 ir_rvalue *read_rvalue(s_expression *);
59 ir_assignment *read_assignment(s_expression *);
60 ir_expression *read_expression(s_expression *);
61 ir_swizzle *read_swizzle(s_expression *);
62 ir_constant *read_constant(s_expression *);
63 ir_texture *read_texture(s_expression *);
64 ir_emit_vertex *read_emit_vertex(s_expression *);
65 ir_end_primitive *read_end_primitive(s_expression *);
66
67 ir_dereference *read_dereference(s_expression *);
68 ir_dereference_variable *read_var_ref(s_expression *);
69 };
70
71 } /* anonymous namespace */
72
73 ir_reader::ir_reader(_mesa_glsl_parse_state *state) : state(state)
74 {
75 this->mem_ctx = state;
76 }
77
78 void
79 _mesa_glsl_read_ir(_mesa_glsl_parse_state *state, exec_list *instructions,
80 const char *src, bool scan_for_protos)
81 {
82 ir_reader r(state);
83 r.read(instructions, src, scan_for_protos);
84 }
85
86 void
87 ir_reader::read(exec_list *instructions, const char *src, bool scan_for_protos)
88 {
89 void *sx_mem_ctx = ralloc_context(NULL);
90 s_expression *expr = s_expression::read_expression(sx_mem_ctx, src);
91 if (expr == NULL) {
92 ir_read_error(NULL, "couldn't parse S-Expression.");
93 return;
94 }
95
96 if (scan_for_protos) {
97 scan_for_prototypes(instructions, expr);
98 if (state->error)
99 return;
100 }
101
102 read_instructions(instructions, expr, NULL);
103 ralloc_free(sx_mem_ctx);
104
105 if (debug)
106 validate_ir_tree(instructions);
107 }
108
109 void
110 ir_reader::ir_read_error(s_expression *expr, const char *fmt, ...)
111 {
112 va_list ap;
113
114 state->error = true;
115
116 if (state->current_function != NULL)
117 ralloc_asprintf_append(&state->info_log, "In function %s:\n",
118 state->current_function->function_name());
119 ralloc_strcat(&state->info_log, "error: ");
120
121 va_start(ap, fmt);
122 ralloc_vasprintf_append(&state->info_log, fmt, ap);
123 va_end(ap);
124 ralloc_strcat(&state->info_log, "\n");
125
126 if (expr != NULL) {
127 ralloc_strcat(&state->info_log, "...in this context:\n ");
128 expr->print();
129 ralloc_strcat(&state->info_log, "\n\n");
130 }
131 }
132
133 const glsl_type *
134 ir_reader::read_type(s_expression *expr)
135 {
136 s_expression *s_base_type;
137 s_int *s_size;
138
139 s_pattern pat[] = { "array", s_base_type, s_size };
140 if (MATCH(expr, pat)) {
141 const glsl_type *base_type = read_type(s_base_type);
142 if (base_type == NULL) {
143 ir_read_error(NULL, "when reading base type of array type");
144 return NULL;
145 }
146
147 return glsl_type::get_array_instance(base_type, s_size->value());
148 }
149
150 s_symbol *type_sym = SX_AS_SYMBOL(expr);
151 if (type_sym == NULL) {
152 ir_read_error(expr, "expected <type>");
153 return NULL;
154 }
155
156 const glsl_type *type = state->symbols->get_type(type_sym->value());
157 if (type == NULL)
158 ir_read_error(expr, "invalid type: %s", type_sym->value());
159
160 return type;
161 }
162
163
164 void
165 ir_reader::scan_for_prototypes(exec_list *instructions, s_expression *expr)
166 {
167 s_list *list = SX_AS_LIST(expr);
168 if (list == NULL) {
169 ir_read_error(expr, "Expected (<instruction> ...); found an atom.");
170 return;
171 }
172
173 foreach_list(n, &list->subexpressions) {
174 s_list *sub = SX_AS_LIST(n);
175 if (sub == NULL)
176 continue; // not a (function ...); ignore it.
177
178 s_symbol *tag = SX_AS_SYMBOL(sub->subexpressions.get_head());
179 if (tag == NULL || strcmp(tag->value(), "function") != 0)
180 continue; // not a (function ...); ignore it.
181
182 ir_function *f = read_function(sub, true);
183 if (f == NULL)
184 return;
185 instructions->push_tail(f);
186 }
187 }
188
189 ir_function *
190 ir_reader::read_function(s_expression *expr, bool skip_body)
191 {
192 bool added = false;
193 s_symbol *name;
194
195 s_pattern pat[] = { "function", name };
196 if (!PARTIAL_MATCH(expr, pat)) {
197 ir_read_error(expr, "Expected (function <name> (signature ...) ...)");
198 return NULL;
199 }
200
201 ir_function *f = state->symbols->get_function(name->value());
202 if (f == NULL) {
203 f = new(mem_ctx) ir_function(name->value());
204 added = state->symbols->add_function(f);
205 assert(added);
206 }
207
208 /* Skip over "function" tag and function name (which are guaranteed to be
209 * present by the above PARTIAL_MATCH call).
210 */
211 exec_node *node = ((s_list *) expr)->subexpressions.head->next->next;
212 for (/* nothing */; !node->is_tail_sentinel(); node = node->next) {
213 s_expression *s_sig = (s_expression *) node;
214 read_function_sig(f, s_sig, skip_body);
215 }
216 return added ? f : NULL;
217 }
218
219 static bool
220 always_available(const _mesa_glsl_parse_state *)
221 {
222 return true;
223 }
224
225 void
226 ir_reader::read_function_sig(ir_function *f, s_expression *expr, bool skip_body)
227 {
228 s_expression *type_expr;
229 s_list *paramlist;
230 s_list *body_list;
231
232 s_pattern pat[] = { "signature", type_expr, paramlist, body_list };
233 if (!MATCH(expr, pat)) {
234 ir_read_error(expr, "Expected (signature <type> (parameters ...) "
235 "(<instruction> ...))");
236 return;
237 }
238
239 const glsl_type *return_type = read_type(type_expr);
240 if (return_type == NULL)
241 return;
242
243 s_symbol *paramtag = SX_AS_SYMBOL(paramlist->subexpressions.get_head());
244 if (paramtag == NULL || strcmp(paramtag->value(), "parameters") != 0) {
245 ir_read_error(paramlist, "Expected (parameters ...)");
246 return;
247 }
248
249 // Read the parameters list into a temporary place.
250 exec_list hir_parameters;
251 state->symbols->push_scope();
252
253 /* Skip over the "parameters" tag. */
254 exec_node *node = paramlist->subexpressions.head->next;
255 for (/* nothing */; !node->is_tail_sentinel(); node = node->next) {
256 ir_variable *var = read_declaration((s_expression *) node);
257 if (var == NULL)
258 return;
259
260 hir_parameters.push_tail(var);
261 }
262
263 ir_function_signature *sig =
264 f->exact_matching_signature(state, &hir_parameters);
265 if (sig == NULL && skip_body) {
266 /* If scanning for prototypes, generate a new signature. */
267 /* ir_reader doesn't know what languages support a given built-in, so
268 * just say that they're always available. For now, other mechanisms
269 * guarantee the right built-ins are available.
270 */
271 sig = new(mem_ctx) ir_function_signature(return_type, always_available);
272 f->add_signature(sig);
273 } else if (sig != NULL) {
274 const char *badvar = sig->qualifiers_match(&hir_parameters);
275 if (badvar != NULL) {
276 ir_read_error(expr, "function `%s' parameter `%s' qualifiers "
277 "don't match prototype", f->name, badvar);
278 return;
279 }
280
281 if (sig->return_type != return_type) {
282 ir_read_error(expr, "function `%s' return type doesn't "
283 "match prototype", f->name);
284 return;
285 }
286 } else {
287 /* No prototype for this body exists - skip it. */
288 state->symbols->pop_scope();
289 return;
290 }
291 assert(sig != NULL);
292
293 sig->replace_parameters(&hir_parameters);
294
295 if (!skip_body && !body_list->subexpressions.is_empty()) {
296 if (sig->is_defined) {
297 ir_read_error(expr, "function %s redefined", f->name);
298 return;
299 }
300 state->current_function = sig;
301 read_instructions(&sig->body, body_list, NULL);
302 state->current_function = NULL;
303 sig->is_defined = true;
304 }
305
306 state->symbols->pop_scope();
307 }
308
309 void
310 ir_reader::read_instructions(exec_list *instructions, s_expression *expr,
311 ir_loop *loop_ctx)
312 {
313 // Read in a list of instructions
314 s_list *list = SX_AS_LIST(expr);
315 if (list == NULL) {
316 ir_read_error(expr, "Expected (<instruction> ...); found an atom.");
317 return;
318 }
319
320 foreach_in_list(s_expression, sub, &list->subexpressions) {
321 ir_instruction *ir = read_instruction(sub, loop_ctx);
322 if (ir != NULL) {
323 /* Global variable declarations should be moved to the top, before
324 * any functions that might use them. Functions are added to the
325 * instruction stream when scanning for prototypes, so without this
326 * hack, they always appear before variable declarations.
327 */
328 if (state->current_function == NULL && ir->as_variable() != NULL)
329 instructions->push_head(ir);
330 else
331 instructions->push_tail(ir);
332 }
333 }
334 }
335
336
337 ir_instruction *
338 ir_reader::read_instruction(s_expression *expr, ir_loop *loop_ctx)
339 {
340 s_symbol *symbol = SX_AS_SYMBOL(expr);
341 if (symbol != NULL) {
342 if (strcmp(symbol->value(), "break") == 0 && loop_ctx != NULL)
343 return new(mem_ctx) ir_loop_jump(ir_loop_jump::jump_break);
344 if (strcmp(symbol->value(), "continue") == 0 && loop_ctx != NULL)
345 return new(mem_ctx) ir_loop_jump(ir_loop_jump::jump_continue);
346 }
347
348 s_list *list = SX_AS_LIST(expr);
349 if (list == NULL || list->subexpressions.is_empty()) {
350 ir_read_error(expr, "Invalid instruction.\n");
351 return NULL;
352 }
353
354 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
355 if (tag == NULL) {
356 ir_read_error(expr, "expected instruction tag");
357 return NULL;
358 }
359
360 ir_instruction *inst = NULL;
361 if (strcmp(tag->value(), "declare") == 0) {
362 inst = read_declaration(list);
363 } else if (strcmp(tag->value(), "assign") == 0) {
364 inst = read_assignment(list);
365 } else if (strcmp(tag->value(), "if") == 0) {
366 inst = read_if(list, loop_ctx);
367 } else if (strcmp(tag->value(), "loop") == 0) {
368 inst = read_loop(list);
369 } else if (strcmp(tag->value(), "call") == 0) {
370 inst = read_call(list);
371 } else if (strcmp(tag->value(), "return") == 0) {
372 inst = read_return(list);
373 } else if (strcmp(tag->value(), "function") == 0) {
374 inst = read_function(list, false);
375 } else if (strcmp(tag->value(), "emit-vertex") == 0) {
376 inst = read_emit_vertex(list);
377 } else if (strcmp(tag->value(), "end-primitive") == 0) {
378 inst = read_end_primitive(list);
379 } else {
380 inst = read_rvalue(list);
381 if (inst == NULL)
382 ir_read_error(NULL, "when reading instruction");
383 }
384 return inst;
385 }
386
387 ir_variable *
388 ir_reader::read_declaration(s_expression *expr)
389 {
390 s_list *s_quals;
391 s_expression *s_type;
392 s_symbol *s_name;
393
394 s_pattern pat[] = { "declare", s_quals, s_type, s_name };
395 if (!MATCH(expr, pat)) {
396 ir_read_error(expr, "expected (declare (<qualifiers>) <type> <name>)");
397 return NULL;
398 }
399
400 const glsl_type *type = read_type(s_type);
401 if (type == NULL)
402 return NULL;
403
404 ir_variable *var = new(mem_ctx) ir_variable(type, s_name->value(),
405 ir_var_auto);
406
407 foreach_list(n, &s_quals->subexpressions) {
408 s_symbol *qualifier = SX_AS_SYMBOL(n);
409 if (qualifier == NULL) {
410 ir_read_error(expr, "qualifier list must contain only symbols");
411 return NULL;
412 }
413
414 // FINISHME: Check for duplicate/conflicting qualifiers.
415 if (strcmp(qualifier->value(), "centroid") == 0) {
416 var->data.centroid = 1;
417 } else if (strcmp(qualifier->value(), "sample") == 0) {
418 var->data.sample = 1;
419 } else if (strcmp(qualifier->value(), "invariant") == 0) {
420 var->data.invariant = 1;
421 } else if (strcmp(qualifier->value(), "uniform") == 0) {
422 var->data.mode = ir_var_uniform;
423 } else if (strcmp(qualifier->value(), "auto") == 0) {
424 var->data.mode = ir_var_auto;
425 } else if (strcmp(qualifier->value(), "in") == 0) {
426 var->data.mode = ir_var_function_in;
427 } else if (strcmp(qualifier->value(), "shader_in") == 0) {
428 var->data.mode = ir_var_shader_in;
429 } else if (strcmp(qualifier->value(), "const_in") == 0) {
430 var->data.mode = ir_var_const_in;
431 } else if (strcmp(qualifier->value(), "out") == 0) {
432 var->data.mode = ir_var_function_out;
433 } else if (strcmp(qualifier->value(), "shader_out") == 0) {
434 var->data.mode = ir_var_shader_out;
435 } else if (strcmp(qualifier->value(), "inout") == 0) {
436 var->data.mode = ir_var_function_inout;
437 } else if (strcmp(qualifier->value(), "temporary") == 0) {
438 var->data.mode = ir_var_temporary;
439 } else if (strcmp(qualifier->value(), "stream1") == 0) {
440 var->data.stream = 1;
441 } else if (strcmp(qualifier->value(), "stream2") == 0) {
442 var->data.stream = 2;
443 } else if (strcmp(qualifier->value(), "stream3") == 0) {
444 var->data.stream = 3;
445 } else if (strcmp(qualifier->value(), "smooth") == 0) {
446 var->data.interpolation = INTERP_QUALIFIER_SMOOTH;
447 } else if (strcmp(qualifier->value(), "flat") == 0) {
448 var->data.interpolation = INTERP_QUALIFIER_FLAT;
449 } else if (strcmp(qualifier->value(), "noperspective") == 0) {
450 var->data.interpolation = INTERP_QUALIFIER_NOPERSPECTIVE;
451 } else {
452 ir_read_error(expr, "unknown qualifier: %s", qualifier->value());
453 return NULL;
454 }
455 }
456
457 // Add the variable to the symbol table
458 state->symbols->add_variable(var);
459
460 return var;
461 }
462
463
464 ir_if *
465 ir_reader::read_if(s_expression *expr, ir_loop *loop_ctx)
466 {
467 s_expression *s_cond;
468 s_expression *s_then;
469 s_expression *s_else;
470
471 s_pattern pat[] = { "if", s_cond, s_then, s_else };
472 if (!MATCH(expr, pat)) {
473 ir_read_error(expr, "expected (if <condition> (<then>...) (<else>...))");
474 return NULL;
475 }
476
477 ir_rvalue *condition = read_rvalue(s_cond);
478 if (condition == NULL) {
479 ir_read_error(NULL, "when reading condition of (if ...)");
480 return NULL;
481 }
482
483 ir_if *iff = new(mem_ctx) ir_if(condition);
484
485 read_instructions(&iff->then_instructions, s_then, loop_ctx);
486 read_instructions(&iff->else_instructions, s_else, loop_ctx);
487 if (state->error) {
488 delete iff;
489 iff = NULL;
490 }
491 return iff;
492 }
493
494
495 ir_loop *
496 ir_reader::read_loop(s_expression *expr)
497 {
498 s_expression *s_body;
499
500 s_pattern loop_pat[] = { "loop", s_body };
501 if (!MATCH(expr, loop_pat)) {
502 ir_read_error(expr, "expected (loop <body>)");
503 return NULL;
504 }
505
506 ir_loop *loop = new(mem_ctx) ir_loop;
507
508 read_instructions(&loop->body_instructions, s_body, loop);
509 if (state->error) {
510 delete loop;
511 loop = NULL;
512 }
513 return loop;
514 }
515
516
517 ir_return *
518 ir_reader::read_return(s_expression *expr)
519 {
520 s_expression *s_retval;
521
522 s_pattern return_value_pat[] = { "return", s_retval};
523 s_pattern return_void_pat[] = { "return" };
524 if (MATCH(expr, return_value_pat)) {
525 ir_rvalue *retval = read_rvalue(s_retval);
526 if (retval == NULL) {
527 ir_read_error(NULL, "when reading return value");
528 return NULL;
529 }
530 return new(mem_ctx) ir_return(retval);
531 } else if (MATCH(expr, return_void_pat)) {
532 return new(mem_ctx) ir_return;
533 } else {
534 ir_read_error(expr, "expected (return <rvalue>) or (return)");
535 return NULL;
536 }
537 }
538
539
540 ir_rvalue *
541 ir_reader::read_rvalue(s_expression *expr)
542 {
543 s_list *list = SX_AS_LIST(expr);
544 if (list == NULL || list->subexpressions.is_empty())
545 return NULL;
546
547 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
548 if (tag == NULL) {
549 ir_read_error(expr, "expected rvalue tag");
550 return NULL;
551 }
552
553 ir_rvalue *rvalue = read_dereference(list);
554 if (rvalue != NULL || state->error)
555 return rvalue;
556 else if (strcmp(tag->value(), "swiz") == 0) {
557 rvalue = read_swizzle(list);
558 } else if (strcmp(tag->value(), "expression") == 0) {
559 rvalue = read_expression(list);
560 } else if (strcmp(tag->value(), "constant") == 0) {
561 rvalue = read_constant(list);
562 } else {
563 rvalue = read_texture(list);
564 if (rvalue == NULL && !state->error)
565 ir_read_error(expr, "unrecognized rvalue tag: %s", tag->value());
566 }
567
568 return rvalue;
569 }
570
571 ir_assignment *
572 ir_reader::read_assignment(s_expression *expr)
573 {
574 s_expression *cond_expr = NULL;
575 s_expression *lhs_expr, *rhs_expr;
576 s_list *mask_list;
577
578 s_pattern pat4[] = { "assign", mask_list, lhs_expr, rhs_expr };
579 s_pattern pat5[] = { "assign", cond_expr, mask_list, lhs_expr, rhs_expr };
580 if (!MATCH(expr, pat4) && !MATCH(expr, pat5)) {
581 ir_read_error(expr, "expected (assign [<condition>] (<write mask>) "
582 "<lhs> <rhs>)");
583 return NULL;
584 }
585
586 ir_rvalue *condition = NULL;
587 if (cond_expr != NULL) {
588 condition = read_rvalue(cond_expr);
589 if (condition == NULL) {
590 ir_read_error(NULL, "when reading condition of assignment");
591 return NULL;
592 }
593 }
594
595 unsigned mask = 0;
596
597 s_symbol *mask_symbol;
598 s_pattern mask_pat[] = { mask_symbol };
599 if (MATCH(mask_list, mask_pat)) {
600 const char *mask_str = mask_symbol->value();
601 unsigned mask_length = strlen(mask_str);
602 if (mask_length > 4) {
603 ir_read_error(expr, "invalid write mask: %s", mask_str);
604 return NULL;
605 }
606
607 const unsigned idx_map[] = { 3, 0, 1, 2 }; /* w=bit 3, x=0, y=1, z=2 */
608
609 for (unsigned i = 0; i < mask_length; i++) {
610 if (mask_str[i] < 'w' || mask_str[i] > 'z') {
611 ir_read_error(expr, "write mask contains invalid character: %c",
612 mask_str[i]);
613 return NULL;
614 }
615 mask |= 1 << idx_map[mask_str[i] - 'w'];
616 }
617 } else if (!mask_list->subexpressions.is_empty()) {
618 ir_read_error(mask_list, "expected () or (<write mask>)");
619 return NULL;
620 }
621
622 ir_dereference *lhs = read_dereference(lhs_expr);
623 if (lhs == NULL) {
624 ir_read_error(NULL, "when reading left-hand side of assignment");
625 return NULL;
626 }
627
628 ir_rvalue *rhs = read_rvalue(rhs_expr);
629 if (rhs == NULL) {
630 ir_read_error(NULL, "when reading right-hand side of assignment");
631 return NULL;
632 }
633
634 if (mask == 0 && (lhs->type->is_vector() || lhs->type->is_scalar())) {
635 ir_read_error(expr, "non-zero write mask required.");
636 return NULL;
637 }
638
639 return new(mem_ctx) ir_assignment(lhs, rhs, condition, mask);
640 }
641
642 ir_call *
643 ir_reader::read_call(s_expression *expr)
644 {
645 s_symbol *name;
646 s_list *params;
647 s_list *s_return = NULL;
648
649 ir_dereference_variable *return_deref = NULL;
650
651 s_pattern void_pat[] = { "call", name, params };
652 s_pattern non_void_pat[] = { "call", name, s_return, params };
653 if (MATCH(expr, non_void_pat)) {
654 return_deref = read_var_ref(s_return);
655 if (return_deref == NULL) {
656 ir_read_error(s_return, "when reading a call's return storage");
657 return NULL;
658 }
659 } else if (!MATCH(expr, void_pat)) {
660 ir_read_error(expr, "expected (call <name> [<deref>] (<param> ...))");
661 return NULL;
662 }
663
664 exec_list parameters;
665
666 foreach_in_list(s_expression, e, &params->subexpressions) {
667 ir_rvalue *param = read_rvalue(e);
668 if (param == NULL) {
669 ir_read_error(e, "when reading parameter to function call");
670 return NULL;
671 }
672 parameters.push_tail(param);
673 }
674
675 ir_function *f = state->symbols->get_function(name->value());
676 if (f == NULL) {
677 ir_read_error(expr, "found call to undefined function %s",
678 name->value());
679 return NULL;
680 }
681
682 ir_function_signature *callee = f->matching_signature(state, &parameters);
683 if (callee == NULL) {
684 ir_read_error(expr, "couldn't find matching signature for function "
685 "%s", name->value());
686 return NULL;
687 }
688
689 if (callee->return_type == glsl_type::void_type && return_deref) {
690 ir_read_error(expr, "call has return value storage but void type");
691 return NULL;
692 } else if (callee->return_type != glsl_type::void_type && !return_deref) {
693 ir_read_error(expr, "call has non-void type but no return value storage");
694 return NULL;
695 }
696
697 return new(mem_ctx) ir_call(callee, return_deref, &parameters);
698 }
699
700 ir_expression *
701 ir_reader::read_expression(s_expression *expr)
702 {
703 s_expression *s_type;
704 s_symbol *s_op;
705 s_expression *s_arg[4] = {NULL};
706
707 s_pattern pat[] = { "expression", s_type, s_op, s_arg[0] };
708 if (!PARTIAL_MATCH(expr, pat)) {
709 ir_read_error(expr, "expected (expression <type> <operator> "
710 "<operand> [<operand>] [<operand>] [<operand>])");
711 return NULL;
712 }
713 s_arg[1] = (s_expression *) s_arg[0]->next; // may be tail sentinel
714 s_arg[2] = (s_expression *) s_arg[1]->next; // may be tail sentinel or NULL
715 if (s_arg[2])
716 s_arg[3] = (s_expression *) s_arg[2]->next; // may be tail sentinel or NULL
717
718 const glsl_type *type = read_type(s_type);
719 if (type == NULL)
720 return NULL;
721
722 /* Read the operator */
723 ir_expression_operation op = ir_expression::get_operator(s_op->value());
724 if (op == (ir_expression_operation) -1) {
725 ir_read_error(expr, "invalid operator: %s", s_op->value());
726 return NULL;
727 }
728
729 int num_operands = -3; /* skip "expression" <type> <operation> */
730 foreach_in_list(s_expression, e, &((s_list *) expr)->subexpressions)
731 ++num_operands;
732
733 int expected_operands = ir_expression::get_num_operands(op);
734 if (num_operands != expected_operands) {
735 ir_read_error(expr, "found %d expression operands, expected %d",
736 num_operands, expected_operands);
737 return NULL;
738 }
739
740 ir_rvalue *arg[4] = {NULL};
741 for (int i = 0; i < num_operands; i++) {
742 arg[i] = read_rvalue(s_arg[i]);
743 if (arg[i] == NULL) {
744 ir_read_error(NULL, "when reading operand #%d of %s", i, s_op->value());
745 return NULL;
746 }
747 }
748
749 return new(mem_ctx) ir_expression(op, type, arg[0], arg[1], arg[2], arg[3]);
750 }
751
752 ir_swizzle *
753 ir_reader::read_swizzle(s_expression *expr)
754 {
755 s_symbol *swiz;
756 s_expression *sub;
757
758 s_pattern pat[] = { "swiz", swiz, sub };
759 if (!MATCH(expr, pat)) {
760 ir_read_error(expr, "expected (swiz <swizzle> <rvalue>)");
761 return NULL;
762 }
763
764 if (strlen(swiz->value()) > 4) {
765 ir_read_error(expr, "expected a valid swizzle; found %s", swiz->value());
766 return NULL;
767 }
768
769 ir_rvalue *rvalue = read_rvalue(sub);
770 if (rvalue == NULL)
771 return NULL;
772
773 ir_swizzle *ir = ir_swizzle::create(rvalue, swiz->value(),
774 rvalue->type->vector_elements);
775 if (ir == NULL)
776 ir_read_error(expr, "invalid swizzle");
777
778 return ir;
779 }
780
781 ir_constant *
782 ir_reader::read_constant(s_expression *expr)
783 {
784 s_expression *type_expr;
785 s_list *values;
786
787 s_pattern pat[] = { "constant", type_expr, values };
788 if (!MATCH(expr, pat)) {
789 ir_read_error(expr, "expected (constant <type> (...))");
790 return NULL;
791 }
792
793 const glsl_type *type = read_type(type_expr);
794 if (type == NULL)
795 return NULL;
796
797 if (values == NULL) {
798 ir_read_error(expr, "expected (constant <type> (...))");
799 return NULL;
800 }
801
802 if (type->is_array()) {
803 unsigned elements_supplied = 0;
804 exec_list elements;
805 foreach_in_list(s_expression, elt, &values->subexpressions) {
806 ir_constant *ir_elt = read_constant(elt);
807 if (ir_elt == NULL)
808 return NULL;
809 elements.push_tail(ir_elt);
810 elements_supplied++;
811 }
812
813 if (elements_supplied != type->length) {
814 ir_read_error(values, "expected exactly %u array elements, "
815 "given %u", type->length, elements_supplied);
816 return NULL;
817 }
818 return new(mem_ctx) ir_constant(type, &elements);
819 }
820
821 ir_constant_data data = { { 0 } };
822
823 // Read in list of values (at most 16).
824 unsigned k = 0;
825 foreach_in_list(s_expression, expr, &values->subexpressions) {
826 if (k >= 16) {
827 ir_read_error(values, "expected at most 16 numbers");
828 return NULL;
829 }
830
831 if (type->base_type == GLSL_TYPE_FLOAT) {
832 s_number *value = SX_AS_NUMBER(expr);
833 if (value == NULL) {
834 ir_read_error(values, "expected numbers");
835 return NULL;
836 }
837 data.f[k] = value->fvalue();
838 } else {
839 s_int *value = SX_AS_INT(expr);
840 if (value == NULL) {
841 ir_read_error(values, "expected integers");
842 return NULL;
843 }
844
845 switch (type->base_type) {
846 case GLSL_TYPE_UINT: {
847 data.u[k] = value->value();
848 break;
849 }
850 case GLSL_TYPE_INT: {
851 data.i[k] = value->value();
852 break;
853 }
854 case GLSL_TYPE_BOOL: {
855 data.b[k] = value->value();
856 break;
857 }
858 default:
859 ir_read_error(values, "unsupported constant type");
860 return NULL;
861 }
862 }
863 ++k;
864 }
865 if (k != type->components()) {
866 ir_read_error(values, "expected %u constant values, found %u",
867 type->components(), k);
868 return NULL;
869 }
870
871 return new(mem_ctx) ir_constant(type, &data);
872 }
873
874 ir_dereference_variable *
875 ir_reader::read_var_ref(s_expression *expr)
876 {
877 s_symbol *s_var;
878 s_pattern var_pat[] = { "var_ref", s_var };
879
880 if (MATCH(expr, var_pat)) {
881 ir_variable *var = state->symbols->get_variable(s_var->value());
882 if (var == NULL) {
883 ir_read_error(expr, "undeclared variable: %s", s_var->value());
884 return NULL;
885 }
886 return new(mem_ctx) ir_dereference_variable(var);
887 }
888 return NULL;
889 }
890
891 ir_dereference *
892 ir_reader::read_dereference(s_expression *expr)
893 {
894 s_expression *s_subject;
895 s_expression *s_index;
896 s_symbol *s_field;
897
898 s_pattern array_pat[] = { "array_ref", s_subject, s_index };
899 s_pattern record_pat[] = { "record_ref", s_subject, s_field };
900
901 ir_dereference_variable *var_ref = read_var_ref(expr);
902 if (var_ref != NULL) {
903 return var_ref;
904 } else if (MATCH(expr, array_pat)) {
905 ir_rvalue *subject = read_rvalue(s_subject);
906 if (subject == NULL) {
907 ir_read_error(NULL, "when reading the subject of an array_ref");
908 return NULL;
909 }
910
911 ir_rvalue *idx = read_rvalue(s_index);
912 if (idx == NULL) {
913 ir_read_error(NULL, "when reading the index of an array_ref");
914 return NULL;
915 }
916 return new(mem_ctx) ir_dereference_array(subject, idx);
917 } else if (MATCH(expr, record_pat)) {
918 ir_rvalue *subject = read_rvalue(s_subject);
919 if (subject == NULL) {
920 ir_read_error(NULL, "when reading the subject of a record_ref");
921 return NULL;
922 }
923 return new(mem_ctx) ir_dereference_record(subject, s_field->value());
924 }
925 return NULL;
926 }
927
928 ir_texture *
929 ir_reader::read_texture(s_expression *expr)
930 {
931 s_symbol *tag = NULL;
932 s_expression *s_type = NULL;
933 s_expression *s_sampler = NULL;
934 s_expression *s_coord = NULL;
935 s_expression *s_offset = NULL;
936 s_expression *s_proj = NULL;
937 s_list *s_shadow = NULL;
938 s_expression *s_lod = NULL;
939 s_expression *s_sample_index = NULL;
940 s_expression *s_component = NULL;
941
942 ir_texture_opcode op = ir_tex; /* silence warning */
943
944 s_pattern tex_pattern[] =
945 { "tex", s_type, s_sampler, s_coord, s_offset, s_proj, s_shadow };
946 s_pattern lod_pattern[] =
947 { "lod", s_type, s_sampler, s_coord };
948 s_pattern txf_pattern[] =
949 { "txf", s_type, s_sampler, s_coord, s_offset, s_lod };
950 s_pattern txf_ms_pattern[] =
951 { "txf_ms", s_type, s_sampler, s_coord, s_sample_index };
952 s_pattern txs_pattern[] =
953 { "txs", s_type, s_sampler, s_lod };
954 s_pattern tg4_pattern[] =
955 { "tg4", s_type, s_sampler, s_coord, s_offset, s_component };
956 s_pattern query_levels_pattern[] =
957 { "query_levels", s_type, s_sampler };
958 s_pattern other_pattern[] =
959 { tag, s_type, s_sampler, s_coord, s_offset, s_proj, s_shadow, s_lod };
960
961 if (MATCH(expr, lod_pattern)) {
962 op = ir_lod;
963 } else if (MATCH(expr, tex_pattern)) {
964 op = ir_tex;
965 } else if (MATCH(expr, txf_pattern)) {
966 op = ir_txf;
967 } else if (MATCH(expr, txf_ms_pattern)) {
968 op = ir_txf_ms;
969 } else if (MATCH(expr, txs_pattern)) {
970 op = ir_txs;
971 } else if (MATCH(expr, tg4_pattern)) {
972 op = ir_tg4;
973 } else if (MATCH(expr, query_levels_pattern)) {
974 op = ir_query_levels;
975 } else if (MATCH(expr, other_pattern)) {
976 op = ir_texture::get_opcode(tag->value());
977 if (op == -1)
978 return NULL;
979 } else {
980 ir_read_error(NULL, "unexpected texture pattern %s", tag->value());
981 return NULL;
982 }
983
984 ir_texture *tex = new(mem_ctx) ir_texture(op);
985
986 // Read return type
987 const glsl_type *type = read_type(s_type);
988 if (type == NULL) {
989 ir_read_error(NULL, "when reading type in (%s ...)",
990 tex->opcode_string());
991 return NULL;
992 }
993
994 // Read sampler (must be a deref)
995 ir_dereference *sampler = read_dereference(s_sampler);
996 if (sampler == NULL) {
997 ir_read_error(NULL, "when reading sampler in (%s ...)",
998 tex->opcode_string());
999 return NULL;
1000 }
1001 tex->set_sampler(sampler, type);
1002
1003 if (op != ir_txs) {
1004 // Read coordinate (any rvalue)
1005 tex->coordinate = read_rvalue(s_coord);
1006 if (tex->coordinate == NULL) {
1007 ir_read_error(NULL, "when reading coordinate in (%s ...)",
1008 tex->opcode_string());
1009 return NULL;
1010 }
1011
1012 if (op != ir_txf_ms && op != ir_lod) {
1013 // Read texel offset - either 0 or an rvalue.
1014 s_int *si_offset = SX_AS_INT(s_offset);
1015 if (si_offset == NULL || si_offset->value() != 0) {
1016 tex->offset = read_rvalue(s_offset);
1017 if (tex->offset == NULL) {
1018 ir_read_error(s_offset, "expected 0 or an expression");
1019 return NULL;
1020 }
1021 }
1022 }
1023 }
1024
1025 if (op != ir_txf && op != ir_txf_ms &&
1026 op != ir_txs && op != ir_lod && op != ir_tg4 &&
1027 op != ir_query_levels) {
1028 s_int *proj_as_int = SX_AS_INT(s_proj);
1029 if (proj_as_int && proj_as_int->value() == 1) {
1030 tex->projector = NULL;
1031 } else {
1032 tex->projector = read_rvalue(s_proj);
1033 if (tex->projector == NULL) {
1034 ir_read_error(NULL, "when reading projective divide in (%s ..)",
1035 tex->opcode_string());
1036 return NULL;
1037 }
1038 }
1039
1040 if (s_shadow->subexpressions.is_empty()) {
1041 tex->shadow_comparitor = NULL;
1042 } else {
1043 tex->shadow_comparitor = read_rvalue(s_shadow);
1044 if (tex->shadow_comparitor == NULL) {
1045 ir_read_error(NULL, "when reading shadow comparitor in (%s ..)",
1046 tex->opcode_string());
1047 return NULL;
1048 }
1049 }
1050 }
1051
1052 switch (op) {
1053 case ir_txb:
1054 tex->lod_info.bias = read_rvalue(s_lod);
1055 if (tex->lod_info.bias == NULL) {
1056 ir_read_error(NULL, "when reading LOD bias in (txb ...)");
1057 return NULL;
1058 }
1059 break;
1060 case ir_txl:
1061 case ir_txf:
1062 case ir_txs:
1063 tex->lod_info.lod = read_rvalue(s_lod);
1064 if (tex->lod_info.lod == NULL) {
1065 ir_read_error(NULL, "when reading LOD in (%s ...)",
1066 tex->opcode_string());
1067 return NULL;
1068 }
1069 break;
1070 case ir_txf_ms:
1071 tex->lod_info.sample_index = read_rvalue(s_sample_index);
1072 if (tex->lod_info.sample_index == NULL) {
1073 ir_read_error(NULL, "when reading sample_index in (txf_ms ...)");
1074 return NULL;
1075 }
1076 break;
1077 case ir_txd: {
1078 s_expression *s_dx, *s_dy;
1079 s_pattern dxdy_pat[] = { s_dx, s_dy };
1080 if (!MATCH(s_lod, dxdy_pat)) {
1081 ir_read_error(s_lod, "expected (dPdx dPdy) in (txd ...)");
1082 return NULL;
1083 }
1084 tex->lod_info.grad.dPdx = read_rvalue(s_dx);
1085 if (tex->lod_info.grad.dPdx == NULL) {
1086 ir_read_error(NULL, "when reading dPdx in (txd ...)");
1087 return NULL;
1088 }
1089 tex->lod_info.grad.dPdy = read_rvalue(s_dy);
1090 if (tex->lod_info.grad.dPdy == NULL) {
1091 ir_read_error(NULL, "when reading dPdy in (txd ...)");
1092 return NULL;
1093 }
1094 break;
1095 }
1096 case ir_tg4:
1097 tex->lod_info.component = read_rvalue(s_component);
1098 if (tex->lod_info.component == NULL) {
1099 ir_read_error(NULL, "when reading component in (tg4 ...)");
1100 return NULL;
1101 }
1102 break;
1103 default:
1104 // tex and lod don't have any extra parameters.
1105 break;
1106 };
1107 return tex;
1108 }
1109
1110 ir_emit_vertex *
1111 ir_reader::read_emit_vertex(s_expression *expr)
1112 {
1113 s_expression *s_stream = NULL;
1114
1115 s_pattern pat[] = { "emit-vertex", s_stream };
1116
1117 if (MATCH(expr, pat)) {
1118 ir_rvalue *stream = read_dereference(s_stream);
1119 if (stream == NULL) {
1120 ir_read_error(NULL, "when reading stream info in emit-vertex");
1121 return NULL;
1122 }
1123 return new(mem_ctx) ir_emit_vertex(stream);
1124 }
1125 ir_read_error(NULL, "when reading emit-vertex");
1126 return NULL;
1127 }
1128
1129 ir_end_primitive *
1130 ir_reader::read_end_primitive(s_expression *expr)
1131 {
1132 s_expression *s_stream = NULL;
1133
1134 s_pattern pat[] = { "end-primitive", s_stream };
1135
1136 if (MATCH(expr, pat)) {
1137 ir_rvalue *stream = read_dereference(s_stream);
1138 if (stream == NULL) {
1139 ir_read_error(NULL, "when reading stream info in end-primitive");
1140 return NULL;
1141 }
1142 return new(mem_ctx) ir_end_primitive(stream);
1143 }
1144 ir_read_error(NULL, "when reading end-primitive");
1145 return NULL;
1146 }