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