/
llvmti.xtm
3990 lines (3653 loc) · 179 KB
/
llvmti.xtm
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) 2011, Andrew Sorensen
;;
;; 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.
;;
;; Neither the name of the authors nor other contributors may be used to endorse
;; or promote products derived from this software without specific prior written
;; permission.
;;
;;
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 DISCLEXTD. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 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.
;;
;;
(define *impc:docstrings* '())
(define *impc:closuretypes* '())
(define *impc:ti:print-sub-checks* #f)
(define *impc:ti:print-main-check* #f)
(define *impc:ti:print-unifications* #f)
(define *impc:compile* #t)
(define *impc:compiler:print* #f)
(define *impc:compiler:print-types* #f)
(define *impc:compiler:verbose* #f)
(define *impc:compiler:print-raw-llvm* #f)
(define *impc:compiler:precomp* (make-string 0))
(define *impc:compiler:precomp-llvm* #f)
(define *impc:compiler:process* (ipc:get-process-name))
;(define *impc:compiler:process* "utility")
(define *impc:zone* (sys:default-mzone))
(define *impc:default-zone-size* (* 8 1024))
(define icr:new-zone
(lambda args
(if (null? args)
(sys:create-mzone *impc:default-zone-size*)
(sys:create-mzone (car args)))))
(define icr:destroy-zone
(lambda (zone)
(if (equal? *impc:zone* zone)
(set! *impc:zone* (sys:default-mzone)))
(if (equal? zone (sys:default-mzone))
(print-notification "You are not allowed to destroy the default zone")
(sys:destrop-mzone zone))))
(define icr:set-zone
(lambda (zone)
(set! *impc:zone* zone)))
(define icr:set-zone-default
(lambda ()
(set! *impc:zone* (sys:default-mzone))))
(define llvm:get-function-args-withoutzone
(lambda (name)
(if (llvm:get-function (string-append name "_getter"))
(let ((ftype (llvm:get-function-args name)))
(list* (car ftype) (cddr ftype)))
(llvm:get-function-args name))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; strips pretty-types from source code
;; returns a cons of (the-new-ast any-explicit-types)
;;
(define impc:ti:get-var-types
(lambda (ast)
(let* ((types '())
(f (lambda (ast)
;(print 'ast: ast 'types: types)
(cond ((null? ast) '())
((atom? ast) ast)
;; ((equal? (car ast) 'dotimes)
;; (list* 'dotimes
;; (list (if (regex:match? (symbol->string (caadr ast)) ":")
;; (let ((t (regex:split (symbol->string (caadr ast)) ":")))
;; (if (regex:match? (cadr t) "^\\<|\\[")
;; (if (not (regex:match? (cadr t) "\\>|\\]"))
;; (print-error 'Compiler 'Error: 'Syntax 'error: 'bad 'type (cadr t))))
;; (set! types (cons (cons (string->symbol (car t)) (string->symbol (cadr t))) types))
;; (string->symbol (car t)))
;; (caadr ast))
;; (cadadr ast))
;; (f (cddr ast))))
((equal? (car ast) 'lambda)
(list* 'lambda
(map (lambda (a)
(if (regex:match? (symbol->string a) ":")
(let ((t (regex:split (symbol->string a) ":")))
(if (regex:match? (cadr t) "^\\<|\\[")
(if (not (regex:match? (cadr t) "\\>|\\]"))
(print-error 'Compiler 'Error: 'Syntax 'error: 'bad 'type (cadr t))))
(set! types (cons (cons (string->symbol (car t)) (string->symbol (cadr t))) types))
(string->symbol (car t)))
a))
(cadr ast))
(f (cddr ast))))
((member (car ast) '(let let* letrec))
(list* (car ast)
(map (lambda (a)
(if (or (atom? a)
(null? (cdr a))
(> (length (cdr a)) 1))
(print-error 'Compiler 'Error: 'syntax 'error: 'badly 'formed 'let 'expression (sexpr->string a)))
(if (regex:match? (symbol->string (car a)) ":")
(let ((t (regex:split (symbol->string (car a)) ":")))
(if (regex:match? (cadr t) "^\\<|\\[")
(if (not (regex:match? (cadr t) "\\>|\\]"))
(print-error 'Compiler 'Error: 'Syntax 'error: 'bad 'type (cadr t))))
(set! types (cons (cons (string->symbol (car t)) (string->symbol (cadr t))) types))
(list (string->symbol (car t)) (car (f (cdr a)))))
(list (car a) (car (f (cdr a))))))
(cadr ast))
(f (cddr ast))))
((pair? ast)
(cons (f (car ast))
(f (cdr ast))))))))
(cons (f ast) types))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Simple Compile Time Generics using type classes
;;
(define *impc:ti:vtypes* (list))
(define impc:ti:add-vtype
(lambda (name type)
(if (string? type)
(set! type (impc:ir:get-type-from-pretty-str type)))
(if (string? name)
(set! name (string->symbol name)))
(let ((v (assoc-strcmp name *impc:ti:vtypes*)))
(if v
(if (member type v) #t
(set-cdr! v (cons type (cdr v))))
(set! *impc:ti:vtypes* (cons (list name type) *impc:ti:vtypes*)))
(ascii-print-color 0 7 10)
(print "Appended type variable ")
(ascii-print-color 1 2 10)
(print name)
(ascii-print-color 0 7 10)
(print " >>> ")
(ascii-print-color 1 3 10)
(print type)
(ascii-print-color 0 7 10)
(print)
#t)))
(define impc:ti:vtypes
(lambda (name)
(let ((res (assoc-strcmp name *impc:ti:vtypes*)))
(if res (cdr res)
#f))))
(define impc:ti:vtype-match?
(lambda (name type)
(let ((res (assoc-strcmp name *impc:ti:vtypes*)))
(if (list? res) (if (member type res) #t #f)
#f))))
(define impc:ti:check-for-vtypes
(lambda (ast)
(let* ((atypes (cdr (impc:ti:get-var-types ast)))
(types (map (lambda (v) (impc:ti:vtypes (cdr v))) atypes))
(rest (cl:remove #f types)))
;(println 'atypes: atypes 'types: types 'rest: rest)
(if (> (length rest) 0) #t #f))))
(define impc:ti:force-vtypes
(lambda (ast)
(replace-all ast '((y:num . y:double)))))
(define impc:ti:vtype-transforms
(lambda (name ast)
(if (symbol? name) (set! name (symbol->string name)))
(let* ((stripped (impc:ti:get-var-types ast))
(args (cadr (caddr (car stripped))))
(atypes (reverse (cdr stripped))) ;; statically bound types
(tvar-lists (map (lambda (v) (impc:ti:vtypes (cdr v))) atypes))
(tlists (cl:remove #f tvar-lists))
(combinations (if (= (length tlists) 1)
(map (lambda (x) (list x)) (car tlists))
(apply multi-list-combination tlists)))
(fixedtype-lists (map (lambda (comb)
(map (lambda (at c)
(let ((findstr (string-append (symbol->string (car at))
":"
(symbol->string (cdr at))))
(replacestr (string-append (symbol->string (car at))
":"
(symbol->string c))))
(cons (string->symbol findstr)
(string->symbol replacestr))))
atypes
comb))
combinations))
(fixedtype-lists2 (map (lambda (comb)
(map (lambda (at c)
(let ((findstr (string-append ":" (symbol->string (cdr at))))
(replacestr (impc:ir:get-base-type (symbol->string c))))
(cons (string->symbol findstr)
(string->symbol replacestr))))
atypes
comb))
combinations))
(newnames (map (lambda (r)
(let* ((hash (string-hash (apply string-append
(map (lambda (t)
(symbol->string t))
r))))
(nameandhash (string-append name "--" (number->string hash))))
(cons (string->symbol name) (string->symbol nameandhash))))
combinations)))
(list (map (lambda (n) (cdr n)) newnames)
(map (lambda (n r r2)
;(println '-> (cons n (append r r2)))
(replace-all (cl:copy-list ast) (cons n (append r r2))))
newnames
fixedtype-lists
fixedtype-lists2)))))
(define impc:ti:parametric-poly-pass
(lambda (ast)
;(println 'impc:ti:parametric-poly-pass)
(if (not (impc:ti:check-for-vtypes ast))
(eval ast (interaction-environment)) ;; if not generic func compile normally
(let* ((storeprint print) ;; set print to nothing but store original as storeprint
(name (cadr ast)) ;; otherwise compile all required templates
(dat (impc:ti:vtype-transforms name ast))
(_ (set! print (lambda args (storeprint "")))) ;;setting print to suppress compiler output
(res (map (lambda (fname nast)
;(println 'fname: fname)
(let ((val (call/cc (lambda (k)
(set! print-error (lambda args
;(println "andrew world")
;(apply pprint-error args)
(k #f)))
#t))))
;(println 'bingo: val 'fname: fname)
(if (equal? val #t)
(let* ((rr (eval nast (interaction-environment)))
(type (impc:ir:get-function-type (symbol->string fname)))
(strtype (impc:ir:pretty-print-type type)))
(if (null? type)
#f
(begin
(eval `(bind-poly ,name ,fname ,(string->symbol strtype))
(interaction-environment))
type))))))
(car dat)
(cadr dat))))
;; restore print-error and print
(set! print-error (lambda args (apply pprint-error args) (error "")))
(set! print storeprint)
(print)
(if (> (length (cl:remove '() (cl:remove #f res))) 0)
(begin
(ascii-print-color 0 7 10)
(print 'Compiled 'generic 'function )
(ascii-print-color 1 2 10)
(print "" name)
(ascii-print-color 0 7 10)
(print)
(ascii-print-color 1 3 10)
(for-each (lambda (k)
(print)
(print "\t" (impc:ir:pretty-print-type k)))
(cl:remove '() (cl:remove #f res)))
(ascii-print-color 0 7 10)
(print)
(print)
(print ".... ")
(ascii-print-color 1 2 10)
(print name)
(ascii-print-color 0 7 10)
(print) (print)
#t)
(begin (print-error 'Compile 'Error: 'failed 'to 'find 'any 'valid 'forms 'for 'generic 'function: name)
#f))))))
;;
;; TRANSFORM CODE
;;
;; Transform straight R5RS code into
;; a simpler but still valid R5RS scheme code
;;
(define impc:ti:and
(lambda (ast)
(if (pair? ast)
(list 'if (car ast)
(if (null? (cdr ast))
(car ast)
(impc:ti:and (cdr ast)))
#f))))
(define impc:ti:or
(lambda (ast)
(if (pair? ast)
(list 'if (car ast)
(car ast)
(if (null? (cdr ast))
#f
(impc:ti:or (cdr ast)))))))
(define impc:ti:cond
(lambda (ast)
(if (null? ast) '()
(list 'if (caar ast)
(if (null? (cdar ast))
'()
(apply list 'begin (cdar ast)))
(impc:ti:cond (cdr ast))))))
(define impc:ti:list
(lambda (ast)
(if (null? ast) 'null
(list 'cons
(car ast)
(impc:ti:list (cdr ast))))))
;; (define impc:ti:map
;; (lambda (ast)
;; (list 'let 'maplloop (append (map (lambda (p l)
;; (cons l (list p)))
;; (cdr ast)
;; (list 'l1 'l2 'l3 'l4 'l5 'l6 'l7 'l8 'l9))
;; (list (list 'll '(list))))
;; (list 'if '(null? l1) '(reverse ll)
;; (append '(maplloop)
;; (map (lambda (p l)
;; (list 'cdr l))
;; (cdr ast)
;; (list 'l1 'l2 'l3 'l4 'l5 'l6 'l7 'l8 'l9))
;; (list (list 'cons (append (list (car ast))
;; (map (lambda (p l)
;; (list 'car l))
;; (cdr ast)
;; (list 'l1 'l2 'l3 'l4 'l5 'l6 'l7 'l8 'l9)))
;; 'll)))))))
(define impc:ti:not
(lambda (ast)
(list 'if ast #f #t)))
(define impc:ti:case
(lambda (expr body)
(if (null? body)
'(list)
`(if ,(if (eq? 'else (caar body))
#t
(list 'member expr (cons 'list (caar body))))
,@(cdar body)
,(impc:ti:case expr (cdr body))))))
(define impc:ti:quote
(lambda (ast)
(cond ((null? ast) '(impc_null)) ;(list))
((symbol? ast) `(llvm_make_symbol ,(symbol->string ast)))
((list? ast)
(cons 'list (map (lambda (a)
(if (or (eq? 'NIL a)
(null? a))
'(list)
a))
ast)))
(else ast))))
(define impc:ti:random
(lambda (ast)
(case (length ast)
((0) (append (list 'imp_rand) ast))
((1) (append (list 'imp_rand1) ast))
((2) (append (list 'imp_rand2) ast)))))
;; no anonymous lambdas !!!
(define impc:ti:lambda
(lambda (ast)
(let* ((fname (gensym))
(rest (cons (impc:ti:first-transform (cadr ast) #t)
(list (cons 'begin (impc:ti:first-transform (cddr ast) #t)))))
(expr (cons 'lambda rest)))
`(let ((,fname ,expr))
(begin ,fname)))))
;; replace (* 2 3 4 5) or (+ 2 3 4 5)
;; with (* 2 (* 3 (* 4 5))) etc..
(define impc:ti:binary-arity
(lambda (ast inbody?)
(let ((op (car ast))
(inlst (reverse (cdr ast))))
(let loop ((rest (cdr inlst))
(lst (car inlst)))
(if (null? rest) lst
(loop (cdr rest) (cons op (cons (impc:ti:first-transform (car rest) inbody?) (list lst)))))))))
(define impc:ti:binary-arity
(lambda (ast inbody?)
(let ((op (car ast))
(inlst (cdr ast)))
(if (< (length inlst) 2)
(print-error 'Compiler 'Error: 'Bad 'arity 'in 'math 'expr ast))
(let loop ((rest (cddr inlst))
(lst (list op
(impc:ti:first-transform (car inlst) inbody?)
(impc:ti:first-transform (cadr inlst) inbody?))))
(if (null? rest) lst
(loop (cdr rest) (list op lst (impc:ti:first-transform (car rest) inbody?))))))))
(define impc:ti:bitwise-not-to-eor
(lambda (ast inbody?)
(list 'bitwise-eor (cadr ast) -1)))
(define impc:ti:afill!
(lambda (ast)
(append '(begin)
(map (lambda (arg idx)
(list 'aset! (car ast) idx arg))
(cdr ast)
(make-list-with-proc (length ast) (lambda (i) i))))))
(define impc:ti:pfill!
(lambda (ast)
(append '(begin)
(map (lambda (arg idx)
(list 'pset! (car ast) idx arg))
(cdr ast)
(make-list-with-proc (length ast) (lambda (i) i))))))
(define impc:ti:tfill!
(lambda (ast)
(append '(begin)
(map (lambda (arg idx)
(list 'tset! (car ast) idx arg))
(cdr ast)
(make-list-with-proc (length ast) (lambda (i) i))))))
(define impc:ti:vfill!
(lambda (ast)
(append '(begin)
(map (lambda (arg idx)
(list 'vset! (car ast) idx arg))
(cdr ast)
(make-list-with-proc (length ast) (lambda (i) i))))))
(define *unique-polynum* 0)
(define impc:ti:first-transform
(lambda (ast inbody?)
;(println inbody? 'transforming-ast: ast)
(if (null? ast) '()
(cond ((list? ast)
(cond ((or (impc:ir:poly-types (car ast))
(impc:ir:gpoly-types (car ast)))
;; (and (impc:ir:poly-types (car ast))
;; (not (impc:ir:gpoly-types (car ast))))
(set! *unique-polynum* (+ 1 *unique-polynum*))
(cons (string->symbol (string-append (symbol->string (car ast))
"##" ;"$$$"
(number->string *unique-polynum*)))
(impc:ti:first-transform (cdr ast) inbody?)))
((eq? (car ast) 'and)
(impc:ti:first-transform (impc:ti:and (cdr ast)) inbody?))
((eq? (car ast) 'random)
(impc:ti:first-transform (impc:ti:random (cdr ast)) inbody?))
((eq? (car ast) 'quote)
(impc:ti:first-transform (impc:ti:quote (cadr ast)) inbody?))
((eq? (car ast) 'list)
(impc:ti:first-transform (impc:ti:list (cdr ast)) inbody?))
;; ((member (car ast) '(map for-each))
;; (impc:ti:first-transform (impc:ti:map (cdr ast)) inbody?))
((eq? (car ast) 'case)
(impc:ti:first-transform (impc:ti:case (cadr ast) (cddr ast)) inbody?))
((eq? (car ast) 'afill!)
(impc:ti:first-transform (impc:ti:afill! (cdr ast)) inbody?))
((eq? (car ast) 'pfill!)
(impc:ti:first-transform (impc:ti:pfill! (cdr ast)) inbody?))
((eq? (car ast) 'tfill!)
(impc:ti:first-transform (impc:ti:tfill! (cdr ast)) inbody?))
((eq? (car ast) 'vfill!)
(impc:ti:first-transform (impc:ti:vfill! (cdr ast)) inbody?))
((eq? (car ast) 'or)
(impc:ti:first-transform (impc:ti:or (cdr ast)) inbody?))
((eq? (car ast) 'free)
(list 'free (list 'bitcast (impc:ti:first-transform (cadr ast) inbody?)
'i8*)))
((member (car ast) '(array))
(impc:ti:first-transform (impc:ti:array ast) inbody?))
((member (car ast) '(tuple))
(impc:ti:first-transform (impc:ti:tuple ast) inbody?))
;((eq? (car ast) 'null?)
; (impc:ti:first-transform `(impc_isnull ,(cadr ast)) inbody?))
((eq? (car ast) 'not)
(impc:ti:first-transform (impc:ti:not (cadr ast)) inbody?))
;; ((eq? (car ast) 'list)
;; (impc:ti:first-transform (impc:ti:binary-arity (cons 'mcons (append (cdr ast) '(nilnil))) inbody?) inbody?))
((and (member (car ast) '(* - / + % bitwise-and bitwise-or bitwise-eor bitwise-shift-left bitwise-shift-right))
(<> (length ast) 3))
(impc:ti:first-transform (impc:ti:binary-arity ast inbody?) inbody?))
((member (car ast) '(bitwise-not ~))
(impc:ti:bitwise-not-to-eor ast inbody?))
((eq? (car ast) 'lambda)
(if inbody?
(impc:ti:lambda ast)
(cons (impc:ti:first-transform (car ast) inbody?)
(cons (impc:ti:first-transform (cadr ast) #t)
(list (cons 'begin (impc:ti:first-transform (cddr ast) #t)))))))
;; ((eq? (car ast) 'cons)
;; (println 'ast: ast)
;; (impc:ti:first-transform (impc:ti:cons ast) inbody?))
((eq? (car ast) 'cond)
(impc:ti:first-transform (impc:ti:cond (cdr ast)) inbody?))
((eq? (car ast) 'cset!)
(list 'closure-set!
(impc:ti:first-transform (cadr ast) inbody?)
(symbol->string (caddr ast))
(impc:ti:first-transform (cadddr ast) inbody?)
(if (not (null? (cddddr ast)))
(impc:ir:get-type-str (impc:ir:convert-from-pretty-types (car (cddddr ast)))))))
((eq? (car ast) 'cref)
(list 'closure-ref
(impc:ti:first-transform (cadr ast) inbody?)
(symbol->string (caddr ast))
(if (not (null? (cdddr ast)))
(impc:ir:get-type-str (impc:ir:convert-from-pretty-types (cadddr ast))))))
((eq? (car ast) 'dotimes)
(list 'dotimes
(impc:ti:first-transform (cadr ast) inbody?)
(cons 'begin (impc:ti:first-transform (cddr ast) inbody?))))
;; ((member (car ast) '(let let* letrec))
;; (cons (impc:ti:first-transform (car ast) inbody?)
;; (cons (impc:ti:first-transform (cadr ast) #f)
;; (list (cons 'begin (impc:ti:first-transform (cddr ast) #t))))))
((member (car ast) '(let let* letrec))
(cons (impc:ti:first-transform (car ast) inbody?)
(cons (map (lambda (p)
(list (impc:ti:first-transform (car p) #f)
(impc:ti:first-transform (cadr p) #f))
)
(cadr ast))
(list (cons 'begin (impc:ti:first-transform (cddr ast) #t))))))
((and (symbol? (car ast))
(regex:match? (symbol->string (car ast)) ".*\\..*"))
(if (regex:match? (symbol->string (car ast)) ".*\\..*:.*")
(let* ((subs (regex:split (symbol->string (car ast)) "\\."))
(a (string->symbol (car subs)))
(subs2 (regex:split (cadr subs) ":"))
(b (string->symbol (car subs2)))
(c (string->symbol (cadr subs2))))
(if (= (length ast) 1)
(impc:ti:first-transform (list 'cref a b c) inbody?)
(impc:ti:first-transform (list 'cset! a b (cadr ast) c) inbody?)))
(let* ((subs (regex:split (symbol->string (car ast)) "\\."))
(a (string->symbol (car subs)))
(b (string->symbol (cadr subs))))
(if (= (length ast) 1)
(impc:ti:first-transform (list 'cref a b) inbody?)
(impc:ti:first-transform (list 'cset! a b (cadr ast)) inbody?)))))
((and (atom? (car ast))
(symbol? (car ast))
(not (eq? 'dotimes (car ast)))
(defined? (car ast))
(macro? (eval (car ast))))
(impc:ti:first-transform (macro-expand ast) 'inbody?))
(else (cons ;(impc:ti:first-transform (car ast) inbody?)
(impc:ti:first-transform (car ast) #t)
;(impc:ti:first-transform (cdr ast) inbody?)))))
(impc:ti:first-transform (cdr ast) #t)))))
(else (cond ((eq? ast #f) '(impc_false))
((eq? ast #t) '(impc_true))
((eq? ast 'cast) 'bitcast)
((eq? ast '&) 'bitwise-and)
((eq? ast 'bor) 'bitwise-or) ; can't use a pipe
((eq? ast '^) 'bitwise-eor)
((eq? ast '<<) 'bitwise-shift-left)
((eq? ast '>>) 'bitwise-shift-right)
((eq? ast '~) 'bitwise-not)
((eq? ast 'else) '(impc_true))
((eq? ast 'printf) 'llvm_printf)
((eq? ast 'sprintf) 'llvm_sprintf)
((eq? ast 'null) '(impc_null))
((eq? ast 'now) 'llvm_now)
((eq? ast 'pset!) 'pointer-set!)
((eq? ast 'pref) 'pointer-ref)
((eq? ast 'pref-ptr) 'pointer-ref-ptr)
((eq? ast 'vset!) 'vector-set!)
((eq? ast 'vref) 'vector-ref)
((eq? ast 'aset!) 'array-set!)
((eq? ast 'aref) 'array-ref)
((eq? ast 'aref-ptr) 'array-ref-ptr)
((eq? ast 'tset!) 'tuple-set!)
((eq? ast 'tref) 'tuple-ref)
((eq? ast 'tref-ptr) 'tuple-ref-ptr)
((eq? ast 'salloc) 'stack-alloc)
((eq? ast 'halloc) 'heap-alloc)
((eq? ast 'zalloc) 'zone-alloc)
((eq? ast 'alloc) 'zone-alloc)
((eq? ast 'schedule) 'callback)
((eq? ast 'void) '(void))
(else ast)))))))
;;
;; TYPE INFERENCE CODE
;;
;; request? can be a type - or a symbol if it's a symbol it must be a free variable available in vars
;;
;;
;; is 't' a complex type?
(define impc:ti:complex-type?
(lambda (t)
(if (atom? t) #f
(if (and (number? (car t)) ;; if list starts with a number (i.e. not a symbol)
(<> (car t) *impc:ir:void*) ;; if not void
(> (modulo (car t) *impc:ir:pointer*) 10)) ;; if proper complex type (tuple,array,closure)
#t
#f))))
;; this will ...
;; 1. try to unify the generic type (vs) using (vars)
;; 2. check against specifications of the polytype that may already exist
;; 3. if 2. exists then return the typename of the specification of the generic type
;; 4. if 2. does not exist then create specific type, add it to type polys and return it
;; 5. if type cannot be unified throw compiler error.
(define impc:ti:symbol-expand-generic-type
(lambda (vs vars all-vs)
;(println 'generic-type-in: vs 'vars: vars 'allvs: all-vs)
(let* ((rsplit1 (regex:split (symbol->string vs) "##")) ;\\$\\$\\$"))
(gnum (if (> (length rsplit1) 1) (cadr rsplit1) #f))
(rsplit2 (regex:split (car rsplit1) "(%)|(\\*)"))
(gpolyname (car rsplit2))
(spec (if (> (length rsplit2) 1) (cadr rsplit2) #f))
(ptrdepth (impc:ir:get-ptr-depth (car rsplit1)))
(t1 (symbol->string (impc:ir:gpolytype-types (string->symbol (impc:ir:get-base-type gpolyname))))))
;(println '@@@@@t1: t1 'spec: spec 'gnum: gnum)
(for-each (lambda (v)
;; if v is not unified then unify it before proceding!
;(println 'vs: vs 'v1: v 'spec: spec 'gnum: gnum)
;(set! v (cons (car v) (impc:ti:type-unify (cdr v) vars)))
;(println 'v2: v)
(if (and (regex:match? (symbol->string (car v)) "!")
(if (not spec) #t
(regex:match? (symbol->string (car v)) (string-append "%" spec)))
(if (not gnum) #t
(regex:match? (symbol->string (car v)) (string-append "##" gnum))) ;\\$\\$\\$" gnum)))
(regex:match? t1 (car (regex:split (symbol->string (car v)) "(##)|(%)"))) ;(\\$\\$\\$)|(%)")))
(not (null? (cdr v))))
(let* ((t (impc:ti:type-unify (cdr v) vars))
;(tl (impc:ir:get-type-str t))
(tl (impc:ir:pretty-print-type t)))
;(println 'tl: tl)
(if (null? tl)
(set! t1 (car v))
(set! t1 (regex:replace t1 (car (regex:split (symbol->string (car v)) "(##)|(%)")) tl))))
#f))
vars)
;(println '------ vs 't2: t1)
;(println '---------- 't22: (impc:ti:type-unify (impc:ir:get-type-from-pretty-str t1) vars))
(if (string? t1) (set! t1 (string->symbol t1)))
;(println 'aaaa: (regex:replace (symbol->string t1) (string-append gpolyname "([^-][^-])") "$1")) ; "[^-][^-]") " "))
(if (impc:ir:type? (impc:ir:get-type-from-pretty-str (regex:replace (symbol->string t1) (string-append gpolyname "([^-][^-])") "$1")))
(let* ((t2 (symbol->string t1))
(base (impc:ir:get-base-type gpolyname)) ;(symbol->string vs)))
(newname (string-append base "--" (number->string (string-hash t2))))
;(l (println 'newname: newname))
(newtype1 (regex:replace t2 (string-append base "([^-][^-])") (string-append newname "$1")))
;(ll (println 'newtype1: newtype1))
(newtype2 (impc:ti:type-unify (impc:ir:get-type-from-pretty-str newtype1 (apply string-append newname (make-list-with-proc ptrdepth (lambda (kk) "*")))) vars))
;(lll (println 'newtype2: newtype2))
;(newtype3 (impc:ir:get-type-str newtype2 (apply string-append newname (make-list-with-proc ptrdepth (lambda (kk) "*"))))))
;;
;; we should do a type normalize but we CAN'T include newname
;; because we haven't added it to LLVM
(newtype3 (impc:ir:get-type-str
(map (lambda (ttt)
;(println 'ttt: ttt)
(if (and (string? ttt)
(regex:match? ttt newname))
ttt
(impc:ti:type-normalize ttt)))
newtype2))))
;(println 'newtype2: newtype2 'newtype3: newtype3 'newname: newname)
;; ok now we have a type we need to add it to llvm and polytype
(llvm:compile (string-append "%" newname " = type " newtype3))
;; next we should try to normalize the newtype
(let* ((newtype4 (impc:ti:type-normalize (impc:ir:get-type-from-str (apply string-append newtype3 (make-list-with-proc ptrdepth (lambda (kk) "*"))))))
(newtype5 (impc:ir:get-type-str newtype4)))
;(println 'newtype3: newtype3 'newtype4: newtype4 'newtype5: newtype5)
(impc:ir:add-polytype (string->symbol base) (string->symbol newname) (string->symbol newtype5))
newtype4))
vs))))
;; takes types with symbols and expands them
;; using types associated with symbols in vars
;; if a particular var doesn't have a type yet
;; then we try to reverse expand
;; (i.e. look at other closure options that may include type values
;; and assign those values into vars)
(define impc:ti:symbol-expand
(lambda (vs vars all-vs)
;(println 'symbol-expand: vs) ; 'vars: vars)
;(println 'all-vs: all-vs)
(if (atom? vs)
(if (symbol? vs)
(if (impc:ir:gpolytype-types (string->symbol (impc:ir:get-base-type (car (regex:split (car (regex:split (symbol->string vs) "##")) "%"))))) ;"\\$\\$\\$")) "%")))))
(impc:ti:symbol-expand-generic-type vs vars all-vs)
(if (not (assoc-strcmp vs vars)) ;; if not in vars
(if (regex:match? (symbol->string vs) "^![^#]*$") ;; then check to see if symbol is a !gvar
vs
(print-error 'Compiler 'Error: 'variable 'not 'marked 'as 'free! (symbol->string vs) 'check 'the 'variable 'name 'in 'the 'polytype))
;; check to see a type has been defined
;; otherwise return null
(let ((t (cdr (assoc vs vars))))
;; first check to see if the symbol vs has a value
(if (null? t) ;; if it doesn't we might need to reverse match!
(let* ((positions (map (lambda (x)
(if (atom? x)
(if (and (symbol? x)
(regex:match? (symbol->string x) "!"))
(print-error 'Compiler 'Error: 'generics 'error: 'this 'closure 'must 'have 'a 'suitable 'generic 'type 'signature)
(print-error 'Compiler 'Error: 'severe 'type 'error: 'have 'you 'specified 'an 'incorrect 'type? x))
(cl:position vs x)))
all-vs))
(position (cl:find-if number? positions))
(values (if position
(map (lambda (x)
(list-ref x position))
all-vs)
'()))
(value (cl:find-if impc:ir:type? values)))
;; if we found a value force it into vars
(if value (impc:ti:update-var vs vars '() value))
;(if value (impc:ti:force-var vs vars '() value))
;(if value (print-notification 'backward 'assigning value 'to vs))
(if value
value
vs)) ;; replace with 't' to go back!!
t)))) ;; else if symbol does have a value then return it
(begin ;(println 'ccc: vs)
vs))
(cons (impc:ti:symbol-expand (car vs) vars all-vs)
(impc:ti:symbol-expand (cdr vs) vars all-vs)))))
;; impc:ti:intersection* is cl:intersection for
;; an arbirary number of sets (i.e. args)
;; also handles *impc:ir:other* which we want
;; to match against anything.
(define impc:ti:intersection*
(lambda args
(let loop ((a args)
(res '()))
(if (null? a)
res
(loop (cdr a)
(if (null? res)
(car a)
(if (null? (car a))
res
(cl:intersection (car a) res))))))))
(define impc:ti:complex-unify
(lambda (sym types vars)
;(println 'cu:> 'sym: sym 'types: types)
;(println 'types types)
(set! types (map (lambda (x)
(if (string? x)
(let ((x2 (if (char=? (string-ref x 0) #\%)
(substring x 1 (string-length x))
x)))
(if (llvm:get-named-type (impc:ir:get-base-type x2))
(let ((t (llvm:get-named-type (impc:ir:get-base-type x2)))
(ptrdepth (impc:ir:get-ptr-depth x2)))
;(set! named-type x)
(set! t (apply string-append t (make-list-with-proc ptrdepth (lambda (i) "*"))))
(impc:ir:get-type-from-str t))
x))
x))
types))
;(println 'vars: vars)
(set! types (impc:ti:type-unify types vars))
;(println 'types2: types)
;; next do a sanity check
;; (if (cl:find-if (lambda (x) (not (list? x))) types)
;; (apply print-error 'Compiler 'Error: 'bad 'type: (symbol->string sym) 'invalid 'mixed 'type 'definitions: types))
;; do we need sub unification? (NOT SURE IF I SHOULD DO THIS REALLY!)
;; (if (cl:find-if (lambda (x) (not (list? x))) types)
;; (set! types (list (apply impc:ti:intersection*
;; (map (lambda (k)
;; (println 'k: k)
;; (if (atom? k)
;; (list k) k))
;; types)))))
;; ;; (apply print-error 'Compiler 'Error: 'bad 'type: (symbol->string sym) 'invalid 'mixed 'type 'definitions: types))
;(println 'sym: sym 'types3: types)
types))
;; this goes through IN ORDER and returns either:
;; NULL if the lists don't match
;; or
(define impc:ti:unify-lists
(lambda args
;(println 'unify: args 'norm: (impc:ti:type-normalize args))
(if (null? args)
args
(let ((lgths (map (lambda (k) (length k)) args)))
(if (not (null? (cl:remove (car lgths) lgths)))
'()
(let ((result
(apply map (lambda args
(let ((l1 (cl:remove '() args)))
(if (null? l1) l1
(let ((l2 (cl:remove-duplicates l1)))
(if (null? l2)
l2
;;(car l2))))))
(if (= 1 (length l2))
(car l2)
'()))))))
args)))
;(println 'result: result)
(if (member '() result)
'()
result)))))))
;; this is here to normalize any recursive tuples
;; i.e. put them in their simplist "named" form
;; you can pass in a a complete list of types
;; at the end and have this normalize them
(define impc:ti:type-normalize
(lambda (t)
(cond ((atom? t) t)
((and (list? t)
(not (null? t))
(number? (car t))
(= *impc:ir:tuple* (modulo (car t) *impc:ir:pointer*)))
;; first check all sub tuples for possible normalization!
(set! t (map (lambda (a) (impc:ti:type-normalize a)) t))
(let ((named-types (cl:remove-if-not string? t)))
(if (null? named-types)
t
(let ((res (map (lambda (k)
(let* ((split (regex:split k "%|(--)"))
(gen-type (symbol->string (impc:ir:gpolytype-types (cadr split))))
(named-type (impc:ir:get-type-from-str (llvm:get-named-type k)))
(match (map (lambda (a b)
(if (equal? a b) #t
(if (and (symbol? a)
(regex:match? gen-type (symbol->string a)))
#t
#f)))
t ;; type coming in
named-type)))
(if (member #f match) #f k)))
named-types)))
(set! res (cl:remove-if-not string? res))
(if (null? res)
(impc:ti:type-normalize (cdr t))
(if (car res)
(car res)
t))))))
((pair? t)
(cons (impc:ti:type-normalize (car t))
(impc:ti:type-normalize (cdr t)))))))
(define impc:ti:type-unify
(lambda (t vars)
;(println 't: t)
(cond ((atom? t)
;; (if (symbol? t)
;; (impc:ti:type-unify (impc:ti:symbol-expand t vars '()) vars)
;; t))
(if (and (symbol? t)
(assoc t vars))
(impc:ti:type-unify (cdr (assoc t vars)) vars)
(if (and (symbol? t)
(impc:ir:gpolytype-types (string->symbol (impc:ir:get-base-type (car (regex:split (car (regex:split (symbol->string t) "##")) "%"))))))
(impc:ti:symbol-expand-generic-type t vars '())
t)))
((list? t)
(cond ((impc:ti:complex-type? t)
(map (lambda (v) (impc:ti:type-unify v vars)) t))
(else (let* ((trick (impc:ti:type-normalize
(map (lambda (v)
(impc:ti:type-unify v vars))
t)))
(typelgth (if (list? (car trick)) (length (car trick)) 1))
(atoms (cl:remove-duplicates (cl:remove #f (map (lambda (v) (if (and (atom? v) (not (null? v))) v #f)) trick))))
(lists (cl:remove #f (map (lambda (v) (if (list? v) v #f)) trick))))
;; (println)
;; (println 'type: t)
;; (println 'typelgth: typelgth)
;; (println 'trick: trick)
;; (println 'atoms: atoms)
;; (println 'lists: lists)
;; (println)
(set! lists (apply impc:ti:unify-lists lists))
;; (println 'uniflists: lists)
;; typelgth helps us to decide if the returned type has
;; been artificially shortened because of unification
;; for example (f (112 2 3) (112 2 3) (112 2 sym))
;; may unify as (112 2) if sym cannot be expanded.
;; this length check basically says we cannot unify
;; succesfully in this instance so return null
(if (<> (length lists) typelgth) (set! lists '()))
(if (null? atoms) lists
(if (> (length atoms) 1)
(append atoms lists)
(if (or (null? lists)
(member (car atoms) lists))
(car atoms)
(append atoms lists))))))))
((pair? t)
(impc:ti:type-unify (cdr t) vars))
(else (print-error 'Compiler 'Error: 'bad 'type t 'in 'unification)))))
(define impc:ti:sym-unify
(lambda (sym types vars)
;(println 'tu:> sym types)
(if (not (cl:find-if list? types))
(if (= (length types) 1)
(car types)
types) ;; if not complex just return it
(impc:ti:complex-unify sym types vars))))
;; (let (;(lll (println 'stuff))
;; (ntypes (flatten-1 (impc:ti:unify-var-scan types types))))
;; ;(println 'types: types 'ntypes: ntypes)
;; (impc:ti:complex-unify sym ntypes vars)))))