Skip to content

Commit 8ab6b11

Browse files
committed
kernel: replace ExecBegin/ExecEnd
We can instead store the old lvars on the stack resp. in the interpreter state.
1 parent b97b002 commit 8ab6b11

7 files changed

Lines changed: 58 additions & 116 deletions

File tree

src/funcs.c

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ static ModuleStateOffset FuncsStateOffset = -1;
4343

4444
struct FuncsModuleState {
4545
Int RecursionDepth;
46-
Obj ExecState;
4746
};
4847

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

769-
770-
771-
/****************************************************************************
772-
**
773-
*F ExecBegin() . . . . . . . . . . . . . . . . . . . . . begin an execution
774-
*F ExecEnd(<error>) . . . . . . . . . . . . . . . . . . . end an execution
775-
*/
776-
777-
void ExecBegin(Obj frame)
778-
{
779-
// remember the old execution state
780-
PushPlist(FuncsState()->ExecState, STATE(CurrLVars));
781-
782-
// set up new state
783-
SWITCH_TO_OLD_LVARS( frame );
784-
}
785-
786-
void ExecEnd(UInt error)
787-
{
788-
// switch back to the old state
789-
SWITCH_TO_OLD_LVARS(PopPlist(FuncsState()->ExecState));
790-
}
791768

792769
/****************************************************************************
793770
**
@@ -851,11 +828,6 @@ static Int InitKernel (
851828
{
852829
RecursionTrapInterval = 5000;
853830

854-
#if !defined(HPCGAP)
855-
/* make the global variable known to Gasman */
856-
InitGlobalBag( &FuncsState()->ExecState, "src/funcs.c:ExecState" );
857-
#endif
858-
859831
/* Register the handler for our exported function */
860832
InitHdlrFuncsFromTable( GVarFuncs );
861833

