2 * Copyright © 2008, 2009 Intel Corporation
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:
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
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.
30 #include "main/mtypes.h"
34 #include "glsl_parser_extras.h"
35 #include "glsl_parser.h"
36 #include "ir_optimization.h"
38 _mesa_glsl_parse_state::_mesa_glsl_parse_state(struct __GLcontextRec
*ctx
,
39 GLenum target
, void *mem_ctx
)
42 case GL_VERTEX_SHADER
: this->target
= vertex_shader
; break;
43 case GL_FRAGMENT_SHADER
: this->target
= fragment_shader
; break;
44 case GL_GEOMETRY_SHADER
: this->target
= geometry_shader
; break;
48 this->translation_unit
.make_empty();
49 this->symbols
= new(mem_ctx
) glsl_symbol_table
;
50 this->info_log
= talloc_strdup(mem_ctx
, "");
52 this->loop_or_switch_nesting
= NULL
;
53 this->ARB_texture_rectangle_enable
= true;
56 this->extensions
= &ctx
->Extensions
;
58 this->Const
.MaxLights
= ctx
->Const
.MaxLights
;
59 this->Const
.MaxClipPlanes
= ctx
->Const
.MaxClipPlanes
;
60 this->Const
.MaxTextureUnits
= ctx
->Const
.MaxTextureUnits
;
61 this->Const
.MaxTextureCoords
= ctx
->Const
.MaxTextureCoordUnits
;
62 this->Const
.MaxVertexAttribs
= ctx
->Const
.VertexProgram
.MaxAttribs
;
63 this->Const
.MaxVertexUniformComponents
= ctx
->Const
.VertexProgram
.MaxUniformComponents
;
64 this->Const
.MaxVaryingFloats
= ctx
->Const
.MaxVarying
* 4;
65 this->Const
.MaxVertexTextureImageUnits
= ctx
->Const
.MaxVertexTextureImageUnits
;
66 this->Const
.MaxCombinedTextureImageUnits
= ctx
->Const
.MaxCombinedTextureImageUnits
;
67 this->Const
.MaxTextureImageUnits
= ctx
->Const
.MaxTextureImageUnits
;
68 this->Const
.MaxFragmentUniformComponents
= ctx
->Const
.FragmentProgram
.MaxUniformComponents
;
70 this->Const
.MaxDrawBuffers
= ctx
->Const
.MaxDrawBuffers
;
72 /* If there is no GL context (standalone compiler), fill in constants
73 * with the minimum required values.
75 static struct gl_extensions null_extensions
;
77 memset(&null_extensions
, 0, sizeof(null_extensions
));
78 null_extensions
.ARB_draw_buffers
= GL_TRUE
;
79 null_extensions
.ARB_fragment_coord_conventions
= GL_TRUE
;
80 null_extensions
.EXT_texture_array
= GL_TRUE
;
81 null_extensions
.NV_texture_rectangle
= GL_TRUE
;
83 this->extensions
= &null_extensions
;
86 this->Const
.MaxLights
= 8;
87 this->Const
.MaxClipPlanes
= 8;
88 this->Const
.MaxTextureUnits
= 2;
90 /* More than the 1.10 minimum to appease parser tests taken from
91 * apps that (hopefully) already checked the number of coords.
93 this->Const
.MaxTextureCoords
= 4;
95 this->Const
.MaxVertexAttribs
= 16;
96 this->Const
.MaxVertexUniformComponents
= 512;
97 this->Const
.MaxVaryingFloats
= 32;
98 this->Const
.MaxVertexTextureImageUnits
= 0;
99 this->Const
.MaxCombinedTextureImageUnits
= 2;
100 this->Const
.MaxTextureImageUnits
= 2;
101 this->Const
.MaxFragmentUniformComponents
= 64;
103 this->Const
.MaxDrawBuffers
= 2;
108 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target
)
111 case vertex_shader
: return "vertex";
112 case fragment_shader
: return "fragment";
113 case geometry_shader
: return "geometry";
114 case ir_shader
: break;
117 assert(!"Should not get here.");
123 _mesa_glsl_error(YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
124 const char *fmt
, ...)
130 assert(state
->info_log
!= NULL
);
131 state
->info_log
= talloc_asprintf_append(state
->info_log
,
132 "%u:%u(%u): error: ",
137 state
->info_log
= talloc_vasprintf_append(state
->info_log
, fmt
, ap
);
139 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n");
144 _mesa_glsl_warning(const YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
145 const char *fmt
, ...)
149 assert(state
->info_log
!= NULL
);
150 state
->info_log
= talloc_asprintf_append(state
->info_log
,
151 "%u:%u(%u): warning: ",
156 state
->info_log
= talloc_vasprintf_append(state
->info_log
, fmt
, ap
);
158 state
->info_log
= talloc_strdup_append(state
->info_log
, "\n");
163 _mesa_glsl_process_extension(const char *name
, YYLTYPE
*name_locp
,
164 const char *behavior
, YYLTYPE
*behavior_locp
,
165 _mesa_glsl_parse_state
*state
)
174 if (strcmp(behavior
, "warn") == 0) {
175 ext_mode
= extension_warn
;
176 } else if (strcmp(behavior
, "require") == 0) {
177 ext_mode
= extension_require
;
178 } else if (strcmp(behavior
, "enable") == 0) {
179 ext_mode
= extension_enable
;
180 } else if (strcmp(behavior
, "disable") == 0) {
181 ext_mode
= extension_disable
;
183 _mesa_glsl_error(behavior_locp
, state
,
184 "Unknown extension behavior `%s'",
189 bool unsupported
= false;
191 if (strcmp(name
, "all") == 0) {
192 if ((ext_mode
== extension_enable
) || (ext_mode
== extension_require
)) {
193 _mesa_glsl_error(name_locp
, state
, "Cannot %s all extensions",
194 (ext_mode
== extension_enable
)
195 ? "enable" : "require");
198 } else if (strcmp(name
, "GL_ARB_draw_buffers") == 0) {
199 /* This extension is only supported in fragment shaders.
201 if (state
->target
!= fragment_shader
) {
204 state
->ARB_draw_buffers_enable
= (ext_mode
!= extension_disable
);
205 state
->ARB_draw_buffers_warn
= (ext_mode
== extension_warn
);
207 } else if (strcmp(name
, "GL_ARB_fragment_coord_conventions") == 0) {
208 state
->ARB_fragment_coord_conventions_enable
=
209 (ext_mode
!= extension_disable
);
210 state
->ARB_fragment_coord_conventions_warn
=
211 (ext_mode
== extension_warn
);
213 unsupported
= !state
->extensions
->ARB_fragment_coord_conventions
;
214 } else if (strcmp(name
, "GL_ARB_texture_rectangle") == 0) {
215 state
->ARB_texture_rectangle_enable
= (ext_mode
!= extension_disable
);
216 state
->ARB_texture_rectangle_warn
= (ext_mode
== extension_warn
);
217 } else if (strcmp(name
, "GL_EXT_texture_array") == 0) {
218 state
->EXT_texture_array_enable
= (ext_mode
!= extension_disable
);
219 state
->EXT_texture_array_warn
= (ext_mode
== extension_warn
);
221 unsupported
= !state
->extensions
->EXT_texture_array
;
227 static const char *const fmt
= "extension `%s' unsupported in %s shader";
229 if (ext_mode
== extension_require
) {
230 _mesa_glsl_error(name_locp
, state
, fmt
,
231 name
, _mesa_glsl_shader_target_name(state
->target
));
234 _mesa_glsl_warning(name_locp
, state
, fmt
,
235 name
, _mesa_glsl_shader_target_name(state
->target
));
243 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier
*q
)
249 printf("invariant ");
252 printf("attribute ");
275 if (q
->noperspective
)
276 printf("noperspective ");
281 ast_node::print(void) const
283 printf("unhandled node ");
287 ast_node::ast_node(void)
289 this->location
.source
= 0;
290 this->location
.line
= 0;
291 this->location
.column
= 0;
296 ast_opt_array_size_print(bool is_array
, const ast_expression
*array_size
)
310 ast_compound_statement::print(void) const
314 foreach_list_const(n
, &this->statements
) {
315 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
323 ast_compound_statement::ast_compound_statement(int new_scope
,
324 ast_node
*statements
)
326 this->new_scope
= new_scope
;
328 if (statements
!= NULL
) {
329 this->statements
.push_degenerate_list_at_head(&statements
->link
);
335 ast_expression::print(void) const
349 subexpressions
[0]->print();
350 printf("%s ", operator_string(oper
));
351 subexpressions
[1]->print();
354 case ast_field_selection
:
355 subexpressions
[0]->print();
356 printf(". %s ", primary_expression
.identifier
);
365 printf("%s ", operator_string(oper
));
366 subexpressions
[0]->print();
371 subexpressions
[0]->print();
372 printf("%s ", operator_string(oper
));
375 case ast_conditional
:
376 subexpressions
[0]->print();
378 subexpressions
[1]->print();
380 subexpressions
[1]->print();
383 case ast_array_index
:
384 subexpressions
[0]->print();
386 subexpressions
[1]->print();
390 case ast_function_call
: {
391 subexpressions
[0]->print();
394 foreach_list_const (n
, &this->expressions
) {
395 if (n
!= this->expressions
.get_head())
398 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
407 printf("%s ", primary_expression
.identifier
);
410 case ast_int_constant
:
411 printf("%d ", primary_expression
.int_constant
);
414 case ast_uint_constant
:
415 printf("%u ", primary_expression
.uint_constant
);
418 case ast_float_constant
:
419 printf("%f ", primary_expression
.float_constant
);
422 case ast_bool_constant
:
424 primary_expression
.bool_constant
430 foreach_list_const(n
, & this->expressions
) {
431 if (n
!= this->expressions
.get_head())
434 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
447 ast_expression::ast_expression(int oper
,
452 this->oper
= ast_operators(oper
);
453 this->subexpressions
[0] = ex0
;
454 this->subexpressions
[1] = ex1
;
455 this->subexpressions
[2] = ex2
;
460 ast_expression_statement::print(void) const
469 ast_expression_statement::ast_expression_statement(ast_expression
*ex
) :
477 ast_function::print(void) const
479 return_type
->print();
480 printf(" %s (", identifier
);
482 foreach_list_const(n
, & this->parameters
) {
483 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
491 ast_function::ast_function(void)
492 : is_definition(false), signature(NULL
)
499 ast_fully_specified_type::print(void) const
501 _mesa_ast_type_qualifier_print(& qualifier
);
507 ast_parameter_declarator::print(void) const
511 printf("%s ", identifier
);
512 ast_opt_array_size_print(is_array
, array_size
);
517 ast_function_definition::print(void) const
525 ast_declaration::print(void) const
527 printf("%s ", identifier
);
528 ast_opt_array_size_print(is_array
, array_size
);
532 initializer
->print();
537 ast_declaration::ast_declaration(char *identifier
, int is_array
,
538 ast_expression
*array_size
,
539 ast_expression
*initializer
)
541 this->identifier
= identifier
;
542 this->is_array
= is_array
;
543 this->array_size
= array_size
;
544 this->initializer
= initializer
;
549 ast_declarator_list::print(void) const
551 assert(type
|| invariant
);
556 printf("invariant ");
558 foreach_list_const (ptr
, & this->declarations
) {
559 if (ptr
!= this->declarations
.get_head())
562 ast_node
*ast
= exec_node_data(ast_node
, ptr
, link
);
570 ast_declarator_list::ast_declarator_list(ast_fully_specified_type
*type
)
573 this->invariant
= false;
577 ast_jump_statement::print(void) const
581 printf("continue; ");
588 if (opt_return_value
)
589 opt_return_value
->print();
600 ast_jump_statement::ast_jump_statement(int mode
, ast_expression
*return_value
)
602 this->mode
= ast_jump_modes(mode
);
604 if (mode
== ast_return
)
605 opt_return_value
= return_value
;
610 ast_selection_statement::print(void) const
616 then_statement
->print();
618 if (else_statement
) {
620 else_statement
->print();
626 ast_selection_statement::ast_selection_statement(ast_expression
*condition
,
627 ast_node
*then_statement
,
628 ast_node
*else_statement
)
630 this->condition
= condition
;
631 this->then_statement
= then_statement
;
632 this->else_statement
= else_statement
;
637 ast_iteration_statement::print(void) const
643 init_statement
->print();
651 rest_expression
->print();
677 ast_iteration_statement::ast_iteration_statement(int mode
,
680 ast_expression
*rest_expression
,
683 this->mode
= ast_iteration_modes(mode
);
684 this->init_statement
= init
;
685 this->condition
= condition
;
686 this->rest_expression
= rest_expression
;
692 ast_struct_specifier::print(void) const
694 printf("struct %s { ", name
);
695 foreach_list_const(n
, &this->declarations
) {
696 ast_node
*ast
= exec_node_data(ast_node
, n
, link
);
703 ast_struct_specifier::ast_struct_specifier(char *identifier
,
704 ast_node
*declarator_list
)
707 this->declarations
.push_degenerate_list_at_head(&declarator_list
->link
);
711 do_common_optimization(exec_list
*ir
, bool linked
)
713 GLboolean progress
= GL_FALSE
;
715 progress
= do_sub_to_add_neg(ir
) || progress
;
718 progress
= do_function_inlining(ir
) || progress
;
719 progress
= do_dead_functions(ir
) || progress
;
721 progress
= do_structure_splitting(ir
) || progress
;
722 progress
= do_if_simplification(ir
) || progress
;
723 progress
= do_copy_propagation(ir
) || progress
;
725 progress
= do_dead_code(ir
) || progress
;
727 progress
= do_dead_code_unlinked(ir
) || progress
;
728 progress
= do_dead_code_local(ir
) || progress
;
729 progress
= do_tree_grafting(ir
) || progress
;
730 progress
= do_constant_propagation(ir
) || progress
;
732 progress
= do_constant_variable(ir
) || progress
;
734 progress
= do_constant_variable_unlinked(ir
) || progress
;
735 progress
= do_constant_folding(ir
) || progress
;
736 progress
= do_algebraic(ir
) || progress
;
737 progress
= do_if_return(ir
) || progress
;
738 progress
= do_vec_index_to_swizzle(ir
) || progress
;
739 progress
= do_swizzle_swizzle(ir
) || progress
;
740 progress
= do_noop_swizzle(ir
) || progress
;