1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs classes classes.builtin
4 classes.singleton classes.tuple combinators
5 combinators.short-circuit compiler.tree
6 compiler.tree.combinators compiler.tree.late-optimizations fry
7 kernel math.partial-dispatch memoize sequences
8 stack-checker.dependencies words ;
9 IN: compiler.tree.finalization
11 GENERIC: finalize* ( node -- nodes )
13 : finalize ( nodes -- nodes' ) [ finalize* ] map-nodes ;
15 : splice-final ( quot -- nodes ) splice-quot finalize ;
17 : splice-predicate ( word -- nodes )
18 [ +definition+ depends-on ] [ def>> splice-final ] bi ;
20 M: #copy finalize* drop f ;
24 [ [ in-d>> length ] [ out-d>> length ] bi = ]
25 [ [ in-r>> length ] [ out-r>> length ] bi = ]
26 [ [ in-d>> ] [ out-d>> ] [ mapping>> ] tri '[ _ at = ] 2all? ]
27 [ [ in-r>> ] [ out-r>> ] [ mapping>> ] tri '[ _ at = ] 2all? ]
28 } 1&& [ drop f ] when ;
30 MEMO: cached-expansion ( word -- nodes )
33 GENERIC: finalize-word ( #call word -- nodes )
35 M: predicate finalize-word
36 "predicating" word-prop {
37 { [ dup builtin-class? ] [ drop word>> cached-expansion ] }
38 { [ dup tuple-class? ] [ drop word>> splice-predicate ] }
39 { [ dup singleton-class? ] [ drop word>> splice-predicate ] }
43 M: math-partial finalize-word
44 dup primitive? [ drop ] [ nip cached-expansion ] if ;
46 M: word finalize-word drop ;
49 dup word>> finalize-word ;