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(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
);
87 ir_read_error(_mesa_glsl_parse_state
*state
, s_expression
*expr
,
94 state
->info_log
= talloc_strdup_append(state
->info_log
, "error: ");
97 state
->info_log
= talloc_vasprintf_append(state
->info_log
, fmt
, ap
);
99 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n");
102 state
->info_log
= talloc_strdup_append(state
->info_log
,
103 "...in this context:\n ");
105 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n\n");
109 static const glsl_type
*
110 read_type(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
112 s_list
*list
= SX_AS_LIST(expr
);
114 s_symbol
*type_sym
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
115 if (type_sym
== NULL
) {
116 ir_read_error(st
, expr
, "expected type (array ...) or (struct ...)");
119 if (strcmp(type_sym
->value(), "array") == 0) {
120 if (list
->length() != 3) {
121 ir_read_error(st
, expr
, "expected type (array <type> <int>)");
126 s_expression
*base_expr
= (s_expression
*) type_sym
->next
;
127 const glsl_type
*base_type
= read_type(st
, base_expr
);
128 if (base_type
== NULL
) {
129 ir_read_error(st
, NULL
, "when reading base type of array");
134 s_int
*size
= SX_AS_INT(base_expr
->next
);
136 ir_read_error(st
, expr
, "found non-integer array size");
140 return glsl_type::get_array_instance(base_type
, size
->value());
141 } else if (strcmp(type_sym
->value(), "struct") == 0) {
142 assert(false); // FINISHME
144 ir_read_error(st
, expr
, "expected (array ...) or (struct ...); "
145 "found (%s ...)", type_sym
->value());
150 s_symbol
*type_sym
= SX_AS_SYMBOL(expr
);
151 if (type_sym
== NULL
) {
152 ir_read_error(st
, expr
, "expected <type> (symbol or list)");
156 const glsl_type
*type
= st
->symbols
->get_type(type_sym
->value());
158 ir_read_error(st
, expr
, "invalid type: %s", type_sym
->value());
165 scan_for_prototypes(_mesa_glsl_parse_state
*st
, exec_list
*instructions
,
168 s_list
*list
= SX_AS_LIST(expr
);
170 ir_read_error(st
, expr
, "Expected (<instruction> ...); found an atom.");
174 foreach_iter(exec_list_iterator
, it
, list
->subexpressions
) {
175 s_list
*sub
= SX_AS_LIST(it
.get());
177 continue; // not a (function ...); ignore it.
179 s_symbol
*tag
= SX_AS_SYMBOL(sub
->subexpressions
.get_head());
180 if (tag
== NULL
|| strcmp(tag
->value(), "function") != 0)
181 continue; // not a (function ...); ignore it.
183 ir_function
*f
= read_function(st
, sub
, true);
186 instructions
->push_tail(f
);
191 read_function(_mesa_glsl_parse_state
*st
, s_list
*list
, bool skip_body
)
193 if (list
->length() < 3) {
194 ir_read_error(st
, list
, "Expected (function <name> (signature ...) ...)");
198 s_symbol
*name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
200 ir_read_error(st
, list
, "Expected (function <name> ...)");
204 ir_function
*f
= st
->symbols
->get_function(name
->value());
206 f
= new ir_function(name
->value());
207 bool added
= st
->symbols
->add_function(name
->value(), f
);
211 exec_list_iterator it
= list
->subexpressions
.iterator();
212 it
.next(); // skip "function" tag
213 it
.next(); // skip function name
214 for (/* nothing */; it
.has_next(); it
.next()) {
215 s_list
*siglist
= SX_AS_LIST(it
.get());
216 if (siglist
== NULL
) {
217 ir_read_error(st
, list
, "Expected (function (signature ...) ...)");
221 s_symbol
*tag
= SX_AS_SYMBOL(siglist
->subexpressions
.get_head());
222 if (tag
== NULL
|| strcmp(tag
->value(), "signature") != 0) {
223 ir_read_error(st
, siglist
, "Expected (signature ...)");
227 read_function_sig(st
, f
, siglist
, skip_body
);
233 read_function_sig(_mesa_glsl_parse_state
*st
, ir_function
*f
, s_list
*list
,
236 if (list
->length() != 4) {
237 ir_read_error(st
, list
, "Expected (signature <type> (parameters ...) "
238 "(<instruction> ...))");
242 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
243 const glsl_type
*return_type
= read_type(st
, type_expr
);
244 if (return_type
== NULL
)
247 s_list
*paramlist
= SX_AS_LIST(type_expr
->next
);
248 s_list
*body_list
= SX_AS_LIST(paramlist
->next
);
249 if (paramlist
== NULL
|| body_list
== NULL
) {
250 ir_read_error(st
, list
, "Expected (signature <type> (parameters ...) "
251 "(<instruction> ...))");
254 s_symbol
*paramtag
= SX_AS_SYMBOL(paramlist
->subexpressions
.get_head());
255 if (paramtag
== NULL
|| strcmp(paramtag
->value(), "parameters") != 0) {
256 ir_read_error(st
, paramlist
, "Expected (parameters ...)");
260 // Read the parameters list into a temporary place.
261 exec_list hir_parameters
;
262 st
->symbols
->push_scope();
264 exec_list_iterator it
= paramlist
->subexpressions
.iterator();
265 for (it
.next() /* skip "parameters" */; it
.has_next(); it
.next()) {
266 s_list
*decl
= SX_AS_LIST(it
.get());
267 ir_variable
*var
= read_declaration(st
, decl
);
271 hir_parameters
.push_tail(var
);
274 ir_function_signature
*sig
= f
->exact_matching_signature(&hir_parameters
);
276 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
277 if (badvar
!= NULL
) {
278 ir_read_error(st
, list
, "function `%s' parameter `%s' qualifiers "
279 "don't match prototype", f
->name
, badvar
);
283 if (sig
->return_type
!= return_type
) {
284 ir_read_error(st
, list
, "function `%s' return type doesn't "
285 "match prototype", f
->name
);
289 sig
= new ir_function_signature(return_type
);
290 f
->add_signature(sig
);
293 sig
->replace_parameters(&hir_parameters
);
296 if (sig
->is_defined
) {
297 ir_read_error(st
, list
, "function %s redefined", f
->name
);
300 read_instructions(st
, &sig
->body
, body_list
, NULL
);
301 sig
->is_defined
= true;
304 st
->symbols
->pop_scope();
308 read_instructions(_mesa_glsl_parse_state
*st
, exec_list
*instructions
,
309 s_expression
*expr
, ir_loop
*loop_ctx
)
311 // Read in a list of instructions
312 s_list
*list
= SX_AS_LIST(expr
);
314 ir_read_error(st
, expr
, "Expected (<instruction> ...); found an atom.");
318 foreach_iter(exec_list_iterator
, it
, list
->subexpressions
) {
319 s_expression
*sub
= (s_expression
*) it
.get();
320 ir_instruction
*ir
= read_instruction(st
, sub
, loop_ctx
);
322 ir_read_error(st
, sub
, "Invalid instruction.\n");
325 instructions
->push_tail(ir
);
330 static ir_instruction
*
331 read_instruction(_mesa_glsl_parse_state
*st
, s_expression
*expr
,
334 s_symbol
*symbol
= SX_AS_SYMBOL(expr
);
335 if (symbol
!= NULL
) {
336 if (strcmp(symbol
->value(), "break") == 0 && loop_ctx
!= NULL
)
337 return new ir_loop_jump(loop_ctx
, ir_loop_jump::jump_break
);
338 if (strcmp(symbol
->value(), "continue") == 0 && loop_ctx
!= NULL
)
339 return new ir_loop_jump(loop_ctx
, ir_loop_jump::jump_continue
);
342 s_list
*list
= SX_AS_LIST(expr
);
343 if (list
== NULL
|| list
->subexpressions
.is_empty())
346 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
348 ir_read_error(st
, expr
, "expected instruction tag");
352 ir_instruction
*inst
= NULL
;
353 if (strcmp(tag
->value(), "declare") == 0) {
354 inst
= read_declaration(st
, list
);
355 } else if (strcmp(tag
->value(), "if") == 0) {
356 inst
= read_if(st
, list
, loop_ctx
);
357 } else if (strcmp(tag
->value(), "loop") == 0) {
358 inst
= read_loop(st
, list
);
359 } else if (strcmp(tag
->value(), "return") == 0) {
360 inst
= read_return(st
, list
);
361 } else if (strcmp(tag
->value(), "function") == 0) {
362 inst
= read_function(st
, list
, false);
364 inst
= read_rvalue(st
, list
);
366 ir_read_error(st
, NULL
, "when reading instruction");
373 read_declaration(_mesa_glsl_parse_state
*st
, s_list
*list
)
375 if (list
->length() != 4) {
376 ir_read_error(st
, list
, "expected (declare (<qualifiers>) <type> "
381 s_list
*quals
= SX_AS_LIST(list
->subexpressions
.head
->next
);
383 ir_read_error(st
, list
, "expected a list of variable qualifiers");
387 s_expression
*type_expr
= (s_expression
*) quals
->next
;
388 const glsl_type
*type
= read_type(st
, type_expr
);
392 s_symbol
*var_name
= SX_AS_SYMBOL(type_expr
->next
);
393 if (var_name
== NULL
) {
394 ir_read_error(st
, list
, "expected variable name, found non-symbol");
398 ir_variable
*var
= new ir_variable(type
, var_name
->value());
400 foreach_iter(exec_list_iterator
, it
, quals
->subexpressions
) {
401 s_symbol
*qualifier
= SX_AS_SYMBOL(it
.get());
402 if (qualifier
== NULL
) {
403 ir_read_error(st
, list
, "qualifier list must contain only symbols");
408 // FINISHME: Check for duplicate/conflicting qualifiers.
409 if (strcmp(qualifier
->value(), "centroid") == 0) {
411 } else if (strcmp(qualifier
->value(), "invariant") == 0) {
413 } else if (strcmp(qualifier
->value(), "uniform") == 0) {
414 var
->mode
= ir_var_uniform
;
415 } else if (strcmp(qualifier
->value(), "auto") == 0) {
416 var
->mode
= ir_var_auto
;
417 } else if (strcmp(qualifier
->value(), "in") == 0) {
418 var
->mode
= ir_var_in
;
419 } else if (strcmp(qualifier
->value(), "out") == 0) {
420 var
->mode
= ir_var_out
;
421 } else if (strcmp(qualifier
->value(), "inout") == 0) {
422 var
->mode
= ir_var_inout
;
423 } else if (strcmp(qualifier
->value(), "smooth") == 0) {
424 var
->interpolation
= ir_var_smooth
;
425 } else if (strcmp(qualifier
->value(), "flat") == 0) {
426 var
->interpolation
= ir_var_flat
;
427 } else if (strcmp(qualifier
->value(), "noperspective") == 0) {
428 var
->interpolation
= ir_var_noperspective
;
430 ir_read_error(st
, list
, "unknown qualifier: %s", qualifier
->value());
436 // Add the variable to the symbol table
437 st
->symbols
->add_variable(var_name
->value(), var
);
444 read_if(_mesa_glsl_parse_state
*st
, s_list
*list
, ir_loop
*loop_ctx
)
446 if (list
->length() != 4) {
447 ir_read_error(st
, list
, "expected (if <condition> (<then> ...) "
452 s_expression
*cond_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
453 ir_rvalue
*condition
= read_rvalue(st
, cond_expr
);
454 if (condition
== NULL
) {
455 ir_read_error(st
, NULL
, "when reading condition of (if ...)");
459 s_expression
*then_expr
= (s_expression
*) cond_expr
->next
;
460 s_expression
*else_expr
= (s_expression
*) then_expr
->next
;
462 ir_if
*iff
= new ir_if(condition
);
464 read_instructions(st
, &iff
->then_instructions
, then_expr
, loop_ctx
);
465 read_instructions(st
, &iff
->else_instructions
, else_expr
, loop_ctx
);
475 read_loop(_mesa_glsl_parse_state
*st
, s_list
*list
)
477 if (list
->length() != 6) {
478 ir_read_error(st
, list
, "expected (loop <counter> <from> <to> "
479 "<increment> <body>)");
483 s_expression
*count_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
484 s_expression
*from_expr
= (s_expression
*) count_expr
->next
;
485 s_expression
*to_expr
= (s_expression
*) from_expr
->next
;
486 s_expression
*inc_expr
= (s_expression
*) to_expr
->next
;
487 s_expression
*body_expr
= (s_expression
*) inc_expr
->next
;
489 // FINISHME: actually read the count/from/to fields.
491 ir_loop
*loop
= new ir_loop
;
492 read_instructions(st
, &loop
->body_instructions
, body_expr
, loop
);
502 read_return(_mesa_glsl_parse_state
*st
, s_list
*list
)
504 if (list
->length() != 2) {
505 ir_read_error(st
, list
, "expected (return <rvalue>)");
509 s_expression
*expr
= (s_expression
*) list
->subexpressions
.head
->next
;
511 ir_rvalue
*retval
= read_rvalue(st
, expr
);
512 if (retval
== NULL
) {
513 ir_read_error(st
, NULL
, "when reading return value");
517 return new ir_return(retval
);
522 read_rvalue(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
524 s_list
*list
= SX_AS_LIST(expr
);
525 if (list
== NULL
|| list
->subexpressions
.is_empty())
528 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
530 ir_read_error(st
, expr
, "expected rvalue tag");
534 ir_rvalue
*rvalue
= read_dereference(st
, list
);
535 if (rvalue
!= NULL
|| st
->error
)
537 else if (strcmp(tag
->value(), "swiz") == 0) {
538 rvalue
= read_swizzle(st
, list
);
539 } else if (strcmp(tag
->value(), "assign") == 0) {
540 rvalue
= read_assignment(st
, list
);
541 } else if (strcmp(tag
->value(), "expression") == 0) {
542 rvalue
= read_expression(st
, list
);
543 } else if (strcmp(tag
->value(), "call") == 0) {
544 rvalue
= read_call(st
, list
);
545 } else if (strcmp(tag
->value(), "constant") == 0) {
546 rvalue
= read_constant(st
, list
);
548 rvalue
= read_texture(st
, list
);
549 if (rvalue
== NULL
&& !st
->error
)
550 ir_read_error(st
, expr
, "unrecognized rvalue tag: %s", tag
->value());
556 static ir_assignment
*
557 read_assignment(_mesa_glsl_parse_state
*st
, s_list
*list
)
559 if (list
->length() != 4) {
560 ir_read_error(st
, list
, "expected (assign <condition> <lhs> <rhs>)");
564 s_expression
*cond_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
565 s_expression
*lhs_expr
= (s_expression
*) cond_expr
->next
;
566 s_expression
*rhs_expr
= (s_expression
*) lhs_expr
->next
;
568 // FINISHME: Deal with "true" condition
569 ir_rvalue
*condition
= read_rvalue(st
, cond_expr
);
570 if (condition
== NULL
) {
571 ir_read_error(st
, NULL
, "when reading condition of assignment");
575 ir_rvalue
*lhs
= read_rvalue(st
, lhs_expr
);
577 ir_read_error(st
, NULL
, "when reading left-hand side of assignment");
581 ir_rvalue
*rhs
= read_rvalue(st
, rhs_expr
);
583 ir_read_error(st
, NULL
, "when reading right-hand side of assignment");
587 return new ir_assignment(lhs
, rhs
, condition
);
591 read_call(_mesa_glsl_parse_state
*st
, s_list
*list
)
593 if (list
->length() != 3) {
594 ir_read_error(st
, list
, "expected (call <name> (<param> ...))");
598 s_symbol
*name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
599 s_list
*params
= SX_AS_LIST(name
->next
);
600 if (name
== NULL
|| params
== NULL
) {
601 ir_read_error(st
, list
, "expected (call <name> (<param> ...))");
605 exec_list parameters
;
607 foreach_iter(exec_list_iterator
, it
, params
->subexpressions
) {
608 s_expression
*expr
= (s_expression
*) it
.get();
609 ir_rvalue
*param
= read_rvalue(st
, expr
);
611 ir_read_error(st
, list
, "when reading parameter to function call");
614 parameters
.push_tail(param
);
617 ir_function
*f
= st
->symbols
->get_function(name
->value());
619 ir_read_error(st
, list
, "found call to undefined function %s",
624 const ir_function_signature
*callee
= f
->matching_signature(¶meters
);
625 if (callee
== NULL
) {
626 ir_read_error(st
, list
, "couldn't find matching signature for function "
627 "%s", name
->value());
631 return new ir_call(callee
, ¶meters
);
634 static ir_expression
*
635 read_expression(_mesa_glsl_parse_state
*st
, s_list
*list
)
637 const unsigned list_length
= list
->length();
638 if (list_length
< 4) {
639 ir_read_error(st
, list
, "expected (expression <type> <operator> "
640 "<operand> [<operand>])");
644 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
645 const glsl_type
*type
= read_type(st
, type_expr
);
649 /* Read the operator */
650 s_symbol
*op_sym
= SX_AS_SYMBOL(type_expr
->next
);
651 if (op_sym
== NULL
) {
652 ir_read_error(st
, list
, "expected operator, found non-symbol");
656 ir_expression_operation op
= ir_expression::get_operator(op_sym
->value());
657 if (op
== (ir_expression_operation
) -1) {
658 ir_read_error(st
, list
, "invalid operator: %s", op_sym
->value());
662 /* Now that we know the operator, check for the right number of operands */
663 if (ir_expression::get_num_operands(op
) == 2) {
664 if (list_length
!= 5) {
665 ir_read_error(st
, list
, "expected (expression <type> %s <operand> "
666 " <operand>)", op_sym
->value());
670 if (list_length
!= 4) {
671 ir_read_error(st
, list
, "expected (expression <type> %s <operand>)",
677 s_expression
*exp1
= (s_expression
*) (op_sym
->next
);
678 ir_rvalue
*arg1
= read_rvalue(st
, exp1
);
680 ir_read_error(st
, NULL
, "when reading first operand of %s",
685 ir_rvalue
*arg2
= NULL
;
686 if (ir_expression::get_num_operands(op
) == 2) {
687 s_expression
*exp2
= (s_expression
*) (exp1
->next
);
688 arg2
= read_rvalue(st
, exp2
);
690 ir_read_error(st
, NULL
, "when reading second operand of %s",
696 return new ir_expression(op
, type
, arg1
, arg2
);
700 read_swizzle(_mesa_glsl_parse_state
*st
, s_list
*list
)
702 if (list
->length() != 3) {
703 ir_read_error(st
, list
, "expected (swiz <swizzle> <rvalue>)");
707 s_symbol
*swiz
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
709 ir_read_error(st
, list
, "expected a valid swizzle; found non-symbol");
713 if (strlen(swiz
->value()) > 4) {
714 ir_read_error(st
, list
, "expected a valid swizzle; found %s",
719 s_expression
*sub
= (s_expression
*) swiz
->next
;
721 ir_read_error(st
, list
, "expected rvalue: (swizzle %s <rvalue>)",
726 ir_rvalue
*rvalue
= read_rvalue(st
, sub
);
730 ir_swizzle
*ir
= ir_swizzle::create(rvalue
, swiz
->value(),
731 rvalue
->type
->vector_elements
);
733 ir_read_error(st
, list
, "invalid swizzle");
739 read_constant(_mesa_glsl_parse_state
*st
, s_list
*list
)
741 if (list
->length() != 3) {
742 ir_read_error(st
, list
, "expected (constant <type> (<num> ... <num>))");
746 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
747 const glsl_type
*type
= read_type(st
, type_expr
);
751 s_list
*values
= SX_AS_LIST(type_expr
->next
);
752 if (values
== NULL
) {
753 ir_read_error(st
, list
, "expected (constant <type> (<num> ... <num>))");
757 const glsl_type
*const base_type
= type
->get_base_type();
759 ir_constant_data data
;
761 // Read in list of values (at most 16).
763 foreach_iter(exec_list_iterator
, it
, values
->subexpressions
) {
765 ir_read_error(st
, values
, "expected at most 16 numbers");
769 s_expression
*expr
= (s_expression
*) it
.get();
771 if (base_type
->base_type
== GLSL_TYPE_FLOAT
) {
772 s_number
*value
= SX_AS_NUMBER(expr
);
774 ir_read_error(st
, values
, "expected numbers");
777 data
.f
[k
] = value
->fvalue();
779 s_int
*value
= SX_AS_INT(expr
);
781 ir_read_error(st
, values
, "expected integers");
785 switch (base_type
->base_type
) {
786 case GLSL_TYPE_UINT
: {
787 data
.u
[k
] = value
->value();
790 case GLSL_TYPE_INT
: {
791 data
.i
[k
] = value
->value();
794 case GLSL_TYPE_BOOL
: {
795 data
.b
[k
] = value
->value();
799 ir_read_error(st
, values
, "unsupported constant type");
806 return new ir_constant(type
, &data
);
809 static ir_dereference
*
810 read_dereference(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
812 s_list
*list
= SX_AS_LIST(expr
);
813 if (list
== NULL
|| list
->subexpressions
.is_empty())
816 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.head
);
819 if (strcmp(tag
->value(), "var_ref") == 0)
820 return read_var_ref(st
, list
);
821 if (strcmp(tag
->value(), "array_ref") == 0)
822 return read_array_ref(st
, list
);
823 if (strcmp(tag
->value(), "record_ref") == 0)
824 return read_record_ref(st
, list
);
828 static ir_dereference
*
829 read_var_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
831 if (list
->length() != 2) {
832 ir_read_error(st
, list
, "expected (var_ref <variable name>)");
835 s_symbol
*var_name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
836 if (var_name
== NULL
) {
837 ir_read_error(st
, list
, "expected (var_ref <variable name>)");
841 ir_variable
*var
= st
->symbols
->get_variable(var_name
->value());
843 ir_read_error(st
, list
, "undeclared variable: %s", var_name
->value());
847 return new ir_dereference_variable(var
);
850 static ir_dereference
*
851 read_array_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
853 if (list
->length() != 3) {
854 ir_read_error(st
, list
, "expected (array_ref <rvalue> <index>)");
858 s_expression
*subj_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
859 ir_rvalue
*subject
= read_rvalue(st
, subj_expr
);
860 if (subject
== NULL
) {
861 ir_read_error(st
, NULL
, "when reading the subject of an array_ref");
865 s_expression
*idx_expr
= (s_expression
*) subj_expr
->next
;
866 ir_rvalue
*idx
= read_rvalue(st
, idx_expr
);
867 return new ir_dereference_array(subject
, idx
);
870 static ir_dereference
*
871 read_record_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
873 if (list
->length() != 3) {
874 ir_read_error(st
, list
, "expected (record_ref <rvalue> <field>)");
878 s_expression
*subj_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
879 ir_rvalue
*subject
= read_rvalue(st
, subj_expr
);
880 if (subject
== NULL
) {
881 ir_read_error(st
, NULL
, "when reading the subject of a record_ref");
885 s_symbol
*field
= SX_AS_SYMBOL(subj_expr
->next
);
887 ir_read_error(st
, list
, "expected (record_ref ... <field name>)");
890 return new ir_dereference_record(subject
, field
->value());
894 valid_texture_list_length(ir_texture_opcode op
, s_list
*list
)
896 unsigned required_length
= 7;
899 else if (op
== ir_tex
)
902 return list
->length() == required_length
;
906 read_texture(_mesa_glsl_parse_state
*st
, s_list
*list
)
908 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.head
);
911 ir_texture_opcode op
= ir_texture::get_opcode(tag
->value());
912 if (op
== (ir_texture_opcode
) -1)
915 if (!valid_texture_list_length(op
, list
)) {
916 ir_read_error(st
, NULL
, "invalid list size in (%s ...)", tag
->value());
920 ir_texture
*tex
= new ir_texture(op
);
922 // Read sampler (must be a deref)
923 s_expression
*sampler_expr
= (s_expression
*) tag
->next
;
924 ir_dereference
*sampler
= read_dereference(st
, sampler_expr
);
925 if (sampler
== NULL
) {
926 ir_read_error(st
, NULL
, "when reading sampler in (%s ...)", tag
->value());
929 tex
->set_sampler(sampler
);
931 // Read coordinate (any rvalue)
932 s_expression
*coordinate_expr
= (s_expression
*) sampler_expr
->next
;
933 tex
->coordinate
= read_rvalue(st
, coordinate_expr
);
934 if (tex
->coordinate
== NULL
) {
935 ir_read_error(st
, NULL
, "when reading coordinate in (%s ...)",
940 // Read texel offset, i.e. (0 0 0)
941 s_list
*offset_list
= SX_AS_LIST(coordinate_expr
->next
);
942 if (offset_list
== NULL
|| offset_list
->length() != 3) {
943 ir_read_error(st
, offset_list
, "expected (<int> <int> <int>)");
946 s_int
*offset_x
= SX_AS_INT(offset_list
->subexpressions
.head
);
947 s_int
*offset_y
= SX_AS_INT(offset_x
->next
);
948 s_int
*offset_z
= SX_AS_INT(offset_y
->next
);
949 if (offset_x
== NULL
|| offset_y
== NULL
|| offset_z
== NULL
) {
950 ir_read_error(st
, offset_list
, "expected (<int> <int> <int>)");
953 tex
->offsets
[0] = offset_x
->value();
954 tex
->offsets
[1] = offset_y
->value();
955 tex
->offsets
[2] = offset_z
->value();
958 s_expression
*lod_expr
= (s_expression
*) offset_list
->next
;
959 tex
->lod_info
.lod
= read_rvalue(st
, lod_expr
);
960 if (tex
->lod_info
.lod
== NULL
) {
961 ir_read_error(st
, NULL
, "when reading LOD in (txf ...)");
965 s_expression
*proj_expr
= (s_expression
*) offset_list
->next
;
966 s_int
*proj_as_int
= SX_AS_INT(proj_expr
);
967 if (proj_as_int
&& proj_as_int
->value() == 1) {
968 tex
->projector
= NULL
;
970 tex
->projector
= read_rvalue(st
, proj_expr
);
971 if (tex
->projector
== NULL
) {
972 ir_read_error(st
, NULL
, "when reading projective divide in (%s ..)",
978 s_list
*shadow_list
= SX_AS_LIST(proj_expr
->next
);
979 if (shadow_list
== NULL
) {
980 ir_read_error(st
, NULL
, "shadow comparitor must be a list");
983 if (shadow_list
->subexpressions
.is_empty()) {
984 tex
->shadow_comparitor
= NULL
;
986 tex
->shadow_comparitor
= read_rvalue(st
, shadow_list
);
987 if (tex
->shadow_comparitor
== NULL
) {
988 ir_read_error(st
, NULL
, "when reading shadow comparitor in (%s ..)",
993 s_expression
*lod_expr
= (s_expression
*) shadow_list
->next
;
997 tex
->lod_info
.bias
= read_rvalue(st
, lod_expr
);
998 if (tex
->lod_info
.bias
== NULL
) {
999 ir_read_error(st
, NULL
, "when reading LOD bias in (txb ...)");
1004 tex
->lod_info
.lod
= read_rvalue(st
, lod_expr
);
1005 if (tex
->lod_info
.lod
== NULL
) {
1006 ir_read_error(st
, NULL
, "when reading LOD in (txl ...)");
1011 s_list
*lod_list
= SX_AS_LIST(lod_expr
);
1012 if (lod_list
->length() != 2) {
1013 ir_read_error(st
, lod_expr
, "expected (dPdx dPdy) in (txd ...)");
1016 s_expression
*dx_expr
= (s_expression
*) lod_list
->subexpressions
.head
;
1017 s_expression
*dy_expr
= (s_expression
*) dx_expr
->next
;
1019 tex
->lod_info
.grad
.dPdx
= read_rvalue(st
, dx_expr
);
1020 if (tex
->lod_info
.grad
.dPdx
== NULL
) {
1021 ir_read_error(st
, NULL
, "when reading dPdx in (txd ...)");
1024 tex
->lod_info
.grad
.dPdy
= read_rvalue(st
, dy_expr
);
1025 if (tex
->lod_info
.grad
.dPdy
== NULL
) {
1026 ir_read_error(st
, NULL
, "when reading dPdy in (txd ...)");
1032 // tex doesn't have any extra parameters and txf was handled earlier.