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