6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat Nov 23, 2024 10:51 am

All times are UTC




Post new topic Reply to topic  [ 24 posts ]  Go to page Previous  1, 2
Author Message
PostPosted: Thu Jan 14, 2021 8:22 pm 
Offline

Joined: Sun Apr 26, 2020 3:08 am
Posts: 357
GARTHWILSON wrote:
>BODY doesn't make any changes to the word. All it does is return the PFA, given the CFA. It's only information about the word.

As was pointed out earlier, the meaning of >BODY is more than just CFA 2+ or just 2+. It is a representation of where in a word an author wants to take his/her code. Pointing to a "Body" of a word means you want to do something with the body of that word. Reading from the "Body" of the code is fine. No errors.

But writing to a "Body" of a word may cause catastrophe. Especially if the "Body" of a word contains assembly code. And it is easier to modify >BODY for error checking instead of every word that calls >BODY.

I guess it depends what the programmers intentions are and how confident they are. But for some, error checking against possible human error, should become second nature. And also for some, it is hard to decipher what another programmers intentions, or meaning of words, are and the possible side-effects to using such words haphazardly.


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 14, 2021 9:01 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8545
Location: Southern California
Wow, looking at my "Starting Forth" (on paper), second edition, page 197 where there's a chart of behaviors of EXECUTE, ', ['], COMPILE, [COMPILE], FIND, and CFA of different Forth versions up until the time of writing, it makes me glad for Forth-83. The earlier ones appear to be kind of messy.

There's no reason not to use >BODY with a primitive. You could have for example,
Code:
CREATE CELLS        \ CELLS' stack effect will be ( n -- 2n )
   ' 2* >BODY   runtime
(although there are other ways to do it). ' 2* gets the CFA of 2*, >BODY increments it by 2 (in ITC 6502 Forth) to get the PFA, ie, the machine code of that primitive, and runtime puts that address in the CFA of the last-created word, ie, of CELLS, so CELLS will execute the same code as 2*, and there's no difference in run speed either. CELLS has no parameter field of its own, and 2*'s parameter field is machine code.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 15, 2021 9:46 pm 
Offline

Joined: Fri May 05, 2017 9:27 pm
Posts: 895
IamRob wrote:
GARTHWILSON wrote:
>BODY doesn't make any changes to the word. All it does is return the PFA, given the CFA. It's only information about the word.

As was pointed out earlier, the meaning of >BODY is more than just CFA 2+ or just 2+. It is a representation of where in a word an author wants to take his/her code. Pointing to a "Body" of a word means you want to do something with the body of that word. Reading from the "Body" of the code is fine. No errors.

But writing to a "Body" of a word may cause catastrophe. Especially if the "Body" of a word contains assembly code. And it is easier to modify >BODY for error checking instead of every word that calls >BODY.

I guess it depends what the programmers intentions are and how confident they are. But for some, error checking against possible human error, should become second nature. And also for some, it is hard to decipher what another programmers intentions, or meaning of words, are and the possible side-effects to using such words haphazardly.


>BODY is just a tool, like all Forth words. It is up to the programmer to use it responsibly. Here are some words from Fleet Forth's system loader that would not assemble or compile if >BODY had the error protection you propose:
Code:
CODE ADEPTH  ( -- N )
   ' AP0 >BODY C@ # LDY,
   SEC,  UP )Y LDA,
   AP0 @ SBC,
   .A LSR,
   APUSH JMP,
END-CODE

