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