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