convert 'pass context' to 'global context'
[nedit-bw.git] / typed-Inst.patch
blobe99bdb0da0b3a7c0768b0d2349074f1054ceab2c
1 ---
3 source/interpret.c | 214 +++++++++++++++++++++++++++++++++++------------------
4 source/interpret.h | 16 ++-
5 2 files changed, 156 insertions(+), 74 deletions(-)
7 diff --quilt old/source/interpret.c new/source/interpret.c
8 --- old/source/interpret.c
9 +++ new/source/interpret.c
10 @@ -113,6 +113,7 @@ static int inTypeOfMode;
12 static const char *tagToStr(enum typeTags tag);
13 static const char *typeToStr(enum symTypes type);
14 +static const char *instTypeToStr(enum instTypes type);
16 /*#define DEBUG_ASSEMBLY*/
17 /*#define DEBUG_STACK*/
18 @@ -336,7 +337,8 @@ int AddOp(int op, char **msg)
19 *msg = "macro too large";
20 return 0;
22 - ProgP->op = op;
23 + ProgP->type = OP_INST;
24 + ProgP->val.op = op;
25 ProgP++;
26 return 1;
28 @@ -351,7 +353,8 @@ int AddSym(Symbol *sym, char **msg)
29 return 0;
31 sym->added = 1;
32 - ProgP->sym = sym;
33 + ProgP->type = SYM_INST;
34 + ProgP->val.sym = sym;
35 ProgP++;
36 return 1;
38 @@ -359,13 +362,14 @@ int AddSym(Symbol *sym, char **msg)
40 ** Add an immediate value operand to the current program
42 -int AddImmediate(int value, char **msg)
43 +int AddImmediate(int immed, char **msg)
45 if (ProgP >= &Prog[PROGRAM_SIZE]) {
46 *msg = "macro too large";
47 return 0;
49 - ProgP->value = value;
50 + ProgP->type = IMMED_INST;
51 + ProgP->val.immed = immed;
52 ProgP++;
53 return 1;
55 @@ -379,8 +383,8 @@ int AddBranchOffset(Inst *to, char **msg
56 *msg = "macro too large";
57 return 0;
59 - /* Should be ptrdiff_t for branch offsets */
60 - ProgP->value = to - ProgP;
61 + ProgP->type = BRANCH_INST;
62 + ProgP->val.branch = to - ProgP;
63 ProgP++;
65 return 1;
66 @@ -391,7 +395,11 @@ int AddBranchOffset(Inst *to, char **msg
68 int SetBranchOffset(Inst *from, Inst *to, char **msg)
70 - from->value = to - from;
71 + if (from->type != BRANCH_INST) {
72 + *msg = "not a branch instruction";
73 + return 0;
74 + }
75 + from->val.branch = to - from;
77 return 1;
79 @@ -444,10 +452,14 @@ int AddBreakAddr(Inst *addr, char **msg)
80 *msg = "break outside loop";
81 return 0;
83 + if (addr->type != BRANCH_INST) {
84 + *msg = "not a branch instruction for break";
85 + return 0;
86 + }
87 if (!addLoopAddr(addr, msg)) {
88 return 0;
90 - addr->value = NEEDS_BREAK;
91 + addr->val.branch = NEEDS_BREAK;
92 return 1;
95 @@ -457,10 +469,14 @@ int AddContinueAddr(Inst *addr, char **m
96 *msg = "continue outside loop";
97 return 0;
99 + if (addr->type != BRANCH_INST) {
100 + *msg = "not a branch instruction for break";
101 + return 0;
103 if (!addLoopAddr(addr, msg)) {
104 return 0;
106 - addr->value = NEEDS_CONTINUE;
107 + addr->val.branch = NEEDS_CONTINUE;
108 return 1;
111 @@ -484,10 +500,10 @@ void FillLoopAddrs(Inst *breakAddr, Inst
113 if (*LoopStackPtr == NULL)
114 break;
115 - if ((*LoopStackPtr)->value == NEEDS_BREAK)
116 - (*LoopStackPtr)->value = breakAddr - *LoopStackPtr;
117 - else if ((*LoopStackPtr)->value == NEEDS_CONTINUE)
118 - (*LoopStackPtr)->value = continueAddr - *LoopStackPtr;
119 + if ((*LoopStackPtr)->val.immed == NEEDS_BREAK)
120 + (*LoopStackPtr)->val.branch = breakAddr - *LoopStackPtr;
121 + else if ((*LoopStackPtr)->val.immed == NEEDS_CONTINUE)
122 + (*LoopStackPtr)->val.branch = continueAddr - *LoopStackPtr;
123 else
124 fprintf(stderr, "NEdit: internal error (uat) in macro parser\n");
126 @@ -659,14 +675,20 @@ int ContinueMacro(RestartData *continuat
128 /* Execute an instruction */
129 inst = PC++;
130 - switch (inst->op) {
131 + if (inst->type != OP_INST) {
132 + status = execError("Unexpected instruction of type %s",
133 + instTypeToStr(inst->type));
135 + else {
136 + switch (inst->val.op) {
137 #define OP(name, fn) case OP_##name:
138 #include "ops.h"
139 #undef OP
140 - status = (OpFns[inst->op])();
141 - break;
142 - default:
143 - status = execError("Illegal instruction at %8p", (char *)inst);
144 + status = (OpFns[inst->val.op])();
145 + break;
146 + default:
147 + status = execError("Illegal instruction at %8p", (char *)inst);
151 /* If error return was not STAT_OK, return to caller */
152 @@ -742,7 +764,7 @@ void PreemptMacro(void)
154 void ModifyReturnedValue(RestartData *context, DataValue dv)
156 - if ((context->pc-1)->op == OP_FETCH_RET_VAL)
157 + if ((context->pc-1)->val.op == OP_FETCH_RET_VAL)
158 *(context->stackP-1) = dv;
161 @@ -1322,19 +1344,31 @@ static void addToGlobalSymTab(Symbol *sy
163 #define GET_SYM(s) \
164 do { \
165 - s = PC->sym; \
166 + if (PC->type != SYM_INST) { \
167 + return execError("Unexpected instruction, expected <symbol>: %s", \
168 + instTypeToStr(PC->type)); \
169 + } \
170 + s = PC->val.sym; \
171 PC++; \
172 } while (0)
174 #define GET_IMMED(i) \
175 do { \
176 - i = PC->value; \
177 + if (PC->type != IMMED_INST) { \
178 + return execError("Unexpected instruction, expected <immediate>: %s", \
179 + instTypeToStr(PC->type)); \
180 + } \
181 + i = PC->val.immed; \
182 PC++; \
183 } while (0)
185 #define GET_BRANCH(a) \
186 do { \
187 - a = PC + PC->value; \
188 + if (PC->type != BRANCH_INST) { \
189 + return execError("Unexpected instruction, expected <branch>: %s", \
190 + instTypeToStr(PC->type)); \
191 + } \
192 + a = PC + PC->val.branch; \
193 PC++; \
194 } while (0)
196 @@ -1348,7 +1382,7 @@ static void addToGlobalSymTab(Symbol *sy
197 if (PC == NULL) { \
198 PUSH(dv); \
200 - else if (PC->op == OP_FETCH_RET_VAL) { \
201 + else if (PC->type == OP_INST && PC->val.op == OP_FETCH_RET_VAL) { \
202 PUSH(dv); \
203 PC++; \
205 @@ -4322,6 +4356,23 @@ static const char *typeToStr(enum symTyp
209 +static const char *instTypeToStr(enum instTypes type)
211 + switch (type) {
212 + case OP_INST:
213 + return "<operation>";
214 + case IMMED_INST:
215 + return "<immediate>";
216 + case BRANCH_INST:
217 + return "<branch>";
218 + case SYM_INST:
219 + return "<symbol>";
220 + default:
221 + return "";
226 #ifdef DEBUG_DISASSEMBLER /* dumping values in disassembly or stack dump */
227 static char *printdBuffer = NULL;
228 static int printdPos = 0;
229 @@ -4480,26 +4531,50 @@ static void disasmInternal(Inst *inst, i
230 for (i = 0; i < nInstr; ++i) {
231 printd("Prog %8p", &inst[i]);
233 - switch (inst[i].op) {
234 + if (inst[i].type != OP_INST) {
235 + switch (inst[i].type) {
236 + case IMMED_INST:
237 + printd(" %s: %d\n",
238 + instTypeToStr(inst[i].type),
239 + inst[i].val.immed);
240 + break;
241 + case BRANCH_INST:
242 + printd(" %s: (%+td) %8p\n",
243 + instTypeToStr(inst[i].type),
244 + inst[i].val.branch,
245 + &inst[i] + inst[i].val.branch);
246 + case SYM_INST:
247 + printd(" %s: %s\n",
248 + instTypeToStr(inst[i].type),
249 + inst[i].val.sym->name);
250 + break;
251 + default:
252 + printd(" <unknown>: %tx\n", inst[i].val.branch);
255 + continue;
258 + switch (inst[i].val.op) {
259 #define OP(name, fn) case OP_##name:
260 #include "ops.h"
261 #undef OP
262 - printd(" %*s", (int)opLen, opNames[inst[i].op]);
263 + printd(" %*s", (int)opLen, opNames[inst[i].val.op]);
265 - switch (inst[i].op) {
266 + switch (inst[i].val.op) {
267 case OP_PUSH_SYM:
268 case OP_ASSIGN:
269 - printd(" %s", inst[i+1].sym->name);
270 - if (inst[i+1].sym->type == CONST_SYM
271 - && inst[i+1].sym->value.tag == STRING_TAG) {
272 + printd(" %s", inst[i+1].val.sym->name);
273 + if (inst[i+1].val.sym->type == CONST_SYM
274 + && inst[i+1].val.sym->value.tag == STRING_TAG) {
275 printd(" ");
276 - dumpVal(inst[i+1].sym->value);
277 + dumpVal(inst[i+1].val.sym->value);
279 ++i;
280 break;
282 case OP_PUSH_IMMED:
283 - printd(" %d", inst[i+1].value);
284 + printd(" %d", inst[i+1].val.immed);
285 ++i;
286 break;
288 @@ -4507,19 +4582,20 @@ static void disasmInternal(Inst *inst, i
289 case OP_BRANCH_TRUE:
290 case OP_BRANCH_FALSE:
291 case OP_BRANCH_NEVER:
292 - printd(" to=(%+d) %8p",
293 - inst[i+1].value, &inst[i+1] + inst[i+1].value);
294 + printd(" to=(%+td) %8p",
295 + inst[i+1].val.branch,
296 + &inst[i+1] + inst[i+1].val.branch);
297 ++i;
298 break;
300 case OP_CONCAT:
301 - printd(" nExpr=%d", inst[i+1].value);
302 + printd(" nExpr=%d", inst[i+1].val.immed);
303 ++i;
304 break;
306 case OP_SUBR_CALL: {
307 - int args = inst[i+2].value;
308 - printd(" %s", inst[i+1].sym->name);
309 + int args = inst[i+2].val.immed;
310 + printd(" %s", inst[i+1].val.sym->name);
311 if (args < 0) {
312 printd(" %d+args[] (%d)", -args - 1, args);
314 @@ -4531,7 +4607,7 @@ static void disasmInternal(Inst *inst, i
315 break;
317 case OP_UNPACKTOARGS: {
318 - int args = inst[i+2].value;
319 + int args = inst[i+2].val.immed;
320 if (args < 0) {
321 printd(" %d+args[] (%d)", -args - 1, args);
323 @@ -4543,56 +4619,56 @@ static void disasmInternal(Inst *inst, i
324 break;
326 case OP_SUBR_CALL_STACKED_N:
327 - printd(" %s =args[] (?)", inst[i+1].sym->name);
328 + printd(" %s =args[] (?)", inst[i+1].val.sym->name);
329 ++i;
330 break;
332 case OP_BEGIN_ARRAY_ITER:
333 case OP_BEGIN_ARRAY_MULTI_ITER_ARRAY:
334 - printd(" %s in", inst[i+1].sym->name);
335 + printd(" %s in", inst[i+1].val.sym->name);
336 ++i;
337 break;
339 case OP_ARRAY_ITER:
340 - if (!inst[i+1].value) {
341 + if (!inst[i+1].val.immed) {
342 /* without val */
343 - printd(" %s = %s++ end-loop=(%+d) %8p",
344 - inst[i+2].sym->name,
345 - inst[i+3].sym->name,
346 - inst[i+4].value,
347 - &inst[i+4] + inst[i+4].value);
348 + printd(" %s = %s++ end-loop=(%+td) %8p",
349 + inst[i+2].val.sym->name,
350 + inst[i+3].val.sym->name,
351 + inst[i+4].val.branch,
352 + &inst[i+4] + inst[i+4].val.branch);
353 i += 4;
355 else {
356 /* with val */
357 - printd(" %s=%s = %s++ end-loop=(%+d) %8p",
358 - inst[i+2].sym->name,
359 - inst[i+3].sym->name,
360 - inst[i+4].sym->name,
361 - inst[i+5].value,
362 - &inst[i+5] + inst[i+5].value);
363 + printd(" %s=%s = %s++ end-loop=(%+td) %8p",
364 + inst[i+2].val.sym->name,
365 + inst[i+3].val.sym->name,
366 + inst[i+4].val.sym->name,
367 + inst[i+5].val.branch,
368 + &inst[i+5] + inst[i+5].val.branch);
369 i += 5;
371 break;
373 case OP_ARRAY_MULTI_ITER_ARRAY:
374 - if (!inst[i+1].value) {
375 + if (!inst[i+1].val.immed) {
376 /* without val */
377 - printd(" %s[] = %s++ end-loop=(%+d) %8p",
378 - inst[i+2].sym->name,
379 - inst[i+3].sym->name,
380 - inst[i+4].value,
381 - &inst[i+4] + inst[i+4].value);
382 + printd(" %s[] = %s++ end-loop=(%+td) %8p",
383 + inst[i+2].val.sym->name,
384 + inst[i+3].val.sym->name,
385 + inst[i+4].val.branch,
386 + &inst[i+4] + inst[i+4].val.branch);
387 i += 4;
389 else {
390 /* with val */
391 - printd(" %s[]=%s = %s++ end-loop=(%+d) %8p",
392 - inst[i+2].sym->name,
393 - inst[i+3].sym->name,
394 - inst[i+4].sym->name,
395 - inst[i+5].value,
396 - &inst[i+5] + inst[i+5].value);
397 + printd(" %s[]=%s = %s++ end-loop=(%+td) %8p",
398 + inst[i+2].val.sym->name,
399 + inst[i+3].val.sym->name,
400 + inst[i+4].val.sym->name,
401 + inst[i+5].val.branch,
402 + &inst[i+5] + inst[i+5].val.branch);
403 i += 5;
405 break;
406 @@ -4603,21 +4679,21 @@ static void disasmInternal(Inst *inst, i
407 case OP_ANONARRAY_INDEX_VAL:
408 case OP_NAMED_ARG1:
409 case OP_NAMED_ARGN:
410 - printd(" nDim=%d", inst[i+1].value);
411 + printd(" nDim=%d", inst[i+1].val.immed);
412 ++i;
413 break;
415 case OP_ARRAY_REF_ASSIGN_SETUP:
416 printd(" binOp=%s nDim=%d",
417 - inst[i+1].value ? "true" : "false",
418 - inst[i+2].value);
419 + inst[i+1].val.immed ? "true" : "false",
420 + inst[i+2].val.immed);
421 i += 2;
422 break;
424 case OP_PUSH_ARRAY_SYM:
425 printd(" %s %s",
426 - inst[i+1].sym->name,
427 - inst[i+2].value ? "createAndRef" : "refOnly");
428 + inst[i+1].val.sym->name,
429 + inst[i+2].val.immed ? "createAndRef" : "refOnly");
430 i += 2;
431 break;
433 @@ -4627,7 +4703,7 @@ static void disasmInternal(Inst *inst, i
434 break;
436 default:
437 - printd(" %x\n", inst[i].value);
438 + printd(" %lx\n", inst[i].val.immed);
439 break;
442 diff --quilt old/source/interpret.h new/source/interpret.h
443 --- old/source/interpret.h
444 +++ new/source/interpret.h
445 @@ -54,6 +54,8 @@ enum typeTags {NO_TAG, INT_TAG, STRING_T
447 enum execReturnCodes {MACRO_TIME_LIMIT, MACRO_PREEMPT, MACRO_DONE, MACRO_ERROR};
449 +enum instTypes {OP_INST, IMMED_INST, BRANCH_INST, SYM_INST};
451 #define ARRAY_DIM_SEP "\034"
453 struct DataValueTag;
454 @@ -61,10 +63,14 @@ struct SparseArrayEntryTag;
455 struct ProgramTag;
456 struct SymbolRec;
458 -typedef union InstTag {
459 - enum operations op;
460 - int value;
461 - struct SymbolRec *sym;
462 +typedef struct InstTag {
463 + enum instTypes type;
464 + union {
465 + enum operations op;
466 + int immed;
467 + ptrdiff_t branch;
468 + struct SymbolRec *sym;
469 + } val;
470 } Inst;
472 typedef int (*BuiltInSubr)(WindowInfo *window, struct DataValueTag *argList,
473 @@ -150,7 +156,7 @@ int ArrayCopy(DataValue *dstArray, DataV
474 void BeginCreatingProgram(const char *name, AccumulatorData *acc);
475 int AddOp(int op, char **msg);
476 int AddSym(Symbol *sym, char **msg);
477 -int AddImmediate(int value, char **msg);
478 +int AddImmediate(int immed, char **msg);
479 int AddBranchOffset(Inst *to, char **msg);
480 int SetBranchOffset(Inst *from, Inst *to, char **msg);
481 Inst *GetPC(void);