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