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