#include <fcntl.h>
#include <unistd.h>
+extern "C" {
+#include <talloc.h>
+}
+
#include "ast.h"
#include "glsl_parser_extras.h"
#include "glsl_parser.h"
#include "ir_optimization.h"
#include "ir_print_visitor.h"
+#include "program.h"
static char *
int dump_ast = 0;
int dump_lir = 0;
+int do_link = 0;
const struct option compiler_opts[] = {
{ "dump-ast", 0, &dump_ast, 1 },
{ "dump-lir", 0, &dump_lir, 1 },
+ { "link", 0, &do_link, 1 },
{ NULL, 0, NULL, 0 }
};
-int
-main(int argc, char **argv)
+void
+compile_shader(struct glsl_shader *shader)
{
struct _mesa_glsl_parse_state state;
- char *shader;
- size_t shader_len;
- exec_list instructions;
-
- int c;
- int idx = 0;
- while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1)
- /* empty */ ;
-
-
- if (argc <= optind)
- usage_fail(argv[0]);
-
- const unsigned len = strlen(argv[optind]);
- if (len < 6)
- usage_fail(argv[0]);
-
- const char *const ext = & argv[optind][len - 5];
- enum _mesa_glsl_parser_targets target;
- if (strncmp(".vert", ext, 5) == 0)
- target = vertex_shader;
- else if (strncmp(".geom", ext, 5) == 0)
- target = geometry_shader;
- else if (strncmp(".frag", ext, 5) == 0)
- target = fragment_shader;
- else
- usage_fail(argv[0]);
-
- shader = load_text_file(argv[optind], & shader_len);
memset(& state, 0, sizeof(state));
- state.target = target;
+ switch (shader->Type) {
+ case GL_VERTEX_SHADER: state.target = vertex_shader; break;
+ case GL_FRAGMENT_SHADER: state.target = fragment_shader; break;
+ case GL_GEOMETRY_SHADER: state.target = geometry_shader; break;
+ }
+
state.scanner = NULL;
state.translation_unit.make_empty();
state.symbols = new glsl_symbol_table;
+ state.info_log = talloc_strdup(shader, "");
state.error = false;
state.temp_index = 0;
state.loop_or_switch_nesting = NULL;
state.ARB_texture_rectangle_enable = true;
- _mesa_glsl_lexer_ctor(& state, shader, shader_len);
+ _mesa_glsl_lexer_ctor(& state, shader->Source, shader->SourceLen);
_mesa_glsl_parse(& state);
_mesa_glsl_lexer_dtor(& state);
printf("\n\n");
}
+ shader->ir.make_empty();
if (!state.error && !state.translation_unit.is_empty())
- _mesa_ast_to_hir(&instructions, &state);
+ _mesa_ast_to_hir(&shader->ir, &state);
/* Optimization passes */
- if (!state.error && !instructions.is_empty()) {
+ if (!state.error && !shader->ir.is_empty()) {
bool progress;
do {
progress = false;
- progress = do_function_inlining(&instructions) || progress;
- progress = do_if_simplification(&instructions) || progress;
- progress = do_copy_propagation(&instructions) || progress;
- progress = do_dead_code_local(&instructions) || progress;
- progress = do_dead_code_unlinked(&instructions) || progress;
- progress = do_constant_variable_unlinked(&instructions) || progress;
- progress = do_constant_folding(&instructions) || progress;
- progress = do_vec_index_to_swizzle(&instructions) || progress;
- progress = do_swizzle_swizzle(&instructions) || progress;
+ progress = do_function_inlining(&shader->ir) || progress;
+ progress = do_if_simplification(&shader->ir) || progress;
+ progress = do_copy_propagation(&shader->ir) || progress;
+ progress = do_dead_code_local(&shader->ir) || progress;
+ progress = do_dead_code_unlinked(&shader->ir) || progress;
+ progress = do_constant_variable_unlinked(&shader->ir) || progress;
+ progress = do_constant_folding(&shader->ir) || progress;
+ progress = do_vec_index_to_swizzle(&shader->ir) || progress;
+ progress = do_swizzle_swizzle(&shader->ir) || progress;
} while (progress);
}
/* Print out the resulting IR */
if (!state.error && dump_lir) {
- _mesa_print_ir(&instructions, &state);
+ _mesa_print_ir(&shader->ir, &state);
+ }
+
+ shader->symbols = state.symbols;
+ shader->CompileStatus = !state.error;
+
+ if (shader->InfoLog)
+ talloc_free(shader->InfoLog);
+
+ shader->InfoLog = state.info_log;
+
+ return;
+}
+
+int
+main(int argc, char **argv)
+{
+ int status = EXIT_SUCCESS;
+
+ int c;
+ int idx = 0;
+ while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1)
+ /* empty */ ;
+
+
+ if (argc <= optind)
+ usage_fail(argv[0]);
+
+ struct glsl_program whole_program;
+ memset(&whole_program, 0, sizeof(whole_program));
+
+ for (/* empty */; argc > optind; optind++) {
+ whole_program.Shaders = (struct glsl_shader **)
+ realloc(whole_program.Shaders,
+ sizeof(struct glsl_shader *) * (whole_program.NumShaders + 1));
+ assert(whole_program.Shaders != NULL);
+
+ /* talloc context should probably be whole_program */
+ struct glsl_shader *shader = talloc_zero(NULL, glsl_shader);
+
+ whole_program.Shaders[whole_program.NumShaders] = shader;
+ whole_program.NumShaders++;
+
+ const unsigned len = strlen(argv[optind]);
+ if (len < 6)
+ usage_fail(argv[0]);
+
+ const char *const ext = & argv[optind][len - 5];
+ if (strncmp(".vert", ext, 5) == 0)
+ shader->Type = GL_VERTEX_SHADER;
+ else if (strncmp(".geom", ext, 5) == 0)
+ shader->Type = GL_GEOMETRY_SHADER;
+ else if (strncmp(".frag", ext, 5) == 0)
+ shader->Type = GL_FRAGMENT_SHADER;
+ else
+ usage_fail(argv[0]);
+
+ shader->Source = load_text_file(argv[optind], &shader->SourceLen);
+ if (shader->Source == NULL) {
+ printf("File \"%s\" does not exist.\n", argv[optind]);
+ exit(EXIT_FAILURE);
+ }
+
+ compile_shader(shader);
+
+ if (!shader->CompileStatus) {
+ printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog);
+ status = EXIT_FAILURE;
+ break;
+ }
}
- delete state.symbols;
+ if ((status == EXIT_SUCCESS) && do_link) {
+ link_shaders(&whole_program);
+ status = (whole_program.LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
+ }
- return state.error != 0;
+ return status;
}