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