Re: CREATE



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
behavior) it simply pushes the address of the word after it's EXIT op-
code to the stack:

[example deleted]

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."
==================================================
.



Relevant Pages

  • Re: LEAVE
    ... the xt into the outside definition, and then LEAVE could do EXIT. ... DO compiles a primitive and a branch address. ... But it looks very complex to do it with standard ... Or keep track on a separate stack ...
    (comp.lang.forth)
  • Re: CREATE
    ... 832E=pointer to EXIT ... So, in effect, 5 CONSTANT FIVE compiles the equivalent of: ... VARIABLE VAR ok ... A000=pointer to previous dictionary entry ...
    (comp.lang.forth)
  • Re: CREATE
    ... compiles its value as a literal when it's encountered in a ... Rename the current CREATE to HEADER, (header-comma, just my preference ... .int LIT, DOVAR, COMMA // Append DOVAR ... Is it the address immediately after the EXIT, ...
    (comp.lang.forth)
  • Re: 2.6.12-rc3 compile failure in tgafb.c
    ... > I'll be able to run more tests after this one compiles:) ... static void __exit ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)
  • Re: Sizes of Integer Types
    ... So what would you rather have, code using short that compiles happily on ... Will ALL of your code run on a system with 16 bit char, short and int with only 8K of space for the program and 8K of space for data? ... NOW do you get that some code is not required to run on ANY conforming implementations? ...
    (comp.lang.c)

Loading