comparison cbackend_c.rhope @ 92:e73a93fb5de1

Beginning of port of compiler to itself, some bugfixes and a refcount optimization
author Mike Pavone <pavone@retrodev.com>
date Mon, 02 Aug 2010 00:58:55 -0400
parents
children 5a08705f7610
comparison
equal deleted inserted replaced
91:bcdc326b3d6e 92:e73a93fb5de1
1 Import backendutils_c.rhope
2
3 Blueprint C Method Registry
4 {
5 Lookup
6 Next ID
7 }
8
9 C Method Registry[:out]
10 {
11 builtins <- [[[[[[[[[[[[[Dictionary[]
12 ]Set["+", "METHOD_ADD"]
13 ]Set["-", "METHOD_SUB"]
14 ]Set["/", "METHOD_DIV"]
15 ]Set["*", "METHOD_MUL"]
16 ]Set["LShift", "METHOD_LSHIFT"]
17 ]Set["RShift", "METHOD_RSHIFT"]
18 ]Set["=", "METHOD_EQUALS"]
19 ]Set[">", "METHOD_GREATER"]
20 ]Set["<", "METHOD_LESS"]
21 ]Set["If", "METHOD_IF"]
22 ]Set["Set Missing Field", "METHOD_SETFIELDMISSING"]
23 ]Set["Get Missing Field", "METHOD_GETFIELDMISSING"]
24 ]Set["Missing Method", "METHOD_MISSING"]
25 out <- [[Build[C Method Registry()]]Lookup <<[builtins]]Next ID<<[0]
26
27 }
28
29 Register Method@C Method Registry[reg,method:out]
30 {
31 [[reg]Lookup >>]Index[method]
32 {
33 out <- reg
34 }{
35 method ID <- [reg]Next ID>>
36 new lookup <- [[reg]Lookup >>]Set[method, ["METHOD_FIRST_USER+"]Append[String[method ID]]]
37 out <- [[reg]Lookup <<[new lookup]]Next ID <<[[method ID]+[1]]
38 }
39 }
40
41 Method ID@C Method Registry[reg,method:out,notfound]
42 {
43 out,notfound <- [[reg]Lookup >>]Index[method]
44 }
45
46 Blueprint C Field Registry
47 {
48 Lookup
49 Next ID
50 }
51
52 C Field Registry[:out]
53 {
54 out <- [[Build[C Field Registry()]]Lookup <<[Dictionary[]]]Next ID<<[1]
55
56 }
57
58 Register Field@C Field Registry[reg,field:out]
59 {
60 [[reg]Lookup >>]Index[field]
61 {
62 out <- reg
63 }{
64 field ID <- [reg]Next ID>>
65 new lookup <- [[reg]Lookup >>]Set[field, field ID]
66 out <- [[reg]Lookup <<[new lookup]]Next ID <<[[field ID]+[1]]
67 }
68 }
69
70 Field ID@C Field Registry[reg,field:out,notfound]
71 {
72 out,notfound <- [[reg]Lookup >>]Index[field]
73 }
74
75 Blueprint C Type
76 {
77 Name
78 Fields
79 Methods
80 Init
81 Copy
82 Cleanup
83
84 }
85
86 C Type[name:out]
87 {
88 out <- [[[[[[Build[C Type()]]Name <<[name]]Fields <<[()]]Methods <<[()]]Init <<["NULL"]]Copy <<["NULL"]]Cleanup <<["NULL"]
89 }
90
91 Add Field@C Type[ctype,name,type:out]
92 {
93 out <- [ctype]Fields <<[ [[ctype]Fields >>]Append[ [[()]Append[name]]Append[type] ] ]
94 }
95
96 Add Method@C Type[ctype,name:out]
97 {
98 out <- [ctype]Methods <<[ [[ctype]Methods >>]Append[name] ]
99 }
100
101 Register Methods@C Type[ctype,method reg:out]
102 {
103 out <- Fold[Register Method[?], method reg, [ctype]Methods >>]
104 }
105
106 _Register Field C[reg,field:out]
107 {
108 name <- [field]Index[0]
109 out <- [reg]Register Field[name]
110 }
111
112 Register Fields@C Type[ctype,field reg:out]
113 {
114 out <- Fold[_Register Field C[?], field reg, [ctype]Fields >>]
115 }
116
117 Rhope Type to C[type:out,array]
118 {
119 If[[Blueprint Of[type]]=[Type Instance()]]
120 {
121 variant <- [type]Variant >>
122 If[[[type]Name >>] = ["Array"]]
123 {
124 [("Naked","Raw Pointer")]Find[=[variant,?]]
125 {
126 /*
127 //Below code assumes that paramaterized types are implemented
128 pre param <- [[type]Params >>]Index[0] {}
129 { pre param <- Type Instance["Any Type"] }
130 [[type]Params >>]Index[1]
131 { param,param <- [pre param]Set Variant[~] }
132 { param <- Val[pre param] }
133 child type <- Rhope Type to C[param]
134 If[[variant] = ["Naked"]]
135 {
136 out <- Val[child type]
137 array <- "[1]"
138 }{
139 out <- [child type]Append[" *"]
140 array <- ""
141 } */
142 out <- "void *"
143 array <- ""
144 }{
145 typename <- "Array"
146 }
147 primitive <- No
148 }{
149 ,regulartype <- [("Naked","Raw Pointer")]Find[=[variant,?]]
150 {
151 [("Int64","Int32","Int16","Int8")]Find[=[[type]Name >>,?]]
152 {
153 primitive <- Yes
154 [[type]Name >>]Slice[3] {}
155 { typename <- [["int"]Append[~]]Append["_t"] }
156 }{
157 ,regulartype <- [("UInt64","UInt32","UInt16","UInt8")]Find[=[[type]Name >>,?]]
158 {
159 primitive <- Yes
160 [[type]Name >>]Slice[4] {}
161 { typename <- [["uint"]Append[~]]Append["_t"] }
162 }
163 }
164 }
165
166 Val[regulartype]
167 {
168 typename <- [type]Name >>
169 primitive <- No
170 }
171 }
172 }{
173 typename <- type
174 param <- "Any Type"
175 variant <- "boxed"
176 primitive <- No
177 }
178
179 Val[typename]
180 { array <- "" }
181 If[[typename] = ["Any Type"]]
182 {
183 out <- "struct object *"
184 }{
185 [("Naked","Raw Pointer")]Find[=[variant,?]]
186 {
187 If[primitive]
188 {
189 prefix <- ""
190 }{
191 prefix <- "nt_"
192 }
193 }{
194 prefix <- "t_"
195 }
196
197 If[[variant]=["Naked"]]
198 {
199 postfix <- ""
200 }{
201 postfix <- " *"
202 }
203 }
204 If[primitive]
205 {
206 escaped <- Val[typename]
207 }{
208 escaped <- Escape Rhope Name[typename]
209 }
210 out <- [[prefix]Append[escaped]]Append[postfix]
211 }
212
213 _Type Def C Type[text,field:out]
214 {
215 name <- [field]Index[0]
216 ,postfix <- Rhope Type to C[[field]Index[1]]
217 { type <- ["\n\t"]Append[~] }
218
219 out <- [[[[text]Append[type]]Append[" "]]Append[[Escape Rhope Name[name]]Append[postfix]]]Append[";"]
220 }
221
222 Type Def@C Type[ctype:out]
223 {
224 If[[[[ctype]Fields >>]Length] = [1]]
225 {
226 out <- [[[[_Type Def C Type["typedef struct {\n\tobject _SP_header;\n\t", [[ctype]Fields >>]Index[0]]]Append["\n} t_"]]Append[Escape Rhope Name[[ctype]Name >>]]]Append[";"]
227 ]Append[
228 [[[["typedef "
229 ]Append[Rhope Type to C[ [[[ctype]Fields >>]Index[0]]Index[1] ]]
230 ]Append[" nt_"]
231 ]Append[Escape Rhope Name[[ctype]Name >>]]
232 ]Append[";"] ]
233 }{
234 //HACK!!!
235 If[[[ctype]Name >>]=["Blueprint"]]
236 {
237 out <- ""
238 }{
239 [("Array","Worker")]Find[=[[ctype]Name >>,?]]
240 { oend <- "\nMObject(" }
241 { oend <- "\nObject(" }
242 out <- [Fold[_Type Def C Type[?], "OBegin", [ctype]Fields >>]]Append[ [[oend]Append[Escape Rhope Name[[ctype]Name >>]]]Append[")"] ]
243 }
244 }
245 }
246
247 _Type Init C[type name,method reg,text,method:out]
248 {
249 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["));"]
250 }
251
252 _Type Init C Field[type name,field reg,text,field:out]
253 {
254 fname <- [field]Index[0]
255 out <- [[[[text]Append[[["\n\tadd_getter(bp, "]Append[ String[[field reg]Field ID[fname]] ]]Append[ [[", MethodName("]Append[Escape Rhope Name[[fname]Append[" >>"]]]]Append[[","]Append[Escape Rhope Name[type name]]]]]]Append["));"]
256 ]Append[[["\n\tadd_setter(bp, "]Append[ String[[field reg]Field ID[fname]] ]]Append[ [[", MethodName("]Append[Escape Rhope Name[[fname]Append[" <<"]]]]Append[[","]Append[Escape Rhope Name[type name]]]]]]Append["));"]
257 }
258
259 Type Init@C Type[ctype,id,method reg,field reg:out]
260 {
261 [("Array","Worker")]Find[=[[ctype]Name >>, ?]]
262 { size <- "-1" }
263 {
264 [("Int64","Int32","Int16","Int8")]Find[=[[ctype]Name >>, ?]]
265 {
266 [[ctype]Name >>]Slice[3] {}
267 { typename <- [["int"]Append[~]]Append["_t"] }
268 }{
269 [("UInt64","UInt32","UInt16","UInt8")]Find[=[[ctype]Name >>, ?]]
270 {
271 [[ctype]Name >>]Slice[4] {}
272 { typename <- [["uint"]Append[~]]Append["_t"] }
273 }{
274 If[[[ctype]Name >>]=["Blueprint"]]
275 { typename <- "blueprint *" }
276 {
277 If[[[ctype]Name >>]=["Boolean"]]
278 { typename <- "int32_t" }
279 { typename <- ["nt_"]Append[Escape Rhope Name[[ctype]Name >>]] }
280 }
281 }
282 }
283 size <- [["sizeof("]Append[typename]]Append[")"]
284 }
285 start <- [["\tbp = register_type_byid("
286 ]Append[id]
287 ]Append[
288 [[", "]Append[size]
289 ]Append[
290 [", (special_func)"]Append[
291 [
292 [[[[Escape Rhope Name[[ctype]Init >>]
293 ]Append[", (special_func)"]
294 ]Append[Escape Rhope Name[[ctype]Copy >> ]]
295 ]Append[", (special_func)"]
296 ]Append[Escape Rhope Name[[ctype]Cleanup >>]]
297 ]Append[");"]]] ]
298 out <- Val[start]//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 >>]
299 }
300
301 Blueprint C Type Registry
302 {
303 Lookup
304 Definitions
305 Next ID
306 }
307
308 C Type Registry[:out]
309 {
310 out <- [[[Build[C Type Registry()]]Lookup << [
311 [[[[[[[[[[[[[[[[[[Dictionary[]
312 ]Set["UInt8", "TYPE_UINT8"] //1
313 ]Set["UInt16", "TYPE_UINT16"] //2
314 ]Set["UInt32", "TYPE_UINT32"] //3
315 ]Set["UInt64", "TYPE_UINT64"] //4
316 ]Set["Int8", "TYPE_INT8"] //5
317 ]Set["Int16", "TYPE_INT16"] //6
318 ]Set["Int32", "TYPE_INT32"] //7
319 ]Set["Int64", "TYPE_INT64"] //8
320 ]Set["Boolean", "TYPE_BOOLEAN"] //9
321 ]Set["Float32", "TYPE_FLOAT32"] //10
322 ]Set["Float64", "TYPE_FLOAT64"] //11
323 ]Set["Real Number", "TYPE_FLOAT64"] //12
324 ]Set["Blueprint", "TYPE_BLUEPRINT"] //13
325 ]Set["Array", "TYPE_ARRAY"] //14
326 ]Set["Worker", "TYPE_WORKER"] //15
327 ]Set["Method Missing Exception", "TYPE_METHODMISSINGEXCEPTION"] //16
328 ]Set["Field Missing Exception", "TYPE_FIELDMISSINGEXCEPTION"] //17
329 ]Set["Wrong Type Exception", "TYPE_WRONGTYPEEXCEPTION"]] //18
330 ]Definitions << [Dictionary[]]
331 ]Next ID <<[0]
332 }
333
334 _Type Defs C[text,def:out]
335 {
336 out <- [[text]Append[[def]Type Def]]Append["\n\n"]
337 }
338
339 Type Defs@C Type Registry[reg:out]
340 {
341 out <- Fold[_Type Defs C[?], "", [reg]Definitions >>]
342 }
343
344 _Type Inits C[reg,method reg,field reg,text,def,name:out]
345 {
346 out <- [[text]Append[ [def]Type Init[[reg]Type ID[name], method reg, field reg] ]]Append["\n\n"]
347 }
348
349 Type Inits@C Type Registry[reg,method reg,field reg:out]
350 {
351 out <- Fold[_Type Inits C[reg, method reg, field reg, ?], "", [reg]Definitions >>]
352 }
353
354 Register Type@C Type Registry[reg,def:out]
355 {
356 name <- [def]Name >>
357 [[reg]Lookup >>]Index[name]
358 {
359 [[reg]Definitions >>]Index[name]
360 {
361 out <- reg
362 }{
363 out <- [reg]Definitions <<[[[reg]Definitions >>]Set[name, def]]
364 }
365 }{
366 out <- [[[reg]Lookup <<[ [[reg]Lookup >>]Set[name, ["TYPE_FIRST_USER+"]Append[String[[reg]Next ID >>]]] ]
367 ]Definitions <<[ [[reg]Definitions >>]Set[name, def] ]
368 ]Next ID <<[ [[reg]Next ID >>]+[1] ]
369 }
370 }
371
372 Type ID@C Type Registry[reg,name:out,notfound]
373 {
374 out <- [[reg]Lookup >>]Index[name] {}
375 {
376 ,notfound <- If[[name]=["Any Type"]]
377 { out <- "0" }
378 }
379 }
380
381 Simple Type?@C Type Registry[reg,name:yep,nope,notfound]
382 {
383 ,notfound <- [[reg]Definitions >>]Index[name]
384 {
385 yep,nope <- If[[[[~]Fields >>]Length] = [1]]
386 }
387 }
388
389 Blueprint C Function
390 {
391 Name
392 Inputs
393 Outputs
394 Convention
395 Variables
396 Statements
397 Method Registry
398 Field Registry
399 Type Registry
400 Constants
401 Input Types
402 Output Types
403 Resume Index
404 Last NumParams
405 }
406
407 C Function[name,inputs,outputs,convention:out]
408 {
409 out <- C Function With Registry[name,inputs,outputs,convention, C Method Registry[], C Field Registry[], C Type Registry[]]
410 }
411
412 C Function With Registry[name,inputs,outputs,convention,registry,field reg,type reg:out]
413 {
414 out <- [[[[[[[[[[[[[[Build[C Function()]
415 ]Name <<[name]
416 ]Inputs <<[inputs]
417 ]Outputs <<[outputs]
418 ]Convention <<[convention]
419 ]Variables <<[Dictionary[]]
420 ]Statements <<[()]
421 ]Method Registry <<[registry]
422 ]Field Registry <<[field reg]
423 ]Type Registry <<[type reg]
424 ]Constants <<[Dictionary[]]
425 ]Input Types <<[ Fold[Append[?, "Any Type"], (), inputs] ]
426 ]Output Types <<[ Fold[Append[?, "Any Type"], (), outputs] ]
427 ]Resume Index <<[1]
428 ]Last NumParams <<[-1]
429 }
430
431 Set Input Type@C Function[func,type,input num:out]
432 {
433 out <- [func]Input Types <<[ [[func]Input Types >>]Set[input num, type] ]
434 }
435
436 Set Output Type@C Function[func,type,output num:out]
437 {
438 out <- [func]Output Types <<[ [[func]Output Types >>]Set[output num, type] ]
439 }
440
441 Register Constant@C Function[func,name,constant:out]
442 {
443 out <- [func]Constants <<[ [[func]Constants >>]Set[name, constant] ]
444 }
445
446 Allocate Var@C Function[func,name,type:out]
447 {
448 out <- [func]Variables <<[ [[func]Variables >>]Set[name,type] ]
449 }
450
451 Add Statement@C Function[func,statement:out]
452 {
453 out <- [func]Statements <<[ [[func]Statements >>]Append[["\t"]Append[[statement]Append[";\n"]]] ]
454 }
455
456 Add Raw Line@C Function[func,line:out]
457 {
458 out <- [func]Statements <<[ [[func]Statements >>]Append[["\t"]Append[[line]Append["\n"]]] ]
459 }
460
461 Add Operator Statement@C Function[func,psource1,psource2,pdest,op:out]
462 {
463 source1 <- [psource1]Make Op[func]
464 source2 <- [psource2]Make Op[func]
465 dest <- [pdest]Make Op[func]
466 out <- [func]Add Statement[[[[[dest]Append[" = "]]Append[source1]]Append[op]]Append[source2]]
467 }
468
469 Add@C Function[func,source1,source2,dest:out]
470 {
471 out <- [func]Add Operator Statement[source1,source2,dest," + "]
472 }
473
474 Sub@C Function[func,source1,source2,dest:out]
475 {
476 out <- [func]Add Operator Statement[source1,source2,dest," - "]
477 }
478
479 Multiply@C Function[func,source1,source2,dest:out]
480 {
481 out <- [func]Add Operator Statement[source1,source2,dest," * "]
482 }
483
484 Divide@C Function[func,source1,source2,dest:out]
485 {
486 out <- [func]Add Operator Statement[source1,source2,dest," / "]
487 }
488
489 DoLShift@C Function[func,source1,source2,dest:out]
490 {
491 out <- [func]Add Operator Statement[source1,source2,dest," << "]
492 }
493
494 DoRShift@C Function[func,source1,source2,dest:out]
495 {
496 out <- [func]Add Operator Statement[source1,source2,dest," >> "]
497 }
498
499 CompLess@C Function[func,source1,source2,dest:out]
500 {
501 out <- [func]Add Operator Statement[source1,source2,dest," < "]
502 }
503
504 CompGreater@C Function[func,source1,source2,dest:out]
505 {
506 out <- [func]Add Operator Statement[source1,source2,dest," > "]
507 }
508
509 CompEqual@C Function[func,source1,source2,dest:out]
510 {
511 out <- [func]Add Operator Statement[source1,source2,dest," == "]
512 }
513
514 CompLessEqual@C Function[func,source1,source2,dest:out]
515 {
516 out <- [func]Add Operator Statement[source1,source2,dest," <= "]
517 }
518
519 CompGreaterEqual@C Function[func,source1,source2,dest:out]
520 {
521 out <- [func]Add Operator Statement[source1,source2,dest," >= "]
522 }
523
524 CompNotEqual@C Function[func,source1,source2,dest:out]
525 {
526 out <- [func]Add Operator Statement[source1,source2,dest," != "]
527 }
528
529 Move@C Function[func,psource,pdest:out]
530 {
531 source <- [psource]Make Op[func]
532 dest <- [pdest]Make Op[func]
533 out <- [func]Add Statement[[[dest]Append[" = "]]Append[source]]
534 }
535
536 Do AddRef@C Function[func,psource,pdest:out]
537 {
538 source <- [psource]Make Op[func]
539 dest <- [pdest]Make Op[func]
540 out <- [func]Add Statement[[[[dest]Append[" = add_ref((object *)"]]Append[source]]Append[")"]]
541 }
542
543 AddRef No Dest@C Function[func,psource:out]
544 {
545 source <- [psource]Make Op[func]
546 out <- [func]Add Statement[[["add_ref((object *)"]Append[source]]Append[")"]]
547 }
548
549 Release@C Function[func,psource:out]
550 {
551 source <- [psource]Make Op[func]
552 out <- [func]Add Statement[[["release_ref((object *)"]Append[source]]Append[")"]]
553 }
554
555 Set Null@C Function[func,pdest:out]
556 {
557 dest <- [pdest]Make Op[func]
558 out <- [func]Add Statement[[dest]Append[" = NULL"]]
559 }
560
561 Lookup Constant@C Function[func,const,doaddref:out]
562 {
563 var <- ["_const_"]Append[Escape Rhope Name[const]]
564 If[doaddref]
565 {
566 out <- [["add_ref("]Append[var]]Append[")"]
567 }{
568 out <- Val[var]
569 }
570 }
571
572 Field Result@C Function[func,var,field:out]
573 {
574 as op <- [var]Make Op[func]
575 [(String(),String Cat(),String Slice())]Find[=[Blueprint Of[var],?]]
576 {
577 [[func]Inputs >>]Find[=[var,?]]
578 {
579 type <- [[func]Input Types >>]Index[~]
580
581 }{
582 type <- [[func]Variables >>]Index[var] { Print["op refers to a var"] }
583 {
584 [[func]Outputs >>]Find[=[var,?]]
585 {
586 type <- [[func]Output Types >>]Index[~]
587 }{
588 //Does it make sense for us to do this?
589 type <- Type Instance["Any Type"]
590 }
591 }
592 }
593 }{
594 type <- Type Instance["Any Type"]
595 }
596 If[[[func]Convention >>] = ["rhope"]]
597 {
598 If[[type] = ["Any Type"]]
599 {
600 rvar <- Val[as op]
601 }{
602 rvar <- [[[["(("]Append[ Rhope Type to C[type] ]]Append[")("]]Append[as op]]Append["))"]
603 }
604 }{
605 rvar <- Val[as op]
606 }
607
608 [[func]Type Registry >>]Simple Type?[[type]Name >>]
609 { access <- "->" }
610 { access <- "->payload." }
611 {
612 //TODO: Generate some kind of error/exception in this case
613 access <- "->"
614 }
615 out <- [[rvar]Append[access]]Append[Escape Rhope Name[field]]
616 }
617
618 Read Field@C Function[func,var,field:out,result op]
619 {
620 out <- func
621 result op <- Field Ref[var,field]
622 }
623
624 Write Field@C Function[func,var,field:out,result op]
625 {
626 out <- func
627 result op <- Field Ref[var,field]
628 }
629
630 Set Field Null@C Function[func,var,field:out]
631 {
632 out <- [func]Add Statement[ [[func]Field Result[var,field]]Append[" = NULL"] ]
633 }
634
635 Copy@C Function[func,pdest:out]
636 {
637 dest <- [pdest]Make Op[func]
638 out <- [func]Add Statement[ [dest]Append[[[" = copy_object("]Append[dest]]Append[")"]] ]
639 }
640
641 Box@C Function[func,psource,pdest,type:out]
642 {
643 dest <- [pdest]Make Op[func]
644 source <- [psource]Make Op[func]
645 out <- [func]Add Statement[
646 [[[[[dest
647 ]Append[" = naked_to_boxed("]
648 ]Append[ [[func]Type Registry >>]Type ID[[type]Name >>] ]
649 ]Append[", &"]
650 ]Append[source]
651 ]Append[")"] ]
652 }
653
654 Unbox@C Function[func,psource,pdest:out]
655 {
656 dest <- [pdest]Make Op[func]
657 source <- [psource]Make Op[func]
658 out <- [func]Add Statement[
659 [[[["boxed_to_naked("
660 ]Append[source]
661 ]Append[", &"]
662 ]Append[dest]
663 ]Append[")"] ]
664 }
665
666 Get Raw Pointer@C Function[func,psource,pdest:out]
667 {
668 dest <- [pdest]Make Op[func]
669 source <- [psource]Make Op[func]
670 out <- [func]Add Statement[ [[[dest]Append[" = (void*)("]]Append[source]]Append[" + 1)"] ]
671 }
672
673 Array Raw Pointer@C Function[func,psource,pdest:out]
674 {
675 dest <- [pdest]Make Op[func]
676 source <- [psource]Make Op[func]
677 out <- [func]Add Statement[ [[[dest]Append[" = ((char *)"]]Append[source]]Append[")+ sizeof(t_Array)"] ]
678 }
679
680 _Function Arg C[func,val,inputnum:out]
681 {
682 out <- [func]Add Raw Line[
683 [[[["SetParam("
684 ]Append[String[inputnum]]
685 ]Append[", "]
686 ]Append[val]
687 ]Append[")"]
688 ]
689 }
690
691 _Val Function Arg C[func,val,inputnum,worker:out]
692 {
693 out <- [func]Add Raw Line[
694 [[[[[["VCSetParam("
695 ]Append[worker]
696 ]Append[", "]
697 ]Append[String[inputnum]]
698 ]Append[", "]
699 ]Append[val]
700 ]Append[")"]
701 ]
702 }
703
704 Method Call@C Function[func,method,args:out]
705 {
706 out <- [func]Call[method,args]
707 }
708
709 Val Call@C Function[func,to call,args:out]
710 {
711 worker <- Make Op[Strip Addref[to call], func]
712 rargs <- Map[args, Make Op[?, func]]
713
714 If[[[func]Last NumParams >>] = [-1]]
715 {
716 freed <- Val[func]
717 }{
718 freed <- [func]Add Raw Line["FreeCall"]
719 }
720 prepped <- [[freed]Add Raw Line[
721 [[[["VCPrepCall("
722 ]Append[worker]
723 ]Append[", "]
724 ]Append[String[[rargs]Length]]
725 ]Append[")"] ]
726 ]Last NumParams <<[[rargs]Length]
727
728
729 out <- [[[[Fold[_Val Function Arg C[?, ?, ?, worker], prepped, rargs]
730 ]Add Raw Line[
731 [[[[[[[["ValCall("
732 ]Append[worker]
733 ]Append[", "]
734 ]Append[String[[rargs]Length]]
735 ]Append[", "]
736 ]Append[String[[func]Resume Index >>]]
737 ]Append[", "]
738 ]Append[Escape Rhope Name[[func]Name >>]]
739 ]Append[")"]]
740 ]Add Raw Line["DISPATCH"]
741 ]Add Raw Line[
742 [[[["ValCallPostlude("
743 ]Append[String[[func]Resume Index >>]]
744 ]Append[", "]
745 ]Append[Escape Rhope Name[[func]Name >>]]
746 ]Append[")"]]
747 ]Resume Index <<[ [[func]Resume Index >>]+[1] ]
748 }
749
750 Call@C Function[func,name,args:out]
751 {
752 If[[name]=["Call@Worker"]]
753 {
754 //TODO: Handle case when user explicitly calls the fully qualified version, but the type of the first arg isn't Worker
755 out <- [func]Val Call[[args]Index[0], Tail[args,1]]
756 }{
757 If[[name]=["Call"]]
758 {
759 to call <- [args]Index[0]
760 last numparams <- [func]Last NumParams >>
761 out <- [[[[[[func]Add Raw Line[[["if (get_blueprint("]Append[Make Op[Strip Addref[to call], func]]]Append[")->type_id == TYPE_WORKER) {"]]
762 ]Val Call[to call, Tail[args,1]]
763 ]Add Raw Line["} else {"]
764 ]Last NumParams <<[last numparams]
765 ]Func Base["Call",args, "Call"]
766 ]Add Raw Line["}"]
767 }{
768 out <- [func]Func Base[Escape Rhope Name[name],args, "Call"]
769 }
770 }
771 }
772
773 Func Base@C Function[func,tocall,args,type:out]
774 {
775 Print[ [[func]Name >>]Append[ [": Func Base("]Append[tocall] ] ]
776 rargs <- Map[args, Make Op[?, func]]
777
778 If[[[rargs]Length] > [[func]Last NumParams >>]]
779 {
780 If[[[func]Last NumParams >>] = [-1]]
781 {
782 freed <- Val[func]
783 }{
784 freed <- [func]Add Raw Line["FreeCall"]
785 }
786 prepped <- [[freed]Add Raw Line[ [["PrepCall("]Append[String[[rargs]Length]]]Append[")"] ]
787 ]Last NumParams <<[[rargs]Length]
788 }{
789 prepped <- Val[func]
790 }
791
792
793 out <- [[Fold[_Function Arg C[?], prepped, rargs]
794 ]Add Raw Line[
795 [[[[[[[[[type]Append["("]
796 ]Append[tocall]
797 ]Append[", "]
798 ]Append[String[[rargs]Length]]
799 ]Append[", "]
800 ]Append[String[[func]Resume Index >>]]
801 ]Append[", "]
802 ]Append[Escape Rhope Name[[func]Name >>]]
803 ]Append[")"]]
804 ]Resume Index <<[ [[func]Resume Index >>]+[1] ]
805 }
806
807 Call Foreign@C Function[func,name,language,args,store result:out]
808 {
809 rargs <- Map[args, Make Op[?, func]]
810 //Assume language = "C" for now
811 base <- [[[name]Append["("]]Append[ Join[rargs, ", "] ]]Append[")"]
812 ,do store <- [(String(), String Slice(), String Cat())]Find[=[Blueprint Of[store result], ?]]
813 {
814 ,do store <- If[[store result]=[""]]
815 { stmt <- Val[base] }
816 }
817
818 Val[do store]
819 { stmt <- [[Make Op[store result, func]]Append[" = "]]Append[base] }
820 out <- [func]Add Statement[stmt]
821 }
822
823 Get Field Call@C Function[func,field,source:out]
824 {
825 out <- [func]Func Base[Escape Rhope Name[[field]Append[" >>"]], [()]Append[source], "Call"]
826 }
827
828 Set Field Call@C Function[func,field,object,value:out]
829 {
830 out <- [func]Func Base[Escape Rhope Name[[field]Append[" <<"]], [[()]Append[object]]Append[value], "Call"]
831 }
832
833 Tail Method Call@C Function[func,method,args:out]
834 {
835 out <- [func]Func Base[[[func]Method Registry >>]Method ID[method],args, "TMCall"]
836 }
837
838 Tail Call@C Function[func,name,args:out]
839 {
840 out <- [func]Func Base[Escape Rhope Name[name],args, "TCall"]
841 }
842
843 Resolve@C Function[func,op:out]
844 {
845 If[[[func]Convention >>] = ["rhope"]]
846 {
847 [[func]Inputs >>]Find[=[op,?]]
848 {
849 out <- [["my_cdata->params["]Append[String[~]]]Append[" ]"]
850 }{
851 out <- [[["lv_"]Append[Escape Rhope Name[[func]Name >>]]]Append["->"]]Append[Escape Rhope Name[op]]
852 }
853 }{
854 out <- Escape Rhope Name[op]
855 }
856 }
857
858 Resolve Output@C Function[func,name:out]
859 {
860 If[[[func]Convention >>] = ["rhope"]]
861 {
862 out <- [[["lv_"]Append[Escape Rhope Name[[func]Name >>]]]Append["->"]]Append[Escape Rhope Name[name]]
863 }{
864 out <- Escape Rhope Name[name]
865 }
866 }
867
868 Instruction Stream@C Function[func:out]
869 {
870 out <- [func]Statements <<[()]
871 }
872
873 _If C[func, statement:out]
874 {
875 out <- [func]Statements <<[ [[func]Statements >>]Append[ ["\t"]Append[statement] ] ]
876 }
877
878 Do If@C Function[func,condition,stream:out]
879 {
880 cond <- [condition]Make Op[func]
881 out <- [[Fold[_If C[?], [[func
882 ]Add Raw Line[ [["if("]Append[cond]]Append[")"] ]
883 ]Add Raw Line["{"], [stream]Statements >>]
884 ]Add Raw Line["}"]
885 ]Resume Index <<[[stream]Resume Index >>]
886
887 }
888
889 Discard Outputs@C Function[func,first to discard:out]
890 {
891 out <- [[[[[func
892 ]Add Raw Line[[["for(idx = "]Append[String[first to discard]]]Append["; idx < cdata->num_params; ++idx)"]]
893 ]Add Raw Line["{"]
894 ]Add Raw Line[" if (cdata->params[idx])"]
895 ]Add Raw Line[" release_ref(cdata->params[idx]);"]
896 ]Add Raw Line["}"]
897 }
898
899 Result Reference@C Function[func,output:out]
900 {
901 out <- [["cdata->params["]Append[String[output]]]Append["]"]
902 }
903
904 Checked Result Reference@C Function[func,output:out]
905 {
906 out <- [[[["("]Append[output]]Append[" < cdata->num_params ? cdata->params["]]Append[output]]Append["] : NULL)"]
907 }
908
909
910 If Null Else@C Function[func,left,right:out]
911 {
912 check <- [[Make Condition[left]]Strip Addref]Make Op[func]
913 l <- [left]Make Op[func]
914 r <- [right]Make Op[func]
915 out <- [[[[[["("
916 ]Append[check]
917 ]Append[" ? "]
918 ]Append[l]
919 ]Append[" : "]
920 ]Append[r]
921 ]Append[")"]
922 }
923
924 _Set Outputs C[string,inputname,inputnum,func:out]
925 {
926 out <- [string]Append[[[ [ ["\tRet("]Append[String[inputnum]] ]Append[ [[", lv_"]Append[Escape Rhope Name[[func]Name >>]]]Append["->"]]]Append[Escape Rhope Name[inputname]]]Append[")\n"]]
927 }
928
929 Set Outputs@C Function[func:out]
930 {
931 If[[[func]Convention >>] = ["rhope"]]
932 {
933 out <- [[[Fold[_Set Outputs C[?, ?, ?, func], "", [func]Outputs >>]]Append["\tNumRet("]]Append[String[[[func]Outputs >>]Length]]]Append[")\n"]
934 }{
935 [[func]Outputs >>]Index[0]
936 {
937 out <- [["\treturn "]Append[Escape Rhope Name[~]]]Append[";\n"]
938 }{
939 out <- ""
940 }
941 }
942 }
943 _Output Defs C[string,varname,index,func:out]
944 {
945 out <- [[[string]Append[ ["\t"]Append[Rhope Type to C[[[func]Output Types >>]Index[index]]] ]]Append[[" "]Append[Escape Rhope Name[varname]]]]Append[";\n"]
946 }
947 _Var Defs C[string,type,varname:out]
948 {
949 Print[["Variable: "]Append[varname]]
950 {
951 out <- [[[string]Append[ ["\t"]Append[Rhope Type to C[type]] ]]Append[[" "]Append[Escape Rhope Name[varname]]]]Append[";\n"]
952 { Print["Produced Output"]}
953 }
954 }
955
956
957 Definitions@C Function[func:out]
958 {
959 Print[["Definitions@C Function: "]Append[[func]Name >>]]
960 {
961 If[ [[[func]Convention >>] = ["rhope"]] And [[ [[[func]Variables >>]Length]+[[[func]Outputs >>]Length] ] > [0]] ]
962 {
963 localtype <- [[[Fold[_Output Defs C[?, ?, ?, func], Fold[_Var Defs C[?],"typedef struct {\n", [func]Variables >>], [func]Outputs >>]]Append["} lt_"]]Append[Escape Rhope Name[[func]Name >>]]]Append[";\n"]
964 }{
965 localtype <- ""
966 }
967
968 If[ [[func]Convention >>] = ["rhope"] ]
969 {
970 /* parts <- [[func]Name >>]Split["@"]
971 [parts]Index[1]
972 {
973 proto <- [[[["MethodDef("
974 ]Append[Escape Rhope Name[[parts]Index[0]]]
975 ]Append[", "]
976 ]Append[Escape Rhope Name[~]]
977 ]Append[")\n"]
978 }{
979 proto <- [["FuncDef("]Append[Escape Rhope Name[[func]Name >>]]]Append[")\n"]
980 } */
981 out <- Val[localtype]
982 }{
983 out <- [[func]Naked Proto]Append[";\n"]
984 }
985 }
986 }
987
988 _Proto Input[list,input,index,types:out]
989 {
990 out <- [list]Append[ [[Rhope Type to C[[types]Index[index]]]Append[" "]]Append[Escape Rhope Name[input]] ]
991 }
992
993 Naked Proto@C Function[func:out]
994 {
995 [[func]Output Types >>]Index[0]
996 {
997 outtype <- [Rhope Type to C[~]]Append[" "]
998 }{
999 outtype <- "void "
1000 }
1001 out <- [[[[outtype
1002 ]Append[ Escape Rhope Name[[func]Name >>]]
1003 ]Append["("]
1004 ]Append[ [Fold[_Proto Input[?, ?, ?, [func]Input Types >>], (), [func]Inputs >>]]Join[", "] ]
1005 ]Append[")"]
1006 }
1007
1008 Type Check@C Function[func,text,type,input num:out]
1009 {
1010 If[[type] = ["Any Type"]]
1011 {
1012 out <- text
1013 }{
1014 out <- [text]Append[ [["\tParam("]Append[input num]]Append[ [[", "]Append[ [[func]Type Registry >>]Type ID[type] ]]Append[")"] ] ]
1015 }
1016 }
1017
1018 Check Param Type C[text,type,input num,func:out]
1019 {
1020 [(String(),String Cat(),String Slice())]Find[=[Blueprint Of[type],?]]
1021 {
1022 typename <- type
1023 }{
1024 typename <- [type]Name >>
1025 }
1026 If[[typename] = ["Any Type"]]
1027 {
1028 out <- text
1029 }{
1030 out <- [text]Append[[[["\tParam("]Append[String[input num]]]Append[ [","]Append[ [[func]Type Registry >>]Type ID[typename] ] ]]Append[")\n"]]
1031 }
1032 }
1033
1034 Text@C Function[func:out]
1035 {
1036 Print[["Text@C Function: "]Append[[func]Name >>]]
1037 If[ [[func]Convention >>] = ["rhope"] ]
1038 {
1039 before <- [[func]Name >>]Partition["@"] {} {}
1040 {
1041 type <- "MethodImpl"
1042 cname <- [[[[Escape Rhope Name[before]
1043 ]Append[", "]
1044 ]Append[Escape Rhope Name[~]]
1045 ]Append[", "]
1046 ]Append[ [[func]Type Registry >>]Type ID[~] ]
1047 }{
1048 type <- "Func"
1049 cname <- Val[fname]
1050 }
1051 fname <- Escape Rhope Name[[func]Name >>]
1052 param check <- Fold[Check Param Type C[?, ?, ?, func], "", [func]Input Types >>]
1053 If[ [ [[[func]Variables >>]Length]+[[[func]Outputs >>]Length] ] = [0] ]
1054 {
1055 out <- [[[[[[[[ [type]Append["NoLocals("]
1056 ]Append[cname]
1057 ]Append[",\n\tNumParams "]
1058 ]Append[ String[[[func]Inputs >>]Length] ]
1059 ]Append[")\n\n"]
1060 ]Append[param check]
1061 ]Append[ [[func]Statements >>]Join[""] ]
1062 ]Append["EndFuncNoLocals\n"]
1063 ]Append["DISPATCH"]
1064 }{
1065 If[[[func]Last NumParams >>] = [-1]]
1066 {
1067 freecall <- ""
1068 }{
1069 freecall <- "\n\tFreeCall\n"
1070 }
1071 out <- [[[[[[[[[[ [type]Append["("]
1072 ]Append[cname]
1073 ]Append[",\n\tNumParams "]
1074 ]Append[ String[[[func]Inputs >>]Length] ]
1075 ]Append[")\n\n"]
1076 ]Append[param check]
1077 ]Append[ [[func]Statements >>]Join[""] ]
1078 ]Append[freecall]
1079 ]Append[[func]Set Outputs]
1080 ]Append[[["EndFunc("]Append[fname]]Append[")\n"]]
1081 ]Append["DISPATCH"]
1082 }
1083 }{
1084
1085 out <- [[[
1086 Fold[_Output Defs C[?, ?, ?, func],
1087 Fold[_Var Defs C[?], [[func]Naked Proto]Append["\n{"], [func]Variables >>], [func]Outputs >>]
1088 ]Append[[[func]Statements >>]Join[""]]
1089 ]Append[[func]Set Outputs]
1090 ]Append["}"]
1091 }
1092 }
1093
1094 Blueprint C Program
1095 {
1096 Functions
1097 Method Registry
1098 Field Registry
1099 Type Registry
1100 Libraries
1101 }
1102
1103 C Program[:out]
1104 {
1105 out <- [[[[[Build[C Program()]]Functions <<[Dictionary[]]]Method Registry <<[C Method Registry[]]]Type Registry <<[C Type Registry[]]]Field Registry <<[C Field Registry[]]]Libraries <<[Dictionary[]]
1106 }
1107
1108 Link@C Program[program,language,library:out]
1109 {
1110 If[[library] = ["runtime"]]
1111 {
1112 out <- program
1113 }{
1114 langlibs <- [[program]Libraries >>]Index[language] {}
1115 { langlibs <- Dictionary[] }
1116 out <- [program]Libraries <<[ [[program]Libraries >>]Set[language, [langlibs]Set[library, Yes]] ]
1117 }
1118 }
1119
1120 Register Type@C Program[program,def:out]
1121 {
1122 out <- [[[program]Type Registry <<[ [[program]Type Registry >>]Register Type[def] ]
1123 ]Method Registry <<[ [def]Register Methods[[program]Method Registry >>] ]
1124 ]Field Registry <<[ [def]Register Fields[[program]Field Registry >>] ]
1125 }
1126
1127 Create Type@C Program[program,name:out]
1128 {
1129 out <- C Type[name]
1130 }
1131
1132 Create Function@C Program[program,name,inputs,outputs,convention:out]
1133 {
1134 out <- C Function With Registry[name,inputs,outputs,convention, [program]Method Registry >>, [program]Field Registry >>, [program]Type Registry >>]
1135 }
1136
1137 Store Function@C Program[program,func:out]
1138 {
1139 out <- [program]Functions <<[ [[program]Functions >>]Set[ [func]Name >>, func] ]
1140 }
1141
1142 Method?@C Program[program,funcname:is,isnot]
1143 {
1144 is,isnot <- [[program]Method Registry >>]Method ID[funcname]
1145 }
1146
1147 _Defs C Program[text,func:out]
1148 {
1149 def <- [func]Definitions
1150 If[[def]=[""]]
1151 {
1152 out <- text
1153 }{
1154 out <- [text]Append[[def]Append["\n\n"]]
1155 }
1156 }
1157
1158 _Text C Program[text,func,type reg:out]
1159 {
1160 out <- [text]Append[[[ [func]Type Registry <<[type reg] ]Text]Append["\n\n"]]
1161 }
1162
1163 Combine Consts[consts,func:out]
1164 {
1165 out <- Combine[[func]Constants >>, consts]
1166 }
1167
1168 _Consts C Program[text,value,name:out]
1169 {
1170 out <- [text]Append[ [["object * _const_"]Append[Escape Rhope Name[name]]]Append[";\n"] ]
1171 }
1172
1173 Const Construct C[value,type reg:out]
1174 {
1175 valtype <- Blueprint Of[value]
1176 [(Int8(),UInt8(),Int16(),UInt16(),Int32(),UInt32(),Int64(),UInt64())]Find[=[valtype,?]]
1177 {
1178 size <- [("8","16","32","64")]Index[[~]/[2]]
1179 If[[~]Mod[2]]
1180 { s <- "UI" }
1181 { s <- "I" }
1182
1183 out <- [[[[[["make_"
1184 ]Append[s]
1185 ]Append["nt"]
1186 ]Append[size]
1187 ]Append["("]
1188 ]Append[String[value]]
1189 ]Append[")"]
1190 }{
1191 If[[valtype] = [Type Instance()]]
1192 {
1193 //TODO: Support parametric types
1194 typeid <- [type reg]Type ID[[value]Name >>]
1195 out <- [["make_Blueprint("]Append[typeid]]Append[")"]
1196 }{
1197 If[[valtype] = [Boolean()]]
1198 {
1199 If[value]
1200 {
1201 out <- "make_Bool(1)"
1202 }{
1203 out <- "make_Bool(0)"
1204 }
1205 }{
1206
1207 [(String(),String Slice(),String Cat())]Find[=[valtype,?]]
1208 {
1209 out <- [["make_String(\""]Append[ [[[value]Replace["\\", "\\\\"]]Replace["\n", "\\n"]]Replace["\"", "\\\""] ]]Append["\")"]
1210 }{
1211 If[[valtype]=[Worker Literal()]]
1212 {
1213 //TODO: Figure out how to fully support these in nested cases
1214 //or workaround the problem higher up in the food chain
1215 [[value]Args >>]Last
1216 { size <- String[[~]+[1]] }
1217 { size <- "0" }
1218 out <- [[[[[["make_Worker(FUNC_"
1219 ]Append[Escape Rhope Name[[value]Name >>]]
1220 ]Append[", "]
1221 ]Append[size]
1222 ]Append[", "]
1223 ]Append[String[Fold[+[1,?], 0, [value]Args >>]]]
1224 ]Append[")"]
1225 }{
1226 out <- "UnhandledLiteralType"
1227 }
1228 }
1229 }
1230 }
1231
1232 }
1233 }
1234
1235 _Set Worker Params C[text,param,num,type reg,name:out]
1236 {
1237 out <- [text]Append[
1238 [[[[[["\t((object **)(((t_Worker *)_const_"
1239 ]Append[name]
1240 ]Append[")+1))["]
1241 ]Append[String[num]]
1242 ]Append["] = "]
1243 ]Append[Const Construct C[param, type reg]]
1244 ]Append[";\n"] ]
1245 }
1246
1247 _Set Consts C Program[text,value,name,type reg:out]
1248 {
1249 valtype <- Blueprint Of[value]
1250 [(String(),String Cat(),String Slice(),Worker Literal(),List(),List Leaf())]Find[=[valtype,?]]
1251 {
1252 out <- text
1253 }{
1254 Const Construct C[value,type reg]
1255 { out <- [text]Append[ [[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = "]]Append[~]]Append[";\n"] ] }
1256 }
1257 }
1258
1259 _Set List Els[text,el,index,type reg:out]
1260 {
1261 out <- [[text]Append[
1262 [["\tinout[1] = "
1263 ]Append[Const Construct C[index,type reg]]
1264 ]Append[
1265 [[";\n\tinout[2] = "
1266 ]Append[Const Construct C[el, type reg]]
1267 ]Append[";\n"]
1268 ]]]Append["\trhope(FUNC_Set, inout, 3, 3);\n"]
1269 }
1270
1271 _Set Late Consts C[text,value,name,type reg:out]
1272 {
1273 valtype <- Blueprint Of[value]
1274 [(String(),String Cat(),String Slice(),Worker Literal(),List(),List Leaf())]Find[=[valtype,?]]
1275 {
1276 If[[~]>[3]]
1277 {
1278 out <- [Fold[_Set List Els[?, ?, ?, type reg], "\trhope(FUNC_List, inout, 0, 1);\n", value]
1279 ]Append[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = inout[0];\n"]]
1280 }{
1281 Const Construct C[value,type reg]
1282 { init <- [text]Append[ [[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = "]]Append[~]]Append[";\n"] ] }
1283
1284 If[[valtype]=[Worker Literal()]]
1285 {
1286 out <- Fold[_Set Worker Params C[?, ?, ?, type reg, Escape Rhope Name[name]], init, [value]Args >>]
1287 }{
1288 out <- Val[init]
1289 }
1290 }
1291 }{
1292 out <- text
1293 }
1294 }
1295
1296 _Dispatch Switch Sub[text, num, name:out]
1297 {
1298 out <- [[[[[text
1299 ]Append["\tResumeEntry("]
1300 ]Append[String[num]]
1301 ]Append[","]
1302 ]Append[name]
1303 ]Append[")\\\n"]
1304 }
1305
1306 _Dispatch Switch[text,func,raw name:out]
1307 {
1308 If[[[func]Convention >>] = ["rhope"]]
1309 {
1310 name <- Escape Rhope Name[raw name]
1311 out <- [[text]Append[ [["\tDispatchEntry("]Append[name]]Append[")\\\n"] ]
1312 ]Append[Fold[_Dispatch Switch Sub[?, ?, name], "", Range[1, [func]Resume Index >>]]]
1313 }{
1314 out <- text
1315 }
1316 }
1317
1318 _Dispatch Switch Methods[text,id,raw name:out]
1319 {
1320 name <- Escape Rhope Name[raw name]
1321 out <- [text]Append[ [["\tDispatchEntry("]Append[name]]Append[")\\\n"] ]
1322 }
1323
1324 _Dispatch Enum Sub[text, num, name:out]
1325 {
1326 out <- [[[[[text
1327 ]Append["\tRES_"]
1328 ]Append[String[num]]
1329 ]Append["_"]
1330 ]Append[name]
1331 ]Append[",\n"]
1332 }
1333
1334 _Dispatch Enum[text,func,raw name:out]
1335 {
1336 If[[[func]Convention >>] = ["rhope"]]
1337 {
1338 name <- Escape Rhope Name[raw name]
1339 out <- [[text]Append[ [["\tFUNC_"]Append[name]]Append[",\n"] ]
1340 ]Append[Fold[_Dispatch Enum Sub[?, ?, name], "", Range[1, [func]Resume Index >>]]]
1341 }{
1342 out <- text
1343 }
1344 }
1345
1346 _Dispatch Enum Methods[text,types,name:out]
1347 {
1348 out <- [text]Append[ [["\tFUNC_"]Append[Escape Rhope Name[name]]]Append[",\n"] ]
1349 }
1350
1351 Dispatch@C Program[program,all methods:out]
1352 {
1353 out <- [[[[["typedef enum {\n"
1354 ]Append[Fold[_Dispatch Enum[?],
1355 [Fold[_Dispatch Enum Methods[?], "", all methods]]Append["\tFUNC_Build,\n\tFUNC_BlueprintSP_Of,\n\tFUNC_ID,\n\tFUNC_BlueprintSP_FromSP_ID,\n"],
1356 [program]Functions >>]]
1357 ]Append["\tEND\n} funcids;\n\n"]
1358 ]Append["#define DispatchEntries \\\n"]
1359 ]Append[Fold[_Dispatch Switch[?],
1360 [Fold[_Dispatch Switch Methods[?], "", all methods]]Append["\tDispatchEntry(Build)\\\n\tDispatchEntry(BlueprintSP_Of)\\\n\tDispatchEntry(ID)\\\n\tDispatchEntry(BlueprintSP_FromSP_ID)\\\n"],
1361 [program]Functions >>]]
1362 ]Append["\tEndEntry\n\n"]
1363 }
1364
1365 Not Native[func:out]
1366 {
1367 If[[[func]Convention >>] = ["rhope"]]
1368 { out <- No }
1369 { out <- Yes }
1370 }
1371
1372 Native[func:out]
1373 {
1374 out <- [[func]Convention >>] = ["rhope"]
1375 }
1376
1377 Local Pointers[text,func:out]
1378 {
1379 If[ [ [[[func]Variables >>]Length]+[[[func]Outputs >>]Length] ] = [0] ]
1380 {
1381 out <- text
1382 }{
1383 out <- [text]Append[[["\tFuncDef("]Append[Escape Rhope Name[[func]Name >>]]]Append[")\n"]]
1384 }
1385 }
1386
1387 _Method to Types[dict,name,type:out]
1388 {
1389 typelist <- [dict]Index[name] {}
1390 { typelist <- () }
1391
1392 out <- [dict]Set[name, [typelist]Append[[type]Name >>]]
1393
1394 }
1395
1396 _Field to Types[dict,field,type:out]
1397 {
1398 name <- [field]Index[0]
1399 out <- _Method to Types[_Method to Types[dict, [name]Append[" >>"], type], [name]Append[" <<"], type]
1400
1401 }
1402
1403 Method to Types[dict,type:out]
1404 {
1405 out <- Fold[_Method to Types[?, ?, type], dict, [type]Methods >>]
1406 }
1407
1408 Field to Types[dict,type:out]
1409 {
1410 out <- Fold[_Field to Types[?, ?, type], dict, [type]Fields >>]
1411 }
1412
1413 _Method Dispatch[text, type, method, reg: out]
1414 {
1415 out <- [[[[[[[text]Append["\tMethodDispatch("]]Append[ [reg]Type ID[type] ]]Append[","]]Append[Escape Rhope Name[method]]]Append[","]]Append[Escape Rhope Name[type]]]Append[")\n"]
1416 }
1417
1418 Method Dispatch[text, types, method, reg: out]
1419 {
1420 out <- [[[Fold[_Method Dispatch[?, ?, method, reg], [[[text]Append["Method("]]Append[ Escape Rhope Name[method] ]]Append[")\n"], types]
1421 ]Append["EndMethod("]
1422 ]Append[Escape Rhope Name[method]]
1423 ]Append[")\n\n"]
1424 }
1425
1426 Text@C Program[program:out]
1427 {
1428 type defs <- [[program]Type Registry >>]Definitions >>
1429 constants <- Fold[Combine Consts[?], Dictionary[], [program]Functions >>]
1430 all methods <- Fold[Field to Types[?], Fold[Method to Types[?], Dictionary[], type defs], type defs]
1431 headers <- "#include <stdio.h>
1432 #include <stdlib.h>
1433 #include \"builtin.h\"
1434 #include \"object.h\"
1435 #include \"context.h\"
1436 #include \"func.h\"
1437 #include \"integer.h\"
1438 #include \"blueprint.h\"
1439 #include \"array.h\"
1440 #include \"worker.h\"
1441 #include \"bool.h\"\n\n"
1442 out <- [[[[[[[[[[[[[[[headers
1443 ]Append[[program]Dispatch[all methods]]
1444 ]Append[[[program]Type Registry >>]Type Defs]
1445 ]Append[Fold[_Consts C Program[?],
1446 Fold[_Defs C Program[?], "", [program]Functions >>],
1447 constants]]
1448 ]Append[Fold[_Text C Program[?, ?, [program]Type Registry >>], "", Filter[[program]Functions >>, Not Native[?]]]]
1449 ]Append["\n
1450 int32_t rhope(uint32_t func, object ** params, uint16_t numparams, uint16_t callspace)
1451 {
1452 uint16_t resume,idx, vcparam_offset, last_vcparam;
1453 context * ct;
1454 calldata * cdata, *temp_cdata, *my_cdata;
1455 DispatchVar
1456 FuncDef(Build)
1457 FuncDef(BlueprintSP_Of)
1458 FuncDef(ID)
1459 FuncDef(BlueprintSP_FromSP_ID)\n"]
1460 ]Append[Fold[Local Pointers[?], "", [program]Functions >>]]
1461 ]Append["
1462 ct = new_context();
1463 cdata = alloc_cdata(ct, NULL, callspace);
1464 cdata->num_params = numparams;
1465 for(idx = 0; idx < numparams; ++idx)
1466 cdata->params[idx] = params[idx];
1467 cdata->func = END;
1468 DISPATCH\n"]
1469 ]Append[Fold[Method Dispatch[?, ?, ?, [program]Type Registry >>], "", all methods]]
1470 ]Append["
1471 Func(Build,
1472 NumParams 1)
1473
1474 Param(0, TYPE_BLUEPRINT)
1475
1476 lv_Build->bp = ((t_Blueprint *)(cdata->params[0]))->bp;
1477 release_ref(cdata->params[0]);
1478
1479 Ret(0, new_object_bp(lv_Build->bp))
1480 EndFunc(Build)
1481 DISPATCH
1482
1483 Func(BlueprintSP_Of,
1484 NumParams 1)
1485
1486 lv_BlueprintSP_Of->bp = get_blueprint(cdata->params[0]);
1487 release_ref(cdata->params[0]);
1488
1489 Ret(0, new_object(TYPE_BLUEPRINT))
1490 ((t_Blueprint *)cdata->params[0])->bp = lv_BlueprintSP_Of->bp;
1491 EndFunc(BlueprintSP_Of)
1492 DISPATCH
1493
1494 Func(ID, NumParams 1)
1495
1496 Param(0, TYPE_BLUEPRINT)
1497
1498 lv_ID->id = new_object(TYPE_UINT32);
1499 ((t_UInt32 *)lv_ID->id)->Num = ((t_Blueprint *)cdata->params[0])->bp->type_id;
1500 release_ref(cdata->params[0]);
1501 Ret(0, lv_ID->id)
1502 EndFunc(ID)
1503 DISPATCH
1504
1505 Func(BlueprintSP_FromSP_ID, NumParams 1)
1506
1507 Param(0, TYPE_UINT32)
1508
1509 lv_BlueprintSP_FromSP_ID->type = ((t_UInt32 *)cdata->params[0])->Num;
1510 if (lv_BlueprintSP_FromSP_ID->type >= max_registered_type || !registered_types[lv_BlueprintSP_FromSP_ID->type]) {
1511 Ret(1, cdata->params[0])
1512 Ret(0, NULL)
1513 } else {
1514 release_ref(cdata->params[0]);
1515 Ret(0, new_object(TYPE_BLUEPRINT))
1516 ((t_Blueprint *)cdata->params[0])->bp = registered_types[lv_BlueprintSP_FromSP_ID->type];
1517 Ret(1, NULL)
1518 }
1519
1520 EndFunc(BlueprintSP_FromSP_ID)
1521 DISPATCH\n"]
1522 ]Append[Fold[_Text C Program[?, ?, [program]Type Registry >>], "", Filter[[program]Functions >>, Native[?]]]]
1523 ]Append["
1524 DO_END:
1525 for(idx = 0; idx < cdata->num_params; ++idx)
1526 params[idx] = cdata->params[idx];
1527 free_context(ct);
1528 return cdata->num_params;
1529
1530 _exception:
1531 puts(\"Exception! Trace follows:\");
1532 while(cdata && cdata->func != END)
1533 {
1534 printf(\"%d\\n\", cdata->func);
1535 cdata = cdata->lastframe;
1536 }
1537 return -1;
1538 }
1539
1540 #include \"builtin.c\"
1541 #include \"array.c\"
1542 #include \"worker.c\"
1543
1544 int main(int argc, char **argv)
1545 {
1546 blueprint * bp;
1547 int numret;
1548 int idx;
1549 object * inout[2];
1550 register_builtin_types();\n\n"]
1551 ]Append[ [[program]Type Registry >>]Type Inits[[program]Method Registry >>, [program]Field Registry >>] ]
1552 ]Append[Fold[_Set Consts C Program[?, ?, ?, [program]Type Registry >>], "", constants]]
1553 ]Append[Fold[_Set Late Consts C[?, ?, ?, [program]Type Registry >>], "", constants]]
1554 ]Append["
1555 rhope(FUNC_List, inout, 0, 1);
1556 for (idx = 0; idx < argc; ++idx)
1557 {
1558 inout[1] = make_String(argv[idx]);
1559 rhope(FUNC_Append, inout, 2, 2);
1560 }
1561 numret = rhope(FUNC_Main, inout, 1, 1);
1562 if (!numret)
1563 return 0;
1564 if (numret < 0)
1565 return numret;
1566 if (get_blueprint(inout[0])->type_id == TYPE_INT32)
1567 return ((t_Int32 *)inout[0])->Num;
1568
1569 rhope(FUNC_If, inout, 1, 2);
1570 if (inout[0])
1571 return 0;
1572 return 1;
1573 }\n\n"]
1574
1575 }
1576
1577