-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.scm
2197 lines (1895 loc) · 86.2 KB
/
core.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;---------------------------------------------------------------------------
;;
;; Copyright (c) 2015, Baptiste Saleil. All rights reserved.
;;
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions are
;; met:
;; 1. Redistributions of source code must retain the above copyright
;; notice, this list of conditions and the following disclaimer.
;; 2. Redistributions in binary form must reproduce the above copyright
;; notice, this list of conditions and the following disclaimer in the
;; documentation and/or other materials provided with the distribution.
;; 3. The name of the author may not be used to endorse or promote
;; products derived from this software without specific prior written
;; permission.
;;
;; THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
;; WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
;; MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
;; NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
;; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
;; NOT LIMITED TO PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
;; THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;
;;---------------------------------------------------------------------------
(include "~~lib/_asm#.scm")
(include "~~lib/_x86#.scm")
(include "~~lib/_codegen#.scm")
;;-----------------------------------------------------------------------------
(include "config.scm")
(include "x86-debug.scm")
;;--------------------------------------------------------------------------------
;; Compiler options
(define opt-static-mode #f)
(define opt-static-pass #f)
(define opt-inlining-limit #f) ;; Control gambit inlining-limit declaration
(define opt-ctime #f) ;; Print compilation time
(define opt-stats #f) ;; Print stats report
(define opt-stats-full #f) ;; Print full stats report
(define opt-export-locat-info #f) ;; Pretty print number of versions for each locat object
(define opt-time #f) ;; Print exec time in processor cycles
(define opt-verbose-jit #f) ;; JIT Verbose debugging
(define opt-entry-points #t) ;; Use multiple entry points (#t to use cc-tables, #f to use flat closures)
(define opt-return-points #t) ;; Use multiple return points (#t to use cr-tables, #f to use a generic return point)
(define opt-overflow-fallback #f) ;; Automatic fallback to generic entry point if cctable overflows
(define opt-use-lib #t) ;; Use scheme std lib (see lib/ folder)
(define opt-vers-regalloc #t) ;; Use register allocation for code specialization
(define opt-dump-bin #f) ;; Print generated binary bytes to stdout
(define opt-cc-max #f) ;; Global cctable max size
(define opt-cr-max #f) ;; Global crtable max size
(define opt-const-vers #f) ;; Use cst information in code versioning
(define opt-call-max-len #f) ;; Max number of args allowed when using a specialized entry point (use a generic ep if nb-args > opt-call-max-len)
(define opt-closest-cx-overflow #t) ;; Use the closest ctx associated to an existing slot of the cx table when the table oferflows (if possible) instead of using generic ctx
(define opt-lazy-inlined-call #t) ;; The function body is inlined at a call if (1) the identity of the callee is known, (2) there is no version of the function for this ctx
(define opt-nan-boxing #f) ;; Use nan boxing istead of tagging to encode values
(define opt-float-unboxing #t) ;; Use automatic float unboxing based on type specialization
(define opt-int-unboxing #f) ;; TODO
(define opt-disable-pair-tag #f) ;; Use the generic TAG_MEMOBJ tag for pairs instead of the specific TAG_PAIR tag
(define opt-count-fnargs #f) ;; Count and print the number of arguments used at function calls
(define opt-free-versioning #t) ;; Use free variable information in code versioning
(define opt-count-table-specs #f) ;; Count and print the number of specializations of cxtables
(define opt-code-size #f) ;; Print generated code size after execution
(define opt-propagate-continuation #f) ;; TODO
(define opt-regalloc-inlined-call #f) ;; TODO
;; This is the list of cst types used for versioning (if opt-const-vers is #t)
;; All csts types are enabled by default
(define opt-cv-preds
(list ctx-type-cha? ctx-type-voi?
ctx-type-nul? ctx-type-int?
ctx-type-boo? ctx-type-pai?
ctx-type-vec? ctx-type-fec?
ctx-type-str? ctx-type-sym?
ctx-type-flo? ctx-type-clo?))
;; Is the type a cst used for versioning ?
(define (const-versioned? type)
(and opt-const-vers
(ctx-type-cst? type)
(let loop ((preds opt-cv-preds))
(if (null? preds)
#f
(or ((car preds) type)
(loop (cdr preds)))))))
;; Macro to compute compilation time
(define real-compilation-time 0.0)
(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))))
;;-----------------------------------------------------------------------------
;; Forward declarations
(define x86-upush #f)
(define x86-upop #f)
(define x86-upush-l #f)
(define x86-upop-l #f)
(define x86-ppush #f)
(define x86-ppop #f)
(define x86-usp #f)
(define x86-pcall #f)
(define asc-entry-load-get #f)
(define asc-entry-load-clear #f)
(define asc-globalfn-entry-get #f)
(define global-closures-get #f)
(define gen-closure #f)
(define init-c #f)
(define get___heap_limit-addr #f)
(define get___alloc_still-addr #f)
(define get-pstate-addr #f)
(define run-gc #f) ;; mem.scm
(define immediate-to-xmm #f)
(define expand-tl #f) ;; expand.scm
(define gen-ast #f) ;; ast.scm
(define alloc-ptr #f)
(define global-ptr #f)
(define entry-points-locs #f)
(define codegen-loc-to-x86opnd #f)
(define codegen-test-fixnum #f)
(define atom-node-make #f)
(define mlc-gambit-call #f)
(define get-heap_limit-addr #f)
(define get-hp-addr #f)
(define global-offset #f)
(define selector-reg #f)
(define x86-call-label-aligned-ret #f)
(define codegen-freg-to-x86reg #f)
(define OFFSET_FLONUM #f)
(define gen-allocation-imm #f)
(define codegen-prologue-rest> #f)
(define gen-drop-int #f)
(define gen-drop-float #f)
(define asc-cnnum-table-get #f)
(define make-lco-id #f)
(define is-lco-id? #f)
(define codegen-test-value #f)
(define codegen-test-mem-obj #f)
(define codegen-test-value #f)
(define codegen-test-char #f)
(define codegen-box-int #f)
(define write_lc_global #f)
(define write_lc_stack #f)
(define write_lc_stack_ptr #f)
(define write_lc_stack_desc #f)
(define write_lc_stack_usedesc #f)
(define write_desc_intraprocedural #f)
(define write_stubs_limits #f)
(define get_lc_stack_ptr_addr #f)
(define block_gc #f)
(define unblock_gc #f)
(define selector-init-val #f)
;;-----------------------------------------------------------------------------
;; Object life
(define LIFE_MOVE 0)
(define LIFE_STILL 5)
(define LIFE_PERM 6)
;; Tags
(define TAG_NUMBER 0)
(define TAG_MEMOBJ 1)
(define TAG_SPECIAL 2)
(define TAG_PAIR 3)
;; Char mask is 100000...00011
(define SPECIAL_MASK (bitwise-not (- (expt 2 63) 4)))
;; STags
(define STAG_VECTOR 0)
(define STAG_PAIR 1)
(define STAG_MOBJECT 5) ;; TODO: rename to STAG_BOX
(define STAG_SYMBOL 8)
(define STAG_PROCEDURE 14)
(define STAG_IPORT 17)
(define STAG_OPORT 18)
(define STAG_STRING 19)
(define STAG_F64VECTOR 29)
(define STAG_FLONUM 30)
;;-----------------------------------------------------------------------------
;; ctx-types jit implementation
;; Generic ctx-type instances
;; Used to avoid type creation for simple operations
;; MUST NOT BE MUTATED
;; NOTE: we need to reorganize 'primitives' data structure in ast.scm
;; to replace ATX_* uses by symbols uses to identify types
(define ATX_ALL 'CTX_SPECIAL_ALL) ; Represents all ctx types
(define ATX_NUM 'CTX_SPECIAL_NUM) ; Represents number types
(define ATX_UNK (make-ctx-tunk))
(define ATX_CHA (make-ctx-tcha))
(define ATX_VOI (make-ctx-tvoi))
(define ATX_NUL (make-ctx-tnul))
(define ATX_RET (make-ctx-tret))
(define ATX_INT (make-ctx-tint))
(define ATX_BOO (make-ctx-tboo))
(define ATX_BOX (make-ctx-tbox))
(define ATX_PAI (make-ctx-tpai))
(define ATX_VEC (make-ctx-tvec))
(define ATX_FEC (make-ctx-tfec))
(define ATX_STR (make-ctx-tstr))
(define ATX_SYM (make-ctx-tsym))
(define ATX_IPO (make-ctx-tipo))
(define ATX_FLO (make-ctx-tflo))
(define ATX_OPO (make-ctx-topo))
(define ATX_CLO (make-ctx-tclo))
(define ATX_TYPE_LIST
(list ATX_ALL ATX_NUM ATX_UNK ATX_CHA ATX_VOI
ATX_NUL ATX_RET ATX_INT ATX_BOO ATX_BOX
ATX_PAI ATX_VEC ATX_FEC ATX_STR ATX_SYM
ATX_IPO ATX_FLO ATX_OPO ATX_CLO))
(define (ctx-type->stag type)
(cond ((ctx-type-box? type) STAG_MOBJECT)
((ctx-type-pai? type) STAG_PAIR)
((ctx-type-vec? type) STAG_VECTOR)
((ctx-type-fec? type) STAG_F64VECTOR)
((ctx-type-str? type) STAG_STRING)
((ctx-type-sym? type) STAG_SYMBOL)
((ctx-type-flo? type) STAG_FLONUM)
((ctx-type-clo? type) STAG_PROCEDURE)
(else (pp type) (error "Internal error (ctx-type->stag)"))))
;;-----------------------------------------------------------------------------
;; Errors
(define ERR_MSG "!!! ERROR - EXEC ERROR")
(define ERR_ARR_OVERFLOW "!!! ERROR - ARITHMETIC OVERFLOW")
(define ERR_WRONG_NUM_ARGS "!!! ERROR - WRONG NUMBER OF ARGUMENTS")
(define ERR_OPEN_INPUT_FILE "!!! ERROR - CAN'T OPEN INPUT FILE")
(define ERR_OPEN_OUTPUT_FILE "!!! ERROR - CAN'T OPEN OUTPUT FILE")
(define ERR_READ_CHAR "!!! ERROR - CAN'T READ CHAR")
(define ERR_WRITE_CHAR "!!! ERROR - CAN'T WRITE CHAR")
(define ERR_DIVIDE_ZERO "!!! ERROR - DIVIDE BY ZERO")
(define ERR_INTERNAL "!!! ERROR - INTERNAL ERROR")
(define ERR_BEGIN "!!! ERROR - ILL-FORMED BEGIN")
(define ERR_LET "!!! ERROR - ILL-FORMED LET")
(define ERR_LET* "!!! ERROR - ILL-FORMED LET*")
(define ERR_LETREC "!!! ERROR - ILL-FORMED LETREC")
(define ERR_CHECK "!!! ERROR - CHECK FAILED")
(define (ERR_TYPE_EXPECTED type)
(string-append (string-upcase (ctx-type-symbol type))
" EXPECTED"))
(define ERR_NUMBER_EXPECTED "NUMBER EXPECTED")
(define (ERR_UNKNOWN_VAR var)
(if (string? var)
(string-append "Can't find variable: " var)
(string-append "Can't find variable: " (symbol->string var))))
;;-----------------------------------------------------------------------------
(define ENCODING_VOID -18) ;; encoded VOID
(define ENCODING_EOF -14) ;; encoded EOF
;;--------------------------------------------------------------------------------
;; Runtime error
;; Return lazy code object which generates an error with 'msg'
(define (get-lazy-error msg)
(make-lazy-code
(make-lco-id 104)
(lambda (cgc ctx)
(gen-error cgc msg))))
(define (gen-error cgc err #!optional (stop-exec? #t))
;; Put error msg in RAX
(let ((r1 (car regalloc-regs)))
(x86-upush cgc r1)
(x86-mov cgc r1 (x86-imm-int (obj-encoding err 1)))
(x86-pcall cgc label-rt-error-handler)
(x86-upop cgc r1)))
;; The procedure exec-error is callable from generated machine code.
;; This function print the error message in rax
(c-define (rt-error usp psp) (long long) void "rt_error" ""
(block_gc 0)
(let ((msg (encoding-obj (get-i64 (+ usp (reg-sp-offset-r (x86-rbx)))))))
(if (not (equal? msg ""))
(println msg))
(exit 0)))
;;-----------------------------------------------------------------------------
(c-define (gambit-process-statistics usp psp) (long long) void "gambit_process_statistics" ""
(block_gc 7)
(let ((v (##process-statistics)))
(put-i64 (+ usp 88)
(##object->encoding v)))
(unblock_gc))
(c-define (gambit-str-to-sym-tag usp psp) (long long) void "gambit_str_to_sym_tag" ""
(block_gc 1)
(let* ((encoding48 (get-u48 (+ usp 88)))
(str (##encoding->object encoding48))
(sym (string->symbol str)))
(put-i64 (+ usp 88)
(##object->encoding sym)))
(unblock_gc))
(c-define (gambit-str-to-sym-nan usp psp) (long long) void "gambit_str_to_sym_nan" ""
(block_gc 2)
(let* ((encoding48 (get-u48 (+ usp 88)))
(str (##encoding->object (+ encoding48 TAG_MEMOBJ)))
(sym (string->symbol str)))
(put-i64 (+ usp 88)
(##object->encoding sym)))
(unblock_gc))
(c-define (gambit-call usp psp) (long long) void "gambit_call" ""
(block_gc 3)
(write_lc_stack_ptr usp)
(write_lc_stack_usedesc 5)
(assert (<= (encoding-obj (get-i64 (+ usp (* (+ n-regalloc-regs 4) 8)))) 2)
"Unsupported number of arguments in gambit-call primitive")
(let* ((nargs
(encoding-obj (get-i64 (+ usp (* (+ n-regalloc-regs 4) 8)))))
(op-sym
(let* ((woffset (+ usp (* (+ n-regalloc-regs 3) 8)))
(getter (lambda () (get-u64 woffset)))
(word (get-u64 woffset)))
(if opt-nan-boxing
(nanboxing-encoding-obj word getter)
(tagging-encoding-obj word)))))
(let ((retval
(cond ((= nargs 0)
(unblock_gc)
(apply (eval op-sym) '()))
((= nargs 1)
(let ((a1 (tagging-encoding-obj (get-u64 (+ usp (* (+ n-regalloc-regs 5) 8))))))
(unblock_gc)
(apply (eval op-sym) (list a1))))
((= nargs 2)
(let ((a1 (tagging-encoding-obj (get-u64 (+ usp (* (+ n-regalloc-regs 6) 8)))))
(a2 (tagging-encoding-obj (get-u64 (+ usp (* (+ n-regalloc-regs 5) 8))))))
(unblock_gc)
(apply (eval op-sym) (list a1 a2))))
(else
(error "Invalid number of arguments in gambit$$ primitive")))))
(put-i64 (+ usp (* 8 (+ (length regalloc-regs) 1)))
(obj-encoding retval 4)))))
;;-----------------------------------------------------------------------------
;; The procedures do-callback* are callable from generated machine code.
;; RCX holds selector (CL)
(c-define (do-callback usp psp) (long long) void "do_callback" ""
(block_gc 4)
(write_lc_stack_ptr usp)
(write_lc_stack_usedesc 3)
(write_lc_stack_desc (vector-ref (get-scmobj (get-i64 psp)) 1))
(unblock_gc)
(let* ((ret-addr (get-i64 psp))
(callback-fn
(vector-ref (get-scmobj ret-addr) 0))
(selector
(tagging-encoding-obj (get-u48 (- psp 16))))
(new-ret-addr
(run-add-to-ctime
(lambda ()
(callback-fn ret-addr selector)))))
;; replace return address
(put-i64 psp
new-ret-addr)
;; reset selector
(put-i64 (+ usp (selector-sp-offset))
selector-init-val)
(write_lc_stack_usedesc 0)))
;; Same behavior as 'do-callback' but calls callback with call site addr
;; The call code call the stub, then the stub call 'do-callback-fn'
;; Here is the stack when stub calls 'do-callback-fn' :
;; +---------------+
;; | Saved reg n | <<-- RSP
;; +---------------+
;; | Saved reg ... |
;; +---------------+
;; | Saved reg 1 |
;; +---------------+
;; | Return addr | (return addr for do-callback-fn function)
;; +---------------+
;; | Call ctx id |
;; +---------------+
;; | Return addr | (continuation addr)
;; +---------------+
;; | Closure |
;; +---------------+
;; | Call arg n |
;; +---------------+
;; | Call arg ... |
;; +---------------+
;; | Call arg 1 |
;; +---------------+
(c-define (do-callback-fn usp psp) (long long) void "do_callback_fn" ""
(block_gc 5)
(write_lc_stack_ptr usp)
(write_lc_stack_usedesc 1)
(if (or (not opt-entry-points)
(= (tagging-encoding-obj (get-u48 (- psp 16))) 1))
;; TODO: move in ctx
(let* ((nargs (encoding-obj (get-i64 (+ usp (reg-sp-offset-r (x86-rdi))))))
(fs (+ (max (- nargs nb-args-regs) 0) 1))
(fo (arithmetic-shift (- (expt 2 fs) 1) 8))
(desc (+ fs fo)))
(write_lc_stack_desc desc))
(let ((cc-idx (encoding-obj (get-i64 (+ usp (reg-sp-offset-r (x86-r11)))))))
(write_lc_stack_desc (get-cc-gc-map-desc cc-idx))))
(unblock_gc)
(let* ((ret-addr (get-i64 psp))
(selector
(tagging-encoding-obj (get-u48 (- psp 16))))
(cc-idx
(if opt-entry-points
(encoding-obj (get-i64 (+ usp (reg-sp-offset-r (x86-r11)))))
#f))
(cc-idx-data
(if (or (not opt-entry-points) (= selector 1))
#f
(cctable-get-data cc-idx)))
(stack (and cc-idx-data (cdr cc-idx-data)))
(cn-num (and cc-idx-data (car cc-idx-data)))
;; Closure is used as a Gambit procedure to keep an updated reference
(closure
(and (not opt-entry-points)
(let ((u48 (get-u48 (+ usp (reg-sp-offset-r (x86-rsi))))))
(if opt-nan-boxing
(let ((tag (get-msb-u16 (+ usp (reg-sp-offset-r (x86-rsi))))))
(if (= tag NB_MASK_MEM_UNSHIFTED)
(##encoding->object (+ u48 TAG_MEMOBJ))
#f))
(if (= (bitwise-and u48 #b11) TAG_MEMOBJ)
(##encoding->object u48)
#f)))))
(callback-fn
(vector-ref (get-scmobj ret-addr) 0))
(new-ret-addr
(run-add-to-ctime
(lambda ()
(callback-fn stack cc-idx cn-num ret-addr selector closure)))))
;; replace return address
(put-i64 psp new-ret-addr)
;; reset selector
(put-i64 (+ usp (selector-sp-offset))
selector-init-val)
(write_lc_stack_usedesc 0)))
;(println "exit do_callback_fn")))
;; The procedures do-callback* are callable from generated machine code.
;; RCX holds selector (CL)
(c-define (do-callback-cont usp psp) (long long) void "do_callback_cont" ""
(block_gc 6)
(write_lc_stack_ptr usp)
(if (##bignum? (get-u48 (- psp 16))) (error "N1"))
(if (= (tagging-encoding-obj (get-u48 (- psp 16))) 1)
(error "NYI"))
(if (##bignum? (get-i64 (+ usp (reg-sp-offset-r (x86-r11)))))
(error "N2"))
(let ((cr-idx (encoding-obj (get-i64 (+ usp (reg-sp-offset-r (x86-r11)))))))
(write_lc_stack_ptr usp)
(write_lc_stack_usedesc 2)
(write_lc_stack_desc (get-cr-gc-map-desc cr-idx)))
(unblock_gc)
(let* ((ret-addr (get-i64 psp))
(callback-fn
(vector-ref (get-scmobj ret-addr) 0))
(selector
(tagging-encoding-obj (get-u48 (- psp 16))))
(type-idx
(encoding-obj (get-i64 (+ usp (reg-sp-offset-r (x86-r11))))))
(type
(if (or (not opt-return-points) (= selector 1))
#f
(crtable-get-data type-idx)))
(new-ret-addr
(run-add-to-ctime
(lambda ()
(callback-fn ret-addr selector type)))))
;; replace return address
(put-i64 psp
new-ret-addr)
;; reset selector
(put-i64 (+ usp (selector-sp-offset))
selector-init-val)
(write_lc_stack_usedesc 0)))
;(println "exit do_callback_cont")))
;;-----------------------------------------------------------------------------
;; Create label used by generated machine code to call c-define functions
(define-macro (set-cdef-label! label sym c-code)
`(set! ,label
(asm-make-label
cgc
,sym
(##foreign-address
((c-lambda ()
(pointer void)
,c-code))))))
(define (init-labels cgc)
(set-cdef-label! label-rt-error 'rt_error "___result = ___CAST(void*,rt_error);")
(set-cdef-label! label-gambit-call 'gambit_call "___result = ___CAST(void*,gambit_call);")
(set-cdef-label! label-gambit-process-statistics 'gambit_process_statistics "___result = ___CAST(void*,gambit_process_statistics);")
(set-cdef-label! label-gambit-str-to-sym-tag 'gambit_str_to_sym_tag "___result = ___CAST(void*,gambit_str_to_sym_tag);")
(set-cdef-label! label-gambit-str-to-sym-nan 'gambit_str_to_sym_nan "___result = ___CAST(void*,gambit_str_to_sym_nan);")
(set-cdef-label! label-do-callback 'do_callback "___result = ___CAST(void*,do_callback);")
(set-cdef-label! label-do-callback-fn 'do_callback_fn "___result = ___CAST(void*,do_callback_fn);")
(set-cdef-label! label-do-callback-cont 'do_callback_cont "___result = ___CAST(void*,do_callback_cont);"))
;;-----------------------------------------------------------------------------
;; Machine code block management
;; CODE
(define code-len 6000000)
(define code-addr #f)
(define mcb #f)
;; STUB SPACE
(define ssb-len 6000000)
(define ssb-addr #f)
(define ssb #f)
;; User stack (ustack) is the used by generated machine code
;; This stack is a scheme object. This allows the Gambit GC to scan the stack
;; to find roots.
;; Process stack (pstack) is still used for each call to c code (stubs and others)
(define ustack #f)
(define ustack-init #f) ;; initial sp value (right of the stack)
(define ustack-len 1000000) ;; 1M
(define (init-mcb)
(set! mcb (make-mcb code-len))
(set! ssb (make-mcb ssb-len))
(set! code-addr (##foreign-address mcb))
(set! ssb-addr (##foreign-address ssb))
(write_stubs_limits ssb-addr (+ ssb-addr ssb-len))
(begin (set! ustack (make-u64vector ustack-len #xFFFE000000000000))
(write_lc_stack (object-address ustack)))
(set! ustack-init (+ (object-address ustack) 8 (* 8 ustack-len))))
;; BLOCK :
;; 0 8 (nb-globals * 8 + 8)
;; +----------+----------+----------+----------+
;; | Bottom | Global 1 | ... | Global n |
;; | stack | | | |
;; | addr | | | |
;; +----------+----------+----------+----------+
(define globals-space #f)
(define globals-len 10000) ;; 1024 globals
(define globals-addr #f)
(define block #f)
(define block-addr #f)
(define stats-slots
(let loop ((idx 15) (atxs ATX_TYPE_LIST))
(if (null? atxs)
'()
(cons (cons (car atxs) idx)
(loop (+ idx 1) (cdr atxs))))))
(define debug-slots
(append '((calls . 6) (tests . 7) (extests . 8) (closures . 9) (time . 10) (other . 11) (flbox . 12) (flunbox . 13) (allocbytes . 14))
stats-slots))
(define block-len (+ 6 (length debug-slots)))
(define count-fnargs-block-len 25)
(define count-fnargs-block #f)
(define count-fnargs-block-addr #f)
;; rest
(define count-fnargsr-block #f)
(define count-fnargsr-block-addr #f)
(define (init-block)
(if opt-nan-boxing
(begin (set! globals-space (make-u64vector globals-len #xFFFE000000000000))
(write_lc_global (object-address globals-space)))
(set! globals-space (alloc-still-vector-i64 globals-len 0)))
(set! globals-addr (+ (object-address globals-space) 8))
(set! block (alloc-perm-u64vector block-len))
(assert (perm-object? block) "Init error, block should be allocated as a permanent object")
(set! block-addr (+ (object-address block) 8))
(if opt-count-fnargs
(begin
(set! count-fnargs-block (alloc-perm-u64vector count-fnargs-block-len))
(set! count-fnargsr-block (alloc-perm-u64vector count-fnargs-block-len))
(set! count-fnargs-block-addr (+ (object-address count-fnargs-block) 8))
(set! count-fnargsr-block-addr (+ (object-address count-fnargsr-block) 8))))
(let loop ((i 0))
(if (< i (u64vector-length block))
(begin (put-i64 (+ block-addr (* i 8)) 0)
(loop (+ i 1))))))
(define (write-mcb code start)
(let ((len (u8vector-length code)))
(let loop ((i (fx- len 1)))
(if (fx>= i 0)
(begin
(##machine-code-block-set! mcb (fx+ start i) (u8vector-ref code i))
(loop (fx- i 1)))
mcb))))
(define (code-gen arch addr gen #!optional ctx)
(define (gen-code cgc)
(let ((code (asm-assemble-to-u8vector cgc)))
(if opt-verbose-jit
(begin
(println "------------------------------------------------------------------------")
(asm-display-listing cgc (current-output-port) #t)
(force-output)))
(write-mcb code (- addr code-addr))
(u8vector-length code)))
(let* ((cgc (make-codegen-context))
(endianness 'le))
(asm-init-code-block cgc addr endianness)
(codegen-context-listing-format-set! cgc 'nasm)
(x86-arch-set! cgc arch)
;; If the code-gen function is called to generate function stub, use ctx
(if ctx
(gen cgc ctx)
(gen cgc))
(if opt-static-mode
0
(gen-code cgc))))
;;-----------------------------------------------------------------------------
;; Code and stub management
(define code-alloc #f)
(define stub-alloc #f)
(define stub-freelist #f)
(define (init-code-allocator)
(init-block)
(init-mcb)
(set! code-alloc code-addr)
(set! stub-alloc (+ ssb-addr ssb-len))
(set! stub-freelist 0))
(define (code-add gen)
(let ((len (code-gen 'x86-64 code-alloc gen)))
(set! code-alloc (+ code-alloc len))))
(define (stub-add max-selector gen)
(let* ((alloc
(if (= stub-freelist 0)
(begin
(set! stub-alloc (- stub-alloc 24))
stub-alloc)
(let ((a stub-freelist))
(set! stub-freelist (get-i64 a))
a)))
(stub-labels
'()))
(code-gen
'x86-64
alloc
(lambda (cgc)
(let loop ((i max-selector))
(let ((label
(asm-make-label
#f
(string->symbol
(string-append "stub_"
(number->string alloc 16)
"_"
(number->string i))))))
(set! stub-labels (cons label stub-labels))
(x86-label cgc label)
(if (> i 0)
(begin
(x86-add cgc selector-reg (x86-imm-int (tagging-obj-encoding 1))) ;; increment selector
(loop (- i 1))))))
(gen cgc)))
stub-labels))
(define (create-stub label-handler max-selector . args)
(let* ((len
(length args))
(obj
(alloc-still-vector len))
(stub-labels
(stub-add
max-selector
(lambda (cgc)
(call-handler cgc label-handler obj)))))
(subvector-move! (list->vector args) 0 len obj 0)
;(if opt-verbose-jit
; (pp (list 'obj= obj)))
stub-labels))
(define (call-handler cgc label-handler obj)
(x86-pcall cgc label-handler)
(asm-64 cgc (tagging-obj-encoding obj 9)))
(define (stub-reclaim stub-addr)
(put-i64 stub-addr stub-freelist)
(set! stub-freelist stub-addr))
;;-----------------------------------------------------------------------------
(define label-heap-limit-handler #f)
(define label-alloc-still-handler #f)
(define label-gambit-call-handler #f)
(define label-gambit-process-statistics-handler #f)
(define label-gambit-str-to-sym-handler #f)
(define label-do-callback-handler #f)
(define label-do-callback-fn-handler #f)
(define label-do-callback-cont-handler #f)
(define label-rt-error-handler #f)
(define label-err-wrong-num-args #f)
(define (gen-addr-handler cgc id addr cargs-generator)
(let ((label-handler (asm-make-label cgc id)))
(x86-label cgc label-handler)
;; Save regalloc-regs to ustack because stub could trigger GC,
;; then registers are treated as stack roots
;; Save selector to allow stub to access its value
(upush-pop-regs
cgc
(cons selector-reg regalloc-regs)
(lambda (cgc)
;; 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)
;; Set usp as the first c-arg
(x86-mov cgc (x86-rdi) (x86-usp))
;; Save ustack ptr to pstack
(x86-ppush cgc (x86-usp))
;; Save c caller save registers
(ppush-pop-regs
cgc
(set-sub c-caller-save-regs regalloc-regs)
(lambda (cgc)
(ppush-pop-xmm
cgc
xmm-regs
(lambda (cgc)
(cargs-generator cgc) ;; Gen c args
;; Aligned call to addr
(x86-mov cgc (x86-rax) (x86-rsp)) ;; align stack-pointer for C call
(x86-and cgc (x86-rsp) (x86-imm-int -16))
(x86-sub cgc (x86-rsp) (x86-imm-int 8))
(x86-ppush cgc (x86-rax))
(x86-mov cgc (x86-rax) (x86-imm-int addr))
(x86-pcall cgc (x86-rax))
(x86-ppop cgc (x86-rsp))))))
;; Update LC heap ptr and heap limit from Gambit heap ptr and heap limit
(let ((r1 selector-reg)
(r2 alloc-ptr))
;; heap limit
(x86-mov cgc r1 (x86-imm-int (get-heap_limit-addr)))
(x86-mov cgc r1 (x86-mem 0 r1))
(x86-mov cgc r2 (x86-imm-int block-addr))
(x86-mov cgc (x86-mem (* 8 5) r2) r1)
;; hp
(x86-mov cgc alloc-ptr (x86-imm-int (get-hp-addr)))
(x86-mov cgc alloc-ptr (x86-mem 0 alloc-ptr)))
;; Set rsp to saved ustack sp
(x86-ppop cgc (x86-usp))))
label-handler))
(define (gen-handler cgc id label)
(let ((label-handler (asm-make-label cgc id)))
(x86-label cgc label-handler)
;; Save regalloc-regs to ustack because stub could trigger GC,
;; then registers are treated as stack roots
;; Save selector to allow stub to access its value
(upush-pop-regs
cgc
(cons selector-reg regalloc-regs)
(lambda (cgc)
;; 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)
;; Set usp & psp as the first & second c-args
(x86-mov cgc (x86-rdi) (x86-usp)) ;; NOTE: will not be correctly restored if rdi is not a reg-alloc reg
(x86-mov cgc (x86-rsi) (x86-rsp)) ;; NOTE: will not be correctly restored if rsi is not a reg-alloc reg
;; Save ustack ptr to pstack
(x86-ppush cgc (x86-usp))
;; Save c caller save registers
(ppush-pop-regs
cgc
(set-sub c-caller-save-regs regalloc-regs)
(lambda (cgc)
(ppush-pop-xmm
cgc
xmm-regs
(lambda (cgc)
;; Aligned call to label
(x86-mov cgc (x86-rax) (x86-rsp)) ;; align stack-pointer for C call
(x86-and cgc (x86-rsp) (x86-imm-int -16))
(x86-sub cgc (x86-rsp) (x86-imm-int 8))
(x86-ppush cgc (x86-rax))
(x86-pcall cgc label)
(x86-ppop cgc (x86-rsp))))))
;; Update LC heap ptr and heap limit from Gambit heap ptr and heap limit
(let ((r1 selector-reg)
(r2 alloc-ptr))
;; heap limit
(x86-mov cgc r1 (x86-imm-int (get-heap_limit-addr)))
(x86-mov cgc r1 (x86-mem 0 r1))
(x86-mov cgc r2 (x86-imm-int block-addr))
(x86-mov cgc (x86-mem (* 8 5) r2) r1)
;; hp
(x86-mov cgc alloc-ptr (x86-imm-int (get-hp-addr)))
(x86-mov cgc alloc-ptr (x86-mem 0 alloc-ptr)))
;; Set rsp to saved ustack sp
(x86-ppop cgc (x86-usp))))
label-handler))
(define (init-rtlib cgc)
(let ((label-rtlib-skip (asm-make-label cgc 'rtlib_skip)))
(x86-jmp cgc label-rtlib-skip)
;; heap_limit
(set! label-heap-limit-handler
(gen-addr-handler cgc 'heap_limit_handler (get___heap_limit-addr) (lambda (cgc) #f)))
(x86-ret cgc)
;; heap_limit
(set! label-alloc-still-handler
(gen-addr-handler cgc 'alloc_still_handler (get___alloc_still-addr)
(lambda (cgc)
;; rdi rsi rdx (pstate, stag, len)
;;(x86-mov cgc (x86-rdi) (x86-imm-int 0))
(let* (;; NOTE: Must match the registers saved using ppush-pop-regs in gen-addr-handler
(saved-offset (+ (length xmm-regs) (length (set-sub c-caller-save-regs regalloc-regs))))
(stag-offset (* 8 (+ saved-offset 2))))
;; stag is not encoded, then it is in pstack
;;alloc_still_handler
(let ((addr (get_lc_stack_ptr_addr)))
(x86-mov cgc (x86-rax) (x86-imm-int addr))
(x86-mov cgc (x86-mem 0 (x86-rax)) (x86-usp)))
(x86-mov cgc (x86-rdi) (x86-mem stag-offset (x86-rsp)))
(x86-mov cgc (x86-rsi) (x86-mem (* 8 (+ (length regalloc-regs) 2)) (x86-rbp)))))))
;(x86-mov cgc (x86-rdi) (x86-imm-int (get-pstate-addr)))))))
(x86-ret cgc)
(set! label-gambit-call-handler
(gen-handler cgc 'gambit_call_handler label-gambit-call))
(x86-ret cgc)
(set! label-gambit-process-statistics-handler
(gen-handler cgc 'gambit_process_statistics label-gambit-process-statistics))
(x86-ret cgc)
(set! label-gambit-str-to-sym-handler
(if opt-nan-boxing
(gen-handler cgc 'gambit_str_to_sym_handler label-gambit-str-to-sym-nan)
(gen-handler cgc 'gambit_str_to_sym_handler label-gambit-str-to-sym-tag)))
(x86-ret cgc)
;; do_callback
(set! label-do-callback-handler
(gen-handler cgc 'do_callback_handler label-do-callback))
(x86-ret cgc)
;; do_callback_fn
(set! label-do-callback-fn-handler
(gen-handler cgc 'do_callback_fn_handler label-do-callback-fn))
(x86-ret cgc)
;; do_callback_cont
(set! label-do-callback-cont-handler
(gen-handler cgc 'do_callback_cont_handler label-do-callback-cont))
(x86-ret cgc)
;; Runtime error
(set! label-rt-error-handler
(gen-handler cgc 'rt_error_handler label-rt-error))
(x86-ret cgc)
(set! label-err-wrong-num-args (asm-make-label #f 'err_wrong_num_args))
(x86-label cgc label-err-wrong-num-args)
(gen-error cgc ERR_WRONG_NUM_ARGS)
;; -------------------------
(x86-label cgc label-rtlib-skip)
;; Save all regs to pstack (because the GC must *not* scan these values)
(ppush-xmm cgc)
(ppush-regs cgc all-regs)
;; Set usp to ustack init
(x86-mov cgc (x86-usp) (x86-imm-int ustack-init))
;; Put heaplimit in heaplimit slot
;; TODO: remove cst slots
(x86-mov cgc (x86-rax) (x86-imm-int block-addr))
(x86-mov cgc (x86-rcx) (x86-imm-int (get-heap_limit-addr)))
(x86-mov cgc (x86-rcx) (x86-mem 0 (x86-rcx)))
(x86-mov cgc (x86-mem (* 8 5) (x86-rax)) (x86-rcx))
(x86-mov cgc (x86-rcx) (x86-imm-int selector-init-val))
;; Heap addr in alloc-ptr
(x86-mov cgc alloc-ptr (x86-imm-int (get-hp-addr)))
(x86-mov cgc alloc-ptr (x86-mem 0 alloc-ptr))
(x86-mov cgc global-ptr (x86-imm-int globals-addr)) ;; Globals addr in r10
;; Set all registers used for regalloc to 0
(for-each (lambda (el)
(x86-mov cgc el (x86-imm-int (obj-encoding 0))))
regalloc-regs)
(let ((label (asm-make-label #f (new-sym 'prog_begin))))
(x86-label cgc label))))
(define (init-backend)
(if opt-disable-pair-tag
(set! TAG_PAIR TAG_MEMOBJ))
(write_desc_intraprocedural (not opt-return-points))
(init-c)
(init-code-allocator)
(init-interprocedural)
(init-values)
(code-add
(lambda (cgc)
(init-labels cgc)
(init-rtlib cgc))))
;;-----------------------------------------------------------------------------
(define (upush-pop-regs cgc regs proc)
(x86-upush-l cgc regs)