1 Subject: modifications to the InterpretDebug patch
5 makefiles/Makefile.bertw | 3
6 source/interpret.c | 249 ++++++++++++++++++++++++++++++++---------------
10 5 files changed, 184 insertions(+), 91 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 int outPrintd();
21 -static void disasm(Inst *inst, int nInstr);
22 +static const char *printd(const char *f, ...)
23 +__attribute__((__format__(printf,1,2)));
24 +static int outPrintd(void);
25 +static void disasm(const char *name, Inst *inst, int nInstr);
26 static void disasmInternal(Inst *inst, int nInstr);
27 #endif /* #if defined(DEBUG_ASSEMBLY) || defined(DEBUG_STACK) */
29 #ifdef DEBUG_ASSEMBLY /* for disassembly */
30 -#define DISASM(i, n) disasm(i, n)
31 +#define DISASM(name, i, n) disasm(name, i, n)
32 #else /* #ifndef DEBUG_ASSEMBLY */
34 +#define DISASM(name, i, n)
35 #endif /* #ifndef DEBUG_ASSEMBLY */
37 #ifdef DEBUG_STACK /* for run-time instruction and stack trace */
38 static void stackdump(int n, int extra);
39 static void stackdumpInternal(int n, int extra);
40 #define STACKDUMP(n, x) stackdump(n, x)
41 -#define DISASM_RT(i, n) disasm(i, n)
42 +#define DISASM_RT(i, n) disasm(NULL, i, n)
43 #else /* #ifndef DEBUG_STACK */
44 #define STACKDUMP(n, x)
45 #define DISASM_RT(i, n)
46 @@ -161,6 +162,8 @@ static Inst *ProgP; /* next free spot
47 static Inst *LoopStack[LOOP_STACK_SIZE]; /* addresses of break, cont stmts */
48 static Inst **LoopStackPtr = LoopStack; /* to fill at the end of a loop */
50 +static const char *ProgramName = "";
52 /* Global data for the interpreter */
53 static DataValue *TheStack; /* the stack */
54 static DataValue *StackP; /* next free spot on stack */
55 @@ -253,7 +256,7 @@ void InitMacroGlobals(void)
56 ** Start collecting instructions for a program. Clears the program
57 ** and the symbol table.
59 -void BeginCreatingProgram(AccumulatorData *acc)
60 +void BeginCreatingProgram(const char *name, AccumulatorData *acc)
63 acc->localSymList = LocalSymList;
64 @@ -261,10 +264,12 @@ void BeginCreatingProgram(AccumulatorDat
66 memcpy(acc->loopStack, LoopStack, sizeof(*LoopStack) * LOOP_STACK_SIZE);
67 acc->loopStackPtr = LoopStackPtr;
68 + acc->name = ProgramName;
72 LoopStackPtr = LoopStack;
77 @@ -278,12 +283,13 @@ Program *FinishCreatingProgram(Accumulat
78 int progLen, fpOffset = 0;
81 - newProg = (Program *)XtMalloc(sizeof(Program));
82 + newProg = XtNew(Program);
83 + newProg->name = XtMalloc(strlen(ProgramName) + 1); /* +1 for '\0' */
84 progLen = ((char *)ProgP) - ((char *)Prog);
85 newProg->code = (Inst *)XtMalloc(progLen);
86 memcpy(newProg->code, Prog, progLen);
87 newProg->localSymList = LocalSymList;
88 - newProg->name = NULL;
89 + strcpy(newProg->name, ProgramName);
90 newProg->refcount = 1;
92 /* Local variables' values are stored on the stack. Here we assign
93 @@ -291,7 +297,7 @@ Program *FinishCreatingProgram(Accumulat
94 for (s = newProg->localSymList; s != NULL; s = s->next)
95 s->value.val.n = fpOffset++;
97 - DISASM(newProg->code, ProgP - Prog);
98 + DISASM(newProg->name, newProg->code, ProgP - Prog);
101 LocalSymList = acc->localSymList;
102 @@ -299,6 +305,7 @@ Program *FinishCreatingProgram(Accumulat
104 memcpy(LoopStack, acc->loopStack, sizeof(*LoopStack) * LOOP_STACK_SIZE);
105 LoopStackPtr = acc->loopStackPtr;
106 + ProgramName = acc->name;
110 @@ -3588,7 +3595,9 @@ static int errCheck(const char *s)
112 static char *stackDumpStr(DataValue *fp, const char *msg, char **s, int *pLen)
115 + static const char backtraceMsg[] = "\n\nBacktrace:";
116 + char frameBuf[TYPE_INT_STR_SIZE(int) + 2];
117 + int len, nFrames, frameWidth, thisFrameWidth;
121 @@ -3596,26 +3605,30 @@ static char *stackDumpStr(DataValue *fp,
126 + static const char stackdumpMsg[] = "\n\nStack:\n";
127 disasmInternal(PC - 1, 1);
128 stackdumpInternal(0, 50);
132 /* first measure the lengths */
133 - len = strlen(msg) + 1;
134 + len = strlen(msg) + strlen(backtraceMsg) + 1;
139 len = len + FP_GET_ITEM(nfp, FP_FUNCTION_NAME).val.str.len + 1;
140 pc = FP_GET_RET_PC(nfp);
141 nfp = FP_GET_OLD_FP(nfp);
143 + frameWidth = lenLongAsStr(nFrames);
144 + len += nFrames * (2 + frameWidth);
146 - len += strlen(dump);
147 + len += strlen(stackdumpMsg) + strlen(dump);
151 - *s = *s ? XtRealloc(*s, len) : XtMalloc(len);
152 + *s = XtRealloc(*s, len);
156 @@ -3623,10 +3636,25 @@ static char *stackDumpStr(DataValue *fp,
170 + thisFrameWidth = 0;
171 + op = longAsStr(nFrames);
176 + while (thisFrameWidth++ < frameWidth)
179 op = FP_GET_ITEM(nfp, FP_FUNCTION_NAME).val.str.rep;
182 @@ -3634,12 +3662,15 @@ static char *stackDumpStr(DataValue *fp,
183 nfp = FP_GET_OLD_FP(nfp);
199 @@ -3754,27 +3785,27 @@ static const char *printd(const char *f,
206 const char *s = printd(NULL);
209 static int outIsTTY = -1;
211 - outIsTTY = isatty(fileno(stdout));
212 + outIsTTY = isatty(fileno(stderr));
216 for (cp = s; *cp; cp++)
218 - printf("\033[K\n");
219 + fprintf(stderr, "\033[K\n");
222 + fputc(*cp, stderr);
226 for (cp = s; *cp; cp++)
228 + fputc(*cp, stderr);
232 @@ -3785,40 +3816,59 @@ static void dumpVal(DataValue dv)
236 - printd("i=%d", dv.val.n);
237 + printd(" <%s %d>", tagToStr(INT_TAG), dv.val.n);
243 - char *src = dv.val.str.rep;
246 + const char *src = dv.val.str.rep;
247 + unsigned len = dv.val.str.len;
249 - printd("s=<NULL>");
250 + printd(" <%s NULL>", tagToStr(STRING_TAG));
253 - for (k = 0; src[k] && k < sizeof s - 1; k++) {
254 - s[k] = isprint(src[k]) ? src[k] : '?';
255 + for (k = 0, l = 0; src[k] && l < sizeof s - 1; k++, l++) {
257 + const char to[] = "\\\"ntbrfave";
258 +#ifdef EBCDIC_CHARSET
259 + const char from[] = "\\\"\n\t\b\r\f\a\v\x27"; /* EBCDIC escape */
261 + const char from[] = "\\\"\n\t\b\r\f\a\v\x1B"; /* ASCII escape */
263 + if ((e = strchr(from, src[k]))) {
264 + if (l < sizeof s - 2) {
266 + s[l] = to[e - from];
269 + else if (isprint(src[k])) {
277 - printd("s=\"%s\"%s[%d]", s,
278 - src[k] ? "..." : "", strlen(src));
280 + printd(" <%s %u:\"%s\"%s>", tagToStr(STRING_TAG),
281 + len, s, src[k] ? "..." : "");
286 - printd("%08p <%s>[%d]", dv.val.arrayPtr, tagToStr(ARRAY_TAG),
288 + printd(" <%s %u:%p>", tagToStr(ARRAY_TAG), ArraySize(&dv),
292 - if (!dv.val.inst) {
293 - printd("<%s>", tagToStr(NO_TAG));
294 + if (!*(void **)&dv.val) {
295 + printd(" <%s>", tagToStr(NO_TAG));
298 - printd("?%8p", dv.val.inst);
299 + printd(" ?%p", *(void **)&dv.val);
303 - printd("UNKNOWN DATA TAG %d ?%8p", dv.tag, dv.val.inst);
304 + printd(" <unknown value %d:%p>", dv.tag, *(void **)&dv.val);
308 @@ -3833,57 +3883,65 @@ static void disasmInternal(Inst *inst, i
312 + static size_t opLen;
316 + for (j = 0; j < N_OPS; ++j) {
317 + if (opLen < strlen(opNames[j])) {
318 + opLen = strlen(opNames[j]);
323 for (i = 0; i < nInstr; ++i) {
324 - printd("Prog %8p ", &inst[i]);
325 + printd("Prog %10p", &inst[i]);
326 for (j = 0; j < N_OPS; ++j) {
327 if (inst[i].func == OpFns[j]) {
328 - printd("%22s ", opNames[j]);
329 + printd(" %*s", (int)opLen, opNames[j]);
330 if (j == OP_PUSH_SYM || j == OP_ASSIGN) {
331 Symbol *sym = inst[i+1].sym;
332 - printd("%s", sym->name);
333 - if (sym->value.tag == STRING_TAG &&
334 - strncmp(sym->name, "string #", 8) == 0) {
335 + printd(" %s", sym->name);
336 + if (sym->type == CONST_SYM
337 + && sym->value.tag == STRING_TAG) {
342 else if (j == OP_PUSH_IMMED) {
343 - printd("i=%d", inst[i+1].value);
344 + printd(" <immediate %d>", inst[i+1].value);
347 else if (j == OP_BRANCH || j == OP_BRANCH_FALSE ||
348 j == OP_BRANCH_NEVER || j == OP_BRANCH_TRUE) {
349 - printd("to=(%d) %p", inst[i+1].value,
350 + printd(" to=(%+d) %p", inst[i+1].value,
351 &inst[i+1] + inst[i+1].value);
354 else if (j == OP_CONCAT) {
355 - printd("nExpr=%d", inst[i+1].value);
356 + printd(" nExpr=%d", inst[i+1].value);
359 else if (j == OP_SUBR_CALL) {
360 int args = inst[i+2].value;
361 - printd("%s ", inst[i+1].sym->name);
362 + printd(" %s", inst[i+1].sym->name);
364 - printd("%d+args[] (%d)", -args - 1, args);
365 + printd(" %d+args[] (%d)", -args - 1, args);
368 - printd("%d args", args);
369 + printd(" %d args", args);
373 else if (j == OP_SUBR_CALL_STACKED_N) {
374 - printd("%s args[] (?)", inst[i+1].sym->name);
375 + printd(" %s args[] (?)", inst[i+1].sym->name);
378 else if (j == OP_BEGIN_ARRAY_ITER) {
379 - printd("%s in", inst[i+1].sym->name);
380 + printd(" %s in", inst[i+1].sym->name);
383 else if (j == OP_ARRAY_ITER) {
384 - printd("%s = %s++ end-loop=(%d) %p",
385 + printd(" %s = %s++ end-loop=(%+d) %p",
388 inst[i+3].value, &inst[i+3] + inst[i+3].value);
389 @@ -3895,18 +3953,20 @@ static void disasmInternal(Inst *inst, i
390 j == OP_ANONARRAY_INDEX_VAL ||
391 j == OP_NAMED_ARG1 ||
392 j == OP_NAMED_ARGN) {
393 - printd("nDim=%d", inst[i+1].value);
394 + printd(" nDim=%d", inst[i+1].value);
397 else if (j == OP_ARRAY_REF_ASSIGN_SETUP) {
398 - printd("binOp=%s ", inst[i+1].value ? "true" : "false");
399 - printd("nDim=%d", inst[i+2].value);
400 + printd(" binOp=%s nDim=%d",
401 + inst[i+1].value ? "true" : "false",
405 else if (j == OP_PUSH_ARRAY_SYM) {
406 - printd("%s", inst[++i].sym->name);
407 - printd(" %s", inst[i+1].value ? "createAndRef" : "refOnly");
410 + inst[i+1].sym->name,
411 + inst[i+2].value ? "createAndRef" : "refOnly");
416 @@ -3914,18 +3974,20 @@ static void disasmInternal(Inst *inst, i
420 - printd("%x\n", inst[i].value);
421 + printd(" %x\n", inst[i].value);
426 -static void disasm(Inst *inst, int nInstr)
427 +static void disasm(const char *name, Inst *inst, int nInstr)
429 static int outIsTTY = -1;
430 - if (outIsTTY == -1) outIsTTY = isatty(fileno(stdout));
431 + if (outIsTTY == -1) outIsTTY = isatty(fileno(stderr));
432 if (outIsTTY) { printd("\033[H"); }
433 + if (name) printd(">> %s\n", name);
434 disasmInternal(inst, nInstr);
435 if (outIsTTY) { printd("\033[J\n"); }
436 + if (name) printd("\n");
439 #endif /* #ifdef DEBUG_DISASSEMBLER */
440 @@ -3938,7 +4000,11 @@ static void stackdumpframe(DataValue *ar
441 Inst *retPC = FP_GET_RET_PC(fp);
442 DataValue *oldFP = retPC ? FP_GET_OLD_FP(fp) : NULL;
443 DataValue *arg1 = &FP_GET_ARG_N(fp, 0);
444 + DataValue *argc = &FP_GET_ITEM(fp, FP_ARG_COUNT_INDEX);
445 DataValue *fnNm = &FP_GET_ITEM(fp, FP_FUNCTION_NAME);
446 + DataValue *prFP = &FP_GET_ITEM(fp, FP_PROG_INDEX);
447 + DataValue *ofFP = &FP_GET_ITEM(fp, FP_OLD_FP_INDEX);
448 + DataValue *rpFP = &FP_GET_ITEM(fp, FP_RET_PC_INDEX);
450 DataValue *endDv = (arg1 > outpt) ? arg1 : outpt;
451 int nArgs = FP_GET_ARG_COUNT(fp);
452 @@ -3976,17 +4042,16 @@ static void stackdumpframe(DataValue *ar
453 for (dv = endDv; dv < sp; dv++)
454 #endif /* #ifdef DEBUG_STACK_HEADFIRST */
456 - const char *posFmt = "%-6s ";
457 + const char *posFmt = "%-6s";
458 const char *symName = "";
461 char buffer[sizeof(STACK_DUMP_ARG_PREFIX) + TYPE_INT_STR_SIZE(int)];
462 int offset = dv - fp;
463 - const char *leadIn = (dv >= arrow) ? ">>>>" :
464 - (dv == arg1) ? "----" :
465 - (dv == fnNm) ? "====" : "";
466 - printd("%4.4s", leadIn);
467 - printd("%8p%c", dv, topMark);
468 + const char *leadIn = (dv >= arrow) ? ">>>>>" :
469 + (dv == arg1) ? "---->" :
470 + (dv == argc) ? "====>" : "";
471 + printd("%5.5s%10p%c", leadIn, dv, topMark);
473 case FP_ARG_COUNT_INDEX: pos = "NArgs"; break; /* num. arguments */
474 case FP_FUNCTION_NAME: pos = "FnName"; break;
475 @@ -4004,10 +4069,7 @@ static void stackdumpframe(DataValue *ar
477 else if (0 <= offset && offset < nSyms) {
478 sprintf(pos = buffer, offset ? "[%d]" : "FP[%d]", offset);
481 - else if (offset == 0) {
487 @@ -4022,12 +4084,39 @@ static void stackdumpframe(DataValue *ar
491 - printd("%-*.*s ", symLen, symLen, symName);
492 + printd(" %-*.*s", symLen, symLen, symName);
494 - if (dv == fnNm && dv->tag == STRING_TAG && dv->val.str.rep)
495 - printd("%s", dv->val.str.rep);
496 + if (dv == fnNm && dv->tag == STRING_TAG && dv->val.str.rep) {
497 + printd(" %s", dv->val.str.rep);
501 + if (dv->val.dataval) {
502 + printd(" %p", dv->val.dataval);
510 + if (dv->val.inst) {
511 + printd(" %p", dv->val.inst);
519 + Program *prog = dv->val.prog;
520 + printd(" <%s:%u %p>",
521 + prog->name, prog->refcount,
530 @@ -4054,7 +4143,7 @@ static void stackdumpInternal(int n, int
531 if (outpt < TheStack)
532 printd("--------------Stack base--------------\n");
533 stackdumpframe(arrow, outpt, FrameP, StackP, '*');
534 - printd("Stack ----->\n");
535 + printd("Stack ----->\n\n");
536 #endif /* #ifdef DEBUG_STACK_HEADFIRST */
539 @@ -4062,15 +4151,19 @@ static void stackdump(int n, int extra)
541 static int outIsTTY = -1;
543 - outIsTTY = isatty(fileno(stdout));
544 + outIsTTY = isatty(fileno(stderr));
546 - stackdumpInternal(n, extra);
547 +#ifndef DEBUG_STACKDUMP_EXTRA
548 +#define DEBUG_STACKDUMP_EXTRA 0
550 + stackdumpInternal(n, extra + DEBUG_STACKDUMP_EXTRA);
551 +#undef DEBUG_STACKDUMP_EXTRA
561 #endif /* ifdef DEBUG_STACK */
562 diff --quilt old/source/interpret.h new/source/interpret.h
563 --- old/source/interpret.h
564 +++ new/source/interpret.h
565 @@ -105,10 +105,10 @@ typedef struct SymbolRec {
568 typedef struct ProgramTag {
570 Symbol *localSymList;
576 /* Information needed to re-start a preempted macro */
577 @@ -128,6 +128,7 @@ typedef struct AccumulatorDataTag {
579 Inst *loopStack[LOOP_STACK_SIZE];
584 void InitMacroGlobals(void);
585 @@ -144,7 +145,7 @@ int ArrayCopy(DataValue *dstArray, DataV
587 /* Routines for creating a program, (accumulated beginning with
588 BeginCreatingProgram and returned via FinishCreatingProgram) */
589 -void BeginCreatingProgram(AccumulatorData *acc);
590 +void BeginCreatingProgram(const char *name, AccumulatorData *acc);
591 int AddOp(int op, char **msg);
592 int AddSym(Symbol *sym, char **msg);
593 int AddImmediate(int value, char **msg);
594 diff --quilt old/source/parse.y new/source/parse.y
595 --- old/source/parse.y
596 +++ new/source/parse.y
597 @@ -196,7 +196,7 @@ definesym: SYMBOL {
598 yyerror("try to override built-in subroutine"); YYERROR;
600 $$.sym = PromoteToGlobal($1);
601 - BeginCreatingProgram($$.acc);
602 + BeginCreatingProgram($$.sym->name, $$.acc);
605 define: definekw blank definesym blank blockwb {
606 @@ -745,14 +745,16 @@ Program *ParseMacro(char *expr, char **m
609 AccumulatorData *acc = XtNew(AccumulatorData);
610 - static const char *prefix = ">> ";
613 int oldyydebug = yydebug;
617 - BeginCreatingProgram(acc);
619 + name = "--unknown--";
621 + BeginCreatingProgram(name, acc);
623 /* whether we allow the "define" keyword */
624 AllowDefine = allowDefine;
625 @@ -780,12 +782,6 @@ Program *ParseMacro(char *expr, char **m
626 prog = FinishCreatingProgram(acc);
630 - name = "--unknown--";
632 - prog->name = XtMalloc(strlen(name) + strlen(prefix) + 1);
633 - strcat(strcpy(prog->name, prefix), name);
635 /* parse succeeded */
638 diff --quilt old/source/userCmds.c new/source/userCmds.c
639 --- old/source/userCmds.c
640 +++ new/source/userCmds.c
641 @@ -1286,13 +1286,13 @@ static int doMacroMenuCmd(WindowInfo *wi
642 int DoNamedMacroMenuCmd(WindowInfo *window, const char *itemName)
644 return doMacroMenuCmd(window, itemName, MacroMenuItems, NMacroMenuItems,
649 int DoNamedBGMenuCmd(WindowInfo *window, const char *itemName)
651 return doMacroMenuCmd(window, itemName, BGMenuItems, NBGMenuItems,
652 - "background-menu>");
653 + "Background Menu>");
657 diff --quilt old/makefiles/Makefile.bertw new/makefiles/Makefile.bertw
658 --- old/makefiles/Makefile.bertw
659 +++ new/makefiles/Makefile.bertw
660 @@ -23,6 +23,9 @@ ifdef DEBUG
662 ifndef DEBUG_NO_STACKDUMP
663 CFLAGS += -DDEBUG_STACK
664 + ifdef DEBUG_STACKDUMP_EXTRA
665 + CFLAGS += -DDEBUG_STACKDUMP_EXTRA=$(DEBUG_STACKDUMP_EXTRA)
668 ifndef DEBUG_NO_DISASM
669 CFLAGS += -DDEBUG_ASSEMBLY