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