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 | 7f68cbd | 2010-10-05 17:00:31 -0700 | [diff] [blame^] | 184 | } else if (strcmp(name, "GL_ARB_explicit_attrib_location") == 0) { |
| 185 | state->ARB_explicit_attrib_location_enable = |
| 186 | (ext_mode != extension_disable); |
| 187 | state->ARB_explicit_attrib_location_warn = |
| 188 | (ext_mode == extension_warn); |
| 189 | |
| 190 | unsupported = !state->extensions->ARB_explicit_attrib_location; |
Ian Romanick | f50f065 | 2010-06-30 17:30:03 -0700 | [diff] [blame] | 191 | } else if (strcmp(name, "GL_ARB_fragment_coord_conventions") == 0) { |
| 192 | state->ARB_fragment_coord_conventions_enable = |
| 193 | (ext_mode != extension_disable); |
| 194 | state->ARB_fragment_coord_conventions_warn = |
| 195 | (ext_mode == extension_warn); |
| 196 | |
| 197 | unsupported = !state->extensions->ARB_fragment_coord_conventions; |
Ian Romanick | eb56cea | 2010-04-23 13:32:23 -0700 | [diff] [blame] | 198 | } else if (strcmp(name, "GL_ARB_texture_rectangle") == 0) { |
Ian Romanick | 0c82465 | 2010-04-07 17:13:44 -0700 | [diff] [blame] | 199 | state->ARB_texture_rectangle_enable = (ext_mode != extension_disable); |
| 200 | state->ARB_texture_rectangle_warn = (ext_mode == extension_warn); |
Ian Romanick | 667f4e1 | 2010-06-30 16:42:07 -0700 | [diff] [blame] | 201 | } else if (strcmp(name, "GL_EXT_texture_array") == 0) { |
| 202 | state->EXT_texture_array_enable = (ext_mode != extension_disable); |
| 203 | state->EXT_texture_array_warn = (ext_mode == extension_warn); |
| 204 | |
| 205 | unsupported = !state->extensions->EXT_texture_array; |
Ian Romanick | e701761 | 2010-04-07 16:46:25 -0700 | [diff] [blame] | 206 | } else { |
Ian Romanick | 887a8b0 | 2010-04-07 16:57:56 -0700 | [diff] [blame] | 207 | unsupported = true; |
| 208 | } |
| 209 | |
| 210 | if (unsupported) { |
| 211 | static const char *const fmt = "extension `%s' unsupported in %s shader"; |
| 212 | |
Ian Romanick | e701761 | 2010-04-07 16:46:25 -0700 | [diff] [blame] | 213 | if (ext_mode == extension_require) { |
Ian Romanick | 887a8b0 | 2010-04-07 16:57:56 -0700 | [diff] [blame] | 214 | _mesa_glsl_error(name_locp, state, fmt, |
| 215 | name, _mesa_glsl_shader_target_name(state->target)); |
Ian Romanick | e701761 | 2010-04-07 16:46:25 -0700 | [diff] [blame] | 216 | return false; |
Ian Romanick | 1799a0c | 2010-04-07 14:50:36 -0700 | [diff] [blame] | 217 | } else { |
Ian Romanick | 887a8b0 | 2010-04-07 16:57:56 -0700 | [diff] [blame] | 218 | _mesa_glsl_warning(name_locp, state, fmt, |
| 219 | name, _mesa_glsl_shader_target_name(state->target)); |
Ian Romanick | e701761 | 2010-04-07 16:46:25 -0700 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
| 223 | return true; |
| 224 | } |
| 225 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 226 | void |
| 227 | _mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q) |
| 228 | { |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 229 | if (q->flags.q.constant) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 230 | printf("const "); |
| 231 | |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 232 | if (q->flags.q.invariant) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 233 | printf("invariant "); |
| 234 | |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 235 | if (q->flags.q.attribute) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 236 | printf("attribute "); |
| 237 | |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 238 | if (q->flags.q.varying) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 239 | printf("varying "); |
| 240 | |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 241 | if (q->flags.q.in && q->flags.q.out) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 242 | printf("inout "); |
| 243 | else { |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 244 | if (q->flags.q.in) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 245 | printf("in "); |
| 246 | |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 247 | if (q->flags.q.out) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 248 | printf("out "); |
| 249 | } |
| 250 | |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 251 | if (q->flags.q.centroid) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 252 | printf("centroid "); |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 253 | if (q->flags.q.uniform) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 254 | printf("uniform "); |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 255 | if (q->flags.q.smooth) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 256 | printf("smooth "); |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 257 | if (q->flags.q.flat) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 258 | printf("flat "); |
Ian Romanick | e24d35a | 2010-10-05 16:38:47 -0700 | [diff] [blame] | 259 | if (q->flags.q.noperspective) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 260 | printf("noperspective "); |
| 261 | } |
| 262 | |
| 263 | |
| 264 | void |
| 265 | ast_node::print(void) const |
| 266 | { |
Ian Romanick | 03d3f3a | 2010-04-02 11:03:47 -0700 | [diff] [blame] | 267 | printf("unhandled node "); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | |
| 271 | ast_node::ast_node(void) |
| 272 | { |
Carl Worth | ec9675e | 2010-07-29 16:39:36 -0700 | [diff] [blame] | 273 | this->location.source = 0; |
| 274 | this->location.line = 0; |
| 275 | this->location.column = 0; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 278 | |
| 279 | static void |
| 280 | ast_opt_array_size_print(bool is_array, const ast_expression *array_size) |
| 281 | { |
| 282 | if (is_array) { |
| 283 | printf("[ "); |
| 284 | |
| 285 | if (array_size) |
| 286 | array_size->print(); |
| 287 | |
| 288 | printf("] "); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 293 | void |
| 294 | ast_compound_statement::print(void) const |
| 295 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 296 | printf("{\n"); |
| 297 | |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 298 | foreach_list_const(n, &this->statements) { |
| 299 | ast_node *ast = exec_node_data(ast_node, n, link); |
| 300 | ast->print(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | printf("}\n"); |
| 304 | } |
| 305 | |
| 306 | |
| 307 | ast_compound_statement::ast_compound_statement(int new_scope, |
| 308 | ast_node *statements) |
| 309 | { |
| 310 | this->new_scope = new_scope; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 311 | |
| 312 | if (statements != NULL) { |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 313 | this->statements.push_degenerate_list_at_head(&statements->link); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
| 317 | |
| 318 | void |
| 319 | ast_expression::print(void) const |
| 320 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 321 | switch (oper) { |
| 322 | case ast_assign: |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 323 | case ast_mul_assign: |
| 324 | case ast_div_assign: |
| 325 | case ast_mod_assign: |
| 326 | case ast_add_assign: |
| 327 | case ast_sub_assign: |
| 328 | case ast_ls_assign: |
| 329 | case ast_rs_assign: |
| 330 | case ast_and_assign: |
| 331 | case ast_xor_assign: |
| 332 | case ast_or_assign: |
| 333 | subexpressions[0]->print(); |
Ian Romanick | 88349b2 | 2010-02-22 19:10:25 -0800 | [diff] [blame] | 334 | printf("%s ", operator_string(oper)); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 335 | subexpressions[1]->print(); |
| 336 | break; |
| 337 | |
| 338 | case ast_field_selection: |
| 339 | subexpressions[0]->print(); |
| 340 | printf(". %s ", primary_expression.identifier); |
| 341 | break; |
| 342 | |
| 343 | case ast_plus: |
| 344 | case ast_neg: |
| 345 | case ast_bit_not: |
| 346 | case ast_logic_not: |
| 347 | case ast_pre_inc: |
| 348 | case ast_pre_dec: |
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 | subexpressions[0]->print(); |
| 351 | break; |
| 352 | |
| 353 | case ast_post_inc: |
| 354 | case ast_post_dec: |
| 355 | subexpressions[0]->print(); |
Ian Romanick | 88349b2 | 2010-02-22 19:10:25 -0800 | [diff] [blame] | 356 | printf("%s ", operator_string(oper)); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 357 | break; |
| 358 | |
| 359 | case ast_conditional: |
| 360 | subexpressions[0]->print(); |
| 361 | printf("? "); |
| 362 | subexpressions[1]->print(); |
| 363 | printf(": "); |
| 364 | subexpressions[1]->print(); |
| 365 | break; |
| 366 | |
| 367 | case ast_array_index: |
| 368 | subexpressions[0]->print(); |
| 369 | printf("[ "); |
| 370 | subexpressions[1]->print(); |
| 371 | printf("] "); |
| 372 | break; |
| 373 | |
| 374 | case ast_function_call: { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 375 | subexpressions[0]->print(); |
| 376 | printf("( "); |
| 377 | |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 378 | foreach_list_const (n, &this->expressions) { |
Ian Romanick | 2384937 | 2010-05-14 16:06:41 -0700 | [diff] [blame] | 379 | if (n != this->expressions.get_head()) |
| 380 | printf(", "); |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 381 | |
| 382 | ast_node *ast = exec_node_data(ast_node, n, link); |
| 383 | ast->print(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | printf(") "); |
| 387 | break; |
| 388 | } |
| 389 | |
| 390 | case ast_identifier: |
| 391 | printf("%s ", primary_expression.identifier); |
| 392 | break; |
| 393 | |
| 394 | case ast_int_constant: |
| 395 | printf("%d ", primary_expression.int_constant); |
| 396 | break; |
| 397 | |
| 398 | case ast_uint_constant: |
| 399 | printf("%u ", primary_expression.uint_constant); |
| 400 | break; |
| 401 | |
| 402 | case ast_float_constant: |
| 403 | printf("%f ", primary_expression.float_constant); |
| 404 | break; |
| 405 | |
| 406 | case ast_bool_constant: |
| 407 | printf("%s ", |
| 408 | primary_expression.bool_constant |
| 409 | ? "true" : "false"); |
| 410 | break; |
| 411 | |
| 412 | case ast_sequence: { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 413 | printf("( "); |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 414 | foreach_list_const(n, & this->expressions) { |
| 415 | if (n != this->expressions.get_head()) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 416 | printf(", "); |
| 417 | |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 418 | ast_node *ast = exec_node_data(ast_node, n, link); |
| 419 | ast->print(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 420 | } |
| 421 | printf(") "); |
| 422 | break; |
| 423 | } |
Ian Romanick | 88349b2 | 2010-02-22 19:10:25 -0800 | [diff] [blame] | 424 | |
| 425 | default: |
| 426 | assert(0); |
| 427 | break; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
| 431 | ast_expression::ast_expression(int oper, |
| 432 | ast_expression *ex0, |
| 433 | ast_expression *ex1, |
| 434 | ast_expression *ex2) |
| 435 | { |
| 436 | this->oper = ast_operators(oper); |
| 437 | this->subexpressions[0] = ex0; |
| 438 | this->subexpressions[1] = ex1; |
| 439 | this->subexpressions[2] = ex2; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | |
| 443 | void |
| 444 | ast_expression_statement::print(void) const |
| 445 | { |
| 446 | if (expression) |
| 447 | expression->print(); |
| 448 | |
| 449 | printf("; "); |
| 450 | } |
| 451 | |
| 452 | |
| 453 | ast_expression_statement::ast_expression_statement(ast_expression *ex) : |
| 454 | expression(ex) |
| 455 | { |
| 456 | /* empty */ |
| 457 | } |
| 458 | |
| 459 | |
| 460 | void |
| 461 | ast_function::print(void) const |
| 462 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 463 | return_type->print(); |
| 464 | printf(" %s (", identifier); |
| 465 | |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 466 | foreach_list_const(n, & this->parameters) { |
| 467 | ast_node *ast = exec_node_data(ast_node, n, link); |
| 468 | ast->print(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | printf(")"); |
| 472 | } |
| 473 | |
| 474 | |
| 475 | ast_function::ast_function(void) |
Ian Romanick | 92318a9 | 2010-03-31 18:23:21 -0700 | [diff] [blame] | 476 | : is_definition(false), signature(NULL) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 477 | { |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 478 | /* empty */ |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | |
| 482 | void |
| 483 | ast_fully_specified_type::print(void) const |
| 484 | { |
| 485 | _mesa_ast_type_qualifier_print(& qualifier); |
| 486 | specifier->print(); |
| 487 | } |
| 488 | |
| 489 | |
| 490 | void |
| 491 | ast_parameter_declarator::print(void) const |
| 492 | { |
| 493 | type->print(); |
| 494 | if (identifier) |
| 495 | printf("%s ", identifier); |
| 496 | ast_opt_array_size_print(is_array, array_size); |
| 497 | } |
| 498 | |
| 499 | |
| 500 | void |
| 501 | ast_function_definition::print(void) const |
| 502 | { |
| 503 | prototype->print(); |
| 504 | body->print(); |
| 505 | } |
| 506 | |
| 507 | |
| 508 | void |
| 509 | ast_declaration::print(void) const |
| 510 | { |
| 511 | printf("%s ", identifier); |
| 512 | ast_opt_array_size_print(is_array, array_size); |
| 513 | |
| 514 | if (initializer) { |
| 515 | printf("= "); |
| 516 | initializer->print(); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | |
| 521 | ast_declaration::ast_declaration(char *identifier, int is_array, |
| 522 | ast_expression *array_size, |
| 523 | ast_expression *initializer) |
| 524 | { |
| 525 | this->identifier = identifier; |
| 526 | this->is_array = is_array; |
| 527 | this->array_size = array_size; |
| 528 | this->initializer = initializer; |
| 529 | } |
| 530 | |
| 531 | |
| 532 | void |
| 533 | ast_declarator_list::print(void) const |
| 534 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 535 | assert(type || invariant); |
| 536 | |
| 537 | if (type) |
| 538 | type->print(); |
| 539 | else |
| 540 | printf("invariant "); |
| 541 | |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 542 | foreach_list_const (ptr, & this->declarations) { |
| 543 | if (ptr != this->declarations.get_head()) |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 544 | printf(", "); |
| 545 | |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 546 | ast_node *ast = exec_node_data(ast_node, ptr, link); |
| 547 | ast->print(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | printf("; "); |
| 551 | } |
| 552 | |
| 553 | |
| 554 | ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type) |
| 555 | { |
| 556 | this->type = type; |
Ian Romanick | 3832706 | 2010-07-01 17:10:11 -0700 | [diff] [blame] | 557 | this->invariant = false; |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | void |
| 561 | ast_jump_statement::print(void) const |
| 562 | { |
| 563 | switch (mode) { |
| 564 | case ast_continue: |
| 565 | printf("continue; "); |
| 566 | break; |
| 567 | case ast_break: |
| 568 | printf("break; "); |
| 569 | break; |
| 570 | case ast_return: |
| 571 | printf("return "); |
| 572 | if (opt_return_value) |
| 573 | opt_return_value->print(); |
| 574 | |
| 575 | printf("; "); |
| 576 | break; |
| 577 | case ast_discard: |
| 578 | printf("discard; "); |
| 579 | break; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | |
| 584 | ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value) |
| 585 | { |
| 586 | this->mode = ast_jump_modes(mode); |
| 587 | |
| 588 | if (mode == ast_return) |
| 589 | opt_return_value = return_value; |
| 590 | } |
| 591 | |
| 592 | |
| 593 | void |
| 594 | ast_selection_statement::print(void) const |
| 595 | { |
| 596 | printf("if ( "); |
| 597 | condition->print(); |
| 598 | printf(") "); |
| 599 | |
| 600 | then_statement->print(); |
| 601 | |
| 602 | if (else_statement) { |
| 603 | printf("else "); |
| 604 | else_statement->print(); |
| 605 | } |
| 606 | |
| 607 | } |
| 608 | |
| 609 | |
| 610 | ast_selection_statement::ast_selection_statement(ast_expression *condition, |
| 611 | ast_node *then_statement, |
| 612 | ast_node *else_statement) |
| 613 | { |
| 614 | this->condition = condition; |
| 615 | this->then_statement = then_statement; |
| 616 | this->else_statement = else_statement; |
| 617 | } |
| 618 | |
| 619 | |
| 620 | void |
| 621 | ast_iteration_statement::print(void) const |
| 622 | { |
| 623 | switch (mode) { |
| 624 | case ast_for: |
| 625 | printf("for( "); |
| 626 | if (init_statement) |
| 627 | init_statement->print(); |
| 628 | printf("; "); |
| 629 | |
| 630 | if (condition) |
| 631 | condition->print(); |
| 632 | printf("; "); |
| 633 | |
| 634 | if (rest_expression) |
| 635 | rest_expression->print(); |
| 636 | printf(") "); |
| 637 | |
| 638 | body->print(); |
| 639 | break; |
| 640 | |
| 641 | case ast_while: |
| 642 | printf("while ( "); |
| 643 | if (condition) |
| 644 | condition->print(); |
| 645 | printf(") "); |
| 646 | body->print(); |
| 647 | break; |
| 648 | |
| 649 | case ast_do_while: |
| 650 | printf("do "); |
| 651 | body->print(); |
| 652 | printf("while ( "); |
| 653 | if (condition) |
| 654 | condition->print(); |
| 655 | printf("); "); |
| 656 | break; |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | |
| 661 | ast_iteration_statement::ast_iteration_statement(int mode, |
| 662 | ast_node *init, |
| 663 | ast_node *condition, |
| 664 | ast_expression *rest_expression, |
| 665 | ast_node *body) |
| 666 | { |
| 667 | this->mode = ast_iteration_modes(mode); |
| 668 | this->init_statement = init; |
| 669 | this->condition = condition; |
| 670 | this->rest_expression = rest_expression; |
| 671 | this->body = body; |
| 672 | } |
| 673 | |
| 674 | |
| 675 | void |
| 676 | ast_struct_specifier::print(void) const |
| 677 | { |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 678 | printf("struct %s { ", name); |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 679 | foreach_list_const(n, &this->declarations) { |
| 680 | ast_node *ast = exec_node_data(ast_node, n, link); |
| 681 | ast->print(); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 682 | } |
| 683 | printf("} "); |
| 684 | } |
| 685 | |
| 686 | |
| 687 | ast_struct_specifier::ast_struct_specifier(char *identifier, |
| 688 | ast_node *declarator_list) |
| 689 | { |
Kenneth Graunke | ca92ae2 | 2010-09-18 11:11:09 +0200 | [diff] [blame] | 690 | if (identifier == NULL) { |
| 691 | static unsigned anon_count = 1; |
| 692 | identifier = talloc_asprintf(this, "#anon_struct_%04x", anon_count); |
| 693 | anon_count++; |
| 694 | } |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 695 | name = identifier; |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 696 | this->declarations.push_degenerate_list_at_head(&declarator_list->link); |
Ian Romanick | a87ac25 | 2010-02-22 13:19:34 -0800 | [diff] [blame] | 697 | } |
Eric Anholt | 2f4fe15 | 2010-08-10 13:06:49 -0700 | [diff] [blame] | 698 | |
| 699 | bool |
Luca Barbieri | e591c46 | 2010-09-05 22:29:58 +0200 | [diff] [blame] | 700 | do_common_optimization(exec_list *ir, bool linked, unsigned max_unroll_iterations) |
Eric Anholt | 2f4fe15 | 2010-08-10 13:06:49 -0700 | [diff] [blame] | 701 | { |
| 702 | GLboolean progress = GL_FALSE; |
| 703 | |
| 704 | progress = do_sub_to_add_neg(ir) || progress; |
| 705 | |
| 706 | if (linked) { |
| 707 | progress = do_function_inlining(ir) || progress; |
| 708 | progress = do_dead_functions(ir) || progress; |
| 709 | } |
| 710 | progress = do_structure_splitting(ir) || progress; |
| 711 | progress = do_if_simplification(ir) || progress; |
| 712 | progress = do_copy_propagation(ir) || progress; |
| 713 | if (linked) |
| 714 | progress = do_dead_code(ir) || progress; |
| 715 | else |
| 716 | progress = do_dead_code_unlinked(ir) || progress; |
| 717 | progress = do_dead_code_local(ir) || progress; |
| 718 | progress = do_tree_grafting(ir) || progress; |
| 719 | progress = do_constant_propagation(ir) || progress; |
| 720 | if (linked) |
| 721 | progress = do_constant_variable(ir) || progress; |
| 722 | else |
| 723 | progress = do_constant_variable_unlinked(ir) || progress; |
| 724 | progress = do_constant_folding(ir) || progress; |
| 725 | progress = do_algebraic(ir) || progress; |
Luca Barbieri | 3361cba | 2010-09-07 00:24:08 +0200 | [diff] [blame] | 726 | progress = do_lower_jumps(ir) || progress; |
Eric Anholt | 2f4fe15 | 2010-08-10 13:06:49 -0700 | [diff] [blame] | 727 | progress = do_vec_index_to_swizzle(ir) || progress; |
| 728 | progress = do_swizzle_swizzle(ir) || progress; |
Eric Anholt | 8f8cdbf | 2010-08-13 07:16:38 -0700 | [diff] [blame] | 729 | progress = do_noop_swizzle(ir) || progress; |
Eric Anholt | 2f4fe15 | 2010-08-10 13:06:49 -0700 | [diff] [blame] | 730 | |
Ian Romanick | 8f2214f | 2010-09-13 14:25:26 -0700 | [diff] [blame] | 731 | progress = optimize_redundant_jumps(ir) || progress; |
| 732 | |
Ian Romanick | 8df2dbf | 2010-08-26 16:45:22 -0700 | [diff] [blame] | 733 | loop_state *ls = analyze_loop_variables(ir); |
| 734 | progress = set_loop_controls(ir, ls) || progress; |
Luca Barbieri | e591c46 | 2010-09-05 22:29:58 +0200 | [diff] [blame] | 735 | progress = unroll_loops(ir, ls, max_unroll_iterations) || progress; |
Ian Romanick | 8df2dbf | 2010-08-26 16:45:22 -0700 | [diff] [blame] | 736 | delete ls; |
| 737 | |
Eric Anholt | 2f4fe15 | 2010-08-10 13:06:49 -0700 | [diff] [blame] | 738 | return progress; |
| 739 | } |
Eric Anholt | b838464 | 2010-08-18 16:56:39 -0700 | [diff] [blame] | 740 | |
| 741 | extern "C" { |
| 742 | |
| 743 | /** |
| 744 | * To be called at GL teardown time, this frees compiler datastructures. |
| 745 | * |
| 746 | * After calling this, any previously compiled shaders and shader |
| 747 | * programs would be invalid. So this should happen at approximately |
| 748 | * program exit. |
| 749 | */ |
| 750 | void |
| 751 | _mesa_destroy_shader_compiler(void) |
| 752 | { |
| 753 | _mesa_destroy_shader_compiler_caches(); |
| 754 | |
| 755 | _mesa_glsl_release_types(); |
| 756 | } |
| 757 | |
| 758 | /** |
| 759 | * Releases compiler caches to trade off performance for memory. |
| 760 | * |
| 761 | * Intended to be used with glReleaseShaderCompiler(). |
| 762 | */ |
| 763 | void |
| 764 | _mesa_destroy_shader_compiler_caches(void) |
| 765 | { |
| 766 | _mesa_glsl_release_functions(); |
| 767 | } |
| 768 | |
| 769 | } |