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-22 22:16:46 -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-23 23:55:22 -05:00
|
|
|
USE: parser
|
2004-12-26 17:04:08 -05:00
|
|
|
USE: prettyprint
|
2004-11-28 19:07:24 -05:00
|
|
|
|
2004-12-25 15:52:08 -05:00
|
|
|
: with-dataflow ( param op [ intypes outtypes ] quot -- )
|
2004-11-28 19:07:24 -05:00
|
|
|
#! Take input parameters, execute quotation, take output
|
|
|
|
#! parameters, add node. The quotation is called with the
|
|
|
|
#! stack effect.
|
2004-12-24 17:29:16 -05:00
|
|
|
>r dup car ensure-d
|
2004-12-23 16:37:16 -05:00
|
|
|
>r dataflow, r> r> rot
|
2004-12-23 01:14:07 -05:00
|
|
|
[ pick car swap dataflow-inputs ] keep
|
2004-12-24 17:29:16 -05:00
|
|
|
pick 2slip cdr car swap
|
2004-12-23 01:14:07 -05:00
|
|
|
dataflow-outputs ; inline
|
2004-11-28 19:07:24 -05:00
|
|
|
|
2004-12-23 01:14:07 -05:00
|
|
|
: consume-d ( typelist -- )
|
|
|
|
[ pop-d 2drop ] each ;
|
2004-12-22 22:16:46 -05:00
|
|
|
|
2004-12-23 01:14:07 -05:00
|
|
|
: produce-d ( typelist -- )
|
|
|
|
[ <computed> push-d ] each ;
|
2004-12-22 22:16:46 -05:00
|
|
|
|
2004-12-30 02:40:14 -05:00
|
|
|
: (consume/produce) ( param op effect )
|
2004-12-24 17:29:16 -05:00
|
|
|
[ unswons consume-d car produce-d ] with-dataflow ;
|
2004-12-03 22:12:58 -05:00
|
|
|
|
2004-12-23 16:37:16 -05:00
|
|
|
: consume/produce ( word [ in-types out-types ] -- )
|
2004-11-28 19:07:24 -05:00
|
|
|
#! Add a node to the dataflow graph that consumes and
|
|
|
|
#! produces a number of values.
|
2004-12-03 22:12:58 -05:00
|
|
|
#call swap (consume/produce) ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2004-12-23 16:37:16 -05:00
|
|
|
: apply-effect ( word [ in-types out-types ] -- )
|
2004-11-26 22:23:57 -05:00
|
|
|
#! If a word does not have special inference behavior, we
|
|
|
|
#! either execute the word in the meta interpreter (if it is
|
|
|
|
#! side-effect-free and all parameters are literal), or
|
|
|
|
#! simply apply its stack effect to the meta-interpreter.
|
2004-11-27 23:09:32 -05:00
|
|
|
over "infer" word-property dup [
|
2004-12-24 17:29:16 -05:00
|
|
|
swap car ensure-d call drop
|
2004-11-26 22:23:57 -05:00
|
|
|
] [
|
2004-11-28 19:07:24 -05:00
|
|
|
drop consume/produce
|
2004-11-26 22:23:57 -05:00
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
: no-effect ( word -- )
|
|
|
|
"Unknown stack effect: " swap word-name cat2 throw ;
|
|
|
|
|
2004-12-03 22:12:58 -05:00
|
|
|
: with-block ( word label quot -- )
|
|
|
|
#! Execute a quotation with the word on the stack, and add
|
|
|
|
#! its dataflow contribution to a new block node in the IR.
|
2004-12-29 03:35:46 -05:00
|
|
|
over [
|
|
|
|
>r
|
|
|
|
dupd cons
|
|
|
|
recursive-state cons@
|
|
|
|
r> call
|
|
|
|
] (with-block) ;
|
2004-12-03 22:12:58 -05:00
|
|
|
|
2004-11-28 19:07:24 -05:00
|
|
|
: inline-compound ( word -- effect )
|
|
|
|
#! Infer the stack effect of a compound word in the current
|
|
|
|
#! inferencer instance.
|
2004-12-27 22:58:43 -05:00
|
|
|
gensym [ word-parameter infer-quot effect ] with-block ;
|
2004-11-28 19:07:24 -05:00
|
|
|
|
2004-12-27 15:27:18 -05:00
|
|
|
: infer-compound ( word -- effect )
|
2004-11-28 19:07:24 -05:00
|
|
|
#! Infer a word's stack effect in a separate inferencer
|
|
|
|
#! instance.
|
2004-11-26 22:23:57 -05:00
|
|
|
[
|
|
|
|
recursive-state get init-inference
|
2004-12-27 15:27:18 -05:00
|
|
|
dup dup inline-compound
|
2004-11-28 19:07:24 -05:00
|
|
|
[ "infer-effect" set-word-property ] keep
|
2004-12-27 15:27:18 -05:00
|
|
|
] with-scope consume/produce ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2004-12-24 02:52:02 -05:00
|
|
|
GENERIC: (apply-word)
|
|
|
|
|
|
|
|
M: compound (apply-word) ( word -- )
|
2004-11-26 22:23:57 -05:00
|
|
|
#! Infer a compound word's stack effect.
|
2004-11-27 00:33:17 -05:00
|
|
|
dup "inline" word-property [
|
2004-11-28 19:07:24 -05:00
|
|
|
inline-compound drop
|
2004-11-26 22:23:57 -05:00
|
|
|
] [
|
2004-11-28 19:07:24 -05:00
|
|
|
infer-compound
|
2004-11-26 22:23:57 -05:00
|
|
|
] ifte ;
|
|
|
|
|
2004-12-24 02:52:02 -05:00
|
|
|
M: symbol (apply-word) ( word -- )
|
|
|
|
apply-literal ;
|
|
|
|
|
2004-11-26 22:23:57 -05:00
|
|
|
: current-word ( -- word )
|
|
|
|
#! Push word we're currently inferring effect of.
|
|
|
|
recursive-state get car car ;
|
|
|
|
|
2004-12-30 02:40:14 -05:00
|
|
|
: check-recursion ( word -- )
|
2004-11-26 22:23:57 -05:00
|
|
|
#! If at the location of the recursive call, we're taking
|
|
|
|
#! more items from the stack than producing, we have a
|
2004-12-30 02:40:14 -05:00
|
|
|
#! diverging recursion. Note that this check is not done for
|
|
|
|
#! mutually-recursive words. Generally they should be
|
|
|
|
#! avoided.
|
|
|
|
recursive-state get car = [
|
|
|
|
d-in get vector-length
|
|
|
|
meta-d get vector-length > [
|
|
|
|
current-word word-name " diverges." cat2 throw
|
|
|
|
] when
|
2004-11-26 22:23:57 -05:00
|
|
|
] when ;
|
|
|
|
|
2004-12-30 20:46:20 -05:00
|
|
|
: decompose ( x y -- effect )
|
|
|
|
#! Return a stack effect such that x*effect = y.
|
|
|
|
2unlist >r
|
|
|
|
swap 2unlist >r
|
|
|
|
over length over length - head nip
|
|
|
|
r> append
|
|
|
|
r> 2list ;
|
|
|
|
|
2004-12-30 02:40:14 -05:00
|
|
|
: base-case ( word -- effect )
|
2004-12-30 20:46:20 -05:00
|
|
|
effect swap
|
2004-12-30 02:40:14 -05:00
|
|
|
[
|
|
|
|
inferring-base-case on
|
|
|
|
copy-inference
|
|
|
|
inline-compound
|
|
|
|
inferring-base-case off
|
2004-12-30 20:46:20 -05:00
|
|
|
] with-scope decompose ;
|
2004-12-30 02:40:14 -05:00
|
|
|
|
|
|
|
: recursive-word ( word label -- )
|
2004-11-26 22:23:57 -05:00
|
|
|
#! Handle a recursive call, by either applying a previously
|
2004-12-03 22:12:58 -05:00
|
|
|
#! inferred base case, or raising an error. If the recursive
|
|
|
|
#! call is to a local block, emit a label call node.
|
2004-12-30 02:40:14 -05:00
|
|
|
inferring-base-case get [
|
|
|
|
drop word-name " does not have a base case." cat2 throw
|
2004-11-26 22:23:57 -05:00
|
|
|
] [
|
2004-12-30 02:40:14 -05:00
|
|
|
2dup [ drop #call-label ] [ nip #call ] ifte
|
2004-12-30 20:46:20 -05:00
|
|
|
rot base-case (consume/produce)
|
2004-12-30 02:40:14 -05:00
|
|
|
] ifte ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
|
|
|
: apply-word ( word -- )
|
|
|
|
#! Apply the word's stack effect to the inferencer state.
|
2004-12-29 03:35:46 -05:00
|
|
|
dup recursive-state get assoc [
|
2004-12-30 02:40:14 -05:00
|
|
|
dup check-recursion recursive-word
|
2004-11-26 22:23:57 -05:00
|
|
|
] [
|
2004-12-29 03:35:46 -05:00
|
|
|
dup "infer-effect" word-property [
|
2004-12-23 18:26:04 -05:00
|
|
|
apply-effect
|
2004-11-26 22:23:57 -05:00
|
|
|
] [
|
2004-12-29 03:35:46 -05:00
|
|
|
(apply-word)
|
|
|
|
] ifte*
|
|
|
|
] ifte* ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2004-12-19 22:53:41 -05:00
|
|
|
: infer-call ( -- )
|
2004-12-23 01:14:07 -05:00
|
|
|
[ general-list ] ensure-d
|
2004-12-03 22:12:58 -05:00
|
|
|
dataflow-drop,
|
|
|
|
gensym dup [
|
2004-12-19 22:53:41 -05:00
|
|
|
drop pop-d dup
|
|
|
|
value-recursion recursive-state set
|
2004-12-22 22:16:46 -05:00
|
|
|
literal-value infer-quot
|
2004-12-03 22:12:58 -05:00
|
|
|
] with-block ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
|
|
|
\ call [ infer-call ] "infer" set-word-property
|
|
|
|
|
2004-12-23 23:55:22 -05:00
|
|
|
\ undefined-method t "terminator" set-word-property
|
|
|
|
\ not-a-number t "terminator" set-word-property
|
2004-12-27 22:58:43 -05:00
|
|
|
\ throw t "terminator" set-word-property
|