diff nworker.rhope @ 49:3e20ed8959c4

Added initial FFI implementation, Array type and 64-bit integers
author Mike Pavone <pavone@retrodev.com>
date Thu, 08 Apr 2010 01:02:18 -0400
parents a24eb366195c
children 689fb73e7612
line wrap: on
line diff
--- a/nworker.rhope	Tue Mar 02 00:18:49 2010 -0500
+++ b/nworker.rhope	Thu Apr 08 01:02:18 2010 -0400
@@ -295,12 +295,13 @@
 	NodeResults
 	Free Temps
 	Name
-	Builtin?
+	Builtin?
+	Library
 }
 
 NWorker[convention:out]
 {
-	out <- [[[[[[[[Build["NWorker"]]Convention <<[convention]]Nodes <<[()]]Inputs <<[()]]Outputs <<[()]]Input Types <<[()]]Output Types <<[()]]Name <<["Anonymous"]]Builtin? <<[No]
+	out <- [[[[[[[[[Build["NWorker"]]Convention <<[convention]]Nodes <<[()]]Inputs <<[()]]Outputs <<[()]]Input Types <<[()]]Output Types <<[()]]Name <<["Anonymous"]]Builtin? <<[No]]Library << [""]
 }
 
 Add Node@NWorker[worker,type,data,inputs,outputs:out,node index]
@@ -731,8 +732,12 @@
 				with call <- [func]Method Call[[[node]Data >>]Name >>, inputs]
 				{ Print["Method Call done"] }
 			}{
-				Print["Function!"]
-				with call <- [func]Call[[[node]Data >>]Name >>, inputs]
+				Print["Function!"]
+				{
+					Pretty Print[inputs, ""]
+					{ with call <- [func]Call[[[node]Data >>]Name >>, inputs]}
+				}
+				
 			}
 			}
 		}
@@ -762,13 +767,16 @@
 		nstream <- Compile Call Node[node, program, stream, inputs, node index]
 	}{
 		If[[[node]Type >>] = ["output"]]
-		{
+		{
+			Print["Compiling output node"]
 			inputs <- [worker]Collect Inputs[node]
 			[conditions]For Backend
-			{
+			{
+				Print["has conditions"]
 				stream <- [func]Instruction Stream
 				nfunc <- [func]Do If[~, nstream]
-			}{
+			}{
+				Print["no conditions"]
 				stream <- Val[func]
 				nfunc <- Val[nstream]
 			}
@@ -865,29 +873,171 @@
 Result Vars@NWorker[worker:out]
 {
 	out <- Fold["Node Result Vars", (), [worker]Nodes >>]
-}
+}
+
+Make Basic Type[type:out]
+{
+	out <- [Type Instance[[type]Name >>]]Params <<[ [type]Params >> ]
+}
+
+FInputs[ifunc, input type, index, inputs:out]
+{	
+	func <- [ifunc]Set Input Type[Make Basic Type[input type], index]
+	name <- [inputs]Index[index]
+	Print[["FInputs: "]Append[index]]
+	Pretty Print[input type, ""]
+	{
+	If[[[input type]Variant >>] = ["Naked"]]
+	{
+		
+		naked <- [" naked"]Append[name]
+		
+		out <- [[[func]Allocate Var[naked, input type]
+		]Unbox[name, naked]
+		]Release[name]
+	}{
+		If[[input type]Mutable? >>]
+		{
+			name <- [inputs]Index[index]
+			copied <- [func]Copy[name, name]
+			
+		}{
+			copied <- Val[func]
+		}
+		If[[[input type]Variant >>] = ["Raw Pointer"]]
+		{
+			raw <- [" raw"]Append[name]
+			If[[[input type]Name >>]=["Array"]]
+			{
+				
+				out <- [[copied]Allocate Var[raw, input type]
+				]Array Raw Pointer[name, raw]
+			}{
+				out <- [[copied]Allocate Var[raw, input type]
+				]Get Raw Pointer[name, raw]
+			}
+		}{
+			out <- Val[copied]
+		}
+	}
+	}
+}
+
+FParams[input:out]
+{
+	iname <- [input]Index[0]
+	type <- [input]Index[1]
+	If[[[type]Variant >>] = ["Naked"]]
+	{ out <- [" naked"]Append[iname] }
+	{ 
+		If[[[type]Variant >>] = ["Raw Pointer"]]
+		{ out <- [" raw"]Append[iname] }
+		{ out <- Val[iname] }
+	}
+}
+_Return Param[outputs, inputs, input types, index:out,none]
+{
+	Print[["_Return Param: "]Append[output]]
+	output <- [outputs]Index[index]
+	[inputs]Find[output]
+	{
+		If[[[input types]Index[~]]Mutable? >>]	
+		{
+			,none <- [outputs]Next[index]
+			{
+				out,none <- _Return Param[outputs, index, input types, ~]
+			}
+		} { 
+			out <- index
+		}
+	}{
+		out <- index
+	}
+}
+
+Return Param[outputs, inputs, input types:out,none]
+{
+	Print["Return Param"]
+	,none <- [outputs]First
+	{ out,none <- _Return Param[outputs, inputs, input types, ~] }
+	
+}
+
+Save Foreign Result[func, output, index, output types, inputs, input types:out]
+{
+	type <- [output types]Index[index]
+	If[[[type]Variant >>] = ["Naked"]]
+	{
+		out <- [func]Box[[" naked"]Append[output], output, type]
+	}{
+		[inputs]Find[output]
+		{
+			If[[[input types]Index[~]]Mutable? >>]
+			{ 
+				out <- [func]Move[output, Output[output]]
+			}{
+				out <- func
+			}
+		}{	
+			out <- func
+		}
+	}
+}
+
+Compile Foreign Stub[worker,program,name:out]
+{
+	Print[["Compiling FFI stub for "]Append[name]]
+	ifunc <- [[program]Create Function[name, [worker]Inputs >>, [worker]Outputs >>, "rhope"]
+	]Output Types <<[Map[[worker]Output Types >>, "Make Basic Type"]]
+	
+	rp num <- Return Param[[worker]Outputs >>, [worker]Inputs >>, [worker]Input Types >>]
+	{
+		rbase <- [[worker]Outputs >>]Index[rp num]
+		If[[[[[worker]Output Types >>]Index[rp num]]Variant >>] = ["Naked"]]
+		{
+			rparam <- [" naked"]Append[rbase]
+			rfunc <- [ifunc]Allocate Var[rparam, [[worker]Output Types >>]Index[rp num]]
+		}{
+			rparam <- Val[rbase]
+			rfunc <- Val[ifunc]
+		}
+	}{
+		rparam <- ""
+		rfunc <- Val[ifunc]
+	}
+	
+	Fold[["FInputs"]Set Input[3, [worker]Inputs >>], rfunc, [worker]Input Types >>]
+	{ [~]Call Foreign[name, [worker]Convention >>, Map[Zip[[worker]Inputs >>, [worker]Input Types >>], "FParams"], rparam]
+	{ Fold[[[["Save Foreign Result"]Set Input[3, [worker]Output Types >>]]Set Input[4, [worker]Inputs >>]]Set Input[5, [worker]Input Types >>], ~, [worker]Outputs >>]
+	{ out <- [program]Store Function[~] }}}
+}		
 
 Compile Worker@NWorker[worker,program,name:out]
 {
 	If[[worker]Builtin? >>]
 	{
 		out <- program
-	}{
-		Print[["Compiling: "]Append[name]]
-		{
-		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, ~]
-		}{
-			final func <- Val[func]
-		}
-		out <- [program]Store Function[Fold["Release", Fold[["Release Var"]Set Input[0, worker], final func, res vars], [worker]Inputs >>]]
+	}{
+		If[[[worker]Library >>] = [""]]
+		{	
+			Print[["Compiling: "]Append[name]]
+			{
+			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, ~]
+			}{
+				final func <- Val[func]
+			}
+			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]
 		}
 	}
 }
