-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathast.scm
3517 lines (3083 loc) · 148 KB
/
ast.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 "~~lib/_asm#.scm")
(include "~~lib/_x86#.scm")
(include "config.scm")
(include "extern/copy-permanent.scm")
(define free-vars #f)
;;-----------------------------------------------------------------------------
(define perm-domain #f)
(define locat-table #f)
(define (init-frontend)
(set! locat-table (make-table test: eq?))
(set! perm-domain (make-perm-domain))
(init-primitives))
(define (make-lco-id i)
(cons '##lco-id i))
(define (is-lco-id? i)
(and (pair? i) (eq? (car i) '##lco-id)))
;;-----------------------------------------------------------------------------
;; Macros
;; Multiple bindings let
;; ex: (let ((a/b/c (foo))) (println a b c))
;; -> (let ((#sym (foo))
;; (a (car #sym))
;; (b (cadr #sym))
;; (c (caddr #sym)))
;; (println a b c))
(define-macro (mlet bindings . body)
(define (string-split str char)
(define (list-split lst el r)
(if (null? lst)
(list (reverse r))
(if (eq? (car lst) el)
(cons (reverse r)
(list-split (cdr lst)
el
'()))
(list-split (cdr lst)
el
(cons (car lst) r)))))
(map list->string (list-split (string->list str) char '())))
(define (count fn lst)
(if (null? lst)
0
(if (fn (car lst))
(+ 1 (count fn (cdr lst)))
(count fn (cdr lst)))))
(define (write-bindings bindings)
(if (null? bindings)
'()
(let ((id (caar bindings)))
(if (> (count (lambda (el) (char=? el #\/))
(string->list (symbol->string id)))
0)
(let* ((r (string-split (symbol->string id) #\/))
(tmp (gensym)))
(define (build-subbinding r i)
(if (null? r)
'()
(let ((accessor-sym
(string->symbol
(string-append
"ca"
(make-string i #\d)
"r"))))
(cons `(,(string->symbol (car r)) (,accessor-sym ,tmp))
(build-subbinding (cdr r) (+ i 1))))))
(append `((,tmp ,@(cdar bindings)) ,@(build-subbinding r 0))
(write-bindings (cdr bindings))))
(cons (car bindings) (write-bindings (cdr bindings)))))))
`(let* ,(write-bindings bindings) ,@body))
;;-----------------------------------------------------------------------------
;; Parsistent data structures
;; Associate an entry object to a function number
;; if opt-entry-points is #t, entry is the cc-table
;; if opt-entry-points is #f, entry is the 1-sized vector which contains ep
(define asc-globalfn-entry (make-table))
(define (asc-globalfn-entry-add fn-num entry)
(table-set! asc-globalfn-entry fn-num entry))
(define (asc-globalfn-entry-get fn-num)
(table-ref asc-globalfn-entry fn-num))
;; Keep each constant of the program in a still box
;; allowing the compiler to generate:
;; mov dest, [box]
;; to load the constant
(define cst-table (make-table test: equal?))
(define (cst-get cst)
(define (get-obj-addr box)
(let ((box-addr (- (tagging-obj-encoding box) TAG_MEMOBJ)))
(+ box-addr 8)))
(let ((r (table-ref cst-table cst #f)))
(if r
(get-obj-addr box))
(let* ((box (alloc-still-vector 1)))
(vector-set! box 0 cst)
(table-set! cst-table cst box)
;;
(get-obj-addr box))))
;;
;; entry-object -> stubs
;; Associate a pair generic,stub to an entry object
;; This structure is used to determine if an entry point is a stub
;; address or a version address
;;
(define asc-entry-stub (make-table test: eq?))
;; Add an entry to the table
(define (asc-entry-stub-add cctable generic-addr stub-addr)
(assert (not (table-ref asc-entry-stub cctable #f))
"Internal error")
(table-set! asc-entry-stub cctable (cons generic-addr stub-addr)))
;; Read an entry from the table
(define (asc-entry-stub-get cctable)
(table-ref asc-entry-stub cctable #f))
;;
;; (entry-obj . stack) -> label list
;; Associate a list of label to a pair entry-obj/stack
;; This structure is used to store all addresses where the compiler generated a
;; direct jump to a stub.
;; When the stub generates a version stored in this entry object
;; it patches all stored labels and clear the table entry
;;
;; cc-idx is the cctable index if using cctable, #f otherwise
(define asc-entry-load
(make-table
test: (lambda (k1 k2)
(and (eq? (car k1) (car k2)) ;; eq? on cctables
(eq? (cdr k1) (cdr k2)))))) ;; eq? on cc-idx
;; Add an entry to the table
(define (asc-entry-load-add entry-obj cc-idx label)
(let ((r (table-ref asc-entry-load (cons entry-obj cc-idx) '())))
(table-set! asc-entry-load (cons entry-obj cc-idx) (cons label r))))
;; Get all labels from entry object and cc-idx
(define (asc-entry-load-get entry-obj cc-idx)
(table-ref asc-entry-load (cons entry-obj cc-idx) '()))
;; Clear the entry for the entry-object/cc-idx
(define (asc-entry-load-clear entry-obj cc-idx)
(table-set! asc-entry-load (cons entry-obj cc-idx) '())) ;; TODO: remove table entry
;; Global variables information
(define nb-globals 0)
(define asc-globals (make-table))
(define (asc-globals-add id stype)
(table-set! asc-globals id (cons nb-globals stype))
(set! nb-globals (+ nb-globals 1)))
(define (asc-globals-get id)
(table-ref asc-globals id #f))
(define (global-pos global) (car global))
(define (global-stype global) (cdr global))
(define (global-stype-set! global stype) (set-cdr! global stype))
;;
;; Associate a closure (encoding of scm object) to an entry-obj-loc
;; This asc keeps allocations of constant closures as permanent objects
(define asc-entryobj-globalclo (make-table))
(define (asc-entryobj-globalclo-get entry-obj-loc)
(table-ref asc-entryobj-globalclo entry-obj-loc #f))
(define (asc-entryobj-globalclo-add entry-obj-loc clo-ptr)
(assert (not (asc-entryobj-globalclo-get entry-obj-loc))
"Internal error")
(table-set! asc-entryobj-globalclo entry-obj-loc clo-ptr))
;;
;; Associate an fn-num & ep entry object to an ast (eq? on ast)
(define asc-ast-epentry (make-table test: eq?))
(define (asc-ast-epentry-get ast)
(table-ref asc-ast-epentry ast #f))
(define (asc-ast-epentry-add ast eo)
(assert (not (asc-ast-epentry-get ast))
"Internal error")
(table-set! asc-ast-epentry ast eo))
(define asc-fnnum-ctx (make-table))
(define (asc-fnnum-ctx-get fn-num)
(let ((r (table-ref asc-fnnum-ctx fn-num #f)))
(assert r "Internal error")
r))
(define (asc-fnnum-ctx-set fn-num ctx)
(table-set! asc-fnnum-ctx fn-num ctx))
(define asc-fnnum-lco (make-table test: (lambda (a b) (and (= (car a) (car b))
(= (cdr a) (cdr b))))))
(define (asc-fnnum-lco-add fn-num nb-ncst-free lazy-code)
(table-set! asc-fnnum-lco (cons fn-num nb-ncst-free) lazy-code))
(define (asc-fnnum-lco-get fn-num nb-ncst-free)
(table-ref asc-fnnum-lco (cons fn-num nb-ncst-free)))
(define asc-fnnum-nbargs (make-table))
(define (asc-fnnum-nbargs-add fn-num nb-args)
(table-set! asc-fnnum-nbargs fn-num nb-args))
(define (asc-fnnum-nbargs-get fn-num)
(table-ref asc-fnnum-nbargs fn-num))
(define asc-cnnum-lco (make-table))
(define (asc-cnnum-lco-add cn-num lazy-code)
(table-set! asc-cnnum-lco cn-num lazy-code))
(define (asc-cnnum-lco-get cn-num)
(table-ref asc-cnnum-lco cn-num))
(define asc-cnnum-table (make-table))
(define (asc-cnnum-table-add cn-num crtable)
(table-set! asc-cnnum-table cn-num crtable))
(define (asc-cnnum-table-get cn-num)
(let ((r (table-ref asc-cnnum-table cn-num)))
(if (procedure? r)
(let ((table (r)))
(table-set! asc-cnnum-table cn-num table)
table)
r)))
(define asc-cnnum-ctx (make-table))
(define (asc-cnnum-ctx-add cn-num ctx)
(table-set! asc-cnnum-ctx cn-num ctx))
(define (asc-cnnum-ctx-get cn-num)
(table-ref asc-cnnum-ctx cn-num))
;;-----------------------------------------------------------------------------
;; Type predicates
(define type-predicates `(
(output-port? . ,ATX_OPO)
(input-port? . ,ATX_IPO)
(symbol? . ,ATX_SYM)
(string? . ,ATX_STR)
(char? . ,ATX_CHA)
(vector? . ,ATX_VEC)
(f64vector? . ,ATX_FEC)
(fixnum? . ,ATX_INT)
(flonum? . ,ATX_FLO)
(procedure? . ,ATX_CLO)
(pair? . ,ATX_PAI)
(null? . ,ATX_NUL)
))
(define (type-predicate? sym)
(assq sym type-predicates))
;; WARNING: this function return constant instance ATX_* do not modify it!
(define (predicate->ctx-type predicate)
(let ((r (assq predicate type-predicates)))
(if r
(cdr r)
(error ERR_INTERNAL))))
;;-----------------------------------------------------------------------------
;; Primitives
(define (primitive-get sym) (assoc sym primitives))
(define (primitive-sym prim) (list-ref prim 0))
(define (primitive-tag prim) (list-ref prim 1))
(define (primitive-lco-cst prim) (list-ref prim 2))
(define (primitive-lcofun prim) (list-ref prim 3))
(define (primitive-codegen prim) (list-ref prim 4))
(define (primitive-box-fl-args? prim) (list-ref prim 5))
(define (primitive-rettype prim) (list-ref prim 6))
(define (primitive-nbargs prim) (list-ref prim 7))
(define (primitive-argtypes prim) (list-tail prim 8))
;; TODO: remove or add ?/! from codegen-p functions
;; * Most primitives are "simple", they only need to call code generator.
;; A "simple" primitive has only a codegen function and no lco getter
;; * More complex primitives need more complex work.
;; A "complex" primitive has no codegen function but a lco getter that is
;; used to build lco chain and return first lco of the chain
(define primitives '())
(define (init-primitives)
(set! primitives `(
;; Symbol Primitive classification LCO cst all LCO getter Codegen function Box fl args? Return-type Nb-args Args-type
(car (,ATX_PAI) ,cst-car #f ,codegen-p-cxr #f ,ATX_UNK 1 ,ATX_PAI )
(cdr (,ATX_PAI) ,cst-cdr #f ,codegen-p-cxr #f ,ATX_UNK 1 ,ATX_PAI )
(cons (,ATX_PAI) #f #f ,codegen-p-cons #t ,ATX_PAI 2 ,ATX_ALL ,ATX_ALL )
(eq? #f ,cst-eq? ,lco-p-eq? #f #f ,ATX_BOO 2 ,ATX_ALL ,ATX_ALL )
(char=? (,ATX_CHA) ,cst-char=? ,lco-p-eq? #f #f ,ATX_BOO 2 ,ATX_CHA ,ATX_CHA )
(quotient (,ATX_INT) ,cst-binop ,lco-p-binop #f #f ,ATX_INT 2 ,ATX_INT ,ATX_INT )
(modulo (,ATX_INT) ,cst-binop ,lco-p-binop #f #f ,ATX_INT 2 ,ATX_INT ,ATX_INT )
(remainder (,ATX_INT) ,cst-remainder ,lco-p-binop #f #f ,ATX_INT 2 ,ATX_INT ,ATX_INT )
(odd? (,ATX_INT) ,dummy-cst-all #f ,codegen-p-odd?-even? #f ,ATX_BOO 1 ,ATX_INT )
(even? (,ATX_INT) ,dummy-cst-all #f ,codegen-p-odd?-even? #f ,ATX_BOO 1 ,ATX_INT )
(zero? (,ATX_NUM) ,dummy-cst-all ,lco-p-zero? #f #f ,ATX_BOO 1 ,ATX_NUM )
(sin (,ATX_FLO) ,dummy-cst-all #f ,codegen-p-native-fl #f ,ATX_FLO 1 ,ATX_NUM )
(cos (,ATX_FLO) ,dummy-cst-all #f ,codegen-p-native-fl #f ,ATX_FLO 1 ,ATX_NUM )
(atan (,ATX_FLO) ,dummy-cst-all #f ,codegen-p-native-fl #f ,ATX_FLO 1 ,ATX_NUM )
(sqrt (,ATX_FLO) ,dummy-cst-all #f ,codegen-p-sqrt #f ,ATX_FLO 1 ,ATX_NUM )
(not #f ,cst-not ,lco-p-not ,codegen-p-not #f ,ATX_BOO 1 ,ATX_ALL )
(set-car! (,ATX_PAI) #f #f ,codegen-p-set-cxr! #t ,ATX_VOI 2 ,ATX_PAI ,ATX_ALL )
(set-cdr! (,ATX_PAI) #f #f ,codegen-p-set-cxr! #t ,ATX_VOI 2 ,ATX_PAI ,ATX_ALL )
(vector-length (,ATX_VEC) ,cst-vec-len #f ,codegen-p-*vector-length #f ,ATX_INT 1 ,ATX_VEC )
(f64vector-length (,ATX_FEC) ,cst-f64-len #f ,codegen-p-*vector-length #f ,ATX_INT 1 ,ATX_FEC )
(vector-ref (,ATX_VEC) ,cst-vec-ref #f ,codegen-p-*vector-ref #f ,ATX_UNK 2 ,ATX_VEC ,ATX_INT )
(f64vector-ref (,ATX_FEC) ,dummy-cst-all #f ,codegen-p-*vector-ref #f ,ATX_FLO 2 ,ATX_FEC ,ATX_INT )
(char->integer (,ATX_CHA ,ATX_INT) ,cst-char->int #f ,codegen-p-ch<->int #f ,ATX_INT 1 ,ATX_CHA )
(integer->char (,ATX_CHA ,ATX_INT) ,cst-int->char #f ,codegen-p-ch<->int #f ,ATX_CHA 1 ,ATX_INT )
(string-ref (,ATX_STR) ,cst-str-ref #f ,codegen-p-string-ref #f ,ATX_CHA 2 ,ATX_STR ,ATX_INT )
(string-set! (,ATX_STR) #f #f ,codegen-p-string-set! #t ,ATX_VOI 3 ,ATX_STR ,ATX_INT ,ATX_CHA )
(vector-set! (,ATX_VEC) #f #f ,codegen-p-vector-set! #t ,ATX_VOI 3 ,ATX_VEC ,ATX_INT ,ATX_ALL )
(f64vector-set! (,ATX_FEC) #f #f ,codegen-p-f64vector-set! #f ,ATX_VOI 3 ,ATX_FEC ,ATX_INT ,ATX_FLO )
(string-length (,ATX_STR) ,cst-str-len #f ,codegen-p-string-length #f ,ATX_INT 1 ,ATX_STR )
(exit #f #f #f #f #f ,ATX_VOI 0 )
(make-vector (,ATX_VEC) #f #f ,codegen-p-make-vector #t ,ATX_VEC 2 ,ATX_INT ,ATX_ALL )
(make-f64vector (,ATX_FEC) #f #f ,codegen-p-make-f64vector #f ,ATX_FEC 2 ,ATX_INT ,ATX_FLO )
(make-string (,ATX_STR) #f #f ,codegen-p-make-string #f ,ATX_STR 2 ,ATX_INT ,ATX_CHA )
(eof-object? #f ,dummy-cst-all ,lco-p-eof-obj ,codegen-p-eof-object? #f ,ATX_BOO 1 ,ATX_ALL )
(symbol->string (,ATX_STR ,ATX_SYM) ,cst-sym->str #f ,codegen-p-symbol->string #f ,ATX_STR 1 ,ATX_SYM )
(string->symbol (,ATX_STR ,ATX_SYM) ,cst-str->sym #f ,codegen-p-string->symbol #f ,ATX_SYM 1 ,ATX_STR )
(current-output-port (,ATX_OPO) #f ,lco-p-cur-x-port #f #f ,ATX_OPO 0 )
(current-input-port (,ATX_IPO) #f ,lco-p-cur-x-port #f #f ,ATX_IPO 0 )
(number? (,ATX_NUM) ,cst-number? ,lco-p-number? #f #f ,ATX_BOO 1 ,ATX_ALL )
(##apply #f #f ,lco-p-apply #f #f ,ATX_UNK 2 ,ATX_CLO ,ATX_ALL )
(##box #f #f #f ,codegen-p-box #t ,ATX_BOX 1 ,ATX_ALL )
(##unbox #f #f #f ,codegen-p-unbox #t ,ATX_UNK 1 ,ATX_ALL )
(##set-box! #f #f #f ,codegen-p-set-box #t ,ATX_VOI 2 ,ATX_ALL ,ATX_ALL )
(##gettime-ns #f #f #f ,codegen-p-gettime-ns #f ,ATX_INT 0 )
(vector (,ATX_VEC) #f ,lco-p-vector #f #t ,ATX_VEC #f )
(f64vector (,ATX_FEC) #f ,lco-p-f64vector #f #f ,ATX_FEC #f ,ATX_FLO )
(list (,ATX_PAI) #f ,lco-p-list #f #t ,ATX_PAI #f )
(bitwise-and (,ATX_INT) ,dummy-cst-all #f ,codegen-p-bitwise-and #f ,ATX_INT 2 ,ATX_INT ,ATX_INT )
;; These primitives are inlined during expansion but still here to build lambda
(real? (,ATX_NUM) ,dummy-cst-all #f #f #f ,ATX_BOO 1 ,ATX_ALL )
(eqv? #f ,dummy-cst-all #f #f #f ,ATX_BOO 2 ,ATX_ALL ,ATX_ALL )
;;
(##print-double #f #f #f ,codegen-p-print-double #f ,ATX_VOI 1 ,ATX_FLO )
(##print-perm-string #f #f #f ,codegen-p-print-perm-string #f ,ATX_VOI 1 ,ATX_STR )
(##process-statistics #f #f #f ,codegen-p-process-statistics #f ,ATX_FEC 0 )
(##register-lc-call #f #f #f ,codegen-p-register-lc-call #f ,ATX_VOI 2 ,ATX_INT ,ATX_ALL ))))
(define (get-prim-lambda ast sym primitive)
(let ((nbargs (primitive-nbargs primitive)))
(if nbargs
;; Fixed nb args primitive
(let* ((args (build-list (primitive-nbargs primitive) (lambda (x) (string->symbol (string-append "arg" (number->string x))))))
(args-nodes (map atom-node-make args)))
`(lambda ,args (,ast ,@args-nodes)))
;; Rest param primitive
(cond ((eq? sym 'list)
(let ((node-l (atom-node-make 'l)))
`(lambda l ,node-l)))
((eq? sym 'vector)
(error "NYI case in get-prim-lambda"))
((eq? sym 'f64vector)
(error "NYI case in get-prim-lambda"))
(else (error "Internal error"))))))
(define (assert-p-nbargs sym ast)
(let ((prim (primitive-get sym)))
(assert (or (not (primitive-nbargs prim))
(and (= (length (cdr ast))
(primitive-nbargs prim))))
ERR_WRONG_NUM_ARGS)))
;;-----------------------------------------------------------------------------
;; AST SPECIAL NODES
(define (atom-node-make val)
(list '$$atom val))
(define (atom-node? n)
(and (pair? n)
(eq? (car n) '$$atom)))
(define (atom-node-val n)
(cadr n))
(define (atom-node-val-set! node n)
(set-car! (cdr node) n))
;;-----------------------------------------------------------------------------
;; AST DISPATCH
;; Gen lazy code from a list of exprs
(define (gen-ast-l lst succ)
(foldr (lambda (el r) (gen-ast el r)) succ lst))
;; Gen lazy code from ast
(define (gen-ast ast succ)
(assert (pair? ast) "Internal error")
(let ((op (car ast)))
(cond
;; Atom node
((atom-node? ast) (mlc-atom ast succ))
;; Special forms
((eq? op 'begin) (mlc-begin ast succ))
((eq? op 'define) (mlc-define ast succ))
((eq? op 'if) (mlc-if ast succ))
((eq? op 'lambda) (mlc-lambda-ast ast succ))
((eq? op 'let) (mlc-let ast succ)) ;; Also handles let* (let* is a macro)
((eq? op 'letrec) (mlc-letrec ast succ))
((eq? op 'set!) (mlc-set! ast succ))
;; Known operator
((atom-node? op)
(let ((val (atom-node-val op)))
(cond
;;
((primitive-get val) (mlc-primitive val ast succ))
((type-predicate? val) (mlc-test val ast succ))
((gambit-call? op) (mlc-gambit-call ast succ #f))
((member val '(+ - * < > <= >= = /)) (mlc-op-n ast succ val))
;; Call
(else (mlc-call ast succ)))))
;; Call expr
(else (mlc-call ast succ)))))
;;-----------------------------------------------------------------------------
;; ATOM
(define (mlc-atom ast succ)
(let ((val (atom-node-val ast)))
(cond ((string? val) (mlc-literal val ast succ))
((symbol? val) (mlc-identifier val ast succ))
((compiler-flonum? val) (mlc-literal (exact->inexact val) ast succ))
((literal? val) (mlc-literal val ast succ))
((and (pair? val)
(eq? (car val) 'quote))
(mlc-literal (cadr val) ast succ))
(else (error "Internal error (mlc-atom)")))))
;;-----------------------------------------------------------------------------
;; LITERALS
(define (compiler-flonum? n)
(and (number? n)
(or (flonum? n) ;; 3.3
(and (integer? n) (inexact? n)) ;; 3.
(and (not (integer? n)) (exact? n))))) ;; (/ 10 3)
;;
;; Make lazy code from num/bool/char/null literal
;;
(define (mlc-literal lit ast succ)
;; nan boxing and int !32
(if (and opt-nan-boxing
(integer? lit)
(not (int32? lit)))
(set! lit (exact->inexact lit)))
(if (and (integer? lit)
(not (fixnum? lit)))
;; Bignum, fall back to flonum
(set! lit (exact->inexact lit)))
(make-lazy-code
(make-lco-id 1)
(lambda (cgc ctx)
(let ((ctx (ctx-push ctx (literal->ctx-type lit) #f)))
(jump-to-version cgc succ ctx)))))
;;-----------------------------------------------------------------------------
;; VARIABLES GET
;;
;; Make lazy code from SYMBOL
;;
(define (mlc-identifier sym ast succ)
(define next-is-cond (member 'cond (lazy-code-flags succ)))
(define (inlined-cond? type-fn)
(and next-is-cond
(let ((type (type-fn)))
(cond ((and (ctx-type-boo? type)
(ctx-type-cst? type))
(if (ctx-type-cst type)
(lazy-code-lco-true succ)
(lazy-code-lco-false succ)))
((and (not (ctx-type-boo? type))
(not (ctx-type-unk? type)))
(lazy-code-lco-true succ))
(else #f)))))
(define (lcl-inlined-cond? ctx identifier)
(inlined-cond? (lambda () (ctx-identifier-type ctx identifier))))
(define (gbl-inlined-cond? id)
(inlined-cond? (lambda ()
(let ((r (asc-globals-get id)))
(and r (global-stype r))))))
(make-lazy-code
(make-lco-id 2)
(lambda (cgc ctx)
(let ((local (assoc sym (ctx-env ctx)))
(global (asc-globals-get sym)))
;;
(cond ;; Identifier local or global and inlined condition
((or (and local (lcl-inlined-cond? ctx (cdr local)))
(and global (gbl-inlined-cond? sym)))
=>
(lambda (lco)
(jump-to-version cgc lco ctx)))
;; Identifier local and cst literal
((and local
(ctx-type-cst? (ctx-identifier-type ctx (cdr local)))
(not (ctx-identifier-loc ctx (cdr local))))
;; TODO use =>
(let* ((ctx (ctx-push ctx (ctx-identifier-type ctx (cdr local)) #f sym)))
(jump-to-version cgc succ ctx)))
;; Identifier is a local variable
(local
(gen-get-localvar cgc ast ctx local succ))
;; Identifier is a global variable
(global
(gen-get-globalvar cgc ast ctx global succ))
;; Primitive
((primitive-get sym) =>
(lambda (r)
(let ((ast (get-prim-lambda ast sym r)))
(jump-to-version cgc (gen-ast ast succ) ctx))))
(else (gen-error cgc (ERR_UNKNOWN_VAR sym))))))))
(define (gen-get-localvar cgc ast ctx local succ)
(let ((loc (ctx-identifier-loc ctx (cdr local)))
(type (ctx-identifier-type ctx (cdr local))))
(if (and (or (ctx-loc-is-register? loc)
(ctx-loc-is-fregister? loc)))
;; id is in a reg/freg loc
(jump-to-version cgc succ (ctx-push ctx type loc (car local)))
;; id is in mem
(mlet ((type (ctx-identifier-type ctx (cdr local)))
(moves/reg/ctx
(if (and opt-float-unboxing
(ctx-type-flo? type))
(ctx-get-free-freg ast ctx succ 0)
(ctx-get-free-reg ast ctx succ 0)))
(loc (ctx-identifier-loc ctx (cdr local))))
(apply-moves cgc ctx moves)
(if (ctx-loc-is-freemem? loc)
;; It's a free var that is only in closure
(let ((lclo (ctx-get-closure-loc ctx)))
(assert (not (and (not opt-free-versioning) (not (ctx-type-unk? type)))) "Internal error")
(codegen-get-free cgc (ctx-fs ctx) (ctx-ffs ctx) reg lclo loc))
;; The variable is in a register or in non closure memory
(apply-moves cgc ctx (list (cons loc reg))))
(jump-to-version cgc succ (ctx-push ctx type reg (car local)))))))
(define (gen-get-globalvar cgc ast ctx global succ)
(let ((type (or (global-stype global) (make-ctx-tunk))))
(if (ctx-type-cst? type)
;; Type is cst, push cst to ctx
(jump-to-version cgc succ (ctx-push ctx type #f))
;; Type is not a cst, free a register and use it
(mlet ((moves/reg/ctx (ctx-get-free-reg ast ctx succ 0)))
(apply-moves cgc ctx moves)
(codegen-get-global cgc (global-pos global) reg)
(jump-to-version cgc succ (ctx-push ctx type reg))))))
;;-----------------------------------------------------------------------------
;; VARIABLES SET
;;
;; Make lazy code from SET!
;;
(define (mlc-set! ast succ)
(let* ((id (cadr ast))
(lazy-set!
(make-lazy-code
(make-lco-id 3)
(lambda (cgc ctx)
(let ((global (asc-globals-get id)))
(if global
(gen-set-globalvar cgc ast ctx global succ)
(error "Internal error"))))))
(lazy-drop
(make-lazy-code
(make-lco-id 4)
(lambda (cgc ctx)
(let ((ctx (drop-cst-value cgc ast ctx 0)))
(jump-to-version cgc lazy-set! ctx))))))
(gen-ast (caddr ast) lazy-drop)))
(define (gen-set-globalvar cgc ast ctx global succ)
(mlet ((pos (global-pos global))
(moves/reg/ctx (ctx-get-free-reg ast ctx succ 1))
(tval (ctx-get-type ctx 0))
(cst? (and (ctx-type-cst? tval)
(not (ctx-get-loc ctx 0))))
(lval (if cst?
(ctx-type-cst tval)
(ctx-get-loc ctx 0))))
(apply-moves cgc ctx moves)
(codegen-set-global cgc (ctx-fs ctx) (ctx-ffs ctx) reg pos lval cst?)
(jump-to-version cgc succ (ctx-push (ctx-pop ctx) (make-ctx-tvoi) reg))))
;;-----------------------------------------------------------------------------
;; INTERNAL FORMS
;;
;; Make lazy code from DEFINE
;;
(define (mlc-define ast succ)
(let ((global (asc-globals-get (cadr ast))))
(cond
((and global (ctx-type-clo? (global-stype global)))
;; CONST FN TODO: remove when const versioning implemented!
(let* ((identifier (cadr ast))
(fn-num (init-entry-cst (caddr ast) '() (ctx-init))))
(ctx-type-cst-set! (global-stype global) fn-num)
(make-lazy-code
(make-lco-id 5)
(lambda (cgc ctx)
(jump-to-version cgc succ (ctx-push ctx (global-stype global) #f))))))
((and global (ctx-type-cst? (global-stype global)))
(make-lazy-code
(make-lco-id 6)
(lambda (cgc ctx)
(jump-to-version cgc succ (ctx-push ctx (make-ctx-tnulc '()) #f)))))
(else
(let* ((val (and (atom-node? (caddr ast)) (atom-node-val (caddr ast))))
(primitive (and val (assoc val primitives))))
(if primitive
;; form (define sym primitive)
(begin (set! primitives
(cons (cons (cadr ast) (cdr primitive))
primitives))
(make-lazy-code (make-lco-id 7)
(lambda (cgc ctx)
(jump-to-version cgc succ (ctx-push ctx (make-ctx-tbooc #f) #f)))))
;; other
(let* ((identifier (cadr ast))
(lazy-bind (make-lazy-code
(make-lco-id 8)
(lambda (cgc ctx)
(mlet ((pos (global-pos (asc-globals-get identifier))) ;; Lookup in globals
;;
(moves/reg/ctx (ctx-get-free-reg ast ctx succ 1))
(type (ctx-get-type ctx 0))
(cst? (and (ctx-type-cst? type) (not (ctx-get-loc ctx 0))))
(lvalue (if cst?
(ctx-type-cst type)
(ctx-get-loc ctx 0))))
(apply-moves cgc ctx moves)
(codegen-define-bind cgc (ctx-fs ctx) (ctx-ffs ctx) pos reg lvalue cst?)
(jump-to-version cgc succ (ctx-push (ctx-pop ctx) (make-ctx-tvoi) reg))))))
(lazy-drop
(make-lazy-code
(make-lco-id 9)
(lambda (cgc ctx)
(let ((type (ctx-get-type ctx 0)))
(if (and (ctx-type-cst? type)
(ctx-type-clo? type))
(let ((ctx (drop-cst-value cgc ast ctx 0)))
(jump-to-version cgc lazy-bind ctx))
(jump-to-version cgc lazy-bind ctx))))))
(lazy-val (gen-ast (caddr ast) lazy-drop)))
(let ((void (to-64-value (if opt-nan-boxing NB_ENCODED_VOID ENCODING_VOID))))
(put-i64 (+ globals-addr (* 8 (global-pos (asc-globals-get (cadr ast))))) void))
lazy-val)))))))
;;
;; Make lazy code from LAMBDA
;;
;;
;; Create and return a generic prologue lco
(define (get-lazy-generic-prologue ast succ rest-param nb-formal)
(make-lazy-code-entry
(make-lco-id 10)
rest-param
(lambda (cgc ctx)
(let ((nb-args (ctx-nb-args ctx))
(label-next (asm-make-label #f (new-sym 'label-next))))
;;
(if (not rest-param)
(codegen-prologue-gen-nrest cgc nb-args)
(codegen-prologue-gen-rest cgc (ctx-fs ctx) (ctx-ffs ctx) nb-args))
;;
(jump-to-version cgc succ ctx)))))
(define (gen-drop-int cgc ctx idx-from idx-to)
(define (drop idx-from)
(if (not (= idx-from idx-to))
(let* ((type (ctx-get-type ctx idx-from)))
(if (and (ctx-type-int? type)
(not (ctx-type-cst? type)))
(let ((loc (ctx-get-loc ctx idx-from)))
(codegen-box-int cgc (ctx-fs ctx) (ctx-ffs ctx) loc loc)))
(drop (+ idx-from 1)))))
(if opt-int-unboxing
(drop idx-from)))
(define (gen-drop-float cgc ctx ast idx-from idx-to)
(define (drop ctx idx-from)
(if (= idx-from idx-to)
ctx
(let* ((type (ctx-get-type ctx idx-from)))
(if (and (ctx-type-flo? type)
(not (ctx-type-cst? type)))
(mlet ((moves/reg/ctx (ctx-get-free-reg ast ctx #f 0))
(loc (ctx-get-loc ctx idx-from))
(opnd (codegen-loc-to-x86opnd (ctx-fs ctx) (ctx-ffs ctx) loc))
(ropnd (codegen-reg-to-x86reg reg)))
(apply-moves cgc ctx moves)
(codegen-box-float cgc (ctx->gc-map-desc ctx) (ctx-fs ctx) (ctx-ffs ctx) loc reg)
(let* ((ident (ctx-ident-at ctx idx-from))
(ctx
;; Remove slot-info if the slot belongs to an identifier with > 1 slots
(if (and ident (= (length (identifier-sslots (cdr ident))) 1))
ctx
(ctx-remove-slot-info ctx idx-from)))
(ctx (ctx-set-loc ctx (stack-idx-to-slot ctx idx-from) reg))
(ctx (ctx-set-type ctx idx-from (make-ctx-tunk) #f)))
(drop ctx (+ idx-from 1))))
(drop ctx (+ idx-from 1))))))
(if (not opt-float-unboxing)
ctx
(drop ctx idx-from)))
;;
;; Create and return a prologue lco
(define (get-lazy-prologue ast succ rest-param)
(lazy-code-f-entry! succ)
(if rest-param
(lazy-code-f-rest! succ))
succ)
;;
;; Create and return a function return lco
(define (get-lazy-return)
(define (gen-return-rp cgc ctx)
(set! ctx (drop-cst-value cgc #f ctx 0))
(let* ((fs (ctx-fs ctx))
(ffs (ctx-ffs ctx))
;; Return value loc
(type-ret (ctx-get-type ctx 0))
;(type-arg (ctx-identifier-type ctx (cdr (ctx-ident ctx 'n0))))
(lret (ctx-get-loc ctx 0))
;; Return address object loc
(laddr (ctx-get-retobj-loc ctx)))
(let ((float-val? (and opt-float-unboxing (ctx-type-flo? type-ret)))
(int-val? (and opt-int-unboxing (ctx-type-int? type-ret)))
(gc-desc (ctx->gc-map-desc ctx)))
(codegen-return-rp cgc gc-desc fs ffs laddr lret float-val? int-val?))))
(define (gen-return-cr cgc ctx)
(let ((cridx (crtable-get-idx (ctx-init-return ctx))))
(assert (not (and (not cridx)
(ctx-type-flo? (ctx-get-type ctx 0))))
"NYI case, cr overflow and ret value is a tflo")
(set-cr-gc-map-desc cridx (ret-gc-map-desc))
(if (or (not (const-versioned? (ctx-get-type ctx 0)))
(not cridx))
(set! ctx (drop-cst-value cgc #f ctx 0)))
(let* ((fs (ctx-fs ctx))
(ffs (ctx-ffs ctx))
;; Return value loc
(type-ret (ctx-get-type ctx 0))
;(type-arg (ctx-identifier-type ctx (cdr (ctx-ident ctx 'n0))))
(lret (ctx-get-loc ctx 0))
;; Return address object loc
(laddr (ctx-get-retobj-loc ctx))
(taddr (ctx-get-type ctx (- (length (ctx-stack ctx)) 1)))
;;
(cst? (not lret)))
(assert (if (not lret) (ctx-type-cst? type-ret) #t) "Internal error gen-return-cr")
(if (ctx-type-cst? taddr)
(let ((lco (asc-cnnum-lco-get (ctx-type-cst taddr)))
(opret (if (and opt-float-unboxing
(ctx-type-flo? type-ret))
(codegen-freg-to-x86reg return-freg)
(codegen-reg-to-x86reg return-reg)))
(opretval (and lret (codegen-loc-to-x86opnd fs ffs lret))))
(if (and opretval
(not (eq? opret opretval)))
(if (and opt-float-unboxing
(ctx-type-flo? type-ret))
(x86-movsd cgc opret opretval)
(x86-mov cgc opret opretval)))
(codegen-return-clean cgc fs ffs)
(let* ((ctx (asc-cnnum-ctx-get (ctx-type-cst taddr)))
(ctx
(cond ((not lret)
(ctx-push ctx type-ret #f))
((and opt-float-unboxing (ctx-type-flo? type-ret))
(ctx-push ctx type-ret return-freg))
(else
(ctx-push ctx type-ret return-reg)))))
(x86-label cgc (asm-make-label #f (new-sym 'inlined_cont_)))
(jump-to-version cgc lco ctx)))
(let ((float-val? (and opt-float-unboxing (ctx-type-flo? type-ret))))
(codegen-return-cr cgc fs ffs laddr lret cridx float-val? cst?))))))
(make-lazy-code-ret ;; Lazy-code with 'ret flag
(make-lco-id 32)
(lambda (cgc ctx)
(if opt-return-points
(gen-return-cr cgc ctx)
(gen-return-rp cgc ctx)))))
;; eq? on ast and = on nb-ncst-free
(define fn-prologues (make-table test: (lambda (k1 k2) (and (eq? (car k1) (car k2))
(= (cdr k1) (cdr k2))))))
(define (get-fn-prologues ast fn-num nb-ncst-free)
;; Function use rest param ?
(define rest-param? (or (and (not (list? (cadr ast))) (not (pair? (cadr ast)))) ;; (foo . rest)
(and (pair? (cadr ast)) (not (list? (cadr ast)))))) ;; (foo a..z . rest)
;; List of formal params
(define params
(if rest-param?
(formal-params (cadr ast))
(cadr ast)))
(define nb-params (length params))
(let* ((k (cons ast nb-ncst-free))
(r (table-ref fn-prologues k #f)))
(if r
(begin (asc-fnnum-lco-add fn-num nb-ncst-free (car r))
r)
(let* ((lazy-ret (get-lazy-return))
(lazy-body (gen-ast (caddr ast) lazy-ret))
(lazy-prologue (get-lazy-prologue ast lazy-body rest-param?))
(lazy-prologue-gen (get-lazy-generic-prologue ast lazy-body rest-param? nb-params))
(prologues (cons lazy-prologue lazy-prologue-gen)))
(table-set! fn-prologues k prologues)
(asc-fnnum-lco-add fn-num nb-ncst-free lazy-prologue)
prologues))))
;;
;; Create fn entry stub
(define (create-fn-stub ast fn-num fn-generator nb-ncst-free)
(let* ((prologues (get-fn-prologues ast fn-num nb-ncst-free))
(lazy-prologue (car prologues))
(lazy-prologue-gen (cdr prologues)))
(list
lazy-prologue
(add-fn-callback
1
fn-num
(lambda (stack cc-idx cn-num ret-addr selector closure)
(cond ;; CASE 1 - Use entry point (no cctable)
((eq? opt-entry-points #f)
(fn-generator closure lazy-prologue-gen #f cc-idx cn-num #f))
;; CASE 2 - Function is called using generic entry point
((= selector 1)
(fn-generator #f lazy-prologue-gen #f cc-idx cn-num #t))
;; CASE 3 - Use multiple entry points
(else
(fn-generator #f lazy-prologue stack cc-idx cn-num #f))))))))
(define (get-entry-obj ast ctx fvars-imm fvars-late all-params bound-id)
(define fn-num #f)
(define free-cst/ncst (find-const-free (append fvars-imm fvars-late) fvars-late ctx))
;; Generator used to generate function code waiting for runtime data
;; First create function entry ctx
;; Then generate function prologue code
(define (fn-generator closure prologue stack cc-idx cn-num generic?)
;; In case the limit in the number of version is reached, we give #f to ctx-init-fn to get a generic ctx
;; but we still want to patch cctable at index corresponding to stack
(let* ((ctxstack (if generic? #f stack))
(free-const (car free-cst/ncst))
(free-nconst (cdr free-cst/ncst))
(ctx (ctx-init-fn cn-num ctxstack all-params (append fvars-imm fvars-late) free-const free-nconst fn-num bound-id)))
(gen-version-fn ast closure entry-obj prologue ctx cc-idx generic?)))
;; ---------------------------------------------------------------------------
;; Return 'entry-obj' (entry object)
;; An entry object is the object that contains entry-points-locs
;; In the case of -cc, entry object is the cctable
(define (get-entry-obj-cc)
(let* ((r (get-cctable ast ctx fvars-imm fvars-late))
(new? (car r))
(cctable (cadr r)))
(set! fn-num (cddr r))
(if new?
(mlet (;; Create stub only if cctable is new
(lco/stub-labels (create-fn-stub ast fn-num fn-generator (length (cdr free-cst/ncst))))
(stub-addr (asm-label-pos (list-ref stub-labels 0)))
(generic-addr (asm-label-pos (list-ref stub-labels 1))))
(asc-globalfn-entry-add fn-num (cons cctable (cons lco stub-addr)))
(asc-entry-stub-add cctable generic-addr stub-addr)
(cctable-fill cctable stub-addr generic-addr)))
(values fn-num cctable)))
;; In the case of -ep, entry object is the still vector of size 1 that contain the single entry point
(define (get-entry-obj-ep)
(let ((existing (asc-ast-epentry-get ast)))
(if existing
;; An entry points object already exists for this ast, use it!
(values (car existing) (cdr existing))
;; TODO: we are supposed to use only one e.p. with -ep objects
;; use a max-selector of 0 in create-fn-stub, and use only one -addr
(mlet (;; Create stub
(fn-num (new-fn-num))
(lco/stub-labels (create-fn-stub ast fn-num fn-generator (length (cdr free-cst/ncst))))
(stub-addr (asm-label-pos (list-ref stub-labels 0)))
(generic-addr (asm-label-pos (list-ref stub-labels 1)))
(entryvec (get-entry-points-loc ast stub-addr)))
(asc-globalfn-entry-add fn-num (cons entryvec (cons lco stub-addr)))
(asc-entry-stub-add entryvec generic-addr stub-addr)
(asc-ast-epentry-add ast (cons fn-num entryvec))
(values fn-num entryvec)))))
(define entry-obj #f)
(call-with-values
(if opt-entry-points
get-entry-obj-cc
get-entry-obj-ep)
(lambda (fn-num obj)
(set! entry-obj obj)
(let ((free-const (car free-cst/ncst))
(free-nconst (cdr free-cst/ncst)))
(asc-fnnum-ctx-set fn-num (list all-params (append fvars-imm fvars-late) free-const free-nconst fn-num bound-id))
(values fn-num entry-obj)))))
;;
;; Init constant lambda
(define (init-entry-cst ast free ctx)
;; Flatten list of param (include rest param)
(define all-params (flatten (cadr ast)))
(call-with-values
(lambda () (get-entry-obj ast ctx free '() all-params #f))
(lambda (fn-num entry-obj)
fn-num)))
;;
;; Init non constant lambda
(define (init-entry ast ctx fvars-imm fvars-late bound-id)