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;
35 ir_reader(_mesa_glsl_parse_state
*);
37 void read(exec_list
*instructions
, const char *src
, bool scan_for_protos
);
41 _mesa_glsl_parse_state
*state
;
43 void ir_read_error(s_expression
*, const char *fmt
, ...);
45 const glsl_type
*read_type(s_expression
*);
47 void scan_for_prototypes(exec_list
*, s_expression
*);
48 ir_function
*read_function(s_expression
*, bool skip_body
);
49 void read_function_sig(ir_function
*, s_expression
*, bool skip_body
);
51 void read_instructions(exec_list
*, s_expression
*, ir_loop
*);
52 ir_instruction
*read_instruction(s_expression
*, ir_loop
*);
53 ir_variable
*read_declaration(s_expression
*);
54 ir_if
*read_if(s_expression
*, ir_loop
*);
55 ir_loop
*read_loop(s_expression
*);
56 ir_call
*read_call(s_expression
*);
57 ir_return
*read_return(s_expression
*);
58 ir_rvalue
*read_rvalue(s_expression
*);
59 ir_assignment
*read_assignment(s_expression
*);
60 ir_expression
*read_expression(s_expression
*);
61 ir_swizzle
*read_swizzle(s_expression
*);
62 ir_constant
*read_constant(s_expression
*);
63 ir_texture
*read_texture(s_expression
*);
64 ir_emit_vertex
*read_emit_vertex(s_expression
*);
65 ir_end_primitive
*read_end_primitive(s_expression
*);
67 ir_dereference
*read_dereference(s_expression
*);
68 ir_dereference_variable
*read_var_ref(s_expression
*);
71 } /* anonymous namespace */
73 ir_reader::ir_reader(_mesa_glsl_parse_state
*state
) : state(state
)
75 this->mem_ctx
= state
;
79 _mesa_glsl_read_ir(_mesa_glsl_parse_state
*state
, exec_list
*instructions
,
80 const char *src
, bool scan_for_protos
)
83 r
.read(instructions
, src
, scan_for_protos
);
87 ir_reader::read(exec_list
*instructions
, const char *src
, bool scan_for_protos
)
89 void *sx_mem_ctx
= ralloc_context(NULL
);
90 s_expression
*expr
= s_expression::read_expression(sx_mem_ctx
, src
);
92 ir_read_error(NULL
, "couldn't parse S-Expression.");
96 if (scan_for_protos
) {
97 scan_for_prototypes(instructions
, expr
);
102 read_instructions(instructions
, expr
, NULL
);
103 ralloc_free(sx_mem_ctx
);
106 validate_ir_tree(instructions
);
110 ir_reader::ir_read_error(s_expression
*expr
, const char *fmt
, ...)
116 if (state
->current_function
!= NULL
)
117 ralloc_asprintf_append(&state
->info_log
, "In function %s:\n",
118 state
->current_function
->function_name());
119 ralloc_strcat(&state
->info_log
, "error: ");
122 ralloc_vasprintf_append(&state
->info_log
, fmt
, ap
);
124 ralloc_strcat(&state
->info_log
, "\n");
127 ralloc_strcat(&state
->info_log
, "...in this context:\n ");
129 ralloc_strcat(&state
->info_log
, "\n\n");
134 ir_reader::read_type(s_expression
*expr
)
136 s_expression
*s_base_type
;
139 s_pattern pat
[] = { "array", s_base_type
, s_size
};
140 if (MATCH(expr
, pat
)) {
141 const glsl_type
*base_type
= read_type(s_base_type
);
142 if (base_type
== NULL
) {
143 ir_read_error(NULL
, "when reading base type of array type");
147 return glsl_type::get_array_instance(base_type
, s_size
->value());
150 s_symbol
*type_sym
= SX_AS_SYMBOL(expr
);
151 if (type_sym
== NULL
) {
152 ir_read_error(expr
, "expected <type>");
156 const glsl_type
*type
= state
->symbols
->get_type(type_sym
->value());
158 ir_read_error(expr
, "invalid type: %s", type_sym
->value());
165 ir_reader::scan_for_prototypes(exec_list
*instructions
, s_expression
*expr
)
167 s_list
*list
= SX_AS_LIST(expr
);
169 ir_read_error(expr
, "Expected (<instruction> ...); found an atom.");
173 foreach_iter(exec_list_iterator
, it
, list
->subexpressions
) {
174 s_list
*sub
= SX_AS_LIST(it
.get());
176 continue; // not a (function ...); ignore it.
178 s_symbol
*tag
= SX_AS_SYMBOL(sub
->subexpressions
.get_head());
179 if (tag
== NULL
|| strcmp(tag
->value(), "function") != 0)
180 continue; // not a (function ...); ignore it.
182 ir_function
*f
= read_function(sub
, true);
185 instructions
->push_tail(f
);
190 ir_reader::read_function(s_expression
*expr
, bool skip_body
)
195 s_pattern pat
[] = { "function", name
};
196 if (!PARTIAL_MATCH(expr
, pat
)) {
197 ir_read_error(expr
, "Expected (function <name> (signature ...) ...)");
201 ir_function
*f
= state
->symbols
->get_function(name
->value());
203 f
= new(mem_ctx
) ir_function(name
->value());
204 added
= state
->symbols
->add_function(f
);
208 exec_list_iterator it
= ((s_list
*) expr
)->subexpressions
.iterator();
209 it
.next(); // skip "function" tag
210 it
.next(); // skip function name
211 for (/* nothing */; it
.has_next(); it
.next()) {
212 s_expression
*s_sig
= (s_expression
*) it
.get();
213 read_function_sig(f
, s_sig
, skip_body
);
215 return added
? f
: NULL
;
219 always_available(const _mesa_glsl_parse_state
*)
225 ir_reader::read_function_sig(ir_function
*f
, s_expression
*expr
, bool skip_body
)
227 s_expression
*type_expr
;
231 s_pattern pat
[] = { "signature", type_expr
, paramlist
, body_list
};
232 if (!MATCH(expr
, pat
)) {
233 ir_read_error(expr
, "Expected (signature <type> (parameters ...) "
234 "(<instruction> ...))");
238 const glsl_type
*return_type
= read_type(type_expr
);
239 if (return_type
== NULL
)
242 s_symbol
*paramtag
= SX_AS_SYMBOL(paramlist
->subexpressions
.get_head());
243 if (paramtag
== NULL
|| strcmp(paramtag
->value(), "parameters") != 0) {
244 ir_read_error(paramlist
, "Expected (parameters ...)");
248 // Read the parameters list into a temporary place.
249 exec_list hir_parameters
;
250 state
->symbols
->push_scope();
252 exec_list_iterator it
= paramlist
->subexpressions
.iterator();
253 for (it
.next() /* skip "parameters" */; it
.has_next(); it
.next()) {
254 ir_variable
*var
= read_declaration((s_expression
*) it
.get());
258 hir_parameters
.push_tail(var
);
261 ir_function_signature
*sig
=
262 f
->exact_matching_signature(state
, &hir_parameters
);
263 if (sig
== NULL
&& skip_body
) {
264 /* If scanning for prototypes, generate a new signature. */
265 /* ir_reader doesn't know what languages support a given built-in, so
266 * just say that they're always available. For now, other mechanisms
267 * guarantee the right built-ins are available.
269 sig
= new(mem_ctx
) ir_function_signature(return_type
, always_available
);
270 f
->add_signature(sig
);
271 } else if (sig
!= NULL
) {
272 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
273 if (badvar
!= NULL
) {
274 ir_read_error(expr
, "function `%s' parameter `%s' qualifiers "
275 "don't match prototype", f
->name
, badvar
);
279 if (sig
->return_type
!= return_type
) {
280 ir_read_error(expr
, "function `%s' return type doesn't "
281 "match prototype", f
->name
);
285 /* No prototype for this body exists - skip it. */
286 state
->symbols
->pop_scope();
291 sig
->replace_parameters(&hir_parameters
);
293 if (!skip_body
&& !body_list
->subexpressions
.is_empty()) {
294 if (sig
->is_defined
) {
295 ir_read_error(expr
, "function %s redefined", f
->name
);
298 state
->current_function
= sig
;
299 read_instructions(&sig
->body
, body_list
, NULL
);
300 state
->current_function
= NULL
;
301 sig
->is_defined
= true;
304 state
->symbols
->pop_scope();
308 ir_reader::read_instructions(exec_list
*instructions
, s_expression
*expr
,
311 // Read in a list of instructions
312 s_list
*list
= SX_AS_LIST(expr
);
314 ir_read_error(expr
, "Expected (<instruction> ...); found an atom.");
318 foreach_iter(exec_list_iterator
, it
, list
->subexpressions
) {
319 s_expression
*sub
= (s_expression
*) it
.get();
320 ir_instruction
*ir
= read_instruction(sub
, loop_ctx
);
322 /* Global variable declarations should be moved to the top, before
323 * any functions that might use them. Functions are added to the
324 * instruction stream when scanning for prototypes, so without this
325 * hack, they always appear before variable declarations.
327 if (state
->current_function
== NULL
&& ir
->as_variable() != NULL
)
328 instructions
->push_head(ir
);
330 instructions
->push_tail(ir
);
337 ir_reader::read_instruction(s_expression
*expr
, ir_loop
*loop_ctx
)
339 s_symbol
*symbol
= SX_AS_SYMBOL(expr
);
340 if (symbol
!= NULL
) {
341 if (strcmp(symbol
->value(), "break") == 0 && loop_ctx
!= NULL
)
342 return new(mem_ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
343 if (strcmp(symbol
->value(), "continue") == 0 && loop_ctx
!= NULL
)
344 return new(mem_ctx
) ir_loop_jump(ir_loop_jump::jump_continue
);
347 s_list
*list
= SX_AS_LIST(expr
);
348 if (list
== NULL
|| list
->subexpressions
.is_empty()) {
349 ir_read_error(expr
, "Invalid instruction.\n");
353 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
355 ir_read_error(expr
, "expected instruction tag");
359 ir_instruction
*inst
= NULL
;
360 if (strcmp(tag
->value(), "declare") == 0) {
361 inst
= read_declaration(list
);
362 } else if (strcmp(tag
->value(), "assign") == 0) {
363 inst
= read_assignment(list
);
364 } else if (strcmp(tag
->value(), "if") == 0) {
365 inst
= read_if(list
, loop_ctx
);
366 } else if (strcmp(tag
->value(), "loop") == 0) {
367 inst
= read_loop(list
);
368 } else if (strcmp(tag
->value(), "call") == 0) {
369 inst
= read_call(list
);
370 } else if (strcmp(tag
->value(), "return") == 0) {
371 inst
= read_return(list
);
372 } else if (strcmp(tag
->value(), "function") == 0) {
373 inst
= read_function(list
, false);
374 } else if (strcmp(tag
->value(), "emit-vertex") == 0) {
375 inst
= read_emit_vertex(list
);
376 } else if (strcmp(tag
->value(), "end-primitive") == 0) {
377 inst
= read_end_primitive(list
);
379 inst
= read_rvalue(list
);
381 ir_read_error(NULL
, "when reading instruction");
387 ir_reader::read_declaration(s_expression
*expr
)
390 s_expression
*s_type
;
393 s_pattern pat
[] = { "declare", s_quals
, s_type
, s_name
};
394 if (!MATCH(expr
, pat
)) {
395 ir_read_error(expr
, "expected (declare (<qualifiers>) <type> <name>)");
399 const glsl_type
*type
= read_type(s_type
);
403 ir_variable
*var
= new(mem_ctx
) ir_variable(type
, s_name
->value(),
406 foreach_iter(exec_list_iterator
, it
, s_quals
->subexpressions
) {
407 s_symbol
*qualifier
= SX_AS_SYMBOL(it
.get());
408 if (qualifier
== NULL
) {
409 ir_read_error(expr
, "qualifier list must contain only symbols");
413 // FINISHME: Check for duplicate/conflicting qualifiers.
414 if (strcmp(qualifier
->value(), "centroid") == 0) {
416 } else if (strcmp(qualifier
->value(), "sample") == 0) {
418 } else if (strcmp(qualifier
->value(), "invariant") == 0) {
420 } else if (strcmp(qualifier
->value(), "uniform") == 0) {
421 var
->mode
= ir_var_uniform
;
422 } else if (strcmp(qualifier
->value(), "auto") == 0) {
423 var
->mode
= ir_var_auto
;
424 } else if (strcmp(qualifier
->value(), "in") == 0) {
425 var
->mode
= ir_var_function_in
;
426 } else if (strcmp(qualifier
->value(), "shader_in") == 0) {
427 var
->mode
= ir_var_shader_in
;
428 } else if (strcmp(qualifier
->value(), "const_in") == 0) {
429 var
->mode
= ir_var_const_in
;
430 } else if (strcmp(qualifier
->value(), "out") == 0) {
431 var
->mode
= ir_var_function_out
;
432 } else if (strcmp(qualifier
->value(), "shader_out") == 0) {
433 var
->mode
= ir_var_shader_out
;
434 } else if (strcmp(qualifier
->value(), "inout") == 0) {
435 var
->mode
= ir_var_function_inout
;
436 } else if (strcmp(qualifier
->value(), "temporary") == 0) {
437 var
->mode
= ir_var_temporary
;
438 } else if (strcmp(qualifier
->value(), "smooth") == 0) {
439 var
->interpolation
= INTERP_QUALIFIER_SMOOTH
;
440 } else if (strcmp(qualifier
->value(), "flat") == 0) {
441 var
->interpolation
= INTERP_QUALIFIER_FLAT
;
442 } else if (strcmp(qualifier
->value(), "noperspective") == 0) {
443 var
->interpolation
= INTERP_QUALIFIER_NOPERSPECTIVE
;
445 ir_read_error(expr
, "unknown qualifier: %s", qualifier
->value());
450 // Add the variable to the symbol table
451 state
->symbols
->add_variable(var
);
458 ir_reader::read_if(s_expression
*expr
, ir_loop
*loop_ctx
)
460 s_expression
*s_cond
;
461 s_expression
*s_then
;
462 s_expression
*s_else
;
464 s_pattern pat
[] = { "if", s_cond
, s_then
, s_else
};
465 if (!MATCH(expr
, pat
)) {
466 ir_read_error(expr
, "expected (if <condition> (<then>...) (<else>...))");
470 ir_rvalue
*condition
= read_rvalue(s_cond
);
471 if (condition
== NULL
) {
472 ir_read_error(NULL
, "when reading condition of (if ...)");
476 ir_if
*iff
= new(mem_ctx
) ir_if(condition
);
478 read_instructions(&iff
->then_instructions
, s_then
, loop_ctx
);
479 read_instructions(&iff
->else_instructions
, s_else
, loop_ctx
);
489 ir_reader::read_loop(s_expression
*expr
)
491 s_expression
*s_body
;
493 s_pattern loop_pat
[] = { "loop", s_body
};
494 if (!MATCH(expr
, loop_pat
)) {
495 ir_read_error(expr
, "expected (loop <body>)");
499 ir_loop
*loop
= new(mem_ctx
) ir_loop
;
501 read_instructions(&loop
->body_instructions
, s_body
, loop
);
511 ir_reader::read_return(s_expression
*expr
)
513 s_expression
*s_retval
;
515 s_pattern return_value_pat
[] = { "return", s_retval
};
516 s_pattern return_void_pat
[] = { "return" };
517 if (MATCH(expr
, return_value_pat
)) {
518 ir_rvalue
*retval
= read_rvalue(s_retval
);
519 if (retval
== NULL
) {
520 ir_read_error(NULL
, "when reading return value");
523 return new(mem_ctx
) ir_return(retval
);
524 } else if (MATCH(expr
, return_void_pat
)) {
525 return new(mem_ctx
) ir_return
;
527 ir_read_error(expr
, "expected (return <rvalue>) or (return)");
534 ir_reader::read_rvalue(s_expression
*expr
)
536 s_list
*list
= SX_AS_LIST(expr
);
537 if (list
== NULL
|| list
->subexpressions
.is_empty())
540 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
542 ir_read_error(expr
, "expected rvalue tag");
546 ir_rvalue
*rvalue
= read_dereference(list
);
547 if (rvalue
!= NULL
|| state
->error
)
549 else if (strcmp(tag
->value(), "swiz") == 0) {
550 rvalue
= read_swizzle(list
);
551 } else if (strcmp(tag
->value(), "expression") == 0) {
552 rvalue
= read_expression(list
);
553 } else if (strcmp(tag
->value(), "constant") == 0) {
554 rvalue
= read_constant(list
);
556 rvalue
= read_texture(list
);
557 if (rvalue
== NULL
&& !state
->error
)
558 ir_read_error(expr
, "unrecognized rvalue tag: %s", tag
->value());
565 ir_reader::read_assignment(s_expression
*expr
)
567 s_expression
*cond_expr
= NULL
;
568 s_expression
*lhs_expr
, *rhs_expr
;
571 s_pattern pat4
[] = { "assign", mask_list
, lhs_expr
, rhs_expr
};
572 s_pattern pat5
[] = { "assign", cond_expr
, mask_list
, lhs_expr
, rhs_expr
};
573 if (!MATCH(expr
, pat4
) && !MATCH(expr
, pat5
)) {
574 ir_read_error(expr
, "expected (assign [<condition>] (<write mask>) "
579 ir_rvalue
*condition
= NULL
;
580 if (cond_expr
!= NULL
) {
581 condition
= read_rvalue(cond_expr
);
582 if (condition
== NULL
) {
583 ir_read_error(NULL
, "when reading condition of assignment");
590 s_symbol
*mask_symbol
;
591 s_pattern mask_pat
[] = { mask_symbol
};
592 if (MATCH(mask_list
, mask_pat
)) {
593 const char *mask_str
= mask_symbol
->value();
594 unsigned mask_length
= strlen(mask_str
);
595 if (mask_length
> 4) {
596 ir_read_error(expr
, "invalid write mask: %s", mask_str
);
600 const unsigned idx_map
[] = { 3, 0, 1, 2 }; /* w=bit 3, x=0, y=1, z=2 */
602 for (unsigned i
= 0; i
< mask_length
; i
++) {
603 if (mask_str
[i
] < 'w' || mask_str
[i
] > 'z') {
604 ir_read_error(expr
, "write mask contains invalid character: %c",
608 mask
|= 1 << idx_map
[mask_str
[i
] - 'w'];
610 } else if (!mask_list
->subexpressions
.is_empty()) {
611 ir_read_error(mask_list
, "expected () or (<write mask>)");
615 ir_dereference
*lhs
= read_dereference(lhs_expr
);
617 ir_read_error(NULL
, "when reading left-hand side of assignment");
621 ir_rvalue
*rhs
= read_rvalue(rhs_expr
);
623 ir_read_error(NULL
, "when reading right-hand side of assignment");
627 if (mask
== 0 && (lhs
->type
->is_vector() || lhs
->type
->is_scalar())) {
628 ir_read_error(expr
, "non-zero write mask required.");
632 return new(mem_ctx
) ir_assignment(lhs
, rhs
, condition
, mask
);
636 ir_reader::read_call(s_expression
*expr
)
640 s_list
*s_return
= NULL
;
642 ir_dereference_variable
*return_deref
= NULL
;
644 s_pattern void_pat
[] = { "call", name
, params
};
645 s_pattern non_void_pat
[] = { "call", name
, s_return
, params
};
646 if (MATCH(expr
, non_void_pat
)) {
647 return_deref
= read_var_ref(s_return
);
648 if (return_deref
== NULL
) {
649 ir_read_error(s_return
, "when reading a call's return storage");
652 } else if (!MATCH(expr
, void_pat
)) {
653 ir_read_error(expr
, "expected (call <name> [<deref>] (<param> ...))");
657 exec_list parameters
;
659 foreach_iter(exec_list_iterator
, it
, params
->subexpressions
) {
660 s_expression
*expr
= (s_expression
*) it
.get();
661 ir_rvalue
*param
= read_rvalue(expr
);
663 ir_read_error(expr
, "when reading parameter to function call");
666 parameters
.push_tail(param
);
669 ir_function
*f
= state
->symbols
->get_function(name
->value());
671 ir_read_error(expr
, "found call to undefined function %s",
676 ir_function_signature
*callee
= f
->matching_signature(state
, ¶meters
);
677 if (callee
== NULL
) {
678 ir_read_error(expr
, "couldn't find matching signature for function "
679 "%s", name
->value());
683 if (callee
->return_type
== glsl_type::void_type
&& return_deref
) {
684 ir_read_error(expr
, "call has return value storage but void type");
686 } else if (callee
->return_type
!= glsl_type::void_type
&& !return_deref
) {
687 ir_read_error(expr
, "call has non-void type but no return value storage");
691 return new(mem_ctx
) ir_call(callee
, return_deref
, ¶meters
);
695 ir_reader::read_expression(s_expression
*expr
)
697 s_expression
*s_type
;
699 s_expression
*s_arg
[4] = {NULL
};
701 s_pattern pat
[] = { "expression", s_type
, s_op
, s_arg
[0] };
702 if (!PARTIAL_MATCH(expr
, pat
)) {
703 ir_read_error(expr
, "expected (expression <type> <operator> "
704 "<operand> [<operand>] [<operand>] [<operand>])");
707 s_arg
[1] = (s_expression
*) s_arg
[0]->next
; // may be tail sentinel
708 s_arg
[2] = (s_expression
*) s_arg
[1]->next
; // may be tail sentinel or NULL
710 s_arg
[3] = (s_expression
*) s_arg
[2]->next
; // may be tail sentinel or NULL
712 const glsl_type
*type
= read_type(s_type
);
716 /* Read the operator */
717 ir_expression_operation op
= ir_expression::get_operator(s_op
->value());
718 if (op
== (ir_expression_operation
) -1) {
719 ir_read_error(expr
, "invalid operator: %s", s_op
->value());
723 int num_operands
= -3; /* skip "expression" <type> <operation> */
724 foreach_list(n
, &((s_list
*) expr
)->subexpressions
)
727 int expected_operands
= ir_expression::get_num_operands(op
);
728 if (num_operands
!= expected_operands
) {
729 ir_read_error(expr
, "found %d expression operands, expected %d",
730 num_operands
, expected_operands
);
734 ir_rvalue
*arg
[4] = {NULL
};
735 for (int i
= 0; i
< num_operands
; i
++) {
736 arg
[i
] = read_rvalue(s_arg
[i
]);
737 if (arg
[i
] == NULL
) {
738 ir_read_error(NULL
, "when reading operand #%d of %s", i
, s_op
->value());
743 return new(mem_ctx
) ir_expression(op
, type
, arg
[0], arg
[1], arg
[2], arg
[3]);
747 ir_reader::read_swizzle(s_expression
*expr
)
752 s_pattern pat
[] = { "swiz", swiz
, sub
};
753 if (!MATCH(expr
, pat
)) {
754 ir_read_error(expr
, "expected (swiz <swizzle> <rvalue>)");
758 if (strlen(swiz
->value()) > 4) {
759 ir_read_error(expr
, "expected a valid swizzle; found %s", swiz
->value());
763 ir_rvalue
*rvalue
= read_rvalue(sub
);
767 ir_swizzle
*ir
= ir_swizzle::create(rvalue
, swiz
->value(),
768 rvalue
->type
->vector_elements
);
770 ir_read_error(expr
, "invalid swizzle");
776 ir_reader::read_constant(s_expression
*expr
)
778 s_expression
*type_expr
;
781 s_pattern pat
[] = { "constant", type_expr
, values
};
782 if (!MATCH(expr
, pat
)) {
783 ir_read_error(expr
, "expected (constant <type> (...))");
787 const glsl_type
*type
= read_type(type_expr
);
791 if (values
== NULL
) {
792 ir_read_error(expr
, "expected (constant <type> (...))");
796 if (type
->is_array()) {
797 unsigned elements_supplied
= 0;
799 foreach_iter(exec_list_iterator
, it
, values
->subexpressions
) {
800 s_expression
*elt
= (s_expression
*) it
.get();
801 ir_constant
*ir_elt
= read_constant(elt
);
804 elements
.push_tail(ir_elt
);
808 if (elements_supplied
!= type
->length
) {
809 ir_read_error(values
, "expected exactly %u array elements, "
810 "given %u", type
->length
, elements_supplied
);
813 return new(mem_ctx
) ir_constant(type
, &elements
);
816 ir_constant_data data
= { { 0 } };
818 // Read in list of values (at most 16).
820 foreach_iter(exec_list_iterator
, it
, values
->subexpressions
) {
822 ir_read_error(values
, "expected at most 16 numbers");
826 s_expression
*expr
= (s_expression
*) it
.get();
828 if (type
->base_type
== GLSL_TYPE_FLOAT
) {
829 s_number
*value
= SX_AS_NUMBER(expr
);
831 ir_read_error(values
, "expected numbers");
834 data
.f
[k
] = value
->fvalue();
836 s_int
*value
= SX_AS_INT(expr
);
838 ir_read_error(values
, "expected integers");
842 switch (type
->base_type
) {
843 case GLSL_TYPE_UINT
: {
844 data
.u
[k
] = value
->value();
847 case GLSL_TYPE_INT
: {
848 data
.i
[k
] = value
->value();
851 case GLSL_TYPE_BOOL
: {
852 data
.b
[k
] = value
->value();
856 ir_read_error(values
, "unsupported constant type");
862 if (k
!= type
->components()) {
863 ir_read_error(values
, "expected %u constant values, found %u",
864 type
->components(), k
);
868 return new(mem_ctx
) ir_constant(type
, &data
);
871 ir_dereference_variable
*
872 ir_reader::read_var_ref(s_expression
*expr
)
875 s_pattern var_pat
[] = { "var_ref", s_var
};
877 if (MATCH(expr
, var_pat
)) {
878 ir_variable
*var
= state
->symbols
->get_variable(s_var
->value());
880 ir_read_error(expr
, "undeclared variable: %s", s_var
->value());
883 return new(mem_ctx
) ir_dereference_variable(var
);
889 ir_reader::read_dereference(s_expression
*expr
)
891 s_expression
*s_subject
;
892 s_expression
*s_index
;
895 s_pattern array_pat
[] = { "array_ref", s_subject
, s_index
};
896 s_pattern record_pat
[] = { "record_ref", s_subject
, s_field
};
898 ir_dereference_variable
*var_ref
= read_var_ref(expr
);
899 if (var_ref
!= NULL
) {
901 } else if (MATCH(expr
, array_pat
)) {
902 ir_rvalue
*subject
= read_rvalue(s_subject
);
903 if (subject
== NULL
) {
904 ir_read_error(NULL
, "when reading the subject of an array_ref");
908 ir_rvalue
*idx
= read_rvalue(s_index
);
910 ir_read_error(NULL
, "when reading the index of an array_ref");
913 return new(mem_ctx
) ir_dereference_array(subject
, idx
);
914 } else if (MATCH(expr
, record_pat
)) {
915 ir_rvalue
*subject
= read_rvalue(s_subject
);
916 if (subject
== NULL
) {
917 ir_read_error(NULL
, "when reading the subject of a record_ref");
920 return new(mem_ctx
) ir_dereference_record(subject
, s_field
->value());
926 ir_reader::read_texture(s_expression
*expr
)
928 s_symbol
*tag
= NULL
;
929 s_expression
*s_type
= NULL
;
930 s_expression
*s_sampler
= NULL
;
931 s_expression
*s_coord
= NULL
;
932 s_expression
*s_offset
= NULL
;
933 s_expression
*s_proj
= NULL
;
934 s_list
*s_shadow
= NULL
;
935 s_expression
*s_lod
= NULL
;
936 s_expression
*s_sample_index
= NULL
;
937 s_expression
*s_component
= NULL
;
939 ir_texture_opcode op
= ir_tex
; /* silence warning */
941 s_pattern tex_pattern
[] =
942 { "tex", s_type
, s_sampler
, s_coord
, s_offset
, s_proj
, s_shadow
};
943 s_pattern lod_pattern
[] =
944 { "lod", s_type
, s_sampler
, s_coord
};
945 s_pattern txf_pattern
[] =
946 { "txf", s_type
, s_sampler
, s_coord
, s_offset
, s_lod
};
947 s_pattern txf_ms_pattern
[] =
948 { "txf_ms", s_type
, s_sampler
, s_coord
, s_sample_index
};
949 s_pattern txs_pattern
[] =
950 { "txs", s_type
, s_sampler
, s_lod
};
951 s_pattern tg4_pattern
[] =
952 { "tg4", s_type
, s_sampler
, s_coord
, s_offset
, s_component
};
953 s_pattern query_levels_pattern
[] =
954 { "query_levels", s_type
, s_sampler
};
955 s_pattern other_pattern
[] =
956 { tag
, s_type
, s_sampler
, s_coord
, s_offset
, s_proj
, s_shadow
, s_lod
};
958 if (MATCH(expr
, lod_pattern
)) {
960 } else if (MATCH(expr
, tex_pattern
)) {
962 } else if (MATCH(expr
, txf_pattern
)) {
964 } else if (MATCH(expr
, txf_ms_pattern
)) {
966 } else if (MATCH(expr
, txs_pattern
)) {
968 } else if (MATCH(expr
, tg4_pattern
)) {
970 } else if (MATCH(expr
, query_levels_pattern
)) {
971 op
= ir_query_levels
;
972 } else if (MATCH(expr
, other_pattern
)) {
973 op
= ir_texture::get_opcode(tag
->value());
977 ir_read_error(NULL
, "unexpected texture pattern %s", tag
->value());
981 ir_texture
*tex
= new(mem_ctx
) ir_texture(op
);
984 const glsl_type
*type
= read_type(s_type
);
986 ir_read_error(NULL
, "when reading type in (%s ...)",
987 tex
->opcode_string());
991 // Read sampler (must be a deref)
992 ir_dereference
*sampler
= read_dereference(s_sampler
);
993 if (sampler
== NULL
) {
994 ir_read_error(NULL
, "when reading sampler in (%s ...)",
995 tex
->opcode_string());
998 tex
->set_sampler(sampler
, type
);
1001 // Read coordinate (any rvalue)
1002 tex
->coordinate
= read_rvalue(s_coord
);
1003 if (tex
->coordinate
== NULL
) {
1004 ir_read_error(NULL
, "when reading coordinate in (%s ...)",
1005 tex
->opcode_string());
1009 if (op
!= ir_txf_ms
&& op
!= ir_lod
) {
1010 // Read texel offset - either 0 or an rvalue.
1011 s_int
*si_offset
= SX_AS_INT(s_offset
);
1012 if (si_offset
== NULL
|| si_offset
->value() != 0) {
1013 tex
->offset
= read_rvalue(s_offset
);
1014 if (tex
->offset
== NULL
) {
1015 ir_read_error(s_offset
, "expected 0 or an expression");
1022 if (op
!= ir_txf
&& op
!= ir_txf_ms
&&
1023 op
!= ir_txs
&& op
!= ir_lod
&& op
!= ir_tg4
&&
1024 op
!= ir_query_levels
) {
1025 s_int
*proj_as_int
= SX_AS_INT(s_proj
);
1026 if (proj_as_int
&& proj_as_int
->value() == 1) {
1027 tex
->projector
= NULL
;
1029 tex
->projector
= read_rvalue(s_proj
);
1030 if (tex
->projector
== NULL
) {
1031 ir_read_error(NULL
, "when reading projective divide in (%s ..)",
1032 tex
->opcode_string());
1037 if (s_shadow
->subexpressions
.is_empty()) {
1038 tex
->shadow_comparitor
= NULL
;
1040 tex
->shadow_comparitor
= read_rvalue(s_shadow
);
1041 if (tex
->shadow_comparitor
== NULL
) {
1042 ir_read_error(NULL
, "when reading shadow comparitor in (%s ..)",
1043 tex
->opcode_string());
1051 tex
->lod_info
.bias
= read_rvalue(s_lod
);
1052 if (tex
->lod_info
.bias
== NULL
) {
1053 ir_read_error(NULL
, "when reading LOD bias in (txb ...)");
1060 tex
->lod_info
.lod
= read_rvalue(s_lod
);
1061 if (tex
->lod_info
.lod
== NULL
) {
1062 ir_read_error(NULL
, "when reading LOD in (%s ...)",
1063 tex
->opcode_string());
1068 tex
->lod_info
.sample_index
= read_rvalue(s_sample_index
);
1069 if (tex
->lod_info
.sample_index
== NULL
) {
1070 ir_read_error(NULL
, "when reading sample_index in (txf_ms ...)");
1075 s_expression
*s_dx
, *s_dy
;
1076 s_pattern dxdy_pat
[] = { s_dx
, s_dy
};
1077 if (!MATCH(s_lod
, dxdy_pat
)) {
1078 ir_read_error(s_lod
, "expected (dPdx dPdy) in (txd ...)");
1081 tex
->lod_info
.grad
.dPdx
= read_rvalue(s_dx
);
1082 if (tex
->lod_info
.grad
.dPdx
== NULL
) {
1083 ir_read_error(NULL
, "when reading dPdx in (txd ...)");
1086 tex
->lod_info
.grad
.dPdy
= read_rvalue(s_dy
);
1087 if (tex
->lod_info
.grad
.dPdy
== NULL
) {
1088 ir_read_error(NULL
, "when reading dPdy in (txd ...)");
1094 tex
->lod_info
.component
= read_rvalue(s_component
);
1095 if (tex
->lod_info
.component
== NULL
) {
1096 ir_read_error(NULL
, "when reading component in (tg4 ...)");
1101 // tex and lod don't have any extra parameters.
1108 ir_reader::read_emit_vertex(s_expression
*expr
)
1110 s_pattern pat
[] = { "emit-vertex" };
1112 if (MATCH(expr
, pat
)) {
1113 return new(mem_ctx
) ir_emit_vertex();
1115 ir_read_error(NULL
, "when reading emit-vertex");
1120 ir_reader::read_end_primitive(s_expression
*expr
)
1122 s_pattern pat
[] = { "end-primitive" };
1124 if (MATCH(expr
, pat
)) {
1125 return new(mem_ctx
) ir_end_primitive();
1127 ir_read_error(NULL
, "when reading end-primitive");