factor/library/compiler/xt.factor

154 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-03-15 22:23:52 -05:00
USING: assembler errors generic kernel lists math namespaces
strings vectors words ;
2005-03-14 20:09:32 -05:00
! To support saving compiled code to disk, generator words
! append relocation instructions to this vector.
SYMBOL: relocation-table
: rel, ( n -- ) relocation-table get vector-push ;
: relocating compiled-offset cell - rel, ;
: rel-primitive ( word rel/abs -- )
#! If flag is true; relative.
0 1 ? rel, relocating word-primitive rel, ;
: rel-dlsym ( name dll rel/abs -- )
#! If flag is true; relative.
2 3 ? rel, relocating cons intern-literal rel, ;
: rel-address ( rel/abs -- )
#! Relocate address just compiled. If flag is true,
#! relative, and there is nothing to do.
[ 4 rel, relocating 0 rel, ] unless ;
: rel-word ( word rel/abs -- )
#! If flag is true; relative.
over primitive? [ rel-primitive ] [ nip rel-address ] ifte ;
! 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.
!
! This has the advantage that we can compile a word before the
! words it depends on and perform a fixup later; among other
! things this enables mutually recursive words.
SYMBOL: compiled-xts
: save-xt ( word -- )
2004-12-10 21:39:27 -05:00
compiled-offset swap compiled-xts [ acons ] change ;
: commit-xt ( xt word -- )
dup t "compiled" set-word-prop set-word-xt ;
: commit-xts ( -- )
2005-03-14 11:25:41 -05:00
#! We must flush the instruction cache on PowerPC.
flush-icache
compiled-xts get [ unswons commit-xt ] each
compiled-xts off ;
: compiled-xt ( word -- xt )
2005-01-02 23:57:54 -05:00
dup compiled-xts get assoc [ word-xt ] ?unless ;
! Words being compiled are consed onto this list. When a word
! is encountered that has not been previously compiled, it is
! consed onto this list. Compilation stops when the list is
! empty.
SYMBOL: compile-words
2005-03-15 22:23:52 -05:00
! deferred-xts is a list of objects responding to the fixup
! generic.
SYMBOL: deferred-xts
! Some machinery to allow forward references
GENERIC: fixup ( object -- )
TUPLE: relative word where to ;
: just-compiled compiled-offset 4 - ;
C: relative ( word -- )
dup t rel-word
[ set-relative-word ] keep
[ just-compiled swap set-relative-where ] keep
[ compiled-offset swap set-relative-to ] keep ;
: relative ( word -- ) <relative> deferred-xts cons@ ;
: relative-fixup ( relative -- addr )
dup relative-word compiled-xt swap relative-to - ;
2005-03-15 22:23:52 -05:00
M: relative fixup ( relative -- )
dup relative-fixup swap relative-where set-compiled-cell ;
2005-03-15 22:23:52 -05:00
TUPLE: absolute word where ;
C: absolute ( word -- )
dup f rel-word
[ set-absolute-word ] keep
[ just-compiled swap set-absolute-where ] keep ;
: absolute ( word -- ) <absolute> deferred-xts cons@ ;
M: absolute fixup ( absolute -- )
dup absolute-word compiled-xt
swap absolute-where set-compiled-cell ;
! Fixups where the address is inside a bitfield in the
! instruction.
TUPLE: relative-bitfld mask ;
2005-03-15 22:23:52 -05:00
C: relative-bitfld ( word mask -- )
[ set-relative-bitfld-mask ] keep
2005-03-15 22:23:52 -05:00
[ >r <relative> r> set-delegate ] keep
[ just-compiled swap set-relative-to ] keep ;
: relative-24 ( word -- )
BIN: 11111111111111111111111100 <relative-bitfld>
deferred-xts cons@ ;
2005-03-15 22:23:52 -05:00
: relative-14 ( word -- )
BIN: 1111111111111100 <relative-bitfld>
deferred-xts cons@ ;
: check-bitfld ( fixup -- )
2005-03-15 22:23:52 -05:00
#! Check that the address can fit in a 24-bit wide address
#! field, used by PowerPC instructions.
dup relative-fixup dup rot relative-bitfld-mask bitand = [
2005-03-19 00:30:49 -05:00
"Cannot jump this far" throw
2005-03-15 22:23:52 -05:00
] unless ;
M: relative-bitfld fixup
dup check-bitfld
dup relative-fixup swap relative-where
[ compiled-cell bitor ] keep
2005-03-15 22:23:52 -05:00
set-compiled-cell ;
: compiling? ( word -- ? )
#! A word that is compiling or already compiled will not be
#! added to the list of words to be compiled.
dup compiled? [
drop t
] [
dup compile-words get contains? [
drop t
] [
compiled-xts get assoc
] ifte
] ifte ;
2005-03-15 22:23:52 -05:00
: fixup-xts ( -- )
deferred-xts get [ fixup ] each deferred-xts off ;
: with-compiler ( quot -- )
2005-03-15 22:23:52 -05:00
[ call fixup-xts commit-xts ] with-scope ;
: postpone-word ( word -- )
dup compiling? [ drop ] [ compile-words unique@ ] ifte ;