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