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