]> gitweb.factorcode.org Git - factor.git/commitdiff
starting to update simplifier for vops
authorSlava Pestov <slava@factorcode.org>
Mon, 9 May 2005 06:34:15 +0000 (06:34 +0000)
committerSlava Pestov <slava@factorcode.org>
Mon, 9 May 2005 06:34:15 +0000 (06:34 +0000)
20 files changed:
TODO.FACTOR.txt
library/alien/c-types.factor
library/alien/compiler.factor
library/bootstrap/boot-stage3.factor
library/compiler/compiler.factor
library/compiler/generator.factor
library/compiler/intrinsics.factor
library/compiler/linearizer.factor
library/compiler/optimizer.factor
library/compiler/simplifier.factor
library/compiler/vops.factor
library/compiler/x86/alien.factor
library/compiler/x86/fixnum.factor
library/compiler/x86/generator.factor
library/compiler/x86/stack.factor
library/generic/tuple.factor
library/inference/words.factor
library/test/benchmark/fib.factor
library/test/compiler/intrinsics.factor
library/test/tuple.factor

index 26548784a38a19008bb6e9602e886db50a2f59ec..6e3e02facc172e4f8855e3bcafc6f7dbbb8d52ac 100644 (file)
 - dipping seq-2nmap, seq-2each\r
 - array sort\r
 - tiled window manager\r
-- PPC #box-float #unbox-float\r
+- redo new compiler backend for PowerPC\r
 - weird bug uncovered during bootstrap stress-test\r
 - images saved from plugin do not work\r
 - making an image from plugin hangs\r
 - generic skip\r
+- inference needs to be more robust with heavily recursive code\r
 \r
 + plugin:\r
 \r
index 8d2efe03638b1d1b75064f29553174ca95c234a7..d1e619a363710aa98697ceb855624aec827c1772 100644 (file)
@@ -1,8 +1,9 @@
 ! Copyright (C) 2004, 2005 Slava Pestov.
 ! See http://factor.sf.net/license.txt for BSD license.
 IN: alien
