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