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