summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Thurston <thurston@complang.org>2012-08-30 10:11:46 -0400
committerAdrian Thurston <thurston@complang.org>2012-08-30 10:11:46 -0400
commit04f0ec491524920e53dcbd74541079dfa3076f56 (patch)
tree2479e2907f2ec17e5a6c8b9993880d843a20a194
parent0555af659a87fd943fe6b85b27aa787e4d523fdc (diff)
downloadcolm-04f0ec491524920e53dcbd74541079dfa3076f56.tar.gz
added separate functions for initializing and clearing the stack
-rw-r--r--colm/bytecode.h2
-rw-r--r--colm/program.c38
2 files changed, 32 insertions, 8 deletions
diff --git a/colm/bytecode.h b/colm/bytecode.h
index dc4e2ea8..d07dfb2e 100644
--- a/colm/bytecode.h
+++ b/colm/bytecode.h
@@ -432,8 +432,10 @@ typedef unsigned char uchar;
#define vm_local_iframe(o) (exec->iframePtr[o])
#define vm_plocal_iframe(o) (&exec->iframePtr[o])
+void vm_init( struct ColmProgram * );
Tree** vm_grow( struct ColmProgram *, Tree **, int );
Tree** vm_shrink( struct ColmProgram * );
+void vm_clear( struct ColmProgram * );
typedef Tree *SW;
typedef Tree **StackPtr;
diff --git a/colm/program.c b/colm/program.c
index a96b931e..ed983c46 100644
--- a/colm/program.c
+++ b/colm/program.c
@@ -76,6 +76,22 @@ void allocGlobal( Program *prg )
prg->global = tree;
}
+void vm_init( Program *prg )
+{
+ StackBlock *b = malloc( sizeof(StackBlock) );
+ b->data = malloc( sizeof(Tree*) * VM_STACK_SIZE );
+ b->len = VM_STACK_SIZE;
+ b->offset = 0;
+ b->next = 0;
+
+ prg->stackBlock = b;
+
+ prg->sb_beg = prg->stackBlock->data;
+ prg->sb_end = prg->stackBlock->data + prg->stackBlock->len;
+
+ prg->stackRoot = prg->sb_end;
+}
+
Tree **vm_grow( Program *prg, Tree **sp, int n )
{
/* Close off the current block. */
@@ -140,6 +156,17 @@ Tree **vm_shrink( Program *prg )
}
}
+void vm_clear( Program *prg )
+{
+ while ( prg->stackBlock != 0 ) {
+ StackBlock *b = prg->stackBlock;
+ prg->stackBlock = prg->stackBlock->next;
+
+ free( b->data );
+ free( b );
+ }
+}
+
Tree *returnVal( struct ColmProgram *prg )
{
return prg->returnVal;
@@ -177,12 +204,8 @@ Program *colmNewProgram( RuntimeData *rtd )
/* Allocate the global variable. */
allocGlobal( prg );
- /*
- * Allocate the VM stack. Give it one sentinal so that when execution pops
- * the first thing it pushes the stackRoot does not become invalid due to a
- * free.
- */
- prg->stackRoot = vm_grow( prg, 0, 1 );
+ /* Allocate the VM stack. */
+ vm_init( prg);
return prg;
}
@@ -280,8 +303,7 @@ int colmDeleteProgram( Program *prg )
rb = next;
}
- while ( prg->stackBlock->next != 0 )
- vm_shrink( prg );
+ vm_clear( prg );
free( prg );