factor/library/compiler/inference/words.factor

178 lines
5.2 KiB
Factor
Raw Normal View History

2006-05-15 01:49:07 -04:00
! Copyright (C) 2004, 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: arrays errors generic hashtables interpreter kernel
2005-12-16 22:24:39 -05:00
math math-internals namespaces parser prettyprint sequences
strings vectors words ;
IN: inference
2005-09-14 00:37:50 -04:00
: consume-values ( n node -- )
over ensure-values
2006-07-19 23:10:02 -04:00
over 0 rot node-inputs
meta-d get [ length swap - ] keep set-length ;
2005-09-14 00:37:50 -04:00
: produce-values ( n node -- )
2006-07-19 23:10:02 -04:00
>r [ drop <computed> ] map dup r> set-node-out-d
meta-d get swap nappend ;
2005-09-09 00:17:19 -04:00
2006-08-15 04:57:12 -04:00
: recursing? ( word -- label/f )
recursive-state get <reversed> assoc ;
: make-call-node ( word -- node )
dup "inline" word-prop
[ dup recursing? [ #call-label ] [ #call ] ?if ]
[ #call ]
if ;
2005-05-17 16:13:08 -04:00
: consume/produce ( word effect -- )
#! Add a node to the dataflow graph that consumes and
#! produces a number of values.
2006-08-15 04:57:12 -04:00
meta-d get clone >r
swap make-call-node
over effect-in length over consume-values
over effect-out length over produce-values
2006-08-15 04:57:12 -04:00
r> over #call-label? [ over set-node-in-d ] [ drop ] if
node, effect-terminated? [ terminate ] when ;
2004-11-26 22:23:57 -05:00
2006-08-15 05:24:30 -04:00
: no-effect ( word -- * )
"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
2006-08-10 16:33:15 -04:00
: nest-node ( -- ) #entry node, ;
2005-12-16 22:24:39 -05:00
2006-08-10 16:33:15 -04:00
: unnest-node ( new-node -- new-node )
dup node-param #return node,
dataflow-graph get 1array over set-node-children ;
2005-12-16 22:24:39 -05:00
2006-08-15 04:57:12 -04:00
: add-recursive-state ( word label -- )
2array recursive-state [ swap add ] change ;
2006-08-15 04:57:12 -04:00
: inline-block ( word -- node-block variables )
[
2006-08-10 16:33:15 -04:00
copy-inference nest-node
2006-08-15 04:57:12 -04:00
gensym 2dup add-recursive-state
2006-08-10 16:33:15 -04:00
#label >r word-def infer-quot r>
unnest-node
] make-hash ;
: apply-infer ( hash -- )
{ meta-d meta-r d-in }
[ [ swap hash ] keep set ] each-with ;
GENERIC: collect-recursion* ( label node -- )
M: node collect-recursion* 2drop ;
M: #call-label collect-recursion*
2006-08-10 16:33:15 -04:00
tuck node-param eq? [ node-in-d , ] [ drop ] if ;
: collect-recursion ( #label -- seq )
#! Collect the input stacks of all #call-label nodes that
#! call given label.
dup node-param swap
[ [ collect-recursion* ] each-node-with ] { } make ;
: join-values ( node -- )
#! We have to infer recursive labels twice to determine
#! which literals survive the recursion (eg, quotations)
#! and which don't (loop indices, etc). The latter cannot
#! be folded.
2006-08-10 16:03:51 -04:00
collect-recursion meta-d get add unify-lengths unify-stacks
2006-08-15 04:57:12 -04:00
meta-d [ length tail* >vector ] change ;
: splice-node ( node -- )
#! Labels which do not call themselves are just spliced into
#! the IR, and no #label node is added.
dup node-successor [
dup node, penultimate-node f over set-node-successor
dup current-node set
] when drop ;
: inline-closure ( word -- )
#! This is not a closure in the lexical scope sense, but a
#! closure under recursive value substitution.
#! If the block does not call itself, there is no point in
#! having the block node in the IR. Just add its contents.
2006-08-15 04:57:12 -04:00
dup inline-block over recursive-label? [
meta-d get >r
2006-08-15 04:57:12 -04:00
drop join-values inline-block apply-infer
2006-03-04 02:53:22 -05:00
r> over set-node-in-d node,
] [
apply-infer node-child node-successor splice-node drop
] if ;
2006-08-15 04:57:12 -04:00
: infer-compound ( word -- effect )
2004-11-26 22:23:57 -05:00
[
2006-01-19 23:28:45 -05:00
recursive-state get init-inference
2006-08-15 04:57:12 -04:00
[ inline-block nip [ current-effect ] bind ] keep
] with-scope over consume/produce ;
2005-07-31 23:38:33 -04:00
GENERIC: apply-word
2004-12-24 02:52:02 -05:00
M: object apply-word
#! A primitive with an unknown stack effect.
no-effect ;
TUPLE: effect-error word effect ;
2006-01-19 23:28:45 -05:00
2006-08-15 04:57:12 -04:00
: effect-error ( word effect -- * ) <effect-error> throw ;
: check-effect ( word effect -- )
over recorded get push
dup pick "declared-effect" word-prop dup
[ effect<= [ effect-error ] unless ] [ 2drop ] if
"infer-effect" set-word-prop ;
M: compound apply-word
2005-07-31 23:38:33 -04:00
#! Infer a compound word's stack effect.
[
2006-08-15 04:57:12 -04:00
dup infer-compound check-effect
2005-09-21 01:12:16 -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 -- )
2006-07-25 00:14:18 -04:00
dup "no-effect" word-prop [ no-effect ] when
dup "infer-effect" word-prop [
over "infer" word-prop [
swap effect-in length ensure-values call drop
] [
2006-07-26 15:03:49 -04:00
consume/produce
2005-09-24 15:21:17 -04:00
] if*
2006-07-25 00:14:18 -04:00
] [
apply-word
] if* ;
M: word apply-object apply-default ;
M: symbol apply-object apply-literal ;
: declared-effect ( word -- effect )
dup "declared-effect" word-prop [ ] [
"The recursive word " swap word-name
" does not declare a stack effect" append3
inference-error
] ?if ;
: recursive-effect ( word -- effect )
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.
2006-08-18 01:35:04 -04:00
dup "infer-effect" word-prop [ ] [ declared-effect ] ?if ;
2004-11-26 22:23:57 -05:00
M: compound apply-object
2004-11-26 22:23:57 -05:00
#! Apply the word's stack effect to the inferencer state.
dup "inline" word-prop [
dup recursive-state get peek first eq? [
dup recursive-effect consume/produce
] [
inline-closure
] if
2004-11-26 22:23:57 -05:00
] [
dup recursing? [
dup recursive-effect consume/produce
] [
apply-default
] if
] if ;