-USING: assembler compiler errors generic hashtables kernel lists
-math namespaces parser sequences strings words ;
+USING: assembler compiler compiler-backend errors generic
+hashtables kernel lists math namespaces parser sequences strings
+words ;
 
 : <c-type> ( -- type )
     <namespace> [
index 42f477a1c5fd21d8518d1afc97680b094c57eeb2..e7a1f83a5ee349f790f4ca72f2ebd4b26eb69387 100644 (file)
@@ -1,8 +1,9 @@
 ! Copyright (C) 2004, 2005 Slava Pestov.
 ! See http://factor.sf.net/license.txt for BSD license.
 IN: alien
-USING: assembler compiler errors generic inference kernel lists
-math namespaces sequences stdio strings unparser words ;
+USING: assembler compiler compiler-backend errors generic
+inference kernel lists math namespaces sequences stdio strings
+unparser words ;
 
 ! ! ! WARNING ! ! !
 ! Reloading this file into a running Factor instance on Win32
index bb9efe1ff095eef85bb17f71979772c5ba8af557..5b769397341ba32783753735786c36ec654c3103 100644 (file)
@@ -1,7 +1,7 @@
 ! Copyright (C) 2004, 2005 Slava Pestov.
 ! See http://factor.sf.net/license.txt for BSD license.
 USING: alien assembler command-line compiler io-internals kernel
-lists namespaces parser sequences stdio unparser words ;
+lists math namespaces parser sequences stdio unparser words ;
 
 "Compiling base..." print
 
@@ -31,6 +31,7 @@ init-assembler
 
 compile? [
     \ car compile
+    \ * compile
     \ length compile
     \ = compile
     \ unparse compile
index 000d4ba510cdc975c686ecf02ce25710fbe0da14..c24a5c1b04cf8c21ed6a7c741418922a1333da16 100644 (file)
@@ -1,7 +1,7 @@
 ! Copyright (C) 2004, 2005 Slava Pestov.
 IN: compiler
-USING: errors inference kernel lists namespaces prettyprint
-stdio words ;
+USING: compiler-backend compiler-frontend errors inference
+kernel lists namespaces prettyprint stdio words ;
 
 : supported-cpu? ( -- ? )
     cpu "unknown" = not ;
index a6556611cbc30ca3831504d65298f9f1f9a76454..d569cb67a8c4f01e74975bf084e28ccdaf91859a 100644 (file)
@@ -1,8 +1,11 @@
 ! Copyright (C) 2004, 2005 Slava Pestov.
 ! See http://factor.sf.net/license.txt for BSD license.
-IN: compiler
-USING: assembler errors inference kernel lists math namespaces
-sequences strings vectors words ;
+IN: compiler-backend
+USING: assembler compiler errors inference kernel lists math
+namespaces sequences strings vectors words ;
+
+! Compile a VOP.
+GENERIC: generate-node ( vop -- )
 
 : generate-code ( word linear -- length )
     compiled-offset >r
index 29986cb7bb5ae67895e858c62d72b98a3aa436b7..ad2d719eed01b347c8c64c9929f9b723481d93a6 100644 (file)
@@ -1,9 +1,9 @@
 ! Copyright (C) 2005 Slava Pestov.
 ! See http://factor.sf.net/license.txt for BSD license.
-IN: compiler
-USING: assembler generic hashtables inference kernel
-kernel-internals lists math math-internals namespaces sequences
-words ;
+IN: compiler-frontend
+USING: assembler compiler-backend generic hashtables inference
+kernel kernel-internals lists math math-internals namespaces
+sequences words ;
 
 : immediate? ( obj -- ? )
     #! fixnums and f have a pointerless representation, and
@@ -171,6 +171,7 @@ words ;
     [[ fixnum<       %fixnum<       ]]
     [[ fixnum>=      %fixnum>=      ]]
     [[ fixnum>       %fixnum>       ]]
+    [[ eq?           %eq?           ]]
 ] [
     uncons over intrinsic
     [ literal, 0 , \ binary-op , ] make-list
index dfec8a9ebc246da691c4a0a20ea337f71ab3422d..4a5c799f5b2f277bed55de8f0be21bab87b865d7 100644 (file)
@@ -1,8 +1,8 @@
 ! Copyright (C) 2004, 2005 Slava Pestov.
 ! See http://factor.sf.net/license.txt for BSD license.
-IN: compiler
-USING: inference kernel lists math namespaces words strings
-errors prettyprint kernel-internals ;
+IN: compiler-frontend
+USING: compiler-backend inference kernel lists math namespaces
+words strings errors prettyprint kernel-internals ;
 
 : >linear ( node -- )
     #! Dataflow OPs have a linearizer word property. This
@@ -21,12 +21,6 @@ errors prettyprint kernel-internals ;
     #! rest is arguments.
     [ %prologue , (linearize) ] make-list ;
 
-: <label> ( -- label )
-    gensym  dup t "label" set-word-prop ;
-
-: label? ( obj -- ? )
-    dup word? [ "label" word-prop ] [ drop f ] ifte ;
-
 : linearize-simple-label ( node -- )
     #! Some labels become simple labels after the optimization
     #! stage.
@@ -46,7 +40,7 @@ errors prettyprint kernel-internals ;
     #! not contain non-tail recursive calls to itself.
     <label> dup %return-to , >r
     linearize-simple-label
-    %return ,
+    %return ,
     r> %label , ;
 
 #label [
@@ -105,4 +99,4 @@ errors prettyprint kernel-internals ;
 
 #values [ drop ] "linearizer" set-word-prop
 
-#return [ drop %return , ] "linearizer" set-word-prop
+#return [ drop %return , ] "linearizer" set-word-prop
index 54cd9ab414698275b477cd3a34a2cdbe3f836d00..a2cc4f50aa76b0447fc4139ba4cb657adb45150a 100644 (file)
@@ -1,6 +1,6 @@
 ! Copyright (C) 2004, 2005 Slava Pestov.
 ! See http://factor.sf.net/license.txt for BSD license.
-IN: compiler
+IN: compiler-frontend
 USING: inference kernel kernel-internals lists namespaces
 sequences vectors words words ;
 
index eea6f48ce3dc0d2eef794ae63deea66e4cbcb411..8d66824c5ae92f678e1198f027390ff9ce932c58 100644 (file)
 ! Copyright (C) 2004, 2005 Slava Pestov.
 ! See http://factor.sf.net/license.txt for BSD license.
-IN: compiler
-USING: inference kernel lists math namespaces prettyprint
-strings words ;
+IN: compiler-backend
+USING: generic inference kernel lists math namespaces
+prettyprint strings words ;
 
-: simplify ;
+! A peephole optimizer operating on the linear IR.
 
 ! The linear IR being simplified is stored in this variable.
-! SYMBOL: simplifying
-! 
-! : simplifiers ( linear -- list )
-!     #! A list of quotations with stack effect
-!     #! ( linear -- linear ? ) that can simplify the first node
-!     #! in the linear IR.
-!     car car "simplifiers" word-prop ;
-! 
-! : simplify-node ( linear list -- linear ? )
-!     dup [
-!         uncons >r call [
-!             r> drop t
-!         ] [
-!             r> simplify-node
-!         ] ifte
-!     ] when ;
-! 
-! : simplify-1 ( linear -- linear ? )
-!     #! Return a new linear IR.
-!     dup [
-!         dup simplifiers simplify-node
-!         [ uncons simplify-1 drop cons t ]
-!         [ uncons simplify-1 >r cons r> ] ifte
-!     ] [
-!         f
-!     ] ifte ;
-! 
-! : simplify ( linear -- linear )
-!     #! Keep simplifying until simplify-1 returns f.
-!     [
-!         dup simplifying set  simplify-1
-!     ] with-scope  [ simplify ] when ;
-! 
-! : label-called? ( label linear -- ? )
-!     [ uncons pick = swap #label = not and ] some? nip ;
-! 
-! #label [
-!     [
-!         dup car cdr simplifying get label-called?
-!         [ f ] [ cdr t ] ifte
-!     ]
-! ] "simplifiers" set-word-prop
-! 
-! : next-physical? ( op linear -- ? )
-!     cdr dup [ car car = ] [ 2drop f ] ifte ;
-! 
-! : cancel ( linear op -- linear param ? )
-!     #! If the following op is as given, remove it, and return
-!     #! its param.
-!     over next-physical? [ cdr unswons cdr t ] [ f f ] ifte ;
-! 
-! \ drop [
-!     [
-!         #push-immediate cancel [
-!             #replace-immediate swons swons t
-!         ] when
-!     ] [
-!         #push-indirect cancel [
-!             #replace-indirect swons swons t
-!         ] when
-!     ]
-! ] "simplifiers" set-word-prop
-! 
-! : find-label ( label -- rest )
-!     simplifying get [
-!         uncons pick = swap #label = and
-!     ] some? nip ;
-! 
-! : next-logical ( linear -- linear )
-!     dup car car "next-logical" word-prop call ;
-! 
-! #label [
-!     cdr next-logical
-! ] "next-logical" set-word-prop
-! 
-! #jump-label [
-!     car cdr find-label cdr
-! ] "next-logical" set-word-prop
-! 
-! #target-label [
-!     car cdr find-label cdr
-! ] "next-logical" set-word-prop
-! 
-! : next-logical? ( op linear -- ? )
-!     next-logical dup [ car car = ] [ 2drop f ] ifte ;
-! 
-! : reduce ( linear op new -- linear ? )
-!     >r over cdr next-logical? [
-!         unswons cdr r> swons swons t
-!     ] [
-!         r> drop f
-!     ] ifte ;
-! 
-! #call [
-!     [ #return #jump reduce ]
-! ] "simplifiers" set-word-prop
-! 
-! #call-label [
-!     [ #return #jump-label reduce ]
-! ] "simplifiers" set-word-prop
-! 
-! : double-jump ( linear op1 op2 -- linear ? )
-!     #! A jump to a jump is just a jump. If the next logical node
-!     #! is a jump of type op1, replace the jump at the car of the
-!     #! list with a jump of type op2.
-!     swap pick next-logical? [
-!         over next-logical car cdr cons swap cdr cons t
-!     ] [
-!         drop f
-!     ] ifte ;
-! 
-! : useless-jump ( linear -- linear ? )
-!     #! A jump to a label immediately following is not needed.
-!     dup car cdr find-label over cdr eq? [ cdr t ] [ f ] ifte ;
-! 
-! : (dead-code) ( linear -- linear ? )
-!     #! Remove all nodes until the next #label.
-!     dup [
-!         dup car car #label = [
-!             f
-!         ] [
-!             cdr (dead-code) t or
-!         ] ifte
-!     ] [
-!         f
-!     ] ifte ;
-! 
-! : dead-code ( linear -- linear ? )
-!     uncons (dead-code) >r cons r> ;
+SYMBOL: simplifying
+
+GENERIC: simplify-node ( linear vop -- linear ? )
+
+! The next node following this node in terms of control flow, or
+! f if this is a conditional.
+GENERIC: next-logical ( linear vop -- linear )
+
+! No delegation.
+M: tuple simplify-node drop f ;
+
+: simplify-1 ( list -- list ? )
+    #! Return a new linear IR.
+     dup [
+         dup car simplify-node
+         [ uncons simplify-1 drop cons t ]
+         [ uncons simplify-1 >r cons r> ] ifte
+     ] [
+         f
+     ] ifte ;
+
+: simplify ( linear -- linear )
+    #! Keep simplifying until simplify-1 returns f.
+    [
+        dup simplifying set  simplify-1
+    ] with-scope  [ simplify ] when ;
+
+: label-called? ( label -- ? )
+    simplifying get [ calls-label? ] some-with? ;
+
+M: %label simplify-node ( linear vop -- linear ? )
+    vop-label label-called? [ f ] [ cdr t ] ifte ;
+
+: next-physical? ( linear class -- vop ? )
+    #! If the following op has given class, remove it and
+    #! return it.
+    over cdr dup [
+        car class = [ cdr car t ] [ f ] ifte
+    ] [
+        3drop f f
+    ] ifte ;
+
+M: %inc-d simplify-node ( linear vop -- linear ? )
+    #! %inc-d cancels a following %inc-d.
+    >r dup \ %inc-d next-physical? [
+        vop-literal r> vop-literal + dup 0 = [
+            drop cdr cdr f
+        ] [
+            %inc-d >r cdr cdr r> swons t
+        ] ifte
+    ] [
+        r> 2drop f
+    ] ifte ;
+
+: dead-load? ( linear vop -- ? )
+    #! Is the %replace-d followed by a %peek-d of the same
+    #! stack slot and vreg?
+    swap cdr car dup %peek-d? [
+        over vop-source over vop-dest = >r
+        swap vop-literal swap vop-literal = r> and
+    ] [
+        2drop f
+    ] ifte ;
+
+: dead-store? ( linear n -- ? )
+    #! Is the %replace-d followed by a %dec-d, so the stored
+    #! value is lost?
+    swap \ %inc-d next-physical? [
+        vop-literal + 0 <
+    ] [
+        2drop f
+    ] ifte ;
+
+M: %replace-d simplify-node ( linear vop -- linear ? )
+    2dup dead-load? [
+        drop uncons cdr cons t
+    ] [
+        2dup vop-literal dead-store? [
+            drop cdr t
+        ] [
+            drop f
+        ] ifte
+    ] ifte ;
+
+M: %immediate-d simplify-node ( linear vop -- linear ? )
+    over 0 dead-store? [ drop cdr t ] [ drop f ] ifte ;
+
+: pop? ( vop -- ? ) dup %inc-d? swap vop-literal -1 = and ;
+
+: can-fast-branch? ( linear -- ? )
+    unswons class fast-branch [
+        unswons pop? [ car %jump-t? ] [ drop f ] ifte
+    ] [
+        drop f
+    ] ifte ;
+
+: fast-branch-params ( linear -- src dest label linear )
+    uncons >r dup vop-source swap vop-dest r> cdr
+    uncons >r vop-label r> ;
+
+M: %fixnum<= simplify-node ( linear vop -- linear ? )
+    drop dup can-fast-branch? [
+        fast-branch-params >r
+        %jump-fixnum<= >r -1 %inc-d r>
+        r> cons cons t
+    ] [
+        f
+    ] ifte ;
+
+M: %eq? simplify-node ( linear vop -- linear ? )
+    drop dup can-fast-branch? [
+        fast-branch-params >r
+        %jump-eq? >r -1 %inc-d r>
+        r> cons cons t
+    ] [
+        f
+    ] ifte ;
+
+: find-label ( label -- rest )
+    simplifying get [
+        dup %label? [ vop-label = ] [ 2drop f ] ifte
+    ] some-with? ;
+
+M: %label next-logical ( linear vop -- linear )
+    drop cdr dup car next-logical ;
+
+M: %jump-label next-logical ( linear vop -- linear )
+    nip vop-label find-label cdr ;
+
+M: %target-label next-logical ( linear vop -- linear )
+    nip vop-label find-label cdr ;
+
+M: object next-logical ( linear vop -- linear )
+    drop ;
+
+: next-logical? ( op linear -- ? )
+    dup car next-logical dup [ car class = ] [ 2drop f ] ifte ;
+
+: reduce ( linear op new -- linear ? )
+    >r over cdr next-logical? [
+        dup car vop-label
+        r> execute swap cdr cons t
+    ] [
+        r> drop f
+    ] ifte ; inline
+
+M: %call simplify-node ( linear vop -- ? )
+    #! Tail call optimization.
+    drop \ %return \ %jump reduce ;
+
+M: %call-label simplify-node ( linear vop -- ? )
+    #! Tail call optimization.
+    drop \ %return \ %jump-label reduce ;
+
+: double-jump ( linear op2 op1 -- linear ? )
+    #! A jump to a jump is just a jump. If the next logical node
+    #! is a jump of type op1, replace the jump at the car of the
+    #! list with a jump of type op2.
+    pick next-logical? [
+        >r dup dup car next-logical car vop-label
+        r> execute swap cdr cons t
+    ] [
+        drop f
+    ] ifte ; inline
+
+: useless-jump ( linear -- linear ? )
+    #! A jump to a label immediately following is not needed.
+    dup car cdr find-label over cdr eq? [ cdr t ] [ f ] ifte ;
+
+: (dead-code) ( linear -- linear ? )
+    #! Remove all nodes until the next #label.
+    dup [
+        dup car %label? [
+            f
+        ] [
+            cdr (dead-code) t or
+        ] ifte
+    ] [
+        f
+    ] ifte ;
+
+: dead-code ( linear -- linear ? )
+    uncons (dead-code) >r cons r> ;
+
+M: %jump-label simplify-node ( linear vop -- ? )
+    drop
+    \ %return dup double-jump [
+        t
+    ] [
+        \ %jump-label dup double-jump [
+            t
+        ] [
+            \ %jump dup double-jump
+            ! [
+            !     t
+            ! ] [
+            !     useless-jump [
+            !         t
+            !     ] [
+            !         dead-code
+            !     ] ifte
+            ! ] ifte
+        ] ifte
+    ] ifte ;
+
 ! 
 ! #jump-label [
 !     [ #return #return double-jump ]
@@ -146,8 +222,8 @@ strings words ;
 ! ] "simplifiers" set-word-prop
 ! 
 ! #target-label [
