Re: Build your own Forth for Microchip PIC: Design thoughts
- From: Brad Eckert <nospaambrad1@xxxxxxxxxxxx>
- Date: Thu, 28 Jun 2007 09:13:18 -0700
On Jun 27, 8:14 pm, byron@upstairs.(none) (Byron Jeff) wrote:
Problem Definition: I want to develop an incremental word cross compiler
for the PIC. I hope this has been a "been there, done that." type
exercise for some of you. The idea is to develop and test the word on
the host, then cross compile the word into its PIC equivalent for
transport to the target. So for example say I develop a simple word on
the host:
: add2 2 + ;
Here are a couple of options:
1. The compiler creates a word ADD2 on the host that pushes 2 onto the
target's stack and tells the target to execute the "+" token. That
tests the word on the host. Another way to test on the host is to make
the target model closely match the host model. For example, I
currently run a 32-bit little-endian Forth on a 16-bit big-endian
target just so my models match. In the case of the PIC, you could make
the host more closely model the 16-bit target but it sounds to me like
tackling that one would require more Forth experience on your part. My
point about models is that you can think of Forth as a modeling
language instead of a programming language and see where that takes
you.
2. The compiler creates a data structure ADD2 on the host that
contains information like execution address and it also compiles the
definitions into tokens or whatever code the PIC expects to execute.
The host could download this code (ROM image) to the target right away
or it could cache it until you actually test ADD2 or tell the host to
flush the cache.
1. Instead of taking input from the normal input buffer, I want to
fundamentally parse a dictionary definition. The way I see it is
something like running "see <word>" taking the output and parsing it.
What I'm out of the loop with is accessing dictionary definitions
directly and parsing the words in those definitions.
Once a definition is compiled, it's a black box. Your code doesn't
have any business peeking inside. SEE is a decompiler (mostly a sanity
checker), which happens to spit out Forth source in some
implementations but assembly code in other implementations.
2. I know that create puts new words in the dictionary and allot will
allocate space. I'm unsure how to access that allotted space once I
created it or exactly how to get a pointer to the definition's alloted
area. Brad used C, to extend and write into the dictionary space for
example.
The word >BODY is used to address data structures created by CREATE.
In the case of target compilation, colon would create a data structure
containing the word's execution address along with whatever other
stuff you want. The text interpreter could use >BODY to get that
address and execute it on the target, compile it into the ROM image,
etc.
As an example of a text interpreter, here is a snippet of a text
interpreter that I load on top of an ANS Forth (including GForth),
which I use to compile to a target.
: top-order ( wid -- ) >r get-order nip r> swap set-order ;
: INTERPRETER ( -- ) *INTERPRETER dup top-order set-current ;
: COMPILER ( -- ) *COMPILER dup top-order set-current ;
: T[ ( -- ) *INTERPRETER top-order 0 tstate ! ;
: T] ( -- ) *COMPILER top-order -1 tstate ! ;
: HOST ( -- ) *HOST dup dup 3 set-order *HOST set-current 0
targeting ! ;
: isnumber ( a -- n )
dup dup c@ + c@ [char] . = dup >r if -1 over c+! then \ trim
trailing .
count over c@ [char] - = dup >r if 1 /string then \ accept
leading '-'
0 0 2swap >number abort" ???" drop \ convert
to number
r> if dnegate then
tstate @ if r> if swap ,lit else drop then ,lit
else r> 0= if drop then then ;
: TARGET ( -- ) \ start target interpreter
1 targeting ! 0 targlines !
*HOST *TARGET *TARGET 3 set-order
*TARGET set-current T[
begin targeting @ while
bl word dup c@ if
find dup if tstate @ = >r
dup >body @ magic = \ target word?
if >body cell+ dup
r> if cell+ then @ execute \ ( a -- )
else r> if compile, else execute then
then
else drop isnumber
then
else drop
depth 0< abort" Stack underflow"
source-id if 1 targlines +!
verbose @ if cr targlines @ . .stack then
else ." _ok " .stack cr
then refill 0= abort" Missing HOST directive at end of file"
then
repeat ;
4. I know that Forth parses and does dictionary lookups. What kinds of
words should I be looking for to do these activities? Can parsing be
done from an arbitrary buffer?
Parsing should be done from the input buffer, which could be the
console or a file or a text string (see EVALUATE). These are all just
streams of ASCII characters that are generally passed over once,
although some fancy compilers may do more complex things.
I would recommend writing your tools in Forth to get experience
writing Forth. It's a completely different skill from implementing
Forths.
--
Brad Eckert
.
- Follow-Ups:
- Re: Build your own Forth for Microchip PIC: Design thoughts
- From: none
- Re: Build your own Forth for Microchip PIC: Design thoughts
- From: Brad Eckert
- Re: Build your own Forth for Microchip PIC: Design thoughts
- References:
- Prev by Date: Re: Build your own Forth for Microchip PIC: Design thoughts
- Next by Date: Re: Build your own Forth for Microchip PIC: Design thoughts
- Previous by thread: Re: Build your own Forth for Microchip PIC: Design thoughts
- Next by thread: Re: Build your own Forth for Microchip PIC: Design thoughts
- Index(es):
Relevant Pages
|