Mercurial > projects > ddmd
annotate dmd/codegen/Util.d @ 73:ef02e2e203c2
Updating to dmd2.033
author | korDen |
---|---|
date | Sat, 28 Aug 2010 19:42:41 +0400 |
parents | f708f0452e81 |
children | 7e0d548de9e6 |
rev | line source |
---|---|
0 | 1 module dmd.codegen.Util; |
2 | |
3 import dmd.Loc; | |
73 | 4 import dmd.Id; |
0 | 5 import dmd.IRState; |
6 import dmd.Type; | |
7 import dmd.Array; | |
8 import dmd.Dsymbol; | |
9 import dmd.FuncDeclaration; | |
10 import dmd.Identifier; | |
11 import dmd.RET; | |
12 import dmd.TY; | |
13 import dmd.LINK; | |
14 import dmd.Expression; | |
15 import dmd.Argument; | |
16 import dmd.STC; | |
17 import dmd.Global; | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
18 import dmd.Module; |
0 | 19 import dmd.InterfaceDeclaration; |
20 import dmd.AggregateDeclaration; | |
21 import dmd.AttribDeclaration; | |
22 import dmd.TupleDeclaration; | |
23 import dmd.StructDeclaration; | |
24 import dmd.VarDeclaration; | |
25 import dmd.ClassDeclaration; | |
26 import dmd.TemplateMixin; | |
27 import dmd.TypedefDeclaration; | |
28 import dmd.ExpInitializer; | |
29 import dmd.TypeFunction; | |
30 import dmd.TypeStruct; | |
31 import dmd.TypeSArray; | |
32 import dmd.TOK; | |
33 import dmd.Util; | |
34 import dmd.LabelStatement; | |
35 import dmd.DsymbolExp; | |
36 import dmd.LabelDsymbol; | |
37 import dmd.backend.elem; | |
38 import dmd.backend.TYPE; | |
39 import dmd.backend.Util; | |
40 import dmd.backend.Classsym; | |
41 import dmd.backend.SC; | |
42 import dmd.backend.FL; | |
43 import dmd.backend.SFL; | |
44 import dmd.backend.STR; | |
45 import dmd.backend.TYM; | |
46 import dmd.backend.TF; | |
47 import dmd.backend.OPER; | |
48 import dmd.backend.mTYman; | |
49 import dmd.backend.TYFL; | |
50 import dmd.backend.mTY; | |
51 import dmd.backend.Symbol; | |
52 import dmd.backend.Blockx; | |
53 import dmd.backend.RTLSYM; | |
54 import dmd.backend.block; | |
55 import dmd.backend.LIST; | |
56 | |
57 import std.string; | |
58 import core.stdc.string; | |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
59 import core.stdc.stdlib; |
0 | 60 |
4 | 61 import core.memory; |
2 | 62 |
0 | 63 /************************************ |
64 * Call a function. | |
65 */ | |
66 | |
67 elem* callfunc(Loc loc, | |
68 IRState* irs, | |
69 int directcall, // 1: don't do virtual call | |
70 Type tret, // return type | |
71 elem *ec, // evaluates to function address | |
72 Type ectype, // original type of ec | |
73 FuncDeclaration fd, // if !=null, this is the function being called | |
74 Type t, // TypeDelegate or TypeFunction for this function | |
75 elem* ehidden, // if !=null, this is the 'hidden' argument | |
76 Array arguments) | |
77 { | |
78 elem* ep; | |
79 elem* e; | |
80 elem* ethis = null; | |
81 elem* eside = null; | |
82 int i; | |
83 tym_t ty; | |
84 tym_t tyret; | |
85 RET retmethod; | |
86 int reverse; | |
87 TypeFunction tf; | |
88 OPER op; | |
89 | |
90 static if (false) { | |
91 printf("callfunc(directcall = %d, tret = '%s', ec = %p, fd = %p)\n", | |
92 directcall, tret.toChars(), ec, fd); | |
93 printf("ec: "); elem_print(ec); | |
94 if (fd) | |
95 printf("fd = '%s'\n", fd.toChars()); | |
96 } | |
97 | |
98 t = t.toBasetype(); | |
99 if (t.ty == TY.Tdelegate) | |
100 { | |
101 // A delegate consists of: | |
102 // { Object *this; Function *funcptr; } | |
103 assert(!fd); | |
104 assert(t.nextOf().ty == TY.Tfunction); | |
105 tf = cast(TypeFunction)t.nextOf(); | |
106 ethis = ec; | |
107 ec = el_same(ðis); | |
108 ethis = el_una(OPER.OP64_32, TYM.TYnptr, ethis); // get this | |
109 ec = array_toPtr(t, ec); // get funcptr | |
110 ec = el_una(OPER.OPind, tf.totym(), ec); | |
111 } | |
112 else | |
113 { | |
114 assert(t.ty == TY.Tfunction); | |
115 tf = cast(TypeFunction)t; | |
116 } | |
117 | |
118 retmethod = tf.retStyle(); | |
119 ty = ec.Ety; | |
120 if (fd) | |
121 ty = fd.toSymbol().Stype.Tty; | |
122 reverse = tyrevfunc(ty); | |
123 ep = null; | |
124 if (arguments) | |
125 { | |
126 // j=1 if _arguments[] is first argument | |
127 int j = (tf.linkage == LINK.LINKd && tf.varargs == 1); | |
128 | |
129 for (i = 0; i < arguments.dim ; i++) | |
130 { | |
131 Expression arg; | |
132 elem* ea; | |
133 | |
134 arg = cast(Expression)arguments.data[i]; | |
135 //printf("\targ[%d]: %s\n", i, arg.toChars()); | |
136 | |
137 size_t nparams = Argument.dim(tf.parameters); | |
138 if (i - j < nparams && i >= j) | |
139 { | |
140 Argument p = Argument.getNth(tf.parameters, i - j); | |
141 | |
142 if (p.storageClass & (STC.STCout | STC.STCref)) | |
143 { | |
144 // Convert argument to a pointer, | |
145 // use AddrExp.toElem() | |
146 Expression ae = arg.addressOf(null); | |
147 ea = ae.toElem(irs); | |
148 goto L1; | |
149 } | |
150 } | |
151 ea = arg.toElem(irs); | |
152 L1: | |
153 if (tybasic(ea.Ety) == TYM.TYstruct) | |
154 { | |
155 ea = el_una(OPER.OPstrpar, TYM.TYstruct, ea); | |
156 ea.Enumbytes = ea.E1.Enumbytes; | |
157 assert(ea.Enumbytes); | |
158 } | |
159 if (reverse) | |
160 ep = el_param(ep,ea); | |
161 else | |
162 ep = el_param(ea,ep); | |
163 } | |
164 } | |
165 | |
166 if (retmethod == RET.RETstack) | |
167 { | |
168 if (!ehidden) | |
169 { | |
170 // Don't have one, so create one | |
171 type* tt; | |
172 | |
173 if (tf.next.toBasetype().ty == TY.Tstruct) | |
174 tt = tf.next.toCtype(); | |
175 else | |
176 tt = type_fake(tf.next.totym()); | |
177 | |
178 Symbol* stmp = symbol_genauto(tt); | |
179 ehidden = el_ptr(stmp); | |
180 } | |
181 if ((global.params.isLinux || global.params.isOSX || global.params.isFreeBSD || global.params.isSolaris) && tf.linkage != LINK.LINKd) { | |
182 ; // ehidden goes last on Linux/OSX C++ | |
183 } else { | |
184 if (ep) | |
185 { | |
186 static if (false) { // BUG: implement | |
187 if (reverse && type_mangle(tfunc) == mTYman.mTYman_cpp) { | |
188 ep = el_param(ehidden,ep); | |
189 } else { | |
190 ep = el_param(ep,ehidden); | |
191 } | |
192 } else { | |
193 ep = el_param(ep,ehidden); | |
194 } | |
195 } | |
196 else | |
197 ep = ehidden; | |
198 ehidden = null; | |
199 } | |
200 } | |
201 | |
202 if (fd && fd.isMember2()) | |
203 { | |
204 InterfaceDeclaration intd; | |
205 Symbol* sfunc; | |
206 AggregateDeclaration ad; | |
207 | |
208 ad = fd.isThis(); | |
209 if (ad) | |
210 { | |
211 ethis = ec; | |
212 if (ad.isStructDeclaration() && tybasic(ec.Ety) != TYM.TYnptr) | |
213 { | |
214 ethis = addressElem(ec, ectype); | |
215 } | |
216 } | |
217 else | |
218 { | |
219 // Evaluate ec for side effects | |
220 eside = ec; | |
221 } | |
222 sfunc = fd.toSymbol(); | |
223 | |
224 if (!fd.isVirtual() || | |
225 directcall || // BUG: fix | |
226 fd.isFinal()) | |
227 { | |
228 // make static call | |
229 ec = el_var(sfunc); | |
230 } | |
231 else | |
232 { | |
233 // make virtual call | |
234 elem* ev; | |
235 uint vindex; | |
236 | |
237 assert(ethis); | |
238 ev = el_same(ðis); | |
239 ev = el_una(OPER.OPind, TYM.TYnptr, ev); | |
240 vindex = fd.vtblIndex; | |
241 | |
242 // Build *(ev + vindex * 4) | |
243 ec = el_bin(OPER.OPadd, TYM.TYnptr, ev, el_long(TYM.TYint, vindex * 4)); | |
244 ec = el_una(OPER.OPind, TYM.TYnptr, ec); | |
245 ec = el_una(OPER.OPind, tybasic(sfunc.Stype.Tty), ec); | |
246 } | |
247 } | |
248 else if (fd && fd.isNested()) | |
249 { | |
250 assert(!ethis); | |
251 ethis = getEthis(Loc(0), irs, fd); | |
252 } | |
253 | |
254 ep = el_param(ep, ethis); | |
255 if (ehidden) | |
256 ep = el_param(ep, ehidden); // if ehidden goes last | |
257 | |
258 tyret = tret.totym(); | |
259 | |
260 // Look for intrinsic functions | |
261 if (ec.Eoper == OPER.OPvar && (op = intrinsic_oper(ec.EV.sp.Vsym.Sident.ptr)) != OPER.OPMAX) | |
262 { | |
263 el_free(ec); | |
264 if (OTbinary(op)) | |
265 { | |
266 ep.Eoper = op; | |
267 ep.Ety = tyret; | |
268 e = ep; | |
269 if (op == OPER.OPscale) | |
270 { | |
271 elem *et = e.E1; | |
272 e.E1() = el_una(OPER.OPd_ld, TYM.TYldouble, e.E1); | |
273 e.E1() = el_una(OPER.OPs32_d, TYM.TYdouble, e.E2); | |
274 e.E2() = et; | |
275 } | |
276 else if (op == OPER.OPyl2x || op == OPER.OPyl2xp1) | |
277 { | |
278 elem *et = e.E1; | |
279 e.E1() = e.E2; | |
280 e.E2() = et; | |
281 } | |
282 } | |
283 else | |
284 e = el_una(op,tyret,ep); | |
285 } | |
286 else if (ep) | |
287 e = el_bin(tf.ispure ? OPER.OPcallns : OPER.OPcall, tyret, ec, ep); | |
288 else | |
289 e = el_una(tf.ispure ? OPER.OPucallns : OPER.OPucall, tyret, ec); | |
290 | |
291 if (retmethod == RET.RETstack) | |
292 { | |
293 e.Ety = TYM.TYnptr; | |
294 e = el_una(OPER.OPind, tyret, e); | |
295 } | |
296 | |
297 version (DMDV2) { | |
298 if (tf.isref) | |
299 { | |
300 e.Ety = TYM.TYnptr; | |
301 e = el_una(OPER.OPind, tyret, e); | |
302 } | |
303 } | |
304 | |
305 if (tybasic(tyret) == TYM.TYstruct) | |
306 { | |
307 e.Enumbytes = cast(uint)tret.size(); | |
308 } | |
309 | |
310 e = el_combine(eside, e); | |
311 return e; | |
312 } | |
313 | |
314 /************************************** | |
315 * Fake a struct symbol. | |
316 */ | |
317 | |
318 Classsym* fake_classsym(Identifier id) | |
319 { | |
320 TYPE* t; | |
321 Classsym* scc; | |
322 | |
323 scc = cast(Classsym*)symbol_calloc(toStringz(id.toChars())); | |
324 scc.Sclass = SC.SCstruct; | |
325 scc.Sstruct = struct_calloc(); | |
326 scc.Sstruct.Sstructalign = 8; | |
327 //scc.Sstruct.ptrtype = TYM.TYnptr; | |
328 scc.Sstruct.Sflags = STR.STRglobal; | |
329 | |
330 t = type_alloc(TYM.TYstruct); | |
331 t.Tflags |= TF.TFsizeunknown | TF.TFforward; | |
332 t.Ttag = scc; // structure tag name | |
333 assert(t.Tmangle == 0); | |
334 t.Tmangle = mTYman.mTYman_d; | |
335 t.Tcount++; | |
336 scc.Stype = t; | |
337 slist_add(scc); | |
338 return scc; | |
339 } | |
340 | |
341 /****************************************** | |
342 * Return elem that evaluates to the static frame pointer for function fd. | |
343 * If fd is a member function, the returned expression will compute the value | |
344 * of fd's 'this' variable. | |
345 * This routine is critical for implementing nested functions. | |
346 */ | |
347 | |
348 elem* getEthis(Loc loc, IRState* irs, Dsymbol fd) | |
349 { | |
350 elem* ethis; | |
351 FuncDeclaration thisfd = irs.getFunc(); | |
352 Dsymbol fdparent = fd.toParent2(); | |
353 | |
354 //printf("getEthis(thisfd = '%s', fd = '%s', fdparent = '%s')\n", thisfd.toChars(), fd.toChars(), fdparent.toChars()); | |
73 | 355 if (fdparent == thisfd || |
356 /* These two are compiler generated functions for the in and out contracts, | |
357 * and are called from an overriding function, not just the one they're | |
358 * nested inside, so this hack is so they'll pass | |
359 */ | |
360 fd.ident == Id.require || fd.ident == Id.ensure) | |
0 | 361 { |
362 /* Going down one nesting level, i.e. we're calling | |
363 * a nested function from its enclosing function. | |
364 */ | |
365 ///version (DMDV2) { | |
366 if (irs.sclosure) | |
367 ethis = el_var(irs.sclosure); | |
368 else | |
369 ///} | |
370 if (irs.sthis) | |
371 { | |
372 // We have a 'this' pointer for the current function | |
373 ethis = el_var(irs.sthis); | |
374 | |
375 /* If no variables in the current function's frame are | |
376 * referenced by nested functions, then we can 'skip' | |
377 * adding this frame into the linked list of stack | |
378 * frames. | |
379 */ | |
380 version (DMDV2) { | |
381 bool cond = (thisfd.closureVars.dim != 0); | |
382 } else { | |
383 bool cond = thisfd.nestedFrameRef; | |
384 } | |
385 if (cond) | |
386 { | |
387 /* Local variables are referenced, can't skip. | |
388 * Address of 'this' gives the 'this' for the nested | |
389 * function | |
390 */ | |
391 ethis = el_una(OPER.OPaddr, TYM.TYnptr, ethis); | |
392 } | |
393 } | |
394 else | |
395 { | |
396 /* No 'this' pointer for current function, | |
397 * use null if no references to the current function's frame | |
398 */ | |
399 ethis = el_long(TYM.TYnptr, 0); | |
400 version (DMDV2) { | |
401 bool cond = (thisfd.closureVars.dim != 0); | |
402 } else { | |
403 bool cond = thisfd.nestedFrameRef; | |
404 } | |
405 if (cond) | |
406 { | |
407 /* OPframeptr is an operator that gets the frame pointer | |
408 * for the current function, i.e. for the x86 it gets | |
409 * the value of EBP | |
410 */ | |
411 ethis.Eoper = OPER.OPframeptr; | |
412 } | |
413 } | |
73 | 414 |
415 //if (fdparent != thisfd) ethis = el_bin(OPadd, TYnptr, ethis, el_long(TYint, 0x18)); | |
0 | 416 } |
417 else | |
418 { | |
419 if (!irs.sthis) // if no frame pointer for this function | |
420 { | |
421 fd.error(loc, "is a nested function and cannot be accessed from %s", irs.getFunc().toChars()); | |
422 ethis = el_long(TYM.TYnptr, 0); // error recovery | |
423 } | |
424 else | |
425 { | |
426 ethis = el_var(irs.sthis); | |
427 Dsymbol s = thisfd; | |
428 while (fd != s) | |
429 { | |
430 /* Go up a nesting level, i.e. we need to find the 'this' | |
431 * of an enclosing function. | |
432 * Our 'enclosing function' may also be an inner class. | |
433 */ | |
434 | |
435 //printf("\ts = '%s'\n", s.toChars()); | |
436 thisfd = s.isFuncDeclaration(); | |
437 if (thisfd) | |
438 { | |
439 /* Enclosing function is a function. | |
440 */ | |
441 if (fdparent == s.toParent2()) | |
442 break; | |
443 | |
444 if (thisfd.isNested()) | |
445 { | |
446 FuncDeclaration p = s.toParent2().isFuncDeclaration(); | |
447 version (DMDV2) { | |
448 bool cond = !p || p.closureVars.dim; | |
449 } else { | |
450 bool cond = !p || p.nestedFrameRef; | |
451 } | |
452 if (cond) { | |
453 ethis = el_una(OPER.OPind, TYM.TYnptr, ethis); | |
454 } | |
455 } | |
456 else if (thisfd.vthis) | |
457 { | |
458 ; | |
459 } | |
460 else | |
461 { | |
462 // Error should have been caught by front end | |
463 assert(0); | |
464 } | |
465 } | |
466 else | |
467 { | |
468 /* Enclosed by an aggregate. That means the current | |
469 * function must be a member function of that aggregate. | |
470 */ | |
471 ClassDeclaration cd; | |
472 StructDeclaration sd; | |
473 AggregateDeclaration ad = s.isAggregateDeclaration(); | |
474 | |
475 if (!ad) | |
476 goto Lnoframe; | |
477 | |
478 cd = s.isClassDeclaration(); | |
479 | |
480 if (cd && fd.isClassDeclaration() && fd.isClassDeclaration().isBaseOf(cd, null)) | |
481 break; | |
482 | |
483 sd = s.isStructDeclaration(); | |
484 | |
485 if (fd == sd) | |
486 break; | |
487 | |
488 if (!ad.isNested() || !ad.vthis) | |
489 { | |
490 Lnoframe: | |
491 irs.getFunc().error(loc, "cannot get frame pointer to %s", fd.toChars()); | |
492 return el_long(TYM.TYnptr, 0); // error recovery | |
493 } | |
494 | |
495 ethis = el_bin(OPER.OPadd, TYM.TYnptr, ethis, el_long(TYM.TYint, ad.vthis.offset)); | |
496 ethis = el_una(OPER.OPind, TYM.TYnptr, ethis); | |
497 | |
498 if (fdparent == s.toParent2()) | |
499 break; | |
500 | |
501 if (auto fdd = s.toParent2().isFuncDeclaration()) | |
502 { | |
503 /* Remember that frames for functions that have no | |
504 * nested references are skipped in the linked list | |
505 * of frames. | |
506 */ | |
507 version (DMDV2) { | |
508 bool cond = (fdd.closureVars.dim != 0); | |
509 } else { | |
510 bool cond = fdd.nestedFrameRef; | |
511 } | |
512 if (cond) { | |
513 ethis = el_una(OPER.OPind, TYM.TYnptr, ethis); | |
514 } | |
515 break; | |
516 } | |
517 } | |
518 s = s.toParent2(); | |
519 assert(s); | |
520 } | |
521 } | |
522 } | |
523 | |
524 static if (false) { | |
525 printf("ethis:\n"); | |
526 elem_print(ethis); | |
527 printf("\n"); | |
528 } | |
529 | |
530 return ethis; | |
531 } | |
532 | |
533 /***************************************** | |
534 * Convert array to a pointer to the data. | |
535 */ | |
536 | |
537 elem* array_toPtr(Type t, elem* e) | |
538 { | |
539 //printf("array_toPtr()\n"); | |
540 //elem_print(e); | |
541 t = t.toBasetype(); | |
542 switch (t.ty) | |
543 { | |
544 case TY.Tpointer: | |
545 break; | |
546 | |
547 case TY.Tarray: | |
548 case TY.Tdelegate: | |
549 if (e.Eoper == OPER.OPcomma) | |
550 { | |
551 e.Ety = TYM.TYnptr; | |
552 e.E2() = array_toPtr(t, e.E2); | |
553 } | |
554 else if (e.Eoper == OPER.OPpair) | |
555 { | |
556 e.Eoper = OPER.OPcomma; | |
557 e.Ety = TYM.TYnptr; | |
558 } | |
559 else | |
560 { | |
561 static if (true) { | |
562 e = el_una(OPER.OPmsw, TYM.TYnptr, e); | |
563 } else { | |
564 e = el_una(OPER.OPaddr, TYM.TYnptr, e); | |
565 e = el_bin(OPER.OPadd, TYM.TYnptr, e, el_long(TYM.TYint, 4)); | |
566 e = el_una(OPER.OPind, TYM.TYnptr, e); | |
567 } | |
568 } | |
569 break; | |
570 | |
571 case TY.Tsarray: | |
572 e = el_una(OPER.OPaddr, TYM.TYnptr, e); | |
573 break; | |
574 | |
575 default: | |
576 ///t.print(); | |
577 assert(0); | |
578 } | |
579 return e; | |
580 } | |
581 | |
582 /******************************************* | |
583 * Take address of an elem. | |
584 */ | |
585 | |
586 elem* addressElem(elem* e, Type t) | |
587 { | |
588 elem** pe; | |
589 | |
590 //printf("addressElem()\n"); | |
591 | |
592 for (pe = &e; (*pe).Eoper == OPER.OPcomma; pe = &(*pe).E2()) { | |
593 ; | |
594 } | |
595 | |
596 if ((*pe).Eoper != OPER.OPvar && (*pe).Eoper != OPER.OPind) | |
597 { | |
598 Symbol* stmp; | |
599 elem* eeq; | |
600 elem* ee = *pe; | |
601 type* tx; | |
602 | |
603 // Convert to ((tmp=ee),tmp) | |
604 TY ty; | |
605 if (t && ((ty = t.toBasetype().ty) == TY.Tstruct || ty == TY.Tsarray)) | |
606 tx = t.toCtype(); | |
607 else | |
608 tx = type_fake(ee.Ety); | |
609 stmp = symbol_genauto(tx); | |
610 eeq = el_bin(OPER.OPeq,ee.Ety,el_var(stmp),ee); | |
611 | |
612 if (tybasic(ee.Ety) == TYM.TYstruct) | |
613 { | |
614 eeq.Eoper = OPER.OPstreq; | |
615 eeq.Enumbytes = ee.Enumbytes; | |
616 } | |
617 else if (tybasic(ee.Ety) == TYM.TYarray) | |
618 { | |
619 eeq.Eoper = OPER.OPstreq; | |
620 eeq.Ety = TYM.TYstruct; | |
621 eeq.Ejty = cast(ubyte)eeq.Ety; | |
622 eeq.Enumbytes = cast(uint)t.size(); | |
623 } | |
624 *pe = el_bin(OPER.OPcomma, ee.Ety, eeq, el_var(stmp)); | |
625 } | |
626 | |
627 e = el_una(OPER.OPaddr, TYM.TYnptr, e); | |
628 return e; | |
629 } | |
630 | |
631 /******************************************* | |
632 * Convert intrinsic function to operator. | |
633 * Returns that operator, -1 if not an intrinsic function. | |
634 */ | |
635 | |
636 extern (C++) extern int intrinsic_op(char* name); | |
637 | |
638 OPER intrinsic_oper(const(char)* name) | |
639 { | |
640 int result = intrinsic_op(cast(char*)name); | |
641 if (result == -1) return OPER.OPMAX; | |
642 return cast(OPER)result; | |
643 } | |
644 | |
645 /************************************** | |
646 */ | |
647 | |
648 elem* Dsymbol_toElem(Dsymbol s, IRState *irs) | |
649 { | |
650 elem *e = null; | |
651 Symbol* sp; | |
652 AttribDeclaration ad; | |
653 VarDeclaration vd; | |
654 ClassDeclaration cd; | |
655 StructDeclaration sd; | |
656 FuncDeclaration fd; | |
657 TemplateMixin tm; | |
658 TupleDeclaration td; | |
659 TypedefDeclaration tyd; | |
660 | |
661 //printf("Dsymbol_toElem() %s\n", s.toChars()); | |
662 ad = s.isAttribDeclaration(); | |
663 if (ad) | |
664 { | |
665 Array decl = ad.include(null, null); | |
666 if (decl && decl.dim) | |
667 { | |
668 for (size_t i = 0; i < decl.dim; i++) | |
669 { | |
670 s = cast(Dsymbol)decl.data[i]; | |
671 e = el_combine(e, Dsymbol_toElem(s, irs)); | |
672 } | |
673 } | |
674 } | |
675 else if ((vd = s.isVarDeclaration()) !is null) | |
676 { | |
677 s = s.toAlias(); | |
678 if (s != vd) | |
679 return Dsymbol_toElem(s, irs); | |
680 if (vd.isStatic() || vd.storage_class & (STC.STCextern | STC.STCtls | STC.STCgshared)) | |
681 vd.toObjFile(0); | |
682 else | |
683 { | |
684 sp = s.toSymbol(); | |
685 symbol_add(sp); | |
686 //printf("\tadding symbol '%s'\n", sp.Sident); | |
687 if (vd.init) | |
688 { | |
689 ExpInitializer ie = vd.init.isExpInitializer(); | |
690 if (ie) { | |
691 e = ie.exp.toElem(irs); | |
692 } | |
693 } | |
694 } | |
695 } | |
696 else if ((cd = s.isClassDeclaration()) !is null) | |
697 { | |
698 irs.deferToObj.push(cast(void*)s); | |
699 } | |
700 else if ((sd = s.isStructDeclaration()) !is null) | |
701 { | |
702 irs.deferToObj.push(cast(void*)sd); | |
703 } | |
704 else if ((fd = s.isFuncDeclaration()) !is null) | |
705 { | |
706 //printf("function %s\n", fd.toChars()); | |
707 irs.deferToObj.push(cast(void*)fd); | |
708 } | |
709 else if ((tm = s.isTemplateMixin()) !is null) | |
710 { | |
711 //printf("%s\n", tm.toChars()); | |
712 if (tm.members) | |
713 { | |
714 for (size_t i = 0; i < tm.members.dim; i++) | |
715 { | |
716 Dsymbol sm = cast(Dsymbol)tm.members.data[i]; | |
717 e = el_combine(e, Dsymbol_toElem(sm, irs)); | |
718 } | |
719 } | |
720 } | |
721 else if ((td = s.isTupleDeclaration()) !is null) | |
722 { | |
723 for (size_t i = 0; i < td.objects.dim; i++) | |
724 { | |
725 Object o = cast(Object)td.objects.data[i]; | |
726 ///if (o.dyncast() == DYNCAST_EXPRESSION) | |
727 if (Expression eo = cast(Expression)o) | |
728 { | |
729 if (eo.op == TOK.TOKdsymbol) | |
730 { | |
731 DsymbolExp se = cast(DsymbolExp)eo; | |
732 e = el_combine(e, Dsymbol_toElem(se.s, irs)); | |
733 } | |
734 } | |
735 } | |
736 } | |
737 else if ((tyd = s.isTypedefDeclaration()) !is null) | |
738 { | |
739 irs.deferToObj.push(cast(void*)tyd); | |
740 } | |
741 | |
742 return e; | |
743 } | |
744 | |
745 /************************************** | |
746 * Given an expression e that is an array, | |
747 * determine and set the 'length' variable. | |
748 * Input: | |
749 * lengthVar Symbol of 'length' variable | |
750 * &e expression that is the array | |
751 * t1 Type of the array | |
752 * Output: | |
753 * e is rewritten to avoid side effects | |
754 * Returns: | |
755 * expression that initializes 'length' | |
756 */ | |
757 | |
758 elem* resolveLengthVar(VarDeclaration lengthVar, elem** pe, Type t1) | |
759 { | |
760 //printf("resolveLengthVar()\n"); | |
761 elem* einit = null; | |
762 | |
763 if (lengthVar && !(lengthVar.storage_class & STC.STCconst)) | |
764 { | |
765 elem* elength; | |
766 Symbol* slength; | |
767 | |
768 if (t1.ty == TY.Tsarray) | |
769 { | |
770 TypeSArray tsa = cast(TypeSArray)t1; | |
771 long length = tsa.dim.toInteger(); | |
772 | |
773 elength = el_long(TYM.TYuint, length); | |
774 goto L3; | |
775 } | |
776 else if (t1.ty == TY.Tarray) | |
777 { | |
778 elength = *pe; | |
779 *pe = el_same(&elength); | |
780 elength = el_una(OPER.OP64_32, TYM.TYuint, elength); | |
781 | |
782 L3: | |
783 slength = lengthVar.toSymbol(); | |
784 //symbol_add(slength); | |
785 | |
786 einit = el_bin(OPER.OPeq, TYM.TYuint, el_var(slength), elength); | |
787 } | |
788 } | |
789 return einit; | |
790 } | |
791 | |
792 /******************************************* | |
793 * Set an array pointed to by eptr to evalue: | |
794 * eptr[0..edim] = evalue; | |
795 * Input: | |
796 * eptr where to write the data to | |
797 * evalue value to write | |
798 * edim number of times to write evalue to eptr[] | |
799 * tb type of evalue | |
800 */ | |
801 | |
802 elem* setArray(elem* eptr, elem* edim, Type tb, elem* evalue, IRState* irs, int op) | |
803 { | |
804 int r; | |
805 elem* e; | |
806 int sz = cast(int)tb.size(); | |
807 | |
808 if (tb.ty == TY.Tfloat80 || tb.ty == TY.Timaginary80) | |
809 r = RTLSYM.RTLSYM_MEMSET80; | |
810 else if (tb.ty == TY.Tcomplex80) | |
811 r = RTLSYM.RTLSYM_MEMSET160; | |
812 else if (tb.ty == TY.Tcomplex64) | |
813 r = RTLSYM.RTLSYM_MEMSET128; | |
814 else | |
815 { | |
816 switch (sz) | |
817 { | |
818 case 1: r = RTLSYM.RTLSYM_MEMSET8; break; | |
819 case 2: r = RTLSYM.RTLSYM_MEMSET16; break; | |
820 case 4: r = RTLSYM.RTLSYM_MEMSET32; break; | |
821 case 8: r = RTLSYM.RTLSYM_MEMSET64; break; | |
822 default: r = RTLSYM.RTLSYM_MEMSETN; break; | |
823 } | |
824 | |
825 /* Determine if we need to do postblit | |
826 */ | |
827 if (op != TOK.TOKblit) | |
828 { | |
829 StructDeclaration sd = needsPostblit(tb); | |
830 if (sd) | |
831 { | |
832 /* Need to do postblit. | |
833 * void *_d_arraysetassign(void *p, void *value, int dim, TypeInfo ti); | |
834 */ | |
835 r = (op == TOK.TOKconstruct) ? RTLSYM.RTLSYM_ARRAYSETCTOR : RTLSYM.RTLSYM_ARRAYSETASSIGN; | |
836 evalue = el_una(OPER.OPaddr, TYM.TYnptr, evalue); | |
837 Expression ti = tb.getTypeInfo(null); | |
838 elem* eti = ti.toElem(irs); | |
839 e = el_params(eti, edim, evalue, eptr, null); | |
840 e = el_bin(OPER.OPcall, TYM.TYnptr, el_var(rtlsym[r]), e); | |
841 return e; | |
842 } | |
843 } | |
844 | |
845 if (r == RTLSYM.RTLSYM_MEMSETN) | |
846 { | |
847 // void *_memsetn(void *p, void *value, int dim, int sizelem) | |
848 evalue = el_una(OPER.OPaddr, TYM.TYnptr, evalue); | |
849 elem *esz = el_long(TYM.TYint, sz); | |
850 e = el_params(esz, edim, evalue, eptr, null); | |
851 e = el_bin(OPER.OPcall, TYM.TYnptr, el_var(rtlsym[r]), e); | |
852 return e; | |
853 } | |
854 } | |
855 if (sz > 1 && sz <= 8 && evalue.Eoper == OPER.OPconst && el_allbits(evalue, 0)) | |
856 { | |
857 r = RTLSYM.RTLSYM_MEMSET8; | |
858 edim = el_bin(OPER.OPmul, TYM.TYuint, edim, el_long(TYM.TYuint, sz)); | |
859 } | |
860 | |
861 if (tybasic(evalue.Ety) == TYM.TYstruct) | |
862 { | |
863 evalue = el_una(OPER.OPstrpar, TYM.TYstruct, evalue); | |
864 evalue.Enumbytes = evalue.E1.Enumbytes; | |
865 assert(evalue.Enumbytes); | |
866 } | |
867 | |
868 // Be careful about parameter side effect ordering | |
869 if (r == RTLSYM.RTLSYM_MEMSET8) | |
870 { | |
871 e = el_param(edim, evalue); | |
872 e = el_bin(OPER.OPmemset, TYM.TYnptr, eptr, e); | |
873 } | |
874 else | |
875 { | |
876 e = el_params(edim, evalue, eptr, null); | |
877 e = el_bin(OPER.OPcall, TYM.TYnptr, el_var(rtlsym[r]), e); | |
878 } | |
879 return e; | |
880 } | |
881 | |
882 /************************* | |
883 * Initialize the hidden aggregate member, vthis, with | |
884 * the context pointer. | |
885 * Returns: | |
886 * *(ey + ad.vthis.offset) = this; | |
887 */ | |
888 version (DMDV2) { | |
889 elem* setEthis(Loc loc, IRState* irs, elem* ey, AggregateDeclaration ad) | |
890 { | |
891 elem* ethis; | |
892 FuncDeclaration thisfd = irs.getFunc(); | |
893 int offset = 0; | |
894 Dsymbol cdp = ad.toParent2(); // class/func we're nested in | |
895 | |
896 //printf("setEthis(ad = %s, cdp = %s, thisfd = %s)\n", ad.toChars(), cdp.toChars(), thisfd.toChars()); | |
897 | |
898 if (cdp is thisfd) | |
899 { | |
900 /* Class we're new'ing is a local class in this function: | |
901 * void thisfd() { class ad { } } | |
902 */ | |
903 if (irs.sclosure) | |
904 ethis = el_var(irs.sclosure); | |
905 else if (irs.sthis) | |
906 { | |
907 /// version (DMDV2) { | |
908 if (thisfd.closureVars.dim) | |
909 /// } else { | |
910 /// if (thisfd.nestedFrameRef) | |
911 /// } | |
912 { | |
913 ethis = el_ptr(irs.sthis); | |
914 } | |
915 else | |
916 ethis = el_var(irs.sthis); | |
917 } | |
918 else | |
919 { | |
920 ethis = el_long(TYM.TYnptr, 0); | |
921 /// version (DMDV2) { | |
922 if (thisfd.closureVars.dim) | |
923 /// } else { | |
924 /// if (thisfd.nestedFrameRef) | |
925 /// } | |
926 { | |
927 ethis.Eoper = OPER.OPframeptr; | |
928 } | |
929 } | |
930 } | |
931 else if (thisfd.vthis && ( | |
932 cdp == thisfd.toParent2() || ( | |
933 cdp.isClassDeclaration() && cdp.isClassDeclaration().isBaseOf(thisfd.toParent2().isClassDeclaration(), &offset) | |
934 ) | |
935 ) | |
936 ) | |
937 { | |
938 /* Class we're new'ing is at the same level as thisfd | |
939 */ | |
940 assert(offset == 0); // BUG: should handle this case | |
941 ethis = el_var(irs.sthis); | |
942 } | |
943 else | |
944 { | |
945 ethis = getEthis(loc, irs, ad.toParent2()); | |
946 ethis = el_una(OPER.OPaddr, TYM.TYnptr, ethis); | |
947 } | |
948 | |
949 ey = el_bin(OPER.OPadd, TYM.TYnptr, ey, el_long(TYM.TYint, ad.vthis.offset)); | |
950 ey = el_una(OPER.OPind, TYM.TYnptr, ey); | |
951 ey = el_bin(OPER.OPeq, TYM.TYnptr, ey, ethis); | |
952 | |
953 return ey; | |
954 } | |
955 } | |
956 | |
957 /******************************************** | |
958 * Determine if t is an array of structs that need a postblit. | |
959 */ | |
960 StructDeclaration needsPostblit(Type t) | |
961 { | |
962 t = t.toBasetype(); | |
963 | |
964 while (t.ty == TY.Tsarray) | |
965 t = t.nextOf().toBasetype(); | |
966 | |
967 if (t.ty == TY.Tstruct) | |
968 { | |
969 StructDeclaration sd = (cast(TypeStruct)t).sym; | |
970 if (sd.postblit) | |
971 return sd; | |
972 } | |
973 | |
974 return null; | |
975 } | |
976 | |
977 /***************************************** | |
978 * Convert array to a dynamic array. | |
979 */ | |
980 | |
981 elem* array_toDarray(Type t, elem* e) | |
982 { | |
983 uint dim; | |
984 elem* ef = null; | |
985 elem* ex; | |
986 | |
987 //printf("array_toDarray(t = %s)\n", t.toChars()); | |
988 //elem_print(e); | |
989 t = t.toBasetype(); | |
990 switch (t.ty) | |
991 { | |
992 case TY.Tarray: | |
993 break; | |
994 | |
995 case TY.Tsarray: | |
996 e = el_una(OPER.OPaddr, TYM.TYnptr, e); | |
997 dim = cast(uint)(cast(TypeSArray)t).dim.toInteger(); | |
998 e = el_pair(TYM.TYullong, el_long(TYM.TYint, dim), e); | |
999 break; | |
1000 | |
1001 default: | |
1002 L1: | |
1003 switch (e.Eoper) | |
1004 { | |
1005 case OPER.OPconst: | |
1006 { | |
1007 size_t len = tysize[tybasic(e.Ety)]; | |
1008 elem* es = el_calloc(); | |
1009 es.Eoper = OPER.OPstring; | |
1010 | |
1011 // Match MEM_PH_FREE for OPstring in ztc\el.c | |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
1012 es.EV.ss.Vstring = cast(char*)malloc(len); /// ! |
0 | 1013 memcpy(es.EV.ss.Vstring, &e.EV, len); |
1014 | |
1015 es.EV.ss.Vstrlen = len; | |
1016 es.Ety = TYM.TYnptr; | |
1017 e = es; | |
1018 break; | |
1019 } | |
1020 | |
1021 case OPER.OPvar: | |
1022 e = el_una(OPER.OPaddr, TYM.TYnptr, e); | |
1023 break; | |
1024 | |
1025 case OPER.OPcomma: | |
1026 ef = el_combine(ef, e.E1); | |
1027 ex = e; | |
1028 e = e.E2; | |
1029 ex.E1() = null; | |
1030 ex.E2() = null; | |
1031 el_free(ex); | |
1032 goto L1; | |
1033 | |
1034 case OPER.OPind: | |
1035 ex = e; | |
1036 e = e.E1; | |
1037 ex.E1() = null; | |
1038 ex.E2() = null; | |
1039 el_free(ex); | |
1040 break; | |
1041 | |
1042 default: | |
1043 { | |
1044 // Copy expression to a variable and take the | |
1045 // address of that variable. | |
1046 Symbol* stmp; | |
1047 tym_t ty = tybasic(e.Ety); | |
1048 | |
1049 if (ty == TYM.TYstruct) | |
1050 { | |
1051 if (e.Enumbytes == 4) | |
1052 ty = TYM.TYint; | |
1053 else if (e.Enumbytes == 8) | |
1054 ty = TYM.TYllong; | |
1055 } | |
1056 e.Ety = ty; | |
1057 stmp = symbol_genauto(type_fake(ty)); | |
1058 e = el_bin(OPER.OPeq, e.Ety, el_var(stmp), e); | |
1059 e = el_bin(OPER.OPcomma, TYM.TYnptr, e, el_una(OPER.OPaddr, TYM.TYnptr, el_var(stmp))); | |
1060 break; | |
1061 } | |
1062 } | |
1063 dim = 1; | |
1064 e = el_pair(TYM.TYullong, el_long(TYM.TYint, dim), e); | |
1065 break; | |
1066 } | |
1067 | |
1068 return el_combine(ef, e); | |
1069 } | |
1070 | |
1071 elem* sarray_toDarray(Loc loc, Type tfrom, Type tto, elem* e) | |
1072 { | |
1073 //printf("sarray_toDarray()\n"); | |
1074 //elem_print(e); | |
1075 | |
1076 elem* elen; | |
1077 uint dim = cast(uint)(cast(TypeSArray)tfrom).dim.toInteger(); | |
1078 | |
1079 if (tto) | |
1080 { | |
1081 uint fsize = cast(uint)tfrom.nextOf().size(); | |
1082 uint tsize = cast(uint)tto.nextOf().size(); | |
1083 | |
1084 if ((dim * fsize) % tsize != 0) | |
1085 { | |
1086 Lerr: | |
1087 error(loc, "cannot cast %s to %s since sizes don't line up", tfrom.toChars(), tto.toChars()); | |
1088 } | |
1089 dim = (dim * fsize) / tsize; | |
1090 } | |
1091 | |
1092 L1: | |
1093 elen = el_long(TYM.TYint, dim); | |
1094 e = el_una(OPER.OPaddr, TYM.TYnptr, e); | |
1095 e = el_pair(TYM.TYullong, elen, e); | |
1096 return e; | |
1097 } | |
1098 | |
1099 elem* eval_Darray(IRState* irs, Expression e) | |
1100 { | |
1101 elem* ex = e.toElem(irs); | |
1102 return array_toDarray(e.type, ex); | |
1103 } | |
1104 | |
1105 /*********************************************** | |
1106 * Generate code to set index into scope table. | |
1107 */ | |
1108 | |
1109 void setScopeIndex(Blockx* blx, block* b, int scope_index) | |
1110 { | |
1111 version (SEH) { | |
1112 block_appendexp(b, nteh_setScopeTableIndex(blx, scope_index)); | |
1113 } | |
1114 } | |
1115 | |
1116 /**************************************** | |
1117 * Create a static symbol we can hang DT initializers onto. | |
1118 */ | |
1119 | |
1120 Symbol* static_sym() | |
1121 { | |
1122 Symbol* s; | |
1123 type* t; | |
1124 | |
1125 t = type_alloc(TYint); | |
1126 t.Tcount++; | |
1127 s = symbol_calloc("internal"); | |
1128 s.Sclass = SCstatic; | |
1129 s.Sfl = FLextern; | |
1130 s.Sflags |= SFLnodebug; | |
1131 s.Stype = t; | |
1132 version (ELFOBJ_OR_MACHOBJ) { | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
1133 s.Sseg = Segment.DATA; |
0 | 1134 } |
1135 slist_add(s); | |
1136 return s; | |
1137 } | |
1138 | |
1139 /************************************** | |
1140 * Convert label to block. | |
1141 */ | |
1142 | |
1143 block* labelToBlock(Loc loc, Blockx *blx, LabelDsymbol label) | |
1144 { | |
1145 LabelStatement s; | |
1146 | |
1147 if (!label.statement) | |
1148 { | |
1149 error(loc, "undefined label %s", label.toChars()); | |
1150 return null; | |
1151 } | |
1152 | |
1153 s = label.statement; | |
1154 if (!s.lblock) | |
1155 { | |
1156 s.lblock = block_calloc(blx); | |
1157 if (s.isReturnLabel) | |
1158 s.lblock.Btry = null; | |
1159 } | |
1160 return s.lblock; | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
1161 } |
67 | 1162 |
1163 /******************************************* | |
1164 * Generate elem to zero fill contents of Symbol stmp | |
1165 * from *poffset..offset2. | |
1166 * May store anywhere from 0..maxoff, as this function | |
1167 * tries to use aligned int stores whereever possible. | |
1168 * Update *poffset to end of initialized hole; *poffset will be >= offset2. | |
1169 */ | |
1170 | |
1171 elem* fillHole(Symbol* stmp, size_t* poffset, size_t offset2, size_t maxoff) | |
1172 { | |
1173 elem* e = null; | |
1174 int basealign = 1; | |
1175 | |
1176 while (*poffset < offset2) | |
1177 { | |
1178 tym_t ty; | |
1179 elem* e1; | |
1180 | |
1181 if (tybasic(stmp.Stype.Tty) == TYnptr) | |
1182 e1 = el_var(stmp); | |
1183 else | |
1184 e1 = el_ptr(stmp); | |
1185 | |
1186 if (basealign) | |
1187 *poffset &= ~3; | |
1188 | |
1189 basealign = 1; | |
1190 size_t sz = maxoff - *poffset; | |
1191 switch (sz) | |
1192 { | |
1193 case 1: ty = TYchar; break; | |
1194 case 2: ty = TYshort; break; | |
1195 case 3: | |
1196 ty = TYshort; | |
1197 basealign = 0; | |
1198 break; | |
1199 default: | |
1200 ty = TYlong; | |
1201 break; | |
1202 } | |
1203 e1 = el_bin(OPadd, TYnptr, e1, el_long(TYsize_t, *poffset)); | |
1204 e1 = el_una(OPind, ty, e1); | |
1205 e1 = el_bin(OPeq, ty, e1, el_long(ty, 0)); | |
1206 e = el_combine(e, e1); | |
1207 *poffset += tysize[ty]; | |
1208 } | |
1209 return e; | |
1210 } |