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.
29 #include <sys/types.h>
35 #include "glsl_parser_extras.h"
36 #include "glsl_parser.h"
37 #include "ir_constant_folding.h"
38 #include "ir_dead_code.h"
39 #include "ir_function_inlining.h"
40 #include "ir_if_simplification.h"
41 #include "ir_print_visitor.h"
44 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target
)
47 case vertex_shader
: return "vertex";
48 case fragment_shader
: return "fragment";
49 case geometry_shader
: return "geometry";
52 assert(!"Should not get here.");
57 _mesa_glsl_error(YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
66 len
= snprintf(buf
, sizeof(buf
), "%u:%u(%u): error: ",
67 locp
->source
, locp
->first_line
, locp
->first_column
);
70 vsnprintf(buf
+ len
, sizeof(buf
) - len
, fmt
, ap
);
78 _mesa_glsl_warning(const YYLTYPE
*locp
, const _mesa_glsl_parse_state
*state
,
85 len
= snprintf(buf
, sizeof(buf
), "%u:%u(%u): warning: ",
86 locp
->source
, locp
->first_line
, locp
->first_column
);
89 vsnprintf(buf
+ len
, sizeof(buf
) - len
, fmt
, ap
);
97 _mesa_glsl_process_extension(const char *name
, YYLTYPE
*name_locp
,
98 const char *behavior
, YYLTYPE
*behavior_locp
,
99 _mesa_glsl_parse_state
*state
)
108 if (strcmp(behavior
, "warn") == 0) {
109 ext_mode
= extension_warn
;
110 } else if (strcmp(behavior
, "require") == 0) {
111 ext_mode
= extension_require
;
112 } else if (strcmp(behavior
, "enable") == 0) {
113 ext_mode
= extension_enable
;
114 } else if (strcmp(behavior
, "disable") == 0) {
115 ext_mode
= extension_disable
;
117 _mesa_glsl_error(behavior_locp
, state
,
118 "Unknown extension behavior `%s'",
123 bool unsupported
= false;
125 if (strcmp(name
, "all") == 0) {
126 if ((ext_mode
== extension_enable
) || (ext_mode
== extension_require
)) {
127 _mesa_glsl_error(name_locp
, state
, "Cannot %s all extensions",
128 (ext_mode
== extension_enable
)
129 ? "enable" : "require");
132 } if (strcmp(name
, "GL_ARB_draw_buffers") == 0) {
133 /* This extension is only supported in fragment shaders.
135 if (state
->target
!= fragment_shader
) {
138 state
->ARB_draw_buffers_enable
= (ext_mode
!= extension_disable
);
139 state
->ARB_draw_buffers_warn
= (ext_mode
== extension_warn
);
141 } if (strcmp(name
, "GL_ARB_texture_rectangle") == 0) {
142 state
->ARB_texture_rectangle_enable
= (ext_mode
!= extension_disable
);
143 state
->ARB_texture_rectangle_warn
= (ext_mode
== extension_warn
);
149 static const char *const fmt
= "extension `%s' unsupported in %s shader";
151 if (ext_mode
== extension_require
) {
152 _mesa_glsl_error(name_locp
, state
, fmt
,
153 name
, _mesa_glsl_shader_target_name(state
->target
));
156 _mesa_glsl_warning(name_locp
, state
, fmt
,
157 name
, _mesa_glsl_shader_target_name(state
->target
));
165 ast_node::~ast_node()
172 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier
*q
)
178 printf("invariant ");
181 printf("attribute ");
204 if (q
->noperspective
)
205 printf("noperspective ");
210 ast_node::print(void) const
212 printf("unhandled node ");
216 ast_node::ast_node(void)
218 make_empty_list(this);
223 ast_opt_array_size_print(bool is_array
, const ast_expression
*array_size
)
237 ast_compound_statement::print(void) const
239 const struct simple_node
*ptr
;
243 foreach(ptr
, & statements
) {
244 ((ast_node
*)ptr
)->print();
251 ast_compound_statement::ast_compound_statement(int new_scope
,
252 ast_node
*statements
)
254 this->new_scope
= new_scope
;
255 make_empty_list(& this->statements
);
257 if (statements
!= NULL
) {
258 /* This seems odd, but it works. The simple_list is,
259 * basically, a circular list. insert_at_tail adds
260 * the specified node to the list before the current
263 insert_at_tail((struct simple_node
*) statements
,
270 ast_expression::print(void) const
284 subexpressions
[0]->print();
285 printf("%s ", operator_string(oper
));
286 subexpressions
[1]->print();
289 case ast_field_selection
:
290 subexpressions
[0]->print();
291 printf(". %s ", primary_expression
.identifier
);
300 printf("%s ", operator_string(oper
));
301 subexpressions
[0]->print();
306 subexpressions
[0]->print();
307 printf("%s ", operator_string(oper
));
310 case ast_conditional
:
311 subexpressions
[0]->print();
313 subexpressions
[1]->print();
315 subexpressions
[1]->print();
318 case ast_array_index
:
319 subexpressions
[0]->print();
321 subexpressions
[1]->print();
325 case ast_function_call
: {
326 ast_expression
*parameters
= subexpressions
[1];
328 subexpressions
[0]->print();
331 if (parameters
!= NULL
) {
332 struct simple_node
*ptr
;
335 foreach (ptr
, (struct simple_node
*) parameters
) {
337 ((ast_node
*)ptr
)->print();
346 printf("%s ", primary_expression
.identifier
);
349 case ast_int_constant
:
350 printf("%d ", primary_expression
.int_constant
);
353 case ast_uint_constant
:
354 printf("%u ", primary_expression
.uint_constant
);
357 case ast_float_constant
:
358 printf("%f ", primary_expression
.float_constant
);
361 case ast_bool_constant
:
363 primary_expression
.bool_constant
368 struct simple_node
*ptr
;
369 struct simple_node
*const head
= first_elem(& expressions
);
372 foreach (ptr
, & expressions
) {
376 ((ast_node
*)ptr
)->print();
388 ast_expression::ast_expression(int oper
,
393 this->oper
= ast_operators(oper
);
394 this->subexpressions
[0] = ex0
;
395 this->subexpressions
[1] = ex1
;
396 this->subexpressions
[2] = ex2
;
397 make_empty_list(& expressions
);
402 ast_expression_statement::print(void) const
411 ast_expression_statement::ast_expression_statement(ast_expression
*ex
) :
419 ast_function::print(void) const
421 struct simple_node
*ptr
;
423 return_type
->print();
424 printf(" %s (", identifier
);
426 foreach(ptr
, & parameters
) {
427 ((ast_node
*)ptr
)->print();
434 ast_function::ast_function(void)
435 : is_definition(false), signature(NULL
)
437 make_empty_list(& parameters
);
442 ast_fully_specified_type::print(void) const
444 _mesa_ast_type_qualifier_print(& qualifier
);
450 ast_parameter_declarator::print(void) const
454 printf("%s ", identifier
);
455 ast_opt_array_size_print(is_array
, array_size
);
460 ast_function_definition::print(void) const
468 ast_declaration::print(void) const
470 printf("%s ", identifier
);
471 ast_opt_array_size_print(is_array
, array_size
);
475 initializer
->print();
480 ast_declaration::ast_declaration(char *identifier
, int is_array
,
481 ast_expression
*array_size
,
482 ast_expression
*initializer
)
484 this->identifier
= identifier
;
485 this->is_array
= is_array
;
486 this->array_size
= array_size
;
487 this->initializer
= initializer
;
492 ast_declarator_list::print(void) const
494 struct simple_node
*head
;
495 struct simple_node
*ptr
;
497 assert(type
|| invariant
);
502 printf("invariant ");
504 head
= first_elem(& declarations
);
505 foreach (ptr
, & declarations
) {
509 ((ast_node
*)ptr
)->print();
516 ast_declarator_list::ast_declarator_list(ast_fully_specified_type
*type
)
519 make_empty_list(& this->declarations
);
523 ast_jump_statement::print(void) const
527 printf("continue; ");
534 if (opt_return_value
)
535 opt_return_value
->print();
546 ast_jump_statement::ast_jump_statement(int mode
, ast_expression
*return_value
)
548 this->mode
= ast_jump_modes(mode
);
550 if (mode
== ast_return
)
551 opt_return_value
= return_value
;
556 ast_selection_statement::print(void) const
562 then_statement
->print();
564 if (else_statement
) {
566 else_statement
->print();
572 ast_selection_statement::ast_selection_statement(ast_expression
*condition
,
573 ast_node
*then_statement
,
574 ast_node
*else_statement
)
576 this->condition
= condition
;
577 this->then_statement
= then_statement
;
578 this->else_statement
= else_statement
;
583 ast_iteration_statement::print(void) const
589 init_statement
->print();
597 rest_expression
->print();
623 ast_iteration_statement::ast_iteration_statement(int mode
,
626 ast_expression
*rest_expression
,
629 this->mode
= ast_iteration_modes(mode
);
630 this->init_statement
= init
;
631 this->condition
= condition
;
632 this->rest_expression
= rest_expression
;
638 ast_struct_specifier::print(void) const
640 struct simple_node
*ptr
;
642 printf("struct %s { ", name
);
643 foreach (ptr
, & declarations
) {
644 ((ast_node
*)ptr
)->print();
650 ast_struct_specifier::ast_struct_specifier(char *identifier
,
651 ast_node
*declarator_list
)
655 /* This seems odd, but it works. The simple_list is,
656 * basically, a circular list. insert_at_tail adds
657 * the specified node to the list before the current
660 insert_at_tail((struct simple_node
*) declarator_list
,
666 load_text_file(const char *file_name
, size_t *size
)
670 ssize_t total_read
= 0;
671 int fd
= open(file_name
, O_RDONLY
);
678 if (fstat(fd
, & st
) == 0) {
679 text
= (char *) malloc(st
.st_size
+ 1);
682 ssize_t bytes
= read(fd
, text
+ total_read
,
683 st
.st_size
- total_read
);
695 } while (total_read
< st
.st_size
);
697 text
[total_read
] = '\0';
709 main(int argc
, char **argv
)
711 struct _mesa_glsl_parse_state state
;
714 struct simple_node
*ptr
;
715 exec_list instructions
;
718 printf("Usage: %s [v|g|f] <shader_file>\n", argv
[0]);
722 switch (argv
[1][0]) {
724 state
.target
= vertex_shader
;
727 state
.target
= geometry_shader
;
730 state
.target
= fragment_shader
;
733 printf("Usage: %s [v|g|f] <shader_file>\n", argv
[0]);
737 shader
= load_text_file(argv
[2], & shader_len
);
739 state
.scanner
= NULL
;
740 make_empty_list(& state
.translation_unit
);
741 state
.symbols
= new glsl_symbol_table
;
743 state
.temp_index
= 0;
744 state
.loop_or_switch_nesting
= NULL
;
745 state
.ARB_texture_rectangle_enable
= true;
747 _mesa_glsl_lexer_ctor(& state
, shader
, shader_len
);
748 _mesa_glsl_parse(& state
);
749 _mesa_glsl_lexer_dtor(& state
);
751 foreach (ptr
, & state
.translation_unit
) {
752 ((ast_node
*)ptr
)->print();
755 _mesa_ast_to_hir(&instructions
, &state
);
757 /* Optimization passes */
763 progress
= do_function_inlining(&instructions
) || progress
;
764 progress
= do_if_simplification(&instructions
) || progress
;
765 progress
= do_dead_code_unlinked(&instructions
) || progress
;
767 /* Constant folding */
768 ir_constant_folding_visitor constant_folding
;
769 visit_exec_list(&instructions
, &constant_folding
);
773 /* Print out the resulting IR */
777 foreach_iter(exec_list_iterator
, iter
, instructions
) {
780 ((ir_instruction
*)iter
.get())->accept(& v
);
785 delete state
.symbols
;
787 return state
.error
!= 0;