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.
25 #include "ir_reader.h"
26 #include "glsl_parser_extras.h"
27 #include "glsl_types.h"
28 #include "s_expression.h"
30 static void ir_read_error(s_expression
*expr
, const char *fmt
, ...);
31 static const glsl_type
*read_type(_mesa_glsl_parse_state
*, s_expression
*);
33 static void read_instructions(_mesa_glsl_parse_state
*, exec_list
*,
34 s_expression
*, ir_loop
*);
35 static ir_instruction
*read_instruction(_mesa_glsl_parse_state
*,
36 s_expression
*, ir_loop
*);
37 static ir_variable
*read_declaration(_mesa_glsl_parse_state
*, s_list
*);
38 static ir_if
*read_if(_mesa_glsl_parse_state
*, s_list
*, ir_loop
*);
39 static ir_loop
*read_loop(_mesa_glsl_parse_state
*st
, s_list
*list
);
40 static ir_return
*read_return(_mesa_glsl_parse_state
*, s_list
*);
42 static ir_rvalue
*read_rvalue(_mesa_glsl_parse_state
*, s_expression
*);
43 static ir_assignment
*read_assignment(_mesa_glsl_parse_state
*, s_list
*);
44 static ir_expression
*read_expression(_mesa_glsl_parse_state
*, s_list
*);
45 static ir_swizzle
*read_swizzle(_mesa_glsl_parse_state
*, s_list
*);
46 static ir_constant
*read_constant(_mesa_glsl_parse_state
*, s_list
*);
47 static ir_dereference
*read_var_ref(_mesa_glsl_parse_state
*, s_list
*);
48 static ir_dereference
*read_array_ref(_mesa_glsl_parse_state
*, s_list
*);
49 static ir_dereference
*read_record_ref(_mesa_glsl_parse_state
*, s_list
*);
52 _mesa_glsl_read_ir(_mesa_glsl_parse_state
*state
, exec_list
*instructions
,
55 s_expression
*expr
= s_expression::read_expression(src
);
57 ir_read_error(NULL
, "couldn't parse S-Expression.");
61 printf("S-Expression:\n");
63 printf("\n-------------\n");
65 _mesa_glsl_initialize_types(state
);
66 _mesa_glsl_initialize_variables(instructions
, state
);
67 _mesa_glsl_initialize_constructors(instructions
, state
);
68 _mesa_glsl_initialize_functions(instructions
, state
);
70 read_instructions(state
, instructions
, expr
, NULL
);
74 ir_read_error(s_expression
*expr
, const char *fmt
, ...)
80 // FIXME: state->error = true;
82 len
= snprintf(buf
, sizeof(buf
), "error: ");
85 vsnprintf(buf
+ len
, sizeof(buf
) - len
, fmt
, ap
);
91 static const glsl_type
*
92 read_type(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
94 s_list
*list
= SX_AS_LIST(expr
);
96 s_symbol
*type_sym
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
97 if (type_sym
== NULL
) {
98 ir_read_error(expr
, "expected type (array ...) or (struct ...)");
101 if (strcmp(type_sym
->value(), "array") == 0) {
102 if (list
->length() != 3) {
103 ir_read_error(expr
, "expected type (array <type> <int>)");
108 s_expression
*base_expr
= (s_expression
*) type_sym
->next
;
109 const glsl_type
*base_type
= read_type(st
, base_expr
);
110 if (base_type
== NULL
) {
111 ir_read_error(expr
, "when reading base type of array");
116 s_int
*size
= SX_AS_INT(base_expr
->next
);
118 ir_read_error(expr
, "found non-integer array size");
122 return glsl_type::get_array_instance(base_type
, size
->value());
123 } else if (strcmp(type_sym
->value(), "struct") == 0) {
124 assert(false); // FINISHME
126 ir_read_error(expr
, "expected (array ...) or (struct ...); found (%s ...)", type_sym
->value());
131 s_symbol
*type_sym
= SX_AS_SYMBOL(expr
);
132 if (type_sym
== NULL
) {
133 ir_read_error(expr
, "expected <type> (symbol or list)");
137 const glsl_type
*type
= st
->symbols
->get_type(type_sym
->value());
139 ir_read_error(expr
, "invalid type: %s", type_sym
->value());
146 read_instructions(_mesa_glsl_parse_state
*st
, exec_list
*instructions
,
147 s_expression
*expr
, ir_loop
*loop_ctx
)
149 // Read in a list of instructions
150 s_list
*list
= SX_AS_LIST(expr
);
152 ir_read_error(expr
, "Expected (<instruction> ...); found an atom.");
157 foreach_iter(exec_list_iterator
, it
, list
->subexpressions
) {
158 s_expression
*sub
= (s_expression
*) it
.get();
159 ir_instruction
*ir
= read_instruction(st
, sub
, loop_ctx
);
161 ir_read_error(sub
, "Invalid instruction.\n");
165 instructions
->push_tail(ir
);
170 static ir_instruction
*
171 read_instruction(_mesa_glsl_parse_state
*st
, s_expression
*expr
,
174 s_symbol
*symbol
= SX_AS_SYMBOL(expr
);
175 if (symbol
!= NULL
) {
176 if (strcmp(symbol
->value(), "break") == 0 && loop_ctx
!= NULL
)
177 return new ir_loop_jump(loop_ctx
, ir_loop_jump::jump_break
);
178 if (strcmp(symbol
->value(), "continue") == 0 && loop_ctx
!= NULL
)
179 return new ir_loop_jump(loop_ctx
, ir_loop_jump::jump_continue
);
182 s_list
*list
= SX_AS_LIST(expr
);
183 if (list
== NULL
|| list
->subexpressions
.is_empty())
186 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
188 ir_read_error(expr
, "expected instruction tag");
192 ir_instruction
*inst
= NULL
;
193 if (strcmp(tag
->value(), "declare") == 0) {
194 inst
= read_declaration(st
, list
);
195 } else if (strcmp(tag
->value(), "if") == 0) {
196 inst
= read_if(st
, list
, loop_ctx
);
197 } else if (strcmp(tag
->value(), "loop") == 0) {
198 inst
= read_loop(st
, list
);
199 } else if (strcmp(tag
->value(), "return") == 0) {
200 inst
= read_return(st
, list
);
202 inst
= read_rvalue(st
, list
);
204 ir_read_error(list
, "when reading instruction");
211 read_declaration(_mesa_glsl_parse_state
*st
, s_list
*list
)
213 if (list
->length() != 4) {
214 ir_read_error(list
, "expected (declare (<qualifiers>) <type> <name>)");
218 s_list
*quals
= SX_AS_LIST(list
->subexpressions
.head
->next
);
220 ir_read_error(list
, "expected a list of variable qualifiers");
224 s_expression
*type_expr
= (s_expression
*) quals
->next
;
225 const glsl_type
*type
= read_type(st
, type_expr
);
229 s_symbol
*var_name
= SX_AS_SYMBOL(type_expr
->next
);
230 if (var_name
== NULL
) {
231 ir_read_error(list
, "expected variable name, found non-symbol");
235 ir_variable
*var
= new ir_variable(type
, var_name
->value());
237 foreach_iter(exec_list_iterator
, it
, quals
->subexpressions
) {
238 s_symbol
*qualifier
= SX_AS_SYMBOL(it
.get());
239 if (qualifier
== NULL
) {
240 ir_read_error(list
, "qualifier list must contain only symbols");
245 // FINISHME: Check for duplicate/conflicting qualifiers.
246 if (strcmp(qualifier
->value(), "centroid") == 0) {
248 } else if (strcmp(qualifier
->value(), "invariant") == 0) {
250 } else if (strcmp(qualifier
->value(), "uniform") == 0) {
251 var
->mode
= ir_var_uniform
;
252 } else if (strcmp(qualifier
->value(), "auto") == 0) {
253 var
->mode
= ir_var_auto
;
254 } else if (strcmp(qualifier
->value(), "in") == 0) {
255 var
->mode
= ir_var_in
;
256 } else if (strcmp(qualifier
->value(), "out") == 0) {
257 var
->mode
= ir_var_out
;
258 } else if (strcmp(qualifier
->value(), "inout") == 0) {
259 var
->mode
= ir_var_inout
;
260 } else if (strcmp(qualifier
->value(), "smooth") == 0) {
261 var
->interpolation
= ir_var_smooth
;
262 } else if (strcmp(qualifier
->value(), "flat") == 0) {
263 var
->interpolation
= ir_var_flat
;
264 } else if (strcmp(qualifier
->value(), "noperspective") == 0) {
265 var
->interpolation
= ir_var_noperspective
;
267 ir_read_error(list
, "unknown qualifier: %s", qualifier
->value());
273 // Add the variable to the symbol table
274 st
->symbols
->add_variable(var_name
->value(), var
);
281 read_if(_mesa_glsl_parse_state
*st
, s_list
*list
, ir_loop
*loop_ctx
)
283 if (list
->length() != 4) {
284 ir_read_error(list
, "expected (if <condition> (<then> ...) "
289 s_expression
*cond_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
290 ir_rvalue
*condition
= read_rvalue(st
, cond_expr
);
291 if (condition
== NULL
) {
292 ir_read_error(list
, "when reading condition of (if ...)");
296 s_expression
*then_expr
= (s_expression
*) cond_expr
->next
;
297 s_expression
*else_expr
= (s_expression
*) then_expr
->next
;
299 ir_if
*iff
= new ir_if(condition
);
301 read_instructions(st
, &iff
->then_instructions
, then_expr
, loop_ctx
);
302 read_instructions(st
, &iff
->else_instructions
, else_expr
, loop_ctx
);
312 read_loop(_mesa_glsl_parse_state
*st
, s_list
*list
)
314 if (list
->length() != 6) {
315 ir_read_error(list
, "expected (loop <counter> <from> <to> <increment> "
320 s_expression
*count_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
321 s_expression
*from_expr
= (s_expression
*) count_expr
->next
;
322 s_expression
*to_expr
= (s_expression
*) from_expr
->next
;
323 s_expression
*inc_expr
= (s_expression
*) to_expr
->next
;
324 s_expression
*body_expr
= (s_expression
*) inc_expr
->next
;
326 // FINISHME: actually read the count/from/to fields.
328 ir_loop
*loop
= new ir_loop
;
329 read_instructions(st
, &loop
->body_instructions
, body_expr
, loop
);
339 read_return(_mesa_glsl_parse_state
*st
, s_list
*list
)
341 if (list
->length() != 2) {
342 ir_read_error(list
, "expected (return <rvalue>)");
346 s_expression
*expr
= (s_expression
*) list
->subexpressions
.head
->next
;
348 ir_rvalue
*retval
= read_rvalue(st
, expr
);
349 if (retval
== NULL
) {
350 ir_read_error(list
, "when reading return value");
354 return new ir_return(retval
);
359 read_rvalue(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
361 s_list
*list
= SX_AS_LIST(expr
);
362 if (list
== NULL
|| list
->subexpressions
.is_empty())
365 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.get_head());
367 ir_read_error(expr
, "expected rvalue tag");
371 ir_rvalue
*rvalue
= NULL
;
372 if (strcmp(tag
->value(), "swiz") == 0) {
373 rvalue
= read_swizzle(st
, list
);
374 } else if (strcmp(tag
->value(), "assign") == 0) {
375 rvalue
= read_assignment(st
, list
);
376 } else if (strcmp(tag
->value(), "expression") == 0) {
377 rvalue
= read_expression(st
, list
);
379 } else if (strcmp(tag
->value(), "constant") == 0) {
380 rvalue
= read_constant(st
, list
);
381 } else if (strcmp(tag
->value(), "var_ref") == 0) {
382 rvalue
= read_var_ref(st
, list
);
383 } else if (strcmp(tag
->value(), "array_ref") == 0) {
384 rvalue
= read_array_ref(st
, list
);
385 } else if (strcmp(tag
->value(), "record_ref") == 0) {
386 rvalue
= read_record_ref(st
, list
);
388 ir_read_error(expr
, "unrecognized rvalue tag: %s", tag
->value());
394 static ir_assignment
*
395 read_assignment(_mesa_glsl_parse_state
*st
, s_list
*list
)
397 if (list
->length() != 4) {
398 ir_read_error(list
, "expected (assign <condition> <lhs> <rhs>)");
402 s_expression
*cond_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
403 s_expression
*lhs_expr
= (s_expression
*) cond_expr
->next
;
404 s_expression
*rhs_expr
= (s_expression
*) lhs_expr
->next
;
406 // FINISHME: Deal with "true" condition
407 ir_rvalue
*condition
= read_rvalue(st
, cond_expr
);
408 if (condition
== NULL
) {
409 ir_read_error(list
, "when reading condition of assignment");
413 ir_rvalue
*lhs
= read_rvalue(st
, lhs_expr
);
415 ir_read_error(list
, "when reading left-hand side of assignment");
419 ir_rvalue
*rhs
= read_rvalue(st
, rhs_expr
);
421 ir_read_error(list
, "when reading right-hand side of assignment");
425 return new ir_assignment(lhs
, rhs
, condition
);
429 static ir_expression
*
430 read_expression(_mesa_glsl_parse_state
*st
, s_list
*list
)
432 const unsigned list_length
= list
->length();
433 if (list_length
< 4) {
434 ir_read_error(list
, "expected (expression <type> <operator> <operand> "
439 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
440 const glsl_type
*type
= read_type(st
, type_expr
);
444 /* Read the operator */
445 s_symbol
*op_sym
= SX_AS_SYMBOL(type_expr
->next
);
446 if (op_sym
== NULL
) {
447 ir_read_error(list
, "expected operator, found non-symbol");
451 ir_expression_operation op
= ir_expression::get_operator(op_sym
->value());
452 if (op
== (ir_expression_operation
) -1) {
453 ir_read_error(list
, "invalid operator: %s", op_sym
->value());
457 /* Now that we know the operator, check for the right number of operands */
458 if (ir_expression::get_num_operands(op
) == 2) {
459 if (list_length
!= 5) {
460 ir_read_error(list
, "expected (expression %s <operand1> <operand2>)",
465 if (list_length
!= 4) {
466 ir_read_error(list
, "expected (expression %s <operand>)",
472 s_expression
*exp1
= (s_expression
*) (op_sym
->next
);
473 ir_rvalue
*arg1
= read_rvalue(st
, exp1
);
475 ir_read_error(list
, "when reading first operand of %s", op_sym
->value());
479 ir_rvalue
*arg2
= NULL
;
480 if (ir_expression::get_num_operands(op
) == 2) {
481 s_expression
*exp2
= (s_expression
*) (exp1
->next
);
482 arg2
= read_rvalue(st
, exp2
);
484 ir_read_error(list
, "when reading second operand of %s",
490 return new ir_expression(op
, type
, arg1
, arg2
);
494 read_swizzle(_mesa_glsl_parse_state
*st
, s_list
*list
)
496 if (list
->length() != 3) {
497 ir_read_error(list
, "expected (swiz <swizzle> <rvalue>)");
501 s_symbol
*swiz
= SX_AS_SYMBOL(list
->subexpressions
.head
->next
);
503 ir_read_error(list
, "expected a valid swizzle; found non-symbol");
507 unsigned num_components
= strlen(swiz
->value());
508 if (num_components
> 4) {
509 ir_read_error(list
, "expected a valid swizzle; found %s", swiz
->value());
513 s_expression
*sub
= (s_expression
*) swiz
->next
;
515 ir_read_error(list
, "expected rvalue: (swizzle %s <rvalue>)", swiz
->value());
519 ir_rvalue
*rvalue
= read_rvalue(st
, sub
);
523 return ir_swizzle::create(rvalue
, swiz
->value(), num_components
);
527 read_constant(_mesa_glsl_parse_state
*st
, s_list
*list
)
529 if (list
->length() != 3) {
530 ir_read_error(list
, "expected (constant <type> (<num> ... <num>))");
534 s_expression
*type_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
535 const glsl_type
*type
= read_type(st
, type_expr
);
539 s_list
*values
= SX_AS_LIST(type_expr
->next
);
540 if (values
== NULL
) {
541 ir_read_error(list
, "expected (constant <type> (<num> ... <num>))");
545 const glsl_type
*const base_type
= type
->get_base_type();
552 // Read in list of values (at most 16).
554 foreach_iter(exec_list_iterator
, it
, values
->subexpressions
) {
556 ir_read_error(values
, "expected at most 16 numbers");
560 s_expression
*expr
= (s_expression
*) it
.get();
562 if (base_type
->base_type
== GLSL_TYPE_FLOAT
) {
563 s_number
*value
= SX_AS_NUMBER(expr
);
565 ir_read_error(values
, "expected numbers");
568 f
[k
] = value
->fvalue();
570 s_int
*value
= SX_AS_INT(expr
);
572 ir_read_error(values
, "expected integers");
576 switch (base_type
->base_type
) {
577 case GLSL_TYPE_UINT
: {
578 u
[k
] = value
->value();
581 case GLSL_TYPE_INT
: {
582 i
[k
] = value
->value();
585 case GLSL_TYPE_BOOL
: {
586 b
[k
] = value
->value();
590 ir_read_error(values
, "unsupported constant type");
596 switch (base_type
->base_type
) {
598 return new ir_constant(type
, u
);
600 return new ir_constant(type
, i
);
602 return new ir_constant(type
, b
);
603 case GLSL_TYPE_FLOAT
:
604 return new ir_constant(type
, f
);
606 return NULL
; // should not be reached
609 static ir_instruction
*
610 read_dereferencable(_mesa_glsl_parse_state
*st
, s_expression
*expr
)
612 // Read the subject of a dereference - either a variable name or a swizzle
613 s_symbol
*var_name
= SX_AS_SYMBOL(expr
);
614 if (var_name
!= NULL
) {
615 ir_variable
*var
= st
->symbols
->get_variable(var_name
->value());
617 ir_read_error(expr
, "undeclared variable: %s", var_name
->value());
621 // Hopefully a (swiz ...)
622 s_list
*list
= SX_AS_LIST(expr
);
623 if (list
!= NULL
&& !list
->subexpressions
.is_empty()) {
624 s_symbol
*tag
= SX_AS_SYMBOL(list
->subexpressions
.head
);
625 if (tag
!= NULL
&& strcmp(tag
->value(), "swiz") == 0)
626 return read_swizzle(st
, list
);
629 ir_read_error(expr
, "expected variable name or (swiz ...)");
633 static ir_dereference
*
634 read_var_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
636 if (list
->length() != 2) {
637 ir_read_error(list
, "expected (var_ref <variable name or (swiz)>)");
640 s_expression
*subj_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
641 ir_instruction
*subject
= read_dereferencable(st
, subj_expr
);
644 return new ir_dereference(subject
);
647 static ir_dereference
*
648 read_array_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
650 if (list
->length() != 3) {
651 ir_read_error(list
, "expected (array_ref <variable name or (swiz)> "
656 s_expression
*subj_expr
= (s_expression
*) list
->subexpressions
.head
->next
;
657 ir_instruction
*subject
= read_dereferencable(st
, subj_expr
);
661 s_expression
*idx_expr
= (s_expression
*) subj_expr
->next
;
662 ir_rvalue
*idx
= read_rvalue(st
, idx_expr
);
663 return new ir_dereference(subject
, idx
);
666 static ir_dereference
*
667 read_record_ref(_mesa_glsl_parse_state
*st
, s_list
*list
)
669 ir_read_error(list
, "FINISHME: record refs not yet supported.");