c94d4b44138bc3a431be66110fa1561d51cc7ea6
[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 const unsigned list_length = list->length();
585 if (list_length < 4 || list_length > 5) {
586 ir_read_error(st, list, "expected (assign <condition> (<write mask>) "
587 "<lhs> <rhs>)");
588 return NULL;
589 }
590
591 s_expression *cond_expr = (s_expression*) list->subexpressions.head->next;
592 s_list *mask_list = SX_AS_LIST(cond_expr->next);
593 s_expression *lhs_expr = (s_expression*)
594 (list_length == 4 ? cond_expr->next : cond_expr->next->next);
595 s_expression *rhs_expr = (s_expression*) lhs_expr->next;
596
597 ir_rvalue *condition = read_rvalue(st, cond_expr);
598 if (condition == NULL) {
599 ir_read_error(st, NULL, "when reading condition of assignment");
600 return NULL;
601 }
602
603 if (list_length == 5 && mask_list == NULL || mask_list->length() > 1) {
604 ir_read_error(st, mask_list, "expected () or (<write mask>)");
605 return NULL;
606 }
607
608 unsigned mask = 0;
609 if (list_length == 5 && mask_list->length() == 1) {
610 s_symbol *mask_symbol = SX_AS_SYMBOL(mask_list->subexpressions.head);
611 if (mask_symbol == NULL) {
612 ir_read_error(st, list, "expected a write mask; found non-symbol");
613 return NULL;
614 }
615
616 const char *mask_str = mask_symbol->value();
617 unsigned mask_length = strlen(mask_str);
618 if (mask_length > 4) {
619 ir_read_error(st, list, "invalid write mask: %s", mask_str);
620 return NULL;
621 }
622
623 const unsigned idx_map[] = { 3, 0, 1, 2 }; /* w=bit 3, x=0, y=1, z=2 */
624
625 for (unsigned i = 0; i < mask_length; i++) {
626 if (mask_str[i] < 'w' || mask_str[i] > 'z') {
627 ir_read_error(st, list, "write mask contains invalid character: %c",
628 mask_str[i]);
629 return NULL;
630 }
631 mask |= 1 << idx_map[mask_str[i] - 'w'];
632 }
633 }
634
635 ir_dereference *lhs = read_dereference(st, lhs_expr);
636 if (lhs == NULL) {
637 ir_read_error(st, NULL, "when reading left-hand side of assignment");
638 return NULL;
639 }
640
641 ir_rvalue *rhs = read_rvalue(st, rhs_expr);
642 if (rhs == NULL) {
643 ir_read_error(st, NULL, "when reading right-hand side of assignment");
644 return NULL;
645 }
646
647 if (mask == 0 && (lhs->type->is_vector() || lhs->type->is_scalar())) {
648 ir_read_error(st, list, "non-zero write mask required.");
649 return NULL;
650 }
651
652 return new(ctx) ir_assignment(lhs, rhs, condition, mask);
653 }
654
655 static ir_call *
656 read_call(_mesa_glsl_parse_state *st, s_list *list)
657 {
658 void *ctx = st;
659 if (list->length() != 3) {
660 ir_read_error(st, list, "expected (call <name> (<param> ...))");
661 return NULL;
662 }
663
664 s_symbol *name = SX_AS_SYMBOL(list->subexpressions.head->next);
665 s_list *params = SX_AS_LIST(name->next);
666 if (name == NULL || params == NULL) {
667 ir_read_error(st, list, "expected (call <name> (<param> ...))");
668 return NULL;
669 }
670
671 exec_list parameters;
672
673 foreach_iter(exec_list_iterator, it, params->subexpressions) {
674 s_expression *expr = (s_expression*) it.get();
675 ir_rvalue *param = read_rvalue(st, expr);
676 if (param == NULL) {
677 ir_read_error(st, list, "when reading parameter to function call");
678 return NULL;
679 }
680 parameters.push_tail(param);
681 }
682
683 ir_function *f = st->symbols->get_function(name->value());
684 if (f == NULL) {
685 ir_read_error(st, list, "found call to undefined function %s",
686 name->value());
687 return NULL;
688 }
689
690 ir_function_signature *callee = f->matching_signature(&parameters);
691 if (callee == NULL) {
692 ir_read_error(st, list, "couldn't find matching signature for function "
693 "%s", name->value());
694 return NULL;
695 }
696
697 return new(ctx) ir_call(callee, &parameters);
698 }
699
700 static ir_expression *
701 read_expression(_mesa_glsl_parse_state *st, s_list *list)
702 {
703 void *ctx = st;
704 const unsigned list_length = list->length();
705 if (list_length < 4) {
706 ir_read_error(st, list, "expected (expression <type> <operator> "
707 "<operand> [<operand>])");
708 return NULL;
709 }
710
711 s_expression *type_expr = (s_expression*) list->subexpressions.head->next;
712 const glsl_type *type = read_type(st, type_expr);
713 if (type == NULL)
714 return NULL;
715
716 /* Read the operator */
717 s_symbol *op_sym = SX_AS_SYMBOL(type_expr->next);
718 if (op_sym == NULL) {
719 ir_read_error(st, list, "expected operator, found non-symbol");
720 return NULL;
721 }
722
723 ir_expression_operation op = ir_expression::get_operator(op_sym->value());
724 if (op == (ir_expression_operation) -1) {
725 ir_read_error(st, list, "invalid operator: %s", op_sym->value());
726 return NULL;
727 }
728
729 /* Now that we know the operator, check for the right number of operands */
730 if (ir_expression::get_num_operands(op) == 2) {
731 if (list_length != 5) {
732 ir_read_error(st, list, "expected (expression <type> %s <operand> "
733 " <operand>)", op_sym->value());
734 return NULL;
735 }
736 } else {
737 if (list_length != 4) {
738 ir_read_error(st, list, "expected (expression <type> %s <operand>)",
739 op_sym->value());
740 return NULL;
741 }
742 }
743
744 s_expression *exp1 = (s_expression*) (op_sym->next);
745 ir_rvalue *arg1 = read_rvalue(st, exp1);
746 if (arg1 == NULL) {
747 ir_read_error(st, NULL, "when reading first operand of %s",
748 op_sym->value());
749 return NULL;
750 }
751
752 ir_rvalue *arg2 = NULL;
753 if (ir_expression::get_num_operands(op) == 2) {
754 s_expression *exp2 = (s_expression*) (exp1->next);
755 arg2 = read_rvalue(st, exp2);
756 if (arg2 == NULL) {
757 ir_read_error(st, NULL, "when reading second operand of %s",
758 op_sym->value());
759 return NULL;
760 }
761 }
762
763 return new(ctx) ir_expression(op, type, arg1, arg2);
764 }
765
766 static ir_swizzle *
767 read_swizzle(_mesa_glsl_parse_state *st, s_list *list)
768 {
769 if (list->length() != 3) {
770 ir_read_error(st, list, "expected (swiz <swizzle> <rvalue>)");
771 return NULL;
772 }
773
774 s_symbol *swiz = SX_AS_SYMBOL(list->subexpressions.head->next);
775 if (swiz == NULL) {
776 ir_read_error(st, list, "expected a valid swizzle; found non-symbol");
777 return NULL;
778 }
779
780 if (strlen(swiz->value()) > 4) {
781 ir_read_error(st, list, "expected a valid swizzle; found %s",
782 swiz->value());
783 return NULL;
784 }
785
786 s_expression *sub = (s_expression*) swiz->next;
787 if (sub == NULL) {
788 ir_read_error(st, list, "expected rvalue: (swizzle %s <rvalue>)",
789 swiz->value());
790 return NULL;
791 }
792
793 ir_rvalue *rvalue = read_rvalue(st, sub);
794 if (rvalue == NULL)
795 return NULL;
796
797 ir_swizzle *ir = ir_swizzle::create(rvalue, swiz->value(),
798 rvalue->type->vector_elements);
799 if (ir == NULL)
800 ir_read_error(st, list, "invalid swizzle");
801
802 return ir;
803 }
804
805 static ir_constant *
806 read_constant(_mesa_glsl_parse_state *st, s_list *list)
807 {
808 void *ctx = st;
809 if (list->length() != 3) {
810 ir_read_error(st, list, "expected (constant <type> (...))");
811 return NULL;
812 }
813
814 s_expression *type_expr = (s_expression*) list->subexpressions.head->next;
815 const glsl_type *type = read_type(st, type_expr);
816 if (type == NULL)
817 return NULL;
818
819 s_list *values = SX_AS_LIST(type_expr->next);
820 if (values == NULL) {
821 ir_read_error(st, list, "expected (constant <type> (...))");
822 return NULL;
823 }
824
825 if (type->is_array()) {
826 const unsigned elements_supplied = values->length();
827 if (elements_supplied != type->length) {
828 ir_read_error(st, values, "expected exactly %u array elements, "
829 "given %u", type->length, elements_supplied);
830 return NULL;
831 }
832
833 exec_list elements;
834 foreach_iter(exec_list_iterator, it, values->subexpressions) {
835 s_expression *expr = (s_expression *) it.get();
836 s_list *elt = SX_AS_LIST(expr);
837 if (elt == NULL) {
838 ir_read_error(st, expr, "expected (constant ...) array element");
839 return NULL;
840 }
841
842 ir_constant *ir_elt = read_constant(st, elt);
843 if (ir_elt == NULL)
844 return NULL;
845 elements.push_tail(ir_elt);
846 }
847 return new(ctx) ir_constant(type, &elements);
848 }
849
850 const glsl_type *const base_type = type->get_base_type();
851
852 ir_constant_data data = { { 0 } };
853
854 // Read in list of values (at most 16).
855 int k = 0;
856 foreach_iter(exec_list_iterator, it, values->subexpressions) {
857 if (k >= 16) {
858 ir_read_error(st, values, "expected at most 16 numbers");
859 return NULL;
860 }
861
862 s_expression *expr = (s_expression*) it.get();
863
864 if (base_type->base_type == GLSL_TYPE_FLOAT) {
865 s_number *value = SX_AS_NUMBER(expr);
866 if (value == NULL) {
867 ir_read_error(st, values, "expected numbers");
868 return NULL;
869 }
870 data.f[k] = value->fvalue();
871 } else {
872 s_int *value = SX_AS_INT(expr);
873 if (value == NULL) {
874 ir_read_error(st, values, "expected integers");
875 return NULL;
876 }
877
878 switch (base_type->base_type) {
879 case GLSL_TYPE_UINT: {
880 data.u[k] = value->value();
881 break;
882 }
883 case GLSL_TYPE_INT: {
884 data.i[k] = value->value();
885 break;
886 }
887 case GLSL_TYPE_BOOL: {
888 data.b[k] = value->value();
889 break;
890 }
891 default:
892 ir_read_error(st, values, "unsupported constant type");
893 return NULL;
894 }
895 }
896 ++k;
897 }
898
899 return new(ctx) ir_constant(type, &data);
900 }
901
902 static ir_dereference *
903 read_dereference(_mesa_glsl_parse_state *st, s_expression *expr)
904 {
905 s_list *list = SX_AS_LIST(expr);
906 if (list == NULL || list->subexpressions.is_empty())
907 return NULL;
908
909 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.head);
910 assert(tag != NULL);
911
912 if (strcmp(tag->value(), "var_ref") == 0)
913 return read_var_ref(st, list);
914 if (strcmp(tag->value(), "array_ref") == 0)
915 return read_array_ref(st, list);
916 if (strcmp(tag->value(), "record_ref") == 0)
917 return read_record_ref(st, list);
918 return NULL;
919 }
920
921 static ir_dereference *
922 read_var_ref(_mesa_glsl_parse_state *st, s_list *list)
923 {
924 void *ctx = st;
925 if (list->length() != 2) {
926 ir_read_error(st, list, "expected (var_ref <variable name>)");
927 return NULL;
928 }
929 s_symbol *var_name = SX_AS_SYMBOL(list->subexpressions.head->next);
930 if (var_name == NULL) {
931 ir_read_error(st, list, "expected (var_ref <variable name>)");
932 return NULL;
933 }
934
935 ir_variable *var = st->symbols->get_variable(var_name->value());
936 if (var == NULL) {
937 ir_read_error(st, list, "undeclared variable: %s", var_name->value());
938 return NULL;
939 }
940
941 return new(ctx) ir_dereference_variable(var);
942 }
943
944 static ir_dereference *
945 read_array_ref(_mesa_glsl_parse_state *st, s_list *list)
946 {
947 void *ctx = st;
948 if (list->length() != 3) {
949 ir_read_error(st, list, "expected (array_ref <rvalue> <index>)");
950 return NULL;
951 }
952
953 s_expression *subj_expr = (s_expression*) list->subexpressions.head->next;
954 ir_rvalue *subject = read_rvalue(st, subj_expr);
955 if (subject == NULL) {
956 ir_read_error(st, NULL, "when reading the subject of an array_ref");
957 return NULL;
958 }
959
960 s_expression *idx_expr = (s_expression*) subj_expr->next;
961 ir_rvalue *idx = read_rvalue(st, idx_expr);
962 return new(ctx) ir_dereference_array(subject, idx);
963 }
964
965 static ir_dereference *
966 read_record_ref(_mesa_glsl_parse_state *st, s_list *list)
967 {
968 void *ctx = st;
969 if (list->length() != 3) {
970 ir_read_error(st, list, "expected (record_ref <rvalue> <field>)");
971 return NULL;
972 }
973
974 s_expression *subj_expr = (s_expression*) list->subexpressions.head->next;
975 ir_rvalue *subject = read_rvalue(st, subj_expr);
976 if (subject == NULL) {
977 ir_read_error(st, NULL, "when reading the subject of a record_ref");
978 return NULL;
979 }
980
981 s_symbol *field = SX_AS_SYMBOL(subj_expr->next);
982 if (field == NULL) {
983 ir_read_error(st, list, "expected (record_ref ... <field name>)");
984 return NULL;
985 }
986 return new(ctx) ir_dereference_record(subject, field->value());
987 }
988
989 static bool
990 valid_texture_list_length(ir_texture_opcode op, s_list *list)
991 {
992 unsigned required_length = 7;
993 if (op == ir_txf)
994 required_length = 5;
995 else if (op == ir_tex)
996 required_length = 6;
997
998 return list->length() == required_length;
999 }
1000
1001 static ir_texture *
1002 read_texture(_mesa_glsl_parse_state *st, s_list *list)
1003 {
1004 void *ctx = st;
1005 s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.head);
1006 assert(tag != NULL);
1007
1008 ir_texture_opcode op = ir_texture::get_opcode(tag->value());
1009 if (op == (ir_texture_opcode) -1)
1010 return NULL;
1011
1012 if (!valid_texture_list_length(op, list)) {
1013 ir_read_error(st, NULL, "invalid list size in (%s ...)", tag->value());
1014 return NULL;
1015 }
1016
1017 ir_texture *tex = new(ctx) ir_texture(op);
1018
1019 // Read sampler (must be a deref)
1020 s_expression *sampler_expr = (s_expression *) tag->next;
1021 ir_dereference *sampler = read_dereference(st, sampler_expr);
1022 if (sampler == NULL) {
1023 ir_read_error(st, NULL, "when reading sampler in (%s ...)", tag->value());
1024 return NULL;
1025 }
1026 tex->set_sampler(sampler);
1027
1028 // Read coordinate (any rvalue)
1029 s_expression *coordinate_expr = (s_expression *) sampler_expr->next;
1030 tex->coordinate = read_rvalue(st, coordinate_expr);
1031 if (tex->coordinate == NULL) {
1032 ir_read_error(st, NULL, "when reading coordinate in (%s ...)",
1033 tag->value());
1034 return NULL;
1035 }
1036
1037 // Read texel offset, i.e. (0 0 0)
1038 s_list *offset_list = SX_AS_LIST(coordinate_expr->next);
1039 if (offset_list == NULL || offset_list->length() != 3) {
1040 ir_read_error(st, offset_list, "expected (<int> <int> <int>)");
1041 return NULL;
1042 }
1043 s_int *offset_x = SX_AS_INT(offset_list->subexpressions.head);
1044 s_int *offset_y = SX_AS_INT(offset_x->next);
1045 s_int *offset_z = SX_AS_INT(offset_y->next);
1046 if (offset_x == NULL || offset_y == NULL || offset_z == NULL) {
1047 ir_read_error(st, offset_list, "expected (<int> <int> <int>)");
1048 return NULL;
1049 }
1050 tex->offsets[0] = offset_x->value();
1051 tex->offsets[1] = offset_y->value();
1052 tex->offsets[2] = offset_z->value();
1053
1054 if (op == ir_txf) {
1055 s_expression *lod_expr = (s_expression *) offset_list->next;
1056 tex->lod_info.lod = read_rvalue(st, lod_expr);
1057 if (tex->lod_info.lod == NULL) {
1058 ir_read_error(st, NULL, "when reading LOD in (txf ...)");
1059 return NULL;
1060 }
1061 } else {
1062 s_expression *proj_expr = (s_expression *) offset_list->next;
1063 s_int *proj_as_int = SX_AS_INT(proj_expr);
1064 if (proj_as_int && proj_as_int->value() == 1) {
1065 tex->projector = NULL;
1066 } else {
1067 tex->projector = read_rvalue(st, proj_expr);
1068 if (tex->projector == NULL) {
1069 ir_read_error(st, NULL, "when reading projective divide in (%s ..)",
1070 tag->value());
1071 return NULL;
1072 }
1073 }
1074
1075 s_list *shadow_list = SX_AS_LIST(proj_expr->next);
1076 if (shadow_list == NULL) {
1077 ir_read_error(st, NULL, "shadow comparitor must be a list");
1078 return NULL;
1079 }
1080 if (shadow_list->subexpressions.is_empty()) {
1081 tex->shadow_comparitor= NULL;
1082 } else {
1083 tex->shadow_comparitor = read_rvalue(st, shadow_list);
1084 if (tex->shadow_comparitor == NULL) {
1085 ir_read_error(st, NULL, "when reading shadow comparitor in (%s ..)",
1086 tag->value());
1087 return NULL;
1088 }
1089 }
1090 s_expression *lod_expr = (s_expression *) shadow_list->next;
1091
1092 switch (op) {
1093 case ir_txb:
1094 tex->lod_info.bias = read_rvalue(st, lod_expr);
1095 if (tex->lod_info.bias == NULL) {
1096 ir_read_error(st, NULL, "when reading LOD bias in (txb ...)");
1097 return NULL;
1098 }
1099 break;
1100 case ir_txl:
1101 tex->lod_info.lod = read_rvalue(st, lod_expr);
1102 if (tex->lod_info.lod == NULL) {
1103 ir_read_error(st, NULL, "when reading LOD in (txl ...)");
1104 return NULL;
1105 }
1106 break;
1107 case ir_txd: {
1108 s_list *lod_list = SX_AS_LIST(lod_expr);
1109 if (lod_list->length() != 2) {
1110 ir_read_error(st, lod_expr, "expected (dPdx dPdy) in (txd ...)");
1111 return NULL;
1112 }
1113 s_expression *dx_expr = (s_expression *) lod_list->subexpressions.head;
1114 s_expression *dy_expr = (s_expression *) dx_expr->next;
1115
1116 tex->lod_info.grad.dPdx = read_rvalue(st, dx_expr);
1117 if (tex->lod_info.grad.dPdx == NULL) {
1118 ir_read_error(st, NULL, "when reading dPdx in (txd ...)");
1119 return NULL;
1120 }
1121 tex->lod_info.grad.dPdy = read_rvalue(st, dy_expr);
1122 if (tex->lod_info.grad.dPdy == NULL) {
1123 ir_read_error(st, NULL, "when reading dPdy in (txd ...)");
1124 return NULL;
1125 }
1126 break;
1127 }
1128 default:
1129 // tex doesn't have any extra parameters and txf was handled earlier.
1130 break;
1131 };
1132 }
1133 return tex;
1134 }