blob: a0fad52d62ac3fd7a4818ad334a2f2cceef6e478 [file] [log] [blame]
Ian Romanicka87ac252010-02-22 13:19:34 -08001/*
2 * Copyright © 2008, 2009 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23#include <stdio.h>
24#include <stdarg.h>
25#include <stdlib.h>
26#include <string.h>
27#include <assert.h>
28
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <fcntl.h>
32#include <unistd.h>
33
34#include "ast.h"
35#include "glsl_parser_extras.h"
36#include "glsl_parser.tab.h"
37#include "symbol_table.h"
38
39void
40_mesa_glsl_error(YYLTYPE *locp, void *state, const char *fmt, ...)
41{
42 char buf[1024];
43 int len;
44 va_list ap;
45
46 (void) state;
47 len = snprintf(buf, sizeof(buf), "%u:%u(%u): error: ",
48 locp->source, locp->first_line, locp->first_column);
49
50 va_start(ap, fmt);
51 vsnprintf(buf + len, sizeof(buf) - len, fmt, ap);
52 va_end(ap);
53
54 printf("%s\n", buf);
55}
56
57
58ast_node::~ast_node()
59{
60 /* empty */
61}
62
63
64void
65_mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q)
66{
67 if (q->constant)
68 printf("const ");
69
70 if (q->invariant)
71 printf("invariant ");
72
73 if (q->attribute)
74 printf("attribute ");
75
76 if (q->varying)
77 printf("varying ");
78
79 if (q->in && q->out)
80 printf("inout ");
81 else {
82 if (q->in)
83 printf("in ");
84
85 if (q->out)
86 printf("out ");
87 }
88
89 if (q->centroid)
90 printf("centroid ");
91 if (q->uniform)
92 printf("uniform ");
93 if (q->smooth)
94 printf("smooth ");
95 if (q->flat)
96 printf("flat ");
97 if (q->noperspective)
98 printf("noperspective ");
99}
100
101
102void
103ast_node::print(void) const
104{
105 printf("node_%d ", type);
106}
107
108
109ast_node::ast_node(void)
110{
Ian Romanick53d27742010-02-22 13:22:10 -0800111 make_empty_list(this);
Ian Romanicka87ac252010-02-22 13:19:34 -0800112}
113
114void
115ast_type_specifier::print(void) const
116{
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;
167
168 case ast_struct:
169 structure->print();
170 break;
171
172 case ast_type_name: printf("%s ", type_name); break;
173 }
174
175 if (is_array) {
176 printf("[ ");
177
178 if (array_size) {
179 array_size->print();
180 }
181
182 printf("] ");
183 }
184}
185
186static void
187ast_opt_array_size_print(bool is_array, const ast_expression *array_size)
188{
189 if (is_array) {
190 printf("[ ");
191
192 if (array_size)
193 array_size->print();
194
195 printf("] ");
196 }
197}
198
199
200ast_type_specifier::ast_type_specifier(int specifier)
201{
202 type_specifier = ast_types(specifier);
203}
204
205
206void
207ast_compound_statement::print(void) const
208{
209 const struct simple_node *ptr;
210
211 printf("{\n");
212
213 foreach(ptr, & statements) {
214 _mesa_ast_print(ptr);
215 }
216
217 printf("}\n");
218}
219
220
221ast_compound_statement::ast_compound_statement(int new_scope,
222 ast_node *statements)
223{
224 this->new_scope = new_scope;
225 make_empty_list(& this->statements);
226
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
231 * head.
232 */
233 insert_at_tail((struct simple_node *) statements,
234 & this->statements);
235 }
236}
237
238
239void
240ast_expression::print(void) const
241{
Ian Romanicka87ac252010-02-22 13:19:34 -0800242 switch (oper) {
243 case ast_assign:
Ian Romanicka87ac252010-02-22 13:19:34 -0800244 case ast_mul_assign:
245 case ast_div_assign:
246 case ast_mod_assign:
247 case ast_add_assign:
248 case ast_sub_assign:
249 case ast_ls_assign:
250 case ast_rs_assign:
251 case ast_and_assign:
252 case ast_xor_assign:
253 case ast_or_assign:
254 subexpressions[0]->print();
Ian Romanick88349b22010-02-22 19:10:25 -0800255 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800256 subexpressions[1]->print();
257 break;
258
259 case ast_field_selection:
260 subexpressions[0]->print();
261 printf(". %s ", primary_expression.identifier);
262 break;
263
264 case ast_plus:
265 case ast_neg:
266 case ast_bit_not:
267 case ast_logic_not:
268 case ast_pre_inc:
269 case ast_pre_dec:
Ian Romanick88349b22010-02-22 19:10:25 -0800270 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800271 subexpressions[0]->print();
272 break;
273
274 case ast_post_inc:
275 case ast_post_dec:
276 subexpressions[0]->print();
Ian Romanick88349b22010-02-22 19:10:25 -0800277 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800278 break;
279
280 case ast_conditional:
281 subexpressions[0]->print();
282 printf("? ");
283 subexpressions[1]->print();
284 printf(": ");
285 subexpressions[1]->print();
286 break;
287
288 case ast_array_index:
289 subexpressions[0]->print();
290 printf("[ ");
291 subexpressions[1]->print();
292 printf("] ");
293 break;
294
295 case ast_function_call: {
296 ast_expression *parameters = subexpressions[1];
297
298 subexpressions[0]->print();
299 printf("( ");
300
301 if (parameters != NULL) {
302 struct simple_node *ptr;
303
304 parameters->print();
305 foreach (ptr, (struct simple_node *) parameters) {
306 printf(", ");
307 _mesa_ast_print(ptr);
308 }
309 }
310
311 printf(") ");
312 break;
313 }
314
315 case ast_identifier:
316 printf("%s ", primary_expression.identifier);
317 break;
318
319 case ast_int_constant:
320 printf("%d ", primary_expression.int_constant);
321 break;
322
323 case ast_uint_constant:
324 printf("%u ", primary_expression.uint_constant);
325 break;
326
327 case ast_float_constant:
328 printf("%f ", primary_expression.float_constant);
329 break;
330
331 case ast_bool_constant:
332 printf("%s ",
333 primary_expression.bool_constant
334 ? "true" : "false");
335 break;
336
337 case ast_sequence: {
338 struct simple_node *ptr;
339 struct simple_node *const head = first_elem(& expressions);
340
341 printf("( ");
342 foreach (ptr, & expressions) {
343 if (ptr != head)
344 printf(", ");
345
346 _mesa_ast_print(ptr);
347 }
348 printf(") ");
349 break;
350 }
Ian Romanick88349b22010-02-22 19:10:25 -0800351
352 default:
353 assert(0);
354 break;
Ian Romanicka87ac252010-02-22 13:19:34 -0800355 }
356}
357
358ast_expression::ast_expression(int oper,
359 ast_expression *ex0,
360 ast_expression *ex1,
361 ast_expression *ex2)
362{
363 this->oper = ast_operators(oper);
364 this->subexpressions[0] = ex0;
365 this->subexpressions[1] = ex1;
366 this->subexpressions[2] = ex2;
367 make_empty_list(& expressions);
368}
369
370
371void
372ast_expression_statement::print(void) const
373{
374 if (expression)
375 expression->print();
376
377 printf("; ");
378}
379
380
381ast_expression_statement::ast_expression_statement(ast_expression *ex) :
382 expression(ex)
383{
384 /* empty */
385}
386
387
388void
389ast_function::print(void) const
390{
391 struct simple_node *ptr;
392
393 return_type->print();
394 printf(" %s (", identifier);
395
396 foreach(ptr, & parameters) {
397 _mesa_ast_print(ptr);
398 }
399
400 printf(")");
401}
402
403
404ast_function::ast_function(void)
405{
406 make_empty_list(& parameters);
407}
408
409
410void
411ast_fully_specified_type::print(void) const
412{
413 _mesa_ast_type_qualifier_print(& qualifier);
414 specifier->print();
415}
416
417
418void
419ast_parameter_declarator::print(void) const
420{
421 type->print();
422 if (identifier)
423 printf("%s ", identifier);
424 ast_opt_array_size_print(is_array, array_size);
425}
426
427
428void
429ast_function_definition::print(void) const
430{
431 prototype->print();
432 body->print();
433}
434
435
436void
437ast_declaration::print(void) const
438{
439 printf("%s ", identifier);
440 ast_opt_array_size_print(is_array, array_size);
441
442 if (initializer) {
443 printf("= ");
444 initializer->print();
445 }
446}
447
448
449ast_declaration::ast_declaration(char *identifier, int is_array,
450 ast_expression *array_size,
451 ast_expression *initializer)
452{
453 this->identifier = identifier;
454 this->is_array = is_array;
455 this->array_size = array_size;
456 this->initializer = initializer;
457}
458
459
460void
461ast_declarator_list::print(void) const
462{
463 struct simple_node *head;
464 struct simple_node *ptr;
465
466 assert(type || invariant);
467
468 if (type)
469 type->print();
470 else
471 printf("invariant ");
472
473 head = first_elem(& declarations);
474 foreach (ptr, & declarations) {
475 if (ptr != head)
476 printf(", ");
477
478 _mesa_ast_print(ptr);
479 }
480
481 printf("; ");
482}
483
484
485ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type)
486{
487 this->type = type;
488 make_empty_list(& this->declarations);
489}
490
491void
492ast_jump_statement::print(void) const
493{
494 switch (mode) {
495 case ast_continue:
496 printf("continue; ");
497 break;
498 case ast_break:
499 printf("break; ");
500 break;
501 case ast_return:
502 printf("return ");
503 if (opt_return_value)
504 opt_return_value->print();
505
506 printf("; ");
507 break;
508 case ast_discard:
509 printf("discard; ");
510 break;
511 }
512}
513
514
515ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value)
516{
517 this->mode = ast_jump_modes(mode);
518
519 if (mode == ast_return)
520 opt_return_value = return_value;
521}
522
523
524void
525ast_selection_statement::print(void) const
526{
527 printf("if ( ");
528 condition->print();
529 printf(") ");
530
531 then_statement->print();
532
533 if (else_statement) {
534 printf("else ");
535 else_statement->print();
536 }
537
538}
539
540
541ast_selection_statement::ast_selection_statement(ast_expression *condition,
542 ast_node *then_statement,
543 ast_node *else_statement)
544{
545 this->condition = condition;
546 this->then_statement = then_statement;
547 this->else_statement = else_statement;
548}
549
550
551void
552ast_iteration_statement::print(void) const
553{
554 switch (mode) {
555 case ast_for:
556 printf("for( ");
557 if (init_statement)
558 init_statement->print();
559 printf("; ");
560
561 if (condition)
562 condition->print();
563 printf("; ");
564
565 if (rest_expression)
566 rest_expression->print();
567 printf(") ");
568
569 body->print();
570 break;
571
572 case ast_while:
573 printf("while ( ");
574 if (condition)
575 condition->print();
576 printf(") ");
577 body->print();
578 break;
579
580 case ast_do_while:
581 printf("do ");
582 body->print();
583 printf("while ( ");
584 if (condition)
585 condition->print();
586 printf("); ");
587 break;
588 }
589}
590
591
592ast_iteration_statement::ast_iteration_statement(int mode,
593 ast_node *init,
594 ast_node *condition,
595 ast_expression *rest_expression,
596 ast_node *body)
597{
598 this->mode = ast_iteration_modes(mode);
599 this->init_statement = init;
600 this->condition = condition;
601 this->rest_expression = rest_expression;
602 this->body = body;
603}
604
605
606void
607ast_struct_specifier::print(void) const
608{
609 struct simple_node *ptr;
610
611 printf("struct %s { ", name);
612 foreach (ptr, & declarations) {
613 _mesa_ast_print(ptr);
614 }
615 printf("} ");
616}
617
618
619ast_struct_specifier::ast_struct_specifier(char *identifier,
620 ast_node *declarator_list)
621{
622 name = identifier;
623
624 /* This seems odd, but it works. The simple_list is,
625 * basically, a circular list. insert_at_tail adds
626 * the specified node to the list before the current
627 * head.
628 */
629 insert_at_tail((struct simple_node *) declarator_list,
630 & declarations);
631}
632
633
634static char *
635load_text_file(const char *file_name, size_t *size)
636{
637 char *text = NULL;
638 struct stat st;
639 ssize_t total_read = 0;
640 int fd = open(file_name, O_RDONLY);
641
642 *size = 0;
643 if (fd < 0) {
644 return NULL;
645 }
646
647 if (fstat(fd, & st) == 0) {
648 text = (char *) malloc(st.st_size + 1);
649 if (text != NULL) {
650 do {
651 ssize_t bytes = read(fd, text + total_read,
652 st.st_size - total_read);
653 if (bytes < 0) {
654 free(text);
655 text = NULL;
656 break;
657 }
658
659 if (bytes == 0) {
660 break;
661 }
662
663 total_read += bytes;
664 } while (total_read < st.st_size);
665
666 text[total_read] = '\0';
667 *size = total_read;
668 }
669 }
670
671 close(fd);
672
673 return text;
674}
675
676
677int
678main(int argc, char **argv)
679{
680 struct _mesa_glsl_parse_state state;
681 char *shader;
682 size_t shader_len;
683 struct simple_node *ptr;
684 struct simple_node instructions;
685
686 (void) argc;
687 shader = load_text_file(argv[1], & shader_len);
688
689 state.scanner = NULL;
690 make_empty_list(& state.translation_unit);
691 state.symbols = _mesa_symbol_table_ctor();
692
693 _mesa_glsl_lexer_ctor(& state, shader, shader_len);
694 _mesa_glsl_parse(& state);
695 _mesa_glsl_lexer_dtor(& state);
696
697 foreach (ptr, & state.translation_unit) {
698 _mesa_ast_print(ptr);
699 }
700
701#if 0
702 make_empty_list(& instructions);
703 foreach (ptr, & state.translation_unit) {
704 _mesa_ast_to_hir(ptr, &instructions, &state);
705 }
706#endif
707
708 _mesa_symbol_table_dtor(state.symbols);
709
710 return 0;
711}