factor/library/compiler/generator/xt.factor

107 lines
2.7 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-09 03:25:15 -04:00
: compiled-offset ( -- n ) building get length code-format * ;
TUPLE: label # offset ;
SYMBOL: label-table
: push-label ( label -- )
2006-08-09 03:25:15 -04:00
label-table get dup length pick set-label-# push ;
2006-04-28 18:38:48 -04:00
2006-08-10 00:14:43 -04:00
C: label ( -- label ) dup push-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 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
SYMBOL: label-relocation-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 00:14:43 -04:00
compiled-offset 3array label-relocation-table get push ;
: generate-labels ( -- )
label-relocation-table get [
first3 >r >r label-offset r> 6 r> (rel)
relocation-table get swap nappend
] each ;
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 compile-words get member? or
swap compiled-xts get hash-member? or ;
: with-compiler ( quot -- )
2005-06-07 03:44:34 -04:00
[
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
compiled-xts get hash>alist finalize-compile
2005-06-07 03:44:34 -04:00
] 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 ;