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