Mercurial > repos > rhope
diff cbackend.rhope @ 35:3498713c3dc9
C backend portion of supporting user defined types is done, but untested. Parser/NWorker still need a fair bit of work
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Wed, 30 Sep 2009 23:55:04 -0400 |
parents | df038cef648b |
children | 495dddadd058 |
line wrap: on
line diff
--- a/cbackend.rhope Wed Sep 30 01:25:03 2009 -0400 +++ b/cbackend.rhope Wed Sep 30 23:55:04 2009 -0400 @@ -91,11 +91,15 @@ Name Fields Methods + Init + Copy + Cleanup + } C Type[name:out] { - out <- [[[Build["C Type"]]Name <<[name]]Fields <<[()]]Methods <<[()] + out <- [[[[[[Build["C Type"]]Name <<[name]]Fields <<[()]]Methods <<[()]]Init <<["NULL"]]Copy <<["NULL"]]Cleanup <<["NULL"] } Add Field@C Type[ctype,name,type:out] @@ -122,15 +126,15 @@ { If[[typename] = ["Any Type"]] { - ctype <- "struct object" + out <- "struct object *" }{ ctype <- ["t_"]Append[Escape Rhope Name[typename]] - } - If[naked] - { - out <- Val[ctype] - }{ - out <- [ctype]Append[" * "] + If[naked] + { + out <- Val[ctype] + }{ + out <- [ctype]Append[" *"] + } } } @@ -138,7 +142,7 @@ { name <- [field]Index[0] type <- ["\n\t"]Append[Rhope Type to C[[field]Index[1], No]] - out <- [[[text]Append[type]]Append[Escape Rhope Name[name]]]Append[";"] + out <- [[[[text]Append[type]]Append[" "]]Append[Escape Rhope Name[name]]]Append[";"] } Type Def@C Type[ctype:out] @@ -154,7 +158,21 @@ Type Init@C Type[ctype,id,method reg,field reg:out] { //TODO: Handle function pointers for build/copy/destroy funcs - start <- [["\tbp = register_type_byid("]Append[id]]Append[ [[", sizeof("]Append[["t_"]Append[Escape Rhope Name[ [ctype]Name >> ]]]]Append["), NULL, NULL, NULL);"] ] + start <- [["\tbp = register_type_byid(" + ]Append[id] + ]Append[ + [[", sizeof(" + ]Append[ + ["t_"]Append[Escape Rhope Name[ [ctype]Name >> ]]] + ]Append[ + ["), "]Append[ + [ + [[[[Escape Rhope Name NU[[ctype]Init >>] + ]Append[", "] + ]Append[Escape Rhope Name NU[[ctype]Copy >> ]] + ]Append[", "] + ]Append[Escape Rhope Name NU[[ctype]Cleanup >>]] + ]Append[");"]]] ] out <- Fold[[["_Type Init C"]Set Input[0, [ctype]Name >>]]Set Input[1, method reg], start, [ctype]Methods >>] } @@ -236,17 +254,20 @@ Variables Statements Method Registry + Field Registry Constants + Input Types + Output Types } C Function[name,inputs,outputs,convention:out] { - out <- C Function With Registry[name,inputs,outputs,convention, C Method Registry[]] + out <- C Function With Registry[name,inputs,outputs,convention, C Method Registry[], C Field Registry[]] } -C Function With Registry[name,inputs,outputs,convention,registry:out] +C Function With Registry[name,inputs,outputs,convention,registry,field reg:out] { - out <- [[[[[[[[Build["C Function"] + out <- [[[[[[[[[[[Build["C Function"] ]Name <<[name] ]Inputs <<[inputs] ]Outputs <<[outputs] @@ -254,7 +275,20 @@ ]Variables <<[New@Dictionary[]] ]Statements <<[()] ]Method Registry <<[registry] + ]Field Registry <<[field reg] ]Constants <<[New@Dictionary[]] + ]Input Types <<[ Fold[["Append"]Set Input[1, "Any Type"], (), inputs] ] + ]Output Types <<[ Fold[["Append"]Set Input[1, "Any Type"], (), outputs] ] +} + +Set Input Type@C Function[func,input num,type:out] +{ + out <- [func]Input Types <<[ [[func]Input Types >>]Set[input num, type] ] +} + +Set Output Type@C Function[func,output num,type:out] +{ + out <- [func]Output Types <<[ [[func]Output Types >>]Set[output num, type] ] } Register Constant@C Function[func,name,constant:out] @@ -268,6 +302,7 @@ Allocate Var@C Function[func,name,type:out] { + Print[["Allocate Var: "]Append[name]] out <- [func]Variables <<[ [[func]Variables >>]Set[name,type] ] } @@ -318,9 +353,15 @@ AddRef@C Function[func,psource,pdest:out] { - source <- [psource]Make Op[func] - dest <- [pdest]Make Op[func] - out <- [func]Add Statement[[[[dest]Append[" = add_ref("]]Append[source]]Append[")"]] + source <- [psource]Make Op[func] + dest <- [pdest]Make Op[func] + out <- [func]Add Statement[[[[dest]Append[" = add_ref("]]Append[source]]Append[")"]] +} + +AddRef No Dest@C Function[func,psource:out] +{ + source <- [psource]Make Op[func] + out <- [func]Add Statement[[["add_ref("]Append[source]]Append[")"]] } Release@C Function[func,psource:out] @@ -332,6 +373,7 @@ Set Null@C Function[func,pdest:out] { dest <- [pdest]Make Op[func] + Print[["Set Null: "]Append[dest]] out <- [func]Add Statement[[dest]Append[" = NULL"]] } @@ -340,6 +382,22 @@ out <- [["add_ref(_const_"]Append[Escape Rhope Name[const]]]Append[")"] } +Field Result@C Function[func,var,field:out] +{ + out <- [[var]Append["->"]]Append[field] +} + +Get Field@C Function[func,var,field:out,result op] +{ + out <- func + result op <- Field Ref[var,field] +} + +Set Field Null@C Function[func,var,field:out] +{ + out <- [func]Add Statement[ [[[var]Append["->"]]Append[field]]Append[" = NULL"] ] +} + _Function Arg C[func,val,inputnum:out] { out <- [func]Add Statement[ @@ -367,6 +425,23 @@ ]Add Raw Line[[[[[ [type]Append["("] ]Append[tocall]]Append[", "]]Append[ [rargs]Length ]]Append[")"]] } +Field Base@C Function[func,field,args,type:out] +{ + rargs <- Map[args, ["Make Op"]Set Input[1, func]] + out <- [Fold["_Function Arg C", func, rargs] + ]Add Raw Line[[[ [type]Append["("] ]Append[ [[func]Field Registry >>]Field ID[field] ]]Append[")"]] +} + +Get Field Call@C Function[func,field,source:out] +{ + out <- [func]Field Base[field, [()]Append[source], "GFieldCall"] +} + +Set Field Call@C Function[func,field,object,value:out] +{ + out <- [func]Field Base[field, [[()]Append[object]]Append[value], "SFieldCall"] +} + Tail Method Call@C Function[func,method,args:out] { out <- [func]Func Base[[[func]Method Registry >>]Method ID[method],args, "TMCall"] @@ -379,11 +454,16 @@ Resolve@C Function[func,op:out] { - [[func]Inputs >>]Find[op] + If[[[func]Convention >>] = ["rhope"]] { - out <- [["cdata->params["]Append[~]]Append["]"] + [[func]Inputs >>]Find[op] + { + out <- [["cdata->params["]Append[~]]Append["]"] + }{ + out <- ["locals->"]Append[Escape Rhope Name[op]] + } }{ - out <- ["locals->"]Append[Escape Rhope Name[op]] + out <- Escape Rhope Name[op] } } @@ -423,34 +503,6 @@ ]Append[")"] } -Init Outputs@C Function[func:out] -{ - If[[[[func]Outputs >>]Length ] > [0]] - { - out <- [["\tlocals->"]Append[ [[func]Outputs >>]Join[" = NULL;\n\tlocals->"] ]]Append[" = NULL;\n"] - }{ - out <- "" - } -} - -_Release Inputs[string,inputname,inputnum:out] -{ - out <- [[[string - ]Append["\trelease_ref(cdata->params["] - ]Append[inputnum] - ]Append["]);\n"] -} - -Release Inputs@C Function[func:out] -{ - If[[[[func]Inputs >>]Length ] > [0]] - { - out <- Fold["_Release Inputs", "", [func]Inputs >>] - }{ - out <- "" - } -} - _Set Outputs C[string,inputname,inputnum:out] { out <- [string]Append[[[ [ ["\tRet("]Append[inputnum] ]Append[", locals->"]]Append[inputname]]Append[")\n"]] @@ -458,38 +510,76 @@ Set Outputs@C Function[func:out] { - If[[[[func]Outputs >>]Length ] > [0]] + If[[[func]Convention >>] = ["rhope"]] { out <- Fold["_Set Outputs C", "", [func]Outputs >>] }{ - out <- "" + [[func]Outputs >>]Index[0] + { + out <- [["\treturn "]Append[Escape Rhope Name[~]]]Append[";\n"] + }{ + out <- "" + } } } -_Output Defs C[string,varname:out] +_Output Defs C[string,varname,index,func:out] { - out <- [[[string]Append["\tobject *"]]Append[Escape Rhope Name[varname]]]Append[";\n"] + out <- [[[string]Append[ ["\t"]Append[Rhope Type to C[[[func]Output Types >>]Index[index], No]] ]]Append[Escape Rhope Name[varname]]]Append[";\n"] } _Var Defs C[string,type,varname:out] { - out <- [[[string]Append["\tobject *"]]Append[Escape Rhope Name[varname]]]Append[";\n"] + out <- [[[string]Append[ ["\t"]Append[Rhope Type to C[type, No]] ]]Append[Escape Rhope Name[varname]]]Append[";\n"] } Definitions@C Function[func:out] { - If[ [ [[[func]Variables >>]Length]+[[[func]Outputs >>]Length] ] > [0]] + If[ [[[func]Convention >>] = ["rhope"]] And [[ [[[func]Variables >>]Length]+[[[func]Outputs >>]Length] ] > [0]] ] + { + localtype <- [[[Fold[["_Output Defs C"]Set Input[3, func], Fold["_Var Defs C","typedef struct {\n", [func]Variables >>], [func]Outputs >>]]Append["} l_"]]Append[Escape Rhope Name[[func]Name >>]]]Append[";\n"] + }{ + localtype <- "" + } + + If[ [[func]Convention >>] = ["rhope"] ] { - out <- [[[Fold["_Output Defs C", Fold["_Var Defs C","typedef struct {\n", [func]Variables >>], [func]Outputs >>]]Append["} l_"]]Append[Escape Rhope Name[[func]Name >>]]]Append[";\n"] + parts <- [[func]Name >>]Split["@"] + [parts]Index[1] + { + proto <- [[[["MethodDef(" + ]Append[Escape Rhope Name[[parts]Index[0]]] + ]Append[", "] + ]Append[Escape Rhope Name[~]] + ]Append[")\n"] + }{ + proto <- [["FuncDef("]Append[Escape Rhope Name[[func]Name >>]]]Append[")\n"] + } }{ - out <- "" + proto <- [[func]Naked Proto]Append[";\n"] } + out <- [localtype]Append[proto] +} + +Naked Proto@C Function[func:out] +{ + [[func]Output Types >>]Index[0] + { + outtype <- [Rhope Type to C[~, Yes]]Append[" "] + }{ + outtype <- "void " + } + out <- [[[[outtype + ]Append[ Escape Rhope Name NU[[func]Name >>]] + ]Append["("] + ]Append[ [[func]Input Types >>]Join[", "] ] + ]Append[")"] } Text@C Function[func:out] -{ - cname <- Escape Rhope Name[[func]Name >>] +{ If[ [[func]Convention >>] = ["rhope"] ] { + cname <- Escape Rhope Name[[func]Name >>] If[ [[[func]Variables >>]Length] = [0] ] { out <- [[[[[["FuncNoLocals(" @@ -512,8 +602,13 @@ ]Append["EndFunc"] } }{ - //TODO: We need to store input and output types somewhere so we can reference them here - out <- "oops" + + out <- [[[ + Fold[["_Output Defs C"]Set Input[3, func], + Fold["_Var Defs C", [[func]Naked Proto]Append["\n{"], [func]Variables >>], [func]Outputs >>] + ]Append[[[func]Statements >>]Join[""]] + ]Append[[func]Set Outputs] + ]Append["}"] } } @@ -544,7 +639,7 @@ Create Function@C Program[program,name,inputs,outputs,convention:out] { - out <- C Function With Registry[name,inputs,outputs,convention, [program]Method Registry >>] + out <- C Function With Registry[name,inputs,outputs,convention, [program]Method Registry >>, [program]Field Registry >>] } Store Function@C Program[program,func:out]