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