glsl: Change texel offsets to a single vector rvalue.
[profile/ivi/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 "ir_reader.h"
25 #include "glsl_parser_extras.h"
26 #include "glsl_types.h"
27 #include "s_expression.h"
28
29 const static bool debug = false;
30
31 class ir_reader {
32 public:
33    ir_reader(_mesa_glsl_parse_state *);
34
35    void read(exec_list *instructions, const char *src, bool scan_for_protos);
36
37 private:
38    void *mem_ctx;
39    _mesa_glsl_parse_state *state;
40
41    void ir_read_error(s_expression *, const char *fmt, ...);
42
43    const glsl_type *read_type(s_expression *);
44
45    void scan_for_prototypes(exec_list *, s_expression *);
46    ir_function *read_function(s_expression *, bool skip_body);
47    void read_function_sig(ir_function *, s_expression *, bool skip_body);
48
49    void read_instructions(exec_list *, s_expression *, ir_loop *);
50    ir_instruction *read_instruction(s_expression *, ir_loop *);
51    ir_variable *read_declaration(s_expression *);
52    ir_if *read_if(s_expression *, ir_loop *);
53    ir_loop *read_loop(s_expression *);
54    ir_return *read_return(s_expression *);
55    ir_rvalue *read_rvalue(s_expression *);
56    ir_assignment *read_assignment(s_expression *);
57    ir_expression *read_expression(s_expression *);
58    ir_call *read_call(s_expression *);
59    ir_swizzle *read_swizzle(s_expression *);
60    ir_constant *read_constant(s_expression *);
61    ir_texture *read_texture(s_expression *);
62
63    ir_dereference *read_dereference(s_expression *);
64 };
65
66 ir_reader::ir_reader(_mesa_glsl_parse_state *state) : state(state)
67 {
68    this->mem_ctx = state;
69 }
70
71 void
72 _mesa_glsl_read_ir(_mesa_glsl_parse_state *state, exec_list *instructions,
73                    const char *src, bool scan_for_protos)
74 {
75    ir_reader r(state);
76    r.read(instructions, src, scan_for_protos);
77 }
78
79 void
80 ir_reader::read(exec_list *instructions, const char *src, bool scan_for_protos)
81 {
82    s_expression *expr = s_expression::read_expression(mem_ctx, src);
83    if (expr == NULL) {
84       ir_read_error(NULL, "couldn't parse S-Expression.");
85       return;
86    }
87    
88    if (scan_for_protos) {
89       scan_for_prototypes(instructions, expr);
90       if (state->error)
91          return;
92    }
93
94    read_instructions(instructions, expr, NULL);
95    ralloc_free(expr);
96
97    if (debug)
98       validate_ir_tree(instructions);
99 }
100
101 void
102 ir_reader::ir_read_error(s_expression *expr, const char *fmt, ...)
103 {
104    va_list ap;
105
106    state->error = true;
107
108    if (state->current_function != NULL)
109       ralloc_asprintf_append(&state->info_log, "In function %s:\n",
110                              state->current_function->function_name());
111    ralloc_strcat(&state->info_log, "error: ");
112
113    va_start(ap, fmt);
114    ralloc_vasprintf_append(&state->info_log, fmt, ap);
115    va_end(ap);
116    ralloc_strcat(&state->info_log, "\n");
117
118    if (expr != NULL) {
119       ralloc_strcat(&state->info_log, "...in this context:\n   ");
120       expr->print();
121       ralloc_strcat(&state->info_log, "\n\n");
122    }
123 }
124
125 const glsl_type *
126 ir_reader::read_type(s_expression *expr)
127 {
128    s_expression *s_base_type;
129    s_int *s_size;
130
131    s_pattern pat[] = { "array", s_base_type, s_size };
132    if (MATCH(expr, pat)) {
133       const glsl_type *base_type = read_type(s_base_type);
134       if (base_type == NULL) {
135          ir_read_error(NULL, "when reading base type of array type");
136          return NULL;
137       }
138
139       return glsl_type::get_array_instance(base_type, s_size->value());
140    }
141    
142    s_symbol *type_sym = SX_AS_SYMBOL(expr);
143    if (type_sym == NULL) {
144       ir_read_error(expr, "expected <type>");
145       return NULL;
146    }
147
148    const glsl_type *type = state->symbols->get_type(type_sym->value());
149    if (type == NULL)
150       ir_read_error(expr, "invalid type: %s", type_sym->value());
151
152    return type;
153 }
154
155
156 void
157 ir_reader::scan_for_prototypes(exec_list *instructions, s_expression *expr)
158 {
159    s_list *list = SX_AS_LIST(expr);
160    if (list == NULL) {
161       ir_read_error(expr, "Expected (<instruction> ...); found an atom.");
162       return;
163    }
164
165    foreach_iter(exec_list_iterator, it, list->subexpressions) {
166       s_list *sub = SX_AS_LIST(it.get());
167       if (sub == NULL)
168          continue; // not a (function ...); ignore it.
169
170       s_symbol *tag = SX_AS_SYMBOL(sub->subexpressions.get_head());
171       if (tag == NULL || strcmp(tag->value(), "function") != 0)
172          continue; // not a (function ...); ignore it.
173
174       ir_function *f = read_function(sub, true);
175       if (f == NULL)
176          return;
177       instructions->push_tail(f);
178    }
179 }
180
181 ir_function *
182 ir_reader::read_function(s_expression *expr, bool skip_body)
183 {
184    bool added = false;
185    s_symbol *name;
186
187    s_pattern pat[] = { "function", name };
188    if (!PARTIAL_MATCH(expr, pat)) {
189       ir_read_error(expr, "Expected (function <name> (signature ...) ...)");
190       return NULL;
191    }
192
193    ir_function *f = state->symbols->get_function(name->value());
194    if (f == NULL) {
195       f = new(mem_ctx) ir_function(name->value());
196       added = state->symbols->add_function(f);
197       assert(added);
198    }
199
200    exec_list_iterator it = ((s_list *) expr)->subexpressions.iterator();
201    it.next(); // skip "function" tag
202    it.next(); // skip function name
203    for (/* nothing */; it.has_next(); it.next()) {
204       s_expression *s_sig = (s_expression *) it.get();
205       read_function_sig(f, s_sig, skip_body);
206    }
207    return added ? f : NULL;
208 }
209
210 void
211 ir_reader::read_function_sig(ir_function *f, s_expression *expr, bool skip_body)
212 {
213    s_expression *type_expr;
214    s_list *paramlist;
215    s_list *body_list;
216
217    s_pattern pat[] = { "signature", type_expr, paramlist, body_list };
218    if (!MATCH(expr, pat)) {
219       ir_read_error(expr, "Expected (signature <type> (parameters ...) "
220                           "(<instruction> ...))");
221       return;
222    }
223
224    const glsl_type *return_type = read_type(type_expr);
225    if (return_type == NULL)
226       return;
227
228    s_symbol *paramtag = SX_AS_SYMBOL(paramlist->subexpressions.get_head());
229    if (paramtag == NULL || strcmp(paramtag->value(), "parameters") != 0) {
230       ir_read_error(paramlist, "Expected (parameters ...)");
231       return;
232    }
233
234    // Read the parameters list into a temporary place.
235    exec_list hir_parameters;
236    state->symbols->push_scope();
237
238    exec_list_iterator it = paramlist->subexpressions.iterator();
239    for (it.next() /* skip "parameters" */; it.has_next(); it.next()) {
240       ir_variable *var = read_declaration((s_expression *) it.get());
241       if (var == NULL)
242          return;
243
244       hir_parameters.push_tail(var);
245    }
246
247    ir_function_signature *sig = f->exact_matching_signature(&hir_parameters);
248    if (sig == NULL && skip_body) {
249       /* If scanning for prototypes, generate a new signature. */
250       sig = new(mem_ctx) ir_function_signature(return_type);
251       sig->is_builtin = true;
252       f->add_signature(sig);
253    } else if (sig != NULL) {
254       const char *badvar = sig->qualifiers_match(&hir_parameters);
255       if (badvar != NULL) {
256          ir_read_error(expr, "function `%s' parameter `%s' qualifiers "
257                        "don't match prototype", f->name, badvar);
258          return;
259       }
260
261       if (sig->return_type != return_type) {
262          ir_read_error(expr, "function `%s' return type doesn't "
263                        "match prototype", f->name);
264          return;
265       }
266    } else {
267       /* No prototype for this body exists - skip it. */
268       state->symbols->pop_scope();
269       return;
270    }
271    assert(sig != NULL);
272
273    sig->replace_parameters(&hir_parameters);
274
275    if (!skip_body && !body_list->subexpressions.is_empty()) {
276       if (sig->is_defined) {
277          ir_read_error(expr, "function %s redefined", f->name);
278          return;
279       }
280       state->current_function = sig;
281       read_instructions(&sig->body, body_list, NULL);
282       state->current_function = NULL;
283       sig->is_defined = true;
284    }
285
286    state->symbols->pop_scope();
287 }
288
289 void
290 ir_reader::read_instructions(exec_list *instructions, s_expression *expr,
291                              ir_loop *loop_ctx)
292 {
293    // Read in a list of instructions
294    s_list *list = SX_AS_LIST(expr);
295    if (list == NULL) {
296       ir_read_error(expr, "Expected (<instruction> ...); found an atom.");
297       return;
298    }
299
300    foreach_iter(exec_list_iterator, it, list->subexpressions) {
301       s_expression *sub = (s_expression*) it.get();
302       ir_instruction *ir = read_instruction(sub, loop_ctx);
303       if (ir != NULL) {
304          /* Global variable declarations should be moved to the top, before
305           * any functions that might use them.  Functions are added to the
306           * instruction stream when scanning for prototypes, so without this
307           * hack, they always appear before variable declarations.
308           */
309          if (state->current_function == NULL && ir->as_variable() != NULL)
310             instructions->push_head(ir);
311          else
312             instructions->push_tail(ir);
313       }
314    }
315 }
316
317
318 ir_instruction *
319 ir_reader::read_instruction(s_expression *expr, ir_loop *loop_ctx)
320 {
321    s_symbol *symbol = SX_AS_SYMBOL(expr);
322    if (symbol != NULL) {
323       if (strcmp(symbol->value(), "break") == 0 && loop_ctx != NULL)
324          return new(mem_ctx) ir_loop_jump(ir_loop_jump::jump_break);
325       if (strcmp(symbol->value(), "continue") == 0 && loop_ctx != NULL)
326          return new(mem_ctx) ir_loop_jump(ir_loop_jump::jump_continue);
327    }
328
329    s_list *list = SX_AS_LIST(expr);
330    if (list == NULL || list->subexpressions.is_empty()) {
331       ir_read_error(expr, "Invalid instruction.\n");
332       return NULL;
333    }
334
335    s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
336    if (tag == NULL) {
337       ir_read_error(expr, "expected instruction tag");
338       return NULL;
339    }
340
341    ir_instruction *inst = NULL;
342    if (strcmp(tag->value(), "declare") == 0) {
343       inst = read_declaration(list);
344    } else if (strcmp(tag->value(), "assign") == 0) {
345       inst = read_assignment(list);
346    } else if (strcmp(tag->value(), "if") == 0) {
347       inst = read_if(list, loop_ctx);
348    } else if (strcmp(tag->value(), "loop") == 0) {
349       inst = read_loop(list);
350    } else if (strcmp(tag->value(), "return") == 0) {
351       inst = read_return(list);
352    } else if (strcmp(tag->value(), "function") == 0) {
353       inst = read_function(list, false);
354    } else {
355       inst = read_rvalue(list);
356       if (inst == NULL)
357          ir_read_error(NULL, "when reading instruction");
358    }
359    return inst;
360 }
361
362 ir_variable *
363 ir_reader::read_declaration(s_expression *expr)
364 {
365    s_list *s_quals;
366    s_expression *s_type;
367    s_symbol *s_name;
368
369    s_pattern pat[] = { "declare", s_quals, s_type, s_name };
370    if (!MATCH(expr, pat)) {
371       ir_read_error(expr, "expected (declare (<qualifiers>) <type> <name>)");
372       return NULL;
373    }
374
375    const glsl_type *type = read_type(s_type);
376    if (type == NULL)
377       return NULL;
378
379    ir_variable *var = new(mem_ctx) ir_variable(type, s_name->value(),
380                                                ir_var_auto);
381
382    foreach_iter(exec_list_iterator, it, s_quals->subexpressions) {
383       s_symbol *qualifier = SX_AS_SYMBOL(it.get());
384       if (qualifier == NULL) {
385          ir_read_error(expr, "qualifier list must contain only symbols");
386          return NULL;
387       }
388
389       // FINISHME: Check for duplicate/conflicting qualifiers.
390       if (strcmp(qualifier->value(), "centroid") == 0) {
391          var->centroid = 1;
392       } else if (strcmp(qualifier->value(), "invariant") == 0) {
393          var->invariant = 1;
394       } else if (strcmp(qualifier->value(), "uniform") == 0) {
395          var->mode = ir_var_uniform;
396       } else if (strcmp(qualifier->value(), "auto") == 0) {
397          var->mode = ir_var_auto;
398       } else if (strcmp(qualifier->value(), "in") == 0) {
399          var->mode = ir_var_in;
400       } else if (strcmp(qualifier->value(), "out") == 0) {
401          var->mode = ir_var_out;
402       } else if (strcmp(qualifier->value(), "inout") == 0) {
403          var->mode = ir_var_inout;
404       } else if (strcmp(qualifier->value(), "smooth") == 0) {
405          var->interpolation = ir_var_smooth;
406       } else if (strcmp(qualifier->value(), "flat") == 0) {
407          var->interpolation = ir_var_flat;
408       } else if (strcmp(qualifier->value(), "noperspective") == 0) {
409          var->interpolation = ir_var_noperspective;
410       } else {
411          ir_read_error(expr, "unknown qualifier: %s", qualifier->value());
412          return NULL;
413       }
414    }
415
416    // Add the variable to the symbol table
417    state->symbols->add_variable(var);
418
419    return var;
420 }
421
422
423 ir_if *
424 ir_reader::read_if(s_expression *expr, ir_loop *loop_ctx)
425 {
426    s_expression *s_cond;
427    s_expression *s_then;
428    s_expression *s_else;
429
430    s_pattern pat[] = { "if", s_cond, s_then, s_else };
431    if (!MATCH(expr, pat)) {
432       ir_read_error(expr, "expected (if <condition> (<then>...) (<else>...))");
433       return NULL;
434    }
435
436    ir_rvalue *condition = read_rvalue(s_cond);
437    if (condition == NULL) {
438       ir_read_error(NULL, "when reading condition of (if ...)");
439       return NULL;
440    }
441
442    ir_if *iff = new(mem_ctx) ir_if(condition);
443
444    read_instructions(&iff->then_instructions, s_then, loop_ctx);
445    read_instructions(&iff->else_instructions, s_else, loop_ctx);
446    if (state->error) {
447       delete iff;
448       iff = NULL;
449    }
450    return iff;
451 }
452
453
454 ir_loop *
455 ir_reader::read_loop(s_expression *expr)
456 {
457    s_expression *s_counter, *s_from, *s_to, *s_inc, *s_body;
458
459    s_pattern pat[] = { "loop", s_counter, s_from, s_to, s_inc, s_body };
460    if (!MATCH(expr, pat)) {
461       ir_read_error(expr, "expected (loop <counter> <from> <to> "
462                           "<increment> <body>)");
463       return NULL;
464    }
465
466    // FINISHME: actually read the count/from/to fields.
467
468    ir_loop *loop = new(mem_ctx) ir_loop;
469    read_instructions(&loop->body_instructions, s_body, loop);
470    if (state->error) {
471       delete loop;
472       loop = NULL;
473    }
474    return loop;
475 }
476
477
478 ir_return *
479 ir_reader::read_return(s_expression *expr)
480 {
481    s_expression *s_retval;
482
483    s_pattern pat[] = { "return", s_retval};
484    if (!MATCH(expr, pat)) {
485       ir_read_error(expr, "expected (return <rvalue>)");
486       return NULL;
487    }
488
489    ir_rvalue *retval = read_rvalue(s_retval);
490    if (retval == NULL) {
491       ir_read_error(NULL, "when reading return value");
492       return NULL;
493    }
494
495    return new(mem_ctx) ir_return(retval);
496 }
497
498
499 ir_rvalue *
500 ir_reader::read_rvalue(s_expression *expr)
501 {
502    s_list *list = SX_AS_LIST(expr);
503    if (list == NULL || list->subexpressions.is_empty())
504       return NULL;
505
506    s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
507    if (tag == NULL) {
508       ir_read_error(expr, "expected rvalue tag");
509       return NULL;
510    }
511
512    ir_rvalue *rvalue = read_dereference(list);
513    if (rvalue != NULL || state->error)
514       return rvalue;
515    else if (strcmp(tag->value(), "swiz") == 0) {
516       rvalue = read_swizzle(list);
517    } else if (strcmp(tag->value(), "expression") == 0) {
518       rvalue = read_expression(list);
519    } else if (strcmp(tag->value(), "call") == 0) {
520       rvalue = read_call(list);
521    } else if (strcmp(tag->value(), "constant") == 0) {
522       rvalue = read_constant(list);
523    } else {
524       rvalue = read_texture(list);
525       if (rvalue == NULL && !state->error)
526          ir_read_error(expr, "unrecognized rvalue tag: %s", tag->value());
527    }
528
529    return rvalue;
530 }
531
532 ir_assignment *
533 ir_reader::read_assignment(s_expression *expr)
534 {
535    s_expression *cond_expr = NULL;
536    s_expression *lhs_expr, *rhs_expr;
537    s_list       *mask_list;
538
539    s_pattern pat4[] = { "assign",            mask_list, lhs_expr, rhs_expr };
540    s_pattern pat5[] = { "assign", cond_expr, mask_list, lhs_expr, rhs_expr };
541    if (!MATCH(expr, pat4) && !MATCH(expr, pat5)) {
542       ir_read_error(expr, "expected (assign [<condition>] (<write mask>) "
543                           "<lhs> <rhs>)");
544       return NULL;
545    }
546
547    ir_rvalue *condition = NULL;
548    if (cond_expr != NULL) {
549       condition = read_rvalue(cond_expr);
550       if (condition == NULL) {
551          ir_read_error(NULL, "when reading condition of assignment");
552          return NULL;
553       }
554    }
555
556    unsigned mask = 0;
557
558    s_symbol *mask_symbol;
559    s_pattern mask_pat[] = { mask_symbol };
560    if (MATCH(mask_list, mask_pat)) {
561       const char *mask_str = mask_symbol->value();
562       unsigned mask_length = strlen(mask_str);
563       if (mask_length > 4) {
564          ir_read_error(expr, "invalid write mask: %s", mask_str);
565          return NULL;
566       }
567
568       const unsigned idx_map[] = { 3, 0, 1, 2 }; /* w=bit 3, x=0, y=1, z=2 */
569
570       for (unsigned i = 0; i < mask_length; i++) {
571          if (mask_str[i] < 'w' || mask_str[i] > 'z') {
572             ir_read_error(expr, "write mask contains invalid character: %c",
573                           mask_str[i]);
574             return NULL;
575          }
576          mask |= 1 << idx_map[mask_str[i] - 'w'];
577       }
578    } else if (!mask_list->subexpressions.is_empty()) {
579       ir_read_error(mask_list, "expected () or (<write mask>)");
580       return NULL;
581    }
582
583    ir_dereference *lhs = read_dereference(lhs_expr);
584    if (lhs == NULL) {
585       ir_read_error(NULL, "when reading left-hand side of assignment");
586       return NULL;
587    }
588
589    ir_rvalue *rhs = read_rvalue(rhs_expr);
590    if (rhs == NULL) {
591       ir_read_error(NULL, "when reading right-hand side of assignment");
592       return NULL;
593    }
594
595    if (mask == 0 && (lhs->type->is_vector() || lhs->type->is_scalar())) {
596       ir_read_error(expr, "non-zero write mask required.");
597       return NULL;
598    }
599
600    return new(mem_ctx) ir_assignment(lhs, rhs, condition, mask);
601 }
602
603 ir_call *
604 ir_reader::read_call(s_expression *expr)
605 {
606    s_symbol *name;
607    s_list *params;
608
609    s_pattern pat[] = { "call", name, params };
610    if (!MATCH(expr, pat)) {
611       ir_read_error(expr, "expected (call <name> (<param> ...))");
612       return NULL;
613    }
614
615    exec_list parameters;
616
617    foreach_iter(exec_list_iterator, it, params->subexpressions) {
618       s_expression *expr = (s_expression*) it.get();
619       ir_rvalue *param = read_rvalue(expr);
620       if (param == NULL) {
621          ir_read_error(expr, "when reading parameter to function call");
622          return NULL;
623       }
624       parameters.push_tail(param);
625    }
626
627    ir_function *f = state->symbols->get_function(name->value());
628    if (f == NULL) {
629       ir_read_error(expr, "found call to undefined function %s",
630                     name->value());
631       return NULL;
632    }
633
634    ir_function_signature *callee = f->matching_signature(&parameters);
635    if (callee == NULL) {
636       ir_read_error(expr, "couldn't find matching signature for function "
637                     "%s", name->value());
638       return NULL;
639    }
640
641    return new(mem_ctx) ir_call(callee, &parameters);
642 }
643
644 ir_expression *
645 ir_reader::read_expression(s_expression *expr)
646 {
647    s_expression *s_type;
648    s_symbol *s_op;
649    s_expression *s_arg1;
650
651    s_pattern pat[] = { "expression", s_type, s_op, s_arg1 };
652    if (!PARTIAL_MATCH(expr, pat)) {
653       ir_read_error(expr, "expected (expression <type> <operator> "
654                           "<operand> [<operand>])");
655       return NULL;
656    }
657    s_expression *s_arg2 = (s_expression *) s_arg1->next; // may be tail sentinel
658
659    const glsl_type *type = read_type(s_type);
660    if (type == NULL)
661       return NULL;
662
663    /* Read the operator */
664    ir_expression_operation op = ir_expression::get_operator(s_op->value());
665    if (op == (ir_expression_operation) -1) {
666       ir_read_error(expr, "invalid operator: %s", s_op->value());
667       return NULL;
668    }
669     
670    unsigned num_operands = ir_expression::get_num_operands(op);
671    if (num_operands == 1 && !s_arg1->next->is_tail_sentinel()) {
672       ir_read_error(expr, "expected (expression <type> %s <operand>)",
673                     s_op->value());
674       return NULL;
675    }
676
677    ir_rvalue *arg1 = read_rvalue(s_arg1);
678    ir_rvalue *arg2 = NULL;
679    if (arg1 == NULL) {
680       ir_read_error(NULL, "when reading first operand of %s", s_op->value());
681       return NULL;
682    }
683
684    if (num_operands == 2) {
685       if (s_arg2->is_tail_sentinel() || !s_arg2->next->is_tail_sentinel()) {
686          ir_read_error(expr, "expected (expression <type> %s <operand> "
687                              "<operand>)", s_op->value());
688          return NULL;
689       }
690       arg2 = read_rvalue(s_arg2);
691       if (arg2 == NULL) {
692          ir_read_error(NULL, "when reading second operand of %s",
693                        s_op->value());
694          return NULL;
695       }
696    }
697
698    return new(mem_ctx) ir_expression(op, type, arg1, arg2);
699 }
700
701 ir_swizzle *
702 ir_reader::read_swizzle(s_expression *expr)
703 {
704    s_symbol *swiz;
705    s_expression *sub;
706
707    s_pattern pat[] = { "swiz", swiz, sub };
708    if (!MATCH(expr, pat)) {
709       ir_read_error(expr, "expected (swiz <swizzle> <rvalue>)");
710       return NULL;
711    }
712
713    if (strlen(swiz->value()) > 4) {
714       ir_read_error(expr, "expected a valid swizzle; found %s", swiz->value());
715       return NULL;
716    }
717
718    ir_rvalue *rvalue = read_rvalue(sub);
719    if (rvalue == NULL)
720       return NULL;
721
722    ir_swizzle *ir = ir_swizzle::create(rvalue, swiz->value(),
723                                        rvalue->type->vector_elements);
724    if (ir == NULL)
725       ir_read_error(expr, "invalid swizzle");
726
727    return ir;
728 }
729
730 ir_constant *
731 ir_reader::read_constant(s_expression *expr)
732 {
733    s_expression *type_expr;
734    s_list *values;
735
736    s_pattern pat[] = { "constant", type_expr, values };
737    if (!MATCH(expr, pat)) {
738       ir_read_error(expr, "expected (constant <type> (...))");
739       return NULL;
740    }
741
742    const glsl_type *type = read_type(type_expr);
743    if (type == NULL)
744       return NULL;
745
746    if (values == NULL) {
747       ir_read_error(expr, "expected (constant <type> (...))");
748       return NULL;
749    }
750
751    if (type->is_array()) {
752       unsigned elements_supplied = 0;
753       exec_list elements;
754       foreach_iter(exec_list_iterator, it, values->subexpressions) {
755          s_expression *elt = (s_expression *) it.get();
756          ir_constant *ir_elt = read_constant(elt);
757          if (ir_elt == NULL)
758             return NULL;
759          elements.push_tail(ir_elt);
760          elements_supplied++;
761       }
762
763       if (elements_supplied != type->length) {
764          ir_read_error(values, "expected exactly %u array elements, "
765                        "given %u", type->length, elements_supplied);
766          return NULL;
767       }
768       return new(mem_ctx) ir_constant(type, &elements);
769    }
770
771    const glsl_type *const base_type = type->get_base_type();
772
773    ir_constant_data data = { { 0 } };
774
775    // Read in list of values (at most 16).
776    int k = 0;
777    foreach_iter(exec_list_iterator, it, values->subexpressions) {
778       if (k >= 16) {
779          ir_read_error(values, "expected at most 16 numbers");
780          return NULL;
781       }
782
783       s_expression *expr = (s_expression*) it.get();
784
785       if (base_type->base_type == GLSL_TYPE_FLOAT) {
786          s_number *value = SX_AS_NUMBER(expr);
787          if (value == NULL) {
788             ir_read_error(values, "expected numbers");
789             return NULL;
790          }
791          data.f[k] = value->fvalue();
792       } else {
793          s_int *value = SX_AS_INT(expr);
794          if (value == NULL) {
795             ir_read_error(values, "expected integers");
796             return NULL;
797          }
798
799          switch (base_type->base_type) {
800          case GLSL_TYPE_UINT: {
801             data.u[k] = value->value();
802             break;
803          }
804          case GLSL_TYPE_INT: {
805             data.i[k] = value->value();
806             break;
807          }
808          case GLSL_TYPE_BOOL: {
809             data.b[k] = value->value();
810             break;
811          }
812          default:
813             ir_read_error(values, "unsupported constant type");
814             return NULL;
815          }
816       }
817       ++k;
818    }
819
820    return new(mem_ctx) ir_constant(type, &data);
821 }
822
823 ir_dereference *
824 ir_reader::read_dereference(s_expression *expr)
825 {
826    s_symbol *s_var;
827    s_expression *s_subject;
828    s_expression *s_index;
829    s_symbol *s_field;
830
831    s_pattern var_pat[] = { "var_ref", s_var };
832    s_pattern array_pat[] = { "array_ref", s_subject, s_index };
833    s_pattern record_pat[] = { "record_ref", s_subject, s_field };
834
835    if (MATCH(expr, var_pat)) {
836       ir_variable *var = state->symbols->get_variable(s_var->value());
837       if (var == NULL) {
838          ir_read_error(expr, "undeclared variable: %s", s_var->value());
839          return NULL;
840       }
841       return new(mem_ctx) ir_dereference_variable(var);
842    } else if (MATCH(expr, array_pat)) {
843       ir_rvalue *subject = read_rvalue(s_subject);
844       if (subject == NULL) {
845          ir_read_error(NULL, "when reading the subject of an array_ref");
846          return NULL;
847       }
848
849       ir_rvalue *idx = read_rvalue(s_index);
850       if (subject == NULL) {
851          ir_read_error(NULL, "when reading the index of an array_ref");
852          return NULL;
853       }
854       return new(mem_ctx) ir_dereference_array(subject, idx);
855    } else if (MATCH(expr, record_pat)) {
856       ir_rvalue *subject = read_rvalue(s_subject);
857       if (subject == NULL) {
858          ir_read_error(NULL, "when reading the subject of a record_ref");
859          return NULL;
860       }
861       return new(mem_ctx) ir_dereference_record(subject, s_field->value());
862    }
863    return NULL;
864 }
865
866 ir_texture *
867 ir_reader::read_texture(s_expression *expr)
868 {
869    s_symbol *tag = NULL;
870    s_expression *s_sampler = NULL;
871    s_expression *s_coord = NULL;
872    s_expression *s_offset = NULL;
873    s_expression *s_proj = NULL;
874    s_list *s_shadow = NULL;
875    s_expression *s_lod = NULL;
876
877    ir_texture_opcode op = ir_tex; /* silence warning */
878
879    s_pattern tex_pattern[] =
880       { "tex", s_sampler, s_coord, s_offset, s_proj, s_shadow };
881    s_pattern txf_pattern[] =
882       { "txf", s_sampler, s_coord, s_offset, s_lod };
883    s_pattern other_pattern[] =
884       { tag, s_sampler, s_coord, s_offset, s_proj, s_shadow, s_lod };
885
886    if (MATCH(expr, tex_pattern)) {
887       op = ir_tex;
888    } else if (MATCH(expr, txf_pattern)) {
889       op = ir_txf;
890    } else if (MATCH(expr, other_pattern)) {
891       op = ir_texture::get_opcode(tag->value());
892       if (op == -1)
893          return NULL;
894    } else {
895       ir_read_error(NULL, "unexpected texture pattern");
896       return NULL;
897    }
898
899    ir_texture *tex = new(mem_ctx) ir_texture(op);
900
901    // Read sampler (must be a deref)
902    ir_dereference *sampler = read_dereference(s_sampler);
903    if (sampler == NULL) {
904       ir_read_error(NULL, "when reading sampler in (%s ...)",
905                     tex->opcode_string());
906       return NULL;
907    }
908    tex->set_sampler(sampler);
909
910    // Read coordinate (any rvalue)
911    tex->coordinate = read_rvalue(s_coord);
912    if (tex->coordinate == NULL) {
913       ir_read_error(NULL, "when reading coordinate in (%s ...)",
914                     tex->opcode_string());
915       return NULL;
916    }
917
918    // Read texel offset - either 0 or an rvalue.
919    s_int *si_offset = SX_AS_INT(s_offset);
920    if (si_offset == NULL || si_offset->value() != 0) {
921       tex->offset = read_rvalue(s_offset);
922       if (tex->offset == NULL) {
923          ir_read_error(s_offset, "expected 0 or an expression");
924          return NULL;
925       }
926    }
927
928    if (op != ir_txf) {
929       s_int *proj_as_int = SX_AS_INT(s_proj);
930       if (proj_as_int && proj_as_int->value() == 1) {
931          tex->projector = NULL;
932       } else {
933          tex->projector = read_rvalue(s_proj);
934          if (tex->projector == NULL) {
935             ir_read_error(NULL, "when reading projective divide in (%s ..)",
936                           tex->opcode_string());
937             return NULL;
938          }
939       }
940
941       if (s_shadow->subexpressions.is_empty()) {
942          tex->shadow_comparitor = NULL;
943       } else {
944          tex->shadow_comparitor = read_rvalue(s_shadow);
945          if (tex->shadow_comparitor == NULL) {
946             ir_read_error(NULL, "when reading shadow comparitor in (%s ..)",
947                           tex->opcode_string());
948             return NULL;
949          }
950       }
951    }
952
953    switch (op) {
954    case ir_txb:
955       tex->lod_info.bias = read_rvalue(s_lod);
956       if (tex->lod_info.bias == NULL) {
957          ir_read_error(NULL, "when reading LOD bias in (txb ...)");
958          return NULL;
959       }
960       break;
961    case ir_txl:
962    case ir_txf:
963       tex->lod_info.lod = read_rvalue(s_lod);
964       if (tex->lod_info.lod == NULL) {
965          ir_read_error(NULL, "when reading LOD in (%s ...)",
966                        tex->opcode_string());
967          return NULL;
968       }
969       break;
970    case ir_txd: {
971       s_expression *s_dx, *s_dy;
972       s_pattern dxdy_pat[] = { s_dx, s_dy };
973       if (!MATCH(s_lod, dxdy_pat)) {
974          ir_read_error(s_lod, "expected (dPdx dPdy) in (txd ...)");
975          return NULL;
976       }
977       tex->lod_info.grad.dPdx = read_rvalue(s_dx);
978       if (tex->lod_info.grad.dPdx == NULL) {
979          ir_read_error(NULL, "when reading dPdx in (txd ...)");
980          return NULL;
981       }
982       tex->lod_info.grad.dPdy = read_rvalue(s_dy);
983       if (tex->lod_info.grad.dPdy == NULL) {
984          ir_read_error(NULL, "when reading dPdy in (txd ...)");
985          return NULL;
986       }
987       break;
988    }
989    default:
990       // tex doesn't have any extra parameters.
991       break;
992    };
993    return tex;
994 }