xref: /aosp_15_r20/external/mesa3d/src/compiler/glsl/ir_reader.cpp (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 "compiler/glsl_types.h"
27 #include "s_expression.h"
28 
29 static const bool debug = false;
30 
31 namespace {
32 
33 class ir_reader {
34 public:
35    ir_reader(_mesa_glsl_parse_state *);
36 
37    void read(exec_list *instructions, const char *src, bool scan_for_protos);
38 
39 private:
40    void *mem_ctx;
41    _mesa_glsl_parse_state *state;
42 
43    void ir_read_error(s_expression *, const char *fmt, ...);
44 
45    const glsl_type *read_type(s_expression *);
46 
47    void scan_for_prototypes(exec_list *, s_expression *);
48    ir_function *read_function(s_expression *, bool skip_body);
49    void read_function_sig(ir_function *, s_expression *, bool skip_body);
50 
51    void read_instructions(exec_list *, s_expression *, ir_loop *);
52    ir_instruction *read_instruction(s_expression *, ir_loop *);
53    ir_variable *read_declaration(s_expression *);
54    ir_if *read_if(s_expression *, ir_loop *);
55    ir_loop *read_loop(s_expression *);
56    ir_call *read_call(s_expression *);
57    ir_return *read_return(s_expression *);
58    ir_rvalue *read_rvalue(s_expression *);
59    ir_assignment *read_assignment(s_expression *);
60    ir_expression *read_expression(s_expression *);
61    ir_swizzle *read_swizzle(s_expression *);
62    ir_constant *read_constant(s_expression *);
63    ir_texture *read_texture(s_expression *);
64    ir_emit_vertex *read_emit_vertex(s_expression *);
65    ir_end_primitive *read_end_primitive(s_expression *);
66    ir_barrier *read_barrier(s_expression *);
67 
68    ir_dereference *read_dereference(s_expression *);
69    ir_dereference_variable *read_var_ref(s_expression *);
70 };
71 
72 } /* anonymous namespace */
73 
ir_reader(_mesa_glsl_parse_state * state)74 ir_reader::ir_reader(_mesa_glsl_parse_state *state) : state(state)
75 {
76    this->mem_ctx = state;
77 }
78 
79 void
_mesa_glsl_read_ir(_mesa_glsl_parse_state * state,exec_list * instructions,const char * src,bool scan_for_protos)80 _mesa_glsl_read_ir(_mesa_glsl_parse_state *state, exec_list *instructions,
81 		   const char *src, bool scan_for_protos)
82 {
83    ir_reader r(state);
84    r.read(instructions, src, scan_for_protos);
85 }
86 
87 void
read(exec_list * instructions,const char * src,bool scan_for_protos)88 ir_reader::read(exec_list *instructions, const char *src, bool scan_for_protos)
89 {
90    void *sx_mem_ctx = ralloc_context(NULL);
91    s_expression *expr = s_expression::read_expression(sx_mem_ctx, src);
92    if (expr == NULL) {
93       ir_read_error(NULL, "couldn't parse S-Expression.");
94       return;
95    }
96 
97    if (scan_for_protos) {
98       scan_for_prototypes(instructions, expr);
99       if (state->error)
100 	 return;
101    }
102 
103    read_instructions(instructions, expr, NULL);
104    ralloc_free(sx_mem_ctx);
105 
106    if (debug)
107       validate_ir_tree(instructions);
108 }
109 
110 void
ir_read_error(s_expression * expr,const char * fmt,...)111 ir_reader::ir_read_error(s_expression *expr, const char *fmt, ...)
112 {
113    va_list ap;
114 
115    state->error = true;
116 
117    if (state->current_function != NULL)
118       ralloc_asprintf_append(&state->info_log, "In function %s:\n",
119 			     state->current_function->function_name());
120    ralloc_strcat(&state->info_log, "error: ");
121 
122    va_start(ap, fmt);
123    ralloc_vasprintf_append(&state->info_log, fmt, ap);
124    va_end(ap);
125    ralloc_strcat(&state->info_log, "\n");
126 
127    if (expr != NULL) {
128       ralloc_strcat(&state->info_log, "...in this context:\n   ");
129       expr->print();
130       ralloc_strcat(&state->info_log, "\n\n");
131    }
132 }
133 
134 const glsl_type *
read_type(s_expression * expr)135 ir_reader::read_type(s_expression *expr)
136 {
137    s_expression *s_base_type;
138    s_int *s_size;
139 
140    s_pattern pat[] = { "array", s_base_type, s_size };
141    if (MATCH(expr, pat)) {
142       const glsl_type *base_type = read_type(s_base_type);
143       if (base_type == NULL) {
144 	 ir_read_error(NULL, "when reading base type of array type");
145 	 return NULL;
146       }
147 
148       return glsl_array_type(base_type, s_size->value(), 0);
149    }
150 
151    s_symbol *type_sym = SX_AS_SYMBOL(expr);
152    if (type_sym == NULL) {
153       ir_read_error(expr, "expected <type>");
154       return NULL;
155    }
156 
157    const glsl_type *type = state->symbols->get_type(type_sym->value());
158    if (type == NULL)
159       ir_read_error(expr, "invalid type: %s", type_sym->value());
160 
161    return type;
162 }
163 
164 
165 void
scan_for_prototypes(exec_list * instructions,s_expression * expr)166 ir_reader::scan_for_prototypes(exec_list *instructions, s_expression *expr)
167 {
168    s_list *list = SX_AS_LIST(expr);
169    if (list == NULL) {
170       ir_read_error(expr, "Expected (<instruction> ...); found an atom.");
171       return;
172    }
173 
174    foreach_in_list(s_list, sub, &list->subexpressions) {
175       if (!sub->is_list())
176 	 continue; // not a (function ...); ignore it.
177 
178       s_symbol *tag = SX_AS_SYMBOL(sub->subexpressions.get_head());
179       if (tag == NULL || strcmp(tag->value(), "function") != 0)
180 	 continue; // not a (function ...); ignore it.
181 
182       ir_function *f = read_function(sub, true);
183       if (f == NULL)
184 	 return;
185       instructions->push_tail(f);
186    }
187 }
188 
189 ir_function *
read_function(s_expression * expr,bool skip_body)190 ir_reader::read_function(s_expression *expr, bool skip_body)
191 {
192    bool added = false;
193    s_symbol *name;
194 
195    s_pattern pat[] = { "function", name };
196    if (!PARTIAL_MATCH(expr, pat)) {
197       ir_read_error(expr, "Expected (function <name> (signature ...) ...)");
198       return NULL;
199    }
200 
201    ir_function *f = state->symbols->get_function(name->value());
202    if (f == NULL) {
203       f = new(mem_ctx) ir_function(name->value());
204       added = state->symbols->add_function(f);
205       assert(added);
206    }
207 
208    /* Skip over "function" tag and function name (which are guaranteed to be
209     * present by the above PARTIAL_MATCH call).
210     */
211    exec_node *node = ((s_list *) expr)->subexpressions.get_head_raw()->next->next;
212    for (/* nothing */; !node->is_tail_sentinel(); node = node->next) {
213       s_expression *s_sig = (s_expression *) node;
214       read_function_sig(f, s_sig, skip_body);
215    }
216    return added ? f : NULL;
217 }
218 
219 static bool
always_available(const _mesa_glsl_parse_state *)220 always_available(const _mesa_glsl_parse_state *)
221 {
222    return true;
223 }
224 
225 void
read_function_sig(ir_function * f,s_expression * expr,bool skip_body)226 ir_reader::read_function_sig(ir_function *f, s_expression *expr, bool skip_body)
227 {
228    s_expression *type_expr;
229    s_list *paramlist;
230    s_list *body_list;
231 
232    s_pattern pat[] = { "signature", type_expr, paramlist, body_list };
233    if (!MATCH(expr, pat)) {
234       ir_read_error(expr, "Expected (signature <type> (parameters ...) "
235 			  "(<instruction> ...))");
236       return;
237    }
238 
239    const glsl_type *return_type = read_type(type_expr);
240    if (return_type == NULL)
241       return;
242 
243    s_symbol *paramtag = SX_AS_SYMBOL(paramlist->subexpressions.get_head());
244    if (paramtag == NULL || strcmp(paramtag->value(), "parameters") != 0) {
245       ir_read_error(paramlist, "Expected (parameters ...)");
246       return;
247    }
248 
249    // Read the parameters list into a temporary place.
250    exec_list hir_parameters;
251    state->symbols->push_scope();
252 
253    /* Skip over the "parameters" tag. */
254    exec_node *node = paramlist->subexpressions.get_head_raw()->next;
255    for (/* nothing */; !node->is_tail_sentinel(); node = node->next) {
256       ir_variable *var = read_declaration((s_expression *) node);
257       if (var == NULL)
258 	 return;
259 
260       hir_parameters.push_tail(var);
261    }
262 
263    ir_function_signature *sig =
264       f->exact_matching_signature(state, &hir_parameters);
265    if (sig == NULL && skip_body) {
266       /* If scanning for prototypes, generate a new signature. */
267       /* ir_reader doesn't know what languages support a given built-in, so
268        * just say that they're always available.  For now, other mechanisms
269        * guarantee the right built-ins are available.
270        */
271       sig = new(mem_ctx) ir_function_signature(return_type, always_available);
272       f->add_signature(sig);
273    } else if (sig != NULL) {
274       const char *badvar = sig->qualifiers_match(&hir_parameters);
275       if (badvar != NULL) {
276 	 ir_read_error(expr, "function `%s' parameter `%s' qualifiers "
277 		       "don't match prototype", f->name, badvar);
278 	 return;
279       }
280 
281       if (sig->return_type != return_type) {
282 	 ir_read_error(expr, "function `%s' return type doesn't "
283 		       "match prototype", f->name);
284 	 return;
285       }
286    } else {
287       /* No prototype for this body exists - skip it. */
288       state->symbols->pop_scope();
289       return;
290    }
291    assert(sig != NULL);
292 
293    sig->replace_parameters(&hir_parameters);
294 
295    if (!skip_body && !body_list->subexpressions.is_empty()) {
296       if (sig->is_defined) {
297 	 ir_read_error(expr, "function %s redefined", f->name);
298 	 return;
299       }
300       state->current_function = sig;
301       read_instructions(&sig->body, body_list, NULL);
302       state->current_function = NULL;
303       sig->is_defined = true;
304    }
305 
306    state->symbols->pop_scope();
307 }
308 
309 void
read_instructions(exec_list * instructions,s_expression * expr,ir_loop * loop_ctx)310 ir_reader::read_instructions(exec_list *instructions, s_expression *expr,
311 			     ir_loop *loop_ctx)
312 {
313    // Read in a list of instructions
314    s_list *list = SX_AS_LIST(expr);
315    if (list == NULL) {
316       ir_read_error(expr, "Expected (<instruction> ...); found an atom.");
317       return;
318    }
319 
320    foreach_in_list(s_expression, sub, &list->subexpressions) {
321       ir_instruction *ir = read_instruction(sub, loop_ctx);
322       if (ir != NULL) {
323 	 /* Global variable declarations should be moved to the top, before
324 	  * any functions that might use them.  Functions are added to the
325 	  * instruction stream when scanning for prototypes, so without this
326 	  * hack, they always appear before variable declarations.
327 	  */
328 	 if (state->current_function == NULL && ir->as_variable() != NULL)
329 	    instructions->push_head(ir);
330 	 else
331 	    instructions->push_tail(ir);
332       }
333    }
334 }
335 
336 
337 ir_instruction *
read_instruction(s_expression * expr,ir_loop * loop_ctx)338 ir_reader::read_instruction(s_expression *expr, ir_loop *loop_ctx)
339 {
340    s_symbol *symbol = SX_AS_SYMBOL(expr);
341    if (symbol != NULL) {
342       if (strcmp(symbol->value(), "break") == 0 && loop_ctx != NULL)
343 	 return new(mem_ctx) ir_loop_jump(ir_loop_jump::jump_break);
344       if (strcmp(symbol->value(), "continue") == 0 && loop_ctx != NULL)
345 	 return new(mem_ctx) ir_loop_jump(ir_loop_jump::jump_continue);
346    }
347 
348    s_list *list = SX_AS_LIST(expr);
349    if (list == NULL || list->subexpressions.is_empty()) {
350       ir_read_error(expr, "Invalid instruction.\n");
351       return NULL;
352    }
353 
354    s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
355    if (tag == NULL) {
356       ir_read_error(expr, "expected instruction tag");
357       return NULL;
358    }
359 
360    ir_instruction *inst = NULL;
361    if (strcmp(tag->value(), "declare") == 0) {
362       inst = read_declaration(list);
363    } else if (strcmp(tag->value(), "assign") == 0) {
364       inst = read_assignment(list);
365    } else if (strcmp(tag->value(), "if") == 0) {
366       inst = read_if(list, loop_ctx);
367    } else if (strcmp(tag->value(), "loop") == 0) {
368       inst = read_loop(list);
369    } else if (strcmp(tag->value(), "call") == 0) {
370       inst = read_call(list);
371    } else if (strcmp(tag->value(), "return") == 0) {
372       inst = read_return(list);
373    } else if (strcmp(tag->value(), "function") == 0) {
374       inst = read_function(list, false);
375    } else if (strcmp(tag->value(), "emit-vertex") == 0) {
376       inst = read_emit_vertex(list);
377    } else if (strcmp(tag->value(), "end-primitive") == 0) {
378       inst = read_end_primitive(list);
379    } else if (strcmp(tag->value(), "barrier") == 0) {
380       inst = read_barrier(list);
381    } else {
382       inst = read_rvalue(list);
383       if (inst == NULL)
384 	 ir_read_error(NULL, "when reading instruction");
385    }
386    return inst;
387 }
388 
389 ir_variable *
read_declaration(s_expression * expr)390 ir_reader::read_declaration(s_expression *expr)
391 {
392    s_list *s_quals;
393    s_expression *s_type;
394    s_symbol *s_name;
395 
396    s_pattern pat[] = { "declare", s_quals, s_type, s_name };
397    if (!MATCH(expr, pat)) {
398       ir_read_error(expr, "expected (declare (<qualifiers>) <type> <name>)");
399       return NULL;
400    }
401 
402    const glsl_type *type = read_type(s_type);
403    if (type == NULL)
404       return NULL;
405 
406    ir_variable *var = new(mem_ctx) ir_variable(type, s_name->value(),
407 					       ir_var_auto);
408 
409    foreach_in_list(s_symbol, qualifier, &s_quals->subexpressions) {
410       if (!qualifier->is_symbol()) {
411 	 ir_read_error(expr, "qualifier list must contain only symbols");
412 	 return NULL;
413       }
414 
415       // FINISHME: Check for duplicate/conflicting qualifiers.
416       if (strcmp(qualifier->value(), "centroid") == 0) {
417 	 var->data.centroid = 1;
418       } else if (strcmp(qualifier->value(), "sample") == 0) {
419          var->data.sample = 1;
420       } else if (strcmp(qualifier->value(), "patch") == 0) {
421          var->data.patch = 1;
422       } else if (strcmp(qualifier->value(), "explicit_invariant") == 0) {
423          var->data.explicit_invariant = true;
424       } else if (strcmp(qualifier->value(), "invariant") == 0) {
425          var->data.invariant = true;
426       } else if (strcmp(qualifier->value(), "uniform") == 0) {
427 	 var->data.mode = ir_var_uniform;
428       } else if (strcmp(qualifier->value(), "shader_storage") == 0) {
429 	 var->data.mode = ir_var_shader_storage;
430       } else if (strcmp(qualifier->value(), "auto") == 0) {
431 	 var->data.mode = ir_var_auto;
432       } else if (strcmp(qualifier->value(), "in") == 0) {
433 	 var->data.mode = ir_var_function_in;
434       } else if (strcmp(qualifier->value(), "shader_in") == 0) {
435          var->data.mode = ir_var_shader_in;
436       } else if (strcmp(qualifier->value(), "const_in") == 0) {
437 	 var->data.mode = ir_var_const_in;
438       } else if (strcmp(qualifier->value(), "out") == 0) {
439 	 var->data.mode = ir_var_function_out;
440       } else if (strcmp(qualifier->value(), "shader_out") == 0) {
441 	 var->data.mode = ir_var_shader_out;
442       } else if (strcmp(qualifier->value(), "inout") == 0) {
443 	 var->data.mode = ir_var_function_inout;
444       } else if (strcmp(qualifier->value(), "temporary") == 0) {
445 	 var->data.mode = ir_var_temporary;
446       } else if (strcmp(qualifier->value(), "stream1") == 0) {
447 	 var->data.stream = 1;
448       } else if (strcmp(qualifier->value(), "stream2") == 0) {
449 	 var->data.stream = 2;
450       } else if (strcmp(qualifier->value(), "stream3") == 0) {
451 	 var->data.stream = 3;
452       } else if (strcmp(qualifier->value(), "smooth") == 0) {
453 	 var->data.interpolation = INTERP_MODE_SMOOTH;
454       } else if (strcmp(qualifier->value(), "flat") == 0) {
455 	 var->data.interpolation = INTERP_MODE_FLAT;
456       } else if (strcmp(qualifier->value(), "noperspective") == 0) {
457 	 var->data.interpolation = INTERP_MODE_NOPERSPECTIVE;
458       } else {
459 	 ir_read_error(expr, "unknown qualifier: %s", qualifier->value());
460 	 return NULL;
461       }
462    }
463 
464    // Add the variable to the symbol table
465    state->symbols->add_variable(var);
466 
467    return var;
468 }
469 
470 
471 ir_if *
read_if(s_expression * expr,ir_loop * loop_ctx)472 ir_reader::read_if(s_expression *expr, ir_loop *loop_ctx)
473 {
474    s_expression *s_cond;
475    s_expression *s_then;
476    s_expression *s_else;
477 
478    s_pattern pat[] = { "if", s_cond, s_then, s_else };
479    if (!MATCH(expr, pat)) {
480       ir_read_error(expr, "expected (if <condition> (<then>...) (<else>...))");
481       return NULL;
482    }
483 
484    ir_rvalue *condition = read_rvalue(s_cond);
485    if (condition == NULL) {
486       ir_read_error(NULL, "when reading condition of (if ...)");
487       return NULL;
488    }
489 
490    ir_if *iff = new(mem_ctx) ir_if(condition);
491 
492    read_instructions(&iff->then_instructions, s_then, loop_ctx);
493    read_instructions(&iff->else_instructions, s_else, loop_ctx);
494    if (state->error) {
495       delete iff;
496       iff = NULL;
497    }
498    return iff;
499 }
500 
501 
502 ir_loop *
read_loop(s_expression * expr)503 ir_reader::read_loop(s_expression *expr)
504 {
505    s_expression *s_body;
506 
507    s_pattern loop_pat[] = { "loop", s_body };
508    if (!MATCH(expr, loop_pat)) {
509       ir_read_error(expr, "expected (loop <body>)");
510       return NULL;
511    }
512 
513    ir_loop *loop = new(mem_ctx) ir_loop;
514 
515    read_instructions(&loop->body_instructions, s_body, loop);
516    if (state->error) {
517       delete loop;
518       loop = NULL;
519    }
520    return loop;
521 }
522 
523 
524 ir_return *
read_return(s_expression * expr)525 ir_reader::read_return(s_expression *expr)
526 {
527    s_expression *s_retval;
528 
529    s_pattern return_value_pat[] = { "return", s_retval};
530    s_pattern return_void_pat[] = { "return" };
531    if (MATCH(expr, return_value_pat)) {
532       ir_rvalue *retval = read_rvalue(s_retval);
533       if (retval == NULL) {
534          ir_read_error(NULL, "when reading return value");
535          return NULL;
536       }
537       return new(mem_ctx) ir_return(retval);
538    } else if (MATCH(expr, return_void_pat)) {
539       return new(mem_ctx) ir_return;
540    } else {
541       ir_read_error(expr, "expected (return <rvalue>) or (return)");
542       return NULL;
543    }
544 }
545 
546 
547 ir_rvalue *
read_rvalue(s_expression * expr)548 ir_reader::read_rvalue(s_expression *expr)
549 {
550    s_list *list = SX_AS_LIST(expr);
551    if (list == NULL || list->subexpressions.is_empty())
552       return NULL;
553 
554    s_symbol *tag = SX_AS_SYMBOL(list->subexpressions.get_head());
555    if (tag == NULL) {
556       ir_read_error(expr, "expected rvalue tag");
557       return NULL;
558    }
559 
560    ir_rvalue *rvalue = read_dereference(list);
561    if (rvalue != NULL || state->error)
562       return rvalue;
563    else if (strcmp(tag->value(), "swiz") == 0) {
564       rvalue = read_swizzle(list);
565    } else if (strcmp(tag->value(), "expression") == 0) {
566       rvalue = read_expression(list);
567    } else if (strcmp(tag->value(), "constant") == 0) {
568       rvalue = read_constant(list);
569    } else {
570       rvalue = read_texture(list);
571       if (rvalue == NULL && !state->error)
572 	 ir_read_error(expr, "unrecognized rvalue tag: %s", tag->value());
573    }
574 
575    return rvalue;
576 }
577 
578 ir_assignment *
read_assignment(s_expression * expr)579 ir_reader::read_assignment(s_expression *expr)
580 {
581    s_expression *cond_expr = NULL;
582    s_expression *lhs_expr, *rhs_expr;
583    s_list       *mask_list;
584 
585    s_pattern pat4[] = { "assign",            mask_list, lhs_expr, rhs_expr };
586    s_pattern pat5[] = { "assign", cond_expr, mask_list, lhs_expr, rhs_expr };
587    if (!MATCH(expr, pat4) && !MATCH(expr, pat5)) {
588       ir_read_error(expr, "expected (assign (<write mask>) <lhs> <rhs>)");
589       return NULL;
590    }
591 
592    if (cond_expr != NULL) {
593       ir_rvalue *condition = read_rvalue(cond_expr);
594       if (condition == NULL)
595          ir_read_error(NULL, "when reading condition of assignment");
596       else
597          ir_read_error(expr, "conditional assignemnts are deprecated");
598 
599       return NULL;
600    }
601 
602    unsigned mask = 0;
603 
604    s_symbol *mask_symbol;
605    s_pattern mask_pat[] = { mask_symbol };
606    if (MATCH(mask_list, mask_pat)) {
607       const char *mask_str = mask_symbol->value();
608       unsigned mask_length = strlen(mask_str);
609       if (mask_length > 4) {
610 	 ir_read_error(expr, "invalid write mask: %s", mask_str);
611 	 return NULL;
612       }
613 
614       const unsigned idx_map[] = { 3, 0, 1, 2 }; /* w=bit 3, x=0, y=1, z=2 */
615 
616       for (unsigned i = 0; i < mask_length; i++) {
617 	 if (mask_str[i] < 'w' || mask_str[i] > 'z') {
618 	    ir_read_error(expr, "write mask contains invalid character: %c",
619 			  mask_str[i]);
620 	    return NULL;
621 	 }
622 	 mask |= 1 << idx_map[mask_str[i] - 'w'];
623       }
624    } else if (!mask_list->subexpressions.is_empty()) {
625       ir_read_error(mask_list, "expected () or (<write mask>)");
626       return NULL;
627    }
628 
629    ir_dereference *lhs = read_dereference(lhs_expr);
630    if (lhs == NULL) {
631       ir_read_error(NULL, "when reading left-hand side of assignment");
632       return NULL;
633    }
634 
635    ir_rvalue *rhs = read_rvalue(rhs_expr);
636    if (rhs == NULL) {
637       ir_read_error(NULL, "when reading right-hand side of assignment");
638       return NULL;
639    }
640 
641    if (mask == 0 && (glsl_type_is_vector(lhs->type) || glsl_type_is_scalar(lhs->type))) {
642       ir_read_error(expr, "non-zero write mask required.");
643       return NULL;
644    }
645 
646    return new(mem_ctx) ir_assignment(lhs, rhs, mask);
647 }
648 
649 ir_call *
read_call(s_expression * expr)650 ir_reader::read_call(s_expression *expr)
651 {
652    s_symbol *name;
653    s_list *params;
654    s_list *s_return = NULL;
655 
656    ir_dereference_variable *return_deref = NULL;
657 
658    s_pattern void_pat[] = { "call", name, params };
659    s_pattern non_void_pat[] = { "call", name, s_return, params };
660    if (MATCH(expr, non_void_pat)) {
661       return_deref = read_var_ref(s_return);
662       if (return_deref == NULL) {
663 	 ir_read_error(s_return, "when reading a call's return storage");
664 	 return NULL;
665       }
666    } else if (!MATCH(expr, void_pat)) {
667       ir_read_error(expr, "expected (call <name> [<deref>] (<param> ...))");
668       return NULL;
669    }
670 
671    exec_list parameters;
672 
673    foreach_in_list(s_expression, e, &params->subexpressions) {
674       ir_rvalue *param = read_rvalue(e);
675       if (param == NULL) {
676 	 ir_read_error(e, "when reading parameter to function call");
677 	 return NULL;
678       }
679       parameters.push_tail(param);
680    }
681 
682    ir_function *f = state->symbols->get_function(name->value());
683    if (f == NULL) {
684       ir_read_error(expr, "found call to undefined function %s",
685 		    name->value());
686       return NULL;
687    }
688 
689    ir_function_signature *callee =
690       f->matching_signature(state, &parameters,
691                             state->has_implicit_conversions(),
692                             state->has_implicit_int_to_uint_conversion(),
693                             true);
694    if (callee == NULL) {
695       ir_read_error(expr, "couldn't find matching signature for function "
696                     "%s", name->value());
697       return NULL;
698    }
699 
700    if (callee->return_type == &glsl_type_builtin_void && return_deref) {
701       ir_read_error(expr, "call has return value storage but void type");
702       return NULL;
703    } else if (callee->return_type != &glsl_type_builtin_void && !return_deref) {
704       ir_read_error(expr, "call has non-void type but no return value storage");
705       return NULL;
706    }
707 
708    return new(mem_ctx) ir_call(callee, return_deref, &parameters);
709 }
710 
711 ir_expression *
read_expression(s_expression * expr)712 ir_reader::read_expression(s_expression *expr)
713 {
714    s_expression *s_type;
715    s_symbol *s_op;
716    s_expression *s_arg[4] = {NULL};
717 
718    s_pattern pat[] = { "expression", s_type, s_op, s_arg[0] };
719    if (!PARTIAL_MATCH(expr, pat)) {
720       ir_read_error(expr, "expected (expression <type> <operator> "
721 			  "<operand> [<operand>] [<operand>] [<operand>])");
722       return NULL;
723    }
724    s_arg[1] = (s_expression *) s_arg[0]->next; // may be tail sentinel
725    s_arg[2] = (s_expression *) s_arg[1]->next; // may be tail sentinel or NULL
726    if (s_arg[2])
727       s_arg[3] = (s_expression *) s_arg[2]->next; // may be tail sentinel or NULL
728 
729    const glsl_type *type = read_type(s_type);
730    if (type == NULL)
731       return NULL;
732 
733    /* Read the operator */
734    ir_expression_operation op = ir_expression::get_operator(s_op->value());
735    if (op == (ir_expression_operation) -1) {
736       ir_read_error(expr, "invalid operator: %s", s_op->value());
737       return NULL;
738    }
739 
740    /* Skip "expression" <type> <operation> by subtracting 3. */
741    int num_operands = (int) ((s_list *) expr)->subexpressions.length() - 3;
742 
743    int expected_operands = ir_expression::get_num_operands(op);
744    if (num_operands != expected_operands) {
745       ir_read_error(expr, "found %d expression operands, expected %d",
746                     num_operands, expected_operands);
747       return NULL;
748    }
749 
750    ir_rvalue *arg[4] = {NULL};
751    for (int i = 0; i < num_operands; i++) {
752       arg[i] = read_rvalue(s_arg[i]);
753       if (arg[i] == NULL) {
754          ir_read_error(NULL, "when reading operand #%d of %s", i, s_op->value());
755          return NULL;
756       }
757    }
758 
759    return new(mem_ctx) ir_expression(op, type, arg[0], arg[1], arg[2], arg[3]);
760 }
761 
762 ir_swizzle *
read_swizzle(s_expression * expr)763 ir_reader::read_swizzle(s_expression *expr)
764 {
765    s_symbol *swiz;
766    s_expression *sub;
767 
768    s_pattern pat[] = { "swiz", swiz, sub };
769    if (!MATCH(expr, pat)) {
770       ir_read_error(expr, "expected (swiz <swizzle> <rvalue>)");
771       return NULL;
772    }
773 
774    if (strlen(swiz->value()) > 4) {
775       ir_read_error(expr, "expected a valid swizzle; found %s", swiz->value());
776       return NULL;
777    }
778 
779    ir_rvalue *rvalue = read_rvalue(sub);
780    if (rvalue == NULL)
781       return NULL;
782 
783    ir_swizzle *ir = ir_swizzle::create(rvalue, swiz->value(),
784 				       rvalue->type->vector_elements);
785    if (ir == NULL)
786       ir_read_error(expr, "invalid swizzle");
787 
788    return ir;
789 }
790 
791 ir_constant *
read_constant(s_expression * expr)792 ir_reader::read_constant(s_expression *expr)
793 {
794    s_expression *type_expr;
795    s_list *values;
796 
797    s_pattern pat[] = { "constant", type_expr, values };
798    if (!MATCH(expr, pat)) {
799       ir_read_error(expr, "expected (constant <type> (...))");
800       return NULL;
801    }
802 
803    const glsl_type *type = read_type(type_expr);
804    if (type == NULL)
805       return NULL;
806 
807    if (values == NULL) {
808       ir_read_error(expr, "expected (constant <type> (...))");
809       return NULL;
810    }
811 
812    if (glsl_type_is_array(type)) {
813       unsigned elements_supplied = 0;
814       exec_list elements;
815       foreach_in_list(s_expression, elt, &values->subexpressions) {
816 	 ir_constant *ir_elt = read_constant(elt);
817 	 if (ir_elt == NULL)
818 	    return NULL;
819 	 elements.push_tail(ir_elt);
820 	 elements_supplied++;
821       }
822 
823       if (elements_supplied != type->length) {
824 	 ir_read_error(values, "expected exactly %u array elements, "
825 		       "given %u", type->length, elements_supplied);
826 	 return NULL;
827       }
828       return new(mem_ctx) ir_constant(type, &elements);
829    }
830 
831    ir_constant_data data = { { 0 } };
832 
833    // Read in list of values (at most 16).
834    unsigned k = 0;
835    foreach_in_list(s_expression, expr, &values->subexpressions) {
836       if (k >= 16) {
837 	 ir_read_error(values, "expected at most 16 numbers");
838 	 return NULL;
839       }
840 
841       if (glsl_type_is_float(type)) {
842 	 s_number *value = SX_AS_NUMBER(expr);
843 	 if (value == NULL) {
844 	    ir_read_error(values, "expected numbers");
845 	    return NULL;
846 	 }
847 	 data.f[k] = value->fvalue();
848       } else {
849 	 s_int *value = SX_AS_INT(expr);
850 	 if (value == NULL) {
851 	    ir_read_error(values, "expected integers");
852 	    return NULL;
853 	 }
854 
855 	 switch (type->base_type) {
856 	 case GLSL_TYPE_UINT: {
857 	    data.u[k] = value->value();
858 	    break;
859 	 }
860 	 case GLSL_TYPE_INT: {
861 	    data.i[k] = value->value();
862 	    break;
863 	 }
864 	 case GLSL_TYPE_BOOL: {
865 	    data.b[k] = value->value();
866 	    break;
867 	 }
868 	 default:
869 	    ir_read_error(values, "unsupported constant type");
870 	    return NULL;
871 	 }
872       }
873       ++k;
874    }
875    if (k != glsl_get_components(type)) {
876       ir_read_error(values, "expected %u constant values, found %u",
877 		    glsl_get_components(type), k);
878       return NULL;
879    }
880 
881    return new(mem_ctx) ir_constant(type, &data);
882 }
883 
884 ir_dereference_variable *
read_var_ref(s_expression * expr)885 ir_reader::read_var_ref(s_expression *expr)
886 {
887    s_symbol *s_var;
888    s_pattern var_pat[] = { "var_ref", s_var };
889 
890    if (MATCH(expr, var_pat)) {
891       ir_variable *var = state->symbols->get_variable(s_var->value());
892       if (var == NULL) {
893 	 ir_read_error(expr, "undeclared variable: %s", s_var->value());
894 	 return NULL;
895       }
896       return new(mem_ctx) ir_dereference_variable(var);
897    }
898    return NULL;
899 }
900 
901 ir_dereference *
read_dereference(s_expression * expr)902 ir_reader::read_dereference(s_expression *expr)
903 {
904    s_expression *s_subject;
905    s_expression *s_index;
906    s_symbol *s_field;
907 
908    s_pattern array_pat[] = { "array_ref", s_subject, s_index };
909    s_pattern record_pat[] = { "record_ref", s_subject, s_field };
910 
911    ir_dereference_variable *var_ref = read_var_ref(expr);
912    if (var_ref != NULL) {
913       return var_ref;
914    } else if (MATCH(expr, array_pat)) {
915       ir_rvalue *subject = read_rvalue(s_subject);
916       if (subject == NULL) {
917 	 ir_read_error(NULL, "when reading the subject of an array_ref");
918 	 return NULL;
919       }
920 
921       ir_rvalue *idx = read_rvalue(s_index);
922       if (idx == NULL) {
923 	 ir_read_error(NULL, "when reading the index of an array_ref");
924 	 return NULL;
925       }
926       return new(mem_ctx) ir_dereference_array(subject, idx);
927    } else if (MATCH(expr, record_pat)) {
928       ir_rvalue *subject = read_rvalue(s_subject);
929       if (subject == NULL) {
930 	 ir_read_error(NULL, "when reading the subject of a record_ref");
931 	 return NULL;
932       }
933       return new(mem_ctx) ir_dereference_record(subject, s_field->value());
934    }
935    return NULL;
936 }
937 
938 ir_texture *
read_texture(s_expression * expr)939 ir_reader::read_texture(s_expression *expr)
940 {
941    s_symbol *tag = NULL;
942    s_expression *s_sparse = NULL;
943    s_expression *s_type = NULL;
944    s_expression *s_sampler = NULL;
945    s_expression *s_coord = NULL;
946    s_expression *s_offset = NULL;
947    s_expression *s_proj = NULL;
948    s_list *s_shadow = NULL;
949    s_list *s_clamp = NULL;
950    s_expression *s_lod = NULL;
951    s_expression *s_sample_index = NULL;
952    s_expression *s_component = NULL;
953 
954    ir_texture_opcode op = ir_tex; /* silence warning */
955 
956    s_pattern tex_pattern[] =
957       { "tex", s_type, s_sampler, s_coord, s_sparse, s_offset, s_proj, s_shadow, s_clamp };
958    s_pattern txb_pattern[] =
959       { "txb", s_type, s_sampler, s_coord, s_sparse, s_offset, s_proj, s_shadow, s_clamp, s_lod };
960    s_pattern txd_pattern[] =
961       { "txd", s_type, s_sampler, s_coord, s_sparse, s_offset, s_proj, s_shadow, s_clamp, s_lod };
962    s_pattern lod_pattern[] =
963       { "lod", s_type, s_sampler, s_coord };
964    s_pattern txf_pattern[] =
965       { "txf", s_type, s_sampler, s_coord, s_sparse, s_offset, s_lod };
966    s_pattern txf_ms_pattern[] =
967       { "txf_ms", s_type, s_sampler, s_coord, s_sparse, s_sample_index };
968    s_pattern txs_pattern[] =
969       { "txs", s_type, s_sampler, s_lod };
970    s_pattern tg4_pattern[] =
971       { "tg4", s_type, s_sampler, s_coord, s_sparse, s_offset, s_component };
972    s_pattern query_levels_pattern[] =
973       { "query_levels", s_type, s_sampler };
974    s_pattern texture_samples_pattern[] =
975       { "samples", s_type, s_sampler };
976    s_pattern other_pattern[] =
977       { tag, s_type, s_sampler, s_coord, s_sparse, s_offset, s_proj, s_shadow, s_lod };
978 
979    if (MATCH(expr, lod_pattern)) {
980       op = ir_lod;
981    } else if (MATCH(expr, tex_pattern)) {
982       op = ir_tex;
983    } else if (MATCH(expr, txb_pattern)) {
984       op = ir_txb;
985    } else if (MATCH(expr, txd_pattern)) {
986       op = ir_txd;
987    } else if (MATCH(expr, txf_pattern)) {
988       op = ir_txf;
989    } else if (MATCH(expr, txf_ms_pattern)) {
990       op = ir_txf_ms;
991    } else if (MATCH(expr, txs_pattern)) {
992       op = ir_txs;
993    } else if (MATCH(expr, tg4_pattern)) {
994       op = ir_tg4;
995    } else if (MATCH(expr, query_levels_pattern)) {
996       op = ir_query_levels;
997    } else if (MATCH(expr, texture_samples_pattern)) {
998       op = ir_texture_samples;
999    } else if (MATCH(expr, other_pattern)) {
1000       op = ir_texture::get_opcode(tag->value());
1001       if (op == (ir_texture_opcode) -1)
1002 	 return NULL;
1003    } else {
1004       ir_read_error(NULL, "unexpected texture pattern %s", tag->value());
1005       return NULL;
1006    }
1007 
1008    bool is_sparse = false;
1009    if (s_sparse) {
1010       s_int *sparse = SX_AS_INT(s_sparse);
1011       if (sparse == NULL) {
1012          ir_read_error(NULL, "when reading sparse");
1013          return NULL;
1014       }
1015       is_sparse = sparse->value();
1016    }
1017 
1018    ir_texture *tex = new(mem_ctx) ir_texture(op, is_sparse);
1019 
1020    // Read return type
1021    const glsl_type *type = read_type(s_type);
1022    if (type == NULL) {
1023       ir_read_error(NULL, "when reading type in (%s ...)",
1024 		    tex->opcode_string());
1025       return NULL;
1026    }
1027 
1028    // Read sampler (must be a deref)
1029    ir_dereference *sampler = read_dereference(s_sampler);
1030    if (sampler == NULL) {
1031       ir_read_error(NULL, "when reading sampler in (%s ...)",
1032 		    tex->opcode_string());
1033       return NULL;
1034    }
1035 
1036    if (is_sparse) {
1037       const glsl_type *texel = glsl_get_field_type(type, "texel");
1038       if (texel == &glsl_type_builtin_error) {
1039          ir_read_error(NULL, "invalid type for sparse texture");
1040          return NULL;
1041       }
1042       type = texel;
1043    }
1044    tex->set_sampler(sampler, type);
1045 
1046    if (op != ir_txs) {
1047       // Read coordinate (any rvalue)
1048       tex->coordinate = read_rvalue(s_coord);
1049       if (tex->coordinate == NULL) {
1050 	 ir_read_error(NULL, "when reading coordinate in (%s ...)",
1051 		       tex->opcode_string());
1052 	 return NULL;
1053       }
1054 
1055       if (op != ir_txf_ms && op != ir_lod) {
1056          // Read texel offset - either 0 or an rvalue.
1057          s_int *si_offset = SX_AS_INT(s_offset);
1058          if (si_offset == NULL || si_offset->value() != 0) {
1059             tex->offset = read_rvalue(s_offset);
1060             if (tex->offset == NULL) {
1061                ir_read_error(s_offset, "expected 0 or an expression");
1062                return NULL;
1063             }
1064          }
1065       }
1066    }
1067 
1068    if (op != ir_txf && op != ir_txf_ms &&
1069        op != ir_txs && op != ir_lod && op != ir_tg4 &&
1070        op != ir_query_levels && op != ir_texture_samples) {
1071       s_int *proj_as_int = SX_AS_INT(s_proj);
1072       if (proj_as_int && proj_as_int->value() == 1) {
1073 	 tex->projector = NULL;
1074       } else {
1075 	 tex->projector = read_rvalue(s_proj);
1076 	 if (tex->projector == NULL) {
1077 	    ir_read_error(NULL, "when reading projective divide in (%s ..)",
1078 	                  tex->opcode_string());
1079 	    return NULL;
1080 	 }
1081       }
1082 
1083       if (s_shadow->subexpressions.is_empty()) {
1084 	 tex->shadow_comparator = NULL;
1085       } else {
1086 	 tex->shadow_comparator = read_rvalue(s_shadow);
1087 	 if (tex->shadow_comparator == NULL) {
1088 	    ir_read_error(NULL, "when reading shadow comparator in (%s ..)",
1089 			  tex->opcode_string());
1090 	    return NULL;
1091 	 }
1092       }
1093    }
1094 
1095    if (op == ir_tex || op == ir_txb || op == ir_txd) {
1096       if (s_clamp->subexpressions.is_empty()) {
1097          tex->clamp = NULL;
1098       } else {
1099          tex->clamp = read_rvalue(s_clamp);
1100          if (tex->clamp == NULL) {
1101             ir_read_error(NULL, "when reading clamp in (%s ..)",
1102                           tex->opcode_string());
1103             return NULL;
1104          }
1105       }
1106    }
1107 
1108    switch (op) {
1109    case ir_txb:
1110       tex->lod_info.bias = read_rvalue(s_lod);
1111       if (tex->lod_info.bias == NULL) {
1112 	 ir_read_error(NULL, "when reading LOD bias in (txb ...)");
1113 	 return NULL;
1114       }
1115       break;
1116    case ir_txl:
1117    case ir_txf:
1118    case ir_txs:
1119       tex->lod_info.lod = read_rvalue(s_lod);
1120       if (tex->lod_info.lod == NULL) {
1121 	 ir_read_error(NULL, "when reading LOD in (%s ...)",
1122 		       tex->opcode_string());
1123 	 return NULL;
1124       }
1125       break;
1126    case ir_txf_ms:
1127       tex->lod_info.sample_index = read_rvalue(s_sample_index);
1128       if (tex->lod_info.sample_index == NULL) {
1129          ir_read_error(NULL, "when reading sample_index in (txf_ms ...)");
1130          return NULL;
1131       }
1132       break;
1133    case ir_txd: {
1134       s_expression *s_dx, *s_dy;
1135       s_pattern dxdy_pat[] = { s_dx, s_dy };
1136       if (!MATCH(s_lod, dxdy_pat)) {
1137 	 ir_read_error(s_lod, "expected (dPdx dPdy) in (txd ...)");
1138 	 return NULL;
1139       }
1140       tex->lod_info.grad.dPdx = read_rvalue(s_dx);
1141       if (tex->lod_info.grad.dPdx == NULL) {
1142 	 ir_read_error(NULL, "when reading dPdx in (txd ...)");
1143 	 return NULL;
1144       }
1145       tex->lod_info.grad.dPdy = read_rvalue(s_dy);
1146       if (tex->lod_info.grad.dPdy == NULL) {
1147 	 ir_read_error(NULL, "when reading dPdy in (txd ...)");
1148 	 return NULL;
1149       }
1150       break;
1151    }
1152    case ir_tg4:
1153       tex->lod_info.component = read_rvalue(s_component);
1154       if (tex->lod_info.component == NULL) {
1155          ir_read_error(NULL, "when reading component in (tg4 ...)");
1156          return NULL;
1157       }
1158       break;
1159    default:
1160       // tex and lod don't have any extra parameters.
1161       break;
1162    };
1163    return tex;
1164 }
1165 
1166 ir_emit_vertex *
read_emit_vertex(s_expression * expr)1167 ir_reader::read_emit_vertex(s_expression *expr)
1168 {
1169    s_expression *s_stream = NULL;
1170 
1171    s_pattern pat[] = { "emit-vertex", s_stream };
1172 
1173    if (MATCH(expr, pat)) {
1174       ir_rvalue *stream = read_dereference(s_stream);
1175       if (stream == NULL) {
1176          ir_read_error(NULL, "when reading stream info in emit-vertex");
1177          return NULL;
1178       }
1179       return new(mem_ctx) ir_emit_vertex(stream);
1180    }
1181    ir_read_error(NULL, "when reading emit-vertex");
1182    return NULL;
1183 }
1184 
1185 ir_end_primitive *
read_end_primitive(s_expression * expr)1186 ir_reader::read_end_primitive(s_expression *expr)
1187 {
1188    s_expression *s_stream = NULL;
1189 
1190    s_pattern pat[] = { "end-primitive", s_stream };
1191 
1192    if (MATCH(expr, pat)) {
1193       ir_rvalue *stream = read_dereference(s_stream);
1194       if (stream == NULL) {
1195          ir_read_error(NULL, "when reading stream info in end-primitive");
1196          return NULL;
1197       }
1198       return new(mem_ctx) ir_end_primitive(stream);
1199    }
1200    ir_read_error(NULL, "when reading end-primitive");
1201    return NULL;
1202 }
1203 
1204 ir_barrier *
read_barrier(s_expression * expr)1205 ir_reader::read_barrier(s_expression *expr)
1206 {
1207    s_pattern pat[] = { "barrier" };
1208 
1209    if (MATCH(expr, pat)) {
1210       return new(mem_ctx) ir_barrier();
1211    }
1212    ir_read_error(NULL, "when reading barrier");
1213    return NULL;
1214 }
1215