drop abstract-access-to-FrameP.patch
[nedit-bw.git] / global-RestartData.patch
blob904f5e0bdba7063b5fd65b0c7845f86506fe1006
1 ---
3 source/interpret.c | 133 +++++++++++++++++++++--------------------------------
4 source/interpret.h | 3 -
5 2 files changed, 57 insertions(+), 79 deletions(-)
7 diff --quilt old/source/interpret.c new/source/interpret.c
8 --- old/source/interpret.c
9 +++ new/source/interpret.c
10 @@ -81,8 +81,7 @@ static const char CVSID[] = "$Id: interp
11 enum opStatusCodes {STAT_OK=2, STAT_DONE, STAT_ERROR, STAT_PREEMPT};
13 static int addLoopAddr(Inst *addr, char **msg);
14 -static void saveContext(RestartData *context);
15 -static void restoreContext(RestartData *context);
16 +static RestartData *setContext(RestartData *context);
18 #define OP(name, fn) static int fn(void);
19 #include "ops.h"
20 @@ -175,16 +174,14 @@ static Inst **LoopStackPtr = LoopStack;
21 static const char *ProgramName = "";
23 /* Global data for the interpreter */
24 -static DataValue *TheStack; /* the stack */
25 -static DataValue *StackP; /* next free spot on stack */
26 -static DataValue *FrameP; /* frame pointer (start of local variables
27 - for the current subroutine invocation) */
28 -static Inst *PC; /* program counter during execution */
29 -static char *ErrMsg; /* global for returning error messages
30 - from executing functions */
31 -static WindowInfo
32 - *InitiatingWindow = NULL; /* window from which macro was run */
33 -static WindowInfo *FocusWindow; /* window on which macro commands operate */
34 +static RestartData *Interpreter;
35 +#define StackP (Interpreter->stackP)
36 +#define TheStack (Interpreter->stack)
37 +#define FrameP (Interpreter->frameP)
38 +#define PC (Interpreter->pc)
39 +#define InitiatingWindow (Interpreter->runWindow)
40 +#define FocusWindow (Interpreter->focusWindow)
41 +#define ErrMsg (Interpreter->errMsg)
42 static int PreemptRequest; /* passes preemption requests from called
43 routines back up to the interpreter */
45 @@ -532,7 +529,7 @@ static int setupFrame(RestartData *conte
48 /* !OK_TO_PUSH(totalPushs) */
49 - if (!((context->stackP + totalPushs) <= &context->stack[STACK_SIZE])) {
50 + if (!((context->stackP + totalPushs) <= (context->stack + STACK_SIZE))) {
51 return execError(StackOverflowMsg, "");
54 @@ -596,30 +593,30 @@ static int setupFrame(RestartData *conte
55 return STAT_OK;
58 -static Inst *rewindFrame(DataValue **frameP, DataValue **stackP)
59 +static void rewindFrame(RestartData *context)
61 /* get stored return information */
62 - int nArgs = FP_GET_ARG_COUNT(*frameP);
63 - DataValue *newFrameP = FP_GET_OLD_FP(*frameP);
64 - Inst *newPC = FP_GET_RET_PC(*frameP);
65 - Program *prog = FP_GET_PROG(*frameP);
66 + int nArgs = FP_GET_ARG_COUNT(context->frameP);
67 + DataValue *newFrameP = FP_GET_OLD_FP(context->frameP);
68 + Inst *newPC = FP_GET_RET_PC(context->frameP);
69 + Program *prog = FP_GET_PROG(context->frameP);
71 /* pop past local variables */
72 - *stackP = *frameP;
73 + context->stackP = context->frameP;
74 /* pop past arguments */
75 - *stackP -= (FP_TO_ARGS_DIST + nArgs);
76 + context->stackP -= (FP_TO_ARGS_DIST + nArgs);
78 - *frameP = newFrameP;
79 + context->frameP = newFrameP;
81 FreeProgram(prog);
83 - return newPC;
84 + context->pc = newPC;
87 static void rewindStack(RestartData *context)
89 while (context->pc) {
90 - context->pc = rewindFrame(&context->frameP, &context->stackP);
91 + rewindFrame(context);
95 @@ -648,7 +645,7 @@ int ExecuteMacro(WindowInfo *window, Pro
96 and a program counter) which will retain the program state across
97 preemption and resumption of execution */
98 context = (RestartData *)XtMalloc(sizeof(RestartData));
99 - context->stack = (DataValue *)XtMalloc(sizeof(DataValue) * STACK_SIZE);
100 + memset(context, 0, sizeof(*context));
101 *continuation = context;
102 context->stackP = context->stack;
103 context->runWindow = window;
104 @@ -679,18 +676,17 @@ int ContinueMacro(RestartData *continuat
106 register int status, instCount = 0;
107 register Inst *inst;
108 - RestartData oldContext;
110 - /* To allow macros to be invoked arbitrarily (such as those automatically
111 - triggered within smart-indent) within executing macros, this call is
112 - reentrant. */
113 - saveContext(&oldContext);
114 + RestartData *oldContext;
117 + ** To allow macros to be invoked arbitrarily (such as those automatically
118 + ** triggered within smart-indent) within executing macros, this call is
119 + ** reentrant.
120 + **
121 ** Execution Loop: Call the succesive routine addresses in the program
122 ** until one returns something other than STAT_OK, then take action
124 - restoreContext(continuation);
125 + oldContext = setContext(continuation);
126 ErrMsg = NULL;
127 for (;;) {
129 @@ -715,21 +711,18 @@ int ContinueMacro(RestartData *continuat
130 /* If error return was not STAT_OK, return to caller */
131 if (status != STAT_OK) {
132 if (status == STAT_PREEMPT) {
133 - saveContext(continuation);
134 - restoreContext(&oldContext);
135 + setContext(oldContext);
136 return MACRO_PREEMPT;
137 } else if (status == STAT_ERROR) {
138 *msg = ErrMsg;
139 - saveContext(continuation);
140 + setContext(oldContext);
141 FreeRestartData(continuation);
142 - restoreContext(&oldContext);
143 return MACRO_ERROR;
144 } else if (status == STAT_DONE) {
145 *msg = "";
146 *result = *--StackP;
147 - saveContext(continuation);
148 + setContext(oldContext);
149 FreeRestartData(continuation);
150 - restoreContext(&oldContext);
151 return MACRO_DONE;
154 @@ -739,8 +732,7 @@ int ContinueMacro(RestartData *continuat
155 X, other macros, and other shell scripts a chance to execute */
156 instCount++;
157 if (instCount >= INSTRUCTION_LIMIT) {
158 - saveContext(continuation);
159 - restoreContext(&oldContext);
160 + setContext(oldContext);
161 return MACRO_TIME_LIMIT;
164 @@ -776,7 +768,6 @@ int RunMacroAsSubrCall(RestartData *cont
165 void FreeRestartData(RestartData *context)
167 rewindStack(context);
168 - XtFree((char *)context->stack);
169 XtFree((char *)context);
172 @@ -797,8 +788,8 @@ void PreemptMacro(void)
174 void ModifyReturnedValue(RestartData *context, DataValue dv)
176 - if ((context->pc-1)->val.op == OP_FETCH_RET_VAL)
177 - *(context->stackP-1) = dv;
178 + if ((context->pc - 1)->val.op == OP_FETCH_RET_VAL)
179 + *(context->stackP - 1) = dv;
183 @@ -807,7 +798,7 @@ void ModifyReturnedValue(RestartData *co
185 WindowInfo *MacroRunWindow(void)
187 - return InitiatingWindow;
188 + return Interpreter ? InitiatingWindow : NULL;
192 @@ -817,7 +808,7 @@ WindowInfo *MacroRunWindow(void)
194 WindowInfo *MacroFocusWindow(void)
196 - return FocusWindow;
197 + return Interpreter ? FocusWindow : NULL;
201 @@ -826,7 +817,12 @@ WindowInfo *MacroFocusWindow(void)
203 void SetMacroFocusWindow(WindowInfo *window)
205 - FocusWindow = window;
206 + if (Interpreter) {
207 + FocusWindow = window;
209 + else {
210 + fprintf(stderr, "NEdit: setting FocusWindow while not running a macro\n");
215 @@ -1319,24 +1315,11 @@ void GarbageCollectStrings(void)
217 ** Save and restore execution context to data structure "context"
219 -static void saveContext(RestartData *context)
220 +static RestartData *setContext(RestartData *new)
222 - context->stack = TheStack;
223 - context->stackP = StackP;
224 - context->frameP = FrameP;
225 - context->pc = PC;
226 - context->runWindow = InitiatingWindow;
227 - context->focusWindow = FocusWindow;
230 -static void restoreContext(RestartData *context)
232 - TheStack = context->stack;
233 - StackP = context->stackP;
234 - FrameP = context->frameP;
235 - PC = context->pc;
236 - InitiatingWindow = context->runWindow;
237 - FocusWindow = context->focusWindow;
238 + RestartData *old = Interpreter;
239 + Interpreter = new;
240 + return old;
243 static void freeSymbolList(Symbol *symList)
244 @@ -1436,7 +1419,7 @@ static void addToGlobalSymTab(Symbol *sy
246 /* true, if you can push n values */
247 #define OK_TO_PUSH(n) \
248 - (StackP + (n) <= &TheStack[STACK_SIZE])
249 + (StackP + (n) <= (TheStack + STACK_SIZE))
251 #define PUSH_CHECK(n) \
252 do { \
253 @@ -1605,9 +1588,8 @@ static int pushSymVal(void)
254 symVal = FP_GET_ARG_N(FrameP, argNum);
256 } else if (s->type == PROC_VALUE_SYM) {
257 - char *errMsg;
258 - if (!(s->value.val.subr)(FocusWindow, NULL, 0,
259 - &symVal, &errMsg)) {
260 + char *errMsg;
261 + if (!s->value.val.subr(FocusWindow, NULL, 0, &symVal, &errMsg)) {
262 EXEC_ERROR(errMsg, s->name);
264 } else if (s->type == C_FUNCTION_SYM
265 @@ -2377,8 +2359,8 @@ static int eq(void)
266 /* negated eq() call */
267 static int ne(void)
269 - eq();
270 - return not();
271 + int status = eq();
272 + return (status == STAT_OK) ? not() : status;
276 @@ -2708,7 +2690,6 @@ static int callSubroutineFromSymbol(Symb
277 static DataValue noValue = {NO_TAG, {0}};
278 DataValue argArray = noValue;
279 Program *prog;
280 - char *errMsg;
281 int haveNamedArgs = (nArgs < 0);
282 DataValue *symValPtr = NULL;
284 @@ -2736,6 +2717,7 @@ static int callSubroutineFromSymbol(Symb
286 if (sym->type == C_FUNCTION_SYM) {
287 DataValue result;
288 + char *errMsg;
290 PUSH(argArray); /* push dummy named arg array */
292 @@ -2744,9 +2726,9 @@ static int callSubroutineFromSymbol(Symb
294 /* Call the function and check for preemption */
295 PreemptRequest = False;
296 - if (!sym->value.val.subr(FocusWindow, StackP,
297 - nArgs, &result, &errMsg))
298 + if (!sym->value.val.subr(FocusWindow, StackP, nArgs, &result, &errMsg)) {
299 EXEC_ERROR(errMsg, sym->name);
301 PUSH_RET_VAL(result);
302 return PreemptRequest ? STAT_PREEMPT : STAT_OK;
304 @@ -2759,15 +2741,10 @@ static int callSubroutineFromSymbol(Symb
305 ** values which are already there.
307 if (sym->type == MACRO_FUNCTION_SYM) {
308 - RestartData context;
309 - int status;
310 prog = sym->value.val.prog;
311 prog->refcount++;
312 /* -nArgs means 'arguments are on stack' */
313 - saveContext(&context);
314 - status = setupFrame(&context, prog, -nArgs, NULL, argArray, sym->name);
315 - restoreContext(&context);
316 - return status;
317 + return setupFrame(Interpreter, prog, -nArgs, NULL, argArray, sym->name);
321 @@ -3043,7 +3020,7 @@ static int returnValOrNone(int valOnStac
322 retVal = noValue;
325 - PC = rewindFrame(&FrameP, &StackP);
326 + rewindFrame(Interpreter);
328 /* push returned value, if requsted */
329 PUSH_RET_VAL(retVal);
330 diff --quilt old/source/interpret.h new/source/interpret.h
331 --- old/source/interpret.h
332 +++ new/source/interpret.h
333 @@ -121,12 +121,13 @@ typedef struct ProgramTag {
335 /* Information needed to re-start a preempted macro */
336 typedef struct {
337 - DataValue *stack;
338 + DataValue stack[STACK_SIZE];
339 DataValue *stackP;
340 DataValue *frameP;
341 Inst *pc;
342 WindowInfo *runWindow;
343 WindowInfo *focusWindow;
344 + char *errMsg;
345 } RestartData;
347 /* state of the accumulator (aka compiler) */