InterpretDebug-mods: calculate OpName length
[nedit-bw.git] / InterpretDebug-mods.patch
blob8982d7c8cc2807767781ede8b856a746ed9fa4d8
1 ---
3 source/interpret.c | 113 ++++++++++++++++++++++++++++++++++-------------------
4 source/interpret.h | 2
5 source/parse.y | 14 ++----
6 source/userCmds.c | 4 -
7 4 files changed, 82 insertions(+), 51 deletions(-)
9 diff --quilt old/source/interpret.c new/source/interpret.c
10 --- old/source/interpret.c
11 +++ new/source/interpret.c
12 @@ -161,25 +161,25 @@ static SparseArrayEntry *allocateSparseA
14 #if defined(DEBUG_ASSEMBLY) || defined(DEBUG_STACK)
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 #endif /* #ifndef DEBUG_STACK */
42 @@ -324,11 +324,11 @@ void BeginCreatingProgram(void)
44 ** Finish up the program under construction, and return it (code and
45 ** symbol table) as a package that ExecuteMacro can execute. This
46 ** program must be freed with FreeProgram.
48 -Program *FinishCreatingProgram(void)
49 +Program *FinishCreatingProgram(const char *name)
51 Program *newProg;
52 int progLen, fpOffset = 0;
53 Symbol *s;
55 @@ -342,12 +342,15 @@ Program *FinishCreatingProgram(void)
57 /* Local variables' values are stored on the stack. Here we assign
58 frame pointer offsets to them. */
59 for (s = newProg->localSymList; s != NULL; s = s->next)
60 s->value.val.n = fpOffset++;
62 - DISASM(newProg->code, ProgP - Prog);
64 + if (name)
65 + newProg->name = strdup(name);
67 + DISASM(newProg->name, newProg->code, ProgP - Prog);
69 return newProg;
72 void FreeProgram(Program *prog)
73 @@ -2919,11 +2922,11 @@ static void arrayDisposeNode(rbTreeNode
74 src->color = -1;
77 SparseArrayEntry *ArrayNew(void)
79 - return((SparseArrayEntry *)rbTreeNew(arrayEmptyAllocator));
80 + return((SparseArrayEntry *)rbTreeNew(arrayEmptyAllocator));
84 ** insert a DataValue into an array, allocate the array if needed
85 ** keyStr must be a string that was allocated with AllocString()
86 @@ -3601,32 +3604,50 @@ int outPrintd()
87 #ifdef DEBUG_DISASSEMBLER /* dumping values in disassembly or stack dump */
88 static void dumpVal(DataValue dv)
90 switch (dv.tag) {
91 case INT_TAG:
92 - printd("i=%d", dv.val.n);
93 + printd("<integer> %d", dv.val.n);
94 break;
95 case STRING_TAG:
97 - int k;
98 - char s[21];
99 + int k, l;
100 + char s[64];
101 char *src = dv.val.str.rep;
102 if (!src) {
103 - printd("s=<NULL>");
104 + printd("<string> <NULL>");
106 else {
107 - for (k = 0; src[k] && k < sizeof s - 1; k++) {
108 - s[k] = isprint(src[k]) ? src[k] : '?';
109 + for (k = 0, l = 0; src[k] && l < sizeof s - 1; k++, l++) {
110 + char *e;
111 + const char to[] = "\\\"ntbrfave";
112 +#ifdef EBCDIC_CHARSET
113 + const char from[] = "\\\"\n\t\b\r\f\a\v\x27"; /* EBCDIC escape */
114 +#else
115 + const char from[] = "\\\"\n\t\b\r\f\a\v\x1B"; /* ASCII escape */
116 +#endif
117 + if ((e = strchr(from, src[k]))) {
118 + if (l < sizeof s - 2) {
119 + s[l++] = '\\';
120 + s[l] = to[e - from];
123 + else if (isprint(src[k])) {
124 + s[l] = src[k];
126 + else {
127 + s[l] = '?';
130 - s[k] = 0;
131 - printd("s=\"%s\"%s[%d]", s,
132 + s[l] = 0;
133 + printd("<string> \"%s\"%s[%d]", s,
134 src[k] ? "..." : "", strlen(src));
137 break;
138 case ARRAY_TAG:
139 - printd("%08p <array>[%d]", dv.val.arrayPtr, ArraySize(&dv));
140 + printd("<array> %8p[%d]", dv.val.arrayPtr, ArraySize(&dv));
141 break;
142 case NO_TAG:
143 if (!dv.val.inst) {
144 printd("<no value>");
146 @@ -3698,99 +3719,113 @@ static void disasmInternal(Inst *inst, i
147 "SWAP_TOP2", /* swapTop2: cf namedArgN */
148 "SUBR_CALL_STACKED_N", /* callSubroutineStackedN */
149 "UNPACKTOARGS", /* unpackArrayToArgs */
151 int i, j;
152 + static size_t opLen;
154 - printd("\n");
155 + if (!opLen) {
156 + for (j = 0; j < N_OPS; ++j) {
157 + if (opLen < strlen(opNames[j])) {
158 + opLen = strlen(opNames[j]);
163 for (i = 0; i < nInstr; ++i) {
164 - printd("Prog %8p ", &inst[i]);
165 + printd("Prog %8p", &inst[i]);
166 for (j = 0; j < N_OPS; ++j) {
167 if (inst[i].func == OpFns[j]) {
168 - printd("%22s ", opNames[j]);
169 + printd(" %*s", (int)opLen, opNames[j]);
170 if (j == OP_PUSH_SYM || j == OP_ASSIGN) {
171 Symbol *sym = inst[i+1].sym;
172 - printd("%s", sym->name);
173 + printd(" %s", sym->name);
174 if (sym->value.tag == STRING_TAG &&
175 strncmp(sym->name, "string #", 8) == 0) {
176 + printd(" ");
177 dumpVal(sym->value);
179 ++i;
181 else if (j == OP_BRANCH || j == OP_BRANCH_FALSE ||
182 j == OP_BRANCH_NEVER || j == OP_BRANCH_TRUE) {
183 - printd("to=(%d) %p", inst[i+1].value,
184 + printd(" to=(%+d) %8p", inst[i+1].value,
185 &inst[i+1] + inst[i+1].value);
186 ++i;
188 else if (j == OP_CONCAT) {
189 - printd("nExpr=%d", inst[i+1].value);
190 + printd(" nExpr=%d", inst[i+1].value);
191 ++i;
193 else if (j == OP_SUBR_CALL) {
194 int args = inst[i+2].value;
195 - printd("%s ", inst[i+1].sym->name);
196 + printd(" %s", inst[i+1].sym->name);
197 if (args < 0) {
198 - printd("%d+args[] (%d)", -args - 1, args);
199 + printd(" %d+args[] (%d)", -args - 1, args);
201 else {
202 - printd("%d args", args);
203 + printd(" %d args", args);
205 i += 2;
207 else if (j == OP_SUBR_CALL_STACKED_N) {
208 - printd("%s args[] (?)", inst[i+1].sym->name);
209 + printd(" %s args[] (?)", inst[i+1].sym->name);
210 ++i;
212 else if (j == OP_BEGIN_ARRAY_ITER) {
213 - printd("%s in", inst[i+1].sym->name);
214 + printd(" %s in", inst[i+1].sym->name);
215 ++i;
217 else if (j == OP_ARRAY_ITER) {
218 - printd("%s = %s++ end-loop=(%d) %p",
219 + printd(" %s = %s++ end-loop=(%+d) %8p",
220 inst[i+1].sym->name,
221 inst[i+2].sym->name,
222 - inst[i+3].value, &inst[i+3] + inst[i+3].value);
223 + inst[i+3].value,
224 + &inst[i+3] + inst[i+3].value);
225 i += 3;
227 else if (j == OP_ARRAY_REF ||
228 j == OP_ARRAY_DELETE ||
229 j == OP_ARRAY_ASSIGN ||
230 j == OP_ANONARRAY_INDEX_VAL ||
231 j == OP_NAMED_ARG1 ||
232 j == OP_NAMED_ARGN) {
233 - printd("nDim=%d", inst[i+1].value);
234 + printd(" nDim=%d", inst[i+1].value);
235 ++i;
237 else if (j == OP_ARRAY_REF_ASSIGN_SETUP) {
238 - printd("binOp=%s ", inst[i+1].value ? "true" : "false");
239 - printd("nDim=%d", inst[i+2].value);
240 + printd(" binOp=%s nDim=%d",
241 + inst[i+1].value ? "true" : "false",
242 + inst[i+2].value);
243 i += 2;
245 else if (j == OP_PUSH_ARRAY_SYM) {
246 - printd("%s", inst[++i].sym->name);
247 - printd(" %s", inst[i+1].value ? "createAndRef" : "refOnly");
248 - ++i;
249 + printd(" %s %s",
250 + inst[i+1].sym->name,
251 + inst[i+2].value ? "createAndRef" : "refOnly");
252 + i += 2;
255 printd("\n");
256 break;
259 if (j == N_OPS) {
260 - printd("%x\n", inst[i].value);
261 + printd(" %x\n", inst[i].value);
266 -static void disasm(Inst *inst, int nInstr)
267 +static void disasm(const char *name, Inst *inst, int nInstr)
269 static int outIsTTY = -1;
270 if (outIsTTY == -1) outIsTTY = isatty(fileno(stdout));
271 if (outIsTTY) { printd("\033[H"); }
272 + if (name) printd(">> %s\n", name);
273 disasmInternal(inst, nInstr);
274 if (outIsTTY) { printd("\033[J\n"); }
275 + if (name) printd("\n");
276 outPrintd();
278 #endif /* #ifdef DEBUG_DISASSEMBLER */
280 #ifdef DEBUG_STACK /* for run-time stack dumping */
281 @@ -3914,11 +3949,11 @@ static void stackdumpInternal(int n, int
282 printd("--------------Stack base--------------\n");
283 #else
284 if (outpt < TheStack)
285 printd("--------------Stack base--------------\n");
286 stackdumpframe(arrow, outpt, FrameP, StackP, '*');
287 - printd("Stack ----->\n");
288 + printd("Stack ----->\n\n");
289 #endif /* #ifdef DEBUG_STACK_HEADFIRST */
292 static void stackdump(int n, int extra)
294 diff --quilt old/source/interpret.h new/source/interpret.h
295 --- old/source/interpret.h
296 +++ new/source/interpret.h
297 @@ -149,11 +149,11 @@ Inst *GetPC(void);
298 Symbol *InstallIteratorSymbol(void);
299 Symbol *LookupStringConstSymbol(const char *value);
300 Symbol *InstallStringConstSymbol(const char *str);
301 Symbol *LookupSymbol(const char *name);
302 Symbol *InstallSymbol(const char *name, enum symTypes type, DataValue value);
303 -Program *FinishCreatingProgram(void);
304 +Program *FinishCreatingProgram(const char *name);
305 void SwapCode(Inst *start, Inst *boundary, Inst *end);
306 void StartLoopAddrList(void);
307 int AddBreakAddr(Inst *addr);
308 int AddContinueAddr(Inst *addr);
309 void FillLoopAddrs(Inst *breakAddr, Inst *continueAddr);
310 diff --quilt old/source/parse.y new/source/parse.y
311 --- old/source/parse.y
312 +++ new/source/parse.y
313 @@ -525,33 +525,29 @@ blank: /* nothing */
314 ** to where parsing failed in stoppedAt.
316 Program *ParseMacro(char *expr, char **msg, char **stoppedAt, const char *name)
318 Program *prog;
319 - static const char *prefix = ">> ";
321 BeginCreatingProgram();
323 + if (!name)
324 + name = "--unknown--";
326 /* call yyparse to parse the string and check for success. If the parse
327 failed, return the error message and string index (the grammar aborts
328 parsing at the first error) */
329 InPtr = expr;
330 if (yyparse()) {
331 *msg = ErrMsg;
332 *stoppedAt = InPtr;
333 - FreeProgram(FinishCreatingProgram());
334 + FreeProgram(FinishCreatingProgram(NULL));
335 return NULL;
338 /* get the newly created program */
339 - prog = FinishCreatingProgram();
341 - if (!name)
342 - name = "--unknown--";
344 - prog->name = XtMalloc(strlen(name) + strlen(prefix) + 1);
345 - strcat(strcpy(prog->name, prefix), name);
346 + prog = FinishCreatingProgram(name);
348 /* parse succeeded */
349 *msg = "";
350 *stoppedAt = InPtr;
351 return prog;
352 diff --quilt old/source/userCmds.c new/source/userCmds.c
353 --- old/source/userCmds.c
354 +++ new/source/userCmds.c
355 @@ -1284,17 +1284,17 @@ static int doMacroMenuCmd(WindowInfo *wi
358 int DoNamedMacroMenuCmd(WindowInfo *window, const char *itemName)
360 return doMacroMenuCmd(window, itemName, MacroMenuItems, NMacroMenuItems,
361 - "macro-menu>");
362 + "Macro Menu>");
365 int DoNamedBGMenuCmd(WindowInfo *window, const char *itemName)
367 return doMacroMenuCmd(window, itemName, BGMenuItems, NBGMenuItems,
368 - "background-menu>");
369 + "Background Menu>");
373 ** Cache user menus:
374 ** Rebuild all of the Shell, Macro, Background menus of given editor window.