comparison nworker.rhope @ 36:495dddadd058

User defined types work in the compiler now
author Mike Pavone <pavone@retrodev.com>
date Sat, 03 Oct 2009 03:18:15 -0400
parents df038cef648b
children 640f541e9116
comparison
equal deleted inserted replaced
35:3498713c3dc9 36:495dddadd058
295 { 295 {
296 ,node index <- [worker]Add Node["output",number,1,0] 296 ,node index <- [worker]Add Node["output",number,1,0]
297 { out <- [~]Outputs <<[[[~]Outputs >>]Set[number,name]] } 297 { out <- [~]Outputs <<[[[~]Outputs >>]Set[number,name]] }
298 } 298 }
299 299
300 Add Object Get@NWorker[worker,fieldname:out,node index]
301 {
302 out, node index <- [worker]Add Node["getfield",fieldname,1,1]
303 }
304
305 Add Object Set@NWorker[worker,fieldname:out,node index]
306 {
307 out, node index <- [worker]Add Node["setfield",fieldname,2,1]
308 }
309
300 Add Wire@NWorker[worker,from,output,to,input:out] 310 Add Wire@NWorker[worker,from,output,to,input:out]
301 { 311 {
302 Print[[[[[[[[["Add Wire@NWorker["]Append[from]]Append[","]]Append[output]]Append[","]]Append[to]]Append[","]]Append[input]]Append["]"]] 312 Print[[[[[[[[["Add Wire@NWorker["]Append[from]]Append[","]]Append[output]]Append[","]]Append[to]]Append[","]]Append[input]]Append["]"]]
303 { 313 {
304 fromw <- [[[worker]Nodes >>]Index[from]]Wire From[to,input,output] 314 fromw <- [[[worker]Nodes >>]Index[from]]Wire From[to,input,output]
418 } 428 }
419 429
420 Format Input@NWorker[worker,noderef:out] 430 Format Input@NWorker[worker,noderef:out]
421 { 431 {
422 node <- [[worker]Nodes >>]Index[[noderef]Index >>] 432 node <- [[worker]Nodes >>]Index[[noderef]Index >>]
423 If[[[node]Type >>] = ["call"]] 433 [("call","getfield","setfield")]Find[[node]Type >>]
424 { 434 {
425 out <- AddRef[ [[["__result_"]Append[[noderef]Index >>]]Append["_"]]Append[[noderef]IO Num >>] ] 435 out <- AddRef[ [[["__result_"]Append[[noderef]Index >>]]Append["_"]]Append[[noderef]IO Num >>] ]
426 }{ 436 }{
427 If[[[node]Type >>] = ["input"]] 437 If[[[node]Type >>] = ["input"]]
428 { 438 {
429 input name <- [[worker]Inputs >>]Index[ [node]Data >> ] 439 input name <- [[worker]Inputs >>]Index[ [node]Data >> ]
430 out <- AddRef[input name] 440 out <- AddRef[input name]
431 }{ 441 }{
432 If[[[node]Type >>] = ["const"]] 442 If[[[node]Type >>] = ["const"]]
433 { 443 {
434 out <- Constant[[[Type Of[[node]Data >>]]Append["_"]]Append[[node]Data >>]] 444 If[[Type Of[[node]Data >>]] = ["Type Literal"]]
445 {
446 //TODO: Support parametric types
447 datstring <- [[node]Data >>]Name >>
448 }{
449 datstring <- [node]Data >>
450 }
451 out <- Constant[[[Type Of[[node]Data >>]]Append["_"]]Append[datstring]]
452
435 } 453 }
436 } 454 }
437 } 455 }
438 } 456 }
439 457
496 out <- [func]Move[Result[num], out var] 514 out <- [func]Move[Result[num], out var]
497 } 515 }
498 516
499 Compile Call Node[node,program,func,inputs,node index:out] 517 Compile Call Node[node,program,func,inputs,node index:out]
500 { 518 {
501 [program]Method?[[[node]Data >>]Name >>] 519 If[[[node]Type >>] = ["getfield"]]
502 { 520 {
503 Print["Method!"] 521 with call <- [func]Get Field Call[[node]Data >>, [inputs]Index[0]]
504 with call <- [func]Method Call[[[node]Data >>]Name >>, inputs] 522 }{
505 { Print["Method Call done"] } 523 If[[[node]Type >>] = ["setfield"]]
506 }{ 524 {
507 Print["Function!"] 525 with call <- [func]Set Field Call[[node]Data >>, [inputs]Index[0], [inputs]Index[1]]
508 with call <- [func]Call[[[node]Data >>]Name >>, inputs] 526 }{
527 Print[["Call: "]Append[[[node]Data >>]Name >>]]
528 {
529 [program]Method?[[[node]Data >>]Name >>]
530 {
531 Print["Method!"]
532 with call <- [func]Method Call[[[node]Data >>]Name >>, inputs]
533 { Print["Method Call done"] }
534 }{
535 Print["Function!"]
536 with call <- [func]Call[[[node]Data >>]Name >>, inputs]
537 }
538 }
539 }
509 } 540 }
510 out <- Fold[["Save Result"]Set Input[2, node index], with call, Range[0, [node]Outputs >>]] 541 out <- Fold[["Save Result"]Set Input[2, node index], with call, Range[0, [node]Outputs >>]]
511 } 542 }
512 543
513 Compile Node@NWorker[worker,program,func,nodes,current:out,out worker] 544 Compile Node@NWorker[worker,program,func,nodes,current:out,out worker]
514 { 545 {
515 Print[["Compile Node: "]Append[[node]Type >>]] 546 Print[["Compile Node: "]Append[[node]Type >>]]
516 node index <- [nodes]Index[current] 547 node index <- [nodes]Index[current]
517 node <- [[worker]Nodes >>]Index[node index] 548 node <- [[worker]Nodes >>]Index[node index]
518 conditions <- [worker]Collect Conditions[node] 549 conditions <- [worker]Collect Conditions[node]
519 If[[[node]Type >>] = ["call"]] 550 [("call","getfield","setfield")]Find[[node]Type >>]
520 { 551 {
521 Print[["Call: "]Append[[[node]Data >>]Name >>]]
522
523 inputs <- [worker]Collect Inputs[node] 552 inputs <- [worker]Collect Inputs[node]
524 [conditions]For Backend 553 [conditions]For Backend
525 { 554 {
526 Print["Conditional execution, do if"] 555 Print["Conditional execution, do if"]
527 stream <- [func]Instruction Stream 556 stream <- [func]Instruction Stream
547 nstream <- [stream]Move[[inputs]Index[0], [[worker]Outputs >>]Index[ [node]Data >> ] ] 576 nstream <- [stream]Move[[inputs]Index[0], [[worker]Outputs >>]Index[ [node]Data >> ] ]
548 }{ 577 }{
549 If[[[node]Type >>] = ["const"]] 578 If[[[node]Type >>] = ["const"]]
550 { 579 {
551 //TODO: Handle list constants 580 //TODO: Handle list constants
552 nfunc <- [func]Register Constant[[[Type Of[[node]Data >>]]Append["_"]]Append[[node]Data >>], [node]Data >>] 581 If[[Type Of[[node]Data >>]] = ["Type Literal"]]
582 {
583 //TODO: Support parametric types
584 datstring <- [[node]Data >>]Name >>
585 }{
586 datstring <- [node]Data >>
587 }
588 nfunc <- [func]Register Constant[[[Type Of[[node]Data >>]]Append["_"]]Append[datstring], [node]Data >>]
553 }{ 589 }{
554 nfunc <- Val[func] 590 nfunc <- Val[func]
555 } 591 }
556 592
557 } 593 }
617 out <- [vars]Append[[[["__result_"]Append[index]]Append["_"]]Append[io num]] 653 out <- [vars]Append[[[["__result_"]Append[index]]Append["_"]]Append[io num]]
618 } 654 }
619 655
620 Node Result Vars[vars,node,index:out] 656 Node Result Vars[vars,node,index:out]
621 { 657 {
622 If[[[node]Type >>] = ["call"]] 658 [("call","getfield","setfield")]Find[[node]Type >>]
623 { 659 {
624 out <- Fold[["Result Var"]Set Input[2, index], vars, Range[0, [node]Outputs >>]] 660 out <- Fold[["Result Var"]Set Input[2, index], vars, Range[0, [node]Outputs >>]]
625 }{ 661 }{
626 out <- vars 662 out <- vars
627 } 663 }
674 Test Graph to Backend[:out] 710 Test Graph to Backend[:out]
675 { 711 {
676 out <- [Test[]]Compile Worker[C Program[], "Test"] 712 out <- [Test[]]Compile Worker[C Program[], "Test"]
677 } 713 }
678 714
715 Blueprint NBlueprint
716 {
717 Fields
718 Methods
719 }
720
721 NBlueprint[:out]
722 {
723 out <- [[Build["NBlueprint"]]Fields <<[()]]Methods <<[New@Dictionary[]]
724 }
725
726 Add Field@NBlueprint[bp,name,type:out]
727 {
728 out <- [bp]Fields <<[ [[bp]Fields >>]Append[ [[()]Append[name]]Append[type] ] ]
729 }
730
731 Add Method@NBlueprint[bp,name:out]
732 {
733 out <- [bp]Methods <<[ [[bp]Methods >>]Set[name, Yes] ]
734 }
735
736 _Compile Blueprint Fields[type,field:out]
737 {
738 name <- [field]Index[0]
739 ftype <- [field]Index[1]
740 out <- [type]Add Field[name,ftype]
741 }
742
743 _Compile Blueprint Methods[type,junk,name:out]
744 {
745 out <- [type]Add Method[name]
746 }
747
748 Make Init[func,field:out]
749 {
750 name <- [field]Index[0]
751 out <- [func]Set Field Null["obj", name]
752 }
753
754 Make Copy[func,field:out]
755 {
756 name <- [field]Index[0]
757 got <- [func]Read Field["obj", name] {}
758 {
759 stream <- [[got]Instruction Stream
760 ]AddRef No Dest[~]
761 out <- [got]Do If[~, stream]
762 }
763 }
764
765 Make Cleanup[func,field:out]
766 {
767 name <- [field]Index[0]
768 got <- [func]Read Field["obj", name] {}
769 {
770 stream <- [[got]Instruction Stream
771 ]Release[~]
772 out <- [got]Do If[~, stream]
773 }
774 }
775
776 Make Special@NBlueprint[bp,backend,func name,bp name,pop worker:out]
777 {
778 func <- [[backend]Create Function[func name,("obj"),(),"cdecl"]
779 ]Set Input Type[0, bp name]
780 out <- [backend]Store Function[Fold[pop worker, func, [bp]Fields >>]]
781 }
782
783 Getters Setters[backend,field,type name:out]
784 {
785 //TODO: Throw an exception or something if we read a field that is empty
786 Print[["Getters Setters: "]Append[name]]
787 name <- [field]Index[0]
788 type <- [field]Index[1]
789 ,getref <- [[[[backend]Create Function[ [[[name]Append[" >>"]]Append["@"]]Append[type name], ("obj"), ("out"), "rhope"]
790 ]Set Input Type[0, type name]
791 ]Set Output Type[0, type]
792 ]Read Field["obj", name]
793 { getter <- [~]Do AddRef[getref, "out"]
794 { Print["Got getter"] } }
795
796 ,origref <- [[[[[[backend]Create Function[ [[[name]Append[" <<"]]Append["@"]]Append[type name], ("obj","newval"), ("out"), "rhope"]
797 ]Set Input Type[0, type name]
798 ]Set Input Type[1, type]
799 ]Set Output Type[0, type name]
800 ]Copy["obj"]
801 ]Read Field["obj", name]
802 {
803 stream <- [[~]Instruction Stream
804 ]Release[origref]
805 ,setref <- [[~]Do If[origref, stream]
806 ]Write Field["obj", name]
807 {
808 setter <- [[~]Move["newval", setref]
809 ]Move["obj", "out"]
810 { Print["got setter"] }
811 }
812 }
813
814 out <- [[backend]Store Function[getter]]Store Function[setter]
815
816 }
817
818 Compile Blueprint@NBlueprint[bp,backend,name:out]
819 {
820
821 //Rhope identifiers can't start with spaces, so we can use identifiers that start with spaces for special functions
822 init name <- [" init "]Append[name]
823 copy name <- [" copy "]Append[name]
824 cleanup name <- [" cleanup "]Append[name]
825 type <- [[[Fold["_Compile Blueprint Methods", Fold["_Compile Blueprint Fields", [backend]Create Type[name], [bp]Fields >>], [bp]Methods >>]
826 ]Init <<[init name]
827 ]Copy <<[copy name]
828 ]Cleanup <<[cleanup name]
829
830 got specials <- [bp]Make Special[
831 [bp]Make Special[
832 [bp]Make Special[[backend]Register Type[type], init name, name, "Make Init"],
833 copy name, name, "Make Copy"],
834 cleanup name, name, "Make Cleanup"]
835 out <- Fold[["Getters Setters"]Set Input[2, name], got specials, [bp]Fields >>]
836 }
837
679 Blueprint NProgram 838 Blueprint NProgram
680 { 839 {
681 Blueprints 840 Blueprints
682 Workers 841 Workers
683 Worker Refs 842 Worker Refs
687 { 846 {
688 out <- [[[Build["NProgram"]]Blueprints <<[New@Dictionary[]]]Workers <<[New@Dictionary[]]]Worker Refs <<[New@Dictionary[]] 847 out <- [[[Build["NProgram"]]Blueprints <<[New@Dictionary[]]]Workers <<[New@Dictionary[]]]Worker Refs <<[New@Dictionary[]]
689 } 848 }
690 849
691 Bind Worker@NProgram[prog,name,worker:out] 850 Bind Worker@NProgram[prog,name,worker:out]
692 { 851 {
693 out <- [prog]Workers << [ [[prog]Workers >>]Set[name, worker] ] 852 after bind <- [prog]Workers << [ [[prog]Workers >>]Set[name, worker] ]
853 parts <- [name]Split["@"]
854 [parts]Index[1]
855 {
856 out <- [after bind]Blueprints <<[ [[after bind]Blueprints >>]Set[~, [[[after bind]Blueprints >>]Index[~]]Add Method[[parts]Index[0]] ] ]
857 }{
858 out <- Val[after bind]
859 }
694 } 860 }
695 861
696 Bind Blueprint@NProgram[prog,name,blueprint:out] 862 Bind Blueprint@NProgram[prog,name,blueprint:out]
697 { 863 {
864 Print[["Bind blueprint: "]Append[name]]
698 out <- [prog]Blueprints << [ [[prog]Blueprints >>]Set[name, blueprint] ] 865 out <- [prog]Blueprints << [ [[prog]Blueprints >>]Set[name, blueprint] ]
699 } 866 }
700 867
868 _Compile Program BP[backend, blueprint, name:out]
869 {
870 out <- [blueprint]Compile Blueprint[backend, name]
871 }
872
701 _Compile Program[backend, worker, name:out] 873 _Compile Program[backend, worker, name:out]
702 { 874 {
703 out <- [worker]Compile Worker[backend, name] 875 out <- [worker]Compile Worker[backend, name]
704 } 876 }
705 877
706 Compile Program@NProgram[prog, backend:out] 878 Compile Program@NProgram[prog, backend:out]
707 { 879 {
708 out <- Fold["_Compile Program", backend, [prog]Workers >>] 880 out <- Fold["_Compile Program", Fold["_Compile Program BP", backend, [prog]Blueprints >>], [prog]Workers >>]
709 } 881 }
710 882
711 Register Worker@NProgram[prog, name, convention, inputs, outputs: out] 883 Register Worker@NProgram[prog, name, convention, inputs, outputs: out]
712 { 884 {
713 Print[["Register Worker "]Append[name]] 885 Print[["Register Worker "]Append[name]]
714 out <- [prog]Worker Refs <<[ [[prog]Worker Refs >>]Set[name, Worker Ref[name, convention, inputs, outputs]]] 886 after reg <- [prog]Worker Refs <<[ [[prog]Worker Refs >>]Set[name, Worker Ref[name, convention, inputs, outputs]]]
887 parts <- [name]Split["@"]
888 [parts]Index[1]
889 {
890 out <- [prog]Register Worker[[parts]Index[0], convention, inputs, outputs]
891 }{
892 out <- Val[after reg]
893 }
715 } 894 }
716 895
717 Register Builtins@NProgram[prog:out] 896 Register Builtins@NProgram[prog:out]
718 { 897 {
719 out <- [[[[[[[[[prog]Register Worker["+", "rhope", 2, 1] 898 out <- [[[[[[[[[prog]Register Worker["+@Int32", "rhope", 2, 1]
720 ]Register Worker["-", "rhope", 2, 1] 899 ]Register Worker["-@Int32", "rhope", 2, 1]
721 ]Register Worker["*", "rhope", 2, 1] 900 ]Register Worker["*@Int32", "rhope", 2, 1]
722 ]Register Worker["/", "rhope", 2, 1] 901 ]Register Worker["/@Int32", "rhope", 2, 1]
723 ]Register Worker["Print", "rhope", 1, 1] 902 ]Register Worker["Print", "rhope", 1, 1]
724 ]Register Worker["Index", "rhope", 2, 1] 903 ]Register Worker["If@Yes No", "rhope", 1, 2]
725 ]Register Worker["If", "rhope", 1, 2] 904 ]Register Worker["<@Int32", "rhope", 2, 1]
726 ]Register Worker["<", "rhope", 2, 1] 905 ]Register Worker[">@Int32", "rhope", 1, 1]
727 ]Register Worker[">", "rhope", 1, 1] 906 ]Register Worker["Build", "rhope", 1, 1]
728 } 907 }
729 908
730 Find Worker@NProgram[prog, name:out,notfound] 909 Find Worker@NProgram[prog, name:out,notfound]
731 { 910 {
732 Print[ ["Find Worker@NProgram: "]Append[name] ] 911 Print[ ["Find Worker@NProgram: "]Append[name] ]
733 out,notfound <- [[prog]Worker Refs >>]Index[name] 912 out,notfound <- [[prog]Worker Refs >>]Index[name]
734 } 913 }
914