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_function_inlining.h"
39 #include "ir_print_visitor.h"
42 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target)
45 case vertex_shader: return "vertex";
46 case fragment_shader: return "fragment";
47 case geometry_shader: return "geometry";
50 assert(!"Should not get here.");
55 _mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
64 len = snprintf(buf, sizeof(buf), "%u:%u(%u): error: ",
65 locp->source, locp->first_line, locp->first_column);
68 vsnprintf(buf + len, sizeof(buf) - len, fmt, ap);
76 _mesa_glsl_warning(const YYLTYPE *locp, const _mesa_glsl_parse_state *state,
83 len = snprintf(buf, sizeof(buf), "%u:%u(%u): warning: ",
84 locp->source, locp->first_line, locp->first_column);
87 vsnprintf(buf + len, sizeof(buf) - len, fmt, ap);
95 _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
96 const char *behavior, YYLTYPE *behavior_locp,
97 _mesa_glsl_parse_state *state)
106 if (strcmp(behavior, "warn") == 0) {
107 ext_mode = extension_warn;
108 } else if (strcmp(behavior, "require") == 0) {
109 ext_mode = extension_require;
110 } else if (strcmp(behavior, "enable") == 0) {
111 ext_mode = extension_enable;
112 } else if (strcmp(behavior, "disable") == 0) {
113 ext_mode = extension_disable;
115 _mesa_glsl_error(behavior_locp, state,
116 "Unknown extension behavior `%s'",
121 bool unsupported = false;
123 if (strcmp(name, "all") == 0) {
124 if ((ext_mode == extension_enable) || (ext_mode == extension_require)) {
125 _mesa_glsl_error(name_locp, state, "Cannot %s all extensions",
126 (ext_mode == extension_enable)
127 ? "enable" : "require");
130 } if (strcmp(name, "GL_ARB_draw_buffers") == 0) {
131 /* This extension is only supported in fragment shaders.
133 if (state->target != fragment_shader) {
136 state->ARB_draw_buffers_enable = (ext_mode != extension_disable);
137 state->ARB_draw_buffers_warn = (ext_mode == extension_warn);
139 } if (strcmp(name, "GL_ARB_texture_rectangle") == 0) {
140 state->ARB_texture_rectangle_enable = (ext_mode != extension_disable);
141 state->ARB_texture_rectangle_warn = (ext_mode == extension_warn);
147 static const char *const fmt = "extension `%s' unsupported in %s shader";
149 if (ext_mode == extension_require) {
150 _mesa_glsl_error(name_locp, state, fmt,
151 name, _mesa_glsl_shader_target_name(state->target));
154 _mesa_glsl_warning(name_locp, state, fmt,
155 name, _mesa_glsl_shader_target_name(state->target));
163 ast_node::~ast_node()
170 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q)
176 printf("invariant ");
179 printf("attribute ");
202 if (q->noperspective)
203 printf("noperspective ");
208 ast_node::print(void) const
210 printf("unhandled node ");
214 ast_node::ast_node(void)
216 make_empty_list(this);
221 ast_opt_array_size_print(bool is_array, const ast_expression *array_size)
235 ast_compound_statement::print(void) const
237 const struct simple_node *ptr;
241 foreach(ptr, & statements) {
242 ((ast_node *)ptr)->print();
249 ast_compound_statement::ast_compound_statement(int new_scope,
250 ast_node *statements)
252 this->new_scope = new_scope;
253 make_empty_list(& this->statements);
255 if (statements != NULL) {
256 /* This seems odd, but it works. The simple_list is,
257 * basically, a circular list. insert_at_tail adds
258 * the specified node to the list before the current
261 insert_at_tail((struct simple_node *) statements,
268 ast_expression::print(void) const
282 subexpressions[0]->print();
283 printf("%s ", operator_string(oper));
284 subexpressions[1]->print();
287 case ast_field_selection:
288 subexpressions[0]->print();
289 printf(". %s ", primary_expression.identifier);
298 printf("%s ", operator_string(oper));
299 subexpressions[0]->print();
304 subexpressions[0]->print();
305 printf("%s ", operator_string(oper));
308 case ast_conditional:
309 subexpressions[0]->print();
311 subexpressions[1]->print();
313 subexpressions[1]->print();
316 case ast_array_index:
317 subexpressions[0]->print();
319 subexpressions[1]->print();
323 case ast_function_call: {
324 ast_expression *parameters = subexpressions[1];
326 subexpressions[0]->print();
329 if (parameters != NULL) {
330 struct simple_node *ptr;
333 foreach (ptr, (struct simple_node *) parameters) {
335 ((ast_node *)ptr)->print();
344 printf("%s ", primary_expression.identifier);
347 case ast_int_constant:
348 printf("%d ", primary_expression.int_constant);
351 case ast_uint_constant:
352 printf("%u ", primary_expression.uint_constant);
355 case ast_float_constant:
356 printf("%f ", primary_expression.float_constant);
359 case ast_bool_constant:
361 primary_expression.bool_constant
366 struct simple_node *ptr;
367 struct simple_node *const head = first_elem(& expressions);
370 foreach (ptr, & expressions) {
374 ((ast_node *)ptr)->print();
386 ast_expression::ast_expression(int oper,
391 this->oper = ast_operators(oper);
392 this->subexpressions[0] = ex0;
393 this->subexpressions[1] = ex1;
394 this->subexpressions[2] = ex2;
395 make_empty_list(& expressions);
400 ast_expression_statement::print(void) const
409 ast_expression_statement::ast_expression_statement(ast_expression *ex) :
417 ast_function::print(void) const
419 struct simple_node *ptr;
421 return_type->print();
422 printf(" %s (", identifier);
424 foreach(ptr, & parameters) {
425 ((ast_node *)ptr)->print();
432 ast_function::ast_function(void)
433 : is_definition(false), signature(NULL)
435 make_empty_list(& parameters);
440 ast_fully_specified_type::print(void) const
442 _mesa_ast_type_qualifier_print(& qualifier);
448 ast_parameter_declarator::print(void) const
452 printf("%s ", identifier);
453 ast_opt_array_size_print(is_array, array_size);
458 ast_function_definition::print(void) const
466 ast_declaration::print(void) const
468 printf("%s ", identifier);
469 ast_opt_array_size_print(is_array, array_size);
473 initializer->print();
478 ast_declaration::ast_declaration(char *identifier, int is_array,
479 ast_expression *array_size,
480 ast_expression *initializer)
482 this->identifier = identifier;
483 this->is_array = is_array;
484 this->array_size = array_size;
485 this->initializer = initializer;
490 ast_declarator_list::print(void) const
492 struct simple_node *head;
493 struct simple_node *ptr;
495 assert(type || invariant);
500 printf("invariant ");
502 head = first_elem(& declarations);
503 foreach (ptr, & declarations) {
507 ((ast_node *)ptr)->print();
514 ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type)
517 make_empty_list(& this->declarations);
521 ast_jump_statement::print(void) const
525 printf("continue; ");
532 if (opt_return_value)
533 opt_return_value->print();
544 ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value)
546 this->mode = ast_jump_modes(mode);
548 if (mode == ast_return)
549 opt_return_value = return_value;
554 ast_selection_statement::print(void) const
560 then_statement->print();
562 if (else_statement) {
564 else_statement->print();
570 ast_selection_statement::ast_selection_statement(ast_expression *condition,
571 ast_node *then_statement,
572 ast_node *else_statement)
574 this->condition = condition;
575 this->then_statement = then_statement;
576 this->else_statement = else_statement;
581 ast_iteration_statement::print(void) const
587 init_statement->print();
595 rest_expression->print();
621 ast_iteration_statement::ast_iteration_statement(int mode,
624 ast_expression *rest_expression,
627 this->mode = ast_iteration_modes(mode);
628 this->init_statement = init;
629 this->condition = condition;
630 this->rest_expression = rest_expression;
636 ast_struct_specifier::print(void) const
638 struct simple_node *ptr;
640 printf("struct %s { ", name);
641 foreach (ptr, & declarations) {
642 ((ast_node *)ptr)->print();
648 ast_struct_specifier::ast_struct_specifier(char *identifier,
649 ast_node *declarator_list)
653 /* This seems odd, but it works. The simple_list is,
654 * basically, a circular list. insert_at_tail adds
655 * the specified node to the list before the current
658 insert_at_tail((struct simple_node *) declarator_list,
664 load_text_file(const char *file_name, size_t *size)
668 ssize_t total_read = 0;
669 int fd = open(file_name, O_RDONLY);
676 if (fstat(fd, & st) == 0) {
677 text = (char *) malloc(st.st_size + 1);
680 ssize_t bytes = read(fd, text + total_read,
681 st.st_size - total_read);
693 } while (total_read < st.st_size);
695 text[total_read] = '\0';
707 main(int argc, char **argv)
709 struct _mesa_glsl_parse_state state;
712 struct simple_node *ptr;
713 exec_list instructions;
716 printf("Usage: %s [v|g|f] <shader_file>\n", argv[0]);
720 switch (argv[1][0]) {
722 state.target = vertex_shader;
725 state.target = geometry_shader;
728 state.target = fragment_shader;
731 printf("Usage: %s [v|g|f] <shader_file>\n", argv[0]);
735 shader = load_text_file(argv[2], & shader_len);
737 state.scanner = NULL;
738 make_empty_list(& state.translation_unit);
739 state.symbols = new glsl_symbol_table;
741 state.temp_index = 0;
742 state.loop_or_switch_nesting = NULL;
743 state.ARB_texture_rectangle_enable = true;
745 _mesa_glsl_lexer_ctor(& state, shader, shader_len);
746 _mesa_glsl_parse(& state);
747 _mesa_glsl_lexer_dtor(& state);
749 foreach (ptr, & state.translation_unit) {
750 ((ast_node *)ptr)->print();
753 _mesa_ast_to_hir(&instructions, &state);
755 /* Optimization passes */
761 progress = do_function_inlining(&instructions) || progress;
763 /* Constant folding */
764 ir_constant_folding_visitor constant_folding;
765 visit_exec_list(&instructions, &constant_folding);
769 /* Print out the resulting IR */
773 foreach_iter(exec_list_iterator, iter, instructions) {
776 ((ir_instruction *)iter.get())->accept(& v);
781 delete state.symbols;
783 return state.error != 0;