disable OM2.3.1 by default, enable with make bertw OM231=1
[nedit-bw.git] / InterpretDebug-mods.patch
bloba379aa991d1d0e467dcff7a5051439ad032a3549
1 Subject: modifications to the InterpretDebug patch
3 ---
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 */
29 -#define DISASM(i, n)
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)
66 /* save state */
67 acc->localSymList = LocalSymList;
68 memcpy(acc->prog, Prog, sizeof(Inst) * PROGRAM_SIZE);
69 acc->progP = ProgP;
70 memcpy(acc->loopStack, LoopStack, sizeof(Inst) * LOOP_STACK_SIZE);
71 acc->loopStackPtr = LoopStackPtr;
72 + acc->name = ProgramName;
74 LocalSymList = NULL;
75 ProgP = Prog;
76 LoopStackPtr = LoopStack;
77 + ProgramName = name;
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
85 Program *newProg;
86 int progLen, fpOffset = 0;
87 Symbol *s;
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);
107 /* restore state */
108 LocalSymList = acc->localSymList;
109 memcpy(Prog, acc->prog, sizeof(Inst) * PROGRAM_SIZE);
110 ProgP = acc->progP;
111 memcpy(LoopStack, acc->loopStack, sizeof(Inst) * LOOP_STACK_SIZE);
112 LoopStackPtr = acc->loopStackPtr;
113 + ProgramName = acc->name;
115 return newProg;
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
130 src->color = -1;
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)
146 switch (dv.tag) {
147 case INT_TAG:
148 - printd("i=%d", dv.val.n);
149 + printd("<integer> %d", dv.val.n);
150 break;
151 case STRING_TAG:
153 - int k;
154 - char s[21];
155 + int k, l;
156 + char s[64];
157 char *src = dv.val.str.rep;
158 if (!src) {
159 - printd("s=<NULL>");
160 + printd("<string> <NULL>");
162 else {
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++) {
166 + char *e;
167 + const char to[] = "\\\"ntbrfave";
168 +#ifdef EBCDIC_CHARSET
169 + const char from[] = "\\\"\n\t\b\r\f\a\v\x27"; /* EBCDIC escape */
170 +#else
171 + const char from[] = "\\\"\n\t\b\r\f\a\v\x1B"; /* ASCII escape */
172 +#endif
173 + if ((e = strchr(from, src[k]))) {
174 + if (l < sizeof s - 2) {
175 + s[l++] = '\\';
176 + s[l] = to[e - from];
179 + else if (isprint(src[k])) {
180 + s[l] = src[k];
182 + else {
183 + s[l] = '?';
186 - s[k] = 0;
187 - printd("s=\"%s\"%s[%d]", s,
188 + s[l] = 0;
189 + printd("<string> \"%s\"%s[%d]", s,
190 src[k] ? "..." : "", strlen(src));
193 break;
194 case ARRAY_TAG:
195 - printd("%08p <array>[%d]", dv.val.arrayPtr, ArraySize(&dv));
196 + printd("<array> %8p[%d]", dv.val.arrayPtr, ArraySize(&dv));
197 break;
198 case NO_TAG:
199 if (!dv.val.inst) {
200 printd("<no value>");
202 @@ -3811,61 +3833,70 @@ static void disasmInternal(Inst *inst, i
203 #define OP(name, fn) #name,
204 #include "ops.h"
205 #undef OP
207 int i, j;
208 + static size_t opLen;
210 - printd("\n");
211 + if (!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) {
232 + printd(" ");
233 dumpVal(sym->value);
235 ++i;
237 else if (j == OP_PUSH_IMMED) {
238 - printd("%d", inst[i+1].value);
239 + printd(" %d", inst[i+1].value);
240 ++i;
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);
247 ++i;
249 else if (j == OP_CONCAT) {
250 - printd("nExpr=%d", inst[i+1].value);
251 + printd(" nExpr=%d", inst[i+1].value);
252 ++i;
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);
258 if (args < 0) {
259 - printd("%d+args[] (%d)", -args - 1, args);
260 + printd(" %d+args[] (%d)", -args - 1, args);
262 else {
263 - printd("%d args", args);
264 + printd(" %d args", args);
266 i += 2;
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);
271 ++i;
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);
276 ++i;
278 else if (j == OP_ARRAY_ITER) {
279 - printd("%s = %s++ end-loop=(%d) %p",
280 + printd(" %s = %s++ end-loop=(%+d) %8p",
281 inst[i+1].sym->name,
282 inst[i+2].sym->name,
283 inst[i+3].value, &inst[i+3] + inst[i+3].value);
284 i += 3;
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);
294 ++i;
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",
301 + inst[i+2].value);
302 i += 2;
304 else if (j == OP_PUSH_ARRAY_SYM) {
305 - printd("%s", inst[++i].sym->name);
306 - printd(" %s", inst[i+1].value ? "createAndRef" : "refOnly");
307 - ++i;
308 + printd(" %s %s",
309 + inst[i+1].sym->name,
310 + inst[i+2].value ? "createAndRef" : "refOnly");
311 + i += 2;
314 printd("\n");
315 break;
318 if (j == N_OPS) {
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");
335 outPrintd();
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");
342 #else
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;
360 Inst *code;
361 unsigned refcount;
362 - char *name;
363 + char name[1];
364 } Program;
366 /* Information needed to re-start a preempted macro */
367 typedef struct {
368 DataValue *stack;
369 @@ -126,10 +126,11 @@ typedef struct AccumulatorDataTag {
370 Symbol *localSymList;
371 Inst prog[PROGRAM_SIZE];
372 Inst *progP;
373 Inst *loopStack[LOOP_STACK_SIZE];
374 Inst **loopStackPtr;
375 + const char *name;
376 } AccumulatorData;
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);
393 Inst *GetPC(void);
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 */;
399 } else {
400 yyerror("try to override built-in subroutine"); YYERROR;
402 $4 = PromoteToGlobal($4);
403 - BeginCreatingProgram($1);
404 + BeginCreatingProgram($4->name, $1);
406 blank blockwb {
407 ADD_OP(OP_RETURN_NO_VAL);
408 Program *prog = FinishCreatingProgram($1);
409 XtFree((char *)$1);
410 @@ -598,13 +598,15 @@ blank: /* nothing */
411 Program *ParseMacro(char *expr, char **msg, char **stoppedAt, int allowDefine,
412 const char *name)
414 Program *prog;
415 AccumulatorData *acc = (AccumulatorData *)XtMalloc(sizeof(*acc));
416 - static const char *prefix = ">> ";
418 - BeginCreatingProgram(acc);
419 + if (!name)
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);
432 XtFree((char *)acc);
434 - if (!name)
435 - name = "--unknown--";
437 - prog->name = XtMalloc(strlen(name) + strlen(prefix) + 1);
438 - strcat(strcpy(prog->name, prefix), name);
440 /* parse succeeded */
441 *msg = "";
442 *stoppedAt = InPtr;
443 return prog;
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,
454 - "macro-menu>");
455 + "Macro Menu>");
458 int DoNamedBGMenuCmd(WindowInfo *window, const char *itemName)
460 return doMacroMenuCmd(window, itemName, BGMenuItems, NBGMenuItems,
461 - "background-menu>");
462 + "Background Menu>");
466 ** Cache user menus:
467 ** Rebuild all of the Shell, Macro, Background menus of given editor window.