import macro_name.patch from refcount
[nedit-bw.git] / InterpretDebug-mods.patch
blob2ed7a8fd155708a8cf26897a2badaced5505ae69
1 Subject: modifications to the InterpretDebug patch
3 ---
5 makefiles/Makefile.bertw | 3
6 source/interpret.c | 189 +++++++++++++++++++++++++++++++----------------
7 source/interpret.h | 5 -
8 source/parse.y | 14 +--
9 source/userCmds.c | 4
10 5 files changed, 140 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,59 @@ 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 + unsigned len = dv.val.str.len;
214 if (!src) {
215 - printd("s=<NULL>");
216 + printd(" <%s NULL>", tagToStr(STRING_TAG));
218 else {
219 - for (k = 0; src[k] && k < sizeof s - 1; k++) {
220 - s[k] = isprint(src[k]) ? src[k] : '?';
221 + for (k = 0, l = 0; src[k] && l < sizeof s - 1; k++, l++) {
222 + char *e;
223 + const char to[] = "\\\"ntbrfave";
224 +#ifdef EBCDIC_CHARSET
225 + const char from[] = "\\\"\n\t\b\r\f\a\v\x27"; /* EBCDIC escape */
226 +#else
227 + const char from[] = "\\\"\n\t\b\r\f\a\v\x1B"; /* ASCII escape */
228 +#endif
229 + if ((e = strchr(from, src[k]))) {
230 + if (l < sizeof s - 2) {
231 + s[l++] = '\\';
232 + s[l] = to[e - from];
235 + else if (isprint(src[k])) {
236 + s[l] = src[k];
238 + else {
239 + s[l] = '?';
242 - s[k] = 0;
243 - printd("s=\"%s\"%s[%d]", s,
244 - src[k] ? "..." : "", strlen(src));
245 + s[l] = 0;
246 + printd(" <%s %u:\"%s\"%s>", tagToStr(STRING_TAG),
247 + len, s, src[k] ? "..." : "");
250 break;
251 case ARRAY_TAG:
252 - printd("%08p <%s>[%d]", dv.val.arrayPtr, tagToStr(ARRAY_TAG),
253 - ArraySize(&dv));
254 + printd(" <%s %u:%8p>", tagToStr(ARRAY_TAG), ArraySize(&dv),
255 + dv.val.arrayPtr);
256 break;
257 case NO_TAG:
258 - if (!dv.val.inst) {
259 - printd("<%s>", tagToStr(NO_TAG));
260 + if (!*(void **)&dv.val) {
261 + printd(" <%s>", tagToStr(NO_TAG));
263 else {
264 - printd("?%8p", dv.val.inst);
265 + printd(" ?%8p", *(void **)&dv.val);
267 break;
268 default:
269 - printd("UNKNOWN DATA TAG %d ?%8p", dv.tag, dv.val.inst);
270 + printd(" <unknown value %d:%8p>", dv.tag, *(void **)&dv.val);
271 break;
274 @@ -3833,57 +3883,65 @@ static void disasmInternal(Inst *inst, i
275 #undef OP
277 int i, j;
278 + static size_t opLen;
280 - printd("\n");
281 + if (!opLen) {
282 + for (j = 0; j < N_OPS; ++j) {
283 + if (opLen < strlen(opNames[j])) {
284 + opLen = strlen(opNames[j]);
289 for (i = 0; i < nInstr; ++i) {
290 - printd("Prog %8p ", &inst[i]);
291 + printd("Prog %8p", &inst[i]);
292 for (j = 0; j < N_OPS; ++j) {
293 if (inst[i].func == OpFns[j]) {
294 - printd("%22s ", opNames[j]);
295 + printd(" %*s", (int)opLen, opNames[j]);
296 if (j == OP_PUSH_SYM || j == OP_ASSIGN) {
297 Symbol *sym = inst[i+1].sym;
298 - printd("%s", sym->name);
299 - if (sym->value.tag == STRING_TAG &&
300 - strncmp(sym->name, "string #", 8) == 0) {
301 + printd(" %s", sym->name);
302 + if (sym->type == CONST_SYM
303 + && sym->value.tag == STRING_TAG) {
304 dumpVal(sym->value);
306 ++i;
308 else if (j == OP_PUSH_IMMED) {
309 - printd("i=%d", inst[i+1].value);
310 + printd(" <immediate %d>", inst[i+1].value);
311 ++i;
313 else if (j == OP_BRANCH || j == OP_BRANCH_FALSE ||
314 j == OP_BRANCH_NEVER || j == OP_BRANCH_TRUE) {
315 - printd("to=(%d) %p", inst[i+1].value,
316 + printd(" to=(%+d) %8p", inst[i+1].value,
317 &inst[i+1] + inst[i+1].value);
318 ++i;
320 else if (j == OP_CONCAT) {
321 - printd("nExpr=%d", inst[i+1].value);
322 + printd(" nExpr=%d", inst[i+1].value);
323 ++i;
325 else if (j == OP_SUBR_CALL) {
326 int args = inst[i+2].value;
327 - printd("%s ", inst[i+1].sym->name);
328 + printd(" %s", inst[i+1].sym->name);
329 if (args < 0) {
330 - printd("%d+args[] (%d)", -args - 1, args);
331 + printd(" %d+args[] (%d)", -args - 1, args);
333 else {
334 - printd("%d args", args);
335 + printd(" %d args", args);
337 i += 2;
339 else if (j == OP_SUBR_CALL_STACKED_N) {
340 - printd("%s args[] (?)", inst[i+1].sym->name);
341 + printd(" %s args[] (?)", inst[i+1].sym->name);
342 ++i;
344 else if (j == OP_BEGIN_ARRAY_ITER) {
345 - printd("%s in", inst[i+1].sym->name);
346 + printd(" %s in", inst[i+1].sym->name);
347 ++i;
349 else if (j == OP_ARRAY_ITER) {
350 - printd("%s = %s++ end-loop=(%d) %p",
351 + printd(" %s = %s++ end-loop=(%+d) %8p",
352 inst[i+1].sym->name,
353 inst[i+2].sym->name,
354 inst[i+3].value, &inst[i+3] + inst[i+3].value);
355 @@ -3895,18 +3953,20 @@ static void disasmInternal(Inst *inst, i
356 j == OP_ANONARRAY_INDEX_VAL ||
357 j == OP_NAMED_ARG1 ||
358 j == OP_NAMED_ARGN) {
359 - printd("nDim=%d", inst[i+1].value);
360 + printd(" nDim=%d", inst[i+1].value);
361 ++i;
363 else if (j == OP_ARRAY_REF_ASSIGN_SETUP) {
364 - printd("binOp=%s ", inst[i+1].value ? "true" : "false");
365 - printd("nDim=%d", inst[i+2].value);
366 + printd(" binOp=%s nDim=%d",
367 + inst[i+1].value ? "true" : "false",
368 + inst[i+2].value);
369 i += 2;
371 else if (j == OP_PUSH_ARRAY_SYM) {
372 - printd("%s", inst[++i].sym->name);
373 - printd(" %s", inst[i+1].value ? "createAndRef" : "refOnly");
374 - ++i;
375 + printd(" %s %s",
376 + inst[i+1].sym->name,
377 + inst[i+2].value ? "createAndRef" : "refOnly");
378 + i += 2;
381 printd("\n");
382 @@ -3914,18 +3974,20 @@ static void disasmInternal(Inst *inst, i
385 if (j == N_OPS) {
386 - printd("%x\n", inst[i].value);
387 + printd(" %x\n", inst[i].value);
392 -static void disasm(Inst *inst, int nInstr)
393 +static void disasm(const char *name, Inst *inst, int nInstr)
395 static int outIsTTY = -1;
396 if (outIsTTY == -1) outIsTTY = isatty(fileno(stdout));
397 if (outIsTTY) { printd("\033[H"); }
398 + if (name) printd(">> %s\n", name);
399 disasmInternal(inst, nInstr);
400 if (outIsTTY) { printd("\033[J\n"); }
401 + if (name) printd("\n");
402 outPrintd();
404 #endif /* #ifdef DEBUG_DISASSEMBLER */
405 @@ -3976,7 +4038,7 @@ static void stackdumpframe(DataValue *ar
406 for (dv = endDv; dv < sp; dv++)
407 #endif /* #ifdef DEBUG_STACK_HEADFIRST */
409 - const char *posFmt = "%-6s ";
410 + const char *posFmt = "%-6s";
411 const char *symName = "";
413 char *pos = "";
414 @@ -3985,8 +4047,7 @@ static void stackdumpframe(DataValue *ar
415 const char *leadIn = (dv >= arrow) ? ">>>>" :
416 (dv == arg1) ? "----" :
417 (dv == fnNm) ? "====" : "";
418 - printd("%4.4s", leadIn);
419 - printd("%8p%c", dv, topMark);
420 + printd("%4.4s%8p%c", leadIn, dv, topMark);
421 switch (offset) {
422 case FP_ARG_COUNT_INDEX: pos = "NArgs"; break; /* num. arguments */
423 case FP_FUNCTION_NAME: pos = "FnName"; break;
424 @@ -4004,7 +4065,7 @@ static void stackdumpframe(DataValue *ar
426 else if (0 <= offset && offset < nSyms) {
427 sprintf(pos = buffer, offset ? "[%d]" : "FP[%d]", offset);
428 - posFmt = "%6s ";
429 + posFmt = "%6s";
431 else if (offset == 0) {
432 pos = "FrameP";
433 @@ -4022,10 +4083,10 @@ static void stackdumpframe(DataValue *ar
437 - printd("%-*.*s ", symLen, symLen, symName);
438 + printd(" %-*.*s", symLen, symLen, symName);
440 if (dv == fnNm && dv->tag == STRING_TAG && dv->val.str.rep)
441 - printd("%s", dv->val.str.rep);
442 + printd(" %s", dv->val.str.rep);
443 else
444 dumpVal(*dv);
446 @@ -4054,7 +4115,7 @@ static void stackdumpInternal(int n, int
447 if (outpt < TheStack)
448 printd("--------------Stack base--------------\n");
449 stackdumpframe(arrow, outpt, FrameP, StackP, '*');
450 - printd("Stack ----->\n");
451 + printd("Stack ----->\n\n");
452 #endif /* #ifdef DEBUG_STACK_HEADFIRST */
455 @@ -4064,7 +4125,11 @@ static void stackdump(int n, int extra)
456 if (outIsTTY == -1)
457 outIsTTY = isatty(fileno(stdout));
459 - stackdumpInternal(n, extra);
460 +#ifndef DEBUG_STACKDUMP_EXTRA
461 +#define DEBUG_STACKDUMP_EXTRA 0
462 +#endif
463 + stackdumpInternal(n, extra + DEBUG_STACKDUMP_EXTRA);
464 +#undef DEBUG_STACKDUMP_EXTRA
466 if (outIsTTY)
467 printd("\033[J\n");
468 diff --quilt old/source/interpret.h new/source/interpret.h
469 --- old/source/interpret.h
470 +++ new/source/interpret.h
471 @@ -105,10 +105,10 @@ typedef struct SymbolRec {
472 } Symbol;
474 typedef struct ProgramTag {
475 + char *name;
476 Symbol *localSymList;
477 Inst *code;
478 unsigned refcount;
479 - char *name;
480 } Program;
482 /* Information needed to re-start a preempted macro */
483 @@ -128,6 +128,7 @@ typedef struct AccumulatorDataTag {
484 Inst *progP;
485 Inst *loopStack[LOOP_STACK_SIZE];
486 Inst **loopStackPtr;
487 + const char *name;
488 } AccumulatorData;
490 void InitMacroGlobals(void);
491 @@ -144,7 +145,7 @@ int ArrayCopy(DataValue *dstArray, DataV
493 /* Routines for creating a program, (accumulated beginning with
494 BeginCreatingProgram and returned via FinishCreatingProgram) */
495 -void BeginCreatingProgram(AccumulatorData *acc);
496 +void BeginCreatingProgram(const char *name, AccumulatorData *acc);
497 int AddOp(int op, char **msg);
498 int AddSym(Symbol *sym, char **msg);
499 int AddImmediate(int value, char **msg);
500 diff --quilt old/source/parse.y new/source/parse.y
501 --- old/source/parse.y
502 +++ new/source/parse.y
503 @@ -196,7 +196,7 @@ definesym: SYMBOL {
504 yyerror("try to override built-in subroutine"); YYERROR;
506 $$.sym = PromoteToGlobal($1);
507 - BeginCreatingProgram($$.acc);
508 + BeginCreatingProgram($$.sym->name, $$.acc);
511 define: definekw blank definesym blank blockwb {
512 @@ -745,14 +745,16 @@ Program *ParseMacro(char *expr, char **m
514 Program *prog;
515 AccumulatorData *acc = XtNew(AccumulatorData);
516 - static const char *prefix = ">> ";
518 #if YYDEBUG
519 int oldyydebug = yydebug;
520 yydebug = 1;
521 #endif
523 - BeginCreatingProgram(acc);
524 + if (!name)
525 + name = "--unknown--";
527 + BeginCreatingProgram(name, acc);
529 /* whether we allow the "define" keyword */
530 AllowDefine = allowDefine;
531 @@ -780,12 +782,6 @@ Program *ParseMacro(char *expr, char **m
532 prog = FinishCreatingProgram(acc);
533 XtFree((char *)acc);
535 - if (!name)
536 - name = "--unknown--";
538 - prog->name = XtMalloc(strlen(name) + strlen(prefix) + 1);
539 - strcat(strcpy(prog->name, prefix), name);
541 /* parse succeeded */
542 *msg = "";
543 *stoppedAt = InPtr;
544 diff --quilt old/source/userCmds.c new/source/userCmds.c
545 --- old/source/userCmds.c
546 +++ new/source/userCmds.c
547 @@ -1286,13 +1286,13 @@ static int doMacroMenuCmd(WindowInfo *wi
548 int DoNamedMacroMenuCmd(WindowInfo *window, const char *itemName)
550 return doMacroMenuCmd(window, itemName, MacroMenuItems, NMacroMenuItems,
551 - "macro-menu>");
552 + "Macro Menu>");
555 int DoNamedBGMenuCmd(WindowInfo *window, const char *itemName)
557 return doMacroMenuCmd(window, itemName, BGMenuItems, NBGMenuItems,
558 - "background-menu>");
559 + "Background Menu>");
563 diff --quilt old/makefiles/Makefile.bertw new/makefiles/Makefile.bertw
564 --- old/makefiles/Makefile.bertw
565 +++ new/makefiles/Makefile.bertw
566 @@ -23,6 +23,9 @@ ifdef DEBUG
567 ifdef DEBUG_MACRO
568 ifndef DEBUG_NO_STACKDUMP
569 CFLAGS += -DDEBUG_STACK
570 + ifdef DEBUG_STACKDUMP_EXTRA
571 + CFLAGS += -DDEBUG_STACKDUMP_EXTRA=$(DEBUG_STACKDUMP_EXTRA)
572 + endif
573 endif
574 ifndef DEBUG_NO_DISASM
575 CFLAGS += -DDEBUG_ASSEMBLY