-
-
Notifications
You must be signed in to change notification settings - Fork 594
/
cv8.d
1194 lines (1039 loc) · 33.5 KB
/
cv8.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) 2012-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/cv8.d, backend/cv8.d)
*/
// This module generates the .debug$S and .debug$T sections for Win64,
// which are the MS-Coff symbolic debug info and type debug info sections.
module dmd.backend.cv8;
version (MARS)
{
import core.stdc.stdio;
import core.stdc.stdlib;
import core.stdc.string;
extern (C) nothrow char* getcwd(char*, size_t);
import dmd.backend.cc;
import dmd.backend.cdef;
import dmd.backend.cgcv;
import dmd.backend.code;
import dmd.backend.code_x86;
import dmd.backend.cv4;
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.ty;
import dmd.backend.type;
import dmd.backend.varstats;
import dmd.backend.xmm;
extern (C++):
nothrow:
static if (TARGET_WINDOS)
{
int REGSIZE();
// Determine if this Symbol is stored in a COMDAT
bool symbol_iscomdat(Symbol* s)
{
version (MARS)
{
return s.Sclass == SCcomdat ||
config.flags2 & CFG2comdat && s.Sclass == SCinline ||
config.flags4 & CFG4allcomdat && s.Sclass == SCglobal;
}
else
{
return s.Sclass == SCcomdat ||
config.flags2 & CFG2comdat && s.Sclass == SCinline ||
config.flags4 & CFG4allcomdat && (s.Sclass == SCglobal || s.Sclass == SCstatic);
}
}
// if symbols get longer than 65500 bytes, the linker reports corrupt debug info or exits with
// 'fatal error LNK1318: Unexpected PDB error; RPC (23) '(0x000006BA)'
enum CV8_MAX_SYMBOL_LENGTH = 0xffd8;
// The "F1" section, which is the symbols
private __gshared Outbuffer *F1_buf;
// The "F2" section, which is the line numbers
private __gshared Outbuffer *F2_buf;
// The "F3" section, which is global and a string table of source file names.
private __gshared Outbuffer *F3_buf;
// The "F4" section, which is global and a lists info about source files.
private __gshared Outbuffer *F4_buf;
/* Fixups that go into F1 section
*/
struct F1_Fixups
{
Symbol *s;
uint offset;
uint value;
}
private __gshared Outbuffer *F1fixup; // array of F1_Fixups
/* Struct in which to collect per-function data, for later emission
* into .debug$S.
*/
struct FuncData
{
Symbol *sfunc;
uint section_length;
const(char)* srcfilename;
uint srcfileoff;
uint linepairstart; // starting byte index of offset/line pairs in linebuf[]
uint linepairbytes; // number of bytes for offset/line pairs
uint linepairsegment; // starting byte index of filename segment for offset/line pairs
Outbuffer *f1buf;
Outbuffer *f1fixup;
}
__gshared FuncData currentfuncdata;
private __gshared Outbuffer *funcdata; // array of FuncData's
private __gshared Outbuffer *linepair; // array of offset/line pairs
void cv8_writename(Outbuffer *buf, const(char)* name, size_t len)
{
if(config.flags2 & CFG2gms)
{
const(char)* start = name;
const(char)* cur = strchr(start, '.');
const(char)* end = start + len;
while(cur != null)
{
if(cur >= end)
{
buf.writen(start, end - start);
return;
}
buf.writen(start, cur - start);
buf.writeByte('@');
start = cur + 1;
if(start >= end)
return;
cur = strchr(start, '.');
}
buf.writen(start, end - start);
}
else
buf.writen(name, len);
}
/************************************************
* Called at the start of an object file generation.
* One source file can generate multiple object files; this starts an object file.
* Input:
* filename source file name
*/
void cv8_initfile(const(char)* filename)
{
//printf("cv8_initfile()\n");
// Recycle buffers; much faster than delete/renew
if (!F1_buf)
{
__gshared Outbuffer f1buf;
f1buf.enlarge(1024);
F1_buf = &f1buf;
}
F1_buf.setsize(0);
if (!F1fixup)
{
__gshared Outbuffer f1fixupbuf;
f1fixupbuf.enlarge(1024);
F1fixup = &f1fixupbuf;
}
F1fixup.setsize(0);
if (!F2_buf)
{
__gshared Outbuffer f2buf;
f2buf.enlarge(1024);
F2_buf = &f2buf;
}
F2_buf.setsize(0);
if (!F3_buf)
{
__gshared Outbuffer f3buf;
f3buf.enlarge(1024);
F3_buf = &f3buf;
}
F3_buf.setsize(0);
F3_buf.writeByte(0); // first "filename"
if (!F4_buf)
{
__gshared Outbuffer f4buf;
f4buf.enlarge(1024);
F4_buf = &f4buf;
}
F4_buf.setsize(0);
if (!funcdata)
{
__gshared Outbuffer funcdatabuf;
funcdatabuf.enlarge(1024);
funcdata = &funcdatabuf;
}
funcdata.setsize(0);
if (!linepair)
{
__gshared Outbuffer linepairbuf;
linepairbuf.enlarge(1024);
linepair = &linepairbuf;
}
linepair.setsize(0);
memset(¤tfuncdata, 0, currentfuncdata.sizeof);
currentfuncdata.f1buf = F1_buf;
currentfuncdata.f1fixup = F1fixup;
cv_init();
}
void cv8_termfile(const(char)* objfilename)
{
//printf("cv8_termfile()\n");
/* Write out the debug info sections.
*/
int seg = MsCoffObj_seg_debugS();
uint value = 4;
objmod.bytes(seg,0,4,&value);
/* Start with starting symbol in separate "F1" section
*/
Outbuffer buf;
buf.enlarge(1024);
size_t len = strlen(objfilename);
buf.writeWord(cast(int)(2 + 4 + len + 1));
buf.writeWord(S_COMPILAND_V3);
buf.write32(0);
buf.write(objfilename, cast(uint)(len + 1));
// write S_COMPILE record
buf.writeWord(2 + 1 + 1 + 2 + 1 + VERSION.length + 1);
buf.writeWord(S_COMPILE);
buf.writeByte(I64 ? 0xD0 : 6); // target machine AMD64 or x86 (Pentium II)
buf.writeByte(config.flags2 & CFG2gms ? (CPP != 0) : 'D'); // language index (C/C++/D)
buf.writeWord(0x800 | (config.inline8087 ? 0 : (1<<3))); // 32-bit, float package
buf.writeByte(VERSION.length + 1);
buf.writeByte('Z');
buf.write(VERSION.ptr, VERSION.length);
cv8_writesection(seg, 0xF1, &buf);
// Write out "F2" sections
uint length = cast(uint)funcdata.size();
ubyte *p = funcdata.buf;
for (uint u = 0; u < length; u += FuncData.sizeof)
{ FuncData *fd = cast(FuncData *)(p + u);
F2_buf.setsize(0);
F2_buf.write32(cast(uint)fd.sfunc.Soffset);
F2_buf.write32(0);
F2_buf.write32(fd.section_length);
F2_buf.write(linepair.buf + fd.linepairstart, fd.linepairbytes);
int f2seg = seg;
if (symbol_iscomdat(fd.sfunc))
{
f2seg = MsCoffObj_seg_debugS_comdat(fd.sfunc);
objmod.bytes(f2seg, 0, 4, &value);
}
uint offset = cast(uint)SegData[f2seg].SDoffset + 8;
cv8_writesection(f2seg, 0xF2, F2_buf);
objmod.reftoident(f2seg, offset, fd.sfunc, 0, CFseg | CFoff);
if (f2seg != seg && fd.f1buf.size())
{
// Write out "F1" section
const uint f1offset = cast(uint)SegData[f2seg].SDoffset;
cv8_writesection(f2seg, 0xF1, fd.f1buf);
// Fixups for "F1" section
const uint fixupLength = cast(uint)fd.f1fixup.size();
ubyte *pfixup = fd.f1fixup.buf;
for (uint v = 0; v < fixupLength; v += F1_Fixups.sizeof)
{ F1_Fixups *f = cast(F1_Fixups *)(pfixup + v);
objmod.reftoident(f2seg, f1offset + 8 + f.offset, f.s, f.value, CFseg | CFoff);
}
}
}
// Write out "F3" section
if (F3_buf.size() > 1)
cv8_writesection(seg, 0xF3, F3_buf);
// Write out "F4" section
if (F4_buf.size() > 0)
cv8_writesection(seg, 0xF4, F4_buf);
if (F1_buf.size())
{
// Write out "F1" section
uint f1offset = cast(uint)SegData[seg].SDoffset;
cv8_writesection(seg, 0xF1, F1_buf);
// Fixups for "F1" section
length = cast(uint)F1fixup.size();
p = F1fixup.buf;
for (uint u = 0; u < length; u += F1_Fixups.sizeof)
{ F1_Fixups *f = cast(F1_Fixups *)(p + u);
objmod.reftoident(seg, f1offset + 8 + f.offset, f.s, f.value, CFseg | CFoff);
}
}
// Write out .debug$T section
cv_term();
}
/************************************************
* Called at the start of a module.
* Note that there can be multiple modules in one object file.
* cv8_initfile() must be called first.
*/
void cv8_initmodule(const(char)* filename, const(char)* modulename)
{
//printf("cv8_initmodule(filename = %s, modulename = %s)\n", filename, modulename);
}
void cv8_termmodule()
{
//printf("cv8_termmodule()\n");
assert(config.objfmt == OBJ_MSCOFF);
}
/******************************************
* Called at the start of a function.
*/
void cv8_func_start(Symbol *sfunc)
{
//printf("cv8_func_start(%s)\n", sfunc.Sident);
currentfuncdata.sfunc = sfunc;
currentfuncdata.section_length = 0;
currentfuncdata.srcfilename = null;
currentfuncdata.linepairstart += currentfuncdata.linepairbytes;
currentfuncdata.linepairbytes = 0;
currentfuncdata.f1buf = F1_buf;
currentfuncdata.f1fixup = F1fixup;
if (symbol_iscomdat(sfunc))
{
// This leaks memory
currentfuncdata.f1buf = cast(Outbuffer*)mem_calloc(Outbuffer.sizeof);
currentfuncdata.f1buf.enlarge(128);
currentfuncdata.f1fixup = cast(Outbuffer*)mem_calloc(Outbuffer.sizeof);
currentfuncdata.f1fixup.enlarge(128);
}
varStats_startFunction();
}
void cv8_func_term(Symbol *sfunc)
{
//printf("cv8_func_term(%s)\n", sfunc.Sident);
assert(currentfuncdata.sfunc == sfunc);
currentfuncdata.section_length = cast(uint)(retoffset + retsize);
funcdata.write(¤tfuncdata, currentfuncdata.sizeof);
// Write function symbol
assert(tyfunc(sfunc.ty()));
idx_t typidx;
func_t* fn = sfunc.Sfunc;
if(fn.Fclass)
{
// generate member function type info
// it would be nicer if this could be in cv4_typidx, but the function info is not available there
uint nparam;
ubyte call = cv4_callconv(sfunc.Stype);
idx_t paramidx = cv4_arglist(sfunc.Stype,&nparam);
uint next = cv4_typidx(sfunc.Stype.Tnext);
type* classtype = cast(type*)fn.Fclass;
uint classidx = cv4_typidx(classtype);
type *tp = type_allocn(TYnptr, classtype);
uint thisidx = cv4_typidx(tp); // TODO
debtyp_t *d = debtyp_alloc(2 + 4 + 4 + 4 + 1 + 1 + 2 + 4 + 4);
TOWORD(d.data.ptr,LF_MFUNCTION_V2);
TOLONG(d.data.ptr + 2,next); // return type
TOLONG(d.data.ptr + 6,classidx); // class type
TOLONG(d.data.ptr + 10,thisidx); // this type
d.data.ptr[14] = call;
d.data.ptr[15] = 0; // reserved
TOWORD(d.data.ptr + 16,nparam);
TOLONG(d.data.ptr + 18,paramidx);
TOLONG(d.data.ptr + 22,0); // this adjust
typidx = cv_debtyp(d);
}
else
typidx = cv_typidx(sfunc.Stype);
const(char)* id = sfunc.prettyIdent ? sfunc.prettyIdent : prettyident(sfunc);
size_t len = strlen(id);
if(len > CV8_MAX_SYMBOL_LENGTH)
len = CV8_MAX_SYMBOL_LENGTH;
/*
* 2 length (not including these 2 bytes)
* 2 S_GPROC_V3
* 4 parent
* 4 pend
* 4 pnext
* 4 size of function
* 4 size of function prolog
* 4 offset to function epilog
* 4 type index
* 6 seg:offset of function start
* 1 flags
* n 0 terminated name string
*/
Outbuffer *buf = currentfuncdata.f1buf;
buf.reserve(cast(uint)(2 + 2 + 4 * 7 + 6 + 1 + len + 1));
buf.writeWordn(cast(int)(2 + 4 * 7 + 6 + 1 + len + 1));
buf.writeWordn(sfunc.Sclass == SCstatic ? S_LPROC_V3 : S_GPROC_V3);
buf.write32(0); // parent
buf.write32(0); // pend
buf.write32(0); // pnext
buf.write32(cast(uint)currentfuncdata.section_length); // size of function
buf.write32(cast(uint)startoffset); // size of prolog
buf.write32(cast(uint)retoffset); // offset to epilog
buf.write32(typidx);
F1_Fixups f1f;
f1f.s = sfunc;
f1f.offset = cast(uint)buf.size();
f1f.value = 0;
currentfuncdata.f1fixup.write(&f1f, f1f.sizeof);
buf.write32(0);
buf.writeWordn(0);
buf.writeByte(0);
buf.writen(id, len);
buf.writeByte(0);
struct cv8
{
nothrow:
// record for CV record S_BLOCK_V3
struct block_v3_data
{
ushort len;
ushort id;
uint pParent;
uint pEnd;
uint length;
uint offset;
ushort seg;
ubyte[1] name;
}
extern (C++) static void endArgs()
{
Outbuffer *buf = currentfuncdata.f1buf;
buf.writeWord(2);
buf.writeWord(S_ENDARG);
}
extern (C++) static void beginBlock(int offset, int length)
{
Outbuffer *buf = currentfuncdata.f1buf;
uint soffset = cast(uint)buf.size();
// parent and end to be filled by linker
block_v3_data block32 = { block_v3_data.sizeof - 2, S_BLOCK_V3, 0, 0, length, offset, 0, [ 0 ] };
buf.write(&block32, block32.sizeof);
size_t offOffset = cast(char*)&block32.offset - cast(char*)&block32;
F1_Fixups f1f;
f1f.s = currentfuncdata.sfunc;
f1f.offset = cast(uint)(soffset + offOffset);
f1f.value = offset;
currentfuncdata.f1fixup.write(&f1f, f1f.sizeof);
}
extern (C++) static void endBlock()
{
Outbuffer *buf = currentfuncdata.f1buf;
buf.writeWord(2);
buf.writeWord(S_END);
}
}
varStats_writeSymbolTable(&globsym, &cv8_outsym, &cv8.endArgs, &cv8.beginBlock, &cv8.endBlock);
/* Put out function return record S_RETURN
* (VC doesn't, so we won't bother, either.)
*/
// Write function end symbol
buf.writeWord(2);
buf.writeWord(S_END);
currentfuncdata.f1buf = F1_buf;
currentfuncdata.f1fixup = F1fixup;
}
/**********************************************
*/
void cv8_linnum(Srcpos srcpos, uint offset)
{
//printf("cv8_linnum(file = %s, line = %d, offset = x%x)\n", srcpos.Sfilename, (int)srcpos.Slinnum, (uint)offset);
if (!srcpos.Sfilename)
return;
varStats_recordLineOffset(srcpos, offset);
__gshared uint lastoffset;
__gshared uint lastlinnum;
if (!currentfuncdata.srcfilename ||
(currentfuncdata.srcfilename != srcpos.Sfilename && strcmp(currentfuncdata.srcfilename, srcpos.Sfilename)))
{
currentfuncdata.srcfilename = srcpos.Sfilename;
uint srcfileoff = cv8_addfile(srcpos.Sfilename);
// new file segment
currentfuncdata.linepairsegment = currentfuncdata.linepairstart + currentfuncdata.linepairbytes;
linepair.write32(srcfileoff);
linepair.write32(0); // reserve space for length information
linepair.write32(12);
currentfuncdata.linepairbytes += 12;
}
else if (offset <= lastoffset || srcpos.Slinnum == lastlinnum)
return; // avoid multiple entries for the same offset
lastoffset = offset;
lastlinnum = srcpos.Slinnum;
linepair.write32(offset);
linepair.write32(srcpos.Slinnum | 0x80000000); // mark as statement, not expression
currentfuncdata.linepairbytes += 8;
// update segment length
auto segmentbytes = currentfuncdata.linepairstart + currentfuncdata.linepairbytes - currentfuncdata.linepairsegment;
auto segmentheader = cast(uint*)(linepair.buf + currentfuncdata.linepairsegment);
segmentheader[1] = (segmentbytes - 12) / 8;
segmentheader[2] = segmentbytes;
}
/**********************************************
* Add source file, if it isn't already there.
* Return offset into F4.
*/
uint cv8_addfile(const(char)* filename)
{
//printf("cv8_addfile('%s')\n", filename);
/* The algorithms here use a linear search. This is acceptable only
* because we expect only 1 or 2 files to appear.
* Unlike C, there won't be lots of .h source files to be accounted for.
*/
uint length = cast(uint)F3_buf.size();
ubyte *p = F3_buf.buf;
size_t len = strlen(filename);
// ensure the filename is absolute to help the debugger to find the source
// without having to know the working directory during compilation
__gshared char[260] cwd = 0;
__gshared uint cwdlen;
bool abs = (*filename == '\\') ||
(*filename == '/') ||
(*filename && filename[1] == ':');
if (!abs && cwd[0] == 0)
{
if (getcwd(cwd.ptr, cwd.sizeof))
{
cwdlen = cast(uint)strlen(cwd.ptr);
if(cwd[cwdlen - 1] != '\\' && cwd[cwdlen - 1] != '/')
cwd[cwdlen++] = '\\';
}
}
uint off = 1;
while (off + len < length)
{
if (!abs)
{
if (memcmp(p + off, cwd.ptr, cwdlen) == 0 &&
memcmp(p + off + cwdlen, filename, len + 1) == 0)
goto L1;
}
else if (memcmp(p + off, filename, len + 1) == 0)
{ // Already there
//printf("\talready there at %x\n", off);
goto L1;
}
off += strlen(cast(const(char)* )(p + off)) + 1;
}
off = length;
// Add it
if(!abs)
F3_buf.write(cwd.ptr, cwdlen);
F3_buf.write(filename, cast(uint)(len + 1));
L1:
// off is the offset of the filename in F3.
// Find it in F4.
length = cast(uint)F4_buf.size();
p = F4_buf.buf;
uint u = 0;
while (u + 8 <= length)
{
//printf("\t%x\n", *(uint *)(p + u));
if (off == *cast(uint *)(p + u))
{
//printf("\tfound %x\n", u);
return u;
}
u += 4;
ushort type = *cast(ushort *)(p + u);
u += 2;
if (type == 0x0110)
u += 16; // MD5 checksum
u += 2;
}
// Not there. Add it.
F4_buf.write32(off);
/* Write 10 01 [MD5 checksum]
* or
* 00 00
*/
F4_buf.writeShort(0);
// 2 bytes of pad
F4_buf.writeShort(0);
//printf("\tadded %x\n", length);
return length;
}
void cv8_writesection(int seg, uint type, Outbuffer *buf)
{
/* Write out as:
* bytes desc
* -------+----
* 4 type
* 4 length
* length data
* pad pad to 4 byte boundary
*/
uint off = cast(uint)SegData[seg].SDoffset;
objmod.bytes(seg,off,4,&type);
uint length = cast(uint)buf.size();
objmod.bytes(seg,off+4,4,&length);
objmod.bytes(seg,off+8,length,buf.buf);
// Align to 4
uint pad = ((length + 3) & ~3) - length;
objmod.lidata(seg,off+8+length,pad);
}
void cv8_outsym(Symbol *s)
{
//printf("cv8_outsym(s = '%s')\n", s.Sident);
//type_print(s.Stype);
//symbol_print(s);
if (s.Sflags & SFLnodebug)
return;
idx_t typidx = cv_typidx(s.Stype);
//printf("typidx = %x\n", typidx);
const(char)* id = s.prettyIdent ? s.prettyIdent : prettyident(s);
size_t len = strlen(id);
if(len > CV8_MAX_SYMBOL_LENGTH)
len = CV8_MAX_SYMBOL_LENGTH;
F1_Fixups f1f;
f1f.value = 0;
Outbuffer *buf = currentfuncdata.f1buf;
uint sr;
uint base;
switch (s.Sclass)
{
case SCparameter:
case SCregpar:
case SCshadowreg:
if (s.Sfl == FLreg)
{
s.Sfl = FLpara;
cv8_outsym(s);
s.Sfl = FLreg;
goto case_register;
}
base = cast(uint)(Para.size - BPoff); // cancel out add of BPoff
goto L1;
case SCauto:
if (s.Sfl == FLreg)
goto case_register;
case_auto:
base = cast(uint)Auto.size;
L1:
if (s.Sscope) // local variables moved into the closure cannot be emitted directly
break;
static if (1)
{
// Register relative addressing
buf.reserve(cast(uint)(2 + 2 + 4 + 4 + 2 + len + 1));
buf.writeWordn(cast(uint)(2 + 4 + 4 + 2 + len + 1));
buf.writeWordn(0x1111);
buf.write32(cast(uint)(s.Soffset + base + BPoff));
buf.write32(typidx);
buf.writeWordn(I64 ? 334 : 22); // relative to RBP/EBP
cv8_writename(buf, id, len);
buf.writeByte(0);
}
else
{
// This is supposed to work, implicit BP relative addressing, but it does not
buf.reserve(2 + 2 + 4 + 4 + len + 1);
buf.writeWordn( 2 + 4 + 4 + len + 1);
buf.writeWordn(S_BPREL_V3);
buf.write32(s.Soffset + base + BPoff);
buf.write32(typidx);
cv8_writename(buf, id, len);
buf.writeByte(0);
}
break;
case SCbprel:
base = -BPoff;
goto L1;
case SCfastpar:
if (s.Sfl != FLreg)
{ base = cast(uint)Fast.size;
goto L1;
}
goto L2;
case SCregister:
if (s.Sfl != FLreg)
goto case_auto;
goto case;
case SCpseudo:
case_register:
L2:
buf.reserve(cast(uint)(2 + 2 + 4 + 2 + len + 1));
buf.writeWordn(cast(uint)(2 + 4 + 2 + len + 1));
buf.writeWordn(S_REGISTER_V3);
buf.write32(typidx);
buf.writeWordn(cv8_regnum(s));
cv8_writename(buf, id, len);
buf.writeByte(0);
break;
case SCextern:
break;
case SCstatic:
case SClocstat:
sr = S_LDATA_V3;
goto Ldata;
case SCglobal:
case SCcomdat:
case SCcomdef:
sr = S_GDATA_V3;
Ldata:
/*
* 2 length (not including these 2 bytes)
* 2 S_GDATA_V2
* 4 typidx
* 6 ref to symbol
* n 0 terminated name string
*/
if (s.ty() & mTYthread) // thread local storage
sr = (sr == S_GDATA_V3) ? 0x1113 : 0x1112;
buf.reserve(cast(uint)(2 + 2 + 4 + 6 + len + 1));
buf.writeWordn(cast(uint)(2 + 4 + 6 + len + 1));
buf.writeWordn(sr);
buf.write32(typidx);
f1f.s = s;
f1f.offset = cast(uint)buf.size();
F1fixup.write(&f1f, f1f.sizeof);
buf.write32(0);
buf.writeWordn(0);
cv8_writename(buf, id, len);
buf.writeByte(0);
break;
default:
break;
}
}
/*******************************************
* Put out a name for a user defined type.
* Input:
* id the name
* typidx and its type
*/
void cv8_udt(const(char)* id, idx_t typidx)
{
//printf("cv8_udt('%s', %x)\n", id, typidx);
Outbuffer *buf = currentfuncdata.f1buf;
size_t len = strlen(id);
if (len > CV8_MAX_SYMBOL_LENGTH)
len = CV8_MAX_SYMBOL_LENGTH;
buf.reserve(cast(uint)(2 + 2 + 4 + len + 1));
buf.writeWordn(cast(uint)(2 + 4 + len + 1));
buf.writeWordn(S_UDT_V3);
buf.write32(typidx);
cv8_writename(buf, id, len);
buf.writeByte(0);
}
/*********************************************
* Get Codeview register number for symbol s.
*/
int cv8_regnum(Symbol *s)
{
int reg = s.Sreglsw;
assert(s.Sfl == FLreg);
if ((1 << reg) & XMMREGS)
return reg - XMM0 + 154;
switch (type_size(s.Stype))
{
case 1:
if (reg < 4)
reg += 1;
else if (reg >= 4 && reg < 8)
reg += 324 - 4;
else
reg += 344 - 4;
break;
case 2:
if (reg < 8)
reg += 9;
else
reg += 352 - 8;
break;
case 4:
if (reg < 8)
reg += 17;
else
reg += 360 - 8;
break;
case 8:
reg += 328;
break;
default:
reg = 0;
break;
}
return reg;
}
/***************************************
* Put out a forward ref for structs, unions, and classes.
* Only put out the real definitions with toDebug().
*/
idx_t cv8_fwdref(Symbol *s)
{
assert(config.fulltypes == CV8);
// if (s.Stypidx && !global.params.multiobj)
// return s.Stypidx;
struct_t *st = s.Sstruct;
uint leaf;
uint numidx;
if (st.Sflags & STRunion)
{
leaf = LF_UNION_V3;
numidx = 10;
}
else if (st.Sflags & STRclass)
{
leaf = LF_CLASS_V3;
numidx = 18;
}
else
{
leaf = LF_STRUCTURE_V3;
numidx = 18;
}
uint len = numidx + cv4_numericbytes(0);
int idlen = cast(int)strlen(s.Sident.ptr);
if (idlen > CV8_MAX_SYMBOL_LENGTH)
idlen = CV8_MAX_SYMBOL_LENGTH;
debtyp_t *d = debtyp_alloc(len + idlen + 1);
TOWORD(d.data.ptr, leaf);
TOWORD(d.data.ptr + 2, 0); // number of fields
TOWORD(d.data.ptr + 4, 0x80); // property
TOLONG(d.data.ptr + 6, 0); // field list
if (leaf == LF_CLASS_V3 || leaf == LF_STRUCTURE_V3)
{
TOLONG(d.data.ptr + 10, 0); // dList
TOLONG(d.data.ptr + 14, 0); // vshape
}
cv4_storenumeric(d.data.ptr + numidx, 0);
cv_namestring(d.data.ptr + len, s.Sident.ptr, idlen);
d.data.ptr[len + idlen] = 0;
idx_t typidx = cv_debtyp(d);
s.Stypidx = typidx;
return typidx;
}
/****************************************
* Return type index for a darray of type E[]
* Input:
* t darray type
* etypidx type index for E
*/
idx_t cv8_darray(type *t, idx_t etypidx)
{
//printf("cv8_darray(etypidx = %x)\n", etypidx);
/* Put out a struct:
* struct dArray {
* size_t length;
* E* ptr;
* }
*/
static if (0)
{
d = debtyp_alloc(18);
TOWORD(d.data.ptr, 0x100F);
TOWORD(d.data.ptr + 2, OEM);
TOWORD(d.data.ptr + 4, 1); // 1 = dynamic array
TOLONG(d.data.ptr + 6, 2); // count of type indices to follow
TOLONG(d.data.ptr + 10, 0x23); // index type, T_UQUAD
TOLONG(d.data.ptr + 14, next); // element type
return cv_debtyp(d);
}
type *tp = type_pointer(t.Tnext);
idx_t ptridx = cv4_typidx(tp);
type_free(tp);
__gshared const ubyte[38] fl =
[
0x03, 0x12, // LF_FIELDLIST_V2
0x0d, 0x15, // LF_MEMBER_V3
0x03, 0x00, // attribute
0x23, 0x00, 0x00, 0x00, // size_t
0x00, 0x00, // offset
'l', 'e', 'n', 'g', 't', 'h', 0x00,
0xf3, 0xf2, 0xf1, // align to 4-byte including length word before data
0x0d, 0x15,
0x03, 0x00,
0x00, 0x00, 0x00, 0x00, // etypidx
0x08, 0x00,
'p', 't', 'r', 0x00,
0xf2, 0xf1,
];
debtyp_t *f = debtyp_alloc(fl.sizeof);
memcpy(f.data.ptr,fl.ptr,fl.sizeof);
TOLONG(f.data.ptr + 6, I64 ? 0x23 : 0x22); // size_t
TOLONG(f.data.ptr + 26, ptridx);
TOWORD(f.data.ptr + 30, _tysize[TYnptr]);
idx_t fieldlist = cv_debtyp(f);
const(char)* id;
switch (t.Tnext.Tty)
{
case mTYimmutable | TYchar:
id = "string";
break;
case mTYimmutable | TYwchar_t:
id = "wstring";
break;
case mTYimmutable | TYdchar:
id = "dstring";
break;
default: