\ @(#) clone.fth 97/12/10 1.1
\ Create the smallest dictionary required to run an application.
\ Clone decompiles the Forth dictionary starting with the top
\ word in the program. It then moves all referenced secondaries
\ This work was inspired by the CLONE feature that Mike Haas wrote
\ for JForth. Mike's CLONE disassembled 68000 machine code then
\ reassembled it which is much more difficult.
\ Copyright Phil Burk & 3DO 1994
\ O- trap custom 'C' calls
\ O- investigate ALITERAL, XLITERAL, use XLITERAL in [']
: PRIMITIVE? ( xt -- flag , true if primitive )
: 'SELF ( -- xt , return xt of word being compiled )
long clr_OriginalXT \ original XT of word
long clr_NewXT \ corresponding XT in cloned dictionary
long clr_TotalSize \ size including data in body
variable CL-INITIAL-REFS \ initial number of refs to allocate
variable CL-REF-LEVEL \ level of threading while scanning
variable CL-NUM-REFS \ number of secondaries referenced
variable CL-MAX-REFS \ max number of secondaries allocated
variable CL-LEVEL-MAX \ max level reached while scanning
variable CL-LEVEL-ABORT \ max level before aborting
variable CL-REFERENCES \ pointer to cl.reference array
variable CL-TRACE \ print debug stuff if true
\ Cloned dictionary builds in allocated memory but XTs are relative
\ to normal code-base, if CL-TEST-MODE true.
variable CL-INITIAL-DICT \ initial size of dict to allocate
20 1024 * cl-initial-dict !
variable CL-DICT-SIZE \ size of allocated cloned dictionary
variable CL-DICT-BASE \ pointer to virtual base of cloned dictionary
variable CL-DICT-ALLOC \ pointer to allocated dictionary memory
variable CL-DICT-PTR \ rel pointer index into cloned dictionary
cl-ref-level @ 2* 2* spaces
: CL.DICT[] ( relptr -- addr )
: CL, ( cell -- , comma into clone dictionary )
cl-dict-ptr @ cl.dict[] !
: CL.FREE.DICT ( -- , free dictionary we built into )
: CL.FREE.REFS ( -- , free dictionary we built into )
: CL.ALLOC.REFS ( -- , allocate references to track )
cl-initial-refs @ \ initial number of references
dup cl-max-refs ! \ maximum allowed
: CL.RESIZE.REFS ( -- , allocate references to track )
cl-max-refs @ \ current number of references allocated
5 * 4 / dup cl-max-refs ! \ new maximum allowed
\ cl.indent ." Resize # references to " dup . cr
cl-references @ swap resize dup ?error
: CL.ALLOC.DICT ( -- , allocate dictionary to build into )
cl-initial-dict @ \ initial dictionary size
\ kludge dictionary if testing
cl-dict-alloc @ code-base @ - cl-dict-ptr +!
code-base @ cl-dict-base !
cl-dict-alloc @ cl-dict-base !
." cl-dict-alloc = $" cl-dict-alloc @ .hex cr
." cl-dict-base = $" cl-dict-base @ .hex cr
." cl-dict-ptr = $" cl-dict-ptr @ .hex cr
: CODEADDR>DATASIZE { code-addr -- datasize }
\ Determine size of any literal data following execution token.
\ Examples are text following (."), or branch offsets.
['] (literal) OF cell ENDOF \ a number
['] 0branch OF cell ENDOF \ branch offset
['] (+loop) OF cell ENDOF
['] (.") OF code-addr cell+ c@ 1+ ENDOF \ text
['] (s") OF code-addr cell+ c@ 1+ ENDOF
['] (c") OF code-addr cell+ c@ 1+ ENDOF
: XT>SIZE ( xt -- wordsize , including code and data )
dup c@ 1+ + aligned 8 + \ get next name
name> >code \ where is next word
\ ------------------------------------------------------------------
: CL.TRAVERSE.SECONDARY { code-addr ca-process | xt dsize -- }
\ scan secondary and pass each code-address to ca-process
\ CA-PROCESS ( code-addr -- , required stack action for vector )
cl-ref-level @ cl-level-abort @ > abort" Clone exceeded CL-ABORT-LEVEL"
\ cl.indent ." CL.TRAVERSE.SECONDARY - code-addr = $" code-addr .hex ." , xt = $" xt .hex cr
code-addr codeaddr>datasize -> dsize \ any data after this?
code-addr ca-process execute \ process it
code-addr cell+ dsize + aligned -> code-addr \ skip past data
\ !!! Bummer! EXIT called in middle of secondary will cause early stop.
xt ['] EXIT = \ stop when we get to EXIT
\ ------------------------------------------------------------------
\ ------------------------------------------------------------------
: CL.REF[] ( index -- clref )
: CL.DUMP.REFS ( -- , print references )
dup s@ clr_OriginalXT >name id. ." => "
dup s@ clr_TotalSize . cr
: CL.XT>REF_INDEX { xt | indx flag -- index flag , true if found }
\ cl.indent ." CL.XT>REF_INDEX - indx = " indx . cr
indx cl.ref[] s@ clr_OriginalXT
\ cl.indent ." CL.XT>REF_INDEX - clr_OriginalXT = " dup . cr
\ cl.indent ." CL.XT>REF_INDEX - " xt >name id. space indx . flag . cr
: CL.ADD.REF { xt | clref -- , add referenced secondary to list }
cl-references @ 0= abort" CL.ADD.REF - References not allocated!"
\ do we need to allocate more room?
cl-num-refs @ cl-max-refs @ >=
cl-num-refs @ cl.ref[] -> clref \ index into array
xt clref s! clr_OriginalXT
xt xt>size clref s! clr_TotalSize
\ ------------------------------------------------------------------
\ called by cl.traverse.secondary to compile each piece of secondary
: CL.RECOMPILE.SECONDARY { code-addr | xt clref dsize -- , }
\ recompile to new location
\ cl.indent ." CL.RECOMPILE.SECONDARY - enter - " .s cr
\ cl.indent ." CL.RECOMPILE.SECONDARY - xt = $" dup .hex dup >name id. cr
dup 0= abort" CL.RECOMPILE.SECONDARY - unresolved NewXT"
cl.indent ." CL.RECOMPILE.SECONDARY - xt not in ref table!" cr
\ transfer any literal data
code-addr codeaddr>datasize -> dsize
\ cl.indent ." CL.RECOMPILE.SECONDARY - copy inline data of size" dsize . cr
code-addr cell+ cl-dict-ptr @ cl.dict[] dsize move
cl-dict-ptr @ dsize + aligned cl-dict-ptr !
\ cl.indent ." CL.RECOMPILE.SECONDARY - leave - " .s cr
: CL.RECOMPILE.REF { indx | clref codesize datasize -- }
\ all references have been resolved so recompile new secondary
clref s@ clr_OriginalXT >name id. ." recompiled at $"
cl-dict-ptr @ .hex cr \ new address
cl-dict-ptr @ clref s! clr_NewXT
\ traverse this secondary and compile into new dictionary
>code ['] cl.recompile.secondary cl.traverse.secondary
\ determine whether there is any data following definition
clref s@ clr_NewXT - -> codesize \ size of cloned code
clref s@ clr_TotalSize \ total bytes
." Move data: data size = " datasize . ." codesize = " codesize . cr
\ copy any data that followed definition
clref s@ clr_OriginalXT >code codesize +
clref s@ clr_NewXT cl-dict-base @ + codesize +
datasize cl-dict-ptr +! \ allot space in clone dictionary
depth r> - abort" Stack depth change in CL.RECOMPILE.REF"
\ ------------------------------------------------------------------
: CL.SCAN.SECONDARY ( code-addr -- , scan word and add referenced secondaries to list )
\ cl.indent ." CL.SCAN.SECONDARY - enter - " .s cr
cl-ref-level @ cl-level-max @ MAX cl-level-max !
\ cl.indent ." CL.SCAN.SECONDARY - xt = " dup . dup >name id. cr
\ cl.indent ." CL.SCAN.SECONDARY - found primitive." cr
drop \ indx \ already referenced once so ignore
>code 'self cl.traverse.secondary \ use 'self for recursion!
r> cl.recompile.ref \ now that all refs resolved, recompile
\ cl.indent ." CL.SCAN.SECONDARY - leave - " .s cr
depth r> - abort" Stack depth change in CL.SCAN.SECONDARY"
: CL.CLONE.XT ( xt -- , scan top word and add referenced secondaries to list )
dup primitive? abort" Cannot CLONE a PRIMITIVE word!"
dup cl.add.ref \ word being cloned is top of ref list
>code ['] cl.scan.secondary cl.traverse.secondary
\ ------------------------------------------------------------------
: CL.XT>NEW_XT ( xt -- xt' , convert normal xt to xt in cloned dict )
cl.xt>ref_index 0= abort" not in cloned dictionary!"
: CL.XT>NEW_ADDR ( xt -- addr , addr in cloned dict )
." Clone scan went " cl-level-max @ . ." levels deep." cr
." Clone scanned " cl-num-refs @ . ." secondaries." cr
." New dictionary size = " cl-dict-ptr @ cl-dict-base @ - . cr
\ ------------------------------------------------------------------
: CL.TERM ( -- , cleanup )
['] first_colon cl-dict-ptr !
: 'CLONE ( xt -- , clone dictionary from this word )
IF ." WARNING - CL-TEST-MODE on so we can't save cloned image." cr
: SAVE-CLONE ( <filename> -- )
." Save cloned image in " dup count type
drop ." SAVE-CLONE unimplemented!" \ %Q
\ ---------------------------------- TESTS --------------------
cl-test-mode @ not abort" CL-TEST-MODE not on!"
0 cl.ref[] s@ clr_NewXT execute
cl-test-mode @ abort" CL-TEST-MODE on!"
0 cl.ref[] s@ clr_NewXT \ get cloned execution token
cl-dict-base @ code-base !
\ WARNING - code-base munged, only execute primitives or cloned code
code-base ! \ restore code base for normal
." TCL4 succeded! Yay!" cr
\ do deferred words get cloned!
: TCL.DOIT ." Hello Fred!" cr ;