1 Subject: modifications to the InterpretDebug patch
5 source/interpret.c | 117 ++++++++++++++++++++++++++++++++++-------------------
6 source/interpret.h | 5 +-
7 source/parse.y | 14 ++----
8 source/userCmds.c | 4 -
9 4 files changed, 86 insertions(+), 54 deletions(-)
11 diff --quilt old/source/interpret.c new/source/interpret.c
12 --- old/source/interpret.c
13 +++ new/source/interpret.c
14 @@ -112,25 +112,25 @@ static const char *tagToStr(enum typeTag
16 #if defined(DEBUG_ASSEMBLY) || defined(DEBUG_STACK)
17 #define DEBUG_DISASSEMBLER
18 static const char *printd(const char *f, ...);
19 static int outPrintd();
20 -static void disasm(Inst *inst, int nInstr);
21 +static void disasm(const char *name, Inst *inst, int nInstr);
22 static void disasmInternal(Inst *inst, int nInstr);
23 #endif /* #if defined(DEBUG_ASSEMBLY) || defined(DEBUG_STACK) */
25 #ifdef DEBUG_ASSEMBLY /* for disassembly */
26 -#define DISASM(i, n) disasm(i, n)
27 +#define DISASM(name, i, n) disasm(name, i, n)
28 #else /* #ifndef DEBUG_ASSEMBLY */
30 +#define DISASM(name, i, n)
31 #endif /* #ifndef DEBUG_ASSEMBLY */
33 #ifdef DEBUG_STACK /* for run-time instruction and stack trace */
34 static void stackdump(int n, int extra);
35 static void stackdumpInternal(int n, int extra);
36 #define STACKDUMP(n, x) stackdump(n, x)
37 -#define DISASM_RT(i, n) disasm(i, n)
38 +#define DISASM_RT(i, n) disasm(NULL, i, n)
39 #else /* #ifndef DEBUG_STACK */
40 #define STACKDUMP(n, x)
41 #define DISASM_RT(i, n)
42 #endif /* #ifndef DEBUG_STACK */
44 @@ -159,10 +159,12 @@ static Symbol *LocalSymList = NULL; /*
45 static Inst Prog[PROGRAM_SIZE]; /* the program */
46 static Inst *ProgP; /* next free spot for code gen. */
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 static DataValue *FrameP; /* frame pointer (start of local variables
56 for the current subroutine invocation) */
57 @@ -251,22 +253,24 @@ void InitMacroGlobals(void)
60 ** Start collecting instructions for a program. Clears the program
61 ** and the symbol table.
63 -void BeginCreatingProgram(AccumulatorData *acc)
64 +void BeginCreatingProgram(const char *name, AccumulatorData *acc)
67 acc->localSymList = LocalSymList;
68 memcpy(acc->prog, Prog, sizeof(Inst) * PROGRAM_SIZE);
70 memcpy(acc->loopStack, LoopStack, sizeof(Inst) * LOOP_STACK_SIZE);
71 acc->loopStackPtr = LoopStackPtr;
72 + acc->name = ProgramName;
76 LoopStackPtr = LoopStack;
81 ** Finish up the program under construction, and return it (code and
82 ** symbol table) as a package that ExecuteMacro can execute. This
83 @@ -276,41 +280,41 @@ Program *FinishCreatingProgram(Accumulat
86 int progLen, fpOffset = 0;
89 - newProg = (Program *)XtMalloc(sizeof(Program));
90 + newProg = (Program *)XtMalloc(sizeof(Program) + strlen(ProgramName));
91 progLen = ((char *)ProgP) - ((char *)Prog);
92 newProg->code = (Inst *)XtMalloc(progLen);
93 memcpy(newProg->code, Prog, progLen);
94 newProg->localSymList = LocalSymList;
95 - newProg->name = NULL;
96 + strcpy(newProg->name, ProgramName);
97 newProg->refcount = 1;
99 /* Local variables' values are stored on the stack. Here we assign
100 frame pointer offsets to them. */
101 for (s = newProg->localSymList; s != NULL; s = s->next)
102 s->value.val.n = fpOffset++;
104 - DISASM(newProg->code, ProgP - Prog);
105 + DISASM(newProg->name, newProg->code, ProgP - Prog);
108 LocalSymList = acc->localSymList;
109 memcpy(Prog, acc->prog, sizeof(Inst) * PROGRAM_SIZE);
111 memcpy(LoopStack, acc->loopStack, sizeof(Inst) * LOOP_STACK_SIZE);
112 LoopStackPtr = acc->loopStackPtr;
113 + ProgramName = acc->name;
118 void FreeProgram(Program *prog)
120 if (--prog->refcount == 0) {
121 freeSymbolTable(prog->localSymList);
122 XtFree((char *)prog->code);
123 - XtFree((char *)prog->name);
124 XtFree((char *)prog);
129 @@ -3066,11 +3070,11 @@ static void arrayDisposeNode(rbTreeNode
133 SparseArrayEntry *ArrayNew(void)
135 - return((SparseArrayEntry *)rbTreeNew(arrayEmptyAllocator));
136 + return((SparseArrayEntry *)rbTreeNew(arrayEmptyAllocator));
140 ** insert a DataValue into an array, allocate the array if needed
141 ** keyStr must be a string that was allocated with AllocString()
142 @@ -3764,32 +3768,50 @@ int outPrintd()
143 #ifdef DEBUG_DISASSEMBLER /* dumping values in disassembly or stack dump */
144 static void dumpVal(DataValue dv)
148 - printd("i=%d", dv.val.n);
149 + printd("<integer> %d", dv.val.n);
157 char *src = dv.val.str.rep;
159 - printd("s=<NULL>");
160 + printd("<string> <NULL>");
163 - for (k = 0; src[k] && k < sizeof s - 1; k++) {
164 - s[k] = isprint(src[k]) ? src[k] : '?';
165 + for (k = 0, l = 0; src[k] && l < sizeof s - 1; k++, l++) {
167 + const char to[] = "\\\"ntbrfave";
168 +#ifdef EBCDIC_CHARSET
169 + const char from[] = "\\\"\n\t\b\r\f\a\v\x27"; /* EBCDIC escape */
171 + const char from[] = "\\\"\n\t\b\r\f\a\v\x1B"; /* ASCII escape */
173 + if ((e = strchr(from, src[k]))) {
174 + if (l < sizeof s - 2) {
176 + s[l] = to[e - from];
179 + else if (isprint(src[k])) {
187 - printd("s=\"%s\"%s[%d]", s,
189 + printd("<string> \"%s\"%s[%d]", s,
190 src[k] ? "..." : "", strlen(src));
195 - printd("%08p <array>[%d]", dv.val.arrayPtr, ArraySize(&dv));
196 + printd("<array> %8p[%d]", dv.val.arrayPtr, ArraySize(&dv));
200 printd("<no value>");
202 @@ -3811,61 +3833,70 @@ static void disasmInternal(Inst *inst, i
203 #define OP(name, fn) #name,
208 + static size_t opLen;
212 + for (j = 0; j < N_OPS; ++j) {
213 + if (opLen < strlen(opNames[j])) {
214 + opLen = strlen(opNames[j]);
219 for (i = 0; i < nInstr; ++i) {
220 - printd("Prog %8p ", &inst[i]);
221 + printd("Prog %8p", &inst[i]);
222 for (j = 0; j < N_OPS; ++j) {
223 if (inst[i].func == OpFns[j]) {
224 - printd("%22s ", opNames[j]);
225 + printd(" %*s", (int)opLen, opNames[j]);
226 if (j == OP_PUSH_SYM || j == OP_ASSIGN) {
227 Symbol *sym = inst[i+1].sym;
228 - printd("%s", sym->name);
229 + printd(" %s", sym->name);
230 if (sym->value.tag == STRING_TAG &&
231 strncmp(sym->name, "string #", 8) == 0) {
237 else if (j == OP_PUSH_IMMED) {
238 - printd("%d", inst[i+1].value);
239 + printd(" %d", inst[i+1].value);
242 else if (j == OP_BRANCH || j == OP_BRANCH_FALSE ||
243 j == OP_BRANCH_NEVER || j == OP_BRANCH_TRUE) {
244 - printd("to=(%d) %p", inst[i+1].value,
245 + printd(" to=(%+d) %8p", inst[i+1].value,
246 &inst[i+1] + inst[i+1].value);
249 else if (j == OP_CONCAT) {
250 - printd("nExpr=%d", inst[i+1].value);
251 + printd(" nExpr=%d", inst[i+1].value);
254 else if (j == OP_SUBR_CALL) {
255 int args = inst[i+2].value;
256 - printd("%s ", inst[i+1].sym->name);
257 + printd(" %s", inst[i+1].sym->name);
259 - printd("%d+args[] (%d)", -args - 1, args);
260 + printd(" %d+args[] (%d)", -args - 1, args);
263 - printd("%d args", args);
264 + printd(" %d args", args);
268 else if (j == OP_SUBR_CALL_STACKED_N) {
269 - printd("%s args[] (?)", inst[i+1].sym->name);
270 + printd(" %s args[] (?)", inst[i+1].sym->name);
273 else if (j == OP_BEGIN_ARRAY_ITER) {
274 - printd("%s in", inst[i+1].sym->name);
275 + printd(" %s in", inst[i+1].sym->name);
278 else if (j == OP_ARRAY_ITER) {
279 - printd("%s = %s++ end-loop=(%d) %p",
280 + printd(" %s = %s++ end-loop=(%+d) %8p",
283 inst[i+3].value, &inst[i+3] + inst[i+3].value);
286 @@ -3873,41 +3904,45 @@ static void disasmInternal(Inst *inst, i
287 j == OP_ARRAY_DELETE ||
288 j == OP_ARRAY_ASSIGN ||
289 j == OP_ANONARRAY_INDEX_VAL ||
290 j == OP_NAMED_ARG1 ||
291 j == OP_NAMED_ARGN) {
292 - printd("nDim=%d", inst[i+1].value);
293 + printd(" nDim=%d", inst[i+1].value);
296 else if (j == OP_ARRAY_REF_ASSIGN_SETUP) {
297 - printd("binOp=%s ", inst[i+1].value ? "true" : "false");
298 - printd("nDim=%d", inst[i+2].value);
299 + printd(" binOp=%s nDim=%d",
300 + inst[i+1].value ? "true" : "false",
304 else if (j == OP_PUSH_ARRAY_SYM) {
305 - printd("%s", inst[++i].sym->name);
306 - printd(" %s", inst[i+1].value ? "createAndRef" : "refOnly");
309 + inst[i+1].sym->name,
310 + inst[i+2].value ? "createAndRef" : "refOnly");
319 - printd("%x\n", inst[i].value);
320 + printd(" %x\n", inst[i].value);
325 -static void disasm(Inst *inst, int nInstr)
326 +static void disasm(const char *name, Inst *inst, int nInstr)
328 static int outIsTTY = -1;
329 if (outIsTTY == -1) outIsTTY = isatty(fileno(stdout));
330 if (outIsTTY) { printd("\033[H"); }
331 + if (name) printd(">> %s\n", name);
332 disasmInternal(inst, nInstr);
333 if (outIsTTY) { printd("\033[J\n"); }
334 + if (name) printd("\n");
337 #endif /* #ifdef DEBUG_DISASSEMBLER */
339 #ifdef DEBUG_STACK /* for run-time stack dumping */
340 @@ -4032,11 +4067,11 @@ static void stackdumpInternal(int n, int
341 printd("--------------Stack base--------------\n");
343 if (outpt < TheStack)
344 printd("--------------Stack base--------------\n");
345 stackdumpframe(arrow, outpt, FrameP, StackP, '*');
346 - printd("Stack ----->\n");
347 + printd("Stack ----->\n\n");
348 #endif /* #ifdef DEBUG_STACK_HEADFIRST */
351 static void stackdump(int n, int extra)
353 diff --quilt old/source/interpret.h new/source/interpret.h
354 --- old/source/interpret.h
355 +++ new/source/interpret.h
356 @@ -106,11 +106,11 @@ typedef struct SymbolRec {
358 typedef struct ProgramTag {
359 Symbol *localSymList;
366 /* Information needed to re-start a preempted macro */
369 @@ -126,10 +126,11 @@ typedef struct AccumulatorDataTag {
370 Symbol *localSymList;
371 Inst prog[PROGRAM_SIZE];
373 Inst *loopStack[LOOP_STACK_SIZE];
378 void InitMacroGlobals(void);
380 SparseArrayEntry *arrayIterateFirst(DataValue *theArray);
381 @@ -142,11 +143,11 @@ unsigned ArraySize(DataValue *theArray);
382 Boolean ArrayGet(DataValue* theArray, char* keyStr, DataValue* theValue);
383 int ArrayCopy(DataValue *dstArray, DataValue *srcArray);
385 /* Routines for creating a program, (accumulated beginning with
386 BeginCreatingProgram and returned via FinishCreatingProgram) */
387 -void BeginCreatingProgram(AccumulatorData *acc);
388 +void BeginCreatingProgram(const char *name, AccumulatorData *acc);
389 int AddOp(int op, char **msg);
390 int AddSym(Symbol *sym, char **msg);
391 int AddImmediate(int value, char **msg);
392 int AddBranchOffset(Inst *to, char **msg);
394 diff --quilt old/source/parse.y new/source/parse.y
395 --- old/source/parse.y
396 +++ new/source/parse.y
397 @@ -178,11 +178,11 @@ define: DEFINE {
398 /* newly created sym, or we overwrite a local sym */;
400 yyerror("try to override built-in subroutine"); YYERROR;
402 $4 = PromoteToGlobal($4);
403 - BeginCreatingProgram($1);
404 + BeginCreatingProgram($4->name, $1);
407 ADD_OP(OP_RETURN_NO_VAL);
408 Program *prog = FinishCreatingProgram($1);
410 @@ -598,13 +598,15 @@ blank: /* nothing */
411 Program *ParseMacro(char *expr, char **msg, char **stoppedAt, int allowDefine,
415 AccumulatorData *acc = (AccumulatorData *)XtMalloc(sizeof(*acc));
416 - static const char *prefix = ">> ";
418 - BeginCreatingProgram(acc);
420 + name = "--unknown--";
422 + BeginCreatingProgram(name, acc);
424 /* whether we allow the "define" keyword */
425 AllowDefine = allowDefine;
427 /* call yyparse to parse the string and check for success. If the parse
428 @@ -621,16 +623,10 @@ Program *ParseMacro(char *expr, char **m
430 /* get the newly created program */
431 prog = FinishCreatingProgram(acc);
435 - name = "--unknown--";
437 - prog->name = XtMalloc(strlen(name) + strlen(prefix) + 1);
438 - strcat(strcpy(prog->name, prefix), name);
440 /* parse succeeded */
445 diff --quilt old/source/userCmds.c new/source/userCmds.c
446 --- old/source/userCmds.c
447 +++ new/source/userCmds.c
448 @@ -1284,17 +1284,17 @@ static int doMacroMenuCmd(WindowInfo *wi
451 int DoNamedMacroMenuCmd(WindowInfo *window, const char *itemName)
453 return doMacroMenuCmd(window, itemName, MacroMenuItems, NMacroMenuItems,
458 int DoNamedBGMenuCmd(WindowInfo *window, const char *itemName)
460 return doMacroMenuCmd(window, itemName, BGMenuItems, NBGMenuItems,
461 - "background-menu>");
462 + "Background Menu>");
467 ** Rebuild all of the Shell, Macro, Background menus of given editor window.