From: Peep P. <so...@us...> - 2004-03-18 20:56:53
|
Update of /cvsroot/agd/server/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1867 Modified Files: interpret.c Log Message: Fixes and changes, cleanup. Index: interpret.c =================================================================== RCS file: /cvsroot/agd/server/src/interpret.c,v retrieving revision 1.11 retrieving revision 1.12 diff -u -d -r1.11 -r1.12 --- interpret.c 16 Mar 2004 14:12:07 -0000 1.11 +++ interpret.c 18 Mar 2004 20:47:08 -0000 1.12 @@ -4,14 +4,10 @@ #include <stdarg.h> #include <setjmp.h> -int stop_execution; - char *opc_name(int i); -/* jmp_buf error_context; int have_error_context; -*/ #ifdef DEBUG int dont_debug_interpreter; @@ -22,7 +18,7 @@ /* This is the stack containing all local variables, function arguments and function return values. */ variable_t value_stack[VALUE_STACK_SIZE]; -variable_t *sp = value_stack, /* Stack pointer to the last element pushed. */ +variable_t *sp = value_stack, /* Stack pointer after the last element pushed. */ *fp; /* Frame pointer - where locals begin on the stack */ int *cp; /* Code pointer into code->data */ @@ -39,12 +35,12 @@ control_stack_t control_stack[CONTROL_STACK_SIZE]; control_stack_t *csp = control_stack; -#define CHECK_CSP() if(csp >= control_stack + CONTROL_STACK_SIZE) {\ - runtime("control stack overflow (too deep recursion)"); return; } -#define CHECK_SP() if(sp >= value_stack + VALUE_STACK_SIZE) {\ - runtime("value stack overflow"); return; } +#define CHECK_CSP() if(csp >= control_stack + CONTROL_STACK_SIZE) \ + runtime("control stack overflow (too deep recursion)"); +#define CHECK_SP() if(sp >= value_stack + VALUE_STACK_SIZE) \ + runtime("value stack overflow"); -void (**dfptr)(void); +extern void (**dfptr)(void); void call_function(object_t *ob, int index); void do_assign(variable_t *lval, variable_t *rval); @@ -68,8 +64,7 @@ } out: - stop_execution = 1; -/* longjmp(error_context, 1);*/ + longjmp(error_context, 1); } int get_fun_index(object_t *ob, char *fun) @@ -116,6 +111,14 @@ CHECK_SP() } +void push_lvalue(variable_t *v) +{ + sp->type = T_LVALUE; + sp->u.v = v; + sp++; + CHECK_SP() +} + void free_value(variable_t *v); void push_var(variable_t *v) { @@ -226,7 +229,6 @@ csp--; if(csp < control_stack) { runtime("control stack underflow"); - return; } fp = csp->fp; @@ -296,14 +298,11 @@ break; case F_PUSH_LVAR_LVALUE: cp++; - sp->type = T_LVALUE; - sp->u.v = &fp[*cp]; - if(sp->u.v == sp) { + push_lvalue(&fp[*cp]); +/* if(sp->u.v == sp) { runtime("lvalue points to itself"); return; - } - sp++; - CHECK_SP() + }*/ break; case F_PUSH_GVAR: cp++; @@ -311,10 +310,7 @@ break; case F_PUSH_GVAR_LVALUE: cp++; - sp->type = T_LVALUE; - sp->u.v = this_ob->variables.data[*cp]; - sp++; - CHECK_SP() + push_lvalue(this_ob->variables.data[*cp]); break; case F_ADD: tmp = add_vars(sp-2, sp-1); @@ -368,6 +364,19 @@ tmpi = --tmp->u.i; push_int(tmpi); break; + case F_RANGE: + tmpi = sp[-2].u.s[sp[-1].u.i]; + pop_stack(); pop_stack(); + push_int(tmpi); + break; + case F_RANGE_LVALUE: + /* A bit hacky. */ + *sp = sp[-2]; + sp->u.s += sp[-1].u.i; + pop_stack(); pop_stack(); + push_var(sp+2); + push_lvalue(sp-1); + break; case F_JMPF: sp--; if(!test_var(sp)) { @@ -459,10 +468,20 @@ name = (char *) *++cp; ++cp; sp--; - ob = sp->u.ob; + if(sp->type == T_OBJECT) { + ob = sp->u.ob; + } else { + char *s; + /* T_STRING */ + s = sp->u.s; + ob = find_object(s); + if(!ob) { + ob = load_object(s); + } + } + if(!ob) { runtime("call_other on NULL object"); - return; } index = get_fun_index(ob, name); @@ -477,9 +496,13 @@ case F_RETURN: { variable_t tmp; - tmp = sp[-1]; + do_assign(&tmp, sp-1); + pop_locals(); push_var(&tmp); - pop_control_stack(1); + if(csp == saved_csp + 1) + pop_control_stack(1); + else + pop_control_stack(0); } break; default: @@ -491,13 +514,13 @@ show_stack(); #endif /* We return to the control level where we entered - we have finished. */ - if(csp == saved_csp) { /*&control_stack[0]*/ + if(csp == saved_csp) { debug("interpret", "csp (%d) == saved_csp (%d); returning\n", csp - &control_stack[0], saved_csp - &control_stack[0]); return; } /* Abnormal termination - need to pop unnecessary stuff off the stack. */ - if(stop_execution || this_ob->flags & O_DESTRUCTED) { - debug("interpret", "stop_execution(%d) or this_ob destructed (%d); returning\n", stop_execution, this_ob->flags & O_DESTRUCTED); + if(this_ob->flags & O_DESTRUCTED) { + debug("interpret", "this_ob destructed; returning\n"); /* Should we pop to the bottom? */ pop_control_stack(1); pop_locals(); @@ -507,28 +530,37 @@ goto again; } -/* This calls function number index in object ob. - The return value goes at fp[-1]. -*/ void call_function(object_t *ob, int index) { - int i; + int i, j; function_t *f; f = ob->prog->functions.data[index]; if(!f) { char buf[256]; sprintf(buf, "function %d doesn't exist in object \"%s\"", index, ob->name); runtime(buf); - return; } /* funcno = index; lineno = f->lineno;*/ -/* - if(csp->num_arg < f->args.length) { - + + i = f->args.length - csp->num_arg; + if(i > 0) { + for(j=0;j<i;j++) { + switch((*(variable_t*)f->args.data[j]).type) { + case T_INT: + push_int(0); + break; + case T_OBJECT: + push_object(NULL); + break; + case T_STRING: + push_string("0", ST_STATIC); + break; + } + } } -*/ + #ifdef DEBUG debug("interpret", "call_function %d on %s\n", index, ob->name); #endif @@ -550,7 +582,6 @@ csp->fp = fp; csp->sp = sp; cp = 0; - stop_execution = 0; } /* apply(): This is what the driver uses to call functions in LPC objects @@ -567,6 +598,7 @@ int index; va_list va; char *p; + int i, j; #ifdef DEBUG struct timeval tm, tm2; #endif @@ -613,7 +645,6 @@ debug("lpc", "apply \"%s\" on \"%s\"\n", fun, ob->name); #endif -#if 0 if(!have_error_context) { if(setjmp(error_context)) { /* setjmp() returned a value, @@ -622,7 +653,6 @@ } have_error_context = 1; } -#endif call_function(ob, index); eval_instruction(); |