/
llvmti.xtm
11811 lines (10961 loc) · 614 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 DISCLAIMED. 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.
;;
;; A windows allocation bug that I can't track down yet!
(define *WINDOWS_ALLOC_BUG* (if (and (string=? (sys:platform) "Windows")
(not (sys:mcjit-enabled)))
#t #f))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; flags for printing debug info ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define *impc:ti:print-sub-checks* #f)
(define *impc:ti:print-main-check* #f)
(define *impc:ti:print-unifications* #f)
(define *impc:ti:print-full-generic-types* #f)
(define *impc:ti:print-code-specialization-compiles* #f)
(define *impc:compile* #t)
(define *impc:compiler:print* #f)
(define *impc:compiler:print-ast* #f)
(define *impc:compiler:print-work-names #f) ;; this prints test__498 for example (i.e. closure bodies)
(define *impc:compiler:verbose* #f)
(define *impc:compiler:print-raw-llvm* #f)
(define *impc:compiler:allow-structural-calls #f)
(define *impc:compiler:process* (ipc:get-process-name))
;;(define *impc:compiler:process* "utility")
(define *impc:ti:bound-lambdas* '())
(define *impc:zone* (sys:default-mzone))
(define *impc:default-zone-size* 0) ;(* 8 1024))
(define *impc:compiler:message:level* 'high)
(define *impc:aot:prev-compiler-message-level* *impc:compiler:message:level*)
(define *impc:ti:implicit-adhoc-compiles* #t)
(define suppress-compiler-messages
(lambda (bool)
(if bool
(set! *impc:compiler:message:level* 'low)
(set! *impc:compiler:message:level* 'high))))
(define-macro (sys:with-quiet-compiler . form)
`(let ((msglvl *impc:compiler:message:level*))
(set! *impc:compiler:message:level* 'low)
(let ((res (catch #f ,@form)))
(set! *impc:compiler:message:level* msglvl)
res)))
(define-macro (sys:with-noisy-compiler . form)
`(let ((msglvl *impc:compiler:message:level*))
(set! *impc:compiler:message:level* 'high)
(let ((res (catch #f ,@form)))
(set! *impc:compiler:message:level* msglvl)
res)))
(define *impc:alphabetlist* '(a b c d e f g h i j k l m n o p q r s t u v w x y z))
(define *impc:alphabetidxlist* '(0 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))
;;;;;;;;;;;;;;;;
;; type enums ;;
;;;;;;;;;;;;;;;;
;; MAKE SURE SIGNED VERSIONS ARE
;; LOWER THAN UNSIGNED VERSIONS
;; i.e. si64 should be lower than ui64
;;
(define *impc:ir:notype* -3) ;; no type!
(define *impc:ir:badtype* -2)
(define *impc:ir:void* -1)
(define *impc:ir:fp64* 0)
(define *impc:ir:double* 0)
(define *impc:ir:fp32* 1)
(define *impc:ir:float* 1)
(define *impc:ir:si64* 2)
(define *impc:ir:ui64* 3)
(define *impc:ir:si32* 4)
(define *impc:ir:ui32* 5)
(define *impc:ir:si16* 6)
(define *impc:ir:ui16* 7)
(define *impc:ir:si8* 8)
(define *impc:ir:ui8* 9)
(define *impc:ir:i1* 10)
(define *impc:ir:char* 11)
(define *impc:ir:null* 12)
(define *impc:ir:closure* 13)
(define *impc:ir:tuple* 14)
(define *impc:ir:array* 15)
(define *impc:ir:vector* 16)
;; this should be incremented to represent the lowest native type
(define *impc:ir:lowest-base-type* 17)
;; and a non-type
(define *impc:ir:other* 1000)
;; pointer offset
(define *impc:ir:pointer* 100)
;;
;; global string constant cnt
;;
;; This WILL cause problems when
;; doing multi-core compilation
;;
(define *impc:ir:gstrcnt* 0)
;; local stack vars
;;
;; This is to hold local stack
;; allocations for if statements
;; These get promoted to the
;; top of the closure to avoid
;; excessive stack allocation
;; in loops etc..
;;
(define *impc:ir:ls_var* '())
;; a list of currently valid local symbols!
;; NOT including global symbols
(define *impc:ir:sym-name-stack* '())
;; type of size_t on platform
(define *impc:ir:size_t_str* (if (= 64 (sys:pointer-size)) "i64" "i32"))
;; pointer size in bytes (as a string)
(define *impc:ir:pointer_size_bytes_str* (if (= 64 (sys:pointer-size)) "8" "4"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Compiler error printing
(define *impc:compiler:pretty-print-name-color* 'green)
(define *impc:compiler:pretty-print-type-color* 'yellow)
(define *impc:compiler:pretty-print-error-color* 'red)
(define *impc:compiler:pretty-print-code-color* 'cyan)
(define impc:compiler:print-constraint-error
(lambda (name type constraint . args)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Constraint Error"))
(print " ")
(print-with-colors *impc:compiler:pretty-print-name-color* 'default #f (print name))
(print " failed constraint ")
(print-with-colors *impc:compiler:pretty-print-code-color* 'default #f (print constraint))
(print "\nwith type: ")
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f (print type))
(if (not (null? args))
(begin
(print "\nast: ")
(define ast (cons (string->symbol name) (cdar args)))
(print (sexpr->string ast))))
(println)
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-double-colon-error
(lambda (var)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Syntax Error"))
(print " double colon error for ")
(print-with-colors *impc:compiler:pretty-print-name-color* 'default #t (print var "\n"))
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-bad-type-error
(lambda (type . message)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Type Error"))
(print " bad type ")
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f (print type))
(if (not (null? message))
(print " " (car message)))
(println)
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-bad-numeric-value-error
(lambda (value expected-type)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Type Error"))
(print " bad numeric value ")
(print-with-colors *impc:compiler:pretty-print-code-color* 'default #f (print value))
(print ", should be ")
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f (print expected-type "\n"))
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-bad-type-error-with-ast
(lambda (type message ast)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Type Error"))
(print " bad type ")
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f (print type))
(print " " message " ")
(print-with-colors *impc:compiler:pretty-print-code-color* 'default #f (print ast "\n"))
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-badly-formed-expression-error
(lambda (expr-type ast)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Syntax Error"))
(print " badly formed ")
(print-with-colors *impc:compiler:pretty-print-name-color* 'default #t (print expr-type))
(print " expression:\n")
(print-with-colors *impc:compiler:pretty-print-code-color* 'default #f (print ast "\n"))
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-index-oob-error
(lambda (type ast)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Syntax Error"))
(print " ")
(print-with-colors *impc:compiler:pretty-print-name-color* 'default #t (print type))
(print " index out of bounds: ")
(print-with-colors *impc:compiler:pretty-print-code-color* 'default #f (print ast "\n"))
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-cannot-expand-non-generic-error
(lambda (name)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Generics Error"))
(print " cannot expand on non-generic ")
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f (print name "\n"))
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-expansion-arity-error
(lambda (before after)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Generics Error"))
(print " expansion arity error ")
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f (print before))
(print " -> ")
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f (print after "\n"))
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-no-valid-forms-for-generic-error
(lambda (name)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Generics Error"))
(print " cannot find any valid forms for generic function ")
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f (print name "\n"))
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-shadow-var-error
(lambda (name)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Compiler Error"))
(print " cannot define ")
(print-with-colors *impc:compiler:pretty-print-name-color* 'default #t (print name))
(print " as a shadow variable\n")
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-already-bound-error
(lambda (name type)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Compiler Error "))
(print-with-colors *impc:compiler:pretty-print-name-color* 'default #t (print name))
(print " already bound as ")
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f (print type "\n"))
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-no-redefinitions-error
(lambda (name oldtype newtype)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Compiler Error"))
(print " cannot redefine or overload the type signature of existing functions. In this case, ")
(print-with-colors *impc:compiler:pretty-print-name-color* 'default #t (print name)) (print " from ")
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f (print oldtype)) (print " to ")
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f (print newtype "\n"))
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-bad-arity-error
(lambda (ast)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Syntax Error"))
(print " bad arity in expression:\n")
(print-with-colors *impc:compiler:pretty-print-code-color* 'default #f (print ast "\n"))
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-no-retval-error
(lambda (ast)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Syntax Error"))
(print " no return value for body: ")
(print-with-colors *impc:compiler:pretty-print-code-color* 'default #f (print ast "\n"))
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-needs-zone-size-error
(lambda (expr-name)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Syntax Error"))
(print " ")
(print-with-colors *impc:compiler:pretty-print-name-color* 'default #t (print expr-name))
(print " requires a zone size as its first argument\n")
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-needs-zone-delay-error
(lambda (expr-name)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Syntax Error"))
(print " ")
(print-with-colors *impc:compiler:pretty-print-name-color* 'default #t (print expr-name))
(print " requires an ")
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f (print "i64"))
(print " delay as its second argument\n")
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-variable-not-marked-as-free-error
(lambda (vs)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Type Error"))
(print " variable " )
(print-with-colors *impc:compiler:pretty-print-name-color* 'default #t (print vs))
(print " not marked as free - check the variable name in the polytype\n")
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define *impc:compiler:top-level-generic-error* #f)
(define impc:compiler:print-type-mismatch-error
(lambda (got expected . name)
(if *impc:compiler:top-level-generic-error*
(begin
(set! name (list (car *impc:compiler:top-level-generic-error*)))))
(if (and (not (null? name))
(list? (car name))
(symbol? (caar name)))
(set! name (list (car (regex:type-split (symbol->string (caar name)) "_poly_")))))
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Type Error"))
(if (not (null? name))
(begin (print " in ")
(print-with-colors *impc:compiler:pretty-print-name-color* 'default #t
(print (car name)))
(print ",")))
(print " got " )
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f
(print (impc:ir:pretty-print-type got)))
(print ", was expecting ")
(if (and (list? expected)
(= 1 (length expected)))
(set! expected (car expected)))
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f
(print (impc:ir:pretty-print-type expected)))
(println)
(if *impc:compiler:top-level-generic-error*
(set! *impc:compiler:top-level-generic-error* #f))
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-type-conflict-error
(lambda (type1 type2 ast)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Type Error"))
(print " conflicting " )
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f (print type1))
(print " with ")
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f (print type2))
(if *impc:compiler:top-level-generic-error*
(begin
(print " calling ")
(print-with-colors *impc:compiler:pretty-print-name-color* 'default #f
(print (car *impc:compiler:top-level-generic-error*)))
(print "\n")
(set! *impc:compiler:top-level-generic-error* #f))
(begin
(print " in ")
(print-with-colors *impc:compiler:pretty-print-code-color* 'default #f (print ast "\n"))))
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-if-type-conflict-error
(lambda (then else)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Type Error"))
(print " type conflict between " )
(print-with-colors *impc:compiler:pretty-print-name-color* 'default #t (print "then"))
(print " (")
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f (print then))
(print ") and ")
(print-with-colors *impc:compiler:pretty-print-name-color* 'default #t (print "else"))
(print " (")
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f (print else))
(print ") branch of " )
(print-with-colors *impc:compiler:pretty-print-name-color* 'default #t (print "if"))
(print " statement\n")
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-unsupported-conversion-error
(lambda (from to)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Type Error"))
(print " unsupported conversion from " )
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f (print (if (number? from) (impc:ir:get-type-str from) from)))
(print " to ")
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f (print (if (number? to) (impc:ir:get-type-str to) to) "\n"))
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-could-not-resolve-types_find-expr
(lambda (name ast)
(cond ((atom? ast) #f)
((and (pair? ast)
(symbol? (car ast))
(equal? name (car ast)))
(vector ast))
((pair? ast)
(list (impc:compiler:print-could-not-resolve-types_find-expr
name (car ast))
(impc:compiler:print-could-not-resolve-types_find-expr
name (cdr ast))))
(else #f))))
(define impc:compiler:print-could-not-resolve-types
(lambda (types ast . name)
(if (and (not (null? types))
(list? types)
(= (length types) 1)
(list? (car types)))
(set! types (car types)))
(if (and (not (null? types))
(symbol? (car types)))
(set! types (list types)))
;; (println 'types: types)
;; (println 'ast: ast)
(print-with-colors 'black 'red #t (print "Could not resolve types!"))
(if (not (null? name))
(begin
(print-with-colors 'red 'black #t (print "::"))
(print-with-colors 'black 'red #t (print (car name)))))
(print-with-colors 'default 'default #t (print "\n"))
(for-each (lambda (t)
;; (println 't t (impc:ir:type? (cdr t)))
(if (or (atom? t)
(and (not (null? (cdr t)))
(not (atom? (cdr t)))
(member (cadr t) '(213)))
(and (not (null? (cdr t)))
(impc:ir:type? (cdr t))))
'done
(let* ((ts (if (atom? (cdr t))
(if (impc:ir:type? (cdr t))
(list (cdr t))
'())
(map (lambda (x)
(if (impc:ir:type? x) x
#f))
(cdr t))))
(tsr (cl:remove #f ts))
(expr1 (if (null? ast) '()
(flatten (impc:compiler:print-could-not-resolve-types_find-expr (car t) ast))))
(expr2 (cl:remove #f expr1))
(expr1a (if (null? expr2) '() (vector-ref (car expr2) 0)))
(all-expr (cl:every (lambda (x) (symbol? x)) expr1a))
(expr (if all-expr (car expr1a) expr1a)))
;; (println tsr ': expr)
;; (println 'tsr tsr (car t))
(if (null? tsr)
(begin
(if (and (symbol? (car t))
(or (regex:match? (symbol->string (car t)) "^_anon_lambda" )
(regex:match? (symbol->string (car t)) "^!")))
'done
(begin
(print-with-colors 'red 'black #t (print "unresolved: "))
(if (null? expr)
(print-with-colors 'red 'black #t (print (car t)))
(print-with-colors 'red 'black #t (print expr)))
(print-with-colors 'default 'default #t (print "\n")))))
(begin (print-with-colors 'red 'black #t (print "ambiguous: "))
(if (null? expr)
(print-with-colors 'red 'black #t (print (car t)))
(print-with-colors 'red 'black #t (print expr)))
(print-with-colors 'default 'default #t (print "\n"))
(for-each (lambda (x n)
(print (string-append "(" (number->string n) ") "))
(print-with-colors 'default 'black #t (print (impc:ir:pretty-print-type x) "\n")))
tsr
(range 0 (length tsr))))))))
types)
(print-with-colors 'red 'default #t (print '------------------------))
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f types)
(println)
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-could-not-resolve-type-error
(lambda (types . message)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Type Error"))
(if (list? types)
(print " couldn't resolve types: ")
(print " couldn't resolve type: "))
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f (print types))
(if (not (null? message))
(print " " (car message)))
(println)
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-could-not-resolve-generic-type-error
(lambda (types ast)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Type Error"))
(if (list? types)
(print " couldn't resolve generic types: ")
(print " couldn't resolve generic type: "))
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f (print types))
(print " ")
(print-with-colors *impc:compiler:pretty-print-code-color* 'default #f (print ast "\n"))
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-missing-identifier-error
(lambda (name type)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Compiler Error"))
(print " cannot find" type "")
(if (impc:ir:poly-or-adhoc? (symbol->string name))
(let ((split-name (impc:ir:split-and-decode-poly-adhoc-name (symbol->string name))))
(print-with-colors *impc:compiler:pretty-print-name-color* 'default #t (print (car split-name)))
(print ":")
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f (print (cadr split-name) "\n")))
(print-with-colors *impc:compiler:pretty-print-name-color* 'default #t (print name "\n")))
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-missing-generic-type-error
(lambda (type-name)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Type Error"))
(print " cannot find generic type ")
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f (print type-name "\n"))
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-not-during-aot-error
(lambda (message)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Compiler Error"))
(print " cannot access LLVM during AOT-compilation.")
(println)
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-compiler-error
(lambda (message . ast)
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Compiler Error"))
(print " " message)
(if (not (null? ast))
(begin (print " ast: ")
(print-with-colors *impc:compiler:pretty-print-code-color* 'default #f
(print (car ast)))))
(println)
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-compiler-warning
(lambda (message . ast)
(print-with-colors *impc:compiler:pretty-print-type-color*
'default #t (print "Compiler Warning"))
(print " " message)
(if (not (null? ast))
(begin (print " ast: ")
(print-with-colors *impc:compiler:pretty-print-code-color* 'default #f
(print (car ast)))))
(println)
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-compiler-failed-error
(lambda ()
(print-with-colors *impc:compiler:pretty-print-error-color*
'default #t (print "Compiler Failed."))
(if (impc:aot:currently-compiling?)
(quit 2)
(throw ""))))
(define impc:compiler:print-binding-details-to-log
(lambda (lead-string symname type)
(if (equal? *impc:compiler:message:level* 'high)
(begin
(print lead-string " ")
(print-with-colors *impc:compiler:pretty-print-name-color* 'default #t (print symname))
(print " >>> ")
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f (print type))
(println)))))
(define impc:compiler:print-bind-func-details-to-log
(lambda (lead-string symname type zone-size)
(if (equal? *impc:compiler:message:level* 'high)
(begin
(print lead-string " ")
(print-with-colors *impc:compiler:pretty-print-name-color* 'default #t (print symname))
(print " >>> ")
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f (print type))
;; (print " zone size: ")
;; (print-with-colors *impc:compiler:pretty-print-code-color* 'default #f
;; (print zone-size))
;; (if (= zone-size *impc:default-zone-size*)
;; (print " (default)"))
(println)))))
(define impc:compiler:print-lib-binding-details-to-log
(lambda (libname symname type)
;; don't write the header stuff for other AOT-compiled xtm libs -
;; assume a header file already exists in that case
(if (equal? *impc:compiler:message:level* 'high)
(begin
(print "LibBound: ")
(print-with-colors *impc:compiler:pretty-print-name-color* 'default #t (print symname))
(print " >>> ")
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #f (print type))
(print " bound from ")
(print-with-colors *impc:compiler:pretty-print-code-color* 'default #f (print libname))
(println)))))
(define impc:compiler:print-polying-details-to-log
(lambda (lead-string poly-name native-name type)
(if (equal? *impc:compiler:message:level* 'high)
(begin
(print lead-string " ")
(print-with-colors *impc:compiler:pretty-print-name-color*
'default #t (print poly-name))
(print " with ")
(print-with-colors *impc:compiler:pretty-print-name-color*
'default #t (print native-name))
(print " >>> ")
(print-with-colors *impc:compiler:pretty-print-type-color*
'default #f (print type))
(println)))))
(define impc:compiler:print-dylib-loading-details-to-log
(lambda (dylib-path)
(let* ((basename (car (reverse (regex:split dylib-path "[/\\\\]"))))
(libname (car (regex:split basename "[.]"))))
(if (equal? *impc:compiler:message:level* 'high)
(begin
(print "Lib Load: ")
(print-with-colors *impc:compiler:pretty-print-name-color*
'default #t (print libname))
(print " dynamic library loaded from ")
(print-with-colors *impc:compiler:pretty-print-code-color*
'default #t (print dylib-path))
(println))))))
(define impc:compiler:print-no-scheme-stub-notification
(lambda (symname)
(if #f ;;(equal? *impc:compiler:message:level* 'high)
(begin
(print "There is no ")
(print-with-colors 'cyan 'default #t (print "scheme stub"))
(print " available for ")
(print-with-colors *impc:compiler:pretty-print-name-color* 'default #t (print symname "\n"))))))
;;;;;;;;;;;;;;;;;;;;;;;;;
;; GLOBAL XTLANG CACHE ;;
;;;;;;;;;;;;;;;;;;;;;;;;;
;; global lists which cache the state (especially types) of all the
;; things.
;; types should be stored in the list format, although they can be
;; printed prettily, obviously
;;;;;;;;;;;;;;;;;;;;
;; language builtins
;; -----------------
;;
(define *impc:ti:builtin-cache* '(;; math operators
("+" . #("[!v,!v,!v...]*" "addition operator: overload xtm_addition to add support for new types" (arg1 arg2...)))
("-" . #("[!v,!v,!v...]*" "subtraction operator: overload xtm_subtraction to add support for new types" (arg1 arg2...)))
("*" . #("[!v,!v,!v...]*" "multiplication operator: overload xtm_multiplication to add support for new types" (arg1 arg2...)))
("/" . #("[!v,!v,!v...]*" "division operator: overload xtm_division to add support for new types" (arg1 arg2...)))
("%" . #("[!v,!v,!v]*" "modulo operator: overload xtm_modulo to add support for new types" (arg1 arg2)))
("set!" . #("[!v,!v,!v]*" "set var to value" (var value)))
;; pointer/tuple/array/vector set/ref
("pref" . #("[!v,!v*,i64]*" "pointer-(de)reference" (ptr idx)))
("pref-ptr" . #("[!v*,!v*,i64]*" "pointer-(de)reference" (ptr idx)))
("pset!" . #("[!v,!v*,i64,!v]*" "pointer-set" (ptr idx val)))
("pfill!" . #("[!v,!v*,!v...]*" "pointer-fill fill ptr with values" (ptr v1...)))
("tref" . #("[!v,!v*,i64]*" "tuple-(de)reference" (tuple idx)))
("tref-ptr" . #("[!v*,!v*,i64]*" "tuple-(de)reference" (tuple idx)))
("tset!" . #("[!v,!v*,i64,!v]*" "tuple-set" (tuple idx val)))
("tfill!" . #("[!v,!v*,!v...]*" "tuple-fill fill tuple with values" (tuple v1...)))
("aref" . #("[!v,!v*,i64]*" "array-(de)reference" (array idx)))
("aref-ptr" . #("[!v*,!v*,i64]*" "array-(de)reference" (array idx)))
("aset!" . #("[!v,!v*,i64,!v]*" "array-set" (array idx val)))
("afill!" . #("[!v,!v*,!v...]*" "array-fill fill array with values" (array v1...)))
("vref" . #("[!v,!v*,i64]*" "vector-(de)reference" (vector idx)))
("vref-ptr" . #("[!v*,!v*,i64]*" "vector-(de)reference" (vector idx)))
("vset!" . #("[!v,!v*,i64,!v]*" "vector-set" (vector idx val)))
("vfill!" . #("[!v,!v*,!v...]*" "vector-fill fill vector with values" (vector v1...)))
;; printing
("println" . #("[void,!v...]*" "generic print function - to add support for NewType, overload print:[void,NewType]*" (val1...)))
;; memory allocation
("alloc" . #("[!v*,i64]*" "allocate memory from current zone with size (optional, default = 1)" (optional-size)))
("zalloc" . #("[!v*,i64]*" "allocate memory from current zone with size (optional, default = 1)" (optional-size)))
("halloc" . #("[!v*,i64]*" "allocate memory from the heap with size (optional, default = 1)" (optional-size)))
("salloc" . #("[!v*,i64]*" "allocate memory from the stack zone with size (optional, default = 1)" (optional-size)))
;; Extempore runtime stuff
("callback" . #("[i1,i64,sym,args...]*" "set callback for closure at time with args" (time closure args...)))
;; special scheme macros
("call-as-xtlang" . #("[String*,!v]*" "the body of this (scheme) macro will be executed as xtlang" (body)))))
;;
;; language builtins - the cache is just used for documentation at
;; this stage, the actual builtins are handled in the compiler (mostly
;; in first-transform). In the future, however, we could integrate
;; that stuff into the builtin-cache
;;
;; (name . #(type-str docstring args))
;;
;; The other differences between this and the closure-list are that
;; the type is stored as a string rather than a list (so that we can
;; handle weird/overloaded/varargs things nicely for documentation
;; purposes) and also that "body" is replaced by "args"
;;
(define impc:ti:print-builtin-cache
(lambda ()
(println '*impc:ti:builtin-cache*: *impc:ti:builtin-cache*)))
(define impc:ti:reset-builtin-cache
(lambda ()
(set! *impc:ti:builtin-cache* '())))
;; this is never called in regular compilation! the builtin cache is
;; populated by hand (see above) and is mostly here for documentation
;; (especially for language builtins)
(define impc:ti:register-new-builtin
(lambda (builtin-name type-str docstring args)
;; check arg types
(if (not (and (or (string? builtin-name) (begin (println 'bad 'builtin-name: builtin-name) #f))
(or (string? type-str) (begin (println 'bad 'type: type-str) #f))
(or (string? docstring) (begin (println 'bad 'docstring: docstring)))
(or (list? args) (begin (println 'bad 'args: args) #f))))
(impc:compiler:print-compiler-error "couldn't register new builtin")
(if (impc:ti:builtin-exists? builtin-name)
(impc:compiler:print-already-bound-error builtin-name (impc:ti:get-builtin-type builtin-name))
;; create a new entry
(begin
(set! *impc:ti:builtin-cache*
(cons (cons builtin-name (vector type-str docstring args))
*impc:ti:builtin-cache*))
(car *impc:ti:builtin-cache*))))))
(define impc:ti:get-builtin-type-str
(lambda (builtin-name)
(let ((builtin-data (assoc-strcmp builtin-name *impc:ti:builtin-cache*)))
;; (println 'cls-list: builtin-data)
(if builtin-data (vector-ref (cdr builtin-data) 0) #f))))
(define impc:ti:builtin-exists?
(lambda (builtin-name)
(let ((res (impc:ti:get-builtin-type-str builtin-name)))
(if (and res (not (null? res))) #t #f))))
(define impc:ti:set-builtin-type-str
(lambda (builtin-name type-str)
(let ((builtin-data (assoc-strcmp builtin-name *impc:ti:builtin-cache*)))
(if builtin-data
(if (not (null? (vector-ref (cdr builtin-data) 0)))
(begin (print "Warning: attempting to re-type already typed builtin")
(print-with-colors *impc:compiler:pretty-print-name-color* 'default #t (print builtin-name))
(print " to ")
(print-with-colors *impc:compiler:pretty-print-type-color* 'default #t (print (impc:ir:pretty-print-type-str type-str) "\n")))
(vector-set! (cdr builtin-data) 0 type-str))
(impc:compiler:print-compiler-error "tried to set type of unknown builtin" builtin-name)))))
(define impc:ti:get-builtin-docstring
(lambda (builtin-name)
(let ((builtin-data (assoc-strcmp builtin-name *impc:ti:builtin-cache*)))
(if builtin-data (vector-ref (cdr builtin-data) 1) #f))))
(define impc:ti:set-builtin-docstring
(lambda (builtin-name docstring)
(let ((builtin-data (assoc-strcmp builtin-name *impc:ti:builtin-cache*)))
(if builtin-data (vector-set! (cdr builtin-data) 1 docstring) #f))))
(define impc:ti:get-builtin-args
(lambda (builtin-name)
(let ((builtin-data (assoc-strcmp builtin-name *impc:ti:builtin-cache*)))
(if builtin-data (vector-ref (cdr builtin-data) 2) #f))))
(define impc:ti:set-builtin-args
(lambda (builtin-name args)
(let ((builtin-data (assoc-strcmp builtin-name *impc:ti:builtin-cache*)))
(if builtin-data (vector-set! (cdr builtin-data) 2 args) #f))))
;;;;;;;;;;;;;;
;; named types
;; -----------
;;
(define *impc:ti:namedtype-cache* '(("mzone" . #((14 108 2 2 2 108 "%mzone*") "Extempore memory zone"))
("clsvar" . #((14 108 4 108 2 "%clsvar*") "Extempore closure address table: <name,offset,type,TODO_whatisthis,next_table>"))))
;;
;; each element of the list is of the form
;;
;; (name . #(type docstring))
(define impc:ti:print-namedtype-cache
(lambda ()
(println '*impc:ti:namedtype-cache*: *impc:ti:namedtype-cache*)))
(define impc:ti:reset-namedtype-cache
(lambda ()
(set! *impc:ti:namedtype-cache* '())))
;; type is immutable, doesn't need a setter
(define impc:ti:get-namedtype-type
(lambda (namedtype-name)
(if (string? namedtype-name)
(let ((ptr-depth (impc:ir:get-ptr-depth namedtype-name))
(namedtype-data (assoc-strcmp (impc:ir:clean-named-type namedtype-name) *impc:ti:namedtype-cache*)))
(if namedtype-data (impc:ir:pointer++ (vector-ref (cdr namedtype-data) 0) ptr-depth) #f))
#f)))
(define impc:ti:namedtype-exists?
(lambda (namedtype-name)
(let ((res (impc:ti:get-namedtype-type namedtype-name)))
(if (and res (not (null? res))) #t #f))))
(define impc:ti:register-new-namedtype
(lambda (namedtype-name type docstring)
;; (println 'namedtype-name: namedtype-name 'type: type 'docstring: docstring)
(if (impc:ti:namedtype-exists? namedtype-name)
'donothing ;;(impc:compiler:print-already-bound-error namedtype-name (impc:ir:pretty-print-type type))
;; check arg types
(if (not (and (or (string? namedtype-name) (begin (println 'bad 'namedtype-name: namedtype-name) #f))
(or (list? type) (integer? type) (begin (println 'bad 'type: type) #f))
(or (string? docstring) (begin (println 'bad 'docstring: docstring) #f))))
(impc:compiler:print-compiler-error "couldn't register new named type")
(begin
;; add to the AOT-header if we're precompiling
(set! *impc:ti:namedtype-cache*
(cons (cons namedtype-name (vector type docstring))
*impc:ti:namedtype-cache*))
(car *impc:ti:namedtype-cache*)
(impc:aot:insert-namedtype-binding-details namedtype-name type docstring))))))
(define impc:ti:get-namedtype-docstring
(lambda (namedtype-name)
(let ((namedtype-data (assoc-strcmp (impc:ir:clean-named-type namedtype-name) *impc:ti:namedtype-cache*)))
(if namedtype-data (vector-ref (cdr namedtype-data) 1) #f))))
(define impc:ti:set-namedtype-docstring
(lambda (namedtype-name docstring)
(let ((namedtype-data (assoc-strcmp (impc:ir:clean-named-type namedtype-name) *impc:ti:namedtype-cache*)))
(if namedtype-data (vector-set! (cdr namedtype-data) 1 docstring) #f))))
;;;;;;;;;;;;;;;
;; type aliases
;; ------------
;;
(define *impc:ti:typealias-cache* '())
;;
;; each element of the list is of the form
;;
;; (name . #(type-alias docstring))
(define impc:ti:print-typealias-cache
(lambda ()
(println '*impc:ti:typealias-cache*: *impc:ti:typealias-cache*)))
(define impc:ti:reset-typealias-cache
(lambda ()
(set! *impc:ti:typealias-cache* '())))
(define impc:ti:get-typealias-type
(lambda (typealias-name)
(if (string? typealias-name)
(let ((ptr-depth (impc:ir:get-ptr-depth typealias-name))
(typealias-data (assoc-strcmp (impc:ir:clean-named-type typealias-name) *impc:ti:typealias-cache*)))
(if typealias-data (impc:ir:pointer++ (vector-ref (cdr typealias-data) 0) ptr-depth) #f))
#f)))
;; as above but returns pretty type
(define impc:ti:get-typealias-type-pretty
(lambda (typealias-name)
(if (string? typealias-name)
(let ((ptr-depth (impc:ir:get-ptr-depth typealias-name))
(typealias-data (assoc-strcmp (impc:ir:clean-named-type typealias-name) *impc:ti:typealias-cache*)))
(if typealias-data (impc:ir:pretty-print-type (impc:ir:pointer++ (vector-ref (cdr typealias-data) 0) ptr-depth))
#f))
#f)))
(define impc:ti:typealias-exists?
(lambda (typealias-name)
(let ((res (impc:ti:get-typealias-type typealias-name)))
(if (and res (not (null? res))) #t #f))))
;; this one will recursively keep following aliases until it reaches
;; the "ground" type
(define impc:ti:get-typealias-ground-type
(lambda (typealias-name)
(let loop ((lowered-alias (impc:ti:get-typealias-type typealias-name)))
(if (and lowered-alias (string? lowered-alias))
(loop (impc:ti:get-typealias-type lowered-alias))
lowered-alias))))
;; as above but returns pretty print
(define impc:ti:get-typealias-ground-type-pretty
(lambda (typealias-name)
(let loop ((lowered-alias (impc:ti:get-typealias-type typealias-name)))
(if (and lowered-alias (string? lowered-alias))
(loop (impc:ti:get-typealias-type lowered-alias))
(impc:ir:pretty-print-type lowered-alias)))))