Switch Sparc to new section handling stuff. Refactor printing of module-level GVs significantly.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@54450 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Anton Korobeynikov 2008-08-07 09:51:25 +00:00
parent 84e160e265
commit 5b794b98ce
4 changed files with 119 additions and 76 deletions

View File

@ -54,6 +54,7 @@ namespace {
return "Sparc Assembly Printer";
}
void printModuleLevelGV(const GlobalVariable* GVar);
void printOperand(const MachineInstr *MI, int opNum);
void printMemOperand(const MachineInstr *MI, int opNum,
const char *Modifier = 0);
@ -61,6 +62,7 @@ namespace {
bool printInstruction(const MachineInstr *MI); // autogenerated.
bool runOnMachineFunction(MachineFunction &F);
std::string getSectionForFunction(const Function &F) const;
bool doInitialization(Module &M);
bool doFinalization(Module &M);
};
@ -78,6 +80,11 @@ FunctionPass *llvm::createSparcCodePrinterPass(std::ostream &o,
return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo());
}
// Substitute old hook with new one temporary
std::string SparcAsmPrinter::getSectionForFunction(const Function &F) const {
return TAI->SectionForGlobal(&F);
}
/// runOnMachineFunction - This uses the printInstruction()
/// method to print assembly for each instruction.
///
@ -177,21 +184,21 @@ void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
const char *Modifier) {
printOperand(MI, opNum);
// If this is an ADD operand, emit it like normal operands.
if (Modifier && !strcmp(Modifier, "arith")) {
O << ", ";
printOperand(MI, opNum+1);
return;
}
if (MI->getOperand(opNum+1).isRegister() &&
MI->getOperand(opNum+1).getReg() == SP::G0)
return; // don't print "+%g0"
if (MI->getOperand(opNum+1).isImmediate() &&
MI->getOperand(opNum+1).getImm() == 0)
return; // don't print "+0"
O << "+";
if (MI->getOperand(opNum+1).isGlobalAddress() ||
MI->getOperand(opNum+1).isConstantPoolIndex()) {
@ -216,77 +223,90 @@ bool SparcAsmPrinter::doInitialization(Module &M) {
}
bool SparcAsmPrinter::doFinalization(Module &M) {
const TargetData *TD = TM.getTargetData();
// Print out module-level global variables here.
for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
I != E; ++I)
if (I->hasInitializer()) { // External global require no code
// Check to see if this is a special global used by LLVM, if so, emit it.
if (EmitSpecialLLVMGlobal(I))
continue;
O << "\n\n";
std::string name = Mang->getValueName(I);
Constant *C = I->getInitializer();
unsigned Size = TD->getABITypeSize(C->getType());
unsigned Align = TD->getPreferredAlignment(I);
printModuleLevelGV(I);
if (C->isNullValue() && (I->hasCommonLinkage() ||
I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
I->hasWeakLinkage() /* FIXME: Verify correct */)) {
SwitchToDataSection(".data", I);
if (I->hasInternalLinkage())
O << "\t.local " << name << "\n";
O << "\t.comm " << name << "," << TD->getABITypeSize(C->getType())
<< "," << Align;
O << "\n";
} else {
switch (I->getLinkage()) {
case GlobalValue::CommonLinkage:
case GlobalValue::LinkOnceLinkage:
case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
// Nonnull linkonce -> weak
O << "\t.weak " << name << "\n";
SwitchToDataSection("", I);
O << "\t.section\t\".llvm.linkonce.d." << name
<< "\",\"aw\",@progbits\n";
break;
case GlobalValue::AppendingLinkage:
// FIXME: appending linkage variables should go into a section of
// their name or something. For now, just emit them as external.
case GlobalValue::ExternalLinkage:
// If external or appending, declare as a global symbol
O << "\t.globl " << name << "\n";
// FALL THROUGH
case GlobalValue::InternalLinkage:
if (C->isNullValue())
SwitchToDataSection(".bss", I);
else
SwitchToDataSection(".data", I);
break;
case GlobalValue::GhostLinkage:
cerr << "Should not have any unmaterialized functions!\n";
abort();
case GlobalValue::DLLImportLinkage:
cerr << "DLLImport linkage is not supported by this target!\n";
abort();
case GlobalValue::DLLExportLinkage:
cerr << "DLLExport linkage is not supported by this target!\n";
abort();
default:
assert(0 && "Unknown linkage type!");
}
O << "\t.align " << Align << "\n";
O << "\t.type " << name << ",#object\n";
O << "\t.size " << name << "," << Size << "\n";
O << name << ":\n";
EmitGlobalConstant(C);
}
}
O << '\n';
return AsmPrinter::doFinalization(M);
}
void SparcAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
const TargetData *TD = TM.getTargetData();
if (!GVar->hasInitializer())
return; // External global require no code
// Check to see if this is a special global used by LLVM, if so, emit it.
if (EmitSpecialLLVMGlobal(GVar))
return;
O << "\n\n";
std::string SectionName = TAI->SectionForGlobal(GVar);
std::string name = Mang->getValueName(GVar);
Constant *C = GVar->getInitializer();
unsigned Size = TD->getABITypeSize(C->getType());
unsigned Align = TD->getPreferredAlignment(GVar);
// FIXME: ELF supports visibility
SwitchToDataSection(SectionName.c_str());
if (C->isNullValue() && !GVar->hasSection()) {
if (!GVar->isThreadLocal() &&
(GVar->hasInternalLinkage() || GVar->isWeakForLinker())) {
if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
if (GVar->hasInternalLinkage())
O << "\t.local " << name << "\n";
O << TAI->getCOMMDirective() << name << ',' << Size;
if (TAI->getCOMMDirectiveTakesAlignment())
O << ',' << (1 << Align);
O << '\n';
return;
}
}
switch (GVar->getLinkage()) {
case GlobalValue::CommonLinkage:
case GlobalValue::LinkOnceLinkage:
case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
// Nonnull linkonce -> weak
O << "\t.weak " << name << "\n";
break;
case GlobalValue::AppendingLinkage:
// FIXME: appending linkage variables should go into a section of
// their name or something. For now, just emit them as external.
case GlobalValue::ExternalLinkage:
// If external or appending, declare as a global symbol
O << TAI->getGlobalDirective() << name << "\n";
// FALL THROUGH
case GlobalValue::InternalLinkage:
break;
case GlobalValue::GhostLinkage:
cerr << "Should not have any unmaterialized functions!\n";
abort();
case GlobalValue::DLLImportLinkage:
cerr << "DLLImport linkage is not supported by this target!\n";
abort();
case GlobalValue::DLLExportLinkage:
cerr << "DLLExport linkage is not supported by this target!\n";
abort();
default:
assert(0 && "Unknown linkage type!");
}
if (Align)
O << "\t.align " << Align << "\n";
if (TAI->hasDotTypeDotSizeDirective()) {
O << "\t.type " << name << ",#object\n";
O << "\t.size " << name << "," << Size << "\n";
}
O << name << ":\n";
EmitGlobalConstant(C);
}

