2008-08-31 06:40:29 -04:00
|
|
|
! Copyright (C) 2008 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-10-20 21:40:15 -04:00
|
|
|
USING: kernel accessors sequences words memoize classes.builtin
|
2008-09-02 03:02:05 -04:00
|
|
|
compiler.tree
|
2008-09-12 19:08:38 -04:00
|
|
|
compiler.tree.combinators
|
2008-09-02 23:59:49 -04:00
|
|
|
compiler.tree.propagation.info
|
2008-09-12 19:08:38 -04:00
|
|
|
compiler.tree.late-optimizations ;
|
2008-08-31 06:40:29 -04:00
|
|
|
IN: compiler.tree.finalization
|
|
|
|
|
2008-09-12 19:08:38 -04:00
|
|
|
! This is a late-stage optimization.
|
|
|
|
! See the comment in compiler.tree.late-optimizations.
|
|
|
|
|
2008-09-02 23:59:49 -04:00
|
|
|
! This pass runs after propagation, so that it can expand
|
2008-10-20 21:40:15 -04:00
|
|
|
! built-in type predicates; these cannot
|
2008-09-02 23:59:49 -04:00
|
|
|
! be expanded before propagation since we need to see 'fixnum?'
|
|
|
|
! instead of 'tag 0 eq?' and so on, for semantic reasoning.
|
|
|
|
! We also delete empty stack shuffles and copies to facilitate
|
2008-09-12 19:08:38 -04:00
|
|
|
! tail call optimization in the code generator.
|
2008-09-02 23:59:49 -04:00
|
|
|
|
2008-08-31 06:40:29 -04:00
|
|
|
GENERIC: finalize* ( node -- nodes )
|
|
|
|
|
2008-10-08 04:51:44 -04:00
|
|
|
: finalize ( nodes -- nodes' ) [ finalize* ] map-nodes ;
|
|
|
|
|
|
|
|
: splice-final ( quot -- nodes ) splice-quot finalize ;
|
|
|
|
|
2008-08-31 06:40:29 -04:00
|
|
|
M: #copy finalize* drop f ;
|
|
|
|
|
|
|
|
M: #shuffle finalize*
|
|
|
|
dup shuffle-effect
|
|
|
|
[ in>> ] [ out>> ] bi sequence=
|
|
|
|
[ drop f ] when ;
|
|
|
|
|
2008-09-02 23:59:49 -04:00
|
|
|
: builtin-predicate? ( #call -- ? )
|
|
|
|
word>> "predicating" word-prop builtin-class? ;
|
|
|
|
|
|
|
|
MEMO: builtin-predicate-expansion ( word -- nodes )
|
2008-10-08 04:51:44 -04:00
|
|
|
def>> splice-final ;
|
2008-09-02 23:59:49 -04:00
|
|
|
|
|
|
|
: expand-builtin-predicate ( #call -- nodes )
|
|
|
|
word>> builtin-predicate-expansion ;
|
|
|
|
|
2008-09-02 03:02:05 -04:00
|
|
|
M: #call finalize*
|
2008-10-20 21:40:15 -04:00
|
|
|
dup builtin-predicate? [ expand-builtin-predicate ] when ;
|
2008-09-02 03:02:05 -04:00
|
|
|
|
2008-08-31 06:40:29 -04:00
|
|
|
M: node finalize* ;
|