glsl_type::generate_constructor_prototype now generates the function too
[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(_mesa_glsl_parse_state *, s_expression *,
31 const char *fmt, ...);
32 static const glsl_type *read_type(_mesa_glsl_parse_state *, s_expression *);
33
34 static void scan_for_prototypes(_mesa_glsl_parse_state *, exec_list *,
35 s_expression *);
36 static ir_function *read_function(_mesa_glsl_parse_state *, s_list *,
37 bool skip_body);
38 static void read_function_sig(_mesa_glsl_parse_state *, ir_function *,
39 s_list *, bool skip_body);
40
41 static void read_instructions(_mesa_glsl_parse_state *, exec_list *,
42 s_expression *, ir_loop *);
43 static ir_instruction *read_instruction(_mesa_glsl_parse_state *,
44 s_expression *, ir_loop *);
45 static ir_variable *read_declaration(_mesa_glsl_parse_state *, s_list *);
46 static ir_if *read_if(_mesa_glsl_parse_state *, s_list *, ir_loop *);
47 static ir_loop *read_loop(_mesa_glsl_parse_state *st, s_list *list);
48 static ir_return *read_return(_mesa_glsl_parse_state *, s_list *);
49
50 static ir_rvalue *read_rvalue(_mesa_glsl_parse_state *, s_expression *);
51 static ir_assignment *read_assignment(_mesa_glsl_parse_state *, s_list *);
52 static ir_expression *read_expression(_mesa_glsl_parse_state *, s_list *);
53 static ir_call *read_call(_mesa_glsl_parse_state *, s_list *);
54 static ir_swizzle *read_swizzle(_mesa_glsl_parse_state *, s_list *);
55 static ir_constant *read_constant(_mesa_glsl_parse_state *, s_list *);
56 static ir_dereference *read_var_ref(_mesa_glsl_parse_state *, s_list *);
57 static ir_dereference *read_array_ref(_mesa_glsl_parse_state *, s_list *);
58 static ir_dereference *read_record_ref(_mesa_glsl_parse_state *, s_list *);
59
60 void
61 _mesa_glsl_read_ir(_mesa_glsl_parse_state *state, exec_list *instructions,
62 const char *src)
63 {
64 s_expression *expr = s_expression::read_expression(src);
65 if (expr == NULL) {
66 ir_read_error(state, NULL, "couldn't parse S-Expression.");
67 return;
68 }
69
70 _mesa_glsl_initialize_types(state);
71
72 /* FINISHME: Constructors probably shouldn't be emitted as part of the IR.
73 * FINISHME: Once they're not, remake them by calling:
74 * FINISHME: _mesa_glsl_initialize_constructors(instructions, state);
75 */
76
77 scan_for_prototypes(state, instructions, expr);
78 if (state->error)
79 return;
80
81 read_instructions(state, instructions, expr, NULL);
82 }
83
84 static void
85 ir_read_error(_mesa_glsl_parse_state *state, s_expression *expr,
86 const char *fmt, ...)
87 {
88 va_list ap;
89
90 state->error = true;
91
92 printf("error: ");
93
94 va_start(ap, fmt);
95 vprintf(fmt, ap);
96 va_end(ap);
97 printf("\n");
98
99 if (expr != NULL) {
100 printf("...in this context:\n ");
101 expr->print();
102 printf("\n\n");
103 }
104 }
105
106 static const glsl_type *
107 read_type(_mesa_glsl_parse_state *st, s_expression *expr)
108 {
109 s_list *list = SX_AS_LIST(expr);
110 if (list != NULL) {
111 s_symbol *type_sym = SX_AS_SYMBOL(list->subexpressions.get_head());
112 if (type_sym == NULL) {
113 ir_read_error(st, expr, "expected type (array ...) or (struct ...)");
114 return NULL;
115 }
116 if (strcmp(type_sym->value(), "array") == 0) {
117 if (list->length() != 3) {
118 ir_read_error(st, expr, "expected type (array <type> <int>)");
119 return NULL;
120 }
121
122 // Read base type
123 s_expression *base_expr = (s_expression*) type_sym->next;
124 const glsl_type *base_type = read_type(st, base_expr);
125 if (base_type == NULL) {
126 ir_read_error(st, NULL, "when reading base type of array");
127 return NULL;
128 }
129
130 // Read array size
131 s_int *size = SX_AS_INT(base_expr->next);
132 if (size == NULL) {
133 ir_read_error(st, expr, "found non-integer array size");
134 return NULL;
135 }
136
137 return glsl_type::get_array_instance(base_type, size->value());
138 } else if (strcmp(type_sym->value(), "struct") == 0) {
139 assert(false); // FINISHME
140 } else {
141 ir_read_error(st, expr, "expected (array ...) or (struct ...); "
142 "found (%s ...)", type_sym->value());
143 return NULL;
144 }
145 }
146
147 s_symbol *type_sym = SX_AS_SYMBOL(expr);
148 if (type_sym == NULL) {
149 ir_read_error(st, expr, "expected <type> (symbol or list)");
150 return NULL;
151 }
152
153 const glsl_type *type = st->symbols->get_type(type_sym->value());
154 if (type == NULL)
155 ir_read_error(st, expr, "invalid type: %s", type_sym->value());
156
157 return type;
158 }
159
160
161 static void
162 scan_for_prototypes(_mesa_glsl_parse_state *st, exec_list *instructions,
163 s_expression *expr)
164 {
165 s_list *list = SX_AS_LIST(expr);
166 if (list == NULL) {
167 ir_read_error(st, expr, "Expected (<instruction> ...); found an atom.");
168 return;
169 }
170
171 foreach_iter(exec_list_iterator, it, list->subexpressions) {
172 s_list *sub = SX_AS_LIST(it.get());
173 if (sub == NULL)
174 continue; // not a (function ...); ignore it.
175
176 s_symbol *tag = SX_AS_SYMBOL(sub->subexpressions.get_head());
177 if (tag == NULL || strcmp(tag->value(), "function") != 0)
178 continue; // not a (function ...); ignore it.
179
180 ir_function *f = read_function(st, sub, true);
181 if (f == NULL)
182 return;
183 instructions->push_tail(f);
184 }
185 }
186
187 static ir_function *
188 read_function(_mesa_glsl_parse_state *st, s_list *list, bool skip_body)
189 {
190 if (list->length() < 3) {
191 ir_read_error(st, list, "Expected (function <name> (signature ...) ...)");
192 return NULL;
193 }
194
195 s_symbol *name = SX_AS_SYMBOL(list->subexpressions.head->next);
196 if (name == NULL) {
197 ir_read_error(st, list, "Expected (function <name> ...)");
198 return NULL;
199 }
200
201 ir_function *f = st->symbols->get_function(name->value());
202 if (f == NULL) {
203 f = new ir_function(name->value());
204 bool added = st->symbols->add_function(name->value(), f);
205 assert(added);
206 }
207
208 exec_list_iterator it = list->subexpressions.iterator();
209 it.next(); // skip "function" tag
210 it.next(); // skip function name
211 for (/* nothing */; it.has_next(); it.next()) {
212 s_list *siglist = SX_AS_LIST(it.get());
213 if (siglist == NULL) {
214 ir_read_error(st, list, "Expected (function (signature ...) ...)");
215 return NULL;
216 }
217
218 s_symbol *tag = SX_AS_SYMBOL(siglist->subexpressions.get_head());
219 if (tag == NULL || strcmp(tag->value(), "signature") != 0) {
220 ir_read_error(st, siglist, "Expected (signature ...)");
221 return NULL;
222 }
223
224 read_function_sig(st, f, siglist, skip_body);
225 }
226 return f;
227 }
228
229 static void
230 read_function_sig(_mesa_glsl_parse_state *st, ir_function *f, s_list *list,
231 bool skip_body)
232 {
233 if (list->length() != 4) {
234 ir_read_error(st, list, "Expected (signature <type> (parameters ...) "
235 "(<instruction> ...))");
236 return;
237 }
238
239 s_expression *type_expr = (s_expression*) list->subexpressions.head->next;
240 const glsl_type *return_type = read_type(st, type_expr);
241 if (return_type == NULL)
242 return;
243
244 s_list *paramlist = SX_AS_LIST(type_expr->next);
245 s_list *body_list = SX_AS_LIST(paramlist->next);
246 if (paramlist == NULL || body_list == NULL) {
247 ir_read_error(st, list, "Expected (signature <type> (parameters ...) "
248 "(<instruction> ...))");
249 return;
250 }
251 s_symbol *paramtag = SX_AS_SYMBOL(paramlist->subexpressions.get_head());
252 if (paramtag == NULL || strcmp(paramtag->value(), "parameters") != 0) {
253 ir_read_error(st, paramlist, "Expected (parameters ...)");
254 return;
255 }
256
257 // Read the parameters list into a temporary place.
258 exec_list hir_parameters;
259 st->symbols->push_scope();
260
261 exec_list_iterator it = paramlist->subexpressions.iterator();
262 for (it.next() /* skip "parameters" */; it.has_next(); it.next()) {
263 s_list *decl = SX_AS_LIST(it.get());
264 ir_variable *var = read_declaration(st, decl);
265 if (var == NULL)
266 return;
267
268 hir_parameters.push_tail(var);
269 }
270
271 ir_function_signature *sig = f->exact_matching_signature(&hir_parameters);
272 if (sig != NULL) {
273 const char *badvar = sig->qualifiers_match(&hir_parameters);
274 if (badvar != NULL) {
275 ir_read_error(st, list, "function `%s' parameter `%s' qualifiers "
276 "don't match prototype", f->name, badvar);
277 return;
278 }
279
280 if (sig->return_type != return_type) {
281 ir_read_error(st, list, "function `%s' return type doesn't "
282 "match prototype", f->name);
283 return;
284 }
285 } else {
286 sig = new ir_function_signature(return_type);
287 f->add_signature(sig);
288 }
289
290 sig->replace_parameters(&hir_parameters);
291
292 if (!skip_body) {
293 if (sig->is_defined) {
294 ir_read_error(st, list, "function %s redefined", f->name);
295 return;
296 }
297 read_instructions(st, &sig->body, body_list, NULL);
298 }
299
300 st->symbols->pop_scope();
301 }
302
303 static void
304 read_instructions(_mesa_glsl_parse_state *st, exec_list *instructions,
305 s_expression *expr, ir_loop *loop_ctx)
306 {
307 // Read in a list of instructions
308 s_list *list = SX_AS_LIST(expr);
309 if (list == NULL) {
310 ir_read_error(st, expr, "Expected (<instruction> ...); found an atom.");
311 return;
312 }
313
314 foreach_iter(exec_list_iterator, it, list->subexpressions) {
315 s_expression *sub = (s_expression*) it.get();
316 ir_instruction *ir = read_instruction(st, sub, loop_ctx);
317 if (ir == NULL) {
318 ir_read_error(st, sub, "Invalid instruction.\n");
319 return;
320 }
321 instructions->push_tail(ir);
322 }
323 }
324
325
326 static ir_instruction *
327 read_instruction(_mesa_glsl_parse_state *st, s_expression *expr,
328 ir_loop *loop_ctx)
329 {
330 s_symbol *symbol = SX_AS_SYMBOL(expr);
331 if (symbol != NULL) {
332 if (strcmp(symbol->value(), "break") == 0 && loop_ctx != NULL)
333 return new ir_loop_jump(loop_ctx, ir_loop_jump::jump_break);
334 if (strcmp(symbol->value(), "continue") == 0 && loop_ctx != NULL)
335 return new ir_loop_jump(loop_ctx, ir_loop_jump::jump_continue);
336 }
337
338 s_list *list = SX_AS_LIST(expr);
339 if (list == NULL || list->subexpressions.is_empty())
340 return NULL;
341
342 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
343 if (tag == NULL) {
344 ir_read_error(st, expr, "expected instruction tag");
345 return NULL;
346 }
347
348 ir_instruction *inst = NULL;
349 if (strcmp(tag->value(), "declare") == 0) {
350 inst = read_declaration(st, list);
351 } else if (strcmp(tag->value(), "if") == 0) {
352 inst = read_if(st, list, loop_ctx);
353 } else if (strcmp(tag->value(), "loop") == 0) {
354 inst = read_loop(st, list);
355 } else if (strcmp(tag->value(), "return") == 0) {
356 inst = read_return(st, list);
357 } else if (strcmp(tag->value(), "function") == 0) {
358 inst = read_function(st, list, false);
359 } else {
360 inst = read_rvalue(st, list);
361 if (inst == NULL)
362 ir_read_error(st, NULL, "when reading instruction");
363 }
364 return inst;
365 }
366
367
368 static ir_variable *
369 read_declaration(_mesa_glsl_parse_state *st, s_list *list)
370 {
371 if (list->length() != 4) {
372 ir_read_error(st, list, "expected (declare (<qualifiers>) <type> "
373 "<name>)");
374 return NULL;
375 }
376
377 s_list *quals = SX_AS_LIST(list->subexpressions.head->next);
378 if (quals == NULL) {
379 ir_read_error(st, list, "expected a list of variable qualifiers");
380 return NULL;
381 }
382
383 s_expression *type_expr = (s_expression*) quals->next;
384 const glsl_type *type = read_type(st, type_expr);
385 if (type == NULL)
386 return NULL;
387
388 s_symbol *var_name = SX_AS_SYMBOL(type_expr->next);
389 if (var_name == NULL) {
390 ir_read_error(st, list, "expected variable name, found non-symbol");
391 return NULL;
392 }
393
394 ir_variable *var = new ir_variable(type, var_name->value());
395
396 foreach_iter(exec_list_iterator, it, quals->subexpressions) {
397 s_symbol *qualifier = SX_AS_SYMBOL(it.get());
398 if (qualifier == NULL) {
399 ir_read_error(st, list, "qualifier list must contain only symbols");
400 delete var;
401 return NULL;
402 }
403
404 // FINISHME: Check for duplicate/conflicting qualifiers.
405 if (strcmp(qualifier->value(), "centroid") == 0) {
406 var->centroid = 1;
407 } else if (strcmp(qualifier->value(), "invariant") == 0) {
408 var->invariant = 1;
409 } else if (strcmp(qualifier->value(), "uniform") == 0) {
410 var->mode = ir_var_uniform;
411 } else if (strcmp(qualifier->value(), "auto") == 0) {
412 var->mode = ir_var_auto;
413 } else if (strcmp(qualifier->value(), "in") == 0) {
414 var->mode = ir_var_in;
415 } else if (strcmp(qualifier->value(), "out") == 0) {
416 var->mode = ir_var_out;
417 } else if (strcmp(qualifier->value(), "inout") == 0) {
418 var->mode = ir_var_inout;
419 } else if (strcmp(qualifier->value(), "smooth") == 0) {
420 var->interpolation = ir_var_smooth;
421 } else if (strcmp(qualifier->value(), "flat") == 0) {
422 var->interpolation = ir_var_flat;
423 } else if (strcmp(qualifier->value(), "noperspective") == 0) {
424 var->interpolation = ir_var_noperspective;
425 } else {
426 ir_read_error(st, list, "unknown qualifier: %s", qualifier->value());
427 delete var;
428 return NULL;
429 }
430 }
431
432 // Add the variable to the symbol table
433 st->symbols->add_variable(var_name->value(), var);
434
435 return var;
436 }
437
438
439 static ir_if *
440 read_if(_mesa_glsl_parse_state *st, s_list *list, ir_loop *loop_ctx)
441 {
442 if (list->length() != 4) {
443 ir_read_error(st, list, "expected (if <condition> (<then> ...) "
444 "(<else> ...))");
445 return NULL;
446 }
447
448 s_expression *cond_expr = (s_expression*) list->subexpressions.head->next;
449 ir_rvalue *condition = read_rvalue(st, cond_expr);
450 if (condition == NULL) {
451 ir_read_error(st, NULL, "when reading condition of (if ...)");
452 return NULL;
453 }
454
455 s_expression *then_expr = (s_expression*) cond_expr->next;
456 s_expression *else_expr = (s_expression*) then_expr->next;
457
458 ir_if *iff = new ir_if(condition);
459
460 read_instructions(st, &iff->then_instructions, then_expr, loop_ctx);
461 read_instructions(st, &iff->else_instructions, else_expr, loop_ctx);
462 if (st->error) {
463 delete iff;
464 iff = NULL;
465 }
466 return iff;
467 }
468
469
470 static ir_loop *
471 read_loop(_mesa_glsl_parse_state *st, s_list *list)
472 {
473 if (list->length() != 6) {
474 ir_read_error(st, list, "expected (loop <counter> <from> <to> "
475 "<increment> <body>)");
476 return NULL;
477 }
478
479 s_expression *count_expr = (s_expression*) list->subexpressions.head->next;
480 s_expression *from_expr = (s_expression*) count_expr->next;
481 s_expression *to_expr = (s_expression*) from_expr->next;
482 s_expression *inc_expr = (s_expression*) to_expr->next;
483 s_expression *body_expr = (s_expression*) inc_expr->next;
484
485 // FINISHME: actually read the count/from/to fields.
486
487 ir_loop *loop = new ir_loop;
488 read_instructions(st, &loop->body_instructions, body_expr, loop);
489 if (st->error) {
490 delete loop;
491 loop = NULL;
492 }
493 return loop;
494 }
495
496
497 static ir_return *
498 read_return(_mesa_glsl_parse_state *st, s_list *list)
499 {
500 if (list->length() != 2) {
501 ir_read_error(st, list, "expected (return <rvalue>)");
502 return NULL;
503 }
504
505 s_expression *expr = (s_expression*) list->subexpressions.head->next;
506
507 ir_rvalue *retval = read_rvalue(st, expr);
508 if (retval == NULL) {
509 ir_read_error(st, NULL, "when reading return value");
510 return NULL;
511 }
512
513 return new ir_return(retval);
514 }
515
516
517 static ir_rvalue *
518 read_rvalue(_mesa_glsl_parse_state *st, s_expression *expr)
519 {
520 s_list *list = SX_AS_LIST(expr);
521 if (list == NULL || list->subexpressions.is_empty())
522 return NULL;
523
524 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
525 if (tag == NULL) {
526 ir_read_error(st, expr, "expected rvalue tag");
527 return NULL;
528 }
529
530 ir_rvalue *rvalue = NULL;
531 if (strcmp(tag->value(), "swiz") == 0) {
532 rvalue = read_swizzle(st, list);
533 } else if (strcmp(tag->value(), "assign") == 0) {
534 rvalue = read_assignment(st, list);
535 } else if (strcmp(tag->value(), "expression") == 0) {
536 rvalue = read_expression(st, list);
537 } else if (strcmp(tag->value(), "call") == 0) {
538 rvalue = read_call(st, list);
539 } else if (strcmp(tag->value(), "constant") == 0) {
540 rvalue = read_constant(st, list);
541 } else if (strcmp(tag->value(), "var_ref") == 0) {
542 rvalue = read_var_ref(st, list);
543 } else if (strcmp(tag->value(), "array_ref") == 0) {
544 rvalue = read_array_ref(st, list);
545 } else if (strcmp(tag->value(), "record_ref") == 0) {
546 rvalue = read_record_ref(st, list);
547 } else {
548 ir_read_error(st, expr, "unrecognized rvalue tag: %s", tag->value());
549 }
550
551 return rvalue;
552 }
553
554 static ir_assignment *
555 read_assignment(_mesa_glsl_parse_state *st, s_list *list)
556 {
557 if (list->length() != 4) {
558 ir_read_error(st, list, "expected (assign <condition> <lhs> <rhs>)");
559 return NULL;
560 }
561
562 s_expression *cond_expr = (s_expression*) list->subexpressions.head->next;
563 s_expression *lhs_expr = (s_expression*) cond_expr->next;
564 s_expression *rhs_expr = (s_expression*) lhs_expr->next;
565
566 // FINISHME: Deal with "true" condition
567 ir_rvalue *condition = read_rvalue(st, cond_expr);
568 if (condition == NULL) {
569 ir_read_error(st, NULL, "when reading condition of assignment");
570 return NULL;
571 }
572
573 ir_rvalue *lhs = read_rvalue(st, lhs_expr);
574 if (lhs == NULL) {
575 ir_read_error(st, NULL, "when reading left-hand side of assignment");
576 return NULL;
577 }
578
579 ir_rvalue *rhs = read_rvalue(st, rhs_expr);
580 if (rhs == NULL) {
581 ir_read_error(st, NULL, "when reading right-hand side of assignment");
582 return NULL;
583 }
584
585 return new ir_assignment(lhs, rhs, condition);
586 }
587
588 static ir_call *
589 read_call(_mesa_glsl_parse_state *st, s_list *list)
590 {
591 if (list->length() != 3) {
592 ir_read_error(st, list, "expected (call <name> (<param> ...))");
593 return NULL;
594 }
595
596 s_symbol *name = SX_AS_SYMBOL(list->subexpressions.head->next);
597 s_list *params = SX_AS_LIST(name->next);
598 if (name == NULL || params == NULL) {
599 ir_read_error(st, list, "expected (call <name> (<param> ...))");
600 return NULL;
601 }
602
603 exec_list parameters;
604
605 foreach_iter(exec_list_iterator, it, params->subexpressions) {
606 s_expression *expr = (s_expression*) it.get();
607 ir_rvalue *param = read_rvalue(st, expr);
608 if (param == NULL) {
609 ir_read_error(st, list, "when reading parameter to function call");
610 return NULL;
611 }
612 parameters.push_tail(param);
613 }
614
615 ir_function *f = st->symbols->get_function(name->value());
616 if (f == NULL) {
617 ir_read_error(st, list, "found call to undefined function %s",
618 name->value());
619 return NULL;
620 }
621
622 const ir_function_signature *callee = f->matching_signature(&parameters);
623 if (callee == NULL) {
624 ir_read_error(st, list, "couldn't find matching signature for function "
625 "%s", name->value());
626 return NULL;
627 }
628
629 return new ir_call(callee, &parameters);
630 }
631
632 static ir_expression *
633 read_expression(_mesa_glsl_parse_state *st, s_list *list)
634 {
635 const unsigned list_length = list->length();
636 if (list_length < 4) {
637 ir_read_error(st, list, "expected (expression <type> <operator> "
638 "<operand> [<operand>])");
639 return NULL;
640 }
641
642 s_expression *type_expr = (s_expression*) list->subexpressions.head->next;
643 const glsl_type *type = read_type(st, type_expr);
644 if (type == NULL)
645 return NULL;
646
647 /* Read the operator */
648 s_symbol *op_sym = SX_AS_SYMBOL(type_expr->next);
649 if (op_sym == NULL) {
650 ir_read_error(st, list, "expected operator, found non-symbol");
651 return NULL;
652 }
653
654 ir_expression_operation op = ir_expression::get_operator(op_sym->value());
655 if (op == (ir_expression_operation) -1) {
656 ir_read_error(st, list, "invalid operator: %s", op_sym->value());
657 return NULL;
658 }
659
660 /* Now that we know the operator, check for the right number of operands */
661 if (ir_expression::get_num_operands(op) == 2) {
662 if (list_length != 5) {
663 ir_read_error(st, list, "expected (expression <type> %s <operand> "
664 " <operand>)", op_sym->value());
665 return NULL;
666 }
667 } else {
668 if (list_length != 4) {
669 ir_read_error(st, list, "expected (expression <type> %s <operand>)",
670 op_sym->value());
671 return NULL;
672 }
673 }
674
675 s_expression *exp1 = (s_expression*) (op_sym->next);
676 ir_rvalue *arg1 = read_rvalue(st, exp1);
677 if (arg1 == NULL) {
678 ir_read_error(st, NULL, "when reading first operand of %s",
679 op_sym->value());
680 return NULL;
681 }
682
683 ir_rvalue *arg2 = NULL;
684 if (ir_expression::get_num_operands(op) == 2) {
685 s_expression *exp2 = (s_expression*) (exp1->next);
686 arg2 = read_rvalue(st, exp2);
687 if (arg2 == NULL) {
688 ir_read_error(st, NULL, "when reading second operand of %s",
689 op_sym->value());
690 return NULL;
691 }
692 }
693
694 return new ir_expression(op, type, arg1, arg2);
695 }
696
697 static ir_swizzle *
698 read_swizzle(_mesa_glsl_parse_state *st, s_list *list)
699 {
700 if (list->length() != 3) {
701 ir_read_error(st, list, "expected (swiz <swizzle> <rvalue>)");
702 return NULL;
703 }
704
705 s_symbol *swiz = SX_AS_SYMBOL(list->subexpressions.head->next);
706 if (swiz == NULL) {
707 ir_read_error(st, list, "expected a valid swizzle; found non-symbol");
708 return NULL;
709 }
710
711 if (strlen(swiz->value()) > 4) {
712 ir_read_error(st, list, "expected a valid swizzle; found %s",
713 swiz->value());
714 return NULL;
715 }
716
717 s_expression *sub = (s_expression*) swiz->next;
718 if (sub == NULL) {
719 ir_read_error(st, list, "expected rvalue: (swizzle %s <rvalue>)",
720 swiz->value());
721 return NULL;
722 }
723
724 ir_rvalue *rvalue = read_rvalue(st, sub);
725 if (rvalue == NULL)
726 return NULL;
727
728 ir_swizzle *ir = ir_swizzle::create(rvalue, swiz->value(),
729 rvalue->type->vector_elements);
730 if (ir == NULL)
731 ir_read_error(st, list, "invalid swizzle");
732
733 return ir;
734 }
735
736 static ir_constant *
737 read_constant(_mesa_glsl_parse_state *st, s_list *list)
738 {
739 if (list->length() != 3) {
740 ir_read_error(st, list, "expected (constant <type> (<num> ... <num>))");
741 return NULL;
742 }
743
744 s_expression *type_expr = (s_expression*) list->subexpressions.head->next;
745 const glsl_type *type = read_type(st, type_expr);
746 if (type == NULL)
747 return NULL;
748
749 s_list *values = SX_AS_LIST(type_expr->next);
750 if (values == NULL) {
751 ir_read_error(st, list, "expected (constant <type> (<num> ... <num>))");
752 return NULL;
753 }
754
755 const glsl_type *const base_type = type->get_base_type();
756
757 unsigned u[16];
758 int i[16];
759 float f[16];
760 bool b[16];
761
762 // Read in list of values (at most 16).
763 int k = 0;
764 foreach_iter(exec_list_iterator, it, values->subexpressions) {
765 if (k >= 16) {
766 ir_read_error(st, values, "expected at most 16 numbers");
767 return NULL;
768 }
769
770 s_expression *expr = (s_expression*) it.get();
771
772 if (base_type->base_type == GLSL_TYPE_FLOAT) {
773 s_number *value = SX_AS_NUMBER(expr);
774 if (value == NULL) {
775 ir_read_error(st, values, "expected numbers");
776 return NULL;
777 }
778 f[k] = value->fvalue();
779 } else {
780 s_int *value = SX_AS_INT(expr);
781 if (value == NULL) {
782 ir_read_error(st, values, "expected integers");
783 return NULL;
784 }
785
786 switch (base_type->base_type) {
787 case GLSL_TYPE_UINT: {
788 u[k] = value->value();
789 break;
790 }
791 case GLSL_TYPE_INT: {
792 i[k] = value->value();
793 break;
794 }
795 case GLSL_TYPE_BOOL: {
796 b[k] = value->value();
797 break;
798 }
799 default:
800 ir_read_error(st, values, "unsupported constant type");
801 return NULL;
802 }
803 }
804 ++k;
805 }
806 switch (base_type->base_type) {
807 case GLSL_TYPE_UINT:
808 return new ir_constant(type, u);
809 case GLSL_TYPE_INT:
810 return new ir_constant(type, i);
811 case GLSL_TYPE_BOOL:
812 return new ir_constant(type, b);
813 case GLSL_TYPE_FLOAT:
814 return new ir_constant(type, f);
815 }
816 return NULL; // should not be reached
817 }
818
819 static ir_instruction *
820 read_dereferencable(_mesa_glsl_parse_state *st, s_expression *expr)
821 {
822 // Read the subject of a dereference - either a variable name or a swizzle
823 s_symbol *var_name = SX_AS_SYMBOL(expr);
824 if (var_name != NULL) {
825 ir_variable *var = st->symbols->get_variable(var_name->value());
826 if (var == NULL) {
827 ir_read_error(st, expr, "undeclared variable: %s", var_name->value());
828 }
829 return var;
830 } else {
831 // Hopefully a (swiz ...)
832 s_list *list = SX_AS_LIST(expr);
833 if (list != NULL && !list->subexpressions.is_empty()) {
834 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.head);
835 if (tag != NULL && strcmp(tag->value(), "swiz") == 0)
836 return read_swizzle(st, list);
837 }
838 }
839 ir_read_error(st, expr, "expected variable name or (swiz ...)");
840 return NULL;
841 }
842
843 static ir_dereference *
844 read_var_ref(_mesa_glsl_parse_state *st, s_list *list)
845 {
846 if (list->length() != 2) {
847 ir_read_error(st, list, "expected (var_ref <variable name or (swiz)>)");
848 return NULL;
849 }
850 s_expression *subj_expr = (s_expression*) list->subexpressions.head->next;
851 ir_instruction *subject = read_dereferencable(st, subj_expr);
852 if (subject == NULL)
853 return NULL;
854 return new ir_dereference(subject);
855 }
856
857 static ir_dereference *
858 read_array_ref(_mesa_glsl_parse_state *st, s_list *list)
859 {
860 if (list->length() != 3) {
861 ir_read_error(st, list, "expected (array_ref <variable name or (swiz)> "
862 "<rvalue>)");
863 return NULL;
864 }
865
866 s_expression *subj_expr = (s_expression*) list->subexpressions.head->next;
867 ir_instruction *subject = read_dereferencable(st, subj_expr);
868 if (subject == NULL)
869 return NULL;
870
871 s_expression *idx_expr = (s_expression*) subj_expr->next;
872 ir_rvalue *idx = read_rvalue(st, idx_expr);
873 return new ir_dereference(subject, idx);
874 }
875
876 static ir_dereference *
877 read_record_ref(_mesa_glsl_parse_state *st, s_list *list)
878 {
879 ir_read_error(st, list, "FINISHME: record refs not yet supported.");
880 return NULL;
881 }