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
*);
66 ir_barrier
*read_barrier(s_expression
*);
68 ir_dereference
*read_dereference(s_expression
*);
69 ir_dereference_variable
*read_var_ref(s_expression
*);
72 } /* anonymous namespace */
74 ir_reader::ir_reader(_mesa_glsl_parse_state
*state
) : state(state
)
76 this->mem_ctx
= state
;
80 _mesa_glsl_read_ir(_mesa_glsl_parse_state
*state
, exec_list
*instructions
,
81 const char *src
, bool scan_for_protos
)
84 r
.read(instructions
, src
, scan_for_protos
);
88 ir_reader::read(exec_list
*instructions
, const char *src
, bool scan_for_protos
)
90 void *sx_mem_ctx
= ralloc_context(NULL
);
91 s_expression
*expr
= s_expression::read_expression(sx_mem_ctx
, src
);
93 ir_read_error(NULL
, "couldn't parse S-Expression.");
97 if (scan_for_protos
) {
98 scan_for_prototypes(instructions
, expr
);
103 read_instructions(instructions
, expr
, NULL
);
104 ralloc_free(sx_mem_ctx
);
107 validate_ir_tree(instructions
);
111 ir_reader::ir_read_error(s_expression
*expr
, const char *fmt
, ...)
117 if (state
->current_function
!= NULL
)
118 ralloc_asprintf_append(&state
->info_log
, "In function %s:\n",
119 state
->current_function
->function_name());
120 ralloc_strcat(&state
->info_log
, "error: ");
123 ralloc_vasprintf_append(&state
->info_log
, fmt
, ap
);
125 ralloc_strcat(&state
->info_log
, "\n");
128 ralloc_strcat(&state
->info_log
, "...in this context:\n ");
130 ralloc_strcat(&state
->info_log
, "\n\n");
135 ir_reader::read_type(s_expression
*expr
)
137 s_expression
*s_base_type
;
140 s_pattern pat
[] = { "array", s_base_type
, s_size
};
141 if (MATCH(expr
, pat
)) {
142 const glsl_type
*base_type
= read_type(s_base_type
);
143 if (base_type
== NULL
) {
144 ir_read_error(NULL
, "when reading base type of array type");
148 return glsl_type::get_array_instance(base_type
, s_size
->value());
151 s_symbol
*type_sym
= SX_AS_SYMBOL(expr
);
152 if (type_sym
== NULL
) {
153 ir_read_error(expr
, "expected <type>");
157 const glsl_type
*type
= state
->symbols
->get_type(type_sym
->value());
159 ir_read_error(expr
, "invalid type: %s", type_sym
->value());
166 ir_reader::scan_for_prototypes(exec_list
*instructions
, s_expression
*expr
)
168 s_list
*list
= SX_AS_LIST(expr
);
170 ir_read_error(expr
, "Expected (<instruction> ...); found an atom.");
174 foreach_in_list(s_list
, sub
, &list
->subexpressions
) {
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 /* Skip over "function" tag and function name (which are guaranteed to be
209 * present by the above PARTIAL_MATCH call).
211 exec_node
*node
= ((s_list
*) expr
)->subexpressions
.head
->next
->next
;
212 for (/* nothing */; !node
->is_tail_sentinel(); node
= node
->next
) {
213 s_expression
*s_sig
= (s_expression
*) node
;
214 read_function_sig(f
, s_sig
, skip_body
);
216 return added
? f
: NULL
;
220 always_available(const _mesa_glsl_parse_state
*)
226 ir_reader::read_function_sig(ir_function
*f
, s_expression
*expr
, bool skip_body
)
228 s_expression
*type_expr
;
232 s_pattern pat
[] = { "signature", type_expr
, paramlist
, body_list
};
233 if (!MATCH(expr
, pat
)) {
234 ir_read_error(expr
, "Expected (signature <type> (parameters ...) "
235 "(<instruction> ...))");
239 const glsl_type
*return_type
= read_type(type_expr
);
240 if (return_type
== NULL
)
243 s_symbol
*paramtag
= SX_AS_SYMBOL(paramlist
->subexpressions
.get_head());
244 if (paramtag
== NULL
|| strcmp(paramtag
->value(), "parameters") != 0) {
245 ir_read_error(paramlist
, "Expected (parameters ...)");
249 // Read the parameters list into a temporary place.
250 exec_list hir_parameters
;
251 state
->symbols
->push_scope();
253 /* Skip over the "parameters" tag. */
254 exec_node
*node
= paramlist
->subexpressions
.head
->next
;
255 for (/* nothing */; !node
->is_tail_sentinel(); node
= node
->next
) {
256 ir_variable
*var
= read_declaration((s_expression
*) node
);
260 hir_parameters
.push_tail(var
);
263 ir_function_signature
*sig
=
264 f
->exact_matching_signature(state
, &hir_parameters
);
265 if (sig
== NULL
&& skip_body
) {
266 /* If scanning for prototypes, generate a new signature. */
267 /* ir_reader doesn't know what languages support a given built-in, so
268 * just say that they're always available. For now, other mechanisms
269 * guarantee the right built-ins are available.
271 sig
= new(mem_ctx
) ir_function_signature(return_type
, always_available
);
272 f
->add_signature(sig
);
273 } else if (sig
!= NULL
) {
274 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
275 if (badvar
!= NULL
) {
276 ir_read_error(expr
, "function `%s' parameter `%s' qualifiers "
277 "don't match prototype", f
->name
, badvar
);
281 if (sig
->return_type
!= return_type
) {
282 ir_read_error(expr
, "function `%s' return type doesn't "
283 "match prototype", f
->name
);
287 /* No prototype for this body exists - skip it. */
288 state
->symbols
->pop_scope();
293 sig
->replace_parameters(&hir_parameters
);
295 if (!skip_body
&& !body_list
->subexpressions
.is_empty()) {
296 if (sig
->is_defined
) {
297 ir_read_error(expr
, "function %s redefined", f
->name
);
300 state
->current_function
= sig
;
301 read_instructions(&sig
->body
, body_list
, NULL
);
302 state
->current_function
= NULL
;
303 sig
->is_defined
= true;
306 state
->symbols
->pop_scope();
310 ir_reader::read_instructions(exec_list
*instructions
, s_expression
*expr
,
313 // Read in a list of instructions
314 s_list
*list
= SX_AS_LIST(expr
);
316 ir_read_error(expr
, "Expected (<instruction> ...); found an atom.");
320 foreach_in_list(s_expression
, sub
, &list
->subexpressions
) {
321 ir_instruction
*ir
= read_instruction(sub
, loop_ctx
);
323 /* Global variable declarations should be moved to the top, before
324 * any functions that might use them. Functions are added to the
325 * instruction stream when scanning for prototypes, so without this
326 * hack, they always appear before variable declarations.
328 if (state
->current_function
== NULL
&& ir
->as_variable() != NULL
)
329 instructions
->push_head(ir
);
331 instructions
->push_tail(ir
);
338 ir_reader::read_instruction(s_expression
*expr
, ir_loop
*loop_ctx
)
340 s_symbol
*symbol
= SX_AS_SYMBOL(expr
);
341 if (symbol
!= NULL
) {
342 if (strcmp(symbol
->value(), "break") == 0 && loop_ctx
!= NULL
)
343 return new(mem_ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
344 if (strcmp(symbol
->value(), "continue") == 0 && loop_ctx
!= NULL
)
345 return new(mem_ctx
) ir_loop_jump(ir_loop_jump::jump_continue
);
348 s_list
*list
= SX_AS_LIST(expr
);
349 if (list
== NULL
|| list
->subexpressions
.is_empty()) {
350 ir_read_error(expr
, "Invalid instruction.\n");
354 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
356 ir_read_error(expr
, "expected instruction tag");
360 ir_instruction
*inst
= NULL
;
361 if (strcmp(tag
->value(), "declare") == 0) {
362 inst
= read_declaration(list
);
363 } else if (strcmp(tag
->value(), "assign") == 0) {
364 inst
= read_assignment(list
);
365 } else if (strcmp(tag
->value(), "if") == 0) {
366 inst
= read_if(list
, loop_ctx
);
367 } else if (strcmp(tag
->value(), "loop") == 0) {
368 inst
= read_loop(list
);
369 } else if (strcmp(tag
->value(), "call") == 0) {
370 inst
= read_call(list
);
371 } else if (strcmp(tag
->value(), "return") == 0) {
372 inst
= read_return(list
);
373 } else if (strcmp(tag
->value(), "function") == 0) {
374 inst
= read_function(list
, false);
375 } else if (strcmp(tag
->value(), "emit-vertex") == 0) {
376 inst
= read_emit_vertex(list
);
377 } else if (strcmp(tag
->value(), "end-primitive") == 0) {
378 inst
= read_end_primitive(list
);
379 } else if (strcmp(tag
->value(), "barrier") == 0) {
380 inst
= read_barrier(list
);
382 inst
= read_rvalue(list
);
384 ir_read_error(NULL
, "when reading instruction");
390 ir_reader::read_declaration(s_expression
*expr
)
393 s_expression
*s_type
;
396 s_pattern pat
[] = { "declare", s_quals
, s_type
, s_name
};
397 if (!MATCH(expr
, pat
)) {
398 ir_read_error(expr
, "expected (declare (<qualifiers>) <type> <name>)");
402 const glsl_type
*type
= read_type(s_type
);
406 ir_variable
*var
= new(mem_ctx
) ir_variable(type
, s_name
->value(),
409 foreach_in_list(s_symbol
, qualifier
, &s_quals
->subexpressions
) {
410 if (!qualifier
->is_symbol()) {
411 ir_read_error(expr
, "qualifier list must contain only symbols");
415 // FINISHME: Check for duplicate/conflicting qualifiers.
416 if (strcmp(qualifier
->value(), "centroid") == 0) {
417 var
->data
.centroid
= 1;
418 } else if (strcmp(qualifier
->value(), "sample") == 0) {
419 var
->data
.sample
= 1;
420 } else if (strcmp(qualifier
->value(), "patch") == 0) {
422 } else if (strcmp(qualifier
->value(), "invariant") == 0) {
423 var
->data
.invariant
= 1;
424 } else if (strcmp(qualifier
->value(), "uniform") == 0) {
425 var
->data
.mode
= ir_var_uniform
;
426 } else if (strcmp(qualifier
->value(), "shader_storage") == 0) {
427 var
->data
.mode
= ir_var_shader_storage
;
428 } else if (strcmp(qualifier
->value(), "auto") == 0) {
429 var
->data
.mode
= ir_var_auto
;
430 } else if (strcmp(qualifier
->value(), "in") == 0) {
431 var
->data
.mode
= ir_var_function_in
;
432 } else if (strcmp(qualifier
->value(), "shader_in") == 0) {
433 var
->data
.mode
= ir_var_shader_in
;
434 } else if (strcmp(qualifier
->value(), "const_in") == 0) {
435 var
->data
.mode
= ir_var_const_in
;
436 } else if (strcmp(qualifier
->value(), "out") == 0) {
437 var
->data
.mode
= ir_var_function_out
;
438 } else if (strcmp(qualifier
->value(), "shader_out") == 0) {
439 var
->data
.mode
= ir_var_shader_out
;
440 } else if (strcmp(qualifier
->value(), "inout") == 0) {
441 var
->data
.mode
= ir_var_function_inout
;
442 } else if (strcmp(qualifier
->value(), "temporary") == 0) {
443 var
->data
.mode
= ir_var_temporary
;
444 } else if (strcmp(qualifier
->value(), "stream1") == 0) {
445 var
->data
.stream
= 1;
446 } else if (strcmp(qualifier
->value(), "stream2") == 0) {
447 var
->data
.stream
= 2;
448 } else if (strcmp(qualifier
->value(), "stream3") == 0) {
449 var
->data
.stream
= 3;
450 } else if (strcmp(qualifier
->value(), "smooth") == 0) {
451 var
->data
.interpolation
= INTERP_QUALIFIER_SMOOTH
;
452 } else if (strcmp(qualifier
->value(), "flat") == 0) {
453 var
->data
.interpolation
= INTERP_QUALIFIER_FLAT
;
454 } else if (strcmp(qualifier
->value(), "noperspective") == 0) {
455 var
->data
.interpolation
= INTERP_QUALIFIER_NOPERSPECTIVE
;
457 ir_read_error(expr
, "unknown qualifier: %s", qualifier
->value());
462 // Add the variable to the symbol table
463 state
->symbols
->add_variable(var
);
470 ir_reader::read_if(s_expression
*expr
, ir_loop
*loop_ctx
)
472 s_expression
*s_cond
;
473 s_expression
*s_then
;
474 s_expression
*s_else
;
476 s_pattern pat
[] = { "if", s_cond
, s_then
, s_else
};
477 if (!MATCH(expr
, pat
)) {
478 ir_read_error(expr
, "expected (if <condition> (<then>...) (<else>...))");
482 ir_rvalue
*condition
= read_rvalue(s_cond
);
483 if (condition
== NULL
) {
484 ir_read_error(NULL
, "when reading condition of (if ...)");
488 ir_if
*iff
= new(mem_ctx
) ir_if(condition
);
490 read_instructions(&iff
->then_instructions
, s_then
, loop_ctx
);
491 read_instructions(&iff
->else_instructions
, s_else
, loop_ctx
);
501 ir_reader::read_loop(s_expression
*expr
)
503 s_expression
*s_body
;
505 s_pattern loop_pat
[] = { "loop", s_body
};
506 if (!MATCH(expr
, loop_pat
)) {
507 ir_read_error(expr
, "expected (loop <body>)");
511 ir_loop
*loop
= new(mem_ctx
) ir_loop
;
513 read_instructions(&loop
->body_instructions
, s_body
, loop
);
523 ir_reader::read_return(s_expression
*expr
)
525 s_expression
*s_retval
;
527 s_pattern return_value_pat
[] = { "return", s_retval
};
528 s_pattern return_void_pat
[] = { "return" };
529 if (MATCH(expr
, return_value_pat
)) {
530 ir_rvalue
*retval
= read_rvalue(s_retval
);
531 if (retval
== NULL
) {
532 ir_read_error(NULL
, "when reading return value");
535 return new(mem_ctx
) ir_return(retval
);
536 } else if (MATCH(expr
, return_void_pat
)) {
537 return new(mem_ctx
) ir_return
;
539 ir_read_error(expr
, "expected (return <rvalue>) or (return)");
546 ir_reader::read_rvalue(s_expression
*expr
)
548 s_list
*list
= SX_AS_LIST(expr
);
549 if (list
== NULL
|| list
->subexpressions
.is_empty())
552 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
554 ir_read_error(expr
, "expected rvalue tag");
558 ir_rvalue
*rvalue
= read_dereference(list
);
559 if (rvalue
!= NULL
|| state
->error
)
561 else if (strcmp(tag
->value(), "swiz") == 0) {
562 rvalue
= read_swizzle(list
);
563 } else if (strcmp(tag
->value(), "expression") == 0) {
564 rvalue
= read_expression(list
);
565 } else if (strcmp(tag
->value(), "constant") == 0) {
566 rvalue
= read_constant(list
);
568 rvalue
= read_texture(list
);
569 if (rvalue
== NULL
&& !state
->error
)
570 ir_read_error(expr
, "unrecognized rvalue tag: %s", tag
->value());
577 ir_reader::read_assignment(s_expression
*expr
)
579 s_expression
*cond_expr
= NULL
;
580 s_expression
*lhs_expr
, *rhs_expr
;
583 s_pattern pat4
[] = { "assign", mask_list
, lhs_expr
, rhs_expr
};
584 s_pattern pat5
[] = { "assign", cond_expr
, mask_list
, lhs_expr
, rhs_expr
};
585 if (!MATCH(expr
, pat4
) && !MATCH(expr
, pat5
)) {
586 ir_read_error(expr
, "expected (assign [<condition>] (<write mask>) "
591 ir_rvalue
*condition
= NULL
;
592 if (cond_expr
!= NULL
) {
593 condition
= read_rvalue(cond_expr
);
594 if (condition
== NULL
) {
595 ir_read_error(NULL
, "when reading condition of assignment");
602 s_symbol
*mask_symbol
;
603 s_pattern mask_pat
[] = { mask_symbol
};
604 if (MATCH(mask_list
, mask_pat
)) {
605 const char *mask_str
= mask_symbol
->value();
606 unsigned mask_length
= strlen(mask_str
);
607 if (mask_length
> 4) {
608 ir_read_error(expr
, "invalid write mask: %s", mask_str
);
612 const unsigned idx_map
[] = { 3, 0, 1, 2 }; /* w=bit 3, x=0, y=1, z=2 */
614 for (unsigned i
= 0; i
< mask_length
; i
++) {
615 if (mask_str
[i
] < 'w' || mask_str
[i
] > 'z') {
616 ir_read_error(expr
, "write mask contains invalid character: %c",
620 mask
|= 1 << idx_map
[mask_str
[i
] - 'w'];
622 } else if (!mask_list
->subexpressions
.is_empty()) {
623 ir_read_error(mask_list
, "expected () or (<write mask>)");
627 ir_dereference
*lhs
= read_dereference(lhs_expr
);
629 ir_read_error(NULL
, "when reading left-hand side of assignment");
633 ir_rvalue
*rhs
= read_rvalue(rhs_expr
);
635 ir_read_error(NULL
, "when reading right-hand side of assignment");
639 if (mask
== 0 && (lhs
->type
->is_vector() || lhs
->type
->is_scalar())) {
640 ir_read_error(expr
, "non-zero write mask required.");
644 return new(mem_ctx
) ir_assignment(lhs
, rhs
, condition
, mask
);
648 ir_reader::read_call(s_expression
*expr
)
652 s_list
*s_return
= NULL
;
654 ir_dereference_variable
*return_deref
= NULL
;
656 s_pattern void_pat
[] = { "call", name
, params
};
657 s_pattern non_void_pat
[] = { "call", name
, s_return
, params
};
658 if (MATCH(expr
, non_void_pat
)) {
659 return_deref
= read_var_ref(s_return
);
660 if (return_deref
== NULL
) {
661 ir_read_error(s_return
, "when reading a call's return storage");
664 } else if (!MATCH(expr
, void_pat
)) {
665 ir_read_error(expr
, "expected (call <name> [<deref>] (<param> ...))");
669 exec_list parameters
;
671 foreach_in_list(s_expression
, e
, ¶ms
->subexpressions
) {
672 ir_rvalue
*param
= read_rvalue(e
);
674 ir_read_error(e
, "when reading parameter to function call");
677 parameters
.push_tail(param
);
680 ir_function
*f
= state
->symbols
->get_function(name
->value());
682 ir_read_error(expr
, "found call to undefined function %s",
687 ir_function_signature
*callee
=
688 f
->matching_signature(state
, ¶meters
, true);
689 if (callee
== NULL
) {
690 ir_read_error(expr
, "couldn't find matching signature for function "
691 "%s", name
->value());
695 if (callee
->return_type
== glsl_type::void_type
&& return_deref
) {
696 ir_read_error(expr
, "call has return value storage but void type");
698 } else if (callee
->return_type
!= glsl_type::void_type
&& !return_deref
) {
699 ir_read_error(expr
, "call has non-void type but no return value storage");
703 return new(mem_ctx
) ir_call(callee
, return_deref
, ¶meters
);
707 ir_reader::read_expression(s_expression
*expr
)
709 s_expression
*s_type
;
711 s_expression
*s_arg
[4] = {NULL
};
713 s_pattern pat
[] = { "expression", s_type
, s_op
, s_arg
[0] };
714 if (!PARTIAL_MATCH(expr
, pat
)) {
715 ir_read_error(expr
, "expected (expression <type> <operator> "
716 "<operand> [<operand>] [<operand>] [<operand>])");
719 s_arg
[1] = (s_expression
*) s_arg
[0]->next
; // may be tail sentinel
720 s_arg
[2] = (s_expression
*) s_arg
[1]->next
; // may be tail sentinel or NULL
722 s_arg
[3] = (s_expression
*) s_arg
[2]->next
; // may be tail sentinel or NULL
724 const glsl_type
*type
= read_type(s_type
);
728 /* Read the operator */
729 ir_expression_operation op
= ir_expression::get_operator(s_op
->value());
730 if (op
== (ir_expression_operation
) -1) {
731 ir_read_error(expr
, "invalid operator: %s", s_op
->value());
735 /* Skip "expression" <type> <operation> by subtracting 3. */
736 int num_operands
= (int) ((s_list
*) expr
)->subexpressions
.length() - 3;
738 int expected_operands
= ir_expression::get_num_operands(op
);
739 if (num_operands
!= expected_operands
) {
740 ir_read_error(expr
, "found %d expression operands, expected %d",
741 num_operands
, expected_operands
);
745 ir_rvalue
*arg
[4] = {NULL
};
746 for (int i
= 0; i
< num_operands
; i
++) {
747 arg
[i
] = read_rvalue(s_arg
[i
]);
748 if (arg
[i
] == NULL
) {
749 ir_read_error(NULL
, "when reading operand #%d of %s", i
, s_op
->value());
754 return new(mem_ctx
) ir_expression(op
, type
, arg
[0], arg
[1], arg
[2], arg
[3]);
758 ir_reader::read_swizzle(s_expression
*expr
)
763 s_pattern pat
[] = { "swiz", swiz
, sub
};
764 if (!MATCH(expr
, pat
)) {
765 ir_read_error(expr
, "expected (swiz <swizzle> <rvalue>)");
769 if (strlen(swiz
->value()) > 4) {
770 ir_read_error(expr
, "expected a valid swizzle; found %s", swiz
->value());
774 ir_rvalue
*rvalue
= read_rvalue(sub
);
778 ir_swizzle
*ir
= ir_swizzle::create(rvalue
, swiz
->value(),
779 rvalue
->type
->vector_elements
);
781 ir_read_error(expr
, "invalid swizzle");
787 ir_reader::read_constant(s_expression
*expr
)
789 s_expression
*type_expr
;
792 s_pattern pat
[] = { "constant", type_expr
, values
};
793 if (!MATCH(expr
, pat
)) {
794 ir_read_error(expr
, "expected (constant <type> (...))");
798 const glsl_type
*type
= read_type(type_expr
);
802 if (values
== NULL
) {
803 ir_read_error(expr
, "expected (constant <type> (...))");
807 if (type
->is_array()) {
808 unsigned elements_supplied
= 0;
810 foreach_in_list(s_expression
, elt
, &values
->subexpressions
) {
811 ir_constant
*ir_elt
= read_constant(elt
);
814 elements
.push_tail(ir_elt
);
818 if (elements_supplied
!= type
->length
) {
819 ir_read_error(values
, "expected exactly %u array elements, "
820 "given %u", type
->length
, elements_supplied
);
823 return new(mem_ctx
) ir_constant(type
, &elements
);
826 ir_constant_data data
= { { 0 } };
828 // Read in list of values (at most 16).
830 foreach_in_list(s_expression
, expr
, &values
->subexpressions
) {
832 ir_read_error(values
, "expected at most 16 numbers");
836 if (type
->base_type
== GLSL_TYPE_FLOAT
) {
837 s_number
*value
= SX_AS_NUMBER(expr
);
839 ir_read_error(values
, "expected numbers");
842 data
.f
[k
] = value
->fvalue();
844 s_int
*value
= SX_AS_INT(expr
);
846 ir_read_error(values
, "expected integers");
850 switch (type
->base_type
) {
851 case GLSL_TYPE_UINT
: {
852 data
.u
[k
] = value
->value();
855 case GLSL_TYPE_INT
: {
856 data
.i
[k
] = value
->value();
859 case GLSL_TYPE_BOOL
: {
860 data
.b
[k
] = value
->value();
864 ir_read_error(values
, "unsupported constant type");
870 if (k
!= type
->components()) {
871 ir_read_error(values
, "expected %u constant values, found %u",
872 type
->components(), k
);
876 return new(mem_ctx
) ir_constant(type
, &data
);
879 ir_dereference_variable
*
880 ir_reader::read_var_ref(s_expression
*expr
)
883 s_pattern var_pat
[] = { "var_ref", s_var
};
885 if (MATCH(expr
, var_pat
)) {
886 ir_variable
*var
= state
->symbols
->get_variable(s_var
->value());
888 ir_read_error(expr
, "undeclared variable: %s", s_var
->value());
891 return new(mem_ctx
) ir_dereference_variable(var
);
897 ir_reader::read_dereference(s_expression
*expr
)
899 s_expression
*s_subject
;
900 s_expression
*s_index
;
903 s_pattern array_pat
[] = { "array_ref", s_subject
, s_index
};
904 s_pattern record_pat
[] = { "record_ref", s_subject
, s_field
};
906 ir_dereference_variable
*var_ref
= read_var_ref(expr
);
907 if (var_ref
!= NULL
) {
909 } else if (MATCH(expr
, array_pat
)) {
910 ir_rvalue
*subject
= read_rvalue(s_subject
);
911 if (subject
== NULL
) {
912 ir_read_error(NULL
, "when reading the subject of an array_ref");
916 ir_rvalue
*idx
= read_rvalue(s_index
);
918 ir_read_error(NULL
, "when reading the index of an array_ref");
921 return new(mem_ctx
) ir_dereference_array(subject
, idx
);
922 } else if (MATCH(expr
, record_pat
)) {
923 ir_rvalue
*subject
= read_rvalue(s_subject
);
924 if (subject
== NULL
) {
925 ir_read_error(NULL
, "when reading the subject of a record_ref");
928 return new(mem_ctx
) ir_dereference_record(subject
, s_field
->value());
934 ir_reader::read_texture(s_expression
*expr
)
936 s_symbol
*tag
= NULL
;
937 s_expression
*s_type
= NULL
;
938 s_expression
*s_sampler
= NULL
;
939 s_expression
*s_coord
= NULL
;
940 s_expression
*s_offset
= NULL
;
941 s_expression
*s_proj
= NULL
;
942 s_list
*s_shadow
= NULL
;
943 s_expression
*s_lod
= NULL
;
944 s_expression
*s_sample_index
= NULL
;
945 s_expression
*s_component
= NULL
;
947 ir_texture_opcode op
= ir_tex
; /* silence warning */
949 s_pattern tex_pattern
[] =
950 { "tex", s_type
, s_sampler
, s_coord
, s_offset
, s_proj
, s_shadow
};
951 s_pattern lod_pattern
[] =
952 { "lod", s_type
, s_sampler
, s_coord
};
953 s_pattern txf_pattern
[] =
954 { "txf", s_type
, s_sampler
, s_coord
, s_offset
, s_lod
};
955 s_pattern txf_ms_pattern
[] =
956 { "txf_ms", s_type
, s_sampler
, s_coord
, s_sample_index
};
957 s_pattern txs_pattern
[] =
958 { "txs", s_type
, s_sampler
, s_lod
};
959 s_pattern tg4_pattern
[] =
960 { "tg4", s_type
, s_sampler
, s_coord
, s_offset
, s_component
};
961 s_pattern query_levels_pattern
[] =
962 { "query_levels", s_type
, s_sampler
};
963 s_pattern other_pattern
[] =
964 { tag
, s_type
, s_sampler
, s_coord
, s_offset
, s_proj
, s_shadow
, s_lod
};
966 if (MATCH(expr
, lod_pattern
)) {
968 } else if (MATCH(expr
, tex_pattern
)) {
970 } else if (MATCH(expr
, txf_pattern
)) {
972 } else if (MATCH(expr
, txf_ms_pattern
)) {
974 } else if (MATCH(expr
, txs_pattern
)) {
976 } else if (MATCH(expr
, tg4_pattern
)) {
978 } else if (MATCH(expr
, query_levels_pattern
)) {
979 op
= ir_query_levels
;
980 } else if (MATCH(expr
, other_pattern
)) {
981 op
= ir_texture::get_opcode(tag
->value());
982 if (op
== (ir_texture_opcode
) -1)
985 ir_read_error(NULL
, "unexpected texture pattern %s", tag
->value());
989 ir_texture
*tex
= new(mem_ctx
) ir_texture(op
);
992 const glsl_type
*type
= read_type(s_type
);
994 ir_read_error(NULL
, "when reading type in (%s ...)",
995 tex
->opcode_string());
999 // Read sampler (must be a deref)
1000 ir_dereference
*sampler
= read_dereference(s_sampler
);
1001 if (sampler
== NULL
) {
1002 ir_read_error(NULL
, "when reading sampler in (%s ...)",
1003 tex
->opcode_string());
1006 tex
->set_sampler(sampler
, type
);
1009 // Read coordinate (any rvalue)
1010 tex
->coordinate
= read_rvalue(s_coord
);
1011 if (tex
->coordinate
== NULL
) {
1012 ir_read_error(NULL
, "when reading coordinate in (%s ...)",
1013 tex
->opcode_string());
1017 if (op
!= ir_txf_ms
&& op
!= ir_lod
) {
1018 // Read texel offset - either 0 or an rvalue.
1019 s_int
*si_offset
= SX_AS_INT(s_offset
);
1020 if (si_offset
== NULL
|| si_offset
->value() != 0) {
1021 tex
->offset
= read_rvalue(s_offset
);
1022 if (tex
->offset
== NULL
) {
1023 ir_read_error(s_offset
, "expected 0 or an expression");
1030 if (op
!= ir_txf
&& op
!= ir_txf_ms
&&
1031 op
!= ir_txs
&& op
!= ir_lod
&& op
!= ir_tg4
&&
1032 op
!= ir_query_levels
) {
1033 s_int
*proj_as_int
= SX_AS_INT(s_proj
);
1034 if (proj_as_int
&& proj_as_int
->value() == 1) {
1035 tex
->projector
= NULL
;
1037 tex
->projector
= read_rvalue(s_proj
);
1038 if (tex
->projector
== NULL
) {
1039 ir_read_error(NULL
, "when reading projective divide in (%s ..)",
1040 tex
->opcode_string());
1045 if (s_shadow
->subexpressions
.is_empty()) {
1046 tex
->shadow_comparitor
= NULL
;
1048 tex
->shadow_comparitor
= read_rvalue(s_shadow
);
1049 if (tex
->shadow_comparitor
== NULL
) {
1050 ir_read_error(NULL
, "when reading shadow comparitor in (%s ..)",
1051 tex
->opcode_string());
1059 tex
->lod_info
.bias
= read_rvalue(s_lod
);
1060 if (tex
->lod_info
.bias
== NULL
) {
1061 ir_read_error(NULL
, "when reading LOD bias in (txb ...)");
1068 tex
->lod_info
.lod
= read_rvalue(s_lod
);
1069 if (tex
->lod_info
.lod
== NULL
) {
1070 ir_read_error(NULL
, "when reading LOD in (%s ...)",
1071 tex
->opcode_string());
1076 tex
->lod_info
.sample_index
= read_rvalue(s_sample_index
);
1077 if (tex
->lod_info
.sample_index
== NULL
) {
1078 ir_read_error(NULL
, "when reading sample_index in (txf_ms ...)");
1083 s_expression
*s_dx
, *s_dy
;
1084 s_pattern dxdy_pat
[] = { s_dx
, s_dy
};
1085 if (!MATCH(s_lod
, dxdy_pat
)) {
1086 ir_read_error(s_lod
, "expected (dPdx dPdy) in (txd ...)");
1089 tex
->lod_info
.grad
.dPdx
= read_rvalue(s_dx
);
1090 if (tex
->lod_info
.grad
.dPdx
== NULL
) {
1091 ir_read_error(NULL
, "when reading dPdx in (txd ...)");
1094 tex
->lod_info
.grad
.dPdy
= read_rvalue(s_dy
);
1095 if (tex
->lod_info
.grad
.dPdy
== NULL
) {
1096 ir_read_error(NULL
, "when reading dPdy in (txd ...)");
1102 tex
->lod_info
.component
= read_rvalue(s_component
);
1103 if (tex
->lod_info
.component
== NULL
) {
1104 ir_read_error(NULL
, "when reading component in (tg4 ...)");
1109 // tex and lod don't have any extra parameters.
1116 ir_reader::read_emit_vertex(s_expression
*expr
)
1118 s_expression
*s_stream
= NULL
;
1120 s_pattern pat
[] = { "emit-vertex", s_stream
};
1122 if (MATCH(expr
, pat
)) {
1123 ir_rvalue
*stream
= read_dereference(s_stream
);
1124 if (stream
== NULL
) {
1125 ir_read_error(NULL
, "when reading stream info in emit-vertex");
1128 return new(mem_ctx
) ir_emit_vertex(stream
);
1130 ir_read_error(NULL
, "when reading emit-vertex");
1135 ir_reader::read_end_primitive(s_expression
*expr
)
1137 s_expression
*s_stream
= NULL
;
1139 s_pattern pat
[] = { "end-primitive", s_stream
};
1141 if (MATCH(expr
, pat
)) {
1142 ir_rvalue
*stream
= read_dereference(s_stream
);
1143 if (stream
== NULL
) {
1144 ir_read_error(NULL
, "when reading stream info in end-primitive");
1147 return new(mem_ctx
) ir_end_primitive(stream
);
1149 ir_read_error(NULL
, "when reading end-primitive");
1154 ir_reader::read_barrier(s_expression
*expr
)
1156 s_pattern pat
[] = { "barrier" };
1158 if (MATCH(expr
, pat
)) {
1159 return new(mem_ctx
) ir_barrier();
1161 ir_read_error(NULL
, "when reading barrier");