91122cc605fca3264b42592f3d7f4f1d58ea1ee1
[mesa.git] / src / glsl / glsl_parser_extras.cpp
1 /*
2 * Copyright © 2008, 2009 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <assert.h>
27
28 extern "C" {
29 #include "main/core.h" /* for struct gl_context */
30 #include "main/context.h"
31 }
32
33 #include "ralloc.h"
34 #include "ast.h"
35 #include "glsl_parser_extras.h"
36 #include "glsl_parser.h"
37 #include "ir_optimization.h"
38 #include "loop_analysis.h"
39
40 /**
41 * Format a short human-readable description of the given GLSL version.
42 */
43 const char *
44 glsl_compute_version_string(void *mem_ctx, bool is_es, unsigned version)
45 {
46 return ralloc_asprintf(mem_ctx, "GLSL%s %d.%02d", is_es ? " ES" : "",
47 version / 100, version % 100);
48 }
49
50 _mesa_glsl_parse_state::_mesa_glsl_parse_state(struct gl_context *_ctx,
51 GLenum target, void *mem_ctx)
52 : ctx(_ctx)
53 {
54 switch (target) {
55 case GL_VERTEX_SHADER: this->target = vertex_shader; break;
56 case GL_FRAGMENT_SHADER: this->target = fragment_shader; break;
57 case GL_GEOMETRY_SHADER: this->target = geometry_shader; break;
58 }
59
60 this->scanner = NULL;
61 this->translation_unit.make_empty();
62 this->symbols = new(mem_ctx) glsl_symbol_table;
63 this->info_log = ralloc_strdup(mem_ctx, "");
64 this->error = false;
65 this->loop_nesting_ast = NULL;
66 this->switch_state.switch_nesting_ast = NULL;
67
68 this->num_builtins_to_link = 0;
69
70 /* Set default language version and extensions */
71 this->language_version = 110;
72 this->es_shader = false;
73 this->ARB_texture_rectangle_enable = true;
74
75 /* OpenGL ES 2.0 has different defaults from desktop GL. */
76 if (ctx->API == API_OPENGLES2) {
77 this->language_version = 100;
78 this->es_shader = true;
79 this->ARB_texture_rectangle_enable = false;
80 }
81
82 this->extensions = &ctx->Extensions;
83
84 this->Const.MaxLights = ctx->Const.MaxLights;
85 this->Const.MaxClipPlanes = ctx->Const.MaxClipPlanes;
86 this->Const.MaxTextureUnits = ctx->Const.MaxTextureUnits;
87 this->Const.MaxTextureCoords = ctx->Const.MaxTextureCoordUnits;
88 this->Const.MaxVertexAttribs = ctx->Const.VertexProgram.MaxAttribs;
89 this->Const.MaxVertexUniformComponents = ctx->Const.VertexProgram.MaxUniformComponents;
90 this->Const.MaxVaryingFloats = ctx->Const.MaxVarying * 4;
91 this->Const.MaxVertexTextureImageUnits = ctx->Const.MaxVertexTextureImageUnits;
92 this->Const.MaxCombinedTextureImageUnits = ctx->Const.MaxCombinedTextureImageUnits;
93 this->Const.MaxTextureImageUnits = ctx->Const.MaxTextureImageUnits;
94 this->Const.MaxFragmentUniformComponents = ctx->Const.FragmentProgram.MaxUniformComponents;
95 this->Const.MinProgramTexelOffset = ctx->Const.MinProgramTexelOffset;
96 this->Const.MaxProgramTexelOffset = ctx->Const.MaxProgramTexelOffset;
97
98 this->Const.MaxDrawBuffers = ctx->Const.MaxDrawBuffers;
99
100 const unsigned lowest_version =
101 (ctx->API == API_OPENGLES2) || ctx->Extensions.ARB_ES2_compatibility
102 ? 100 : 110;
103 const unsigned highest_version =
104 _mesa_is_desktop_gl(ctx) ? ctx->Const.GLSLVersion : 100;
105 char *supported = ralloc_strdup(this, "");
106
107 for (unsigned ver = lowest_version; ver <= highest_version; ver += 10) {
108 const char *const prefix = (ver == lowest_version)
109 ? ""
110 : ((ver == highest_version) ? ", and " : ", ");
111
112 ralloc_asprintf_append(& supported, "%s%d.%02d%s",
113 prefix,
114 ver / 100, ver % 100,
115 (ver == 100) ? " ES" : "");
116 }
117
118 this->supported_version_string = supported;
119
120 if (ctx->Const.ForceGLSLExtensionsWarn)
121 _mesa_glsl_process_extension("all", NULL, "warn", NULL, this);
122
123 this->default_uniform_qualifier = new(this) ast_type_qualifier();
124 this->default_uniform_qualifier->flags.q.shared = 1;
125 this->default_uniform_qualifier->flags.q.column_major = 1;
126 }
127
128 /**
129 * Determine whether the current GLSL version is sufficiently high to support
130 * a certain feature, and generate an error message if it isn't.
131 *
132 * \param required_glsl_version and \c required_glsl_es_version are
133 * interpreted as they are in _mesa_glsl_parse_state::is_version().
134 *
135 * \param locp is the parser location where the error should be reported.
136 *
137 * \param fmt (and additional arguments) constitute a printf-style error
138 * message to report if the version check fails. Information about the
139 * current and required GLSL versions will be appended. So, for example, if
140 * the GLSL version being compiled is 1.20, and check_version(130, 300, locp,
141 * "foo unsupported") is called, the error message will be "foo unsupported in
142 * GLSL 1.20 (GLSL 1.30 or GLSL 3.00 ES required)".
143 */
144 bool
145 _mesa_glsl_parse_state::check_version(unsigned required_glsl_version,
146 unsigned required_glsl_es_version,
147 YYLTYPE *locp, const char *fmt, ...)
148 {
149 if (this->is_version(required_glsl_version, required_glsl_es_version))
150 return true;
151
152 va_list args;
153 va_start(args, fmt);
154 char *problem = ralloc_vasprintf(this, fmt, args);
155 va_end(args);
156 const char *glsl_version_string
157 = glsl_compute_version_string(ctx, false, required_glsl_version);
158 const char *glsl_es_version_string
159 = glsl_compute_version_string(ctx, true, required_glsl_es_version);
160 const char *requirement_string = "";
161 if (required_glsl_version && required_glsl_es_version) {
162 requirement_string = ralloc_asprintf(this, " (%s or %s required)",
163 glsl_version_string,
164 glsl_es_version_string);
165 } else if (required_glsl_version) {
166 requirement_string = ralloc_asprintf(this, " (%s required)",
167 glsl_version_string);
168 } else if (required_glsl_es_version) {
169 requirement_string = ralloc_asprintf(this, " (%s required)",
170 glsl_es_version_string);
171 }
172 _mesa_glsl_error(locp, this, "%s in %s%s.",
173 problem, this->get_version_string(),
174 requirement_string);
175
176 return false;
177 }
178
179 /**
180 * Process a GLSL #version directive.
181 *
182 * \param version is the integer that follows the #version token.
183 *
184 * \param ident is a string identifier that follows the integer, if any is
185 * present. Otherwise NULL.
186 */
187 void
188 _mesa_glsl_parse_state::process_version_directive(YYLTYPE *locp, int version,
189 const char *ident)
190 {
191 bool es_token_present = false;
192 if (ident) {
193 if (strcmp(ident, "es") == 0) {
194 es_token_present = true;
195 } else {
196 _mesa_glsl_error(locp, this,
197 "Illegal text following version number\n");
198 }
199 }
200
201 bool supported = false;
202
203 if (es_token_present) {
204 this->es_shader = true;
205 switch (version) {
206 case 100:
207 _mesa_glsl_error(locp, this,
208 "GLSL 1.00 ES should be selected using "
209 "`#version 100'\n");
210 supported = this->ctx->API == API_OPENGLES2 ||
211 this->ctx->Extensions.ARB_ES2_compatibility;
212 break;
213 case 300:
214 supported = _mesa_is_gles3(this->ctx) ||
215 this->ctx->Extensions.ARB_ES3_compatibility;
216 break;
217 default:
218 supported = false;
219 break;
220 }
221 } else {
222 switch (version) {
223 case 100:
224 this->es_shader = true;
225 supported = this->ctx->API == API_OPENGLES2 ||
226 this->ctx->Extensions.ARB_ES2_compatibility;
227 break;
228 case 110:
229 case 120:
230 /* FINISHME: Once the OpenGL 3.0 'forward compatible' context or
231 * the OpenGL 3.2 Core context is supported, this logic will need
232 * change. Older versions of GLSL are no longer supported
233 * outside the compatibility contexts of 3.x.
234 */
235 case 130:
236 case 140:
237 case 150:
238 case 330:
239 case 400:
240 case 410:
241 case 420:
242 supported = _mesa_is_desktop_gl(this->ctx) &&
243 ((unsigned) version) <= this->ctx->Const.GLSLVersion;
244 break;
245 default:
246 supported = false;
247 break;
248 }
249 }
250
251 this->language_version = version;
252
253 if (!supported) {
254 _mesa_glsl_error(locp, this, "%s is not supported. "
255 "Supported versions are: %s\n",
256 this->get_version_string(),
257 this->supported_version_string);
258
259 /* On exit, the language_version must be set to a valid value.
260 * Later calls to _mesa_glsl_initialize_types will misbehave if
261 * the version is invalid.
262 */
263 switch (this->ctx->API) {
264 case API_OPENGL_COMPAT:
265 case API_OPENGL_CORE:
266 this->language_version = this->ctx->Const.GLSLVersion;
267 break;
268
269 case API_OPENGLES:
270 assert(!"Should not get here.");
271 /* FALLTHROUGH */
272
273 case API_OPENGLES2:
274 this->language_version = 100;
275 break;
276 }
277 }
278
279 if (this->language_version >= 140) {
280 this->ARB_uniform_buffer_object_enable = true;
281 }
282
283 if (this->language_version == 300 && this->es_shader) {
284 this->ARB_explicit_attrib_location_enable = true;
285 }
286 }
287
288 const char *
289 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target)
290 {
291 switch (target) {
292 case vertex_shader: return "vertex";
293 case fragment_shader: return "fragment";
294 case geometry_shader: return "geometry";
295 }
296
297 assert(!"Should not get here.");
298 return "unknown";
299 }
300
301 /* This helper function will append the given message to the shader's
302 info log and report it via GL_ARB_debug_output. Per that extension,
303 'type' is one of the enum values classifying the message, and
304 'id' is the implementation-defined ID of the given message. */
305 static void
306 _mesa_glsl_msg(const YYLTYPE *locp, _mesa_glsl_parse_state *state,
307 GLenum type, GLuint id, const char *fmt, va_list ap)
308 {
309 bool error = (type == GL_DEBUG_TYPE_ERROR_ARB);
310
311 assert(state->info_log != NULL);
312
313 /* Get the offset that the new message will be written to. */
314 int msg_offset = strlen(state->info_log);
315
316 ralloc_asprintf_append(&state->info_log, "%u:%u(%u): %s: ",
317 locp->source,
318 locp->first_line,
319 locp->first_column,
320 error ? "error" : "warning");
321 ralloc_vasprintf_append(&state->info_log, fmt, ap);
322
323 const char *const msg = &state->info_log[msg_offset];
324 struct gl_context *ctx = state->ctx;
325 /* Report the error via GL_ARB_debug_output. */
326 if (error)
327 _mesa_shader_debug(ctx, type, id, msg, strlen(msg));
328
329 ralloc_strcat(&state->info_log, "\n");
330 }
331
332 void
333 _mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
334 const char *fmt, ...)
335 {
336 va_list ap;
337 GLenum type = GL_DEBUG_TYPE_ERROR_ARB;
338
339 state->error = true;
340
341 va_start(ap, fmt);
342 _mesa_glsl_msg(locp, state, type, SHADER_ERROR_UNKNOWN, fmt, ap);
343 va_end(ap);
344 }
345
346
347 void
348 _mesa_glsl_warning(const YYLTYPE *locp, _mesa_glsl_parse_state *state,
349 const char *fmt, ...)
350 {
351 va_list ap;
352 GLenum type = GL_DEBUG_TYPE_OTHER_ARB;
353
354 va_start(ap, fmt);
355 _mesa_glsl_msg(locp, state, type, 0, fmt, ap);
356 va_end(ap);
357 }
358
359
360 /**
361 * Enum representing the possible behaviors that can be specified in
362 * an #extension directive.
363 */
364 enum ext_behavior {
365 extension_disable,
366 extension_enable,
367 extension_require,
368 extension_warn
369 };
370
371 /**
372 * Element type for _mesa_glsl_supported_extensions
373 */
374 struct _mesa_glsl_extension {
375 /**
376 * Name of the extension when referred to in a GLSL extension
377 * statement
378 */
379 const char *name;
380
381 /** True if this extension is available to vertex shaders */
382 bool avail_in_VS;
383
384 /** True if this extension is available to geometry shaders */
385 bool avail_in_GS;
386
387 /** True if this extension is available to fragment shaders */
388 bool avail_in_FS;
389
390 /** True if this extension is available to desktop GL shaders */
391 bool avail_in_GL;
392
393 /** True if this extension is available to GLES shaders */
394 bool avail_in_ES;
395
396 /**
397 * Flag in the gl_extensions struct indicating whether this
398 * extension is supported by the driver, or
399 * &gl_extensions::dummy_true if supported by all drivers.
400 *
401 * Note: the type (GLboolean gl_extensions::*) is a "pointer to
402 * member" type, the type-safe alternative to the "offsetof" macro.
403 * In a nutshell:
404 *
405 * - foo bar::* p declares p to be an "offset" to a field of type
406 * foo that exists within struct bar
407 * - &bar::baz computes the "offset" of field baz within struct bar
408 * - x.*p accesses the field of x that exists at "offset" p
409 * - x->*p is equivalent to (*x).*p
410 */
411 const GLboolean gl_extensions::* supported_flag;
412
413 /**
414 * Flag in the _mesa_glsl_parse_state struct that should be set
415 * when this extension is enabled.
416 *
417 * See note in _mesa_glsl_extension::supported_flag about "pointer
418 * to member" types.
419 */
420 bool _mesa_glsl_parse_state::* enable_flag;
421
422 /**
423 * Flag in the _mesa_glsl_parse_state struct that should be set
424 * when the shader requests "warn" behavior for this extension.
425 *
426 * See note in _mesa_glsl_extension::supported_flag about "pointer
427 * to member" types.
428 */
429 bool _mesa_glsl_parse_state::* warn_flag;
430
431
432 bool compatible_with_state(const _mesa_glsl_parse_state *state) const;
433 void set_flags(_mesa_glsl_parse_state *state, ext_behavior behavior) const;
434 };
435
436 #define EXT(NAME, VS, GS, FS, GL, ES, SUPPORTED_FLAG) \
437 { "GL_" #NAME, VS, GS, FS, GL, ES, &gl_extensions::SUPPORTED_FLAG, \
438 &_mesa_glsl_parse_state::NAME##_enable, \
439 &_mesa_glsl_parse_state::NAME##_warn }
440
441 /**
442 * Table of extensions that can be enabled/disabled within a shader,
443 * and the conditions under which they are supported.
444 */
445 static const _mesa_glsl_extension _mesa_glsl_supported_extensions[] = {
446 /* target availability API availability */
447 /* name VS GS FS GL ES supported flag */
448 EXT(ARB_conservative_depth, false, false, true, true, false, ARB_conservative_depth),
449 EXT(ARB_draw_buffers, false, false, true, true, false, dummy_true),
450 EXT(ARB_draw_instanced, true, false, false, true, false, ARB_draw_instanced),
451 EXT(ARB_explicit_attrib_location, true, false, true, true, false, ARB_explicit_attrib_location),
452 EXT(ARB_fragment_coord_conventions, true, false, true, true, false, ARB_fragment_coord_conventions),
453 EXT(ARB_texture_rectangle, true, false, true, true, false, dummy_true),
454 EXT(EXT_texture_array, true, false, true, true, false, EXT_texture_array),
455 EXT(ARB_shader_texture_lod, true, false, true, true, false, ARB_shader_texture_lod),
456 EXT(ARB_shader_stencil_export, false, false, true, true, false, ARB_shader_stencil_export),
457 EXT(AMD_conservative_depth, false, false, true, true, false, ARB_conservative_depth),
458 EXT(AMD_shader_stencil_export, false, false, true, true, false, ARB_shader_stencil_export),
459 EXT(OES_texture_3D, true, false, true, false, true, EXT_texture3D),
460 EXT(OES_EGL_image_external, true, false, true, false, true, OES_EGL_image_external),
461 EXT(ARB_shader_bit_encoding, true, true, true, true, false, ARB_shader_bit_encoding),
462 EXT(ARB_uniform_buffer_object, true, false, true, true, false, ARB_uniform_buffer_object),
463 EXT(OES_standard_derivatives, false, false, true, false, true, OES_standard_derivatives),
464 EXT(ARB_texture_cube_map_array, true, false, true, true, false, ARB_texture_cube_map_array),
465 };
466
467 #undef EXT
468
469
470 /**
471 * Determine whether a given extension is compatible with the target,
472 * API, and extension information in the current parser state.
473 */
474 bool _mesa_glsl_extension::compatible_with_state(const _mesa_glsl_parse_state *
475 state) const
476 {
477 /* Check that this extension matches the type of shader we are
478 * compiling to.
479 */
480 switch (state->target) {
481 case vertex_shader:
482 if (!this->avail_in_VS) {
483 return false;
484 }
485 break;
486 case geometry_shader:
487 if (!this->avail_in_GS) {
488 return false;
489 }
490 break;
491 case fragment_shader:
492 if (!this->avail_in_FS) {
493 return false;
494 }
495 break;
496 default:
497 assert (!"Unrecognized shader target");
498 return false;
499 }
500
501 /* Check that this extension matches whether we are compiling
502 * for desktop GL or GLES.
503 */
504 if (state->es_shader) {
505 if (!this->avail_in_ES) return false;
506 } else {
507 if (!this->avail_in_GL) return false;
508 }
509
510 /* Check that this extension is supported by the OpenGL
511 * implementation.
512 *
513 * Note: the ->* operator indexes into state->extensions by the
514 * offset this->supported_flag. See
515 * _mesa_glsl_extension::supported_flag for more info.
516 */
517 return state->extensions->*(this->supported_flag);
518 }
519
520 /**
521 * Set the appropriate flags in the parser state to establish the
522 * given behavior for this extension.
523 */
524 void _mesa_glsl_extension::set_flags(_mesa_glsl_parse_state *state,
525 ext_behavior behavior) const
526 {
527 /* Note: the ->* operator indexes into state by the
528 * offsets this->enable_flag and this->warn_flag. See
529 * _mesa_glsl_extension::supported_flag for more info.
530 */
531 state->*(this->enable_flag) = (behavior != extension_disable);
532 state->*(this->warn_flag) = (behavior == extension_warn);
533 }
534
535 /**
536 * Find an extension by name in _mesa_glsl_supported_extensions. If
537 * the name is not found, return NULL.
538 */
539 static const _mesa_glsl_extension *find_extension(const char *name)
540 {
541 for (unsigned i = 0; i < Elements(_mesa_glsl_supported_extensions); ++i) {
542 if (strcmp(name, _mesa_glsl_supported_extensions[i].name) == 0) {
543 return &_mesa_glsl_supported_extensions[i];
544 }
545 }
546 return NULL;
547 }
548
549
550 bool
551 _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
552 const char *behavior_string, YYLTYPE *behavior_locp,
553 _mesa_glsl_parse_state *state)
554 {
555 ext_behavior behavior;
556 if (strcmp(behavior_string, "warn") == 0) {
557 behavior = extension_warn;
558 } else if (strcmp(behavior_string, "require") == 0) {
559 behavior = extension_require;
560 } else if (strcmp(behavior_string, "enable") == 0) {
561 behavior = extension_enable;
562 } else if (strcmp(behavior_string, "disable") == 0) {
563 behavior = extension_disable;
564 } else {
565 _mesa_glsl_error(behavior_locp, state,
566 "Unknown extension behavior `%s'",
567 behavior_string);
568 return false;
569 }
570
571 if (strcmp(name, "all") == 0) {
572 if ((behavior == extension_enable) || (behavior == extension_require)) {
573 _mesa_glsl_error(name_locp, state, "Cannot %s all extensions",
574 (behavior == extension_enable)
575 ? "enable" : "require");
576 return false;
577 } else {
578 for (unsigned i = 0;
579 i < Elements(_mesa_glsl_supported_extensions); ++i) {
580 const _mesa_glsl_extension *extension
581 = &_mesa_glsl_supported_extensions[i];
582 if (extension->compatible_with_state(state)) {
583 _mesa_glsl_supported_extensions[i].set_flags(state, behavior);
584 }
585 }
586 }
587 } else {
588 const _mesa_glsl_extension *extension = find_extension(name);
589 if (extension && extension->compatible_with_state(state)) {
590 extension->set_flags(state, behavior);
591 } else {
592 static const char *const fmt = "extension `%s' unsupported in %s shader";
593
594 if (behavior == extension_require) {
595 _mesa_glsl_error(name_locp, state, fmt,
596 name, _mesa_glsl_shader_target_name(state->target));
597 return false;
598 } else {
599 _mesa_glsl_warning(name_locp, state, fmt,
600 name, _mesa_glsl_shader_target_name(state->target));
601 }
602 }
603 }
604
605 return true;
606 }
607
608 void
609 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q)
610 {
611 if (q->flags.q.constant)
612 printf("const ");
613
614 if (q->flags.q.invariant)
615 printf("invariant ");
616
617 if (q->flags.q.attribute)
618 printf("attribute ");
619
620 if (q->flags.q.varying)
621 printf("varying ");
622
623 if (q->flags.q.in && q->flags.q.out)
624 printf("inout ");
625 else {
626 if (q->flags.q.in)
627 printf("in ");
628
629 if (q->flags.q.out)
630 printf("out ");
631 }
632
633 if (q->flags.q.centroid)
634 printf("centroid ");
635 if (q->flags.q.uniform)
636 printf("uniform ");
637 if (q->flags.q.smooth)
638 printf("smooth ");
639 if (q->flags.q.flat)
640 printf("flat ");
641 if (q->flags.q.noperspective)
642 printf("noperspective ");
643 }
644
645
646 void
647 ast_node::print(void) const
648 {
649 printf("unhandled node ");
650 }
651
652
653 ast_node::ast_node(void)
654 {
655 this->location.source = 0;
656 this->location.line = 0;
657 this->location.column = 0;
658 }
659
660
661 static void
662 ast_opt_array_size_print(bool is_array, const ast_expression *array_size)
663 {
664 if (is_array) {
665 printf("[ ");
666
667 if (array_size)
668 array_size->print();
669
670 printf("] ");
671 }
672 }
673
674
675 void
676 ast_compound_statement::print(void) const
677 {
678 printf("{\n");
679
680 foreach_list_const(n, &this->statements) {
681 ast_node *ast = exec_node_data(ast_node, n, link);
682 ast->print();
683 }
684
685 printf("}\n");
686 }
687
688
689 ast_compound_statement::ast_compound_statement(int new_scope,
690 ast_node *statements)
691 {
692 this->new_scope = new_scope;
693
694 if (statements != NULL) {
695 this->statements.push_degenerate_list_at_head(&statements->link);
696 }
697 }
698
699
700 void
701 ast_expression::print(void) const
702 {
703 switch (oper) {
704 case ast_assign:
705 case ast_mul_assign:
706 case ast_div_assign:
707 case ast_mod_assign:
708 case ast_add_assign:
709 case ast_sub_assign:
710 case ast_ls_assign:
711 case ast_rs_assign:
712 case ast_and_assign:
713 case ast_xor_assign:
714 case ast_or_assign:
715 subexpressions[0]->print();
716 printf("%s ", operator_string(oper));
717 subexpressions[1]->print();
718 break;
719
720 case ast_field_selection:
721 subexpressions[0]->print();
722 printf(". %s ", primary_expression.identifier);
723 break;
724
725 case ast_plus:
726 case ast_neg:
727 case ast_bit_not:
728 case ast_logic_not:
729 case ast_pre_inc:
730 case ast_pre_dec:
731 printf("%s ", operator_string(oper));
732 subexpressions[0]->print();
733 break;
734
735 case ast_post_inc:
736 case ast_post_dec:
737 subexpressions[0]->print();
738 printf("%s ", operator_string(oper));
739 break;
740
741 case ast_conditional:
742 subexpressions[0]->print();
743 printf("? ");
744 subexpressions[1]->print();
745 printf(": ");
746 subexpressions[2]->print();
747 break;
748
749 case ast_array_index:
750 subexpressions[0]->print();
751 printf("[ ");
752 subexpressions[1]->print();
753 printf("] ");
754 break;
755
756 case ast_function_call: {
757 subexpressions[0]->print();
758 printf("( ");
759
760 foreach_list_const (n, &this->expressions) {
761 if (n != this->expressions.get_head())
762 printf(", ");
763
764 ast_node *ast = exec_node_data(ast_node, n, link);
765 ast->print();
766 }
767
768 printf(") ");
769 break;
770 }
771
772 case ast_identifier:
773 printf("%s ", primary_expression.identifier);
774 break;
775
776 case ast_int_constant:
777 printf("%d ", primary_expression.int_constant);
778 break;
779
780 case ast_uint_constant:
781 printf("%u ", primary_expression.uint_constant);
782 break;
783
784 case ast_float_constant:
785 printf("%f ", primary_expression.float_constant);
786 break;
787
788 case ast_bool_constant:
789 printf("%s ",
790 primary_expression.bool_constant
791 ? "true" : "false");
792 break;
793
794 case ast_sequence: {
795 printf("( ");
796 foreach_list_const(n, & this->expressions) {
797 if (n != this->expressions.get_head())
798 printf(", ");
799
800 ast_node *ast = exec_node_data(ast_node, n, link);
801 ast->print();
802 }
803 printf(") ");
804 break;
805 }
806
807 default:
808 assert(0);
809 break;
810 }
811 }
812
813 ast_expression::ast_expression(int oper,
814 ast_expression *ex0,
815 ast_expression *ex1,
816 ast_expression *ex2)
817 {
818 this->oper = ast_operators(oper);
819 this->subexpressions[0] = ex0;
820 this->subexpressions[1] = ex1;
821 this->subexpressions[2] = ex2;
822 this->non_lvalue_description = NULL;
823 }
824
825
826 void
827 ast_expression_statement::print(void) const
828 {
829 if (expression)
830 expression->print();
831
832 printf("; ");
833 }
834
835
836 ast_expression_statement::ast_expression_statement(ast_expression *ex) :
837 expression(ex)
838 {
839 /* empty */
840 }
841
842
843 void
844 ast_function::print(void) const
845 {
846 return_type->print();
847 printf(" %s (", identifier);
848
849 foreach_list_const(n, & this->parameters) {
850 ast_node *ast = exec_node_data(ast_node, n, link);
851 ast->print();
852 }
853
854 printf(")");
855 }
856
857
858 ast_function::ast_function(void)
859 : is_definition(false), signature(NULL)
860 {
861 /* empty */
862 }
863
864
865 void
866 ast_fully_specified_type::print(void) const
867 {
868 _mesa_ast_type_qualifier_print(& qualifier);
869 specifier->print();
870 }
871
872
873 void
874 ast_parameter_declarator::print(void) const
875 {
876 type->print();
877 if (identifier)
878 printf("%s ", identifier);
879 ast_opt_array_size_print(is_array, array_size);
880 }
881
882
883 void
884 ast_function_definition::print(void) const
885 {
886 prototype->print();
887 body->print();
888 }
889
890
891 void
892 ast_declaration::print(void) const
893 {
894 printf("%s ", identifier);
895 ast_opt_array_size_print(is_array, array_size);
896
897 if (initializer) {
898 printf("= ");
899 initializer->print();
900 }
901 }
902
903
904 ast_declaration::ast_declaration(const char *identifier, int is_array,
905 ast_expression *array_size,
906 ast_expression *initializer)
907 {
908 this->identifier = identifier;
909 this->is_array = is_array;
910 this->array_size = array_size;
911 this->initializer = initializer;
912 }
913
914
915 void
916 ast_declarator_list::print(void) const
917 {
918 assert(type || invariant);
919
920 if (type)
921 type->print();
922 else
923 printf("invariant ");
924
925 foreach_list_const (ptr, & this->declarations) {
926 if (ptr != this->declarations.get_head())
927 printf(", ");
928
929 ast_node *ast = exec_node_data(ast_node, ptr, link);
930 ast->print();
931 }
932
933 printf("; ");
934 }
935
936
937 ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type)
938 {
939 this->type = type;
940 this->invariant = false;
941 this->ubo_qualifiers_valid = false;
942 }
943
944 void
945 ast_jump_statement::print(void) const
946 {
947 switch (mode) {
948 case ast_continue:
949 printf("continue; ");
950 break;
951 case ast_break:
952 printf("break; ");
953 break;
954 case ast_return:
955 printf("return ");
956 if (opt_return_value)
957 opt_return_value->print();
958
959 printf("; ");
960 break;
961 case ast_discard:
962 printf("discard; ");
963 break;
964 }
965 }
966
967
968 ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value)
969 {
970 this->mode = ast_jump_modes(mode);
971
972 if (mode == ast_return)
973 opt_return_value = return_value;
974 }
975
976
977 void
978 ast_selection_statement::print(void) const
979 {
980 printf("if ( ");
981 condition->print();
982 printf(") ");
983
984 then_statement->print();
985
986 if (else_statement) {
987 printf("else ");
988 else_statement->print();
989 }
990
991 }
992
993
994 ast_selection_statement::ast_selection_statement(ast_expression *condition,
995 ast_node *then_statement,
996 ast_node *else_statement)
997 {
998 this->condition = condition;
999 this->then_statement = then_statement;
1000 this->else_statement = else_statement;
1001 }
1002
1003
1004 void
1005 ast_switch_statement::print(void) const
1006 {
1007 printf("switch ( ");
1008 test_expression->print();
1009 printf(") ");
1010
1011 body->print();
1012 }
1013
1014
1015 ast_switch_statement::ast_switch_statement(ast_expression *test_expression,
1016 ast_node *body)
1017 {
1018 this->test_expression = test_expression;
1019 this->body = body;
1020 }
1021
1022
1023 void
1024 ast_switch_body::print(void) const
1025 {
1026 printf("{\n");
1027 if (stmts != NULL) {
1028 stmts->print();
1029 }
1030 printf("}\n");
1031 }
1032
1033
1034 ast_switch_body::ast_switch_body(ast_case_statement_list *stmts)
1035 {
1036 this->stmts = stmts;
1037 }
1038
1039
1040 void ast_case_label::print(void) const
1041 {
1042 if (test_value != NULL) {
1043 printf("case ");
1044 test_value->print();
1045 printf(": ");
1046 } else {
1047 printf("default: ");
1048 }
1049 }
1050
1051
1052 ast_case_label::ast_case_label(ast_expression *test_value)
1053 {
1054 this->test_value = test_value;
1055 }
1056
1057
1058 void ast_case_label_list::print(void) const
1059 {
1060 foreach_list_const(n, & this->labels) {
1061 ast_node *ast = exec_node_data(ast_node, n, link);
1062 ast->print();
1063 }
1064 printf("\n");
1065 }
1066
1067
1068 ast_case_label_list::ast_case_label_list(void)
1069 {
1070 }
1071
1072
1073 void ast_case_statement::print(void) const
1074 {
1075 labels->print();
1076 foreach_list_const(n, & this->stmts) {
1077 ast_node *ast = exec_node_data(ast_node, n, link);
1078 ast->print();
1079 printf("\n");
1080 }
1081 }
1082
1083
1084 ast_case_statement::ast_case_statement(ast_case_label_list *labels)
1085 {
1086 this->labels = labels;
1087 }
1088
1089
1090 void ast_case_statement_list::print(void) const
1091 {
1092 foreach_list_const(n, & this->cases) {
1093 ast_node *ast = exec_node_data(ast_node, n, link);
1094 ast->print();
1095 }
1096 }
1097
1098
1099 ast_case_statement_list::ast_case_statement_list(void)
1100 {
1101 }
1102
1103
1104 void
1105 ast_iteration_statement::print(void) const
1106 {
1107 switch (mode) {
1108 case ast_for:
1109 printf("for( ");
1110 if (init_statement)
1111 init_statement->print();
1112 printf("; ");
1113
1114 if (condition)
1115 condition->print();
1116 printf("; ");
1117
1118 if (rest_expression)
1119 rest_expression->print();
1120 printf(") ");
1121
1122 body->print();
1123 break;
1124
1125 case ast_while:
1126 printf("while ( ");
1127 if (condition)
1128 condition->print();
1129 printf(") ");
1130 body->print();
1131 break;
1132
1133 case ast_do_while:
1134 printf("do ");
1135 body->print();
1136 printf("while ( ");
1137 if (condition)
1138 condition->print();
1139 printf("); ");
1140 break;
1141 }
1142 }
1143
1144
1145 ast_iteration_statement::ast_iteration_statement(int mode,
1146 ast_node *init,
1147 ast_node *condition,
1148 ast_expression *rest_expression,
1149 ast_node *body)
1150 {
1151 this->mode = ast_iteration_modes(mode);
1152 this->init_statement = init;
1153 this->condition = condition;
1154 this->rest_expression = rest_expression;
1155 this->body = body;
1156 }
1157
1158
1159 void
1160 ast_struct_specifier::print(void) const
1161 {
1162 printf("struct %s { ", name);
1163 foreach_list_const(n, &this->declarations) {
1164 ast_node *ast = exec_node_data(ast_node, n, link);
1165 ast->print();
1166 }
1167 printf("} ");
1168 }
1169
1170
1171 ast_struct_specifier::ast_struct_specifier(const char *identifier,
1172 ast_declarator_list *declarator_list)
1173 {
1174 if (identifier == NULL) {
1175 static unsigned anon_count = 1;
1176 identifier = ralloc_asprintf(this, "#anon_struct_%04x", anon_count);
1177 anon_count++;
1178 }
1179 name = identifier;
1180 this->declarations.push_degenerate_list_at_head(&declarator_list->link);
1181 }
1182
1183 /**
1184 * Do the set of common optimizations passes
1185 *
1186 * \param ir List of instructions to be optimized
1187 * \param linked Is the shader linked? This enables
1188 * optimizations passes that remove code at
1189 * global scope and could cause linking to
1190 * fail.
1191 * \param uniform_locations_assigned Have locations already been assigned for
1192 * uniforms? This prevents the declarations
1193 * of unused uniforms from being removed.
1194 * The setting of this flag only matters if
1195 * \c linked is \c true.
1196 * \param max_unroll_iterations Maximum number of loop iterations to be
1197 * unrolled. Setting to 0 forces all loops
1198 * to be unrolled.
1199 */
1200 bool
1201 do_common_optimization(exec_list *ir, bool linked,
1202 bool uniform_locations_assigned,
1203 unsigned max_unroll_iterations)
1204 {
1205 GLboolean progress = GL_FALSE;
1206
1207 progress = lower_instructions(ir, SUB_TO_ADD_NEG) || progress;
1208
1209 if (linked) {
1210 progress = do_function_inlining(ir) || progress;
1211 progress = do_dead_functions(ir) || progress;
1212 progress = do_structure_splitting(ir) || progress;
1213 }
1214 progress = do_if_simplification(ir) || progress;
1215 progress = do_copy_propagation(ir) || progress;
1216 progress = do_copy_propagation_elements(ir) || progress;
1217 if (linked)
1218 progress = do_dead_code(ir, uniform_locations_assigned) || progress;
1219 else
1220 progress = do_dead_code_unlinked(ir) || progress;
1221 progress = do_dead_code_local(ir) || progress;
1222 progress = do_tree_grafting(ir) || progress;
1223 progress = do_constant_propagation(ir) || progress;
1224 if (linked)
1225 progress = do_constant_variable(ir) || progress;
1226 else
1227 progress = do_constant_variable_unlinked(ir) || progress;
1228 progress = do_constant_folding(ir) || progress;
1229 progress = do_algebraic(ir) || progress;
1230 progress = do_lower_jumps(ir) || progress;
1231 progress = do_vec_index_to_swizzle(ir) || progress;
1232 progress = do_swizzle_swizzle(ir) || progress;
1233 progress = do_noop_swizzle(ir) || progress;
1234
1235 progress = optimize_split_arrays(ir, linked) || progress;
1236 progress = optimize_redundant_jumps(ir) || progress;
1237
1238 loop_state *ls = analyze_loop_variables(ir);
1239 if (ls->loop_found) {
1240 progress = set_loop_controls(ir, ls) || progress;
1241 progress = unroll_loops(ir, ls, max_unroll_iterations) || progress;
1242 }
1243 delete ls;
1244
1245 return progress;
1246 }
1247
1248 extern "C" {
1249
1250 /**
1251 * To be called at GL teardown time, this frees compiler datastructures.
1252 *
1253 * After calling this, any previously compiled shaders and shader
1254 * programs would be invalid. So this should happen at approximately
1255 * program exit.
1256 */
1257 void
1258 _mesa_destroy_shader_compiler(void)
1259 {
1260 _mesa_destroy_shader_compiler_caches();
1261
1262 _mesa_glsl_release_types();
1263 }
1264
1265 /**
1266 * Releases compiler caches to trade off performance for memory.
1267 *
1268 * Intended to be used with glReleaseShaderCompiler().
1269 */
1270 void
1271 _mesa_destroy_shader_compiler_caches(void)
1272 {
1273 _mesa_glsl_release_functions();
1274 }
1275
1276 }