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.tab.h"
37 #include "symbol_table.h"
40 _mesa_glsl_error(YYLTYPE
*locp
, void *state
, const char *fmt
, ...)
47 len
= snprintf(buf
, sizeof(buf
), "%u:%u(%u): error: ",
48 locp
->source
, locp
->first_line
, locp
->first_column
);
51 vsnprintf(buf
+ len
, sizeof(buf
) - len
, fmt
, ap
);
65 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier
*q
)
98 printf("noperspective ");
103 ast_node::print(void) const
105 printf("node_%d ", type
);
109 ast_node::ast_node(void)
111 make_empty_list(this);
115 ast_type_specifier::print(void) const
117 switch (type_specifier
) {
118 case ast_void
: printf("void "); break;
119 case ast_float
: printf("float "); break;
120 case ast_int
: printf("int "); break;
121 case ast_uint
: printf("uint "); break;
122 case ast_bool
: printf("bool "); break;
123 case ast_vec2
: printf("vec2 "); break;
124 case ast_vec3
: printf("vec3 "); break;
125 case ast_vec4
: printf("vec4 "); break;
126 case ast_bvec2
: printf("bvec2 "); break;
127 case ast_bvec3
: printf("bvec3 "); break;
128 case ast_bvec4
: printf("bvec4 "); break;
129 case ast_ivec2
: printf("ivec2 "); break;
130 case ast_ivec3
: printf("ivec3 "); break;
131 case ast_ivec4
: printf("ivec4 "); break;
132 case ast_uvec2
: printf("uvec2 "); break;
133 case ast_uvec3
: printf("uvec3 "); break;
134 case ast_uvec4
: printf("uvec4 "); break;
135 case ast_mat2
: printf("mat2 "); break;
136 case ast_mat2x3
: printf("mat2x3 "); break;
137 case ast_mat2x4
: printf("mat2x4 "); break;
138 case ast_mat3x2
: printf("mat3x2 "); break;
139 case ast_mat3
: printf("mat3 "); break;
140 case ast_mat3x4
: printf("mat3x4 "); break;
141 case ast_mat4x2
: printf("mat4x2 "); break;
142 case ast_mat4x3
: printf("mat4x3 "); break;
143 case ast_mat4
: printf("mat4 "); break;
144 case ast_sampler1d
: printf("sampler1d "); break;
145 case ast_sampler2d
: printf("sampler2d "); break;
146 case ast_sampler3d
: printf("sampler3d "); break;
147 case ast_samplercube
: printf("samplercube "); break;
148 case ast_sampler1dshadow
: printf("sampler1dshadow "); break;
149 case ast_sampler2dshadow
: printf("sampler2dshadow "); break;
150 case ast_samplercubeshadow
: printf("samplercubeshadow "); break;
151 case ast_sampler1darray
: printf("sampler1darray "); break;
152 case ast_sampler2darray
: printf("sampler2darray "); break;
153 case ast_sampler1darrayshadow
: printf("sampler1darrayshadow "); break;
154 case ast_sampler2darrayshadow
: printf("sampler2darrayshadow "); break;
155 case ast_isampler1d
: printf("isampler1d "); break;
156 case ast_isampler2d
: printf("isampler2d "); break;
157 case ast_isampler3d
: printf("isampler3d "); break;
158 case ast_isamplercube
: printf("isamplercube "); break;
159 case ast_isampler1darray
: printf("isampler1darray "); break;
160 case ast_isampler2darray
: printf("isampler2darray "); break;
161 case ast_usampler1d
: printf("usampler1d "); break;
162 case ast_usampler2d
: printf("usampler2d "); break;
163 case ast_usampler3d
: printf("usampler3d "); break;
164 case ast_usamplercube
: printf("usamplercube "); break;
165 case ast_usampler1darray
: printf("usampler1darray "); break;
166 case ast_usampler2darray
: printf("usampler2darray "); break;
172 case ast_type_name
: printf("%s ", type_name
); break;
187 ast_opt_array_size_print(bool is_array
, const ast_expression
*array_size
)
200 ast_type_specifier::ast_type_specifier(int specifier
)
202 type_specifier
= ast_types(specifier
);
207 ast_compound_statement::print(void) const
209 const struct simple_node
*ptr
;
213 foreach(ptr
, & statements
) {
214 _mesa_ast_print(ptr
);
221 ast_compound_statement::ast_compound_statement(int new_scope
,
222 ast_node
*statements
)
224 this->new_scope
= new_scope
;
225 make_empty_list(& this->statements
);
227 if (statements
!= NULL
) {
228 /* This seems odd, but it works. The simple_list is,
229 * basically, a circular list. insert_at_tail adds
230 * the specified node to the list before the current
233 insert_at_tail((struct simple_node
*) statements
,
240 ast_expression::print(void) const
242 static const char *const operators
[] = {
318 subexpressions
[0]->print();
319 printf("%s ", operators
[oper
]);
320 subexpressions
[1]->print();
323 case ast_field_selection
:
324 subexpressions
[0]->print();
325 printf(". %s ", primary_expression
.identifier
);
334 printf("%s ", operators
[oper
]);
335 subexpressions
[0]->print();
340 subexpressions
[0]->print();
341 printf("%s ", operators
[oper
]);
344 case ast_conditional
:
345 subexpressions
[0]->print();
347 subexpressions
[1]->print();
349 subexpressions
[1]->print();
352 case ast_array_index
:
353 subexpressions
[0]->print();
355 subexpressions
[1]->print();
359 case ast_function_call
: {
360 ast_expression
*parameters
= subexpressions
[1];
362 subexpressions
[0]->print();
365 if (parameters
!= NULL
) {
366 struct simple_node
*ptr
;
369 foreach (ptr
, (struct simple_node
*) parameters
) {
371 _mesa_ast_print(ptr
);
380 printf("%s ", primary_expression
.identifier
);
383 case ast_int_constant
:
384 printf("%d ", primary_expression
.int_constant
);
387 case ast_uint_constant
:
388 printf("%u ", primary_expression
.uint_constant
);
391 case ast_float_constant
:
392 printf("%f ", primary_expression
.float_constant
);
395 case ast_bool_constant
:
397 primary_expression
.bool_constant
402 struct simple_node
*ptr
;
403 struct simple_node
*const head
= first_elem(& expressions
);
406 foreach (ptr
, & expressions
) {
410 _mesa_ast_print(ptr
);
418 ast_expression::ast_expression(int oper
,
423 this->oper
= ast_operators(oper
);
424 this->subexpressions
[0] = ex0
;
425 this->subexpressions
[1] = ex1
;
426 this->subexpressions
[2] = ex2
;
427 make_empty_list(& expressions
);
432 ast_expression_statement::print(void) const
441 ast_expression_statement::ast_expression_statement(ast_expression
*ex
) :
449 ast_function::print(void) const
451 struct simple_node
*ptr
;
453 return_type
->print();
454 printf(" %s (", identifier
);
456 foreach(ptr
, & parameters
) {
457 _mesa_ast_print(ptr
);
464 ast_function::ast_function(void)
466 make_empty_list(& parameters
);
471 ast_fully_specified_type::print(void) const
473 _mesa_ast_type_qualifier_print(& qualifier
);
479 ast_parameter_declarator::print(void) const
483 printf("%s ", identifier
);
484 ast_opt_array_size_print(is_array
, array_size
);
489 ast_function_definition::print(void) const
497 ast_declaration::print(void) const
499 printf("%s ", identifier
);
500 ast_opt_array_size_print(is_array
, array_size
);
504 initializer
->print();
509 ast_declaration::ast_declaration(char *identifier
, int is_array
,
510 ast_expression
*array_size
,
511 ast_expression
*initializer
)
513 this->identifier
= identifier
;
514 this->is_array
= is_array
;
515 this->array_size
= array_size
;
516 this->initializer
= initializer
;
521 ast_declarator_list::print(void) const
523 struct simple_node
*head
;
524 struct simple_node
*ptr
;
526 assert(type
|| invariant
);
531 printf("invariant ");
533 head
= first_elem(& declarations
);
534 foreach (ptr
, & declarations
) {
538 _mesa_ast_print(ptr
);
545 ast_declarator_list::ast_declarator_list(ast_fully_specified_type
*type
)
548 make_empty_list(& this->declarations
);
552 ast_jump_statement::print(void) const
556 printf("continue; ");
563 if (opt_return_value
)
564 opt_return_value
->print();
575 ast_jump_statement::ast_jump_statement(int mode
, ast_expression
*return_value
)
577 this->mode
= ast_jump_modes(mode
);
579 if (mode
== ast_return
)
580 opt_return_value
= return_value
;
585 ast_selection_statement::print(void) const
591 then_statement
->print();
593 if (else_statement
) {
595 else_statement
->print();
601 ast_selection_statement::ast_selection_statement(ast_expression
*condition
,
602 ast_node
*then_statement
,
603 ast_node
*else_statement
)
605 this->condition
= condition
;
606 this->then_statement
= then_statement
;
607 this->else_statement
= else_statement
;
612 ast_iteration_statement::print(void) const
618 init_statement
->print();
626 rest_expression
->print();
652 ast_iteration_statement::ast_iteration_statement(int mode
,
655 ast_expression
*rest_expression
,
658 this->mode
= ast_iteration_modes(mode
);
659 this->init_statement
= init
;
660 this->condition
= condition
;
661 this->rest_expression
= rest_expression
;
667 ast_struct_specifier::print(void) const
669 struct simple_node
*ptr
;
671 printf("struct %s { ", name
);
672 foreach (ptr
, & declarations
) {
673 _mesa_ast_print(ptr
);
679 ast_struct_specifier::ast_struct_specifier(char *identifier
,
680 ast_node
*declarator_list
)
684 /* This seems odd, but it works. The simple_list is,
685 * basically, a circular list. insert_at_tail adds
686 * the specified node to the list before the current
689 insert_at_tail((struct simple_node
*) declarator_list
,
695 load_text_file(const char *file_name
, size_t *size
)
699 ssize_t total_read
= 0;
700 int fd
= open(file_name
, O_RDONLY
);
707 if (fstat(fd
, & st
) == 0) {
708 text
= (char *) malloc(st
.st_size
+ 1);
711 ssize_t bytes
= read(fd
, text
+ total_read
,
712 st
.st_size
- total_read
);
724 } while (total_read
< st
.st_size
);
726 text
[total_read
] = '\0';
738 main(int argc
, char **argv
)
740 struct _mesa_glsl_parse_state state
;
743 struct simple_node
*ptr
;
744 struct simple_node instructions
;
747 shader
= load_text_file(argv
[1], & shader_len
);
749 state
.scanner
= NULL
;
750 make_empty_list(& state
.translation_unit
);
751 state
.symbols
= _mesa_symbol_table_ctor();
753 _mesa_glsl_lexer_ctor(& state
, shader
, shader_len
);
754 _mesa_glsl_parse(& state
);
755 _mesa_glsl_lexer_dtor(& state
);
757 foreach (ptr
, & state
.translation_unit
) {
758 _mesa_ast_print(ptr
);
762 make_empty_list(& instructions
);
763 foreach (ptr
, & state
.translation_unit
) {
764 _mesa_ast_to_hir(ptr
, &instructions
, &state
);
768 _mesa_symbol_table_dtor(state
.symbols
);