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_print_visitor.h"
41 _mesa_glsl_error(YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
50 len
= snprintf(buf
, sizeof(buf
), "%u:%u(%u): error: ",
51 locp
->source
, locp
->first_line
, locp
->first_column
);
54 vsnprintf(buf
+ len
, sizeof(buf
) - len
, fmt
, ap
);
68 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier
*q
)
100 if (q
->noperspective
)
101 printf("noperspective ");
106 ast_node::print(void) const
108 printf("unhandled node ");
112 ast_node::ast_node(void)
114 make_empty_list(this);
119 ast_opt_array_size_print(bool is_array
, const ast_expression
*array_size
)
133 ast_compound_statement::print(void) const
135 const struct simple_node
*ptr
;
139 foreach(ptr
, & statements
) {
140 ((ast_node
*)ptr
)->print();
147 ast_compound_statement::ast_compound_statement(int new_scope
,
148 ast_node
*statements
)
150 this->new_scope
= new_scope
;
151 make_empty_list(& this->statements
);
153 if (statements
!= NULL
) {
154 /* This seems odd, but it works. The simple_list is,
155 * basically, a circular list. insert_at_tail adds
156 * the specified node to the list before the current
159 insert_at_tail((struct simple_node
*) statements
,
166 ast_expression::print(void) const
180 subexpressions
[0]->print();
181 printf("%s ", operator_string(oper
));
182 subexpressions
[1]->print();
185 case ast_field_selection
:
186 subexpressions
[0]->print();
187 printf(". %s ", primary_expression
.identifier
);
196 printf("%s ", operator_string(oper
));
197 subexpressions
[0]->print();
202 subexpressions
[0]->print();
203 printf("%s ", operator_string(oper
));
206 case ast_conditional
:
207 subexpressions
[0]->print();
209 subexpressions
[1]->print();
211 subexpressions
[1]->print();
214 case ast_array_index
:
215 subexpressions
[0]->print();
217 subexpressions
[1]->print();
221 case ast_function_call
: {
222 ast_expression
*parameters
= subexpressions
[1];
224 subexpressions
[0]->print();
227 if (parameters
!= NULL
) {
228 struct simple_node
*ptr
;
231 foreach (ptr
, (struct simple_node
*) parameters
) {
233 ((ast_node
*)ptr
)->print();
242 printf("%s ", primary_expression
.identifier
);
245 case ast_int_constant
:
246 printf("%d ", primary_expression
.int_constant
);
249 case ast_uint_constant
:
250 printf("%u ", primary_expression
.uint_constant
);
253 case ast_float_constant
:
254 printf("%f ", primary_expression
.float_constant
);
257 case ast_bool_constant
:
259 primary_expression
.bool_constant
264 struct simple_node
*ptr
;
265 struct simple_node
*const head
= first_elem(& expressions
);
268 foreach (ptr
, & expressions
) {
272 ((ast_node
*)ptr
)->print();
284 ast_expression::ast_expression(int oper
,
289 this->oper
= ast_operators(oper
);
290 this->subexpressions
[0] = ex0
;
291 this->subexpressions
[1] = ex1
;
292 this->subexpressions
[2] = ex2
;
293 make_empty_list(& expressions
);
298 ast_expression_statement::print(void) const
307 ast_expression_statement::ast_expression_statement(ast_expression
*ex
) :
315 ast_function::print(void) const
317 struct simple_node
*ptr
;
319 return_type
->print();
320 printf(" %s (", identifier
);
322 foreach(ptr
, & parameters
) {
323 ((ast_node
*)ptr
)->print();
330 ast_function::ast_function(void)
331 : is_definition(false), signature(NULL
)
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
= new glsl_symbol_table
;
639 state
.temp_index
= 0;
640 state
.loop_or_switch_nesting
= NULL
;
642 _mesa_glsl_lexer_ctor(& state
, shader
, shader_len
);
643 _mesa_glsl_parse(& state
);
644 _mesa_glsl_lexer_dtor(& state
);
646 foreach (ptr
, & state
.translation_unit
) {
647 ((ast_node
*)ptr
)->print();
650 _mesa_ast_to_hir(&instructions
, &state
);
652 /* Optimization passes */
654 /* Constant folding */
655 ir_constant_folding_visitor constant_folding
;
656 visit_exec_list(&instructions
, &constant_folding
);
659 /* Print out the resulting IR */
663 foreach_iter(exec_list_iterator
, iter
, instructions
) {
666 ((ir_instruction
*)iter
.get())->accept(& v
);
671 delete state
.symbols
;
673 return state
.error
!= 0;