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