NULL terminate string bulk list
[nedit-bw.git] / InterpretDebug-mods.patch
bloba8f01fba8188943b2b95779841611380064aaa97
1 Subject: modifications to the InterpretDebug patch
3 ---
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 */
27 -#define DISASM(i, n)
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)
56 /* save state */
57 acc->localSymList = LocalSymList;
58 @@ -261,10 +263,12 @@ void BeginCreatingProgram(AccumulatorDat
59 acc->progP = ProgP;
60 memcpy(acc->loopStack, LoopStack, sizeof(*LoopStack) * LOOP_STACK_SIZE);
61 acc->loopStackPtr = LoopStackPtr;
62 + acc->name = ProgramName;
64 LocalSymList = NULL;
65 ProgP = Prog;
66 LoopStackPtr = LoopStack;
67 + ProgramName = name;
71 @@ -278,12 +282,12 @@ Program *FinishCreatingProgram(Accumulat
72 int progLen, fpOffset = 0;
73 Symbol *s;
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);
93 /* restore state */
94 LocalSymList = acc->localSymList;
95 @@ -299,6 +303,7 @@ Program *FinishCreatingProgram(Accumulat
96 ProgP = acc->progP;
97 memcpy(LoopStack, acc->loopStack, sizeof(*LoopStack) * LOOP_STACK_SIZE);
98 LoopStackPtr = acc->loopStackPtr;
99 + ProgramName = acc->name;
101 return newProg;
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)
124 - int len;
125 + static const char backtraceMsg[] = "\n\nBacktrace:";
126 + char frameBuf[TYPE_INT_STR_SIZE(int) + 2];
127 + int len, nFrames, frameWidth, thisFrameWidth;
128 const char *op;
129 char *np;
130 DataValue *nfp = fp;
131 @@ -3594,22 +3600,26 @@ static char *stackDumpStr(DataValue *fp,
133 #ifdef DEBUG_STACK
134 const char *dump;
135 - printd("\n\n");
136 + static const char stackdumpMsg[] = "\n\nStack:\n";
137 disasmInternal(PC - 1, 1);
138 stackdumpInternal(0, 50);
139 dump = printd(NULL);
140 #endif
142 /* first measure the lengths */
143 - len = strlen(msg) + 1;
144 + len = strlen(msg) + strlen(backtraceMsg) + 1;
145 nfp = fp;
146 + nFrames = 0;
147 do {
148 + nFrames++;
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);
152 } while (pc);
153 + frameWidth = lenLongAsStr(nFrames);
154 + len += nFrames * (2 + frameWidth);
155 #ifdef DEBUG_STACK
156 - len += strlen(dump);
157 + len += strlen(stackdumpMsg) + strlen(dump);
158 #endif
159 if (*pLen < len)
161 @@ -3621,10 +3631,25 @@ static char *stackDumpStr(DataValue *fp,
162 op = msg;
163 while (*op)
164 *np++ = *op++;
165 + op = backtraceMsg;
166 + while (*op)
167 + *np++ = *op++;
169 nfp = fp;
170 + nFrames = 0;
171 do {
172 + nFrames++;
173 *np++ = '\n';
174 + *np++ = '#';
175 + thisFrameWidth = 0;
176 + op = longAsStr(nFrames);
177 + while (*op) {
178 + thisFrameWidth++;
179 + *np++ = *op++;
181 + while (thisFrameWidth++ < frameWidth)
182 + *np++ = ' ';
183 + *np++ = ' ';
184 op = FP_GET_ITEM(nfp, FP_FUNCTION_NAME).val.str.rep;
185 while (*op)
186 *np++ = *op++;
187 @@ -3632,12 +3657,15 @@ static char *stackDumpStr(DataValue *fp,
188 nfp = FP_GET_OLD_FP(nfp);
189 } while (pc);
190 #ifdef DEBUG_STACK
191 + op = stackdumpMsg;
192 + while (*op)
193 + *np++ = *op++;
194 op = dump;
195 while (*op)
196 *np++ = *op++;
197 #endif
199 - *np = 0;
200 + *np = '\0';
201 return *s;
204 @@ -3779,28 +3807,46 @@ static void dumpVal(DataValue dv)
206 switch (dv.tag) {
207 case INT_TAG:
208 - printd("i=%d", dv.val.n);
209 + printd("<integer> %d", dv.val.n);
210 break;
211 case STRING_TAG:
213 - int k;
214 - char s[21];
215 + int k, l;
216 + char s[64];
217 char *src = dv.val.str.rep;
218 if (!src) {
219 - printd("s=<NULL>");
220 + printd("<string> <NULL>");
222 else {
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++) {
226 + char *e;
227 + const char to[] = "\\\"ntbrfave";
228 +#ifdef EBCDIC_CHARSET
229 + const char from[] = "\\\"\n\t\b\r\f\a\v\x27"; /* EBCDIC escape */
230 +#else
231 + const char from[] = "\\\"\n\t\b\r\f\a\v\x1B"; /* ASCII escape */
232 +#endif
233 + if ((e = strchr(from, src[k]))) {
234 + if (l < sizeof s - 2) {
235 + s[l++] = '\\';
236 + s[l] = to[e - from];
239 + else if (isprint(src[k])) {
240 + s[l] = src[k];
242 + else {
243 + s[l] = '?';
246 - s[k] = 0;
247 - printd("s=\"%s\"%s[%d]", s,
248 + s[l] = 0;
249 + printd("<string> \"%s\"%s[%d]", s,
250 src[k] ? "..." : "", strlen(src));
253 break;
254 case ARRAY_TAG:
255 - printd("%08p <array>[%d]", dv.val.arrayPtr, ArraySize(&dv));
256 + printd("<array> %8p[%d]", dv.val.arrayPtr, ArraySize(&dv));
257 break;
258 case NO_TAG:
259 if (!dv.val.inst) {
260 @@ -3826,57 +3872,66 @@ static void disasmInternal(Inst *inst, i
261 #undef OP
263 int i, j;
264 + static size_t opLen;
266 - printd("\n");
267 + if (!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) {
288 + printd(" ");
289 dumpVal(sym->value);
291 ++i;
293 else if (j == OP_PUSH_IMMED) {
294 - printd("%d", inst[i+1].value);
295 + printd(" %d", inst[i+1].value);
296 ++i;
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);
303 ++i;
305 else if (j == OP_CONCAT) {
306 - printd("nExpr=%d", inst[i+1].value);
307 + printd(" nExpr=%d", inst[i+1].value);
308 ++i;
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);
314 if (args < 0) {
315 - printd("%d+args[] (%d)", -args - 1, args);
316 + printd(" %d+args[] (%d)", -args - 1, args);
318 else {
319 - printd("%d args", args);
320 + printd(" %d args", args);
322 i += 2;
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);
327 ++i;
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);
332 ++i;
334 else if (j == OP_ARRAY_ITER) {
335 - printd("%s = %s++ end-loop=(%d) %p",
336 + printd(" %s = %s++ end-loop=(%+d) %8p",
337 inst[i+1].sym->name,
338 inst[i+2].sym->name,
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);
346 ++i;
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",
353 + inst[i+2].value);
354 i += 2;
356 else if (j == OP_PUSH_ARRAY_SYM) {
357 - printd("%s", inst[++i].sym->name);
358 - printd(" %s", inst[i+1].value ? "createAndRef" : "refOnly");
359 - ++i;
360 + printd(" %s %s",
361 + inst[i+1].sym->name,
362 + inst[i+2].value ? "createAndRef" : "refOnly");
363 + i += 2;
366 printd("\n");
367 @@ -3907,18 +3964,20 @@ static void disasmInternal(Inst *inst, i
370 if (j == N_OPS) {
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");
387 outPrintd();
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;
404 Inst *code;
405 unsigned refcount;
406 - char *name;
407 + char name[1];
408 } Program;
410 /* Information needed to re-start a preempted macro */
411 @@ -128,6 +128,7 @@ typedef struct AccumulatorDataTag {
412 Inst *progP;
413 Inst *loopStack[LOOP_STACK_SIZE];
414 Inst **loopStackPtr;
415 + const char *name;
416 } AccumulatorData;
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
442 Program *prog;
443 AccumulatorData *acc = (AccumulatorData *)XtMalloc(sizeof(*acc));
444 - static const char *prefix = ">> ";
446 #if YYDEBUG
447 int oldyydebug = yydebug;
448 yydebug = 1;
449 #endif
451 - BeginCreatingProgram(acc);
452 + if (!name)
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);
461 XtFree((char *)acc);
463 - if (!name)
464 - name = "--unknown--";
466 - prog->name = XtMalloc(strlen(name) + strlen(prefix) + 1);
467 - strcat(strcpy(prog->name, prefix), name);
469 /* parse succeeded */
470 *msg = "";
471 *stoppedAt = InPtr;
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,
479 - "macro-menu>");
480 + "Macro Menu>");
483 int DoNamedBGMenuCmd(WindowInfo *window, const char *itemName)
485 return doMacroMenuCmd(window, itemName, BGMenuItems, NBGMenuItems,
486 - "background-menu>");
487 + "Background Menu>");