-!     [ #jump-label #target-label double-jump ]
-! !   [ #jump #target double-jump ]
+!     [ #target-label #jump-label double-jump ]
+! !   [ #target #jump double-jump ]
 ! ] "simplifiers" set-word-prop
 ! 
 ! #jump [ [ dead-code ] ] "simplifiers" set-word-prop
index 1c659cd7a6efb14cab5949e0957549533840656b..8312699052b0ed6832b69e2856a6c51924f70f27 100644 (file)
@@ -1,7 +1,8 @@
 ! Copyright (C) 2004, 2005 Slava Pestov.
 ! See http://factor.sf.net/license.txt for BSD license.
-IN: compiler
-USING: errors generic kernel namespaces parser ;
+IN: compiler-backend
+USING: errors generic hashtables kernel math namespaces parser
+words ;
 
 ! The linear IR is the second of the two intermediate
 ! representations used by Factor. It is basically a high-level
@@ -10,14 +11,22 @@ USING: errors generic kernel namespaces parser ;
 ! This file defines all the types of VOPs. A linear IR program
 ! is then just a list of VOPs.
 
+: <label> ( -- label )
+    #! Make a label.
+    gensym  dup t "label" set-word-prop ;
+
+: label? ( obj -- ? )
+    dup word? [ "label" word-prop ] [ drop f ] ifte ;
+
 ! A virtual register
 TUPLE: vreg n ;
 
 ! A virtual operation
 TUPLE: vop source dest literal label ;
 
-! Compile a VOP.
-GENERIC: generate-node ( vop -- )
+GENERIC: calls-label? ( label vop -- ? )
+
+M: vop calls-label? vop-label = ;
 
 : make-vop ( source dest literal label vop -- vop )
     [ >r <vop> r> set-delegate ] keep ;
@@ -40,8 +49,14 @@ VOP: %prologue
 : %prologue empty-vop <%prologue> ;
 VOP: %label
 : %label label-vop <%label> ;
+M: %label calls-label? 2drop f ;
+
+! Return vops take a label that is ignored, to have the
+! same stack effect as jumps. This is needed for the
+! simplifier.
 VOP: %return
-: %return empty-vop <%return> ;
+: %return ( label) label-vop <%return> ;
+
 VOP: %return-to
 : %return-to label-vop <%return-to> ;
 VOP: %jump
@@ -70,23 +85,23 @@ VOP: %end-dispatch
 ! stack operations
 VOP: %peek-d
 : %peek-d ( vreg n -- ) >r >r f r> <vreg> r> f <%peek-d> ;
-VOP: %dec-d
-: %dec-d ( n -- ) literal-vop <%dec-d> ;
 VOP: %replace-d
 : %replace-d ( vreg n -- ) >r <vreg> f r> f <%replace-d> ;
 VOP: %inc-d
 : %inc-d ( n -- ) literal-vop <%inc-d> ;
+: %dec-d ( n -- ) neg %inc-d ;
 VOP: %immediate
 VOP: %immediate-d
 : %immediate-d ( obj -- ) literal-vop <%immediate-d> ;
 VOP: %peek-r
 : %peek-r ( vreg n -- ) >r >r f r> <vreg> r> f <%peek-r> ;
-VOP: %dec-r
-: %dec-r ( n -- ) literal-vop <%dec-r> ;
 VOP: %replace-r
 : %replace-r ( vreg n -- ) >r <vreg> f r> f <%replace-r> ;
 VOP: %inc-r
 : %inc-r ( n -- ) literal-vop <%inc-r> ;
+! this exists, unlike %dec-d which does not, due to x86 quirks
+VOP: %dec-r
+: %dec-r ( n -- ) literal-vop <%dec-r> ;
 
 : in-1 0 0 %peek-d , ;
 : in-2 0 1 %peek-d ,  1 0 %peek-d , ;
@@ -128,13 +143,28 @@ VOP: %fixnum-bitor  : %fixnum-bitor src/dest-vop <%fixnum-bitor> ;
 VOP: %fixnum-bitxor : %fixnum-bitxor src/dest-vop <%fixnum-bitxor> ;
 VOP: %fixnum-bitnot : %fixnum-bitnot <vreg> dest-vop <%fixnum-bitnot> ;
 VOP: %fixnum-shift  : %fixnum-shift src/dest-vop <%fixnum-shift> ;
+
 VOP: %fixnum<=      : %fixnum<= src/dest-vop <%fixnum<=> ;
 VOP: %fixnum<       : %fixnum< src/dest-vop <%fixnum<> ;
 VOP: %fixnum>=      : %fixnum>= src/dest-vop <%fixnum>=> ;
 VOP: %fixnum>       : %fixnum> src/dest-vop <%fixnum>> ;
-
 VOP: %eq?           : %eq? src/dest-vop <%eq?> ;
 
+VOP: %jump-fixnum<= : %jump-fixnum<= f swap <%jump-fixnum<=> ;
+VOP: %jump-fixnum<  : %jump-fixnum< f swap <%jump-fixnum<> ;
+VOP: %jump-fixnum>= : %jump-fixnum>= f swap <%jump-fixnum>=> ;
+VOP: %jump-fixnum>  : %jump-fixnum> f swap <%jump-fixnum>> ;
+VOP: %jump-eq?      : %jump-eq? f swap <%jump-eq?> ;
+
+: fast-branch ( class -- class )
+    {{
+        [[ %fixnum<= %jump-fixnum<= ]]
+        [[ %fixnum<  %jump-fixnum<  ]]
+        [[ %fixnum>= %jump-fixnum>= ]]
+        [[ %fixnum>  %jump-fixnum>  ]]
+        [[ %eq?      %jump-eq?      ]]
+    }} hash ;
+
 ! some slightly optimized inline assembly
 VOP: %type
 : %type ( vreg ) <vreg> dest-vop <%type> ;
index 635b0a0a6a8f56c745c36d7e2466418e86188e48..6c6d08db3a0230f43ea70288a9de7299deb6dd3c 100644 (file)
@@ -1,6 +1,6 @@
 ! Copyright (C) 2005 Slava Pestov.
 ! See http://factor.sf.net/license.txt for BSD license.
-IN: compiler
+IN: compiler-backend
 USING: alien assembler inference kernel kernel-internals lists
 math memory namespaces words ;
 
index afb7893814c99fb10f58abc85de4c9be3831c41b..f770c57c885fd86a06c48a0dd881718d100f1f03 100644 (file)
@@ -1,8 +1,8 @@
 ! Copyright (C) 2005 Slava Pestov.
 ! See http://factor.sf.net/license.txt for BSD license.
-IN: compiler
-USING: assembler errors kernel math math-internals memory
-namespaces words ;
+IN: compiler-backend
+USING: assembler compiler errors kernel math math-internals
+memory namespaces words ;
 
 : simple-overflow ( dest -- )
     #! If the previous arithmetic operation overflowed, then we
@@ -130,3 +130,22 @@ M: %fixnum>= generate-node ( vop -- )
 
 M: %eq? generate-node ( vop -- )
     fixnum-compare  \ JE  conditional ;
+
+: fixnum-branch ( vop -- label )
+    dup vop-dest v>operand over vop-source v>operand CMP
+    vop-label ;
+
+M: %jump-fixnum< generate-node ( vop -- )
+    fixnum-branch JL ;
+
+M: %jump-fixnum<= generate-node ( vop -- )
+    fixnum-branch JLE ;
+
+M: %jump-fixnum> generate-node ( vop -- )
+    fixnum-branch JG ;
+
+M: %jump-fixnum>= generate-node ( vop -- )
+    fixnum-branch JGE ;
+
+M: %jump-eq? generate-node ( vop -- )
+    fixnum-branch JE ;
index eb421ffcdecfd1129c01d1f35d329da4832240c3..5b40c6c9962a2ab7e2e8d29f18d16b8c1b477166 100644 (file)
@@ -1,8 +1,8 @@
 ! Copyright (C) 2005 Slava Pestov.
 ! See http://factor.sf.net/license.txt for BSD license.
-IN: compiler
-USING: alien assembler inference kernel kernel-internals lists
-math memory namespaces sequences words ;
+IN: compiler-backend
+USING: alien assembler compiler inference kernel
+kernel-internals lists math memory namespaces sequences words ;
 
 GENERIC: v>operand
 M: integer v>operand address ;
index 6ee92999ad95261d68c496022db0a964b1bb43b5..7c36e63083e91b5e988255bde33ae57fba8a4e3c 100644 (file)
@@ -1,8 +1,8 @@
 ! Copyright (C) 2005 Slava Pestov.
 ! See http://factor.sf.net/license.txt for BSD license.
-IN: compiler
-USING: alien assembler inference kernel lists math memory
-sequences words ;
+IN: compiler-backend
+USING: alien assembler compiler inference kernel lists math
+memory sequences words ;
 
 : rel-cs ( -- )
     #! Add an entry to the relocation table for the 32-bit
@@ -20,14 +20,12 @@ sequences words ;
 M: %peek-d generate-node ( vop -- )
     dup vop-dest v>operand swap vop-literal ds-op MOV ;
 
-M: %dec-d generate-node ( vop -- )
-    vop-literal ESI swap cell * SUB ;
-
 M: %replace-d generate-node ( vop -- )
     dup vop-source v>operand swap vop-literal ds-op swap MOV ;
 
 M: %inc-d generate-node ( vop -- )
-    vop-literal ESI swap cell * ADD ;
+    ESI swap vop-literal cell *
+    dup 0 > [ ADD ] [ neg SUB ] ifte ;
 
 M: %immediate generate-node ( vop -- )
     dup vop-dest v>operand swap vop-literal address MOV ;
index 6eb933b821e03360e19e34dea417f88004b25f5d..18492b0e9618649aa775f4d38ac0e4ee47f45511 100644 (file)
@@ -68,8 +68,9 @@ UNION: arrayed array tuple ;
 : tuple-predicate ( word -- )
     #! Make a foo? word for testing the tuple class at the top
     #! of the stack.
-    dup predicate-word swap [ swap class eq? ] cons
-    define-compound ;
+    dup predicate-word swap [
+        literal, [ swap class eq? ] %
+    ] make-list define-compound ;
 
 : check-shape ( word slots -- )
     #! If the new list of slots is different from the previous,
index 7d1db296dc61c05e1e5ee6bf704daf53bcf49664..e9d2ecb7f5bba703cb3acbe0a1a850b8d6e96462 100644 (file)
@@ -1,8 +1,9 @@
 ! Copyright (C) 2004, 2005 Slava Pestov.
 ! See http://factor.sf.net/license.txt for BSD license.
 IN: inference
-USING: errors generic interpreter kernel lists math namespaces
-sequences strings vectors words hashtables parser prettyprint ;
+USING: errors generic interpreter kernel lists math
+math-internals namespaces sequences strings vectors words
+hashtables parser prettyprint ;
 
 : with-dataflow ( param op [[ in# out# ]] quot -- )
     #! Take input parameters, execute quotation, take output
@@ -170,8 +171,17 @@ M: word apply-object ( word -- )
 \ * [ [ number number ] [ number ] ] "infer-effect" set-word-prop
 \ - [ [ number number ] [ number ] ] "infer-effect" set-word-prop
 \ + [ [ number number ] [ number ] ] "infer-effect" set-word-prop
+\ integer/ [ [ integer integer ] [ rational ] ] "infer-effect" set-word-prop
+\ gcd [ [ integer integer ] [ integer integer ] ] "infer-effect" set-word-prop
 \ = [ [ object object ] [ boolean ] ] "infer-effect" set-word-prop
+\ <= [ [ number number ] [ boolean ] ] "infer-effect" set-word-prop
+\ < [ [ number number ] [ boolean ] ] "infer-effect" set-word-prop
+\ >= [ [ number number ] [ boolean ] ] "infer-effect" set-word-prop
+\ > [ [ number number ] [ boolean ] ] "infer-effect" set-word-prop
 \ <no-method> [ [ object object ] [ tuple ] ] "infer-effect" set-word-prop
+\ set-no-method-generic [ [ object tuple ] [ ] ] "infer-effect" set-word-prop
+\ set-no-method-object [ [ object tuple ] [ ] ] "infer-effect" set-word-prop
+\ car [ [ general-list ] [ object ] ] "infer-effect" set-word-prop
 
 \ no-method t "terminator" set-word-prop
 \ no-method [ [ object word ] [ ] ] "infer-effect" set-word-prop
index b63022de0ae5ef68c228038ea2bf527ad3b40bc0..17c06038c7e198e4e269a09f93198d8bad41316c 100644 (file)
@@ -20,3 +20,19 @@ USE: math-internals
     compiled
 
 [ 9227465 ] [ 34 fib ] unit-test
+
+TUPLE: box i ;
+
+: tuple-fib ( n -- n )
+    dup box-i 1 <= [
+        drop 1 <box>
+    ] [
+        box-i 1 - <box>
+        dup tuple-fib
+        swap
+        box-i 1 - <box>
+        tuple-fib
+        swap box-i swap box-i + <box>
+    ] ifte ; compiled
+
+[ << box f 9227465 ] [ << box f 34 >> tuple-fib ] unit-test
index 93edc36924a495e1973a185e6c5c75a88d11c2f5..6016a700a40e3de44bdaa87877b2fa5012542582 100644 (file)
@@ -32,6 +32,9 @@ math-internals test words ;
 [ 1 1 2 ] [ [ 1 2 dupd ] compile-1 ] unit-test
 [ 1 1 2 ] [ 1 [ 2 dupd ] compile-1 ] unit-test
 [ 1 1 2 ] [ 1 2 [ dupd ] compile-1 ] unit-test
+[ 2 ] [ [ 1 2 nip ] compile-1 ] unit-test
+[ 2 ] [ 1 [ 2 nip ] compile-1 ] unit-test
+[ 2 ] [ 1 2 [ nip ] compile-1 ] unit-test
 
 [ 4 ] [ 12 7 [ fixnum-bitand ] compile-1 ] unit-test
 [ 4 ] [ 12 [ 7 fixnum-bitand ] compile-1 ] unit-test
index ce1b8136440be3b6945cd9b6a8308f8929fb8fc5..34afdac24331b2a4f4c47f4db7dcd799157bae69 100644 (file)
@@ -51,3 +51,8 @@ C: quuux-tuple-2
     
     point-x
 ] unit-test
+
+TUPLE: predicate-test ;
+: predicate-test drop f ;
+
+[ t ] [ <predicate-test> predicate-test? ] unit-test