factor/library/compiler/inference/branches.factor

121 lines
3.7 KiB
Factor
Raw Normal View History

2005-01-13 14:41:08 -05:00
! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/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
2006-07-19 23:10:02 -04:00
: add-inputs ( n stack -- stack )
tuck length - dup 0 >
[ value-vector dup rot nappend ] [ drop ] if ;
: 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.
dup 0 [ length max ] reduce swap [ add-inputs ] 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 ;
2005-07-26 16:39:14 -04:00
: unify-stacks ( seq -- stack )
2004-11-26 22:23:57 -05:00
#! Replace differing literals in stacks with unknown
#! results.
2005-09-18 01:37:28 -04:00
[ ] subset dup empty?
2005-09-24 15:21:17 -04:00
[ drop f ] [ unify-lengths flip [ unify-values ] map ] if ;
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
: unify-in-d ( seq -- n )
#! Input is a sequence of positive integers or f.
#! Output is the maximum or 0.
0 [ [ max ] when* ] reduce ;
: unbalanced-branches ( in out -- )
{ "Unbalanced branches:" } -rot [
2006-04-17 17:17:34 -04:00
swap unparse " " rot length unparse append3
] 2map append "\n" join inference-error ;
2005-07-26 16:39:14 -04:00
: unify-effect ( in out -- in out )
2005-09-18 01:37:28 -04:00
#! In is a sequence of integers; out is a sequence of stacks.
2dup balanced? [
unify-stacks >r unify-in-d r>
] [
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 -- )
2005-09-28 23:29:00 -04:00
dup d-in active-variable
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 )
[ [ dataflow-graph get ] bind ] map ;
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
2006-07-26 15:03:49 -04:00
: no-base-case ( -- )
"Cannot infer base case" inference-error ;
: recursive-branch ( hash ? -- obj )
#! If the branch made an unresolved recursive call, and we
#! are inferring the base case, ignore the branch (the base
#! case being the stack effect of the branches not making
#! recursive calls). Otherwise, raise an error.
[
base-case-continuation get
[ drop f ] [ no-base-case ] if
] when ;
2004-12-27 15:27:18 -05:00
: infer-branch ( value -- namespace )
#! Return a namespace with inferencer variables:
2006-05-15 01:37:11 -04:00
#! meta-d, meta-c, d-in. They are set to f if
#! terminate was called.
[
[
base-case-continuation set
copy-inference
dup value-recursion recursive-state set
dup value-literal infer-quot
2005-09-28 20:09:10 -04:00
terminated? get [ #values node, ] unless
f
2006-07-26 00:38:00 -04:00
] callcc1 nip
2006-07-26 15:03:49 -04:00
] make-hash swap recursive-branch ;
2006-07-26 00:38:00 -04:00
: notify-base-case ( -- )
base-case-continuation get
[ t swap continue-with ] [ no-base-case ] if* ;
2004-12-07 23:21:32 -05:00
: (infer-branches) ( branchlist -- list )
2006-07-26 00:38:00 -04:00
[ infer-branch ] map [ ] subset
dup empty? [ notify-base-case ] when
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, ;