2 * Copyright © 2010 Intel Corporation
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:
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
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.
24 #include "ir_reader.h"
25 #include "glsl_parser_extras.h"
26 #include "glsl_types.h"
27 #include "s_expression.h"
29 const static bool debug = false;
33 ir_reader(_mesa_glsl_parse_state *);
35 void read(exec_list *instructions, const char *src, bool scan_for_protos);
39 _mesa_glsl_parse_state *state;
41 void ir_read_error(s_expression *, const char *fmt, ...);
43 const glsl_type *read_type(s_expression *);
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);
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 *);
63 ir_dereference *read_dereference(s_expression *);
64 ir_dereference_variable *read_var_ref(s_expression *);
67 ir_reader::ir_reader(_mesa_glsl_parse_state *state) : state(state)
69 this->mem_ctx = state;
73 _mesa_glsl_read_ir(_mesa_glsl_parse_state *state, exec_list *instructions,
74 const char *src, bool scan_for_protos)
77 r.read(instructions, src, scan_for_protos);
81 ir_reader::read(exec_list *instructions, const char *src, bool scan_for_protos)
83 void *sx_mem_ctx = ralloc_context(NULL);
84 s_expression *expr = s_expression::read_expression(sx_mem_ctx, src);
86 ir_read_error(NULL, "couldn't parse S-Expression.");
90 if (scan_for_protos) {
91 scan_for_prototypes(instructions, expr);
96 read_instructions(instructions, expr, NULL);
97 ralloc_free(sx_mem_ctx);
100 validate_ir_tree(instructions);
104 ir_reader::ir_read_error(s_expression *expr, const char *fmt, ...)
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: ");
116 ralloc_vasprintf_append(&state->info_log, fmt, ap);
118 ralloc_strcat(&state->info_log, "\n");
121 ralloc_strcat(&state->info_log, "...in this context:\n ");
123 ralloc_strcat(&state->info_log, "\n\n");
128 ir_reader::read_type(s_expression *expr)
130 s_expression *s_base_type;
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");
141 return glsl_type::get_array_instance(base_type, s_size->value());
144 s_symbol *type_sym = SX_AS_SYMBOL(expr);
145 if (type_sym == NULL) {
146 ir_read_error(expr, "expected <type>");
150 const glsl_type *type = state->symbols->get_type(type_sym->value());
152 ir_read_error(expr, "invalid type: %s", type_sym->value());
159 ir_reader::scan_for_prototypes(exec_list *instructions, s_expression *expr)
161 s_list *list = SX_AS_LIST(expr);
163 ir_read_error(expr, "Expected (<instruction> ...); found an atom.");
167 foreach_iter(exec_list_iterator, it, list->subexpressions) {
168 s_list *sub = SX_AS_LIST(it.get());
170 continue; // not a (function ...); ignore it.
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.
176 ir_function *f = read_function(sub, true);
179 instructions->push_tail(f);
184 ir_reader::read_function(s_expression *expr, bool skip_body)
189 s_pattern pat[] = { "function", name };
190 if (!PARTIAL_MATCH(expr, pat)) {
191 ir_read_error(expr, "Expected (function <name> (signature ...) ...)");
195 ir_function *f = state->symbols->get_function(name->value());
197 f = new(mem_ctx) ir_function(name->value());
198 added = state->symbols->add_function(f);
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);
209 return added ? f : NULL;
213 ir_reader::read_function_sig(ir_function *f, s_expression *expr, bool skip_body)
215 s_expression *type_expr;
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> ...))");
226 const glsl_type *return_type = read_type(type_expr);
227 if (return_type == NULL)
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 ...)");
236 // Read the parameters list into a temporary place.
237 exec_list hir_parameters;
238 state->symbols->push_scope();
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());
246 hir_parameters.push_tail(var);
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);
263 if (sig->return_type != return_type) {
264 ir_read_error(expr, "function `%s' return type doesn't "
265 "match prototype", f->name);
269 /* No prototype for this body exists - skip it. */
270 state->symbols->pop_scope();
275 sig->replace_parameters(&hir_parameters);
277 if (!skip_body && !body_list->subexpressions.is_empty()) {
278 if (sig->is_defined) {
279 ir_read_error(expr, "function %s redefined", f->name);
282 state->current_function = sig;
283 read_instructions(&sig->body, body_list, NULL);
284 state->current_function = NULL;
285 sig->is_defined = true;
288 state->symbols->pop_scope();
292 ir_reader::read_instructions(exec_list *instructions, s_expression *expr,
295 // Read in a list of instructions
296 s_list *list = SX_AS_LIST(expr);
298 ir_read_error(expr, "Expected (<instruction> ...); found an atom.");
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);
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.
311 if (state->current_function == NULL && ir->as_variable() != NULL)
312 instructions->push_head(ir);
314 instructions->push_tail(ir);
321 ir_reader::read_instruction(s_expression *expr, ir_loop *loop_ctx)
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);
331 s_list *list = SX_AS_LIST(expr);
332 if (list == NULL || list->subexpressions.is_empty()) {
333 ir_read_error(expr, "Invalid instruction.\n");
337 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
339 ir_read_error(expr, "expected instruction tag");
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);
359 inst = read_rvalue(list);
361 ir_read_error(NULL, "when reading instruction");
367 ir_reader::read_declaration(s_expression *expr)
370 s_expression *s_type;
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>)");
379 const glsl_type *type = read_type(s_type);
383 ir_variable *var = new(mem_ctx) ir_variable(type, s_name->value(),
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");
393 // FINISHME: Check for duplicate/conflicting qualifiers.
394 if (strcmp(qualifier->value(), "centroid") == 0) {
396 } else if (strcmp(qualifier->value(), "invariant") == 0) {
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_in;
404 } else if (strcmp(qualifier->value(), "const_in") == 0) {
405 var->mode = ir_var_const_in;
406 } else if (strcmp(qualifier->value(), "out") == 0) {
407 var->mode = ir_var_out;
408 } else if (strcmp(qualifier->value(), "inout") == 0) {
409 var->mode = ir_var_inout;
410 } else if (strcmp(qualifier->value(), "temporary") == 0) {
411 var->mode = ir_var_temporary;
412 } else if (strcmp(qualifier->value(), "smooth") == 0) {
413 var->interpolation = INTERP_QUALIFIER_SMOOTH;
414 } else if (strcmp(qualifier->value(), "flat") == 0) {
415 var->interpolation = INTERP_QUALIFIER_FLAT;
416 } else if (strcmp(qualifier->value(), "noperspective") == 0) {
417 var->interpolation = INTERP_QUALIFIER_NOPERSPECTIVE;
419 ir_read_error(expr, "unknown qualifier: %s", qualifier->value());
424 // Add the variable to the symbol table
425 state->symbols->add_variable(var);
432 ir_reader::read_if(s_expression *expr, ir_loop *loop_ctx)
434 s_expression *s_cond;
435 s_expression *s_then;
436 s_expression *s_else;
438 s_pattern pat[] = { "if", s_cond, s_then, s_else };
439 if (!MATCH(expr, pat)) {
440 ir_read_error(expr, "expected (if <condition> (<then>...) (<else>...))");
444 ir_rvalue *condition = read_rvalue(s_cond);
445 if (condition == NULL) {
446 ir_read_error(NULL, "when reading condition of (if ...)");
450 ir_if *iff = new(mem_ctx) ir_if(condition);
452 read_instructions(&iff->then_instructions, s_then, loop_ctx);
453 read_instructions(&iff->else_instructions, s_else, loop_ctx);
463 ir_reader::read_loop(s_expression *expr)
465 s_expression *s_counter, *s_from, *s_to, *s_inc, *s_body;
467 s_pattern pat[] = { "loop", s_counter, s_from, s_to, s_inc, s_body };
468 if (!MATCH(expr, pat)) {
469 ir_read_error(expr, "expected (loop <counter> <from> <to> "
470 "<increment> <body>)");
474 // FINISHME: actually read the count/from/to fields.
476 ir_loop *loop = new(mem_ctx) ir_loop;
477 read_instructions(&loop->body_instructions, s_body, loop);
487 ir_reader::read_return(s_expression *expr)
489 s_expression *s_retval;
491 s_pattern return_value_pat[] = { "return", s_retval};
492 s_pattern return_void_pat[] = { "return" };
493 if (MATCH(expr, return_value_pat)) {
494 ir_rvalue *retval = read_rvalue(s_retval);
495 if (retval == NULL) {
496 ir_read_error(NULL, "when reading return value");
499 return new(mem_ctx) ir_return(retval);
500 } else if (MATCH(expr, return_void_pat)) {
501 return new(mem_ctx) ir_return;
503 ir_read_error(expr, "expected (return <rvalue>) or (return)");
510 ir_reader::read_rvalue(s_expression *expr)
512 s_list *list = SX_AS_LIST(expr);
513 if (list == NULL || list->subexpressions.is_empty())
516 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
518 ir_read_error(expr, "expected rvalue tag");
522 ir_rvalue *rvalue = read_dereference(list);
523 if (rvalue != NULL || state->error)
525 else if (strcmp(tag->value(), "swiz") == 0) {
526 rvalue = read_swizzle(list);
527 } else if (strcmp(tag->value(), "expression") == 0) {
528 rvalue = read_expression(list);
529 } else if (strcmp(tag->value(), "constant") == 0) {
530 rvalue = read_constant(list);
532 rvalue = read_texture(list);
533 if (rvalue == NULL && !state->error)
534 ir_read_error(expr, "unrecognized rvalue tag: %s", tag->value());
541 ir_reader::read_assignment(s_expression *expr)
543 s_expression *cond_expr = NULL;
544 s_expression *lhs_expr, *rhs_expr;
547 s_pattern pat4[] = { "assign", mask_list, lhs_expr, rhs_expr };
548 s_pattern pat5[] = { "assign", cond_expr, mask_list, lhs_expr, rhs_expr };
549 if (!MATCH(expr, pat4) && !MATCH(expr, pat5)) {
550 ir_read_error(expr, "expected (assign [<condition>] (<write mask>) "
555 ir_rvalue *condition = NULL;
556 if (cond_expr != NULL) {
557 condition = read_rvalue(cond_expr);
558 if (condition == NULL) {
559 ir_read_error(NULL, "when reading condition of assignment");
566 s_symbol *mask_symbol;
567 s_pattern mask_pat[] = { mask_symbol };
568 if (MATCH(mask_list, mask_pat)) {
569 const char *mask_str = mask_symbol->value();
570 unsigned mask_length = strlen(mask_str);
571 if (mask_length > 4) {
572 ir_read_error(expr, "invalid write mask: %s", mask_str);
576 const unsigned idx_map[] = { 3, 0, 1, 2 }; /* w=bit 3, x=0, y=1, z=2 */
578 for (unsigned i = 0; i < mask_length; i++) {
579 if (mask_str[i] < 'w' || mask_str[i] > 'z') {
580 ir_read_error(expr, "write mask contains invalid character: %c",
584 mask |= 1 << idx_map[mask_str[i] - 'w'];
586 } else if (!mask_list->subexpressions.is_empty()) {
587 ir_read_error(mask_list, "expected () or (<write mask>)");
591 ir_dereference *lhs = read_dereference(lhs_expr);
593 ir_read_error(NULL, "when reading left-hand side of assignment");
597 ir_rvalue *rhs = read_rvalue(rhs_expr);
599 ir_read_error(NULL, "when reading right-hand side of assignment");
603 if (mask == 0 && (lhs->type->is_vector() || lhs->type->is_scalar())) {
604 ir_read_error(expr, "non-zero write mask required.");
608 return new(mem_ctx) ir_assignment(lhs, rhs, condition, mask);
612 ir_reader::read_call(s_expression *expr)
616 s_list *s_return = NULL;
618 ir_dereference_variable *return_deref = NULL;
620 s_pattern void_pat[] = { "call", name, params };
621 s_pattern non_void_pat[] = { "call", name, s_return, params };
622 if (MATCH(expr, non_void_pat)) {
623 return_deref = read_var_ref(s_return);
624 if (return_deref == NULL) {
625 ir_read_error(s_return, "when reading a call's return storage");
628 } else if (!MATCH(expr, void_pat)) {
629 ir_read_error(expr, "expected (call <name> [<deref>] (<param> ...))");
633 exec_list parameters;
635 foreach_iter(exec_list_iterator, it, params->subexpressions) {
636 s_expression *expr = (s_expression*) it.get();
637 ir_rvalue *param = read_rvalue(expr);
639 ir_read_error(expr, "when reading parameter to function call");
642 parameters.push_tail(param);
645 ir_function *f = state->symbols->get_function(name->value());
647 ir_read_error(expr, "found call to undefined function %s",
652 ir_function_signature *callee = f->matching_signature(¶meters);
653 if (callee == NULL) {
654 ir_read_error(expr, "couldn't find matching signature for function "
655 "%s", name->value());
659 if (callee->return_type == glsl_type::void_type && return_deref) {
660 ir_read_error(expr, "call has return value storage but void type");
662 } else if (callee->return_type != glsl_type::void_type && !return_deref) {
663 ir_read_error(expr, "call has non-void type but no return value storage");
667 return new(mem_ctx) ir_call(callee, return_deref, ¶meters);
671 ir_reader::read_expression(s_expression *expr)
673 s_expression *s_type;
675 s_expression *s_arg1;
677 s_pattern pat[] = { "expression", s_type, s_op, s_arg1 };
678 if (!PARTIAL_MATCH(expr, pat)) {
679 ir_read_error(expr, "expected (expression <type> <operator> "
680 "<operand> [<operand>])");
683 s_expression *s_arg2 = (s_expression *) s_arg1->next; // may be tail sentinel
685 const glsl_type *type = read_type(s_type);
689 /* Read the operator */
690 ir_expression_operation op = ir_expression::get_operator(s_op->value());
691 if (op == (ir_expression_operation) -1) {
692 ir_read_error(expr, "invalid operator: %s", s_op->value());
696 unsigned num_operands = ir_expression::get_num_operands(op);
697 if (num_operands == 1 && !s_arg1->next->is_tail_sentinel()) {
698 ir_read_error(expr, "expected (expression <type> %s <operand>)",
703 ir_rvalue *arg1 = read_rvalue(s_arg1);
704 ir_rvalue *arg2 = NULL;
706 ir_read_error(NULL, "when reading first operand of %s", s_op->value());
710 if (num_operands == 2) {
711 if (s_arg2->is_tail_sentinel() || !s_arg2->next->is_tail_sentinel()) {
712 ir_read_error(expr, "expected (expression <type> %s <operand> "
713 "<operand>)", s_op->value());
716 arg2 = read_rvalue(s_arg2);
718 ir_read_error(NULL, "when reading second operand of %s",
724 return new(mem_ctx) ir_expression(op, type, arg1, arg2);
728 ir_reader::read_swizzle(s_expression *expr)
733 s_pattern pat[] = { "swiz", swiz, sub };
734 if (!MATCH(expr, pat)) {
735 ir_read_error(expr, "expected (swiz <swizzle> <rvalue>)");
739 if (strlen(swiz->value()) > 4) {
740 ir_read_error(expr, "expected a valid swizzle; found %s", swiz->value());
744 ir_rvalue *rvalue = read_rvalue(sub);
748 ir_swizzle *ir = ir_swizzle::create(rvalue, swiz->value(),
749 rvalue->type->vector_elements);
751 ir_read_error(expr, "invalid swizzle");
757 ir_reader::read_constant(s_expression *expr)
759 s_expression *type_expr;
762 s_pattern pat[] = { "constant", type_expr, values };
763 if (!MATCH(expr, pat)) {
764 ir_read_error(expr, "expected (constant <type> (...))");
768 const glsl_type *type = read_type(type_expr);
772 if (values == NULL) {
773 ir_read_error(expr, "expected (constant <type> (...))");
777 if (type->is_array()) {
778 unsigned elements_supplied = 0;
780 foreach_iter(exec_list_iterator, it, values->subexpressions) {
781 s_expression *elt = (s_expression *) it.get();
782 ir_constant *ir_elt = read_constant(elt);
785 elements.push_tail(ir_elt);
789 if (elements_supplied != type->length) {
790 ir_read_error(values, "expected exactly %u array elements, "
791 "given %u", type->length, elements_supplied);
794 return new(mem_ctx) ir_constant(type, &elements);
797 ir_constant_data data = { { 0 } };
799 // Read in list of values (at most 16).
801 foreach_iter(exec_list_iterator, it, values->subexpressions) {
803 ir_read_error(values, "expected at most 16 numbers");
807 s_expression *expr = (s_expression*) it.get();
809 if (type->base_type == GLSL_TYPE_FLOAT) {
810 s_number *value = SX_AS_NUMBER(expr);
812 ir_read_error(values, "expected numbers");
815 data.f[k] = value->fvalue();
817 s_int *value = SX_AS_INT(expr);
819 ir_read_error(values, "expected integers");
823 switch (type->base_type) {
824 case GLSL_TYPE_UINT: {
825 data.u[k] = value->value();
828 case GLSL_TYPE_INT: {
829 data.i[k] = value->value();
832 case GLSL_TYPE_BOOL: {
833 data.b[k] = value->value();
837 ir_read_error(values, "unsupported constant type");
843 if (k != type->components()) {
844 ir_read_error(values, "expected %u constant values, found %u",
845 type->components(), k);
849 return new(mem_ctx) ir_constant(type, &data);
852 ir_dereference_variable *
853 ir_reader::read_var_ref(s_expression *expr)
856 s_pattern var_pat[] = { "var_ref", s_var };
858 if (MATCH(expr, var_pat)) {
859 ir_variable *var = state->symbols->get_variable(s_var->value());
861 ir_read_error(expr, "undeclared variable: %s", s_var->value());
864 return new(mem_ctx) ir_dereference_variable(var);
870 ir_reader::read_dereference(s_expression *expr)
872 s_expression *s_subject;
873 s_expression *s_index;
876 s_pattern array_pat[] = { "array_ref", s_subject, s_index };
877 s_pattern record_pat[] = { "record_ref", s_subject, s_field };
879 ir_dereference_variable *var_ref = read_var_ref(expr);
880 if (var_ref != NULL) {
882 } else if (MATCH(expr, array_pat)) {
883 ir_rvalue *subject = read_rvalue(s_subject);
884 if (subject == NULL) {
885 ir_read_error(NULL, "when reading the subject of an array_ref");
889 ir_rvalue *idx = read_rvalue(s_index);
890 if (subject == NULL) {
891 ir_read_error(NULL, "when reading the index of an array_ref");
894 return new(mem_ctx) ir_dereference_array(subject, idx);
895 } else if (MATCH(expr, record_pat)) {
896 ir_rvalue *subject = read_rvalue(s_subject);
897 if (subject == NULL) {
898 ir_read_error(NULL, "when reading the subject of a record_ref");
901 return new(mem_ctx) ir_dereference_record(subject, s_field->value());
907 ir_reader::read_texture(s_expression *expr)
909 s_symbol *tag = NULL;
910 s_expression *s_type = NULL;
911 s_expression *s_sampler = NULL;
912 s_expression *s_coord = NULL;
913 s_expression *s_offset = NULL;
914 s_expression *s_proj = NULL;
915 s_list *s_shadow = NULL;
916 s_expression *s_lod = NULL;
918 ir_texture_opcode op = ir_tex; /* silence warning */
920 s_pattern tex_pattern[] =
921 { "tex", s_type, s_sampler, s_coord, s_offset, s_proj, s_shadow };
922 s_pattern txf_pattern[] =
923 { "txf", s_type, s_sampler, s_coord, s_offset, s_lod };
924 s_pattern txs_pattern[] =
925 { "txs", s_type, s_sampler, s_lod };
926 s_pattern other_pattern[] =
927 { tag, s_type, s_sampler, s_coord, s_offset, s_proj, s_shadow, s_lod };
929 if (MATCH(expr, tex_pattern)) {
931 } else if (MATCH(expr, txf_pattern)) {
933 } else if (MATCH(expr, txs_pattern)) {
935 } else if (MATCH(expr, other_pattern)) {
936 op = ir_texture::get_opcode(tag->value());
940 ir_read_error(NULL, "unexpected texture pattern");
944 ir_texture *tex = new(mem_ctx) ir_texture(op);
947 const glsl_type *type = read_type(s_type);
949 ir_read_error(NULL, "when reading type in (%s ...)",
950 tex->opcode_string());
954 // Read sampler (must be a deref)
955 ir_dereference *sampler = read_dereference(s_sampler);
956 if (sampler == NULL) {
957 ir_read_error(NULL, "when reading sampler in (%s ...)",
958 tex->opcode_string());
961 tex->set_sampler(sampler, type);
964 // Read coordinate (any rvalue)
965 tex->coordinate = read_rvalue(s_coord);
966 if (tex->coordinate == NULL) {
967 ir_read_error(NULL, "when reading coordinate in (%s ...)",
968 tex->opcode_string());
972 // Read texel offset - either 0 or an rvalue.
973 s_int *si_offset = SX_AS_INT(s_offset);
974 if (si_offset == NULL || si_offset->value() != 0) {
975 tex->offset = read_rvalue(s_offset);
976 if (tex->offset == NULL) {
977 ir_read_error(s_offset, "expected 0 or an expression");
983 if (op != ir_txf && op != ir_txs) {
984 s_int *proj_as_int = SX_AS_INT(s_proj);
985 if (proj_as_int && proj_as_int->value() == 1) {
986 tex->projector = NULL;
988 tex->projector = read_rvalue(s_proj);
989 if (tex->projector == NULL) {
990 ir_read_error(NULL, "when reading projective divide in (%s ..)",
991 tex->opcode_string());
996 if (s_shadow->subexpressions.is_empty()) {
997 tex->shadow_comparitor = NULL;
999 tex->shadow_comparitor = read_rvalue(s_shadow);
1000 if (tex->shadow_comparitor == NULL) {
1001 ir_read_error(NULL, "when reading shadow comparitor in (%s ..)",
1002 tex->opcode_string());
1010 tex->lod_info.bias = read_rvalue(s_lod);
1011 if (tex->lod_info.bias == NULL) {
1012 ir_read_error(NULL, "when reading LOD bias in (txb ...)");
1019 tex->lod_info.lod = read_rvalue(s_lod);
1020 if (tex->lod_info.lod == NULL) {
1021 ir_read_error(NULL, "when reading LOD in (%s ...)",
1022 tex->opcode_string());
1027 s_expression *s_dx, *s_dy;
1028 s_pattern dxdy_pat[] = { s_dx, s_dy };
1029 if (!MATCH(s_lod, dxdy_pat)) {
1030 ir_read_error(s_lod, "expected (dPdx dPdy) in (txd ...)");
1033 tex->lod_info.grad.dPdx = read_rvalue(s_dx);
1034 if (tex->lod_info.grad.dPdx == NULL) {
1035 ir_read_error(NULL, "when reading dPdx in (txd ...)");
1038 tex->lod_info.grad.dPdy = read_rvalue(s_dy);
1039 if (tex->lod_info.grad.dPdy == NULL) {
1040 ir_read_error(NULL, "when reading dPdy in (txd ...)");
1046 // tex doesn't have any extra parameters.