-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathctx.scm
2186 lines (1939 loc) · 80.9 KB
/
ctx.scm
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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;---------------------------------------------------------------------------
;;
;; Copyright (c) 2015, Baptiste Saleil. All rights reserved.
;;
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions are
;; met:
;; 1. Redistributions of source code must retain the above copyright
;; notice, this list of conditions and the following disclaimer.
;; 2. Redistributions in binary form must reproduce the above copyright
;; notice, this list of conditions and the following disclaimer in the
;; documentation and/or other materials provided with the distribution.
;; 3. The name of the author may not be used to endorse or promote
;; products derived from this software without specific prior written
;; permission.
;;
;; THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
;; WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
;; MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
;; NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
;; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
;; NOT LIMITED TO PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
;; THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;
;;---------------------------------------------------------------------------
(include "config.scm")
(define regalloc-regs #f)
(define regalloc-fregs #f)
(define lazy-code-flags #f)
(define mem-allocated-kind #f)
(define live-out? #f)
(define copy-permanent #f)
(define perm-domain #f)
(define opt-entry-points #f)
(define opt-const-vers #f)
(define const-versioned? #f)
(define opt-propagate-continuation #f)
(define opt-static-mode #f)
(define opt-float-unboxing #f)
(define opt-int-unboxing #f)
(define opt-free-versioning #f)
(define asc-cnnum-ctx-get #f)
(define asc-cnnum-lco-get #f)
(define asc-fnnum-ctx-get #f)
(define asc-fnnum-lco-get #f)
(define asc-fnnum-nbargs-get #f)
(define gen-version-first #f)
(define sort #f)
;;-----------------------------------------------------------------------------
;; Ctx
;; Compilation context
(define-type ctx
constructor: make-ctx*
stack ;; virtual stack of types
slot-loc ;; alist which associates a virtual stack slot to a location
free-regs ;; list of current free virtual registers
free-mems ;; list of current free memory slots
free-fregs ;; list of current free virtual float registers
free-fmems ;; list of current free virtual float memory slots
env ;; alist which associates a variable symbol to an identifier object
nb-actual ;;
nb-args ;; number of arguments of function of the current stack frame
fs ;; current frame size
ffs ;; curent float stack frame size
fn-num ;; fn-num of current function
)
;; check for alpha conversion:
;; Check that every id is unique when creating a ctx
(define (contains-dbl ids)
(if (null? ids)
#f
(if (member (car ids) (cdr ids))
(car ids)
(contains-dbl (cdr ids)))))
(define (make-ctx stack slot-loc free-regs free-mems free-fregs free-fmems env nb-actual nb-args fs ffs fn-num)
;; Check alpha conversion
;; All operations on ctx must create a ctx with distinct variable names
(assert
(let* ((local-ids (map car env))
(r (contains-dbl local-ids)))
(or (not r)
(begin (println "WIP: alpha conversion needed for " r)
(pp local-ids)
#f)))
"Internal error")
(assert
(let* ((used-fregs
(foldr (lambda (sl r)
(if (and (cdr sl) (ctx-loc-is-fregister? (cdr sl)))
(cons (cdr sl) r)
r))
'()
slot-loc))
(total (set-union used-fregs free-fregs)))
(if (not (= (length total)
(length (ctx-init-free-fregs))))
(begin
(pp (make-ctx* stack slot-loc free-regs free-mems free-fregs free-fmems env nb-actual nb-args fs ffs fn-num))
(error "CHECK FAILED")
#f)
#t))
"Internal error")
(let ((free-regs (sort free-regs (lambda (a b) (< (cdr a) (cdr b)))))
(free-mems (sort free-mems (lambda (a b) (< (cdr a) (cdr b)))))
(free-fregs (sort free-fregs (lambda (a b) (< (cdr a) (cdr b)))))
(free-fmems (sort free-fmems (lambda (a b) (< (cdr a) (cdr b))))))
(make-ctx* stack slot-loc free-regs free-mems free-fregs free-fmems env nb-actual nb-args fs ffs fn-num)))
(define (ctx-copy ctx #!optional stack slot-loc free-regs free-mems free-fregs free-fmems env nb-actual nb-args fs ffs fn-num)
(make-ctx
(or stack (ctx-stack ctx))
(or slot-loc (ctx-slot-loc ctx))
(or free-regs (ctx-free-regs ctx))
(or free-mems (ctx-free-mems ctx))
(or free-fregs (ctx-free-fregs ctx))
(or free-fmems (ctx-free-fmems ctx))
(or env (ctx-env ctx))
(or nb-actual (ctx-nb-actual ctx))
(or nb-args (ctx-nb-args ctx))
(or fs (ctx-fs ctx))
(or ffs (ctx-ffs ctx))
(or fn-num (ctx-fn-num ctx))))
;; Return ctx that only contains regalloc information
(define (ctx-rm-regalloc ctx)
(ctx-copy ctx #f 0 0 0 #f #f #f #f #f 0 0))
;; Generate initial free regs list
(define (ctx-init-free-regs)
(build-list (length regalloc-regs) (lambda (i) (cons 'r i))))
;; Generate initial free fregs list
(define (ctx-init-free-fregs)
(build-list (length regalloc-fregs) (lambda (i) (cons 'fr i))))
;; Create an empty context
(define (ctx-init)
(make-ctx '()
'()
(ctx-init-free-regs)
'()
(ctx-init-free-fregs)
'()
'()
#f
-1
0
0
#f))
;;---------------------------------------------------------------------------
;; META CTX TYPES
;; List of all registered tags
(define meta-tags '())
;; Table type symbol -> tags associated to this type
(define meta-type-tags (make-table))
;; Table type symbol -> dynamic tags associated to this type
(define meta-dynamic-type-tags (make-table))
;; Set the value of meta-(dynamic)-type-tags
(define (meta-type-tags-set table type-name tags)
(assert (not (table-ref table type-name #f))
"Internal error")
(table-set! table type-name tags))
;; Ctx type utils
(define (ctx-make-type sym . dynamic-tags) (cons sym dynamic-tags))
(define (ctx-type-symbol type) (car type))
(define (ctx-type-dtags type) (cdr type))
(define (ctx-type-tag type i) (list-ref type (+ i 1)))
(define (ctx-type-tag-set! type i val)
(let loop ((i i) (lst (cdr type)))
(if (= i 0)
(set-car! lst val)
(loop (- i 1) (cdr lst)))))
;; Generic type predicate
;; check if given tag exists in the set of tags associated to given type
(define (ctx-type-predicate tag)
(lambda (type)
(member tag (table-ref meta-type-tags (ctx-type-symbol type)))))
;; Generic type tag accessor (get)
;; check that given dynamic tag exists in the set of dynamic tags associated to given type
;; then, return the value associated to the tag from the type object
(define (ctx-type-accessor tag)
(lambda (type)
(let* ((dtags (table-ref meta-dynamic-type-tags (ctx-type-symbol type)))
(idx (index-of tag dtags)))
(assert idx "Internal error")
(ctx-type-tag type idx))))
;; Generic type tag accessor (set)
;; check that given dynamic tag exists in the set of dynamic tags associated to given type
;; then, replace the value associated to the tag from the type object
(define (ctx-type-accessor! tag)
(lambda (type val)
(let* ((dtags (table-ref meta-dynamic-type-tags (ctx-type-symbol type)))
(idx (index-of tag dtags)))
(assert idx "Internal error")
(ctx-type-tag-set! type idx val))))
;;---------------------------------------------------------------------------
;; Register type tags
(define-macro (meta-add-tags . tags)
;; Example with the call (meta-add-tags (cst)):
;; (define ctx-type-cst? (ctx-type-predicate 'cst))
;; (define ctx-type-cst (ctx-type-accessor 'cst))
;; (set! meta-tags '(cst))
;; pref is a str prefix, suf is a list of str suffix
;; Add prefix and suffix to the symbol 'sym' and return the new symbol
(define (format-sym pref sym . suf)
(let* ((sym-str (symbol->string sym))
(str (string-append pref sym-str (apply string-append suf))))
(string->symbol str)))
;; Generate predicate and accessor for each tag
(define (generate-pred-accs tags)
(if (null? tags)
'()
(let ((sym-pred (format-sym "ctx-type-" (car tags) "?"))
(sym-accs (format-sym "ctx-type-" (car tags)))
(sym-accss (format-sym "ctx-type-" (car tags) "-set!")))
(append `((define ,sym-pred (ctx-type-predicate ',(car tags)))
(define ,sym-accs (ctx-type-accessor ',(car tags)))
(define ,sym-accss (ctx-type-accessor! ',(car tags))))
(generate-pred-accs (cdr tags))))))
`(begin
,@(generate-pred-accs tags)
(set! meta-tags ',tags)))
;;---------------------------------------------------------------------------
;; Register a new type with a name, a list of tags and a list of dynamic (instantiated tags)
(define-macro (meta-add-type name tags dynamic-tags)
;; Example with the call (meta-add-type chac (cha cst) (cst)):
;; (assert (null? (keep (lambda (el) (not (member el meta-tags))) '(cha cst))) "Internal error")
;; (assert (null? (keep (lambda (el) (not (member el '(cha cst)))) '(cst))) "Internal error")
;; (meta-type-tags-set meta-type-tags 'chac '(cha cst))
;; (meta-type-tags-set meta-dynamic-type-tags 'chac '(cst))
;; (define make-ctx-tchac (lambda (cst) (ctx-make-type 'chac cst)))
;; pref is a str prefix, suf is a list of str suffix
;; Add prefix and suffix to the symbol 'sym' and return the new symbol
(define (format-sym pref sym . suf)
(let* ((sym-str (symbol->string sym))
(str (string-append pref sym-str (apply string-append suf))))
(string->symbol str)))
;; Generate various assertions
(define (generate-asserts)
`(;; Check tags exist
(assert (null? (keep (lambda (el) (not (member el meta-tags))) ',tags))
"Internal error")
;; Check dynamic tags exist for this type
(assert (null? (keep (lambda (el) (not (member el ',tags))) ',dynamic-tags))
"Internal error")))
;; Generate constructor
(define (generate-constructor)
(let ((sym (format-sym "make-ctx-t" name)))
`(define (,sym ,@dynamic-tags) (ctx-make-type ',name ,@dynamic-tags))))
`(begin
;; generate assertions
,@(generate-asserts)
;; add static tags
(meta-type-tags-set meta-type-tags ',name ',tags)
(meta-type-tags-set meta-dynamic-type-tags ',name ',dynamic-tags)
;; generate constructor
,(generate-constructor)))
;;---------------------------------------------------------------------------
(meta-add-tags
;; Type tags
unk cha voi nul ret int boo box pai vec fec str sym ipo flo opo clo
;; Other tags
cst id)
(meta-add-type unk (unk) ())
(meta-add-type cha (cha) ())
(meta-add-type voi (voi) ())
(meta-add-type nul (nul) ())
(meta-add-type ret (ret) ())
(meta-add-type int (int) ())
(meta-add-type boo (boo) ())
(meta-add-type box (box) ())
(meta-add-type pai (pai) ())
(meta-add-type vec (vec) ())
(meta-add-type fec (fec) ())
(meta-add-type str (str) ())
(meta-add-type sym (sym) ())
(meta-add-type ipo (ipo) ())
(meta-add-type flo (flo) ())
(meta-add-type opo (opo) ())
(meta-add-type clo (clo) ())
(meta-add-type chac (cha cst) (cst))
(meta-add-type nulc (nul cst) (cst))
(meta-add-type intc (int cst) (cst))
(meta-add-type booc (boo cst) (cst))
(meta-add-type paic (pai cst) (cst))
(meta-add-type vecc (vec cst) (cst))
(meta-add-type fecc (fec cst) (cst))
(meta-add-type strc (str cst) (cst))
(meta-add-type symc (sym cst) (cst))
(meta-add-type floc (flo cst) (cst))
(meta-add-type cloc (clo cst) (cst))
(meta-add-type retc (ret cst) (cst))
(meta-add-type cloi (clo cst id) (cst))
(define (ctx-type-mem-allocated? type)
(or (ctx-type-box? type)
(ctx-type-pai? type)
(ctx-type-vec? type)
(ctx-type-fec? type)
(ctx-type-str? type)
(ctx-type-sym? type)
(ctx-type-ipo? type)
(ctx-type-flo? type)
(ctx-type-opo? type)
(ctx-type-clo? type)))
(define (ctx-string->tpred str)
(define (is s) (string=? s str))
(cond
((is "cha") ctx-type-cha?)
((is "voi") ctx-type-voi?)
((is "nul") ctx-type-nul?)
((is "int") ctx-type-int?)
((is "boo") ctx-type-boo?)
((is "vec") ctx-type-vec?)
((is "fec") ctx-type-fec?)
((is "str") ctx-type-str?)
((is "sym") ctx-type-sym?)
((is "flo") ctx-type-flo?)
((is "pai") ctx-type-pai?)
((is "clo") ctx-type-clo?)
(else (error "Internal error"))))
(define (ctx-type-ctor type)
(cond
((ctx-type-cha? type) make-ctx-tcha)
((ctx-type-voi? type) make-ctx-tvoi)
((ctx-type-nul? type) make-ctx-tnul)
((ctx-type-int? type) make-ctx-tint)
((ctx-type-boo? type) make-ctx-tboo)
((ctx-type-vec? type) make-ctx-tvec)
((ctx-type-fec? type) make-ctx-tfec)
((ctx-type-str? type) make-ctx-tstr)
((ctx-type-sym? type) make-ctx-tsym)
((ctx-type-flo? type) make-ctx-tflo)
((ctx-type-pai? type) make-ctx-tpai)
((ctx-type-clo? type) make-ctx-tclo)
((ctx-type-ret? type) make-ctx-tret)
(else (error "Internal error"))))
;; Check if two ctx-type objects represent the same type
(define (ctx-type-teq? t1 t2)
(eq? (ctx-type-symbol t1)
(ctx-type-symbol t2)))
;; Check if t1 'is-a' t2
;; (t1 has the tag associated to t2)
(define (ctx-type-is-a? t1 t2)
(let ((pred (ctx-type-predicate (ctx-type-symbol t2))))
(pred t1)))
;; Return a new type instance without any constant information
(define (ctx-type-nocst t)
(if (ctx-type-cst? t)
(let ((ctor (ctx-type-ctor t)))
(ctor))
t))
;; Build and return a ctx type from a literal
(define (literal->ctx-type l)
(if (##bignum? l)
(set! l (exact->inexact l)))
(if (and (##mem-allocated? l)
(not (eq? (mem-allocated-kind l) 'PERM)))
(set! l (copy-permanent l #f perm-domain)))
(cond
((char? l) (make-ctx-tchac l))
((null? l) (make-ctx-tnulc l))
((fixnum? l) (make-ctx-tintc l))
((boolean? l) (make-ctx-tbooc l))
((pair? l) (make-ctx-tpaic l))
((vector? l) (make-ctx-tvecc l))
((f64vector? l) (make-ctx-tfecc l))
((string? l) (make-ctx-tstrc l))
((symbol? l) (make-ctx-tsymc l))
((flonum? l) (make-ctx-tfloc l))
(else (pp l) (error "Internal error (literal->ctx-type)"))))
;; CTX IDENTIFIER LOC
;; Return best loc for identifier. (Register if available, memory otherwise)
(define (ctx-identifier-loc ctx identifier)
(define (get-best-loc slot-loc sslots mloc)
(if (null? slot-loc)
mloc
(let ((sl (car slot-loc)))
(if (member (car sl) sslots)
(if (ctx-loc-is-register? (cdr sl))
(cdr sl)
(get-best-loc (cdr slot-loc) sslots (cdr sl)))
(get-best-loc (cdr slot-loc) sslots mloc)))))
(let ((sslots (identifier-sslots identifier)))
(if (null? sslots)
;; Free var
(begin
(assert (eq? (identifier-kind identifier) 'free) "Internal error (ctx-identifier-loc) 2")
(identifier-cloc identifier))
(get-best-loc (ctx-slot-loc ctx) sslots #f))))
;;
(define (ctx-fs-inc ctx)
(ctx-copy ctx #f #f #f #f #f #f #f #f #f (+ (ctx-fs ctx) 1)))
(define (ctx-fs-update ctx fs)
(ctx-copy ctx #f #f #f #f #f #f #f #f #f fs))
(define (ctx-reset-nb-actual ctx)
(ctx-copy ctx #f #f #f #f #f #f #f (ctx-nb-args ctx)))
;; Init a stack for a call ctx or a fn ctx
;; This function removes the csts not used for versioning from 'stack'
(define (ctx-init-stack stack nels add-suffix?)
(let ((nstack
(let loop ((stack stack) (nels nels))
(if (or (null? stack)
(and (number? nels) (= nels 0)))
'()
(let ((type (car stack)))
(if (and (ctx-type-cst? type)
(not (const-versioned? type)))
;; it's a non versioned cst, remove it
(cons (ctx-type-nocst type) (loop (cdr stack) (and nels (- nels 1))))
(cons type (loop (cdr stack) (and nels (- nels 1))))))))))
(if add-suffix?
(append nstack (list (make-ctx-tclo) (make-ctx-tret)))
stack)))
;;
;; CTX INIT CALL
(define (ctx-init-call ctx nb-args)
(ctx-copy
(ctx-init)
(ctx-init-stack (ctx-stack ctx) nb-args #t)))
;;
;; CTX INIT RETURN
(define (ctx-init-return ctx)
(let ((type (ctx-get-type ctx 0)))
(if (const-versioned? type)
type
(ctx-type-nocst type))))
;;
;; GENERIC
(define (ctx-generic ctx)
(define slot-loc (ctx-slot-loc ctx))
(define free-regs (ctx-free-regs ctx))
(define free-mems (ctx-free-mems ctx))
(define fs (ctx-fs ctx))
(define stack #f)
(define (change-loc slot-loc slot loc)
(if (null? slot-loc)
(error "Internal error")
(let ((sl (car slot-loc)))
(if (eq? (car sl) slot)
(cons (cons slot loc) (cdr slot-loc))
(cons sl (change-loc (cdr slot-loc) slot loc))))))
(define (set-new-loc! slot)
(cond ((not (null? free-regs))
(set! slot-loc (change-loc slot-loc slot (car free-regs)))
(set! free-regs (cdr free-regs)))
((not (null? free-mems))
(set! slot-loc (change-loc slot-loc slot (car free-mems)))
(set! free-mems (cdr free-mems)))
(else
(set! slot-loc (change-loc slot-loc slot (cons 'm fs)))
(set! fs (+ fs 1)))))
;; TODO: merge three functions
(define (get-new-loc)
(cond ((not (null? free-regs))
(let ((r (car free-regs)))
(set! free-regs (cdr free-regs))
r))
((not (null? free-mems))
(let ((m (car free-mems)))
(set! free-mems (cdr free-mems))
m))
(else
(set! fs (+ fs 1))
(cons 'm (- fs 1)))))
(define (compute-stack stack slot)
(if (null? stack)
'()
(let* ((first (car stack))
(ntype
(if (and (ctx-type-cst? first)
(not (ctx-type-id? first)))
(let ((ident (ctx-ident-at ctx (slot-to-stack-idx ctx slot))))
(if (and ident (identifier-cst (cdr ident)))
;; It's a cst associated to a cst identifier
first
;; If stack type is cst, and not associated to a cst identifier, then change loc
(begin
(set-new-loc! slot)
(trigger-type-lost first)
(make-ctx-tunk))))
(begin
(trigger-type-lost first)
(make-ctx-tunk)))))
(cons ntype (compute-stack (cdr stack) (- slot 1))))))
(define (compute-env env)
(if (null? env)
'()
(let ((first (car env)))
(if (identifier-stype (cdr first))
;; there is a stype in identifier, it's a free variable
(let ((new-stype (make-ctx-tunk)))
(trigger-type-lost (identifier-stype (cdr first)))
(assert (eq? (identifier-kind (cdr first)) 'free) "Internal error")
(cons (cons (car first)
(identifier-copy (cdr first) #f '() #f new-stype))
(compute-env (cdr env))))
;; no stype in identifier
(let ((sslots (identifier-sslots (cdr first))))
(assert (eq? (identifier-kind (cdr first)) 'local) "Internal error")
(if (null? sslots)
;; If slots is null, it's a special case used in letrec (letrec uses tmp binding with no slot)
(cons first (compute-env (cdr env)))
(cons (cons (car first)
(identifier-copy (cdr first) #f (list (list-ref sslots (- (length sslots) 1)))))
(compute-env (cdr env)))))))))
(define (compute-slot-loc slot-loc)
(if (null? slot-loc)
'()
(let ((first (car slot-loc)))
(if (or (ctx-loc-is-fregister? (cdr first))
(ctx-loc-is-fmemory? (cdr first))
(and (cdr first)
(find (lambda (el) (eq? (cdr el) (cdr first)))
(cdr slot-loc))))
;; Loc is also used by at least one other slot
(let ((loc (get-new-loc)))
(cons (cons (car first) loc)
(compute-slot-loc (cdr slot-loc))))
;;
(cons first
(compute-slot-loc (cdr slot-loc)))))))
(set! stack (compute-stack (ctx-stack ctx) (- (length (ctx-stack ctx)) 1)))
(let ((env (compute-env (ctx-env ctx)))
(slot-loc (compute-slot-loc slot-loc)))
(ctx-copy ctx stack slot-loc free-regs free-mems (ctx-init-free-fregs) '() env #f #f fs 0)))
;constructor: make-ctx*
;stack ;; virtual stack of types
;slot-loc ;; alist which associates a virtual stack slot to a location
;free-regs ;; list of current free virtual registers
;free-mems ;; list of current free memory slots
;free-fregs ;; list of current free virtual float registers
;free-fmems ;; list of current free virtual float memory slots
;env ;; alist which associates a variable symbol to an identifier object
;nb-actual ;;
;nb-args ;; number of arguments of function of the current stack frame
;fs ;; current frame size
;ffs ;; curent float stack frame size
;fn-num ;; fn-num of current function
(define (ctx-init-*-stack nb-args cn-num free-const stack from-inlined-call?)
;; stack suffix (clo ret)
(define stack-suffix
(if cn-num
(list (make-ctx-tclo) (make-ctx-tretc cn-num))
(list (make-ctx-tclo) (make-ctx-tret))))
;; const free vars
(define (init-const-stack free-const)
(define (init free-const)
(if (null? free-const)
'()
(cons (cdar free-const)
(init (cdr free-const)))))
(reverse (init free-const)))
;;
(append (cond (from-inlined-call? stack)
(stack (ctx-init-stack stack #f #f))
(else (make-list nb-args (make-ctx-tunk))))
(init-const-stack free-const)
stack-suffix))
;;
;; SLOT-LOC
(define (ctx-init-*-slot-loc cn-num nb-free nb-free-const get-slot-loc-local #!optional cloloc inlined-tail?)
(define cont (if cn-num '(0 . #f) '(0 m . 0)))
(define clo
(begin
(assert (not (and (= (- nb-free nb-free-const) 0)
cloloc
(ctx-loc-is-memory? (car cloloc))))
"Internal error") ;; closure pushed but not set to a mem loc in ctx
(cond ((= (- nb-free nb-free-const) 0) '(1 . #f))
(cloloc
(if (and (ctx-loc-is-memory? (car cloloc))
(not inlined-tail?))
(if (ctx-loc-is-memory? (cdr cont))
'(1 m . 1)
'(1 m . 0))
(cons 1 (car cloloc))))
(else '(1 r . 2)))))
(define (init-slot-loc-base)
(append (reverse (build-list nb-free-const (lambda (n) (cons (+ n 2) #f))))
(list clo cont)))
(let ((mem 0))
(if (not cn-num) (set! mem (+ mem 1)))
(if (ctx-loc-is-memory? (cdr clo)) (set! mem (+ mem 1)))
(let* ((fargs-regs (ctx-init-free-fregs))
(sl-base (init-slot-loc-base))
(sl-local (get-slot-loc-local (+ nb-free-const 2) args-regs fargs-regs mem)))
(append sl-local sl-base))))
(define (ctx-init-*-free-regs slot-loc)
(set-sub (ctx-init-free-regs)
(map cdr slot-loc)))
(define (ctx-init-*-free-fregs slot-loc)
(set-sub (ctx-init-free-fregs)
(map cdr slot-loc)))
;;
;; ENV
(define (ctx-init-*-env args free-const free-nconst bound-id)
;; Create env for !const free vars
(define (init-env-free ids nvar)
(if (null? ids)
'()
(let* ((id (caar ids))
(enc-type (if (and opt-entry-points opt-free-versioning) ;; We can't use free variable type to specialize ctx if opt-entry-points is #f
(or (cdar ids) (make-ctx-tclo)) ;; enclosing type, or #f if late
(make-ctx-tunk)))
(identifier (make-identifier 'free '() '() enc-type (cons 'f nvar) #f (eq? id bound-id))))
(cons (cons id identifier)
(init-env-free (cdr ids) (+ nvar 1))))))
;; Create env for const free vars
(define (init-env-free-const free-const slot)
(if (null? free-const)
'()
(let ((first (car free-const)))
(cons (cons (car first)
(make-identifier 'local (list slot) '() #f #f #t (eq? (car first) bound-id)))
(init-env-free-const (cdr free-const) (+ slot 1))))))
;; Create env for !free vars
(define (init-env-local ids slot)
(if (null? ids)
'()
(let* ((id (car ids))
(identifier
(make-identifier
'local (list slot) '() #f #f #f #f)))
(cons (cons id identifier)
(init-env-local (cdr ids) (+ slot 1))))))
;;
(let ((nb-free-const (length free-const)))
(append (init-env-free free-nconst 0)
(init-env-free-const free-const 2)
(init-env-local args (+ nb-free-const 2)))))
(define (ctx-init-fn-inlined-tco cn-num call-ctx call-nb-args enclosing-ctx args free-vars late-fbinds fn-num bound-id)
;; TODO WIP MAKE IT GLOBAL FOR BOTH CTX_INIT_FN_FUNCTIONS
;; Separate constant and non constant free vars
;; Return a pair with const and nconst sets
;; const contains id and type of all constant free vars
;; nconst contains id and type of all non constant free vars
(define (find-const-free ids)
(let loop ((ids ids)
(const '())
(nconst '()))
(if (null? ids)
(cons const (reverse nconst)) ;; Order is important for non cst free variables!
(let* ((id (car ids))
(late? (member id late-fbinds))
(enc-identifier (and (not late?) (cdr (ctx-ident enclosing-ctx id))))
(enc-type (and (not late?) (ctx-identifier-type enclosing-ctx enc-identifier)))
(enc-loc (and (not late?) (ctx-identifier-loc enclosing-ctx enc-identifier)))
(cst? (and (not late?) (ctx-type-cst? enc-type) (not enc-loc))))
;; If an enclosing identifer is cst, type *must* represent a cst
(assert (or (not enc-identifier)
(not (identifier-cst enc-identifier))
(and (identifier-cst enc-identifier) cst?))
"Internal error")
(if cst?
(loop (cdr ids) (cons (cons id enc-type) const) nconst)
(loop (cdr ids) const (cons (cons id enc-type) nconst)))))))
(define (get-stack free-const free-nconst)
(let* ((arg-types (list-head (ctx-stack call-ctx) call-nb-args))
(arg-clo (list-ref (ctx-stack call-ctx) call-nb-args)))
(ctx-init-*-stack call-nb-args cn-num free-const arg-types #t)))
(define (get-slot-loc free-const free-nconst)
(define (init-slot-loc-local slot regs fregs mem)
(let loop ((sidx 0) (slot (+ slot call-nb-args -1)))
(if (= sidx call-nb-args)
'()
(let ((loc (ctx-get-loc call-ctx sidx)))
(cons (cons slot loc)
(loop (+ sidx 1) (- slot 1)))))))
(let ((lcloloc (list (ctx-get-loc call-ctx call-nb-args))))
(ctx-init-*-slot-loc cn-num (length free-vars) (length free-const) init-slot-loc-local lcloloc #t)))
(let* ((r (find-const-free free-vars))
(free-const (car r))
(free-nconst (cdr r))
;;
(stack (get-stack free-const free-nconst))
(slot-loc (get-slot-loc free-const free-nconst))
(fs (ctx-fs call-ctx))
(ffs (ctx-ffs call-ctx))
(free-regs (ctx-init-*-free-regs slot-loc))
(free-mems (set-sub (build-list fs (lambda (m) (cons 'm m)))
(map cdr slot-loc)))
(free-fregs (ctx-init-*-free-fregs slot-loc))
(free-fmems (set-sub (build-list ffs (lambda (m) (cons 'fm m)))
(map cdr slot-loc)))
(env (ctx-init-*-env args free-const free-nconst bound-id))
(nb-actual call-nb-args)
(nb-args (length args)))
(make-ctx stack slot-loc free-regs free-mems free-fregs free-fmems env nb-actual nb-args fs ffs fn-num)))
;; Called when a clo or ret constant information is lost
(define type-lost-list '())
(define (trigger-type-lost type)
(if (and opt-static-mode
(ctx-type-cst? type)
(or (ctx-type-ret? type)
(ctx-type-clo? type)))
(set! type-lost-list (cons type type-lost-list))))
(define (apply-types-lost)
(for-each apply-type-lost type-lost-list)
(set! type-lost-list '()))
(define (apply-type-lost type)
(define (clo-lost fn-num)
(let* ((r (asc-fnnum-ctx-get fn-num))
(nb-ncst-free (length (list-ref r 3)))
(lco (asc-fnnum-lco-get fn-num nb-ncst-free))
(nbargs (asc-fnnum-nbargs-get fn-num)))
;; If nb-args is #f, the function has rest param.
;; Functions with rest param are not optimized using static mode info
;; then, no need to compute info for the function here.
(if nbargs
(let* ((stack (build-list nbargs (lambda (e) (make-ctx-tunk))))
(ctx (apply ctx-init-fn (cons #f (cons stack r)))))
(gen-version-first lco ctx)))))
(define (ret-lost cn-num)
(let ((lco (asc-cnnum-lco-get cn-num))
(ctx (asc-cnnum-ctx-get cn-num)))
(gen-version-first lco (ctx-push ctx (make-ctx-tunk) return-reg))))
(if (and opt-static-mode (ctx-type-cst? type))
(cond ;;
((ctx-type-ret? type) (ret-lost (ctx-type-cst type)))
;;
((ctx-type-clo? type) (clo-lost (ctx-type-cst type))))))
(define (ctx-init-fn-inlined-ntco cn-num call-ctx call-nb-args enclosing-ctx args free-vars late-fbinds fn-num bound-id)
;; TODO WIP MAKE IT GLOBAL FOR BOTH CTX_INIT_FN_FUNCTIONS
;; Separate constant and non constant free vars
;; Return a pair with const and nconst sets
;; const contains id and type of all constant free vars
;; nconst contains id and type of all non constant free vars
(define (find-const-free ids)
(let loop ((ids ids)
(const '())
(nconst '()))
(if (null? ids)
(cons const (reverse nconst)) ;; Order is important for non cst free variables!
(let* ((id (car ids))
(late? (member id late-fbinds))
(enc-identifier (and (not late?) (cdr (ctx-ident enclosing-ctx id))))
(enc-type (and (not late?) (ctx-identifier-type enclosing-ctx enc-identifier)))
(enc-loc (and (not late?) (ctx-identifier-loc enclosing-ctx enc-identifier)))
(cst? (and (not late?) (ctx-type-cst? enc-type) (not enc-loc))))
;; If an enclosing identifer is cst, type *must* represent a cst
(assert (or (not enc-identifier)
(not (identifier-cst enc-identifier))
(and (identifier-cst enc-identifier) cst?))
"Internal error")
(if cst?
(loop (cdr ids) (cons (cons id enc-type) const) nconst)
(loop (cdr ids) const (cons (cons id enc-type) nconst)))))))
(define (get-stack free-const free-nconst)
(let* ((arg-types (list-head (ctx-stack call-ctx) call-nb-args))
(arg-clo (list-ref (ctx-stack call-ctx) call-nb-args)))
(ctx-init-*-stack call-nb-args cn-num free-const arg-types #t)))
(define (get-slot-loc free-const free-nconst)
(define (init-slot-loc-local slot regs fregs mem)
(let loop ((sidx 0) (slot (+ slot call-nb-args -1)) (mem mem))
(if (= sidx call-nb-args)
'()
(let ((loc (ctx-get-loc call-ctx sidx)))
(cond ((ctx-loc-is-memory? loc)
(cons (cons slot (cons 'm mem))
(loop (+ sidx 1) (- slot 1) (+ mem 1))))
((ctx-loc-is-fmemory? loc) (error "e2"))
(else (cons (cons slot loc)
(loop (+ sidx 1) (- slot 1) mem))))))))
(let ((lcloloc (list (ctx-get-loc call-ctx call-nb-args))))
(ctx-init-*-slot-loc cn-num (length free-vars) (length free-const) init-slot-loc-local lcloloc)))
(let* ((r (find-const-free free-vars))
(free-const (car r))
(free-nconst (cdr r))
;;
(stack (get-stack free-const free-nconst))
(slot-loc (get-slot-loc free-const free-nconst))
(free-regs (ctx-init-*-free-regs slot-loc))
(fs (foldr (lambda (sl fs)
(if (ctx-loc-is-memory? (cdr sl))
(max (+ (cddr sl) 1) fs)
fs))
0
slot-loc))
(free-mems '()) ;; TODO: implementer quand mem
(free-fregs (ctx-init-*-free-fregs slot-loc))
(free-fmems '()) ;; TODO: implementer quand fmem
(env (ctx-init-*-env args free-const free-nconst bound-id))
(nb-actual call-nb-args)
(nb-args (length args))
(ffs 0)) ;; TODO: implementer quand fmem
(make-ctx stack slot-loc free-regs free-mems free-fregs free-fmems env nb-actual nb-args fs ffs fn-num)))
;;
;; CTX INIT FN INLINING
(define (ctx-init-fn-inlined tail? cn-num call-ctx call-nb-args enclosing-ctx args free-vars late-fbinds fn-num bound-id)
;; TODO WIP MAKE IT GLOBAL FOR BOTH CTX_INIT_FN_FUNCTIONS
;; Separate constant and non constant free vars
;; Return a pair with const and nconst sets
;; const contains id and type of all constant free vars
;; nconst contains id and type of all non constant free vars
(define (find-const-free ids)
(let loop ((ids ids)
(const '())
(nconst '()))
(if (null? ids)
(cons const (reverse nconst)) ;; Order is important for non cst free variables!
(let* ((id (car ids))
(late? (member id late-fbinds))
(enc-identifier (and (not late?) (cdr (ctx-ident enclosing-ctx id))))
(enc-type (and (not late?) (ctx-identifier-type enclosing-ctx enc-identifier)))
(enc-loc (and (not late?) (ctx-identifier-loc enclosing-ctx enc-identifier)))
(cst? (and (not late?) (ctx-type-cst? enc-type) (not enc-loc))))
;; If an enclosing identifer is cst, type *must* represent a cst
(assert (or (not enc-identifier)
(not (identifier-cst enc-identifier))
(and (identifier-cst enc-identifier) cst?))
"Internal error")
(if cst?
(loop (cdr ids) (cons (cons id enc-type) const) nconst)
(loop (cdr ids) const (cons (cons id enc-type) nconst)))))))
(define (init-slot-loc-local slot regs fregs mem)
(let loop ((n call-nb-args)
(sl (ctx-slot-loc call-ctx))
(slot (+ slot call-nb-args -1))
(mem mem))
(if (= n 0)
'()
(let ((loc (if (ctx-loc-is-memory? (cdar sl))
(let ((r (cons 'm mem)))
(set! mem (+ mem 1))
r)
(cdar sl))))
(cons (cons slot loc)
(loop (- n 1) (cdr sl) (- slot 1) mem))))))
(define (init-slot-loc-local-tail slot regs fregs mem)
(let ((csl (list-head (ctx-slot-loc call-ctx) call-nb-args)))
(let loop ((csl csl) (slot (+ slot call-nb-args -1)))
(if (null? csl)
'()
(let ((sl (car csl)))
(cons (cons slot (cdr sl))
(loop (cdr csl) (- slot 1))))))))
(let* ((r (find-const-free free-vars))
(free-const (car r))
(free-nconst (cdr r))
(types (list-head (ctx-stack call-ctx) call-nb-args))
;;
(stack (ctx-init-*-stack #f cn-num free-const types #t))
(clo-loc (list (cdr (list-ref (ctx-slot-loc call-ctx) call-nb-args))))
(slot-loc (if tail?
(ctx-init-*-slot-loc cn-num (length free-vars) (length free-const) init-slot-loc-local-tail clo-loc)
(ctx-init-*-slot-loc cn-num (length free-vars) (length free-const) init-slot-loc-local clo-loc)))
(free-regs (ctx-init-*-free-regs slot-loc))
(free-fregs (ctx-init-*-free-fregs slot-loc))
(env (ctx-init-*-env args free-const free-nconst bound-id))
(nb-actual call-nb-args)
(nb-args (length args))
(fs (if tail?
(ctx-fs call-ctx)
(foldr (lambda (sl fs)
(if (ctx-loc-is-memory? (cdr sl))
(max fs (+ (cddr sl) 1))
fs))
0
slot-loc)))
(fn-num fn-num)
;;
(ffs (if tail?
(ctx-ffs call-ctx)
0))
(free-mems (set-sub (build-list fs (lambda (n) (cons 'm n)))
(map cdr slot-loc)))
(free-fmems (set-sub (build-list ffs (lambda (n) (cons 'fm n)))
(map cdr slot-loc))))
(make-ctx stack slot-loc free-regs free-mems free-fregs free-fmems env nb-actual nb-args fs ffs fn-num)))
;; Separate constant and non constant free vars
;; Return a pair with const and nconst sets
;; const contains id and type of all constant free vars
;; nconst contains id and type of all non constant free vars
(define (find-const-free ids late-fbinds enclosing-ctx)
(let loop ((ids ids)
(const '())
(nconst '()))
(if (null? ids)
(cons const (reverse nconst)) ;; Order is important for non cst free variables!
(let* ((id (car ids))
(late? (member id late-fbinds))
(enc-identifier (and (not late?) (cdr (ctx-ident enclosing-ctx id))))
(enc-type (and (not late?) (ctx-identifier-type enclosing-ctx enc-identifier)))
(enc-loc (and (not late?) (ctx-identifier-loc enclosing-ctx enc-identifier)))
(cst? (and (not late?) (ctx-type-cst? enc-type) (not enc-loc))))
;; If an enclosing identifer is cst, type *must* represent a cst
(assert (or (not enc-identifier)
(not (identifier-cst enc-identifier))
(and (identifier-cst enc-identifier) cst?))
"Internal error")
(if cst?
(loop (cdr ids) (cons (cons id enc-type) const) nconst)
(loop (cdr ids) const (cons (cons id enc-type) nconst)))))))
;;
;; CTX INIT FN
(define (ctx-init-fn cn-num stack args free-vars free-const free-nconst fn-num bound-id)
;; pick the next available loc:
;; pick the first reg if regs != () or the next mem if regs == ()
;; return (loc updated-regs updated-mem)