Constants: immediate words? 
Author Message
 Constants: immediate words?

 Hello,

 Many thanks for the answers to my question about ROLL. The m{*filter*}is:
sometimes ROLL (or -ROLL) avoids brainstorming!
 I have another question concerning words created via CONSTANT: is it
possible to define them as immediate words without violating the ANS
standard? (I would like my compiler to inline constants without
considering them as a particular case).
 Is something like:
 0 CONSTANT ZERO
 1 ' ZERO >BODY !
 an acceptable programming style in Forth ?

 Regards,

 Sebastien



Mon, 19 Jan 2004 16:55:25 GMT  
 Constants: immediate words?

Quote:

> Is something like:
> 0 CONSTANT ZERO
> 1 ' ZERO >BODY !
> an acceptable programming style in Forth ?

Not acceptable, not allowed in ANS Forth.
What you want to do here is the reason for VALUEs.
( 0 VALUE zero  1 TO zero )

-marcel



Mon, 19 Jan 2004 17:43:12 GMT  
 Constants: immediate words?

Marcel Hendrix a crit :

Quote:

> > Is something like:
> > 0 CONSTANT ZERO
> > 1 ' ZERO >BODY !
> > an acceptable programming style in Forth ?

> Not acceptable, not allowed in ANS Forth.
> What you want to do here is the reason for VALUEs.
> ( 0 VALUE zero  1 TO zero )

> -marcel

 I fully agree with that. So, if a constant cannot have its value
changed, is it possible to define it as an immediate word ? (that pushes
its values on the stack when state is OFF ans that compiles its value as
a literal when state is ON)

 Regards,
 Sebastien.



Mon, 19 Jan 2004 20:05:52 GMT  
 Constants: immediate words?

Quote:


> > Is something like:
> > 0 CONSTANT ZERO
> > 1 ' ZERO >BODY !
> > an acceptable programming style in Forth ?

> Not acceptable, not allowed in ANS Forth.
> What you want to do here is the reason for VALUEs.
> ( 0 VALUE zero  1 TO zero )

> -marcel

How is this functionally different in application from

VARIABLE zeroprime

1 zeroprime !


merely syntactic sugar.

Jerry
--
Engineering is the art of making what you want from things you can get.
-----------------------------------------------------------------------



Mon, 19 Jan 2004 23:01:14 GMT  
 Constants: immediate words?
[..]

Quote:
>> What you want to do here is the reason for VALUEs.
>> ( 0 VALUE zero  1 TO zero )

> How is this functionally different in application from

> VARIABLE zeroprime


( assuming ';' ?)

Quote:
> 1 zeroprime !

> merely syntactic sugar.

Yes. Functionally it is nothing new. But the fact that the hack exists
means people were concerned about speed, which is the real problem a
VALUE addresses (for those without a reasonable compiler).

-marcel



Mon, 19 Jan 2004 23:52:19 GMT  
 Constants: immediate words?
 1) I asked this question because one could define CONSTANT as:

 : CONSTANT
    CREATE ,

;

 The standard says that it is not allowed to use >BODY with a word that
was not created via a call to CREATE (or a word that calls CREATE). Does
it say that it is not allowed to modify the value of a constant defined
with the above definition of CONSTANT? (I wish it does, but I don't know
where)

2) If I define MYCONSTANT like that:

: MYCONSTANT
    CREATE ,


            POSTPONE LITERAL
        THEN
        IMMEDIATE
;  ok

5 MYCONSTANT C1  ok
: TEST1 C1 + ;  ok
SEE TEST1
: TEST1          lit "0x5" + ; IMMEDIATE  ok

5 CONSTANT C2  ok
: TEST2 C2 + ;  ok
SEE TEST2
: TEST2          C2 + ;  ok

 Is MYCONSTANT an ANS equivalent of CONSTANT despite the fact that it
creates immediate words ?

 Regards,

 Sebastien.



Tue, 20 Jan 2004 00:04:11 GMT  
 Constants: immediate words?

Sbastien Furic a crit :

Quote:
>  1) I asked this question because one could define CONSTANT as:

>  : CONSTANT
>     CREATE ,

> ;

>  The standard says that it is not allowed to use >BODY with a word that
> was not created via a call to CREATE (or a word that calls CREATE). Does
> it say that it is not allowed to modify the value of a constant defined
> with the above definition of CONSTANT? (I wish it does, but I don't know
> where)

> 2) If I define MYCONSTANT like that:

> : MYCONSTANT
>     CREATE ,


>             POSTPONE LITERAL
>         THEN
>         IMMEDIATE
> ;  ok

 Sorry, I meant:

