1 \ @(#) clone.fth 97/12/10 1.1
4 \ Create the smallest dictionary required to run an application.
6 \ Clone decompiles the Forth dictionary starting with the top
7 \ word in the program. It then moves all referenced secondaries
8 \ into a new dictionary.
10 \ This work was inspired by the CLONE feature that Mike Haas wrote
11 \ for JForth. Mike's CLONE disassembled 68000 machine code then
12 \ reassembled it which is much more difficult.
14 \ Copyright Phil Burk & 3DO 1994
16 \ O- trap custom 'C' calls
17 \ O- investigate ALITERAL, XLITERAL, use XLITERAL in [']
23 : PRIMITIVE? ( xt -- flag , true if primitive )
27 : 'SELF ( -- xt , return xt of word being compiled )
35 long clr_OriginalXT \ original XT of word
36 long clr_NewXT \ corresponding XT in cloned dictionary
37 long clr_TotalSize \ size including data in body
40 variable CL-INITIAL-REFS \ initial number of refs to allocate
42 variable CL-REF-LEVEL \ level of threading while scanning
43 variable CL-NUM-REFS \ number of secondaries referenced
44 variable CL-MAX-REFS \ max number of secondaries allocated
45 variable CL-LEVEL-MAX \ max level reached while scanning
46 variable CL-LEVEL-ABORT \ max level before aborting
48 variable CL-REFERENCES \ pointer to cl.reference array
49 variable CL-TRACE \ print debug stuff if true
51 \ Cloned dictionary builds in allocated memory but XTs are relative
52 \ to normal code-base, if CL-TEST-MODE true.
55 variable CL-INITIAL-DICT \ initial size of dict to allocate
56 20 1024 * cl-initial-dict !
57 variable CL-DICT-SIZE \ size of allocated cloned dictionary
58 variable CL-DICT-BASE \ pointer to virtual base of cloned dictionary
59 variable CL-DICT-ALLOC \ pointer to allocated dictionary memory
60 variable CL-DICT-PTR \ rel pointer index into cloned dictionary
65 cl-ref-level @ 2* 2* spaces
67 : CL.DUMP.NAME ( xt -- )
72 : CL.DICT[] ( relptr -- addr )
76 : CL, ( cell -- , comma into clone dictionary )
77 cl-dict-ptr @ cl.dict[] !
82 : CL.FREE.DICT ( -- , free dictionary we built into )
90 : CL.FREE.REFS ( -- , free dictionary we built into )
98 : CL.ALLOC.REFS ( -- , allocate references to track )
99 cl-initial-refs @ \ initial number of references
100 dup cl-max-refs ! \ maximum allowed
101 sizeof() cl.reference *
106 : CL.RESIZE.REFS ( -- , allocate references to track )
107 cl-max-refs @ \ current number of references allocated
108 5 * 4 / dup cl-max-refs ! \ new maximum allowed
109 \ cl.indent ." Resize # references to " dup . cr
110 sizeof() cl.reference *
111 cl-references @ swap resize dup ?error
116 : CL.ALLOC.DICT ( -- , allocate dictionary to build into )
117 cl-initial-dict @ \ initial dictionary size
122 \ kludge dictionary if testing
125 cl-dict-alloc @ code-base @ - cl-dict-ptr +!
126 code-base @ cl-dict-base !
128 cl-dict-alloc @ cl-dict-base !
131 ." cl-dict-alloc = $" cl-dict-alloc @ .hex cr
132 ." cl-dict-base = $" cl-dict-base @ .hex cr
133 ." cl-dict-ptr = $" cl-dict-ptr @ .hex cr
136 : CODEADDR>DATASIZE { code-addr -- datasize }
137 \ Determine size of any literal data following execution token.
138 \ Examples are text following (."), or branch offsets.
141 ['] (literal) OF cell ENDOF \ a number
142 ['] 0branch OF cell ENDOF \ branch offset
143 ['] branch OF cell ENDOF
145 ['] (?do) OF cell ENDOF
146 ['] (loop) OF cell ENDOF
147 ['] (+loop) OF cell ENDOF
148 ['] (.") OF code-addr cell+ c@ 1+ ENDOF \ text
149 ['] (s") OF code-addr cell+ c@ 1+ ENDOF
150 ['] (c") OF code-addr cell+ c@ 1+ ENDOF
155 : XT>SIZE ( xt -- wordsize , including code and data )
162 dup c@ 1+ + aligned 8 + \ get next name
163 name> >code \ where is next word
168 \ ------------------------------------------------------------------
169 : CL.TRAVERSE.SECONDARY { code-addr ca-process | xt dsize -- }
170 \ scan secondary and pass each code-address to ca-process
171 \ CA-PROCESS ( code-addr -- , required stack action for vector )
173 cl-ref-level @ cl-level-abort @ > abort" Clone exceeded CL-ABORT-LEVEL"
176 \ cl.indent ." CL.TRAVERSE.SECONDARY - code-addr = $" code-addr .hex ." , xt = $" xt .hex cr
177 code-addr codeaddr>datasize -> dsize \ any data after this?
178 code-addr ca-process execute \ process it
179 code-addr cell+ dsize + aligned -> code-addr \ skip past data
180 \ !!! Bummer! EXIT called in middle of secondary will cause early stop.
181 xt ['] EXIT = \ stop when we get to EXIT
186 \ ------------------------------------------------------------------
188 : CL.DUMP.XT ( xt -- )
201 \ ------------------------------------------------------------------
202 : CL.REF[] ( index -- clref )
203 sizeof() cl.reference *
207 : CL.DUMP.REFS ( -- , print references )
212 dup s@ clr_OriginalXT >name id. ." => "
215 dup s@ clr_TotalSize . cr
220 : CL.XT>REF_INDEX { xt | indx flag -- index flag , true if found }
222 \ cl.indent ." CL.XT>REF_INDEX - indx = " indx . cr
223 indx cl-num-refs @ >=
227 indx cl.ref[] s@ clr_OriginalXT
228 \ cl.indent ." CL.XT>REF_INDEX - clr_OriginalXT = " dup . cr
240 \ cl.indent ." CL.XT>REF_INDEX - " xt >name id. space indx . flag . cr
243 : CL.ADD.REF { xt | clref -- , add referenced secondary to list }
244 cl-references @ 0= abort" CL.ADD.REF - References not allocated!"
246 \ do we need to allocate more room?
247 cl-num-refs @ cl-max-refs @ >=
252 cl-num-refs @ cl.ref[] -> clref \ index into array
253 xt clref s! clr_OriginalXT
255 xt xt>size clref s! clr_TotalSize
260 \ ------------------------------------------------------------------
262 \ called by cl.traverse.secondary to compile each piece of secondary
263 : CL.RECOMPILE.SECONDARY { code-addr | xt clref dsize -- , }
264 \ recompile to new location
265 \ cl.indent ." CL.RECOMPILE.SECONDARY - enter - " .s cr
267 \ cl.indent ." CL.RECOMPILE.SECONDARY - xt = $" dup .hex dup >name id. cr
277 dup 0= abort" CL.RECOMPILE.SECONDARY - unresolved NewXT"
280 cl.indent ." CL.RECOMPILE.SECONDARY - xt not in ref table!" cr
285 \ transfer any literal data
286 code-addr codeaddr>datasize -> dsize
289 \ cl.indent ." CL.RECOMPILE.SECONDARY - copy inline data of size" dsize . cr
290 code-addr cell+ cl-dict-ptr @ cl.dict[] dsize move
291 cl-dict-ptr @ dsize + aligned cl-dict-ptr !
293 \ cl.indent ." CL.RECOMPILE.SECONDARY - leave - " .s cr
296 : CL.RECOMPILE.REF { indx | clref codesize datasize -- }
297 \ all references have been resolved so recompile new secondary
299 indx cl.ref[] -> clref
303 clref s@ clr_OriginalXT >name id. ." recompiled at $"
304 cl-dict-ptr @ .hex cr \ new address
306 cl-dict-ptr @ clref s! clr_NewXT
308 \ traverse this secondary and compile into new dictionary
309 clref s@ clr_OriginalXT
310 >code ['] cl.recompile.secondary cl.traverse.secondary
312 \ determine whether there is any data following definition
314 clref s@ clr_NewXT - -> codesize \ size of cloned code
315 clref s@ clr_TotalSize \ total bytes
316 codesize - -> datasize
320 ." Move data: data size = " datasize . ." codesize = " codesize . cr
323 \ copy any data that followed definition
326 clref s@ clr_OriginalXT >code codesize +
327 clref s@ clr_NewXT cl-dict-base @ + codesize +
329 datasize cl-dict-ptr +! \ allot space in clone dictionary
332 depth r> - abort" Stack depth change in CL.RECOMPILE.REF"
335 \ ------------------------------------------------------------------
336 : CL.SCAN.SECONDARY ( code-addr -- , scan word and add referenced secondaries to list )
338 \ cl.indent ." CL.SCAN.SECONDARY - enter - " .s cr
339 cl-ref-level @ cl-level-max @ MAX cl-level-max !
341 \ cl.indent ." CL.SCAN.SECONDARY - xt = " dup . dup >name id. cr
346 \ cl.indent ." CL.SCAN.SECONDARY - found primitive." cr
350 drop \ indx \ already referenced once so ignore
355 >code 'self cl.traverse.secondary \ use 'self for recursion!
356 r> cl.recompile.ref \ now that all refs resolved, recompile
359 \ cl.indent ." CL.SCAN.SECONDARY - leave - " .s cr
360 depth r> - abort" Stack depth change in CL.SCAN.SECONDARY"
363 : CL.CLONE.XT ( xt -- , scan top word and add referenced secondaries to list )
364 dup primitive? abort" Cannot CLONE a PRIMITIVE word!"
368 dup cl.add.ref \ word being cloned is top of ref list
369 >code ['] cl.scan.secondary cl.traverse.secondary
373 \ ------------------------------------------------------------------
374 : CL.XT>NEW_XT ( xt -- xt' , convert normal xt to xt in cloned dict )
375 cl.xt>ref_index 0= abort" not in cloned dictionary!"
376 cl.ref[] s@ clr_NewXT
378 : CL.XT>NEW_ADDR ( xt -- addr , addr in cloned dict )
384 ." Clone scan went " cl-level-max @ . ." levels deep." cr
385 ." Clone scanned " cl-num-refs @ . ." secondaries." cr
386 ." New dictionary size = " cl-dict-ptr @ cl-dict-base @ - . cr
390 \ ------------------------------------------------------------------
391 : CL.TERM ( -- , cleanup )
399 ['] first_colon cl-dict-ptr !
404 : 'CLONE ( xt -- , clone dictionary from this word )
410 IF ." WARNING - CL-TEST-MODE on so we can't save cloned image." cr
414 : SAVE-CLONE ( <filename> -- )
416 ." Save cloned image in " dup count type
417 drop ." SAVE-CLONE unimplemented!" \ %Q
420 : CLONE ( <name> -- )
426 \ ---------------------------------- TESTS --------------------
430 cl-test-mode @ not abort" CL-TEST-MODE not on!"
431 0 cl.ref[] s@ clr_NewXT execute
435 : TEST.CLONE.REAL ( -- )
436 cl-test-mode @ abort" CL-TEST-MODE on!"
438 0 cl.ref[] s@ clr_NewXT \ get cloned execution token
439 cl-dict-base @ code-base !
440 \ WARNING - code-base munged, only execute primitives or cloned code
442 code-base ! \ restore code base for normal
466 ." VAR1 = " var1 @ . cr
471 ." TCL4 succeded! Yay!" cr
475 \ do deferred words get cloned!
478 : TCL.DOIT ." Hello Fred!" cr ;
479 ' tcl.doit is tcl.vector