factor/library/compiler/xt.factor

157 lines
4.3 KiB
Factor
Raw Normal View History

2005-03-14 11:25:41 -05:00
! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: compiler
2005-12-04 16:20:17 -05:00
USING: assembler errors generic hashtables kernel
kernel-internals lists math namespaces prettyprint sequences
strings vectors words ;
2005-03-14 20:09:32 -05:00
2005-12-04 16:23:58 -05:00
! We use a hashtable "compiled-xts" that maps words to
! xt's that are currently being compiled. The commit-xt's word
! sets the xt of each word in the hashtable to the value in the
! hastable.
SYMBOL: compiled-xts
: save-xt ( word -- )
compiled-offset swap compiled-xts get set-hash ;
: commit-xts ( -- )
#! We must flush the instruction cache on PowerPC.
flush-icache
2005-12-04 19:56:42 -05:00
compiled-xts get [ swap set-word-xt ] hash-each ;
2005-12-04 16:23:58 -05:00
: compiled-xt ( word -- xt )
dup compiled-xts get hash [ ] [ word-xt ] ?if ;
2005-12-04 16:29:30 -05:00
! deferred-xts is a vector of objects responding to the fixup
! generic.
SYMBOL: deferred-xts
: deferred-xt deferred-xts get push ;
2005-12-04 16:20:17 -05:00
! To support saving compiled code to disk, generator words
! append relocation instructions to this vector.
SYMBOL: relocation-table
2005-12-04 16:20:17 -05:00
: rel, ( n -- ) relocation-table get push ;
2005-12-04 16:20:17 -05:00
: relocating compiled-offset cell - rel, ;
2005-12-04 16:20:17 -05:00
: rel-type, ( rel/abs 16/16 type -- )
swap 8 shift bitor swap 16 shift bitor rel, ;
2005-12-04 16:20:17 -05:00
: rel-primitive ( word relative 16/16 -- )
0 rel-type, relocating word-primitive rel, ;
2005-12-04 16:20:17 -05:00
: rel-dlsym ( name dll rel/abs 16/16 -- )
1 rel-type, relocating cons add-literal rel, ;
2005-12-04 16:20:17 -05:00
: rel-address ( rel/abs 16/16 -- )
#! Relocate address just compiled.
over 1 = [ 2drop ] [ 2 rel-type, relocating 0 rel, ] if ;
2005-12-04 16:20:17 -05:00
: rel-word ( word rel/abs 16/16 -- )
pick primitive? [ rel-primitive ] [ rel-address drop ] if ;
: rel-userenv ( n 16/16 -- )
0 swap 3 rel-type, relocating rel, ;
2005-03-15 22:23:52 -05:00
2005-12-04 16:20:17 -05:00
: rel-cards ( 16/16 -- )
0 swap 4 rel-type, compiled-offset cell 2 * - rel, 0 rel, ;
2005-03-15 22:23:52 -05:00
2005-12-04 16:20:17 -05:00
! This is for fixing up forward references
GENERIC: resolve ( fixup -- addr )
2005-03-15 22:23:52 -05:00
2005-12-04 16:20:17 -05:00
TUPLE: absolute word ;
2005-03-15 22:23:52 -05:00
2005-12-04 16:20:17 -05:00
M: absolute resolve absolute-word compiled-xt ;
2005-03-15 22:23:52 -05:00
2005-12-04 16:20:17 -05:00
TUPLE: relative word to ;
2005-04-16 00:23:27 -04:00
2005-12-04 16:20:17 -05:00
M: relative resolve
[ relative-word compiled-xt ] keep relative-to - ;
2005-03-15 22:23:52 -05:00
2005-12-04 16:20:17 -05:00
GENERIC: fixup ( addr fixup -- )
2005-03-15 22:23:52 -05:00
2005-12-04 16:20:17 -05:00
TUPLE: fixup-cell at ;
2005-03-15 22:23:52 -05:00
2005-12-04 16:20:17 -05:00
C: fixup-cell ( resolver at -- fixup )
[ set-fixup-cell-at ] keep [ set-delegate ] keep ;
2005-03-15 22:23:52 -05:00
2005-12-04 16:20:17 -05:00
M: fixup-cell fixup ( addr fixup -- )
fixup-cell-at set-compiled-cell ;
2005-03-15 22:23:52 -05:00
2005-12-04 16:20:17 -05:00
TUPLE: fixup-4 at ;
2005-12-04 16:20:17 -05:00
C: fixup-4 ( resolver at -- fixup )
[ set-fixup-4-at ] keep [ set-delegate ] keep ;
2005-03-15 22:23:52 -05:00
2005-12-04 16:20:17 -05:00
M: fixup-4 fixup ( addr fixup -- )
fixup-4-at set-compiled-4 ;
2005-03-15 22:23:52 -05:00
2005-12-04 16:20:17 -05:00
TUPLE: fixup-bitfield at mask ;
2005-03-15 22:23:52 -05:00
2005-12-04 16:20:17 -05:00
C: fixup-bitfield ( resolver at mask -- fixup )
[ set-fixup-bitfield-mask ] keep
[ set-fixup-bitfield-at ] keep
[ set-delegate ] keep ;
2005-03-15 22:23:52 -05:00
2005-12-04 16:20:17 -05:00
: <fixup-3> ( resolver at -- )
#! Only for PowerPC branch instructions.
BIN: 11111111111111111111111100 <fixup-bitfield> ;
2005-03-15 22:23:52 -05:00
2005-12-04 16:20:17 -05:00
: <fixup-2> ( resolver at -- )
#! Only for PowerPC conditional branch instructions.
BIN: 1111111111111100 <fixup-bitfield> ;
: or-compiled ( n off -- )
[ compiled-cell bitor ] keep set-compiled-cell ;
2005-12-04 16:20:17 -05:00
M: fixup-bitfield fixup ( addr fixup -- )
[ fixup-bitfield-mask bitand ] keep
fixup-bitfield-at or-compiled ;
TUPLE: fixup-2/2 at ;
C: fixup-2/2 ( resolver at -- fixup )
[ set-fixup-2/2-at ] keep [ set-delegate ] keep ;
M: fixup-2/2 fixup ( addr fixup -- )
fixup-2/2-at >r w>h/h r> tuck 4 - or-compiled or-compiled ;
2005-12-04 16:20:17 -05:00
: relative-4 ( word -- )
dup 1 0 rel-word
compiled-offset <relative>
compiled-offset 4 - <fixup-4> deferred-xt ;
2005-12-04 16:20:17 -05:00
: absolute-cell ( word -- )
dup 0 0 rel-word
<absolute> compiled-offset cell - <fixup-cell> deferred-xt ;
2005-12-04 16:20:17 -05:00
! When a word is encountered that has not been previously
! compiled, it is pushed onto this vector. Compilation stops
! when the vector is empty.
SYMBOL: compile-words
: compiling? ( word -- ? )
#! A word that is compiling or already compiled will not be
#! added to the list of words to be compiled.
2005-12-04 16:29:30 -05:00
dup compiled? over compile-words get member? or
[ drop t ] [ compiled-xts get hash ] if ;
2005-03-15 22:23:52 -05:00
: fixup-xts ( -- )
2005-12-04 19:56:42 -05:00
deferred-xts get [ dup resolve swap fixup ] each ;
: with-compiler ( quot -- )
2005-06-07 03:44:34 -04:00
[
2005-12-04 19:56:42 -05:00
V{ } clone deferred-xts set
2005-11-27 17:45:48 -05:00
H{ } clone compiled-xts set
V{ } clone compile-words set
2005-06-07 03:44:34 -04:00
call
fixup-xts
commit-xts
] with-scope ;
: postpone-word ( word -- )
2005-09-14 00:37:50 -04:00
dup compiling? not over compound? and
[ dup compile-words get push ] when drop ;