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
|
|
|
|
2005-09-14 00:37:50 -04:00
|
|
|
: consume-values ( n node -- )
|
|
|
|
over ensure-values
|
|
|
|
over 0 rot node-inputs [ pop-d 2drop ] each ;
|
2004-12-22 22:16:46 -05:00
|
|
|
|
2005-09-14 00:37:50 -04:00
|
|
|
: produce-values ( n node -- )
|
2005-09-17 15:25:18 -04:00
|
|
|
over [ drop <value> push-d ] each 0 swap node-outputs ;
|
2005-09-09 00:17:19 -04: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-09-14 00:37:50 -04:00
|
|
|
swap #call
|
|
|
|
over first length over consume-values
|
|
|
|
swap second length over produce-values
|
|
|
|
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-09-09 23:40:08 -04:00
|
|
|
: with-recursive-state ( word label quot -- )
|
|
|
|
>r over word-def cons cons
|
|
|
|
recursive-state [ cons ] change r>
|
|
|
|
call
|
2005-05-17 16:13:08 -04:00
|
|
|
recursive-state [ cdr ] change ; inline
|
|
|
|
|
|
|
|
: inline-block ( word -- node-block )
|
2005-09-09 23:40:08 -04:00
|
|
|
gensym 2dup [
|
|
|
|
[
|
|
|
|
dup #label >r
|
|
|
|
#entry node,
|
|
|
|
swap word-def infer-quot
|
|
|
|
#return node, r>
|
|
|
|
] with-nesting
|
|
|
|
] with-recursive-state ;
|
2004-11-28 19:07:24 -05:00
|
|
|
|
2005-09-17 22:25:18 -04:00
|
|
|
: infer-compound ( word base-case -- terminates? effect )
|
2004-11-28 19:07:24 -05:00
|
|
|
#! Infer a word's stack effect in a separate inferencer
|
2005-09-17 22:25:18 -04:00
|
|
|
#! instance. Outputs a boolean if the word terminates
|
|
|
|
#! control flow by throwing an exception or restoring a
|
|
|
|
#! continuation.
|
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
|
2005-09-17 22:25:18 -04:00
|
|
|
[ inline-block drop active? not effect ] keep
|
|
|
|
] with-scope over consume/produce over [ terminate ] when ;
|
2005-05-20 23:52:31 -04:00
|
|
|
|
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.
|
|
|
|
[
|
2005-09-17 22:25:18 -04:00
|
|
|
dup dup f infer-compound
|
|
|
|
>r "terminates" set-word-prop r>
|
|
|
|
"infer-effect" set-word-prop
|
2005-09-21 01:12:16 -04:00
|
|
|
] [
|
2005-09-20 20:18:01 -04:00
|
|
|
swap t "no-effect" set-word-prop rethrow
|
2005-09-21 01:12:16 -04:00
|
|
|
] recover ;
|
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 [
|
2005-09-14 00:37:50 -04:00
|
|
|
swap first length ensure-values call drop
|
2005-07-31 23:38:33 -04:00
|
|
|
] [
|
2005-09-17 22:25:18 -04:00
|
|
|
dupd consume/produce
|
|
|
|
"terminates" word-prop [ terminate ] when
|
2005-09-24 15:21:17 -04:00
|
|
|
] if*
|
2005-02-24 20:52:17 -05:00
|
|
|
] [
|
2005-07-31 23:38:33 -04:00
|
|
|
apply-word
|
2005-09-24 15:21:17 -04:00
|
|
|
] if*
|
|
|
|
] if ;
|
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-09-24 15:21:17 -04:00
|
|
|
[ #call-label ] [ #call ] ?if
|
2005-08-04 17:39:39 -04:00
|
|
|
r> over set-node-in-d node,
|
2005-05-20 23:52:31 -04:00
|
|
|
] [
|
2005-09-17 22:25:18 -04:00
|
|
|
drop dup t infer-compound nip "base-case" set-word-prop
|
2005-09-24 15:21:17 -04:00
|
|
|
] if ;
|
2005-05-20 23:52:31 -04:00
|
|
|
|
|
|
|
: base-case ( word label -- )
|
2005-09-20 20:18:01 -04:00
|
|
|
[ inferring-base-case on (base-case) ]
|
|
|
|
[ inferring-base-case off ] cleanup ;
|
2005-01-13 17:28:29 -05:00
|
|
|
|
2005-09-14 00:37:50 -04:00
|
|
|
: no-base-case ( word -- )
|
|
|
|
{
|
|
|
|
"The base case of a recursive word could not be inferred.\n"
|
|
|
|
"This means the word calls itself in every control flow path.\n"
|
|
|
|
"See the handbook for details."
|
|
|
|
} concat inference-error ;
|
|
|
|
|
|
|
|
: notify-base-case ( -- )
|
|
|
|
base-case-continuation get
|
2005-09-24 15:21:17 -04:00
|
|
|
[ t swap continue-with ] [ no-base-case ] if* ;
|
2005-09-14 00:37:50 -04:00
|
|
|
|
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-09-14 00:37:50 -04:00
|
|
|
notify-base-case
|
2005-05-20 23:52:31 -04:00
|
|
|
] [
|
|
|
|
car base-case
|
2005-09-24 15:21:17 -04:00
|
|
|
] if
|
|
|
|
] if*
|
|
|
|
] if* ;
|
2004-11-26 22:23:57 -05:00
|
|
|
|
2005-09-04 01:09:46 -04:00
|
|
|
: splice-node ( node -- )
|
|
|
|
dup node-successor [
|
|
|
|
dup node, penultimate-node f over set-node-successor
|
|
|
|
dup current-node set
|
|
|
|
] when drop ;
|
|
|
|
|
|
|
|
: block, ( block -- )
|
|
|
|
#! If the block does not call itself, there is no point in
|
|
|
|
#! having the block node in the IR. Just add its contents.
|
|
|
|
dup recursive-label? [
|
|
|
|
node,
|
|
|
|
] [
|
2005-09-04 19:24:24 -04:00
|
|
|
node-child node-successor splice-node
|
2005-09-24 15:21:17 -04:00
|
|
|
] if ;
|
2005-09-04 01:09:46 -04: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-08-29 21:00:39 -04:00
|
|
|
dup "inline" word-prop
|
2005-09-24 15:21:17 -04:00
|
|
|
[ inline-block block, ] [ apply-default ] if
|
|
|
|
] if* ;
|