-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.scm
807 lines (697 loc) · 28.9 KB
/
main.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
#! gsi-script
;;---------------------------------------------------------------------------
;;
;; 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/_x86#.scm")
(include "config.scm")
(include "./extern/Sort.scm")
(define pp pretty-print)
(define-macro (string-bold str)
`(string-append "\033[1m" ,str "\033[0m"))
(define-macro (run-add-to-ctime f)
(let ((tmp (gensym)))
`(if opt-ctime
(let ((,tmp (##exec-stats ,f)))
(set! real-compilation-time
(+ (- (cdr (assoc 'real-time ,tmp))
(cdr (assoc 'gc-real-time ,tmp)))
real-compilation-time))
(cdr (assoc 'result ,tmp)))
(,f))))
;;--------------------------------------------------------------------------------
;; Compiler options
(define (print-help)
(newline)
(println (string-bold "NAME"))
(println " lc - Scheme JIT compiler")
(newline)
(println (string-bold "SYNOPSIS"))
(println " ./lc [file] [options]")
(newline)
(println (string-bold "OPTIONS"))
(let ((options (sort compiler-options (lambda (a b)
(string<? (symbol->string (car a))
(symbol->string (car b)))))))
(for-each (lambda (option) (println " " (car option))
(println " " " " (cadr option))
(newline))
options))
(newline))
;; Contains all compiler options
;; An option contains (option-text help-text option-lambda)
(define compiler-options `(
;; OPTIONS PARSED BY THE LAUNCHER (here for doc)
(--gdb
"Run the compiler with gdb"
,(lambda (args) (cdr args)))
(--min-heap
"Set the minimum heap size (mb). Ex. '--min-heap 1000000' to ask for a 1 gigabyte heap"
,(lambda (args) (cdr args)))
;; LC RUNTIME OPTIONS
(--call-max-len
"Set the max number of args allowed when using a specialized entry point"
,(lambda (args) (if (not opt-entry-points) (error "--call-max-len requires interprocedural extensions"))
(set! opt-call-max-len (string->number (cadr args)))
(cdr args)))
(--cc-max
"Set the max size of the global entry points table"
,(lambda (args) (set! opt-cc-max (string->number (cadr args)))
(cdr args)))
(--cr-max
"Set the max size of the global return points table"
,(lambda (args) (set! opt-cr-max (string->number (cadr args)))
(cdr args)))
(--code-size
"Print generated code size after execution"
,(lambda (args) (set! opt-code-size #t) args))
(--const-vers-types
"Select the type of the constants used for interprocedural versioning. (ex. --const-vers-types boo cha str)"
,(lambda (args)
;; TODO: types -> preds
(define (end next-args types)
(set! opt-cv-preds types)
(cons (car args) next-args))
(if (not opt-const-vers) (error "--const-vers-types needs --opt-const-vers"))
(let loop ((curr (cdr args))
(types '()))
(if (null? curr)
(end curr types)
(let* ((next (car curr))
(chars (string->list next)))
(if (char=? (car chars) #\-)
(end curr types)
(loop
(cdr curr)
(cons (ctx-string->tpred next)
types))))))))
(--count-function-args
"Count and print the number of arguments used at function calls"
,(lambda (args) (set! opt-count-fnargs #t) args))
(--count-table-specs
"Count and print the number of specializations of cxtables"
,(lambda (args) (set! opt-count-table-specs #t) args))
(--ctime
"Print compilation time after execution"
,(lambda (args) (set! opt-ctime #t) args))
;; TODO: switch to default when fully implemented ?
(--enable-continuation-propagation
"Disable continuation (cn-num) propagation"
,(lambda (args) (set! opt-propagate-continuation #t) args))
(--enable-int-unboxing
"Enable automatic integer unboxing based on type specialization"
,(lambda (args) (set! opt-int-unboxing #t) args))
(--disable-entry-points
"Disable the use of multiple entry points use only one generic entry point"
,(lambda (args) (if opt-call-max-len (error "--call-max-len requires interprocedural extensions"))
(set! opt-entry-points #f) args))
(--disable-float-unboxing
"Disable automatic float unboxing based on type specialization"
,(lambda (args) (set! opt-float-unboxing #f) args))
(--disable-free-versioning
"Disable versioning based on free variables"
,(lambda (args) (set! opt-free-versioning #f) args))
(--disable-inlined-call
"Disable lazy call inlining when callee identity is known. Lazy call inlining causes function duplication which is
not necessarily expected if --disable-entry-points is provided"
,(lambda (args) (set! opt-lazy-inlined-call #f) args))
(--disable-pair-tag
"Use the genereric tag TAG_MEMOBJ for pairs instead of the specific TAG_PAIR"
,(lambda (args) (set! opt-disable-pair-tag #t) args))
(--enable-regalloc-inlined-call
"TODO"
,(lambda (args) (set! opt-regalloc-inlined-call #t) args))
(--disable-return-points
"Disable the use of multiple return points use only one generic return point"
,(lambda (args) (set! opt-return-points #f) args))
(--enable-const-vers
"Enable Interprocedural versioning based on constants"
,(lambda (args) (set! opt-const-vers #t) args))
(--enable-cxoverflow-fallback
"Enable automatic fallback to generic entry/return point when cxtable overflows, default throws an error"
,(lambda (args) (set! opt-overflow-fallback #t) args))
(--disable-cxoverflow-closest
"Use the closest ctx associated to an existing slot of the cx table when the table oferflows (if possible) instead of using generic ctx"
,(lambda (args) (set! opt-closest-cx-overflow #f) args))
(--help
"Print help"
,(lambda (args)
(print-help)
(exit 0)))
(--dump-binary
"Dump machine code block"
,(lambda (args) (set! opt-dump-bin #t)
args))
(--export-locat-info
"Export locat info so it can be used by locatview tool"
,(lambda (args) (set! opt-export-locat-info #t)
args))
(--nan-boxing
"Enable nan boxing"
,(lambda (args) (set! opt-nan-boxing #t)
args))
(--nolib
"Do not include the standard library"
,(lambda (args) (set! opt-use-lib #f)
args))
(--inlining-limit
"Control gambit frontend 'inlining-limit' delcaration"
,(lambda (args) (set! opt-inlining-limit (string->number (cadr args)))
(cdr args)))
(--static-pass
"Enable static pass"
,(lambda (args) (set! opt-static-pass #t)
args))
(--stats
"Print stats about execution"
,(lambda (args) (if opt-time (error "--stats option can't be used with --time"))
(set! opt-stats #t)
args))
(--stats-full
"Print full stats about execution"
,(lambda (args) (if opt-time (error "--stats-full option can't be used with --time"))
(set! opt-stats #t)
(set! opt-count-fnargs #t)
(set! opt-count-table-specs #t)
(set! opt-stats-full #t)
args))
(--time
"Print exec time information"
,(lambda (args) (if opt-stats (error "--time option can't be used with --stats"))
(if opt-stats-full (error "--time option can't be used with --stats-full"))
(set! opt-time #t)
args))
(--verbose-jit
"Set jit as verbose"
,(lambda (args) (set! opt-verbose-jit #t) args))
))
(define (parse-args args)
(cond ;;
((null? args) '())
;;
((not (eq? (string-ref (car args) 0) #\-))
(cons (car args) (parse-args (cdr args))))
;;
(else
(let ((opt (assq (string->symbol (car args)) compiler-options)))
(if opt
(let ((fn (caddr opt)))
(set! args (fn args)))
(error "Unknown argument " (car args))))
(parse-args (cdr args)))))
;;-----------------------------------------------------------------------------
(define (lazy-exprs exprs succ)
(let ((lazy-final
(make-lazy-code
(make-lco-id 1001)
(lambda (cgc ctx)
;; Update gambit heap ptr from LC heap ptr
(x86-mov cgc (x86-rax) (x86-imm-int (get-hp-addr)))
(x86-mov cgc (x86-mem 0 (x86-rax)) alloc-ptr)
;; Write NULL in lc_stack_ptr of lc global ctx
;; to stop scanning lc stack when gc is triggered
(let ((addr (get_lc_stack_ptr_addr)))
(x86-mov cgc (x86-rax) (x86-imm-int addr))
(x86-mov cgc (x86-rbx) (x86-imm-int 0))
(x86-mov cgc (x86-mem 0 (x86-rax)) (x86-rbx)))
;; Set rax to 0 (return value)
(x86-mov cgc (x86-rax) (x86-imm-int 0))
(if (> (ctx-ffs ctx) 0)
(x86-add cgc (x86-rsp) (x86-imm-int (* 8 (ctx-ffs ctx)))))
;; Restore registers values from pstack
(ppop-regs-reverse cgc all-regs)
(ppop-xmm cgc)
(x86-ret cgc)))))
(cond ((null? exprs) succ)
((and (= (length exprs) 1)
(not succ))
(gen-ast (car exprs) lazy-final))
(else
(let ((next (lazy-exprs (cdr exprs) succ)))
(gen-ast (car exprs)
(make-lazy-code
(make-lco-id 1002)
(lambda (cgc ctx)
(jump-to-version cgc
next
(ctx-pop ctx))))))))))
;;-----------------------------------------------------------------------------
;; Bash mode
(define (exec prog)
(define (one-exec)
(set! from-space init-from-space)
(set! to-space init-to-space)
(##machine-code-block-exec mcb))
(define (run-dynamic-pass lco)
(strat-switch-mode 'dynamic)
(gen-version-first lco (ctx-init)))
(define (run-static-pass lco)
;; save options
(define tmp-propagate-continuation opt-propagate-continuation)
(define tmp-opt-const-vers opt-const-vers)
(define tmp-max-versions opt-max-versions)
(println "Running static BBV...")
;; Set static config
(set! opt-static-mode #t)
(set! opt-propagate-continuation #t)
(set! opt-const-vers #t)
(set! opt-max-versions #f)
(strat-switch-mode 'static)
(gen-version-first lco (ctx-init))
(println "done!")
; Reset cc/cr tables
(set! global-cc-table (make-table test: equal?))
(set! global-cr-table (make-table))
(set! all-crtables (make-table test: eq?))
(set! all-cctables (make-table test: eq?))
;; restore options
(set! opt-static-mode #f)
(set! opt-propagate-continuation tmp-propagate-continuation)
(set! opt-const-vers tmp-opt-const-vers)
(set! opt-max-versions tmp-max-versions))
(init-backend)
(let ((lco (lazy-exprs prog #f)))
(run-add-to-ctime
(lambda ()
(if opt-static-pass
(run-static-pass lco))
(run-dynamic-pass lco))))
(if opt-time
(begin (##machine-code-block-exec mcb)
(set! lco #f)
(set! all-lazy-code #f)
(set! asc-cc-stub #f)
(set! asc-entry-load #f)
(set! ctime-entries #f)
(set! stub-freelist #f)
(if opt-nan-boxing
(u64vector-fill! ustack #xFFFE000000000000)
(u64vector-fill! ustack 0))
(if opt-nan-boxing
(u64vector-fill! globals-space #xFFFE000000000000)
(vector-fill! globals-space 0))
(##gc)
(time (##machine-code-block-exec mcb)
(current-output-port)))
(##machine-code-block-exec mcb)))
;;-----------------------------------------------------------------------------
;; Main
;; Run command
(define (run path . args)
(let* ((port
(open-process (list path: path
arguments: args
;;stderr-redirection: #t
)))
(output
(read-line port #f))
(status
(process-status port)))
(close-port port)
(cons status output)))
(define (copy-with-declare src dst)
(let* ((declare-pre "(declare (standard-bindings) (extended-bindings) ")
(declare-inlining
(if opt-inlining-limit
(string-append "(inlining-limit " (number->string opt-inlining-limit) ")")
""))
(declare-suf " (not inline-primitives) (block) (not safe))")
(declare-all (string-append declare-pre declare-inlining declare-suf)))
(run "./copy-with-declare.sh" src dst (if opt-use-lib "lib" "nolib") declare-all)))
(define (main . args)
;; Set options and get files from cl args
(define files #f)
;; Extend compiler options using strat options
(set! compiler-options
(append compiler-options
(strat-get-options)))
(set! files (parse-args args))
(init-frontend)
(cond ;; If no files specified then start REPL
; ((null? files)
; (copy-with-declare "" "./tmp")
; (let ((content (c#expand-program "./tmp" #f locat-table)))
; (repl (expand-tl content))))
;; Can only exec 1 file
((= (length files) 1)
(copy-with-declare (car files) "./tmp")
(let ((content (c#expand-program "./tmp" #f locat-table))) ;(read-all (open-input-file (car files)))))
(let ((exp-content (expand-tl content)))
;; If we want to count fnargs, inject the ##register-lc-call
;; primitive in lambdas
(if opt-count-fnargs
(set! exp-content (count-fnargs-inject exp-content)))
(analyses-find-global-types! exp-content)
(analyses-a-conversion! exp-content)
(compute-liveness exp-content)
(exec exp-content))))
(else
(print-help)
(exit 0)))
(rt-print-opts)
(print-opts)
0)
;; TODO
(define (get-versions-info lazy-codes)
(define (get-versions-info-h lcs min max)
(if (null? lcs)
(cons min max)
(let* ((lc (car lcs))
(nb (lazy-code-nb-real-versions lc)))
(cond ((< nb min)
(get-versions-info-h (cdr lcs) nb max))
((> nb max)
(get-versions-info-h (cdr lcs) min nb))
(else
(get-versions-info-h (cdr lcs) min max))))))
(if (null? lazy-codes)
(cons 0 0)
(let* ((lc (car lazy-codes))
(nb (lazy-code-nb-real-versions lc)))
(get-versions-info-h (cdr lazy-codes) nb nb))))
;; TODO
(define (get-versions-info-full lazy-codes)
(define table (make-table))
(define (get-versions-info-full-h lcs)
(if (null? lcs)
#t
(let* ((lco (car lcs))
(nb-versions (lazy-code-nb-real-versions lco))
(r (table-ref table nb-versions (cons 0 '()))))
(table-set!
table nb-versions
(cons (+ (car r) 1)
(cons (lazy-code-flags lco) (cdr r))))
(get-versions-info-full-h (cdr lcs)))))
(get-versions-info-full-h lazy-codes)
(table->list table))
;;-----------------------------------------------------------------------------
(define (rt-print-opts)
(if opt-stats
(begin (println "Closures: " (get-slot 'closures))
(println "Executed tests: " (get-slot 'tests))
(println "Flonum boxing operations: " (if opt-nan-boxing "N/A" (get-slot 'flbox)))
(println "Flonum unboxing operations: " (if opt-nan-boxing "N/A" (get-slot 'flunbox)))
(println "Bytes allocated: " (get-slot 'allocbytes)))))
(define (print-opts)
(if opt-ctime
(print-ctime))
(if opt-stats
(print-stats))
(if opt-count-fnargs
(print-count-fnargs))
(if opt-count-table-specs
(print-count-table-specs))
(if opt-stats-full
(print-stats-full))
(if opt-export-locat-info
(export-locat-info))
(if (and opt-code-size (not opt-stats))
(print-code-size))
(if opt-dump-bin
(print-mcb)))
(define (print-ctime)
(println
"Compilation time:"
(* real-compilation-time 1000.0))) ;; print in ms
(define (print-mcb)
(let ((f (open-output-file "dump.bin")))
(define (print-mcb-h pos lim)
(if (= pos lim)
(newline)
(let ((byte (get-u8 pos)))
(write-char (integer->char byte) f)
(print-mcb-h (+ pos 1) lim))))
(print-mcb-h code-addr code-alloc)
(println ">> Dump written in dump.bin")
(close-output-port f)))
(define (print-code-size)
(let ((code-bytes (- code-alloc code-addr)))
(println "Code size (bytes): " code-bytes)))
(define (print-stats)
;; Print stats report
(let ((code-bytes (- code-alloc code-addr))
(stub-bytes (- (+ ssb-addr ssb-len) stub-alloc)))
;; Code size
(print-code-size)
;; Stub size
(println "Stub size (bytes): " stub-bytes)
;; Code + Stub size
(println "Total size (bytes): " (+ code-bytes stub-bytes))
;; Global cx-table size
(println "Global cc table size: " (table-length global-cc-table))
(println "Global cr table size: " (table-length global-cr-table))
;; Number of cc tables
(println "Number of cctables: " (cctables-total))
;; Number of cr tables
(println "Number of crtables: " (crtables-total))
;; CC table space (kb)
(print "CC table space (bytes): ")
(println (* (table-length global-cc-table) 8 (cctables-total)))
;; CR table space (kb)
(print "CR table space (bytes): ")
(println (* 16 8 (crtables-total)))
;; Min/Max versions number of stubs
(let ((versions-info (get-versions-info all-lazy-code)))
(println "Min versions number: " (car versions-info))
(println "Max versions number: " (cdr versions-info)))
(println "-------------------------")))
(define (print-count-fnargs)
(define (print block block-len)
(let ((max-index
(let loop ((i (- block-len 1)))
(if (not (= (u64vector-ref block i) 0))
i
(loop (- i 1))))))
(let loop ((i 0))
(if (<= i max-index)
(begin (println i ': (u64vector-ref block i))
(loop (+ i 1)))))))
(println "-------------------------")
(println "Number of call executed per number of arguments")
(println "#args:#calls")
(print count-fnargs-block count-fnargs-block-len)
(println "-------------------------")
(println "Number of call executed per number of arguments (rest)")
(println "#args:#calls")
(print count-fnargsr-block count-fnargs-block-len)
(println "-------------------------"))
(define (print-count-table-specs)
(define (get-cxtable-spec tables)
(define rtable '())
(define (add nspec)
(let ((r (assoc nspec rtable)))
(if r
(set-cdr! r (+ (cdr r) 1))
(set! rtable (cons (cons nspec 1) rtable)))))
(let ((lsts (table->list tables)))
(map (lambda (el)
(let ((nspec (table-length (cdr el))))
(add nspec)))
lsts))
rtable)
(define (export cxtables)
(let* ((cc (get-cxtable-spec cxtables))
(smax (apply max (map car cc))))
(let loop ((i 0))
(if (<= i smax)
(let ((r (assoc i cc)))
(println i ":" (or (and r (cdr r)) 0))
(loop (+ i 1)))))))
(println "-------------------------")
(println "Number of cctables per number of specialized tables")
(println "#spec:#tables")
(export all-cctables)
(println "-------------------------")
(println "Number of crtables per number of specialized tables")
(println "#spec:#tables")
(export all-crtables)
(println "-------------------------"))
(define (print-stats-full)
;; Classification stats
(let loop ((slots stats-slots))
(if (not (null? slots))
(let ((atx (caar slots))
(val (u64vector-ref block (cdar slots))))
(cond ((eq? atx ATX_ALL) #f)
((eq? atx ATX_NUM) (println "prim_num:" val))
(else (println "prim_" (caaar slots) ":" val)))
(loop (cdr slots)))))
;; Lco num stats
(println "Number of stubs for each number of version")
(println "#versions;#stubs;#ret;#entry;#cont;#cond")
(let ((versions-info-full (get-versions-info-full all-lazy-code)))
(for-each (lambda (n)
(println (car n) ";"
(cadr n) ";"
(count (cddr n) (lambda (n) (member 'ret n))) ";"
(count (cddr n) (lambda (n) (member 'entry n))) ";"
(count (cddr n) (lambda (n) (member 'cont n))) ";"
(count (cddr n) (lambda (n) (member 'cond n)))))
(sort versions-info-full (lambda (n m) (< (car n) (car m)))))
(println "-------------------------"))
;; Lco ids stats
(println "Number of lco for each lco-id")
(println "#lco-id;#lco")
(let (;; find max lco-id
(max-id (foldr (lambda (l r)
(define ast (lazy-code-ast l #t))
(if (and (pair? ast) (eq? (car ast) '##lco-id))
(max (cdr ast) r)
r))
0
all-lazy-code)))
;; for each lco-id, count # of lco and add it to lst if # != 0
(let loop ((i 0) (lst '()))
(if (> i max-id)
;; Sort by lco-id and print
(for-each (lambda (p)
(if (< (car p) 10) (print " "))
(if (< (car p) 100) (print " "))
(if (< (car p) 1000) (print " "))
(println (car p) ":" (cdr p)))
(reverse (sort lst (lambda (n m) (< (cdr n) (cdr m))))))
(let ((n (count all-lazy-code (lambda (lco) (equal? (lazy-code-ast lco #t) `(##lco-id . ,i))))))
(if (> n 0)
(loop (+ i 1) (cons (cons i n) lst))
(loop (+ i 1) lst)))))))
;;-----------------------------------------------------------------------------
;; Locat infos
(define (export-locat-info)
(define restable '())
(define (restable-add locat lco)
(set! restable
(cons (cons locat lco)
restable)))
;;---------------------------------------------------------------------------
;; Locat formatter to output data so that locat tool can use it (tools/locatview)
(define locat-formatted-str "")
(define asc-linecol-n (make-table)) ;; associate a integer n to a "line.col" string
(define (next-linecol-n lin col)
(let* ((str (string-append (number->string lin) "." (number->string col)))
(r (table-ref asc-linecol-n str 0)))
(table-set! asc-linecol-n str (+ r 1))
r))
(define (print-array-item i)
(print "\"" i "\"" ","))
(define (format-code-header)
(print "var code = "))
(define (format-code-footer)
(println ";"))
(define (format-locat-header)
(println "var locat_info = {"))
(define (format-locat-footer)
(println "}"))
(define (format-n-versions n)
(print-array-item "~# versions")
(print-array-item n))
(define (format-serial s)
(print-array-item "~# serial")
(print-array-item s))
(define (format-ctxs versions)
(define (format-ctx ctx n)
;; Ctx id
(print-array-item (string-append "~version"))
;; Stack
(print-array-item
(string-append
"v"
(number->string n)
": "
(with-output-to-string '()
(lambda ()
(for-each
(lambda (stype)
(if (ctx-type-cst? stype)
(print (ctx-type-symbol stype) "(" (ctx-type-cst stype) ") ")
(print (ctx-type-symbol stype) " ")))
(ctx-stack ctx))))))
;; Slot loc
; (print-array-item
; (string-append
; "Reg-alloc -> "
; (with-output-to-string '()
; (lambda () (display (ctx-slot-loc ctx))))))
; (print-array-item
; (string-append
; "env -> "
; (with-output-to-string '()
; (lambda () (display (ctx-env ctx)))))))
)
(let loop ((versions versions)
(n 1))
(if (not (null? versions))
(let ((ctx (car versions)))
;(print-array-item (string-append "ctx" (number->string n)))
(format-ctx ctx n)
(loop (cdr versions) (+ n 1))))))
(define (format-entry lin col lco)
(let ((n (next-linecol-n lin col)))
(print " \"" lin "." col "." n "\"" ": [")
(format-n-versions (lazy-code-nb-real-versions lco))
(format-serial (##object->serial-number lco))
(format-ctxs (lazy-code-versions-ctx lco))
(println "],")))
;; For each lco
(for-each
(lambda (x)
(let ((ast (lazy-code-ast x)))
;; If an ast is associated to the lco and there is 1 or more versions
(if (and ast (> (lazy-code-nb-real-versions x) 0))
;; Then, if a locat object is associated to this ast, add versions number
(let ((r (table-ref locat-table ast #f)))
(if r
(restable-add r x))))))
all-lazy-code)
;; Format locat info for jsview (see tools/)
;; Add locat_info var to string
(let ((file (open-output-file '(path: "./tools/locatview/locat.js" char-encoding: UTF-8)))
(output
(with-output-to-string '()
(lambda ()
;; Write locat info
(format-locat-header)
(for-each
(lambda (x)
(let* ((locat (car x))
(file (vector-ref locat 0))
(lin (+ 1 (bitwise-and (vector-ref locat 1) (- (expt 2 16) 1))))
(col (+ 1 (arithmetic-shift (vector-ref locat 1) -16))))
(format-entry lin col (cdr x))))
restable)
(format-locat-footer)
;; Write code
(let* ((port (open-input-file '(path: "./tmp" char-encoding: UTF-8)))
(code (read-line port #f)))
(close-input-port port)
(format-code-header)
(write code)
(format-code-footer))))))
;;
(display output file)
(force-output file)
(close-output-port file)))