: VOCS
   [ ' FORTH >BODY ] LITERAL 0
   .VOCS  CR ;

ADEPTH returns the depth of the auxiliary data stack. VOCS shows the vocabularies in the system.

These words from the decompiler, SEE , would not work properly with such a restrictive version of >BODY :
Code:
: (.DEST)  ( ADDR -- )
   AFIND DROP ?DUP
   IF
      ."  ' " L>NAME DUP ID.
      ."  >BODY" NAME> >BODY -
      ?DUP 0EXIT
      SPACE U. ." +"  EXIT
   THEN
   DROP ;

: CREATEDIS  ( CFA -- )
   DUP AFIND NIP
   SWAP - 2- ?DUP
   IF
      OVER @ AFIND DROP
      L>NAME CR ID.
      SPACE 16BITS U.R
      SWAP >BODY  CR ." PFA: "
      16BITS U.R SPACE 16BITS U.R
      EXIT
   THEN
   @ DUP CR (.DEST)  DIS ;

: ?CODE  ( ADDR -- F )
   DUP @ SWAP >BODY = ;

: >DIS  ( CFA -- )
   >BODY DIS ;
   
: >:DIS  ( CFA -- )
   >BODY :DIS ;
   
: DDIS  ( CFA -- )
   ."  DEFERED TO "
   >BODY @ DUP .NAME SPACE U. ;

With such a restrictive version of >BODY , it would fail to help with portability. In each of these cases the word 2+ would be used. Now suppose the system loader is ported to a DTC (Direct Threaded Code) version of Fleet Forth. The code fields would be three bytes, not two. Every instance of 2+ would need to be examined to see if it needed replaced with 3 + .


Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 15, 2021 9:54 pm 
Offline

Joined: Fri May 05, 2017 9:27 pm
Posts: 895
IamRob wrote:
After reading some other topics and finally understanding them, I see that >BODY makes sense if only used with words without CODE.


If you want really restrictive, the Ansi Forth Standard states the following for >BODY
Code:
>BODY
( xt -- a-addr )
a-addr is the data-field address corresponding to xt. An ambiguous condition exists if xt is not for a word defined via CREATE.



Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 15, 2021 10:06 pm 
Offline

Joined: Fri May 05, 2017 9:27 pm
Posts: 895
IamRob wrote:
GARTHWILSON wrote:
JimBoyd wrote:
It's my understanding that fig-Forth does it the other way around. FIND returns the address of a word's body and the word CFA is used to go from the parameter field address to the code field address.

Yes; but what goes the other direction? It's not PFA, because that takes the NFA to give you the PFA; and NFA goes the opposite direction. I'm looking in the fig-Forth assembly source code and the installation manual and I'm not finding a word to do that. I guess you're just supposed to know to use 2+.

This kind of confusion is why I am kind of adopting a "best suited" method. Some words don't even have a PFA as the code is or can be redirected to elsewhere in memory.

My Forth breaks more if I use : PFA CFA 2+, so I rewrote it so that FIND returns the CFA, then to get the PFA it is just : PFA CFA @. Even with an STC forth, PFA, instead of being 3+, could be made to work with CFA @, without breaking STC or DTC portability.

I don't know, but I think this seems to be an easier port between different threaded Forths.


The Forth-83 Standard introduced words to move among the various fields in a Forth Word by way of an experimental proposal:
Code:
                    DEFINITION FIELD ADDRESS CONVERSION OPERATORS

                                         by

                                    Kim R. Harris

          A.  INTRODUCTION

          The standard provides a transportable way to obtain the
          compilation address of a definition in the dictionary of a FORTH
          system (cf., FIND and ' ).  It also provides an operator to
          convert a compilation address to its corresponding parameter
          field address.  However, the standard does not provide a
          transportable way to convert either of these addresses to the
          other fields of a definition.  Since various FORTH
          implementations have different dictionary structures, a standard
          set of conversion operators would increase transportability and
          readability.

          A set of words is proposed which allows the conversion of any
          definitions field address to any other.

          B.  GLOSSARY

          In the following words, the compilation address is either the
          source or the destination, so it is not indicated in the names.

          >BODY        addr1 -- addr2                            "to-body"
               addr2 is the parameter field address corresponding to the
               compilation address addr1.

          >NAME        addr1 -- addr2                            "to-name"
               addr2 is the name field address corresponding to the
               compilation address addr1.

          >LINK        addr1 -- addr2                            "to-link"
               addr2 is the link field address corresponding to the
               compilation address addr1.

          BODY>        addr1 -- addr2                          "from-body"
               addr2 is the compilation address corresponding to the
               parameter field address addr1.

          NAME>        addr1 -- addr2                          "from-name"
               addr2 is the compilation address corresponding to the name
               field address addr1.

          LINK>        addr1 -- addr2                          "from-link"
               addr2 is the compilation address corresponding to the link
               field address addr1.


          The previous set of words is complete, but may be inefficient for
          going between two fields when one is not the compilation address.
          For greater efficiency, additional operators may be defined which
          name both the source and destination fields.

          N>LINK       addr1 -- addr2                       "name-to-link"
               addr2 is the link field address corresponding to the name
               field address addr1.

          L>NAME       addr1 -- addr2                       "link-to-name"
               addr2 is the name field address corresponding to the link
               field address addr1.


          C.  DISCUSSION

          The previous words provide a complete, consistent, and efficient
          set of definition field address conversion operations.  They can
          be implemented in a FORTH system which uses any combination of
          the following options for its dictionary structure:

               Link fields first or second.
               Fixed or variable length name fields.
               Additional fields in the definitions structure.
               Heads contiguous or separated from bodies.
               Indirect, direct, subroutine, or token threaded code.

          The words are compatible with this standard; their inclusion
          would not require other changes to be made to the standard.

By the way, in a Forth-83 system both FIND and ' (tick) return the CFA.


Top
 Profile  
Reply with quote  
PostPosted: Sat Jan 16, 2021 12:49 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8545
Location: Southern California
Jim, that's a very good and helpful piece. Thanks.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
PostPosted: Sat Jan 16, 2021 2:47 am 
Offline

Joined: Sun Apr 26, 2020 3:08 am
Posts: 357
GARTHWILSON wrote:
>BODY doesn't make any changes to the word. All it does is return the PFA, given the CFA. It's only information about the word.

I didn't mean to indicate that >BODY by itself could cause any damage.

Fig Forth, and the Forth I am using is based on Fig Forth but with the 79 Standard, returns the PFA when [b[-FIND[/b] is used. >BODY, then is not needed. But, I changed -FIND to return the CFA instead. And I changed the PFA in INTERPRET to point to a null word, so it doesn't do anything. But I may revive it if there turns out to be a better way. Which now means I might need >BODY, but I need to understand under what conditions >BODY is used, or only should be use, and what it was meant to reference.

I was looking at DOER/MAKE and some examples which means it can make a word do different things. My first thought was that if I mistyped a word after MAKE, and it happened to be a word that contained a word primitive, then there would be machine language code starting where the PFA is. In this case MAKE wouldn't be doing anything other than causing a fantastic crash down the road.

Therefore I am trying to understand the reference that >BODY is trying to make. Also the fact that some of my words don't even contain a PFA, has confused me quite a bit. Then, having a word that references the PFA of another word, then becomes too much to bear, especially when some words may not contain a PFA or the PFA is Machine Language code instead of an indirect thread address.

I still am not sure I understand the context in which >BODY is being used. Is >BODY taking the place of PFA in newer standards?


Top
 Profile  
Reply with quote  
PostPosted: Sat Jan 16, 2021 3:26 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8545
Location: Southern California
IamRob wrote:
Is >BODY taking the place of PFA in newer standards?

My copy of the 1994 ANS Forth standard says in section 6.1.0550, on page 32, in the core word set, that >BODY takes in the "execution token" (which would be the CFA in 6502 ITC Forth) and returns the data-field address (which would be our PFA). Then it says, "An ambiguous condition exists if xt is not for a word defined via CREATE."

Edit: I just checked the 2012 standard I have in a .pdf file, and it has the same thing so I took a screenshot:

Attachment:
toBODY_Forth2012screenshot.gif
toBODY_Forth2012screenshot.gif [ 14.06 KiB | Viewed 695 times ]

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
PostPosted: Sat Jan 16, 2021 2:17 pm 
Offline

Joined: Sun Apr 26, 2020 3:08 am
Posts: 357
GARTHWILSON wrote:
IamRob wrote:
Is >BODY taking the place of PFA in newer standards?

My copy of the 1994 ANS Forth standard says in section 6.1.0550, on page 32, in the core word set, that >BODY takes in the "execution token" (which would be the CFA in 6502 ITC Forth) and returns the data-field address (which would be our PFA). Then it says, "An ambiguous condition exists if xt is not for a word defined via CREATE."

Edit: I just checked the 2012 standard I have in a .pdf file, and it has the same thing so I took a screenshot:

Attachment:
toBODY_Forth2012screenshot.gif

Ahhh! Gotchya! The words I was fearing about would be considered ambiguous.

Although the joke is on them, because I can still create an ambiguous word using a colon definition. Which is what I am presently doing now to create code words.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 24 posts ]  Go to page Previous  1, 2

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: