ir_reader: Drop support for reading the old assignment format.
[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, bool scan_for_protos)
72 {
73 s_expression *expr = s_expression::read_expression(state, src);
74 if (expr == NULL) {
75 ir_read_error(state, NULL, "couldn't parse S-Expression.");
76 return;
77 }
78
79 if (scan_for_protos) {
80 scan_for_prototypes(state, instructions, expr);
81 if (state->error)
82 return;
83 }
84
85 read_instructions(state, instructions, expr, NULL);
86 talloc_free(expr);
87 }
88
89 static void
90 ir_read_error(_mesa_glsl_parse_state *state, s_expression *expr,
91 const char *fmt, ...)
92 {
93 va_list ap;
94
95 state->error = true;
96
97 if (state->current_function != NULL)
98 state->info_log = talloc_asprintf_append(state->info_log,
99 "In function %s:\n",
100 state->current_function->function_name());
101 state->info_log = talloc_strdup_append(state->info_log, "error: ");
102
103 va_start(ap, fmt);
104 state->info_log = talloc_vasprintf_append(state->info_log, fmt, ap);
105 va_end(ap);
106 state->info_log = talloc_strdup_append(state->info_log, "\n");
107
108 if (expr != NULL) {
109 state->info_log = talloc_strdup_append(state->info_log,
110 "...in this context:\n ");
111 expr->print();
112 state->info_log = talloc_strdup_append(state->info_log, "\n\n");
113 }
114 }
115
116 static const glsl_type *
117 read_type(_mesa_glsl_parse_state *st, s_expression *expr)
118 {
119 s_list *list = SX_AS_LIST(expr);
120 if (list != NULL) {
121 s_symbol *type_sym = SX_AS_SYMBOL(list->subexpressions.get_head());
122 if (type_sym == NULL) {
123 ir_read_error(st, expr, "expected type (array ...) or (struct ...)");
124 return NULL;
125 }
126 if (strcmp(type_sym->value(), "array") == 0) {
127 if (list->length() != 3) {
128 ir_read_error(st, expr, "expected type (array <type> <int>)");
129 return NULL;
130 }
131
132 // Read base type
133 s_expression *base_expr = (s_expression*) type_sym->next;
134 const glsl_type *base_type = read_type(st, base_expr);
135 if (base_type == NULL) {
136 ir_read_error(st, NULL, "when reading base type of array");
137 return NULL;
138 }
139
140 // Read array size
141 s_int *size = SX_AS_INT(base_expr->next);
142 if (size == NULL) {
143 ir_read_error(st, expr, "found non-integer array size");
144 return NULL;
145 }
146
147 return glsl_type::get_array_instance(base_type, size->value());
148 } else if (strcmp(type_sym->value(), "struct") == 0) {
149 assert(false); // FINISHME
150 } else {
151 ir_read_error(st, expr, "expected (array ...) or (struct ...); "
152 "found (%s ...)", type_sym->value());
153 return NULL;
154 }
155 }
156
157 s_symbol *type_sym = SX_AS_SYMBOL(expr);
158 if (type_sym == NULL) {
159 ir_read_error(st, expr, "expected <type> (symbol or list)");
160 return NULL;
161 }
162
163 const glsl_type *type = st->symbols->get_type(type_sym->value());
164 if (type == NULL)
165 ir_read_error(st, expr, "invalid type: %s", type_sym->value());
166
167 return type;
168 }
169
170
171 static void
172 scan_for_prototypes(_mesa_glsl_parse_state *st, exec_list *instructions,
173 s_expression *expr)
174 {
175 s_list *list = SX_AS_LIST(expr);
176 if (list == NULL) {
177 ir_read_error(st, expr, "Expected (<instruction> ...); found an atom.");
178 return;
179 }
180
181 foreach_iter(exec_list_iterator, it, list->subexpressions) {
182 s_list *sub = SX_AS_LIST(it.get());
183 if (sub == NULL)
184 continue; // not a (function ...); ignore it.
185
186 s_symbol *tag = SX_AS_SYMBOL(sub->subexpressions.get_head());
187 if (tag == NULL || strcmp(tag->value(), "function") != 0)
188 continue; // not a (function ...); ignore it.
189
190 ir_function *f = read_function(st, sub, true);
191 if (f == NULL)
192 return;
193 instructions->push_tail(f);
194 }
195 }
196
197 static ir_function *
198 read_function(_mesa_glsl_parse_state *st, s_list *list, bool skip_body)
199 {
200 void *ctx = st;
201 bool added = false;
202 if (list->length() < 3) {
203 ir_read_error(st, list, "Expected (function <name> (signature ...) ...)");
204 return NULL;
205 }
206
207 s_symbol *name = SX_AS_SYMBOL(list->subexpressions.head->next);
208 if (name == NULL) {
209 ir_read_error(st, list, "Expected (function <name> ...)");
210 return NULL;
211 }
212
213 ir_function *f = st->symbols->get_function(name->value());
214 if (f == NULL) {
215 f = new(ctx) ir_function(name->value());
216 f->is_builtin = true;
217 added = st->symbols->add_function(f->name, f);
218 assert(added);
219 }
220
221 exec_list_iterator it = list->subexpressions.iterator();
222 it.next(); // skip "function" tag
223 it.next(); // skip function name
224 for (/* nothing */; it.has_next(); it.next()) {
225 s_list *siglist = SX_AS_LIST(it.get());
226 if (siglist == NULL) {
227 ir_read_error(st, list, "Expected (function (signature ...) ...)");
228 return NULL;
229 }
230
231 s_symbol *tag = SX_AS_SYMBOL(siglist->subexpressions.get_head());
232 if (tag == NULL || strcmp(tag->value(), "signature") != 0) {
233 ir_read_error(st, siglist, "Expected (signature ...)");
234 return NULL;
235 }
236
237 read_function_sig(st, f, siglist, skip_body);
238 }
239 return added ? f : NULL;
240 }
241
242 static void
243 read_function_sig(_mesa_glsl_parse_state *st, ir_function *f, s_list *list,
244 bool skip_body)
245 {
246 void *ctx = st;
247 if (list->length() != 4) {
248 ir_read_error(st, list, "Expected (signature <type> (parameters ...) "
249 "(<instruction> ...))");
250 return;
251 }
252
253 s_expression *type_expr = (s_expression*) list->subexpressions.head->next;
254 const glsl_type *return_type = read_type(st, type_expr);
255 if (return_type == NULL)
256 return;
257
258 s_list *paramlist = SX_AS_LIST(type_expr->next);
259 s_list *body_list = SX_AS_LIST(paramlist->next);
260 if (paramlist == NULL || body_list == NULL) {
261 ir_read_error(st, list, "Expected (signature <type> (parameters ...) "
262 "(<instruction> ...))");
263 return;
264 }
265 s_symbol *paramtag = SX_AS_SYMBOL(paramlist->subexpressions.get_head());
266 if (paramtag == NULL || strcmp(paramtag->value(), "parameters") != 0) {
267 ir_read_error(st, paramlist, "Expected (parameters ...)");
268 return;
269 }
270
271 // Read the parameters list into a temporary place.
272 exec_list hir_parameters;
273 st->symbols->push_scope();
274
275 exec_list_iterator it = paramlist->subexpressions.iterator();
276 for (it.next() /* skip "parameters" */; it.has_next(); it.next()) {
277 s_list *decl = SX_AS_LIST(it.get());
278 ir_variable *var = read_declaration(st, decl);
279 if (var == NULL)
280 return;
281
282 hir_parameters.push_tail(var);
283 }
284
285 ir_function_signature *sig = f->exact_matching_signature(&hir_parameters);
286 if (sig == NULL && skip_body) {
287 /* If scanning for prototypes, generate a new signature. */
288 sig = new(ctx) ir_function_signature(return_type);
289 f->add_signature(sig);
290 } else if (sig != NULL) {
291 const char *badvar = sig->qualifiers_match(&hir_parameters);
292 if (badvar != NULL) {
293 ir_read_error(st, list, "function `%s' parameter `%s' qualifiers "
294 "don't match prototype", f->name, badvar);
295 return;
296 }
297
298 if (sig->return_type != return_type) {
299 ir_read_error(st, list, "function `%s' return type doesn't "
300 "match prototype", f->name);
301 return;
302 }
303 } else {
304 /* No prototype for this body exists - skip it. */
305 st->symbols->pop_scope();
306 return;
307 }
308 assert(sig != NULL);
309
310 sig->replace_parameters(&hir_parameters);
311
312 if (!skip_body && !body_list->subexpressions.is_empty()) {
313 if (sig->is_defined) {
314 ir_read_error(st, list, "function %s redefined", f->name);
315 return;
316 }
317 st->current_function = sig;
318 read_instructions(st, &sig->body, body_list, NULL);
319 st->current_function = NULL;
320 sig->is_defined = true;
321 }
322
323 st->symbols->pop_scope();
324 }
325
326 static void
327 read_instructions(_mesa_glsl_parse_state *st, exec_list *instructions,
328 s_expression *expr, ir_loop *loop_ctx)
329 {
330 // Read in a list of instructions
331 s_list *list = SX_AS_LIST(expr);
332 if (list == NULL) {
333 ir_read_error(st, expr, "Expected (<instruction> ...); found an atom.");
334 return;
335 }
336
337 foreach_iter(exec_list_iterator, it, list->subexpressions) {
338 s_expression *sub = (s_expression*) it.get();
339 ir_instruction *ir = read_instruction(st, sub, loop_ctx);
340 if (ir != NULL)
341 instructions->push_tail(ir);
342 }
343 }
344
345
346 static ir_instruction *
347 read_instruction(_mesa_glsl_parse_state *st, s_expression *expr,
348 ir_loop *loop_ctx)
349 {
350 void *ctx = st;
351 s_symbol *symbol = SX_AS_SYMBOL(expr);
352 if (symbol != NULL) {
353 if (strcmp(symbol->value(), "break") == 0 && loop_ctx != NULL)
354 return new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
355 if (strcmp(symbol->value(), "continue") == 0 && loop_ctx != NULL)
356 return new(ctx) ir_loop_jump(ir_loop_jump::jump_continue);
357 }
358
359 s_list *list = SX_AS_LIST(expr);
360 if (list == NULL || list->subexpressions.is_empty()) {
361 ir_read_error(st, expr, "Invalid instruction.\n");
362 return NULL;
363 }
364
365 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
366 if (tag == NULL) {
367 ir_read_error(st, expr, "expected instruction tag");
368 return NULL;
369 }
370
371 ir_instruction *inst = NULL;
372 if (strcmp(tag->value(), "declare") == 0) {
373 inst = read_declaration(st, list);
374 } else if (strcmp(tag->value(), "assign") == 0) {
375 inst = read_assignment(st, list);
376 } else if (strcmp(tag->value(), "if") == 0) {
377 inst = read_if(st, list, loop_ctx);
378 } else if (strcmp(tag->value(), "loop") == 0) {
379 inst = read_loop(st, list);
380 } else if (strcmp(tag->value(), "return") == 0) {
381 inst = read_return(st, list);
382 } else if (strcmp(tag->value(), "function") == 0) {
383 inst = read_function(st, list, false);
384 } else {
385 inst = read_rvalue(st, list);
386 if (inst == NULL)
387 ir_read_error(st, NULL, "when reading instruction");
388 }
389 return inst;
390 }
391
392
393 static ir_variable *
394 read_declaration(_mesa_glsl_parse_state *st, s_list *list)
395 {
396 void *ctx = st;
397 if (list->length() != 4) {
398 ir_read_error(st, list, "expected (declare (<qualifiers>) <type> "
399 "<name>)");
400 return NULL;
401 }
402
403 s_list *quals = SX_AS_LIST(list->subexpressions.head->next);
404 if (quals == NULL) {
405 ir_read_error(st, list, "expected a list of variable qualifiers");
406 return NULL;
407 }
408
409 s_expression *type_expr = (s_expression*) quals->next;
410 const glsl_type *type = read_type(st, type_expr);
411 if (type == NULL)
412 return NULL;
413
414 s_symbol *var_name = SX_AS_SYMBOL(type_expr->next);
415 if (var_name == NULL) {
416 ir_read_error(st, list, "expected variable name, found non-symbol");
417 return NULL;
418 }
419
420 ir_variable *var = new(ctx) ir_variable(type, var_name->value(),
421 ir_var_auto);
422
423 foreach_iter(exec_list_iterator, it, quals->subexpressions) {
424 s_symbol *qualifier = SX_AS_SYMBOL(it.get());
425 if (qualifier == NULL) {
426 ir_read_error(st, list, "qualifier list must contain only symbols");
427 delete var;
428 return NULL;
429 }
430
431 // FINISHME: Check for duplicate/conflicting qualifiers.
432 if (strcmp(qualifier->value(), "centroid") == 0) {
433 var->centroid = 1;
434 } else if (strcmp(qualifier->value(), "invariant") == 0) {
435 var->invariant = 1;
436 } else if (strcmp(qualifier->value(), "uniform") == 0) {
437 var->mode = ir_var_uniform;
438 } else if (strcmp(qualifier->value(), "auto") == 0) {
439 var->mode = ir_var_auto;
440 } else if (strcmp(qualifier->value(), "in") == 0) {
441 var->mode = ir_var_in;
442 } else if (strcmp(qualifier->value(), "out") == 0) {
443 var->mode = ir_var_out;
444 } else if (strcmp(qualifier->value(), "inout") == 0) {
445 var->mode = ir_var_inout;
446 } else if (strcmp(qualifier->value(), "smooth") == 0) {
447 var->interpolation = ir_var_smooth;
448 } else if (strcmp(qualifier->value(), "flat") == 0) {
449 var->interpolation = ir_var_flat;
450 } else if (strcmp(qualifier->value(), "noperspective") == 0) {
451 var->interpolation = ir_var_noperspective;
452 } else {
453 ir_read_error(st, list, "unknown qualifier: %s", qualifier->value());
454 delete var;
455 return NULL;
456 }
457 }
458
459 // Add the variable to the symbol table
460 st->symbols->add_variable(var->name, var);
461
462 return var;
463 }
464
465
466 static ir_if *
467 read_if(_mesa_glsl_parse_state *st, s_list *list, ir_loop *loop_ctx)
468 {
469 void *ctx = st;
470 if (list->length() != 4) {
471 ir_read_error(st, list, "expected (if <condition> (<then> ...) "
472 "(<else> ...))");
473 return NULL;
474 }
475
476 s_expression *cond_expr = (s_expression*) list->subexpressions.head->next;
477 ir_rvalue *condition = read_rvalue(st, cond_expr);
478 if (condition == NULL) {
479 ir_read_error(st, NULL, "when reading condition of (if ...)");
480 return NULL;
481 }
482
483 s_expression *then_expr = (s_expression*) cond_expr->next;
484 s_expression *else_expr = (s_expression*) then_expr->next;
485
486 ir_if *iff = new(ctx) ir_if(condition);
487
488 read_instructions(st, &iff->then_instructions, then_expr, loop_ctx);
489 read_instructions(st, &iff->else_instructions, else_expr, loop_ctx);
490 if (st->error) {
491 delete iff;
492 iff = NULL;
493 }
494 return iff;
495 }
496
497
498 static ir_loop *
499 read_loop(_mesa_glsl_parse_state *st, s_list *list)
500 {
501 void *ctx = st;
502 if (list->length() != 6) {
503 ir_read_error(st, list, "expected (loop <counter> <from> <to> "
504 "<increment> <body>)");
505 return NULL;
506 }
507
508 s_expression *count_expr = (s_expression*) list->subexpressions.head->next;
509 s_expression *from_expr = (s_expression*) count_expr->next;
510 s_expression *to_expr = (s_expression*) from_expr->next;
511 s_expression *inc_expr = (s_expression*) to_expr->next;
512 s_expression *body_expr = (s_expression*) inc_expr->next;
513
514 // FINISHME: actually read the count/from/to fields.
515
516 ir_loop *loop = new(ctx) ir_loop;
517 read_instructions(st, &loop->body_instructions, body_expr, loop);
518 if (st->error) {
519 delete loop;
520 loop = NULL;
521 }
522 return loop;
523 }
524
525
526 static ir_return *
527 read_return(_mesa_glsl_parse_state *st, s_list *list)
528 {
529 void *ctx = st;
530 if (list->length() != 2) {
531 ir_read_error(st, list, "expected (return <rvalue>)");
532 return NULL;
533 }
534
535 s_expression *expr = (s_expression*) list->subexpressions.head->next;
536
537 ir_rvalue *retval = read_rvalue(st, expr);
538 if (retval == NULL) {
539 ir_read_error(st, NULL, "when reading return value");
540 return NULL;
541 }
542
543 return new(ctx) ir_return(retval);
544 }
545
546
547 static ir_rvalue *
548 read_rvalue(_mesa_glsl_parse_state *st, s_expression *expr)
549 {
550 s_list *list = SX_AS_LIST(expr);
551 if (list == NULL || list->subexpressions.is_empty())
552 return NULL;
553
554 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
555 if (tag == NULL) {
556 ir_read_error(st, expr, "expected rvalue tag");
557 return NULL;
558 }
559
560 ir_rvalue *rvalue = read_dereference(st, list);
561 if (rvalue != NULL || st->error)
562 return rvalue;
563 else if (strcmp(tag->value(), "swiz") == 0) {
564 rvalue = read_swizzle(st, list);
565 } else if (strcmp(tag->value(), "expression") == 0) {
566 rvalue = read_expression(st, list);
567 } else if (strcmp(tag->value(), "call") == 0) {
568 rvalue = read_call(st, list);
569 } else if (strcmp(tag->value(), "constant") == 0) {
570 rvalue = read_constant(st, list);
571 } else {
572 rvalue = read_texture(st, list);
573 if (rvalue == NULL && !st->error)
574 ir_read_error(st, expr, "unrecognized rvalue tag: %s", tag->value());
575 }
576
577 return rvalue;
578 }
579
580 static ir_assignment *
581 read_assignment(_mesa_glsl_parse_state *st, s_list *list)
582 {
583 void *ctx = st;
584 if (list->length() != 5) {
585 ir_read_error(st, list, "expected (assign <condition> (<write mask>) "
586 "<lhs> <rhs>)");
587 return NULL;
588 }
589
590 s_expression *cond_expr = (s_expression*) list->subexpressions.head->next;
591 s_list *mask_list = SX_AS_LIST(cond_expr->next);
592 s_expression *lhs_expr = (s_expression*) cond_expr->next->next;
593 s_expression *rhs_expr = (s_expression*) lhs_expr->next;
594
595 ir_rvalue *condition = read_rvalue(st, cond_expr);
596 if (condition == NULL) {
597 ir_read_error(st, NULL, "when reading condition of assignment");
598 return NULL;
599 }
600
601 if (mask_list == NULL || mask_list->length() > 1) {
602 ir_read_error(st, mask_list, "expected () or (<write mask>)");
603 return NULL;
604 }
605
606 unsigned mask = 0;
607 if (mask_list->length() == 1) {
608 s_symbol *mask_symbol = SX_AS_SYMBOL(mask_list->subexpressions.head);
609 if (mask_symbol == NULL) {
610 ir_read_error(st, list, "expected a write mask; found non-symbol");
611 return NULL;
612 }
613
614 const char *mask_str = mask_symbol->value();
615 unsigned mask_length = strlen(mask_str);
616 if (mask_length > 4) {
617 ir_read_error(st, list, "invalid write mask: %s", mask_str);
618 return NULL;
619 }
620
621 const unsigned idx_map[] = { 3, 0, 1, 2 }; /* w=bit 3, x=0, y=1, z=2 */
622
623 for (unsigned i = 0; i < mask_length; i++) {
624 if (mask_str[i] < 'w' || mask_str[i] > 'z') {
625 ir_read_error(st, list, "write mask contains invalid character: %c",
626 mask_str[i]);
627 return NULL;
628 }
629 mask |= 1 << idx_map[mask_str[i] - 'w'];
630 }
631 }
632
633 ir_dereference *lhs = read_dereference(st, lhs_expr);
634 if (lhs == NULL) {
635 ir_read_error(st, NULL, "when reading left-hand side of assignment");
636 return NULL;
637 }
638
639 ir_rvalue *rhs = read_rvalue(st, rhs_expr);
640 if (rhs == NULL) {
641 ir_read_error(st, NULL, "when reading right-hand side of assignment");
642 return NULL;
643 }
644
645 if (mask == 0 && (lhs->type->is_vector() || lhs->type->is_scalar())) {
646 ir_read_error(st, list, "non-zero write mask required.");
647 return NULL;
648 }
649
650 return new(ctx) ir_assignment(lhs, rhs, condition, mask);
651 }
652
653 static ir_call *
654 read_call(_mesa_glsl_parse_state *st, s_list *list)
655 {
656 void *ctx = st;
657 if (list->length() != 3) {
658 ir_read_error(st, list, "expected (call <name> (<param> ...))");
659 return NULL;
660 }
661
662 s_symbol *name = SX_AS_SYMBOL(list->subexpressions.head->next);
663 s_list *params = SX_AS_LIST(name->next);
664 if (name == NULL || params == NULL) {
665 ir_read_error(st, list, "expected (call <name> (<param> ...))");
666 return NULL;
667 }
668
669 exec_list parameters;
670
671 foreach_iter(exec_list_iterator, it, params->subexpressions) {
672 s_expression *expr = (s_expression*) it.get();
673 ir_rvalue *param = read_rvalue(st, expr);
674 if (param == NULL) {
675 ir_read_error(st, list, "when reading parameter to function call");
676 return NULL;
677 }
678 parameters.push_tail(param);
679 }
680
681 ir_function *f = st->symbols->get_function(name->value());
682 if (f == NULL) {
683 ir_read_error(st, list, "found call to undefined function %s",
684 name->value());
685 return NULL;
686 }
687
688 ir_function_signature *callee = f->matching_signature(&parameters);
689 if (callee == NULL) {
690 ir_read_error(st, list, "couldn't find matching signature for function "
691 "%s", name->value());
692 return NULL;
693 }
694
695 return new(ctx) ir_call(callee, &parameters);
696 }
697
698 static ir_expression *
699 read_expression(_mesa_glsl_parse_state *st, s_list *list)
700 {
701 void *ctx = st;
702 const unsigned list_length = list->length();
703 if (list_length < 4) {
704 ir_read_error(st, list, "expected (expression <type> <operator> "
705 "<operand> [<operand>])");
706 return NULL;
707 }
708
709 s_expression *type_expr = (s_expression*) list->subexpressions.head->next;
710 const glsl_type *type = read_type(st, type_expr);
711 if (type == NULL)
712 return NULL;
713
714 /* Read the operator */
715 s_symbol *op_sym = SX_AS_SYMBOL(type_expr->next);
716 if (op_sym == NULL) {
717 ir_read_error(st, list, "expected operator, found non-symbol");
718 return NULL;
719 }
720
721 ir_expression_operation op = ir_expression::get_operator(op_sym->value());
722 if (op == (ir_expression_operation) -1) {
723 ir_read_error(st, list, "invalid operator: %s", op_sym->value());
724 return NULL;
725 }
726
727 /* Now that we know the operator, check for the right number of operands */
728 if (ir_expression::get_num_operands(op) == 2) {
729 if (list_length != 5) {
730 ir_read_error(st, list, "expected (expression <type> %s <operand> "
731 " <operand>)", op_sym->value());
732 return NULL;
733 }
734 } else {
735 if (list_length != 4) {
736 ir_read_error(st, list, "expected (expression <type> %s <operand>)",
737 op_sym->value());
738 return NULL;
739 }
740 }
741
742 s_expression *exp1 = (s_expression*) (op_sym->next);
743 ir_rvalue *arg1 = read_rvalue(st, exp1);
744 if (arg1 == NULL) {
745 ir_read_error(st, NULL, "when reading first operand of %s",
746 op_sym->value());
747 return NULL;
748 }
749
750 ir_rvalue *arg2 = NULL;
751 if (ir_expression::get_num_operands(op) == 2) {
752 s_expression *exp2 = (s_expression*) (exp1->next);
753 arg2 = read_rvalue(st, exp2);
754 if (arg2 == NULL) {
755 ir_read_error(st, NULL, "when reading second operand of %s",
756 op_sym->value());
757 return NULL;
758 }
759 }
760
761 return new(ctx) ir_expression(op, type, arg1, arg2);
762 }
763
764 static ir_swizzle *
765 read_swizzle(_mesa_glsl_parse_state *st, s_list *list)
766 {
767 if (list->length() != 3) {
768 ir_read_error(st, list, "expected (swiz <swizzle> <rvalue>)");
769 return NULL;
770 }
771
772 s_symbol *swiz = SX_AS_SYMBOL(list->subexpressions.head->next);
773 if (swiz == NULL) {
774 ir_read_error(st, list, "expected a valid swizzle; found non-symbol");
775 return NULL;
776 }
777
778 if (strlen(swiz->value()) > 4) {
779 ir_read_error(st, list, "expected a valid swizzle; found %s",
780 swiz->value());
781 return NULL;
782 }
783
784 s_expression *sub = (s_expression*) swiz->next;
785 if (sub == NULL) {
786 ir_read_error(st, list, "expected rvalue: (swizzle %s <rvalue>)",
787 swiz->value());
788 return NULL;
789 }
790
791 ir_rvalue *rvalue = read_rvalue(st, sub);
792 if (rvalue == NULL)
793 return NULL;
794
795 ir_swizzle *ir = ir_swizzle::create(rvalue, swiz->value(),
796 rvalue->type->vector_elements);
797 if (ir == NULL)
798 ir_read_error(st, list, "invalid swizzle");
799
800 return ir;
801 }
802
803 static ir_constant *
804 read_constant(_mesa_glsl_parse_state *st, s_list *list)
805 {
806 void *ctx = st;
807 if (list->length() != 3) {
808 ir_read_error(st, list, "expected (constant <type> (...))");
809 return NULL;
810 }
811
812 s_expression *type_expr = (s_expression*) list->subexpressions.head->next;
813 const glsl_type *type = read_type(st, type_expr);
814 if (type == NULL)
815 return NULL;
816
817 s_list *values = SX_AS_LIST(type_expr->next);
818 if (values == NULL) {
819 ir_read_error(st, list, "expected (constant <type> (...))");
820 return NULL;
821 }
822
823 if (type->is_array()) {
824 const unsigned elements_supplied = values->length();
825 if (elements_supplied != type->length) {
826 ir_read_error(st, values, "expected exactly %u array elements, "
827 "given %u", type->length, elements_supplied);
828 return NULL;
829 }
830
831 exec_list elements;
832 foreach_iter(exec_list_iterator, it, values->subexpressions) {
833 s_expression *expr = (s_expression *) it.get();
834 s_list *elt = SX_AS_LIST(expr);
835 if (elt == NULL) {
836 ir_read_error(st, expr, "expected (constant ...) array element");
837 return NULL;
838 }
839
840 ir_constant *ir_elt = read_constant(st, elt);
841 if (ir_elt == NULL)
842 return NULL;
843 elements.push_tail(ir_elt);
844 }
845 return new(ctx) ir_constant(type, &elements);
846 }
847
848 const glsl_type *const base_type = type->get_base_type();
849
850 ir_constant_data data = { { 0 } };
851
852 // Read in list of values (at most 16).
853 int k = 0;
854 foreach_iter(exec_list_iterator, it, values->subexpressions) {
855 if (k >= 16) {
856 ir_read_error(st, values, "expected at most 16 numbers");
857 return NULL;
858 }
859
860 s_expression *expr = (s_expression*) it.get();
861
862 if (base_type->base_type == GLSL_TYPE_FLOAT) {
863 s_number *value = SX_AS_NUMBER(expr);
864 if (value == NULL) {
865 ir_read_error(st, values, "expected numbers");
866 return NULL;
867 }
868 data.f[k] = value->fvalue();
869 } else {
870 s_int *value = SX_AS_INT(expr);
871 if (value == NULL) {
872 ir_read_error(st, values, "expected integers");
873 return NULL;
874 }
875
876 switch (base_type->base_type) {
877 case GLSL_TYPE_UINT: {
878 data.u[k] = value->value();
879 break;
880 }
881 case GLSL_TYPE_INT: {
882 data.i[k] = value->value();
883 break;
884 }
885 case GLSL_TYPE_BOOL: {
886 data.b[k] = value->value();
887 break;
888 }
889 default:
890 ir_read_error(st, values, "unsupported constant type");
891 return NULL;
892 }
893 }
894 ++k;
895 }
896
897 return new(ctx) ir_constant(type, &data);
898 }
899
900 static ir_dereference *
901 read_dereference(_mesa_glsl_parse_state *st, s_expression *expr)
902 {
903 s_list *list = SX_AS_LIST(expr);
904 if (list == NULL || list->subexpressions.is_empty())
905 return NULL;
906
907 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.head);
908 assert(tag != NULL);
909
910 if (strcmp(tag->value(), "var_ref") == 0)
911 return read_var_ref(st, list);
912 if (strcmp(tag->value(), "array_ref") == 0)
913 return read_array_ref(st, list);
914 if (strcmp(tag->value(), "record_ref") == 0)
915 return read_record_ref(st, list);
916 return NULL;
917 }
918
919 static ir_dereference *
920 read_var_ref(_mesa_glsl_parse_state *st, s_list *list)
921 {
922 void *ctx = st;
923 if (list->length() != 2) {
924 ir_read_error(st, list, "expected (var_ref <variable name>)");
925 return NULL;
926 }
927 s_symbol *var_name = SX_AS_SYMBOL(list->subexpressions.head->next);
928 if (var_name == NULL) {
929 ir_read_error(st, list, "expected (var_ref <variable name>)");
930 return NULL;
931 }
932
933 ir_variable *var = st->symbols->get_variable(var_name->value());
934 if (var == NULL) {
935 ir_read_error(st, list, "undeclared variable: %s", var_name->value());
936 return NULL;
937 }
938
939 return new(ctx) ir_dereference_variable(var);
940 }
941
942 static ir_dereference *
943 read_array_ref(_mesa_glsl_parse_state *st, s_list *list)
944 {
945 void *ctx = st;
946 if (list->length() != 3) {
947 ir_read_error(st, list, "expected (array_ref <rvalue> <index>)");
948 return NULL;
949 }
950
951 s_expression *subj_expr = (s_expression*) list->subexpressions.head->next;
952 ir_rvalue *subject = read_rvalue(st, subj_expr);
953 if (subject == NULL) {
954 ir_read_error(st, NULL, "when reading the subject of an array_ref");
955 return NULL;
956 }
957
958 s_expression *idx_expr = (s_expression*) subj_expr->next;
959 ir_rvalue *idx = read_rvalue(st, idx_expr);
960 return new(ctx) ir_dereference_array(subject, idx);
961 }
962
963 static ir_dereference *
964 read_record_ref(_mesa_glsl_parse_state *st, s_list *list)
965 {
966 void *ctx = st;
967 if (list->length() != 3) {
968 ir_read_error(st, list, "expected (record_ref <rvalue> <field>)");
969 return NULL;
970 }
971
972 s_expression *subj_expr = (s_expression*) list->subexpressions.head->next;
973 ir_rvalue *subject = read_rvalue(st, subj_expr);
974 if (subject == NULL) {
975 ir_read_error(st, NULL, "when reading the subject of a record_ref");
976 return NULL;
977 }
978
979 s_symbol *field = SX_AS_SYMBOL(subj_expr->next);
980 if (field == NULL) {
981 ir_read_error(st, list, "expected (record_ref ... <field name>)");
982 return NULL;
983 }
984 return new(ctx) ir_dereference_record(subject, field->value());
985 }
986
987 static bool
988 valid_texture_list_length(ir_texture_opcode op, s_list *list)
989 {
990 unsigned required_length = 7;
991 if (op == ir_txf)
992 required_length = 5;
993 else if (op == ir_tex)
994 required_length = 6;
995
996 return list->length() == required_length;
997 }
998
999 static ir_texture *
1000 read_texture(_mesa_glsl_parse_state *st, s_list *list)
1001 {
1002 void *ctx = st;
1003 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.head);
1004 assert(tag != NULL);
1005
1006 ir_texture_opcode op = ir_texture::get_opcode(tag->value());
1007 if (op == (ir_texture_opcode) -1)
1008 return NULL;
1009
1010 if (!valid_texture_list_length(op, list)) {
1011 ir_read_error(st, NULL, "invalid list size in (%s ...)", tag->value());
1012 return NULL;
1013 }
1014
1015 ir_texture *tex = new(ctx) ir_texture(op);
1016
1017 // Read sampler (must be a deref)
1018 s_expression *sampler_expr = (s_expression *) tag->next;
1019 ir_dereference *sampler = read_dereference(st, sampler_expr);
1020 if (sampler == NULL) {
1021 ir_read_error(st, NULL, "when reading sampler in (%s ...)", tag->value());
1022 return NULL;
1023 }
1024 tex->set_sampler(sampler);
1025
1026 // Read coordinate (any rvalue)
1027 s_expression *coordinate_expr = (s_expression *) sampler_expr->next;
1028 tex->coordinate = read_rvalue(st, coordinate_expr);
1029 if (tex->coordinate == NULL) {
1030 ir_read_error(st, NULL, "when reading coordinate in (%s ...)",
1031 tag->value());
1032 return NULL;
1033 }
1034
1035 // Read texel offset, i.e. (0 0 0)
1036 s_list *offset_list = SX_AS_LIST(coordinate_expr->next);
1037 if (offset_list == NULL || offset_list->length() != 3) {
1038 ir_read_error(st, offset_list, "expected (<int> <int> <int>)");
1039 return NULL;
1040 }
1041 s_int *offset_x = SX_AS_INT(offset_list->subexpressions.head);
1042 s_int *offset_y = SX_AS_INT(offset_x->next);
1043 s_int *offset_z = SX_AS_INT(offset_y->next);
1044 if (offset_x == NULL || offset_y == NULL || offset_z == NULL) {
1045 ir_read_error(st, offset_list, "expected (<int> <int> <int>)");
1046 return NULL;
1047 }
1048 tex->offsets[0] = offset_x->value();
1049 tex->offsets[1] = offset_y->value();
1050 tex->offsets[2] = offset_z->value();
1051
1052 if (op == ir_txf) {
1053 s_expression *lod_expr = (s_expression *) offset_list->next;
1054 tex->lod_info.lod = read_rvalue(st, lod_expr);
1055 if (tex->lod_info.lod == NULL) {
1056 ir_read_error(st, NULL, "when reading LOD in (txf ...)");
1057 return NULL;
1058 }
1059 } else {
1060 s_expression *proj_expr = (s_expression *) offset_list->next;
1061 s_int *proj_as_int = SX_AS_INT(proj_expr);
1062 if (proj_as_int && proj_as_int->value() == 1) {
1063 tex->projector = NULL;
1064 } else {
1065 tex->projector = read_rvalue(st, proj_expr);
1066 if (tex->projector == NULL) {
1067 ir_read_error(st, NULL, "when reading projective divide in (%s ..)",
1068 tag->value());
1069 return NULL;
1070 }
1071 }
1072
1073 s_list *shadow_list = SX_AS_LIST(proj_expr->next);
1074 if (shadow_list == NULL) {
1075 ir_read_error(st, NULL, "shadow comparitor must be a list");
1076 return NULL;
1077 }
1078 if (shadow_list->subexpressions.is_empty()) {
1079 tex->shadow_comparitor= NULL;
1080 } else {
1081 tex->shadow_comparitor = read_rvalue(st, shadow_list);
1082 if (tex->shadow_comparitor == NULL) {
1083 ir_read_error(st, NULL, "when reading shadow comparitor in (%s ..)",
1084 tag->value());
1085 return NULL;
1086 }
1087 }
1088 s_expression *lod_expr = (s_expression *) shadow_list->next;
1089
1090 switch (op) {
1091 case ir_txb:
1092 tex->lod_info.bias = read_rvalue(st, lod_expr);
1093 if (tex->lod_info.bias == NULL) {
1094 ir_read_error(st, NULL, "when reading LOD bias in (txb ...)");
1095 return NULL;
1096 }
1097 break;
1098 case ir_txl:
1099 tex->lod_info.lod = read_rvalue(st, lod_expr);
1100 if (tex->lod_info.lod == NULL) {
1101 ir_read_error(st, NULL, "when reading LOD in (txl ...)");
1102 return NULL;
1103 }
1104 break;
1105 case ir_txd: {
1106 s_list *lod_list = SX_AS_LIST(lod_expr);
1107 if (lod_list->length() != 2) {
1108 ir_read_error(st, lod_expr, "expected (dPdx dPdy) in (txd ...)");
1109 return NULL;
1110 }
1111 s_expression *dx_expr = (s_expression *) lod_list->subexpressions.head;
1112 s_expression *dy_expr = (s_expression *) dx_expr->next;
1113
1114 tex->lod_info.grad.dPdx = read_rvalue(st, dx_expr);
1115 if (tex->lod_info.grad.dPdx == NULL) {
1116 ir_read_error(st, NULL, "when reading dPdx in (txd ...)");
1117 return NULL;
1118 }
1119 tex->lod_info.grad.dPdy = read_rvalue(st, dy_expr);
1120 if (tex->lod_info.grad.dPdy == NULL) {
1121 ir_read_error(st, NULL, "when reading dPdy in (txd ...)");
1122 return NULL;
1123 }
1124 break;
1125 }
1126 default:
1127 // tex doesn't have any extra parameters and txf was handled earlier.
1128 break;
1129 };
1130 }
1131 return tex;
1132 }