Download 1 - Forth

Transcript
F d y , youwould want to add anadditionalm-input choice
that would allowthe user to seled whetherthe debugger step@
into the called word, or executed it as a whole, as it does now.
found in old, reliable, interpreted BASIC. To refresh your
memory, executing TRACEON in BASIC would cause the
system to display the number of the current line BASIC was
executing. This was handy for locating exactly where the
Last CaUs ToJumps
system either did a belly-flop or hung in an i n f i t e loop. I
SC/Forth32 is an optimizing compiler. Among other things,
wanted a similar constructfor my Forth work. I wanted words
this means that the compiler is intelligent enough to recogto tell me when they were about to execute, and I wanted to
nize that if the last instruction in the &ition of a word is a call
be able to turn this behavior on and off. As in BASIC, this
instruction, that call can be converted ro an unconditional jump.
would make it easier to pinpoint where the program died.
This saves return stack space, as well as reduang some execution
My solution was a pair of words-TRACEON and
time that would ordinarily be unnecessarily consumed moving
TRACEOFF-thal you could use as brackets. That is, words
addresses between the retum stack and the instruction pointer.
compiled after TRACEON would display their names when
From the debugger's point of view, the jump imtruction
executed. TRACEOFF would disable tracing; subsequent
is just a jump; there's no indicationthat this was a call optimized
words would act normally. I was satisfied to have only colon
into a jump. If you single-stepinto this situation,itwill appear
wordsbe affectedby TRACEON and TRACEOFF.(I could have
that you have nested down into a word, and in some severe
extended the trace word to cover defining words, but I didn't
cases this nesting can go on for several levels as you repeatedly
need that particular feature.)
encounter the last instruction of each word. Ultimately,of course,
you will encounter a Forth primitive and pop out the end
Trace Operation
Trace
~~AcEONworks
by patching the : (colon) word. The last
As a final tool, I built a simple execution trace facility. I word in SCfiorth32's definition of : is 1, which puts Forth
based the execution trace words on the trace commands inthe compilingstate.The SC32instruction that calls ] is located
eight cells into the definition
of :.TRACEONoverwrites that
Figure One. SC32 instruction types.
..
location with a call to the
Instruction Top 3 bits of
word >>TRACE.
instruction
Type
Description
So, after you execute
whenever : ex000
The SC32 places the return address on the return stack, TRACEON,
Call
and jumps to the location given by the instruction's
ecutes, it calls >>TRACE as
remaining 29 bits.
its last instruction. >>TRACE
will compile the word TRACE
Branch
001
Same as a call instruction, only the SC32 doesn't
into the dictionary. Hence,
place anything on the return stack.
TRACE becomes the first
word executed by whatever
Conditional
010
If the SC32's FL flag is zero, this instruction performs
word : has just defined.
branch
a branch. Otherwise, the processor proceeds to the
>>TRACEthen executes ] so
next instruction.
that the compiler enters the
proper state at the end of :.
011
Executes a variety of arithmetic, logical, and shift
(A sideeffect is that words
operations, depending on the remaining 29 bits.
compiled after TRACEON are
one cell longer than they
Load
100
Adds an offset (encoded in the lower 16 bits of the
would
ordinarily be.)
instruction) to the contents of a designated source
Now, whenever the coregister. The contents of the resulting address are
lon-defined word executes,
loaded into a designated destination register.
it immediately calls TRACE.
TRACE fetches the return
Store
101
Adds an offset (encoded in the lower 16 bits of the
address from the returnstack
instruction) to the contents of a designated source
and decrements that address
register. The contents of a designated destination
by one cell. The resulting
register are stored at that address.
cell address points to the
Load address
110
Adds an offset (encoded in the lower 16 bits of the
body of the calling word,
low
instruction) to the contents of a designated source
and TRACE can unleash
register. -Theresult is placed in a designated
HISNAME (described above)
destination register.
to print the name field.
TRACEOFF simply un111
Load address
Adds an offset (encoded in the lower 16 bits of the
patches :, overwriting the
high
instruction) to the contents of a designated source
call to >>TRACE with a call
register a e s h i f t i n g that offset to the left 16 bits. The
to 1. The source to the TRACE
result is placed in a designated destination register.
systemisshownin ListingTwo.
Forth Dimensions
May 1992 June
1
I
L-