ir_reader: Add support for "break" and "continue" in loops.
[mesa.git] / ir_reader.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23 #include <cstdio>
24 #include <cstdarg>
25 #include "ir_reader.h"
26 #include "glsl_parser_extras.h"
27 #include "glsl_types.h"
28 #include "s_expression.h"
29
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 *);
32
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 *);
41
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 *);
50
51 void
52 _mesa_glsl_read_ir(_mesa_glsl_parse_state *state, exec_list *instructions,
53 const char *src)
54 {
55 s_expression *expr = s_expression::read_expression(src);
56 if (expr == NULL) {
57 ir_read_error(NULL, "couldn't parse S-Expression.");
58 state->error = true;
59 return;
60 }
61 printf("S-Expression:\n");
62 expr->print();
63 printf("\n-------------\n");
64
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);
69
70 read_instructions(state, instructions, expr, NULL);
71 }
72
73 static void
74 ir_read_error(s_expression *expr, const char *fmt, ...)
75 {
76 char buf[1024];
77 int len;
78 va_list ap;
79
80 // FIXME: state->error = true;
81
82 len = snprintf(buf, sizeof(buf), "error: ");
83
84 va_start(ap, fmt);
85 vsnprintf(buf + len, sizeof(buf) - len, fmt, ap);
86 va_end(ap);
87
88 printf("%s\n", buf);
89 }
90
91 static const glsl_type *
92 read_type(_mesa_glsl_parse_state *st, s_expression *expr)
93 {
94 s_list *list = SX_AS_LIST(expr);
95 if (list != NULL) {
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 ...)");
99 return NULL;
100 }
101 if (strcmp(type_sym->value(), "array") == 0) {
102 if (list->length() != 3) {
103 ir_read_error(expr, "expected type (array <type> <int>)");
104 return NULL;
105 }
106
107 // Read base type
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");
112 return NULL;
113 }
114
115 // Read array size
116 s_int *size = SX_AS_INT(base_expr->next);
117 if (size == NULL) {
118 ir_read_error(expr, "found non-integer array size");
119 return NULL;
120 }
121
122 return glsl_type::get_array_instance(base_type, size->value());
123 } else if (strcmp(type_sym->value(), "struct") == 0) {
124 assert(false); // FINISHME
125 } else {
126 ir_read_error(expr, "expected (array ...) or (struct ...); found (%s ...)", type_sym->value());
127 return NULL;
128 }
129 }
130
131 s_symbol *type_sym = SX_AS_SYMBOL(expr);
132 if (type_sym == NULL) {
133 ir_read_error(expr, "expected <type> (symbol or list)");
134 return NULL;
135 }
136
137 const glsl_type *type = st->symbols->get_type(type_sym->value());
138 if (type == NULL)
139 ir_read_error(expr, "invalid type: %s", type_sym->value());
140
141 return type;
142 }
143
144
145 static void
146 read_instructions(_mesa_glsl_parse_state *st, exec_list *instructions,
147 s_expression *expr, ir_loop *loop_ctx)
148 {
149 // Read in a list of instructions
150 s_list *list = SX_AS_LIST(expr);
151 if (list == NULL) {
152 ir_read_error(expr, "Expected (<instruction> ...); found an atom.");
153 st->error = true;
154 return;
155 }
156
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);
160 if (ir == NULL) {
161 ir_read_error(sub, "Invalid instruction.\n");
162 st->error = true;
163 return;
164 }
165 instructions->push_tail(ir);
166 }
167 }
168
169
170 static ir_instruction *
171 read_instruction(_mesa_glsl_parse_state *st, s_expression *expr,
172 ir_loop *loop_ctx)
173 {
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);
180 }
181
182 s_list *list = SX_AS_LIST(expr);
183 if (list == NULL || list->subexpressions.is_empty())
184 return NULL;
185
186 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
187 if (tag == NULL) {
188 ir_read_error(expr, "expected instruction tag");
189 return NULL;
190 }
191
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);
201 } else {
202 inst = read_rvalue(st, list);
203 if (inst == NULL)
204 ir_read_error(list, "when reading instruction");
205 }
206 return inst;
207 }
208
209
210 static ir_variable *
211 read_declaration(_mesa_glsl_parse_state *st, s_list *list)
212 {
213 if (list->length() != 4) {
214 ir_read_error(list, "expected (declare (<qualifiers>) <type> <name>)");
215 return NULL;
216 }
217
218 s_list *quals = SX_AS_LIST(list->subexpressions.head->next);
219 if (quals == NULL) {
220 ir_read_error(list, "expected a list of variable qualifiers");
221 return NULL;
222 }
223
224 s_expression *type_expr = (s_expression*) quals->next;
225 const glsl_type *type = read_type(st, type_expr);
226 if (type == NULL)
227 return NULL;
228
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");
232 return NULL;
233 }
234
235 ir_variable *var = new ir_variable(type, var_name->value());
236
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");
241 delete var;
242 return NULL;
243 }
244
245 // FINISHME: Check for duplicate/conflicting qualifiers.
246 if (strcmp(qualifier->value(), "centroid") == 0) {
247 var->centroid = 1;
248 } else if (strcmp(qualifier->value(), "invariant") == 0) {
249 var->invariant = 1;
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;
266 } else {
267 ir_read_error(list, "unknown qualifier: %s", qualifier->value());
268 delete var;
269 return NULL;
270 }
271 }
272
273 // Add the variable to the symbol table
274 st->symbols->add_variable(var_name->value(), var);
275
276 return var;
277 }
278
279
280 static ir_if *
281 read_if(_mesa_glsl_parse_state *st, s_list *list, ir_loop *loop_ctx)
282 {
283 if (list->length() != 4) {
284 ir_read_error(list, "expected (if <condition> (<then> ...) "
285 "(<else> ...))");
286 return NULL;
287 }
288
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 ...)");
293 return NULL;
294 }
295
296 s_expression *then_expr = (s_expression*) cond_expr->next;
297 s_expression *else_expr = (s_expression*) then_expr->next;
298
299 ir_if *iff = new ir_if(condition);
300
301 read_instructions(st, &iff->then_instructions, then_expr, loop_ctx);
302 read_instructions(st, &iff->else_instructions, else_expr, loop_ctx);
303 if (st->error) {
304 delete iff;
305 iff = NULL;
306 }
307 return iff;
308 }
309
310
311 static ir_loop *
312 read_loop(_mesa_glsl_parse_state *st, s_list *list)
313 {
314 if (list->length() != 6) {
315 ir_read_error(list, "expected (loop <counter> <from> <to> <increment> "
316 "<body>)");
317 return NULL;
318 }
319
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;
325
326 // FINISHME: actually read the count/from/to fields.
327
328 ir_loop *loop = new ir_loop;
329 read_instructions(st, &loop->body_instructions, body_expr, loop);
330 if (st->error) {
331 delete loop;
332 loop = NULL;
333 }
334 return loop;
335 }
336
337
338 static ir_return *
339 read_return(_mesa_glsl_parse_state *st, s_list *list)
340 {
341 if (list->length() != 2) {
342 ir_read_error(list, "expected (return <rvalue>)");
343 return NULL;
344 }
345
346 s_expression *expr = (s_expression*) list->subexpressions.head->next;
347
348 ir_rvalue *retval = read_rvalue(st, expr);
349 if (retval == NULL) {
350 ir_read_error(list, "when reading return value");
351 return NULL;
352 }
353
354 return new ir_return(retval);
355 }
356
357
358 static ir_rvalue *
359 read_rvalue(_mesa_glsl_parse_state *st, s_expression *expr)
360 {
361 s_list *list = SX_AS_LIST(expr);
362 if (list == NULL || list->subexpressions.is_empty())
363 return NULL;
364
365 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
366 if (tag == NULL) {
367 ir_read_error(expr, "expected rvalue tag");
368 return NULL;
369 }
370
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);
378 // FINISHME: ir_call
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);
387 } else {
388 ir_read_error(expr, "unrecognized rvalue tag: %s", tag->value());
389 }
390
391 return rvalue;
392 }
393
394 static ir_assignment *
395 read_assignment(_mesa_glsl_parse_state *st, s_list *list)
396 {
397 if (list->length() != 4) {
398 ir_read_error(list, "expected (assign <condition> <lhs> <rhs>)");
399 return NULL;
400 }
401
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;
405
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");
410 return NULL;
411 }
412
413 ir_rvalue *lhs = read_rvalue(st, lhs_expr);
414 if (lhs == NULL) {
415 ir_read_error(list, "when reading left-hand side of assignment");
416 return NULL;
417 }
418
419 ir_rvalue *rhs = read_rvalue(st, rhs_expr);
420 if (rhs == NULL) {
421 ir_read_error(list, "when reading right-hand side of assignment");
422 return NULL;
423 }
424
425 return new ir_assignment(lhs, rhs, condition);
426 }
427
428
429 static ir_expression *
430 read_expression(_mesa_glsl_parse_state *st, s_list *list)
431 {
432 const unsigned list_length = list->length();
433 if (list_length < 4) {
434 ir_read_error(list, "expected (expression <type> <operator> <operand> "
435 "[<operand>])");
436 return NULL;
437 }
438
439 s_expression *type_expr = (s_expression*) list->subexpressions.head->next;
440 const glsl_type *type = read_type(st, type_expr);
441 if (type == NULL)
442 return NULL;
443
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");
448 return NULL;
449 }
450
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());
454 return NULL;
455 }
456
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>)",
461 op_sym->value());
462 return NULL;
463 }
464 } else {
465 if (list_length != 4) {
466 ir_read_error(list, "expected (expression %s <operand>)",
467 op_sym->value());
468 return NULL;
469 }
470 }
471
472 s_expression *exp1 = (s_expression*) (op_sym->next);
473 ir_rvalue *arg1 = read_rvalue(st, exp1);
474 if (arg1 == NULL) {
475 ir_read_error(list, "when reading first operand of %s", op_sym->value());
476 return NULL;
477 }
478
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);
483 if (arg2 == NULL) {
484 ir_read_error(list, "when reading second operand of %s",
485 op_sym->value());
486 return NULL;
487 }
488 }
489
490 return new ir_expression(op, type, arg1, arg2);
491 }
492
493 static ir_swizzle *
494 read_swizzle(_mesa_glsl_parse_state *st, s_list *list)
495 {
496 if (list->length() != 3) {
497 ir_read_error(list, "expected (swiz <swizzle> <rvalue>)");
498 return NULL;
499 }
500
501 s_symbol *swiz = SX_AS_SYMBOL(list->subexpressions.head->next);
502 if (swiz == NULL) {
503 ir_read_error(list, "expected a valid swizzle; found non-symbol");
504 return NULL;
505 }
506
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());
510 return NULL;
511 }
512
513 s_expression *sub = (s_expression*) swiz->next;
514 if (sub == NULL) {
515 ir_read_error(list, "expected rvalue: (swizzle %s <rvalue>)", swiz->value());
516 return NULL;
517 }
518
519 ir_rvalue *rvalue = read_rvalue(st, sub);
520 if (rvalue == NULL)
521 return NULL;
522
523 return ir_swizzle::create(rvalue, swiz->value(), num_components);
524 }
525
526 static ir_constant *
527 read_constant(_mesa_glsl_parse_state *st, s_list *list)
528 {
529 if (list->length() != 3) {
530 ir_read_error(list, "expected (constant <type> (<num> ... <num>))");
531 return NULL;
532 }
533
534 s_expression *type_expr = (s_expression*) list->subexpressions.head->next;
535 const glsl_type *type = read_type(st, type_expr);
536 if (type == NULL)
537 return NULL;
538
539 s_list *values = SX_AS_LIST(type_expr->next);
540 if (values == NULL) {
541 ir_read_error(list, "expected (constant <type> (<num> ... <num>))");
542 return NULL;
543 }
544
545 const glsl_type *const base_type = type->get_base_type();
546
547 unsigned u[16];
548 int i[16];
549 float f[16];
550 bool b[16];
551
552 // Read in list of values (at most 16).
553 int k = 0;
554 foreach_iter(exec_list_iterator, it, values->subexpressions) {
555 if (k >= 16) {
556 ir_read_error(values, "expected at most 16 numbers");
557 return NULL;
558 }
559
560 s_expression *expr = (s_expression*) it.get();
561
562 if (base_type->base_type == GLSL_TYPE_FLOAT) {
563 s_number *value = SX_AS_NUMBER(expr);
564 if (value == NULL) {
565 ir_read_error(values, "expected numbers");
566 return NULL;
567 }
568 f[k] = value->fvalue();
569 } else {
570 s_int *value = SX_AS_INT(expr);
571 if (value == NULL) {
572 ir_read_error(values, "expected integers");
573 return NULL;
574 }
575
576 switch (base_type->base_type) {
577 case GLSL_TYPE_UINT: {
578 u[k] = value->value();
579 break;
580 }
581 case GLSL_TYPE_INT: {
582 i[k] = value->value();
583 break;
584 }
585 case GLSL_TYPE_BOOL: {
586 b[k] = value->value();
587 break;
588 }
589 default:
590 ir_read_error(values, "unsupported constant type");
591 return NULL;
592 }
593 }
594 ++k;
595 }
596 switch (base_type->base_type) {
597 case GLSL_TYPE_UINT:
598 return new ir_constant(type, u);
599 case GLSL_TYPE_INT:
600 return new ir_constant(type, i);
601 case GLSL_TYPE_BOOL:
602 return new ir_constant(type, b);
603 case GLSL_TYPE_FLOAT:
604 return new ir_constant(type, f);
605 }
606 return NULL; // should not be reached
607 }
608
609 static ir_instruction *
610 read_dereferencable(_mesa_glsl_parse_state *st, s_expression *expr)
611 {
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());
616 if (var == NULL) {
617 ir_read_error(expr, "undeclared variable: %s", var_name->value());
618 }
619 return var;
620 } else {
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);
627 }
628 }
629 ir_read_error(expr, "expected variable name or (swiz ...)");
630 return NULL;
631 }
632
633 static ir_dereference *
634 read_var_ref(_mesa_glsl_parse_state *st, s_list *list)
635 {
636 if (list->length() != 2) {
637 ir_read_error(list, "expected (var_ref <variable name or (swiz)>)");
638 return NULL;
639 }
640 s_expression *subj_expr = (s_expression*) list->subexpressions.head->next;
641 ir_instruction *subject = read_dereferencable(st, subj_expr);
642 if (subject == NULL)
643 return NULL;
644 return new ir_dereference(subject);
645 }
646
647 static ir_dereference *
648 read_array_ref(_mesa_glsl_parse_state *st, s_list *list)
649 {
650 if (list->length() != 3) {
651 ir_read_error(list, "expected (array_ref <variable name or (swiz)> "
652 "<rvalue>)");
653 return NULL;
654 }
655
656 s_expression *subj_expr = (s_expression*) list->subexpressions.head->next;
657 ir_instruction *subject = read_dereferencable(st, subj_expr);
658 if (subject == NULL)
659 return NULL;
660
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);
664 }
665
666 static ir_dereference *
667 read_record_ref(_mesa_glsl_parse_state *st, s_list *list)
668 {
669 ir_read_error(list, "FINISHME: record refs not yet supported.");
670 return NULL;
671 }