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
,
73 s_expression
*expr
= s_expression::read_expression(state
, src
);
75 ir_read_error(state
, NULL
, "couldn't parse S-Expression.");
79 scan_for_prototypes(state
, instructions
, expr
);
83 read_instructions(state
, instructions
, expr
, NULL
);
88 ir_read_error(_mesa_glsl_parse_state
*state
, s_expression
*expr
,
95 state
->info_log
= talloc_strdup_append(state
->info_log
, "error: ");
98 state
->info_log
= talloc_vasprintf_append(state
->info_log
, fmt
, ap
);
100 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n");
103 state
->info_log
= talloc_strdup_append(state
->info_log
,
104 "...in this context:\n ");
106 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n\n");
110 static const glsl_type
*
111 read_type(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
113 s_list
*list
= SX_AS_LIST(expr
);
115 s_symbol
*type_sym
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
116 if (type_sym
== NULL
) {
117 ir_read_error(st
, expr
, "expected type (array ...) or (struct ...)");
120 if (strcmp(type_sym
->value(), "array") == 0) {
121 if (list
->length() != 3) {
122 ir_read_error(st
, expr
, "expected type (array <type> <int>)");
127 s_expression
*base_expr
= (s_expression
*) type_sym
->next
;
128 const glsl_type
*base_type
= read_type(st
, base_expr
);
129 if (base_type
== NULL
) {
130 ir_read_error(st
, NULL
, "when reading base type of array");
135 s_int
*size
= SX_AS_INT(base_expr
->next
);
137 ir_read_error(st
, expr
, "found non-integer array size");
141 return glsl_type::get_array_instance(base_type
, size
->value());
142 } else if (strcmp(type_sym
->value(), "struct") == 0) {
143 assert(false); // FINISHME
145 ir_read_error(st
, expr
, "expected (array ...) or (struct ...); "
146 "found (%s ...)", type_sym
->value());
151 s_symbol
*type_sym
= SX_AS_SYMBOL(expr
);
152 if (type_sym
== NULL
) {
153 ir_read_error(st
, expr
, "expected <type> (symbol or list)");
157 const glsl_type
*type
= st
->symbols
->get_type(type_sym
->value());
159 ir_read_error(st
, expr
, "invalid type: %s", type_sym
->value());
166 scan_for_prototypes(_mesa_glsl_parse_state
*st
, exec_list
*instructions
,
169 s_list
*list
= SX_AS_LIST(expr
);
171 ir_read_error(st
, expr
, "Expected (<instruction> ...); found an atom.");
175 foreach_iter(exec_list_iterator
, it
, list
->subexpressions
) {
176 s_list
*sub
= SX_AS_LIST(it
.get());
178 continue; // not a (function ...); ignore it.
180 s_symbol
*tag
= SX_AS_SYMBOL(sub
->subexpressions
.get_head());
181 if (tag
== NULL
|| strcmp(tag
->value(), "function") != 0)
182 continue; // not a (function ...); ignore it.
184 ir_function
*f
= read_function(st
, sub
, true);
187 instructions
->push_tail(f
);
192 read_function(_mesa_glsl_parse_state
*st
, s_list
*list
, bool skip_body
)
196 if (list
->length() < 3) {
197 ir_read_error(st
, list
, "Expected (function <name> (signature ...) ...)");
201 s_symbol
*name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
203 ir_read_error(st
, list
, "Expected (function <name> ...)");
207 ir_function
*f
= st
->symbols
->get_function(name
->value());
209 f
= new(ctx
) ir_function(name
->value());
210 added
= st
->symbols
->add_function(f
->name
, f
);
214 exec_list_iterator it
= list
->subexpressions
.iterator();
215 it
.next(); // skip "function" tag
216 it
.next(); // skip function name
217 for (/* nothing */; it
.has_next(); it
.next()) {
218 s_list
*siglist
= SX_AS_LIST(it
.get());
219 if (siglist
== NULL
) {
220 ir_read_error(st
, list
, "Expected (function (signature ...) ...)");
224 s_symbol
*tag
= SX_AS_SYMBOL(siglist
->subexpressions
.get_head());
225 if (tag
== NULL
|| strcmp(tag
->value(), "signature") != 0) {
226 ir_read_error(st
, siglist
, "Expected (signature ...)");
230 read_function_sig(st
, f
, siglist
, skip_body
);
232 return added
? f
: NULL
;
236 read_function_sig(_mesa_glsl_parse_state
*st
, ir_function
*f
, s_list
*list
,
240 if (list
->length() != 4) {
241 ir_read_error(st
, list
, "Expected (signature <type> (parameters ...) "
242 "(<instruction> ...))");
246 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
247 const glsl_type
*return_type
= read_type(st
, type_expr
);
248 if (return_type
== NULL
)
251 s_list
*paramlist
= SX_AS_LIST(type_expr
->next
);
252 s_list
*body_list
= SX_AS_LIST(paramlist
->next
);
253 if (paramlist
== NULL
|| body_list
== NULL
) {
254 ir_read_error(st
, list
, "Expected (signature <type> (parameters ...) "
255 "(<instruction> ...))");
258 s_symbol
*paramtag
= SX_AS_SYMBOL(paramlist
->subexpressions
.get_head());
259 if (paramtag
== NULL
|| strcmp(paramtag
->value(), "parameters") != 0) {
260 ir_read_error(st
, paramlist
, "Expected (parameters ...)");
264 // Read the parameters list into a temporary place.
265 exec_list hir_parameters
;
266 st
->symbols
->push_scope();
268 exec_list_iterator it
= paramlist
->subexpressions
.iterator();
269 for (it
.next() /* skip "parameters" */; it
.has_next(); it
.next()) {
270 s_list
*decl
= SX_AS_LIST(it
.get());
271 ir_variable
*var
= read_declaration(st
, decl
);
275 hir_parameters
.push_tail(var
);
278 ir_function_signature
*sig
= f
->exact_matching_signature(&hir_parameters
);
280 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
281 if (badvar
!= NULL
) {
282 ir_read_error(st
, list
, "function `%s' parameter `%s' qualifiers "
283 "don't match prototype", f
->name
, badvar
);
287 if (sig
->return_type
!= return_type
) {
288 ir_read_error(st
, list
, "function `%s' return type doesn't "
289 "match prototype", f
->name
);
293 sig
= new(ctx
) ir_function_signature(return_type
);
294 sig
->is_built_in
= true;
295 f
->add_signature(sig
);
298 sig
->replace_parameters(&hir_parameters
);
301 if (sig
->is_defined
) {
302 ir_read_error(st
, list
, "function %s redefined", f
->name
);
305 read_instructions(st
, &sig
->body
, body_list
, NULL
);
306 sig
->is_defined
= true;
309 st
->symbols
->pop_scope();
313 read_instructions(_mesa_glsl_parse_state
*st
, exec_list
*instructions
,
314 s_expression
*expr
, ir_loop
*loop_ctx
)
316 // Read in a list of instructions
317 s_list
*list
= SX_AS_LIST(expr
);
319 ir_read_error(st
, expr
, "Expected (<instruction> ...); found an atom.");
323 foreach_iter(exec_list_iterator
, it
, list
->subexpressions
) {
324 s_expression
*sub
= (s_expression
*) it
.get();
325 ir_instruction
*ir
= read_instruction(st
, sub
, loop_ctx
);
327 instructions
->push_tail(ir
);
332 static ir_instruction
*
333 read_instruction(_mesa_glsl_parse_state
*st
, s_expression
*expr
,
337 s_symbol
*symbol
= SX_AS_SYMBOL(expr
);
338 if (symbol
!= NULL
) {
339 if (strcmp(symbol
->value(), "break") == 0 && loop_ctx
!= NULL
)
340 return new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
341 if (strcmp(symbol
->value(), "continue") == 0 && loop_ctx
!= NULL
)
342 return new(ctx
) ir_loop_jump(ir_loop_jump::jump_continue
);
345 s_list
*list
= SX_AS_LIST(expr
);
346 if (list
== NULL
|| list
->subexpressions
.is_empty()) {
347 ir_read_error(st
, expr
, "Invalid instruction.\n");
351 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
353 ir_read_error(st
, expr
, "expected instruction tag");
357 ir_instruction
*inst
= NULL
;
358 if (strcmp(tag
->value(), "declare") == 0) {
359 inst
= read_declaration(st
, list
);
360 } else if (strcmp(tag
->value(), "if") == 0) {
361 inst
= read_if(st
, list
, loop_ctx
);
362 } else if (strcmp(tag
->value(), "loop") == 0) {
363 inst
= read_loop(st
, list
);
364 } else if (strcmp(tag
->value(), "return") == 0) {
365 inst
= read_return(st
, list
);
366 } else if (strcmp(tag
->value(), "function") == 0) {
367 inst
= read_function(st
, list
, false);
369 inst
= read_rvalue(st
, list
);
371 ir_read_error(st
, NULL
, "when reading instruction");
378 read_declaration(_mesa_glsl_parse_state
*st
, s_list
*list
)
381 if (list
->length() != 4) {
382 ir_read_error(st
, list
, "expected (declare (<qualifiers>) <type> "
387 s_list
*quals
= SX_AS_LIST(list
->subexpressions
.head
->next
);
389 ir_read_error(st
, list
, "expected a list of variable qualifiers");
393 s_expression
*type_expr
= (s_expression
*) quals
->next
;
394 const glsl_type
*type
= read_type(st
, type_expr
);
398 s_symbol
*var_name
= SX_AS_SYMBOL(type_expr
->next
);
399 if (var_name
== NULL
) {
400 ir_read_error(st
, list
, "expected variable name, found non-symbol");
404 ir_variable
*var
= new(ctx
) ir_variable(type
, var_name
->value(),
407 foreach_iter(exec_list_iterator
, it
, quals
->subexpressions
) {
408 s_symbol
*qualifier
= SX_AS_SYMBOL(it
.get());
409 if (qualifier
== NULL
) {
410 ir_read_error(st
, list
, "qualifier list must contain only symbols");
415 // FINISHME: Check for duplicate/conflicting qualifiers.
416 if (strcmp(qualifier
->value(), "centroid") == 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_in
;
426 } else if (strcmp(qualifier
->value(), "out") == 0) {
427 var
->mode
= ir_var_out
;
428 } else if (strcmp(qualifier
->value(), "inout") == 0) {
429 var
->mode
= ir_var_inout
;
430 } else if (strcmp(qualifier
->value(), "smooth") == 0) {
431 var
->interpolation
= ir_var_smooth
;
432 } else if (strcmp(qualifier
->value(), "flat") == 0) {
433 var
->interpolation
= ir_var_flat
;
434 } else if (strcmp(qualifier
->value(), "noperspective") == 0) {
435 var
->interpolation
= ir_var_noperspective
;
437 ir_read_error(st
, list
, "unknown qualifier: %s", qualifier
->value());
443 // Add the variable to the symbol table
444 st
->symbols
->add_variable(var
->name
, var
);
451 read_if(_mesa_glsl_parse_state
*st
, s_list
*list
, ir_loop
*loop_ctx
)
454 if (list
->length() != 4) {
455 ir_read_error(st
, list
, "expected (if <condition> (<then> ...) "
460 s_expression
*cond_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
461 ir_rvalue
*condition
= read_rvalue(st
, cond_expr
);
462 if (condition
== NULL
) {
463 ir_read_error(st
, NULL
, "when reading condition of (if ...)");
467 s_expression
*then_expr
= (s_expression
*) cond_expr
->next
;
468 s_expression
*else_expr
= (s_expression
*) then_expr
->next
;
470 ir_if
*iff
= new(ctx
) ir_if(condition
);
472 read_instructions(st
, &iff
->then_instructions
, then_expr
, loop_ctx
);
473 read_instructions(st
, &iff
->else_instructions
, else_expr
, loop_ctx
);
483 read_loop(_mesa_glsl_parse_state
*st
, s_list
*list
)
486 if (list
->length() != 6) {
487 ir_read_error(st
, list
, "expected (loop <counter> <from> <to> "
488 "<increment> <body>)");
492 s_expression
*count_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
493 s_expression
*from_expr
= (s_expression
*) count_expr
->next
;
494 s_expression
*to_expr
= (s_expression
*) from_expr
->next
;
495 s_expression
*inc_expr
= (s_expression
*) to_expr
->next
;
496 s_expression
*body_expr
= (s_expression
*) inc_expr
->next
;
498 // FINISHME: actually read the count/from/to fields.
500 ir_loop
*loop
= new(ctx
) ir_loop
;
501 read_instructions(st
, &loop
->body_instructions
, body_expr
, loop
);
511 read_return(_mesa_glsl_parse_state
*st
, s_list
*list
)
514 if (list
->length() != 2) {
515 ir_read_error(st
, list
, "expected (return <rvalue>)");
519 s_expression
*expr
= (s_expression
*) list
->subexpressions
.head
->next
;
521 ir_rvalue
*retval
= read_rvalue(st
, expr
);
522 if (retval
== NULL
) {
523 ir_read_error(st
, NULL
, "when reading return value");
527 return new(ctx
) ir_return(retval
);
532 read_rvalue(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
534 s_list
*list
= SX_AS_LIST(expr
);
535 if (list
== NULL
|| list
->subexpressions
.is_empty())
538 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
540 ir_read_error(st
, expr
, "expected rvalue tag");
544 ir_rvalue
*rvalue
= read_dereference(st
, list
);
545 if (rvalue
!= NULL
|| st
->error
)
547 else if (strcmp(tag
->value(), "swiz") == 0) {
548 rvalue
= read_swizzle(st
, list
);
549 } else if (strcmp(tag
->value(), "assign") == 0) {
550 rvalue
= read_assignment(st
, list
);
551 } else if (strcmp(tag
->value(), "expression") == 0) {
552 rvalue
= read_expression(st
, list
);
553 } else if (strcmp(tag
->value(), "call") == 0) {
554 rvalue
= read_call(st
, list
);
555 } else if (strcmp(tag
->value(), "constant") == 0) {
556 rvalue
= read_constant(st
, list
);
558 rvalue
= read_texture(st
, list
);
559 if (rvalue
== NULL
&& !st
->error
)
560 ir_read_error(st
, expr
, "unrecognized rvalue tag: %s", tag
->value());
566 static ir_assignment
*
567 read_assignment(_mesa_glsl_parse_state
*st
, s_list
*list
)
570 if (list
->length() != 4) {
571 ir_read_error(st
, list
, "expected (assign <condition> <lhs> <rhs>)");
575 s_expression
*cond_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
576 s_expression
*lhs_expr
= (s_expression
*) cond_expr
->next
;
577 s_expression
*rhs_expr
= (s_expression
*) lhs_expr
->next
;
579 // FINISHME: Deal with "true" condition
580 ir_rvalue
*condition
= read_rvalue(st
, cond_expr
);
581 if (condition
== NULL
) {
582 ir_read_error(st
, NULL
, "when reading condition of assignment");
586 ir_rvalue
*lhs
= read_rvalue(st
, lhs_expr
);
588 ir_read_error(st
, NULL
, "when reading left-hand side of assignment");
592 ir_rvalue
*rhs
= read_rvalue(st
, rhs_expr
);
594 ir_read_error(st
, NULL
, "when reading right-hand side of assignment");
598 return new(ctx
) ir_assignment(lhs
, rhs
, condition
);
602 read_call(_mesa_glsl_parse_state
*st
, s_list
*list
)
605 if (list
->length() != 3) {
606 ir_read_error(st
, list
, "expected (call <name> (<param> ...))");
610 s_symbol
*name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
611 s_list
*params
= SX_AS_LIST(name
->next
);
612 if (name
== NULL
|| params
== NULL
) {
613 ir_read_error(st
, list
, "expected (call <name> (<param> ...))");
617 exec_list parameters
;
619 foreach_iter(exec_list_iterator
, it
, params
->subexpressions
) {
620 s_expression
*expr
= (s_expression
*) it
.get();
621 ir_rvalue
*param
= read_rvalue(st
, expr
);
623 ir_read_error(st
, list
, "when reading parameter to function call");
626 parameters
.push_tail(param
);
629 ir_function
*f
= st
->symbols
->get_function(name
->value());
631 ir_read_error(st
, list
, "found call to undefined function %s",
636 ir_function_signature
*callee
= f
->matching_signature(¶meters
);
637 if (callee
== NULL
) {
638 ir_read_error(st
, list
, "couldn't find matching signature for function "
639 "%s", name
->value());
643 return new(ctx
) ir_call(callee
, ¶meters
);
646 static ir_expression
*
647 read_expression(_mesa_glsl_parse_state
*st
, s_list
*list
)
650 const unsigned list_length
= list
->length();
651 if (list_length
< 4) {
652 ir_read_error(st
, list
, "expected (expression <type> <operator> "
653 "<operand> [<operand>])");
657 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
658 const glsl_type
*type
= read_type(st
, type_expr
);
662 /* Read the operator */
663 s_symbol
*op_sym
= SX_AS_SYMBOL(type_expr
->next
);
664 if (op_sym
== NULL
) {
665 ir_read_error(st
, list
, "expected operator, found non-symbol");
669 ir_expression_operation op
= ir_expression::get_operator(op_sym
->value());
670 if (op
== (ir_expression_operation
) -1) {
671 ir_read_error(st
, list
, "invalid operator: %s", op_sym
->value());
675 /* Now that we know the operator, check for the right number of operands */
676 if (ir_expression::get_num_operands(op
) == 2) {
677 if (list_length
!= 5) {
678 ir_read_error(st
, list
, "expected (expression <type> %s <operand> "
679 " <operand>)", op_sym
->value());
683 if (list_length
!= 4) {
684 ir_read_error(st
, list
, "expected (expression <type> %s <operand>)",
690 s_expression
*exp1
= (s_expression
*) (op_sym
->next
);
691 ir_rvalue
*arg1
= read_rvalue(st
, exp1
);
693 ir_read_error(st
, NULL
, "when reading first operand of %s",
698 ir_rvalue
*arg2
= NULL
;
699 if (ir_expression::get_num_operands(op
) == 2) {
700 s_expression
*exp2
= (s_expression
*) (exp1
->next
);
701 arg2
= read_rvalue(st
, exp2
);
703 ir_read_error(st
, NULL
, "when reading second operand of %s",
709 return new(ctx
) ir_expression(op
, type
, arg1
, arg2
);
713 read_swizzle(_mesa_glsl_parse_state
*st
, s_list
*list
)
715 if (list
->length() != 3) {
716 ir_read_error(st
, list
, "expected (swiz <swizzle> <rvalue>)");
720 s_symbol
*swiz
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
722 ir_read_error(st
, list
, "expected a valid swizzle; found non-symbol");
726 if (strlen(swiz
->value()) > 4) {
727 ir_read_error(st
, list
, "expected a valid swizzle; found %s",
732 s_expression
*sub
= (s_expression
*) swiz
->next
;
734 ir_read_error(st
, list
, "expected rvalue: (swizzle %s <rvalue>)",
739 ir_rvalue
*rvalue
= read_rvalue(st
, sub
);
743 ir_swizzle
*ir
= ir_swizzle::create(rvalue
, swiz
->value(),
744 rvalue
->type
->vector_elements
);
746 ir_read_error(st
, list
, "invalid swizzle");
752 read_constant(_mesa_glsl_parse_state
*st
, s_list
*list
)
755 if (list
->length() != 3) {
756 ir_read_error(st
, list
, "expected (constant <type> (...))");
760 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
761 const glsl_type
*type
= read_type(st
, type_expr
);
765 s_list
*values
= SX_AS_LIST(type_expr
->next
);
766 if (values
== NULL
) {
767 ir_read_error(st
, list
, "expected (constant <type> (...))");
771 if (type
->is_array()) {
772 const unsigned elements_supplied
= values
->length();
773 if (elements_supplied
!= type
->length
) {
774 ir_read_error(st
, values
, "expected exactly %u array elements, "
775 "given %u", type
->length
, elements_supplied
);
780 foreach_iter(exec_list_iterator
, it
, values
->subexpressions
) {
781 s_expression
*expr
= (s_expression
*) it
.get();
782 s_list
*elt
= SX_AS_LIST(expr
);
784 ir_read_error(st
, expr
, "expected (constant ...) array element");
788 ir_constant
*ir_elt
= read_constant(st
, elt
);
791 elements
.push_tail(ir_elt
);
793 return new(ctx
) ir_constant(type
, &elements
);
796 const glsl_type
*const base_type
= type
->get_base_type();
798 ir_constant_data data
;
800 // Read in list of values (at most 16).
802 foreach_iter(exec_list_iterator
, it
, values
->subexpressions
) {
804 ir_read_error(st
, values
, "expected at most 16 numbers");
808 s_expression
*expr
= (s_expression
*) it
.get();
810 if (base_type
->base_type
== GLSL_TYPE_FLOAT
) {
811 s_number
*value
= SX_AS_NUMBER(expr
);
813 ir_read_error(st
, values
, "expected numbers");
816 data
.f
[k
] = value
->fvalue();
818 s_int
*value
= SX_AS_INT(expr
);
820 ir_read_error(st
, values
, "expected integers");
824 switch (base_type
->base_type
) {
825 case GLSL_TYPE_UINT
: {
826 data
.u
[k
] = value
->value();
829 case GLSL_TYPE_INT
: {
830 data
.i
[k
] = value
->value();
833 case GLSL_TYPE_BOOL
: {
834 data
.b
[k
] = value
->value();
838 ir_read_error(st
, values
, "unsupported constant type");
845 return new(ctx
) ir_constant(type
, &data
);
848 static ir_dereference
*
849 read_dereference(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
851 s_list
*list
= SX_AS_LIST(expr
);
852 if (list
== NULL
|| list
->subexpressions
.is_empty())
855 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.head
);
858 if (strcmp(tag
->value(), "var_ref") == 0)
859 return read_var_ref(st
, list
);
860 if (strcmp(tag
->value(), "array_ref") == 0)
861 return read_array_ref(st
, list
);
862 if (strcmp(tag
->value(), "record_ref") == 0)
863 return read_record_ref(st
, list
);
867 static ir_dereference
*
868 read_var_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
871 if (list
->length() != 2) {
872 ir_read_error(st
, list
, "expected (var_ref <variable name>)");
875 s_symbol
*var_name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
876 if (var_name
== NULL
) {
877 ir_read_error(st
, list
, "expected (var_ref <variable name>)");
881 ir_variable
*var
= st
->symbols
->get_variable(var_name
->value());
883 ir_read_error(st
, list
, "undeclared variable: %s", var_name
->value());
887 return new(ctx
) ir_dereference_variable(var
);
890 static ir_dereference
*
891 read_array_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
894 if (list
->length() != 3) {
895 ir_read_error(st
, list
, "expected (array_ref <rvalue> <index>)");
899 s_expression
*subj_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
900 ir_rvalue
*subject
= read_rvalue(st
, subj_expr
);
901 if (subject
== NULL
) {
902 ir_read_error(st
, NULL
, "when reading the subject of an array_ref");
906 s_expression
*idx_expr
= (s_expression
*) subj_expr
->next
;
907 ir_rvalue
*idx
= read_rvalue(st
, idx_expr
);
908 return new(ctx
) ir_dereference_array(subject
, idx
);
911 static ir_dereference
*
912 read_record_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
915 if (list
->length() != 3) {
916 ir_read_error(st
, list
, "expected (record_ref <rvalue> <field>)");
920 s_expression
*subj_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
921 ir_rvalue
*subject
= read_rvalue(st
, subj_expr
);
922 if (subject
== NULL
) {
923 ir_read_error(st
, NULL
, "when reading the subject of a record_ref");
927 s_symbol
*field
= SX_AS_SYMBOL(subj_expr
->next
);
929 ir_read_error(st
, list
, "expected (record_ref ... <field name>)");
932 return new(ctx
) ir_dereference_record(subject
, field
->value());
936 valid_texture_list_length(ir_texture_opcode op
, s_list
*list
)
938 unsigned required_length
= 7;
941 else if (op
== ir_tex
)
944 return list
->length() == required_length
;
948 read_texture(_mesa_glsl_parse_state
*st
, s_list
*list
)
951 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.head
);
954 ir_texture_opcode op
= ir_texture::get_opcode(tag
->value());
955 if (op
== (ir_texture_opcode
) -1)
958 if (!valid_texture_list_length(op
, list
)) {
959 ir_read_error(st
, NULL
, "invalid list size in (%s ...)", tag
->value());
963 ir_texture
*tex
= new(ctx
) ir_texture(op
);
965 // Read sampler (must be a deref)
966 s_expression
*sampler_expr
= (s_expression
*) tag
->next
;
967 ir_dereference
*sampler
= read_dereference(st
, sampler_expr
);
968 if (sampler
== NULL
) {
969 ir_read_error(st
, NULL
, "when reading sampler in (%s ...)", tag
->value());
972 tex
->set_sampler(sampler
);
974 // Read coordinate (any rvalue)
975 s_expression
*coordinate_expr
= (s_expression
*) sampler_expr
->next
;
976 tex
->coordinate
= read_rvalue(st
, coordinate_expr
);
977 if (tex
->coordinate
== NULL
) {
978 ir_read_error(st
, NULL
, "when reading coordinate in (%s ...)",
983 // Read texel offset, i.e. (0 0 0)
984 s_list
*offset_list
= SX_AS_LIST(coordinate_expr
->next
);
985 if (offset_list
== NULL
|| offset_list
->length() != 3) {
986 ir_read_error(st
, offset_list
, "expected (<int> <int> <int>)");
989 s_int
*offset_x
= SX_AS_INT(offset_list
->subexpressions
.head
);
990 s_int
*offset_y
= SX_AS_INT(offset_x
->next
);
991 s_int
*offset_z
= SX_AS_INT(offset_y
->next
);
992 if (offset_x
== NULL
|| offset_y
== NULL
|| offset_z
== NULL
) {
993 ir_read_error(st
, offset_list
, "expected (<int> <int> <int>)");
996 tex
->offsets
[0] = offset_x
->value();
997 tex
->offsets
[1] = offset_y
->value();
998 tex
->offsets
[2] = offset_z
->value();
1001 s_expression
*lod_expr
= (s_expression
*) offset_list
->next
;
1002 tex
->lod_info
.lod
= read_rvalue(st
, lod_expr
);
1003 if (tex
->lod_info
.lod
== NULL
) {
1004 ir_read_error(st
, NULL
, "when reading LOD in (txf ...)");
1008 s_expression
*proj_expr
= (s_expression
*) offset_list
->next
;
1009 s_int
*proj_as_int
= SX_AS_INT(proj_expr
);
1010 if (proj_as_int
&& proj_as_int
->value() == 1) {
1011 tex
->projector
= NULL
;
1013 tex
->projector
= read_rvalue(st
, proj_expr
);
1014 if (tex
->projector
== NULL
) {
1015 ir_read_error(st
, NULL
, "when reading projective divide in (%s ..)",
1021 s_list
*shadow_list
= SX_AS_LIST(proj_expr
->next
);
1022 if (shadow_list
== NULL
) {
1023 ir_read_error(st
, NULL
, "shadow comparitor must be a list");
1026 if (shadow_list
->subexpressions
.is_empty()) {
1027 tex
->shadow_comparitor
= NULL
;
1029 tex
->shadow_comparitor
= read_rvalue(st
, shadow_list
);
1030 if (tex
->shadow_comparitor
== NULL
) {
1031 ir_read_error(st
, NULL
, "when reading shadow comparitor in (%s ..)",
1036 s_expression
*lod_expr
= (s_expression
*) shadow_list
->next
;
1040 tex
->lod_info
.bias
= read_rvalue(st
, lod_expr
);
1041 if (tex
->lod_info
.bias
== NULL
) {
1042 ir_read_error(st
, NULL
, "when reading LOD bias in (txb ...)");
1047 tex
->lod_info
.lod
= read_rvalue(st
, lod_expr
);
1048 if (tex
->lod_info
.lod
== NULL
) {
1049 ir_read_error(st
, NULL
, "when reading LOD in (txl ...)");
1054 s_list
*lod_list
= SX_AS_LIST(lod_expr
);
1055 if (lod_list
->length() != 2) {
1056 ir_read_error(st
, lod_expr
, "expected (dPdx dPdy) in (txd ...)");
1059 s_expression
*dx_expr
= (s_expression
*) lod_list
->subexpressions
.head
;
1060 s_expression
*dy_expr
= (s_expression
*) dx_expr
->next
;
1062 tex
->lod_info
.grad
.dPdx
= read_rvalue(st
, dx_expr
);
1063 if (tex
->lod_info
.grad
.dPdx
== NULL
) {
1064 ir_read_error(st
, NULL
, "when reading dPdx in (txd ...)");
1067 tex
->lod_info
.grad
.dPdy
= read_rvalue(st
, dy_expr
);
1068 if (tex
->lod_info
.grad
.dPdy
== NULL
) {
1069 ir_read_error(st
, NULL
, "when reading dPdy in (txd ...)");
1075 // tex doesn't have any extra parameters and txf was handled earlier.