MultipleAssignment selectors
[nedit-bw.git] / typed-Inst.patch
blobf531703a1956c5d58ec34354a036c4f7ff534a2b
1 ---
3 source/interpret.c | 219 ++++++++++++++++++++++++++++++++++++-----------------
4 source/interpret.h | 16 ++-
5 2 files changed, 161 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 @@ -112,6 +112,7 @@ static SparseArrayEntry *allocateSparseA
11 static int inTypeOfMode;
13 static const char *tagToStr(enum typeTags tag);
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 @@ -4301,6 +4335,22 @@ static const char *tagToStr(enum typeTag
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 "";
225 #ifdef DEBUG_DISASSEMBLER /* dumping values in disassembly or stack dump */
226 static char *printdBuffer = NULL;
227 static int printdPos = 0;
228 @@ -4461,25 +4511,55 @@ static void disasmInternal(Inst *inst, i
229 for (i = 0; i < nInstr; ++i) {
230 printd("Prog %8p", &inst[i]);
232 - switch (inst[i].op) {
233 + if (inst[i].type != OP_INST) {
234 + switch (inst[i].type) {
235 + case IMMED_INST:
236 + printd(" <%s %d>\n",
237 + instTypeToStr(inst[i].type),
238 + inst[i].val.immed);
239 + 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 + break;
248 + case SYM_INST:
249 + printd(" <%s %s>\n",
250 + instTypeToStr(inst[i].type),
251 + inst[i].val.sym->name);
252 + break;
254 + default:
255 + printd(" <unknown inst type %d %8p>\n", inst[i].type,
256 + *(void **)&inst[i].val.immed);
257 + break;
260 + continue;
263 + switch (inst[i].val.op) {
264 #define OP(name, fn) case OP_##name:
265 #include "ops.h"
266 #undef OP
267 - printd(" %*s", (int)opLen, opNames[inst[i].op]);
268 + printd(" %*s", (int)opLen, opNames[inst[i].val.op]);
270 - switch (inst[i].op) {
271 + switch (inst[i].val.op) {
272 case OP_PUSH_SYM:
273 case OP_ASSIGN:
274 - printd(" %s", inst[i+1].sym->name);
275 - if (inst[i+1].sym->type == CONST_SYM
276 - && inst[i+1].sym->value.tag == STRING_TAG) {
277 - dumpVal(inst[i+1].sym->value);
278 + printd(" %s", inst[i+1].val.sym->name);
279 + if (inst[i+1].val.sym->type == CONST_SYM
280 + && inst[i+1].val.sym->value.tag == STRING_TAG) {
281 + dumpVal(inst[i+1].val.sym->value);
283 ++i;
284 break;
286 case OP_PUSH_IMMED:
287 - printd(" %d", inst[i+1].value);
288 + printd(" %d", inst[i+1].val.immed);
289 ++i;
290 break;
292 @@ -4487,19 +4567,20 @@ static void disasmInternal(Inst *inst, i
293 case OP_BRANCH_TRUE:
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);
301 ++i;
302 break;
304 case OP_CONCAT:
305 - printd(" nExpr=%d", inst[i+1].value);
306 + printd(" nExpr=%d", inst[i+1].val.immed);
307 ++i;
308 break;
310 case OP_SUBR_CALL: {
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);
315 if (args < 0) {
316 printd(" %d+args[] (%d)", -args - 1, args);
318 @@ -4511,7 +4592,7 @@ static void disasmInternal(Inst *inst, i
319 break;
321 case OP_UNPACKTOARGS: {
322 - int args = inst[i+2].value;
323 + int args = inst[i+2].val.immed;
324 if (args < 0) {
325 printd(" %d+args[] (%d)", -args - 1, args);
327 @@ -4523,56 +4604,56 @@ static void disasmInternal(Inst *inst, i
328 break;
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);
333 ++i;
334 break;
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);
340 ++i;
341 break;
343 case OP_ARRAY_ITER:
344 - if (!inst[i+1].value) {
345 + if (!inst[i+1].val.immed) {
346 /* without val */
347 - printd(" %s = %s++ end-loop=(%+d) %8p",
348 - inst[i+2].sym->name,
349 - inst[i+3].sym->name,
350 - inst[i+4].value,
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);
357 i += 4;
359 else {
360 /* with val */
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,
365 - inst[i+5].value,
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);
373 i += 5;
375 break;
377 case OP_ARRAY_ITER_ARRAY:
378 - if (!inst[i+1].value) {
379 + if (!inst[i+1].val.immed) {
380 /* without val */
381 - printd(" %s[] = %s++ end-loop=(%+d) %8p",
382 - inst[i+2].sym->name,
383 - inst[i+3].sym->name,
384 - inst[i+4].value,
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);
391 i += 4;
393 else {
394 /* with val */
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,
399 - inst[i+5].value,
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);
407 i += 5;
409 break;
410 @@ -4583,21 +4664,21 @@ static void disasmInternal(Inst *inst, i
411 case OP_ANONARRAY_INDEX_VAL:
412 case OP_NAMED_ARG1:
413 case OP_NAMED_ARGN:
414 - printd(" nDim=%d", inst[i+1].value);
415 + printd(" nDim=%d", inst[i+1].val.immed);
416 ++i;
417 break;
419 case OP_ARRAY_REF_ASSIGN_SETUP:
420 printd(" binOp=%s nDim=%d",
421 - inst[i+1].value ? "true" : "false",
422 - inst[i+2].value);
423 + inst[i+1].val.immed ? "true" : "false",
424 + inst[i+2].val.immed);
425 i += 2;
426 break;
428 case OP_PUSH_ARRAY_SYM:
429 printd(" %s %s",
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");
434 i += 2;
435 break;
437 @@ -4607,7 +4688,7 @@ static void disasmInternal(Inst *inst, i
438 break;
440 default:
441 - printd(" <unknown op %d>\n", inst[i].op);
442 + printd(" <unknown op %d>\n", inst[i].val.op);
443 break;
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"
457 struct DataValueTag;
458 @@ -61,10 +63,14 @@ struct SparseArrayEntryTag;
459 struct ProgramTag;
460 struct SymbolRec;
462 -typedef union InstTag {
463 - enum operations op;
464 - int value;
465 - struct SymbolRec *sym;
466 +typedef struct InstTag {
467 + enum instTypes type;
468 + union {
469 + enum operations op;
470 + int immed;
471 + ptrdiff_t branch;
472 + struct SymbolRec *sym;
473 + } val;
474 } Inst;
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);
485 Inst *GetPC(void);