# HG changeset patch # User Mike Pavone # Date 1275870550 14400 # Node ID d4b44ae2e34a5ba3eebed1bbaea88b1b2e6a651d # Parent 1db811fa47446f55d4e503417645161ec7901578 New variant of C backend works now diff -r 1db811fa4744 -r d4b44ae2e34a cbackend.rhope --- a/cbackend.rhope Tue Jun 01 01:13:54 2010 -0400 +++ b/cbackend.rhope Sun Jun 06 20:29:10 2010 -0400 @@ -1,6 +1,5 @@ Import extendlib.rhope Import backendutils.rhope -Import number.rhope Blueprint Blueprint Def { @@ -303,7 +302,7 @@ ]Append[", (special_func)"] ]Append[Escape Rhope Name NU[[ctype]Cleanup >>]] ]Append[");"]]] ] - out <- Fold[[["_Type Init C Field"]Set Input[0, [ctype]Name >>]]Set Input[1, field reg], Fold[[["_Type Init C"]Set Input[0, [ctype]Name >>]]Set Input[1, method reg], start, [ctype]Methods >>], [ctype]Fields >>] + out <- Val[start]//Fold[[["_Type Init C Field"]Set Input[0, [ctype]Name >>]]Set Input[1, field reg], Fold[[["_Type Init C"]Set Input[0, [ctype]Name >>]]Set Input[1, method reg], start, [ctype]Methods >>], [ctype]Fields >>] } Blueprint C Type Registry @@ -414,6 +413,7 @@ Input Types Output Types Resume Index + Last NumParams } C Function[name,inputs,outputs,convention:out] @@ -423,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] @@ -437,6 +437,7 @@ ]Input Types <<[ Fold[["Append"]Set Input[1, "Any Type"], (), inputs] ] ]Output Types <<[ Fold[["Append"]Set Input[1, "Any Type"], (), outputs] ] ]Resume Index <<[1] + ]Last NumParams <<[-1] } Set Input Type@C Function[func,type,input num:out] @@ -565,7 +566,7 @@ { source <- [psource]Make Op[func] Print[["Release: "]Append[source]] - out <- [func]Add Statement[[["release_ref("]Append[source]]Append[")"]] + out <- [func]Add Statement[[["release_ref((object *)"]Append[source]]Append[")"]] } Set Null@C Function[func,pdest:out] @@ -708,7 +709,23 @@ Func Base@C Function[func,tocall,args,type:out] { rargs <- Map[args, ["Make Op"]Set Input[1, func]] - out <- [[Fold["_Function Arg C", [func]Add Raw Line[ [["PrepCall("]Append[[rargs]Length]]Append[")"] ], rargs] + + If[[[rargs]Length] > [[func]Last NumParams >>]] + { + If[[[func]Last NumParams >>] = [-1]] + { + freed <- Val[func] + }{ + freed <- [func]Add Raw Line["FreeCall"] + } + prepped <- [[freed]Add Raw Line[ [["PrepCall("]Append[[rargs]Length]]Append[")"] ] + ]Last NumParams <<[[rargs]Length] + }{ + prepped <- Val[func] + } + + + out <- [[Fold["_Function Arg C", prepped, rargs] ]Add Raw Line[ [[[[[[[[[type]Append["("] ]Append[tocall] @@ -738,21 +755,14 @@ out <- [func]Add Statement[stmt] } -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"] + out <- [func]Func Base[Escape Rhope Name[[field]Append[" >>"]], [()]Append[source], "Call"] } Set Field Call@C Function[func,field,object,value:out] { - out <- [func]Field Base[field, [[()]Append[object]]Append[value], "SFieldCall"] + out <- [func]Func Base[Escape Rhope Name[[field]Append[" <<"]], [[()]Append[object]]Append[value], "Call"] } Tail Method Call@C Function[func,method,args:out] @@ -771,9 +781,9 @@ { [[func]Inputs >>]Find[op] { - out <- [["cdata->params["]Append[~]]Append["]"] + out <- [["my_cdata->params["]Append[~]]Append[" ]"] }{ - out <- ["locals->"]Append[Escape Rhope Name[op]] + out <- [[["lv_"]Append[Escape Rhope Name[[func]Name >>]]]Append["->"]]Append[Escape Rhope Name[op]] } }{ out <- Escape Rhope Name[op] @@ -784,7 +794,7 @@ { If[[[func]Convention >>] = ["rhope"]] { - out <- ["locals->"]Append[Escape Rhope Name[name]] + out <- [[["lv_"]Append[Escape Rhope Name[[func]Name >>]]]Append["->"]]Append[Escape Rhope Name[name]] }{ out <- Escape Rhope Name[name] } @@ -803,16 +813,17 @@ Do If@C Function[func,condition,stream:out] { cond <- [condition]Make Op[func] - out <- [Fold["_If C", [[func + out <- [[Fold["_If C", [[func ]Add Raw Line[ [["if("]Append[cond]]Append[")"] ] ]Add Raw Line["{"], [stream]Statements >>] ]Add Raw Line["}"] + ]Resume Index <<[[stream]Resume Index >>] } Result Reference@C Function[func,output:out] { - out <- [["call->params["]Append[output]]Append["]"] + out <- [["cdata->params["]Append[output]]Append["]"] } If Null Else@C Function[func,left,right:out] @@ -829,16 +840,16 @@ ]Append[")"] } -_Set Outputs C[string,inputname,inputnum:out] +_Set Outputs C[string,inputname,inputnum,func:out] { - out <- [string]Append[[[ [ ["\tRet("]Append[inputnum] ]Append[", locals->"]]Append[inputname]]Append[")\n"]] + out <- [string]Append[[[ [ ["\tRet("]Append[inputnum] ]Append[ [[", lv_"]Append[Escape Rhope Name[[func]Name >>]]]Append["->"]]]Append[inputname]]Append[")\n"]] } Set Outputs@C Function[func:out] { If[[[func]Convention >>] = ["rhope"]] { - out <- Fold["_Set Outputs C", "", [func]Outputs >>] + out <- [[[Fold[["_Set Outputs C"]Set Input[3, func], "", [func]Outputs >>]]Append["\tNumRet("]]Append[[[func]Outputs >>]Length]]Append[")\n"] }{ [[func]Outputs >>]Index[0] { @@ -864,7 +875,7 @@ { 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 <- [[[Fold[["_Output Defs C"]Set Input[3, func], Fold["_Var Defs C","typedef struct {\n", [func]Variables >>], [func]Outputs >>]]Append["} lt_"]]Append[Escape Rhope Name[[func]Name >>]]]Append[";\n"] }{ localtype <- "" } @@ -881,7 +892,7 @@ ]Append[")\n"] }{ proto <- [["FuncDef("]Append[Escape Rhope Name[[func]Name >>]]]Append[")\n"] - } */ + } */ out <- Val[localtype] }{ out <- [[func]Naked Proto]Append[";\n"] @@ -941,41 +952,50 @@ { Print[["Text@C Function: "]Append[[func]Name >>]] If[ [[func]Convention >>] = ["rhope"] ] - { - ,before <- [[func]Name >>]Get DString["@"] - { - type <- "MethodImpl" - cname <- [[[[Escape Rhope Name[before] - ]Append[", "] - ]Append[Escape Rhope Name[~]] - ]Append[", "] - ]Append[ [[func]Type Registry >>]Type ID[~] ] - }{}{}{ - type <- "Func" - cname <- Val[fname] - } + { + ,before <- [[func]Name >>]Get DString["@"] + { + type <- "MethodImpl" + cname <- [[[[Escape Rhope Name[before] + ]Append[", "] + ]Append[Escape Rhope Name[~]] + ]Append[", "] + ]Append[ [[func]Type Registry >>]Type ID[~] ] + }{}{}{ + type <- "Func" + cname <- Val[fname] + } fname <- Escape Rhope Name[[func]Name >>] param check <- Fold[["Check Param Type C"]Set Input[3, func], "", [func]Input Types >>] If[ [ [[[func]Variables >>]Length]+[[[func]Outputs >>]Length] ] = [0] ] { - out <- [[[[[[[ [type]Append["NoLocals("] + out <- [[[[[[[[ [type]Append["NoLocals("] ]Append[cname] ]Append[",\n\tNumParams "] ]Append[ [[func]Inputs >>]Length ] ]Append[")\n\n"] ]Append[param check] ]Append[ [[func]Statements >>]Join[""] ] - ]Append["EndFuncNoLocals"] + ]Append["EndFuncNoLocals\n"] + ]Append["DISPATCH"] }{ - out <- [[[[[[[[ [type]Append["("] + If[[[func]Last NumParams >>] = [-1]] + { + freecall <- "" + }{ + freecall <- "\n\tFreeCall\n" + } + out <- [[[[[[[[[[ [type]Append["("] ]Append[cname] ]Append[",\n\tNumParams "] ]Append[ [[func]Inputs >>]Length ] ]Append[")\n\n"] ]Append[param check] ]Append[ [[func]Statements >>]Join[""] ] + ]Append[freecall] ]Append[[func]Set Outputs] - ]Append[[["EndFunc("]Append[fname]]Append[")"]] + ]Append[[["EndFunc("]Append[fname]]Append[")\n"]] + ]Append["DISPATCH"] } }{ @@ -1138,17 +1158,23 @@ } _Dispatch Switch[text,func,raw name:out] -{ - If[[[func]Convention >>] = ["rhope"]] +{ + If[[[func]Convention >>] = ["rhope"]] { 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 >>]]] - }{ - out <- text + ]Append[Fold[["_Dispatch Switch Sub"]Set Input[2, name], "", Range[1, [func]Resume Index >>]]] + }{ + out <- text } } +_Dispatch Switch Methods[text,id,raw name:out] +{ + name <- Escape Rhope Name[raw name] + out <- [text]Append[ [[[["\tcase FUNC_"]Append[name]]Append[": goto f_"]]Append[name]]Append[";\\\n"] ] +} + _Dispatch Enum Sub[text, num, name:out] { out <- [[[[[text @@ -1160,44 +1186,105 @@ } _Dispatch Enum[text,func,raw name:out] -{ - If[[[func]Convention >>] = ["rhope"]] +{ + If[[[func]Convention >>] = ["rhope"]] { 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 >>]]] - }{ - out <- text + ]Append[Fold[["_Dispatch Enum Sub"]Set Input[2, name], "", Range[1, [func]Resume Index >>]]] + }{ + out <- text } } -Dispatch@C Program[program:out] +_Dispatch Enum Methods[text,types,name:out] +{ + out <- [text]Append[ [["\tFUNC_"]Append[Escape Rhope Name[name]]]Append[",\n"] ] +} + +Dispatch@C Program[program,all methods:out] { out <- [[[[["typedef enum {\n" - ]Append[Fold["_Dispatch Enum", "", [program]Functions >>]] + ]Append[Fold["_Dispatch Enum", + [Fold["_Dispatch Enum Methods", "", all methods]]Append["\tFUNC_Build,\n\tFUNC_BlueprintSP_Of,\n"], + [program]Functions >>]] ]Append["\tEND\n} funcids;\n\n"] ]Append["#define DISPATCH switch(func) { \\\n"] - ]Append[Fold["_Dispatch Switch", "", [program]Functions >>]] + ]Append[Fold["_Dispatch Switch", + [Fold["_Dispatch Switch Methods", "", all methods]]Append["\tcase FUNC_Build: goto f_Build;\\\n\tcase FUNC_BlueprintSP_Of: goto f_BlueprintSP_Of;\\\n"], + [program]Functions >>]] ]Append["\tcase END: goto DO_END;\\\n}\n\n"] -} - -Not Native[func:out] -{ - If[[[func]Convention >>] = ["rhope"]] - { out <- No } - { out <- Yes } -} - -Native[func:out] -{ - out <- [[func]Convention >>] = ["rhope"] +} + +Not Native[func:out] +{ + If[[[func]Convention >>] = ["rhope"]] + { out <- No } + { out <- Yes } +} + +Native[func:out] +{ + out <- [[func]Convention >>] = ["rhope"] +} + +Local Pointers[text,func:out] +{ + If[ [ [[[func]Variables >>]Length]+[[[func]Outputs >>]Length] ] = [0] ] + { + out <- text + }{ + out <- [text]Append[[["\tFuncDef("]Append[Escape Rhope Name[[func]Name >>]]]Append[")\n"]] + } +} + +_Method to Types[dict,name,type:out] +{ + typelist <- [dict]Index[name] {} + { typelist <- () } + + out <- [dict]Set[name, [typelist]Append[[type]Name >>]] + +} + +_Field to Types[dict,field,type:out] +{ + name <- [field]Index[0] + out <- _Method to Types[_Method to Types[dict, [name]Append[" >>"], type], [name]Append[" <<"], type] + +} + +Method to Types[dict,type:out] +{ + out <- Fold[["_Method to Types"]Set Input[2, type], dict, [type]Methods >>] +} + +Field to Types[dict,type:out] +{ + out <- Fold[["_Field to Types"]Set Input[2, type], dict, [type]Fields >>] +} + +_Method Dispatch[text, type, method, reg: out] +{ + out <- [[[[[[[text]Append["\tMethodDispatch("]]Append[ [reg]Type ID[type] ]]Append[","]]Append[Escape Rhope Name[method]]]Append[","]]Append[Escape Rhope Name[type]]]Append[")\n"] +} + +Method Dispatch[text, types, method, reg: out] +{ + out <- [[[Fold[[["_Method Dispatch"]Set Input[2, method]]Set Input[3, reg], [[[text]Append["Method("]]Append[ Escape Rhope Name[method] ]]Append[")\n"], types] + ]Append["EndMethod("] + ]Append[Escape Rhope Name[method]] + ]Append[")\n\n"] } Text@C Program[program:out] { Print["Text@C Program"] + type defs <- [[program]Type Registry >>]Definitions >> constants <- Fold["Combine Consts", Dictionary[], [program]Functions >>] + all methods <- Fold["Field to Types", Fold["Method to Types", Dictionary[], type defs], type defs] headers <- "#include +#include #include \"builtin.h\" #include \"object.h\" #include \"context.h\" @@ -1206,28 +1293,82 @@ #include \"blueprint.h\" #include \"array.h\" #include \"bool.h\"\n\n" - out <- [[[[[[[[[[[headers - ]Append[[program]Dispatch] - ]Append[[[program]Type Registry >>]Type Defs] + out <- [[[[[[[[[[[[[[[headers + ]Append[[program]Dispatch[all methods]] + ]Append[[[program]Type Registry >>]Type Defs] ]Append[Fold["_Consts C Program", Fold["_Defs C Program", "", [program]Functions >>], constants]] - ]Append[Fold[["_Text C Program"]Set Input[2, [program]Type Registry >>], "", Filter[[program]Functions >>, "Not Native"]]] - ]Append["\n -void rhope(uint32_t func) -{ - uint16_t resume,idx; - context * ct; - calldata * cdata, *temp_cdata;"] + ]Append[Fold[["_Text C Program"]Set Input[2, [program]Type Registry >>], "", Filter[[program]Functions >>, "Not Native"]]] + ]Append["\n +uint16_t rhope(uint32_t func, object ** params, uint16_t numparams, uint16_t callspace) +{ + uint16_t resume,idx; + context * ct; + calldata * cdata, *temp_cdata, *my_cdata;\n\nFuncDef(Build)\nFuncDef(BlueprintSP_Of)\n"] + ]Append[Fold["Local Pointers", "", [program]Functions >>]] + ]Append[" + ct = new_context(); + cdata = alloc_cdata(ct, NULL, callspace); + cdata->num_params = numparams; + for(idx = 0; idx < numparams; ++idx) + cdata->params[0-idx] = params[idx]; + cdata->func = END; +DISPATCH\n"] + ]Append[Fold[["Method Dispatch"]Set Input[3, [program]Type Registry >>], "", all methods]] + ]Append[" +Func(Build, + NumParams 1) + + Param(0, TYPE_BLUEPRINT) + + lv_Build->bp = ((t_Blueprint *)(cdata->params[0]))->bp; + release_ref(cdata->params[0]); + + Ret(0, new_object_bp(lv_Build->bp)) +EndFunc(Build) +DISPATCH + +Func(BlueprintSP_Of, + NumParams 1) + + lv_BlueprintSP_Of->bp = get_blueprint(cdata->params[0]); + release_ref(cdata->params[0]); + + Ret(0, new_object(TYPE_BLUEPRINT)) + ((t_Blueprint *)cdata->params[0])->bp = lv_BlueprintSP_Of->bp; +EndFunc(BlueprintSP_Of) +DISPATCH\n"] ]Append[Fold[["_Text C Program"]Set Input[2, [program]Type Registry >>], "", Filter[[program]Functions >>, "Native"]]] - ]Append["\n\nint main(int argc, char **argv) + ]Append[" +DO_END: + for(idx = 0; idx < cdata->num_params; ++idx) + params[idx] = cdata->params[0-idx]; + free_context(ct); + return cdata->num_params; + +_exception: + puts(\"Exception! Trace follows:\"); + while(cdata && cdata->func != END) + { + printf(\"%d\\n\", cdata->func); + cdata = cdata->lastframe; + } + return 0; +} + +#include \"builtin.c\" +#include \"array.c\" + +int main(int argc, char **argv) { + 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 Late Consts C"]Set Input[3, [program]Type Registry >>], "", constants]] ]Append[" - rhope(FUNC_Main); + rhope(FUNC_Main, NULL, 0, 0); return 0; }\n\n"] diff -r 1db811fa4744 -r d4b44ae2e34a nworker.rhope --- a/nworker.rhope Tue Jun 01 01:13:54 2010 -0400 +++ b/nworker.rhope Sun Jun 06 20:29:10 2010 -0400 @@ -1,6 +1,7 @@ Import extendlib.rhope Import cbackend.rhope - +Import number.rhope +Import boolean.rhope Blueprint Condition Set { @@ -295,7 +296,7 @@ NodeResults Free Temps Name - Builtin? + Builtin? Library } @@ -346,8 +347,8 @@ new type <- Val[type] new count <- 1 } - new node <- [node]Input Types <<[ - [ [node]Input Types >> ]Set[ [dest]IO Num >>, [[()]Append[new type]]Append[new count] ] + new node <- [node]Input Types <<[ + [ [node]Input Types >> ]Set[ [dest]IO Num >>, [[()]Append[new type]]Append[new count] ] ] out <- Infer Types Node[[nodelist]Set[[dest]Index >>, new node], new node, [dest]Index >>, prog, worker] } @@ -365,21 +366,21 @@ If[[Type Of[[node]Data >>]] = ["Whole Number"]] { outtype <- Type Instance["Int32"] - }{ - If[[Type Of[[node]Data >>]] = ["Type Instance"]] - { - outtype <- Type Instance["Blueprint"] - }{ - If[[Type Of[[node]Data >>]] = ["Machine Integer"]] - { - If[[[node]Data >>]Signed? >>] - { base <- "Int" } - { base <- "UInt" } - - outtype <- Type Instance[[base]Append[ [[node]Data >>]Size >> ]] + }{ + If[[Type Of[[node]Data >>]] = ["Type Instance"]] + { + outtype <- Type Instance["Blueprint"] + }{ + If[[Type Of[[node]Data >>]] = ["Machine Integer"]] + { + If[[[node]Data >>]Signed? >>] + { base <- "Int" } + { base <- "UInt" } + + outtype <- Type Instance[[base]Append[ [[node]Data >>]Size >> ]] }{ - outtype <- Type Instance[Type Of[[node]Data >>]] - } + outtype <- Type Instance[Type Of[[node]Data >>]] + } } } nextnode <- [node]Output Types <<[ [()]Append[outtype] ] @@ -462,9 +463,9 @@ } Infer Types@NWorker[worker,prog:out] -{ +{ Print[["Start inference: "]Append[[worker]Name >>]] - out <- [worker]Nodes <<[Fold[[["Infer Types Node"]Set Input[3, prog]]Set Input[4, worker], [worker]Nodes >>, [worker]Nodes >>]] + out <- [worker]Nodes <<[Fold[[["Infer Types Node"]Set Input[3, prog]]Set Input[4, worker], [worker]Nodes >>, [worker]Nodes >>]] { Print[["End inference: "]Append[[worker]Name >>]] } } @@ -654,15 +655,15 @@ { //TODO: Support parametric types datstring <- [[node]Data >>]Name >> - }{ + }{ If[[Type Of[[node]Data >>]] = ["Machine Integer"]] - { - If[[[node]Data >>]Signed? >>] - { s <- "i" } - { s <- "u" } - datstring <- [[[[node]Data >>]Value >>]Append[s]]Append[[[node]Data >>]Size >>] + { + If[[[node]Data >>]Signed? >>] + { s <- "i" } + { s <- "u" } + datstring <- [[[[node]Data >>]Value >>]Append[s]]Append[[[node]Data >>]Size >>] }{ - datstring <- [node]Data >> + datstring <- [node]Data >> } } out <- Constant[[[Type Of[[node]Data >>]]Append["_"]]Append[datstring]] @@ -749,10 +750,10 @@ with call <- [func]Method Call[[[node]Data >>]Name >>, inputs] { Print["Method Call done"] } }{ - Print["Function!"] - { - Pretty Print[inputs, ""] - { with call <- [func]Call[[[node]Data >>]Name >>, inputs]} + Print["Function!"] + { + Pretty Print[inputs, ""] + { with call <- [func]Call[[[node]Data >>]Name >>, inputs]} } } @@ -784,15 +785,15 @@ nstream <- Compile Call Node[node, program, stream, inputs, node index] }{ If[[[node]Type >>] = ["output"]] - { + { Print["Compiling output node"] inputs <- [worker]Collect Inputs[node] [conditions]For Backend - { + { Print["has conditions"] stream <- [func]Instruction Stream nfunc <- [func]Do If[~, nstream] - }{ + }{ Print["no conditions"] stream <- Val[func] nfunc <- Val[nstream] @@ -808,13 +809,13 @@ datstring <- [[node]Data >>]Name >> }{ If[[Type Of[[node]Data >>]] = ["Machine Integer"]] - { - If[[[node]Data >>]Signed? >>] - { s <- "i" } - { s <- "u" } - datstring <- [[[[node]Data >>]Value >>]Append[s]]Append[[[node]Data >>]Size >>] + { + If[[[node]Data >>]Signed? >>] + { s <- "i" } + { s <- "u" } + datstring <- [[[[node]Data >>]Value >>]Append[s]]Append[[[node]Data >>]Size >>] }{ - datstring <- [node]Data >> + datstring <- [node]Data >> } } nfunc <- [func]Register Constant[[[Type Of[[node]Data >>]]Append["_"]]Append[datstring], [node]Data >>] @@ -898,143 +899,143 @@ Result Vars@NWorker[worker:out] { out <- Fold["Node Result Vars", (), [worker]Nodes >>] -} - -Make Basic Type[type:out] -{ - out <- [Type Instance[[type]Name >>]]Params <<[ [type]Params >> ] -} - -FInputs[ifunc, input type, index, inputs:out] -{ - func <- [ifunc]Set Input Type[Make Basic Type[input type], index] - name <- [inputs]Index[index] - Print[["FInputs: "]Append[index]] - Pretty Print[input type, ""] - { - If[[[input type]Variant >>] = ["Naked"]] - { - - naked <- [" naked"]Append[name] - - out <- [[[func]Allocate Var[naked, input type] - ]Unbox[name, naked] - ]Release[name] - }{ - If[[input type]Mutable? >>] - { - name <- [inputs]Index[index] - copied <- [func]Copy[name, name] - - }{ - copied <- Val[func] - } - If[[[input type]Variant >>] = ["Raw Pointer"]] - { - raw <- [" raw"]Append[name] - If[[[input type]Name >>]=["Array"]] - { - - out <- [[copied]Allocate Var[raw, input type] - ]Array Raw Pointer[name, raw] - }{ - out <- [[copied]Allocate Var[raw, input type] - ]Get Raw Pointer[name, raw] - } - }{ - out <- Val[copied] - } - } - } -} - -FParams[input:out] -{ - iname <- [input]Index[0] - type <- [input]Index[1] - If[[[type]Variant >>] = ["Naked"]] - { out <- [" naked"]Append[iname] } - { - If[[[type]Variant >>] = ["Raw Pointer"]] - { out <- [" raw"]Append[iname] } - { out <- Val[iname] } - } -} -_Return Param[outputs, inputs, input types, index:out,none] -{ - Print[["_Return Param: "]Append[output]] - output <- [outputs]Index[index] - [inputs]Find[output] - { - If[[[input types]Index[~]]Mutable? >>] - { - ,none <- [outputs]Next[index] - { - out,none <- _Return Param[outputs, inputs, input types, ~] - } - } { - out <- index - } - }{ - out <- index - } -} - -Return Param[outputs, inputs, input types:out,none] -{ - Print["Return Param"] - ,none <- [outputs]First - { out,none <- _Return Param[outputs, inputs, input types, ~] } - -} - -Save Foreign Result[func, output, index, output types, inputs, input types:out] -{ - type <- [output types]Index[index] - If[[[type]Variant >>] = ["Naked"]] - { - out <- [func]Box[[" naked"]Append[output], output, type] - }{ - [inputs]Find[output] - { - If[[[input types]Index[~]]Mutable? >>] - { - out <- [func]Move[output, Output[output]] - }{ - out <- func - } - }{ - out <- func - } - } -} - -Compile Foreign Stub[worker,program,name:out] -{ - Print[["Compiling FFI stub for "]Append[name]] - ifunc <- [[program]Create Function[name, [worker]Inputs >>, [worker]Outputs >>, "rhope"] - ]Output Types <<[Map[[worker]Output Types >>, "Make Basic Type"]] - - rp num <- Return Param[[worker]Outputs >>, [worker]Inputs >>, [worker]Input Types >>] - { - rbase <- [[worker]Outputs >>]Index[rp num] - If[[[[[worker]Output Types >>]Index[rp num]]Variant >>] = ["Naked"]] - { - rparam <- [" naked"]Append[rbase] - rfunc <- [ifunc]Allocate Var[rparam, [[worker]Output Types >>]Index[rp num]] - }{ - rparam <- Val[rbase] - rfunc <- Val[ifunc] - } - }{ - rparam <- "" - rfunc <- Val[ifunc] - } - - Fold[["FInputs"]Set Input[3, [worker]Inputs >>], rfunc, [worker]Input Types >>] - { [~]Call Foreign[name, [worker]Convention >>, Map[Zip[[worker]Inputs >>, [worker]Input Types >>], "FParams"], rparam] - { Fold[[[["Save Foreign Result"]Set Input[3, [worker]Output Types >>]]Set Input[4, [worker]Inputs >>]]Set Input[5, [worker]Input Types >>], ~, [worker]Outputs >>] - { out <- [program]Store Function[~] }}} +} + +Make Basic Type[type:out] +{ + out <- [Type Instance[[type]Name >>]]Params <<[ [type]Params >> ] +} + +FInputs[ifunc, input type, index, inputs:out] +{ + func <- [ifunc]Set Input Type[Make Basic Type[input type], index] + name <- [inputs]Index[index] + Print[["FInputs: "]Append[index]] + Pretty Print[input type, ""] + { + If[[[input type]Variant >>] = ["Naked"]] + { + + naked <- [" naked"]Append[name] + + out <- [[[func]Allocate Var[naked, input type] + ]Unbox[name, naked] + ]Release[name] + }{ + If[[input type]Mutable? >>] + { + name <- [inputs]Index[index] + copied <- [func]Copy[name, name] + + }{ + copied <- Val[func] + } + If[[[input type]Variant >>] = ["Raw Pointer"]] + { + raw <- [" raw"]Append[name] + If[[[input type]Name >>]=["Array"]] + { + + out <- [[copied]Allocate Var[raw, input type] + ]Array Raw Pointer[name, raw] + }{ + out <- [[copied]Allocate Var[raw, input type] + ]Get Raw Pointer[name, raw] + } + }{ + out <- Val[copied] + } + } + } +} + +FParams[input:out] +{ + iname <- [input]Index[0] + type <- [input]Index[1] + If[[[type]Variant >>] = ["Naked"]] + { out <- [" naked"]Append[iname] } + { + If[[[type]Variant >>] = ["Raw Pointer"]] + { out <- [" raw"]Append[iname] } + { out <- Val[iname] } + } +} +_Return Param[outputs, inputs, input types, index:out,none] +{ + Print[["_Return Param: "]Append[output]] + output <- [outputs]Index[index] + [inputs]Find[output] + { + If[[[input types]Index[~]]Mutable? >>] + { + ,none <- [outputs]Next[index] + { + out,none <- _Return Param[outputs, inputs, input types, ~] + } + } { + out <- index + } + }{ + out <- index + } +} + +Return Param[outputs, inputs, input types:out,none] +{ + Print["Return Param"] + ,none <- [outputs]First + { out,none <- _Return Param[outputs, inputs, input types, ~] } + +} + +Save Foreign Result[func, output, index, output types, inputs, input types:out] +{ + type <- [output types]Index[index] + If[[[type]Variant >>] = ["Naked"]] + { + out <- [func]Box[[" naked"]Append[output], output, type] + }{ + [inputs]Find[output] + { + If[[[input types]Index[~]]Mutable? >>] + { + out <- [func]Move[output, Output[output]] + }{ + out <- func + } + }{ + out <- func + } + } +} + +Compile Foreign Stub[worker,program,name:out] +{ + Print[["Compiling FFI stub for "]Append[name]] + ifunc <- [[program]Create Function[name, [worker]Inputs >>, [worker]Outputs >>, "rhope"] + ]Output Types <<[Map[[worker]Output Types >>, "Make Basic Type"]] + + rp num <- Return Param[[worker]Outputs >>, [worker]Inputs >>, [worker]Input Types >>] + { + rbase <- [[worker]Outputs >>]Index[rp num] + If[[[[[worker]Output Types >>]Index[rp num]]Variant >>] = ["Naked"]] + { + rparam <- [" naked"]Append[rbase] + rfunc <- [ifunc]Allocate Var[rparam, [[worker]Output Types >>]Index[rp num]] + }{ + rparam <- Val[rbase] + rfunc <- Val[ifunc] + } + }{ + rparam <- "" + rfunc <- Val[ifunc] + } + + Fold[["FInputs"]Set Input[3, [worker]Inputs >>], rfunc, [worker]Input Types >>] + { [~]Call Foreign[name, [worker]Convention >>, Map[Zip[[worker]Inputs >>, [worker]Input Types >>], "FParams"], rparam] + { Fold[[[["Save Foreign Result"]Set Input[3, [worker]Output Types >>]]Set Input[4, [worker]Inputs >>]]Set Input[5, [worker]Input Types >>], ~, [worker]Outputs >>] + { out <- [program]Store Function[~] }}} } Compile Worker@NWorker[worker,program,name:out] @@ -1042,8 +1043,8 @@ If[[worker]Builtin? >>] { out <- program - }{ - If[[[worker]Library >>] = [""]] + }{ + If[[[worker]Library >>] = [""]] { Print[["Compiling: "]Append[name]] { @@ -1060,9 +1061,9 @@ final func <- Val[func] } out <- [program]Store Function[Fold["Release", Fold[["Release Var"]Set Input[0, worker], final func, res vars], [worker]Inputs >>]] - } - }{ - out <- Compile Foreign Stub[worker,[program]Link[[worker]Convention >>, [worker]Library >> ],name] + } + }{ + out <- Compile Foreign Stub[worker,[program]Link[[worker]Convention >>, [worker]Library >> ],name] } } } @@ -1208,7 +1209,7 @@ { getter <- [[start getter]Do AddRef[getref, "out"]]Release["obj"] }{ - getter <- [[start getter]Box[getref, "out", type]]Release["obj"] + getter <- [[start getter]Box[getref, "out", type]]Release["obj"] { Print["Got getter"] } } @@ -1236,7 +1237,7 @@ ,setref <- [begin setter]Write Field["obj", name] { setter <- [[~]Unbox["newval", setref] - ]Move["obj", "out"] + ]Move["obj", "out"] { Print ["got setter"] } } } @@ -1291,8 +1292,8 @@ after bind <- [prog]Workers << [ [[prog]Workers >>]Set[name, [worker]Name <<[name]] ] parts <- [name]Split["@"] [parts]Index[1] - { - orig bp <- [[after bind]Blueprints >>]Index[~] {} + { + orig bp <- [[after bind]Blueprints >>]Index[~] {} { orig bp <- NBlueprint[] } out <- [after bind]Blueprints <<[ [[after bind]Blueprints >>]Set[~, [orig bp]Add Method[[parts]Index[0]] ] ] }{ @@ -1317,16 +1318,16 @@ } _Compile Program[backend, worker, name:out] -{ +{ Print["_Compile Program"] out <- [worker]Compile Worker[backend, name] } Compile Program@NProgram[prog, backend:out] -{ - backend with bps <- Generate Number Methods[Fold["_Compile Program BP Special", Fold["_Compile Program BP", backend, [prog]Blueprints >>], [prog]Blueprints >>]] - { Print["All blueprints added"] } - workers with infer <- SMap[[prog]Workers >>, ["Infer Types"]Set Input[1, prog]] +{ + backend with bps <- Generate Boolean Methods[Generate Number Methods[Fold["_Compile Program BP Special", Fold["_Compile Program BP", backend, [prog]Blueprints >>], [prog]Blueprints >>]]] + { Print["All blueprints added"] } + workers with infer <- SMap[[prog]Workers >>, ["Infer Types"]Set Input[1, prog]] { Print["Type inference complete"] } out <- Fold["_Compile Program", backend with bps, workers with infer] } @@ -1339,10 +1340,10 @@ Register Worker@NProgram[prog, name, convention, inputs, outputs: out] { Print[["Register Worker "]Append[name]] - after reg <- [prog]Worker Refs <<[ - [ [prog]Worker Refs >> ]Set[name, - Worker Ref[name, convention, inputs, outputs, No] - ] + after reg <- [prog]Worker Refs <<[ + [ [prog]Worker Refs >> ]Set[name, + Worker Ref[name, convention, inputs, outputs, No] + ] ] parts <- [name]Split["@"] [parts]Index[1] @@ -1357,8 +1358,8 @@ { registered <- [[[[[prog]Register Worker["Print", "rhope", 1, 1] ]Register Worker["If@Boolean", "rhope", 1, 2] - ]Register Worker["Build", "rhope", 1, 1] - ]Register Worker["Blueprint Of", "rhope", 1, 1] + ]Register Worker["Build", "rhope", 1, 1] + ]Register Worker["Blueprint Of", "rhope", 1, 1] ]Register Number Methods out <- [[[[registered]Bind Worker["If@Boolean", @@ -1381,13 +1382,13 @@ ]Input Types <<[ [()]Append[Type Instance["Blueprint"]] ] ]Outputs <<[("out")] ]Output Types <<[ [()]Append[Type Instance["Any Type"]] ] - ]Builtin? <<[Yes]] - ]Bind Worker["Blueprint Of", - [[[[[NWorker["rhope"] - ]Inputs <<[("object")] - ]Input Types <<[ [()]Append[Type Instance["Any Type"]]] - ]Outputs <<[("type")] - ]Output Types <<[ [()]Append[Type Instance["Blueprint"]]] + ]Builtin? <<[Yes]] + ]Bind Worker["Blueprint Of", + [[[[[NWorker["rhope"] + ]Inputs <<[("object")] + ]Input Types <<[ [()]Append[Type Instance["Any Type"]]] + ]Outputs <<[("type")] + ]Output Types <<[ [()]Append[Type Instance["Blueprint"]]] ]Builtin? <<[Yes]] } diff -r 1db811fa4744 -r d4b44ae2e34a runtime/blueprint.c --- a/runtime/blueprint.c Tue Jun 01 01:13:54 2010 -0400 +++ b/runtime/blueprint.c Sun Jun 06 20:29:10 2010 -0400 @@ -1,6 +1,6 @@ #include "blueprint.h" #include "builtin.h" -#include "context.h" +#include "context.h" #include typedef struct @@ -8,31 +8,6 @@ blueprint * bp; } l_Build; -Func(Build, - NumParams 1, - CallSpace 1, - l_Build) - - Param(0, TYPE_BLUEPRINT) - - locals->bp = ((t_Blueprint *)(cdata->params[0]))->bp; - release_ref(cdata->params[0]); - - Ret(0, new_object_bp(locals->bp)) -EndFunc - -Func(BlueprintSP_Of, - NumParams 1, - CallSpace 1, - l_Build) - - locals->bp = get_blueprint(cdata->params[0]); - release_ref(cdata->params[0]); - - Ret(0, new_object(TYPE_BLUEPRINT)) - ((t_Blueprint *)cdata->params[0])->bp = locals->bp; -EndFunc - object * make_Blueprint(int32_t type_id) { t_Blueprint * obj; @@ -40,13 +15,13 @@ obj = (t_Blueprint *)ret; obj->bp = type_id ? get_blueprint_byid(type_id) : NULL; return ret; -} - -int32_t _internal_blueprint_eq(object * left, object * right) -{ - t_Blueprint * l = (t_Blueprint *)left; - t_Blueprint * r = (t_Blueprint *)right; - return l->bp == r->bp; -} - +} +int32_t _internal_blueprint_eq(object * left, object * right) +{ + t_Blueprint * l = (t_Blueprint *)left; + t_Blueprint * r = (t_Blueprint *)right; + return l->bp == r->bp; +} + + diff -r 1db811fa4744 -r d4b44ae2e34a runtime/blueprint.h --- a/runtime/blueprint.h Tue Jun 01 01:13:54 2010 -0400 +++ b/runtime/blueprint.h Sun Jun 06 20:29:10 2010 -0400 @@ -7,8 +7,15 @@ Box(blueprint *,bp,Blueprint) object * make_Blueprint(int32_t type_id); -FuncDef(Build) -FuncDef(BlueprintSP_Of) + int32_t _internal_blueprint_eq(object * left, object * right); +typedef struct { + blueprint * bp; +} lt_Build; + +typedef struct { + blueprint * bp; +} lt_BlueprintSP_Of; + #endif //BLUEPRINT_H_ diff -r 1db811fa4744 -r d4b44ae2e34a runtime/builtin.c --- a/runtime/builtin.c Tue Jun 01 01:13:54 2010 -0400 +++ b/runtime/builtin.c Sun Jun 06 20:29:10 2010 -0400 @@ -3,8 +3,8 @@ #include "integer.h" #include "bool.h" #include -#include -#include +#include +#include #include void register_builtin_type(uint32_t type) @@ -23,7 +23,7 @@ uint32_t i; for(i = 0; i < TYPE_FIRST_USER; ++i) register_builtin_type(i); -} +} object * make_Int64(int64_t val) { t_Int64 * obj; @@ -31,8 +31,8 @@ obj = (t_Int64 *)ret; obj->Num = val; return ret; -} - +} + object * make_Int32(int32_t val) { @@ -41,8 +41,8 @@ obj = (t_Int32 *)ret; obj->Num = val; return ret; -} - +} + object * make_Int16(int16_t val) { t_Int16 * obj; @@ -50,8 +50,8 @@ obj = (t_Int16 *)ret; obj->Num = val; return ret; -} - +} + object * make_Int8(int8_t val) { t_Int8 * obj; @@ -59,8 +59,8 @@ obj = (t_Int8 *)ret; obj->Num = val; return ret; -} - +} + object * make_UInt64(uint64_t val) { t_UInt64 * obj; @@ -68,8 +68,8 @@ obj = (t_UInt64 *)ret; obj->Num = val; return ret; -} - +} + object * make_UInt32(uint32_t val) { t_UInt32 * obj; @@ -77,8 +77,8 @@ obj = (t_UInt32 *)ret; obj->Num = val; return ret; -} - +} + object * make_UInt16(uint16_t val) { t_UInt16 * obj; @@ -86,8 +86,8 @@ obj = (t_UInt16 *)ret; obj->Num = val; return ret; -} - +} + object * make_UInt8(uint8_t val) { t_UInt8 * obj; @@ -95,35 +95,19 @@ obj = (t_UInt8 *)ret; obj->Num = val; return ret; -} - -object * make_String(char * text) -{ - returntype ret; - context * ct; - calldata * cdata; - object * retobj; - t_Array * arr = (t_Array *)_internal_array_allocnaked(strlen(text), make_Blueprint(TYPE_UINT8)); - arr->payload.Length = arr->payload.Storage; - memcpy(((char *)arr) + sizeof(t_Array), text, arr->payload.Length); - - //This is really ugly, but I don't see a good way around it at the moment - ct = new_context(); - cdata = alloc_cdata(ct, 1); - cdata->params[0] = (object *)arr; - cdata->num_params = 1; - cdata->resume = 0; - ret = f_String(cdata); - while(ret == TAIL_RETURN) - ret = cdata->tail_func(cdata); - if(ret == EXCEPTION_RETURN) - { - puts("Exception while building string literal!"); - exit(-1); - } - retobj = cdata->params[0]; - free_context(ct); - return retobj; -} - - +} + +object * make_String(char * text) +{ + object * params[1]; + t_Array * arr = (t_Array *)_internal_array_allocnaked(strlen(text), make_Blueprint(TYPE_UINT8)); + arr->payload.Length = arr->payload.Storage; + memcpy(((char *)arr) + sizeof(t_Array), text, arr->payload.Length); + + params[0] = (object *)arr; + rhope(FUNC_String, params, 1, 1); + + return params[0]; +} + + diff -r 1db811fa4744 -r d4b44ae2e34a runtime/context.c --- a/runtime/context.c Tue Jun 01 01:13:54 2010 -0400 +++ b/runtime/context.c Sun Jun 06 20:29:10 2010 -0400 @@ -19,18 +19,18 @@ c->stack_begin = new_stack(); c->current_stack = c->stack_begin; return c; -} - -void free_context(context * c) -{ - stackchunk *next,*current = c->stack_begin; - while(current) - { - next = current->next; - free(current); - current = next; - } - free(c); +} + +void free_context(context * c) +{ + stackchunk *next,*current = c->stack_begin; + while(current) + { + next = current->next; + free(current); + current = next; + } + free(c); } void * alloc_stack(context * ct, uint32_t size) @@ -64,8 +64,12 @@ calldata * alloc_cdata(context * ct, calldata * lastframe, uint32_t num_params) { - calldata * retval = (calldata *)(((char *)alloc_stack(ct, sizeof(calldata)+(num_params-1)*sizeof(object *))) + sizeof(object *)*(num_params-1)); + //Make sure we have enough space for at least 32 return values + calldata * retval = alloc_stack(ct, sizeof(calldata)+(31)*sizeof(object *)); + //But only actually reserve space for the number requested + free_stack(ct, retval->params + num_params); retval->lastframe = lastframe; + retval->callspace = num_params; return retval; } diff -r 1db811fa4744 -r d4b44ae2e34a runtime/context.h --- a/runtime/context.h Tue Jun 01 01:13:54 2010 -0400 +++ b/runtime/context.h Sun Jun 06 20:29:10 2010 -0400 @@ -22,7 +22,7 @@ stackchunk * new_stack(); context * new_context(); void * alloc_stack(context * ct, uint32_t size); -calldata * alloc_cdata(context * ct, uint32_t num_params); -void free_stack(context * ct, void * data); +calldata * alloc_cdata(context * ct, calldata * lastframe, uint32_t num_params); +void free_stack(context * ct, void * data); void free_context(context * c); #endif //_CONTEXT_H_ diff -r 1db811fa4744 -r d4b44ae2e34a runtime/func.h --- a/runtime/func.h Tue Jun 01 01:13:54 2010 -0400 +++ b/runtime/func.h Sun Jun 06 20:29:10 2010 -0400 @@ -1,9 +1,9 @@ #ifndef _FUNC_H_ #define _FUNC_H_ -typedef struct object object; -typedef struct calldata calldata; - +typedef struct object object; +typedef struct calldata calldata; + typedef enum { NORMAL_RETURN=0, EXCEPTION_RETURN, @@ -22,16 +22,18 @@ #define Func(name,numparams) \ f_ ## name:\ for(idx = numparams; idx < cdata->num_params; ++idx)\ - release_ref(cdata->params[0-idx]); cdata->num_params = numparams;\ + release_ref(cdata->params[idx]); cdata->num_params = numparams;\ sf_ ## name:\ - lv_ ## name = alloc_stack(ct, sizeof(lt_ ## name)); + lv_ ## name = alloc_stack(ct, sizeof(lt_ ## name));\ + my_cdata = cdata; #define FuncNoLocals(name,numparams) \ f_ ## name:\ for(idx = numparams; idx < cdata->num_params; ++idx)\ - release_ref(cdata->params[0-idx]); cdata->num_params = numparams;\ -sf_ ## name: + release_ref(cdata->params[idx]); cdata->num_params = numparams;\ +sf_ ## name:\ + my_cdata = cdata; #define EndFunc(name) \ free_stack(ct, lv_ ## name);\ @@ -46,8 +48,11 @@ switch(get_blueprint(cdata->params[0])->type_id)\ { -#define EndMethod \ +#define EndMethod(name) \ default:\ + printf("Type %d does not implement method %s\n", get_blueprint(cdata->params[0])->type_id, #name);\ + cdata = alloc_cdata(ct, cdata, 0);\ + cdata->func = FUNC_ ## name;\ goto _exception;\ } @@ -60,7 +65,11 @@ f_ ## name ## AT_ ## type_name:\ sf_ ## name ## AT_ ## type_name:\ if (cdata->num_params < 1)\ + {\ + cdata = alloc_cdata(ct, cdata, 0);\ + cdata->func = FUNC_ ## name ## AT_ ## type_name;\ goto _exception;\ + }\ if(get_blueprint(cdata->params[0])->type_id != mytype_id)\ {\ puts("uh oh, need conversion and that's not implemented yet!");\ @@ -68,14 +77,17 @@ }\ m_ ## name ## AT_ ## type_name:\ for(idx = numparams; idx < cdata->num_params; ++idx)\ - release_ref(cdata->params[0-idx]); cdata->num_params = numparams;\ - lv_ ## name ## AT_ ## type_name = alloc_stack(ct, sizeof(lt_ ## name ## AT_ ## type_name)); + release_ref(cdata->params[idx]); cdata->num_params = numparams;\ + lv_ ## name ## AT_ ## type_name = alloc_stack(ct, sizeof(lt_ ## name ## AT_ ## type_name));\ + my_cdata = cdata; #define MethodImplNoLocals(name,type_name,mytype_id,numparams) \ f_ ## name ## AT_ ## type_name:\ sf_ ## name ## AT_ ## type_name:\ if (cdata->num_params < 1)\ + cdata = alloc_cdata(ct, cdata, 0);\ + cdata->func = FUNC_ ## name ## AT_ ## type_name;\ goto _exception;\ if(get_blueprint(cdata->params[0])->type_id != mytype_id)\ {\ @@ -84,20 +96,22 @@ }\ m_ ## name ## AT_ ## type_name:\ for(idx = numparams; idx < cdata->num_params; ++idx)\ - release_ref(cdata->params[0-idx]); cdata->num_params = numparams; + release_ref(cdata->params[idx]); cdata->num_params = numparams;\ + my_cdata = cdata; #define NumParams #define CallSpace #define Param(num,convtypeid) \ - if(get_blueprint(cdata->params[0-num])->type_id != convtypeid)\ + if(get_blueprint(cdata->params[num])->type_id != convtypeid)\ {\ puts("uh oh, need conversion and that's not implemented yet!");\ exit(1);\ } #define CopiedParam(num,convtypeid) Param(num,convtypeid) cdata->params[num] = copy_object(cdata->params[num]); -#define Ret(num,val) cdata->params[0-num] = (object *)(val); +#define Ret(num,val) cdata->params[num] = (object *)(val); +#define NumRet(num) cdata->num_params = num; #define Exception #define FuncDef(name) lt_ ## name * lv_ ## name; #define MethodDef(name) lt_ ## name ## AT_ ## type_name * lv_ ## name ## AT_ ## type_name; @@ -105,14 +119,16 @@ #define PrepCall(callspace) cdata = alloc_cdata(ct, cdata, callspace); -#define SetParam(num,value) cdata->params[0-num] = value; +#define SetParam(num,value) cdata->params[num] = value; #define Call(tocall, numparams, resumeto, myname)\ - cdata->func = FUNC_ ## myname ## _ ## resumeto;\ + cdata->func = RES_ ## resumeto ## _ ## myname;\ cdata->num_params = numparams;\ goto sf_ ## tocall;\ r ## resumeto ## _ ## myname:\ - lv_ ## myname = (lt_ ## myname *)(cdata->lastframe+1); + my_cdata = cdata->lastframe;\ + lv_ ## myname = (lt_ ## myname *)(my_cdata->params+my_cdata->callspace); + #define FreeCall\ temp_cdata = cdata->lastframe;\ @@ -137,4 +153,4 @@ goto sf_ ## tocall; -#endif //_FUNC_H_ +#endif //_FUNC_H_ diff -r 1db811fa4744 -r d4b44ae2e34a runtime/object.h --- a/runtime/object.h Tue Jun 01 01:13:54 2010 -0400 +++ b/runtime/object.h Sun Jun 06 20:29:10 2010 -0400 @@ -42,17 +42,17 @@ } multisize; #pragma pack(push,1) -typedef struct calldata { - object *params[1]; +struct calldata { struct calldata *lastframe; + uint32_t func; uint16_t num_params; - uint16_t resume; - uint32_t func; + uint16_t callspace; + object *params[1]; }; #pragma pack(pop) #define OBegin typedef struct { -#define Object(name) } nt_ ## name; typedef struct { object SP_header; nt_ ## name payload; } t_ ## name; +#define Object(name) } nt_ ## name; typedef struct { object SP_header; nt_ ## name payload; } t_ ## name; #define MObject(name) } nt_ ## name; typedef struct { multisize SP_header; nt_ ## name payload; } t_ ## name; #define Box(nakedtype,fieldname,objectname) typedef struct{ object SP_header; nakedtype fieldname; } t_ ## objectname; diff -r 1db811fa4744 -r d4b44ae2e34a test/fib.c --- a/test/fib.c Tue Jun 01 01:13:54 2010 -0400 +++ b/test/fib.c Sun Jun 06 20:29:10 2010 -0400 @@ -4,117 +4,353 @@ #include "context.h" #include "func.h" #include "integer.h" +#include "bool.h" -typedef struct -{ - object * work1; - object * work2; - object * out; -} fib_t; +typedef enum { + FUNC__LT_, + FUNC__LT__AT_Int32, + FUNC__MN_, + FUNC__MN__AT_Int32, + FUNC__PL_, + FUNC__PL__AT_Int32, + FUNC_Fib, + FUNC_Fib_1, + FUNC_Fib_2, + FUNC_Fib_3, + FUNC_Fib_4, + FUNC_Fib_5, + FUNC_Fib_6, + FUNC_If, + FUNC_If_AT_Yes_SP_No, + FUNC_Main, + FUNC_Main_1, + FUNC_Main_2, + FUNC_Print, + END +} funcids; -object * const_1; -object * const_2; +#define DISPATCH switch(func) { \ + case FUNC__LT_: goto f__LT_;\ + case FUNC__LT__AT_Int32: goto f__LT__AT_Int32;\ + case FUNC__MN_: goto f__MN_;\ + case FUNC__MN__AT_Int32: goto f__MN__AT_Int32;\ + case FUNC__PL_: goto f__PL_;\ + case FUNC__PL__AT_Int32: goto f__PL__AT_Int32;\ + case FUNC_Fib: goto f_Fib;\ + case FUNC_Fib_1: goto r1_Fib;\ + case FUNC_Fib_2: goto r2_Fib;\ + case FUNC_Fib_3: goto r3_Fib;\ + case FUNC_Fib_4: goto r4_Fib;\ + case FUNC_Fib_5: goto r5_Fib;\ + case FUNC_Fib_6: goto r6_Fib;\ + case FUNC_If: goto f_If;\ + case FUNC_If_AT_Yes_SP_No: goto f_If_AT_Yes_SP_No;\ + case FUNC_Main: goto f_Main;\ + case FUNC_Main_1: goto r1_Main;\ + case FUNC_Main_2: goto r2_Main;\ + case FUNC_Print: goto f_Print;\ + case END: goto DO_END;\ +} + -Func(Fib, - NumParams 1, - CallSpace 2, - fib_t); - - locals->out = NULL; - +typedef struct { + struct object *____result__0__0; + struct object *____result__0__1; + struct object *____result__10__0; + struct object *____result__11__0; + struct object *____result__1__0; + struct object *____result__5__0; + struct object *____result__6__0; + struct object *____result__7__0; + struct object *out; +} lt_Fib; + +typedef struct { + struct object *____result__0__0; + struct object *____result__1__0; +} lt_Main; + +object * _const_Whole_SP_Number__1; +object * _const_Whole_SP_Number__2; +object * _const_Whole_SP_Number__30; + +void rhope(uint32_t func) +{ + uint16_t resume,idx; + context * ct; + calldata * cdata, *temp_cdata; +FuncDef(Fib) +FuncDef(Main) + + ct = new_context(); + cdata = alloc_cdata(ct, NULL, 0); + cdata->num_params = 0; + cdata->func = END; +DISPATCH +Method(_LT_) + MethodDispatch(TYPE_INT32,_LT_,Int32) +EndMethod + +MethodImplNoLocals(_LT_,Int32,TYPE_INT32, + NumParams 2) - call->params[0] = add_ref(cdata->params[0]); - call->params[1] = add_ref(const_2); - MCall(METHOD_LESS, 2) + Param(1, TYPE_INT32) - MCall(METHOD_IF, 1) - - if(call->params[0]) + if(((t_Int32 *)cdata->params[0])->num < ((t_Int32 *)cdata->params[-1])->num) { - release_ref(call->params[0]); - locals->out = add_ref(const_1); + release_ref(cdata->params[0]); + Ret(0, Yes) + } else { + release_ref(cdata->params[0]); + Ret(0, No) } - if(call->params[1]) + release_ref(cdata->params[-1]); +EndFuncNoLocals +DISPATCH + +Method(_MN_) + MethodDispatch(TYPE_INT32,_MN_,Int32) +EndMethod +DISPATCH + +MethodImplNoLocals(_MN_,Int32,TYPE_INT32, + NumParams 2) + + Param(1, TYPE_INT32) + + cdata->params[0] = copy_object(cdata->params[0]); + ((t_Int32 *)cdata->params[0])->num -= ((t_Int32 *)cdata->params[-1])->num; + + release_ref(cdata->params[-1]); +EndFuncNoLocals +DISPATCH + +Method(_PL_) + MethodDispatch(TYPE_INT32,_PL_,Int32) +EndMethod +DISPATCH + +MethodImplNoLocals(_PL_,Int32,TYPE_INT32, + NumParams 2) + + Param(1, TYPE_INT32) + + cdata->params[0] = copy_object(cdata->params[0]); + ((t_Int32 *)cdata->params[0])->num += ((t_Int32 *)cdata->params[-1])->num; + + release_ref(cdata->params[-1]); +EndFuncNoLocals +DISPATCH + +Func(Fib, + NumParams 1) + lv_Fib->____result__0__0 = NULL; + lv_Fib->____result__0__1 = NULL; + lv_Fib->____result__1__0 = NULL; + lv_Fib->____result__5__0 = NULL; + lv_Fib->____result__6__0 = NULL; + lv_Fib->____result__7__0 = NULL; + lv_Fib->____result__10__0 = NULL; + lv_Fib->____result__11__0 = NULL; + lv_Fib->out = NULL; + + PrepCall(2) + + SetParam(0, add_ref(cdata->lastframe->params[0])) + SetParam(1, add_ref(_const_Whole_SP_Number__2)) + Call(_LT_, + NumParams 2, 1, Fib) + lv_Fib->____result__1__0 = cdata->params[0]; + + SetParam(0, add_ref(lv_Fib->____result__1__0)) + Call(If, + NumParams 1, 2, Fib) + lv_Fib->____result__0__0 = cdata->params[0]; + lv_Fib->____result__0__1 = cdata->params[-1]; + + if(lv_Fib->____result__0__1) + { + SetParam(0, add_ref(cdata->lastframe->params[0])) + SetParam(1, add_ref(_const_Whole_SP_Number__1)) + Call(_MN_, + NumParams 2, 3, Fib) + lv_Fib->____result__7__0 = cdata->params[0]; + } + if(lv_Fib->____result__0__1) + { + SetParam(0, add_ref(cdata->lastframe->params[0])) + SetParam(1, add_ref(_const_Whole_SP_Number__2)) + Call(_MN_, + NumParams 2, 4, Fib) + lv_Fib->____result__11__0 = cdata->params[0]; + } + if(lv_Fib->____result__0__0) { - release_ref(call->params[1]); - call->params[0] = add_ref(cdata->params[0]); - call->params[1] = add_ref(const_1); - MCall(METHOD_SUB, 2) - locals->work1 = call->params[0]; - - call->params[0] = add_ref(cdata->params[0]); - call->params[1] = add_ref(const_2); - MCall(METHOD_SUB, 2) - locals->work2 = call->params[0]; - - call->params[0] = locals->work1; - Call(Fib, 1) - locals->work1 = call->params[0]; - - call->params[0] = locals->work2; - Call(Fib, 1) - locals->work2 = call->params[0]; - - if(!locals->out) + lv_Fib->out = add_ref(_const_Whole_SP_Number__1); + } + if(lv_Fib->____result__0__1) + { + SetParam(0, add_ref(lv_Fib->____result__7__0)) + Call(Fib, + NumParams 1, 5, Fib) + lv_Fib->____result__6__0 = cdata->params[0]; + } + if(lv_Fib->____result__0__1) + { + SetParam(0, add_ref(lv_Fib->____result__11__0)) + Call(Fib, + NumParams 1, 6, Fib) + lv_Fib->____result__10__0 = cdata->params[0]; + } + if(lv_Fib->____result__0__1) + { + if(lv_Fib->____result__0__0) + { + release_ref(lv_Fib->____result__0__0); + } + if(lv_Fib->____result__0__1) + { + release_ref(lv_Fib->____result__0__1); + } + if(lv_Fib->____result__1__0) + { + release_ref(lv_Fib->____result__1__0); + } + if(lv_Fib->____result__7__0) + { + release_ref(lv_Fib->____result__7__0); + } + if(lv_Fib->____result__11__0) { - release_ref(cdata->params[0]); - cdata->params[0] = locals->work1; - cdata->params[1] = locals->work2; - TMCall(METHOD_ADD, 2) + release_ref(lv_Fib->____result__11__0); } + TPrepCall(2) + SetParam(0, lv_Fib->____result__6__0) + SetParam(1, lv_Fib->____result__10__0) + TCall(_PL_, + NumParams 2) + //lv_Fib->____result__5__0 = cdata->params[0]; + } + FreeCall + if(lv_Fib->____result__0__1) + { + lv_Fib->out = add_ref(lv_Fib->____result__5__0); + } + if(lv_Fib->____result__0__0) + { + release_ref(lv_Fib->____result__0__0); + } + if(lv_Fib->____result__0__1) + { + release_ref(lv_Fib->____result__0__1); + } + if(lv_Fib->____result__1__0) + { + release_ref(lv_Fib->____result__1__0); + } + if(lv_Fib->____result__5__0) + { + release_ref(lv_Fib->____result__5__0); + } + if(lv_Fib->____result__6__0) + { + release_ref(lv_Fib->____result__6__0); + } + if(lv_Fib->____result__7__0) + { + release_ref(lv_Fib->____result__7__0); + } + if(lv_Fib->____result__10__0) + { + release_ref(lv_Fib->____result__10__0); + } + if(lv_Fib->____result__11__0) + { + release_ref(lv_Fib->____result__11__0); } release_ref(cdata->params[0]); - Ret(0, locals->out) + Ret(0, lv_Fib->out) +EndFunc(Fib) +DISPATCH + +Method(If) + MethodDispatch(TYPE_BOOLEAN,If,Yes_SP_No) +EndMethod + +MethodImplNoLocals(If,Yes_SP_No,TYPE_BOOLEAN, + NumParams 1) -EndFunc + if(((t_Boolean *)(cdata->params[0]))->val) + { + Ret(1, NULL) + } else { + Ret(1, cdata->params[0]); + Ret(0, NULL) + } +EndFuncNoLocals +DISPATCH -FuncNoLocals(Main, - NumParams 1, - CallSpace 2) +Func(Main, + NumParams 0) + + lv_Main->____result__0__0 = NULL; + lv_Main->____result__1__0 = NULL; + + PrepCall(1) + SetParam(0, add_ref(_const_Whole_SP_Number__30)) + Call(Fib, + NumParams 1, 1, Main) + lv_Main->____result__1__0 = cdata->params[0]; + FreeCall + + PrepCall(1) + SetParam(0, add_ref(lv_Main->____result__1__0)) + Call(Print, + NumParams 1, 2, Main) + lv_Main->____result__0__0 = cdata->params[0]; + FreeCall - TCall(Fib, 1) -EndFunc + if(lv_Main->____result__0__0) + { + release_ref(lv_Main->____result__0__0); + } + if(lv_Main->____result__1__0) + { + release_ref(lv_Main->____result__1__0); + } +EndFunc(Main) +DISPATCH -#ifdef _WIN32 - #include "windows.h" -#endif +FuncNoLocals(Print, + NumParams 1) + + if(get_blueprint(cdata->params[0])->type_id == TYPE_INT32) + { + printf("%d\n", ((t_Int32 *)(cdata->params[0]))->num); + } else { + puts("Don't know how to print this type"); + } + release_ref(cdata->params[0]); + Ret(0, make_Int32(0)) +EndFuncNoLocals +DISPATCH + +DO_END: + return; + +_exception: + puts("whoops, exception!"); +} int main(int argc, char ** argv) { - returntype ret; - calldata *cdata; - context * ct; - #ifdef _WIN32 - DWORD dur; - dur = timeGetTime(); - #endif - if(argc < 2) - { - puts("not enought arguments"); - return -1; - } register_builtin_types(); - ct = new_context(); - cdata = alloc_cdata(ct, 1); - cdata->num_params = 1; - cdata->resume = 0; - const_1 = make_Int32(1); - const_2 = make_Int32(2); - cdata->params[0] = make_Int32(atoi(argv[1])); - ret = f_Main(cdata); - while(ret == TAIL_RETURN) - ret = cdata->tail_func(cdata); - if(ret == EXCEPTION_RETURN) { - puts("Exception!"); - return -1; - } - printf("Result: %d\n", ((t_Int32 *)cdata->params[0])->num); - #ifdef _WIN32 - dur = timeGetTime()-dur; - printf("Took %d milliseconds\n", dur); - #endif + _const_Whole_SP_Number__1 = make_Int32(1); + _const_Whole_SP_Number__2 = make_Int32(2); + _const_Whole_SP_Number__30 = make_Int32(30); + + rhope(FUNC_Main); return 0; } -