View File

@ -15,11 +15,31 @@
using namespace llvm;
SparcTargetAsmInfo::SparcTargetAsmInfo(const SparcTargetMachine &TM) {
SparcELFTargetAsmInfo::SparcELFTargetAsmInfo(const TargetMachine &TM):
ELFTargetAsmInfo(TM) {
Data16bitsDirective = "\t.half\t";
Data32bitsDirective = "\t.word\t";
Data64bitsDirective = 0; // .xword is only supported by V9.
ZeroDirective = "\t.skip\t";
CommentString = "!";
ConstantPoolSection = "\t.section \".rodata\",#alloc\n";
COMMDirectiveTakesAlignment = true;
}
std::string SparcELFTargetAsmInfo::PrintSectionFlags(unsigned flags) const {
std::string Flags = ",";
if (flags & SectionFlags::Mergeable)
return ELFTargetAsmInfo::PrintSectionFlags(flags);
if (!(flags & SectionFlags::Debug))
Flags += "#alloc";
if (flags & SectionFlags::Code)
Flags += "#execinstr";
if (flags & SectionFlags::Writeable)
Flags += "#write";
if (flags & SectionFlags::TLS)
Flags += "#tls";
return Flags;
}

View File

@ -15,17 +15,19 @@
#define SPARCTARGETASMINFO_H
#include "llvm/Target/TargetAsmInfo.h"
#include "llvm/Target/ELFTargetAsmInfo.h"
namespace llvm {
// Forward declaration.
class SparcTargetMachine;
class TargetMachine;
struct SparcTargetAsmInfo : public TargetAsmInfo {
explicit SparcTargetAsmInfo(const SparcTargetMachine &TM);
struct SparcELFTargetAsmInfo : public ELFTargetAsmInfo {
explicit SparcELFTargetAsmInfo(const TargetMachine &TM);
std::string PrintSectionFlags(unsigned flags) const;
};
} // namespace llvm
#endif

View File

@ -22,7 +22,8 @@ using namespace llvm;
static RegisterTarget<SparcTargetMachine> X("sparc", " SPARC");
const TargetAsmInfo *SparcTargetMachine::createTargetAsmInfo() const {
return new SparcTargetAsmInfo(*this);
// FIXME: Handle Solaris subtarget someday :)
return new SparcELFTargetAsmInfo(*this);
}
/// SparcTargetMachine ctor - Create an ILP32 architecture model