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.
30 #include "ir_reader.h"
31 #include "glsl_parser_extras.h"
32 #include "glsl_types.h"
33 #include "s_expression.h"
35 static void ir_read_error(_mesa_glsl_parse_state
*, s_expression
*,
36 const char *fmt
, ...);
37 static const glsl_type
*read_type(_mesa_glsl_parse_state
*, s_expression
*);
39 static void scan_for_prototypes(_mesa_glsl_parse_state
*, exec_list
*,
41 static ir_function
*read_function(_mesa_glsl_parse_state
*, s_list
*,
43 static void read_function_sig(_mesa_glsl_parse_state
*, ir_function
*,
44 s_list
*, bool skip_body
);
46 static void read_instructions(_mesa_glsl_parse_state
*, exec_list
*,
47 s_expression
*, ir_loop
*);
48 static ir_instruction
*read_instruction(_mesa_glsl_parse_state
*,
49 s_expression
*, ir_loop
*);
50 static ir_variable
*read_declaration(_mesa_glsl_parse_state
*, s_list
*);
51 static ir_if
*read_if(_mesa_glsl_parse_state
*, s_list
*, ir_loop
*);
52 static ir_loop
*read_loop(_mesa_glsl_parse_state
*st
, s_list
*list
);
53 static ir_return
*read_return(_mesa_glsl_parse_state
*, s_list
*);
55 static ir_rvalue
*read_rvalue(_mesa_glsl_parse_state
*, s_expression
*);
56 static ir_assignment
*read_assignment(_mesa_glsl_parse_state
*, s_list
*);
57 static ir_expression
*read_expression(_mesa_glsl_parse_state
*, s_list
*);
58 static ir_call
*read_call(_mesa_glsl_parse_state
*, s_list
*);
59 static ir_swizzle
*read_swizzle(_mesa_glsl_parse_state
*, s_list
*);
60 static ir_constant
*read_constant(_mesa_glsl_parse_state
*, s_list
*);
61 static ir_texture
*read_texture(_mesa_glsl_parse_state
*, s_list
*);
63 static ir_dereference
*read_dereference(_mesa_glsl_parse_state
*,
65 static ir_dereference
*read_var_ref(_mesa_glsl_parse_state
*, s_list
*);
66 static ir_dereference
*read_array_ref(_mesa_glsl_parse_state
*, s_list
*);
67 static ir_dereference
*read_record_ref(_mesa_glsl_parse_state
*, s_list
*);
70 _mesa_glsl_read_ir(_mesa_glsl_parse_state
*state
, exec_list
*instructions
,
71 const char *src
, bool scan_for_protos
)
73 s_expression
*expr
= s_expression::read_expression(state
, src
);
75 ir_read_error(state
, NULL
, "couldn't parse S-Expression.");
79 if (scan_for_protos
) {
80 scan_for_prototypes(state
, instructions
, expr
);
85 read_instructions(state
, instructions
, expr
, NULL
);
90 ir_read_error(_mesa_glsl_parse_state
*state
, s_expression
*expr
,
97 state
->info_log
= talloc_strdup_append(state
->info_log
, "error: ");
100 state
->info_log
= talloc_vasprintf_append(state
->info_log
, fmt
, ap
);
102 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n");
105 state
->info_log
= talloc_strdup_append(state
->info_log
,
106 "...in this context:\n ");
108 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n\n");
112 static const glsl_type
*
113 read_type(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
115 s_list
*list
= SX_AS_LIST(expr
);
117 s_symbol
*type_sym
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
118 if (type_sym
== NULL
) {
119 ir_read_error(st
, expr
, "expected type (array ...) or (struct ...)");
122 if (strcmp(type_sym
->value(), "array") == 0) {
123 if (list
->length() != 3) {
124 ir_read_error(st
, expr
, "expected type (array <type> <int>)");
129 s_expression
*base_expr
= (s_expression
*) type_sym
->next
;
130 const glsl_type
*base_type
= read_type(st
, base_expr
);
131 if (base_type
== NULL
) {
132 ir_read_error(st
, NULL
, "when reading base type of array");
137 s_int
*size
= SX_AS_INT(base_expr
->next
);
139 ir_read_error(st
, expr
, "found non-integer array size");
143 return glsl_type::get_array_instance(base_type
, size
->value());
144 } else if (strcmp(type_sym
->value(), "struct") == 0) {
145 assert(false); // FINISHME
147 ir_read_error(st
, expr
, "expected (array ...) or (struct ...); "
148 "found (%s ...)", type_sym
->value());
153 s_symbol
*type_sym
= SX_AS_SYMBOL(expr
);
154 if (type_sym
== NULL
) {
155 ir_read_error(st
, expr
, "expected <type> (symbol or list)");
159 const glsl_type
*type
= st
->symbols
->get_type(type_sym
->value());
161 ir_read_error(st
, expr
, "invalid type: %s", type_sym
->value());
168 scan_for_prototypes(_mesa_glsl_parse_state
*st
, exec_list
*instructions
,
171 s_list
*list
= SX_AS_LIST(expr
);
173 ir_read_error(st
, expr
, "Expected (<instruction> ...); found an atom.");
177 foreach_iter(exec_list_iterator
, it
, list
->subexpressions
) {
178 s_list
*sub
= SX_AS_LIST(it
.get());
180 continue; // not a (function ...); ignore it.
182 s_symbol
*tag
= SX_AS_SYMBOL(sub
->subexpressions
.get_head());
183 if (tag
== NULL
|| strcmp(tag
->value(), "function") != 0)
184 continue; // not a (function ...); ignore it.
186 ir_function
*f
= read_function(st
, sub
, true);
189 instructions
->push_tail(f
);
194 read_function(_mesa_glsl_parse_state
*st
, s_list
*list
, bool skip_body
)
198 if (list
->length() < 3) {
199 ir_read_error(st
, list
, "Expected (function <name> (signature ...) ...)");
203 s_symbol
*name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
205 ir_read_error(st
, list
, "Expected (function <name> ...)");
209 ir_function
*f
= st
->symbols
->get_function(name
->value());
211 f
= new(ctx
) ir_function(name
->value());
212 added
= st
->symbols
->add_function(f
->name
, f
);
216 exec_list_iterator it
= list
->subexpressions
.iterator();
217 it
.next(); // skip "function" tag
218 it
.next(); // skip function name
219 for (/* nothing */; it
.has_next(); it
.next()) {
220 s_list
*siglist
= SX_AS_LIST(it
.get());
221 if (siglist
== NULL
) {
222 ir_read_error(st
, list
, "Expected (function (signature ...) ...)");
226 s_symbol
*tag
= SX_AS_SYMBOL(siglist
->subexpressions
.get_head());
227 if (tag
== NULL
|| strcmp(tag
->value(), "signature") != 0) {
228 ir_read_error(st
, siglist
, "Expected (signature ...)");
232 read_function_sig(st
, f
, siglist
, skip_body
);
234 return added
? f
: NULL
;
238 read_function_sig(_mesa_glsl_parse_state
*st
, ir_function
*f
, s_list
*list
,
242 if (list
->length() != 4) {
243 ir_read_error(st
, list
, "Expected (signature <type> (parameters ...) "
244 "(<instruction> ...))");
248 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
249 const glsl_type
*return_type
= read_type(st
, type_expr
);
250 if (return_type
== NULL
)
253 s_list
*paramlist
= SX_AS_LIST(type_expr
->next
);
254 s_list
*body_list
= SX_AS_LIST(paramlist
->next
);
255 if (paramlist
== NULL
|| body_list
== NULL
) {
256 ir_read_error(st
, list
, "Expected (signature <type> (parameters ...) "
257 "(<instruction> ...))");
260 s_symbol
*paramtag
= SX_AS_SYMBOL(paramlist
->subexpressions
.get_head());
261 if (paramtag
== NULL
|| strcmp(paramtag
->value(), "parameters") != 0) {
262 ir_read_error(st
, paramlist
, "Expected (parameters ...)");
266 // Read the parameters list into a temporary place.
267 exec_list hir_parameters
;
268 st
->symbols
->push_scope();
270 exec_list_iterator it
= paramlist
->subexpressions
.iterator();
271 for (it
.next() /* skip "parameters" */; it
.has_next(); it
.next()) {
272 s_list
*decl
= SX_AS_LIST(it
.get());
273 ir_variable
*var
= read_declaration(st
, decl
);
277 hir_parameters
.push_tail(var
);
280 ir_function_signature
*sig
= f
->exact_matching_signature(&hir_parameters
);
281 if (sig
== NULL
&& skip_body
) {
282 /* If scanning for prototypes, generate a new signature. */
283 sig
= new(ctx
) ir_function_signature(return_type
);
284 sig
->is_built_in
= true;
285 f
->add_signature(sig
);
286 } else if (sig
!= NULL
) {
287 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
288 if (badvar
!= NULL
) {
289 ir_read_error(st
, list
, "function `%s' parameter `%s' qualifiers "
290 "don't match prototype", f
->name
, badvar
);
294 if (sig
->return_type
!= return_type
) {
295 ir_read_error(st
, list
, "function `%s' return type doesn't "
296 "match prototype", f
->name
);
300 /* No prototype for this body exists - skip it. */
301 st
->symbols
->pop_scope();
306 sig
->replace_parameters(&hir_parameters
);
308 if (!skip_body
&& !body_list
->subexpressions
.is_empty()) {
309 if (sig
->is_defined
) {
310 ir_read_error(st
, list
, "function %s redefined", f
->name
);
313 read_instructions(st
, &sig
->body
, body_list
, NULL
);
314 sig
->is_defined
= true;
317 st
->symbols
->pop_scope();
321 read_instructions(_mesa_glsl_parse_state
*st
, exec_list
*instructions
,
322 s_expression
*expr
, ir_loop
*loop_ctx
)
324 // Read in a list of instructions
325 s_list
*list
= SX_AS_LIST(expr
);
327 ir_read_error(st
, expr
, "Expected (<instruction> ...); found an atom.");
331 foreach_iter(exec_list_iterator
, it
, list
->subexpressions
) {
332 s_expression
*sub
= (s_expression
*) it
.get();
333 ir_instruction
*ir
= read_instruction(st
, sub
, loop_ctx
);
335 instructions
->push_tail(ir
);
340 static ir_instruction
*
341 read_instruction(_mesa_glsl_parse_state
*st
, s_expression
*expr
,
345 s_symbol
*symbol
= SX_AS_SYMBOL(expr
);
346 if (symbol
!= NULL
) {
347 if (strcmp(symbol
->value(), "break") == 0 && loop_ctx
!= NULL
)
348 return new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
349 if (strcmp(symbol
->value(), "continue") == 0 && loop_ctx
!= NULL
)
350 return new(ctx
) ir_loop_jump(ir_loop_jump::jump_continue
);
353 s_list
*list
= SX_AS_LIST(expr
);
354 if (list
== NULL
|| list
->subexpressions
.is_empty()) {
355 ir_read_error(st
, expr
, "Invalid instruction.\n");
359 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
361 ir_read_error(st
, expr
, "expected instruction tag");
365 ir_instruction
*inst
= NULL
;
366 if (strcmp(tag
->value(), "declare") == 0) {
367 inst
= read_declaration(st
, list
);
368 } else if (strcmp(tag
->value(), "assign") == 0) {
369 inst
= read_assignment(st
, list
);
370 } else if (strcmp(tag
->value(), "if") == 0) {
371 inst
= read_if(st
, list
, loop_ctx
);
372 } else if (strcmp(tag
->value(), "loop") == 0) {
373 inst
= read_loop(st
, list
);
374 } else if (strcmp(tag
->value(), "return") == 0) {
375 inst
= read_return(st
, list
);
376 } else if (strcmp(tag
->value(), "function") == 0) {
377 inst
= read_function(st
, list
, false);
379 inst
= read_rvalue(st
, list
);
381 ir_read_error(st
, NULL
, "when reading instruction");
388 read_declaration(_mesa_glsl_parse_state
*st
, s_list
*list
)
391 if (list
->length() != 4) {
392 ir_read_error(st
, list
, "expected (declare (<qualifiers>) <type> "
397 s_list
*quals
= SX_AS_LIST(list
->subexpressions
.head
->next
);
399 ir_read_error(st
, list
, "expected a list of variable qualifiers");
403 s_expression
*type_expr
= (s_expression
*) quals
->next
;
404 const glsl_type
*type
= read_type(st
, type_expr
);
408 s_symbol
*var_name
= SX_AS_SYMBOL(type_expr
->next
);
409 if (var_name
== NULL
) {
410 ir_read_error(st
, list
, "expected variable name, found non-symbol");
414 ir_variable
*var
= new(ctx
) ir_variable(type
, var_name
->value(),
417 foreach_iter(exec_list_iterator
, it
, quals
->subexpressions
) {
418 s_symbol
*qualifier
= SX_AS_SYMBOL(it
.get());
419 if (qualifier
== NULL
) {
420 ir_read_error(st
, list
, "qualifier list must contain only symbols");
425 // FINISHME: Check for duplicate/conflicting qualifiers.
426 if (strcmp(qualifier
->value(), "centroid") == 0) {
428 } else if (strcmp(qualifier
->value(), "invariant") == 0) {
430 } else if (strcmp(qualifier
->value(), "uniform") == 0) {
431 var
->mode
= ir_var_uniform
;
432 } else if (strcmp(qualifier
->value(), "auto") == 0) {
433 var
->mode
= ir_var_auto
;
434 } else if (strcmp(qualifier
->value(), "in") == 0) {
435 var
->mode
= ir_var_in
;
436 } else if (strcmp(qualifier
->value(), "out") == 0) {
437 var
->mode
= ir_var_out
;
438 } else if (strcmp(qualifier
->value(), "inout") == 0) {
439 var
->mode
= ir_var_inout
;
440 } else if (strcmp(qualifier
->value(), "smooth") == 0) {
441 var
->interpolation
= ir_var_smooth
;
442 } else if (strcmp(qualifier
->value(), "flat") == 0) {
443 var
->interpolation
= ir_var_flat
;
444 } else if (strcmp(qualifier
->value(), "noperspective") == 0) {
445 var
->interpolation
= ir_var_noperspective
;
447 ir_read_error(st
, list
, "unknown qualifier: %s", qualifier
->value());
453 // Add the variable to the symbol table
454 st
->symbols
->add_variable(var
->name
, var
);
461 read_if(_mesa_glsl_parse_state
*st
, s_list
*list
, ir_loop
*loop_ctx
)
464 if (list
->length() != 4) {
465 ir_read_error(st
, list
, "expected (if <condition> (<then> ...) "
470 s_expression
*cond_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
471 ir_rvalue
*condition
= read_rvalue(st
, cond_expr
);
472 if (condition
== NULL
) {
473 ir_read_error(st
, NULL
, "when reading condition of (if ...)");
477 s_expression
*then_expr
= (s_expression
*) cond_expr
->next
;
478 s_expression
*else_expr
= (s_expression
*) then_expr
->next
;
480 ir_if
*iff
= new(ctx
) ir_if(condition
);
482 read_instructions(st
, &iff
->then_instructions
, then_expr
, loop_ctx
);
483 read_instructions(st
, &iff
->else_instructions
, else_expr
, loop_ctx
);
493 read_loop(_mesa_glsl_parse_state
*st
, s_list
*list
)
496 if (list
->length() != 6) {
497 ir_read_error(st
, list
, "expected (loop <counter> <from> <to> "
498 "<increment> <body>)");
502 s_expression
*count_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
503 s_expression
*from_expr
= (s_expression
*) count_expr
->next
;
504 s_expression
*to_expr
= (s_expression
*) from_expr
->next
;
505 s_expression
*inc_expr
= (s_expression
*) to_expr
->next
;
506 s_expression
*body_expr
= (s_expression
*) inc_expr
->next
;
508 // FINISHME: actually read the count/from/to fields.
510 ir_loop
*loop
= new(ctx
) ir_loop
;
511 read_instructions(st
, &loop
->body_instructions
, body_expr
, loop
);
521 read_return(_mesa_glsl_parse_state
*st
, s_list
*list
)
524 if (list
->length() != 2) {
525 ir_read_error(st
, list
, "expected (return <rvalue>)");
529 s_expression
*expr
= (s_expression
*) list
->subexpressions
.head
->next
;
531 ir_rvalue
*retval
= read_rvalue(st
, expr
);
532 if (retval
== NULL
) {
533 ir_read_error(st
, NULL
, "when reading return value");
537 return new(ctx
) ir_return(retval
);
542 read_rvalue(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
544 s_list
*list
= SX_AS_LIST(expr
);
545 if (list
== NULL
|| list
->subexpressions
.is_empty())
548 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
550 ir_read_error(st
, expr
, "expected rvalue tag");
554 ir_rvalue
*rvalue
= read_dereference(st
, list
);
555 if (rvalue
!= NULL
|| st
->error
)
557 else if (strcmp(tag
->value(), "swiz") == 0) {
558 rvalue
= read_swizzle(st
, list
);
559 } else if (strcmp(tag
->value(), "expression") == 0) {
560 rvalue
= read_expression(st
, list
);
561 } else if (strcmp(tag
->value(), "call") == 0) {
562 rvalue
= read_call(st
, list
);
563 } else if (strcmp(tag
->value(), "constant") == 0) {
564 rvalue
= read_constant(st
, list
);
566 rvalue
= read_texture(st
, list
);
567 if (rvalue
== NULL
&& !st
->error
)
568 ir_read_error(st
, expr
, "unrecognized rvalue tag: %s", tag
->value());
574 static ir_assignment
*
575 read_assignment(_mesa_glsl_parse_state
*st
, s_list
*list
)
578 if (list
->length() != 4) {
579 ir_read_error(st
, list
, "expected (assign <condition> <lhs> <rhs>)");
583 s_expression
*cond_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
584 s_expression
*lhs_expr
= (s_expression
*) cond_expr
->next
;
585 s_expression
*rhs_expr
= (s_expression
*) lhs_expr
->next
;
587 // FINISHME: Deal with "true" condition
588 ir_rvalue
*condition
= read_rvalue(st
, cond_expr
);
589 if (condition
== NULL
) {
590 ir_read_error(st
, NULL
, "when reading condition of assignment");
594 ir_rvalue
*lhs
= read_rvalue(st
, lhs_expr
);
596 ir_read_error(st
, NULL
, "when reading left-hand side of assignment");
600 ir_rvalue
*rhs
= read_rvalue(st
, rhs_expr
);
602 ir_read_error(st
, NULL
, "when reading right-hand side of assignment");
606 return new(ctx
) ir_assignment(lhs
, rhs
, condition
);
610 read_call(_mesa_glsl_parse_state
*st
, s_list
*list
)
613 if (list
->length() != 3) {
614 ir_read_error(st
, list
, "expected (call <name> (<param> ...))");
618 s_symbol
*name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
619 s_list
*params
= SX_AS_LIST(name
->next
);
620 if (name
== NULL
|| params
== NULL
) {
621 ir_read_error(st
, list
, "expected (call <name> (<param> ...))");
625 exec_list parameters
;
627 foreach_iter(exec_list_iterator
, it
, params
->subexpressions
) {
628 s_expression
*expr
= (s_expression
*) it
.get();
629 ir_rvalue
*param
= read_rvalue(st
, expr
);
631 ir_read_error(st
, list
, "when reading parameter to function call");
634 parameters
.push_tail(param
);
637 ir_function
*f
= st
->symbols
->get_function(name
->value());
639 ir_read_error(st
, list
, "found call to undefined function %s",
644 ir_function_signature
*callee
= f
->matching_signature(¶meters
);
645 if (callee
== NULL
) {
646 ir_read_error(st
, list
, "couldn't find matching signature for function "
647 "%s", name
->value());
651 return new(ctx
) ir_call(callee
, ¶meters
);
654 static ir_expression
*
655 read_expression(_mesa_glsl_parse_state
*st
, s_list
*list
)
658 const unsigned list_length
= list
->length();
659 if (list_length
< 4) {
660 ir_read_error(st
, list
, "expected (expression <type> <operator> "
661 "<operand> [<operand>])");
665 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
666 const glsl_type
*type
= read_type(st
, type_expr
);
670 /* Read the operator */
671 s_symbol
*op_sym
= SX_AS_SYMBOL(type_expr
->next
);
672 if (op_sym
== NULL
) {
673 ir_read_error(st
, list
, "expected operator, found non-symbol");
677 ir_expression_operation op
= ir_expression::get_operator(op_sym
->value());
678 if (op
== (ir_expression_operation
) -1) {
679 ir_read_error(st
, list
, "invalid operator: %s", op_sym
->value());
683 /* Now that we know the operator, check for the right number of operands */
684 if (ir_expression::get_num_operands(op
) == 2) {
685 if (list_length
!= 5) {
686 ir_read_error(st
, list
, "expected (expression <type> %s <operand> "
687 " <operand>)", op_sym
->value());
691 if (list_length
!= 4) {
692 ir_read_error(st
, list
, "expected (expression <type> %s <operand>)",
698 s_expression
*exp1
= (s_expression
*) (op_sym
->next
);
699 ir_rvalue
*arg1
= read_rvalue(st
, exp1
);
701 ir_read_error(st
, NULL
, "when reading first operand of %s",
706 ir_rvalue
*arg2
= NULL
;
707 if (ir_expression::get_num_operands(op
) == 2) {
708 s_expression
*exp2
= (s_expression
*) (exp1
->next
);
709 arg2
= read_rvalue(st
, exp2
);
711 ir_read_error(st
, NULL
, "when reading second operand of %s",
717 return new(ctx
) ir_expression(op
, type
, arg1
, arg2
);
721 read_swizzle(_mesa_glsl_parse_state
*st
, s_list
*list
)
723 if (list
->length() != 3) {
724 ir_read_error(st
, list
, "expected (swiz <swizzle> <rvalue>)");
728 s_symbol
*swiz
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
730 ir_read_error(st
, list
, "expected a valid swizzle; found non-symbol");
734 if (strlen(swiz
->value()) > 4) {
735 ir_read_error(st
, list
, "expected a valid swizzle; found %s",
740 s_expression
*sub
= (s_expression
*) swiz
->next
;
742 ir_read_error(st
, list
, "expected rvalue: (swizzle %s <rvalue>)",
747 ir_rvalue
*rvalue
= read_rvalue(st
, sub
);
751 ir_swizzle
*ir
= ir_swizzle::create(rvalue
, swiz
->value(),
752 rvalue
->type
->vector_elements
);
754 ir_read_error(st
, list
, "invalid swizzle");
760 read_constant(_mesa_glsl_parse_state
*st
, s_list
*list
)
763 if (list
->length() != 3) {
764 ir_read_error(st
, list
, "expected (constant <type> (...))");
768 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
769 const glsl_type
*type
= read_type(st
, type_expr
);
773 s_list
*values
= SX_AS_LIST(type_expr
->next
);
774 if (values
== NULL
) {
775 ir_read_error(st
, list
, "expected (constant <type> (...))");
779 if (type
->is_array()) {
780 const unsigned elements_supplied
= values
->length();
781 if (elements_supplied
!= type
->length
) {
782 ir_read_error(st
, values
, "expected exactly %u array elements, "
783 "given %u", type
->length
, elements_supplied
);
788 foreach_iter(exec_list_iterator
, it
, values
->subexpressions
) {
789 s_expression
*expr
= (s_expression
*) it
.get();
790 s_list
*elt
= SX_AS_LIST(expr
);
792 ir_read_error(st
, expr
, "expected (constant ...) array element");
796 ir_constant
*ir_elt
= read_constant(st
, elt
);
799 elements
.push_tail(ir_elt
);
801 return new(ctx
) ir_constant(type
, &elements
);
804 const glsl_type
*const base_type
= type
->get_base_type();
806 ir_constant_data data
;
808 // Read in list of values (at most 16).
810 foreach_iter(exec_list_iterator
, it
, values
->subexpressions
) {
812 ir_read_error(st
, values
, "expected at most 16 numbers");
816 s_expression
*expr
= (s_expression
*) it
.get();
818 if (base_type
->base_type
== GLSL_TYPE_FLOAT
) {
819 s_number
*value
= SX_AS_NUMBER(expr
);
821 ir_read_error(st
, values
, "expected numbers");
824 data
.f
[k
] = value
->fvalue();
826 s_int
*value
= SX_AS_INT(expr
);
828 ir_read_error(st
, values
, "expected integers");
832 switch (base_type
->base_type
) {
833 case GLSL_TYPE_UINT
: {
834 data
.u
[k
] = value
->value();
837 case GLSL_TYPE_INT
: {
838 data
.i
[k
] = value
->value();
841 case GLSL_TYPE_BOOL
: {
842 data
.b
[k
] = value
->value();
846 ir_read_error(st
, values
, "unsupported constant type");
853 return new(ctx
) ir_constant(type
, &data
);
856 static ir_dereference
*
857 read_dereference(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
859 s_list
*list
= SX_AS_LIST(expr
);
860 if (list
== NULL
|| list
->subexpressions
.is_empty())
863 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.head
);
866 if (strcmp(tag
->value(), "var_ref") == 0)
867 return read_var_ref(st
, list
);
868 if (strcmp(tag
->value(), "array_ref") == 0)
869 return read_array_ref(st
, list
);
870 if (strcmp(tag
->value(), "record_ref") == 0)
871 return read_record_ref(st
, list
);
875 static ir_dereference
*
876 read_var_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
879 if (list
->length() != 2) {
880 ir_read_error(st
, list
, "expected (var_ref <variable name>)");
883 s_symbol
*var_name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
884 if (var_name
== NULL
) {
885 ir_read_error(st
, list
, "expected (var_ref <variable name>)");
889 ir_variable
*var
= st
->symbols
->get_variable(var_name
->value());
891 ir_read_error(st
, list
, "undeclared variable: %s", var_name
->value());
895 return new(ctx
) ir_dereference_variable(var
);
898 static ir_dereference
*
899 read_array_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
902 if (list
->length() != 3) {
903 ir_read_error(st
, list
, "expected (array_ref <rvalue> <index>)");
907 s_expression
*subj_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
908 ir_rvalue
*subject
= read_rvalue(st
, subj_expr
);
909 if (subject
== NULL
) {
910 ir_read_error(st
, NULL
, "when reading the subject of an array_ref");
914 s_expression
*idx_expr
= (s_expression
*) subj_expr
->next
;
915 ir_rvalue
*idx
= read_rvalue(st
, idx_expr
);
916 return new(ctx
) ir_dereference_array(subject
, idx
);
919 static ir_dereference
*
920 read_record_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
923 if (list
->length() != 3) {
924 ir_read_error(st
, list
, "expected (record_ref <rvalue> <field>)");
928 s_expression
*subj_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
929 ir_rvalue
*subject
= read_rvalue(st
, subj_expr
);
930 if (subject
== NULL
) {
931 ir_read_error(st
, NULL
, "when reading the subject of a record_ref");
935 s_symbol
*field
= SX_AS_SYMBOL(subj_expr
->next
);
937 ir_read_error(st
, list
, "expected (record_ref ... <field name>)");
940 return new(ctx
) ir_dereference_record(subject
, field
->value());
944 valid_texture_list_length(ir_texture_opcode op
, s_list
*list
)
946 unsigned required_length
= 7;
949 else if (op
== ir_tex
)
952 return list
->length() == required_length
;
956 read_texture(_mesa_glsl_parse_state
*st
, s_list
*list
)
959 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.head
);
962 ir_texture_opcode op
= ir_texture::get_opcode(tag
->value());
963 if (op
== (ir_texture_opcode
) -1)
966 if (!valid_texture_list_length(op
, list
)) {
967 ir_read_error(st
, NULL
, "invalid list size in (%s ...)", tag
->value());
971 ir_texture
*tex
= new(ctx
) ir_texture(op
);
973 // Read sampler (must be a deref)
974 s_expression
*sampler_expr
= (s_expression
*) tag
->next
;
975 ir_dereference
*sampler
= read_dereference(st
, sampler_expr
);
976 if (sampler
== NULL
) {
977 ir_read_error(st
, NULL
, "when reading sampler in (%s ...)", tag
->value());
980 tex
->set_sampler(sampler
);
982 // Read coordinate (any rvalue)
983 s_expression
*coordinate_expr
= (s_expression
*) sampler_expr
->next
;
984 tex
->coordinate
= read_rvalue(st
, coordinate_expr
);
985 if (tex
->coordinate
== NULL
) {
986 ir_read_error(st
, NULL
, "when reading coordinate in (%s ...)",
991 // Read texel offset, i.e. (0 0 0)
992 s_list
*offset_list
= SX_AS_LIST(coordinate_expr
->next
);
993 if (offset_list
== NULL
|| offset_list
->length() != 3) {
994 ir_read_error(st
, offset_list
, "expected (<int> <int> <int>)");
997 s_int
*offset_x
= SX_AS_INT(offset_list
->subexpressions
.head
);
998 s_int
*offset_y
= SX_AS_INT(offset_x
->next
);
999 s_int
*offset_z
= SX_AS_INT(offset_y
->next
);
1000 if (offset_x
== NULL
|| offset_y
== NULL
|| offset_z
== NULL
) {
1001 ir_read_error(st
, offset_list
, "expected (<int> <int> <int>)");
1004 tex
->offsets
[0] = offset_x
->value();
1005 tex
->offsets
[1] = offset_y
->value();
1006 tex
->offsets
[2] = offset_z
->value();
1009 s_expression
*lod_expr
= (s_expression
*) offset_list
->next
;
1010 tex
->lod_info
.lod
= read_rvalue(st
, lod_expr
);
1011 if (tex
->lod_info
.lod
== NULL
) {
1012 ir_read_error(st
, NULL
, "when reading LOD in (txf ...)");
1016 s_expression
*proj_expr
= (s_expression
*) offset_list
->next
;
1017 s_int
*proj_as_int
= SX_AS_INT(proj_expr
);
1018 if (proj_as_int
&& proj_as_int
->value() == 1) {
1019 tex
->projector
= NULL
;
1021 tex
->projector
= read_rvalue(st
, proj_expr
);
1022 if (tex
->projector
== NULL
) {
1023 ir_read_error(st
, NULL
, "when reading projective divide in (%s ..)",
1029 s_list
*shadow_list
= SX_AS_LIST(proj_expr
->next
);
1030 if (shadow_list
== NULL
) {
1031 ir_read_error(st
, NULL
, "shadow comparitor must be a list");
1034 if (shadow_list
->subexpressions
.is_empty()) {
1035 tex
->shadow_comparitor
= NULL
;
1037 tex
->shadow_comparitor
= read_rvalue(st
, shadow_list
);
1038 if (tex
->shadow_comparitor
== NULL
) {
1039 ir_read_error(st
, NULL
, "when reading shadow comparitor in (%s ..)",
1044 s_expression
*lod_expr
= (s_expression
*) shadow_list
->next
;
1048 tex
->lod_info
.bias
= read_rvalue(st
, lod_expr
);
1049 if (tex
->lod_info
.bias
== NULL
) {
1050 ir_read_error(st
, NULL
, "when reading LOD bias in (txb ...)");
1055 tex
->lod_info
.lod
= read_rvalue(st
, lod_expr
);
1056 if (tex
->lod_info
.lod
== NULL
) {
1057 ir_read_error(st
, NULL
, "when reading LOD in (txl ...)");
1062 s_list
*lod_list
= SX_AS_LIST(lod_expr
);
1063 if (lod_list
->length() != 2) {
1064 ir_read_error(st
, lod_expr
, "expected (dPdx dPdy) in (txd ...)");
1067 s_expression
*dx_expr
= (s_expression
*) lod_list
->subexpressions
.head
;
1068 s_expression
*dy_expr
= (s_expression
*) dx_expr
->next
;
1070 tex
->lod_info
.grad
.dPdx
= read_rvalue(st
, dx_expr
);
1071 if (tex
->lod_info
.grad
.dPdx
== NULL
) {
1072 ir_read_error(st
, NULL
, "when reading dPdx in (txd ...)");
1075 tex
->lod_info
.grad
.dPdy
= read_rvalue(st
, dy_expr
);
1076 if (tex
->lod_info
.grad
.dPdy
== NULL
) {
1077 ir_read_error(st
, NULL
, "when reading dPdy in (txd ...)");
1083 // tex doesn't have any extra parameters and txf was handled earlier.