comparison gen/typinf.c @ 54:28e99b04a132 trunk

[svn r58] Fixed cond expression resulting in a non-basic type. Fixed identity expression for dynamic arrays. Revamped the system to keep track of lvalues and rvalues and their relations. Typedef declaration now generate the custom typeinfo. Other bugfixes.
author lindquist
date Wed, 24 Oct 2007 01:37:34 +0200
parents 0c77619e803b
children a9d29e9f1fed
comparison
equal deleted inserted replaced
53:06ccc817acd4 54:28e99b04a132
28 #include "aggregate.h" 28 #include "aggregate.h"
29 29
30 #include "gen/irstate.h" 30 #include "gen/irstate.h"
31 #include "gen/logger.h" 31 #include "gen/logger.h"
32 #include "gen/runtime.h" 32 #include "gen/runtime.h"
33 #include "gen/tollvm.h"
34 #include "gen/arrays.h"
33 35
34 /******************************************* 36 /*******************************************
35 * Get a canonicalized form of the TypeInfo for use with the internal 37 * Get a canonicalized form of the TypeInfo for use with the internal
36 * runtime library routines. Canonicalized in that static arrays are 38 * runtime library routines. Canonicalized in that static arrays are
37 * represented as dynamic arrays, enums are represented by their 39 * represented as dynamic arrays, enums are represented by their
240 Logger::println("type = '%s'", tinfo->toChars()); 242 Logger::println("type = '%s'", tinfo->toChars());
241 243
242 if (llvmTouched) return; 244 if (llvmTouched) return;
243 else llvmTouched = true; 245 else llvmTouched = true;
244 246
245 Logger::println("Getting typeinfo var: %s", mangle()); 247 Logger::println("typeinfo mangle: %s", mangle());
246 llvmValue = LLVM_D_GetRuntimeGlobal(gIR->module, mangle()); 248
247 assert(llvmValue); 249 if (tinfo->builtinTypeInfo()) {
248 Logger::cout() << "Got:" << '\n' << *llvmValue << '\n'; 250 // this is a declaration of a builtin __initZ var
251 llvmValue = LLVM_D_GetRuntimeGlobal(gIR->module, mangle());
252 assert(llvmValue);
253 Logger::cout() << "Got typeinfo var:" << '\n' << *llvmValue << '\n';
254 }
255 else {
256 toDt(NULL);
257 // this is a specialized typeinfo
258 //std::vector<const llvm::Type*> stypes;
259 //stypes.push_back(
260 }
249 } 261 }
250 262
251 /* ========================================================================= */ 263 /* ========================================================================= */
252 264
253 void TypeInfoDeclaration::toDt(dt_t **pdt) 265 void TypeInfoDeclaration::toDt(dt_t **pdt)
255 assert(0 && "TypeInfoDeclaration"); 267 assert(0 && "TypeInfoDeclaration");
256 } 268 }
257 269
258 void TypeInfoTypedefDeclaration::toDt(dt_t **pdt) 270 void TypeInfoTypedefDeclaration::toDt(dt_t **pdt)
259 { 271 {
260 assert(0 && "TypeInfoTypedefDeclaration"); 272 Logger::println("TypeInfoTypedefDeclaration::toDt() %s", toChars());
273 LOG_SCOPE;
274
275 ClassDeclaration* base = Type::typeinfotypedef;
276 base->toObjFile();
277
278 llvm::Constant* initZ = base->llvmInitZ;
279 assert(initZ);
280 const llvm::StructType* stype = llvm::cast<llvm::StructType>(initZ->getType());
281
282 std::vector<llvm::Constant*> sinits;
283 sinits.push_back(initZ->getOperand(0));
284
285 assert(tinfo->ty == Ttypedef);
286 TypeTypedef *tc = (TypeTypedef *)tinfo;
287 TypedefDeclaration *sd = tc->sym;
288
289 // TypeInfo base
290 //const llvm::PointerType* basept = llvm::cast<llvm::PointerType>(initZ->getOperand(1)->getType());
291 //sinits.push_back(llvm::ConstantPointerNull::get(basept));
292 Logger::println("generating base typeinfo");
293 //sd->basetype = sd->basetype->merge();
294 sd->basetype->getTypeInfo(NULL); // generate vtinfo
295 assert(sd->basetype->vtinfo);
296 if (!sd->basetype->vtinfo->llvmValue)
297 sd->basetype->vtinfo->toObjFile();
298 assert(llvm::isa<llvm::Constant>(sd->basetype->vtinfo->llvmValue));
299 llvm::Constant* castbase = llvm::cast<llvm::Constant>(sd->basetype->vtinfo->llvmValue);
300 castbase = llvm::ConstantExpr::getBitCast(castbase, initZ->getOperand(1)->getType());
301 sinits.push_back(castbase);
302
303 // char[] name
304 char *name = sd->toPrettyChars();
305 sinits.push_back(LLVM_DtoConstString(name));
306 assert(sinits.back()->getType() == initZ->getOperand(2)->getType());
307
308 // void[] init
309 //const llvm::PointerType* initpt = llvm::PointerType::get(llvm::Type::Int8Ty);
310 //sinits.push_back(LLVM_DtoConstantSlice(LLVM_DtoConstSize_t(0), llvm::ConstantPointerNull::get(initpt)));
311 sinits.push_back(initZ->getOperand(3));
312
313 // create the symbol
314 llvm::Constant* tiInit = llvm::ConstantStruct::get(stype, sinits);
315 llvm::GlobalVariable* gvar = new llvm::GlobalVariable(stype,true,llvm::GlobalValue::InternalLinkage,tiInit,toChars(),gIR->module);
316
317 llvmValue = gvar;
318
319 /*
320 dtxoff(pdt, Type::typeinfotypedef->toVtblSymbol(), 0, TYnptr); // vtbl for TypeInfo_Typedef
321 dtdword(pdt, 0); // monitor
322
323 assert(tinfo->ty == Ttypedef);
324
325 TypeTypedef *tc = (TypeTypedef *)tinfo;
326 TypedefDeclaration *sd = tc->sym;
327 //printf("basetype = %s\n", sd->basetype->toChars());
328
329 // Put out:
330 // TypeInfo base;
331 // char[] name;
332 // void[] m_init;
333
334 sd->basetype = sd->basetype->merge();
335 sd->basetype->getTypeInfo(NULL); // generate vtinfo
336 assert(sd->basetype->vtinfo);
337 dtxoff(pdt, sd->basetype->vtinfo->toSymbol(), 0, TYnptr); // TypeInfo for basetype
338
339 char *name = sd->toPrettyChars();
340 size_t namelen = strlen(name);
341 dtdword(pdt, namelen);
342 dtabytes(pdt, TYnptr, 0, namelen + 1, name);
343
344 // void[] init;
345 if (tinfo->isZeroInit() || !sd->init)
346 { // 0 initializer, or the same as the base type
347 dtdword(pdt, 0); // init.length
348 dtdword(pdt, 0); // init.ptr
349 }
350 else
351 {
352 dtdword(pdt, sd->type->size()); // init.length
353 dtxoff(pdt, sd->toInitializer(), 0, TYnptr); // init.ptr
354 */
261 } 355 }
262 356
263 void TypeInfoEnumDeclaration::toDt(dt_t **pdt) 357 void TypeInfoEnumDeclaration::toDt(dt_t **pdt)
264 { 358 {
265 assert(0 && "TypeInfoEnumDeclaration"); 359 assert(0 && "TypeInfoEnumDeclaration");