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