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(state
, 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
);
88 ir_read_error(_mesa_glsl_parse_state
*state
, s_expression
*expr
,
95 state
->info_log
= talloc_strdup_append(state
->info_log
, "error: ");
98 state
->info_log
= talloc_vasprintf_append(state
->info_log
, fmt
, ap
);
100 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n");
103 state
->info_log
= talloc_strdup_append(state
->info_log
,
104 "...in this context:\n ");
106 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n\n");
110 static const glsl_type
*
111 read_type(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
113 s_list
*list
= SX_AS_LIST(expr
);
115 s_symbol
*type_sym
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
116 if (type_sym
== NULL
) {
117 ir_read_error(st
, expr
, "expected type (array ...) or (struct ...)");
120 if (strcmp(type_sym
->value(), "array") == 0) {
121 if (list
->length() != 3) {
122 ir_read_error(st
, expr
, "expected type (array <type> <int>)");
127 s_expression
*base_expr
= (s_expression
*) type_sym
->next
;
128 const glsl_type
*base_type
= read_type(st
, base_expr
);
129 if (base_type
== NULL
) {
130 ir_read_error(st
, NULL
, "when reading base type of array");
135 s_int
*size
= SX_AS_INT(base_expr
->next
);
137 ir_read_error(st
, expr
, "found non-integer array size");
141 return glsl_type::get_array_instance(st
, base_type
, size
->value());
142 } else if (strcmp(type_sym
->value(), "struct") == 0) {
143 assert(false); // FINISHME
145 ir_read_error(st
, expr
, "expected (array ...) or (struct ...); "
146 "found (%s ...)", type_sym
->value());
151 s_symbol
*type_sym
= SX_AS_SYMBOL(expr
);
152 if (type_sym
== NULL
) {
153 ir_read_error(st
, expr
, "expected <type> (symbol or list)");
157 const glsl_type
*type
= st
->symbols
->get_type(type_sym
->value());
159 ir_read_error(st
, expr
, "invalid type: %s", type_sym
->value());
166 scan_for_prototypes(_mesa_glsl_parse_state
*st
, exec_list
*instructions
,
169 s_list
*list
= SX_AS_LIST(expr
);
171 ir_read_error(st
, expr
, "Expected (<instruction> ...); found an atom.");
175 foreach_iter(exec_list_iterator
, it
, list
->subexpressions
) {
176 s_list
*sub
= SX_AS_LIST(it
.get());
178 continue; // not a (function ...); ignore it.
180 s_symbol
*tag
= SX_AS_SYMBOL(sub
->subexpressions
.get_head());
181 if (tag
== NULL
|| strcmp(tag
->value(), "function") != 0)
182 continue; // not a (function ...); ignore it.
184 ir_function
*f
= read_function(st
, sub
, true);
187 instructions
->push_tail(f
);
192 read_function(_mesa_glsl_parse_state
*st
, s_list
*list
, bool skip_body
)
194 void *ctx
= talloc_parent(st
);
195 if (list
->length() < 3) {
196 ir_read_error(st
, list
, "Expected (function <name> (signature ...) ...)");
200 s_symbol
*name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
202 ir_read_error(st
, list
, "Expected (function <name> ...)");
206 ir_function
*f
= st
->symbols
->get_function(name
->value());
208 f
= new(ctx
) ir_function(name
->value());
209 bool added
= st
->symbols
->add_function(name
->value(), f
);
213 exec_list_iterator it
= list
->subexpressions
.iterator();
214 it
.next(); // skip "function" tag
215 it
.next(); // skip function name
216 for (/* nothing */; it
.has_next(); it
.next()) {
217 s_list
*siglist
= SX_AS_LIST(it
.get());
218 if (siglist
== NULL
) {
219 ir_read_error(st
, list
, "Expected (function (signature ...) ...)");
223 s_symbol
*tag
= SX_AS_SYMBOL(siglist
->subexpressions
.get_head());
224 if (tag
== NULL
|| strcmp(tag
->value(), "signature") != 0) {
225 ir_read_error(st
, siglist
, "Expected (signature ...)");
229 read_function_sig(st
, f
, siglist
, skip_body
);
235 read_function_sig(_mesa_glsl_parse_state
*st
, ir_function
*f
, s_list
*list
,
238 void *ctx
= talloc_parent(st
);
239 if (list
->length() != 4) {
240 ir_read_error(st
, list
, "Expected (signature <type> (parameters ...) "
241 "(<instruction> ...))");
245 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
246 const glsl_type
*return_type
= read_type(st
, type_expr
);
247 if (return_type
== NULL
)
250 s_list
*paramlist
= SX_AS_LIST(type_expr
->next
);
251 s_list
*body_list
= SX_AS_LIST(paramlist
->next
);
252 if (paramlist
== NULL
|| body_list
== NULL
) {
253 ir_read_error(st
, list
, "Expected (signature <type> (parameters ...) "
254 "(<instruction> ...))");
257 s_symbol
*paramtag
= SX_AS_SYMBOL(paramlist
->subexpressions
.get_head());
258 if (paramtag
== NULL
|| strcmp(paramtag
->value(), "parameters") != 0) {
259 ir_read_error(st
, paramlist
, "Expected (parameters ...)");
263 // Read the parameters list into a temporary place.
264 exec_list hir_parameters
;
265 st
->symbols
->push_scope();
267 exec_list_iterator it
= paramlist
->subexpressions
.iterator();
268 for (it
.next() /* skip "parameters" */; it
.has_next(); it
.next()) {
269 s_list
*decl
= SX_AS_LIST(it
.get());
270 ir_variable
*var
= read_declaration(st
, decl
);
274 hir_parameters
.push_tail(var
);
277 ir_function_signature
*sig
= f
->exact_matching_signature(&hir_parameters
);
279 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
280 if (badvar
!= NULL
) {
281 ir_read_error(st
, list
, "function `%s' parameter `%s' qualifiers "
282 "don't match prototype", f
->name
, badvar
);
286 if (sig
->return_type
!= return_type
) {
287 ir_read_error(st
, list
, "function `%s' return type doesn't "
288 "match prototype", f
->name
);
292 sig
= new(ctx
) ir_function_signature(return_type
);
293 f
->add_signature(sig
);
296 sig
->replace_parameters(&hir_parameters
);
299 if (sig
->is_defined
) {
300 ir_read_error(st
, list
, "function %s redefined", f
->name
);
303 read_instructions(st
, &sig
->body
, body_list
, NULL
);
304 sig
->is_defined
= true;
307 st
->symbols
->pop_scope();
311 read_instructions(_mesa_glsl_parse_state
*st
, exec_list
*instructions
,
312 s_expression
*expr
, ir_loop
*loop_ctx
)
314 // Read in a list of instructions
315 s_list
*list
= SX_AS_LIST(expr
);
317 ir_read_error(st
, expr
, "Expected (<instruction> ...); found an atom.");
321 foreach_iter(exec_list_iterator
, it
, list
->subexpressions
) {
322 s_expression
*sub
= (s_expression
*) it
.get();
323 ir_instruction
*ir
= read_instruction(st
, sub
, loop_ctx
);
325 ir_read_error(st
, sub
, "Invalid instruction.\n");
328 instructions
->push_tail(ir
);
333 static ir_instruction
*
334 read_instruction(_mesa_glsl_parse_state
*st
, s_expression
*expr
,
337 void *ctx
= talloc_parent(st
);
338 s_symbol
*symbol
= SX_AS_SYMBOL(expr
);
339 if (symbol
!= NULL
) {
340 if (strcmp(symbol
->value(), "break") == 0 && loop_ctx
!= NULL
)
341 return new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
342 if (strcmp(symbol
->value(), "continue") == 0 && loop_ctx
!= NULL
)
343 return new(ctx
) ir_loop_jump(ir_loop_jump::jump_continue
);
346 s_list
*list
= SX_AS_LIST(expr
);
347 if (list
== NULL
|| list
->subexpressions
.is_empty())
350 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
352 ir_read_error(st
, expr
, "expected instruction tag");
356 ir_instruction
*inst
= NULL
;
357 if (strcmp(tag
->value(), "declare") == 0) {
358 inst
= read_declaration(st
, list
);
359 } else if (strcmp(tag
->value(), "if") == 0) {
360 inst
= read_if(st
, list
, loop_ctx
);
361 } else if (strcmp(tag
->value(), "loop") == 0) {
362 inst
= read_loop(st
, list
);
363 } else if (strcmp(tag
->value(), "return") == 0) {
364 inst
= read_return(st
, list
);
365 } else if (strcmp(tag
->value(), "function") == 0) {
366 inst
= read_function(st
, list
, false);
368 inst
= read_rvalue(st
, list
);
370 ir_read_error(st
, NULL
, "when reading instruction");
377 read_declaration(_mesa_glsl_parse_state
*st
, s_list
*list
)
379 void *ctx
= talloc_parent(st
);
380 if (list
->length() != 4) {
381 ir_read_error(st
, list
, "expected (declare (<qualifiers>) <type> "
386 s_list
*quals
= SX_AS_LIST(list
->subexpressions
.head
->next
);
388 ir_read_error(st
, list
, "expected a list of variable qualifiers");
392 s_expression
*type_expr
= (s_expression
*) quals
->next
;
393 const glsl_type
*type
= read_type(st
, type_expr
);
397 s_symbol
*var_name
= SX_AS_SYMBOL(type_expr
->next
);
398 if (var_name
== NULL
) {
399 ir_read_error(st
, list
, "expected variable name, found non-symbol");
403 ir_variable
*var
= new(ctx
) ir_variable(type
, var_name
->value());
405 foreach_iter(exec_list_iterator
, it
, quals
->subexpressions
) {
406 s_symbol
*qualifier
= SX_AS_SYMBOL(it
.get());
407 if (qualifier
== NULL
) {
408 ir_read_error(st
, list
, "qualifier list must contain only symbols");
413 // FINISHME: Check for duplicate/conflicting qualifiers.
414 if (strcmp(qualifier
->value(), "centroid") == 0) {
416 } else if (strcmp(qualifier
->value(), "invariant") == 0) {
418 } else if (strcmp(qualifier
->value(), "uniform") == 0) {
419 var
->mode
= ir_var_uniform
;
420 } else if (strcmp(qualifier
->value(), "auto") == 0) {
421 var
->mode
= ir_var_auto
;
422 } else if (strcmp(qualifier
->value(), "in") == 0) {
423 var
->mode
= ir_var_in
;
424 } else if (strcmp(qualifier
->value(), "out") == 0) {
425 var
->mode
= ir_var_out
;
426 } else if (strcmp(qualifier
->value(), "inout") == 0) {
427 var
->mode
= ir_var_inout
;
428 } else if (strcmp(qualifier
->value(), "smooth") == 0) {
429 var
->interpolation
= ir_var_smooth
;
430 } else if (strcmp(qualifier
->value(), "flat") == 0) {
431 var
->interpolation
= ir_var_flat
;
432 } else if (strcmp(qualifier
->value(), "noperspective") == 0) {
433 var
->interpolation
= ir_var_noperspective
;
435 ir_read_error(st
, list
, "unknown qualifier: %s", qualifier
->value());
441 // Add the variable to the symbol table
442 st
->symbols
->add_variable(var_name
->value(), var
);
449 read_if(_mesa_glsl_parse_state
*st
, s_list
*list
, ir_loop
*loop_ctx
)
451 void *ctx
= talloc_parent(st
);
452 if (list
->length() != 4) {
453 ir_read_error(st
, list
, "expected (if <condition> (<then> ...) "
458 s_expression
*cond_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
459 ir_rvalue
*condition
= read_rvalue(st
, cond_expr
);
460 if (condition
== NULL
) {
461 ir_read_error(st
, NULL
, "when reading condition of (if ...)");
465 s_expression
*then_expr
= (s_expression
*) cond_expr
->next
;
466 s_expression
*else_expr
= (s_expression
*) then_expr
->next
;
468 ir_if
*iff
= new(ctx
) ir_if(condition
);
470 read_instructions(st
, &iff
->then_instructions
, then_expr
, loop_ctx
);
471 read_instructions(st
, &iff
->else_instructions
, else_expr
, loop_ctx
);
481 read_loop(_mesa_glsl_parse_state
*st
, s_list
*list
)
483 void *ctx
= talloc_parent(st
);
484 if (list
->length() != 6) {
485 ir_read_error(st
, list
, "expected (loop <counter> <from> <to> "
486 "<increment> <body>)");
490 s_expression
*count_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
491 s_expression
*from_expr
= (s_expression
*) count_expr
->next
;
492 s_expression
*to_expr
= (s_expression
*) from_expr
->next
;
493 s_expression
*inc_expr
= (s_expression
*) to_expr
->next
;
494 s_expression
*body_expr
= (s_expression
*) inc_expr
->next
;
496 // FINISHME: actually read the count/from/to fields.
498 ir_loop
*loop
= new(ctx
) ir_loop
;
499 read_instructions(st
, &loop
->body_instructions
, body_expr
, loop
);
509 read_return(_mesa_glsl_parse_state
*st
, s_list
*list
)
511 void *ctx
= talloc_parent(st
);
512 if (list
->length() != 2) {
513 ir_read_error(st
, list
, "expected (return <rvalue>)");
517 s_expression
*expr
= (s_expression
*) list
->subexpressions
.head
->next
;
519 ir_rvalue
*retval
= read_rvalue(st
, expr
);
520 if (retval
== NULL
) {
521 ir_read_error(st
, NULL
, "when reading return value");
525 return new(ctx
) ir_return(retval
);
530 read_rvalue(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
532 s_list
*list
= SX_AS_LIST(expr
);
533 if (list
== NULL
|| list
->subexpressions
.is_empty())
536 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
538 ir_read_error(st
, expr
, "expected rvalue tag");
542 ir_rvalue
*rvalue
= read_dereference(st
, list
);
543 if (rvalue
!= NULL
|| st
->error
)
545 else if (strcmp(tag
->value(), "swiz") == 0) {
546 rvalue
= read_swizzle(st
, list
);
547 } else if (strcmp(tag
->value(), "assign") == 0) {
548 rvalue
= read_assignment(st
, list
);
549 } else if (strcmp(tag
->value(), "expression") == 0) {
550 rvalue
= read_expression(st
, list
);
551 } else if (strcmp(tag
->value(), "call") == 0) {
552 rvalue
= read_call(st
, list
);
553 } else if (strcmp(tag
->value(), "constant") == 0) {
554 rvalue
= read_constant(st
, list
);
556 rvalue
= read_texture(st
, list
);
557 if (rvalue
== NULL
&& !st
->error
)
558 ir_read_error(st
, expr
, "unrecognized rvalue tag: %s", tag
->value());
564 static ir_assignment
*
565 read_assignment(_mesa_glsl_parse_state
*st
, s_list
*list
)
567 void *ctx
= talloc_parent(st
);
568 if (list
->length() != 4) {
569 ir_read_error(st
, list
, "expected (assign <condition> <lhs> <rhs>)");
573 s_expression
*cond_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
574 s_expression
*lhs_expr
= (s_expression
*) cond_expr
->next
;
575 s_expression
*rhs_expr
= (s_expression
*) lhs_expr
->next
;
577 // FINISHME: Deal with "true" condition
578 ir_rvalue
*condition
= read_rvalue(st
, cond_expr
);
579 if (condition
== NULL
) {
580 ir_read_error(st
, NULL
, "when reading condition of assignment");
584 ir_rvalue
*lhs
= read_rvalue(st
, lhs_expr
);
586 ir_read_error(st
, NULL
, "when reading left-hand side of assignment");
590 ir_rvalue
*rhs
= read_rvalue(st
, rhs_expr
);
592 ir_read_error(st
, NULL
, "when reading right-hand side of assignment");
596 return new(ctx
) ir_assignment(lhs
, rhs
, condition
);
600 read_call(_mesa_glsl_parse_state
*st
, s_list
*list
)
602 void *ctx
= talloc_parent(st
);
603 if (list
->length() != 3) {
604 ir_read_error(st
, list
, "expected (call <name> (<param> ...))");
608 s_symbol
*name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
609 s_list
*params
= SX_AS_LIST(name
->next
);
610 if (name
== NULL
|| params
== NULL
) {
611 ir_read_error(st
, list
, "expected (call <name> (<param> ...))");
615 exec_list parameters
;
617 foreach_iter(exec_list_iterator
, it
, params
->subexpressions
) {
618 s_expression
*expr
= (s_expression
*) it
.get();
619 ir_rvalue
*param
= read_rvalue(st
, expr
);
621 ir_read_error(st
, list
, "when reading parameter to function call");
624 parameters
.push_tail(param
);
627 ir_function
*f
= st
->symbols
->get_function(name
->value());
629 ir_read_error(st
, list
, "found call to undefined function %s",
634 const ir_function_signature
*callee
= f
->matching_signature(¶meters
);
635 if (callee
== NULL
) {
636 ir_read_error(st
, list
, "couldn't find matching signature for function "
637 "%s", name
->value());
641 return new(ctx
) ir_call(callee
, ¶meters
);
644 static ir_expression
*
645 read_expression(_mesa_glsl_parse_state
*st
, s_list
*list
)
647 void *ctx
= talloc_parent(st
);
648 const unsigned list_length
= list
->length();
649 if (list_length
< 4) {
650 ir_read_error(st
, list
, "expected (expression <type> <operator> "
651 "<operand> [<operand>])");
655 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
656 const glsl_type
*type
= read_type(st
, type_expr
);
660 /* Read the operator */
661 s_symbol
*op_sym
= SX_AS_SYMBOL(type_expr
->next
);
662 if (op_sym
== NULL
) {
663 ir_read_error(st
, list
, "expected operator, found non-symbol");
667 ir_expression_operation op
= ir_expression::get_operator(op_sym
->value());
668 if (op
== (ir_expression_operation
) -1) {
669 ir_read_error(st
, list
, "invalid operator: %s", op_sym
->value());
673 /* Now that we know the operator, check for the right number of operands */
674 if (ir_expression::get_num_operands(op
) == 2) {
675 if (list_length
!= 5) {
676 ir_read_error(st
, list
, "expected (expression <type> %s <operand> "
677 " <operand>)", op_sym
->value());
681 if (list_length
!= 4) {
682 ir_read_error(st
, list
, "expected (expression <type> %s <operand>)",
688 s_expression
*exp1
= (s_expression
*) (op_sym
->next
);
689 ir_rvalue
*arg1
= read_rvalue(st
, exp1
);
691 ir_read_error(st
, NULL
, "when reading first operand of %s",
696 ir_rvalue
*arg2
= NULL
;
697 if (ir_expression::get_num_operands(op
) == 2) {
698 s_expression
*exp2
= (s_expression
*) (exp1
->next
);
699 arg2
= read_rvalue(st
, exp2
);
701 ir_read_error(st
, NULL
, "when reading second operand of %s",
707 return new(ctx
) ir_expression(op
, type
, arg1
, arg2
);
711 read_swizzle(_mesa_glsl_parse_state
*st
, s_list
*list
)
713 if (list
->length() != 3) {
714 ir_read_error(st
, list
, "expected (swiz <swizzle> <rvalue>)");
718 s_symbol
*swiz
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
720 ir_read_error(st
, list
, "expected a valid swizzle; found non-symbol");
724 if (strlen(swiz
->value()) > 4) {
725 ir_read_error(st
, list
, "expected a valid swizzle; found %s",
730 s_expression
*sub
= (s_expression
*) swiz
->next
;
732 ir_read_error(st
, list
, "expected rvalue: (swizzle %s <rvalue>)",
737 ir_rvalue
*rvalue
= read_rvalue(st
, sub
);
741 ir_swizzle
*ir
= ir_swizzle::create(rvalue
, swiz
->value(),
742 rvalue
->type
->vector_elements
);
744 ir_read_error(st
, list
, "invalid swizzle");
750 read_constant(_mesa_glsl_parse_state
*st
, s_list
*list
)
752 void *ctx
= talloc_parent(st
);
753 if (list
->length() != 3) {
754 ir_read_error(st
, list
, "expected (constant <type> (<num> ... <num>))");
758 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
759 const glsl_type
*type
= read_type(st
, type_expr
);
763 s_list
*values
= SX_AS_LIST(type_expr
->next
);
764 if (values
== NULL
) {
765 ir_read_error(st
, list
, "expected (constant <type> (<num> ... <num>))");
769 const glsl_type
*const base_type
= type
->get_base_type();
771 ir_constant_data data
;
773 // Read in list of values (at most 16).
775 foreach_iter(exec_list_iterator
, it
, values
->subexpressions
) {
777 ir_read_error(st
, values
, "expected at most 16 numbers");
781 s_expression
*expr
= (s_expression
*) it
.get();
783 if (base_type
->base_type
== GLSL_TYPE_FLOAT
) {
784 s_number
*value
= SX_AS_NUMBER(expr
);
786 ir_read_error(st
, values
, "expected numbers");
789 data
.f
[k
] = value
->fvalue();
791 s_int
*value
= SX_AS_INT(expr
);
793 ir_read_error(st
, values
, "expected integers");
797 switch (base_type
->base_type
) {
798 case GLSL_TYPE_UINT
: {
799 data
.u
[k
] = value
->value();
802 case GLSL_TYPE_INT
: {
803 data
.i
[k
] = value
->value();
806 case GLSL_TYPE_BOOL
: {
807 data
.b
[k
] = value
->value();
811 ir_read_error(st
, values
, "unsupported constant type");
818 return new(ctx
) ir_constant(type
, &data
);
821 static ir_dereference
*
822 read_dereference(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
824 s_list
*list
= SX_AS_LIST(expr
);
825 if (list
== NULL
|| list
->subexpressions
.is_empty())
828 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.head
);
831 if (strcmp(tag
->value(), "var_ref") == 0)
832 return read_var_ref(st
, list
);
833 if (strcmp(tag
->value(), "array_ref") == 0)
834 return read_array_ref(st
, list
);
835 if (strcmp(tag
->value(), "record_ref") == 0)
836 return read_record_ref(st
, list
);
840 static ir_dereference
*
841 read_var_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
843 void *ctx
= talloc_parent(st
);
844 if (list
->length() != 2) {
845 ir_read_error(st
, list
, "expected (var_ref <variable name>)");
848 s_symbol
*var_name
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
849 if (var_name
== NULL
) {
850 ir_read_error(st
, list
, "expected (var_ref <variable name>)");
854 ir_variable
*var
= st
->symbols
->get_variable(var_name
->value());
856 ir_read_error(st
, list
, "undeclared variable: %s", var_name
->value());
860 return new(ctx
) ir_dereference_variable(var
);
863 static ir_dereference
*
864 read_array_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
866 void *ctx
= talloc_parent(st
);
867 if (list
->length() != 3) {
868 ir_read_error(st
, list
, "expected (array_ref <rvalue> <index>)");
872 s_expression
*subj_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
873 ir_rvalue
*subject
= read_rvalue(st
, subj_expr
);
874 if (subject
== NULL
) {
875 ir_read_error(st
, NULL
, "when reading the subject of an array_ref");
879 s_expression
*idx_expr
= (s_expression
*) subj_expr
->next
;
880 ir_rvalue
*idx
= read_rvalue(st
, idx_expr
);
881 return new(ctx
) ir_dereference_array(subject
, idx
);
884 static ir_dereference
*
885 read_record_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
887 void *ctx
= talloc_parent(st
);
888 if (list
->length() != 3) {
889 ir_read_error(st
, list
, "expected (record_ref <rvalue> <field>)");
893 s_expression
*subj_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
894 ir_rvalue
*subject
= read_rvalue(st
, subj_expr
);
895 if (subject
== NULL
) {
896 ir_read_error(st
, NULL
, "when reading the subject of a record_ref");
900 s_symbol
*field
= SX_AS_SYMBOL(subj_expr
->next
);
902 ir_read_error(st
, list
, "expected (record_ref ... <field name>)");
905 return new(ctx
) ir_dereference_record(subject
, field
->value());
909 valid_texture_list_length(ir_texture_opcode op
, s_list
*list
)
911 unsigned required_length
= 7;
914 else if (op
== ir_tex
)
917 return list
->length() == required_length
;
921 read_texture(_mesa_glsl_parse_state
*st
, s_list
*list
)
923 void *ctx
= talloc_parent(st
);
924 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.head
);
927 ir_texture_opcode op
= ir_texture::get_opcode(tag
->value());
928 if (op
== (ir_texture_opcode
) -1)
931 if (!valid_texture_list_length(op
, list
)) {
932 ir_read_error(st
, NULL
, "invalid list size in (%s ...)", tag
->value());
936 ir_texture
*tex
= new(ctx
) ir_texture(op
);
938 // Read sampler (must be a deref)
939 s_expression
*sampler_expr
= (s_expression
*) tag
->next
;
940 ir_dereference
*sampler
= read_dereference(st
, sampler_expr
);
941 if (sampler
== NULL
) {
942 ir_read_error(st
, NULL
, "when reading sampler in (%s ...)", tag
->value());
945 tex
->set_sampler(sampler
);
947 // Read coordinate (any rvalue)
948 s_expression
*coordinate_expr
= (s_expression
*) sampler_expr
->next
;
949 tex
->coordinate
= read_rvalue(st
, coordinate_expr
);
950 if (tex
->coordinate
== NULL
) {
951 ir_read_error(st
, NULL
, "when reading coordinate in (%s ...)",
956 // Read texel offset, i.e. (0 0 0)
957 s_list
*offset_list
= SX_AS_LIST(coordinate_expr
->next
);
958 if (offset_list
== NULL
|| offset_list
->length() != 3) {
959 ir_read_error(st
, offset_list
, "expected (<int> <int> <int>)");
962 s_int
*offset_x
= SX_AS_INT(offset_list
->subexpressions
.head
);
963 s_int
*offset_y
= SX_AS_INT(offset_x
->next
);
964 s_int
*offset_z
= SX_AS_INT(offset_y
->next
);
965 if (offset_x
== NULL
|| offset_y
== NULL
|| offset_z
== NULL
) {
966 ir_read_error(st
, offset_list
, "expected (<int> <int> <int>)");
969 tex
->offsets
[0] = offset_x
->value();
970 tex
->offsets
[1] = offset_y
->value();
971 tex
->offsets
[2] = offset_z
->value();
974 s_expression
*lod_expr
= (s_expression
*) offset_list
->next
;
975 tex
->lod_info
.lod
= read_rvalue(st
, lod_expr
);
976 if (tex
->lod_info
.lod
== NULL
) {
977 ir_read_error(st
, NULL
, "when reading LOD in (txf ...)");
981 s_expression
*proj_expr
= (s_expression
*) offset_list
->next
;
982 s_int
*proj_as_int
= SX_AS_INT(proj_expr
);
983 if (proj_as_int
&& proj_as_int
->value() == 1) {
984 tex
->projector
= NULL
;
986 tex
->projector
= read_rvalue(st
, proj_expr
);
987 if (tex
->projector
== NULL
) {
988 ir_read_error(st
, NULL
, "when reading projective divide in (%s ..)",
994 s_list
*shadow_list
= SX_AS_LIST(proj_expr
->next
);
995 if (shadow_list
== NULL
) {
996 ir_read_error(st
, NULL
, "shadow comparitor must be a list");
999 if (shadow_list
->subexpressions
.is_empty()) {
1000 tex
->shadow_comparitor
= NULL
;
1002 tex
->shadow_comparitor
= read_rvalue(st
, shadow_list
);
1003 if (tex
->shadow_comparitor
== NULL
) {
1004 ir_read_error(st
, NULL
, "when reading shadow comparitor in (%s ..)",
1009 s_expression
*lod_expr
= (s_expression
*) shadow_list
->next
;
1013 tex
->lod_info
.bias
= read_rvalue(st
, lod_expr
);
1014 if (tex
->lod_info
.bias
== NULL
) {
1015 ir_read_error(st
, NULL
, "when reading LOD bias in (txb ...)");
1020 tex
->lod_info
.lod
= read_rvalue(st
, lod_expr
);
1021 if (tex
->lod_info
.lod
== NULL
) {
1022 ir_read_error(st
, NULL
, "when reading LOD in (txl ...)");
1027 s_list
*lod_list
= SX_AS_LIST(lod_expr
);
1028 if (lod_list
->length() != 2) {
1029 ir_read_error(st
, lod_expr
, "expected (dPdx dPdy) in (txd ...)");
1032 s_expression
*dx_expr
= (s_expression
*) lod_list
->subexpressions
.head
;
1033 s_expression
*dy_expr
= (s_expression
*) dx_expr
->next
;
1035 tex
->lod_info
.grad
.dPdx
= read_rvalue(st
, dx_expr
);
1036 if (tex
->lod_info
.grad
.dPdx
== NULL
) {
1037 ir_read_error(st
, NULL
, "when reading dPdx in (txd ...)");
1040 tex
->lod_info
.grad
.dPdy
= read_rvalue(st
, dy_expr
);
1041 if (tex
->lod_info
.grad
.dPdy
== NULL
) {
1042 ir_read_error(st
, NULL
, "when reading dPdy in (txd ...)");
1048 // tex doesn't have any extra parameters and txf was handled earlier.