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(), "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
;
413 ir_read_error(expr
, "unknown qualifier: %s", qualifier
->value());
418 // Add the variable to the symbol table
419 state
->symbols
->add_variable(var
);
426 ir_reader::read_if(s_expression
*expr
, ir_loop
*loop_ctx
)
428 s_expression
*s_cond
;
429 s_expression
*s_then
;
430 s_expression
*s_else
;
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>...))");
438 ir_rvalue
*condition
= read_rvalue(s_cond
);
439 if (condition
== NULL
) {
440 ir_read_error(NULL
, "when reading condition of (if ...)");
444 ir_if
*iff
= new(mem_ctx
) ir_if(condition
);
446 read_instructions(&iff
->then_instructions
, s_then
, loop_ctx
);
447 read_instructions(&iff
->else_instructions
, s_else
, loop_ctx
);
457 ir_reader::read_loop(s_expression
*expr
)
459 s_expression
*s_counter
, *s_from
, *s_to
, *s_inc
, *s_body
;
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>)");
468 // FINISHME: actually read the count/from/to fields.
470 ir_loop
*loop
= new(mem_ctx
) ir_loop
;
471 read_instructions(&loop
->body_instructions
, s_body
, loop
);
481 ir_reader::read_return(s_expression
*expr
)
483 s_expression
*s_retval
;
485 s_pattern pat
[] = { "return", s_retval
};
486 if (!MATCH(expr
, pat
)) {
487 ir_read_error(expr
, "expected (return <rvalue>)");
491 ir_rvalue
*retval
= read_rvalue(s_retval
);
492 if (retval
== NULL
) {
493 ir_read_error(NULL
, "when reading return value");
497 return new(mem_ctx
) ir_return(retval
);
502 ir_reader::read_rvalue(s_expression
*expr
)
504 s_list
*list
= SX_AS_LIST(expr
);
505 if (list
== NULL
|| list
->subexpressions
.is_empty())
508 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
510 ir_read_error(expr
, "expected rvalue tag");
514 ir_rvalue
*rvalue
= read_dereference(list
);
515 if (rvalue
!= NULL
|| state
->error
)
517 else if (strcmp(tag
->value(), "swiz") == 0) {
518 rvalue
= read_swizzle(list
);
519 } else if (strcmp(tag
->value(), "expression") == 0) {
520 rvalue
= read_expression(list
);
521 } else if (strcmp(tag
->value(), "call") == 0) {
522 rvalue
= read_call(list
);
523 } else if (strcmp(tag
->value(), "constant") == 0) {
524 rvalue
= read_constant(list
);
526 rvalue
= read_texture(list
);
527 if (rvalue
== NULL
&& !state
->error
)
528 ir_read_error(expr
, "unrecognized rvalue tag: %s", tag
->value());
535 ir_reader::read_assignment(s_expression
*expr
)
537 s_expression
*cond_expr
= NULL
;
538 s_expression
*lhs_expr
, *rhs_expr
;
541 s_pattern pat4
[] = { "assign", mask_list
, lhs_expr
, rhs_expr
};
542 s_pattern pat5
[] = { "assign", cond_expr
, mask_list
, lhs_expr
, rhs_expr
};
543 if (!MATCH(expr
, pat4
) && !MATCH(expr
, pat5
)) {
544 ir_read_error(expr
, "expected (assign [<condition>] (<write mask>) "
549 ir_rvalue
*condition
= NULL
;
550 if (cond_expr
!= NULL
) {
551 condition
= read_rvalue(cond_expr
);
552 if (condition
== NULL
) {
553 ir_read_error(NULL
, "when reading condition of assignment");
560 s_symbol
*mask_symbol
;
561 s_pattern mask_pat
[] = { mask_symbol
};
562 if (MATCH(mask_list
, mask_pat
)) {
563 const char *mask_str
= mask_symbol
->value();
564 unsigned mask_length
= strlen(mask_str
);
565 if (mask_length
> 4) {
566 ir_read_error(expr
, "invalid write mask: %s", mask_str
);
570 const unsigned idx_map
[] = { 3, 0, 1, 2 }; /* w=bit 3, x=0, y=1, z=2 */
572 for (unsigned i
= 0; i
< mask_length
; i
++) {
573 if (mask_str
[i
] < 'w' || mask_str
[i
] > 'z') {
574 ir_read_error(expr
, "write mask contains invalid character: %c",
578 mask
|= 1 << idx_map
[mask_str
[i
] - 'w'];
580 } else if (!mask_list
->subexpressions
.is_empty()) {
581 ir_read_error(mask_list
, "expected () or (<write mask>)");
585 ir_dereference
*lhs
= read_dereference(lhs_expr
);
587 ir_read_error(NULL
, "when reading left-hand side of assignment");
591 ir_rvalue
*rhs
= read_rvalue(rhs_expr
);
593 ir_read_error(NULL
, "when reading right-hand side of assignment");
597 if (mask
== 0 && (lhs
->type
->is_vector() || lhs
->type
->is_scalar())) {
598 ir_read_error(expr
, "non-zero write mask required.");
602 return new(mem_ctx
) ir_assignment(lhs
, rhs
, condition
, mask
);
606 ir_reader::read_call(s_expression
*expr
)
611 s_pattern pat
[] = { "call", name
, params
};
612 if (!MATCH(expr
, pat
)) {
613 ir_read_error(expr
, "expected (call <name> (<param> ...))");
617 exec_list parameters
;
619 foreach_iter(exec_list_iterator
, it
, params
->subexpressions
) {
620 s_expression
*expr
= (s_expression
*) it
.get();
621 ir_rvalue
*param
= read_rvalue(expr
);
623 ir_read_error(expr
, "when reading parameter to function call");
626 parameters
.push_tail(param
);
629 ir_function
*f
= state
->symbols
->get_function(name
->value());
631 ir_read_error(expr
, "found call to undefined function %s",
636 ir_function_signature
*callee
= f
->matching_signature(¶meters
);
637 if (callee
== NULL
) {
638 ir_read_error(expr
, "couldn't find matching signature for function "
639 "%s", name
->value());
643 return new(mem_ctx
) ir_call(callee
, ¶meters
);
647 ir_reader::read_expression(s_expression
*expr
)
649 s_expression
*s_type
;
651 s_expression
*s_arg1
;
653 s_pattern pat
[] = { "expression", s_type
, s_op
, s_arg1
};
654 if (!PARTIAL_MATCH(expr
, pat
)) {
655 ir_read_error(expr
, "expected (expression <type> <operator> "
656 "<operand> [<operand>])");
659 s_expression
*s_arg2
= (s_expression
*) s_arg1
->next
; // may be tail sentinel
661 const glsl_type
*type
= read_type(s_type
);
665 /* Read the operator */
666 ir_expression_operation op
= ir_expression::get_operator(s_op
->value());
667 if (op
== (ir_expression_operation
) -1) {
668 ir_read_error(expr
, "invalid operator: %s", s_op
->value());
672 unsigned num_operands
= ir_expression::get_num_operands(op
);
673 if (num_operands
== 1 && !s_arg1
->next
->is_tail_sentinel()) {
674 ir_read_error(expr
, "expected (expression <type> %s <operand>)",
679 ir_rvalue
*arg1
= read_rvalue(s_arg1
);
680 ir_rvalue
*arg2
= NULL
;
682 ir_read_error(NULL
, "when reading first operand of %s", s_op
->value());
686 if (num_operands
== 2) {
687 if (s_arg2
->is_tail_sentinel() || !s_arg2
->next
->is_tail_sentinel()) {
688 ir_read_error(expr
, "expected (expression <type> %s <operand> "
689 "<operand>)", s_op
->value());
692 arg2
= read_rvalue(s_arg2
);
694 ir_read_error(NULL
, "when reading second operand of %s",
700 return new(mem_ctx
) ir_expression(op
, type
, arg1
, arg2
);
704 ir_reader::read_swizzle(s_expression
*expr
)
709 s_pattern pat
[] = { "swiz", swiz
, sub
};
710 if (!MATCH(expr
, pat
)) {
711 ir_read_error(expr
, "expected (swiz <swizzle> <rvalue>)");
715 if (strlen(swiz
->value()) > 4) {
716 ir_read_error(expr
, "expected a valid swizzle; found %s", swiz
->value());
720 ir_rvalue
*rvalue
= read_rvalue(sub
);
724 ir_swizzle
*ir
= ir_swizzle::create(rvalue
, swiz
->value(),
725 rvalue
->type
->vector_elements
);
727 ir_read_error(expr
, "invalid swizzle");
733 ir_reader::read_constant(s_expression
*expr
)
735 s_expression
*type_expr
;
738 s_pattern pat
[] = { "constant", type_expr
, values
};
739 if (!MATCH(expr
, pat
)) {
740 ir_read_error(expr
, "expected (constant <type> (...))");
744 const glsl_type
*type
= read_type(type_expr
);
748 if (values
== NULL
) {
749 ir_read_error(expr
, "expected (constant <type> (...))");
753 if (type
->is_array()) {
754 unsigned elements_supplied
= 0;
756 foreach_iter(exec_list_iterator
, it
, values
->subexpressions
) {
757 s_expression
*elt
= (s_expression
*) it
.get();
758 ir_constant
*ir_elt
= read_constant(elt
);
761 elements
.push_tail(ir_elt
);
765 if (elements_supplied
!= type
->length
) {
766 ir_read_error(values
, "expected exactly %u array elements, "
767 "given %u", type
->length
, elements_supplied
);
770 return new(mem_ctx
) ir_constant(type
, &elements
);
773 const glsl_type
*const base_type
= type
->get_base_type();
775 ir_constant_data data
= { { 0 } };
777 // Read in list of values (at most 16).
779 foreach_iter(exec_list_iterator
, it
, values
->subexpressions
) {
781 ir_read_error(values
, "expected at most 16 numbers");
785 s_expression
*expr
= (s_expression
*) it
.get();
787 if (base_type
->base_type
== GLSL_TYPE_FLOAT
) {
788 s_number
*value
= SX_AS_NUMBER(expr
);
790 ir_read_error(values
, "expected numbers");
793 data
.f
[k
] = value
->fvalue();
795 s_int
*value
= SX_AS_INT(expr
);
797 ir_read_error(values
, "expected integers");
801 switch (base_type
->base_type
) {
802 case GLSL_TYPE_UINT
: {
803 data
.u
[k
] = value
->value();
806 case GLSL_TYPE_INT
: {
807 data
.i
[k
] = value
->value();
810 case GLSL_TYPE_BOOL
: {
811 data
.b
[k
] = value
->value();
815 ir_read_error(values
, "unsupported constant type");
822 return new(mem_ctx
) ir_constant(type
, &data
);
826 ir_reader::read_dereference(s_expression
*expr
)
829 s_expression
*s_subject
;
830 s_expression
*s_index
;
833 s_pattern var_pat
[] = { "var_ref", s_var
};
834 s_pattern array_pat
[] = { "array_ref", s_subject
, s_index
};
835 s_pattern record_pat
[] = { "record_ref", s_subject
, s_field
};
837 if (MATCH(expr
, var_pat
)) {
838 ir_variable
*var
= state
->symbols
->get_variable(s_var
->value());
840 ir_read_error(expr
, "undeclared variable: %s", s_var
->value());
843 return new(mem_ctx
) ir_dereference_variable(var
);
844 } else if (MATCH(expr
, array_pat
)) {
845 ir_rvalue
*subject
= read_rvalue(s_subject
);
846 if (subject
== NULL
) {
847 ir_read_error(NULL
, "when reading the subject of an array_ref");
851 ir_rvalue
*idx
= read_rvalue(s_index
);
852 if (subject
== NULL
) {
853 ir_read_error(NULL
, "when reading the index of an array_ref");
856 return new(mem_ctx
) ir_dereference_array(subject
, idx
);
857 } else if (MATCH(expr
, record_pat
)) {
858 ir_rvalue
*subject
= read_rvalue(s_subject
);
859 if (subject
== NULL
) {
860 ir_read_error(NULL
, "when reading the subject of a record_ref");
863 return new(mem_ctx
) ir_dereference_record(subject
, s_field
->value());
869 ir_reader::read_texture(s_expression
*expr
)
871 s_symbol
*tag
= NULL
;
872 s_expression
*s_type
= NULL
;
873 s_expression
*s_sampler
= NULL
;
874 s_expression
*s_coord
= NULL
;
875 s_expression
*s_offset
= NULL
;
876 s_expression
*s_proj
= NULL
;
877 s_list
*s_shadow
= NULL
;
878 s_expression
*s_lod
= NULL
;
880 ir_texture_opcode op
= ir_tex
; /* silence warning */
882 s_pattern tex_pattern
[] =
883 { "tex", s_type
, s_sampler
, s_coord
, s_offset
, s_proj
, s_shadow
};
884 s_pattern txf_pattern
[] =
885 { "txf", s_type
, s_sampler
, s_coord
, s_offset
, s_lod
};
886 s_pattern other_pattern
[] =
887 { tag
, s_type
, s_sampler
, s_coord
, s_offset
, s_proj
, s_shadow
, s_lod
};
889 if (MATCH(expr
, tex_pattern
)) {
891 } else if (MATCH(expr
, txf_pattern
)) {
893 } else if (MATCH(expr
, other_pattern
)) {
894 op
= ir_texture::get_opcode(tag
->value());
898 ir_read_error(NULL
, "unexpected texture pattern");
902 ir_texture
*tex
= new(mem_ctx
) ir_texture(op
);
905 const glsl_type
*type
= read_type(s_type
);
907 ir_read_error(NULL
, "when reading type in (%s ...)",
908 tex
->opcode_string());
912 // Read sampler (must be a deref)
913 ir_dereference
*sampler
= read_dereference(s_sampler
);
914 if (sampler
== NULL
) {
915 ir_read_error(NULL
, "when reading sampler in (%s ...)",
916 tex
->opcode_string());
919 tex
->set_sampler(sampler
, type
);
921 // Read coordinate (any rvalue)
922 tex
->coordinate
= read_rvalue(s_coord
);
923 if (tex
->coordinate
== NULL
) {
924 ir_read_error(NULL
, "when reading coordinate in (%s ...)",
925 tex
->opcode_string());
929 // Read texel offset - either 0 or an rvalue.
930 s_int
*si_offset
= SX_AS_INT(s_offset
);
931 if (si_offset
== NULL
|| si_offset
->value() != 0) {
932 tex
->offset
= read_rvalue(s_offset
);
933 if (tex
->offset
== NULL
) {
934 ir_read_error(s_offset
, "expected 0 or an expression");
940 s_int
*proj_as_int
= SX_AS_INT(s_proj
);
941 if (proj_as_int
&& proj_as_int
->value() == 1) {
942 tex
->projector
= NULL
;
944 tex
->projector
= read_rvalue(s_proj
);
945 if (tex
->projector
== NULL
) {
946 ir_read_error(NULL
, "when reading projective divide in (%s ..)",
947 tex
->opcode_string());
952 if (s_shadow
->subexpressions
.is_empty()) {
953 tex
->shadow_comparitor
= NULL
;
955 tex
->shadow_comparitor
= read_rvalue(s_shadow
);
956 if (tex
->shadow_comparitor
== NULL
) {
957 ir_read_error(NULL
, "when reading shadow comparitor in (%s ..)",
958 tex
->opcode_string());
966 tex
->lod_info
.bias
= read_rvalue(s_lod
);
967 if (tex
->lod_info
.bias
== NULL
) {
968 ir_read_error(NULL
, "when reading LOD bias in (txb ...)");
974 tex
->lod_info
.lod
= read_rvalue(s_lod
);
975 if (tex
->lod_info
.lod
== NULL
) {
976 ir_read_error(NULL
, "when reading LOD in (%s ...)",
977 tex
->opcode_string());
982 s_expression
*s_dx
, *s_dy
;
983 s_pattern dxdy_pat
[] = { s_dx
, s_dy
};
984 if (!MATCH(s_lod
, dxdy_pat
)) {
985 ir_read_error(s_lod
, "expected (dPdx dPdy) in (txd ...)");
988 tex
->lod_info
.grad
.dPdx
= read_rvalue(s_dx
);
989 if (tex
->lod_info
.grad
.dPdx
== NULL
) {
990 ir_read_error(NULL
, "when reading dPdx in (txd ...)");
993 tex
->lod_info
.grad
.dPdy
= read_rvalue(s_dy
);
994 if (tex
->lod_info
.grad
.dPdy
== NULL
) {
995 ir_read_error(NULL
, "when reading dPdy in (txd ...)");
1001 // tex doesn't have any extra parameters.