Run through the list of globals once and sectionize all types of globlas includeing declarations. Later emit them from their section lists.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@71661 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Sanjiv Gupta 2009-05-13 15:13:17 +00:00
parent e4eb2d2b57
commit ad6585b021
4 changed files with 76 additions and 42 deletions

View File

@ -133,7 +133,7 @@ void PIC16AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
// If its a libcall name, record it to decls section. // If its a libcall name, record it to decls section.
if (PAN::getSymbolTag(Sname) == PAN::LIBCALL) { if (PAN::getSymbolTag(Sname) == PAN::LIBCALL) {
Decls.push_back(Sname); LibcallDecls.push_back(Sname);
} }
O << Sname; O << Sname;
@ -153,16 +153,16 @@ void PIC16AsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
O << PIC16CondCodeToString((PIC16CC::CondCodes)CC); O << PIC16CondCodeToString((PIC16CC::CondCodes)CC);
} }
void PIC16AsmPrinter::printDecls(void) { void PIC16AsmPrinter::printLibcallDecls(void) {
// If no libcalls used, return. // If no libcalls used, return.
if (Decls.empty()) return; if (LibcallDecls.empty()) return;
O << TAI->getCommentString() << "External decls for libcalls - BEGIN." <<"\n"; O << TAI->getCommentString() << "External decls for libcalls - BEGIN." <<"\n";
// Remove duplicate entries. // Remove duplicate entries.
Decls.sort(); LibcallDecls.sort();
Decls.unique(); LibcallDecls.unique();
for (std::list<const char*>::const_iterator I = Decls.begin(); for (std::list<const char*>::const_iterator I = LibcallDecls.begin();
I != Decls.end(); I++) { I != LibcallDecls.end(); I++) {
O << TAI->getExternDirective() << *I << "\n"; O << TAI->getExternDirective() << *I << "\n";
O << TAI->getExternDirective() << PAN::getArgsLabel(*I) << "\n"; O << TAI->getExternDirective() << PAN::getArgsLabel(*I) << "\n";
O << TAI->getExternDirective() << PAN::getRetvalLabel(*I) << "\n"; O << TAI->getExternDirective() << PAN::getRetvalLabel(*I) << "\n";
@ -188,17 +188,22 @@ bool PIC16AsmPrinter::doInitialization (Module &M) {
I->setSection(TAI->SectionForGlobal(I)->getName()); I->setSection(TAI->SectionForGlobal(I)->getName());
} }
EmitExternsAndGlobals (M); EmitFunctionDecls(M);
EmitUndefinedVars(M);
EmitDefinedVars(M);
EmitIData(M); EmitIData(M);
EmitUData(M); EmitUData(M);
EmitAutos(M);
EmitRomData(M); EmitRomData(M);
EmitAutos(M);
return Result; 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. // 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++) { for (Module::iterator I = M.begin(), E = M.end(); I != E; I++) {
std::string Name = Mang->getValueName(I); std::string Name = Mang->getValueName(I);
if (Name.compare("@abort") == 0) if (Name.compare("@abort") == 0)
@ -219,33 +224,38 @@ void PIC16AsmPrinter::EmitExternsAndGlobals (Module &M) {
O << directive << PAN::getArgsLabel(Name) << "\n"; O << directive << PAN::getArgsLabel(Name) << "\n";
} }
// Emit header file to include declaration of library functions O << TAI->getCommentString() << "Function Declarations - END." <<"\n";
// 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";
} }
// Emit variables imported from other Modules.
void PIC16AsmPrinter::EmitUndefinedVars (Module &M)
{
std::vector<const GlobalVariable*> 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<const GlobalVariable*> 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) void PIC16AsmPrinter::EmitRomData (Module &M)
{ {
const PIC16TargetAsmInfo *PTAI = static_cast<const PIC16TargetAsmInfo *>(TAI);
std::vector<const GlobalVariable*> Items = PTAI->ROSection->Items; std::vector<const GlobalVariable*> Items = PTAI->ROSection->Items;
if (! Items.size()) return; if (! Items.size()) return;
@ -262,7 +272,7 @@ void PIC16AsmPrinter::EmitRomData (Module &M)
} }
bool PIC16AsmPrinter::doFinalization(Module &M) { bool PIC16AsmPrinter::doFinalization(Module &M) {
printDecls(); printLibcallDecls();
O << "\t" << "END\n"; O << "\t" << "END\n";
bool Result = AsmPrinter::doFinalization(M); bool Result = AsmPrinter::doFinalization(M);
return Result; return Result;
@ -314,7 +324,6 @@ void PIC16AsmPrinter::emitFunctionData(MachineFunction &MF) {
} }
void PIC16AsmPrinter::EmitIData (Module &M) { void PIC16AsmPrinter::EmitIData (Module &M) {
const PIC16TargetAsmInfo *PTAI = static_cast<const PIC16TargetAsmInfo *>(TAI);
// Print all IDATA sections. // Print all IDATA sections.
std::vector <PIC16Section *>IDATASections = PTAI->IDATASections; std::vector <PIC16Section *>IDATASections = PTAI->IDATASections;
@ -333,7 +342,6 @@ void PIC16AsmPrinter::EmitIData (Module &M) {
} }
void PIC16AsmPrinter::EmitUData (Module &M) { void PIC16AsmPrinter::EmitUData (Module &M) {
const PIC16TargetAsmInfo *PTAI = static_cast<const PIC16TargetAsmInfo *>(TAI);
const TargetData *TD = TM.getTargetData(); const TargetData *TD = TM.getTargetData();
// Print all BSS sections. // Print all BSS sections.
@ -358,7 +366,6 @@ void PIC16AsmPrinter::EmitAutos (Module &M)
{ {
// Section names for all globals are already set. // Section names for all globals are already set.
const PIC16TargetAsmInfo *PTAI = static_cast<const PIC16TargetAsmInfo *>(TAI);
const TargetData *TD = TM.getTargetData(); const TargetData *TD = TM.getTargetData();
// Now print all Autos sections. // Now print all Autos sections.

View File

@ -17,6 +17,7 @@
#include "PIC16.h" #include "PIC16.h"
#include "PIC16TargetMachine.h" #include "PIC16TargetMachine.h"
#include "PIC16TargetAsmInfo.h"
#include "llvm/CodeGen/AsmPrinter.h" #include "llvm/CodeGen/AsmPrinter.h"
#include "llvm/Support/CommandLine.h" #include "llvm/Support/CommandLine.h"
#include "llvm/Target/TargetAsmInfo.h" #include "llvm/Target/TargetAsmInfo.h"
@ -31,6 +32,7 @@ namespace llvm {
bool V) bool V)
: AsmPrinter(O, TM, T, OL, V) { : AsmPrinter(O, TM, T, OL, V) {
PTLI = TM.getTargetLowering(); PTLI = TM.getTargetLowering();
PTAI = static_cast<const PIC16TargetAsmInfo *> (T);
} }
private : private :
virtual const char *getPassName() const { virtual const char *getPassName() const {
@ -42,13 +44,15 @@ namespace llvm {
void printCCOperand(const MachineInstr *MI, int opNum); void printCCOperand(const MachineInstr *MI, int opNum);
bool printInstruction(const MachineInstr *MI); // definition autogenerated. bool printInstruction(const MachineInstr *MI); // definition autogenerated.
bool printMachineInstruction(const MachineInstr *MI); 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 EmitIData (Module &M);
void EmitUData (Module &M); void EmitUData (Module &M);
void EmitAutos (Module &M); void EmitAutos (Module &M);
void EmitRomData (Module &M); void EmitRomData (Module &M);
void emitFunctionData(MachineFunction &MF); void emitFunctionData(MachineFunction &MF);
void printDecls(void); void printLibcallDecls(void);
protected: protected:
bool doInitialization(Module &M); bool doInitialization(Module &M);
@ -56,7 +60,8 @@ namespace llvm {
private: private:
PIC16TargetLowering *PTLI; PIC16TargetLowering *PTLI;
std::list<const char *> Decls; // List of extern decls. const PIC16TargetAsmInfo *PTAI;
std::list<const char *> LibcallDecls; // List of extern decls.
}; };
} // end of namespace } // end of namespace

View File

@ -45,6 +45,8 @@ PIC16TargetAsmInfo(const PIC16TargetMachine &TM)
// in BeginModule, and gpasm cribbs for that .text symbol. // in BeginModule, and gpasm cribbs for that .text symbol.
TextSection = getUnnamedSection("", SectionFlags::Code); TextSection = getUnnamedSection("", SectionFlags::Code);
ROSection = new PIC16Section(getReadOnlySection()); ROSection = new PIC16Section(getReadOnlySection());
ExternalVarDecls = new PIC16Section(getNamedSection("ExternalVarDecls"));
ExternalVarDefs = new PIC16Section(getNamedSection("ExternalVarDefs"));
} }
const char *PIC16TargetAsmInfo::getRomDirective(unsigned size) const 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 // We select the section based on the initializer here, so it really
// has to be a GlobalVariable. // has to be a GlobalVariable.
const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1); const GlobalVariable *GV = dyn_cast<GlobalVariable>(GV1);
if (!GV1 || ! GV->hasInitializer())
if (!GV)
return TargetAsmInfo::SelectSectionForGlobal(GV1); 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 // First, if this is an automatic variable for a function, get the section
// name for it and return. // name for it and return.
const std::string name = GV->getName(); const std::string name = GV->getName();
@ -206,6 +217,11 @@ PIC16TargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV1) const {
return getSectionForAuto(GV); return getSectionForAuto(GV);
} }
// Record Exteranl Var Defs.
if (GV->hasExternalLinkage() || GV->hasCommonLinkage()) {
ExternalVarDefs->Items.push_back(GV);
}
// See if this is an uninitialized global. // See if this is an uninitialized global.
const Constant *C = GV->getInitializer(); const Constant *C = GV->getInitializer();
if (C->isNullValue()) if (C->isNullValue())
@ -240,4 +256,6 @@ PIC16TargetAsmInfo::~PIC16TargetAsmInfo() {
} }
delete ROSection; delete ROSection;
delete ExternalVarDecls;
delete ExternalVarDefs;
} }

