Mercurial > projects > ddmd
annotate dmd/codegen/Util.d @ 77:ad4792a1cfd6
more D-ification container accessing
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Sun, 29 Aug 2010 14:36:55 +0100 |
parents | 7e0d548de9e6 |
children | be2ab491772e |
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 { | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
665 auto decl = ad.include(null, null); |
0 | 666 if (decl && decl.dim) |
667 { | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
73
diff
changeset
|
668 foreach(s; decl) |
0 | 669 e = el_combine(e, Dsymbol_toElem(s, irs)); |
670 } | |
671 } | |
672 else if ((vd = s.isVarDeclaration()) !is null) | |
673 { | |
674 s = s.toAlias(); | |
675 if (s != vd) | |
676 return Dsymbol_toElem(s, irs); | |
677 if (vd.isStatic() || vd.storage_class & (STC.STCextern | STC.STCtls | STC.STCgshared)) | |
678 vd.toObjFile(0); | |
679 else | |
680 { | |
681 sp = s.toSymbol(); | |
682 symbol_add(sp); | |
683 //printf("\tadding symbol '%s'\n", sp.Sident); | |
684 if (vd.init) | |
685 { | |
686 ExpInitializer ie = vd.init.isExpInitializer(); | |
687 if (ie) { | |
688 e = ie.exp.toElem(irs); | |
689 } | |
690 } | |
691 } | |
692 } | |
693 else if ((cd = s.isClassDeclaration()) !is null) | |
694 { | |
695 irs.deferToObj.push(cast(void*)s); | |
696 } | |
697 else if ((sd = s.isStructDeclaration()) !is null) | |
698 { | |
699 irs.deferToObj.push(cast(void*)sd); | |
700 } | |
701 else if ((fd = s.isFuncDeclaration()) !is null) | |
702 { | |
703 //printf("function %s\n", fd.toChars()); | |
704 irs.deferToObj.push(cast(void*)fd); | |
705 } | |
706 else if ((tm = s.isTemplateMixin()) !is null) | |
707 { | |
708 //printf("%s\n", tm.toChars()); | |
709 if (tm.members) | |
710 { | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
711 foreach(Dsymbol sm; tm.members) |
0 | 712 e = el_combine(e, Dsymbol_toElem(sm, irs)); |
713 } | |
714 } | |
715 else if ((td = s.isTupleDeclaration()) !is null) | |
716 { | |
717 for (size_t i = 0; i < td.objects.dim; i++) | |
718 { | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
719 auto o = cast(Object)td.objects.data[i]; |
0 | 720 ///if (o.dyncast() == DYNCAST_EXPRESSION) |
721 if (Expression eo = cast(Expression)o) | |
722 { | |
723 if (eo.op == TOK.TOKdsymbol) | |
724 { | |
725 DsymbolExp se = cast(DsymbolExp)eo; | |
726 e = el_combine(e, Dsymbol_toElem(se.s, irs)); | |
727 } | |
728 } | |
729 } | |
730 } | |
731 else if ((tyd = s.isTypedefDeclaration()) !is null) | |
732 { | |
733 irs.deferToObj.push(cast(void*)tyd); | |
734 } | |
735 | |
736 return e; | |
737 } | |
738 | |
739 /************************************** | |
740 * Given an expression e that is an array, | |
741 * determine and set the 'length' variable. | |
742 * Input: | |
743 * lengthVar Symbol of 'length' variable | |
744 * &e expression that is the array | |
745 * t1 Type of the array | |
746 * Output: | |
747 * e is rewritten to avoid side effects | |
748 * Returns: | |
749 * expression that initializes 'length' | |
750 */ | |
751 | |
752 elem* resolveLengthVar(VarDeclaration lengthVar, elem** pe, Type t1) | |
753 { | |
754 //printf("resolveLengthVar()\n"); | |
755 elem* einit = null; | |
756 | |
757 if (lengthVar && !(lengthVar.storage_class & STC.STCconst)) | |
758 { | |
759 elem* elength; | |
760 Symbol* slength; | |
761 | |
762 if (t1.ty == TY.Tsarray) | |
763 { | |
764 TypeSArray tsa = cast(TypeSArray)t1; | |
765 long length = tsa.dim.toInteger(); | |
766 | |
767 elength = el_long(TYM.TYuint, length); | |
768 goto L3; | |
769 } | |
770 else if (t1.ty == TY.Tarray) | |
771 { | |
772 elength = *pe; | |
773 *pe = el_same(&elength); | |
774 elength = el_una(OPER.OP64_32, TYM.TYuint, elength); | |
775 | |
776 L3: | |
777 slength = lengthVar.toSymbol(); | |
778 //symbol_add(slength); | |
779 | |
780 einit = el_bin(OPER.OPeq, TYM.TYuint, el_var(slength), elength); | |
781 } | |
782 } | |
783 return einit; | |
784 } | |
785 | |
786 /******************************************* | |
787 * Set an array pointed to by eptr to evalue: | |
788 * eptr[0..edim] = evalue; | |
789 * Input: | |
790 * eptr where to write the data to | |
791 * evalue value to write | |
792 * edim number of times to write evalue to eptr[] | |
793 * tb type of evalue | |
794 */ | |
795 | |
796 elem* setArray(elem* eptr, elem* edim, Type tb, elem* evalue, IRState* irs, int op) | |
797 { | |
798 int r; | |
799 elem* e; | |
800 int sz = cast(int)tb.size(); | |
801 | |
802 if (tb.ty == TY.Tfloat80 || tb.ty == TY.Timaginary80) | |
803 r = RTLSYM.RTLSYM_MEMSET80; | |
804 else if (tb.ty == TY.Tcomplex80) | |
805 r = RTLSYM.RTLSYM_MEMSET160; | |
806 else if (tb.ty == TY.Tcomplex64) | |
807 r = RTLSYM.RTLSYM_MEMSET128; | |
808 else | |
809 { | |
810 switch (sz) | |
811 { | |
812 case 1: r = RTLSYM.RTLSYM_MEMSET8; break; | |
813 case 2: r = RTLSYM.RTLSYM_MEMSET16; break; | |
814 case 4: r = RTLSYM.RTLSYM_MEMSET32; break; | |
815 case 8: r = RTLSYM.RTLSYM_MEMSET64; break; | |
816 default: r = RTLSYM.RTLSYM_MEMSETN; break; | |
817 } | |
818 | |
819 /* Determine if we need to do postblit | |
820 */ | |
821 if (op != TOK.TOKblit) | |
822 { | |
823 StructDeclaration sd = needsPostblit(tb); | |
824 if (sd) | |
825 { | |
826 /* Need to do postblit. | |
827 * void *_d_arraysetassign(void *p, void *value, int dim, TypeInfo ti); | |
828 */ | |
829 r = (op == TOK.TOKconstruct) ? RTLSYM.RTLSYM_ARRAYSETCTOR : RTLSYM.RTLSYM_ARRAYSETASSIGN; | |
830 evalue = el_una(OPER.OPaddr, TYM.TYnptr, evalue); | |
831 Expression ti = tb.getTypeInfo(null); | |
832 elem* eti = ti.toElem(irs); | |
833 e = el_params(eti, edim, evalue, eptr, null); | |
834 e = el_bin(OPER.OPcall, TYM.TYnptr, el_var(rtlsym[r]), e); | |
835 return e; | |
836 } | |
837 } | |
838 | |
839 if (r == RTLSYM.RTLSYM_MEMSETN) | |
840 { | |
841 // void *_memsetn(void *p, void *value, int dim, int sizelem) | |
842 evalue = el_una(OPER.OPaddr, TYM.TYnptr, evalue); | |
843 elem *esz = el_long(TYM.TYint, sz); | |
844 e = el_params(esz, edim, evalue, eptr, null); | |
845 e = el_bin(OPER.OPcall, TYM.TYnptr, el_var(rtlsym[r]), e); | |
846 return e; | |
847 } | |
848 } | |
849 if (sz > 1 && sz <= 8 && evalue.Eoper == OPER.OPconst && el_allbits(evalue, 0)) | |
850 { | |
851 r = RTLSYM.RTLSYM_MEMSET8; | |
852 edim = el_bin(OPER.OPmul, TYM.TYuint, edim, el_long(TYM.TYuint, sz)); | |
853 } | |
854 | |
855 if (tybasic(evalue.Ety) == TYM.TYstruct) | |
856 { | |
857 evalue = el_una(OPER.OPstrpar, TYM.TYstruct, evalue); | |
858 evalue.Enumbytes = evalue.E1.Enumbytes; | |
859 assert(evalue.Enumbytes); | |
860 } | |
861 | |
862 // Be careful about parameter side effect ordering | |
863 if (r == RTLSYM.RTLSYM_MEMSET8) | |
864 { | |
865 e = el_param(edim, evalue); | |
866 e = el_bin(OPER.OPmemset, TYM.TYnptr, eptr, e); | |
867 } | |
868 else | |
869 { | |
870 e = el_params(edim, evalue, eptr, null); | |
871 e = el_bin(OPER.OPcall, TYM.TYnptr, el_var(rtlsym[r]), e); | |
872 } | |
873 return e; | |
874 } | |
875 | |
876 /************************* | |
877 * Initialize the hidden aggregate member, vthis, with | |
878 * the context pointer. | |
879 * Returns: | |
880 * *(ey + ad.vthis.offset) = this; | |
881 */ | |
882 version (DMDV2) { | |
883 elem* setEthis(Loc loc, IRState* irs, elem* ey, AggregateDeclaration ad) | |
884 { | |
885 elem* ethis; | |
886 FuncDeclaration thisfd = irs.getFunc(); | |
887 int offset = 0; | |
888 Dsymbol cdp = ad.toParent2(); // class/func we're nested in | |
889 | |
890 //printf("setEthis(ad = %s, cdp = %s, thisfd = %s)\n", ad.toChars(), cdp.toChars(), thisfd.toChars()); | |
891 | |
892 if (cdp is thisfd) | |
893 { | |
894 /* Class we're new'ing is a local class in this function: | |
895 * void thisfd() { class ad { } } | |
896 */ | |
897 if (irs.sclosure) | |
898 ethis = el_var(irs.sclosure); | |
899 else if (irs.sthis) | |
900 { | |
901 /// version (DMDV2) { | |
902 if (thisfd.closureVars.dim) | |
903 /// } else { | |
904 /// if (thisfd.nestedFrameRef) | |
905 /// } | |
906 { | |
907 ethis = el_ptr(irs.sthis); | |
908 } | |
909 else | |
910 ethis = el_var(irs.sthis); | |
911 } | |
912 else | |
913 { | |
914 ethis = el_long(TYM.TYnptr, 0); | |
915 /// version (DMDV2) { | |
916 if (thisfd.closureVars.dim) | |
917 /// } else { | |
918 /// if (thisfd.nestedFrameRef) | |
919 /// } | |
920 { | |
921 ethis.Eoper = OPER.OPframeptr; | |
922 } | |
923 } | |
924 } | |
925 else if (thisfd.vthis && ( | |
926 cdp == thisfd.toParent2() || ( | |
927 cdp.isClassDeclaration() && cdp.isClassDeclaration().isBaseOf(thisfd.toParent2().isClassDeclaration(), &offset) | |
928 ) | |
929 ) | |
930 ) | |
931 { | |
932 /* Class we're new'ing is at the same level as thisfd | |
933 */ | |
934 assert(offset == 0); // BUG: should handle this case | |
935 ethis = el_var(irs.sthis); | |
936 } | |
937 else | |
938 { | |
939 ethis = getEthis(loc, irs, ad.toParent2()); | |
940 ethis = el_una(OPER.OPaddr, TYM.TYnptr, ethis); | |
941 } | |
942 | |
943 ey = el_bin(OPER.OPadd, TYM.TYnptr, ey, el_long(TYM.TYint, ad.vthis.offset)); | |
944 ey = el_una(OPER.OPind, TYM.TYnptr, ey); | |
945 ey = el_bin(OPER.OPeq, TYM.TYnptr, ey, ethis); | |
946 | |
947 return ey; | |
948 } | |
949 } | |
950 | |
951 /******************************************** | |
952 * Determine if t is an array of structs that need a postblit. | |
953 */ | |
954 StructDeclaration needsPostblit(Type t) | |
955 { | |
956 t = t.toBasetype(); | |
957 | |
958 while (t.ty == TY.Tsarray) | |
959 t = t.nextOf().toBasetype(); | |
960 | |
961 if (t.ty == TY.Tstruct) | |
962 { | |
963 StructDeclaration sd = (cast(TypeStruct)t).sym; | |
964 if (sd.postblit) | |
965 return sd; | |
966 } | |
967 | |
968 return null; | |
969 } | |
970 | |
971 /***************************************** | |
972 * Convert array to a dynamic array. | |
973 */ | |
974 | |
975 elem* array_toDarray(Type t, elem* e) | |
976 { | |
977 uint dim; | |
978 elem* ef = null; | |
979 elem* ex; | |
980 | |
981 //printf("array_toDarray(t = %s)\n", t.toChars()); | |
982 //elem_print(e); | |
983 t = t.toBasetype(); | |
984 switch (t.ty) | |
985 { | |
986 case TY.Tarray: | |
987 break; | |
988 | |
989 case TY.Tsarray: | |
990 e = el_una(OPER.OPaddr, TYM.TYnptr, e); | |
991 dim = cast(uint)(cast(TypeSArray)t).dim.toInteger(); | |
992 e = el_pair(TYM.TYullong, el_long(TYM.TYint, dim), e); | |
993 break; | |
994 | |
995 default: | |
996 L1: | |
997 switch (e.Eoper) | |
998 { | |
999 case OPER.OPconst: | |
1000 { | |
1001 size_t len = tysize[tybasic(e.Ety)]; | |
1002 elem* es = el_calloc(); | |
1003 es.Eoper = OPER.OPstring; | |
1004 | |
1005 // 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
|
1006 es.EV.ss.Vstring = cast(char*)malloc(len); /// ! |
0 | 1007 memcpy(es.EV.ss.Vstring, &e.EV, len); |
1008 | |
1009 es.EV.ss.Vstrlen = len; | |
1010 es.Ety = TYM.TYnptr; | |
1011 e = es; | |
1012 break; | |
1013 } | |
1014 | |
1015 case OPER.OPvar: | |
1016 e = el_una(OPER.OPaddr, TYM.TYnptr, e); | |
1017 break; | |
1018 | |
1019 case OPER.OPcomma: | |
1020 ef = el_combine(ef, e.E1); | |
1021 ex = e; | |
1022 e = e.E2; | |
1023 ex.E1() = null; | |
1024 ex.E2() = null; | |
1025 el_free(ex); | |
1026 goto L1; | |
1027 | |
1028 case OPER.OPind: | |
1029 ex = e; | |
1030 e = e.E1; | |
1031 ex.E1() = null; | |
1032 ex.E2() = null; | |
1033 el_free(ex); | |
1034 break; | |
1035 | |
1036 default: | |
1037 { | |
1038 // Copy expression to a variable and take the | |
1039 // address of that variable. | |
1040 Symbol* stmp; | |
1041 tym_t ty = tybasic(e.Ety); | |
1042 | |
1043 if (ty == TYM.TYstruct) | |
1044 { | |
1045 if (e.Enumbytes == 4) | |
1046 ty = TYM.TYint; | |
1047 else if (e.Enumbytes == 8) | |
1048 ty = TYM.TYllong; | |
1049 } | |
1050 e.Ety = ty; | |
1051 stmp = symbol_genauto(type_fake(ty)); | |
1052 e = el_bin(OPER.OPeq, e.Ety, el_var(stmp), e); | |
1053 e = el_bin(OPER.OPcomma, TYM.TYnptr, e, el_una(OPER.OPaddr, TYM.TYnptr, el_var(stmp))); | |
1054 break; | |
1055 } | |
1056 } | |
1057 dim = 1; | |
1058 e = el_pair(TYM.TYullong, el_long(TYM.TYint, dim), e); | |
1059 break; | |
1060 } | |
1061 | |
1062 return el_combine(ef, e); | |
1063 } | |
1064 | |
1065 elem* sarray_toDarray(Loc loc, Type tfrom, Type tto, elem* e) | |
1066 { | |
1067 //printf("sarray_toDarray()\n"); | |
1068 //elem_print(e); | |
1069 | |
1070 elem* elen; | |
1071 uint dim = cast(uint)(cast(TypeSArray)tfrom).dim.toInteger(); | |
1072 | |
1073 if (tto) | |
1074 { | |
1075 uint fsize = cast(uint)tfrom.nextOf().size(); | |
1076 uint tsize = cast(uint)tto.nextOf().size(); | |
1077 | |
1078 if ((dim * fsize) % tsize != 0) | |
1079 { | |
1080 Lerr: | |
1081 error(loc, "cannot cast %s to %s since sizes don't line up", tfrom.toChars(), tto.toChars()); | |
1082 } | |
1083 dim = (dim * fsize) / tsize; | |
1084 } | |
1085 | |
1086 L1: | |
1087 elen = el_long(TYM.TYint, dim); | |
1088 e = el_una(OPER.OPaddr, TYM.TYnptr, e); | |
1089 e = el_pair(TYM.TYullong, elen, e); | |
1090 return e; | |
1091 } | |
1092 | |
1093 elem* eval_Darray(IRState* irs, Expression e) | |
1094 { | |
1095 elem* ex = e.toElem(irs); | |
1096 return array_toDarray(e.type, ex); | |
1097 } | |
1098 | |
1099 /*********************************************** | |
1100 * Generate code to set index into scope table. | |
1101 */ | |
1102 | |
1103 void setScopeIndex(Blockx* blx, block* b, int scope_index) | |
1104 { | |
1105 version (SEH) { | |
1106 block_appendexp(b, nteh_setScopeTableIndex(blx, scope_index)); | |
1107 } | |
1108 } | |
1109 | |
1110 /**************************************** | |
1111 * Create a static symbol we can hang DT initializers onto. | |
1112 */ | |
1113 | |
1114 Symbol* static_sym() | |
1115 { | |
1116 Symbol* s; | |
1117 type* t; | |
1118 | |
1119 t = type_alloc(TYint); | |
1120 t.Tcount++; | |
1121 s = symbol_calloc("internal"); | |
1122 s.Sclass = SCstatic; | |
1123 s.Sfl = FLextern; | |
1124 s.Sflags |= SFLnodebug; | |
1125 s.Stype = t; | |
1126 version (ELFOBJ_OR_MACHOBJ) { | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
1127 s.Sseg = Segment.DATA; |
0 | 1128 } |
1129 slist_add(s); | |
1130 return s; | |
1131 } | |
1132 | |
1133 /************************************** | |
1134 * Convert label to block. | |
1135 */ | |
1136 | |
1137 block* labelToBlock(Loc loc, Blockx *blx, LabelDsymbol label) | |
1138 { | |
1139 LabelStatement s; | |
1140 | |
1141 if (!label.statement) | |
1142 { | |
1143 error(loc, "undefined label %s", label.toChars()); | |
1144 return null; | |
1145 } | |
1146 | |
1147 s = label.statement; | |
1148 if (!s.lblock) | |
1149 { | |
1150 s.lblock = block_calloc(blx); | |
1151 if (s.isReturnLabel) | |
1152 s.lblock.Btry = null; | |
1153 } | |
1154 return s.lblock; | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
1155 } |
67 | 1156 |
1157 /******************************************* | |
1158 * Generate elem to zero fill contents of Symbol stmp | |
1159 * from *poffset..offset2. | |
1160 * May store anywhere from 0..maxoff, as this function | |
1161 * tries to use aligned int stores whereever possible. | |
1162 * Update *poffset to end of initialized hole; *poffset will be >= offset2. | |
1163 */ | |
1164 | |
1165 elem* fillHole(Symbol* stmp, size_t* poffset, size_t offset2, size_t maxoff) | |
1166 { | |
1167 elem* e = null; | |
1168 int basealign = 1; | |
1169 | |
1170 while (*poffset < offset2) | |
1171 { | |
1172 tym_t ty; | |
1173 elem* e1; | |
1174 | |
1175 if (tybasic(stmp.Stype.Tty) == TYnptr) | |
1176 e1 = el_var(stmp); | |
1177 else | |
1178 e1 = el_ptr(stmp); | |
1179 | |
1180 if (basealign) | |
1181 *poffset &= ~3; | |
1182 | |
1183 basealign = 1; | |
1184 size_t sz = maxoff - *poffset; | |
1185 switch (sz) | |
1186 { | |
1187 case 1: ty = TYchar; break; | |
1188 case 2: ty = TYshort; break; | |
1189 case 3: | |
1190 ty = TYshort; | |
1191 basealign = 0; | |
1192 break; | |
1193 default: | |
1194 ty = TYlong; | |
1195 break; | |
1196 } | |
1197 e1 = el_bin(OPadd, TYnptr, e1, el_long(TYsize_t, *poffset)); | |
1198 e1 = el_una(OPind, ty, e1); | |
1199 e1 = el_bin(OPeq, ty, e1, el_long(ty, 0)); | |
1200 e = el_combine(e, e1); | |
1201 *poffset += tysize[ty]; | |
1202 } | |
1203 return e; | |
1204 } |