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
, void *state
, const char *fmt
, ...)
48 len
= snprintf(buf
, sizeof(buf
), "%u:%u(%u): error: ",
49 locp
->source
, locp
->first_line
, locp
->first_column
);
52 vsnprintf(buf
+ len
, sizeof(buf
) - len
, fmt
, ap
);
66 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier
*q
)
99 printf("noperspective ");
104 ast_node::print(void) const
106 printf("node_%d ", type
);
110 ast_node::ast_node(void)
112 make_empty_list(this);
116 ast_type_specifier::print(void) const
118 switch (type_specifier
) {
119 case ast_void
: printf("void "); break;
120 case ast_float
: printf("float "); break;
121 case ast_int
: printf("int "); break;
122 case ast_uint
: printf("uint "); break;
123 case ast_bool
: printf("bool "); break;
124 case ast_vec2
: printf("vec2 "); break;
125 case ast_vec3
: printf("vec3 "); break;
126 case ast_vec4
: printf("vec4 "); break;
127 case ast_bvec2
: printf("bvec2 "); break;
128 case ast_bvec3
: printf("bvec3 "); break;
129 case ast_bvec4
: printf("bvec4 "); break;
130 case ast_ivec2
: printf("ivec2 "); break;
131 case ast_ivec3
: printf("ivec3 "); break;
132 case ast_ivec4
: printf("ivec4 "); break;
133 case ast_uvec2
: printf("uvec2 "); break;
134 case ast_uvec3
: printf("uvec3 "); break;
135 case ast_uvec4
: printf("uvec4 "); break;
136 case ast_mat2
: printf("mat2 "); break;
137 case ast_mat2x3
: printf("mat2x3 "); break;
138 case ast_mat2x4
: printf("mat2x4 "); break;
139 case ast_mat3x2
: printf("mat3x2 "); break;
140 case ast_mat3
: printf("mat3 "); break;
141 case ast_mat3x4
: printf("mat3x4 "); break;
142 case ast_mat4x2
: printf("mat4x2 "); break;
143 case ast_mat4x3
: printf("mat4x3 "); break;
144 case ast_mat4
: printf("mat4 "); break;
145 case ast_sampler1d
: printf("sampler1d "); break;
146 case ast_sampler2d
: printf("sampler2d "); break;
147 case ast_sampler3d
: printf("sampler3d "); break;
148 case ast_samplercube
: printf("samplercube "); break;
149 case ast_sampler1dshadow
: printf("sampler1dshadow "); break;
150 case ast_sampler2dshadow
: printf("sampler2dshadow "); break;
151 case ast_samplercubeshadow
: printf("samplercubeshadow "); break;
152 case ast_sampler1darray
: printf("sampler1darray "); break;
153 case ast_sampler2darray
: printf("sampler2darray "); break;
154 case ast_sampler1darrayshadow
: printf("sampler1darrayshadow "); break;
155 case ast_sampler2darrayshadow
: printf("sampler2darrayshadow "); break;
156 case ast_isampler1d
: printf("isampler1d "); break;
157 case ast_isampler2d
: printf("isampler2d "); break;
158 case ast_isampler3d
: printf("isampler3d "); break;
159 case ast_isamplercube
: printf("isamplercube "); break;
160 case ast_isampler1darray
: printf("isampler1darray "); break;
161 case ast_isampler2darray
: printf("isampler2darray "); break;
162 case ast_usampler1d
: printf("usampler1d "); break;
163 case ast_usampler2d
: printf("usampler2d "); break;
164 case ast_usampler3d
: printf("usampler3d "); break;
165 case ast_usamplercube
: printf("usamplercube "); break;
166 case ast_usampler1darray
: printf("usampler1darray "); break;
167 case ast_usampler2darray
: printf("usampler2darray "); break;
173 case ast_type_name
: printf("%s ", type_name
); break;
188 ast_opt_array_size_print(bool is_array
, const ast_expression
*array_size
)
201 ast_type_specifier::ast_type_specifier(int specifier
)
203 type_specifier
= ast_types(specifier
);
208 ast_compound_statement::print(void) const
210 const struct simple_node
*ptr
;
214 foreach(ptr
, & statements
) {
215 ((ast_node
*)ptr
)->print();
222 ast_compound_statement::ast_compound_statement(int new_scope
,
223 ast_node
*statements
)
225 this->new_scope
= new_scope
;
226 make_empty_list(& this->statements
);
228 if (statements
!= NULL
) {
229 /* This seems odd, but it works. The simple_list is,
230 * basically, a circular list. insert_at_tail adds
231 * the specified node to the list before the current
234 insert_at_tail((struct simple_node
*) statements
,
241 ast_expression::print(void) const
255 subexpressions
[0]->print();
256 printf("%s ", operator_string(oper
));
257 subexpressions
[1]->print();
260 case ast_field_selection
:
261 subexpressions
[0]->print();
262 printf(". %s ", primary_expression
.identifier
);
271 printf("%s ", operator_string(oper
));
272 subexpressions
[0]->print();
277 subexpressions
[0]->print();
278 printf("%s ", operator_string(oper
));
281 case ast_conditional
:
282 subexpressions
[0]->print();
284 subexpressions
[1]->print();
286 subexpressions
[1]->print();
289 case ast_array_index
:
290 subexpressions
[0]->print();
292 subexpressions
[1]->print();
296 case ast_function_call
: {
297 ast_expression
*parameters
= subexpressions
[1];
299 subexpressions
[0]->print();
302 if (parameters
!= NULL
) {
303 struct simple_node
*ptr
;
306 foreach (ptr
, (struct simple_node
*) parameters
) {
308 ((ast_node
*)ptr
)->print();
317 printf("%s ", primary_expression
.identifier
);
320 case ast_int_constant
:
321 printf("%d ", primary_expression
.int_constant
);
324 case ast_uint_constant
:
325 printf("%u ", primary_expression
.uint_constant
);
328 case ast_float_constant
:
329 printf("%f ", primary_expression
.float_constant
);
332 case ast_bool_constant
:
334 primary_expression
.bool_constant
339 struct simple_node
*ptr
;
340 struct simple_node
*const head
= first_elem(& expressions
);
343 foreach (ptr
, & expressions
) {
347 ((ast_node
*)ptr
)->print();
359 ast_expression::ast_expression(int oper
,
364 this->oper
= ast_operators(oper
);
365 this->subexpressions
[0] = ex0
;
366 this->subexpressions
[1] = ex1
;
367 this->subexpressions
[2] = ex2
;
368 make_empty_list(& expressions
);
373 ast_expression_statement::print(void) const
382 ast_expression_statement::ast_expression_statement(ast_expression
*ex
) :
390 ast_function::print(void) const
392 struct simple_node
*ptr
;
394 return_type
->print();
395 printf(" %s (", identifier
);
397 foreach(ptr
, & parameters
) {
398 ((ast_node
*)ptr
)->print();
405 ast_function::ast_function(void)
407 make_empty_list(& parameters
);
412 ast_fully_specified_type::print(void) const
414 _mesa_ast_type_qualifier_print(& qualifier
);
420 ast_parameter_declarator::print(void) const
424 printf("%s ", identifier
);
425 ast_opt_array_size_print(is_array
, array_size
);
430 ast_function_definition::print(void) const
438 ast_declaration::print(void) const
440 printf("%s ", identifier
);
441 ast_opt_array_size_print(is_array
, array_size
);
445 initializer
->print();
450 ast_declaration::ast_declaration(char *identifier
, int is_array
,
451 ast_expression
*array_size
,
452 ast_expression
*initializer
)
454 this->identifier
= identifier
;
455 this->is_array
= is_array
;
456 this->array_size
= array_size
;
457 this->initializer
= initializer
;
462 ast_declarator_list::print(void) const
464 struct simple_node
*head
;
465 struct simple_node
*ptr
;
467 assert(type
|| invariant
);
472 printf("invariant ");
474 head
= first_elem(& declarations
);
475 foreach (ptr
, & declarations
) {
479 ((ast_node
*)ptr
)->print();
486 ast_declarator_list::ast_declarator_list(ast_fully_specified_type
*type
)
489 make_empty_list(& this->declarations
);
493 ast_jump_statement::print(void) const
497 printf("continue; ");
504 if (opt_return_value
)
505 opt_return_value
->print();
516 ast_jump_statement::ast_jump_statement(int mode
, ast_expression
*return_value
)
518 this->mode
= ast_jump_modes(mode
);
520 if (mode
== ast_return
)
521 opt_return_value
= return_value
;
526 ast_selection_statement::print(void) const
532 then_statement
->print();
534 if (else_statement
) {
536 else_statement
->print();
542 ast_selection_statement::ast_selection_statement(ast_expression
*condition
,
543 ast_node
*then_statement
,
544 ast_node
*else_statement
)
546 this->condition
= condition
;
547 this->then_statement
= then_statement
;
548 this->else_statement
= else_statement
;
553 ast_iteration_statement::print(void) const
559 init_statement
->print();
567 rest_expression
->print();
593 ast_iteration_statement::ast_iteration_statement(int mode
,
596 ast_expression
*rest_expression
,
599 this->mode
= ast_iteration_modes(mode
);
600 this->init_statement
= init
;
601 this->condition
= condition
;
602 this->rest_expression
= rest_expression
;
608 ast_struct_specifier::print(void) const
610 struct simple_node
*ptr
;
612 printf("struct %s { ", name
);
613 foreach (ptr
, & declarations
) {
614 ((ast_node
*)ptr
)->print();
620 ast_struct_specifier::ast_struct_specifier(char *identifier
,
621 ast_node
*declarator_list
)
625 /* This seems odd, but it works. The simple_list is,
626 * basically, a circular list. insert_at_tail adds
627 * the specified node to the list before the current
630 insert_at_tail((struct simple_node
*) declarator_list
,
636 load_text_file(const char *file_name
, size_t *size
)
640 ssize_t total_read
= 0;
641 int fd
= open(file_name
, O_RDONLY
);
648 if (fstat(fd
, & st
) == 0) {
649 text
= (char *) malloc(st
.st_size
+ 1);
652 ssize_t bytes
= read(fd
, text
+ total_read
,
653 st
.st_size
- total_read
);
665 } while (total_read
< st
.st_size
);
667 text
[total_read
] = '\0';
679 main(int argc
, char **argv
)
681 struct _mesa_glsl_parse_state state
;
684 struct simple_node
*ptr
;
685 exec_list instructions
;
688 shader
= load_text_file(argv
[1], & shader_len
);
690 state
.scanner
= NULL
;
691 make_empty_list(& state
.translation_unit
);
692 state
.symbols
= _mesa_symbol_table_ctor();
694 _mesa_glsl_lexer_ctor(& state
, shader
, shader_len
);
695 _mesa_glsl_parse(& state
);
696 _mesa_glsl_lexer_dtor(& state
);
698 foreach (ptr
, & state
.translation_unit
) {
699 ((ast_node
*)ptr
)->print();
702 foreach (ptr
, & state
.translation_unit
) {
703 ((ast_node
*)ptr
)->hir(&instructions
, &state
);
707 foreach_iter(exec_list_iterator
, iter
, instructions
) {
710 ((ir_instruction
*)iter
.get())->accept(& v
);
713 _mesa_symbol_table_dtor(state
.symbols
);