@@ -922,7 +894,6 @@ static Int InitKernel (
922894

923895
static Int InitModuleState(void)
924896
{
925-
FuncsState()->ExecState = NEW_PLIST(T_PLIST_EMPTY, 16);
926897
FuncsState()->RecursionDepth = 0;
927898

928899
return 0;

src/funcs.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,6 @@
2727
*/
2828
Obj MakeFunction(Obj fexp);
2929

30-
/****************************************************************************
31-
**
32-
*F ExecBegin( <frame> ) . . . . . . . . begin an execution in context frame
33-
** if in doubt, pass STATE(BottomLVars) as <frame>
34-
**
35-
*F ExecEnd(<error>) . . . . . . . . . . . . . . . . . . . end an execution
36-
*/
37-
void ExecBegin(Obj frame);
38-
void ExecEnd(UInt error);
39-
4030

4131
/****************************************************************************
4232
**

src/hpc/threadapi.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "set.h"
3131
#include "stats.h"
3232
#include "stringobj.h"
33+
#include "vars.h"
3334

3435
#include "hpc/guards.h"
3536
#include "hpc/misc.h"
@@ -431,14 +432,15 @@ static GVarDescriptor GVarTHREAD_EXIT;
431432

432433
static void ThreadedInterpreter(void * funcargs)
433434
{
434-
Obj tmp, func;
435+
Obj tmp, func, oldLVars;
435436
int i;
436437

437438
// initialize everything and begin a fresh execution context
438439
STATE(NrError) = 0;
439440
STATE(ThrownObject) = 0;
441+
oldLVars = STATE(CurrLVars);
442+
SWITCH_TO_OLD_LVARS(STATE(BottomLVars));
440443

441-
ExecBegin(STATE(BottomLVars));
442444
tmp = KEPTALIVE(funcargs);
443445
StopKeepAlive(funcargs);
444446
func = ELM_PLIST(tmp, 1);
@@ -460,13 +462,12 @@ static void ThreadedInterpreter(void * funcargs)
460462
exit = GVarOptFunction(&GVarTHREAD_EXIT);
461463
if (exit)
462464
CALL_0ARGS(exit);
463-
ExecEnd(0);
464465
}
465466
CATCH_ERROR
466467
{
467-
ExecEnd(1);
468468
ClearError();
469469
}
470+
SWITCH_TO_OLD_LVARS(oldLVars);
470471
}
471472

472473

src/intrprtr.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -243,20 +243,23 @@ void IntrBegin(IntrState * intr, Obj frame)
243243
/* no return-statement was yet interpreted */
244244
intr->IntrReturning = 0;
245245

246-
/* start an execution environment */
247-
ExecBegin(frame);
246+
// remember the old execution state
247+
intr->oldLVars = STATE(CurrLVars);
248+
249+
// start an execution environment
250+
SWITCH_TO_OLD_LVARS( frame );
248251
}
249252

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

257+
// restore the execution environment
258+
SWITCH_TO_OLD_LVARS(intr->oldLVars);
259+
254260
/* if everything went fine */
255261
if ( ! error ) {
256262

257-
/* leave the execution environment */
258-
ExecEnd(0);
259-
260263
/* remember whether the interpreter interpreted a return-statement */
261264
intrReturning = intr->IntrReturning;
262265

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

277-
/* leave the execution environment */
278-
ExecEnd(1);
279-
280280
/* clean up the coder too */
281281
if ( intr->IntrCoding > 0 ) { CodeEnd(1); }
282282

src/intrprtr.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ ExecStatus IntrReturning;
6868
*/
6969
Obj StackObj;
7070

71+
/****************************************************************************
72+
**
73+
*F oldLVars
74+
*/
75+
Bag oldLVars;
7176

7277
};
7378

src/modules.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "stringobj.h"
3636
#include "sysfiles.h"
3737
#include "sysopt.h"
38+
#include "vars.h"
3839

3940
#ifdef HAVE_DLOPEN
4041
#include <dlfcn.h>
@@ -151,9 +152,10 @@ Int ActivateModule(StructInitInfo * info)
151152
if (info->initLibrary) {
152153
// Start a new executor to run the outer function of the module in
153154
// global context
154-
ExecBegin(STATE(BottomLVars));
155+
Bag oldLvars = STATE(CurrLVars);
156+
SWITCH_TO_OLD_LVARS(STATE(BottomLVars));
155157
res = res || info->initLibrary(info);
156-
ExecEnd(res);
158+
SWITCH_TO_OLD_LVARS(oldLvars);
157159
}
158160
}
159161

src/read.c

Lines changed: 36 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2686,37 +2686,6 @@ void ReadEvalError(void)
26862686
}
26872687

26882688

