Skip to content

Commit

Permalink
kernel: replace ExecBegin/ExecEnd
Browse files Browse the repository at this point in the history
We can instead store the old lvars on the stack resp. in the
interpreter state.
  • Loading branch information
fingolfin committed Jan 2, 2020
1 parent b97b002 commit 8ab6b11
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 116 deletions.
29 changes: 0 additions & 29 deletions src/funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ static ModuleStateOffset FuncsStateOffset = -1;

struct FuncsModuleState {
Int RecursionDepth;
Obj ExecState;
};

extern inline struct FuncsModuleState *FuncsState(void)
Expand Down Expand Up @@ -766,28 +765,6 @@ static void PrintFunccallOpts(Expr call)
Pr(" %4<)", 0, 0);
}



/****************************************************************************
**
*F ExecBegin() . . . . . . . . . . . . . . . . . . . . . begin an execution
*F ExecEnd(<error>) . . . . . . . . . . . . . . . . . . . end an execution
*/

void ExecBegin(Obj frame)
{
// remember the old execution state
PushPlist(FuncsState()->ExecState, STATE(CurrLVars));

// set up new state
SWITCH_TO_OLD_LVARS( frame );
}

void ExecEnd(UInt error)
{
// switch back to the old state
SWITCH_TO_OLD_LVARS(PopPlist(FuncsState()->ExecState));
}

/****************************************************************************
**
Expand Down Expand Up @@ -851,11 +828,6 @@ static Int InitKernel (
{
RecursionTrapInterval = 5000;

#if !defined(HPCGAP)
/* make the global variable known to Gasman */
InitGlobalBag( &FuncsState()->ExecState, "src/funcs.c:ExecState" );
#endif

/* Register the handler for our exported function */
InitHdlrFuncsFromTable( GVarFuncs );

Expand Down Expand Up @@ -922,7 +894,6 @@ static Int InitKernel (

static Int InitModuleState(void)
{
FuncsState()->ExecState = NEW_PLIST(T_PLIST_EMPTY, 16);
FuncsState()->RecursionDepth = 0;

return 0;
Expand Down
10 changes: 0 additions & 10 deletions src/funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@
*/
Obj MakeFunction(Obj fexp);

/****************************************************************************
**
*F ExecBegin( <frame> ) . . . . . . . . begin an execution in context frame
** if in doubt, pass STATE(BottomLVars) as <frame>
**
*F ExecEnd(<error>) . . . . . . . . . . . . . . . . . . . end an execution
*/
void ExecBegin(Obj frame);
void ExecEnd(UInt error);


/****************************************************************************
**
Expand Down
9 changes: 5 additions & 4 deletions src/hpc/threadapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "set.h"
#include "stats.h"
#include "stringobj.h"
#include "vars.h"

#include "hpc/guards.h"
#include "hpc/misc.h"
Expand Down Expand Up @@ -431,14 +432,15 @@ static GVarDescriptor GVarTHREAD_EXIT;

static void ThreadedInterpreter(void * funcargs)
{
Obj tmp, func;
Obj tmp, func, oldLVars;
int i;

// initialize everything and begin a fresh execution context
STATE(NrError) = 0;
STATE(ThrownObject) = 0;
oldLVars = STATE(CurrLVars);
SWITCH_TO_OLD_LVARS(STATE(BottomLVars));

ExecBegin(STATE(BottomLVars));
tmp = KEPTALIVE(funcargs);
StopKeepAlive(funcargs);
func = ELM_PLIST(tmp, 1);
Expand All @@ -460,13 +462,12 @@ static void ThreadedInterpreter(void * funcargs)
exit = GVarOptFunction(&GVarTHREAD_EXIT);
if (exit)
CALL_0ARGS(exit);
ExecEnd(0);
}
CATCH_ERROR
{
ExecEnd(1);
ClearError();
}
SWITCH_TO_OLD_LVARS(oldLVars);
}


Expand Down
16 changes: 8 additions & 8 deletions src/intrprtr.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,20 +243,23 @@ void IntrBegin(IntrState * intr, Obj frame)
/* no return-statement was yet interpreted */
intr->IntrReturning = 0;

/* start an execution environment */
ExecBegin(frame);
// remember the old execution state
intr->oldLVars = STATE(CurrLVars);

// start an execution environment
SWITCH_TO_OLD_LVARS( frame );
}

ExecStatus IntrEnd(IntrState * intr, UInt error, Obj *result)
{
UInt intrReturning; /* interpreted return-statement? */

// restore the execution environment
SWITCH_TO_OLD_LVARS(intr->oldLVars);

/* if everything went fine */
if ( ! error ) {

/* leave the execution environment */
ExecEnd(0);

/* remember whether the interpreter interpreted a return-statement */
intrReturning = intr->IntrReturning;

Expand All @@ -274,9 +277,6 @@ ExecStatus IntrEnd(IntrState * intr, UInt error, Obj *result)
/* otherwise clean up the mess */
else {

/* leave the execution environment */
ExecEnd(1);

/* clean up the coder too */
if ( intr->IntrCoding > 0 ) { CodeEnd(1); }

Expand Down
5 changes: 5 additions & 0 deletions src/intrprtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ ExecStatus IntrReturning;
*/
Obj StackObj;

/****************************************************************************
**
*F oldLVars
*/
Bag oldLVars;

};

Expand Down
6 changes: 4 additions & 2 deletions src/modules.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "stringobj.h"
#include "sysfiles.h"
#include "sysopt.h"
#include "vars.h"

#ifdef HAVE_DLOPEN
#include <dlfcn.h>
Expand Down Expand Up @@ -151,9 +152,10 @@ Int ActivateModule(StructInitInfo * info)
if (info->initLibrary) {
// Start a new executor to run the outer function of the module in
// global context
ExecBegin(STATE(BottomLVars));
Bag oldLvars = STATE(CurrLVars);
SWITCH_TO_OLD_LVARS(STATE(BottomLVars));
res = res || info->initLibrary(info);
ExecEnd(res);
SWITCH_TO_OLD_LVARS(oldLvars);
}
}

Expand Down
99 changes: 36 additions & 63 deletions src/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -2686,37 +2686,6 @@ void ReadEvalError(void)
}


/****************************************************************************
**
** Reader state -- the next group of functions are used to "push" the
** current interpreter state allowing GAP code to be interpreted in the
** middle of other code. This is used, for instance, in the command-line
** editor.
*/
struct SavedReaderState {
UInt userHasQuit;
syJmp_buf readJmpError;
UInt nrError;
};

static void SaveReaderState(struct SavedReaderState *s) {
s->userHasQuit = STATE(UserHasQuit);
s->nrError = STATE(NrError);
memcpy(s->readJmpError, STATE(ReadJmpError), sizeof(syJmp_buf));
}

static void ClearReaderState(void ) {
STATE(UserHasQuit) = 0;
STATE(NrError) = 0;
}

static void RestoreReaderState(const struct SavedReaderState *s) {
memcpy(STATE(ReadJmpError), s->readJmpError, sizeof(syJmp_buf));
STATE(UserHasQuit) = s->userHasQuit;
STATE(NrError) = s->nrError;
}


/****************************************************************************
**
*F Call0ArgsInNewReader(Obj f) . . . . . . . . . . . . call a GAP function
Expand All @@ -2725,30 +2694,32 @@ static void RestoreReaderState(const struct SavedReaderState *s) {
*/
Obj Call0ArgsInNewReader(Obj f)
{
struct SavedReaderState s;
Obj result;
syJmp_buf readJmpError;
volatile Obj result = 0;

// remember the old reader context
SaveReaderState(&s);
// remember the old state
UInt userHasQuit = STATE(UserHasQuit);
UInt nrError = STATE(NrError);
Bag oldLvars = STATE(CurrLVars);
memcpy(readJmpError, STATE(ReadJmpError), sizeof(syJmp_buf));

// initialize everything and begin a fresh execution context
ClearReaderState();
ExecBegin(STATE(BottomLVars));
// initialize everything
STATE(UserHasQuit) = 0;
STATE(NrError) = 0;
SWITCH_TO_OLD_LVARS(STATE(BottomLVars));

TRY_IF_NO_ERROR
{
result = CALL_0ARGS(f);
ExecEnd(0);
}
CATCH_ERROR
{
result = 0;
ExecEnd(1);
ClearError();
}

// switch back to the old reader context
RestoreReaderState(&s);
// switch back to the old state
SWITCH_TO_OLD_LVARS(oldLvars);
ClearError();
memcpy(STATE(ReadJmpError), readJmpError, sizeof(syJmp_buf));
STATE(UserHasQuit) = userHasQuit;
STATE(NrError) = nrError;

return result;
}

Expand All @@ -2761,30 +2732,32 @@ Obj Call0ArgsInNewReader(Obj f)
*/
Obj Call1ArgsInNewReader(Obj f, Obj a)
{
struct SavedReaderState s;
Obj result;
syJmp_buf readJmpError;
volatile Obj result = 0;

// remember the old reader context
SaveReaderState(&s);
// remember the old state
UInt userHasQuit = STATE(UserHasQuit);
UInt nrError = STATE(NrError);
Bag oldLvars = STATE(CurrLVars);
memcpy(readJmpError, STATE(ReadJmpError), sizeof(syJmp_buf));

// initialize everything and begin a fresh execution context
ClearReaderState();
ExecBegin(STATE(BottomLVars));
// initialize everything
STATE(UserHasQuit) = 0;
STATE(NrError) = 0;
SWITCH_TO_OLD_LVARS(STATE(BottomLVars));

TRY_IF_NO_ERROR
{
result = CALL_1ARGS(f, a);
ExecEnd(0);
}
CATCH_ERROR
{
result = 0;
ExecEnd(1);
ClearError();
}

// switch back to the old reader context
RestoreReaderState(&s);
// switch back to the old state
SWITCH_TO_OLD_LVARS(oldLvars);
ClearError();
memcpy(STATE(ReadJmpError), readJmpError, sizeof(syJmp_buf));
STATE(UserHasQuit) = userHasQuit;
STATE(NrError) = nrError;

return result;
}

Expand Down

0 comments on commit 8ab6b11

Please sign in to comment.