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