On Apr 21, 11:48 am, "Maki" <veselic...@[EMAIL PROTECTED]
> wrote:
> I have recently cross-compiled my first forth system (subroutine
threads),
> and now I want to separate headers from code. Current header arrangement
> looks like this:
> [link][flags][count][name] code
> I guess I need one more pointer in the header like this:
> [link][flags][count][name][code_ptr] which will connect header to
> definition.
>
> Questions:
>
> 1. What is the best way to handle allocation in name dictionary? My
current
> thought is to revector dp with words like:
> PROGRAM HERE ( returns dp from program code dictionary)
> NAMES HERE ( returns dp from name dictionary)
>
> 2. Implementation of word >name is trickier. It appears that >name now
> should use FIND to go back to name field.
>
> Are there any other pitfalls that I should pay attention to when doing
this?
> Any advices an comments appreciated.
> Best regards,
> M.
>
> --
> M.Veselic
> Sigma Lab.
Win32Forth uses separate sections; in the V6, two (a system area for
the headers and disposable words not required at run time, and a data
section for the rest), and in the STC version, four (x86 code and
header code, data and headers).
The big problem is ensuring that data words do not refer to system
words. To be quite frank, unless you are very constrained on memory,
it's not worth it.
The header structure;
[ link field ] -4 4 lfa
[ ' compile, ] +0 4 ct token -- compile,
+---- [ xt ptr field ] 4 4 xt-ptr
| [ ' comp ] 8 4 ct token -- comp field
| [ file field ] 12 4 ffa
| [ view field ] 16 2 vfa
| [ stk effects ] 18 2 ste
| [ optimize field ] 20 2 ofa
| [ count byte ] 22 1 nfa
| [ the name letters ] 23 n
| [ alignment bytes ] 0 to 3 bytes for name alignment
|
|
| [ ct-ptr ] -4 4 ptr to ct token pair
+---> [ xt field ] +0 xt code field (the xt)
The code section points back at the header (ct-ptr). To simplify
relocation, it's not a strict pointer, but an offset from the xt to
the header.
The code;
\ DP is the current data pointer, DP @[EMAIL PROTECTED]
is the equivalent of HERE
\
\ Each set of pointers to a data ("dictionary") space is a structure.
\ These structures MUST RESIDE IN THE APPLICATION SPACE if they are
linked
\
\ CELL OFFSET FUNCTION
\ ---- ------ --------
\ 0 0 Current pointer to area
\ 1 4 Address of the area (origin)
\ 2 8 Highest address of area (origin + length)
\ 4 16 Link of all the DP areas; set in DP-LINK
\ 5 20 Counted name of the area
\
\ 4 defined by default; APP -- std area, SYS -- system area, not
saved on
\ TURNKEY and CODE, SYS-CODE -- area for executable x86 code.
\
\ Actual values for these 3 are filled in by the meta compiler.
\ See also PDP and LDP (procs and locals data respectively)
variable dp-link \ list of dp structures
0 dp-link !
create sdp 0 , 0 , 0 , dp-link link, ," sys" \ system
create kdp 0 , 0 , 0 , dp-link link, ," syscode" \ system code (aka
kode)
create adp 0 , 0 , 0 , dp-link link, ," app" \ application
create cdp 0 , 0 , 0 , dp-link link, ," appcode" \ code
adp value dp \ data pointer defaults to app space
cdp value xdp \ xdp is the default code pointer
dp value odp
xdp value oxdp
\ ----------------- Switching section areas --------------------
\ To switch between data areas, >DP saves and resets the data pointer.
\
\ IN-xxxx is used in open code to switch HERE ALLOT , W, etc to point
\ to the specific data area; the current DP is saved in ODP, so
\ it can be reseted using IN-PREVIOUS.
\
\ >XXXX and XXXX> move to and from a specific data area, and save the
\ current DP. Should only be used while compiling, as the return stack
is
\ used to save/restore the current value, and must be used in matching
pairs.
: get-section ( -- n m ) dp xdp ;
: save-section ( -- ) get-section to oxdp to odp ;
: set-section ( n m -- ) save-section to xdp to dp ;
: in-application ( -- ) \ w32f
\ *G Activate the application data area.
adp cdp set-section ;
: in-system ( -- ) \ w32f
\ *G Activate the system data area.
sdp kdp set-section ;
: in-previous ( -- ) \ w32f
\ *G Restore the data area after a call to IN-APPLICATION or IN-
SYSTEM.
odp oxdp set-section ;
: in-app? ( -- f ) dp adp = ; \ if the dp is set to adp
: in-sys? ( -- f ) dp sdp = ; \ if the dp is set to sdp
: >dp ( -- ) \ nasty piece of code!
2r> \ my ret, caller's ret
dp >r \ save dp
2>r \ and save back
to dp ;
code dp> ( -- ) \ nasty piece of code!
0 0 in/out
mov ecx, 4 [esp] \ saved old dp
mov ' dp >body , ecx \ restore dp
ret 4 \ discard old value
c;
: >application ( -- ) adp >dp ; \ select app dict, save prev dict
: >system ( -- ) sdp >dp ; \ select sys dict, save prev dict
: >code ( -- ) xdp >dp ; \ select code dict, save prev dict
: here ( -- a1 ) dp @[EMAIL PROTECTED]
; \ next free byte
: , ( n -- ) here ! cell dp +! ; \ cell store, incr
: w, ( n -- ) here w! 2 dp +! ; \ word store, incr
: c, ( n -- ) here c! dp 1+! ; \ char store
etc. The key words are
.. IN-SYSTEM IN-APPLICATION and IN-PREVIOUS; move between sections (and
only used in open code) so that DP, HERE etc point at the right
sections
.. >APPLICATION >SYSTEM and >CODE to switch inside a word when
compiling.
Yes, it's messy.
--
Regards
Alex McDonald


|