diff cbackend.rhope @ 34:df038cef648b

More work on supporting user defined types in the C backend
author Mike Pavone <pavone@retrodev.com>
date Wed, 30 Sep 2009 01:25:03 -0400
parents 3b47a8538df2
children 3498713c3dc9
line wrap: on
line diff
--- a/cbackend.rhope	Mon Sep 28 22:08:40 2009 -0400
+++ b/cbackend.rhope	Wed Sep 30 01:25:03 2009 -0400
@@ -57,6 +57,35 @@
 	out,notfound <- [[reg]Lookup >>]Index[method]
 }
 
+Blueprint C Field Registry
+{
+	Lookup
+	Next ID
+}
+
+C Field Registry[:out]
+{
+	out <- [[Build["C Field Registry"]]Lookup <<[New@Dictionary[]]]Next ID<<[0]
+
+}
+
+Register Field@C Field Registry[reg,field:out]
+{
+	[[reg]Lookup >>]Index[field]
+	{
+		out <- reg
+	}{
+		field ID <- [reg]Next ID>>
+		new lookup <- [[reg]Lookup >>]Set[field, field ID]
+		out <- [[reg]Lookup <<[new lookup]]Next ID <<[[field ID]+[1]]
+	}
+}
+
+Field ID@C Field Registry[reg,field:out,notfound]
+{
+	out,notfound <- [[reg]Lookup >>]Index[field]
+}
+
 Blueprint C Type
 {
 	Name
@@ -79,16 +108,36 @@
 	out <- [ctype]Methods <<[ [[ctype]Methods >>]Append[name] ]
 }
 
+Register Methods@C Type[ctype,method reg:out]
+{
+	out <- Fold["Register Method", method reg, [ctype]Methods >>]
+}
+
+Register Fields@C Type[ctype,field reg:out]
+{
+	out <- Fold["Register Field", field reg, [ctype]Fields >>]
+}
+
+Rhope Type to C[typename,naked:out]
+{
+	If[[typename] = ["Any Type"]]
+	{
+		ctype <- "struct object"
+	}{
+		ctype <- ["t_"]Append[Escape Rhope Name[typename]]
+	}
+	If[naked]
+	{
+		out <- Val[ctype]
+	}{
+		out <- [ctype]Append[" * "]
+	}
+}
+
 _Type Def C Type[text,field:out]
 {
 	name <- [field]Index[0]
-	rawtype <- [field]Index[1]
-	If[[rawtype] = ["Any Type"]]
-	{
-		type <- "\n\tobject * "
-	}{
-		type <- [["\n\tt_"]Append[Escape Rhope Name[rawtype]]]Append[" * "]
-	}
+	type <- ["\n\t"]Append[Rhope Type to C[[field]Index[1], No]]
 	out <- [[[text]Append[type]]Append[Escape Rhope Name[name]]]Append[";"]
 }
 
@@ -140,8 +189,29 @@
 		]Next ID <<[0]
 }
 
