diff --git a/lib/Target/PIC16/PIC16AsmPrinter.cpp b/lib/Target/PIC16/PIC16AsmPrinter.cpp index 8c39272e031..3e8ac016edc 100644 --- a/lib/Target/PIC16/PIC16AsmPrinter.cpp +++ b/lib/Target/PIC16/PIC16AsmPrinter.cpp @@ -133,7 +133,7 @@ void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) { // If its a libcall name, record it to decls section. if (PAN::getSymbolTag(Sname) == PAN::LIBCALL) { - Decls.push_back(Sname); + LibcallDecls.push_back(Sname); } O << Sname; @@ -153,16 +153,16 @@ void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) { O << PIC16CondCodeToString((PIC16CC::CondCodes)CC); } -void PIC16AsmPrinter::printDecls(void) { +void PIC16AsmPrinter::printLibcallDecls(void) { // If no libcalls used, return. - if (Decls.empty()) return; + if (LibcallDecls.empty()) return; O << TAI->getCommentString() << "External decls for libcalls - BEGIN." <<"\n"; // Remove duplicate entries. - Decls.sort(); - Decls.unique(); - for (std::list::const_iterator I = Decls.begin(); - I != Decls.end(); I++) { + LibcallDecls.sort(); + LibcallDecls.unique(); + for (std::list::const_iterator I = LibcallDecls.begin(); + I != LibcallDecls.end(); I++) { O << TAI->getExternDirective() << *I << "\n"; O << TAI->getExternDirective() << PAN::getArgsLabel(*I) << "\n"; O << TAI->getExternDirective() << PAN::getRetvalLabel(*I) << "\n"; @@ -188,17 +188,22 @@ bool PIC16AsmPrinter::doInitialization (Module &M) { I->setSection(TAI->SectionForGlobal(I)->getName()); } - EmitExternsAndGlobals (M); + EmitFunctionDecls(M); + EmitUndefinedVars(M); + EmitDefinedVars(M); EmitIData(M); EmitUData(M); - EmitAutos(M); EmitRomData(M); + EmitAutos(M); return Result; } -void PIC16AsmPrinter::EmitExternsAndGlobals (Module &M) { +// Emit extern decls for functions imported from other modules, and emit +// global declarations for function defined in this module and which are +// available to other modules. +void PIC16AsmPrinter::EmitFunctionDecls (Module &M) { // Emit declarations for external functions. - O << TAI->getCommentString() << "External defs and decls - BEGIN." <<"\n"; + O << TAI->getCommentString() << "Function Declarations - BEGIN." <<"\n"; for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) { std::string Name = Mang->getValueName(I); if (Name.compare("@abort") == 0) @@ -219,33 +224,38 @@ void PIC16AsmPrinter::EmitExternsAndGlobals (Module &M) { O << directive << PAN::getArgsLabel(Name) << "\n"; } - // Emit header file to include declaration of library functions - // FIXME: find out libcall names. - // O << "\t#include C16IntrinsicCalls.INC\n"; - - // Emit declarations for external variable declarations and definitions. - for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); - I != E; I++) { - // Any variables reaching here with ".auto." in its name is a local scope - // variable and should not be printed in global data section. - std::string Name = Mang->getValueName(I); - if (PAN::isLocalName(Name)) - continue; - - if (!(I->isDeclaration() || I->hasExternalLinkage() || - I->hasCommonLinkage())) - continue; - - const char *directive = I->isDeclaration() ? TAI->getExternDirective() : - TAI->getGlobalDirective(); - O << directive << Name << "\n"; - } - O << TAI->getCommentString() << "External defs and decls - END." <<"\n"; + O << TAI->getCommentString() << "Function Declarations - END." <<"\n"; } +// Emit variables imported from other Modules. +void PIC16AsmPrinter::EmitUndefinedVars (Module &M) +{ + std::vector Items = PTAI->ExternalVarDecls->Items; + if (! Items.size()) return; + + O << "\n" << TAI->getCommentString() << "Imported Variables - BEGIN" << "\n"; + for (unsigned j = 0; j < Items.size(); j++) { + O << TAI->getExternDirective() << Mang->getValueName(Items[j]) << "\n"; + } + O << TAI->getCommentString() << "Imported Variables - END" << "\n"; +} + +// Emit variables defined in this module and are available to other modules. +void PIC16AsmPrinter::EmitDefinedVars (Module &M) +{ + std::vector Items = PTAI->ExternalVarDefs->Items; + if (! Items.size()) return; + + O << "\n" << TAI->getCommentString() << "Exported Variables - BEGIN" << "\n"; + for (unsigned j = 0; j < Items.size(); j++) { + O << TAI->getGlobalDirective() << Mang->getValueName(Items[j]) << "\n"; + } + O << TAI->getCommentString() << "Exported Variables - END" << "\n"; +} + +// Emit initialized data placed in ROM. void PIC16AsmPrinter::EmitRomData (Module &M) { - const PIC16TargetAsmInfo *PTAI = static_cast(TAI); std::vector Items = PTAI->ROSection->Items; if (! Items.size()) return; @@ -262,7 +272,7 @@ void PIC16AsmPrinter::EmitRomData (Module &M) } bool PIC16AsmPrinter::doFinalization(Module &M) { - printDecls(); + printLibcallDecls(); O << "\t" << "END\n"; bool Result = AsmPrinter::doFinalization(M); return Result; @@ -314,7 +324,6 @@ void PIC16AsmPrinter::emitFunctionData(MachineFunction &MF) { } void PIC16AsmPrinter::EmitIData (Module &M) { - const PIC16TargetAsmInfo *PTAI = static_cast(TAI); // Print all IDATA sections. std::vector IDATASections = PTAI->IDATASections; @@ -333,7 +342,6 @@ void PIC16AsmPrinter::EmitIData (Module &M) { } void PIC16AsmPrinter::EmitUData (Module &M) { - const PIC16TargetAsmInfo *PTAI = static_cast(TAI); const TargetData *TD = TM.getTargetData(); // Print all BSS sections. @@ -358,7 +366,6 @@ void PIC16AsmPrinter::EmitAutos (Module &M) { // Section names for all globals are already set. - const PIC16TargetAsmInfo *PTAI = static_cast(TAI); const TargetData *TD = TM.getTargetData(); // Now print all Autos sections. diff --git a/lib/Target/PIC16/PIC16AsmPrinter.h b/lib/Target/PIC16/PIC16AsmPrinter.h index f021695eab6..32a2ebdca91 100644 --- a/lib/Target/PIC16/PIC16AsmPrinter.h +++ b/lib/Target/PIC16/PIC16AsmPrinter.h @@ -17,6 +17,7 @@ #include "PIC16.h" #include "PIC16TargetMachine.h" +#include "PIC16TargetAsmInfo.h" #include "llvm/CodeGen/AsmPrinter.h" #include "llvm/Support/CommandLine.h" #include "llvm/Target/TargetAsmInfo.h" @@ -31,6 +32,7 @@ namespace llvm { bool V) : AsmPrinter(O, TM, T, OL, V) { PTLI = TM.getTargetLowering(); + PTAI = static_cast (T); } private : virtual const char *getPassName() const { @@ -42,13 +44,15 @@ namespace llvm { void printCCOperand(const MachineInstr *MI, int opNum); bool printInstruction(const MachineInstr *MI); // definition autogenerated. bool printMachineInstruction(const MachineInstr *MI); - void EmitExternsAndGlobals (Module &M); + void EmitFunctionDecls (Module &M); + void EmitUndefinedVars (Module &M); + void EmitDefinedVars (Module &M); void EmitIData (Module &M); void EmitUData (Module &M); void EmitAutos (Module &M); void EmitRomData (Module &M); void emitFunctionData(MachineFunction &MF); - void printDecls(void); + void printLibcallDecls(void); protected: bool doInitialization(Module &M); @@ -56,7 +60,8 @@ namespace llvm { private: PIC16TargetLowering *PTLI; - std::list Decls; // List of extern decls. + const PIC16TargetAsmInfo *PTAI; + std::list LibcallDecls; // List of extern decls. }; } // end of namespace diff --git a/lib/Target/PIC16/PIC16TargetAsmInfo.cpp b/lib/Target/PIC16/PIC16TargetAsmInfo.cpp index 295188f0b41..9d7592a2e12 100644 --- a/lib/Target/PIC16/PIC16TargetAsmInfo.cpp +++ b/lib/Target/PIC16/PIC16TargetAsmInfo.cpp @@ -45,6 +45,8 @@ PIC16TargetAsmInfo(const PIC16TargetMachine &TM) // in BeginModule, and gpasm cribbs for that .text symbol. TextSection = getUnnamedSection("", SectionFlags::Code); ROSection = new PIC16Section(getReadOnlySection()); + ExternalVarDecls = new PIC16Section(getNamedSection("ExternalVarDecls")); + ExternalVarDefs = new PIC16Section(getNamedSection("ExternalVarDefs")); } const char *PIC16TargetAsmInfo::getRomDirective(unsigned size) const @@ -196,9 +198,18 @@ PIC16TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV1) const { // We select the section based on the initializer here, so it really // has to be a GlobalVariable. const GlobalVariable *GV = dyn_cast(GV1); - if (!GV1 || ! GV->hasInitializer()) + + if (!GV) return TargetAsmInfo::SelectSectionForGlobal(GV1); + // Record Exteranl Var Decls. + if (GV->isDeclaration()) { + ExternalVarDecls->Items.push_back(GV); + return ExternalVarDecls->S_; + } + + assert (GV->hasInitializer() && "A def without initializer?"); + // First, if this is an automatic variable for a function, get the section // name for it and return. const std::string name = GV->getName(); @@ -206,6 +217,11 @@ PIC16TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV1) const { return getSectionForAuto(GV); } + // Record Exteranl Var Defs. + if (GV->hasExternalLinkage() || GV->hasCommonLinkage()) { + ExternalVarDefs->Items.push_back(GV); + } + // See if this is an uninitialized global. const Constant *C = GV->getInitializer(); if (C->isNullValue()) @@ -240,4 +256,6 @@ PIC16TargetAsmInfo::~PIC16TargetAsmInfo() { } delete ROSection; + delete ExternalVarDecls; + delete ExternalVarDefs; } diff --git a/lib/Target/PIC16/PIC16TargetAsmInfo.h b/lib/Target/PIC16/PIC16TargetAsmInfo.h index a39209daaf6..e464e36f788 100644 --- a/lib/Target/PIC16/PIC16TargetAsmInfo.h +++ b/lib/Target/PIC16/PIC16TargetAsmInfo.h @@ -45,6 +45,8 @@ namespace llvm { mutable std::vector IDATASections; mutable std::vector AutosSections; mutable PIC16Section *ROSection; + mutable PIC16Section *ExternalVarDecls; + mutable PIC16Section *ExternalVarDefs; virtual ~PIC16TargetAsmInfo(); private: @@ -57,6 +59,8 @@ namespace llvm { const Section *getIDATASectionForGlobal(const GlobalVariable *GV) const; const Section *getSectionForAuto(const GlobalVariable *GV) const; virtual const Section *SelectSectionForGlobal(const GlobalValue *GV) const; + + public: void SetSectionForGVs(Module &M); std::vector getBSSSections() const {