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