factor/basis/compiler/compiler.factor

160 lines
4.1 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2009 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors kernel namespaces arrays sequences io words fry
continuations vocabs assocs dlists definitions math graphs generic
combinators deques search-deques macros io source-files.errors stack-checker
stack-checker.state stack-checker.inlining combinators.short-circuit
compiler.errors compiler.units compiler.tree.builder
compiler.tree.optimizer compiler.cfg.builder compiler.cfg.optimizer
compiler.cfg.linearization compiler.cfg.two-operand
compiler.cfg.linear-scan compiler.cfg.stack-frame compiler.codegen
compiler.utilities ;
2007-09-20 18:09:08 -04:00
IN: compiler
2008-10-07 17:17:55 -04:00
SYMBOL: compile-queue
SYMBOL: compiled
: queue-compile? ( word -- ? )
2008-10-07 17:17:55 -04:00
{
[ "forgotten" word-prop ]
[ compiled get key? ]
[ inlined-block? ]
[ primitive? ]
} 1|| not ;
: queue-compile ( word -- )
dup queue-compile? [ compile-queue get push-front ] [ drop ] if ;
2008-10-07 17:17:55 -04:00
: maybe-compile ( word -- )
dup optimized>> [ drop ] [ queue-compile ] if ;
2008-10-07 17:17:55 -04:00
SYMBOLS: +optimized+ +unoptimized+ ;
2008-06-11 03:58:38 -04:00
: ripple-up ( words -- )
dup "compiled-status" word-prop +unoptimized+ eq?
2008-06-11 03:58:38 -04:00
[ usage [ word? ] filter ] [ compiled-usage keys ] if
[ queue-compile ] each ;
2009-03-22 21:16:31 -04:00
: ripple-up? ( status word -- ? )
[
[ nip changed-effects get key? ]
[ "compiled-status" word-prop eq? not ] 2bi or
] keep "compiled-status" word-prop and ;
: save-compiled-status ( word status -- )
2009-03-22 21:16:31 -04:00
[ over ripple-up? [ ripple-up ] [ drop ] if ]
[ "compiled-status" set-word-prop ]
2008-06-11 03:58:38 -04:00
2bi ;
2008-10-07 17:17:55 -04:00
: start ( word -- )
2008-12-08 15:58:00 -05:00
"trace-compilation" get [ dup name>> print flush ] when
2008-10-07 17:17:55 -04:00
H{ } clone dependencies set
H{ } clone generic-dependencies set
2008-05-07 08:49:36 -04:00
f swap compiler-error ;
2008-01-01 14:54:14 -05:00
: ignore-error? ( word error -- ? )
[
{
[ inline? ]
[ macro? ]
[ "no-compile" word-prop ]
[ "special" word-prop ]
} 1||
] [ error-type +compiler-warning+ eq? ] bi* and ;
: (fail) ( word compiled -- * )
swap
2009-04-17 00:14:11 -04:00
[ compiled-unxref ]
[ compiled get set-at ]
2009-04-17 00:14:11 -04:00
[ +unoptimized+ save-compiled-status ]
tri
2008-10-07 17:17:55 -04:00
return ;
: not-compiled-def ( word error -- def )
'[ _ _ not-compiled ] [ ] like ;
2009-04-17 00:14:11 -04:00
: fail ( word error -- * )
2dup ignore-error?
[ drop f over def>> ]
[ 2dup not-compiled-def ] if
[ swap compiler-error ] [ (fail) ] bi-curry* bi ;
2009-04-17 00:14:11 -04:00
: frontend ( word -- nodes )
dup contains-breakpoints? [ dup def>> (fail) ] [
2009-04-17 00:14:11 -04:00
[ build-tree-from-word ] [ fail ] recover optimize-tree
] if ;
2008-01-01 14:54:14 -05:00
! Only switch this off for debugging.
SYMBOL: compile-dependencies?
t compile-dependencies? set-global
2008-10-07 17:17:55 -04:00
: save-asm ( asm -- )
[ [ code>> ] [ label>> ] bi compiled get set-at ]
[ compile-dependencies? get [ calls>> [ maybe-compile ] each ] [ drop ] if ]
2008-10-07 17:17:55 -04:00
bi ;
: backend ( nodes word -- )
build-cfg [
2008-10-22 19:39:41 -04:00
optimize-cfg
2008-10-07 17:17:55 -04:00
build-mr
2008-10-28 05:38:37 -04:00
convert-two-operand
2008-10-07 17:17:55 -04:00
linear-scan
build-stack-frame
generate
save-asm
] each ;
: finish ( word -- )
[ +optimized+ save-compiled-status ]
2008-10-19 02:10:21 -04:00
[ compiled-unxref ]
[
dup crossref?
[
dependencies get
generic-dependencies get
2008-10-19 02:10:21 -04:00
compiled-xref
] [ drop ] if
] tri ;
2008-01-01 14:54:14 -05:00
: (compile) ( word -- )
2008-08-15 00:35:19 -04:00
'[
2008-09-10 23:11:40 -04:00
_ {
2008-10-07 17:17:55 -04:00
[ start ]
[ frontend ]
[ backend ]
[ finish ]
2008-05-07 08:49:36 -04:00
} cleave
2008-08-15 00:35:19 -04:00
] with-return ;
2008-08-19 15:06:20 -04:00
: compile-loop ( deque -- )
[ (compile) yield-hook get call( -- ) ] slurp-deque ;
: decompile ( word -- )
dup def>> 2array 1array modify-code-heap ;
: compile-call ( quot -- )
[ dup infer define-temp ] with-compilation-unit execute ;
\ compile-call t "no-compile" set-word-prop
SINGLETON: optimizing-compiler
M: optimizing-compiler recompile ( words -- alist )
[
2008-06-11 03:58:38 -04:00
<hashed-dlist> compile-queue set
H{ } clone compiled set
2008-02-25 04:38:37 -05:00
[ queue-compile ] each
compile-queue get compile-loop
compiled get >alist
] with-scope ;
2007-09-20 18:09:08 -04:00
: enable-compiler ( -- )
optimizing-compiler compiler-impl set-global ;
: disable-compiler ( -- )
f compiler-impl set-global ;
: recompile-all ( -- )
all-words compile ;