blob: 553cd675a7a1a714f8487bb03e394ae07f5dd218 [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"
Ian Romanickd59673c2010-02-25 17:17:23 -080036#include "glsl_parser.h"
Eric Anholt62735692010-04-05 15:24:28 -070037#include "ir_constant_folding.h"
Ian Romanick1c4156f2010-03-10 09:27:03 -080038#include "ir_print_visitor.h"
Ian Romanicka87ac252010-02-22 13:19:34 -080039
Ian Romanick5bfe30a2010-04-07 16:44:30 -070040const char *
41_mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target)
42{
43 switch (target) {
44 case vertex_shader: return "vertex";
45 case fragment_shader: return "fragment";
46 case geometry_shader: return "geometry";
47 }
48
49 assert(!"Should not get here.");
50}
51
52
Ian Romanicka87ac252010-02-22 13:19:34 -080053void
Ian Romanick1f585182010-03-11 14:08:33 -080054_mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
55 const char *fmt, ...)
Ian Romanicka87ac252010-02-22 13:19:34 -080056{
57 char buf[1024];
58 int len;
59 va_list ap;
60
Ian Romanick71d0bbf2010-03-23 13:21:19 -070061 state->error = true;
Ian Romanick1f585182010-03-11 14:08:33 -080062
Ian Romanicka87ac252010-02-22 13:19:34 -080063 len = snprintf(buf, sizeof(buf), "%u:%u(%u): error: ",
64 locp->source, locp->first_line, locp->first_column);
65
66 va_start(ap, fmt);
67 vsnprintf(buf + len, sizeof(buf) - len, fmt, ap);
68 va_end(ap);
69
70 printf("%s\n", buf);
71}
72
73
Ian Romanick56b8b212010-04-07 14:47:46 -070074void
75_mesa_glsl_warning(const YYLTYPE *locp, const _mesa_glsl_parse_state *state,
76 const char *fmt, ...)
77{
78 char buf[1024];
79 int len;
80 va_list ap;
81
82 len = snprintf(buf, sizeof(buf), "%u:%u(%u): warning: ",
83 locp->source, locp->first_line, locp->first_column);
84
85 va_start(ap, fmt);
86 vsnprintf(buf + len, sizeof(buf) - len, fmt, ap);
87 va_end(ap);
88
89 printf("%s\n", buf);
90}
91
92
Ian Romanicke7017612010-04-07 16:46:25 -070093bool
94_mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
95 const char *behavior, YYLTYPE *behavior_locp,
96 _mesa_glsl_parse_state *state)
97{
98 enum {
99 extension_disable,
100 extension_enable,
101 extension_require,
102 extension_warn
103 } ext_mode;
Ian Romanicke7017612010-04-07 16:46:25 -0700104
105 if (strcmp(behavior, "warn") == 0) {
106 ext_mode = extension_warn;
107 } else if (strcmp(behavior, "require") == 0) {
108 ext_mode = extension_require;
109 } else if (strcmp(behavior, "enable") == 0) {
110 ext_mode = extension_enable;
111 } else if (strcmp(behavior, "disable") == 0) {
112 ext_mode = extension_disable;
113 } else {
114 _mesa_glsl_error(behavior_locp, state,
115 "Unknown extension behavior `%s'",
116 behavior);
117 return false;
118 }
119
Ian Romanick887a8b02010-04-07 16:57:56 -0700120 bool unsupported = false;
121
Ian Romanicke7017612010-04-07 16:46:25 -0700122 if (strcmp(name, "all") == 0) {
123 if ((ext_mode == extension_enable) || (ext_mode == extension_require)) {
124 _mesa_glsl_error(name_locp, state, "Cannot %s all extensions",
125 (ext_mode == extension_enable)
126 ? "enable" : "require");
127 return false;
128 }
Ian Romanickc77b2572010-04-07 16:59:46 -0700129 } if (strcmp(name, "GL_ARB_draw_buffers") == 0) {
130 /* This extension is only supported in fragment shaders.
131 */
132 if (state->target != fragment_shader) {
133 unsupported = true;
134 } else {
135 state->ARB_draw_buffers_enable = (ext_mode != extension_disable);
136 state->ARB_draw_buffers_warn = (ext_mode == extension_warn);
137 }
Ian Romanicke7017612010-04-07 16:46:25 -0700138 } else {
Ian Romanick887a8b02010-04-07 16:57:56 -0700139 unsupported = true;
140 }
141
142 if (unsupported) {
143 static const char *const fmt = "extension `%s' unsupported in %s shader";
144
Ian Romanicke7017612010-04-07 16:46:25 -0700145 if (ext_mode == extension_require) {
Ian Romanick887a8b02010-04-07 16:57:56 -0700146 _mesa_glsl_error(name_locp, state, fmt,
147 name, _mesa_glsl_shader_target_name(state->target));
Ian Romanicke7017612010-04-07 16:46:25 -0700148 return false;
Ian Romanick1799a0c2010-04-07 14:50:36 -0700149 } else {
Ian Romanick887a8b02010-04-07 16:57:56 -0700150 _mesa_glsl_warning(name_locp, state, fmt,
151 name, _mesa_glsl_shader_target_name(state->target));
Ian Romanicke7017612010-04-07 16:46:25 -0700152 }
153 }
154
155 return true;
156}
157
158
Ian Romanicka87ac252010-02-22 13:19:34 -0800159ast_node::~ast_node()
160{
161 /* empty */
162}
163
164
165void
166_mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q)
167{
168 if (q->constant)
169 printf("const ");
170
171 if (q->invariant)
172 printf("invariant ");
173
174 if (q->attribute)
175 printf("attribute ");
176
177 if (q->varying)
178 printf("varying ");
179
180 if (q->in && q->out)
181 printf("inout ");
182 else {
183 if (q->in)
184 printf("in ");
185
186 if (q->out)
187 printf("out ");
188 }
189
190 if (q->centroid)
191 printf("centroid ");
192 if (q->uniform)
193 printf("uniform ");
194 if (q->smooth)
195 printf("smooth ");
196 if (q->flat)
197 printf("flat ");
198 if (q->noperspective)
199 printf("noperspective ");
200}
201
202
203void
204ast_node::print(void) const
205{
Ian Romanick03d3f3a2010-04-02 11:03:47 -0700206 printf("unhandled node ");
Ian Romanicka87ac252010-02-22 13:19:34 -0800207}
208
209
210ast_node::ast_node(void)
211{
Ian Romanick53d27742010-02-22 13:22:10 -0800212 make_empty_list(this);
Ian Romanicka87ac252010-02-22 13:19:34 -0800213}
214
Ian Romanicka87ac252010-02-22 13:19:34 -0800215
216static void
217ast_opt_array_size_print(bool is_array, const ast_expression *array_size)
218{
219 if (is_array) {
220 printf("[ ");
221
222 if (array_size)
223 array_size->print();
224
225 printf("] ");
226 }
227}
228
229
Ian Romanicka87ac252010-02-22 13:19:34 -0800230void
231ast_compound_statement::print(void) const
232{
233 const struct simple_node *ptr;
234
235 printf("{\n");
236
237 foreach(ptr, & statements) {
Ian Romanicke41a1cd2010-02-25 12:49:55 -0800238 ((ast_node *)ptr)->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800239 }
240
241 printf("}\n");
242}
243
244
245ast_compound_statement::ast_compound_statement(int new_scope,
246 ast_node *statements)
247{
248 this->new_scope = new_scope;
249 make_empty_list(& this->statements);
250
251 if (statements != NULL) {
252 /* This seems odd, but it works. The simple_list is,
253 * basically, a circular list. insert_at_tail adds
254 * the specified node to the list before the current
255 * head.
256 */
257 insert_at_tail((struct simple_node *) statements,
258 & this->statements);
259 }
260}
261
262
263void
264ast_expression::print(void) const
265{
Ian Romanicka87ac252010-02-22 13:19:34 -0800266 switch (oper) {
267 case ast_assign:
Ian Romanicka87ac252010-02-22 13:19:34 -0800268 case ast_mul_assign:
269 case ast_div_assign:
270 case ast_mod_assign:
271 case ast_add_assign:
272 case ast_sub_assign:
273 case ast_ls_assign:
274 case ast_rs_assign:
275 case ast_and_assign:
276 case ast_xor_assign:
277 case ast_or_assign:
278 subexpressions[0]->print();
Ian Romanick88349b22010-02-22 19:10:25 -0800279 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800280 subexpressions[1]->print();
281 break;
282
283 case ast_field_selection:
284 subexpressions[0]->print();
285 printf(". %s ", primary_expression.identifier);
286 break;
287
288 case ast_plus:
289 case ast_neg:
290 case ast_bit_not:
291 case ast_logic_not:
292 case ast_pre_inc:
293 case ast_pre_dec:
Ian Romanick88349b22010-02-22 19:10:25 -0800294 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800295 subexpressions[0]->print();
296 break;
297
298 case ast_post_inc:
299 case ast_post_dec:
300 subexpressions[0]->print();
Ian Romanick88349b22010-02-22 19:10:25 -0800301 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800302 break;
303
304 case ast_conditional:
305 subexpressions[0]->print();
306 printf("? ");
307 subexpressions[1]->print();
308 printf(": ");
309 subexpressions[1]->print();
310 break;
311
312 case ast_array_index:
313 subexpressions[0]->print();
314 printf("[ ");
315 subexpressions[1]->print();
316 printf("] ");
317 break;
318
319 case ast_function_call: {
320 ast_expression *parameters = subexpressions[1];
321
322 subexpressions[0]->print();
323 printf("( ");
324
325 if (parameters != NULL) {
326 struct simple_node *ptr;
327
328 parameters->print();
329 foreach (ptr, (struct simple_node *) parameters) {
330 printf(", ");
Ian Romanicke41a1cd2010-02-25 12:49:55 -0800331 ((ast_node *)ptr)->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800332 }
333 }
334
335 printf(") ");
336 break;
337 }
338
339 case ast_identifier:
340 printf("%s ", primary_expression.identifier);
341 break;
342
343 case ast_int_constant:
344 printf("%d ", primary_expression.int_constant);
345 break;
346
347 case ast_uint_constant:
348 printf("%u ", primary_expression.uint_constant);
349 break;
350
351 case ast_float_constant:
352 printf("%f ", primary_expression.float_constant);
353 break;
354
355 case ast_bool_constant:
356 printf("%s ",
357 primary_expression.bool_constant
358 ? "true" : "false");
359 break;
360
361 case ast_sequence: {
362 struct simple_node *ptr;
363 struct simple_node *const head = first_elem(& expressions);
364
365 printf("( ");
366 foreach (ptr, & expressions) {
367 if (ptr != head)
368 printf(", ");
369
Ian Romanicke41a1cd2010-02-25 12:49:55 -0800370 ((ast_node *)ptr)->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800371 }
372 printf(") ");
373 break;
374 }
Ian Romanick88349b22010-02-22 19:10:25 -0800375
376 default:
377 assert(0);
378 break;
Ian Romanicka87ac252010-02-22 13:19:34 -0800379 }
380}
381
382ast_expression::ast_expression(int oper,
383 ast_expression *ex0,
384 ast_expression *ex1,
385 ast_expression *ex2)
386{
387 this->oper = ast_operators(oper);
388 this->subexpressions[0] = ex0;
389 this->subexpressions[1] = ex1;
390 this->subexpressions[2] = ex2;
391 make_empty_list(& expressions);
392}
393
394
395void
396ast_expression_statement::print(void) const
397{
398 if (expression)
399 expression->print();
400
401 printf("; ");
402}
403
404
405ast_expression_statement::ast_expression_statement(ast_expression *ex) :
406 expression(ex)
407{
408 /* empty */
409}
410
411
412void
413ast_function::print(void) const
414{
415 struct simple_node *ptr;
416
417 return_type->print();
418 printf(" %s (", identifier);
419
420 foreach(ptr, & parameters) {
Ian Romanicke41a1cd2010-02-25 12:49:55 -0800421 ((ast_node *)ptr)->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800422 }
423
424 printf(")");
425}
426
427
428ast_function::ast_function(void)
Ian Romanick92318a92010-03-31 18:23:21 -0700429 : is_definition(false), signature(NULL)
Ian Romanicka87ac252010-02-22 13:19:34 -0800430{
431 make_empty_list(& parameters);
432}
433
434
435void
436ast_fully_specified_type::print(void) const
437{
438 _mesa_ast_type_qualifier_print(& qualifier);
439 specifier->print();
440}
441
442
443void
444ast_parameter_declarator::print(void) const
445{
446 type->print();
447 if (identifier)
448 printf("%s ", identifier);
449 ast_opt_array_size_print(is_array, array_size);
450}
451
452
453void
454ast_function_definition::print(void) const
455{
456 prototype->print();
457 body->print();
458}
459
460
461void
462ast_declaration::print(void) const
463{
464 printf("%s ", identifier);
465 ast_opt_array_size_print(is_array, array_size);
466
467 if (initializer) {
468 printf("= ");
469 initializer->print();
470 }
471}
472
473
474ast_declaration::ast_declaration(char *identifier, int is_array,
475 ast_expression *array_size,
476 ast_expression *initializer)
477{
478 this->identifier = identifier;
479 this->is_array = is_array;
480 this->array_size = array_size;
481 this->initializer = initializer;
482}
483
484
485void
486ast_declarator_list::print(void) const
487{
488 struct simple_node *head;
489 struct simple_node *ptr;
490
491 assert(type || invariant);
492
493 if (type)
494 type->print();
495 else
496 printf("invariant ");
497
498 head = first_elem(& declarations);
499 foreach (ptr, & declarations) {
500 if (ptr != head)
501 printf(", ");
502
Ian Romanicke41a1cd2010-02-25 12:49:55 -0800503 ((ast_node *)ptr)->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800504 }
505
506 printf("; ");
507}
508
509
510ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type)
511{
512 this->type = type;
513 make_empty_list(& this->declarations);
514}
515
516void
517ast_jump_statement::print(void) const
518{
519 switch (mode) {
520 case ast_continue:
521 printf("continue; ");
522 break;
523 case ast_break:
524 printf("break; ");
525 break;
526 case ast_return:
527 printf("return ");
528 if (opt_return_value)
529 opt_return_value->print();
530
531 printf("; ");
532 break;
533 case ast_discard:
534 printf("discard; ");
535 break;
536 }
537}
538
539
540ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value)
541{
542 this->mode = ast_jump_modes(mode);
543
544 if (mode == ast_return)
545 opt_return_value = return_value;
546}
547
548
549void
550ast_selection_statement::print(void) const
551{
552 printf("if ( ");
553 condition->print();
554 printf(") ");
555
556 then_statement->print();
557
558 if (else_statement) {
559 printf("else ");
560 else_statement->print();
561 }
562
563}
564
565
566ast_selection_statement::ast_selection_statement(ast_expression *condition,
567 ast_node *then_statement,
568 ast_node *else_statement)
569{
570 this->condition = condition;
571 this->then_statement = then_statement;
572 this->else_statement = else_statement;
573}
574
575
576void
577ast_iteration_statement::print(void) const
578{
579 switch (mode) {
580 case ast_for:
581 printf("for( ");
582 if (init_statement)
583 init_statement->print();
584 printf("; ");
585
586 if (condition)
587 condition->print();
588 printf("; ");
589
590 if (rest_expression)
591 rest_expression->print();
592 printf(") ");
593
594 body->print();
595 break;
596
597 case ast_while:
598 printf("while ( ");
599 if (condition)
600 condition->print();
601 printf(") ");
602 body->print();
603 break;
604
605 case ast_do_while:
606 printf("do ");
607 body->print();
608 printf("while ( ");
609 if (condition)
610 condition->print();
611 printf("); ");
612 break;
613 }
614}
615
616
617ast_iteration_statement::ast_iteration_statement(int mode,
618 ast_node *init,
619 ast_node *condition,
620 ast_expression *rest_expression,
621 ast_node *body)
622{
623 this->mode = ast_iteration_modes(mode);
624 this->init_statement = init;
625 this->condition = condition;
626 this->rest_expression = rest_expression;
627 this->body = body;
628}
629
630
631void
632ast_struct_specifier::print(void) const
633{
634 struct simple_node *ptr;
635
636 printf("struct %s { ", name);
637 foreach (ptr, & declarations) {
Ian Romanicke41a1cd2010-02-25 12:49:55 -0800638 ((ast_node *)ptr)->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800639 }
640 printf("} ");
641}
642
643
644ast_struct_specifier::ast_struct_specifier(char *identifier,
645 ast_node *declarator_list)
646{
647 name = identifier;
648
649 /* This seems odd, but it works. The simple_list is,
650 * basically, a circular list. insert_at_tail adds
651 * the specified node to the list before the current
652 * head.
653 */
654 insert_at_tail((struct simple_node *) declarator_list,
655 & declarations);
656}
657
658
659static char *
660load_text_file(const char *file_name, size_t *size)
661{
662 char *text = NULL;
663 struct stat st;
664 ssize_t total_read = 0;
665 int fd = open(file_name, O_RDONLY);
666
667 *size = 0;
668 if (fd < 0) {
669 return NULL;
670 }
671
672 if (fstat(fd, & st) == 0) {
673 text = (char *) malloc(st.st_size + 1);
674 if (text != NULL) {
675 do {
676 ssize_t bytes = read(fd, text + total_read,
677 st.st_size - total_read);
678 if (bytes < 0) {
679 free(text);
680 text = NULL;
681 break;
682 }
683
684 if (bytes == 0) {
685 break;
686 }
687
688 total_read += bytes;
689 } while (total_read < st.st_size);
690
691 text[total_read] = '\0';
692 *size = total_read;
693 }
694 }
695
696 close(fd);
697
698 return text;
699}
700
701
702int
703main(int argc, char **argv)
704{
705 struct _mesa_glsl_parse_state state;
706 char *shader;
707 size_t shader_len;
708 struct simple_node *ptr;
Ian Romanick0044e7e2010-03-08 23:44:00 -0800709 exec_list instructions;
Ian Romanicka87ac252010-02-22 13:19:34 -0800710
Ian Romanick8e6cd3b2010-03-10 09:31:30 -0800711 if (argc < 3) {
712 printf("Usage: %s [v|g|f] <shader_file>\n", argv[0]);
713 return EXIT_FAILURE;
714 }
715
716 switch (argv[1][0]) {
717 case 'v':
718 state.target = vertex_shader;
719 break;
720 case 'g':
721 state.target = geometry_shader;
722 break;
723 case 'f':
724 state.target = fragment_shader;
725 break;
726 default:
727 printf("Usage: %s [v|g|f] <shader_file>\n", argv[0]);
728 return EXIT_FAILURE;
729 }
730
731 shader = load_text_file(argv[2], & shader_len);
Ian Romanicka87ac252010-02-22 13:19:34 -0800732
733 state.scanner = NULL;
734 make_empty_list(& state.translation_unit);
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700735 state.symbols = new glsl_symbol_table;
Ian Romanick1f585182010-03-11 14:08:33 -0800736 state.error = false;
Ian Romanick5185a5f2010-03-29 15:20:42 -0700737 state.temp_index = 0;
Ian Romanicke9d0f262010-04-05 17:01:53 -0700738 state.loop_or_switch_nesting = NULL;
Ian Romanicka87ac252010-02-22 13:19:34 -0800739
740 _mesa_glsl_lexer_ctor(& state, shader, shader_len);
741 _mesa_glsl_parse(& state);
742 _mesa_glsl_lexer_dtor(& state);
743
744 foreach (ptr, & state.translation_unit) {
Ian Romanicke41a1cd2010-02-25 12:49:55 -0800745 ((ast_node *)ptr)->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800746 }
747
Ian Romanickd949a9a2010-03-10 09:55:22 -0800748 _mesa_ast_to_hir(&instructions, &state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800749
Eric Anholt62735692010-04-05 15:24:28 -0700750 /* Optimization passes */
751 if (!state.error) {
752 /* Constant folding */
Eric Anholt70b74922010-04-06 11:52:09 -0700753 ir_constant_folding_visitor constant_folding;
754 visit_exec_list(&instructions, &constant_folding);
Eric Anholt62735692010-04-05 15:24:28 -0700755 }
756
757 /* Print out the resulting IR */
Ian Romanick1c4156f2010-03-10 09:27:03 -0800758 printf("\n\n");
Ian Romanick1c4156f2010-03-10 09:27:03 -0800759
Ian Romanick1f585182010-03-11 14:08:33 -0800760 if (!state.error) {
761 foreach_iter(exec_list_iterator, iter, instructions) {
762 ir_print_visitor v;
763
764 ((ir_instruction *)iter.get())->accept(& v);
Ian Romanickd1464272010-03-25 18:29:25 -0700765 printf("\n");
Ian Romanick1f585182010-03-11 14:08:33 -0800766 }
Ian Romanick1c4156f2010-03-10 09:27:03 -0800767 }
768
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700769 delete state.symbols;
Ian Romanicka87ac252010-02-22 13:19:34 -0800770
Eric Anholt7c15bb22010-03-25 14:37:25 -0700771 return state.error != 0;
Ian Romanicka87ac252010-02-22 13:19:34 -0800772}