comparison 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
comparison
equal deleted inserted replaced
138:1411de6050e1 139:a68e6828d896
242 out <- [Fold[_Type Def C Type[?,?,p], "OBegin", [ctype]Fields >>]]Append[ [[oend]Append[Escape Rhope Name[[ctype]Name >>,p]]]Append[")"] ] 242 out <- [Fold[_Type Def C Type[?,?,p], "OBegin", [ctype]Fields >>]]Append[ [[oend]Append[Escape Rhope Name[[ctype]Name >>,p]]]Append[")"] ]
243 } 243 }
244 } 244 }
245 } 245 }
246 246
247 _Global Type Defs C[text,store,p:out]
248 {
249 estore <- Escape Rhope Name[[store]Name >>, p]
250 varnames <- Map[Keys[[store]Vars >>], Escape Rhope Name[?, p]]
251 out <- [text]Append[
252 [[[[[[[[["typedef struct {\n\tobject header;\n\t"
253 ]Append[ [[ Map[varnames, ["object * gs_"]Append[?]] ]Join[";\n\t"]
254 ]Append[";\n} mutt_"]]
255 ]Append[estore]
256 ]Append[[[";\nmutable_object * gs_"]Append[estore]]Append[";\n\n"]]
257 ]Append[[["void gscopy_"]Append[estore]]Append[[["(object * obj)\n{\n\tmutt_"]Append[estore]]Append[[[" *mutt_obj = (mutt_"]Append[estore]]Append[" *)obj;\n\t"]]]]
258 ]Append[ [Map[varnames, ["add_ref(mutt_obj->gs_"]Append[?]]]Join[");\n\t"] ]
259 ]Append[");\n}\n\n"]
260 ]Append[[["void gscleanup_"]Append[estore]]Append[[["(object * obj)\n{\n\tmutt_"]Append[estore]]Append[[[" *mutt_obj = (mutt_"]Append[estore]]Append[" *)obj;\n\t"]]]]
261 ]Append[ [Map[varnames, ["release_ref(mutt_obj->gs_"]Append[?]]]Join[");\n\t"] ]
262 ]Append[");\n}\n\n"] ]
263 }
264
265 _Global Type Inits C[text,store,p:out]
266 {
267 estore <- Escape Rhope Name[[store]Name >>, p]
268 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;"] ]
269 }
270
247 _Type Init C[type name,method reg,text,method,p:out] 271 _Type Init C[type name,method reg,text,method,p:out]
248 { 272 {
249 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["));"] 273 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["));"]
250 } 274 }
251 275
406 Field Registry 430 Field Registry
407 Type Registry 431 Type Registry
408 Constants 432 Constants
409 Input Types 433 Input Types
410 Output Types 434 Output Types
435 Uses
411 Resume Index 436 Resume Index
412 Last NumParams 437 Last NumParams
413 Escape Pattern 438 Escape Pattern
414 } 439 }
415 440
418 out <- C Function With Registry[name,inputs,outputs,convention, C Method Registry[], C Field Registry[], C Type Registry[p],p] 443 out <- C Function With Registry[name,inputs,outputs,convention, C Method Registry[], C Field Registry[], C Type Registry[p],p]
419 } 444 }
420 445
421 C Function With Registry[name,inputs,outputs,convention,registry,field reg,type reg,p:out] 446 C Function With Registry[name,inputs,outputs,convention,registry,field reg,type reg,p:out]
422 { 447 {
423 out <- [[[[[[[[[[[[[[[Build[C Function()] 448 out <- [[[[[[[[[[[[[[[[Build[C Function()]
424 ]Name <<[name] 449 ]Name <<[name]
425 ]Inputs <<[inputs] 450 ]Inputs <<[inputs]
426 ]Outputs <<[outputs] 451 ]Outputs <<[outputs]
427 ]Convention <<[convention] 452 ]Convention <<[convention]
428 ]Variables <<[Dictionary[]] 453 ]Variables <<[Dictionary[]]
434 ]Input Types <<[ Fold[Append[?, "Any Type"], (), inputs] ] 459 ]Input Types <<[ Fold[Append[?, "Any Type"], (), inputs] ]
435 ]Output Types <<[ Fold[Append[?, "Any Type"], (), outputs] ] 460 ]Output Types <<[ Fold[Append[?, "Any Type"], (), outputs] ]
436 ]Resume Index <<[1] 461 ]Resume Index <<[1]
437 ]Last NumParams <<[-1] 462 ]Last NumParams <<[-1]
438 ]Escape Pattern <<[p] 463 ]Escape Pattern <<[p]
464 ]Uses <<[()]
439 } 465 }
440 466
441 Set Input Type@C Function[func,type,input num:out] 467 Set Input Type@C Function[func,type,input num:out]
442 { 468 {
443 out <- [func]Input Types <<[ [[func]Input Types >>]Set[input num, type] ] 469 out <- [func]Input Types <<[ [[func]Input Types >>]Set[input num, type] ]
650 { 676 {
651 out <- func 677 out <- func
652 result op <- Field Ref[var,field] 678 result op <- Field Ref[var,field]
653 } 679 }
654 680
655 681 Global Reference@C Function[func,store,var:out]
656 682 {
683
684 estore <- Escape Rhope Name[store,[func]Escape Pattern >>]
685 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 >>]]
686 }
687
688 Set Global@C Function[func,store,var,src:out]
689 {
690 estore <- Escape Rhope Name[store,[func]Escape Pattern >>]
691 cell pointer <- [[["lv_"]Append[Escape Rhope Name[[func]Name >>,[func]Escape Pattern >>]]]Append["->gs_"]]Append[estore]
692 out <- [[[[func]Add Statement[[["tmp = copy_object("]Append[[cell pointer]Append["->local_data"]]]Append[")"]]
693 ]Add Statement[ [[[["((mutt_"]Append[estore]]Append[" *)tmp)->gs_"]]Append[Escape Rhope Name[var, [func]Escape Pattern >>]]]Append[[" = "]Append[[src]Make Op[func]]] ]
694 ]Add Statement[[cell pointer]Append["->local_data = tmp"]]
695 ]Add Statement[[cell pointer]Append["->local_version++"]]
696 }
657 697
658 Set Field Null@C Function[func,var,field:out] 698 Set Field Null@C Function[func,var,field:out]
659 { 699 {
660 out <- [func]Add Statement[ [[func]Field Result[var,field]]Append[" = NULL"] ] 700 out <- [func]Add Statement[ [[func]Field Result[var,field]]Append[" = NULL"] ]
661 } 701 }
981 } 1021 }
982 _Var Defs C[string,type,varname,p:out] 1022 _Var Defs C[string,type,varname,p:out]
983 { 1023 {
984 out <- [[[string]Append[ ["\t"]Append[Rhope Type to C[type,p]] ]]Append[[" "]Append[Escape Rhope Name[varname,p]]]]Append[";\n"] 1024 out <- [[[string]Append[ ["\t"]Append[Rhope Type to C[type,p]] ]]Append[[" "]Append[Escape Rhope Name[varname,p]]]]Append[";\n"]
985 } 1025 }
1026 _Global Cell Defs C[func,p,string,store:out]
1027 {
1028 out <- [string]Append[ [["\ttrans_cell *gs_"]Append[Escape Rhope Name[store,p]]]Append[";\n"] ]
1029 }
986 1030
987 1031
988 Definitions@C Function[func:out] 1032 Definitions@C Function[func:out]
989 { 1033 {
990 Print[["Definitions@C Function: "]Append[[func]Name >>]] 1034 Print[["Definitions@C Function: "]Append[[func]Name >>]]
991 { 1035 {
992 If[ [[[func]Convention >>] = ["rhope"]] And [[ [[[func]Variables >>]Length]+[[[func]Outputs >>]Length] ] > [0]] ] 1036 If[ [[[func]Convention >>] = ["rhope"]] And [[ [[[func]Variables >>]Length]+[[[func]Outputs >>]Length] ] > [0]] ]
993 { 1037 {
994 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"] 1038 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"]
995 }{ 1039 }{
996 localtype <- "" 1040 localtype <- ""
997 } 1041 }
998 1042
999 If[ [[func]Convention >>] = ["rhope"] ] 1043 If[ [[func]Convention >>] = ["rhope"] ]
1058 { 1102 {
1059 out <- text 1103 out <- text
1060 }{ 1104 }{
1061 out <- [text]Append[[[["\tParam("]Append[String[input num]]]Append[ [","]Append[ [[func]Type Registry >>]Type ID[typename] ] ]]Append[")\n"]] 1105 out <- [text]Append[[[["\tParam("]Append[String[input num]]]Append[ [","]Append[ [[func]Type Registry >>]Type ID[typename] ] ]]Append[")\n"]]
1062 } 1106 }
1107 }
1108
1109 Find Trans Cell@C Function[func,text,store:out]
1110 {
1111 estore <- Escape Rhope Name[store,[func]Escape Pattern >>]
1112 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"]] ]
1063 } 1113 }
1064 1114
1065 Text@C Function[func:out] 1115 Text@C Function[func:out]
1066 { 1116 {
1067 Print[["Text@C Function: "]Append[[func]Name >>]] 1117 Print[["Text@C Function: "]Append[[func]Name >>]]
1079 type <- "Func" 1129 type <- "Func"
1080 cname <- Val[fname] 1130 cname <- Val[fname]
1081 } 1131 }
1082 fname <- Escape Rhope Name[[func]Name >>,[func]Escape Pattern >>] 1132 fname <- Escape Rhope Name[[func]Name >>,[func]Escape Pattern >>]
1083 param check <- Fold[Check Param Type C[?, ?, ?, func], "", [func]Input Types >>] 1133 param check <- Fold[Check Param Type C[?, ?, ?, func], "", [func]Input Types >>]
1084 If[ [ [[[func]Variables >>]Length]+[[[func]Outputs >>]Length] ] = [0] ] 1134 If[ [ [[[[func]Variables >>]Length]+[[[func]Outputs >>]Length]]+[[[func]Uses >>]Length] ] = [0] ]
1085 { 1135 {
1086 out <- [[[[[[[[ [type]Append["NoLocals("] 1136 out <- [[[[[[[[ [type]Append["NoLocals("]
1087 ]Append[cname] 1137 ]Append[cname]
1088 ]Append[",\n\tNumParams "] 1138 ]Append[",\n\tNumParams "]
1089 ]Append[ String[[[func]Inputs >>]Length] ] 1139 ]Append[ String[[[func]Inputs >>]Length] ]
1097 { 1147 {
1098 freecall <- "" 1148 freecall <- ""
1099 }{ 1149 }{
1100 freecall <- "\n\tFreeCall\n" 1150 freecall <- "\n\tFreeCall\n"
1101 } 1151 }
1102 out <- [[[[[[[[[[ [type]Append["("] 1152 If[[[func]Uses >>]Length]
1153 {
1154 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"]
1155 init trans <- [[Fold[Find Trans Cell[func,?], begin trans, [func]Uses >>]
1156 ]Append[[["transretry_"]Append[fname]]Append[":\n"]]
1157 ]Append["\tfor(idx = 0; idx < cdata->num_params; ++idx) { add_ref(cdata->params[idx]); }\n"]
1158 //TODO: Figure out whether this is a readonly or write transaction
1159 commit trans <- [[["\tif(!commit_transaction(ct, 0)) { prep_retry(ct); goto transretry_"]Append[fname]]Append["; }\n"]
1160 ]Append["\tfor(idx = 0; idx < cdata->num_params; ++idx) { release_ref(cdata->params[idx]); }\n"]
1161 }{
1162 init trans <- ""
1163 commit trans <- ""
1164 }
1165 out <- [[[[[[[[[[[[ [type]Append["("]
1103 ]Append[cname] 1166 ]Append[cname]
1104 ]Append[",\n\tNumParams "] 1167 ]Append[",\n\tNumParams "]
1105 ]Append[ String[[[func]Inputs >>]Length] ] 1168 ]Append[ String[[[func]Inputs >>]Length] ]
1106 ]Append[")\n\n"] 1169 ]Append[")\n\n"]
1107 ]Append[param check] 1170 ]Append[param check]
1171 ]Append[init trans]
1108 ]Append[ [[func]Statements >>]Join[""] ] 1172 ]Append[ [[func]Statements >>]Join[""] ]
1109 ]Append[freecall] 1173 ]Append[freecall]
1174 ]Append[commit trans]
1110 ]Append[[func]Set Outputs] 1175 ]Append[[func]Set Outputs]
1111 ]Append[[["EndFunc("]Append[fname]]Append[")\n"]] 1176 ]Append[[["EndFunc("]Append[fname]]Append[")\n"]]
1112 ]Append["DISPATCH"] 1177 ]Append["DISPATCH"]
1113 } 1178 }
1114 }{ 1179 }{
1127 Functions 1192 Functions
1128 Method Registry 1193 Method Registry
1129 Field Registry 1194 Field Registry
1130 Type Registry 1195 Type Registry
1131 Libraries 1196 Libraries
1197 Global Stores
1132 Escape Pattern 1198 Escape Pattern
1133 } 1199 }
1134 1200
1135 C Program[:out] 1201 C Program[:out]
1136 { 1202 {
1137 p <- Pattern[("_", "@", " ", ":", "?", "+", "-", "*", "/", "<", ">", "(", ")", "!", "=", "'", 1203 p <- Pattern[("_", "@", " ", ":", "?", "+", "-", "*", "/", "<", ">", "(", ")", "!", "=", "'",
1138 "\"", "\t", ",", ".", "\n", "{", "}", "[", "]", "#", "\\", "\r", ";", "&", "|", "%", "^", "`", "~")] 1204 "\"", "\t", ",", ".", "\n", "{", "}", "[", "]", "#", "\\", "\r", ";", "&", "|", "%", "^", "`", "~")]
1139 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] 1205 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[]]
1140 } 1206 }
1141 1207
1142 Supported Number Types@C Program[program:out] 1208 Supported Number Types@C Program[program:out]
1143 { 1209 {
1144 out <- ("Int8","Int16","Int32","Int64","UInt8","UInt16","UInt32","UInt64") 1210 out <- ("Int8","Int16","Int32","Int64","UInt8","UInt16","UInt32","UInt64")
1371 }{ 1437 }{
1372 out <- text 1438 out <- text
1373 } 1439 }
1374 } 1440 }
1375 1441
1442 _Global Var Init C[text,value,name,storevar,type reg:out]
1443 {
1444 out <- [[[[[text]Append[storevar]]Append[Escape Rhope Name[name, [type reg]Escape Pattern >>]]]Append[" = "]]Append[Const Construct C[value, type reg]]]Append[";\n"]
1445 }
1446
1447 _Global Store Inits C[text,store,type reg:out]
1448 {
1449 estore <- Escape Rhope Name[[store]Name >>, [type reg]Escape Pattern >>]
1450 out <- Fold[_Global Var Init C[?,?,?, [[[["\t((mutt_"]Append[estore]]Append[" *)(gs_"]]Append[estore]]Append["->data))->gs_"], type reg], text, [store]Vars >>]
1451 }
1452
1376 _Dispatch Switch Sub[text, num, name:out] 1453 _Dispatch Switch Sub[text, num, name:out]
1377 { 1454 {
1378 out <- [[[[[text 1455 out <- [[[[[text
1379 ]Append["\tResumeEntry("] 1456 ]Append["\tResumeEntry("]
1380 ]Append[String[num]] 1457 ]Append[String[num]]
1531 #include \"blueprint.h\" 1608 #include \"blueprint.h\"
1532 #include \"array.h\" 1609 #include \"array.h\"
1533 #include \"worker.h\" 1610 #include \"worker.h\"
1534 #include \"bool.h\" 1611 #include \"bool.h\"
1535 #include <sys/time.h>\n\n" 1612 #include <sys/time.h>\n\n"
1536 out <- [[[[[[[[[[[[[[[[[[headers 1613 out <- [[[[[[[[[[[[[[[[[[[[[headers
1537 ]Append[[program]Dispatch[all methods]] 1614 ]Append[[program]Dispatch[all methods]]
1538 ]Append[[[program]Type Registry >>]Type Defs] 1615 ]Append[[[program]Type Registry >>]Type Defs]
1616 ]Append[Fold[_Global Type Defs C[?,?,p], "", [program]Global Stores >>]]
1539 ]Append[Fold[_Consts C Program[?,?,?,p], 1617 ]Append[Fold[_Consts C Program[?,?,?,p],
1540 Fold[_Defs C Program[?], "", [program]Functions >>], 1618 Fold[_Defs C Program[?], "", [program]Functions >>],
1541 constants]] 1619 constants]]
1542 ]Append[Fold[_Text C Program[?, ?, [program]Type Registry >>], "", Filter[[program]Functions >>, Not Native[?]]]] 1620 ]Append[Fold[_Text C Program[?, ?, [program]Type Registry >>], "", Filter[[program]Functions >>, Not Native[?]]]]
1543 ]Append["\n 1621 ]Append["\n
1554 #ifdef ENABLE_PROFILING 1632 #ifdef ENABLE_PROFILING
1555 struct timeval time; 1633 struct timeval time;
1556 #endif 1634 #endif
1557 uint16_t resume,idx, vcparam_offset, last_vcparam; 1635 uint16_t resume,idx, vcparam_offset, last_vcparam;
1558 context * ct; 1636 context * ct;
1637 void *tmp;
1559 calldata * cdata, *temp_cdata, *my_cdata; 1638 calldata * cdata, *temp_cdata, *my_cdata;
1560 DispatchVar 1639 DispatchVar
1561 FuncDef(Build) 1640 FuncDef(Build)
1562 FuncDef(BlueprintSP_Of) 1641 FuncDef(BlueprintSP_Of)
1563 FuncDef(ID) 1642 FuncDef(ID)
1650 { 1729 {
1651 blueprint * bp; 1730 blueprint * bp;
1652 int numret; 1731 int numret;
1653 int idx; 1732 int idx;
1654 object * inout[3]; 1733 object * inout[3];
1655 register_builtin_types();\n\n"] 1734 register_builtin_types();
1735 register_type_byid(TYPE_MUTABLEGLOBAL, sizeof(mutable_object)-sizeof(object), NULL, NULL, NULL);\n\n"]
1656 ]Append[ [[program]Type Registry >>]Type Inits[[program]Method Registry >>, [program]Field Registry >>] ] 1736 ]Append[ [[program]Type Registry >>]Type Inits[[program]Method Registry >>, [program]Field Registry >>] ]
1737 ]Append[Fold[_Global Type Inits C[?, ?, p], "", [program]Global Stores >>]]
1657 ]Append[Fold[_Set Consts C Program[?, ?, ?, [program]Type Registry >>], "", constants]] 1738 ]Append[Fold[_Set Consts C Program[?, ?, ?, [program]Type Registry >>], "", constants]]
1658 ]Append[Fold[_Set Late Consts C[?, ?, ?, [program]Type Registry >>], "", constants]] 1739 ]Append[Fold[_Set Late Consts C[?, ?, ?, [program]Type Registry >>], "", constants]]
1740 ]Append[Fold[_Global Store Inits C[?, ?, [program]Type Registry >>], "", [program]Global Stores >>]]
1659 ]Append[Fold[Init Type Names[?, ?, ?, [program]Type Registry >>], "", [[program]Type Registry >>]Lookup >>]] 1741 ]Append[Fold[Init Type Names[?, ?, ?, [program]Type Registry >>], "", [[program]Type Registry >>]Lookup >>]]
1660 ]Append[" 1742 ]Append["
1661 rhope(FUNC_List, inout, 0, 1); 1743 rhope(FUNC_List, inout, 0, 1);
1662 for (idx = 0; idx < argc; ++idx) 1744 for (idx = 0; idx < argc; ++idx)
1663 { 1745 {