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_dereference
*read_var_ref(_mesa_glsl_parse_state
*, s_list
*);
57 static ir_dereference
*read_array_ref(_mesa_glsl_parse_state
*, s_list
*);
58 static ir_dereference
*read_record_ref(_mesa_glsl_parse_state
*, s_list
*);
61 _mesa_glsl_read_ir(_mesa_glsl_parse_state
*state
, exec_list
*instructions
,
64 s_expression
*expr
= s_expression::read_expression(src
);
66 ir_read_error(state
, NULL
, "couldn't parse S-Expression.");
70 _mesa_glsl_initialize_types(state
);
72 /* FINISHME: Constructors probably shouldn't be emitted as part of the IR.
73 * FINISHME: Once they're not, remake them by calling:
74 * FINISHME: _mesa_glsl_initialize_constructors(instructions, state);
77 scan_for_prototypes(state
, instructions
, expr
);
81 read_instructions(state
, instructions
, expr
, NULL
);
85 ir_read_error(_mesa_glsl_parse_state
*state
, s_expression
*expr
,
100 printf("...in this context:\n ");
106 static const glsl_type
*
107 read_type(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
109 s_list
*list
= SX_AS_LIST(expr
);
111 s_symbol
*type_sym
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
112 if (type_sym
== NULL
) {
113 ir_read_error(st
, expr
, "expected type (array ...) or (struct ...)");
116 if (strcmp(type_sym
->value(), "array") == 0) {
117 if (list
->length() != 3) {
118 ir_read_error(st
, expr
, "expected type (array <type> <int>)");
123 s_expression
*base_expr
= (s_expression
*) type_sym
->next
;
124 const glsl_type
*base_type
= read_type(st
, base_expr
);
125 if (base_type
== NULL
) {
126 ir_read_error(st
, NULL
, "when reading base type of array");
131 s_int
*size
= SX_AS_INT(base_expr
->next
);
133 ir_read_error(st
, expr
, "found non-integer array size");
137 return glsl_type::get_array_instance(base_type
, size
->value());
138 } else if (strcmp(type_sym
->value(), "struct") == 0) {
139 assert(false); // FINISHME
141 ir_read_error(st
, expr
, "expected (array ...) or (struct ...); "
142 "found (%s ...)", type_sym
->value());
147 s_symbol
*type_sym
= SX_AS_SYMBOL(expr
);
148 if (type_sym
== NULL
) {
149 ir_read_error(st
, expr
, "expected <type> (symbol or list)");
153 const glsl_type
*type
= st
->symbols
->get_type(type_sym
->value());
155 ir_read_error(st
, expr
, "invalid type: %s", type_sym
->value());
162 scan_for_prototypes(_mesa_glsl_parse_state
*st
, exec_list
*instructions
,
165 s_list
*list
= SX_AS_LIST(expr
);
167 ir_read_error(st
, expr
, "Expected (<instruction> ...); found an atom.");
171 foreach_iter(exec_list_iterator
, it
, list
->subexpressions
) {
172 s_list
*sub
= SX_AS_LIST(it
.get());
174 continue; // not a (function ...); ignore it.
176 s_symbol
*tag
= SX_AS_SYMBOL(sub
->subexpressions
.get_head());
177 if (tag
== NULL
|| strcmp(tag
->value(), "function") != 0)
178 continue; // not a (function ...); ignore it.
180 ir_function
*f
= read_function(st
, sub
, true);
183 instructions
->push_tail(f
);
188 read_function(_mesa_glsl_parse_state
*st
, s_list
*list
, bool skip_body
)
190 if (list
->length() < 3) {
191 ir_read_error(st
, list
, "Expected (function <name> (signature ...) ...)");
195 s_symbol
*name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
197 ir_read_error(st
, list
, "Expected (function <name> ...)");
201 ir_function
*f
= st
->symbols
->get_function(name
->value());
203 f
= new ir_function(name
->value());
204 bool added
= st
->symbols
->add_function(name
->value(), f
);
208 exec_list_iterator it
= list
->subexpressions
.iterator();
209 it
.next(); // skip "function" tag
210 it
.next(); // skip function name
211 for (/* nothing */; it
.has_next(); it
.next()) {
212 s_list
*siglist
= SX_AS_LIST(it
.get());
213 if (siglist
== NULL
) {
214 ir_read_error(st
, list
, "Expected (function (signature ...) ...)");
218 s_symbol
*tag
= SX_AS_SYMBOL(siglist
->subexpressions
.get_head());
219 if (tag
== NULL
|| strcmp(tag
->value(), "signature") != 0) {
220 ir_read_error(st
, siglist
, "Expected (signature ...)");
224 read_function_sig(st
, f
, siglist
, skip_body
);
230 read_function_sig(_mesa_glsl_parse_state
*st
, ir_function
*f
, s_list
*list
,
233 if (list
->length() != 4) {
234 ir_read_error(st
, list
, "Expected (signature <type> (parameters ...) "
235 "(<instruction> ...))");
239 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
240 const glsl_type
*return_type
= read_type(st
, type_expr
);
241 if (return_type
== NULL
)
244 s_list
*paramlist
= SX_AS_LIST(type_expr
->next
);
245 s_list
*body_list
= SX_AS_LIST(paramlist
->next
);
246 if (paramlist
== NULL
|| body_list
== NULL
) {
247 ir_read_error(st
, list
, "Expected (signature <type> (parameters ...) "
248 "(<instruction> ...))");
251 s_symbol
*paramtag
= SX_AS_SYMBOL(paramlist
->subexpressions
.get_head());
252 if (paramtag
== NULL
|| strcmp(paramtag
->value(), "parameters") != 0) {
253 ir_read_error(st
, paramlist
, "Expected (parameters ...)");
257 // Read the parameters list into a temporary place.
258 exec_list hir_parameters
;
259 st
->symbols
->push_scope();
261 exec_list_iterator it
= paramlist
->subexpressions
.iterator();
262 for (it
.next() /* skip "parameters" */; it
.has_next(); it
.next()) {
263 s_list
*decl
= SX_AS_LIST(it
.get());
264 ir_variable
*var
= read_declaration(st
, decl
);
268 hir_parameters
.push_tail(var
);
271 ir_function_signature
*sig
= f
->exact_matching_signature(&hir_parameters
);
273 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
274 if (badvar
!= NULL
) {
275 ir_read_error(st
, list
, "function `%s' parameter `%s' qualifiers "
276 "don't match prototype", f
->name
, badvar
);
280 if (sig
->return_type
!= return_type
) {
281 ir_read_error(st
, list
, "function `%s' return type doesn't "
282 "match prototype", f
->name
);
286 sig
= new ir_function_signature(return_type
);
287 f
->add_signature(sig
);
290 sig
->replace_parameters(&hir_parameters
);
293 if (sig
->is_defined
) {
294 ir_read_error(st
, list
, "function %s redefined", f
->name
);
297 read_instructions(st
, &sig
->body
, body_list
, NULL
);
300 st
->symbols
->pop_scope();
304 read_instructions(_mesa_glsl_parse_state
*st
, exec_list
*instructions
,
305 s_expression
*expr
, ir_loop
*loop_ctx
)
307 // Read in a list of instructions
308 s_list
*list
= SX_AS_LIST(expr
);
310 ir_read_error(st
, expr
, "Expected (<instruction> ...); found an atom.");
314 foreach_iter(exec_list_iterator
, it
, list
->subexpressions
) {
315 s_expression
*sub
= (s_expression
*) it
.get();
316 ir_instruction
*ir
= read_instruction(st
, sub
, loop_ctx
);
318 ir_read_error(st
, sub
, "Invalid instruction.\n");
321 instructions
->push_tail(ir
);
326 static ir_instruction
*
327 read_instruction(_mesa_glsl_parse_state
*st
, s_expression
*expr
,
330 s_symbol
*symbol
= SX_AS_SYMBOL(expr
);
331 if (symbol
!= NULL
) {
332 if (strcmp(symbol
->value(), "break") == 0 && loop_ctx
!= NULL
)
333 return new ir_loop_jump(loop_ctx
, ir_loop_jump::jump_break
);
334 if (strcmp(symbol
->value(), "continue") == 0 && loop_ctx
!= NULL
)
335 return new ir_loop_jump(loop_ctx
, ir_loop_jump::jump_continue
);
338 s_list
*list
= SX_AS_LIST(expr
);
339 if (list
== NULL
|| list
->subexpressions
.is_empty())
342 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
344 ir_read_error(st
, expr
, "expected instruction tag");
348 ir_instruction
*inst
= NULL
;
349 if (strcmp(tag
->value(), "declare") == 0) {
350 inst
= read_declaration(st
, list
);
351 } else if (strcmp(tag
->value(), "if") == 0) {
352 inst
= read_if(st
, list
, loop_ctx
);
353 } else if (strcmp(tag
->value(), "loop") == 0) {
354 inst
= read_loop(st
, list
);
355 } else if (strcmp(tag
->value(), "return") == 0) {
356 inst
= read_return(st
, list
);
357 } else if (strcmp(tag
->value(), "function") == 0) {
358 inst
= read_function(st
, list
, false);
360 inst
= read_rvalue(st
, list
);
362 ir_read_error(st
, NULL
, "when reading instruction");
369 read_declaration(_mesa_glsl_parse_state
*st
, s_list
*list
)
371 if (list
->length() != 4) {
372 ir_read_error(st
, list
, "expected (declare (<qualifiers>) <type> "
377 s_list
*quals
= SX_AS_LIST(list
->subexpressions
.head
->next
);
379 ir_read_error(st
, list
, "expected a list of variable qualifiers");
383 s_expression
*type_expr
= (s_expression
*) quals
->next
;
384 const glsl_type
*type
= read_type(st
, type_expr
);
388 s_symbol
*var_name
= SX_AS_SYMBOL(type_expr
->next
);
389 if (var_name
== NULL
) {
390 ir_read_error(st
, list
, "expected variable name, found non-symbol");
394 ir_variable
*var
= new ir_variable(type
, var_name
->value());
396 foreach_iter(exec_list_iterator
, it
, quals
->subexpressions
) {
397 s_symbol
*qualifier
= SX_AS_SYMBOL(it
.get());
398 if (qualifier
== NULL
) {
399 ir_read_error(st
, list
, "qualifier list must contain only symbols");
404 // FINISHME: Check for duplicate/conflicting qualifiers.
405 if (strcmp(qualifier
->value(), "centroid") == 0) {
407 } else if (strcmp(qualifier
->value(), "invariant") == 0) {
409 } else if (strcmp(qualifier
->value(), "uniform") == 0) {
410 var
->mode
= ir_var_uniform
;
411 } else if (strcmp(qualifier
->value(), "auto") == 0) {
412 var
->mode
= ir_var_auto
;
413 } else if (strcmp(qualifier
->value(), "in") == 0) {
414 var
->mode
= ir_var_in
;
415 } else if (strcmp(qualifier
->value(), "out") == 0) {
416 var
->mode
= ir_var_out
;
417 } else if (strcmp(qualifier
->value(), "inout") == 0) {
418 var
->mode
= ir_var_inout
;
419 } else if (strcmp(qualifier
->value(), "smooth") == 0) {
420 var
->interpolation
= ir_var_smooth
;
421 } else if (strcmp(qualifier
->value(), "flat") == 0) {
422 var
->interpolation
= ir_var_flat
;
423 } else if (strcmp(qualifier
->value(), "noperspective") == 0) {
424 var
->interpolation
= ir_var_noperspective
;
426 ir_read_error(st
, list
, "unknown qualifier: %s", qualifier
->value());
432 // Add the variable to the symbol table
433 st
->symbols
->add_variable(var_name
->value(), var
);
440 read_if(_mesa_glsl_parse_state
*st
, s_list
*list
, ir_loop
*loop_ctx
)
442 if (list
->length() != 4) {
443 ir_read_error(st
, list
, "expected (if <condition> (<then> ...) "
448 s_expression
*cond_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
449 ir_rvalue
*condition
= read_rvalue(st
, cond_expr
);
450 if (condition
== NULL
) {
451 ir_read_error(st
, NULL
, "when reading condition of (if ...)");
455 s_expression
*then_expr
= (s_expression
*) cond_expr
->next
;
456 s_expression
*else_expr
= (s_expression
*) then_expr
->next
;
458 ir_if
*iff
= new ir_if(condition
);
460 read_instructions(st
, &iff
->then_instructions
, then_expr
, loop_ctx
);
461 read_instructions(st
, &iff
->else_instructions
, else_expr
, loop_ctx
);
471 read_loop(_mesa_glsl_parse_state
*st
, s_list
*list
)
473 if (list
->length() != 6) {
474 ir_read_error(st
, list
, "expected (loop <counter> <from> <to> "
475 "<increment> <body>)");
479 s_expression
*count_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
480 s_expression
*from_expr
= (s_expression
*) count_expr
->next
;
481 s_expression
*to_expr
= (s_expression
*) from_expr
->next
;
482 s_expression
*inc_expr
= (s_expression
*) to_expr
->next
;
483 s_expression
*body_expr
= (s_expression
*) inc_expr
->next
;
485 // FINISHME: actually read the count/from/to fields.
487 ir_loop
*loop
= new ir_loop
;
488 read_instructions(st
, &loop
->body_instructions
, body_expr
, loop
);
498 read_return(_mesa_glsl_parse_state
*st
, s_list
*list
)
500 if (list
->length() != 2) {
501 ir_read_error(st
, list
, "expected (return <rvalue>)");
505 s_expression
*expr
= (s_expression
*) list
->subexpressions
.head
->next
;
507 ir_rvalue
*retval
= read_rvalue(st
, expr
);
508 if (retval
== NULL
) {
509 ir_read_error(st
, NULL
, "when reading return value");
513 return new ir_return(retval
);
518 read_rvalue(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
520 s_list
*list
= SX_AS_LIST(expr
);
521 if (list
== NULL
|| list
->subexpressions
.is_empty())
524 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
526 ir_read_error(st
, expr
, "expected rvalue tag");
530 ir_rvalue
*rvalue
= NULL
;
531 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
);
541 } else if (strcmp(tag
->value(), "var_ref") == 0) {
542 rvalue
= read_var_ref(st
, list
);
543 } else if (strcmp(tag
->value(), "array_ref") == 0) {
544 rvalue
= read_array_ref(st
, list
);
545 } else if (strcmp(tag
->value(), "record_ref") == 0) {
546 rvalue
= read_record_ref(st
, list
);
548 ir_read_error(st
, expr
, "unrecognized rvalue tag: %s", tag
->value());
554 static ir_assignment
*
555 read_assignment(_mesa_glsl_parse_state
*st
, s_list
*list
)
557 if (list
->length() != 4) {
558 ir_read_error(st
, list
, "expected (assign <condition> <lhs> <rhs>)");
562 s_expression
*cond_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
563 s_expression
*lhs_expr
= (s_expression
*) cond_expr
->next
;
564 s_expression
*rhs_expr
= (s_expression
*) lhs_expr
->next
;
566 // FINISHME: Deal with "true" condition
567 ir_rvalue
*condition
= read_rvalue(st
, cond_expr
);
568 if (condition
== NULL
) {
569 ir_read_error(st
, NULL
, "when reading condition of assignment");
573 ir_rvalue
*lhs
= read_rvalue(st
, lhs_expr
);
575 ir_read_error(st
, NULL
, "when reading left-hand side of assignment");
579 ir_rvalue
*rhs
= read_rvalue(st
, rhs_expr
);
581 ir_read_error(st
, NULL
, "when reading right-hand side of assignment");
585 return new ir_assignment(lhs
, rhs
, condition
);
589 read_call(_mesa_glsl_parse_state
*st
, s_list
*list
)
591 if (list
->length() != 3) {
592 ir_read_error(st
, list
, "expected (call <name> (<param> ...))");
596 s_symbol
*name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
597 s_list
*params
= SX_AS_LIST(name
->next
);
598 if (name
== NULL
|| params
== NULL
) {
599 ir_read_error(st
, list
, "expected (call <name> (<param> ...))");
603 exec_list parameters
;
605 foreach_iter(exec_list_iterator
, it
, params
->subexpressions
) {
606 s_expression
*expr
= (s_expression
*) it
.get();
607 ir_rvalue
*param
= read_rvalue(st
, expr
);
609 ir_read_error(st
, list
, "when reading parameter to function call");
612 parameters
.push_tail(param
);
615 ir_function
*f
= st
->symbols
->get_function(name
->value());
617 ir_read_error(st
, list
, "found call to undefined function %s",
622 const ir_function_signature
*callee
= f
->matching_signature(¶meters
);
623 if (callee
== NULL
) {
624 ir_read_error(st
, list
, "couldn't find matching signature for function "
625 "%s", name
->value());
629 return new ir_call(callee
, ¶meters
);
632 static ir_expression
*
633 read_expression(_mesa_glsl_parse_state
*st
, s_list
*list
)
635 const unsigned list_length
= list
->length();
636 if (list_length
< 4) {
637 ir_read_error(st
, list
, "expected (expression <type> <operator> "
638 "<operand> [<operand>])");
642 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
643 const glsl_type
*type
= read_type(st
, type_expr
);
647 /* Read the operator */
648 s_symbol
*op_sym
= SX_AS_SYMBOL(type_expr
->next
);
649 if (op_sym
== NULL
) {
650 ir_read_error(st
, list
, "expected operator, found non-symbol");
654 ir_expression_operation op
= ir_expression::get_operator(op_sym
->value());
655 if (op
== (ir_expression_operation
) -1) {
656 ir_read_error(st
, list
, "invalid operator: %s", op_sym
->value());
660 /* Now that we know the operator, check for the right number of operands */
661 if (ir_expression::get_num_operands(op
) == 2) {
662 if (list_length
!= 5) {
663 ir_read_error(st
, list
, "expected (expression <type> %s <operand> "
664 " <operand>)", op_sym
->value());
668 if (list_length
!= 4) {
669 ir_read_error(st
, list
, "expected (expression <type> %s <operand>)",
675 s_expression
*exp1
= (s_expression
*) (op_sym
->next
);
676 ir_rvalue
*arg1
= read_rvalue(st
, exp1
);
678 ir_read_error(st
, NULL
, "when reading first operand of %s",
683 ir_rvalue
*arg2
= NULL
;
684 if (ir_expression::get_num_operands(op
) == 2) {
685 s_expression
*exp2
= (s_expression
*) (exp1
->next
);
686 arg2
= read_rvalue(st
, exp2
);
688 ir_read_error(st
, NULL
, "when reading second operand of %s",
694 return new ir_expression(op
, type
, arg1
, arg2
);
698 read_swizzle(_mesa_glsl_parse_state
*st
, s_list
*list
)
700 if (list
->length() != 3) {
701 ir_read_error(st
, list
, "expected (swiz <swizzle> <rvalue>)");
705 s_symbol
*swiz
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
707 ir_read_error(st
, list
, "expected a valid swizzle; found non-symbol");
711 if (strlen(swiz
->value()) > 4) {
712 ir_read_error(st
, list
, "expected a valid swizzle; found %s",
717 s_expression
*sub
= (s_expression
*) swiz
->next
;
719 ir_read_error(st
, list
, "expected rvalue: (swizzle %s <rvalue>)",
724 ir_rvalue
*rvalue
= read_rvalue(st
, sub
);
728 ir_swizzle
*ir
= ir_swizzle::create(rvalue
, swiz
->value(),
729 rvalue
->type
->vector_elements
);
731 ir_read_error(st
, list
, "invalid swizzle");
737 read_constant(_mesa_glsl_parse_state
*st
, s_list
*list
)
739 if (list
->length() != 3) {
740 ir_read_error(st
, list
, "expected (constant <type> (<num> ... <num>))");
744 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
745 const glsl_type
*type
= read_type(st
, type_expr
);
749 s_list
*values
= SX_AS_LIST(type_expr
->next
);
750 if (values
== NULL
) {
751 ir_read_error(st
, list
, "expected (constant <type> (<num> ... <num>))");
755 const glsl_type
*const base_type
= type
->get_base_type();
762 // Read in list of values (at most 16).
764 foreach_iter(exec_list_iterator
, it
, values
->subexpressions
) {
766 ir_read_error(st
, values
, "expected at most 16 numbers");
770 s_expression
*expr
= (s_expression
*) it
.get();
772 if (base_type
->base_type
== GLSL_TYPE_FLOAT
) {
773 s_number
*value
= SX_AS_NUMBER(expr
);
775 ir_read_error(st
, values
, "expected numbers");
778 f
[k
] = value
->fvalue();
780 s_int
*value
= SX_AS_INT(expr
);
782 ir_read_error(st
, values
, "expected integers");
786 switch (base_type
->base_type
) {
787 case GLSL_TYPE_UINT
: {
788 u
[k
] = value
->value();
791 case GLSL_TYPE_INT
: {
792 i
[k
] = value
->value();
795 case GLSL_TYPE_BOOL
: {
796 b
[k
] = value
->value();
800 ir_read_error(st
, values
, "unsupported constant type");
806 switch (base_type
->base_type
) {
808 return new ir_constant(type
, u
);
810 return new ir_constant(type
, i
);
812 return new ir_constant(type
, b
);
813 case GLSL_TYPE_FLOAT
:
814 return new ir_constant(type
, f
);
816 return NULL
; // should not be reached
819 static ir_instruction
*
820 read_dereferencable(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
822 // Read the subject of a dereference - either a variable name or a swizzle
823 s_symbol
*var_name
= SX_AS_SYMBOL(expr
);
824 if (var_name
!= NULL
) {
825 ir_variable
*var
= st
->symbols
->get_variable(var_name
->value());
827 ir_read_error(st
, expr
, "undeclared variable: %s", var_name
->value());
831 // Hopefully a (swiz ...)
832 s_list
*list
= SX_AS_LIST(expr
);
833 if (list
!= NULL
&& !list
->subexpressions
.is_empty()) {
834 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.head
);
835 if (tag
!= NULL
&& strcmp(tag
->value(), "swiz") == 0)
836 return read_swizzle(st
, list
);
839 ir_read_error(st
, expr
, "expected variable name or (swiz ...)");
843 static ir_dereference
*
844 read_var_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
846 if (list
->length() != 2) {
847 ir_read_error(st
, list
, "expected (var_ref <variable name or (swiz)>)");
850 s_expression
*subj_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
851 ir_instruction
*subject
= read_dereferencable(st
, subj_expr
);
854 return new ir_dereference(subject
);
857 static ir_dereference
*
858 read_array_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
860 if (list
->length() != 3) {
861 ir_read_error(st
, list
, "expected (array_ref <variable name or (swiz)> "
866 s_expression
*subj_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
867 ir_instruction
*subject
= read_dereferencable(st
, subj_expr
);
871 s_expression
*idx_expr
= (s_expression
*) subj_expr
->next
;
872 ir_rvalue
*idx
= read_rvalue(st
, idx_expr
);
873 return new ir_dereference(subject
, idx
);
876 static ir_dereference
*
877 read_record_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
879 ir_read_error(st
, list
, "FINISHME: record refs not yet supported.");