]> gitweb.factorcode.org Git - factor.git/commitdiff
partial evaluation of branches
authorSlava Pestov <slava@factorcode.org>
Mon, 27 Dec 2004 20:27:18 +0000 (20:27 +0000)
committerSlava Pestov <slava@factorcode.org>
Mon, 27 Dec 2004 20:27:18 +0000 (20:27 +0000)
library/cli.factor
library/compiler/compiler.factor
library/inference/branches.factor
library/inference/inference.factor
library/inference/words.factor
library/test/inference.factor

index 600d146f8ed3b9d31d4486f81b0398c837692dca..91774132a75a31cdca7c8f6790ee96601b65d260 100644 (file)
@@ -85,11 +85,12 @@ USE: kernel-internals
 : default-cli-args
     #! Some flags are *on* by default, unless user specifies
     #! -no-<flag> CLI switch
-    t "user-init" set
-    t "interactive" set
-    t "smart-terminal" set
-    t "verbose-compile" set
-    t "compile" set ;
+    "user-init" on
+    "interactive" on
+    "smart-terminal" on
+    "verbose-compile" on
+    "compile" on
+    os "win32" = [ "graphical" on ] when ;
 
 : cli-args ( -- args ) 10 getenv ;
 
index 1f0bb60ee4934f4c0168a44e73a989b93b45b299..66b245e222d96dece05c8b1943c108a387ae30bd 100644 (file)
@@ -41,6 +41,7 @@ USE: strings
 USE: unparser
 USE: vectors
 USE: words
+USE: test
 
 : supported-cpu? ( -- ? )
     cpu "unknown" = not ;
@@ -97,7 +98,7 @@ M: compound (compile) ( word -- )
 : compile-all ( -- )
     #! Compile all words.
     supported-cpu? [
-        [ try-compile ] each-word
+        [ [ try-compile ] each-word ] time
     ] [
         "Unsupported CPU" print
     ] ifte ;
index fd643a95b24f1421e8485f1ec26563fb51286a9f..250a4fd5bf8e4961174fb77f6e4c4d3384d365be 100644 (file)
@@ -39,6 +39,10 @@ USE: words
 USE: hashtables
 USE: prettyprint
 
+! If this symbol is on, partial evalution of conditionals is
+! disabled.
+SYMBOL: inferring-base-case
+
 : vector-length< ( vec1 vec2 -- ? )
     swap vector-length swap vector-length < ;
 
@@ -127,9 +131,8 @@ SYMBOL: cloned
     d-in [ deep-clone-vector ] change
     dataflow-graph off ;
 