View File

@ -45,6 +45,8 @@ namespace llvm {
mutable std::vector<PIC16Section *> IDATASections; mutable std::vector<PIC16Section *> IDATASections;
mutable std::vector<PIC16Section *> AutosSections; mutable std::vector<PIC16Section *> AutosSections;
mutable PIC16Section *ROSection; mutable PIC16Section *ROSection;
mutable PIC16Section *ExternalVarDecls;
mutable PIC16Section *ExternalVarDefs;
virtual ~PIC16TargetAsmInfo(); virtual ~PIC16TargetAsmInfo();
private: private:
@ -57,6 +59,8 @@ namespace llvm {
const Section *getIDATASectionForGlobal(const GlobalVariable *GV) const; const Section *getIDATASectionForGlobal(const GlobalVariable *GV) const;
const Section *getSectionForAuto(const GlobalVariable *GV) const; const Section *getSectionForAuto(const GlobalVariable *GV) const;
virtual const Section *SelectSectionForGlobal(const GlobalValue *GV) const; virtual const Section *SelectSectionForGlobal(const GlobalValue *GV) const;
public: public:
void SetSectionForGVs(Module &M); void SetSectionForGVs(Module &M);
std::vector<PIC16Section *> getBSSSections() const { std::vector<PIC16Section *> getBSSSections() const {