comparison gen/structs.cpp @ 102:027b8d8b71ec trunk

[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up. Basically it tries to do the following in order: Resolve types, Declare symbols, Create constant initializers, Apply initializers, Generate functions bodies. ClassInfo is now has the most useful(biased?) members working. Probably other stuf...
author lindquist
date Sun, 18 Nov 2007 06:52:57 +0100
parents 5071469303d4
children 27b9f749d9fe
comparison
equal deleted inserted replaced
101:169fda3a77d4 102:027b8d8b71ec
94 assert(vd); 94 assert(vd);
95 llvm::Constant* v = DtoConstInitializer(vd->type, ini); 95 llvm::Constant* v = DtoConstInitializer(vd->type, ini);
96 inits.push_back(DUnionIdx(vd->llvmFieldIndex, vd->llvmFieldIndexOffset, v)); 96 inits.push_back(DUnionIdx(vd->llvmFieldIndex, vd->llvmFieldIndexOffset, v));
97 } 97 }
98 98
99 DtoConstInitStruct((StructDeclaration*)si->ad);
99 return si->ad->llvmUnion->getConst(inits); 100 return si->ad->llvmUnion->getConst(inits);
100 } 101 }
101 102
102 ////////////////////////////////////////////////////////////////////////////////////////// 103 //////////////////////////////////////////////////////////////////////////////////////////
103 104
163 return new llvm::GetElementPtrInst(ptr, DtoConstUint(os / llt_sz), "tmp", gIR->scopebb()); 164 return new llvm::GetElementPtrInst(ptr, DtoConstUint(os / llt_sz), "tmp", gIR->scopebb());
164 } 165 }
165 166
166 ////////////////////////////////////////////////////////////////////////////////////////// 167 //////////////////////////////////////////////////////////////////////////////////////////
167 168
168 void DtoDeclareStruct(StructDeclaration* sd) 169 void DtoResolveStruct(StructDeclaration* sd)
169 { 170 {
170 if (sd->llvmTouched) return; 171 if (sd->llvmResolved) return;
171 sd->llvmTouched = true; 172 sd->llvmResolved = true;
172 173
173 Logger::println("DtoDeclareStruct(%s)", sd->toChars()); 174 Logger::println("DtoResolveStruct(%s)", sd->toChars());
174 LOG_SCOPE; 175 LOG_SCOPE;
175 176
176 TypeStruct* ts = (TypeStruct*)DtoDType(sd->type); 177 TypeStruct* ts = (TypeStruct*)DtoDType(sd->type);
177 178
178 IRStruct* irstruct = new IRStruct(ts); 179 IRStruct* irstruct = new IRStruct(ts);
278 279
279 if (sd->parent->isModule()) { 280 if (sd->parent->isModule()) {
280 gIR->module->addTypeName(sd->mangle(),structtype); 281 gIR->module->addTypeName(sd->mangle(),structtype);
281 } 282 }
282 283
284 gIR->structs.pop_back();
285
286 gIR->declareList.push_back(sd);
287 }
288
289 //////////////////////////////////////////////////////////////////////////////////////////
290
291 void DtoDeclareStruct(StructDeclaration* sd)
292 {
293 if (sd->llvmDeclared) return;
294 sd->llvmDeclared = true;
295
296 Logger::println("DtoDeclareStruct(%s)", sd->toChars());
297 LOG_SCOPE;
298
299 TypeStruct* ts = (TypeStruct*)DtoDType(sd->type);
300
283 std::string initname("_D"); 301 std::string initname("_D");
284 initname.append(sd->mangle()); 302 initname.append(sd->mangle());
285 initname.append("6__initZ"); 303 initname.append("6__initZ");
286 304
287 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; 305 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage;
288 llvm::GlobalVariable* initvar = new llvm::GlobalVariable(ts->llvmType->get(), true, _linkage, NULL, initname, gIR->module); 306 llvm::GlobalVariable* initvar = new llvm::GlobalVariable(ts->llvmType->get(), true, _linkage, NULL, initname, gIR->module);
289 ts->llvmInit = initvar; 307 ts->llvmInit = initvar;
290 308
291 gIR->structs.pop_back(); 309 gIR->constInitList.push_back(sd);
292
293 gIR->constInitQueue.push_back(sd);
294 if (sd->getModule() == gIR->dmodule) 310 if (sd->getModule() == gIR->dmodule)
295 gIR->defineQueue.push_back(sd); 311 gIR->defineList.push_back(sd);
296
297 // declare typeinfo
298 if (sd->getModule() == gIR->dmodule && sd->llvmInternal != LLVMnotypeinfo)
299 sd->type->getTypeInfo(NULL);
300 } 312 }
301 313
302 ////////////////////////////////////////////////////////////////////////////////////////// 314 //////////////////////////////////////////////////////////////////////////////////////////
303 315
304 void DtoConstInitStruct(StructDeclaration* sd) 316 void DtoConstInitStruct(StructDeclaration* sd)
305 { 317 {
306 IRStruct* irstruct = sd->llvmIRStruct; 318 if (sd->llvmInitialized) return;
307 if (irstruct->constinited) return; 319 sd->llvmInitialized = true;
308 irstruct->constinited = true;
309 320
310 Logger::println("DtoConstInitStruct(%s)", sd->toChars()); 321 Logger::println("DtoConstInitStruct(%s)", sd->toChars());
311 LOG_SCOPE; 322 LOG_SCOPE;
312 323
324 IRStruct* irstruct = sd->llvmIRStruct;
313 gIR->structs.push_back(irstruct); 325 gIR->structs.push_back(irstruct);
314 326
315 // make sure each offset knows its default initializer 327 // make sure each offset knows its default initializer
316 for (IRStruct::OffsetMap::iterator i=irstruct->offsets.begin(); i!=irstruct->offsets.end(); ++i) 328 for (IRStruct::OffsetMap::iterator i=irstruct->offsets.begin(); i!=irstruct->offsets.end(); ++i)
317 { 329 {
363 Logger::println("Zero initialized"); 375 Logger::println("Zero initialized");
364 sd->llvmInitZ = llvm::ConstantAggregateZero::get(structtype); 376 sd->llvmInitZ = llvm::ConstantAggregateZero::get(structtype);
365 } 377 }
366 378
367 gIR->structs.pop_back(); 379 gIR->structs.pop_back();
380
381 // emit typeinfo
382 if (sd->getModule() == gIR->dmodule && sd->llvmInternal != LLVMnotypeinfo)
383 sd->type->getTypeInfo(NULL);
368 } 384 }
369 385
370 ////////////////////////////////////////////////////////////////////////////////////////// 386 //////////////////////////////////////////////////////////////////////////////////////////
371 387
372 void DtoDefineStruct(StructDeclaration* sd) 388 void DtoDefineStruct(StructDeclaration* sd)
373 { 389 {
374 IRStruct* irstruct = sd->llvmIRStruct; 390 if (sd->llvmDefined) return;
375 if (irstruct->defined) return; 391 sd->llvmDefined = true;
376 irstruct->defined = true;
377
378 DtoConstInitStruct(sd);
379 392
380 Logger::println("DtoDefineStruct(%s)", sd->toChars()); 393 Logger::println("DtoDefineStruct(%s)", sd->toChars());
381 LOG_SCOPE; 394 LOG_SCOPE;
382 395
383 assert(sd->type->ty == Tstruct); 396 assert(sd->type->ty == Tstruct);