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