factor/library/compiler/xt.factor

193 lines
5.2 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-21 02:43:41 -05:00
USING: assembler compiler-backend compiler-frontend 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 ;
: cell-just-compiled compiled-offset cell - ;
2005-12-09 00:02:41 -05:00
: 4-just-compiled compiled-offset 4 - ;
2005-12-11 15:14:41 -05:00
: rel-absolute-cell 0 ;
: rel-absolute 1 ;
: rel-relative 2 ;
: rel-2/2 3 ;
2005-12-11 15:14:41 -05:00
: rel-type, ( arg class type -- )
#! Write a relocation instruction for the runtime image
#! loader.
2005-12-11 15:37:16 -05:00
over >r >r >r 16 shift r> 8 shift bitor r> bitor rel,
compiled-offset r> rel-absolute-cell = cell 4 ? - rel, ;
2005-12-11 15:14:41 -05:00
: rel-dlsym ( name dll class -- )
>r cons add-literal compiled-base - cell / r>
1 rel-type, ;
2005-12-11 15:14:41 -05:00
: rel-address ( class -- )
2005-12-04 16:20:17 -05:00
#! Relocate address just compiled.
2005-12-11 15:37:16 -05:00
dup rel-relative = [ drop ] [ 0 swap 2 rel-type, ] if ;
2005-12-11 15:14:41 -05:00
: rel-word ( word class -- )
over primitive? [
>r word-primitive r> 0 rel-type,
] [
rel-address drop
] if ;
2005-12-04 16:20:17 -05:00
2005-12-11 15:14:41 -05:00
: rel-userenv ( n class -- ) 3 rel-type, ;
2005-03-15 22:23:52 -05:00
2005-12-11 15:37:16 -05:00
: rel-cards ( class -- ) 0 swap 4 rel-type, ;
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 -- )
2005-12-11 15:14:41 -05:00
dup rel-relative rel-word
2005-12-04 16:20:17 -05:00
compiled-offset <relative>
2005-12-09 00:02:41 -05:00
4-just-compiled <fixup-4> deferred-xt ;
: relative-3 ( word -- )
2005-12-11 15:14:41 -05:00
#! Labels only -- no image relocation information saved
2005-12-09 00:02:41 -05:00
4-just-compiled <relative>
4-just-compiled <fixup-3> deferred-xt ;
: relative-2 ( word -- )
2005-12-11 15:14:41 -05:00
#! Labels only -- no image relocation information saved
2005-12-09 00:02:41 -05:00
4-just-compiled <relative>
4-just-compiled <fixup-2> deferred-xt ;
: relative-2/2 ( word -- )
2005-12-11 15:14:41 -05:00
#! Labels only -- no image relocation information saved
2005-12-09 00:02:41 -05:00
compiled-offset <relative>
4-just-compiled <fixup-2/2> deferred-xt ;
2005-12-04 22:06:12 -05:00
: absolute-4 ( word -- )
2005-12-11 15:14:41 -05:00
dup rel-absolute rel-word
2005-12-09 00:02:41 -05:00
<absolute> 4-just-compiled <fixup-4> deferred-xt ;
: absolute-2/2 ( word -- )
2005-12-11 15:14:41 -05:00
dup rel-2/2 rel-word
2005-12-09 00:02:41 -05:00
<absolute> cell-just-compiled <fixup-2/2> deferred-xt ;
2005-12-04 22:06:12 -05:00
2005-12-04 16:20:17 -05:00
: absolute-cell ( word -- )
2005-12-11 15:14:41 -05:00
dup rel-absolute-cell rel-word
2005-12-09 00:02:41 -05:00
<absolute> cell-just-compiled <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-21 02:43:41 -05:00
dup compiled?
over label? or
2005-12-21 02:49:24 -05:00
over linearized get ?hash or
2005-12-21 02:43:41 -05:00
over compile-words get member? or
swap compiled-xts get hash or ;
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 ;