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 void *sx_mem_ctx
= ralloc_context(NULL
);
83 s_expression
*expr
= s_expression::read_expression(sx_mem_ctx
, src
);
85 ir_read_error(NULL
, "couldn't parse S-Expression.");
89 if (scan_for_protos
) {
90 scan_for_prototypes(instructions
, expr
);
95 read_instructions(instructions
, expr
, NULL
);
96 ralloc_free(sx_mem_ctx
);
99 validate_ir_tree(instructions
);
103 ir_reader::ir_read_error(s_expression
*expr
, const char *fmt
, ...)
109 if (state
->current_function
!= NULL
)
110 ralloc_asprintf_append(&state
->info_log
, "In function %s:\n",
111 state
->current_function
->function_name());
112 ralloc_strcat(&state
->info_log
, "error: ");
115 ralloc_vasprintf_append(&state
->info_log
, fmt
, ap
);
117 ralloc_strcat(&state
->info_log
, "\n");
120 ralloc_strcat(&state
->info_log
, "...in this context:\n ");
122 ralloc_strcat(&state
->info_log
, "\n\n");
127 ir_reader::read_type(s_expression
*expr
)
129 s_expression
*s_base_type
;
132 s_pattern pat
[] = { "array", s_base_type
, s_size
};
133 if (MATCH(expr
, pat
)) {
134 const glsl_type
*base_type
= read_type(s_base_type
);
135 if (base_type
== NULL
) {
136 ir_read_error(NULL
, "when reading base type of array type");
140 return glsl_type::get_array_instance(base_type
, s_size
->value());
143 s_symbol
*type_sym
= SX_AS_SYMBOL(expr
);
144 if (type_sym
== NULL
) {
145 ir_read_error(expr
, "expected <type>");
149 const glsl_type
*type
= state
->symbols
->get_type(type_sym
->value());
151 ir_read_error(expr
, "invalid type: %s", type_sym
->value());
158 ir_reader::scan_for_prototypes(exec_list
*instructions
, s_expression
*expr
)
160 s_list
*list
= SX_AS_LIST(expr
);
162 ir_read_error(expr
, "Expected (<instruction> ...); found an atom.");
166 foreach_iter(exec_list_iterator
, it
, list
->subexpressions
) {
167 s_list
*sub
= SX_AS_LIST(it
.get());
169 continue; // not a (function ...); ignore it.
171 s_symbol
*tag
= SX_AS_SYMBOL(sub
->subexpressions
.get_head());
172 if (tag
== NULL
|| strcmp(tag
->value(), "function") != 0)
173 continue; // not a (function ...); ignore it.
175 ir_function
*f
= read_function(sub
, true);
178 instructions
->push_tail(f
);
183 ir_reader::read_function(s_expression
*expr
, bool skip_body
)
188 s_pattern pat
[] = { "function", name
};
189 if (!PARTIAL_MATCH(expr
, pat
)) {
190 ir_read_error(expr
, "Expected (function <name> (signature ...) ...)");
194 ir_function
*f
= state
->symbols
->get_function(name
->value());
196 f
= new(mem_ctx
) ir_function(name
->value());
197 added
= state
->symbols
->add_function(f
);
201 exec_list_iterator it
= ((s_list
*) expr
)->subexpressions
.iterator();
202 it
.next(); // skip "function" tag
203 it
.next(); // skip function name
204 for (/* nothing */; it
.has_next(); it
.next()) {
205 s_expression
*s_sig
= (s_expression
*) it
.get();
206 read_function_sig(f
, s_sig
, skip_body
);
208 return added
? f
: NULL
;
212 ir_reader::read_function_sig(ir_function
*f
, s_expression
*expr
, bool skip_body
)
214 s_expression
*type_expr
;
218 s_pattern pat
[] = { "signature", type_expr
, paramlist
, body_list
};
219 if (!MATCH(expr
, pat
)) {
220 ir_read_error(expr
, "Expected (signature <type> (parameters ...) "
221 "(<instruction> ...))");
225 const glsl_type
*return_type
= read_type(type_expr
);
226 if (return_type
== NULL
)
229 s_symbol
*paramtag
= SX_AS_SYMBOL(paramlist
->subexpressions
.get_head());
230 if (paramtag
== NULL
|| strcmp(paramtag
->value(), "parameters") != 0) {
231 ir_read_error(paramlist
, "Expected (parameters ...)");
235 // Read the parameters list into a temporary place.
236 exec_list hir_parameters
;
237 state
->symbols
->push_scope();
239 exec_list_iterator it
= paramlist
->subexpressions
.iterator();
240 for (it
.next() /* skip "parameters" */; it
.has_next(); it
.next()) {
241 ir_variable
*var
= read_declaration((s_expression
*) it
.get());
245 hir_parameters
.push_tail(var
);
248 ir_function_signature
*sig
= f
->exact_matching_signature(&hir_parameters
);
249 if (sig
== NULL
&& skip_body
) {
250 /* If scanning for prototypes, generate a new signature. */
251 sig
= new(mem_ctx
) ir_function_signature(return_type
);
252 sig
->is_builtin
= true;
253 f
->add_signature(sig
);
254 } else if (sig
!= NULL
) {
255 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
256 if (badvar
!= NULL
) {
257 ir_read_error(expr
, "function `%s' parameter `%s' qualifiers "
258 "don't match prototype", f
->name
, badvar
);
262 if (sig
->return_type
!= return_type
) {
263 ir_read_error(expr
, "function `%s' return type doesn't "
264 "match prototype", f
->name
);
268 /* No prototype for this body exists - skip it. */
269 state
->symbols
->pop_scope();
274 sig
->replace_parameters(&hir_parameters
);
276 if (!skip_body
&& !body_list
->subexpressions
.is_empty()) {
277 if (sig
->is_defined
) {
278 ir_read_error(expr
, "function %s redefined", f
->name
);
281 state
->current_function
= sig
;
282 read_instructions(&sig
->body
, body_list
, NULL
);
283 state
->current_function
= NULL
;
284 sig
->is_defined
= true;
287 state
->symbols
->pop_scope();
291 ir_reader::read_instructions(exec_list
*instructions
, s_expression
*expr
,
294 // Read in a list of instructions
295 s_list
*list
= SX_AS_LIST(expr
);
297 ir_read_error(expr
, "Expected (<instruction> ...); found an atom.");
301 foreach_iter(exec_list_iterator
, it
, list
->subexpressions
) {
302 s_expression
*sub
= (s_expression
*) it
.get();
303 ir_instruction
*ir
= read_instruction(sub
, loop_ctx
);
305 /* Global variable declarations should be moved to the top, before
306 * any functions that might use them. Functions are added to the
307 * instruction stream when scanning for prototypes, so without this
308 * hack, they always appear before variable declarations.
310 if (state
->current_function
== NULL
&& ir
->as_variable() != NULL
)
311 instructions
->push_head(ir
);
313 instructions
->push_tail(ir
);
320 ir_reader::read_instruction(s_expression
*expr
, ir_loop
*loop_ctx
)
322 s_symbol
*symbol
= SX_AS_SYMBOL(expr
);
323 if (symbol
!= NULL
) {
324 if (strcmp(symbol
->value(), "break") == 0 && loop_ctx
!= NULL
)
325 return new(mem_ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
326 if (strcmp(symbol
->value(), "continue") == 0 && loop_ctx
!= NULL
)
327 return new(mem_ctx
) ir_loop_jump(ir_loop_jump::jump_continue
);
330 s_list
*list
= SX_AS_LIST(expr
);
331 if (list
== NULL
|| list
->subexpressions
.is_empty()) {
332 ir_read_error(expr
, "Invalid instruction.\n");
336 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
338 ir_read_error(expr
, "expected instruction tag");
342 ir_instruction
*inst
= NULL
;
343 if (strcmp(tag
->value(), "declare") == 0) {
344 inst
= read_declaration(list
);
345 } else if (strcmp(tag
->value(), "assign") == 0) {
346 inst
= read_assignment(list
);
347 } else if (strcmp(tag
->value(), "if") == 0) {
348 inst
= read_if(list
, loop_ctx
);
349 } else if (strcmp(tag
->value(), "loop") == 0) {
350 inst
= read_loop(list
);
351 } else if (strcmp(tag
->value(), "return") == 0) {
352 inst
= read_return(list
);
353 } else if (strcmp(tag
->value(), "function") == 0) {
354 inst
= read_function(list
, false);
356 inst
= read_rvalue(list
);
358 ir_read_error(NULL
, "when reading instruction");
364 ir_reader::read_declaration(s_expression
*expr
)
367 s_expression
*s_type
;
370 s_pattern pat
[] = { "declare", s_quals
, s_type
, s_name
};
371 if (!MATCH(expr
, pat
)) {
372 ir_read_error(expr
, "expected (declare (<qualifiers>) <type> <name>)");
376 const glsl_type
*type
= read_type(s_type
);
380 ir_variable
*var
= new(mem_ctx
) ir_variable(type
, s_name
->value(),
383 foreach_iter(exec_list_iterator
, it
, s_quals
->subexpressions
) {
384 s_symbol
*qualifier
= SX_AS_SYMBOL(it
.get());
385 if (qualifier
== NULL
) {
386 ir_read_error(expr
, "qualifier list must contain only symbols");
390 // FINISHME: Check for duplicate/conflicting qualifiers.
391 if (strcmp(qualifier
->value(), "centroid") == 0) {
393 } else if (strcmp(qualifier
->value(), "invariant") == 0) {
395 } else if (strcmp(qualifier
->value(), "uniform") == 0) {
396 var
->mode
= ir_var_uniform
;
397 } else if (strcmp(qualifier
->value(), "auto") == 0) {
398 var
->mode
= ir_var_auto
;
399 } else if (strcmp(qualifier
->value(), "in") == 0) {
400 var
->mode
= ir_var_in
;
401 } else if (strcmp(qualifier
->value(), "const_in") == 0) {
402 var
->mode
= ir_var_const_in
;
403 } else if (strcmp(qualifier
->value(), "out") == 0) {
404 var
->mode
= ir_var_out
;
405 } else if (strcmp(qualifier
->value(), "inout") == 0) {
406 var
->mode
= ir_var_inout
;
407 } else if (strcmp(qualifier
->value(), "smooth") == 0) {
408 var
->interpolation
= INTERP_QUALIFIER_SMOOTH
;
409 } else if (strcmp(qualifier
->value(), "flat") == 0) {
410 var
->interpolation
= INTERP_QUALIFIER_FLAT
;
411 } else if (strcmp(qualifier
->value(), "noperspective") == 0) {
412 var
->interpolation
= INTERP_QUALIFIER_NOPERSPECTIVE
;
414 ir_read_error(expr
, "unknown qualifier: %s", qualifier
->value());
419 // Add the variable to the symbol table
420 state
->symbols
->add_variable(var
);
427 ir_reader::read_if(s_expression
*expr
, ir_loop
*loop_ctx
)
429 s_expression
*s_cond
;
430 s_expression
*s_then
;
431 s_expression
*s_else
;
433 s_pattern pat
[] = { "if", s_cond
, s_then
, s_else
};
434 if (!MATCH(expr
, pat
)) {
435 ir_read_error(expr
, "expected (if <condition> (<then>...) (<else>...))");
439 ir_rvalue
*condition
= read_rvalue(s_cond
);
440 if (condition
== NULL
) {
441 ir_read_error(NULL
, "when reading condition of (if ...)");
445 ir_if
*iff
= new(mem_ctx
) ir_if(condition
);
447 read_instructions(&iff
->then_instructions
, s_then
, loop_ctx
);
448 read_instructions(&iff
->else_instructions
, s_else
, loop_ctx
);
458 ir_reader::read_loop(s_expression
*expr
)
460 s_expression
*s_counter
, *s_from
, *s_to
, *s_inc
, *s_body
;
462 s_pattern pat
[] = { "loop", s_counter
, s_from
, s_to
, s_inc
, s_body
};
463 if (!MATCH(expr
, pat
)) {
464 ir_read_error(expr
, "expected (loop <counter> <from> <to> "
465 "<increment> <body>)");
469 // FINISHME: actually read the count/from/to fields.
471 ir_loop
*loop
= new(mem_ctx
) ir_loop
;
472 read_instructions(&loop
->body_instructions
, s_body
, loop
);
482 ir_reader::read_return(s_expression
*expr
)
484 s_expression
*s_retval
;
486 s_pattern return_value_pat
[] = { "return", s_retval
};
487 s_pattern return_void_pat
[] = { "return" };
488 if (MATCH(expr
, return_value_pat
)) {
489 ir_rvalue
*retval
= read_rvalue(s_retval
);
490 if (retval
== NULL
) {
491 ir_read_error(NULL
, "when reading return value");
494 return new(mem_ctx
) ir_return(retval
);
495 } else if (MATCH(expr
, return_void_pat
)) {
496 return new(mem_ctx
) ir_return
;
498 ir_read_error(expr
, "expected (return <rvalue>) or (return)");
505 ir_reader::read_rvalue(s_expression
*expr
)
507 s_list
*list
= SX_AS_LIST(expr
);
508 if (list
== NULL
|| list
->subexpressions
.is_empty())
511 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
513 ir_read_error(expr
, "expected rvalue tag");
517 ir_rvalue
*rvalue
= read_dereference(list
);
518 if (rvalue
!= NULL
|| state
->error
)
520 else if (strcmp(tag
->value(), "swiz") == 0) {
521 rvalue
= read_swizzle(list
);
522 } else if (strcmp(tag
->value(), "expression") == 0) {
523 rvalue
= read_expression(list
);
524 } else if (strcmp(tag
->value(), "call") == 0) {
525 rvalue
= read_call(list
);
526 } else if (strcmp(tag
->value(), "constant") == 0) {
527 rvalue
= read_constant(list
);
529 rvalue
= read_texture(list
);
530 if (rvalue
== NULL
&& !state
->error
)
531 ir_read_error(expr
, "unrecognized rvalue tag: %s", tag
->value());
538 ir_reader::read_assignment(s_expression
*expr
)
540 s_expression
*cond_expr
= NULL
;
541 s_expression
*lhs_expr
, *rhs_expr
;
544 s_pattern pat4
[] = { "assign", mask_list
, lhs_expr
, rhs_expr
};
545 s_pattern pat5
[] = { "assign", cond_expr
, mask_list
, lhs_expr
, rhs_expr
};
546 if (!MATCH(expr
, pat4
) && !MATCH(expr
, pat5
)) {
547 ir_read_error(expr
, "expected (assign [<condition>] (<write mask>) "
552 ir_rvalue
*condition
= NULL
;
553 if (cond_expr
!= NULL
) {
554 condition
= read_rvalue(cond_expr
);
555 if (condition
== NULL
) {
556 ir_read_error(NULL
, "when reading condition of assignment");
563 s_symbol
*mask_symbol
;
564 s_pattern mask_pat
[] = { mask_symbol
};
565 if (MATCH(mask_list
, mask_pat
)) {
566 const char *mask_str
= mask_symbol
->value();
567 unsigned mask_length
= strlen(mask_str
);
568 if (mask_length
> 4) {
569 ir_read_error(expr
, "invalid write mask: %s", mask_str
);
573 const unsigned idx_map
[] = { 3, 0, 1, 2 }; /* w=bit 3, x=0, y=1, z=2 */
575 for (unsigned i
= 0; i
< mask_length
; i
++) {
576 if (mask_str
[i
] < 'w' || mask_str
[i
] > 'z') {
577 ir_read_error(expr
, "write mask contains invalid character: %c",
581 mask
|= 1 << idx_map
[mask_str
[i
] - 'w'];
583 } else if (!mask_list
->subexpressions
.is_empty()) {
584 ir_read_error(mask_list
, "expected () or (<write mask>)");
588 ir_dereference
*lhs
= read_dereference(lhs_expr
);
590 ir_read_error(NULL
, "when reading left-hand side of assignment");
594 ir_rvalue
*rhs
= read_rvalue(rhs_expr
);
596 ir_read_error(NULL
, "when reading right-hand side of assignment");
600 if (mask
== 0 && (lhs
->type
->is_vector() || lhs
->type
->is_scalar())) {
601 ir_read_error(expr
, "non-zero write mask required.");
605 return new(mem_ctx
) ir_assignment(lhs
, rhs
, condition
, mask
);
609 ir_reader::read_call(s_expression
*expr
)
614 s_pattern pat
[] = { "call", name
, params
};
615 if (!MATCH(expr
, pat
)) {
616 ir_read_error(expr
, "expected (call <name> (<param> ...))");
620 exec_list parameters
;
622 foreach_iter(exec_list_iterator
, it
, params
->subexpressions
) {
623 s_expression
*expr
= (s_expression
*) it
.get();
624 ir_rvalue
*param
= read_rvalue(expr
);
626 ir_read_error(expr
, "when reading parameter to function call");
629 parameters
.push_tail(param
);
632 ir_function
*f
= state
->symbols
->get_function(name
->value());
634 ir_read_error(expr
, "found call to undefined function %s",
639 ir_function_signature
*callee
= f
->matching_signature(¶meters
);
640 if (callee
== NULL
) {
641 ir_read_error(expr
, "couldn't find matching signature for function "
642 "%s", name
->value());
646 return new(mem_ctx
) ir_call(callee
, ¶meters
);
650 ir_reader::read_expression(s_expression
*expr
)
652 s_expression
*s_type
;
654 s_expression
*s_arg1
;
656 s_pattern pat
[] = { "expression", s_type
, s_op
, s_arg1
};
657 if (!PARTIAL_MATCH(expr
, pat
)) {
658 ir_read_error(expr
, "expected (expression <type> <operator> "
659 "<operand> [<operand>])");
662 s_expression
*s_arg2
= (s_expression
*) s_arg1
->next
; // may be tail sentinel
664 const glsl_type
*type
= read_type(s_type
);
668 /* Read the operator */
669 ir_expression_operation op
= ir_expression::get_operator(s_op
->value());
670 if (op
== (ir_expression_operation
) -1) {
671 ir_read_error(expr
, "invalid operator: %s", s_op
->value());
675 unsigned num_operands
= ir_expression::get_num_operands(op
);
676 if (num_operands
== 1 && !s_arg1
->next
->is_tail_sentinel()) {
677 ir_read_error(expr
, "expected (expression <type> %s <operand>)",
682 ir_rvalue
*arg1
= read_rvalue(s_arg1
);
683 ir_rvalue
*arg2
= NULL
;
685 ir_read_error(NULL
, "when reading first operand of %s", s_op
->value());
689 if (num_operands
== 2) {
690 if (s_arg2
->is_tail_sentinel() || !s_arg2
->next
->is_tail_sentinel()) {
691 ir_read_error(expr
, "expected (expression <type> %s <operand> "
692 "<operand>)", s_op
->value());
695 arg2
= read_rvalue(s_arg2
);
697 ir_read_error(NULL
, "when reading second operand of %s",
703 return new(mem_ctx
) ir_expression(op
, type
, arg1
, arg2
);
707 ir_reader::read_swizzle(s_expression
*expr
)
712 s_pattern pat
[] = { "swiz", swiz
, sub
};
713 if (!MATCH(expr
, pat
)) {
714 ir_read_error(expr
, "expected (swiz <swizzle> <rvalue>)");
718 if (strlen(swiz
->value()) > 4) {
719 ir_read_error(expr
, "expected a valid swizzle; found %s", swiz
->value());
723 ir_rvalue
*rvalue
= read_rvalue(sub
);
727 ir_swizzle
*ir
= ir_swizzle::create(rvalue
, swiz
->value(),
728 rvalue
->type
->vector_elements
);
730 ir_read_error(expr
, "invalid swizzle");
736 ir_reader::read_constant(s_expression
*expr
)
738 s_expression
*type_expr
;
741 s_pattern pat
[] = { "constant", type_expr
, values
};
742 if (!MATCH(expr
, pat
)) {
743 ir_read_error(expr
, "expected (constant <type> (...))");
747 const glsl_type
*type
= read_type(type_expr
);
751 if (values
== NULL
) {
752 ir_read_error(expr
, "expected (constant <type> (...))");
756 if (type
->is_array()) {
757 unsigned elements_supplied
= 0;
759 foreach_iter(exec_list_iterator
, it
, values
->subexpressions
) {
760 s_expression
*elt
= (s_expression
*) it
.get();
761 ir_constant
*ir_elt
= read_constant(elt
);
764 elements
.push_tail(ir_elt
);
768 if (elements_supplied
!= type
->length
) {
769 ir_read_error(values
, "expected exactly %u array elements, "
770 "given %u", type
->length
, elements_supplied
);
773 return new(mem_ctx
) ir_constant(type
, &elements
);
776 ir_constant_data data
= { { 0 } };
778 // Read in list of values (at most 16).
780 foreach_iter(exec_list_iterator
, it
, values
->subexpressions
) {
782 ir_read_error(values
, "expected at most 16 numbers");
786 s_expression
*expr
= (s_expression
*) it
.get();
788 if (type
->base_type
== GLSL_TYPE_FLOAT
) {
789 s_number
*value
= SX_AS_NUMBER(expr
);
791 ir_read_error(values
, "expected numbers");
794 data
.f
[k
] = value
->fvalue();
796 s_int
*value
= SX_AS_INT(expr
);
798 ir_read_error(values
, "expected integers");
802 switch (type
->base_type
) {
803 case GLSL_TYPE_UINT
: {
804 data
.u
[k
] = value
->value();
807 case GLSL_TYPE_INT
: {
808 data
.i
[k
] = value
->value();
811 case GLSL_TYPE_BOOL
: {
812 data
.b
[k
] = value
->value();
816 ir_read_error(values
, "unsupported constant type");
822 if (k
!= type
->components()) {
823 ir_read_error(values
, "expected %u constant values, found %u",
824 type
->components(), k
);
828 return new(mem_ctx
) ir_constant(type
, &data
);
832 ir_reader::read_dereference(s_expression
*expr
)
835 s_expression
*s_subject
;
836 s_expression
*s_index
;
839 s_pattern var_pat
[] = { "var_ref", s_var
};
840 s_pattern array_pat
[] = { "array_ref", s_subject
, s_index
};
841 s_pattern record_pat
[] = { "record_ref", s_subject
, s_field
};
843 if (MATCH(expr
, var_pat
)) {
844 ir_variable
*var
= state
->symbols
->get_variable(s_var
->value());
846 ir_read_error(expr
, "undeclared variable: %s", s_var
->value());
849 return new(mem_ctx
) ir_dereference_variable(var
);
850 } else if (MATCH(expr
, array_pat
)) {
851 ir_rvalue
*subject
= read_rvalue(s_subject
);
852 if (subject
== NULL
) {
853 ir_read_error(NULL
, "when reading the subject of an array_ref");
857 ir_rvalue
*idx
= read_rvalue(s_index
);
858 if (subject
== NULL
) {
859 ir_read_error(NULL
, "when reading the index of an array_ref");
862 return new(mem_ctx
) ir_dereference_array(subject
, idx
);
863 } else if (MATCH(expr
, record_pat
)) {
864 ir_rvalue
*subject
= read_rvalue(s_subject
);
865 if (subject
== NULL
) {
866 ir_read_error(NULL
, "when reading the subject of a record_ref");
869 return new(mem_ctx
) ir_dereference_record(subject
, s_field
->value());
875 ir_reader::read_texture(s_expression
*expr
)
877 s_symbol
*tag
= NULL
;
878 s_expression
*s_type
= NULL
;
879 s_expression
*s_sampler
= NULL
;
880 s_expression
*s_coord
= NULL
;
881 s_expression
*s_offset
= NULL
;
882 s_expression
*s_proj
= NULL
;
883 s_list
*s_shadow
= NULL
;
884 s_expression
*s_lod
= NULL
;
886 ir_texture_opcode op
= ir_tex
; /* silence warning */
888 s_pattern tex_pattern
[] =
889 { "tex", s_type
, s_sampler
, s_coord
, s_offset
, s_proj
, s_shadow
};
890 s_pattern txf_pattern
[] =
891 { "txf", s_type
, s_sampler
, s_coord
, s_offset
, s_lod
};
892 s_pattern txs_pattern
[] =
893 { "txs", s_type
, s_sampler
, s_lod
};
894 s_pattern other_pattern
[] =
895 { tag
, s_type
, s_sampler
, s_coord
, s_offset
, s_proj
, s_shadow
, s_lod
};
897 if (MATCH(expr
, tex_pattern
)) {
899 } else if (MATCH(expr
, txf_pattern
)) {
901 } else if (MATCH(expr
, txs_pattern
)) {
903 } else if (MATCH(expr
, other_pattern
)) {
904 op
= ir_texture::get_opcode(tag
->value());
908 ir_read_error(NULL
, "unexpected texture pattern");
912 ir_texture
*tex
= new(mem_ctx
) ir_texture(op
);
915 const glsl_type
*type
= read_type(s_type
);
917 ir_read_error(NULL
, "when reading type in (%s ...)",
918 tex
->opcode_string());
922 // Read sampler (must be a deref)
923 ir_dereference
*sampler
= read_dereference(s_sampler
);
924 if (sampler
== NULL
) {
925 ir_read_error(NULL
, "when reading sampler in (%s ...)",
926 tex
->opcode_string());
929 tex
->set_sampler(sampler
, type
);
932 // Read coordinate (any rvalue)
933 tex
->coordinate
= read_rvalue(s_coord
);
934 if (tex
->coordinate
== NULL
) {
935 ir_read_error(NULL
, "when reading coordinate in (%s ...)",
936 tex
->opcode_string());
940 // Read texel offset - either 0 or an rvalue.
941 s_int
*si_offset
= SX_AS_INT(s_offset
);
942 if (si_offset
== NULL
|| si_offset
->value() != 0) {
943 tex
->offset
= read_rvalue(s_offset
);
944 if (tex
->offset
== NULL
) {
945 ir_read_error(s_offset
, "expected 0 or an expression");
951 if (op
!= ir_txf
&& op
!= ir_txs
) {
952 s_int
*proj_as_int
= SX_AS_INT(s_proj
);
953 if (proj_as_int
&& proj_as_int
->value() == 1) {
954 tex
->projector
= NULL
;
956 tex
->projector
= read_rvalue(s_proj
);
957 if (tex
->projector
== NULL
) {
958 ir_read_error(NULL
, "when reading projective divide in (%s ..)",
959 tex
->opcode_string());
964 if (s_shadow
->subexpressions
.is_empty()) {
965 tex
->shadow_comparitor
= NULL
;
967 tex
->shadow_comparitor
= read_rvalue(s_shadow
);
968 if (tex
->shadow_comparitor
== NULL
) {
969 ir_read_error(NULL
, "when reading shadow comparitor in (%s ..)",
970 tex
->opcode_string());
978 tex
->lod_info
.bias
= read_rvalue(s_lod
);
979 if (tex
->lod_info
.bias
== NULL
) {
980 ir_read_error(NULL
, "when reading LOD bias in (txb ...)");
987 tex
->lod_info
.lod
= read_rvalue(s_lod
);
988 if (tex
->lod_info
.lod
== NULL
) {
989 ir_read_error(NULL
, "when reading LOD in (%s ...)",
990 tex
->opcode_string());
995 s_expression
*s_dx
, *s_dy
;
996 s_pattern dxdy_pat
[] = { s_dx
, s_dy
};
997 if (!MATCH(s_lod
, dxdy_pat
)) {
998 ir_read_error(s_lod
, "expected (dPdx dPdy) in (txd ...)");
1001 tex
->lod_info
.grad
.dPdx
= read_rvalue(s_dx
);
1002 if (tex
->lod_info
.grad
.dPdx
== NULL
) {
1003 ir_read_error(NULL
, "when reading dPdx in (txd ...)");
1006 tex
->lod_info
.grad
.dPdy
= read_rvalue(s_dy
);
1007 if (tex
->lod_info
.grad
.dPdy
== NULL
) {
1008 ir_read_error(NULL
, "when reading dPdy in (txd ...)");
1014 // tex doesn't have any extra parameters.