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.
25 #include "ir_reader.h"
26 #include "glsl_parser_extras.h"
27 #include "glsl_types.h"
28 #include "s_expression.h"
30 static void ir_read_error(_mesa_glsl_parse_state
*, s_expression
*,
31 const char *fmt
, ...);
32 static const glsl_type
*read_type(_mesa_glsl_parse_state
*, s_expression
*);
34 static void scan_for_prototypes(_mesa_glsl_parse_state
*, exec_list
*,
36 static ir_function
*read_function(_mesa_glsl_parse_state
*, s_list
*,
38 static void read_function_sig(_mesa_glsl_parse_state
*, ir_function
*,
39 s_list
*, bool skip_body
);
41 static void read_instructions(_mesa_glsl_parse_state
*, exec_list
*,
42 s_expression
*, ir_loop
*);
43 static ir_instruction
*read_instruction(_mesa_glsl_parse_state
*,
44 s_expression
*, ir_loop
*);
45 static ir_variable
*read_declaration(_mesa_glsl_parse_state
*, s_list
*);
46 static ir_if
*read_if(_mesa_glsl_parse_state
*, s_list
*, ir_loop
*);
47 static ir_loop
*read_loop(_mesa_glsl_parse_state
*st
, s_list
*list
);
48 static ir_return
*read_return(_mesa_glsl_parse_state
*, s_list
*);
50 static ir_rvalue
*read_rvalue(_mesa_glsl_parse_state
*, s_expression
*);
51 static ir_assignment
*read_assignment(_mesa_glsl_parse_state
*, s_list
*);
52 static ir_expression
*read_expression(_mesa_glsl_parse_state
*, s_list
*);
53 static ir_call
*read_call(_mesa_glsl_parse_state
*, s_list
*);
54 static ir_swizzle
*read_swizzle(_mesa_glsl_parse_state
*, s_list
*);
55 static ir_constant
*read_constant(_mesa_glsl_parse_state
*, s_list
*);
56 static ir_texture
*read_texture(_mesa_glsl_parse_state
*, s_list
*);
58 static ir_dereference
*read_dereference(_mesa_glsl_parse_state
*,
60 static ir_dereference
*read_var_ref(_mesa_glsl_parse_state
*, s_list
*);
61 static ir_dereference
*read_array_ref(_mesa_glsl_parse_state
*, s_list
*);
62 static ir_dereference
*read_record_ref(_mesa_glsl_parse_state
*, s_list
*);
65 _mesa_glsl_read_ir(_mesa_glsl_parse_state
*state
, exec_list
*instructions
,
68 s_expression
*expr
= s_expression::read_expression(src
);
70 ir_read_error(state
, NULL
, "couldn't parse S-Expression.");
74 scan_for_prototypes(state
, instructions
, expr
);
78 read_instructions(state
, instructions
, expr
, NULL
);
82 ir_read_error(_mesa_glsl_parse_state
*state
, s_expression
*expr
,
97 printf("...in this context:\n ");
103 static const glsl_type
*
104 read_type(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
106 s_list
*list
= SX_AS_LIST(expr
);
108 s_symbol
*type_sym
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
109 if (type_sym
== NULL
) {
110 ir_read_error(st
, expr
, "expected type (array ...) or (struct ...)");
113 if (strcmp(type_sym
->value(), "array") == 0) {
114 if (list
->length() != 3) {
115 ir_read_error(st
, expr
, "expected type (array <type> <int>)");
120 s_expression
*base_expr
= (s_expression
*) type_sym
->next
;
121 const glsl_type
*base_type
= read_type(st
, base_expr
);
122 if (base_type
== NULL
) {
123 ir_read_error(st
, NULL
, "when reading base type of array");
128 s_int
*size
= SX_AS_INT(base_expr
->next
);
130 ir_read_error(st
, expr
, "found non-integer array size");
134 return glsl_type::get_array_instance(base_type
, size
->value());
135 } else if (strcmp(type_sym
->value(), "struct") == 0) {
136 assert(false); // FINISHME
138 ir_read_error(st
, expr
, "expected (array ...) or (struct ...); "
139 "found (%s ...)", type_sym
->value());
144 s_symbol
*type_sym
= SX_AS_SYMBOL(expr
);
145 if (type_sym
== NULL
) {
146 ir_read_error(st
, expr
, "expected <type> (symbol or list)");
150 const glsl_type
*type
= st
->symbols
->get_type(type_sym
->value());
152 ir_read_error(st
, expr
, "invalid type: %s", type_sym
->value());
159 scan_for_prototypes(_mesa_glsl_parse_state
*st
, exec_list
*instructions
,
162 s_list
*list
= SX_AS_LIST(expr
);
164 ir_read_error(st
, expr
, "Expected (<instruction> ...); found an atom.");
168 foreach_iter(exec_list_iterator
, it
, list
->subexpressions
) {
169 s_list
*sub
= SX_AS_LIST(it
.get());
171 continue; // not a (function ...); ignore it.
173 s_symbol
*tag
= SX_AS_SYMBOL(sub
->subexpressions
.get_head());
174 if (tag
== NULL
|| strcmp(tag
->value(), "function") != 0)
175 continue; // not a (function ...); ignore it.
177 ir_function
*f
= read_function(st
, sub
, true);
180 instructions
->push_tail(f
);
185 read_function(_mesa_glsl_parse_state
*st
, s_list
*list
, bool skip_body
)
187 if (list
->length() < 3) {
188 ir_read_error(st
, list
, "Expected (function <name> (signature ...) ...)");
192 s_symbol
*name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
194 ir_read_error(st
, list
, "Expected (function <name> ...)");
198 ir_function
*f
= st
->symbols
->get_function(name
->value());
200 f
= new ir_function(name
->value());
201 bool added
= st
->symbols
->add_function(name
->value(), f
);
205 exec_list_iterator it
= list
->subexpressions
.iterator();
206 it
.next(); // skip "function" tag
207 it
.next(); // skip function name
208 for (/* nothing */; it
.has_next(); it
.next()) {
209 s_list
*siglist
= SX_AS_LIST(it
.get());
210 if (siglist
== NULL
) {
211 ir_read_error(st
, list
, "Expected (function (signature ...) ...)");
215 s_symbol
*tag
= SX_AS_SYMBOL(siglist
->subexpressions
.get_head());
216 if (tag
== NULL
|| strcmp(tag
->value(), "signature") != 0) {
217 ir_read_error(st
, siglist
, "Expected (signature ...)");
221 read_function_sig(st
, f
, siglist
, skip_body
);
227 read_function_sig(_mesa_glsl_parse_state
*st
, ir_function
*f
, s_list
*list
,
230 if (list
->length() != 4) {
231 ir_read_error(st
, list
, "Expected (signature <type> (parameters ...) "
232 "(<instruction> ...))");
236 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
237 const glsl_type
*return_type
= read_type(st
, type_expr
);
238 if (return_type
== NULL
)
241 s_list
*paramlist
= SX_AS_LIST(type_expr
->next
);
242 s_list
*body_list
= SX_AS_LIST(paramlist
->next
);
243 if (paramlist
== NULL
|| body_list
== NULL
) {
244 ir_read_error(st
, list
, "Expected (signature <type> (parameters ...) "
245 "(<instruction> ...))");
248 s_symbol
*paramtag
= SX_AS_SYMBOL(paramlist
->subexpressions
.get_head());
249 if (paramtag
== NULL
|| strcmp(paramtag
->value(), "parameters") != 0) {
250 ir_read_error(st
, paramlist
, "Expected (parameters ...)");
254 // Read the parameters list into a temporary place.
255 exec_list hir_parameters
;
256 st
->symbols
->push_scope();
258 exec_list_iterator it
= paramlist
->subexpressions
.iterator();
259 for (it
.next() /* skip "parameters" */; it
.has_next(); it
.next()) {
260 s_list
*decl
= SX_AS_LIST(it
.get());
261 ir_variable
*var
= read_declaration(st
, decl
);
265 hir_parameters
.push_tail(var
);
268 ir_function_signature
*sig
= f
->exact_matching_signature(&hir_parameters
);
270 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
271 if (badvar
!= NULL
) {
272 ir_read_error(st
, list
, "function `%s' parameter `%s' qualifiers "
273 "don't match prototype", f
->name
, badvar
);
277 if (sig
->return_type
!= return_type
) {
278 ir_read_error(st
, list
, "function `%s' return type doesn't "
279 "match prototype", f
->name
);
283 sig
= new ir_function_signature(return_type
);
284 f
->add_signature(sig
);
287 sig
->replace_parameters(&hir_parameters
);
290 if (sig
->is_defined
) {
291 ir_read_error(st
, list
, "function %s redefined", f
->name
);
294 read_instructions(st
, &sig
->body
, body_list
, NULL
);
295 sig
->is_defined
= true;
298 st
->symbols
->pop_scope();
302 read_instructions(_mesa_glsl_parse_state
*st
, exec_list
*instructions
,
303 s_expression
*expr
, ir_loop
*loop_ctx
)
305 // Read in a list of instructions
306 s_list
*list
= SX_AS_LIST(expr
);
308 ir_read_error(st
, expr
, "Expected (<instruction> ...); found an atom.");
312 foreach_iter(exec_list_iterator
, it
, list
->subexpressions
) {
313 s_expression
*sub
= (s_expression
*) it
.get();
314 ir_instruction
*ir
= read_instruction(st
, sub
, loop_ctx
);
316 ir_read_error(st
, sub
, "Invalid instruction.\n");
319 instructions
->push_tail(ir
);
324 static ir_instruction
*
325 read_instruction(_mesa_glsl_parse_state
*st
, s_expression
*expr
,
328 s_symbol
*symbol
= SX_AS_SYMBOL(expr
);
329 if (symbol
!= NULL
) {
330 if (strcmp(symbol
->value(), "break") == 0 && loop_ctx
!= NULL
)
331 return new ir_loop_jump(loop_ctx
, ir_loop_jump::jump_break
);
332 if (strcmp(symbol
->value(), "continue") == 0 && loop_ctx
!= NULL
)
333 return new ir_loop_jump(loop_ctx
, ir_loop_jump::jump_continue
);
336 s_list
*list
= SX_AS_LIST(expr
);
337 if (list
== NULL
|| list
->subexpressions
.is_empty())
340 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
342 ir_read_error(st
, expr
, "expected instruction tag");
346 ir_instruction
*inst
= NULL
;
347 if (strcmp(tag
->value(), "declare") == 0) {
348 inst
= read_declaration(st
, list
);
349 } else if (strcmp(tag
->value(), "if") == 0) {
350 inst
= read_if(st
, list
, loop_ctx
);
351 } else if (strcmp(tag
->value(), "loop") == 0) {
352 inst
= read_loop(st
, list
);
353 } else if (strcmp(tag
->value(), "return") == 0) {
354 inst
= read_return(st
, list
);
355 } else if (strcmp(tag
->value(), "function") == 0) {
356 inst
= read_function(st
, list
, false);
358 inst
= read_rvalue(st
, list
);
360 ir_read_error(st
, NULL
, "when reading instruction");
367 read_declaration(_mesa_glsl_parse_state
*st
, s_list
*list
)
369 if (list
->length() != 4) {
370 ir_read_error(st
, list
, "expected (declare (<qualifiers>) <type> "
375 s_list
*quals
= SX_AS_LIST(list
->subexpressions
.head
->next
);
377 ir_read_error(st
, list
, "expected a list of variable qualifiers");
381 s_expression
*type_expr
= (s_expression
*) quals
->next
;
382 const glsl_type
*type
= read_type(st
, type_expr
);
386 s_symbol
*var_name
= SX_AS_SYMBOL(type_expr
->next
);
387 if (var_name
== NULL
) {
388 ir_read_error(st
, list
, "expected variable name, found non-symbol");
392 ir_variable
*var
= new ir_variable(type
, var_name
->value());
394 foreach_iter(exec_list_iterator
, it
, quals
->subexpressions
) {
395 s_symbol
*qualifier
= SX_AS_SYMBOL(it
.get());
396 if (qualifier
== NULL
) {
397 ir_read_error(st
, list
, "qualifier list must contain only symbols");
402 // FINISHME: Check for duplicate/conflicting qualifiers.
403 if (strcmp(qualifier
->value(), "centroid") == 0) {
405 } else if (strcmp(qualifier
->value(), "invariant") == 0) {
407 } else if (strcmp(qualifier
->value(), "uniform") == 0) {
408 var
->mode
= ir_var_uniform
;
409 } else if (strcmp(qualifier
->value(), "auto") == 0) {
410 var
->mode
= ir_var_auto
;
411 } else if (strcmp(qualifier
->value(), "in") == 0) {
412 var
->mode
= ir_var_in
;
413 } else if (strcmp(qualifier
->value(), "out") == 0) {
414 var
->mode
= ir_var_out
;
415 } else if (strcmp(qualifier
->value(), "inout") == 0) {
416 var
->mode
= ir_var_inout
;
417 } else if (strcmp(qualifier
->value(), "smooth") == 0) {
418 var
->interpolation
= ir_var_smooth
;
419 } else if (strcmp(qualifier
->value(), "flat") == 0) {
420 var
->interpolation
= ir_var_flat
;
421 } else if (strcmp(qualifier
->value(), "noperspective") == 0) {
422 var
->interpolation
= ir_var_noperspective
;
424 ir_read_error(st
, list
, "unknown qualifier: %s", qualifier
->value());
430 // Add the variable to the symbol table
431 st
->symbols
->add_variable(var_name
->value(), var
);
438 read_if(_mesa_glsl_parse_state
*st
, s_list
*list
, ir_loop
*loop_ctx
)
440 if (list
->length() != 4) {
441 ir_read_error(st
, list
, "expected (if <condition> (<then> ...) "
446 s_expression
*cond_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
447 ir_rvalue
*condition
= read_rvalue(st
, cond_expr
);
448 if (condition
== NULL
) {
449 ir_read_error(st
, NULL
, "when reading condition of (if ...)");
453 s_expression
*then_expr
= (s_expression
*) cond_expr
->next
;
454 s_expression
*else_expr
= (s_expression
*) then_expr
->next
;
456 ir_if
*iff
= new ir_if(condition
);
458 read_instructions(st
, &iff
->then_instructions
, then_expr
, loop_ctx
);
459 read_instructions(st
, &iff
->else_instructions
, else_expr
, loop_ctx
);
469 read_loop(_mesa_glsl_parse_state
*st
, s_list
*list
)
471 if (list
->length() != 6) {
472 ir_read_error(st
, list
, "expected (loop <counter> <from> <to> "
473 "<increment> <body>)");
477 s_expression
*count_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
478 s_expression
*from_expr
= (s_expression
*) count_expr
->next
;
479 s_expression
*to_expr
= (s_expression
*) from_expr
->next
;
480 s_expression
*inc_expr
= (s_expression
*) to_expr
->next
;
481 s_expression
*body_expr
= (s_expression
*) inc_expr
->next
;
483 // FINISHME: actually read the count/from/to fields.
485 ir_loop
*loop
= new ir_loop
;
486 read_instructions(st
, &loop
->body_instructions
, body_expr
, loop
);
496 read_return(_mesa_glsl_parse_state
*st
, s_list
*list
)
498 if (list
->length() != 2) {
499 ir_read_error(st
, list
, "expected (return <rvalue>)");
503 s_expression
*expr
= (s_expression
*) list
->subexpressions
.head
->next
;
505 ir_rvalue
*retval
= read_rvalue(st
, expr
);
506 if (retval
== NULL
) {
507 ir_read_error(st
, NULL
, "when reading return value");
511 return new ir_return(retval
);
516 read_rvalue(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
518 s_list
*list
= SX_AS_LIST(expr
);
519 if (list
== NULL
|| list
->subexpressions
.is_empty())
522 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
524 ir_read_error(st
, expr
, "expected rvalue tag");
528 ir_rvalue
*rvalue
= read_dereference(st
, list
);
529 if (rvalue
!= NULL
|| st
->error
)
531 else if (strcmp(tag
->value(), "swiz") == 0) {
532 rvalue
= read_swizzle(st
, list
);
533 } else if (strcmp(tag
->value(), "assign") == 0) {
534 rvalue
= read_assignment(st
, list
);
535 } else if (strcmp(tag
->value(), "expression") == 0) {
536 rvalue
= read_expression(st
, list
);
537 } else if (strcmp(tag
->value(), "call") == 0) {
538 rvalue
= read_call(st
, list
);
539 } else if (strcmp(tag
->value(), "constant") == 0) {
540 rvalue
= read_constant(st
, list
);
542 rvalue
= read_texture(st
, list
);
543 if (rvalue
== NULL
&& !st
->error
)
544 ir_read_error(st
, expr
, "unrecognized rvalue tag: %s", tag
->value());
550 static ir_assignment
*
551 read_assignment(_mesa_glsl_parse_state
*st
, s_list
*list
)
553 if (list
->length() != 4) {
554 ir_read_error(st
, list
, "expected (assign <condition> <lhs> <rhs>)");
558 s_expression
*cond_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
559 s_expression
*lhs_expr
= (s_expression
*) cond_expr
->next
;
560 s_expression
*rhs_expr
= (s_expression
*) lhs_expr
->next
;
562 // FINISHME: Deal with "true" condition
563 ir_rvalue
*condition
= read_rvalue(st
, cond_expr
);
564 if (condition
== NULL
) {
565 ir_read_error(st
, NULL
, "when reading condition of assignment");
569 ir_rvalue
*lhs
= read_rvalue(st
, lhs_expr
);
571 ir_read_error(st
, NULL
, "when reading left-hand side of assignment");
575 ir_rvalue
*rhs
= read_rvalue(st
, rhs_expr
);
577 ir_read_error(st
, NULL
, "when reading right-hand side of assignment");
581 return new ir_assignment(lhs
, rhs
, condition
);
585 read_call(_mesa_glsl_parse_state
*st
, s_list
*list
)
587 if (list
->length() != 3) {
588 ir_read_error(st
, list
, "expected (call <name> (<param> ...))");
592 s_symbol
*name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
593 s_list
*params
= SX_AS_LIST(name
->next
);
594 if (name
== NULL
|| params
== NULL
) {
595 ir_read_error(st
, list
, "expected (call <name> (<param> ...))");
599 exec_list parameters
;
601 foreach_iter(exec_list_iterator
, it
, params
->subexpressions
) {
602 s_expression
*expr
= (s_expression
*) it
.get();
603 ir_rvalue
*param
= read_rvalue(st
, expr
);
605 ir_read_error(st
, list
, "when reading parameter to function call");
608 parameters
.push_tail(param
);
611 ir_function
*f
= st
->symbols
->get_function(name
->value());
613 ir_read_error(st
, list
, "found call to undefined function %s",
618 const ir_function_signature
*callee
= f
->matching_signature(¶meters
);
619 if (callee
== NULL
) {
620 ir_read_error(st
, list
, "couldn't find matching signature for function "
621 "%s", name
->value());
625 return new ir_call(callee
, ¶meters
);
628 static ir_expression
*
629 read_expression(_mesa_glsl_parse_state
*st
, s_list
*list
)
631 const unsigned list_length
= list
->length();
632 if (list_length
< 4) {
633 ir_read_error(st
, list
, "expected (expression <type> <operator> "
634 "<operand> [<operand>])");
638 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
639 const glsl_type
*type
= read_type(st
, type_expr
);
643 /* Read the operator */
644 s_symbol
*op_sym
= SX_AS_SYMBOL(type_expr
->next
);
645 if (op_sym
== NULL
) {
646 ir_read_error(st
, list
, "expected operator, found non-symbol");
650 ir_expression_operation op
= ir_expression::get_operator(op_sym
->value());
651 if (op
== (ir_expression_operation
) -1) {
652 ir_read_error(st
, list
, "invalid operator: %s", op_sym
->value());
656 /* Now that we know the operator, check for the right number of operands */
657 if (ir_expression::get_num_operands(op
) == 2) {
658 if (list_length
!= 5) {
659 ir_read_error(st
, list
, "expected (expression <type> %s <operand> "
660 " <operand>)", op_sym
->value());
664 if (list_length
!= 4) {
665 ir_read_error(st
, list
, "expected (expression <type> %s <operand>)",
671 s_expression
*exp1
= (s_expression
*) (op_sym
->next
);
672 ir_rvalue
*arg1
= read_rvalue(st
, exp1
);
674 ir_read_error(st
, NULL
, "when reading first operand of %s",
679 ir_rvalue
*arg2
= NULL
;
680 if (ir_expression::get_num_operands(op
) == 2) {
681 s_expression
*exp2
= (s_expression
*) (exp1
->next
);
682 arg2
= read_rvalue(st
, exp2
);
684 ir_read_error(st
, NULL
, "when reading second operand of %s",
690 return new ir_expression(op
, type
, arg1
, arg2
);
694 read_swizzle(_mesa_glsl_parse_state
*st
, s_list
*list
)
696 if (list
->length() != 3) {
697 ir_read_error(st
, list
, "expected (swiz <swizzle> <rvalue>)");
701 s_symbol
*swiz
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
703 ir_read_error(st
, list
, "expected a valid swizzle; found non-symbol");
707 if (strlen(swiz
->value()) > 4) {
708 ir_read_error(st
, list
, "expected a valid swizzle; found %s",
713 s_expression
*sub
= (s_expression
*) swiz
->next
;
715 ir_read_error(st
, list
, "expected rvalue: (swizzle %s <rvalue>)",
720 ir_rvalue
*rvalue
= read_rvalue(st
, sub
);
724 ir_swizzle
*ir
= ir_swizzle::create(rvalue
, swiz
->value(),
725 rvalue
->type
->vector_elements
);
727 ir_read_error(st
, list
, "invalid swizzle");
733 read_constant(_mesa_glsl_parse_state
*st
, s_list
*list
)
735 if (list
->length() != 3) {
736 ir_read_error(st
, list
, "expected (constant <type> (<num> ... <num>))");
740 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
741 const glsl_type
*type
= read_type(st
, type_expr
);
745 s_list
*values
= SX_AS_LIST(type_expr
->next
);
746 if (values
== NULL
) {
747 ir_read_error(st
, list
, "expected (constant <type> (<num> ... <num>))");
751 const glsl_type
*const base_type
= type
->get_base_type();
758 // Read in list of values (at most 16).
760 foreach_iter(exec_list_iterator
, it
, values
->subexpressions
) {
762 ir_read_error(st
, values
, "expected at most 16 numbers");
766 s_expression
*expr
= (s_expression
*) it
.get();
768 if (base_type
->base_type
== GLSL_TYPE_FLOAT
) {
769 s_number
*value
= SX_AS_NUMBER(expr
);
771 ir_read_error(st
, values
, "expected numbers");
774 f
[k
] = value
->fvalue();
776 s_int
*value
= SX_AS_INT(expr
);
778 ir_read_error(st
, values
, "expected integers");
782 switch (base_type
->base_type
) {
783 case GLSL_TYPE_UINT
: {
784 u
[k
] = value
->value();
787 case GLSL_TYPE_INT
: {
788 i
[k
] = value
->value();
791 case GLSL_TYPE_BOOL
: {
792 b
[k
] = value
->value();
796 ir_read_error(st
, values
, "unsupported constant type");
802 switch (base_type
->base_type
) {
804 return new ir_constant(type
, u
);
806 return new ir_constant(type
, i
);
808 return new ir_constant(type
, b
);
809 case GLSL_TYPE_FLOAT
:
810 return new ir_constant(type
, f
);
812 return NULL
; // should not be reached
815 static ir_dereference
*
816 read_dereference(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
818 s_list
*list
= SX_AS_LIST(expr
);
819 if (list
== NULL
|| list
->subexpressions
.is_empty())
822 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.head
);
825 if (strcmp(tag
->value(), "var_ref") == 0)
826 return read_var_ref(st
, list
);
827 if (strcmp(tag
->value(), "array_ref") == 0)
828 return read_array_ref(st
, list
);
829 if (strcmp(tag
->value(), "record_ref") == 0)
830 return read_record_ref(st
, list
);
834 static ir_dereference
*
835 read_var_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
837 if (list
->length() != 2) {
838 ir_read_error(st
, list
, "expected (var_ref <variable name>)");
841 s_symbol
*var_name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
842 if (var_name
== NULL
) {
843 ir_read_error(st
, list
, "expected (var_ref <variable name>)");
847 ir_variable
*var
= st
->symbols
->get_variable(var_name
->value());
849 ir_read_error(st
, list
, "undeclared variable: %s", var_name
->value());
853 return new ir_dereference_variable(var
);
856 static ir_dereference
*
857 read_array_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
859 if (list
->length() != 3) {
860 ir_read_error(st
, list
, "expected (array_ref <rvalue> <index>)");
864 s_expression
*subj_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
865 ir_rvalue
*subject
= read_rvalue(st
, subj_expr
);
866 if (subject
== NULL
) {
867 ir_read_error(st
, NULL
, "when reading the subject of an array_ref");
871 s_expression
*idx_expr
= (s_expression
*) subj_expr
->next
;
872 ir_rvalue
*idx
= read_rvalue(st
, idx_expr
);
873 return new ir_dereference_array(subject
, idx
);
876 static ir_dereference
*
877 read_record_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
879 if (list
->length() != 3) {
880 ir_read_error(st
, list
, "expected (record_ref <rvalue> <field>)");
884 s_expression
*subj_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
885 ir_rvalue
*subject
= read_rvalue(st
, subj_expr
);
886 if (subject
== NULL
) {
887 ir_read_error(st
, NULL
, "when reading the subject of a record_ref");
891 s_symbol
*field
= SX_AS_SYMBOL(subj_expr
->next
);
893 ir_read_error(st
, list
, "expected (record_ref ... <field name>)");
896 return new ir_dereference_record(subject
, field
->value());
900 valid_texture_list_length(ir_texture_opcode op
, s_list
*list
)
902 unsigned required_length
= 7;
905 else if (op
== ir_tex
)
908 return list
->length() == required_length
;
912 read_texture(_mesa_glsl_parse_state
*st
, s_list
*list
)
914 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.head
);
917 ir_texture_opcode op
= ir_texture::get_opcode(tag
->value());
918 if (op
== (ir_texture_opcode
) -1)
921 if (!valid_texture_list_length(op
, list
)) {
922 ir_read_error(st
, NULL
, "invalid list size in (%s ...)", tag
->value());
926 ir_texture
*tex
= new ir_texture(op
);
928 // Read sampler (must be a deref)
929 s_expression
*sampler_expr
= (s_expression
*) tag
->next
;
930 ir_dereference
*sampler
= read_dereference(st
, sampler_expr
);
931 if (sampler
== NULL
) {
932 ir_read_error(st
, NULL
, "when reading sampler in (%s ...)", tag
->value());
935 tex
->set_sampler(sampler
);
937 // Read coordinate (any rvalue)
938 s_expression
*coordinate_expr
= (s_expression
*) sampler_expr
->next
;
939 tex
->coordinate
= read_rvalue(st
, coordinate_expr
);
940 if (tex
->coordinate
== NULL
) {
941 ir_read_error(st
, NULL
, "when reading coordinate in (%s ...)",
946 // Read texel offset, i.e. (0 0 0)
947 s_list
*offset_list
= SX_AS_LIST(coordinate_expr
->next
);
948 if (offset_list
== NULL
|| offset_list
->length() != 3) {
949 ir_read_error(st
, offset_list
, "expected (<int> <int> <int>)");
952 s_int
*offset_x
= SX_AS_INT(offset_list
->subexpressions
.head
);
953 s_int
*offset_y
= SX_AS_INT(offset_x
->next
);
954 s_int
*offset_z
= SX_AS_INT(offset_y
->next
);
955 if (offset_x
== NULL
|| offset_y
== NULL
|| offset_z
== NULL
) {
956 ir_read_error(st
, offset_list
, "expected (<int> <int> <int>)");
959 tex
->offsets
[0] = offset_x
->value();
960 tex
->offsets
[1] = offset_y
->value();
961 tex
->offsets
[2] = offset_z
->value();
964 s_expression
*lod_expr
= (s_expression
*) offset_list
->next
;
965 tex
->lod_info
.lod
= read_rvalue(st
, lod_expr
);
966 if (tex
->lod_info
.lod
== NULL
) {
967 ir_read_error(st
, NULL
, "when reading LOD in (txf ...)");
971 s_expression
*proj_expr
= (s_expression
*) offset_list
->next
;
972 s_int
*proj_as_int
= SX_AS_INT(proj_expr
);
973 if (proj_as_int
&& proj_as_int
->value() == 1) {
974 tex
->projector
= NULL
;
976 tex
->projector
= read_rvalue(st
, proj_expr
);
977 if (tex
->projector
== NULL
) {
978 ir_read_error(st
, NULL
, "when reading projective divide in (%s ..)",
984 s_list
*shadow_list
= SX_AS_LIST(proj_expr
->next
);
985 if (shadow_list
== NULL
) {
986 ir_read_error(st
, NULL
, "shadow comparitor must be a list");
989 if (shadow_list
->subexpressions
.is_empty()) {
990 tex
->shadow_comparitor
= NULL
;
992 tex
->shadow_comparitor
= read_rvalue(st
, shadow_list
);
993 if (tex
->shadow_comparitor
== NULL
) {
994 ir_read_error(st
, NULL
, "when reading shadow comparitor in (%s ..)",
999 s_expression
*lod_expr
= (s_expression
*) shadow_list
->next
;
1003 tex
->lod_info
.bias
= read_rvalue(st
, lod_expr
);
1004 if (tex
->lod_info
.bias
== NULL
) {
1005 ir_read_error(st
, NULL
, "when reading LOD bias in (txb ...)");
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 (txl ...)");
1017 s_list
*lod_list
= SX_AS_LIST(lod_expr
);
1018 if (lod_list
->length() != 2) {
1019 ir_read_error(st
, lod_expr
, "expected (dPdx dPdy) in (txd ...)");
1022 s_expression
*dx_expr
= (s_expression
*) lod_list
->subexpressions
.head
;
1023 s_expression
*dy_expr
= (s_expression
*) dx_expr
->next
;
1025 tex
->lod_info
.grad
.dPdx
= read_rvalue(st
, dx_expr
);
1026 if (tex
->lod_info
.grad
.dPdx
== NULL
) {
1027 ir_read_error(st
, NULL
, "when reading dPdx in (txd ...)");
1030 tex
->lod_info
.grad
.dPdy
= read_rvalue(st
, dy_expr
);
1031 if (tex
->lod_info
.grad
.dPdy
== NULL
) {
1032 ir_read_error(st
, NULL
, "when reading dPdy in (txd ...)");
1038 // tex doesn't have any extra parameters and txf was handled earlier.