glsl2: Move the compiler to the subdirectory it will live in in Mesa.
[mesa.git] / src / glsl / 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
24 #include <cstdarg>
25
26 extern "C" {
27 #include <talloc.h>
28 }
29
30 #include "ir_reader.h"
31 #include "glsl_parser_extras.h"
32 #include "glsl_types.h"
33 #include "s_expression.h"
34
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 *);
38
39 static void scan_for_prototypes(_mesa_glsl_parse_state *, exec_list *,
40 s_expression *);
41 static ir_function *read_function(_mesa_glsl_parse_state *, s_list *,
42 bool skip_body);
43 static void read_function_sig(_mesa_glsl_parse_state *, ir_function *,
44 s_list *, bool skip_body);
45
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 *);
54
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 *);
62
63 static ir_dereference *read_dereference(_mesa_glsl_parse_state *,
64 s_expression *);
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 *);
68
69 void
70 _mesa_glsl_read_ir(_mesa_glsl_parse_state *state, exec_list *instructions,
71 const char *src)
72 {
73 void *ctx = talloc_parent(state);
74 s_expression *expr = s_expression::read_expression(ctx, src);
75 if (expr == NULL) {
76 ir_read_error(state, NULL, "couldn't parse S-Expression.");
77 return;
78 }
79
80 scan_for_prototypes(state, instructions, expr);
81 if (state->error)
82 return;
83
84 read_instructions(state, instructions, expr, NULL);
85 }
86
87 static void
88 ir_read_error(_mesa_glsl_parse_state *state, s_expression *expr,
89 const char *fmt, ...)
90 {
91 va_list ap;
92
93 state->error = true;
94
95 state->info_log = talloc_strdup_append(state->info_log, "error: ");
96
97 va_start(ap, fmt);
98 state->info_log = talloc_vasprintf_append(state->info_log, fmt, ap);
99 va_end(ap);
100 state->info_log = talloc_strdup_append(state->info_log, "\n");
101
102 if (expr != NULL) {
103 state->info_log = talloc_strdup_append(state->info_log,
104 "...in this context:\n ");
105 expr->print();
106 state->info_log = talloc_strdup_append(state->info_log, "\n\n");
107 }
108 }
109
110 static const glsl_type *
111 read_type(_mesa_glsl_parse_state *st, s_expression *expr)
112 {
113 s_list *list = SX_AS_LIST(expr);
114 if (list != NULL) {
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 ...)");
118 return NULL;
119 }
120 if (strcmp(type_sym->value(), "array") == 0) {
121 if (list->length() != 3) {
122 ir_read_error(st, expr, "expected type (array <type> <int>)");
123 return NULL;
124 }
125
126 // Read base type
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");
131 return NULL;
132 }
133
134 // Read array size
135 s_int *size = SX_AS_INT(base_expr->next);
136 if (size == NULL) {
137 ir_read_error(st, expr, "found non-integer array size");
138 return NULL;
139 }
140
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
144 } else {
145 ir_read_error(st, expr, "expected (array ...) or (struct ...); "
146 "found (%s ...)", type_sym->value());
147 return NULL;
148 }
149 }
150
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)");
154 return NULL;
155 }
156
157 const glsl_type *type = st->symbols->get_type(type_sym->value());
158 if (type == NULL)
159 ir_read_error(st, expr, "invalid type: %s", type_sym->value());
160
161 return type;
162 }
163
164
165 static void
166 scan_for_prototypes(_mesa_glsl_parse_state *st, exec_list *instructions,
167 s_expression *expr)
168 {
169 s_list *list = SX_AS_LIST(expr);
170 if (list == NULL) {
171 ir_read_error(st, expr, "Expected (<instruction> ...); found an atom.");
172 return;
173 }
174
175 foreach_iter(exec_list_iterator, it, list->subexpressions) {
176 s_list *sub = SX_AS_LIST(it.get());
177 if (sub == NULL)
178 continue; // not a (function ...); ignore it.
179
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.
183
184 ir_function *f = read_function(st, sub, true);
185 if (f == NULL)
186 return;
187 instructions->push_tail(f);
188 }
189 }
190
191 static ir_function *
192 read_function(_mesa_glsl_parse_state *st, s_list *list, bool skip_body)
193 {
194 void *ctx = talloc_parent(st);
195 if (list->length() < 3) {
196 ir_read_error(st, list, "Expected (function <name> (signature ...) ...)");
197 return NULL;
198 }
199
200 s_symbol *name = SX_AS_SYMBOL(list->subexpressions.head->next);
201 if (name == NULL) {
202 ir_read_error(st, list, "Expected (function <name> ...)");
203 return NULL;
204 }
205
206 ir_function *f = st->symbols->get_function(name->value());
207 if (f == NULL) {
208 f = new(ctx) ir_function(name->value());
209 bool added = st->symbols->add_function(name->value(), f);
210 assert(added);
211 }
212
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 ...) ...)");
220 return NULL;
221 }
222
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 ...)");
226 return NULL;
227 }
228
229 read_function_sig(st, f, siglist, skip_body);
230 }
231 return f;
232 }
233
234 static void
235 read_function_sig(_mesa_glsl_parse_state *st, ir_function *f, s_list *list,
236 bool skip_body)
237 {
238 void *ctx = talloc_parent(st);
239 if (list->length() != 4) {
240 ir_read_error(st, list, "Expected (signature <type> (parameters ...) "
241 "(<instruction> ...))");
242 return;
243 }
244
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)
248 return;
249
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> ...))");
255 return;
256 }
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 ...)");
260 return;
261 }
262
263 // Read the parameters list into a temporary place.
264 exec_list hir_parameters;
265 st->symbols->push_scope();
266
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);
271 if (var == NULL)
272 return;
273
274 hir_parameters.push_tail(var);
275 }
276
277 ir_function_signature *sig = f->exact_matching_signature(&hir_parameters);
278 if (sig != NULL) {
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);
283 return;
284 }
285
286 if (sig->return_type != return_type) {
287 ir_read_error(st, list, "function `%s' return type doesn't "
288 "match prototype", f->name);
289 return;
290 }
291 } else {
292 sig = new(ctx) ir_function_signature(return_type);
293 f->add_signature(sig);
294 }
295
296 sig->replace_parameters(&hir_parameters);
297
298 if (!skip_body) {
299 if (sig->is_defined) {
300 ir_read_error(st, list, "function %s redefined", f->name);
301 return;
302 }
303 read_instructions(st, &sig->body, body_list, NULL);
304 sig->is_defined = true;
305 }
306
307 st->symbols->pop_scope();
308 }
309
310 static void
311 read_instructions(_mesa_glsl_parse_state *st, exec_list *instructions,
312 s_expression *expr, ir_loop *loop_ctx)
313 {
314 // Read in a list of instructions
315 s_list *list = SX_AS_LIST(expr);
316 if (list == NULL) {
317 ir_read_error(st, expr, "Expected (<instruction> ...); found an atom.");
318 return;
319 }
320
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);
324 if (ir == NULL) {
325 ir_read_error(st, sub, "Invalid instruction.\n");
326 return;
327 }
328 instructions->push_tail(ir);
329 }
330 }
331
332
333 static ir_instruction *
334 read_instruction(_mesa_glsl_parse_state *st, s_expression *expr,
335 ir_loop *loop_ctx)
336 {
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);
344 }
345
346 s_list *list = SX_AS_LIST(expr);
347 if (list == NULL || list->subexpressions.is_empty())
348 return NULL;
349
350 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
351 if (tag == NULL) {
352 ir_read_error(st, expr, "expected instruction tag");
353 return NULL;
354 }
355
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);
367 } else {
368 inst = read_rvalue(st, list);
369 if (inst == NULL)
370 ir_read_error(st, NULL, "when reading instruction");
371 }
372 return inst;
373 }
374
375
376 static ir_variable *
377 read_declaration(_mesa_glsl_parse_state *st, s_list *list)
378 {
379 void *ctx = talloc_parent(st);
380 if (list->length() != 4) {
381 ir_read_error(st, list, "expected (declare (<qualifiers>) <type> "
382 "<name>)");
383 return NULL;
384 }
385
386 s_list *quals = SX_AS_LIST(list->subexpressions.head->next);
387 if (quals == NULL) {
388 ir_read_error(st, list, "expected a list of variable qualifiers");
389 return NULL;
390 }
391
392 s_expression *type_expr = (s_expression*) quals->next;
393 const glsl_type *type = read_type(st, type_expr);
394 if (type == NULL)
395 return NULL;
396
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");
400 return NULL;
401 }
402
403 ir_variable *var = new(ctx) ir_variable(type, var_name->value());
404
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");
409 delete var;
410 return NULL;
411 }
412
413 // FINISHME: Check for duplicate/conflicting qualifiers.
414 if (strcmp(qualifier->value(), "centroid") == 0) {
415 var->centroid = 1;
416 } else if (strcmp(qualifier->value(), "invariant") == 0) {
417 var->invariant = 1;
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;
434 } else {
435 ir_read_error(st, list, "unknown qualifier: %s", qualifier->value());
436 delete var;
437 return NULL;
438 }
439 }
440
441 // Add the variable to the symbol table
442 st->symbols->add_variable(var_name->value(), var);
443
444 return var;
445 }
446
447
448 static ir_if *
449 read_if(_mesa_glsl_parse_state *st, s_list *list, ir_loop *loop_ctx)
450 {
451 void *ctx = talloc_parent(st);
452 if (list->length() != 4) {
453 ir_read_error(st, list, "expected (if <condition> (<then> ...) "
454 "(<else> ...))");
455 return NULL;
456 }
457
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 ...)");
462 return NULL;
463 }
464
465 s_expression *then_expr = (s_expression*) cond_expr->next;
466 s_expression *else_expr = (s_expression*) then_expr->next;
467
468 ir_if *iff = new(ctx) ir_if(condition);
469
470 read_instructions(st, &iff->then_instructions, then_expr, loop_ctx);
471 read_instructions(st, &iff->else_instructions, else_expr, loop_ctx);
472 if (st->error) {
473 delete iff;
474 iff = NULL;
475 }
476 return iff;
477 }
478
479
480 static ir_loop *
481 read_loop(_mesa_glsl_parse_state *st, s_list *list)
482 {
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>)");
487 return NULL;
488 }
489
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;
495
496 // FINISHME: actually read the count/from/to fields.
497
498 ir_loop *loop = new(ctx) ir_loop;
499 read_instructions(st, &loop->body_instructions, body_expr, loop);
500 if (st->error) {
501 delete loop;
502 loop = NULL;
503 }
504 return loop;
505 }
506
507
508 static ir_return *
509 read_return(_mesa_glsl_parse_state *st, s_list *list)
510 {
511 void *ctx = talloc_parent(st);
512 if (list->length() != 2) {
513 ir_read_error(st, list, "expected (return <rvalue>)");
514 return NULL;
515 }
516
517 s_expression *expr = (s_expression*) list->subexpressions.head->next;
518
519 ir_rvalue *retval = read_rvalue(st, expr);
520 if (retval == NULL) {
521 ir_read_error(st, NULL, "when reading return value");
522 return NULL;
523 }
524
525 return new(ctx) ir_return(retval);
526 }
527
528
529 static ir_rvalue *
530 read_rvalue(_mesa_glsl_parse_state *st, s_expression *expr)
531 {
532 s_list *list = SX_AS_LIST(expr);
533 if (list == NULL || list->subexpressions.is_empty())
534 return NULL;
535
536 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
537 if (tag == NULL) {
538 ir_read_error(st, expr, "expected rvalue tag");
539 return NULL;
540 }
541
542 ir_rvalue *rvalue = read_dereference(st, list);
543 if (rvalue != NULL || st->error)
544 return rvalue;
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);
555 } else {
556 rvalue = read_texture(st, list);
557 if (rvalue == NULL && !st->error)
558 ir_read_error(st, expr, "unrecognized rvalue tag: %s", tag->value());
559 }
560
561 return rvalue;
562 }
563
564 static ir_assignment *
565 read_assignment(_mesa_glsl_parse_state *st, s_list *list)
566 {
567 void *ctx = talloc_parent(st);
568 if (list->length() != 4) {
569 ir_read_error(st, list, "expected (assign <condition> <lhs> <rhs>)");
570 return NULL;
571 }
572
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;
576
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");
581 return NULL;
582 }
583
584 ir_rvalue *lhs = read_rvalue(st, lhs_expr);
585 if (lhs == NULL) {
586 ir_read_error(st, NULL, "when reading left-hand side of assignment");
587 return NULL;
588 }
589
590 ir_rvalue *rhs = read_rvalue(st, rhs_expr);
591 if (rhs == NULL) {
592 ir_read_error(st, NULL, "when reading right-hand side of assignment");
593 return NULL;
594 }
595
596 return new(ctx) ir_assignment(lhs, rhs, condition);
597 }
598
599 static ir_call *
600 read_call(_mesa_glsl_parse_state *st, s_list *list)
601 {
602 void *ctx = talloc_parent(st);
603 if (list->length() != 3) {
604 ir_read_error(st, list, "expected (call <name> (<param> ...))");
605 return NULL;
606 }
607
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> ...))");
612 return NULL;
613 }
614
615 exec_list parameters;
616
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);
620 if (param == NULL) {
621 ir_read_error(st, list, "when reading parameter to function call");
622 return NULL;
623 }
624 parameters.push_tail(param);
625 }
626
627 ir_function *f = st->symbols->get_function(name->value());
628 if (f == NULL) {
629 ir_read_error(st, list, "found call to undefined function %s",
630 name->value());
631 return NULL;
632 }
633
634 const ir_function_signature *callee = f->matching_signature(&parameters);
635 if (callee == NULL) {
636 ir_read_error(st, list, "couldn't find matching signature for function "
637 "%s", name->value());
638 return NULL;
639 }
640
641 return new(ctx) ir_call(callee, &parameters);
642 }
643
644 static ir_expression *
645 read_expression(_mesa_glsl_parse_state *st, s_list *list)
646 {
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>])");
652 return NULL;
653 }
654
655 s_expression *type_expr = (s_expression*) list->subexpressions.head->next;
656 const glsl_type *type = read_type(st, type_expr);
657 if (type == NULL)
658 return NULL;
659
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");
664 return NULL;
665 }
666
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());
670 return NULL;
671 }
672
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());
678 return NULL;
679 }
680 } else {
681 if (list_length != 4) {
682 ir_read_error(st, list, "expected (expression <type> %s <operand>)",
683 op_sym->value());
684 return NULL;
685 }
686 }
687
688 s_expression *exp1 = (s_expression*) (op_sym->next);
689 ir_rvalue *arg1 = read_rvalue(st, exp1);
690 if (arg1 == NULL) {
691 ir_read_error(st, NULL, "when reading first operand of %s",
692 op_sym->value());
693 return NULL;
694 }
695
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);
700 if (arg2 == NULL) {
701 ir_read_error(st, NULL, "when reading second operand of %s",
702 op_sym->value());
703 return NULL;
704 }
705 }
706
707 return new(ctx) ir_expression(op, type, arg1, arg2);
708 }
709
710 static ir_swizzle *
711 read_swizzle(_mesa_glsl_parse_state *st, s_list *list)
712 {
713 if (list->length() != 3) {
714 ir_read_error(st, list, "expected (swiz <swizzle> <rvalue>)");
715 return NULL;
716 }
717
718 s_symbol *swiz = SX_AS_SYMBOL(list->subexpressions.head->next);
719 if (swiz == NULL) {
720 ir_read_error(st, list, "expected a valid swizzle; found non-symbol");
721 return NULL;
722 }
723
724 if (strlen(swiz->value()) > 4) {
725 ir_read_error(st, list, "expected a valid swizzle; found %s",
726 swiz->value());
727 return NULL;
728 }
729
730 s_expression *sub = (s_expression*) swiz->next;
731 if (sub == NULL) {
732 ir_read_error(st, list, "expected rvalue: (swizzle %s <rvalue>)",
733 swiz->value());
734 return NULL;
735 }
736
737 ir_rvalue *rvalue = read_rvalue(st, sub);
738 if (rvalue == NULL)
739 return NULL;
740
741 ir_swizzle *ir = ir_swizzle::create(rvalue, swiz->value(),
742 rvalue->type->vector_elements);
743 if (ir == NULL)
744 ir_read_error(st, list, "invalid swizzle");
745
746 return ir;
747 }
748
749 static ir_constant *
750 read_constant(_mesa_glsl_parse_state *st, s_list *list)
751 {
752 void *ctx = talloc_parent(st);
753 if (list->length() != 3) {
754 ir_read_error(st, list, "expected (constant <type> (<num> ... <num>))");
755 return NULL;
756 }
757
758 s_expression *type_expr = (s_expression*) list->subexpressions.head->next;
759 const glsl_type *type = read_type(st, type_expr);
760 if (type == NULL)
761 return NULL;
762
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>))");
766 return NULL;
767 }
768
769 const glsl_type *const base_type = type->get_base_type();
770
771 ir_constant_data data;
772
773 // Read in list of values (at most 16).
774 int k = 0;
775 foreach_iter(exec_list_iterator, it, values->subexpressions) {
776 if (k >= 16) {
777 ir_read_error(st, values, "expected at most 16 numbers");
778 return NULL;
779 }
780
781 s_expression *expr = (s_expression*) it.get();
782
783 if (base_type->base_type == GLSL_TYPE_FLOAT) {
784 s_number *value = SX_AS_NUMBER(expr);
785 if (value == NULL) {
786 ir_read_error(st, values, "expected numbers");
787 return NULL;
788 }
789 data.f[k] = value->fvalue();
790 } else {
791 s_int *value = SX_AS_INT(expr);
792 if (value == NULL) {
793 ir_read_error(st, values, "expected integers");
794 return NULL;
795 }
796
797 switch (base_type->base_type) {
798 case GLSL_TYPE_UINT: {
799 data.u[k] = value->value();
800 break;
801 }
802 case GLSL_TYPE_INT: {
803 data.i[k] = value->value();
804 break;
805 }
806 case GLSL_TYPE_BOOL: {
807 data.b[k] = value->value();
808 break;
809 }
810 default:
811 ir_read_error(st, values, "unsupported constant type");
812 return NULL;
813 }
814 }
815 ++k;
816 }
817
818 return new(ctx) ir_constant(type, &data);
819 }
820
821 static ir_dereference *
822 read_dereference(_mesa_glsl_parse_state *st, s_expression *expr)
823 {
824 s_list *list = SX_AS_LIST(expr);
825 if (list == NULL || list->subexpressions.is_empty())
826 return NULL;
827
828 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.head);
829 assert(tag != NULL);
830
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);
837 return NULL;
838 }
839
840 static ir_dereference *
841 read_var_ref(_mesa_glsl_parse_state *st, s_list *list)
842 {
843 void *ctx = talloc_parent(st);
844 if (list->length() != 2) {
845 ir_read_error(st, list, "expected (var_ref <variable name>)");
846 return NULL;
847 }
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>)");
851 return NULL;
852 }
853
854 ir_variable *var = st->symbols->get_variable(var_name->value());
855 if (var == NULL) {
856 ir_read_error(st, list, "undeclared variable: %s", var_name->value());
857 return NULL;
858 }
859
860 return new(ctx) ir_dereference_variable(var);
861 }
862
863 static ir_dereference *
864 read_array_ref(_mesa_glsl_parse_state *st, s_list *list)
865 {
866 void *ctx = talloc_parent(st);
867 if (list->length() != 3) {
868 ir_read_error(st, list, "expected (array_ref <rvalue> <index>)");
869 return NULL;
870 }
871
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");
876 return NULL;
877 }
878
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);
882 }
883
884 static ir_dereference *
885 read_record_ref(_mesa_glsl_parse_state *st, s_list *list)
886 {
887 void *ctx = talloc_parent(st);
888 if (list->length() != 3) {
889 ir_read_error(st, list, "expected (record_ref <rvalue> <field>)");
890 return NULL;
891 }
892
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");
897 return NULL;
898 }
899
900 s_symbol *field = SX_AS_SYMBOL(subj_expr->next);
901 if (field == NULL) {
902 ir_read_error(st, list, "expected (record_ref ... <field name>)");
903 return NULL;
904 }
905 return new(ctx) ir_dereference_record(subject, field->value());
906 }
907
908 static bool
909 valid_texture_list_length(ir_texture_opcode op, s_list *list)
910 {
911 unsigned required_length = 7;
912 if (op == ir_txf)
913 required_length = 5;
914 else if (op == ir_tex)
915 required_length = 6;
916
917 return list->length() == required_length;
918 }
919
920 static ir_texture *
921 read_texture(_mesa_glsl_parse_state *st, s_list *list)
922 {
923 void *ctx = talloc_parent(st);
924 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.head);
925 assert(tag != NULL);
926
927 ir_texture_opcode op = ir_texture::get_opcode(tag->value());
928 if (op == (ir_texture_opcode) -1)
929 return NULL;
930
931 if (!valid_texture_list_length(op, list)) {
932 ir_read_error(st, NULL, "invalid list size in (%s ...)", tag->value());
933 return NULL;
934 }
935
936 ir_texture *tex = new(ctx) ir_texture(op);
937
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());
943 return NULL;
944 }
945 tex->set_sampler(sampler);
946
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 ...)",
952 tag->value());
953 return NULL;
954 }
955
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>)");
960 return NULL;
961 }
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>)");
967 return NULL;
968 }
969 tex->offsets[0] = offset_x->value();
970 tex->offsets[1] = offset_y->value();
971 tex->offsets[2] = offset_z->value();
972
973 if (op == ir_txf) {
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 ...)");
978 return NULL;
979 }
980 } else {
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;
985 } else {
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 ..)",
989 tag->value());
990 return NULL;
991 }
992 }
993
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");
997 return NULL;
998 }
999 if (shadow_list->subexpressions.is_empty()) {
1000 tex->shadow_comparitor= NULL;
1001 } else {
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 ..)",
1005 tag->value());
1006 return NULL;
1007 }
1008 }
1009 s_expression *lod_expr = (s_expression *) shadow_list->next;
1010
1011 switch (op) {
1012 case ir_txb:
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 ...)");
1016 return NULL;
1017 }
1018 break;
1019 case ir_txl:
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 ...)");
1023 return NULL;
1024 }
1025 break;
1026 case ir_txd: {
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 ...)");
1030 return NULL;
1031 }
1032 s_expression *dx_expr = (s_expression *) lod_list->subexpressions.head;
1033 s_expression *dy_expr = (s_expression *) dx_expr->next;
1034
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 ...)");
1038 return NULL;
1039 }
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 ...)");
1043 return NULL;
1044 }
1045 break;
1046 }
1047 default:
1048 // tex doesn't have any extra parameters and txf was handled earlier.
1049 break;
1050 };
1051 }
1052 return tex;
1053 }