# HG changeset patch # User Mike Pavone # Date 1280725135 14400 # Node ID e73a93fb5de1ebaa2c1c4b31e1b353fa2271f053 # Parent bcdc326b3d6e93de9227a53839d57ced8510575b Beginning of port of compiler to itself, some bugfixes and a refcount optimization diff -r bcdc326b3d6e -r e73a93fb5de1 backendutils.rhope --- a/backendutils.rhope Sat Jul 31 17:17:23 2010 -0400 +++ b/backendutils.rhope Mon Aug 02 00:58:55 2010 -0400 @@ -1,7 +1,7 @@ Escape Rhope Name NU[name:escaped] { - escaped <- [[[[[[[[[[[[[[[[[[[[[[[name]Replace["_","UN_"] + escaped <- [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[name]Replace["_","UN_"] ]Replace["@","AT_"] ]Replace[" ","SP_"] ]Replace[":","CN_"] @@ -24,6 +24,15 @@ ]Replace["\n", "NL_"] ]Replace["{", "LC_"] ]Replace["}", "RC_"] + ]Replace["[", "LS_"] + ]Replace["]", "RS_"] + ]Replace["#", "HS_"] + ]Replace["\\", "BS_"] + ]Replace["\r", "CR_"] + ]Replace[";", "SC_"] + ]Replace["&", "AM_"] + ]Replace["|", "PI_"] + ]Replace["%", "PC_"] } Escape Rhope Name[name:escaped] diff -r bcdc326b3d6e -r e73a93fb5de1 backendutils_c.rhope --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/backendutils_c.rhope Mon Aug 02 00:58:55 2010 -0400 @@ -0,0 +1,332 @@ + +Escape Rhope Name[name:escaped] +{ + escaped <- [name]Replace[ + ("_", "@", " ", ":", "?", "+", "-", "*", "/", "<", ">", "(", ")", "!", "=", "'", + "\"", "\t", ",", ".", "\n", "{", "}", "[", "]", "#", "\\", "\r", ";", "&", "|", "%", "^"), + ("UN_","AT_","SP_","CN_","QN_","PL_","MN_","TM_","DV_","LT_","GT_","LP_","RP_","NT_","EQ_","PR_", + "DP_","TB_","CM_","PD_","NL_","LC_","RC_", "LS_", "RS_", "HS_", "BS_", "CR_", "SC_", "AM_", "PI_", "PC_", "CT_")] +} + +Blueprint AddRef +{ + Value +} + +AddRef[value:out] +{ + out <- [Build[AddRef()]]Value <<[value] +} + +Make Op@AddRef[addref,func:out] +{ + //TODO: Make me work with other backends + out <- [["add_ref((object *)"]Append[ [[addref]Value >>]Make Op[func] ]]Append[")"] +} + +Strip Addref@AddRef[op:out] +{ + out <- [[op]Value >>]Strip Addref +} + +Make Op@String[string,func:out] +{ + out <- [func]Resolve[string] +} + +Strip Addref@String[op:out] +{ + out <- op +} + +Make Op@Whole Number[num,func:out] +{ + out <- num +} + +Strip Addref@Whole Number[op:out] +{ + out <- op +} + +Make Op@Real Number[num,func:out] +{ + out <- num +} + +Strip Addref@Real Number[op:out] +{ + out <- op +} + +Blueprint Output +{ + Name +} + +Output[name:out] +{ + out <- [Build[Output()]]Name <<[name] +} + +Make Op@Output[op,func:out] +{ + out <- [func]Resolve Output[[op]Name >>] +} + +Strip Addref@Output[op:out] +{ + out <- op +} + +Blueprint Constant +{ + Value + Need Addref +} + +Constant[var:out] +{ + out <- [[Build[Constant()]]Value <<[var]]Need Addref <<[Yes] +} + +Make Op@Constant[const,func:out] +{ + out <- [func]Lookup Constant[[const]Value >>, [const]Need Addref >>] +} + +Strip Addref@Constant[op:out] +{ + out <- [op]Need Addref <<[No] +} + +Blueprint Result +{ + Output Num +} + +Result[num:out] +{ + out <- [Build[Result()]]Output Num <<[num] +} + +Make Op@Result[result,func:out] +{ + out <- [func]Result Reference[[result]Output Num>>] +} + +Strip Addref@Result[op:out] +{ + out <- op +} + +Blueprint Check Result +{ + Output Num +} + +Check Result[num:out] +{ + out <- [Build[Check Result()]]Output Num <<[num] +} + +Make Op@Check Result[result,func:out] +{ + out <- [func]Checked Result Reference[[result]Output Num>>] +} + +Strip Addref@Check Result[op:out] +{ + out <- op +} + +Make Condition[op:out] +{ + If[[Blueprint Of[op]]=[OrValue()]] + { + out <- OrCond[Make Condition[[op]Left >>], Make Condition[[op]Right >>]] + }{ + out <- op + } +} + +Blueprint OrValue +{ + Left + Right +} + +OrValue[left,right:out] +{ + out <- [[Build[OrValue()]]Left <<[left]]Right <<[right] +} + +Make Op@OrValue[orval,func:out] +{ + out <- [func]If Null Else[[orval]Left >>, [orval]Right >>] +} + +Strip Addref@OrValue[op:out] +{ + out <- [[op]Left <<[ [[op]Left >>]Strip Addref ]]Right <<[ [[op]Right >>]Strip Addref ] +} + +Blueprint NotCond +{ + Condition +} + +NotCond[cond:out] +{ + out <- [Build[NotCond()]]Condition <<[[cond]Strip Addref] +} + +Make Op@NotCond[cond,func:out] +{ + out <- ["!"]Append[[[cond]Condition >>]Make Op[func]] +} + +Strip Addref@NotCond[op:out] +{ + out <- op +} + +Blueprint OrCond +{ + Condition1 + Condition2 +} + +OrCond[cond1,cond2:out] +{ + out <- [[Build[OrCond()]]Condition1 <<[[cond1]Strip Addref]]Condition2 <<[[cond2]Strip Addref] +} + +Make Op@OrCond[cond,func:out] +{ + out <- ["("]Append[[[ [[cond]Condition1 >>]Make Op[func] ]Append[" || "]]Append[[ [[cond]Condition2 >>]Make Op[func] ]Append[")"]]] +} + +Strip Addref@OrCond[op:out] +{ + out <- op +} + +Blueprint AndCond +{ + Condition1 + Condition2 +} + +AndCond[cond1,cond2:out] +{ + out <- [[Build[AndCond()]]Condition1 <<[[cond1]Strip Addref]]Condition2 <<[[cond2]Strip Addref] +} + +Make Op@AndCond[cond,func:out] +{ + out <- ["("]Append[[[ [[cond]Condition1 >>]Make Op[func] ]Append[" && "]]Append[[ [[cond]Condition2 >>]Make Op[func] ]Append[")"]]] +} + +Strip Addref@AndCond[op:out] +{ + out <- op +} + +Blueprint Field Ref +{ + Variable + Field +} + +Field Ref[var,field:out] +{ + out <- [[Build[Field Ref()]]Variable <<[var]]Field <<[field] +} + +Make Op@Field Ref[ref,func:out] +{ + out <- [func]Field Result[[ref]Variable >>,[ref]Field >>] +} + +Strip Addref@Field Ref[op:out] +{ + out <- op +} + +Blueprint Type Instance +{ + Name + Params + Variant + Mutable? +} + +Type Instance[raw name:out] +{ + If[[raw name]=[""]] + { + name <- "Any Type" + }{ + name <- raw name + } + out <- [[[[Build[Type Instance()]]Name <<[name]]Params <<[()]]Variant <<["Boxed"]]Mutable? <<[No] +} + +Set Variant@Type Instance[type,variant:out,invalid] +{ + [("Boxed","Naked","Pointer","Raw Pointer")]Find[=[variant,?]] + { + out <- [type]Variant <<[variant] + }{ + invalid <- type + } +} + +=@Type Instance[type,compare:out] +{ + If[[Blueprint Of[compare]] = [Type Instance()]] + { + //TODO: Compare parameters + ,out <- If[[[type]Name >>] = [[compare]Name >>]] + { out <- [[type]Variant >>] = [[compare]Variant >>] } + }{ + out <- [[type]Name >>] = [compare] + } +} + +String@Type Instance[type:out] +{ + typestr <- [[[[type]Name >> + ]Append["("] + ]Append[ [[type]Params >>]Join[", "] ] + ]Append[")"] + If[[type]Mutable? >>] + { + out <- [[ [["("]Append[typestr]]Append[", "] ]Append[[type]Variant >>]]Append[", Mutable)"] + }{ + If[[[type]Variant >>] = ["Boxed"]] + { + out <- Val[typestr] + }{ + out <- [[ [["("]Append[typestr]]Append[", "] ]Append[[type]Variant >>]]Append[", Mutable)"] + } + } +} + +Blueprint Worker Literal +{ + Name + Args +} + +Worker Literal[name:out] +{ + out <- [[Build[Worker Literal()]]Name <<[name]]Args <<[()] +} + +Set Input@Worker Literal[worker,argnum,val:out] +{ + out <- [worker]Args <<[ [[worker]Args >>]Set[argnum, val] ] +} + diff -r bcdc326b3d6e -r e73a93fb5de1 cbackend.rhope --- a/cbackend.rhope Sat Jul 31 17:17:23 2010 -0400 +++ b/cbackend.rhope Mon Aug 02 00:58:55 2010 -0400 @@ -765,9 +765,11 @@ If[[name]=["Call"]] { to call <- [args]Index[0] - out <- [[[[[func]Add Raw Line[[["if (get_blueprint("]Append[Make Op[Strip Addref[to call], func]]]Append[")->type_id == TYPE_WORKER) {"]] + last numparams <- [func]Last NumParams >> + out <- [[[[[[func]Add Raw Line[[["if (get_blueprint("]Append[Make Op[Strip Addref[to call], func]]]Append[")->type_id == TYPE_WORKER) {"]] ]Val Call[to call, Tail[args,1]] ]Add Raw Line["} else {"] + ]Last NumParams <<[last numparams] ]Func Base["Call",args, "Call"] ]Add Raw Line["}"] }{ @@ -1210,7 +1212,7 @@ }{ If[[valtype] = ["String"]] { - out <- [["make_String(\""]Append[ [[[value]Replace["\\", "\\\\"]]Replace["\n", "\\n"]]Replace["\"", "\\\""] ]]Append["\")"] + out <- [["make_String(\""]Append[ [[[[value]Replace["\\", "\\\\"]]Replace["\n", "\\n"]]Replace["\r", "\\r"]]Replace["\"", "\\\""] ]]Append["\")"] }{ If[[valtype]=["Worker Literal"]] { @@ -1280,7 +1282,7 @@ { If[[valtype]=["List"]] { - out <- [Fold[["_Set List Els"]Set Input[3, type reg], "\trhope(FUNC_List, inout, 0, 1);\n", value] + out <- [Fold[["_Set List Els"]Set Input[3, type reg], [text]Append["\trhope(FUNC_List, inout, 0, 1);\n"], value] ]Append[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = inout[0];\n"]] }{ Const Construct C[value,type reg] diff -r bcdc326b3d6e -r e73a93fb5de1 cbackend_c.rhope --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cbackend_c.rhope Mon Aug 02 00:58:55 2010 -0400 @@ -0,0 +1,1577 @@ +Import backendutils_c.rhope + +Blueprint C Method Registry +{ + Lookup + Next ID +} + +C Method Registry[:out] +{ + builtins <- [[[[[[[[[[[[[Dictionary[] + ]Set["+", "METHOD_ADD"] + ]Set["-", "METHOD_SUB"] + ]Set["/", "METHOD_DIV"] + ]Set["*", "METHOD_MUL"] + ]Set["LShift", "METHOD_LSHIFT"] + ]Set["RShift", "METHOD_RSHIFT"] + ]Set["=", "METHOD_EQUALS"] + ]Set[">", "METHOD_GREATER"] + ]Set["<", "METHOD_LESS"] + ]Set["If", "METHOD_IF"] + ]Set["Set Missing Field", "METHOD_SETFIELDMISSING"] + ]Set["Get Missing Field", "METHOD_GETFIELDMISSING"] + ]Set["Missing Method", "METHOD_MISSING"] + out <- [[Build[C Method Registry()]]Lookup <<[builtins]]Next ID<<[0] + +} + +Register Method@C Method Registry[reg,method:out] +{ + [[reg]Lookup >>]Index[method] + { + out <- reg + }{ + method ID <- [reg]Next ID>> + new lookup <- [[reg]Lookup >>]Set[method, ["METHOD_FIRST_USER+"]Append[String[method ID]]] + out <- [[reg]Lookup <<[new lookup]]Next ID <<[[method ID]+[1]] + } +} + +Method ID@C Method Registry[reg,method:out,notfound] +{ + out,notfound <- [[reg]Lookup >>]Index[method] +} + +Blueprint C Field Registry +{ + Lookup + Next ID +} + +C Field Registry[:out] +{ + out <- [[Build[C Field Registry()]]Lookup <<[Dictionary[]]]Next ID<<[1] + +} + +Register Field@C Field Registry[reg,field:out] +{ + [[reg]Lookup >>]Index[field] + { + out <- reg + }{ + field ID <- [reg]Next ID>> + new lookup <- [[reg]Lookup >>]Set[field, field ID] + out <- [[reg]Lookup <<[new lookup]]Next ID <<[[field ID]+[1]] + } +} + +Field ID@C Field Registry[reg,field:out,notfound] +{ + out,notfound <- [[reg]Lookup >>]Index[field] +} + +Blueprint C Type +{ + Name + Fields + Methods + Init + Copy + Cleanup + +} + +C Type[name:out] +{ + out <- [[[[[[Build[C Type()]]Name <<[name]]Fields <<[()]]Methods <<[()]]Init <<["NULL"]]Copy <<["NULL"]]Cleanup <<["NULL"] +} + +Add Field@C Type[ctype,name,type:out] +{ + out <- [ctype]Fields <<[ [[ctype]Fields >>]Append[ [[()]Append[name]]Append[type] ] ] +} + +Add Method@C Type[ctype,name:out] +{ + out <- [ctype]Methods <<[ [[ctype]Methods >>]Append[name] ] +} + +Register Methods@C Type[ctype,method reg:out] +{ + out <- Fold[Register Method[?], method reg, [ctype]Methods >>] +} + +_Register Field C[reg,field:out] +{ + name <- [field]Index[0] + out <- [reg]Register Field[name] +} + +Register Fields@C Type[ctype,field reg:out] +{ + out <- Fold[_Register Field C[?], field reg, [ctype]Fields >>] +} + +Rhope Type to C[type:out,array] +{ + If[[Blueprint Of[type]]=[Type Instance()]] + { + variant <- [type]Variant >> + 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"] } + [[type]Params >>]Index[1] + { param,param <- [pre param]Set Variant[~] } + { param <- Val[pre param] } + child type <- Rhope Type to C[param] + If[[variant] = ["Naked"]] + { + out <- Val[child type] + array <- "[1]" + }{ + out <- [child type]Append[" *"] + array <- "" + } */ + out <- "void *" + array <- "" + }{ + typename <- "Array" + } + primitive <- No + }{ + ,regulartype <- [("Naked","Raw Pointer")]Find[=[variant,?]] + { + [("Int64","Int32","Int16","Int8")]Find[=[[type]Name >>,?]] + { + primitive <- Yes + [[type]Name >>]Slice[3] {} + { typename <- [["int"]Append[~]]Append["_t"] } + }{ + ,regulartype <- [("UInt64","UInt32","UInt16","UInt8")]Find[=[[type]Name >>,?]] + { + primitive <- Yes + [[type]Name >>]Slice[4] {} + { typename <- [["uint"]Append[~]]Append["_t"] } + } + } + } + + Val[regulartype] + { + typename <- [type]Name >> + primitive <- No + } + } + }{ + typename <- type + param <- "Any Type" + variant <- "boxed" + primitive <- No + } + + Val[typename] + { array <- "" } + If[[typename] = ["Any Type"]] + { + out <- "struct object *" + }{ + [("Naked","Raw Pointer")]Find[=[variant,?]] + { + If[primitive] + { + prefix <- "" + }{ + prefix <- "nt_" + } + }{ + prefix <- "t_" + } + + If[[variant]=["Naked"]] + { + postfix <- "" + }{ + postfix <- " *" + } + } + If[primitive] + { + escaped <- Val[typename] + }{ + escaped <- Escape Rhope Name[typename] + } + out <- [[prefix]Append[escaped]]Append[postfix] +} + +_Type Def C Type[text,field:out] +{ + name <- [field]Index[0] + ,postfix <- Rhope Type to C[[field]Index[1]] + { type <- ["\n\t"]Append[~] } + + out <- [[[[text]Append[type]]Append[" "]]Append[[Escape Rhope Name[name]]Append[postfix]]]Append[";"] +} + +Type Def@C Type[ctype:out] +{ + 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[Escape Rhope Name[[ctype]Name >>]]]Append[";"] + ]Append[ + [[[["typedef " + ]Append[Rhope Type to C[ [[[ctype]Fields >>]Index[0]]Index[1] ]] + ]Append[" nt_"] + ]Append[Escape Rhope Name[[ctype]Name >>]] + ]Append[";"] ] + }{ + //HACK!!! + If[[[ctype]Name >>]=["Blueprint"]] + { + out <- "" + }{ + [("Array","Worker")]Find[=[[ctype]Name >>,?]] + { oend <- "\nMObject(" } + { oend <- "\nObject(" } + out <- [Fold[_Type Def C Type[?], "OBegin", [ctype]Fields >>]]Append[ [[oend]Append[Escape Rhope Name[[ctype]Name >>]]]Append[")"] ] + } + } +} + +_Type Init C[type name,method reg,text,method:out] +{ + out <- [[text]Append[[["\n\tadd_method(bp, "]Append[ [method reg]Method ID[method] ]]Append[ [[", MethodName("]Append[Escape Rhope Name[method]]]Append[[","]Append[Escape Rhope Name[type name]]]]]]Append["));"] +} + +_Type Init C Field[type name,field reg,text,field:out] +{ + fname <- [field]Index[0] + out <- [[[[text]Append[[["\n\tadd_getter(bp, "]Append[ String[[field reg]Field ID[fname]] ]]Append[ [[", MethodName("]Append[Escape Rhope Name[[fname]Append[" >>"]]]]Append[[","]Append[Escape Rhope Name[type name]]]]]]Append["));"] + ]Append[[["\n\tadd_setter(bp, "]Append[ String[[field reg]Field ID[fname]] ]]Append[ [[", MethodName("]Append[Escape Rhope Name[[fname]Append[" <<"]]]]Append[[","]Append[Escape Rhope Name[type name]]]]]]Append["));"] +} + +Type Init@C Type[ctype,id,method reg,field reg:out] +{ + [("Array","Worker")]Find[=[[ctype]Name >>, ?]] + { size <- "-1" } + { + [("Int64","Int32","Int16","Int8")]Find[=[[ctype]Name >>, ?]] + { + [[ctype]Name >>]Slice[3] {} + { typename <- [["int"]Append[~]]Append["_t"] } + }{ + [("UInt64","UInt32","UInt16","UInt8")]Find[=[[ctype]Name >>, ?]] + { + [[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 >>]] } + } + } + } + size <- [["sizeof("]Append[typename]]Append[")"] + } + start <- [["\tbp = register_type_byid(" + ]Append[id] + ]Append[ + [[", "]Append[size] + ]Append[ + [", (special_func)"]Append[ + [ + [[[[Escape Rhope Name[[ctype]Init >>] + ]Append[", (special_func)"] + ]Append[Escape Rhope Name[[ctype]Copy >> ]] + ]Append[", (special_func)"] + ]Append[Escape Rhope Name[[ctype]Cleanup >>]] + ]Append[");"]]] ] + 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 +{ + Lookup + Definitions + Next ID +} + +C Type Registry[:out] +{ + out <- [[[Build[C Type Registry()]]Lookup << [ + [[[[[[[[[[[[[[[[[[Dictionary[] + ]Set["UInt8", "TYPE_UINT8"] //1 + ]Set["UInt16", "TYPE_UINT16"] //2 + ]Set["UInt32", "TYPE_UINT32"] //3 + ]Set["UInt64", "TYPE_UINT64"] //4 + ]Set["Int8", "TYPE_INT8"] //5 + ]Set["Int16", "TYPE_INT16"] //6 + ]Set["Int32", "TYPE_INT32"] //7 + ]Set["Int64", "TYPE_INT64"] //8 + ]Set["Boolean", "TYPE_BOOLEAN"] //9 + ]Set["Float32", "TYPE_FLOAT32"] //10 + ]Set["Float64", "TYPE_FLOAT64"] //11 + ]Set["Real Number", "TYPE_FLOAT64"] //12 + ]Set["Blueprint", "TYPE_BLUEPRINT"] //13 + ]Set["Array", "TYPE_ARRAY"] //14 + ]Set["Worker", "TYPE_WORKER"] //15 + ]Set["Method Missing Exception", "TYPE_METHODMISSINGEXCEPTION"] //16 + ]Set["Field Missing Exception", "TYPE_FIELDMISSINGEXCEPTION"] //17 + ]Set["Wrong Type Exception", "TYPE_WRONGTYPEEXCEPTION"]] //18 + ]Definitions << [Dictionary[]] + ]Next ID <<[0] +} + +_Type Defs C[text,def:out] +{ + out <- [[text]Append[[def]Type Def]]Append["\n\n"] +} + +Type Defs@C Type Registry[reg:out] +{ + out <- Fold[_Type Defs C[?], "", [reg]Definitions >>] +} + +_Type Inits C[reg,method reg,field reg,text,def,name:out] +{ + out <- [[text]Append[ [def]Type Init[[reg]Type ID[name], method reg, field reg] ]]Append["\n\n"] +} + +Type Inits@C Type Registry[reg,method reg,field reg:out] +{ + out <- Fold[_Type Inits C[reg, method reg, field reg, ?], "", [reg]Definitions >>] +} + +Register Type@C Type Registry[reg,def:out] +{ + name <- [def]Name >> + [[reg]Lookup >>]Index[name] + { + [[reg]Definitions >>]Index[name] + { + out <- reg + }{ + out <- [reg]Definitions <<[[[reg]Definitions >>]Set[name, def]] + } + }{ + out <- [[[reg]Lookup <<[ [[reg]Lookup >>]Set[name, ["TYPE_FIRST_USER+"]Append[String[[reg]Next ID >>]]] ] + ]Definitions <<[ [[reg]Definitions >>]Set[name, def] ] + ]Next ID <<[ [[reg]Next ID >>]+[1] ] + } +} + +Type ID@C Type Registry[reg,name:out,notfound] +{ + out <- [[reg]Lookup >>]Index[name] {} + { + ,notfound <- If[[name]=["Any Type"]] + { out <- "0" } + } +} + +Simple Type?@C Type Registry[reg,name:yep,nope,notfound] +{ + ,notfound <- [[reg]Definitions >>]Index[name] + { + yep,nope <- If[[[[~]Fields >>]Length] = [1]] + } +} + +Blueprint C Function +{ + Name + Inputs + Outputs + Convention + Variables + Statements + Method Registry + Field Registry + Type Registry + Constants + Input Types + Output Types + Resume Index + Last NumParams +} + +C Function[name,inputs,outputs,convention:out] +{ + out <- C Function With Registry[name,inputs,outputs,convention, C Method Registry[], C Field Registry[], C Type Registry[]] +} + +C Function With Registry[name,inputs,outputs,convention,registry,field reg,type reg:out] +{ + out <- [[[[[[[[[[[[[[Build[C Function()] + ]Name <<[name] + ]Inputs <<[inputs] + ]Outputs <<[outputs] + ]Convention <<[convention] + ]Variables <<[Dictionary[]] + ]Statements <<[()] + ]Method Registry <<[registry] + ]Field Registry <<[field reg] + ]Type Registry <<[type reg] + ]Constants <<[Dictionary[]] + ]Input Types <<[ Fold[Append[?, "Any Type"], (), inputs] ] + ]Output Types <<[ Fold[Append[?, "Any Type"], (), outputs] ] + ]Resume Index <<[1] + ]Last NumParams <<[-1] +} + +Set Input Type@C Function[func,type,input num:out] +{ + out <- [func]Input Types <<[ [[func]Input Types >>]Set[input num, type] ] +} + +Set Output Type@C Function[func,type,output num:out] +{ + out <- [func]Output Types <<[ [[func]Output Types >>]Set[output num, type] ] +} + +Register Constant@C Function[func,name,constant:out] +{ + out <- [func]Constants <<[ [[func]Constants >>]Set[name, constant] ] +} + +Allocate Var@C Function[func,name,type:out] +{ + out <- [func]Variables <<[ [[func]Variables >>]Set[name,type] ] +} + +Add Statement@C Function[func,statement:out] +{ + out <- [func]Statements <<[ [[func]Statements >>]Append[["\t"]Append[[statement]Append[";\n"]]] ] +} + +Add Raw Line@C Function[func,line:out] +{ + out <- [func]Statements <<[ [[func]Statements >>]Append[["\t"]Append[[line]Append["\n"]]] ] +} + +Add Operator Statement@C Function[func,psource1,psource2,pdest,op:out] +{ + source1 <- [psource1]Make Op[func] + source2 <- [psource2]Make Op[func] + dest <- [pdest]Make Op[func] + out <- [func]Add Statement[[[[[dest]Append[" = "]]Append[source1]]Append[op]]Append[source2]] +} + +Add@C Function[func,source1,source2,dest:out] +{ + out <- [func]Add Operator Statement[source1,source2,dest," + "] +} + +Sub@C Function[func,source1,source2,dest:out] +{ + out <- [func]Add Operator Statement[source1,source2,dest," - "] +} + +Multiply@C Function[func,source1,source2,dest:out] +{ + out <- [func]Add Operator Statement[source1,source2,dest," * "] +} + +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," != "] +} + +Move@C Function[func,psource,pdest:out] +{ + source <- [psource]Make Op[func] + dest <- [pdest]Make Op[func] + out <- [func]Add Statement[[[dest]Append[" = "]]Append[source]] +} + +Do 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((object *)"]]Append[source]]Append[")"]] +} + +AddRef No Dest@C Function[func,psource:out] +{ + source <- [psource]Make Op[func] + out <- [func]Add Statement[[["add_ref((object *)"]Append[source]]Append[")"]] +} + +Release@C Function[func,psource:out] +{ + source <- [psource]Make Op[func] + out <- [func]Add Statement[[["release_ref((object *)"]Append[source]]Append[")"]] +} + +Set Null@C Function[func,pdest:out] +{ + dest <- [pdest]Make Op[func] + out <- [func]Add Statement[[dest]Append[" = NULL"]] +} + +Lookup Constant@C Function[func,const,doaddref:out] +{ + var <- ["_const_"]Append[Escape Rhope Name[const]] + If[doaddref] + { + out <- [["add_ref("]Append[var]]Append[")"] + }{ + out <- Val[var] + } +} + +Field Result@C Function[func,var,field:out] +{ + as op <- [var]Make Op[func] + [(String(),String Cat(),String Slice())]Find[=[Blueprint Of[var],?]] + { + [[func]Inputs >>]Find[=[var,?]] + { + type <- [[func]Input Types >>]Index[~] + + }{ + type <- [[func]Variables >>]Index[var] { Print["op refers to a var"] } + { + [[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"] + } + If[[[func]Convention >>] = ["rhope"]] + { + If[[type] = ["Any Type"]] + { + rvar <- Val[as op] + }{ + rvar <- [[[["(("]Append[ Rhope Type to C[type] ]]Append[")("]]Append[as op]]Append["))"] + } + }{ + rvar <- Val[as op] + } + + [[func]Type Registry >>]Simple Type?[[type]Name >>] + { access <- "->" } + { access <- "->payload." } + { + //TODO: Generate some kind of error/exception in this case + access <- "->" + } + out <- [[rvar]Append[access]]Append[Escape Rhope Name[field]] +} + +Read Field@C Function[func,var,field:out,result op] +{ + out <- func + result op <- Field Ref[var,field] +} + +Write 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[ [[func]Field Result[var,field]]Append[" = NULL"] ] +} + +Copy@C Function[func,pdest:out] +{ + dest <- [pdest]Make Op[func] + out <- [func]Add Statement[ [dest]Append[[[" = copy_object("]Append[dest]]Append[")"]] ] +} + +Box@C Function[func,psource,pdest,type:out] +{ + dest <- [pdest]Make Op[func] + source <- [psource]Make Op[func] + out <- [func]Add Statement[ + [[[[[dest + ]Append[" = naked_to_boxed("] + ]Append[ [[func]Type Registry >>]Type ID[[type]Name >>] ] + ]Append[", &"] + ]Append[source] + ]Append[")"] ] +} + +Unbox@C Function[func,psource,pdest:out] +{ + dest <- [pdest]Make Op[func] + source <- [psource]Make Op[func] + out <- [func]Add Statement[ + [[[["boxed_to_naked(" + ]Append[source] + ]Append[", &"] + ]Append[dest] + ]Append[")"] ] +} + +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[" = (void*)("]]Append[source]]Append[" + 1)"] ] +} + +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)"] ] +} + +_Function Arg C[func,val,inputnum:out] +{ + out <- [func]Add Raw Line[ + [[[["SetParam(" + ]Append[String[inputnum]] + ]Append[", "] + ]Append[val] + ]Append[")"] + ] +} + +_Val Function Arg C[func,val,inputnum,worker:out] +{ + out <- [func]Add Raw Line[ + [[[[[["VCSetParam(" + ]Append[worker] + ]Append[", "] + ]Append[String[inputnum]] + ]Append[", "] + ]Append[val] + ]Append[")"] + ] +} + +Method Call@C Function[func,method,args:out] +{ + out <- [func]Call[method,args] +} + +Val Call@C Function[func,to call,args:out] +{ + worker <- Make Op[Strip Addref[to call], func] + rargs <- Map[args, Make Op[?, func]] + + If[[[func]Last NumParams >>] = [-1]] + { + freed <- Val[func] + }{ + freed <- [func]Add Raw Line["FreeCall"] + } + prepped <- [[freed]Add Raw Line[ + [[[["VCPrepCall(" + ]Append[worker] + ]Append[", "] + ]Append[String[[rargs]Length]] + ]Append[")"] ] + ]Last NumParams <<[[rargs]Length] + + + out <- [[[[Fold[_Val Function Arg C[?, ?, ?, worker], prepped, rargs] + ]Add Raw Line[ + [[[[[[[["ValCall(" + ]Append[worker] + ]Append[", "] + ]Append[String[[rargs]Length]] + ]Append[", "] + ]Append[String[[func]Resume Index >>]] + ]Append[", "] + ]Append[Escape Rhope Name[[func]Name >>]] + ]Append[")"]] + ]Add Raw Line["DISPATCH"] + ]Add Raw Line[ + [[[["ValCallPostlude(" + ]Append[String[[func]Resume Index >>]] + ]Append[", "] + ]Append[Escape Rhope Name[[func]Name >>]] + ]Append[")"]] + ]Resume Index <<[ [[func]Resume Index >>]+[1] ] +} + +Call@C Function[func,name,args:out] +{ + If[[name]=["Call@Worker"]] + { + //TODO: Handle case when user explicitly calls the fully qualified version, but the type of the first arg isn't Worker + out <- [func]Val Call[[args]Index[0], Tail[args,1]] + }{ + If[[name]=["Call"]] + { + to call <- [args]Index[0] + last numparams <- [func]Last NumParams >> + out <- [[[[[[func]Add Raw Line[[["if (get_blueprint("]Append[Make Op[Strip Addref[to call], func]]]Append[")->type_id == TYPE_WORKER) {"]] + ]Val Call[to call, Tail[args,1]] + ]Add Raw Line["} else {"] + ]Last NumParams <<[last numparams] + ]Func Base["Call",args, "Call"] + ]Add Raw Line["}"] + }{ + out <- [func]Func Base[Escape Rhope Name[name],args, "Call"] + } + } +} + +Func Base@C Function[func,tocall,args,type:out] +{ + Print[ [[func]Name >>]Append[ [": Func Base("]Append[tocall] ] ] + rargs <- Map[args, Make Op[?, func]] + + 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[String[[rargs]Length]]]Append[")"] ] + ]Last NumParams <<[[rargs]Length] + }{ + prepped <- Val[func] + } + + + out <- [[Fold[_Function Arg C[?], prepped, rargs] + ]Add Raw Line[ + [[[[[[[[[type]Append["("] + ]Append[tocall] + ]Append[", "] + ]Append[String[[rargs]Length]] + ]Append[", "] + ]Append[String[[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[?, func]] + //Assume language = "C" for now + base <- [[[name]Append["("]]Append[ Join[rargs, ", "] ]]Append[")"] + ,do store <- [(String(), String Slice(), String Cat())]Find[=[Blueprint Of[store result], ?]] + { + ,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] +} + +Get Field Call@C Function[func,field,source:out] +{ + out <- [func]Func Base[Escape Rhope Name[[field]Append[" >>"]], [()]Append[source], "Call"] +} + +Set Field Call@C Function[func,field,object,value:out] +{ + out <- [func]Func Base[Escape Rhope Name[[field]Append[" <<"]], [[()]Append[object]]Append[value], "Call"] +} + +Tail Method Call@C Function[func,method,args:out] +{ + out <- [func]Func Base[[[func]Method Registry >>]Method ID[method],args, "TMCall"] +} + +Tail Call@C Function[func,name,args:out] +{ + out <- [func]Func Base[Escape Rhope Name[name],args, "TCall"] +} + +Resolve@C Function[func,op:out] +{ + If[[[func]Convention >>] = ["rhope"]] + { + [[func]Inputs >>]Find[=[op,?]] + { + out <- [["my_cdata->params["]Append[String[~]]]Append[" ]"] + }{ + out <- [[["lv_"]Append[Escape Rhope Name[[func]Name >>]]]Append["->"]]Append[Escape Rhope Name[op]] + } + }{ + out <- Escape Rhope Name[op] + } +} + +Resolve Output@C Function[func,name:out] +{ + If[[[func]Convention >>] = ["rhope"]] + { + out <- [[["lv_"]Append[Escape Rhope Name[[func]Name >>]]]Append["->"]]Append[Escape Rhope Name[name]] + }{ + out <- Escape Rhope Name[name] + } +} + +Instruction Stream@C Function[func:out] +{ + out <- [func]Statements <<[()] +} + +_If C[func, statement:out] +{ + out <- [func]Statements <<[ [[func]Statements >>]Append[ ["\t"]Append[statement] ] ] +} + +Do If@C Function[func,condition,stream:out] +{ + cond <- [condition]Make Op[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 >>] + +} + +Discard Outputs@C Function[func,first to discard:out] +{ + out <- [[[[[func + ]Add Raw Line[[["for(idx = "]Append[String[first to discard]]]Append["; idx < cdata->num_params; ++idx)"]] + ]Add Raw Line["{"] + ]Add Raw Line[" if (cdata->params[idx])"] + ]Add Raw Line[" release_ref(cdata->params[idx]);"] + ]Add Raw Line["}"] +} + +Result Reference@C Function[func,output:out] +{ + out <- [["cdata->params["]Append[String[output]]]Append["]"] +} + +Checked Result Reference@C Function[func,output:out] +{ + out <- [[[["("]Append[output]]Append[" < cdata->num_params ? cdata->params["]]Append[output]]Append["] : NULL)"] +} + + +If Null Else@C Function[func,left,right:out] +{ + check <- [[Make Condition[left]]Strip Addref]Make Op[func] + l <- [left]Make Op[func] + r <- [right]Make Op[func] + out <- [[[[[["(" + ]Append[check] + ]Append[" ? "] + ]Append[l] + ]Append[" : "] + ]Append[r] + ]Append[")"] +} + +_Set Outputs C[string,inputname,inputnum,func:out] +{ + out <- [string]Append[[[ [ ["\tRet("]Append[String[inputnum]] ]Append[ [[", lv_"]Append[Escape Rhope Name[[func]Name >>]]]Append["->"]]]Append[Escape Rhope Name[inputname]]]Append[")\n"]] +} + +Set Outputs@C Function[func:out] +{ + If[[[func]Convention >>] = ["rhope"]] + { + out <- [[[Fold[_Set Outputs C[?, ?, ?, func], "", [func]Outputs >>]]Append["\tNumRet("]]Append[String[[[func]Outputs >>]Length]]]Append[")\n"] + }{ + [[func]Outputs >>]Index[0] + { + out <- [["\treturn "]Append[Escape Rhope Name[~]]]Append[";\n"] + }{ + out <- "" + } + } +} +_Output Defs C[string,varname,index,func:out] +{ + out <- [[[string]Append[ ["\t"]Append[Rhope Type to C[[[func]Output Types >>]Index[index]]] ]]Append[[" "]Append[Escape Rhope Name[varname]]]]Append[";\n"] +} +_Var Defs C[string,type,varname:out] +{ + Print[["Variable: "]Append[varname]] + { + out <- [[[string]Append[ ["\t"]Append[Rhope Type to C[type]] ]]Append[[" "]Append[Escape Rhope Name[varname]]]]Append[";\n"] + { Print["Produced Output"]} + } +} + + +Definitions@C Function[func:out] +{ + Print[["Definitions@C Function: "]Append[[func]Name >>]] + { + If[ [[[func]Convention >>] = ["rhope"]] And [[ [[[func]Variables >>]Length]+[[[func]Outputs >>]Length] ] > [0]] ] + { + localtype <- [[[Fold[_Output Defs C[?, ?, ?, func], Fold[_Var Defs C[?],"typedef struct {\n", [func]Variables >>], [func]Outputs >>]]Append["} lt_"]]Append[Escape Rhope Name[[func]Name >>]]]Append[";\n"] + }{ + localtype <- "" + } + + If[ [[func]Convention >>] = ["rhope"] ] + { + /* 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 <- Val[localtype] + }{ + out <- [[func]Naked Proto]Append[";\n"] + } + } +} + +_Proto Input[list,input,index,types:out] +{ + out <- [list]Append[ [[Rhope Type to C[[types]Index[index]]]Append[" "]]Append[Escape Rhope Name[input]] ] +} + +Naked Proto@C Function[func:out] +{ + [[func]Output Types >>]Index[0] + { + outtype <- [Rhope Type to C[~]]Append[" "] + }{ + outtype <- "void " + } + out <- [[[[outtype + ]Append[ Escape Rhope Name[[func]Name >>]] + ]Append["("] + ]Append[ [Fold[_Proto Input[?, ?, ?, [func]Input Types >>], (), [func]Inputs >>]]Join[", "] ] + ]Append[")"] +} + +Type Check@C Function[func,text,type,input num:out] +{ + If[[type] = ["Any Type"]] + { + out <- text + }{ + out <- [text]Append[ [["\tParam("]Append[input num]]Append[ [[", "]Append[ [[func]Type Registry >>]Type ID[type] ]]Append[")"] ] ] + } +} + +Check Param Type C[text,type,input num,func:out] +{ + [(String(),String Cat(),String Slice())]Find[=[Blueprint Of[type],?]] + { + typename <- type + }{ + typename <- [type]Name >> + } + If[[typename] = ["Any Type"]] + { + out <- text + }{ + out <- [text]Append[[[["\tParam("]Append[String[input num]]]Append[ [","]Append[ [[func]Type Registry >>]Type ID[typename] ] ]]Append[")\n"]] + } +} + +Text@C Function[func:out] +{ + Print[["Text@C Function: "]Append[[func]Name >>]] + If[ [[func]Convention >>] = ["rhope"] ] + { + before <- [[func]Name >>]Partition["@"] {} {} + { + 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[?, ?, ?, func], "", [func]Input Types >>] + If[ [ [[[func]Variables >>]Length]+[[[func]Outputs >>]Length] ] = [0] ] + { + out <- [[[[[[[[ [type]Append["NoLocals("] + ]Append[cname] + ]Append[",\n\tNumParams "] + ]Append[ String[[[func]Inputs >>]Length] ] + ]Append[")\n\n"] + ]Append[param check] + ]Append[ [[func]Statements >>]Join[""] ] + ]Append["EndFuncNoLocals\n"] + ]Append["DISPATCH"] + }{ + If[[[func]Last NumParams >>] = [-1]] + { + freecall <- "" + }{ + freecall <- "\n\tFreeCall\n" + } + out <- [[[[[[[[[[ [type]Append["("] + ]Append[cname] + ]Append[",\n\tNumParams "] + ]Append[ String[[[func]Inputs >>]Length] ] + ]Append[")\n\n"] + ]Append[param check] + ]Append[ [[func]Statements >>]Join[""] ] + ]Append[freecall] + ]Append[[func]Set Outputs] + ]Append[[["EndFunc("]Append[fname]]Append[")\n"]] + ]Append["DISPATCH"] + } + }{ + + out <- [[[ + Fold[_Output Defs C[?, ?, ?, func], + Fold[_Var Defs C[?], [[func]Naked Proto]Append["\n{"], [func]Variables >>], [func]Outputs >>] + ]Append[[[func]Statements >>]Join[""]] + ]Append[[func]Set Outputs] + ]Append["}"] + } +} + +Blueprint C Program +{ + Functions + Method Registry + Field 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]] ] + } +} + +Register Type@C Program[program,def:out] +{ + out <- [[[program]Type Registry <<[ [[program]Type Registry >>]Register Type[def] ] + ]Method Registry <<[ [def]Register Methods[[program]Method Registry >>] ] + ]Field Registry <<[ [def]Register Fields[[program]Field Registry >>] ] +} + +Create Type@C Program[program,name:out] +{ + out <- C Type[name] +} + +Create Function@C Program[program,name,inputs,outputs,convention:out] +{ + out <- C Function With Registry[name,inputs,outputs,convention, [program]Method Registry >>, [program]Field Registry >>, [program]Type Registry >>] +} + +Store Function@C Program[program,func:out] +{ + out <- [program]Functions <<[ [[program]Functions >>]Set[ [func]Name >>, func] ] +} + +Method?@C Program[program,funcname:is,isnot] +{ + is,isnot <- [[program]Method Registry >>]Method ID[funcname] +} + +_Defs C Program[text,func:out] +{ + def <- [func]Definitions + If[[def]=[""]] + { + out <- text + }{ + out <- [text]Append[[def]Append["\n\n"]] + } +} + +_Text C Program[text,func,type reg:out] +{ + out <- [text]Append[[[ [func]Type Registry <<[type reg] ]Text]Append["\n\n"]] +} + +Combine Consts[consts,func:out] +{ + out <- Combine[[func]Constants >>, consts] +} + +_Consts C Program[text,value,name:out] +{ + out <- [text]Append[ [["object * _const_"]Append[Escape Rhope Name[name]]]Append[";\n"] ] +} + +Const Construct C[value,type reg:out] +{ + valtype <- Blueprint Of[value] + [(Int8(),UInt8(),Int16(),UInt16(),Int32(),UInt32(),Int64(),UInt64())]Find[=[valtype,?]] + { + size <- [("8","16","32","64")]Index[[~]/[2]] + If[[~]Mod[2]] + { s <- "UI" } + { s <- "I" } + + out <- [[[[[["make_" + ]Append[s] + ]Append["nt"] + ]Append[size] + ]Append["("] + ]Append[String[value]] + ]Append[")"] + }{ + If[[valtype] = [Type Instance()]] + { + //TODO: Support parametric types + typeid <- [type reg]Type ID[[value]Name >>] + out <- [["make_Blueprint("]Append[typeid]]Append[")"] + }{ + If[[valtype] = [Boolean()]] + { + If[value] + { + out <- "make_Bool(1)" + }{ + out <- "make_Bool(0)" + } + }{ + + [(String(),String Slice(),String Cat())]Find[=[valtype,?]] + { + out <- [["make_String(\""]Append[ [[[value]Replace["\\", "\\\\"]]Replace["\n", "\\n"]]Replace["\"", "\\\""] ]]Append["\")"] + }{ + If[[valtype]=[Worker Literal()]] + { + //TODO: Figure out how to fully support these in nested cases + //or workaround the problem higher up in the food chain + [[value]Args >>]Last + { size <- String[[~]+[1]] } + { size <- "0" } + out <- [[[[[["make_Worker(FUNC_" + ]Append[Escape Rhope Name[[value]Name >>]] + ]Append[", "] + ]Append[size] + ]Append[", "] + ]Append[String[Fold[+[1,?], 0, [value]Args >>]]] + ]Append[")"] + }{ + out <- "UnhandledLiteralType" + } + } + } + } + + } +} + +_Set Worker Params C[text,param,num,type reg,name:out] +{ + out <- [text]Append[ + [[[[[["\t((object **)(((t_Worker *)_const_" + ]Append[name] + ]Append[")+1))["] + ]Append[String[num]] + ]Append["] = "] + ]Append[Const Construct C[param, type reg]] + ]Append[";\n"] ] +} + +_Set Consts C Program[text,value,name,type reg:out] +{ + valtype <- Blueprint Of[value] + [(String(),String Cat(),String Slice(),Worker Literal(),List(),List Leaf())]Find[=[valtype,?]] + { + out <- text + }{ + Const Construct C[value,type reg] + { out <- [text]Append[ [[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = "]]Append[~]]Append[";\n"] ] } + } +} + +_Set List Els[text,el,index,type reg:out] +{ + out <- [[text]Append[ + [["\tinout[1] = " + ]Append[Const Construct C[index,type reg]] + ]Append[ + [[";\n\tinout[2] = " + ]Append[Const Construct C[el, type reg]] + ]Append[";\n"] + ]]]Append["\trhope(FUNC_Set, inout, 3, 3);\n"] +} + +_Set Late Consts C[text,value,name,type reg:out] +{ + valtype <- Blueprint Of[value] + [(String(),String Cat(),String Slice(),Worker Literal(),List(),List Leaf())]Find[=[valtype,?]] + { + If[[~]>[3]] + { + out <- [Fold[_Set List Els[?, ?, ?, type reg], "\trhope(FUNC_List, inout, 0, 1);\n", value] + ]Append[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = inout[0];\n"]] + }{ + Const Construct C[value,type reg] + { init <- [text]Append[ [[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = "]]Append[~]]Append[";\n"] ] } + + If[[valtype]=[Worker Literal()]] + { + out <- Fold[_Set Worker Params C[?, ?, ?, type reg, Escape Rhope Name[name]], init, [value]Args >>] + }{ + out <- Val[init] + } + } + }{ + out <- text + } +} + +_Dispatch Switch Sub[text, num, name:out] +{ + out <- [[[[[text + ]Append["\tResumeEntry("] + ]Append[String[num]] + ]Append[","] + ]Append[name] + ]Append[")\\\n"] +} + +_Dispatch Switch[text,func,raw name:out] +{ + If[[[func]Convention >>] = ["rhope"]] + { + name <- Escape Rhope Name[raw name] + out <- [[text]Append[ [["\tDispatchEntry("]Append[name]]Append[")\\\n"] ] + ]Append[Fold[_Dispatch Switch Sub[?, ?, 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[ [["\tDispatchEntry("]Append[name]]Append[")\\\n"] ] +} + +_Dispatch Enum Sub[text, num, name:out] +{ + out <- [[[[[text + ]Append["\tRES_"] + ]Append[String[num]] + ]Append["_"] + ]Append[name] + ]Append[",\n"] +} + +_Dispatch Enum[text,func,raw name:out] +{ + If[[[func]Convention >>] = ["rhope"]] + { + name <- Escape Rhope Name[raw name] + out <- [[text]Append[ [["\tFUNC_"]Append[name]]Append[",\n"] ] + ]Append[Fold[_Dispatch Enum Sub[?, ?, name], "", Range[1, [func]Resume Index >>]]] + }{ + out <- text + } +} + +_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[?], + [Fold[_Dispatch Enum Methods[?], "", all methods]]Append["\tFUNC_Build,\n\tFUNC_BlueprintSP_Of,\n\tFUNC_ID,\n\tFUNC_BlueprintSP_FromSP_ID,\n"], + [program]Functions >>]] + ]Append["\tEND\n} funcids;\n\n"] + ]Append["#define DispatchEntries \\\n"] + ]Append[Fold[_Dispatch Switch[?], + [Fold[_Dispatch Switch Methods[?], "", all methods]]Append["\tDispatchEntry(Build)\\\n\tDispatchEntry(BlueprintSP_Of)\\\n\tDispatchEntry(ID)\\\n\tDispatchEntry(BlueprintSP_FromSP_ID)\\\n"], + [program]Functions >>]] + ]Append["\tEndEntry\n\n"] +} + +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[?, ?, type], dict, [type]Methods >>] +} + +Field to Types[dict,type:out] +{ + out <- Fold[_Field to Types[?, ?, 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[?, ?, method, 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] +{ + 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\" +#include \"func.h\" +#include \"integer.h\" +#include \"blueprint.h\" +#include \"array.h\" +#include \"worker.h\" +#include \"bool.h\"\n\n" + 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[?, ?, [program]Type Registry >>], "", Filter[[program]Functions >>, Not Native[?]]]] + ]Append["\n +int32_t rhope(uint32_t func, object ** params, uint16_t numparams, uint16_t callspace) +{ + uint16_t resume,idx, vcparam_offset, last_vcparam; + context * ct; + calldata * cdata, *temp_cdata, *my_cdata; + DispatchVar + FuncDef(Build) + FuncDef(BlueprintSP_Of) + FuncDef(ID) + FuncDef(BlueprintSP_FromSP_ID)\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[idx] = params[idx]; + cdata->func = END; +DISPATCH\n"] + ]Append[Fold[Method Dispatch[?, ?, ?, [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 + +Func(ID, NumParams 1) + + Param(0, TYPE_BLUEPRINT) + + lv_ID->id = new_object(TYPE_UINT32); + ((t_UInt32 *)lv_ID->id)->Num = ((t_Blueprint *)cdata->params[0])->bp->type_id; + release_ref(cdata->params[0]); + Ret(0, lv_ID->id) +EndFunc(ID) +DISPATCH + +Func(BlueprintSP_FromSP_ID, NumParams 1) + + Param(0, TYPE_UINT32) + + lv_BlueprintSP_FromSP_ID->type = ((t_UInt32 *)cdata->params[0])->Num; + if (lv_BlueprintSP_FromSP_ID->type >= max_registered_type || !registered_types[lv_BlueprintSP_FromSP_ID->type]) { + Ret(1, cdata->params[0]) + Ret(0, NULL) + } else { + release_ref(cdata->params[0]); + Ret(0, new_object(TYPE_BLUEPRINT)) + ((t_Blueprint *)cdata->params[0])->bp = registered_types[lv_BlueprintSP_FromSP_ID->type]; + Ret(1, NULL) + } + +EndFunc(BlueprintSP_FromSP_ID) +DISPATCH\n"] + ]Append[Fold[_Text C Program[?, ?, [program]Type Registry >>], "", Filter[[program]Functions >>, Native[?]]]] + ]Append[" +DO_END: + for(idx = 0; idx < cdata->num_params; ++idx) + params[idx] = cdata->params[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 -1; +} + +#include \"builtin.c\" +#include \"array.c\" +#include \"worker.c\" + +int main(int argc, char **argv) +{ + blueprint * bp; + int numret; + int idx; + object * inout[2]; + register_builtin_types();\n\n"] + ]Append[ [[program]Type Registry >>]Type Inits[[program]Method Registry >>, [program]Field Registry >>] ] + ]Append[Fold[_Set Consts C Program[?, ?, ?, [program]Type Registry >>], "", constants]] + ]Append[Fold[_Set Late Consts C[?, ?, ?, [program]Type Registry >>], "", constants]] + ]Append[" + rhope(FUNC_List, inout, 0, 1); + for (idx = 0; idx < argc; ++idx) + { + inout[1] = make_String(argv[idx]); + rhope(FUNC_Append, inout, 2, 2); + } + numret = rhope(FUNC_Main, inout, 1, 1); + if (!numret) + return 0; + if (numret < 0) + return numret; + if (get_blueprint(inout[0])->type_id == TYPE_INT32) + return ((t_Int32 *)inout[0])->Num; + + rhope(FUNC_If, inout, 1, 2); + if (inout[0]) + return 0; + return 1; +}\n\n"] + +} + + diff -r bcdc326b3d6e -r e73a93fb5de1 dict.rhope --- a/dict.rhope Sat Jul 31 17:17:23 2010 -0400 +++ b/dict.rhope Mon Aug 02 00:58:55 2010 -0400 @@ -238,14 +238,14 @@ } } -_Print Dict[dict,key] +_Print Seq[dict,key] { val <- String[[dict]Index[key]] - Print[ [[["\t"]Append[String[key]]]Append[": "]]Append[val] ] + Print[ [[["\t"]Append[String[key]]]Append[":\t"]]Append[val] ] { [dict]Next[key] { - _Print Dict[dict, ~] + _Print Seq[dict, ~] } } } @@ -253,7 +253,7 @@ Print@Dictionary[dict:out] { Print["Dictionary"] - { _Print Dict[dict, [dict]First] } + { _Print Seq[dict, [dict]First] } } Print@Empty Dictionary[dict:out] @@ -261,3 +261,39 @@ Print["Dictionary\n\t{Empty}"] } +Length@Empty Dictionary[dict:out] +{ + out <- 0 +} + +Length@Dictionary[dict:out] +{ + If[[dict]Bits >>] + { + out <- [Length[[dict]Straight >>]]+[[Length[[dict]Left >>]] + [Length[[dict]Right >>]]] + }{ + out <- [1]+[[Length[[dict]Left >>]] + [Length[[dict]Right >>]]] + } +} + +_Combine[source,dest,key:out] +{ + new dest <- [dest]Set[key, [source]Index[key]] + [source]Next[key] + { + out <- _Combine[source, new dest, ~] + }{ + out <- Val[new dest] + } +} + +Combine[source,dest:out] +{ + [source]First + { + out <- _Combine[source, dest, ~] + }{ + out <- dest + } +} + diff -r bcdc326b3d6e -r e73a93fb5de1 functional.rhope --- a/functional.rhope Sat Jul 31 17:17:23 2010 -0400 +++ b/functional.rhope Mon Aug 02 00:58:55 2010 -0400 @@ -60,7 +60,7 @@ { ,not found <- [list]First { - loc <- _Find[list,pred,~] + loc,not found <- _Find[list,pred,~] } } diff -r bcdc326b3d6e -r e73a93fb5de1 kernel.rhope --- a/kernel.rhope Sat Jul 31 17:17:23 2010 -0400 +++ b/kernel.rhope Mon Aug 02 00:58:55 2010 -0400 @@ -3,6 +3,7 @@ Import functional.rhope Import file.rhope Import dict.rhope +Import range.rhope Val[in:out] { @@ -46,31 +47,51 @@ read[filedes(Int32,Naked),buf(Array,Raw Pointer,Mutable),nbyte(Int64,Naked):read(Int64,Naked),buf] } -_Print Int32[n,buf:out] +_String 32[n,buf:out] { - If[[n] < [10i32]] + If[[n] < [10u32]] { - byte <- [[n]Trunc Int8] + [48i8] + byte <- [[n]Trunc UInt8] + [48u8] out <- [buf]Append[byte] }{ - next <- [n]/[10i32] + next <- [n]/[10u32] - byte <- [[[n]-[[next]*[10i32]]]Trunc Int8] + [48i8] - out <- [_Print Int32[next, buf]]Append[byte] + byte <- [[[n]-[[next]*[10u32]]]Trunc UInt8] + [48u8] + out <- [_String 32[next, buf]]Append[byte] } } +_String Int32[n:out] +{ + If[[n] < [0i32]] + { + buf <- [Array[]]Append[45u8] + }{ + buf <- Array[] + } + val <- Abs UInt[n] + out <- _String 32[val, buf] +} + +String@Int32[n:out] +{ + out <- String[_String Int32[n]] +} + Print@Int32[n:out] { - If[[n] < [0i32]] - { - val <- [0i32]-[n] - buf <- [Array[]]Append[45i8] - }{ - val <- Val[n] - buf <- Array[] - } - fbuf <- [_Print Int32[val, buf]]Append[10i8] + fbuf <- [_String Int32[n]]Append[10u8] + out <- write[1i32, fbuf, Int64[[fbuf]Length >>]] +} + +String@UInt32[n:out] +{ + out <- String[_String 32[n, Array[]]] +} + +Print@UInt32[n:out] +{ + fbuf <- [_String 32[n, Array[]]]Append[10u8] out <- write[1i32, fbuf, Int64[[fbuf]Length >>]] } @@ -134,6 +155,39 @@ yes,no <- If[[num]!=[0u8]] } +Abs@Int64[num:out] +{ + If[[num]<[0i64]] + { out <- [0i64]-[num] } + { out <- num } +} + +Abs@Int32[num:out] +{ + If[[num]<[0i32]] + { out <- [0i32]-[num] } + { out <- num } +} + +Abs@Int16[num:out] +{ + If[[num]<[0i16]] + { out <- [0i16]-[num] } + { out <- num } +} + +Abs@Int8[num:out] +{ + If[[num]<[0i8]] + { out <- [0i8]-[num] } + { out <- num } +} + +Mod[a,b:out] +{ + out <- [a]-[[[a]/[b]]*[b]] +} + Blueprint Array { @@ -177,7 +231,7 @@ { If[[cur]<[[source]Size >>]] { - [source]Get Input[cur] + [source]Get Input[Int32[cur]] { next <- _internal_worker_setinput[dest, cur, ~] }{ @@ -309,10 +363,11 @@ out <- _internal_array_copyin[farray, index, val] }{ boxed <- _internal_array_allocboxed[[farray]Storage >>] - [array]First + [farray]First { - copied <- _Copy to Boxed[farray, boxed, ~] + copied <- [_Copy to Boxed[farray, boxed, ~]]Length <<[ [farray]Length >> ] }{ + Print["Uh oh, no First on Naked Array!"] //I don't think this case should happen normally copied <- Val[boxed] } diff -r bcdc326b3d6e -r e73a93fb5de1 list.rhope --- a/list.rhope Sat Jul 31 17:17:23 2010 -0400 +++ b/list.rhope Mon Aug 02 00:58:55 2010 -0400 @@ -277,3 +277,57 @@ } } +New Like@List[in:out] +{ + out <- List[] +} + +New Like@List Leaf[in:out] +{ + out <- List[] +} + +//TODO: Implement a more efficent version of this +_Tail[list, cur, dest:out] +{ + ndest <- [dest]Append[[list]Index[cur]] + [list]Next[cur] + { + out <- _Tail[list, ~, ndest] + }{ + out <- Val[ndest] + } +} +Tail[list,start:out] +{ + newlist <- New Like[list] + [list]Index[start] + { + out <- _Tail[list, start, newlist] + }{ + out <- Val[newlist] + } +} + +Concatenate[left,right:out] +{ + out <- Fold[Append[?], left, right] +} + +Print@List Leaf[list:out] +{ + If[[[list]Buffer >>]Length] + { + Print["List"] + { _Print Seq[list, [list]First] } + }{ + Print["List\n\t{Empty}"] + } +} + +Print@List[list:out] +{ + Print["List"] + { _Print Seq[list, [list]First] } +} + diff -r bcdc326b3d6e -r e73a93fb5de1 number.rhope --- a/number.rhope Sat Jul 31 17:17:23 2010 -0400 +++ b/number.rhope Mon Aug 02 00:58:55 2010 -0400 @@ -76,6 +76,27 @@ out <- [backend]Store Function[ffunc] } +Compile Abs UInt Method[backend,type:out] +{ + outtype <- ["U"]Append[type] + func <- [[[[[[[[[backend]Create Function[["Abs UInt@"]Append[type], ("in"), ("out"), "rhope"] + ]Set Input Type[Type Instance[type], 0] + ]Set Output Type[Type Instance[["U"]Append[type]], 0] + ]Register Constant[["Blueprint_"]Append[outtype], Type Instance[outtype]] + ]Allocate Var["abs", Type Instance[type]] + ]Call["Build", [()]Append[ Constant[["Blueprint_"]Append[outtype]] ]] + ]Move[Result[0], "out"] + ]Call["Abs", [()]Append["in"]] + ]Move[Result[0], "abs"] + { Print["After Call to Abs"] } + + ,src <- [func]Read Field["abs", "Num"] + { ,dst <- [~]Write Field["out", "Num"] + { ffunc <- [[~]Move[src, dst]]Release["abs"] }} + + out <- [backend]Store Function[ffunc] +} + _Generate Number Methods[backend, type:out] { opmap <- (("+", "Add"), ("-", "Sub"), ("*", "Multiply"), ("/", "Divide"), ("LShift", "DoLShift"), ("RShift", "DoRShift")) @@ -84,7 +105,15 @@ Fold[["Compile Number Method"]Set Input[2, type], backend, opmap] { Fold[["Compile Number Comp Method"]Set Input[2, type], ~, compops] { Fold[[["Compile Conversion Method"]Set Input[1, type]]Set Input[3, ""], ~, Legal Conversions[type]] - { out <- Fold[[["Compile Conversion Method"]Set Input[1, type]]Set Input[3, "Trunc "], ~, Truncations[type]] }}} + { + almost <- Fold[[["Compile Conversion Method"]Set Input[1, type]]Set Input[3, "Trunc "], ~, Truncations[type]] + If[[type]Starts With["I"]] + { + out <- Compile Abs UInt Method[almost,type] + }{ + out <- Val[almost] + } + }}} } Generate Number Methods[backend:out] @@ -99,11 +128,11 @@ name <- [[method]Append["@"]]Append[type] out <- [[program]Register Worker[name, "rhope", 2, 1] ]Bind Worker[name, - [[[[[NWorker["rhope"] - ]Inputs <<[("left","right")] - ]Input Types <<[ [[()]Append[ Type Instance[type]]]Append[Type Instance[type]] ] - ]Outputs <<[("out")] - ]Output Types <<[ [()]Append[Type Instance[outtype]] ] + [[[[[NWorker["rhope"] + ]Inputs <<[("left","right")] + ]Input Types <<[ [[()]Append[ Type Instance[type]]]Append[Type Instance[type]] ] + ]Outputs <<[("out")] + ]Output Types <<[ [()]Append[Type Instance[outtype]] ] ]Builtin? <<[Yes] ] } @@ -153,7 +182,24 @@ Fold[[register]Set Input[3, type], program, methods] { Fold[[register]Set Input[3, "Boolean"], ~, compmethods] { Fold[[["Register Conversion Method"]Set Input[1, type]]Set Input[3, ""], ~, Legal Conversions[type]] - { out <- Fold[[["Register Conversion Method"]Set Input[1, type]]Set Input[3, "Trunc "], ~, Truncations[type]] }}} + { + almost <- Fold[[["Register Conversion Method"]Set Input[1, type]]Set Input[3, "Trunc "], ~, Truncations[type]] + If[[type]Starts With["I"]] + { + name <- ["Abs UInt@"]Append[type] + out <- [[almost]Register Worker[name, "rhope", 1, 1] + ]Bind Worker[name + [[[[[NWorker["rhope"] + ]Inputs <<[("in")] + ]Input Types <<[ [()]Append[Type Instance[type]] ] + ]Outputs <<[("out")] + ]Output Types <<[ [()]Append[Type Instance[["U"]Append[type]]] ] + ]Builtin? <<[Yes] + ] + }{ + out <- Val[almost] + } + }}} } Register Number Methods[program:out] diff -r bcdc326b3d6e -r e73a93fb5de1 number_c.rhope --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/number_c.rhope Mon Aug 02 00:58:55 2010 -0400 @@ -0,0 +1,207 @@ + + +Compile Number Method[backend, op, type:out] +{ + name <- [[[op]Index[0]]Append["@"]]Append[type] + backend func <- [op]Index[1] + type inst <- Type Instance[type] + func <- [[[[backend]Create Function[name,("a","b"), ("out"), "rhope"] + ]Set Input Type[type inst, 0] + ]Set Input Type[type inst, 1] + ]Set Output Type[type inst, 0] + + ,ina <- [[func]Copy["a"] + ]Read Field["a", "Num"] + { ,inb <- [~]Read Field["b", "Num"] + { ,outa <- [~]Write Field["a", "Num"] + { after op <- [backend func]Call[~, ina, inb, outa] }}} + + out <- [backend]Store Function[ [[after op]Move["a","out"]]Release["b"] ] + +} + +Compile Number Comp Method[backend, op, type:out] +{ + name <- [ [ [op]Index[0] ]Append["@"] ]Append[type] + backend func <- [op]Index[1] + type inst <- Type Instance[type] + func <- [[[[[[ [backend]Create Function[name,("a","b"), ("out"), "rhope"] + ]Set Input Type[type inst, 0] + ]Set Input Type[type inst, 1] + ]Set Output Type[Type Instance["Boolean"], 0] + ]Register Constant["Blueprint_Boolean", Type Instance["Boolean"]] + ]Call["Build", [()]Append[Constant["Blueprint_Boolean"]]] + ]Move[Result[0], "out"] + + ,ina <- [func]Read Field["a", "Num"] + { ,inb <- [~]Read Field["b", "Num"] + { ,outa <- [~]Write Field["out", "Val"] + { after op <- [backend func]Call[~, ina, inb, outa] }}} + + out <- [backend]Store Function[ [[after op]Release["a"]]Release["b"] ] + +} + +Compile Conversion Method[backend,intype,outtype,prefix:out] +{ + func <- [[[[[[backend]Create Function[[prefix]Append[[[outtype]Append["@"]]Append[intype]], ("in"), ("out"), "rhope"] + ]Set Input Type[Type Instance[intype], 0] + ]Set Output Type[Type Instance[outtype], 0] + ]Register Constant[["Blueprint_"]Append[outtype], Type Instance[outtype]] + ]Call["Build", [()]Append[ Constant[["Blueprint_"]Append[outtype]] ]] + ]Move[Result[0], "out"] + + ,src <- [func]Read Field["in", "Num"] + { ,dst <- [~]Write Field["out", "Num"] + { ffunc <- [[~]Move[src, dst]]Release["in"] }} + + out <- [backend]Store Function[ffunc] +} + +Compile Abs UInt Method[backend,type:out] +{ + outtype <- ["U"]Append[type] + func <- [[[[[[[[[backend]Create Function[["Abs UInt@"]Append[type], ("in"), ("out"), "rhope"] + ]Set Input Type[Type Instance[type], 0] + ]Set Output Type[Type Instance[["U"]Append[type]], 0] + ]Register Constant[["Blueprint_"]Append[outtype], Type Instance[outtype]] + ]Allocate Var["abs", Type Instance[type]] + ]Call["Build", [()]Append[ Constant[["Blueprint_"]Append[outtype]] ]] + ]Move[Result[0], "out"] + ]Call["Abs", [()]Append["in"]] + ]Move[Result[0], "abs"] + { Print["After Call to Abs"] } + + ,src <- [func]Read Field["abs", "Num"] + { ,dst <- [~]Write Field["out", "Num"] + { ffunc <- [[~]Move[src, dst]]Release["abs"] }} + + out <- [backend]Store Function[ffunc] +} + +_Generate Number Methods[backend, type:out] +{ + //Old crappy parser doesn't like Worker literals in List literals, work around for now + opmap <- [[[[[[() + ]Append[ [("+")]Append[Add[?]] ] + ]Append[ [("-")]Append[Sub[?]] ] + ]Append[ [("*")]Append[Multiply[?]] ] + ]Append[ [("/")]Append[Divide[?]] ] + ]Append[ [("LShift")]Append[DoLShift[?]] ] + ]Append[ [("RShift")]Append[DoRShift[?]] ] + //(("+", Add[?]), ("-", Sub[?]), ("*", Multiply[?]), ("/", Divide[?]), ("LShift", DoLShift[?]), ("RShift", DoRShift[?])) + compops <- [[[[[[() + ]Append[ [("<")]Append[CompLess[?]] ] + ]Append[ [(">")]Append[CompGreater[?]] ] + ]Append[ [("=")]Append[CompEqual[?]] ] + ]Append[ [("<=")]Append[CompLessEqual[?]] ] + ]Append[ [(">=")]Append[CompGreaterEqual[?]] ] + ]Append[ [("!=")]Append[CompNotEqual[?]] ] + //(("<", CompLess[?]), (">", CompGreater[?]), ("=", CompEqual[?]), ("<=", CompLessEqual[?]), (">=", CompGreaterEqual[?]), ("!=", CompNotEqual[?])) + + Fold[Compile Number Method[?, ?, type], backend, opmap] + { Fold[Compile Number Comp Method[?, ?, type], ~, compops] + { Fold[Compile Conversion Method[?, type, ?, ""], ~, Legal Conversions[type]] + { + almost <- Fold[Compile Conversion Method[?, type, ?, "Trunc "], ~, Truncations[type]] + If[[type]Starts With["I"]] + { + out <- Compile Abs UInt Method[almost,type] + }{ + out <- Val[almost] + } + }}} +} + +Generate Number Methods[backend:out] +{ + numtypes <- ("Int8","Int16","Int32","Int64","UInt8","UInt16","UInt32","UInt64") + + out <- Fold[_Generate Number Methods[?], backend, numtypes] +} + +Register Number Method[program, method, type, outtype:out] +{ + name <- [[method]Append["@"]]Append[type] + out <- [[program]Register Worker[name, "rhope", 2, 1] + ]Bind Worker[name, + [[[[[NWorker["rhope"] + ]Inputs <<[("left","right")] + ]Input Types <<[ [[()]Append[ Type Instance[type]]]Append[Type Instance[type]] ] + ]Outputs <<[("out")] + ]Output Types <<[ [()]Append[Type Instance[outtype]] ] + ]Builtin? <<[Yes] + ] +} + +Register Conversion Method[program, intype, outtype,prefix:out] +{ + name <- [prefix]Append[[[outtype]Append["@"]]Append[intype]] + out <- [[program]Register Worker[name, "rhope", 1, 1] + ]Bind Worker[name, + [[[[[NWorker["rhope"] + ]Inputs <<[("in")] + ]Input Types <<[ [()]Append[Type Instance[intype]] ] + ]Outputs <<[("out")] + ]Output Types <<[ [()]Append[Type Instance[outtype]] ] + ]Builtin? <<[Yes] + ] +} + +Legal Conversions[type:convs] +{ + bigger <- Map[Filter[(16,32,64), >[?, size]], String[?]] + base convs <- Map[bigger, Append["Int", ?]] + If[[type]Starts With["U"]] + { + [type]Slice[4] {} + { size <- Int32[~] } + convs <- Concatenate[base convs, Map[bigger, Append["UInt", ?]]] + }{ + [type]Slice[3] {} + { size <- Int32[~] } + convs <- Val[base convs] + } +} + +Truncations[type:truncs] +{ + u <- [type]Partition["Int"] {} {} + { size <- Int32[~] } + truncs <- Map[Map[Filter[(8,16,32), <[?, size]], String[?]] Append[[u]Append["Int"], ?]] +} + +_Register Number Methods[program,type:out] +{ + methods <- ("+", "-", "*", "/", "LShift", "RShift") + compmethods <- ("<", ">", "=", "<=", ">=", "!=") + register <- Val[Register Number Method[?, ?, type]] + Fold[[register]Set Input[3, type], program, methods] + { Fold[[register]Set Input[3, "Boolean"], ~, compmethods] + { Fold[Register Conversion Method[?, type, ?, ""], ~, Legal Conversions[type]] + { + almost <- Fold[Register Conversion Method[?, type, ?, "Trunc "], ~, Truncations[type]] + If[[type]Starts With["I"]] + { + name <- ["Abs UInt@"]Append[type] + out <- [[almost]Register Worker[name, "rhope", 1, 1] + ]Bind Worker[name + [[[[[NWorker["rhope"] + ]Inputs <<[("in")] + ]Input Types <<[ [()]Append[Type Instance[type]] ] + ]Outputs <<[("out")] + ]Output Types <<[ [()]Append[Type Instance[["U"]Append[type]]] ] + ]Builtin? <<[Yes] + ] + }{ + out <- Val[almost] + } + }}} +} + +Register Number Methods[program:out] +{ + numtypes <- ("Int8","Int16","Int32","Int64","UInt8","UInt16","UInt32","UInt64") + out <- Fold[_Register Number Methods[?], program, numtypes] +} + diff -r bcdc326b3d6e -r e73a93fb5de1 nworker.rhope --- a/nworker.rhope Sat Jul 31 17:17:23 2010 -0400 +++ b/nworker.rhope Mon Aug 02 00:58:55 2010 -0400 @@ -67,6 +67,7 @@ Empty?@Condition Set[set:not empty,empty] { + Print["Empty?@Condition Set"] [[set]Variables >>]First { not empty <- Yes @@ -304,7 +305,12 @@ Add Full Node@NWorker[worker,type,data,inputs,min inputs,outputs,min outputs:out,node index] { - out <- [worker]Nodes <<[[[worker]Nodes >>]Append[ [[NWorker Node[type,data,inputs,outputs]]Min Inputs <<[min inputs]]Min Outputs <<[min outputs] ]] + out <- [worker]Nodes <<[[[worker]Nodes >>]Append[ + [[[NWorker Node[type,data,inputs,outputs] + ]Min Inputs <<[min inputs] + ]Min Outputs <<[min outputs] + ]Wires To <<[List of Lists[[min inputs]+[1]]] + ]] node index <- [[worker]Nodes >>]Length } @@ -678,7 +684,7 @@ [("call","getfield","setfield")]Find[[node]Type >>] { - out <- AddRef[ [[["__result_"]Append[[noderef]Index >>]]Append["_"]]Append[[noderef]IO Num >>] ] + maybe addref <- [[["__result_"]Append[[noderef]Index >>]]Append["_"]]Append[[noderef]IO Num >>] }{ conditions <- [node]Conditions >> @@ -703,6 +709,16 @@ } } } + + Val[maybe addref] + { + If[[Length[[[node]Wires From >>]Index[[noderef]IO Num >>]]] > [1]] + { + out <- AddRef[maybe addref] + }{ + out <- Val[maybe addref] + } + } } Collect Input@NWorker[worker,nodeinput:out] @@ -719,7 +735,7 @@ out <- Val[first] } }{ - out <- No + out <- "Missing" } } @@ -928,22 +944,48 @@ Release Var@NWorker[worker,func,name:out] { //_result_index_ionum + Print[["Release Var@NWorker: "]Append[name]] parts <- [name]Split["_"] - index <- >]Index[index] - do if <- If[[[node]Outputs >>] > [1]] {} + dests <- [[node]Wires From >>]Index[io num] {} + { + Print["oops"] + { Pretty Print[node, ""] + { Pretty Print[parts, ""]}} } + If[[[dests]Length] = [1]] { - ,do if <- [[node]Conditions >>]Empty? + Print["Single dest, maybe release"] + { Print[["Dest index: "]Append[dest index]] } + dest index <- [[dests]Index[0]]Index >> + dest node <- [[worker]Nodes >>]Index[dest index] + { Print["got dest node"] } + + [[dest node]Conditions >>]For Backend { - out <- [func]Release[name] + Print["dest has conditions"] + out <- [func]Do If[AndCond[NotCond[~], name], [[func]Instruction Stream]Release[name]] + { Print["got output"] } + }{ + Print["No conditions on dest, no release needed"] + out <- func } - } + }{ + Print["Multiple (or zero) dests, definitely release"] + do if <- If[[[node]Outputs >>] > [1]] {} + { + do if <- [[node]Conditions >>]Empty? {} + { + out <- [func]Release[name] + } + } - Val[do if] - { - stream <- [[func]Instruction Stream]Release[name] - out <- [func]Do If[name, stream] + Val[do if] + { + stream <- [[func]Instruction Stream]Release[name] + out <- [func]Do If[name, stream] + } } } @@ -1147,7 +1189,7 @@ init vars <- Concatenate[res vars, [with conds]No Release Results] func <- Fold["Set Null", Fold["Set Null", Fold[["Allocate Var"]Set Input[2, "Any Type"], ifunc, init vars], init vars], [worker]Outputs >>] - out <- [program]Store Function[Fold["Release", Fold[["Release Var"]Set Input[0, worker], final func, res vars], [worker]Inputs >>]] + out <- [program]Store Function[Fold["Release", Fold[["Release Var"]Set Input[0, with conds], final func, res vars], [worker]Inputs >>]] }{ out <- Compile Foreign Stub[worker,[program]Link[[worker]Convention >>, [worker]Library >> ],name] } diff -r bcdc326b3d6e -r e73a93fb5de1 runtime/fixed_alloc.c --- a/runtime/fixed_alloc.c Sat Jul 31 17:17:23 2010 -0400 +++ b/runtime/fixed_alloc.c Mon Aug 02 00:58:55 2010 -0400 @@ -3,12 +3,12 @@ #include #include -uint16_t max_free[(MAX_SIZE-MIN_SIZE)/STRIDE]; +uint16_t max_free[(MAX_SIZE-MIN_SIZE)/STRIDE+1]; void fixed_alloc_init() { int i; - for(i = 0; i < (MAX_SIZE-MIN_SIZE)/STRIDE; ++i) + for(i = 0; i < ((MAX_SIZE-MIN_SIZE)/STRIDE+1); ++i) max_free[i] = (BLOCK_SIZE - sizeof(mem_block)+1)*8/((i*STRIDE+MIN_SIZE)*8+1); } diff -r bcdc326b3d6e -r e73a93fb5de1 runtime/fixed_alloc.h --- a/runtime/fixed_alloc.h Sat Jul 31 17:17:23 2010 -0400 +++ b/runtime/fixed_alloc.h Mon Aug 02 00:58:55 2010 -0400 @@ -28,7 +28,7 @@ //num_elements = (BLOCK_SIZE - sizeof(mem_block)+1)*8/(sizeof(element)*8+1) typedef struct { mem_block *freelist; - mem_block *inuse[(MAX_SIZE-MIN_SIZE)/STRIDE]; + mem_block *inuse[(MAX_SIZE-MIN_SIZE)/STRIDE+1]; uint32_t freecount; uint32_t fullcount; } mem_manager; diff -r bcdc326b3d6e -r e73a93fb5de1 string.rhope --- a/string.rhope Sat Jul 31 17:17:23 2010 -0400 +++ b/string.rhope Mon Aug 02 00:58:55 2010 -0400 @@ -144,6 +144,16 @@ } +Int32@String Slice[string:out] +{ + out <- Int32[[string]Flatten] +} + +Int32@String Cat[string:out] +{ + out <- Int32[[string]Flatten] +} + Flatten@String[string:out] { out <- string @@ -248,8 +258,13 @@ { rbyte <- [right]Byte[index] {} { - Print["Could not fetch byte from right string at offset:"] - { Print[index] } + Print[["Could not fetch byte from right string at offset:"]Append[String[index]]] + { Print[["Right string has type ID: "]Append[ String[ID[Blueprint Of[right]]] ]] + { Print[[right]Byte Length] + { Print[[right]Length] + { Print[["Left string has type ID: "]Append[ String[ID[Blueprint Of[left]]] ]] + { Print[[left]Byte Length] + { Print[[left]Length] }}}}}} out <- No } ,out <- If[[~]=[rbyte]] @@ -361,7 +376,7 @@ //TODO: Handle invalid slicepoints sliceoffset <- CPOff to BOff[[[string]Source >>]Buffer >>, 0i32, [string]Offset >>, slicepoint] left <- String Slice[[string]Source >>, [string]Offset >>, slicepoint, [sliceoffset]-[[string]Offset >>]] - right <- String Slice[[string]Source >>, sliceoffset, [[string]Length >>]-[slicepoint], [[[string]Source >>]Byte Length]-[sliceoffset]] + right <- String Slice[[string]Source >>, sliceoffset, [[string]Length >>]-[slicepoint], [[string]Byte Length]-[[sliceoffset]-[[string]Offset >>]]] } Blueprint String Cat @@ -584,3 +599,51 @@ out <- string } +Replace[string,toreplace,with:out] +{ + ,delim,after <-[string]Partition[toreplace] + { + wt <- Blueprint Of[with] + If[ [[[wt]=[String()]] Or [[wt]=[String Slice()]]] Or [[wt]=[String Cat()]] ] + { + replacement <- with + }{ + replacement <- [with]Index[[toreplace]Find[=[delim,?]]] + } + out <- [[~]Append[replacement]]Append[Replace[after,toreplace,with]] + } {} {} { + out <- string + } +} + +_Join[list,delim,current,index:out] +{ + [list]Next[index] + { + out <- _Join[list, delim, [[current]Append[delim]]Append[String[[list]Index[~]]], ~] + }{ + out <- current + } +} + +Join[list,delim:out] +{ + [list]First + { + out <- _Join[list, delim, String[[list]Index[~]], ~] + }{ + out <- "" + } +} + +Starts With[thing,starts with:out] +{ + out <- [[thing]Slice[[starts with]Length]] = [starts with] +} + +Ends With[thing,ends with:out] +{ + ,compare <- [thing]Slice[ [[thing]Length] - [[ends with]Length] ] + out <- [compare] = [ends with] +} + diff -r bcdc326b3d6e -r e73a93fb5de1 testc_c.rhope --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testc_c.rhope Mon Aug 02 00:58:55 2010 -0400 @@ -0,0 +1,88 @@ +Import cbackend_c.rhope +Import number_c.rhope +Import boolean.rhope + +//Dummy versions of nworker methods so the code in number_c.rhope will compile +Register Worker[prog,name,convention,inputs,outputs:out] +{ + out <- prog +} + +Bind Worker[prog,name,worker:out] +{ + out <- prog +} + +Blueprint NWorker +{ + Inputs + Input Types + Outputs + Output Types + Builtin? +} + +NWorker[convention:out] +{ + out <- convention +} + +Main[] +{ + prog <- [[C Program[]]Generate Number Methods]Generate Boolean Methods + base <- [[[[[[[[[prog]Create Function["Fib",("n"),("out"),"rhope"] + ]Allocate Var["work1", Type Instance["Any Type"]] + ]Allocate Var["work2", Type Instance["Any Type"]] + ]Register Constant["const_1", 1] + ]Register Constant["const_2", 2] + ]Set Null["out"] + ]Method Call["<", [[()]Append[AddRef["n"]]]Append[AddRef[Constant["const_2"]]]] + ]Move[Result[0], "work1"] + ]Method Call["If", [()]Append["work1"]] + {Print["base done"] } + + stream1 <- [[[base]Instruction Stream + ]Release[Result[0]] + ]Move[AddRef[Constant["const_1"]], "out"] + {Print["stream1 done"]} + + + + [[[[[[[[[[base]Instruction Stream + ]Release[Result[0]] + ]Method Call["-", [[()]Append[AddRef["n"]]]Append[AddRef[Constant["const_1"]]]] + ]Move[Result[0], "work1"] + ]Method Call["-", [[()]Append[AddRef["n"]]]Append[AddRef[Constant["const_2"]]]] + ]Move[Result[0], "work2"] + ]Call["Fib", ("work1")] + ]Move[Result[0], "work1"] + ]Call["Fib", ("work2")] + ]Move[Result[0], "work2"] + { + stream3 <- [[[~]Instruction Stream + ]Release["n"] + ]Method Call["+", ("work1","work2")] + stream2 <- [~]Do If[NotCond["out"], stream3] + {Print["stream2 done"]} + } + + + func <- [[[base + ]Do If[Result[0], stream1] + ]Do If[Result[1], stream2] + ]Release["n"] + {Print["func done"] } + + main <- [[[[prog]Create Function["Main", (), ("out"), "rhope"] + ]Register Constant["const_30", 30] + ]Call["Fib", [()]Append[Constant["const_30"]]] + ]Move[Result[0], "out"] + + Print[ + [[[prog]Store Function[func] + ]Store Function[main] + ]Text + ] + +} + diff -r bcdc326b3d6e -r e73a93fb5de1 testdictfirstnext.rhope --- a/testdictfirstnext.rhope Sat Jul 31 17:17:23 2010 -0400 +++ b/testdictfirstnext.rhope Mon Aug 02 00:58:55 2010 -0400 @@ -9,5 +9,7 @@ ]Set["fool", "wise"] Print[dict] + {Print[ [[Dictionary[]]Set["work1", "foo"]]Set["work2", "bar"] ] + } } diff -r bcdc326b3d6e -r e73a93fb5de1 testlistliteral.rhope --- a/testlistliteral.rhope Sat Jul 31 17:17:23 2010 -0400 +++ b/testlistliteral.rhope Mon Aug 02 00:58:55 2010 -0400 @@ -1,5 +1,15 @@ + +Print List[idx, list] +{ + [list]Index[idx] + { + Print[~] + { Print List[[idx]+[1], list]} + } +} Main[] { Print[Fold[+[?], 0, (5,27,39,43)]] + { Print List[0, ("foo","bar","baz")] } }