-
-
Notifications
You must be signed in to change notification settings - Fork 594
/
cg87.d
3876 lines (3501 loc) · 115 KB
/
cg87.d
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
/**
* Compiler implementation of the
* $(LINK2 http://www.dlang.org, D programming language).
*
* Copyright: Copyright (C) 1987-1995 by Symantec
* Copyright (C) 2000-2019 by The D Language Foundation, All Rights Reserved
* Authors: $(LINK2 http://www.digitalmars.com, Walter Bright)
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/backend/cg87.d, backend/cg87.d)
*/
module dmd.backend.cg87;
version (SCPP)
version = COMPILE;
version (MARS)
version = COMPILE;
version (COMPILE)
{
import core.stdc.stdio;
import core.stdc.stdlib;
import core.stdc.string;
import dmd.backend.cc;
import dmd.backend.cdef;
import dmd.backend.code;
import dmd.backend.code_x86;
import dmd.backend.codebuilder;
import dmd.backend.mem;
import dmd.backend.el;
import dmd.backend.global;
import dmd.backend.oper;
import dmd.backend.ty;
import dmd.backend.evalu8 : el_toldoubled;
extern (C++):
int REGSIZE();
private extern (D) uint mask(uint m) { return 1 << m; }
void callcdxxx(ref CodeBuilder cdb, elem *e, regm_t *pretregs, OPER op);
// Constants that the 8087 supports directly
// BUG: rewrite for 80 bit long doubles
enum PI = 3.14159265358979323846;
enum LOG2 = 0.30102999566398119521;
enum LN2 = 0.6931471805599453094172321;
enum LOG2T = 3.32192809488736234787;
enum LOG2E = 1.4426950408889634074; // 1/LN2
enum FWAIT = 0x9B; // FWAIT opcode
/* Mark variable referenced by e as not a register candidate */
uint notreg(elem* e) { return e.EV.Vsym.Sflags &= ~GTregcand; }
/* Generate the appropriate ESC instruction */
ubyte ESC(uint MF, uint b) { return cast(ubyte)(0xD8 + (MF << 1) + b); }
enum
{ // Values for MF
MFfloat = 0,
MFlong = 1,
MFdouble = 2,
MFword = 3
}
__gshared
{
NDP[8] _8087elems; // 8087 stack
NDP ndp_zero;
int stackused = 0; // number of items on the 8087 stack
}
/*********************************
*/
struct Dconst
{
int round;
Symbol *roundto0;
Symbol *roundtonearest;
}
private __gshared Dconst oldd;
enum NDPP = 0; // print out debugging info
bool NOSAHF() { return I64 || config.fpxmmregs; } // can't use SAHF instruction
enum CW_roundto0 = 0xFBF;
enum CW_roundtonearest = 0x3BF;
/**********************************
* When we need to temporarilly save 8087 registers, we record information
* about the save into an array of NDP structs:
*/
version (SCPP)
struct NDP
{
elem *e; // which elem is stored here (NULL if none)
uint offset; // offset from e (used for complex numbers)
__gshared NDP *save;
__gshared int savemax; // # of entries in save[]
__gshared int savetop; // # of entries used in save[]
}
debug
enum NDPSAVEINC = 2; // flush reallocation bugs
else
enum NDPSAVEINC = 8; // allocation chunk sizes
private void getlvalue87(ref CodeBuilder cdb,code *pcs,elem *e,regm_t keepmsk)
{
// the x87 instructions cannot read XMM registers
if (e.Eoper == OPvar || e.Eoper == OPrelconst)
e.EV.Vsym.Sflags &= ~GTregcand;
getlvalue(cdb, pcs, e, keepmsk);
if (ADDFWAIT())
pcs.Iflags |= CFwait;
if (I32)
pcs.Iflags &= ~CFopsize;
else if (I64)
pcs.Irex &= ~REX_W;
}
/****************************************
* Store/load to ndp save location i
*/
private void ndp_fstp(ref CodeBuilder cdb, int i, tym_t ty)
{
switch (tybasic(ty))
{
case TYfloat:
case TYifloat:
case TYcfloat:
cdb.genc1(0xD9,modregrm(2,3,BPRM),FLndp,i); // FSTP m32real i[BP]
break;
case TYdouble:
case TYdouble_alias:
case TYidouble:
case TYcdouble:
cdb.genc1(0xDD,modregrm(2,3,BPRM),FLndp,i); // FSTP m64real i[BP]
break;
case TYldouble:
case TYildouble:
case TYcldouble:
cdb.genc1(0xDB,modregrm(2,7,BPRM),FLndp,i); // FSTP m80real i[BP]
break;
default:
assert(0);
}
}
private void ndp_fld(ref CodeBuilder cdb, int i, tym_t ty)
{
switch (tybasic(ty))
{
case TYfloat:
case TYifloat:
case TYcfloat:
cdb.genc1(0xD9,modregrm(2,0,BPRM),FLndp,i);
break;
case TYdouble:
case TYdouble_alias:
case TYidouble:
case TYcdouble:
cdb.genc1(0xDD,modregrm(2,0,BPRM),FLndp,i);
break;
case TYldouble:
case TYildouble:
case TYcldouble:
cdb.genc1(0xDB,modregrm(2,5,BPRM),FLndp,i); // FLD m80real i[BP]
break;
default:
assert(0);
}
}
/**************************
* Return index of empty slot in NDP.save[].
*/
private int getemptyslot()
{
int i;
for (i = 0; i < NDP.savemax; i++)
if (NDP.save[i].e == null)
goto L1;
// Out of room, reallocate NDP.save[]
NDP.save = cast(NDP *)mem_realloc(NDP.save,
(NDP.savemax + NDPSAVEINC) * (*NDP.save).sizeof);
/* clear out new portion of NDP.save[] */
memset(NDP.save + NDP.savemax,0,NDPSAVEINC * (*NDP.save).sizeof);
i = NDP.savemax;
NDP.savemax += NDPSAVEINC;
L1: if (i >= NDP.savetop)
NDP.savetop = i + 1;
return i;
}
/*********************************
* Pop 8087 stack.
*/
void pop87() { pop87(__LINE__, __FILE__); }
void pop87(int line, const(char)* file)
{
int i;
if (NDPP)
printf("pop87(%s(%d): stackused=%d)\n", file, line, stackused);
--stackused;
assert(stackused >= 0);
for (i = 0; i < _8087elems.length - 1; i++)
_8087elems[i] = _8087elems[i + 1];
// end of stack is nothing
_8087elems[_8087elems.length - 1] = ndp_zero;
}
/*******************************
* Push 8087 stack. Generate and return any code
* necessary to preserve anything that might run off the end of the stack.
*/
void push87(ref CodeBuilder cdb) { push87(cdb,__LINE__,__FILE__); }
void push87(ref CodeBuilder cdb, int line, const(char)* file)
{
// if we would lose the top register off of the stack
if (_8087elems[7].e != null)
{
int i = getemptyslot();
NDP.save[i] = _8087elems[7];
cdb.genf2(0xD9,0xF6); // FDECSTP
genfwait(cdb);
ndp_fstp(cdb, i, _8087elems[7].e.Ety); // FSTP i[BP]
assert(stackused == 8);
if (NDPP) printf("push87() : overflow\n");
}
else
{
if (NDPP) printf("push87(%s(%d): %d)\n", file, line, stackused);
stackused++;
assert(stackused <= 8);
}
// Shift the stack up
for (int i = 7; i > 0; i--)
_8087elems[i] = _8087elems[i - 1];
_8087elems[0] = ndp_zero;
}
/*****************************
* Note elem e as being in ST(i) as being a value we want to keep.
*/
void note87(elem *e, uint offset, int i)
{
note87(e, offset, i, __LINE__);
}
void note87(elem *e, uint offset, int i, int linnum)
{
if (NDPP)
printf("note87(e = %p.%d, i = %d, stackused = %d, line = %d)\n",e,offset,i,stackused,linnum);
static if (0)
{
if (_8087elems[i].e)
printf("_8087elems[%d].e = %p\n",i,_8087elems[i].e);
}
//if (i >= stackused) *cast(char*)0=0;
assert(i < stackused);
while (e.Eoper == OPcomma)
e = e.EV.E2;
_8087elems[i].e = e;
_8087elems[i].offset = offset;
}
/****************************************************
* Exchange two entries in 8087 stack.
*/
void xchg87(int i, int j)
{
NDP save;
save = _8087elems[i];
_8087elems[i] = _8087elems[j];
_8087elems[j] = save;
}
/****************************
* Make sure that elem e is in register ST(i). Reload it if necessary.
* Input:
* i 0..3 8087 register number
* flag 1 don't bother with FXCH
*/
private void makesure87(ref CodeBuilder cdb,elem *e,uint offset,int i,uint flag)
{
makesure87(cdb,e,offset,i,flag,__LINE__);
}
private void makesure87(ref CodeBuilder cdb,elem *e,uint offset,int i,uint flag,int linnum)
{
debug if (NDPP) printf("makesure87(e=%p, offset=%d, i=%d, flag=%d, line=%d)\n",e,offset,i,flag,linnum);
while (e.Eoper == OPcomma)
e = e.EV.E2;
assert(e && i < 4);
L1:
if (_8087elems[i].e != e || _8087elems[i].offset != offset)
{
debug if (_8087elems[i].e)
printf("_8087elems[%d].e = %p, .offset = %d\n",i,_8087elems[i].e,_8087elems[i].offset);
assert(_8087elems[i].e == null);
int j;
for (j = 0; 1; j++)
{
if (j >= NDP.savetop && e.Eoper == OPcomma)
{
e = e.EV.E2; // try right side
goto L1;
}
debug if (j >= NDP.savetop)
printf("e = %p, NDP.savetop = %d\n",e,NDP.savetop);
assert(j < NDP.savetop);
//printf("\tNDP.save[%d] = %p, .offset = %d\n", j, NDP.save[j].e, NDP.save[j].offset);
if (e == NDP.save[j].e && offset == NDP.save[j].offset)
break;
}
push87(cdb);
genfwait(cdb);
ndp_fld(cdb, j, e.Ety); // FLD j[BP]
if (!(flag & 1))
{
while (i != 0)
{
cdb.genf2(0xD9,0xC8 + i); // FXCH ST(i)
i--;
}
}
NDP.save[j] = ndp_zero; // back in 8087
}
//_8087elems[i].e = null;
}
/****************************
* Save in memory any values in the 8087 that we want to keep.
*/
void save87(ref CodeBuilder cdb)
{
bool any = false;
while (_8087elems[0].e && stackused)
{
// Save it
int i = getemptyslot();
if (NDPP) printf("saving %p in temporary NDP.save[%d]\n",_8087elems[0].e,i);
NDP.save[i] = _8087elems[0];
genfwait(cdb);
ndp_fstp(cdb,i,_8087elems[0].e.Ety); // FSTP i[BP]
pop87();
any = true;
}
if (any) // if any stores
genfwait(cdb); // wait for last one to finish
}
/******************************************
* Save any noted values that would be destroyed by n pushes
*/
void save87regs(ref CodeBuilder cdb, uint n)
{
assert(n <= 7);
uint j = 8 - n;
if (stackused > j)
{
for (uint k = 8; k > j; k--)
{
cdb.genf2(0xD9,0xF6); // FDECSTP
genfwait(cdb);
if (k <= stackused)
{
int i = getemptyslot();
ndp_fstp(cdb, i, _8087elems[k - 1].e.Ety); // FSTP i[BP]
NDP.save[i] = _8087elems[k - 1];
_8087elems[k - 1] = ndp_zero;
}
}
for (uint k = 8; k > j; k--)
{
if (k > stackused)
{ cdb.genf2(0xD9,0xF7); // FINCSTP
genfwait(cdb);
}
}
stackused = j;
}
}
/*****************************************************
* Save/restore ST0 or ST01
*/
void gensaverestore87(regm_t regm, ref CodeBuilder cdbsave, ref CodeBuilder cdbrestore)
{
//printf("gensaverestore87(%s)\n", regm_str(regm));
assert(regm == mST0 || regm == mST01);
int i = getemptyslot();
NDP.save[i].e = el_calloc(); // this blocks slot [i] for the life of this function
ndp_fstp(cdbsave, i, TYldouble);
CodeBuilder cdb2a;
cdb2a.ctor();
ndp_fld(cdb2a, i, TYldouble);
if (regm == mST01)
{
int j = getemptyslot();
NDP.save[j].e = el_calloc();
ndp_fstp(cdbsave, j, TYldouble);
ndp_fld(cdbrestore, j, TYldouble);
}
cdbrestore.append(cdb2a);
}
/*************************************
* Find which, if any, slot on stack holds elem e.
*/
private int cse_get(elem *e, uint offset)
{
int i;
for (i = 0; 1; i++)
{
if (i == stackused)
{
i = -1;
//printf("cse not found\n");
//elem_print(e);
break;
}
if (_8087elems[i].e == e &&
_8087elems[i].offset == offset)
{ //printf("cse found %d\n",i);
//elem_print(e);
break;
}
}
return i;
}
/*************************************
* Reload common subexpression.
*/
void comsub87(ref CodeBuilder cdb,elem *e,regm_t *pretregs)
{
//printf("comsub87(e = %p, *pretregs = %s)\n", e, regm_str(*pretregs));
// Look on 8087 stack
int i = cse_get(e, 0);
if (tycomplex(e.Ety))
{
uint sz = tysize(e.Ety);
int j = cse_get(e, sz / 2);
if (i >= 0 && j >= 0)
{
push87(cdb);
push87(cdb);
cdb.genf2(0xD9,0xC0 + i); // FLD ST(i)
cdb.genf2(0xD9,0xC0 + j + 1); // FLD ST(j + 1)
fixresult_complex87(cdb,e,mST01,pretregs);
}
else
// Reload
loaddata(cdb,e,pretregs);
}
else
{
if (i >= 0)
{
push87(cdb);
cdb.genf2(0xD9,0xC0 + i); // FLD ST(i)
if (*pretregs & XMMREGS)
fixresult87(cdb,e,mST0,pretregs);
else
fixresult(cdb,e,mST0,pretregs);
}
else
// Reload
loaddata(cdb,e,pretregs);
}
freenode(e);
}
/*******************************
* Decide if we need to gen an FWAIT.
*/
void genfwait(ref CodeBuilder cdb)
{
if (ADDFWAIT())
cdb.gen1(FWAIT);
}
/***************************
* Put the 8087 flags into the CPU flags.
*/
private void cg87_87topsw(ref CodeBuilder cdb)
{
/* Note that SAHF is not available on some early I64 processors
* and will cause a seg fault
*/
assert(!NOSAHF);
getregs(cdb,mAX);
if (config.target_cpu >= TARGET_80286)
cdb.genf2(0xDF,0xE0); // FSTSW AX
else
{
cdb.genfltreg(0xD8+5,7,0); // FSTSW floatreg[BP]
genfwait(cdb); // FWAIT
cdb.genfltreg(0x8A,4,1); // MOV AH,floatreg+1[BP]
}
cdb.gen1(0x9E); // SAHF
code_orflag(cdb.last(),CFpsw);
}
/*****************************************
* Jump to ctarget if condition code C2 is set.
*/
private void genjmpifC2(ref CodeBuilder cdb, code *ctarget)
{
if (NOSAHF)
{
getregs(cdb,mAX);
cdb.genf2(0xDF,0xE0); // FSTSW AX
cdb.genc2(0xF6,modregrm(3,0,4),4); // TEST AH,4
genjmp(cdb, JNE, FLcode, cast(block *)ctarget); // JNE ctarget
}
else
{
cg87_87topsw(cdb);
genjmp(cdb, JP, FLcode, cast(block *)ctarget); // JP ctarget
}
}
/***************************
* Set the PSW based on the state of ST0.
* Input:
* pop if stack should be popped after test
* Returns:
* start of code appended to c.
*/
private void genftst(ref CodeBuilder cdb,elem *e,int pop)
{
if (NOSAHF)
{
push87(cdb);
cdb.gen2(0xD9,0xEE); // FLDZ
cdb.gen2(0xDF,0xE9); // FUCOMIP ST1
pop87();
if (pop)
{
cdb.genf2(0xDD,modregrm(3,3,0)); // FPOP
pop87();
}
}
else if (config.flags4 & CFG4fastfloat) // if fast floating point
{
cdb.genf2(0xD9,0xE4); // FTST
cg87_87topsw(cdb); // put 8087 flags in CPU flags
if (pop)
{
cdb.genf2(0xDD,modregrm(3,3,0)); // FPOP
pop87();
}
}
else if (config.target_cpu >= TARGET_80386)
{
// FUCOMP doesn't raise exceptions on QNANs, unlike FTST
push87(cdb);
cdb.gen2(0xD9,0xEE); // FLDZ
cdb.gen2(pop ? 0xDA : 0xDD,0xE9); // FUCOMPP / FUCOMP
pop87();
if (pop)
pop87();
cg87_87topsw(cdb); // put 8087 flags in CPU flags
}
else
{
// Call library function which does not raise exceptions
regm_t regm = 0;
callclib(cdb,e,CLIB.ftest,®m,0);
if (pop)
{
cdb.genf2(0xDD,modregrm(3,3,0)); // FPOP
pop87();
}
}
}
/*************************************
* Determine if there is a special 8087 instruction to load
* constant e.
* Input:
* im 0 load real part
* 1 load imaginary part
* Returns:
* opcode if found
* 0 if not
*/
ubyte loadconst(elem *e, int im)
{
elem_debug(e);
assert(im == 0 || im == 1);
immutable float[7] fval =
[0.0,1.0,PI,LOG2T,LOG2E,LOG2,LN2];
immutable double[7] dval =
[0.0,1.0,PI,LOG2T,LOG2E,LOG2,LN2];
static if (real.sizeof < 10)
{
import dmd.root.longdouble;
immutable targ_ldouble[7] ldval =
[ld_zero,ld_one,ld_pi,ld_log2t,ld_log2e,ld_log2,ld_ln2];
}
else
{
enum M_PI_L = 0x1.921fb54442d1846ap+1L; // 3.14159 fldpi
enum M_LOG2T_L = 0x1.a934f0979a3715fcp+1L; // 3.32193 fldl2t
enum M_LOG2E_L = 0x1.71547652b82fe178p+0L; // 1.4427 fldl2e
enum M_LOG2_L = 0x1.34413509f79fef32p-2L; // 0.30103 fldlg2
enum M_LN2_L = 0x1.62e42fefa39ef358p-1L; // 0.693147 fldln2
immutable targ_ldouble[7] ldval =
[0.0,1.0,M_PI_L,M_LOG2T_L,M_LOG2E_L,M_LOG2_L,M_LN2_L];
}
immutable ubyte[7 + 1] opcode =
/* FLDZ,FLD1,FLDPI,FLDL2T,FLDL2E,FLDLG2,FLDLN2,0 */
[0xEE,0xE8,0xEB,0xE9,0xEA,0xEC,0xED,0];
int i;
targ_float f;
targ_double d;
targ_ldouble ld;
int sz;
int zero;
void *p;
immutable ubyte[16] zeros;
if (im == 0)
{
switch (tybasic(e.Ety))
{
case TYfloat:
case TYifloat:
case TYcfloat:
f = e.EV.Vfloat;
sz = 4;
p = &f;
break;
case TYdouble:
case TYdouble_alias:
case TYidouble:
case TYcdouble:
d = e.EV.Vdouble;
sz = 8;
p = &d;
break;
case TYldouble:
case TYildouble:
case TYcldouble:
ld = e.EV.Vldouble;
sz = 10;
p = &ld;
break;
default:
assert(0);
}
}
else
{
switch (tybasic(e.Ety))
{
case TYcfloat:
f = e.EV.Vcfloat.im;
sz = 4;
p = &f;
break;
case TYcdouble:
d = e.EV.Vcdouble.im;
sz = 8;
p = &d;
break;
case TYcldouble:
ld = e.EV.Vcldouble.im;
sz = 10;
p = &ld;
break;
default:
assert(0);
}
}
// Note that for this purpose, -0 is not regarded as +0,
// since FLDZ loads a +0
assert(sz <= zeros.length);
zero = (memcmp(p, zeros.ptr, sz) == 0);
if (zero && config.target_cpu >= TARGET_PentiumPro)
return 0xEE; // FLDZ is the only one with 1 micro-op
// For some reason, these instructions take more clocks
if (config.flags4 & CFG4speed && config.target_cpu >= TARGET_Pentium)
return 0;
if (zero)
return 0xEE;
for (i = 1; i < fval.length; i++)
{
switch (sz)
{
case 4:
if (fval[i] != f)
continue;
break;
case 8:
if (dval[i] != d)
continue;
break;
case 10:
if (ldval[i] != ld)
continue;
break;
default:
assert(0);
}
break;
}
return opcode[i];
}
/******************************
* Given the result of an expression is in retregs,
* generate necessary code to return result in *pretregs.
*/
void fixresult87(ref CodeBuilder cdb,elem *e,regm_t retregs,regm_t *pretregs)
{
//printf("fixresult87(e = %p, retregs = x%x, *pretregs = x%x)\n", e,retregs,*pretregs);
//printf("fixresult87(e = %p, retregs = %s, *pretregs = %s)\n", e,regm_str(retregs),regm_str(*pretregs));
assert(!*pretregs || retregs);
if (*pretregs & mST01)
{
fixresult_complex87(cdb, e, retregs, pretregs);
return;
}
tym_t tym = tybasic(e.Ety);
uint sz = _tysize[tym];
//printf("tym = x%x, sz = %d\n", tym, sz);
/* if retregs needs to be transferred into the 8087 */
if (*pretregs & mST0 && retregs & (mBP | ALLREGS))
{
if (sz > DOUBLESIZE) elem_print(e);
assert(sz <= DOUBLESIZE);
if (!I16)
{
if (*pretregs & mPSW)
{ // Set flags
regm_t r = retregs | mPSW;
fixresult(cdb,e,retregs,&r);
}
push87(cdb);
if (sz == REGSIZE || (I64 && sz == 4))
{
const reg = findreg(retregs);
cdb.genfltreg(STO,reg,0); // MOV fltreg,reg
cdb.genfltreg(0xD9,0,0); // FLD float ptr fltreg
}
else
{
const msreg = findregmsw(retregs);
const lsreg = findreglsw(retregs);
cdb.genfltreg(STO,lsreg,0); // MOV fltreg,lsreg
cdb.genfltreg(STO,msreg,4); // MOV fltreg+4,msreg
cdb.genfltreg(0xDD,0,0); // FLD double ptr fltreg
}
}
else
{
regm_t regm = (sz == FLOATSIZE) ? FLOATREGS : DOUBLEREGS;
regm |= *pretregs & mPSW;
fixresult(cdb,e,retregs,®m);
regm = 0; // don't worry about result from CLIB.xxx
callclib(cdb,e,
((sz == FLOATSIZE) ? CLIB.fltto87 : CLIB.dblto87),
®m,0);
}
}
else if (*pretregs & (mBP | ALLREGS) && retregs & mST0)
{
assert(sz <= DOUBLESIZE);
uint mf = (sz == FLOATSIZE) ? MFfloat : MFdouble;
if (*pretregs & mPSW && !(retregs & mPSW))
genftst(cdb,e,0);
// FSTP floatreg
pop87();
cdb.genfltreg(ESC(mf,1),3,0);
genfwait(cdb);
reg_t reg;
allocreg(cdb,pretregs,®,(sz == FLOATSIZE) ? TYfloat : TYdouble);
if (sz == FLOATSIZE)
{
if (!I16)
cdb.genfltreg(LOD,reg,0);
else
{
cdb.genfltreg(LOD,reg,REGSIZE);
cdb.genfltreg(LOD,findreglsw(*pretregs),0);
}
}
else
{ assert(sz == DOUBLESIZE);
if (I16)
{
cdb.genfltreg(LOD,AX,6);
cdb.genfltreg(LOD,BX,4);
cdb.genfltreg(LOD,CX,2);
cdb.genfltreg(LOD,DX,0);
}
else if (I32)
{
cdb.genfltreg(LOD,reg,REGSIZE);
cdb.genfltreg(LOD,findreglsw(*pretregs),0);
}
else // I64
{
cdb.genfltreg(LOD,reg,0);
code_orrex(cdb.last(), REX_W);
}
}
}
else if (*pretregs == 0 && retregs == mST0)
{
cdb.genf2(0xDD,modregrm(3,3,0)); // FPOP
pop87();
}
else
{
if (*pretregs & mPSW)
{
if (!(retregs & mPSW))
{
genftst(cdb,e,!(*pretregs & (mST0 | XMMREGS))); // FTST
}
}
if (*pretregs & mST0 && retregs & XMMREGS)
{
assert(sz <= DOUBLESIZE);
uint mf = (sz == FLOATSIZE) ? MFfloat : MFdouble;
// MOVD floatreg,XMM?
const reg = findreg(retregs);
cdb.genxmmreg(xmmstore(tym),reg,0,tym);
push87(cdb);
cdb.genfltreg(ESC(mf,1),0,0); // FLD float/double ptr fltreg
}
else if (retregs & mST0 && *pretregs & XMMREGS)
{
assert(sz <= DOUBLESIZE);
uint mf = (sz == FLOATSIZE) ? MFfloat : MFdouble;
// FSTP floatreg
pop87();
cdb.genfltreg(ESC(mf,1),3,0);
genfwait(cdb);
// MOVD XMM?,floatreg
reg_t reg;
allocreg(cdb,pretregs,®,(sz == FLOATSIZE) ? TYfloat : TYdouble);
cdb.genxmmreg(xmmload(tym),reg,0,tym);
}
else
assert(!(*pretregs & mST0) || (retregs & mST0));
}
if (*pretregs & mST0)
note87(e,0,0);
}
/********************************
* Generate in-line 8087 code for the following operators:
* add
* min
* mul
* div
* cmp
*/
// Reverse the order that the op is done in
__gshared const ubyte[9] oprev = [ cast(ubyte)-1,0,1,2,3,5,4,7,6 ];
void orth87(ref CodeBuilder cdb,elem *e,regm_t *pretregs)
{
//printf("orth87(+e = %p, *pretregs = %s)\n", e, regm_str(*pretregs));
// we could be evaluating / for side effects only
assert(*pretregs != 0);
elem *e1 = e.EV.E1;
elem *e2 = e.EV.E2;
uint sz2 = tysize(e1.Ety);
if (tycomplex(e1.Ety))
sz2 /= 2;
OPER eoper = e.Eoper;
if (eoper == OPmul && e2.Eoper == OPconst && el_toldoubled(e.EV.E2) == 2.0L)
{
// Perform "mul 2.0" as fadd ST(0), ST
regm_t retregs = mST0;
codelem(cdb,e1,&retregs,false);
cdb.genf2(0xDC, 0xC0); // fadd ST(0), ST;
fixresult87(cdb,e,mST0,pretregs); // result is in ST(0).
freenode(e2);
return;
}
uint op;
if (OTrel(eoper))
eoper = OPeqeq;
bool imaginary;
static uint X(OPER op, uint ty1, uint ty2) { return (op << 16) + ty1 * 256 + ty2; }
switch (X(eoper, tybasic(e1.Ety), tybasic(e2.Ety)))
{
case X(OPadd, TYfloat, TYfloat):
case X(OPadd, TYdouble, TYdouble):
case X(OPadd, TYdouble_alias, TYdouble_alias):
case X(OPadd, TYldouble, TYldouble):
case X(OPadd, TYldouble, TYdouble):
case X(OPadd, TYdouble, TYldouble):
case X(OPadd, TYifloat, TYifloat):
case X(OPadd, TYidouble, TYidouble):
case X(OPadd, TYildouble, TYildouble):
op = 0; // FADDP
break;
case X(OPmin, TYfloat, TYfloat):
case X(OPmin, TYdouble, TYdouble):
case X(OPmin, TYdouble_alias, TYdouble_alias):
case X(OPmin, TYldouble, TYldouble):
case X(OPmin, TYldouble, TYdouble):
case X(OPmin, TYdouble, TYldouble):
case X(OPmin, TYifloat, TYifloat):
case X(OPmin, TYidouble, TYidouble):
case X(OPmin, TYildouble, TYildouble):