1 ! Copyright (C) 2004, 2006 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
4 USING: alien arrays assembler errors generic hashtables kernel
5 kernel-internals math namespaces prettyprint queues
6 sequences strings vectors words ;
10 : compiled-offset ( -- n ) building get length code-format * ;
14 C: label ( -- label ) ;
16 : define-label ( name -- ) <label> swap set ;
18 : resolve-label ( label/name -- )
19 dup string? [ get ] when
20 compiled-offset swap set-label-offset ;
24 : save-xt ( word xt -- )
25 swap dup unchanged-word compiled-xts get set-hash ;
27 : push-new* ( obj table -- n )
28 2dup [ eq? ] find-with drop dup -1 > [
31 drop dup length >r push r>
36 : add-literal ( obj -- n ) literal-table get push-new* ;
40 : add-word ( word -- n ) word-table get push-new* ;
42 SYMBOL: relocation-table
45 : rel-absolute-cell 0 ;
48 : rel-absolute-2/2 3 ;
49 : rel-relative-2/2 4 ;
53 : (rel) ( arg class type offset -- pair )
54 #! Write a relocation instruction for the runtime image
56 pick rel-absolute-cell = cell 4 ? -
57 >r >r >r 16 shift r> 8 shift bitor r> bitor r>
60 : rel, ( arg class type -- )
61 compiled-offset (rel) relocation-table get swap nappend ;
63 : rel-dlsym ( name dll class -- )
64 >r >r string>char-alien r> 2array add-literal r> 1 rel, ;
66 : rel-here ( class -- )
67 dup rel-relative = [ drop ] [ 0 swap 2 rel, ] if ;
69 : rel-word ( word class -- )
71 [ >r word-primitive r> 0 ] [ >r add-word r> 5 ] if
74 : rel-cards ( class -- ) 0 swap 3 rel, ;
76 : rel-literal ( literal class -- )
77 >r add-literal r> 4 rel, ;
79 : rel-label ( label class -- )
80 compiled-offset 3array label-table get push ;
82 : generate-labels ( -- )
84 first3 >r >r label-offset r> 6 r> (rel)
85 relocation-table get swap nappend
88 : compiling? ( word -- ? )
90 { [ dup compiled-xts get hash-member? ] [ drop t ] }
91 { [ dup word-changed? ] [ drop f ] }
92 { [ t ] [ compiled? ] }
95 : with-compiler ( quot -- )
97 H{ } clone compiled-xts set
99 compiled-xts get hash>alist finalize-compile