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