factor/library/compiler/inference/words.factor

167 lines
5.1 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
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.
2005-09-14 00:37:50 -04:00
swap #call
over effect-in length over consume-values
over effect-out length over produce-values
node, effect-terminated? [ terminate ] when ;
2004-11-26 22:23:57 -05: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
TUPLE: rstate label count ;
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
: add-recursive-state ( word label count -- )
2006-08-10 16:33:15 -04:00
<rstate> 2array recursive-state [ swap add ] change ;
: inline-block ( word count -- node-block variables )
[
2006-08-10 16:33:15 -04:00
copy-inference nest-node
>r gensym 2dup r> add-recursive-state
#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
meta-d [ length tail* ] 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.
dup 0 inline-block over recursive-label? [
meta-d get >r
drop join-values 0 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 ;
: infer-compound ( word count -- effect )
#! Infer a word's stack effect in a separate inferencer
#! instance. Outputs a true boolean if the word terminates
2005-09-17 22:25:18 -04:00
#! control flow by throwing an exception or restoring a
#! continuation.
2004-11-26 22:23:57 -05:00
[
2006-01-19 23:28:45 -05:00
recursive-state get init-inference
over >r inline-block nip
[ current-effect ] bind r>
] 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
: effect-error ( -- * ) <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.
[
dup 0 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.
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 recursive-state get <reversed> assoc [
dup recursive-effect consume/produce
2004-11-26 22:23:57 -05:00
] [
2005-08-29 21:00:39 -04:00
dup "inline" word-prop
[ inline-closure ] [ apply-default ] if
] if ;