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