2689-
/****************************************************************************
2690-
**
2691-
** Reader state -- the next group of functions are used to "push" the
2692-
** current interpreter state allowing GAP code to be interpreted in the
2693-
** middle of other code. This is used, for instance, in the command-line
2694-
** editor.
2695-
*/
2696-
struct SavedReaderState {
2697-
UInt userHasQuit;
2698-
syJmp_buf readJmpError;
2699-
UInt nrError;
2700-
};
2701-
2702-
static void SaveReaderState(struct SavedReaderState *s) {
2703-
s->userHasQuit = STATE(UserHasQuit);
2704-
s->nrError = STATE(NrError);
2705-
memcpy(s->readJmpError, STATE(ReadJmpError), sizeof(syJmp_buf));
2706-
}
2707-
2708-
static void ClearReaderState(void ) {
2709-
STATE(UserHasQuit) = 0;
2710-
STATE(NrError) = 0;
2711-
}
2712-
2713-
static void RestoreReaderState(const struct SavedReaderState *s) {
2714-
memcpy(STATE(ReadJmpError), s->readJmpError, sizeof(syJmp_buf));
2715-
STATE(UserHasQuit) = s->userHasQuit;
2716-
STATE(NrError) = s->nrError;
2717-
}
2718-
2719-
27202689
/****************************************************************************
27212690
**
27222691
*F Call0ArgsInNewReader(Obj f) . . . . . . . . . . . . call a GAP function
@@ -2725,30 +2694,32 @@ static void RestoreReaderState(const struct SavedReaderState *s) {
27252694
*/
27262695
Obj Call0ArgsInNewReader(Obj f)
27272696
{
2728-
struct SavedReaderState s;
2729-
Obj result;
2697+
syJmp_buf readJmpError;
2698+
volatile Obj result = 0;
27302699

2731-
// remember the old reader context
2732-
SaveReaderState(&s);
2700+
// remember the old state
2701+
UInt userHasQuit = STATE(UserHasQuit);
2702+
UInt nrError = STATE(NrError);
2703+
Bag oldLvars = STATE(CurrLVars);
2704+
memcpy(readJmpError, STATE(ReadJmpError), sizeof(syJmp_buf));
27332705

2734-
// initialize everything and begin a fresh execution context
2735-
ClearReaderState();
2736-
ExecBegin(STATE(BottomLVars));
2706+
// initialize everything
2707+
STATE(UserHasQuit) = 0;
2708+
STATE(NrError) = 0;
2709+
SWITCH_TO_OLD_LVARS(STATE(BottomLVars));
27372710

27382711
TRY_IF_NO_ERROR
27392712
{
27402713
result = CALL_0ARGS(f);
2741-
ExecEnd(0);
2742-
}
2743-
CATCH_ERROR
2744-
{
2745-
result = 0;
2746-
ExecEnd(1);
2747-
ClearError();
27482714
}
27492715

2750-
// switch back to the old reader context
2751-
RestoreReaderState(&s);
2716+
// switch back to the old state
2717+
SWITCH_TO_OLD_LVARS(oldLvars);
2718+
ClearError();
2719+
memcpy(STATE(ReadJmpError), readJmpError, sizeof(syJmp_buf));
2720+
STATE(UserHasQuit) = userHasQuit;
2721+
STATE(NrError) = nrError;
2722+
27522723
return result;
27532724
}
27542725

@@ -2761,30 +2732,32 @@ Obj Call0ArgsInNewReader(Obj f)
27612732
*/
27622733
Obj Call1ArgsInNewReader(Obj f, Obj a)
27632734
{
2764-
struct SavedReaderState s;
2765-
Obj result;
2735+
syJmp_buf readJmpError;
2736+
volatile Obj result = 0;
27662737

2767-
// remember the old reader context
2768-
SaveReaderState(&s);
2738+
// remember the old state
2739+
UInt userHasQuit = STATE(UserHasQuit);
2740+
UInt nrError = STATE(NrError);
2741+
Bag oldLvars = STATE(CurrLVars);
2742+
memcpy(readJmpError, STATE(ReadJmpError), sizeof(syJmp_buf));
27692743

2770-
// initialize everything and begin a fresh execution context
2771-
ClearReaderState();
2772-
ExecBegin(STATE(BottomLVars));
2744+
// initialize everything
2745+
STATE(UserHasQuit) = 0;
2746+
STATE(NrError) = 0;
2747+
SWITCH_TO_OLD_LVARS(STATE(BottomLVars));
27732748

27742749
TRY_IF_NO_ERROR
27752750
{
27762751
result = CALL_1ARGS(f, a);
2777-
ExecEnd(0);
2778-
}
2779-
CATCH_ERROR
2780-
{
2781-
result = 0;
2782-
ExecEnd(1);
2783-
ClearError();
27842752
}
27852753

2786-
// switch back to the old reader context
2787-
RestoreReaderState(&s);
2754+
// switch back to the old state
2755+
SWITCH_TO_OLD_LVARS(oldLvars);
2756+
ClearError();
2757+
memcpy(STATE(ReadJmpError), readJmpError, sizeof(syJmp_buf));
2758+
STATE(UserHasQuit) = userHasQuit;
2759+
STATE(NrError) = nrError;
2760+
27882761
return result;
27892762
}
27902763

0 commit comments

Comments
 (0)