comparison gen/toobj.cpp @ 1386:402bffb1b356

Use RTTIBuilder to build !ModuleInfo, still needs some work.
author Tomas Lindquist Olsen <tomas.l.olsen gmail com>
date Sun, 17 May 2009 17:04:47 +0200
parents 98d435fa2a2d
children 83bdf7bed9c5 f35a9a77d256
comparison
equal deleted inserted replaced
1385:1609490c123f 1386:402bffb1b356
34 #include "id.h" 34 #include "id.h"
35 #include "import.h" 35 #include "import.h"
36 #include "template.h" 36 #include "template.h"
37 #include "scope.h" 37 #include "scope.h"
38 38
39 #include "gen/abi.h"
40 #include "gen/arrays.h"
41 #include "gen/classes.h"
42 #include "gen/cl_options.h"
43 #include "gen/functions.h"
39 #include "gen/irstate.h" 44 #include "gen/irstate.h"
45 #include "gen/llvmhelpers.h"
46 #include "gen/llvm-version.h"
40 #include "gen/logger.h" 47 #include "gen/logger.h"
48 #include "gen/optimizer.h"
49 #include "gen/programs.h"
50 #include "gen/rttibuilder.h"
51 #include "gen/runtime.h"
52 #include "gen/structs.h"
53 #include "gen/todebug.h"
41 #include "gen/tollvm.h" 54 #include "gen/tollvm.h"
42 #include "gen/llvmhelpers.h"
43 #include "gen/arrays.h"
44 #include "gen/structs.h"
45 #include "gen/classes.h"
46 #include "gen/functions.h"
47 #include "gen/todebug.h"
48 #include "gen/runtime.h"
49 #include "gen/abi.h"
50 #include "gen/cl_options.h"
51 #include "gen/optimizer.h"
52 #include "gen/llvm-version.h"
53 #include "gen/programs.h"
54 55
55 #include "ir/irvar.h" 56 #include "ir/irvar.h"
56 #include "ir/irmodule.h" 57 #include "ir/irmodule.h"
57 #include "ir/irtype.h" 58 #include "ir/irtype.h"
58 59
616 if (!moduleinfo) 617 if (!moduleinfo)
617 { 618 {
618 error("object.d is missing the ModuleInfo class"); 619 error("object.d is missing the ModuleInfo class");
619 fatal(); 620 fatal();
620 } 621 }
621
622 moduleinfo->codegen(Type::sir);
623
624 // check for patch 622 // check for patch
625 if (moduleinfo->fields.dim != 9) 623 else if (moduleinfo->fields.dim != 9)
626 { 624 {
627 error("object.d ModuleInfo class is incorrect"); 625 error("object.d ModuleInfo class is incorrect");
628 fatal(); 626 fatal();
629 } 627 }
630 628
631 // moduleinfo llvm struct type 629 // use the RTTIBuilder
632 const llvm::StructType* moduleinfoTy = isaStruct(moduleinfo->type->irtype->getPA()); 630 RTTIBuilder b(moduleinfo);
633 // classinfo llvm struct type 631
634 const llvm::StructType* classinfoTy = isaStruct(ClassDeclaration::classinfo->type->irtype->getPA()); 632 // some types
635 633 const LLType* moduleinfoTy = moduleinfo->type->irtype->getPA();
636 // initializer vector 634 const LLType* classinfoTy = ClassDeclaration::classinfo->type->irtype->getPA();
637 std::vector<LLConstant*> initVec;
638 LLConstant* c = 0;
639
640 // vtable
641 c = moduleinfo->ir.irStruct->getVtblSymbol();
642 initVec.push_back(c);
643
644 // monitor
645 c = getNullPtr(getPtrToType(LLType::Int8Ty));
646 initVec.push_back(c);
647 635
648 // name 636 // name
649 char *name = toPrettyChars(); 637 b.push_string(toPrettyChars());
650 c = DtoConstString(name);
651 initVec.push_back(c);
652 638
653 // importedModules[] 639 // importedModules[]
654 int aimports_dim = aimports.dim; 640 int aimports_dim = aimports.dim;
655 std::vector<LLConstant*> importInits; 641 std::vector<LLConstant*> importInits;
642 LLConstant* c = 0;
656 for (size_t i = 0; i < aimports.dim; i++) 643 for (size_t i = 0; i < aimports.dim; i++)
657 { 644 {
658 Module *m = (Module *)aimports.data[i]; 645 Module *m = (Module *)aimports.data[i];
659 if (!m->needModuleInfo() || m == this) 646 if (!m->needModuleInfo() || m == this)
660 continue; 647 continue;
679 if (!m_gvar) m_gvar = new llvm::GlobalVariable(importArrTy, true, llvm::GlobalValue::InternalLinkage, c, m_name, gIR->module); 666 if (!m_gvar) m_gvar = new llvm::GlobalVariable(importArrTy, true, llvm::GlobalValue::InternalLinkage, c, m_name, gIR->module);
680 c = llvm::ConstantExpr::getBitCast(m_gvar, getPtrToType(importArrTy->getElementType())); 667 c = llvm::ConstantExpr::getBitCast(m_gvar, getPtrToType(importArrTy->getElementType()));
681 c = DtoConstSlice(DtoConstSize_t(importInits.size()), c); 668 c = DtoConstSlice(DtoConstSize_t(importInits.size()), c);
682 } 669 }
683 else 670 else
671 {
684 c = DtoConstSlice( DtoConstSize_t(0), getNullValue(getPtrToType(moduleinfoTy)) ); 672 c = DtoConstSlice( DtoConstSize_t(0), getNullValue(getPtrToType(moduleinfoTy)) );
685 initVec.push_back(c); 673 }
674 b.push(c);
686 675
687 // localClasses[] 676 // localClasses[]
688 ClassDeclarations aclasses; 677 ClassDeclarations aclasses;
689 //printf("members->dim = %d\n", members->dim); 678 //printf("members->dim = %d\n", members->dim);
690 for (size_t i = 0; i < members->dim; i++) 679 for (size_t i = 0; i < members->dim; i++)
729 c = DtoBitCast(m_gvar, getPtrToType(classinfoTy)); 718 c = DtoBitCast(m_gvar, getPtrToType(classinfoTy));
730 c = DtoConstSlice(DtoConstSize_t(classInits.size()), c); 719 c = DtoConstSlice(DtoConstSize_t(classInits.size()), c);
731 } 720 }
732 else 721 else
733 c = DtoConstSlice( DtoConstSize_t(0), getNullValue(getPtrToType(classinfoTy)) ); 722 c = DtoConstSlice( DtoConstSize_t(0), getNullValue(getPtrToType(classinfoTy)) );
734 initVec.push_back(c); 723 b.push(c);
735 724
736 // flags 725 // flags (4 means MIstandalone)
737 c = DtoConstUint(0); 726 unsigned mi_flags = needmoduleinfo ? 0 : 4;
738 if (!needmoduleinfo) 727 b.push_uint(mi_flags);
739 c = DtoConstUint(4); // flags (4 means MIstandalone)
740 initVec.push_back(c);
741 728
742 // function pointer type for next three fields 729 // function pointer type for next three fields
743 const LLType* fnptrTy = getPtrToType(LLFunctionType::get(LLType::VoidTy, std::vector<const LLType*>(), false)); 730 const LLType* fnptrTy = getPtrToType(LLFunctionType::get(LLType::VoidTy, std::vector<const LLType*>(), false));
744 731
745 // ctor 732 // ctor
746 llvm::Function* fctor = build_module_ctor(); 733 llvm::Function* fctor = build_module_ctor();
747 c = fctor ? fctor : getNullValue(fnptrTy); 734 c = fctor ? fctor : getNullValue(fnptrTy);
748 initVec.push_back(c); 735 b.push(c);
749 736
750 // dtor 737 // dtor
751 llvm::Function* fdtor = build_module_dtor(); 738 llvm::Function* fdtor = build_module_dtor();
752 c = fdtor ? fdtor : getNullValue(fnptrTy); 739 c = fdtor ? fdtor : getNullValue(fnptrTy);
753 initVec.push_back(c); 740 b.push(c);
754 741
755 // unitTest 742 // unitTest
756 llvm::Function* unittest = build_module_unittest(); 743 llvm::Function* unittest = build_module_unittest();
757 c = unittest ? unittest : getNullValue(fnptrTy); 744 c = unittest ? unittest : getNullValue(fnptrTy);
758 initVec.push_back(c); 745 b.push(c);
759 746
760 // xgetMembers 747 // xgetMembers
761 c = getNullValue(getVoidPtrType()); 748 c = getNullValue(getVoidPtrType());
762 initVec.push_back(c); 749 b.push(c);
763 750
764 // ictor 751 // ictor
765 c = getNullValue(fnptrTy); 752 c = getNullValue(fnptrTy);
766 initVec.push_back(c); 753 b.push(c);
767 754
768 /*Logger::println("MODULE INFO INITIALIZERS"); 755 /*Logger::println("MODULE INFO INITIALIZERS");
769 for (size_t i=0; i<initVec.size(); ++i) 756 for (size_t i=0; i<initVec.size(); ++i)
770 { 757 {
771 Logger::cout() << *initVec[i] << '\n'; 758 Logger::cout() << *initVec[i] << '\n';
772 if (initVec[i]->getType() != moduleinfoTy->getElementType(i)) 759 if (initVec[i]->getType() != moduleinfoTy->getElementType(i))
773 assert(0); 760 assert(0);
774 }*/ 761 }*/
775 762
776 // create initializer 763 // create initializer
777 LLConstant* constMI = llvm::ConstantStruct::get(initVec); 764 LLConstant* constMI = b.get_constant();
778 765
779 // create name 766 // create name
780 std::string MIname("_D"); 767 std::string MIname("_D");
781 MIname.append(mangle()); 768 MIname.append(mangle());
782 MIname.append("8__ModuleZ"); 769 MIname.append("8__ModuleZ");