diff cbackend_c.rhope @ 139:a68e6828d896

Global stores and transactions are working. Definately leaks memory on retries. Probably a fair number of bugs to work out. However, a basic test program works.
author Mike Pavone <pavone@retrodev.com>
date Fri, 19 Nov 2010 04:04:14 -0500
parents fc3815b7462f
children c14698c512f1
line wrap: on
line diff
--- a/cbackend_c.rhope	Tue Nov 16 21:53:18 2010 -0500
+++ b/cbackend_c.rhope	Fri Nov 19 04:04:14 2010 -0500
@@ -244,6 +244,30 @@
 	}
 }
 
+_Global Type Defs C[text,store,p:out]
+{
+	estore <- Escape Rhope Name[[store]Name >>, p]
+	varnames <- Map[Keys[[store]Vars >>], Escape Rhope Name[?, p]]
+	out <- [text]Append[ 
+		[[[[[[[[["typedef struct {\n\tobject header;\n\t"
+			]Append[ [[ Map[varnames, ["object * gs_"]Append[?]] ]Join[";\n\t"]
+				]Append[";\n} mutt_"]]
+			]Append[estore]
+			]Append[[[";\nmutable_object * gs_"]Append[estore]]Append[";\n\n"]]
+			]Append[[["void gscopy_"]Append[estore]]Append[[["(object * obj)\n{\n\tmutt_"]Append[estore]]Append[[[" *mutt_obj = (mutt_"]Append[estore]]Append[" *)obj;\n\t"]]]]
+			]Append[ [Map[varnames, ["add_ref(mutt_obj->gs_"]Append[?]]]Join[");\n\t"] ]
+			]Append[");\n}\n\n"]
+			]Append[[["void gscleanup_"]Append[estore]]Append[[["(object * obj)\n{\n\tmutt_"]Append[estore]]Append[[[" *mutt_obj = (mutt_"]Append[estore]]Append[" *)obj;\n\t"]]]]
+			]Append[ [Map[varnames, ["release_ref(mutt_obj->gs_"]Append[?]]]Join[");\n\t"] ]
+			]Append[");\n}\n\n"] ]
+}
+
+_Global Type Inits C[text,store,p:out]
+{
+	estore <- Escape Rhope Name[[store]Name >>, p]
+	out <- [text]Append[ [[[[[[[[["\tbp = register_type(sizeof(mutt_"]Append[estore]]Append[ [[[")-sizeof(object), NULL, gscopy_"]Append[estore]]Append[", gscleanup_"]]Append[estore] ]]Append[");\n\tgs_"]]Append[estore]]Append[" = (mutable_object *)new_object(TYPE_MUTABLEGLOBAL);\n\tgs_"]]Append[estore]]Append["->data = new_object_bp(bp);\n\tgs_"]]Append[estore]]Append["->version = 0;"] ]
+}
+
 _Type Init C[type name,method reg,text,method,p:out]
 {
 	out <- [[text]Append[[["\n\tadd_method(bp, "]Append[ [method reg]Method ID[method] ]]Append[  [[", MethodName("]Append[Escape Rhope Name[method,p]]]Append[[","]Append[Escape Rhope Name[type name,p]]]]]]Append["));"]
@@ -408,6 +432,7 @@
 	Constants
 	Input Types
 	Output Types
+	Uses
 	Resume Index
 	Last NumParams
 	Escape Pattern
@@ -420,7 +445,7 @@
 
 C Function With Registry[name,inputs,outputs,convention,registry,field reg,type reg,p:out]
 {
-	out <- [[[[[[[[[[[[[[[Build[C Function()]
+	out <- [[[[[[[[[[[[[[[[Build[C Function()]
 		]Name <<[name]
 		]Inputs <<[inputs]
 		]Outputs <<[outputs]
@@ -436,6 +461,7 @@
 		]Resume Index <<[1]
 		]Last NumParams <<[-1]
 		]Escape Pattern <<[p]
+		]Uses <<[()]
 }
 
 Set Input Type@C Function[func,type,input num:out]
@@ -652,8 +678,22 @@
 	result op <- Field Ref[var,field]
 }
 
+Global Reference@C Function[func,store,var:out]
+{
+	
+	estore <- Escape Rhope Name[store,[func]Escape Pattern >>]
+	out <- [[[[[ [["((mutt_"]Append[estore]]Append["*)lv_"]]Append[Escape Rhope Name[[func]Name >>,[func]Escape Pattern >>]]]Append["->gs_"]]Append[estore]]Append["->local_data)->gs_"]]Append[Escape Rhope Name[var,[func]Escape Pattern >>]]
+}
 
-
+Set Global@C Function[func,store,var,src:out]
+{
+	estore <- Escape Rhope Name[store,[func]Escape Pattern >>]
+	cell pointer <- [[["lv_"]Append[Escape Rhope Name[[func]Name >>,[func]Escape Pattern >>]]]Append["->gs_"]]Append[estore]
+	out <- [[[[func]Add Statement[[["tmp = copy_object("]Append[[cell pointer]Append["->local_data"]]]Append[")"]]
+		]Add Statement[ [[[["((mutt_"]Append[estore]]Append[" *)tmp)->gs_"]]Append[Escape Rhope Name[var, [func]Escape Pattern >>]]]Append[[" = "]Append[[src]Make Op[func]]] ]
+		]Add Statement[[cell pointer]Append["->local_data = tmp"]]
+		]Add Statement[[cell pointer]Append["->local_version++"]]
+}
 
 Set Field Null@C Function[func,var,field:out]
 {
@@ -983,6 +1023,10 @@
 {
 	out <- [[[string]Append[ ["\t"]Append[Rhope Type to C[type,p]] ]]Append[[" "]Append[Escape Rhope Name[varname,p]]]]Append[";\n"]
 }
+_Global Cell Defs C[func,p,string,store:out]
+{
+	out <- [string]Append[ [["\ttrans_cell *gs_"]Append[Escape Rhope Name[store,p]]]Append[";\n"] ]
+}
 
 
 Definitions@C Function[func:out]
@@ -991,7 +1035,7 @@
 	{
 	If[ [[[func]Convention >>] = ["rhope"]] And [[ [[[func]Variables >>]Length]+[[[func]Outputs >>]Length] ] > [0]] ]
 	{
-		localtype <- [[[Fold[_Output Defs C[?, ?, ?, func], Fold[_Var Defs C[?,?,?,[func]Escape Pattern >>],"typedef struct {\n", [func]Variables >>], [func]Outputs >>]]Append["} lt_"]]Append[Escape Rhope Name[[func]Name >>,[func]Escape Pattern >>]]]Append[";\n"]
+		localtype <- [[[Fold[_Global Cell Defs C[func,[func]Escape Pattern >>,?], Fold[_Output Defs C[?, ?, ?, func], Fold[_Var Defs C[?,?,?,[func]Escape Pattern >>],"typedef struct {\n", [func]Variables >>], [func]Outputs >>], [func]Uses >>]]Append["} lt_"]]Append[Escape Rhope Name[[func]Name >>,[func]Escape Pattern >>]]]Append[";\n"]
 	}{
 		localtype <- ""
 	}
@@ -1062,6 +1106,12 @@
 	}
 }
 
+Find Trans Cell@C Function[func,text,store:out]
+{
+	estore <- Escape Rhope Name[store,[func]Escape Pattern >>]
+	out <- [text]Append[  [[[["lv_"]Append[Escape Rhope Name[[func]Name >>,[func]Escape Pattern >>]]]Append["->gs_"]]Append[estore]]Append[[[" = find_obj_cell(ct->transaction, gs_"]Append[estore]]Append[");\n"]] ]
+}
+
 Text@C Function[func:out]
 {	
 	Print[["Text@C Function: "]Append[[func]Name >>]]
@@ -1081,7 +1131,7 @@
 		}
 		fname <- Escape Rhope Name[[func]Name >>,[func]Escape Pattern >>]
 		param check <- Fold[Check Param Type C[?, ?, ?, func], "", [func]Input Types >>]
-		If[ [ [[[func]Variables >>]Length]+[[[func]Outputs >>]Length] ] = [0] ]
+		If[ [ [[[[func]Variables >>]Length]+[[[func]Outputs >>]Length]]+[[[func]Uses >>]Length] ] = [0] ]
 		{
 			out <- [[[[[[[[ [type]Append["NoLocals("]
 				]Append[cname]
@@ -1099,14 +1149,29 @@
 			}{
 				freecall <- "\n\tFreeCall\n"
 			}
-			out <- [[[[[[[[[[ [type]Append["("]
+			If[[[func]Uses >>]Length]
+			{
+				begin trans <- [[[["\tbegin_transaction(ct, "]Append[ String[[[func]Uses >>]Length] ]]Append[", "]]Append[ [Map[Map[[func]Uses >>, Escape Rhope Name[?, [func]Escape Pattern >>]], ["gs_"]Append[?]]]Join[", "] ]]Append[");\n"]
+				init trans <- [[Fold[Find Trans Cell[func,?], begin trans, [func]Uses >>]
+					]Append[[["transretry_"]Append[fname]]Append[":\n"]]
+					]Append["\tfor(idx = 0; idx < cdata->num_params; ++idx) { add_ref(cdata->params[idx]); }\n"]
+				//TODO: Figure out whether this is a readonly or write transaction
+				commit trans <- [[["\tif(!commit_transaction(ct, 0)) { prep_retry(ct); goto transretry_"]Append[fname]]Append["; }\n"]
+					]Append["\tfor(idx = 0; idx < cdata->num_params; ++idx) { release_ref(cdata->params[idx]); }\n"]
+			}{
+				init trans <- ""
+				commit trans <- ""
+			}
+			out <- [[[[[[[[[[[[ [type]Append["("]
 				]Append[cname]
 				]Append[",\n\tNumParams "]
 				]Append[ String[[[func]Inputs >>]Length] ]
 				]Append[")\n\n"]
 				]Append[param check]
+				]Append[init trans]
 				]Append[ [[func]Statements >>]Join[""] ]
 				]Append[freecall]
+				]Append[commit trans]
 				]Append[[func]Set Outputs]
 				]Append[[["EndFunc("]Append[fname]]Append[")\n"]]
 				]Append["DISPATCH"]
@@ -1129,6 +1194,7 @@
 	Field Registry
 	Type Registry
 	Libraries
+	Global Stores
 	Escape Pattern
 }
 
@@ -1136,7 +1202,7 @@
 {
 	p <- Pattern[("_",  "@",  " ",  ":",  "?",  "+",  "-",  "*",  "/",  "<",  ">",  "(",  ")",  "!",  "=",  "'",  
 			"\"", "\t", ",",  ".",  "\n", "{",  "}",   "[",   "]",   "#",   "\\",  "\r",  ";",   "&",   "|",   "%",   "^",   "`",   "~")]
-	out <- [[[[[[Build[C Program()]]Functions <<[Dictionary[]]]Method Registry <<[C Method Registry[]]]Type Registry <<[C Type Registry[p]]]Field Registry <<[C Field Registry[]]]Libraries <<[Dictionary[]]]Escape Pattern <<[p]
+	out <- [[[[[[[Build[C Program()]]Functions <<[Dictionary[]]]Method Registry <<[C Method Registry[]]]Type Registry <<[C Type Registry[p]]]Field Registry <<[C Field Registry[]]]Libraries <<[Dictionary[]]]Escape Pattern <<[p]]Global Stores <<[Dictionary[]]
 }
 
 Supported Number Types@C Program[program:out]
@@ -1373,6 +1439,17 @@
 	}
 }
 
+_Global Var Init C[text,value,name,storevar,type reg:out]
+{
+	out <- [[[[[text]Append[storevar]]Append[Escape Rhope Name[name, [type reg]Escape Pattern >>]]]Append[" = "]]Append[Const Construct C[value, type reg]]]Append[";\n"]
+}
+
+_Global Store Inits C[text,store,type reg:out]
+{
+	estore <- Escape Rhope Name[[store]Name >>, [type reg]Escape Pattern >>]
+	out <- Fold[_Global Var Init C[?,?,?, [[[["\t((mutt_"]Append[estore]]Append[" *)(gs_"]]Append[estore]]Append["->data))->gs_"], type reg], text, [store]Vars >>]
+}
+
 _Dispatch Switch Sub[text, num, name:out]
 {
 	out <- [[[[[text
@@ -1533,9 +1610,10 @@
 #include \"worker.h\"
 #include \"bool.h\"
 #include <sys/time.h>\n\n"
-	out <- [[[[[[[[[[[[[[[[[[headers
+	out <- [[[[[[[[[[[[[[[[[[[[[headers
 		]Append[[program]Dispatch[all methods]]
 		]Append[[[program]Type Registry >>]Type Defs]
+		]Append[Fold[_Global Type Defs C[?,?,p], "", [program]Global Stores >>]]
 		]Append[Fold[_Consts C Program[?,?,?,p], 
 					Fold[_Defs C Program[?], "", [program]Functions >>], 
 					constants]]
@@ -1556,6 +1634,7 @@
 #endif
 	uint16_t resume,idx, vcparam_offset, last_vcparam;
 	context * ct;
+	void *tmp;
 	calldata * cdata, *temp_cdata, *my_cdata;
 	DispatchVar
 	FuncDef(Build)
@@ -1652,10 +1731,13 @@
 	int numret;
 	int idx;
 	object * inout[3];
-	register_builtin_types();\n\n"]
+	register_builtin_types();
+	register_type_byid(TYPE_MUTABLEGLOBAL, sizeof(mutable_object)-sizeof(object), NULL, NULL, NULL);\n\n"]
 		]Append[ [[program]Type Registry >>]Type Inits[[program]Method Registry >>, [program]Field Registry >>] ]
+		]Append[Fold[_Global Type Inits C[?, ?, p], "", [program]Global Stores >>]]
 		]Append[Fold[_Set Consts C Program[?, ?, ?, [program]Type Registry >>], "", constants]]
 		]Append[Fold[_Set Late Consts C[?, ?, ?, [program]Type Registry >>], "", constants]]
+		]Append[Fold[_Global Store Inits C[?, ?, [program]Type Registry >>], "", [program]Global Stores >>]]
 		]Append[Fold[Init Type Names[?, ?, ?, [program]Type Registry >>], "", [[program]Type Registry >>]Lookup >>]]
 		]Append["
 	rhope(FUNC_List, inout, 0, 1);