3 source/interpret.c | 218 ++++++++++++++++++++++++++++++++++++-----------------
4 source/interpret.h | 16 ++-
5 2 files changed, 160 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";
23 + ProgP->type = OP_INST;
28 @@ -351,7 +353,8 @@ int AddSym(Symbol *sym, char **msg)
33 + ProgP->type = SYM_INST;
34 + ProgP->val.sym = sym;
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";
49 - ProgP->value = value;
50 + ProgP->type = IMMED_INST;
51 + ProgP->val.immed = immed;
55 @@ -379,8 +383,8 @@ int AddBranchOffset(Inst *to, char **msg
56 *msg = "macro too large";
59 - /* Should be ptrdiff_t for branch offsets */
60 - ProgP->value = to - ProgP;
61 + ProgP->type = BRANCH_INST;
62 + ProgP->val.branch = to - ProgP;
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";
75 + from->val.branch = to - from;
79 @@ -444,10 +452,14 @@ int AddBreakAddr(Inst *addr, char **msg)
80 *msg = "break outside loop";
83 + if (addr->type != BRANCH_INST) {
84 + *msg = "not a branch instruction for break";
87 if (!addLoopAddr(addr, msg)) {
90 - addr->value = NEEDS_BREAK;
91 + addr->val.branch = NEEDS_BREAK;
95 @@ -457,10 +469,14 @@ int AddContinueAddr(Inst *addr, char **m
96 *msg = "continue outside loop";
99 + if (addr->type != BRANCH_INST) {
100 + *msg = "not a branch instruction for break";
103 if (!addLoopAddr(addr, msg)) {
106 - addr->value = NEEDS_CONTINUE;
107 + addr->val.branch = NEEDS_CONTINUE;
111 @@ -484,10 +500,10 @@ void FillLoopAddrs(Inst *breakAddr, Inst
113 if (*LoopStackPtr == NULL)
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;
124 fprintf(stderr, "NEdit: internal error (uat) in macro parser\n");
126 @@ -659,14 +675,20 @@ int ContinueMacro(RestartData *continuat
128 /* Execute an instruction */
130 - switch (inst->op) {
131 + if (inst->type != OP_INST) {
132 + status = execError("Unexpected instruction of type <%s>",
133 + instTypeToStr(inst->type));
136 + switch (inst->val.op) {
137 #define OP(name, fn) case OP_##name:
140 - status = (OpFns[inst->op])();
143 - status = execError("Illegal instruction at %8p", (char *)inst);
144 + status = (OpFns[inst->val.op])();
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
166 + if (PC->type != SYM_INST) { \
167 + return execError("Unexpected instruction, expected <symbol>: <%s>", \
168 + instTypeToStr(PC->type)); \
174 #define GET_IMMED(i) \
177 + if (PC->type != IMMED_INST) { \
178 + return execError("Unexpected instruction, expected <immediate>: <%s>", \
179 + instTypeToStr(PC->type)); \
181 + i = PC->val.immed; \
185 #define GET_BRANCH(a) \
187 - a = PC + PC->value; \
188 + if (PC->type != BRANCH_INST) { \
189 + return execError("Unexpected instruction, expected <branch>: <%s>", \
190 + instTypeToStr(PC->type)); \
192 + a = PC + PC->val.branch; \
196 @@ -1348,7 +1382,7 @@ static void addToGlobalSymTab(Symbol *sy
200 - else if (PC->op == OP_FETCH_RET_VAL) { \
201 + else if (PC->type == OP_INST && PC->val.op == OP_FETCH_RET_VAL) { \
205 @@ -4322,6 +4356,23 @@ static const char *typeToStr(enum symTyp
209 +static const char *instTypeToStr(enum instTypes type)
213 + return "operation";
215 + return "immediate";
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,54 @@ 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) {
237 + printd(" <%s %d>\n",
238 + instTypeToStr(inst[i].type),
239 + inst[i].val.immed);
243 + printd(" <%s (%+td) %8p>\n",
244 + instTypeToStr(inst[i].type),
245 + inst[i].val.branch,
246 + &inst[i] + inst[i].val.branch);
250 + printd(" <%s %s>\n",
251 + instTypeToStr(inst[i].type),
252 + inst[i].val.sym->name);
256 + printd(" <unknown %tx>\n", inst[i].val.branch);
262 + switch (inst[i].val.op) {
263 #define OP(name, fn) case OP_##name:
266 - printd(" %*s", (int)opLen, opNames[inst[i].op]);
267 + printd(" %*s", (int)opLen, opNames[inst[i].val.op]);
269 - switch (inst[i].op) {
270 + switch (inst[i].val.op) {
273 - printd(" %s", inst[i+1].sym->name);
274 - if (inst[i+1].sym->type == CONST_SYM
275 - && inst[i+1].sym->value.tag == STRING_TAG) {
276 + printd(" %s", inst[i+1].val.sym->name);
277 + if (inst[i+1].val.sym->type == CONST_SYM
278 + && inst[i+1].val.sym->value.tag == STRING_TAG) {
280 - dumpVal(inst[i+1].sym->value);
281 + dumpVal(inst[i+1].val.sym->value);
287 - printd(" %d", inst[i+1].value);
288 + printd(" %d", inst[i+1].val.immed);
292 @@ -4507,19 +4586,20 @@ static void disasmInternal(Inst *inst, i
294 case OP_BRANCH_FALSE:
295 case OP_BRANCH_NEVER:
296 - printd(" to=(%+d) %8p",
297 - inst[i+1].value, &inst[i+1] + inst[i+1].value);
298 + printd(" to=(%+td) %8p",
299 + inst[i+1].val.branch,
300 + &inst[i+1] + inst[i+1].val.branch);
305 - printd(" nExpr=%d", inst[i+1].value);
306 + printd(" nExpr=%d", inst[i+1].val.immed);
311 - int args = inst[i+2].value;
312 - printd(" %s", inst[i+1].sym->name);
313 + int args = inst[i+2].val.immed;
314 + printd(" %s", inst[i+1].val.sym->name);
316 printd(" %d+args[] (%d)", -args - 1, args);
318 @@ -4531,7 +4611,7 @@ static void disasmInternal(Inst *inst, i
321 case OP_UNPACKTOARGS: {
322 - int args = inst[i+2].value;
323 + int args = inst[i+2].val.immed;
325 printd(" %d+args[] (%d)", -args - 1, args);
327 @@ -4543,56 +4623,56 @@ static void disasmInternal(Inst *inst, i
330 case OP_SUBR_CALL_STACKED_N:
331 - printd(" %s =args[] (?)", inst[i+1].sym->name);
332 + printd(" %s =args[] (?)", inst[i+1].val.sym->name);
336 case OP_BEGIN_ARRAY_ITER:
337 case OP_BEGIN_ARRAY_ITER_ARRAY:
338 - printd(" %s in", inst[i+1].sym->name);
339 + printd(" %s in", inst[i+1].val.sym->name);
344 - if (!inst[i+1].value) {
345 + if (!inst[i+1].val.immed) {
347 - printd(" %s = %s++ end-loop=(%+d) %8p",
348 - inst[i+2].sym->name,
349 - inst[i+3].sym->name,
351 - &inst[i+4] + inst[i+4].value);
352 + printd(" %s = %s++ end-loop=(%+td) %8p",
353 + inst[i+2].val.sym->name,
354 + inst[i+3].val.sym->name,
355 + inst[i+4].val.branch,
356 + &inst[i+4] + inst[i+4].val.branch);
361 - printd(" %s=%s = %s++ end-loop=(%+d) %8p",
362 - inst[i+2].sym->name,
363 - inst[i+3].sym->name,
364 - inst[i+4].sym->name,
366 - &inst[i+5] + inst[i+5].value);
367 + printd(" %s=%s = %s++ end-loop=(%+td) %8p",
368 + inst[i+2].val.sym->name,
369 + inst[i+3].val.sym->name,
370 + inst[i+4].val.sym->name,
371 + inst[i+5].val.branch,
372 + &inst[i+5] + inst[i+5].val.branch);
377 case OP_ARRAY_ITER_ARRAY:
378 - if (!inst[i+1].value) {
379 + if (!inst[i+1].val.immed) {
381 - printd(" %s[] = %s++ end-loop=(%+d) %8p",
382 - inst[i+2].sym->name,
383 - inst[i+3].sym->name,
385 - &inst[i+4] + inst[i+4].value);
386 + printd(" %s[] = %s++ end-loop=(%+td) %8p",
387 + inst[i+2].val.sym->name,
388 + inst[i+3].val.sym->name,
389 + inst[i+4].val.branch,
390 + &inst[i+4] + inst[i+4].val.branch);
395 - printd(" %s[]=%s = %s++ end-loop=(%+d) %8p",
396 - inst[i+2].sym->name,
397 - inst[i+3].sym->name,
398 - inst[i+4].sym->name,
400 - &inst[i+5] + inst[i+5].value);
401 + printd(" %s[]=%s = %s++ end-loop=(%+td) %8p",
402 + inst[i+2].val.sym->name,
403 + inst[i+3].val.sym->name,
404 + inst[i+4].val.sym->name,
405 + inst[i+5].val.branch,
406 + &inst[i+5] + inst[i+5].val.branch);
410 @@ -4603,21 +4683,21 @@ static void disasmInternal(Inst *inst, i
411 case OP_ANONARRAY_INDEX_VAL:
414 - printd(" nDim=%d", inst[i+1].value);
415 + printd(" nDim=%d", inst[i+1].val.immed);
419 case OP_ARRAY_REF_ASSIGN_SETUP:
420 printd(" binOp=%s nDim=%d",
421 - inst[i+1].value ? "true" : "false",
423 + inst[i+1].val.immed ? "true" : "false",
424 + inst[i+2].val.immed);
428 case OP_PUSH_ARRAY_SYM:
430 - inst[i+1].sym->name,
431 - inst[i+2].value ? "createAndRef" : "refOnly");
432 + inst[i+1].val.sym->name,
433 + inst[i+2].val.immed ? "createAndRef" : "refOnly");
437 @@ -4627,7 +4707,7 @@ static void disasmInternal(Inst *inst, i
441 - printd(" %x\n", inst[i].value);
442 + printd(" %lx\n", inst[i].val.immed);
446 diff --quilt old/source/interpret.h new/source/interpret.h
447 --- old/source/interpret.h
448 +++ new/source/interpret.h
449 @@ -54,6 +54,8 @@ enum typeTags {NO_TAG, INT_TAG, STRING_T
451 enum execReturnCodes {MACRO_TIME_LIMIT, MACRO_PREEMPT, MACRO_DONE, MACRO_ERROR};
453 +enum instTypes {OP_INST, IMMED_INST, BRANCH_INST, SYM_INST};
455 #define ARRAY_DIM_SEP "\034"
458 @@ -61,10 +63,14 @@ struct SparseArrayEntryTag;
462 -typedef union InstTag {
463 - enum operations op;
465 - struct SymbolRec *sym;
466 +typedef struct InstTag {
467 + enum instTypes type;
469 + enum operations op;
472 + struct SymbolRec *sym;
476 typedef int (*BuiltInSubr)(WindowInfo *window, struct DataValueTag *argList,
477 @@ -150,7 +156,7 @@ int ArrayCopy(DataValue *dstArray, DataV
478 void BeginCreatingProgram(const char *name, AccumulatorData *acc);
479 int AddOp(int op, char **msg);
480 int AddSym(Symbol *sym, char **msg);
481 -int AddImmediate(int value, char **msg);
482 +int AddImmediate(int immed, char **msg);
483 int AddBranchOffset(Inst *to, char **msg);
484 int SetBranchOffset(Inst *from, Inst *to, char **msg);