-: infer-branch ( value save-effect -- namespace )
+: infer-branch ( value -- namespace )
     <namespace> [
-        save-effect set
         uncons [ unswons [ \ value-class set ] bind ] when*
         dup value-recursion recursive-state set
         copy-inference
@@ -154,7 +157,7 @@ SYMBOL: dual-recursive-state
     #! Return effect namespace if inference didn't fail.
     [
         [ dual-branch dual-recursive-state set ] keep
-        infer-branch
+        infer-branch
     ] [
         [ 2drop f ] when
     ] catch ;
@@ -167,12 +170,16 @@ SYMBOL: dual-recursive-state
     #! Either the word is not recursive, or it is recursive
     #! and the base case throws an error.
     [
+        inferring-base-case on
+
         [ terminator-quot? not ] subset dup length 1 > [
             infer-base-cases unify-effects
             effect dual-recursive-state get set-base
         ] [
             drop
         ] ifte
+        
+        inferring-base-case off
     ] with-scope ;
 
 : (infer-branches) ( branchlist -- list )
@@ -182,7 +189,7 @@ SYMBOL: dual-recursive-state
     #! is a pair [ value | class ] indicating a type propagation
     #! for the given branch.
     dup infer-base-case [
-        dup infer-branch swap terminator-quot? [
+        dup infer-branch swap terminator-quot? [
             [ meta-d off meta-r off d-in off ] extend
         ] when
     ] map ;
@@ -198,11 +205,17 @@ SYMBOL: dual-recursive-state
     #! parameter is a vector.
     (infer-branches) dup unify-effects unify-dataflow ;
 
+: static-branch? ( value -- )
+    literal? inferring-base-case get not and ;
+
 : static-ifte ( true false -- )
     #! If the branch taken is statically known, just infer
     #! along that branch.
-    pop-d literal-value [ drop ] [ nip ] ifte
-    literal-value infer-quot ;
+    dataflow-drop, pop-d literal-value [ drop ] [ nip ] ifte
+    gensym [
+        dup value-recursion recursive-state set
+        literal-value infer-quot
+    ] (with-block) ;
 
 : dynamic-ifte ( true false -- )
     #! If branch taken is computed, infer along both paths and
@@ -219,11 +232,11 @@ SYMBOL: dual-recursive-state
     [ object general-list general-list ] ensure-d
     dataflow-drop, pop-d
     dataflow-drop, pop-d swap
-!    peek-d literal? [
-!        static-ifte
-!    ] [
-        dynamic-ifte ;
-!    ] ifte ;
+    peek-d static-branch? [
+        static-ifte
+    ] [
+        dynamic-ifte
+    ] ifte ;
 
 \ ifte [ infer-ifte ] "infer" set-word-property
 
index d19ee27022e117f47f5eb2d5ffefd717020c62f5..b7fa68452be7462721520625cb3660a1d6fda951 100644 (file)
@@ -59,12 +59,6 @@ SYMBOL: entry-effect
 ! makes a local jump to this label.
 SYMBOL: recursive-label
 
-! When inferring stack effects of mutually recursive words, we
-! don't want to save the fact that one word does not have a
-! stack effect before the base case of its mutual pair is
-! inferred.
-SYMBOL: save-effect
-
 ! A value has the following slots:
 GENERIC: literal-value ( value -- obj )
 GENERIC: value= ( literal value -- ? )
@@ -149,8 +143,7 @@ M: literal value-class-and ( class value -- )
     init-interpreter
     0 <vector> d-in set
     recursive-state set
-    dataflow-graph off
-    save-effect on ;
+    dataflow-graph off ;
 
 DEFER: apply-word
 
index f5e1a1d4ce05a4534766b3524b3c529d77751553..aa954b2c41251e6306f1c65fb09cd674cee52b72 100644 (file)
@@ -109,29 +109,14 @@ USE: prettyprint
         [ swap <chained-error> rethrow ] when*
     ] catch ;
 
-: (infer-compound) ( word -- effect )
+: infer-compound ( word -- effect )
     #! Infer a word's stack effect in a separate inferencer
     #! instance.
     [
         recursive-state get init-inference
-        dup inline-compound
+        dup dup inline-compound
         [ "infer-effect" set-word-property ] keep
-    ] with-scope ;
-
-: infer-compound ( word -- )
-    #! Infer the stack effect of a compound word in a separate
-    #! inferencer instance, caching the result.
-    [
-        dup (infer-compound) consume/produce
-    ] [
-        [
-            swap save-effect get [
-                t "no-effect" set-word-property
-            ] [
-                drop
-            ] ifte rethrow
-        ] when*
-    ] catch ;
+    ] with-scope consume/produce ;
 
 GENERIC: (apply-word)
 
index 89173069ace24924e528a86d3798a9afc702b99d..09696199a51e642b0f79f9c5a5ecfe8a79d0971a 100644 (file)
@@ -41,7 +41,6 @@ USE: generic
 [ [ call ] infer old-effect ] unit-test-fails
 
 [ [ 2 | 4 ] ] [ [ 2dup ] infer old-effect ] unit-test
-[ [ 2 | 0 ] ] [ [ set-vector-length ] infer old-effect ] unit-test
 [ [ 2 | 0 ] ] [ [ vector-push ] infer old-effect ] unit-test
 
 [ [ 1 | 0 ] ] [ [ [ ] [ ] ifte ] infer old-effect ] unit-test
@@ -99,6 +98,13 @@ USE: generic
 
 [ [ bad-recursion-2 ] infer old-effect ] unit-test-fails
 
+! Not sure how to fix this one
+
+! : funny-recursion
+!     dup [ funny-recursion 1 ] [ 2 ] ifte drop ;
+! 
+! [ [ 1 | 1 ] ] [ [ funny-recursion ] infer old-effect ] unit-test
+
 ! Simple combinators
 [ [ 1 | 2 ] ] [ [ [ car ] keep cdr ] infer old-effect ] unit-test
 
@@ -123,6 +129,9 @@ DEFER: foe
         2drop f
     ] ifte ;
 
+[ [ 2 | 1 ] ] [ [ fie ] infer old-effect ] unit-test
+[ [ 2 | 1 ] ] [ [ foe ] infer old-effect ] unit-test
+
 ! This form should not have a stack effect
 : bad-bin 5 [ 5 bad-bin bad-bin 5 ] [ 2drop ] ifte ;
 [ [ bad-bin ] infer old-effect ] unit-test-fails
@@ -149,9 +158,8 @@ SYMBOL: sym-test
 
 [ [ 0 | 1 ] ] [ [ sym-test ] infer old-effect ] unit-test
 
-[ [ 2 | 1 ] ] [ [ fie ] infer old-effect ] unit-test
-[ [ 2 | 1 ] ] [ [ foe ] infer old-effect ] unit-test
 
+[ [ 2 | 0 ] ] [ [ set-vector-length ] infer old-effect ] unit-test
 [ [ 2 | 1 ] ] [ [ 2list ] infer old-effect ] unit-test
 [ [ 3 | 1 ] ] [ [ 3list ] infer old-effect ] unit-test
 [ [ 2 | 1 ] ] [ [ append ] infer old-effect ] unit-test