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