@@ -1116,8 +1266,10 @@
 	after bind <- [prog]Workers << [ [[prog]Workers >>]Set[name, [worker]Name <<[name]] ]
 	parts <- [name]Split["@"]
 	[parts]Index[1]
-	{
-		out <- [after bind]Blueprints <<[ [[after bind]Blueprints >>]Set[~, [[[after bind]Blueprints >>]Index[~]]Add Method[[parts]Index[0]] ] ]
+	{
+		orig bp <- [[after bind]Blueprints >>]Index[~] {}
+		{ orig bp <- NBlueprint[] }
+		out <- [after bind]Blueprints <<[ [[after bind]Blueprints >>]Set[~, [orig bp]Add Method[[parts]Index[0]] ] ]
 	}{
 		out <- Val[after bind]
 	}
@@ -1178,12 +1330,13 @@
 
 Register Builtins@NProgram[prog:out]
 {
-	registered <- [[[[prog]Register Worker["Print", "rhope", 1, 1]
+	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 Number Methods
 	
-	out <- [[[registered]Bind Worker["If@Boolean",
+	out <- [[[[registered]Bind Worker["If@Boolean",
 		[[[[[NWorker["rhope"]
 		]Inputs <<[("condition")]
 		]Input Types <<[ [()]Append[Type Instance["Boolean"]] ]
@@ -1203,7 +1356,14 @@
 		]Input Types <<[ [()]Append[Type Instance["Blueprint"]] ]
 		]Outputs <<[("out")]
 		]Output Types <<[ [()]Append[Type Instance["Any Type"]] ]
-		]Builtin? <<[Yes]] 
+		]Builtin? <<[Yes]] 
+	]Bind Worker["Blueprint Of",
+		[[[[[NWorker["rhope"]
+		]Inputs <<[("object")]
+		]Input Types <<[ [()]Append[Type Instance["Any Type"]]]
+		]Outputs <<[("type")]
+		]Output Types <<[ [()]Append[Type Instance["Blueprint"]]]
+		]Builtin? <<[Yes]]
 }
 
 Find Worker@NProgram[prog, name:out,notfound]