1 Subject: modifications to the InterpretDebug patch
5 source/interpret.c | 151 ++++++++++++++++++++++++++++++++++++-----------------
6 source/interpret.h | 5 +
7 source/parse.y | 14 +---
8 source/userCmds.c | 4 -
9 4 files changed, 115 insertions(+), 59 deletions(-)
11 diff --quilt old/source/interpret.c new/source/interpret.c
12 --- old/source/interpret.c
13 +++ new/source/interpret.c
14 @@ -114,21 +114,21 @@ static const char *tagToStr(enum typeTag
15 #define DEBUG_DISASSEMBLER
16 static const char *printd(const char *f, ...);
17 static int outPrintd();
18 -static void disasm(Inst *inst, int nInstr);
19 +static void disasm(const char *name, Inst *inst, int nInstr);
20 static void disasmInternal(Inst *inst, int nInstr);
21 #endif /* #if defined(DEBUG_ASSEMBLY) || defined(DEBUG_STACK) */
23 #ifdef DEBUG_ASSEMBLY /* for disassembly */
24 -#define DISASM(i, n) disasm(i, n)
25 +#define DISASM(name, i, n) disasm(name, i, n)
26 #else /* #ifndef DEBUG_ASSEMBLY */
28 +#define DISASM(name, i, n)
29 #endif /* #ifndef DEBUG_ASSEMBLY */
31 #ifdef DEBUG_STACK /* for run-time instruction and stack trace */
32 static void stackdump(int n, int extra);
33 static void stackdumpInternal(int n, int extra);
34 #define STACKDUMP(n, x) stackdump(n, x)
35 -#define DISASM_RT(i, n) disasm(i, n)
36 +#define DISASM_RT(i, n) disasm(NULL, i, n)
37 #else /* #ifndef DEBUG_STACK */
38 #define STACKDUMP(n, x)
39 #define DISASM_RT(i, n)
40 @@ -161,6 +161,8 @@ static Inst *ProgP; /* next free spot
41 static Inst *LoopStack[LOOP_STACK_SIZE]; /* addresses of break, cont stmts */
42 static Inst **LoopStackPtr = LoopStack; /* to fill at the end of a loop */
44 +static const char *ProgramName = "";
46 /* Global data for the interpreter */
47 static DataValue *TheStack; /* the stack */
48 static DataValue *StackP; /* next free spot on stack */
49 @@ -253,7 +255,7 @@ void InitMacroGlobals(void)
50 ** Start collecting instructions for a program. Clears the program
51 ** and the symbol table.
53 -void BeginCreatingProgram(AccumulatorData *acc)
54 +void BeginCreatingProgram(const char *name, AccumulatorData *acc)
57 acc->localSymList = LocalSymList;
58 @@ -261,10 +263,12 @@ void BeginCreatingProgram(AccumulatorDat
60 memcpy(acc->loopStack, LoopStack, sizeof(*LoopStack) * LOOP_STACK_SIZE);
61 acc->loopStackPtr = LoopStackPtr;
62 + acc->name = ProgramName;
66 LoopStackPtr = LoopStack;
71 @@ -278,12 +282,12 @@ Program *FinishCreatingProgram(Accumulat
72 int progLen, fpOffset = 0;
75 - newProg = (Program *)XtMalloc(sizeof(Program));
76 + newProg = (Program *)XtMalloc(sizeof(Program) + strlen(ProgramName));
77 progLen = ((char *)ProgP) - ((char *)Prog);
78 newProg->code = (Inst *)XtMalloc(progLen);
79 memcpy(newProg->code, Prog, progLen);
80 newProg->localSymList = LocalSymList;
81 - newProg->name = NULL;
82 + strcpy(newProg->name, ProgramName);
83 newProg->refcount = 1;
85 /* Local variables' values are stored on the stack. Here we assign
86 @@ -291,7 +295,7 @@ Program *FinishCreatingProgram(Accumulat
87 for (s = newProg->localSymList; s != NULL; s = s->next)
88 s->value.val.n = fpOffset++;
90 - DISASM(newProg->code, ProgP - Prog);
91 + DISASM(newProg->name, newProg->code, ProgP - Prog);
94 LocalSymList = acc->localSymList;
95 @@ -299,6 +303,7 @@ Program *FinishCreatingProgram(Accumulat
97 memcpy(LoopStack, acc->loopStack, sizeof(*LoopStack) * LOOP_STACK_SIZE);
98 LoopStackPtr = acc->loopStackPtr;
99 + ProgramName = acc->name;
103 @@ -308,7 +313,6 @@ void FreeProgram(Program *prog)
104 if (--prog->refcount == 0) {
105 freeSymbolTable(prog->localSymList);
106 XtFree((char *)prog->code);
107 - XtFree((char *)prog->name);
108 XtFree((char *)prog);
111 @@ -3074,7 +3078,7 @@ static void arrayDisposeNode(rbTreeNode
113 SparseArrayEntry *ArrayNew(void)
115 - return((SparseArrayEntry *)rbTreeNew(arrayEmptyAllocator));
116 + return((SparseArrayEntry *)rbTreeNew(arrayEmptyAllocator));
120 @@ -3586,7 +3590,9 @@ static int errCheck(const char *s)
122 static char *stackDumpStr(DataValue *fp, const char *msg, char **s, int *pLen)
125 + static const char backtraceMsg[] = "\n\nBacktrace:";
126 + char frameBuf[TYPE_INT_STR_SIZE(int) + 2];
127 + int len, nFrames, frameWidth, thisFrameWidth;
131 @@ -3594,22 +3600,26 @@ static char *stackDumpStr(DataValue *fp,
136 + static const char stackdumpMsg[] = "\n\nStack:\n";
137 disasmInternal(PC - 1, 1);
138 stackdumpInternal(0, 50);
142 /* first measure the lengths */
143 - len = strlen(msg) + 1;
144 + len = strlen(msg) + strlen(backtraceMsg) + 1;
149 len = len + FP_GET_ITEM(nfp, FP_FUNCTION_NAME).val.str.len + 1;
150 pc = FP_GET_RET_PC(nfp);
151 nfp = FP_GET_OLD_FP(nfp);
153 + frameWidth = lenLongAsStr(nFrames);
154 + len += nFrames * (2 + frameWidth);
156 - len += strlen(dump);
157 + len += strlen(stackdumpMsg) + strlen(dump);
161 @@ -3621,10 +3631,25 @@ static char *stackDumpStr(DataValue *fp,
175 + thisFrameWidth = 0;
176 + op = longAsStr(nFrames);
181 + while (thisFrameWidth++ < frameWidth)
184 op = FP_GET_ITEM(nfp, FP_FUNCTION_NAME).val.str.rep;
187 @@ -3632,12 +3657,15 @@ static char *stackDumpStr(DataValue *fp,
188 nfp = FP_GET_OLD_FP(nfp);
204 @@ -3779,28 +3807,46 @@ static void dumpVal(DataValue dv)
208 - printd("i=%d", dv.val.n);
209 + printd("<integer> %d", dv.val.n);
217 char *src = dv.val.str.rep;
219 - printd("s=<NULL>");
220 + printd("<string> <NULL>");
223 - for (k = 0; src[k] && k < sizeof s - 1; k++) {
224 - s[k] = isprint(src[k]) ? src[k] : '?';
225 + for (k = 0, l = 0; src[k] && l < sizeof s - 1; k++, l++) {
227 + const char to[] = "\\\"ntbrfave";
228 +#ifdef EBCDIC_CHARSET
229 + const char from[] = "\\\"\n\t\b\r\f\a\v\x27"; /* EBCDIC escape */
231 + const char from[] = "\\\"\n\t\b\r\f\a\v\x1B"; /* ASCII escape */
233 + if ((e = strchr(from, src[k]))) {
234 + if (l < sizeof s - 2) {
236 + s[l] = to[e - from];
239 + else if (isprint(src[k])) {
247 - printd("s=\"%s\"%s[%d]", s,
249 + printd("<string> \"%s\"%s[%d]", s,
250 src[k] ? "..." : "", strlen(src));
255 - printd("%08p <array>[%d]", dv.val.arrayPtr, ArraySize(&dv));
256 + printd("<array> %8p[%d]", dv.val.arrayPtr, ArraySize(&dv));
260 @@ -3826,57 +3872,66 @@ static void disasmInternal(Inst *inst, i
264 + static size_t opLen;
268 + for (j = 0; j < N_OPS; ++j) {
269 + if (opLen < strlen(opNames[j])) {
270 + opLen = strlen(opNames[j]);
275 for (i = 0; i < nInstr; ++i) {
276 - printd("Prog %8p ", &inst[i]);
277 + printd("Prog %8p", &inst[i]);
278 for (j = 0; j < N_OPS; ++j) {
279 if (inst[i].func == OpFns[j]) {
280 - printd("%22s ", opNames[j]);
281 + printd(" %*s", (int)opLen, opNames[j]);
282 if (j == OP_PUSH_SYM || j == OP_ASSIGN) {
283 Symbol *sym = inst[i+1].sym;
284 - printd("%s", sym->name);
285 + printd(" %s", sym->name);
286 if (sym->value.tag == STRING_TAG &&
287 strncmp(sym->name, "string #", 8) == 0) {
293 else if (j == OP_PUSH_IMMED) {
294 - printd("%d", inst[i+1].value);
295 + printd(" %d", inst[i+1].value);
298 else if (j == OP_BRANCH || j == OP_BRANCH_FALSE ||
299 j == OP_BRANCH_NEVER || j == OP_BRANCH_TRUE) {
300 - printd("to=(%d) %p", inst[i+1].value,
301 + printd(" to=(%+d) %8p", inst[i+1].value,
302 &inst[i+1] + inst[i+1].value);
305 else if (j == OP_CONCAT) {
306 - printd("nExpr=%d", inst[i+1].value);
307 + printd(" nExpr=%d", inst[i+1].value);
310 else if (j == OP_SUBR_CALL) {
311 int args = inst[i+2].value;
312 - printd("%s ", inst[i+1].sym->name);
313 + printd(" %s", inst[i+1].sym->name);
315 - printd("%d+args[] (%d)", -args - 1, args);
316 + printd(" %d+args[] (%d)", -args - 1, args);
319 - printd("%d args", args);
320 + printd(" %d args", args);
324 else if (j == OP_SUBR_CALL_STACKED_N) {
325 - printd("%s args[] (?)", inst[i+1].sym->name);
326 + printd(" %s args[] (?)", inst[i+1].sym->name);
329 else if (j == OP_BEGIN_ARRAY_ITER) {
330 - printd("%s in", inst[i+1].sym->name);
331 + printd(" %s in", inst[i+1].sym->name);
334 else if (j == OP_ARRAY_ITER) {
335 - printd("%s = %s++ end-loop=(%d) %p",
336 + printd(" %s = %s++ end-loop=(%+d) %8p",
339 inst[i+3].value, &inst[i+3] + inst[i+3].value);
340 @@ -3888,18 +3943,20 @@ static void disasmInternal(Inst *inst, i
341 j == OP_ANONARRAY_INDEX_VAL ||
342 j == OP_NAMED_ARG1 ||
343 j == OP_NAMED_ARGN) {
344 - printd("nDim=%d", inst[i+1].value);
345 + printd(" nDim=%d", inst[i+1].value);
348 else if (j == OP_ARRAY_REF_ASSIGN_SETUP) {
349 - printd("binOp=%s ", inst[i+1].value ? "true" : "false");
350 - printd("nDim=%d", inst[i+2].value);
351 + printd(" binOp=%s nDim=%d",
352 + inst[i+1].value ? "true" : "false",
356 else if (j == OP_PUSH_ARRAY_SYM) {
357 - printd("%s", inst[++i].sym->name);
358 - printd(" %s", inst[i+1].value ? "createAndRef" : "refOnly");
361 + inst[i+1].sym->name,
362 + inst[i+2].value ? "createAndRef" : "refOnly");
367 @@ -3907,18 +3964,20 @@ static void disasmInternal(Inst *inst, i
371 - printd("%x\n", inst[i].value);
372 + printd(" %x\n", inst[i].value);
377 -static void disasm(Inst *inst, int nInstr)
378 +static void disasm(const char *name, Inst *inst, int nInstr)
380 static int outIsTTY = -1;
381 if (outIsTTY == -1) outIsTTY = isatty(fileno(stdout));
382 if (outIsTTY) { printd("\033[H"); }
383 + if (name) printd(">> %s\n", name);
384 disasmInternal(inst, nInstr);
385 if (outIsTTY) { printd("\033[J\n"); }
386 + if (name) printd("\n");
389 #endif /* #ifdef DEBUG_DISASSEMBLER */
390 @@ -4047,7 +4106,7 @@ static void stackdumpInternal(int n, int
391 if (outpt < TheStack)
392 printd("--------------Stack base--------------\n");
393 stackdumpframe(arrow, outpt, FrameP, StackP, '*');
394 - printd("Stack ----->\n");
395 + printd("Stack ----->\n\n");
396 #endif /* #ifdef DEBUG_STACK_HEADFIRST */
399 diff --quilt old/source/interpret.h new/source/interpret.h
400 --- old/source/interpret.h
401 +++ new/source/interpret.h
402 @@ -108,7 +108,7 @@ typedef struct ProgramTag {
403 Symbol *localSymList;
410 /* Information needed to re-start a preempted macro */
411 @@ -128,6 +128,7 @@ typedef struct AccumulatorDataTag {
413 Inst *loopStack[LOOP_STACK_SIZE];
418 void InitMacroGlobals(void);
419 @@ -144,7 +145,7 @@ int ArrayCopy(DataValue *dstArray, DataV
421 /* Routines for creating a program, (accumulated beginning with
422 BeginCreatingProgram and returned via FinishCreatingProgram) */
423 -void BeginCreatingProgram(AccumulatorData *acc);
424 +void BeginCreatingProgram(const char *name, AccumulatorData *acc);
425 int AddOp(int op, char **msg);
426 int AddSym(Symbol *sym, char **msg);
427 int AddImmediate(int value, char **msg);
428 diff --quilt old/source/parse.y new/source/parse.y
429 --- old/source/parse.y
430 +++ new/source/parse.y
431 @@ -196,7 +196,7 @@ definesym: SYMBOL {
432 yyerror("try to override built-in subroutine"); YYERROR;
434 $$.sym = PromoteToGlobal($1);
435 - BeginCreatingProgram($$.acc);
436 + BeginCreatingProgram($$.sym->name, $$.acc);
439 define: definekw blank definesym blank blockwb {
440 @@ -747,14 +747,16 @@ Program *ParseMacro(char *expr, char **m
443 AccumulatorData *acc = (AccumulatorData *)XtMalloc(sizeof(*acc));
444 - static const char *prefix = ">> ";
447 int oldyydebug = yydebug;
451 - BeginCreatingProgram(acc);
453 + name = "--unknown--";
455 + BeginCreatingProgram(name, acc);
457 /* whether we allow the "define" keyword */
458 AllowDefine = allowDefine;
459 @@ -782,12 +784,6 @@ Program *ParseMacro(char *expr, char **m
460 prog = FinishCreatingProgram(acc);
464 - name = "--unknown--";
466 - prog->name = XtMalloc(strlen(name) + strlen(prefix) + 1);
467 - strcat(strcpy(prog->name, prefix), name);
469 /* parse succeeded */
472 diff --quilt old/source/userCmds.c new/source/userCmds.c
473 --- old/source/userCmds.c
474 +++ new/source/userCmds.c
475 @@ -1286,13 +1286,13 @@ static int doMacroMenuCmd(WindowInfo *wi
476 int DoNamedMacroMenuCmd(WindowInfo *window, const char *itemName)
478 return doMacroMenuCmd(window, itemName, MacroMenuItems, NMacroMenuItems,
483 int DoNamedBGMenuCmd(WindowInfo *window, const char *itemName)
485 return doMacroMenuCmd(window, itemName, BGMenuItems, NBGMenuItems,
486 - "background-menu>");
487 + "Background Menu>");