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_variable
*
68 read_var_ref(_mesa_glsl_parse_state
*, s_list
*);
69 static ir_dereference_array
*
70 read_array_ref(_mesa_glsl_parse_state
*, s_list
*);
71 static ir_dereference_record
*
72 read_record_ref(_mesa_glsl_parse_state
*, s_list
*);
75 _mesa_glsl_read_ir(_mesa_glsl_parse_state
*state
, exec_list
*instructions
,
76 const char *src
, bool scan_for_protos
)
78 s_expression
*expr
= s_expression::read_expression(state
, src
);
80 ir_read_error(state
, NULL
, "couldn't parse S-Expression.");
84 if (scan_for_protos
) {
85 scan_for_prototypes(state
, instructions
, expr
);
90 read_instructions(state
, instructions
, expr
, NULL
);
94 validate_ir_tree(instructions
);
98 ir_read_error(_mesa_glsl_parse_state
*state
, s_expression
*expr
,
105 if (state
->current_function
!= NULL
)
106 state
->info_log
= talloc_asprintf_append(state
->info_log
,
108 state
->current_function
->function_name());
109 state
->info_log
= talloc_strdup_append(state
->info_log
, "error: ");
112 state
->info_log
= talloc_vasprintf_append(state
->info_log
, fmt
, ap
);
114 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n");
117 state
->info_log
= talloc_strdup_append(state
->info_log
,
118 "...in this context:\n ");
120 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n\n");
124 static const glsl_type
*
125 read_type(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
127 s_list
*list
= SX_AS_LIST(expr
);
129 s_symbol
*type_sym
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
130 if (type_sym
== NULL
) {
131 ir_read_error(st
, expr
, "expected type (array ...) or (struct ...)");
134 if (strcmp(type_sym
->value(), "array") == 0) {
135 if (list
->length() != 3) {
136 ir_read_error(st
, expr
, "expected type (array <type> <int>)");
141 s_expression
*base_expr
= (s_expression
*) type_sym
->next
;
142 const glsl_type
*base_type
= read_type(st
, base_expr
);
143 if (base_type
== NULL
) {
144 ir_read_error(st
, NULL
, "when reading base type of array");
149 s_int
*size
= SX_AS_INT(base_expr
->next
);
151 ir_read_error(st
, expr
, "found non-integer array size");
155 return glsl_type::get_array_instance(base_type
, size
->value());
156 } else if (strcmp(type_sym
->value(), "struct") == 0) {
157 assert(false); // FINISHME
159 ir_read_error(st
, expr
, "expected (array ...) or (struct ...); "
160 "found (%s ...)", type_sym
->value());
165 s_symbol
*type_sym
= SX_AS_SYMBOL(expr
);
166 if (type_sym
== NULL
) {
167 ir_read_error(st
, expr
, "expected <type> (symbol or list)");
171 const glsl_type
*type
= st
->symbols
->get_type(type_sym
->value());
173 ir_read_error(st
, expr
, "invalid type: %s", type_sym
->value());
180 scan_for_prototypes(_mesa_glsl_parse_state
*st
, exec_list
*instructions
,
183 s_list
*list
= SX_AS_LIST(expr
);
185 ir_read_error(st
, expr
, "Expected (<instruction> ...); found an atom.");
189 foreach_iter(exec_list_iterator
, it
, list
->subexpressions
) {
190 s_list
*sub
= SX_AS_LIST(it
.get());
192 continue; // not a (function ...); ignore it.
194 s_symbol
*tag
= SX_AS_SYMBOL(sub
->subexpressions
.get_head());
195 if (tag
== NULL
|| strcmp(tag
->value(), "function") != 0)
196 continue; // not a (function ...); ignore it.
198 ir_function
*f
= read_function(st
, sub
, true);
201 instructions
->push_tail(f
);
206 read_function(_mesa_glsl_parse_state
*st
, s_list
*list
, bool skip_body
)
210 if (list
->length() < 3) {
211 ir_read_error(st
, list
, "Expected (function <name> (signature ...) ...)");
215 s_symbol
*name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
217 ir_read_error(st
, list
, "Expected (function <name> ...)");
221 ir_function
*f
= st
->symbols
->get_function(name
->value());
223 f
= new(ctx
) ir_function(name
->value());
224 added
= st
->symbols
->add_function(f
);
228 exec_list_iterator it
= list
->subexpressions
.iterator();
229 it
.next(); // skip "function" tag
230 it
.next(); // skip function name
231 for (/* nothing */; it
.has_next(); it
.next()) {
232 s_list
*siglist
= SX_AS_LIST(it
.get());
233 if (siglist
== NULL
) {
234 ir_read_error(st
, list
, "Expected (function (signature ...) ...)");
238 s_symbol
*tag
= SX_AS_SYMBOL(siglist
->subexpressions
.get_head());
239 if (tag
== NULL
|| strcmp(tag
->value(), "signature") != 0) {
240 ir_read_error(st
, siglist
, "Expected (signature ...)");
244 read_function_sig(st
, f
, siglist
, skip_body
);
246 return added
? f
: NULL
;
250 read_function_sig(_mesa_glsl_parse_state
*st
, ir_function
*f
, s_list
*list
,
254 if (list
->length() != 4) {
255 ir_read_error(st
, list
, "Expected (signature <type> (parameters ...) "
256 "(<instruction> ...))");
260 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
261 const glsl_type
*return_type
= read_type(st
, type_expr
);
262 if (return_type
== NULL
)
265 s_list
*paramlist
= SX_AS_LIST(type_expr
->next
);
266 s_list
*body_list
= SX_AS_LIST(type_expr
->next
->next
);
267 if (paramlist
== NULL
|| body_list
== NULL
) {
268 ir_read_error(st
, list
, "Expected (signature <type> (parameters ...) "
269 "(<instruction> ...))");
272 s_symbol
*paramtag
= SX_AS_SYMBOL(paramlist
->subexpressions
.get_head());
273 if (paramtag
== NULL
|| strcmp(paramtag
->value(), "parameters") != 0) {
274 ir_read_error(st
, paramlist
, "Expected (parameters ...)");
278 // Read the parameters list into a temporary place.
279 exec_list hir_parameters
;
280 st
->symbols
->push_scope();
282 exec_list_iterator it
= paramlist
->subexpressions
.iterator();
283 for (it
.next() /* skip "parameters" */; it
.has_next(); it
.next()) {
284 s_list
*decl
= SX_AS_LIST(it
.get());
285 ir_variable
*var
= read_declaration(st
, decl
);
289 hir_parameters
.push_tail(var
);
292 ir_function_signature
*sig
= f
->exact_matching_signature(&hir_parameters
);
293 if (sig
== NULL
&& skip_body
) {
294 /* If scanning for prototypes, generate a new signature. */
295 sig
= new(ctx
) ir_function_signature(return_type
);
296 sig
->is_builtin
= true;
297 f
->add_signature(sig
);
298 } else if (sig
!= NULL
) {
299 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
300 if (badvar
!= NULL
) {
301 ir_read_error(st
, list
, "function `%s' parameter `%s' qualifiers "
302 "don't match prototype", f
->name
, badvar
);
306 if (sig
->return_type
!= return_type
) {
307 ir_read_error(st
, list
, "function `%s' return type doesn't "
308 "match prototype", f
->name
);
312 /* No prototype for this body exists - skip it. */
313 st
->symbols
->pop_scope();
318 sig
->replace_parameters(&hir_parameters
);
320 if (!skip_body
&& !body_list
->subexpressions
.is_empty()) {
321 if (sig
->is_defined
) {
322 ir_read_error(st
, list
, "function %s redefined", f
->name
);
325 st
->current_function
= sig
;
326 read_instructions(st
, &sig
->body
, body_list
, NULL
);
327 st
->current_function
= NULL
;
328 sig
->is_defined
= true;
331 st
->symbols
->pop_scope();
335 read_instructions(_mesa_glsl_parse_state
*st
, exec_list
*instructions
,
336 s_expression
*expr
, ir_loop
*loop_ctx
)
338 // Read in a list of instructions
339 s_list
*list
= SX_AS_LIST(expr
);
341 ir_read_error(st
, expr
, "Expected (<instruction> ...); found an atom.");
345 foreach_iter(exec_list_iterator
, it
, list
->subexpressions
) {
346 s_expression
*sub
= (s_expression
*) it
.get();
347 ir_instruction
*ir
= read_instruction(st
, sub
, loop_ctx
);
349 /* Global variable declarations should be moved to the top, before
350 * any functions that might use them. Functions are added to the
351 * instruction stream when scanning for prototypes, so without this
352 * hack, they always appear before variable declarations.
354 if (st
->current_function
== NULL
&& ir
->as_variable() != NULL
)
355 instructions
->push_head(ir
);
357 instructions
->push_tail(ir
);
363 static ir_instruction
*
364 read_instruction(_mesa_glsl_parse_state
*st
, s_expression
*expr
,
368 s_symbol
*symbol
= SX_AS_SYMBOL(expr
);
369 if (symbol
!= NULL
) {
370 if (strcmp(symbol
->value(), "break") == 0 && loop_ctx
!= NULL
)
371 return new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
372 if (strcmp(symbol
->value(), "continue") == 0 && loop_ctx
!= NULL
)
373 return new(ctx
) ir_loop_jump(ir_loop_jump::jump_continue
);
376 s_list
*list
= SX_AS_LIST(expr
);
377 if (list
== NULL
|| list
->subexpressions
.is_empty()) {
378 ir_read_error(st
, expr
, "Invalid instruction.\n");
382 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
384 ir_read_error(st
, expr
, "expected instruction tag");
388 ir_instruction
*inst
= NULL
;
389 if (strcmp(tag
->value(), "declare") == 0) {
390 inst
= read_declaration(st
, list
);
391 } else if (strcmp(tag
->value(), "assign") == 0) {
392 inst
= read_assignment(st
, list
);
393 } else if (strcmp(tag
->value(), "if") == 0) {
394 inst
= read_if(st
, list
, loop_ctx
);
395 } else if (strcmp(tag
->value(), "loop") == 0) {
396 inst
= read_loop(st
, list
);
397 } else if (strcmp(tag
->value(), "return") == 0) {
398 inst
= read_return(st
, list
);
399 } else if (strcmp(tag
->value(), "function") == 0) {
400 inst
= read_function(st
, list
, false);
402 inst
= read_rvalue(st
, list
);
404 ir_read_error(st
, NULL
, "when reading instruction");
411 read_declaration(_mesa_glsl_parse_state
*st
, s_list
*list
)
414 if (list
->length() != 4) {
415 ir_read_error(st
, list
, "expected (declare (<qualifiers>) <type> "
420 s_list
*quals
= SX_AS_LIST(list
->subexpressions
.head
->next
);
422 ir_read_error(st
, list
, "expected a list of variable qualifiers");
426 s_expression
*type_expr
= (s_expression
*) quals
->next
;
427 const glsl_type
*type
= read_type(st
, type_expr
);
431 s_symbol
*var_name
= SX_AS_SYMBOL(type_expr
->next
);
432 if (var_name
== NULL
) {
433 ir_read_error(st
, list
, "expected variable name, found non-symbol");
437 ir_variable
*var
= new(ctx
) ir_variable(type
, var_name
->value(),
440 foreach_iter(exec_list_iterator
, it
, quals
->subexpressions
) {
441 s_symbol
*qualifier
= SX_AS_SYMBOL(it
.get());
442 if (qualifier
== NULL
) {
443 ir_read_error(st
, list
, "qualifier list must contain only symbols");
448 // FINISHME: Check for duplicate/conflicting qualifiers.
449 if (strcmp(qualifier
->value(), "centroid") == 0) {
451 } else if (strcmp(qualifier
->value(), "invariant") == 0) {
453 } else if (strcmp(qualifier
->value(), "uniform") == 0) {
454 var
->mode
= ir_var_uniform
;
455 } else if (strcmp(qualifier
->value(), "auto") == 0) {
456 var
->mode
= ir_var_auto
;
457 } else if (strcmp(qualifier
->value(), "in") == 0) {
458 var
->mode
= ir_var_in
;
459 } else if (strcmp(qualifier
->value(), "out") == 0) {
460 var
->mode
= ir_var_out
;
461 } else if (strcmp(qualifier
->value(), "inout") == 0) {
462 var
->mode
= ir_var_inout
;
463 } else if (strcmp(qualifier
->value(), "smooth") == 0) {
464 var
->interpolation
= ir_var_smooth
;
465 } else if (strcmp(qualifier
->value(), "flat") == 0) {
466 var
->interpolation
= ir_var_flat
;
467 } else if (strcmp(qualifier
->value(), "noperspective") == 0) {
468 var
->interpolation
= ir_var_noperspective
;
470 ir_read_error(st
, list
, "unknown qualifier: %s", qualifier
->value());
476 // Add the variable to the symbol table
477 st
->symbols
->add_variable(var
->name
, var
);
484 read_if(_mesa_glsl_parse_state
*st
, s_list
*list
, ir_loop
*loop_ctx
)
487 if (list
->length() != 4) {
488 ir_read_error(st
, list
, "expected (if <condition> (<then> ...) "
493 s_expression
*cond_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
494 ir_rvalue
*condition
= read_rvalue(st
, cond_expr
);
495 if (condition
== NULL
) {
496 ir_read_error(st
, NULL
, "when reading condition of (if ...)");
500 s_expression
*then_expr
= (s_expression
*) cond_expr
->next
;
501 s_expression
*else_expr
= (s_expression
*) then_expr
->next
;
503 ir_if
*iff
= new(ctx
) ir_if(condition
);
505 read_instructions(st
, &iff
->then_instructions
, then_expr
, loop_ctx
);
506 read_instructions(st
, &iff
->else_instructions
, else_expr
, loop_ctx
);
516 read_loop(_mesa_glsl_parse_state
*st
, s_list
*list
)
519 if (list
->length() != 6) {
520 ir_read_error(st
, list
, "expected (loop <counter> <from> <to> "
521 "<increment> <body>)");
525 s_expression
*count_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
526 s_expression
*from_expr
= (s_expression
*) count_expr
->next
;
527 s_expression
*to_expr
= (s_expression
*) from_expr
->next
;
528 s_expression
*inc_expr
= (s_expression
*) to_expr
->next
;
529 s_expression
*body_expr
= (s_expression
*) inc_expr
->next
;
531 // FINISHME: actually read the count/from/to fields.
533 ir_loop
*loop
= new(ctx
) ir_loop
;
534 read_instructions(st
, &loop
->body_instructions
, body_expr
, loop
);
544 read_return(_mesa_glsl_parse_state
*st
, s_list
*list
)
547 if (list
->length() != 2) {
548 ir_read_error(st
, list
, "expected (return <rvalue>)");
552 s_expression
*expr
= (s_expression
*) list
->subexpressions
.head
->next
;
554 ir_rvalue
*retval
= read_rvalue(st
, expr
);
555 if (retval
== NULL
) {
556 ir_read_error(st
, NULL
, "when reading return value");
560 return new(ctx
) ir_return(retval
);
565 read_rvalue(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
567 s_list
*list
= SX_AS_LIST(expr
);
568 if (list
== NULL
|| list
->subexpressions
.is_empty())
571 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
573 ir_read_error(st
, expr
, "expected rvalue tag");
577 ir_rvalue
*rvalue
= read_dereference(st
, list
);
578 if (rvalue
!= NULL
|| st
->error
)
580 else if (strcmp(tag
->value(), "swiz") == 0) {
581 rvalue
= read_swizzle(st
, list
);
582 } else if (strcmp(tag
->value(), "expression") == 0) {
583 rvalue
= read_expression(st
, list
);
584 } else if (strcmp(tag
->value(), "call") == 0) {
585 rvalue
= read_call(st
, list
);
586 } else if (strcmp(tag
->value(), "constant") == 0) {
587 rvalue
= read_constant(st
, list
);
589 rvalue
= read_texture(st
, list
);
590 if (rvalue
== NULL
&& !st
->error
)
591 ir_read_error(st
, expr
, "unrecognized rvalue tag: %s", tag
->value());
597 static ir_assignment
*
598 read_assignment(_mesa_glsl_parse_state
*st
, s_list
*list
)
601 if (list
->length() != 5) {
602 ir_read_error(st
, list
, "expected (assign <condition> (<write mask>) "
607 s_expression
*cond_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
608 s_list
*mask_list
= SX_AS_LIST(cond_expr
->next
);
609 s_expression
*lhs_expr
= (s_expression
*) cond_expr
->next
->next
;
610 s_expression
*rhs_expr
= (s_expression
*) lhs_expr
->next
;
612 ir_rvalue
*condition
= read_rvalue(st
, cond_expr
);
613 if (condition
== NULL
) {
614 ir_read_error(st
, NULL
, "when reading condition of assignment");
618 if (mask_list
== NULL
|| mask_list
->length() > 1) {
619 ir_read_error(st
, mask_list
, "expected () or (<write mask>)");
624 if (mask_list
->length() == 1) {
625 s_symbol
*mask_symbol
= SX_AS_SYMBOL(mask_list
->subexpressions
.head
);
626 if (mask_symbol
== NULL
) {
627 ir_read_error(st
, list
, "expected a write mask; found non-symbol");
631 const char *mask_str
= mask_symbol
->value();
632 unsigned mask_length
= strlen(mask_str
);
633 if (mask_length
> 4) {
634 ir_read_error(st
, list
, "invalid write mask: %s", mask_str
);
638 const unsigned idx_map
[] = { 3, 0, 1, 2 }; /* w=bit 3, x=0, y=1, z=2 */
640 for (unsigned i
= 0; i
< mask_length
; i
++) {
641 if (mask_str
[i
] < 'w' || mask_str
[i
] > 'z') {
642 ir_read_error(st
, list
, "write mask contains invalid character: %c",
646 mask
|= 1 << idx_map
[mask_str
[i
] - 'w'];
650 ir_dereference
*lhs
= read_dereference(st
, lhs_expr
);
652 ir_read_error(st
, NULL
, "when reading left-hand side of assignment");
656 ir_rvalue
*rhs
= read_rvalue(st
, rhs_expr
);
658 ir_read_error(st
, NULL
, "when reading right-hand side of assignment");
662 if (mask
== 0 && (lhs
->type
->is_vector() || lhs
->type
->is_scalar())) {
663 ir_read_error(st
, list
, "non-zero write mask required.");
667 return new(ctx
) ir_assignment(lhs
, rhs
, condition
, mask
);
671 read_call(_mesa_glsl_parse_state
*st
, s_list
*list
)
674 if (list
->length() != 3) {
675 ir_read_error(st
, list
, "expected (call <name> (<param> ...))");
679 s_symbol
*name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
680 s_list
*params
= SX_AS_LIST(list
->subexpressions
.head
->next
->next
);
681 if (name
== NULL
|| params
== NULL
) {
682 ir_read_error(st
, list
, "expected (call <name> (<param> ...))");
686 exec_list parameters
;
688 foreach_iter(exec_list_iterator
, it
, params
->subexpressions
) {
689 s_expression
*expr
= (s_expression
*) it
.get();
690 ir_rvalue
*param
= read_rvalue(st
, expr
);
692 ir_read_error(st
, list
, "when reading parameter to function call");
695 parameters
.push_tail(param
);
698 ir_function
*f
= st
->symbols
->get_function(name
->value());
700 ir_read_error(st
, list
, "found call to undefined function %s",
705 ir_function_signature
*callee
= f
->matching_signature(¶meters
);
706 if (callee
== NULL
) {
707 ir_read_error(st
, list
, "couldn't find matching signature for function "
708 "%s", name
->value());
712 return new(ctx
) ir_call(callee
, ¶meters
);
715 static ir_expression
*
716 read_expression(_mesa_glsl_parse_state
*st
, s_list
*list
)
719 const unsigned list_length
= list
->length();
720 if (list_length
< 4) {
721 ir_read_error(st
, list
, "expected (expression <type> <operator> "
722 "<operand> [<operand>])");
726 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
727 const glsl_type
*type
= read_type(st
, type_expr
);
731 /* Read the operator */
732 s_symbol
*op_sym
= SX_AS_SYMBOL(type_expr
->next
);
733 if (op_sym
== NULL
) {
734 ir_read_error(st
, list
, "expected operator, found non-symbol");
738 ir_expression_operation op
= ir_expression::get_operator(op_sym
->value());
739 if (op
== (ir_expression_operation
) -1) {
740 ir_read_error(st
, list
, "invalid operator: %s", op_sym
->value());
744 /* Now that we know the operator, check for the right number of operands */
745 if (ir_expression::get_num_operands(op
) == 2) {
746 if (list_length
!= 5) {
747 ir_read_error(st
, list
, "expected (expression <type> %s <operand> "
748 " <operand>)", op_sym
->value());
752 if (list_length
!= 4) {
753 ir_read_error(st
, list
, "expected (expression <type> %s <operand>)",
759 s_expression
*exp1
= (s_expression
*) (op_sym
->next
);
760 ir_rvalue
*arg1
= read_rvalue(st
, exp1
);
762 ir_read_error(st
, NULL
, "when reading first operand of %s",
767 ir_rvalue
*arg2
= NULL
;
768 if (ir_expression::get_num_operands(op
) == 2) {
769 s_expression
*exp2
= (s_expression
*) (exp1
->next
);
770 arg2
= read_rvalue(st
, exp2
);
772 ir_read_error(st
, NULL
, "when reading second operand of %s",
778 return new(ctx
) ir_expression(op
, type
, arg1
, arg2
);
782 read_swizzle(_mesa_glsl_parse_state
*st
, s_list
*list
)
784 if (list
->length() != 3) {
785 ir_read_error(st
, list
, "expected (swiz <swizzle> <rvalue>)");
789 s_symbol
*swiz
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
791 ir_read_error(st
, list
, "expected a valid swizzle; found non-symbol");
795 if (strlen(swiz
->value()) > 4) {
796 ir_read_error(st
, list
, "expected a valid swizzle; found %s",
801 s_expression
*sub
= (s_expression
*) swiz
->next
;
802 ir_rvalue
*rvalue
= read_rvalue(st
, sub
);
806 ir_swizzle
*ir
= ir_swizzle::create(rvalue
, swiz
->value(),
807 rvalue
->type
->vector_elements
);
809 ir_read_error(st
, list
, "invalid swizzle");
815 read_constant(_mesa_glsl_parse_state
*st
, s_list
*list
)
818 if (list
->length() != 3) {
819 ir_read_error(st
, list
, "expected (constant <type> (...))");
823 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
824 const glsl_type
*type
= read_type(st
, type_expr
);
828 s_list
*values
= SX_AS_LIST(type_expr
->next
);
829 if (values
== NULL
) {
830 ir_read_error(st
, list
, "expected (constant <type> (...))");
834 if (type
->is_array()) {
835 const unsigned elements_supplied
= values
->length();
836 if (elements_supplied
!= type
->length
) {
837 ir_read_error(st
, values
, "expected exactly %u array elements, "
838 "given %u", type
->length
, elements_supplied
);
843 foreach_iter(exec_list_iterator
, it
, values
->subexpressions
) {
844 s_expression
*expr
= (s_expression
*) it
.get();
845 s_list
*elt
= SX_AS_LIST(expr
);
847 ir_read_error(st
, expr
, "expected (constant ...) array element");
851 ir_constant
*ir_elt
= read_constant(st
, elt
);
854 elements
.push_tail(ir_elt
);
856 return new(ctx
) ir_constant(type
, &elements
);
859 const glsl_type
*const base_type
= type
->get_base_type();
861 ir_constant_data data
= { { 0 } };
863 // Read in list of values (at most 16).
865 foreach_iter(exec_list_iterator
, it
, values
->subexpressions
) {
867 ir_read_error(st
, values
, "expected at most 16 numbers");
871 s_expression
*expr
= (s_expression
*) it
.get();
873 if (base_type
->base_type
== GLSL_TYPE_FLOAT
) {
874 s_number
*value
= SX_AS_NUMBER(expr
);
876 ir_read_error(st
, values
, "expected numbers");
879 data
.f
[k
] = value
->fvalue();
881 s_int
*value
= SX_AS_INT(expr
);
883 ir_read_error(st
, values
, "expected integers");
887 switch (base_type
->base_type
) {
888 case GLSL_TYPE_UINT
: {
889 data
.u
[k
] = value
->value();
892 case GLSL_TYPE_INT
: {
893 data
.i
[k
] = value
->value();
896 case GLSL_TYPE_BOOL
: {
897 data
.b
[k
] = value
->value();
901 ir_read_error(st
, values
, "unsupported constant type");
908 return new(ctx
) ir_constant(type
, &data
);
911 static ir_dereference
*
912 read_dereference(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
914 s_list
*list
= SX_AS_LIST(expr
);
915 if (list
== NULL
|| list
->subexpressions
.is_empty())
918 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.head
);
921 if (strcmp(tag
->value(), "var_ref") == 0)
922 return read_var_ref(st
, list
);
923 if (strcmp(tag
->value(), "array_ref") == 0)
924 return read_array_ref(st
, list
);
925 if (strcmp(tag
->value(), "record_ref") == 0)
926 return read_record_ref(st
, list
);
930 static ir_dereference_variable
*
931 read_var_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
934 if (list
->length() != 2) {
935 ir_read_error(st
, list
, "expected (var_ref <variable name>)");
938 s_symbol
*var_name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
939 if (var_name
== NULL
) {
940 ir_read_error(st
, list
, "expected (var_ref <variable name>)");
944 ir_variable
*var
= st
->symbols
->get_variable(var_name
->value());
946 ir_read_error(st
, list
, "undeclared variable: %s", var_name
->value());
950 return new(ctx
) ir_dereference_variable(var
);
953 static ir_dereference_array
*
954 read_array_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
957 if (list
->length() != 3) {
958 ir_read_error(st
, list
, "expected (array_ref <rvalue> <index>)");
962 s_expression
*subj_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
963 ir_rvalue
*subject
= read_rvalue(st
, subj_expr
);
964 if (subject
== NULL
) {
965 ir_read_error(st
, NULL
, "when reading the subject of an array_ref");
969 s_expression
*idx_expr
= (s_expression
*) subj_expr
->next
;
970 ir_rvalue
*idx
= read_rvalue(st
, idx_expr
);
971 return new(ctx
) ir_dereference_array(subject
, idx
);
974 static ir_dereference_record
*
975 read_record_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
978 if (list
->length() != 3) {
979 ir_read_error(st
, list
, "expected (record_ref <rvalue> <field>)");
983 s_expression
*subj_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
984 ir_rvalue
*subject
= read_rvalue(st
, subj_expr
);
985 if (subject
== NULL
) {
986 ir_read_error(st
, NULL
, "when reading the subject of a record_ref");
990 s_symbol
*field
= SX_AS_SYMBOL(subj_expr
->next
);
992 ir_read_error(st
, list
, "expected (record_ref ... <field name>)");
995 return new(ctx
) ir_dereference_record(subject
, field
->value());
999 valid_texture_list_length(ir_texture_opcode op
, s_list
*list
)
1001 unsigned required_length
= 7;
1003 required_length
= 5;
1004 else if (op
== ir_tex
)
1005 required_length
= 6;
1007 return list
->length() == required_length
;
1011 read_texture(_mesa_glsl_parse_state
*st
, s_list
*list
)
1014 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.head
);
1015 assert(tag
!= NULL
);
1017 ir_texture_opcode op
= ir_texture::get_opcode(tag
->value());
1018 if (op
== (ir_texture_opcode
) -1)
1021 if (!valid_texture_list_length(op
, list
)) {
1022 ir_read_error(st
, NULL
, "invalid list size in (%s ...)", tag
->value());
1026 ir_texture
*tex
= new(ctx
) ir_texture(op
);
1028 // Read sampler (must be a deref)
1029 s_expression
*sampler_expr
= (s_expression
*) tag
->next
;
1030 ir_dereference
*sampler
= read_dereference(st
, sampler_expr
);
1031 if (sampler
== NULL
) {
1032 ir_read_error(st
, NULL
, "when reading sampler in (%s ...)", tag
->value());
1035 tex
->set_sampler(sampler
);
1037 // Read coordinate (any rvalue)
1038 s_expression
*coordinate_expr
= (s_expression
*) sampler_expr
->next
;
1039 tex
->coordinate
= read_rvalue(st
, coordinate_expr
);
1040 if (tex
->coordinate
== NULL
) {
1041 ir_read_error(st
, NULL
, "when reading coordinate in (%s ...)",
1046 // Read texel offset, i.e. (0 0 0)
1047 s_list
*offset_list
= SX_AS_LIST(coordinate_expr
->next
);
1048 if (offset_list
== NULL
|| offset_list
->length() != 3) {
1049 ir_read_error(st
, offset_list
, "expected (<int> <int> <int>)");
1052 s_int
*offset_x
= SX_AS_INT(offset_list
->subexpressions
.head
);
1053 s_int
*offset_y
= SX_AS_INT(offset_list
->subexpressions
.head
->next
);
1054 s_int
*offset_z
= SX_AS_INT(offset_list
->subexpressions
.head
->next
->next
);
1055 if (offset_x
== NULL
|| offset_y
== NULL
|| offset_z
== NULL
) {
1056 ir_read_error(st
, offset_list
, "expected (<int> <int> <int>)");
1059 tex
->offsets
[0] = offset_x
->value();
1060 tex
->offsets
[1] = offset_y
->value();
1061 tex
->offsets
[2] = offset_z
->value();
1064 s_expression
*lod_expr
= (s_expression
*) offset_list
->next
;
1065 tex
->lod_info
.lod
= read_rvalue(st
, lod_expr
);
1066 if (tex
->lod_info
.lod
== NULL
) {
1067 ir_read_error(st
, NULL
, "when reading LOD in (txf ...)");
1071 s_expression
*proj_expr
= (s_expression
*) offset_list
->next
;
1072 s_int
*proj_as_int
= SX_AS_INT(proj_expr
);
1073 if (proj_as_int
&& proj_as_int
->value() == 1) {
1074 tex
->projector
= NULL
;
1076 tex
->projector
= read_rvalue(st
, proj_expr
);
1077 if (tex
->projector
== NULL
) {
1078 ir_read_error(st
, NULL
, "when reading projective divide in (%s ..)",
1084 s_list
*shadow_list
= SX_AS_LIST(proj_expr
->next
);
1085 if (shadow_list
== NULL
) {
1086 ir_read_error(st
, NULL
, "shadow comparitor must be a list");
1089 if (shadow_list
->subexpressions
.is_empty()) {
1090 tex
->shadow_comparitor
= NULL
;
1092 tex
->shadow_comparitor
= read_rvalue(st
, shadow_list
);
1093 if (tex
->shadow_comparitor
== NULL
) {
1094 ir_read_error(st
, NULL
, "when reading shadow comparitor in (%s ..)",
1099 s_expression
*lod_expr
= (s_expression
*) shadow_list
->next
;
1103 tex
->lod_info
.bias
= read_rvalue(st
, lod_expr
);
1104 if (tex
->lod_info
.bias
== NULL
) {
1105 ir_read_error(st
, NULL
, "when reading LOD bias in (txb ...)");
1110 tex
->lod_info
.lod
= read_rvalue(st
, lod_expr
);
1111 if (tex
->lod_info
.lod
== NULL
) {
1112 ir_read_error(st
, NULL
, "when reading LOD in (txl ...)");
1117 s_list
*lod_list
= SX_AS_LIST(lod_expr
);
1118 if (lod_list
->length() != 2) {
1119 ir_read_error(st
, lod_expr
, "expected (dPdx dPdy) in (txd ...)");
1122 s_expression
*dx_expr
= (s_expression
*) lod_list
->subexpressions
.head
;
1123 s_expression
*dy_expr
= (s_expression
*) dx_expr
->next
;
1125 tex
->lod_info
.grad
.dPdx
= read_rvalue(st
, dx_expr
);
1126 if (tex
->lod_info
.grad
.dPdx
== NULL
) {
1127 ir_read_error(st
, NULL
, "when reading dPdx in (txd ...)");
1130 tex
->lod_info
.grad
.dPdy
= read_rvalue(st
, dy_expr
);
1131 if (tex
->lod_info
.grad
.dPdy
== NULL
) {
1132 ir_read_error(st
, NULL
, "when reading dPdy in (txd ...)");
1138 // tex doesn't have any extra parameters and txf was handled earlier.