comparison gen/toobj.cpp @ 96:ce7ed8f59b99 trunk

[svn r100] Moved test/ray.d to demos/ray.d. Cleanups.
author lindquist
date Mon, 12 Nov 2007 07:58:44 +0100
parents 61615fa85940
children 6789050b5ad1
comparison
equal deleted inserted replaced
95:71b8fecdae38 96:ce7ed8f59b99
30 #include "import.h" 30 #include "import.h"
31 #include "template.h" 31 #include "template.h"
32 #include "scope.h" 32 #include "scope.h"
33 33
34 #include "gen/irstate.h" 34 #include "gen/irstate.h"
35 #include "gen/elem.h"
36 #include "gen/logger.h" 35 #include "gen/logger.h"
37 #include "gen/tollvm.h" 36 #include "gen/tollvm.h"
38 #include "gen/arrays.h" 37 #include "gen/arrays.h"
39 #include "gen/structs.h" 38 #include "gen/structs.h"
40 #include "gen/todebug.h" 39 #include "gen/todebug.h"
330 dsym->toObjFile(); 329 dsym->toObjFile();
331 } 330 }
332 331
333 Logger::println("doing struct fields"); 332 Logger::println("doing struct fields");
334 333
335 llvm::StructType* structtype = 0; 334 const llvm::StructType* structtype = 0;
336 std::vector<llvm::Constant*> fieldinits; 335 std::vector<llvm::Constant*> fieldinits;
337 336
338 if (gIR->topstruct().offsets.empty()) 337 if (gIR->topstruct().offsets.empty())
339 { 338 {
340 std::vector<const llvm::Type*> fieldtypes; 339 std::vector<const llvm::Type*> fieldtypes;
425 // refine abstract types for stuff like: struct S{S* next;} 424 // refine abstract types for stuff like: struct S{S* next;}
426 if (gIR->topstruct().recty != 0) 425 if (gIR->topstruct().recty != 0)
427 { 426 {
428 llvm::PATypeHolder& pa = gIR->topstruct().recty; 427 llvm::PATypeHolder& pa = gIR->topstruct().recty;
429 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(structtype); 428 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(structtype);
430 structtype = llvm::cast<llvm::StructType>(pa.get()); 429 structtype = isaStruct(pa.get());
431 } 430 }
432 431
433 ts->llvmType = structtype; 432 ts->llvmType = structtype;
434 llvmType = structtype; 433 llvmType = structtype;
435 434
585 for (IRStruct::OffsetMap::iterator i=gIR->topstruct().offsets.begin(); i!=gIR->topstruct().offsets.end(); ++i) { 584 for (IRStruct::OffsetMap::iterator i=gIR->topstruct().offsets.begin(); i!=gIR->topstruct().offsets.end(); ++i) {
586 fieldtypes.push_back(DtoType(i->second.var->type)); 585 fieldtypes.push_back(DtoType(i->second.var->type));
587 fieldinits.push_back(i->second.init); 586 fieldinits.push_back(i->second.init);
588 } 587 }
589 588
590 llvm::StructType* structtype = llvm::StructType::get(fieldtypes); 589 const llvm::StructType* structtype = llvm::StructType::get(fieldtypes);
591 // refine abstract types for stuff like: class C {C next;} 590 // refine abstract types for stuff like: class C {C next;}
592 if (gIR->topstruct().recty != 0) 591 if (gIR->topstruct().recty != 0)
593 { 592 {
594 llvm::PATypeHolder& pa = gIR->topstruct().recty; 593 llvm::PATypeHolder& pa = gIR->topstruct().recty;
595 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(structtype); 594 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(structtype);
596 structtype = llvm::cast<llvm::StructType>(pa.get()); 595 structtype = isaStruct(pa.get());
597 } 596 }
598 597
599 ts->llvmType = structtype; 598 ts->llvmType = structtype;
600 llvmType = structtype; 599 llvmType = structtype;
601 600
662 661
663 //////////////////////////////////////////////////////////////////////////////// 662 ////////////////////////////////////////////////////////////////////////////////
664 663
665 // refine for final vtable type 664 // refine for final vtable type
666 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(svtbl_ty); 665 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(svtbl_ty);
667 svtbl_ty = llvm::cast<llvm::StructType>(pa.get()); 666 svtbl_ty = isaStruct(pa.get());
668 structtype = llvm::cast<llvm::StructType>(gIR->topstruct().recty.get()); 667 structtype = isaStruct(gIR->topstruct().recty.get());
669 ts->llvmType = structtype; 668 ts->llvmType = structtype;
670 llvmType = structtype; 669 llvmType = structtype;
671 670
672 // generate initializer 671 // generate initializer
673 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; 672 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage;
791 TypeStruct* ts = (TypeStruct*)t; 790 TypeStruct* ts = (TypeStruct*)t;
792 assert(ts->sym->llvmInitZ); 791 assert(ts->sym->llvmInitZ);
793 _init = ts->sym->llvmInitZ; 792 _init = ts->sym->llvmInitZ;
794 } 793 }
795 // array single value init 794 // array single value init
796 else if (llvm::isa<llvm::ArrayType>(_type)) 795 else if (isaArray(_type))
797 { 796 {
798 _init = DtoConstStaticArray(_type, _init); 797 _init = DtoConstStaticArray(_type, _init);
799 } 798 }
800 else { 799 else {
801 Logger::cout() << "Unexpected initializer type: " << *_type << '\n'; 800 Logger::cout() << "Unexpected initializer type: " << *_type << '\n';
831 Logger::cout() << "field init is: " << *_init << " type should be " << *_type << '\n'; 830 Logger::cout() << "field init is: " << *_init << " type should be " << *_type << '\n';
832 if (_type != _init->getType()) 831 if (_type != _init->getType())
833 { 832 {
834 if (t->ty == Tsarray) 833 if (t->ty == Tsarray)
835 { 834 {
836 const llvm::ArrayType* arrty = llvm::cast<llvm::ArrayType>(_type); 835 const llvm::ArrayType* arrty = isaArray(_type);
837 uint64_t n = arrty->getNumElements(); 836 uint64_t n = arrty->getNumElements();
838 std::vector<llvm::Constant*> vals(n,_init); 837 std::vector<llvm::Constant*> vals(n,_init);
839 _init = llvm::ConstantArray::get(arrty, vals); 838 _init = llvm::ConstantArray::get(arrty, vals);
840 } 839 }
841 else if (t->ty == Tarray) 840 else if (t->ty == Tarray)
842 { 841 {
843 assert(llvm::isa<llvm::StructType>(_type)); 842 assert(isaStruct(_type));
844 _init = llvm::ConstantAggregateZero::get(_type); 843 _init = llvm::ConstantAggregateZero::get(_type);
845 } 844 }
846 else if (t->ty == Tstruct) 845 else if (t->ty == Tstruct)
847 { 846 {
848 const llvm::StructType* structty = llvm::cast<llvm::StructType>(_type); 847 const llvm::StructType* structty = isaStruct(_type);
849 TypeStruct* ts = (TypeStruct*)t; 848 TypeStruct* ts = (TypeStruct*)t;
850 assert(ts); 849 assert(ts);
851 assert(ts->sym); 850 assert(ts->sym);
852 assert(ts->sym->llvmInitZ); 851 assert(ts->sym->llvmInitZ);
853 _init = ts->sym->llvmInitZ; 852 _init = ts->sym->llvmInitZ;