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";
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,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) {
237 + printd(" %s: %d\n",
238 + instTypeToStr(inst[i].type),
239 + inst[i].val.immed);
242 + printd(" %s: (%+td) %8p\n",
243 + instTypeToStr(inst[i].type),
244 + inst[i].val.branch,
245 + &inst[i] + inst[i].val.branch);
247 + printd(" %s: %s\n",
248 + instTypeToStr(inst[i].type),
249 + inst[i].val.sym->name);
252 + printd(" <unknown>: %tx\n", inst[i].val.branch);
258 + switch (inst[i].val.op) {
259 #define OP(name, fn) case OP_##name:
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) {
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) {
276 - dumpVal(inst[i+1].sym->value);
277 + dumpVal(inst[i+1].val.sym->value);
283 - printd(" %d", inst[i+1].value);
284 + printd(" %d", inst[i+1].val.immed);
288 @@ -4507,19 +4582,20 @@ static void disasmInternal(Inst *inst, i
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);
301 - printd(" nExpr=%d", inst[i+1].value);
302 + printd(" nExpr=%d", inst[i+1].val.immed);
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);
312 printd(" %d+args[] (%d)", -args - 1, args);
314 @@ -4531,7 +4607,7 @@ static void disasmInternal(Inst *inst, i
317 case OP_UNPACKTOARGS: {
318 - int args = inst[i+2].value;
319 + int args = inst[i+2].val.immed;
321 printd(" %d+args[] (%d)", -args - 1, args);
323 @@ -4543,56 +4619,56 @@ static void disasmInternal(Inst *inst, i
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);
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);
340 - if (!inst[i+1].value) {
341 + if (!inst[i+1].val.immed) {
343 - printd(" %s = %s++ end-loop=(%+d) %8p",
344 - inst[i+2].sym->name,
345 - inst[i+3].sym->name,
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);
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,
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);
373 case OP_ARRAY_MULTI_ITER_ARRAY:
374 - if (!inst[i+1].value) {
375 + if (!inst[i+1].val.immed) {
377 - printd(" %s[] = %s++ end-loop=(%+d) %8p",
378 - inst[i+2].sym->name,
379 - inst[i+3].sym->name,
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);
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,
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);
406 @@ -4603,21 +4679,21 @@ static void disasmInternal(Inst *inst, i
407 case OP_ANONARRAY_INDEX_VAL:
410 - printd(" nDim=%d", inst[i+1].value);
411 + printd(" nDim=%d", inst[i+1].val.immed);
415 case OP_ARRAY_REF_ASSIGN_SETUP:
416 printd(" binOp=%s nDim=%d",
417 - inst[i+1].value ? "true" : "false",
419 + inst[i+1].val.immed ? "true" : "false",
420 + inst[i+2].val.immed);
424 case OP_PUSH_ARRAY_SYM:
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");
433 @@ -4627,7 +4703,7 @@ static void disasmInternal(Inst *inst, i
437 - printd(" %x\n", inst[i].value);
438 + printd(" %lx\n", inst[i].val.immed);
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"
454 @@ -61,10 +63,14 @@ struct SparseArrayEntryTag;
458 -typedef union InstTag {
459 - enum operations op;
461 - struct SymbolRec *sym;
462 +typedef struct InstTag {
463 + enum instTypes type;
465 + enum operations op;
468 + struct SymbolRec *sym;
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);