comparison cbackend.rhope @ 34:df038cef648b

More work on supporting user defined types in the C backend
author Mike Pavone <pavone@retrodev.com>
date Wed, 30 Sep 2009 01:25:03 -0400
parents 3b47a8538df2
children 3498713c3dc9
comparison
equal deleted inserted replaced
33:3b47a8538df2 34:df038cef648b
55 Method ID@C Method Registry[reg,method:out,notfound] 55 Method ID@C Method Registry[reg,method:out,notfound]
56 { 56 {
57 out,notfound <- [[reg]Lookup >>]Index[method] 57 out,notfound <- [[reg]Lookup >>]Index[method]
58 } 58 }
59 59
60 Blueprint C Field Registry
61 {
62 Lookup
63 Next ID
64 }
65
66 C Field Registry[:out]
67 {
68 out <- [[Build["C Field Registry"]]Lookup <<[New@Dictionary[]]]Next ID<<[0]
69
70 }
71
72 Register Field@C Field Registry[reg,field:out]
73 {
74 [[reg]Lookup >>]Index[field]
75 {
76 out <- reg
77 }{
78 field ID <- [reg]Next ID>>
79 new lookup <- [[reg]Lookup >>]Set[field, field ID]
80 out <- [[reg]Lookup <<[new lookup]]Next ID <<[[field ID]+[1]]
81 }
82 }
83
84 Field ID@C Field Registry[reg,field:out,notfound]
85 {
86 out,notfound <- [[reg]Lookup >>]Index[field]
87 }
88
60 Blueprint C Type 89 Blueprint C Type
61 { 90 {
62 Name 91 Name
63 Fields 92 Fields
64 Methods 93 Methods
77 Add Method@C Type[ctype,name:out] 106 Add Method@C Type[ctype,name:out]
78 { 107 {
79 out <- [ctype]Methods <<[ [[ctype]Methods >>]Append[name] ] 108 out <- [ctype]Methods <<[ [[ctype]Methods >>]Append[name] ]
80 } 109 }
81 110
111 Register Methods@C Type[ctype,method reg:out]
112 {
113 out <- Fold["Register Method", method reg, [ctype]Methods >>]
114 }
115
116 Register Fields@C Type[ctype,field reg:out]
117 {
118 out <- Fold["Register Field", field reg, [ctype]Fields >>]
119 }
120
121 Rhope Type to C[typename,naked:out]
122 {
123 If[[typename] = ["Any Type"]]
124 {
125 ctype <- "struct object"
126 }{
127 ctype <- ["t_"]Append[Escape Rhope Name[typename]]
128 }
129 If[naked]
130 {
131 out <- Val[ctype]
132 }{
133 out <- [ctype]Append[" * "]
134 }
135 }
136
82 _Type Def C Type[text,field:out] 137 _Type Def C Type[text,field:out]
83 { 138 {
84 name <- [field]Index[0] 139 name <- [field]Index[0]
85 rawtype <- [field]Index[1] 140 type <- ["\n\t"]Append[Rhope Type to C[[field]Index[1], No]]
86 If[[rawtype] = ["Any Type"]]
87 {
88 type <- "\n\tobject * "
89 }{
90 type <- [["\n\tt_"]Append[Escape Rhope Name[rawtype]]]Append[" * "]
91 }
92 out <- [[[text]Append[type]]Append[Escape Rhope Name[name]]]Append[";"] 141 out <- [[[text]Append[type]]Append[Escape Rhope Name[name]]]Append[";"]
93 } 142 }
94 143
95 Type Def@C Type[ctype:out] 144 Type Def@C Type[ctype:out]
96 { 145 {
138 ]Set["Wrong Type Exception", "TYPE_WRONGTYPEEXCEPTION"]] 187 ]Set["Wrong Type Exception", "TYPE_WRONGTYPEEXCEPTION"]]
139 ]Definitions << [New@Dictionary[]] 188 ]Definitions << [New@Dictionary[]]
140 ]Next ID <<[0] 189 ]Next ID <<[0]
141 } 190 }
142 191
143 Register Type@C Type Registry[reg,name,def:out] 192 _Type Defs C[text,def:out]
144 { 193 {
194 out <- [[text]Append["\n\n"]]Append[[def]Type Def]
195 }
196
197 Type Defs@C Type Registry[reg:out]
198 {
199 out <- Fold["_Type Defs C", "", [reg]Definitions >>]
200 }
201
202 _Type Inits C[reg,method reg,field reg,text,def,name:out]
203 {
204 out <- [[text]Append["\n\n"]]Append[ [def]Type Init[[reg]Type ID[name], method reg, field reg] ]
205 }
206
207 Type Inits@C Type Registry[reg,method reg,field reg:out]
208 {
209 out <- Fold[[[["_Type Inits C"]Set Input[0, reg]]Set Input[1, method reg]]Set Input[2, field reg], "", [reg]Definitions >>]
210 }
211
212 Register Type@C Type Registry[reg,def:out]
213 {
214 name <- [def]Name >>
145 [[reg]Lookup >>]Index[name] 215 [[reg]Lookup >>]Index[name]
146 { 216 {
147 out <- reg 217 out <- reg
148 }{ 218 }{
149 out <- [[[reg]Lookup <<[ [[reg]Lookup >>]Set[name, ["TYPE_FIRST_USER+"]Append[[reg]Next ID >>]] ] 219 out <- [[[reg]Lookup <<[ [[reg]Lookup >>]Set[name, ["TYPE_FIRST_USER+"]Append[[reg]Next ID >>]] ]
416 } 486 }
417 487
418 Text@C Function[func:out] 488 Text@C Function[func:out]
419 { 489 {
420 cname <- Escape Rhope Name[[func]Name >>] 490 cname <- Escape Rhope Name[[func]Name >>]
421 If[ [[[func]Variables >>]Length] = [0] ] 491 If[ [[func]Convention >>] = ["rhope"] ]
422 { 492 {
423 out <- [[[[[["FuncNoLocals(" 493 If[ [[[func]Variables >>]Length] = [0] ]
424 ]Append[cname] 494 {
425 ]Append[",\n\tNumParams "] 495 out <- [[[[[["FuncNoLocals("
426 ]Append[ [[func]Inputs >>]Length ] 496 ]Append[cname]
427 ]Append[",\n\tCallSpace 32)\n\n"]//TODO: Fill in with calculated callspace value 497 ]Append[",\n\tNumParams "]
428 ]Append[ [[func]Statements >>]Join[""] ] 498 ]Append[ [[func]Inputs >>]Length ]
429 ]Append["EndFunc"] 499 ]Append[",\n\tCallSpace 32)\n\n"]//TODO: Fill in with calculated callspace value
430 }{ 500 ]Append[ [[func]Statements >>]Join[""] ]
431 out <- [[[[[[[[["Func(" 501 ]Append["EndFunc"]
432 ]Append[cname] 502 }{
433 ]Append[",\n\tNumParams "] 503 out <- [[[[[[[[["Func("
434 ]Append[ [[func]Inputs >>]Length ] 504 ]Append[cname]
435 ]Append[",\n\tCallSpace 32,\n\t"]//TODO: Fill in with calculated callspace value 505 ]Append[",\n\tNumParams "]
436 ]Append[["l_"]Append[cname]] 506 ]Append[ [[func]Inputs >>]Length ]
437 ]Append[")\n\n"] 507 ]Append[",\n\tCallSpace 32,\n\t"]//TODO: Fill in with calculated callspace value
438 ]Append[ [[func]Statements >>]Join[""] ] 508 ]Append[["l_"]Append[cname]]
439 ]Append[[func]Set Outputs] 509 ]Append[")\n\n"]
440 ]Append["EndFunc"] 510 ]Append[ [[func]Statements >>]Join[""] ]
511 ]Append[[func]Set Outputs]
512 ]Append["EndFunc"]
513 }
514 }{
515 //TODO: We need to store input and output types somewhere so we can reference them here
516 out <- "oops"
441 } 517 }
442 } 518 }
443 519
444 Blueprint C Program 520 Blueprint C Program
445 { 521 {
446 Functions 522 Functions
447 Method Registry 523 Method Registry
524 Field Registry
525 Type Registry
448 } 526 }
449 527
450 C Program[:out] 528 C Program[:out]
451 { 529 {
452 out <- [[Build["C Program"]]Functions <<[New@Dictionary[]]]Method Registry <<[C Method Registry[]] 530 out <- [[[[Build["C Program"]]Functions <<[New@Dictionary[]]]Method Registry <<[C Method Registry[]]]Type Registry <<[C Type Registry[]]]Field Registry <<[C Field Registry[]]
531 }
532
533 Register Type@C Program[program,def:out]
534 {
535 out <- [[[program]Type Registry <<[ [[program]Type Registry >>]Register Type[def] ]
536 ]Method Registry <<[ [def]Register Methods[[program]Method Registry >>] ]
537 ]Field Registry <<[ [def]Register Fields[[program]Field Registry >>] ]
538 }
539
540 Create Type@C Program[program,name:out]
541 {
542 out <- C Type[name]
453 } 543 }
454 544
455 Create Function@C Program[program,name,inputs,outputs,convention:out] 545 Create Function@C Program[program,name,inputs,outputs,convention:out]
456 { 546 {
457 out <- C Function With Registry[name,inputs,outputs,convention, [program]Method Registry >>] 547 out <- C Function With Registry[name,inputs,outputs,convention, [program]Method Registry >>]
506 #include \"builtin.h\" 596 #include \"builtin.h\"
507 #include \"object.h\" 597 #include \"object.h\"
508 #include \"context.h\" 598 #include \"context.h\"
509 #include \"func.h\" 599 #include \"func.h\"
510 #include \"integer.h\"\n\n" 600 #include \"integer.h\"\n\n"
511 out <- [[[[headers]Append[Fold["_Text C Program", 601 out <- [[[[[[headers
602 ]Append[[[program]Type Registry >>]Type Defs]
603 ]Append[Fold["_Text C Program",
512 Fold["_Consts C Program", 604 Fold["_Consts C Program",
513 Fold["_Defs C Program", "", [program]Functions >>], 605 Fold["_Defs C Program", "", [program]Functions >>],
514 constants 606 constants
515 ], [program]Functions >>]] 607 ], [program]Functions >>]]
516 ]Append["int main(int argc, char **argv) 608 ]Append["int main(int argc, char **argv)
517 { 609 {
518 returntype ret; 610 returntype ret;
519 calldata *cdata; 611 calldata *cdata;
520 context * ct; 612 context * ct;
521 register_builtin_types();\n\n"] 613 register_builtin_types();\n\n"]
614 ]Append[ [[program]Type Registry >>]Type Inits[[program]Method Registry >>, [program]Field Registry >>] ]
522 ]Append[Fold["_Set Consts C Program", "", constants]] 615 ]Append[Fold["_Set Consts C Program", "", constants]]
523 ]Append[" 616 ]Append["
524 ct = new_context(); 617 ct = new_context();
525 cdata = alloc_cdata(ct, 0); 618 cdata = alloc_cdata(ct, 0);
526 cdata->num_params = 0; 619 cdata->num_params = 0;