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_counter
, *s_from
, *s_to
, *s_inc
, *s_body
;
493 s_pattern pat
[] = { "loop", s_counter
, s_from
, s_to
, s_inc
, s_body
};
494 if (!MATCH(expr
, pat
)) {
495 ir_read_error(expr
, "expected (loop <counter> <from> <to> "
496 "<increment> <body>)");
500 // FINISHME: actually read the count/from/to fields.
502 ir_loop
*loop
= new(mem_ctx
) ir_loop
;
503 read_instructions(&loop
->body_instructions
, s_body
, loop
);
513 ir_reader::read_return(s_expression
*expr
)
515 s_expression
*s_retval
;
517 s_pattern return_value_pat
[] = { "return", s_retval
};
518 s_pattern return_void_pat
[] = { "return" };
519 if (MATCH(expr
, return_value_pat
)) {
520 ir_rvalue
*retval
= read_rvalue(s_retval
);
521 if (retval
== NULL
) {
522 ir_read_error(NULL
, "when reading return value");
525 return new(mem_ctx
) ir_return(retval
);
526 } else if (MATCH(expr
, return_void_pat
)) {
527 return new(mem_ctx
) ir_return
;
529 ir_read_error(expr
, "expected (return <rvalue>) or (return)");
536 ir_reader::read_rvalue(s_expression
*expr
)
538 s_list
*list
= SX_AS_LIST(expr
);
539 if (list
== NULL
|| list
->subexpressions
.is_empty())
542 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
544 ir_read_error(expr
, "expected rvalue tag");
548 ir_rvalue
*rvalue
= read_dereference(list
);
549 if (rvalue
!= NULL
|| state
->error
)
551 else if (strcmp(tag
->value(), "swiz") == 0) {
552 rvalue
= read_swizzle(list
);
553 } else if (strcmp(tag
->value(), "expression") == 0) {
554 rvalue
= read_expression(list
);
555 } else if (strcmp(tag
->value(), "constant") == 0) {
556 rvalue
= read_constant(list
);
558 rvalue
= read_texture(list
);
559 if (rvalue
== NULL
&& !state
->error
)
560 ir_read_error(expr
, "unrecognized rvalue tag: %s", tag
->value());
567 ir_reader::read_assignment(s_expression
*expr
)
569 s_expression
*cond_expr
= NULL
;
570 s_expression
*lhs_expr
, *rhs_expr
;
573 s_pattern pat4
[] = { "assign", mask_list
, lhs_expr
, rhs_expr
};
574 s_pattern pat5
[] = { "assign", cond_expr
, mask_list
, lhs_expr
, rhs_expr
};
575 if (!MATCH(expr
, pat4
) && !MATCH(expr
, pat5
)) {
576 ir_read_error(expr
, "expected (assign [<condition>] (<write mask>) "
581 ir_rvalue
*condition
= NULL
;
582 if (cond_expr
!= NULL
) {
583 condition
= read_rvalue(cond_expr
);
584 if (condition
== NULL
) {
585 ir_read_error(NULL
, "when reading condition of assignment");
592 s_symbol
*mask_symbol
;
593 s_pattern mask_pat
[] = { mask_symbol
};
594 if (MATCH(mask_list
, mask_pat
)) {
595 const char *mask_str
= mask_symbol
->value();
596 unsigned mask_length
= strlen(mask_str
);
597 if (mask_length
> 4) {
598 ir_read_error(expr
, "invalid write mask: %s", mask_str
);
602 const unsigned idx_map
[] = { 3, 0, 1, 2 }; /* w=bit 3, x=0, y=1, z=2 */
604 for (unsigned i
= 0; i
< mask_length
; i
++) {
605 if (mask_str
[i
] < 'w' || mask_str
[i
] > 'z') {
606 ir_read_error(expr
, "write mask contains invalid character: %c",
610 mask
|= 1 << idx_map
[mask_str
[i
] - 'w'];
612 } else if (!mask_list
->subexpressions
.is_empty()) {
613 ir_read_error(mask_list
, "expected () or (<write mask>)");
617 ir_dereference
*lhs
= read_dereference(lhs_expr
);
619 ir_read_error(NULL
, "when reading left-hand side of assignment");
623 ir_rvalue
*rhs
= read_rvalue(rhs_expr
);
625 ir_read_error(NULL
, "when reading right-hand side of assignment");
629 if (mask
== 0 && (lhs
->type
->is_vector() || lhs
->type
->is_scalar())) {
630 ir_read_error(expr
, "non-zero write mask required.");
634 return new(mem_ctx
) ir_assignment(lhs
, rhs
, condition
, mask
);
638 ir_reader::read_call(s_expression
*expr
)
642 s_list
*s_return
= NULL
;
644 ir_dereference_variable
*return_deref
= NULL
;
646 s_pattern void_pat
[] = { "call", name
, params
};
647 s_pattern non_void_pat
[] = { "call", name
, s_return
, params
};
648 if (MATCH(expr
, non_void_pat
)) {
649 return_deref
= read_var_ref(s_return
);
650 if (return_deref
== NULL
) {
651 ir_read_error(s_return
, "when reading a call's return storage");
654 } else if (!MATCH(expr
, void_pat
)) {
655 ir_read_error(expr
, "expected (call <name> [<deref>] (<param> ...))");
659 exec_list parameters
;
661 foreach_iter(exec_list_iterator
, it
, params
->subexpressions
) {
662 s_expression
*expr
= (s_expression
*) it
.get();
663 ir_rvalue
*param
= read_rvalue(expr
);
665 ir_read_error(expr
, "when reading parameter to function call");
668 parameters
.push_tail(param
);
671 ir_function
*f
= state
->symbols
->get_function(name
->value());
673 ir_read_error(expr
, "found call to undefined function %s",
678 ir_function_signature
*callee
= f
->matching_signature(state
, ¶meters
);
679 if (callee
== NULL
) {
680 ir_read_error(expr
, "couldn't find matching signature for function "
681 "%s", name
->value());
685 if (callee
->return_type
== glsl_type::void_type
&& return_deref
) {
686 ir_read_error(expr
, "call has return value storage but void type");
688 } else if (callee
->return_type
!= glsl_type::void_type
&& !return_deref
) {
689 ir_read_error(expr
, "call has non-void type but no return value storage");
693 return new(mem_ctx
) ir_call(callee
, return_deref
, ¶meters
);
697 ir_reader::read_expression(s_expression
*expr
)
699 s_expression
*s_type
;
701 s_expression
*s_arg
[4] = {NULL
};
703 s_pattern pat
[] = { "expression", s_type
, s_op
, s_arg
[0] };
704 if (!PARTIAL_MATCH(expr
, pat
)) {
705 ir_read_error(expr
, "expected (expression <type> <operator> "
706 "<operand> [<operand>] [<operand>] [<operand>])");
709 s_arg
[1] = (s_expression
*) s_arg
[0]->next
; // may be tail sentinel
710 s_arg
[2] = (s_expression
*) s_arg
[1]->next
; // may be tail sentinel or NULL
712 s_arg
[3] = (s_expression
*) s_arg
[2]->next
; // may be tail sentinel or NULL
714 const glsl_type
*type
= read_type(s_type
);
718 /* Read the operator */
719 ir_expression_operation op
= ir_expression::get_operator(s_op
->value());
720 if (op
== (ir_expression_operation
) -1) {
721 ir_read_error(expr
, "invalid operator: %s", s_op
->value());
725 int num_operands
= -3; /* skip "expression" <type> <operation> */
726 foreach_list(n
, &((s_list
*) expr
)->subexpressions
)
729 int expected_operands
= ir_expression::get_num_operands(op
);
730 if (num_operands
!= expected_operands
) {
731 ir_read_error(expr
, "found %d expression operands, expected %d",
732 num_operands
, expected_operands
);
736 ir_rvalue
*arg
[4] = {NULL
};
737 for (int i
= 0; i
< num_operands
; i
++) {
738 arg
[i
] = read_rvalue(s_arg
[i
]);
739 if (arg
[i
] == NULL
) {
740 ir_read_error(NULL
, "when reading operand #%d of %s", i
, s_op
->value());
745 return new(mem_ctx
) ir_expression(op
, type
, arg
[0], arg
[1], arg
[2], arg
[3]);
749 ir_reader::read_swizzle(s_expression
*expr
)
754 s_pattern pat
[] = { "swiz", swiz
, sub
};
755 if (!MATCH(expr
, pat
)) {
756 ir_read_error(expr
, "expected (swiz <swizzle> <rvalue>)");
760 if (strlen(swiz
->value()) > 4) {
761 ir_read_error(expr
, "expected a valid swizzle; found %s", swiz
->value());
765 ir_rvalue
*rvalue
= read_rvalue(sub
);
769 ir_swizzle
*ir
= ir_swizzle::create(rvalue
, swiz
->value(),
770 rvalue
->type
->vector_elements
);
772 ir_read_error(expr
, "invalid swizzle");
778 ir_reader::read_constant(s_expression
*expr
)
780 s_expression
*type_expr
;
783 s_pattern pat
[] = { "constant", type_expr
, values
};
784 if (!MATCH(expr
, pat
)) {
785 ir_read_error(expr
, "expected (constant <type> (...))");
789 const glsl_type
*type
= read_type(type_expr
);
793 if (values
== NULL
) {
794 ir_read_error(expr
, "expected (constant <type> (...))");
798 if (type
->is_array()) {
799 unsigned elements_supplied
= 0;
801 foreach_iter(exec_list_iterator
, it
, values
->subexpressions
) {
802 s_expression
*elt
= (s_expression
*) it
.get();
803 ir_constant
*ir_elt
= read_constant(elt
);
806 elements
.push_tail(ir_elt
);
810 if (elements_supplied
!= type
->length
) {
811 ir_read_error(values
, "expected exactly %u array elements, "
812 "given %u", type
->length
, elements_supplied
);
815 return new(mem_ctx
) ir_constant(type
, &elements
);
818 ir_constant_data data
= { { 0 } };
820 // Read in list of values (at most 16).
822 foreach_iter(exec_list_iterator
, it
, values
->subexpressions
) {
824 ir_read_error(values
, "expected at most 16 numbers");
828 s_expression
*expr
= (s_expression
*) it
.get();
830 if (type
->base_type
== GLSL_TYPE_FLOAT
) {
831 s_number
*value
= SX_AS_NUMBER(expr
);
833 ir_read_error(values
, "expected numbers");
836 data
.f
[k
] = value
->fvalue();
838 s_int
*value
= SX_AS_INT(expr
);
840 ir_read_error(values
, "expected integers");
844 switch (type
->base_type
) {
845 case GLSL_TYPE_UINT
: {
846 data
.u
[k
] = value
->value();
849 case GLSL_TYPE_INT
: {
850 data
.i
[k
] = value
->value();
853 case GLSL_TYPE_BOOL
: {
854 data
.b
[k
] = value
->value();
858 ir_read_error(values
, "unsupported constant type");
864 if (k
!= type
->components()) {
865 ir_read_error(values
, "expected %u constant values, found %u",
866 type
->components(), k
);
870 return new(mem_ctx
) ir_constant(type
, &data
);
873 ir_dereference_variable
*
874 ir_reader::read_var_ref(s_expression
*expr
)
877 s_pattern var_pat
[] = { "var_ref", s_var
};
879 if (MATCH(expr
, var_pat
)) {
880 ir_variable
*var
= state
->symbols
->get_variable(s_var
->value());
882 ir_read_error(expr
, "undeclared variable: %s", s_var
->value());
885 return new(mem_ctx
) ir_dereference_variable(var
);
891 ir_reader::read_dereference(s_expression
*expr
)
893 s_expression
*s_subject
;
894 s_expression
*s_index
;
897 s_pattern array_pat
[] = { "array_ref", s_subject
, s_index
};
898 s_pattern record_pat
[] = { "record_ref", s_subject
, s_field
};
900 ir_dereference_variable
*var_ref
= read_var_ref(expr
);
901 if (var_ref
!= NULL
) {
903 } else if (MATCH(expr
, array_pat
)) {
904 ir_rvalue
*subject
= read_rvalue(s_subject
);
905 if (subject
== NULL
) {
906 ir_read_error(NULL
, "when reading the subject of an array_ref");
910 ir_rvalue
*idx
= read_rvalue(s_index
);
912 ir_read_error(NULL
, "when reading the index of an array_ref");
915 return new(mem_ctx
) ir_dereference_array(subject
, idx
);
916 } else if (MATCH(expr
, record_pat
)) {
917 ir_rvalue
*subject
= read_rvalue(s_subject
);
918 if (subject
== NULL
) {
919 ir_read_error(NULL
, "when reading the subject of a record_ref");
922 return new(mem_ctx
) ir_dereference_record(subject
, s_field
->value());
928 ir_reader::read_texture(s_expression
*expr
)
930 s_symbol
*tag
= NULL
;
931 s_expression
*s_type
= NULL
;
932 s_expression
*s_sampler
= NULL
;
933 s_expression
*s_coord
= NULL
;
934 s_expression
*s_offset
= NULL
;
935 s_expression
*s_proj
= NULL
;
936 s_list
*s_shadow
= NULL
;
937 s_expression
*s_lod
= NULL
;
938 s_expression
*s_sample_index
= NULL
;
939 s_expression
*s_component
= NULL
;
941 ir_texture_opcode op
= ir_tex
; /* silence warning */
943 s_pattern tex_pattern
[] =
944 { "tex", s_type
, s_sampler
, s_coord
, s_offset
, s_proj
, s_shadow
};
945 s_pattern lod_pattern
[] =
946 { "lod", s_type
, s_sampler
, s_coord
};
947 s_pattern txf_pattern
[] =
948 { "txf", s_type
, s_sampler
, s_coord
, s_offset
, s_lod
};
949 s_pattern txf_ms_pattern
[] =
950 { "txf_ms", s_type
, s_sampler
, s_coord
, s_sample_index
};
951 s_pattern txs_pattern
[] =
952 { "txs", s_type
, s_sampler
, s_lod
};
953 s_pattern tg4_pattern
[] =
954 { "tg4", s_type
, s_sampler
, s_coord
, s_offset
, s_component
};
955 s_pattern query_levels_pattern
[] =
956 { "query_levels", s_type
, s_sampler
};
957 s_pattern other_pattern
[] =
958 { tag
, s_type
, s_sampler
, s_coord
, s_offset
, s_proj
, s_shadow
, s_lod
};
960 if (MATCH(expr
, lod_pattern
)) {
962 } else if (MATCH(expr
, tex_pattern
)) {
964 } else if (MATCH(expr
, txf_pattern
)) {
966 } else if (MATCH(expr
, txf_ms_pattern
)) {
968 } else if (MATCH(expr
, txs_pattern
)) {
970 } else if (MATCH(expr
, tg4_pattern
)) {
972 } else if (MATCH(expr
, query_levels_pattern
)) {
973 op
= ir_query_levels
;
974 } else if (MATCH(expr
, other_pattern
)) {
975 op
= ir_texture::get_opcode(tag
->value());
979 ir_read_error(NULL
, "unexpected texture pattern %s", tag
->value());
983 ir_texture
*tex
= new(mem_ctx
) ir_texture(op
);
986 const glsl_type
*type
= read_type(s_type
);
988 ir_read_error(NULL
, "when reading type in (%s ...)",
989 tex
->opcode_string());
993 // Read sampler (must be a deref)
994 ir_dereference
*sampler
= read_dereference(s_sampler
);
995 if (sampler
== NULL
) {
996 ir_read_error(NULL
, "when reading sampler in (%s ...)",
997 tex
->opcode_string());
1000 tex
->set_sampler(sampler
, type
);
1003 // Read coordinate (any rvalue)
1004 tex
->coordinate
= read_rvalue(s_coord
);
1005 if (tex
->coordinate
== NULL
) {
1006 ir_read_error(NULL
, "when reading coordinate in (%s ...)",
1007 tex
->opcode_string());
1011 if (op
!= ir_txf_ms
&& op
!= ir_lod
) {
1012 // Read texel offset - either 0 or an rvalue.
1013 s_int
*si_offset
= SX_AS_INT(s_offset
);
1014 if (si_offset
== NULL
|| si_offset
->value() != 0) {
1015 tex
->offset
= read_rvalue(s_offset
);
1016 if (tex
->offset
== NULL
) {
1017 ir_read_error(s_offset
, "expected 0 or an expression");
1024 if (op
!= ir_txf
&& op
!= ir_txf_ms
&&
1025 op
!= ir_txs
&& op
!= ir_lod
&& op
!= ir_tg4
&&
1026 op
!= ir_query_levels
) {
1027 s_int
*proj_as_int
= SX_AS_INT(s_proj
);
1028 if (proj_as_int
&& proj_as_int
->value() == 1) {
1029 tex
->projector
= NULL
;
1031 tex
->projector
= read_rvalue(s_proj
);
1032 if (tex
->projector
== NULL
) {
1033 ir_read_error(NULL
, "when reading projective divide in (%s ..)",
1034 tex
->opcode_string());
1039 if (s_shadow
->subexpressions
.is_empty()) {
1040 tex
->shadow_comparitor
= NULL
;
1042 tex
->shadow_comparitor
= read_rvalue(s_shadow
);
1043 if (tex
->shadow_comparitor
== NULL
) {
1044 ir_read_error(NULL
, "when reading shadow comparitor in (%s ..)",
1045 tex
->opcode_string());
1053 tex
->lod_info
.bias
= read_rvalue(s_lod
);
1054 if (tex
->lod_info
.bias
== NULL
) {
1055 ir_read_error(NULL
, "when reading LOD bias in (txb ...)");
1062 tex
->lod_info
.lod
= read_rvalue(s_lod
);
1063 if (tex
->lod_info
.lod
== NULL
) {
1064 ir_read_error(NULL
, "when reading LOD in (%s ...)",
1065 tex
->opcode_string());
1070 tex
->lod_info
.sample_index
= read_rvalue(s_sample_index
);
1071 if (tex
->lod_info
.sample_index
== NULL
) {
1072 ir_read_error(NULL
, "when reading sample_index in (txf_ms ...)");
1077 s_expression
*s_dx
, *s_dy
;
1078 s_pattern dxdy_pat
[] = { s_dx
, s_dy
};
1079 if (!MATCH(s_lod
, dxdy_pat
)) {
1080 ir_read_error(s_lod
, "expected (dPdx dPdy) in (txd ...)");
1083 tex
->lod_info
.grad
.dPdx
= read_rvalue(s_dx
);
1084 if (tex
->lod_info
.grad
.dPdx
== NULL
) {
1085 ir_read_error(NULL
, "when reading dPdx in (txd ...)");
1088 tex
->lod_info
.grad
.dPdy
= read_rvalue(s_dy
);
1089 if (tex
->lod_info
.grad
.dPdy
== NULL
) {
1090 ir_read_error(NULL
, "when reading dPdy in (txd ...)");
1096 tex
->lod_info
.component
= read_rvalue(s_component
);
1097 if (tex
->lod_info
.component
== NULL
) {
1098 ir_read_error(NULL
, "when reading component in (tg4 ...)");
1103 // tex and lod don't have any extra parameters.
1110 ir_reader::read_emit_vertex(s_expression
*expr
)
1112 s_pattern pat
[] = { "emit-vertex" };
1114 if (MATCH(expr
, pat
)) {
1115 return new(mem_ctx
) ir_emit_vertex();
1117 ir_read_error(NULL
, "when reading emit-vertex");
1122 ir_reader::read_end_primitive(s_expression
*expr
)
1124 s_pattern pat
[] = { "end-primitive" };
1126 if (MATCH(expr
, pat
)) {
1127 return new(mem_ctx
) ir_end_primitive();
1129 ir_read_error(NULL
, "when reading end-primitive");