Remove dead code assignments and variable declarations.
[mesa.git] / glsl_parser_extras.cpp
1 /*
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.h"
37 #include "ir_constant_folding.h"
38 #include "ir_dead_code.h"
39 #include "ir_function_inlining.h"
40 #include "ir_if_simplification.h"
41 #include "ir_print_visitor.h"
42
43 const char *
44 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target)
45 {
46 switch (target) {
47 case vertex_shader: return "vertex";
48 case fragment_shader: return "fragment";
49 case geometry_shader: return "geometry";
50 }
51
52 assert(!"Should not get here.");
53 }
54
55
56 void
57 _mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
58 const char *fmt, ...)
59 {
60 char buf[1024];
61 int len;
62 va_list ap;
63
64 state->error = true;
65
66 len = snprintf(buf, sizeof(buf), "%u:%u(%u): error: ",
67 locp->source, locp->first_line, locp->first_column);
68
69 va_start(ap, fmt);
70 vsnprintf(buf + len, sizeof(buf) - len, fmt, ap);
71 va_end(ap);
72
73 printf("%s\n", buf);
74 }
75
76
77 void
78 _mesa_glsl_warning(const YYLTYPE *locp, const _mesa_glsl_parse_state *state,
79 const char *fmt, ...)
80 {
81 char buf[1024];
82 int len;
83 va_list ap;
84
85 len = snprintf(buf, sizeof(buf), "%u:%u(%u): warning: ",
86 locp->source, locp->first_line, locp->first_column);
87
88 va_start(ap, fmt);
89 vsnprintf(buf + len, sizeof(buf) - len, fmt, ap);
90 va_end(ap);
91
92 printf("%s\n", buf);
93 }
94
95
96 bool
97 _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
98 const char *behavior, YYLTYPE *behavior_locp,
99 _mesa_glsl_parse_state *state)
100 {
101 enum {
102 extension_disable,
103 extension_enable,
104 extension_require,
105 extension_warn
106 } ext_mode;
107
108 if (strcmp(behavior, "warn") == 0) {
109 ext_mode = extension_warn;
110 } else if (strcmp(behavior, "require") == 0) {
111 ext_mode = extension_require;
112 } else if (strcmp(behavior, "enable") == 0) {
113 ext_mode = extension_enable;
114 } else if (strcmp(behavior, "disable") == 0) {
115 ext_mode = extension_disable;
116 } else {
117 _mesa_glsl_error(behavior_locp, state,
118 "Unknown extension behavior `%s'",
119 behavior);
120 return false;
121 }
122
123 bool unsupported = false;
124
125 if (strcmp(name, "all") == 0) {
126 if ((ext_mode == extension_enable) || (ext_mode == extension_require)) {
127 _mesa_glsl_error(name_locp, state, "Cannot %s all extensions",
128 (ext_mode == extension_enable)
129 ? "enable" : "require");
130 return false;
131 }
132 } if (strcmp(name, "GL_ARB_draw_buffers") == 0) {
133 /* This extension is only supported in fragment shaders.
134 */
135 if (state->target != fragment_shader) {
136 unsupported = true;
137 } else {
138 state->ARB_draw_buffers_enable = (ext_mode != extension_disable);
139 state->ARB_draw_buffers_warn = (ext_mode == extension_warn);
140 }
141 } if (strcmp(name, "GL_ARB_texture_rectangle") == 0) {
142 state->ARB_texture_rectangle_enable = (ext_mode != extension_disable);
143 state->ARB_texture_rectangle_warn = (ext_mode == extension_warn);
144 } else {
145 unsupported = true;
146 }
147
148 if (unsupported) {
149 static const char *const fmt = "extension `%s' unsupported in %s shader";
150
151 if (ext_mode == extension_require) {
152 _mesa_glsl_error(name_locp, state, fmt,
153 name, _mesa_glsl_shader_target_name(state->target));
154 return false;
155 } else {
156 _mesa_glsl_warning(name_locp, state, fmt,
157 name, _mesa_glsl_shader_target_name(state->target));
158 }
159 }
160
161 return true;
162 }
163
164
165 ast_node::~ast_node()
166 {
167 /* empty */
168 }
169
170
171 void
172 _mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q)
173 {
174 if (q->constant)
175 printf("const ");
176
177 if (q->invariant)
178 printf("invariant ");
179
180 if (q->attribute)
181 printf("attribute ");
182
183 if (q->varying)
184 printf("varying ");
185
186 if (q->in && q->out)
187 printf("inout ");
188 else {
189 if (q->in)
190 printf("in ");
191
192 if (q->out)
193 printf("out ");
194 }
195
196 if (q->centroid)
197 printf("centroid ");
198 if (q->uniform)
199 printf("uniform ");
200 if (q->smooth)
201 printf("smooth ");
202 if (q->flat)
203 printf("flat ");
204 if (q->noperspective)
205 printf("noperspective ");
206 }
207
208
209 void
210 ast_node::print(void) const
211 {
212 printf("unhandled node ");
213 }
214
215
216 ast_node::ast_node(void)
217 {
218 make_empty_list(this);
219 }
220
221
222 static void
223 ast_opt_array_size_print(bool is_array, const ast_expression *array_size)
224 {
225 if (is_array) {
226 printf("[ ");
227
228 if (array_size)
229 array_size->print();
230
231 printf("] ");
232 }
233 }
234
235
236 void
237 ast_compound_statement::print(void) const
238 {
239 const struct simple_node *ptr;
240
241 printf("{\n");
242
243 foreach(ptr, & statements) {
244 ((ast_node *)ptr)->print();
245 }
246
247 printf("}\n");
248 }
249
250
251 ast_compound_statement::ast_compound_statement(int new_scope,
252 ast_node *statements)
253 {
254 this->new_scope = new_scope;
255 make_empty_list(& this->statements);
256
257 if (statements != NULL) {
258 /* This seems odd, but it works. The simple_list is,
259 * basically, a circular list. insert_at_tail adds
260 * the specified node to the list before the current
261 * head.
262 */
263 insert_at_tail((struct simple_node *) statements,
264 & this->statements);
265 }
266 }
267
268
269 void
270 ast_expression::print(void) const
271 {
272 switch (oper) {
273 case ast_assign:
274 case ast_mul_assign:
275 case ast_div_assign:
276 case ast_mod_assign:
277 case ast_add_assign:
278 case ast_sub_assign:
279 case ast_ls_assign:
280 case ast_rs_assign:
281 case ast_and_assign:
282 case ast_xor_assign:
283 case ast_or_assign:
284 subexpressions[0]->print();
285 printf("%s ", operator_string(oper));
286 subexpressions[1]->print();
287 break;
288
289 case ast_field_selection:
290 subexpressions[0]->print();
291 printf(". %s ", primary_expression.identifier);
292 break;
293
294 case ast_plus:
295 case ast_neg:
296 case ast_bit_not:
297 case ast_logic_not:
298 case ast_pre_inc:
299 case ast_pre_dec:
300 printf("%s ", operator_string(oper));
301 subexpressions[0]->print();
302 break;
303
304 case ast_post_inc:
305 case ast_post_dec:
306 subexpressions[0]->print();
307 printf("%s ", operator_string(oper));
308 break;
309
310 case ast_conditional:
311 subexpressions[0]->print();
312 printf("? ");
313 subexpressions[1]->print();
314 printf(": ");
315 subexpressions[1]->print();
316 break;
317
318 case ast_array_index:
319 subexpressions[0]->print();
320 printf("[ ");
321 subexpressions[1]->print();
322 printf("] ");
323 break;
324
325 case ast_function_call: {
326 ast_expression *parameters = subexpressions[1];
327
328 subexpressions[0]->print();
329 printf("( ");
330
331 if (parameters != NULL) {
332 struct simple_node *ptr;
333
334 parameters->print();
335 foreach (ptr, (struct simple_node *) parameters) {
336 printf(", ");
337 ((ast_node *)ptr)->print();
338 }
339 }
340
341 printf(") ");
342 break;
343 }
344
345 case ast_identifier:
346 printf("%s ", primary_expression.identifier);
347 break;
348
349 case ast_int_constant:
350 printf("%d ", primary_expression.int_constant);
351 break;
352
353 case ast_uint_constant:
354 printf("%u ", primary_expression.uint_constant);
355 break;
356
357 case ast_float_constant:
358 printf("%f ", primary_expression.float_constant);
359 break;
360
361 case ast_bool_constant:
362 printf("%s ",
363 primary_expression.bool_constant
364 ? "true" : "false");
365 break;
366
367 case ast_sequence: {
368 struct simple_node *ptr;
369 struct simple_node *const head = first_elem(& expressions);
370
371 printf("( ");
372 foreach (ptr, & expressions) {
373 if (ptr != head)
374 printf(", ");
375
376 ((ast_node *)ptr)->print();
377 }
378 printf(") ");
379 break;
380 }
381
382 default:
383 assert(0);
384 break;
385 }
386 }
387
388 ast_expression::ast_expression(int oper,
389 ast_expression *ex0,
390 ast_expression *ex1,
391 ast_expression *ex2)
392 {
393 this->oper = ast_operators(oper);
394 this->subexpressions[0] = ex0;
395 this->subexpressions[1] = ex1;
396 this->subexpressions[2] = ex2;
397 make_empty_list(& expressions);
398 }
399
400
401 void
402 ast_expression_statement::print(void) const
403 {
404 if (expression)
405 expression->print();
406
407 printf("; ");
408 }
409
410
411 ast_expression_statement::ast_expression_statement(ast_expression *ex) :
412 expression(ex)
413 {
414 /* empty */
415 }
416
417
418 void
419 ast_function::print(void) const
420 {
421 struct simple_node *ptr;
422
423 return_type->print();
424 printf(" %s (", identifier);
425
426 foreach(ptr, & parameters) {
427 ((ast_node *)ptr)->print();
428 }
429
430 printf(")");
431 }
432
433
434 ast_function::ast_function(void)
435 : is_definition(false), signature(NULL)
436 {
437 make_empty_list(& parameters);
438 }
439
440
441 void
442 ast_fully_specified_type::print(void) const
443 {
444 _mesa_ast_type_qualifier_print(& qualifier);
445 specifier->print();
446 }
447
448
449 void
450 ast_parameter_declarator::print(void) const
451 {
452 type->print();
453 if (identifier)
454 printf("%s ", identifier);
455 ast_opt_array_size_print(is_array, array_size);
456 }
457
458
459 void
460 ast_function_definition::print(void) const
461 {
462 prototype->print();
463 body->print();
464 }
465
466
467 void
468 ast_declaration::print(void) const
469 {
470 printf("%s ", identifier);
471 ast_opt_array_size_print(is_array, array_size);
472
473 if (initializer) {
474 printf("= ");
475 initializer->print();
476 }
477 }
478
479
480 ast_declaration::ast_declaration(char *identifier, int is_array,
481 ast_expression *array_size,
482 ast_expression *initializer)
483 {
484 this->identifier = identifier;
485 this->is_array = is_array;
486 this->array_size = array_size;
487 this->initializer = initializer;
488 }
489
490
491 void
492 ast_declarator_list::print(void) const
493 {
494 struct simple_node *head;
495 struct simple_node *ptr;
496
497 assert(type || invariant);
498
499 if (type)
500 type->print();
501 else
502 printf("invariant ");
503
504 head = first_elem(& declarations);
505 foreach (ptr, & declarations) {
506 if (ptr != head)
507 printf(", ");
508
509 ((ast_node *)ptr)->print();
510 }
511
512 printf("; ");
513 }
514
515
516 ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type)
517 {
518 this->type = type;
519 make_empty_list(& this->declarations);
520 }
521
522 void
523 ast_jump_statement::print(void) const
524 {
525 switch (mode) {
526 case ast_continue:
527 printf("continue; ");
528 break;
529 case ast_break:
530 printf("break; ");
531 break;
532 case ast_return:
533 printf("return ");
534 if (opt_return_value)
535 opt_return_value->print();
536
537 printf("; ");
538 break;
539 case ast_discard:
540 printf("discard; ");
541 break;
542 }
543 }
544
545
546 ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value)
547 {
548 this->mode = ast_jump_modes(mode);
549
550 if (mode == ast_return)
551 opt_return_value = return_value;
552 }
553
554
555 void
556 ast_selection_statement::print(void) const
557 {
558 printf("if ( ");
559 condition->print();
560 printf(") ");
561
562 then_statement->print();
563
564 if (else_statement) {
565 printf("else ");
566 else_statement->print();
567 }
568
569 }
570
571
572 ast_selection_statement::ast_selection_statement(ast_expression *condition,
573 ast_node *then_statement,
574 ast_node *else_statement)
575 {
576 this->condition = condition;
577 this->then_statement = then_statement;
578 this->else_statement = else_statement;
579 }
580
581
582 void
583 ast_iteration_statement::print(void) const
584 {
585 switch (mode) {
586 case ast_for:
587 printf("for( ");
588 if (init_statement)
589 init_statement->print();
590 printf("; ");
591
592 if (condition)
593 condition->print();
594 printf("; ");
595
596 if (rest_expression)
597 rest_expression->print();
598 printf(") ");
599
600 body->print();
601 break;
602
603 case ast_while:
604 printf("while ( ");
605 if (condition)
606 condition->print();
607 printf(") ");
608 body->print();
609 break;
610
611 case ast_do_while:
612 printf("do ");
613 body->print();
614 printf("while ( ");
615 if (condition)
616 condition->print();
617 printf("); ");
618 break;
619 }
620 }
621
622
623 ast_iteration_statement::ast_iteration_statement(int mode,
624 ast_node *init,
625 ast_node *condition,
626 ast_expression *rest_expression,
627 ast_node *body)
628 {
629 this->mode = ast_iteration_modes(mode);
630 this->init_statement = init;
631 this->condition = condition;
632 this->rest_expression = rest_expression;
633 this->body = body;
634 }
635
636
637 void
638 ast_struct_specifier::print(void) const
639 {
640 struct simple_node *ptr;
641
642 printf("struct %s { ", name);
643 foreach (ptr, & declarations) {
644 ((ast_node *)ptr)->print();
645 }
646 printf("} ");
647 }
648
649
650 ast_struct_specifier::ast_struct_specifier(char *identifier,
651 ast_node *declarator_list)
652 {
653 name = identifier;
654
655 /* This seems odd, but it works. The simple_list is,
656 * basically, a circular list. insert_at_tail adds
657 * the specified node to the list before the current
658 * head.
659 */
660 insert_at_tail((struct simple_node *) declarator_list,
661 & declarations);
662 }
663
664
665 static char *
666 load_text_file(const char *file_name, size_t *size)
667 {
668 char *text = NULL;
669 struct stat st;
670 ssize_t total_read = 0;
671 int fd = open(file_name, O_RDONLY);
672
673 *size = 0;
674 if (fd < 0) {
675 return NULL;
676 }
677
678 if (fstat(fd, & st) == 0) {
679 text = (char *) malloc(st.st_size + 1);
680 if (text != NULL) {
681 do {
682 ssize_t bytes = read(fd, text + total_read,
683 st.st_size - total_read);
684 if (bytes < 0) {
685 free(text);
686 text = NULL;
687 break;
688 }
689
690 if (bytes == 0) {
691 break;
692 }
693
694 total_read += bytes;
695 } while (total_read < st.st_size);
696
697 text[total_read] = '\0';
698 *size = total_read;
699 }
700 }
701
702 close(fd);
703
704 return text;
705 }
706
707
708 int
709 main(int argc, char **argv)
710 {
711 struct _mesa_glsl_parse_state state;
712 char *shader;
713 size_t shader_len;
714 struct simple_node *ptr;
715 exec_list instructions;
716
717 if (argc < 3) {
718 printf("Usage: %s [v|g|f] <shader_file>\n", argv[0]);
719 return EXIT_FAILURE;
720 }
721
722 switch (argv[1][0]) {
723 case 'v':
724 state.target = vertex_shader;
725 break;
726 case 'g':
727 state.target = geometry_shader;
728 break;
729 case 'f':
730 state.target = fragment_shader;
731 break;
732 default:
733 printf("Usage: %s [v|g|f] <shader_file>\n", argv[0]);
734 return EXIT_FAILURE;
735 }
736
737 shader = load_text_file(argv[2], & shader_len);
738
739 state.scanner = NULL;
740 make_empty_list(& state.translation_unit);
741 state.symbols = new glsl_symbol_table;
742 state.error = false;
743 state.temp_index = 0;
744 state.loop_or_switch_nesting = NULL;
745 state.ARB_texture_rectangle_enable = true;
746
747 _mesa_glsl_lexer_ctor(& state, shader, shader_len);
748 _mesa_glsl_parse(& state);
749 _mesa_glsl_lexer_dtor(& state);
750
751 foreach (ptr, & state.translation_unit) {
752 ((ast_node *)ptr)->print();
753 }
754
755 _mesa_ast_to_hir(&instructions, &state);
756
757 /* Optimization passes */
758 if (!state.error) {
759 bool progress;
760 do {
761 progress = false;
762
763 progress = do_function_inlining(&instructions) || progress;
764 progress = do_if_simplification(&instructions) || progress;
765 progress = do_dead_code_unlinked(&instructions) || progress;
766
767 /* Constant folding */
768 ir_constant_folding_visitor constant_folding;
769 visit_exec_list(&instructions, &constant_folding);
770 } while (progress);
771 }
772
773 /* Print out the resulting IR */
774 printf("\n\n");
775
776 if (!state.error) {
777 foreach_iter(exec_list_iterator, iter, instructions) {
778 ir_print_visitor v;
779
780 ((ir_instruction *)iter.get())->accept(& v);
781 printf("\n");
782 }
783 }
784
785 delete state.symbols;
786
787 return state.error != 0;
788 }