-
Notifications
You must be signed in to change notification settings - Fork 0
/
toobj.d
1608 lines (1376 loc) · 47.5 KB
/
toobj.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) 1999-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/tocsym.d, _toobj.d)
* Documentation: https://dlang.org/phobos/dmd_toobj.html
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/toobj.d
*/
module dmd.toobj;
import core.stdc.stdio;
import core.stdc.stddef;
import core.stdc.string;
import core.stdc.time;
import dmd.root.array;
import dmd.root.outbuffer;
import dmd.root.rmem;
import dmd.root.rootobject;
import dmd.aggregate;
import dmd.arraytypes;
import dmd.attrib;
import dmd.dclass;
import dmd.declaration;
import dmd.denum;
import dmd.dmodule;
import dmd.dscope;
import dmd.dstruct;
import dmd.dsymbol;
import dmd.dtemplate;
import dmd.errors;
import dmd.expression;
import dmd.func;
import dmd.globals;
import dmd.glue;
import dmd.hdrgen;
import dmd.id;
import dmd.init;
import dmd.irstate;
import dmd.mtype;
import dmd.nspace;
import dmd.objc_glue;
import dmd.statement;
import dmd.staticassert;
import dmd.target;
import dmd.tocsym;
import dmd.toctype;
import dmd.tocvdebug;
import dmd.todt;
import dmd.tokens;
import dmd.traits;
import dmd.typinf;
import dmd.visitor;
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.dt;
import dmd.backend.el;
import dmd.backend.global;
import dmd.backend.obj;
import dmd.backend.oper;
import dmd.backend.ty;
import dmd.backend.type;
extern (C++):
alias toSymbol = dmd.tocsym.toSymbol;
alias toSymbol = dmd.glue.toSymbol;
/* ================================================================== */
// Put out instance of ModuleInfo for this Module
void genModuleInfo(Module m)
{
//printf("Module.genmoduleinfo() %s\n", m.toChars());
if (!Module.moduleinfo)
{
ObjectNotFound(Id.ModuleInfo);
}
Symbol *msym = toSymbol(m);
//////////////////////////////////////////////
m.csym.Sclass = SCglobal;
m.csym.Sfl = FLdata;
auto dtb = DtBuilder(0);
ClassDeclarations aclasses;
//printf("members.dim = %d\n", members.dim);
foreach (i; 0 .. m.members.dim)
{
Dsymbol member = (*m.members)[i];
//printf("\tmember '%s'\n", member.toChars());
member.addLocalClass(&aclasses);
}
// importedModules[]
size_t aimports_dim = m.aimports.dim;
for (size_t i = 0; i < m.aimports.dim; i++)
{
Module mod = m.aimports[i];
if (!mod.needmoduleinfo)
aimports_dim--;
}
FuncDeclaration sgetmembers = m.findGetMembers();
// These must match the values in druntime/src/object_.d
enum
{
MIstandalone = 0x4,
MItlsctor = 0x8,
MItlsdtor = 0x10,
MIctor = 0x20,
MIdtor = 0x40,
MIxgetMembers = 0x80,
MIictor = 0x100,
MIunitTest = 0x200,
MIimportedModules = 0x400,
MIlocalClasses = 0x800,
MIname = 0x1000,
}
uint flags = 0;
if (!m.needmoduleinfo)
flags |= MIstandalone;
if (m.sctor)
flags |= MItlsctor;
if (m.sdtor)
flags |= MItlsdtor;
if (m.ssharedctor)
flags |= MIctor;
if (m.sshareddtor)
flags |= MIdtor;
if (sgetmembers)
flags |= MIxgetMembers;
if (m.sictor)
flags |= MIictor;
if (m.stest)
flags |= MIunitTest;
if (aimports_dim)
flags |= MIimportedModules;
if (aclasses.dim)
flags |= MIlocalClasses;
flags |= MIname;
dtb.dword(flags); // _flags
dtb.dword(0); // _index
if (flags & MItlsctor)
dtb.xoff(m.sctor, 0, TYnptr);
if (flags & MItlsdtor)
dtb.xoff(m.sdtor, 0, TYnptr);
if (flags & MIctor)
dtb.xoff(m.ssharedctor, 0, TYnptr);
if (flags & MIdtor)
dtb.xoff(m.sshareddtor, 0, TYnptr);
if (flags & MIxgetMembers)
dtb.xoff(toSymbol(sgetmembers), 0, TYnptr);
if (flags & MIictor)
dtb.xoff(m.sictor, 0, TYnptr);
if (flags & MIunitTest)
dtb.xoff(m.stest, 0, TYnptr);
if (flags & MIimportedModules)
{
dtb.size(aimports_dim);
foreach (i; 0 .. m.aimports.dim)
{
Module mod = m.aimports[i];
if (!mod.needmoduleinfo)
continue;
Symbol *s = toSymbol(mod);
/* Weak references don't pull objects in from the library,
* they resolve to 0 if not pulled in by something else.
* Don't pull in a module just because it was imported.
*/
s.Sflags |= SFLweak;
dtb.xoff(s, 0, TYnptr);
}
}
if (flags & MIlocalClasses)
{
dtb.size(aclasses.dim);
foreach (i; 0 .. aclasses.dim)
{
ClassDeclaration cd = aclasses[i];
dtb.xoff(toSymbol(cd), 0, TYnptr);
}
}
if (flags & MIname)
{
// Put out module name as a 0-terminated string, to save bytes
m.nameoffset = dtb.length();
const(char) *name = m.toPrettyChars();
m.namelen = strlen(name);
dtb.nbytes(cast(uint)m.namelen + 1, name);
//printf("nameoffset = x%x\n", nameoffset);
}
objc.generateModuleInfo(m);
m.csym.Sdt = dtb.finish();
out_readonly(m.csym);
outdata(m.csym);
//////////////////////////////////////////////
objmod.moduleinfo(msym);
}
/*****************************************
* write pointer references for typed data to the object file
* a class type is considered to mean a reference to a class instance
* Params:
* type = type of the data to check for pointers
* s = symbol that contains the data
* offset = offset of the data inside the Symbol's memory
*/
void write_pointers(Type type, Symbol *s, uint offset)
{
uint ty = type.toBasetype().ty;
if (ty == Tclass)
return objmod.write_pointerRef(s, offset);
write_instance_pointers(type, s, offset);
}
/*****************************************
* write pointer references for typed data to the object file
* a class type is considered to mean the instance, not a reference
* Params:
* type = type of the data to check for pointers
* s = symbol that contains the data
* offset = offset of the data inside the Symbol's memory
*/
void write_instance_pointers(Type type, Symbol *s, uint offset)
{
if (!type.hasPointers())
return;
Array!(d_uns64) data;
d_uns64 sz = getTypePointerBitmap(Loc.initial, type, &data);
if (sz == d_uns64.max)
return;
const bytes_size_t = cast(size_t)Type.tsize_t.size(Loc.initial);
const bits_size_t = bytes_size_t * 8;
auto words = cast(size_t)(sz / bytes_size_t);
for (size_t i = 0; i < data.dim; i++)
{
size_t bits = words < bits_size_t ? words : bits_size_t;
for (size_t b = 0; b < bits; b++)
if (data[i] & (1L << b))
{
auto off = cast(uint) ((i * bits_size_t + b) * bytes_size_t);
objmod.write_pointerRef(s, off + offset);
}
words -= bits;
}
}
/* ================================================================== */
void toObjFile(Dsymbol ds, bool multiobj)
{
//printf("toObjFile(%s)\n", ds.toChars());
extern (C++) final class ToObjFile : Visitor
{
alias visit = Visitor.visit;
public:
bool multiobj;
this(bool multiobj)
{
this.multiobj = multiobj;
}
void visitNoMultiObj(Dsymbol ds)
{
bool multiobjsave = multiobj;
multiobj = false;
ds.accept(this);
multiobj = multiobjsave;
}
override void visit(Dsymbol ds)
{
//printf("Dsymbol.toObjFile('%s')\n", ds.toChars());
// ignore
}
override void visit(FuncDeclaration fd)
{
// in glue.c
FuncDeclaration_toObjFile(fd, multiobj);
}
override void visit(ClassDeclaration cd)
{
//printf("ClassDeclaration.toObjFile('%s')\n", cd.toChars());
if (cd.type.ty == Terror)
{
cd.error("had semantic errors when compiling");
return;
}
if (!cd.members)
return;
if (multiobj && !cd.hasStaticCtorOrDtor())
{
obj_append(cd);
return;
}
if (global.params.symdebugref)
Type_toCtype(cd.type); // calls toDebug() only once
else if (global.params.symdebug)
toDebug(cd);
assert(cd.semanticRun >= PASS.semantic3done); // semantic() should have been run to completion
enum_SC scclass = SCcomdat;
// Put out the members
/* There might be static ctors in the members, and they cannot
* be put in separate obj files.
*/
cd.members.foreachDsymbol( (s) { s.accept(this); } );
if (cd.classKind == ClassKind.objc)
{
objc.toObjFile(cd);
return;
}
// If something goes wrong during this pass don't bother with the
// rest as we may have incomplete info
// https://issues.dlang.org/show_bug.cgi?id=17918
if (!finishVtbl(cd))
{
return;
}
const bool gentypeinfo = global.params.useTypeInfo && Type.dtypeinfo;
const bool genclassinfo = gentypeinfo || !(cd.isCPPclass || cd.isCOMclass);
// Generate C symbols
if (genclassinfo)
toSymbol(cd); // __ClassZ symbol
toVtblSymbol(cd); // __vtblZ symbol
Symbol *sinit = toInitializer(cd); // __initZ symbol
//////////////////////////////////////////////
// Generate static initializer
{
sinit.Sclass = scclass;
sinit.Sfl = FLdata;
auto dtb = DtBuilder(0);
ClassDeclaration_toDt(cd, dtb);
sinit.Sdt = dtb.finish();
out_readonly(sinit);
outdata(sinit);
}
//////////////////////////////////////////////
// Put out the TypeInfo
if (gentypeinfo)
genTypeInfo(cd.loc, cd.type, null);
//toObjFile(cd.type.vtinfo, multiobj);
if (genclassinfo)
{
genClassInfoForClass(cd, sinit);
}
//////////////////////////////////////////////
// Put out the vtbl[]
//printf("putting out %s.vtbl[]\n", toChars());
auto dtbv = DtBuilder(0);
if (cd.vtblOffset())
dtbv.xoff(cd.csym, 0, TYnptr); // first entry is ClassInfo reference
foreach (i; cd.vtblOffset() .. cd.vtbl.dim)
{
FuncDeclaration fd = cd.vtbl[i].isFuncDeclaration();
//printf("\tvtbl[%d] = %p\n", i, fd);
if (fd && (fd.fbody || !cd.isAbstract()))
{
dtbv.xoff(toSymbol(fd), 0, TYnptr);
}
else
dtbv.size(0);
}
if (dtbv.isZeroLength())
{
/* Someone made an 'extern (C++) class C { }' with no virtual functions.
* But making an empty vtbl[] causes linking problems, so make a dummy
* entry.
*/
dtbv.size(0);
}
cd.vtblsym.csym.Sdt = dtbv.finish();
cd.vtblsym.csym.Sclass = scclass;
cd.vtblsym.csym.Sfl = FLdata;
out_readonly(cd.vtblsym.csym);
outdata(cd.vtblsym.csym);
if (cd.isExport())
objmod.export_symbol(cd.vtblsym.csym,0);
}
override void visit(InterfaceDeclaration id)
{
//printf("InterfaceDeclaration.toObjFile('%s')\n", id.toChars());
if (id.type.ty == Terror)
{
id.error("had semantic errors when compiling");
return;
}
if (!id.members)
return;
if (global.params.symdebugref)
Type_toCtype(id.type); // calls toDebug() only once
else if (global.params.symdebug)
toDebug(id);
// Put out the members
id.members.foreachDsymbol( (s) { visitNoMultiObj(s); } );
// Generate C symbols
toSymbol(id);
//////////////////////////////////////////////
// Put out the TypeInfo
if (global.params.useTypeInfo && Type.dtypeinfo)
{
genTypeInfo(id.loc, id.type, null);
id.type.vtinfo.accept(this);
}
//////////////////////////////////////////////
genClassInfoForInterface(id);
}
override void visit(StructDeclaration sd)
{
//printf("StructDeclaration.toObjFile('%s')\n", sd.toChars());
if (sd.type.ty == Terror)
{
sd.error("had semantic errors when compiling");
return;
}
if (multiobj && !sd.hasStaticCtorOrDtor())
{
obj_append(sd);
return;
}
// Anonymous structs/unions only exist as part of others,
// do not output forward referenced structs's
if (!sd.isAnonymous() && sd.members)
{
if (global.params.symdebugref)
Type_toCtype(sd.type); // calls toDebug() only once
else if (global.params.symdebug)
toDebug(sd);
if (global.params.useTypeInfo && Type.dtypeinfo)
genTypeInfo(sd.loc, sd.type, null);
// Generate static initializer
toInitializer(sd);
if (sd.isInstantiated())
{
sd.sinit.Sclass = SCcomdat;
}
else
{
sd.sinit.Sclass = SCglobal;
}
sd.sinit.Sfl = FLdata;
auto dtb = DtBuilder(0);
StructDeclaration_toDt(sd, dtb);
sd.sinit.Sdt = dtb.finish();
out_readonly(sd.sinit); // put in read-only segment
outdata(sd.sinit);
// Put out the members
/* There might be static ctors in the members, and they cannot
* be put in separate obj files.
*/
sd.members.foreachDsymbol( (s) { s.accept(this); } );
if (sd.xeq && sd.xeq != StructDeclaration.xerreq)
sd.xeq.accept(this);
if (sd.xcmp && sd.xcmp != StructDeclaration.xerrcmp)
sd.xcmp.accept(this);
if (sd.xhash)
sd.xhash.accept(this);
}
}
override void visit(VarDeclaration vd)
{
//printf("VarDeclaration.toObjFile(%p '%s' type=%s) protection %d\n", vd, vd.toChars(), vd.type.toChars(), vd.protection);
//printf("\talign = %d\n", vd.alignment);
if (vd.type.ty == Terror)
{
vd.error("had semantic errors when compiling");
return;
}
if (vd.aliassym)
{
visitNoMultiObj(vd.toAlias());
return;
}
// Do not store variables we cannot take the address of
if (!vd.canTakeAddressOf())
{
return;
}
if (!vd.isDataseg() || vd.storage_class & STC.extern_)
return;
Symbol *s = toSymbol(vd);
d_uns64 sz64 = vd.type.size(vd.loc);
if (sz64 == SIZE_INVALID)
{
vd.error("size overflow");
return;
}
if (sz64 >= target.maxStaticDataSize)
{
vd.error("size of 0x%llx exceeds max allowed size 0x%llx", sz64, target.maxStaticDataSize);
}
uint sz = cast(uint)sz64;
Dsymbol parent = vd.toParent();
s.Sclass = SCglobal;
do
{
/* Global template data members need to be in comdat's
* in case multiple .obj files instantiate the same
* template with the same types.
*/
if (parent.isTemplateInstance() && !parent.isTemplateMixin())
{
s.Sclass = SCcomdat;
break;
}
parent = parent.parent;
} while (parent);
s.Sfl = FLdata;
if (!sz && vd.type.toBasetype().ty != Tsarray)
assert(0); // this shouldn't be possible
auto dtb = DtBuilder(0);
if (config.objfmt == OBJ_MACH && global.params.is64bit && (s.Stype.Tty & mTYLINK) == mTYthread)
{
tlsToDt(vd, s, sz, dtb);
}
else if (!sz)
{
/* Give it a byte of data
* so we can take the 'address' of this symbol
* and avoid problematic behavior of object file format
*/
dtb.nzeros(1);
}
else if (vd._init)
{
initializerToDt(vd, dtb);
}
else
{
Type_toDt(vd.type, dtb);
}
s.Sdt = dtb.finish();
// See if we can convert a comdat to a comdef,
// which saves on exe file space.
if (s.Sclass == SCcomdat &&
s.Sdt &&
dtallzeros(s.Sdt) &&
!vd.isThreadlocal())
{
s.Sclass = SCglobal;
dt2common(&s.Sdt);
}
outdata(s);
if (vd.type.isMutable() || !vd._init)
write_pointers(vd.type, s, 0);
if (vd.isExport())
objmod.export_symbol(s, 0);
}
override void visit(EnumDeclaration ed)
{
if (ed.semanticRun >= PASS.obj) // already written
return;
//printf("EnumDeclaration.toObjFile('%s')\n", ed.toChars());
if (ed.errors || ed.type.ty == Terror)
{
ed.error("had semantic errors when compiling");
return;
}
if (ed.isAnonymous())
return;
if (global.params.symdebugref)
Type_toCtype(ed.type); // calls toDebug() only once
else if (global.params.symdebug)
toDebug(ed);
if (global.params.useTypeInfo && Type.dtypeinfo)
genTypeInfo(ed.loc, ed.type, null);
TypeEnum tc = cast(TypeEnum)ed.type;
if (!tc.sym.members || ed.type.isZeroInit(Loc.initial))
{
}
else
{
enum_SC scclass = SCglobal;
if (ed.isInstantiated())
scclass = SCcomdat;
// Generate static initializer
toInitializer(ed);
ed.sinit.Sclass = scclass;
ed.sinit.Sfl = FLdata;
auto dtb = DtBuilder(0);
Expression_toDt(tc.sym.defaultval, dtb);
ed.sinit.Sdt = dtb.finish();
outdata(ed.sinit);
}
ed.semanticRun = PASS.obj;
}
override void visit(TypeInfoDeclaration tid)
{
if (isSpeculativeType(tid.tinfo))
{
//printf("-speculative '%s'\n", tid.toPrettyChars());
return;
}
//printf("TypeInfoDeclaration.toObjFile(%p '%s') protection %d\n", tid, tid.toChars(), tid.protection);
if (multiobj)
{
obj_append(tid);
return;
}
Symbol *s = toSymbol(tid);
s.Sclass = SCcomdat;
s.Sfl = FLdata;
auto dtb = DtBuilder(0);
TypeInfo_toDt(dtb, tid);
s.Sdt = dtb.finish();
// See if we can convert a comdat to a comdef,
// which saves on exe file space.
if (s.Sclass == SCcomdat &&
dtallzeros(s.Sdt))
{
s.Sclass = SCglobal;
dt2common(&s.Sdt);
}
outdata(s);
if (tid.isExport())
objmod.export_symbol(s, 0);
}
override void visit(AttribDeclaration ad)
{
Dsymbols *d = ad.include(null);
if (d)
{
for (size_t i = 0; i < d.dim; i++)
{
Dsymbol s = (*d)[i];
s.accept(this);
}
}
}
override void visit(PragmaDeclaration pd)
{
if (pd.ident == Id.lib)
{
assert(pd.args && pd.args.dim == 1);
Expression e = (*pd.args)[0];
assert(e.op == TOK.string_);
StringExp se = cast(StringExp)e;
char *name = cast(char *)mem.xmalloc(se.numberOfCodeUnits() + 1);
se.writeTo(name, true);
/* Embed the library names into the object file.
* The linker will then automatically
* search that library, too.
*/
if (!obj_includelib(name))
{
/* The format does not allow embedded library names,
* so instead append the library name to the list to be passed
* to the linker.
*/
global.params.libfiles.push(name);
}
}
else if (pd.ident == Id.framework)
{
assert(pd.args && pd.args.dim == 1);
printf("got a framework\n");
Expression e = (*pd.args)[0];
assert(e.op == TOK.string_);
StringExp se = cast(StringExp)e;
char *name = cast(char *)mem.xmalloc(se.numberOfCodeUnits() + 1);
se.writeTo(name, true);
/* Append the framework name to the list to be passed
* to the linker.
*/
global.params.frameworks.push(name);
}
else if (pd.ident == Id.startaddress)
{
assert(pd.args && pd.args.dim == 1);
Expression e = (*pd.args)[0];
Dsymbol sa = getDsymbol(e);
FuncDeclaration f = sa.isFuncDeclaration();
assert(f);
Symbol *s = toSymbol(f);
obj_startaddress(s);
}
else if (pd.ident == Id.linkerDirective)
{
assert(pd.args && pd.args.dim == 1);
Expression e = (*pd.args)[0];
assert(e.op == TOK.string_);
StringExp se = cast(StringExp)e;
char *directive = cast(char *)mem.xmalloc(se.numberOfCodeUnits() + 1);
se.writeTo(directive, true);
obj_linkerdirective(directive);
}
else if (pd.ident == Id.crt_constructor || pd.ident == Id.crt_destructor)
{
immutable isCtor = pd.ident == Id.crt_constructor;
static uint recurse(Dsymbol s, bool isCtor)
{
if (auto ad = s.isAttribDeclaration())
{
uint nestedCount;
auto decls = ad.include(null);
if (decls)
{
for (size_t i = 0; i < decls.dim; ++i)
nestedCount += recurse((*decls)[i], isCtor);
}
return nestedCount;
}
else if (auto f = s.isFuncDeclaration())
{
f.isCrtCtorDtor |= isCtor ? 1 : 2;
if (f.linkage != LINK.c)
f.error("must be `extern(C)` for `pragma(%s)`", isCtor ? "crt_constructor".ptr : "crt_destructor".ptr);
return 1;
}
else
return 0;
assert(0);
}
if (recurse(pd, isCtor) > 1)
pd.error("can only apply to a single declaration");
}
visit(cast(AttribDeclaration)pd);
}
override void visit(TemplateInstance ti)
{
//printf("TemplateInstance.toObjFile(%p, '%s')\n", ti, ti.toChars());
if (!isError(ti) && ti.members)
{
if (!ti.needsCodegen())
{
//printf("-speculative (%p, %s)\n", ti, ti.toPrettyChars());
return;
}
//printf("TemplateInstance.toObjFile(%p, '%s')\n", ti, ti.toPrettyChars());
if (multiobj)
{
// Append to list of object files to be written later
obj_append(ti);
}
else
{
ti.members.foreachDsymbol( (s) { s.accept(this); } );
}
}
}
override void visit(TemplateMixin tm)
{
//printf("TemplateMixin.toObjFile('%s')\n", tm.toChars());
if (!isError(tm))
{
tm.members.foreachDsymbol( (s) { s.accept(this); } );
}
}
override void visit(StaticAssert sa)
{
}
override void visit(Nspace ns)
{
//printf("Nspace.toObjFile('%s', this = %p)\n", ns.toChars(), ns);
if (!isError(ns) && ns.members)
{
if (multiobj)
{
// Append to list of object files to be written later
obj_append(ns);
}
else
{
ns.members.foreachDsymbol( (s) { s.accept(this); } );
}
}
}
private:
static void initializerToDt(VarDeclaration vd, ref DtBuilder dtb)
{
Initializer_toDt(vd._init, dtb);
// Look for static array that is block initialized
ExpInitializer ie = vd._init.isExpInitializer();
Type tb = vd.type.toBasetype();
if (tb.ty == Tsarray && ie &&
!tb.nextOf().equals(ie.exp.type.toBasetype().nextOf()) &&
ie.exp.implicitConvTo(tb.nextOf())
)
{
auto dim = (cast(TypeSArray)tb).dim.toInteger();
// Duplicate Sdt 'dim-1' times, as we already have the first one
while (--dim > 0)
{
Expression_toDt(ie.exp, dtb);
}
}
}
/**
* Output a TLS symbol for Mach-O.
*
* A TLS variable in the Mach-O format consists of two symbols.
* One symbol for the data, which contains the initializer, if any.
* The name of this symbol is the same as the variable, but with the
* "$tlv$init" suffix. If the variable has an initializer it's placed in
* the __thread_data section. Otherwise it's placed in the __thread_bss
* section.
*
* The other symbol is for the TLV descriptor. The symbol has the same
* name as the variable and is placed in the __thread_vars section.
* A TLV descriptor has the following structure, where T is the type of
* the variable:
*
* struct TLVDescriptor(T)
* {
* extern(C) T* function(TLVDescriptor*) thunk;
* size_t key;
* size_t offset;
* }
*
* Params:
* vd = the variable declaration for the symbol
* s = the backend Symbol corresponsing to vd
* sz = data size of s
* dtb = where to put the data
*/
static void tlsToDt(VarDeclaration vd, Symbol *s, uint sz, ref DtBuilder dtb)
{
assert(config.objfmt == OBJ_MACH && global.params.is64bit && (s.Stype.Tty & mTYLINK) == mTYthread);
Symbol *tlvInit = createTLVDataSymbol(vd, s);
auto tlvInitDtb = DtBuilder(0);
if (sz == 0)
tlvInitDtb.nzeros(1);
else if (vd._init)
initializerToDt(vd, tlvInitDtb);
else
Type_toDt(vd.type, tlvInitDtb);
tlvInit.Sdt = tlvInitDtb.finish();
outdata(tlvInit);
if (global.params.is64bit)
tlvInit.Sclass = SCextern;
Symbol* tlvBootstrap = objmod.tlv_bootstrap();
dtb.xoff(tlvBootstrap, 0, TYnptr);
dtb.size(0);
dtb.xoff(tlvInit, 0, TYnptr);
}
/**
* Creates the data symbol used to initialize a TLS variable for Mach-O.
*
* Params:
* vd = the variable declaration for the symbol
* s = the back end symbol corresponding to vd
*
* Returns: the newly created symbol
*/
static Symbol *createTLVDataSymbol(VarDeclaration vd, Symbol *s)
{
assert(config.objfmt == OBJ_MACH && global.params.is64bit && (s.Stype.Tty & mTYLINK) == mTYthread);
// Compute identifier for tlv symbol
OutBuffer buffer;
buffer.writestring(s.Sident);
buffer.writestring("$tlv$init");
const(char) *tlvInitName = buffer.peekChars();
// Compute type for tlv symbol
type *t = type_fake(vd.type.ty);
type_setty(&t, t.Tty | mTYthreadData);
type_setmangle(&t, mangle(vd));
Symbol *tlvInit = symbol_name(tlvInitName, SCstatic, t);
tlvInit.Sdt = null;
tlvInit.Salignment = type_alignsize(s.Stype);
if (vd.linkage == LINK.cpp)
tlvInit.Sflags |= SFLpublic;
return tlvInit;
}
/**