: MYCONSTANT
    CREATE ,
    IMMEDIATE


            POSTPONE LITERAL
        THEN
;  ok

5 MYCONSTANT C1  ok
: TEST1 C1 + ;  ok
SEE TEST1
: TEST1          lit "0x5" + ;  ok

 Regards,

 Sebastien.



Tue, 20 Jan 2004 00:13:58 GMT  
 Constants: immediate words?

Quote:

>  1) I asked this question because one could define CONSTANT as:

>  : CONSTANT
>     CREATE ,

> ;

>  The standard says that it is not allowed to use >BODY with a word that
> was not created via a call to CREATE (or a word that calls CREATE). Does
> it say that it is not allowed to modify the value of a constant defined
> with the above definition of CONSTANT? (I wish it does, but I don't know
> where)

One _could_ define CONSTANT in a number of ways.  The point is, though, that
since CONSTANT is a CORE word, it's assumed that the system provides it, and
if you are writing a portable program you may not make any assumptions about
how it is defined.  This is why you can't change a CONSTANT in a portable
program (or, indeed, use >BODY with a CONSTANT portably), because you have
no way of knowing how it was defined.

Quote:
> 2) If I define MYCONSTANT like that:

> : MYCONSTANT
>     CREATE ,


>             POSTPONE LITERAL
>         THEN
>         IMMEDIATE
> ;  ok

> 5 MYCONSTANT C1  ok
> : TEST1 C1 + ;  ok
> SEE TEST1
> : TEST1          lit "0x5" + ; IMMEDIATE  ok

> 5 CONSTANT C2  ok
> : TEST2 C2 + ;  ok
> SEE TEST2
> : TEST2          C2 + ;  ok

>  Is MYCONSTANT an ANS equivalent of CONSTANT despite the fact that it
> creates immediate words ?

Creating IMMEDIATE words is perfectly legal (and I do take note of your
later version of this code), that is why IMMEDIATE is a core word.  Insofar
as MYCONSTANT is your definition and violates no rules, it's perfectly
legal.

What I suspect you were really asking is, might an implementation that
defines CONSTANT this way be as legal as the definition in 1) above?  The
answer is, yes, that is yet another possible legal implementation, and a
good illustration of why portable code cannot assume it can change the value
of a CONSTANT.

Changing CONSTANTs is rendered illegal by the point you raised initially,
that you cannot use >BODY to get the address of a CONSTANT because you have
no guarantee that it was defined using CREATE.

The TC deliberately excluded CONSTANT from data objects you can change in
order to liberalize possible implementations, including the use of literals
as in your example, as well as more advanced constructs such as building the
literal into an instruction in an optimizing direct code compiler, putting
CONSTANTs in PROM in embedded systems, etc.  As Marcel notes, it is
unnecessary to change constants because VALUE provides a changable constant
that is more user-friendly.

Cheers,
Elizabeth

--
================================================
Elizabeth D. Rather   (US & Canada)       800-55-FORTH
FORTH Inc.                                      +1 310-491-3356
5155 W. Rosecrans Ave. #1018  Fax: +1 310-978-9454
Hawthorne, CA 90250
http://www.forth.com

"Forth-based products and Services for real-time
applications since 1973."
================================================



Tue, 20 Jan 2004 01:30:19 GMT  
 Constants: immediate words?

Quote:


> [..]
> >> What you want to do here is the reason for VALUEs.
> >> ( 0 VALUE zero  1 TO zero )

> > How is this functionally different in application from

> > VARIABLE zeroprime

> ( assuming ';' ?)

> > 1 zeroprime !


> > merely syntactic sugar.

> Yes. Functionally it is nothing new. But the fact that the hack exists
> means people were concerned about speed, which is the real problem a
> VALUE addresses (for those without a reasonable compiler).

> -marcel

That figures. I wrote my own TO hack in 68HC11 land for that very
reason. But it works on constants, and is entirely dependent on the
workings of MAXforth. Perhaps I'll try to conform; there's not much
invested yet in the code I have.

Jerry
--
Engineering is the art of making what you want from things you can get.
-----------------------------------------------------------------------



Tue, 20 Jan 2004 01:33:55 GMT  
 Constants: immediate words?

Quote:

> I have another question concerning words created via CONSTANT: is it
>possible to define them as immediate words without violating the ANS
>standard? (I would like my compiler to inline constants without
>considering them as a particular case).

Are you writing your own ANSI-compliant compiler?  If not, then yes, you
can make CONSTANT do almost anything you want.  Keep in mind that this
isn't as simple as just dropping the number on the stack; you have to
compile the number as a literal.  Here's a quick, untested definition:


