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