summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Romanick <ian.d.romanick@intel.com>2010-06-14 14:46:09 -0700
committerIan Romanick <ian.d.romanick@intel.com>2010-06-14 14:46:09 -0700
commit61d4aa041f800429127aab3263c50cbc6e69ae50 (patch)
tree94e026fd92bd8fd1b63917522f6fd7df8d7f59b7
parent824b659d917a5f14a1f66b891d25036ef9f9adc6 (diff)
Move stand-alone compiler main routine to main.cpp
-rw-r--r--Makefile.am1
-rw-r--r--glsl_parser_extras.cpp148
-rw-r--r--main.cpp175
3 files changed, 176 insertions, 148 deletions
diff --git a/Makefile.am b/Makefile.am
index 544d446aec..44748dc886 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -24,6 +24,7 @@ AUTOMAKE_OPTIONS = foreign
bin_PROGRAMS = glsl
glsl_SOURCES = \
+ main.cpp \
builtin_types.h \
symbol_table.c hash_table.c glsl_types.cpp \
glsl_parser.ypp glsl_lexer.lpp glsl_parser_extras.cpp \
diff --git a/glsl_parser_extras.cpp b/glsl_parser_extras.cpp
index 5ebbc27aca..8cf765f95d 100644
--- a/glsl_parser_extras.cpp
+++ b/glsl_parser_extras.cpp
@@ -22,21 +22,12 @@
*/
#include <stdio.h>
#include <stdarg.h>
-#include <stdlib.h>
#include <string.h>
#include <assert.h>
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <unistd.h>
#include "ast.h"
#include "glsl_parser_extras.h"
#include "glsl_parser.h"
-#include "ir_optimization.h"
-#include "ir_print_visitor.h"
-#include "ir_reader.h"
const char *
_mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target)
@@ -639,142 +630,3 @@ ast_struct_specifier::ast_struct_specifier(char *identifier,
name = identifier;
this->declarations.push_degenerate_list_at_head(&declarator_list->link);
}
-
-
-static char *
-load_text_file(const char *file_name, size_t *size)
-{
- char *text = NULL;
- struct stat st;
- ssize_t total_read = 0;
- int fd = open(file_name, O_RDONLY);
-
- *size = 0;
- if (fd < 0) {
- return NULL;
- }
-
- if (fstat(fd, & st) == 0) {
- text = (char *) malloc(st.st_size + 1);
- if (text != NULL) {
- do {
- ssize_t bytes = read(fd, text + total_read,
- st.st_size - total_read);
- if (bytes < 0) {
- free(text);
- text = NULL;
- break;
- }
-
- if (bytes == 0) {
- break;
- }
-
- total_read += bytes;
- } while (total_read < st.st_size);
-
- text[total_read] = '\0';
- *size = total_read;
- }
- }
-
- close(fd);
-
- return text;
-}
-
-
-int
-main(int argc, char **argv)
-{
- struct _mesa_glsl_parse_state state;
- char *shader;
- size_t shader_len;
- exec_list instructions;
-
- if (argc < 3) {
- printf("Usage: %s [v|g|f|i] <shader_file>\n", argv[0]);
- return EXIT_FAILURE;
- }
-
- memset(& state, 0, sizeof(state));
-
- switch (argv[1][0]) {
- case 'v':
- state.target = vertex_shader;
- break;
- case 'g':
- state.target = geometry_shader;
- break;
- case 'f':
- state.target = fragment_shader;
- break;
- case 'i':
- state.target = ir_shader;
- break;
- default:
- printf("Usage: %s [v|g|f|i] <shader_file>\n", argv[0]);
- return EXIT_FAILURE;
- }
-
- shader = load_text_file(argv[2], & shader_len);
-
- state.scanner = NULL;
- state.translation_unit.make_empty();
- state.symbols = new glsl_symbol_table;
- state.error = false;
- state.temp_index = 0;
- state.loop_or_switch_nesting = NULL;
- state.ARB_texture_rectangle_enable = true;
-
- if (state.target != ir_shader) {
- _mesa_glsl_lexer_ctor(& state, shader, shader_len);
- _mesa_glsl_parse(& state);
- _mesa_glsl_lexer_dtor(& state);
-
- foreach_list_const(n, &state.translation_unit) {
- ast_node *ast = exec_node_data(ast_node, n, link);
- ast->print();
- }
-
- if (!state.error && !state.translation_unit.is_empty())
- _mesa_ast_to_hir(&instructions, &state);
- } else {
- /* FINISHME: We should initialize this to the max GLSL version supported
- * FINISHME: by the driver. At the moment, we don't know what that is.
- */
- state.language_version = 130;
- _mesa_glsl_initialize_types(&state);
-
- _mesa_glsl_read_ir(&state, &instructions, shader);
- }
-
- /* Optimization passes */
- if (!state.error && !instructions.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;
- } while (progress);
- }
-
- /* Print out the resulting IR */
- printf("\n\n");
-
- if (!state.error) {
- _mesa_print_ir(&instructions, &state);
- }
-
- delete state.symbols;
-
- return state.error != 0;
-}
diff --git a/main.cpp b/main.cpp
new file mode 100644
index 0000000000..12fb70315c
--- /dev/null
+++ b/main.cpp
@@ -0,0 +1,175 @@
+/*
+ * Copyright © 2008, 2009 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+#include <cstdlib>
+#include <cstdio>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "ast.h"
+#include "glsl_parser_extras.h"
+#include "glsl_parser.h"
+#include "ir_optimization.h"
+#include "ir_print_visitor.h"
+#include "ir_reader.h"
+
+
+static char *
+load_text_file(const char *file_name, size_t *size)
+{
+ char *text = NULL;
+ struct stat st;
+ ssize_t total_read = 0;
+ int fd = open(file_name, O_RDONLY);
+
+ *size = 0;
+ if (fd < 0) {
+ return NULL;
+ }
+
+ if (fstat(fd, & st) == 0) {
+ text = (char *) malloc(st.st_size + 1);
+ if (text != NULL) {
+ do {
+ ssize_t bytes = read(fd, text + total_read,
+ st.st_size - total_read);
+ if (bytes < 0) {
+ free(text);
+ text = NULL;
+ break;
+ }
+
+ if (bytes == 0) {
+ break;
+ }
+
+ total_read += bytes;
+ } while (total_read < st.st_size);
+
+ text[total_read] = '\0';
+ *size = total_read;
+ }
+ }
+
+ close(fd);
+
+ return text;
+}
+
+
+int
+main(int argc, char **argv)
+{
+ struct _mesa_glsl_parse_state state;
+ char *shader;
+ size_t shader_len;
+ exec_list instructions;
+
+ if (argc < 3) {
+ printf("Usage: %s [v|g|f|i] <shader_file>\n", argv[0]);
+ return EXIT_FAILURE;
+ }
+
+ memset(& state, 0, sizeof(state));
+
+ switch (argv[1][0]) {
+ case 'v':
+ state.target = vertex_shader;
+ break;
+ case 'g':
+ state.target = geometry_shader;
+ break;
+ case 'f':
+ state.target = fragment_shader;
+ break;
+ case 'i':
+ state.target = ir_shader;
+ break;
+ default:
+ printf("Usage: %s [v|g|f|i] <shader_file>\n", argv[0]);
+ return EXIT_FAILURE;
+ }
+
+ shader = load_text_file(argv[2], & shader_len);
+
+ state.scanner = NULL;
+ state.translation_unit.make_empty();
+ state.symbols = new glsl_symbol_table;
+ state.error = false;
+ state.temp_index = 0;
+ state.loop_or_switch_nesting = NULL;
+ state.ARB_texture_rectangle_enable = true;
+
+ if (state.target != ir_shader) {
+ _mesa_glsl_lexer_ctor(& state, shader, shader_len);
+ _mesa_glsl_parse(& state);
+ _mesa_glsl_lexer_dtor(& state);
+
+ foreach_list_const(n, &state.translation_unit) {
+ ast_node *ast = exec_node_data(ast_node, n, link);
+ ast->print();
+ }
+
+ if (!state.error && !state.translation_unit.is_empty())
+ _mesa_ast_to_hir(&instructions, &state);
+ } else {
+ /* FINISHME: We should initialize this to the max GLSL version supported
+ * FINISHME: by the driver. At the moment, we don't know what that is.
+ */
+ state.language_version = 130;
+ _mesa_glsl_initialize_types(&state);
+
+ _mesa_glsl_read_ir(&state, &instructions, shader);
+ }
+
+ /* Optimization passes */
+ if (!state.error && !instructions.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;
+ } while (progress);
+ }
+
+ /* Print out the resulting IR */
+ printf("\n\n");
+
+ if (!state.error) {
+ _mesa_print_ir(&instructions, &state);
+ }
+
+ delete state.symbols;
+
+ return state.error != 0;
+}