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> |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 25 | #include <string.h> |
| 26 | #include <assert.h> |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 27 | |
Kenneth Graunke | b2ba6fa | 2010-06-17 15:15:35 -0700 | [diff] [blame] | 28 | extern "C" { |
| 29 | #include <talloc.h> |
Chia-I Wu | bfd7c9a | 2010-08-23 17:51:42 +0800 | [diff] [blame] | 30 | #include "main/core.h" /* for struct __GLcontextRec */ |
Kenneth Graunke | b2ba6fa | 2010-06-17 15:15:35 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 33 | #include "ast.h" |
| 34 | #include "glsl_parser_extras.h" |
Ian Romanick | d59673c | 2010-02-25 17:17:23 -0800 | [diff] [blame] | 35 | #include "glsl_parser.h" |
Eric Anholt | 2f4fe15 | 2010-08-10 13:06:49 -0700 | [diff] [blame] | 36 | #include "ir_optimization.h" |
Ian Romanick | 8df2dbf | 2010-08-26 16:45:22 -0700 | [diff] [blame] | 37 | #include "loop_analysis.h" |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 38 | |
Ian Romanick | 2462a53 | 2010-07-18 15:59:43 -0700 | [diff] [blame] | 39 | _mesa_glsl_parse_state::_mesa_glsl_parse_state(struct __GLcontextRec *ctx, |
| 40 | GLenum target, void *mem_ctx) |
| 41 | { |
| 42 | switch (target) { |
| 43 | case GL_VERTEX_SHADER: this->target = vertex_shader; break; |
| 44 | case GL_FRAGMENT_SHADER: this->target = fragment_shader; break; |
| 45 | case GL_GEOMETRY_SHADER: this->target = geometry_shader; break; |
| 46 | } |
| 47 | |
| 48 | this->scanner = NULL; |
| 49 | this->translation_unit.make_empty(); |
| 50 | this->symbols = new(mem_ctx) glsl_symbol_table; |
| 51 | this->info_log = talloc_strdup(mem_ctx, ""); |
| 52 | this->error = false; |
| 53 | this->loop_or_switch_nesting = NULL; |
| 54 | this->ARB_texture_rectangle_enable = true; |
| 55 | |
| 56 | if (ctx != NULL) { |
| 57 | this->extensions = &ctx->Extensions; |
| 58 | |
| 59 | this->Const.MaxLights = ctx->Const.MaxLights; |
| 60 | this->Const.MaxClipPlanes = ctx->Const.MaxClipPlanes; |
| 61 | this->Const.MaxTextureUnits = ctx->Const.MaxTextureUnits; |
| 62 | this->Const.MaxTextureCoords = ctx->Const.MaxTextureCoordUnits; |
| 63 | this->Const.MaxVertexAttribs = ctx->Const.VertexProgram.MaxAttribs; |
| 64 | this->Const.MaxVertexUniformComponents = ctx->Const.VertexProgram.MaxUniformComponents; |
| 65 | this->Const.MaxVaryingFloats = ctx->Const.MaxVarying * 4; |
| 66 | this->Const.MaxVertexTextureImageUnits = ctx->Const.MaxVertexTextureImageUnits; |
| 67 | this->Const.MaxCombinedTextureImageUnits = ctx->Const.MaxCombinedTextureImageUnits; |
| 68 | this->Const.MaxTextureImageUnits = ctx->Const.MaxTextureImageUnits; |
| 69 | this->Const.MaxFragmentUniformComponents = ctx->Const.FragmentProgram.MaxUniformComponents; |
| 70 | |
| 71 | this->Const.MaxDrawBuffers = ctx->Const.MaxDrawBuffers; |
| 72 | } else { |
Kenneth Graunke | aa9f86a | 2010-07-22 16:20:36 -0700 | [diff] [blame] | 73 | /* If there is no GL context (standalone compiler), fill in constants |
| 74 | * with the minimum required values. |
| 75 | */ |
Ian Romanick | 2462a53 | 2010-07-18 15:59:43 -0700 | [diff] [blame] | 76 | static struct gl_extensions null_extensions; |
| 77 | |
| 78 | memset(&null_extensions, 0, sizeof(null_extensions)); |
Ian Romanick | 4f397e1 | 2010-08-05 17:50:13 -0700 | [diff] [blame] | 79 | null_extensions.ARB_draw_buffers = GL_TRUE; |
| 80 | null_extensions.ARB_fragment_coord_conventions = GL_TRUE; |
| 81 | null_extensions.EXT_texture_array = GL_TRUE; |
| 82 | null_extensions.NV_texture_rectangle = GL_TRUE; |
| 83 | |
Ian Romanick | 2462a53 | 2010-07-18 15:59:43 -0700 | [diff] [blame] | 84 | this->extensions = &null_extensions; |
Kenneth Graunke | aa9f86a | 2010-07-22 16:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | /* 1.10 minimums. */ |
| 87 | this->Const.MaxLights = 8; |
| 88 | this->Const.MaxClipPlanes = 8; |
| 89 | this->Const.MaxTextureUnits = 2; |
| 90 | |
| 91 | /* More than the 1.10 minimum to appease parser tests taken from |
| 92 | * apps that (hopefully) already checked the number of coords. |
| 93 | */ |
| 94 | this->Const.MaxTextureCoords = 4; |
| 95 | |
| 96 | this->Const.MaxVertexAttribs = 16; |
| 97 | this->Const.MaxVertexUniformComponents = 512; |
| 98 | this->Const.MaxVaryingFloats = 32; |
| 99 | this->Const.MaxVertexTextureImageUnits = 0; |
| 100 | this->Const.MaxCombinedTextureImageUnits = 2; |
| 101 | this->Const.MaxTextureImageUnits = 2; |
| 102 | this->Const.MaxFragmentUniformComponents = 64; |
| 103 | |
| 104 | this->Const.MaxDrawBuffers = 2; |
Ian Romanick | 2462a53 | 2010-07-18 15:59:43 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
Ian Romanick | 5bfe30a | 2010-04-07 16:44:30 -0700 | [diff] [blame] | 108 | const char * |
| 109 | _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target) |
| 110 | { |
| 111 | switch (target) { |
| 112 | case vertex_shader: return "vertex"; |
| 113 | case fragment_shader: return "fragment"; |
| 114 | case geometry_shader: return "geometry"; |
Eric Anholt | 81f49a7 | 2010-04-29 17:57:28 -0700 | [diff] [blame] | 115 | case ir_shader: break; |
Ian Romanick | 5bfe30a | 2010-04-07 16:44:30 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | assert(!"Should not get here."); |
Eric Anholt | 87a2ee8 | 2010-07-18 17:47:15 -0700 | [diff] [blame] | 119 | return "unknown"; |
Ian Romanick | 5bfe30a | 2010-04-07 16:44:30 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 123 | void |
Ian Romanick | 1f58518 | 2010-03-11 14:08:33 -0800 | [diff] [blame] | 124 | _mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state, |
| 125 | const char *fmt, ...) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 126 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 127 | va_list ap; |
| 128 | |
Ian Romanick | 71d0bbf | 2010-03-23 13:21:19 -0700 | [diff] [blame] | 129 | state->error = true; |
Ian Romanick | 1f58518 | 2010-03-11 14:08:33 -0800 | [diff] [blame] | 130 | |
Kenneth Graunke | b2ba6fa | 2010-06-17 15:15:35 -0700 | [diff] [blame] | 131 | assert(state->info_log != NULL); |
| 132 | state->info_log = talloc_asprintf_append(state->info_log, |
| 133 | "%u:%u(%u): error: ", |
| 134 | locp->source, |
| 135 | locp->first_line, |
| 136 | locp->first_column); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 137 | va_start(ap, fmt); |
Kenneth Graunke | b2ba6fa | 2010-06-17 15:15:35 -0700 | [diff] [blame] | 138 | state->info_log = talloc_vasprintf_append(state->info_log, fmt, ap); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 139 | va_end(ap); |
Kenneth Graunke | b2ba6fa | 2010-06-17 15:15:35 -0700 | [diff] [blame] | 140 | state->info_log = talloc_strdup_append(state->info_log, "\n"); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | |
Ian Romanick | 56b8b21 | 2010-04-07 14:47:46 -0700 | [diff] [blame] | 144 | void |
Eric Anholt | 3623df6 | 2010-04-29 18:00:33 -0700 | [diff] [blame] | 145 | _mesa_glsl_warning(const YYLTYPE *locp, _mesa_glsl_parse_state *state, |
Ian Romanick | 56b8b21 | 2010-04-07 14:47:46 -0700 | [diff] [blame] | 146 | const char *fmt, ...) |
| 147 | { |
Ian Romanick | 56b8b21 | 2010-04-07 14:47:46 -0700 | [diff] [blame] | 148 | va_list ap; |
| 149 | |
Kenneth Graunke | b2ba6fa | 2010-06-17 15:15:35 -0700 | [diff] [blame] | 150 | assert(state->info_log != NULL); |
| 151 | state->info_log = talloc_asprintf_append(state->info_log, |
| 152 | "%u:%u(%u): warning: ", |
| 153 | locp->source, |
| 154 | locp->first_line, |
| 155 | locp->first_column); |
Ian Romanick | 56b8b21 | 2010-04-07 14:47:46 -0700 | [diff] [blame] | 156 | va_start(ap, fmt); |
Kenneth Graunke | b2ba6fa | 2010-06-17 15:15:35 -0700 | [diff] [blame] | 157 | state->info_log = talloc_vasprintf_append(state->info_log, fmt, ap); |
Ian Romanick | 56b8b21 | 2010-04-07 14:47:46 -0700 | [diff] [blame] | 158 | va_end(ap); |
Kenneth Graunke | b2ba6fa | 2010-06-17 15:15:35 -0700 | [diff] [blame] | 159 | state->info_log = talloc_strdup_append(state->info_log, "\n"); |
Ian Romanick | 56b8b21 | 2010-04-07 14:47:46 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | |
Ian Romanick | e701761 | 2010-04-07 16:46:25 -0700 | [diff] [blame] | 163 | bool |
| 164 | _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp, |
| 165 | const char *behavior, YYLTYPE *behavior_locp, |
| 166 | _mesa_glsl_parse_state *state) |
| 167 | { |
| 168 | enum { |
| 169 | extension_disable, |
| 170 | extension_enable, |
| 171 | extension_require, |
| 172 | extension_warn |
| 173 | } ext_mode; |
Ian Romanick | e701761 | 2010-04-07 16:46:25 -0700 | [diff] [blame] | 174 | |
| 175 | if (strcmp(behavior, "warn") == 0) { |
| 176 | ext_mode = extension_warn; |
| 177 | } else if (strcmp(behavior, "require") == 0) { |
| 178 | ext_mode = extension_require; |
| 179 | } else if (strcmp(behavior, "enable") == 0) { |
| 180 | ext_mode = extension_enable; |
| 181 | } else if (strcmp(behavior, "disable") == 0) { |
| 182 | ext_mode = extension_disable; |
| 183 | } else { |
| 184 | _mesa_glsl_error(behavior_locp, state, |
| 185 | "Unknown extension behavior `%s'", |
| 186 | behavior); |
| 187 | return false; |
| 188 | } |
| 189 | |
Ian Romanick | 887a8b0 | 2010-04-07 16:57:56 -0700 | [diff] [blame] | 190 | bool unsupported = false; |
| 191 | |
Ian Romanick | e701761 | 2010-04-07 16:46:25 -0700 | [diff] [blame] | 192 | if (strcmp(name, "all") == 0) { |
| 193 | if ((ext_mode == extension_enable) || (ext_mode == extension_require)) { |
| 194 | _mesa_glsl_error(name_locp, state, "Cannot %s all extensions", |
| 195 | (ext_mode == extension_enable) |
| 196 | ? "enable" : "require"); |
| 197 | return false; |
| 198 | } |
Ian Romanick | eb56cea | 2010-04-23 13:32:23 -0700 | [diff] [blame] | 199 | } else if (strcmp(name, "GL_ARB_draw_buffers") == 0) { |
Ian Romanick | c77b257 | 2010-04-07 16:59:46 -0700 | [diff] [blame] | 200 | /* This extension is only supported in fragment shaders. |
| 201 | */ |
| 202 | if (state->target != fragment_shader) { |
| 203 | unsupported = true; |
| 204 | } else { |
| 205 | state->ARB_draw_buffers_enable = (ext_mode != extension_disable); |
| 206 | state->ARB_draw_buffers_warn = (ext_mode == extension_warn); |
| 207 | } |
Ian Romanick | f50f065 | 2010-06-30 17:30:03 -0700 | [diff] [blame] | 208 | } else if (strcmp(name, "GL_ARB_fragment_coord_conventions") == 0) { |
| 209 | state->ARB_fragment_coord_conventions_enable = |
| 210 | (ext_mode != extension_disable); |
| 211 | state->ARB_fragment_coord_conventions_warn = |
| 212 | (ext_mode == extension_warn); |
| 213 | |
| 214 | unsupported = !state->extensions->ARB_fragment_coord_conventions; |
Ian Romanick | eb56cea | 2010-04-23 13:32:23 -0700 | [diff] [blame] | 215 | } else if (strcmp(name, "GL_ARB_texture_rectangle") == 0) { |
Ian Romanick | 0c82465 | 2010-04-07 17:13:44 -0700 | [diff] [blame] | 216 | state->ARB_texture_rectangle_enable = (ext_mode != extension_disable); |
| 217 | state->ARB_texture_rectangle_warn = (ext_mode == extension_warn); |
Ian Romanick | 667f4e1 | 2010-06-30 16:42:07 -0700 | [diff] [blame] | 218 | } else if (strcmp(name, "GL_EXT_texture_array") == 0) { |
| 219 | state->EXT_texture_array_enable = (ext_mode != extension_disable); |
| 220 | state->EXT_texture_array_warn = (ext_mode == extension_warn); |
| 221 | |
| 222 | unsupported = !state->extensions->EXT_texture_array; |
Ian Romanick | e701761 | 2010-04-07 16:46:25 -0700 | [diff] [blame] | 223 | } else { |
Ian Romanick | 887a8b0 | 2010-04-07 16:57:56 -0700 | [diff] [blame] | 224 | unsupported = true; |
| 225 | } |
| 226 | |
| 227 | if (unsupported) { |
| 228 | static const char *const fmt = "extension `%s' unsupported in %s shader"; |
| 229 | |
Ian Romanick | e701761 | 2010-04-07 16:46:25 -0700 | [diff] [blame] | 230 | if (ext_mode == extension_require) { |
Ian Romanick | 887a8b0 | 2010-04-07 16:57:56 -0700 | [diff] [blame] | 231 | _mesa_glsl_error(name_locp, state, fmt, |
| 232 | name, _mesa_glsl_shader_target_name(state->target)); |
Ian Romanick | e701761 | 2010-04-07 16:46:25 -0700 | [diff] [blame] | 233 | return false; |
Ian Romanick | 1799a0c | 2010-04-07 14:50:36 -0700 | [diff] [blame] | 234 | } else { |
Ian Romanick | 887a8b0 | 2010-04-07 16:57:56 -0700 | [diff] [blame] | 235 | _mesa_glsl_warning(name_locp, state, fmt, |
| 236 | name, _mesa_glsl_shader_target_name(state->target)); |
Ian Romanick | e701761 | 2010-04-07 16:46:25 -0700 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
| 240 | return true; |
| 241 | } |
| 242 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 243 | void |
| 244 | _mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q) |
| 245 | { |
| 246 | if (q->constant) |
| 247 | printf("const "); |
| 248 | |
| 249 | if (q->invariant) |
| 250 | printf("invariant "); |
| 251 | |
| 252 | if (q->attribute) |
| 253 | printf("attribute "); |
| 254 | |
| 255 | if (q->varying) |
| 256 | printf("varying "); |
| 257 | |
| 258 | if (q->in && q->out) |
| 259 | printf("inout "); |
| 260 | else { |
| 261 | if (q->in) |
| 262 | printf("in "); |
| 263 | |
| 264 | if (q->out) |
| 265 | printf("out "); |
| 266 | } |
| 267 | |
| 268 | if (q->centroid) |
| 269 | printf("centroid "); |
| 270 | if (q->uniform) |
| 271 | printf("uniform "); |
| 272 | if (q->smooth) |
| 273 | printf("smooth "); |
| 274 | if (q->flat) |
| 275 | printf("flat "); |
| 276 | if (q->noperspective) |
| 277 | printf("noperspective "); |
| 278 | } |
| 279 | |
| 280 | |
| 281 | void |
| 282 | ast_node::print(void) const |
| 283 | { |
Ian Romanick | 03d3f3a | 2010-04-02 11:03:47 -0700 | [diff] [blame] | 284 | printf("unhandled node "); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | |
| 288 | ast_node::ast_node(void) |
| 289 | { |
Carl Worth | ec9675e | 2010-07-29 16:39:36 -0700 | [diff] [blame] | 290 | this->location.source = 0; |
| 291 | this->location.line = 0; |
| 292 | this->location.column = 0; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 295 | |
| 296 | static void |
| 297 | ast_opt_array_size_print(bool is_array, const ast_expression *array_size) |
| 298 | { |
| 299 | if (is_array) { |
| 300 | printf("[ "); |
| 301 | |
| 302 | if (array_size) |
| 303 | array_size->print(); |
| 304 | |
| 305 | printf("] "); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 310 | void |
| 311 | ast_compound_statement::print(void) const |
| 312 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 313 | printf("{\n"); |
| 314 | |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 315 | foreach_list_const(n, &this->statements) { |
| 316 | ast_node *ast = exec_node_data(ast_node, n, link); |
| 317 | ast->print(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | printf("}\n"); |
| 321 | } |
| 322 | |
| 323 | |
| 324 | ast_compound_statement::ast_compound_statement(int new_scope, |
| 325 | ast_node *statements) |
| 326 | { |
| 327 | this->new_scope = new_scope; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 328 | |
| 329 | if (statements != NULL) { |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 330 | this->statements.push_degenerate_list_at_head(&statements->link); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
| 334 | |
| 335 | void |
| 336 | ast_expression::print(void) const |
| 337 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 338 | switch (oper) { |
| 339 | case ast_assign: |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 340 | case ast_mul_assign: |
| 341 | case ast_div_assign: |
| 342 | case ast_mod_assign: |
| 343 | case ast_add_assign: |
| 344 | case ast_sub_assign: |
| 345 | case ast_ls_assign: |
| 346 | case ast_rs_assign: |
| 347 | case ast_and_assign: |
| 348 | case ast_xor_assign: |
| 349 | case ast_or_assign: |
| 350 | subexpressions[0]->print(); |
Ian Romanick | 88349b2 | 2010-02-22 19:10:25 -0800 | [diff] [blame] | 351 | printf("%s ", operator_string(oper)); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 352 | subexpressions[1]->print(); |
| 353 | break; |
| 354 | |
| 355 | case ast_field_selection: |
| 356 | subexpressions[0]->print(); |
| 357 | printf(". %s ", primary_expression.identifier); |
| 358 | break; |
| 359 | |
| 360 | case ast_plus: |
| 361 | case ast_neg: |
| 362 | case ast_bit_not: |
| 363 | case ast_logic_not: |
| 364 | case ast_pre_inc: |
| 365 | case ast_pre_dec: |
Ian Romanick | 88349b2 | 2010-02-22 19:10:25 -0800 | [diff] [blame] | 366 | printf("%s ", operator_string(oper)); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 367 | subexpressions[0]->print(); |
| 368 | break; |
| 369 | |
| 370 | case ast_post_inc: |
| 371 | case ast_post_dec: |
| 372 | subexpressions[0]->print(); |
Ian Romanick | 88349b2 | 2010-02-22 19:10:25 -0800 | [diff] [blame] | 373 | printf("%s ", operator_string(oper)); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 374 | break; |
| 375 | |
| 376 | case ast_conditional: |
| 377 | subexpressions[0]->print(); |
| 378 | printf("? "); |
| 379 | subexpressions[1]->print(); |
| 380 | printf(": "); |
| 381 | subexpressions[1]->print(); |
| 382 | break; |
| 383 | |
| 384 | case ast_array_index: |
| 385 | subexpressions[0]->print(); |
| 386 | printf("[ "); |
| 387 | subexpressions[1]->print(); |
| 388 | printf("] "); |
| 389 | break; |
| 390 | |
| 391 | case ast_function_call: { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 392 | subexpressions[0]->print(); |
| 393 | printf("( "); |
| 394 | |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 395 | foreach_list_const (n, &this->expressions) { |
Ian Romanick | 2384937 | 2010-05-14 16:06:41 -0700 | [diff] [blame] | 396 | if (n != this->expressions.get_head()) |
| 397 | printf(", "); |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 398 | |
| 399 | ast_node *ast = exec_node_data(ast_node, n, link); |
| 400 | ast->print(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | printf(") "); |
| 404 | break; |
| 405 | } |
| 406 | |
| 407 | case ast_identifier: |
| 408 | printf("%s ", primary_expression.identifier); |
| 409 | break; |
| 410 | |
| 411 | case ast_int_constant: |
| 412 | printf("%d ", primary_expression.int_constant); |
| 413 | break; |
| 414 | |
| 415 | case ast_uint_constant: |
| 416 | printf("%u ", primary_expression.uint_constant); |
| 417 | break; |
| 418 | |
| 419 | case ast_float_constant: |
| 420 | printf("%f ", primary_expression.float_constant); |
| 421 | break; |
| 422 | |
| 423 | case ast_bool_constant: |
| 424 | printf("%s ", |
| 425 | primary_expression.bool_constant |
| 426 | ? "true" : "false"); |
| 427 | break; |
| 428 | |
| 429 | case ast_sequence: { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 430 | printf("( "); |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 431 | foreach_list_const(n, & this->expressions) { |
| 432 | if (n != this->expressions.get_head()) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 433 | printf(", "); |
| 434 | |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 435 | ast_node *ast = exec_node_data(ast_node, n, link); |
| 436 | ast->print(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 437 | } |
| 438 | printf(") "); |
| 439 | break; |
| 440 | } |
Ian Romanick | 88349b2 | 2010-02-22 19:10:25 -0800 | [diff] [blame] | 441 | |
| 442 | default: |
| 443 | assert(0); |
| 444 | break; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
| 448 | ast_expression::ast_expression(int oper, |
| 449 | ast_expression *ex0, |
| 450 | ast_expression *ex1, |
| 451 | ast_expression *ex2) |
| 452 | { |
| 453 | this->oper = ast_operators(oper); |
| 454 | this->subexpressions[0] = ex0; |
| 455 | this->subexpressions[1] = ex1; |
| 456 | this->subexpressions[2] = ex2; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | |
| 460 | void |
| 461 | ast_expression_statement::print(void) const |
| 462 | { |
| 463 | if (expression) |
| 464 | expression->print(); |
| 465 | |
| 466 | printf("; "); |
| 467 | } |
| 468 | |
| 469 | |
| 470 | ast_expression_statement::ast_expression_statement(ast_expression *ex) : |
| 471 | expression(ex) |
| 472 | { |
| 473 | /* empty */ |
| 474 | } |
| 475 | |
| 476 | |
| 477 | void |
| 478 | ast_function::print(void) const |
| 479 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 480 | return_type->print(); |
| 481 | printf(" %s (", identifier); |
| 482 | |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 483 | foreach_list_const(n, & this->parameters) { |
| 484 | ast_node *ast = exec_node_data(ast_node, n, link); |
| 485 | ast->print(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | printf(")"); |
| 489 | } |
| 490 | |
| 491 | |
| 492 | ast_function::ast_function(void) |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 493 | : is_definition(false), signature(NULL) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 494 | { |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 495 | /* empty */ |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | |
| 499 | void |
| 500 | ast_fully_specified_type::print(void) const |
| 501 | { |
| 502 | _mesa_ast_type_qualifier_print(& qualifier); |
| 503 | specifier->print(); |
| 504 | } |
| 505 | |
| 506 | |
| 507 | void |
| 508 | ast_parameter_declarator::print(void) const |
| 509 | { |
| 510 | type->print(); |
| 511 | if (identifier) |
| 512 | printf("%s ", identifier); |
| 513 | ast_opt_array_size_print(is_array, array_size); |
| 514 | } |
| 515 | |
| 516 | |
| 517 | void |
| 518 | ast_function_definition::print(void) const |
| 519 | { |
| 520 | prototype->print(); |
| 521 | body->print(); |
| 522 | } |
| 523 | |
| 524 | |
| 525 | void |
| 526 | ast_declaration::print(void) const |
| 527 | { |
| 528 | printf("%s ", identifier); |
| 529 | ast_opt_array_size_print(is_array, array_size); |
| 530 | |
| 531 | if (initializer) { |
| 532 | printf("= "); |
| 533 | initializer->print(); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | |
| 538 | ast_declaration::ast_declaration(char *identifier, int is_array, |
| 539 | ast_expression *array_size, |
| 540 | ast_expression *initializer) |
| 541 | { |
| 542 | this->identifier = identifier; |
| 543 | this->is_array = is_array; |
| 544 | this->array_size = array_size; |
| 545 | this->initializer = initializer; |
| 546 | } |
| 547 | |
| 548 | |
| 549 | void |
| 550 | ast_declarator_list::print(void) const |
| 551 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 552 | assert(type || invariant); |
| 553 | |
| 554 | if (type) |
| 555 | type->print(); |
| 556 | else |
| 557 | printf("invariant "); |
| 558 | |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 559 | foreach_list_const (ptr, & this->declarations) { |
| 560 | if (ptr != this->declarations.get_head()) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 561 | printf(", "); |
| 562 | |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 563 | ast_node *ast = exec_node_data(ast_node, ptr, link); |
| 564 | ast->print(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | printf("; "); |
| 568 | } |
| 569 | |
| 570 | |
| 571 | ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type) |
| 572 | { |
| 573 | this->type = type; |
Ian Romanick | 3832706 | 2010-07-01 17:10:11 -0700 | [diff] [blame] | 574 | this->invariant = false; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | void |
| 578 | ast_jump_statement::print(void) const |
| 579 | { |
| 580 | switch (mode) { |
| 581 | case ast_continue: |
| 582 | printf("continue; "); |
| 583 | break; |
| 584 | case ast_break: |
| 585 | printf("break; "); |
| 586 | break; |
| 587 | case ast_return: |
| 588 | printf("return "); |
| 589 | if (opt_return_value) |
| 590 | opt_return_value->print(); |
| 591 | |
| 592 | printf("; "); |
| 593 | break; |
| 594 | case ast_discard: |
| 595 | printf("discard; "); |
| 596 | break; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | |
| 601 | ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value) |
| 602 | { |
| 603 | this->mode = ast_jump_modes(mode); |
| 604 | |
| 605 | if (mode == ast_return) |
| 606 | opt_return_value = return_value; |
| 607 | } |
| 608 | |
| 609 | |
| 610 | void |
| 611 | ast_selection_statement::print(void) const |
| 612 | { |
| 613 | printf("if ( "); |
| 614 | condition->print(); |
| 615 | printf(") "); |
| 616 | |
| 617 | then_statement->print(); |
| 618 | |
| 619 | if (else_statement) { |
| 620 | printf("else "); |
| 621 | else_statement->print(); |
| 622 | } |
| 623 | |
| 624 | } |
| 625 | |
| 626 | |
| 627 | ast_selection_statement::ast_selection_statement(ast_expression *condition, |
| 628 | ast_node *then_statement, |
| 629 | ast_node *else_statement) |
| 630 | { |
| 631 | this->condition = condition; |
| 632 | this->then_statement = then_statement; |
| 633 | this->else_statement = else_statement; |
| 634 | } |
| 635 | |
| 636 | |
| 637 | void |
| 638 | ast_iteration_statement::print(void) const |
| 639 | { |
| 640 | switch (mode) { |
| 641 | case ast_for: |
| 642 | printf("for( "); |
| 643 | if (init_statement) |
| 644 | init_statement->print(); |
| 645 | printf("; "); |
| 646 | |
| 647 | if (condition) |
| 648 | condition->print(); |
| 649 | printf("; "); |
| 650 | |
| 651 | if (rest_expression) |
| 652 | rest_expression->print(); |
| 653 | printf(") "); |
| 654 | |
| 655 | body->print(); |
| 656 | break; |
| 657 | |
| 658 | case ast_while: |
| 659 | printf("while ( "); |
| 660 | if (condition) |
| 661 | condition->print(); |
| 662 | printf(") "); |
| 663 | body->print(); |
| 664 | break; |
| 665 | |
| 666 | case ast_do_while: |
| 667 | printf("do "); |
| 668 | body->print(); |
| 669 | printf("while ( "); |
| 670 | if (condition) |
| 671 | condition->print(); |
| 672 | printf("); "); |
| 673 | break; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | |
| 678 | ast_iteration_statement::ast_iteration_statement(int mode, |
| 679 | ast_node *init, |
| 680 | ast_node *condition, |
| 681 | ast_expression *rest_expression, |
| 682 | ast_node *body) |
| 683 | { |
| 684 | this->mode = ast_iteration_modes(mode); |
| 685 | this->init_statement = init; |
| 686 | this->condition = condition; |
| 687 | this->rest_expression = rest_expression; |
| 688 | this->body = body; |
| 689 | } |
| 690 | |
| 691 | |
| 692 | void |
| 693 | ast_struct_specifier::print(void) const |
| 694 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 695 | printf("struct %s { ", name); |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 696 | foreach_list_const(n, &this->declarations) { |
| 697 | ast_node *ast = exec_node_data(ast_node, n, link); |
| 698 | ast->print(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 699 | } |
| 700 | printf("} "); |
| 701 | } |
| 702 | |
| 703 | |
| 704 | ast_struct_specifier::ast_struct_specifier(char *identifier, |
| 705 | ast_node *declarator_list) |
| 706 | { |
| 707 | name = identifier; |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 708 | this->declarations.push_degenerate_list_at_head(&declarator_list->link); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 709 | } |
Eric Anholt | 2f4fe15 | 2010-08-10 13:06:49 -0700 | [diff] [blame] | 710 | |
| 711 | bool |
| 712 | do_common_optimization(exec_list *ir, bool linked) |
| 713 | { |
| 714 | GLboolean progress = GL_FALSE; |
| 715 | |
| 716 | progress = do_sub_to_add_neg(ir) || progress; |
| 717 | |
| 718 | if (linked) { |
| 719 | progress = do_function_inlining(ir) || progress; |
| 720 | progress = do_dead_functions(ir) || progress; |
| 721 | } |
| 722 | progress = do_structure_splitting(ir) || progress; |
| 723 | progress = do_if_simplification(ir) || progress; |
| 724 | progress = do_copy_propagation(ir) || progress; |
| 725 | if (linked) |
| 726 | progress = do_dead_code(ir) || progress; |
| 727 | else |
| 728 | progress = do_dead_code_unlinked(ir) || progress; |
| 729 | progress = do_dead_code_local(ir) || progress; |
| 730 | progress = do_tree_grafting(ir) || progress; |
| 731 | progress = do_constant_propagation(ir) || progress; |
| 732 | if (linked) |
| 733 | progress = do_constant_variable(ir) || progress; |
| 734 | else |
| 735 | progress = do_constant_variable_unlinked(ir) || progress; |
| 736 | progress = do_constant_folding(ir) || progress; |
| 737 | progress = do_algebraic(ir) || progress; |
| 738 | progress = do_if_return(ir) || progress; |
| 739 | progress = do_vec_index_to_swizzle(ir) || progress; |
| 740 | progress = do_swizzle_swizzle(ir) || progress; |
Eric Anholt | 8f8cdbf | 2010-08-13 07:16:38 -0700 | [diff] [blame] | 741 | progress = do_noop_swizzle(ir) || progress; |
Eric Anholt | 2f4fe15 | 2010-08-10 13:06:49 -0700 | [diff] [blame] | 742 | |
Ian Romanick | 8df2dbf | 2010-08-26 16:45:22 -0700 | [diff] [blame] | 743 | loop_state *ls = analyze_loop_variables(ir); |
| 744 | progress = set_loop_controls(ir, ls) || progress; |
| 745 | delete ls; |
| 746 | |
Eric Anholt | 2f4fe15 | 2010-08-10 13:06:49 -0700 | [diff] [blame] | 747 | return progress; |
| 748 | } |
Eric Anholt | b838464 | 2010-08-18 16:56:39 -0700 | [diff] [blame] | 749 | |
| 750 | extern "C" { |
| 751 | |
| 752 | /** |
| 753 | * To be called at GL teardown time, this frees compiler datastructures. |
| 754 | * |
| 755 | * After calling this, any previously compiled shaders and shader |
| 756 | * programs would be invalid. So this should happen at approximately |
| 757 | * program exit. |
| 758 | */ |
| 759 | void |
| 760 | _mesa_destroy_shader_compiler(void) |
| 761 | { |
| 762 | _mesa_destroy_shader_compiler_caches(); |
| 763 | |
| 764 | _mesa_glsl_release_types(); |
| 765 | } |
| 766 | |
| 767 | /** |
| 768 | * Releases compiler caches to trade off performance for memory. |
| 769 | * |
| 770 | * Intended to be used with glReleaseShaderCompiler(). |
| 771 | */ |
| 772 | void |
| 773 | _mesa_destroy_shader_compiler_caches(void) |
| 774 | { |
| 775 | _mesa_glsl_release_functions(); |
| 776 | } |
| 777 | |
| 778 | } |