comparison cbackend.rhope @ 35:3498713c3dc9

C backend portion of supporting user defined types is done, but untested. Parser/NWorker still need a fair bit of work
author Mike Pavone <pavone@retrodev.com>
date Wed, 30 Sep 2009 23:55:04 -0400
parents df038cef648b
children 495dddadd058
comparison
equal deleted inserted replaced
34:df038cef648b 35:3498713c3dc9
89 Blueprint C Type 89 Blueprint C Type
90 { 90 {
91 Name 91 Name
92 Fields 92 Fields
93 Methods 93 Methods
94 Init
95 Copy
96 Cleanup
97
94 } 98 }
95 99
96 C Type[name:out] 100 C Type[name:out]
97 { 101 {
98 out <- [[[Build["C Type"]]Name <<[name]]Fields <<[()]]Methods <<[()] 102 out <- [[[[[[Build["C Type"]]Name <<[name]]Fields <<[()]]Methods <<[()]]Init <<["NULL"]]Copy <<["NULL"]]Cleanup <<["NULL"]
99 } 103 }
100 104
101 Add Field@C Type[ctype,name,type:out] 105 Add Field@C Type[ctype,name,type:out]
102 { 106 {
103 out <- [ctype]Fields <<[ [[ctype]Fields >>]Append[ [[()]Append[name]]Append[type] ] ] 107 out <- [ctype]Fields <<[ [[ctype]Fields >>]Append[ [[()]Append[name]]Append[type] ] ]
120 124
121 Rhope Type to C[typename,naked:out] 125 Rhope Type to C[typename,naked:out]
122 { 126 {
123 If[[typename] = ["Any Type"]] 127 If[[typename] = ["Any Type"]]
124 { 128 {
125 ctype <- "struct object" 129 out <- "struct object *"
126 }{ 130 }{
127 ctype <- ["t_"]Append[Escape Rhope Name[typename]] 131 ctype <- ["t_"]Append[Escape Rhope Name[typename]]
128 } 132 If[naked]
129 If[naked] 133 {
130 { 134 out <- Val[ctype]
131 out <- Val[ctype] 135 }{
132 }{ 136 out <- [ctype]Append[" *"]
133 out <- [ctype]Append[" * "] 137 }
134 } 138 }
135 } 139 }
136 140
137 _Type Def C Type[text,field:out] 141 _Type Def C Type[text,field:out]
138 { 142 {
139 name <- [field]Index[0] 143 name <- [field]Index[0]
140 type <- ["\n\t"]Append[Rhope Type to C[[field]Index[1], No]] 144 type <- ["\n\t"]Append[Rhope Type to C[[field]Index[1], No]]
141 out <- [[[text]Append[type]]Append[Escape Rhope Name[name]]]Append[";"] 145 out <- [[[[text]Append[type]]Append[" "]]Append[Escape Rhope Name[name]]]Append[";"]
142 } 146 }
143 147
144 Type Def@C Type[ctype:out] 148 Type Def@C Type[ctype:out]
145 { 149 {
146 out <- [Fold["_Type Def C Type", "OBegin", [ctype]Fields >>]]Append[ [["\nObject("]Append[Escape Rhope Name[[ctype]Name >>]]]Append[")"] ] 150 out <- [Fold["_Type Def C Type", "OBegin", [ctype]Fields >>]]Append[ [["\nObject("]Append[Escape Rhope Name[[ctype]Name >>]]]Append[")"] ]
152 } 156 }
153 157
154 Type Init@C Type[ctype,id,method reg,field reg:out] 158 Type Init@C Type[ctype,id,method reg,field reg:out]
155 { 159 {
156 //TODO: Handle function pointers for build/copy/destroy funcs 160 //TODO: Handle function pointers for build/copy/destroy funcs
157 start <- [["\tbp = register_type_byid("]Append[id]]Append[ [[", sizeof("]Append[["t_"]Append[Escape Rhope Name[ [ctype]Name >> ]]]]Append["), NULL, NULL, NULL);"] ] 161 start <- [["\tbp = register_type_byid("
162 ]Append[id]
163 ]Append[
164 [[", sizeof("
165 ]Append[
166 ["t_"]Append[Escape Rhope Name[ [ctype]Name >> ]]]
167 ]Append[
168 ["), "]Append[
169 [
170 [[[[Escape Rhope Name NU[[ctype]Init >>]
171 ]Append[", "]
172 ]Append[Escape Rhope Name NU[[ctype]Copy >> ]]
173 ]Append[", "]
174 ]Append[Escape Rhope Name NU[[ctype]Cleanup >>]]
175 ]Append[");"]]] ]
158 out <- Fold[[["_Type Init C"]Set Input[0, [ctype]Name >>]]Set Input[1, method reg], start, [ctype]Methods >>] 176 out <- Fold[[["_Type Init C"]Set Input[0, [ctype]Name >>]]Set Input[1, method reg], start, [ctype]Methods >>]
159 } 177 }
160 178
161 Blueprint C Type Registry 179 Blueprint C Type Registry
162 { 180 {
234 Outputs 252 Outputs
235 Convention 253 Convention
236 Variables 254 Variables
237 Statements 255 Statements
238 Method Registry 256 Method Registry
257 Field Registry
239 Constants 258 Constants
259 Input Types
260 Output Types
240 } 261 }
241 262
242 C Function[name,inputs,outputs,convention:out] 263 C Function[name,inputs,outputs,convention:out]
243 { 264 {
244 out <- C Function With Registry[name,inputs,outputs,convention, C Method Registry[]] 265 out <- C Function With Registry[name,inputs,outputs,convention, C Method Registry[], C Field Registry[]]
245 } 266 }
246 267
247 C Function With Registry[name,inputs,outputs,convention,registry:out] 268 C Function With Registry[name,inputs,outputs,convention,registry,field reg:out]
248 { 269 {
249 out <- [[[[[[[[Build["C Function"] 270 out <- [[[[[[[[[[[Build["C Function"]
250 ]Name <<[name] 271 ]Name <<[name]
251 ]Inputs <<[inputs] 272 ]Inputs <<[inputs]
252 ]Outputs <<[outputs] 273 ]Outputs <<[outputs]
253 ]Convention <<[convention] 274 ]Convention <<[convention]
254 ]Variables <<[New@Dictionary[]] 275 ]Variables <<[New@Dictionary[]]
255 ]Statements <<[()] 276 ]Statements <<[()]
256 ]Method Registry <<[registry] 277 ]Method Registry <<[registry]
278 ]Field Registry <<[field reg]
257 ]Constants <<[New@Dictionary[]] 279 ]Constants <<[New@Dictionary[]]
280 ]Input Types <<[ Fold[["Append"]Set Input[1, "Any Type"], (), inputs] ]
281 ]Output Types <<[ Fold[["Append"]Set Input[1, "Any Type"], (), outputs] ]
282 }
283
284 Set Input Type@C Function[func,input num,type:out]
285 {
286 out <- [func]Input Types <<[ [[func]Input Types >>]Set[input num, type] ]
287 }
288
289 Set Output Type@C Function[func,output num,type:out]
290 {
291 out <- [func]Output Types <<[ [[func]Output Types >>]Set[output num, type] ]
258 } 292 }
259 293
260 Register Constant@C Function[func,name,constant:out] 294 Register Constant@C Function[func,name,constant:out]
261 { 295 {
262 Print["Register Constant"] 296 Print["Register Constant"]
266 { Print["Got register constant output"] } 300 { Print["Got register constant output"] }
267 } 301 }
268 302
269 Allocate Var@C Function[func,name,type:out] 303 Allocate Var@C Function[func,name,type:out]
270 { 304 {
305 Print[["Allocate Var: "]Append[name]]
271 out <- [func]Variables <<[ [[func]Variables >>]Set[name,type] ] 306 out <- [func]Variables <<[ [[func]Variables >>]Set[name,type] ]
272 } 307 }
273 308
274 Add Statement@C Function[func,statement:out] 309 Add Statement@C Function[func,statement:out]
275 { 310 {
316 out <- [func]Add Statement[[[dest]Append[" = "]]Append[source]] 351 out <- [func]Add Statement[[[dest]Append[" = "]]Append[source]]
317 } 352 }
318 353
319 AddRef@C Function[func,psource,pdest:out] 354 AddRef@C Function[func,psource,pdest:out]
320 { 355 {
321 source <- [psource]Make Op[func] 356 source <- [psource]Make Op[func]
322 dest <- [pdest]Make Op[func] 357 dest <- [pdest]Make Op[func]
323 out <- [func]Add Statement[[[[dest]Append[" = add_ref("]]Append[source]]Append[")"]] 358 out <- [func]Add Statement[[[[dest]Append[" = add_ref("]]Append[source]]Append[")"]]
359 }
360
361 AddRef No Dest@C Function[func,psource:out]
362 {
363 source <- [psource]Make Op[func]
364 out <- [func]Add Statement[[["add_ref("]Append[source]]Append[")"]]
324 } 365 }
325 366
326 Release@C Function[func,psource:out] 367 Release@C Function[func,psource:out]
327 { 368 {
328 source <- [psource]Make Op[func] 369 source <- [psource]Make Op[func]
330 } 371 }
331 372
332 Set Null@C Function[func,pdest:out] 373 Set Null@C Function[func,pdest:out]
333 { 374 {
334 dest <- [pdest]Make Op[func] 375 dest <- [pdest]Make Op[func]
376 Print[["Set Null: "]Append[dest]]
335 out <- [func]Add Statement[[dest]Append[" = NULL"]] 377 out <- [func]Add Statement[[dest]Append[" = NULL"]]
336 } 378 }
337 379
338 Lookup Constant@C Function[func,const:out] 380 Lookup Constant@C Function[func,const:out]
339 { 381 {
340 out <- [["add_ref(_const_"]Append[Escape Rhope Name[const]]]Append[")"] 382 out <- [["add_ref(_const_"]Append[Escape Rhope Name[const]]]Append[")"]
383 }
384
385 Field Result@C Function[func,var,field:out]
386 {
387 out <- [[var]Append["->"]]Append[field]
388 }
389
390 Get Field@C Function[func,var,field:out,result op]
391 {
392 out <- func
393 result op <- Field Ref[var,field]
394 }
395
396 Set Field Null@C Function[func,var,field:out]
397 {
398 out <- [func]Add Statement[ [[[var]Append["->"]]Append[field]]Append[" = NULL"] ]
341 } 399 }
342 400
343 _Function Arg C[func,val,inputnum:out] 401 _Function Arg C[func,val,inputnum:out]
344 { 402 {
345 out <- [func]Add Statement[ 403 out <- [func]Add Statement[
365 rargs <- Map[args, ["Make Op"]Set Input[1, func]] 423 rargs <- Map[args, ["Make Op"]Set Input[1, func]]
366 out <- [Fold["_Function Arg C", func, rargs] 424 out <- [Fold["_Function Arg C", func, rargs]
367 ]Add Raw Line[[[[[ [type]Append["("] ]Append[tocall]]Append[", "]]Append[ [rargs]Length ]]Append[")"]] 425 ]Add Raw Line[[[[[ [type]Append["("] ]Append[tocall]]Append[", "]]Append[ [rargs]Length ]]Append[")"]]
368 } 426 }
369 427
428 Field Base@C Function[func,field,args,type:out]
429 {
430 rargs <- Map[args, ["Make Op"]Set Input[1, func]]
431 out <- [Fold["_Function Arg C", func, rargs]
432 ]Add Raw Line[[[ [type]Append["("] ]Append[ [[func]Field Registry >>]Field ID[field] ]]Append[")"]]
433 }
434
435 Get Field Call@C Function[func,field,source:out]
436 {
437 out <- [func]Field Base[field, [()]Append[source], "GFieldCall"]
438 }
439
440 Set Field Call@C Function[func,field,object,value:out]
441 {
442 out <- [func]Field Base[field, [[()]Append[object]]Append[value], "SFieldCall"]
443 }
444
370 Tail Method Call@C Function[func,method,args:out] 445 Tail Method Call@C Function[func,method,args:out]
371 { 446 {
372 out <- [func]Func Base[[[func]Method Registry >>]Method ID[method],args, "TMCall"] 447 out <- [func]Func Base[[[func]Method Registry >>]Method ID[method],args, "TMCall"]
373 } 448 }
374 449
377 out <- [func]Func Base[Escape Rhope Name[name],args, "TCall"] 452 out <- [func]Func Base[Escape Rhope Name[name],args, "TCall"]
378 } 453 }
379 454
380 Resolve@C Function[func,op:out] 455 Resolve@C Function[func,op:out]
381 { 456 {
382 [[func]Inputs >>]Find[op] 457 If[[[func]Convention >>] = ["rhope"]]
383 { 458 {
384 out <- [["cdata->params["]Append[~]]Append["]"] 459 [[func]Inputs >>]Find[op]
385 }{ 460 {
386 out <- ["locals->"]Append[Escape Rhope Name[op]] 461 out <- [["cdata->params["]Append[~]]Append["]"]
462 }{
463 out <- ["locals->"]Append[Escape Rhope Name[op]]
464 }
465 }{
466 out <- Escape Rhope Name[op]
387 } 467 }
388 } 468 }
389 469
390 Instruction Stream@C Function[func:out] 470 Instruction Stream@C Function[func:out]
391 { 471 {
421 ]Append[" : "] 501 ]Append[" : "]
422 ]Append[right] 502 ]Append[right]
423 ]Append[")"] 503 ]Append[")"]
424 } 504 }
425 505
426 Init Outputs@C Function[func:out]
427 {
428 If[[[[func]Outputs >>]Length ] > [0]]
429 {
430 out <- [["\tlocals->"]Append[ [[func]Outputs >>]Join[" = NULL;\n\tlocals->"] ]]Append[" = NULL;\n"]
431 }{
432 out <- ""
433 }
434 }
435
436 _Release Inputs[string,inputname,inputnum:out]
437 {
438 out <- [[[string
439 ]Append["\trelease_ref(cdata->params["]
440 ]Append[inputnum]
441 ]Append["]);\n"]
442 }
443
444 Release Inputs@C Function[func:out]
445 {
446 If[[[[func]Inputs >>]Length ] > [0]]
447 {
448 out <- Fold["_Release Inputs", "", [func]Inputs >>]
449 }{
450 out <- ""
451 }
452 }
453
454 _Set Outputs C[string,inputname,inputnum:out] 506 _Set Outputs C[string,inputname,inputnum:out]
455 { 507 {
456 out <- [string]Append[[[ [ ["\tRet("]Append[inputnum] ]Append[", locals->"]]Append[inputname]]Append[")\n"]] 508 out <- [string]Append[[[ [ ["\tRet("]Append[inputnum] ]Append[", locals->"]]Append[inputname]]Append[")\n"]]
457 } 509 }
458 510
459 Set Outputs@C Function[func:out] 511 Set Outputs@C Function[func:out]
460 { 512 {
461 If[[[[func]Outputs >>]Length ] > [0]] 513 If[[[func]Convention >>] = ["rhope"]]
462 { 514 {
463 out <- Fold["_Set Outputs C", "", [func]Outputs >>] 515 out <- Fold["_Set Outputs C", "", [func]Outputs >>]
464 }{ 516 }{
465 out <- "" 517 [[func]Outputs >>]Index[0]
466 } 518 {
467 } 519 out <- [["\treturn "]Append[Escape Rhope Name[~]]]Append[";\n"]
468 _Output Defs C[string,varname:out] 520 }{
469 { 521 out <- ""
470 out <- [[[string]Append["\tobject *"]]Append[Escape Rhope Name[varname]]]Append[";\n"] 522 }
523 }
524 }
525 _Output Defs C[string,varname,index,func:out]
526 {
527 out <- [[[string]Append[ ["\t"]Append[Rhope Type to C[[[func]Output Types >>]Index[index], No]] ]]Append[Escape Rhope Name[varname]]]Append[";\n"]
471 } 528 }
472 _Var Defs C[string,type,varname:out] 529 _Var Defs C[string,type,varname:out]
473 { 530 {
474 out <- [[[string]Append["\tobject *"]]Append[Escape Rhope Name[varname]]]Append[";\n"] 531 out <- [[[string]Append[ ["\t"]Append[Rhope Type to C[type, No]] ]]Append[Escape Rhope Name[varname]]]Append[";\n"]
475 } 532 }
476 533
477 534
478 Definitions@C Function[func:out] 535 Definitions@C Function[func:out]
479 { 536 {
480 If[ [ [[[func]Variables >>]Length]+[[[func]Outputs >>]Length] ] > [0]] 537 If[ [[[func]Convention >>] = ["rhope"]] And [[ [[[func]Variables >>]Length]+[[[func]Outputs >>]Length] ] > [0]] ]
481 { 538 {
482 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"] 539 localtype <- [[[Fold[["_Output Defs C"]Set Input[3, func], Fold["_Var Defs C","typedef struct {\n", [func]Variables >>], [func]Outputs >>]]Append["} l_"]]Append[Escape Rhope Name[[func]Name >>]]]Append[";\n"]
483 }{ 540 }{
484 out <- "" 541 localtype <- ""
485 } 542 }
543
544 If[ [[func]Convention >>] = ["rhope"] ]
545 {
546 parts <- [[func]Name >>]Split["@"]
547 [parts]Index[1]
548 {
549 proto <- [[[["MethodDef("
550 ]Append[Escape Rhope Name[[parts]Index[0]]]
551 ]Append[", "]
552 ]Append[Escape Rhope Name[~]]
553 ]Append[")\n"]
554 }{
555 proto <- [["FuncDef("]Append[Escape Rhope Name[[func]Name >>]]]Append[")\n"]
556 }
557 }{
558 proto <- [[func]Naked Proto]Append[";\n"]
559 }
560 out <- [localtype]Append[proto]
561 }
562
563 Naked Proto@C Function[func:out]
564 {
565 [[func]Output Types >>]Index[0]
566 {
567 outtype <- [Rhope Type to C[~, Yes]]Append[" "]
568 }{
569 outtype <- "void "
570 }
571 out <- [[[[outtype
572 ]Append[ Escape Rhope Name NU[[func]Name >>]]
573 ]Append["("]
574 ]Append[ [[func]Input Types >>]Join[", "] ]
575 ]Append[")"]
486 } 576 }
487 577
488 Text@C Function[func:out] 578 Text@C Function[func:out]
489 { 579 {
490 cname <- Escape Rhope Name[[func]Name >>]
491 If[ [[func]Convention >>] = ["rhope"] ] 580 If[ [[func]Convention >>] = ["rhope"] ]
492 { 581 {
582 cname <- Escape Rhope Name[[func]Name >>]
493 If[ [[[func]Variables >>]Length] = [0] ] 583 If[ [[[func]Variables >>]Length] = [0] ]
494 { 584 {
495 out <- [[[[[["FuncNoLocals(" 585 out <- [[[[[["FuncNoLocals("
496 ]Append[cname] 586 ]Append[cname]
497 ]Append[",\n\tNumParams "] 587 ]Append[",\n\tNumParams "]
510 ]Append[ [[func]Statements >>]Join[""] ] 600 ]Append[ [[func]Statements >>]Join[""] ]
511 ]Append[[func]Set Outputs] 601 ]Append[[func]Set Outputs]
512 ]Append["EndFunc"] 602 ]Append["EndFunc"]
513 } 603 }
514 }{ 604 }{
515 //TODO: We need to store input and output types somewhere so we can reference them here 605
516 out <- "oops" 606 out <- [[[
607 Fold[["_Output Defs C"]Set Input[3, func],
608 Fold["_Var Defs C", [[func]Naked Proto]Append["\n{"], [func]Variables >>], [func]Outputs >>]
609 ]Append[[[func]Statements >>]Join[""]]
610 ]Append[[func]Set Outputs]
611 ]Append["}"]
517 } 612 }
518 } 613 }
519 614
520 Blueprint C Program 615 Blueprint C Program
521 { 616 {
542 out <- C Type[name] 637 out <- C Type[name]
543 } 638 }
544 639
545 Create Function@C Program[program,name,inputs,outputs,convention:out] 640 Create Function@C Program[program,name,inputs,outputs,convention:out]
546 { 641 {
547 out <- C Function With Registry[name,inputs,outputs,convention, [program]Method Registry >>] 642 out <- C Function With Registry[name,inputs,outputs,convention, [program]Method Registry >>, [program]Field Registry >>]
548 } 643 }
549 644
550 Store Function@C Program[program,func:out] 645 Store Function@C Program[program,func:out]
551 { 646 {
552 out <- [program]Functions <<[ [[program]Functions >>]Set[ [func]Name >>, func] ] 647 out <- [program]Functions <<[ [[program]Functions >>]Set[ [func]Name >>, func] ]