comparison nworker.rhope @ 30:914ad38f9b59

Compiler now works for some simple programs
author Mike Pavone <pavone@retrodev.com>
date Mon, 28 Sep 2009 19:42:33 -0400
parents e9272f7ebd26
children df038cef648b
comparison
equal deleted inserted replaced
29:3cc5e4a42344 30:914ad38f9b59
1 Import extendlib.rhope 1 Import extendlib.rhope
2 Import cbackend.rhope 2 Import cbackend.rhope
3
4
5 Blueprint Condition Set
6 {
7 Variables
8 Subsets
9 Condition Type
10 }
11
12 AndSet[:out]
13 {
14 out <- [[[Build["Condition Set"]]Variables <<[New@Dictionary[]]]Subsets <<[New@Dictionary[]]]Condition Type <<["And"]
15 }
16
17 OrSet[:out]
18 {
19 out <- [[[Build["Condition Set"]]Variables <<[New@Dictionary[]]]Subsets <<[New@Dictionary[]]]Condition Type <<["Or"]
20 }
21
22 To String@Condition Set[set:out]
23 {
24 out <- [[[[[set]Condition Type >>
25 ]Append["Set:\n\tVariables:\n\t\t"]
26 ]Append[ Join[Keys[[set]Variables >>], "\n\t\t"] ]
27 ]Append["\n\tSubsets:\n\t\t"]
28 ]Append[ Join[Keys[[set]Subsets >>], "\n\t\t"] ]
29 }
30
31 Add Condition@Condition Set[set,cond:out]
32 {
33 If[[Type Of[cond]] = ["Condition Set"]]
34 {
35 out <- [set]Subsets <<[ [[set]Subsets>>]Set[[cond]To String, cond] ]
36 }{
37 out <- [set]Variables <<[ [[set]Variables >>]Set[cond, Yes] ]
38 }
39 }
40
41 =@Condition Set[set1,set2:out]
42 {
43 ,out <- If[[[set1]Condition Type >>] = [[set2]Condition Type >>]]
44 {
45 ,out <- If[[[set1]Variables >>] = [[set2]Variables >>]]
46 {
47 out,out <- If[[[set1]Subsets >>] = [[set2]Subsets >>]]
48 }
49 }
50 }
51
52 _For Backend Var[current,junk,variable,type:out]
53 {
54 out <- [[<String@Worker[[type]Append["Cond"]]]Do[ [[()]Append[current]]Append[variable] ]]Index[0]
55 }
56
57 _For Backend Subset[current,subset,type:out]
58 {
59 Print["Calling For Backend on subset"]
60 {
61 [subset]For Backend
62 {
63 Print["Got output from for backend"]
64 out <- [[<String@Worker[[type]Append["Cond"]]]Do[ [[()]Append[current]]Append[~] ]]Index[0]
65 { Print["done _For Backend Subset with condition"] }
66 }{
67 Print["none output from for backend"]
68 out <- current
69 }
70 }
71 }
72
73 Empty?@Condition Set[set:not empty,empty]
74 {
75 [[set]Variables >>]First
76 {
77 not empty <- Yes
78 }{
79 ,empty <- [[set]Subsets >>]First Non-empty Set
80 {
81 not empty <- Yes
82 }
83 }
84 }
85
86 _First Non-empty Set[setlist,index:out,none]
87 {
88 current <- [setlist]Index[index]
89 [[current]Variables >>]First
90 {
91 out <- index
92 }{
93 ,trynext <- [[current]Subsets >>]First Non-empty Set
94 {
95 out <- index
96 }
97 }
98 Val[trynext]
99 {
100 ,none <- [setlist]Next[index]
101 {
102 out,none <- _First Non-empty Set[setlist, ~]
103 }
104 }
105 }
106
107 First Non-empty Set[setlist:index,none]
108 {
109 ,none <- [setlist]First
110 {
111 index,none <- _First Non-empty Set[setlist,~]
112 }
113 }
114
115 For Backend@Condition Set[set:out,none]
116 {
117 Print["For Backend"]
118 firstvar <- [[set]Variables >>]First
119 {
120 Print["At least one var"]
121 [[set]Variables >>]Next[~]
122 {
123 Print["at least two vars"]
124 vars <- _Fold[[set]Variables >>, ~, firstvar, ["_For Backend Var"]Set Input[3, [set]Condition Type >>]]
125 }{
126 Print["just one var"]
127 vars <- Val[firstvar]
128 }
129 out <- Fold[["_For Backend Subset"]Set Input[2, [set]Condition Type >>], vars, [set]Subsets >>]
130 { Print["done For Backend, with vars"] }
131 }{
132 Print["no vars"]
133 [[set]Subsets >>]First Non-empty Set
134 {
135 Print[["At least one non-empty subset: "]Append[~]]
136 firstsub <- [[[set]Subsets >>]Index[~]]For Backend
137 [[set]Subsets >>]Next[~]
138 {
139 Print["at least two subsets"]
140 out <- _Fold[[set]Subsets >>, ~, firstsub, ["_For Backend Subset"]Set Input[2, [set]Condition Type >>]]
141 { Print["done with subsets, but no vars"] }
142 }{
143 out <- Val[firstsub]
144 { Pretty Print[~, "1s:\t"] }
145 }
146 }{
147 Print["done with none"]
148 none <- Yes
149 }
150 }
151 }
3 152
4 Set@Range[range,index,val:out] 153 Set@Range[range,index,val:out]
5 { 154 {
6 out <- [[()]Concatenate[range] 155 out <- [[()]Concatenate[range]
7 ]Set[index,val] 156 ]Set[index,val]
50 Data 199 Data
51 Inputs 200 Inputs
52 Outputs 201 Outputs
53 Wires From 202 Wires From
54 Wires To 203 Wires To
55 } 204 Conditions
56 205 }
57 Wire To@NWorker Node[node,from,output,input:out] 206
58 { 207 Wire To@NWorker Node[node,from,output,pre input:out]
208 {
209 input <- [pre input]+[1]
59 out <- [node]Wires To <<[ 210 out <- [node]Wires To <<[
60 [[node]Wires To >>]Set[input, 211 [[node]Wires To >>]Set[input,
61 [[[node]Wires To >>]Index[input] 212 [[[node]Wires To >>]Index[input]
62 ]Append[Node Ref[from,output]] 213 ]Append[Node Ref[from,output]]
63 ] 214 ]
90 } 241 }
91 242
92 243
93 NWorker Node[type,data,inputs,outputs:out] 244 NWorker Node[type,data,inputs,outputs:out]
94 { 245 {
95 out <- [[[[[[Build["NWorker Node"] 246 out <- [[[[[[[Build["NWorker Node"]
96 ]Type <<[type] 247 ]Type <<[type]
97 ]Data <<[data] 248 ]Data <<[data]
98 ]Inputs <<[inputs] 249 ]Inputs <<[inputs]
99 ]Outputs <<[outputs] 250 ]Outputs <<[outputs]
100 ]Wires From <<[List of Lists[outputs]] 251 ]Wires From <<[List of Lists[outputs]]
101 ]Wires To <<[List of Lists[inputs]] 252 ]Wires To <<[List of Lists[[inputs]+[1]]]
253 ]Conditions <<[AndSet[]]
102 } 254 }
103 255
104 Blueprint NWorker 256 Blueprint NWorker
105 { 257 {
106 Convention 258 Convention
107 Nodes 259 Nodes
108 Inputs 260 Inputs
109 Outputs 261 Outputs
262 Uses
110 NodeResults 263 NodeResults
111 Free Temps 264 Free Temps
112 } 265 }
113 266
114 NWorker[convention:out] 267 NWorker[convention:out]
144 { out <- [~]Outputs <<[[[~]Outputs >>]Set[number,name]] } 297 { out <- [~]Outputs <<[[[~]Outputs >>]Set[number,name]] }
145 } 298 }
146 299
147 Add Wire@NWorker[worker,from,output,to,input:out] 300 Add Wire@NWorker[worker,from,output,to,input:out]
148 { 301 {
302 Print[[[[[[[[["Add Wire@NWorker["]Append[from]]Append[","]]Append[output]]Append[","]]Append[to]]Append[","]]Append[input]]Append["]"]]
303 {
149 fromw <- [[[worker]Nodes >>]Index[from]]Wire From[to,input,output] 304 fromw <- [[[worker]Nodes >>]Index[from]]Wire From[to,input,output]
305 { Print["fromw"] }
150 tow <- [[[worker]Nodes >>]Index[to]]Wire To[from,output,input] 306 tow <- [[[worker]Nodes >>]Index[to]]Wire To[from,output,input]
307 { Print["tow"] }
151 nodes <- [[[worker]Nodes >>]Set[from, fromw]]Set[to, tow] 308 nodes <- [[[worker]Nodes >>]Set[from, fromw]]Set[to, tow]
309 { Print["nodes"] }
152 out <- [worker]Nodes <<[nodes] 310 out <- [worker]Nodes <<[nodes]
311 { Print["Add Wire@NWorker done"] }
312 }
313 }
314
315 Uses@NWorker[worker,uses:out]
316 {
317 out <- [worker]Uses <<[uses]
153 } 318 }
154 319
155 _No Dependencies[list,node,index:out] 320 _No Dependencies[list,node,index:out]
156 { 321 {
157 [[node]Wires To>>]First 322 [[node]Wires To>>]Index[1]
158 { 323 {
159 out <- Val[list] 324 out <- Val[list]
160 }{ 325 }{
161 out <- [list]Append[index] 326 [[[node]Wires To>>]Index[0]]First
327 {
328 out <- Val[list]
329 }{
330 out <- [list]Append[index]
331 }
162 } 332 }
163 } 333 }
164 334
165 No Dependencies@NWorker[worker:out] 335 No Dependencies@NWorker[worker:out]
166 { 336 {
256 }{ 426 }{
257 If[[[node]Type >>] = ["input"]] 427 If[[[node]Type >>] = ["input"]]
258 { 428 {
259 input name <- [[worker]Inputs >>]Index[ [node]Data >> ] 429 input name <- [[worker]Inputs >>]Index[ [node]Data >> ]
260 out <- AddRef[input name] 430 out <- AddRef[input name]
431 }{
432 If[[[node]Type >>] = ["const"]]
433 {
434 out <- Constant[[[Type Of[[node]Data >>]]Append["_"]]Append[[node]Data >>]]
435 }
261 } 436 }
262 } 437 }
263 } 438 }
264 439
265 Collect Input@NWorker[worker,nodeinput:out] 440 Collect Input@NWorker[worker,nodeinput:out]
266 { 441 {
267 inputchoices <- Map[nodeinput, ["Format Input"]Set Input[0, worker]] 442 inputchoices <- Map[nodeinput, ["Format Input"]Set Input[0, worker]]
443
268 [inputchoices]First 444 [inputchoices]First
269 { 445 {
270 first <- [inputchoices]Index[~] 446 first <- [inputchoices]Index[~]
271 [inputchoices]Next[~] 447 [inputchoices]Next[~]
272 { 448 {
279 } 455 }
280 } 456 }
281 457
282 Collect Inputs@NWorker[worker,node:out] 458 Collect Inputs@NWorker[worker,node:out]
283 { 459 {
284 out <- Map[[node]Wires To>>, ["Collect Input"]Set Input[0, worker]] 460 out <- Map[Tail[[node]Wires To>>, 1], ["Collect Input"]Set Input[0, worker]]
461 }
462
463 Collect Input Condition@NWorker[worker,set,noderef:out]
464 {
465 node <- [[worker]Nodes >>]Index[ [noderef]Index >> ]
466 If[[[node]Outputs >>] > [1]]
467 {
468 out <- [set]Add Condition[ [["__result_"]Append[[noderef]Index >>]]Append[["_"]Append[[noderef]IO Num >>]] ]
469 }{
470 out <- [set]Add Condition[[node]Conditions >>]
471 }
472 }
473
474 Collect Condition@NWorker[worker,set,nodeinput:out]
475 {
476 out <- [set]Add Condition[Fold[["Collect Input Condition"]Set Input[0, worker], OrSet[], nodeinput]]
477 }
478
479 Collect Conditions@NWorker[worker,node:out]
480 {
481 Print["Collect Conditions"]
482 {
483 out <- Fold[["Collect Condition"]Set Input[0, worker], AndSet[], [node]Wires To>>]
484 { Print["done Collect Conditions"] }
485 }
285 } 486 }
286 487
287 Save Result[func,num,node index:out] 488 Save Result[func,num,node index:out]
288 { 489 {
490 Print["Save Result"]
289 out var <- [[["__result_"]Append[node index]]Append["_"]]Append[num] 491 out var <- [[["__result_"]Append[node index]]Append["_"]]Append[num]
290 out <- [[func]Allocate Var[out var, "Any Type"] 492 Print[out var]
493 /*out <- [[func]Allocate Var[out var, "Any Type"]
291 ]Move[Result[num], out var] 494 ]Move[Result[num], out var]
292 } 495 */
293 496 out <- [func]Move[Result[num], out var]
294 Compile Node@NWorker[worker,program,func,nodes,current:out] 497 }
295 { 498
296 Print[[node]Type >>] 499 Compile Call Node[node,program,func,inputs,node index:out]
500 {
501 [program]Method?[[[node]Data >>]Name >>]
502 {
503 Print["Method!"]
504 with call <- [func]Method Call[[[node]Data >>]Name >>, inputs]
505 { Print["Method Call done"] }
506 }{
507 Print["Function!"]
508 with call <- [func]Call[[[node]Data >>]Name >>, inputs]
509 }
510 out <- Fold[["Save Result"]Set Input[2, node index], with call, Range[0, [node]Outputs >>]]
511 }
512
513 Compile Node@NWorker[worker,program,func,nodes,current:out,out worker]
514 {
515 Print[["Compile Node: "]Append[[node]Type >>]]
297 node index <- [nodes]Index[current] 516 node index <- [nodes]Index[current]
298 node <- [[worker]Nodes >>]Index[node index] 517 node <- [[worker]Nodes >>]Index[node index]
518 conditions <- [worker]Collect Conditions[node]
299 If[[[node]Type >>] = ["call"]] 519 If[[[node]Type >>] = ["call"]]
300 { 520 {
521 Print[["Call: "]Append[[[node]Data >>]Name >>]]
522
301 inputs <- [worker]Collect Inputs[node] 523 inputs <- [worker]Collect Inputs[node]
302 [program]Method?[[[node]Data >>]Name >>] 524 [conditions]For Backend
303 { 525 {
304 with call <- [func]Method Call[[[node]Data >>]Name >>, inputs] 526 Print["Conditional execution, do if"]
305 }{ 527 stream <- [func]Instruction Stream
306 with call <- [func]Call[[[node]Data >>]Name >>, inputs] 528 nfunc <- [func]Do If[~, nstream]
307 } 529 }{
308 nfunc <- Fold[["Save Result"]Set Input[2, node index], with call, Range[0, [node]Outputs >>]] 530 Print["No conditions, full steam ahead"]
531 stream <- Val[func]
532 nfunc <- Val[nstream]
533 }
534 nstream <- Compile Call Node[node, program, stream, inputs, node index]
309 }{ 535 }{
310 If[[[node]Type >>] = ["output"]] 536 If[[[node]Type >>] = ["output"]]
311 { 537 {
312 inputs <- [worker]Collect Inputs[node] 538 inputs <- [worker]Collect Inputs[node]
313 nfunc <- [func]Move[[inputs]Index[0], [[worker]Outputs >>]Index[ [node]Data >> ] ] 539 [conditions]For Backend
314 }{ 540 {
315 nfunc <- Val[func] 541 stream <- [func]Instruction Stream
316 } 542 nfunc <- [func]Do If[~, nstream]
543 }{
544 stream <- Val[func]
545 nfunc <- Val[nstream]
546 }
547 nstream <- [stream]Move[[inputs]Index[0], [[worker]Outputs >>]Index[ [node]Data >> ] ]
548 }{
549 If[[[node]Type >>] = ["const"]]
550 {
551 //TODO: Handle list constants
552 nfunc <- [func]Register Constant[[[Type Of[[node]Data >>]]Append["_"]]Append[[node]Data >>], [node]Data >>]
553 }{
554 nfunc <- Val[func]
555 }
556
557 }
558 }
559 If[[[node]Outputs >>] = [0]]
560 {
561 nworker <- Val[worker]
562 }{
563 nworker <- [worker]Nodes <<[ [[worker]Nodes >>]Set[node index, [node]Conditions <<[conditions]] ]
317 } 564 }
318 [nodes]Next[current] 565 [nodes]Next[current]
319 { 566 {
320 out <- [worker]Compile Node[program,nfunc,nodes,~] 567 out,out worker <- [nworker]Compile Node[program,nfunc,nodes,~]
321 }{ 568 }{
322 out <- Val[nfunc] 569 out <- Val[nfunc]
323 } 570 out worker <- Val[nworker]
324 } 571 }
325 572 }
326 Compile Group@NWorker[worker,program,func,groups,current:out] 573
574 Compile Group@NWorker[worker,program,func,groups,current:out,out worker]
327 { 575 {
328 nodes <- [groups]Index[current] 576 nodes <- [groups]Index[current]
329 [nodes]First 577 [nodes]First
330 { 578 {
331 nfunc <- [worker]Compile Node[program,func,nodes,~] 579 nfunc,nworker <- [worker]Compile Node[program,func,nodes,~]
332 }{ 580 }{
333 nfunc <- Val[func] 581 nfunc <- Val[func]
582 nworker <- Val[worker]
334 } 583 }
335 [groups]Next[current] 584 [groups]Next[current]
336 { 585 {
337 out <- [worker]Compile Group[program,nfunc,groups,~] 586 out,out worker <- [nworker]Compile Group[program,nfunc,groups,~]
338 }{ 587 }{
339 out <- Val[nfunc] 588 out <- Val[nfunc]
340 } 589 out worker <- Val[nworker]
341 } 590 }
342 591 }
343 Release Var[func,type,name:out] 592
344 { 593 Release Var@NWorker[worker,func,name:out]
345 out <- [func]Release[name] 594 {
595 //_result_index_ionum
596 parts <- [name]Split["_"]
597 index <- <String@Whole Number[ [parts]Index[2] ]
598 io num <- <String@Whole Number[ [parts]Index[3] ]
599 node <- [[worker]Nodes >>]Index[index]
600 do if <- If[[[node]Outputs >>] > [1]] {}
601 {
602 ,do if <- [[node]Conditions >>]Empty?
603 {
604 out <- [func]Release[name]
605 }
606 }
607
608 Val[do if]
609 {
610 stream <- [[func]Instruction Stream]Release[name]
611 out <- [func]Do If[name, stream]
612 }
613 }
614
615 Result Var[vars,io num,index:out]
616 {
617 out <- [vars]Append[[[["__result_"]Append[index]]Append["_"]]Append[io num]]
618 }
619
620 Node Result Vars[vars,node,index:out]
621 {
622 If[[[node]Type >>] = ["call"]]
623 {
624 out <- Fold[["Result Var"]Set Input[2, index], vars, Range[0, [node]Outputs >>]]
625 }{
626 out <- vars
627 }
628 }
629
630 Result Vars@NWorker[worker:out]
631 {
632 out <- Fold["Node Result Vars", (), [worker]Nodes >>]
346 } 633 }
347 634
348 Compile Worker@NWorker[worker,program,name:out] 635 Compile Worker@NWorker[worker,program,name:out]
349 { 636 {
350 func <- Fold["Null", [program]Create Function[name,[worker]Inputs >>, [worker]Outputs >>, [worker]Convention >>], [worker]Outputs >>] 637 Print[["Compiling: "]Append[name]]
638 {
639 ifunc <- [program]Create Function[name,[worker]Inputs >>, [worker]Outputs >>, [worker]Convention >>]
640
641 res vars <- [worker]Result Vars
642 func <- Fold["Set Null", Fold["Set Null", Fold[["Allocate Var"]Set Input[2, "Any Type"], ifunc, res vars], res vars], [worker]Outputs >>]
643
351 groups <- [worker]Dependency Groups 644 groups <- [worker]Dependency Groups
352 [groups]First 645 [groups]First
353 { 646 {
354 final func <- [worker]Compile Group[program,func,groups, ~] 647 final func <- [worker]Compile Group[program,func,groups, ~]
355 }{ 648 }{
356 final func <- Val[func] 649 final func <- Val[func]
357 } 650 }
358 out <- [program]Store Function[Fold["Release Var", final func, [final func]Variables >>]] 651 out <- [program]Store Function[Fold[["Release Var"]Set Input[0, worker], final func, res vars]]
652 }
359 } 653 }
360 654
361 Test[:out] 655 Test[:out]
362 { 656 {
363 ref+ <- Worker Ref["+","cdecl",2,1] 657 ref+ <- Worker Ref["+","cdecl",2,1]
380 Test Graph to Backend[:out] 674 Test Graph to Backend[:out]
381 { 675 {
382 out <- [Test[]]Compile Worker[C Program[], "Test"] 676 out <- [Test[]]Compile Worker[C Program[], "Test"]
383 } 677 }
384 678
385 Main[] 679 Blueprint NProgram
386 { 680 {
387 Pretty Print[Test Graph to Backend[], ""] 681 Blueprints
388 } 682 Workers
683 Worker Refs
684 }
685
686 NProgram[:out]
687 {
688 out <- [[[Build["NProgram"]]Blueprints <<[New@Dictionary[]]]Workers <<[New@Dictionary[]]]Worker Refs <<[New@Dictionary[]]
689 }
690
691 Bind Worker@NProgram[prog,name,worker:out]
692 {
693 out <- [prog]Workers << [ [[prog]Workers >>]Set[name, worker] ]
694 }
695
696 Bind Blueprint@NProgram[prog,name,blueprint:out]
697 {
698 out <- [prog]Blueprints << [ [[prog]Blueprints >>]Set[name, blueprint] ]
699 }
700
701 _Compile Program[backend, worker, name:out]
702 {
703 out <- [worker]Compile Worker[backend, name]
704 }
705
706 Compile Program@NProgram[prog, backend:out]
707 {
708 out <- Fold["_Compile Program", backend, [prog]Workers >>]
709 }
710
711 Register Worker@NProgram[prog, name, convention, inputs, outputs: out]
712 {
713 Print[["Register Worker "]Append[name]]
714 out <- [prog]Worker Refs <<[ [[prog]Worker Refs >>]Set[name, Worker Ref[name, convention, inputs, outputs]]]
715 }
716
717 Register Builtins@NProgram[prog:out]
718 {
719 out <- [[[[[[[[[prog]Register Worker["+", "cdecl", 2, 1]
720 ]Register Worker["-", "cdecl", 2, 1]
721 ]Register Worker["*", "cdecl", 2, 1]
722 ]Register Worker["/", "cdecl", 2, 1]
723 ]Register Worker["Print", "cdecl", 1, 1]
724 ]Register Worker["Index", "cdecl", 2, 1]
725 ]Register Worker["If", "cdecl", 1, 2]
726 ]Register Worker["<", "cdecl", 2, 1]
727 ]Register Worker["<String@Whole Number", "cdecl", 1, 1]
728 }
729
730 Find Worker@NProgram[prog, name:out,notfound]
731 {
732 Print[ ["Find Worker@NProgram: "]Append[name] ]
733 out,notfound <- [[prog]Worker Refs >>]Index[name]
734 }