pushString: fix syntax error
[nedit-bw.git] / typed-Inst.patch
blob912823a5a22261693932a2c9caf8f33b10947993
1 ---
3 source/interpret.c | 293 ++++++++++++++++++++++++++++++++++++++---------------
4 source/interpret.h | 16 +-
5 2 files changed, 224 insertions(+), 85 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 @@ -338,7 +339,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 @@ -352,7 +354,8 @@ int AddSym(Symbol *sym, char **msg)
29 *msg = "macro too large";
30 return 0;
32 - ProgP->sym = sym;
33 + ProgP->type = SYM_INST;
34 + ProgP->val.sym = sym;
35 ProgP++;
36 return 1;
38 @@ -360,13 +363,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 @@ -380,8 +384,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 @@ -392,7 +396,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 @@ -445,10 +453,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.immed = NEEDS_BREAK;
92 return 1;
95 @@ -458,10 +470,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.immed = NEEDS_CONTINUE;
108 return 1;
111 @@ -485,10 +501,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 @@ -660,14 +676,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 @@ -743,7 +765,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 @@ -1321,19 +1343,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 @@ -1347,7 +1381,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 @@ -4242,6 +4276,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 "unknown";
225 #ifdef DEBUG_DISASSEMBLER /* dumping values in disassembly or stack dump */
226 static char *printdBuffer = NULL;
227 static int printdPos = 0;
228 @@ -4378,20 +4428,74 @@ static void dumpVal(DataValue dv)
229 #endif /* #ifdef DEBUG_DISASSEMBLER */
231 #ifdef DEBUG_DISASSEMBLER /* For debugging code generation */
232 -static void disasmInternal(Inst *inst, int nInstr)
233 +static int checkOperands(Inst *inst, int nopers, ...)
235 - static const char *opNames[] = {
236 + va_list args;
237 + int i;
239 + va_start(args, nopers);
240 + for (i = 0; i < nopers; i++) {
241 + enum instTypes type = va_arg(args, enum instTypes);
242 + if (inst[i].type != type) {
243 + va_end(args);
244 + printd(" operand %d is not of type <%s>",
245 + i + 1, instTypeToStr(type));
246 + return False;
249 + va_end(args);
251 + return True;
254 +static const char *OpNames[] = {
255 #define OP(name, fn) #name,
256 #include "ops.h"
257 #undef OP
258 - };
261 +static void dumpInst(Inst *inst, const char *name)
263 + name = name ? name : instTypeToStr(inst->type);
265 + switch (inst->type) {
266 + case OP_INST:
267 + printd(" <%s %s>", name, OpNames[inst->val.op]);
268 + break;
270 + case IMMED_INST:
271 + printd(" <%s %d>", name, inst->val.immed);
272 + break;
274 + case BRANCH_INST:
275 + printd(" <%s %+td:%8p>", name,
276 + inst->val.branch, inst + inst->val.branch);
277 + break;
279 + case SYM_INST:
280 + printd(" <%s %s>", name, inst->val.sym->name);
281 + break;
283 + default:
284 + printd(" <unknown inst %d:%8p>", inst->type,
285 + *(void **)&inst->val.immed);
286 + break;
290 +static void disasmInternal(Inst *inst, int nInstr)
292 +#define CHECK_OPERANDS(n, ...) \
293 + if (!checkOperands(&inst[i+1], n, __VA_ARGS__)) \
294 + break
296 int i;
297 static size_t opLen;
299 if (!opLen) {
300 for (i = 0; i < N_OPS; ++i) {
301 - if (opLen < strlen(opNames[i])) {
302 - opLen = strlen(opNames[i]);
303 + if (opLen < strlen(OpNames[i])) {
304 + opLen = strlen(OpNames[i]);
308 @@ -4399,25 +4503,33 @@ static void disasmInternal(Inst *inst, i
309 for (i = 0; i < nInstr; ++i) {
310 printd("Prog %8p", &inst[i]);
312 - switch (inst[i].op) {
313 + if (inst[i].type != OP_INST) {
314 + dumpInst(&inst[i], NULL);
315 + printd("\n");
316 + continue;
319 + switch (inst[i].val.op) {
320 #define OP(name, fn) case OP_##name:
321 #include "ops.h"
322 #undef OP
323 - printd(" %*s", (int)opLen, opNames[inst[i].op]);
324 + printd(" %*s", (int)opLen, OpNames[inst[i].val.op]);
326 - switch (inst[i].op) {
327 + switch (inst[i].val.op) {
328 case OP_PUSH_SYM:
329 case OP_ASSIGN:
330 - printd(" %s", inst[i+1].sym->name);
331 - if (inst[i+1].sym->type == CONST_SYM
332 - && inst[i+1].sym->value.tag == STRING_TAG) {
333 - dumpVal(inst[i+1].sym->value);
334 + CHECK_OPERANDS(1, SYM_INST);
335 + dumpInst(&inst[i+1], NULL);
336 + if (inst[i+1].val.sym->type == CONST_SYM
337 + && inst[i+1].val.sym->value.tag == STRING_TAG) {
338 + dumpVal(inst[i+1].val.sym->value);
340 ++i;
341 break;
343 case OP_PUSH_IMMED:
344 - printd(" <immediate %d>", inst[i+1].value);
345 + CHECK_OPERANDS(1, IMMED_INST);
346 + dumpInst(&inst[i+1], NULL);
347 ++i;
348 break;
350 @@ -4425,37 +4537,42 @@ static void disasmInternal(Inst *inst, i
351 case OP_BRANCH_TRUE:
352 case OP_BRANCH_FALSE:
353 case OP_BRANCH_NEVER:
354 - printd(" to=(%+d) %8p",
355 - inst[i+1].value, &inst[i+1] + inst[i+1].value);
356 + CHECK_OPERANDS(1, BRANCH_INST);
357 + dumpInst(&inst[i+1], NULL);
358 ++i;
359 break;
361 case OP_CONCAT:
362 - printd(" nExpr=%d", inst[i+1].value);
363 + CHECK_OPERANDS(1, IMMED_INST);
364 + dumpInst(&inst[i+1], "nExpr");
365 ++i;
366 break;
368 case OP_SUBR_CALL: {
369 - int args = inst[i+2].value;
370 - printd(" %s", inst[i+1].sym->name);
371 + int args;
372 + CHECK_OPERANDS(2, SYM_INST, IMMED_INST);
373 + args = inst[i+2].val.immed;
374 + dumpInst(&inst[i+1], NULL);
375 if (args < 0) {
376 - printd(" %d+args[] (%d)", -args - 1, args);
377 + printd("(%d+args[])", -args - 1);
379 else {
380 - printd(" %d args", args);
381 + printd("(%d args)", args);
383 i += 2;
385 break;
387 case OP_SUBR_CALL_UNPACK_ARRAY: {
388 - int args = inst[i+2].value;
389 - printd(" %s", inst[i+1].sym->name);
390 + int args;
391 + CHECK_OPERANDS(2, SYM_INST, IMMED_INST);
392 + args = inst[i+2].val.immed;
393 + dumpInst(&inst[i+1], NULL);
394 if (args < 0) {
395 - printd(" %d+args[] =args[]", -args - 1);
396 + printd("(%d+args[], =args[])", -args - 1);
398 else {
399 - printd(" %d args =args[]", args);
400 + printd("(%d args, =args[])", args);
402 i += 2;
404 @@ -4463,50 +4580,64 @@ static void disasmInternal(Inst *inst, i
406 case OP_BEGIN_ARRAY_ITER:
407 case OP_BEGIN_ARRAY_ITER_ARRAY:
408 - printd(" %s in", inst[i+1].sym->name);
409 + CHECK_OPERANDS(1, SYM_INST);
410 + printd(" in");
411 + dumpInst(&inst[i+1], "aryIter");
412 ++i;
413 break;
415 case OP_ARRAY_ITER:
416 - if (!inst[i+1].value) {
417 + CHECK_OPERANDS(1, IMMED_INST);
418 + if (!inst[i+1].val.immed) {
419 /* without val */
420 - printd(" %s = %s++ end-loop=(%+d) %8p",
421 - inst[i+2].sym->name,
422 - inst[i+3].sym->name,
423 - inst[i+4].value,
424 - &inst[i+4] + inst[i+4].value);
425 + CHECK_OPERANDS(4, IMMED_INST,
426 + SYM_INST, SYM_INST, BRANCH_INST);
427 + dumpInst(&inst[i+2], "key");
428 + printd(" :=");
429 + dumpInst(&inst[i+3], "aryIter");
430 + printd("++");
431 + dumpInst(&inst[i+4], "end-loop");
432 i += 4;
434 else {
435 /* with val */
436 - printd(" %s=%s = %s++ end-loop=(%+d) %8p",
437 - inst[i+2].sym->name,
438 - inst[i+3].sym->name,
439 - inst[i+4].sym->name,
440 - inst[i+5].value,
441 - &inst[i+5] + inst[i+5].value);
442 + CHECK_OPERANDS(5, IMMED_INST,
443 + SYM_INST, SYM_INST, SYM_INST, BRANCH_INST);
444 + dumpInst(&inst[i+2], "key");
445 + printd(" =");
446 + dumpInst(&inst[i+3], "val");
447 + printd(" :=");
448 + dumpInst(&inst[i+4], "aryIter");
449 + printd("++");
450 + dumpInst(&inst[i+5], "end-loop");
451 i += 5;
453 break;
455 case OP_ARRAY_ITER_ARRAY:
456 - if (!inst[i+1].value) {
457 + CHECK_OPERANDS(1, IMMED_INST);
458 + if (!inst[i+1].val.immed) {
459 /* without val */
460 - printd(" %s[] = %s++ end-loop=(%+d) %8p",
461 - inst[i+2].sym->name,
462 - inst[i+3].sym->name,
463 - inst[i+4].value,
464 - &inst[i+4] + inst[i+4].value);
465 + CHECK_OPERANDS(4, IMMED_INST,
466 + SYM_INST, SYM_INST, BRANCH_INST);
467 + dumpInst(&inst[i+2], "keyArr");
468 + printd(" :=");
469 + dumpInst(&inst[i+3], "aryIter");
470 + printd("++");
471 + dumpInst(&inst[i+4], "end-loop");
472 i += 4;
474 else {
475 /* with val */
476 - printd(" %s[]=%s = %s++ end-loop=(%+d) %8p",
477 - inst[i+2].sym->name,
478 - inst[i+3].sym->name,
479 - inst[i+4].sym->name,
480 - inst[i+5].value,
481 - &inst[i+5] + inst[i+5].value);
482 + CHECK_OPERANDS(5, IMMED_INST,
483 + SYM_INST, SYM_INST, SYM_INST, BRANCH_INST);
484 + dumpInst(&inst[i+2], "keyArr");
485 + printd("[] =");
486 + dumpInst(&inst[i+3], "val");
487 + printd(" :=");
488 + dumpInst(&inst[i+4], "aryIter");
489 + printd("++");
490 + dumpInst(&inst[i+5], "end-loop");
491 i += 5;
493 break;
494 @@ -4517,21 +4648,23 @@ static void disasmInternal(Inst *inst, i
495 case OP_ANONARRAY_INDEX_VAL:
496 case OP_NAMED_ARG1:
497 case OP_NAMED_ARGN:
498 - printd(" nDim=%d", inst[i+1].value);
499 + CHECK_OPERANDS(1, IMMED_INST);
500 + dumpInst(&inst[i+1], "nDim");
501 ++i;
502 break;
504 case OP_ARRAY_REF_ASSIGN_SETUP:
505 - printd(" binOp=%s nDim=%d",
506 - inst[i+1].value ? "true" : "false",
507 - inst[i+2].value);
508 + CHECK_OPERANDS(2, IMMED_INST, IMMED_INST);
509 + printd(" <binOp %s>,",
510 + inst[i+1].val.immed ? "true" : "false");
511 + dumpInst(&inst[i+2], "nDim");
512 i += 2;
513 break;
515 case OP_PUSH_ARRAY_SYM:
516 - printd(" %s %s",
517 - inst[i+1].sym->name,
518 - inst[i+2].value ? "createAndRef" : "refOnly");
519 + CHECK_OPERANDS(2, SYM_INST, IMMED_INST);
520 + dumpInst(&inst[i+1],
521 + inst[i+2].val.immed ? "createAndRef" : "refOnly");
522 i += 2;
523 break;
525 @@ -4543,7 +4676,7 @@ static void disasmInternal(Inst *inst, i
526 break;
528 default:
529 - printd(" <unknown op %d>\n", inst[i].op);
530 + printd(" <unknown op %d>\n", inst[i].val.op);
531 break;
534 diff --quilt old/source/interpret.h new/source/interpret.h
535 --- old/source/interpret.h
536 +++ new/source/interpret.h
537 @@ -54,6 +54,8 @@ enum typeTags {NO_TAG, INT_TAG, STRING_T
539 enum execReturnCodes {MACRO_TIME_LIMIT, MACRO_PREEMPT, MACRO_DONE, MACRO_ERROR};
541 +enum instTypes {OP_INST, IMMED_INST, BRANCH_INST, SYM_INST};
543 #define ARRAY_DIM_SEP "\034"
545 struct DataValueTag;
546 @@ -61,10 +63,14 @@ struct SparseArrayEntryTag;
547 struct ProgramTag;
548 struct SymbolRec;
550 -typedef union InstTag {
551 - enum operations op;
552 - int value;
553 - struct SymbolRec *sym;
554 +typedef struct InstTag {
555 + enum instTypes type;
556 + union {
557 + enum operations op;
558 + int immed;
559 + ptrdiff_t branch;
560 + struct SymbolRec *sym;
561 + } val;
562 } Inst;
564 typedef int (*BuiltInSubr)(WindowInfo *window, struct DataValueTag *argList,
565 @@ -149,7 +155,7 @@ int ArrayCopy(DataValue *dstArray, DataV
566 void BeginCreatingProgram(const char *name, AccumulatorData *acc);
567 int AddOp(int op, char **msg);
568 int AddSym(Symbol *sym, char **msg);
569 -int AddImmediate(int value, char **msg);
570 +int AddImmediate(int immed, char **msg);
571 int AddBranchOffset(Inst *to, char **msg);
572 int SetBranchOffset(Inst *from, Inst *to, char **msg);
573 Inst *GetPC(void);