Re: CREATE
- From: Elizabeth D Rather <erather@xxxxxxxxx>
- Date: Tue, 21 Jul 2009 07:11:47 -1000
MarkWills wrote:
Elizabeth said:
And some systems define CONSTANT in
such a way that it actually compiles its value as a literal when it's
encountered in a definition. This works particularly well with
optimizing compilers.
That's what my system does:
5 CONSTANT FIVE ok
A000$ 16 DUMP
A000 75C4 0004 4649 5645
A008 8320 6DDC 0005 832E
ok
75C4=link to previous dictionary entry
0004=length of this word
4649=FI
5645=VE
8320=pointer to DOCOL
6DDC=pointer to LIT
0005=the literal value 5
832E=pointer to EXIT
So, in effect, 5 CONSTANT FIVE compiles the equivalent of:
: FIVE 5 ;
No, that's not what I mean at all. The kind of CONSTANT I was referring to generates a literal in a definition in which it's invoked, so there's no call to a separate definition.
Your version is bulky in contrast to the more common implementation, which contains just the head, code address, and the value. Yours requires the extra space for the calls to LIT and EXIT.
My VARIABLE word is more complex, but not much. At run time (instance[example deleted]
behavior) it simply pushes the address of the word after it's EXIT op-
code to the stack:
Note, after compilation, HERE points to A022, because the word
immediately after EXIT is reserved for the 'payload', as I call it.
So, it looks something like this if you de-compiled it:
: VAR A020$ ;
Thus 65535 VAR ! writes $FFFF into $A020.
Again, it's bulky. Most implementations have only the head, code address, and the data space ("payload").
Elementary stuff. This works fine. No problem at all. And also has the
advantage that I can do things like:
VARIABLE BUFFER 8 ALLOT \ 10 element array called BUFFER. HERE updated
accordingly (obviously).
Yes, with a standard CREATE you'd say,
CREATE BUFFER 10 ALLOT
....which is more readable because you don't have to mentally add the two elements that VARIABLE contributed.
The worrying part is that, as it stands, it would seem implementing
DOES> is not possible (or, at least would be a bit hackish), so I need
to refactor slightly. I need to rename my current CREATE to (CREATE).
It just creates a linked dictionary entry, that's it.
Then I need to implement CREATE proper. I'm still struggling a bit
though. From the explanation everyone has given, it would seems
CREATEs stack diagram is:
CREATE ( -- addr )
No. CREATE itself has no stack effect. It's the instance behavior of CREATE that returns the address.
Josh said:CREATE defines a word which returns the new value of HERE
But which address? Is it the address immediately after the EXIT, like
my VARIABLE example above?
The address of the intended data space. In most implementations, as I've noted above, there is no EXIT.
If so, then DOES> just needs to back up HERE a few bytes so that
subsequent compilation patches over the code that returns the address,
compile an EXIT in at the end and update HERE accordingly. Doesn't
sound too bad.
Have I got it about right?
You would benefit by studying some existing standard implementations, such as GFORTH.
Cheers,
Elizabeth
--
==================================================
Elizabeth D. Rather (US & Canada) 800-55-FORTH
FORTH Inc. +1 310.999.6784
5959 West Century Blvd. Suite 700
Los Angeles, CA 90045
http://www.forth.com
"Forth-based products and Services for real-time
applications since 1973."
==================================================
.
- Follow-Ups:
- Re: CREATE
- From: Michael Haardt
- Re: CREATE
- From: MarkWills
- Re: CREATE
- References:
- CREATE
- From: MarkWills
- Re: CREATE
- From: Andrew Haley
- Re: CREATE
- From: MarkWills
- Re: CREATE
- From: Coos Haak
- Re: CREATE
- From: Elizabeth D Rather
- Re: CREATE
- From: MarkWills
- CREATE
- Prev by Date: Re: CREATE
- Next by Date: Re: 64 by 32 bit division
- Previous by thread: Re: CREATE
- Next by thread: Re: CREATE
- Index(es):
Relevant Pages
|
Loading