2008-07-20 05:24:37 -04:00
|
|
|
! Copyright (C) 2008 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-11-12 20:08:40 -05:00
|
|
|
USING: assocs arrays namespaces sequences kernel definitions
|
|
|
|
math effects accessors words fry classes.algebra
|
2008-12-04 07:02:49 -05:00
|
|
|
compiler.units stack-checker.values stack-checker.visitor ;
|
2008-07-20 05:24:37 -04:00
|
|
|
IN: stack-checker.state
|
|
|
|
|
|
|
|
! Did the current control-flow path throw an error?
|
|
|
|
SYMBOL: terminated?
|
|
|
|
|
|
|
|
! Number of inputs current word expects from the stack
|
|
|
|
SYMBOL: d-in
|
|
|
|
|
2008-12-04 07:02:49 -05:00
|
|
|
DEFER: commit-literals
|
|
|
|
|
2008-07-20 05:24:37 -04:00
|
|
|
! Compile-time data stack
|
2008-12-04 07:02:49 -05:00
|
|
|
: meta-d ( -- stack ) commit-literals \ meta-d get ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
|
|
|
! Compile-time retain stack
|
2008-12-04 07:02:49 -05:00
|
|
|
: meta-r ( -- stack ) \ meta-r get ;
|
|
|
|
|
|
|
|
! Uncommitted literals. This is a form of local dead-code
|
|
|
|
! elimination; the goal is to reduce the number of IR nodes
|
|
|
|
! which get constructed. Technically it is redundant since
|
|
|
|
! we do global DCE later, but it speeds up compile time.
|
|
|
|
SYMBOL: literals
|
|
|
|
|
|
|
|
: (push-literal) ( obj -- )
|
|
|
|
dup <literal> make-known
|
|
|
|
[ nip \ meta-d get push ] [ #push, ] 2bi ;
|
|
|
|
|
|
|
|
: commit-literals ( -- )
|
|
|
|
literals get [
|
|
|
|
[ [ (push-literal) ] each ] [ delete-all ] bi
|
|
|
|
] unless-empty ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-12-04 07:02:49 -05:00
|
|
|
: current-stack-height ( -- n ) meta-d length d-in get - ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
|
|
|
: current-effect ( -- effect )
|
|
|
|
d-in get
|
2008-12-04 07:02:49 -05:00
|
|
|
meta-d length <effect>
|
2008-07-20 05:24:37 -04:00
|
|
|
terminated? get >>terminated? ;
|
|
|
|
|
|
|
|
: init-inference ( -- )
|
|
|
|
terminated? off
|
2008-12-04 07:02:49 -05:00
|
|
|
V{ } clone \ meta-d set
|
|
|
|
V{ } clone literals set
|
2008-07-20 05:24:37 -04:00
|
|
|
0 d-in set ;
|
|
|
|
|
|
|
|
! Words that the current quotation depends on
|
|
|
|
SYMBOL: dependencies
|
|
|
|
|
|
|
|
: depends-on ( word how -- )
|
2008-09-03 19:23:48 -04:00
|
|
|
over primitive? [ 2drop ] [
|
|
|
|
dependencies get dup [
|
2008-09-10 23:11:40 -04:00
|
|
|
swap '[ _ strongest-dependency ] change-at
|
2008-09-03 19:23:48 -04:00
|
|
|
] [ 3drop ] if
|
|
|
|
] if ;
|
2008-08-31 02:34:00 -04:00
|
|
|
|
|
|
|
! Generic words that the current quotation depends on
|
|
|
|
SYMBOL: generic-dependencies
|
|
|
|
|
2008-11-12 20:08:40 -05:00
|
|
|
: ?class-or ( class/f class -- class' )
|
|
|
|
swap [ class-or ] when* ;
|
|
|
|
|
2008-08-31 02:34:00 -04:00
|
|
|
: depends-on-generic ( generic class -- )
|
|
|
|
generic-dependencies get dup
|
2008-11-12 20:08:40 -05:00
|
|
|
[ swap '[ _ ?class-or ] change-at ] [ 3drop ] if ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
|
|
|
! Words we've inferred the stack effect of, for rollback
|
|
|
|
SYMBOL: recorded
|