2005-01-10 23:08:27 -05:00
|
|
|
! Copyright (C) 2004, 2005 Slava Pestov.
|
2005-01-30 15:57:25 -05:00
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2004-11-26 22:23:57 -05:00
|
|
|
IN: inference
|
2005-05-09 02:34:15 -04:00
|
|
|
USING: errors generic interpreter kernel lists math
|
|
|
|
math-internals namespaces sequences strings vectors words
|
|
|
|
hashtables parser prettyprint ;
|
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 -- )
|
2005-07-27 01:46:06 -04:00
|
|
|
[ drop <computed> push-d ] each ;
|
2004-12-22 22:16:46 -05:00
|
|
|
|
2005-05-17 16:13:08 -04:00
|
|
|
: consume/produce ( word effect -- )
|
2004-11-28 19:07:24 -05:00
|
|
|
#! Add a node to the dataflow graph that consumes and
|
|
|
|
#! produces a number of values.
|
2005-05-17 16:13:08 -04:00
|
|
|
swap #call [
|
|
|
|
over [
|
2005-08-22 15:33:18 -04:00
|
|
|
2unseq swap consume-d produce-d
|
2005-05-17 16:13:08 -04:00
|
|
|
] hairy-node
|
|
|
|
] keep node, ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
|
|
|
: no-effect ( word -- )
|
2005-08-04 23:59:45 -04:00
|
|
|
"Stack effect inference of the word " swap word-name
|
|
|
|
" was already attempted, and failed" append3
|
2005-05-17 16:13:08 -04:00
|
|
|
inference-error ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2005-05-15 21:17:56 -04:00
|
|
|
: recursive? ( word -- ? )
|
|
|
|
f swap dup word-def [ = or ] tree-each-with ;
|
|
|
|
|
2005-05-17 16:13:08 -04:00
|
|
|
: with-block ( word [[ label quot ]] quot -- block-node )
|
2005-05-15 21:17:56 -04:00
|
|
|
#! Execute a quotation with the word on the stack, and add
|
2005-05-17 16:13:08 -04:00
|
|
|
#! its dataflow contribution to a new #label node in the IR.
|
|
|
|
>r 2dup cons recursive-state [ cons ] change r>
|
|
|
|
[ swap car #label slip ] with-nesting
|
|
|
|
recursive-state [ cdr ] change ; inline
|
|
|
|
|
|
|
|
: inline-block ( word -- node-block )
|
2005-08-12 18:02:03 -04:00
|
|
|
gensym over word-def cons [
|
|
|
|
#entry node, word-def infer-quot #return node,
|
|
|
|
] with-block ;
|
2004-11-28 19:07:24 -05:00
|
|
|
|
2005-05-15 21:17:56 -04:00
|
|
|
: inline-compound ( word -- )
|
|
|
|
#! Infer the stack effect of a compound word in the current
|
|
|
|
#! inferencer instance. If the word in question is recursive
|
|
|
|
#! we infer its stack effect inside a new block.
|
|
|
|
dup recursive? [
|
2005-05-17 16:13:08 -04:00
|
|
|
inline-block node,
|
2005-05-15 21:17:56 -04:00
|
|
|
] [
|
|
|
|
word-def infer-quot
|
|
|
|
] ifte ;
|
|
|
|
|
2005-07-31 23:38:33 -04:00
|
|
|
: infer-compound ( word base-case -- 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
|
|
|
[
|
2005-05-20 23:52:31 -04:00
|
|
|
inferring-base-case set
|
|
|
|
recursive-state get init-inference
|
|
|
|
dup inline-block drop
|
2005-07-27 20:13:11 -04:00
|
|
|
effect
|
2005-05-20 23:52:31 -04:00
|
|
|
] with-scope [ consume/produce ] keep ;
|
|
|
|
|
2005-07-31 23:38:33 -04:00
|
|
|
GENERIC: apply-word
|
2004-12-24 02:52:02 -05:00
|
|
|
|
2005-07-31 23:38:33 -04:00
|
|
|
M: object apply-word ( word -- )
|
2005-02-22 23:07:47 -05:00
|
|
|
#! A primitive with an unknown stack effect.
|
|
|
|
no-effect ;
|
|
|
|
|
2005-07-31 23:38:33 -04:00
|
|
|
M: compound apply-word ( word -- )
|
|
|
|
#! Infer a compound word's stack effect.
|
|
|
|
[
|
|
|
|
dup f infer-compound "infer-effect" set-word-prop
|
2005-06-15 23:27:28 -04:00
|
|
|
] [
|
2005-07-31 23:38:33 -04:00
|
|
|
[ swap t "no-effect" set-word-prop rethrow ] when*
|
|
|
|
] catch ;
|
2005-05-15 21:17:56 -04:00
|
|
|
|
2005-07-31 23:38:33 -04:00
|
|
|
: apply-default ( word -- )
|
2005-03-05 14:45:23 -05:00
|
|
|
dup "no-effect" word-prop [
|
2005-02-08 22:02:44 -05:00
|
|
|
no-effect
|
2004-11-26 22:23:57 -05:00
|
|
|
] [
|
2005-07-31 23:38:33 -04:00
|
|
|
dup "infer-effect" word-prop [
|
|
|
|
over "infer" word-prop [
|
|
|
|
swap car ensure-d call drop
|
|
|
|
] [
|
|
|
|
consume/produce
|
|
|
|
] ifte*
|
2005-02-24 20:52:17 -05:00
|
|
|
] [
|
2005-07-31 23:38:33 -04:00
|
|
|
apply-word
|
2005-02-24 20:52:17 -05:00
|
|
|
] ifte*
|
2005-07-31 23:38:33 -04:00
|
|
|
] ifte ;
|
2005-02-24 20:52:17 -05:00
|
|
|
|
2005-07-31 23:38:33 -04:00
|
|
|
M: word apply-object ( word -- )
|
2005-02-24 20:52:17 -05:00
|
|
|
apply-default ;
|
|
|
|
|
2005-07-31 23:38:33 -04:00
|
|
|
M: symbol apply-object ( word -- )
|
|
|
|
apply-literal ;
|
2005-02-24 20:52:17 -05:00
|
|
|
|
2005-05-20 23:52:31 -04:00
|
|
|
: (base-case) ( word label -- )
|
|
|
|
over "inline" word-prop [
|
2005-08-04 17:39:39 -04:00
|
|
|
meta-d get clone >r
|
2005-05-20 23:52:31 -04:00
|
|
|
over inline-block drop
|
2005-08-04 17:39:39 -04:00
|
|
|
[ #call-label ] [ #call ] ?ifte
|
|
|
|
r> over set-node-in-d node,
|
2005-05-20 23:52:31 -04:00
|
|
|
] [
|
2005-07-31 23:38:33 -04:00
|
|
|
drop dup t infer-compound "base-case" set-word-prop
|
2005-05-20 23:52:31 -04:00
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
: base-case ( word label -- )
|
2005-01-13 17:28:29 -05:00
|
|
|
[
|
2005-05-14 00:23:00 -04:00
|
|
|
inferring-base-case on
|
2005-05-20 23:52:31 -04:00
|
|
|
(base-case)
|
2005-01-13 17:28:29 -05:00
|
|
|
] [
|
2005-05-14 00:23:00 -04:00
|
|
|
inferring-base-case off
|
2005-01-13 17:28:29 -05:00
|
|
|
rethrow
|
|
|
|
] catch ;
|
|
|
|
|
2005-05-20 23:52:31 -04:00
|
|
|
: recursive-word ( word [[ label quot ]] -- )
|
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.
|
2005-05-15 21:17:56 -04:00
|
|
|
over "infer-effect" word-prop [
|
|
|
|
nip consume/produce
|
2004-11-26 22:23:57 -05:00
|
|
|
] [
|
2005-05-20 23:52:31 -04:00
|
|
|
over "base-case" word-prop [
|
|
|
|
nip consume/produce
|
2005-05-15 21:17:56 -04:00
|
|
|
] [
|
2005-05-20 23:52:31 -04:00
|
|
|
inferring-base-case get [
|
2005-05-23 00:25:52 -04:00
|
|
|
2drop terminate
|
2005-05-20 23:52:31 -04:00
|
|
|
] [
|
|
|
|
car base-case
|
|
|
|
] ifte
|
|
|
|
] ifte*
|
2005-05-15 21:17:56 -04:00
|
|
|
] ifte* ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2005-07-31 23:38:33 -04:00
|
|
|
M: compound apply-object ( word -- )
|
2004-11-26 22:23:57 -05:00
|
|
|
#! Apply the word's stack effect to the inferencer state.
|
2004-12-29 03:35:46 -05:00
|
|
|
dup recursive-state get assoc [
|
2005-01-31 14:02:09 -05:00
|
|
|
recursive-word
|
2004-11-26 22:23:57 -05:00
|
|
|
] [
|
2005-07-31 23:38:33 -04:00
|
|
|
dup "inline" word-prop [
|
|
|
|
inline-compound
|
|
|
|
] [
|
|
|
|
apply-default
|
|
|
|
] ifte
|
2004-12-29 03:35:46 -05:00
|
|
|
] ifte* ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2005-08-07 00:00:57 -04:00
|
|
|
: infer-shuffle ( word -- )
|
|
|
|
dup #call [
|
|
|
|
over "infer-effect" word-prop
|
2005-08-08 15:21:14 -04:00
|
|
|
[ meta-d [ swap with-datastack ] change ] hairy-node
|
2005-08-07 00:00:57 -04:00
|
|
|
] keep node, ;
|