ddaf1d0a5057 — Leonard Ritter 26 days ago
* UVM: improved bind points
2 files changed, 75 insertions(+), 94 deletions(-)

M testing/BDD.sc
M testing/test_node2.sc
M testing/BDD.sc +3 -18
@@ 283,26 283,11 @@ for x3 x2 x1 in (dim 2 2 2)
     print x1 x2 x3 a b
 
 do
-    b1 := (& (& (& (T 'a) (T 'b)) (T 'c)) (T 'd))
-    b2 := (& (& (& (T 'a) (T 'b)) (T 'c)) (~ (T 'd)))
+    b1 := (T 'a)
+    b2 := (& (T 'b) (T 'c))
 
     print
-        b1 | b2
-    print
-        b1 & (~ b2)
-    print
-        b2 & (~ b1)
-
-do
-    b1 := (& (& (& (T 'a) (T 'b)) (T 'c)) (T 'd))
-    b2 := (& (& (T 'a) (T 'b)) (T 'c))
-    m :=
-        b1 | b2
-    print "m" m
-    print "b1"
-        T (copy b2) 0 (copy b1)
-    print "b2"
-        b2 | (~ b1)
+        b2 | b1
 
 #
     if a

          
M testing/test_node2.sc +72 -76
@@ 357,6 357,7 @@ do
 
     2d defnode              super       signature   eval        partial-eval    canonicalize
         ------------------------------------------------------------------------------------
+        TypeVoid            OpType      ""          -           -               -
         TypeInt             OpType      "ub"        -           -               -
         TypeBool            OpType      ""          -           -               -
         TypeReal            OpType      "u"         -           -               -

          
@@ 375,7 376,6 @@ do
         Output              OpPure      "O"         -           -               -
         State               OpPure      "@@"        -           -               -
         Defined             OpPure      "@"         -           -               -
-        Then                OpControl   "@@"        evalthen    -               -
         Merge               OpControl   "@@"        evalmerge   partmerge       commutative
         Select              OpControl   "@@@"       -           -               -
         Bind                OpInstr     "@@"        evalbind    -               -

          
@@ 493,6 493,7 @@ fn idstr (id)
 
 struct Module
     nodes : (Array RcNode)
+    roots : (Set Id)
     # maps node specification to id
     rnodes :
         Map RcNode Id

          
@@ 522,7 523,7 @@ struct Module
         let newid = ((countof self.nodes) as Id)
         'append self.nodes (copy node)
         'set self.rnodes node newid
-        newid
+        copy newid
 
     fn... nodeId
     case (self, node : RcNode)

          
@@ 553,6 554,10 @@ struct Module
         'nodeId self
             Node Op.TypeBool NoType
 
+    fn voidType (self)
+        'nodeId self
+            Node Op.TypeVoid NoType
+
     fn... vectorType (self, compId : Id, count : i32)
         'nodeId self
             Node Op.TypeVector NoType compId (count as u32)

          
@@ 756,6 761,10 @@ struct Module
                 genvisitor
                     visit-id = visit
                 \ self node remap
+        local newroots : (Set Id)
+        for id in self.roots
+            'insert newroots (remap @ id)
+        self.roots = newroots
         self.rnodes_valid = false
         'clear self.select_cache
 

          
@@ 783,22 792,16 @@ struct Module
                 \ self node queue visited
         queue
 
-    fn... reachable-indegrees (self, root : Id = NoId)
+    fn... reachable-indegrees (self)
         let nodes = self.nodes
         let count = ((countof nodes) as u32)
         local visited : (Array i32)
         'resize visited count 0
 
-        # find roots
         local queue : (Array Id)
-        if (root != NoId)
-            visited @ root = 1
-            'append queue root
-        else
-            for id node in (enumerate nodes Id)
-                if (node.opCode == Op.Bind)
-                    visited @ id = 1
-                    'append queue id
+        for id in self.roots
+            visited @ id = 1
+            'append queue id
         # tag all reachable nodes
         for nodeid in queue
             node := nodes @ nodeid

          
@@ 905,10 908,13 @@ struct Module
                     'append result s
                 ...
         node := self.nodes @ nodeId
-        write (idstr nodeId)
-        if (node.typeId != NoType)
-            write " : " (idstr node.typeId)
-        write " = "
+        if (nodeId in self.roots)
+            write "(" (idstr nodeId) ") "
+        else
+            write (idstr nodeId)
+            if (node.typeId != NoType)
+                write " : " (idstr node.typeId)
+            write " = "
         write (repr node.opCode)
         inline write (result ...)
             va-map

          
@@ 1011,11 1017,24 @@ struct Module
         va-switch-case OpDefs Op 'constant? ('getOp self id)
             inline (cls) false
 
+    fn isundef? (self id)
+        ('getOp self id) == Op.Undefined
+
+    fn isthen? (self id)
+        and (('getOp self id) == Op.Select)
+            or
+                isundef? self ('getArg self id 1)
+                isundef? self ('getArg self id 2)
+
     fn unthen (self id)
-        if (('getOp self id) == Op.Then)
-            _ ('getArg self id 0) ('getArg self id 1)
-        else
-            _ ('constBool self true) id
+        returning Id Id
+        loop (cond id = ('constBool self true) id)
+            if (('getOp self id) == Op.Select)
+                if (isundef? self ('getArg self id 1))
+                    repeat ('and self cond ('getArg self id 0)) ('getArg self id 2)
+                elseif (isundef? self ('getArg self id 2))
+                    repeat ('and self cond ('not self ('getArg self id 0))) ('getArg self id 1)
+            break (copy cond) (copy id)
 
     fn... getbool (self, id : Id)
         """"returns bool as signed integer; zero = undefined

          
@@ 1060,7 1079,7 @@ struct Module
                 if (id != NoId)
                     try
                         let newid = ('get substmap id)
-                        assert (id != newid) (.. (idstr id) " == " (idstr newid) " -> " (('tostring self newid) as string))
+                        assert (id != newid)
                         id = newid
                     else;
                 ;

          
@@ 1100,6 1119,8 @@ struct Module
     select =
         fn... "select" (self, cond : Id, fvalue : Id, tvalue : Id)
             returning Id
+            if ('isundef? self cond)
+                return ('undef self ('getType self tvalue))
             key := (tupleof cond fvalue tvalue)
             try
                 return

          
@@ 1108,9 1129,6 @@ struct Module
             else;
             let result =
                 loop (cond fvalue tvalue = cond fvalue tvalue)
-                    let c cond = ('unthen self cond)
-                    let cf fvalue = ('unthen self fvalue)
-                    let ct tvalue = ('unthen self tvalue)
                     if (('getOp self cond) == Op.Select)
                         # (select (select a b c) d e) -> (select a (select b d e) (select c d e))
                         let u v w =

          
@@ 1118,9 1136,7 @@ struct Module
                             'getArg self cond 1
                             'getArg self cond 2
                         repeat
-                            'then self
-                                'and self c ('and self cf ct)
-                                u
+                            u
                             this-function self v fvalue tvalue
                             this-function self w fvalue tvalue
                     else

          
@@ 1146,13 1162,9 @@ struct Module
                                 elseif ((fvalue == constfalse) & (tvalue == consttrue))
                                     copy cond
                                 else
-                                    vvv copy
                                     'nodeId self
                                         Node Op.Select ('getType self tvalue) cond fvalue tvalue
-                        break
-                            'then self
-                                'and self c ('and self cf ct)
-                                stmt
+                        break stmt
             'set self.select_cache key result
             result
 

          
@@ 1162,6 1174,11 @@ struct Module
             let c2 value2 = ('unthen self value2)
             let lcond1 = ('and self c1 ('not self c2))
             let lcond2 = ('and self ('not self c1) c2)
+            print ('tostring self c1)
+            print ('tostring self value1)
+            print ('tostring self c2)
+            print ('tostring self value2)
+            print;
             'then self
                 'or self c1 c2
                 if (('getbool self lcond1) >= 0)

          
@@ 1169,36 1186,28 @@ struct Module
                 else
                     'select self lcond2 value1 value2
 
+    subbind =
+        fn... "subbind" (self, target : Id, source : Id)
+            returning Id
+            if ('isundef? self source)
+                return ('undef self ('voidType self))
+            elseif (('getOp self source) == Op.Select)
+                'select self ('getArg self source 0)
+                    this-function self target ('getArg self source 1)
+                    this-function self target ('getArg self source 2)
+            else
+                'nodeId self
+                    Node Op.Bind ('voidType self) target source
+
     bind =
         fn... "bind" (self, target : Id, source : Id)
-            'nodeId self
-                Node Op.Bind NoType target source
-
+            let id = ('subbind self target source)
+            if (not ('isundef? self source))
+                'insert self.roots id
+            ;
     then =
         fn... "then" (self, cond : Id, value : Id)
-            returning Id
-            if (cond == value)
-                return value
-            #'select cond ('undef ('typeof value)
-            let c1 cond = ('unthen self cond)
-            let c2 value = ('unthen self value)
-            let value =
-                if (not ('isconstant? self cond))
-                    let consttrue = ('constBool self true)
-                    local substmap : (Map Id Id)
-                    'set substmap cond consttrue
-                    'subst self value substmap
-                else value
-            let cond = ('and self ('and self c1 cond) c2)
-            let b = ('getbool self cond)
-            if (b > 0) (copy value)
-            elseif (b < 0)
-                vvv copy
-                'undef self ('getType self value)
-            else
-                vvv copy
-                'nodeId self
-                    Node Op.Then ('getType self value) cond value
+            'select self cond ('undef self ('getType self value)) value
 
     undef =
         fn... "undef" (self, typeId : Id)

          
@@ 1206,10 1215,8 @@ struct Module
                 Node Op.Undefined typeId
 
     else =
-        fn... "else" (self, condition : Id, value : Id)
-            'then self
-                'not self condition
-                value
+        fn... "else" (self, cond : Id, value : Id)
+            'select self cond value ('undef self ('getType self value))
 
     or =
         fn... "or" (self, value1 : Id, value2 : Id)

          
@@ 1226,16 1233,7 @@ struct Module
 
     not =
         fn... "not" (self, _value : Id)
-            let cond value = ('unthen self _value)
-            let b = ('getbool self value)
-            if (b > 0)
-                'then self cond
-                    'constBool self false
-            elseif (b < 0)
-                'then self cond
-                    'constBool self true
-            else
-                'select self _value ('constBool self true) ('constBool self false)
+            'select self _value ('constBool self true) ('constBool self false)
 
 ################################################################################
 

          
@@ 1564,8 1562,6 @@ static-if main-module?
 
         let string =
             stringType;
-        let bangtype =
-            tupleType;
         let inttype =
             integerType 32 true
         let readline =

          
@@ 1582,13 1578,13 @@ static-if main-module?
         let exit? =
             equal (constString "\n") readline
 
-        #bind exit
+        bind exit
             then exit? (constInt inttype 0)
 
         bind prompt
             then (or setup exit?) (constString "> ")
 
-        #bind stdout
+        bind stdout
             merge
                 then exit? (constString "exiting...\n")
                 else exit? readline