blob: 6cd69c8b8826fd0ba685243a6ef2e2c34aa7af71 [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 }
129 } else {
Ian Romanick887a8b02010-04-07 16:57:56 -0700130 unsupported = true;
131 }
132
133 if (unsupported) {
134 static const char *const fmt = "extension `%s' unsupported in %s shader";
135
Ian Romanicke7017612010-04-07 16:46:25 -0700136 if (ext_mode == extension_require) {
Ian Romanick887a8b02010-04-07 16:57:56 -0700137 _mesa_glsl_error(name_locp, state, fmt,
138 name, _mesa_glsl_shader_target_name(state->target));
Ian Romanicke7017612010-04-07 16:46:25 -0700139 return false;
Ian Romanick1799a0c2010-04-07 14:50:36 -0700140 } else {
Ian Romanick887a8b02010-04-07 16:57:56 -0700141 _mesa_glsl_warning(name_locp, state, fmt,
142 name, _mesa_glsl_shader_target_name(state->target));
Ian Romanicke7017612010-04-07 16:46:25 -0700143 }
144 }
145
146 return true;
147}
148
149
Ian Romanicka87ac252010-02-22 13:19:34 -0800150ast_node::~ast_node()
151{
152 /* empty */
153}
154
155
156void
157_mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q)
158{
159 if (q->constant)
160 printf("const ");
161
162 if (q->invariant)
163 printf("invariant ");
164
165 if (q->attribute)
166 printf("attribute ");
167
168 if (q->varying)
169 printf("varying ");
170
171 if (q->in && q->out)
172 printf("inout ");
173 else {
174 if (q->in)
175 printf("in ");
176
177 if (q->out)
178 printf("out ");
179 }
180
181 if (q->centroid)
182 printf("centroid ");
183 if (q->uniform)
184 printf("uniform ");
185 if (q->smooth)
186 printf("smooth ");
187 if (q->flat)
188 printf("flat ");
189 if (q->noperspective)
190 printf("noperspective ");
191}
192
193
194void
195ast_node::print(void) const
196{
Ian Romanick03d3f3a2010-04-02 11:03:47 -0700197 printf("unhandled node ");
Ian Romanicka87ac252010-02-22 13:19:34 -0800198}
199
200
201ast_node::ast_node(void)
202{
Ian Romanick53d27742010-02-22 13:22:10 -0800203 make_empty_list(this);
Ian Romanicka87ac252010-02-22 13:19:34 -0800204}
205
Ian Romanicka87ac252010-02-22 13:19:34 -0800206
207static void
208ast_opt_array_size_print(bool is_array, const ast_expression *array_size)
209{
210 if (is_array) {
211 printf("[ ");
212
213 if (array_size)
214 array_size->print();
215
216 printf("] ");
217 }
218}
219
220
Ian Romanicka87ac252010-02-22 13:19:34 -0800221void
222ast_compound_statement::print(void) const
223{
224 const struct simple_node *ptr;
225
226 printf("{\n");
227
228 foreach(ptr, & statements) {
Ian Romanicke41a1cd2010-02-25 12:49:55 -0800229 ((ast_node *)ptr)->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800230 }
231
232 printf("}\n");
233}
234
235
236ast_compound_statement::ast_compound_statement(int new_scope,
237 ast_node *statements)
238{
239 this->new_scope = new_scope;
240 make_empty_list(& this->statements);
241
242 if (statements != NULL) {
243 /* This seems odd, but it works. The simple_list is,
244 * basically, a circular list. insert_at_tail adds
245 * the specified node to the list before the current
246 * head.
247 */
248 insert_at_tail((struct simple_node *) statements,
249 & this->statements);
250 }
251}
252
253
254void
255ast_expression::print(void) const
256{
Ian Romanicka87ac252010-02-22 13:19:34 -0800257 switch (oper) {
258 case ast_assign:
Ian Romanicka87ac252010-02-22 13:19:34 -0800259 case ast_mul_assign:
260 case ast_div_assign:
261 case ast_mod_assign:
262 case ast_add_assign:
263 case ast_sub_assign:
264 case ast_ls_assign:
265 case ast_rs_assign:
266 case ast_and_assign:
267 case ast_xor_assign:
268 case ast_or_assign:
269 subexpressions[0]->print();
Ian Romanick88349b22010-02-22 19:10:25 -0800270 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800271 subexpressions[1]->print();
272 break;
273
274 case ast_field_selection:
275 subexpressions[0]->print();
276 printf(". %s ", primary_expression.identifier);
277 break;
278
279 case ast_plus:
280 case ast_neg:
281 case ast_bit_not:
282 case ast_logic_not:
283 case ast_pre_inc:
284 case ast_pre_dec:
Ian Romanick88349b22010-02-22 19:10:25 -0800285 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800286 subexpressions[0]->print();
287 break;
288
289 case ast_post_inc:
290 case ast_post_dec:
291 subexpressions[0]->print();
Ian Romanick88349b22010-02-22 19:10:25 -0800292 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800293 break;
294
295 case ast_conditional:
296 subexpressions[0]->print();
297 printf("? ");
298 subexpressions[1]->print();
299 printf(": ");
300 subexpressions[1]->print();
301 break;
302
303 case ast_array_index:
304 subexpressions[0]->print();
305 printf("[ ");
306 subexpressions[1]->print();
307 printf("] ");
308 break;
309
310 case ast_function_call: {
311 ast_expression *parameters = subexpressions[1];
312
313 subexpressions[0]->print();
314 printf("( ");
315
316 if (parameters != NULL) {
317 struct simple_node *ptr;
318
319 parameters->print();
320 foreach (ptr, (struct simple_node *) parameters) {
321 printf(", ");
Ian Romanicke41a1cd2010-02-25 12:49:55 -0800322 ((ast_node *)ptr)->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800323 }
324 }
325
326 printf(") ");
327 break;
328 }
329
330 case ast_identifier:
331 printf("%s ", primary_expression.identifier);
332 break;
333
334 case ast_int_constant:
335 printf("%d ", primary_expression.int_constant);
336 break;
337
338 case ast_uint_constant:
339 printf("%u ", primary_expression.uint_constant);
340 break;
341
342 case ast_float_constant:
343 printf("%f ", primary_expression.float_constant);
344 break;
345
346 case ast_bool_constant:
347 printf("%s ",
348 primary_expression.bool_constant
349 ? "true" : "false");
350 break;
351
352 case ast_sequence: {
353 struct simple_node *ptr;
354 struct simple_node *const head = first_elem(& expressions);
355
356 printf("( ");
357 foreach (ptr, & expressions) {
358 if (ptr != head)
359 printf(", ");
360
Ian Romanicke41a1cd2010-02-25 12:49:55 -0800361 ((ast_node *)ptr)->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800362 }
363 printf(") ");
364 break;
365 }
Ian Romanick88349b22010-02-22 19:10:25 -0800366
367 default:
368 assert(0);
369 break;
Ian Romanicka87ac252010-02-22 13:19:34 -0800370 }
371}
372
373ast_expression::ast_expression(int oper,
374 ast_expression *ex0,
375 ast_expression *ex1,
376 ast_expression *ex2)
377{
378 this->oper = ast_operators(oper);
379 this->subexpressions[0] = ex0;
380 this->subexpressions[1] = ex1;
381 this->subexpressions[2] = ex2;
382 make_empty_list(& expressions);
383}
384
385
386void
387ast_expression_statement::print(void) const
388{
389 if (expression)
390 expression->print();
391
392 printf("; ");
393}
394
395
396ast_expression_statement::ast_expression_statement(ast_expression *ex) :
397 expression(ex)
398{
399 /* empty */
400}
401
402
403void
404ast_function::print(void) const
405{
406 struct simple_node *ptr;
407
408 return_type->print();
409 printf(" %s (", identifier);
410
411 foreach(ptr, & parameters) {
Ian Romanicke41a1cd2010-02-25 12:49:55 -0800412 ((ast_node *)ptr)->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800413 }
414
415 printf(")");
416}
417
418
419ast_function::ast_function(void)
Ian Romanick92318a92010-03-31 18:23:21 -0700420 : is_definition(false), signature(NULL)
Ian Romanicka87ac252010-02-22 13:19:34 -0800421{
422 make_empty_list(& parameters);
423}
424
425
426void
427ast_fully_specified_type::print(void) const
428{
429 _mesa_ast_type_qualifier_print(& qualifier);
430 specifier->print();
431}
432
433
434void
435ast_parameter_declarator::print(void) const
436{
437 type->print();
438 if (identifier)
439 printf("%s ", identifier);
440 ast_opt_array_size_print(is_array, array_size);
441}
442
443
444void
445ast_function_definition::print(void) const
446{
447 prototype->print();
448 body->print();
449}
450
451
452void
453ast_declaration::print(void) const
454{
455 printf("%s ", identifier);
456 ast_opt_array_size_print(is_array, array_size);
457
458 if (initializer) {
459 printf("= ");
460 initializer->print();
461 }
462}
463
464
465ast_declaration::ast_declaration(char *identifier, int is_array,
466 ast_expression *array_size,
467 ast_expression *initializer)
468{
469 this->identifier = identifier;
470 this->is_array = is_array;
471 this->array_size = array_size;
472 this->initializer = initializer;
473}
474
475
476void
477ast_declarator_list::print(void) const
478{
479 struct simple_node *head;
480 struct simple_node *ptr;
481
482 assert(type || invariant);
483
484 if (type)
485 type->print();
486 else
487 printf("invariant ");
488
489 head = first_elem(& declarations);
490 foreach (ptr, & declarations) {
491 if (ptr != head)
492 printf(", ");
493
Ian Romanicke41a1cd2010-02-25 12:49:55 -0800494 ((ast_node *)ptr)->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800495 }
496
497 printf("; ");
498}
499
500
501ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type)
502{
503 this->type = type;
504 make_empty_list(& this->declarations);
505}
506
507void
508ast_jump_statement::print(void) const
509{
510 switch (mode) {
511 case ast_continue:
512 printf("continue; ");
513 break;
514 case ast_break:
515 printf("break; ");
516 break;
517 case ast_return:
518 printf("return ");
519 if (opt_return_value)
520 opt_return_value->print();
521
522 printf("; ");
523 break;
524 case ast_discard:
525 printf("discard; ");
526 break;
527 }
528}
529
530
531ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value)
532{
533 this->mode = ast_jump_modes(mode);
534
535 if (mode == ast_return)
536 opt_return_value = return_value;
537}
538
539
540void
541ast_selection_statement::print(void) const
542{
543 printf("if ( ");
544 condition->print();
545 printf(") ");
546
547 then_statement->print();
548
549 if (else_statement) {
550 printf("else ");
551 else_statement->print();
552 }
553
554}
555
556
557ast_selection_statement::ast_selection_statement(ast_expression *condition,
558 ast_node *then_statement,
559 ast_node *else_statement)
560{
561 this->condition = condition;
562 this->then_statement = then_statement;
563 this->else_statement = else_statement;
564}
565
566
567void
568ast_iteration_statement::print(void) const
569{
570 switch (mode) {
571 case ast_for:
572 printf("for( ");
573 if (init_statement)
574 init_statement->print();
575 printf("; ");
576
577 if (condition)
578 condition->print();
579 printf("; ");
580
581 if (rest_expression)
582 rest_expression->print();
583 printf(") ");
584
585 body->print();
586 break;
587
588 case ast_while:
589 printf("while ( ");
590 if (condition)
591 condition->print();
592 printf(") ");
593 body->print();
594 break;
595
596 case ast_do_while:
597 printf("do ");
598 body->print();
599 printf("while ( ");
600 if (condition)
601 condition->print();
602 printf("); ");
603 break;
604 }
605}
606
607
608ast_iteration_statement::ast_iteration_statement(int mode,
609 ast_node *init,
610 ast_node *condition,
611 ast_expression *rest_expression,
612 ast_node *body)
613{
614 this->mode = ast_iteration_modes(mode);
615 this->init_statement = init;
616 this->condition = condition;
617 this->rest_expression = rest_expression;
618 this->body = body;
619}
620
621
622void
623ast_struct_specifier::print(void) const
624{
625 struct simple_node *ptr;
626
627 printf("struct %s { ", name);
628 foreach (ptr, & declarations) {
Ian Romanicke41a1cd2010-02-25 12:49:55 -0800629 ((ast_node *)ptr)->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800630 }
631 printf("} ");
632}
633
634
635ast_struct_specifier::ast_struct_specifier(char *identifier,
636 ast_node *declarator_list)
637{
638 name = identifier;
639
640 /* This seems odd, but it works. The simple_list is,
641 * basically, a circular list. insert_at_tail adds
642 * the specified node to the list before the current
643 * head.
644 */
645 insert_at_tail((struct simple_node *) declarator_list,
646 & declarations);
647}
648
649
650static char *
651load_text_file(const char *file_name, size_t *size)
652{
653 char *text = NULL;
654 struct stat st;
655 ssize_t total_read = 0;
656 int fd = open(file_name, O_RDONLY);
657
658 *size = 0;
659 if (fd < 0) {
660 return NULL;
661 }
662
663 if (fstat(fd, & st) == 0) {
664 text = (char *) malloc(st.st_size + 1);
665 if (text != NULL) {
666 do {
667 ssize_t bytes = read(fd, text + total_read,
668 st.st_size - total_read);
669 if (bytes < 0) {
670 free(text);
671 text = NULL;
672 break;
673 }
674
675 if (bytes == 0) {
676 break;
677 }
678
679 total_read += bytes;
680 } while (total_read < st.st_size);
681
682 text[total_read] = '\0';
683 *size = total_read;
684 }
685 }
686
687 close(fd);
688
689 return text;
690}
691
692
693int
694main(int argc, char **argv)
695{
696 struct _mesa_glsl_parse_state state;
697 char *shader;
698 size_t shader_len;
699 struct simple_node *ptr;
Ian Romanick0044e7e2010-03-08 23:44:00 -0800700 exec_list instructions;
Ian Romanicka87ac252010-02-22 13:19:34 -0800701
Ian Romanick8e6cd3b2010-03-10 09:31:30 -0800702 if (argc < 3) {
703 printf("Usage: %s [v|g|f] <shader_file>\n", argv[0]);
704 return EXIT_FAILURE;
705 }
706
707 switch (argv[1][0]) {
708 case 'v':
709 state.target = vertex_shader;
710 break;
711 case 'g':
712 state.target = geometry_shader;
713 break;
714 case 'f':
715 state.target = fragment_shader;
716 break;
717 default:
718 printf("Usage: %s [v|g|f] <shader_file>\n", argv[0]);
719 return EXIT_FAILURE;
720 }
721
722 shader = load_text_file(argv[2], & shader_len);
Ian Romanicka87ac252010-02-22 13:19:34 -0800723
724 state.scanner = NULL;
725 make_empty_list(& state.translation_unit);
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700726 state.symbols = new glsl_symbol_table;
Ian Romanick1f585182010-03-11 14:08:33 -0800727 state.error = false;
Ian Romanick5185a5f2010-03-29 15:20:42 -0700728 state.temp_index = 0;
Ian Romanicke9d0f262010-04-05 17:01:53 -0700729 state.loop_or_switch_nesting = NULL;
Ian Romanicka87ac252010-02-22 13:19:34 -0800730
731 _mesa_glsl_lexer_ctor(& state, shader, shader_len);
732 _mesa_glsl_parse(& state);
733 _mesa_glsl_lexer_dtor(& state);
734
735 foreach (ptr, & state.translation_unit) {
Ian Romanicke41a1cd2010-02-25 12:49:55 -0800736 ((ast_node *)ptr)->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800737 }
738
Ian Romanickd949a9a2010-03-10 09:55:22 -0800739 _mesa_ast_to_hir(&instructions, &state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800740
Eric Anholt62735692010-04-05 15:24:28 -0700741 /* Optimization passes */
742 if (!state.error) {
743 /* Constant folding */
Eric Anholt70b74922010-04-06 11:52:09 -0700744 ir_constant_folding_visitor constant_folding;
745 visit_exec_list(&instructions, &constant_folding);
Eric Anholt62735692010-04-05 15:24:28 -0700746 }
747
748 /* Print out the resulting IR */
Ian Romanick1c4156f2010-03-10 09:27:03 -0800749 printf("\n\n");
Ian Romanick1c4156f2010-03-10 09:27:03 -0800750
Ian Romanick1f585182010-03-11 14:08:33 -0800751 if (!state.error) {
752 foreach_iter(exec_list_iterator, iter, instructions) {
753 ir_print_visitor v;
754
755 ((ir_instruction *)iter.get())->accept(& v);
Ian Romanickd1464272010-03-25 18:29:25 -0700756 printf("\n");
Ian Romanick1f585182010-03-11 14:08:33 -0800757 }
Ian Romanick1c4156f2010-03-10 09:27:03 -0800758 }
759
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700760 delete state.symbols;
Ian Romanicka87ac252010-02-22 13:19:34 -0800761
Eric Anholt7c15bb22010-03-25 14:37:25 -0700762 return state.error != 0;
Ian Romanicka87ac252010-02-22 13:19:34 -0800763}