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