glsl2: Perform initial bits of loop analysis during compilation
[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 <talloc.h>
30 #include "main/core.h" /* for struct __GLcontextRec */
31 }
32
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 __GLcontextRec *ctx,
40 GLenum target, void *mem_ctx)
41 {
42 switch (target) {
43 case GL_VERTEX_SHADER: this->target = vertex_shader; break;
44 case GL_FRAGMENT_SHADER: this->target = fragment_shader; break;
45 case GL_GEOMETRY_SHADER: this->target = geometry_shader; break;
46 }
47
48 this->scanner = NULL;
49 this->translation_unit.make_empty();
50 this->symbols = new(mem_ctx) glsl_symbol_table;
51 this->info_log = talloc_strdup(mem_ctx, "");
52 this->error = false;
53 this->loop_or_switch_nesting = NULL;
54 this->ARB_texture_rectangle_enable = true;
55
56 if (ctx != NULL) {
57 this->extensions = &ctx->Extensions;
58
59 this->Const.MaxLights = ctx->Const.MaxLights;
60 this->Const.MaxClipPlanes = ctx->Const.MaxClipPlanes;
61 this->Const.MaxTextureUnits = ctx->Const.MaxTextureUnits;
62 this->Const.MaxTextureCoords = ctx->Const.MaxTextureCoordUnits;
63 this->Const.MaxVertexAttribs = ctx->Const.VertexProgram.MaxAttribs;
64 this->Const.MaxVertexUniformComponents = ctx->Const.VertexProgram.MaxUniformComponents;
65 this->Const.MaxVaryingFloats = ctx->Const.MaxVarying * 4;
66 this->Const.MaxVertexTextureImageUnits = ctx->Const.MaxVertexTextureImageUnits;
67 this->Const.MaxCombinedTextureImageUnits = ctx->Const.MaxCombinedTextureImageUnits;
68 this->Const.MaxTextureImageUnits = ctx->Const.MaxTextureImageUnits;
69 this->Const.MaxFragmentUniformComponents = ctx->Const.FragmentProgram.MaxUniformComponents;
70
71 this->Const.MaxDrawBuffers = ctx->Const.MaxDrawBuffers;
72 } else {
73 /* If there is no GL context (standalone compiler), fill in constants
74 * with the minimum required values.
75 */
76 static struct gl_extensions null_extensions;
77
78 memset(&null_extensions, 0, sizeof(null_extensions));
79 null_extensions.ARB_draw_buffers = GL_TRUE;
80 null_extensions.ARB_fragment_coord_conventions = GL_TRUE;
81 null_extensions.EXT_texture_array = GL_TRUE;
82 null_extensions.NV_texture_rectangle = GL_TRUE;
83
84 this->extensions = &null_extensions;
85
86 /* 1.10 minimums. */
87 this->Const.MaxLights = 8;
88 this->Const.MaxClipPlanes = 8;
89 this->Const.MaxTextureUnits = 2;
90
91 /* More than the 1.10 minimum to appease parser tests taken from
92 * apps that (hopefully) already checked the number of coords.
93 */
94 this->Const.MaxTextureCoords = 4;
95
96 this->Const.MaxVertexAttribs = 16;
97 this->Const.MaxVertexUniformComponents = 512;
98 this->Const.MaxVaryingFloats = 32;
99 this->Const.MaxVertexTextureImageUnits = 0;
100 this->Const.MaxCombinedTextureImageUnits = 2;
101 this->Const.MaxTextureImageUnits = 2;
102 this->Const.MaxFragmentUniformComponents = 64;
103
104 this->Const.MaxDrawBuffers = 2;
105 }
106 }
107
108 const char *
109 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target)
110 {
111 switch (target) {
112 case vertex_shader: return "vertex";
113 case fragment_shader: return "fragment";
114 case geometry_shader: return "geometry";
115 case ir_shader: break;
116 }
117
118 assert(!"Should not get here.");
119 return "unknown";
120 }
121
122
123 void
124 _mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
125 const char *fmt, ...)
126 {
127 va_list ap;
128
129 state->error = true;
130
131 assert(state->info_log != NULL);
132 state->info_log = talloc_asprintf_append(state->info_log,
133 "%u:%u(%u): error: ",
134 locp->source,
135 locp->first_line,
136 locp->first_column);
137 va_start(ap, fmt);
138 state->info_log = talloc_vasprintf_append(state->info_log, fmt, ap);
139 va_end(ap);
140 state->info_log = talloc_strdup_append(state->info_log, "\n");
141 }
142
143
144 void
145 _mesa_glsl_warning(const YYLTYPE *locp, _mesa_glsl_parse_state *state,
146 const char *fmt, ...)
147 {
148 va_list ap;
149
150 assert(state->info_log != NULL);
151 state->info_log = talloc_asprintf_append(state->info_log,
152 "%u:%u(%u): warning: ",
153 locp->source,
154 locp->first_line,
155 locp->first_column);
156 va_start(ap, fmt);
157 state->info_log = talloc_vasprintf_append(state->info_log, fmt, ap);
158 va_end(ap);
159 state->info_log = talloc_strdup_append(state->info_log, "\n");
160 }
161
162
163 bool
164 _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
165 const char *behavior, YYLTYPE *behavior_locp,
166 _mesa_glsl_parse_state *state)
167 {
168 enum {
169 extension_disable,
170 extension_enable,
171 extension_require,
172 extension_warn
173 } ext_mode;
174
175 if (strcmp(behavior, "warn") == 0) {
176 ext_mode = extension_warn;
177 } else if (strcmp(behavior, "require") == 0) {
178 ext_mode = extension_require;
179 } else if (strcmp(behavior, "enable") == 0) {
180 ext_mode = extension_enable;
181 } else if (strcmp(behavior, "disable") == 0) {
182 ext_mode = extension_disable;
183 } else {
184 _mesa_glsl_error(behavior_locp, state,
185 "Unknown extension behavior `%s'",
186 behavior);
187 return false;
188 }
189
190 bool unsupported = false;
191
192 if (strcmp(name, "all") == 0) {
193 if ((ext_mode == extension_enable) || (ext_mode == extension_require)) {
194 _mesa_glsl_error(name_locp, state, "Cannot %s all extensions",
195 (ext_mode == extension_enable)
196 ? "enable" : "require");
197 return false;
198 }
199 } else if (strcmp(name, "GL_ARB_draw_buffers") == 0) {
200 /* This extension is only supported in fragment shaders.
201 */
202 if (state->target != fragment_shader) {
203 unsupported = true;
204 } else {
205 state->ARB_draw_buffers_enable = (ext_mode != extension_disable);
206 state->ARB_draw_buffers_warn = (ext_mode == extension_warn);
207 }
208 } else if (strcmp(name, "GL_ARB_fragment_coord_conventions") == 0) {
209 state->ARB_fragment_coord_conventions_enable =
210 (ext_mode != extension_disable);
211 state->ARB_fragment_coord_conventions_warn =
212 (ext_mode == extension_warn);
213
214 unsupported = !state->extensions->ARB_fragment_coord_conventions;
215 } else if (strcmp(name, "GL_ARB_texture_rectangle") == 0) {
216 state->ARB_texture_rectangle_enable = (ext_mode != extension_disable);
217 state->ARB_texture_rectangle_warn = (ext_mode == extension_warn);
218 } else if (strcmp(name, "GL_EXT_texture_array") == 0) {
219 state->EXT_texture_array_enable = (ext_mode != extension_disable);
220 state->EXT_texture_array_warn = (ext_mode == extension_warn);
221
222 unsupported = !state->extensions->EXT_texture_array;
223 } else {
224 unsupported = true;
225 }
226
227 if (unsupported) {
228 static const char *const fmt = "extension `%s' unsupported in %s shader";
229
230 if (ext_mode == extension_require) {
231 _mesa_glsl_error(name_locp, state, fmt,
232 name, _mesa_glsl_shader_target_name(state->target));
233 return false;
234 } else {
235 _mesa_glsl_warning(name_locp, state, fmt,
236 name, _mesa_glsl_shader_target_name(state->target));
237 }
238 }
239
240 return true;
241 }
242
243 void
244 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q)
245 {
246 if (q->constant)
247 printf("const ");
248
249 if (q->invariant)
250 printf("invariant ");
251
252 if (q->attribute)
253 printf("attribute ");
254
255 if (q->varying)
256 printf("varying ");
257
258 if (q->in && q->out)
259 printf("inout ");
260 else {
261 if (q->in)
262 printf("in ");
263
264 if (q->out)
265 printf("out ");
266 }
267
268 if (q->centroid)
269 printf("centroid ");
270 if (q->uniform)
271 printf("uniform ");
272 if (q->smooth)
273 printf("smooth ");
274 if (q->flat)
275 printf("flat ");
276 if (q->noperspective)
277 printf("noperspective ");
278 }
279
280
281 void
282 ast_node::print(void) const
283 {
284 printf("unhandled node ");
285 }
286
287
288 ast_node::ast_node(void)
289 {
290 this->location.source = 0;
291 this->location.line = 0;
292 this->location.column = 0;
293 }
294
295
296 static void
297 ast_opt_array_size_print(bool is_array, const ast_expression *array_size)
298 {
299 if (is_array) {
300 printf("[ ");
301
302 if (array_size)
303 array_size->print();
304
305 printf("] ");
306 }
307 }
308
309
310 void
311 ast_compound_statement::print(void) const
312 {
313 printf("{\n");
314
315 foreach_list_const(n, &this->statements) {
316 ast_node *ast = exec_node_data(ast_node, n, link);
317 ast->print();
318 }
319
320 printf("}\n");
321 }
322
323
324 ast_compound_statement::ast_compound_statement(int new_scope,
325 ast_node *statements)
326 {
327 this->new_scope = new_scope;
328
329 if (statements != NULL) {
330 this->statements.push_degenerate_list_at_head(&statements->link);
331 }
332 }
333
334
335 void
336 ast_expression::print(void) const
337 {
338 switch (oper) {
339 case ast_assign:
340 case ast_mul_assign:
341 case ast_div_assign:
342 case ast_mod_assign:
343 case ast_add_assign:
344 case ast_sub_assign:
345 case ast_ls_assign:
346 case ast_rs_assign:
347 case ast_and_assign:
348 case ast_xor_assign:
349 case ast_or_assign:
350 subexpressions[0]->print();
351 printf("%s ", operator_string(oper));
352 subexpressions[1]->print();
353 break;
354
355 case ast_field_selection:
356 subexpressions[0]->print();
357 printf(". %s ", primary_expression.identifier);
358 break;
359
360 case ast_plus:
361 case ast_neg:
362 case ast_bit_not:
363 case ast_logic_not:
364 case ast_pre_inc:
365 case ast_pre_dec:
366 printf("%s ", operator_string(oper));
367 subexpressions[0]->print();
368 break;
369
370 case ast_post_inc:
371 case ast_post_dec:
372 subexpressions[0]->print();
373 printf("%s ", operator_string(oper));
374 break;
375
376 case ast_conditional:
377 subexpressions[0]->print();
378 printf("? ");
379 subexpressions[1]->print();
380 printf(": ");
381 subexpressions[1]->print();
382 break;
383
384 case ast_array_index:
385 subexpressions[0]->print();
386 printf("[ ");
387 subexpressions[1]->print();
388 printf("] ");
389 break;
390
391 case ast_function_call: {
392 subexpressions[0]->print();
393 printf("( ");
394
395 foreach_list_const (n, &this->expressions) {
396 if (n != this->expressions.get_head())
397 printf(", ");
398
399 ast_node *ast = exec_node_data(ast_node, n, link);
400 ast->print();
401 }
402
403 printf(") ");
404 break;
405 }
406
407 case ast_identifier:
408 printf("%s ", primary_expression.identifier);
409 break;
410
411 case ast_int_constant:
412 printf("%d ", primary_expression.int_constant);
413 break;
414
415 case ast_uint_constant:
416 printf("%u ", primary_expression.uint_constant);
417 break;
418
419 case ast_float_constant:
420 printf("%f ", primary_expression.float_constant);
421 break;
422
423 case ast_bool_constant:
424 printf("%s ",
425 primary_expression.bool_constant
426 ? "true" : "false");
427 break;
428
429 case ast_sequence: {
430 printf("( ");
431 foreach_list_const(n, & this->expressions) {
432 if (n != this->expressions.get_head())
433 printf(", ");
434
435 ast_node *ast = exec_node_data(ast_node, n, link);
436 ast->print();
437 }
438 printf(") ");
439 break;
440 }
441
442 default:
443 assert(0);
444 break;
445 }
446 }
447
448 ast_expression::ast_expression(int oper,
449 ast_expression *ex0,
450 ast_expression *ex1,
451 ast_expression *ex2)
452 {
453 this->oper = ast_operators(oper);
454 this->subexpressions[0] = ex0;
455 this->subexpressions[1] = ex1;
456 this->subexpressions[2] = ex2;
457 }
458
459
460 void
461 ast_expression_statement::print(void) const
462 {
463 if (expression)
464 expression->print();
465
466 printf("; ");
467 }
468
469
470 ast_expression_statement::ast_expression_statement(ast_expression *ex) :
471 expression(ex)
472 {
473 /* empty */
474 }
475
476
477 void
478 ast_function::print(void) const
479 {
480 return_type->print();
481 printf(" %s (", identifier);
482
483 foreach_list_const(n, & this->parameters) {
484 ast_node *ast = exec_node_data(ast_node, n, link);
485 ast->print();
486 }
487
488 printf(")");
489 }
490
491
492 ast_function::ast_function(void)
493 : is_definition(false), signature(NULL)
494 {
495 /* empty */
496 }
497
498
499 void
500 ast_fully_specified_type::print(void) const
501 {
502 _mesa_ast_type_qualifier_print(& qualifier);
503 specifier->print();
504 }
505
506
507 void
508 ast_parameter_declarator::print(void) const
509 {
510 type->print();
511 if (identifier)
512 printf("%s ", identifier);
513 ast_opt_array_size_print(is_array, array_size);
514 }
515
516
517 void
518 ast_function_definition::print(void) const
519 {
520 prototype->print();
521 body->print();
522 }
523
524
525 void
526 ast_declaration::print(void) const
527 {
528 printf("%s ", identifier);
529 ast_opt_array_size_print(is_array, array_size);
530
531 if (initializer) {
532 printf("= ");
533 initializer->print();
534 }
535 }
536
537
538 ast_declaration::ast_declaration(char *identifier, int is_array,
539 ast_expression *array_size,
540 ast_expression *initializer)
541 {
542 this->identifier = identifier;
543 this->is_array = is_array;
544 this->array_size = array_size;
545 this->initializer = initializer;
546 }
547
548
549 void
550 ast_declarator_list::print(void) const
551 {
552 assert(type || invariant);
553
554 if (type)
555 type->print();
556 else
557 printf("invariant ");
558
559 foreach_list_const (ptr, & this->declarations) {
560 if (ptr != this->declarations.get_head())
561 printf(", ");
562
563 ast_node *ast = exec_node_data(ast_node, ptr, link);
564 ast->print();
565 }
566
567 printf("; ");
568 }
569
570
571 ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type)
572 {
573 this->type = type;
574 this->invariant = false;
575 }
576
577 void
578 ast_jump_statement::print(void) const
579 {
580 switch (mode) {
581 case ast_continue:
582 printf("continue; ");
583 break;
584 case ast_break:
585 printf("break; ");
586 break;
587 case ast_return:
588 printf("return ");
589 if (opt_return_value)
590 opt_return_value->print();
591
592 printf("; ");
593 break;
594 case ast_discard:
595 printf("discard; ");
596 break;
597 }
598 }
599
600
601 ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value)
602 {
603 this->mode = ast_jump_modes(mode);
604
605 if (mode == ast_return)
606 opt_return_value = return_value;
607 }
608
609
610 void
611 ast_selection_statement::print(void) const
612 {
613 printf("if ( ");
614 condition->print();
615 printf(") ");
616
617 then_statement->print();
618
619 if (else_statement) {
620 printf("else ");
621 else_statement->print();
622 }
623
624 }
625
626
627 ast_selection_statement::ast_selection_statement(ast_expression *condition,
628 ast_node *then_statement,
629 ast_node *else_statement)
630 {
631 this->condition = condition;
632 this->then_statement = then_statement;
633 this->else_statement = else_statement;
634 }
635
636
637 void
638 ast_iteration_statement::print(void) const
639 {
640 switch (mode) {
641 case ast_for:
642 printf("for( ");
643 if (init_statement)
644 init_statement->print();
645 printf("; ");
646
647 if (condition)
648 condition->print();
649 printf("; ");
650
651 if (rest_expression)
652 rest_expression->print();
653 printf(") ");
654
655 body->print();
656 break;
657
658 case ast_while:
659 printf("while ( ");
660 if (condition)
661 condition->print();
662 printf(") ");
663 body->print();
664 break;
665
666 case ast_do_while:
667 printf("do ");
668 body->print();
669 printf("while ( ");
670 if (condition)
671 condition->print();
672 printf("); ");
673 break;
674 }
675 }
676
677
678 ast_iteration_statement::ast_iteration_statement(int mode,
679 ast_node *init,
680 ast_node *condition,
681 ast_expression *rest_expression,
682 ast_node *body)
683 {
684 this->mode = ast_iteration_modes(mode);
685 this->init_statement = init;
686 this->condition = condition;
687 this->rest_expression = rest_expression;
688 this->body = body;
689 }
690
691
692 void
693 ast_struct_specifier::print(void) const
694 {
695 printf("struct %s { ", name);
696 foreach_list_const(n, &this->declarations) {
697 ast_node *ast = exec_node_data(ast_node, n, link);
698 ast->print();
699 }
700 printf("} ");
701 }
702
703
704 ast_struct_specifier::ast_struct_specifier(char *identifier,
705 ast_node *declarator_list)
706 {
707 name = identifier;
708 this->declarations.push_degenerate_list_at_head(&declarator_list->link);
709 }
710
711 bool
712 do_common_optimization(exec_list *ir, bool linked)
713 {
714 GLboolean progress = GL_FALSE;
715
716 progress = do_sub_to_add_neg(ir) || progress;
717
718 if (linked) {
719 progress = do_function_inlining(ir) || progress;
720 progress = do_dead_functions(ir) || progress;
721 }
722 progress = do_structure_splitting(ir) || progress;
723 progress = do_if_simplification(ir) || progress;
724 progress = do_copy_propagation(ir) || progress;
725 if (linked)
726 progress = do_dead_code(ir) || progress;
727 else
728 progress = do_dead_code_unlinked(ir) || progress;
729 progress = do_dead_code_local(ir) || progress;
730 progress = do_tree_grafting(ir) || progress;
731 progress = do_constant_propagation(ir) || progress;
732 if (linked)
733 progress = do_constant_variable(ir) || progress;
734 else
735 progress = do_constant_variable_unlinked(ir) || progress;
736 progress = do_constant_folding(ir) || progress;
737 progress = do_algebraic(ir) || progress;
738 progress = do_if_return(ir) || progress;
739 progress = do_vec_index_to_swizzle(ir) || progress;
740 progress = do_swizzle_swizzle(ir) || progress;
741 progress = do_noop_swizzle(ir) || progress;
742
743 loop_state *ls = analyze_loop_variables(ir);
744 progress = set_loop_controls(ir, ls) || progress;
745 delete ls;
746
747 return progress;
748 }
749
750 extern "C" {
751
752 /**
753 * To be called at GL teardown time, this frees compiler datastructures.
754 *
755 * After calling this, any previously compiled shaders and shader
756 * programs would be invalid. So this should happen at approximately
757 * program exit.
758 */
759 void
760 _mesa_destroy_shader_compiler(void)
761 {
762 _mesa_destroy_shader_compiler_caches();
763
764 _mesa_glsl_release_types();
765 }
766
767 /**
768 * Releases compiler caches to trade off performance for memory.
769 *
770 * Intended to be used with glReleaseShaderCompiler().
771 */
772 void
773 _mesa_destroy_shader_compiler_caches(void)
774 {
775 _mesa_glsl_release_functions();
776 }
777
778 }