factor/core/compiler/units/units.factor

174 lines
4.8 KiB
Factor
Raw Normal View History

! Copyright (C) 2008, 2009 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
math math.order classes classes.algebra classes.tuple
classes.tuple.private generic source-files.errors
kernel.private ;
IN: compiler.units
SYMBOL: old-definitions
SYMBOL: new-definitions
TUPLE: redefine-error def ;
: redefine-error ( definition -- )
\ redefine-error boa
{ { "Continue" t } } throw-restarts drop ;
<PRIVATE
: add-once ( key assoc -- )
2008-05-25 20:44:37 -04:00
2dup key? [ over redefine-error ] when conjoin ;
: (remember-definition) ( definition loc assoc -- )
2008-11-02 04:10:27 -05:00
[ over set-where ] dip add-once ;
PRIVATE>
: remember-definition ( definition loc -- )
new-definitions get first (remember-definition) ;
: fake-definition ( definition -- )
old-definitions get [ delete-at ] with each ;
: remember-class ( class loc -- )
2008-11-02 04:10:27 -05:00
[ dup new-definitions get first key? [ dup redefine-error ] when ] dip
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: compiler-impl
HOOK: recompile compiler-impl ( words -- alist )
HOOK: to-recompile compiler-impl ( -- words )
HOOK: process-forgotten-words compiler-impl ( words -- )
: compile ( words -- ) recompile modify-code-heap ;
! Non-optimizing compiler
M: f recompile
[ dup def>> ] { } map>assoc ;
M: f to-recompile
changed-definitions get [ drop word? ] assoc-filter
changed-generics get assoc-union keys ;
M: f process-forgotten-words drop ;
: without-optimizer ( quot -- )
[ f compiler-impl ] dip with-variable ; inline
: <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-startup-hook
2008-02-25 20:37:43 -05:00
2009-05-01 09:21:31 -04:00
! This goes here because vocabs cannot depend on init
[ V{ } clone vocab-observers set-global ]
"vocabs" add-startup-hook
2009-05-01 09:21:31 -04:00
: add-definition-observer ( obj -- )
2008-02-25 20:37:43 -05:00
definition-observers get push ;
: remove-definition-observer ( obj -- )
2009-10-28 01:23:08 -04:00
definition-observers get remove-eq! drop ;
: notify-definition-observers ( assoc -- )
definition-observers get
2008-01-09 17:36:30 -05:00
[ definitions-changed ] with each ;
! Incremented each time stack effects potentially changed, used
! by compiler.tree.propagation.call-effect for call( and execute(
! inline caching
: effect-counter ( -- n ) 47 special-object ; inline
GENERIC: bump-effect-counter* ( defspec -- ? )
M: object bump-effect-counter* drop f ;
<PRIVATE
: changed-vocabs ( assoc -- vocabs )
[ drop word? ] assoc-filter
[ drop vocabulary>> dup [ vocab ] when dup ] assoc-map ;
2008-04-05 08:00:09 -04:00
: updated-definitions ( -- assoc )
H{ } clone
dup forgotten-definitions get update
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
dup dup changed-vocabs update ;
: process-forgotten-definitions ( -- )
forgotten-definitions get keys
[ [ word? ] filter process-forgotten-words ]
[ [ delete-definition-errors ] each ]
bi ;
2008-06-11 03:58:38 -04:00
: bump-effect-counter? ( -- ? )
changed-effects get new-words get assoc-diff assoc-empty? not
changed-definitions get [ drop bump-effect-counter* ] assoc-any?
or ;
: bump-effect-counter ( -- )
bump-effect-counter? [
47 special-object 0 or
1 +
47 set-special-object
] when ;
: notify-observers ( -- )
updated-definitions dup assoc-empty?
[ drop ] [ notify-definition-observers notify-error-observers ] if ;
: finish-compilation-unit ( -- )
[ ] [
remake-generics
to-recompile recompile
update-tuples
process-forgotten-definitions
modify-code-heap
bump-effect-counter
notify-observers
] if-bootstrapping ;
PRIVATE>
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
2009-03-22 21:16:31 -04:00
H{ } clone changed-effects set
H{ } clone outdated-generics set
2008-05-28 20:34:18 -04:00
H{ } clone outdated-tuples set
H{ } clone new-words 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
: 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
2009-03-22 21:16:31 -04:00
H{ } clone changed-effects set
H{ } clone outdated-generics set
H{ } clone forgotten-definitions set
H{ } clone outdated-tuples set
H{ } clone new-words set
2008-08-30 03:32:17 -04:00
H{ } clone new-classes set
<definitions> new-definitions set
<definitions> old-definitions set
[ finish-compilation-unit ] [ ] cleanup
] with-scope ; inline