Tizen 2.0 Release
[profile/ivi/osmesa.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(), "const_in") == 0) {
401          var->mode = ir_var_const_in;
402       } else if (strcmp(qualifier->value(), "out") == 0) {
403          var->mode = ir_var_out;
404       } else if (strcmp(qualifier->value(), "inout") == 0) {
405          var->mode = ir_var_inout;
406       } else if (strcmp(qualifier->value(), "smooth") == 0) {
407          var->interpolation = ir_var_smooth;
408       } else if (strcmp(qualifier->value(), "flat") == 0) {
409          var->interpolation = ir_var_flat;
410       } else if (strcmp(qualifier->value(), "noperspective") == 0) {
411          var->interpolation = ir_var_noperspective;
412       } else {
413          ir_read_error(expr, "unknown qualifier: %s", qualifier->value());
414          return NULL;
415       }
416    }
417
418    // Add the variable to the symbol table
419    state->symbols->add_variable(var);
420
421    return var;
422 }
423
424
425 ir_if *
426 ir_reader::read_if(s_expression *expr, ir_loop *loop_ctx)
427 {
428    s_expression *s_cond;
429    s_expression *s_then;
430    s_expression *s_else;
431
432    s_pattern pat[] = { "if", s_cond, s_then, s_else };
433    if (!MATCH(expr, pat)) {
434       ir_read_error(expr, "expected (if <condition> (<then>...) (<else>...))");
435       return NULL;
436    }
437
438    ir_rvalue *condition = read_rvalue(s_cond);
439    if (condition == NULL) {
440       ir_read_error(NULL, "when reading condition of (if ...)");
441       return NULL;
442    }
443
444    ir_if *iff = new(mem_ctx) ir_if(condition);
445
446    read_instructions(&iff->then_instructions, s_then, loop_ctx);
447    read_instructions(&iff->else_instructions, s_else, loop_ctx);
448    if (state->error) {
449       delete iff;
450       iff = NULL;
451    }
452    return iff;
453 }
454
455
456 ir_loop *
457 ir_reader::read_loop(s_expression *expr)
458 {
459    s_expression *s_counter, *s_from, *s_to, *s_inc, *s_body;
460
461    s_pattern pat[] = { "loop", s_counter, s_from, s_to, s_inc, s_body };
462    if (!MATCH(expr, pat)) {
463       ir_read_error(expr, "expected (loop <counter> <from> <to> "
464                           "<increment> <body>)");
465       return NULL;
466    }
467
468    // FINISHME: actually read the count/from/to fields.
469
470    ir_loop *loop = new(mem_ctx) ir_loop;
471    read_instructions(&loop->body_instructions, s_body, loop);
472    if (state->error) {
473       delete loop;
474       loop = NULL;
475    }
476    return loop;
477 }
478
479
480 ir_return *
481 ir_reader::read_return(s_expression *expr)
482 {
483    s_expression *s_retval;
484
485    s_pattern pat[] = { "return", s_retval};
486    if (!MATCH(expr, pat)) {
487       ir_read_error(expr, "expected (return <rvalue>)");
488       return NULL;
489    }
490
491    ir_rvalue *retval = read_rvalue(s_retval);
492    if (retval == NULL) {
493       ir_read_error(NULL, "when reading return value");
494       return NULL;
495    }
496
497    return new(mem_ctx) ir_return(retval);
498 }
499
500
501 ir_rvalue *
502 ir_reader::read_rvalue(s_expression *expr)
503 {
504    s_list *list = SX_AS_LIST(expr);
505    if (list == NULL || list->subexpressions.is_empty())
506       return NULL;
507
508    s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
509    if (tag == NULL) {
510       ir_read_error(expr, "expected rvalue tag");
511       return NULL;
512    }
513
514    ir_rvalue *rvalue = read_dereference(list);
515    if (rvalue != NULL || state->error)
516       return rvalue;
517    else if (strcmp(tag->value(), "swiz") == 0) {
518       rvalue = read_swizzle(list);
519    } else if (strcmp(tag->value(), "expression") == 0) {
520       rvalue = read_expression(list);
521    } else if (strcmp(tag->value(), "call") == 0) {
522       rvalue = read_call(list);
523    } else if (strcmp(tag->value(), "constant") == 0) {
524       rvalue = read_constant(list);
525    } else {
526       rvalue = read_texture(list);
527       if (rvalue == NULL && !state->error)
528          ir_read_error(expr, "unrecognized rvalue tag: %s", tag->value());
529    }
530
531    return rvalue;
532 }
533
534 ir_assignment *
535 ir_reader::read_assignment(s_expression *expr)
536 {
537    s_expression *cond_expr = NULL;
538    s_expression *lhs_expr, *rhs_expr;
539    s_list       *mask_list;
540
541    s_pattern pat4[] = { "assign",            mask_list, lhs_expr, rhs_expr };
542    s_pattern pat5[] = { "assign", cond_expr, mask_list, lhs_expr, rhs_expr };
543    if (!MATCH(expr, pat4) && !MATCH(expr, pat5)) {
544       ir_read_error(expr, "expected (assign [<condition>] (<write mask>) "
545                           "<lhs> <rhs>)");
546       return NULL;
547    }
548
549    ir_rvalue *condition = NULL;
550    if (cond_expr != NULL) {
551       condition = read_rvalue(cond_expr);
552       if (condition == NULL) {
553          ir_read_error(NULL, "when reading condition of assignment");
554          return NULL;
555       }
556    }
557
558    unsigned mask = 0;
559
560    s_symbol *mask_symbol;
561    s_pattern mask_pat[] = { mask_symbol };
562    if (MATCH(mask_list, mask_pat)) {
563       const char *mask_str = mask_symbol->value();
564       unsigned mask_length = strlen(mask_str);
565       if (mask_length > 4) {
566          ir_read_error(expr, "invalid write mask: %s", mask_str);
567          return NULL;
568       }
569
570       const unsigned idx_map[] = { 3, 0, 1, 2 }; /* w=bit 3, x=0, y=1, z=2 */
571
572       for (unsigned i = 0; i < mask_length; i++) {
573          if (mask_str[i] < 'w' || mask_str[i] > 'z') {
574             ir_read_error(expr, "write mask contains invalid character: %c",
575                           mask_str[i]);
576             return NULL;
577          }
578          mask |= 1 << idx_map[mask_str[i] - 'w'];
579       }
580    } else if (!mask_list->subexpressions.is_empty()) {
581       ir_read_error(mask_list, "expected () or (<write mask>)");
582       return NULL;
583    }
584
585    ir_dereference *lhs = read_dereference(lhs_expr);
586    if (lhs == NULL) {
587       ir_read_error(NULL, "when reading left-hand side of assignment");
588       return NULL;
589    }
590
591    ir_rvalue *rhs = read_rvalue(rhs_expr);
592    if (rhs == NULL) {
593       ir_read_error(NULL, "when reading right-hand side of assignment");
594       return NULL;
595    }
596
597    if (mask == 0 && (lhs->type->is_vector() || lhs->type->is_scalar())) {
598       ir_read_error(expr, "non-zero write mask required.");
599       return NULL;
600    }
601
602    return new(mem_ctx) ir_assignment(lhs, rhs, condition, mask);
603 }
604
605 ir_call *
606 ir_reader::read_call(s_expression *expr)
607 {
608    s_symbol *name;
609    s_list *params;
610
611    s_pattern pat[] = { "call", name, params };
612    if (!MATCH(expr, pat)) {
613       ir_read_error(expr, "expected (call <name> (<param> ...))");
614       return NULL;
615    }
616
617    exec_list parameters;
618
619    foreach_iter(exec_list_iterator, it, params->subexpressions) {
620       s_expression *expr = (s_expression*) it.get();
621       ir_rvalue *param = read_rvalue(expr);
622       if (param == NULL) {
623          ir_read_error(expr, "when reading parameter to function call");
624          return NULL;
625       }
626       parameters.push_tail(param);
627    }
628
629    ir_function *f = state->symbols->get_function(name->value());
630    if (f == NULL) {
631       ir_read_error(expr, "found call to undefined function %s",
632                     name->value());
633       return NULL;
634    }
635
636    ir_function_signature *callee = f->matching_signature(&parameters);
637    if (callee == NULL) {
638       ir_read_error(expr, "couldn't find matching signature for function "
639                     "%s", name->value());
640       return NULL;
641    }
642
643    return new(mem_ctx) ir_call(callee, &parameters);
644 }
645
646 ir_expression *
647 ir_reader::read_expression(s_expression *expr)
648 {
649    s_expression *s_type;
650    s_symbol *s_op;
651    s_expression *s_arg1;
652
653    s_pattern pat[] = { "expression", s_type, s_op, s_arg1 };
654    if (!PARTIAL_MATCH(expr, pat)) {
655       ir_read_error(expr, "expected (expression <type> <operator> "
656                           "<operand> [<operand>])");
657       return NULL;
658    }
659    s_expression *s_arg2 = (s_expression *) s_arg1->next; // may be tail sentinel
660
661    const glsl_type *type = read_type(s_type);
662    if (type == NULL)
663       return NULL;
664
665    /* Read the operator */
666    ir_expression_operation op = ir_expression::get_operator(s_op->value());
667    if (op == (ir_expression_operation) -1) {
668       ir_read_error(expr, "invalid operator: %s", s_op->value());
669       return NULL;
670    }
671     
672    unsigned num_operands = ir_expression::get_num_operands(op);
673    if (num_operands == 1 && !s_arg1->next->is_tail_sentinel()) {
674       ir_read_error(expr, "expected (expression <type> %s <operand>)",
675                     s_op->value());
676       return NULL;
677    }
678
679    ir_rvalue *arg1 = read_rvalue(s_arg1);
680    ir_rvalue *arg2 = NULL;
681    if (arg1 == NULL) {
682       ir_read_error(NULL, "when reading first operand of %s", s_op->value());
683       return NULL;
684    }
685
686    if (num_operands == 2) {
687       if (s_arg2->is_tail_sentinel() || !s_arg2->next->is_tail_sentinel()) {
688          ir_read_error(expr, "expected (expression <type> %s <operand> "
689                              "<operand>)", s_op->value());
690          return NULL;
691       }
692       arg2 = read_rvalue(s_arg2);
693       if (arg2 == NULL) {
694          ir_read_error(NULL, "when reading second operand of %s",
695                        s_op->value());
696          return NULL;
697       }
698    }
699
700    return new(mem_ctx) ir_expression(op, type, arg1, arg2);
701 }
702
703 ir_swizzle *
704 ir_reader::read_swizzle(s_expression *expr)
705 {
706    s_symbol *swiz;
707    s_expression *sub;
708
709    s_pattern pat[] = { "swiz", swiz, sub };
710    if (!MATCH(expr, pat)) {
711       ir_read_error(expr, "expected (swiz <swizzle> <rvalue>)");
712       return NULL;
713    }
714
715    if (strlen(swiz->value()) > 4) {
716       ir_read_error(expr, "expected a valid swizzle; found %s", swiz->value());
717       return NULL;
718    }
719
720    ir_rvalue *rvalue = read_rvalue(sub);
721    if (rvalue == NULL)
722       return NULL;
723
724    ir_swizzle *ir = ir_swizzle::create(rvalue, swiz->value(),
725                                        rvalue->type->vector_elements);
726    if (ir == NULL)
727       ir_read_error(expr, "invalid swizzle");
728
729    return ir;
730 }
731
732 ir_constant *
733 ir_reader::read_constant(s_expression *expr)
734 {
735    s_expression *type_expr;
736    s_list *values;
737
738    s_pattern pat[] = { "constant", type_expr, values };
739    if (!MATCH(expr, pat)) {
740       ir_read_error(expr, "expected (constant <type> (...))");
741       return NULL;
742    }
743
744    const glsl_type *type = read_type(type_expr);
745    if (type == NULL)
746       return NULL;
747
748    if (values == NULL) {
749       ir_read_error(expr, "expected (constant <type> (...))");
750       return NULL;
751    }
752
753    if (type->is_array()) {
754       unsigned elements_supplied = 0;
755       exec_list elements;
756       foreach_iter(exec_list_iterator, it, values->subexpressions) {
757          s_expression *elt = (s_expression *) it.get();
758          ir_constant *ir_elt = read_constant(elt);
759          if (ir_elt == NULL)
760             return NULL;
761          elements.push_tail(ir_elt);
762          elements_supplied++;
763       }
764
765       if (elements_supplied != type->length) {
766          ir_read_error(values, "expected exactly %u array elements, "
767                        "given %u", type->length, elements_supplied);
768          return NULL;
769       }
770       return new(mem_ctx) ir_constant(type, &elements);
771    }
772
773    const glsl_type *const base_type = type->get_base_type();
774
775    ir_constant_data data = { { 0 } };
776
777    // Read in list of values (at most 16).
778    int k = 0;
779    foreach_iter(exec_list_iterator, it, values->subexpressions) {
780       if (k >= 16) {
781          ir_read_error(values, "expected at most 16 numbers");
782          return NULL;
783       }
784
785       s_expression *expr = (s_expression*) it.get();
786
787       if (base_type->base_type == GLSL_TYPE_FLOAT) {
788          s_number *value = SX_AS_NUMBER(expr);
789          if (value == NULL) {
790             ir_read_error(values, "expected numbers");
791             return NULL;
792          }
793          data.f[k] = value->fvalue();
794       } else {
795          s_int *value = SX_AS_INT(expr);
796          if (value == NULL) {
797             ir_read_error(values, "expected integers");
798             return NULL;
799          }
800
801          switch (base_type->base_type) {
802          case GLSL_TYPE_UINT: {
803             data.u[k] = value->value();
804             break;
805          }
806          case GLSL_TYPE_INT: {
807             data.i[k] = value->value();
808             break;
809          }
810          case GLSL_TYPE_BOOL: {
811             data.b[k] = value->value();
812             break;
813          }
814          default:
815             ir_read_error(values, "unsupported constant type");
816             return NULL;
817          }
818       }
819       ++k;
820    }
821
822    return new(mem_ctx) ir_constant(type, &data);
823 }
824
825 ir_dereference *
826 ir_reader::read_dereference(s_expression *expr)
827 {
828    s_symbol *s_var;
829    s_expression *s_subject;
830    s_expression *s_index;
831    s_symbol *s_field;
832
833    s_pattern var_pat[] = { "var_ref", s_var };
834    s_pattern array_pat[] = { "array_ref", s_subject, s_index };
835    s_pattern record_pat[] = { "record_ref", s_subject, s_field };
836
837    if (MATCH(expr, var_pat)) {
838       ir_variable *var = state->symbols->get_variable(s_var->value());
839       if (var == NULL) {
840          ir_read_error(expr, "undeclared variable: %s", s_var->value());
841          return NULL;
842       }
843       return new(mem_ctx) ir_dereference_variable(var);
844    } else if (MATCH(expr, array_pat)) {
845       ir_rvalue *subject = read_rvalue(s_subject);
846       if (subject == NULL) {
847          ir_read_error(NULL, "when reading the subject of an array_ref");
848          return NULL;
849       }
850
851       ir_rvalue *idx = read_rvalue(s_index);
852       if (subject == NULL) {
853          ir_read_error(NULL, "when reading the index of an array_ref");
854          return NULL;
855       }
856       return new(mem_ctx) ir_dereference_array(subject, idx);
857    } else if (MATCH(expr, record_pat)) {
858       ir_rvalue *subject = read_rvalue(s_subject);
859       if (subject == NULL) {
860          ir_read_error(NULL, "when reading the subject of a record_ref");
861          return NULL;
862       }
863       return new(mem_ctx) ir_dereference_record(subject, s_field->value());
864    }
865    return NULL;
866 }
867
868 ir_texture *
869 ir_reader::read_texture(s_expression *expr)
870 {
871    s_symbol *tag = NULL;
872    s_expression *s_type = NULL;
873    s_expression *s_sampler = NULL;
874    s_expression *s_coord = NULL;
875    s_expression *s_offset = NULL;
876    s_expression *s_proj = NULL;
877    s_list *s_shadow = NULL;
878    s_expression *s_lod = NULL;
879
880    ir_texture_opcode op = ir_tex; /* silence warning */
881
882    s_pattern tex_pattern[] =
883       { "tex", s_type, s_sampler, s_coord, s_offset, s_proj, s_shadow };
884    s_pattern txf_pattern[] =
885       { "txf", s_type, s_sampler, s_coord, s_offset, s_lod };
886    s_pattern other_pattern[] =
887       { tag, s_type, s_sampler, s_coord, s_offset, s_proj, s_shadow, s_lod };
888
889    if (MATCH(expr, tex_pattern)) {
890       op = ir_tex;
891    } else if (MATCH(expr, txf_pattern)) {
892       op = ir_txf;
893    } else if (MATCH(expr, other_pattern)) {
894       op = ir_texture::get_opcode(tag->value());
895       if (op == -1)
896          return NULL;
897    } else {
898       ir_read_error(NULL, "unexpected texture pattern");
899       return NULL;
900    }
901
902    ir_texture *tex = new(mem_ctx) ir_texture(op);
903
904    // Read return type
905    const glsl_type *type = read_type(s_type);
906    if (type == NULL) {
907       ir_read_error(NULL, "when reading type in (%s ...)",
908                     tex->opcode_string());
909       return NULL;
910    }
911
912    // Read sampler (must be a deref)
913    ir_dereference *sampler = read_dereference(s_sampler);
914    if (sampler == NULL) {
915       ir_read_error(NULL, "when reading sampler in (%s ...)",
916                     tex->opcode_string());
917       return NULL;
918    }
919    tex->set_sampler(sampler, type);
920
921    // Read coordinate (any rvalue)
922    tex->coordinate = read_rvalue(s_coord);
923    if (tex->coordinate == NULL) {
924       ir_read_error(NULL, "when reading coordinate in (%s ...)",
925                     tex->opcode_string());
926       return NULL;
927    }
928
929    // Read texel offset - either 0 or an rvalue.
930    s_int *si_offset = SX_AS_INT(s_offset);
931    if (si_offset == NULL || si_offset->value() != 0) {
932       tex->offset = read_rvalue(s_offset);
933       if (tex->offset == NULL) {
934          ir_read_error(s_offset, "expected 0 or an expression");
935          return NULL;
936       }
937    }
938
939    if (op != ir_txf) {
940       s_int *proj_as_int = SX_AS_INT(s_proj);
941       if (proj_as_int && proj_as_int->value() == 1) {
942          tex->projector = NULL;
943       } else {
944          tex->projector = read_rvalue(s_proj);
945          if (tex->projector == NULL) {
946             ir_read_error(NULL, "when reading projective divide in (%s ..)",
947                           tex->opcode_string());
948             return NULL;
949          }
950       }
951
952       if (s_shadow->subexpressions.is_empty()) {
953          tex->shadow_comparitor = NULL;
954       } else {
955          tex->shadow_comparitor = read_rvalue(s_shadow);
956          if (tex->shadow_comparitor == NULL) {
957             ir_read_error(NULL, "when reading shadow comparitor in (%s ..)",
958                           tex->opcode_string());
959             return NULL;
960          }
961       }
962    }
963
964    switch (op) {
965    case ir_txb:
966       tex->lod_info.bias = read_rvalue(s_lod);
967       if (tex->lod_info.bias == NULL) {
968          ir_read_error(NULL, "when reading LOD bias in (txb ...)");
969          return NULL;
970       }
971       break;
972    case ir_txl:
973    case ir_txf:
974       tex->lod_info.lod = read_rvalue(s_lod);
975       if (tex->lod_info.lod == NULL) {
976          ir_read_error(NULL, "when reading LOD in (%s ...)",
977                        tex->opcode_string());
978          return NULL;
979       }
980       break;
981    case ir_txd: {
982       s_expression *s_dx, *s_dy;
983       s_pattern dxdy_pat[] = { s_dx, s_dy };
984       if (!MATCH(s_lod, dxdy_pat)) {
985          ir_read_error(s_lod, "expected (dPdx dPdy) in (txd ...)");
986          return NULL;
987       }
988       tex->lod_info.grad.dPdx = read_rvalue(s_dx);
989       if (tex->lod_info.grad.dPdx == NULL) {
990          ir_read_error(NULL, "when reading dPdx in (txd ...)");
991          return NULL;
992       }
993       tex->lod_info.grad.dPdy = read_rvalue(s_dy);
994       if (tex->lod_info.grad.dPdy == NULL) {
995          ir_read_error(NULL, "when reading dPdy in (txd ...)");
996          return NULL;
997       }
998       break;
999    }
1000    default:
1001       // tex doesn't have any extra parameters.
1002       break;
1003    };
1004    return tex;
1005 }