refresh InterpretDebug5.diff
[nedit-bw.git] / InterpretDebug-mods.patch
blob09deb48cd7009b070d4b6d22c877cee6a08d6c97
1 Subject: modifications to the InterpretDebug patch
3 ---
5 makefiles/Makefile.bertw | 3
6 source/interpret.c | 188 +++++++++++++++++++++++++++++++----------------
7 source/interpret.h | 5 -
8 source/parse.y | 14 +--
9 source/userCmds.c | 4 -
10 5 files changed, 139 insertions(+), 75 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,13 @@ Program *FinishCreatingProgram(Accumulat
77 int progLen, fpOffset = 0;
78 Symbol *s;
80 - newProg = (Program *)XtMalloc(sizeof(Program));
81 + newProg = XtNew(Program);
82 + newProg->name = XtMalloc(strlen(ProgramName) + 1); /* +1 for '\0' */
83 progLen = ((char *)ProgP) - ((char *)Prog);
84 newProg->code = (Inst *)XtMalloc(progLen);
85 memcpy(newProg->code, Prog, progLen);
86 newProg->localSymList = LocalSymList;
87 - newProg->name = NULL;
88 + strcpy(newProg->name, ProgramName);
89 newProg->refcount = 1;
91 /* Local variables' values are stored on the stack. Here we assign
92 @@ -291,7 +297,7 @@ Program *FinishCreatingProgram(Accumulat
93 for (s = newProg->localSymList; s != NULL; s = s->next)
94 s->value.val.n = fpOffset++;
96 - DISASM(newProg->code, ProgP - Prog);
97 + DISASM(newProg->name, newProg->code, ProgP - Prog);
99 /* restore state */
100 LocalSymList = acc->localSymList;
101 @@ -299,6 +305,7 @@ Program *FinishCreatingProgram(Accumulat
102 ProgP = acc->progP;
103 memcpy(LoopStack, acc->loopStack, sizeof(*LoopStack) * LOOP_STACK_SIZE);
104 LoopStackPtr = acc->loopStackPtr;
105 + ProgramName = acc->name;
107 return newProg;
109 @@ -3588,7 +3595,9 @@ static int errCheck(const char *s)
111 static char *stackDumpStr(DataValue *fp, const char *msg, char **s, int *pLen)
113 - int len;
114 + static const char backtraceMsg[] = "\n\nBacktrace:";
115 + char frameBuf[TYPE_INT_STR_SIZE(int) + 2];
116 + int len, nFrames, frameWidth, thisFrameWidth;
117 const char *op;
118 char *np;
119 DataValue *nfp = fp;
120 @@ -3596,26 +3605,30 @@ static char *stackDumpStr(DataValue *fp,
122 #ifdef DEBUG_STACK
123 const char *dump;
124 - printd("\n\n");
125 + static const char stackdumpMsg[] = "\n\nStack:\n";
126 disasmInternal(PC - 1, 1);
127 stackdumpInternal(0, 50);
128 dump = printd(NULL);
129 #endif
131 /* first measure the lengths */
132 - len = strlen(msg) + 1;
133 + len = strlen(msg) + strlen(backtraceMsg) + 1;
134 nfp = fp;
135 + nFrames = 0;
136 do {
137 + nFrames++;
138 len = len + FP_GET_ITEM(nfp, FP_FUNCTION_NAME).val.str.len + 1;
139 pc = FP_GET_RET_PC(nfp);
140 nfp = FP_GET_OLD_FP(nfp);
141 } while (pc);
142 + frameWidth = lenLongAsStr(nFrames);
143 + len += nFrames * (2 + frameWidth);
144 #ifdef DEBUG_STACK
145 - len += strlen(dump);
146 + len += strlen(stackdumpMsg) + strlen(dump);
147 #endif
148 if (*pLen < len)
150 - *s = *s ? XtRealloc(*s, len) : XtMalloc(len);
151 + *s = XtRealloc(*s, len);
152 *pLen = len;
154 /* now copy */
155 @@ -3623,10 +3636,25 @@ static char *stackDumpStr(DataValue *fp,
156 op = msg;
157 while (*op)
158 *np++ = *op++;
159 + op = backtraceMsg;
160 + while (*op)
161 + *np++ = *op++;
163 nfp = fp;
164 + nFrames = 0;
165 do {
166 + nFrames++;
167 *np++ = '\n';
168 + *np++ = '#';
169 + thisFrameWidth = 0;
170 + op = longAsStr(nFrames);
171 + while (*op) {
172 + thisFrameWidth++;
173 + *np++ = *op++;
175 + while (thisFrameWidth++ < frameWidth)
176 + *np++ = ' ';
177 + *np++ = ' ';
178 op = FP_GET_ITEM(nfp, FP_FUNCTION_NAME).val.str.rep;
179 while (*op)
180 *np++ = *op++;
181 @@ -3634,12 +3662,15 @@ static char *stackDumpStr(DataValue *fp,
182 nfp = FP_GET_OLD_FP(nfp);
183 } while (pc);
184 #ifdef DEBUG_STACK
185 + op = stackdumpMsg;
186 + while (*op)
187 + *np++ = *op++;
188 op = dump;
189 while (*op)
190 *np++ = *op++;
191 #endif
193 - *np = 0;
194 + *np = '\0';
195 return *s;
198 @@ -3785,40 +3816,58 @@ static void dumpVal(DataValue dv)
200 switch (dv.tag) {
201 case INT_TAG:
202 - printd("i=%d", dv.val.n);
203 + printd(" <%s %d>", tagToStr(INT_TAG), dv.val.n);
204 break;
205 case STRING_TAG:
207 - int k;
208 - char s[21];
209 - char *src = dv.val.str.rep;
210 + int k, l;
211 + char s[64];
212 + const char *src = dv.val.str.rep;
213 if (!src) {
214 - printd("s=<NULL>");
215 + printd(" <%s NULL>", tagToStr(STRING_TAG));
217 else {
218 - for (k = 0; src[k] && k < sizeof s - 1; k++) {
219 - s[k] = isprint(src[k]) ? src[k] : '?';
220 + for (k = 0, l = 0; src[k] && l < sizeof s - 1; k++, l++) {
221 + char *e;
222 + const char to[] = "\\\"ntbrfave";
223 +#ifdef EBCDIC_CHARSET
224 + const char from[] = "\\\"\n\t\b\r\f\a\v\x27"; /* EBCDIC escape */
225 +#else
226 + const char from[] = "\\\"\n\t\b\r\f\a\v\x1B"; /* ASCII escape */
227 +#endif
228 + if ((e = strchr(from, src[k]))) {
229 + if (l < sizeof s - 2) {
230 + s[l++] = '\\';
231 + s[l] = to[e - from];
234 + else if (isprint(src[k])) {
235 + s[l] = src[k];
237 + else {
238 + s[l] = '?';
241 - s[k] = 0;
242 - printd("s=\"%s\"%s[%d]", s,
243 - src[k] ? "..." : "", strlen(src));
244 + s[l] = 0;
245 + printd(" <%s %u:\"%s\"%s>", tagToStr(STRING_TAG),
246 + (unsigned)strlen(src), s, src[k] ? "..." : "");
249 break;
250 case ARRAY_TAG:
251 - printd("%08p <%s>[%d]", dv.val.arrayPtr, tagToStr(ARRAY_TAG),
252 - ArraySize(&dv));
253 + printd(" <%s %u:%8p>", tagToStr(ARRAY_TAG), ArraySize(&dv),
254 + dv.val.arrayPtr);
255 break;
256 case NO_TAG:
257 - if (!dv.val.inst) {
258 - printd("<%s>", tagToStr(NO_TAG));
259 + if (!*(void **)&dv.val) {
260 + printd(" <%s>", tagToStr(NO_TAG));
262 else {
263 - printd("?%8p", dv.val.inst);
264 + printd(" ?%8p", *(void **)&dv.val);
266 break;
267 default:
268 - printd("UNKNOWN DATA TAG %d ?%8p", dv.tag, dv.val.inst);
269 + printd(" <unknown value %d:%8p>", dv.tag, *(void **)&dv.val);
270 break;
273 @@ -3833,57 +3882,65 @@ static void disasmInternal(Inst *inst, i
274 #undef OP
276 int i, j;
277 + static size_t opLen;
279 - printd("\n");
280 + if (!opLen) {
281 + for (j = 0; j < N_OPS; ++j) {
282 + if (opLen < strlen(opNames[j])) {
283 + opLen = strlen(opNames[j]);
288 for (i = 0; i < nInstr; ++i) {
289 - printd("Prog %8p ", &inst[i]);
290 + printd("Prog %8p", &inst[i]);
291 for (j = 0; j < N_OPS; ++j) {
292 if (inst[i].func == OpFns[j]) {
293 - printd("%22s ", opNames[j]);
294 + printd(" %*s", (int)opLen, opNames[j]);
295 if (j == OP_PUSH_SYM || j == OP_ASSIGN) {
296 Symbol *sym = inst[i+1].sym;
297 - printd("%s", sym->name);
298 - if (sym->value.tag == STRING_TAG &&
299 - strncmp(sym->name, "string #", 8) == 0) {
300 + printd(" %s", sym->name);
301 + if (sym->type == CONST_SYM
302 + && sym->value.tag == STRING_TAG) {
303 dumpVal(sym->value);
305 ++i;
307 else if (j == OP_PUSH_IMMED) {
308 - printd("i=%d", inst[i+1].value);
309 + printd(" <immediate %d>", inst[i+1].value);
310 ++i;
312 else if (j == OP_BRANCH || j == OP_BRANCH_FALSE ||
313 j == OP_BRANCH_NEVER || j == OP_BRANCH_TRUE) {
314 - printd("to=(%d) %p", inst[i+1].value,
315 + printd(" to=(%+d) %8p", inst[i+1].value,
316 &inst[i+1] + inst[i+1].value);
317 ++i;
319 else if (j == OP_CONCAT) {
320 - printd("nExpr=%d", inst[i+1].value);
321 + printd(" nExpr=%d", inst[i+1].value);
322 ++i;
324 else if (j == OP_SUBR_CALL) {
325 int args = inst[i+2].value;
326 - printd("%s ", inst[i+1].sym->name);
327 + printd(" %s", inst[i+1].sym->name);
328 if (args < 0) {
329 - printd("%d+args[] (%d)", -args - 1, args);
330 + printd(" %d+args[] (%d)", -args - 1, args);
332 else {
333 - printd("%d args", args);
334 + printd(" %d args", args);
336 i += 2;
338 else if (j == OP_SUBR_CALL_STACKED_N) {
339 - printd("%s args[] (?)", inst[i+1].sym->name);
340 + printd(" %s args[] (?)", inst[i+1].sym->name);
341 ++i;
343 else if (j == OP_BEGIN_ARRAY_ITER) {
344 - printd("%s in", inst[i+1].sym->name);
345 + printd(" %s in", inst[i+1].sym->name);
346 ++i;
348 else if (j == OP_ARRAY_ITER) {
349 - printd("%s = %s++ end-loop=(%d) %p",
350 + printd(" %s = %s++ end-loop=(%+d) %8p",
351 inst[i+1].sym->name,
352 inst[i+2].sym->name,
353 inst[i+3].value, &inst[i+3] + inst[i+3].value);
354 @@ -3895,18 +3952,20 @@ static void disasmInternal(Inst *inst, i
355 j == OP_ANONARRAY_INDEX_VAL ||
356 j == OP_NAMED_ARG1 ||
357 j == OP_NAMED_ARGN) {
358 - printd("nDim=%d", inst[i+1].value);
359 + printd(" nDim=%d", inst[i+1].value);
360 ++i;
362 else if (j == OP_ARRAY_REF_ASSIGN_SETUP) {
363 - printd("binOp=%s ", inst[i+1].value ? "true" : "false");
364 - printd("nDim=%d", inst[i+2].value);
365 + printd(" binOp=%s nDim=%d",
366 + inst[i+1].value ? "true" : "false",
367 + inst[i+2].value);
368 i += 2;
370 else if (j == OP_PUSH_ARRAY_SYM) {
371 - printd("%s", inst[++i].sym->name);
372 - printd(" %s", inst[i+1].value ? "createAndRef" : "refOnly");
373 - ++i;
374 + printd(" %s %s",
375 + inst[i+1].sym->name,
376 + inst[i+2].value ? "createAndRef" : "refOnly");
377 + i += 2;
380 printd("\n");
381 @@ -3914,18 +3973,20 @@ static void disasmInternal(Inst *inst, i
384 if (j == N_OPS) {
385 - printd("%x\n", inst[i].value);
386 + printd(" %x\n", inst[i].value);
391 -static void disasm(Inst *inst, int nInstr)
392 +static void disasm(const char *name, Inst *inst, int nInstr)
394 static int outIsTTY = -1;
395 if (outIsTTY == -1) outIsTTY = isatty(fileno(stdout));
396 if (outIsTTY) { printd("\033[H"); }
397 + if (name) printd(">> %s\n", name);
398 disasmInternal(inst, nInstr);
399 if (outIsTTY) { printd("\033[J\n"); }
400 + if (name) printd("\n");
401 outPrintd();
403 #endif /* #ifdef DEBUG_DISASSEMBLER */
404 @@ -3976,7 +4037,7 @@ static void stackdumpframe(DataValue *ar
405 for (dv = endDv; dv < sp; dv++)
406 #endif /* #ifdef DEBUG_STACK_HEADFIRST */
408 - const char *posFmt = "%-6s ";
409 + const char *posFmt = "%-6s";
410 const char *symName = "";
412 char *pos = "";
413 @@ -3985,8 +4046,7 @@ static void stackdumpframe(DataValue *ar
414 const char *leadIn = (dv >= arrow) ? ">>>>" :
415 (dv == arg1) ? "----" :
416 (dv == fnNm) ? "====" : "";
417 - printd("%4.4s", leadIn);
418 - printd("%8p%c", dv, topMark);
419 + printd("%4.4s%8p%c", leadIn, dv, topMark);
420 switch (offset) {
421 case FP_ARG_COUNT_INDEX: pos = "NArgs"; break; /* num. arguments */
422 case FP_FUNCTION_NAME: pos = "FnName"; break;
423 @@ -4004,7 +4064,7 @@ static void stackdumpframe(DataValue *ar
425 else if (0 <= offset && offset < nSyms) {
426 sprintf(pos = buffer, offset ? "[%d]" : "FP[%d]", offset);
427 - posFmt = "%6s ";
428 + posFmt = "%6s";
430 else if (offset == 0) {
431 pos = "FrameP";
432 @@ -4022,10 +4082,10 @@ static void stackdumpframe(DataValue *ar
436 - printd("%-*.*s ", symLen, symLen, symName);
437 + printd(" %-*.*s", symLen, symLen, symName);
439 if (dv == fnNm && dv->tag == STRING_TAG && dv->val.str.rep)
440 - printd("%s", dv->val.str.rep);
441 + printd(" %s", dv->val.str.rep);
442 else
443 dumpVal(*dv);
445 @@ -4054,7 +4114,7 @@ static void stackdumpInternal(int n, int
446 if (outpt < TheStack)
447 printd("--------------Stack base--------------\n");
448 stackdumpframe(arrow, outpt, FrameP, StackP, '*');
449 - printd("Stack ----->\n");
450 + printd("Stack ----->\n\n");
451 #endif /* #ifdef DEBUG_STACK_HEADFIRST */
454 @@ -4064,7 +4124,11 @@ static void stackdump(int n, int extra)
455 if (outIsTTY == -1)
456 outIsTTY = isatty(fileno(stdout));
458 - stackdumpInternal(n, extra);
459 +#ifndef DEBUG_STACKDUMP_EXTRA
460 +#define DEBUG_STACKDUMP_EXTRA 0
461 +#endif
462 + stackdumpInternal(n, extra + DEBUG_STACKDUMP_EXTRA);
463 +#undef DEBUG_STACKDUMP_EXTRA
465 if (outIsTTY)
466 printd("\033[J\n");
467 diff --quilt old/source/interpret.h new/source/interpret.h
468 --- old/source/interpret.h
469 +++ new/source/interpret.h
470 @@ -105,10 +105,10 @@ typedef struct SymbolRec {
471 } Symbol;
473 typedef struct ProgramTag {
474 + char *name;
475 Symbol *localSymList;
476 Inst *code;
477 unsigned refcount;
478 - char *name;
479 } Program;
481 /* Information needed to re-start a preempted macro */
482 @@ -128,6 +128,7 @@ typedef struct AccumulatorDataTag {
483 Inst *progP;
484 Inst *loopStack[LOOP_STACK_SIZE];
485 Inst **loopStackPtr;
486 + const char *name;
487 } AccumulatorData;
489 void InitMacroGlobals(void);
490 @@ -144,7 +145,7 @@ int ArrayCopy(DataValue *dstArray, DataV
492 /* Routines for creating a program, (accumulated beginning with
493 BeginCreatingProgram and returned via FinishCreatingProgram) */
494 -void BeginCreatingProgram(AccumulatorData *acc);
495 +void BeginCreatingProgram(const char *name, AccumulatorData *acc);
496 int AddOp(int op, char **msg);
497 int AddSym(Symbol *sym, char **msg);
498 int AddImmediate(int value, char **msg);
499 diff --quilt old/source/parse.y new/source/parse.y
500 --- old/source/parse.y
501 +++ new/source/parse.y
502 @@ -196,7 +196,7 @@ definesym: SYMBOL {
503 yyerror("try to override built-in subroutine"); YYERROR;
505 $$.sym = PromoteToGlobal($1);
506 - BeginCreatingProgram($$.acc);
507 + BeginCreatingProgram($$.sym->name, $$.acc);
510 define: definekw blank definesym blank blockwb {
511 @@ -745,14 +745,16 @@ Program *ParseMacro(char *expr, char **m
513 Program *prog;
514 AccumulatorData *acc = XtNew(AccumulatorData);
515 - static const char *prefix = ">> ";
517 #if YYDEBUG
518 int oldyydebug = yydebug;
519 yydebug = 1;
520 #endif
522 - BeginCreatingProgram(acc);
523 + if (!name)
524 + name = "--unknown--";
526 + BeginCreatingProgram(name, acc);
528 /* whether we allow the "define" keyword */
529 AllowDefine = allowDefine;
530 @@ -780,12 +782,6 @@ Program *ParseMacro(char *expr, char **m
531 prog = FinishCreatingProgram(acc);
532 XtFree((char *)acc);
534 - if (!name)
535 - name = "--unknown--";
537 - prog->name = XtMalloc(strlen(name) + strlen(prefix) + 1);
538 - strcat(strcpy(prog->name, prefix), name);
540 /* parse succeeded */
541 *msg = "";
542 *stoppedAt = InPtr;
543 diff --quilt old/source/userCmds.c new/source/userCmds.c
544 --- old/source/userCmds.c
545 +++ new/source/userCmds.c
546 @@ -1286,13 +1286,13 @@ static int doMacroMenuCmd(WindowInfo *wi
547 int DoNamedMacroMenuCmd(WindowInfo *window, const char *itemName)
549 return doMacroMenuCmd(window, itemName, MacroMenuItems, NMacroMenuItems,
550 - "macro-menu>");
551 + "Macro Menu>");
554 int DoNamedBGMenuCmd(WindowInfo *window, const char *itemName)
556 return doMacroMenuCmd(window, itemName, BGMenuItems, NBGMenuItems,
557 - "background-menu>");
558 + "Background Menu>");
562 diff --quilt old/makefiles/Makefile.bertw new/makefiles/Makefile.bertw
563 --- old/makefiles/Makefile.bertw
564 +++ new/makefiles/Makefile.bertw
565 @@ -23,6 +23,9 @@ ifdef DEBUG
566 ifdef DEBUG_MACRO
567 ifndef DEBUG_NO_STACKDUMP
568 CFLAGS += -DDEBUG_STACK
569 + ifdef DEBUG_STACKDUMP_EXTRA
570 + CFLAGS += -DDEBUG_STACKDUMP_EXTRA=$(DEBUG_STACKDUMP_EXTRA)
571 + endif
572 endif
573 ifndef DEBUG_NO_DISASM
574 CFLAGS += -DDEBUG_ASSEMBLY