-
-
Notifications
You must be signed in to change notification settings - Fork 597
/
cod3.d
8257 lines (7487 loc) · 258 KB
/
cod3.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) 1994-1998 by Symantec
* Copyright (C) 2000-2021 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/cod3.d, backend/cod3.d)
* Documentation: https://dlang.org/phobos/dmd_backend_cod3.html
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/backend/cod3.d
*/
module dmd.backend.cod3;
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.backend;
import dmd.backend.cc;
import dmd.backend.cdef;
import dmd.backend.cgcse;
import dmd.backend.code;
import dmd.backend.code_x86;
import dmd.backend.codebuilder;
import dmd.backend.dlist;
import dmd.backend.dvec;
import dmd.backend.melf;
import dmd.backend.mem;
import dmd.backend.el;
import dmd.backend.exh;
import dmd.backend.global;
import dmd.backend.obj;
import dmd.backend.oper;
import dmd.backend.outbuf;
import dmd.backend.rtlsym;
import dmd.backend.symtab;
import dmd.backend.ty;
import dmd.backend.type;
import dmd.backend.xmm;
version (SCPP)
{
import parser;
import precomp;
}
extern (C++):
nothrow:
@safe:
version (MARS)
enum MARS = true;
else
enum MARS = false;
int REGSIZE();
extern __gshared CGstate cgstate;
extern __gshared ubyte[FLMAX] segfl;
extern __gshared bool[FLMAX] stackfl, flinsymtab;
private extern (D) uint mask(uint m) { return 1 << m; }
//private void genorreg(ref CodeBuilder c, uint t, uint f) { genregs(c, 0x09, f, t); }
extern __gshared targ_size_t retsize;
enum JMPJMPTABLE = false; // benchmarking shows it's slower
enum MINLL = 0x8000_0000_0000_0000L;
enum MAXLL = 0x7FFF_FFFF_FFFF_FFFFL;
/*************
* Size in bytes of each instruction.
* 0 means illegal instruction.
* bit M: if there is a modregrm field (EV1 is reserved for modregrm)
* bit T: if there is a second operand (EV2)
* bit E: if second operand is only 8 bits
* bit A: a short version exists for the AX reg
* bit R: a short version exists for regs
* bits 2..0: size of instruction (excluding optional bytes)
*/
enum
{
M = 0x80,
T = 0x40,
E = 0x20,
A = 0x10,
R = 0x08,
W = 0,
}
private __gshared ubyte[256] inssize =
[ M|2,M|2,M|2,M|2, T|E|2,T|3,1,1, /* 00 */
M|2,M|2,M|2,M|2, T|E|2,T|3,1,1, /* 08 */
M|2,M|2,M|2,M|2, T|E|2,T|3,1,1, /* 10 */
M|2,M|2,M|2,M|2, T|E|2,T|3,1,1, /* 18 */
M|2,M|2,M|2,M|2, T|E|2,T|3,1,1, /* 20 */
M|2,M|2,M|2,M|2, T|E|2,T|3,1,1, /* 28 */
M|2,M|2,M|2,M|2, T|E|2,T|3,1,1, /* 30 */
M|2,M|2,M|2,M|2, T|E|2,T|3,1,1, /* 38 */
1,1,1,1, 1,1,1,1, /* 40 */
1,1,1,1, 1,1,1,1, /* 48 */
1,1,1,1, 1,1,1,1, /* 50 */
1,1,1,1, 1,1,1,1, /* 58 */
1,1,M|2,M|2, 1,1,1,1, /* 60 */
T|3,M|T|4,T|E|2,M|T|E|3, 1,1,1,1, /* 68 */
T|E|2,T|E|2,T|E|2,T|E|2, T|E|2,T|E|2,T|E|2,T|E|2, /* 70 */
T|E|2,T|E|2,T|E|2,T|E|2, T|E|2,T|E|2,T|E|2,T|E|2, /* 78 */
M|T|E|A|3,M|T|A|4,M|T|E|3,M|T|E|3, M|2,M|2,M|2,M|A|R|2, /* 80 */
M|A|2,M|A|2,M|A|2,M|A|2, M|2,M|2,M|2,M|R|2, /* 88 */
1,1,1,1, 1,1,1,1, /* 90 */
1,1,T|5,1, 1,1,1,1, /* 98 */
// cod3_set32() patches this
// T|5,T|5,T|5,T|5, 1,1,1,1, /* A0 */
T|3,T|3,T|3,T|3, 1,1,1,1, /* A0 */
T|E|2,T|3,1,1, 1,1,1,1, /* A8 */
T|E|2,T|E|2,T|E|2,T|E|2, T|E|2,T|E|2,T|E|2,T|E|2, /* B0 */
T|3,T|3,T|3,T|3, T|3,T|3,T|3,T|3, /* B8 */
M|T|E|3,M|T|E|3,T|3,1, M|2,M|2,M|T|E|R|3,M|T|R|4, /* C0 */
T|E|4,1,T|3,1, 1,T|E|2,1,1, /* C8 */
M|2,M|2,M|2,M|2, T|E|2,T|E|2,0,1, /* D0 */
/* For the floating instructions, allow room for the FWAIT */
M|2,M|2,M|2,M|2, M|2,M|2,M|2,M|2, /* D8 */
T|E|2,T|E|2,T|E|2,T|E|2, T|E|2,T|E|2,T|E|2,T|E|2, /* E0 */
T|3,T|3,T|5,T|E|2, 1,1,1,1, /* E8 */
1,0,1,1, 1,1,M|A|2,M|A|2, /* F0 */
1,1,1,1, 1,1,M|2,M|R|2 /* F8 */
];
private __gshared const ubyte[256] inssize32 =
[ 2,2,2,2, 2,5,1,1, /* 00 */
2,2,2,2, 2,5,1,1, /* 08 */
2,2,2,2, 2,5,1,1, /* 10 */
2,2,2,2, 2,5,1,1, /* 18 */
2,2,2,2, 2,5,1,1, /* 20 */
2,2,2,2, 2,5,1,1, /* 28 */
2,2,2,2, 2,5,1,1, /* 30 */
2,2,2,2, 2,5,1,1, /* 38 */
1,1,1,1, 1,1,1,1, /* 40 */
1,1,1,1, 1,1,1,1, /* 48 */
1,1,1,1, 1,1,1,1, /* 50 */
1,1,1,1, 1,1,1,1, /* 58 */
1,1,2,2, 1,1,1,1, /* 60 */
5,6,2,3, 1,1,1,1, /* 68 */
2,2,2,2, 2,2,2,2, /* 70 */
2,2,2,2, 2,2,2,2, /* 78 */
3,6,3,3, 2,2,2,2, /* 80 */
2,2,2,2, 2,2,2,2, /* 88 */
1,1,1,1, 1,1,1,1, /* 90 */
1,1,7,1, 1,1,1,1, /* 98 */
5,5,5,5, 1,1,1,1, /* A0 */
2,5,1,1, 1,1,1,1, /* A8 */
2,2,2,2, 2,2,2,2, /* B0 */
5,5,5,5, 5,5,5,5, /* B8 */
3,3,3,1, 2,2,3,6, /* C0 */
4,1,3,1, 1,2,1,1, /* C8 */
2,2,2,2, 2,2,0,1, /* D0 */
/* For the floating instructions, don't need room for the FWAIT */
2,2,2,2, 2,2,2,2, /* D8 */
2,2,2,2, 2,2,2,2, /* E0 */
5,5,7,2, 1,1,1,1, /* E8 */
1,0,1,1, 1,1,2,2, /* F0 */
1,1,1,1, 1,1,2,2 /* F8 */
];
/* For 2 byte opcodes starting with 0x0F */
private __gshared ubyte[256] inssize2 =
[ M|3,M|3,M|3,M|3, 2,2,2,2, // 00
2,2,M|3,2, 2,M|3,2,M|T|E|4, // 08
M|3,M|3,M|3,M|3, M|3,M|3,M|3,M|3, // 10
M|3,2,2,2, 2,2,2,2, // 18
M|3,M|3,M|3,M|3, M|3,2,M|3,2, // 20
M|3,M|3,M|3,M|3, M|3,M|3,M|3,M|3, // 28
2,2,2,2, 2,2,2,2, // 30
M|4,2,M|T|E|5,2, 2,2,2,2, // 38
M|3,M|3,M|3,M|3, M|3,M|3,M|3,M|3, // 40
M|3,M|3,M|3,M|3, M|3,M|3,M|3,M|3, // 48
M|3,M|3,M|3,M|3, M|3,M|3,M|3,M|3, // 50
M|3,M|3,M|3,M|3, M|3,M|3,M|3,M|3, // 58
M|3,M|3,M|3,M|3, M|3,M|3,M|3,M|3, // 60
M|3,M|3,M|3,M|3, M|3,M|3,M|3,M|3, // 68
M|T|E|4,M|T|E|4,M|T|E|4,M|T|E|4, M|3,M|3,M|3,2, // 70
2,2,2,2, M|3,M|3,M|3,M|3, // 78
W|T|4,W|T|4,W|T|4,W|T|4, W|T|4,W|T|4,W|T|4,W|T|4, // 80
W|T|4,W|T|4,W|T|4,W|T|4, W|T|4,W|T|4,W|T|4,W|T|4, // 88
M|3,M|3,M|3,M|3, M|3,M|3,M|3,M|3, // 90
M|3,M|3,M|3,M|3, M|3,M|3,M|3,M|3, // 98
2,2,2,M|3, M|T|E|4,M|3,2,2, // A0
2,2,2,M|3, M|T|E|4,M|3,M|3,M|3, // A8
M|E|3,M|3,M|3,M|3, M|3,M|3,M|3,M|3, // B0
M|3,2,M|T|E|4,M|3, M|3,M|3,M|3,M|3, // B8
M|3,M|3,M|T|E|4,M|3, M|T|E|4,M|T|E|4,M|T|E|4,M|3, // C0
2,2,2,2, 2,2,2,2, // C8
M|3,M|3,M|3,M|3, M|3,M|3,M|3,M|3, // D0
M|3,M|3,M|3,M|3, M|3,M|3,M|3,M|3, // D8
M|3,M|3,M|3,M|3, M|3,M|3,M|3,M|3, // E0
M|3,M|3,M|3,M|3, M|3,M|3,M|3,M|3, // E8
M|3,M|3,M|3,M|3, M|3,M|3,M|3,M|3, // F0
M|3,M|3,M|3,M|3, M|3,M|3,M|3,2 // F8
];
/*************************************************
* Generate code to save `reg` in `regsave` stack area.
* Params:
* regsave = register save areay on stack
* cdb = where to write generated code
* reg = register to save
* idx = set to location in regsave for use in REGSAVE_restore()
*/
@trusted
void REGSAVE_save(ref REGSAVE regsave, ref CodeBuilder cdb, reg_t reg, out uint idx)
{
if (isXMMreg(reg))
{
regsave.alignment = 16;
regsave.idx = (regsave.idx + 15) & ~15;
idx = regsave.idx;
regsave.idx += 16;
// MOVD idx[RBP],xmm
opcode_t op = STOAPD;
if (TARGET_LINUX && I32)
// Haven't yet figured out why stack is not aligned to 16
op = STOUPD;
cdb.genc1(op,modregxrm(2, reg - XMM0, BPRM),FLregsave,cast(targ_uns) idx);
}
else
{
if (!regsave.alignment)
regsave.alignment = REGSIZE;
idx = regsave.idx;
regsave.idx += REGSIZE;
// MOV idx[RBP],reg
cdb.genc1(0x89,modregxrm(2, reg, BPRM),FLregsave,cast(targ_uns) idx);
if (I64)
code_orrex(cdb.last(), REX_W);
}
reflocal = true;
if (regsave.idx > regsave.top)
regsave.top = regsave.idx; // keep high water mark
}
/*******************************
* Restore `reg` from `regsave` area.
* Complement REGSAVE_save().
*/
@trusted
void REGSAVE_restore(const ref REGSAVE regsave, ref CodeBuilder cdb, reg_t reg, uint idx)
{
if (isXMMreg(reg))
{
assert(regsave.alignment == 16);
// MOVD xmm,idx[RBP]
opcode_t op = LODAPD;
if (TARGET_LINUX && I32)
// Haven't yet figured out why stack is not aligned to 16
op = LODUPD;
cdb.genc1(op,modregxrm(2, reg - XMM0, BPRM),FLregsave,cast(targ_uns) idx);
}
else
{ // MOV reg,idx[RBP]
cdb.genc1(0x8B,modregxrm(2, reg, BPRM),FLregsave,cast(targ_uns) idx);
if (I64)
code_orrex(cdb.last(), REX_W);
}
}
/************************************
* Size for vex encoded instruction.
*/
@trusted
ubyte vex_inssize(code *c)
{
assert(c.Iflags & CFvex && c.Ivex.pfx == 0xC4);
ubyte ins;
if (c.Iflags & CFvex3)
{
switch (c.Ivex.mmmm)
{
case 0: // no prefix
case 1: // 0F
ins = cast(ubyte)(inssize2[c.Ivex.op] + 2);
break;
case 2: // 0F 38
ins = cast(ubyte)(inssize2[0x38] + 1);
break;
case 3: // 0F 3A
ins = cast(ubyte)(inssize2[0x3A] + 1);
break;
default:
printf("Iop = %x mmmm = %x\n", c.Iop, c.Ivex.mmmm);
assert(0);
}
}
else
{
ins = cast(ubyte)(inssize2[c.Ivex.op] + 1);
}
return ins;
}
/************************************
* Determine if there is a modregrm byte for code.
*/
@trusted
int cod3_EA(code *c)
{ uint ins;
opcode_t op1 = c.Iop & 0xFF;
if (op1 == ESCAPE)
ins = 0;
else if ((c.Iop & 0xFFFD00) == 0x0F3800)
ins = inssize2[(c.Iop >> 8) & 0xFF];
else if ((c.Iop & 0xFF00) == 0x0F00)
ins = inssize2[op1];
else
ins = inssize[op1];
return ins & M;
}
/********************************
* setup ALLREGS and BYTEREGS
* called by: codgen
*/
@trusted
void cod3_initregs()
{
if (I64)
{
ALLREGS = mAX|mBX|mCX|mDX|mSI|mDI| mR8|mR9|mR10|mR11|mR12|mR13|mR14|mR15;
BYTEREGS = ALLREGS;
}
else
{
ALLREGS = ALLREGS_INIT;
BYTEREGS = BYTEREGS_INIT;
}
}
/********************************
* set initial global variable values
*/
@trusted
void cod3_setdefault()
{
fregsaved = mBP | mSI | mDI;
}
/********************************
* Fix global variables for 386.
*/
@trusted
void cod3_set32()
{
inssize[0xA0] = T|5;
inssize[0xA1] = T|5;
inssize[0xA2] = T|5;
inssize[0xA3] = T|5;
BPRM = 5; /* [EBP] addressing mode */
fregsaved = mBP | mBX | mSI | mDI; // saved across function calls
FLOATREGS = FLOATREGS_32;
FLOATREGS2 = FLOATREGS2_32;
DOUBLEREGS = DOUBLEREGS_32;
if (config.flags3 & CFG3eseqds)
fregsaved |= mES;
foreach (ref v; inssize2[0x80 .. 0x90])
v = W|T|6;
TARGET_STACKALIGN = config.fpxmmregs ? 16 : 4;
}
/********************************
* Fix global variables for I64.
*/
@trusted
void cod3_set64()
{
inssize[0xA0] = T|5; // MOV AL,mem
inssize[0xA1] = T|5; // MOV RAX,mem
inssize[0xA2] = T|5; // MOV mem,AL
inssize[0xA3] = T|5; // MOV mem,RAX
BPRM = 5; // [RBP] addressing mode
fregsaved = (config.exe & EX_windos)
? mBP | mBX | mDI | mSI | mR12 | mR13 | mR14 | mR15 | mES | mXMM6 | mXMM7 // also XMM8..15;
: mBP | mBX | mR12 | mR13 | mR14 | mR15 | mES; // saved across function calls
FLOATREGS = FLOATREGS_64;
FLOATREGS2 = FLOATREGS2_64;
DOUBLEREGS = DOUBLEREGS_64;
ALLREGS = mAX|mBX|mCX|mDX|mSI|mDI| mR8|mR9|mR10|mR11|mR12|mR13|mR14|mR15;
BYTEREGS = ALLREGS;
foreach (ref v; inssize2[0x80 .. 0x90])
v = W|T|6;
TARGET_STACKALIGN = config.fpxmmregs ? 16 : 8;
}
/*********************************
* Word or dword align start of function.
* Params:
* seg = segment to write alignment bytes to
* nbytes = number of alignment bytes to write
*/
@trusted
void cod3_align_bytes(int seg, size_t nbytes)
{
/* Table 4-2 from Intel Instruction Set Reference M-Z
* 1 bytes NOP 90
* 2 bytes 66 NOP 66 90
* 3 bytes NOP DWORD ptr [EAX] 0F 1F 00
* 4 bytes NOP DWORD ptr [EAX + 00H] 0F 1F 40 00
* 5 bytes NOP DWORD ptr [EAX + EAX*1 + 00H] 0F 1F 44 00 00
* 6 bytes 66 NOP DWORD ptr [EAX + EAX*1 + 00H] 66 0F 1F 44 00 00
* 7 bytes NOP DWORD ptr [EAX + 00000000H] 0F 1F 80 00 00 00 00
* 8 bytes NOP DWORD ptr [EAX + EAX*1 + 00000000H] 0F 1F 84 00 00 00 00 00
* 9 bytes 66 NOP DWORD ptr [EAX + EAX*1 + 00000000H] 66 0F 1F 84 00 00 00 00 00
* only for CPUs: CPUID.01H.EAX[Bytes 11:8] = 0110B or 1111B
*/
assert(SegData[seg].SDseg == seg);
while (nbytes)
{ size_t n = nbytes;
const(char)* p;
if (nbytes > 1 && (I64 || config.fpxmmregs))
{
switch (n)
{
case 2: p = "\x66\x90"; break;
case 3: p = "\x0F\x1F\x00"; break;
case 4: p = "\x0F\x1F\x40\x00"; break;
case 5: p = "\x0F\x1F\x44\x00\x00"; break;
case 6: p = "\x66\x0F\x1F\x44\x00\x00"; break;
case 7: p = "\x0F\x1F\x80\x00\x00\x00\x00"; break;
case 8: p = "\x0F\x1F\x84\x00\x00\x00\x00\x00"; break;
default: p = "\x66\x0F\x1F\x84\x00\x00\x00\x00\x00"; n = 9; break;
}
}
else
{
static immutable ubyte[15] nops = [
0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90
]; // XCHG AX,AX
if (n > nops.length)
n = nops.length;
p = cast(char*)nops;
}
objmod.write_bytes(SegData[seg],cast(uint)n,cast(char*)p);
nbytes -= n;
}
}
/****************************
* Align start of function.
* Params:
* seg = segment of function
*/
@trusted
void cod3_align(int seg)
{
if (config.exe & EX_windos)
{
if (config.flags4 & CFG4speed) // if optimized for speed
{
// Pick alignment based on CPU target
if (config.target_cpu == TARGET_80486 ||
config.target_cpu >= TARGET_PentiumPro)
{ // 486 does reads on 16 byte boundaries, so if we are near
// such a boundary, align us to it
const nbytes = -Offset(seg) & 15;
if (nbytes < 8)
cod3_align_bytes(seg, nbytes);
}
}
}
else
{
const nbytes = -Offset(seg) & 7;
cod3_align_bytes(seg, nbytes);
}
}
/**********************************
* Generate code to adjust the stack pointer by `nbytes`
* Params:
* cdb = code builder
* nbytes = number of bytes to adjust stack pointer
*/
void cod3_stackadj(ref CodeBuilder cdb, int nbytes)
{
//printf("cod3_stackadj(%d)\n", nbytes);
uint grex = I64 ? REX_W << 16 : 0;
uint rm;
if (nbytes > 0)
rm = modregrm(3,5,SP); // SUB ESP,nbytes
else
{
nbytes = -nbytes;
rm = modregrm(3,0,SP); // ADD ESP,nbytes
}
cdb.genc2(0x81, grex | rm, nbytes);
}
/**********************************
* Generate code to align the stack pointer at `nbytes`
* Params:
* cdb = code builder
* nbytes = number of bytes to align stack pointer
*/
void cod3_stackalign(ref CodeBuilder cdb, int nbytes)
{
//printf("cod3_stackalign(%d)\n", nbytes);
const grex = I64 ? REX_W << 16 : 0;
const rm = modregrm(3, 4, SP); // AND ESP,-nbytes
cdb.genc2(0x81, grex | rm, -nbytes);
}
/* Constructor that links the ModuleReference to the head of
* the list pointed to by _Dmoduleref
*
* For ELF object files.
*/
static if (0)
{
void cod3_buildmodulector(Outbuffer* buf, int codeOffset, int refOffset)
{
/* ret
* codeOffset:
* pushad
* mov EAX,&ModuleReference
* mov ECX,_DmoduleRef
* mov EDX,[ECX]
* mov [EAX],EDX
* mov [ECX],EAX
* popad
* ret
*/
const int seg = CODE;
if (I64 && config.flags3 & CFG3pic)
{ // LEA RAX,ModuleReference[RIP]
buf.writeByte(REX | REX_W);
buf.writeByte(LEA);
buf.writeByte(modregrm(0,AX,5));
codeOffset += 3;
codeOffset += Obj.writerel(seg, codeOffset, R_X86_64_PC32, 3 /*STI_DATA*/, refOffset - 4);
// MOV RCX,_DmoduleRef@GOTPCREL[RIP]
buf.writeByte(REX | REX_W);
buf.writeByte(0x8B);
buf.writeByte(modregrm(0,CX,5));
codeOffset += 3;
codeOffset += Obj.writerel(seg, codeOffset, R_X86_64_GOTPCREL, Obj.external_def("_Dmodule_ref"), -4);
}
else
{
/* movl ModuleReference*, %eax */
buf.writeByte(0xB8);
codeOffset += 1;
const uint reltype = I64 ? R_X86_64_32 : R_386_32;
codeOffset += Obj.writerel(seg, codeOffset, reltype, 3 /*STI_DATA*/, refOffset);
/* movl _Dmodule_ref, %ecx */
buf.writeByte(0xB9);
codeOffset += 1;
codeOffset += Obj.writerel(seg, codeOffset, reltype, Obj.external_def("_Dmodule_ref"), 0);
}
if (I64)
buf.writeByte(REX | REX_W);
buf.writeByte(0x8B); buf.writeByte(0x11); /* movl (%ecx), %edx */
if (I64)
buf.writeByte(REX | REX_W);
buf.writeByte(0x89); buf.writeByte(0x10); /* movl %edx, (%eax) */
if (I64)
buf.writeByte(REX | REX_W);
buf.writeByte(0x89); buf.writeByte(0x01); /* movl %eax, (%ecx) */
buf.writeByte(0xC3); /* ret */
}
}
/*****************************
* Given a type, return a mask of
* registers to hold that type.
* Input:
* tyf function type
*/
@trusted
regm_t regmask(tym_t tym, tym_t tyf)
{
switch (tybasic(tym))
{
case TYvoid:
case TYnoreturn:
case TYstruct:
case TYarray:
return 0;
case TYbool:
case TYwchar_t:
case TYchar16:
case TYchar:
case TYschar:
case TYuchar:
case TYshort:
case TYushort:
case TYint:
case TYuint:
case TYnullptr:
case TYnptr:
case TYnref:
case TYsptr:
case TYcptr:
case TYimmutPtr:
case TYsharePtr:
case TYrestrictPtr:
case TYfgPtr:
return mAX;
case TYfloat:
case TYifloat:
if (I64)
return mXMM0;
if (config.exe & EX_flat)
return mST0;
goto case TYlong;
case TYlong:
case TYulong:
case TYdchar:
if (!I16)
return mAX;
goto case TYfptr;
case TYfptr:
case TYhptr:
return mDX | mAX;
case TYcent:
case TYucent:
assert(I64);
return mDX | mAX;
case TYvptr:
return mDX | mBX;
case TYdouble:
case TYdouble_alias:
case TYidouble:
if (I64)
return mXMM0;
if (config.exe & EX_flat)
return mST0;
return DOUBLEREGS;
case TYllong:
case TYullong:
return I64 ? cast(regm_t) mAX : (I32 ? mDX | mAX : DOUBLEREGS);
case TYldouble:
case TYildouble:
return mST0;
case TYcfloat:
if (config.exe & EX_posix && I32 && tybasic(tyf) == TYnfunc)
return mDX | mAX;
goto case TYcdouble;
case TYcdouble:
if (I64)
return mXMM0 | mXMM1;
goto case TYcldouble;
case TYcldouble:
return mST01;
// SIMD vector types
case TYfloat4:
case TYdouble2:
case TYschar16:
case TYuchar16:
case TYshort8:
case TYushort8:
case TYlong4:
case TYulong4:
case TYllong2:
case TYullong2:
case TYfloat8:
case TYdouble4:
case TYschar32:
case TYuchar32:
case TYshort16:
case TYushort16:
case TYlong8:
case TYulong8:
case TYllong4:
case TYullong4:
if (!config.fpxmmregs)
{ printf("SIMD operations not supported on this platform\n");
exit(1);
}
return mXMM0;
default:
debug WRTYxx(tym);
assert(0);
}
}
/*******************************
* setup register allocator parameters with platform specific data
*/
void cgreg_dst_regs(reg_t* dst_integer_reg, reg_t* dst_float_reg)
{
*dst_integer_reg = AX;
*dst_float_reg = XMM0;
}
@trusted
void cgreg_set_priorities(tym_t ty, const(reg_t)** pseq, const(reg_t)** pseqmsw)
{
//printf("cgreg_set_priorities %x\n", ty);
const sz = tysize(ty);
if (tyxmmreg(ty))
{
static immutable ubyte[9] sequence = [XMM0,XMM1,XMM2,XMM3,XMM4,XMM5,XMM6,XMM7,NOREG];
*pseq = sequence.ptr;
}
else if (I64)
{
if (sz == REGSIZE * 2)
{
static immutable ubyte[3] seqmsw1 = [CX,DX,NOREG];
static immutable ubyte[5] seqlsw1 = [AX,BX,SI,DI,NOREG];
*pseq = seqlsw1.ptr;
*pseqmsw = seqmsw1.ptr;
}
else
{ // R10 is reserved for the static link
static immutable ubyte[15] sequence2 = [AX,CX,DX,SI,DI,R8,R9,R11,BX,R12,R13,R14,R15,BP,NOREG];
*pseq = cast(ubyte*)sequence2.ptr;
}
}
else if (I32)
{
if (sz == REGSIZE * 2)
{
static immutable ubyte[5] seqlsw3 = [AX,BX,SI,DI,NOREG];
static immutable ubyte[3] seqmsw3 = [CX,DX,NOREG];
*pseq = seqlsw3.ptr;
*pseqmsw = seqmsw3.ptr;
}
else
{
static immutable ubyte[8] sequence4 = [AX,CX,DX,BX,SI,DI,BP,NOREG];
*pseq = sequence4.ptr;
}
}
else
{ assert(I16);
if (typtr(ty))
{
// For pointer types, try to pick index register first
static immutable ubyte[8] seqidx5 = [BX,SI,DI,AX,CX,DX,BP,NOREG];
*pseq = seqidx5.ptr;
}
else
{
// Otherwise, try to pick index registers last
static immutable ubyte[8] sequence6 = [AX,CX,DX,BX,SI,DI,BP,NOREG];
*pseq = sequence6.ptr;
}
}
}
/*******************************************
* Call finally block.
* Params:
* bf = block to call
* retregs = registers to preserve across call
* Returns:
* code generated
*/
@trusted
private code *callFinallyBlock(block *bf, regm_t retregs)
{
CodeBuilder cdbs; cdbs.ctor();
CodeBuilder cdbr; cdbr.ctor();
int nalign = 0;
calledFinally = true;
uint npush = gensaverestore(retregs,cdbs,cdbr);
if (STACKALIGN >= 16)
{ npush += REGSIZE;
if (npush & (STACKALIGN - 1))
{ nalign = STACKALIGN - (npush & (STACKALIGN - 1));
cod3_stackadj(cdbs, nalign);
}
}
cdbs.genc(0xE8,0,0,0,FLblock,cast(targ_size_t)bf);
regcon.immed.mval = 0;
if (nalign)
cod3_stackadj(cdbs, -nalign);
cdbs.append(cdbr);
return cdbs.finish();
}
/*******************************
* Generate block exit code
*/
@trusted
void outblkexitcode(ref CodeBuilder cdb, block *bl, ref int anyspill, const(char)* sflsave, Symbol** retsym, const regm_t mfuncregsave)
{
CodeBuilder cdb2; cdb2.ctor();
elem *e = bl.Belem;
block *nextb;
regm_t retregs = 0;
if (bl.BC != BCasm)
assert(bl.Bcode == null);
switch (bl.BC) /* block exit condition */
{
case BCiftrue:
{
bool jcond = true;
block *bs1 = bl.nthSucc(0);
block *bs2 = bl.nthSucc(1);
if (bs1 == bl.Bnext)
{ // Swap bs1 and bs2
block *btmp;
jcond ^= 1;
btmp = bs1;
bs1 = bs2;
bs2 = btmp;
}
logexp(cdb,e,jcond,FLblock,cast(code *) bs1);
nextb = bs2;
}
L5:
if (configv.addlinenumbers && bl.Bsrcpos.Slinnum &&
!(funcsym_p.ty() & mTYnaked))
{
//printf("BCiftrue: %s(%u)\n", bl.Bsrcpos.Sfilename ? bl.Bsrcpos.Sfilename : "", bl.Bsrcpos.Slinnum);
cdb.genlinnum(bl.Bsrcpos);
}
if (nextb != bl.Bnext)
{
assert(!(bl.Bflags & BFLepilog));
genjmp(cdb,JMP,FLblock,nextb);
}
break;
case BCjmptab:
case BCifthen:
case BCswitch:
{
assert(!(bl.Bflags & BFLepilog));
doswitch(cdb,bl); // hide messy details
break;
}
version (MARS)
{
case BCjcatch: // D catch clause of try-catch
assert(ehmethod(funcsym_p) != EHmethod.EH_NONE);
// Mark all registers as destroyed. This will prevent
// register assignments to variables used in catch blocks.
getregs(cdb,lpadregs());
if (config.ehmethod == EHmethod.EH_DWARF)
{
/* Each block must have ESP set to the same value it was at the end
* of the prolog. But the unwinder calls catch blocks with ESP set
* at the value it was when the throwing function was called, which
* may have arguments pushed on the stack.
* This instruction will reset ESP to the correct offset from EBP.
*/
cdb.gen1(ESCAPE | ESCfixesp);
}
goto case_goto;
}
version (SCPP)
{
case BCcatch: // C++ catch clause of try-catch
// Mark all registers as destroyed. This will prevent
// register assignments to variables used in catch blocks.
getregs(cdb,allregs | mES);
goto case_goto;
case BCtry:
usednteh |= EHtry;
if (config.exe == EX_WIN32)
usednteh |= NTEHtry;
goto case_goto;
}
case BCgoto:
nextb = bl.nthSucc(0);
if ((MARS ||
funcsym_p.Sfunc.Fflags3 & Fnteh) &&
ehmethod(funcsym_p) != EHmethod.EH_DWARF &&
bl.Btry != nextb.Btry &&
nextb.BC != BC_finally)
{
regm_t retregsx = 0;
gencodelem(cdb,e,&retregsx,true);
int toindex = nextb.Btry ? nextb.Btry.Bscope_index : -1;
assert(bl.Btry);
int fromindex = bl.Btry.Bscope_index;
version (MARS)
{
if (toindex + 1 == fromindex)
{ // Simply call __finally
if (bl.Btry &&
bl.Btry.nthSucc(1).BC == BCjcatch)
{
goto L5; // it's a try-catch, not a try-finally
}
}
}
if (config.ehmethod == EHmethod.EH_WIN32 && !(funcsym_p.Sfunc.Fflags3 & Feh_none) ||
config.ehmethod == EHmethod.EH_SEH)
{
nteh_unwind(cdb,0,toindex);
}
else
{
version (MARS)
{
if (toindex + 1 <= fromindex)
{
//c = cat(c, linux_unwind(0, toindex));
block *bt;
//printf("B%d: fromindex = %d, toindex = %d\n", bl.Bdfoidx, fromindex, toindex);
bt = bl;
while ((bt = bt.Btry) != null && bt.Bscope_index != toindex)
{ block *bf;
//printf("\tbt.Bscope_index = %d, bt.Blast_index = %d\n", bt.Bscope_index, bt.Blast_index);
bf = bt.nthSucc(1);
// Only look at try-finally blocks
if (bf.BC == BCjcatch)
continue;
if (bf == nextb)
continue;
//printf("\tbf = B%d, nextb = B%d\n", bf.Bdfoidx, nextb.Bdfoidx);
if (nextb.BC == BCgoto &&
!nextb.Belem &&
bf == nextb.nthSucc(0))
continue;
// call __finally
cdb.append(callFinallyBlock(bf.nthSucc(0), retregsx));
}
}
}
}
goto L5;
}
case_goto:
{
regm_t retregsx = 0;
gencodelem(cdb,e,&retregsx,true);