r600g: Add support for RATs in evergreen_cb()
[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 };
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 }
776
777 void
778 ast_jump_statement::print(void) const
779 {
780 switch (mode) {
781 case ast_continue:
782 printf("continue; ");
783 break;
784 case ast_break:
785 printf("break; ");
786 break;
787 case ast_return:
788 printf("return ");
789 if (opt_return_value)
790 opt_return_value->print();
791
792 printf("; ");
793 break;
794 case ast_discard:
795 printf("discard; ");
796 break;
797 }
798 }
799
800
801 ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value)
802 {
803 this->mode = ast_jump_modes(mode);
804
805 if (mode == ast_return)
806 opt_return_value = return_value;
807 }
808
809
810 void
811 ast_selection_statement::print(void) const
812 {
813 printf("if ( ");
814 condition->print();
815 printf(") ");
816
817 then_statement->print();
818
819 if (else_statement) {
820 printf("else ");
821 else_statement->print();
822 }
823
824 }
825
826
827 ast_selection_statement::ast_selection_statement(ast_expression *condition,
828 ast_node *then_statement,
829 ast_node *else_statement)
830 {
831 this->condition = condition;
832 this->then_statement = then_statement;
833 this->else_statement = else_statement;
834 }
835
836
837 void
838 ast_switch_statement::print(void) const
839 {
840 printf("switch ( ");
841 test_expression->print();
842 printf(") ");
843
844 body->print();
845 }
846
847
848 ast_switch_statement::ast_switch_statement(ast_expression *test_expression,
849 ast_node *body)
850 {
851 this->test_expression = test_expression;
852 this->body = body;
853 }
854
855
856 void
857 ast_switch_body::print(void) const
858 {
859 printf("{\n");
860 if (stmts != NULL) {
861 stmts->print();
862 }
863 printf("}\n");
864 }
865
866
867 ast_switch_body::ast_switch_body(ast_case_statement_list *stmts)
868 {
869 this->stmts = stmts;
870 }
871
872
873 void ast_case_label::print(void) const
874 {
875 if (test_value != NULL) {
876 printf("case ");
877 test_value->print();
878 printf(": ");
879 } else {
880 printf("default: ");
881 }
882 }
883
884
885 ast_case_label::ast_case_label(ast_expression *test_value)
886 {
887 this->test_value = test_value;
888 }
889
890
891 void ast_case_label_list::print(void) const
892 {
893 foreach_list_const(n, & this->labels) {
894 ast_node *ast = exec_node_data(ast_node, n, link);
895 ast->print();
896 }
897 printf("\n");
898 }
899
900
901 ast_case_label_list::ast_case_label_list(void)
902 {
903 }
904
905
906 void ast_case_statement::print(void) const
907 {
908 labels->print();
909 foreach_list_const(n, & this->stmts) {
910 ast_node *ast = exec_node_data(ast_node, n, link);
911 ast->print();
912 printf("\n");
913 }
914 }
915
916
917 ast_case_statement::ast_case_statement(ast_case_label_list *labels)
918 {
919 this->labels = labels;
920 }
921
922
923 void ast_case_statement_list::print(void) const
924 {
925 foreach_list_const(n, & this->cases) {
926 ast_node *ast = exec_node_data(ast_node, n, link);
927 ast->print();
928 }
929 }
930
931
932 ast_case_statement_list::ast_case_statement_list(void)
933 {
934 }
935
936
937 void
938 ast_iteration_statement::print(void) const
939 {
940 switch (mode) {
941 case ast_for:
942 printf("for( ");
943 if (init_statement)
944 init_statement->print();
945 printf("; ");
946
947 if (condition)
948 condition->print();
949 printf("; ");
950
951 if (rest_expression)
952 rest_expression->print();
953 printf(") ");
954
955 body->print();
956 break;
957
958 case ast_while:
959 printf("while ( ");
960 if (condition)
961 condition->print();
962 printf(") ");
963 body->print();
964 break;
965
966 case ast_do_while:
967 printf("do ");
968 body->print();
969 printf("while ( ");
970 if (condition)
971 condition->print();
972 printf("); ");
973 break;
974 }
975 }
976
977
978 ast_iteration_statement::ast_iteration_statement(int mode,
979 ast_node *init,
980 ast_node *condition,
981 ast_expression *rest_expression,
982 ast_node *body)
983 {
984 this->mode = ast_iteration_modes(mode);
985 this->init_statement = init;
986 this->condition = condition;
987 this->rest_expression = rest_expression;
988 this->body = body;
989 }
990
991
992 void
993 ast_struct_specifier::print(void) const
994 {
995 printf("struct %s { ", name);
996 foreach_list_const(n, &this->declarations) {
997 ast_node *ast = exec_node_data(ast_node, n, link);
998 ast->print();
999 }
1000 printf("} ");
1001 }
1002
1003
1004 ast_struct_specifier::ast_struct_specifier(const char *identifier,
1005 ast_node *declarator_list)
1006 {
1007 if (identifier == NULL) {
1008 static unsigned anon_count = 1;
1009 identifier = ralloc_asprintf(this, "#anon_struct_%04x", anon_count);
1010 anon_count++;
1011 }
1012 name = identifier;
1013 this->declarations.push_degenerate_list_at_head(&declarator_list->link);
1014 }
1015
1016 /**
1017 * Do the set of common optimizations passes
1018 *
1019 * \param ir List of instructions to be optimized
1020 * \param linked Is the shader linked? This enables
1021 * optimizations passes that remove code at
1022 * global scope and could cause linking to
1023 * fail.
1024 * \param uniform_locations_assigned Have locations already been assigned for
1025 * uniforms? This prevents the declarations
1026 * of unused uniforms from being removed.
1027 * The setting of this flag only matters if
1028 * \c linked is \c true.
1029 * \param max_unroll_iterations Maximum number of loop iterations to be
1030 * unrolled. Setting to 0 forces all loops
1031 * to be unrolled.
1032 */
1033 bool
1034 do_common_optimization(exec_list *ir, bool linked,
1035 bool uniform_locations_assigned,
1036 unsigned max_unroll_iterations)
1037 {
1038 GLboolean progress = GL_FALSE;
1039
1040 progress = lower_instructions(ir, SUB_TO_ADD_NEG) || progress;
1041
1042 if (linked) {
1043 progress = do_function_inlining(ir) || progress;
1044 progress = do_dead_functions(ir) || progress;
1045 progress = do_structure_splitting(ir) || progress;
1046 }
1047 progress = do_if_simplification(ir) || progress;
1048 progress = do_copy_propagation(ir) || progress;
1049 progress = do_copy_propagation_elements(ir) || progress;
1050 if (linked)
1051 progress = do_dead_code(ir, uniform_locations_assigned) || progress;
1052 else
1053 progress = do_dead_code_unlinked(ir) || progress;
1054 progress = do_dead_code_local(ir) || progress;
1055 progress = do_tree_grafting(ir) || progress;
1056 progress = do_constant_propagation(ir) || progress;
1057 if (linked)
1058 progress = do_constant_variable(ir) || progress;
1059 else
1060 progress = do_constant_variable_unlinked(ir) || progress;
1061 progress = do_constant_folding(ir) || progress;
1062 progress = do_algebraic(ir) || progress;
1063 progress = do_lower_jumps(ir) || progress;
1064 progress = do_vec_index_to_swizzle(ir) || progress;
1065 progress = do_swizzle_swizzle(ir) || progress;
1066 progress = do_noop_swizzle(ir) || progress;
1067
1068 progress = optimize_split_arrays(ir, linked) || progress;
1069 progress = optimize_redundant_jumps(ir) || progress;
1070
1071 loop_state *ls = analyze_loop_variables(ir);
1072 if (ls->loop_found) {
1073 progress = set_loop_controls(ir, ls) || progress;
1074 progress = unroll_loops(ir, ls, max_unroll_iterations) || progress;
1075 }
1076 delete ls;
1077
1078 return progress;
1079 }
1080
1081 extern "C" {
1082
1083 /**
1084 * To be called at GL teardown time, this frees compiler datastructures.
1085 *
1086 * After calling this, any previously compiled shaders and shader
1087 * programs would be invalid. So this should happen at approximately
1088 * program exit.
1089 */
1090 void
1091 _mesa_destroy_shader_compiler(void)
1092 {
1093 _mesa_destroy_shader_compiler_caches();
1094
1095 _mesa_glsl_release_types();
1096 }
1097
1098 /**
1099 * Releases compiler caches to trade off performance for memory.
1100 *
1101 * Intended to be used with glReleaseShaderCompiler().
1102 */
1103 void
1104 _mesa_destroy_shader_compiler_caches(void)
1105 {
1106 _mesa_glsl_release_functions();
1107 }
1108
1109 }