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 "symbol_table.h"
38 #include "ir_print_visitor.h"
41 _mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
51 len = snprintf(buf, sizeof(buf), "%u:%u(%u): error: ",
52 locp->source, locp->first_line, locp->first_column);
55 vsnprintf(buf + len, sizeof(buf) - len, fmt, ap);
69 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q)
101 if (q->noperspective)
102 printf("noperspective ");
107 ast_node::print(void) const
109 printf("node_%d ", type);
113 ast_node::ast_node(void)
115 make_empty_list(this);
120 ast_opt_array_size_print(bool is_array, const ast_expression *array_size)
134 ast_compound_statement::print(void) const
136 const struct simple_node *ptr;
140 foreach(ptr, & statements) {
141 ((ast_node *)ptr)->print();
148 ast_compound_statement::ast_compound_statement(int new_scope,
149 ast_node *statements)
151 this->new_scope = new_scope;
152 make_empty_list(& this->statements);
154 if (statements != NULL) {
155 /* This seems odd, but it works. The simple_list is,
156 * basically, a circular list. insert_at_tail adds
157 * the specified node to the list before the current
160 insert_at_tail((struct simple_node *) statements,
167 ast_expression::print(void) const
181 subexpressions[0]->print();
182 printf("%s ", operator_string(oper));
183 subexpressions[1]->print();
186 case ast_field_selection:
187 subexpressions[0]->print();
188 printf(". %s ", primary_expression.identifier);
197 printf("%s ", operator_string(oper));
198 subexpressions[0]->print();
203 subexpressions[0]->print();
204 printf("%s ", operator_string(oper));
207 case ast_conditional:
208 subexpressions[0]->print();
210 subexpressions[1]->print();
212 subexpressions[1]->print();
215 case ast_array_index:
216 subexpressions[0]->print();
218 subexpressions[1]->print();
222 case ast_function_call: {
223 ast_expression *parameters = subexpressions[1];
225 subexpressions[0]->print();
228 if (parameters != NULL) {
229 struct simple_node *ptr;
232 foreach (ptr, (struct simple_node *) parameters) {
234 ((ast_node *)ptr)->print();
243 printf("%s ", primary_expression.identifier);
246 case ast_int_constant:
247 printf("%d ", primary_expression.int_constant);
250 case ast_uint_constant:
251 printf("%u ", primary_expression.uint_constant);
254 case ast_float_constant:
255 printf("%f ", primary_expression.float_constant);
258 case ast_bool_constant:
260 primary_expression.bool_constant
265 struct simple_node *ptr;
266 struct simple_node *const head = first_elem(& expressions);
269 foreach (ptr, & expressions) {
273 ((ast_node *)ptr)->print();
285 ast_expression::ast_expression(int oper,
290 this->oper = ast_operators(oper);
291 this->subexpressions[0] = ex0;
292 this->subexpressions[1] = ex1;
293 this->subexpressions[2] = ex2;
294 make_empty_list(& expressions);
299 ast_expression_statement::print(void) const
308 ast_expression_statement::ast_expression_statement(ast_expression *ex) :
316 ast_function::print(void) const
318 struct simple_node *ptr;
320 return_type->print();
321 printf(" %s (", identifier);
323 foreach(ptr, & parameters) {
324 ((ast_node *)ptr)->print();
331 ast_function::ast_function(void)
333 make_empty_list(& parameters);
338 ast_fully_specified_type::print(void) const
340 _mesa_ast_type_qualifier_print(& qualifier);
346 ast_parameter_declarator::print(void) const
350 printf("%s ", identifier);
351 ast_opt_array_size_print(is_array, array_size);
356 ast_function_definition::print(void) const
364 ast_declaration::print(void) const
366 printf("%s ", identifier);
367 ast_opt_array_size_print(is_array, array_size);
371 initializer->print();
376 ast_declaration::ast_declaration(char *identifier, int is_array,
377 ast_expression *array_size,
378 ast_expression *initializer)
380 this->identifier = identifier;
381 this->is_array = is_array;
382 this->array_size = array_size;
383 this->initializer = initializer;
388 ast_declarator_list::print(void) const
390 struct simple_node *head;
391 struct simple_node *ptr;
393 assert(type || invariant);
398 printf("invariant ");
400 head = first_elem(& declarations);
401 foreach (ptr, & declarations) {
405 ((ast_node *)ptr)->print();
412 ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type)
415 make_empty_list(& this->declarations);
419 ast_jump_statement::print(void) const
423 printf("continue; ");
430 if (opt_return_value)
431 opt_return_value->print();
442 ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value)
444 this->mode = ast_jump_modes(mode);
446 if (mode == ast_return)
447 opt_return_value = return_value;
452 ast_selection_statement::print(void) const
458 then_statement->print();
460 if (else_statement) {
462 else_statement->print();
468 ast_selection_statement::ast_selection_statement(ast_expression *condition,
469 ast_node *then_statement,
470 ast_node *else_statement)
472 this->condition = condition;
473 this->then_statement = then_statement;
474 this->else_statement = else_statement;
479 ast_iteration_statement::print(void) const
485 init_statement->print();
493 rest_expression->print();
519 ast_iteration_statement::ast_iteration_statement(int mode,
522 ast_expression *rest_expression,
525 this->mode = ast_iteration_modes(mode);
526 this->init_statement = init;
527 this->condition = condition;
528 this->rest_expression = rest_expression;
534 ast_struct_specifier::print(void) const
536 struct simple_node *ptr;
538 printf("struct %s { ", name);
539 foreach (ptr, & declarations) {
540 ((ast_node *)ptr)->print();
546 ast_struct_specifier::ast_struct_specifier(char *identifier,
547 ast_node *declarator_list)
551 /* This seems odd, but it works. The simple_list is,
552 * basically, a circular list. insert_at_tail adds
553 * the specified node to the list before the current
556 insert_at_tail((struct simple_node *) declarator_list,
562 load_text_file(const char *file_name, size_t *size)
566 ssize_t total_read = 0;
567 int fd = open(file_name, O_RDONLY);
574 if (fstat(fd, & st) == 0) {
575 text = (char *) malloc(st.st_size + 1);
578 ssize_t bytes = read(fd, text + total_read,
579 st.st_size - total_read);
591 } while (total_read < st.st_size);
593 text[total_read] = '\0';
605 main(int argc, char **argv)
607 struct _mesa_glsl_parse_state state;
610 struct simple_node *ptr;
611 exec_list instructions;
614 printf("Usage: %s [v|g|f] <shader_file>\n", argv[0]);
618 switch (argv[1][0]) {
620 state.target = vertex_shader;
623 state.target = geometry_shader;
626 state.target = fragment_shader;
629 printf("Usage: %s [v|g|f] <shader_file>\n", argv[0]);
633 shader = load_text_file(argv[2], & shader_len);
635 state.scanner = NULL;
636 make_empty_list(& state.translation_unit);
637 state.symbols = _mesa_symbol_table_ctor();
640 _mesa_glsl_lexer_ctor(& state, shader, shader_len);
641 _mesa_glsl_parse(& state);
642 _mesa_glsl_lexer_dtor(& state);
644 foreach (ptr, & state.translation_unit) {
645 ((ast_node *)ptr)->print();
648 _mesa_ast_to_hir(&instructions, &state);
653 foreach_iter(exec_list_iterator, iter, instructions) {
656 ((ir_instruction *)iter.get())->accept(& v);
660 _mesa_symbol_table_dtor(state.symbols);