-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcodegen.scm
3569 lines (3077 loc) · 141 KB
/
codegen.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 "config.scm")
(include "~~lib/_x86#.scm")
(include "~~lib/_asm#.scm")
(define-macro (on-dynamic-mode expr)
`(if (not opt-static-mode)
,expr))
(define-macro (l32 loc)
`(if (x86-reg? ,loc)
(x86-r64->r32 ,loc)
,loc))
;;-----------------------------------------------------------------------------
;; x86-push/pop redef
(define x86-ppush #f)
(define x86-ppop #f)
(define x86-upush #f)
(define x86-upush #f)
(define (x86-upush-l cgc opnds)
(x86-sub cgc (x86-usp) (x86-imm-int (* 8 (length opnds))))
(let loop ((offset (* 8 (- (length opnds) 1)))
(l opnds))
(if (not (null? l))
(begin (x86-mov cgc (x86-mem offset (x86-usp)) (car l))
(loop (- offset 8) (cdr l))))))
(define (x86-upop-l cgc opnds)
(let loop ((offset 0)
(l opnds))
(if (not (null? l))
(begin (x86-mov cgc (car l) (x86-mem offset (x86-usp)))
(loop (+ offset 8) (cdr l)))))
(x86-add cgc (x86-usp) (x86-imm-int (* 8 (length opnds)))))
(let ((gpush x86-push)
(gpop x86-pop)
(push/pop-error
(lambda n
(error "Internal error, do *NOT* directly use x86-push/pop functions."))))
(set! x86-ppop gpop)
(set! x86-ppush gpush)
(set! x86-upush
(lambda (cgc op)
(cond ((x86-imm? op)
(x86-sub cgc (x86-usp) (x86-imm-int 8))
(x86-mov cgc (x86-mem 0 (x86-usp)) op 64))
((x86-mem? op)
;; TODO
(x86-ppush cgc (x86-rax))
(x86-mov cgc (x86-rax) op)
(x86-sub cgc (x86-usp) (x86-imm-int 8))
(x86-mov cgc (x86-mem 0 (x86-usp)) (x86-rax))
(x86-ppop cgc (x86-rax)))
(else
(x86-sub cgc (x86-usp) (x86-imm-int 8))
(x86-mov cgc (x86-mem 0 (x86-usp)) op)))))
(set! x86-upop
(lambda (cgc op)
(x86-mov cgc op (x86-mem 0 (x86-usp)))
(x86-add cgc (x86-usp) (x86-imm-int 8))))
(set! x86-push push/pop-error)
(set! x86-pop push/pop-error))
;;-----------------------------------------------------------------------------
;; x86-call redef
;; x86-call function produce an error.
;; x86-call could generate a call with a non aligned return address
;; which may cause trouble to the gc
;; Use specialized *x86-call-label-unaligned-ret* and *x86-call-label-aligned-ret* instead
;; x86-call-label-aligned-ret is a ucall: store ret addr in ustack and align return address
;; x86-call-label-unaligned-ret is a pcall: store ret addr in pstack and does not change return address
;; Generate a call to a label with a return address not necessarily aligned to 4
(define x86-call-label-unaligned-ret #f)
(define (gen-x86-error-call)
(lambda (cgc opnd)
(error "Internal error, do *NOT* directly use x86-call function.")))
;; Generate a call to a label with a return address aligned to 4
;; (end with 00 which is the integer tag)
(define (gen-x86-aligned-call call-fn)
(lambda (cgc label)
(define align-mult 4) ;; tag 00 (int tag)
(define call-size 5) ;; Call to a label is a *CALL rel32* which is a 5 bytes instruction on x86_64
(define opnop #x90) ;; NOP opcode
(define nop-needed 0) ;; Number of NOP needed to align return address
(asm-at-assembly
cgc
(lambda (cb self)
(let ((ex (modulo (+ self call-size) align-mult)))
(if (> ex 0)
(set! nop-needed (- align-mult ex))
(set! nop-needed 0))
nop-needed))
(lambda (cb self)
(let loop ((i nop-needed))
(if (> i 0)
(begin (asm-8 cb opnop)
(loop (- i 1)))))))
(call-fn cgc label)))
;; Redefine calls
(set! x86-pcall x86-call)
(let ((gambit-call x86-call))
(set! x86-call (gen-x86-error-call)))
;(set! x86-call-label-unaligned-ret gambit-call)
;(set! x86-call-label-aligned-ret (gen-x86-aligned-call gambit-call)))
;;-----------------------------------------------------------------------------
;; x86 Registers
;; x86 registers map associate virtual register to x86-register
(define codegen-regmap
(foldr (lambda (el r)
(cons (cons (cons 'r el)
(list-ref regalloc-regs el))
r))
'()
(build-list (length regalloc-regs) (lambda (l) l))))
(define alloc-ptr (x86-r9))
(define global-ptr (x86-r8))
(define selector-reg (x86-rcx))
(define selector-init-val #x0001000000000000)
(define tmp-reg (x86-rax))
(define selector-reg-32 (x86-ecx))
(define (x86-usp) (x86-rbp)) ;; user stack pointer is rbp
(define (x86-r64->r32 r64)
(assert (x86-reg64? r64) "Internal error")
(cond ((eq? r64 (x86-rax)) (x86-eax))
((eq? r64 (x86-rbx)) (x86-ebx))
((eq? r64 (x86-rcx)) (x86-ecx))
((eq? r64 (x86-rdx)) (x86-edx))
((eq? r64 (x86-rsi)) (x86-esi))
((eq? r64 (x86-rdi)) (x86-edi))
((eq? r64 (x86-rbp)) (x86-ebp))
((eq? r64 (x86-rsp)) (x86-esp))
((eq? r64 (x86-r8 )) (x86-r8d))
((eq? r64 (x86-r9 )) (x86-r9d))
((eq? r64 (x86-r10)) (x86-r10d))
((eq? r64 (x86-r11)) (x86-r11d))
((eq? r64 (x86-r12)) (x86-r12d))
((eq? r64 (x86-r13)) (x86-r13d))
((eq? r64 (x86-r14)) (x86-r14d))
((eq? r64 (x86-r15)) (x86-r15d))))
;; NOTE: temporary register is always rax
;; NOTE: selector is always rcx
;; Offsets
(define OFFSET_PAIR_CAR 16)
(define OFFSET_PAIR_CDR 8)
(define OFFSET_PROC_EP 8)
(define OFFSET_CONT_EP 24)
(define OFFSET_CONT_GENERIC 16)
(define OFFSET_BOX 8)
(define OFFSET_FLONUM 8)
(define OFFSET_BODY 8)
(define (OFFSET_PROC_FREE i) (+ 16 (* 8 i)))
;;-----------------------------------------------------------------------------
;; x86 Codegen utils
(define-macro (neq? l r)
`(not (eq? ,l ,r)))
(define (know-dest-symbol dest-addr)
(let ((str (string-append "dest_"
(number->string dest-addr 16)
"_")))
(new-sym (string->symbol str))))
(define (int32? n)
(and (integer? n)
(>= n (expt -2 31))
(< n (expt 2 31))))
(define (codegen-set-bool cgc b reg)
(let ((dest (codegen-reg-to-x86reg reg)))
(x86-mov cgc dest (x86-imm-int (obj-encoding b 1)))))
(define (codegen-loc-to-x86opnd fs ffs loc)
(cond ((eq? loc 'selector)
selector-reg)
((eq? loc 'tmp)
tmp-reg)
((ctx-loc-is-register? loc)
(codegen-reg-to-x86reg loc))
((ctx-loc-is-memory? loc)
(codegen-mem-to-x86mem fs loc))
((ctx-loc-is-fregister? loc)
(codegen-freg-to-x86reg loc))
(else (error "Internal error"))))
(define (codegen-mem-to-x86mem fs mem)
(x86-mem (* 8 (- fs (cdr mem) 1)) (x86-usp)))
(define (codegen-reg-to-x86reg reg)
(cond ((eq? reg 'selector) selector-reg)
((eq? reg 'tmp) tmp-reg)
(else
(cdr (assoc reg codegen-regmap)))))
(define (codegen-freg-to-x86reg freg)
(list-ref regalloc-fregs (cdr freg)))
(define (codegen-is-imm-64? imm)
(or (< imm -2147483648)
(> imm 2147483647)))
(define (immediate-to-xmm cgc xmm flo)
(if (= flo 0.0)
(x86-pxor cgc xmm xmm)
(let* ((rep (flonum->ieee754 flo 'double))
(rl (and (not (= rep 0)) (xmm_imm_shift rep)))) ;; right left
(if rl
;; We can use psrl/psll
(begin (x86-pcmpeqb cgc xmm xmm)
(and (> (car rl) 0) (x86-psrlw cgc xmm (x86-imm-int (car rl))))
(and (> (cdr rl) 0) (x86-psllw cgc xmm (x86-imm-int (cdr rl)))))
(begin (x86-mov cgc (x86-rax) (x86-imm-int (to-64-value (flonum->ieee754 flo 'double))))
(x86-movd/movq cgc xmm (x86-rax)))))))
;;-----------------------------------------------------------------------------
;; Stacks
;;-----------------------------------------------------------------------------
(define (codegen-clean-stacks cgc ustack pstack)
(if (not (= ustack 0))
(x86-add cgc (x86-usp) (x86-imm-int (* 8 ustack))))
(if (not (= pstack 0))
(x86-add cgc (x86-rsp) (x86-imm-int (* 8 pstack)))))
;;-----------------------------------------------------------------------------
;; Define
;;-----------------------------------------------------------------------------
(define (codegen-define-bind cgc fs ffs pos reg lvalue cst?)
(let ((dest (codegen-reg-to-x86reg reg))
(opval (if cst?
(x86-imm-int (obj-encoding lvalue 2))
(codegen-loc-to-x86opnd fs ffs lvalue))))
(cond ((x86-reg? opval)
(x86-mov cgc (x86-mem (* 8 pos) global-ptr) opval))
(else (x86-mov cgc (x86-rax) opval)
(x86-mov cgc (x86-mem (* 8 pos) global-ptr) (x86-rax))))
(x86-mov cgc dest (x86-imm-int (obj-encoding #!void)))))
;;-----------------------------------------------------------------------------
;; If
;;-----------------------------------------------------------------------------
(define (codegen-if cgc fs ffs label-jump label-false label-true lcond)
(let ((opcond (codegen-loc-to-x86opnd fs ffs lcond))
(immf (obj-encoding #f 3)))
(if (codegen-is-imm-64? immf)
(begin (x86-mov cgc (x86-rax) (x86-imm-int immf))
(x86-cmp cgc opcond (x86-rax)))
(x86-cmp cgc opcond (x86-imm-int immf)))
(x86-label cgc label-jump)
(x86-je cgc label-false)
(x86-jmp cgc label-true)))
(define (codegen-inlined-if cgc label-jump label-false label-true x86-op)
(x86-label cgc label-jump)
(x86-op cgc label-false)
(x86-jmp cgc label-true))
;;-----------------------------------------------------------------------------
;; Variables
;;-----------------------------------------------------------------------------
;; get global
(define (codegen-get-global cgc pos reg)
(let ((dest (codegen-reg-to-x86reg reg)))
(x86-mov cgc dest (x86-mem (* 8 pos) global-ptr))))
;; get free
;; Get free variable directly from closure.
(define (codegen-get-free cgc fs ffs reg lclo lvar)
(let ((copnd (codegen-loc-to-x86opnd fs ffs lclo))
(coffset (- (* 8 (+ (cdr lvar) 2)) TAG_MEMOBJ))
(dest (codegen-loc-to-x86opnd fs ffs reg))
(x86-op (if (ctx-loc-is-fregister? reg) x86-movsd x86-mov)))
(if opt-nan-boxing
(if (x86-reg? copnd)
(x86-op cgc dest (x86-mem (* 8 (+ (cdr lvar) 2)) copnd selector-reg))
(begin
(x86-mov cgc (x86-rax) copnd)
(x86-mov cgc dest (x86-mem (* 8 (+ (cdr lvar) 2)) (x86-rax) selector-reg))))
(if (x86-reg? copnd)
(x86-op cgc dest (x86-mem coffset copnd))
(begin
(x86-mov cgc (x86-rax) copnd) ;; Get closure
(x86-op cgc dest (x86-mem coffset (x86-rax))))))))
;; set free
(define (codegen-write-free-variable cgc fs ffs src-loc dst-pos closure-loc base-reg)
(if opt-nan-boxing
(codegen-write-free-variable-nan cgc fs ffs src-loc dst-pos closure-loc base-reg)
(codegen-write-free-variable-tag cgc fs ffs src-loc dst-pos closure-loc base-reg)))
(define (codegen-write-free-variable-nan cgc fs ffs src-loc dst-pos closure-loc base-reg)
(cond ;;
((ctx-loc-is-freemem? src-loc)
(let* ((closure-opnd (codegen-loc-to-x86opnd fs ffs closure-loc))
(fvar-pos (cdr src-loc))
(fvar-offset (+ 16 (* 8 fvar-pos)))
(treg (x86-rax)))
(if (ctx-loc-is-memory? closure-loc)
(begin (x86-mov cgc (x86-rax) closure-opnd)
(set! closure-opnd (x86-rax))))
(x86-mov cgc treg (x86-mem fvar-offset closure-opnd selector-reg))
(x86-mov cgc (x86-mem (* 8 dst-pos) base-reg) treg)))
;;
((ctx-loc-is-memory? src-loc)
(x86-mov cgc (x86-rax) (codegen-loc-to-x86opnd fs ffs src-loc))
(x86-mov cgc (x86-mem (* 8 dst-pos) base-reg) (x86-rax)))
;;
((ctx-loc-is-fregister? src-loc)
(x86-movd/movq cgc (x86-rax) (codegen-freg-to-x86reg src-loc))
(x86-mov cgc (x86-mem (* 8 dst-pos) base-reg) (x86-rax)))
;;
(else
(let ((opn (codegen-reg-to-x86reg src-loc)))
(x86-mov cgc (x86-mem (* 8 dst-pos) base-reg) opn)))))
(define (codegen-write-free-variable-tag cgc fs ffs src-loc dst-pos closure-loc base-reg)
(cond ;;
((ctx-loc-is-freemem? src-loc)
(let* ((closure-opnd (codegen-loc-to-x86opnd fs ffs closure-loc))
(fvar-pos (cdr src-loc))
(fvar-offset (+ 16 (* 8 fvar-pos))))
(if (ctx-loc-is-memory? closure-loc)
(begin (x86-mov cgc (x86-rax) closure-opnd)
(set! closure-opnd (x86-rax))))
(x86-mov cgc (x86-rax) (x86-mem (- fvar-offset TAG_MEMOBJ) closure-opnd))
(x86-mov cgc (x86-mem (* 8 dst-pos) base-reg) (x86-rax))))
;;
((ctx-loc-is-memory? src-loc)
(x86-mov cgc (x86-rax) (codegen-loc-to-x86opnd fs ffs src-loc))
(x86-mov cgc (x86-mem (* 8 dst-pos) base-reg) (x86-rax)))
;;
((ctx-loc-is-fregister? src-loc)
(assert (neq? base-reg (x86-rax)) "Internal error in codegen, unimplemented case")
(x86-movd/movq cgc (x86-rax) (codegen-freg-to-x86reg src-loc))
(x86-mov cgc (x86-mem (* 8 dst-pos) base-reg) (x86-rax)))
;;
(else
(let ((opn (codegen-reg-to-x86reg src-loc)))
(x86-mov cgc (x86-mem (* 8 dst-pos) base-reg) opn)))))
;; set global
(define (codegen-set-global cgc fs ffs reg pos lval cst?)
(let ((dest (codegen-reg-to-x86reg reg))
(opval (if cst?
(x86-imm-int (obj-encoding lval 4))
(codegen-loc-to-x86opnd fs ffs lval))))
(if (or (x86-imm? opval)
(x86-mem? opval))
(begin (x86-mov cgc (x86-rax) opval)
(set! opval (x86-rax))))
(x86-mov cgc (x86-mem (* 8 pos) global-ptr) opval)
(x86-mov cgc dest (x86-imm-int (obj-encoding #!void)))))
;;-----------------------------------------------------------------------------
;; Boxing
;;-----------------------------------------------------------------------------
(define (codegen-box-float-nan cgc gc-desc src dst)
(if (x86-mem? src)
(x86-mov cgc dst src)
(x86-movd/movq cgc dst src)))
(define (codegen-box-float-tag cgc gc-desc src dst)
(gen-allocation-imm cgc STAG_FLONUM 8 gc-desc)
(if (and (x86-reg? src) (x86-xmm? src))
(x86-movsd cgc (x86-mem (+ -16 OFFSET_FLONUM) alloc-ptr) src)
(begin
(x86-mov cgc (x86-rax) src)
(x86-mov cgc (x86-mem (+ -16 OFFSET_FLONUM) alloc-ptr) (x86-rax))))
(x86-lea cgc dst (x86-mem (- TAG_MEMOBJ 16) alloc-ptr)))
;; if float-loc? is #f, float-loc is a loc, else float-loc is an x86 opnd
(define (codegen-box-float cgc gc-desc fs ffs float-loc dest-loc #!optional (float-loc? #t))
(define opnd (if float-loc? (codegen-loc-to-x86opnd fs ffs float-loc) float-loc))
(define dest (codegen-reg-to-x86reg dest-loc))
(assert (ctx-loc-is-register? dest-loc) "Internal codegen error")
(if opt-nan-boxing
(codegen-box-float-nan cgc gc-desc opnd dest)
(codegen-box-float-tag cgc gc-desc opnd dest)))
(define (codegen-box-int-tag cgc src dst)
(if (not (eq? src dst))
(x86-mov cgc dst src))
(x86-shl cgc dst (x86-imm-int 2)))
(define (codegen-box-int cgc fs ffs int-loc dest-loc)
(define opnd (codegen-loc-to-x86opnd fs ffs int-loc))
(define dest (codegen-reg-to-x86reg dest-loc))
(assert (ctx-loc-is-register? dest-loc) "Internal codegen error")
(if opt-nan-boxing
(error "NYI")
(codegen-box-int-tag cgc opnd dest)))
;;-----------------------------------------------------------------------------
;; Functions
;;-----------------------------------------------------------------------------
;; Generate a generic function prologue without rest param
(define (codegen-prologue-gen-nrest cgc fn-nb-args)
(let ((imm (obj-encoding fn-nb-args)))
(if (codegen-is-imm-64? imm)
(begin (x86-mov cgc (x86-rax) (x86-imm-int imm))
(x86-cmp cgc (x86-rdi) (x86-rax)))
(x86-cmp cgc (x86-rdi) (x86-imm-int (obj-encoding fn-nb-args 7)))))
(x86-jne cgc label-err-wrong-num-args))
;; Generate a generic function prologue with rest param
(define (codegen-prologue-gen-rest cgc fs ffs fn-nb-args)
(if opt-nan-boxing
(codegen-prologue-gen-rest-nan cgc fs ffs fn-nb-args)
(codegen-prologue-gen-rest-tag cgc fs ffs fn-nb-args)))
(define (codegen-prologue-gen-rest-nan cgc fs ffs fn-nb-args)
(let ((nb-args-regs (length args-regs))
(label-rest (asm-make-label #f (new-sym 'prologue-rest)))
(label-rest-loop (asm-make-label #f (new-sym 'prologue-rest-loop)))
(label-rest-end (asm-make-label #f (new-sym 'prologue-rest-end)))
(label-next-arg (asm-make-label #f (new-sym 'prologue-next-arg)))
(label-from-stack (asm-make-label #f (new-sym 'prologue-from-stack)))
(label-next-arg-end (asm-make-label #f (new-sym 'prologue-next-arg-end))))
(x86-mov cgc (x86-rax) (x86-imm-int (obj-encoding (- fn-nb-args 1))))
(x86-cmp cgc (x86-rdi) (x86-rax))
(x86-jge cgc label-rest)
(gen-error cgc ERR_WRONG_NUM_ARGS)
;; Get next arg
(x86-label cgc label-next-arg)
(x86-mov cgc (x86-rax) (x86-imm-int (obj-encoding (length args-regs))))
(x86-cmp cgc (x86-rdi) (x86-rax))
(x86-jg cgc label-from-stack)
(let loop ((i (length args-regs))
(regs (reverse args-regs)))
(if (> i 0)
(begin
(x86-mov cgc (x86-rax) (x86-imm-int (obj-encoding i)))
(x86-cmp cgc (x86-rdi) (x86-rax))
(x86-mov cgc (x86-rax) (codegen-loc-to-x86opnd fs ffs (car regs)))
(x86-je cgc label-next-arg-end)
(loop (- i 1) (cdr regs)))))
(x86-jmp cgc label-next-arg-end)
(x86-label cgc label-from-stack)
(x86-upop cgc (x86-rax))
(x86-label cgc label-next-arg-end)
(x86-sub cgc (x86-rdi) (x86-imm-int 1))
(x86-ret cgc)
;; END get next arg
(x86-label cgc label-rest)
;; cdr (rax) = '()
(x86-mov cgc selector-reg (x86-imm-int (obj-encoding '() 12)))
(x86-label cgc label-rest-loop)
(x86-mov cgc (x86-rax) (x86-imm-int (obj-encoding (- fn-nb-args 1))))
(x86-cmp cgc (x86-rdi) (x86-rax))
(x86-je cgc label-rest-end)
;; Alloc
(gen-allocation-imm cgc STAG_PAIR 16 0)
(x86-pcall cgc label-next-arg)
(x86-mov cgc (x86-mem (+ -24 OFFSET_PAIR_CAR) alloc-ptr) (x86-rax))
(x86-mov cgc (x86-mem (+ -24 OFFSET_PAIR_CDR) alloc-ptr) selector-reg)
;;
(x86-mov cgc selector-reg (x86-imm-int (to-64-value NB_MASK_MEM)))
(x86-lea cgc selector-reg (x86-mem -24 alloc-ptr selector-reg))
(x86-jmp cgc label-rest-loop)
;;
(x86-label cgc label-rest-end)
(if (<= fn-nb-args (length args-regs))
(let ((reg (list-ref args-regs (- fn-nb-args 1))))
(x86-mov cgc (codegen-reg-to-x86reg reg) selector-reg))
(x86-upush cgc selector-reg))
;; Reset selector used as temporary
(x86-mov cgc selector-reg (x86-imm-int selector-init-val))))
(define (codegen-prologue-gen-rest-tag cgc fs ffs fn-nb-args)
(let ((nb-args-regs (length args-regs))
(label-rest (asm-make-label #f (new-sym 'prologue-rest)))
(label-rest-loop (asm-make-label #f (new-sym 'prologue-rest-loop)))
(label-rest-end (asm-make-label #f (new-sym 'prologue-rest-end)))
(label-next-arg (asm-make-label #f (new-sym 'prologue-next-arg)))
(label-from-stack (asm-make-label #f (new-sym 'prologue-from-stack)))
(label-next-arg-end (asm-make-label #f (new-sym 'prologue-next-arg-end))))
(x86-cmp cgc (x86-rdi) (x86-imm-int (obj-encoding (- fn-nb-args 1) 8)))
(x86-jge cgc label-rest)
(gen-error cgc ERR_WRONG_NUM_ARGS)
;; Get next arg
(x86-label cgc label-next-arg)
(x86-cmp cgc (x86-rdi) (x86-imm-int (obj-encoding (length args-regs) 9)))
(x86-jg cgc label-from-stack)
(let loop ((i (length args-regs))
(regs (reverse args-regs)))
(if (> i 0)
(begin
(x86-cmp cgc (x86-rdi) (x86-imm-int (obj-encoding i 10)))
(x86-mov cgc (x86-rax) (codegen-loc-to-x86opnd fs ffs (car regs)))
(x86-je cgc label-next-arg-end)
(loop (- i 1) (cdr regs)))))
(x86-jmp cgc label-next-arg-end)
(x86-label cgc label-from-stack)
(x86-upop cgc (x86-rax))
(x86-label cgc label-next-arg-end)
(x86-sub cgc (x86-rdi) (x86-imm-int (obj-encoding 1 11)))
(x86-ret cgc)
;; END get next arg
(x86-label cgc label-rest)
;; cdr (rax) = '()
(x86-mov cgc selector-reg (x86-imm-int (obj-encoding '() 12)))
(x86-label cgc label-rest-loop)
(x86-cmp cgc (x86-rdi) (x86-imm-int (obj-encoding (- fn-nb-args 1) 13)))
(x86-je cgc label-rest-end)
;; Alloc
(x86-mov cgc (x86-rax) (x86-imm-int (get_lc_stack_usedesc_addr)))
(x86-mov cgc (x86-mem 0 (x86-rax)) (x86-imm-int 4) 64)
;; TODO OPTIMIZE (GC MAP INTERFACE)
(let ((label-next (asm-make-label #f (new-sym 'next)))
(addr (get_lc_stack_desc_addr)))
(x86-mov cgc (x86-rax) (x86-rdi))
(x86-sub cgc (x86-rax) (x86-imm-int (* nb-args-regs 4)))
(x86-jns cgc label-next) ;; jump if negative
(x86-mov cgc (x86-rax) (x86-imm-int 0))
(x86-label cgc label-next)
(x86-upush cgc (x86-rax)))
(gen-allocation-imm cgc STAG_PAIR 16 0) ;; alloc: fn-nb-args
(x86-add cgc (x86-usp) (x86-imm-int 8))
(x86-pcall cgc label-next-arg)
(x86-mov cgc (x86-mem (+ -24 OFFSET_PAIR_CAR) alloc-ptr) (x86-rax))
(x86-mov cgc (x86-mem (+ -24 OFFSET_PAIR_CDR) alloc-ptr) selector-reg)
(x86-lea cgc selector-reg (x86-mem (+ -24 TAG_PAIR) alloc-ptr))
;;
(x86-mov cgc (x86-rax) (x86-imm-int (get_lc_stack_usedesc_addr)))
(x86-mov cgc (x86-mem 0 (x86-rax)) (x86-imm-int 0) 64)
;;
(x86-jmp cgc label-rest-loop)
;;
(x86-label cgc label-rest-end)
(if (<= fn-nb-args (length args-regs))
(let ((reg (list-ref args-regs (- fn-nb-args 1))))
(x86-mov cgc (codegen-reg-to-x86reg reg) selector-reg))
(x86-upush cgc selector-reg))
;; Reset selector used as temporary
(x86-mov cgc selector-reg (x86-imm-int selector-init-val))))
;; Generate specialized function prologue with rest param and actual == formal
(define (codegen-prologue-rest= cgc destreg)
(let ((dest
(and destreg (codegen-reg-to-x86reg destreg))))
(if dest
(x86-mov cgc dest (x86-imm-int (obj-encoding '() 15)))
(x86-upush cgc (x86-imm-int (obj-encoding '() 16))))))
;; Generate specialized function prologue with rest param and actual > formal
(define (codegen-prologue-rest> cgc gc-desc fs ffs locs rloc)
;; TODO: one allocation for list
;; TODO: use x86 loop for mem locs
(let ((dest (codegen-loc-to-x86opnd fs ffs rloc)))
(x86-mov cgc selector-reg (x86-imm-int (obj-encoding '() 17)))
(let loop ((locs locs))
(if (not (null? locs))
(let* ((loc (car locs))
(cdr-pushed? #f)
(opnd
(cond ((eq? (car loc) 'c)
(x86-imm-int (obj-encoding (cdr loc) 18)))
((eq? (car loc) 'cf)
(let ((entry-obj (car (asc-globalfn-entry-get (cdr loc)))))
(x86-upush cgc selector-reg)
(gen-closure cgc 'selector #f entry-obj '())
(set! cdr-pushed? #t)
selector-reg))
(else
(codegen-loc-to-x86opnd fs ffs loc)))))
(gen-allocation-imm cgc STAG_PAIR 16 gc-desc)
(if (or (x86-mem? opnd)
(x86-imm-int? opnd))
(begin (x86-mov cgc (x86-rax) opnd)
(x86-mov cgc (x86-mem (+ -24 OFFSET_PAIR_CAR) alloc-ptr) (x86-rax)))
(x86-mov cgc (x86-mem (+ -24 OFFSET_PAIR_CAR) alloc-ptr) opnd))
(if cdr-pushed?
(x86-upop cgc selector-reg))
(x86-mov cgc (x86-mem (+ -24 OFFSET_PAIR_CDR) alloc-ptr) selector-reg)
(if opt-nan-boxing
(begin (x86-mov cgc (x86-rax) (x86-imm-int (to-64-value NB_MASK_MEM)))
(x86-lea cgc selector-reg (x86-mem -24 alloc-ptr (x86-rax))))
(x86-lea cgc selector-reg (x86-mem (+ -24 TAG_PAIR) alloc-ptr)))
(loop (cdr locs)))))
(x86-mov cgc dest selector-reg)
(x86-mov cgc selector-reg (x86-imm-int selector-init-val))))
;; Alloc closure and write header
(define (codegen-closure-create cgc nb-free gc-desc)
(let* ((closure-size (+ 1 nb-free))) ;; entry point & free vars
;; Alloc closure
(gen-allocation-imm cgc STAG_PROCEDURE (* 8 closure-size) gc-desc)))
;; Write entry point in closure (do not use cctable)
(define (codegen-closure-ep cgc entryvec-loc nb-free)
(let ((offset (+ OFFSET_PROC_EP (* -8 (+ nb-free 2)))))
(if (int32? (+ 8 entryvec-loc))
(x86-mov cgc (x86-rax) (x86-mem (+ 8 entryvec-loc)))
(begin (x86-mov cgc (x86-rax) (x86-imm-int (+ 8 entryvec-loc)))
(x86-mov cgc (x86-rax) (x86-mem 0 (x86-rax)))))
(x86-mov cgc (x86-mem offset alloc-ptr) (x86-rax))))
;; Write cctable ptr in closure (use multiple entry points)
(define (codegen-closure-cc cgc cctable-loc nb-free)
(let ((offset (+ OFFSET_PROC_EP (* -8 (+ nb-free 2)))))
(x86-mov cgc (x86-rax) (x86-imm-int cctable-loc))
(x86-mov cgc (x86-mem offset alloc-ptr) (x86-rax))))
;; Load closure in tmp register
(define (codegen-load-closure cgc fs ffs loc)
(let ((opnd (codegen-loc-to-x86opnd fs ffs loc)))
(x86-mov cgc (x86-rax) opnd)))
;;; Push closure
(define (codegen-closure-put cgc reg nb-free)
(if opt-nan-boxing
(let ((dest (codegen-reg-to-x86reg reg))
(offset (* -8 (+ nb-free 2))))
(x86-mov cgc dest (x86-imm-int (to-64-value NB_MASK_MEM)))
(x86-lea cgc dest (x86-mem offset alloc-ptr dest)))
(let ((dest (codegen-reg-to-x86reg reg))
(offset (+ (* -8 (+ nb-free 2)) TAG_MEMOBJ)))
(x86-lea cgc dest (x86-mem offset alloc-ptr)))))
;;
(define (codegen-return-retobj cgc fs ffs lretobj)
(let ((opretobj (codegen-loc-to-x86opnd fs ffs lretobj)))
(if (neq? opretobj (x86-rdx))
(x86-mov cgc (x86-rdx) opretobj))))
;;
(define (codegen-return-clean cgc fs ffs)
(if (> fs 0)
(x86-add cgc (x86-usp) (x86-imm-int (* 8 fs))))
(if (> ffs 0)
(x86-add cgc (x86-rsp) (x86-imm-int (* 8 ffs)))))
;; Generate function return using a return address
;; Retaddr (or cctable) is in rdx
(define (codegen-return-rp cgc gc-desc fs ffs lretobj lretval float? int?)
(let ((opret (codegen-reg-to-x86reg return-reg))
(opretval (codegen-loc-to-x86opnd fs ffs lretval)))
(if (and (eq? lretobj return-reg)
(not float?))
(error "NYI, see codegen-return-cr"))
;; Move return value to return register
(cond (float?
(codegen-box-float cgc gc-desc fs ffs lretval return-reg))
(int?
(codegen-box-int cgc fs ffs lretval return-reg))
;;
(else
(if (neq? opret opretval)
(x86-mov cgc opret opretval)))))
(codegen-return-retobj cgc fs ffs lretobj)
(codegen-return-clean cgc fs ffs)
(x86-jmp cgc (x86-rdx)))
;; Generate function return using a crtable
;; Retaddr (or cctable) is in rdx
(define (codegen-return-cr cgc fs ffs lretobj lretval cridx float? cst?)
(define cont-saved? #f)
(define (move-retval)
(let ((opret (if float?
(codegen-freg-to-x86reg return-freg)
(codegen-reg-to-x86reg return-reg)))
(opretval (and (not cst?) (codegen-loc-to-x86opnd fs ffs lretval))))
(if (and opretval
(neq? opret opretval))
(if float?
(x86-movsd cgc opret opretval)
(x86-mov cgc opret opretval)))))
(assert (not (and cst? (not opt-const-vers)))
"Internal error")
;; If the continuation is in the return register,
;; save it to rax
(if (and (eq? lretobj return-reg)
(not float?))
(begin
(x86-mov cgc (x86-rax) (codegen-reg-to-x86reg return-reg))
(set! cont-saved? #t)))
(move-retval)
(if (not cont-saved?)
(codegen-return-retobj cgc fs ffs lretobj))
(codegen-return-clean cgc fs ffs)
(let ((table-reg (if cont-saved? (x86-rax) (x86-rdx))))
(if cridx
(x86-mov cgc (x86-rax) (x86-mem (+ OFFSET_CONT_EP (* 8 cridx)) table-reg))
(x86-mov cgc (x86-rax) (x86-mem OFFSET_CONT_GENERIC table-reg)))
(x86-mov cgc (x86-r11) (x86-imm-int (obj-encoding cridx 20)))
(x86-jmp cgc (x86-rax))))
;; Generate function call using a single entry point
;; eploc is the cctable or entry points if it's known
(define (codegen-call-ep cgc nb-args eploc direct-eploc)
(if nb-args ;; If nb-args given, move encoded in rdi, else nb-args is already encoded in rdi (apply)
(x86-mov cgc (x86-rdi) (x86-imm-int (obj-encoding nb-args 21))))
(cond (direct-eploc
;; If it's a direct call to a not yet generated entry point, add stub_load label
(if (eq? (car direct-eploc) 'stub)
(let ((load-label (caddr direct-eploc)))
(x86-label cgc load-label)))
(let ((label (asm-make-label #f (know-dest-symbol (cadr direct-eploc)) (cadr direct-eploc))))
(x86-jmp cgc label)))
(eploc
(error "NYI codegen-call-ep"))
;(x86-jmp cgc (x86-mem (+ eploc 8) #f)))
(else
(if opt-nan-boxing
(begin
(x86-mov cgc (x86-rdx) (x86-mem 8 (x86-rsi) selector-reg))
(x86-jmp cgc (x86-rdx)))
(begin
(x86-mov cgc (x86-rdx) (x86-mem (- 8 TAG_MEMOBJ) (x86-rsi)))
(x86-jmp cgc (x86-rdx)))))))
;; Generate function call using a cctable and generic entry point
;; eploc is the cctable or entry points if it's known
(define (codegen-call-cc-gen cgc nb-args eploc)
(if opt-nan-boxing
(codegen-call-cc-gen-nan cgc nb-args eploc)
(codegen-call-cc-gen-tag cgc nb-args eploc)))
(define (codegen-call-cc-gen-nan cgc nb-args eploc)
(if nb-args
(x86-mov cgc (x86-rdi) (x86-imm-int (obj-encoding nb-args 22))))
(if eploc
(x86-mov cgc (x86-rdx) (x86-imm-int eploc))
(x86-mov cgc (x86-rdx) (x86-mem 8 (x86-rax) selector-reg)))
(x86-jmp cgc (x86-mem 8 (x86-rdx)))) ;; Jump to generic entry point
(define (codegen-call-cc-gen-tag cgc nb-args eploc)
(if nb-args
(x86-mov cgc (x86-rdi) (x86-imm-int (obj-encoding nb-args 22))))
(if eploc
(x86-mov cgc (x86-rdx) (x86-imm-int eploc))
(x86-mov cgc (x86-rdx) (x86-mem (- 8 TAG_MEMOBJ) (x86-rsi)))) ;; Get table
(x86-jmp cgc (x86-mem 8 (x86-rdx)))) ;; Jump to generic entry point
;; Generate function call using a cctable and specialized entry point
;; eploc is the cctable or entry points if it's known
(define (codegen-call-cc-spe cgc idx nb-args eploc direct-eploc)
;; Closure is in rax
(let ((cct-offset (* 8 (+ 2 idx))))
;; 1 - Put ctx in r11
(if (or (not direct-eploc) ;; ctx needed if it's not a direct call
(neq? (car direct-eploc) 'ep)) ;; ctx needed if it's a direct call to a stub
(x86-mov cgc (x86-r11) (x86-imm-int (obj-encoding idx 23))))
;; 2 - Put nbargs in rdi if needed
(x86-mov cgc (x86-rdi) (x86-imm-int (obj-encoding nb-args)))
;; 3- Get cc-table
(cond (direct-eploc
;; If it's a direct call to a not yet generated entry point, add stub_load label
(if (eq? (car direct-eploc) 'stub)
(let ((load-label (caddr direct-eploc)))
(x86-label cgc load-label)))
(let ((label (asm-make-label #f (know-dest-symbol (cadr direct-eploc)) (cadr direct-eploc))))
(x86-jmp cgc label)))
(eploc
(x86-mov cgc (x86-rdx) (x86-imm-int eploc))
(x86-jmp cgc (x86-mem cct-offset (x86-rdx))))
(else
(if opt-nan-boxing
(x86-mov cgc (x86-rdx) (x86-mem 8 (x86-rsi) selector-reg))
(x86-mov cgc (x86-rdx) (x86-mem (- 8 TAG_MEMOBJ) (x86-rsi))))
(x86-jmp cgc (x86-mem cct-offset (x86-rdx)))))))
;; Load continuation using specialized return points
(define (codegen-load-cont-cr cgc crtable-loc)
(x86-mov cgc (x86-rax) (x86-imm-int crtable-loc))
(assert (= (modulo crtable-loc 4) 0) "Internal error")
(x86-mov cgc (x86-mem 0 (x86-usp)) (x86-rax)))
(define (codegen-load-cont-rp cgc label-load-ret label-cont-stub)
(x86-label cgc label-load-ret)
(x86-mov cgc (x86-rax) (x86-imm-int (vector-ref label-cont-stub 1)))
(assert (= (modulo (vector-ref label-cont-stub 1) 4) 0) "Internal error")
(x86-mov cgc (x86-mem 0 (x86-usp)) (x86-rax)))
(define (codegen-apply-xxx cgc fs ffs lst-loc)
(if opt-nan-boxing
(codegen-apply-xxx-nan cgc fs ffs lst-loc)
(codegen-apply-xxx-tag cgc fs ffs lst-loc)))
(define (codegen-apply-xxx-nan cgc fs ffs lst-loc)
;; r11, selector & r15 are used as tmp registers
;; It is safe because they are not used for parameters.
;; And if they are used after, they already are saved on the stack
(define label-end (asm-make-label #f (new-sym 'apply-end-args)))
(define lst-op (codegen-loc-to-x86opnd fs ffs lst-loc))
(x86-mov cgc (x86-rdx) lst-op)
(x86-mov cgc (x86-r11) (x86-imm-int 0))
(let loop ((args-regs args-regs))
(if (null? args-regs)
(let ((label-loop (asm-make-label #f (new-sym 'apply-loop-args))))
(x86-label cgc label-loop)
(x86-mov cgc (x86-rsi) (x86-imm-int (obj-encoding '())))
(x86-cmp cgc (x86-rdx) (x86-rsi))
(x86-je cgc label-end)
(x86-inc cgc (x86-r11))
(x86-mov cgc (x86-rsi) (x86-rdx))
(x86-mov cgc (x86-rdx) (x86-mem OFFSET_PAIR_CAR (x86-rsi) selector-reg))
(x86-upush cgc (x86-rdx))
(x86-mov cgc (x86-rdx) (x86-mem OFFSET_PAIR_CDR (x86-rsi) selector-reg))
(x86-jmp cgc label-loop))
(begin
(x86-mov cgc (x86-rsi) (x86-imm-int (obj-encoding '())))
(x86-cmp cgc (x86-rdx) (x86-rsi))
(x86-je cgc label-end)
(x86-inc cgc (x86-r11))
(x86-nop cgc)
(x86-mov cgc (x86-rsi) (x86-rdx))
(x86-mov cgc (codegen-loc-to-x86opnd fs ffs (car args-regs)) (x86-mem OFFSET_PAIR_CAR (x86-rsi) selector-reg))
(x86-mov cgc (x86-rdx) (x86-mem OFFSET_PAIR_CDR (x86-rsi) selector-reg))
(loop (cdr args-regs)))))
(x86-label cgc label-end)
(x86-mov cgc selector-reg (x86-imm-int (to-64-value NB_MASK_FIX)))
(x86-or cgc (x86-r11) selector-reg)
;; Reset selector used as tmp reg
(x86-mov cgc selector-reg (x86-imm-int selector-init-val)))
(define (codegen-apply-xxx-tag cgc fs ffs lst-loc)
;; r11, selector & r15 are used as tmp registers
;; It is safe because they are not used for parameters.
;; And if they are used after, they already are saved on the stack
(define label-end (asm-make-label #f (new-sym 'apply-end-args)))
(define lst-op (codegen-loc-to-x86opnd fs ffs lst-loc))
(x86-mov cgc (x86-rdx) lst-op)
(x86-mov cgc (x86-r11) (x86-imm-int 0))
(let loop ((args-regs args-regs))
(if (null? args-regs)
(let ((label-loop (asm-make-label #f (new-sym 'apply-loop-args))))
(x86-label cgc label-loop)
(x86-cmp cgc (x86-rdx) (x86-imm-int (obj-encoding '() 24)))
(x86-je cgc label-end)
(x86-add cgc (x86-r11) (x86-imm-int 4))
(x86-mov cgc selector-reg (x86-mem (- OFFSET_PAIR_CAR TAG_PAIR) (x86-rdx)))
(x86-upush cgc selector-reg)
(x86-mov cgc (x86-rdx) (x86-mem (- OFFSET_PAIR_CDR TAG_PAIR) (x86-rdx)))
(x86-jmp cgc label-loop))
(begin
(x86-cmp cgc (x86-rdx) (x86-imm-int (obj-encoding '() 25)))
(x86-je cgc label-end)
(x86-add cgc (x86-r11) (x86-imm-int 4))
(x86-mov cgc (codegen-loc-to-x86opnd fs ffs (car args-regs)) (x86-mem (- OFFSET_PAIR_CAR TAG_PAIR) (x86-rdx)))
(x86-mov cgc (x86-rdx) (x86-mem (- OFFSET_PAIR_CDR TAG_PAIR) (x86-rdx)))
(loop (cdr args-regs)))))
(x86-label cgc label-end)
;; Reset selector used as tmp reg
(x86-mov cgc selector-reg (x86-imm-int selector-init-val)))
(define (codegen-gambit-call cgc gc-desc op-symbol nb-args dest-loc)
(assert (ctx-loc-is-register? dest-loc) "Internal error")
(x86-mov cgc (x86-rax) (x86-imm-int (obj-encoding nb-args 27)))
(x86-upush cgc (x86-rax))
(x86-mov cgc (x86-rax) (x86-imm-int (obj-encoding op-symbol 28)))
(x86-upush cgc (x86-rax))
(x86-mov cgc (x86-rax) (x86-imm-int gc-desc 29))
(x86-upush cgc (x86-rax))
(x86-mov cgc (x86-rax) (x86-imm-int nb-args 30))
(x86-upush cgc (x86-rax))
(x86-pcall cgc label-gambit-call-handler)
(x86-upop cgc (codegen-reg-to-x86reg dest-loc))
(x86-add cgc (x86-usp) (x86-imm-int (* 8 (+ nb-args 3)))))
;; Shift arguments on stack for tail calls
;; starts with [usp+offset-from] -> [usp+offset-to]
(define (codegen-tail-shift cgc nb-shift offset-from offset-to)
(if (>= nb-shift 0)
(begin
(x86-mov cgc (x86-r11) (x86-mem offset-from (x86-usp)))
(x86-mov cgc (x86-mem offset-to (x86-usp)) (x86-r11))
(codegen-tail-shift cgc (- nb-shift 1) (- offset-from 8) (- offset-to 8)))))
;;-----------------------------------------------------------------------------
;; Operators
;;-----------------------------------------------------------------------------
;;-----------------------------------------------------------------------------
;; N-ary arithmetic operators
(define (codegen-num-ii cgc fs ffs op reg lleft lright lcst? rcst?)
(assert (not (and lcst? rcst?)) "Internal error")
(let ((dest (codegen-reg-to-x86reg reg))
(opleft (and (not lcst?) (codegen-loc-to-x86opnd fs ffs lleft)))
(opright (and (not rcst?) (codegen-loc-to-x86opnd fs ffs lright))))
;; Handle cases like 1. 2. etc...
(if (and lcst? (flonum? lleft))
(set! lleft (##flonum->fixnum lleft)))
(if (and rcst? (flonum? lright))
(set! lright (##flonum->fixnum lright)))
(if opt-nan-boxing
(codegen-num-ii-nb cgc op dest opleft opright lleft lright lcst? rcst?)
(codegen-num-ii-tag cgc op dest opleft opright lleft lright lcst? rcst?))))
(define (codegen-num-ii-nb cgc op dest opleft opright lleft lright lcst? rcst?)
(define (box reg)
(x86-mov cgc (x86-rax) (x86-imm-int (to-64-value NB_MASK_FIX)))
(x86-lea cgc reg (x86-mem 0 reg (x86-rax))))
(define dest32 (x86-r64->r32 dest))
(cond
(lcst?
(cond ((eq? op '+) (if (neq? dest opright)