]> gitweb.factorcode.org Git - factor.git/commitdiff
compiler.cfg.*: some refactoring in the stack tracking for #shuffle nodes
authorBjörn Lindqvist <bjourne@gmail.com>
Thu, 19 Mar 2015 17:03:49 +0000 (17:03 +0000)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 29 Apr 2015 16:31:55 +0000 (09:31 -0700)
instead of having separate words for the stacks like inc-d/r and ds/rs-store use generic words that work on either stack

basis/compiler/cfg/builder/alien/alien-tests.factor [new file with mode: 0644]
basis/compiler/cfg/builder/alien/alien.factor
basis/compiler/cfg/builder/builder-tests.factor
basis/compiler/cfg/builder/builder.factor
basis/compiler/cfg/intrinsics/simd/backend/backend.factor
basis/compiler/cfg/stacks/local/local-docs.factor
basis/compiler/cfg/stacks/local/local-tests.factor
basis/compiler/cfg/stacks/local/local.factor
basis/compiler/cfg/stacks/stacks-docs.factor
basis/compiler/cfg/stacks/stacks-tests.factor
basis/compiler/cfg/stacks/stacks.factor

diff --git a/basis/compiler/cfg/builder/alien/alien-tests.factor b/basis/compiler/cfg/builder/alien/alien-tests.factor
new file mode 100644 (file)
index 0000000..bf60204
--- /dev/null
@@ -0,0 +1,13 @@
+USING: alien.c-types compiler.cfg.builder.alien compiler.cfg.instructions
+compiler.cfg.registers compiler.cfg.stacks.local compiler.cfg.stacks.tests
+cpu.architecture kernel make namespaces tools.test ;
+IN: compiler.cfg.builder.alien.tests
+
+{
+    { 2 3 }
+    { { int-rep f f } { int-rep f f } }
+    V{ T{ ##unbox-any-c-ptr { dst 2 } { src 1 } } }
+} [
+    test-init
+    [ { c-string int } unbox-parameters ] V{ } make
+] unit-test
index 29dd64d6709b68d4139adfdbcf30e93404f5e0d3..d53571720d2dc1d7a8fc0917d9a661951dba0633 100644 (file)
@@ -32,7 +32,7 @@ IN: compiler.cfg.builder.alien
         [ [ <ds-loc> peek-loc ] [ base-type ] bi* unbox-parameter ]
         2 2 mnmap [ concat ] bi@
     ]
-    [ length neg inc-d ] bi ;
+    [ length neg <ds-loc> inc-stack ] bi ;
 
 : prepare-struct-caller ( vregs reps return -- vregs' reps' return-vreg/f )
     dup large-struct? [
index 61e3f9aaca2cad3bef35d60ac4a8fc4f02c71aa1..4dfbadee418f662035f76c21804059057140bb33 100644 (file)
@@ -1,13 +1,13 @@
 USING: accessors alien alien.accessors arrays assocs byte-arrays
 combinators.short-circuit compiler.cfg compiler.cfg.builder compiler.cfg.checker
 compiler.cfg.debugger compiler.cfg.instructions compiler.cfg.optimizer
-compiler.cfg.predecessors compiler.cfg.registers compiler.cfg.rpo
-compiler.cfg.stacks compiler.cfg.stacks.local compiler.cfg.utilities
-compiler.tree compiler.tree.builder compiler.tree.optimizer
-compiler.cfg.representations fry hashtables kernel kernel.private locals make
-math math.partial-dispatch math.private namespaces prettyprint sbufs sequences
-sequences.private slots.private strings strings.private tools.test vectors
-words ;
+compiler.cfg.predecessors compiler.cfg.registers compiler.cfg.representations
+compiler.cfg.rpo compiler.cfg.stacks compiler.cfg.stacks.local
+compiler.cfg.stacks.tests compiler.cfg.utilities compiler.tree
+compiler.tree.builder compiler.tree.optimizer fry hashtables kernel
+kernel.private locals make math math.partial-dispatch math.private namespaces
+prettyprint sbufs sequences sequences.private slots.private strings
+strings.private tools.test vectors words ;
 FROM: alien.c-types => int ;
 IN: compiler.cfg.builder.tests
 
@@ -239,11 +239,7 @@ IN: compiler.cfg.builder.tests
 
 ! make-input-map
 {
-    H{
-        { 81 T{ ds-loc { n 1 } } }
-        { 37 T{ ds-loc { n 2 } } }
-        { 92 T{ ds-loc } }
-    }
+    { { 37 D 2 } { 81 D 1 } { 92 D 0 } }
 } [
     T{ #shuffle { in-d { 37 81 92 } } } make-input-map
 ] unit-test
@@ -283,3 +279,23 @@ IN: compiler.cfg.builder.tests
     V{ } 1 insns>block [ emit-loop-call ] V{ } make drop
     basic-block get successors>> length
 ] unit-test
+
+! store-shuffle
+{
+    H{ { D 2 1 } }
+} [
+    test-init
+    T{ #shuffle { in-d { 7 3 0 } } { out-d { 55 } } { mapping { { 55 3 } } } }
+    emit-node replace-mapping get
+] unit-test
+
+{
+    H{ { D -1 1 } { D 0 1 } }
+} [
+    test-init
+    T{ #shuffle
+       { in-d { 7 } }
+       { out-d { 55 77 } }
+       { mapping { { 55 7 } { 77 7 } } }
+    } emit-node replace-mapping get
+] unit-test
index 859366d09cd6bf43640f6304bb3c2b4eb29dc902..847964e30a3856cc31c1874cafe2f8ae51ad2dfa 100644 (file)
@@ -1,6 +1,6 @@
 ! Copyright (C) 2004, 2010 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: accessors assocs combinators compiler.cfg
+USING: accessors arrays assocs combinators compiler.cfg
 compiler.cfg.builder.blocks compiler.cfg.comparisons
 compiler.cfg.hats compiler.cfg.instructions
 compiler.cfg.intrinsics compiler.cfg.registers
@@ -161,28 +161,26 @@ M: #push emit-node
 ! we try not to introduce useless ##peeks here, since this reduces
 ! the accuracy of global stack analysis.
 
-
-
 : make-input-map ( #shuffle -- assoc )
-    [
-        [ in-d>> <reversed> [ <ds-loc> swap ,, ] each-index ]
-        [ in-r>> <reversed> [ <rs-loc> swap ,, ] each-index ] bi
-    ] H{ } make ;
+    [ in-d>> ds-loc ] [ in-r>> rs-loc ] bi
+    [ over vregs>stack-locs zip ] 2bi@ append ;
+
+: height-changes ( #shuffle -- height-changes )
+    { [ out-d>> ] [ in-d>> ] [ out-r>> ] [ in-r>> ] } cleave
+    4array [ length ] map first4 [ - ] 2bi@ 2array ;
 
-: make-output-seq ( values mapping input-map -- vregs )
-    '[ _ at _ at peek-loc ] map ;
+: store-height-changes ( #shuffle -- )
+    height-changes { ds-loc rs-loc } [ new swap >>n inc-stack ] 2each ;
 
-: load-shuffle ( #shuffle mapping input-map -- ds-vregs rs-vregs )
-    [ [ out-d>> ] 2dip make-output-seq ]
-    [ [ out-r>> ] 2dip make-output-seq ] 3bi ;
+: extract-outputs ( #shuffle -- seq )
+    [ out-d>> ds-loc 2array ] [ out-r>> rs-loc 2array ] bi 2array ;
 
-: store-shuffle ( #shuffle ds-vregs rs-vregs -- )
-    [ [ in-d>> length neg inc-d ] dip ds-store ]
-    [ [ in-r>> length neg inc-r ] dip rs-store ]
-    bi-curry* bi ;
+: out-vregs/stack ( #shuffle -- seq )
+    [ make-input-map ] [ mapping>> ] [ extract-outputs ] tri
+    [ first2 [ [ of of peek-loc ] 2with map ] dip 2array ] 2with map ;
 
 M: #shuffle emit-node
-    dup dup [ mapping>> ] [ make-input-map ] bi load-shuffle store-shuffle ;
+    [ out-vregs/stack ] keep store-height-changes [ first2 store-vregs ] each ;
 
 ! #return
 : end-word ( -- )
index e8cf71bd41573f71be7a4aeb48b73e9734ee2f9a..5a526ec579d18b7d74e16033da825da6d0b65589 100644 (file)
@@ -125,21 +125,21 @@ MACRO: if-literals-match ( quots -- )
     ] ;
 
 CONSTANT: [unary]        [ ds-drop  ds-pop ]
-CONSTANT: [unary/param]  [ [ -2 inc-d ds-pop ] dip ]
+CONSTANT: [unary/param]  [ [ -2 <ds-loc> inc-stack ds-pop ] dip ]
 CONSTANT: [binary]       [ ds-drop 2inputs ]
-CONSTANT: [binary/param] [ [ -2 inc-d 2inputs ] dip ]
+CONSTANT: [binary/param] [ [ -2 <ds-loc> inc-stack 2inputs ] dip ]
 CONSTANT: [quaternary]
     [
-        ds-drop 
+        ds-drop
         D 3 peek-loc
         D 2 peek-loc
         D 1 peek-loc
         D 0 peek-loc
-        -4 inc-d
+        -4 <ds-loc> inc-stack
     ]
 
 :: [emit-vector-op] ( trials params-quot op-quot literal-preds -- quot )
-    params-quot trials op-quot literal-preds 
+    params-quot trials op-quot literal-preds
     '[ [ _ dip _ @ ds-push ] _ if-literals-match ] ;
 
 MACRO: emit-v-vector-op ( trials -- )
@@ -158,6 +158,5 @@ MACRO:: emit-vv-or-vl-vector-op ( var-trials imm-trials literal-pred -- )
     '[
         dup node-input-infos 2 tail-slice* first literal>> @
         [ _ _ emit-vl-vector-op ]
-        [ _   emit-vv-vector-op ] if 
+        [ _   emit-vv-vector-op ] if
     ] ;
-
index a2dce8a6daf0be38a8db64d053848d4c70bd2d84..e8d6903d7ac8a66ed113381d2abbc7d92bc54243 100644 (file)
@@ -47,13 +47,9 @@ HELP: height-state>insns
 HELP: emit-changes
 { $description "Insert height and stack changes prior to the last instruction." } ;
 
-HELP: inc-d
-{ $values { "n" number } }
-{ $description "Increases or decreases the current datastacks height. An " { $link ##inc } " instruction will later be inserted." } ;
-
-HELP: inc-r
-{ $values { "n" number } }
-{ $description "Increases or decreases the current retainstacks height. An " { $link ##inc } " instruction will later be inserted." } ;
+HELP: inc-stack
+{ $values { "loc" loc } }
+{ $description "Increases or decreases the data or retain stack depending on if loc is a " { $link ds-loc } " or " { $link rs-loc } " instance. An " { $link ##inc } " instruction will later be inserted." } ;
 
 ARTICLE: "compiler.cfg.stacks.local" "Local stack analysis"
 "Local stack analysis. We build three sets for every basic block in the CFG:"
@@ -61,7 +57,19 @@ ARTICLE: "compiler.cfg.stacks.local" "Local stack analysis"
   "peek-set: all stack locations that the block reads before writing"
   "replace-set: all stack locations that the block writes"
   "kill-set: all stack locations which become unavailable after the block ends because of the stack height being decremented" }
-"This is done while constructing the CFG." ;
+"This is done while constructing the CFG."
+$nl
+"Words for reading the stack state:"
+{ $subsections
+  peek-loc
+  translate-local-loc }
+"Words for writing the stack state:"
+{ $subsections
+  adjust
+  inc-stack
+  modify-height
+  replace-loc
+} ;
 
 
 ABOUT: "compiler.cfg.stacks.local"
index cef8641d4ddebc471b26152140ea7f46b08b2060..851a13adfaddda5e1606a343503b19f51f600e93 100644 (file)
@@ -1,21 +1,21 @@
 USING: accessors assocs biassocs combinators compiler.cfg
 compiler.cfg.instructions compiler.cfg.registers compiler.cfg.stacks
-compiler.cfg.stacks.height compiler.cfg.stacks.local compiler.cfg.utilities
-cpu.architecture namespaces kernel tools.test ;
+compiler.cfg.stacks.height compiler.cfg.stacks.local compiler.cfg.stacks.tests
+compiler.cfg.utilities cpu.architecture namespaces kernel tools.test ;
 IN: compiler.cfg.stacks.local.tests
 
 {
     { { 3 3 } { 0 0 } }
 } [
-    initial-height-state height-state set
-    3 inc-d height-state get
+    test-init
+    3 <ds-loc> inc-stack height-state get
 ] unit-test
 
 {
     { { 5 3 } { 0 0 } }
 } [
     { { 2 0 } { 0 0 } } height-state set
-    3 inc-d height-state get
+    3 <ds-loc> inc-stack height-state get
 ] unit-test
 
 {
@@ -39,9 +39,8 @@ IN: compiler.cfg.stacks.local.tests
 ] unit-test
 
 { 80 } [
-    initial-height-state height-state set
-    H{ } clone replace-mapping set 80
-    D 77 replace-loc D 77 peek-loc
+    test-init
+    80 D 77 replace-loc D 77 peek-loc
 ] unit-test
 
 { 0 } [
@@ -58,5 +57,5 @@ IN: compiler.cfg.stacks.local.tests
 ] unit-test
 
 { D 2 } [
-    { { 1 2 } { 3 4 } } D 3 translate-local-loc2
+    { { 1 2 } { 3 4 } } D 3 translate-local-loc
 ] unit-test
index 96155c76624a38138c8ca45b8efdf158b8934592..3d7e1198b5f0cbe197e1a633f02a37c51db7ad26 100644 (file)
@@ -40,9 +40,10 @@ IN: compiler.cfg.stacks.local
     [ [ <ds-loc> ] map ] [ [ <rs-loc> ] map ] bi*
     append unique ;
 
-SYMBOLS: height-state peek-sets replace-sets kill-sets ;
+SYMBOLS: height-state peek-sets replace-sets kill-sets locs>vregs ;
 
-SYMBOL: locs>vregs
+: inc-stack ( loc -- )
+    height-state get swap modify-height ;
 
 : loc>vreg ( loc -- vreg ) locs>vregs get [ drop next-vreg ] cache ;
 : vreg>loc ( vreg -- loc/f ) locs>vregs get value-at ;
@@ -58,12 +59,6 @@ SYMBOLS: local-peek-set local-replace-set replace-mapping ;
     height-state get height-state>insns %
     , ;
 
-: inc-d ( n -- )
-    height-state get swap <ds-loc> modify-height ;
-
-: inc-r ( n -- )
-    height-state get swap <rs-loc> modify-height ;
-
 : peek-loc ( loc -- vreg )
     height-state get swap translate-local-loc
     dup replace-mapping get at
index 309298d1ef72154e7a0dc9f4b0aecdbbd124cacb..720548dc4337c2d2dbce583d4c5770f3284cb5dd 100644 (file)
@@ -1,5 +1,6 @@
-USING: compiler.cfg.instructions compiler.cfg.stacks.local compiler.tree
-help.markup help.syntax math sequences ;
+USING: compiler.cfg.instructions compiler.cfg.registers
+compiler.cfg.stacks.local compiler.tree help.markup help.syntax math
+sequences ;
 IN: compiler.cfg.stacks
 
 HELP: ds-push
@@ -20,13 +21,12 @@ HELP: adjust-d
 HELP: ds-drop
 { $description "Used to signal to the stack analysis that the datastacks height is decreased by one." } ;
 
-HELP: ds-store
-{ $values { "vregs" "a " { $link sequence } " of vregs." } }
-{ $description "Registers that a sequence of vregs are stored at at each corresponding index of the data stack. It is used for compiling " { $link #shuffle } " nodes." } ;
-
-HELP: rs-store
-{ $values { "vregs" "a " { $link sequence } " of vregs." } }
-{ $description "Stores one or more virtual register values on the retain stack. This modifies the " { $link height-state } " dynamic variable." } ;
+HELP: store-vregs
+{ $values
+  { "vregs" "a " { $link sequence } " of vregs" }
+  { "loc-class" "either " { $link ds-loc } " or " { $link rs-loc } }
+}
+{ $description "Stores one or more virtual register values on the data or retain stack. The " { $link replace-mapping } " dynamic variable is modified but the " { $link height-state } " is not touched" } ;
 
 HELP: 2inputs
 { $values { "vreg1" "a vreg" } { "vreg2" "a vreg" } }
index d0465e0f5dfaf27fbe23f7a711ca25e1462e1b3c..d745d86c6e9a6a9aaf79f7d204273b53143dbe83 100644 (file)
@@ -1,12 +1,21 @@
 USING: accessors arrays assocs combinators compiler.cfg.registers
-compiler.cfg.stacks.local kernel literals namespaces tools.test ;
-IN: compiler.cfg.stacks
+compiler.cfg.stacks compiler.cfg.stacks.local kernel literals namespaces
+tools.test ;
+IN: compiler.cfg.stacks.tests
 
-{ H{ { D -2 4 } { D -1 3 } { D -3 5 } } } [
-    {
-        ${ height-state initial-height-state }
-        ${ replace-mapping H{ } clone }
-    } [
-        { 3 4 5 } ds-store replace-mapping get
-    ] with-variables
+: test-init ( -- )
+    0 vreg-counter set-global
+    initial-height-state height-state set
+    H{ } clone replace-mapping set
+    H{ } clone locs>vregs set
+    H{ } clone local-peek-set set ;
+
+{
+    H{ { D 1 4 } { D 2 3 } { D 0 5 } }
+    { { 0 0 } { 0 0 } }
+} [
+    test-init
+    { 3 4 5 } ds-loc store-vregs
+    replace-mapping get
+    height-state get
 ] unit-test
index ebc5a096646849eade94a7d687d24b68a0d5acd7..201c328e5d7fb1dd59379a1c1be14338be6f3676 100644 (file)
@@ -3,7 +3,7 @@
 USING: accessors biassocs compiler.cfg compiler.cfg.registers
 compiler.cfg.stacks.finalize compiler.cfg.stacks.global
 compiler.cfg.stacks.height compiler.cfg.stacks.local compiler.cfg.utilities
-kernel math namespaces sequences ;
+fry kernel math namespaces sequences ;
 IN: compiler.cfg.stacks
 
 : begin-stack-analysis ( -- )
@@ -26,45 +26,39 @@ IN: compiler.cfg.stacks
         finalize-stack-shuffling
     } apply-passes ;
 
-: ds-drop ( -- ) -1 inc-d ;
+: ds-drop ( -- ) -1 <ds-loc> inc-stack ;
 
 : ds-peek ( -- vreg ) D 0 peek-loc ;
 
 : ds-pop ( -- vreg ) ds-peek ds-drop ;
 
 : ds-push ( vreg -- )
-    1 inc-d D 0 replace-loc ;
+    1 <ds-loc> inc-stack D 0 replace-loc ;
+
+: stack-locs ( loc-class n -- locs )
+    iota [ swap new swap >>n ] with map <reversed> ;
+
+: vregs>stack-locs ( loc-class vregs -- locs )
+    length stack-locs ;
 
 : ds-load ( n -- vregs )
-    dup 0 =
-    [ drop f ]
-    [ [ iota <reversed> [ <ds-loc> peek-loc ] map ] [ neg inc-d ] bi ] if ;
-
-: ds-store ( vregs -- )
-    [
-        <reversed>
-        [ length inc-d ]
-        [ [ <ds-loc> replace-loc ] each-index ] bi
-    ] unless-empty ;
-
-: rs-store ( vregs -- )
-    [
-        <reversed>
-        [ length inc-r ]
-        [ [ <rs-loc> replace-loc ] each-index ] bi
-    ] unless-empty ;
+    [ iota <reversed> [ <ds-loc> peek-loc ] map ]
+    [ neg <ds-loc> inc-stack ] bi ;
+
+: store-vregs ( vregs loc-class -- )
+    over vregs>stack-locs [ replace-loc ] 2each ;
 
 : (2inputs) ( -- vreg1 vreg2 )
     D 1 peek-loc D 0 peek-loc ;
 
 : 2inputs ( -- vreg1 vreg2 )
-    (2inputs) -2 inc-d ;
+    (2inputs) -2 <ds-loc> inc-stack ;
 
 : (3inputs) ( -- vreg1 vreg2 vreg3 )
     D 2 peek-loc D 1 peek-loc D 0 peek-loc ;
 
 : 3inputs ( -- vreg1 vreg2 vreg3 )
-    (3inputs) -3 inc-d ;
+    (3inputs) -3 <ds-loc> inc-stack ;
 
 : binary-op ( quot -- )
     [ 2inputs ] dip call ds-push ; inline