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