factor/library/compiler/generator/xt.factor

94 lines
2.2 KiB
Factor
Raw Normal View History

2006-05-15 00:03:55 -04:00
! Copyright (C) 2004, 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
IN: compiler
2006-05-15 00:03:55 -04:00
USING: arrays assembler errors generic hashtables kernel
kernel-internals math namespaces prettyprint queues
2006-05-02 20:26:48 -04:00
sequences strings vectors words ;
2005-03-14 20:09:32 -05:00
2006-08-10 14:39:12 -04:00
DEFER: (compile)
2006-08-10 14:39:12 -04:00
: compiled-offset ( -- n ) building get length code-format * ;
2006-08-10 14:39:12 -04:00
TUPLE: label offset ;
2006-04-28 18:38:48 -04:00
2006-08-10 14:39:12 -04:00
C: label ( -- label ) ;
: define-label ( name -- ) <label> swap set ;
: resolve-label ( label -- )
2006-08-09 03:25:15 -04:00
compiled-offset swap set-label-offset ;
2006-04-28 18:38:48 -04:00
2005-12-04 16:23:58 -05:00
SYMBOL: compiled-xts
2006-08-09 03:25:15 -04:00
: save-xt ( word xt -- )
swap dup unchanged-word compiled-xts get set-hash ;
2005-12-04 16:23:58 -05:00
SYMBOL: literal-table
2005-12-04 16:29:30 -05:00
: add-literal ( obj -- n )
dup literal-table get [ eq? ] find-with drop dup -1 > [
nip
] [
drop literal-table get dup length >r push r>
] if ;
2005-12-04 16:29:30 -05:00
2005-12-04 16:20:17 -05:00
SYMBOL: relocation-table
2006-08-10 14:39:12 -04:00
SYMBOL: label-table
2005-12-11 15:14:41 -05:00
: rel-absolute-cell 0 ;
: rel-absolute 1 ;
: rel-relative 2 ;
: rel-absolute-2/2 3 ;
: rel-relative-2/2 4 ;
: rel-relative-2 5 ;
: rel-relative-3 6 ;
2006-08-10 00:14:43 -04:00
: (rel) ( arg class type offset -- { type offset } )
2005-12-11 15:14:41 -05:00
#! Write a relocation instruction for the runtime image
#! loader.
2006-08-10 00:14:43 -04:00
pick rel-absolute-cell = cell 4 ? -
>r >r >r 16 shift r> 8 shift bitor r> bitor r>
2array ;
: rel, ( arg class type -- )
2006-08-10 00:14:43 -04:00
compiled-offset (rel) relocation-table get swap nappend ;
2005-12-11 15:14:41 -05:00
: rel-dlsym ( name dll class -- )
>r 2array add-literal r> 1 rel, ;
: rel-here ( class -- )
dup rel-relative = [ drop ] [ 0 swap 2 rel, ] if ;
2005-12-11 15:14:41 -05:00
: rel-word ( word class -- )
over primitive?
[ >r word-primitive r> 0 ] [ >r add-literal r> 5 ] if
rel, ;
2005-12-04 16:20:17 -05:00
: rel-cards ( class -- ) 0 swap 3 rel, ;
2005-03-15 22:23:52 -05:00
: rel-literal ( literal class -- )
>r add-literal r> 4 rel, ;
: rel-label ( label class -- )
2006-08-10 14:39:12 -04:00
compiled-offset 3array label-table get push ;
2006-08-10 00:14:43 -04:00
: generate-labels ( -- )
2006-08-10 14:39:12 -04:00
label-table get [
2006-08-10 00:14:43 -04:00
first3 >r >r label-offset r> 6 r> (rel)
relocation-table get swap nappend
] each ;
: compiling? ( word -- ? )
{
{ [ dup compiled-xts get hash-member? ] [ drop t ] }
{ [ dup word-changed? ] [ drop f ] }
2006-08-12 15:58:32 -04:00
{ [ t ] [ compiled? ] }
} cond ;
: with-compiler ( quot -- )
2005-06-07 03:44:34 -04:00
[
2005-11-27 17:45:48 -05:00
H{ } clone compiled-xts set
2005-06-07 03:44:34 -04:00
call
compiled-xts get hash>alist finalize-compile
2005-06-07 03:44:34 -04:00
] with-scope ;