glsl: Put `sample`-qualified varyings in their own packing classes
[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_counter, *s_from, *s_to, *s_inc, *s_body;
492
493 s_pattern pat[] = { "loop", s_counter, s_from, s_to, s_inc, s_body };
494 if (!MATCH(expr, pat)) {
495 ir_read_error(expr, "expected (loop <counter> <from> <to> "
496 "<increment> <body>)");
497 return NULL;
498 }
499
500 // FINISHME: actually read the count/from/to fields.
501
502 ir_loop *loop = new(mem_ctx) ir_loop;
503 read_instructions(&loop->body_instructions, s_body, loop);
504 if (state->error) {
505 delete loop;
506 loop = NULL;
507 }
508 return loop;
509 }
510
511
512 ir_return *
513 ir_reader::read_return(s_expression *expr)
514 {
515 s_expression *s_retval;
516
517 s_pattern return_value_pat[] = { "return", s_retval};
518 s_pattern return_void_pat[] = { "return" };
519 if (MATCH(expr, return_value_pat)) {
520 ir_rvalue *retval = read_rvalue(s_retval);
521 if (retval == NULL) {
522 ir_read_error(NULL, "when reading return value");
523 return NULL;
524 }
525 return new(mem_ctx) ir_return(retval);
526 } else if (MATCH(expr, return_void_pat)) {
527 return new(mem_ctx) ir_return;
528 } else {
529 ir_read_error(expr, "expected (return <rvalue>) or (return)");
530 return NULL;
531 }
532 }
533
534
535 ir_rvalue *
536 ir_reader::read_rvalue(s_expression *expr)
537 {
538 s_list *list = SX_AS_LIST(expr);
539 if (list == NULL || list->subexpressions.is_empty())
540 return NULL;
541
542 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
543 if (tag == NULL) {
544 ir_read_error(expr, "expected rvalue tag");
545 return NULL;
546 }
547
548 ir_rvalue *rvalue = read_dereference(list);
549 if (rvalue != NULL || state->error)
550 return rvalue;
551 else if (strcmp(tag->value(), "swiz") == 0) {
552 rvalue = read_swizzle(list);
553 } else if (strcmp(tag->value(), "expression") == 0) {
554 rvalue = read_expression(list);
555 } else if (strcmp(tag->value(), "constant") == 0) {
556 rvalue = read_constant(list);
557 } else {
558 rvalue = read_texture(list);
559 if (rvalue == NULL && !state->error)
560 ir_read_error(expr, "unrecognized rvalue tag: %s", tag->value());
561 }
562
563 return rvalue;
564 }
565
566 ir_assignment *
567 ir_reader::read_assignment(s_expression *expr)
568 {
569 s_expression *cond_expr = NULL;
570 s_expression *lhs_expr, *rhs_expr;
571 s_list *mask_list;
572
573 s_pattern pat4[] = { "assign", mask_list, lhs_expr, rhs_expr };
574 s_pattern pat5[] = { "assign", cond_expr, mask_list, lhs_expr, rhs_expr };
575 if (!MATCH(expr, pat4) && !MATCH(expr, pat5)) {
576 ir_read_error(expr, "expected (assign [<condition>] (<write mask>) "
577 "<lhs> <rhs>)");
578 return NULL;
579 }
580
581 ir_rvalue *condition = NULL;
582 if (cond_expr != NULL) {
583 condition = read_rvalue(cond_expr);
584 if (condition == NULL) {
585 ir_read_error(NULL, "when reading condition of assignment");
586 return NULL;
587 }
588 }
589
590 unsigned mask = 0;
591
592 s_symbol *mask_symbol;
593 s_pattern mask_pat[] = { mask_symbol };
594 if (MATCH(mask_list, mask_pat)) {
595 const char *mask_str = mask_symbol->value();
596 unsigned mask_length = strlen(mask_str);
597 if (mask_length > 4) {
598 ir_read_error(expr, "invalid write mask: %s", mask_str);
599 return NULL;
600 }
601
602 const unsigned idx_map[] = { 3, 0, 1, 2 }; /* w=bit 3, x=0, y=1, z=2 */
603
604 for (unsigned i = 0; i < mask_length; i++) {
605 if (mask_str[i] < 'w' || mask_str[i] > 'z') {
606 ir_read_error(expr, "write mask contains invalid character: %c",
607 mask_str[i]);
608 return NULL;
609 }
610 mask |= 1 << idx_map[mask_str[i] - 'w'];
611 }
612 } else if (!mask_list->subexpressions.is_empty()) {
613 ir_read_error(mask_list, "expected () or (<write mask>)");
614 return NULL;
615 }
616
617 ir_dereference *lhs = read_dereference(lhs_expr);
618 if (lhs == NULL) {
619 ir_read_error(NULL, "when reading left-hand side of assignment");
620 return NULL;
621 }
622
623 ir_rvalue *rhs = read_rvalue(rhs_expr);
624 if (rhs == NULL) {
625 ir_read_error(NULL, "when reading right-hand side of assignment");
626 return NULL;
627 }
628
629 if (mask == 0 && (lhs->type->is_vector() || lhs->type->is_scalar())) {
630 ir_read_error(expr, "non-zero write mask required.");
631 return NULL;
632 }
633
634 return new(mem_ctx) ir_assignment(lhs, rhs, condition, mask);
635 }
636
637 ir_call *
638 ir_reader::read_call(s_expression *expr)
639 {
640 s_symbol *name;
641 s_list *params;
642 s_list *s_return = NULL;
643
644 ir_dereference_variable *return_deref = NULL;
645
646 s_pattern void_pat[] = { "call", name, params };
647 s_pattern non_void_pat[] = { "call", name, s_return, params };
648 if (MATCH(expr, non_void_pat)) {
649 return_deref = read_var_ref(s_return);
650 if (return_deref == NULL) {
651 ir_read_error(s_return, "when reading a call's return storage");
652 return NULL;
653 }
654 } else if (!MATCH(expr, void_pat)) {
655 ir_read_error(expr, "expected (call <name> [<deref>] (<param> ...))");
656 return NULL;
657 }
658
659 exec_list parameters;
660
661 foreach_iter(exec_list_iterator, it, params->subexpressions) {
662 s_expression *expr = (s_expression*) it.get();
663 ir_rvalue *param = read_rvalue(expr);
664 if (param == NULL) {
665 ir_read_error(expr, "when reading parameter to function call");
666 return NULL;
667 }
668 parameters.push_tail(param);
669 }
670
671 ir_function *f = state->symbols->get_function(name->value());
672 if (f == NULL) {
673 ir_read_error(expr, "found call to undefined function %s",
674 name->value());
675 return NULL;
676 }
677
678 ir_function_signature *callee = f->matching_signature(state, &parameters);
679 if (callee == NULL) {
680 ir_read_error(expr, "couldn't find matching signature for function "
681 "%s", name->value());
682 return NULL;
683 }
684
685 if (callee->return_type == glsl_type::void_type && return_deref) {
686 ir_read_error(expr, "call has return value storage but void type");
687 return NULL;
688 } else if (callee->return_type != glsl_type::void_type && !return_deref) {
689 ir_read_error(expr, "call has non-void type but no return value storage");
690 return NULL;
691 }
692
693 return new(mem_ctx) ir_call(callee, return_deref, &parameters);
694 }
695
696 ir_expression *
697 ir_reader::read_expression(s_expression *expr)
698 {
699 s_expression *s_type;
700 s_symbol *s_op;
701 s_expression *s_arg[4] = {NULL};
702
703 s_pattern pat[] = { "expression", s_type, s_op, s_arg[0] };
704 if (!PARTIAL_MATCH(expr, pat)) {
705 ir_read_error(expr, "expected (expression <type> <operator> "
706 "<operand> [<operand>] [<operand>] [<operand>])");
707 return NULL;
708 }
709 s_arg[1] = (s_expression *) s_arg[0]->next; // may be tail sentinel
710 s_arg[2] = (s_expression *) s_arg[1]->next; // may be tail sentinel or NULL
711 if (s_arg[2])
712 s_arg[3] = (s_expression *) s_arg[2]->next; // may be tail sentinel or NULL
713
714 const glsl_type *type = read_type(s_type);
715 if (type == NULL)
716 return NULL;
717
718 /* Read the operator */
719 ir_expression_operation op = ir_expression::get_operator(s_op->value());
720 if (op == (ir_expression_operation) -1) {
721 ir_read_error(expr, "invalid operator: %s", s_op->value());
722 return NULL;
723 }
724
725 int num_operands = -3; /* skip "expression" <type> <operation> */
726 foreach_list(n, &((s_list *) expr)->subexpressions)
727 ++num_operands;
728
729 int expected_operands = ir_expression::get_num_operands(op);
730 if (num_operands != expected_operands) {
731 ir_read_error(expr, "found %d expression operands, expected %d",
732 num_operands, expected_operands);
733 return NULL;
734 }
735
736 ir_rvalue *arg[4] = {NULL};
737 for (int i = 0; i < num_operands; i++) {
738 arg[i] = read_rvalue(s_arg[i]);
739 if (arg[i] == NULL) {
740 ir_read_error(NULL, "when reading operand #%d of %s", i, s_op->value());
741 return NULL;
742 }
743 }
744
745 return new(mem_ctx) ir_expression(op, type, arg[0], arg[1], arg[2], arg[3]);
746 }
747
748 ir_swizzle *
749 ir_reader::read_swizzle(s_expression *expr)
750 {
751 s_symbol *swiz;
752 s_expression *sub;
753
754 s_pattern pat[] = { "swiz", swiz, sub };
755 if (!MATCH(expr, pat)) {
756 ir_read_error(expr, "expected (swiz <swizzle> <rvalue>)");
757 return NULL;
758 }
759
760 if (strlen(swiz->value()) > 4) {
761 ir_read_error(expr, "expected a valid swizzle; found %s", swiz->value());
762 return NULL;
763 }
764
765 ir_rvalue *rvalue = read_rvalue(sub);
766 if (rvalue == NULL)
767 return NULL;
768
769 ir_swizzle *ir = ir_swizzle::create(rvalue, swiz->value(),
770 rvalue->type->vector_elements);
771 if (ir == NULL)
772 ir_read_error(expr, "invalid swizzle");
773
774 return ir;
775 }
776
777 ir_constant *
778 ir_reader::read_constant(s_expression *expr)
779 {
780 s_expression *type_expr;
781 s_list *values;
782
783 s_pattern pat[] = { "constant", type_expr, values };
784 if (!MATCH(expr, pat)) {
785 ir_read_error(expr, "expected (constant <type> (...))");
786 return NULL;
787 }
788
789 const glsl_type *type = read_type(type_expr);
790 if (type == NULL)
791 return NULL;
792
793 if (values == NULL) {
794 ir_read_error(expr, "expected (constant <type> (...))");
795 return NULL;
796 }
797
798 if (type->is_array()) {
799 unsigned elements_supplied = 0;
800 exec_list elements;
801 foreach_iter(exec_list_iterator, it, values->subexpressions) {
802 s_expression *elt = (s_expression *) it.get();
803 ir_constant *ir_elt = read_constant(elt);
804 if (ir_elt == NULL)
805 return NULL;
806 elements.push_tail(ir_elt);
807 elements_supplied++;
808 }
809
810 if (elements_supplied != type->length) {
811 ir_read_error(values, "expected exactly %u array elements, "
812 "given %u", type->length, elements_supplied);
813 return NULL;
814 }
815 return new(mem_ctx) ir_constant(type, &elements);
816 }
817
818 ir_constant_data data = { { 0 } };
819
820 // Read in list of values (at most 16).
821 unsigned k = 0;
822 foreach_iter(exec_list_iterator, it, values->subexpressions) {
823 if (k >= 16) {
824 ir_read_error(values, "expected at most 16 numbers");
825 return NULL;
826 }
827
828 s_expression *expr = (s_expression*) it.get();
829
830 if (type->base_type == GLSL_TYPE_FLOAT) {
831 s_number *value = SX_AS_NUMBER(expr);
832 if (value == NULL) {
833 ir_read_error(values, "expected numbers");
834 return NULL;
835 }
836 data.f[k] = value->fvalue();
837 } else {
838 s_int *value = SX_AS_INT(expr);
839 if (value == NULL) {
840 ir_read_error(values, "expected integers");
841 return NULL;
842 }
843
844 switch (type->base_type) {
845 case GLSL_TYPE_UINT: {
846 data.u[k] = value->value();
847 break;
848 }
849 case GLSL_TYPE_INT: {
850 data.i[k] = value->value();
851 break;
852 }
853 case GLSL_TYPE_BOOL: {
854 data.b[k] = value->value();
855 break;
856 }
857 default:
858 ir_read_error(values, "unsupported constant type");
859 return NULL;
860 }
861 }
862 ++k;
863 }
864 if (k != type->components()) {
865 ir_read_error(values, "expected %u constant values, found %u",
866 type->components(), k);
867 return NULL;
868 }
869
870 return new(mem_ctx) ir_constant(type, &data);
871 }
872
873 ir_dereference_variable *
874 ir_reader::read_var_ref(s_expression *expr)
875 {
876 s_symbol *s_var;
877 s_pattern var_pat[] = { "var_ref", s_var };
878
879 if (MATCH(expr, var_pat)) {
880 ir_variable *var = state->symbols->get_variable(s_var->value());
881 if (var == NULL) {
882 ir_read_error(expr, "undeclared variable: %s", s_var->value());
883 return NULL;
884 }
885 return new(mem_ctx) ir_dereference_variable(var);
886 }
887 return NULL;
888 }
889
890 ir_dereference *
891 ir_reader::read_dereference(s_expression *expr)
892 {
893 s_expression *s_subject;
894 s_expression *s_index;
895 s_symbol *s_field;
896
897 s_pattern array_pat[] = { "array_ref", s_subject, s_index };
898 s_pattern record_pat[] = { "record_ref", s_subject, s_field };
899
900 ir_dereference_variable *var_ref = read_var_ref(expr);
901 if (var_ref != NULL) {
902 return var_ref;
903 } else if (MATCH(expr, array_pat)) {
904 ir_rvalue *subject = read_rvalue(s_subject);
905 if (subject == NULL) {
906 ir_read_error(NULL, "when reading the subject of an array_ref");
907 return NULL;
908 }
909
910 ir_rvalue *idx = read_rvalue(s_index);
911 if (idx == NULL) {
912 ir_read_error(NULL, "when reading the index of an array_ref");
913 return NULL;
914 }
915 return new(mem_ctx) ir_dereference_array(subject, idx);
916 } else if (MATCH(expr, record_pat)) {
917 ir_rvalue *subject = read_rvalue(s_subject);
918 if (subject == NULL) {
919 ir_read_error(NULL, "when reading the subject of a record_ref");
920 return NULL;
921 }
922 return new(mem_ctx) ir_dereference_record(subject, s_field->value());
923 }
924 return NULL;
925 }
926
927 ir_texture *
928 ir_reader::read_texture(s_expression *expr)
929 {
930 s_symbol *tag = NULL;
931 s_expression *s_type = NULL;
932 s_expression *s_sampler = NULL;
933 s_expression *s_coord = NULL;
934 s_expression *s_offset = NULL;
935 s_expression *s_proj = NULL;
936 s_list *s_shadow = NULL;
937 s_expression *s_lod = NULL;
938 s_expression *s_sample_index = NULL;
939 s_expression *s_component = NULL;
940
941 ir_texture_opcode op = ir_tex; /* silence warning */
942
943 s_pattern tex_pattern[] =
944 { "tex", s_type, s_sampler, s_coord, s_offset, s_proj, s_shadow };
945 s_pattern lod_pattern[] =
946 { "lod", s_type, s_sampler, s_coord };
947 s_pattern txf_pattern[] =
948 { "txf", s_type, s_sampler, s_coord, s_offset, s_lod };
949 s_pattern txf_ms_pattern[] =
950 { "txf_ms", s_type, s_sampler, s_coord, s_sample_index };
951 s_pattern txs_pattern[] =
952 { "txs", s_type, s_sampler, s_lod };
953 s_pattern tg4_pattern[] =
954 { "tg4", s_type, s_sampler, s_coord, s_offset, s_component };
955 s_pattern query_levels_pattern[] =
956 { "query_levels", s_type, s_sampler };
957 s_pattern other_pattern[] =
958 { tag, s_type, s_sampler, s_coord, s_offset, s_proj, s_shadow, s_lod };
959
960 if (MATCH(expr, lod_pattern)) {
961 op = ir_lod;
962 } else if (MATCH(expr, tex_pattern)) {
963 op = ir_tex;
964 } else if (MATCH(expr, txf_pattern)) {
965 op = ir_txf;
966 } else if (MATCH(expr, txf_ms_pattern)) {
967 op = ir_txf_ms;
968 } else if (MATCH(expr, txs_pattern)) {
969 op = ir_txs;
970 } else if (MATCH(expr, tg4_pattern)) {
971 op = ir_tg4;
972 } else if (MATCH(expr, query_levels_pattern)) {
973 op = ir_query_levels;
974 } else if (MATCH(expr, other_pattern)) {
975 op = ir_texture::get_opcode(tag->value());
976 if (op == -1)
977 return NULL;
978 } else {
979 ir_read_error(NULL, "unexpected texture pattern %s", tag->value());
980 return NULL;
981 }
982
983 ir_texture *tex = new(mem_ctx) ir_texture(op);
984
985 // Read return type
986 const glsl_type *type = read_type(s_type);
987 if (type == NULL) {
988 ir_read_error(NULL, "when reading type in (%s ...)",
989 tex->opcode_string());
990 return NULL;
991 }
992
993 // Read sampler (must be a deref)
994 ir_dereference *sampler = read_dereference(s_sampler);
995 if (sampler == NULL) {
996 ir_read_error(NULL, "when reading sampler in (%s ...)",
997 tex->opcode_string());
998 return NULL;
999 }
1000 tex->set_sampler(sampler, type);
1001
1002 if (op != ir_txs) {
1003 // Read coordinate (any rvalue)
1004 tex->coordinate = read_rvalue(s_coord);
1005 if (tex->coordinate == NULL) {
1006 ir_read_error(NULL, "when reading coordinate in (%s ...)",
1007 tex->opcode_string());
1008 return NULL;
1009 }
1010
1011 if (op != ir_txf_ms && op != ir_lod) {
1012 // Read texel offset - either 0 or an rvalue.
1013 s_int *si_offset = SX_AS_INT(s_offset);
1014 if (si_offset == NULL || si_offset->value() != 0) {
1015 tex->offset = read_rvalue(s_offset);
1016 if (tex->offset == NULL) {
1017 ir_read_error(s_offset, "expected 0 or an expression");
1018 return NULL;
1019 }
1020 }
1021 }
1022 }
1023
1024 if (op != ir_txf && op != ir_txf_ms &&
1025 op != ir_txs && op != ir_lod && op != ir_tg4 &&
1026 op != ir_query_levels) {
1027 s_int *proj_as_int = SX_AS_INT(s_proj);
1028 if (proj_as_int && proj_as_int->value() == 1) {
1029 tex->projector = NULL;
1030 } else {
1031 tex->projector = read_rvalue(s_proj);
1032 if (tex->projector == NULL) {
1033 ir_read_error(NULL, "when reading projective divide in (%s ..)",
1034 tex->opcode_string());
1035 return NULL;
1036 }
1037 }
1038
1039 if (s_shadow->subexpressions.is_empty()) {
1040 tex->shadow_comparitor = NULL;
1041 } else {
1042 tex->shadow_comparitor = read_rvalue(s_shadow);
1043 if (tex->shadow_comparitor == NULL) {
1044 ir_read_error(NULL, "when reading shadow comparitor in (%s ..)",
1045 tex->opcode_string());
1046 return NULL;
1047 }
1048 }
1049 }
1050
1051 switch (op) {
1052 case ir_txb:
1053 tex->lod_info.bias = read_rvalue(s_lod);
1054 if (tex->lod_info.bias == NULL) {
1055 ir_read_error(NULL, "when reading LOD bias in (txb ...)");
1056 return NULL;
1057 }
1058 break;
1059 case ir_txl:
1060 case ir_txf:
1061 case ir_txs:
1062 tex->lod_info.lod = read_rvalue(s_lod);
1063 if (tex->lod_info.lod == NULL) {
1064 ir_read_error(NULL, "when reading LOD in (%s ...)",
1065 tex->opcode_string());
1066 return NULL;
1067 }
1068 break;
1069 case ir_txf_ms:
1070 tex->lod_info.sample_index = read_rvalue(s_sample_index);
1071 if (tex->lod_info.sample_index == NULL) {
1072 ir_read_error(NULL, "when reading sample_index in (txf_ms ...)");
1073 return NULL;
1074 }
1075 break;
1076 case ir_txd: {
1077 s_expression *s_dx, *s_dy;
1078 s_pattern dxdy_pat[] = { s_dx, s_dy };
1079 if (!MATCH(s_lod, dxdy_pat)) {
1080 ir_read_error(s_lod, "expected (dPdx dPdy) in (txd ...)");
1081 return NULL;
1082 }
1083 tex->lod_info.grad.dPdx = read_rvalue(s_dx);
1084 if (tex->lod_info.grad.dPdx == NULL) {
1085 ir_read_error(NULL, "when reading dPdx in (txd ...)");
1086 return NULL;
1087 }
1088 tex->lod_info.grad.dPdy = read_rvalue(s_dy);
1089 if (tex->lod_info.grad.dPdy == NULL) {
1090 ir_read_error(NULL, "when reading dPdy in (txd ...)");
1091 return NULL;
1092 }
1093 break;
1094 }
1095 case ir_tg4:
1096 tex->lod_info.component = read_rvalue(s_component);
1097 if (tex->lod_info.component == NULL) {
1098 ir_read_error(NULL, "when reading component in (tg4 ...)");
1099 return NULL;
1100 }
1101 break;
1102 default:
1103 // tex and lod don't have any extra parameters.
1104 break;
1105 };
1106 return tex;
1107 }
1108
1109 ir_emit_vertex *
1110 ir_reader::read_emit_vertex(s_expression *expr)
1111 {
1112 s_pattern pat[] = { "emit-vertex" };
1113
1114 if (MATCH(expr, pat)) {
1115 return new(mem_ctx) ir_emit_vertex();
1116 }
1117 ir_read_error(NULL, "when reading emit-vertex");
1118 return NULL;
1119 }
1120
1121 ir_end_primitive *
1122 ir_reader::read_end_primitive(s_expression *expr)
1123 {
1124 s_pattern pat[] = { "end-primitive" };
1125
1126 if (MATCH(expr, pat)) {
1127 return new(mem_ctx) ir_end_primitive();
1128 }
1129 ir_read_error(NULL, "when reading end-primitive");
1130 return NULL;
1131 }