2008-01-09 04:52:08 -05:00
|
|
|
! Copyright (C) 2008 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-08-31 02:34:00 -04:00
|
|
|
USING: accessors arrays kernel continuations assocs namespaces
|
2008-08-30 03:31:27 -04:00
|
|
|
sequences words vocabs definitions hashtables init sets
|
2008-08-31 06:19:16 -04:00
|
|
|
math math.order classes classes.algebra ;
|
2008-01-09 04:52:08 -05:00
|
|
|
IN: compiler.units
|
|
|
|
|
|
|
|
SYMBOL: old-definitions
|
|
|
|
SYMBOL: new-definitions
|
|
|
|
|
|
|
|
TUPLE: redefine-error def ;
|
|
|
|
|
|
|
|
: redefine-error ( definition -- )
|
2008-04-13 16:06:09 -04:00
|
|
|
\ redefine-error boa
|
2008-01-09 04:52:08 -05:00
|
|
|
{ { "Continue" t } } throw-restarts drop ;
|
|
|
|
|
|
|
|
: add-once ( key assoc -- )
|
2008-05-25 20:44:37 -04:00
|
|
|
2dup key? [ over redefine-error ] when conjoin ;
|
2008-01-09 04:52:08 -05:00
|
|
|
|
|
|
|
: (remember-definition) ( definition loc assoc -- )
|
2008-11-02 04:10:27 -05:00
|
|
|
[ over set-where ] dip add-once ;
|
2008-01-09 04:52:08 -05:00
|
|
|
|
|
|
|
: remember-definition ( definition loc -- )
|
|
|
|
new-definitions get first (remember-definition) ;
|
|
|
|
|
2009-03-07 01:42:43 -05:00
|
|
|
: fake-definition ( definition -- )
|
|
|
|
old-definitions get [ delete-at ] with each ;
|
|
|
|
|
2008-01-09 04:52:08 -05:00
|
|
|
: remember-class ( class loc -- )
|
2008-11-02 04:10:27 -05:00
|
|
|
[ dup new-definitions get first key? [ dup redefine-error ] when ] dip
|
2008-01-09 04:52:08 -05:00
|
|
|
new-definitions get second (remember-definition) ;
|
|
|
|
|
|
|
|
: forward-reference? ( word -- ? )
|
|
|
|
dup old-definitions get assoc-stack
|
|
|
|
[ new-definitions get assoc-stack not ]
|
|
|
|
[ drop f ] if ;
|
|
|
|
|
|
|
|
SYMBOL: recompile-hook
|
|
|
|
|
|
|
|
: <definitions> ( -- pair ) { H{ } H{ } } [ clone ] map ;
|
|
|
|
|
|
|
|
SYMBOL: definition-observers
|
|
|
|
|
|
|
|
GENERIC: definitions-changed ( assoc obj -- )
|
|
|
|
|
2008-02-25 20:37:43 -05:00
|
|
|
[ V{ } clone definition-observers set-global ]
|
|
|
|
"compiler.units" add-init-hook
|
|
|
|
|
2008-01-09 04:52:08 -05:00
|
|
|
: add-definition-observer ( obj -- )
|
2008-02-25 20:37:43 -05:00
|
|
|
definition-observers get push ;
|
2008-01-09 04:52:08 -05:00
|
|
|
|
|
|
|
: remove-definition-observer ( obj -- )
|
|
|
|
definition-observers get delete ;
|
|
|
|
|
|
|
|
: notify-definition-observers ( assoc -- )
|
|
|
|
definition-observers get
|
2008-01-09 17:36:30 -05:00
|
|
|
[ definitions-changed ] with each ;
|
2008-01-09 04:52:08 -05:00
|
|
|
|
2008-01-09 16:51:55 -05:00
|
|
|
: changed-vocabs ( assoc -- vocabs )
|
2008-04-26 00:12:44 -04:00
|
|
|
[ drop word? ] assoc-filter
|
2008-06-28 03:36:20 -04:00
|
|
|
[ drop vocabulary>> dup [ vocab ] when dup ] assoc-map ;
|
2008-01-09 04:52:08 -05:00
|
|
|
|
2008-04-05 08:00:09 -04:00
|
|
|
: updated-definitions ( -- assoc )
|
2008-01-09 04:52:08 -05:00
|
|
|
H{ } clone
|
2008-01-09 16:51:55 -05:00
|
|
|
dup forgotten-definitions get update
|
2008-01-09 04:52:08 -05:00
|
|
|
dup new-definitions get first update
|
|
|
|
dup new-definitions get second update
|
2008-04-05 08:00:09 -04:00
|
|
|
dup changed-definitions get update
|
2008-01-09 16:51:55 -05:00
|
|
|
dup dup changed-vocabs update ;
|
2008-01-09 04:52:08 -05:00
|
|
|
|
2008-02-24 03:19:38 -05:00
|
|
|
: compile ( words -- )
|
2009-01-24 21:17:11 -05:00
|
|
|
recompile-hook get call modify-code-heap ;
|
2008-02-24 03:19:38 -05:00
|
|
|
|
2008-03-31 02:19:34 -04:00
|
|
|
SYMBOL: outdated-tuples
|
|
|
|
SYMBOL: update-tuples-hook
|
2008-11-03 04:51:28 -05:00
|
|
|
SYMBOL: remake-generics-hook
|
2008-02-24 03:19:38 -05:00
|
|
|
|
2009-03-07 00:33:30 -05:00
|
|
|
: index>= ( obj1 obj2 seq -- ? )
|
|
|
|
[ index ] curry bi@ >= ;
|
|
|
|
|
2008-08-31 06:19:16 -04:00
|
|
|
: dependency>= ( how1 how2 -- ? )
|
2009-03-07 00:33:30 -05:00
|
|
|
{ called-dependency flushed-dependency inlined-dependency }
|
|
|
|
index>= ;
|
2008-08-31 06:19:16 -04:00
|
|
|
|
2008-08-30 03:31:27 -04:00
|
|
|
: strongest-dependency ( how1 how2 -- how )
|
2008-08-31 06:19:16 -04:00
|
|
|
[ called-dependency or ] bi@ [ dependency>= ] most ;
|
2008-08-30 03:31:27 -04:00
|
|
|
|
|
|
|
: weakest-dependency ( how1 how2 -- how )
|
2008-08-31 06:19:16 -04:00
|
|
|
[ inlined-dependency or ] bi@ [ dependency>= not ] most ;
|
2008-08-30 03:31:27 -04:00
|
|
|
|
|
|
|
: compiled-usage ( word -- assoc )
|
|
|
|
compiled-crossref get at ;
|
|
|
|
|
2008-08-31 02:34:00 -04:00
|
|
|
: (compiled-usages) ( word -- assoc )
|
2008-08-30 03:31:27 -04:00
|
|
|
#! If the word is not flushable anymore, we have to recompile
|
|
|
|
#! all words which flushable away a call (presumably when the
|
|
|
|
#! word was still flushable). If the word is flushable, we
|
|
|
|
#! don't have to recompile words that folded this away.
|
2008-08-31 02:34:00 -04:00
|
|
|
[ compiled-usage ]
|
|
|
|
[ "flushable" word-prop inlined-dependency flushed-dependency ? ] bi
|
2008-08-31 06:19:16 -04:00
|
|
|
[ dependency>= nip ] curry assoc-filter ;
|
2008-08-31 02:34:00 -04:00
|
|
|
|
|
|
|
: compiled-usages ( assoc -- assocs )
|
|
|
|
[ drop word? ] assoc-filter
|
|
|
|
[ [ drop (compiled-usages) ] { } assoc>map ] keep suffix ;
|
|
|
|
|
|
|
|
: compiled-generic-usage ( word -- assoc )
|
|
|
|
compiled-generic-crossref get at ;
|
|
|
|
|
|
|
|
: (compiled-generic-usages) ( generic class -- assoc )
|
2008-11-04 04:38:44 -05:00
|
|
|
[ compiled-generic-usage ] dip
|
|
|
|
[
|
2008-11-05 19:32:02 -05:00
|
|
|
2dup [ valid-class? ] both?
|
2008-11-04 04:38:44 -05:00
|
|
|
[ classes-intersect? ] [ 2drop f ] if nip
|
|
|
|
] curry assoc-filter ;
|
2008-08-31 02:34:00 -04:00
|
|
|
|
|
|
|
: compiled-generic-usages ( assoc -- assocs )
|
|
|
|
[ (compiled-generic-usages) ] { } assoc>map ;
|
|
|
|
|
|
|
|
: words-only ( assoc -- assoc' )
|
|
|
|
[ drop word? ] assoc-filter ;
|
|
|
|
|
|
|
|
: to-recompile ( -- seq )
|
|
|
|
changed-definitions get compiled-usages
|
|
|
|
changed-generics get compiled-generic-usages
|
|
|
|
append assoc-combine keys ;
|
2008-08-30 03:31:27 -04:00
|
|
|
|
2008-02-25 04:38:37 -05:00
|
|
|
: call-recompile-hook ( -- )
|
2008-08-31 02:34:00 -04:00
|
|
|
to-recompile recompile-hook get call ;
|
2008-02-25 04:38:37 -05:00
|
|
|
|
2008-11-03 04:51:28 -05:00
|
|
|
: call-remake-generics-hook ( -- )
|
|
|
|
remake-generics-hook get call ;
|
|
|
|
|
2008-03-31 02:19:34 -04:00
|
|
|
: call-update-tuples-hook ( -- )
|
|
|
|
update-tuples-hook get call ;
|
2008-02-25 04:38:37 -05:00
|
|
|
|
2008-06-11 03:58:38 -04:00
|
|
|
: unxref-forgotten-definitions ( -- )
|
|
|
|
forgotten-definitions get
|
|
|
|
keys [ word? ] filter
|
|
|
|
[ delete-compiled-xref ] each ;
|
|
|
|
|
2008-01-09 04:52:08 -05:00
|
|
|
: finish-compilation-unit ( -- )
|
2008-11-03 04:51:28 -05:00
|
|
|
call-remake-generics-hook
|
2008-02-25 04:38:37 -05:00
|
|
|
call-recompile-hook
|
2008-03-31 02:19:34 -04:00
|
|
|
call-update-tuples-hook
|
2008-06-11 03:58:38 -04:00
|
|
|
unxref-forgotten-definitions
|
2009-01-24 21:17:11 -05:00
|
|
|
modify-code-heap ;
|
2008-05-28 20:34:18 -04:00
|
|
|
|
|
|
|
: with-nested-compilation-unit ( quot -- )
|
|
|
|
[
|
|
|
|
H{ } clone changed-definitions set
|
2008-08-31 02:34:00 -04:00
|
|
|
H{ } clone changed-generics set
|
2008-11-03 04:51:28 -05:00
|
|
|
H{ } clone remake-generics set
|
2008-05-28 20:34:18 -04:00
|
|
|
H{ } clone outdated-tuples set
|
2008-08-30 03:32:17 -04:00
|
|
|
H{ } clone new-classes set
|
2008-05-28 20:34:18 -04:00
|
|
|
[ finish-compilation-unit ] [ ] cleanup
|
|
|
|
] with-scope ; inline
|
2008-01-09 04:52:08 -05:00
|
|
|
|
|
|
|
: with-compilation-unit ( quot -- )
|
|
|
|
[
|
2008-04-05 08:00:09 -04:00
|
|
|
H{ } clone changed-definitions set
|
2008-08-31 02:34:00 -04:00
|
|
|
H{ } clone changed-generics set
|
2008-11-03 04:51:28 -05:00
|
|
|
H{ } clone remake-generics set
|
2008-01-09 16:51:55 -05:00
|
|
|
H{ } clone forgotten-definitions set
|
2008-03-31 02:19:34 -04:00
|
|
|
H{ } clone outdated-tuples set
|
2008-08-30 03:32:17 -04:00
|
|
|
H{ } clone new-classes set
|
2008-01-09 04:52:08 -05:00
|
|
|
<definitions> new-definitions set
|
|
|
|
<definitions> old-definitions set
|
2008-05-28 20:34:18 -04:00
|
|
|
[
|
|
|
|
finish-compilation-unit
|
|
|
|
updated-definitions
|
|
|
|
notify-definition-observers
|
|
|
|
] [ ] cleanup
|
2008-01-09 04:52:08 -05:00
|
|
|
] with-scope ; inline
|
|
|
|
|
2008-02-24 03:19:38 -05:00
|
|
|
: default-recompile-hook ( words -- alist )
|
|
|
|
[ f ] { } map>assoc ;
|
2008-02-17 19:38:29 -05:00
|
|
|
|
2009-02-10 17:16:12 -05:00
|
|
|
recompile-hook [ [ default-recompile-hook ] ] initialize
|