diff nworker.rhope @ 92:e73a93fb5de1

Beginning of port of compiler to itself, some bugfixes and a refcount optimization
author Mike Pavone <pavone@retrodev.com>
date Mon, 02 Aug 2010 00:58:55 -0400
parents bcdc326b3d6e
children 7428aa5d6ade
line wrap: on
line diff
--- a/nworker.rhope	Sat Jul 31 17:17:23 2010 -0400
+++ b/nworker.rhope	Mon Aug 02 00:58:55 2010 -0400
@@ -67,6 +67,7 @@
 
 Empty?@Condition Set[set:not empty,empty]
 {
+	Print["Empty?@Condition Set"]
 	[[set]Variables >>]First
 	{
 		not empty <- Yes
@@ -304,7 +305,12 @@
 
 Add Full Node@NWorker[worker,type,data,inputs,min inputs,outputs,min outputs:out,node index]
 {
-	out <- [worker]Nodes <<[[[worker]Nodes >>]Append[ [[NWorker Node[type,data,inputs,outputs]]Min Inputs <<[min inputs]]Min Outputs <<[min outputs] ]]
+	out <- [worker]Nodes <<[[[worker]Nodes >>]Append[ 
+		[[[NWorker Node[type,data,inputs,outputs]
+		]Min Inputs <<[min inputs]
+		]Min Outputs <<[min outputs]
+		]Wires To <<[List of Lists[[min inputs]+[1]]]
+	]]
 	node index <- [[worker]Nodes >>]Length
 }
 
@@ -678,7 +684,7 @@
 	
 	[("call","getfield","setfield")]Find[[node]Type >>]
 	{
-		out <- AddRef[ [[["__result_"]Append[[noderef]Index >>]]Append["_"]]Append[[noderef]IO Num >>] ]
+		maybe addref <- [[["__result_"]Append[[noderef]Index >>]]Append["_"]]Append[[noderef]IO Num >>]
 	}{
 		conditions <- [node]Conditions >>
 		
@@ -703,6 +709,16 @@
 			}
 		}
 	}
+
+	Val[maybe addref]
+	{
+		If[[Length[[[node]Wires From >>]Index[[noderef]IO Num >>]]] > [1]]
+		{
+			out <- AddRef[maybe addref]
+		}{
+			out <- Val[maybe addref]
+		}
+	}
 }
 
 Collect Input@NWorker[worker,nodeinput:out]
@@ -719,7 +735,7 @@
 			out <- Val[first]
 		}
 	}{
-		out <- No
+		out <- "Missing"
 	}
 }
 
@@ -928,22 +944,48 @@
 Release Var@NWorker[worker,func,name:out]
 {
 	//_result_index_ionum
+	Print[["Release Var@NWorker: "]Append[name]]
 	parts <- [name]Split["_"]
-	index <- <String@Whole Number[ [parts]Index[2] ]
-	io num <- <String@Whole Number[ [parts]Index[3] ]
+	index <- <String@Whole Number[ [parts]Index[3] ]
+	io num <- <String@Whole Number[ [parts]Index[4] ]
 	node <- [[worker]Nodes >>]Index[index]
-	do if <- If[[[node]Outputs >>] > [1]] {}
+	dests <- [[node]Wires From >>]Index[io num] {}
+	{ 
+		Print["oops"]
+		{ Pretty Print[node, ""] 
+		{ Pretty Print[parts, ""]}} }
+	If[[[dests]Length] = [1]]
 	{
-		,do if <- [[node]Conditions >>]Empty?
+		Print["Single dest, maybe release"]
+		{ Print[["Dest index: "]Append[dest index]] }
+		dest index <- [[dests]Index[0]]Index >>
+		dest node <- [[worker]Nodes >>]Index[dest index]
+		{ Print["got dest node"] }
+		
+		[[dest node]Conditions >>]For Backend
 		{
-			out <- [func]Release[name]
+			Print["dest has conditions"]
+			out <- [func]Do If[AndCond[NotCond[~], name],  [[func]Instruction Stream]Release[name]]
+			{ Print["got output"] }
+		}{
+			Print["No conditions on dest, no release needed"]
+			out <- func
 		}
-	}
+	}{
+		Print["Multiple (or zero) dests, definitely release"]
+		do if <- If[[[node]Outputs >>] > [1]] {}
+		{
+			do if <- [[node]Conditions >>]Empty? {}
+			{
+				out <- [func]Release[name]
+			}
+		}
 	
-	Val[do if]
-	{
-		stream <- [[func]Instruction Stream]Release[name]
-		out <- [func]Do If[name, stream]
+		Val[do if]
+		{
+			stream <- [[func]Instruction Stream]Release[name]
+			out <- [func]Do If[name, stream]
+		}
 	}
 }
 
@@ -1147,7 +1189,7 @@
 			init vars <- Concatenate[res vars, [with conds]No Release Results]
 			
 			func <- Fold["Set Null", Fold["Set Null", Fold[["Allocate Var"]Set Input[2, "Any Type"], ifunc, init vars], init vars], [worker]Outputs >>]
-			out <- [program]Store Function[Fold["Release", Fold[["Release Var"]Set Input[0, worker], final func, res vars], [worker]Inputs >>]]
+			out <- [program]Store Function[Fold["Release", Fold[["Release Var"]Set Input[0, with conds], final func, res vars], [worker]Inputs >>]]
 		}{
 			out <- Compile Foreign Stub[worker,[program]Link[[worker]Convention >>, [worker]Library >> ],name]
 		}