Pocket Forth
Volume Number: 5
Issue Number: 4
Column Tag: Forth Forum
By örg Langowski, MacTutor Editorial Board
Note: Source code files accompanying article are located on MacTech CD-ROM orsource code disks.
“Public Domain Pocket Forth”
Imagine: a compiler that creates applications or desk accessories from the same
source code, with only a one or two line change. Impossible? Read on. That compiler
will work interactively, so that you can create code as you go, typing in routine after
routine and checking them out on the fly. The applications will be very small, they can
be run in a 32K partition. And, of course, the code will be fast.
I’m not joking, such a development system does exist. Even more unbelievable,
it’s free. It’s now been several months since I downloaded PocketForth from the GEnie
Forth Roundtable, and had a lot of fun with it since then. Lately, we even received a
letter requesting a review of PocketForth, so I thought this a good opportunity to
introduce you to the public domain Forth for the Macintosh.
PocketForth has been written by Chris Heilman, and unfortunately all the author
leaves in the documentation is his Compuserve address (70566,1474); no mail
address, no phone number. Since I am not on Compuserve (can’t access from here), I
wasn’t able to contact him. Therefore, Chris, when you read this: my apologies that we
couldn’t warn you. I hope you’ll appreciate this review, and please contact us if you
have any comments.
Although PocketForth is completely public domain - even the sources are
available upon request - we’d like to have the author’s authorization before putting his
system on the source code disk. We are working on it, but the Forth compiler might
come on a later disk. Meanwhile, you can download the system from GEnie or
Compuserve; the Stuffit file is about 150K long and contains ample documentation and
examples.
PocketForth implementation
PocketForth is based on FIG-Forth and a Forth for the 68000 described in Dr.
Dobb’s Journal (G. Y. Fletcher, DDJ no. 123, January 1987). It uses a 16-bit stack
and base-relative addressing with 32K offset, therefore the total code size is
restricted to 32K bytes. The implementation uses subroutine threading with JSRs
relative to the base pointer, which is kept in A3. An example illustrates this. Our test
routine simply adds 3 and 4, outputs the sum and a space:
: test 3 4 + . 32 emit ;
this compiles to
move.w #3,-(a6) ; literal 3
move.w #4,-(a6) ; literal 4
jsr $E94(a3) ; +
jsr $BF0(a3) ; .
move.w #32,-(a6) ; literal 32
jsr $9FA(a3) ; emit
rts
As you see, the code is dependent on the correct setup of A3. PocketForth must
therefore execute in a locked block of memory, which is allocated at startup. The
initialization code makes all the standard calls (_MoreMasters, _InitGraf, _InitFonts,
_InitWindows, _InitMenus, _InitDialogs, _TEInit, _FlushEvents, _InitCursor), gets
the PocketForth main code from the resource DICT ID=257 and jumps to its beginning.
DICT 257 is the PocketForth dictionary and contains the names and executable code of
all the known Forth words (this is in contrast to Mach2, which creates headerless code
and the names are kept in a separate vocabulary). The DICT resource is locked, so that
the block won’t move while PocketForth is executing. The startup sequence sets A3 to
point to the beginning of the DICT block, initializes stack pointers and other things, and
enters the Forth interpreter.
The PocketForth Dictionary
PocketForth dictionary entries have a header consisting of a name field and a link
field. The name field is 4 bytes long, the first byte containing the name length, and the
next three bytes the first three characters of the name. This means that the words
compile and compute will have the same dictionary entry (caution!). The upper bit of
the name field’s first byte is the immediate bit; when set, it indicates an immediate
execution word. The link field, after the name field, is 2 bytes long and points to the
previous dictionary entry. The link field is followed by the definition’s executable
code.
Applications vs. desk accessories
You might have already guessed why PocketForth separates the setup code and the
DICT resource. This way, application and DA ‘shells’ can be made that set up the