Mercurial > projects > ddmd
annotate dmd/TypeAArray.d @ 68:ee3a9f34dc48
final bits of codegen implementation to compile Phobos
author | korDen |
---|---|
date | Tue, 24 Aug 2010 16:44:34 +0400 |
parents | f708f0452e81 |
children | 2e2a5c3f943a |
rev | line source |
---|---|
0 | 1 module dmd.TypeAArray; |
2 | |
3 import dmd.TypeArray; | |
4 import dmd.MOD; | |
5 import dmd.ArrayTypes; | |
6 import dmd.TypeInfoDeclaration; | |
68
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
67
diff
changeset
|
7 import dmd.TypeInfoAssociativeArrayDeclaration; |
0 | 8 import dmd.Expression; |
9 import dmd.Scope; | |
10 import dmd.Loc; | |
67 | 11 import dmd.Global; |
0 | 12 import dmd.Dsymbol; |
13 import dmd.Type; | |
14 import dmd.TypeSArray; | |
15 import dmd.OutBuffer; | |
16 import dmd.HdrGenState; | |
17 import dmd.Identifier; | |
18 import dmd.MATCH; | |
19 import dmd.TY; | |
20 import dmd.Id; | |
21 import dmd.CallExp; | |
22 import dmd.IntegerExp; | |
23 import dmd.FuncDeclaration; | |
24 import dmd.VarExp; | |
25 import dmd.TypeFunction; | |
26 import dmd.NullExp; | |
27 import dmd.Array; | |
28 | |
29 import dmd.backend.Symbol; | |
30 import dmd.backend.TYPE; | |
31 import dmd.backend.Util; | |
32 import dmd.backend.SC; | |
33 import dmd.backend.LIST; | |
34 import dmd.backend.TYM; | |
35 import dmd.backend.TF; | |
67 | 36 import dmd.backend.Classsym; |
0 | 37 import dmd.backend.mTYman; |
38 | |
39 import core.stdc.stdio; | |
40 import core.stdc.stdlib; | |
14
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
41 version (Bug4054) |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
42 import core.memory; |
0 | 43 class TypeAArray : TypeArray |
44 { | |
45 Type index; // key type | |
46 | |
47 this(Type t, Type index) | |
48 { | |
49 super(Taarray, t); | |
50 this.index = index; | |
51 } | |
52 | |
53 Type syntaxCopy() | |
54 { | |
51 | 55 Type t = next.syntaxCopy(); |
56 Type ti = index.syntaxCopy(); | |
57 if (t == next && ti == index) | |
58 t = this; | |
59 else | |
60 { | |
61 t = new TypeAArray(t, ti); | |
62 t.mod = mod; | |
63 } | |
64 return t; | |
0 | 65 } |
66 | |
67 version (DumbClone) { | |
68 } else { | |
69 Type clone() | |
70 { | |
71 assert(false); | |
72 } | |
73 } | |
74 ulong size(Loc loc) | |
75 { | |
76 return PTRSIZE /* * 2*/; | |
77 } | |
78 | |
79 Type semantic(Loc loc, Scope sc) | |
80 { | |
81 //printf("TypeAArray::semantic() %s index.ty = %d\n", toChars(), index.ty); | |
82 | |
83 // Deal with the case where we thought the index was a type, but | |
84 // in reality it was an expression. | |
85 if (index.ty == Tident || index.ty == Tinstance || index.ty == Tsarray) | |
86 { | |
87 Expression e; | |
88 Type t; | |
89 Dsymbol s; | |
90 | |
91 index.resolve(loc, sc, &e, &t, &s); | |
92 if (e) | |
93 { // It was an expression - | |
94 // Rewrite as a static array | |
95 TypeSArray tsa = new TypeSArray(next, e); | |
96 return tsa.semantic(loc,sc); | |
97 } | |
98 else if (t) | |
99 index = t; | |
100 else | |
101 index.error(loc, "index is not a type or an expression"); | |
102 } | |
103 else | |
104 index = index.semantic(loc,sc); | |
105 | |
106 if (index.nextOf() && !index.nextOf().isInvariant()) | |
107 { | |
108 index = index.constOf().mutableOf(); | |
109 static if (false) { | |
110 printf("index is %p %s\n", index, index.toChars()); | |
111 index.check(); | |
112 printf("index.mod = x%x\n", index.mod); | |
113 printf("index.ito = x%x\n", index.ito); | |
114 if (index.ito) { | |
115 printf("index.ito.mod = x%x\n", index.ito.mod); | |
116 printf("index.ito.ito = x%x\n", index.ito.ito); | |
117 } | |
118 } | |
119 } | |
120 | |
121 switch (index.toBasetype().ty) | |
122 { | |
123 case Tbool: | |
124 case Tfunction: | |
125 case Tvoid: | |
126 case Tnone: | |
127 error(loc, "can't have associative array key of %s", index.toBasetype().toChars()); | |
128 break; | |
129 default: | |
130 break; /// | |
131 } | |
132 next = next.semantic(loc,sc); | |
133 transitive(); | |
134 | |
135 switch (next.toBasetype().ty) | |
136 { | |
137 case Tfunction: | |
138 case Tnone: | |
139 error(loc, "can't have associative array of %s", next.toChars()); | |
140 break; | |
141 default: | |
142 break; /// | |
143 } | |
144 if (next.isauto()) | |
145 error(loc, "cannot have array of auto %s", next.toChars()); | |
146 | |
147 return merge(); | |
148 } | |
149 | |
150 void resolve(Loc loc, Scope sc, Expression* pe, Type* pt, Dsymbol* ps) | |
151 { | |
51 | 152 //printf("TypeAArray.resolve() %s\n", toChars()); |
153 | |
154 // Deal with the case where we thought the index was a type, but | |
155 // in reality it was an expression. | |
156 if (index.ty == Tident || index.ty == Tinstance || index.ty == Tsarray) | |
157 { | |
158 Expression e; | |
159 Type t; | |
160 Dsymbol s; | |
161 | |
162 index.resolve(loc, sc, &e, &t, &s); | |
163 if (e) | |
164 { // It was an expression - | |
165 // Rewrite as a static array | |
166 | |
167 TypeSArray tsa = new TypeSArray(next, e); | |
168 return tsa.resolve(loc, sc, pe, pt, ps); | |
169 } | |
170 else if (t) | |
171 index = t; | |
172 else | |
173 index.error(loc, "index is not a type or an expression"); | |
174 } | |
175 Type.resolve(loc, sc, pe, pt, ps); | |
0 | 176 } |
177 | |
178 void toDecoBuffer(OutBuffer buf, int flag) | |
179 { | |
180 Type.toDecoBuffer(buf, flag); | |
181 index.toDecoBuffer(buf); | |
182 next.toDecoBuffer(buf, (flag & 0x100) ? MOD.MODundefined : mod); | |
183 } | |
184 | |
185 void toCBuffer2(OutBuffer buf, HdrGenState* hgs, MOD mod) | |
186 { | |
68
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
67
diff
changeset
|
187 if (mod != this.mod) |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
67
diff
changeset
|
188 { |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
67
diff
changeset
|
189 toCBuffer3(buf, hgs, mod); |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
67
diff
changeset
|
190 return; |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
67
diff
changeset
|
191 } |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
67
diff
changeset
|
192 next.toCBuffer2(buf, hgs, this.mod); |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
67
diff
changeset
|
193 buf.writeByte('['); |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
67
diff
changeset
|
194 index.toCBuffer2(buf, hgs, MODundefined); |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
67
diff
changeset
|
195 buf.writeByte(']'); |
0 | 196 } |
197 | |
198 Expression dotExp(Scope sc, Expression e, Identifier ident) | |
199 { | |
200 version (LOGDOTEXP) { | |
201 printf("TypeAArray.dotExp(e = '%s', ident = '%s')\n", e.toChars(), ident.toChars()); | |
202 } | |
203 if (ident == Id.length) | |
204 { | |
205 Expression ec; | |
206 FuncDeclaration fd; | |
207 Expressions arguments; | |
208 | |
209 fd = FuncDeclaration.genCfunc(Type.tsize_t, Id.aaLen); | |
210 ec = new VarExp(Loc(0), fd); | |
211 arguments = new Expressions(); | |
212 arguments.push(cast(void*)e); | |
213 e = new CallExp(e.loc, ec, arguments); | |
214 e.type = (cast(TypeFunction)fd.type).next; | |
215 } | |
216 else if (ident == Id.keys) | |
217 { | |
218 Expression ec; | |
219 FuncDeclaration fd; | |
220 Expressions arguments; | |
221 int size = cast(int)index.size(e.loc); | |
222 | |
223 assert(size); | |
224 fd = FuncDeclaration.genCfunc(Type.tindex, Id.aaKeys); | |
225 ec = new VarExp(Loc(0), fd); | |
226 arguments = new Expressions(); | |
227 arguments.push(cast(void*)e); | |
228 arguments.push(cast(void*)new IntegerExp(Loc(0), size, Type.tsize_t)); | |
229 e = new CallExp(e.loc, ec, arguments); | |
230 e.type = index.arrayOf(); | |
231 } | |
232 else if (ident == Id.values) | |
233 { | |
234 Expression ec; | |
235 FuncDeclaration fd; | |
236 Expressions arguments; | |
237 | |
238 fd = FuncDeclaration.genCfunc(Type.tindex, Id.aaValues); | |
239 ec = new VarExp(Loc(0), fd); | |
240 arguments = new Expressions(); | |
241 arguments.push(cast(void*)e); | |
242 size_t keysize = cast(size_t)index.size(e.loc); | |
243 keysize = (keysize + PTRSIZE - 1) & ~(PTRSIZE - 1); | |
244 arguments.push(cast(void*)new IntegerExp(Loc(0), keysize, Type.tsize_t)); | |
245 arguments.push(cast(void*)new IntegerExp(Loc(0), next.size(e.loc), Type.tsize_t)); | |
246 e = new CallExp(e.loc, ec, arguments); | |
247 e.type = next.arrayOf(); | |
248 } | |
249 else if (ident == Id.rehash) | |
250 { | |
251 Expression ec; | |
252 FuncDeclaration fd; | |
253 Expressions arguments; | |
254 | |
255 fd = FuncDeclaration.genCfunc(Type.tint64, Id.aaRehash); | |
256 ec = new VarExp(Loc(0), fd); | |
257 arguments = new Expressions(); | |
258 arguments.push(cast(void*)e.addressOf(sc)); | |
259 arguments.push(cast(void*)index.getInternalTypeInfo(sc)); | |
260 e = new CallExp(e.loc, ec, arguments); | |
261 e.type = this; | |
262 } | |
263 else | |
264 { | |
265 e = Type.dotExp(sc, e, ident); | |
266 } | |
267 return e; | |
268 } | |
269 | |
270 Expression defaultInit(Loc loc) | |
271 { | |
272 version (LOGDEFAULTINIT) { | |
273 printf("TypeAArray.defaultInit() '%s'\n", toChars()); | |
274 } | |
275 Expression e = new NullExp(loc); | |
276 e.type = this; | |
277 return e; | |
278 } | |
279 | |
280 MATCH deduceType(Scope sc, Type tparam, TemplateParameters parameters, Objects dedtypes) | |
281 { | |
51 | 282 static if (false) { |
283 printf("TypeAArray.deduceType()\n"); | |
284 printf("\tthis = %d, ", ty); print(); | |
285 printf("\ttparam = %d, ", tparam.ty); tparam.print(); | |
286 } | |
287 | |
288 // Extra check that index type must match | |
289 if (tparam && tparam.ty == Taarray) | |
290 { | |
291 TypeAArray tp = cast(TypeAArray)tparam; | |
292 if (!index.deduceType(sc, tp.index, parameters, dedtypes)) | |
293 { | |
294 return MATCHnomatch; | |
295 } | |
296 } | |
297 return Type.deduceType(sc, tparam, parameters, dedtypes); | |
0 | 298 } |
299 | |
300 bool isZeroInit(Loc loc) | |
301 { | |
302 assert(false); | |
303 } | |
304 | |
305 bool checkBoolean() | |
306 { | |
307 assert(false); | |
308 } | |
309 | |
310 TypeInfoDeclaration getTypeInfoDeclaration() | |
311 { | |
68
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
67
diff
changeset
|
312 return new TypeInfoAssociativeArrayDeclaration(this); |
0 | 313 } |
314 | |
315 bool hasPointers() | |
316 { | |
317 return true; | |
318 } | |
319 | |
320 MATCH implicitConvTo(Type to) | |
321 { | |
51 | 322 //printf("TypeAArray.implicitConvTo(to = %s) this = %s\n", to.toChars(), toChars()); |
323 if (equals(to)) | |
324 return MATCHexact; | |
325 | |
326 if (to.ty == Taarray) | |
327 { | |
328 TypeAArray ta = cast(TypeAArray)to; | |
329 | |
330 if (!(next.mod == ta.next.mod || ta.next.mod == MODconst)) | |
331 return MATCHnomatch; // not const-compatible | |
332 | |
333 if (!(index.mod == ta.index.mod || ta.index.mod == MODconst)) | |
334 return MATCHnomatch; // not const-compatible | |
335 | |
336 MATCH m = next.constConv(ta.next); | |
337 MATCH mi = index.constConv(ta.index); | |
338 if (m != MATCHnomatch && mi != MATCHnomatch) | |
339 { | |
340 if (m == MATCHexact && mod != to.mod) | |
341 m = MATCHconst; | |
342 if (mi < m) | |
343 m = mi; | |
344 return m; | |
345 } | |
346 } | |
347 return Type.implicitConvTo(to); | |
0 | 348 } |
349 | |
350 MATCH constConv(Type to) | |
351 { | |
352 assert(false); | |
353 } | |
354 | |
355 version (CPP_MANGLE) { | |
356 void toCppMangle(OutBuffer buf, CppMangleState* cms) | |
357 { | |
358 assert(false); | |
359 } | |
360 } | |
361 | |
362 // Back end | |
363 /******************************************** | |
364 * Determine the right symbol to look up | |
365 * an associative array element. | |
366 * Input: | |
367 * flags 0 don't add value signature | |
368 * 1 add value signature | |
369 */ | |
370 Symbol* aaGetSymbol(const(char)* func, int flags) | |
371 in | |
372 { | |
373 assert(func); | |
374 assert((flags & ~1) == 0); | |
375 } | |
376 out (result) | |
377 { | |
378 assert(result); | |
379 } | |
380 body | |
381 { | |
382 int sz; | |
383 char* id; | |
384 type* t; | |
385 Symbol* s; | |
386 int i; | |
387 | |
388 // Dumb linear symbol table - should use associative array! | |
389 static Array sarray = null; | |
390 | |
391 //printf("aaGetSymbol(func = '%s', flags = %d, key = %p)\n", func, flags, key); | |
392 static if (false) { | |
393 scope OutBuffer buf = new OutBuffer(); | |
394 key.toKeyBuffer(buf); | |
395 | |
396 sz = next.size(); // it's just data, so we only care about the size | |
397 sz = (sz + 3) & ~3; // reduce proliferation of library routines | |
14
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
398 version (Bug4054) |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
399 id = cast(char*)GC.malloc(3 + strlen(func) + buf.offset + sizeof(sz) * 3 + 1); |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
400 else |
0 | 401 id = cast(char*)alloca(3 + strlen(func) + buf.offset + sizeof(sz) * 3 + 1); |
402 buf.writeByte(0); | |
403 if (flags & 1) | |
404 sprintf(id, "_aa%s%s%d", func, buf.data, sz); | |
405 else | |
406 sprintf(id, "_aa%s%s", func, buf.data); | |
407 } else { | |
14
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
408 version (Bug4054) |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
409 id = cast(char*)GC.malloc(3 + strlen(func) + 1); |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
410 else |
0 | 411 id = cast(char*)alloca(3 + strlen(func) + 1); |
412 sprintf(id, "_aa%s", func); | |
413 } | |
414 if (!sarray) | |
415 sarray = new Array(); | |
416 | |
417 // See if symbol is already in sarray | |
418 for (i = 0; i < sarray.dim; i++) | |
419 { | |
420 s = cast(Symbol*)sarray.data[i]; | |
421 if (strcmp(id, s.Sident.ptr) == 0) | |
422 return s; // use existing Symbol | |
423 } | |
424 | |
425 // Create new Symbol | |
426 | |
427 s = symbol_calloc(id); | |
428 slist_add(s); | |
429 s.Sclass = SCextern; | |
430 s.Ssymnum = -1; | |
431 symbol_func(s); | |
432 | |
433 t = type_alloc(TYnfunc); | |
434 t.Tflags = TFprototype | TFfixed; | |
435 t.Tmangle = mTYman_c; | |
436 t.Tparamtypes = null; | |
437 t.Tnext = next.toCtype(); | |
438 t.Tnext.Tcount++; | |
439 t.Tcount++; | |
440 s.Stype = t; | |
441 | |
442 sarray.push(s); // remember it | |
443 return s; | |
444 } | |
445 | |
446 type* toCtype() | |
447 { | |
67 | 448 type* t; |
449 | |
450 if (ctype) | |
451 return ctype; | |
452 | |
453 if (0 && global.params.symdebug) | |
454 { | |
455 /* An associative array is represented by: | |
456 * struct AArray { size_t length; void* ptr; } | |
457 */ | |
458 | |
459 static Symbol* s; | |
460 | |
461 if (!s) | |
462 { | |
463 s = symbol_calloc("_AArray"); | |
464 s.Sclass = SCstruct; | |
465 s.Sstruct = struct_calloc(); | |
466 s.Sstruct.Sflags |= 0; | |
467 s.Sstruct.Salignsize = alignsize(); | |
468 s.Sstruct.Sstructalign = cast(ubyte)global.structalign; | |
469 s.Sstruct.Sstructsize = cast(uint)size(Loc(0)); | |
470 slist_add(s); | |
471 | |
472 Symbol* s1 = symbol_name("length", SCmember, Type.tsize_t.toCtype()); | |
473 list_append(&s.Sstruct.Sfldlst, s1); | |
474 | |
475 Symbol* s2 = symbol_name("data", SCmember, Type.tvoidptr.toCtype()); | |
476 s2.Smemoff = cast(uint)Type.tsize_t.size(); | |
477 list_append(&s.Sstruct.Sfldlst, s2); | |
478 } | |
479 | |
480 t = type_alloc(TYstruct); | |
481 t.Ttag = cast(Classsym*)s; // structure tag name | |
482 t.Tcount++; | |
483 s.Stype = t; | |
484 } | |
485 else | |
486 { | |
487 if (global.params.symdebug == 1) | |
488 { | |
489 /* Generate D symbolic debug info, rather than C | |
490 * Tnext: element type | |
491 * Tkey: key type | |
492 */ | |
493 t = type_allocn(TYaarray, next.toCtype()); | |
494 t.Tkey = index.toCtype(); | |
495 t.Tkey.Tcount++; | |
496 } | |
497 else | |
498 t = type_fake(TYaarray); | |
499 } | |
500 t.Tcount++; | |
501 ctype = t; | |
502 return t; | |
0 | 503 } |
14
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
504 } |