blob: fd44e96602422a83da5ff39bcd0b102ad32123f5 [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 Romanick0c824652010-04-07 17:13:44 -0700138 } if (strcmp(name, "GL_ARB_texture_rectangle") == 0) {
139 state->ARB_texture_rectangle_enable = (ext_mode != extension_disable);
140 state->ARB_texture_rectangle_warn = (ext_mode == extension_warn);
Ian Romanicke7017612010-04-07 16:46:25 -0700141 } else {
Ian Romanick887a8b02010-04-07 16:57:56 -0700142 unsupported = true;
143 }
144
145 if (unsupported) {
146 static const char *const fmt = "extension `%s' unsupported in %s shader";
147
Ian Romanicke7017612010-04-07 16:46:25 -0700148 if (ext_mode == extension_require) {
Ian Romanick887a8b02010-04-07 16:57:56 -0700149 _mesa_glsl_error(name_locp, state, fmt,
150 name, _mesa_glsl_shader_target_name(state->target));
Ian Romanicke7017612010-04-07 16:46:25 -0700151 return false;
Ian Romanick1799a0c2010-04-07 14:50:36 -0700152 } else {
Ian Romanick887a8b02010-04-07 16:57:56 -0700153 _mesa_glsl_warning(name_locp, state, fmt,
154 name, _mesa_glsl_shader_target_name(state->target));
Ian Romanicke7017612010-04-07 16:46:25 -0700155 }
156 }
157
158 return true;
159}
160
161
Ian Romanicka87ac252010-02-22 13:19:34 -0800162ast_node::~ast_node()
163{
164 /* empty */
165}
166
167
168void
169_mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q)
170{
171 if (q->constant)
172 printf("const ");
173
174 if (q->invariant)
175 printf("invariant ");
176
177 if (q->attribute)
178 printf("attribute ");
179
180 if (q->varying)
181 printf("varying ");
182
183 if (q->in && q->out)
184 printf("inout ");
185 else {
186 if (q->in)
187 printf("in ");
188
189 if (q->out)
190 printf("out ");
191 }
192
193 if (q->centroid)
194 printf("centroid ");
195 if (q->uniform)
196 printf("uniform ");
197 if (q->smooth)
198 printf("smooth ");
199 if (q->flat)
200 printf("flat ");
201 if (q->noperspective)
202 printf("noperspective ");
203}
204
205
206void
207ast_node::print(void) const
208{
Ian Romanick03d3f3a2010-04-02 11:03:47 -0700209 printf("unhandled node ");
Ian Romanicka87ac252010-02-22 13:19:34 -0800210}
211
212
213ast_node::ast_node(void)
214{
Ian Romanick53d27742010-02-22 13:22:10 -0800215 make_empty_list(this);
Ian Romanicka87ac252010-02-22 13:19:34 -0800216}
217
Ian Romanicka87ac252010-02-22 13:19:34 -0800218
219static void
220ast_opt_array_size_print(bool is_array, const ast_expression *array_size)
221{
222 if (is_array) {
223 printf("[ ");
224
225 if (array_size)
226 array_size->print();
227
228 printf("] ");
229 }
230}
231
232
Ian Romanicka87ac252010-02-22 13:19:34 -0800233void
234ast_compound_statement::print(void) const
235{
236 const struct simple_node *ptr;
237
238 printf("{\n");
239
240 foreach(ptr, & statements) {
Ian Romanicke41a1cd2010-02-25 12:49:55 -0800241 ((ast_node *)ptr)->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800242 }
243
244 printf("}\n");
245}
246
247
248ast_compound_statement::ast_compound_statement(int new_scope,
249 ast_node *statements)
250{
251 this->new_scope = new_scope;
252 make_empty_list(& this->statements);
253
254 if (statements != NULL) {
255 /* This seems odd, but it works. The simple_list is,
256 * basically, a circular list. insert_at_tail adds
257 * the specified node to the list before the current
258 * head.
259 */
260 insert_at_tail((struct simple_node *) statements,
261 & this->statements);
262 }
263}
264
265
266void
267ast_expression::print(void) const
268{
Ian Romanicka87ac252010-02-22 13:19:34 -0800269 switch (oper) {
270 case ast_assign:
Ian Romanicka87ac252010-02-22 13:19:34 -0800271 case ast_mul_assign:
272 case ast_div_assign:
273 case ast_mod_assign:
274 case ast_add_assign:
275 case ast_sub_assign:
276 case ast_ls_assign:
277 case ast_rs_assign:
278 case ast_and_assign:
279 case ast_xor_assign:
280 case ast_or_assign:
281 subexpressions[0]->print();
Ian Romanick88349b22010-02-22 19:10:25 -0800282 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800283 subexpressions[1]->print();
284 break;
285
286 case ast_field_selection:
287 subexpressions[0]->print();
288 printf(". %s ", primary_expression.identifier);
289 break;
290
291 case ast_plus:
292 case ast_neg:
293 case ast_bit_not:
294 case ast_logic_not:
295 case ast_pre_inc:
296 case ast_pre_dec:
Ian Romanick88349b22010-02-22 19:10:25 -0800297 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800298 subexpressions[0]->print();
299 break;
300
301 case ast_post_inc:
302 case ast_post_dec:
303 subexpressions[0]->print();
Ian Romanick88349b22010-02-22 19:10:25 -0800304 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800305 break;
306
307 case ast_conditional:
308 subexpressions[0]->print();
309 printf("? ");
310 subexpressions[1]->print();
311 printf(": ");
312 subexpressions[1]->print();
313 break;
314
315 case ast_array_index:
316 subexpressions[0]->print();
317 printf("[ ");
318 subexpressions[1]->print();
319 printf("] ");
320 break;
321
322 case ast_function_call: {
323 ast_expression *parameters = subexpressions[1];
324
325 subexpressions[0]->print();
326 printf("( ");
327
328 if (parameters != NULL) {
329 struct simple_node *ptr;
330
331 parameters->print();
332 foreach (ptr, (struct simple_node *) parameters) {
333 printf(", ");
Ian Romanicke41a1cd2010-02-25 12:49:55 -0800334 ((ast_node *)ptr)->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800335 }
336 }
337
338 printf(") ");
339 break;
340 }
341
342 case ast_identifier:
343 printf("%s ", primary_expression.identifier);
344 break;
345
346 case ast_int_constant:
347 printf("%d ", primary_expression.int_constant);
348 break;
349
350 case ast_uint_constant:
351 printf("%u ", primary_expression.uint_constant);
352 break;
353
354 case ast_float_constant:
355 printf("%f ", primary_expression.float_constant);
356 break;
357
358 case ast_bool_constant:
359 printf("%s ",
360 primary_expression.bool_constant
361 ? "true" : "false");
362 break;
363
364 case ast_sequence: {
365 struct simple_node *ptr;
366 struct simple_node *const head = first_elem(& expressions);
367
368 printf("( ");
369 foreach (ptr, & expressions) {
370 if (ptr != head)
371 printf(", ");
372
Ian Romanicke41a1cd2010-02-25 12:49:55 -0800373 ((ast_node *)ptr)->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800374 }
375 printf(") ");
376 break;
377 }
Ian Romanick88349b22010-02-22 19:10:25 -0800378
379 default:
380 assert(0);
381 break;
Ian Romanicka87ac252010-02-22 13:19:34 -0800382 }
383}
384
385ast_expression::ast_expression(int oper,
386 ast_expression *ex0,
387 ast_expression *ex1,
388 ast_expression *ex2)
389{
390 this->oper = ast_operators(oper);
391 this->subexpressions[0] = ex0;
392 this->subexpressions[1] = ex1;
393 this->subexpressions[2] = ex2;
394 make_empty_list(& expressions);
395}
396
397
398void
399ast_expression_statement::print(void) const
400{
401 if (expression)
402 expression->print();
403
404 printf("; ");
405}
406
407
408ast_expression_statement::ast_expression_statement(ast_expression *ex) :
409 expression(ex)
410{
411 /* empty */
412}
413
414
415void
416ast_function::print(void) const
417{
418 struct simple_node *ptr;
419
420 return_type->print();
421 printf(" %s (", identifier);
422
423 foreach(ptr, & parameters) {
Ian Romanicke41a1cd2010-02-25 12:49:55 -0800424 ((ast_node *)ptr)->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800425 }
426
427 printf(")");
428}
429
430
431ast_function::ast_function(void)
Ian Romanick92318a92010-03-31 18:23:21 -0700432 : is_definition(false), signature(NULL)
Ian Romanicka87ac252010-02-22 13:19:34 -0800433{
434 make_empty_list(& parameters);
435}
436
437
438void
439ast_fully_specified_type::print(void) const
440{
441 _mesa_ast_type_qualifier_print(& qualifier);
442 specifier->print();
443}
444
445
446void
447ast_parameter_declarator::print(void) const
448{
449 type->print();
450 if (identifier)
451 printf("%s ", identifier);
452 ast_opt_array_size_print(is_array, array_size);
453}
454
455
456void
457ast_function_definition::print(void) const
458{
459 prototype->print();
460 body->print();
461}
462
463
464void
465ast_declaration::print(void) const
466{
467 printf("%s ", identifier);
468 ast_opt_array_size_print(is_array, array_size);
469
470 if (initializer) {
471 printf("= ");
472 initializer->print();
473 }
474}
475
476
477ast_declaration::ast_declaration(char *identifier, int is_array,
478 ast_expression *array_size,
479 ast_expression *initializer)
480{
481 this->identifier = identifier;
482 this->is_array = is_array;
483 this->array_size = array_size;
484 this->initializer = initializer;
485}
486
487
488void
489ast_declarator_list::print(void) const
490{
491 struct simple_node *head;
492 struct simple_node *ptr;
493
494 assert(type || invariant);
495
496 if (type)
497 type->print();
498 else
499 printf("invariant ");
500
501 head = first_elem(& declarations);
502 foreach (ptr, & declarations) {
503 if (ptr != head)
504 printf(", ");
505
Ian Romanicke41a1cd2010-02-25 12:49:55 -0800506 ((ast_node *)ptr)->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800507 }
508
509 printf("; ");
510}
511
512
513ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type)
514{
515 this->type = type;
516 make_empty_list(& this->declarations);
517}
518
519void
520ast_jump_statement::print(void) const
521{
522 switch (mode) {
523 case ast_continue:
524 printf("continue; ");
525 break;
526 case ast_break:
527 printf("break; ");
528 break;
529 case ast_return:
530 printf("return ");
531 if (opt_return_value)
532 opt_return_value->print();
533
534 printf("; ");
535 break;
536 case ast_discard:
537 printf("discard; ");
538 break;
539 }
540}
541
542
543ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value)
544{
545 this->mode = ast_jump_modes(mode);
546
547 if (mode == ast_return)
548 opt_return_value = return_value;
549}
550
551
552void
553ast_selection_statement::print(void) const
554{
555 printf("if ( ");
556 condition->print();
557 printf(") ");
558
559 then_statement->print();
560
561 if (else_statement) {
562 printf("else ");
563 else_statement->print();
564 }
565
566}
567
568
569ast_selection_statement::ast_selection_statement(ast_expression *condition,
570 ast_node *then_statement,
571 ast_node *else_statement)
572{
573 this->condition = condition;
574 this->then_statement = then_statement;
575 this->else_statement = else_statement;
576}
577
578
579void
580ast_iteration_statement::print(void) const
581{
582 switch (mode) {
583 case ast_for:
584 printf("for( ");
585 if (init_statement)
586 init_statement->print();
587 printf("; ");
588
589 if (condition)
590 condition->print();
591 printf("; ");
592
593 if (rest_expression)
594 rest_expression->print();
595 printf(") ");
596
597 body->print();
598 break;
599
600 case ast_while:
601 printf("while ( ");
602 if (condition)
603 condition->print();
604 printf(") ");
605 body->print();
606 break;
607
608 case ast_do_while:
609 printf("do ");
610 body->print();
611 printf("while ( ");
612 if (condition)
613 condition->print();
614 printf("); ");
615 break;
616 }
617}
618
619
620ast_iteration_statement::ast_iteration_statement(int mode,
621 ast_node *init,
622 ast_node *condition,
623 ast_expression *rest_expression,
624 ast_node *body)
625{
626 this->mode = ast_iteration_modes(mode);
627 this->init_statement = init;
628 this->condition = condition;
629 this->rest_expression = rest_expression;
630 this->body = body;
631}
632
633
634void
635ast_struct_specifier::print(void) const
636{
637 struct simple_node *ptr;
638
639 printf("struct %s { ", name);
640 foreach (ptr, & declarations) {
Ian Romanicke41a1cd2010-02-25 12:49:55 -0800641 ((ast_node *)ptr)->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800642 }
643 printf("} ");
644}
645
646
647ast_struct_specifier::ast_struct_specifier(char *identifier,
648 ast_node *declarator_list)
649{
650 name = identifier;
651
652 /* This seems odd, but it works. The simple_list is,
653 * basically, a circular list. insert_at_tail adds
654 * the specified node to the list before the current
655 * head.
656 */
657 insert_at_tail((struct simple_node *) declarator_list,
658 & declarations);
659}
660
661
662static char *
663load_text_file(const char *file_name, size_t *size)
664{
665 char *text = NULL;
666 struct stat st;
667 ssize_t total_read = 0;
668 int fd = open(file_name, O_RDONLY);
669
670 *size = 0;
671 if (fd < 0) {
672 return NULL;
673 }
674
675 if (fstat(fd, & st) == 0) {
676 text = (char *) malloc(st.st_size + 1);
677 if (text != NULL) {
678 do {
679 ssize_t bytes = read(fd, text + total_read,
680 st.st_size - total_read);
681 if (bytes < 0) {
682 free(text);
683 text = NULL;
684 break;
685 }
686
687 if (bytes == 0) {
688 break;
689 }
690
691 total_read += bytes;
692 } while (total_read < st.st_size);
693
694 text[total_read] = '\0';
695 *size = total_read;
696 }
697 }
698
699 close(fd);
700
701 return text;
702}
703
704
705int
706main(int argc, char **argv)
707{
708 struct _mesa_glsl_parse_state state;
709 char *shader;
710 size_t shader_len;
711 struct simple_node *ptr;
Ian Romanick0044e7e2010-03-08 23:44:00 -0800712 exec_list instructions;
Ian Romanicka87ac252010-02-22 13:19:34 -0800713
Ian Romanick8e6cd3b2010-03-10 09:31:30 -0800714 if (argc < 3) {
715 printf("Usage: %s [v|g|f] <shader_file>\n", argv[0]);
716 return EXIT_FAILURE;
717 }
718
719 switch (argv[1][0]) {
720 case 'v':
721 state.target = vertex_shader;
722 break;
723 case 'g':
724 state.target = geometry_shader;
725 break;
726 case 'f':
727 state.target = fragment_shader;
728 break;
729 default:
730 printf("Usage: %s [v|g|f] <shader_file>\n", argv[0]);
731 return EXIT_FAILURE;
732 }
733
734 shader = load_text_file(argv[2], & shader_len);
Ian Romanicka87ac252010-02-22 13:19:34 -0800735
736 state.scanner = NULL;
737 make_empty_list(& state.translation_unit);
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700738 state.symbols = new glsl_symbol_table;
Ian Romanick1f585182010-03-11 14:08:33 -0800739 state.error = false;
Ian Romanick5185a5f2010-03-29 15:20:42 -0700740 state.temp_index = 0;
Ian Romanicke9d0f262010-04-05 17:01:53 -0700741 state.loop_or_switch_nesting = NULL;
Ian Romanick0c824652010-04-07 17:13:44 -0700742 state.ARB_texture_rectangle_enable = true;
Ian Romanicka87ac252010-02-22 13:19:34 -0800743
744 _mesa_glsl_lexer_ctor(& state, shader, shader_len);
745 _mesa_glsl_parse(& state);
746 _mesa_glsl_lexer_dtor(& state);
747
748 foreach (ptr, & state.translation_unit) {
Ian Romanicke41a1cd2010-02-25 12:49:55 -0800749 ((ast_node *)ptr)->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800750 }
751
Ian Romanickd949a9a2010-03-10 09:55:22 -0800752 _mesa_ast_to_hir(&instructions, &state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800753
Eric Anholt62735692010-04-05 15:24:28 -0700754 /* Optimization passes */
755 if (!state.error) {
756 /* Constant folding */
Eric Anholt70b74922010-04-06 11:52:09 -0700757 ir_constant_folding_visitor constant_folding;
758 visit_exec_list(&instructions, &constant_folding);
Eric Anholt62735692010-04-05 15:24:28 -0700759 }
760
761 /* Print out the resulting IR */
Ian Romanick1c4156f2010-03-10 09:27:03 -0800762 printf("\n\n");
Ian Romanick1c4156f2010-03-10 09:27:03 -0800763
Ian Romanick1f585182010-03-11 14:08:33 -0800764 if (!state.error) {
765 foreach_iter(exec_list_iterator, iter, instructions) {
766 ir_print_visitor v;
767
768 ((ir_instruction *)iter.get())->accept(& v);
Ian Romanickd1464272010-03-25 18:29:25 -0700769 printf("\n");
Ian Romanick1f585182010-03-11 14:08:33 -0800770 }
Ian Romanick1c4156f2010-03-10 09:27:03 -0800771 }
772
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700773 delete state.symbols;
Ian Romanicka87ac252010-02-22 13:19:34 -0800774
Eric Anholt7c15bb22010-03-25 14:37:25 -0700775 return state.error != 0;
Ian Romanicka87ac252010-02-22 13:19:34 -0800776}