comparison cbackend.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 3498713c3dc9
children 640f541e9116
comparison
equal deleted inserted replaced
35:3498713c3dc9 36:495dddadd058
63 Next ID 63 Next ID
64 } 64 }
65 65
66 C Field Registry[:out] 66 C Field Registry[:out]
67 { 67 {
68 out <- [[Build["C Field Registry"]]Lookup <<[New@Dictionary[]]]Next ID<<[0] 68 out <- [[Build["C Field Registry"]]Lookup <<[New@Dictionary[]]]Next ID<<[1]
69 69
70 } 70 }
71 71
72 Register Field@C Field Registry[reg,field:out] 72 Register Field@C Field Registry[reg,field:out]
73 { 73 {
115 Register Methods@C Type[ctype,method reg:out] 115 Register Methods@C Type[ctype,method reg:out]
116 { 116 {
117 out <- Fold["Register Method", method reg, [ctype]Methods >>] 117 out <- Fold["Register Method", method reg, [ctype]Methods >>]
118 } 118 }
119 119
120 _Register Field C[reg,field:out]
121 {
122 name <- [field]Index[0]
123 out <- [reg]Register Field[name]
124 }
125
120 Register Fields@C Type[ctype,field reg:out] 126 Register Fields@C Type[ctype,field reg:out]
121 { 127 {
122 out <- Fold["Register Field", field reg, [ctype]Fields >>] 128 out <- Fold["_Register Field C", field reg, [ctype]Fields >>]
123 } 129 }
124 130
125 Rhope Type to C[typename,naked:out] 131 Rhope Type to C[typename:out]
126 { 132 {
127 If[[typename] = ["Any Type"]] 133 If[[typename] = ["Any Type"]]
128 { 134 {
129 out <- "struct object *" 135 out <- "struct object *"
130 }{ 136 }{
131 ctype <- ["t_"]Append[Escape Rhope Name[typename]] 137 out <- [["t_"]Append[Escape Rhope Name[typename]]]Append[" *"]
132 If[naked]
133 {
134 out <- Val[ctype]
135 }{
136 out <- [ctype]Append[" *"]
137 }
138 } 138 }
139 } 139 }
140 140
141 _Type Def C Type[text,field:out] 141 _Type Def C Type[text,field:out]
142 { 142 {
143 name <- [field]Index[0] 143 name <- [field]Index[0]
144 type <- ["\n\t"]Append[Rhope Type to C[[field]Index[1], No]] 144 type <- ["\n\t"]Append[Rhope Type to C[[field]Index[1]]]
145 out <- [[[[text]Append[type]]Append[" "]]Append[Escape Rhope Name[name]]]Append[";"] 145 out <- [[[[text]Append[type]]Append[" "]]Append[Escape Rhope Name[name]]]Append[";"]
146 } 146 }
147 147
148 Type Def@C Type[ctype:out] 148 Type Def@C Type[ctype:out]
149 { 149 {
153 _Type Init C[type name,method reg,text,method:out] 153 _Type Init C[type name,method reg,text,method:out]
154 { 154 {
155 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["));"] 155 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["));"]
156 } 156 }
157 157
158 _Type Init C Field[type name,field reg,text,field:out]
159 {
160 fname <- [field]Index[0]
161 out <- [[[[text]Append[[["\n\tadd_getter(bp, "]Append[ [field reg]Field ID[fname] ]]Append[ [[", MethodName("]Append[Escape Rhope Name[[fname]Append[" >>"]]]]Append[[","]Append[Escape Rhope Name[type name]]]]]]Append["));"]
162 ]Append[[["\n\tadd_setter(bp, "]Append[ [field reg]Field ID[fname] ]]Append[ [[", MethodName("]Append[Escape Rhope Name[[fname]Append[" <<"]]]]Append[[","]Append[Escape Rhope Name[type name]]]]]]Append["));"]
163 }
164
158 Type Init@C Type[ctype,id,method reg,field reg:out] 165 Type Init@C Type[ctype,id,method reg,field reg:out]
159 { 166 {
160 //TODO: Handle function pointers for build/copy/destroy funcs
161 start <- [["\tbp = register_type_byid(" 167 start <- [["\tbp = register_type_byid("
162 ]Append[id] 168 ]Append[id]
163 ]Append[ 169 ]Append[
164 [[", sizeof(" 170 [[", sizeof("
165 ]Append[ 171 ]Append[
166 ["t_"]Append[Escape Rhope Name[ [ctype]Name >> ]]] 172 ["t_"]Append[Escape Rhope Name[ [ctype]Name >> ]]]
167 ]Append[ 173 ]Append[
168 ["), "]Append[ 174 ["), (special_func)"]Append[
169 [ 175 [
170 [[[[Escape Rhope Name NU[[ctype]Init >>] 176 [[[[Escape Rhope Name NU[[ctype]Init >>]
171 ]Append[", "] 177 ]Append[", (special_func)"]
172 ]Append[Escape Rhope Name NU[[ctype]Copy >> ]] 178 ]Append[Escape Rhope Name NU[[ctype]Copy >> ]]
173 ]Append[", "] 179 ]Append[", (special_func)"]
174 ]Append[Escape Rhope Name NU[[ctype]Cleanup >>]] 180 ]Append[Escape Rhope Name NU[[ctype]Cleanup >>]]
175 ]Append[");"]]] ] 181 ]Append[");"]]] ]
176 out <- Fold[[["_Type Init C"]Set Input[0, [ctype]Name >>]]Set Input[1, method reg], start, [ctype]Methods >>] 182 out <- Fold[[["_Type Init C Field"]Set Input[0, [ctype]Name >>]]Set Input[1, field reg], Fold[[["_Type Init C"]Set Input[0, [ctype]Name >>]]Set Input[1, method reg], start, [ctype]Methods >>], [ctype]Fields >>]
177 } 183 }
178 184
179 Blueprint C Type Registry 185 Blueprint C Type Registry
180 { 186 {
181 Lookup 187 Lookup
207 ]Next ID <<[0] 213 ]Next ID <<[0]
208 } 214 }
209 215
210 _Type Defs C[text,def:out] 216 _Type Defs C[text,def:out]
211 { 217 {
212 out <- [[text]Append["\n\n"]]Append[[def]Type Def] 218 out <- [[text]Append[[def]Type Def]]Append["\n\n"]
213 } 219 }
214 220
215 Type Defs@C Type Registry[reg:out] 221 Type Defs@C Type Registry[reg:out]
216 { 222 {
217 out <- Fold["_Type Defs C", "", [reg]Definitions >>] 223 out <- Fold["_Type Defs C", "", [reg]Definitions >>]
218 } 224 }
219 225
220 _Type Inits C[reg,method reg,field reg,text,def,name:out] 226 _Type Inits C[reg,method reg,field reg,text,def,name:out]
221 { 227 {
222 out <- [[text]Append["\n\n"]]Append[ [def]Type Init[[reg]Type ID[name], method reg, field reg] ] 228 out <- [[text]Append[ [def]Type Init[[reg]Type ID[name], method reg, field reg] ]]Append["\n\n"]
223 } 229 }
224 230
225 Type Inits@C Type Registry[reg,method reg,field reg:out] 231 Type Inits@C Type Registry[reg,method reg,field reg:out]
226 { 232 {
227 out <- Fold[[[["_Type Inits C"]Set Input[0, reg]]Set Input[1, method reg]]Set Input[2, field reg], "", [reg]Definitions >>] 233 out <- Fold[[[["_Type Inits C"]Set Input[0, reg]]Set Input[1, method reg]]Set Input[2, field reg], "", [reg]Definitions >>]
253 Convention 259 Convention
254 Variables 260 Variables
255 Statements 261 Statements
256 Method Registry 262 Method Registry
257 Field Registry 263 Field Registry
264 Type Registry
258 Constants 265 Constants
259 Input Types 266 Input Types
260 Output Types 267 Output Types
261 } 268 }
262 269
265 out <- C Function With Registry[name,inputs,outputs,convention, C Method Registry[], C Field Registry[]] 272 out <- C Function With Registry[name,inputs,outputs,convention, C Method Registry[], C Field Registry[]]
266 } 273 }
267 274
268 C Function With Registry[name,inputs,outputs,convention,registry,field reg:out] 275 C Function With Registry[name,inputs,outputs,convention,registry,field reg:out]
269 { 276 {
270 out <- [[[[[[[[[[[Build["C Function"] 277 out <- [[[[[[[[[[[[Build["C Function"]
271 ]Name <<[name] 278 ]Name <<[name]
272 ]Inputs <<[inputs] 279 ]Inputs <<[inputs]
273 ]Outputs <<[outputs] 280 ]Outputs <<[outputs]
274 ]Convention <<[convention] 281 ]Convention <<[convention]
275 ]Variables <<[New@Dictionary[]] 282 ]Variables <<[New@Dictionary[]]
276 ]Statements <<[()] 283 ]Statements <<[()]
277 ]Method Registry <<[registry] 284 ]Method Registry <<[registry]
278 ]Field Registry <<[field reg] 285 ]Field Registry <<[field reg]
286 ]Type Registry <<[C Type Registry[]]
279 ]Constants <<[New@Dictionary[]] 287 ]Constants <<[New@Dictionary[]]
280 ]Input Types <<[ Fold[["Append"]Set Input[1, "Any Type"], (), inputs] ] 288 ]Input Types <<[ Fold[["Append"]Set Input[1, "Any Type"], (), inputs] ]
281 ]Output Types <<[ Fold[["Append"]Set Input[1, "Any Type"], (), outputs] ] 289 ]Output Types <<[ Fold[["Append"]Set Input[1, "Any Type"], (), outputs] ]
282 } 290 }
283 291
293 301
294 Register Constant@C Function[func,name,constant:out] 302 Register Constant@C Function[func,name,constant:out]
295 { 303 {
296 Print["Register Constant"] 304 Print["Register Constant"]
297 Print[name] 305 Print[name]
298 Print[constant]
299 out <- [func]Constants <<[ [[func]Constants >>]Set[name, constant] ] 306 out <- [func]Constants <<[ [[func]Constants >>]Set[name, constant] ]
300 { Print["Got register constant output"] } 307 { Print["Got register constant output"] }
301 } 308 }
302 309
303 Allocate Var@C Function[func,name,type:out] 310 Allocate Var@C Function[func,name,type:out]
349 source <- [psource]Make Op[func] 356 source <- [psource]Make Op[func]
350 dest <- [pdest]Make Op[func] 357 dest <- [pdest]Make Op[func]
351 out <- [func]Add Statement[[[dest]Append[" = "]]Append[source]] 358 out <- [func]Add Statement[[[dest]Append[" = "]]Append[source]]
352 } 359 }
353 360
354 AddRef@C Function[func,psource,pdest:out] 361 Do AddRef@C Function[func,psource,pdest:out]
355 { 362 {
356 source <- [psource]Make Op[func] 363 source <- [psource]Make Op[func]
357 dest <- [pdest]Make Op[func] 364 dest <- [pdest]Make Op[func]
358 out <- [func]Add Statement[[[[dest]Append[" = add_ref("]]Append[source]]Append[")"]] 365 out <- [func]Add Statement[[[[dest]Append[" = add_ref("]]Append[source]]Append[")"]]
359 } 366 }
382 out <- [["add_ref(_const_"]Append[Escape Rhope Name[const]]]Append[")"] 389 out <- [["add_ref(_const_"]Append[Escape Rhope Name[const]]]Append[")"]
383 } 390 }
384 391
385 Field Result@C Function[func,var,field:out] 392 Field Result@C Function[func,var,field:out]
386 { 393 {
387 out <- [[var]Append["->"]]Append[field] 394 as op <- [var]Make Op[func]
388 } 395 If[[[Type Of[var]] = ["String"]] And[ [[func]Convention >>] = ["rhope"]] ]
389 396 {
390 Get Field@C Function[func,var,field:out,result op] 397 [[func]Inputs >>]Find[var]
398 {
399 type <- [[func]Input Types >>]Index[~]
400 If[[type] = ["Any Type"]]
401 {
402 rvar <- Val[as op]
403 }{
404 rvar <- [[[["(("]Append[ Rhope Type to C[type] ]]Append[")("]]Append[as op]]Append["))"]
405 }
406 }{
407 rvar <- Val[as op]
408 }
409 }{
410 rvar <- Val[as op]
411 }
412 out <- [[rvar]Append["->"]]Append[Escape Rhope Name[field]]
413 }
414
415 Read Field@C Function[func,var,field:out,result op]
391 { 416 {
392 out <- func 417 out <- func
393 result op <- Field Ref[var,field] 418 result op <- Field Ref[var,field]
394 } 419 }
395 420
421 Write Field@C Function[func,var,field:out,result op]
422 {
423 out <- func
424 result op <- Field Ref[var,field]
425 }
426
396 Set Field Null@C Function[func,var,field:out] 427 Set Field Null@C Function[func,var,field:out]
397 { 428 {
398 out <- [func]Add Statement[ [[[var]Append["->"]]Append[field]]Append[" = NULL"] ] 429 out <- [func]Add Statement[ [[[var]Append["->"]]Append[field]]Append[" = NULL"] ]
430 }
431
432 Copy@C Function[func,pdest:out]
433 {
434 dest <- [pdest]Make Op[func]
435 out <- [func]Add Statement[ [dest]Append[[[" = copy_object("]Append[dest]]Append[")"]] ]
399 } 436 }
400 437
401 _Function Arg C[func,val,inputnum:out] 438 _Function Arg C[func,val,inputnum:out]
402 { 439 {
403 out <- [func]Add Statement[ 440 out <- [func]Add Statement[
522 } 559 }
523 } 560 }
524 } 561 }
525 _Output Defs C[string,varname,index,func:out] 562 _Output Defs C[string,varname,index,func:out]
526 { 563 {
527 out <- [[[string]Append[ ["\t"]Append[Rhope Type to C[[[func]Output Types >>]Index[index], No]] ]]Append[Escape Rhope Name[varname]]]Append[";\n"] 564 out <- [[[string]Append[ ["\t"]Append[Rhope Type to C[[[func]Output Types >>]Index[index]]] ]]Append[Escape Rhope Name[varname]]]Append[";\n"]
528 } 565 }
529 _Var Defs C[string,type,varname:out] 566 _Var Defs C[string,type,varname:out]
530 { 567 {
531 out <- [[[string]Append[ ["\t"]Append[Rhope Type to C[type, No]] ]]Append[Escape Rhope Name[varname]]]Append[";\n"] 568 out <- [[[string]Append[ ["\t"]Append[Rhope Type to C[type]] ]]Append[Escape Rhope Name[varname]]]Append[";\n"]
532 } 569 }
533 570
534 571
535 Definitions@C Function[func:out] 572 Definitions@C Function[func:out]
536 { 573 {
558 proto <- [[func]Naked Proto]Append[";\n"] 595 proto <- [[func]Naked Proto]Append[";\n"]
559 } 596 }
560 out <- [localtype]Append[proto] 597 out <- [localtype]Append[proto]
561 } 598 }
562 599
600 _Proto Input[list,input,index,types:out]
601 {
602 Print[["_Proto Input: "]Append[input]]
603 { Print[[types]Index[index]] }
604 out <- [list]Append[ [[Rhope Type to C[[types]Index[index]]]Append[" "]]Append[Escape Rhope Name[input]] ]
605 { Pretty Print[~, ""] }
606 }
607
563 Naked Proto@C Function[func:out] 608 Naked Proto@C Function[func:out]
564 { 609 {
610 Print[["Naked Proto: "]Append[ [func]Name >>] ]
565 [[func]Output Types >>]Index[0] 611 [[func]Output Types >>]Index[0]
566 { 612 {
567 outtype <- [Rhope Type to C[~, Yes]]Append[" "] 613 outtype <- [Rhope Type to C[~]]Append[" "]
568 }{ 614 }{
569 outtype <- "void " 615 outtype <- "void "
570 } 616 }
571 out <- [[[[outtype 617 out <- [[[[outtype
572 ]Append[ Escape Rhope Name NU[[func]Name >>]] 618 ]Append[ Escape Rhope Name NU[[func]Name >>]]
573 ]Append["("] 619 ]Append["("]
574 ]Append[ [[func]Input Types >>]Join[", "] ] 620 ]Append[ [Fold[["_Proto Input"]Set Input[3, [func]Input Types >>], (), [func]Inputs >>]]Join[", "] ]
575 ]Append[")"] 621 ]Append[")"]
622 { Print[~] }
623 }
624
625 Type Check@C Function[func,text,type,input num:out]
626 {
627 If[[type] = ["Any Type"]]
628 {
629 out <- text
630 }{
631 out <- [text]Append[ [["\tParam("]Append[input num]]Append[ [[", "]Append[ [[func]Type Registry >>]Type ID[type] ]]Append[")"] ] ]
632 }
576 } 633 }
577 634
578 Text@C Function[func:out] 635 Text@C Function[func:out]
579 { 636 {
580 If[ [[func]Convention >>] = ["rhope"] ] 637 If[ [[func]Convention >>] = ["rhope"] ]
581 { 638 {
582 cname <- Escape Rhope Name[[func]Name >>] 639 cname <- Escape Rhope Name[[func]Name >>]
583 If[ [[[func]Variables >>]Length] = [0] ] 640 If[ [ [[[func]Variables >>]Length]+[[[func]Outputs >>]Length] ] = [0] ]
584 { 641 {
585 out <- [[[[[["FuncNoLocals(" 642 out <- [[[[[["FuncNoLocals("
586 ]Append[cname] 643 ]Append[cname]
587 ]Append[",\n\tNumParams "] 644 ]Append[",\n\tNumParams "]
588 ]Append[ [[func]Inputs >>]Length ] 645 ]Append[ [[func]Inputs >>]Length ]
661 }{ 718 }{
662 out <- [text]Append[[def]Append["\n\n"]] 719 out <- [text]Append[[def]Append["\n\n"]]
663 } 720 }
664 } 721 }
665 722
666 _Text C Program[text,func:out] 723 _Text C Program[text,func,type reg:out]
667 { 724 {
668 out <- [text]Append[[[func]Text]Append["\n\n"]] 725 out <- [text]Append[[[ [func]Type Registry <<[type reg] ]Text]Append["\n\n"]]
669 } 726 }
670 727
671 Combine Consts[consts,func:out] 728 Combine Consts[consts,func:out]
672 { 729 {
673 out <- Combine[[func]Constants >>, consts] 730 out <- Combine[[func]Constants >>, consts]
676 _Consts C Program[text,value,name:out] 733 _Consts C Program[text,value,name:out]
677 { 734 {
678 out <- [text]Append[ [["object * _const_"]Append[Escape Rhope Name[name]]]Append[";\n"] ] 735 out <- [text]Append[ [["object * _const_"]Append[Escape Rhope Name[name]]]Append[";\n"] ]
679 } 736 }
680 737
681 _Set Consts C Program[text,value,name:out] 738 _Set Consts C Program[text,value,name,type reg:out]
682 { 739 {
683 //TODO: Support more constant types 740 //TODO: Support more constant types
684 out <- [text]Append[[[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = make_Int32("]]Append[value]]Append[");\n"]] 741 valtype <- Type Of[value]
742 [("Int32","Whole Number")]Find[valtype]
743 {
744 out <- [text]Append[[[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = make_Int32("]]Append[value]]Append[");\n"]]
745 }{
746 If[[valtype] = ["Type Literal"]]
747 {
748 //TODO: Support parametric types
749 typeid <- [type reg]Type ID[[value]Name >>]
750 out <- [text]Append[[[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = make_Blueprint("]]Append[typeid]]Append[");\n"]]
751 }
752 }
685 } 753 }
686 754
687 Text@C Program[program:out] 755 Text@C Program[program:out]
688 { 756 {
757 Print["Text@C Program"]
689 constants <- Fold["Combine Consts", New@Dictionary[], [program]Functions >>] 758 constants <- Fold["Combine Consts", New@Dictionary[], [program]Functions >>]
690 headers <- "#include <stdio.h> 759 headers <- "#include <stdio.h>
691 #include \"builtin.h\" 760 #include \"builtin.h\"
692 #include \"object.h\" 761 #include \"object.h\"
693 #include \"context.h\" 762 #include \"context.h\"
694 #include \"func.h\" 763 #include \"func.h\"
695 #include \"integer.h\"\n\n" 764 #include \"integer.h\"
765 #include \"blueprint.h\"\n\n"
696 out <- [[[[[[headers 766 out <- [[[[[[headers
697 ]Append[[[program]Type Registry >>]Type Defs] 767 ]Append[[[program]Type Registry >>]Type Defs]
698 ]Append[Fold["_Text C Program", 768 ]Append[Fold[["_Text C Program"]Set Input[2, [program]Type Registry >>],
699 Fold["_Consts C Program", 769 Fold["_Consts C Program",
700 Fold["_Defs C Program", "", [program]Functions >>], 770 Fold["_Defs C Program", "", [program]Functions >>],
701 constants 771 constants
702 ], [program]Functions >>]] 772 ], [program]Functions >>]]
703 ]Append["int main(int argc, char **argv) 773 ]Append["int main(int argc, char **argv)
704 { 774 {
705 returntype ret; 775 returntype ret;
706 calldata *cdata; 776 calldata *cdata;
707 context * ct; 777 context * ct;
778 blueprint * bp;
708 register_builtin_types();\n\n"] 779 register_builtin_types();\n\n"]
709 ]Append[ [[program]Type Registry >>]Type Inits[[program]Method Registry >>, [program]Field Registry >>] ] 780 ]Append[ [[program]Type Registry >>]Type Inits[[program]Method Registry >>, [program]Field Registry >>] ]
710 ]Append[Fold["_Set Consts C Program", "", constants]] 781 ]Append[Fold[["_Set Consts C Program"]Set Input[3, [program]Type Registry >>], "", constants]]
711 ]Append[" 782 ]Append["
712 ct = new_context(); 783 ct = new_context();
713 cdata = alloc_cdata(ct, 0); 784 cdata = alloc_cdata(ct, 0);
714 cdata->num_params = 0; 785 cdata->num_params = 0;
715 cdata->resume = 0; 786 cdata->resume = 0;