drops and reorders
[nedit-bw.git] / global-RestartData.patch
blob21ab1d66647e2ba9e76790299adade3ff90da16b
1 ---
3 source/interpret.c | 134 +++++++++++++++++++++--------------------------------
4 source/interpret.h | 3 -
5 2 files changed, 57 insertions(+), 80 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 @@ -533,7 +530,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 @@ -597,30 +594,30 @@ static int setupFrame(RestartData *conte
55 context->pc = prog->code;
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,8 +645,7 @@ int ExecuteMacro(WindowInfo *window, Pro
96 /* Create an execution context (a stack, a stack pointer, a frame pointer,
97 and a program counter) which will retain the program state across
98 preemption and resumption of execution */
99 - context = (RestartData *)XtMalloc(sizeof(RestartData));
100 - context->stack = (DataValue *)XtMalloc(sizeof(DataValue) * STACK_SIZE);
101 + context = XtNew(RestartData);
102 *continuation = context;
103 context->stackP = context->stack;
104 context->runWindow = window;
105 @@ -680,18 +676,17 @@ int ContinueMacro(RestartData *continuat
107 register int status, instCount = 0;
108 register Inst *inst;
109 - RestartData oldContext;
111 - /* To allow macros to be invoked arbitrarily (such as those automatically
112 - triggered within smart-indent) within executing macros, this call is
113 - reentrant. */
114 - saveContext(&oldContext);
115 + RestartData *oldContext;
118 + ** To allow macros to be invoked arbitrarily (such as those automatically
119 + ** triggered within smart-indent) within executing macros, this call is
120 + ** reentrant.
121 + **
122 ** Execution Loop: Call the succesive routine addresses in the program
123 ** until one returns something other than STAT_OK, then take action
125 - restoreContext(continuation);
126 + oldContext = setContext(continuation);
127 ErrMsg = NULL;
128 for (;;) {
130 @@ -716,21 +711,18 @@ int ContinueMacro(RestartData *continuat
131 /* If error return was not STAT_OK, return to caller */
132 if (status != STAT_OK) {
133 if (status == STAT_PREEMPT) {
134 - saveContext(continuation);
135 - restoreContext(&oldContext);
136 + setContext(oldContext);
137 return MACRO_PREEMPT;
138 } else if (status == STAT_ERROR) {
139 *msg = ErrMsg;
140 - saveContext(continuation);
141 + setContext(oldContext);
142 FreeRestartData(continuation);
143 - restoreContext(&oldContext);
144 return MACRO_ERROR;
145 } else if (status == STAT_DONE) {
146 *msg = "";
147 *result = *--StackP;
148 - saveContext(continuation);
149 + setContext(oldContext);
150 FreeRestartData(continuation);
151 - restoreContext(&oldContext);
152 return MACRO_DONE;
155 @@ -740,8 +732,7 @@ int ContinueMacro(RestartData *continuat
156 X, other macros, and other shell scripts a chance to execute */
157 instCount++;
158 if (instCount >= INSTRUCTION_LIMIT) {
159 - saveContext(continuation);
160 - restoreContext(&oldContext);
161 + setContext(oldContext);
162 return MACRO_TIME_LIMIT;
165 @@ -777,7 +768,6 @@ int RunMacroAsSubrCall(RestartData *cont
166 void FreeRestartData(RestartData *context)
168 rewindStack(context);
169 - XtFree((char *)context->stack);
170 XtFree((char *)context);
173 @@ -798,8 +788,8 @@ void PreemptMacro(void)
175 void ModifyReturnedValue(RestartData *context, DataValue dv)
177 - if ((context->pc-1)->val.op == OP_FETCH_RET_VAL)
178 - *(context->stackP-1) = dv;
179 + if ((context->pc - 1)->val.op == OP_FETCH_RET_VAL)
180 + *(context->stackP - 1) = dv;
184 @@ -808,7 +798,7 @@ void ModifyReturnedValue(RestartData *co
186 WindowInfo *MacroRunWindow(void)
188 - return InitiatingWindow;
189 + return Interpreter ? InitiatingWindow : NULL;
193 @@ -818,7 +808,7 @@ WindowInfo *MacroRunWindow(void)
195 WindowInfo *MacroFocusWindow(void)
197 - return FocusWindow;
198 + return Interpreter ? FocusWindow : NULL;
202 @@ -827,7 +817,12 @@ WindowInfo *MacroFocusWindow(void)
204 void SetMacroFocusWindow(WindowInfo *window)
206 - FocusWindow = window;
207 + if (Interpreter) {
208 + FocusWindow = window;
210 + else {
211 + fprintf(stderr, "NEdit: setting FocusWindow while not running a macro\n");
216 @@ -1317,24 +1312,11 @@ void GarbageCollectStrings(void)
218 ** Save and restore execution context to data structure "context"
220 -static void saveContext(RestartData *context)
221 +static RestartData *setContext(RestartData *new)
223 - context->stack = TheStack;
224 - context->stackP = StackP;
225 - context->frameP = FrameP;
226 - context->pc = PC;
227 - context->runWindow = InitiatingWindow;
228 - context->focusWindow = FocusWindow;
231 -static void restoreContext(RestartData *context)
233 - TheStack = context->stack;
234 - StackP = context->stackP;
235 - FrameP = context->frameP;
236 - PC = context->pc;
237 - InitiatingWindow = context->runWindow;
238 - FocusWindow = context->focusWindow;
239 + RestartData *old = Interpreter;
240 + Interpreter = new;
241 + return old;
244 static void freeSymbolList(Symbol *symList)
245 @@ -1435,7 +1417,7 @@ static void addToGlobalSymTab(Symbol *sy
247 /* true, if you can push n values */
248 #define OK_TO_PUSH(n) \
249 - (StackP + (n) <= &TheStack[STACK_SIZE])
250 + (StackP + (n) <= (TheStack + STACK_SIZE))
252 #define PUSH_CHECK(n) \
253 do { \
254 @@ -1604,9 +1586,8 @@ static int pushSymVal(void)
255 symVal = FP_GET_ARG_N(FrameP, argNum);
257 } else if (s->type == PROC_VALUE_SYM) {
258 - char *errMsg;
259 - if (!(s->value.val.subr)(FocusWindow, NULL, 0,
260 - &symVal, &errMsg)) {
261 + char *errMsg;
262 + if (!s->value.val.subr(FocusWindow, NULL, 0, &symVal, &errMsg)) {
263 EXEC_ERROR(errMsg, s->name);
265 } else
266 @@ -2371,8 +2352,8 @@ static int eq(void)
267 /* negated eq() call */
268 static int ne(void)
270 - eq();
271 - return not();
272 + int status = eq();
273 + return (status == STAT_OK) ? not() : status;
277 @@ -2702,7 +2683,6 @@ static int callSubroutineFromSymbol(Symb
278 static DataValue noValue = {NO_TAG, {0}};
279 DataValue argArray = noValue;
280 Program *prog;
281 - char *errMsg;
282 int haveNamedArgs = (nArgs < 0);
284 if (haveNamedArgs) {
285 @@ -2715,6 +2695,7 @@ static int callSubroutineFromSymbol(Symb
287 if (sym->type == C_FUNCTION_SYM) {
288 DataValue result;
289 + char *errMsg;
291 PUSH(argArray); /* push dummy named arg array */
293 @@ -2723,9 +2704,9 @@ static int callSubroutineFromSymbol(Symb
295 /* Call the function and check for preemption */
296 PreemptRequest = False;
297 - if (!sym->value.val.subr(FocusWindow, StackP,
298 - nArgs, &result, &errMsg))
299 + if (!sym->value.val.subr(FocusWindow, StackP, nArgs, &result, &errMsg)) {
300 EXEC_ERROR(errMsg, sym->name);
302 PUSH_RET_VAL(result);
303 return PreemptRequest ? STAT_PREEMPT : STAT_OK;
305 @@ -2738,15 +2719,10 @@ static int callSubroutineFromSymbol(Symb
306 ** values which are already there.
308 if (sym->type == MACRO_FUNCTION_SYM) {
309 - RestartData context;
310 - int status;
311 prog = sym->value.val.prog;
312 prog->refcount++;
313 /* -nArgs means 'arguments are on stack' */
314 - saveContext(&context);
315 - status = setupFrame(&context, prog, -nArgs, NULL, argArray, sym->name);
316 - restoreContext(&context);
317 - return status;
318 + return setupFrame(Interpreter, prog, -nArgs, NULL, argArray, sym->name);
322 @@ -3010,7 +2986,7 @@ static int returnValOrNone(int valOnStac
323 retVal = noValue;
326 - PC = rewindFrame(&FrameP, &StackP);
327 + rewindFrame(Interpreter);
329 /* push returned value, if requsted */
330 PUSH_RET_VAL(retVal);
331 diff --quilt old/source/interpret.h new/source/interpret.h
332 --- old/source/interpret.h
333 +++ new/source/interpret.h
334 @@ -120,12 +120,13 @@ typedef struct ProgramTag {
336 /* Information needed to re-start a preempted macro */
337 typedef struct {
338 - DataValue *stack;
339 + DataValue stack[STACK_SIZE];
340 DataValue *stackP;
341 DataValue *frameP;
342 Inst *pc;
343 WindowInfo *runWindow;
344 WindowInfo *focusWindow;
345 + char *errMsg;
346 } RestartData;
348 /* state of the accumulator (aka compiler) */