Mercurial > projects > ldc
comparison gen/structs.cpp @ 244:a95056b3c996 trunk
[svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
Did a lot of smaller cleans up here and there.
Replaced more llvm::Foo with LLFoo for common stuff.
Split up tollvm.cpp.
author | lindquist |
---|---|
date | Mon, 09 Jun 2008 09:37:08 +0200 |
parents | 0806379a5eca |
children | 665b81613475 |
comparison
equal
deleted
inserted
replaced
243:4d006f7b2ada | 244:a95056b3c996 |
---|---|
7 #include "init.h" | 7 #include "init.h" |
8 #include "declaration.h" | 8 #include "declaration.h" |
9 | 9 |
10 #include "gen/irstate.h" | 10 #include "gen/irstate.h" |
11 #include "gen/tollvm.h" | 11 #include "gen/tollvm.h" |
12 #include "gen/llvmhelpers.h" | |
12 #include "gen/arrays.h" | 13 #include "gen/arrays.h" |
13 #include "gen/logger.h" | 14 #include "gen/logger.h" |
14 #include "gen/structs.h" | 15 #include "gen/structs.h" |
15 | 16 |
16 #include "ir/irstruct.h" | 17 #include "ir/irstruct.h" |
17 | |
18 ////////////////////////////////////////////////////////////////////////////////////////// | |
19 | |
20 const LLType* DtoStructType(Type* t) | |
21 { | |
22 assert(0); | |
23 std::vector<const LLType*> types; | |
24 return llvm::StructType::get(types); | |
25 } | |
26 | |
27 ////////////////////////////////////////////////////////////////////////////////////////// | |
28 | |
29 LLValue* DtoStructZeroInit(LLValue* v) | |
30 { | |
31 assert(gIR); | |
32 uint64_t n = getTypeStoreSize(v->getType()->getContainedType(0)); | |
33 //LLType* sarrty = getPtrToType(llvm::ArrayType::get(llvm::Type::Int8Ty, n)); | |
34 const LLType* sarrty = getPtrToType(llvm::Type::Int8Ty); | |
35 | |
36 LLValue* sarr = DtoBitCast(v, sarrty); | |
37 | |
38 llvm::Function* fn = LLVM_DeclareMemSet32(); | |
39 assert(fn); | |
40 std::vector<LLValue*> llargs; | |
41 llargs.resize(4); | |
42 llargs[0] = sarr; | |
43 llargs[1] = llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false); | |
44 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false); | |
45 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
46 | |
47 LLValue* ret = llvm::CallInst::Create(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); | |
48 | |
49 return ret; | |
50 } | |
51 | |
52 ////////////////////////////////////////////////////////////////////////////////////////// | |
53 | |
54 LLValue* DtoStructCopy(LLValue* dst, LLValue* src) | |
55 { | |
56 Logger::cout() << "dst = " << *dst << " src = " << *src << '\n'; | |
57 assert(dst->getType() == src->getType()); | |
58 assert(gIR); | |
59 | |
60 uint64_t n = getTypeStoreSize(dst->getType()->getContainedType(0)); | |
61 //LLType* sarrty = getPtrToType(llvm::ArrayType::get(llvm::Type::Int8Ty, n)); | |
62 const LLType* arrty = getPtrToType(llvm::Type::Int8Ty); | |
63 | |
64 LLValue* dstarr = DtoBitCast(dst,arrty); | |
65 LLValue* srcarr = DtoBitCast(src,arrty); | |
66 | |
67 llvm::Function* fn = LLVM_DeclareMemCpy32(); | |
68 std::vector<LLValue*> llargs; | |
69 llargs.resize(4); | |
70 llargs[0] = dstarr; | |
71 llargs[1] = srcarr; | |
72 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false); | |
73 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
74 | |
75 return llvm::CallInst::Create(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); | |
76 } | |
77 | 18 |
78 ////////////////////////////////////////////////////////////////////////////////////////// | 19 ////////////////////////////////////////////////////////////////////////////////////////// |
79 LLConstant* DtoConstStructInitializer(StructInitializer* si) | 20 LLConstant* DtoConstStructInitializer(StructInitializer* si) |
80 { | 21 { |
81 Logger::println("DtoConstStructInitializer: %s", si->toChars()); | 22 Logger::println("DtoConstStructInitializer: %s", si->toChars()); |
225 std::vector<const LLType*> fieldtypes; | 166 std::vector<const LLType*> fieldtypes; |
226 | 167 |
227 if (irstruct->offsets.empty()) | 168 if (irstruct->offsets.empty()) |
228 { | 169 { |
229 Logger::println("has no fields"); | 170 Logger::println("has no fields"); |
230 fieldtypes.push_back(llvm::Type::Int8Ty); | 171 fieldtypes.push_back(LLType::Int8Ty); |
231 structtype = llvm::StructType::get(fieldtypes); | 172 structtype = llvm::StructType::get(fieldtypes); |
232 } | 173 } |
233 else | 174 else |
234 { | 175 { |
235 Logger::println("has fields"); | 176 Logger::println("has fields"); |
271 else { | 212 else { |
272 // commit the field | 213 // commit the field |
273 fieldtypes.push_back(fieldtype); | 214 fieldtypes.push_back(fieldtype); |
274 irstruct->defaultFields.push_back(fieldinit); | 215 irstruct->defaultFields.push_back(fieldinit); |
275 if (fieldpad) { | 216 if (fieldpad) { |
276 fieldtypes.push_back(llvm::ArrayType::get(llvm::Type::Int8Ty, fieldpad)); | 217 fieldtypes.push_back(llvm::ArrayType::get(LLType::Int8Ty, fieldpad)); |
277 irstruct->defaultFields.push_back(NULL); | 218 irstruct->defaultFields.push_back(NULL); |
278 idx++; | 219 idx++; |
279 } | 220 } |
280 | 221 |
281 idx++; | 222 idx++; |
290 } | 231 } |
291 } | 232 } |
292 fieldtypes.push_back(fieldtype); | 233 fieldtypes.push_back(fieldtype); |
293 irstruct->defaultFields.push_back(fieldinit); | 234 irstruct->defaultFields.push_back(fieldinit); |
294 if (fieldpad) { | 235 if (fieldpad) { |
295 fieldtypes.push_back(llvm::ArrayType::get(llvm::Type::Int8Ty, fieldpad)); | 236 fieldtypes.push_back(llvm::ArrayType::get(LLType::Int8Ty, fieldpad)); |
296 irstruct->defaultFields.push_back(NULL); | 237 irstruct->defaultFields.push_back(NULL); |
297 } | 238 } |
298 | 239 |
299 Logger::println("creating struct type"); | 240 Logger::println("creating struct type"); |
300 structtype = llvm::StructType::get(fieldtypes); | 241 structtype = llvm::StructType::get(fieldtypes); |
378 c = irstruct->defaultFields[i]->ir.irField->constInit; | 319 c = irstruct->defaultFields[i]->ir.irField->constInit; |
379 assert(c); | 320 assert(c); |
380 } | 321 } |
381 else { | 322 else { |
382 const llvm::ArrayType* arrty = isaArray(structtype->getElementType(i)); | 323 const llvm::ArrayType* arrty = isaArray(structtype->getElementType(i)); |
383 std::vector<LLConstant*> vals(arrty->getNumElements(), llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false)); | 324 std::vector<LLConstant*> vals(arrty->getNumElements(), llvm::ConstantInt::get(LLType::Int8Ty, 0, false)); |
384 c = llvm::ConstantArray::get(arrty, vals); | 325 c = llvm::ConstantArray::get(arrty, vals); |
385 } | 326 } |
386 fieldinits_ll.push_back(c); | 327 fieldinits_ll.push_back(c); |
387 } | 328 } |
388 | 329 |
390 sd->ir.irStruct->dunion = new DUnion; // uses gIR->topstruct() | 331 sd->ir.irStruct->dunion = new DUnion; // uses gIR->topstruct() |
391 | 332 |
392 // always generate the constant initalizer | 333 // always generate the constant initalizer |
393 if (!sd->zeroInit) { | 334 if (!sd->zeroInit) { |
394 Logger::println("Not zero initialized"); | 335 Logger::println("Not zero initialized"); |
336 #if 0 | |
395 //assert(tk == gIR->gIR->topstruct()().size()); | 337 //assert(tk == gIR->gIR->topstruct()().size()); |
396 #ifndef LLVMD_NO_LOGGER | 338 #ifndef LLVMD_NO_LOGGER |
397 Logger::cout() << "struct type: " << *structtype << '\n'; | 339 Logger::cout() << "struct type: " << *structtype << '\n'; |
398 for (size_t k=0; k<fieldinits_ll.size(); ++k) { | 340 for (size_t k=0; k<fieldinits_ll.size(); ++k) { |
399 Logger::cout() << "Type:" << '\n'; | 341 Logger::cout() << "Type:" << '\n'; |
401 Logger::cout() << "Value:" << '\n'; | 343 Logger::cout() << "Value:" << '\n'; |
402 Logger::cout() << *fieldinits_ll[k] << '\n'; | 344 Logger::cout() << *fieldinits_ll[k] << '\n'; |
403 } | 345 } |
404 Logger::cout() << "Initializer printed" << '\n'; | 346 Logger::cout() << "Initializer printed" << '\n'; |
405 #endif | 347 #endif |
348 #endif | |
406 sd->ir.irStruct->constInit = llvm::ConstantStruct::get(structtype,fieldinits_ll); | 349 sd->ir.irStruct->constInit = llvm::ConstantStruct::get(structtype,fieldinits_ll); |
407 } | 350 } |
408 else { | 351 else { |
409 Logger::println("Zero initialized"); | 352 Logger::println("Zero initialized"); |
410 sd->ir.irStruct->constInit = llvm::ConstantAggregateZero::get(structtype); | 353 sd->ir.irStruct->constInit = llvm::ConstantAggregateZero::get(structtype); |
496 } | 439 } |
497 | 440 |
498 static void push_nulls(size_t nbytes, std::vector<LLConstant*>& out) | 441 static void push_nulls(size_t nbytes, std::vector<LLConstant*>& out) |
499 { | 442 { |
500 assert(nbytes > 0); | 443 assert(nbytes > 0); |
501 std::vector<LLConstant*> i(nbytes, llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false)); | 444 std::vector<LLConstant*> i(nbytes, llvm::ConstantInt::get(LLType::Int8Ty, 0, false)); |
502 out.push_back(llvm::ConstantArray::get(llvm::ArrayType::get(llvm::Type::Int8Ty, nbytes), i)); | 445 out.push_back(llvm::ConstantArray::get(llvm::ArrayType::get(LLType::Int8Ty, nbytes), i)); |
503 } | 446 } |
504 | 447 |
505 LLConstant* DUnion::getConst(std::vector<DUnionIdx>& in) | 448 LLConstant* DUnion::getConst(std::vector<DUnionIdx>& in) |
506 { | 449 { |
507 std::sort(in.begin(), in.end()); | 450 std::sort(in.begin(), in.end()); |