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