-Register Type@C Type Registry[reg,name,def:out]
+_Type Defs C[text,def:out]
+{
+	out <- [[text]Append["\n\n"]]Append[[def]Type Def]
+}
+
+Type Defs@C Type Registry[reg:out]
+{
+	out <- Fold["_Type Defs C", "", [reg]Definitions >>]
+}
+
+_Type Inits C[reg,method reg,field reg,text,def,name:out]
 {
+	out <- [[text]Append["\n\n"]]Append[ [def]Type Init[[reg]Type ID[name], method reg, field reg] ]
+}
+
+Type Inits@C Type Registry[reg,method reg,field reg:out]
+{
+	out <- Fold[[[["_Type Inits C"]Set Input[0, reg]]Set Input[1, method reg]]Set Input[2, field reg], "", [reg]Definitions >>]
+}
+
+Register Type@C Type Registry[reg,def:out]
+{
+	name <- [def]Name >>
 	[[reg]Lookup >>]Index[name]
 	{
 		out <- reg
@@ -418,26 +488,32 @@
 Text@C Function[func:out]
 {
 	cname <- Escape Rhope Name[[func]Name >>]
-	If[ [[[func]Variables >>]Length] = [0] ]
+	If[ [[func]Convention >>] = ["rhope"] ]
 	{
-		out <- [[[[[["FuncNoLocals("
-			]Append[cname]
-			]Append[",\n\tNumParams "]
-			]Append[ [[func]Inputs >>]Length ]
-			]Append[",\n\tCallSpace 32)\n\n"]//TODO: Fill in with calculated callspace value
-			]Append[ [[func]Statements >>]Join[""] ]
-			]Append["EndFunc"]
+		If[ [[[func]Variables >>]Length] = [0] ]
+		{
+			out <- [[[[[["FuncNoLocals("
+				]Append[cname]
+				]Append[",\n\tNumParams "]
+				]Append[ [[func]Inputs >>]Length ]
+				]Append[",\n\tCallSpace 32)\n\n"]//TODO: Fill in with calculated callspace value
+				]Append[ [[func]Statements >>]Join[""] ]
+				]Append["EndFunc"]
+		}{
+			out <- [[[[[[[[["Func("
+				]Append[cname]
+				]Append[",\n\tNumParams "]
+				]Append[ [[func]Inputs >>]Length ]
+				]Append[",\n\tCallSpace 32,\n\t"]//TODO: Fill in with calculated callspace value
+				]Append[["l_"]Append[cname]]
+				]Append[")\n\n"]
+				]Append[ [[func]Statements >>]Join[""] ]
+				]Append[[func]Set Outputs]
+				]Append["EndFunc"]
+		}
 	}{
-		out <- [[[[[[[[["Func("
-			]Append[cname]
-			]Append[",\n\tNumParams "]
-			]Append[ [[func]Inputs >>]Length ]
-			]Append[",\n\tCallSpace 32,\n\t"]//TODO: Fill in with calculated callspace value
-			]Append[["l_"]Append[cname]]
-			]Append[")\n\n"]
-			]Append[ [[func]Statements >>]Join[""] ]
-			]Append[[func]Set Outputs]
-			]Append["EndFunc"]
+		//TODO: We need to store input and output types somewhere so we can reference them here
+		out <- "oops"
 	}
 }
 
@@ -445,11 +521,25 @@
 {
 	Functions
 	Method Registry
+	Field Registry
+	Type Registry
 }
 
 C Program[:out]
 {
-	out <- [[Build["C Program"]]Functions <<[New@Dictionary[]]]Method Registry <<[C Method Registry[]]
+	out <- [[[[Build["C Program"]]Functions <<[New@Dictionary[]]]Method Registry <<[C Method Registry[]]]Type Registry <<[C Type Registry[]]]Field Registry <<[C Field Registry[]]
+}
+
+Register Type@C Program[program,def:out]
+{
+	out <- [[[program]Type Registry <<[ [[program]Type Registry >>]Register Type[def] ]
+		]Method Registry <<[ [def]Register Methods[[program]Method Registry >>] ]
+		]Field Registry <<[ [def]Register Fields[[program]Field Registry >>] ]
+}
+
+Create Type@C Program[program,name:out]
+{
+	out <- C Type[name]
 }
 
 Create Function@C Program[program,name,inputs,outputs,convention:out]
@@ -508,7 +598,9 @@
 #include \"context.h\"
 #include \"func.h\"
 #include \"integer.h\"\n\n"
-	out <- [[[[headers]Append[Fold["_Text C Program", 
+	out <- [[[[[[headers
+		]Append[[[program]Type Registry >>]Type Defs]
+		]Append[Fold["_Text C Program", 
 				Fold["_Consts C Program", 
 					Fold["_Defs C Program", "", [program]Functions >>], 
 					constants
@@ -519,6 +611,7 @@
 	calldata *cdata;
 	context * ct;
 	register_builtin_types();\n\n"]
+		]Append[ [[program]Type Registry >>]Type Inits[[program]Method Registry >>, [program]Field Registry >>] ]
 		]Append[Fold["_Set Consts C Program", "", constants]]
 		]Append["
 	ct = new_context();