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
,
71 const char *src
, bool scan_for_protos
)
73 s_expression
*expr
= s_expression::read_expression(state
, src
);
75 ir_read_error(state
, NULL
, "couldn't parse S-Expression.");
79 if (scan_for_protos
) {
80 scan_for_prototypes(state
, instructions
, expr
);
85 read_instructions(state
, instructions
, expr
, NULL
);
90 ir_read_error(_mesa_glsl_parse_state
*state
, s_expression
*expr
,
97 if (state
->current_function
!= NULL
)
98 state
->info_log
= talloc_asprintf_append(state
->info_log
,
100 state
->current_function
->function_name());
101 state
->info_log
= talloc_strdup_append(state
->info_log
, "error: ");
104 state
->info_log
= talloc_vasprintf_append(state
->info_log
, fmt
, ap
);
106 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n");
109 state
->info_log
= talloc_strdup_append(state
->info_log
,
110 "...in this context:\n ");
112 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n\n");
116 static const glsl_type
*
117 read_type(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
119 s_list
*list
= SX_AS_LIST(expr
);
121 s_symbol
*type_sym
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
122 if (type_sym
== NULL
) {
123 ir_read_error(st
, expr
, "expected type (array ...) or (struct ...)");
126 if (strcmp(type_sym
->value(), "array") == 0) {
127 if (list
->length() != 3) {
128 ir_read_error(st
, expr
, "expected type (array <type> <int>)");
133 s_expression
*base_expr
= (s_expression
*) type_sym
->next
;
134 const glsl_type
*base_type
= read_type(st
, base_expr
);
135 if (base_type
== NULL
) {
136 ir_read_error(st
, NULL
, "when reading base type of array");
141 s_int
*size
= SX_AS_INT(base_expr
->next
);
143 ir_read_error(st
, expr
, "found non-integer array size");
147 return glsl_type::get_array_instance(base_type
, size
->value());
148 } else if (strcmp(type_sym
->value(), "struct") == 0) {
149 assert(false); // FINISHME
151 ir_read_error(st
, expr
, "expected (array ...) or (struct ...); "
152 "found (%s ...)", type_sym
->value());
157 s_symbol
*type_sym
= SX_AS_SYMBOL(expr
);
158 if (type_sym
== NULL
) {
159 ir_read_error(st
, expr
, "expected <type> (symbol or list)");
163 const glsl_type
*type
= st
->symbols
->get_type(type_sym
->value());
165 ir_read_error(st
, expr
, "invalid type: %s", type_sym
->value());
172 scan_for_prototypes(_mesa_glsl_parse_state
*st
, exec_list
*instructions
,
175 s_list
*list
= SX_AS_LIST(expr
);
177 ir_read_error(st
, expr
, "Expected (<instruction> ...); found an atom.");
181 foreach_iter(exec_list_iterator
, it
, list
->subexpressions
) {
182 s_list
*sub
= SX_AS_LIST(it
.get());
184 continue; // not a (function ...); ignore it.
186 s_symbol
*tag
= SX_AS_SYMBOL(sub
->subexpressions
.get_head());
187 if (tag
== NULL
|| strcmp(tag
->value(), "function") != 0)
188 continue; // not a (function ...); ignore it.
190 ir_function
*f
= read_function(st
, sub
, true);
193 instructions
->push_tail(f
);
198 read_function(_mesa_glsl_parse_state
*st
, s_list
*list
, bool skip_body
)
202 if (list
->length() < 3) {
203 ir_read_error(st
, list
, "Expected (function <name> (signature ...) ...)");
207 s_symbol
*name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
209 ir_read_error(st
, list
, "Expected (function <name> ...)");
213 ir_function
*f
= st
->symbols
->get_function(name
->value());
215 f
= new(ctx
) ir_function(name
->value());
216 f
->is_builtin
= true;
217 added
= st
->symbols
->add_function(f
->name
, f
);
221 exec_list_iterator it
= list
->subexpressions
.iterator();
222 it
.next(); // skip "function" tag
223 it
.next(); // skip function name
224 for (/* nothing */; it
.has_next(); it
.next()) {
225 s_list
*siglist
= SX_AS_LIST(it
.get());
226 if (siglist
== NULL
) {
227 ir_read_error(st
, list
, "Expected (function (signature ...) ...)");
231 s_symbol
*tag
= SX_AS_SYMBOL(siglist
->subexpressions
.get_head());
232 if (tag
== NULL
|| strcmp(tag
->value(), "signature") != 0) {
233 ir_read_error(st
, siglist
, "Expected (signature ...)");
237 read_function_sig(st
, f
, siglist
, skip_body
);
239 return added
? f
: NULL
;
243 read_function_sig(_mesa_glsl_parse_state
*st
, ir_function
*f
, s_list
*list
,
247 if (list
->length() != 4) {
248 ir_read_error(st
, list
, "Expected (signature <type> (parameters ...) "
249 "(<instruction> ...))");
253 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
254 const glsl_type
*return_type
= read_type(st
, type_expr
);
255 if (return_type
== NULL
)
258 s_list
*paramlist
= SX_AS_LIST(type_expr
->next
);
259 s_list
*body_list
= SX_AS_LIST(paramlist
->next
);
260 if (paramlist
== NULL
|| body_list
== NULL
) {
261 ir_read_error(st
, list
, "Expected (signature <type> (parameters ...) "
262 "(<instruction> ...))");
265 s_symbol
*paramtag
= SX_AS_SYMBOL(paramlist
->subexpressions
.get_head());
266 if (paramtag
== NULL
|| strcmp(paramtag
->value(), "parameters") != 0) {
267 ir_read_error(st
, paramlist
, "Expected (parameters ...)");
271 // Read the parameters list into a temporary place.
272 exec_list hir_parameters
;
273 st
->symbols
->push_scope();
275 exec_list_iterator it
= paramlist
->subexpressions
.iterator();
276 for (it
.next() /* skip "parameters" */; it
.has_next(); it
.next()) {
277 s_list
*decl
= SX_AS_LIST(it
.get());
278 ir_variable
*var
= read_declaration(st
, decl
);
282 hir_parameters
.push_tail(var
);
285 ir_function_signature
*sig
= f
->exact_matching_signature(&hir_parameters
);
286 if (sig
== NULL
&& skip_body
) {
287 /* If scanning for prototypes, generate a new signature. */
288 sig
= new(ctx
) ir_function_signature(return_type
);
289 f
->add_signature(sig
);
290 } else if (sig
!= NULL
) {
291 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
292 if (badvar
!= NULL
) {
293 ir_read_error(st
, list
, "function `%s' parameter `%s' qualifiers "
294 "don't match prototype", f
->name
, badvar
);
298 if (sig
->return_type
!= return_type
) {
299 ir_read_error(st
, list
, "function `%s' return type doesn't "
300 "match prototype", f
->name
);
304 /* No prototype for this body exists - skip it. */
305 st
->symbols
->pop_scope();
310 sig
->replace_parameters(&hir_parameters
);
312 if (!skip_body
&& !body_list
->subexpressions
.is_empty()) {
313 if (sig
->is_defined
) {
314 ir_read_error(st
, list
, "function %s redefined", f
->name
);
317 st
->current_function
= sig
;
318 read_instructions(st
, &sig
->body
, body_list
, NULL
);
319 st
->current_function
= NULL
;
320 sig
->is_defined
= true;
323 st
->symbols
->pop_scope();
327 read_instructions(_mesa_glsl_parse_state
*st
, exec_list
*instructions
,
328 s_expression
*expr
, ir_loop
*loop_ctx
)
330 // Read in a list of instructions
331 s_list
*list
= SX_AS_LIST(expr
);
333 ir_read_error(st
, expr
, "Expected (<instruction> ...); found an atom.");
337 foreach_iter(exec_list_iterator
, it
, list
->subexpressions
) {
338 s_expression
*sub
= (s_expression
*) it
.get();
339 ir_instruction
*ir
= read_instruction(st
, sub
, loop_ctx
);
341 instructions
->push_tail(ir
);
346 static ir_instruction
*
347 read_instruction(_mesa_glsl_parse_state
*st
, s_expression
*expr
,
351 s_symbol
*symbol
= SX_AS_SYMBOL(expr
);
352 if (symbol
!= NULL
) {
353 if (strcmp(symbol
->value(), "break") == 0 && loop_ctx
!= NULL
)
354 return new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
355 if (strcmp(symbol
->value(), "continue") == 0 && loop_ctx
!= NULL
)
356 return new(ctx
) ir_loop_jump(ir_loop_jump::jump_continue
);
359 s_list
*list
= SX_AS_LIST(expr
);
360 if (list
== NULL
|| list
->subexpressions
.is_empty()) {
361 ir_read_error(st
, expr
, "Invalid instruction.\n");
365 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
367 ir_read_error(st
, expr
, "expected instruction tag");
371 ir_instruction
*inst
= NULL
;
372 if (strcmp(tag
->value(), "declare") == 0) {
373 inst
= read_declaration(st
, list
);
374 } else if (strcmp(tag
->value(), "assign") == 0) {
375 inst
= read_assignment(st
, list
);
376 } else if (strcmp(tag
->value(), "if") == 0) {
377 inst
= read_if(st
, list
, loop_ctx
);
378 } else if (strcmp(tag
->value(), "loop") == 0) {
379 inst
= read_loop(st
, list
);
380 } else if (strcmp(tag
->value(), "return") == 0) {
381 inst
= read_return(st
, list
);
382 } else if (strcmp(tag
->value(), "function") == 0) {
383 inst
= read_function(st
, list
, false);
385 inst
= read_rvalue(st
, list
);
387 ir_read_error(st
, NULL
, "when reading instruction");
394 read_declaration(_mesa_glsl_parse_state
*st
, s_list
*list
)
397 if (list
->length() != 4) {
398 ir_read_error(st
, list
, "expected (declare (<qualifiers>) <type> "
403 s_list
*quals
= SX_AS_LIST(list
->subexpressions
.head
->next
);
405 ir_read_error(st
, list
, "expected a list of variable qualifiers");
409 s_expression
*type_expr
= (s_expression
*) quals
->next
;
410 const glsl_type
*type
= read_type(st
, type_expr
);
414 s_symbol
*var_name
= SX_AS_SYMBOL(type_expr
->next
);
415 if (var_name
== NULL
) {
416 ir_read_error(st
, list
, "expected variable name, found non-symbol");
420 ir_variable
*var
= new(ctx
) ir_variable(type
, var_name
->value(),
423 foreach_iter(exec_list_iterator
, it
, quals
->subexpressions
) {
424 s_symbol
*qualifier
= SX_AS_SYMBOL(it
.get());
425 if (qualifier
== NULL
) {
426 ir_read_error(st
, list
, "qualifier list must contain only symbols");
431 // FINISHME: Check for duplicate/conflicting qualifiers.
432 if (strcmp(qualifier
->value(), "centroid") == 0) {
434 } else if (strcmp(qualifier
->value(), "invariant") == 0) {
436 } else if (strcmp(qualifier
->value(), "uniform") == 0) {
437 var
->mode
= ir_var_uniform
;
438 } else if (strcmp(qualifier
->value(), "auto") == 0) {
439 var
->mode
= ir_var_auto
;
440 } else if (strcmp(qualifier
->value(), "in") == 0) {
441 var
->mode
= ir_var_in
;
442 } else if (strcmp(qualifier
->value(), "out") == 0) {
443 var
->mode
= ir_var_out
;
444 } else if (strcmp(qualifier
->value(), "inout") == 0) {
445 var
->mode
= ir_var_inout
;
446 } else if (strcmp(qualifier
->value(), "smooth") == 0) {
447 var
->interpolation
= ir_var_smooth
;
448 } else if (strcmp(qualifier
->value(), "flat") == 0) {
449 var
->interpolation
= ir_var_flat
;
450 } else if (strcmp(qualifier
->value(), "noperspective") == 0) {
451 var
->interpolation
= ir_var_noperspective
;
453 ir_read_error(st
, list
, "unknown qualifier: %s", qualifier
->value());
459 // Add the variable to the symbol table
460 st
->symbols
->add_variable(var
->name
, var
);
467 read_if(_mesa_glsl_parse_state
*st
, s_list
*list
, ir_loop
*loop_ctx
)
470 if (list
->length() != 4) {
471 ir_read_error(st
, list
, "expected (if <condition> (<then> ...) "
476 s_expression
*cond_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
477 ir_rvalue
*condition
= read_rvalue(st
, cond_expr
);
478 if (condition
== NULL
) {
479 ir_read_error(st
, NULL
, "when reading condition of (if ...)");
483 s_expression
*then_expr
= (s_expression
*) cond_expr
->next
;
484 s_expression
*else_expr
= (s_expression
*) then_expr
->next
;
486 ir_if
*iff
= new(ctx
) ir_if(condition
);
488 read_instructions(st
, &iff
->then_instructions
, then_expr
, loop_ctx
);
489 read_instructions(st
, &iff
->else_instructions
, else_expr
, loop_ctx
);
499 read_loop(_mesa_glsl_parse_state
*st
, s_list
*list
)
502 if (list
->length() != 6) {
503 ir_read_error(st
, list
, "expected (loop <counter> <from> <to> "
504 "<increment> <body>)");
508 s_expression
*count_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
509 s_expression
*from_expr
= (s_expression
*) count_expr
->next
;
510 s_expression
*to_expr
= (s_expression
*) from_expr
->next
;
511 s_expression
*inc_expr
= (s_expression
*) to_expr
->next
;
512 s_expression
*body_expr
= (s_expression
*) inc_expr
->next
;
514 // FINISHME: actually read the count/from/to fields.
516 ir_loop
*loop
= new(ctx
) ir_loop
;
517 read_instructions(st
, &loop
->body_instructions
, body_expr
, loop
);
527 read_return(_mesa_glsl_parse_state
*st
, s_list
*list
)
530 if (list
->length() != 2) {
531 ir_read_error(st
, list
, "expected (return <rvalue>)");
535 s_expression
*expr
= (s_expression
*) list
->subexpressions
.head
->next
;
537 ir_rvalue
*retval
= read_rvalue(st
, expr
);
538 if (retval
== NULL
) {
539 ir_read_error(st
, NULL
, "when reading return value");
543 return new(ctx
) ir_return(retval
);
548 read_rvalue(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
550 s_list
*list
= SX_AS_LIST(expr
);
551 if (list
== NULL
|| list
->subexpressions
.is_empty())
554 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
556 ir_read_error(st
, expr
, "expected rvalue tag");
560 ir_rvalue
*rvalue
= read_dereference(st
, list
);
561 if (rvalue
!= NULL
|| st
->error
)
563 else if (strcmp(tag
->value(), "swiz") == 0) {
564 rvalue
= read_swizzle(st
, list
);
565 } else if (strcmp(tag
->value(), "expression") == 0) {
566 rvalue
= read_expression(st
, list
);
567 } else if (strcmp(tag
->value(), "call") == 0) {
568 rvalue
= read_call(st
, list
);
569 } else if (strcmp(tag
->value(), "constant") == 0) {
570 rvalue
= read_constant(st
, list
);
572 rvalue
= read_texture(st
, list
);
573 if (rvalue
== NULL
&& !st
->error
)
574 ir_read_error(st
, expr
, "unrecognized rvalue tag: %s", tag
->value());
580 static ir_assignment
*
581 read_assignment(_mesa_glsl_parse_state
*st
, s_list
*list
)
584 const unsigned list_length
= list
->length();
585 if (list_length
< 4 || list_length
> 5) {
586 ir_read_error(st
, list
, "expected (assign <condition> (<write mask>) "
591 s_expression
*cond_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
592 s_list
*mask_list
= SX_AS_LIST(cond_expr
->next
);
593 s_expression
*lhs_expr
= (s_expression
*)
594 (list_length
== 4 ? cond_expr
->next
: cond_expr
->next
->next
);
595 s_expression
*rhs_expr
= (s_expression
*) lhs_expr
->next
;
597 ir_rvalue
*condition
= read_rvalue(st
, cond_expr
);
598 if (condition
== NULL
) {
599 ir_read_error(st
, NULL
, "when reading condition of assignment");
603 if (list_length
== 5 && mask_list
== NULL
|| mask_list
->length() > 1) {
604 ir_read_error(st
, mask_list
, "expected () or (<write mask>)");
609 if (list_length
== 5 && mask_list
->length() == 1) {
610 s_symbol
*mask_symbol
= SX_AS_SYMBOL(mask_list
->subexpressions
.head
);
611 if (mask_symbol
== NULL
) {
612 ir_read_error(st
, list
, "expected a write mask; found non-symbol");
616 const char *mask_str
= mask_symbol
->value();
617 unsigned mask_length
= strlen(mask_str
);
618 if (mask_length
> 4) {
619 ir_read_error(st
, list
, "invalid write mask: %s", mask_str
);
623 const unsigned idx_map
[] = { 3, 0, 1, 2 }; /* w=bit 3, x=0, y=1, z=2 */
625 for (unsigned i
= 0; i
< mask_length
; i
++) {
626 if (mask_str
[i
] < 'w' || mask_str
[i
] > 'z') {
627 ir_read_error(st
, list
, "write mask contains invalid character: %c",
631 mask
|= 1 << idx_map
[mask_str
[i
] - 'w'];
635 ir_dereference
*lhs
= read_dereference(st
, lhs_expr
);
637 ir_read_error(st
, NULL
, "when reading left-hand side of assignment");
641 ir_rvalue
*rhs
= read_rvalue(st
, rhs_expr
);
643 ir_read_error(st
, NULL
, "when reading right-hand side of assignment");
647 if (mask
== 0 && (lhs
->type
->is_vector() || lhs
->type
->is_scalar())) {
648 ir_read_error(st
, list
, "non-zero write mask required.");
652 return new(ctx
) ir_assignment(lhs
, rhs
, condition
, mask
);
656 read_call(_mesa_glsl_parse_state
*st
, s_list
*list
)
659 if (list
->length() != 3) {
660 ir_read_error(st
, list
, "expected (call <name> (<param> ...))");
664 s_symbol
*name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
665 s_list
*params
= SX_AS_LIST(name
->next
);
666 if (name
== NULL
|| params
== NULL
) {
667 ir_read_error(st
, list
, "expected (call <name> (<param> ...))");
671 exec_list parameters
;
673 foreach_iter(exec_list_iterator
, it
, params
->subexpressions
) {
674 s_expression
*expr
= (s_expression
*) it
.get();
675 ir_rvalue
*param
= read_rvalue(st
, expr
);
677 ir_read_error(st
, list
, "when reading parameter to function call");
680 parameters
.push_tail(param
);
683 ir_function
*f
= st
->symbols
->get_function(name
->value());
685 ir_read_error(st
, list
, "found call to undefined function %s",
690 ir_function_signature
*callee
= f
->matching_signature(¶meters
);
691 if (callee
== NULL
) {
692 ir_read_error(st
, list
, "couldn't find matching signature for function "
693 "%s", name
->value());
697 return new(ctx
) ir_call(callee
, ¶meters
);
700 static ir_expression
*
701 read_expression(_mesa_glsl_parse_state
*st
, s_list
*list
)
704 const unsigned list_length
= list
->length();
705 if (list_length
< 4) {
706 ir_read_error(st
, list
, "expected (expression <type> <operator> "
707 "<operand> [<operand>])");
711 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
712 const glsl_type
*type
= read_type(st
, type_expr
);
716 /* Read the operator */
717 s_symbol
*op_sym
= SX_AS_SYMBOL(type_expr
->next
);
718 if (op_sym
== NULL
) {
719 ir_read_error(st
, list
, "expected operator, found non-symbol");
723 ir_expression_operation op
= ir_expression::get_operator(op_sym
->value());
724 if (op
== (ir_expression_operation
) -1) {
725 ir_read_error(st
, list
, "invalid operator: %s", op_sym
->value());
729 /* Now that we know the operator, check for the right number of operands */
730 if (ir_expression::get_num_operands(op
) == 2) {
731 if (list_length
!= 5) {
732 ir_read_error(st
, list
, "expected (expression <type> %s <operand> "
733 " <operand>)", op_sym
->value());
737 if (list_length
!= 4) {
738 ir_read_error(st
, list
, "expected (expression <type> %s <operand>)",
744 s_expression
*exp1
= (s_expression
*) (op_sym
->next
);
745 ir_rvalue
*arg1
= read_rvalue(st
, exp1
);
747 ir_read_error(st
, NULL
, "when reading first operand of %s",
752 ir_rvalue
*arg2
= NULL
;
753 if (ir_expression::get_num_operands(op
) == 2) {
754 s_expression
*exp2
= (s_expression
*) (exp1
->next
);
755 arg2
= read_rvalue(st
, exp2
);
757 ir_read_error(st
, NULL
, "when reading second operand of %s",
763 return new(ctx
) ir_expression(op
, type
, arg1
, arg2
);
767 read_swizzle(_mesa_glsl_parse_state
*st
, s_list
*list
)
769 if (list
->length() != 3) {
770 ir_read_error(st
, list
, "expected (swiz <swizzle> <rvalue>)");
774 s_symbol
*swiz
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
776 ir_read_error(st
, list
, "expected a valid swizzle; found non-symbol");
780 if (strlen(swiz
->value()) > 4) {
781 ir_read_error(st
, list
, "expected a valid swizzle; found %s",
786 s_expression
*sub
= (s_expression
*) swiz
->next
;
788 ir_read_error(st
, list
, "expected rvalue: (swizzle %s <rvalue>)",
793 ir_rvalue
*rvalue
= read_rvalue(st
, sub
);
797 ir_swizzle
*ir
= ir_swizzle::create(rvalue
, swiz
->value(),
798 rvalue
->type
->vector_elements
);
800 ir_read_error(st
, list
, "invalid swizzle");
806 read_constant(_mesa_glsl_parse_state
*st
, s_list
*list
)
809 if (list
->length() != 3) {
810 ir_read_error(st
, list
, "expected (constant <type> (...))");
814 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
815 const glsl_type
*type
= read_type(st
, type_expr
);
819 s_list
*values
= SX_AS_LIST(type_expr
->next
);
820 if (values
== NULL
) {
821 ir_read_error(st
, list
, "expected (constant <type> (...))");
825 if (type
->is_array()) {
826 const unsigned elements_supplied
= values
->length();
827 if (elements_supplied
!= type
->length
) {
828 ir_read_error(st
, values
, "expected exactly %u array elements, "
829 "given %u", type
->length
, elements_supplied
);
834 foreach_iter(exec_list_iterator
, it
, values
->subexpressions
) {
835 s_expression
*expr
= (s_expression
*) it
.get();
836 s_list
*elt
= SX_AS_LIST(expr
);
838 ir_read_error(st
, expr
, "expected (constant ...) array element");
842 ir_constant
*ir_elt
= read_constant(st
, elt
);
845 elements
.push_tail(ir_elt
);
847 return new(ctx
) ir_constant(type
, &elements
);
850 const glsl_type
*const base_type
= type
->get_base_type();
852 ir_constant_data data
= { { 0 } };
854 // Read in list of values (at most 16).
856 foreach_iter(exec_list_iterator
, it
, values
->subexpressions
) {
858 ir_read_error(st
, values
, "expected at most 16 numbers");
862 s_expression
*expr
= (s_expression
*) it
.get();
864 if (base_type
->base_type
== GLSL_TYPE_FLOAT
) {
865 s_number
*value
= SX_AS_NUMBER(expr
);
867 ir_read_error(st
, values
, "expected numbers");
870 data
.f
[k
] = value
->fvalue();
872 s_int
*value
= SX_AS_INT(expr
);
874 ir_read_error(st
, values
, "expected integers");
878 switch (base_type
->base_type
) {
879 case GLSL_TYPE_UINT
: {
880 data
.u
[k
] = value
->value();
883 case GLSL_TYPE_INT
: {
884 data
.i
[k
] = value
->value();
887 case GLSL_TYPE_BOOL
: {
888 data
.b
[k
] = value
->value();
892 ir_read_error(st
, values
, "unsupported constant type");
899 return new(ctx
) ir_constant(type
, &data
);
902 static ir_dereference
*
903 read_dereference(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
905 s_list
*list
= SX_AS_LIST(expr
);
906 if (list
== NULL
|| list
->subexpressions
.is_empty())
909 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.head
);
912 if (strcmp(tag
->value(), "var_ref") == 0)
913 return read_var_ref(st
, list
);
914 if (strcmp(tag
->value(), "array_ref") == 0)
915 return read_array_ref(st
, list
);
916 if (strcmp(tag
->value(), "record_ref") == 0)
917 return read_record_ref(st
, list
);
921 static ir_dereference
*
922 read_var_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
925 if (list
->length() != 2) {
926 ir_read_error(st
, list
, "expected (var_ref <variable name>)");
929 s_symbol
*var_name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
930 if (var_name
== NULL
) {
931 ir_read_error(st
, list
, "expected (var_ref <variable name>)");
935 ir_variable
*var
= st
->symbols
->get_variable(var_name
->value());
937 ir_read_error(st
, list
, "undeclared variable: %s", var_name
->value());
941 return new(ctx
) ir_dereference_variable(var
);
944 static ir_dereference
*
945 read_array_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
948 if (list
->length() != 3) {
949 ir_read_error(st
, list
, "expected (array_ref <rvalue> <index>)");
953 s_expression
*subj_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
954 ir_rvalue
*subject
= read_rvalue(st
, subj_expr
);
955 if (subject
== NULL
) {
956 ir_read_error(st
, NULL
, "when reading the subject of an array_ref");
960 s_expression
*idx_expr
= (s_expression
*) subj_expr
->next
;
961 ir_rvalue
*idx
= read_rvalue(st
, idx_expr
);
962 return new(ctx
) ir_dereference_array(subject
, idx
);
965 static ir_dereference
*
966 read_record_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
969 if (list
->length() != 3) {
970 ir_read_error(st
, list
, "expected (record_ref <rvalue> <field>)");
974 s_expression
*subj_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
975 ir_rvalue
*subject
= read_rvalue(st
, subj_expr
);
976 if (subject
== NULL
) {
977 ir_read_error(st
, NULL
, "when reading the subject of a record_ref");
981 s_symbol
*field
= SX_AS_SYMBOL(subj_expr
->next
);
983 ir_read_error(st
, list
, "expected (record_ref ... <field name>)");
986 return new(ctx
) ir_dereference_record(subject
, field
->value());
990 valid_texture_list_length(ir_texture_opcode op
, s_list
*list
)
992 unsigned required_length
= 7;
995 else if (op
== ir_tex
)
998 return list
->length() == required_length
;
1002 read_texture(_mesa_glsl_parse_state
*st
, s_list
*list
)
1005 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.head
);
1006 assert(tag
!= NULL
);
1008 ir_texture_opcode op
= ir_texture::get_opcode(tag
->value());
1009 if (op
== (ir_texture_opcode
) -1)
1012 if (!valid_texture_list_length(op
, list
)) {
1013 ir_read_error(st
, NULL
, "invalid list size in (%s ...)", tag
->value());
1017 ir_texture
*tex
= new(ctx
) ir_texture(op
);
1019 // Read sampler (must be a deref)
1020 s_expression
*sampler_expr
= (s_expression
*) tag
->next
;
1021 ir_dereference
*sampler
= read_dereference(st
, sampler_expr
);
1022 if (sampler
== NULL
) {
1023 ir_read_error(st
, NULL
, "when reading sampler in (%s ...)", tag
->value());
1026 tex
->set_sampler(sampler
);
1028 // Read coordinate (any rvalue)
1029 s_expression
*coordinate_expr
= (s_expression
*) sampler_expr
->next
;
1030 tex
->coordinate
= read_rvalue(st
, coordinate_expr
);
1031 if (tex
->coordinate
== NULL
) {
1032 ir_read_error(st
, NULL
, "when reading coordinate in (%s ...)",
1037 // Read texel offset, i.e. (0 0 0)
1038 s_list
*offset_list
= SX_AS_LIST(coordinate_expr
->next
);
1039 if (offset_list
== NULL
|| offset_list
->length() != 3) {
1040 ir_read_error(st
, offset_list
, "expected (<int> <int> <int>)");
1043 s_int
*offset_x
= SX_AS_INT(offset_list
->subexpressions
.head
);
1044 s_int
*offset_y
= SX_AS_INT(offset_x
->next
);
1045 s_int
*offset_z
= SX_AS_INT(offset_y
->next
);
1046 if (offset_x
== NULL
|| offset_y
== NULL
|| offset_z
== NULL
) {
1047 ir_read_error(st
, offset_list
, "expected (<int> <int> <int>)");
1050 tex
->offsets
[0] = offset_x
->value();
1051 tex
->offsets
[1] = offset_y
->value();
1052 tex
->offsets
[2] = offset_z
->value();
1055 s_expression
*lod_expr
= (s_expression
*) offset_list
->next
;
1056 tex
->lod_info
.lod
= read_rvalue(st
, lod_expr
);
1057 if (tex
->lod_info
.lod
== NULL
) {
1058 ir_read_error(st
, NULL
, "when reading LOD in (txf ...)");
1062 s_expression
*proj_expr
= (s_expression
*) offset_list
->next
;
1063 s_int
*proj_as_int
= SX_AS_INT(proj_expr
);
1064 if (proj_as_int
&& proj_as_int
->value() == 1) {
1065 tex
->projector
= NULL
;
1067 tex
->projector
= read_rvalue(st
, proj_expr
);
1068 if (tex
->projector
== NULL
) {
1069 ir_read_error(st
, NULL
, "when reading projective divide in (%s ..)",
1075 s_list
*shadow_list
= SX_AS_LIST(proj_expr
->next
);
1076 if (shadow_list
== NULL
) {
1077 ir_read_error(st
, NULL
, "shadow comparitor must be a list");
1080 if (shadow_list
->subexpressions
.is_empty()) {
1081 tex
->shadow_comparitor
= NULL
;
1083 tex
->shadow_comparitor
= read_rvalue(st
, shadow_list
);
1084 if (tex
->shadow_comparitor
== NULL
) {
1085 ir_read_error(st
, NULL
, "when reading shadow comparitor in (%s ..)",
1090 s_expression
*lod_expr
= (s_expression
*) shadow_list
->next
;
1094 tex
->lod_info
.bias
= read_rvalue(st
, lod_expr
);
1095 if (tex
->lod_info
.bias
== NULL
) {
1096 ir_read_error(st
, NULL
, "when reading LOD bias in (txb ...)");
1101 tex
->lod_info
.lod
= read_rvalue(st
, lod_expr
);
1102 if (tex
->lod_info
.lod
== NULL
) {
1103 ir_read_error(st
, NULL
, "when reading LOD in (txl ...)");
1108 s_list
*lod_list
= SX_AS_LIST(lod_expr
);
1109 if (lod_list
->length() != 2) {
1110 ir_read_error(st
, lod_expr
, "expected (dPdx dPdy) in (txd ...)");
1113 s_expression
*dx_expr
= (s_expression
*) lod_list
->subexpressions
.head
;
1114 s_expression
*dy_expr
= (s_expression
*) dx_expr
->next
;
1116 tex
->lod_info
.grad
.dPdx
= read_rvalue(st
, dx_expr
);
1117 if (tex
->lod_info
.grad
.dPdx
== NULL
) {
1118 ir_read_error(st
, NULL
, "when reading dPdx in (txd ...)");
1121 tex
->lod_info
.grad
.dPdy
= read_rvalue(st
, dy_expr
);
1122 if (tex
->lod_info
.grad
.dPdy
== NULL
) {
1123 ir_read_error(st
, NULL
, "when reading dPdy in (txd ...)");
1129 // tex doesn't have any extra parameters and txf was handled earlier.