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