1 /*******************************************************************
3 ** Forth Inspired Command Language - programming tools
4 ** Author: John Sadler (john_sadler@alum.mit.edu)
5 ** Created: 20 June 2000
6 ** $Id: tools.c,v 1.11 2001/12/05 07:21:34 jsadler Exp $
7 *******************************************************************/
9 ** Copyright (c) 1997-2001 John Sadler (john_sadler@alum.mit.edu)
10 ** All rights reserved.
12 ** Get the latest Ficl release at http://ficl.sourceforge.net
14 ** I am interested in hearing from anyone who uses ficl. If you have
15 ** a problem, a success story, a defect, an enhancement request, or
16 ** if you would like to contribute to the ficl release, please
17 ** contact me by email at the address above.
19 ** L I C E N S E and D I S C L A I M E R
21 ** Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions
24 ** 1. Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** 2. Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in the
28 ** documentation and/or other materials provided with the distribution.
30 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
31 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
34 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 ** SEE needs information about the addresses of functions that
46 ** are the CFAs of colon definitions, constants, variables, DOES>
47 ** words, and so on. It gets this information from a table and supporting
48 ** functions in words.c.
49 ** colonParen doDoes createParen variableParen userParen constantParen
51 ** Step and break debugger for Ficl
52 ** debug ( xt -- ) Start debugging an xt
54 ** Specify breakpoint default action
61 #include <stdio.h> /* sprintf */
72 ** nBREAKPOINTS sizes the breakpoint array. One breakpoint (bp 0) is reserved
73 ** for the STEP command. The rest are user programmable.
75 #define nBREAKPOINTS 32
80 /**************************************************************************
82 ** Set a breakpoint at the current value of IP by
83 ** storing that address in a BREAKPOINT record
84 **************************************************************************/
85 static void vmSetBreak(FICL_VM *pVM, FICL_BREAKPOINT *pBP)
87 FICL_WORD *pStep = ficlLookup(pVM->pSys, "step-break");
90 pBP->address = pVM->ip;
91 pBP->origXT = *pVM->ip;
96 /**************************************************************************
97 ** d e b u g P r o m p t
98 **************************************************************************/
99 static void debugPrompt(FICL_VM *pVM)
101 vmTextOut(pVM, "dbg> ", 0);
105 /**************************************************************************
106 ** i s A F i c l W o r d
107 ** Vet a candidate pointer carefully to make sure
108 ** it's not some chunk o' inline data...
109 ** It has to have a name, and it has to look
110 ** like it's in the dictionary address range.
111 ** NOTE: this excludes :noname words!
112 **************************************************************************/
113 int isAFiclWord(FICL_DICT *pd, FICL_WORD *pFW)
116 if (!dictIncludes(pd, pFW))
119 if (!dictIncludes(pd, pFW->name))
122 if ((pFW->link != NULL) && !dictIncludes(pd, pFW->link))
125 if ((pFW->nName <= 0) || (pFW->name[pFW->nName] != '\0'))
128 if (strlen(pFW->name) != pFW->nName)
136 static int isPrimitive(FICL_WORD *pFW)
138 WORDKIND wk = ficlWordClassify(pFW);
139 return ((wk != COLON) && (wk != DOES));
144 /**************************************************************************
145 f i n d E n c l o s i n g W o r d
146 ** Given a pointer to something, check to make sure it's an address in the
147 ** dictionary. If so, search backwards until we find something that looks
148 ** like a dictionary header. If successful, return the address of the
149 ** FICL_WORD found. Otherwise return NULL.
150 ** nSEARCH_CELLS sets the maximum neighborhood this func will search before giving up
151 **************************************************************************/
152 #define nSEARCH_CELLS 100
154 static FICL_WORD *findEnclosingWord(FICL_VM *pVM, CELL *cp)
157 FICL_DICT *pd = vmGetDict(pVM);
160 if (!dictIncludes(pd, (void *)cp))
163 for (i = nSEARCH_CELLS; i > 0; --i, --cp)
165 pFW = (FICL_WORD *)(cp + 1 - (sizeof (FICL_WORD) / sizeof (CELL)));
166 if (isAFiclWord(pd, pFW))
174 /**************************************************************************
176 ** TOOLS ( "<spaces>name" -- )
177 ** Display a human-readable representation of the named word's definition.
178 ** The source of the representation (object-code decompilation, source
179 ** block, etc.) and the particular form of the display is implementation
181 **************************************************************************/
183 ** seeColon (for proctologists only)
184 ** Walks a colon definition, decompiling
185 ** on the fly. Knows about primitive control structures.
187 static void seeColon(FICL_VM *pVM, CELL *pc)
191 FICL_DICT *pd = vmGetDict(pVM);
192 FICL_WORD *pSemiParen = ficlLookup(pVM->pSys, "(;)");
195 for (; pc->p != pSemiParen; pc++)
197 FICL_WORD *pFW = (FICL_WORD *)(pc->p);
200 if ((void *)pc == (void *)pVM->ip)
204 cp += sprintf(cp, "%3d ", pc-param0);
206 if (isAFiclWord(pd, pFW))
208 WORDKIND kind = ficlWordClassify(pFW);
215 if (isAFiclWord(pd, c.p))
217 FICL_WORD *pLit = (FICL_WORD *)c.p;
218 sprintf(cp, "%.*s ( %#lx literal )",
219 pLit->nName, pLit->name, c.u);
222 sprintf(cp, "literal %ld (%#lx)", c.i, c.u);
226 FICL_STRING *sp = (FICL_STRING *)(void *)++pc;
227 pc = (CELL *)alignPtr(sp->text + sp->count + 1) - 1;
228 sprintf(cp, "s\" %.*s\"", sp->count, sp->text);
233 FICL_STRING *sp = (FICL_STRING *)(void *)++pc;
234 pc = (CELL *)alignPtr(sp->text + sp->count + 1) - 1;
235 sprintf(cp, "c\" %.*s\"", sp->count, sp->text);
241 sprintf(cp, "if / while (branch %d)", pc+c.i-param0);
243 sprintf(cp, "until (branch %d)", pc+c.i-param0);
248 sprintf(cp, "repeat (branch %d)", pc+c.i-param0);
250 sprintf(cp, "else (branch %d)", pc+c.i-param0);
252 sprintf(cp, "endof (branch %d)", pc+c.i-param0);
257 sprintf(cp, "of (branch %d)", pc+c.i-param0);
262 sprintf(cp, "?do (leave %d)", (CELL *)c.p-param0);
266 sprintf(cp, "do (leave %d)", (CELL *)c.p-param0);
270 sprintf(cp, "loop (branch %d)", pc+c.i-param0);
274 sprintf(cp, "+loop (branch %d)", pc+c.i-param0);
277 sprintf(cp, "%.*s", pFW->nName, pFW->name);
282 else /* probably not a word - punt and print value */
284 sprintf(cp, "%ld ( %#lx )", pc->i, pc->u);
287 vmTextOut(pVM, pVM->pad, 1);
290 vmTextOut(pVM, ";", 1);
294 ** Here's the outer part of the decompiler. It's
295 ** just a big nested conditional that checks the
296 ** CFA of the word to decompile for each kind of
297 ** known word-builder code, and tries to do
298 ** something appropriate. If the CFA is not recognized,
299 ** just indicate that it is a primitive.
301 static void seeXT(FICL_VM *pVM)
306 pFW = (FICL_WORD *)stackPopPtr(pVM->pStack);
307 kind = ficlWordClassify(pFW);
312 sprintf(pVM->pad, ": %.*s", pFW->nName, pFW->name);
313 vmTextOut(pVM, pVM->pad, 1);
314 seeColon(pVM, pFW->param);
318 vmTextOut(pVM, "does>", 1);
319 seeColon(pVM, (CELL *)pFW->param->p);
323 vmTextOut(pVM, "create", 1);
327 sprintf(pVM->pad, "variable = %ld (%#lx)", pFW->param->i, pFW->param->u);
328 vmTextOut(pVM, pVM->pad, 1);
333 sprintf(pVM->pad, "user variable %ld (%#lx)", pFW->param->i, pFW->param->u);
334 vmTextOut(pVM, pVM->pad, 1);
339 sprintf(pVM->pad, "constant = %ld (%#lx)", pFW->param->i, pFW->param->u);
340 vmTextOut(pVM, pVM->pad, 1);
343 sprintf(pVM->pad, "%.*s is a primitive", pFW->nName, pFW->name);
344 vmTextOut(pVM, pVM->pad, 1);
348 if (pFW->flags & FW_IMMEDIATE)
350 vmTextOut(pVM, "immediate", 1);
353 if (pFW->flags & FW_COMPILE)
355 vmTextOut(pVM, "compile-only", 1);
362 static void see(FICL_VM *pVM)
370 /**************************************************************************
371 f i c l D e b u g X T
373 ** Given an xt of a colon definition or a word defined by DOES>, set the
374 ** VM up to debug the word: push IP, set the xt as the next thing to execute,
375 ** set a breakpoint at its first instruction, and run to the breakpoint.
376 ** Note: the semantics of this word are equivalent to "step in"
377 **************************************************************************/
378 void ficlDebugXT(FICL_VM *pVM)
380 FICL_WORD *xt = stackPopPtr(pVM->pStack);
381 WORDKIND wk = ficlWordClassify(xt);
383 stackPushPtr(pVM->pStack, xt);
391 ** Run the colon code and set a breakpoint at the next instruction
394 vmSetBreak(pVM, &(pVM->pSys->bpStep));
406 /**************************************************************************
409 ** Execute the next instruction, stepping into it if it's a colon definition
410 ** or a does> word. This is the easy kind of step.
411 **************************************************************************/
412 void stepIn(FICL_VM *pVM)
415 ** Do one step of the inner loop
422 ** Now set a breakpoint at the next instruction
424 vmSetBreak(pVM, &(pVM->pSys->bpStep));
430 /**************************************************************************
433 ** Execute the next instruction atomically. This requires some insight into
434 ** the memory layout of compiled code. Set a breakpoint at the next instruction
435 ** in this word, and run until we hit it
436 **************************************************************************/
437 void stepOver(FICL_VM *pVM)
441 FICL_WORD *pStep = ficlLookup(pVM->pSys, "step-break");
445 kind = ficlWordClassify(pFW);
452 ** assume that the next cell holds an instruction
453 ** set a breakpoint there and return to the inner interp
455 pVM->pSys->bpStep.address = pVM->ip + 1;
456 pVM->pSys->bpStep.origXT = pVM->ip[1];
469 /**************************************************************************
472 ** Handles breakpoints for stepped execution.
473 ** Upon entry, bpStep contains the address and replaced instruction
474 ** of the current breakpoint.
475 ** Clear the breakpoint
476 ** Get a command from the console.
477 ** i (step in) - execute the current instruction and set a new breakpoint
479 ** o (step over) - execute the current instruction to completion and set
480 ** a new breakpoint at the IP
481 ** g (go) - execute the current instruction and exit
482 ** q (quit) - abort current word
483 ** b (toggle breakpoint)
484 **************************************************************************/
485 void stepBreak(FICL_VM *pVM)
493 assert(pVM->pSys->bpStep.address);
494 assert(pVM->pSys->bpStep.origXT);
496 ** Clear the breakpoint that caused me to run
497 ** Restore the original instruction at the breakpoint,
498 ** and restore the IP
500 pVM->ip = (IPTYPE)(pVM->pSys->bpStep.address);
501 *pVM->ip = pVM->pSys->bpStep.origXT;
504 ** If there's an onStep, do it
506 pOnStep = ficlLookup(pVM->pSys, "on-step");
508 ficlExecXT(pVM, pOnStep);
511 ** Print the name of the next instruction
513 pFW = pVM->pSys->bpStep.origXT;
514 sprintf(pVM->pad, "next: %.*s", pFW->nName, pFW->name);
516 if (isPrimitive(pFW))
518 strcat(pVM->pad, " ( primitive )");
522 vmTextOut(pVM, pVM->pad, 1);
532 if (!strincmp(si.cp, "i", si.count))
536 else if (!strincmp(si.cp, "g", si.count))
540 else if (!strincmp(si.cp, "l", si.count))
543 xt = findEnclosingWord(pVM, (CELL *)(pVM->ip));
546 stackPushPtr(pVM->pStack, xt);
551 vmTextOut(pVM, "sorry - can't do that", 1);
553 vmThrow(pVM, VM_RESTART);
555 else if (!strincmp(si.cp, "o", si.count))
559 else if (!strincmp(si.cp, "q", si.count))
561 ficlTextOut(pVM, FICL_PROMPT, 0);
562 vmThrow(pVM, VM_ABORT);
564 else if (!strincmp(si.cp, "x", si.count))
567 ** Take whatever's left in the TIB and feed it to a subordinate ficlExec
570 char *cp = pVM->tib.cp + pVM->tib.index;
571 int count = pVM->tib.end - cp;
572 FICL_WORD *oldRun = pVM->runningWord;
574 ret = ficlExecC(pVM, cp, count);
576 if (ret == VM_OUTOFTEXT)
579 pVM->runningWord = oldRun;
580 vmTextOut(pVM, "", 1);
587 vmTextOut(pVM, "i -- step In", 1);
588 vmTextOut(pVM, "o -- step Over", 1);
589 vmTextOut(pVM, "g -- Go (execute to completion)", 1);
590 vmTextOut(pVM, "l -- List source code", 1);
591 vmTextOut(pVM, "q -- Quit (stop debugging and abort)", 1);
592 vmTextOut(pVM, "x -- eXecute the rest of the line as ficl words", 1);
594 vmThrow(pVM, VM_RESTART);
601 /**************************************************************************
604 ** Signal the system to shut down - this causes ficlExec to return
605 ** VM_USEREXIT. The rest is up to you.
606 **************************************************************************/
607 static void bye(FICL_VM *pVM)
609 vmThrow(pVM, VM_USEREXIT);
614 /**************************************************************************
615 d i s p l a y S t a c k
617 ** Display the parameter stack (code for ".s")
618 **************************************************************************/
619 static void displayPStack(FICL_VM *pVM)
621 FICL_STACK *pStk = pVM->pStack;
622 int d = stackDepth(pStk);
626 vmCheckStack(pVM, 0, 0);
629 vmTextOut(pVM, "(Stack Empty) ", 0);
633 for (i = 0; i < d; i++)
635 vmTextOut(pVM, ltoa((*pCell++).i, pVM->pad, pVM->base), 0);
636 vmTextOut(pVM, " ", 0);
643 static void displayRStack(FICL_VM *pVM)
645 FICL_STACK *pStk = pVM->rStack;
646 int d = stackDepth(pStk);
649 FICL_DICT *dp = vmGetDict(pVM);
651 vmCheckStack(pVM, 0, 0);
654 vmTextOut(pVM, "(Stack Empty) ", 0);
658 for (i = 0; i < d; i++)
662 ** Attempt to find the word that contains the
663 ** stacked address (as if it is part of a colon definition).
664 ** If this works, print the name of the word. Otherwise print
665 ** the value as a number.
667 if (dictIncludes(dp, c.p))
669 FICL_WORD *pFW = findEnclosingWord(pVM, c.p);
672 int offset = (CELL *)c.p - &pFW->param[0];
673 sprintf(pVM->pad, "%s+%d ", pFW->name, offset);
674 vmTextOut(pVM, pVM->pad, 0);
675 continue; /* no need to print the numeric value */
678 vmTextOut(pVM, ltoa(c.i, pVM->pad, pVM->base), 0);
679 vmTextOut(pVM, " ", 0);
687 /**************************************************************************
690 **************************************************************************/
691 static void forgetWid(FICL_VM *pVM)
693 FICL_DICT *pDict = vmGetDict(pVM);
696 pHash = (FICL_HASH *)stackPopPtr(pVM->pStack);
697 hashForget(pHash, pDict->here);
703 /**************************************************************************
705 ** TOOLS EXT ( "<spaces>name" -- )
706 ** Skip leading space delimiters. Parse name delimited by a space.
707 ** Find name, then delete name from the dictionary along with all
708 ** words added to the dictionary after name. An ambiguous
709 ** condition exists if name cannot be found.
711 ** If the Search-Order word set is present, FORGET searches the
712 ** compilation word list. An ambiguous condition exists if the
713 ** compilation word list is deleted.
714 **************************************************************************/
715 static void forget(FICL_VM *pVM)
718 FICL_DICT *pDict = vmGetDict(pVM);
719 FICL_HASH *pHash = pDict->pCompile;
722 where = ((FICL_WORD *)stackPopPtr(pVM->pStack))->name;
723 hashForget(pHash, where);
724 pDict->here = PTRtoCELL where;
730 /**************************************************************************
733 **************************************************************************/
735 static void listWords(FICL_VM *pVM)
737 FICL_DICT *dp = vmGetDict(pVM);
738 FICL_HASH *pHash = dp->pSearch[dp->nLists - 1];
746 char *pPad = pVM->pad;
748 for (i = 0; i < pHash->size; i++)
750 for (wp = pHash->table[i]; wp != NULL; wp = wp->link, nWords++)
752 if (wp->nName == 0) /* ignore :noname defs */
756 nChars += sprintf(pPad + nChars, "%s", cp);
765 vmTextOut(pVM, "--- Press Enter to continue ---",0);
767 vmTextOut(pVM,"\r",0);
769 vmTextOut(pVM, pPad, 1);
773 len = nCOLWIDTH - nChars % nCOLWIDTH;
775 pPad[nChars++] = ' ';
785 vmTextOut(pVM, "--- Press Enter to continue ---",0);
787 vmTextOut(pVM,"\r",0);
789 vmTextOut(pVM, pPad, 1);
798 vmTextOut(pVM, pPad, 1);
801 sprintf(pVM->pad, "Dictionary: %d words, %ld cells used of %u total",
802 nWords, (long) (dp->here - dp->dict), dp->size);
803 vmTextOut(pVM, pVM->pad, 1);
808 /**************************************************************************
810 ** Print symbols defined in the environment
811 **************************************************************************/
812 static void listEnv(FICL_VM *pVM)
814 FICL_DICT *dp = pVM->pSys->envp;
815 FICL_HASH *pHash = dp->pForthWords;
820 for (i = 0; i < pHash->size; i++)
822 for (wp = pHash->table[i]; wp != NULL; wp = wp->link, nWords++)
824 vmTextOut(pVM, wp->name, 1);
828 sprintf(pVM->pad, "Environment: %d words, %ld cells used of %u total",
829 nWords, (long) (dp->here - dp->dict), dp->size);
830 vmTextOut(pVM, pVM->pad, 1);
835 /**************************************************************************
836 e n v C o n s t a n t
837 ** Ficl interface to ficlSetEnv and ficlSetEnvD - allow ficl code to set
838 ** environment constants...
839 **************************************************************************/
840 static void envConstant(FICL_VM *pVM)
845 vmCheckStack(pVM, 1, 0);
850 ficlSetEnv(pVM->pSys, pVM->pad, (FICL_UNS)value);
854 static void env2Constant(FICL_VM *pVM)
859 vmCheckStack(pVM, 2, 0);
865 ficlSetEnvD(pVM->pSys, pVM->pad, v1, v2);
870 /**************************************************************************
871 f i c l C o m p i l e T o o l s
872 ** Builds wordset for debugger and TOOLS optional word set
873 **************************************************************************/
875 void ficlCompileTools(FICL_SYSTEM *pSys)
877 FICL_DICT *dp = pSys->dp;
881 ** TOOLS and TOOLS EXT
883 dictAppendWord(dp, ".s", displayPStack, FW_DEFAULT);
884 dictAppendWord(dp, "bye", bye, FW_DEFAULT);
885 dictAppendWord(dp, "forget", forget, FW_DEFAULT);
886 dictAppendWord(dp, "see", see, FW_DEFAULT);
887 dictAppendWord(dp, "words", listWords, FW_DEFAULT);
890 ** Set TOOLS environment query values
892 ficlSetEnv(pSys, "tools", FICL_TRUE);
893 ficlSetEnv(pSys, "tools-ext", FICL_FALSE);
898 dictAppendWord(dp, "r.s", displayRStack, FW_DEFAULT); /* guy carver */
899 dictAppendWord(dp, ".env", listEnv, FW_DEFAULT);
900 dictAppendWord(dp, "env-constant",
901 envConstant, FW_DEFAULT);
902 dictAppendWord(dp, "env-2constant",
903 env2Constant, FW_DEFAULT);
904 dictAppendWord(dp, "debug-xt", ficlDebugXT, FW_DEFAULT);
905 dictAppendWord(dp, "parse-order",
908 dictAppendWord(dp, "step-break",stepBreak, FW_DEFAULT);
909 dictAppendWord(dp, "forget-wid",forgetWid, FW_DEFAULT);
910 dictAppendWord(dp, "see-xt", seeXT, FW_DEFAULT);