/
glue.d
1533 lines (1335 loc) · 43.6 KB
/
glue.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
/**
* Generate the object file for function declarations and critical sections.
*
* Copyright: Copyright (C) 1999-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/glue.d, _glue.d)
* Documentation: $(LINK https://dlang.org/phobos/dmd_glue.html)
* Coverage: $(LINK https://codecov.io/gh/dlang/dmd/src/master/src/dmd/glue.d)
*/
module dmd.glue;
import core.stdc.stdio;
import core.stdc.string;
import core.stdc.stdlib;
import dmd.root.array;
import dmd.root.file;
import dmd.root.filename;
import dmd.root.outbuffer;
import dmd.root.rmem;
import dmd.root.string;
import dmd.backend.cdef;
import dmd.backend.cc;
import dmd.backend.code;
import dmd.backend.dt;
import dmd.backend.el;
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.aggregate;
import dmd.arraytypes;
import dmd.astenums;
import dmd.blockexit;
import dmd.dclass;
import dmd.declaration;
import dmd.dmangle;
import dmd.dmodule;
import dmd.dmsc;
import dmd.dstruct;
import dmd.dsymbol;
import dmd.dtemplate;
import dmd.e2ir;
import dmd.errors;
import dmd.expression;
import dmd.func;
import dmd.globals;
import dmd.identifier;
import dmd.id;
import dmd.lib;
import dmd.mtype;
import dmd.objc_glue;
import dmd.s2ir;
import dmd.statement;
import dmd.target;
import dmd.tocsym;
import dmd.toctype;
import dmd.toir;
import dmd.toobj;
import dmd.typesem;
import dmd.utils;
alias symbols = Array!(Symbol*);
alias toSymbol = dmd.tocsym.toSymbol;
/**
* Generate code for `modules` and write objects/libraries
*
* Params:
* modules = array of `Module`s to generate code for
* libmodules = array of objects/libraries already generated (passed on command line)
* libname = {.lib,.a} file output name
* objdir = directory to write object files to
* lib = write library file instead of object file(s)
* obj = generate object files
* oneobj = write one object file instead of multiple ones
* multiobj = break one object file into multiple ones
* verbose = print progress message when generatig code
*/
void generateCodeAndWrite(Module[] modules, const(char)*[] libmodules,
const(char)[] libname, const(char)[] objdir,
bool lib, bool obj, bool oneobj, bool multiobj,
bool verbose)
{
Library library = null;
if (lib)
{
library = Library.factory();
library.setFilename(objdir, libname);
// Add input object and input library files to output library
foreach (p; libmodules)
library.addObject(p.toDString(), null);
}
if (!obj)
{
}
else if (oneobj)
{
Module firstm; // first module we generate code for
foreach (m; modules)
{
if (m.isHdrFile)
continue;
if (!firstm)
{
firstm = m;
obj_start(m.srcfile.toChars());
}
if (verbose)
message("code %s", m.toChars());
genObjFile(m, false);
}
if (!global.errors && firstm)
{
obj_end(library, firstm.objfile.toChars());
}
}
else
{
foreach (m; modules)
{
if (m.isHdrFile)
continue;
if (verbose)
message("code %s", m.toChars());
obj_start(m.srcfile.toChars());
genObjFile(m, multiobj);
obj_end(library, m.objfile.toChars());
obj_write_deferred(library);
if (global.errors && !lib)
m.deleteObjFile();
}
}
if (lib && !global.errors)
library.write();
}
extern (C++):
//extern
__gshared Symbol* bzeroSymbol; /// common location for immutable zeros
private __gshared
{
elem *eictor;
Symbol *ictorlocalgot;
symbols sctors;
StaticDtorDeclarations ectorgates;
symbols sdtors;
symbols stests;
symbols ssharedctors;
SharedStaticDtorDeclarations esharedctorgates;
symbols sshareddtors;
const(char)* lastmname;
Dsymbols obj_symbols_towrite;
}
/**************************************
* Append s to list of object files to generate later.
*/
void obj_append(Dsymbol s)
{
//printf("deferred: %s\n", s.toChars());
obj_symbols_towrite.push(s);
}
private void obj_write_deferred(Library library)
{
for (size_t i = 0; i < obj_symbols_towrite.dim; i++)
{
Dsymbol s = obj_symbols_towrite[i];
Module m = s.getModule();
const(char)* mname;
if (m)
{
mname = m.srcfile.toChars();
lastmname = mname;
}
else
{
//mname = s.ident.toChars();
mname = lastmname;
assert(mname);
}
obj_start(mname);
__gshared int count;
count++; // sequence for generating names
/* Create a module that's a doppelganger of m, with just
* enough to be able to create the moduleinfo.
*/
OutBuffer idbuf;
idbuf.printf("%s.%d", m ? m.ident.toChars() : mname, count);
if (!m)
{
// it doesn't make sense to make up a module if we don't know where to put the symbol
// so output it into its own object file without ModuleInfo
objmod.initfile(idbuf.peekChars(), null, mname);
toObjFile(s, false);
objmod.termfile();
}
else
{
Identifier id = Identifier.create(idbuf.extractChars());
Module md = new Module(mname.toDString, id, 0, 0);
md.members = new Dsymbols();
md.members.push(s); // its only 'member' is s
md.doppelganger = 1; // identify this module as doppelganger
md.md = m.md;
md.aimports.push(m); // it only 'imports' m
genObjFile(md, false);
}
/* Set object file name to be source name with sequence number,
* as mangled symbol names get way too long.
*/
const(char)* fname = FileName.removeExt(mname);
OutBuffer namebuf;
uint hash = 0;
for (const(char)* p = s.toChars(); *p; p++)
hash += *p;
namebuf.printf("%s_%x_%x.%.*s", fname, count, hash,
cast(int)target.obj_ext.length, target.obj_ext.ptr);
FileName.free(cast(char *)fname);
fname = namebuf.extractChars();
//printf("writing '%s'\n", fname);
obj_end(library, fname);
}
obj_symbols_towrite.dim = 0;
}
/***********************************************
* Generate function that calls array of functions and gates.
* Params:
* m = module symbol (for name mangling purposes)
* sctors = array of functions
* ectorgates = array of gates
* id = identifier string for generator function
* Returns:
* function Symbol generated
*/
private Symbol *callFuncsAndGates(Module m, symbols *sctors, StaticDtorDeclarations *ectorgates,
const(char)* id)
{
Symbol *sctor = null;
if ((sctors && sctors.dim) ||
(ectorgates && ectorgates.dim))
{
__gshared type *t;
if (!t)
{
/* t will be the type of the functions generated:
* extern (C) void func();
*/
t = type_function(TYnfunc, null, false, tstypes[TYvoid]);
t.Tmangle = mTYman_c;
}
localgot = null;
sctor = toSymbolX(m, id, SCglobal, t, "FZv");
cstate.CSpsymtab = &sctor.Sfunc.Flocsym;
elem *ector = null;
if (ectorgates)
{
foreach (f; *ectorgates)
{
Symbol *s = toSymbol(f.vgate);
elem *e = el_var(s);
e = el_bin(OPaddass, TYint, e, el_long(TYint, 1));
ector = el_combine(ector, e);
}
}
if (sctors)
{
foreach (s; *sctors)
{
elem *e = el_una(OPucall, TYvoid, el_var(s));
ector = el_combine(ector, e);
}
}
block *b = block_calloc();
b.BC = BCret;
b.Belem = ector;
sctor.Sfunc.Fstartline.Sfilename = m.arg.xarraydup.ptr;
sctor.Sfunc.Fstartblock = b;
writefunc(sctor);
}
return sctor;
}
/**************************************
* Prepare for generating obj file.
*/
private __gshared Outbuffer objbuf;
private void obj_start(const(char)* srcfile)
{
//printf("obj_start()\n");
bzeroSymbol = null;
rtlsym_reset();
clearStringTab();
version (Windows)
{
// Produce Ms COFF files for 64 bit code, OMF for 32 bit code
assert(objbuf.length() == 0);
objmod = target.mscoff ? MsCoffObj_init(&objbuf, srcfile, null)
: OmfObj_init(&objbuf, srcfile, null);
}
else
{
objmod = Obj.init(&objbuf, srcfile, null);
}
el_reset();
cg87_reset();
out_reset();
objc.reset();
}
/****************************************
* Finish creating the object module and writing it to objbuf[].
* Then either write the object module to an actual file,
* or add it to a library.
* Params:
* objfilename = what to call the object module
* library = if non-null, add object module to this library
*/
private void obj_end(Library library, const(char)* objfilename)
{
objmod.term(objfilename);
//delete objmod;
objmod = null;
if (library)
{
// Transfer ownership of image buffer to library
library.addObject(objfilename.toDString(), objbuf.extractSlice[]);
}
else
{
//printf("write obj %s\n", objfilename);
writeFile(Loc.initial, objfilename.toDString, objbuf[]);
}
objbuf.dtor();
}
bool obj_includelib(const(char)* name) nothrow
{
return objmod.includelib(name);
}
extern(D) bool obj_includelib(const(char)[] name) nothrow
{
return name.toCStringThen!(n => obj_includelib(n.ptr));
}
void obj_startaddress(Symbol *s)
{
return objmod.startaddress(s);
}
bool obj_linkerdirective(const(char)* directive)
{
return objmod.linkerdirective(directive);
}
/**************************************
* Generate .obj file for Module.
*/
private void genObjFile(Module m, bool multiobj)
{
//EEcontext *ee = env.getEEcontext();
//printf("Module.genobjfile(multiobj = %d) %s\n", multiobj, m.toChars());
lastmname = m.srcfile.toChars();
objmod.initfile(lastmname, null, m.toPrettyChars());
eictor = null;
ictorlocalgot = null;
sctors.setDim(0);
ectorgates.setDim(0);
sdtors.setDim(0);
ssharedctors.setDim(0);
esharedctorgates.setDim(0);
sshareddtors.setDim(0);
stests.setDim(0);
if (m.doppelganger)
{
/* Generate a reference to the moduleinfo, so the module constructors
* and destructors get linked in.
*/
Module mod = m.aimports[0];
assert(mod);
if (mod.sictor || mod.sctor || mod.sdtor || mod.ssharedctor || mod.sshareddtor)
{
Symbol *s = toSymbol(mod);
//objextern(s);
//if (!s.Sxtrnnum) objextdef(s.Sident);
if (!s.Sxtrnnum)
{
//printf("%s\n", s.Sident);
//#if 0 /* This should work, but causes optlink to fail in common/newlib.asm */
// objextdef(s.Sident);
//#else
Symbol *sref = symbol_generate(SCstatic, type_fake(TYnptr));
sref.Sfl = FLdata;
auto dtb = DtBuilder(0);
dtb.xoff(s, 0, TYnptr);
sref.Sdt = dtb.finish();
outdata(sref);
//#endif
}
}
}
if (global.params.cov)
{
/* Create coverage identifier:
* uint[numlines] __coverage;
*/
m.cov = toSymbolX(m, "__coverage", SCstatic, type_fake(TYint), "Z");
m.cov.Sflags |= SFLhidden;
m.cov.Stype.Tmangle = mTYman_d;
m.cov.Sfl = FLdata;
auto dtb = DtBuilder(0);
if (m.ctfe_cov)
{
// initalize the uint[] __coverage symbol with data from ctfe.
static extern (C) int comp_uints (const scope void* a, const scope void* b)
{ return (*cast(uint*) a) - (*cast(uint*) b); }
uint[] sorted_lines = m.ctfe_cov.keys;
qsort(sorted_lines.ptr, sorted_lines.length, sorted_lines[0].sizeof,
&comp_uints);
uint lastLine = 0;
foreach (line;sorted_lines)
{
// zero fill from last line to line.
if (line)
{
assert(line > lastLine);
dtb.nzeros((line - lastLine - 1) * 4);
}
dtb.dword(m.ctfe_cov[line]);
lastLine = line;
}
// zero fill from last line to end
if (m.numlines > lastLine)
dtb.nzeros((m.numlines - lastLine) * 4);
}
else
{
dtb.nzeros(4 * m.numlines);
}
m.cov.Sdt = dtb.finish();
outdata(m.cov);
m.covb = cast(uint *)calloc((m.numlines + 32) / 32, (*m.covb).sizeof);
}
for (int i = 0; i < m.members.dim; i++)
{
auto member = (*m.members)[i];
//printf("toObjFile %s %s\n", member.kind(), member.toChars());
toObjFile(member, multiobj);
}
if (global.params.cov)
{
/* Generate
* private bit[numlines] __bcoverage;
*/
Symbol *bcov = symbol_calloc("__bcoverage");
bcov.Stype = type_fake(TYuint);
bcov.Stype.Tcount++;
bcov.Sclass = SCstatic;
bcov.Sfl = FLdata;
auto dtb = DtBuilder(0);
dtb.nbytes((m.numlines + 32) / 32 * (*m.covb).sizeof, cast(char *)m.covb);
bcov.Sdt = dtb.finish();
outdata(bcov);
free(m.covb);
m.covb = null;
/* Generate:
* _d_cover_register(uint[] __coverage, BitArray __bcoverage, string filename);
* and prepend it to the static constructor.
*/
/* t will be the type of the functions generated:
* extern (C) void func();
*/
type *t = type_function(TYnfunc, null, false, tstypes[TYvoid]);
t.Tmangle = mTYman_c;
m.sictor = toSymbolX(m, "__modictor", SCglobal, t, "FZv");
cstate.CSpsymtab = &m.sictor.Sfunc.Flocsym;
localgot = ictorlocalgot;
elem *ecov = el_pair(TYdarray, el_long(TYsize_t, m.numlines), el_ptr(m.cov));
elem *ebcov = el_pair(TYdarray, el_long(TYsize_t, m.numlines), el_ptr(bcov));
if (target.os == Target.OS.Windows && target.is64bit)
{
ecov = addressElem(ecov, Type.tvoid.arrayOf(), false);
ebcov = addressElem(ebcov, Type.tvoid.arrayOf(), false);
}
elem *efilename = toEfilename(m);
if (target.os == Target.OS.Windows && target.is64bit)
efilename = addressElem(efilename, Type.tstring, true);
elem *e = el_params(
el_long(TYuchar, global.params.covPercent),
ecov,
ebcov,
efilename,
null);
e = el_bin(OPcall, TYvoid, el_var(getRtlsym(RTLSYM_DCOVER2)), e);
eictor = el_combine(e, eictor);
ictorlocalgot = localgot;
}
// If coverage / static constructor / destructor / unittest calls
if (eictor || sctors.dim || ectorgates.dim || sdtors.dim ||
ssharedctors.dim || esharedctorgates.dim || sshareddtors.dim || stests.dim)
{
if (eictor)
{
localgot = ictorlocalgot;
block *b = block_calloc();
b.BC = BCret;
b.Belem = eictor;
m.sictor.Sfunc.Fstartline.Sfilename = m.arg.xarraydup.ptr;
m.sictor.Sfunc.Fstartblock = b;
writefunc(m.sictor);
}
m.sctor = callFuncsAndGates(m, &sctors, &ectorgates, "__modctor");
m.sdtor = callFuncsAndGates(m, &sdtors, null, "__moddtor");
m.ssharedctor = callFuncsAndGates(m, &ssharedctors, cast(StaticDtorDeclarations *)&esharedctorgates, "__modsharedctor");
m.sshareddtor = callFuncsAndGates(m, &sshareddtors, null, "__modshareddtor");
m.stest = callFuncsAndGates(m, &stests, null, "__modtest");
if (m.doppelganger)
genModuleInfo(m);
}
if (m.doppelganger)
{
objc.generateModuleInfo(m);
objmod.termfile();
return;
}
/* Generate module info for templates and -cov.
* Don't generate ModuleInfo if `object.ModuleInfo` is not declared or
* explicitly disabled through compiler switches such as `-betterC`.
* Don't generate ModuleInfo for C files.
*/
if (global.params.useModuleInfo && Module.moduleinfo && !m.isCFile /*|| needModuleInfo()*/)
genModuleInfo(m);
objmod.termfile();
}
/* ================================================================== */
private UnitTestDeclaration needsDeferredNested(FuncDeclaration fd)
{
while (fd && fd.isNested())
{
FuncDeclaration fdp = fd.toParent2().isFuncDeclaration();
if (!fdp)
break;
if (UnitTestDeclaration udp = fdp.isUnitTestDeclaration())
return udp.semanticRun < PASS.obj ? udp : null;
fd = fdp;
}
return null;
}
void FuncDeclaration_toObjFile(FuncDeclaration fd, bool multiobj)
{
ClassDeclaration cd = fd.parent.isClassDeclaration();
//printf("FuncDeclaration.toObjFile(%p, %s.%s)\n", fd, fd.parent.toChars(), fd.toChars());
//if (type) printf("type = %s\n", type.toChars());
version (none)
{
//printf("line = %d\n", getWhere() / LINEINC);
EEcontext *ee = env.getEEcontext();
if (ee.EEcompile == 2)
{
if (ee.EElinnum < (getWhere() / LINEINC) ||
ee.EElinnum > (endwhere / LINEINC)
)
return; // don't compile this function
ee.EEfunc = toSymbol(this);
}
}
if (fd.semanticRun >= PASS.obj) // if toObjFile() already run
return;
if (fd.type && fd.type.ty == Tfunction && (cast(TypeFunction)fd.type).next is null)
return;
// If errors occurred compiling it, such as https://issues.dlang.org/show_bug.cgi?id=6118
if (fd.type && fd.type.ty == Tfunction && (cast(TypeFunction)fd.type).next.ty == Terror)
return;
if (fd.semantic3Errors)
return;
if (global.errors)
return;
if (!fd.fbody)
return;
UnitTestDeclaration ud = fd.isUnitTestDeclaration();
if (ud && !global.params.useUnitTests)
return;
if (multiobj && !fd.isStaticDtorDeclaration() && !fd.isStaticCtorDeclaration() && !fd.isCrtCtorDtor)
{
obj_append(fd);
return;
}
if (fd.semanticRun == PASS.semanticdone)
{
/* What happened is this function failed semantic3() with errors,
* but the errors were gagged.
* Try to reproduce those errors, and then fail.
*/
fd.error("errors compiling the function");
return;
}
assert(fd.semanticRun == PASS.semantic3done);
assert(fd.ident != Id.empty);
for (FuncDeclaration fd2 = fd; fd2; )
{
if (fd2.inNonRoot())
return;
if (fd2.isNested())
fd2 = fd2.toParent2().isFuncDeclaration();
else
break;
}
if (UnitTestDeclaration udp = needsDeferredNested(fd))
{
/* Can't do unittest's out of order, they are order dependent in that their
* execution is done in lexical order.
*/
udp.deferredNested.push(fd);
//printf("%s @[%s]\n\t-. pushed to unittest @[%s]\n",
// fd.toPrettyChars(), fd.loc.toChars(), udp.loc.toChars());
return;
}
// start code generation
fd.semanticRun = PASS.obj;
if (global.params.verbose)
message("function %s", fd.toPrettyChars());
Symbol *s = toSymbol(fd);
func_t *f = s.Sfunc;
// tunnel type of "this" to debug info generation
if (AggregateDeclaration ad = fd.parent.isAggregateDeclaration())
{
.type* t = Type_toCtype(ad.getType());
if (cd)
t = t.Tnext; // skip reference
f.Fclass = cast(Classsym *)t;
}
/* This is done so that the 'this' pointer on the stack is the same
* distance away from the function parameters, so that an overriding
* function can call the nested fdensure or fdrequire of its overridden function
* and the stack offsets are the same.
*/
if (fd.isVirtual() && (fd.fensure || fd.frequire))
f.Fflags3 |= Ffakeeh;
if (fd.eh_none)
// Same as config.ehmethod==EH_NONE, but only for this function
f.Fflags3 |= Feh_none;
s.Sclass = target.os == Target.OS.OSX ? SCcomdat : SCglobal;
for (Dsymbol p = fd.parent; p; p = p.parent)
{
if (p.isTemplateInstance())
{
// functions without D or C++ name mangling mixed in at global scope
// shouldn't have multiple definitions
if (p.isTemplateMixin() && (fd.linkage == LINK.c || fd.linkage == LINK.windows ||
fd.linkage == LINK.objc))
{
const q = p.toParent();
if (q && q.isModule())
{
s.Sclass = SCglobal;
break;
}
}
s.Sclass = SCcomdat;
break;
}
}
if (fd.inlinedNestedCallees)
{
/* https://issues.dlang.org/show_bug.cgi?id=15333
* If fd contains inlined expressions that come from
* nested function bodies, the enclosing of the functions must be
* generated first, in order to calculate correct frame pointer offset.
*/
foreach (fdc; *fd.inlinedNestedCallees)
{
FuncDeclaration fp = fdc.toParent2().isFuncDeclaration();
if (fp && fp.semanticRun < PASS.obj)
{
toObjFile(fp, multiobj);
}
}
}
if (fd.isNested())
{
//if (!(config.flags3 & CFG3pic))
// s.Sclass = SCstatic;
f.Fflags3 |= Fnested;
/* The enclosing function must have its code generated first,
* in order to calculate correct frame pointer offset.
*/
FuncDeclaration fdp = fd.toParent2().isFuncDeclaration();
if (fdp && fdp.semanticRun < PASS.obj)
{
toObjFile(fdp, multiobj);
}
}
else
{
specialFunctions(objmod, fd);
}
symtab_t *symtabsave = cstate.CSpsymtab;
cstate.CSpsymtab = &f.Flocsym;
// Find module m for this function
Module m = null;
for (Dsymbol p = fd.parent; p; p = p.parent)
{
m = p.isModule();
if (m)
break;
}
Dsymbols deferToObj; // write these to OBJ file later
Array!(elem*) varsInScope;
Label*[void*] labels = null;
IRState irs = IRState(m, fd, &varsInScope, &deferToObj, &labels, &global.params, &target);
Symbol *shidden = null;
Symbol *sthis = null;
tym_t tyf = tybasic(s.Stype.Tty);
//printf("linkage = %d, tyf = x%x\n", linkage, tyf);
int reverse = tyrevfunc(s.Stype.Tty);
assert(fd.type.ty == Tfunction);
TypeFunction tf = cast(TypeFunction)fd.type;
RET retmethod = retStyle(tf, fd.needThis());
if (retmethod == RET.stack)
{
// If function returns a struct, put a pointer to that
// as the first argument
.type *thidden = Type_toCtype(tf.next.pointerTo());
char[5 + 10 + 1] hiddenparam = void;
__gshared uint hiddenparami; // how many we've generated so far
const(char)* name;
if (fd.nrvo_can && fd.nrvo_var)
name = fd.nrvo_var.ident.toChars();
else
{
sprintf(hiddenparam.ptr, "__HID%u", ++hiddenparami);
name = hiddenparam.ptr;
}
shidden = symbol_name(name, SCparameter, thidden);
shidden.Sflags |= SFLtrue | SFLfree;
if (fd.nrvo_can && fd.nrvo_var && fd.nrvo_var.nestedrefs.dim)
type_setcv(&shidden.Stype, shidden.Stype.Tty | mTYvolatile);
irs.shidden = shidden;
fd.shidden = shidden;
}
else
{
// Register return style cannot make nrvo.
// Auto functions keep the nrvo_can flag up to here,
// so we should eliminate it before entering backend.
fd.nrvo_can = 0;
}
if (fd.vthis)
{
assert(!fd.vthis.csym);
sthis = toSymbol(fd.vthis);
sthis.Stype = getParentClosureType(sthis, fd);
irs.sthis = sthis;
if (!(f.Fflags3 & Fnested))
f.Fflags3 |= Fmember;
}
// Estimate number of parameters, pi
size_t pi = (fd.v_arguments !is null);
if (fd.parameters)
pi += fd.parameters.dim;
if (fd.objc.selector)
pi++; // Extra argument for Objective-C selector
// Create a temporary buffer, params[], to hold function parameters
Symbol*[10] paramsbuf = void;
Symbol **params = paramsbuf.ptr; // allocate on stack if possible
if (pi + 2 > paramsbuf.length) // allow extra 2 for sthis and shidden
{
params = cast(Symbol **)Mem.check(malloc((pi + 2) * (Symbol *).sizeof));
}
// Get the actual number of parameters, pi, and fill in the params[]
pi = 0;
if (fd.v_arguments)
{
params[pi] = toSymbol(fd.v_arguments);
pi += 1;
}
if (fd.parameters)
{
foreach (i, v; *fd.parameters)
{
//printf("param[%d] = %p, %s\n", i, v, v.toChars());
assert(!v.csym);
params[pi + i] = toSymbol(v);
}
pi += fd.parameters.dim;
}
if (reverse)
{
// Reverse params[] entries
foreach (i, sptmp; params[0 .. pi/2])
{
params[i] = params[pi - 1 - i];
params[pi - 1 - i] = sptmp;
}
}
if (shidden)
{
// shidden becomes last parameter
//params[pi] = shidden;
// shidden becomes first parameter
memmove(params + 1, params, pi * (params[0]).sizeof);
params[0] = shidden;
pi++;
}
pi = objc.addSelectorParameterSymbol(fd, params, pi);
if (sthis)
{
// sthis becomes last parameter
//params[pi] = sthis;
// sthis becomes first parameter
memmove(params + 1, params, pi * (params[0]).sizeof);
params[0] = sthis;
pi++;
}
if (target.isPOSIX && fd.linkage != LINK.d && shidden && sthis)
{
/* swap shidden and sthis
*/
Symbol *sp = params[0];
params[0] = params[1];
params[1] = sp;
}
foreach (sp; params[0 .. pi])
{
sp.Sclass = SCparameter;
sp.Sflags &= ~SFLspill;
sp.Sfl = FLpara;
symbol_add(sp);
}
// Determine register assignments
if (pi)
{
FuncParamRegs fpr = FuncParamRegs.create(tyf);
foreach (sp; params[0 .. pi])
{
if (fpr.alloc(sp.Stype, sp.Stype.Tty, &sp.Spreg, &sp.Spreg2))
{
sp.Sclass = (target.os == Target.OS.Windows && target.is64bit) ? SCshadowreg : SCfastpar;
sp.Sfl = (sp.Sclass == SCshadowreg) ? FLpara : FLfast;
}
}
}
// Done with params
if (params != paramsbuf.ptr)
free(params);
params = null;
localgot = null;
Statement sbody = fd.fbody;
Blockx bx;
bx.startblock = block_calloc();
bx.curblock = bx.startblock;
bx.funcsym = s;
bx.scope_index = -1;
bx.classdec = cast(void*)cd;
bx.member = cast(void*)fd;
bx._module = cast(void*)fd.getModule();
irs.blx = &bx;
// Initialize argptr
if (fd.v_argptr)
{
// Declare va_argsave
if (target.is64bit &&
target.os & Target.OS.Posix)
{
type *t = type_struct_class("__va_argsave_t", 16, 8 * 6 + 8 * 16 + 8 * 3, null, null, false, false, true, false);
// The backend will pick this up by name
Symbol *sv = symbol_name("__va_argsave", SCauto, t);
sv.Stype.Tty |= mTYvolatile;
symbol_add(sv);
}