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