2004-11-26 22:23:57 -05:00
|
|
|
! :folding=indent:collapseFolds=1:
|
|
|
|
|
|
|
|
! $Id$
|
|
|
|
!
|
|
|
|
! Copyright (C) 2004 Slava Pestov.
|
|
|
|
!
|
|
|
|
! Redistribution and use in source and binary forms, with or without
|
|
|
|
! modification, are permitted provided that the following conditions are met:
|
|
|
|
!
|
|
|
|
! 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
! this list of conditions and the following disclaimer.
|
|
|
|
!
|
|
|
|
! 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
! this list of conditions and the following disclaimer in the documentation
|
|
|
|
! and/or other materials provided with the distribution.
|
|
|
|
!
|
|
|
|
! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
IN: inference
|
|
|
|
USE: errors
|
2004-12-10 18:38:40 -05:00
|
|
|
USE: generic
|
2004-11-26 22:23:57 -05:00
|
|
|
USE: interpreter
|
|
|
|
USE: kernel
|
|
|
|
USE: lists
|
|
|
|
USE: math
|
|
|
|
USE: namespaces
|
|
|
|
USE: strings
|
|
|
|
USE: vectors
|
|
|
|
USE: words
|
|
|
|
USE: hashtables
|
|
|
|
|
2004-12-07 23:21:32 -05:00
|
|
|
: longest-vector ( list -- length )
|
2004-11-26 22:23:57 -05:00
|
|
|
[ vector-length ] map [ > ] top ;
|
|
|
|
|
2004-12-23 01:14:07 -05:00
|
|
|
: computed-value-vector ( n -- vector )
|
|
|
|
[ drop object <computed> ] vector-project ;
|
|
|
|
|
|
|
|
: add-inputs ( count stack -- count stack )
|
|
|
|
#! Add this many inputs to the given stack.
|
|
|
|
[ vector-length - dup ] keep
|
|
|
|
>r computed-value-vector dup r> vector-append ;
|
|
|
|
|
2004-12-22 22:16:46 -05:00
|
|
|
: unify-lengths ( list -- list )
|
|
|
|
#! Pad all vectors to the same length. If one vector is
|
|
|
|
#! shorter, pad it with unknown results at the bottom.
|
2004-12-23 01:14:07 -05:00
|
|
|
dup longest-vector swap [ dupd add-inputs nip ] map nip ;
|
2004-12-22 22:16:46 -05:00
|
|
|
|
2004-12-23 16:37:16 -05:00
|
|
|
: unify-classes ( value value -- value )
|
|
|
|
value-class swap value-class class-or <computed> ;
|
|
|
|
|
|
|
|
: unify-results ( value value -- value )
|
2004-11-26 22:23:57 -05:00
|
|
|
#! Replace values with unknown result if they differ,
|
|
|
|
#! otherwise retain them.
|
2004-12-23 16:37:16 -05:00
|
|
|
2dup = [ drop ] [ unify-classes ] ifte ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
|
|
|
: unify-stacks ( list -- stack )
|
|
|
|
#! Replace differing literals in stacks with unknown
|
|
|
|
#! results.
|
2004-12-22 22:16:46 -05:00
|
|
|
uncons [ [ unify-results ] vector-2map ] each ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2004-12-22 22:16:46 -05:00
|
|
|
: unify-d-in ( list -- d-in )
|
|
|
|
[ [ d-in get ] bind ] map unify-lengths unify-stacks ;
|
|
|
|
|
|
|
|
: filter-terminators ( list -- list )
|
|
|
|
[ [ d-in get meta-d get and ] bind ] subset ;
|
|
|
|
|
|
|
|
: balanced? ( list -- ? )
|
|
|
|
[
|
|
|
|
[
|
|
|
|
d-in get vector-length
|
|
|
|
meta-d get vector-length -
|
|
|
|
] bind
|
|
|
|
] map all=? ;
|
2004-12-07 23:21:32 -05:00
|
|
|
|
|
|
|
: unify-datastacks ( list -- datastack )
|
2004-12-22 22:16:46 -05:00
|
|
|
[ [ meta-d get ] bind ] map
|
2004-12-07 23:21:32 -05:00
|
|
|
unify-lengths unify-stacks ;
|
|
|
|
|
|
|
|
: check-lengths ( list -- )
|
|
|
|
[ vector-length ] map all=? [
|
|
|
|
"Unbalanced return stack effect" throw
|
|
|
|
] unless ;
|
|
|
|
|
|
|
|
: unify-callstacks ( list -- datastack )
|
2004-12-22 22:16:46 -05:00
|
|
|
[ [ meta-r get ] bind ] map
|
2004-12-07 23:21:32 -05:00
|
|
|
dup check-lengths unify-stacks ;
|
|
|
|
|
2004-11-26 22:23:57 -05:00
|
|
|
: unify ( list -- )
|
2004-12-22 22:16:46 -05:00
|
|
|
filter-terminators dup balanced? [
|
2004-12-07 23:21:32 -05:00
|
|
|
dup unify-d-in d-in set
|
|
|
|
dup unify-datastacks meta-d set
|
|
|
|
unify-callstacks meta-r set
|
2004-11-26 22:23:57 -05:00
|
|
|
] [
|
|
|
|
"Unbalanced branches" throw
|
|
|
|
] ifte ;
|
|
|
|
|
2004-12-19 22:53:41 -05:00
|
|
|
: infer-branch ( value save-effect -- namespace )
|
2004-12-07 23:21:32 -05:00
|
|
|
<namespace> [
|
2004-12-08 18:39:36 -05:00
|
|
|
save-effect set
|
2004-12-19 22:53:41 -05:00
|
|
|
dup value-recursion recursive-state set
|
2004-12-07 23:21:32 -05:00
|
|
|
copy-interpreter
|
2004-12-23 16:58:33 -05:00
|
|
|
d-in [ [ vector-clone ] vector-map ] change
|
2004-12-07 23:21:32 -05:00
|
|
|
dataflow-graph off
|
2004-12-22 22:16:46 -05:00
|
|
|
literal-value infer-quot
|
2004-12-08 18:39:36 -05:00
|
|
|
#values values-node
|
2004-12-07 23:21:32 -05:00
|
|
|
] extend ;
|
|
|
|
|
2004-11-30 23:56:01 -05:00
|
|
|
: terminator? ( quot -- ? )
|
2004-12-10 18:38:40 -05:00
|
|
|
#! This is a hack. undefined-method has a stack effect that
|
2004-11-30 23:56:01 -05:00
|
|
|
#! probably does not match any other branch of the generic,
|
|
|
|
#! so we handle it specially.
|
2004-12-22 22:16:46 -05:00
|
|
|
literal-value \ undefined-method swap tree-contains? ;
|
2004-11-30 23:56:01 -05:00
|
|
|
|
2004-12-19 22:53:41 -05:00
|
|
|
: recursive-branch ( value -- )
|
2004-11-27 00:33:17 -05:00
|
|
|
#! Set base case if inference didn't fail.
|
2004-11-26 22:23:57 -05:00
|
|
|
[
|
2004-12-08 18:39:36 -05:00
|
|
|
f infer-branch [
|
2004-12-23 16:37:16 -05:00
|
|
|
effect old-effect recursive-state get set-base
|
2004-12-13 16:28:28 -05:00
|
|
|
] bind
|
2004-11-26 22:23:57 -05:00
|
|
|
] [
|
2004-12-19 22:53:41 -05:00
|
|
|
[ drop ] when
|
2004-11-26 22:23:57 -05:00
|
|
|
] catch ;
|
|
|
|
|
2004-12-08 18:39:36 -05:00
|
|
|
: infer-base-case ( branchlist -- )
|
2004-11-30 23:56:01 -05:00
|
|
|
[
|
2004-12-19 22:53:41 -05:00
|
|
|
dup terminator? [
|
|
|
|
drop
|
2004-12-08 18:39:36 -05:00
|
|
|
] [
|
|
|
|
recursive-branch
|
|
|
|
] ifte
|
|
|
|
] each ;
|
|
|
|
|
|
|
|
: (infer-branches) ( branchlist -- list )
|
|
|
|
dup infer-base-case [
|
2004-12-19 22:53:41 -05:00
|
|
|
dup terminator? [
|
2004-12-08 18:39:36 -05:00
|
|
|
t infer-branch [
|
2004-12-07 23:21:32 -05:00
|
|
|
meta-d off meta-r off d-in off
|
|
|
|
] extend
|
2004-11-30 23:56:01 -05:00
|
|
|
] [
|
2004-12-08 18:39:36 -05:00
|
|
|
t infer-branch
|
2004-11-30 23:56:01 -05:00
|
|
|
] ifte
|
2004-12-07 23:21:32 -05:00
|
|
|
] map ;
|
2004-11-29 23:14:12 -05:00
|
|
|
|
|
|
|
: infer-branches ( inputs instruction branchlist -- )
|
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
|
2004-11-29 23:14:12 -05:00
|
|
|
#! base case to this stack effect and try again. The inputs
|
|
|
|
#! parameter is a vector.
|
2004-12-07 23:21:32 -05:00
|
|
|
(infer-branches) [
|
|
|
|
[ [ get-dataflow ] bind ] map
|
|
|
|
swap dataflow, [ node-consume-d set ] bind
|
|
|
|
] keep unify ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
|
|
|
: infer-ifte ( -- )
|
|
|
|
#! Infer effects for both branches, unify.
|
2004-12-23 01:14:07 -05:00
|
|
|
[ object general-list general-list ] ensure-d
|
2004-11-28 21:56:58 -05:00
|
|
|
dataflow-drop, pop-d
|
2004-12-04 23:45:41 -05:00
|
|
|
dataflow-drop, pop-d swap 2list
|
2004-12-01 19:48:08 -05:00
|
|
|
>r 1 meta-d get vector-tail* #ifte r>
|
2004-11-26 22:23:57 -05:00
|
|
|
pop-d drop ( condition )
|
|
|
|
infer-branches ;
|
|
|
|
|
2004-12-13 16:28:28 -05:00
|
|
|
\ ifte [ infer-ifte ] "infer" set-word-property
|
|
|
|
|
2004-12-19 22:53:41 -05:00
|
|
|
: vtable>list ( value -- list )
|
2004-12-22 22:16:46 -05:00
|
|
|
dup value-recursion swap literal-value vector>list
|
|
|
|
[ over <literal> ] map nip ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2004-12-13 16:28:28 -05:00
|
|
|
: infer-dispatch ( -- )
|
2004-11-26 22:23:57 -05:00
|
|
|
#! Infer effects for all branches, unify.
|
2004-12-23 01:14:07 -05:00
|
|
|
[ object vector ] ensure-d
|
2004-11-28 21:56:58 -05:00
|
|
|
dataflow-drop, pop-d vtable>list
|
2004-12-13 16:28:28 -05:00
|
|
|
>r 1 meta-d get vector-tail* #dispatch r>
|
|
|
|
pop-d drop ( n )
|
2004-11-26 22:23:57 -05:00
|
|
|
infer-branches ;
|
|
|
|
|
2004-12-13 16:28:28 -05:00
|
|
|
\ dispatch [ infer-dispatch ] "infer" set-word-property
|
|
|
|
\ dispatch [ 2 | 0 ] "infer-effect" set-word-property
|