|
From: Keith J. <bu...@us...> - 2002-08-22 18:57:30
|
Update of /cvsroot/cup-language/cup/src/vm
In directory usw-pr-cvs1:/tmp/cvs-serv15220/vm
Modified Files:
Makefile debug.c execute.c rules.mk vm.c vm.h
Added Files:
scopes.c
Log Message:
Changlog? Changelog?? I don't need no stinkin' CHANGELOG!
Besides, no one is here but me. ;) -drops a pin, waits for echo-
--- NEW FILE: scopes.c ---
/* (c) CrimeBucket Productions */
#include "../cup.h"
#include "../common/dmem.h"
#include "vm.h"
char *scopes_c_id =
"@(#) $Id: scopes.c,v 1.1 2002/08/22 18:57:25 bucket Exp $";
CUPVALUE *cupvm_scope_findtoken(CUPVM *vm, const char *tok)
{
CUPSCOPE *scope;
CUPVALUE *val;
unsigned long depth = 0;
unsigned long idx = 0;
while ((scope = list_item_from_back(vm->scopes, depth)))
{
while ((val = list_item_from_front(scope->vars, idx)))
{
if (strcmp(val->name, tok) == 0)
return val;
idx++;
}
depth++;
}
return NULL;
}
CUPSCOPE *cupvm_scope_new(void)
{
CUPSCOPE *scope;
scope = dmalloc(sizeof(CUPSCOPE), TYPE_CUPVM);
scope->vars = NULL;
return scope;
}
void cupvm_scope_free(CUPSCOPE *scope)
{
dfree(scope);
}
bool cupvm_scope_push(CUPVM *vm, CUPSCOPE *scope)
{
if (!vm->scopes)
vm->scopes = list_new();
if (!vm->scopes)
return FALSE;
list_item_push_back(vm->scopes, scope);
return TRUE;
}
CUPSCOPE *cupvm_scope_pop(CUPVM *vm)
{
CUPSCOPE *ret;
if (!vm->scopes)
return NULL;
ret = (CUPSCOPE *) list_item_pop_back(vm->scopes);
if (!ret)
return NULL;
if (list_length(vm->scopes) == 0)
{
list_free(vm->scopes);
vm->scopes = NULL;
}
return ret;
}
Index: Makefile
===================================================================
RCS file: /cvsroot/cup-language/cup/src/vm/Makefile,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -r1.5 -r1.6
*** Makefile 2 Aug 2002 15:50:44 -0000 1.5
--- Makefile 22 Aug 2002 18:57:25 -0000 1.6
***************
*** 4,9 ****
-include ../rules.mk
! OBJS = debug.o execute.o values.o vm.o
! SRCS = debug.c execute.c values.c vm.c
HDRS = vm.h
--- 4,9 ----
-include ../rules.mk
! OBJS = debug.o execute.o scopes.o values.o vm.o
! SRCS = debug.c execute.c scopes.c values.c vm.c
HDRS = vm.h
***************
*** 14,17 ****
--- 14,19 ----
execute.o: execute.c $(HDRS) ../cup.h
$(CC) $(CFLAGS) -c execute.c
+ scopes.o: scopes.c $(HDRS) ../cup.h
+ $(CC) $(CFLAGS) -c scopes.c
values.o: values.c $(HDRS) ../cup.h
$(CC) $(CFLAGS) -c values.c
Index: debug.c
===================================================================
RCS file: /cvsroot/cup-language/cup/src/vm/debug.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -r1.9 -r1.10
*** debug.c 2 Aug 2002 15:50:44 -0000 1.9
--- debug.c 22 Aug 2002 18:57:25 -0000 1.10
***************
*** 6,9 ****
--- 6,19 ----
char *debug_c_id = "@(#) $Id$";
+ // Data from ../cup.h
+ char *op_to_str[] = {
+ "NOP", "PUSH", "SET", "ADD",
+ "SUB", "MUL", "DIV", "MOD",
+ "NEG", "NOT", "EQ", "JMP",
+ "JMPF", "JMPT", "JMPR", "RET",
+ "ARG", "POP", "VAR", "PUSHSCOPE",
+ "POPSCOPE", "EXIT"
+ };
+
void cupvm_debug(CUPVM *vm)
{
***************
*** 101,130 ****
{
case CUPOP_ADD:
- printf("%4lX: ADD\n", display);
- break;
case CUPOP_SUB:
- printf("%4lX: SUB\n", display);
- break;
case CUPOP_MUL:
- printf("%4lX: MUL\n", display);
- break;
case CUPOP_DIV:
- printf("%4lX: DIV\n", display);
- break;
case CUPOP_MOD:
- printf("%4lX: MOD\n", display);
- break;
case CUPOP_NEG:
- printf("%4lX: NEG\n", display);
- break;
case CUPOP_NOT:
- printf("%4lX: NOT\n", display);
- break;
case CUPOP_EQ:
- printf("%4lX: EQ\n", display);
- break;
case CUPOP_SET:
! printf("%4lX: SET\n", display);
break;
case CUPOP_JMP:
memcpy(&ul, &(vm->binary[pc + 1]), sizeof(unsigned long));
--- 111,130 ----
{
case CUPOP_ADD:
case CUPOP_SUB:
case CUPOP_MUL:
case CUPOP_DIV:
case CUPOP_MOD:
case CUPOP_NEG:
case CUPOP_NOT:
case CUPOP_EQ:
case CUPOP_SET:
! case CUPOP_RET:
! case CUPOP_EXIT:
! case CUPOP_POP:
! case CUPOP_PUSHSCOPE:
! case CUPOP_POPSCOPE:
! printf("%4lX: %s\n", display, op_to_str[(int) vm->binary[pc]]);
break;
+
case CUPOP_JMP:
memcpy(&ul, &(vm->binary[pc + 1]), sizeof(unsigned long));
***************
*** 143,149 ****
printf("%4lX: JMPR: %lX\n", display, ul);
break;
- case CUPOP_RET:
- printf("%4lX: RET\n", display);
- break;
case CUPOP_PUSH:
printf("%4lX: PUSH ", display);
--- 143,146 ----
***************
*** 160,175 ****
memcpy(&ul, &(vm->binary[pc + 1]), sizeof(unsigned long));
cupvm_debug_data(vm, ul + (vm->dataptr - vm->binary));
- break;
- case CUPOP_EXIT:
- printf("%4lX: EXIT\n", display);
- break;
- case CUPOP_POP:
- printf("%4lX: POP\n", display);
- break;
- case CUPOP_PUSHSCOPE:
- printf("%4lX: PUSHSCOPE\n", display);
- break;
- case CUPOP_POPSCOPE:
- printf("%4lX: POPSCOPE\n", display);
break;
default:
--- 157,160 ----
Index: execute.c
===================================================================
RCS file: /cvsroot/cup-language/cup/src/vm/execute.c,v
retrieving revision 1.11
retrieving revision 1.12
diff -C2 -r1.11 -r1.12
*** execute.c 2 Aug 2002 15:50:44 -0000 1.11
--- execute.c 22 Aug 2002 18:57:25 -0000 1.12
***************
*** 81,85 ****
--- 81,93 ----
break;
case CUPOP_PUSHSCOPE:
+ cupvm_PUSHSCOPE(vm);
+ if (vm->traceback)
+ return;
+ vm->pc++;
+ break;
case CUPOP_POPSCOPE:
+ cupvm_POPSCOPE(vm);
+ if (vm->traceback)
+ return;
vm->pc++;
break;
***************
*** 93,96 ****
--- 101,135 ----
}
+ void cupvm_PUSHSCOPE(CUPVM *vm)
+ {
+ CUPSCOPE *scope;
+
+ scope = cupvm_scope_new();
+ if (!cupvm_scope_push(vm, scope))
+ cupvm_tb(vm, "cupvm_PUSHSCOPE()", "failed to push scope");
+ }
+
+ void cupvm_POPSCOPE(CUPVM *vm)
+ {
+ CUPVALUE *val;
+ CUPSCOPE *scope;
+
+ scope = cupvm_scope_pop(vm);
+ if (!scope)
+ {
+ cupvm_tb(vm, "cupvm_POPSCOPE()", "failed to pop scope");
+ return;
+ }
+
+ if (scope->vars)
+ {
+ while ((val = (CUPVALUE *) list_item_pop_back(scope->vars)))
+ cupvm_value_free(val);
+ list_free(scope->vars);
+ }
+
+ cupvm_scope_free(scope);
+ }
+
void cupvm_PUSH(CUPVM *vm, unsigned long addr)
{
***************
*** 130,138 ****
case CUPDATA_VARIABLE:
// TEMPORARY. Need scoping/variable code
! val = cupvm_value_new();
! val->type = CUPTYPE_INTEGER;
! val->data.val_integer = 0;
! if (!cupvm_value_push(vm, val))
! cupvm_tb(vm, "cupvm_PUSH()", "failed to push value");
return;
default:
--- 169,175 ----
case CUPDATA_VARIABLE:
// TEMPORARY. Need scoping/variable code
! val = cupvm_scope_findtoken(vm, &(vm->dataptr[addr + 1]));
! if (!val)
! cupvm_tb(vm, "cupvm_PUSH()", "could not find variable in scope");
return;
default:
Index: rules.mk
===================================================================
RCS file: /cvsroot/cup-language/cup/src/vm/rules.mk,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** rules.mk 4 May 2002 18:27:49 -0000 1.1
--- rules.mk 22 Aug 2002 18:57:25 -0000 1.2
***************
*** 2,6 ****
# @(#) $Id$
! VMOBJS = vm/debug.o vm/execute.o vm/values.o vm/vm.o
! VMSRCS = vm/debug.c vm/execute.c vm/values.c vm/vm.c
VMHDRS = vm/vm.h
--- 2,6 ----
# @(#) $Id$
! VMOBJS = vm/debug.o vm/execute.o vm/scopes.o vm/values.o vm/vm.o
! VMSRCS = vm/debug.c vm/execute.c vm/scopes.c vm/values.c vm/vm.c
VMHDRS = vm/vm.h
Index: vm.c
===================================================================
RCS file: /cvsroot/cup-language/cup/src/vm/vm.c,v
retrieving revision 1.16
retrieving revision 1.17
diff -C2 -r1.16 -r1.17
*** vm.c 2 Aug 2002 15:50:44 -0000 1.16
--- vm.c 22 Aug 2002 18:57:25 -0000 1.17
***************
*** 53,58 ****
vm->retval = NULL;
vm->stack = NULL;
! cupvm_execute(vm);
if (vm->stack)
--- 53,64 ----
vm->retval = NULL;
vm->stack = NULL;
+ vm->scopes = NULL;
! cupvm_PUSHSCOPE(vm);
! if (!vm->traceback)
! {
! cupvm_execute(vm);
! cupvm_POPSCOPE(vm);
! }
if (vm->stack)
Index: vm.h
===================================================================
RCS file: /cvsroot/cup-language/cup/src/vm/vm.h,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -r1.6 -r1.7
*** vm.h 2 Aug 2002 15:50:44 -0000 1.6
--- vm.h 22 Aug 2002 18:57:25 -0000 1.7
***************
*** 10,20 ****
};
void cupvm_tb(CUPVM *, const char *, const char *);
void cupvm_debug(CUPVM *);
void cupvm_debug_op(CUPVM *, unsigned long, unsigned long);
void cupvm_debug_data(CUPVM *, unsigned long);
CUPVALUE *cupvm_value_new(void);
void cupvm_value_free(CUPVALUE *);
- void cupvm_execute(CUPVM *);
bool cupvm_value_push(CUPVM *, CUPVALUE *);
CUPVALUE *cupvm_value_pop(CUPVM *);
--- 10,33 ----
};
+ typedef struct cupscope CUPSCOPE;
+ struct cupscope
+ {
+ LIST *vars;
+ };
+
void cupvm_tb(CUPVM *, const char *, const char *);
void cupvm_debug(CUPVM *);
void cupvm_debug_op(CUPVM *, unsigned long, unsigned long);
void cupvm_debug_data(CUPVM *, unsigned long);
+ void cupvm_execute(CUPVM *);
CUPVALUE *cupvm_value_new(void);
void cupvm_value_free(CUPVALUE *);
bool cupvm_value_push(CUPVM *, CUPVALUE *);
CUPVALUE *cupvm_value_pop(CUPVM *);
+ CUPSCOPE *cupvm_scope_new(void);
+ void cupvm_scope_free(CUPSCOPE *);
+ bool cupvm_scope_push(CUPVM *, CUPSCOPE *);
+ CUPSCOPE *cupvm_scope_pop(CUPVM *);
+ CUPVALUE *cupvm_scope_findtoken(CUPVM *, const char *);
+ void cupvm_PUSHSCOPE(CUPVM *);
+ void cupvm_POPSCOPE(CUPVM *);
|