Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008, 2009 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 | #include <stdio.h> |
| 24 | #include <stdarg.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <assert.h> |
| 28 | |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <unistd.h> |
| 33 | |
| 34 | #include "ast.h" |
| 35 | #include "glsl_parser_extras.h" |
Ian Romanick | d59673c | 2010-02-25 17:17:23 -0800 | [diff] [blame] | 36 | #include "glsl_parser.h" |
Eric Anholt | 6273569 | 2010-04-05 15:24:28 -0700 | [diff] [blame] | 37 | #include "ir_constant_folding.h" |
Ian Romanick | 1c4156f | 2010-03-10 09:27:03 -0800 | [diff] [blame] | 38 | #include "ir_print_visitor.h" |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 39 | |
Ian Romanick | 5bfe30a | 2010-04-07 16:44:30 -0700 | [diff] [blame] | 40 | const char * |
| 41 | _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target) |
| 42 | { |
| 43 | switch (target) { |
| 44 | case vertex_shader: return "vertex"; |
| 45 | case fragment_shader: return "fragment"; |
| 46 | case geometry_shader: return "geometry"; |
| 47 | } |
| 48 | |
| 49 | assert(!"Should not get here."); |
| 50 | } |
| 51 | |
| 52 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 53 | void |
Ian Romanick | 1f58518 | 2010-03-11 14:08:33 -0800 | [diff] [blame] | 54 | _mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state, |
| 55 | const char *fmt, ...) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 56 | { |
| 57 | char buf[1024]; |
| 58 | int len; |
| 59 | va_list ap; |
| 60 | |
Ian Romanick | 71d0bbf | 2010-03-23 13:21:19 -0700 | [diff] [blame] | 61 | state->error = true; |
Ian Romanick | 1f58518 | 2010-03-11 14:08:33 -0800 | [diff] [blame] | 62 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 63 | len = snprintf(buf, sizeof(buf), "%u:%u(%u): error: ", |
| 64 | locp->source, locp->first_line, locp->first_column); |
| 65 | |
| 66 | va_start(ap, fmt); |
| 67 | vsnprintf(buf + len, sizeof(buf) - len, fmt, ap); |
| 68 | va_end(ap); |
| 69 | |
| 70 | printf("%s\n", buf); |
| 71 | } |
| 72 | |
| 73 | |
Ian Romanick | 56b8b21 | 2010-04-07 14:47:46 -0700 | [diff] [blame] | 74 | void |
| 75 | _mesa_glsl_warning(const YYLTYPE *locp, const _mesa_glsl_parse_state *state, |
| 76 | const char *fmt, ...) |
| 77 | { |
| 78 | char buf[1024]; |
| 79 | int len; |
| 80 | va_list ap; |
| 81 | |
| 82 | len = snprintf(buf, sizeof(buf), "%u:%u(%u): warning: ", |
| 83 | locp->source, locp->first_line, locp->first_column); |
| 84 | |
| 85 | va_start(ap, fmt); |
| 86 | vsnprintf(buf + len, sizeof(buf) - len, fmt, ap); |
| 87 | va_end(ap); |
| 88 | |
| 89 | printf("%s\n", buf); |
| 90 | } |
| 91 | |
| 92 | |
Ian Romanick | e701761 | 2010-04-07 16:46:25 -0700 | [diff] [blame] | 93 | bool |
| 94 | _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp, |
| 95 | const char *behavior, YYLTYPE *behavior_locp, |
| 96 | _mesa_glsl_parse_state *state) |
| 97 | { |
| 98 | enum { |
| 99 | extension_disable, |
| 100 | extension_enable, |
| 101 | extension_require, |
| 102 | extension_warn |
| 103 | } ext_mode; |
Ian Romanick | e701761 | 2010-04-07 16:46:25 -0700 | [diff] [blame] | 104 | |
| 105 | if (strcmp(behavior, "warn") == 0) { |
| 106 | ext_mode = extension_warn; |
| 107 | } else if (strcmp(behavior, "require") == 0) { |
| 108 | ext_mode = extension_require; |
| 109 | } else if (strcmp(behavior, "enable") == 0) { |
| 110 | ext_mode = extension_enable; |
| 111 | } else if (strcmp(behavior, "disable") == 0) { |
| 112 | ext_mode = extension_disable; |
| 113 | } else { |
| 114 | _mesa_glsl_error(behavior_locp, state, |
| 115 | "Unknown extension behavior `%s'", |
| 116 | behavior); |
| 117 | return false; |
| 118 | } |
| 119 | |
Ian Romanick | 887a8b0 | 2010-04-07 16:57:56 -0700 | [diff] [blame^] | 120 | bool unsupported = false; |
| 121 | |
Ian Romanick | e701761 | 2010-04-07 16:46:25 -0700 | [diff] [blame] | 122 | if (strcmp(name, "all") == 0) { |
| 123 | if ((ext_mode == extension_enable) || (ext_mode == extension_require)) { |
| 124 | _mesa_glsl_error(name_locp, state, "Cannot %s all extensions", |
| 125 | (ext_mode == extension_enable) |
| 126 | ? "enable" : "require"); |
| 127 | return false; |
| 128 | } |
| 129 | } else { |
Ian Romanick | 887a8b0 | 2010-04-07 16:57:56 -0700 | [diff] [blame^] | 130 | unsupported = true; |
| 131 | } |
| 132 | |
| 133 | if (unsupported) { |
| 134 | static const char *const fmt = "extension `%s' unsupported in %s shader"; |
| 135 | |
Ian Romanick | e701761 | 2010-04-07 16:46:25 -0700 | [diff] [blame] | 136 | if (ext_mode == extension_require) { |
Ian Romanick | 887a8b0 | 2010-04-07 16:57:56 -0700 | [diff] [blame^] | 137 | _mesa_glsl_error(name_locp, state, fmt, |
| 138 | name, _mesa_glsl_shader_target_name(state->target)); |
Ian Romanick | e701761 | 2010-04-07 16:46:25 -0700 | [diff] [blame] | 139 | return false; |
Ian Romanick | 1799a0c | 2010-04-07 14:50:36 -0700 | [diff] [blame] | 140 | } else { |
Ian Romanick | 887a8b0 | 2010-04-07 16:57:56 -0700 | [diff] [blame^] | 141 | _mesa_glsl_warning(name_locp, state, fmt, |
| 142 | name, _mesa_glsl_shader_target_name(state->target)); |
Ian Romanick | e701761 | 2010-04-07 16:46:25 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 150 | ast_node::~ast_node() |
| 151 | { |
| 152 | /* empty */ |
| 153 | } |
| 154 | |
| 155 | |
| 156 | void |
| 157 | _mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q) |
| 158 | { |
| 159 | if (q->constant) |
| 160 | printf("const "); |
| 161 | |
| 162 | if (q->invariant) |
| 163 | printf("invariant "); |
| 164 | |
| 165 | if (q->attribute) |
| 166 | printf("attribute "); |
| 167 | |
| 168 | if (q->varying) |
| 169 | printf("varying "); |
| 170 | |
| 171 | if (q->in && q->out) |
| 172 | printf("inout "); |
| 173 | else { |
| 174 | if (q->in) |
| 175 | printf("in "); |
| 176 | |
| 177 | if (q->out) |
| 178 | printf("out "); |
| 179 | } |
| 180 | |
| 181 | if (q->centroid) |
| 182 | printf("centroid "); |
| 183 | if (q->uniform) |
| 184 | printf("uniform "); |
| 185 | if (q->smooth) |
| 186 | printf("smooth "); |
| 187 | if (q->flat) |
| 188 | printf("flat "); |
| 189 | if (q->noperspective) |
| 190 | printf("noperspective "); |
| 191 | } |
| 192 | |
| 193 | |
| 194 | void |
| 195 | ast_node::print(void) const |
| 196 | { |
Ian Romanick | 03d3f3a | 2010-04-02 11:03:47 -0700 | [diff] [blame] | 197 | printf("unhandled node "); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | |
| 201 | ast_node::ast_node(void) |
| 202 | { |
Ian Romanick | 53d2774 | 2010-02-22 13:22:10 -0800 | [diff] [blame] | 203 | make_empty_list(this); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 206 | |
| 207 | static void |
| 208 | ast_opt_array_size_print(bool is_array, const ast_expression *array_size) |
| 209 | { |
| 210 | if (is_array) { |
| 211 | printf("[ "); |
| 212 | |
| 213 | if (array_size) |
| 214 | array_size->print(); |
| 215 | |
| 216 | printf("] "); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 221 | void |
| 222 | ast_compound_statement::print(void) const |
| 223 | { |
| 224 | const struct simple_node *ptr; |
| 225 | |
| 226 | printf("{\n"); |
| 227 | |
| 228 | foreach(ptr, & statements) { |
Ian Romanick | e41a1cd | 2010-02-25 12:49:55 -0800 | [diff] [blame] | 229 | ((ast_node *)ptr)->print(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | printf("}\n"); |
| 233 | } |
| 234 | |
| 235 | |
| 236 | ast_compound_statement::ast_compound_statement(int new_scope, |
| 237 | ast_node *statements) |
| 238 | { |
| 239 | this->new_scope = new_scope; |
| 240 | make_empty_list(& this->statements); |
| 241 | |
| 242 | if (statements != NULL) { |
| 243 | /* This seems odd, but it works. The simple_list is, |
| 244 | * basically, a circular list. insert_at_tail adds |
| 245 | * the specified node to the list before the current |
| 246 | * head. |
| 247 | */ |
| 248 | insert_at_tail((struct simple_node *) statements, |
| 249 | & this->statements); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | |
| 254 | void |
| 255 | ast_expression::print(void) const |
| 256 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 257 | switch (oper) { |
| 258 | case ast_assign: |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 259 | case ast_mul_assign: |
| 260 | case ast_div_assign: |
| 261 | case ast_mod_assign: |
| 262 | case ast_add_assign: |
| 263 | case ast_sub_assign: |
| 264 | case ast_ls_assign: |
| 265 | case ast_rs_assign: |
| 266 | case ast_and_assign: |
| 267 | case ast_xor_assign: |
| 268 | case ast_or_assign: |
| 269 | subexpressions[0]->print(); |
Ian Romanick | 88349b2 | 2010-02-22 19:10:25 -0800 | [diff] [blame] | 270 | printf("%s ", operator_string(oper)); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 271 | subexpressions[1]->print(); |
| 272 | break; |
| 273 | |
| 274 | case ast_field_selection: |
| 275 | subexpressions[0]->print(); |
| 276 | printf(". %s ", primary_expression.identifier); |
| 277 | break; |
| 278 | |
| 279 | case ast_plus: |
| 280 | case ast_neg: |
| 281 | case ast_bit_not: |
| 282 | case ast_logic_not: |
| 283 | case ast_pre_inc: |
| 284 | case ast_pre_dec: |
Ian Romanick | 88349b2 | 2010-02-22 19:10:25 -0800 | [diff] [blame] | 285 | printf("%s ", operator_string(oper)); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 286 | subexpressions[0]->print(); |
| 287 | break; |
| 288 | |
| 289 | case ast_post_inc: |
| 290 | case ast_post_dec: |
| 291 | subexpressions[0]->print(); |
Ian Romanick | 88349b2 | 2010-02-22 19:10:25 -0800 | [diff] [blame] | 292 | printf("%s ", operator_string(oper)); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 293 | break; |
| 294 | |
| 295 | case ast_conditional: |
| 296 | subexpressions[0]->print(); |
| 297 | printf("? "); |
| 298 | subexpressions[1]->print(); |
| 299 | printf(": "); |
| 300 | subexpressions[1]->print(); |
| 301 | break; |
| 302 | |
| 303 | case ast_array_index: |
| 304 | subexpressions[0]->print(); |
| 305 | printf("[ "); |
| 306 | subexpressions[1]->print(); |
| 307 | printf("] "); |
| 308 | break; |
| 309 | |
| 310 | case ast_function_call: { |
| 311 | ast_expression *parameters = subexpressions[1]; |
| 312 | |
| 313 | subexpressions[0]->print(); |
| 314 | printf("( "); |
| 315 | |
| 316 | if (parameters != NULL) { |
| 317 | struct simple_node *ptr; |
| 318 | |
| 319 | parameters->print(); |
| 320 | foreach (ptr, (struct simple_node *) parameters) { |
| 321 | printf(", "); |
Ian Romanick | e41a1cd | 2010-02-25 12:49:55 -0800 | [diff] [blame] | 322 | ((ast_node *)ptr)->print(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
| 326 | printf(") "); |
| 327 | break; |
| 328 | } |
| 329 | |
| 330 | case ast_identifier: |
| 331 | printf("%s ", primary_expression.identifier); |
| 332 | break; |
| 333 | |
| 334 | case ast_int_constant: |
| 335 | printf("%d ", primary_expression.int_constant); |
| 336 | break; |
| 337 | |
| 338 | case ast_uint_constant: |
| 339 | printf("%u ", primary_expression.uint_constant); |
| 340 | break; |
| 341 | |
| 342 | case ast_float_constant: |
| 343 | printf("%f ", primary_expression.float_constant); |
| 344 | break; |
| 345 | |
| 346 | case ast_bool_constant: |
| 347 | printf("%s ", |
| 348 | primary_expression.bool_constant |
| 349 | ? "true" : "false"); |
| 350 | break; |
| 351 | |
| 352 | case ast_sequence: { |
| 353 | struct simple_node *ptr; |
| 354 | struct simple_node *const head = first_elem(& expressions); |
| 355 | |
| 356 | printf("( "); |
| 357 | foreach (ptr, & expressions) { |
| 358 | if (ptr != head) |
| 359 | printf(", "); |
| 360 | |
Ian Romanick | e41a1cd | 2010-02-25 12:49:55 -0800 | [diff] [blame] | 361 | ((ast_node *)ptr)->print(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 362 | } |
| 363 | printf(") "); |
| 364 | break; |
| 365 | } |
Ian Romanick | 88349b2 | 2010-02-22 19:10:25 -0800 | [diff] [blame] | 366 | |
| 367 | default: |
| 368 | assert(0); |
| 369 | break; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
| 373 | ast_expression::ast_expression(int oper, |
| 374 | ast_expression *ex0, |
| 375 | ast_expression *ex1, |
| 376 | ast_expression *ex2) |
| 377 | { |
| 378 | this->oper = ast_operators(oper); |
| 379 | this->subexpressions[0] = ex0; |
| 380 | this->subexpressions[1] = ex1; |
| 381 | this->subexpressions[2] = ex2; |
| 382 | make_empty_list(& expressions); |
| 383 | } |
| 384 | |
| 385 | |
| 386 | void |
| 387 | ast_expression_statement::print(void) const |
| 388 | { |
| 389 | if (expression) |
| 390 | expression->print(); |
| 391 | |
| 392 | printf("; "); |
| 393 | } |
| 394 | |
| 395 | |
| 396 | ast_expression_statement::ast_expression_statement(ast_expression *ex) : |
| 397 | expression(ex) |
| 398 | { |
| 399 | /* empty */ |
| 400 | } |
| 401 | |
| 402 | |
| 403 | void |
| 404 | ast_function::print(void) const |
| 405 | { |
| 406 | struct simple_node *ptr; |
| 407 | |
| 408 | return_type->print(); |
| 409 | printf(" %s (", identifier); |
| 410 | |
| 411 | foreach(ptr, & parameters) { |
Ian Romanick | e41a1cd | 2010-02-25 12:49:55 -0800 | [diff] [blame] | 412 | ((ast_node *)ptr)->print(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | printf(")"); |
| 416 | } |
| 417 | |
| 418 | |
| 419 | ast_function::ast_function(void) |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 420 | : is_definition(false), signature(NULL) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 421 | { |
| 422 | make_empty_list(& parameters); |
| 423 | } |
| 424 | |
| 425 | |
| 426 | void |
| 427 | ast_fully_specified_type::print(void) const |
| 428 | { |
| 429 | _mesa_ast_type_qualifier_print(& qualifier); |
| 430 | specifier->print(); |
| 431 | } |
| 432 | |
| 433 | |
| 434 | void |
| 435 | ast_parameter_declarator::print(void) const |
| 436 | { |
| 437 | type->print(); |
| 438 | if (identifier) |
| 439 | printf("%s ", identifier); |
| 440 | ast_opt_array_size_print(is_array, array_size); |
| 441 | } |
| 442 | |
| 443 | |
| 444 | void |
| 445 | ast_function_definition::print(void) const |
| 446 | { |
| 447 | prototype->print(); |
| 448 | body->print(); |
| 449 | } |
| 450 | |
| 451 | |
| 452 | void |
| 453 | ast_declaration::print(void) const |
| 454 | { |
| 455 | printf("%s ", identifier); |
| 456 | ast_opt_array_size_print(is_array, array_size); |
| 457 | |
| 458 | if (initializer) { |
| 459 | printf("= "); |
| 460 | initializer->print(); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | |
| 465 | ast_declaration::ast_declaration(char *identifier, int is_array, |
| 466 | ast_expression *array_size, |
| 467 | ast_expression *initializer) |
| 468 | { |
| 469 | this->identifier = identifier; |
| 470 | this->is_array = is_array; |
| 471 | this->array_size = array_size; |
| 472 | this->initializer = initializer; |
| 473 | } |
| 474 | |
| 475 | |
| 476 | void |
| 477 | ast_declarator_list::print(void) const |
| 478 | { |
| 479 | struct simple_node *head; |
| 480 | struct simple_node *ptr; |
| 481 | |
| 482 | assert(type || invariant); |
| 483 | |
| 484 | if (type) |
| 485 | type->print(); |
| 486 | else |
| 487 | printf("invariant "); |
| 488 | |
| 489 | head = first_elem(& declarations); |
| 490 | foreach (ptr, & declarations) { |
| 491 | if (ptr != head) |
| 492 | printf(", "); |
| 493 | |
Ian Romanick | e41a1cd | 2010-02-25 12:49:55 -0800 | [diff] [blame] | 494 | ((ast_node *)ptr)->print(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | printf("; "); |
| 498 | } |
| 499 | |
| 500 | |
| 501 | ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type) |
| 502 | { |
| 503 | this->type = type; |
| 504 | make_empty_list(& this->declarations); |
| 505 | } |
| 506 | |
| 507 | void |
| 508 | ast_jump_statement::print(void) const |
| 509 | { |
| 510 | switch (mode) { |
| 511 | case ast_continue: |
| 512 | printf("continue; "); |
| 513 | break; |
| 514 | case ast_break: |
| 515 | printf("break; "); |
| 516 | break; |
| 517 | case ast_return: |
| 518 | printf("return "); |
| 519 | if (opt_return_value) |
| 520 | opt_return_value->print(); |
| 521 | |
| 522 | printf("; "); |
| 523 | break; |
| 524 | case ast_discard: |
| 525 | printf("discard; "); |
| 526 | break; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | |
| 531 | ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value) |
| 532 | { |
| 533 | this->mode = ast_jump_modes(mode); |
| 534 | |
| 535 | if (mode == ast_return) |
| 536 | opt_return_value = return_value; |
| 537 | } |
| 538 | |
| 539 | |
| 540 | void |
| 541 | ast_selection_statement::print(void) const |
| 542 | { |
| 543 | printf("if ( "); |
| 544 | condition->print(); |
| 545 | printf(") "); |
| 546 | |
| 547 | then_statement->print(); |
| 548 | |
| 549 | if (else_statement) { |
| 550 | printf("else "); |
| 551 | else_statement->print(); |
| 552 | } |
| 553 | |
| 554 | } |
| 555 | |
| 556 | |
| 557 | ast_selection_statement::ast_selection_statement(ast_expression *condition, |
| 558 | ast_node *then_statement, |
| 559 | ast_node *else_statement) |
| 560 | { |
| 561 | this->condition = condition; |
| 562 | this->then_statement = then_statement; |
| 563 | this->else_statement = else_statement; |
| 564 | } |
| 565 | |
| 566 | |
| 567 | void |
| 568 | ast_iteration_statement::print(void) const |
| 569 | { |
| 570 | switch (mode) { |
| 571 | case ast_for: |
| 572 | printf("for( "); |
| 573 | if (init_statement) |
| 574 | init_statement->print(); |
| 575 | printf("; "); |
| 576 | |
| 577 | if (condition) |
| 578 | condition->print(); |
| 579 | printf("; "); |
| 580 | |
| 581 | if (rest_expression) |
| 582 | rest_expression->print(); |
| 583 | printf(") "); |
| 584 | |
| 585 | body->print(); |
| 586 | break; |
| 587 | |
| 588 | case ast_while: |
| 589 | printf("while ( "); |
| 590 | if (condition) |
| 591 | condition->print(); |
| 592 | printf(") "); |
| 593 | body->print(); |
| 594 | break; |
| 595 | |
| 596 | case ast_do_while: |
| 597 | printf("do "); |
| 598 | body->print(); |
| 599 | printf("while ( "); |
| 600 | if (condition) |
| 601 | condition->print(); |
| 602 | printf("); "); |
| 603 | break; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | |
| 608 | ast_iteration_statement::ast_iteration_statement(int mode, |
| 609 | ast_node *init, |
| 610 | ast_node *condition, |
| 611 | ast_expression *rest_expression, |
| 612 | ast_node *body) |
| 613 | { |
| 614 | this->mode = ast_iteration_modes(mode); |
| 615 | this->init_statement = init; |
| 616 | this->condition = condition; |
| 617 | this->rest_expression = rest_expression; |
| 618 | this->body = body; |
| 619 | } |
| 620 | |
| 621 | |
| 622 | void |
| 623 | ast_struct_specifier::print(void) const |
| 624 | { |
| 625 | struct simple_node *ptr; |
| 626 | |
| 627 | printf("struct %s { ", name); |
| 628 | foreach (ptr, & declarations) { |
Ian Romanick | e41a1cd | 2010-02-25 12:49:55 -0800 | [diff] [blame] | 629 | ((ast_node *)ptr)->print(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 630 | } |
| 631 | printf("} "); |
| 632 | } |
| 633 | |
| 634 | |
| 635 | ast_struct_specifier::ast_struct_specifier(char *identifier, |
| 636 | ast_node *declarator_list) |
| 637 | { |
| 638 | name = identifier; |
| 639 | |
| 640 | /* This seems odd, but it works. The simple_list is, |
| 641 | * basically, a circular list. insert_at_tail adds |
| 642 | * the specified node to the list before the current |
| 643 | * head. |
| 644 | */ |
| 645 | insert_at_tail((struct simple_node *) declarator_list, |
| 646 | & declarations); |
| 647 | } |
| 648 | |
| 649 | |
| 650 | static char * |
| 651 | load_text_file(const char *file_name, size_t *size) |
| 652 | { |
| 653 | char *text = NULL; |
| 654 | struct stat st; |
| 655 | ssize_t total_read = 0; |
| 656 | int fd = open(file_name, O_RDONLY); |
| 657 | |
| 658 | *size = 0; |
| 659 | if (fd < 0) { |
| 660 | return NULL; |
| 661 | } |
| 662 | |
| 663 | if (fstat(fd, & st) == 0) { |
| 664 | text = (char *) malloc(st.st_size + 1); |
| 665 | if (text != NULL) { |
| 666 | do { |
| 667 | ssize_t bytes = read(fd, text + total_read, |
| 668 | st.st_size - total_read); |
| 669 | if (bytes < 0) { |
| 670 | free(text); |
| 671 | text = NULL; |
| 672 | break; |
| 673 | } |
| 674 | |
| 675 | if (bytes == 0) { |
| 676 | break; |
| 677 | } |
| 678 | |
| 679 | total_read += bytes; |
| 680 | } while (total_read < st.st_size); |
| 681 | |
| 682 | text[total_read] = '\0'; |
| 683 | *size = total_read; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | close(fd); |
| 688 | |
| 689 | return text; |
| 690 | } |
| 691 | |
| 692 | |
| 693 | int |
| 694 | main(int argc, char **argv) |
| 695 | { |
| 696 | struct _mesa_glsl_parse_state state; |
| 697 | char *shader; |
| 698 | size_t shader_len; |
| 699 | struct simple_node *ptr; |
Ian Romanick | 0044e7e | 2010-03-08 23:44:00 -0800 | [diff] [blame] | 700 | exec_list instructions; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 701 | |
Ian Romanick | 8e6cd3b | 2010-03-10 09:31:30 -0800 | [diff] [blame] | 702 | if (argc < 3) { |
| 703 | printf("Usage: %s [v|g|f] <shader_file>\n", argv[0]); |
| 704 | return EXIT_FAILURE; |
| 705 | } |
| 706 | |
| 707 | switch (argv[1][0]) { |
| 708 | case 'v': |
| 709 | state.target = vertex_shader; |
| 710 | break; |
| 711 | case 'g': |
| 712 | state.target = geometry_shader; |
| 713 | break; |
| 714 | case 'f': |
| 715 | state.target = fragment_shader; |
| 716 | break; |
| 717 | default: |
| 718 | printf("Usage: %s [v|g|f] <shader_file>\n", argv[0]); |
| 719 | return EXIT_FAILURE; |
| 720 | } |
| 721 | |
| 722 | shader = load_text_file(argv[2], & shader_len); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 723 | |
| 724 | state.scanner = NULL; |
| 725 | make_empty_list(& state.translation_unit); |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 726 | state.symbols = new glsl_symbol_table; |
Ian Romanick | 1f58518 | 2010-03-11 14:08:33 -0800 | [diff] [blame] | 727 | state.error = false; |
Ian Romanick | 5185a5f | 2010-03-29 15:20:42 -0700 | [diff] [blame] | 728 | state.temp_index = 0; |
Ian Romanick | e9d0f26 | 2010-04-05 17:01:53 -0700 | [diff] [blame] | 729 | state.loop_or_switch_nesting = NULL; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 730 | |
| 731 | _mesa_glsl_lexer_ctor(& state, shader, shader_len); |
| 732 | _mesa_glsl_parse(& state); |
| 733 | _mesa_glsl_lexer_dtor(& state); |
| 734 | |
| 735 | foreach (ptr, & state.translation_unit) { |
Ian Romanick | e41a1cd | 2010-02-25 12:49:55 -0800 | [diff] [blame] | 736 | ((ast_node *)ptr)->print(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 737 | } |
| 738 | |
Ian Romanick | d949a9a | 2010-03-10 09:55:22 -0800 | [diff] [blame] | 739 | _mesa_ast_to_hir(&instructions, &state); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 740 | |
Eric Anholt | 6273569 | 2010-04-05 15:24:28 -0700 | [diff] [blame] | 741 | /* Optimization passes */ |
| 742 | if (!state.error) { |
| 743 | /* Constant folding */ |
Eric Anholt | 70b7492 | 2010-04-06 11:52:09 -0700 | [diff] [blame] | 744 | ir_constant_folding_visitor constant_folding; |
| 745 | visit_exec_list(&instructions, &constant_folding); |
Eric Anholt | 6273569 | 2010-04-05 15:24:28 -0700 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | /* Print out the resulting IR */ |
Ian Romanick | 1c4156f | 2010-03-10 09:27:03 -0800 | [diff] [blame] | 749 | printf("\n\n"); |
Ian Romanick | 1c4156f | 2010-03-10 09:27:03 -0800 | [diff] [blame] | 750 | |
Ian Romanick | 1f58518 | 2010-03-11 14:08:33 -0800 | [diff] [blame] | 751 | if (!state.error) { |
| 752 | foreach_iter(exec_list_iterator, iter, instructions) { |
| 753 | ir_print_visitor v; |
| 754 | |
| 755 | ((ir_instruction *)iter.get())->accept(& v); |
Ian Romanick | d146427 | 2010-03-25 18:29:25 -0700 | [diff] [blame] | 756 | printf("\n"); |
Ian Romanick | 1f58518 | 2010-03-11 14:08:33 -0800 | [diff] [blame] | 757 | } |
Ian Romanick | 1c4156f | 2010-03-10 09:27:03 -0800 | [diff] [blame] | 758 | } |
| 759 | |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 760 | delete state.symbols; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 761 | |
Eric Anholt | 7c15bb2 | 2010-03-25 14:37:25 -0700 | [diff] [blame] | 762 | return state.error != 0; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 763 | } |