diff cbackend.rhope @ 74:a844c623c7df

Add support for Worker type
author Mike Pavone <pavone@retrodev.com>
date Thu, 01 Jul 2010 21:32:08 -0400
parents f7bcf3db1342
children 0083b2f7b3c7
line wrap: on
line diff
--- a/cbackend.rhope	Sat Jun 19 23:13:41 2010 -0400
+++ b/cbackend.rhope	Thu Jul 01 21:32:08 2010 -0400
@@ -242,8 +242,8 @@
 		If[[[ctype]Name >>]=["Blueprint"]]
 		{
 			out <- ""	
-		}{
-			If[[[ctype]Name >>]=["Array"]]
+		}{
+			[("Array","Worker")]Find[[ctype]Name >>]
 			{ oend <- "\nMObject(" }
 			{ oend <- "\nObject(" } 
 			out <- [Fold["_Type Def C Type", "OBegin", [ctype]Fields >>]]Append[ [[oend]Append[Escape Rhope Name[[ctype]Name >>]]]Append[")"] ]
@@ -264,8 +264,8 @@
 }
 
 Type Init@C Type[ctype,id,method reg,field reg:out]
-{
-	If[[[ctype]Name >>]=["Array"]]
+{
+	[("Array","Worker")]Find[[ctype]Name >>]
 	{ size <- "-1" }
 	{ 
 		[("Int64","Int32","Int16","Int8")]Find[[ctype]Name >>]
@@ -315,7 +315,7 @@
 C Type Registry[:out]
 {
 	out <- [[[Build["C Type Registry"]]Lookup << [
-			[[[[[[[[[[[[[[[[[Dictionary[]
+			[[[[[[[[[[[[[[[[[[Dictionary[]
 			]Set["UInt8", "TYPE_UINT8"]
 			]Set["UInt16", "TYPE_UINT16"]
 			]Set["UInt32", "TYPE_UINT32"]
@@ -329,7 +329,8 @@
 			]Set["Float64", "TYPE_FLOAT64"]
 			]Set["Real Number", "TYPE_FLOAT64"]
 			]Set["Blueprint", "TYPE_BLUEPRINT"]
-			]Set["Array", "TYPE_ARRAY"]
+			]Set["Array", "TYPE_ARRAY"]
+			]Set["Worker", "TYPE_WORKER"]
 			]Set["Method Missing Exception", "TYPE_METHODMISSINGEXCEPTION"]
 			]Set["Field Missing Exception", "TYPE_FIELDMISSINGEXCEPTION"]
 			]Set["Wrong Type Exception", "TYPE_WRONGTYPEEXCEPTION"]]
@@ -564,9 +565,15 @@
 	out <- [func]Add Statement[[dest]Append[" = NULL"]]
 }
 
-Lookup Constant@C Function[func,const:out]
+Lookup Constant@C Function[func,const,doaddref:out]
 {
-	out <- [["add_ref(_const_"]Append[Escape Rhope Name[const]]]Append[")"]
+	var <- ["_const_"]Append[Escape Rhope Name[const]]
+	If[doaddref]
+	{
+		out <- [["add_ref("]Append[var]]Append[")"]
+	}{
+		out <- Val[var]
+	}
 }
 
 Field Result@C Function[func,var,field:out]
@@ -681,16 +688,86 @@
 		]Append[val]
 		]Append[")"]
 	]
+}
+
+_Val Function Arg C[func,val,inputnum,worker:out]
+{
+	out <- [func]Add Raw Line[
+		[[[[[["VCSetParam("
+		]Append[worker]
+		]Append[", "]
+		]Append[inputnum]
+		]Append[", "]
+		]Append[val]
+		]Append[")"]
+	]
 }
 
 Method Call@C Function[func,method,args:out]
 {
 	out <- [func]Call[method,args]
+}
+
+Val Call@C Function[func,to call,args:out]
+{
+	worker <- Make Op[Strip Addref[to call], func]
+	rargs <- Map[args, ["Make Op"]Set Input[1, func]]
+
+	If[[[func]Last NumParams >>] = [-1]]
+	{
+		freed <- Val[func]
+	}{
+		freed <- [func]Add Raw Line["FreeCall"]
+	}
+	prepped <- [[freed]Add Raw Line[ 
+			[[[["VCPrepCall("
+				]Append[worker]
+				]Append[", "]
+				]Append[[rargs]Length]
+				]Append[")"] ]
+		]Last NumParams <<[[rargs]Length]
+	
+	
+	out <- [[[[Fold[["_Val Function Arg C"]Set Input[3, worker], prepped, rargs]
+	]Add Raw Line[
+		[[[[[[[["ValCall("
+		]Append[worker]
+		]Append[", "]
+		]Append[[rargs]Length]
+		]Append[", "]
+		]Append[[func]Resume Index >>]
+		]Append[", "]
+		]Append[Escape Rhope Name[[func]Name >>]]
+		]Append[")"]]
+	]Add Raw Line["DISPATCH"]
+	]Add Raw Line[
+		[[[["ValCallPostlude("
+		]Append[[func]Resume Index >>]
+		]Append[", "]
+		]Append[Escape Rhope Name[[func]Name >>]]
+		]Append[")"]]
+	]Resume Index <<[ [[func]Resume Index >>]+[1] ]
 }
 
 Call@C Function[func,name,args:out]
-{
-	out <- [func]Func Base[Escape Rhope Name[name],args, "Call"]
+{
+	If[[name]=["Call@Worker"]]
+	{
+		//TODO: Handle case when user explicitly calls the fully qualified version, but the type of the first arg isn't Worker
+		out <- [func]Val Call[[args]Index[0], Tail[args,1]]
+	}{
+		If[[name]=["Call"]]
+		{
+			to call <- [args]Index[0]
+			out <- [[[[[func]Add Raw Line[[["if (get_blueprint("]Append[Make Op[Strip Addref[to call], func]]]Append[")->type_id == TYPE_WORKER) {"]]
+			]Val Call[to call, Tail[args,1]]
+			]Add Raw Line["} else {"]
+			]Func Base["Call",args, "Call"]
+			]Add Raw Line["}"]
+		}{
+			out <- [func]Func Base[Escape Rhope Name[name],args, "Call"]
+		}
+	}
 }
 
 Func Base@C Function[func,tocall,args,type:out]
@@ -829,7 +906,7 @@
 
 _Set Outputs C[string,inputname,inputnum,func:out]
 {
-	out <- [string]Append[[[ [ ["\tRet("]Append[inputnum] ]Append[ [[", lv_"]Append[Escape Rhope Name[[func]Name >>]]]Append["->"]]]Append[inputname]]Append[")\n"]]
+	out <- [string]Append[[[ [ ["\tRet("]Append[inputnum] ]Append[ [[", lv_"]Append[Escape Rhope Name[[func]Name >>]]]Append["->"]]]Append[Escape Rhope Name[inputname]]]Append[")\n"]]
 }
 
 Set Outputs@C Function[func:out]
@@ -1070,29 +1147,28 @@
 _Consts C Program[text,value,name:out]
 {
 	out <- [text]Append[ [["object * _const_"]Append[Escape Rhope Name[name]]]Append[";\n"] ]
-}
-
-_Set Consts C Program[text,value,name,type reg:out]
-{
-	//TODO: Support more constant types
-	valtype <- Type Of[value]
+}
+
+Const Construct C[value,type reg:out]
+{
+	valtype <- Type Of[value]
 	[("Int32","Whole Number")]Find[valtype]
-	{
-		out <- [text]Append[[[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = make_Int32("]]Append[value]]Append[");\n"]]
-	}{
+	{
+		out <- [["make_Int32("]Append[value]]Append[")"]
+	}{
 		If[[valtype] = ["Type Instance"]]
 		{
 			//TODO: Support parametric types
-			typeid <- [type reg]Type ID[[value]Name >>]
-			out <- [text]Append[[[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = make_Blueprint("]]Append[typeid]]Append[");\n"]]
-		}{
+			typeid <- [type reg]Type ID[[value]Name >>]
+			out <- [["make_Blueprint("]Append[typeid]]Append[")"]
+		}{
 			If[[valtype] = ["Yes No"]]
 			{
 				If[value]
 				{
-					out <- [text]Append[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = make_Bool(1);\n"]]
+					out <- "make_Bool(1)"
 				}{
-					out <- [text]Append[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = make_Bool(0);\n"]]
+					out <- "make_Bool(0)"
 				}
 			}{
 				If[[valtype] = ["Machine Integer"]]
@@ -1101,24 +1177,83 @@
 					{ s <- "I" }
 					{ s <- "UI" }
 					
-					make <- [[[" = make_"]Append[s]]Append["nt"]]Append[[value]Size >>]
-					out <- [text]Append[ [[[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[make]]Append["("]]Append[[value]Value >>]]Append[");\n"] ]
-				}{
-					out <- text
-				}
-			}
-		}
+					out <- [[[[[["make_"
+						]Append[s]
+						]Append["nt"]
+						]Append[[value]Size >>]
+						]Append["("]
+						]Append[[value]Value >>]
+						]Append[")"]
+				}{
+					If[[valtype] = ["String"]]
+					{
+						out <- [["make_String(\""]Append[ [[value]Replace["\\", "\\\\"]]Replace["\n", "\\n"]]]Append["\")"]
+					}{
+						If[[valtype]=["Worker Literal"]]
+						{
+							//TODO: Figure out how to fully support these in nested cases
+							//or workaround the problem higher up in the food chain
+							[[value]Args >>]Last
+							{ size <- [~]+[1] }
+							{ size <- "0" }
+							out <- [[[[[["make_Worker(FUNC_"
+									]Append[Escape Rhope Name[[value]Name >>]]
+									]Append[", "]
+									]Append[size]
+									]Append[", "]
+									]Append[[[value]Args >>]Length]
+									]Append[")"]
+						}{
+							out <- "UnhandledLiteralType"
+						}
+					}
+				}
+			}
+		}
+			
+	}
+}
+
+_Set Worker Params C[text,param,num,type reg,name:out]
+{
+	out <- [text]Append[
+		[[[[[["\t((object **)(((t_Worker *)_const_"
+			]Append[name]
+			]Append[")+1))["]
+			]Append[num]
+			]Append["] = "]
+			]Append[Const Construct C[param, type reg]]
+			]Append[";\n"] ]
+}
+
+_Set Consts C Program[text,value,name,type reg:out]
+{
+	valtype <- Type Of[value]
+	[("String","Worker Literal")]Find[valtype]
+	{
+		out <- text
+	}{
+		Const Construct C[value,type reg]
+		{ out <- [text]Append[ [[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = "]]Append[~]]Append[";\n"] ] }
 	}
 }
 
 _Set Late Consts C[text,value,name,type reg:out]
 {
 	valtype <- Type Of[value]
-	If[[valtype] = ["String"]]
-	{
-		out <- [text]Append[ [[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = make_String(\""]]Append[ [[value]Replace["\n", "\\n"]]Replace["\\", "\\\\"]]]Append["\");\n"] ]
-	}{
-		out <- text
+	[("String","Worker Literal")]Find[valtype]
+	{
+		Const Construct C[value,type reg]
+		{ init <- [text]Append[ [[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = "]]Append[~]]Append[";\n"] ] }
+		
+		If[[valtype]=["Worker Literal"]]
+		{
+			out <- Fold[[["_Set Worker Params C"]Set Input[3, type reg]]Set Input[4, Escape Rhope Name[name]], init, [value]Args >>]
+		}{
+			out <- Val[init]
+		}
+	}{
+		out <- text
 	}
 }
 
@@ -1269,7 +1404,8 @@
 #include \"func.h\"
 #include \"integer.h\"
 #include \"blueprint.h\"
-#include \"array.h\"
+#include \"array.h\"
+#include \"worker.h\"
 #include \"bool.h\"\n\n"
 	out <- [[[[[[[[[[[[[[[headers
 		]Append[[program]Dispatch[all methods]]
@@ -1281,7 +1417,7 @@
 		]Append["\n
 uint16_t rhope(uint32_t func, object ** params, uint16_t numparams, uint16_t callspace)
 {
-	uint16_t resume,idx;
+	uint16_t resume,idx, vcparam_offset, last_vcparam;
 	context * ct;
 	calldata * cdata, *temp_cdata, *my_cdata;\n\nFuncDef(Build)\nFuncDef(BlueprintSP_Of)\n"]
 		]Append[Fold["Local Pointers", "", [program]Functions >>]]
@@ -1336,7 +1472,8 @@
 }
 
 #include \"builtin.c\"
-#include \"array.c\"
+#include \"array.c\"
+#include \"worker.c\"
 
 int main(int argc, char **argv)
 {