# HG changeset patch # User Mike Pavone # Date 1275255276 14400 # Node ID e1fd6d244f146fd47ca1f41d396e86a250325735 # Parent 04baa003de5a0b3ab39bafbbf2598f327e150bb8 Some progress on updating C backend to work with new C strategy diff -r 04baa003de5a -r e1fd6d244f14 cbackend.rhope --- a/cbackend.rhope Wed May 05 22:12:23 2010 -0400 +++ b/cbackend.rhope Sun May 30 17:34:36 2010 -0400 @@ -1,5 +1,5 @@ Import extendlib.rhope -Import backendutils.rhope +Import backendutils.rhope Import number.rhope Blueprint Blueprint Def @@ -137,8 +137,8 @@ If[[[type]Name >>] = ["Array"]] { [("Naked","Raw Pointer")]Find[variant] - { - /* + { + /* //Below code assumes that paramaterized types are implemented pre param <- [[type]Params >>]Index[0] {} { pre param <- Type Instance["Any Type"] } @@ -153,8 +153,8 @@ }{ out <- [child type]Append[" *"] array <- "" - } */ - out <- "void *" + } */ + out <- "void *" array <- "" }{ typename <- "Array" @@ -238,16 +238,16 @@ If[[[[ctype]Fields >>]Length] = [1]] { out <- [[[_Type Def C Type["typedef struct {\n\tobject _SP_header;\n\t", [[ctype]Fields >>]Index[0]]]Append["\n} t_"]]Append[[ctype]Name >>]]Append[";"] - }{ - //HACK!!! - If[[[ctype]Name >>]=["Blueprint"]] - { - out <- "" - }{ - If[[[ctype]Name >>]=["Array"]] - { oend <- "\nMObject(" } + }{ + //HACK!!! + If[[[ctype]Name >>]=["Blueprint"]] + { + out <- "" + }{ + If[[[ctype]Name >>]=["Array"]] + { oend <- "\nMObject(" } { oend <- "\nObject(" } - out <- [Fold["_Type Def C Type", "OBegin", [ctype]Fields >>]]Append[ [[oend]Append[Escape Rhope Name[[ctype]Name >>]]]Append[")"] ] + out <- [Fold["_Type Def C Type", "OBegin", [ctype]Fields >>]]Append[ [[oend]Append[Escape Rhope Name[[ctype]Name >>]]]Append[")"] ] } } } @@ -265,10 +265,10 @@ } Type Init@C Type[ctype,id,method reg,field reg:out] -{ - If[[[ctype]Name >>]=["Array"]] - { size <- "-1" } - { +{ + If[[[ctype]Name >>]=["Array"]] + { size <- "-1" } + { [("Int64","Int32","Int16","Int8")]Find[[ctype]Name >>] { [[ctype]Name >>]Slice[3] {} @@ -278,21 +278,21 @@ { [[ctype]Name >>]Slice[4] {} { typename <- [["uint"]Append[~]]Append["_t"] } - }{ - If[[[ctype]Name >>]=["Blueprint"]] - { typename <- "blueprint *" } - { - If[[[ctype]Name >>]=["Boolean"]] - { typename <- "int32_t" } - { typename <- ["nt_"]Append[Escape Rhope Name[[ctype]Name >>]] } - } + }{ + If[[[ctype]Name >>]=["Blueprint"]] + { typename <- "blueprint *" } + { + If[[[ctype]Name >>]=["Boolean"]] + { typename <- "int32_t" } + { typename <- ["nt_"]Append[Escape Rhope Name[[ctype]Name >>]] } + } } - } - size <- [["sizeof("]Append[typename]]Append[")"] + } + size <- [["sizeof("]Append[typename]]Append[")"] } start <- [["\tbp = register_type_byid(" ]Append[id] - ]Append[ + ]Append[ [[", "]Append[size] ]Append[ [", (special_func)"]Append[ @@ -328,7 +328,7 @@ ]Set["Boolean", "TYPE_BOOLEAN"] ]Set["Float32", "TYPE_FLOAT32"] ]Set["Float64", "TYPE_FLOAT64"] - ]Set["Real Number", "TYPE_FLOAT64"] + ]Set["Real Number", "TYPE_FLOAT64"] ]Set["Blueprint", "TYPE_BLUEPRINT"] ]Set["Array", "TYPE_ARRAY"] ]Set["Method Missing Exception", "TYPE_METHODMISSINGEXCEPTION"] @@ -355,7 +355,7 @@ 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 >>] + out <- Fold[[[["_Type Inits C"]Set Input[0, reg]]Set Input[1, method reg]]Set Input[2, field reg], "", [reg]Definitions >>] { Print["Type inits got output"] } } @@ -363,13 +363,13 @@ { name <- [def]Name >> [[reg]Lookup >>]Index[name] - { - [[reg]Definitions >>]Index[name] + { + [[reg]Definitions >>]Index[name] { - out <- reg - }{ - Print[["Registered def for "]Append[name]] - out <- [reg]Definitions <<[[[reg]Definitions >>]Set[name, def]] + out <- reg + }{ + Print[["Registered def for "]Append[name]] + out <- [reg]Definitions <<[[[reg]Definitions >>]Set[name, def]] } }{ out <- [[[reg]Lookup <<[ [[reg]Lookup >>]Set[name, ["TYPE_FIRST_USER+"]Append[[reg]Next ID >>]] ] @@ -380,10 +380,10 @@ Type ID@C Type Registry[reg,name:out,notfound] { - out <- [[reg]Lookup >>]Index[name] {} - { - ,notfound <- If[[name]=["Any Type"]] - { out <- "0" } + out <- [[reg]Lookup >>]Index[name] {} + { + ,notfound <- If[[name]=["Any Type"]] + { out <- "0" } } } @@ -394,8 +394,8 @@ { Print["found type"] yep,nope <- If[[[[~]Fields >>]Length] = [1]] - }{ - Pretty Print[reg, ""] + }{ + Pretty Print[reg, ""] } } @@ -413,6 +413,7 @@ Constants Input Types Output Types + Resume Index } C Function[name,inputs,outputs,convention:out] @@ -422,7 +423,7 @@ C Function With Registry[name,inputs,outputs,convention,registry,field reg,type reg:out] { - out <- [[[[[[[[[[[[Build["C Function"] + out <- [[[[[[[[[[[[[Build["C Function"] ]Name <<[name] ]Inputs <<[inputs] ]Outputs <<[outputs] @@ -435,6 +436,7 @@ ]Constants <<[Dictionary[]] ]Input Types <<[ Fold[["Append"]Set Input[1, "Any Type"], (), inputs] ] ]Output Types <<[ Fold[["Append"]Set Input[1, "Any Type"], (), outputs] ] + ]Resume Index <<[1] } Set Input Type@C Function[func,type,input num:out] @@ -497,46 +499,46 @@ Divide@C Function[func,source1,source2,dest:out] { out <- [func]Add Operator Statement[source1,source2,dest," / "] -} - +} + DoLShift@C Function[func,source1,source2,dest:out] { out <- [func]Add Operator Statement[source1,source2,dest," << "] -} - +} + DoRShift@C Function[func,source1,source2,dest:out] { out <- [func]Add Operator Statement[source1,source2,dest," >> "] -} - -CompLess@C Function[func,source1,source2,dest:out] -{ - out <- [func]Add Operator Statement[source1,source2,dest," < "] -} - -CompGreater@C Function[func,source1,source2,dest:out] -{ - out <- [func]Add Operator Statement[source1,source2,dest," > "] -} - -CompEqual@C Function[func,source1,source2,dest:out] -{ - out <- [func]Add Operator Statement[source1,source2,dest," == "] -} - -CompLessEqual@C Function[func,source1,source2,dest:out] -{ - out <- [func]Add Operator Statement[source1,source2,dest," <= "] -} - -CompGreaterEqual@C Function[func,source1,source2,dest:out] -{ - out <- [func]Add Operator Statement[source1,source2,dest," >= "] -} - -CompNotEqual@C Function[func,source1,source2,dest:out] -{ - out <- [func]Add Operator Statement[source1,source2,dest," != "] +} + +CompLess@C Function[func,source1,source2,dest:out] +{ + out <- [func]Add Operator Statement[source1,source2,dest," < "] +} + +CompGreater@C Function[func,source1,source2,dest:out] +{ + out <- [func]Add Operator Statement[source1,source2,dest," > "] +} + +CompEqual@C Function[func,source1,source2,dest:out] +{ + out <- [func]Add Operator Statement[source1,source2,dest," == "] +} + +CompLessEqual@C Function[func,source1,source2,dest:out] +{ + out <- [func]Add Operator Statement[source1,source2,dest," <= "] +} + +CompGreaterEqual@C Function[func,source1,source2,dest:out] +{ + out <- [func]Add Operator Statement[source1,source2,dest," >= "] +} + +CompNotEqual@C Function[func,source1,source2,dest:out] +{ + out <- [func]Add Operator Statement[source1,source2,dest," != "] } Move@C Function[func,psource,pdest:out] @@ -561,7 +563,7 @@ Release@C Function[func,psource:out] { - source <- [psource]Make Op[func] + source <- [psource]Make Op[func] Print[["Release: "]Append[source]] out <- [func]Add Statement[[["release_ref("]Append[source]]Append[")"]] } @@ -588,13 +590,13 @@ type <- [[func]Input Types >>]Index[~] }{ type <- [[func]Variables >>]Index[var] {} - { - [[func]Outputs >>]Find[var] - { - type <- [[func]Output Types >>]Index[~] + { + [[func]Outputs >>]Find[var] + { + type <- [[func]Output Types >>]Index[~] }{ //Does it make sense for us to do this? - type <- Type Instance["Any Type"] + type <- Type Instance["Any Type"] } } } @@ -666,35 +668,36 @@ ]Append[", &"] ]Append[dest] ]Append[")"] ] -} - -Get Raw Pointer@C Function[func,psource,pdest:out] -{ +} + +Get Raw Pointer@C Function[func,psource,pdest:out] +{ dest <- [pdest]Make Op[func] - source <- [psource]Make Op[func] - out <- [func]Add Statement[ [[[dest]Append[" = &("]]Append[source]]Append["->payload)"] ] -} - -Array Raw Pointer@C Function[func,psource,pdest:out] -{ + source <- [psource]Make Op[func] + out <- [func]Add Statement[ [[[dest]Append[" = &("]]Append[source]]Append["->payload)"] ] +} + +Array Raw Pointer@C Function[func,psource,pdest:out] +{ dest <- [pdest]Make Op[func] - source <- [psource]Make Op[func] - out <- [func]Add Statement[ [[[dest]Append[" = ((char *)"]]Append[source]]Append[")+ sizeof(t_Array)"] ] -} + source <- [psource]Make Op[func] + out <- [func]Add Statement[ [[[dest]Append[" = ((char *)"]]Append[source]]Append[")+ sizeof(t_Array)"] ] +} _Function Arg C[func,val,inputnum:out] { - out <- [func]Add Statement[ - [[["call->params[" + out <- [func]Add Raw Line[ + [[[["SetParam(" ]Append[inputnum] - ]Append["] = "] + ]Append[", "] ]Append[val] + ]Append[")"] ] } Method Call@C Function[func,method,args:out] { - out <- [func]Func Base[[[func]Method Registry >>]Method ID[method],args, "MCall"] + out <- [func]Call[method,args] } Call@C Function[func,name,args:out] @@ -703,29 +706,36 @@ } Func Base@C Function[func,tocall,args,type:out] -{ - Pretty Print[args, ""] - { +{ rargs <- Map[args, ["Make Op"]Set Input[1, func]] - out <- [Fold["_Function Arg C", func, rargs] - ]Add Raw Line[[[[[ [type]Append["("] ]Append[tocall]]Append[", "]]Append[ [rargs]Length ]]Append[")"]] + out <- [[Fold["_Function Arg C", [func]Add Raw Line[ [["PrepCall("]Append[[rargs]Length]]Append[")"] ], rargs] + ]Add Raw Line[ + [[[[[[[[[type]Append["("] + ]Append[tocall] + ]Append[", "] + ]Append[[rargs]Length] + ]Append[", "] + ]Append[[func]Resume Index >>] + ]Append[", "] + ]Append[Escape Rhope Name[[func]Name >>]] + ]Append[")"]] + ]Resume Index <<[ [[func]Resume Index >>]+[1] ] +} + +Call Foreign@C Function[func,name,language,args,store result:out] +{ + rargs <- Map[args, ["Make Op"]Set Input[1, func]] + //Assume language = "C" for now + base <- [[[name]Append["("]]Append[ Join[rargs, ", "] ]]Append[")"] + ,do store <- If[[Type Of[store result]]=["String"]] + { + ,do store <- If[[store result]=[""]] + { stmt <- Val[base] } } -} - -Call Foreign@C Function[func,name,language,args,store result:out] -{ - rargs <- Map[args, ["Make Op"]Set Input[1, func]] - //Assume language = "C" for now - base <- [[[name]Append["("]]Append[ Join[rargs, ", "] ]]Append[")"] - ,do store <- If[[Type Of[store result]]=["String"]] - { - ,do store <- If[[store result]=[""]] - { stmt <- Val[base] } - } - - Val[do store] - { stmt <- [[Make Op[store result, func]]Append[" = "]]Append[base] } - out <- [func]Add Statement[stmt] + + Val[do store] + { stmt <- [[Make Op[store result, func]]Append[" = "]]Append[base] } + out <- [func]Add Statement[stmt] } Field Base@C Function[func,field,args,type:out] @@ -768,16 +778,16 @@ }{ out <- Escape Rhope Name[op] } -} - -Resolve Output@C Function[func,name:out] -{ +} + +Resolve Output@C Function[func,name:out] +{ If[[[func]Convention >>] = ["rhope"]] { out <- ["locals->"]Append[Escape Rhope Name[name]] }{ out <- Escape Rhope Name[name] - } + } } Instruction Stream@C Function[func:out] @@ -806,9 +816,9 @@ } If Null Else@C Function[func,left,right:out] -{ - check <- [[Make Condition[left]]Strip Addref]Make Op[func] - l <- [left]Make Op[func] +{ + check <- [[Make Condition[left]]Strip Addref]Make Op[func] + l <- [left]Make Op[func] r <- [right]Make Op[func] out <- [[[[[["(" ]Append[check] @@ -849,8 +859,8 @@ Definitions@C Function[func:out] -{ - Print[["Definitions@C Function: "]Append[[func]Name >>]] +{ + Print[["Definitions@C Function: "]Append[[func]Name >>]] { If[ [[[func]Convention >>] = ["rhope"]] And [[ [[[func]Variables >>]Length]+[[[func]Outputs >>]Length] ] > [0]] ] { @@ -875,7 +885,7 @@ }{ proto <- [[func]Naked Proto]Append[";\n"] } - out <- [localtype]Append[proto] + out <- [localtype]Append[proto] } } @@ -928,7 +938,7 @@ } Text@C Function[func:out] -{ +{ Print[["Text@C Function: "]Append[[func]Name >>]] If[ [[func]Convention >>] = ["rhope"] ] { @@ -973,25 +983,25 @@ Functions Method Registry Field Registry - Type Registry + Type Registry Libraries } C Program[:out] { out <- [[[[[Build["C Program"]]Functions <<[Dictionary[]]]Method Registry <<[C Method Registry[]]]Type Registry <<[C Type Registry[]]]Field Registry <<[C Field Registry[]]]Libraries <<[Dictionary[]] -} - -Link@C Program[program,language,library:out] -{ - If[[library] = ["runtime"]] - { - out <- program - }{ - langlibs <- [[program]Libraries >>]Index[language] {} - { langlibs <- Dictionary[] } - out <- [program]Libraries <<[ [[program]Libraries >>]Set[language, [langlibs]Set[library, Yes]] ] - } +} + +Link@C Program[program,language,library:out] +{ + If[[library] = ["runtime"]] + { + out <- program + }{ + langlibs <- [[program]Libraries >>]Index[language] {} + { langlibs <- Dictionary[] } + out <- [program]Libraries <<[ [[program]Libraries >>]Set[language, [langlibs]Set[library, Yes]] ] + } } Register Type@C Program[program,def:out] @@ -1048,59 +1058,107 @@ } _Set Consts C Program[text,value,name,type reg:out] -{ - Print[["_Set Consts: "]Append[valtype]] +{ + Print[["_Set Consts: "]Append[valtype]] Pretty Print[value, "_Set Consts: "] //TODO: Support more constant types 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 <- [text]Append[[[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = make_Int32("]]Append[value]]Append[");\n"]] { Print["_Set Consts got output integer"] } }{ 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"]] + out <- [text]Append[[[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = make_Blueprint("]]Append[typeid]]Append[");\n"]] { Print["_Set Consts got output blueprint"] } }{ If[[valtype] = ["Yes No"]] { If[value] { - out <- [text]Append[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = (object *)val_yes;\n"]] + out <- [text]Append[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = (object *)val_yes;\n"]] { Print["_Set Consts got output yes"] } }{ - out <- [text]Append[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = (object *)val_no;\n"]] + out <- [text]Append[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = (object *)val_no;\n"]] { Print["_Set Consts got output no"] } } - }{ - If[[valtype] = ["Machine Integer"]] - { - If[[value]Signed? >>] - { 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 - } + }{ + If[[valtype] = ["Machine Integer"]] + { + If[[value]Signed? >>] + { 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 + } } } } -} - -_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 - } +} + +_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 + } +} + +_Dispatch Switch Sub[text, num, name:out] +{ + out <- [[[[[[[[[text + ]Append["\tcase RES_"] + ]Append[num] + ]Append["_"] + ]Append[name] + ]Append[": goto r"] + ]Append[num] + ]Append["_"] + ]Append[name] + ]Append[";\\\n"] +} + +_Dispatch Switch[text,func,raw name:out] +{ + name <- Escape Rhope Name[raw name] + out <- [[text]Append[ [[[["\tcase FUNC_"]Append[name]]Append[": goto f_"]]Append[name]]Append[";\\\n"] ] + ]Append[Fold[["_Dispatch Switch Sub"]Set Input[2, name], "", Range[1, [func]Resume Index >>]]] +} + +_Dispatch Enum Sub[text, num, name:out] +{ + out <- [[[[[text + ]Append["\tRES_"] + ]Append[num] + ]Append["_"] + ]Append[name] + ]Append[",\n"] +} + +_Dispatch Enum[text,func,raw name:out] +{ + name <- Escape Rhope Name[raw name] + out <- [[text]Append[ [["\tFUNC_"]Append[name]]Append[",\n"] ] + ]Append[Fold[["_Dispatch Enum Sub"]Set Input[2, name], "", Range[1, [func]Resume Index >>]]] +} + +Dispatch@C Program[program:out] +{ + out <- [[[[["typedef enum {\n" + ]Append[Fold["_Dispatch Enum", "", [program]Functions >>]] + ]Append["\tEND\n} funcids;\n\n"] + ]Append["#define DISPATCH switch(func) { \\\n"] + ]Append[Fold["_Dispatch Switch", "", [program]Functions >>]] + ]Append["\tcase END: goto DO_END;\\\n}\n\n"] } Text@C Program[program:out] @@ -1113,38 +1171,25 @@ #include \"context.h\" #include \"func.h\" #include \"integer.h\" -#include \"blueprint.h\" +#include \"blueprint.h\" #include \"array.h\" #include \"bool.h\"\n\n" - out <- [[[[[[[headers + out <- [[[[[[[[headers + ]Append[[program]Dispatch] ]Append[[[program]Type Registry >>]Type Defs] ]Append[Fold[["_Text C Program"]Set Input[2, [program]Type Registry >>], Fold["_Consts C Program", Fold["_Defs C Program", "", [program]Functions >>], constants ], [program]Functions >>]] - ]Append["#include \"builtin.c\"\n#include \"array.c\"\n\nint main(int argc, char **argv) + ]Append["\n\nint main(int argc, char **argv) { - returntype ret; - calldata *cdata; - context * ct; - blueprint * bp; register_builtin_types();\n\n"] ]Append[ [[program]Type Registry >>]Type Inits[[program]Method Registry >>, [program]Field Registry >>] ] - ]Append[Fold[["_Set Consts C Program"]Set Input[3, [program]Type Registry >>], "", constants]] + ]Append[Fold[["_Set Consts C Program"]Set Input[3, [program]Type Registry >>], "", constants]] ]Append[Fold[["_Set Late Consts C"]Set Input[3, [program]Type Registry >>], "", constants]] ]Append[" - ct = new_context(); - cdata = alloc_cdata(ct, 0); - cdata->num_params = 0; - cdata->resume = 0; - ret = f_Main(cdata); - while(ret == TAIL_RETURN) - ret = cdata->tail_func(cdata); - if(ret == EXCEPTION_RETURN) { - puts(\"Exception!\"); - return -1; - } + rhope(FUNC_Main); return 0; }\n\n"]