(This isn't state-smart, of course.  Swizzle as needed.)

State-smartness is dangerous, though -- what if someone wants to
manipulate a defined CONSTANT?  They won't know that it's IMMEDIATE.

Quote:
> Sebastien

--
-William "Billy" Tanksley


Tue, 20 Jan 2004 03:33:24 GMT  
 Constants: immediate words?
A very informative and NICE answer!!
Quote:


> >  1) I asked this question because one could define CONSTANT as:

> >  : CONSTANT
> >     CREATE ,

> > ;

> >  The standard says that it is not allowed to use >BODY with a word that
> > was not created via a call to CREATE (or a word that calls CREATE). Does
> > it say that it is not allowed to modify the value of a constant defined
> > with the above definition of CONSTANT? (I wish it does, but I don't know
> > where)

> One _could_ define CONSTANT in a number of ways.  The point is, though, that
> since CONSTANT is a CORE word, it's assumed that the system provides it, and
> if you are writing a portable program you may not make any assumptions about
> how it is defined.  This is why you can't change a CONSTANT in a portable
> program (or, indeed, use >BODY with a CONSTANT portably), because you have
> no way of knowing how it was defined.

> > 2) If I define MYCONSTANT like that:

> > : MYCONSTANT
> >     CREATE ,


> >             POSTPONE LITERAL
> >         THEN
> >         IMMEDIATE
> > ;  ok

> > 5 MYCONSTANT C1  ok
> > : TEST1 C1 + ;  ok
> > SEE TEST1
> > : TEST1          lit "0x5" + ; IMMEDIATE  ok

> > 5 CONSTANT C2  ok
> > : TEST2 C2 + ;  ok
> > SEE TEST2
> > : TEST2          C2 + ;  ok

> >  Is MYCONSTANT an ANS equivalent of CONSTANT despite the fact that it
> > creates immediate words ?

> Creating IMMEDIATE words is perfectly legal (and I do take note of your
> later version of this code), that is why IMMEDIATE is a core word.  Insofar
> as MYCONSTANT is your definition and violates no rules, it's perfectly
> legal.

> What I suspect you were really asking is, might an implementation that
> defines CONSTANT this way be as legal as the definition in 1) above?  The
> answer is, yes, that is yet another possible legal implementation, and a
> good illustration of why portable code cannot assume it can change the value
> of a CONSTANT.

> Changing CONSTANTs is rendered illegal by the point you raised initially,
> that you cannot use >BODY to get the address of a CONSTANT because you have
> no guarantee that it was defined using CREATE.

> The TC deliberately excluded CONSTANT from data objects you can change in
> order to liberalize possible implementations, including the use of literals
> as in your example, as well as more advanced constructs such as building the
> literal into an instruction in an optimizing direct code compiler, putting
> CONSTANTs in PROM in embedded systems, etc.  As Marcel notes, it is
> unnecessary to change constants because VALUE provides a changable constant
> that is more user-friendly.

> Cheers,
> Elizabeth

> --
> ================================================
> Elizabeth D. Rather   (US & Canada)       800-55-FORTH
> FORTH Inc.                                      +1 310-491-3356
> 5155 W. Rosecrans Ave. #1018  Fax: +1 310-978-9454
> Hawthorne, CA 90250
> http://www.forth.com

> "Forth-based products and Services for real-time
> applications since 1973."
> ================================================



Tue, 20 Jan 2004 04:24:00 GMT  
 Constants: immediate words?

Quote:

>  I have another question concerning words created via CONSTANT: is it
> possible to define them as immediate words without violating the ANS
> standard? (I would like my compiler to inline constants without
> considering them as a particular case).
>  Is something like:
>  0 CONSTANT ZERO
>  1 ' ZERO >BODY !
>  an acceptable programming style in Forth ?

Your code has absolutely nothing to do with your question, or any
other part of your message.  I'm boggled as to what its purpose
is.

I don't know about the ANS standard wrt immediate constants.
Practically, what you'll get from code like this:

  80 constant length immediate
  : forcel length min ;

is something surprising.  I'm sure that /this/ code violates the
ANS standard, since the system-specific flag that the ANS standard
describes : putting on the stack won't be seen by ;  Because
length puts 80 on the stack, even when you're compiling, because
it's IMMEDIATE.  See?  Now, if you want a named value that will
be inlined into code, something like the following might be
better:


  80 iconstant length
  : forcel length min ;

