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_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
*);
63 ir_dereference
*read_dereference(s_expression
*);
66 ir_reader::ir_reader(_mesa_glsl_parse_state
*state
) : state(state
)
68 this->mem_ctx
= state
;
72 _mesa_glsl_read_ir(_mesa_glsl_parse_state
*state
, exec_list
*instructions
,
73 const char *src
, bool scan_for_protos
)
76 r
.read(instructions
, src
, scan_for_protos
);
80 ir_reader::read(exec_list
*instructions
, const char *src
, bool scan_for_protos
)
82 s_expression
*expr
= s_expression::read_expression(mem_ctx
, src
);
84 ir_read_error(NULL
, "couldn't parse S-Expression.");
88 if (scan_for_protos
) {
89 scan_for_prototypes(instructions
, expr
);
94 read_instructions(instructions
, expr
, NULL
);
98 validate_ir_tree(instructions
);
102 ir_reader::ir_read_error(s_expression
*expr
, const char *fmt
, ...)
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: ");
114 ralloc_vasprintf_append(&state
->info_log
, fmt
, ap
);
116 ralloc_strcat(&state
->info_log
, "\n");
119 ralloc_strcat(&state
->info_log
, "...in this context:\n ");
121 ralloc_strcat(&state
->info_log
, "\n\n");
126 ir_reader::read_type(s_expression
*expr
)
128 s_expression
*s_base_type
;
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");
139 return glsl_type::get_array_instance(base_type
, s_size
->value());
142 s_symbol
*type_sym
= SX_AS_SYMBOL(expr
);
143 if (type_sym
== NULL
) {
144 ir_read_error(expr
, "expected <type>");
148 const glsl_type
*type
= state
->symbols
->get_type(type_sym
->value());
150 ir_read_error(expr
, "invalid type: %s", type_sym
->value());
157 ir_reader::scan_for_prototypes(exec_list
*instructions
, s_expression
*expr
)
159 s_list
*list
= SX_AS_LIST(expr
);
161 ir_read_error(expr
, "Expected (<instruction> ...); found an atom.");
165 foreach_iter(exec_list_iterator
, it
, list
->subexpressions
) {
166 s_list
*sub
= SX_AS_LIST(it
.get());
168 continue; // not a (function ...); ignore it.
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.
174 ir_function
*f
= read_function(sub
, true);
177 instructions
->push_tail(f
);
182 ir_reader::read_function(s_expression
*expr
, bool skip_body
)
187 s_pattern pat
[] = { "function", name
};
188 if (!PARTIAL_MATCH(expr
, pat
)) {
189 ir_read_error(expr
, "Expected (function <name> (signature ...) ...)");
193 ir_function
*f
= state
->symbols
->get_function(name
->value());
195 f
= new(mem_ctx
) ir_function(name
->value());
196 added
= state
->symbols
->add_function(f
);
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
);
207 return added
? f
: NULL
;
211 ir_reader::read_function_sig(ir_function
*f
, s_expression
*expr
, bool skip_body
)
213 s_expression
*type_expr
;
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> ...))");
224 const glsl_type
*return_type
= read_type(type_expr
);
225 if (return_type
== NULL
)
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 ...)");
234 // Read the parameters list into a temporary place.
235 exec_list hir_parameters
;
236 state
->symbols
->push_scope();
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());
244 hir_parameters
.push_tail(var
);
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
);
261 if (sig
->return_type
!= return_type
) {
262 ir_read_error(expr
, "function `%s' return type doesn't "
263 "match prototype", f
->name
);
267 /* No prototype for this body exists - skip it. */
268 state
->symbols
->pop_scope();
273 sig
->replace_parameters(&hir_parameters
);
275 if (!skip_body
&& !body_list
->subexpressions
.is_empty()) {
276 if (sig
->is_defined
) {
277 ir_read_error(expr
, "function %s redefined", f
->name
);
280 state
->current_function
= sig
;
281 read_instructions(&sig
->body
, body_list
, NULL
);
282 state
->current_function
= NULL
;
283 sig
->is_defined
= true;
286 state
->symbols
->pop_scope();
290 ir_reader::read_instructions(exec_list
*instructions
, s_expression
*expr
,
293 // Read in a list of instructions
294 s_list
*list
= SX_AS_LIST(expr
);
296 ir_read_error(expr
, "Expected (<instruction> ...); found an atom.");
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
);
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.
309 if (state
->current_function
== NULL
&& ir
->as_variable() != NULL
)
310 instructions
->push_head(ir
);
312 instructions
->push_tail(ir
);
319 ir_reader::read_instruction(s_expression
*expr
, ir_loop
*loop_ctx
)
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
);
329 s_list
*list
= SX_AS_LIST(expr
);
330 if (list
== NULL
|| list
->subexpressions
.is_empty()) {
331 ir_read_error(expr
, "Invalid instruction.\n");
335 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
337 ir_read_error(expr
, "expected instruction tag");
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);
355 inst
= read_rvalue(list
);
357 ir_read_error(NULL
, "when reading instruction");
363 ir_reader::read_declaration(s_expression
*expr
)
366 s_expression
*s_type
;
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>)");
375 const glsl_type
*type
= read_type(s_type
);
379 ir_variable
*var
= new(mem_ctx
) ir_variable(type
, s_name
->value(),
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");
389 // FINISHME: Check for duplicate/conflicting qualifiers.
390 if (strcmp(qualifier
->value(), "centroid") == 0) {
392 } else if (strcmp(qualifier
->value(), "invariant") == 0) {
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(), "out") == 0) {
401 var
->mode
= ir_var_out
;
402 } else if (strcmp(qualifier
->value(), "inout") == 0) {
403 var
->mode
= ir_var_inout
;
404 } else if (strcmp(qualifier
->value(), "smooth") == 0) {
405 var
->interpolation
= ir_var_smooth
;
406 } else if (strcmp(qualifier
->value(), "flat") == 0) {
407 var
->interpolation
= ir_var_flat
;
408 } else if (strcmp(qualifier
->value(), "noperspective") == 0) {
409 var
->interpolation
= ir_var_noperspective
;
411 ir_read_error(expr
, "unknown qualifier: %s", qualifier
->value());
416 // Add the variable to the symbol table
417 state
->symbols
->add_variable(var
);
424 ir_reader::read_if(s_expression
*expr
, ir_loop
*loop_ctx
)
426 s_expression
*s_cond
;
427 s_expression
*s_then
;
428 s_expression
*s_else
;
430 s_pattern pat
[] = { "if", s_cond
, s_then
, s_else
};
431 if (!MATCH(expr
, pat
)) {
432 ir_read_error(expr
, "expected (if <condition> (<then>...) (<else>...))");
436 ir_rvalue
*condition
= read_rvalue(s_cond
);
437 if (condition
== NULL
) {
438 ir_read_error(NULL
, "when reading condition of (if ...)");
442 ir_if
*iff
= new(mem_ctx
) ir_if(condition
);
444 read_instructions(&iff
->then_instructions
, s_then
, loop_ctx
);
445 read_instructions(&iff
->else_instructions
, s_else
, loop_ctx
);
455 ir_reader::read_loop(s_expression
*expr
)
457 s_expression
*s_counter
, *s_from
, *s_to
, *s_inc
, *s_body
;
459 s_pattern pat
[] = { "loop", s_counter
, s_from
, s_to
, s_inc
, s_body
};
460 if (!MATCH(expr
, pat
)) {
461 ir_read_error(expr
, "expected (loop <counter> <from> <to> "
462 "<increment> <body>)");
466 // FINISHME: actually read the count/from/to fields.
468 ir_loop
*loop
= new(mem_ctx
) ir_loop
;
469 read_instructions(&loop
->body_instructions
, s_body
, loop
);
479 ir_reader::read_return(s_expression
*expr
)
481 s_expression
*s_retval
;
483 s_pattern pat
[] = { "return", s_retval
};
484 if (!MATCH(expr
, pat
)) {
485 ir_read_error(expr
, "expected (return <rvalue>)");
489 ir_rvalue
*retval
= read_rvalue(s_retval
);
490 if (retval
== NULL
) {
491 ir_read_error(NULL
, "when reading return value");
495 return new(mem_ctx
) ir_return(retval
);
500 ir_reader::read_rvalue(s_expression
*expr
)
502 s_list
*list
= SX_AS_LIST(expr
);
503 if (list
== NULL
|| list
->subexpressions
.is_empty())
506 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
508 ir_read_error(expr
, "expected rvalue tag");
512 ir_rvalue
*rvalue
= read_dereference(list
);
513 if (rvalue
!= NULL
|| state
->error
)
515 else if (strcmp(tag
->value(), "swiz") == 0) {
516 rvalue
= read_swizzle(list
);
517 } else if (strcmp(tag
->value(), "expression") == 0) {
518 rvalue
= read_expression(list
);
519 } else if (strcmp(tag
->value(), "call") == 0) {
520 rvalue
= read_call(list
);
521 } else if (strcmp(tag
->value(), "constant") == 0) {
522 rvalue
= read_constant(list
);
524 rvalue
= read_texture(list
);
525 if (rvalue
== NULL
&& !state
->error
)
526 ir_read_error(expr
, "unrecognized rvalue tag: %s", tag
->value());
533 ir_reader::read_assignment(s_expression
*expr
)
535 s_expression
*cond_expr
= NULL
;
536 s_expression
*lhs_expr
, *rhs_expr
;
539 s_pattern pat4
[] = { "assign", mask_list
, lhs_expr
, rhs_expr
};
540 s_pattern pat5
[] = { "assign", cond_expr
, mask_list
, lhs_expr
, rhs_expr
};
541 if (!MATCH(expr
, pat4
) && !MATCH(expr
, pat5
)) {
542 ir_read_error(expr
, "expected (assign [<condition>] (<write mask>) "
547 ir_rvalue
*condition
= NULL
;
548 if (cond_expr
!= NULL
) {
549 condition
= read_rvalue(cond_expr
);
550 if (condition
== NULL
) {
551 ir_read_error(NULL
, "when reading condition of assignment");
558 s_symbol
*mask_symbol
;
559 s_pattern mask_pat
[] = { mask_symbol
};
560 if (MATCH(mask_list
, mask_pat
)) {
561 const char *mask_str
= mask_symbol
->value();
562 unsigned mask_length
= strlen(mask_str
);
563 if (mask_length
> 4) {
564 ir_read_error(expr
, "invalid write mask: %s", mask_str
);
568 const unsigned idx_map
[] = { 3, 0, 1, 2 }; /* w=bit 3, x=0, y=1, z=2 */
570 for (unsigned i
= 0; i
< mask_length
; i
++) {
571 if (mask_str
[i
] < 'w' || mask_str
[i
] > 'z') {
572 ir_read_error(expr
, "write mask contains invalid character: %c",
576 mask
|= 1 << idx_map
[mask_str
[i
] - 'w'];
578 } else if (!mask_list
->subexpressions
.is_empty()) {
579 ir_read_error(mask_list
, "expected () or (<write mask>)");
583 ir_dereference
*lhs
= read_dereference(lhs_expr
);
585 ir_read_error(NULL
, "when reading left-hand side of assignment");
589 ir_rvalue
*rhs
= read_rvalue(rhs_expr
);
591 ir_read_error(NULL
, "when reading right-hand side of assignment");
595 if (mask
== 0 && (lhs
->type
->is_vector() || lhs
->type
->is_scalar())) {
596 ir_read_error(expr
, "non-zero write mask required.");
600 return new(mem_ctx
) ir_assignment(lhs
, rhs
, condition
, mask
);
604 ir_reader::read_call(s_expression
*expr
)
609 s_pattern pat
[] = { "call", name
, params
};
610 if (!MATCH(expr
, pat
)) {
611 ir_read_error(expr
, "expected (call <name> (<param> ...))");
615 exec_list parameters
;
617 foreach_iter(exec_list_iterator
, it
, params
->subexpressions
) {
618 s_expression
*expr
= (s_expression
*) it
.get();
619 ir_rvalue
*param
= read_rvalue(expr
);
621 ir_read_error(expr
, "when reading parameter to function call");
624 parameters
.push_tail(param
);
627 ir_function
*f
= state
->symbols
->get_function(name
->value());
629 ir_read_error(expr
, "found call to undefined function %s",
634 ir_function_signature
*callee
= f
->matching_signature(¶meters
);
635 if (callee
== NULL
) {
636 ir_read_error(expr
, "couldn't find matching signature for function "
637 "%s", name
->value());
641 return new(mem_ctx
) ir_call(callee
, ¶meters
);
645 ir_reader::read_expression(s_expression
*expr
)
647 s_expression
*s_type
;
649 s_expression
*s_arg1
;
651 s_pattern pat
[] = { "expression", s_type
, s_op
, s_arg1
};
652 if (!PARTIAL_MATCH(expr
, pat
)) {
653 ir_read_error(expr
, "expected (expression <type> <operator> "
654 "<operand> [<operand>])");
657 s_expression
*s_arg2
= (s_expression
*) s_arg1
->next
; // may be tail sentinel
659 const glsl_type
*type
= read_type(s_type
);
663 /* Read the operator */
664 ir_expression_operation op
= ir_expression::get_operator(s_op
->value());
665 if (op
== (ir_expression_operation
) -1) {
666 ir_read_error(expr
, "invalid operator: %s", s_op
->value());
670 unsigned num_operands
= ir_expression::get_num_operands(op
);
671 if (num_operands
== 1 && !s_arg1
->next
->is_tail_sentinel()) {
672 ir_read_error(expr
, "expected (expression <type> %s <operand>)",
677 ir_rvalue
*arg1
= read_rvalue(s_arg1
);
678 ir_rvalue
*arg2
= NULL
;
680 ir_read_error(NULL
, "when reading first operand of %s", s_op
->value());
684 if (num_operands
== 2) {
685 if (s_arg2
->is_tail_sentinel() || !s_arg2
->next
->is_tail_sentinel()) {
686 ir_read_error(expr
, "expected (expression <type> %s <operand> "
687 "<operand>)", s_op
->value());
690 arg2
= read_rvalue(s_arg2
);
692 ir_read_error(NULL
, "when reading second operand of %s",
698 return new(mem_ctx
) ir_expression(op
, type
, arg1
, arg2
);
702 ir_reader::read_swizzle(s_expression
*expr
)
707 s_pattern pat
[] = { "swiz", swiz
, sub
};
708 if (!MATCH(expr
, pat
)) {
709 ir_read_error(expr
, "expected (swiz <swizzle> <rvalue>)");
713 if (strlen(swiz
->value()) > 4) {
714 ir_read_error(expr
, "expected a valid swizzle; found %s", swiz
->value());
718 ir_rvalue
*rvalue
= read_rvalue(sub
);
722 ir_swizzle
*ir
= ir_swizzle::create(rvalue
, swiz
->value(),
723 rvalue
->type
->vector_elements
);
725 ir_read_error(expr
, "invalid swizzle");
731 ir_reader::read_constant(s_expression
*expr
)
733 s_expression
*type_expr
;
736 s_pattern pat
[] = { "constant", type_expr
, values
};
737 if (!MATCH(expr
, pat
)) {
738 ir_read_error(expr
, "expected (constant <type> (...))");
742 const glsl_type
*type
= read_type(type_expr
);
746 if (values
== NULL
) {
747 ir_read_error(expr
, "expected (constant <type> (...))");
751 if (type
->is_array()) {
752 unsigned elements_supplied
= 0;
754 foreach_iter(exec_list_iterator
, it
, values
->subexpressions
) {
755 s_expression
*elt
= (s_expression
*) it
.get();
756 ir_constant
*ir_elt
= read_constant(elt
);
759 elements
.push_tail(ir_elt
);
763 if (elements_supplied
!= type
->length
) {
764 ir_read_error(values
, "expected exactly %u array elements, "
765 "given %u", type
->length
, elements_supplied
);
768 return new(mem_ctx
) ir_constant(type
, &elements
);
771 const glsl_type
*const base_type
= type
->get_base_type();
773 ir_constant_data data
= { { 0 } };
775 // Read in list of values (at most 16).
777 foreach_iter(exec_list_iterator
, it
, values
->subexpressions
) {
779 ir_read_error(values
, "expected at most 16 numbers");
783 s_expression
*expr
= (s_expression
*) it
.get();
785 if (base_type
->base_type
== GLSL_TYPE_FLOAT
) {
786 s_number
*value
= SX_AS_NUMBER(expr
);
788 ir_read_error(values
, "expected numbers");
791 data
.f
[k
] = value
->fvalue();
793 s_int
*value
= SX_AS_INT(expr
);
795 ir_read_error(values
, "expected integers");
799 switch (base_type
->base_type
) {
800 case GLSL_TYPE_UINT
: {
801 data
.u
[k
] = value
->value();
804 case GLSL_TYPE_INT
: {
805 data
.i
[k
] = value
->value();
808 case GLSL_TYPE_BOOL
: {
809 data
.b
[k
] = value
->value();
813 ir_read_error(values
, "unsupported constant type");
820 return new(mem_ctx
) ir_constant(type
, &data
);
824 ir_reader::read_dereference(s_expression
*expr
)
827 s_expression
*s_subject
;
828 s_expression
*s_index
;
831 s_pattern var_pat
[] = { "var_ref", s_var
};
832 s_pattern array_pat
[] = { "array_ref", s_subject
, s_index
};
833 s_pattern record_pat
[] = { "record_ref", s_subject
, s_field
};
835 if (MATCH(expr
, var_pat
)) {
836 ir_variable
*var
= state
->symbols
->get_variable(s_var
->value());
838 ir_read_error(expr
, "undeclared variable: %s", s_var
->value());
841 return new(mem_ctx
) ir_dereference_variable(var
);
842 } else if (MATCH(expr
, array_pat
)) {
843 ir_rvalue
*subject
= read_rvalue(s_subject
);
844 if (subject
== NULL
) {
845 ir_read_error(NULL
, "when reading the subject of an array_ref");
849 ir_rvalue
*idx
= read_rvalue(s_index
);
850 if (subject
== NULL
) {
851 ir_read_error(NULL
, "when reading the index of an array_ref");
854 return new(mem_ctx
) ir_dereference_array(subject
, idx
);
855 } else if (MATCH(expr
, record_pat
)) {
856 ir_rvalue
*subject
= read_rvalue(s_subject
);
857 if (subject
== NULL
) {
858 ir_read_error(NULL
, "when reading the subject of a record_ref");
861 return new(mem_ctx
) ir_dereference_record(subject
, s_field
->value());
867 ir_reader::read_texture(s_expression
*expr
)
869 s_symbol
*tag
= NULL
;
870 s_expression
*s_sampler
= NULL
;
871 s_expression
*s_coord
= NULL
;
872 s_expression
*s_offset
= NULL
;
873 s_expression
*s_proj
= NULL
;
874 s_list
*s_shadow
= NULL
;
875 s_expression
*s_lod
= NULL
;
877 ir_texture_opcode op
= ir_tex
; /* silence warning */
879 s_pattern tex_pattern
[] =
880 { "tex", s_sampler
, s_coord
, s_offset
, s_proj
, s_shadow
};
881 s_pattern txf_pattern
[] =
882 { "txf", s_sampler
, s_coord
, s_offset
, s_lod
};
883 s_pattern other_pattern
[] =
884 { tag
, s_sampler
, s_coord
, s_offset
, s_proj
, s_shadow
, s_lod
};
886 if (MATCH(expr
, tex_pattern
)) {
888 } else if (MATCH(expr
, txf_pattern
)) {
890 } else if (MATCH(expr
, other_pattern
)) {
891 op
= ir_texture::get_opcode(tag
->value());
895 ir_read_error(NULL
, "unexpected texture pattern");
899 ir_texture
*tex
= new(mem_ctx
) ir_texture(op
);
901 // Read sampler (must be a deref)
902 ir_dereference
*sampler
= read_dereference(s_sampler
);
903 if (sampler
== NULL
) {
904 ir_read_error(NULL
, "when reading sampler in (%s ...)",
905 tex
->opcode_string());
908 tex
->set_sampler(sampler
);
910 // Read coordinate (any rvalue)
911 tex
->coordinate
= read_rvalue(s_coord
);
912 if (tex
->coordinate
== NULL
) {
913 ir_read_error(NULL
, "when reading coordinate in (%s ...)",
914 tex
->opcode_string());
918 // Read texel offset - either 0 or an rvalue.
919 s_int
*si_offset
= SX_AS_INT(s_offset
);
920 if (si_offset
== NULL
|| si_offset
->value() != 0) {
921 tex
->offset
= read_rvalue(s_offset
);
922 if (tex
->offset
== NULL
) {
923 ir_read_error(s_offset
, "expected 0 or an expression");
929 s_int
*proj_as_int
= SX_AS_INT(s_proj
);
930 if (proj_as_int
&& proj_as_int
->value() == 1) {
931 tex
->projector
= NULL
;
933 tex
->projector
= read_rvalue(s_proj
);
934 if (tex
->projector
== NULL
) {
935 ir_read_error(NULL
, "when reading projective divide in (%s ..)",
936 tex
->opcode_string());
941 if (s_shadow
->subexpressions
.is_empty()) {
942 tex
->shadow_comparitor
= NULL
;
944 tex
->shadow_comparitor
= read_rvalue(s_shadow
);
945 if (tex
->shadow_comparitor
== NULL
) {
946 ir_read_error(NULL
, "when reading shadow comparitor in (%s ..)",
947 tex
->opcode_string());
955 tex
->lod_info
.bias
= read_rvalue(s_lod
);
956 if (tex
->lod_info
.bias
== NULL
) {
957 ir_read_error(NULL
, "when reading LOD bias in (txb ...)");
963 tex
->lod_info
.lod
= read_rvalue(s_lod
);
964 if (tex
->lod_info
.lod
== NULL
) {
965 ir_read_error(NULL
, "when reading LOD in (%s ...)",
966 tex
->opcode_string());
971 s_expression
*s_dx
, *s_dy
;
972 s_pattern dxdy_pat
[] = { s_dx
, s_dy
};
973 if (!MATCH(s_lod
, dxdy_pat
)) {
974 ir_read_error(s_lod
, "expected (dPdx dPdy) in (txd ...)");
977 tex
->lod_info
.grad
.dPdx
= read_rvalue(s_dx
);
978 if (tex
->lod_info
.grad
.dPdx
== NULL
) {
979 ir_read_error(NULL
, "when reading dPdx in (txd ...)");
982 tex
->lod_info
.grad
.dPdy
= read_rvalue(s_dy
);
983 if (tex
->lod_info
.grad
.dPdy
== NULL
) {
984 ir_read_error(NULL
, "when reading dPdy in (txd ...)");
990 // tex doesn't have any extra parameters.