-
Notifications
You must be signed in to change notification settings - Fork 7
/
glue.c
1237 lines (1073 loc) · 32.4 KB
/
glue.c
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 D programming language
// Copyright (c) 1999-2011 by Digital Mars
// All Rights Reserved
// written by Walter Bright
// http://www.digitalmars.com
#include <stdio.h>
#include <stddef.h>
#include <time.h>
#include <assert.h>
#if __sun&&__SVR4
#include <alloca.h>
#endif
#include "mars.h"
#include "module.h"
#include "mtype.h"
#include "declaration.h"
#include "statement.h"
#include "enum.h"
#include "aggregate.h"
#include "init.h"
#include "attrib.h"
#include "id.h"
#include "import.h"
#include "template.h"
#include "lib.h"
#include "rmem.h"
#include "cc.h"
#include "global.h"
#include "oper.h"
#include "code.h"
#include "type.h"
#include "dt.h"
#include "cgcv.h"
#include "outbuf.h"
#include "irstate.h"
struct Environment;
Environment *benv;
void slist_add(Symbol *s);
void slist_reset();
void clearStringTab();
#define STATICCTOR 0
typedef ArrayBase<symbol> symbols;
elem *eictor;
symbol *ictorlocalgot;
symbols sctors;
StaticDtorDeclarations ectorgates;
symbols sdtors;
symbols stests;
symbols ssharedctors;
SharedStaticDtorDeclarations esharedctorgates;
symbols sshareddtors;
int dtorcount;
int shareddtorcount;
char *lastmname;
/**************************************
* Append s to list of object files to generate later.
*/
Dsymbols obj_symbols_towrite;
void obj_append(Dsymbol *s)
{
obj_symbols_towrite.push(s);
}
void obj_write_deferred(Library *library)
{
for (size_t i = 0; i < obj_symbols_towrite.dim; i++)
{ Dsymbol *s = obj_symbols_towrite.tdata()[i];
Module *m = s->getModule();
char *mname;
if (m)
{ mname = m->srcfile->toChars();
lastmname = mname;
}
else
{
//mname = s->ident->toChars();
mname = lastmname;
assert(mname);
}
obj_start(mname);
static 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);
char *idstr = idbuf.toChars();
idbuf.data = NULL;
Identifier *id = new Identifier(idstr, TOKidentifier);
Module *md = new Module(mname, id, 0, 0);
md->members = new Dsymbols();
md->members->push(s); // its only 'member' is s
if (m)
{
md->doppelganger = 1; // identify this module as doppelganger
md->md = m->md;
md->aimports.push(m); // it only 'imports' m
md->massert = m->massert;
md->munittest = m->munittest;
md->marray = m->marray;
}
md->genobjfile(0);
/* Set object file name to be source name with sequence number,
* as mangled symbol names get way too long.
*/
char *fname = FileName::removeExt(mname);
OutBuffer namebuf;
unsigned hash = 0;
for (char *p = s->toChars(); *p; p++)
hash += *p;
namebuf.printf("%s_%x_%x.%s", fname, count, hash, global.obj_ext);
namebuf.writeByte(0);
mem.free(fname);
fname = (char *)namebuf.extractData();
//printf("writing '%s'\n", fname);
File *objfile = new File(fname);
obj_end(library, objfile);
}
obj_symbols_towrite.dim = 0;
}
/***********************************************
* Generate function that calls array of functions and gates.
*/
symbol *callFuncsAndGates(Module *m, symbols *sctors, StaticDtorDeclarations *ectorgates,
const char *id)
{
symbol *sctor = NULL;
if ((sctors && sctors->dim) ||
(ectorgates && ectorgates->dim))
{
static type *t;
if (!t)
{
/* t will be the type of the functions generated:
* extern (C) void func();
*/
t = type_alloc(TYnfunc);
t->Tflags |= TFprototype | TFfixed;
t->Tmangle = mTYman_c;
t->Tnext = tsvoid;
tsvoid->Tcount++;
}
localgot = NULL;
sctor = m->toSymbolX(id, SCglobal, t, "FZv");
cstate.CSpsymtab = &sctor->Sfunc->Flocsym;
elem *ector = NULL;
if (ectorgates)
{
for (size_t i = 0; i < ectorgates->dim; i++)
{ StaticDtorDeclaration *f = (*ectorgates)[i];
Symbol *s = f->vgate->toSymbol();
elem *e = el_var(s);
e = el_bin(OPaddass, TYint, e, el_long(TYint, 1));
ector = el_combine(ector, e);
}
}
if (sctors)
{
for (size_t i = 0; i < sctors->dim; i++)
{ symbol *s = (*sctors)[i];
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;
sctor->Sfunc->Fstartblock = b;
writefunc(sctor);
}
return sctor;
}
/**************************************
* Prepare for generating obj file.
*/
Outbuffer objbuf;
void obj_start(char *srcfile)
{
//printf("obj_start()\n");
rtlsym_reset();
slist_reset();
clearStringTab();
obj_init(&objbuf, srcfile, NULL);
el_reset();
#if TX86
cg87_reset();
#endif
out_reset();
}
void obj_end(Library *library, File *objfile)
{
obj_term();
if (library)
{
// Transfer image to library
library->addObject(objfile->name->toChars(), objbuf.buf, objbuf.p - objbuf.buf);
objbuf.buf = NULL;
}
else
{
// Transfer image to file
objfile->setbuffer(objbuf.buf, objbuf.p - objbuf.buf);
objbuf.buf = NULL;
char *p = FileName::path(objfile->name->toChars());
FileName::ensurePathExists(p);
//mem.free(p);
//printf("write obj %s\n", objfile->name->toChars());
objfile->writev();
}
objbuf.pend = NULL;
objbuf.p = NULL;
objbuf.len = 0;
objbuf.inc = 0;
}
/**************************************
* Generate .obj file for Module.
*/
void Module::genobjfile(int multiobj)
{
//EEcontext *ee = env->getEEcontext();
//printf("Module::genobjfile(multiobj = %d) %s\n", multiobj, toChars());
lastmname = srcfile->toChars();
obj_initfile(lastmname, NULL, 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);
dtorcount = 0;
shareddtorcount = 0;
if (doppelganger)
{
/* Generate a reference to the moduleinfo, so the module constructors
* and destructors get linked in.
*/
Module *m = aimports.tdata()[0];
assert(m);
if (m->sictor || m->sctor || m->sdtor || m->ssharedctor || m->sshareddtor)
{
Symbol *s = m->toSymbol();
//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
#if ELFOBJ || MACHOBJ
int nbytes = reftoident(DATA, Offset(DATA), s, 0, I64 ? (CFoff | CFoffset64) : CFoff);
#else
int nbytes = reftoident(DATA, Doffset, s, 0, CFoff);
Doffset += nbytes;
#endif
#endif
}
}
}
if (global.params.cov)
{
/* Create coverage identifier:
* private uint[numlines] __coverage;
*/
cov = symbol_calloc("__coverage");
cov->Stype = type_fake(TYint);
cov->Stype->Tmangle = mTYman_c;
cov->Stype->Tcount++;
cov->Sclass = SCstatic;
cov->Sfl = FLdata;
#if ELFOBJ || MACHOBJ
cov->Sseg = UDATA;
#endif
dtnzeros(&cov->Sdt, 4 * numlines);
outdata(cov);
slist_add(cov);
covb = (unsigned *)calloc((numlines + 32) / 32, sizeof(*covb));
}
for (size_t i = 0; i < members->dim; i++)
{
Dsymbol *member = members->tdata()[i];
member->toObjFile(multiobj);
}
if (global.params.cov)
{
/* Generate
* bit[numlines] __bcoverage;
*/
Symbol *bcov = symbol_calloc("__bcoverage");
bcov->Stype = type_fake(TYuint);
bcov->Stype->Tcount++;
bcov->Sclass = SCstatic;
bcov->Sfl = FLdata;
#if ELFOBJ || MACHOBJ
bcov->Sseg = DATA;
#endif
dtnbytes(&bcov->Sdt, (numlines + 32) / 32 * sizeof(*covb), (char *)covb);
outdata(bcov);
free(covb);
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_alloc(TYnfunc);
t->Tflags |= TFprototype | TFfixed;
t->Tmangle = mTYman_c;
t->Tnext = tsvoid;
tsvoid->Tcount++;
sictor = toSymbolX("__modictor", SCglobal, t, "FZv");
cstate.CSpsymtab = &sictor->Sfunc->Flocsym;
localgot = ictorlocalgot;
elem *e;
e = el_params(el_pair(TYdarray, el_long(TYsize_t, numlines), el_ptr(cov)),
el_pair(TYdarray, el_long(TYsize_t, numlines), el_ptr(bcov)),
toEfilename(),
NULL);
e = el_bin(OPcall, TYvoid, el_var(rtlsym[RTLSYM_DCOVER]), 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;
sictor->Sfunc->Fstartline.Sfilename = arg;
sictor->Sfunc->Fstartblock = b;
writefunc(sictor);
}
sctor = callFuncsAndGates(this, &sctors, &ectorgates, "__modctor");
sdtor = callFuncsAndGates(this, &sdtors, NULL, "__moddtor");
#if DMDV2
ssharedctor = callFuncsAndGates(this, &ssharedctors, (StaticDtorDeclarations *)&esharedctorgates, "__modsharedctor");
sshareddtor = callFuncsAndGates(this, &sshareddtors, NULL, "__modshareddtor");
#endif
stest = callFuncsAndGates(this, &stests, NULL, "__modtest");
if (doppelganger)
genmoduleinfo();
}
if (doppelganger)
{
obj_termfile();
return;
}
if (global.params.multiobj)
{ /* This is necessary because the main .obj for this module is written
* first, but determining whether marray or massert or munittest are needed is done
* possibly later in the doppelganger modules.
* Another way to fix it is do the main one last.
*/
toModuleAssert();
toModuleUnittest();
toModuleArray();
}
#if 1
// Always generate module info, because of templates and -cov
if (1 || needModuleInfo())
genmoduleinfo();
#endif
// If module assert
for (int i = 0; i < 3; i++)
{
Symbol *ma;
unsigned rt;
unsigned bc;
switch (i)
{
case 0: ma = marray; rt = RTLSYM_DARRAY; bc = BCexit; break;
case 1: ma = massert; rt = RTLSYM_DASSERTM; bc = BCexit; break;
case 2: ma = munittest; rt = RTLSYM_DUNITTESTM; bc = BCret; break;
default: assert(0);
}
if (ma)
{
elem *elinnum;
localgot = NULL;
// Call dassert(filename, line)
// Get sole parameter, linnum
{
Symbol *sp = symbol_calloc("linnum");
sp->Stype = type_fake(TYint);
sp->Stype->Tcount++;
sp->Sclass = SCfastpar;
sp->Spreg = I64 ? DI : AX;
sp->Sflags &= ~SFLspill;
sp->Sfl = FLpara; // FLauto?
cstate.CSpsymtab = &ma->Sfunc->Flocsym;
symbol_add(sp);
elinnum = el_var(sp);
}
elem *efilename = el_ptr(toSymbol());
elem *e = el_var(rtlsym[rt]);
e = el_bin(OPcall, TYvoid, e, el_param(elinnum, efilename));
block *b = block_calloc();
b->BC = bc;
b->Belem = e;
ma->Sfunc->Fstartline.Sfilename = arg;
ma->Sfunc->Fstartblock = b;
ma->Sclass = SCglobal;
ma->Sfl = 0;
ma->Sflags |= rtlsym[rt]->Sflags & SFLexit;
writefunc(ma);
}
}
obj_termfile();
}
/* ================================================================== */
void FuncDeclaration::toObjFile(int multiobj)
{
FuncDeclaration *func = this;
ClassDeclaration *cd = func->parent->isClassDeclaration();
int reverse;
int has_arguments;
//printf("FuncDeclaration::toObjFile(%p, %s.%s)\n", func, parent->toChars(), func->toChars());
//if (type) printf("type = %s\n", func->type->toChars());
#if 0
//printf("line = %d\n",func->getWhere() / LINEINC);
EEcontext *ee = env->getEEcontext();
if (ee->EEcompile == 2)
{
if (ee->EElinnum < (func->getWhere() / LINEINC) ||
ee->EElinnum > (func->endwhere / LINEINC)
)
return; // don't compile this function
ee->EEfunc = func->toSymbol();
}
#endif
if (semanticRun >= PASSobj) // if toObjFile() already run
return;
// If errors occurred compiling it, such as bugzilla 6118
if (type && type->ty == Tfunction && ((TypeFunction *)type)->next->ty == Terror)
return;
if (!func->fbody)
{
return;
}
if (func->isUnitTestDeclaration() && !global.params.useUnitTests)
return;
if (multiobj && !isStaticDtorDeclaration() && !isStaticCtorDeclaration())
{ obj_append(this);
return;
}
assert(semanticRun == PASSsemantic3done);
semanticRun = PASSobj;
if (global.params.verbose)
printf("function %s\n",func->toChars());
Symbol *s = func->toSymbol();
func_t *f = s->Sfunc;
#if TARGET_WINDOS
/* 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 (isVirtual() && (fensure || frequire))
f->Fflags3 |= Ffakeeh;
#endif
#if TARGET_OSX
s->Sclass = SCcomdat;
#else
s->Sclass = SCglobal;
#endif
for (Dsymbol *p = parent; p; p = p->parent)
{
if (p->isTemplateInstance())
{
s->Sclass = SCcomdat;
break;
}
}
/* Vector operations should be comdat's
*/
if (isArrayOp)
s->Sclass = SCcomdat;
if (isNested())
{
// if (!(config.flags3 & CFG3pic))
// s->Sclass = SCstatic;
f->Fflags3 |= Fnested;
}
else
{
const char *libname = (global.params.symdebug)
? global.params.debuglibname
: global.params.defaultlibname;
// Pull in RTL startup code
if (func->isMain())
{ objextdef("_main");
#if TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_OPENBSD || TARGET_SOLARIS
obj_ehsections(); // initialize exception handling sections
#endif
#if TARGET_WINDOS
objextdef("__acrtused_con");
#endif
obj_includelib(libname);
s->Sclass = SCglobal;
}
else if (strcmp(s->Sident, "main") == 0 && linkage == LINKc)
{
#if TARGET_WINDOS
objextdef("__acrtused_con"); // bring in C startup code
obj_includelib("snn.lib"); // bring in C runtime library
#endif
s->Sclass = SCglobal;
}
else if (func->isWinMain())
{
objextdef("__acrtused");
obj_includelib(libname);
s->Sclass = SCglobal;
}
// Pull in RTL startup code
else if (func->isDllMain())
{
objextdef("__acrtused_dll");
obj_includelib(libname);
s->Sclass = SCglobal;
}
}
cstate.CSpsymtab = &f->Flocsym;
// Find module m for this function
Module *m = NULL;
for (Dsymbol *p = parent; p; p = p->parent)
{
m = p->isModule();
if (m)
break;
}
IRState irs(m, func);
Dsymbols deferToObj; // write these to OBJ file later
irs.deferToObj = &deferToObj;
TypeFunction *tf;
enum RET retmethod;
symbol *shidden = NULL;
Symbol *sthis = NULL;
tym_t tyf;
tyf = tybasic(s->Stype->Tty);
//printf("linkage = %d, tyf = x%x\n", linkage, tyf);
reverse = tyrevfunc(s->Stype->Tty);
assert(func->type->ty == Tfunction);
tf = (TypeFunction *)(func->type);
has_arguments = (tf->linkage == LINKd) && (tf->varargs == 1);
retmethod = tf->retStyle();
if (retmethod == RETstack)
{
// If function returns a struct, put a pointer to that
// as the first argument
::type *thidden = tf->next->pointerTo()->toCtype();
char hiddenparam[5+4+1];
static int hiddenparami; // how many we've generated so far
sprintf(hiddenparam,"__HID%d",++hiddenparami);
shidden = symbol_name(hiddenparam,SCparameter,thidden);
shidden->Sflags |= SFLtrue | SFLfree;
#if DMDV1
if (func->nrvo_can && func->nrvo_var && func->nrvo_var->nestedref)
#else
if (func->nrvo_can && func->nrvo_var && func->nrvo_var->nestedrefs.dim)
#endif
type_setcv(&shidden->Stype, shidden->Stype->Tty | mTYvolatile);
irs.shidden = shidden;
this->shidden = shidden;
}
if (vthis)
{
assert(!vthis->csym);
sthis = vthis->toSymbol();
irs.sthis = sthis;
if (!(f->Fflags3 & Fnested))
f->Fflags3 |= Fmember;
}
Symbol **params;
unsigned pi;
// Estimate number of parameters, pi
pi = (v_arguments != NULL);
if (parameters)
pi += parameters->dim;
// Allow extra 2 for sthis and shidden
params = (Symbol **)alloca((pi + 2) * sizeof(Symbol *));
// Get the actual number of parameters, pi, and fill in the params[]
pi = 0;
if (v_arguments)
{
params[pi] = v_arguments->toSymbol();
pi += 1;
}
if (parameters)
{
for (size_t i = 0; i < parameters->dim; i++)
{ VarDeclaration *v = parameters->tdata()[i];
if (v->csym)
{
error("compiler error, parameter '%s', bugzilla 2962?", v->toChars());
assert(0);
}
params[pi + i] = v->toSymbol();
}
pi += parameters->dim;
}
if (reverse)
{ // Reverse params[] entries
for (size_t i = 0; i < pi/2; i++)
{
Symbol *sptmp = params[i];
params[i] = params[pi - 1 - i];
params[pi - 1 - i] = sptmp;
}
}
if (shidden)
{
#if 0
// shidden becomes last parameter
params[pi] = shidden;
#else
// shidden becomes first parameter
memmove(params + 1, params, pi * sizeof(params[0]));
params[0] = shidden;
#endif
pi++;
}
if (sthis)
{
#if 0
// sthis becomes last parameter
params[pi] = sthis;
#else
// sthis becomes first parameter
memmove(params + 1, params, pi * sizeof(params[0]));
params[0] = sthis;
#endif
pi++;
}
if ((global.params.isLinux || global.params.isOSX || global.params.isFreeBSD || global.params.isSolaris) &&
linkage != LINKd && shidden && sthis)
{
/* swap shidden and sthis
*/
Symbol *sp = params[0];
params[0] = params[1];
params[1] = sp;
}
for (size_t i = 0; i < pi; i++)
{ Symbol *sp = params[i];
sp->Sclass = SCparameter;
sp->Sflags &= ~SFLspill;
sp->Sfl = FLpara;
symbol_add(sp);
}
// Determine register assignments
if (pi)
{
if (global.params.is64bit)
{
// Order of assignment of pointer or integer parameters
static const unsigned char argregs[6] = { DI,SI,DX,CX,R8,R9 };
int r = 0;
int xmmcnt = XMM0;
for (size_t i = 0; i < pi; i++)
{ Symbol *sp = params[i];
tym_t ty = tybasic(sp->Stype->Tty);
// BUG: doesn't work for structs
if (r < sizeof(argregs)/sizeof(argregs[0]))
{
if (type_jparam(sp->Stype))
{
sp->Sclass = SCfastpar;
sp->Spreg = argregs[r];
sp->Sfl = FLauto;
++r;
}
}
if (xmmcnt <= XMM7)
{
if (tyxmmreg(ty))
{
sp->Sclass = SCfastpar;
sp->Spreg = xmmcnt;
sp->Sfl = FLauto;
++xmmcnt;
}
}
}
}
else
{
// First parameter goes in register
Symbol *sp = params[0];
if ((tyf == TYjfunc || tyf == TYmfunc) &&
type_jparam(sp->Stype))
{ sp->Sclass = SCfastpar;
sp->Spreg = (tyf == TYjfunc) ? AX : CX;
sp->Sfl = FLauto;
//printf("'%s' is SCfastpar\n",sp->Sident);
}
}
}
if (func->fbody)
{ block *b;
Blockx bx;
Statement *sbody;
localgot = NULL;
sbody = func->fbody;
memset(&bx,0,sizeof(bx));
bx.startblock = block_calloc();
bx.curblock = bx.startblock;
bx.funcsym = s;
bx.scope_index = -1;
bx.classdec = cd;
bx.member = func;
bx.module = getModule();
irs.blx = &bx;
#if DMDV2
buildClosure(&irs);
#endif
#if 0
if (func->isSynchronized())
{
if (cd)
{ elem *esync;
if (func->isStatic())
{ // monitor is in ClassInfo
esync = el_ptr(cd->toSymbol());
}
else
{ // 'this' is the monitor
esync = el_var(sthis);
}
if (func->isStatic() || sbody->usesEH() ||
!(config.flags2 & CFG2seh))
{ // BUG: what if frequire or fensure uses EH?
sbody = new SynchronizedStatement(func->loc, esync, sbody);
}
else
{
#if TARGET_WINDOS
if (config.flags2 & CFG2seh)
{
/* The "jmonitor" uses an optimized exception handling frame
* which is a little shorter than the more general EH frame.
* It isn't strictly necessary.
*/
s->Sfunc->Fflags3 |= Fjmonitor;
}
#endif
el_free(esync);
}
}
else
{
error("synchronized function %s must be a member of a class", func->toChars());
}
}
#elif TARGET_WINDOS
if (func->isSynchronized() && cd && config.flags2 & CFG2seh &&
!func->isStatic() && !sbody->usesEH())
{
/* The "jmonitor" hack uses an optimized exception handling frame
* which is a little shorter than the more general EH frame.
*/
s->Sfunc->Fflags3 |= Fjmonitor;
}
#endif
sbody->toIR(&irs);
bx.curblock->BC = BCret;
f->Fstartblock = bx.startblock;
// einit = el_combine(einit,bx.init);
if (isCtorDeclaration())
{
assert(sthis);
for (b = f->Fstartblock; b; b = b->Bnext)
{
if (b->BC == BCret)
{
b->BC = BCretexp;
b->Belem = el_combine(b->Belem, el_var(sthis));
}
}
}
}
// If static constructor
#if DMDV2
if (isSharedStaticCtorDeclaration()) // must come first because it derives from StaticCtorDeclaration
{
ssharedctors.push(s);
}
else
#endif
if (isStaticCtorDeclaration())
{
sctors.push(s);
}
// If static destructor
#if DMDV2
if (isSharedStaticDtorDeclaration()) // must come first because it derives from StaticDtorDeclaration
{
SharedStaticDtorDeclaration *f = isSharedStaticDtorDeclaration();
assert(f);
if (f->vgate)
{ /* Increment destructor's vgate at construction time
*/
esharedctorgates.push(f);
}
sshareddtors.shift(s);
}
else
#endif
if (isStaticDtorDeclaration())
{
StaticDtorDeclaration *f = isStaticDtorDeclaration();
assert(f);
if (f->vgate)
{ /* Increment destructor's vgate at construction time
*/
ectorgates.push(f);
}
sdtors.shift(s);
}
// If unit test
if (isUnitTestDeclaration())
{
stests.push(s);
}
if (global.errors)
return;
writefunc(s);
if (isExport())
obj_export(s, Poffset);
for (size_t i = 0; i < irs.deferToObj->dim; i++)
{
Dsymbol *s = irs.deferToObj->tdata()[i];
s->toObjFile(0);
}
#if TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_OPENBSD || TARGET_SOLARIS
// A hack to get a pointer to this function put in the .dtors segment
if (ident && memcmp(ident->toChars(), "_STD", 4) == 0)
obj_staticdtor(s);
#endif
#if DMDV2
if (irs.startaddress)
{
printf("Setting start address\n");
obj_startaddress(irs.startaddress);
}
#endif
}
/* ================================================================== */
/*****************************
* Return back end type corresponding to D front end type.
*/
unsigned Type::totym()
{ unsigned t;
switch (ty)
{