2006-05-15 00:03:55 -04:00
|
|
|
! Copyright (C) 2004, 2006 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2004-12-01 19:48:08 -05:00
|
|
|
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 * ;
|
2006-08-09 02:12:01 -04:00
|
|
|
|
|
|
|
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-09 03:25:15 -04:00
|
|
|
C: label ( -- label )
|
|
|
|
compiled-offset over set-label-offset dup push-label ;
|
2006-08-09 02:12:01 -04:00
|
|
|
|
|
|
|
: 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
|
|
|
|
|
|
|
: commit-xts ( -- )
|
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
|
|
|
|
2006-08-08 01:38:32 -04:00
|
|
|
SYMBOL: literal-table
|
2005-12-04 16:29:30 -05:00
|
|
|
|
2006-08-08 01:38:32 -04: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-09 02:12:01 -04:00
|
|
|
SYMBOL: label-relocation-table
|
2004-12-01 19:48:08 -05:00
|
|
|
|
2005-12-11 15:14:41 -05:00
|
|
|
: rel-absolute-cell 0 ;
|
|
|
|
: rel-absolute 1 ;
|
|
|
|
: rel-relative 2 ;
|
2006-08-08 01:38:32 -04:00
|
|
|
: rel-absolute-2/2 3 ;
|
|
|
|
: rel-relative-2/2 4 ;
|
|
|
|
: rel-relative-2 5 ;
|
|
|
|
: rel-relative-3 6 ;
|
|
|
|
|
2006-08-09 02:12:01 -04:00
|
|
|
: (rel) ( arg class type -- { m n } )
|
2005-12-11 15:14:41 -05:00
|
|
|
#! Write a relocation instruction for the runtime image
|
|
|
|
#! loader.
|
2006-08-09 02:12:01 -04:00
|
|
|
over >r >r >r 16 shift r> 8 shift bitor r> bitor
|
2006-08-09 03:25:15 -04:00
|
|
|
compiled-offset r> rel-absolute-cell = cell 4 ? - 2array ;
|
2006-08-09 02:12:01 -04:00
|
|
|
|
|
|
|
: rel, ( arg class type -- )
|
2006-08-09 03:25:15 -04:00
|
|
|
(rel) relocation-table get swap nappend ;
|
2006-08-09 02:12:01 -04:00
|
|
|
|
|
|
|
: label, ( arg class type -- )
|
2006-08-09 03:25:15 -04:00
|
|
|
(rel) label-relocation-table get swap nappend ;
|
2004-12-01 19:48:08 -05:00
|
|
|
|
2005-12-11 15:14:41 -05:00
|
|
|
: rel-dlsym ( name dll class -- )
|
2006-08-09 02:12:01 -04:00
|
|
|
>r 2array add-literal r> 1 rel, ;
|
2004-12-01 19:48:08 -05:00
|
|
|
|
2006-08-08 01:38:32 -04:00
|
|
|
: rel-here ( class -- )
|
2006-08-09 02:12:01 -04:00
|
|
|
dup rel-relative = [ drop ] [ 0 swap 2 rel, ] if ;
|
2004-12-01 19:48:08 -05:00
|
|
|
|
2005-12-11 15:14:41 -05:00
|
|
|
: rel-word ( word class -- )
|
2006-08-08 01:38:32 -04:00
|
|
|
over primitive?
|
|
|
|
[ >r word-primitive r> 0 ] [ >r add-literal r> 5 ] if
|
2006-08-09 02:12:01 -04:00
|
|
|
rel, ;
|
2005-12-04 16:20:17 -05:00
|
|
|
|
2006-08-09 02:12:01 -04:00
|
|
|
: rel-cards ( class -- ) 0 swap 3 rel, ;
|
2005-03-15 22:23:52 -05:00
|
|
|
|
2006-08-08 01:38:32 -04:00
|
|
|
: rel-literal ( literal class -- )
|
2006-08-09 02:12:01 -04:00
|
|
|
>r add-literal r> 4 rel, ;
|
|
|
|
|
|
|
|
: rel-label ( label class -- )
|
2006-08-09 03:25:15 -04:00
|
|
|
>r label-# r> 6 label, ;
|
2005-03-20 19:05:57 -05:00
|
|
|
|
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
|
2005-03-20 19:05:57 -05:00
|
|
|
|
2004-12-01 19:48:08 -05:00
|
|
|
: 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
|
2006-08-09 02:12:01 -04:00
|
|
|
swap compiled-xts get hash-member? or ;
|
2004-12-01 19:48:08 -05:00
|
|
|
|
|
|
|
: 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-10-29 23:25:38 -04:00
|
|
|
V{ } clone compile-words set
|
2005-06-07 03:44:34 -04:00
|
|
|
call
|
2006-08-08 01:38:32 -04:00
|
|
|
finalize-compile
|
2005-06-07 03:44:34 -04:00
|
|
|
commit-xts
|
|
|
|
] with-scope ;
|
2004-12-01 19:48:08 -05:00
|
|
|
|
|
|
|
: postpone-word ( word -- )
|
2005-09-14 00:37:50 -04:00
|
|
|
dup compiling? not over compound? and
|
|
|
|
[ dup compile-words get push ] when drop ;
|