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 <cstdlib>
24#include <cstdio>
25#include <getopt.h>
26
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <fcntl.h>
30#include <unistd.h>
31
32#include "ast.h"
33#include "glsl_parser_extras.h"
34#include "glsl_parser.h"
35#include "ir_optimization.h"
36#include "ir_print_visitor.h"
37#include "program.h"
38#include "loop_analysis.h"
39
40#include "ir_to_llvm.h"
41
42#include "src/pixelflinger2/pixelflinger2.h"
43
44static int dump_ast = 0;
45static int dump_hir = 0;
46static int dump_lir = 0;
47
48extern "C" void
49compile_shader(const struct gl_context *ctx, struct gl_shader *shader)
50{
51   struct _mesa_glsl_parse_state *state =
52      new(shader) _mesa_glsl_parse_state(ctx, shader->Type, shader);
53
54   const char *source = shader->Source;
55   state->error = preprocess(state, &source, &state->info_log,
56			     state->extensions, ctx->API);
57
58   if (!state->error) {
59      _mesa_glsl_lexer_ctor(state, source);
60      _mesa_glsl_parse(state);
61      _mesa_glsl_lexer_dtor(state);
62   }
63
64   if (dump_ast) {
65      foreach_list_const(n, &state->translation_unit) {
66	 ast_node *ast = exec_node_data(ast_node, n, link);
67	 ast->print();
68      }
69      printf("\n\n");
70   }
71
72   shader->ir = new(shader) exec_list;
73   if (!state->error && !state->translation_unit.is_empty())
74      _mesa_ast_to_hir(shader->ir, state);
75
76   /* Print out the unoptimized IR. */
77   if (!state->error && dump_hir) {
78      validate_ir_tree(shader->ir);
79      _mesa_print_ir(shader->ir, state);
80   }
81
82   /* Optimization passes */
83   if (!state->error && !shader->ir->is_empty()) {
84      bool progress;
85      do {
86	 progress = do_common_optimization(shader->ir, false, 32);
87      } while (progress);
88
89      validate_ir_tree(shader->ir);
90   }
91
92
93   /* Print out the resulting IR */
94   if (!state->error && dump_lir) {
95      _mesa_print_ir(shader->ir, state);
96   }
97
98   shader->symbols = state->symbols;
99   shader->CompileStatus = !state->error;
100   shader->Version = state->language_version;
101   memcpy(shader->builtins_to_link, state->builtins_to_link,
102	  sizeof(shader->builtins_to_link[0]) * state->num_builtins_to_link);
103   shader->num_builtins_to_link = state->num_builtins_to_link;
104
105   if (shader->InfoLog)
106      hieralloc_free(shader->InfoLog);
107
108   shader->InfoLog = state->info_log;
109
110   /* Retain any live IR, but trash the rest. */
111   reparent_ir(shader->ir, shader);
112
113   hieralloc_free(state);
114
115   return;
116}
117