# HG changeset patch # User Mike Pavone # Date 1254181746 14400 # Node ID 9ee9adc696e7f3180b60953462c02b63a80a7f38 # Parent fab5bb137cf95f14eada79574cacac321b323510# Parent 914ad38f9b592e2d2b17569aa56e511db65b5b7b Merged changes diff -r fab5bb137cf9 -r 9ee9adc696e7 backendutils.rhope --- a/backendutils.rhope Mon Sep 28 19:48:45 2009 -0400 +++ b/backendutils.rhope Mon Sep 28 19:49:06 2009 -0400 @@ -47,17 +47,17 @@ Blueprint Constant { - Var + Value } Constant[var:out] { - out <- [Build["Constant"]]Var <<[var] + out <- [Build["Constant"]]Value <<[var] } Make Op@Constant[const,func:out] { - out <- [const]Var >> + out <- [func]Lookup Constant[[const]Value >>] } Blueprint Result @@ -105,3 +105,35 @@ { out <- ["!"]Append[[[cond]Condition >>]Make Op[func]] } + +Blueprint OrCond +{ + Condition1 + Condition2 +} + +OrCond[cond1,cond2:out] +{ + out <- [[Build["OrCond"]]Condition1 <<[cond1]]Condition2 <<[cond2] +} + +Make Op@OrCond[cond,func:out] +{ + out <- ["("]Append[[[[cond]Condition1 >>]Append[" || "]]Append[[[cond]Condition2 >>]Append[")"]]] +} + +Blueprint AndCond +{ + Condition1 + Condition2 +} + +AndCond[cond1,cond2:out] +{ + out <- [[Build["AndCond"]]Condition1 <<[cond1]]Condition2 <<[cond2] +} + +Make Op@AndCond[cond,func:out] +{ + out <- ["("]Append[[[[cond]Condition1 >>]Append[" && "]]Append[[[cond]Condition2 >>]Append[")"]]] +} diff -r fab5bb137cf9 -r 9ee9adc696e7 cbackend.rhope --- a/cbackend.rhope Mon Sep 28 19:48:45 2009 -0400 +++ b/cbackend.rhope Mon Sep 28 19:49:06 2009 -0400 @@ -66,6 +66,7 @@ Variables Statements Method Registry + Constants } C Function[name,inputs,outputs,convention:out] @@ -75,7 +76,7 @@ C Function With Registry[name,inputs,outputs,convention,registry:out] { - out <- [[[[[[[Build["C Function"] + out <- [[[[[[[[Build["C Function"] ]Name <<[name] ]Inputs <<[inputs] ]Outputs <<[outputs] @@ -83,6 +84,16 @@ ]Variables <<[New@Dictionary[]] ]Statements <<[()] ]Method Registry <<[registry] + ]Constants <<[New@Dictionary[]] +} + +Register Constant@C Function[func,name,constant:out] +{ + Print["Register Constant"] + Print[name] + Print[constant] + out <- [func]Constants <<[ [[func]Constants >>]Set[name, constant] ] + { Print["Got register constant output"] } } Allocate Var@C Function[func,name,type:out] @@ -148,12 +159,17 @@ out <- [func]Add Statement[[["release_ref("]Append[source]]Append[")"]] } -Null@C Function[func,pdest:out] +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:out] +{ + out <- [["add_ref(_const_"]Append[Escape Rhope Name[const]]]Append[")"] +} + _Function Arg C[func,val,inputnum:out] { out <- [func]Add Statement[ @@ -171,7 +187,7 @@ Call@C Function[func,name,args:out] { - out <- [func]Func Base[name,args, "Call"] + out <- [func]Func Base[Escape Rhope Name[name],args, "Call"] } Func Base@C Function[func,tocall,args,type:out] @@ -188,7 +204,7 @@ Tail Call@C Function[func,name,args:out] { - out <- [func]Func Base[name,args, "TCall"] + out <- [func]Func Base[Escape Rhope Name[name],args, "TCall"] } Resolve@C Function[func,op:out] @@ -291,23 +307,38 @@ Definitions@C Function[func:out] { - out <- [[[Fold["_Output Defs C", Fold["_Var Defs C","typedef struct {\n", [func]Variables >>], [func]Outputs >>]]Append["} l_"]]Append[Escape Rhope Name[[func]Name >>]]]Append[";\n"] + If[ [ [[[func]Variables >>]Length]+[[[func]Outputs >>]Length] ] > [0]] + { + out <- [[[Fold["_Output Defs C", Fold["_Var Defs C","typedef struct {\n", [func]Variables >>], [func]Outputs >>]]Append["} l_"]]Append[Escape Rhope Name[[func]Name >>]]]Append[";\n"] + }{ + out <- "" + } } Text@C Function[func:out] { cname <- Escape Rhope Name[[func]Name >>] - out <- [[[[[[[[["Func(" - ]Append[cname] - ]Append[",\n\tNumParams "] - ]Append[ [[func]Inputs >>]Length ] - ]Append[",\n\tCallSpace 32,\n\t"]//TODO: Fill in with calculated callspace value - ]Append[["l_"]Append[cname]] - ]Append[")\n\n"] - ]Append[ [[func]Statements >>]Join[""] ] - ]Append[[func]Set Outputs] - ]Append["EndFunc"] - + If[ [[[func]Variables >>]Length] = [0] ] + { + out <- [[[[[["FuncNoLocals(" + ]Append[cname] + ]Append[",\n\tNumParams "] + ]Append[ [[func]Inputs >>]Length ] + ]Append[",\n\tCallSpace 32)\n\n"]//TODO: Fill in with calculated callspace value + ]Append[ [[func]Statements >>]Join[""] ] + ]Append["EndFunc"] + }{ + out <- [[[[[[[[["Func(" + ]Append[cname] + ]Append[",\n\tNumParams "] + ]Append[ [[func]Inputs >>]Length ] + ]Append[",\n\tCallSpace 32,\n\t"]//TODO: Fill in with calculated callspace value + ]Append[["l_"]Append[cname]] + ]Append[")\n\n"] + ]Append[ [[func]Statements >>]Join[""] ] + ]Append[[func]Set Outputs] + ]Append["EndFunc"] + } } Blueprint C Program @@ -338,21 +369,72 @@ _Defs C Program[text,func:out] { - Print["start _Defs"] - out <- [text]Append[[func]Definitions] - { Print["_Defs done"] } + def <- [func]Definitions + If[[def]=[""]] + { + out <- text + }{ + out <- [text]Append[[def]Append["\n\n"]] + } } _Text C Program[text,func:out] { - Print["start _Text"] - out <- [text]Append[[func]Text] - { Print["_Text done"] } + out <- [text]Append[[[func]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"] ] +} + +_Set Consts C Program[text,value,name:out] +{ + //TODO: Support more constant types + out <- [text]Append[[[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = make_Int32("]]Append[value]]Append[");\n"]] } Text@C Program[program:out] { - out <- Fold["_Text C Program", Fold["_Defs C Program", "", [program]Functions >>], [program]Functions >>] + constants <- Fold["Combine Consts", New@Dictionary[], [program]Functions >>] + headers <- "#include +#include \"builtin.h\" +#include \"object.h\" +#include \"context.h\" +#include \"func.h\" +#include \"integer.h\"\n\n" + out <- [[[[headers]Append[Fold["_Text C Program", + Fold["_Consts C Program", + Fold["_Defs C Program", "", [program]Functions >>], + constants + ], [program]Functions >>]] + ]Append["int main(int argc, char **argv) +{ + returntype ret; + calldata *cdata; + context * ct; + register_builtin_types();\n\n"] + ]Append[Fold["_Set Consts C Program", "", constants]] + ]Append[" + ct = new_context(); + cdata = alloc_cdata(ct, 0); + cdata->num_params = 0; + cdata->resume = 0; + ret = f_Main(cdata); + while(ret == TAIL_RETURN) + ret = cdata->tail_func(cdata); + if(ret == EXCEPTION_RETURN) { + puts(\"Exception!\"); + return -1; + } + return 0; +}\n\n"] + } diff -r fab5bb137cf9 -r 9ee9adc696e7 extendlib.rhope --- a/extendlib.rhope Mon Sep 28 19:48:45 2009 -0400 +++ b/extendlib.rhope Mon Sep 28 19:49:06 2009 -0400 @@ -653,5 +653,64 @@ } } +_Collection =[col1,key1,col2,key2:out] +{ + ,out <- If[[key1]=[key2]] + { + ,out <- If[[[col1]Index[key1]] = [[col2]Index[key2]]] + { + nkey1 <- [col1]Next[key1] + { + [col2]Next[key2] + { + out <- _Collection =[col1,nkey1,col2,~] + }{ + out <- No + } + }{ + [col2]Next[key2] + { + out <- No + }{ + out <- Yes + } + } + } + } +} +Collection =[col1,col2:out] +{ + first1 <- [col1]First + { + [col2]First + { + out <- _Collection =[col1, first1, col2, ~] + }{ + out <- No + } + }{ + [col2]First + { + out <- No + }{ + out <- Yes + } + } +} +=@List[list1,list2:out] +{ + ,out <- If[[[list1]Length >>] = [[list2]Length >>]] + { + out <- Collection =[list1,list2] + } +} + +=@Dictionary[list1,list2:out] +{ + ,out <- If[[[list1]Length >>] = [[list2]Length >>]] + { + out <- Collection =[list1,list2] + } +} diff -r fab5bb137cf9 -r 9ee9adc696e7 interp.c --- a/interp.c Mon Sep 28 19:48:45 2009 -0400 +++ b/interp.c Mon Sep 28 19:49:06 2009 -0400 @@ -2703,6 +2703,7 @@ } if(room_index_ret) *room_index_ret = -1; + printf("Error: Can't set field %s on object with blueprint %s\n", name, company[0]->company); return -1; } @@ -2788,6 +2789,8 @@ } if(room_index_ret) *room_index_ret = -1; + printf("Error: Can't get field %s from object with blueprint %s\n", name, company[0]->company); + print_stack_trace(entry->instance); return -1; } diff -r fab5bb137cf9 -r 9ee9adc696e7 interp.h --- a/interp.h Mon Sep 28 19:48:45 2009 -0400 +++ b/interp.h Mon Sep 28 19:49:06 2009 -0400 @@ -50,7 +50,7 @@ #define USER_FLAG 0x8000 #define TYPE_MASK 0xFF -#define START_COMP_STORAGE 40 +#define START_COMP_STORAGE 128 #define START_DEF_STORAGE 100 #ifdef CPLUSPLUS diff -r fab5bb137cf9 -r 9ee9adc696e7 nworker.rhope --- a/nworker.rhope Mon Sep 28 19:48:45 2009 -0400 +++ b/nworker.rhope Mon Sep 28 19:49:06 2009 -0400 @@ -1,6 +1,155 @@ Import extendlib.rhope Import cbackend.rhope + +Blueprint Condition Set +{ + Variables + Subsets + Condition Type +} + +AndSet[:out] +{ + out <- [[[Build["Condition Set"]]Variables <<[New@Dictionary[]]]Subsets <<[New@Dictionary[]]]Condition Type <<["And"] +} + +OrSet[:out] +{ + out <- [[[Build["Condition Set"]]Variables <<[New@Dictionary[]]]Subsets <<[New@Dictionary[]]]Condition Type <<["Or"] +} + +To String@Condition Set[set:out] +{ + out <- [[[[[set]Condition Type >> + ]Append["Set:\n\tVariables:\n\t\t"] + ]Append[ Join[Keys[[set]Variables >>], "\n\t\t"] ] + ]Append["\n\tSubsets:\n\t\t"] + ]Append[ Join[Keys[[set]Subsets >>], "\n\t\t"] ] +} + +Add Condition@Condition Set[set,cond:out] +{ + If[[Type Of[cond]] = ["Condition Set"]] + { + out <- [set]Subsets <<[ [[set]Subsets>>]Set[[cond]To String, cond] ] + }{ + out <- [set]Variables <<[ [[set]Variables >>]Set[cond, Yes] ] + } +} + +=@Condition Set[set1,set2:out] +{ + ,out <- If[[[set1]Condition Type >>] = [[set2]Condition Type >>]] + { + ,out <- If[[[set1]Variables >>] = [[set2]Variables >>]] + { + out,out <- If[[[set1]Subsets >>] = [[set2]Subsets >>]] + } + } +} + +_For Backend Var[current,junk,variable,type:out] +{ + out <- [[>]First + { + not empty <- Yes + }{ + ,empty <- [[set]Subsets >>]First Non-empty Set + { + not empty <- Yes + } + } +} + +_First Non-empty Set[setlist,index:out,none] +{ + current <- [setlist]Index[index] + [[current]Variables >>]First + { + out <- index + }{ + ,trynext <- [[current]Subsets >>]First Non-empty Set + { + out <- index + } + } + Val[trynext] + { + ,none <- [setlist]Next[index] + { + out,none <- _First Non-empty Set[setlist, ~] + } + } +} + +First Non-empty Set[setlist:index,none] +{ + ,none <- [setlist]First + { + index,none <- _First Non-empty Set[setlist,~] + } +} + +For Backend@Condition Set[set:out,none] +{ + Print["For Backend"] + firstvar <- [[set]Variables >>]First + { + Print["At least one var"] + [[set]Variables >>]Next[~] + { + Print["at least two vars"] + vars <- _Fold[[set]Variables >>, ~, firstvar, ["_For Backend Var"]Set Input[3, [set]Condition Type >>]] + }{ + Print["just one var"] + vars <- Val[firstvar] + } + out <- Fold[["_For Backend Subset"]Set Input[2, [set]Condition Type >>], vars, [set]Subsets >>] + { Print["done For Backend, with vars"] } + }{ + Print["no vars"] + [[set]Subsets >>]First Non-empty Set + { + Print[["At least one non-empty subset: "]Append[~]] + firstsub <- [[[set]Subsets >>]Index[~]]For Backend + [[set]Subsets >>]Next[~] + { + Print["at least two subsets"] + out <- _Fold[[set]Subsets >>, ~, firstsub, ["_For Backend Subset"]Set Input[2, [set]Condition Type >>]] + { Print["done with subsets, but no vars"] } + }{ + out <- Val[firstsub] + { Pretty Print[~, "1s:\t"] } + } + }{ + Print["done with none"] + none <- Yes + } + } +} + Set@Range[range,index,val:out] { out <- [[()]Concatenate[range] @@ -52,10 +201,12 @@ Outputs Wires From Wires To + Conditions } -Wire To@NWorker Node[node,from,output,input:out] +Wire To@NWorker Node[node,from,output,pre input:out] { + input <- [pre input]+[1] out <- [node]Wires To <<[ [[node]Wires To >>]Set[input, [[[node]Wires To >>]Index[input] @@ -92,13 +243,14 @@ NWorker Node[type,data,inputs,outputs:out] { - out <- [[[[[[Build["NWorker Node"] + out <- [[[[[[[Build["NWorker Node"] ]Type <<[type] ]Data <<[data] ]Inputs <<[inputs] ]Outputs <<[outputs] ]Wires From <<[List of Lists[outputs]] - ]Wires To <<[List of Lists[inputs]] + ]Wires To <<[List of Lists[[inputs]+[1]]] + ]Conditions <<[AndSet[]] } Blueprint NWorker @@ -107,6 +259,7 @@ Nodes Inputs Outputs + Uses NodeResults Free Temps } @@ -146,19 +299,36 @@ Add Wire@NWorker[worker,from,output,to,input:out] { + Print[[[[[[[[["Add Wire@NWorker["]Append[from]]Append[","]]Append[output]]Append[","]]Append[to]]Append[","]]Append[input]]Append["]"]] + { fromw <- [[[worker]Nodes >>]Index[from]]Wire From[to,input,output] + { Print["fromw"] } tow <- [[[worker]Nodes >>]Index[to]]Wire To[from,output,input] + { Print["tow"] } nodes <- [[[worker]Nodes >>]Set[from, fromw]]Set[to, tow] + { Print["nodes"] } out <- [worker]Nodes <<[nodes] + { Print["Add Wire@NWorker done"] } + } +} + +Uses@NWorker[worker,uses:out] +{ + out <- [worker]Uses <<[uses] } _No Dependencies[list,node,index:out] { - [[node]Wires To>>]First + [[node]Wires To>>]Index[1] { out <- Val[list] }{ - out <- [list]Append[index] + [[[node]Wires To>>]Index[0]]First + { + out <- Val[list] + }{ + out <- [list]Append[index] + } } } @@ -258,6 +428,11 @@ { input name <- [[worker]Inputs >>]Index[ [node]Data >> ] out <- AddRef[input name] + }{ + If[[[node]Type >>] = ["const"]] + { + out <- Constant[[[Type Of[[node]Data >>]]Append["_"]]Append[[node]Data >>]] + } } } } @@ -265,6 +440,7 @@ Collect Input@NWorker[worker,nodeinput:out] { inputchoices <- Map[nodeinput, ["Format Input"]Set Input[0, worker]] + [inputchoices]First { first <- [inputchoices]Index[~] @@ -281,73 +457,190 @@ Collect Inputs@NWorker[worker,node:out] { - out <- Map[[node]Wires To>>, ["Collect Input"]Set Input[0, worker]] + out <- Map[Tail[[node]Wires To>>, 1], ["Collect Input"]Set Input[0, worker]] +} + +Collect Input Condition@NWorker[worker,set,noderef:out] +{ + node <- [[worker]Nodes >>]Index[ [noderef]Index >> ] + If[[[node]Outputs >>] > [1]] + { + out <- [set]Add Condition[ [["__result_"]Append[[noderef]Index >>]]Append[["_"]Append[[noderef]IO Num >>]] ] + }{ + out <- [set]Add Condition[[node]Conditions >>] + } +} + +Collect Condition@NWorker[worker,set,nodeinput:out] +{ + out <- [set]Add Condition[Fold[["Collect Input Condition"]Set Input[0, worker], OrSet[], nodeinput]] +} + +Collect Conditions@NWorker[worker,node:out] +{ + Print["Collect Conditions"] + { + out <- Fold[["Collect Condition"]Set Input[0, worker], AndSet[], [node]Wires To>>] + { Print["done Collect Conditions"] } + } } Save Result[func,num,node index:out] { + Print["Save Result"] out var <- [[["__result_"]Append[node index]]Append["_"]]Append[num] - out <- [[func]Allocate Var[out var, "Any Type"] + Print[out var] + /*out <- [[func]Allocate Var[out var, "Any Type"] ]Move[Result[num], out var] + */ + out <- [func]Move[Result[num], out var] } -Compile Node@NWorker[worker,program,func,nodes,current:out] +Compile Call Node[node,program,func,inputs,node index:out] { - Print[[node]Type >>] + [program]Method?[[[node]Data >>]Name >>] + { + Print["Method!"] + with call <- [func]Method Call[[[node]Data >>]Name >>, inputs] + { Print["Method Call done"] } + }{ + Print["Function!"] + with call <- [func]Call[[[node]Data >>]Name >>, inputs] + } + out <- Fold[["Save Result"]Set Input[2, node index], with call, Range[0, [node]Outputs >>]] +} + +Compile Node@NWorker[worker,program,func,nodes,current:out,out worker] +{ + Print[["Compile Node: "]Append[[node]Type >>]] node index <- [nodes]Index[current] node <- [[worker]Nodes >>]Index[node index] + conditions <- [worker]Collect Conditions[node] If[[[node]Type >>] = ["call"]] { + Print[["Call: "]Append[[[node]Data >>]Name >>]] + inputs <- [worker]Collect Inputs[node] - [program]Method?[[[node]Data >>]Name >>] + [conditions]For Backend { - with call <- [func]Method Call[[[node]Data >>]Name >>, inputs] + Print["Conditional execution, do if"] + stream <- [func]Instruction Stream + nfunc <- [func]Do If[~, nstream] }{ - with call <- [func]Call[[[node]Data >>]Name >>, inputs] + Print["No conditions, full steam ahead"] + stream <- Val[func] + nfunc <- Val[nstream] } - nfunc <- Fold[["Save Result"]Set Input[2, node index], with call, Range[0, [node]Outputs >>]] + nstream <- Compile Call Node[node, program, stream, inputs, node index] }{ If[[[node]Type >>] = ["output"]] { inputs <- [worker]Collect Inputs[node] - nfunc <- [func]Move[[inputs]Index[0], [[worker]Outputs >>]Index[ [node]Data >> ] ] + [conditions]For Backend + { + stream <- [func]Instruction Stream + nfunc <- [func]Do If[~, nstream] + }{ + stream <- Val[func] + nfunc <- Val[nstream] + } + nstream <- [stream]Move[[inputs]Index[0], [[worker]Outputs >>]Index[ [node]Data >> ] ] }{ - nfunc <- Val[func] + If[[[node]Type >>] = ["const"]] + { + //TODO: Handle list constants + nfunc <- [func]Register Constant[[[Type Of[[node]Data >>]]Append["_"]]Append[[node]Data >>], [node]Data >>] + }{ + nfunc <- Val[func] + } + } } + If[[[node]Outputs >>] = [0]] + { + nworker <- Val[worker] + }{ + nworker <- [worker]Nodes <<[ [[worker]Nodes >>]Set[node index, [node]Conditions <<[conditions]] ] + } [nodes]Next[current] { - out <- [worker]Compile Node[program,nfunc,nodes,~] + out,out worker <- [nworker]Compile Node[program,nfunc,nodes,~] }{ out <- Val[nfunc] + out worker <- Val[nworker] } } -Compile Group@NWorker[worker,program,func,groups,current:out] +Compile Group@NWorker[worker,program,func,groups,current:out,out worker] { nodes <- [groups]Index[current] [nodes]First { - nfunc <- [worker]Compile Node[program,func,nodes,~] + nfunc,nworker <- [worker]Compile Node[program,func,nodes,~] }{ nfunc <- Val[func] + nworker <- Val[worker] } [groups]Next[current] { - out <- [worker]Compile Group[program,nfunc,groups,~] + out,out worker <- [nworker]Compile Group[program,nfunc,groups,~] }{ out <- Val[nfunc] + out worker <- Val[nworker] } } -Release Var[func,type,name:out] +Release Var@NWorker[worker,func,name:out] { - out <- [func]Release[name] + //_result_index_ionum + parts <- [name]Split["_"] + index <- >]Index[index] + 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] + } +} + +Result Var[vars,io num,index:out] +{ + out <- [vars]Append[[[["__result_"]Append[index]]Append["_"]]Append[io num]] +} + +Node Result Vars[vars,node,index:out] +{ + If[[[node]Type >>] = ["call"]] + { + out <- Fold[["Result Var"]Set Input[2, index], vars, Range[0, [node]Outputs >>]] + }{ + out <- vars + } +} + +Result Vars@NWorker[worker:out] +{ + out <- Fold["Node Result Vars", (), [worker]Nodes >>] } Compile Worker@NWorker[worker,program,name:out] { - func <- Fold["Null", [program]Create Function[name,[worker]Inputs >>, [worker]Outputs >>, [worker]Convention >>], [worker]Outputs >>] + Print[["Compiling: "]Append[name]] + { + ifunc <- [program]Create Function[name,[worker]Inputs >>, [worker]Outputs >>, [worker]Convention >>] + + res vars <- [worker]Result Vars + func <- Fold["Set Null", Fold["Set Null", Fold[["Allocate Var"]Set Input[2, "Any Type"], ifunc, res vars], res vars], [worker]Outputs >>] + groups <- [worker]Dependency Groups [groups]First { @@ -355,7 +648,8 @@ }{ final func <- Val[func] } - out <- [program]Store Function[Fold["Release Var", final func, [final func]Variables >>]] + out <- [program]Store Function[Fold[["Release Var"]Set Input[0, worker], final func, res vars]] + } } Test[:out] @@ -382,7 +676,59 @@ out <- [Test[]]Compile Worker[C Program[], "Test"] } -Main[] +Blueprint NProgram +{ + Blueprints + Workers + Worker Refs +} + +NProgram[:out] +{ + out <- [[[Build["NProgram"]]Blueprints <<[New@Dictionary[]]]Workers <<[New@Dictionary[]]]Worker Refs <<[New@Dictionary[]] +} + +Bind Worker@NProgram[prog,name,worker:out] +{ + out <- [prog]Workers << [ [[prog]Workers >>]Set[name, worker] ] +} + +Bind Blueprint@NProgram[prog,name,blueprint:out] +{ + out <- [prog]Blueprints << [ [[prog]Blueprints >>]Set[name, blueprint] ] +} + +_Compile Program[backend, worker, name:out] { - Pretty Print[Test Graph to Backend[], ""] + out <- [worker]Compile Worker[backend, name] +} + +Compile Program@NProgram[prog, backend:out] +{ + out <- Fold["_Compile Program", backend, [prog]Workers >>] +} + +Register Worker@NProgram[prog, name, convention, inputs, outputs: out] +{ + Print[["Register Worker "]Append[name]] + out <- [prog]Worker Refs <<[ [[prog]Worker Refs >>]Set[name, Worker Ref[name, convention, inputs, outputs]]] } + +Register Builtins@NProgram[prog:out] +{ + out <- [[[[[[[[[prog]Register Worker["+", "cdecl", 2, 1] + ]Register Worker["-", "cdecl", 2, 1] + ]Register Worker["*", "cdecl", 2, 1] + ]Register Worker["/", "cdecl", 2, 1] + ]Register Worker["Print", "cdecl", 1, 1] + ]Register Worker["Index", "cdecl", 2, 1] + ]Register Worker["If", "cdecl", 1, 2] + ]Register Worker["<", "cdecl", 2, 1] + ]Register Worker[">]Index[name] +} diff -r fab5bb137cf9 -r 9ee9adc696e7 parser_old.rhope --- a/parser_old.rhope Mon Sep 28 19:48:45 2009 -0400 +++ b/parser_old.rhope Mon Sep 28 19:49:06 2009 -0400 @@ -1,6 +1,4 @@ -Import extendlib.rhope - - +Import nworker.rhope Blueprint Parser { @@ -158,79 +156,74 @@ out <- [[[[Build["Worker Node"]]Name <<[name]]Params <<[params]]Assignments <<[New@List[]]]Blocks <<[New@List[]] } -Add List Helper[inlist,worker,program,key,parse worker,refs:out,worker,refs] +Add List Helper[inlist,worker,program,key,parse worker,refs:out list,out worker,out refs] { - Print[Type Of[inlist]] - Print["Add List Helper"] ,nextworker,nextrefs <- [[inlist]Index[key]]Add to Worker[worker, program, parse worker, refs] { nextlist <- [inlist]Set[key, ~] } [inlist]Next[key] { - Print["Next!"] - list,worker,refs <- Add List Helper[nextlist, nextworker, program, ~, parse worker, nextrefs] + out list,out worker,out refs <- Add List Helper[nextlist, nextworker, program, ~, parse worker, nextrefs] }{ - Print["Done!"] - list <- Val[nextlist] - worker <- Val[nextworker] - refs <- Val[nextrefs] + out list <- Val[nextlist] + out worker <- Val[nextworker] + out refs <- Val[nextrefs] } } -Add List to Worker[list,worker,program,parse worker,refs:list,worker,refs] +Add List to Worker[list,worker,program,parse worker,refs:out list,out worker,out refs] { - Print["Add List to Worker"] - Print[Type Of[list]] - { [list]First { - list,worker,refs <- Add List Helper[list, worker, program, ~, parse worker, refs] - { Print["Done!!"] } + out list,out worker,out refs <- Add List Helper[list, worker, program, ~, parse worker, refs] }{ - list <- list - worker <- worker - refs <- refs - } + out list <- list + out worker <- worker + out refs <- refs } } -_Add Blocks to Worker[blocks,worker,program,parse worker,key,refs:blocks,worker,refs] +_Add Blocks to Worker[blocks,worker,program,parse worker,key,refs:out blocks,out worker,out refs] { block, next worker, nextrefs <- Add List to Worker[[blocks]Index[key], worker, program, parse worker, refs] next blocks <- [blocks]Set[key, block] [blocks]Next[key] { - blocks,worker,refs <- _Add Blocks to Worker[next blocks, next worker, program, parse worker, ~, nextrefs] + out blocks,out worker,out refs <- _Add Blocks to Worker[next blocks, next worker, program, parse worker, ~, nextrefs] }{ - blocks <- Val[next blocks] - worker <- Val[next worker] - refs <- Val[nextrefs] + out blocks <- Val[next blocks] + out worker <- Val[next worker] + out refs <- Val[nextrefs] } } -Add Blocks to Worker[blocks,worker,program,parse worker,refs:blocks,worker,refs] +Add Blocks to Worker[blocks,worker,program,parse worker,refs:out blocks,out worker,out refs] { + Print["Add Blocks to Worker"] + { [blocks]First { - blocks, worker, refs <- _Add Blocks to Worker[blocks, worker, program, parse worker, ~, refs] + out blocks, out worker, out refs <- _Add Blocks to Worker[blocks, worker, program, parse worker, ~, refs] }{ - blocks <- blocks - worker <- worker - refs <- refs + out blocks <- blocks + out worker <- worker + out refs <- refs + } } } -Add to Worker@Worker Node[node,worker,program,parse worker,refs:node,worker,refs] +Add to Worker@Worker Node[node,worker,program,parse worker,refs:out node,out worker,out refs] { [program]Find Worker[[node]Name >>] { after worker <- [worker]Add Worker Call[~] {} { + //Print[[[[node]Name >>]Append[" has index "]]Append[~]] assignment refs <- Fold[[["Assignment Save Reference"]Set Input[3, parse worker]]Set Input[4, ~], refs, [node]Assignments >>] [node]Index <<[~] { params list, params worker, params refs <- Add List to Worker[[~]Params >>, after worker, program, parse worker, assignment refs] - block list, worker, refs <- Add Blocks to Worker[[~]Blocks >>, params worker, program, parse worker, params refs] - node <- [[~]Params <<[params list]]Blocks <<[block list] + block list, out worker, out refs <- Add Blocks to Worker[[~]Blocks >>, params worker, program, parse worker, params refs] + out node <- [[~]Params <<[params list]]Blocks <<[block list] } } }{ @@ -240,37 +233,87 @@ Add Multi Wire[worker,ref,junk,end index,input num:out] { + Print[ + [[[[[[["Wire: " + ]Append[ [ref]Index >> ] + ]Append[","] + ]Append[ [ref]Output Number >>] + ]Append[" to "] + ]Append[end index] + ]Append[","] + ]Append[input num] + ] out <- [worker]Add Wire[[ref]Index >>, [ref]Output Number >>, end index, input num] + { Print["Wire added"] } } Add Param Wire[worker,param,input num,end index,blocks,parse worker,assignments:out] { + Print[[[["param type: " + ]Append[Type Of[param]] + ]Append[", dest index:"] + ]Append[end index]] + { param worker, start index, output num <- [param]Add Wires[worker, blocks, parse worker, assignments] {} { + Print[ + [[[[[[["Wire: " + ]Append[start index ] + ]Append[","] + ]Append[output num] + ]Append[" to "] + ]Append[end index] + ]Append[","] + ]Append[input num] + ] { out <- [param worker]Add Wire[start index, output num, end index, input num] + { Print["wire done, Add Param Wire"] } + } }{}{ out <- Fold[[["Add Multi Wire"]Set Input[3, end index]]Set Input[4, input num], param worker, ~] } + } } _Add Block Wire[worker,node,junk,blocks,parse worker,assignments:out] { - out <- [node]Add Wires[worker, blocks, parse worker, assignments] + Print[["_Add Block Wire "]Append[Type Of[node]]] + { + out <- [node]Add Wires[worker, blocks, parse worker, assignments] + { Print[["_Add Block Wire done "]Append[Type Of[node]]] } + } } Add Block Wire[worker,block nodes,output num,parent index,existing blocks,parse worker,assignments:out] { + Print["Add Block Wire"] + { blocks <- [existing blocks]Append[New@Output Reference[parent index, output num]] out <- Fold[[[["_Add Block Wire"]Set Input[3, blocks]]Set Input[4, parse worker]]Set Input[5, assignments], worker, block nodes] + { Print["done: Add Block Wire"] } + } } -Assignments Add Wires[worker,assignment,output num,parse worker,start index:worker] +Assignments Add Wires[worker,assignment,output num,parse worker,start index:out worker] { + Print[["Assignments Add Wires: "]Append[assignment]] + { [[parse worker]Outputs >>]Find[assignment] { ,output index <- [worker]Add Output[assignment, ~] { - worker <- [~]Add Wire[start index, output num, output index, 0] + Print[ + [[[[[[["Wire: " + ]Append[ start index ] + ]Append[","] + ]Append[ output num] + ]Append[" to "] + ]Append[output index] + ]Append[","] + ]Append[ 0 ] + ] + out worker <- [~]Add Wire[start index, output num, output index, 0] + { Print["wire done, Assignments Add Wires 1"] } } }{ //Ugly hack alert! @@ -279,13 +322,16 @@ parts <- [assignment]Split["::"] ,global index <- [worker]Add Global Set[[parts]Index[0], [parts]Index[1]] { - worker <- [~]Add Wire[start index, output num, global index, 0] + out worker <- [~]Add Wire[start index, output num, global index, 0] + { Print["wire done, Assignments Add Wires 2"] } } }{ - worker <- worker + Print["Pipe assignment, do nothing"] + { out worker <- worker } } } } +} Has Block@Worker Node[junk:out,unused] { @@ -319,14 +365,23 @@ Add Wires@Worker Node[node,worker,blocks,parse worker,assignments:worker,index,num,unused] { + Print["Add Wires@Worker Node"] + { worker,index,num <- Add Wires Worker or Field[node, worker, blocks, parse worker, assignments] + } } Add Wires Worker or Field[node,worker,blocks,parse worker,assignments:worker,index,num,unused] { + Print["Add Wires Worker or Field"] + { Fold[[["Assignments Add Wires"]Set Input[3, parse worker]]Set Input[4, [node]Index >>], worker, [node]Assignments >>] - { Fold[[[[["Add Block Wire"]Set Input[3, [node]Index >>]]Set Input[4, blocks]]Set Input[5, parse worker]]Set Input[6, assignments], ~, [node]Blocks >>] - { params worker <- Fold[[[[["Add Param Wire"]Set Input[3, [node]Index >>]]Set Input[4, blocks]]Set Input[5, parse worker]]Set Input[6, assignments], ~, [node]Params >>] }} + { + Print["Assignments Add Wires done"] + Fold[[[[["Add Block Wire"]Set Input[3, [node]Index >>]]Set Input[4, blocks]]Set Input[5, parse worker]]Set Input[6, assignments], ~, [node]Blocks >>] + { + Print["Add Block Wire fold done"] + params worker <- Fold[[[[["Add Param Wire"]Set Input[3, [node]Index >>]]Set Input[4, blocks]]Set Input[5, parse worker]]Set Input[6, assignments], ~, [node]Params >>] }} If[Has Block Params[[node]Params >>]] { worker <- Val[params worker] @@ -340,6 +395,7 @@ } index <- [node]Index >> num <- 0 + } } Blueprint Field Node @@ -362,7 +418,7 @@ out <- [[[[[Build["Field Node"]]Name <<[name]]Assignments <<[New@List[]]]Blocks <<[New@List[]]]Set? <<[set]]Params <<[params] } -Add to Worker@Field Node[node,worker,program,parse worker,refs:node,worker,refs,unused] +Add to Worker@Field Node[node,worker,program,parse worker,refs:out node,out worker,out refs] { If[[node]Set? >>] { @@ -370,21 +426,24 @@ }{ after worker,index <- [worker]Add Object Get[[node]Name >>] } - index + Val[index] { assignment refs <- Fold[[["Assignment Save Reference"]Set Input[3, parse worker]]Set Input[4, ~], refs, [node]Assignments >>] [node]Index <<[~] { params list, params worker, params refs <- Add List to Worker[[~]Params >>, after worker, program, parse worker, assignment refs] - block list, worker, refs <- Add Blocks to Worker[[~]Blocks >>, params worker, program, parse worker, params refs] - node <- [[~]Params <<[params list]]Blocks <<[block list] + block list, out worker, out refs <- Add Blocks to Worker[[~]Blocks >>, params worker, program, parse worker, params refs] + out node <- [[~]Params <<[params list]]Blocks <<[block list] } } } Add Wires@Field Node[node,worker,blocks,parse worker,assignments:worker,index,num] { + Print["Add Wires@Field Node"] + { worker,index,num <- Add Wires Worker or Field[node, worker, blocks, parse worker, assignments] + } } Blueprint Named Pipe Node @@ -416,12 +475,13 @@ out <- [[[Build["Named Pipe Node"]]Name <<[name]]Assignments <<[New@List[]]]Blocks <<[New@List[]] } -Add to Worker@Named Pipe Node[node,worker,program,parse worker,refs:node,worker,refs] +Add to Worker@Named Pipe Node[node,worker,program,parse worker,refs:out node,out worker,out refs] { [[parse worker]Inputs >>]Find[[node]Name >>] { after add <- [worker]Add Input[[node]Name >>, ~] {} { + Print[["Added input, node:"]Append[~]] assign refs <- Fold[[["Assignment Save Reference"]Set Input[3, parse worker]]Set Input[4, ~], refs, [node]Assignments >>] index node <- [node]Index <<[~] } @@ -431,12 +491,14 @@ //TODO: Handle assignments from a named pipe that isn't an input assign refs <- refs } - block list, worker, refs <- Add Blocks to Worker[[node]Blocks >>, after add, program, parse worker, assign refs] - node <- [index node]Blocks <<[block list] + block list, out worker, out refs <- Add Blocks to Worker[[node]Blocks >>, after add, program, parse worker, assign refs] + out node <- [index node]Blocks <<[block list] } -Add Wires@Named Pipe Node[node,worker,blocks,parse worker,assignments:worker,index,num,unused] +Add Wires@Named Pipe Node[node,worker,blocks,parse worker,assignments:worker,index,num,reflist] { + Print["Add Wires@Named Pipe Node"] + { reflist <- [assignments]Index[[node]Name >>] { //TODO: Fix support for a named pipe with a block @@ -470,7 +532,8 @@ } } } - index <- my index + } + index <- Val[my index] worker <- Fold[[[[["Add Block Wire"]Set Input[3, my index]]Set Input[4, blocks]]Set Input[5, parse worker]]Set Input[6, assignments], wires worker, [node]Blocks >>] } @@ -527,31 +590,40 @@ out <- [[[Build["Literal Node"]]Value <<[value]]Assignments <<[New@List[]]]Blocks <<[New@List[]] } -Add to Worker@Literal Node[node,worker,unused,parse worker,refs:out node,out worker,refs,unused] +Add to Worker@Literal Node[node,worker,unused,parse worker,refs:out node,out worker,out refs] { out worker <- [worker]Add Constant[[node]Value >>] {} { - refs <- Fold[[["Assignment Save Reference"]Set Input[3, parse worker]]Set Input[4, ~], refs, [node]Assignments >>] + out refs <- Fold[[["Assignment Save Reference"]Set Input[3, parse worker]]Set Input[4, ~], refs, [node]Assignments >>] out node <- [node]Index <<[~] } } Add Wires@Literal Node[node,worker,blocks,parse worker,assignments:worker,index,num,unused] { + Print["Add Wires@Literal Node"] + { worker,index,num <- Add Wires Literal or Global[node, worker, blocks, parse worker, assignments] + } } Add Wires Literal or Global[node,worker,blocks,parse worker,junk:worker,index,num,unused] { + Print["Add Wires Literal or Global"] + { assignments worker <- Fold[[["Assignments Add Wires"]Set Input[3, parse worker]]Set Input[4, [node]Index >>], worker, [node]Assignments >>] + { Print["Done fold Assignments Add Wires"] } [blocks]Peek { + Print["Add wire"] worker <- [assignments worker]Add Wire[[~]Index >>, [~]Output Number>>, [node]Index >>, [0]-[1]] + { Print["Add wire done"] } }{ worker <- Val[assignments worker] } index <- [node]Index >> num <- 0 + } } Blueprint Block Node @@ -1115,13 +1187,34 @@ } } +Register Workers Compile[prog, worker, name:out] +{ + Print[["Registering "]Append[name]] + out <- [prog]Register Worker[name, "cdecl", [[worker]Inputs >>]Length, [[worker]Outputs >>]Length] +} + +Add Workers Compile[prog, worker, name:out] +{ + Print[["Add Workers Compile: "]Append[name]] + { + trees, nworker, refs <- Add List to Worker[[worker]Trees >>, [NWorker["cdecl"]]Uses[[worker]Uses Stores >>], prog, worker, New@Dictionary[]] + final nworker <- Fold[[["Add Wires Helper"]Set Input[3, worker]]Set Input[4, refs], nworker, trees] + out <- [prog]Bind Worker[name, final nworker] + } +} + Add Wires Helper[worker,node,unused,parse worker,assignments:out] { + Print["Add Wires Helper"] + { out <- [node]Add Wires[worker, New@List[], parse worker, assignments] + { Print["Add Wires Helper Done"] } + } } Add Contents[parse worker,name,program:out,key] { + Print[["Add Contents: "]Append[name]] worker <- [[program]Find Worker[name]]Uses[[parse worker]Uses Stores >>] trees, contents worker, refs <- Add List to Worker[[parse worker]Trees >>, worker, program, parse worker, New@Dictionary[]] Fold[[["Add Wires Helper"]Set Input[3, parse worker]]Set Input[4, refs], contents worker, trees] @@ -1165,6 +1258,12 @@ { Print["done"] } } +Tree to Program Native[parse tree:out] +{ + registered <- Fold["Register Workers Compile", [NProgram[]]Register Builtins, [parse tree]Workers >>] + out <- Fold["Add Workers Compile", registered, [parse tree]Workers >>] +} + Needs Imports[needs import,not imported?,name:out] { If[not imported?] @@ -1331,16 +1430,22 @@ Main[args] { - [args]Index[1] + Print["start"] + fname <- [args]Index[1] { file <- +#include void register_builtin_type(uint32_t type) { @@ -34,3 +35,17 @@ register_builtin_type(i); } +//TODO: Remove this when it's possible to write Print in Rhope +FuncNoLocals(Print, + NumParams 1, + CallSpace 0) + + if(get_blueprint(cdata->params[0]) == get_blueprint_byid(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)) +EndFunc diff -r fab5bb137cf9 -r 9ee9adc696e7 runtime/builtin.h --- a/runtime/builtin.h Mon Sep 28 19:48:45 2009 -0400 +++ b/runtime/builtin.h Mon Sep 28 19:49:06 2009 -0400 @@ -2,6 +2,9 @@ #define _BUILTIN_H_ #include "plat_types.h" +#include "object.h" +#include "func.h" + //Builtin Types enum { TYPE_UINT8 = 1, @@ -43,4 +46,7 @@ void register_builtin_types(); void register_builtin_type(uint32_t type); +//TODO: Remove this when it's possible to write Print in Rhope +FuncDef(Print) + #endif //_BUILTIN_H_ diff -r fab5bb137cf9 -r 9ee9adc696e7 runtime/object.c --- a/runtime/object.c Mon Sep 28 19:48:45 2009 -0400 +++ b/runtime/object.c Mon Sep 28 19:49:06 2009 -0400 @@ -341,3 +341,11 @@ } bp->method_lookup[methodid-bp->first_methodid] = impl; } + +blueprint * get_blueprint_byid(uint32_t type) +{ + if(type >= max_registered_type) + return NULL; + return registered_types[type]; +} + diff -r fab5bb137cf9 -r 9ee9adc696e7 runtime/object.h --- a/runtime/object.h Mon Sep 28 19:48:45 2009 -0400 +++ b/runtime/object.h Mon Sep 28 19:49:06 2009 -0400 @@ -75,6 +75,7 @@ returntype convert_from(uint32_t convertfrom, calldata * params); object * copy_object(object * tocopy); returntype coerce_value(uint32_t type, calldata * params); +blueprint * get_blueprint_byid(uint32_t type); #define INITIAL_TYPE_STORAGE 32 #define INITIAL_METHOD_LOOKUP 8 diff -r fab5bb137cf9 -r 9ee9adc696e7 worker.c --- a/worker.c Mon Sep 28 19:48:45 2009 -0400 +++ b/worker.c Mon Sep 28 19:49:06 2009 -0400 @@ -107,7 +107,10 @@ returnval = worker_populate_inputs(inputWorker, ioList, inputlist); release_ref(ioList); if(returnval) + { + print_stack_trace(worker_entry->instance); return returnval; + } if(work->def->program != worker_entry->instance->def->program) { prep_program(work->def->program ); }