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