diff nworker.rhope @ 82:2e2e55fc12f9

Fix bug with conditionally assigning a literal or input to a named pipe
author Mike Pavone <pavone@retrodev.com>
date Tue, 27 Jul 2010 15:23:32 -0400
parents 0083b2f7b3c7
children 27bb051d631c
line wrap: on
line diff
--- a/nworker.rhope	Thu Jul 22 05:39:08 2010 +0000
+++ b/nworker.rhope	Tue Jul 27 15:23:32 2010 -0400
@@ -149,9 +149,9 @@
 {
 	Name
 	Convention
-	Inputs
+	Inputs
 	Min Inputs
-	Outputs
+	Outputs
 	Min Outputs
 	Is Method?
 }
@@ -184,10 +184,10 @@
 {
 	Type
 	Data
-	Inputs
+	Inputs
 	Min Inputs
 	Input Types
-	Outputs
+	Outputs
 	Min Outputs
 	Output Types
 	Wires From
@@ -196,8 +196,8 @@
 }
 
 Wire To@NWorker Node[node,from,output,pre input:out]
-{
-	existing cons <- [[node]Wires To >>]Index[input] {}
+{
+	existing cons <- [[node]Wires To >>]Index[input] {}
 	{ existing cons <- () }
 	input <- [pre input]+[1]
 	out <- [node]Wires To <<[
@@ -208,8 +208,8 @@
 }
 
 Wire From@NWorker Node[node,to,input,output:out]
-{
-	existing cons <- [[node]Wires From >>]Index[output] {}
+{
+	existing cons <- [[node]Wires From >>]Index[output] {}
 	{ exist cons <- () }
 	out <- [node]Wires From <<[
 		[[node]Wires From >>]Set[output,
@@ -264,9 +264,9 @@
 	out <- [[[[[[[[[[[Build["NWorker Node"]
 		]Type <<[type]
 		]Data <<[data]
-		]Inputs <<[inputs]
+		]Inputs <<[inputs]
 		]Min Inputs <<[inputs]
-		]Outputs <<[outputs]
+		]Outputs <<[outputs]
 		]Min Outputs <<[outputs]
 		]Wires From <<[List of Lists[outputs]]
 		]Wires To <<[List of Lists[[inputs]+[1]]]
@@ -300,8 +300,8 @@
 {
 	out <- [worker]Nodes <<[[[worker]Nodes >>]Append[NWorker Node[type,data,inputs,outputs]]]
 	node index <- [[worker]Nodes >>]Length
-}
-
+}
+
 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] ]]
@@ -358,7 +358,7 @@
 Infer Types Node[nodelist,node,index,prog,worker:out]
 {
 	If[[[node]Type >>] = ["const"]]
-	{
+	{
 		const type <- Type Of[[node]Data >>]
 		//Temporary hack
 		If[[const type] = ["Whole Number"]]
@@ -376,12 +376,12 @@
 					{ base <- "UInt" }
 					
 					outtype <- Type Instance[[base]Append[ [[node]Data >>]Size >> ]]
-				}{
-					If[[const type] = ["Worker Literal"]]
-					{
-						outtype <- Type Instance["Worker"]
+				}{
+					If[[const type] = ["Worker Literal"]]
+					{
+						outtype <- Type Instance["Worker"]
 					}{
-						outtype <- Type Instance[const type]
+						outtype <- Type Instance[const type]
 					}
 				}
 			}
@@ -635,44 +635,56 @@
 Format Input@NWorker[worker,noderef:out]
 {
 	node <- [[worker]Nodes >>]Index[[noderef]Index >>]
+	
 	[("call","getfield","setfield")]Find[[node]Type >>]
 	{
 		out <- AddRef[ [[["__result_"]Append[[noderef]Index >>]]Append["_"]]Append[[noderef]IO Num >>] ]
 	}{
+		conditions <- [node]Conditions >>
+		
 		If[[[node]Type >>] = ["input"]]
 		{
 			input name <- [[worker]Inputs >>]Index[ [node]Data >> ]
-			out <- AddRef[input name] 
+			[conditions]For Backend
+			{
+				out <- AddRef[ [[["__result_"]Append[[noderef]Index >>]]Append["_"]]Append[[noderef]IO Num >>] ]
+			}{
+				out <- AddRef[input name] 
+			}
 		}{
 			If[[[node]Type >>] = ["const"]]
 			{
-				If[[Type Of[[node]Data >>]] = ["Type Instance"]]
+				[conditions]For Backend
 				{
-					//TODO: Support parametric types
-					datstring <- [[node]Data >>]Name >>
+					out <- AddRef[ [[["__result_"]Append[[noderef]Index >>]]Append["_"]]Append[[noderef]IO Num >>] ]
 				}{
-					If[[Type Of[[node]Data >>]] = ["Machine Integer"]]
+					If[[Type Of[[node]Data >>]] = ["Type Instance"]]
 					{
-						If[[[node]Data >>]Signed? >>]
-						{ s <- "i" }
-						{ s <- "u" }
-						datstring <- [[[[node]Data >>]Value >>]Append[s]]Append[[[node]Data >>]Size >>]
-					}{
-						If[[Type Of[[node]Data >>]] = ["Worker Literal"]]
-						{
-							If[[[[[node]Data >>]Args >>]Length] > [0]]
-							{
-								datstring <- [[["Arg "]Append[[noderef]Index >>]]Append[" "]]Append[[worker]Name >>]
-							}{
-								datstring <- [[node]Data >>]Name >>
-							}
+						//TODO: Support parametric types
+						datstring <- [[node]Data >>]Name >>
+					}{
+						If[[Type Of[[node]Data >>]] = ["Machine Integer"]]
+						{
+							If[[[node]Data >>]Signed? >>]
+							{ s <- "i" }
+							{ s <- "u" }
+							datstring <- [[[[node]Data >>]Value >>]Append[s]]Append[[[node]Data >>]Size >>]
 						}{
-							datstring <- [node]Data >>
+							If[[Type Of[[node]Data >>]] = ["Worker Literal"]]
+							{
+								If[[[[[node]Data >>]Args >>]Length] > [0]]
+								{
+									datstring <- [[["Arg "]Append[[noderef]Index >>]]Append[" "]]Append[[worker]Name >>]
+								}{
+									datstring <- [[node]Data >>]Name >>
+								}
+							}{
+								datstring <- [node]Data >>
+							}
 						}
 					}
+					out <- Constant[[[Type Of[[node]Data >>]]Append["_"]]Append[datstring]]
 				}
-				out <- Constant[[[Type Of[[node]Data >>]]Append["_"]]Append[datstring]]
-					
 			}
 		}
 	}
@@ -729,41 +741,41 @@
 		]Move[Result[num], out var]
 	*/
 	out <- [func]Move[Result[num], out var]	
-}
-
-Save Maybe Result[func,num,node index:out]
-{
-	out var <- [[["__result_"]Append[node index]]Append["_"]]Append[num]
-	out <- [func]Move[Check Result[num], out var]
-}
-
-Max Used Output[node,cur:out]
-{
-	If[[cur] < [0]]
-	{
-		out <- cur
-	}{
-		[[[node]Wires From >>]Index[cur]]Index[0]
-		{
-			out <- cur
-		}{
-			out <- Max Used Output[node, [cur]-[1]]
-		}
-	}
+}
+
+Save Maybe Result[func,num,node index:out]
+{
+	out var <- [[["__result_"]Append[node index]]Append["_"]]Append[num]
+	out <- [func]Move[Check Result[num], out var]
+}
+
+Max Used Output[node,cur:out]
+{
+	If[[cur] < [0]]
+	{
+		out <- cur
+	}{
+		[[[node]Wires From >>]Index[cur]]Index[0]
+		{
+			out <- cur
+		}{
+			out <- Max Used Output[node, [cur]-[1]]
+		}
+	}
 }
 
 Compile Call Node[node,program,func,inputs,node index:out]
 {
 	If[[[node]Type >>] = ["getfield"]]
 	{
-		with call <- [func]Get Field Call[[node]Data >>, [inputs]Index[0]]
-		save outs <- [node]Outputs >>
+		with call <- [func]Get Field Call[[node]Data >>, [inputs]Index[0]]
+		save outs <- [node]Outputs >>
 		out <- Val[after save]
 	}{
 		If[[[node]Type >>] = ["setfield"]]
 		{
-			with call <- [func]Set Field Call[[node]Data >>, [inputs]Index[0], [inputs]Index[1]]
-			save outs <- [node]Outputs >>
+			with call <- [func]Set Field Call[[node]Data >>, [inputs]Index[0], [inputs]Index[1]]
+			save outs <- [node]Outputs >>
 			out <- Val[after save]
 		}{
 			[program]Method?[[[node]Data >>]Name >>]
@@ -771,21 +783,21 @@
 				with call <- [func]Method Call[[[node]Data >>]Name >>, inputs]
 			}{
 				with call <- [func]Call[[[node]Data >>]Name >>, inputs]
-			}
-			first unused <- [Max Used Output[node, [[node]Outputs >>]-[1]]]+[1]
-			If[[first unused] > [[node]Min Outputs >>]]
-			{
-				save outs <- [node]Min Outputs >>
-				after maybe <- Fold[["Save Maybe Result"]Set Input[2, node index], after save, Range[save outs, first unused]]
-			}{
-				save outs <- Val[first unused]
-				after maybe <- Val[after save]
-			}
-			If[[first unused] < [[node]Outputs >>]]
-			{
-				out <- [after maybe]Discard Outputs[first unused]
-			}{
-				out <- Val[after maybe]
+			}
+			first unused <- [Max Used Output[node, [[node]Outputs >>]-[1]]]+[1]
+			If[[first unused] > [[node]Min Outputs >>]]
+			{
+				save outs <- [node]Min Outputs >>
+				after maybe <- Fold[["Save Maybe Result"]Set Input[2, node index], after save, Range[save outs, first unused]]
+			}{
+				save outs <- Val[first unused]
+				after maybe <- Val[after save]
+			}
+			If[[first unused] < [[node]Outputs >>]]
+			{
+				out <- [after maybe]Discard Outputs[first unused]
+			}{
+				out <- Val[after maybe]
 			}
 		}
 	}
@@ -794,9 +806,10 @@
 
 Compile Node@NWorker[worker,program,func,nodes,current:out,out worker]
 {
+	Print[[[["Compile node: "]Append[[node]Type >>]]Append[", "]]Append[node index]]
 	node index <- [nodes]Index[current]
 	node <- [[worker]Nodes >>]Index[node index]
-	conditions <- [worker]Collect Conditions[node]
+	conditions <- [node]Conditions >>
 	[("call","getfield","setfield")]Find[[node]Type >>]
 	{
 		inputs <- [worker]Collect Inputs[node]
@@ -837,39 +850,71 @@
 						{ s <- "i" }
 						{ s <- "u" }
 						datstring <- [[[[node]Data >>]Value >>]Append[s]]Append[[[node]Data >>]Size >>]
-					}{
-						If[[Type Of[[node]Data >>]] = ["Worker Literal"]]
-						{
-							If[[[[[node]Data >>]Args >>]Length] > [0]]
-							{
-								datstring <- [[["Arg "]Append[node index]]Append[" "]]Append[[worker]Name >>]
-							}{
-								datstring <- [[node]Data >>]Name >>
-							}
+					}{
+						If[[Type Of[[node]Data >>]] = ["Worker Literal"]]
+						{
+							If[[[[[node]Data >>]Args >>]Length] > [0]]
+							{
+								datstring <- [[["Arg "]Append[node index]]Append[" "]]Append[[worker]Name >>]
+							}{
+								datstring <- [[node]Data >>]Name >>
+							}
 						}{
-							datstring <- [node]Data >>
+							datstring <- [node]Data >>
 						}
 					}
 				}
-				nfunc <- [func]Register Constant[[[Type Of[[node]Data >>]]Append["_"]]Append[datstring], [node]Data >>]
+				constname <- [[Type Of[[node]Data >>]]Append["_"]]Append[datstring]
+				withconst <- [func]Register Constant[constname, [node]Data >>]
+				[conditions]For Backend
+				{
+					stream <- [[withconst]Instruction Stream
+						]Move[Constant[constname], [[["__result_"]Append[node index]]Append["_"]]Append[0]]
+					nfunc <- [withconst]Do If[~, stream]
+				}{
+					nfunc <- Val[withconst]
+				}
 			}{
-				nfunc <- Val[func]
+				[conditions]For Backend
+				{
+					input name <- [[worker]Inputs >>]Index[ [node]Data >> ]
+					stream <- [[func]Instruction Stream
+						]Move[input name, [[["__result_"]Append[node index]]Append["_"]]Append[0]]
+					nfunc <- [func]Do If[~, stream]
+				}{
+					nfunc <- Val[func]
+				}
 			}
 			
 		}
 	}
-	If[[[node]Outputs >>] = [0]]
-	{
-		nworker <- Val[worker]
-	}{
-		nworker <- [worker]Nodes <<[ [[worker]Nodes >>]Set[node index, [node]Conditions <<[conditions]] ]
-	}
 	[nodes]Next[current]
 	{
-		out,out worker <- [nworker]Compile Node[program,nfunc,nodes,~]
+		out,out worker <- [worker]Compile Node[program,nfunc,nodes,~]
 	}{
 		out <- Val[nfunc]
-		out worker <- Val[nworker]
+		out worker <- Val[worker]
+	}
+}
+
+Save Node Conditions@NWorker[worker,node index:out]
+{
+	node <- [[worker]Nodes >>]Index[node index]
+	conditions <- [worker]Collect Conditions[node]
+	out <- [worker]Nodes <<[ [[worker]Nodes >>]Set[node index, [node]Conditions <<[conditions]] ]
+	
+}
+
+Save Group Conditions@NWorker[worker, groups,current:out]
+{
+	nodes <- [groups]Index[current]
+	nworker <- Fold["Save Node Conditions", worker, nodes]
+
+	[groups]Next[current]
+	{
+		out <- [nworker]Save Group Conditions[groups,~]
+	}{
+		out <- Val[nworker]
 	}
 }
 
@@ -922,12 +967,12 @@
 Node Result Vars[vars,node,index:out]
 {
 	[("call","getfield","setfield")]Find[[node]Type >>]
-	{
-		If[[[node]Type >>]=["call"]]
-		{
-			save outs <- [Max Used Output[node, [[node]Outputs >>]-[1]]]+[1]
-		}{
-			save outs <- [node]Outputs >>
+	{
+		If[[[node]Type >>]=["call"]]
+		{
+			save outs <- [Max Used Output[node, [[node]Outputs >>]-[1]]]+[1]
+		}{
+			save outs <- [node]Outputs >>
 		}
 		out <- Fold[["Result Var"]Set Input[2, index], vars, Range[0, save outs]]
 	}{
@@ -940,6 +985,30 @@
 	out <- Fold["Node Result Vars", (), [worker]Nodes >>]
 }
 
+_No Release[vars,node,index,worker:out]
+{
+	Print[["_No Release"]Append[[node]Type >>]]
+	[("const","input")]Find[[node]Type >>]
+	{
+		[[node]Conditions >>]For Backend
+		{
+			Print[["Const or input with conditions: "]Append[index]]
+			out <- Result Var[vars, 0, index]
+		}{	
+			Print[["Const or input with no conditions: "]Append[index]]
+			out <- vars
+		}
+	}{
+		Print[["Other node type: "]Append[index]]
+		out <- vars
+	}
+}
+
+No Release Results@NWorker[worker:out]
+{
+	out <- Fold[["_No Release"]Set Input[3, worker], (), [worker]Nodes >>]
+}
+
 Make Basic Type[type:out]
 {
 	out <- [Type Instance[[type]Name >>]]Params <<[ [type]Params >> ]
@@ -1071,7 +1140,8 @@
 }		
 
 Compile Worker@NWorker[worker,program,name:out]
-{
+{	
+	Print[["Compile Worker: "]Append[[worker]Name >>]]
 	If[[worker]Builtin? >>]
 	{
 		out <- program
@@ -1080,16 +1150,20 @@
 		{	
 			ifunc <- Fold["Set Output Type", Fold["Set Input Type", [program]Create Function[name,[worker]Inputs >>, [worker]Outputs >>, [worker]Convention >>], [worker]Input Types >>], [worker]Output Types >>]
 			
-			res vars <- [worker]Result Vars
-			func <- Fold["Set Null", Fold["Set Null", Fold[["Allocate Var"]Set Input[2, "Any Type"], ifunc, res vars], res vars], [worker]Outputs >>]
+			
 			
 			groups <- [worker]Dependency Groups
 			[groups]First
 			{
-				final func <- [worker]Compile Group[program,func,groups, ~]
+				with conds <- [worker]Save Group Conditions[groups, ~]
+				final func <- [with conds]Compile Group[program,func,groups, ~]
 			}{
 				final func <- Val[func]
 			}
+			res vars <- [worker]Result Vars
+			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 <- Compile Foreign Stub[worker,[program]Link[[worker]Convention >>, [worker]Library >> ],name]
@@ -1162,12 +1236,12 @@
 }
 
 _Compile Blueprint Methods[type,junk,name:out]
-{
-	If[[[name]=["Call"]] And [[[type]Name >>] = ["Worker"]]]
-	{
-		out <- type
+{
+	If[[[name]=["Call"]] And [[[type]Name >>] = ["Worker"]]]
+	{
+		out <- type
 	}{
-		out <- [type]Add Method[name]
+		out <- [type]Add Method[name]
 	}
 }
 
@@ -1358,18 +1432,19 @@
 }
 
 Register Worker@NProgram[prog, name, convention, inputs, outputs: out]
-{
-	[[prog]Worker Refs >>]Index[name]
-	{
-		ref <- [[[[~]Inputs <<[ Max[[~]Inputs >>, inputs] ]
-		]Min Inputs <<[ Min[[~]Min Inputs >>, inputs] ]
-		]Outputs <<[ Max[[~]Outputs >>, outputs] ]
-		]Min Outputs <<[ Min[[~]Min Outputs >>, outputs] ]
-	}{
-		ref <- Worker Ref[name, convention, inputs, outputs, No]
+{
+	[[prog]Worker Refs >>]Index[name]
+	{
+		ref <- [[[[~]Inputs <<[ Max[[~]Inputs >>, inputs] ]
+		]Min Inputs <<[ Min[[~]Min Inputs >>, inputs] ]
+		]Outputs <<[ Max[[~]Outputs >>, outputs] ]
+		]Min Outputs <<[ Min[[~]Min Outputs >>, outputs] ]
+	}{
+		ref <- Worker Ref[name, convention, inputs, outputs, No]
 	}
 	after reg <- [prog]Worker Refs <<[ 
-		[ [prog]Worker Refs >> ]Set[name, ref]
	]
+		[ [prog]Worker Refs >> ]Set[name, ref]
+	]
 
 	parts <- [name]Split["@"]
 	[parts]Index[1]
@@ -1385,7 +1460,7 @@
 	registered <- [[[[[[prog]Register Worker["Print", "rhope", 1, 1]
 	]Register Worker["If@Boolean", "rhope", 1, 2]
 	]Register Worker["Build", "rhope", 1, 1]
-	]Register Worker["Blueprint Of", "rhope", 1, 1]
+	]Register Worker["Blueprint Of", "rhope", 1, 1]
 	]Register Worker["Call@Worker", "rhope", 1, 2] //We're using 2 because we need to assume that the outputs are conditional
 	]Register Number Methods
 	
@@ -1416,13 +1491,13 @@
 		]Input Types <<[ [()]Append[Type Instance["Any Type"]]]
 		]Outputs <<[("type")]
 		]Output Types <<[ [()]Append[Type Instance["Blueprint"]]]
-		]Builtin? <<[Yes]]
-	]Bind Worker["Call@Worker",
-		[[[[[NWorker["rhope"]
-		]Inputs <<[("worker")]
-		]Input Types <<[ [()]Append[Type Instance["Worker"]] ]
-		]Outputs <<[("ret1","ret2")]
-		]Output Types <<[ [[()]Append[Type Instance["Any Type"]]]Append[Type Instance["Any Type"]] ]
+		]Builtin? <<[Yes]]
+	]Bind Worker["Call@Worker",
+		[[[[[NWorker["rhope"]
+		]Inputs <<[("worker")]
+		]Input Types <<[ [()]Append[Type Instance["Worker"]] ]
+		]Outputs <<[("ret1","ret2")]
+		]Output Types <<[ [[()]Append[Type Instance["Any Type"]]]Append[Type Instance["Any Type"]] ]
 		]Builtin? << [Yes]]
 }