-
Notifications
You must be signed in to change notification settings - Fork 5
/
7.sf
772 lines (688 loc) · 28.8 KB
/
7.sf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
; #F, part 7: SFC main code generator
; Copyright (C) 2007 by Sergei Egorov, All Rights Reserved.
;
; This code is derived from the "90 minute Scheme to C compiler" presented at the
; Montreal Scheme/Lisp User Group on October 20, 2004. The original code was
; Copyright (C) 2004 by Marc Feeley, All Rights Reserved.
#fload "0.sf"
#fload "3.sf"
#fload "6.sf"
;------------------------------------------------------------------------------
; utilities
(define (rassq x al)
(cond [(null? al) #f]
[(eq? x (cdar al)) (car al)]
[else (rassq x (cdr al))]))
(define (merge-by! less? lst1 lst2)
(cond [(atom? lst1) lst2] [(atom? lst2) lst1]
[(less? (car lst2) (car lst1))
(set-cdr! lst2 (merge-by! less? lst1 (cdr lst2))) lst2]
[else (set-cdr! lst1 (merge-by! less? (cdr lst1) lst2)) lst1]))
(define (split-by! lst)
(if (atom? lst) lst
(let loop ([hd lst] [tl (cdr lst)])
(if (or (null? tl) (null? (cdr tl)))
(let ([x (cdr hd)]) (set-cdr! hd '()) x)
(loop (cdr hd) (cddr tl))))))
(define (sort-by! less? lst)
(cond [(null? lst) '()] [(null? (cdr lst)) lst]
[else (let ([lst2 (split-by! lst)])
(merge-by! less? (sort-by! less? lst) (sort-by! less? lst2)))]))
(define (sort-by less? lst)
(sort-by! less? (list-copy lst)))
(define (merge-by less? lst1 lst2)
(merge-by! less? (list-copy lst1) (list-copy lst2)))
;------------------------------------------------------------------------------
; bgc expression is a pattern begin/gvarassign/curry
; (define-variant bgc-exp (id lbid ids exp2)), virtual
(define (bgc-exp id lbid ids exp2)
(begin-exp (gvarassign-exp id (curry-exp lbid ids '())) exp2))
(define (bgc-exp? exp)
(variant-case exp
[begin-exp (exp1 exp2)
(variant-case exp1
[gvarassign-exp (id exp)
(variant-case exp
[curry-exp (id ids rands)
(null? rands)]
[else #f])]
[else #f])]
[else #f]))
(define (bgc-exp->id exp)
(gvarassign-exp->id (begin-exp->exp1 exp)))
(define (bgc-exp->lbid exp)
(curry-exp->id (gvarassign-exp->exp (begin-exp->exp1 exp))))
(define (bgc-exp->ids exp)
(curry-exp->ids (gvarassign-exp->exp (begin-exp->exp1 exp))))
(define (bgc-exp->exp2 exp)
(begin-exp->exp2 exp))
;------------------------------------------------------------------------------
; Code generator
(define (code-generate input-fix-exp)
(define nl "\n ")
(define if-nl "\n ")
(define (nl/regs-comment renv)
(or '() ; comment out for now...
(list* nl "/* live regs: "
(reduce-left
(lambda (id lst)
(cons (if (number? id)
(list "." id " ")
(list (id->uname id) " "))
lst))
(list "*/")
renv))))
(define max-live 0)
(define (alloc-reg id renv)
(set! max-live (max (+ 1 (length renv)) max-live))
(cons id renv))
(define (setlive/comment n)
;(list " SETLIVE(" n ");")
'())
(define (nl/setlive/comment n)
;(list nl "SETLIVE(" n ");")
'())
(define global-vars
(let ([ids (exp->free-vars input-fix-exp)])
; sort ids to simplify debugging
(define (id-rank id)
(string-append
(if (global-id-private-constant? id) "z" "a")
(if (var-assigned-in-exp? id input-fix-exp) "z" "a")
(symbol->string (id->symbol id))))
(define (global-id<? id1 id2)
(string<? (id-rank id1) (id-rank id2)))
(sort-by global-id<? ids)))
; global-constant-labels is defined below
; gclabelapp expression is a pattern for global-constant-labels applications
(define (gclabelapp-exp? exp)
(and (app-exp? exp)
(var-exp? (app-exp->rator exp))
(rassq (var-exp->id (app-exp->rator exp)) global-constant-labels)))
(define (gclabelapp-exp->id exp)
(var-exp->id (app-exp->rator exp)))
(define (gclabelapp-exp->rands exp)
(app-exp->rands exp))
(define (code-gen-to-nextreg exp register-env)
(define (void-tgt) #f)
(define (reg-tgt renv) (length renv))
(define (var-tgt id renv) (cons id renv))
(define (cg-tgt-rval tgt)
(cond [(not tgt) "void"]
[(integer? tgt)
(c-format "obj(r[$a])" tgt)]
[(pair? tgt)
(let ([id (car tgt)] [renv (cdr tgt)])
(cond
[(global-id? id)
(string-append "obj(" (gvar-id->c-name id) ")")]
[(cvar-id? id)
(c-format "$a($a, $a)"
(id->ctype id) (length renv) (cvar-id->c-name id))]
[else
(let ([i (- (length renv) (posq id renv) 1)])
(c-format "obj(r[$a])" i))]))]
[else
(c-error "invalid rval cg target" tgt)]))
(define (cg-tgt-lval tgt)
(cond [(not tgt) "void_from_"]
[(integer? tgt)
(c-format "r[$a] = obj_from_" tgt)]
[(pair? tgt)
(let ([id (car tgt)] [renv (cdr tgt)])
(cond
[(global-id? id)
(string-append (gvar-id->c-name id) " = obj_from_")]
[(cvar-id? id)
(c-format "$a = $a_from_"
(cvar-id->c-name id) (id->ctype id))]
[else
(let ([i (- (length renv) (posq id renv) 1)])
(c-format "r[$a] = obj_from_" i))]))]
[else
(c-error "invalid lval cg target" tgt)]))
(define (cg-prim-args exps renv cont)
(if (null? exps)
(cont "" '() renv)
(let ([rc (nl/regs-comment renv)])
(if (cg-gcsafe-cexp? (car exps))
(let ([atext (cg-cexp (car exps) renv)])
(cg-prim-args
(cdr exps)
renv
(lambda (code atexts renv)
(cont (list rc code)
(cons atext atexts) renv))))
(let ([tgt (reg-tgt renv)] [new-renv (alloc-reg 0 renv)]) ; todo: unbox??
(let ([acode (cg-to tgt (car exps) renv)]
[atext (cg-tgt-rval tgt)])
(cg-prim-args
(cdr exps)
new-renv
(lambda (code atexts renv)
(cont (list rc acode code)
(cons atext atexts) renv)))))))))
(define (cvar-id->c-name id)
(c-mangle (c-undecorate-alvar (id->symbol id))
(string-append "v" (number->string (id->ts id)) "_")))
(define (gvar-id->c-name id)
(c-mangle (symbol->string (id->symbol id)) "cx_"))
(define (cg-let-list exps vars renv cont)
(cond [(null? exps) (cont "" renv)]
[(cvar-id? (car vars))
(let ([id (car vars)])
(let ([acode (cg-to (var-tgt id renv) (car exps) renv)]
[cvid (cvar-id->c-name id)])
(if (and (pair? acode)
(equal? (car acode) nl) (pair? (cdr acode)) (string? (cadr acode))
(string-starts-with? (cadr acode) (string-append cvid " = ")))
(list
nl "{ const " (id->ctype id) "_t " (cadr acode)
(cg-let-list (cdr exps) (cdr vars) renv cont)
" } ")
(list
nl "{ " (id->ctype id) "_t " (cvar-id->c-name id) ";"
(cg-let-list (cdr exps) (cdr vars)
renv
(lambda (code renv)
(cont (list acode code) renv)))
" }"))))]
[else
(let ([rc (nl/regs-comment renv)]
[acode (cg-to (reg-tgt renv) (car exps) renv)])
(cg-let-list (cdr exps) (cdr vars)
(alloc-reg (car vars) renv)
(lambda (code renv)
(cont (list rc acode code) renv))))]))
(define (cg-depends-on? exp spoiled-ids)
(define (dep? exp)
(variant-case exp
[var-exp (id) (memq id spoiled-ids)]
[gvarassign-exp (id exp) (dep? exp)]
[if-exp (test-exp then-exp else-exp)
(orapp dep? test-exp then-exp else-exp)]
[let-exp (ids rands body)
(or (ormap dep? rands) (dep? body))]
[curry-exp (id ids rands)
(ormap dep? rands)]
[labelapp-exp (id rands)
(ormap dep? rands)]
[app-exp (rator rands)
(or (dep? rator) (ormap dep? rands))]
[primapp-exp (effect prim rands)
(ormap dep? rands)]))
(dep? exp))
(define (cg-fast-goto? renv rands)
; check if rands depend on first (length rands) regs
(let loop ([rands rands] [regs (reverse renv)] [spoiled '()])
(cond [(null? rands) #t]
[(null? regs) #f] ; may step on let-bound reg
[(number? (car regs)) #f] ; may step on temporary
[(or (null? spoiled) (not (cg-depends-on? (car rands) spoiled)))
(loop (cdr rands) (cdr regs) (cons (car regs) spoiled))]
[else #f])))
(define (cg-complex-to tgt exp renv)
(variant-case exp
[bgc-exp (id lbid ids exp2)
(let ([cn (lookup-curry-case-no lbid)])
(list
(nl/regs-comment renv)
nl "{ " "static obj c[] = { obj_from_case(" cn ") }; "
(gvar-id->c-name id) " = (obj)c; }" ; /* (id->uname id) */
(cg-to tgt exp2 renv)))]
[gvarassign-exp (id exp)
(list
(nl/regs-comment renv)
(cg-to (var-tgt id renv) exp renv)
(cg-to tgt (void-exp) renv))]
[if-exp (test-exp then-exp else-exp)
(list
(if (cg-cexp? test-exp) ;todo: use test target?
(list if-nl
(cleanup-c-code!
(c-format "if (bool_from_$arg) {" (cg-cexp test-exp renv))))
(let ([code (cg-to (reg-tgt renv) test-exp renv)]
[tmp-renv (alloc-reg 0 renv)])
(list code
(nl/regs-comment renv)
(nl/setlive/comment (length renv))
if-nl "if (bool_from_obj(r[" (length renv) "])) {")))
(cg-to tgt then-exp renv)
if-nl "} else {"
(cg-to tgt else-exp renv)
if-nl "}")]
[degenerate-let-exp (body)
(cg-to tgt body renv)]
[begin-exp (exp1 exp2)
(list
(nl/regs-comment renv)
(cg-to (void-tgt) exp1 renv)
(cg-to tgt exp2 renv))]
[let-exp (ids rands body)
(cg-let-list rands ids renv
(lambda (code new-renv)
(list code (cg-to tgt body new-renv))))]))
(define (cg-application? exp)
(cond [(let-exp? exp) #f]
[(curry-exp? exp) #t]
[(labelapp-exp? exp) #t]
[(app-exp? exp) #t]
[(primapp-exp? exp) #t]
[else #f]))
(define (cg-primapp-to tgt exp renv)
(variant-case exp
[primapp-exp (effect prim rands)
(set! max-live (max (+ (length renv) (length rands)) max-live))
(cg-prim-args rands renv
(lambda (code args new-renv)
(let ([ltext (length new-renv)] [rtext (cg-tgt-lval tgt)])
(list code (nl/regs-comment new-renv) nl
(cleanup-c-code!
; bug fix in 160m
; was just (c-format-prim* ltext rtext prim args)
(if (prim-cexp? prim) ; C expression?
(c-format (string-append rtext "$arg;")
(c-format-primexp* ltext prim args))
(c-format-prim* ltext rtext prim args)))))))]))
(define (cg-application-to tgt exp renv)
(variant-case exp
[primapp-exp (effect prim rands)
(cg-primapp-to tgt exp renv)]
[curry-exp (id ids rands)
; let's make primitive on the fly
(let ([cn (lookup-curry-case-no id)])
(let ([prim (string-append
"hreserve(hbsz($argc+1), $live); /* $live live regs */\n"
" ${*--hp = obj_from_$arg;\n"
" $}*--hp = obj_from_case(" (number->string cn) ");\n"
" $return obj(hendblk($argc+1));")])
(cg-primapp-to tgt
(primapp-exp (*-effect) prim (reverse rands))
renv)))]
[labelapp-exp (id rands)
; let's make primitives on the fly
(if (cg-fast-goto? renv rands)
(let ([lname (lookup-label id #t)])
(let ([prim (string-append
"${r[$arg#] = obj_from_$arg;\n"
" $}goto " lname ";")])
(cg-primapp-to tgt
(primapp-exp (*?!-effect) prim rands)
renv)))
(let ([lname (lookup-label id #t)])
(let ([prim (string-append
"${r[$live+$arg#] = obj_from_$arg;\n"
" $}r += $live; /* shift reg wnd */\n"
" rreserve(MAX_HOSTREGS);\n"
" goto " lname ";")])
(cg-primapp-to tgt
(primapp-exp (*?!-effect) prim rands)
renv))))]
[gclabelapp-exp (id rands)
; let's make primitives on the fly
(if (cg-fast-goto? renv rands)
(let ([lname (make-gclabel id)])
(let ([prim (string-append
"${r[$arg#] = obj_from_$arg;\n"
" $}goto " lname ";")])
(cg-primapp-to tgt
(primapp-exp (*?!-effect) prim rands)
renv)))
(let ([lname (make-gclabel id)])
(let ([prim (string-append
"${r[$live+$arg#] = obj_from_$arg;\n"
" $}r += $live; /* shift reg wnd */\n"
" rreserve(MAX_HOSTREGS);\n"
" goto " lname ";")])
(cg-primapp-to tgt
(primapp-exp (*?!-effect) prim rands)
renv))))]
[app-exp (rator rands)
; let's make primitives on the fly
(if (cg-fast-goto? renv (cons rator rands))
(let ([prim (string-append
"r[0] = obj_from_$arg;\n"
" pc = objptr_from_obj(r[0])[0];\n"
" ${r[$arg#] = obj_from_$arg;\n"
" $}rreserve(MAX_HOSTREGS);\n"
" rc = $arg#;\n"
" goto jump;")])
(cg-primapp-to tgt
(primapp-exp (*?!-effect) prim (cons rator rands))
renv))
(let ([prim (string-append
"r[$live+0] = obj_from_$arg;\n"
" pc = objptr_from_obj(r[$live+0])[0];\n"
" ${r[$live+$arg#] = obj_from_$arg;\n"
" $}r += $live; /* shift reg wnd */\n"
" rreserve(MAX_HOSTREGS);\n"
" rc = $arg#;\n"
" goto jump;")])
(cg-primapp-to tgt
(primapp-exp (*?!-effect) prim (cons rator rands))
renv)))]))
(define cg-returns?
(memoizate-exp-attribute
(lambda (exp)
(variant-case exp
[var-exp (id) #t]
[gvarassign-exp (id exp) #t]
[if-exp (test-exp then-exp else-exp)
(or (cg-returns? then-exp) (cg-returns? else-exp))]
[let-exp (ids rands body)
(cg-returns? body)]
[curry-exp (id ids rands) #t]
[halt-exp (exp) #f] ;unusual primapp
[primapp-exp (effect prim rands) #t]
[else #f]))))
(define cg-gcsafe-cexp?
(memoizate-exp-attribute
(lambda (exp)
(variant-case exp
[var-exp (id) #t]
[gvarassign-exp (id exp) #f] ; why not? r/w ordering?
[if-exp (test-exp then-exp else-exp)
(andapp cg-gcsafe-cexp? test-exp then-exp else-exp)]
[let-exp (ids rands body) #f] ; can't make let into C expr
[curry-exp (id ids rands) #f] ; not gc-safe
[labelapp-exp (id rands) #f]
[app-exp (rator rands) #f]
[primapp-exp (effect prim rands)
(and (prim-cexp? prim) (gc-safe-effect? effect)
(andmap cg-gcsafe-cexp? rands))]))))
(define cg-cexp?
(memoizate-exp-attribute
(lambda (exp)
(variant-case exp
[var-exp (id) #t]
[gvarassign-exp (id exp) #f] ; why not? r/w ordering?
[if-exp (test-exp then-exp else-exp)
(andapp cg-cexp? test-exp then-exp else-exp)]
[let-exp (ids rands body) #f] ; can't make let into C expr
[curry-exp (id ids rands) #f] ; not gc-safe
[labelapp-exp (id rands) #f]
[app-exp (rator rands) #f]
[primapp-exp (effect prim rands)
(and (prim-cexp? prim) (andmap cg-cexp? rands))]))))
(define (cg-cexp exp renv)
(variant-case exp
[var-exp (id)
(if (label-id? id) (c-error "escaping label???" id))
(cg-tgt-rval (var-tgt id renv))]
; no gvarassign-exp
[if-exp (test-exp then-exp else-exp)
; do not box the results unless really needed to !!!
(let ([test-code (cg-cexp test-exp renv)]
[then-code (cg-cexp then-exp renv)]
[else-code (cg-cexp else-exp renv)])
(let ([then-ctype (primexp-ctype then-code)]
[else-ctype (primexp-ctype else-code)])
(let ([if-ctype (if (and then-ctype else-ctype
(string=? then-ctype else-ctype))
then-ctype
"obj")])
(cond
[(and (string=? if-ctype "bool") (string=? else-code "bool(0)"))
(c-format
(string-append if-ctype "(bool_from_$arg && "
if-ctype "_from_$arg)")
test-code then-code)]
[(and (string=? if-ctype "bool") (string=? test-code then-code))
(c-format
(string-append if-ctype "(bool_from_$arg || "
if-ctype "_from_$arg)")
test-code else-code)]
[else
(c-format
(string-append if-ctype "(bool_from_$arg ? "
if-ctype "_from_$arg : "
if-ctype "_from_$arg)")
test-code then-code else-code)]))))]
; no let-exp
; no curry-exp
; no labelapp-exp
; no app-exp
[primapp-exp (effect prim rands) ; expression
(c-format-primexp* (length renv) prim
(map (lambda (rand) (cg-cexp rand renv))
rands))]))
(define (cg-cexp-to tgt exp renv)
(let ([code (cleanup-c-code!
(c-format
(string-append (cg-tgt-lval tgt) "$arg;")
(cg-cexp exp renv)))])
(cond ; more cleanup in b164
[(equal? code "(void)(0);") '()]
[else (list nl code)])))
(define (cg-to tgt exp renv)
(cond [(cg-cexp? exp) (cg-cexp-to tgt exp renv)]
[(cg-application? exp) (cg-application-to tgt exp renv)]
[else (cg-complex-to tgt exp renv)]))
(cg-to (reg-tgt register-env) exp register-env))
(define label-alist '()) ; ((id . "label") ...)
(define (assign-labels! ids)
(define (id->l id)
(let ([l (c-mangle (c-undecorate-alvar (id->symbol id)) "s_")])
(if (ormap (lambda (id&l) (string=? l (cdr id&l))) label-alist)
(string-append l "_v" (number->string (id->ts id)))
l)))
(let loop ([ids ids])
(if (null? ids) #t
(begin
(set! label-alist
(cons (cons (car ids) (id->l (car ids)))
label-alist))
(loop (cdr ids)))))
(set! label-alist (reverse label-alist)))
(define used-labels '()) ; (id ...)
(define (lookup-label id ref?) ; => label name
(if ref? (set! used-labels (adjoinq id used-labels)))
(cond [(assq id label-alist) => cdr]
[else (c-error "can't find a label for" id)]))
(define static-roots '()) ; ((id . lbid) ...)
(define global-labels '()) ; ((lbid . id) ...)
(define global-constant-labels '()) ; ((lbid . id) ...)
(define (add-static-global-label! id lbid)
(set! static-roots (cons (cons id lbid) static-roots))
(set! global-labels (cons (cons lbid id) global-labels))
(if (global-id-constant? id)
(set! global-constant-labels (cons (cons lbid id) global-constant-labels))))
(define used-global-labels '()) ; (id ...)
(define (add-global-label-use! id)
(set! used-global-labels (adjoinq id used-global-labels)))
(define (make-gclabel id)
(c-mangle (symbol->string (id->symbol id)) "gs_"))
(define curry-alist '()) ; ((id ids . rands) ...)
(define (add-curry! id ids rands)
(cond [(assq id curry-alist) =>
(lambda (id&ids&rands)
(if (not (= (length ids) (length (cadr id&ids&rands))))
(c-error "curry ids mismatch??" id ids id&ids&rands))
(if (not (= (length rands) (length (cddr id&ids&rands))))
(c-error "curry args mismatch??" id ids id&ids&rands)))]
[else (set! curry-alist (cons (list* id ids rands) curry-alist))]))
(define goto-alist '()) ; ((id . rands) ...)
(define (add-goto! id rands)
(lookup-label id #t) ;b164 fix: register id as goto target early!
(cond [(assq id goto-alist) =>
(lambda (id&rands)
(if (not (= (length rands) (length (cdr id&rands))))
(c-error "goto args mismatch??" id id&rands)))]
[else (set! goto-alist (cons (cons id rands) goto-alist))]))
(define (collect-curries! lams body)
(define (cc exp)
(variant-case exp
[bgc-exp (id lbid ids exp2)
(add-curry! lbid ids '())
(add-static-global-label! id lbid)
(cc exp2)]
[var-exp (id) 'ok]
[gvarassign-exp (id exp)
(cc exp)]
[if-exp (test-exp then-exp else-exp)
(cc test-exp) (cc then-exp) (cc else-exp)]
[let-exp (ids rands body)
(for-each cc rands) (cc body)]
[curry-exp (id ids rands)
(add-curry! id ids rands)
(for-each cc rands)]
[labelapp-exp (id rands)
(add-goto! id rands)
(for-each cc rands)]
[app-exp (rator rands)
(if (and (var-exp? rator) (global-id-constant? (var-exp->id rator)))
(add-global-label-use! (var-exp->id rator)))
(cc rator) (for-each cc rands)]
[primapp-exp (effect prim rands)
(for-each cc rands)]))
(for-each
(lambda (lam) (cc (lambda-exp->body lam)))
lams)
(cc body))
(define (sort-curries-by-fix-order! ids)
(define (c-before? c1 c2)
(< (posq (car c1) ids) (posq (car c2) ids)))
(set! curry-alist
(sort-by! c-before? curry-alist)))
(define curry-case-no-start 1) ; 0 reserved for entry
(define (lookup-curry-case-no id)
(let loop ([alst curry-alist] [i curry-case-no-start])
(cond [(null? alst) (c-error "no curry for" id)]
[(eq? (caar alst) id) i]
[else (loop (cdr alst) (+ i 1))])))
(define (compile-body exp)
(list
"case " 0 ": /* load module */"
(code-gen-to-nextreg exp '())
"\n\n"))
(define (compile-curry-case id)
(define (cmove ids rands)
(define nlive (+ (length ids) 1))
(define ncopy (length rands))
(define (cm ito ifrom)
(list "\n r[1+" (- ito 1) "] = p[" ifrom "];"))
(if (> ncopy 0)
(list
" { obj* p = objptr_from_obj(r[0]);"
(map cm
(iota nlive (+ nlive ncopy))
(iota 1 (+ 1 ncopy)))
" }\n")
'()))
(define (ccode i id&ids&rands)
(list
(list "case " i ":")
" /* "
(cond [(assq id global-labels) =>
(lambda (p) (id->symbol (cdr p)))] ; Fixme: symbol may contain '*/'!
[else "clo"])
" "
(map (lambda (id) (list (c-undecorate-alvar (id->symbol id)) " "))
(cadr id&ids&rands))
"*/\n"
(if (and (list2? (cadr id&ids&rands))
(eq? (id->symbol (cadadr id&ids&rands)) begin-id-symbol))
(list " assert(rc >= 2);\n" " r[2] = obj_from_void(0); /* ignored */\n")
(list " assert(rc == " (+ 1 (length (cadr id&ids&rands))) ");\n"))
(cmove (cadr id&ids&rands) (cddr id&ids&rands))
" r += 1; /* shift reg. wnd */\n"))
(let loop ([alst curry-alist] [i curry-case-no-start])
(cond [(null? alst) '()] ; no curry case for this id
[(eq? (caar alst) id) (ccode i (car alst))]
[else (loop (cdr alst) (+ i 1))])))
(define (compile-lam lam)
(code-gen-to-nextreg (lambda-exp->body lam)
(reverse (lambda-exp->ids lam))))
(define (compile-label id lam)
(let* ([p (assq id global-constant-labels)]
[ltxt (if (and p (memq (cdr p) used-global-labels))
(list (make-gclabel (cdr p)) ": ")
'())]
[ltxt (if (memq id used-labels)
(list ltxt (if (null? ltxt) '() "\n")
(lookup-label id #f) ": ")
ltxt)])
(list
(compile-curry-case id)
(if (null? ltxt) " " ltxt)
"/* "
(map (lambda (id) (list (c-undecorate-alvar (id->symbol id)) " "))
(lambda-exp->ids lam))
"*/"
(compile-lam lam)
"\n\n")))
(define (compile-labels ids lams body)
(assign-labels! ids)
(collect-curries! lams body)
(sort-curries-by-fix-order! ids)
(let ([bc (compile-body body)]) ; 160m: do body first!
(let loop ([ids ids] [lams lams] [code '()])
(if (null? ids)
(list bc (reverse code))
(let ([c (compile-label (car ids) (car lams))])
(loop (cdr ids) (cdr lams) (cons c code)))))))
(define (compile-globals)
(define (cglob id)
(define name (symbol->string (id->symbol id)))
(if (var-assigned-in-exp? id input-fix-exp)
(if (global-id-private-constant? id)
(c-format "static obj $a; /* constant $a */\n" (c-mangle name "cx_") (id->symbol id))
(c-format "obj $a; /* $a */\n" (c-mangle name "cx_") (id->symbol id)))
(c-format "extern obj $a; /* $a */\n" (c-mangle name "cx_") (id->symbol id))))
(list (map cglob global-vars) "\n"))
(define (main-module?)
(ormap (lambda (id)
(and (eq? (id->symbol id) 'main)
(var-assigned-in-exp? id input-fix-exp)))
global-vars))
(define (compile-roots)
(define (cglob id)
(c-format " &$a,\n"
(c-mangle (symbol->string (id->symbol id)) "cx_")))
(define live-roots
(keep (lambda (id)
(let ([ac (var-assignment-count id input-fix-exp)])
(cond [(zero? ac) #f] ; ignore externs
[(not (assq id static-roots)) #t] ; keep non-statics
[(> ac 1) #t] ; keep true variables (fix for issue 5)
[else #f]))) ; ignore the rest (single-assigned statics)
global-vars))
(if (null? live-roots)
(list "static cxroot_t root = { 0, NULL, NULL };\n\n")
(list "static obj *globv[] = {\n"
(map cglob live-roots)
"};\n\n"
"static cxroot_t root = {\n"
" sizeof(globv)/sizeof(obj *), globv, NULL\n"
"};\n\n")))
(define (compile-cases)
(define ncases (+ curry-case-no-start (length curry-alist)))
(list "static obj host(obj);\n"
"static obj cases[" ncases "] = {"
(let loop ([i 0] [code '()])
(cond [(= i ncases) (reverse code)]
[(zero? (remainder i 5))
(loop (+ i 1) (cons "\n (obj)host," code))]
[else
(loop (+ i 1) (cons " (obj)host," code))]))
"\n};\n"))
; => (main? liveregs globals roots cases code)
(variant-case input-fix-exp
[fix-exp (ids lams body)
; compile-labels should be done first
(let ([code (compile-labels ids lams body)])
; the remaining parts can be done in any order
(let ([globals (compile-globals)])
(let ([roots (compile-roots)])
(let ([cases (compile-cases)])
(list
(main-module?)
(list
; add 2 to max-live: one for topmost "output" reg
; (not in reg-layout), another for extra arg in trampolines
; then multiply by 2 to cover caller's + callee's needs
"#define MAX_HOSTREGS " (* 2 (+ 2 max-live)) "\n")
globals
roots
cases
code)))))]))