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 const static bool debug
= false;
37 static void ir_read_error(_mesa_glsl_parse_state
*, s_expression
*,
38 const char *fmt
, ...);
39 static const glsl_type
*read_type(_mesa_glsl_parse_state
*, s_expression
*);
41 static void scan_for_prototypes(_mesa_glsl_parse_state
*, exec_list
*,
43 static ir_function
*read_function(_mesa_glsl_parse_state
*, s_list
*,
45 static void read_function_sig(_mesa_glsl_parse_state
*, ir_function
*,
46 s_list
*, bool skip_body
);
48 static void read_instructions(_mesa_glsl_parse_state
*, exec_list
*,
49 s_expression
*, ir_loop
*);
50 static ir_instruction
*read_instruction(_mesa_glsl_parse_state
*,
51 s_expression
*, ir_loop
*);
52 static ir_variable
*read_declaration(_mesa_glsl_parse_state
*, s_list
*);
53 static ir_if
*read_if(_mesa_glsl_parse_state
*, s_list
*, ir_loop
*);
54 static ir_loop
*read_loop(_mesa_glsl_parse_state
*st
, s_list
*list
);
55 static ir_return
*read_return(_mesa_glsl_parse_state
*, s_list
*);
57 static ir_rvalue
*read_rvalue(_mesa_glsl_parse_state
*, s_expression
*);
58 static ir_assignment
*read_assignment(_mesa_glsl_parse_state
*, s_list
*);
59 static ir_expression
*read_expression(_mesa_glsl_parse_state
*, s_list
*);
60 static ir_call
*read_call(_mesa_glsl_parse_state
*, s_list
*);
61 static ir_swizzle
*read_swizzle(_mesa_glsl_parse_state
*, s_list
*);
62 static ir_constant
*read_constant(_mesa_glsl_parse_state
*, s_list
*);
63 static ir_texture
*read_texture(_mesa_glsl_parse_state
*, s_list
*);
65 static ir_dereference
*read_dereference(_mesa_glsl_parse_state
*,
67 static ir_dereference
*read_var_ref(_mesa_glsl_parse_state
*, s_list
*);
68 static ir_dereference
*read_array_ref(_mesa_glsl_parse_state
*, s_list
*);
69 static ir_dereference
*read_record_ref(_mesa_glsl_parse_state
*, s_list
*);
72 _mesa_glsl_read_ir(_mesa_glsl_parse_state
*state
, exec_list
*instructions
,
73 const char *src
, bool scan_for_protos
)
75 s_expression
*expr
= s_expression::read_expression(state
, src
);
77 ir_read_error(state
, NULL
, "couldn't parse S-Expression.");
81 if (scan_for_protos
) {
82 scan_for_prototypes(state
, instructions
, expr
);
87 read_instructions(state
, instructions
, expr
, NULL
);
91 validate_ir_tree(instructions
);
95 ir_read_error(_mesa_glsl_parse_state
*state
, s_expression
*expr
,
102 if (state
->current_function
!= NULL
)
103 state
->info_log
= talloc_asprintf_append(state
->info_log
,
105 state
->current_function
->function_name());
106 state
->info_log
= talloc_strdup_append(state
->info_log
, "error: ");
109 state
->info_log
= talloc_vasprintf_append(state
->info_log
, fmt
, ap
);
111 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n");
114 state
->info_log
= talloc_strdup_append(state
->info_log
,
115 "...in this context:\n ");
117 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n\n");
121 static const glsl_type
*
122 read_type(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
124 s_list
*list
= SX_AS_LIST(expr
);
126 s_symbol
*type_sym
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
127 if (type_sym
== NULL
) {
128 ir_read_error(st
, expr
, "expected type (array ...) or (struct ...)");
131 if (strcmp(type_sym
->value(), "array") == 0) {
132 if (list
->length() != 3) {
133 ir_read_error(st
, expr
, "expected type (array <type> <int>)");
138 s_expression
*base_expr
= (s_expression
*) type_sym
->next
;
139 const glsl_type
*base_type
= read_type(st
, base_expr
);
140 if (base_type
== NULL
) {
141 ir_read_error(st
, NULL
, "when reading base type of array");
146 s_int
*size
= SX_AS_INT(base_expr
->next
);
148 ir_read_error(st
, expr
, "found non-integer array size");
152 return glsl_type::get_array_instance(base_type
, size
->value());
153 } else if (strcmp(type_sym
->value(), "struct") == 0) {
154 assert(false); // FINISHME
156 ir_read_error(st
, expr
, "expected (array ...) or (struct ...); "
157 "found (%s ...)", type_sym
->value());
162 s_symbol
*type_sym
= SX_AS_SYMBOL(expr
);
163 if (type_sym
== NULL
) {
164 ir_read_error(st
, expr
, "expected <type> (symbol or list)");
168 const glsl_type
*type
= st
->symbols
->get_type(type_sym
->value());
170 ir_read_error(st
, expr
, "invalid type: %s", type_sym
->value());
177 scan_for_prototypes(_mesa_glsl_parse_state
*st
, exec_list
*instructions
,
180 s_list
*list
= SX_AS_LIST(expr
);
182 ir_read_error(st
, expr
, "Expected (<instruction> ...); found an atom.");
186 foreach_iter(exec_list_iterator
, it
, list
->subexpressions
) {
187 s_list
*sub
= SX_AS_LIST(it
.get());
189 continue; // not a (function ...); ignore it.
191 s_symbol
*tag
= SX_AS_SYMBOL(sub
->subexpressions
.get_head());
192 if (tag
== NULL
|| strcmp(tag
->value(), "function") != 0)
193 continue; // not a (function ...); ignore it.
195 ir_function
*f
= read_function(st
, sub
, true);
198 instructions
->push_tail(f
);
203 read_function(_mesa_glsl_parse_state
*st
, s_list
*list
, bool skip_body
)
207 if (list
->length() < 3) {
208 ir_read_error(st
, list
, "Expected (function <name> (signature ...) ...)");
212 s_symbol
*name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
214 ir_read_error(st
, list
, "Expected (function <name> ...)");
218 ir_function
*f
= st
->symbols
->get_function(name
->value());
220 f
= new(ctx
) ir_function(name
->value());
221 f
->is_builtin
= true;
222 added
= st
->symbols
->add_function(f
->name
, f
);
226 exec_list_iterator it
= list
->subexpressions
.iterator();
227 it
.next(); // skip "function" tag
228 it
.next(); // skip function name
229 for (/* nothing */; it
.has_next(); it
.next()) {
230 s_list
*siglist
= SX_AS_LIST(it
.get());
231 if (siglist
== NULL
) {
232 ir_read_error(st
, list
, "Expected (function (signature ...) ...)");
236 s_symbol
*tag
= SX_AS_SYMBOL(siglist
->subexpressions
.get_head());
237 if (tag
== NULL
|| strcmp(tag
->value(), "signature") != 0) {
238 ir_read_error(st
, siglist
, "Expected (signature ...)");
242 read_function_sig(st
, f
, siglist
, skip_body
);
244 return added
? f
: NULL
;
248 read_function_sig(_mesa_glsl_parse_state
*st
, ir_function
*f
, s_list
*list
,
252 if (list
->length() != 4) {
253 ir_read_error(st
, list
, "Expected (signature <type> (parameters ...) "
254 "(<instruction> ...))");
258 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
259 const glsl_type
*return_type
= read_type(st
, type_expr
);
260 if (return_type
== NULL
)
263 s_list
*paramlist
= SX_AS_LIST(type_expr
->next
);
264 s_list
*body_list
= SX_AS_LIST(paramlist
->next
);
265 if (paramlist
== NULL
|| body_list
== NULL
) {
266 ir_read_error(st
, list
, "Expected (signature <type> (parameters ...) "
267 "(<instruction> ...))");
270 s_symbol
*paramtag
= SX_AS_SYMBOL(paramlist
->subexpressions
.get_head());
271 if (paramtag
== NULL
|| strcmp(paramtag
->value(), "parameters") != 0) {
272 ir_read_error(st
, paramlist
, "Expected (parameters ...)");
276 // Read the parameters list into a temporary place.
277 exec_list hir_parameters
;
278 st
->symbols
->push_scope();
280 exec_list_iterator it
= paramlist
->subexpressions
.iterator();
281 for (it
.next() /* skip "parameters" */; it
.has_next(); it
.next()) {
282 s_list
*decl
= SX_AS_LIST(it
.get());
283 ir_variable
*var
= read_declaration(st
, decl
);
287 hir_parameters
.push_tail(var
);
290 ir_function_signature
*sig
= f
->exact_matching_signature(&hir_parameters
);
291 if (sig
== NULL
&& skip_body
) {
292 /* If scanning for prototypes, generate a new signature. */
293 sig
= new(ctx
) ir_function_signature(return_type
);
294 f
->add_signature(sig
);
295 } else if (sig
!= NULL
) {
296 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
297 if (badvar
!= NULL
) {
298 ir_read_error(st
, list
, "function `%s' parameter `%s' qualifiers "
299 "don't match prototype", f
->name
, badvar
);
303 if (sig
->return_type
!= return_type
) {
304 ir_read_error(st
, list
, "function `%s' return type doesn't "
305 "match prototype", f
->name
);
309 /* No prototype for this body exists - skip it. */
310 st
->symbols
->pop_scope();
315 sig
->replace_parameters(&hir_parameters
);
317 if (!skip_body
&& !body_list
->subexpressions
.is_empty()) {
318 if (sig
->is_defined
) {
319 ir_read_error(st
, list
, "function %s redefined", f
->name
);
322 st
->current_function
= sig
;
323 read_instructions(st
, &sig
->body
, body_list
, NULL
);
324 st
->current_function
= NULL
;
325 sig
->is_defined
= true;
328 st
->symbols
->pop_scope();
332 read_instructions(_mesa_glsl_parse_state
*st
, exec_list
*instructions
,
333 s_expression
*expr
, ir_loop
*loop_ctx
)
335 // Read in a list of instructions
336 s_list
*list
= SX_AS_LIST(expr
);
338 ir_read_error(st
, expr
, "Expected (<instruction> ...); found an atom.");
342 foreach_iter(exec_list_iterator
, it
, list
->subexpressions
) {
343 s_expression
*sub
= (s_expression
*) it
.get();
344 ir_instruction
*ir
= read_instruction(st
, sub
, loop_ctx
);
346 /* Global variable declarations should be moved to the top, before
347 * any functions that might use them. Functions are added to the
348 * instruction stream when scanning for prototypes, so without this
349 * hack, they always appear before variable declarations.
351 if (st
->current_function
== NULL
&& ir
->as_variable() != NULL
)
352 instructions
->push_head(ir
);
354 instructions
->push_tail(ir
);
360 static ir_instruction
*
361 read_instruction(_mesa_glsl_parse_state
*st
, s_expression
*expr
,
365 s_symbol
*symbol
= SX_AS_SYMBOL(expr
);
366 if (symbol
!= NULL
) {
367 if (strcmp(symbol
->value(), "break") == 0 && loop_ctx
!= NULL
)
368 return new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
369 if (strcmp(symbol
->value(), "continue") == 0 && loop_ctx
!= NULL
)
370 return new(ctx
) ir_loop_jump(ir_loop_jump::jump_continue
);
373 s_list
*list
= SX_AS_LIST(expr
);
374 if (list
== NULL
|| list
->subexpressions
.is_empty()) {
375 ir_read_error(st
, expr
, "Invalid instruction.\n");
379 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
381 ir_read_error(st
, expr
, "expected instruction tag");
385 ir_instruction
*inst
= NULL
;
386 if (strcmp(tag
->value(), "declare") == 0) {
387 inst
= read_declaration(st
, list
);
388 } else if (strcmp(tag
->value(), "assign") == 0) {
389 inst
= read_assignment(st
, list
);
390 } else if (strcmp(tag
->value(), "if") == 0) {
391 inst
= read_if(st
, list
, loop_ctx
);
392 } else if (strcmp(tag
->value(), "loop") == 0) {
393 inst
= read_loop(st
, list
);
394 } else if (strcmp(tag
->value(), "return") == 0) {
395 inst
= read_return(st
, list
);
396 } else if (strcmp(tag
->value(), "function") == 0) {
397 inst
= read_function(st
, list
, false);
399 inst
= read_rvalue(st
, list
);
401 ir_read_error(st
, NULL
, "when reading instruction");
408 read_declaration(_mesa_glsl_parse_state
*st
, s_list
*list
)
411 if (list
->length() != 4) {
412 ir_read_error(st
, list
, "expected (declare (<qualifiers>) <type> "
417 s_list
*quals
= SX_AS_LIST(list
->subexpressions
.head
->next
);
419 ir_read_error(st
, list
, "expected a list of variable qualifiers");
423 s_expression
*type_expr
= (s_expression
*) quals
->next
;
424 const glsl_type
*type
= read_type(st
, type_expr
);
428 s_symbol
*var_name
= SX_AS_SYMBOL(type_expr
->next
);
429 if (var_name
== NULL
) {
430 ir_read_error(st
, list
, "expected variable name, found non-symbol");
434 ir_variable
*var
= new(ctx
) ir_variable(type
, var_name
->value(),
437 foreach_iter(exec_list_iterator
, it
, quals
->subexpressions
) {
438 s_symbol
*qualifier
= SX_AS_SYMBOL(it
.get());
439 if (qualifier
== NULL
) {
440 ir_read_error(st
, list
, "qualifier list must contain only symbols");
445 // FINISHME: Check for duplicate/conflicting qualifiers.
446 if (strcmp(qualifier
->value(), "centroid") == 0) {
448 } else if (strcmp(qualifier
->value(), "invariant") == 0) {
450 } else if (strcmp(qualifier
->value(), "uniform") == 0) {
451 var
->mode
= ir_var_uniform
;
452 } else if (strcmp(qualifier
->value(), "auto") == 0) {
453 var
->mode
= ir_var_auto
;
454 } else if (strcmp(qualifier
->value(), "in") == 0) {
455 var
->mode
= ir_var_in
;
456 } else if (strcmp(qualifier
->value(), "out") == 0) {
457 var
->mode
= ir_var_out
;
458 } else if (strcmp(qualifier
->value(), "inout") == 0) {
459 var
->mode
= ir_var_inout
;
460 } else if (strcmp(qualifier
->value(), "smooth") == 0) {
461 var
->interpolation
= ir_var_smooth
;
462 } else if (strcmp(qualifier
->value(), "flat") == 0) {
463 var
->interpolation
= ir_var_flat
;
464 } else if (strcmp(qualifier
->value(), "noperspective") == 0) {
465 var
->interpolation
= ir_var_noperspective
;
467 ir_read_error(st
, list
, "unknown qualifier: %s", qualifier
->value());
473 // Add the variable to the symbol table
474 st
->symbols
->add_variable(var
->name
, var
);
481 read_if(_mesa_glsl_parse_state
*st
, s_list
*list
, ir_loop
*loop_ctx
)
484 if (list
->length() != 4) {
485 ir_read_error(st
, list
, "expected (if <condition> (<then> ...) "
490 s_expression
*cond_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
491 ir_rvalue
*condition
= read_rvalue(st
, cond_expr
);
492 if (condition
== NULL
) {
493 ir_read_error(st
, NULL
, "when reading condition of (if ...)");
497 s_expression
*then_expr
= (s_expression
*) cond_expr
->next
;
498 s_expression
*else_expr
= (s_expression
*) then_expr
->next
;
500 ir_if
*iff
= new(ctx
) ir_if(condition
);
502 read_instructions(st
, &iff
->then_instructions
, then_expr
, loop_ctx
);
503 read_instructions(st
, &iff
->else_instructions
, else_expr
, loop_ctx
);
513 read_loop(_mesa_glsl_parse_state
*st
, s_list
*list
)
516 if (list
->length() != 6) {
517 ir_read_error(st
, list
, "expected (loop <counter> <from> <to> "
518 "<increment> <body>)");
522 s_expression
*count_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
523 s_expression
*from_expr
= (s_expression
*) count_expr
->next
;
524 s_expression
*to_expr
= (s_expression
*) from_expr
->next
;
525 s_expression
*inc_expr
= (s_expression
*) to_expr
->next
;
526 s_expression
*body_expr
= (s_expression
*) inc_expr
->next
;
528 // FINISHME: actually read the count/from/to fields.
530 ir_loop
*loop
= new(ctx
) ir_loop
;
531 read_instructions(st
, &loop
->body_instructions
, body_expr
, loop
);
541 read_return(_mesa_glsl_parse_state
*st
, s_list
*list
)
544 if (list
->length() != 2) {
545 ir_read_error(st
, list
, "expected (return <rvalue>)");
549 s_expression
*expr
= (s_expression
*) list
->subexpressions
.head
->next
;
551 ir_rvalue
*retval
= read_rvalue(st
, expr
);
552 if (retval
== NULL
) {
553 ir_read_error(st
, NULL
, "when reading return value");
557 return new(ctx
) ir_return(retval
);
562 read_rvalue(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
564 s_list
*list
= SX_AS_LIST(expr
);
565 if (list
== NULL
|| list
->subexpressions
.is_empty())
568 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
570 ir_read_error(st
, expr
, "expected rvalue tag");
574 ir_rvalue
*rvalue
= read_dereference(st
, list
);
575 if (rvalue
!= NULL
|| st
->error
)
577 else if (strcmp(tag
->value(), "swiz") == 0) {
578 rvalue
= read_swizzle(st
, list
);
579 } else if (strcmp(tag
->value(), "expression") == 0) {
580 rvalue
= read_expression(st
, list
);
581 } else if (strcmp(tag
->value(), "call") == 0) {
582 rvalue
= read_call(st
, list
);
583 } else if (strcmp(tag
->value(), "constant") == 0) {
584 rvalue
= read_constant(st
, list
);
586 rvalue
= read_texture(st
, list
);
587 if (rvalue
== NULL
&& !st
->error
)
588 ir_read_error(st
, expr
, "unrecognized rvalue tag: %s", tag
->value());
594 static ir_assignment
*
595 read_assignment(_mesa_glsl_parse_state
*st
, s_list
*list
)
598 if (list
->length() != 5) {
599 ir_read_error(st
, list
, "expected (assign <condition> (<write mask>) "
604 s_expression
*cond_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
605 s_list
*mask_list
= SX_AS_LIST(cond_expr
->next
);
606 s_expression
*lhs_expr
= (s_expression
*) cond_expr
->next
->next
;
607 s_expression
*rhs_expr
= (s_expression
*) lhs_expr
->next
;
609 ir_rvalue
*condition
= read_rvalue(st
, cond_expr
);
610 if (condition
== NULL
) {
611 ir_read_error(st
, NULL
, "when reading condition of assignment");
615 if (mask_list
== NULL
|| mask_list
->length() > 1) {
616 ir_read_error(st
, mask_list
, "expected () or (<write mask>)");
621 if (mask_list
->length() == 1) {
622 s_symbol
*mask_symbol
= SX_AS_SYMBOL(mask_list
->subexpressions
.head
);
623 if (mask_symbol
== NULL
) {
624 ir_read_error(st
, list
, "expected a write mask; found non-symbol");
628 const char *mask_str
= mask_symbol
->value();
629 unsigned mask_length
= strlen(mask_str
);
630 if (mask_length
> 4) {
631 ir_read_error(st
, list
, "invalid write mask: %s", mask_str
);
635 const unsigned idx_map
[] = { 3, 0, 1, 2 }; /* w=bit 3, x=0, y=1, z=2 */
637 for (unsigned i
= 0; i
< mask_length
; i
++) {
638 if (mask_str
[i
] < 'w' || mask_str
[i
] > 'z') {
639 ir_read_error(st
, list
, "write mask contains invalid character: %c",
643 mask
|= 1 << idx_map
[mask_str
[i
] - 'w'];
647 ir_dereference
*lhs
= read_dereference(st
, lhs_expr
);
649 ir_read_error(st
, NULL
, "when reading left-hand side of assignment");
653 ir_rvalue
*rhs
= read_rvalue(st
, rhs_expr
);
655 ir_read_error(st
, NULL
, "when reading right-hand side of assignment");
659 if (mask
== 0 && (lhs
->type
->is_vector() || lhs
->type
->is_scalar())) {
660 ir_read_error(st
, list
, "non-zero write mask required.");
664 return new(ctx
) ir_assignment(lhs
, rhs
, condition
, mask
);
668 read_call(_mesa_glsl_parse_state
*st
, s_list
*list
)
671 if (list
->length() != 3) {
672 ir_read_error(st
, list
, "expected (call <name> (<param> ...))");
676 s_symbol
*name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
677 s_list
*params
= SX_AS_LIST(name
->next
);
678 if (name
== NULL
|| params
== NULL
) {
679 ir_read_error(st
, list
, "expected (call <name> (<param> ...))");
683 exec_list parameters
;
685 foreach_iter(exec_list_iterator
, it
, params
->subexpressions
) {
686 s_expression
*expr
= (s_expression
*) it
.get();
687 ir_rvalue
*param
= read_rvalue(st
, expr
);
689 ir_read_error(st
, list
, "when reading parameter to function call");
692 parameters
.push_tail(param
);
695 ir_function
*f
= st
->symbols
->get_function(name
->value());
697 ir_read_error(st
, list
, "found call to undefined function %s",
702 ir_function_signature
*callee
= f
->matching_signature(¶meters
);
703 if (callee
== NULL
) {
704 ir_read_error(st
, list
, "couldn't find matching signature for function "
705 "%s", name
->value());
709 return new(ctx
) ir_call(callee
, ¶meters
);
712 static ir_expression
*
713 read_expression(_mesa_glsl_parse_state
*st
, s_list
*list
)
716 const unsigned list_length
= list
->length();
717 if (list_length
< 4) {
718 ir_read_error(st
, list
, "expected (expression <type> <operator> "
719 "<operand> [<operand>])");
723 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
724 const glsl_type
*type
= read_type(st
, type_expr
);
728 /* Read the operator */
729 s_symbol
*op_sym
= SX_AS_SYMBOL(type_expr
->next
);
730 if (op_sym
== NULL
) {
731 ir_read_error(st
, list
, "expected operator, found non-symbol");
735 ir_expression_operation op
= ir_expression::get_operator(op_sym
->value());
736 if (op
== (ir_expression_operation
) -1) {
737 ir_read_error(st
, list
, "invalid operator: %s", op_sym
->value());
741 /* Now that we know the operator, check for the right number of operands */
742 if (ir_expression::get_num_operands(op
) == 2) {
743 if (list_length
!= 5) {
744 ir_read_error(st
, list
, "expected (expression <type> %s <operand> "
745 " <operand>)", op_sym
->value());
749 if (list_length
!= 4) {
750 ir_read_error(st
, list
, "expected (expression <type> %s <operand>)",
756 s_expression
*exp1
= (s_expression
*) (op_sym
->next
);
757 ir_rvalue
*arg1
= read_rvalue(st
, exp1
);
759 ir_read_error(st
, NULL
, "when reading first operand of %s",
764 ir_rvalue
*arg2
= NULL
;
765 if (ir_expression::get_num_operands(op
) == 2) {
766 s_expression
*exp2
= (s_expression
*) (exp1
->next
);
767 arg2
= read_rvalue(st
, exp2
);
769 ir_read_error(st
, NULL
, "when reading second operand of %s",
775 return new(ctx
) ir_expression(op
, type
, arg1
, arg2
);
779 read_swizzle(_mesa_glsl_parse_state
*st
, s_list
*list
)
781 if (list
->length() != 3) {
782 ir_read_error(st
, list
, "expected (swiz <swizzle> <rvalue>)");
786 s_symbol
*swiz
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
788 ir_read_error(st
, list
, "expected a valid swizzle; found non-symbol");
792 if (strlen(swiz
->value()) > 4) {
793 ir_read_error(st
, list
, "expected a valid swizzle; found %s",
798 s_expression
*sub
= (s_expression
*) swiz
->next
;
800 ir_read_error(st
, list
, "expected rvalue: (swizzle %s <rvalue>)",
805 ir_rvalue
*rvalue
= read_rvalue(st
, sub
);
809 ir_swizzle
*ir
= ir_swizzle::create(rvalue
, swiz
->value(),
810 rvalue
->type
->vector_elements
);
812 ir_read_error(st
, list
, "invalid swizzle");
818 read_constant(_mesa_glsl_parse_state
*st
, s_list
*list
)
821 if (list
->length() != 3) {
822 ir_read_error(st
, list
, "expected (constant <type> (...))");
826 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
827 const glsl_type
*type
= read_type(st
, type_expr
);
831 s_list
*values
= SX_AS_LIST(type_expr
->next
);
832 if (values
== NULL
) {
833 ir_read_error(st
, list
, "expected (constant <type> (...))");
837 if (type
->is_array()) {
838 const unsigned elements_supplied
= values
->length();
839 if (elements_supplied
!= type
->length
) {
840 ir_read_error(st
, values
, "expected exactly %u array elements, "
841 "given %u", type
->length
, elements_supplied
);
846 foreach_iter(exec_list_iterator
, it
, values
->subexpressions
) {
847 s_expression
*expr
= (s_expression
*) it
.get();
848 s_list
*elt
= SX_AS_LIST(expr
);
850 ir_read_error(st
, expr
, "expected (constant ...) array element");
854 ir_constant
*ir_elt
= read_constant(st
, elt
);
857 elements
.push_tail(ir_elt
);
859 return new(ctx
) ir_constant(type
, &elements
);
862 const glsl_type
*const base_type
= type
->get_base_type();
864 ir_constant_data data
= { { 0 } };
866 // Read in list of values (at most 16).
868 foreach_iter(exec_list_iterator
, it
, values
->subexpressions
) {
870 ir_read_error(st
, values
, "expected at most 16 numbers");
874 s_expression
*expr
= (s_expression
*) it
.get();
876 if (base_type
->base_type
== GLSL_TYPE_FLOAT
) {
877 s_number
*value
= SX_AS_NUMBER(expr
);
879 ir_read_error(st
, values
, "expected numbers");
882 data
.f
[k
] = value
->fvalue();
884 s_int
*value
= SX_AS_INT(expr
);
886 ir_read_error(st
, values
, "expected integers");
890 switch (base_type
->base_type
) {
891 case GLSL_TYPE_UINT
: {
892 data
.u
[k
] = value
->value();
895 case GLSL_TYPE_INT
: {
896 data
.i
[k
] = value
->value();
899 case GLSL_TYPE_BOOL
: {
900 data
.b
[k
] = value
->value();
904 ir_read_error(st
, values
, "unsupported constant type");
911 return new(ctx
) ir_constant(type
, &data
);
914 static ir_dereference
*
915 read_dereference(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
917 s_list
*list
= SX_AS_LIST(expr
);
918 if (list
== NULL
|| list
->subexpressions
.is_empty())
921 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.head
);
924 if (strcmp(tag
->value(), "var_ref") == 0)
925 return read_var_ref(st
, list
);
926 if (strcmp(tag
->value(), "array_ref") == 0)
927 return read_array_ref(st
, list
);
928 if (strcmp(tag
->value(), "record_ref") == 0)
929 return read_record_ref(st
, list
);
933 static ir_dereference
*
934 read_var_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
937 if (list
->length() != 2) {
938 ir_read_error(st
, list
, "expected (var_ref <variable name>)");
941 s_symbol
*var_name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
942 if (var_name
== NULL
) {
943 ir_read_error(st
, list
, "expected (var_ref <variable name>)");
947 ir_variable
*var
= st
->symbols
->get_variable(var_name
->value());
949 ir_read_error(st
, list
, "undeclared variable: %s", var_name
->value());
953 return new(ctx
) ir_dereference_variable(var
);
956 static ir_dereference
*
957 read_array_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
960 if (list
->length() != 3) {
961 ir_read_error(st
, list
, "expected (array_ref <rvalue> <index>)");
965 s_expression
*subj_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
966 ir_rvalue
*subject
= read_rvalue(st
, subj_expr
);
967 if (subject
== NULL
) {
968 ir_read_error(st
, NULL
, "when reading the subject of an array_ref");
972 s_expression
*idx_expr
= (s_expression
*) subj_expr
->next
;
973 ir_rvalue
*idx
= read_rvalue(st
, idx_expr
);
974 return new(ctx
) ir_dereference_array(subject
, idx
);
977 static ir_dereference
*
978 read_record_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
981 if (list
->length() != 3) {
982 ir_read_error(st
, list
, "expected (record_ref <rvalue> <field>)");
986 s_expression
*subj_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
987 ir_rvalue
*subject
= read_rvalue(st
, subj_expr
);
988 if (subject
== NULL
) {
989 ir_read_error(st
, NULL
, "when reading the subject of a record_ref");
993 s_symbol
*field
= SX_AS_SYMBOL(subj_expr
->next
);
995 ir_read_error(st
, list
, "expected (record_ref ... <field name>)");
998 return new(ctx
) ir_dereference_record(subject
, field
->value());
1002 valid_texture_list_length(ir_texture_opcode op
, s_list
*list
)
1004 unsigned required_length
= 7;
1006 required_length
= 5;
1007 else if (op
== ir_tex
)
1008 required_length
= 6;
1010 return list
->length() == required_length
;
1014 read_texture(_mesa_glsl_parse_state
*st
, s_list
*list
)
1017 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.head
);
1018 assert(tag
!= NULL
);
1020 ir_texture_opcode op
= ir_texture::get_opcode(tag
->value());
1021 if (op
== (ir_texture_opcode
) -1)
1024 if (!valid_texture_list_length(op
, list
)) {
1025 ir_read_error(st
, NULL
, "invalid list size in (%s ...)", tag
->value());
1029 ir_texture
*tex
= new(ctx
) ir_texture(op
);
1031 // Read sampler (must be a deref)
1032 s_expression
*sampler_expr
= (s_expression
*) tag
->next
;
1033 ir_dereference
*sampler
= read_dereference(st
, sampler_expr
);
1034 if (sampler
== NULL
) {
1035 ir_read_error(st
, NULL
, "when reading sampler in (%s ...)", tag
->value());
1038 tex
->set_sampler(sampler
);
1040 // Read coordinate (any rvalue)
1041 s_expression
*coordinate_expr
= (s_expression
*) sampler_expr
->next
;
1042 tex
->coordinate
= read_rvalue(st
, coordinate_expr
);
1043 if (tex
->coordinate
== NULL
) {
1044 ir_read_error(st
, NULL
, "when reading coordinate in (%s ...)",
1049 // Read texel offset, i.e. (0 0 0)
1050 s_list
*offset_list
= SX_AS_LIST(coordinate_expr
->next
);
1051 if (offset_list
== NULL
|| offset_list
->length() != 3) {
1052 ir_read_error(st
, offset_list
, "expected (<int> <int> <int>)");
1055 s_int
*offset_x
= SX_AS_INT(offset_list
->subexpressions
.head
);
1056 s_int
*offset_y
= SX_AS_INT(offset_x
->next
);
1057 s_int
*offset_z
= SX_AS_INT(offset_y
->next
);
1058 if (offset_x
== NULL
|| offset_y
== NULL
|| offset_z
== NULL
) {
1059 ir_read_error(st
, offset_list
, "expected (<int> <int> <int>)");
1062 tex
->offsets
[0] = offset_x
->value();
1063 tex
->offsets
[1] = offset_y
->value();
1064 tex
->offsets
[2] = offset_z
->value();
1067 s_expression
*lod_expr
= (s_expression
*) offset_list
->next
;
1068 tex
->lod_info
.lod
= read_rvalue(st
, lod_expr
);
1069 if (tex
->lod_info
.lod
== NULL
) {
1070 ir_read_error(st
, NULL
, "when reading LOD in (txf ...)");
1074 s_expression
*proj_expr
= (s_expression
*) offset_list
->next
;
1075 s_int
*proj_as_int
= SX_AS_INT(proj_expr
);
1076 if (proj_as_int
&& proj_as_int
->value() == 1) {
1077 tex
->projector
= NULL
;
1079 tex
->projector
= read_rvalue(st
, proj_expr
);
1080 if (tex
->projector
== NULL
) {
1081 ir_read_error(st
, NULL
, "when reading projective divide in (%s ..)",
1087 s_list
*shadow_list
= SX_AS_LIST(proj_expr
->next
);
1088 if (shadow_list
== NULL
) {
1089 ir_read_error(st
, NULL
, "shadow comparitor must be a list");
1092 if (shadow_list
->subexpressions
.is_empty()) {
1093 tex
->shadow_comparitor
= NULL
;
1095 tex
->shadow_comparitor
= read_rvalue(st
, shadow_list
);
1096 if (tex
->shadow_comparitor
== NULL
) {
1097 ir_read_error(st
, NULL
, "when reading shadow comparitor in (%s ..)",
1102 s_expression
*lod_expr
= (s_expression
*) shadow_list
->next
;
1106 tex
->lod_info
.bias
= read_rvalue(st
, lod_expr
);
1107 if (tex
->lod_info
.bias
== NULL
) {
1108 ir_read_error(st
, NULL
, "when reading LOD bias in (txb ...)");
1113 tex
->lod_info
.lod
= read_rvalue(st
, lod_expr
);
1114 if (tex
->lod_info
.lod
== NULL
) {
1115 ir_read_error(st
, NULL
, "when reading LOD in (txl ...)");
1120 s_list
*lod_list
= SX_AS_LIST(lod_expr
);
1121 if (lod_list
->length() != 2) {
1122 ir_read_error(st
, lod_expr
, "expected (dPdx dPdy) in (txd ...)");
1125 s_expression
*dx_expr
= (s_expression
*) lod_list
->subexpressions
.head
;
1126 s_expression
*dy_expr
= (s_expression
*) dx_expr
->next
;
1128 tex
->lod_info
.grad
.dPdx
= read_rvalue(st
, dx_expr
);
1129 if (tex
->lod_info
.grad
.dPdx
== NULL
) {
1130 ir_read_error(st
, NULL
, "when reading dPdx in (txd ...)");
1133 tex
->lod_info
.grad
.dPdy
= read_rvalue(st
, dy_expr
);
1134 if (tex
->lod_info
.grad
.dPdy
== NULL
) {
1135 ir_read_error(st
, NULL
, "when reading dPdy in (txd ...)");
1141 // tex doesn't have any extra parameters and txf was handled earlier.