Skip to content

Commit

Permalink
Get rid of escapePoint->floating chain
Browse files Browse the repository at this point in the history
  • Loading branch information
shirok committed Oct 19, 2022
1 parent 768aa16 commit 56214b2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 53 deletions.
13 changes: 0 additions & 13 deletions src/gauche/priv/vmP.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ SCM_DECL_BEGIN
*/
typedef struct ScmEscapePointRec {
struct ScmEscapePointRec *prev;
struct ScmEscapePointRec *floating;
ScmObj ehandler; /* handler closure */
ScmContFrame *cont; /* saved continuation */
ScmObj handlers; /* saved dynamic handler chain */
Expand All @@ -104,18 +103,6 @@ typedef struct ScmEscapePointRec {
*/
} ScmEscapePoint;

/* Link management */
#define SCM_VM_FLOATING_EP(vm) \
((vm)->escapePoint? (vm)->escapePoint->floating : vm->escapePointFloating)
#define SCM_VM_FLOATING_EP_SET(vm, ep) \
do { \
if ((vm)->escapePoint) { \
(vm)->escapePoint->floating = (ep); \
} else { \
(vm)->escapePointFloating = (ep); \
} \
} while (0)

/* Escape types */
#define SCM_VM_ESCAPE_NONE 0
#define SCM_VM_ESCAPE_ERROR 1
Expand Down
4 changes: 0 additions & 4 deletions src/gauche/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -480,10 +480,6 @@ struct ScmVMRec {
continuation). used by system's default
exception handler to escape from the error
handlers. */
ScmEscapePoint *escapePointFloating;
/* reverse link of escape point chain
to keep 'active' EPs.
See ScmEscapePoint definition above. */
int escapeReason; /* temporary storage to pass data across
longjmp(). */
void *escapeData[2]; /* ditto. */
Expand Down
47 changes: 11 additions & 36 deletions src/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ ScmVM *Scm_NewVM(ScmVM *proto, ScmObj name)

v->exceptionHandler = DEFAULT_EXCEPTION_HANDLER;
v->exceptionHandlerStack = SCM_NIL;
v->escapePoint = v->escapePointFloating = NULL;
v->escapePoint = NULL;
v->escapeReason = SCM_VM_ESCAPE_NONE;
v->escapeData[0] = NULL;
v->escapeData[1] = NULL;
Expand Down Expand Up @@ -1115,11 +1115,6 @@ static void save_cont(ScmVM *vm)
ep->cont = FORWARDED_CONT(ep->cont);
}
}
for (ScmEscapePoint *ep = SCM_VM_FLOATING_EP(vm); ep; ep = ep->floating) {
if (FORWARDED_CONT_P(ep->cont)) {
ep->cont = FORWARDED_CONT(ep->cont);
}
}
vm->stackBase = vm->stack;
}

Expand Down Expand Up @@ -2366,9 +2361,6 @@ ScmObj Scm_VMDefaultExceptionHandler(ScmObj e)
overhead, but we need to keep all active EPs' cont field valid.
Without this, a complication occurs when save_cont occurs during
executing the error handler and it returns subsequently.
Ep->floating was introduced to deal with it, but it turned out
not covering all cases. Until we find a good way to keep track
of all active EPs, we fall back to this slow-but-safe approach.
See https://github.com/shirok/Gauche/issues/852 for the details.
*/
save_cont(vm);
Expand All @@ -2391,32 +2383,22 @@ ScmObj Scm_VMDefaultExceptionHandler(ScmObj e)

/* Pop the EP and run the error handler. */
vm->escapePoint = ep->prev;
SCM_VM_FLOATING_EP_SET(vm, ep);

vm->errorHandlerContinuable = FALSE;

SCM_UNWIND_PROTECT {
result = Scm_ApplyRec(ep->ehandler, SCM_LIST1(e));
result = Scm_ApplyRec(ep->ehandler, SCM_LIST1(e));

/* save return values */
/* NB: for loop is slightly faster than memcpy */
numVals = vm->numVals;
if (numVals > 1) {
for (int i=0; i<numVals-1; i++) rvals[i] = vm->vals[i];
}

/* call dynamic handlers to rewind */
if (!ep->rewindBefore) {
call_dynamic_handlers(ep->handlers, vm->handlers);
}
/* save return values */
/* NB: for loop is slightly faster than memcpy */
numVals = vm->numVals;
if (numVals > 1) {
for (int i=0; i<numVals-1; i++) rvals[i] = vm->vals[i];
}
SCM_WHEN_ERROR {
/* make sure the floating pointer is reset when an error is
signalled during handlers */
SCM_VM_FLOATING_EP_SET(vm, ep->floating);
SCM_NEXT_HANDLER;

/* call dynamic handlers to rewind */
if (!ep->rewindBefore) {
call_dynamic_handlers(ep->handlers, vm->handlers);
}
SCM_END_PROTECT;

/* If exception is reraised, the exception handler can return
to the caller. */
Expand All @@ -2425,19 +2407,16 @@ ScmObj Scm_VMDefaultExceptionHandler(ScmObj e)

/* recover escape point */
vm->escapePoint = ep;
SCM_VM_FLOATING_EP_SET(vm, ep->floating);

/* call dynamic handlers to reenter dynamic-winds */
call_dynamic_handlers(vmhandlers, ep->handlers);

/* reraise and return */
vm->exceptionHandler = ep->xhandler;
vm->escapePoint = ep->prev;
SCM_VM_FLOATING_EP_SET(vm, ep);
result = Scm_VMThrowException(vm, e, 0);
vm->exceptionHandler = DEFAULT_EXCEPTION_HANDLER;
vm->escapePoint = ep;
SCM_VM_FLOATING_EP_SET(vm, ep->floating);
return result;
}

Expand All @@ -2450,7 +2429,6 @@ ScmObj Scm_VMDefaultExceptionHandler(ScmObj e)

/* Install the continuation */
vm->cont = ep->cont;
SCM_VM_FLOATING_EP_SET(vm, ep->floating);
if (ep->errorReporting) {
SCM_VM_RUNTIME_FLAG_SET(vm, SCM_ERROR_BEING_REPORTED);
}
Expand Down Expand Up @@ -2614,7 +2592,6 @@ static ScmObj with_error_handler(ScmVM *vm, ScmObj handler,
* ep is valid.
*/
ep->prev = vm->escapePoint;
ep->floating = SCM_VM_FLOATING_EP(vm);
ep->ehandler = handler;
ep->cont = vm->cont;
ep->handlers = vm->handlers;
Expand Down Expand Up @@ -2927,7 +2904,6 @@ ScmObj Scm_VMCallCC(ScmObj proc)
ep->prev = NULL;
ep->ehandler = SCM_FALSE;
ep->cont = vm->cont;
ep->floating = NULL;
ep->handlers = vm->handlers;
ep->cstack = vm->cstack;
ep->resetChain = vm->resetChain;
Expand Down Expand Up @@ -2972,7 +2948,6 @@ ScmObj Scm_VMCallPC(ScmObj proc)
ep->prev = NULL;
ep->ehandler = SCM_FALSE;
ep->cont = (cp? vm->cont : NULL);
ep->floating = NULL;
ep->handlers = SCM_NIL; /* don't use for partial continuation */
ep->cstack = NULL; /* so that the partial continuation can be run
on any cstack state. */
Expand Down

0 comments on commit 56214b2

Please sign in to comment.