Someone more familiar with the ANS standard will have to tell
you how portable this is.  Of course, on Forth systems that
follow cmForth's style of a seperate vocabulary that holds all
immediate words will allow even simpler versions of the above.

  also macro definitions
  : length 80 postpone literal ;
  previous definitions

or

  m: length 80 postpone literal ;

But that's probably not what you're looking for.  Perhaps
some optimizing compilers handle CONSTANT.  Regarding
ANS-compliance, what you can do for compliant applications is
simply state 'This program, particularly this next word
ICONSTANT, assumes something about your system.'  and then
either A) pretend that people who have problems are intelligent
enough to handle them, or B) tell them that all they have to
do is delete ICONSTANT.

Now that I think of it, you can just have something like:

  \ This next word assumes something about your system.  I'm
  \ not sure what.  Sorry.  Just comment it if you don't want
  \ it.  It's supposed to create inlining constants.

somewhere obvious in your application source.



Tue, 20 Jan 2004 05:20:53 GMT  
 Constants: immediate words?
On Thu, 2 Aug 2001 17:52:19 +0200, "Marcel Hendrix"

Quote:


>> merely syntactic sugar.
>Yes. Functionally it is nothing new. But the fact that the hack exists
>means people were concerned about speed, which is the real problem a
>VALUE addresses (for those without a reasonable compiler).

Or without a "reasonable microprocessor" ... 16-bit value access is
something of a bottleneck on a 65C02, for example, and even with a
subroutine threaded compiler to get around the potential theader
bottlenecks in getting 16-bit word pointers, something like:

value1:
    LDA #hibyte
    LDY #lobyte
    JMP do_value
   ...
        ... JSR value1 ...

can end up being substantially faster than

        ... JSR do_lit #lo_byte #$hi_byte JSR fetch

Indeed, the switch to subroutine threading to deal with the inner
interpreter bottleneck can actually increase the overhead in fetching
literals, so what seems like the "smart compiler" may even make VALUEs
more valuable when what you want is a resettable constant.

(
----------
Virtually,

Bruce McFarling, Newcastle,

)



Tue, 20 Jan 2004 08:26:35 GMT  
 Constants: immediate words?

Quote:
>Creating IMMEDIATE words is perfectly legal (and I do take note of your
>later version of this code), that is why IMMEDIATE is a core word.  Insofar
>as MYCONSTANT is your definition and violates no rules, it's perfectly
>legal.

This does require that the author(s) of a particular Forth
implementation considered that one would use IMMEDIATE on a constant.
I personally nearly didn't consider this (until this discussion);
therefore, it is likely that a Forth implementation could actually not
be ANS compliant because IMMEDIATE only does something normal on an
executable word.

Quote:
>Changing CONSTANTs is rendered illegal by the point you raised initially,
>that you cannot use >BODY to get the address of a CONSTANT because you have
>no guarantee that it was defined using CREATE.

The use of >BODY on a constant is particularly interesting in Forth
implementations (such as one that I'm working on right now) that
internally treat constants exactly the same as variables and CREATEd
words, that is, variables and CREATEd words are actually constants
which just push an address in data space.  This would mean that there
would be no way for >BODY to error-check whether the word it is being
used on is a constant or a variable or a CREATEd word (except for
data-space bounds checking, which unnecessarily uses extra
instructions, including at least two load register (on the processor
I'm coding in assembly for) instructions (and if the data space base
and the data space top pointers aren't in the cache, there is a
further performance hit from fetching them from memory)), and thus
Quote:
>BODY being used on a constant would really just push the value of the

constant.

--
Yes, I know my enemies.
They're the teachers who tell me to fight me.
Compromise, conformity, assimilation, submission, ignorance,
hypocrisy, brutality, the elite.
All of which are American dreams.

              - Rage Against The Machine



Tue, 20 Jan 2004 14:10:58 GMT  
 Constants: immediate words?

William Stewart a crit :

Quote:
> A very informative and NICE answer!!

 I fully agree...

 Sebastien.



Tue, 20 Jan 2004 15:39:18 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Type inference in loops with immediate constants

2. Secret of Immediate Words?

3. can CREATEd words be IMMEDIATE?

4. test words immediate-mode in Sparc monitor ?

5. Poll: compiling an immediate word

6. Poll: compiling an immediate word

7. mov [edi], constant vs mov [edi+4], constant

8. Usage Constant or other means of noting constants.

9. PGF90-S-0087-Non-constant expression where constant expression required

10. PGF90-S-0087-Non-constant expression where constant expression required

11. named constants vs. literal constants

12. Non-constant expression where constant expression required.

 

 
Powered by phpBB® Forum Software