-
-
Notifications
You must be signed in to change notification settings - Fork 645
/
jitarith.c
2425 lines (2258 loc) · 82.3 KB
/
jitarith.c
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
/*
Racket
Copyright (c) 2006-2016 PLT Design Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301 USA.
*/
#include "schpriv.h"
#include "schmach.h"
#include "future.h"
#ifdef MZ_USE_JIT
#include "jit.h"
#define JITARITH_TS_PROCS
#include "jit_ts.c"
int scheme_jit_is_fixnum(Scheme_Object *rand)
{
if (SCHEME_INTP(rand)
|| (SAME_TYPE(SCHEME_TYPE(rand), scheme_local_type)
&& (SCHEME_GET_LOCAL_TYPE(rand) == SCHEME_LOCAL_TYPE_FIXNUM)))
return 1;
else if (scheme_expr_produces_local_type(rand) == SCHEME_LOCAL_TYPE_FIXNUM)
return 1;
else
return 0;
}
static int can_reorder_unboxing(Scheme_Object *rand, Scheme_Object *rand2, int extfl)
{
/* Can we reorder `rand' and `rand2', given that we want floating-point
results (so it's ok for `rand' to be a floating-point local)? */
return scheme_is_relatively_constant_and_avoids_r1_maybe_fp(rand, rand2, 1, extfl);
}
static int is_inline_unboxable_op(Scheme_Object *obj, int flag, int unsafely, int just_checking_result, int extfl)
/* If unsafely, a result of 2 means that arguments should be checked safely. */
{
if (!SCHEME_PRIMP(obj))
return 0;
if (!(SCHEME_PRIM_PROC_OPT_FLAGS(obj) & flag))
return 0;
/* We have a table here for now, instead of flags accessed via
SCHEME_PRIM_PROC_OPT_FLAGS(), because this function reports
properties of the JIT rather than inherent properties of the
functions. */
if (!extfl) {
if (IS_NAMED_PRIM(obj, "unsafe-fl+")) return 1;
if (IS_NAMED_PRIM(obj, "unsafe-fl-")) return 1;
if (IS_NAMED_PRIM(obj, "unsafe-fl*")) return 1;
if (IS_NAMED_PRIM(obj, "unsafe-fl/")) return 1;
if (IS_NAMED_PRIM(obj, "unsafe-flabs")) return 1;
if (IS_NAMED_PRIM(obj, "unsafe-flsqrt")) return 1;
if (IS_NAMED_PRIM(obj, "unsafe-flmin")) return 1;
if (IS_NAMED_PRIM(obj, "unsafe-flmax")) return 1;
if (IS_NAMED_PRIM(obj, "unsafe-fx->fl")) return 1;
if (IS_NAMED_PRIM(obj, "unsafe-f64vector-ref")) return 1;
if (IS_NAMED_PRIM(obj, "unsafe-flvector-ref")) return 1;
if (IS_NAMED_PRIM(obj, "unsafe-flimag-part")) return 1;
if (IS_NAMED_PRIM(obj, "unsafe-flreal-part")) return 1;
if (unsafely) {
/* These are inline-unboxable when their args are
safely inline-unboxable: */
if (IS_NAMED_PRIM(obj, "fl+")) return 2;
if (IS_NAMED_PRIM(obj, "fl-")) return 2;
if (IS_NAMED_PRIM(obj, "fl*")) return 2;
if (IS_NAMED_PRIM(obj, "fl/")) return 2;
if (IS_NAMED_PRIM(obj, "flabs")) return 2;
if (IS_NAMED_PRIM(obj, "flsqrt")) return 2;
if (IS_NAMED_PRIM(obj, "flmin")) return 2;
if (IS_NAMED_PRIM(obj, "flmax")) return 2;
if (IS_NAMED_PRIM(obj, "flimag-part")) return 2;
if (IS_NAMED_PRIM(obj, "flreal-part")) return 2;
if (just_checking_result) {
if (IS_NAMED_PRIM(obj, "flfloor")) return 1;
if (IS_NAMED_PRIM(obj, "flceiling")) return 1;
if (IS_NAMED_PRIM(obj, "fltruncate")) return 1;
if (IS_NAMED_PRIM(obj, "flround")) return 1;
if (IS_NAMED_PRIM(obj, "flsin")) return 1;
if (IS_NAMED_PRIM(obj, "flcos")) return 1;
if (IS_NAMED_PRIM(obj, "fltan")) return 1;
if (IS_NAMED_PRIM(obj, "flasin")) return 1;
if (IS_NAMED_PRIM(obj, "flacos")) return 1;
if (IS_NAMED_PRIM(obj, "flatan")) return 1;
if (IS_NAMED_PRIM(obj, "fllog")) return 1;
if (IS_NAMED_PRIM(obj, "flexp")) return 1;
if (IS_NAMED_PRIM(obj, "flexpt")) return 1;
}
}
}
#ifdef MZ_LONG_DOUBLE
if (extfl) {
if (IS_NAMED_PRIM(obj, "unsafe-extfl+")) return 1;
if (IS_NAMED_PRIM(obj, "unsafe-extfl-")) return 1;
if (IS_NAMED_PRIM(obj, "unsafe-extfl*")) return 1;
if (IS_NAMED_PRIM(obj, "unsafe-extfl/")) return 1;
if (IS_NAMED_PRIM(obj, "unsafe-extflabs")) return 1;
if (IS_NAMED_PRIM(obj, "unsafe-extflsqrt")) return 1;
if (IS_NAMED_PRIM(obj, "unsafe-extflmin")) return 1;
if (IS_NAMED_PRIM(obj, "unsafe-extflmax")) return 1;
if (IS_NAMED_PRIM(obj, "unsafe-fx->extfl")) return 1;
if (IS_NAMED_PRIM(obj, "unsafe-f80vector-ref")) return 1;
if (IS_NAMED_PRIM(obj, "unsafe-extflvector-ref")) return 1;
if (unsafely) {
/* These are inline-unboxable when their args are
safely inline-unboxable: */
if (IS_NAMED_PRIM(obj, "extfl+")) return 2;
if (IS_NAMED_PRIM(obj, "extfl-")) return 2;
if (IS_NAMED_PRIM(obj, "extfl*")) return 2;
if (IS_NAMED_PRIM(obj, "extfl/")) return 2;
if (IS_NAMED_PRIM(obj, "extflabs")) return 2;
if (IS_NAMED_PRIM(obj, "extflsqrt")) return 2;
if (IS_NAMED_PRIM(obj, "extflmin")) return 2;
if (IS_NAMED_PRIM(obj, "extflmax")) return 2;
if (just_checking_result) {
if (IS_NAMED_PRIM(obj, "extflfloor")) return 1;
if (IS_NAMED_PRIM(obj, "extflceiling")) return 1;
if (IS_NAMED_PRIM(obj, "extfltruncate")) return 1;
if (IS_NAMED_PRIM(obj, "extflround")) return 1;
if (IS_NAMED_PRIM(obj, "extflsin")) return 1;
if (IS_NAMED_PRIM(obj, "extflcos")) return 1;
if (IS_NAMED_PRIM(obj, "extfltan")) return 1;
if (IS_NAMED_PRIM(obj, "extflasin")) return 1;
if (IS_NAMED_PRIM(obj, "extflacos")) return 1;
if (IS_NAMED_PRIM(obj, "extflatan")) return 1;
if (IS_NAMED_PRIM(obj, "extfllog")) return 1;
if (IS_NAMED_PRIM(obj, "extflexp")) return 1;
if (IS_NAMED_PRIM(obj, "extflexpt")) return 1;
}
}
}
#endif
return 0;
}
int scheme_generate_pop_unboxed(mz_jit_state *jitter)
{
#if defined(MZ_USE_JIT_I386)
# if 1
if (jitter->unbox_depth) {
scheme_signal_error("internal error: scheme_generate_pop_unboxed() isn't right");
}
/* The code below doesn't work right because it's emitted *before*
the test for failure. Adding it after the failure test means
moving it to (something like) sjc.unbound_global_code. Meanwhile,
the JIT doesn't currently actually try to reference globals when it has
values on the FP stack. */
# else
/* If we have some arguments pushed on the FP stack, we need
to pop them off before escaping. */
int i;
for (i = jitter->unbox_depth; i--; ) {
FSTPr(0);
}
CHECK_LIMIT();
# endif
#endif
return 1;
}
static int is_unboxing_immediate(Scheme_Object *obj, int unsafely, int extfl)
{
Scheme_Type t;
t = SCHEME_TYPE(obj);
switch (t) {
case scheme_local_type:
if (!extfl) {
if (SCHEME_GET_LOCAL_TYPE(obj) == SCHEME_LOCAL_TYPE_FLONUM)
return 1;
}
#ifdef MZ_LONG_DOUBLE
if (extfl) {
if (SCHEME_GET_LOCAL_TYPE(obj) == SCHEME_LOCAL_TYPE_EXTFLONUM)
return 1;
}
#endif
return unsafely;
case scheme_toplevel_type:
/* Can generalize to allow any toplevel if scheme_generate_pop_unboxed() is fixed */
if ((SCHEME_TOPLEVEL_FLAGS(obj) & SCHEME_TOPLEVEL_FLAGS_MASK) < SCHEME_TOPLEVEL_READY)
return 0;
return unsafely;
break;
case scheme_local_unbox_type:
return unsafely;
break;
default:
if (!unsafely) {
if (!extfl)
return SCHEME_FLOATP(obj);
#ifdef MZ_LONG_DOUBLE
if (extfl)
return SCHEME_LONG_DBLP(obj);
#endif
return 0;
}
return (t > _scheme_values_types_);
}
}
int scheme_can_unbox_inline(Scheme_Object *obj, int fuel, int regs, int unsafely, int extfl)
/* Assuming that `arg' is [unsafely] assumed to produce a flonum, can we
just unbox it without using more than `regs' registers? There
cannot be any errors or function calls, unless we've specifically
instrumented them to save/pop floating-point values before
jumping. If the result is true, then arguments must be evaluated in
order. */
{
Scheme_Type t;
if (!fuel) return 0;
if (!regs) return 0;
t = SCHEME_TYPE(obj);
switch (t) {
case scheme_application2_type:
{
Scheme_App2_Rec *app = (Scheme_App2_Rec *)obj;
int ok_op;
ok_op = is_inline_unboxable_op(app->rator, SCHEME_PRIM_IS_UNARY_INLINED, unsafely, 0, extfl);
if (!ok_op)
return 0;
else if (ok_op == 2)
unsafely = 0;
return scheme_can_unbox_inline(app->rand, fuel - 1, regs, unsafely, extfl);
}
case scheme_application3_type:
{
Scheme_App3_Rec *app = (Scheme_App3_Rec *)obj;
int ok_op;
ok_op = is_inline_unboxable_op(app->rator, SCHEME_PRIM_IS_BINARY_INLINED, unsafely, 0, extfl);
if (!ok_op)
return 0;
else if (ok_op == 2)
unsafely = 0;
if ((SCHEME_PRIM_PROC_OPT_FLAGS(app->rator) & SCHEME_PRIM_IS_BINARY_INLINED)
&& (IS_NAMED_PRIM(app->rator, "unsafe-f64vector-ref")
|| IS_NAMED_PRIM(app->rator, "unsafe-flvector-ref"))) {
if (is_unboxing_immediate(app->rand1, 1, extfl)
&& is_unboxing_immediate(app->rand2, 1, extfl)) {
return 1;
}
}
#ifdef MZ_LONG_DOUBLE
if ((SCHEME_PRIM_PROC_OPT_FLAGS(app->rator) & SCHEME_PRIM_IS_BINARY_INLINED)
&& (IS_NAMED_PRIM(app->rator, "unsafe-f80vector-ref")
|| IS_NAMED_PRIM(app->rator, "unsafe-extflvector-ref"))) {
if (is_unboxing_immediate(app->rand1, 1, extfl)
&& is_unboxing_immediate(app->rand2, 1, extfl)) {
return 1;
}
}
#endif
if (!scheme_can_unbox_inline(app->rand1, fuel - 1, regs, unsafely, extfl))
return 0;
return scheme_can_unbox_inline(app->rand2, fuel - 1, regs - 1, unsafely, extfl);
}
default:
return is_unboxing_immediate(obj, unsafely, extfl);
}
}
int can_unbox_directly(Scheme_Object *obj, int extfl, int bfuel)
/* Used only when !can_unbox_inline(). Detects safe operations that
produce flonums when they don't raise an exception, and that the JIT
supports directly unboxing. */
{
Scheme_Type t;
while (1) {
t = SCHEME_TYPE(obj);
switch (t) {
case scheme_application2_type:
{
Scheme_App2_Rec *app = (Scheme_App2_Rec *)obj;
if (is_inline_unboxable_op(app->rator, SCHEME_PRIM_IS_UNARY_INLINED, 1, 1, extfl))
return 1;
if (SCHEME_PRIMP(app->rator)
&& (SCHEME_PRIM_PROC_OPT_FLAGS(app->rator) & SCHEME_PRIM_IS_UNARY_INLINED)) {
if (!extfl) {
if (IS_NAMED_PRIM(app->rator, "->fl")
|| IS_NAMED_PRIM(app->rator, "fx->fl")
|| IS_NAMED_PRIM(app->rator, "unsafe-flrandom"))
return 1;
}
#ifdef MZ_LONG_DOUBLE
if (extfl) {
if (IS_NAMED_PRIM(app->rator, "->extfl")
|| IS_NAMED_PRIM(app->rator, "fx->extfl"))
return 1;
}
#endif
}
return 0;
}
break;
case scheme_application3_type:
{
Scheme_App3_Rec *app = (Scheme_App3_Rec *)obj;
if (is_inline_unboxable_op(app->rator, SCHEME_PRIM_IS_BINARY_INLINED, 1, 1, extfl))
return 1;
if (SCHEME_PRIMP(app->rator)
&& (SCHEME_PRIM_PROC_OPT_FLAGS(app->rator) & SCHEME_PRIM_IS_BINARY_INLINED)) {
if (!extfl) {
if (IS_NAMED_PRIM(app->rator, "flvector-ref")) return 1;
}
#ifdef MZ_LONG_DOUBLE
if (extfl) {
if (IS_NAMED_PRIM(app->rator, "extflvector-ref")) return 1;
}
#endif
}
return 0;
}
break;
case scheme_let_value_type:
obj = ((Scheme_Let_Value *)obj)->body;
break;
case scheme_let_one_type:
obj = ((Scheme_Let_One *)obj)->body;
break;
case scheme_let_void_type:
obj = ((Scheme_Let_Void *)obj)->body;
break;
case scheme_letrec_type:
obj = ((Scheme_Letrec *)obj)->body;
break;
case scheme_branch_type:
if (!bfuel)
return 0;
bfuel--;
if (!can_unbox_directly(((Scheme_Branch_Rec *)obj)->tbranch, extfl, bfuel))
return 0;
obj = ((Scheme_Branch_Rec *)obj)->fbranch;
break;
case scheme_sequence_type:
obj = ((Scheme_Sequence *)obj)->array[((Scheme_Sequence *)obj)->count - 1];
break;
default:
return is_unboxing_immediate(obj, 0, extfl);
}
}
}
int scheme_can_unbox_directly(Scheme_Object *obj, int extfl)
{
return can_unbox_directly(obj, extfl, 3);
}
static jit_insn *generate_arith_slow_path(mz_jit_state *jitter, Scheme_Object *rator,
jit_insn **_ref, jit_insn **_ref4,
Branch_Info *for_branch, int branch_short,
int orig_args, int reversed, int arith, int use_v, int v,
int dest)
/* *_ref4 is place to set for where to jump (for true case, if for_branch) after completing;
*_ref is place to set for where to jump for false if for_branch, result if !for_branch;
result is place to jump to start slow path if fixnum attempt fails */
{
GC_CAN_IGNORE jit_insn *ref, *ref4, *refslow;
refslow = jit_get_ip();
(void)jit_movi_p(JIT_R2, ((Scheme_Primitive_Proc *)rator)->prim_val);
if (for_branch) {
scheme_prepare_branch_jump(jitter, for_branch);
CHECK_LIMIT();
ref4 = jit_patchable_movi_p(JIT_V1, jit_forward());
mz_set_local_p(JIT_V1, JIT_LOCAL2);
ref = jit_patchable_movi_p(JIT_V1, jit_forward());
} else {
ref4 = NULL;
ref = NULL;
}
if (orig_args == 1) {
if (for_branch) {
(void)jit_jmpi(sjc.call_original_unary_arith_for_branch_code);
} else {
(void)jit_calli(sjc.call_original_unary_arith_code);
}
} else {
if (use_v) {
(void)jit_movi_p(JIT_R1, scheme_make_integer(v));
reversed = !reversed;
}
if (for_branch) {
if (reversed) {
(void)jit_jmpi(sjc.call_original_binary_rev_arith_for_branch_code);
} else {
(void)jit_jmpi(sjc.call_original_binary_arith_for_branch_code);
}
} else {
if (reversed) {
(void)jit_calli(sjc.call_original_binary_rev_arith_code);
} else {
(void)jit_calli(sjc.call_original_binary_arith_code);
}
}
}
if (!for_branch) {
jit_movr_p(dest, JIT_R0);
__START_SHORT_JUMPS__(branch_short);
ref = jit_jmpi(jit_forward());
__END_SHORT_JUMPS__(branch_short);
}
*_ref = ref;
*_ref4 = ref4;
if (arith == ARITH_LSH) {
/* Add tag back to first arg, just in case. See arithmetic-shift branch to refslow. */
ref = jit_get_ip();
if (reversed || use_v) {
jit_ori_l(JIT_R0, JIT_R0, 0x1);
} else {
jit_ori_l(JIT_R1, JIT_R1, 0x1);
}
__START_TINY_JUMPS__(1);
(void)jit_jmpi(refslow);
__END_TINY_JUMPS__(1);
return ref;
} else {
return refslow;
}
}
#ifdef SIXTY_FOUR_BIT_INTEGERS
# define SCHEME_INT_SMALL_ENOUGH(rand2) ((((intptr_t)rand2 & 0x7FFFFFFF) == (intptr_t)rand2) || (((intptr_t)rand2 & 0xFFFFFFFFF8000000) == 0xFFFFFFFFF8000000))
#else
# define SCHEME_INT_SMALL_ENOUGH(rand2) 1
#endif
static int can_fast_double(int arith, int cmp, int two_args)
{
#ifdef INLINE_FP_OPS
if ((arith == ARITH_ADD)
|| (arith == ARITH_SUB)
|| (arith == ARITH_MUL)
|| (arith == ARITH_DIV)
|| (arith == ARITH_ABS)
|| (arith == ARITH_EX_INEX)
|| (arith == ARITH_SQRT)
|| (arith == ARITH_FLUNOP)
|| (arith == ARITH_INEX_EX))
return 1;
#endif
#ifdef INLINE_FP_COMP
if ((!arith && (cmp != CMP_EVENP) && (cmp != CMP_ODDP))
|| ((arith == ARITH_MIN) && two_args)
|| ((arith == ARITH_MAX) && two_args))
return 1;
#endif
return 0;
}
#ifdef CAN_INLINE_ALLOC
# ifdef JIT_USE_FP_OPS
# define DECL_FLONUM_GLUE(op) static void call_ ## op(void) XFORM_SKIP_PROC { \
scheme_jit_save_fp = scheme_double_ ## op(scheme_jit_save_fp); }
# ifdef MZ_LONG_DOUBLE
# define DECL_EXTNUM_GLUE(op) static void call_long_double_ ## op(void) XFORM_SKIP_PROC { \
scheme_jit_save_extfp = scheme_long_double_ ## op(scheme_jit_save_extfp); }
# define DECL_FP_GLUE(op) DECL_FLONUM_GLUE(op) DECL_EXTNUM_GLUE(op)
# else
# define DECL_FP_GLUE(op) DECL_FLONUM_GLUE(op)
# endif
DECL_FP_GLUE(sin)
DECL_FP_GLUE(cos)
DECL_FP_GLUE(tan)
DECL_FP_GLUE(asin)
DECL_FP_GLUE(acos)
DECL_FP_GLUE(atan)
DECL_FP_GLUE(exp)
DECL_FP_GLUE(log)
DECL_FP_GLUE(floor)
DECL_FP_GLUE(ceiling)
DECL_FP_GLUE(truncate)
DECL_FP_GLUE(round)
typedef void (*call_fp_proc)(void);
# ifdef MZ_LONG_DOUBLE
typedef void (*call_extfp_proc)(void);
# endif
# define DECL_BIN_FLONUM_GLUE(op) static void call_ ## op(void) XFORM_SKIP_PROC { \
scheme_jit_save_fp = scheme_double_ ## op(scheme_jit_save_fp, scheme_jit_save_fp2); }
# ifdef MZ_LONG_DOUBLE
# define DECL_BIN_EXTNUM_GLUE(op) static void call_long_double_ ## op(void) XFORM_SKIP_PROC { \
scheme_jit_save_extfp = scheme_long_double_ ## op(scheme_jit_save_extfp, scheme_jit_save_extfp2); }
# define DECL_BIN_FP_GLUE(op) DECL_BIN_FLONUM_GLUE(op) DECL_BIN_EXTNUM_GLUE(op)
# else
# define DECL_BIN_FP_GLUE(op) DECL_BIN_FLONUM_GLUE(op)
# endif
DECL_BIN_FP_GLUE(expt)
typedef void (*call_fp_bin_proc)(void);
# ifdef MZ_LONG_DOUBLE
typedef void (*call_extfp_bin_proc)(void);
# endif
# endif
#endif
int scheme_generate_unboxing(mz_jit_state *jitter, int target)
{
int fpr0 USED_ONLY_SOMETIMES;
#ifdef MZ_LONG_DOUBLE
if (jitter->unbox_extflonum) {
fpr0 = JIT_FPU_FPR_0(jitter->unbox_depth);
jit_fpu_ldxi_ld_fppush(fpr0, target, &((Scheme_Long_Double *)0x0)->long_double_val);
} else
#endif
{
fpr0 = JIT_FPR_0(jitter->unbox_depth);
jit_ldxi_d_fppush(fpr0, target, &((Scheme_Double *)0x0)->double_val);
}
jitter->unbox_depth++;
return 1;
}
int scheme_generate_alloc_double(mz_jit_state *jitter, int inline_retry, int dest)
/* value should be in JIT_FPR0; R0-R2 not saved; V1 used */
{
#ifdef INLINE_FP_OPS
# ifdef CAN_INLINE_ALLOC
scheme_inline_alloc(jitter, sizeof(Scheme_Double), scheme_double_type, 0, 0, 1, inline_retry, 0);
CHECK_LIMIT();
jit_addi_p(dest, JIT_V1, OBJHEAD_SIZE);
(void)jit_stxi_d_fppop(&((Scheme_Double *)0x0)->double_val, dest, JIT_FPR0);
# else
(void)mz_tl_sti_d_fppop(tl_scheme_jit_save_fp, JIT_FPR0, JIT_R0);
JIT_UPDATE_THREAD_RSPTR_IF_NEEDED();
mz_prepare(0);
{
GC_CAN_IGNORE jit_insn *refr;
(void)mz_finish_lwe(ts_malloc_double, refr);
}
jit_retval(dest);
# endif
#endif
return 1;
}
#ifdef MZ_LONG_DOUBLE
int scheme_generate_alloc_long_double(mz_jit_state *jitter, int inline_retry, int dest)
/* same as above */
{
#ifdef INLINE_FP_OPS
# ifdef CAN_INLINE_ALLOC
scheme_inline_alloc(jitter, sizeof(Scheme_Long_Double), scheme_long_double_type, 0, 0, 0, inline_retry, 1);
CHECK_LIMIT();
jit_addi_p(dest, JIT_V1, OBJHEAD_SIZE);
(void)jit_fpu_stxi_ld_fppop(&((Scheme_Long_Double *)0x0)->long_double_val, dest, JIT_FPU_FPR0);
# else
(void)mz_fpu_ta_tl_sti_ld_fppop(tl_scheme_jit_save_extfp, JIT_FPU_FPR0, JIT_R0);
JIT_UPDATE_THREAD_RSPTR_IF_NEEDED();
mz_prepare(0);
{
GC_CAN_IGNORE jit_insn *refr;
(void)mz_finish_lwe(ts_malloc_long_double, refr);
}
jit_retval(dest);
# endif
#endif
return 1;
}
#endif
int scheme_generate_alloc_X_double(mz_jit_state *jitter, int inline_retry, int dest, int extfl)
{
MZ_FPUSEL_STMT(extfl,
return scheme_generate_alloc_long_double(jitter, inline_retry, dest),
return scheme_generate_alloc_double(jitter, inline_retry, dest));
}
static int generate_float_point_arith(mz_jit_state *jitter, Scheme_Object *rator,
int arith, int cmp, int reversed, int two_args, int second_const,
jit_insn **_refd, jit_insn **_refdt, Branch_Info *for_branch,
int branch_short, int unsafe_fl, int unboxed, int unboxed_result,
int dest, int extfl)
/* Unless unboxed, first arg is in JIT_R1, second in JIT_R0.
If unboxed in push/pop mode, first arg is pushed before second.
If unboxed in direct mode, first arg is in JIT_FPR0+depth
and second is in JIT_FPR1+depth (which is backward).
Unboxed implies unsafe unless arith == ARITH_INEX_EX. */
{
#if defined(INLINE_FP_OPS) || defined(INLINE_FP_COMP)
GC_CAN_IGNORE jit_insn *ref8, *ref9, *ref10, *refd, *refdt, *refs = NULL, *refs2 = NULL;
int no_alloc = unboxed_result;
int need_post_pop USED_ONLY_SOMETIMES = 0;
if (!unsafe_fl && !unboxed) {
/* Maybe they're doubles */
__START_TINY_JUMPS__(1);
if (two_args) {
jit_orr_ul(JIT_R2, JIT_R0, JIT_R1);
ref8 = jit_bmsi_ul(jit_forward(), JIT_R2, 0x1);
} else
ref8 = NULL;
jit_ldxi_s(JIT_R2, JIT_R0, &((Scheme_Object *)0x0)->type);
if (extfl)
ref9 = jit_bnei_i(jit_forward(), JIT_R2, scheme_long_double_type);
else
ref9 = jit_bnei_i(jit_forward(), JIT_R2, scheme_double_type);
if (two_args) {
jit_ldxi_s(JIT_R2, JIT_R1, &((Scheme_Object *)0x0)->type);
if (extfl)
ref10 = jit_bnei_i(jit_forward(), JIT_R2, scheme_long_double_type);
else
ref10 = jit_bnei_i(jit_forward(), JIT_R2, scheme_double_type);
} else
ref10 = NULL;
CHECK_LIMIT();
__END_TINY_JUMPS__(1);
} else {
ref8 = ref9 = ref10 = NULL;
}
if (!two_args && !second_const && ((arith == ARITH_MUL) || ((arith == ARITH_DIV) && reversed))) {
/* Special case: multiplication by exact 0 */
(void)jit_movi_p(dest, scheme_make_integer(0));
} else {
/* Yes, they're doubles. First arg is in JIT_R1, second is in JIT_R0.
Put the first arg in fpr0 and second (if any) into fpr1. To work
right with stacks, that means pushing the second argument first. */
int fpr1, fpr0;
fpr0 = JIT_FPUSEL_FPR_0(extfl, jitter->unbox_depth);
fpr1 = JIT_FPUSEL_FPR_1(extfl, 1+jitter->unbox_depth);
if (two_args) {
if (!unboxed) {
MZ_FPUSEL_STMT(extfl,
jit_fpu_ldxi_ld_fppush(fpr1, JIT_R1, &((Scheme_Long_Double *)0x0)->long_double_val),
jit_ldxi_d_fppush(fpr1, JIT_R1, &((Scheme_Double *)0x0)->double_val));
}
} else if ((arith == ARITH_SUB) && !second_const && reversed) {
reversed = 0;
} else if (arith == ARITH_ABS) {
/* abs needs no extra number */
} else if (arith == ARITH_SQRT) {
/* sqrt needs no extra number */
} else if (arith == ARITH_FLUNOP) {
/* flround, flsin, etc. needs no extra number */
} else if (arith == ARITH_EX_INEX) {
/* exact->inexact needs no extra number */
} else if (arith == ARITH_INEX_EX) {
/* inexact->exact needs no extra number */
} else {
#ifdef MZ_LONG_DOUBLE
long_double d;
d = long_double_from_intptr(second_const);
if (extfl) {
mz_fpu_movi_ld_fppush(fpr1, d, JIT_R2)
} else {
mz_movi_d_fppush(fpr1, second_const, JIT_R2);
}
#else
double d = second_const;
mz_movi_d_fppush(fpr1, d, JIT_R2);
#endif
reversed = !reversed;
cmp = -cmp;
}
if (!unboxed) {
if (arith != ARITH_EX_INEX) {
MZ_FPUSEL_STMT(extfl,
jit_fpu_ldxi_ld_fppush(fpr0, JIT_R0, &((Scheme_Long_Double *)0x0)->long_double_val),
jit_ldxi_d_fppush(fpr0, JIT_R0, &((Scheme_Double *)0x0)->double_val));
}
}
#ifdef DIRECT_FPR_ACCESS
# define USES_DIRECT_FPR_ACCESS (!extfl)
#else
# define USES_DIRECT_FPR_ACCESS 0
#endif
#ifdef DIRECT_FPR_ACCESS
if (unboxed && USES_DIRECT_FPR_ACCESS) {
/* arguments are backward */
reversed = !reversed;
cmp = -cmp;
}
#endif
CHECK_LIMIT();
if (arith) {
#if defined(MZ_LONG_DOUBLE) && defined(MZ_NEED_SET_EXTFL_MODE)
int need_control_reset = 0;
if (extfl) {
switch (arith) {
case ARITH_ADD:
case ARITH_MUL:
case ARITH_DIV:
case ARITH_SUB:
case ARITH_SQRT:
jit_set_fp_control(0x37f);
need_control_reset = 1;
break;
}
}
#endif
switch (arith) {
case ARITH_ADD:
jit_FPSEL_addr_xd_fppop(extfl, fpr0, fpr0, fpr1);
break;
case ARITH_MUL:
jit_FPSEL_mulr_xd_fppop(extfl, fpr0, fpr0, fpr1);
break;
case ARITH_DIV:
if (!reversed)
jit_FPSEL_divrr_xd_fppop(extfl, fpr0, fpr0, fpr1);
else
jit_FPSEL_divr_xd_fppop(extfl, fpr0, fpr0, fpr1);
break;
case ARITH_SUB:
{
if (!two_args && !second_const && !reversed) {
/* Need a special case to make sure that (- 0.0) => -0.0 */
jit_FPSEL_negr_xd_fppop(extfl, fpr0, fpr0);
} else if (reversed)
jit_FPSEL_subr_xd_fppop(extfl, fpr0, fpr0, fpr1);
else
jit_FPSEL_subrr_xd_fppop(extfl, fpr0, fpr0, fpr1);
}
break;
case ARITH_MIN:
case ARITH_MAX:
{
GC_CAN_IGNORE jit_insn *refc, *refn;
__START_TINY_JUMPS__(1);
/* If R0 is nan, then copy to R1, ensuring nan result */
refn = jit_FPSEL_beqr_xd(extfl, jit_forward(), fpr0, fpr0);
if (unboxed)
jit_FPSEL_movr_xd_rel(extfl, fpr1, fpr0);
else
jit_movr_p(JIT_R1, JIT_R0);
mz_patch_branch(refn);
if (arith == ARITH_MIN) {
if (unboxed) {
refc = jit_FPSEL_bltr_xd(extfl, jit_forward(), fpr0, fpr1);
} else {
refc = jit_FPSEL_bltr_xd_fppop(extfl, jit_forward(), fpr0, fpr1);
}
} else {
if (unboxed) {
refc = jit_FPSEL_bger_xd(extfl, jit_forward(), fpr0, fpr1);
} else {
refc = jit_FPSEL_bger_xd_fppop(extfl, jit_forward(), fpr0, fpr1);
}
}
CHECK_LIMIT();
if (unboxed) {
jit_FPSEL_movr_xd_rel(extfl, fpr0, fpr1);
need_post_pop = 1;
} else
jit_movr_p(JIT_R0, JIT_R1);
mz_patch_branch(refc);
__END_TINY_JUMPS__(1);
if (!unboxed) {
/* we've already set JIT_R0 */
jit_movr_p(dest, JIT_R0);
no_alloc = 1;
}
}
break;
case ARITH_ABS:
jit_FPSEL_abs_xd_fppop(extfl, fpr0, fpr0);
break;
case ARITH_EX_INEX: /* exact->inexact */
/* no work to do, because argument is already inexact;
no need to allocate, because argument is never unboxed,
and it therefore already resides in R0 */
jit_movr_p(dest, JIT_R0);
no_alloc = 1;
break;
case ARITH_INEX_EX: /* inexact->exact */
if (!unsafe_fl) {
jit_FPSEL_movr_xd_fppush(extfl, fpr1, fpr0);
}
jit_FPSEL_roundr_xd_l_fppop(extfl, JIT_R1, fpr0);
if (!unsafe_fl) {
/* to check whether it fits in a fixnum, we
need to convert back and check whether it
is the same */
if (unboxed) {
JIT_ASSERT(jitter->unbox_depth == 0);
jit_FPSEL_movr_xd_fppush(extfl, JIT_FPR2, fpr1); /* for slow path */
}
jit_FPSEL_extr_l_xd_fppush(extfl, fpr0, JIT_R1);
__START_TINY_JUMPS__(1);
refs = jit_FPSEL_bantieqr_xd_fppop(extfl, jit_forward(), fpr0, fpr1);
__END_TINY_JUMPS__(1);
/* result still may not fit in a fixnum */
jit_lshi_l(JIT_R2, JIT_R1, 1);
jit_rshi_l(JIT_R2, JIT_R2, 1);
__START_TINY_JUMPS__(1);
refs2 = jit_bner_l(jit_forward(), JIT_R1, JIT_R2);
__END_TINY_JUMPS__(1);
#if !defined(DIRECT_FPR_ACCESS) || defined(MZ_LONG_DOUBLE)
if (unboxed && !USES_DIRECT_FPR_ACCESS)
jit_FPSEL_roundr_xd_l_fppop(extfl, JIT_R1, JIT_FPR2); /* slow path won't be needed */
#endif
}
jit_fixnum_l(dest, JIT_R1);
no_alloc = 1;
break;
case ARITH_SQRT:
jit_FPSEL_sqrt_xd_fppop(extfl, fpr0, fpr0);
break;
#ifdef CAN_INLINE_ALLOC
# ifdef JIT_USE_FP_OPS
case ARITH_FLUNOP: /* flfloor, flsin, etc. */
{
call_fp_proc f;
#ifdef MZ_LONG_DOUBLE
if (extfl) {
if (IS_NAMED_PRIM(rator, "extflsin"))
f = call_long_double_sin;
else if (IS_NAMED_PRIM(rator, "extflcos"))
f = call_long_double_cos;
else if (IS_NAMED_PRIM(rator, "extfltan"))
f = call_long_double_tan;
else if (IS_NAMED_PRIM(rator, "extflasin"))
f = call_long_double_asin;
else if (IS_NAMED_PRIM(rator, "extflacos"))
f = call_long_double_acos;
else if (IS_NAMED_PRIM(rator, "extflatan"))
f = call_long_double_atan;
else if (IS_NAMED_PRIM(rator, "extflexp"))
f = call_long_double_exp;
else if (IS_NAMED_PRIM(rator, "extfllog"))
f = call_long_double_log;
else if (IS_NAMED_PRIM(rator, "extflfloor"))
f = call_long_double_floor;
else if (IS_NAMED_PRIM(rator, "extflceiling"))
f = call_long_double_ceiling;
else if (IS_NAMED_PRIM(rator, "extfltruncate"))
f = call_long_double_truncate;
else if (IS_NAMED_PRIM(rator, "extflround"))
f = call_long_double_round;
else {
scheme_signal_error("internal error: unknown extflonum function");
f = NULL;
}
(void)mz_fpu_tl_sti_ld_fppop(tl_scheme_jit_save_extfp, JIT_FPU_FPR0, JIT_R2);
mz_prepare(0);
(void)mz_finish(f);
(void)mz_fpu_tl_ldi_ld_fppush(JIT_FPU_FPR0, tl_scheme_jit_save_extfp, JIT_R2);
} else
#endif
{
if (IS_NAMED_PRIM(rator, "flsin"))
f = call_sin;
else if (IS_NAMED_PRIM(rator, "flcos"))
f = call_cos;
else if (IS_NAMED_PRIM(rator, "fltan"))
f = call_tan;
else if (IS_NAMED_PRIM(rator, "flasin"))
f = call_asin;
else if (IS_NAMED_PRIM(rator, "flacos"))
f = call_acos;
else if (IS_NAMED_PRIM(rator, "flatan"))
f = call_atan;
else if (IS_NAMED_PRIM(rator, "flexp"))
f = call_exp;
else if (IS_NAMED_PRIM(rator, "fllog"))
f = call_log;
else if (IS_NAMED_PRIM(rator, "flfloor"))
f = call_floor;
else if (IS_NAMED_PRIM(rator, "flceiling"))
f = call_ceiling;
else if (IS_NAMED_PRIM(rator, "fltruncate"))
f = call_truncate;
else if (IS_NAMED_PRIM(rator, "flround"))
f = call_round;
else {
scheme_signal_error("internal error: unknown flonum function");
f = NULL;
}
(void)mz_tl_sti_d_fppop(tl_scheme_jit_save_fp, JIT_FPR0, JIT_R2);
mz_prepare(0);
(void)mz_finish(f);
(void)mz_tl_ldi_d_fppush(JIT_FPR0, tl_scheme_jit_save_fp, JIT_R2);
}
}
break;
case ARITH_EXPT: /* flexpt */
{
#ifdef MZ_LONG_DOUBLE
if (extfl) {
if (!reversed) {
(void)mz_fpu_tl_sti_ld_fppop(tl_scheme_jit_save_extfp2, JIT_FPU_FPR0, JIT_R2);
(void)mz_fpu_tl_sti_ld_fppop(tl_scheme_jit_save_extfp, JIT_FPU_FPR1, JIT_R2);
} else {
(void)mz_fpu_tl_sti_ld_fppop(tl_scheme_jit_save_extfp, JIT_FPU_FPR0, JIT_R2);
(void)mz_fpu_tl_sti_ld_fppop(tl_scheme_jit_save_extfp2, JIT_FPU_FPR1, JIT_R2);
}
mz_prepare(0);
(void)mz_finish(call_long_double_expt);
(void)mz_fpu_tl_ldi_ld_fppush(JIT_FPU_FPR0, tl_scheme_jit_save_extfp, JIT_R2);
} else
#endif
{
if (!reversed) {
(void)mz_tl_sti_d_fppop(tl_scheme_jit_save_fp2, JIT_FPR0, JIT_R2);
(void)mz_tl_sti_d_fppop(tl_scheme_jit_save_fp, JIT_FPR1, JIT_R2);
} else {
(void)mz_tl_sti_d_fppop(tl_scheme_jit_save_fp, JIT_FPR0, JIT_R2);
(void)mz_tl_sti_d_fppop(tl_scheme_jit_save_fp2, JIT_FPR1, JIT_R2);
}
mz_prepare(0);
(void)mz_finish(call_expt);
(void)mz_tl_ldi_d_fppush(JIT_FPR0, tl_scheme_jit_save_fp, JIT_R2);
}
}
break;
# endif
#endif
default:
break;
}
CHECK_LIMIT();
if (!no_alloc) {
mz_rs_sync(); /* needed if arguments were unboxed */
scheme_generate_alloc_X_double(jitter, 0, dest, extfl);
CHECK_LIMIT();
#if defined(MZ_USE_JIT_I386)
if (need_post_pop && !USES_DIRECT_FPR_ACCESS)
FSTPr(0);
#endif
} else if (unboxed_result) {
jitter->unbox_depth++;
#if defined(MZ_USE_JIT_I386)
if (need_post_pop && !USES_DIRECT_FPR_ACCESS) {
FXCHr(1);
FSTPr(0);
}
#endif
}
#if defined(MZ_LONG_DOUBLE) && defined(MZ_NEED_SET_EXTFL_MODE)
if (extfl && need_control_reset) {
jit_set_fp_control(0x27f);
}
#endif
} else {
/* The "anti" variants below invert the branch. Unlike the "un"
variants, the "anti" variants invert the comparison result
after the layer where +nan.0 always generates false. */
__START_SHORT_JUMPS__(branch_short);
if (for_branch) {
scheme_prepare_branch_jump(jitter, for_branch);
CHECK_LIMIT();
}
R0_FP_ADJUST(_jitl.r0_can_be_tmp++);
switch (cmp) {
case CMP_LT:
refd = jit_FPSEL_bantigtr_xd_fppop(extfl, jit_forward(), fpr0, fpr1);
break;