Mercurial > projects > ldc
comparison gen/structs.cpp @ 137:ce7b81fb957f trunk
[svn r141] fixed more problems with classinfo
moved more IR state out of the AST classes
author | lindquist |
---|---|
date | Fri, 18 Jan 2008 16:42:16 +0100 |
parents | 0e28624814e8 |
children | 4c577c2b7229 |
comparison
equal
deleted
inserted
replaced
136:0e28624814e8 | 137:ce7b81fb957f |
---|---|
97 llvm::Constant* v = DtoConstInitializer(vd->type, ini); | 97 llvm::Constant* v = DtoConstInitializer(vd->type, ini); |
98 inits.push_back(DUnionIdx(vd->irField->index, vd->irField->indexOffset, v)); | 98 inits.push_back(DUnionIdx(vd->irField->index, vd->irField->indexOffset, v)); |
99 } | 99 } |
100 | 100 |
101 DtoConstInitStruct((StructDeclaration*)si->ad); | 101 DtoConstInitStruct((StructDeclaration*)si->ad); |
102 return si->ad->llvmUnion->getConst(inits); | 102 return si->ad->irStruct->dunion->getConst(inits); |
103 } | 103 } |
104 | 104 |
105 ////////////////////////////////////////////////////////////////////////////////////////// | 105 ////////////////////////////////////////////////////////////////////////////////////////// |
106 | 106 |
107 llvm::Value* DtoIndexStruct(llvm::Value* ptr, StructDeclaration* sd, Type* t, unsigned os, std::vector<unsigned>& idxs) | 107 llvm::Value* DtoIndexStruct(llvm::Value* ptr, StructDeclaration* sd, Type* t, unsigned os, std::vector<unsigned>& idxs) |
113 idxs.push_back(0); | 113 idxs.push_back(0); |
114 | 114 |
115 const llvm::Type* llt = getPtrToType(DtoType(t)); | 115 const llvm::Type* llt = getPtrToType(DtoType(t)); |
116 const llvm::Type* st = getPtrToType(DtoType(sd->type)); | 116 const llvm::Type* st = getPtrToType(DtoType(sd->type)); |
117 if (ptr->getType() != st) { | 117 if (ptr->getType() != st) { |
118 assert(sd->llvmHasUnions); | 118 assert(sd->irStruct->hasUnions); |
119 ptr = gIR->ir->CreateBitCast(ptr, st, "tmp"); | 119 ptr = gIR->ir->CreateBitCast(ptr, st, "tmp"); |
120 } | 120 } |
121 | 121 |
122 for (unsigned i=0; i<sd->fields.dim; ++i) { | 122 for (unsigned i=0; i<sd->fields.dim; ++i) { |
123 VarDeclaration* vd = (VarDeclaration*)sd->fields.data[i]; | 123 VarDeclaration* vd = (VarDeclaration*)sd->fields.data[i]; |
177 LOG_SCOPE; | 177 LOG_SCOPE; |
178 | 178 |
179 TypeStruct* ts = (TypeStruct*)DtoDType(sd->type); | 179 TypeStruct* ts = (TypeStruct*)DtoDType(sd->type); |
180 | 180 |
181 IrStruct* irstruct = new IrStruct(ts); | 181 IrStruct* irstruct = new IrStruct(ts); |
182 sd->llvmIrStruct = irstruct; | 182 sd->irStruct = irstruct; |
183 gIR->structs.push_back(irstruct); | 183 gIR->structs.push_back(irstruct); |
184 | 184 |
185 // fields | 185 // fields |
186 Array* arr = &sd->fields; | 186 Array* arr = &sd->fields; |
187 for (int k=0; k < arr->dim; k++) { | 187 for (int k=0; k < arr->dim; k++) { |
250 size_t s = getABITypeSize(i->second.type); | 250 size_t s = getABITypeSize(i->second.type); |
251 if (s > prevsize) { | 251 if (s > prevsize) { |
252 fieldpad += s - prevsize; | 252 fieldpad += s - prevsize; |
253 prevsize = s; | 253 prevsize = s; |
254 } | 254 } |
255 sd->llvmHasUnions = true; | 255 sd->irStruct->hasUnions = true; |
256 i->second.var->irField->index = idx; | 256 i->second.var->irField->index = idx; |
257 } | 257 } |
258 // intersecting offset? | 258 // intersecting offset? |
259 else if (i->first < (lastoffset + prevsize)) { | 259 else if (i->first < (lastoffset + prevsize)) { |
260 size_t s = getABITypeSize(i->second.type); | 260 size_t s = getABITypeSize(i->second.type); |
261 assert((i->first + s) <= (lastoffset + prevsize)); // this holds because all types are aligned to their size | 261 assert((i->first + s) <= (lastoffset + prevsize)); // this holds because all types are aligned to their size |
262 sd->llvmHasUnions = true; | 262 sd->irStruct->hasUnions = true; |
263 i->second.var->irField->index = idx; | 263 i->second.var->irField->index = idx; |
264 i->second.var->irField->indexOffset = (i->first - lastoffset) / s; | 264 i->second.var->irField->indexOffset = (i->first - lastoffset) / s; |
265 } | 265 } |
266 // fresh offset | 266 // fresh offset |
267 else { | 267 else { |
332 initname.append(sd->mangle()); | 332 initname.append(sd->mangle()); |
333 initname.append("6__initZ"); | 333 initname.append("6__initZ"); |
334 | 334 |
335 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; | 335 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; |
336 llvm::GlobalVariable* initvar = new llvm::GlobalVariable(ts->llvmType->get(), true, _linkage, NULL, initname, gIR->module); | 336 llvm::GlobalVariable* initvar = new llvm::GlobalVariable(ts->llvmType->get(), true, _linkage, NULL, initname, gIR->module); |
337 sd->llvmInit = initvar; | 337 sd->irStruct->init = initvar; |
338 | 338 |
339 gIR->constInitList.push_back(sd); | 339 gIR->constInitList.push_back(sd); |
340 if (sd->getModule() == gIR->dmodule) | 340 if (sd->getModule() == gIR->dmodule) |
341 gIR->defineList.push_back(sd); | 341 gIR->defineList.push_back(sd); |
342 } | 342 } |
349 sd->llvmInitialized = true; | 349 sd->llvmInitialized = true; |
350 | 350 |
351 Logger::println("DtoConstInitStruct(%s): %s", sd->toChars(), sd->loc.toChars()); | 351 Logger::println("DtoConstInitStruct(%s): %s", sd->toChars(), sd->loc.toChars()); |
352 LOG_SCOPE; | 352 LOG_SCOPE; |
353 | 353 |
354 IrStruct* irstruct = sd->llvmIrStruct; | 354 IrStruct* irstruct = sd->irStruct; |
355 gIR->structs.push_back(irstruct); | 355 gIR->structs.push_back(irstruct); |
356 | 356 |
357 // make sure each offset knows its default initializer | 357 // make sure each offset knows its default initializer |
358 for (IrStruct::OffsetMap::iterator i=irstruct->offsets.begin(); i!=irstruct->offsets.end(); ++i) | 358 for (IrStruct::OffsetMap::iterator i=irstruct->offsets.begin(); i!=irstruct->offsets.end(); ++i) |
359 { | 359 { |
381 } | 381 } |
382 fieldinits_ll.push_back(c); | 382 fieldinits_ll.push_back(c); |
383 } | 383 } |
384 | 384 |
385 // generate the union mapper | 385 // generate the union mapper |
386 sd->llvmUnion = new DUnion; // uses gIR->topstruct() | 386 sd->irStruct->dunion = new DUnion; // uses gIR->topstruct() |
387 | 387 |
388 // always generate the constant initalizer | 388 // always generate the constant initalizer |
389 if (!sd->zeroInit) { | 389 if (!sd->zeroInit) { |
390 Logger::println("Not zero initialized"); | 390 Logger::println("Not zero initialized"); |
391 //assert(tk == gIR->gIR->topstruct()().size()); | 391 //assert(tk == gIR->gIR->topstruct()().size()); |
397 Logger::cout() << "Value:" << '\n'; | 397 Logger::cout() << "Value:" << '\n'; |
398 Logger::cout() << *fieldinits_ll[k] << '\n'; | 398 Logger::cout() << *fieldinits_ll[k] << '\n'; |
399 } | 399 } |
400 Logger::cout() << "Initializer printed" << '\n'; | 400 Logger::cout() << "Initializer printed" << '\n'; |
401 #endif | 401 #endif |
402 sd->llvmConstInit = llvm::ConstantStruct::get(structtype,fieldinits_ll); | 402 sd->irStruct->constInit = llvm::ConstantStruct::get(structtype,fieldinits_ll); |
403 } | 403 } |
404 else { | 404 else { |
405 Logger::println("Zero initialized"); | 405 Logger::println("Zero initialized"); |
406 sd->llvmConstInit = llvm::ConstantAggregateZero::get(structtype); | 406 sd->irStruct->constInit = llvm::ConstantAggregateZero::get(structtype); |
407 } | 407 } |
408 | 408 |
409 gIR->structs.pop_back(); | 409 gIR->structs.pop_back(); |
410 | 410 |
411 // emit typeinfo | 411 // emit typeinfo |
423 Logger::println("DtoDefineStruct(%s): %s", sd->toChars(), sd->loc.toChars()); | 423 Logger::println("DtoDefineStruct(%s): %s", sd->toChars(), sd->loc.toChars()); |
424 LOG_SCOPE; | 424 LOG_SCOPE; |
425 | 425 |
426 assert(sd->type->ty == Tstruct); | 426 assert(sd->type->ty == Tstruct); |
427 TypeStruct* ts = (TypeStruct*)sd->type; | 427 TypeStruct* ts = (TypeStruct*)sd->type; |
428 sd->llvmInit->setInitializer(sd->llvmConstInit); | 428 sd->irStruct->init->setInitializer(sd->irStruct->constInit); |
429 | 429 |
430 sd->llvmDModule = gIR->dmodule; | 430 sd->llvmDModule = gIR->dmodule; |
431 } | 431 } |
432 | 432 |
433 ////////////////////////////////////////////////////////////////////////////////////////// | 433 ////////////////////////////////////////////////////////////////////////////////////////// |