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