summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSøren Sandmann Pedersen <ssp@redhat.com>2012-06-15 09:46:30 -0400
committerSøren Sandmann Pedersen <ssp@redhat.com>2012-06-15 09:46:30 -0400
commita109f716fd8c002a3b5f1d4dd04050faed91d218 (patch)
tree99c3017f19e08ff9c0ee8ac75ec5de43af68a4bd
parentaf23cfa151112860a4a15fd684859a6da24664e9 (diff)
Move levels to its own pass
The optimizer needs to know the embeddeng level of the various definitions. Previously, this was computed in the offsets pass, but if the optimizer removes unused variables, the offsets could change. The levels must be computed before optimizations, but offsets must be computers. So split levels to its own pass that runs before the optimizer.
-rw-r--r--Makefile1
-rw-r--r--ast.h3
-rw-r--r--levels.c67
-rw-r--r--main.c5
-rw-r--r--offsets.c23
5 files changed, 79 insertions, 20 deletions
diff --git a/Makefile b/Makefile
index 293a9d8..f1249c2 100644
--- a/Makefile
+++ b/Makefile
@@ -15,6 +15,7 @@ OBJS = \
switch.o \
graph.o \
optimize.o \
+ levels.o \
offsets.o \
init-check.o \
initialized-check.o \
diff --git a/ast.h b/ast.h
index 0cd08ff..ad532d1 100644
--- a/ast.h
+++ b/ast.h
@@ -1463,9 +1463,10 @@ gboolean type_check (ast_t *ast);
gboolean mark_constants (ast_t *ast);
gboolean switch_check (ast_t *ast);
gboolean graph (ast_t *ast);
-gboolean optimize (ast_t *ast);
gboolean init_check (ast_t *ast);
gboolean return_check (ast_t *ast);
+gboolean levels (ast_t *ast);
+gboolean optimize (ast_t *ast);
gboolean offsets (ast_t *ast);
void interpret (ast_t *ast);
diff --git a/levels.c b/levels.c
new file mode 100644
index 0000000..5399bf1
--- /dev/null
+++ b/levels.c
@@ -0,0 +1,67 @@
+/* Ninja
+ * Copyright 2007, Soren Sandmann Pedersen
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include "ast.h"
+
+static void
+do_levels (ast_t *ast, int level)
+{
+ GList *list;
+
+ if (ast_is (ast, AST_DEFINITION, AST_FUNCTION_DEFINITION))
+ {
+ ast_function_definition_t *function = &ast->definition.function;
+
+ function->level = level++;
+ }
+ else if (ast_is (ast, AST_DEFINITION, AST_CLASS_DEFINITION))
+ {
+ ast_class_definition_t *class = &ast->definition.class;
+
+ class->level = level++;
+ }
+ else if (ast->common.type == AST_EXPRESSION)
+ {
+ ast_expression_t *expr = &ast->expression;
+
+ expr->common.level = level;
+ }
+ else if (ast_is (ast, AST_DEFINITION, AST_LABEL_DEFINITION))
+ {
+ ast_label_definition_t *label = &(ast->definition.label);
+
+ label->level = level;
+ }
+ else if (ast_is (ast, AST_DEFINITION, AST_VARIABLE_DEFINITION))
+ {
+ ast_variable_definition_t *variable = &(ast->definition.variable);
+
+ variable->level = level;
+ }
+
+ for (list = ast->common.children->head; list; list = list->next)
+ do_levels (list->data, level);
+}
+
+gboolean
+levels (ast_t *ast)
+{
+ do_levels (ast, 0);
+
+ return TRUE;
+}
diff --git a/main.c b/main.c
index 6140f62..8ab9f25 100644
--- a/main.c
+++ b/main.c
@@ -58,12 +58,15 @@ compile (const char *input, gboolean do_optimize)
if (!return_check (ast))
return NULL;
- if (!offsets (ast))
+ if (!levels (ast))
return NULL;
if (do_optimize && !optimize (ast))
return NULL;
+ if (!offsets (ast))
+ return NULL;
+
return ast;
}
diff --git a/offsets.c b/offsets.c
index f6cd38d..93ead6f 100644
--- a/offsets.c
+++ b/offsets.c
@@ -27,7 +27,7 @@ list_def (gpointer key, gpointer value, gpointer data)
}
static int
-assign_offsets (symbol_table_t *symbols, int offset, int level, int *max)
+assign_offsets (symbol_table_t *symbols, int offset, int *max)
{
GPtrArray *definitions = g_ptr_array_new ();
int i;
@@ -47,7 +47,6 @@ assign_offsets (symbol_table_t *symbols, int offset, int level, int *max)
offset += (size - offset % size);
definition->variable.offset = offset;
- definition->variable.level = level;
offset += size;
}
@@ -62,7 +61,7 @@ assign_offsets (symbol_table_t *symbols, int offset, int level, int *max)
}
static void
-do_offsets (ast_t *ast, int offset, int level, int *max)
+do_offsets (ast_t *ast, int offset, int *max)
{
GList *list;
symbol_table_t *symbols = NULL;
@@ -85,7 +84,6 @@ do_offsets (ast_t *ast, int offset, int level, int *max)
if (0) /* FIXME: For now we don't support virtual methods */
offset += sizeof (gpointer); /* The vtable pointer */
- class->level = level++;
class->env_size = offset;
max = &(class->env_size);
@@ -100,17 +98,10 @@ do_offsets (ast_t *ast, int offset, int level, int *max)
* the old env,
* the return address */
- function->level = level++;
function->env_size = offset;
max = &(function->env_size);
}
- else if (ast_is (ast, AST_DEFINITION, AST_LABEL_DEFINITION))
- {
- ast_label_definition_t *label = &(ast->definition.label);
-
- label->level = level;
- }
else if (ast->common.type == AST_PROGRAM)
{
ast_program_t *program = &(ast->program);
@@ -121,22 +112,18 @@ do_offsets (ast_t *ast, int offset, int level, int *max)
max = &(program->env_size);
}
- else if (ast->common.type == AST_EXPRESSION)
- {
- ast->expression.common.level = level;
- }
if (symbols)
- offset = assign_offsets (symbols, offset, level, max);
+ offset = assign_offsets (symbols, offset, max);
for (list = ast->common.children->head; list != NULL; list = list->next)
- do_offsets (list->data, offset, level, max);
+ do_offsets (list->data, offset, max);
}
gboolean
offsets (ast_t *ast)
{
- do_offsets (ast, 0, 0, NULL);
+ do_offsets (ast, 0, NULL);
/* The offsets pass can never fail */
return TRUE;