factor/library/compiler/inference/branches.factor

92 lines
2.7 KiB
Factor
Raw Normal View History

2006-07-27 18:21:49 -04:00
! Copyright (C) 2004, 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2004-11-26 22:23:57 -05:00
IN: inference
USING: arrays errors generic hashtables interpreter kernel math
namespaces parser prettyprint sequences strings vectors words ;
2004-12-31 02:17:45 -05:00
: unify-lengths ( seq -- seq )
2004-12-31 02:17:45 -05:00
#! Pad all vectors to the same length. If one vector is
#! shorter, pad it with unknown results at the bottom.
2006-08-15 04:57:12 -04:00
dup [ length ] map supremum
2006-07-27 18:21:49 -04:00
swap [ add-inputs nip ] map-with ;
2004-12-31 02:17:45 -05:00
: unify-values ( seq -- value )
2004-12-31 02:17:45 -05:00
#! If all values in list are equal, return the value.
2005-07-27 01:46:06 -04:00
#! Otherwise, unify.
dup all-eq? [ first ] [ drop <computed> ] if ;
2006-08-10 16:03:51 -04:00
: unify-stacks ( seq -- stack ) flip [ unify-values ] map ;
2005-07-26 16:39:14 -04:00
: balanced? ( in out -- ? )
2005-09-24 15:21:17 -04:00
[ dup [ length - ] [ 2drop f ] if ] 2map
2005-09-18 01:37:28 -04:00
[ ] subset all-equal? ;
2004-12-07 23:21:32 -05:00
: unbalanced-branches ( in out -- )
2006-07-27 18:21:49 -04:00
[ swap unparse " " rot length unparse append3 ] 2map
"Unbalanced branches:" add* "\n" join inference-error ;
2006-08-15 04:57:12 -04:00
: unify-inputs ( max-d-in d-in meta-d -- meta-d )
2006-08-10 16:03:51 -04:00
dup [
[ >r - r> length + ] keep add-inputs nip
] [
2nip
] if ;
2005-07-26 16:39:14 -04:00
: unify-effect ( in out -- in out )
2006-07-28 00:50:09 -04:00
#! in is a sequence of integers, out is a sequence of
#! stacks.
2dup balanced? [
2006-08-10 16:03:51 -04:00
over supremum -rot
[ >r dupd r> unify-inputs ] 2map
[ ] subset unify-stacks
] [
unbalanced-branches
2005-09-24 15:21:17 -04:00
] if ;
2004-12-07 23:21:32 -05:00
2005-09-28 23:29:00 -04:00
: active-variable ( seq symbol -- seq )
swap [
terminated? over hash [ 2drop f ] [ hash ] if
] map-with ;
2005-07-26 16:39:14 -04:00
: datastack-effect ( seq -- )
2006-07-27 18:21:49 -04:00
d-in over [ hash ] map-with
2005-09-28 23:29:00 -04:00
swap meta-d active-variable
unify-effect meta-d set d-in set ;
2005-07-26 16:39:14 -04:00
: callstack-effect ( seq -- )
2006-01-02 00:51:03 -05:00
dup length 0 <array>
swap meta-r active-variable
unify-effect meta-r set drop ;
2004-12-07 23:21:32 -05:00
2005-07-26 16:39:14 -04:00
: unify-effects ( seq -- )
dup datastack-effect dup callstack-effect
[ terminated? swap hash ] all? terminated? set ;
2004-11-26 22:23:57 -05:00
2005-05-17 16:13:08 -04:00
: unify-dataflow ( effects -- nodes )
2006-07-27 18:21:49 -04:00
[ dataflow-graph swap hash ] map ;
2005-05-17 16:13:08 -04:00
2004-12-26 17:04:08 -05:00
: copy-inference ( -- )
meta-r [ clone ] change
2005-07-27 01:46:06 -04:00
meta-d [ clone ] change
2005-09-18 01:37:28 -04:00
d-in [ ] change
2005-05-17 16:13:08 -04:00
dataflow-graph off
current-node off ;
2004-12-23 18:26:04 -05:00
2004-12-27 15:27:18 -05:00
: infer-branch ( value -- namespace )
[
2006-08-15 04:57:12 -04:00
copy-inference
dup value-recursion recursive-state set
value-literal infer-quot
terminated? get [ #values node, ] unless
] make-hash ;
2004-12-07 23:21:32 -05:00
: (infer-branches) ( branchlist -- list )
2006-08-15 04:57:12 -04:00
[ infer-branch ] map dup unify-effects unify-dataflow ;
2004-11-29 23:14:12 -05:00
2005-05-17 16:13:08 -04:00
: infer-branches ( branches node -- )
2004-11-26 22:23:57 -05:00
#! Recursive stack effect inference is done here. If one of
#! the branches has an undecidable stack effect, we set the
#! base case to this stack effect and try again.
2005-07-27 01:46:06 -04:00
[ >r (infer-branches) r> set-node-children ] keep
node, #merge node, ;