3 source/interpret.c | 113 ++++++++++++++++++++++++++++++++++-------------------
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 */
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)
52 int progLen, fpOffset = 0;
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);
65 + newProg->name = strdup(name);
67 + DISASM(newProg->name, newProg->code, ProgP - Prog);
72 void FreeProgram(Program *prog)
73 @@ -2919,11 +2922,11 @@ static void arrayDisposeNode(rbTreeNode
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)
92 - printd("i=%d", dv.val.n);
93 + printd("<integer> %d", dv.val.n);
101 char *src = dv.val.str.rep;
103 - printd("s=<NULL>");
104 + printd("<string> <NULL>");
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++) {
111 + const char to[] = "\\\"ntbrfave";
112 +#ifdef EBCDIC_CHARSET
113 + const char from[] = "\\\"\n\t\b\r\f\a\v\x27"; /* EBCDIC escape */
115 + const char from[] = "\\\"\n\t\b\r\f\a\v\x1B"; /* ASCII escape */
117 + if ((e = strchr(from, src[k]))) {
118 + if (l < sizeof s - 2) {
120 + s[l] = to[e - from];
123 + else if (isprint(src[k])) {
131 - printd("s=\"%s\"%s[%d]", s,
133 + printd("<string> \"%s\"%s[%d]", s,
134 src[k] ? "..." : "", strlen(src));
139 - printd("%08p <array>[%d]", dv.val.arrayPtr, ArraySize(&dv));
140 + printd("<array> %8p[%d]", dv.val.arrayPtr, ArraySize(&dv));
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 */
152 + static size_t 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) {
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);
188 else if (j == OP_CONCAT) {
189 - printd("nExpr=%d", inst[i+1].value);
190 + printd(" nExpr=%d", inst[i+1].value);
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);
198 - printd("%d+args[] (%d)", -args - 1, args);
199 + printd(" %d+args[] (%d)", -args - 1, args);
202 - printd("%d args", args);
203 + printd(" %d args", args);
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);
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);
217 else if (j == OP_ARRAY_ITER) {
218 - printd("%s = %s++ end-loop=(%d) %p",
219 + printd(" %s = %s++ end-loop=(%+d) %8p",
222 - inst[i+3].value, &inst[i+3] + inst[i+3].value);
224 + &inst[i+3] + inst[i+3].value);
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);
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",
245 else if (j == OP_PUSH_ARRAY_SYM) {
246 - printd("%s", inst[++i].sym->name);
247 - printd(" %s", inst[i+1].value ? "createAndRef" : "refOnly");
250 + inst[i+1].sym->name,
251 + inst[i+2].value ? "createAndRef" : "refOnly");
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");
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");
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)
319 - static const char *prefix = ">> ";
321 BeginCreatingProgram();
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) */
333 - FreeProgram(FinishCreatingProgram());
334 + FreeProgram(FinishCreatingProgram(NULL));
338 /* get the newly created program */
339 - prog = FinishCreatingProgram();
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 */
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,
365 int DoNamedBGMenuCmd(WindowInfo *window, const char *itemName)
367 return doMacroMenuCmd(window, itemName, BGMenuItems, NBGMenuItems,
368 - "background-menu>");
369 + "Background Menu>");
374 ** Rebuild all of the Shell, Macro, Background menus of given editor window.