blob: 2d045ac9c7ab6b85ae58df4aadce73988c203124 [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>
Ian Romanicka87ac252010-02-22 13:19:34 -080025#include <string.h>
26#include <assert.h>
Ian Romanicka87ac252010-02-22 13:19:34 -080027
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -070028extern "C" {
29#include <talloc.h>
Chia-I Wubfd7c9a2010-08-23 17:51:42 +080030#include "main/core.h" /* for struct __GLcontextRec */
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -070031}
32
Ian Romanicka87ac252010-02-22 13:19:34 -080033#include "ast.h"
34#include "glsl_parser_extras.h"
Ian Romanickd59673c2010-02-25 17:17:23 -080035#include "glsl_parser.h"
Eric Anholt2f4fe152010-08-10 13:06:49 -070036#include "ir_optimization.h"
Ian Romanick8df2dbf2010-08-26 16:45:22 -070037#include "loop_analysis.h"
Ian Romanicka87ac252010-02-22 13:19:34 -080038
Ian Romanick2462a532010-07-18 15:59:43 -070039_mesa_glsl_parse_state::_mesa_glsl_parse_state(struct __GLcontextRec *ctx,
40 GLenum target, void *mem_ctx)
41{
42 switch (target) {
43 case GL_VERTEX_SHADER: this->target = vertex_shader; break;
44 case GL_FRAGMENT_SHADER: this->target = fragment_shader; break;
45 case GL_GEOMETRY_SHADER: this->target = geometry_shader; break;
46 }
47
48 this->scanner = NULL;
49 this->translation_unit.make_empty();
50 this->symbols = new(mem_ctx) glsl_symbol_table;
51 this->info_log = talloc_strdup(mem_ctx, "");
52 this->error = false;
53 this->loop_or_switch_nesting = NULL;
54 this->ARB_texture_rectangle_enable = true;
55
56 if (ctx != NULL) {
57 this->extensions = &ctx->Extensions;
58
59 this->Const.MaxLights = ctx->Const.MaxLights;
60 this->Const.MaxClipPlanes = ctx->Const.MaxClipPlanes;
61 this->Const.MaxTextureUnits = ctx->Const.MaxTextureUnits;
62 this->Const.MaxTextureCoords = ctx->Const.MaxTextureCoordUnits;
63 this->Const.MaxVertexAttribs = ctx->Const.VertexProgram.MaxAttribs;
64 this->Const.MaxVertexUniformComponents = ctx->Const.VertexProgram.MaxUniformComponents;
65 this->Const.MaxVaryingFloats = ctx->Const.MaxVarying * 4;
66 this->Const.MaxVertexTextureImageUnits = ctx->Const.MaxVertexTextureImageUnits;
67 this->Const.MaxCombinedTextureImageUnits = ctx->Const.MaxCombinedTextureImageUnits;
68 this->Const.MaxTextureImageUnits = ctx->Const.MaxTextureImageUnits;
69 this->Const.MaxFragmentUniformComponents = ctx->Const.FragmentProgram.MaxUniformComponents;
70
71 this->Const.MaxDrawBuffers = ctx->Const.MaxDrawBuffers;
72 } else {
Kenneth Graunkeaa9f86a2010-07-22 16:20:36 -070073 /* If there is no GL context (standalone compiler), fill in constants
74 * with the minimum required values.
75 */
Ian Romanick2462a532010-07-18 15:59:43 -070076 static struct gl_extensions null_extensions;
77
78 memset(&null_extensions, 0, sizeof(null_extensions));
Ian Romanick4f397e12010-08-05 17:50:13 -070079 null_extensions.ARB_draw_buffers = GL_TRUE;
80 null_extensions.ARB_fragment_coord_conventions = GL_TRUE;
81 null_extensions.EXT_texture_array = GL_TRUE;
82 null_extensions.NV_texture_rectangle = GL_TRUE;
83
Ian Romanick2462a532010-07-18 15:59:43 -070084 this->extensions = &null_extensions;
Kenneth Graunkeaa9f86a2010-07-22 16:20:36 -070085
86 /* 1.10 minimums. */
87 this->Const.MaxLights = 8;
88 this->Const.MaxClipPlanes = 8;
89 this->Const.MaxTextureUnits = 2;
90
91 /* More than the 1.10 minimum to appease parser tests taken from
92 * apps that (hopefully) already checked the number of coords.
93 */
94 this->Const.MaxTextureCoords = 4;
95
96 this->Const.MaxVertexAttribs = 16;
97 this->Const.MaxVertexUniformComponents = 512;
98 this->Const.MaxVaryingFloats = 32;
99 this->Const.MaxVertexTextureImageUnits = 0;
100 this->Const.MaxCombinedTextureImageUnits = 2;
101 this->Const.MaxTextureImageUnits = 2;
102 this->Const.MaxFragmentUniformComponents = 64;
103
104 this->Const.MaxDrawBuffers = 2;
Ian Romanick2462a532010-07-18 15:59:43 -0700105 }
106}
107
Ian Romanick5bfe30a2010-04-07 16:44:30 -0700108const char *
109_mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target)
110{
111 switch (target) {
112 case vertex_shader: return "vertex";
113 case fragment_shader: return "fragment";
114 case geometry_shader: return "geometry";
Eric Anholt81f49a72010-04-29 17:57:28 -0700115 case ir_shader: break;
Ian Romanick5bfe30a2010-04-07 16:44:30 -0700116 }
117
118 assert(!"Should not get here.");
Eric Anholt87a2ee82010-07-18 17:47:15 -0700119 return "unknown";
Ian Romanick5bfe30a2010-04-07 16:44:30 -0700120}
121
122
Ian Romanicka87ac252010-02-22 13:19:34 -0800123void
Ian Romanick1f585182010-03-11 14:08:33 -0800124_mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
125 const char *fmt, ...)
Ian Romanicka87ac252010-02-22 13:19:34 -0800126{
Ian Romanicka87ac252010-02-22 13:19:34 -0800127 va_list ap;
128
Ian Romanick71d0bbf2010-03-23 13:21:19 -0700129 state->error = true;
Ian Romanick1f585182010-03-11 14:08:33 -0800130
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -0700131 assert(state->info_log != NULL);
132 state->info_log = talloc_asprintf_append(state->info_log,
133 "%u:%u(%u): error: ",
134 locp->source,
135 locp->first_line,
136 locp->first_column);
Ian Romanicka87ac252010-02-22 13:19:34 -0800137 va_start(ap, fmt);
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -0700138 state->info_log = talloc_vasprintf_append(state->info_log, fmt, ap);
Ian Romanicka87ac252010-02-22 13:19:34 -0800139 va_end(ap);
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -0700140 state->info_log = talloc_strdup_append(state->info_log, "\n");
Ian Romanicka87ac252010-02-22 13:19:34 -0800141}
142
143
Ian Romanick56b8b212010-04-07 14:47:46 -0700144void
Eric Anholt3623df62010-04-29 18:00:33 -0700145_mesa_glsl_warning(const YYLTYPE *locp, _mesa_glsl_parse_state *state,
Ian Romanick56b8b212010-04-07 14:47:46 -0700146 const char *fmt, ...)
147{
Ian Romanick56b8b212010-04-07 14:47:46 -0700148 va_list ap;
149
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -0700150 assert(state->info_log != NULL);
151 state->info_log = talloc_asprintf_append(state->info_log,
152 "%u:%u(%u): warning: ",
153 locp->source,
154 locp->first_line,
155 locp->first_column);
Ian Romanick56b8b212010-04-07 14:47:46 -0700156 va_start(ap, fmt);
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -0700157 state->info_log = talloc_vasprintf_append(state->info_log, fmt, ap);
Ian Romanick56b8b212010-04-07 14:47:46 -0700158 va_end(ap);
Kenneth Graunkeb2ba6fa2010-06-17 15:15:35 -0700159 state->info_log = talloc_strdup_append(state->info_log, "\n");
Ian Romanick56b8b212010-04-07 14:47:46 -0700160}
161
162
Ian Romanicke7017612010-04-07 16:46:25 -0700163bool
164_mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
165 const char *behavior, YYLTYPE *behavior_locp,
166 _mesa_glsl_parse_state *state)
167{
168 enum {
169 extension_disable,
170 extension_enable,
171 extension_require,
172 extension_warn
173 } ext_mode;
Ian Romanicke7017612010-04-07 16:46:25 -0700174
175 if (strcmp(behavior, "warn") == 0) {
176 ext_mode = extension_warn;
177 } else if (strcmp(behavior, "require") == 0) {
178 ext_mode = extension_require;
179 } else if (strcmp(behavior, "enable") == 0) {
180 ext_mode = extension_enable;
181 } else if (strcmp(behavior, "disable") == 0) {
182 ext_mode = extension_disable;
183 } else {
184 _mesa_glsl_error(behavior_locp, state,
185 "Unknown extension behavior `%s'",
186 behavior);
187 return false;
188 }
189
Ian Romanick887a8b02010-04-07 16:57:56 -0700190 bool unsupported = false;
191
Ian Romanicke7017612010-04-07 16:46:25 -0700192 if (strcmp(name, "all") == 0) {
193 if ((ext_mode == extension_enable) || (ext_mode == extension_require)) {
194 _mesa_glsl_error(name_locp, state, "Cannot %s all extensions",
195 (ext_mode == extension_enable)
196 ? "enable" : "require");
197 return false;
198 }
Ian Romanickeb56cea2010-04-23 13:32:23 -0700199 } else if (strcmp(name, "GL_ARB_draw_buffers") == 0) {
Ian Romanickc77b2572010-04-07 16:59:46 -0700200 /* This extension is only supported in fragment shaders.
201 */
202 if (state->target != fragment_shader) {
203 unsupported = true;
204 } else {
205 state->ARB_draw_buffers_enable = (ext_mode != extension_disable);
206 state->ARB_draw_buffers_warn = (ext_mode == extension_warn);
207 }
Ian Romanickf50f0652010-06-30 17:30:03 -0700208 } else if (strcmp(name, "GL_ARB_fragment_coord_conventions") == 0) {
209 state->ARB_fragment_coord_conventions_enable =
210 (ext_mode != extension_disable);
211 state->ARB_fragment_coord_conventions_warn =
212 (ext_mode == extension_warn);
213
214 unsupported = !state->extensions->ARB_fragment_coord_conventions;
Ian Romanickeb56cea2010-04-23 13:32:23 -0700215 } else if (strcmp(name, "GL_ARB_texture_rectangle") == 0) {
Ian Romanick0c824652010-04-07 17:13:44 -0700216 state->ARB_texture_rectangle_enable = (ext_mode != extension_disable);
217 state->ARB_texture_rectangle_warn = (ext_mode == extension_warn);
Ian Romanick667f4e12010-06-30 16:42:07 -0700218 } else if (strcmp(name, "GL_EXT_texture_array") == 0) {
219 state->EXT_texture_array_enable = (ext_mode != extension_disable);
220 state->EXT_texture_array_warn = (ext_mode == extension_warn);
221
222 unsupported = !state->extensions->EXT_texture_array;
Ian Romanicke7017612010-04-07 16:46:25 -0700223 } else {
Ian Romanick887a8b02010-04-07 16:57:56 -0700224 unsupported = true;
225 }
226
227 if (unsupported) {
228 static const char *const fmt = "extension `%s' unsupported in %s shader";
229
Ian Romanicke7017612010-04-07 16:46:25 -0700230 if (ext_mode == extension_require) {
Ian Romanick887a8b02010-04-07 16:57:56 -0700231 _mesa_glsl_error(name_locp, state, fmt,
232 name, _mesa_glsl_shader_target_name(state->target));
Ian Romanicke7017612010-04-07 16:46:25 -0700233 return false;
Ian Romanick1799a0c2010-04-07 14:50:36 -0700234 } else {
Ian Romanick887a8b02010-04-07 16:57:56 -0700235 _mesa_glsl_warning(name_locp, state, fmt,
236 name, _mesa_glsl_shader_target_name(state->target));
Ian Romanicke7017612010-04-07 16:46:25 -0700237 }
238 }
239
240 return true;
241}
242
Ian Romanicka87ac252010-02-22 13:19:34 -0800243void
244_mesa_ast_type_qualifier_print(const struct ast_type_qualifier *q)
245{
246 if (q->constant)
247 printf("const ");
248
249 if (q->invariant)
250 printf("invariant ");
251
252 if (q->attribute)
253 printf("attribute ");
254
255 if (q->varying)
256 printf("varying ");
257
258 if (q->in && q->out)
259 printf("inout ");
260 else {
261 if (q->in)
262 printf("in ");
263
264 if (q->out)
265 printf("out ");
266 }
267
268 if (q->centroid)
269 printf("centroid ");
270 if (q->uniform)
271 printf("uniform ");
272 if (q->smooth)
273 printf("smooth ");
274 if (q->flat)
275 printf("flat ");
276 if (q->noperspective)
277 printf("noperspective ");
278}
279
280
281void
282ast_node::print(void) const
283{
Ian Romanick03d3f3a2010-04-02 11:03:47 -0700284 printf("unhandled node ");
Ian Romanicka87ac252010-02-22 13:19:34 -0800285}
286
287
288ast_node::ast_node(void)
289{
Carl Worthec9675e2010-07-29 16:39:36 -0700290 this->location.source = 0;
291 this->location.line = 0;
292 this->location.column = 0;
Ian Romanicka87ac252010-02-22 13:19:34 -0800293}
294
Ian Romanicka87ac252010-02-22 13:19:34 -0800295
296static void
297ast_opt_array_size_print(bool is_array, const ast_expression *array_size)
298{
299 if (is_array) {
300 printf("[ ");
301
302 if (array_size)
303 array_size->print();
304
305 printf("] ");
306 }
307}
308
309
Ian Romanicka87ac252010-02-22 13:19:34 -0800310void
311ast_compound_statement::print(void) const
312{
Ian Romanicka87ac252010-02-22 13:19:34 -0800313 printf("{\n");
314
Ian Romanick304ea902010-05-10 11:17:53 -0700315 foreach_list_const(n, &this->statements) {
316 ast_node *ast = exec_node_data(ast_node, n, link);
317 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800318 }
319
320 printf("}\n");
321}
322
323
324ast_compound_statement::ast_compound_statement(int new_scope,
325 ast_node *statements)
326{
327 this->new_scope = new_scope;
Ian Romanicka87ac252010-02-22 13:19:34 -0800328
329 if (statements != NULL) {
Ian Romanick304ea902010-05-10 11:17:53 -0700330 this->statements.push_degenerate_list_at_head(&statements->link);
Ian Romanicka87ac252010-02-22 13:19:34 -0800331 }
332}
333
334
335void
336ast_expression::print(void) const
337{
Ian Romanicka87ac252010-02-22 13:19:34 -0800338 switch (oper) {
339 case ast_assign:
Ian Romanicka87ac252010-02-22 13:19:34 -0800340 case ast_mul_assign:
341 case ast_div_assign:
342 case ast_mod_assign:
343 case ast_add_assign:
344 case ast_sub_assign:
345 case ast_ls_assign:
346 case ast_rs_assign:
347 case ast_and_assign:
348 case ast_xor_assign:
349 case ast_or_assign:
350 subexpressions[0]->print();
Ian Romanick88349b22010-02-22 19:10:25 -0800351 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800352 subexpressions[1]->print();
353 break;
354
355 case ast_field_selection:
356 subexpressions[0]->print();
357 printf(". %s ", primary_expression.identifier);
358 break;
359
360 case ast_plus:
361 case ast_neg:
362 case ast_bit_not:
363 case ast_logic_not:
364 case ast_pre_inc:
365 case ast_pre_dec:
Ian Romanick88349b22010-02-22 19:10:25 -0800366 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800367 subexpressions[0]->print();
368 break;
369
370 case ast_post_inc:
371 case ast_post_dec:
372 subexpressions[0]->print();
Ian Romanick88349b22010-02-22 19:10:25 -0800373 printf("%s ", operator_string(oper));
Ian Romanicka87ac252010-02-22 13:19:34 -0800374 break;
375
376 case ast_conditional:
377 subexpressions[0]->print();
378 printf("? ");
379 subexpressions[1]->print();
380 printf(": ");
381 subexpressions[1]->print();
382 break;
383
384 case ast_array_index:
385 subexpressions[0]->print();
386 printf("[ ");
387 subexpressions[1]->print();
388 printf("] ");
389 break;
390
391 case ast_function_call: {
Ian Romanicka87ac252010-02-22 13:19:34 -0800392 subexpressions[0]->print();
393 printf("( ");
394
Ian Romanick304ea902010-05-10 11:17:53 -0700395 foreach_list_const (n, &this->expressions) {
Ian Romanick23849372010-05-14 16:06:41 -0700396 if (n != this->expressions.get_head())
397 printf(", ");
Ian Romanick304ea902010-05-10 11:17:53 -0700398
399 ast_node *ast = exec_node_data(ast_node, n, link);
400 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800401 }
402
403 printf(") ");
404 break;
405 }
406
407 case ast_identifier:
408 printf("%s ", primary_expression.identifier);
409 break;
410
411 case ast_int_constant:
412 printf("%d ", primary_expression.int_constant);
413 break;
414
415 case ast_uint_constant:
416 printf("%u ", primary_expression.uint_constant);
417 break;
418
419 case ast_float_constant:
420 printf("%f ", primary_expression.float_constant);
421 break;
422
423 case ast_bool_constant:
424 printf("%s ",
425 primary_expression.bool_constant
426 ? "true" : "false");
427 break;
428
429 case ast_sequence: {
Ian Romanicka87ac252010-02-22 13:19:34 -0800430 printf("( ");
Ian Romanick304ea902010-05-10 11:17:53 -0700431 foreach_list_const(n, & this->expressions) {
432 if (n != this->expressions.get_head())
Ian Romanicka87ac252010-02-22 13:19:34 -0800433 printf(", ");
434
Ian Romanick304ea902010-05-10 11:17:53 -0700435 ast_node *ast = exec_node_data(ast_node, n, link);
436 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800437 }
438 printf(") ");
439 break;
440 }
Ian Romanick88349b22010-02-22 19:10:25 -0800441
442 default:
443 assert(0);
444 break;
Ian Romanicka87ac252010-02-22 13:19:34 -0800445 }
446}
447
448ast_expression::ast_expression(int oper,
449 ast_expression *ex0,
450 ast_expression *ex1,
451 ast_expression *ex2)
452{
453 this->oper = ast_operators(oper);
454 this->subexpressions[0] = ex0;
455 this->subexpressions[1] = ex1;
456 this->subexpressions[2] = ex2;
Ian Romanicka87ac252010-02-22 13:19:34 -0800457}
458
459
460void
461ast_expression_statement::print(void) const
462{
463 if (expression)
464 expression->print();
465
466 printf("; ");
467}
468
469
470ast_expression_statement::ast_expression_statement(ast_expression *ex) :
471 expression(ex)
472{
473 /* empty */
474}
475
476
477void
478ast_function::print(void) const
479{
Ian Romanicka87ac252010-02-22 13:19:34 -0800480 return_type->print();
481 printf(" %s (", identifier);
482
Ian Romanick304ea902010-05-10 11:17:53 -0700483 foreach_list_const(n, & this->parameters) {
484 ast_node *ast = exec_node_data(ast_node, n, link);
485 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800486 }
487
488 printf(")");
489}
490
491
492ast_function::ast_function(void)
Ian Romanick92318a92010-03-31 18:23:21 -0700493 : is_definition(false), signature(NULL)
Ian Romanicka87ac252010-02-22 13:19:34 -0800494{
Ian Romanick304ea902010-05-10 11:17:53 -0700495 /* empty */
Ian Romanicka87ac252010-02-22 13:19:34 -0800496}
497
498
499void
500ast_fully_specified_type::print(void) const
501{
502 _mesa_ast_type_qualifier_print(& qualifier);
503 specifier->print();
504}
505
506
507void
508ast_parameter_declarator::print(void) const
509{
510 type->print();
511 if (identifier)
512 printf("%s ", identifier);
513 ast_opt_array_size_print(is_array, array_size);
514}
515
516
517void
518ast_function_definition::print(void) const
519{
520 prototype->print();
521 body->print();
522}
523
524
525void
526ast_declaration::print(void) const
527{
528 printf("%s ", identifier);
529 ast_opt_array_size_print(is_array, array_size);
530
531 if (initializer) {
532 printf("= ");
533 initializer->print();
534 }
535}
536
537
538ast_declaration::ast_declaration(char *identifier, int is_array,
539 ast_expression *array_size,
540 ast_expression *initializer)
541{
542 this->identifier = identifier;
543 this->is_array = is_array;
544 this->array_size = array_size;
545 this->initializer = initializer;
546}
547
548
549void
550ast_declarator_list::print(void) const
551{
Ian Romanicka87ac252010-02-22 13:19:34 -0800552 assert(type || invariant);
553
554 if (type)
555 type->print();
556 else
557 printf("invariant ");
558
Ian Romanick304ea902010-05-10 11:17:53 -0700559 foreach_list_const (ptr, & this->declarations) {
560 if (ptr != this->declarations.get_head())
Ian Romanicka87ac252010-02-22 13:19:34 -0800561 printf(", ");
562
Ian Romanick304ea902010-05-10 11:17:53 -0700563 ast_node *ast = exec_node_data(ast_node, ptr, link);
564 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800565 }
566
567 printf("; ");
568}
569
570
571ast_declarator_list::ast_declarator_list(ast_fully_specified_type *type)
572{
573 this->type = type;
Ian Romanick38327062010-07-01 17:10:11 -0700574 this->invariant = false;
Ian Romanicka87ac252010-02-22 13:19:34 -0800575}
576
577void
578ast_jump_statement::print(void) const
579{
580 switch (mode) {
581 case ast_continue:
582 printf("continue; ");
583 break;
584 case ast_break:
585 printf("break; ");
586 break;
587 case ast_return:
588 printf("return ");
589 if (opt_return_value)
590 opt_return_value->print();
591
592 printf("; ");
593 break;
594 case ast_discard:
595 printf("discard; ");
596 break;
597 }
598}
599
600
601ast_jump_statement::ast_jump_statement(int mode, ast_expression *return_value)
602{
603 this->mode = ast_jump_modes(mode);
604
605 if (mode == ast_return)
606 opt_return_value = return_value;
607}
608
609
610void
611ast_selection_statement::print(void) const
612{
613 printf("if ( ");
614 condition->print();
615 printf(") ");
616
617 then_statement->print();
618
619 if (else_statement) {
620 printf("else ");
621 else_statement->print();
622 }
623
624}
625
626
627ast_selection_statement::ast_selection_statement(ast_expression *condition,
628 ast_node *then_statement,
629 ast_node *else_statement)
630{
631 this->condition = condition;
632 this->then_statement = then_statement;
633 this->else_statement = else_statement;
634}
635
636
637void
638ast_iteration_statement::print(void) const
639{
640 switch (mode) {
641 case ast_for:
642 printf("for( ");
643 if (init_statement)
644 init_statement->print();
645 printf("; ");
646
647 if (condition)
648 condition->print();
649 printf("; ");
650
651 if (rest_expression)
652 rest_expression->print();
653 printf(") ");
654
655 body->print();
656 break;
657
658 case ast_while:
659 printf("while ( ");
660 if (condition)
661 condition->print();
662 printf(") ");
663 body->print();
664 break;
665
666 case ast_do_while:
667 printf("do ");
668 body->print();
669 printf("while ( ");
670 if (condition)
671 condition->print();
672 printf("); ");
673 break;
674 }
675}
676
677
678ast_iteration_statement::ast_iteration_statement(int mode,
679 ast_node *init,
680 ast_node *condition,
681 ast_expression *rest_expression,
682 ast_node *body)
683{
684 this->mode = ast_iteration_modes(mode);
685 this->init_statement = init;
686 this->condition = condition;
687 this->rest_expression = rest_expression;
688 this->body = body;
689}
690
691
692void
693ast_struct_specifier::print(void) const
694{
Ian Romanicka87ac252010-02-22 13:19:34 -0800695 printf("struct %s { ", name);
Ian Romanick304ea902010-05-10 11:17:53 -0700696 foreach_list_const(n, &this->declarations) {
697 ast_node *ast = exec_node_data(ast_node, n, link);
698 ast->print();
Ian Romanicka87ac252010-02-22 13:19:34 -0800699 }
700 printf("} ");
701}
702
703
704ast_struct_specifier::ast_struct_specifier(char *identifier,
705 ast_node *declarator_list)
706{
707 name = identifier;
Ian Romanick304ea902010-05-10 11:17:53 -0700708 this->declarations.push_degenerate_list_at_head(&declarator_list->link);
Ian Romanicka87ac252010-02-22 13:19:34 -0800709}
Eric Anholt2f4fe152010-08-10 13:06:49 -0700710
711bool
712do_common_optimization(exec_list *ir, bool linked)
713{
714 GLboolean progress = GL_FALSE;
715
716 progress = do_sub_to_add_neg(ir) || progress;
717
718 if (linked) {
719 progress = do_function_inlining(ir) || progress;
720 progress = do_dead_functions(ir) || progress;
721 }
722 progress = do_structure_splitting(ir) || progress;
723 progress = do_if_simplification(ir) || progress;
724 progress = do_copy_propagation(ir) || progress;
725 if (linked)
726 progress = do_dead_code(ir) || progress;
727 else
728 progress = do_dead_code_unlinked(ir) || progress;
729 progress = do_dead_code_local(ir) || progress;
730 progress = do_tree_grafting(ir) || progress;
731 progress = do_constant_propagation(ir) || progress;
732 if (linked)
733 progress = do_constant_variable(ir) || progress;
734 else
735 progress = do_constant_variable_unlinked(ir) || progress;
736 progress = do_constant_folding(ir) || progress;
737 progress = do_algebraic(ir) || progress;
738 progress = do_if_return(ir) || progress;
739 progress = do_vec_index_to_swizzle(ir) || progress;
740 progress = do_swizzle_swizzle(ir) || progress;
Eric Anholt8f8cdbf2010-08-13 07:16:38 -0700741 progress = do_noop_swizzle(ir) || progress;
Eric Anholt2f4fe152010-08-10 13:06:49 -0700742
Ian Romanick8df2dbf2010-08-26 16:45:22 -0700743 loop_state *ls = analyze_loop_variables(ir);
744 progress = set_loop_controls(ir, ls) || progress;
745 delete ls;
746
Eric Anholt2f4fe152010-08-10 13:06:49 -0700747 return progress;
748}
Eric Anholtb8384642010-08-18 16:56:39 -0700749
750extern "C" {
751
752/**
753 * To be called at GL teardown time, this frees compiler datastructures.
754 *
755 * After calling this, any previously compiled shaders and shader
756 * programs would be invalid. So this should happen at approximately
757 * program exit.
758 */
759void
760_mesa_destroy_shader_compiler(void)
761{
762 _mesa_destroy_shader_compiler_caches();
763
764 _mesa_glsl_release_types();
765}
766
767/**
768 * Releases compiler caches to trade off performance for memory.
769 *
770 * Intended to be used with glReleaseShaderCompiler().
771 */
772void
773_mesa_destroy_shader_compiler_caches(void)
774{
775 _mesa_glsl_release_functions();
776}
777
778}