ternary ?: operator
[nedit-bw.git] / InterpretDebug-mods.patch
blob2b5be8fa05903ec86bd1a74094d1adb4e332aca0
1 Subject: modifications to the InterpretDebug patch
3 ---
5 makefiles/Makefile.bertw | 3
6 source/interpret.c | 181 +++++++++++++++++++++++++++++++----------------
7 source/interpret.h | 5 -
8 source/parse.y | 14 +--
9 source/userCmds.c | 4 -
10 5 files changed, 135 insertions(+), 72 deletions(-)
12 diff --quilt old/source/interpret.c new/source/interpret.c
13 --- old/source/interpret.c
14 +++ new/source/interpret.c
15 @@ -112,23 +112,24 @@ static const char *tagToStr(enum typeTag
17 #if defined(DEBUG_ASSEMBLY) || defined(DEBUG_STACK)
18 #define DEBUG_DISASSEMBLER
19 -static const char *printd(const char *f, ...);
20 +static const char *printd(const char *f, ...)
21 +__attribute__((__format__(printf,1,2)));
22 static int outPrintd();
23 -static void disasm(Inst *inst, int nInstr);
24 +static void disasm(const char *name, Inst *inst, int nInstr);
25 static void disasmInternal(Inst *inst, int nInstr);
26 #endif /* #if defined(DEBUG_ASSEMBLY) || defined(DEBUG_STACK) */
28 #ifdef DEBUG_ASSEMBLY /* for disassembly */
29 -#define DISASM(i, n) disasm(i, n)
30 +#define DISASM(name, i, n) disasm(name, i, n)
31 #else /* #ifndef DEBUG_ASSEMBLY */
32 -#define DISASM(i, n)
33 +#define DISASM(name, i, n)
34 #endif /* #ifndef DEBUG_ASSEMBLY */
36 #ifdef DEBUG_STACK /* for run-time instruction and stack trace */
37 static void stackdump(int n, int extra);
38 static void stackdumpInternal(int n, int extra);
39 #define STACKDUMP(n, x) stackdump(n, x)
40 -#define DISASM_RT(i, n) disasm(i, n)
41 +#define DISASM_RT(i, n) disasm(NULL, i, n)
42 #else /* #ifndef DEBUG_STACK */
43 #define STACKDUMP(n, x)
44 #define DISASM_RT(i, n)
45 @@ -161,6 +162,8 @@ static Inst *ProgP; /* next free spot
46 static Inst *LoopStack[LOOP_STACK_SIZE]; /* addresses of break, cont stmts */
47 static Inst **LoopStackPtr = LoopStack; /* to fill at the end of a loop */
49 +static const char *ProgramName = "";
51 /* Global data for the interpreter */
52 static DataValue *TheStack; /* the stack */
53 static DataValue *StackP; /* next free spot on stack */
54 @@ -253,7 +256,7 @@ void InitMacroGlobals(void)
55 ** Start collecting instructions for a program. Clears the program
56 ** and the symbol table.
58 -void BeginCreatingProgram(AccumulatorData *acc)
59 +void BeginCreatingProgram(const char *name, AccumulatorData *acc)
61 /* save state */
62 acc->localSymList = LocalSymList;
63 @@ -261,10 +264,12 @@ void BeginCreatingProgram(AccumulatorDat
64 acc->progP = ProgP;
65 memcpy(acc->loopStack, LoopStack, sizeof(*LoopStack) * LOOP_STACK_SIZE);
66 acc->loopStackPtr = LoopStackPtr;
67 + acc->name = ProgramName;
69 LocalSymList = NULL;
70 ProgP = Prog;
71 LoopStackPtr = LoopStack;
72 + ProgramName = name;
76 @@ -278,12 +283,12 @@ Program *FinishCreatingProgram(Accumulat
77 int progLen, fpOffset = 0;
78 Symbol *s;
80 - newProg = (Program *)XtMalloc(sizeof(Program));
81 + newProg = (Program *)XtMalloc(sizeof(Program) + strlen(ProgramName));
82 progLen = ((char *)ProgP) - ((char *)Prog);
83 newProg->code = (Inst *)XtMalloc(progLen);
84 memcpy(newProg->code, Prog, progLen);
85 newProg->localSymList = LocalSymList;
86 - newProg->name = NULL;
87 + strcpy(newProg->name, ProgramName);
88 newProg->refcount = 1;
90 /* Local variables' values are stored on the stack. Here we assign
91 @@ -291,7 +296,7 @@ Program *FinishCreatingProgram(Accumulat
92 for (s = newProg->localSymList; s != NULL; s = s->next)
93 s->value.val.n = fpOffset++;
95 - DISASM(newProg->code, ProgP - Prog);
96 + DISASM(newProg->name, newProg->code, ProgP - Prog);
98 /* restore state */
99 LocalSymList = acc->localSymList;
100 @@ -299,6 +304,7 @@ Program *FinishCreatingProgram(Accumulat
101 ProgP = acc->progP;
102 memcpy(LoopStack, acc->loopStack, sizeof(*LoopStack) * LOOP_STACK_SIZE);
103 LoopStackPtr = acc->loopStackPtr;
104 + ProgramName = acc->name;
106 return newProg;
108 @@ -308,7 +314,6 @@ void FreeProgram(Program *prog)
109 if (--prog->refcount == 0) {
110 freeSymbolTable(prog->localSymList);
111 XtFree((char *)prog->code);
112 - XtFree((char *)prog->name);
113 XtFree((char *)prog);
116 @@ -3075,7 +3080,7 @@ static void arrayDisposeNode(rbTreeNode
118 SparseArrayEntry *ArrayNew(void)
120 - return((SparseArrayEntry *)rbTreeNew(arrayEmptyAllocator));
121 + return((SparseArrayEntry *)rbTreeNew(arrayEmptyAllocator));
125 @@ -3587,7 +3592,9 @@ static int errCheck(const char *s)
127 static char *stackDumpStr(DataValue *fp, const char *msg, char **s, int *pLen)
129 - int len;
130 + static const char backtraceMsg[] = "\n\nBacktrace:";
131 + char frameBuf[TYPE_INT_STR_SIZE(int) + 2];
132 + int len, nFrames, frameWidth, thisFrameWidth;
133 const char *op;
134 char *np;
135 DataValue *nfp = fp;
136 @@ -3595,22 +3602,26 @@ static char *stackDumpStr(DataValue *fp,
138 #ifdef DEBUG_STACK
139 const char *dump;
140 - printd("\n\n");
141 + static const char stackdumpMsg[] = "\n\nStack:\n";
142 disasmInternal(PC - 1, 1);
143 stackdumpInternal(0, 50);
144 dump = printd(NULL);
145 #endif
147 /* first measure the lengths */
148 - len = strlen(msg) + 1;
149 + len = strlen(msg) + strlen(backtraceMsg) + 1;
150 nfp = fp;
151 + nFrames = 0;
152 do {
153 + nFrames++;
154 len = len + FP_GET_ITEM(nfp, FP_FUNCTION_NAME).val.str.len + 1;
155 pc = FP_GET_RET_PC(nfp);
156 nfp = FP_GET_OLD_FP(nfp);
157 } while (pc);
158 + frameWidth = lenLongAsStr(nFrames);
159 + len += nFrames * (2 + frameWidth);
160 #ifdef DEBUG_STACK
161 - len += strlen(dump);
162 + len += strlen(stackdumpMsg) + strlen(dump);
163 #endif
164 if (*pLen < len)
166 @@ -3622,10 +3633,25 @@ static char *stackDumpStr(DataValue *fp,
167 op = msg;
168 while (*op)
169 *np++ = *op++;
170 + op = backtraceMsg;
171 + while (*op)
172 + *np++ = *op++;
174 nfp = fp;
175 + nFrames = 0;
176 do {
177 + nFrames++;
178 *np++ = '\n';
179 + *np++ = '#';
180 + thisFrameWidth = 0;
181 + op = longAsStr(nFrames);
182 + while (*op) {
183 + thisFrameWidth++;
184 + *np++ = *op++;
186 + while (thisFrameWidth++ < frameWidth)
187 + *np++ = ' ';
188 + *np++ = ' ';
189 op = FP_GET_ITEM(nfp, FP_FUNCTION_NAME).val.str.rep;
190 while (*op)
191 *np++ = *op++;
192 @@ -3633,12 +3659,15 @@ static char *stackDumpStr(DataValue *fp,
193 nfp = FP_GET_OLD_FP(nfp);
194 } while (pc);
195 #ifdef DEBUG_STACK
196 + op = stackdumpMsg;
197 + while (*op)
198 + *np++ = *op++;
199 op = dump;
200 while (*op)
201 *np++ = *op++;
202 #endif
204 - *np = 0;
205 + *np = '\0';
206 return *s;
209 @@ -3782,31 +3811,50 @@ int outPrintd()
210 #ifdef DEBUG_DISASSEMBLER /* dumping values in disassembly or stack dump */
211 static void dumpVal(DataValue dv)
213 + printd(" ");
214 switch (dv.tag) {
215 case INT_TAG:
216 - printd("i=%d", dv.val.n);
217 + printd("<%s %d>", tagToStr(INT_TAG), dv.val.n);
218 break;
219 case STRING_TAG:
221 - int k;
222 - char s[21];
223 - char *src = dv.val.str.rep;
224 + int k, l;
225 + char s[64];
226 + const char *src = dv.val.str.rep;
227 if (!src) {
228 - printd("s=<NULL>");
229 + printd("<%s NULL>", tagToStr(STRING_TAG));
231 else {
232 - for (k = 0; src[k] && k < sizeof s - 1; k++) {
233 - s[k] = isprint(src[k]) ? src[k] : '?';
234 + for (k = 0, l = 0; src[k] && l < sizeof s - 1; k++, l++) {
235 + char *e;
236 + const char to[] = "\\\"ntbrfave";
237 +#ifdef EBCDIC_CHARSET
238 + const char from[] = "\\\"\n\t\b\r\f\a\v\x27"; /* EBCDIC escape */
239 +#else
240 + const char from[] = "\\\"\n\t\b\r\f\a\v\x1B"; /* ASCII escape */
241 +#endif
242 + if ((e = strchr(from, src[k]))) {
243 + if (l < sizeof s - 2) {
244 + s[l++] = '\\';
245 + s[l] = to[e - from];
248 + else if (isprint(src[k])) {
249 + s[l] = src[k];
251 + else {
252 + s[l] = '?';
255 - s[k] = 0;
256 - printd("s=\"%s\"%s[%d]", s,
257 - src[k] ? "..." : "", strlen(src));
258 + s[l] = 0;
259 + printd("<%s %u:\"%s\"%s>", tagToStr(STRING_TAG),
260 + (unsigned)strlen(src), s, src[k] ? "..." : "");
263 break;
264 case ARRAY_TAG:
265 - printd("%08p <%s>[%d]", dv.val.arrayPtr, tagToStr(ARRAY_TAG),
266 - ArraySize(&dv));
267 + printd("<%s %u:%8p>", tagToStr(ARRAY_TAG), ArraySize(&dv),
268 + dv.val.arrayPtr);
269 break;
270 case NO_TAG:
271 if (!dv.val.inst) {
272 @@ -3832,57 +3880,65 @@ static void disasmInternal(Inst *inst, i
273 #undef OP
275 int i, j;
276 + static size_t opLen;
278 - printd("\n");
279 + if (!opLen) {
280 + for (j = 0; j < N_OPS; ++j) {
281 + if (opLen < strlen(opNames[j])) {
282 + opLen = strlen(opNames[j]);
287 for (i = 0; i < nInstr; ++i) {
288 - printd("Prog %8p ", &inst[i]);
289 + printd("Prog %8p", &inst[i]);
290 for (j = 0; j < N_OPS; ++j) {
291 if (inst[i].func == OpFns[j]) {
292 - printd("%22s ", opNames[j]);
293 + printd(" %*s", (int)opLen, opNames[j]);
294 if (j == OP_PUSH_SYM || j == OP_ASSIGN) {
295 Symbol *sym = inst[i+1].sym;
296 - printd("%s", sym->name);
297 - if (sym->value.tag == STRING_TAG &&
298 - strncmp(sym->name, "string #", 8) == 0) {
299 + printd(" %s", sym->name);
300 + if (sym->type == CONST_SYM
301 + && sym->value.tag == STRING_TAG) {
302 dumpVal(sym->value);
304 ++i;
306 else if (j == OP_PUSH_IMMED) {
307 - printd("i=%d", inst[i+1].value);
308 + printd(" <immediate %d>", inst[i+1].value);
309 ++i;
311 else if (j == OP_BRANCH || j == OP_BRANCH_FALSE ||
312 j == OP_BRANCH_NEVER || j == OP_BRANCH_TRUE) {
313 - printd("to=(%d) %p", inst[i+1].value,
314 + printd(" to=(%+d) %8p", inst[i+1].value,
315 &inst[i+1] + inst[i+1].value);
316 ++i;
318 else if (j == OP_CONCAT) {
319 - printd("nExpr=%d", inst[i+1].value);
320 + printd(" nExpr=%d", inst[i+1].value);
321 ++i;
323 else if (j == OP_SUBR_CALL) {
324 int args = inst[i+2].value;
325 - printd("%s ", inst[i+1].sym->name);
326 + printd(" %s", inst[i+1].sym->name);
327 if (args < 0) {
328 - printd("%d+args[] (%d)", -args - 1, args);
329 + printd(" %d+args[] (%d)", -args - 1, args);
331 else {
332 - printd("%d args", args);
333 + printd(" %d args", args);
335 i += 2;
337 else if (j == OP_SUBR_CALL_STACKED_N) {
338 - printd("%s args[] (?)", inst[i+1].sym->name);
339 + printd(" %s args[] (?)", inst[i+1].sym->name);
340 ++i;
342 else if (j == OP_BEGIN_ARRAY_ITER) {
343 - printd("%s in", inst[i+1].sym->name);
344 + printd(" %s in", inst[i+1].sym->name);
345 ++i;
347 else if (j == OP_ARRAY_ITER) {
348 - printd("%s = %s++ end-loop=(%d) %p",
349 + printd(" %s = %s++ end-loop=(%+d) %8p",
350 inst[i+1].sym->name,
351 inst[i+2].sym->name,
352 inst[i+3].value, &inst[i+3] + inst[i+3].value);
353 @@ -3894,18 +3950,20 @@ static void disasmInternal(Inst *inst, i
354 j == OP_ANONARRAY_INDEX_VAL ||
355 j == OP_NAMED_ARG1 ||
356 j == OP_NAMED_ARGN) {
357 - printd("nDim=%d", inst[i+1].value);
358 + printd(" nDim=%d", inst[i+1].value);
359 ++i;
361 else if (j == OP_ARRAY_REF_ASSIGN_SETUP) {
362 - printd("binOp=%s ", inst[i+1].value ? "true" : "false");
363 - printd("nDim=%d", inst[i+2].value);
364 + printd(" binOp=%s nDim=%d",
365 + inst[i+1].value ? "true" : "false",
366 + inst[i+2].value);
367 i += 2;
369 else if (j == OP_PUSH_ARRAY_SYM) {
370 - printd("%s", inst[++i].sym->name);
371 - printd(" %s", inst[i+1].value ? "createAndRef" : "refOnly");
372 - ++i;
373 + printd(" %s %s",
374 + inst[i+1].sym->name,
375 + inst[i+2].value ? "createAndRef" : "refOnly");
376 + i += 2;
379 printd("\n");
380 @@ -3913,18 +3971,20 @@ static void disasmInternal(Inst *inst, i
383 if (j == N_OPS) {
384 - printd("%x\n", inst[i].value);
385 + printd(" %x\n", inst[i].value);
390 -static void disasm(Inst *inst, int nInstr)
391 +static void disasm(const char *name, Inst *inst, int nInstr)
393 static int outIsTTY = -1;
394 if (outIsTTY == -1) outIsTTY = isatty(fileno(stdout));
395 if (outIsTTY) { printd("\033[H"); }
396 + if (name) printd(">> %s\n", name);
397 disasmInternal(inst, nInstr);
398 if (outIsTTY) { printd("\033[J\n"); }
399 + if (name) printd("\n");
400 outPrintd();
402 #endif /* #ifdef DEBUG_DISASSEMBLER */
403 @@ -3975,7 +4035,7 @@ static void stackdumpframe(DataValue *ar
404 for (dv = endDv; dv < sp; dv++)
405 #endif /* #ifdef DEBUG_STACK_HEADFIRST */
407 - const char *posFmt = "%-6s ";
408 + const char *posFmt = "%-6s";
409 const char *symName = "";
411 char *pos = "";
412 @@ -3984,8 +4044,7 @@ static void stackdumpframe(DataValue *ar
413 const char *leadIn = (dv >= arrow) ? ">>>>" :
414 (dv == arg1) ? "----" :
415 (dv == fnNm) ? "====" : "";
416 - printd("%4.4s", leadIn);
417 - printd("%8p%c", dv, topMark);
418 + printd("%4.4s%8p%c", leadIn, dv, topMark);
419 switch (offset) {
420 case FP_ARG_COUNT_INDEX: pos = "NArgs"; break; /* num. arguments */
421 case FP_FUNCTION_NAME: pos = "FnName"; break;
422 @@ -4003,7 +4062,7 @@ static void stackdumpframe(DataValue *ar
424 else if (0 <= offset && offset < nSyms) {
425 sprintf(pos = buffer, offset ? "[%d]" : "FP[%d]", offset);
426 - posFmt = "%6s ";
427 + posFmt = "%6s";
429 else if (offset == 0) {
430 pos = "FrameP";
431 @@ -4021,10 +4080,10 @@ static void stackdumpframe(DataValue *ar
435 - printd("%-*.*s ", symLen, symLen, symName);
436 + printd(" %-*.*s", symLen, symLen, symName);
438 if (dv == fnNm && dv->tag == STRING_TAG && dv->val.str.rep)
439 - printd("%s", dv->val.str.rep);
440 + printd(" %s", dv->val.str.rep);
441 else
442 dumpVal(*dv);
444 @@ -4053,7 +4112,7 @@ static void stackdumpInternal(int n, int
445 if (outpt < TheStack)
446 printd("--------------Stack base--------------\n");
447 stackdumpframe(arrow, outpt, FrameP, StackP, '*');
448 - printd("Stack ----->\n");
449 + printd("Stack ----->\n\n");
450 #endif /* #ifdef DEBUG_STACK_HEADFIRST */
453 @@ -4063,7 +4122,11 @@ static void stackdump(int n, int extra)
454 if (outIsTTY == -1)
455 outIsTTY = isatty(fileno(stdout));
457 - stackdumpInternal(n, extra);
458 +#ifndef DEBUG_STACKDUMP_EXTRA
459 +#define DEBUG_STACKDUMP_EXTRA 0
460 +#endif
461 + stackdumpInternal(n, extra + DEBUG_STACKDUMP_EXTRA);
462 +#undef DEBUG_STACKDUMP_EXTRA
464 if (outIsTTY)
465 printd("\033[J\n");
466 diff --quilt old/source/interpret.h new/source/interpret.h
467 --- old/source/interpret.h
468 +++ new/source/interpret.h
469 @@ -108,7 +108,7 @@ typedef struct ProgramTag {
470 Symbol *localSymList;
471 Inst *code;
472 unsigned refcount;
473 - char *name;
474 + char name[1];
475 } Program;
477 /* Information needed to re-start a preempted macro */
478 @@ -128,6 +128,7 @@ typedef struct AccumulatorDataTag {
479 Inst *progP;
480 Inst *loopStack[LOOP_STACK_SIZE];
481 Inst **loopStackPtr;
482 + const char *name;
483 } AccumulatorData;
485 void InitMacroGlobals(void);
486 @@ -144,7 +145,7 @@ int ArrayCopy(DataValue *dstArray, DataV
488 /* Routines for creating a program, (accumulated beginning with
489 BeginCreatingProgram and returned via FinishCreatingProgram) */
490 -void BeginCreatingProgram(AccumulatorData *acc);
491 +void BeginCreatingProgram(const char *name, AccumulatorData *acc);
492 int AddOp(int op, char **msg);
493 int AddSym(Symbol *sym, char **msg);
494 int AddImmediate(int value, char **msg);
495 diff --quilt old/source/parse.y new/source/parse.y
496 --- old/source/parse.y
497 +++ new/source/parse.y
498 @@ -196,7 +196,7 @@ definesym: SYMBOL {
499 yyerror("try to override built-in subroutine"); YYERROR;
501 $$.sym = PromoteToGlobal($1);
502 - BeginCreatingProgram($$.acc);
503 + BeginCreatingProgram($$.sym->name, $$.acc);
506 define: definekw blank definesym blank blockwb {
507 @@ -745,14 +745,16 @@ Program *ParseMacro(char *expr, char **m
509 Program *prog;
510 AccumulatorData *acc = (AccumulatorData *)XtMalloc(sizeof(*acc));
511 - static const char *prefix = ">> ";
513 #if YYDEBUG
514 int oldyydebug = yydebug;
515 yydebug = 1;
516 #endif
518 - BeginCreatingProgram(acc);
519 + if (!name)
520 + name = "--unknown--";
522 + BeginCreatingProgram(name, acc);
524 /* whether we allow the "define" keyword */
525 AllowDefine = allowDefine;
526 @@ -780,12 +782,6 @@ Program *ParseMacro(char *expr, char **m
527 prog = FinishCreatingProgram(acc);
528 XtFree((char *)acc);
530 - if (!name)
531 - name = "--unknown--";
533 - prog->name = XtMalloc(strlen(name) + strlen(prefix) + 1);
534 - strcat(strcpy(prog->name, prefix), name);
536 /* parse succeeded */
537 *msg = "";
538 *stoppedAt = InPtr;
539 diff --quilt old/source/userCmds.c new/source/userCmds.c
540 --- old/source/userCmds.c
541 +++ new/source/userCmds.c
542 @@ -1286,13 +1286,13 @@ static int doMacroMenuCmd(WindowInfo *wi
543 int DoNamedMacroMenuCmd(WindowInfo *window, const char *itemName)
545 return doMacroMenuCmd(window, itemName, MacroMenuItems, NMacroMenuItems,
546 - "macro-menu>");
547 + "Macro Menu>");
550 int DoNamedBGMenuCmd(WindowInfo *window, const char *itemName)
552 return doMacroMenuCmd(window, itemName, BGMenuItems, NBGMenuItems,
553 - "background-menu>");
554 + "Background Menu>");
558 diff --quilt old/makefiles/Makefile.bertw new/makefiles/Makefile.bertw
559 --- old/makefiles/Makefile.bertw
560 +++ new/makefiles/Makefile.bertw
561 @@ -23,6 +23,9 @@ ifdef DEBUG
562 ifdef DEBUG_MACRO
563 ifndef DEBUG_NO_STACKDUMP
564 CFLAGS += -DDEBUG_STACK
565 + ifdef DEBUG_STACKDUMP_EXTRA
566 + CFLAGS += -DDEBUG_STACKDUMP_EXTRA=$(DEBUG_STACKDUMP_EXTRA)
567 + endif
568 endif
569 ifndef DEBUG_NO_DISASM
570 CFLAGS += -DDEBUG_ASSEMBLY